1 # Copyright 2010-2013 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This test only works on GNU/Linux.
17 if { ![isnative] || [is_remote host] || [target_info exists use_gdb_stub]
18 || ![istarget *-linux*] || [skip_shlib_tests]} {
22 load_lib prelink-support.exp
24 set test "break-interp"
25 set binprefix ${objdir}/${subdir}/${test}
26 # Only to get the $interp_system name.
27 set srcfile_test "start.c"
28 set binfile_test ${test}-test
29 set binfile_lib ${objdir}/${subdir}/${test}.so
30 set srcfile "${test}-main.c"
31 set srcfile_lib "${test}-lib.c"
33 if [get_compiler_info] {
37 # Use -soname so that the new library gets copied by build_executable_own_libs.
39 if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug ldflags=-Wl,-soname,${test}.so]] != ""} {
43 if {[build_executable ${test}.exp $binfile_test ${srcfile_test} {}] == -1} {
47 # Note: The separate debug info file content build-id/crc32 are not verified
48 # contrary to the GDB search algorithm skipping non-matching ones.
49 proc system_debug_get {exec} {
52 set exec_build_id_debug [build_id_debug_filename_get $exec]
53 set debug_base "[file tail $exec].debug"
54 set exec_dir [file dirname $exec]
56 # isfile returns 1 even for symlinks to files.
57 set retval $debug_root/$exec_build_id_debug
58 if [file isfile $retval] {
61 set retval $exec_dir/$debug_base
62 if [file isfile $retval] {
65 set retval $exec_dir/.debug/$debug_base
66 if [file isfile $retval] {
69 set retval $debug_root/$exec_dir/$debug_base
70 if [file isfile $retval] {
79 set test "show debug-file-directory"
80 gdb_test_multiple $test $test {
81 -re "The directory where separate debug symbols are searched for is \"(.*)\".\r\n$gdb_prompt $" {
82 set debug_root $expect_out(1,string)
86 set interp_system [section_get ${objdir}/${subdir}/$binfile_test .interp]
87 set interp_system_debug [system_debug_get $interp_system]
88 verbose -log "$interp_system has debug $interp_system_debug"
90 proc prelinkNO {arg {name ""}} {
91 return [prelink_no $arg $name]
94 proc prelinkYES {arg {name ""}} {
95 return [prelink_yes $arg $name]
98 proc strip_debug {dest} {
99 set test "strip [file tail $dest]"
100 set strip_program [transform strip]
101 set command "exec $strip_program --strip-debug $dest"
102 verbose -log "command is $command"
103 if [catch $command] {
112 # The marker function for the standard runtime linker interface is
113 # _dl_debug_state. The probes-based interface has no specific marker
114 # function; the probe we will stop on (init_start) is in dl_main so we
117 set solib_bp {(_dl_debug_state|dl_main)}
119 # Implementation of reach.
121 proc reach_1 {func command displacement} {
122 global gdb_prompt expect_out solib_bp
124 if {$func == $solib_bp} {
125 # Breakpoint on _dl_debug_state can have problems due to its overlap
126 # with the existing internal breakpoint from GDB.
127 gdb_test_no_output "set stop-on-solib-events 1"
128 } elseif {! [gdb_breakpoint $func allow-pending]} {
133 set test_displacement "seen displacement message as $displacement"
134 set debug_state_count 0
135 gdb_test_multiple $command $test {
136 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
137 # Missing "$gdb_prompt $" is intentional.
138 if {$expect_out(1,string) == "0x0"} {
143 if {$displacement == $case || $displacement == "PRESENT"} {
144 pass $test_displacement
145 set displacement "FOUND-$displacement"
147 fail $test_displacement
151 -re "Breakpoint \[0-9\]+, \\.?(__GI_)?$func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
152 if {$func == $solib_bp} {
158 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in \\.?(__GI_)?$func \\(\\).*\r\n$gdb_prompt $" {
159 if {$func == $solib_bp} {
165 -re "Stopped due to (spurious )?shared library event.*\r\n$gdb_prompt $" {
166 if {$func == $solib_bp} {
167 if {$debug_state_count == 0} {
168 # First stop does not yet relocate the _start function
169 # descriptor on ppc64.
170 set debug_state_count 1
171 send_gdb "continue\n"
181 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
182 fail $test_displacement
185 if {$func == $solib_bp} {
186 gdb_test_no_output "set stop-on-solib-events 0"
190 # `runto' does not check we stopped really at the function we specified.
191 # DISPLACEMENT can be "NONE" for no message to be present, "ZERO" for
192 # displacement of 0 bytes to be present, "NONZERO" for displacement of non-0
193 # bytes to be present and "PRESENT" if both "ZERO" and "NONZERO" are valid.
194 proc reach {func command displacement} {
195 with_test_prefix "reach-$func" {
196 reach_1 $func $command $displacement
200 proc test_core {file displacement} {
201 with_test_prefix "core" {
202 global srcdir subdir gdb_prompt expect_out
204 set corefile [core_find $file {} "segv"]
205 if {$corefile == ""} {
211 # Clear it to never find any separate debug infos in $debug_root.
212 gdb_test_no_output "set debug-file-directory" \
213 "set debug-file-directory for core"
214 gdb_reinitialize_dir $srcdir/$subdir
217 # Print the "PIE (Position Independent Executable) displacement" message.
218 gdb_test_no_output "set verbose on"
220 set test "core loaded"
221 set test_displacement "seen displacement message as $displacement"
222 gdb_test_multiple "core-file $corefile" $test {
223 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
224 # Missing "$gdb_prompt $" is intentional.
225 if {$expect_out(1,string) == "0x0"} {
230 if {$displacement == $case || $displacement == "PRESENT"} {
231 pass $test_displacement
232 set displacement "FOUND-$displacement"
234 fail $test_displacement
238 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
239 # Do not check the binary filename as it may be truncated.
243 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
244 fail $test_displacement
247 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
251 proc test_attach_gdb {file pid displacement prefix} {
252 with_test_prefix "$prefix" {
253 global gdb_prompt expect_out
258 # Print the "PIE (Position Independent Executable) displacement" message.
259 gdb_test_no_output "set verbose on"
261 gdb_test "file $file" "Reading symbols from .*done\\." "file"
264 gdb_test_multiple "attach $pid" $test {
265 -re "Attaching to (program: .*, )?process $pid\r\n" {
266 # Missing "$gdb_prompt $" is intentional.
271 set test "attach final prompt"
272 set test_displacement "seen displacement message as $displacement"
273 gdb_test_multiple "" $test {
274 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
275 # Missing "$gdb_prompt $" is intentional.
276 if {$expect_out(1,string) == "0x0"} {
281 if {$displacement == $case || $displacement == "PRESENT"} {
282 pass $test_displacement
283 set displacement "FOUND-$displacement"
285 fail $test_displacement
289 -re "$gdb_prompt $" {
293 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
294 fail $test_displacement
297 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
302 proc test_attach {file displacement {relink_args ""}} {
308 set test "sleep function started"
310 set command "${file} sleep"
311 set res [remote_spawn host $command]
312 if { $res < 0 || $res == "" } {
313 perror "Spawning $command failed."
317 set pid [exp_pid -i $res]
327 fail "$test (timeout)"
332 if {$relink_args == ""} {
333 test_attach_gdb $exec $pid $displacement "attach"
335 # These could be rather passed as arguments.
336 global interp_saved interp
338 foreach relink {YES NO} {
339 # Formerly this test was testing only prelinking of $EXEC. As the
340 # prelink command automatically prelinks all of $EXEC's libraries,
341 # even $INTERP got prelinked. Therefore, we formerly had to
342 # `[file_copy $interp_saved $interp]' to make $INTERP not affected
343 # by this prelinking of $EXEC.
345 # But now we need to test even prelinking of $INTERP. We could
346 # create a separate test to test just the $INTERP prelinking. For
347 # test simplicity, we merged this test and the test above by not
348 # restoring $INTERP after $EXEC prelink. $INTERP gets restored
351 # `(wrong library or version mismatch?)' messages are printed for
352 # $binfile_lib on platforms converting REL->RELA relocations by
353 # prelink (such as on i386). There is no reliable way to verify
354 # the library file matches the running library in such case but
355 # GDB at least attempts to set the right displacement. We test
356 # `libfunc' is present in the backtrace and therefore the
357 # displacement has been guessed right.
359 if [prelink$relink $relink_args [file tail $exec]] {
360 # /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
361 test_attach_gdb $exec $pid $displacement "attach-relink$relink"
364 file_copy $interp_saved $interp
367 remote_exec host "kill -9 $pid"
370 proc test_ld {file ifmain trynosym displacement} {
371 global srcdir subdir gdb_prompt expect_out inferior_exited_re solib_bp
373 # First test normal `file'-command loaded $FILE with symbols.
377 # Clear it to never find any separate debug infos in $debug_root.
378 gdb_test_no_output "set debug-file-directory"
379 gdb_reinitialize_dir $srcdir/$subdir
382 # Print the "PIE (Position Independent Executable) displacement" message.
383 gdb_test_no_output "set verbose on"
385 # We want to test the re-run of a PIE in the case where the executable
386 # is loaded with a different displacement, but disable-randomization
387 # prevents that from happening. So turn it off.
388 gdb_test "set disable-randomization off"
391 gdb_test_no_output "set args segv"
393 global objdir binfile_test
395 # ld.so needs some executable to run to reach _dl_debug_state.
396 gdb_test_no_output "set args ${objdir}/${subdir}/$binfile_test" "set args OBJDIR/${subdir}/$binfile_test"
399 reach $solib_bp "run" $displacement
401 gdb_test "bt" "#0 +\[^\r\n\]*\\m(__GI_)?$solib_bp\\M.*" "dl bt"
404 reach "main" continue "NONE"
406 reach "libfunc" continue "NONE"
408 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
411 # Try re-run if the new PIE displacement takes effect.
412 gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
413 reach $solib_bp "run" $displacement
416 test_core $file $displacement
418 test_attach $file $displacement
425 with_test_prefix "symbol-less" {
426 # Test also `exec-file'-command loaded $FILE - therefore
427 # without symbols. SYMBOL_OBJFILE is not available and only
428 # EXEC_BFD must be used.
432 # Clear it to never find any separate debug infos in $debug_root.
433 gdb_test_no_output "set debug-file-directory"
434 gdb_reinitialize_dir $srcdir/$subdir
436 # Print the "PIE (Position Independent Executable)
437 # displacement" message.
438 gdb_test_no_output "set verbose on"
440 # Test no (error) message has been printed by `exec-file'.
441 set escapedfile [string_to_regexp $file]
442 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
445 reach $solib_bp run $displacement
447 # Use two separate gdb_test_multiple statements to avoid timeouts due
448 # to slow processing of wildcard capturing long output
449 set test "info files"
451 gdb_test_multiple $test $test {
452 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n" {
453 set entrynohex $expect_out(1,string)
454 gdb_test_multiple "" $test {
455 -re "\r\n$gdb_prompt $" {
462 # `info sym' cannot be tested for .opd as the binary may not have
464 if {[istarget powerpc64-*] && [is_lp64_target]} {
465 set test "convert entry point"
466 gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
467 -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
468 set entrynohex $expect_out(2,string)
473 if {$entrynohex != ""} {
474 gdb_test "break *0x$entrynohex" "" "break at entry point"
475 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
478 # There is no symbol to break at ld.so. Moreover it can
479 # exit with an error code.
481 set test "ld.so exit"
482 set test_displacement "seen displacement message as $displacement"
483 gdb_test_multiple "run" $test {
484 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
485 # Missing "$gdb_prompt $" is intentional.
486 if {$expect_out(1,string) == "0x0"} {
491 if {$displacement == $case || $displacement == "PRESENT"} {
492 pass $test_displacement
493 set displacement "FOUND-$displacement"
495 fail $test_displacement
499 -re "$inferior_exited_re (normally|with code \[0-9\]+).\r\n$gdb_prompt $" {
500 # Do not check the binary filename as it may be truncated.
504 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
505 fail $test_displacement
511 # Create separate binaries for each testcase - to make the possible reported
512 # problem reproducible after the whole test run finishes.
514 foreach ldprelink {NO YES} {
515 foreach ldsepdebug {NO IN SEP} {
516 # Skip running the ldsepdebug test if we do not have system separate
517 # debug info available.
518 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
522 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
523 set interp $binprefix-$ldname
525 # prelink needs to always prelink all the dependencies to do any file
526 # modifications of its files. ld.so also needs all the dependencies to
527 # be prelinked to omit the relocation process. In-memory file offsets
528 # are not dependent whether ld.so went the prelink way or through the
529 # relocation process.
531 # For GDB we are not interested whether prelink succeeds as it is
532 # transparent to GDB. GDB is being tested for differences of file
533 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
534 # the BIN modification to happen but we need to restore the original
535 # possibly unprelinked ld.so to test all the combinations for GDB.
536 set interp_saved ${interp}-saved
538 with_test_prefix "$ldname" {
539 if {$ldsepdebug == "NO"} {
540 file_copy $interp_system $interp
541 # Never call strip-debug before unprelink:
542 # prelink: ...: Section .note.gnu.build-id created after prelinking
543 if ![prelinkNO $interp] {
547 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
548 file_copy $interp_system $interp
549 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
550 file_copy $interp_system $interp
551 file_copy $interp_system_debug "${interp}.debug"
552 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
553 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
556 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
557 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
558 verbose -log "command is $command"
559 if [catch $command] {
566 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
567 file_copy $interp_system $interp
568 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
569 if ![prelinkNO $interp] {
572 gdb_gnu_strip_debug $interp
573 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
574 file_copy $interp_system $interp
575 file_copy $interp_system_debug "${interp}.debug"
578 if {$ldsepdebug == "SEP"} {
579 if ![prelinkNO "${interp}.debug"] {
583 file delete "${interp}.debug"
586 if ![prelink$ldprelink $interp] {
589 if {$ldprelink == "NO"} {
590 set displacement "NONZERO"
592 # x86* kernel loads prelinked PIE binary at its
593 # prelinked address but ppc* kernel loads it at a
594 # random address. prelink normally skips PIE binaries
595 # during the system scan.
596 set displacement "PRESENT"
598 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
600 if ![file_copy $interp $interp_saved] {
604 foreach binprelink {NO YES} {
605 foreach binsepdebug {NO IN SEP} {
606 # "ATTACH" is like "YES" but it is modified during
607 # run. It cannot be used for problem
608 # reproducibility after the testcase ends.
609 foreach binpie {NO YES ATTACH} {
610 # This combination is not possible, non-PIE (fixed address)
611 # binary cannot be prelinked to any (other) address.
612 if {$binprelink == "YES" && $binpie == "NO"} {
616 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
617 set exec $binprefix-$binname
619 with_test_prefix "$binname" {
620 set opts "ldflags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
621 if {$binsepdebug != "NO"} {
624 if {$binpie != "NO"} {
625 lappend opts {additional_flags=-fPIE -pie}
629 set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
630 if {$relink_args == ""} {
634 if {$binsepdebug == "SEP"} {
635 gdb_gnu_strip_debug $exec
638 if {$binpie == "NO"} {
639 set displacement "NONE"
640 } elseif {$binprelink == "NO"} {
641 set displacement "NONZERO"
643 # x86* kernel loads prelinked PIE binary at its prelinked
644 # address but ppc* kernel loads it at a random address.
645 # prelink normally skips PIE binaries during the system scan.
646 set displacement "PRESENT"
649 if {[prelink$binprelink $relink_args [file tail $exec]]
650 && [file_copy $interp_saved $interp]} {
651 if {$binpie != "ATTACH"} {
652 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
654 # If the file has been randomly prelinked it must be
655 # "NONZERO". We could see "ZERO" only if it was unprelinked
656 # and it is now running at the same address - which is 0 but
657 # executable can never run at address 0.
659 set displacement "NONZERO"
660 test_attach $exec $displacement $relink_args
662 # ATTACH means that executables and libraries have been
663 # modified after they have been run. They cannot be reused
664 # for problem reproducibility after the testcase ends in
665 # the ATTACH case. Therefore they are rather deleted not
666 # to confuse after the run finishes.
667 set exec_debug [system_debug_get $exec]
668 if {$exec_debug != ""} {
669 # `file delete [glob "${exec_debug}*"]' does not work.
670 foreach f [glob "${exec_debug}*"] {
674 file delete -force $dir
675 # `file delete [glob "${exec}*"]' does not work.
676 foreach f [glob "${exec}*"] {
686 file delete $interp_saved