]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/shreloc.exp
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / shreloc.exp
CommitLineData
6aba47ca 1# Copyright (C) 2003, 2005, 2007 Free Software Foundation, Inc.
53df362e
RG
2#
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 2 of the License, or
6# (at your option) any later version.
7#
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.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16#
17
53df362e
RG
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.
22
d9407aaa
NC
23if {[istarget *-elf*] || [istarget *-coff] || [istarget *-aout]} then {
24 verbose "test skipped - shared object files not supported by this target."
25 return 0
26}
27
53df362e
RG
28if $tracelevel then {
29 strace $tracelevel
30}
31
32#
33# This file uses shreloc.c, shreloc1.c and shreloc2.c
34#
35
36set prms_id 0
37set bug_id 0
38
39set workdir ${objdir}/${subdir}
96b0c7ac
DJ
40set testfile "shreloc"
41set libfile1 "shreloc1"
42set libfile2 "shreloc2"
43set srcfile $srcdir/$subdir/$testfile.c
44set lib1src $srcdir/$subdir/$libfile1.c
45set lib2src $srcdir/$subdir/$libfile2.c
46set binfile $objdir/$subdir/$testfile
47set lib1_sl $objdir/$subdir/$libfile1.sl
48set lib2_sl $objdir/$subdir/$libfile2.sl
49
50if [get_compiler_info ${binfile}] {
51 return -1
53df362e
RG
52}
53
96b0c7ac
DJ
54set lib_opts "debug"
55set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl]
53df362e
RG
56
57if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
05cfdb42 58 lappend lib_opts "ldflags=-Wl,--image-base,0x04000000"
53df362e
RG
59}
60
96b0c7ac
DJ
61if [test_compiler_info "xlc-*"] {
62
63 # IBM's xlc compiler does not add static variables to the ELF symbol
64 # table by default. We need this option to make the variables show
65 # up in "maint print msymbols".
66
67 lappend lib_opts "additional_flags=-qstatsym"
68
53df362e
RG
69}
70
96b0c7ac
DJ
71if { [gdb_compile_shlib $lib1src $lib1_sl $lib_opts] != ""} {
72 untested "Could not build $lib1_sl."
73 return -1
74} elseif { [gdb_compile_shlib $lib2src $lib2_sl $lib_opts] != ""} {
75 untested "Could not build $lib1_s2."
76 return -1
77} elseif { [gdb_compile $srcfile $binfile executable $exec_opts] != ""} {
78 untested "Could not build $binfile."
53df362e
RG
79 return -1
80}
81
96b0c7ac
DJ
82# Start with a fresh gdb.
83
53df362e
RG
84gdb_exit
85gdb_start
86gdb_reinitialize_dir $srcdir/$subdir
87gdb_load ${workdir}/shreloc
88
89# Load up the shared objects
90if ![runto_main] then {
91 fail "Can't run to main"
92 return 0
93}
94
95proc get_var_address { var } {
96 global gdb_prompt hex
97
98 send_gdb "print &${var}\n"
99 # Match output like:
100 # $1 = (int *) 0x0
101 # $5 = (int (*)()) 0
102 # $6 = (int (*)()) 0x24 <function_bar>
103 gdb_expect {
104 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $"
105 {
106 pass "get address of ${var}"
107 if { $expect_out(1,string) == "0" } {
108 return "0x0"
109 } else {
110 return $expect_out(1,string)
111 }
112 }
113 -re "${gdb_prompt} $"
114 { fail "get address of ${var} (unknown output)" }
115 timeout
116 { fail "get address of ${var} (timeout)" }
117 }
118 return ""
119}
120
121#
122# Check debugging symbol relocations
123#
124
125# Check extern function for relocation
126set fn_1_addr [get_var_address fn_1]
127set fn_2_addr [get_var_address fn_2]
128
129if { "${fn_1_addr}" == "${fn_2_addr}" } {
130 fail "relocated extern functions have different addresses"
131} else {
132 pass "relocated extern functions have different addresses"
133}
134
135# Check extern var for relocation
136set extern_var_1_addr [get_var_address extern_var_1]
137set extern_var_2_addr [get_var_address extern_var_2]
138
139if { "${extern_var_1_addr}" == "${extern_var_2_addr}" } {
140 fail "relocated extern variables have different addresses"
141} else {
142 pass "relocated extern variables have different addresses"
143}
144
145# Check static var for relocation
146set static_var_1_addr [get_var_address static_var_1]
147set static_var_2_addr [get_var_address static_var_2]
148
149if { "${static_var_1_addr}" == "${static_var_2_addr}" } {
150 fail "relocated static variables have different addresses"
151} else {
152 pass "relocated static variables have different addresses"
153}
154
155#
156# Check minimal symbol relocations
157#
158
159proc send_gdb_discard { command } {
160 # Send a command to gdb and discard output up to the next prompt
161
162 global gdb_prompt
163
164 send_gdb "${command}\n"
165
166 # Discard output
167 gdb_expect {
168 -re ".*\[\r\n]+${gdb_prompt} $" {
169 return 1
170 }
171 timeout {
172 fail "{$command} (timeout)"
173 return 0
174 }
175 }
176}
177
178proc get_msym_addrs { var msymfile } {
179 # Extract the list of values for symbols matching var in the
180 # minimal symbol output file
181
182 global gdb_prompt hex
183 set result ""
184
185 send_gdb "shell grep -E \" ${var}(\[ \t\]+.*)?\$\" ${msymfile}\n"
186
187 while 1 {
188 gdb_expect {
189 -re "\[\[\]\[ 0-9\]+\] . (${hex}) ${var}(\[ \t\]+\[^\r\n\]*)?\[\r\n\]+" {
190 set result [concat $result $expect_out(1,string)]
191 }
192
193 -re "$gdb_prompt $" {
a8b7528f 194 pass "get_msym_addrs ${var}"
53df362e
RG
195 return "${result}"
196 }
197
198 -re "\[^\r\n\]*\[\r\n\]+" {
199 # Skip
200 }
201
202 timeout {
203 fail "get_msym_addrs ${var} (timeout)"
204 return -1
205 }
206 }
207 }
208}
209
210proc check_same {var msymfile} {
211 # Check that the minimal symbol values matching var are the same
212
213 set len [llength [lsort -unique [get_msym_addrs "${var}" "${msymfile}"]]]
214
215 if { $len == 1 } {
216 return 1
217 } else {
218 return 0
219 }
220}
221
222proc check_different {var msymfile} {
223 # Check that the minimal symbol values matching var are different
224
225 set addr_list [lsort [get_msym_addrs "${var}" "${msymfile}"]]
226 set prev ""
227
228 if { [llength ${addr_list}] < 2 } {
229 return 0
230 }
231
232 foreach addr ${addr_list} {
233 if { ${prev} == ${addr} } {
234 return 0
235 }
236 set prev ${addr}
237 }
238
239 return 1
240}
241
242set msymfile "${workdir}/shreloc.txt"
243
244if [send_gdb_discard "maint print msymbols ${msymfile}"] {
245 if {[check_different "static_var_\[12\]" "${msymfile}"]} {
246 pass "(msymbol) relocated static vars have different addresses"
247 } else {
248 fail "(msymbol) relocated static vars have different addresses"
249 }
250
251 if {[check_different "extern_var_\[12\]" "${msymfile}"]} {
252 pass "(msymbol) relocated extern vars have different addresses"
253 } else {
254 fail "(msymbol) relocated extern vars have different addresses"
255 }
256
257 if {[check_different "fn_\[12\]" "${msymfile}"]} {
258 pass "(msymbol) relocated functions have different addresses"
259 } else {
260 fail "(msymbol) relocated functions have different addresses"
261 }
262}
263
264if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
265 #
266 # We know the names of some absolute symbols included in the
267 # portable-executable (DLL) format. Check that they didn't get
268 # relocated.
269 #
270 # A better approach would be include absolute symbols via the assembler.
271 #
272 if {[check_same "_minor_os_version__" "${msymfile}"]} {
273 pass "Absolute symbols not relocated"
274 } else {
275 fail "Absolute symbols not relocated"
276 }
277}