1 # Copyright (C) 1993-2019 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 # This file was written by Rob Savoye <rob@cygnus.com>
21 # and rewritten by Ian Lance Taylor <ian@cygnus.com>
23 if ![is_remote host] {
24 if {[which $OBJDUMP] == 0} then {
25 perror "$OBJDUMP does not exist"
30 send_user "Version [binutil_version $OBJDUMP]"
32 # Simple test of objdump -i
34 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -i"]
36 set cpus_expected [list]
37 lappend cpus_expected aarch64 alpha am33-2 arc ARC700 ARCv2 arm cris
38 lappend cpus_expected d10v d30v fr30 fr500 fr550 h8 hppa i386 iamcu ip2022
39 lappend cpus_expected m16c m32c m32r m68hc11 m68hc12 m68k MCore mep c5 h1 MicroBlaze
40 lappend cpus_expected mips mn10200 mn10300 ms1 msp MSP430 nds32 n1h_v3 ns32k
41 lappend cpus_expected or1k or1knd pj powerpc pyramid riscv romp rs6000 s390 sh sparc
42 lappend cpus_expected tic54x tic80 tilegx tms320c30 tms320c4x tms320c54x
43 lappend cpus_expected v850 vax x86-64 xscale xtensa z8k z8001 z8002
45 # Make sure the target CPU shows up in the list.
46 lappend cpus_expected ${target_cpu}
49 set cpus_regex "([join $cpus_expected | ])"
51 verbose -log "CPU regex: $cpus_regex"
53 set want "BFD header file version.*srec\[^\n\]*\n\[^\n\]*header \[^\n\]*endian\[^\n\]*, data \[^\n\]*endian.*$cpus_regex"
55 if [regexp $want $got] then {
61 # The remaining tests require a test file.
64 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
65 fail "objdump (assembling bintest.s)"
68 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest2.o]} then {
69 fail "objdump (assembling)"
73 set testfile [remote_download host tmpdir/bintest.o]
74 set testfile2 [remote_download host tmpdir/bintest2.o]
76 set testfile tmpdir/bintest.o
77 set testfile2 tmpdir/bintest2.o
80 # $testarchive exists only if it is supported.
81 set testarchive tmpdir/bintest.a
82 remote_file host file delete $testarchive
83 if { ![istarget "alpha-*-*"] || [is_elf_format] } then {
84 set got [binutils_run $AR "rc tmpdir/bintest.a $testfile2"]
85 if ![string match "" $got] then {
87 remote_file host delete tmpdir/bintest.a
88 } elseif [is_remote host] {
89 set testarchive [remote_download host tmpdir/bintest.a]
91 remote_file host delete tmpdir/bintest2.o
96 proc test_objdump_f { testfile dumpfile } {
101 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $testfile"]
103 set want "$dumpfile:\[ \]*file format.*architecture:\[ \]*${cpus_regex}.*HAS_RELOC.*HAS_SYMS"
105 if ![regexp $want $got] then {
106 fail "objdump -f ($testfile, $dumpfile)"
108 pass "objdump -f ($testfile, $dumpfile)"
112 test_objdump_f $testfile $testfile
113 if { [ remote_file host exists $testarchive ] } then {
114 test_objdump_f $testarchive bintest2.o
119 proc test_objdump_h { testfile dumpfile } {
123 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $testfile"]
125 set want "$dumpfile:\[ \]*file format.*Sections.*\[0-9\]+\[ \]+\[^ \]*(text|TEXT|P|\\\$CODE\\\$)\[^ \]*\[ \]*(\[0-9a-fA-F\]+).*\[0-9\]+\[ \]+\[^ \]*(\\.data|DATA|D_1)\[^ \]*\[ \]*(\[0-9a-fA-F\]+)"
127 if ![regexp $want $got all text_name text_size data_name data_size] then {
128 fail "objdump -h ($testfile, $dumpfile)"
130 verbose "text name is $text_name size is $text_size"
131 verbose "data name is $data_name size is $data_size"
134 # The [ti]c4x target has the property sizeof(char)=sizeof(long)=1
135 if [istarget *c4x*-*-*] then {
139 # c54x section sizes are in bytes, not octets; adjust accordingly
140 if [istarget *c54x*-*-*] then {
144 if {[expr "0x$text_size"] < $ets || [expr "0x$data_size"] < $eds} then {
145 send_log "sizes too small\n"
146 fail "objdump -h ($testfile, $dumpfile)"
148 pass "objdump -h ($testfile, $dumpfile)"
153 test_objdump_h $testfile $testfile
154 if { [ remote_file host exists $testarchive ] } then {
155 test_objdump_h $testarchive bintest2.o
160 proc test_objdump_t { testfile} {
164 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $testfile"]
166 if [info exists vars] then { unset vars }
167 while {[regexp "(\[a-z\]*_symbol)(.*)" $got all symbol rest]} {
172 if {![info exists vars(text_symbol)] \
173 || ![info exists vars(data_symbol)] \
174 || ![info exists vars(common_symbol)] \
175 || ![info exists vars(external_symbol)]} then {
176 fail "objdump -t ($testfile)"
178 pass "objdump -t ($testfile)"
182 test_objdump_t $testfile
183 if { [ remote_file host exists $testarchive ] } then {
184 test_objdump_t $testarchive
189 proc test_objdump_r { testfile dumpfile } {
193 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -r $testfile"]
195 set want "$dumpfile:\[ \]*file format.*RELOCATION RECORDS FOR \\\[\[^\]\]*(text|TEXT|P|\\\$CODE\\\$)\[^\]\]*\\\].*external_symbol"
197 if [regexp $want $got] then {
198 pass "objdump -r ($testfile, $dumpfile)"
200 fail "objdump -r ($testfile, $dumpfile)"
204 test_objdump_r $testfile $testfile
205 if { [ remote_file host exists $testarchive ] } then {
206 test_objdump_r $testarchive bintest2.o
210 proc test_objdump_d { testfile dumpfile } {
214 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -d $testfile"]
216 set want "$dumpfile:.*Disassembly of section"
217 if ![regexp $want $got] then {
218 fail "objdump -d $testfile: No disassembly title"
222 set want "$dumpfile:.*00+0 <text_symbol>"
223 if ![regexp $want $got] then {
224 fail "objdump -d $testfile: Missing symbol name and address"
228 set want "$dumpfile:.*00+. <text_symbol2>"
229 if ![regexp $want $got] then {
230 fail "objdump -d $testfile: Missing second symbol"
234 set want "$dumpfile:.*00+. <text_symbol3>"
235 if ![regexp $want $got] then {
236 fail "objdump -d $testfile: Missing third symbol"
240 pass "objdump -d $testfile"
243 test_objdump_d $testfile $testfile
244 if { [ remote_file host exists $testarchive ] } then {
245 test_objdump_d $testarchive bintest2.o
248 # Test objdump --disassemble=<symbol>
249 proc test_objdump_d_sym { testfile dumpfile } {
253 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS --disassemble=text_symbol2 $testfile"]
255 set want "$dumpfile:.*Disassembly of section"
256 if ![regexp $want $got] then {
257 fail "objdump --disassemble=text_symbol2 $testfile: No disassembly title"
261 set want "$dumpfile:.*00+0 <text_symbol>"
262 if [regexp $want $got] then {
263 fail "objdump --disassemble=text_symbol2 $testfile: First symbol displayed, when it should be absent"
267 set want "$dumpfile:.*00+. <text_symbol2>"
268 if ![regexp $want $got] then {
269 fail "objdump --disassemble=text_symbol2 $testfile: Missing second symbol"
273 set want "$dumpfile:.*00+. <text_symbol3>"
274 if [regexp $want $got] then {
275 fail "objdump --disassemble=text_symbol2 $testfile: Third symbol displayed when it should be absent"
279 pass "objdump --disassemble=text_symbol2 $testfile"
282 test_objdump_d_sym $testfile $testfile
284 proc test_objdump_d_func_sym { testfile dumpfile } {
288 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS --disassemble=func --disassemble-zeroes $testfile"]
290 set want "$dumpfile:.*Disassembly of section"
291 if ![regexp $want $got] then {
292 fail "objdump --disassemble=func $testfile: No disassembly title"
296 set want "$dumpfile:.*00+0 <start_of_text>"
297 if [regexp $want $got] then {
298 fail "objdump --disassemble=func $testfile: First symbol displayed, when it should be absent"
302 set want "$dumpfile:.*00+. <func>"
303 if ![regexp $want $got] then {
304 fail "objdump --disassemble=func $testfile: Disassembly does not start at function symbol"
308 set want "$dumpfile:.*00+. <global_non_func_sym>"
309 if ![regexp $want $got] then {
310 fail "objdump --disassemble=func $testfile: Non function symbol not displayed"
314 set want "$dumpfile:.*00+. <next_func>"
315 if [regexp $want $got] then {
316 fail "objdump --disassemble=func $testfile: Disassembly did not stop at the next function"
320 pass "objdump --disassemble=func $testfile"
323 proc test_objdump_d_non_func_sym { testfile dumpfile } {
327 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS --disassemble=global_non_func_sym $testfile"]
329 set want "$dumpfile:.*Disassembly of section"
330 if ![regexp $want $got] then {
331 fail "objdump --disassemble=non_func $testfile: No disassembly title"
335 set want "$dumpfile:.*00+0 <start_of_text>"
336 if [regexp $want $got] then {
337 fail "objdump --disassemble=non_func $testfile: First symbol displayed, when it should be absent"
341 set want "$dumpfile:.*00+. <global_non_func_sym>"
342 if ![regexp $want $got] then {
343 fail "objdump --disassemble=non_func $testfile: Non function symbol not displayed"
347 set want "$dumpfile:.*00+. <local_non_func_sym>"
348 if [regexp $want $got] then {
349 fail "objdump --disassemble=non_func $testfile: Disassembly did not stop at the next symbol"
353 pass "objdump --disassemble=non_func $testfile"
356 # Extra test for ELF format - check that --disassemble=func disassembles
357 # all of func, and does not stop at the next symbol.
358 if { [is_elf_format] } then {
360 if {![binutils_assemble $srcdir/$subdir/disasm.s tmpdir/disasm.o]} then {
361 fail "objdump --disassemble=func (assembling disasm.s)"
363 if [is_remote host] {
364 set elftestfile [remote_download host tmpdir/disasm.o]
366 set elftestfile tmpdir/disasm.o
369 test_objdump_d_func_sym $elftestfile $elftestfile
370 test_objdump_d_non_func_sym $elftestfile $elftestfile
377 proc test_objdump_s { testfile dumpfile } {
381 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s $testfile"]
383 set want "$dumpfile:\[ \]*file format.*Contents.*(text|TEXT|P|\\\$CODE\\\$)\[^0-9\]*\[ \]*\[0-9a-fA-F\]*\[ \]*(00000001|01000000|00000100).*Contents.*(data|DATA|D_1)\[^0-9\]*\[ \]*\[0-9a-fA-F\]*\[ \]*(00000002|02000000|00000200)"
385 if [regexp $want $got] then {
386 pass "objdump -s ($testfile, $dumpfile)"
388 fail "objdump -s ($testfile, $dumpfile)"
392 test_objdump_s $testfile $testfile
393 if { [ remote_file host exists $testarchive ] } then {
394 test_objdump_s $testarchive bintest2.o
397 # Test objdump -s on a file that contains a compressed .debug section
399 if { ![is_elf_format] } then {
400 unsupported "objdump compressed debug"
401 } elseif { ![binutils_assemble $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o] } then {
402 fail "objdump compressed debug"
404 if [is_remote host] {
405 set compressed_testfile [remote_download host tmpdir/dw2-compressed.o]
407 set compressed_testfile tmpdir/dw2-compressed.o
410 set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -s -j .zdebug_abbrev $compressed_testfile" "" "/dev/null" "objdump.out"]
412 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
413 fail "objdump -s -j .zdebug_abbrev (reason: unexpected output)"
418 if { [regexp_diff objdump.out $srcdir/$subdir/objdump.s] } then {
419 fail "objdump -s -j .zdebug_abbrev"
421 pass "objdump -s -j .zdebug_abbrev"
424 # Test objdump -W on a file that contains some compressed .debug sections
426 set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -W $compressed_testfile" "" "/dev/null" "objdump.out"]
428 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
429 fail "objdump -W (reason: unexpected output)"
434 if { [regexp_diff objdump.out $srcdir/$subdir/objdump.W] } then {
441 # Test objdump -WL on a file that contains line information for multiple files and search directories.
442 # Not supported on mcore and moxie targets because they do not (yet) support the generation
443 # of DWARF2 line debug information.
445 if { ![is_elf_format]
446 || [istarget "hppa64*-*-hpux*"]
447 || [istarget "ia64*-*-*"]
448 || [istarget "mcore-*-*"]
449 || [istarget "moxie-*-*"]
451 unsupported "objump decode line"
453 if { [istarget "or1k*-*-*"] } then {
454 set decodedline_testsrc $srcdir/$subdir/dw2-decodedline-1.S
456 set decodedline_testsrc $srcdir/$subdir/dw2-decodedline.S
458 if { ![binutils_assemble $decodedline_testsrc tmpdir/dw2-decodedline.o] } then {
459 fail "objdump decoded line"
462 if [is_remote host] {
463 set decodedline_testfile [remote_download host tmpdir/dw2-decodedline.o]
465 set decodedline_testfile tmpdir/dw2-decodedline.o
468 set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -WL $decodedline_testfile" "" "/dev/null" "objdump.out"]
470 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
471 fail "objdump -WL (reason: unexpected output)"
476 if { [regexp_diff objdump.out $srcdir/$subdir/objdump.WL] } then {
483 # Test objdump -W on a file containing debug_ranges information.
485 if { ![is_elf_format] } then {
486 unsupported "objdump debug_ranges test"
487 } elseif { ![binutils_assemble $srcdir/$subdir/dw2-ranges.S tmpdir/dw2-ranges.o] } then {
488 fail "objdump debug_ranges test"
490 if [is_remote host] {
491 set ranges_testfile [remote_download host tmpdir/dw2-ranges.o]
493 set ranges_testfile tmpdir/dw2-ranges.o
496 set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS --dwarf=Ranges $ranges_testfile" "" "/dev/null" "objdump.out"]
498 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
499 fail "objdump -W for debug_ranges (reason: unexpected output)"
504 setup_xfail "msp430-*-*" "nds32*-*-*"
505 if { [regexp_diff objdump.out $srcdir/$subdir/dw2-ranges.W] } then {
506 fail "objdump -W for debug_ranges"
508 pass "objdump -W for debug_ranges"
512 proc test_build_id_debuglink {} {
519 global CFLAGS_FOR_TARGET
521 set test "build-id-debuglink"
523 # Use a fixed build-id.
524 if { [info exists CFLAGS_FOR_TARGET] } {
525 set save_CFLAGS_FOR_TARGET $CFLAGS_FOR_TARGET
527 set CFLAGS_FOR_TARGET "-g -Wl,--build-id=0x12345678abcdef01"
529 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog executable debug] != "" } {
530 unsupported "$test (build)"
534 if { [info exists save_CFLAGS_FOR_TARGET] } {
535 set CFLAGS_FOR_TARGET $save_CFLAGS_FOR_TARGET
537 unset CFLAGS_FOR_TARGET
540 if { [binutils_run $STRIP "--strip-debug --remove-section=.comment tmpdir/testprog -o tmpdir/testprog.strip"] != "" } {
541 fail "$test (strip debug info)"
545 if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } {
546 fail "$test (create separate debug info file)"
550 set got [remote_exec host "mkdir -p .build-id/12" ]
551 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
552 fail "$test (make debug directory)"
556 set got [remote_exec host "cp tmpdir/testprog.debug .build-id/12/345678abcdef01.debug"]
557 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
558 fail "$test (copy debug info into debug directory)"
562 set got [remote_exec host "$OBJDUMP -Sl tmpdir/testprog.strip" "" "/dev/null" "tmpdir/testprog.strip.dump"]
563 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
564 fail "$test (post strip dump)"
568 set src2 tmpdir/testprog.strip.dump
569 verbose " grep -e testprog.c ${src2}"
570 set status [remote_exec build grep "-e testprog.c ${src2}"]
571 set exec_output [lindex $status 1]
572 set exec_output [prune_warnings $exec_output]
573 if [string match "" $exec_output] then {
574 send_log "$exec_output\n"
575 verbose "$exec_output" 1
576 fail "$test (grepping for source file name in disassembly output)"
580 set got [remote_exec host "rm .build-id/12/345678abcdef01.debug"]
581 set got [remote_exec host "rmdir -p .build-id/12" ]
582 set got [remote_exec host "rm tmpdir/testprog.strip.dump"]
583 set got [remote_exec host "rm tmpdir/testprog.debug"]
584 set got [remote_exec host "rm tmpdir/testprog.strip"]
588 if {[is_elf_format]} then {
589 test_build_id_debuglink
592 # Test objdump -Wk on a file containing debug links.
594 if { [is_elf_format] } then {
595 set testsrc $srcdir/$subdir/debuglink.s
597 if { ![binutils_assemble $testsrc tmpdir/debuglink.o] } then {
598 fail "objdump -Wk (reason: could not assemble source)"
601 if [is_remote host] {
602 set testfile [remote_download host tmpdir/debuglink.o]
604 set testfile tmpdir/debuglink.o
607 set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -Wk $testfile" "" "/dev/null" "objdump.out"]
609 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
610 fail "objdump -Wk (reason: unexpected output)"
615 if { [regexp_diff objdump.out $srcdir/$subdir/objdump.Wk] } then {
623 # Very similar to proc test_build_id_debuglink except this time we
624 # display some of the contents of the separate debug info file.
626 proc test_follow_debuglink { options dumpfile } {
631 set test "follow-debuglink ($options)"
633 if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
634 fail "$test (reason: assemble first source file)"
638 if {![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug]} then {
639 fail "$test (reason: assemble second source file)"
643 if ![is_remote host] {
644 set tempfile tmpdir/debuglink.o
646 set tempfile [remote_download host tmpdir/linkdebug.debug]
647 set tempfile [remote_download host tmpdir/debuglink.o]
650 set got [remote_exec host "$OBJDUMP $options $tempfile" "" "/dev/null" "tmpdir/objdump.out"]
651 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
652 fail "$test (reason: unexpected error output from objdump)"
656 if { [regexp_diff tmpdir/objdump.out $srcdir/$subdir/$dumpfile] } then {
658 verbose "output is \n[file_contents objdump.out]" 2
665 set got [remote_exec host "rm tmpdir/objdump.out"]
666 set got [remote_exec host "rm $tempfile"]
667 set got [remote_exec host "rm tmpdir/linkdebug.debug"]
670 if {[is_elf_format]} then {
671 test_follow_debuglink "--dwarf=follow-links --dwarf=info --dwarf=str" objdump.WK2
672 test_follow_debuglink "--dwarf=follow-links --headers --wide" objdump.WK3
675 # Test objdump output with start and stop address limits for the specified
678 proc test_objdump_limited { testfile dopt want start stop } {
682 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS $dopt --start-address 0x$start --stop-address 0x$stop $testfile"]
684 if [regexp $want $got] then {
685 pass "objdump $dopt --start-address 0x$start --stop-address 0x$stop ($testfile)"
687 fail "objdump $dopt --start-address 0x$start --stop-address 0x$stop ($testfile)"
691 # Test objdump -d --start-address M --stop-address N
693 proc test_objdump_disas_limited { testfile start stop } {
694 set want "$testfile:\[ \]*file format.*Disassembly of section .text:\n.*\[ \]*$start:.*"
695 test_objdump_limited $testfile -d $want $start $stop
698 # Test objdump -s --start-address M --stop-address N
700 proc test_objdump_content_limited { testfile start stop } {
701 set want "$testfile:\[ \]*file format.*Contents of section .text:\n\[ \]*$start .*"
702 test_objdump_limited $testfile -s $want $start $stop
705 # Test objdump with --start-address and --stop-address options for higher
706 # address ranges which may be sign-extended on targets that treat addresses
707 # as signed. We only check that objdump produces some dump output at the
708 # specified start address as a proxy for correct enforcement of the
711 if {[is_elf_format]} then {
712 # generate a copy of the test object with .text repositioned
713 if { [binutils_run $OBJCOPY "--change-section-address .text=0x80000000 tmpdir/bintest.o tmpdir/bintest_signed.o"] != "" } {
714 fail "Failed to reposition .text to 0x80000000 (tmpdir/bintest.o -> tmpdir/bintest_signed.o)"
718 if [is_remote host] {
719 set testfile3 [remote_download host tmpdir/bintest_signed.o]
721 set testfile3 tmpdir/bintest_signed.o
724 test_objdump_content_limited $testfile3 "80000004" "80000008"
725 test_objdump_disas_limited $testfile3 "80000004" "80000008"
726 remote_file host delete $testfile3
729 # Options which are not tested: -a -D -R -T -x -l --stabs
730 # I don't see any generic way to test any of these other than -a.
731 # Tests could be written for specific targets, and that should be done
732 # if specific problems are found.