OSDN Git Service

b3408a87ad46aa1c4d0d646d4fc4302e3d75da55
[pf3gnuchains/pf3gnuchains4x.git] / gdb / testsuite / gdb.base / shreloc.exp
1 # Copyright (C) 2003, 2005, 2007 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 # 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
23 if {[skip_shlib_tests]} {
24     return 0
25 }
26
27 if $tracelevel then {
28     strace $tracelevel
29 }
30
31 #
32 # This file uses shreloc.c, shreloc1.c and shreloc2.c
33 #
34
35 set prms_id 0
36 set bug_id 0
37
38 set workdir ${objdir}/${subdir}
39 set testfile "shreloc"
40 set libfile1 "shreloc1"
41 set libfile2 "shreloc2"
42 set srcfile $srcdir/$subdir/$testfile.c
43 set lib1src $srcdir/$subdir/$libfile1.c
44 set lib2src $srcdir/$subdir/$libfile2.c
45 set binfile $objdir/$subdir/$testfile
46 set lib1_sl $objdir/$subdir/$libfile1.sl
47 set lib2_sl $objdir/$subdir/$libfile2.sl
48
49 if [get_compiler_info ${binfile}] {
50     return -1
51 }
52
53 set lib_opts "debug"
54 set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl]
55
56 if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
57     lappend lib_opts "ldflags=-Wl,--image-base,0x04000000"
58 }
59
60 if [test_compiler_info "xlc-*"] {
61
62     # IBM's xlc compiler does not add static variables to the ELF symbol 
63     # table by default.  We need this option to make the variables show 
64     # up in "maint print msymbols". 
65
66     lappend lib_opts "additional_flags=-qstatsym"
67
68 }
69
70 if { [gdb_compile_shlib $lib1src $lib1_sl $lib_opts] != ""} {
71     untested "Could not build $lib1_sl."
72     return -1
73 } elseif { [gdb_compile_shlib $lib2src $lib2_sl $lib_opts] != ""} {
74     untested "Could not build $lib1_s2."
75     return -1
76 } elseif { [gdb_compile $srcfile $binfile executable $exec_opts] != ""} {
77     untested "Could not build $binfile."
78     return -1
79 }
80
81 # Start with a fresh gdb.
82
83 gdb_exit
84 gdb_start
85 gdb_reinitialize_dir $srcdir/$subdir
86 gdb_load ${workdir}/shreloc
87 gdb_load_shlibs $lib1_sl $lib2_sl
88
89 # Load up the shared objects
90 if ![runto_main] then {
91     fail "Can't run to main"
92     return 0
93 }
94
95 proc 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
126 set fn_1_addr [get_var_address fn_1]
127 set fn_2_addr [get_var_address fn_2]
128
129 if { "${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
136 set extern_var_1_addr [get_var_address extern_var_1]
137 set extern_var_2_addr [get_var_address extern_var_2]
138
139 if { "${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
146 set static_var_1_addr [get_var_address static_var_1]
147 set static_var_2_addr [get_var_address static_var_2]
148
149 if { "${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
159 proc 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
178 proc 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 $" {
194                 pass "get_msym_addrs ${var}"
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
210 proc 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
222 proc 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
242 set msymfile "${workdir}/shreloc.txt"
243
244 if [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
264 if {([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 }