d0a839ae59c39136f5081cd833957eae3030290d
[external/binutils.git] / gdb / testsuite / gdb.base / relocate.exp
1 # Copyright 2002-2017 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
19 standard_testfile .c
20 append binfile .o
21
22 remote_exec build "rm -f ${binfile}"
23 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {debug}] != "" } {
24      untested "failed to compile"
25      return -1
26 }
27
28 gdb_exit
29 gdb_start
30 gdb_reinitialize_dir $srcdir/$subdir
31
32 #Check that invalid options are rejected.
33 foreach 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.
42 set test "add-symbol-file positionless -readnow"
43 gdb_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.
50 set test "add-symbol-file positionless -s"
51 gdb_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 }
56 set test "add-symbol-file positionless -s, no -readnow"
57 gdb_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.
64 set test "add-symbol-file different -s .text, after file"
65 gdb_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 }
70 set test "add-symbol-file different -s .text, before file"
71 gdb_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 # Test that passing "--" disables option processing.
77 gdb_test "add-symbol-file -- $binfile 0x100 -s .bss 0x3" \
78     "Unrecognized argument \"-s\"" \
79     "add-symbol-file with -- disables option processing"
80 set test "add-symbol-file with -- disables option processing, non-existent filename"
81 gdb_test_multiple "add-symbol-file -- -non-existent-file 0x100" $test {
82     -re "add symbol table from file \"-non-existent-file\" at\r\n\t\.text_addr = 0x100\r\n\\(y or n\\) " {
83         gdb_test "y" "-non-existent-file: No such file or directory\." $test
84     }
85 }
86 # Test that passing the wrong number of arguments to '-s' leads to an
87 # error.
88 gdb_test "add-symbol-file $binfile -s" \
89     "Missing section name after \"-s\"" \
90     "add-symbol-file with -s without section name"
91 gdb_test "add-symbol-file $binfile -s .bss" \
92     "Missing section address after \"-s\"" \
93     "add-symbol-file with -s without section address"
94 # Test that '-s' accepts section names with '-'
95 set test "add-symbol-file with -s using section name starting with dash"
96 gdb_test_multiple "add-symbol-file -s -section-name 0x200 $binfile 0x100" $test {
97     -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\\) " {
98         gdb_test "n" "Not confirmed\." $test
99     }
100 }
101 # Since we're here, might as well test the 'symbol-file' command and
102 # if its arguments can also be passed at any position.
103 gdb_test "symbol-file -readnow $binfile" \
104     "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
105     "symbol-file with -readnow first"
106 clean_restart
107 gdb_test "symbol-file $binfile -readnow" \
108     "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
109     "symbol-file with -readnow second"
110 gdb_test "symbol-file -readnow" \
111     "no symbol file name was specified" \
112     "symbol-file without filename"
113 gdb_test "symbol-file -- -non-existent-file" \
114     "-non-existent-file: No such file or directory\." \
115     "symbol-file with -- disables option processing"
116 clean_restart
117 gdb_test "symbol-file -readnow -- $binfile" \
118     "Reading symbols from ${binfile}\.\.\.expanding to full symbols\.\.\.done\." \
119     "symbol-file with -- and -readnow"
120 gdb_test "symbol-file -- $binfile -readnow" \
121     "Unrecognized argument \"-readnow\"" \
122     "symbol-file with -- and -readnow, invalid option"
123
124 clean_restart
125
126 gdb_test "add-symbol-file ${binfile} 0 -s" \
127     "Missing section name after .-s." \
128     "add-symbol-file bare -s"
129 gdb_test "add-symbol-file ${binfile} 0 -s .whatever" \
130     "Missing section address after .-s." \
131     "add-symbol-file missing address"
132
133 # Load the object file.
134 gdb_test "add-symbol-file ${binfile} 0" \
135         "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
136         "add-symbol-file ${testfile}.o 0" \
137         "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x0\[\r\n\]+\\(y or n\\) " \
138         "y"
139
140 # Print the addresses of static variables.
141 set static_foo_addr [get_var_address static_foo]
142 set static_bar_addr [get_var_address static_bar]
143
144 # Make sure they have different addresses.
145 if { "${static_foo_addr}" == "${static_bar_addr}" } {
146   fail "static variables have different addresses"
147 } else {
148   pass "static variables have different addresses"
149 }
150
151 # Print the addresses of global variables.
152 set global_foo_addr [get_var_address global_foo]
153 set global_bar_addr [get_var_address global_bar]
154
155 # Make sure they have different addresses.
156 if { "${global_foo_addr}" == "${global_bar_addr}" } {
157   fail "global variables have different addresses"
158 } else {
159   pass "global variables have different addresses"
160 }
161
162 # Print the addresses of functions.
163 set function_foo_addr [get_var_address function_foo]
164 set function_bar_addr [get_var_address function_bar]
165
166 # Make sure they have different addresses.
167 if { "${function_foo_addr}" == "${function_bar_addr}" } {
168   fail "functions have different addresses"
169 } else {
170   pass "functions have different addresses"
171 }
172
173 # Now use a variable as an offset to add-symbol-file, and check that
174 # the functions' addresses change.
175
176 gdb_exit
177 gdb_start
178 gdb_reinitialize_dir $srcdir/$subdir
179
180 gdb_test_no_output "set \$offset = 0x10000"
181
182 # Load the object file.
183 gdb_test "add-symbol-file ${binfile} \$offset" \
184         "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
185         "add-symbol-file ${testfile}.o \$offset" \
186         "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x10000\[\r\n\]+\\(y or n\\) " \
187         "y"
188
189 # Print the addresses of functions.
190 set new_function_foo_addr [get_var_address function_foo]
191
192 # Make sure they have different addresses.
193 if { "${function_foo_addr}" == "${new_function_foo_addr}" } {
194   fail "function foo has a different address"
195 } else {
196   pass "function foo has a different address"
197 }
198
199 # Now try loading the object as an exec-file; we should be able to print
200 # the values of variables after we do this.
201
202 gdb_exit
203 gdb_start
204 gdb_reinitialize_dir $srcdir/$subdir
205 gdb_file_cmd ${binfile}
206
207 # Check the values of the variables.
208 gdb_test "print static_foo" "\\\$$decimal = 1"
209 gdb_test "print static_bar" "\\\$$decimal = 2"
210 gdb_test "print global_foo" "\\\$$decimal = 3"
211 gdb_test "print global_bar" "\\\$$decimal = 4"