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