Add bfd_get_file_size to get archive element size
[external/binutils.git] / binutils / testsuite / binutils-all / objdump.exp
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
23 if ![is_remote host] {
24     if {[which $OBJDUMP] == 0} then {
25         perror "$OBJDUMP does not exist"
26         return
27     }
28 }
29
30 send_user "Version [binutil_version $OBJDUMP]"
31
32 # Simple test of objdump -i
33
34 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -i"]
35
36 set cpus_expected [list]
37 lappend cpus_expected aarch64 alpha arc ARC700 ARCv2 arm cris
38 lappend cpus_expected d10v d30v fr30 fr500 fr550 h8 hppa i386 i860 i960 iamcu ip2022
39 lappend cpus_expected m16c m32c m32r m68hc11 m68hc12 m68k m88k 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 tahoe tic54x tic80 tilegx tms320c30 tms320c4x tms320c54x
43 lappend cpus_expected v850 vax we32k x86-64 xscale xtensa z8k z8001 z8002
44
45 # Make sure the target CPU shows up in the list.
46 lappend cpus_expected ${target_cpu}
47
48 # Create regexp
49 set cpus_regex "([join $cpus_expected | ])"
50
51 verbose -log "CPU regex: $cpus_regex"
52
53 set want "BFD header file version.*srec\[^\n\]*\n\[^\n\]*header \[^\n\]*endian\[^\n\]*, data \[^\n\]*endian.*$cpus_regex"
54
55 if [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
64 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
65     return
66 }
67 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest2.o]} then {
68     return
69 }
70 if [is_remote host] {
71     set testfile [remote_download host tmpdir/bintest.o]
72     set testfile2 [remote_download host tmpdir/bintest2.o]
73 } else {
74     set testfile tmpdir/bintest.o
75     set testfile2 tmpdir/bintest2.o
76 }
77
78 if { ![istarget "alpha-*-*"] || [is_elf_format] } then {
79     remote_file host file delete tmpdir/bintest.a
80     set got [binutils_run $AR "rc tmpdir/bintest.a $testfile2"]
81     if ![string match "" $got] then {
82         fail "bintest.a"
83         remote_file host delete tmpdir/bintest.a
84     } else {
85         if [is_remote host] {
86             set testarchive [remote_download host tmpdir/bintest.a]
87         } else {
88             set testarchive tmpdir/bintest.a
89         }
90     }
91     remote_file host delete tmpdir/bintest2.o
92 }
93
94 # Test objdump -f
95
96 proc test_objdump_f { testfile dumpfile } {
97     global OBJDUMP
98     global OBJDUMPFLAGS
99     global cpus_regex
100
101     set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $testfile"]
102
103     set want "$dumpfile:\[      \]*file format.*architecture:\[         \]*${cpus_regex}.*HAS_RELOC.*HAS_SYMS"
104
105     if ![regexp $want $got] then {
106         fail "objdump -f ($testfile, $dumpfile)"
107     } else {
108         pass "objdump -f ($testfile, $dumpfile)"
109     }
110 }
111
112 test_objdump_f $testfile $testfile
113 if { [ remote_file host exists $testarchive ] } then {
114     test_objdump_f $testarchive bintest2.o
115 }
116
117 # Test objdump -h
118
119 proc test_objdump_h { testfile dumpfile } {
120     global OBJDUMP
121     global OBJDUMPFLAGS
122
123     set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $testfile"]
124
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\]+)"
126
127     if ![regexp $want $got all text_name text_size data_name data_size] then {
128         fail "objdump -h ($testfile, $dumpfile)"
129     } else {
130         verbose "text name is $text_name size is $text_size"
131         verbose "data name is $data_name size is $data_size"
132         set ets 8
133         set eds 4
134         # The [ti]c4x target has the property sizeof(char)=sizeof(long)=1
135         if [istarget *c4x*-*-*] then {
136             set ets 2
137             set eds 1
138         }
139         # c54x section sizes are in bytes, not octets; adjust accordingly
140         if [istarget *c54x*-*-*] then {
141             set ets 4
142             set eds 2
143         }
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)"
147         } else {
148             pass "objdump -h ($testfile, $dumpfile)"
149         }
150     }
151 }
152
153 test_objdump_h $testfile $testfile
154 if { [ remote_file host exists $testarchive ] } then {
155     test_objdump_h $testarchive bintest2.o
156 }
157
158 # Test objdump -t
159
160 proc test_objdump_t { testfile} {
161     global OBJDUMP
162     global OBJDUMPFLAGS
163
164     set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $testfile"]
165
166     if [info exists vars] then { unset vars }
167     while {[regexp "(\[a-z\]*_symbol)(.*)" $got all symbol rest]} {
168         set vars($symbol) 1
169         set got $rest
170     }
171
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)"
177     } else {
178         pass "objdump -t ($testfile)"
179     }
180 }
181
182 test_objdump_t $testfile
183 if { [ remote_file host exists $testarchive ] } then {
184     test_objdump_t $testarchive
185 }
186
187 # Test objdump -r
188
189 proc test_objdump_r { testfile dumpfile } {
190     global OBJDUMP
191     global OBJDUMPFLAGS
192
193     set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -r $testfile"]
194
195     set want "$dumpfile:\[      \]*file format.*RELOCATION RECORDS FOR \\\[\[^\]\]*(text|TEXT|P|\\\$CODE\\\$)\[^\]\]*\\\].*external_symbol"
196
197     if [regexp $want $got] then {
198         pass "objdump -r ($testfile, $dumpfile)"
199     } else {
200         fail "objdump -r ($testfile, $dumpfile)"
201     }
202 }
203
204 test_objdump_r $testfile $testfile
205 if { [ remote_file host exists $testarchive ] } then {
206     test_objdump_r $testarchive bintest2.o
207 }
208
209 # Test objdump -s
210
211 proc test_objdump_s { testfile dumpfile } {
212     global OBJDUMP
213     global OBJDUMPFLAGS
214
215     set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s $testfile"]
216
217     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)"
218
219     if [regexp $want $got] then {
220         pass "objdump -s ($testfile, $dumpfile)"
221     } else {
222         fail "objdump -s ($testfile, $dumpfile)"
223     }
224 }
225
226 test_objdump_s $testfile $testfile
227 if { [ remote_file host exists $testarchive ] } then {
228     test_objdump_s $testarchive bintest2.o
229 }
230
231 # Test objdump -s on a file that contains a compressed .debug section
232
233 if { ![is_elf_format] } then {
234     unsupported "objdump compressed debug"
235 } elseif { ![binutils_assemble $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o] } then {
236     fail "objdump compressed debug"
237 } else {
238     if [is_remote host] {
239         set compressed_testfile [remote_download host tmpdir/dw2-compressed.o]
240     } else {
241         set compressed_testfile tmpdir/dw2-compressed.o
242     }
243
244     set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -s -j .zdebug_abbrev $compressed_testfile" "" "/dev/null" "objdump.out"]
245
246     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
247         fail "objdump -s -j .zdebug_abbrev (reason: unexpected output)"
248         send_log $got
249         send_log "\n"
250     }
251
252     if { [regexp_diff objdump.out $srcdir/$subdir/objdump.s] } then {
253         fail "objdump -s -j .zdebug_abbrev"
254     } else {
255         pass "objdump -s -j .zdebug_abbrev"
256     }
257
258     # Test objdump -W on a file that contains some compressed .debug sections
259
260     set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -W $compressed_testfile" "" "/dev/null" "objdump.out"]
261
262     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
263         fail "objdump -W (reason: unexpected output)"
264         send_log $got
265         send_log "\n"
266     }
267
268     if { [regexp_diff objdump.out $srcdir/$subdir/objdump.W] } then {
269         fail "objdump -W"
270     } else {
271         pass "objdump -W"
272     }
273 }
274
275 # Test objdump -WL on a file that contains line information for multiple files and search directories.
276 # Not supported on mcore and moxie targets because they do not (yet) support the generation
277 # of DWARF2 line debug information.
278
279 if { ![is_elf_format]
280      || [istarget "hppa64*-*-hpux*"]
281      || [istarget "i370-*-*"]
282      || [istarget "i960-*-*"]
283      || [istarget "ia64*-*-*"]
284      || [istarget "mcore-*-*"]
285      || [istarget "moxie-*-*"]
286 } then {
287     unsupported "objump decode line"
288 } else {
289     if { [istarget "or1k*-*-*"] } then {
290         set decodedline_testsrc $srcdir/$subdir/dw2-decodedline-1.S
291     } else {
292         set decodedline_testsrc $srcdir/$subdir/dw2-decodedline.S
293     }
294     if { ![binutils_assemble $decodedline_testsrc tmpdir/dw2-decodedline.o] } then {
295         fail "objdump decoded line"
296     }
297
298     if [is_remote host] {
299         set decodedline_testfile [remote_download host tmpdir/dw2-decodedline.o]
300     } else {
301         set decodedline_testfile tmpdir/dw2-decodedline.o
302     }
303
304     set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -WL $decodedline_testfile" "" "/dev/null" "objdump.out"]
305
306     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
307         fail "objdump -WL (reason: unexpected output)"
308         send_log $got
309         send_log "\n"
310     }
311
312     if { [regexp_diff objdump.out $srcdir/$subdir/objdump.WL] } then {
313         fail "objdump -WL"
314     } else {
315         pass "objdump -WL"
316     }
317 }
318
319 # Test objdump -W on a file containing debug_ranges information.
320
321 if { ![is_elf_format] } then {
322     unsupported "objdump debug_ranges test"
323 } elseif { ![binutils_assemble $srcdir/$subdir/dw2-ranges.S tmpdir/dw2-ranges.o] } then {
324     fail "objdump debug_ranges test"
325 } else {
326     if [is_remote host] {
327         set ranges_testfile [remote_download host tmpdir/dw2-ranges.o]
328     } else {
329         set ranges_testfile tmpdir/dw2-ranges.o
330     }
331
332     set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS --dwarf=Ranges $ranges_testfile" "" "/dev/null" "objdump.out"]
333
334     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
335         fail "objdump -W for debug_ranges (reason: unexpected output)"
336         send_log $got
337         send_log "\n"
338     }
339
340     setup_xfail "msp430-*-*" "nds32*-*-*" "riscv*-*-*"
341     if { [regexp_diff objdump.out $srcdir/$subdir/dw2-ranges.W] } then {
342         fail "objdump -W for debug_ranges"
343     } else {
344         pass "objdump -W for debug_ranges"
345     }
346 }
347
348 proc test_build_id_debuglink {} {
349     global srcdir
350     global subdir
351     global env
352     global CC_FOR_TARGET
353     global STRIP
354     global OBJCOPY
355     global OBJDUMP
356     global CFLAGS_FOR_TARGET
357     
358     set test "build-id-debuglink"
359     if {![info exists CC_FOR_TARGET]} {
360         set CC_FOR_TARGET $env(CC)
361     }
362     if { $CC_FOR_TARGET == "" } {
363         unsupported $test
364         return
365     }
366
367     # Use a fixed build-id.
368     set CFLAGS_FOR_TARGET "-g -Wl,--build-id=0x12345678abcdef01"
369
370     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog exectuable debug] != "" } {
371         fail "$test (build)"
372         return
373     }
374
375     # FIXME: Do we need to restore CFLAGS_FOR_TARGET to its old value ?
376
377     if { [binutils_run $STRIP "--strip-debug --remove-section=.comment tmpdir/testprog -o tmpdir/testprog.strip"] != "" } {
378         fail "$test (strip debug info)"
379         return
380     }
381
382     if { [binutils_run $OBJCOPY "--only-keep-debug tmpdir/testprog tmpdir/testprog.debug"] != "" } {
383         fail "$test (create separate debug info file)"
384         return
385     }
386
387     set got [remote_exec host "mkdir -p .build-id/12" ]
388     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
389         fail "$test (make debug directory)"
390         return
391     }
392
393     set got [remote_exec host "cp tmpdir/testprog.debug .build-id/12/345678abcdef01.debug"]
394     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
395         fail "$test (copy debug info into debug directory)"
396         return
397     }
398     
399     set got [remote_exec host "$OBJDUMP -Sl tmpdir/testprog.strip" "" "/dev/null" "tmpdir/testprog.strip.dump"]
400     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
401         fail "$test (post strip dump)"
402         return
403     }
404
405     set src2 tmpdir/testprog.strip.dump
406     verbose " grep -e testprog.c ${src2}"
407     set status [remote_exec build grep "-e testprog.c ${src2}"]
408     set exec_output [lindex $status 1]
409     set exec_output [prune_warnings $exec_output]
410     if [string match "" $exec_output] then {
411         send_log "$exec_output\n"
412         verbose "$exec_output" 1
413         fail "$test (grepping for source file name in disassembly output)"
414     } else {
415         pass "$test"
416         # Cleanup...
417         set got [remote_exec host "rm .build-id/12/345678abcdef01.debug"]
418         set got [remote_exec host "rmdir -p .build-id/12" ]
419         set got [remote_exec host "rm tmpdir/testprog.strip.dump"]
420         set got [remote_exec host "rm tmpdir/testprog.debug"]
421         set got [remote_exec host "rm tmpdir/testprog.strip"]
422     }
423 }
424
425 if {[isnative] && [is_elf_format]} then {
426     test_build_id_debuglink
427 }
428
429 # Options which are not tested: -a -d -D -R -T -x -l --stabs
430 # I don't see any generic way to test any of these other than -a.
431 # Tests could be written for specific targets, and that should be done
432 # if specific problems are found.