From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 9EBF93858407; Fri, 6 May 2022 02:51:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EBF93858407 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/testsuite] Fix gdb.dwarf2/locexpr-data-member-location.exp with nopie X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 2392dc0f8e243a4f5f9d0db6cee64a1698886e56 X-Git-Newrev: 2899c914f476b549280167e8c52067c4422916b5 Message-Id: <20220506025146.9EBF93858407@sourceware.org> Date: Fri, 6 May 2022 02:51:46 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 May 2022 02:51:46 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D2899c914f476= b549280167e8c52067c4422916b5 commit 2899c914f476b549280167e8c52067c4422916b5 Author: Tom de Vries Date: Fri May 6 04:51:43 2022 +0200 [gdb/testsuite] Fix gdb.dwarf2/locexpr-data-member-location.exp with no= pie =20 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 ... =20 The problem is that the test-case tries to mimic some gdb_compile_shlib behaviour using: ... set flags {additional_flags=3D-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 -m= 32 \ -o func_addr23029.x ... while gdb_compile_shlib properly filters out the -fno-PIE -no-pie. =20 Consequently, the address for foo determined by get_func_info doesn't m= atch the actual address of foo. =20 Fix this by printing the address of foo using the result of gdb_compile= _shlib. Diff: --- .../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 =3D foo (); -} -#endif diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp b/gd= b/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 =20 -# 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. =20 -standard_testfile $lib_basename.c $main_basename.c $lib_basename.S +standard_testfile $main_basename.c $lib_basename.c $lib_basename.S =20 -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}] =20 -# 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 debuggi= ng +# symbols will be present for this compilation, because we want to print s= ome +# type information. if {[gdb_compile_shlib $libsrc $lib_so \ - {additional_flags=3D-DIS_SHAREDLIB debug}] !=3D ""}= { + {debug}] !=3D ""} { untested "failed to compile shared library" return } =20 -# 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 usi= ng +# debug, such that "finish out of foo" prints: +# Value returned is $1 =3D (class B *) $hex +# instead of: +# Value returned is $1 =3D (B *) $hex +# Note that this compilation is used for all GDB sessions. +set exec_options [list debug shlib=3D$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} + =20 -# 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. =20 -if { $long_size =3D=3D -1 || $addr_size =3D=3D -1 \ - || $struct_A_size =3D=3D -1 || $struct_B_size =3D=3D -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 } =20 -# 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] } =20 -# 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 =3D=3D -1 || $addr_size =3D=3D -1 \ + || $struct_A_size =3D=3D -1 || $struct_B_size =3D=3D -1} { + perror "Can't determine type sizes" + return +} =20 # 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=3D-fpic debug} - get_func_info foo $flags - get_func_info bar $flags + global foo_start foo_end + global bar_start bar_end + global libsrc =20 cu { label cu_label } { DW_TAG_compile_unit { @@ -263,17 +273,17 @@ Dwarf::assemble ${asm_file} { =20 lines {version 2} L { include_dir "${::srcdir}/${::subdir}" - file_name "${::srcfile}" 1 + file_name "${::srcfile2}" 1 =20 # 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 =20 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} { } =20 # 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" opti= on. +# 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=3D-DIS_SHAREDLIB nodebug}] !=3D "= "} { + {nodebug}] !=3D ""} { untested "failed to compile shared library" return } =20 -# Compile the main program for use with the shared object. -if [prepare_for_testing "failed to prepare" ${testfile} \ - ${::srcfile2} [list debug shlib=3D$lib_so]] { - return -1 -} +### Second GDB session. =20 -# 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} =20 @@ -347,6 +353,9 @@ gdb_test "p *$" { =3D { =3D {a =3D 8, x =3D 9}, b = =3D 10, x2 =3D 11}} \ # so we'll do the same here: gdb_test "step" "bar \\(.*" "step into bar" =20 + +### 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 {} { } } =20 +# 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. =20 proc shared_gdb_start_use { src options } { @@ -299,9 +311,7 @@ proc shared_gdb_start_use { src options } { gdb_load "$exe" =20 if { $shared_gdb_enabled } { - set shared_gdb_started 1 - set shared_gdb_src $src - set shared_gdb_options $options + share_gdb $src $options } } }