binutils/testsuite/
[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.  Skip empty lines in both files.
171 #
172 # The first optional argument is a list of regexp substitutions of the form:
173 #
174 #    EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
175 #
176 # This tells the function to apply each regexp substitution EXPi->SUBSPECi
177 # in order to every line of FILE_2.
178 #
179 # Return nonzero if differences exist.
180 proc regexp_diff { file_1 file_2 args } {
181     set eof -1
182     set end_1 0
183     set end_2 0
184     set differences 0
185     set diff_pass 0
186     set fail_if_match 0
187     set ref_subst ""
188     if { [llength $args] > 0 } {
189         set ref_subst [lindex $args 0]
190     }
191     if { [llength $args] > 1 } {
192         perror "Too many arguments to regexp_diff"
193         return 1
194     }
195
196     if [file exists $file_1] then {
197         set file_a [open $file_1 r]
198     } else {
199         perror "$file_1 doesn't exist"
200         return 1
201     }
202
203     if [file exists $file_2] then {
204         set file_b [open $file_2 r]
205     } else {
206         perror "$file_2 doesn't exist"
207         close $file_a
208         return 1
209     }
210
211     verbose " Regexp-diff'ing: $file_1 $file_2" 2
212
213     while { 1 } {
214         set line_a ""
215         set line_b ""
216         while { [string length $line_a] == 0 } {
217             # Ignore blank line in FILE_1.
218             if { [gets $file_a line_a] == $eof } {
219                 set end_1 1
220                 break
221             }
222         }
223         while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
224             if { [string match "#pass" $line_b] } {
225                 set end_2 1
226                 set diff_pass 1
227                 break
228             } elseif { [string match "#failif" $line_b] } {
229                 send_log "fail if no difference\n"
230                 verbose "fail if no difference" 3
231                 set fail_if_match 1
232             } elseif { [string match "#..." $line_b] } {
233                 if { [gets $file_b line_b] == $eof } {
234                     set end_2 1
235                     set diff_pass 1
236                     break
237                 }
238                 # Substitute on the reference.
239                 foreach {name value} $ref_subst {
240                     regsub -- $name $line_b $value line_b
241                 }
242                 verbose "looking for \"^$line_b$\"" 3
243                 while { ![regexp "^$line_b$" "$line_a"] } {
244                     verbose "skipping    \"$line_a\"" 3
245                     if { [gets $file_a line_a] == $eof } {
246                         set end_1 1
247                         break
248                     }
249                 }
250                 break
251             }
252             if { [gets $file_b line_b] == $eof } {
253                 set end_2 1
254                 break
255             }
256         }
257
258         if { $diff_pass } {
259             break
260         } elseif { $end_1 && $end_2 } {
261             break
262         } elseif { $end_1 } {
263             send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
264             verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
265             set differences 1
266             break
267         } elseif { $end_2 } {
268             send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
269             verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
270             set differences 1
271             break
272         } else {
273             # Substitute on the reference.
274             foreach {name value} $ref_subst {
275                 regsub -- $name $line_b $value line_b
276             }
277             verbose "regexp \"^$line_b$\"\nline   \"$line_a\"" 3
278             if { ![regexp "^$line_b$" "$line_a"] } {
279                 send_log "regexp_diff match failure\n"
280                 send_log "regexp \"^$line_b$\"\nline   \"$line_a\"\n"
281                 verbose "regexp_diff match failure\n" 3
282                 set differences 1
283             }
284         }
285     }
286
287     if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
288         send_log "$file_1 and $file_2 are different lengths\n"
289         verbose "$file_1 and $file_2 are different lengths" 3
290         set differences 1
291     }
292
293     if { $fail_if_match } {
294         if { $differences == 0 } {
295             set differences 1
296         } else {
297             set differences 0
298         }
299     }
300
301     close $file_a
302     close $file_b
303
304     return $differences
305 }