-# Copyright 2010 Free Software Foundation, Inc.
+# Copyright 2010-2012 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This test only works on GNU/Linux.
-if { ![isnative] || [is_remote host] || ![istarget *-linux*] || [skip_shlib_tests]} {
+if { ![isnative] || [is_remote host] || [target_info exists use_gdb_stub]
+ || ![istarget *-linux*] || [skip_shlib_tests]} {
continue
}
+load_lib prelink-support.exp
+
set test "break-interp"
set binprefix ${objdir}/${subdir}/${test}
# Only to get the $interp_system name.
return -1
}
-# Use -soname so that it is listed with " => " by ldd and this testcase makes
-# a copy of ${binfile_lib} for each prelink variant.
+# Use -soname so that the new library gets copied by build_executable_own_libs.
-if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug additional_flags=-Wl,-soname,${test}.so]] != ""} {
+if {[gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} [list debug ldflags=-Wl,-soname,${test}.so]] != ""} {
return -1
}
return -1
}
-# Return the interpreter filename string.
-# Return "" if no interpreter was found.
-proc section_get {exec section} {
- global objdir
- global subdir
- set tmp "${objdir}/${subdir}/break-interp.interp"
- set objcopy_program [transform objcopy]
-
- set command "exec $objcopy_program -O binary --set-section-flags $section=A --change-section-address $section=0 -j $section $exec $tmp"
- verbose -log "command is $command"
- set result [catch $command output]
- verbose -log "result is $result"
- verbose -log "output is $output"
- if {$result == 1} {
- return ""
- }
- set fi [open $tmp]
- fconfigure $fi -translation binary
- set data [read $fi]
- close $fi
- #file delete $tmp
- # .interp has size $len + 1 but .gnu_debuglink contains garbage after \000.
- set len [string first \000 $data]
- if {$len < 0} {
- verbose -log "section $section not found"
- return ""
- }
- set retval [string range $data 0 [expr $len - 1]]
- verbose -log "section $section is <$retval>"
- return $retval
-}
-
# Note: The separate debug info file content build-id/crc32 are not verified
# contrary to the GDB search algorithm skipping non-matching ones.
proc system_debug_get {exec} {
set interp_system_debug [system_debug_get $interp_system]
verbose -log "$interp_system has debug $interp_system_debug"
-proc prelinkNO_run {arg} {
- set command "exec /usr/sbin/prelink -uN $arg"
- verbose -log "command is $command"
- set result [catch $command output]
- verbose -log "result is $result"
- verbose -log "output is $output"
- return [list $result $output]
-}
-
-proc prelinkNO {arg {name {}}} {
- if {$name == ""} {
- set name [file tail $arg]
- }
- set test "unprelink $name"
- set run [prelinkNO_run $arg]
- set result [lindex $run 0]
- set output [lindex $run 1]
- if {$result == 0 && $output == ""} {
- verbose -log "$name has been now unprelinked"
- set run [prelinkNO_run $arg]
- set result [lindex $run 0]
- set output [lindex $run 1]
- }
- # Last line does miss the trailing \n.
- if {$result == 1 && [regexp {^(/usr/sbin/prelink[^\r\n]*: [^ ]* does not have .gnu.prelink_undo section\n?)*$} $output]} {
- pass $test
- return 1
- } else {
- fail $test
- return 0
- }
+proc prelinkNO {arg {name ""}} {
+ return [prelink_no $arg $name]
}
proc prelinkYES {arg {name ""}} {
- if {$name == ""} {
- set name [file tail $arg]
- }
-
- # Try to unprelink it first so that if it has been already prelinked before
- # we get different address now and the result is not affected by the
- # previous $arg state..
- prelinkNO $arg "$name pre-unprelink"
-
- set test "prelink $name"
- set command "exec /usr/sbin/prelink -qNR --no-exec-shield $arg"
- verbose -log "command is $command"
- set result [catch $command output]
- verbose -log "result is $result"
- verbose -log "output is $output"
- if {$result == 0 && $output == ""} {
- pass $test
- return 1
- } elseif {$result == 1 \
- && [string match -nocase "*: Not enough room to add .dynamic entry" $output]} {
- # Linker should have reserved some entries for prelink.
- xfail $test
- return 0
- } else {
- fail $test
- return 0
- }
-}
-
-# Resolve symlinks.
-proc symlink_resolve {file} {
- set loop 0
- while {[file type $file] == "link"} {
- set target [file readlink $file]
- if {[file pathtype $target] == "relative"} {
- set src2 [file dirname $file]/$target
- } else {
- set src2 $target
- }
- verbose -log "Resolved symlink $file targetting $target as $src2"
- set file $src2
-
- set loop [expr $loop + 1]
- if {$loop > 30} {
- fail "Looping symlink resolution for $file"
- return ""
- }
- }
- return $file
-}
-
-proc copy {src dest} {
- set src [symlink_resolve $src]
- # Test name would contain build-id hash for symlink-unresolved $src.
- set test "copy [file tail $src] to [file tail $dest]"
- set command "file copy -force $src $dest"
- verbose -log "command is $command"
- if [catch $command] {
- fail $test
- return 0
- } else {
- pass $test
- return 1
- }
+ return [prelink_yes $arg $name]
}
proc strip_debug {dest} {
}
}
-# `runto' does not check we stopped really at the function we specified.
-# DISPLACEMENT can be "NONE", "ZERO" or "NONZERO"
-proc reach {func command displacement} {
+# Implementation of reach.
+
+proc reach_1 {func command displacement} {
global gdb_prompt expect_out
- global pf_prefix
- set old_ldprefix $pf_prefix
- lappend pf_prefix "reach-$func:"
+ if {$func == "_dl_debug_state"} {
+ # Breakpoint on _dl_debug_state can have problems due to its overlap
+ # with the existing internal breakpoint from GDB.
+ gdb_test_no_output "set stop-on-solib-events 1"
+ } elseif {! [gdb_breakpoint $func allow-pending]} {
+ return
+ }
- if [gdb_breakpoint $func allow-pending] {
- set test "reach"
- set test_displacement "seen displacement message as $displacement"
- gdb_test_multiple $command $test {
- -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
- # Missing "$gdb_prompt $" is intentional.
- if {$expect_out(1,string) == "0x0"} {
- set case "ZERO"
- } else {
- set case "NONZERO"
- }
- if {$displacement == $case} {
- pass $test_displacement
- # Permit multiple such messages.
- set displacement "FOUND-$displacement"
- } elseif {$displacement != "FOUND-$case"} {
- fail $test_displacement
- }
- exp_continue
+ set test "reach"
+ set test_displacement "seen displacement message as $displacement"
+ set debug_state_count 0
+ gdb_test_multiple $command $test {
+ -re "Using PIE \\(Position Independent Executable\\) displacement (0x\[0-9a-f\]+) " {
+ # Missing "$gdb_prompt $" is intentional.
+ if {$expect_out(1,string) == "0x0"} {
+ set case "ZERO"
+ } else {
+ set case "NONZERO"
+ }
+ if {$displacement == $case || $displacement == "PRESENT"} {
+ pass $test_displacement
+ set displacement "FOUND-$displacement"
+ } else {
+ fail $test_displacement
}
- -re "Breakpoint \[0-9\]+, $func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
+ exp_continue
+ }
+ -re "Breakpoint \[0-9\]+, \\.?(__GI_)?$func \\(.*\\) at .*:\[0-9\]+\r\n.*$gdb_prompt $" {
+ if {$func == "_dl_debug_state"} {
+ fail $test
+ } else {
pass $test
}
- -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in $func \\(\\).*\r\n$gdb_prompt $" {
+ }
+ -re "Breakpoint \[0-9\]+, \[0-9xa-f\]+ in \\.?(__GI_)?$func \\(\\).*\r\n$gdb_prompt $" {
+ if {$func == "_dl_debug_state"} {
+ fail $test
+ } else {
pass $test
}
}
- if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
- fail $test_displacement
+ -re "Stopped due to (spurious )?shared library event.*\r\n$gdb_prompt $" {
+ if {$func == "_dl_debug_state"} {
+ if {$debug_state_count == 0} {
+ # First stop does not yet relocate the _start function
+ # descriptor on ppc64.
+ set debug_state_count 1
+ send_gdb "continue\n"
+ exp_continue
+ } else {
+ pass $test
+ }
+ } else {
+ fail $test
+ }
}
}
+ if ![regexp {^(NONE|FOUND-.*)$} $displacement] {
+ fail $test_displacement
+ }
- set pf_prefix $old_ldprefix
+ if {$func == "_dl_debug_state"} {
+ gdb_test_no_output "set stop-on-solib-events 0"
+ }
+}
+
+# `runto' does not check we stopped really at the function we specified.
+# DISPLACEMENT can be "NONE" for no message to be present, "ZERO" for
+# displacement of 0 bytes to be present, "NONZERO" for displacement of non-0
+# bytes to be present and "PRESENT" if both "ZERO" and "NONZERO" are valid.
+proc reach {func command displacement} {
+ with_test_prefix "reach-$func" {
+ reach_1 $func $command $displacement
+ }
}
-proc test_core {file displacement} {
+proc test_core {file displacement} { with_test_prefix "core" {
global srcdir subdir gdb_prompt expect_out
set corefile [core_find $file {} "segv"]
return
}
- global pf_prefix
- set old_ldprefix $pf_prefix
- lappend pf_prefix "core:"
-
gdb_exit
gdb_start
# Clear it to never find any separate debug infos in $debug_root.
} else {
set case "NONZERO"
}
- if {$displacement == $case} {
+ if {$displacement == $case || $displacement == "PRESENT"} {
pass $test_displacement
- # Permit multiple such messages.
set displacement "FOUND-$displacement"
- } elseif {$displacement != "FOUND-$case"} {
+ } else {
fail $test_displacement
}
exp_continue
}
gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "core main bt"
+}}
- set pf_prefix $old_ldprefix
-}
-
-proc test_attach_gdb {file pid displacement prefix} {
+proc test_attach_gdb {file pid displacement prefix} { with_test_prefix "$prefix" {
global gdb_prompt expect_out
- global pf_prefix
- set old_ldprefix $pf_prefix
- lappend pf_prefix "$prefix:"
-
gdb_exit
gdb_start
# Print the "PIE (Position Independent Executable) displacement" message.
gdb_test_no_output "set verbose on"
- if {$file != ""} {
- gdb_test "file $file" "Reading symbols from .*done\\." "file"
- }
+ gdb_test "file $file" "Reading symbols from .*done\\." "file"
set test "attach"
gdb_test_multiple "attach $pid" $test {
} else {
set case "NONZERO"
}
- if {$displacement == $case} {
+ if {$displacement == $case || $displacement == "PRESENT"} {
pass $test_displacement
- # Permit multiple such messages.
set displacement "FOUND-$displacement"
- } elseif {$displacement != "FOUND-$case"} {
+ } else {
fail $test_displacement
}
exp_continue
gdb_test "bt" "#\[0-9\]+ +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#\[0-9\]+ +\[^\r\n\]*\\mmain\\M.*" "attach main bt"
gdb_exit
-
- set pf_prefix $old_ldprefix
-}
+}}
proc test_attach {file displacement {relink_args ""}} {
global board_info
+ global exec
gdb_exit
}
if {$relink_args == ""} {
- test_attach_gdb "" $pid $displacement "attach"
+ test_attach_gdb $exec $pid $displacement "attach"
} else {
# These could be rather passed as arguments.
- global exec interp_saved interp
+ global interp_saved interp
foreach relink {YES NO} {
- if {[prelink$relink $relink_args [file tail $exec]]
- && [copy $interp_saved $interp]} {
+ # Formerly this test was testing only prelinking of $EXEC. As the
+ # prelink command automatically prelinks all of $EXEC's libraries,
+ # even $INTERP got prelinked. Therefore, we formerly had to
+ # `[file_copy $interp_saved $interp]' to make $INTERP not affected
+ # by this prelinking of $EXEC.
+ #
+ # But now we need to test even prelinking of $INTERP. We could
+ # create a separate test to test just the $INTERP prelinking. For
+ # test simplicity, we merged this test and the test above by not
+ # restoring $INTERP after $EXEC prelink. $INTERP gets restored
+ # later below.
+ #
+ # `(wrong library or version mismatch?)' messages are printed for
+ # $binfile_lib on platforms converting REL->RELA relocations by
+ # prelink (such as on i386). There is no reliable way to verify
+ # the library file matches the running library in such case but
+ # GDB at least attempts to set the right displacement. We test
+ # `libfunc' is present in the backtrace and therefore the
+ # displacement has been guessed right.
+
+ if [prelink$relink $relink_args [file tail $exec]] {
# /proc/PID/exe cannot be loaded as it is "EXECNAME (deleted)".
test_attach_gdb $exec $pid $displacement "attach-relink$relink"
}
}
+ file_copy $interp_saved $interp
}
remote_exec host "kill -9 $pid"
}
proc test_ld {file ifmain trynosym displacement} {
- global srcdir subdir gdb_prompt expect_out
+ global srcdir subdir gdb_prompt expect_out inferior_exited_re
# First test normal `file'-command loaded $FILE with symbols.
# Print the "PIE (Position Independent Executable) displacement" message.
gdb_test_no_output "set verbose on"
- reach "dl_main" "run segv" $displacement
+ # We want to test the re-run of a PIE in the case where the executable
+ # is loaded with a different displacement, but disable-randomization
+ # prevents that from happening. So turn it off.
+ gdb_test "set disable-randomization off"
+
+ if $ifmain {
+ gdb_test_no_output "set args segv"
+ } else {
+ global objdir binfile_test
+
+ # ld.so needs some executable to run to reach _dl_debug_state.
+ gdb_test_no_output "set args ${objdir}/${subdir}/$binfile_test" "set args OBJDIR/${subdir}/$binfile_test"
+ }
- gdb_test "bt" "#0 +\[^\r\n\]*\\mdl_main\\M.*" "dl bt"
+ reach "_dl_debug_state" "run" $displacement
+
+ gdb_test "bt" "#0 +\[^\r\n\]*\\m(__GI_)?_dl_debug_state\\M.*" "dl bt"
if $ifmain {
- # Displacement message will be printed the second time on initializing
- # the linker from svr4_special_symbol_handling. If any ANOFFSET has
- # been already set as non-zero the detection will no longer be run.
- if {$displacement == "NONZERO"} {
- set displacement_main "NONE"
- } else {
- set displacement_main $displacement
- }
- reach "main" continue $displacement_main
+ reach "main" continue "NONE"
reach "libfunc" continue "NONE"
gdb_test "bt" "#0 +\[^\r\n\]*\\mlibfunc\\M\[^\r\n\]*\r\n#1 +\[^\r\n\]*\\mmain\\M.*" "main bt"
+ }
+
+ # Try re-run if the new PIE displacement takes effect.
+ gdb_test "kill" "" "kill" {Kill the program being debugged\? \(y or n\) } "y"
+ reach "_dl_debug_state" "run" $displacement
+ if $ifmain {
test_core $file $displacement
test_attach $file $displacement
global pf_prefix
set old_ldprefix $pf_prefix
- lappend pf_prefix "symbol-less:"
+ append pf_prefix " symbol-less:"
# Test also `exec-file'-command loaded $FILE - therefore without symbols.
# SYMBOL_OBJFILE is not available and only EXEC_BFD must be used.
gdb_test "exec-file $file" "exec-file $escapedfile" "load"
if $ifmain {
- reach "dl_main" run $displacement
+ reach "_dl_debug_state" run $displacement
+ # Use two separate gdb_test_multiple statements to avoid timeouts due
+ # to slow processing of wildcard capturing long output
set test "info files"
set entrynohex ""
gdb_test_multiple $test $test {
- -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n.*$gdb_prompt $" {
+ -re "\r\n\[\t \]*Entry point:\[\t \]*0x(\[0-9a-f\]+)\r\n" {
set entrynohex $expect_out(1,string)
- pass $test
+ gdb_test_multiple "" $test {
+ -re "\r\n$gdb_prompt $" {
+ pass $test
+ }
+ }
+ }
+ }
+
+ # `info sym' cannot be tested for .opd as the binary may not have
+ # symbols.
+ if {[istarget powerpc64-*] && [is_lp64_target]} {
+ set test "convert entry point"
+ gdb_test_multiple "p *(void(*)(void) *) 0x$entrynohex" $test {
+ -re " =( \\(\[^0-9\]*\\))? 0x(\[0-9a-f\]+)( < \[^\r\n\]*)?\r\n$gdb_prompt $" {
+ set entrynohex $expect_out(2,string)
+ pass $test
+ }
}
}
if {$entrynohex != ""} {
} else {
set case "NONZERO"
}
- if {$displacement == $case} {
+ if {$displacement == $case || $displacement == "PRESENT"} {
pass $test_displacement
- # Permit multiple such messages.
set displacement "FOUND-$displacement"
- } elseif {$displacement != "FOUND-$case"} {
+ } else {
fail $test_displacement
}
exp_continue
}
- -re "Program exited (normally|with code \[0-9\]+)\\.\r\n$gdb_prompt $" {
+ -re "$inferior_exited_re (normally|with code \[0-9\]+).\r\n$gdb_prompt $" {
# Do not check the binary filename as it may be truncated.
pass $test
}
set interp_saved ${interp}-saved
set pf_prefix $old_ldprefix
- lappend pf_prefix "$ldname:"
+ append pf_prefix " $ldname:"
if {$ldsepdebug == "NO"} {
- copy $interp_system $interp
+ file_copy $interp_system $interp
# Never call strip-debug before unprelink:
# prelink: ...: Section .note.gnu.build-id created after prelinking
if ![prelinkNO $interp] {
}
strip_debug $interp
} elseif {$ldsepdebug == "IN" && $interp_system_debug == ""} {
- copy $interp_system $interp
+ file_copy $interp_system $interp
} elseif {$ldsepdebug == "IN" && $interp_system_debug != ""} {
- copy $interp_system $interp
- copy $interp_system_debug "${interp}.debug"
+ file_copy $interp_system $interp
+ file_copy $interp_system_debug "${interp}.debug"
# eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
if {![prelinkNO $interp] || ![prelinkNO "${interp}.debug"]} {
continue
pass $test
}
} elseif {$ldsepdebug == "SEP" && $interp_system_debug == ""} {
- copy $interp_system $interp
+ file_copy $interp_system $interp
# eu-unstrip: DWARF data in '...' not adjusted for prelinking bias; consider prelink -u
if ![prelinkNO $interp] {
continue
}
gdb_gnu_strip_debug $interp
} elseif {$ldsepdebug == "SEP" && $interp_system_debug != ""} {
- copy $interp_system $interp
- copy $interp_system_debug "${interp}.debug"
+ file_copy $interp_system $interp
+ file_copy $interp_system_debug "${interp}.debug"
}
if {$ldsepdebug == "SEP"} {
if {$ldprelink == "NO"} {
set displacement "NONZERO"
} else {
- set displacement "ZERO"
+ # x86* kernel loads prelinked PIE binary at its prelinked address
+ # but ppc* kernel loads it at a random address. prelink normally
+ # skips PIE binaries during the system scan.
+ set displacement "PRESENT"
}
test_ld $interp 0 [expr {$ldsepdebug == "NO"}] $displacement
- if ![copy $interp $interp_saved] {
+ if ![file_copy $interp $interp_saved] {
continue
}
set old_binprefix $pf_prefix
set binname "BINprelink${binprelink}debug${binsepdebug}pie${binpie}"
set exec $binprefix-$binname
- set dir ${exec}.d
set pf_prefix $old_binprefix
- lappend pf_prefix "$binname:"
+ append pf_prefix " $binname:"
- set opts "additional_flags=-Wl,--dynamic-linker,$interp,-rpath,$dir"
- lappend opts "additional_flags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
+ set opts "ldflags=-Wl,$binfile_lib,-rpath,[file dirname $binfile_lib]"
if {$binsepdebug != "NO"} {
lappend opts {debug}
}
if {$binpie != "NO"} {
lappend opts {additional_flags=-fPIE -pie}
}
- if {[build_executable ${test}.exp [file tail $exec] $srcfile $opts] == -1} {
+
+ set dir ${exec}.d
+ set relink_args [build_executable_own_libs ${test}.exp [file tail $exec] $srcfile $opts $interp $dir]
+ if {$relink_args == ""} {
continue;
}
+
if {$binsepdebug == "SEP"} {
gdb_gnu_strip_debug $exec
}
- # Supply a self-sufficent directory $dir with the required
- # libraries. To make an executable properly prelinked all
- # its dependencies on libraries must be also prelinked. If
- # some of the system libraries is currently not prelinked
- # we have no right to prelink (modify it) at its current
- # system place.
-
- file delete -force $dir
- file mkdir $dir
-
- set command "ldd $exec"
- set test "ldd [file tail $exec]"
- set result [catch "exec $command" output]
- verbose -log "result of $command is $result"
- verbose -log "output of $command is $output"
- if {$result != 0 || $output == ""} {
- fail $test
- } else {
- pass $test
- }
-
- # gdb testsuite will put there also needless -lm.
- set test "$test output contains libc"
- set libc [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
- if {[llength $libc] == 0} {
- fail $test
- } else {
- pass $test
- }
-
- set dests {}
- for {set i 1} {$i < [llength $libc]} {incr i 2} {
- set abspath [lindex $libc $i]
- set dest "$dir/[file tail $abspath]"
- copy $abspath $dest
- lappend dests $dest
- }
-
if {$binpie == "NO"} {
set displacement "NONE"
} elseif {$binprelink == "NO"} {
set displacement "NONZERO"
} else {
- set displacement "ZERO"
+ # x86* kernel loads prelinked PIE binary at its
+ # prelinked address but ppc* kernel loads it at
+ # a random address. prelink normally skips PIE
+ # binaries during the system scan.
+ set displacement "PRESENT"
}
- set relink_args "--dynamic-linker=$interp --ld-library-path=$dir $exec $interp [concat $dests]"
if {[prelink$binprelink $relink_args [file tail $exec]]
- && [copy $interp_saved $interp]} {
+ && [file_copy $interp_saved $interp]} {
if {$binpie != "ATTACH"} {
test_ld $exec 1 [expr {$binsepdebug == "NO"}] $displacement
} else {