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 load_lib prelink-support.exp
23 set test "break-interp"
24 set binprefix ${objdir}/${subdir}/${test}
25 # Only to get the $interp_system name.
26 set srcfile_test "start.c"
27 set binfile_test ${test}-test
28 set binfile_lib ${objdir}/${subdir}/${test}.so
29 set srcfile "${test}-main.c"
30 set srcfile_lib "${test}-lib.c"
32 if [get_compiler_info ${binfile_lib}] {
36 # Use -soname so that the new library gets copied by build_executable_own_libs.
38 if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug additional_flags=-Wl,-soname,${test}.so]] != ""} {
42 if {[build_executable ${test}.exp $binfile_test ${srcfile_test} {}] == -1} {
46 # Note: The separate debug info file content build-id/crc32 are not verified
47 # contrary to the GDB search algorithm skipping non-matching ones.
48 proc system_debug_get {exec} {
51 set exec_build_id_debug [build_id_debug_filename_get $exec]
52 set debug_base "[file tail $exec].debug"
53 set exec_dir [file dirname $exec]
55 # isfile returns 1 even for symlinks to files.
56 set retval $debug_root/$exec_build_id_debug
57 if [file isfile $retval] {
60 set retval $exec_dir/$debug_base
61 if [file isfile $retval] {
64 set retval $exec_dir/.debug/$debug_base
65 if [file isfile $retval] {
68 set retval $debug_root/$exec_dir/$debug_base
69 if [file isfile $retval] {
78 set test "show debug-file-directory"
79 gdb_test_multiple $test $test {
80 -re "The directory where separate debug symbols are searched for is \"(.*)\".\r\n$gdb_prompt $" {
81 set debug_root $expect_out(1,string)
85 set interp_system [section_get ${objdir}/${subdir}/$binfile_test .interp]
86 set interp_system_debug [system_debug_get $interp_system]
87 verbose -log "$interp_system has debug $interp_system_debug"
89 proc prelinkNO {arg {name ""}} {
90 return [prelink_no $arg $name]
93 proc prelinkYES {arg {name ""}} {
94 return [prelink_yes $arg $name]
97 proc strip_debug {dest} {
98 set test "strip [file tail $dest]"
99 set strip_program [transform strip]
100 set command "exec $strip_program --strip-debug $dest"
101 verbose -log "command is $command"
102 if [catch $command] {
111 # `runto' does not check we stopped really at the function we specified.
112 # DISPLACEMENT can be "NONE", "ZERO" or "NONZERO"
113 proc reach {func command displacement} {
114 global gdb_prompt expect_out
117 set old_ldprefix $pf_prefix
118 lappend pf_prefix "reach-$func:"
120 if [gdb_breakpoint $func allow-pending] {
122 set test_displacement "seen displacement message as $displacement"
123 gdb_test_multiple $command $test {
124 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
125 # Missing "$gdb_prompt $" is intentional.
126 if {$expect_out(1,string) == "0x0"} {
131 if {$displacement == $case} {
132 pass $test_displacement
133 set displacement "FOUND-$displacement"
135 fail $test_displacement
139 -re "Breakpoint \[0-9\]+, \\.?$func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
142 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in \\.?$func \\(\\).*\r\n$gdb_prompt $" {
146 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
147 fail $test_displacement
151 set pf_prefix $old_ldprefix
154 proc test_core {file displacement} {
155 global srcdir subdir gdb_prompt expect_out
157 set corefile [core_find $file {} "segv"]
158 if {$corefile == ""} {
163 set old_ldprefix $pf_prefix
164 lappend pf_prefix "core:"
168 # Clear it to never find any separate debug infos in $debug_root.
169 gdb_test_no_output "set debug-file-directory" \
170 "set debug-file-directory for core"
171 gdb_reinitialize_dir $srcdir/$subdir
174 # Print the "PIE (Position Independent Executable) displacement" message.
175 gdb_test_no_output "set verbose on"
177 set test "core loaded"
178 set test_displacement "seen displacement message as $displacement"
179 gdb_test_multiple "core-file $corefile" $test {
180 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
181 # Missing "$gdb_prompt $" is intentional.
182 if {$expect_out(1,string) == "0x0"} {
187 if {$displacement == $case} {
188 pass $test_displacement
189 set displacement "FOUND-$displacement"
191 fail $test_displacement
195 -re "Core was generated by .*\r\n#0 .*$gdb_prompt $" {
196 # Do not check the binary filename as it may be truncated.
200 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
201 fail $test_displacement
204 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
206 set pf_prefix $old_ldprefix
209 proc test_attach_gdb {file pid displacement prefix} {
210 global gdb_prompt expect_out
213 set old_ldprefix $pf_prefix
214 lappend pf_prefix "$prefix:"
219 # Print the "PIE (Position Independent Executable) displacement" message.
220 gdb_test_no_output "set verbose on"
223 gdb_test "file $file" "Reading symbols from .*done\\." "file"
227 gdb_test_multiple "attach $pid" $test {
228 -re "Attaching to (program: .*, )?process $pid\r\n" {
229 # Missing "$gdb_prompt $" is intentional.
234 set test "attach final prompt"
235 set test_displacement "seen displacement message as $displacement"
236 gdb_test_multiple "" $test {
237 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
238 # Missing "$gdb_prompt $" is intentional.
239 if {$expect_out(1,string) == "0x0"} {
244 if {$displacement == $case} {
245 pass $test_displacement
246 set displacement "FOUND-$displacement"
248 fail $test_displacement
252 -re "$gdb_prompt $" {
256 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
257 fail $test_displacement
260 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
263 set pf_prefix $old_ldprefix
266 proc test_attach {file displacement {relink_args ""}} {
271 set test "sleep function started"
273 set command "${file} sleep"
274 set res [remote_spawn host $command];
275 if { $res < 0 || $res == "" } {
276 perror "Spawning $command failed."
280 set pid [exp_pid -i $res]
290 fail "$test (timeout)"
295 if {$relink_args == ""} {
296 test_attach_gdb "" $pid $displacement "attach"
298 # These could be rather passed as arguments.
299 global exec interp_saved interp
301 foreach relink {YES NO} {
302 # Formerly this test was testing only prelinking of $EXEC. As the
303 # prelink command automatically prelinks all of $EXEC's libraries,
304 # even $INTERP got prelinked. Therefore, we formerly had to
305 # `[file_copy $interp_saved $interp]' to make $INTERP not affected
306 # by this prelinking of $EXEC.
308 # But now we need to test even prelinking of $INTERP. We could
309 # create a separate test to test just the $INTERP prelinking. For
310 # test simplicity, we merged this test and the test above by not
311 # restoring $INTERP after $EXEC prelink. $INTERP gets restored
314 if [prelink$relink $relink_args [file tail $exec]] {
315 # /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
316 test_attach_gdb $exec $pid $displacement "attach-relink$relink"
319 file_copy $interp_saved $interp
322 remote_exec host "kill -9 $pid"
325 proc test_ld {file ifmain trynosym displacement} {
326 global srcdir subdir gdb_prompt expect_out
328 # First test normal `file'-command loaded $FILE with symbols.
332 # Clear it to never find any separate debug infos in $debug_root.
333 gdb_test_no_output "set debug-file-directory"
334 gdb_reinitialize_dir $srcdir/$subdir
337 # Print the "PIE (Position Independent Executable) displacement" message.
338 gdb_test_no_output "set verbose on"
340 # We want to test the re-run of a PIE in the case where the executable
341 # is loaded with a different displacement, but disable-randomization
342 # prevents that from happening. So turn it off.
343 gdb_test "set disable-randomization off"
345 reach "dl_main" "run segv" $displacement
347 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
350 reach "main" continue "NONE"
352 reach "libfunc" continue "NONE"
354 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
357 # Try re-run if the new PIE displacement takes effect.
358 gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
359 reach "dl_main" "run segv" $displacement
362 test_core $file $displacement
364 test_attach $file $displacement
372 set old_ldprefix $pf_prefix
373 lappend pf_prefix "symbol-less:"
375 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
376 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
380 # Clear it to never find any separate debug infos in $debug_root.
381 gdb_test_no_output "set debug-file-directory"
382 gdb_reinitialize_dir $srcdir/$subdir
384 # Print the "PIE (Position Independent Executable) displacement" message.
385 gdb_test_no_output "set verbose on"
387 # Test no (error) message has been printed by `exec-file'.
388 set escapedfile [string_to_regexp $file]
389 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
392 reach "dl_main" run $displacement
394 set test "info files"
396 gdb_test_multiple $test $test {
397 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
398 set entrynohex $expect_out(1,string)
402 # `info sym' cannot be tested for .opd as the binary may not have
404 if [istarget powerpc64-*] {
405 set test "convert entry point"
406 gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
407 -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
408 set entrynohex $expect_out(2,string)
413 if {$entrynohex != ""} {
414 gdb_test "break *0x$entrynohex" "" "break at entry point"
415 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
418 # There is no symbol to break at ld.so. Moreover it can exit with an
421 set test "ld.so exit"
422 set test_displacement "seen displacement message as $displacement"
423 gdb_test_multiple "run" $test {
424 -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
425 # Missing "$gdb_prompt $" is intentional.
426 if {$expect_out(1,string) == "0x0"} {
431 if {$displacement == $case} {
432 pass $test_displacement
433 set displacement "FOUND-$displacement"
435 fail $test_displacement
439 -re "Program exited (normally|with code \[0-9\]+)\\.\r\n$gdb_prompt $" {
440 # Do not check the binary filename as it may be truncated.
444 if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
445 fail $test_displacement
449 set pf_prefix $old_ldprefix
452 # Create separate binaries for each testcase - to make the possible reported
453 # problem reproducible after the whole test run finishes.
455 set old_ldprefix $pf_prefix
456 foreach ldprelink {NO YES} {
457 foreach ldsepdebug {NO IN SEP} {
458 # Skip running the ldsepdebug test if we do not have system separate
459 # debug info available.
460 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
464 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
465 set interp $binprefix-$ldname
467 # prelink needs to always prelink all the dependencies to do any file
468 # modifications of its files. ld.so also needs all the dependencies to
469 # be prelinked to omit the relocation process. In-memory file offsets
470 # are not dependent whether ld.so went the prelink way or through the
471 # relocation process.
473 # For GDB we are not interested whether prelink succeeds as it is
474 # transparent to GDB. GDB is being tested for differences of file
475 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
476 # the BIN modification to happen but we need to restore the original
477 # possibly unprelinked ld.so to test all the combinations for GDB.
478 set interp_saved ${interp}-saved
480 set pf_prefix $old_ldprefix
481 lappend pf_prefix "$ldname:"
483 if {$ldsepdebug == "NO"} {
484 file_copy $interp_system $interp
485 # Never call strip-debug before unprelink:
486 # prelink: ...: Section .note.gnu.build-id created after prelinking
487 if ![prelinkNO $interp] {
491 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
492 file_copy $interp_system $interp
493 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
494 file_copy $interp_system $interp
495 file_copy $interp_system_debug "${interp}.debug"
496 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
497 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
500 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
501 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
502 verbose -log "command is $command"
503 if [catch $command] {
510 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
511 file_copy $interp_system $interp
512 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
513 if ![prelinkNO $interp] {
516 gdb_gnu_strip_debug $interp
517 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
518 file_copy $interp_system $interp
519 file_copy $interp_system_debug "${interp}.debug"
522 if {$ldsepdebug == "SEP"} {
523 if ![prelinkNO "${interp}.debug"] {
527 file delete "${interp}.debug"
530 if ![prelink$ldprelink $interp] {
533 if {$ldprelink == "NO"} {
534 set displacement "NONZERO"
536 set displacement "ZERO"
538 test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
540 if ![file_copy $interp $interp_saved] {
543 set old_binprefix $pf_prefix
544 foreach binprelink {NO YES} {
545 foreach binsepdebug {NO IN SEP} {
546 # "ATTACH" is like "YES" but it is modified during run.
547 # It cannot be used for problem reproducibility after the
549 foreach binpie {NO YES ATTACH} {
550 # This combination is not possible, non-PIE (fixed address)
551 # binary cannot be prelinked to any (other) address.
552 if {$binprelink == "YES" && $binpie == "NO"} {
556 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
557 set exec $binprefix-$binname
559 set pf_prefix $old_binprefix
560 lappend pf_prefix "$binname:"
562 set opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
563 if {$binsepdebug != "NO"} {
566 if {$binpie != "NO"} {
567 lappend opts {additional_flags=-fPIE -pie}
571 set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
572 if {$relink_args == ""} {
576 if {$binsepdebug == "SEP"} {
577 gdb_gnu_strip_debug $exec
580 if {$binpie == "NO"} {
581 set displacement "NONE"
582 } elseif {$binprelink == "NO"} {
583 set displacement "NONZERO"
585 set displacement "ZERO"
588 if {[prelink$binprelink $relink_args [file tail $exec]]
589 && [file_copy $interp_saved $interp]} {
590 if {$binpie != "ATTACH"} {
591 test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
593 # If the file has been randomly prelinked it must
594 # be "NONZERO". We could see "ZERO" only if it was
595 # unprelinked and it is now running at the same
596 # address - which is 0 but executable can never run
599 set displacement "NONZERO"
600 test_attach $exec $displacement $relink_args
602 # ATTACH means that executables and libraries have
603 # been modified after they have been run. They
604 # cannot be reused for problem reproducibility after
605 # the testcase ends in the ATTACH case. Therefore
606 # they are rather deleted not to confuse after the
608 set exec_debug [system_debug_get $exec]
609 if {$exec_debug != ""} {
610 # `file delete [glob "${exec_debug}*"]' does not work.
611 foreach f [glob "${exec_debug}*"] {
615 file delete -force $dir
616 # `file delete [glob "${exec}*"]' does not work.
617 foreach f [glob "${exec}*"] {
626 file delete $interp_saved
629 set pf_prefix $old_ldprefix