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