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: [^ ]* does not have .gnu.prelink_undo section\n?)*$} $output]} {
153 proc prelinkYES {arg {name ""}} {
155 set name [file tail $arg]
157 set test "prelink $name"
158 set command "exec /usr/sbin/prelink -qNR --no-exec-shield $arg"
159 verbose -log "command is $command"
160 set result [catch $command output]
161 verbose -log "result is $result"
162 verbose -log "output is $output"
163 if {$result == 0 && $output == ""} {
173 proc symlink_resolve {file} {
175 while {[file type $file] == "link"} {
176 set target [file readlink $file]
177 if {[file pathtype $target] == "relative"} {
178 set src2 [file dirname $file]/$target
182 verbose -log "Resolved symlink $file targetting $target as $src2"
185 set loop [expr $loop + 1]
187 fail "Looping symlink resolution for $file"
194 proc copy {src dest} {
195 set src [symlink_resolve $src]
196 # Test name would contain build-id hash for symlink-unresolved $src.
197 set test "copy [file tail $src] to [file tail $dest]"
198 set command "file copy -force $src $dest"
199 verbose -log "command is $command"
200 if [catch $command] {
209 proc strip_debug {dest} {
210 set test "strip [file tail $dest]"
211 set strip_program [transform strip]
212 set command "exec $strip_program --strip-debug $dest"
213 verbose -log "command is $command"
214 if [catch $command] {
223 # `runto' does not check we stopped really at the function we specified.
224 proc reach {func command} {
227 if [gdb_breakpoint $func allow-pending] {
228 set test "reach $func"
229 gdb_test_multiple $command $test {
230 -re "Breakpoint \[0-9\]+, $func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
233 -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in $func \\(\\)( from .*)?\r\n$gdb_prompt $" {
240 proc test_core {file} {
241 global srcdir subdir gdb_prompt
243 set corefile [core_find $file]
244 if {$corefile == ""} {
250 # Clear it to never find any separate debug infos in $debug_root.
251 gdb_test "set debug-file-directory" "" "set debug-file-directory for core"
252 gdb_reinitialize_dir $srcdir/$subdir
255 # Do not check the binary filename as it may be truncated.
256 gdb_test "core-file $corefile" "Core was generated by .*\r\n#0 .*" "core loaded"
258 gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
261 proc test_ld {file ifmain trynosym} {
262 global srcdir subdir gdb_prompt
264 # First test normal `file'-command loaded $FILE with symbols.
268 # Clear it to never find any separate debug infos in $debug_root.
269 gdb_test "set debug-file-directory"
270 gdb_reinitialize_dir $srcdir/$subdir
275 gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
278 reach "main" continue
280 reach "libfunc" continue
282 gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
292 set old_ldprefix $pf_prefix
293 lappend pf_prefix "symbol-less:"
295 # Test also `exec-file'-command loaded $FILE - therefore without symbols.
296 # SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
300 # Clear it to never find any separate debug infos in $debug_root.
301 gdb_test "set debug-file-directory"
302 gdb_reinitialize_dir $srcdir/$subdir
304 # Test no (error) message has been printed by `exec-file'.
305 set escapedfile [string_to_regexp $file]
306 gdb_test "exec-file $file" "exec-file $escapedfile" "load"
311 set test "info files"
313 gdb_test_multiple $test $test {
314 -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
315 set entrynohex $expect_out(1,string)
319 if {$entrynohex != ""} {
320 gdb_test "break *0x$entrynohex" "" "break at entry point"
321 gdb_test "continue" "\r\nBreakpoint \[0-9\]+, 0x0*$entrynohex in .*" "entry point reached"
324 # There is no symbol to break at ld.so. Moreover it can exit with an
326 gdb_test "run" "Program exited (normally|with code \[0-9\]+)\\." "ld.so exit"
329 set pf_prefix $old_ldprefix
332 # Create separate binaries for each testcase - to make the possible reported
333 # problem reproducible after the whole test run finishes.
335 set old_ldprefix $pf_prefix
336 foreach ldprelink {NO YES} {
337 foreach ldsepdebug {NO IN SEP} {
338 # Skip running the ldsepdebug test if we do not have system separate
339 # debug info available.
340 if {$interp_system_debug == "" && $ldsepdebug == "SEP"} {
344 set ldname "LDprelink${ldprelink}debug${ldsepdebug}"
345 set interp $binprefix-$ldname
347 # prelink needs to always prelink all the dependencies to do any file
348 # modifications of its files. ld.so also needs all the dependencies to
349 # be prelinked to omit the relocation process. In-memory file offsets
350 # are not dependent whether ld.so went the prelink way or through the
351 # relocation process.
353 # For GDB we are not interested whether prelink succeeds as it is
354 # transparent to GDB. GDB is being tested for differences of file
355 # offsets vs. in-memory offsets. So we have to prelink even ld.so for
356 # the BIN modification to happen but we need to restore the original
357 # possibly unprelinked ld.so to test all the combinations for GDB.
358 set interp_saved ${interp}-saved
360 set pf_prefix $old_ldprefix
361 lappend pf_prefix "$ldname:"
363 if {$ldsepdebug == "NO"} {
364 copy $interp_system $interp
365 # Never call strip-debug before unprelink:
366 # prelink: ...: Section .note.gnu.build-id created after prelinking
367 if ![prelinkNO $interp] {
371 } elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
372 copy $interp_system $interp
373 } elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
374 copy $interp_system $interp
375 copy $interp_system_debug "${interp}.debug"
376 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
377 if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
380 set test "eu-unstrip unprelinked:[file tail $interp_system] + [file tail $interp_system_debug] to [file tail $interp]"
381 set command "exec eu-unstrip -o $interp $interp ${interp}.debug"
382 verbose -log "command is $command"
383 if [catch $command] {
390 } elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
391 copy $interp_system $interp
392 # eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
393 if ![prelinkNO $interp] {
396 gdb_gnu_strip_debug $interp
397 } elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
398 copy $interp_system $interp
399 copy $interp_system_debug "${interp}.debug"
402 if {$ldsepdebug == "SEP"} {
403 if ![prelinkNO "${interp}.debug"] {
407 file delete "${interp}.debug"
410 if ![prelink$ldprelink $interp] {
413 test_ld $interp 0 [expr {$ldsepdebug == "NO"}]
415 if ![copy $interp $interp_saved] {
418 set old_binprefix $pf_prefix
419 foreach binprelink {NO YES} {
420 foreach binsepdebug {NO IN SEP} {
421 foreach binpie {NO YES} {
422 # This combination is not possible, non-PIE (fixed address)
423 # binary cannot be prelinked to any (other) address.
424 if {$binprelink == "YES" && $binpie == "NO"} {
428 set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
429 set exec $binprefix-$binname
432 set pf_prefix $old_binprefix
433 lappend pf_prefix "$binname:"
435 set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
436 lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
437 if {$binsepdebug != "NO"} {
440 if {$binpie == "YES"} {
441 lappend opts {additional_flags=-fPIE -pie}
443 if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
446 if {$binsepdebug == "SEP"} {
447 gdb_gnu_strip_debug $exec
448 # Just a sanity check. As gdb_gnu_strip_debug uses the
449 # "[file dirname $exec]/.debug/[file tail $exec].debug"
450 # variant delete the higher-priority exec.debug file.
451 file delete "$exec.debug"
454 # Supply a self-sufficent directory $dir with the required
455 # libraries. To make an executable properly prelinked all
456 # its dependencies on libraries must be also prelinked. If
457 # some of the system libraries is currently not prelinked
458 # we have no right to prelink (modify it) at its current
461 file delete -force $dir
464 set command "ldd $exec"
465 set result [catch "exec $command" output]
466 verbose -log "result of $command is $result"
467 verbose -log "output of $command is $output"
468 if {$result != 0 || $output == ""} {
474 # gdb testsuite will put there also needless -lm.
475 set test "$command output contains libc"
476 set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
477 if {[llength $libc] == 0} {
484 for {set i 1} {$i < [llength $libc]} {incr i 2} {
485 set abspath [lindex $libc $i]
486 set dest "$dir/[file tail $abspath]"
491 if {[prelink$binprelink "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]" $exec]
492 && [copy $interp_saved $interp]} {
493 test_ld $exec 1 [expr {$binsepdebug == "NO"}]
499 file delete $interp_saved
502 set pf_prefix $old_ldprefix