1 # Copyright 2002-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
16 # relocate.exp -- Expect script to test loading symbols from unrelocated
22 remote_exec build "rm -f ${binfile}"
23 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {debug}] != "" } {
24 untested "failed to compile"
30 #Check that invalid options are rejected.
31 foreach x {"-raednow" "readnow" "foo" "-readnow s"} {
32 set word [lindex $x [expr [llength $x]-1]]
33 gdb_test "add-symbol-file ${binfile} 0 $x" \
34 "Unrecognized argument \"$word\"" \
35 "add-symbol-file: unknown option $word"
38 # Check that we can pass parameters using any position in the command
40 set test "add-symbol-file positionless -readnow"
41 gdb_test_multiple "add-symbol-file -readnow $binfile 0x100 -s .bss 0x3" $test {
42 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
43 gdb_test "n" "Not confirmed\." $test
46 # When we use -s as the first argument, the section will be printed
48 set test "add-symbol-file positionless -s"
49 gdb_test_multiple "add-symbol-file -s .bss 0x3 -readnow $binfile 0x100" $test {
50 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
51 gdb_test "n" "Not confirmed\." $test
54 set test "add-symbol-file positionless -s, no -readnow"
55 gdb_test_multiple "add-symbol-file $binfile 0x100 -s .bss 0x3" $test {
56 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.bss_addr = 0x3\r\n\\(y or n\\) " {
57 gdb_test "n" "Not confirmed\." $test
60 # Check that passing "-s .text", no matter the position, always has
62 set test "add-symbol-file different -s .text, after file"
63 gdb_test_multiple "add-symbol-file $binfile 0x100 -s .text 0x200" $test {
64 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.text_addr = 0x200\r\n\\(y or n\\) " {
65 gdb_test "n" "Not confirmed\." $test
68 set test "add-symbol-file different -s .text, before file"
69 gdb_test_multiple "add-symbol-file -s .text 0x200 $binfile 0x100" $test {
70 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\.text_addr = 0x200\r\n\\(y or n\\) " {
71 gdb_test "n" "Not confirmed\." $test
74 # Check that passing a single "-s .text" is equivalent to passing
75 # the text address in a positional argument.
76 set test "add-symbol-file -s .text, no address"
77 gdb_test_multiple "add-symbol-file $binfile -s .text 0x100" $test {
78 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
79 gdb_test "n" "Not confirmed\." $test
82 # Check section addresses can be omitted.
83 set test "add-symbol-file no address"
84 gdb_test_multiple "add-symbol-file $binfile" $test {
85 -re "add symbol table from file \"${binfile}\"\r\n\\(y or n\\) " {
86 gdb_test "n" "Not confirmed\." $test
89 # Test that passing "--" disables option processing.
90 gdb_test "add-symbol-file -- $binfile 0x100 -s .bss 0x3" \
91 "Unrecognized argument \"-s\"" \
92 "add-symbol-file with -- disables option processing"
93 set test "add-symbol-file with -- disables option processing, non-existent filename"
94 gdb_test_multiple "add-symbol-file -- -non-existent-file 0x100" $test {
95 -re "add symbol table from file \"-non-existent-file\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
96 gdb_test "y" "-non-existent-file: No such file or directory\." $test
99 # Test that passing the wrong number of arguments to '-s' leads to an
101 gdb_test "add-symbol-file $binfile -s" \
102 "Missing section name after \"-s\"" \
103 "add-symbol-file with -s without section name"
104 gdb_test "add-symbol-file $binfile -s .bss" \
105 "Missing section address after \"-s\"" \
106 "add-symbol-file with -s without section address"
107 # Test that '-s' accepts section names with '-'
108 set test "add-symbol-file with -s using section name starting with dash"
109 gdb_test_multiple "add-symbol-file -s -section-name 0x200 $binfile 0x100" $test {
110 -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\t\-section-name_addr = 0x200\r\n\\(y or n\\) " {
111 gdb_test "n" "Not confirmed\." $test
114 # Since we're here, might as well test the 'symbol-file' command and
115 # if its arguments can also be passed at any position.
116 gdb_test "symbol-file -readnow $binfile" \
117 "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
118 "symbol-file with -readnow first"
120 gdb_test "symbol-file $binfile -readnow" \
121 "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
122 "symbol-file with -readnow second"
123 gdb_test "symbol-file -readnow" \
124 "no symbol file name was specified" \
125 "symbol-file without filename"
126 gdb_test "symbol-file -- -non-existent-file" \
127 "-non-existent-file: No such file or directory\." \
128 "symbol-file with -- disables option processing"
130 gdb_test "symbol-file -readnow -- $binfile" \
131 "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
132 "symbol-file with -- and -readnow"
133 gdb_test "symbol-file -- $binfile -readnow" \
134 "Unrecognized argument \"-readnow\"" \
135 "symbol-file with -- and -readnow, invalid option"
139 gdb_test "add-symbol-file ${binfile} 0 -s" \
140 "Missing section name after .-s." \
141 "add-symbol-file bare -s"
142 gdb_test "add-symbol-file ${binfile} 0 -s .whatever" \
143 "Missing section address after .-s." \
144 "add-symbol-file missing address"
146 # Load the object file.
147 gdb_test "add-symbol-file ${binfile} 0" \
148 "Reading symbols from .*${testfile}\\.o\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
149 "add-symbol-file ${testfile}.o 0" \
150 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x0\[\r\n\]+\\(y or n\\) " \
153 with_test_prefix "print addresses, static vars" {
154 # Print the addresses of static variables.
155 set static_foo_addr [get_var_address static_foo]
156 set static_bar_addr [get_var_address static_bar]
159 # Make sure they have different addresses.
160 if { "${static_foo_addr}" == "${static_bar_addr}" } {
161 fail "static variables have different addresses"
163 pass "static variables have different addresses"
166 with_test_prefix "print addresses, global vars" {
167 # Print the addresses of global variables.
168 set global_foo_addr [get_var_address global_foo]
169 set global_bar_addr [get_var_address global_bar]
172 # Make sure they have different addresses.
173 if { "${global_foo_addr}" == "${global_bar_addr}" } {
174 fail "global variables have different addresses"
176 pass "global variables have different addresses"
179 with_test_prefix "print addresses, functions" {
180 # Print the addresses of functions.
181 set function_foo_addr [get_var_address function_foo]
182 set function_bar_addr [get_var_address function_bar]
185 # Make sure they have different addresses.
186 if { "${function_foo_addr}" == "${function_bar_addr}" } {
187 fail "functions have different addresses"
189 pass "functions have different addresses"
192 # Now use a variable as an offset to add-symbol-file, and check that
193 # the functions' addresses change.
197 gdb_test_no_output "set \$offset = 0x10000"
199 # Load the object file.
200 gdb_test "add-symbol-file ${binfile} \$offset" \
201 "Reading symbols from .*${testfile}\\.o\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
202 "add-symbol-file ${testfile}.o \$offset" \
203 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x10000\[\r\n\]+\\(y or n\\) " \
206 # Print the addresses of functions.
207 set new_function_foo_addr [get_var_address function_foo]
209 # Make sure they have different addresses.
210 if { "${function_foo_addr}" == "${new_function_foo_addr}" } {
211 fail "function foo has a different address"
213 pass "function foo has a different address"
216 # Load the object using symbol-file with an offset and check that
217 # all addresses are moved by that offset.
221 set readnow_re "Expanding full symbols from ${binfile}\.\.\."
222 gdb_test "symbol-file -o $offset $binfile" \
223 "Reading symbols from ${binfile}\.\.\.(\r\n$readnow_re)?" \
224 "symbol-file with offset"
226 with_test_prefix "static vars" {
227 # Make sure the address of a static variable is moved by offset.
228 set new_static_foo_addr [get_var_address static_foo]
229 gdb_assert {${new_static_foo_addr} == ${static_foo_addr} + $offset} \
230 "static variable foo is moved by offset"
233 with_test_prefix "global vars" {
234 # Make sure the address of a global variable is moved by offset.
235 set new_global_foo_addr [get_var_address global_foo]
236 gdb_assert {${new_global_foo_addr} == ${global_foo_addr} + $offset} \
237 "global variable foo is moved by offset"
240 with_test_prefix "functions" {
241 # Make sure the address of a function is moved by offset.
242 set new_function_foo_addr [get_var_address function_foo]
243 gdb_assert {${new_function_foo_addr} == ${function_foo_addr} + $offset} \
244 "function foo is moved by offset"
247 # Load the object using add-symbol-file with an offset and check that
248 # all addresses are moved by that offset.
252 gdb_test "add-symbol-file -o $offset $binfile" \
253 "Reading symbols from ${binfile}\.\.\.(\r\n$readnow_re)?" \
254 "add-symbol-file with offset" \
255 "add symbol table from file \".*${testfile}\\.o\" with all sections offset by $offset\[\r\n\]+\\(y or n\\) " \
258 with_test_prefix "static scope, 2nd" {
259 # Make sure the address of a static variable is moved by offset.
260 set new_static_foo_addr [get_var_address static_foo]
261 gdb_assert { ${new_static_foo_addr} == ${static_foo_addr} + $offset } \
262 "static variable foo is moved by offset"
265 with_test_prefix "global vars, 2nd" {
266 # Make sure the address of a global variable is moved by offset.
267 set new_global_foo_addr [get_var_address global_foo]
268 gdb_assert { ${new_global_foo_addr} == ${global_foo_addr} + $offset } \
269 "global variable foo is moved by offset"
272 with_test_prefix "functions, 2nd" {
273 # Make sure the address of a function is moved by offset.
274 set new_function_foo_addr [get_var_address function_foo]
275 gdb_assert { ${new_function_foo_addr} == ${function_foo_addr} + $offset } \
276 "function foo is moved by offset"
279 # Re-load the object giving an explicit address for .text
281 set text [ format "0x%x" [expr ${function_foo_addr} + 0x20000] ]
283 gdb_test "add-symbol-file $binfile -o $offset $text" \
284 "Reading symbols from ${binfile}\.\.\.(\r\n$readnow_re)?" \
285 "add-symbol-file with offset, text address given" \
286 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = ${text}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
289 with_test_prefix "functions, 3rd" {
290 # Make sure function has a different addresses now.
291 set function_foo_addr [get_var_address function_foo]
292 gdb_assert { ${function_foo_addr} != ${new_function_foo_addr} } \
293 "function foo has a different address"
296 # Re-load the object giving an explicit address for .data
298 set data [ format "0x%x" [expr ${global_foo_addr} + 0x20000] ]
300 gdb_test "add-symbol-file $binfile -o $offset -s .data $data" \
301 "Reading symbols from ${binfile}\.\.\.(\r\n$readnow_re)?" \
302 "add-symbol-file with offset, data address given" \
303 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.data_addr = ${data}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
306 with_test_prefix "global vars, 3rd" {
307 # Make sure variable has a different addresses now.
308 set global_foo_addr [get_var_address global_foo]
309 gdb_assert { ${global_foo_addr} != ${new_global_foo_addr} } \
310 "global variable foo has a different address"
313 # Now try loading the object as an exec-file; we should be able to print
314 # the values of variables after we do this.
317 gdb_file_cmd ${binfile}
319 # Check the values of the variables.
320 gdb_test "print static_foo" "\\\$$decimal = 1"
321 gdb_test "print static_bar" "\\\$$decimal = 2"
322 gdb_test "print global_foo" "\\\$$decimal = 3"
323 gdb_test "print global_bar" "\\\$$decimal = 4"