2000-10-31 Philip Blundell <philb@gnu.org>
[binutils.git] / binutils / testsuite / binutils-all / readelf.exp
blob246139180cf7103c03e2670c9c4bf53fb5a29d85
1 # Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
20 # Written by Nick Clifton <nickc@cygnus.com>
21 # Based on scripts written by Ian Lance Taylor <ian@cygnus.com>
22 # and Ken Raeburn <raeburn@cygnus.com>.
24 # First some helpful procedures, then the tests themselves
26 # Return the contents of the filename given
27 proc file_contents { filename } {
28 set file [open $filename r]
29 set contents [read $file]
30 close $file
31 return $contents
34 # regexp_diff, based on simple_diff taken from ld test suite
35 # compares two files line-by-line
36 # file1 contains strings, file2 contains regexps and #-comments
37 # blank lines are ignored in either file
38 # returns non-zero if differences exist
40 proc regexp_diff { file_1 file_2 } {
42 set eof -1
43 set end_1 0
44 set end_2 0
45 set differences 0
46 set diff_pass 0
48 if [file exists $file_1] then {
49 set file_a [open $file_1 r]
50 } else {
51 warning "$file_1 doesn't exist"
52 return 1
55 if [file exists $file_2] then {
56 set file_b [open $file_2 r]
57 } else {
58 fail "$file_2 doesn't exist"
59 close $file_a
60 return 1
63 verbose " Regexp-diff'ing: $file_1 $file_2" 2
65 while { 1 } {
66 set line_a ""
67 set line_b ""
68 while { [string length $line_a] == 0 } {
69 if { [gets $file_a line_a] == $eof } {
70 set end_1 1
71 break
74 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
75 if [ string match "#pass" $line_b ] {
76 set end_2 1
77 set diff_pass 1
78 break
80 if { [gets $file_b line_b] == $eof } {
81 set end_2 1
82 break
86 if { $diff_pass } {
87 break
88 } elseif { $end_1 && $end_2 } {
89 break
90 } elseif { $end_1 } {
91 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
92 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
93 set differences 1
94 break
95 } elseif { $end_2 } {
96 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
97 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
98 set differences 1
99 break
100 } else {
101 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
102 if ![regexp "^$line_b$" "$line_a"] {
103 send_log "regexp_diff match failure\n"
104 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
105 set differences 1
110 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
111 send_log "$file_1 and $file_2 are different lengths\n"
112 verbose "$file_1 and $file_2 are different lengths" 3
113 set differences 1
116 close $file_a
117 close $file_b
119 return $differences
122 # Find out the size by reading the output of the EI_CLASS field.
123 # Similar to the test for readelf -h, but we're just looking for the
124 # EI_CLASS line here.
125 proc readelf_find_size { binary_file } {
126 global READELF
127 global READELFFLAGS
128 global readelf_size
130 set readelf_size ""
131 set testname "finding out ELF size with readelf -h"
132 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
134 if ![string match "" $got] then {
135 send_log $got
136 fail $testname
137 return
140 if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
141 [file_contents readelf.out] nil readelf_size] } {
142 verbose -log "EI_CLASS field not found in output"
143 verbose -log "output is \n[file_contents readelf.out]"
144 fail $testname
145 return
146 } else {
147 verbose -log "ELF size is $readelf_size"
150 pass $testname
153 # Run an individual readelf test.
154 # Basically readelf is run on the binary_file with the given options.
155 # Readelf's output is captured and then compared against the contents
156 # of the regexp_file-readelf_size if it exists, else regexp_file.
158 proc readelf_test { options binary_file regexp_file xfails } {
160 global READELF
161 global READELFFLAGS
162 global readelf_size
163 global srcdir
164 global subdir
166 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
167 catch "exec $READELF $READELFFLAGS $options $binary_file > readelf.out" got
169 foreach xfail $xfails {
170 setup_xfail $xfail
173 if ![string match "" $got] then {
174 send_log $got
175 fail "readelf $options"
176 return
179 if { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
180 set regexp_file $regexp_file-$readelf_size
183 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
184 fail "readelf $options"
185 verbose "output is \n[file_contents readelf.out]" 2
186 return
189 pass "readelf $options"
194 # Only ELF based toolchains need readelf.
195 # For now be paranoid and assume that if ELF is not mentioned
196 # in the target string, then the target is not an ELF based port.
198 if ![istarget "*-*elf"] then {
199 verbose "$READELF is only intended for ELF targets" 2
200 return
203 if ![is_remote host] {
204 if {[which $READELF] == 0} then {
205 perror "$READELF does not exist"
206 return
210 send_user "Version [binutil_version $READELF]"
212 # Assemle the test file.
213 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
214 perror "unresolved 1"
215 unresolved "readelf - failed to assemble"
216 return
219 if ![is_remote host] {
220 set tempfile tmpdir/bintest.o;
221 } else {
222 set tempfile [remote_download host tmpdir/bintest.o]
225 # First, determine the size, so specific output matchers can be used.
226 readelf_find_size $tempfile
228 # Run the tests.
229 readelf_test -h $tempfile readelf.h {}
231 # The v850 fails the next two tests because it creates two special
232 # sections of its own: .call_table_data and .call_table_text
233 # The regexp scripts are not expecting these sections...
234 # MIPS targets fail because they add .rela.txt (empty), .mdebug,
235 # and .reginfo sections.
237 readelf_test -S $tempfile readelf.s {v850*-*-* mips*-*-*}
238 readelf_test -s $tempfile readelf.ss {v850*-*-* mips*-*-*}
239 readelf_test -r $tempfile readelf.r {}
242 # Compile the second test file.
243 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
244 untested "readelf -w"
245 return
248 if [is_remote host] {
249 set tempfile [remote_download host tmpdir/testprog.o];
250 } else {
251 set tempfile tmpdir/testprog.o
254 # The xfail targets here do not default to DWARF2 format debug information
255 # The symptom is that the output of 'readelf -wi' is empty.
257 readelf_test -wi $tempfile readelf.wi {v850*-*-* cris-*-*}