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