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