]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/testsuite/lib/binutils-common.exp
325a07cba436b578625f7eeb67d522eb53296189
[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*aout*]
58 || [istarget *-*-linux*ecoff*]
59 || [istarget *-*-linux*oldld*]
60 || [istarget *-*-rtemscoff*]
61 || [istarget h8500-*-rtems*]
62 || [istarget i?86-*-freebsd\[12\].*]
63 || [istarget i960-*-rtems*] } {
64 return 0
65 }
66
67 if { ![istarget *-*-netbsdelf*]
68 && ( [istarget *-*-netbsd*aout*]
69 || [istarget *-*-netbsdpe*]
70 || [istarget arm*-*-netbsd*]
71 || [istarget sparc-*-netbsd*]
72 || [istarget i*86-*-netbsd*]
73 || [istarget m68*-*-netbsd*]
74 || [istarget vax-*-netbsd*]
75 || [istarget ns32k-*-netbsd*]) } {
76 return 0
77 }
78
79 if { [istarget arm-*-openbsd*]
80 || [istarget i386-*-openbsd\[0-2\].*]
81 || [istarget i386-*-openbsd3.\[0-2\]]
82 || [istarget m68*-*-openbsd*]
83 || [istarget ns32k-*-openbsd*]
84 || [istarget sparc-*-openbsd\[0-2\].*]
85 || [istarget sparc-*-openbsd3.\[0-1\]]
86 || [istarget vax-*-openbsd*] } {
87 return 0
88 }
89
90 return 1
91 }
92
93 # True if the object format is known to be a.out.
94 #
95 proc is_aout_format {} {
96 if { [istarget *-*-netbsdelf]
97 || [istarget sparc64-*-netbsd*]
98 || [istarget sparc64-*-openbsd*] } {
99 return 0
100 }
101 if { [istarget *-*-*\[ab\]out*]
102 || [istarget *-*-linux*oldld*]
103 || [istarget *-*-bsd*]
104 || [istarget *-*-msdos*]
105 || [istarget arm-*-netbsd*]
106 || [istarget arm-*-openbsd*]
107 || [istarget arm-*-riscix*]
108 || [istarget i?86-*-freebsd\[12\].*]
109 || [istarget i?86-*-netbsd*]
110 || [istarget i?86-*-openbsd\[0-2\]*]
111 || [istarget i?86-*-openbsd3.\[0-2\]*]
112 || [istarget i?86-*-vsta]
113 || [istarget i?86-*-mach*]
114 || [istarget m68*-*-netbsd*]
115 || [istarget m68*-*-openbsd*]
116 || [istarget ns32k-*-*]
117 || [istarget pdp11-*-*]
118 || [istarget sparc*-*-sunos4*]
119 || [istarget sparc*-*-netbsd*]
120 || [istarget sparc*-*-openbsd\[0-2\]*]
121 || [istarget sparc*-*-openbsd3.\[0-1\]*]
122 || [istarget sparc*-fujitsu-none]
123 || [istarget vax-dec-ultrix*]
124 || [istarget vax-*-netbsd] } {
125 return 1
126 }
127 return 0
128 }
129
130 # True if the object format is known to be PE COFF.
131 #
132 proc is_pecoff_format {} {
133 if { ![istarget *-*-mingw*]
134 && ![istarget *-*-cygwin*]
135 && ![istarget *-*-cegcc*]
136 && ![istarget *-*-pe*] } {
137 return 0
138 }
139
140 return 1
141 }
142
143 # True if the object format is known to be 64-bit ELF.
144 #
145 proc is_elf64 { binary_file } {
146 global READELF
147 global READELFFLAGS
148
149 set readelf_size ""
150 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
151
152 if ![string match "" $got] then {
153 return 0
154 }
155
156 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
157 [file_contents readelf.out] nil readelf_size] } {
158 return 0
159 }
160
161 if { $readelf_size == "64" } {
162 return 1
163 }
164
165 return 0
166 }
167
168 # True if the ELF target supports STB_GNU_UNIQUE with the ELF header's
169 # OSABI field set to ELFOSABI_GNU.
170 #
171 # This generally depends on the target OS only, however there are a
172 # number of exceptions for bare metal targets as follows. The MSP430
173 # and Visium targets set OSABI to ELFOSABI_STANDALONE and cannot
174 # support STB_GNU_UNIQUE. Likewise non-EABI ARM targets set OSABI to
175 # ELFOSABI_ARM, and TI C6X targets to ELFOSABI_C6000_*. Finally
176 # rather than `bfd_elf_final_link' AM33/2.0, D30V, DLX, i960, and
177 # picoJava targets use `_bfd_generic_final_link', which does not
178 # support STB_GNU_UNIQUE symbol binding causing assertion failures.
179 #
180 proc supports_gnu_unique {} {
181 if { [istarget *-*-gnu*]
182 || [istarget *-*-linux*]
183 || [istarget *-*-nacl*] } {
184 return 1
185 }
186 if { [istarget "arm*-*-*eabi*"] } {
187 return 1
188 }
189 if { [istarget "wasm32*-*-*"] } {
190 return 1
191 }
192 if { ![istarget "*-*-elf*"] } {
193 return 0
194 }
195 if { [istarget "arm*-*-*"]
196 || [istarget "msp430-*-*"]
197 || [istarget "tic6x-*-*"]
198 || [istarget "visium-*-*"] } {
199 return 0
200 }
201 if { [istarget "am33_2.0-*-*"]
202 || [istarget "d30v-*-*"]
203 || [istarget "dlx-*-*"]
204 || [istarget "i960-*-*"]
205 || [istarget "pj*-*-*"] } {
206 return 0
207 }
208 return 1
209 }
210
211 # True for targets that do not sort .symtab as per the ELF standard.
212 # ie. any that have mips_elf32_be_vec, mips_elf32_le_vec,
213 # mips_elf32_n_be_vec or mips_elf32_n_le_vec as the primary bfd target
214 # vector in config.bfd. When syncing with config.bfd, don't forget that
215 # earlier case-matches trump later ones.
216 proc is_bad_symtab {} {
217 if { ![istarget "mips*-*-*"] } {
218 return 0;
219 }
220 if { [istarget "*-*-chorus*"]
221 || [istarget "*-*-irix5*"]
222 || [istarget "*-*-irix6*"]
223 || [istarget "*-*-none"]
224 || [istarget "*-*-rtems*"]
225 || [istarget "*-*-windiss"] } {
226 return 1;
227 }
228 if { [istarget "*-*-elf*"]
229 && ![istarget "*-sde-*"]
230 && ![istarget "*-mti-*"]
231 && ![istarget "*-img-*"] } {
232 return 1;
233 }
234 if { [istarget "*-*-openbsd*"]
235 && ![istarget "mips64*-*-*"] } {
236 return 1;
237 }
238 return 0;
239 }
240
241 # Compare two files line-by-line. FILE_1 is the actual output and FILE_2
242 # is the expected output. Ignore blank lines in either file.
243 #
244 # FILE_2 is a series of regexps, comments and # directives. The directives
245 # are:
246 #
247 # #pass
248 # Treat the test as a PASS if everything up till this point has
249 # matched. Ignore any remaining lines in either FILE_1 or FILE_2.
250 #
251 # #failif
252 # Reverse the sense of the test: expect differences to exist.
253 #
254 # #...
255 # REGEXP
256 # Skip all lines in FILE_1 until the first that matches REGEXP.
257 #
258 # Other # lines are comments. Regexp lines starting with the `!' character
259 # specify inverse matching (use `\!' for literal matching against a leading
260 # `!'). Skip empty lines in both files.
261 #
262 # The first optional argument is a list of regexp substitutions of the form:
263 #
264 # EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
265 #
266 # This tells the function to apply each regexp substitution EXPi->SUBSPECi
267 # in order to every line of FILE_2.
268 #
269 # Return nonzero if differences exist.
270 proc regexp_diff { file_1 file_2 args } {
271 set eof -1
272 set end_1 0
273 set end_2 0
274 set differences 0
275 set diff_pass 0
276 set fail_if_match 0
277 set ref_subst ""
278 if { [llength $args] > 0 } {
279 set ref_subst [lindex $args 0]
280 }
281 if { [llength $args] > 1 } {
282 perror "Too many arguments to regexp_diff"
283 return 1
284 }
285
286 if [file exists $file_1] then {
287 set file_a [open $file_1 r]
288 } else {
289 perror "$file_1 doesn't exist"
290 return 1
291 }
292
293 if [file exists $file_2] then {
294 set file_b [open $file_2 r]
295 } else {
296 perror "$file_2 doesn't exist"
297 close $file_a
298 return 1
299 }
300
301 verbose " Regexp-diff'ing: $file_1 $file_2" 2
302
303 while { 1 } {
304 set line_a ""
305 set line_b ""
306 while { [string length $line_a] == 0 } {
307 # Ignore blank line in FILE_1.
308 if { [gets $file_a line_a] == $eof } {
309 set end_1 1
310 break
311 }
312 }
313 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
314 if { [string match "#pass" $line_b] } {
315 set end_2 1
316 set diff_pass 1
317 break
318 } elseif { [string match "#failif" $line_b] } {
319 send_log "fail if no difference\n"
320 verbose "fail if no difference" 3
321 set fail_if_match 1
322 } elseif { [string match "#..." $line_b] } {
323 if { [gets $file_b line_b] == $eof } {
324 set end_2 1
325 set diff_pass 1
326 break
327 }
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 # Substitute on the reference.
332 foreach {name value} $ref_subst {
333 regsub -- $name $line_bx $value line_bx
334 }
335 verbose "looking for $n\"^$line_bx$\"" 3
336 while { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
337 verbose "skipping \"$line_a\"" 3
338 if { [gets $file_a line_a] == $eof } {
339 set end_1 1
340 break
341 }
342 }
343 break
344 }
345 if { [gets $file_b line_b] == $eof } {
346 set end_2 1
347 break
348 }
349 }
350
351 if { $diff_pass } {
352 break
353 } elseif { $end_1 && $end_2 } {
354 break
355 } elseif { $end_1 } {
356 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
357 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
358 set differences 1
359 break
360 } elseif { $end_2 } {
361 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
362 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
363 set differences 1
364 break
365 } else {
366 set negated [expr { [string index $line_b 0] == "!" }]
367 set line_bx [string range $line_b $negated end]
368 set n [expr { $negated ? "! " : "" }]
369 set s [expr { $negated ? " " : "" }]
370 # Substitute on the reference.
371 foreach {name value} $ref_subst {
372 regsub -- $name $line_bx $value line_bx
373 }
374 verbose "regexp $n\"^$line_bx$\"\nline \"$line_a\"" 3
375 if { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
376 send_log "regexp_diff match failure\n"
377 send_log "regexp $n\"^$line_bx$\"\nline $s\"$line_a\"\n"
378 verbose "regexp_diff match failure\n" 3
379 set differences 1
380 }
381 }
382 }
383
384 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
385 send_log "$file_1 and $file_2 are different lengths\n"
386 verbose "$file_1 and $file_2 are different lengths" 3
387 set differences 1
388 }
389
390 if { $fail_if_match } {
391 if { $differences == 0 } {
392 set differences 1
393 } else {
394 set differences 0
395 }
396 }
397
398 close $file_a
399 close $file_b
400
401 return $differences
402 }