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