1 # Copyright (C) 1999-2021 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 test_iteration } {
49 set testname "finding out ELF size with readelf -h ($test_iteration)"
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 } {
87 set testname "readelf $options [file rootname [file tail $binary_file]]"
89 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
90 set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
92 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
93 fail "$testname (reason: unexpected output)"
99 if { [is_elf_unused_section_symbols ] } {
100 set target_machine unused
102 set target_machine ""
104 if [istarget "mips*-*-*"] then {
105 if [is_bad_symtab] then {
106 set target_machine mips
108 set target_machine tmips
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
120 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
121 send_log [file_contents readelf.out]
130 # Simple proc to skip certain expected warning messages.
132 proc prune_readelf_wi_warnings { text } {
133 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
137 # Testing the "readelf -wi" option is difficult because there
138 # is no guaranteed order to the output, and because some ports
139 # will use indirect string references, whilst others will use
140 # direct references. So instead of having an expected output
141 # file, like the other readelf tests, we grep for strings that
142 # really ought to be there.
144 proc readelf_wi_test {} {
150 # Compile the second test file.
151 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
152 verbose "Unable to compile test file."
153 untested "readelf -wi"
158 set tempfile [remote_download host tmpdir/testprog.o]
160 # Run "readelf -wi" on it.
161 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
163 # Upload the results.
164 set output [remote_upload host readelf.out]
166 file_on_host delete $tempfile
168 # Strip any superflous warnings.
169 set got [prune_readelf_wi_warnings [lindex $got 1]]
171 if ![string match "" $got] then {
172 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
178 if ![file size $output] then {
179 # If the output file is empty, then this target does not
180 # generate dwarf2 output. This is not a failure.
181 verbose "No output from 'readelf -wi'"
182 untested "readelf -wi"
186 # Search for strings that should be in the output.
188 ".*DW_TAG_compile_unit.*"
189 ".*DW_TAG_subprogram.*"
190 ".*DW_TAG_base_type.*"
191 ".*DW_AT_producer.*(GNU C|indirect string).*"
192 ".*DW_AT_language.*(ANSI C|C11).*"
193 ".*DW_AT_name.*(testprog.c|indirect string).*"
195 ".*DW_AT_name.*(main|indirect string).*"
196 ".*\(DW_OP_addr: 0\).*"
199 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
200 setup_xfail msp430*-*-*
202 foreach looked_for $sought {
203 set lines [grep $output $looked_for]
204 if ![llength $lines] then {
205 fail "readelf -wi: missing: $looked_for"
211 file_on_host delete $output
217 # This tests "readelf -wa", but on a file with a compressed
218 # .debug_abbrev section.
220 proc readelf_compressed_wa_test {} {
226 # Compile the compressed-debug-section test file.
227 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
228 verbose "Unable to compile test file."
229 untested "readelf -wa (compressed)"
234 set tempfile [remote_download host tmpdir/dw2-compressed.o]
236 # Run "readelf -wa" on it.
237 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
239 # Upload the results.
240 set output [remote_upload host readelf.out]
242 file_on_host delete $tempfile
244 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
245 fail "readelf -wa (compressed)"
246 verbose "output is \n[file_contents readelf.out]" 2
247 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
251 pass "readelf -wa (compressed)"
254 # Test readelf's dumping abilities.
256 proc readelf_dump_test {} {
262 # Assemble the dump test file.
263 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
264 unsupported "readelf -p: failed to assemble dump test file"
268 set tempfile [remote_download host tmpdir/dumptest.o]
270 # Run "readelf -p.data" on it.
271 set sect_names [get_standard_section_names]
272 if { $sect_names != "" } {
273 set got [remote_exec host "$READELF $READELFFLAGS -p[lindex $sect_names 1] $tempfile" "" "/dev/null" "readelf.out"]
275 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
277 set got [lindex $got 1]
279 # Upload the results.
280 set output [remote_upload host readelf.out]
282 # Check for something going wrong.
283 if ![string match "" $got] then {
284 fail "readelf -p: unexpected output"
290 # Search for strings that should be in the output.
295 foreach looked_for $sought {
296 set lines [grep $output $looked_for]
297 if ![llength $lines] then {
298 fail "readelf -p: missing: $looked_for"
304 file_on_host delete $tempfile
305 file_on_host delete $output
310 # XXX FIXME: Add test of readelf -x here
313 if ![is_remote host] {
314 if {[which $READELF] == 0} then {
315 perror "$READELF does not exist"
320 send_user "Version [binutil_version $READELF]"
322 # Assemble the test file.
323 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
324 unsupported "readelf -h bintest (failed to assemble)"
325 unsupported "readelf -S bintest (failed to assemble)"
326 unsupported "readelf -s bintest (failed to assemble)"
327 unsupported "readelf -r bintest (failed to assemble)"
332 if ![is_remote host] {
333 set tempfile tmpdir/bintest.o
335 set tempfile [remote_download host tmpdir/bintest.o]
338 # First, determine the size, so specific output matchers can be used.
339 readelf_find_size $tempfile 1
342 readelf_test -h $tempfile readelf.h
343 readelf_test -S $tempfile readelf.s
344 setup_xfail "mips-*-*irix*"
345 readelf_test -s $tempfile readelf.ss
346 readelf_test -r $tempfile readelf.r
349 # Test demangling symbol names.
350 if {![binutils_assemble $srcdir/$subdir/mangled.s tmpdir/mangled.o]} then {
351 unsupported "readelf -s -C bintest (failed to assemble)"
354 if ![is_remote host] {
355 set tempfile tmpdir/mangled.o
357 set tempfile [remote_download host tmpdir/mangled.o]
361 readelf_test {--syms --demangle --wide} $tempfile readelf.demangled
365 readelf_compressed_wa_test
369 # These dump tests require an assembler.
370 if {[which $AS] != 0} then {
371 run_dump_test "pr25543"
372 run_dump_test "retain1a"
373 run_dump_test "retain1b"
374 run_dump_test "readelf-maskos-1a"
375 run_dump_test "readelf-maskos-1b"
376 if {![istarget *-*-hpux*]} then {
377 run_dump_test pr26548
378 if {![binutils_assemble_flags $srcdir/$subdir/pr26548.s tmpdir/pr26548e.o {--defsym ERROR=1}]} then {
379 unsupported "pr26548e (failed to assemble)"
381 if ![is_remote host] {
382 set tempfile tmpdir/pr26548e.o
384 set tempfile [remote_download host tmpdir/pr26548e.o]
386 readelf_test -Wwi $tempfile pr26548e.d
391 # PR 13482 - Check for off-by-one errors when dumping .note sections.
392 if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
393 unsupported "readelf -n version (failed to assemble)"
396 if ![is_remote host] {
397 set tempfile tmpdir/version.o
399 set tempfile [remote_download host tmpdir/version.o]
402 readelf_test -n $tempfile readelf.n
406 # PR 18374 - Check that relocations against the .debug_loc section
407 # do not prevent readelf from displaying all the location lists.
408 if {![binutils_assemble $srcdir/$subdir/pr18374.s tmpdir/pr18374.o]} then {
409 unsupported "readelf --debug-dump=loc pr18374 (failed to assemble)"
412 if ![is_remote host] {
413 set tempfile tmpdir/pr18374.o
415 set tempfile [remote_download host tmpdir/pr18374.o]
418 readelf_test --debug-dump=loc $tempfile readelf.pr18374
422 # locview - Check dumping of location lists with location views.
423 if {![binutils_assemble $srcdir/$subdir/locview-1.s tmpdir/locview-1.o]} then {
424 unsupported "readelf --debug-dump=loc locview-1 (failed to assemble)"
427 if ![is_remote host] {
428 set tempfile tmpdir/locview-1.o
430 set tempfile [remote_download host tmpdir/locview-1.o]
433 readelf_test --debug-dump=loc $tempfile readelf.locview-1
435 if {![binutils_assemble $srcdir/$subdir/locview-2.s tmpdir/locview-2.o]} then {
436 unsupported "readelf --debug-dump=loc locview-2 (failed to assemble)"
439 if ![is_remote host] {
440 set tempfile tmpdir/locview-2.o
442 set tempfile [remote_download host tmpdir/locview-2.o]
445 readelf_test --debug-dump=loc $tempfile readelf.locview-2
449 # Check that decompressed dumps work.
450 if {![binutils_assemble $srcdir/$subdir/z.s tmpdir/z.o]} then {
451 unsupported "readelf --decompress --hex-dump .debug_loc z (failed to assemble)"
454 if ![is_remote host] {
455 set tempfile tmpdir/z.o
457 set tempfile [remote_download host tmpdir/z.o]
460 readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z
463 # Skip the next test for the RISCV architectures because they
464 # do not support .ULEB128 pseudo-ops with non-constant values.
465 if ![istarget "riscv*-*-*"] then {
468 if [istarget "hppa*64*-*-hpux*"] {
469 set hpux "--defsym HPUX=1"
472 # Assemble the DWARF-5 test file.
473 if {![binutils_assemble_flags $srcdir/$subdir/dw5.S tmpdir/dw5.o $hpux]} then {
474 unsupported "readelf -wiaoRlL dw5 (failed to assemble)"
478 if ![is_remote host] {
479 set tempfile tmpdir/dw5.o
481 set tempfile [remote_download host tmpdir/dw5.o]
484 # First, determine the size, so specific output matchers can be used.
485 readelf_find_size $tempfile 2
487 # Make sure that readelf can decode the contents.
488 readelf_test -wiaoRlL $tempfile dw5.W
492 # Assemble the DWARF-5 attributes test file.
493 if {![binutils_assemble_flags $srcdir/$subdir/dwarf-attributes.S tmpdir/dwarf-attributes.o ""]} then {
494 unsupported "readelf -wi dwarf-attributes (failed to assemble)"
497 if ![is_remote host] {
498 set tempfile tmpdir/dwarf-attributes.o
500 set tempfile [remote_download host tmpdir/dwarf-attributes.o]
503 # First, determine the size, so specific output matchers can be used.
504 readelf_find_size $tempfile 3
506 # Make sure that readelf can decode the contents.
507 readelf_test -wi $tempfile dwarf-attributes.W
510 # Check that debug link sections can be dumped.
511 if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
512 unsupported "readelf --debug-dump=links (failed to assemble debuglink.s)"
514 if ![is_remote host] {
515 set tempfile tmpdir/debuglink.o
517 set tempfile [remote_download host tmpdir/debuglink.o]
520 readelf_test {--debug-dump=links -wN} $tempfile readelf.k
522 # Check that debug link sections can be followed.
523 if {![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug]} then {
524 unsupported "readelf --debug-dump=follow-links (failed to assemble linkdebug.s)"
526 if [is_remote host] {
527 set tempfile2 [remote_download host tmpdir/linkdebug.debug]
530 readelf_test "-wKis -P" $tempfile readelf.wKis
534 if {![binutils_assemble $srcdir/$subdir/dwo.s tmpdir/dwo.o]} then {
535 unsupported "readelf --debug-dump=links (failed to assemble dwo.s)"
537 if ![is_remote host] {
538 set tempfile tmpdir/dwo.o
540 set tempfile [remote_download host tmpdir/dwo.o]
543 readelf_test {--debug-dump=links --debug-dump=no-follow-links} $tempfile readelf.k2
546 if {![binutils_assemble $srcdir/$subdir/zero-sec.s tmpdir/zero-sec.o]} then {
547 unsupported "readelf --enable-checks (failed to assemble zero-sec.s)"
549 if ![is_remote host] {
550 set tempfile tmpdir/zero-sec.o
552 set tempfile [remote_download host tmpdir/zero-sec.o]
555 readelf_test {--enable-checks --sections --wide} $tempfile zero-sec.r
558 if ![is_remote host] {
559 set test $srcdir/$subdir/pr26112.o.bz2
560 # We need to strip the ".bz2", but can leave the dirname.
561 set t $subdir/[file tail $test]
562 set testname [file rootname $t]
564 set tempfile tmpdir/pr26112.o
565 if {[catch "system \"bzip2 -dc $test > $tempfile\""] != 0} {
566 untested "bzip2 -dc ($testname)"
568 readelf_test {--debug-dump=macro -wN} $tempfile pr26112.r