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