1 # Copyright 1999, 2000, 2001, 2003, 2004, 2007 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 # First some helpful procedures, then the tests themselves
26 # Return the contents of the filename given
27 proc file_contents { filename } {
28 set file [open $filename r]
29 set contents [read $file]
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
37 proc readelf_find_size { binary_file } {
43 set testname "finding out ELF size with readelf -h"
44 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
46 if ![string match "" $got] then {
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]"
59 verbose -log "ELF size is $readelf_size"
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.
70 proc readelf_test { options binary_file regexp_file xfails } {
78 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
79 catch "exec $READELF $READELFFLAGS $options $binary_file > readelf.out" got
81 foreach xfail $xfails {
85 if ![string match "" $got] then {
86 fail "readelf $options (reason: unexpected output)"
93 if [istarget "mips*-*-*"] then {
94 if { [istarget "mips*-*-*linux*"] || [istarget "mips*-sde-elf*"] } then {
95 set target_machine tmips
97 set target_machine mips
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
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
115 pass "readelf $options"
118 # Simple proc to skip certain expected warning messages.
120 proc prune_readelf_wi_warnings { text } {
121 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
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.
132 proc readelf_wi_test {} {
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"
146 set tempfile [remote_download host tmpdir/testprog.o]
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
152 # Upload the results.
153 set output [remote_upload host readelf.out]
155 file_on_host delete $tempfile
157 # Strip any superflous warnings.
158 set got [prune_readelf_wi_warnings $got]
160 if ![string match "" $got] then {
161 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
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"
175 # Search for strings that should be in the output.
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).*"
184 ".*DW_AT_name.*(main|indirect string).*"
185 ".*\(DW_OP_addr: 0\).*"
188 foreach looked_for $sought {
189 set lines [grep $output $looked_for]
190 if ![llength $lines] then {
191 fail "readelf -wi: missing: $looked_for"
197 file_on_host delete $output
204 # Exclude non-ELF targets.
205 if ![is_elf_format] {
206 verbose "$READELF is only intended for ELF targets" 2
210 if ![is_remote host] {
211 if {[which $READELF] == 0} then {
212 perror "$READELF does not exist"
217 send_user "Version [binutil_version $READELF]"
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"
226 if ![is_remote host] {
227 set tempfile tmpdir/bintest.o
229 set tempfile [remote_download host tmpdir/bintest.o]
232 # First, determine the size, so specific output matchers can be used.
233 readelf_find_size $tempfile
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 {}