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