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