1# Copyright 2002-2020 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# relocate.exp -- Expect script to test loading symbols from unrelocated
17#		  object files.
18
19standard_testfile .c
20append binfile .o
21
22remote_exec build "rm -f ${binfile}"
23if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {debug}] != "" } {
24     untested "failed to compile"
25     return -1
26}
27
28gdb_exit
29gdb_start
30gdb_reinitialize_dir $srcdir/$subdir
31
32#Check that invalid options are rejected.
33foreach x {"-raednow" "readnow" "foo" "-readnow s"} {
34    set word [lindex $x [expr [llength $x]-1]]
35    gdb_test "add-symbol-file ${binfile} 0 $x" \
36	"Unrecognized argument \"$word\"" \
37	"add-symbol-file: unknown option $word"
38}
39
40# Check that we can pass parameters using any position in the command
41# line.
42set test "add-symbol-file positionless -readnow"
43gdb_test_multiple "add-symbol-file -readnow $binfile 0x100 -s .bss 0x3" $test {
44    -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\\) " {
45	gdb_test "n" "Not confirmed\." $test
46    }
47}
48# When we use -s as the first argument, the section will be printed
49# first as well.
50set test "add-symbol-file positionless -s"
51gdb_test_multiple "add-symbol-file -s .bss 0x3 -readnow $binfile 0x100" $test {
52    -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\\) " {
53	gdb_test "n" "Not confirmed\." $test
54    }
55}
56set test "add-symbol-file positionless -s, no -readnow"
57gdb_test_multiple "add-symbol-file $binfile 0x100 -s .bss 0x3" $test {
58    -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\\) " {
59	gdb_test "n" "Not confirmed\." $test
60    }
61}
62# Check that passing "-s .text", no matter the position, always has
63# the same result.
64set test "add-symbol-file different -s .text, after file"
65gdb_test_multiple "add-symbol-file $binfile 0x100 -s .text 0x200" $test {
66    -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\\) " {
67	gdb_test "n" "Not confirmed\." $test
68    }
69}
70set test "add-symbol-file different -s .text, before file"
71gdb_test_multiple "add-symbol-file -s .text 0x200 $binfile 0x100" $test {
72    -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\\) " {
73	gdb_test "n" "Not confirmed\." $test
74    }
75}
76# Check that passing a single "-s .text" is equivalent to passing
77# the text address in a positional argument.
78set test "add-symbol-file -s .text, no address"
79gdb_test_multiple "add-symbol-file $binfile -s .text 0x100" $test {
80    -re "add symbol table from file \"${binfile}\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
81	gdb_test "n" "Not confirmed\." $test
82    }
83}
84# Check section addresses can be omitted.
85set test "add-symbol-file no address"
86gdb_test_multiple "add-symbol-file $binfile" $test {
87    -re "add symbol table from file \"${binfile}\"\r\n\\(y or n\\) " {
88	gdb_test "n" "Not confirmed\." $test
89    }
90}
91# Test that passing "--" disables option processing.
92gdb_test "add-symbol-file -- $binfile 0x100 -s .bss 0x3" \
93    "Unrecognized argument \"-s\"" \
94    "add-symbol-file with -- disables option processing"
95set test "add-symbol-file with -- disables option processing, non-existent filename"
96gdb_test_multiple "add-symbol-file -- -non-existent-file 0x100" $test {
97    -re "add symbol table from file \"-non-existent-file\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
98	gdb_test "y" "-non-existent-file: No such file or directory\." $test
99    }
100}
101# Test that passing the wrong number of arguments to '-s' leads to an
102# error.
103gdb_test "add-symbol-file $binfile -s" \
104    "Missing section name after \"-s\"" \
105    "add-symbol-file with -s without section name"
106gdb_test "add-symbol-file $binfile -s .bss" \
107    "Missing section address after \"-s\"" \
108    "add-symbol-file with -s without section address"
109# Test that '-s' accepts section names with '-'
110set test "add-symbol-file with -s using section name starting with dash"
111gdb_test_multiple "add-symbol-file -s -section-name 0x200 $binfile 0x100" $test {
112    -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\\) " {
113	gdb_test "n" "Not confirmed\." $test
114    }
115}
116# Since we're here, might as well test the 'symbol-file' command and
117# if its arguments can also be passed at any position.
118gdb_test "symbol-file -readnow $binfile" \
119    "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
120    "symbol-file with -readnow first"
121clean_restart
122gdb_test "symbol-file $binfile -readnow" \
123    "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
124    "symbol-file with -readnow second"
125gdb_test "symbol-file -readnow" \
126    "no symbol file name was specified" \
127    "symbol-file without filename"
128gdb_test "symbol-file -- -non-existent-file" \
129    "-non-existent-file: No such file or directory\." \
130    "symbol-file with -- disables option processing"
131clean_restart
132gdb_test "symbol-file -readnow -- $binfile" \
133    "Reading symbols from ${binfile}\.\.\.\r\nExpanding full symbols from ${binfile}\.\.\." \
134    "symbol-file with -- and -readnow"
135gdb_test "symbol-file -- $binfile -readnow" \
136    "Unrecognized argument \"-readnow\"" \
137    "symbol-file with -- and -readnow, invalid option"
138
139clean_restart
140
141gdb_test "add-symbol-file ${binfile} 0 -s" \
142    "Missing section name after .-s." \
143    "add-symbol-file bare -s"
144gdb_test "add-symbol-file ${binfile} 0 -s .whatever" \
145    "Missing section address after .-s." \
146    "add-symbol-file missing address"
147
148# Load the object file.
149gdb_test "add-symbol-file ${binfile} 0" \
150	"Reading symbols from .*${testfile}\\.o\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
151	"add-symbol-file ${testfile}.o 0" \
152	"add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x0\[\r\n\]+\\(y or n\\) " \
153	"y"
154
155with_test_prefix "print addresses, static vars" {
156    # Print the addresses of static variables.
157    set static_foo_addr [get_var_address static_foo]
158    set static_bar_addr [get_var_address static_bar]
159}
160
161# Make sure they have different addresses.
162if { "${static_foo_addr}" == "${static_bar_addr}" } {
163  fail "static variables have different addresses"
164} else {
165  pass "static variables have different addresses"
166}
167
168with_test_prefix "print addresses, global vars" {
169    # Print the addresses of global variables.
170    set global_foo_addr [get_var_address global_foo]
171    set global_bar_addr [get_var_address global_bar]
172}
173
174# Make sure they have different addresses.
175if { "${global_foo_addr}" == "${global_bar_addr}" } {
176  fail "global variables have different addresses"
177} else {
178  pass "global variables have different addresses"
179}
180
181with_test_prefix "print addresses, functions" {
182    # Print the addresses of functions.
183    set function_foo_addr [get_var_address function_foo]
184    set function_bar_addr [get_var_address function_bar]
185}
186
187# Make sure they have different addresses.
188if { "${function_foo_addr}" == "${function_bar_addr}" } {
189  fail "functions have different addresses"
190} else {
191  pass "functions have different addresses"
192}
193
194# Now use a variable as an offset to add-symbol-file, and check that
195# the functions' addresses change.
196
197gdb_exit
198gdb_start
199gdb_reinitialize_dir $srcdir/$subdir
200
201gdb_test_no_output "set \$offset = 0x10000"
202
203# Load the object file.
204gdb_test "add-symbol-file ${binfile} \$offset" \
205	"Reading symbols from .*${testfile}\\.o\\.\\.\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
206	"add-symbol-file ${testfile}.o \$offset" \
207	"add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x10000\[\r\n\]+\\(y or n\\) " \
208	"y"
209
210# Print the addresses of functions.
211set new_function_foo_addr [get_var_address function_foo]
212
213# Make sure they have different addresses.
214if { "${function_foo_addr}" == "${new_function_foo_addr}" } {
215  fail "function foo has a different address"
216} else {
217  pass "function foo has a different address"
218}
219
220# Load the object using symbol-file with an offset and check that
221# all addresses are moved by that offset.
222
223set offset 0x10000
224clean_restart
225gdb_test "symbol-file -o $offset $binfile" \
226    "Reading symbols from ${binfile}\.\.\." \
227    "symbol-file with offset"
228
229with_test_prefix "static vars" {
230    # Make sure the address of a static variable is moved by offset.
231    set new_static_foo_addr [get_var_address static_foo]
232    gdb_assert {${new_static_foo_addr} == ${static_foo_addr} + $offset} \
233	"static variable foo is moved by offset"
234}
235
236with_test_prefix "global vars" {
237    # Make sure the address of a global variable is moved by offset.
238    set new_global_foo_addr [get_var_address global_foo]
239    gdb_assert {${new_global_foo_addr} == ${global_foo_addr} + $offset} \
240	"global variable foo is moved by offset"
241}
242
243with_test_prefix "functions" {
244    # Make sure the address of a function is moved by offset.
245    set new_function_foo_addr [get_var_address function_foo]
246    gdb_assert {${new_function_foo_addr} == ${function_foo_addr} + $offset} \
247	"function foo is moved by offset"
248}
249
250# Load the object using add-symbol-file with an offset and check that
251# all addresses are moved by that offset.
252
253set offset 0x10000
254clean_restart
255gdb_test "add-symbol-file -o $offset $binfile" \
256    "Reading symbols from ${binfile}\.\.\." \
257    "add-symbol-file with offset" \
258    "add symbol table from file \".*${testfile}\\.o\" with all sections offset by $offset\[\r\n\]+\\(y or n\\) " \
259    "y"
260
261with_test_prefix "static scope, 2nd" {
262    # Make sure the address of a static variable is moved by offset.
263    set new_static_foo_addr [get_var_address static_foo]
264    gdb_assert { ${new_static_foo_addr} == ${static_foo_addr} + $offset } \
265	"static variable foo is moved by offset"
266}
267
268with_test_prefix "global vars, 2nd" {
269    # Make sure the address of a global variable is moved by offset.
270    set new_global_foo_addr [get_var_address global_foo]
271    gdb_assert { ${new_global_foo_addr} == ${global_foo_addr} + $offset } \
272	"global variable foo is moved by offset"
273}
274
275with_test_prefix "functions, 2nd" {
276    # Make sure the address of a function is moved by offset.
277    set new_function_foo_addr [get_var_address function_foo]
278    gdb_assert { ${new_function_foo_addr} == ${function_foo_addr} + $offset } \
279	"function foo is moved by offset"
280}
281
282# Re-load the object giving an explicit address for .text
283
284set text [ format "0x%x" [expr ${function_foo_addr} + 0x20000] ]
285clean_restart
286gdb_test "add-symbol-file $binfile -o $offset $text" \
287    "Reading symbols from ${binfile}\.\.\." \
288    "add-symbol-file with offset, text address given" \
289    "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\\) " \
290    "y"
291
292with_test_prefix "functions, 3rd" {
293    # Make sure function has a different addresses now.
294    set function_foo_addr [get_var_address function_foo]
295    gdb_assert { ${function_foo_addr} != ${new_function_foo_addr} } \
296	"function foo has a different address"
297}
298
299# Re-load the object giving an explicit address for .data
300
301set data [ format "0x%x" [expr ${global_foo_addr} + 0x20000] ]
302clean_restart
303gdb_test "add-symbol-file $binfile -o $offset -s .data $data" \
304    "Reading symbols from ${binfile}\.\.\." \
305    "add-symbol-file with offset, data address given" \
306    "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\\) " \
307    "y"
308
309with_test_prefix "global vars, 3rd" {
310    # Make sure variable has a different addresses now.
311    set global_foo_addr [get_var_address global_foo]
312    gdb_assert { ${global_foo_addr} != ${new_global_foo_addr} } \
313	"global variable foo has a different address"
314}
315
316# Now try loading the object as an exec-file; we should be able to print
317# the values of variables after we do this.
318
319gdb_exit
320gdb_start
321gdb_reinitialize_dir $srcdir/$subdir
322gdb_file_cmd ${binfile}
323
324# Check the values of the variables.
325gdb_test "print static_foo" "\\\$$decimal = 1"
326gdb_test "print static_bar" "\\\$$decimal = 2"
327gdb_test "print global_foo" "\\\$$decimal = 3"
328gdb_test "print global_bar" "\\\$$decimal = 4"
329