Automatic date update in version.in
[deliverable/binutils-gdb.git] / binutils / testsuite / lib / binutils-common.exp
CommitLineData
6f2750fe 1# Copyright (C) 1993-2016 Free Software Foundation, Inc.
f3097f33
RS
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#
22proc is_elf_format {} {
b3066ae8
AM
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
f3097f33
RS
30 if { ![istarget *-*-sysv4*]
31 && ![istarget *-*-unixware*]
32 && ![istarget *-*-elf*]
33 && ![istarget *-*-eabi*]
34 && ![istarget *-*-rtems*]
35 && ![istarget hppa*64*-*-hpux*]
36 && ![istarget ia64-*-hpux*]
37 && ![istarget *-*-linux*]
38 && ![istarget *-*-gnu*]
5a68afcf 39 && ![istarget *-*-nacl*]
f3097f33
RS
40 && ![istarget frv-*-uclinux*]
41 && ![istarget bfin-*-uclinux]
42 && ![istarget sh*-*-uclinux*]
ac145307 43 && ![istarget tic6x*-*-uclinux*]
f3097f33
RS
44 && ![istarget *-*-irix5*]
45 && ![istarget *-*-irix6*]
7e16ef34 46 && ![istarget *-*-freebsd*]
f3097f33
RS
47 && ![istarget *-*-netbsd*]
48 && ![istarget *-*-openbsd*]
49 && ![istarget *-*-solaris2*] } {
50 return 0
51 }
52
7e16ef34
EM
53 if { [istarget i?86-*-freebsd\[12\].*] } {
54 return 0
55 }
56
f3097f33 57 if { [istarget *-*-linux*aout*]
df26367c 58 || [istarget *-*-linux*ecoff*]
f3097f33
RS
59 || [istarget *-*-linux*oldld*]
60 || [istarget h8500-*-rtems*]
61 || [istarget i960-*-rtems*]
62 || [istarget *-*-rtemscoff*] } {
63 return 0
64 }
65
66 if { ![istarget *-*-netbsdelf*]
67 && ([istarget *-*-netbsd*aout*]
68 || [istarget *-*-netbsdpe*]
69 || [istarget arm*-*-netbsd*]
70 || [istarget sparc-*-netbsd*]
71 || [istarget i*86-*-netbsd*]
72 || [istarget m68*-*-netbsd*]
73 || [istarget vax-*-netbsd*]
74 || [istarget ns32k-*-netbsd*]) } {
75 return 0
76 }
77
78 if { [istarget arm-*-openbsd*]
79 || [istarget i386-*-openbsd\[0-2\].*]
80 || [istarget i386-*-openbsd3.\[0-2\]]
81 || [istarget m68*-*-openbsd*]
82 || [istarget ns32k-*-openbsd*]
83 || [istarget sparc-*-openbsd\[0-2\].*]
84 || [istarget sparc-*-openbsd3.\[0-1\]]
85 || [istarget vax-*-openbsd*] } {
86 return 0
87 }
88
89 return 1
90}
91
92# True if the object format is known to be a.out.
93#
94proc is_aout_format {} {
95 if { [istarget *-*-netbsdelf]
96 || [istarget sparc64-*-netbsd*]
97 || [istarget sparc64-*-openbsd*] } {
98 return 0
99 }
100 if { [istarget *-*-*\[ab\]out*]
101 || [istarget *-*-linux*oldld*]
102 || [istarget *-*-bsd*]
103 || [istarget *-*-msdos*]
104 || [istarget arm-*-netbsd*]
105 || [istarget arm-*-openbsd*]
106 || [istarget arm-*-riscix*]
7e16ef34 107 || [istarget i?86-*-freebsd\[12\].*]
f3097f33
RS
108 || [istarget i?86-*-netbsd*]
109 || [istarget i?86-*-openbsd\[0-2\]*]
110 || [istarget i?86-*-openbsd3.\[0-2\]*]
111 || [istarget i?86-*-vsta]
112 || [istarget i?86-*-mach*]
113 || [istarget m68*-*-netbsd*]
114 || [istarget m68*-*-openbsd*]
115 || [istarget ns32k-*-*]
116 || [istarget pdp11-*-*]
117 || [istarget sparc*-*-sunos4*]
118 || [istarget sparc*-*-netbsd*]
119 || [istarget sparc*-*-openbsd\[0-2\]*]
120 || [istarget sparc*-*-openbsd3.\[0-1\]*]
121 || [istarget sparc*-fujitsu-none]
122 || [istarget vax-dec-ultrix*]
123 || [istarget vax-*-netbsd] } {
124 return 1
125 }
126 return 0
127}
128
129# True if the object format is known to be PE COFF.
130#
131proc is_pecoff_format {} {
132 if { ![istarget *-*-mingw*]
133 && ![istarget *-*-cygwin*]
134 && ![istarget *-*-cegcc*]
135 && ![istarget *-*-pe*] } {
136 return 0
137 }
138
139 return 1
140}
141
142# True if the object format is known to be 64-bit ELF.
143#
144proc is_elf64 { binary_file } {
145 global READELF
146 global READELFFLAGS
147
148 set readelf_size ""
149 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
150
151 if ![string match "" $got] then {
152 return 0
153 }
154
155 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
156 [file_contents readelf.out] nil readelf_size] } {
157 return 0
158 }
159
160 if { $readelf_size == "64" } {
161 return 1
162 }
163
164 return 0
165}
eb22018c
RS
166
167# Compare two files line-by-line. FILE_1 is the actual output and FILE_2
168# is the expected output. Ignore blank lines in either file.
169#
170# FILE_2 is a series of regexps, comments and # directives. The directives
171# are:
172#
173# #pass
174# Treat the test as a PASS if everything up till this point has
175# matched. Ignore any remaining lines in either FILE_1 or FILE_2.
176#
177# #failif
178# Reverse the sense of the test: expect differences to exist.
179#
180# #...
181# REGEXP
182# Skip all lines in FILE_1 until the first that matches REGEXP.
183#
738f4d98
MR
184# Other # lines are comments. Regexp lines starting with the `!' character
185# specify inverse matching (use `\!' for literal matching against a leading
186# `!'). Skip empty lines in both files.
eb22018c
RS
187#
188# The first optional argument is a list of regexp substitutions of the form:
189#
190# EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
191#
192# This tells the function to apply each regexp substitution EXPi->SUBSPECi
193# in order to every line of FILE_2.
194#
195# Return nonzero if differences exist.
196proc regexp_diff { file_1 file_2 args } {
197 set eof -1
198 set end_1 0
199 set end_2 0
200 set differences 0
201 set diff_pass 0
202 set fail_if_match 0
203 set ref_subst ""
204 if { [llength $args] > 0 } {
205 set ref_subst [lindex $args 0]
206 }
207 if { [llength $args] > 1 } {
208 perror "Too many arguments to regexp_diff"
209 return 1
210 }
211
212 if [file exists $file_1] then {
213 set file_a [open $file_1 r]
214 } else {
215 perror "$file_1 doesn't exist"
216 return 1
217 }
218
219 if [file exists $file_2] then {
220 set file_b [open $file_2 r]
221 } else {
222 perror "$file_2 doesn't exist"
223 close $file_a
224 return 1
225 }
226
227 verbose " Regexp-diff'ing: $file_1 $file_2" 2
228
229 while { 1 } {
230 set line_a ""
231 set line_b ""
232 while { [string length $line_a] == 0 } {
233 # Ignore blank line in FILE_1.
234 if { [gets $file_a line_a] == $eof } {
235 set end_1 1
236 break
237 }
238 }
239 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
240 if { [string match "#pass" $line_b] } {
241 set end_2 1
242 set diff_pass 1
243 break
244 } elseif { [string match "#failif" $line_b] } {
245 send_log "fail if no difference\n"
246 verbose "fail if no difference" 3
247 set fail_if_match 1
248 } elseif { [string match "#..." $line_b] } {
249 if { [gets $file_b line_b] == $eof } {
250 set end_2 1
251 set diff_pass 1
252 break
253 }
47a50e5b 254 set negated [expr { [string index $line_b 0] == "!" }]
738f4d98
MR
255 set line_bx [string range $line_b $negated end]
256 set n [expr { $negated ? "! " : "" }]
eb22018c
RS
257 # Substitute on the reference.
258 foreach {name value} $ref_subst {
738f4d98 259 regsub -- $name $line_bx $value line_bx
eb22018c 260 }
738f4d98
MR
261 verbose "looking for $n\"^$line_bx$\"" 3
262 while { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
eb22018c
RS
263 verbose "skipping \"$line_a\"" 3
264 if { [gets $file_a line_a] == $eof } {
265 set end_1 1
266 break
267 }
268 }
269 break
270 }
271 if { [gets $file_b line_b] == $eof } {
272 set end_2 1
273 break
274 }
275 }
276
277 if { $diff_pass } {
278 break
279 } elseif { $end_1 && $end_2 } {
280 break
281 } elseif { $end_1 } {
282 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
283 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
284 set differences 1
285 break
286 } elseif { $end_2 } {
287 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
288 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
289 set differences 1
290 break
291 } else {
47a50e5b 292 set negated [expr { [string index $line_b 0] == "!" }]
738f4d98
MR
293 set line_bx [string range $line_b $negated end]
294 set n [expr { $negated ? "! " : "" }]
295 set s [expr { $negated ? " " : "" }]
eb22018c
RS
296 # Substitute on the reference.
297 foreach {name value} $ref_subst {
738f4d98 298 regsub -- $name $line_bx $value line_bx
eb22018c 299 }
738f4d98
MR
300 verbose "regexp $n\"^$line_bx$\"\nline \"$line_a\"" 3
301 if { [expr [regexp "^$line_bx$" "$line_a"] == $negated] } {
eb22018c 302 send_log "regexp_diff match failure\n"
738f4d98 303 send_log "regexp $n\"^$line_bx$\"\nline $s\"$line_a\"\n"
eb22018c
RS
304 verbose "regexp_diff match failure\n" 3
305 set differences 1
306 }
307 }
308 }
309
310 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
311 send_log "$file_1 and $file_2 are different lengths\n"
312 verbose "$file_1 and $file_2 are different lengths" 3
313 set differences 1
314 }
315
316 if { $fail_if_match } {
317 if { $differences == 0 } {
318 set differences 1
319 } else {
320 set differences 0
321 }
322 }
323
324 close $file_a
325 close $file_b
326
327 return $differences
328}
This page took 0.232368 seconds and 4 git commands to generate.