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