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