1 # Copyright (C) 1999-2015 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 3 of the License, or
6 # (at your option) any later version.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 # Exclude non-ELF targets.
26 verbose "$READELF is only intended for ELF targets" 2
30 # First some helpful procedures, then the tests themselves
32 # Return the contents of the filename given
33 proc file_contents { filename } {
34 set file [open $filename r]
35 set contents [read $file]
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
43 proc readelf_find_size { binary_file } {
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"
55 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
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]"
68 verbose -log "ELF size is $readelf_size"
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.
79 proc readelf_test { options binary_file regexp_file xfails } {
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"]
90 foreach xfail $xfails {
94 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
95 fail "readelf $options (reason: unexpected output)"
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*-img-elf*"]
107 || [istarget "mips*-*freebsd*"] } then {
108 set target_machine tmips
110 set target_machine mips
114 if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
115 set regexp_file $regexp_file-$readelf_size-$target_machine
116 } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
117 set regexp_file $regexp_file-$target_machine
118 } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
119 set regexp_file $regexp_file-$readelf_size
122 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
123 fail "readelf $options"
124 verbose "output is \n[file_contents readelf.out]" 2
128 pass "readelf $options"
131 # Simple proc to skip certain expected warning messages.
133 proc prune_readelf_wi_warnings { text } {
134 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
138 # Testing the "readelf -wi" option is difficult because there
139 # is no guaranteed order to the output, and because some ports
140 # will use indirect string references, whilst others will use
141 # direct references. So instead of having an expected output
142 # file, like the other readelf tests, we grep for strings that
143 # really ought to be there.
145 proc readelf_wi_test {} {
151 # Compile the second test file.
152 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
153 verbose "Unable to compile test file."
154 untested "readelf -wi"
159 set tempfile [remote_download host tmpdir/testprog.o]
161 # Run "readelf -wi" on it.
162 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
164 # Upload the results.
165 set output [remote_upload host readelf.out]
167 file_on_host delete $tempfile
169 # Strip any superflous warnings.
170 set got [prune_readelf_wi_warnings [lindex $got 1]]
172 if ![string match "" $got] then {
173 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
179 if ![file size $output] then {
180 # If the output file is empty, then this target does not
181 # generate dwarf2 output. This is not a failure.
182 verbose "No output from 'readelf -wi'"
183 untested "readelf -wi"
187 # Search for strings that should be in the output.
189 ".*DW_TAG_compile_unit.*"
190 ".*DW_TAG_subprogram.*"
191 ".*DW_TAG_base_type.*"
192 ".*DW_AT_producer.*(GNU C|indirect string).*"
193 ".*DW_AT_language.*ANSI C.*"
194 ".*DW_AT_name.*(testprog.c|indirect string).*"
196 ".*DW_AT_name.*(main|indirect string).*"
197 ".*\(DW_OP_addr: 0\).*"
200 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
201 setup_xfail msp430*-*-*
203 foreach looked_for $sought {
204 set lines [grep $output $looked_for]
205 if ![llength $lines] then {
206 fail "readelf -wi: missing: $looked_for"
212 file_on_host delete $output
218 # This tests "readelf -wa", but on a file with a compressed
219 # .debug_abbrev section.
221 proc readelf_compressed_wa_test {} {
227 # Compile the compressed-debug-section test file.
228 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
229 verbose "Unable to compile test file."
230 untested "readelf -wa (compressed)"
235 set tempfile [remote_download host tmpdir/dw2-compressed.o]
237 # Run "readelf -wa" on it.
238 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
240 # Upload the results.
241 set output [remote_upload host readelf.out]
243 file_on_host delete $tempfile
245 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
246 fail "readelf -wa (compressed)"
247 verbose "output is \n[file_contents readelf.out]" 2
248 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
252 pass "readelf -wa (compressed)"
255 # Test readelf's dumping abilities.
257 proc readelf_dump_test {} {
263 # Assemble the dump test file.
264 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
265 unresolved "readelf -p: failed to assemble dump test file"
269 set tempfile [remote_download host tmpdir/dumptest.o]
271 # Run "readelf -p.data" on it.
272 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
273 set got [lindex $got 1]
275 # Upload the results.
276 set output [remote_upload host readelf.out]
278 # Check for something going wrong.
279 if ![string match "" $got] then {
280 fail "readelf -p: unexpected output"
286 # Search for strings that should be in the output.
291 foreach looked_for $sought {
292 set lines [grep $output $looked_for]
293 if ![llength $lines] then {
294 fail "readelf -p: missing: $looked_for"
300 file_on_host delete $tempfile
301 file_on_host delete $output
306 # XXX FIXME: Add test of readelf -x here
309 if ![is_remote host] {
310 if {[which $READELF] == 0} then {
311 perror "$READELF does not exist"
316 send_user "Version [binutil_version $READELF]"
318 # Assemble the test file.
319 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
320 perror "could not assemble test file"
321 unresolved "readelf - failed to assemble"
325 if ![is_remote host] {
326 set tempfile tmpdir/bintest.o
328 set tempfile [remote_download host tmpdir/bintest.o]
331 # First, determine the size, so specific output matchers can be used.
332 readelf_find_size $tempfile
335 readelf_test -h $tempfile readelf.h {}
336 readelf_test -S $tempfile readelf.s {}
337 readelf_test -s $tempfile readelf.ss {}
338 readelf_test -r $tempfile readelf.r {}
341 readelf_compressed_wa_test
345 # PR 13482 - Check for off-by-one errors when dumping .note sections.
346 if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
347 perror "could not assemble version note test file"
348 unresolved "readelf - failed to assemble"
352 if ![is_remote host] {
353 set tempfile tmpdir/version.o
355 set tempfile [remote_download host tmpdir/version.o]
358 readelf_test -n $tempfile readelf.n {}