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