]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/shreloc.exp
Introduce language_defn::lookup_symbol_local
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / shreloc.exp
CommitLineData
1d506c26 1# Copyright (C) 2003-2024 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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
53df362e
RG
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
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
53df362e
RG
15#
16
53df362e
RG
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
d6195dc9 22require allow_shlib_tests
d9407aaa 23
53df362e
RG
24#
25# This file uses shreloc.c, shreloc1.c and shreloc2.c
26#
27
53df362e 28
0ab77f5f
TT
29standard_testfile .c shreloc1.c shreloc2.c
30
31set srcfile $srcdir/$subdir/$srcfile
32set lib1src $srcdir/$subdir/$srcfile2
33set lib2src $srcdir/$subdir/$srcfile3
34set binfile [standard_output_file $testfile]
35set lib1_sl [standard_output_file shreloc1.sl]
36set lib2_sl [standard_output_file shreloc2.sl]
96b0c7ac 37
96b0c7ac
DJ
38set lib_opts "debug"
39set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl]
53df362e
RG
40
41if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
05cfdb42 42 lappend lib_opts "ldflags=-Wl,--image-base,0x04000000"
53df362e
RG
43}
44
96b0c7ac
DJ
45if [test_compiler_info "xlc-*"] {
46
47 # IBM's xlc compiler does not add static variables to the ELF symbol
48 # table by default. We need this option to make the variables show
49 # up in "maint print msymbols".
50
51 lappend lib_opts "additional_flags=-qstatsym"
52
53df362e
RG
53}
54
96b0c7ac 55if { [gdb_compile_shlib $lib1src $lib1_sl $lib_opts] != ""} {
bc6c7af4 56 untested "could not build $lib1_sl."
96b0c7ac
DJ
57 return -1
58} elseif { [gdb_compile_shlib $lib2src $lib2_sl $lib_opts] != ""} {
bc6c7af4 59 untested "could not build $lib1_s2."
96b0c7ac
DJ
60 return -1
61} elseif { [gdb_compile $srcfile $binfile executable $exec_opts] != ""} {
bc6c7af4 62 untested "could not build $binfile."
53df362e
RG
63 return -1
64}
65
96b0c7ac
DJ
66# Start with a fresh gdb.
67
0ab77f5f 68clean_restart $binfile
d9019901
SM
69gdb_load_shlib $lib1_sl
70gdb_load_shlib $lib2_sl
53df362e
RG
71
72# Load up the shared objects
65a33d75 73if {![runto_main]} {
53df362e
RG
74 return 0
75}
76
53df362e
RG
77#
78# Check debugging symbol relocations
79#
80
81# Check extern function for relocation
82set fn_1_addr [get_var_address fn_1]
83set fn_2_addr [get_var_address fn_2]
84
85if { "${fn_1_addr}" == "${fn_2_addr}" } {
86 fail "relocated extern functions have different addresses"
87} else {
88 pass "relocated extern functions have different addresses"
89}
90
91# Check extern var for relocation
92set extern_var_1_addr [get_var_address extern_var_1]
93set extern_var_2_addr [get_var_address extern_var_2]
94
95if { "${extern_var_1_addr}" == "${extern_var_2_addr}" } {
96 fail "relocated extern variables have different addresses"
97} else {
98 pass "relocated extern variables have different addresses"
99}
100
101# Check static var for relocation
102set static_var_1_addr [get_var_address static_var_1]
103set static_var_2_addr [get_var_address static_var_2]
104
105if { "${static_var_1_addr}" == "${static_var_2_addr}" } {
106 fail "relocated static variables have different addresses"
107} else {
108 pass "relocated static variables have different addresses"
109}
110
111#
112# Check minimal symbol relocations
113#
114
115proc send_gdb_discard { command } {
116 # Send a command to gdb and discard output up to the next prompt
117
118 global gdb_prompt
119
53df362e 120 # Discard output
02746bbc 121 gdb_test_multiple "${command}" "${command}" {
53df362e
RG
122 -re ".*\[\r\n]+${gdb_prompt} $" {
123 return 1
124 }
125 timeout {
126 fail "{$command} (timeout)"
127 return 0
128 }
129 }
130}
131
132proc get_msym_addrs { var msymfile } {
133 # Extract the list of values for symbols matching var in the
134 # minimal symbol output file
135
136 global gdb_prompt hex
137 set result ""
138
139 send_gdb "shell grep -E \" ${var}(\[ \t\]+.*)?\$\" ${msymfile}\n"
140
141 while 1 {
142 gdb_expect {
143 -re "\[\[\]\[ 0-9\]+\] . (${hex}) ${var}(\[ \t\]+\[^\r\n\]*)?\[\r\n\]+" {
144 set result [concat $result $expect_out(1,string)]
145 }
146
147 -re "$gdb_prompt $" {
a8b7528f 148 pass "get_msym_addrs ${var}"
53df362e
RG
149 return "${result}"
150 }
151
152 -re "\[^\r\n\]*\[\r\n\]+" {
153 # Skip
154 }
155
156 timeout {
157 fail "get_msym_addrs ${var} (timeout)"
158 return -1
159 }
160 }
161 }
162}
163
164proc check_same {var msymfile} {
165 # Check that the minimal symbol values matching var are the same
166
167 set len [llength [lsort -unique [get_msym_addrs "${var}" "${msymfile}"]]]
168
169 if { $len == 1 } {
170 return 1
171 } else {
172 return 0
173 }
174}
175
176proc check_different {var msymfile} {
177 # Check that the minimal symbol values matching var are different
178
179 set addr_list [lsort [get_msym_addrs "${var}" "${msymfile}"]]
180 set prev ""
181
182 if { [llength ${addr_list}] < 2 } {
183 return 0
184 }
185
186 foreach addr ${addr_list} {
187 if { ${prev} == ${addr} } {
188 return 0
189 }
190 set prev ${addr}
191 }
192
193 return 1
194}
195
2ed2ad44
YQ
196if [is_remote host] {
197 set msymfile shreloc.txt
198} else {
199 set msymfile [standard_output_file shreloc.txt]
200}
53df362e
RG
201
202if [send_gdb_discard "maint print msymbols ${msymfile}"] {
203 if {[check_different "static_var_\[12\]" "${msymfile}"]} {
204 pass "(msymbol) relocated static vars have different addresses"
205 } else {
206 fail "(msymbol) relocated static vars have different addresses"
207 }
208
209 if {[check_different "extern_var_\[12\]" "${msymfile}"]} {
210 pass "(msymbol) relocated extern vars have different addresses"
211 } else {
212 fail "(msymbol) relocated extern vars have different addresses"
213 }
214
215 if {[check_different "fn_\[12\]" "${msymfile}"]} {
216 pass "(msymbol) relocated functions have different addresses"
217 } else {
218 fail "(msymbol) relocated functions have different addresses"
219 }
220}
221
222if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } {
223 #
224 # We know the names of some absolute symbols included in the
225 # portable-executable (DLL) format. Check that they didn't get
226 # relocated.
227 #
228 # A better approach would be include absolute symbols via the assembler.
229 #
230 if {[check_same "_minor_os_version__" "${msymfile}"]} {
bc6c7af4 231 pass "absolute symbols not relocated"
53df362e 232 } else {
bc6c7af4 233 fail "absolute symbols not relocated"
53df362e
RG
234 }
235}