6eef15fb20e3aa2c43dc6b3e5662053dc2ae9508
[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 gdb_test "add-symbol-file ${binfile} 0 -s" \
41     "Missing section name after .-s." \
42     "add-symbol-file bare -s"
43 gdb_test "add-symbol-file ${binfile} 0 -s .whatever" \
44     "Missing section address after .-s." \
45     "add-symbol-file missing address"
46
47 # Load the object file.
48 gdb_test "add-symbol-file ${binfile} 0" \
49         "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
50         "add-symbol-file ${testfile}.o 0" \
51         "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x0\[\r\n\]+\\(y or n\\) " \
52         "y"
53
54 # Print the addresses of static variables.
55 set static_foo_addr [get_var_address static_foo]
56 set static_bar_addr [get_var_address static_bar]
57
58 # Make sure they have different addresses.
59 if { "${static_foo_addr}" == "${static_bar_addr}" } {
60   fail "static variables have different addresses"
61 } else {
62   pass "static variables have different addresses"
63 }
64
65 # Print the addresses of global variables.
66 set global_foo_addr [get_var_address global_foo]
67 set global_bar_addr [get_var_address global_bar]
68
69 # Make sure they have different addresses.
70 if { "${global_foo_addr}" == "${global_bar_addr}" } {
71   fail "global variables have different addresses"
72 } else {
73   pass "global variables have different addresses"
74 }
75
76 # Print the addresses of functions.
77 set function_foo_addr [get_var_address function_foo]
78 set function_bar_addr [get_var_address function_bar]
79
80 # Make sure they have different addresses.
81 if { "${function_foo_addr}" == "${function_bar_addr}" } {
82   fail "functions have different addresses"
83 } else {
84   pass "functions have different addresses"
85 }
86
87 # Now use a variable as an offset to add-symbol-file, and check that
88 # the functions' addresses change.
89
90 gdb_exit
91 gdb_start
92 gdb_reinitialize_dir $srcdir/$subdir
93
94 gdb_test_no_output "set \$offset = 0x10000"
95
96 # Load the object file.
97 gdb_test "add-symbol-file ${binfile} \$offset" \
98         "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
99         "add-symbol-file ${testfile}.o \$offset" \
100         "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x10000\[\r\n\]+\\(y or n\\) " \
101         "y"
102
103 # Print the addresses of functions.
104 set new_function_foo_addr [get_var_address function_foo]
105
106 # Make sure they have different addresses.
107 if { "${function_foo_addr}" == "${new_function_foo_addr}" } {
108   fail "function foo has a different address"
109 } else {
110   pass "function foo has a different address"
111 }
112
113 # Now try loading the object as an exec-file; we should be able to print
114 # the values of variables after we do this.
115
116 gdb_exit
117 gdb_start
118 gdb_reinitialize_dir $srcdir/$subdir
119 gdb_file_cmd ${binfile}
120
121 # Check the values of the variables.
122 gdb_test "print static_foo" "\\\$$decimal = 1"
123 gdb_test "print static_bar" "\\\$$decimal = 2"
124 gdb_test "print global_foo" "\\\$$decimal = 3"
125 gdb_test "print global_bar" "\\\$$decimal = 4"