1 # Copyright 2010-2012 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 ${binfile_lib}] {
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 # Implementation of reach.
114 proc reach_1 {func command displacement} {
115 global gdb_prompt expect_out
117 if {$func == "_dl_debug_state"} {
118 # Breakpoint on _dl_debug_state can have problems due to its overlap
119 # with the existing internal breakpoint from GDB.
120 gdb_test_no_output "set stop-on-solib-events 1"
121 } elseif {! [gdb_breakpoint $func allow-pending]} {
126 set test_displacement "seen displacement message as $displacement"
127 set debug_state_count 0
128 gdb_test_multiple $command $test {
129 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
130 # Missing "$gdb_prompt $" is intentional.
131 if {$expect_out(1,string) == "0x0"} {
136 if {$displacement == $case || $displacement == "PRESENT"} {
137 pass $test_displacement
138 set displacement "FOUND-$displacement"
140 fail $test_displacement
144 -re "Breakpoint \[0-9\]+, \\.?(__GI_)?$func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
145 if {$func == "_dl_debug_state"} {
151 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in \\.?(__GI_)?$func \\(\\).*\r\n$gdb_prompt $" {
152 if {$func == "_dl_debug_state"} {
158 -re "Stopped due to (spurious )?shared library event.*\r\n$gdb_prompt $" {
159 if {$func == "_dl_debug_state"} {
160 if {$debug_state_count == 0} {
161 # First stop does not yet relocate the _start function
162 # descriptor on ppc64.
163 set debug_state_count 1
164 send_gdb "continue\n"
174 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
175 fail $test_displacement
178 if {$func == "_dl_debug_state"} {
179 gdb_test_no_output "set stop-on-solib-events 0"
183 # `runto' does not check we stopped really at the function we specified.
184 # DISPLACEMENT can be "NONE" for no message to be present, "ZERO" for
185 # displacement of 0 bytes to be present, "NONZERO" for displacement of non-0
186 # bytes to be present and "PRESENT" if both "ZERO" and "NONZERO" are valid.
187 proc reach {func command displacement} {
188 with_test_prefix " reach-$func:" {
189 reach_1 $func $command $displacement
193 proc test_core {file displacement} { with_test_prefix " core:" {
194 global srcdir subdir gdb_prompt expect_out
196 set corefile [core_find $file {} "segv"]
197 if {$corefile == ""} {
203 # Clear it to never find any separate debug infos in $debug_root.
204 gdb_test_no_output "set debug-file-directory" \
205 "set debug-file-directory for core"
206 gdb_reinitialize_dir $srcdir/$subdir
209 # Print the "PIE (Position Independent Executable) displacement" message.
210 gdb_test_no_output "set verbose on"
212 set test "core loaded"
213 set test_displacement "seen displacement message as $displacement"
214 gdb_test_multiple "core-file $corefile" $test {
215 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
216 # Missing "$gdb_prompt $" is intentional.
217 if {$expect_out(1,string) == "0x0"} {
222 if {$displacement == $case || $displacement == "PRESENT"} {
223 pass $test_displacement
224 set displacement "FOUND-$displacement"
226 fail $test_displacement
230 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
231 # Do not check the binary filename as it may be truncated.
235 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
236 fail $test_displacement
239 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
242 proc test_attach_gdb {file pid displacement prefix} { with_test_prefix " $prefix:" {
243 global gdb_prompt expect_out
248 # Print the "PIE (Position Independent Executable) displacement" message.
249 gdb_test_no_output "set verbose on"
251 gdb_test "file $file" "Reading symbols from .*done\\." "file"
254 gdb_test_multiple "attach $pid" $test {
255 -re "Attaching to (program: .*, )?process $pid\r\n" {
256 # Missing "$gdb_prompt $" is intentional.
261 set test "attach final prompt"
262 set test_displacement "seen displacement message as $displacement"
263 gdb_test_multiple "" $test {
264 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
265 # Missing "$gdb_prompt $" is intentional.
266 if {$expect_out(1,string) == "0x0"} {
271 if {$displacement == $case || $displacement == "PRESENT"} {
272 pass $test_displacement
273 set displacement "FOUND-$displacement"
275 fail $test_displacement
279 -re "$gdb_prompt $" {
283 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
284 fail $test_displacement
287 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
291 proc test_attach {file displacement {relink_args ""}} {
297 set test "sleep function started"
299 set command "${file} sleep"
300 set res [remote_spawn host $command];
301 if { $res < 0 || $res == "" } {
302 perror "Spawning $command failed."
306 set pid [exp_pid -i $res]
316 fail "$test (timeout)"
321 if {$relink_args == ""} {
322 test_attach_gdb $exec $pid $displacement "attach"
324 # These could be rather passed as arguments.
325 global interp_saved interp
327 foreach relink {YES NO} {
328 # Formerly this test was testing only prelinking of $EXEC. As the
329 # prelink command automatically prelinks all of $EXEC's libraries,
330 # even $INTERP got prelinked. Therefore, we formerly had to
331 # `[file_copy $interp_saved $interp]' to make $INTERP not affected
332 # by this prelinking of $EXEC.
334 # But now we need to test even prelinking of $INTERP. We could
335 # create a separate test to test just the $INTERP prelinking. For
336 # test simplicity, we merged this test and the test above by not
337 # restoring $INTERP after $EXEC prelink. $INTERP gets restored
340 # `(wrong library or version mismatch?)' messages are printed for
341 # $binfile_lib on platforms converting REL->RELA relocations by
342 # prelink (such as on i386). There is no reliable way to verify
343 # the library file matches the running library in such case but
344 # GDB at least attempts to set the right displacement. We test
345 # `libfunc' is present in the backtrace and therefore the
346 # displacement has been guessed right.
348 if [prelink$relink $relink_args [file tail $exec]] {
349 # /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
350 test_attach_gdb $exec $pid $displacement "attach-relink$relink"
353 file_copy $interp_saved $interp
356 remote_exec host "kill -9 $pid"
359 proc test_ld {file ifmain trynosym displacement} {
360 global srcdir subdir gdb_prompt expect_out inferior_exited_re
362 # First test normal `file'-command loaded $FILE with symbols.
366 # Clear it to never find any separate debug infos in $debug_root.
367 gdb_test_no_output "set debug-file-directory"
368 gdb_reinitialize_dir $srcdir/$subdir
371 # Print the "PIE (Position Independent Executable) displacement" message.
372 gdb_test_no_output "set verbose on"
374 # We want to test the re-run of a PIE in the case where the executable
375 # is loaded with a different displacement, but disable-randomization
376 # prevents that from happening. So turn it off.
377 gdb_test "set disable-randomization off"
380 gdb_test_no_output "set args segv"
382 global objdir binfile_test
384 # ld.so needs some executable to run to reach _dl_debug_state.
385 gdb_test_no_output "set args ${objdir}/${subdir}/$binfile_test" "set args OBJDIR/${subdir}/$binfile_test"
388 reach "_dl_debug_state" "run" $displacement
390 gdb_test "bt" "#0 +\[^\r\n\]*\\m(__GI_)?_dl_debug_state\\M.*" "dl bt"
393 reach "main" continue "NONE"
395 reach "libfunc" continue "NONE"
397 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
400 # Try re-run if the new PIE displacement takes effect.
401 gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
402 reach "_dl_debug_state" "run" $displacement
405 test_core $file $displacement
407 test_attach $file $displacement
415 set old_ldprefix $pf_prefix
416 append pf_prefix " symbol-less:"
418 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
419 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
423 # Clear it to never find any separate debug infos in $debug_root.
424 gdb_test_no_output "set debug-file-directory"
425 gdb_reinitialize_dir $srcdir/$subdir
427 # Print the "PIE (Position Independent Executable) displacement" message.
428 gdb_test_no_output "set verbose on"
430 # Test no (error) message has been printed by `exec-file'.
431 set escapedfile [string_to_regexp $file]
432 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
435 reach "_dl_debug_state" run $displacement
437 # Use two separate gdb_test_multiple statements to avoid timeouts due
438 # to slow processing of wildcard capturing long output
439 set test "info files"
441 gdb_test_multiple $test $test {
442 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n" {
443 set entrynohex $expect_out(1,string)
444 gdb_test_multiple "" $test {
445 -re "\r\n$gdb_prompt $" {
452 # `info sym' cannot be tested for .opd as the binary may not have
454 if {[istarget powerpc64-*] && [is_lp64_target]} {
455 set test "convert entry point"
456 gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
457 -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
458 set entrynohex $expect_out(2,string)
463 if {$entrynohex != ""} {
464 gdb_test "break *0x$entrynohex" "" "break at entry point"
465 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
468 # There is no symbol to break at ld.so. Moreover it can exit with an
471 set test "ld.so exit"
472 set test_displacement "seen displacement message as $displacement"
473 gdb_test_multiple "run" $test {
474 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
475 # Missing "$gdb_prompt $" is intentional.
476 if {$expect_out(1,string) == "0x0"} {
481 if {$displacement == $case || $displacement == "PRESENT"} {
482 pass $test_displacement
483 set displacement "FOUND-$displacement"
485 fail $test_displacement
489 -re "$inferior_exited_re (normally|with code \[0-9\]+).\r\n$gdb_prompt $" {
490 # Do not check the binary filename as it may be truncated.
494 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
495 fail $test_displacement
499 set pf_prefix $old_ldprefix
502 # Create separate binaries for each testcase - to make the possible reported
503 # problem reproducible after the whole test run finishes.
505 set old_ldprefix $pf_prefix
506 foreach ldprelink {NO YES} {
507 foreach ldsepdebug {NO IN SEP} {
508 # Skip running the ldsepdebug test if we do not have system separate
509 # debug info available.
510 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
514 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
515 set interp $binprefix-$ldname
517 # prelink needs to always prelink all the dependencies to do any file
518 # modifications of its files. ld.so also needs all the dependencies to
519 # be prelinked to omit the relocation process. In-memory file offsets
520 # are not dependent whether ld.so went the prelink way or through the
521 # relocation process.
523 # For GDB we are not interested whether prelink succeeds as it is
524 # transparent to GDB. GDB is being tested for differences of file
525 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
526 # the BIN modification to happen but we need to restore the original
527 # possibly unprelinked ld.so to test all the combinations for GDB.
528 set interp_saved ${interp}-saved
530 set pf_prefix $old_ldprefix
531 append pf_prefix " $ldname:"
533 if {$ldsepdebug == "NO"} {
534 file_copy $interp_system $interp
535 # Never call strip-debug before unprelink:
536 # prelink: ...: Section .note.gnu.build-id created after prelinking
537 if ![prelinkNO $interp] {
541 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
542 file_copy $interp_system $interp
543 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
544 file_copy $interp_system $interp
545 file_copy $interp_system_debug "${interp}.debug"
546 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
547 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
550 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
551 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
552 verbose -log "command is $command"
553 if [catch $command] {
560 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
561 file_copy $interp_system $interp
562 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
563 if ![prelinkNO $interp] {
566 gdb_gnu_strip_debug $interp
567 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
568 file_copy $interp_system $interp
569 file_copy $interp_system_debug "${interp}.debug"
572 if {$ldsepdebug == "SEP"} {
573 if ![prelinkNO "${interp}.debug"] {
577 file delete "${interp}.debug"
580 if ![prelink$ldprelink $interp] {
583 if {$ldprelink == "NO"} {
584 set displacement "NONZERO"
586 # x86* kernel loads prelinked PIE binary at its prelinked address
587 # but ppc* kernel loads it at a random address. prelink normally
588 # skips PIE binaries during the system scan.
589 set displacement "PRESENT"
591 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
593 if ![file_copy $interp $interp_saved] {
596 set old_binprefix $pf_prefix
597 foreach binprelink {NO YES} {
598 foreach binsepdebug {NO IN SEP} {
599 # "ATTACH" is like "YES" but it is modified during run.
600 # It cannot be used for problem reproducibility after the
602 foreach binpie {NO YES ATTACH} {
603 # This combination is not possible, non-PIE (fixed address)
604 # binary cannot be prelinked to any (other) address.
605 if {$binprelink == "YES" && $binpie == "NO"} {
609 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
610 set exec $binprefix-$binname
612 set pf_prefix $old_binprefix
613 append pf_prefix " $binname:"
615 set opts "ldflags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
616 if {$binsepdebug != "NO"} {
619 if {$binpie != "NO"} {
620 lappend opts {additional_flags=-fPIE -pie}
624 set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
625 if {$relink_args == ""} {
629 if {$binsepdebug == "SEP"} {
630 gdb_gnu_strip_debug $exec
633 if {$binpie == "NO"} {
634 set displacement "NONE"
635 } elseif {$binprelink == "NO"} {
636 set displacement "NONZERO"
638 # x86* kernel loads prelinked PIE binary at its
639 # prelinked address but ppc* kernel loads it at
640 # a random address. prelink normally skips PIE
641 # binaries during the system scan.
642 set displacement "PRESENT"
645 if {[prelink$binprelink $relink_args [file tail $exec]]
646 && [file_copy $interp_saved $interp]} {
647 if {$binpie != "ATTACH"} {
648 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
650 # If the file has been randomly prelinked it must
651 # be "NONZERO". We could see "ZERO" only if it was
652 # unprelinked and it is now running at the same
653 # address - which is 0 but executable can never run
656 set displacement "NONZERO"
657 test_attach $exec $displacement $relink_args
659 # ATTACH means that executables and libraries have
660 # been modified after they have been run. They
661 # cannot be reused for problem reproducibility after
662 # the testcase ends in the ATTACH case. Therefore
663 # they are rather deleted not to confuse after the
665 set exec_debug [system_debug_get $exec]
666 if {$exec_debug != ""} {
667 # `file delete [glob "${exec_debug}*"]' does not work.
668 foreach f [glob "${exec_debug}*"] {
672 file delete -force $dir
673 # `file delete [glob "${exec}*"]' does not work.
674 foreach f [glob "${exec}*"] {
683 file delete $interp_saved
686 set pf_prefix $old_ldprefix