]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/testsuite/lib/binutils-common.exp
8708030a23f2133ca1f2ca37140d52b6a2a0750a
[thirdparty/binutils-gdb.git] / binutils / testsuite / lib / binutils-common.exp
1 # Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 #
3 # This file is part of the GNU Binutils.
4 #
5 # This file is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19
20 # True if the object format is known to be ELF.
21 #
22 proc is_elf_format {} {
23 # config.sub for these targets curiously transforms a target doublet
24 # ending in -elf to -none. eg. m68hc12-elf to m68hc12-unknown-none
25 # They are always elf.
26 if { [istarget m68hc1*-*] || [istarget xgate-*] } {
27 return 1;
28 }
29
30 if { ![istarget *-*-eabi*]
31 && ![istarget *-*-elf*]
32 && ![istarget *-*-freebsd*]
33 && ![istarget *-*-gnu*]
34 && ![istarget *-*-irix5*]
35 && ![istarget *-*-irix6*]
36 && ![istarget *-*-linux*]
37 && ![istarget *-*-lynxos*]
38 && ![istarget *-*-nacl*]
39 && ![istarget *-*-netbsd*]
40 && ![istarget *-*-openbsd*]
41 && ![istarget *-*-rtems*]
42 && ![istarget *-*-solaris2*]
43 && ![istarget *-*-symbianelf*]
44 && ![istarget *-*-sysv4*]
45 && ![istarget *-*-unixware*]
46 && ![istarget *-*-wasm32*]
47 && ![istarget avr-*-*]
48 && ![istarget bfin-*-uclinux]
49 && ![istarget frv-*-uclinux*]
50 && ![istarget hppa*64*-*-hpux*]
51 && ![istarget ia64-*-hpux*]
52 && ![istarget sh*-*-uclinux*]
53 && ![istarget tic6x*-*-uclinux*] } {
54 return 0
55 }
56
57 if { [istarget *-*-linux*ecoff*]
58 || [istarget *-*-rtemscoff*] } {
59 return 0
60 }
61
62 if { ![istarget *-*-netbsdelf*]
63 && ( [istarget vax-*-netbsd*]
64 || [istarget ns32k-*-netbsd*]) } {
65 return 0
66 }
67
68 if { [istarget arm-*-openbsd*]
69 || [istarget ns32k-*-openbsd*]
70 || [istarget vax-*-openbsd*] } {
71 return 0
72 }
73
74 return 1
75 }
76
77 # True if the object format is known to be a.out.
78 #
79 proc is_aout_format {} {
80 if { [istarget *-*-*aout*]
81 || [istarget *-*-bsd*]
82 || [istarget *-*-msdos*]
83 || [istarget arm-*-netbsd*]
84 || [istarget arm-*-openbsd*]
85 || [istarget ns32k-*-*]
86 || [istarget pdp11-*-*]
87 || [istarget vax-*-netbsd] } {
88 return 1
89 }
90 return 0
91 }
92
93 # True if the object format is known to be PE COFF.
94 #
95 proc is_pecoff_format {} {
96 if { ![istarget *-*-mingw*]
97 && ![istarget *-*-cygwin*]
98 && ![istarget *-*-cegcc*]
99 && ![istarget *-*-pe*] } {
100 return 0
101 }
102
103 return 1
104 }
105
106 # True if the object format is known to be 64-bit ELF.
107 #
108 proc is_elf64 { binary_file } {
109 global READELF
110 global READELFFLAGS
111
112 set readelf_size ""
113 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
114
115 if ![string match "" $got] then {
116 return 0
117 }
118
119 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
120 [file_contents readelf.out] nil readelf_size] } {
121 return 0
122 }
123
124 if { $readelf_size == "64" } {
125 return 1
126 }
127
128 return 0
129 }
130
131 # True if the ELF target supports STB_GNU_UNIQUE with the ELF header's
132 # OSABI field set to ELFOSABI_GNU.
133 #
134 # This generally depends on the target OS only, however there are a
135 # number of exceptions for bare metal targets as follows. The MSP430
136 # and Visium targets set OSABI to ELFOSABI_STANDALONE and cannot
137 # support STB_GNU_UNIQUE. Likewise non-EABI ARM targets set OSABI to
138 # ELFOSABI_ARM, and TI C6X targets to ELFOSABI_C6000_*. Finally
139 # rather than `bfd_elf_final_link' AM33/2.0, D30V, DLX, and
140 # picoJava targets use `_bfd_generic_final_link', which does not
141 # support STB_GNU_UNIQUE symbol binding causing assertion failures.
142 #
143 proc supports_gnu_unique {} {
144 if { [istarget *-*-gnu*]
145 || [istarget *-*-linux*]
146 || [istarget *-*-nacl*] } {
147 return 1
148 }
149 if { [istarget "arm*-*-*eabi*"] } {
150 return 1
151 }
152 if { [istarget "wasm32*-*-*"] } {
153 return 1
154 }
155 if { ![istarget "*-*-elf*"] } {
156 return 0
157 }
158 if { [istarget "arm*-*-*"]
159 || [istarget "msp430-*-*"]
160 || [istarget "tic6x-*-*"]
161 || [istarget "visium-*-*"] } {
162 return 0
163 }
164 if { [istarget "am33_2.0-*-*"]
165 || [istarget "d30v-*-*"]
166 || [istarget "dlx-*-*"]
167 || [istarget "pj*-*-*"] } {
168 return 0
169 }
170 return 1
171 }
172
173 # True for targets that do not sort .symtab as per the ELF standard.
174 # ie. any that have mips_elf32_be_vec, mips_elf32_le_vec,
175 # mips_elf32_n_be_vec or mips_elf32_n_le_vec as the primary bfd target
176 # vector in config.bfd. When syncing with config.bfd, don't forget that
177 # earlier case-matches trump later ones.
178 proc is_bad_symtab {} {
179 if { ![istarget "mips*-*-*"] } {
180 return 0;
181 }
182 if { [istarget "*-*-chorus*"]
183 || [istarget "*-*-irix5*"]
184 || [istarget "*-*-irix6*"]
185 || [istarget "*-*-none"]
186 || [istarget "*-*-rtems*"]
187 || [istarget "*-*-windiss"] } {
188 return 1;
189 }
190 if { [istarget "*-*-elf*"]
191 && ![istarget "*-sde-*"]
192 && ![istarget "*-mti-*"]
193 && ![istarget "*-img-*"] } {
194 return 1;
195 }
196 if { [istarget "*-*-openbsd*"]
197 && ![istarget "mips64*-*-*"] } {
198 return 1;
199 }
200 return 0;
201 }
202
203 # Compare two files line-by-line. FILE_1 is the actual output and FILE_2
204 # is the expected output. Ignore blank lines in either file.
205 #
206 # FILE_2 is a series of regexps, comments and # directives. The directives
207 # are:
208 #
209 # #pass
210 # Treat the test as a PASS if everything up till this point has
211 # matched. Ignore any remaining lines in either FILE_1 or FILE_2.
212 #
213 # #failif
214 # Reverse the sense of the test: expect differences to exist.
215 #
216 # #...
217 # REGEXP
218 # Skip all lines in FILE_1 until the first that matches REGEXP.
219 #
220 # Other # lines are comments. Regexp lines starting with the `!' character
221 # specify inverse matching (use `\!' for literal matching against a leading
222 # `!'). Skip empty lines in both files.
223 #
224 # The first optional argument is a list of regexp substitutions of the form:
225 #
226 # EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
227 #
228 # This tells the function to apply each regexp substitution EXPi->SUBSPECi
229 # in order to every line of FILE_2.
230 #
231 # Return nonzero if differences exist.
232 proc regexp_diff { file_1 file_2 args } {
233 set eof -1
234 set end_1 0
235 set end_2 0
236 set differences 0
237 set diff_pass 0
238 set fail_if_match 0
239 set ref_subst ""
240 if { [llength $args] > 0 } {
241 set ref_subst [lindex $args 0]
242 }
243 if { [llength $args] > 1 } {
244 perror "Too many arguments to regexp_diff"
245 return 1
246 }
247
248 if [file exists $file_1] then {
249 set file_a [open $file_1 r]
250 } else {
251 perror "$file_1 doesn't exist"
252 return 1
253 }
254
255 if [file exists $file_2] then {
256 set file_b [open $file_2 r]
257 } else {
258 perror "$file_2 doesn't exist"
259 close $file_a
260 return 1
261 }
262
263 verbose " Regexp-diff'ing: $file_1 $file_2" 2
264
265 while { 1 } {
266 set line_a ""
267 set line_b ""
268 while { [string length $line_a] == 0 } {
269 # Ignore blank line in FILE_1.
270 if { [gets $file_a line_a] == $eof } {
271 set end_1 1
272 break
273 }
274 }
275 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
276 if { [string match "#pass" $line_b] } {
277 set end_2 1
278 set diff_pass 1
279 break
280 } elseif { [string match "#failif" $line_b] } {
281 send_log "fail if no difference\n"
282 verbose "fail if no difference" 3
283 set fail_if_match 1
284 } elseif { [string match "#..." $line_b] } {
285 if { [gets $file_b line_b] == $eof } {
286 set end_2 1
287 set diff_pass 1
288 break
289 }
290 set negated [expr { [string index $line_b 0] == "!" }]
291 set line_bx [string range $line_b $negated end]
292 set n [expr { $negated ? "! " : "" }]
293 # Substitute on the reference.
294 foreach {name value} $ref_subst {
295 regsub -- $name $line_bx $value line_bx
296 }
297 verbose "looking for $n\"^$line_bx$\"" 3
298 while { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
299 verbose "skipping \"$line_a\"" 3
300 if { [gets $file_a line_a] == $eof } {
301 set end_1 1
302 break
303 }
304 }
305 break
306 }
307 if { [gets $file_b line_b] == $eof } {
308 set end_2 1
309 break
310 }
311 }
312
313 if { $diff_pass } {
314 break
315 } elseif { $end_1 && $end_2 } {
316 break
317 } elseif { $end_1 } {
318 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
319 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
320 set differences 1
321 break
322 } elseif { $end_2 } {
323 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
324 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
325 set differences 1
326 break
327 } else {
328 set negated [expr { [string index $line_b 0] == "!" }]
329 set line_bx [string range $line_b $negated end]
330 set n [expr { $negated ? "! " : "" }]
331 set s [expr { $negated ? " " : "" }]
332 # Substitute on the reference.
333 foreach {name value} $ref_subst {
334 regsub -- $name $line_bx $value line_bx
335 }
336 verbose "regexp $n\"^$line_bx$\"\nline \"$line_a\"" 3
337 if { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
338 send_log "regexp_diff match failure\n"
339 send_log "regexp $n\"^$line_bx$\"\nline $s\"$line_a\"\n"
340 verbose "regexp_diff match failure\n" 3
341 set differences 1
342 }
343 }
344 }
345
346 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
347 send_log "$file_1 and $file_2 are different lengths\n"
348 verbose "$file_1 and $file_2 are different lengths" 3
349 set differences 1
350 }
351
352 if { $fail_if_match } {
353 if { $differences == 0 } {
354 set differences 1
355 } else {
356 set differences 0
357 }
358 }
359
360 close $file_a
361 close $file_b
362
363 return $differences
364 }