* archures.c: Add some more MSP430 machine numbers.
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
1 # Copyright 1999-2013 Free Software Foundation, Inc.
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 3 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
15 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
19
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>.
23
24 # Exclude non-ELF targets.
25 if ![is_elf_format] {
26 verbose "$READELF is only intended for ELF targets" 2
27 return
28 }
29
30 # First some helpful procedures, then the tests themselves
31
32 # Return the contents of the filename given
33 proc file_contents { filename } {
34 set file [open $filename r]
35 set contents [read $file]
36 close $file
37 return $contents
38 }
39
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
42 # EI_CLASS line here.
43 proc readelf_find_size { binary_file } {
44 global READELF
45 global READELFFLAGS
46 global readelf_size
47
48 set readelf_size ""
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"
53 }
54
55 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
56 send_log $got
57 fail $testname
58 return
59 }
60
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]"
65 fail $testname
66 return
67 } else {
68 verbose -log "ELF size is $readelf_size"
69 }
70
71 pass $testname
72 }
73
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.
78
79 proc readelf_test { options binary_file regexp_file xfails } {
80
81 global READELF
82 global READELFFLAGS
83 global readelf_size
84 global srcdir
85 global subdir
86
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"]
89
90 foreach xfail $xfails {
91 setup_xfail $xfail
92 }
93
94 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
95 fail "readelf $options (reason: unexpected output)"
96 send_log $got
97 send_log "\n"
98 return
99 }
100
101 set target_machine ""
102 if [istarget "mips*-*-*"] then {
103 if { [istarget "mips*-*-*linux*"]
104 || [istarget "mips*-sde-elf*"]
105 || [istarget "mips*-*freebsd*"] } then {
106 set target_machine tmips
107 } else {
108 set target_machine mips
109 }
110 }
111
112 if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
113 set regexp_file $regexp_file-$readelf_size-$target_machine
114 } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
115 set regexp_file $regexp_file-$target_machine
116 } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
117 set regexp_file $regexp_file-$readelf_size
118 }
119
120 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
121 fail "readelf $options"
122 verbose "output is \n[file_contents readelf.out]" 2
123 return
124 }
125
126 pass "readelf $options"
127 }
128
129 # Simple proc to skip certain expected warning messages.
130
131 proc prune_readelf_wi_warnings { text } {
132 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
133 return $text
134 }
135
136 # Testing the "readelf -wi" option is difficult because there
137 # is no guaranteed order to the output, and because some ports
138 # will use indirect string references, whilst others will use
139 # direct references. So instead of having an expected output
140 # file, like the other readelf tests, we grep for strings that
141 # really ought to be there.
142
143 proc readelf_wi_test {} {
144 global READELF
145 global READELFFLAGS
146 global srcdir
147 global subdir
148
149 # Compile the second test file.
150 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
151 verbose "Unable to compile test file."
152 untested "readelf -wi"
153 return
154 }
155
156 # Download it.
157 set tempfile [remote_download host tmpdir/testprog.o]
158
159 # Run "readelf -wi" on it.
160 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
161
162 # Upload the results.
163 set output [remote_upload host readelf.out]
164
165 file_on_host delete $tempfile
166
167 # Strip any superflous warnings.
168 set got [prune_readelf_wi_warnings [lindex $got 1]]
169
170 if ![string match "" $got] then {
171 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
172 send_log $got
173 send_log "\n"
174 return
175 }
176
177 if ![file size $output] then {
178 # If the output file is empty, then this target does not
179 # generate dwarf2 output. This is not a failure.
180 verbose "No output from 'readelf -wi'"
181 untested "readelf -wi"
182 return
183 }
184
185 # Search for strings that should be in the output.
186 set sought {
187 ".*DW_TAG_compile_unit.*"
188 ".*DW_TAG_subprogram.*"
189 ".*DW_TAG_base_type.*"
190 ".*DW_AT_producer.*(GNU C|indirect string).*"
191 ".*DW_AT_language.*ANSI C.*"
192 ".*DW_AT_name.*(testprog.c|indirect string).*"
193 ".*DW_AT_name.*fn.*"
194 ".*DW_AT_name.*(main|indirect string).*"
195 ".*\(DW_OP_addr: 0\).*"
196 }
197
198 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
199 setup_xfail msp430*-*-*
200
201 foreach looked_for $sought {
202 set lines [grep $output $looked_for]
203 if ![llength $lines] then {
204 fail "readelf -wi: missing: $looked_for"
205 send_log readelf.out
206 return
207 }
208 }
209
210 file_on_host delete $output
211
212 # All done.
213 pass "readelf -wi"
214 }
215
216 # This tests "readelf -wa", but on a file with a compressed
217 # .debug_abbrev section.
218
219 proc readelf_compressed_wa_test {} {
220 global READELF
221 global READELFFLAGS
222 global srcdir
223 global subdir
224
225 if { ![is_zlib_supported] } {
226 unsupported "readelf -wa (compressed)"
227 return
228 }
229
230 # Compile the compressed-debug-section test file.
231 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
232 verbose "Unable to compile test file."
233 untested "readelf -wa (compressed)"
234 return
235 }
236
237 # Download it.
238 set tempfile [remote_download host tmpdir/dw2-compressed.o]
239
240 # Run "readelf -wa" on it.
241 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
242
243 # Upload the results.
244 set output [remote_upload host readelf.out]
245
246 file_on_host delete $tempfile
247
248 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
249 fail "readelf -wa (compressed)"
250 verbose "output is \n[file_contents readelf.out]" 2
251 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
252 return
253 }
254
255 pass "readelf -wa (compressed)"
256 }
257
258 # Test readelf's dumping abilities.
259
260 proc readelf_dump_test {} {
261 global READELF
262 global READELFFLAGS
263 global srcdir
264 global subdir
265
266 # Assemble the dump test file.
267 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
268 unresolved "readelf -p: failed to assemble dump test file"
269 return
270 }
271 # Download it.
272 set tempfile [remote_download host tmpdir/dumptest.o]
273
274 # Run "readelf -p.data" on it.
275 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
276 set got [lindex $got 1]
277
278 # Upload the results.
279 set output [remote_upload host readelf.out]
280
281 # Check for something going wrong.
282 if ![string match "" $got] then {
283 fail "readelf -p: unexpected output"
284 send_log $got
285 send_log "\n"
286 return
287 }
288
289 # Search for strings that should be in the output.
290 set sought {
291 ".*test_string.*"
292 }
293
294 foreach looked_for $sought {
295 set lines [grep $output $looked_for]
296 if ![llength $lines] then {
297 fail "readelf -p: missing: $looked_for"
298 send_log readelf.out
299 return
300 }
301 }
302
303 file_on_host delete $tempfile
304 file_on_host delete $output
305
306 # All done.
307 pass "readelf -p"
308
309 # XXX FIXME: Add test of readelf -x here
310 }
311
312 if ![is_remote host] {
313 if {[which $READELF] == 0} then {
314 perror "$READELF does not exist"
315 return
316 }
317 }
318
319 send_user "Version [binutil_version $READELF]"
320
321 # Assemble the test file.
322 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
323 perror "could not assemble test file"
324 unresolved "readelf - failed to assemble"
325 return
326 }
327
328 if ![is_remote host] {
329 set tempfile tmpdir/bintest.o
330 } else {
331 set tempfile [remote_download host tmpdir/bintest.o]
332 }
333
334 # First, determine the size, so specific output matchers can be used.
335 readelf_find_size $tempfile
336
337 # Run the tests.
338 readelf_test -h $tempfile readelf.h {}
339 readelf_test -S $tempfile readelf.s {}
340 readelf_test -s $tempfile readelf.ss {}
341 readelf_test -r $tempfile readelf.r {}
342
343 readelf_wi_test
344 readelf_compressed_wa_test
345
346 readelf_dump_test
347
348 # PR 13482 - Check for off-by-one errors when dumping .note sections.
349 if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
350 perror "could not assemble version note test file"
351 unresolved "readelf - failed to assemble"
352 return
353 }
354
355 if ![is_remote host] {
356 set tempfile tmpdir/version.o
357 } else {
358 set tempfile [remote_download host tmpdir/version.o]
359 }
360
361 readelf_test -n $tempfile readelf.n {}
This page took 0.038103 seconds and 4 git commands to generate.