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