]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/testsuite/binutils-all/readelf.exp
* dwarf.c (display_debug_lines_raw): Do not treat .debug_line.dwo
[thirdparty/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
CommitLineData
8b971f9f 1# Copyright 1999, 2000, 2001, 2003, 2004, 2007, 2009, 2012
aa820537 2# Free Software Foundation, Inc.
252b5132
RH
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
32866df7 6# the Free Software Foundation; either version 3 of the License, or
252b5132 7# (at your option) any later version.
65951855 8#
252b5132
RH
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
65951855 13#
252b5132
RH
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
b43b5d5f 16# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
17
18# Please email any bugs, comments, and/or additions to this file to:
19# bug-dejagnu@prep.ai.mit.edu
20
113675b7 21# Written by Nick Clifton <nickc@cygnus.com>
252b5132
RH
22# Based on scripts written by Ian Lance Taylor <ian@cygnus.com>
23# and Ken Raeburn <raeburn@cygnus.com>.
24
09c11c86
NC
25# Exclude non-ELF targets.
26if ![is_elf_format] {
27 verbose "$READELF is only intended for ELF targets" 2
28 return
29}
30
252b5132
RH
31# First some helpful procedures, then the tests themselves
32
33# Return the contents of the filename given
34proc file_contents { filename } {
35 set file [open $filename r]
36 set contents [read $file]
37 close $file
38 return $contents
39}
40
9921923c
HPN
41# Find out the size by reading the output of the EI_CLASS field.
42# Similar to the test for readelf -h, but we're just looking for the
43# EI_CLASS line here.
44proc readelf_find_size { binary_file } {
45 global READELF
46 global READELFFLAGS
47 global readelf_size
48
49 set readelf_size ""
50 set testname "finding out ELF size with readelf -h"
7f6a71ff
JM
51 set got [remote_exec host "$READELF $READELFFLAGS -h $binary_file" "" "/dev/null" "readelf.out"]
52 if [is_remote host] then {
53 remote_upload host "readelf.out"
54 }
9921923c 55
7f6a71ff 56 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
9921923c
HPN
57 send_log $got
58 fail $testname
59 return
60 }
61
62 if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
63 [file_contents readelf.out] nil readelf_size] } {
64 verbose -log "EI_CLASS field not found in output"
65 verbose -log "output is \n[file_contents readelf.out]"
66 fail $testname
67 return
68 } else {
69 verbose -log "ELF size is $readelf_size"
70 }
71
72 pass $testname
73}
74
252b5132
RH
75# Run an individual readelf test.
76# Basically readelf is run on the binary_file with the given options.
77# Readelf's output is captured and then compared against the contents
9921923c 78# of the regexp_file-readelf_size if it exists, else regexp_file.
252b5132
RH
79
80proc readelf_test { options binary_file regexp_file xfails } {
81
82 global READELF
83 global READELFFLAGS
9921923c 84 global readelf_size
252b5132
RH
85 global srcdir
86 global subdir
65951855 87
9921923c 88 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
7f6a71ff 89 set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
252b5132 90
9a1c9383
NC
91 foreach xfail $xfails {
92 setup_xfail $xfail
252b5132 93 }
3f7de0e7 94
7f6a71ff 95 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
3f7de0e7 96 fail "readelf $options (reason: unexpected output)"
252b5132 97 send_log $got
3f7de0e7 98 send_log "\n"
252b5132
RH
99 return
100 }
101
ade0b24f
NC
102 set target_machine ""
103 if [istarget "mips*-*-*"] then {
aeffff67
RS
104 if { [istarget "mips*-*-*linux*"]
105 || [istarget "mips*-sde-elf*"]
106 || [istarget "mips*-*freebsd*"] } then {
eed3fa3b
L
107 set target_machine tmips
108 } else {
109 set target_machine mips
110 }
ade0b24f
NC
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 {
9921923c
HPN
118 set regexp_file $regexp_file-$readelf_size
119 }
120
252b5132
RH
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
cbb356d9
NC
130# Simple proc to skip certain expected warning messages.
131
132proc 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
144proc readelf_wi_test {} {
145 global READELF
146 global READELFFLAGS
147 global srcdir
148 global subdir
65951855 149
cbb356d9
NC
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.
8d263650 158 set tempfile [remote_download host tmpdir/testprog.o]
cbb356d9
NC
159
160 # Run "readelf -wi" on it.
7f6a71ff 161 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
cbb356d9
NC
162
163 # Upload the results.
8d263650 164 set output [remote_upload host readelf.out]
cbb356d9 165
8d263650 166 file_on_host delete $tempfile
65951855 167
cbb356d9 168 # Strip any superflous warnings.
7f6a71ff 169 set got [prune_readelf_wi_warnings [lindex $got 1]]
cbb356d9
NC
170
171 if ![string match "" $got] then {
b279a55c 172 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
cbb356d9
NC
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 }
65951855 185
cbb356d9
NC
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).*"
aaa222e7 196 ".*\(DW_OP_addr: 0\).*"
cbb356d9 197 }
65951855
RM
198
199 foreach looked_for $sought {
cbb356d9
NC
200 set lines [grep $output $looked_for]
201 if ![llength $lines] then {
202 fail "readelf -wi: missing: $looked_for"
203 send_log readelf.out
204 return
205 }
206 }
207
8d263650 208 file_on_host delete $output
65951855 209
cbb356d9
NC
210 # All done.
211 pass "readelf -wi"
212}
252b5132 213
1b315056
CS
214# This tests "readelf -wa", but on a file with a compressed
215# .debug_abbrev section.
216
217proc readelf_compressed_wa_test {} {
218 global READELF
219 global READELFFLAGS
220 global srcdir
221 global subdir
65951855
RM
222
223 if { ![is_zlib_supported] } {
224 unsupported "readelf -wa (compressed)"
225 return
226 }
227
1b315056
CS
228 # Compile the compressed-debug-section test file.
229 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
230 verbose "Unable to compile test file."
231 untested "readelf -wa (compressed)"
232 return
233 }
234
235 # Download it.
236 set tempfile [remote_download host tmpdir/dw2-compressed.o]
237
238 # Run "readelf -wa" on it.
239 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
240
241 # Upload the results.
242 set output [remote_upload host readelf.out]
243
244 file_on_host delete $tempfile
65951855 245
1b315056
CS
246 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
247 fail "readelf -wa (compressed)"
248 verbose "output is \n[file_contents readelf.out]" 2
249 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
250 return
251 }
252
253 pass "readelf -wa (compressed)"
254}
252b5132 255
09c11c86
NC
256# Test readelf's dumping abilities.
257
258proc readelf_dump_test {} {
259 global READELF
260 global READELFFLAGS
261 global srcdir
262 global subdir
65951855 263
09c11c86
NC
264 # Assemble the dump test file.
265 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
266 unresolved "readelf -p: failed to assemble dump test file"
267 return
268 }
269 # Download it.
270 set tempfile [remote_download host tmpdir/dumptest.o]
271
272 # Run "readelf -p.data" on it.
273 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
274 set got [lindex $got 1]
275
276 # Upload the results.
277 set output [remote_upload host readelf.out]
278
279 # Check for something going wrong.
280 if ![string match "" $got] then {
281 fail "readelf -p: unexpected output"
282 send_log $got
283 send_log "\n"
284 return
285 }
286
287 # Search for strings that should be in the output.
288 set sought {
289 ".*test_string.*"
290 }
65951855
RM
291
292 foreach looked_for $sought {
09c11c86
NC
293 set lines [grep $output $looked_for]
294 if ![llength $lines] then {
295 fail "readelf -p: missing: $looked_for"
296 send_log readelf.out
297 return
298 }
299 }
300
65951855 301 file_on_host delete $tempfile
09c11c86
NC
302 file_on_host delete $output
303
304 # All done.
305 pass "readelf -p"
306
307 # XXX FIXME: Add test of readelf -x here
252b5132
RH
308}
309
310if ![is_remote host] {
311 if {[which $READELF] == 0} then {
312 perror "$READELF does not exist"
313 return
314 }
315}
316
317send_user "Version [binutil_version $READELF]"
318
89084430 319# Assemble the test file.
252b5132 320if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
09c11c86 321 perror "could not assemble test file"
252b5132
RH
322 unresolved "readelf - failed to assemble"
323 return
324}
325
326if ![is_remote host] {
8d263650 327 set tempfile tmpdir/bintest.o
252b5132
RH
328} else {
329 set tempfile [remote_download host tmpdir/bintest.o]
330}
331
9921923c
HPN
332# First, determine the size, so specific output matchers can be used.
333readelf_find_size $tempfile
334
335# Run the tests.
252b5132 336readelf_test -h $tempfile readelf.h {}
ade0b24f
NC
337readelf_test -S $tempfile readelf.s {}
338readelf_test -s $tempfile readelf.ss {}
252b5132
RH
339readelf_test -r $tempfile readelf.r {}
340
cbb356d9 341readelf_wi_test
1b315056 342readelf_compressed_wa_test
09c11c86
NC
343
344readelf_dump_test
8b971f9f
NC
345
346# PR 13482 - Check for off-by-one errors when dumping .note sections.
347if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
348 perror "could not assemble version note test file"
349 unresolved "readelf - failed to assemble"
350 return
351}
352
353if ![is_remote host] {
354 set tempfile tmpdir/version.o
355} else {
356 set tempfile [remote_download host tmpdir/version.o]
357}
358
359readelf_test -n $tempfile readelf.n {}