1#   Copyright (C) 1993-2017 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# This file was written by Rob Savoye <rob@cygnus.com>
21# and rewritten by Ian Lance Taylor <ian@cygnus.com>
22
23if ![is_remote host] {
24    if {[which $OBJDUMP] == 0} then {
25	perror "$OBJDUMP does not exist"
26	return
27    }
28}
29
30send_user "Version [binutil_version $OBJDUMP]"
31
32# Simple test of objdump -i
33
34set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -i"]
35
36set cpus_expected [list]
37lappend cpus_expected aarch64 alpha arc ARC700 ARCv2 arm cris
38lappend cpus_expected d10v d30v fr30 fr500 fr550 h8 hppa i386 i860 i960 iamcu ip2022
39lappend cpus_expected m16c m32c m32r m68hc11 m68hc12 m68k m88k MCore mep c5 h1 MicroBlaze
40lappend cpus_expected mips mn10200 mn10300 ms1 msp MSP430 nds32 n1h_v3 ns32k
41lappend cpus_expected or1k or1knd pj powerpc pyramid riscv romp rs6000 s390 sh sparc
42lappend cpus_expected tahoe tic54x tic80 tilegx tms320c30 tms320c4x tms320c54x
43lappend cpus_expected v850 vax we32k x86-64 xscale xtensa z8k z8001 z8002
44
45# Make sure the target CPU shows up in the list.
46lappend cpus_expected ${target_cpu}
47
48# Create regexp
49set cpus_regex "([join $cpus_expected | ])"
50
51verbose -log "CPU regex: $cpus_regex"
52
53set want "BFD header file version.*srec\[^\n\]*\n\[^\n\]*header \[^\n\]*endian\[^\n\]*, data \[^\n\]*endian.*$cpus_regex"
54
55if [regexp $want $got] then {
56    pass "objdump -i"
57} else {
58    fail "objdump -i"
59}
60
61# The remaining tests require a test file.
62
63
64if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
65    return
66}
67if [is_remote host] {
68    set testfile [remote_download host tmpdir/bintest.o]
69} else {
70    set testfile tmpdir/bintest.o
71}
72
73# Test objdump -f
74
75set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $testfile"]
76
77set want "$testfile:\[ 	\]*file format.*architecture:\[ 	\]*${cpus_regex}.*HAS_RELOC.*HAS_SYMS"
78
79if ![regexp $want $got] then {
80    fail "objdump -f"
81} else {
82    pass "objdump -f"
83}
84
85# Test objdump -h
86
87set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $testfile"]
88
89set want "$testfile:\[ 	\]*file format.*Sections.*\[0-9\]+\[ 	\]+\[^ 	\]*(text|TEXT|P|\\\$CODE\\\$)\[^ 	\]*\[ 	\]*(\[0-9a-fA-F\]+).*\[0-9\]+\[ 	\]+\[^ 	\]*(\\.data|DATA|D_1)\[^ 	\]*\[ 	\]*(\[0-9a-fA-F\]+)"
90
91if ![regexp $want $got all text_name text_size data_name data_size] then {
92    fail "objdump -h"
93} else {
94    verbose "text name is $text_name size is $text_size"
95    verbose "data name is $data_name size is $data_size"
96    set ets 8
97    set eds 4
98    # The [ti]c4x target has the property sizeof(char)=sizeof(long)=1
99    if [istarget *c4x*-*-*] then {
100        set ets 2
101        set eds 1
102    }
103    # c54x section sizes are in bytes, not octets; adjust accordingly
104    if [istarget *c54x*-*-*] then {
105	set ets 4
106	set eds 2
107    }
108    if {[expr "0x$text_size"] < $ets || [expr "0x$data_size"] < $eds} then {
109	send_log "sizes too small\n"
110	fail "objdump -h"
111    } else {
112	pass "objdump -h"
113    }
114}
115
116# Test objdump -t
117
118set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $testfile"]
119
120if [info exists vars] then { unset vars }
121while {[regexp "(\[a-z\]*_symbol)(.*)" $got all symbol rest]} {
122    set vars($symbol) 1
123    set got $rest
124}
125
126if {![info exists vars(text_symbol)] \
127     || ![info exists vars(data_symbol)] \
128     || ![info exists vars(common_symbol)] \
129     || ![info exists vars(external_symbol)]} then {
130    fail "objdump -t"
131} else {
132    pass "objdump -t"
133}
134
135# Test objdump -r
136
137set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -r $testfile"]
138
139set want "$testfile:\[ 	\]*file format.*RELOCATION RECORDS FOR \\\[\[^\]\]*(text|TEXT|P|\\\$CODE\\\$)\[^\]\]*\\\].*external_symbol"
140
141if [regexp $want $got] then {
142    pass "objdump -r"
143} else {
144    fail "objdump -r"
145}
146
147# Test objdump -s
148
149set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s $testfile"]
150
151set want "$testfile:\[ 	\]*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)"
152
153if [regexp $want $got] then {
154    pass "objdump -s"
155} else {
156    fail "objdump -s"
157}
158
159# Test objdump -s on a file that contains a compressed .debug section
160
161if { ![is_elf_format] } then {
162    unsupported "objdump compressed debug"
163} elseif { ![binutils_assemble $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o] } then {
164    fail "objdump compressed debug"
165} else {
166    if [is_remote host] {
167	set compressed_testfile [remote_download host tmpdir/dw2-compressed.o]
168    } else {
169	set compressed_testfile tmpdir/dw2-compressed.o
170    }
171
172    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -s -j .zdebug_abbrev $compressed_testfile" "" "/dev/null" "objdump.out"]
173
174    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
175	fail "objdump -s -j .zdebug_abbrev (reason: unexpected output)"
176	send_log $got
177	send_log "\n"
178    }
179
180    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.s] } then {
181	fail "objdump -s -j .zdebug_abbrev"
182    } else {
183	pass "objdump -s -j .zdebug_abbrev"
184    }
185
186    # Test objdump -W on a file that contains some compressed .debug sections
187
188    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -W $compressed_testfile" "" "/dev/null" "objdump.out"]
189
190    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
191	fail "objdump -W (reason: unexpected output)"
192	send_log $got
193	send_log "\n"
194    }
195
196    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.W] } then {
197	fail "objdump -W"
198    } else {
199	pass "objdump -W"
200    }
201}
202
203# Test objdump -WL on a file that contains line information for multiple files and search directories.
204# Not supported on mcore and moxie targets because they do not (yet) support the generation
205# of DWARF2 line debug information.
206
207if { ![is_elf_format]
208     || [istarget "hppa64*-*-hpux*"]
209     || [istarget "i370-*-*"]
210     || [istarget "i960-*-*"]
211     || [istarget "ia64*-*-*"]
212     || [istarget "mcore-*-*"]
213     || [istarget "moxie-*-*"]
214} then {
215    unsupported "objump decode line"
216} else {
217    if { [istarget "or1k*-*-*"] } then {
218        set decodedline_testsrc $srcdir/$subdir/dw2-decodedline-1.S
219    } else {
220        set decodedline_testsrc $srcdir/$subdir/dw2-decodedline.S
221    }
222    if { ![binutils_assemble $decodedline_testsrc tmpdir/dw2-decodedline.o] } then {
223	fail "objdump decoded line"
224    }
225
226    if [is_remote host] {
227	set decodedline_testfile [remote_download host tmpdir/dw2-decodedline.o]
228    } else {
229	set decodedline_testfile tmpdir/dw2-decodedline.o
230    }
231
232    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -WL $decodedline_testfile" "" "/dev/null" "objdump.out"]
233
234    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
235	fail "objdump -WL (reason: unexpected output)"
236	send_log $got
237	send_log "\n"
238    }
239
240    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.WL] } then {
241	fail "objdump -WL"
242    } else {
243	pass "objdump -WL"
244    }
245}
246
247# Test objdump -W on a file containing debug_ranges information.
248
249if { ![is_elf_format] } then {
250    unsupported "objdump debug_ranges test"
251} elseif { ![binutils_assemble $srcdir/$subdir/dw2-ranges.S tmpdir/dw2-ranges.o] } then {
252    fail "objdump debug_ranges test"
253} else {
254    if [is_remote host] {
255	set ranges_testfile [remote_download host tmpdir/dw2-ranges.o]
256    } else {
257	set ranges_testfile tmpdir/dw2-ranges.o
258    }
259
260    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS --dwarf=Ranges $ranges_testfile" "" "/dev/null" "objdump.out"]
261
262    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
263	fail "objdump -W for debug_ranges (reason: unexpected output)"
264	send_log $got
265	send_log "\n"
266    }
267
268    setup_xfail "msp430-*-*" "nds32*-*-*" "riscv*-*-*"
269    if { [regexp_diff objdump.out $srcdir/$subdir/dw2-ranges.W] } then {
270	fail "objdump -W for debug_ranges"
271    } else {
272	pass "objdump -W for debug_ranges"
273    }
274}
275
276proc test_build_id_debuglink {} {
277    global srcdir
278    global subdir
279    global env
280    global CC_FOR_TARGET
281    global STRIP
282    global OBJCOPY
283    global OBJDUMP
284    global CFLAGS_FOR_TARGET
285
286    set test "build-id-debuglink"
287    if {![info exists CC_FOR_TARGET]} {
288	set CC_FOR_TARGET $env(CC)
289    }
290    if { $CC_FOR_TARGET == "" } {
291	unsupported $test
292	return
293    }
294
295    # Use a fixed build-id.
296    set CFLAGS_FOR_TARGET "-g -Wl,--build-id=0x12345678abcdef01"
297
298    if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog exectuable debug] != "" } {
299	fail "$test (build)"
300	return
301    }
302
303    # FIXME: Do we need to restore CFLAGS_FOR_TARGET to its old value ?
304
305    if { [binutils_run $STRIP "--strip-debug --remove-section=.comment tmpdir/testprog -o tmpdir/testprog.strip"] != "" } {
306	fail "$test (strip debug info)"
307	return
308    }
309
310    if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } {
311	fail "$test (create separate debug info file)"
312	return
313    }
314
315    set got [remote_exec host "mkdir -p .build-id/12" ]
316    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
317	fail "$test (make debug directory)"
318	return
319    }
320
321    set got [remote_exec host "cp tmpdir/testprog.debug .build-id/12/345678abcdef01.debug"]
322    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
323	fail "$test (copy debug info into debug directory)"
324	return
325    }
326
327    set got [remote_exec host "$OBJDUMP -Sl tmpdir/testprog.strip" "" "/dev/null" "tmpdir/testprog.strip.dump"]
328    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
329	fail "$test (post strip dump)"
330	return
331    }
332
333    set src2 tmpdir/testprog.strip.dump
334    verbose " grep -e testprog.c ${src2}"
335    set status [remote_exec build grep "-e testprog.c ${src2}"]
336    set exec_output [lindex $status 1]
337    set exec_output [prune_warnings $exec_output]
338    if [string match "" $exec_output] then {
339	send_log "$exec_output\n"
340	verbose "$exec_output" 1
341	fail "$test (grepping for source file name in disassembly output)"
342    } else {
343	pass "$test"
344	# Cleanup...
345	set got [remote_exec host "rm .build-id/12/345678abcdef01.debug"]
346	set got [remote_exec host "rmdir -p .build-id/12" ]
347	set got [remote_exec host "rm tmpdir/testprog.strip.dump"]
348	set got [remote_exec host "rm tmpdir/testprog.debug"]
349	set got [remote_exec host "rm tmpdir/testprog.strip"]
350    }
351}
352
353if {[isnative] && [is_elf_format]} then {
354    test_build_id_debuglink
355}
356
357# Options which are not tested: -a -d -D -R -T -x -l --stabs
358# I don't see any generic way to test any of these other than -a.
359# Tests could be written for specific targets, and that should be done
360# if specific problems are found.
361