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 set got [remote_exec host "$READELF $READELFFLAGS -h $binary_file" "" "/dev/null" "readelf.out"]
45 if [is_remote host] then {
46 remote_upload host "readelf.out"
49 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
55 if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
56 [file_contents readelf.out] nil readelf_size] } {
57 verbose -log "EI_CLASS field not found in output"
58 verbose -log "output is \n[file_contents readelf.out]"
62 verbose -log "ELF size is $readelf_size"
68 # Run an individual readelf test.
69 # Basically readelf is run on the binary_file with the given options.
70 # Readelf's output is captured and then compared against the contents
71 # of the regexp_file-readelf_size if it exists, else regexp_file.
73 proc readelf_test { options binary_file regexp_file xfails } {
81 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
82 set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
84 foreach xfail $xfails {
88 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
89 fail "readelf $options (reason: unexpected output)"
96 if [istarget "mips*-*-*"] then {
97 if { [istarget "mips*-*-*linux*"] || [istarget "mips*-sde-elf*"] } then {
98 set target_machine tmips
100 set target_machine mips
104 if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
105 set regexp_file $regexp_file-$readelf_size-$target_machine
106 } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
107 set regexp_file $regexp_file-$target_machine
108 } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
109 set regexp_file $regexp_file-$readelf_size
112 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
113 fail "readelf $options"
114 verbose "output is \n[file_contents readelf.out]" 2
118 pass "readelf $options"
121 # Simple proc to skip certain expected warning messages.
123 proc prune_readelf_wi_warnings { text } {
124 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
128 # Testing the "readelf -wi" option is difficult because there
129 # is no guaranteed order to the output, and because some ports
130 # will use indirect string references, whilst others will use
131 # direct references. So instead of having an expected output
132 # file, like the other readelf tests, we grep for strings that
133 # really ought to be there.
135 proc readelf_wi_test {} {
141 # Compile the second test file.
142 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
143 verbose "Unable to compile test file."
144 untested "readelf -wi"
149 set tempfile [remote_download host tmpdir/testprog.o]
151 # Run "readelf -wi" on it.
152 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
154 # Upload the results.
155 set output [remote_upload host readelf.out]
157 file_on_host delete $tempfile
159 # Strip any superflous warnings.
160 set got [prune_readelf_wi_warnings [lindex $got 1]]
162 if ![string match "" $got] then {
163 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
169 if ![file size $output] then {
170 # If the output file is empty, then this target does not
171 # generate dwarf2 output. This is not a failure.
172 verbose "No output from 'readelf -wi'"
173 untested "readelf -wi"
177 # Search for strings that should be in the output.
179 ".*DW_TAG_compile_unit.*"
180 ".*DW_TAG_subprogram.*"
181 ".*DW_TAG_base_type.*"
182 ".*DW_AT_producer.*(GNU C|indirect string).*"
183 ".*DW_AT_language.*ANSI C.*"
184 ".*DW_AT_name.*(testprog.c|indirect string).*"
186 ".*DW_AT_name.*(main|indirect string).*"
187 ".*\(DW_OP_addr: 0\).*"
190 foreach looked_for $sought {
191 set lines [grep $output $looked_for]
192 if ![llength $lines] then {
193 fail "readelf -wi: missing: $looked_for"
199 file_on_host delete $output
206 # Exclude non-ELF targets.
207 if ![is_elf_format] {
208 verbose "$READELF is only intended for ELF targets" 2
212 if ![is_remote host] {
213 if {[which $READELF] == 0} then {
214 perror "$READELF does not exist"
219 send_user "Version [binutil_version $READELF]"
221 # Assemble the test file.
222 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
223 perror "unresolved 1"
224 unresolved "readelf - failed to assemble"
228 if ![is_remote host] {
229 set tempfile tmpdir/bintest.o
231 set tempfile [remote_download host tmpdir/bintest.o]
234 # First, determine the size, so specific output matchers can be used.
235 readelf_find_size $tempfile
238 readelf_test -h $tempfile readelf.h {}
239 readelf_test -S $tempfile readelf.s {}
240 readelf_test -s $tempfile readelf.ss {}
241 readelf_test -r $tempfile readelf.r {}