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