bfd:
[external/binutils.git] / binutils / testsuite / binutils-all / readelf.exp
1 #   Copyright 1999, 2000, 2001, 2003, 2004, 2007 Free Software Foundation, Inc.
2
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 3 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.
12
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
19
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>.
23
24 # First some helpful procedures, then the tests themselves
25
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
32 }
33
34 # Find out the size by reading the output of the EI_CLASS field.
35 # Similar to the test for readelf -h, but we're just looking for the
36 # EI_CLASS line here.
37 proc readelf_find_size { binary_file } {
38     global READELF
39     global READELFFLAGS
40     global readelf_size
41
42     set readelf_size ""
43     set testname "finding out ELF size with readelf -h"
44     set got [remote_exec host "$READELF $READELFFLAGS -h $binary_file" "" "/dev/null" "readelf.out"]
45     if [is_remote host] then {
46         remote_upload host "readelf.out"
47     }
48
49     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
50         send_log $got
51         fail $testname
52         return
53     }
54
55     if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
56             [file_contents readelf.out] nil readelf_size] } {
57         verbose -log "EI_CLASS field not found in output"
58         verbose -log "output is \n[file_contents readelf.out]"
59         fail $testname
60         return
61     } else {
62         verbose -log "ELF size is $readelf_size"
63     }
64
65     pass $testname
66 }
67
68 # Run an individual readelf test.
69 # Basically readelf is run on the binary_file with the given options.
70 # Readelf's output is captured and then compared against the contents
71 # of the regexp_file-readelf_size if it exists, else regexp_file.
72
73 proc readelf_test { options binary_file regexp_file xfails } {
74
75     global READELF
76     global READELFFLAGS
77     global readelf_size
78     global srcdir
79     global subdir
80     
81     send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
82     set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
83
84     foreach xfail $xfails {
85         setup_xfail $xfail
86     }
87
88     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
89         fail "readelf $options (reason: unexpected output)"
90         send_log $got
91         send_log "\n"
92         return
93     }
94
95     set target_machine ""
96     if [istarget "mips*-*-*"] then {
97         if { [istarget "mips*-*-*linux*"] || [istarget "mips*-sde-elf*"] } then {
98             set target_machine tmips
99         } else {
100             set target_machine mips
101         }
102     }
103
104     if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
105         set regexp_file $regexp_file-$readelf_size-$target_machine
106     } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
107         set regexp_file $regexp_file-$target_machine
108     } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
109         set regexp_file $regexp_file-$readelf_size
110     }
111
112     if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
113         fail "readelf $options"
114         verbose "output is \n[file_contents readelf.out]" 2
115         return
116     }
117
118     pass "readelf $options"
119 }
120
121 # Simple proc to skip certain expected warning messages.
122
123 proc prune_readelf_wi_warnings { text } {
124     regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
125     return $text
126 }
127
128 # Testing the "readelf -wi" option is difficult because there
129 # is no guaranteed order to the output, and because some ports
130 # will use indirect string references, whilst others will use
131 # direct references.  So instead of having an expected output
132 # file, like the other readelf tests, we grep for strings that
133 # really ought to be there.
134
135 proc readelf_wi_test {} {
136     global READELF
137     global READELFFLAGS
138     global srcdir
139     global subdir
140     
141     # Compile the second test file.
142     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
143         verbose "Unable to compile test file."
144         untested "readelf -wi"
145         return
146     }
147
148     # Download it.
149     set tempfile [remote_download host tmpdir/testprog.o]
150
151     # Run "readelf -wi" on it.
152     set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
153
154     # Upload the results.
155     set output [remote_upload host readelf.out]
156
157     file_on_host delete $tempfile
158     
159     # Strip any superflous warnings.
160     set got [prune_readelf_wi_warnings [lindex $got 1]]
161
162     if ![string match "" $got] then {
163         fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
164         send_log $got
165         send_log "\n"
166         return
167     }
168
169     if ![file size $output] then {
170         # If the output file is empty, then this target does not
171         # generate dwarf2 output.  This is not a failure.
172         verbose "No output from 'readelf -wi'"
173         untested "readelf -wi"
174         return
175     }
176     
177     # Search for strings that should be in the output.
178     set sought {
179         ".*DW_TAG_compile_unit.*"
180         ".*DW_TAG_subprogram.*"
181         ".*DW_TAG_base_type.*"
182         ".*DW_AT_producer.*(GNU C|indirect string).*"
183         ".*DW_AT_language.*ANSI C.*"
184         ".*DW_AT_name.*(testprog.c|indirect string).*"
185         ".*DW_AT_name.*fn.*"
186         ".*DW_AT_name.*(main|indirect string).*"
187         ".*\(DW_OP_addr: 0\).*"
188     }
189     
190     foreach looked_for $sought {        
191         set lines [grep $output $looked_for]
192         if ![llength $lines] then {
193             fail "readelf -wi: missing: $looked_for"
194             send_log readelf.out
195             return
196         }
197     }
198
199     file_on_host delete $output
200     
201     # All done.
202     pass "readelf -wi"
203 }
204
205
206 # Exclude non-ELF targets.
207 if ![is_elf_format] {
208     verbose "$READELF is only intended for ELF targets" 2
209     return
210 }
211
212 if ![is_remote host] {
213     if {[which $READELF] == 0} then {
214         perror "$READELF does not exist"
215         return
216     }
217 }
218
219 send_user "Version [binutil_version $READELF]"
220
221 # Assemble the test file.
222 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
223     perror "unresolved 1"
224     unresolved "readelf - failed to assemble"
225     return
226 }
227
228 if ![is_remote host] {
229     set tempfile tmpdir/bintest.o
230 } else {
231     set tempfile [remote_download host tmpdir/bintest.o]
232 }
233
234 # First, determine the size, so specific output matchers can be used.
235 readelf_find_size $tempfile
236
237 # Run the tests.
238 readelf_test -h $tempfile readelf.h  {}
239 readelf_test -S $tempfile readelf.s  {}
240 readelf_test -s $tempfile readelf.ss {}
241 readelf_test -r $tempfile readelf.r  {}
242
243 readelf_wi_test