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