1 # Copyright 2010 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] || ![istarget *-linux*] || [skip_shlib_tests]} {
21 set test "break-interp"
22 set binprefix ${objdir}/${subdir}/${test}
23 # Only to get the $interp_system name.
24 set srcfile_test "start.c"
25 set binfile_test ${test}-test
26 set binfile_lib ${objdir}/${subdir}/${test}.so
27 set srcfile "${test}-main.c"
28 set srcfile_lib "${test}-lib.c"
30 if [get_compiler_info ${binfile_lib}] {
34 # Use -soname so that it is listed with " => " by ldd and this testcase makes
35 # a copy of ${binfile_lib} for each prelink variant.
37 if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug additional_flags=-Wl,-soname,${test}.so]] != ""} {
41 if {[build_executable ${test}.exp $binfile_test ${srcfile_test} {}] == -1} {
45 # Return the interpreter filename string.
46 # Return "" if no interpreter was found.
47 proc section_get {exec section} {
50 set tmp "${objdir}/${subdir}/break-interp.interp"
51 set objcopy_program [transform objcopy]
53 set command "exec $objcopy_program -O binary --set-section-flags $section=A --change-section-address $section=0 -j $section $exec $tmp"
54 verbose -log "command is $command"
55 set result [catch $command output]
56 verbose -log "result is $result"
57 verbose -log "output is $output"
62 fconfigure $fi -translation binary
66 # .interp has size $len + 1 but .gnu_debuglink contains garbage after \000.
67 set len [string first \000 $data]
69 verbose -log "section $section not found"
72 set retval [string range $data 0 [expr $len - 1]]
73 verbose -log "section $section is <$retval>"
77 # Note: The separate debug info file content build-id/crc32 are not verified
78 # contrary to the GDB search algorithm skipping non-matching ones.
79 proc system_debug_get {exec} {
82 set exec_build_id_debug [build_id_debug_filename_get $exec]
83 set debug_base "[file tail $exec].debug"
84 set exec_dir [file dirname $exec]
86 # isfile returns 1 even for symlinks to files.
87 set retval $debug_root/$exec_build_id_debug
88 if [file isfile $retval] {
91 set retval $exec_dir/$debug_base
92 if [file isfile $retval] {
95 set retval $exec_dir/.debug/$debug_base
96 if [file isfile $retval] {
99 set retval $debug_root/$exec_dir/$debug_base
100 if [file isfile $retval] {
109 set test "show debug-file-directory"
110 gdb_test_multiple $test $test {
111 -re "The directory where separate debug symbols are searched for is \"(.*)\".\r\n$gdb_prompt $" {
112 set debug_root $expect_out(1,string)
116 set interp_system [section_get ${objdir}/${subdir}/$binfile_test .interp]
117 set interp_system_debug [system_debug_get $interp_system]
118 verbose -log "$interp_system has debug $interp_system_debug"
120 proc prelinkNO_run {arg} {
121 set command "exec /usr/sbin/prelink -uN $arg"
122 verbose -log "command is $command"
123 set result [catch $command output]
124 verbose -log "result is $result"
125 verbose -log "output is $output"
126 return [list $result $output]
129 proc prelinkNO {arg {name {}}} {
131 set name [file tail $arg]
133 set test "unprelink $name"
134 set run [prelinkNO_run $arg]
135 set result [lindex $run 0]
136 set output [lindex $run 1]
137 if {$result == 0 && $output == ""} {
138 verbose -log "$name has been now unprelinked"
139 set run [prelinkNO_run $arg]
140 set result [lindex $run 0]
141 set output [lindex $run 1]
143 # Last line does miss the trailing \n.
144 if {$result == 1 && [regexp {^(/usr/sbin/prelink[^\r\n]*: [^ ]* does not have .gnu.prelink_undo section\n?)*$} $output]} {
153 proc prelinkYES {arg {name ""}} {
155 set name [file tail $arg]
158 # Try to unprelink it first so that if it has been already prelinked before
159 # we get different address now and the result is not affected by the
160 # previous $arg state..
161 prelinkNO $arg "$name pre-unprelink"
163 set test "prelink $name"
164 set command "exec /usr/sbin/prelink -qNR --no-exec-shield $arg"
165 verbose -log "command is $command"
166 set result [catch $command output]
167 verbose -log "result is $result"
168 verbose -log "output is $output"
169 if {$result == 0 && $output == ""} {
172 } elseif {$result == 1 \
173 && [string match -nocase "*: Not enough room to add .dynamic entry" $output]} {
174 # Linker should have reserved some entries for prelink.
184 proc symlink_resolve {file} {
186 while {[file type $file] == "link"} {
187 set target [file readlink $file]
188 if {[file pathtype $target] == "relative"} {
189 set src2 [file dirname $file]/$target
193 verbose -log "Resolved symlink $file targetting $target as $src2"
196 set loop [expr $loop + 1]
198 fail "Looping symlink resolution for $file"
205 proc copy {src dest} {
206 set src [symlink_resolve $src]
207 # Test name would contain build-id hash for symlink-unresolved $src.
208 set test "copy [file tail $src] to [file tail $dest]"
209 set command "file copy -force $src $dest"
210 verbose -log "command is $command"
211 if [catch $command] {
220 proc strip_debug {dest} {
221 set test "strip [file tail $dest]"
222 set strip_program [transform strip]
223 set command "exec $strip_program --strip-debug $dest"
224 verbose -log "command is $command"
225 if [catch $command] {
234 # `runto' does not check we stopped really at the function we specified.
235 # DISPLACEMENT can be "NONE", "ZERO" or "NONZERO"
236 proc reach {func command displacement} {
237 global gdb_prompt expect_out
240 set old_ldprefix $pf_prefix
241 lappend pf_prefix "reach-$func:"
243 if [gdb_breakpoint $func allow-pending] {
245 set test_displacement "seen displacement message as $displacement"
246 gdb_test_multiple $command $test {
247 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
248 # Missing "$gdb_prompt $" is intentional.
249 if {$expect_out(1,string) == "0x0"} {
254 if {$displacement == $case} {
255 pass $test_displacement
256 # Permit multiple such messages.
257 set displacement "FOUND-$displacement"
258 } elseif {$displacement != "FOUND-$case"} {
259 fail $test_displacement
263 -re "Breakpoint \[0-9\]+, $func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
266 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in $func \\(\\).*\r\n$gdb_prompt $" {
270 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
271 fail $test_displacement
275 set pf_prefix $old_ldprefix
278 proc test_core {file displacement} {
279 global srcdir subdir gdb_prompt expect_out
281 set corefile [core_find $file {} "segv"]
282 if {$corefile == ""} {
287 set old_ldprefix $pf_prefix
288 lappend pf_prefix "core:"
292 # Clear it to never find any separate debug infos in $debug_root.
293 gdb_test_no_output "set debug-file-directory" \
294 "set debug-file-directory for core"
295 gdb_reinitialize_dir $srcdir/$subdir
298 # Print the "PIE (Position Independent Executable) displacement" message.
299 gdb_test_no_output "set verbose on"
301 set test "core loaded"
302 set test_displacement "seen displacement message as $displacement"
303 gdb_test_multiple "core-file $corefile" $test {
304 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
305 # Missing "$gdb_prompt $" is intentional.
306 if {$expect_out(1,string) == "0x0"} {
311 if {$displacement == $case} {
312 pass $test_displacement
313 # Permit multiple such messages.
314 set displacement "FOUND-$displacement"
315 } elseif {$displacement != "FOUND-$case"} {
316 fail $test_displacement
320 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
321 # Do not check the binary filename as it may be truncated.
325 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
326 fail $test_displacement
329 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
331 set pf_prefix $old_ldprefix
334 proc test_attach_gdb {file pid displacement prefix} {
335 global gdb_prompt expect_out
338 set old_ldprefix $pf_prefix
339 lappend pf_prefix "$prefix:"
344 # Print the "PIE (Position Independent Executable) displacement" message.
345 gdb_test_no_output "set verbose on"
348 gdb_test "file $file" "Reading symbols from .*done\\." "file"
352 gdb_test_multiple "attach $pid" $test {
353 -re "Attaching to (program: .*, )?process $pid\r\n" {
354 # Missing "$gdb_prompt $" is intentional.
359 set test "attach final prompt"
360 set test_displacement "seen displacement message as $displacement"
361 gdb_test_multiple "" $test {
362 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
363 # Missing "$gdb_prompt $" is intentional.
364 if {$expect_out(1,string) == "0x0"} {
369 if {$displacement == $case} {
370 pass $test_displacement
371 # Permit multiple such messages.
372 set displacement "FOUND-$displacement"
373 } elseif {$displacement != "FOUND-$case"} {
374 fail $test_displacement
378 -re "$gdb_prompt $" {
382 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
383 fail $test_displacement
386 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
389 set pf_prefix $old_ldprefix
392 proc test_attach {file displacement {relink_args ""}} {
397 set test "sleep function started"
399 set command "${file} sleep"
400 set res [remote_spawn host $command];
401 if { $res < 0 || $res == "" } {
402 perror "Spawning $command failed."
406 set pid [exp_pid -i $res]
416 fail "$test (timeout)"
421 if {$relink_args == ""} {
422 test_attach_gdb "" $pid $displacement "attach"
424 # These could be rather passed as arguments.
425 global exec interp_saved interp
427 foreach relink {YES NO} {
428 if {[prelink$relink $relink_args [file tail $exec]]
429 && [copy $interp_saved $interp]} {
430 # /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
431 test_attach_gdb $exec $pid $displacement "attach-relink$relink"
436 remote_exec host "kill -9 $pid"
439 proc test_ld {file ifmain trynosym displacement} {
440 global srcdir subdir gdb_prompt expect_out
442 # First test normal `file'-command loaded $FILE with symbols.
446 # Clear it to never find any separate debug infos in $debug_root.
447 gdb_test_no_output "set debug-file-directory"
448 gdb_reinitialize_dir $srcdir/$subdir
451 # Print the "PIE (Position Independent Executable) displacement" message.
452 gdb_test_no_output "set verbose on"
454 reach "dl_main" "run segv" $displacement
456 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
459 # Displacement message will be printed the second time on initializing
460 # the linker from svr4_special_symbol_handling. If any ANOFFSET has
461 # been already set as non-zero the detection will no longer be run.
462 if {$displacement == "NONZERO"} {
463 set displacement_main "NONE"
465 set displacement_main $displacement
467 reach "main" continue $displacement_main
469 reach "libfunc" continue "NONE"
471 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
473 test_core $file $displacement
475 test_attach $file $displacement
483 set old_ldprefix $pf_prefix
484 lappend pf_prefix "symbol-less:"
486 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
487 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
491 # Clear it to never find any separate debug infos in $debug_root.
492 gdb_test_no_output "set debug-file-directory"
493 gdb_reinitialize_dir $srcdir/$subdir
495 # Print the "PIE (Position Independent Executable) displacement" message.
496 gdb_test_no_output "set verbose on"
498 # Test no (error) message has been printed by `exec-file'.
499 set escapedfile [string_to_regexp $file]
500 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
503 reach "dl_main" run $displacement
505 set test "info files"
507 gdb_test_multiple $test $test {
508 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
509 set entrynohex $expect_out(1,string)
513 if {$entrynohex != ""} {
514 gdb_test "break *0x$entrynohex" "" "break at entry point"
515 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
518 # There is no symbol to break at ld.so. Moreover it can exit with an
521 set test "ld.so exit"
522 set test_displacement "seen displacement message as $displacement"
523 gdb_test_multiple "run" $test {
524 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
525 # Missing "$gdb_prompt $" is intentional.
526 if {$expect_out(1,string) == "0x0"} {
531 if {$displacement == $case} {
532 pass $test_displacement
533 # Permit multiple such messages.
534 set displacement "FOUND-$displacement"
535 } elseif {$displacement != "FOUND-$case"} {
536 fail $test_displacement
540 -re "Program exited (normally|with code \[0-9\]+)\\.\r\n$gdb_prompt $" {
541 # Do not check the binary filename as it may be truncated.
545 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
546 fail $test_displacement
550 set pf_prefix $old_ldprefix
553 # Create separate binaries for each testcase - to make the possible reported
554 # problem reproducible after the whole test run finishes.
556 set old_ldprefix $pf_prefix
557 foreach ldprelink {NO YES} {
558 foreach ldsepdebug {NO IN SEP} {
559 # Skip running the ldsepdebug test if we do not have system separate
560 # debug info available.
561 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
565 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
566 set interp $binprefix-$ldname
568 # prelink needs to always prelink all the dependencies to do any file
569 # modifications of its files. ld.so also needs all the dependencies to
570 # be prelinked to omit the relocation process. In-memory file offsets
571 # are not dependent whether ld.so went the prelink way or through the
572 # relocation process.
574 # For GDB we are not interested whether prelink succeeds as it is
575 # transparent to GDB. GDB is being tested for differences of file
576 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
577 # the BIN modification to happen but we need to restore the original
578 # possibly unprelinked ld.so to test all the combinations for GDB.
579 set interp_saved ${interp}-saved
581 set pf_prefix $old_ldprefix
582 lappend pf_prefix "$ldname:"
584 if {$ldsepdebug == "NO"} {
585 copy $interp_system $interp
586 # Never call strip-debug before unprelink:
587 # prelink: ...: Section .note.gnu.build-id created after prelinking
588 if ![prelinkNO $interp] {
592 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
593 copy $interp_system $interp
594 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
595 copy $interp_system $interp
596 copy $interp_system_debug "${interp}.debug"
597 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
598 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
601 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
602 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
603 verbose -log "command is $command"
604 if [catch $command] {
611 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
612 copy $interp_system $interp
613 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
614 if ![prelinkNO $interp] {
617 gdb_gnu_strip_debug $interp
618 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
619 copy $interp_system $interp
620 copy $interp_system_debug "${interp}.debug"
623 if {$ldsepdebug == "SEP"} {
624 if ![prelinkNO "${interp}.debug"] {
628 file delete "${interp}.debug"
631 if ![prelink$ldprelink $interp] {
634 if {$ldprelink == "NO"} {
635 set displacement "NONZERO"
637 set displacement "ZERO"
639 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
641 if ![copy $interp $interp_saved] {
644 set old_binprefix $pf_prefix
645 foreach binprelink {NO YES} {
646 foreach binsepdebug {NO IN SEP} {
647 # "ATTACH" is like "YES" but it is modified during run.
648 # It cannot be used for problem reproducibility after the
650 foreach binpie {NO YES ATTACH} {
651 # This combination is not possible, non-PIE (fixed address)
652 # binary cannot be prelinked to any (other) address.
653 if {$binprelink == "YES" && $binpie == "NO"} {
657 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
658 set exec $binprefix-$binname
661 set pf_prefix $old_binprefix
662 lappend pf_prefix "$binname:"
664 set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
665 lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
666 if {$binsepdebug != "NO"} {
669 if {$binpie != "NO"} {
670 lappend opts {additional_flags=-fPIE -pie}
672 if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
675 if {$binsepdebug == "SEP"} {
676 gdb_gnu_strip_debug $exec
679 # Supply a self-sufficent directory $dir with the required
680 # libraries. To make an executable properly prelinked all
681 # its dependencies on libraries must be also prelinked. If
682 # some of the system libraries is currently not prelinked
683 # we have no right to prelink (modify it) at its current
686 file delete -force $dir
689 set command "ldd $exec"
690 set test "ldd [file tail $exec]"
691 set result [catch "exec $command" output]
692 verbose -log "result of $command is $result"
693 verbose -log "output of $command is $output"
694 if {$result != 0 || $output == ""} {
700 # gdb testsuite will put there also needless -lm.
701 set test "$test output contains libc"
702 set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
703 if {[llength $libc] == 0} {
710 for {set i 1} {$i < [llength $libc]} {incr i 2} {
711 set abspath [lindex $libc $i]
712 set dest "$dir/[file tail $abspath]"
717 if {$binpie == "NO"} {
718 set displacement "NONE"
719 } elseif {$binprelink == "NO"} {
720 set displacement "NONZERO"
722 set displacement "ZERO"
725 set relink_args "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]"
726 if {[prelink$binprelink $relink_args [file tail $exec]]
727 && [copy $interp_saved $interp]} {
728 if {$binpie != "ATTACH"} {
729 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
731 # If the file has been randomly prelinked it must
732 # be "NONZERO". We could see "ZERO" only if it was
733 # unprelinked and it is now running at the same
734 # address - which is 0 but executable can never run
737 set displacement "NONZERO"
738 test_attach $exec $displacement $relink_args
740 # ATTACH means that executables and libraries have
741 # been modified after they have been run. They
742 # cannot be reused for problem reproducibility after
743 # the testcase ends in the ATTACH case. Therefore
744 # they are rather deleted not to confuse after the
746 set exec_debug [system_debug_get $exec]
747 if {$exec_debug != ""} {
748 # `file delete [glob "${exec_debug}*"]' does not work.
749 foreach f [glob "${exec_debug}*"] {
753 file delete -force $dir
754 # `file delete [glob "${exec}*"]' does not work.
755 foreach f [glob "${exec}*"] {
764 file delete $interp_saved
767 set pf_prefix $old_ldprefix