2007-07-05 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
CommitLineData
aef6203b 1# Copyright 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
252b5132
RH
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 2 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
b43b5d5f 15# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-dejagnu@prep.ai.mit.edu
19
113675b7 20# Written by Nick Clifton <nickc@cygnus.com>
252b5132
RH
21# Based on scripts written by Ian Lance Taylor <ian@cygnus.com>
22# and Ken Raeburn <raeburn@cygnus.com>.
23
24# First some helpful procedures, then the tests themselves
25
26# Return the contents of the filename given
27proc file_contents { filename } {
28 set file [open $filename r]
29 set contents [read $file]
30 close $file
31 return $contents
32}
33
9921923c
HPN
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
36# EI_CLASS line here.
37proc readelf_find_size { binary_file } {
38 global READELF
39 global READELFFLAGS
40 global readelf_size
41
42 set readelf_size ""
43 set testname "finding out ELF size with readelf -h"
44 catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got
45
46 if ![string match "" $got] then {
47 send_log $got
48 fail $testname
49 return
50 }
51
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]"
56 fail $testname
57 return
58 } else {
59 verbose -log "ELF size is $readelf_size"
60 }
61
62 pass $testname
63}
64
252b5132
RH
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
9921923c 68# of the regexp_file-readelf_size if it exists, else regexp_file.
252b5132
RH
69
70proc readelf_test { options binary_file regexp_file xfails } {
71
72 global READELF
73 global READELFFLAGS
9921923c 74 global readelf_size
252b5132
RH
75 global srcdir
76 global subdir
77
9921923c 78 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
252b5132
RH
79 catch "exec $READELF $READELFFLAGS $options $binary_file > readelf.out" got
80
9a1c9383
NC
81 foreach xfail $xfails {
82 setup_xfail $xfail
252b5132 83 }
3f7de0e7 84
252b5132 85 if ![string match "" $got] then {
3f7de0e7 86 fail "readelf $options (reason: unexpected output)"
252b5132 87 send_log $got
3f7de0e7 88 send_log "\n"
252b5132
RH
89 return
90 }
91
ade0b24f
NC
92 set target_machine ""
93 if [istarget "mips*-*-*"] then {
4be041b2 94 if { [istarget "mips*-*-*linux*"] || [istarget "mips*-sde-elf*"] } then {
eed3fa3b
L
95 set target_machine tmips
96 } else {
97 set target_machine mips
98 }
ade0b24f
NC
99 }
100
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 {
9921923c
HPN
106 set regexp_file $regexp_file-$readelf_size
107 }
108
252b5132
RH
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
112 return
113 }
114
115 pass "readelf $options"
116}
117
cbb356d9
NC
118# Simple proc to skip certain expected warning messages.
119
120proc prune_readelf_wi_warnings { text } {
121 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
122 return $text
123}
124
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.
131
132proc readelf_wi_test {} {
133 global READELF
134 global READELFFLAGS
135 global srcdir
136 global subdir
137
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"
142 return
143 }
144
145 # Download it.
8d263650 146 set tempfile [remote_download host tmpdir/testprog.o]
cbb356d9
NC
147
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
151
152 # Upload the results.
8d263650 153 set output [remote_upload host readelf.out]
cbb356d9 154
8d263650 155 file_on_host delete $tempfile
cbb356d9
NC
156
157 # Strip any superflous warnings.
158 set got [prune_readelf_wi_warnings $got]
159
160 if ![string match "" $got] then {
b279a55c 161 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
cbb356d9
NC
162 send_log $got
163 send_log "\n"
164 return
165 }
166
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"
172 return
173 }
174
175 # Search for strings that should be in the output.
176 set sought {
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).*"
183 ".*DW_AT_name.*fn.*"
184 ".*DW_AT_name.*(main|indirect string).*"
aaa222e7 185 ".*\(DW_OP_addr: 0\).*"
cbb356d9
NC
186 }
187
188 foreach looked_for $sought {
189 set lines [grep $output $looked_for]
190 if ![llength $lines] then {
191 fail "readelf -wi: missing: $looked_for"
192 send_log readelf.out
193 return
194 }
195 }
196
8d263650 197 file_on_host delete $output
cbb356d9
NC
198
199 # All done.
200 pass "readelf -wi"
201}
252b5132
RH
202
203
9ce701e2
L
204# Exclude non-ELF targets.
205if ![is_elf_format] {
9921923c 206 verbose "$READELF is only intended for ELF targets" 2
252b5132
RH
207 return
208}
209
210if ![is_remote host] {
211 if {[which $READELF] == 0} then {
212 perror "$READELF does not exist"
213 return
214 }
215}
216
217send_user "Version [binutil_version $READELF]"
218
89084430 219# Assemble the test file.
252b5132
RH
220if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
221 perror "unresolved 1"
222 unresolved "readelf - failed to assemble"
223 return
224}
225
226if ![is_remote host] {
8d263650 227 set tempfile tmpdir/bintest.o
252b5132
RH
228} else {
229 set tempfile [remote_download host tmpdir/bintest.o]
230}
231
9921923c
HPN
232# First, determine the size, so specific output matchers can be used.
233readelf_find_size $tempfile
234
235# Run the tests.
252b5132 236readelf_test -h $tempfile readelf.h {}
ade0b24f
NC
237readelf_test -S $tempfile readelf.s {}
238readelf_test -s $tempfile readelf.ss {}
252b5132
RH
239readelf_test -r $tempfile readelf.r {}
240
cbb356d9 241readelf_wi_test
This page took 0.298216 seconds and 4 git commands to generate.