1 # Copyright (C) 1993-2016 Free Software Foundation, Inc.
3 # This file is part of the GNU Binutils.
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.
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.
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,
20 # True if the object format is known to be ELF.
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*] } {
46 if { [istarget i?86-*-freebsd\[12\].*] } {
50 if { [istarget *-*-linux*aout*]
51 || [istarget *-*-linux*ecoff*]
52 || [istarget *-*-linux*oldld*]
53 || [istarget h8500-*-rtems*]
54 || [istarget i960-*-rtems*]
55 || [istarget *-*-rtemscoff*] } {
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*]) } {
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*] } {
85 # True if the object format is known to be a.out.
87 proc is_aout_format {} {
88 if { [istarget *-*-netbsdelf]
89 || [istarget sparc64-*-netbsd*]
90 || [istarget sparc64-*-openbsd*] } {
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] } {
122 # True if the object format is known to be PE COFF.
124 proc is_pecoff_format {} {
125 if { ![istarget *-*-mingw*]
126 && ![istarget *-*-cygwin*]
127 && ![istarget *-*-cegcc*]
128 && ![istarget *-*-pe*] } {
135 # True if the object format is known to be 64-bit ELF.
137 proc is_elf64 { binary_file } {
142 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
144 if ![string match "" $got] then {
148 if { ![regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
149 [file_contents readelf.out] nil readelf_size] } {
153 if { $readelf_size == "64" } {
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.
163 # FILE_2 is a series of regexps, comments and # directives. The directives
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.
171 # Reverse the sense of the test: expect differences to exist.
175 # Skip all lines in FILE_1 until the first that matches REGEXP.
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.
181 # The first optional argument is a list of regexp substitutions of the form:
183 # EXP1 SUBSPEC1 EXP2 SUBSPEC2 ...
185 # This tells the function to apply each regexp substitution EXPi->SUBSPECi
186 # in order to every line of FILE_2.
188 # Return nonzero if differences exist.
189 proc regexp_diff { file_1 file_2 args } {
197 if { [llength $args] > 0 } {
198 set ref_subst [lindex $args 0]
200 if { [llength $args] > 1 } {
201 perror "Too many arguments to regexp_diff"
205 if [file exists $file_1] then {
206 set file_a [open $file_1 r]
208 perror "$file_1 doesn't exist"
212 if [file exists $file_2] then {
213 set file_b [open $file_2 r]
215 perror "$file_2 doesn't exist"
220 verbose " Regexp-diff'ing: $file_1 $file_2" 2
225 while { [string length $line_a] == 0 } {
226 # Ignore blank line in FILE_1.
227 if { [gets $file_a line_a] == $eof } {
232 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
233 if { [string match "#pass" $line_b] } {
237 } elseif { [string match "#failif" $line_b] } {
238 send_log "fail if no difference\n"
239 verbose "fail if no difference" 3
241 } elseif { [string match "#..." $line_b] } {
242 if { [gets $file_b line_b] == $eof } {
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
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 } {
264 if { [gets $file_b line_b] == $eof } {
272 } elseif { $end_1 && $end_2 } {
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
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
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
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
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
309 if { $fail_if_match } {
310 if { $differences == 0 } {