1 # Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Tests for shared object file relocation. If two shared objects have
19 # the same load address (actually, overlapping load spaces), one of
20 # them gets relocated at load-time. Check that gdb gets the right
21 # values for the debugging and minimal symbols.
23 if {[skip_shlib_tests]} {
32 # This file uses shreloc.c, shreloc1.c and shreloc2.c
36 set workdir ${objdir}/${subdir}
37 set testfile "shreloc"
38 set libfile1 "shreloc1"
39 set libfile2 "shreloc2"
40 set srcfile $srcdir/$subdir/$testfile.c
41 set lib1src $srcdir/$subdir/$libfile1.c
42 set lib2src $srcdir/$subdir/$libfile2.c
43 set binfile $objdir/$subdir/$testfile
44 set lib1_sl $objdir/$subdir/$libfile1.sl
45 set lib2_sl $objdir/$subdir/$libfile2.sl
47 if [get_compiler_info ${binfile}] {
52 set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl]
54 if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
55 lappend lib_opts "ldflags=-Wl,--image-base,0x04000000"
58 if [test_compiler_info "xlc-*"] {
60 # IBM's xlc compiler does not add static variables to the ELF symbol
61 # table by default. We need this option to make the variables show
62 # up in "maint print msymbols".
64 lappend lib_opts "additional_flags=-qstatsym"
68 if { [gdb_compile_shlib $lib1src $lib1_sl $lib_opts] != ""} {
69 untested "Could not build $lib1_sl."
71 } elseif { [gdb_compile_shlib $lib2src $lib2_sl $lib_opts] != ""} {
72 untested "Could not build $lib1_s2."
74 } elseif { [gdb_compile $srcfile $binfile executable $exec_opts] != ""} {
75 untested "Could not build $binfile."
79 # Start with a fresh gdb.
83 gdb_reinitialize_dir $srcdir/$subdir
84 gdb_load ${workdir}/shreloc
85 gdb_load_shlibs $lib1_sl $lib2_sl
87 # Load up the shared objects
88 if ![runto_main] then {
89 fail "Can't run to main"
93 proc get_var_address { var } {
99 # $6 = (int (*)()) 0x24 <function_bar>
101 gdb_test_multiple "print &${var}" "get address of ${var}" {
102 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
104 pass "get address of ${var}"
105 if { $expect_out(1,string) == "0" } {
108 return $expect_out(1,string)
116 # Check debugging symbol relocations
119 # Check extern function for relocation
120 set fn_1_addr [get_var_address fn_1]
121 set fn_2_addr [get_var_address fn_2]
123 if { "${fn_1_addr}" == "${fn_2_addr}" } {
124 fail "relocated extern functions have different addresses"
126 pass "relocated extern functions have different addresses"
129 # Check extern var for relocation
130 set extern_var_1_addr [get_var_address extern_var_1]
131 set extern_var_2_addr [get_var_address extern_var_2]
133 if { "${extern_var_1_addr}" == "${extern_var_2_addr}" } {
134 fail "relocated extern variables have different addresses"
136 pass "relocated extern variables have different addresses"
139 # Check static var for relocation
140 set static_var_1_addr [get_var_address static_var_1]
141 set static_var_2_addr [get_var_address static_var_2]
143 if { "${static_var_1_addr}" == "${static_var_2_addr}" } {
144 fail "relocated static variables have different addresses"
146 pass "relocated static variables have different addresses"
150 # Check minimal symbol relocations
153 proc send_gdb_discard { command } {
154 # Send a command to gdb and discard output up to the next prompt
159 gdb_test_multiple "${command}" "${command}" {
160 -re ".*\[\r\n]+${gdb_prompt} $" {
164 fail "{$command} (timeout)"
170 proc get_msym_addrs { var msymfile } {
171 # Extract the list of values for symbols matching var in the
172 # minimal symbol output file
174 global gdb_prompt hex
177 send_gdb "shell grep -E \" ${var}(\[ \t\]+.*)?\$\" ${msymfile}\n"
181 -re "\[\[\]\[ 0-9\]+\] . (${hex}) ${var}(\[ \t\]+\[^\r\n\]*)?\[\r\n\]+" {
182 set result [concat $result $expect_out(1,string)]
185 -re "$gdb_prompt $" {
186 pass "get_msym_addrs ${var}"
190 -re "\[^\r\n\]*\[\r\n\]+" {
195 fail "get_msym_addrs ${var} (timeout)"
202 proc check_same {var msymfile} {
203 # Check that the minimal symbol values matching var are the same
205 set len [llength [lsort -unique [get_msym_addrs "${var}" "${msymfile}"]]]
214 proc check_different {var msymfile} {
215 # Check that the minimal symbol values matching var are different
217 set addr_list [lsort [get_msym_addrs "${var}" "${msymfile}"]]
220 if { [llength ${addr_list}] < 2 } {
224 foreach addr ${addr_list} {
225 if { ${prev} == ${addr} } {
234 set msymfile "${workdir}/shreloc.txt"
236 if [send_gdb_discard "maint print msymbols ${msymfile}"] {
237 if {[check_different "static_var_\[12\]" "${msymfile}"]} {
238 pass "(msymbol) relocated static vars have different addresses"
240 fail "(msymbol) relocated static vars have different addresses"
243 if {[check_different "extern_var_\[12\]" "${msymfile}"]} {
244 pass "(msymbol) relocated extern vars have different addresses"
246 fail "(msymbol) relocated extern vars have different addresses"
249 if {[check_different "fn_\[12\]" "${msymfile}"]} {
250 pass "(msymbol) relocated functions have different addresses"
252 fail "(msymbol) relocated functions have different addresses"
256 if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
258 # We know the names of some absolute symbols included in the
259 # portable-executable (DLL) format. Check that they didn't get
262 # A better approach would be include absolute symbols via the assembler.
264 if {[check_same "_minor_os_version__" "${msymfile}"]} {
265 pass "Absolute symbols not relocated"
267 fail "Absolute symbols not relocated"