binutils/testsuite/
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
1 # Copyright 1999-2013 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 # Exclude non-ELF targets.
25 if ![is_elf_format] {
26 verbose "$READELF is only intended for ELF targets" 2
27 return
28 }
29
30 # First some helpful procedures, then the tests themselves
31
32 # Return the contents of the filename given
33 proc file_contents { filename } {
34 set file [open $filename r]
35 set contents [read $file]
36 close $file
37 return $contents
38 }
39
40 # Find out the size by reading the output of the EI_CLASS field.
41 # Similar to the test for readelf -h, but we're just looking for the
42 # EI_CLASS line here.
43 proc readelf_find_size { binary_file } {
44 global READELF
45 global READELFFLAGS
46 global readelf_size
47
48 set readelf_size ""
49 set testname "finding out ELF size with readelf -h"
50 set got [remote_exec host "$READELF $READELFFLAGS -h $binary_file" "" "/dev/null" "readelf.out"]
51 if [is_remote host] then {
52 remote_upload host "readelf.out"
53 }
54
55 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
56 send_log $got
57 fail $testname
58 return
59 }
60
61 if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
62 [file_contents readelf.out] nil readelf_size] } {
63 verbose -log "EI_CLASS field not found in output"
64 verbose -log "output is \n[file_contents readelf.out]"
65 fail $testname
66 return
67 } else {
68 verbose -log "ELF size is $readelf_size"
69 }
70
71 pass $testname
72 }
73
74 # Run an individual readelf test.
75 # Basically readelf is run on the binary_file with the given options.
76 # Readelf's output is captured and then compared against the contents
77 # of the regexp_file-readelf_size if it exists, else regexp_file.
78
79 proc readelf_test { options binary_file regexp_file xfails } {
80
81 global READELF
82 global READELFFLAGS
83 global readelf_size
84 global srcdir
85 global subdir
86
87 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
88 set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
89
90 foreach xfail $xfails {
91 setup_xfail $xfail
92 }
93
94 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
95 fail "readelf $options (reason: unexpected output)"
96 send_log $got
97 send_log "\n"
98 return
99 }
100
101 set target_machine ""
102 if [istarget "mips*-*-*"] then {
103 if { [istarget "mips*-*-*linux*"]
104 || [istarget "mips*-sde-elf*"]
105 || [istarget "mips*-mti-elf*"]
106 || [istarget "mips*-*freebsd*"] } then {
107 set target_machine tmips
108 } else {
109 set target_machine mips
110 }
111 }
112
113 if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
114 set regexp_file $regexp_file-$readelf_size-$target_machine
115 } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
116 set regexp_file $regexp_file-$target_machine
117 } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
118 set regexp_file $regexp_file-$readelf_size
119 }
120
121 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
122 fail "readelf $options"
123 verbose "output is \n[file_contents readelf.out]" 2
124 return
125 }
126
127 pass "readelf $options"
128 }
129
130 # Simple proc to skip certain expected warning messages.
131
132 proc prune_readelf_wi_warnings { text } {
133 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
134 return $text
135 }
136
137 # Testing the "readelf -wi" option is difficult because there
138 # is no guaranteed order to the output, and because some ports
139 # will use indirect string references, whilst others will use
140 # direct references. So instead of having an expected output
141 # file, like the other readelf tests, we grep for strings that
142 # really ought to be there.
143
144 proc readelf_wi_test {} {
145 global READELF
146 global READELFFLAGS
147 global srcdir
148 global subdir
149
150 # Compile the second test file.
151 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
152 verbose "Unable to compile test file."
153 untested "readelf -wi"
154 return
155 }
156
157 # Download it.
158 set tempfile [remote_download host tmpdir/testprog.o]
159
160 # Run "readelf -wi" on it.
161 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
162
163 # Upload the results.
164 set output [remote_upload host readelf.out]
165
166 file_on_host delete $tempfile
167
168 # Strip any superflous warnings.
169 set got [prune_readelf_wi_warnings [lindex $got 1]]
170
171 if ![string match "" $got] then {
172 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
173 send_log $got
174 send_log "\n"
175 return
176 }
177
178 if ![file size $output] then {
179 # If the output file is empty, then this target does not
180 # generate dwarf2 output. This is not a failure.
181 verbose "No output from 'readelf -wi'"
182 untested "readelf -wi"
183 return
184 }
185
186 # Search for strings that should be in the output.
187 set sought {
188 ".*DW_TAG_compile_unit.*"
189 ".*DW_TAG_subprogram.*"
190 ".*DW_TAG_base_type.*"
191 ".*DW_AT_producer.*(GNU C|indirect string).*"
192 ".*DW_AT_language.*ANSI C.*"
193 ".*DW_AT_name.*(testprog.c|indirect string).*"
194 ".*DW_AT_name.*fn.*"
195 ".*DW_AT_name.*(main|indirect string).*"
196 ".*\(DW_OP_addr: 0\).*"
197 }
198
199 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
200 setup_xfail msp430*-*-*
201
202 foreach looked_for $sought {
203 set lines [grep $output $looked_for]
204 if ![llength $lines] then {
205 fail "readelf -wi: missing: $looked_for"
206 send_log readelf.out
207 return
208 }
209 }
210
211 file_on_host delete $output
212
213 # All done.
214 pass "readelf -wi"
215 }
216
217 # This tests "readelf -wa", but on a file with a compressed
218 # .debug_abbrev section.
219
220 proc readelf_compressed_wa_test {} {
221 global READELF
222 global READELFFLAGS
223 global srcdir
224 global subdir
225
226 if { ![is_zlib_supported] } {
227 unsupported "readelf -wa (compressed)"
228 return
229 }
230
231 # Compile the compressed-debug-section test file.
232 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
233 verbose "Unable to compile test file."
234 untested "readelf -wa (compressed)"
235 return
236 }
237
238 # Download it.
239 set tempfile [remote_download host tmpdir/dw2-compressed.o]
240
241 # Run "readelf -wa" on it.
242 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
243
244 # Upload the results.
245 set output [remote_upload host readelf.out]
246
247 file_on_host delete $tempfile
248
249 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
250 fail "readelf -wa (compressed)"
251 verbose "output is \n[file_contents readelf.out]" 2
252 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
253 return
254 }
255
256 pass "readelf -wa (compressed)"
257 }
258
259 # Test readelf's dumping abilities.
260
261 proc readelf_dump_test {} {
262 global READELF
263 global READELFFLAGS
264 global srcdir
265 global subdir
266
267 # Assemble the dump test file.
268 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
269 unresolved "readelf -p: failed to assemble dump test file"
270 return
271 }
272 # Download it.
273 set tempfile [remote_download host tmpdir/dumptest.o]
274
275 # Run "readelf -p.data" on it.
276 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
277 set got [lindex $got 1]
278
279 # Upload the results.
280 set output [remote_upload host readelf.out]
281
282 # Check for something going wrong.
283 if ![string match "" $got] then {
284 fail "readelf -p: unexpected output"
285 send_log $got
286 send_log "\n"
287 return
288 }
289
290 # Search for strings that should be in the output.
291 set sought {
292 ".*test_string.*"
293 }
294
295 foreach looked_for $sought {
296 set lines [grep $output $looked_for]
297 if ![llength $lines] then {
298 fail "readelf -p: missing: $looked_for"
299 send_log readelf.out
300 return
301 }
302 }
303
304 file_on_host delete $tempfile
305 file_on_host delete $output
306
307 # All done.
308 pass "readelf -p"
309
310 # XXX FIXME: Add test of readelf -x here
311 }
312
313 if ![is_remote host] {
314 if {[which $READELF] == 0} then {
315 perror "$READELF does not exist"
316 return
317 }
318 }
319
320 send_user "Version [binutil_version $READELF]"
321
322 # Assemble the test file.
323 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
324 perror "could not assemble test file"
325 unresolved "readelf - failed to assemble"
326 return
327 }
328
329 if ![is_remote host] {
330 set tempfile tmpdir/bintest.o
331 } else {
332 set tempfile [remote_download host tmpdir/bintest.o]
333 }
334
335 # First, determine the size, so specific output matchers can be used.
336 readelf_find_size $tempfile
337
338 # Run the tests.
339 readelf_test -h $tempfile readelf.h {}
340 readelf_test -S $tempfile readelf.s {}
341 readelf_test -s $tempfile readelf.ss {}
342 readelf_test -r $tempfile readelf.r {}
343
344 readelf_wi_test
345 readelf_compressed_wa_test
346
347 readelf_dump_test
348
349 # PR 13482 - Check for off-by-one errors when dumping .note sections.
350 if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
351 perror "could not assemble version note test file"
352 unresolved "readelf - failed to assemble"
353 return
354 }
355
356 if ![is_remote host] {
357 set tempfile tmpdir/version.o
358 } else {
359 set tempfile [remote_download host tmpdir/version.o]
360 }
361
362 readelf_test -n $tempfile readelf.n {}
This page took 0.038416 seconds and 5 git commands to generate.