From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 6IawIe5rcmJDOQQAWB0awg (envelope-from ) for ; Wed, 04 May 2022 08:05:02 -0400 Received: by simark.ca (Postfix, from userid 112) id 86E621E058; Wed, 4 May 2022 08:05:02 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=K0CTOMT+; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 75B131E00E for ; Wed, 4 May 2022 08:05:01 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4C7B3856274 for ; Wed, 4 May 2022 12:05:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4C7B3856274 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1651665900; bh=DLmjC7I3DCS57E9XS3tD0h7nC/ziD+F50UV5VjOpGQM=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=K0CTOMT+IhkLFWJfsx1vWw7a+y9kFavVmzib3Bgn8lDMrvwKcsAVeErMGEyBeoC73 jm8Lm9a2kuxawV3CbiZQv1gaWQgqBDySf9iMHXlLh5isdRmCdtkiS/wLuhGLo8NWLM EI6MVSKV3W6DK/+NBaKzKcJdYPeFUXixyGow5UR4= Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 47B5E384B077 for ; Wed, 4 May 2022 12:00:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 47B5E384B077 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7C9B9210E5; Wed, 4 May 2022 12:00:49 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 6295C131BD; Wed, 4 May 2022 12:00:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id F3T0FvFqcmJXTwAAMHmgww (envelope-from ); Wed, 04 May 2022 12:00:49 +0000 Date: Wed, 4 May 2022 14:00:47 +0200 To: gdb-patches@sourceware.org Subject: [PATCH][gdb/testsuite] Fix gdb.dwarf2/locexpr-data-member-location.exp with nopie Message-ID: <20220504120046.GA8717@delia.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Tom de Vries via Gdb-patches Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Hi, When running test-case gdb.dwarf2/locexpr-data-member-location.exp with target board unix/-fno-PIE/-no-pie/-m32 I run into: ... (gdb) step^M 26 return 0;^M (gdb) FAIL: gdb.dwarf2/locexpr-data-member-location.exp: step into foo ... The problem is that the test-case tries to mimic some gdb_compile_shlib behaviour using: ... set flags {additional_flags=-fpic debug} get_func_info foo $flags ... but this doesn't work with the target board setting, because we end up doing: ... gcc locexpr-data-member-location-lib.c -fpic -g -lm -fno-PIE -no-pie -m32 \ -o func_addr23029.x ... while gdb_compile_shlib properly filters out the -fno-PIE -no-pie. Consequently, the address for foo determined by get_func_info doesn't match the actual address of foo. Fix this by printing the address of foo using the result of gdb_compile_shlib. Any comments? Thanks, - Tom [gdb/testsuite] Fix gdb.dwarf2/locexpr-data-member-location.exp with nopie --- .../gdb.dwarf2/locexpr-data-member-location-lib.c | 13 -- .../gdb.dwarf2/locexpr-data-member-location.exp | 155 +++++++++++---------- gdb/testsuite/lib/dwarf.exp | 16 ++- 3 files changed, 95 insertions(+), 89 deletions(-) diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c index 80170863800..880837f6a34 100644 --- a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c +++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c @@ -33,16 +33,3 @@ bar (B *v) asm ("bar_label: .globl bar_label"); return v; /* bar return */ } /* bar end */ - -/* Some of the DWARF assembler procs (e.g. function_range) compile - this file, expecting it to be a complete program with a main() - function. When IS_SHAREDLIB is NOT defined, we have main() as - defined below. */ - -#ifndef IS_SHAREDLIB -int -main () -{ - B *b = foo (); -} -#endif diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp index adb4e0a4c21..8e924eac57f 100644 --- a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp +++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp @@ -66,83 +66,93 @@ if ![dwarf2_support] { set main_basename ${::gdb_test_file_name}-main set lib_basename ${::gdb_test_file_name}-lib -# We're generating DWARF assembly for the shared object; therefore, -# the source file for the library / shared object must be listed first -# (in the standard_testfile invocation) since ${srcfile} is used by -# get_func_info (for determining the start, end, and length of a -# function). -# +# We're generating DWARF assembly for the shared object; # The output of Dwarf::assemble will be placed in $lib_basename.S # which will be ${srcfile3} after the execution of standard_testfile. -standard_testfile $lib_basename.c $main_basename.c $lib_basename.S +standard_testfile $main_basename.c $lib_basename.c $lib_basename.S -set libsrc "${::srcdir}/${::subdir}/${::srcfile}" +set libsrc "${::srcdir}/${::subdir}/${::srcfile2}" set lib_so [standard_output_file ${lib_basename}.so] set asm_file [standard_output_file ${::srcfile3}] -# We need to know the size of some types in order to write some of the -# debugging info that we're about to generate. For that, we ask GDB -# by debugging the shared object associated with this test case. - -# Compile the shared library: -DIS_SHAREDLIB prevents main() from -# being defined. Note that debugging symbols will be present for -# this compilation. +# Compile the shared library for the first GDB session. Note that debugging +# symbols will be present for this compilation, because we want to print some +# type information. if {[gdb_compile_shlib $libsrc $lib_so \ - {additional_flags=-DIS_SHAREDLIB debug}] != ""} { + {debug}] != ""} { untested "failed to compile shared library" return } -# Start a fresh GDB and load the shared library. -clean_restart $lib_so +# Compile the main program for use with the shared object. Note we're using +# debug, such that "finish out of foo" prints: +# Value returned is $1 = (class B *) $hex +# instead of: +# Value returned is $1 = (B *) $hex +# Note that this compilation is used for all GDB sessions. +set exec_options [list debug shlib=$lib_so] +if [prepare_for_testing "failed to prepare" ${testfile} \ + ${::srcfile} $exec_options] { + return -1 +} + +# Do whatever is necessary to make sure that the shared library is +# loaded for remote targets. +gdb_load_shlib ${lib_so} + -# Using our running GDB session, determine sizes of several types. -set long_size [get_sizeof "long" -1] -set addr_size [get_sizeof "void *" -1] -set struct_A_size [get_sizeof "g_A" -1] -set struct_B_size [get_sizeof "g_B" -1] +### First GDB session. -if { $long_size == -1 || $addr_size == -1 \ - || $struct_A_size == -1 || $struct_B_size == -1} { - perror "Can't determine type sizes" +# Run to foo to make sure foo refers to the function, and not foo@PLT. +if ![runto foo qualified] then { return } -# Retrieve struct offset of MBR in struct TP -proc get_offsetof { tp mbr } { - return [get_integer_valueof "&((${tp} *) 0)->${mbr}" -1] +with_shared_gdb { + + set session_options $exec_options + + # Rather than start a new session, declare the current session the + # shared one. Otherwise, get_func_info would compile an executable + # in a temp dir, which means -Wl,-rpath,\\\$ORIGIN no longer finds + # the shared lib. + share_gdb ${srcdir}/${subdir}/$srcfile $session_options + + get_func_info foo $session_options + get_func_info bar $session_options + + # Using our running GDB session, determine sizes of several types. + set long_size [get_sizeof "long" -1] + set addr_size [get_sizeof "void *" -1] + set struct_A_size [get_sizeof "g_A" -1] + set struct_B_size [get_sizeof "g_B" -1] + + # Retrieve struct offset of MBR in struct TP + proc get_offsetof { tp mbr } { + return [get_integer_valueof "&((${tp} *) 0)->${mbr}" -1] + } + + # Use running GDB session to get struct offsets + set A_a [get_offsetof A a] + set A_x [get_offsetof A x] + set B_a [get_offsetof B a] + set B_b [get_offsetof B b] + set B_x2 [get_offsetof B x2] } -# Use running GDB session to get struct offsets -set A_a [get_offsetof A a] -set A_x [get_offsetof A x] -set B_a [get_offsetof B a] -set B_b [get_offsetof B b] -set B_x2 [get_offsetof B x2] +if { $long_size == -1 || $addr_size == -1 \ + || $struct_A_size == -1 || $struct_B_size == -1} { + perror "Can't determine type sizes" + return +} # Create the DWARF. Dwarf::assemble ${asm_file} { declare_labels L - - # Find start, end, and length of functions foo and bar. - # These calls to get_func_info will create and set variables - # foo_start, bar_start, foo_end, bar_end, foo_len, and - # bar_len. - # - # In order to get the right answers, get_func_info (and, - # underneath, function_range) should use the same compiler flags - # as those used to make a shared object. For any targets that get - # this far, -fpic is probably correct. - # - # Also, it should be noted that IS_SHAREDLIB is NOT defined as one - # of the additional flags. Not defining IS_SHAREDLIB will cause a - # main() to be defined for the compilation of the shared library - # source file which happens as a result of using get_func_info; - # this is currently required in order to this facility. - set flags {additional_flags=-fpic debug} - get_func_info foo $flags - get_func_info bar $flags + global foo_start foo_end + global bar_start bar_end + global libsrc cu { label cu_label } { DW_TAG_compile_unit { @@ -263,17 +273,17 @@ Dwarf::assemble ${asm_file} { lines {version 2} L { include_dir "${::srcdir}/${::subdir}" - file_name "${::srcfile}" 1 + file_name "${::srcfile2}" 1 # Generate a line table program. program { DW_LNE_set_address $foo_start - line [gdb_get_line_number "foo prologue"] + line [gdb_get_line_number "foo prologue" $libsrc] DW_LNS_copy DW_LNE_set_address foo_label - line [gdb_get_line_number "foo return"] + line [gdb_get_line_number "foo return" $libsrc] DW_LNS_copy - line [gdb_get_line_number "foo end"] + line [gdb_get_line_number "foo end" $libsrc] DW_LNS_copy DW_LNE_set_address $foo_end DW_LNS_advance_line 1 @@ -281,12 +291,12 @@ Dwarf::assemble ${asm_file} { DW_LNE_end_sequence DW_LNE_set_address $bar_start - line [gdb_get_line_number "bar prologue"] + line [gdb_get_line_number "bar prologue" $libsrc] DW_LNS_copy DW_LNE_set_address bar_label - line [gdb_get_line_number "bar return"] + line [gdb_get_line_number "bar return" $libsrc] DW_LNS_copy - line [gdb_get_line_number "bar end"] + line [gdb_get_line_number "bar end" $libsrc] DW_LNS_copy DW_LNE_set_address $bar_end DW_LNS_advance_line 1 @@ -305,24 +315,20 @@ Dwarf::assemble ${asm_file} { } # Compile the shared object again, but this time include / use the -# DWARF info that we've created above. Note that (again) -# -DIS_SHAREDLIB is used to prevent inclusion of main() in the shared -# object. Also note the use of the "nodebug" option. Any debugging -# information that we need will be provided by the DWARF info created -# above. +# DWARF info that we've created above. Note the use of the "nodebug" option. +# Any debugging information that we need will be provided by the DWARF info +# created above. if {[gdb_compile_shlib [list $libsrc $asm_file] $lib_so \ - {additional_flags=-DIS_SHAREDLIB nodebug}] != ""} { + {nodebug}] != ""} { untested "failed to compile shared library" return } -# Compile the main program for use with the shared object. -if [prepare_for_testing "failed to prepare" ${testfile} \ - ${::srcfile2} [list debug shlib=$lib_so]] { - return -1 -} +### Second GDB session. -# Do whatever is necessary to make sure that the shared library is +clean_restart $binfile + +# Again, do whatever is necessary to make sure that the shared library is # loaded for remote targets. gdb_load_shlib ${lib_so} @@ -347,6 +353,9 @@ gdb_test "p *$" { = { = {a = 8, x = 9}, b = 10, x2 = 11}} \ # so we'll do the same here: gdb_test "step" "bar \\(.*" "step into bar" + +### Third GDB session. + # We don't want a clean restart here since that will be too clean. # The original reproducer for PR28030 set a breakpoint in the shared # library and then restarted via "run". The command below does roughly diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp index 55e97c33a6e..b05fdd58f66 100644 --- a/gdb/testsuite/lib/dwarf.exp +++ b/gdb/testsuite/lib/dwarf.exp @@ -268,6 +268,18 @@ proc shared_gdb_disable {} { } } +# Share the current gdb session. + +proc share_gdb { src options } { + global shared_gdb_started + global shared_gdb_src + global shared_gdb_options + + set shared_gdb_started 1 + set shared_gdb_src $src + set shared_gdb_options $options +} + # See above. proc shared_gdb_start_use { src options } { @@ -299,9 +311,7 @@ proc shared_gdb_start_use { src options } { gdb_load "$exe" if { $shared_gdb_enabled } { - set shared_gdb_started 1 - set shared_gdb_src $src - set shared_gdb_options $options + share_gdb $src $options } } }