ELF: Don't generate unused section symbols
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
CommitLineData
250d07de 1# Copyright (C) 1999-2021 Free Software Foundation, Inc.
252b5132
RH
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
32866df7 5# the Free Software Foundation; either version 3 of the License, or
252b5132 6# (at your option) any later version.
65951855 7#
252b5132
RH
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.
65951855 12#
252b5132
RH
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
b43b5d5f 15# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-dejagnu@prep.ai.mit.edu
19
113675b7 20# Written by Nick Clifton <nickc@cygnus.com>
252b5132
RH
21# Based on scripts written by Ian Lance Taylor <ian@cygnus.com>
22# and Ken Raeburn <raeburn@cygnus.com>.
23
09c11c86
NC
24# Exclude non-ELF targets.
25if ![is_elf_format] {
26 verbose "$READELF is only intended for ELF targets" 2
27 return
28}
29
252b5132
RH
30# First some helpful procedures, then the tests themselves
31
32# Return the contents of the filename given
33proc file_contents { filename } {
34 set file [open $filename r]
35 set contents [read $file]
36 close $file
37 return $contents
38}
39
9921923c
HPN
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.
a0a8a934 43proc readelf_find_size { binary_file test_iteration } {
9921923c
HPN
44 global READELF
45 global READELFFLAGS
46 global readelf_size
47
48 set readelf_size ""
a0a8a934 49 set testname "finding out ELF size with readelf -h ($test_iteration)"
7f6a71ff
JM
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 }
9921923c 54
7f6a71ff 55 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
9921923c
HPN
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
252b5132
RH
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
9921923c 77# of the regexp_file-readelf_size if it exists, else regexp_file.
252b5132
RH
78
79proc readelf_test { options binary_file regexp_file xfails } {
80
81 global READELF
82 global READELFFLAGS
9921923c 83 global readelf_size
252b5132
RH
84 global srcdir
85 global subdir
65951855 86
11bb0c7a
AM
87 set testname "readelf $options [file rootname [file tail $binary_file]]"
88
9921923c 89 send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
7f6a71ff 90 set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
252b5132 91
9a1c9383
NC
92 foreach xfail $xfails {
93 setup_xfail $xfail
252b5132 94 }
3f7de0e7 95
7f6a71ff 96 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
11bb0c7a 97 fail "$testname (reason: unexpected output)"
252b5132 98 send_log $got
3f7de0e7 99 send_log "\n"
252b5132
RH
100 return
101 }
102
d1bcae83
L
103 if { [is_elf_unused_section_symbols ] } {
104 set target_machine unused
105 } else {
106 set target_machine ""
107 }
ade0b24f 108 if [istarget "mips*-*-*"] then {
9cc0123f 109 if [is_bad_symtab] then {
eed3fa3b 110 set target_machine mips
9cc0123f
AM
111 } else {
112 set target_machine tmips
eed3fa3b 113 }
ade0b24f
NC
114 }
115
116 if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
117 set regexp_file $regexp_file-$readelf_size-$target_machine
118 } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
119 set regexp_file $regexp_file-$target_machine
120 } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
9921923c
HPN
121 set regexp_file $regexp_file-$readelf_size
122 }
123
252b5132 124 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
11bb0c7a 125 fail $testname
252b5132
RH
126 verbose "output is \n[file_contents readelf.out]" 2
127 return
128 }
129
11bb0c7a 130 pass $testname
252b5132
RH
131}
132
cbb356d9
NC
133# Simple proc to skip certain expected warning messages.
134
135proc prune_readelf_wi_warnings { text } {
136 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
137 return $text
138}
139
140# Testing the "readelf -wi" option is difficult because there
141# is no guaranteed order to the output, and because some ports
142# will use indirect string references, whilst others will use
143# direct references. So instead of having an expected output
144# file, like the other readelf tests, we grep for strings that
145# really ought to be there.
146
147proc readelf_wi_test {} {
148 global READELF
149 global READELFFLAGS
150 global srcdir
151 global subdir
65951855 152
cbb356d9
NC
153 # Compile the second test file.
154 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
155 verbose "Unable to compile test file."
156 untested "readelf -wi"
157 return
158 }
159
160 # Download it.
8d263650 161 set tempfile [remote_download host tmpdir/testprog.o]
cbb356d9
NC
162
163 # Run "readelf -wi" on it.
7f6a71ff 164 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
cbb356d9
NC
165
166 # Upload the results.
8d263650 167 set output [remote_upload host readelf.out]
cbb356d9 168
8d263650 169 file_on_host delete $tempfile
65951855 170
cbb356d9 171 # Strip any superflous warnings.
7f6a71ff 172 set got [prune_readelf_wi_warnings [lindex $got 1]]
cbb356d9
NC
173
174 if ![string match "" $got] then {
b279a55c 175 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
cbb356d9
NC
176 send_log $got
177 send_log "\n"
178 return
179 }
180
181 if ![file size $output] then {
182 # If the output file is empty, then this target does not
183 # generate dwarf2 output. This is not a failure.
184 verbose "No output from 'readelf -wi'"
185 untested "readelf -wi"
186 return
187 }
65951855 188
cbb356d9
NC
189 # Search for strings that should be in the output.
190 set sought {
191 ".*DW_TAG_compile_unit.*"
192 ".*DW_TAG_subprogram.*"
193 ".*DW_TAG_base_type.*"
194 ".*DW_AT_producer.*(GNU C|indirect string).*"
02391b8b 195 ".*DW_AT_language.*(ANSI C|C11).*"
cbb356d9
NC
196 ".*DW_AT_name.*(testprog.c|indirect string).*"
197 ".*DW_AT_name.*fn.*"
198 ".*DW_AT_name.*(main|indirect string).*"
aaa222e7 199 ".*\(DW_OP_addr: 0\).*"
cbb356d9 200 }
65951855 201
13761a11
NC
202 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
203 setup_xfail msp430*-*-*
204
65951855 205 foreach looked_for $sought {
cbb356d9
NC
206 set lines [grep $output $looked_for]
207 if ![llength $lines] then {
208 fail "readelf -wi: missing: $looked_for"
209 send_log readelf.out
210 return
211 }
212 }
213
8d263650 214 file_on_host delete $output
65951855 215
cbb356d9
NC
216 # All done.
217 pass "readelf -wi"
218}
252b5132 219
1b315056
CS
220# This tests "readelf -wa", but on a file with a compressed
221# .debug_abbrev section.
222
223proc readelf_compressed_wa_test {} {
224 global READELF
225 global READELFFLAGS
226 global srcdir
227 global subdir
65951855 228
1b315056
CS
229 # Compile the compressed-debug-section test file.
230 if { [target_compile $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o object debug] != "" } {
231 verbose "Unable to compile test file."
232 untested "readelf -wa (compressed)"
233 return
234 }
235
236 # Download it.
237 set tempfile [remote_download host tmpdir/dw2-compressed.o]
238
239 # Run "readelf -wa" on it.
240 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
241
242 # Upload the results.
243 set output [remote_upload host readelf.out]
244
245 file_on_host delete $tempfile
65951855 246
1b315056
CS
247 if { [string compare [file_contents readelf.out] [file_contents $srcdir/$subdir/readelf.wa]] != 0 } then {
248 fail "readelf -wa (compressed)"
249 verbose "output is \n[file_contents readelf.out]" 2
250 verbose "expected is \n[file_contents $srcdir/$subdir/readelf.wa]" 2
251 return
252 }
253
254 pass "readelf -wa (compressed)"
255}
252b5132 256
09c11c86
NC
257# Test readelf's dumping abilities.
258
259proc readelf_dump_test {} {
260 global READELF
261 global READELFFLAGS
262 global srcdir
263 global subdir
65951855 264
09c11c86
NC
265 # Assemble the dump test file.
266 if {![binutils_assemble $srcdir/$subdir/dumptest.s tmpdir/dumptest.o]} then {
267 unresolved "readelf -p: failed to assemble dump test file"
268 return
269 }
270 # Download it.
271 set tempfile [remote_download host tmpdir/dumptest.o]
272
273 # Run "readelf -p.data" on it.
96037eb0
NC
274 set sect_names [get_standard_section_names]
275 if { $sect_names != "" } {
276 set got [remote_exec host "$READELF $READELFFLAGS -p[lindex $sect_names 1] $tempfile" "" "/dev/null" "readelf.out"]
277 } else {
278 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
279 }
09c11c86
NC
280 set got [lindex $got 1]
281
282 # Upload the results.
283 set output [remote_upload host readelf.out]
284
285 # Check for something going wrong.
286 if ![string match "" $got] then {
287 fail "readelf -p: unexpected output"
288 send_log $got
289 send_log "\n"
290 return
291 }
292
293 # Search for strings that should be in the output.
294 set sought {
295 ".*test_string.*"
296 }
65951855
RM
297
298 foreach looked_for $sought {
09c11c86
NC
299 set lines [grep $output $looked_for]
300 if ![llength $lines] then {
301 fail "readelf -p: missing: $looked_for"
302 send_log readelf.out
303 return
304 }
305 }
306
65951855 307 file_on_host delete $tempfile
09c11c86
NC
308 file_on_host delete $output
309
310 # All done.
311 pass "readelf -p"
312
313 # XXX FIXME: Add test of readelf -x here
252b5132
RH
314}
315
316if ![is_remote host] {
317 if {[which $READELF] == 0} then {
318 perror "$READELF does not exist"
319 return
320 }
321}
322
323send_user "Version [binutil_version $READELF]"
324
89084430 325# Assemble the test file.
252b5132 326if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
11bb0c7a
AM
327 unresolved "readelf -h bintest (failed to assemble)"
328 unresolved "readelf -S bintest (failed to assemble)"
329 unresolved "readelf -s bintest (failed to assemble)"
330 unresolved "readelf -r bintest (failed to assemble)"
f3185997
NC
331 global readelf_size
332 set readelf_size ""
252b5132 333} else {
252b5132 334
11bb0c7a
AM
335 if ![is_remote host] {
336 set tempfile tmpdir/bintest.o
337 } else {
338 set tempfile [remote_download host tmpdir/bintest.o]
339 }
9921923c 340
11bb0c7a 341 # First, determine the size, so specific output matchers can be used.
a0a8a934 342 readelf_find_size $tempfile 1
11bb0c7a
AM
343
344 # Run the tests.
345 readelf_test -h $tempfile readelf.h {}
346 readelf_test -S $tempfile readelf.s {}
347 setup_xfail "mips-*-*irix*"
348 readelf_test -s $tempfile readelf.ss {}
349 readelf_test -r $tempfile readelf.r {}
350}
252b5132 351
79bc120c
NC
352# Test demangling symbol names.
353if {![binutils_assemble $srcdir/$subdir/mangled.s tmpdir/mangled.o]} then {
354 unresolved "readelf -s -C bintest (failed to assemble)"
355} else {
356
357 if ![is_remote host] {
358 set tempfile tmpdir/mangled.o
359 } else {
360 set tempfile [remote_download host tmpdir/mangled.o]
361 }
362
363 # Run the test.
364 readelf_test {--syms --demangle --wide} $tempfile readelf.demangled {}
365}
366
cbb356d9 367readelf_wi_test
1b315056 368readelf_compressed_wa_test
09c11c86
NC
369
370readelf_dump_test
8b971f9f 371
99fabbc9
JL
372# These dump tests require an assembler.
373if {[which $AS] != 0} then {
374 run_dump_test "pr25543"
375 run_dump_test "retain1a"
376 run_dump_test "retain1b"
377 run_dump_test "readelf-maskos-1a"
378 run_dump_test "readelf-maskos-1b"
379}
79bc120c 380
8b971f9f
NC
381# PR 13482 - Check for off-by-one errors when dumping .note sections.
382if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
11bb0c7a 383 unresolved "readelf -n version (failed to assemble)"
8b971f9f 384} else {
0e602686
NC
385
386 if ![is_remote host] {
387 set tempfile tmpdir/version.o
388 } else {
389 set tempfile [remote_download host tmpdir/version.o]
390 }
391
392 readelf_test -n $tempfile readelf.n {}
8b971f9f
NC
393}
394
d1c4b12b
NC
395
396# PR 18374 - Check that relocations against the .debug_loc section
397# do not prevent readelf from displaying all the location lists.
398if {![binutils_assemble $srcdir/$subdir/pr18374.s tmpdir/pr18374.o]} then {
11bb0c7a 399 unresolved "readelf --debug-dump=loc pr18374 (failed to assemble)"
0e602686
NC
400} else {
401
402 if ![is_remote host] {
403 set tempfile tmpdir/pr18374.o
404 } else {
405 set tempfile [remote_download host tmpdir/pr18374.o]
406 }
407
408 readelf_test --debug-dump=loc $tempfile readelf.pr18374 {}
d1c4b12b
NC
409}
410
0e602686 411
9f272209
AO
412# locview - Check dumping of location lists with location views.
413if {![binutils_assemble $srcdir/$subdir/locview-1.s tmpdir/locview-1.o]} then {
414 unresolved "readelf --debug-dump=loc locview-1 (failed to assemble)"
415} else {
416
417 if ![is_remote host] {
418 set tempfile tmpdir/locview-1.o
419 } else {
420 set tempfile [remote_download host tmpdir/locview-1.o]
421 }
422
423 readelf_test --debug-dump=loc $tempfile readelf.locview-1 {}
424}
425if {![binutils_assemble $srcdir/$subdir/locview-2.s tmpdir/locview-2.o]} then {
426 unresolved "readelf --debug-dump=loc locview-2 (failed to assemble)"
427} else {
428
429 if ![is_remote host] {
430 set tempfile tmpdir/locview-2.o
431 } else {
432 set tempfile [remote_download host tmpdir/locview-2.o]
433 }
434
435 readelf_test --debug-dump=loc $tempfile readelf.locview-2 {}
436}
437
438
0e602686
NC
439# Check that decompressed dumps work.
440if {![binutils_assemble $srcdir/$subdir/z.s tmpdir/z.o]} then {
11bb0c7a 441 unresolved "readelf --decompress --hex-dump .debug_loc z (failed to assemble)"
d1c4b12b 442} else {
d1c4b12b 443
0e602686
NC
444 if ![is_remote host] {
445 set tempfile tmpdir/z.o
446 } else {
447 set tempfile [remote_download host tmpdir/z.o]
448 }
449
450 readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z {}
451}
a567769b 452
04914e37
NC
453# Skip the next test for the RISCV architectures because they
454# do not support .ULEB128 pseudo-ops with non-constant values.
a567769b
JK
455if ![istarget "riscv*-*-*"] then {
456
3b83ea38
AM
457 set hpux ""
458 if [istarget "hppa*64*-*-hpux*"] {
459 set hpux "--defsym HPUX=1"
460 }
04914e37 461
a567769b 462 # Assemble the DWARF-5 test file.
3b83ea38 463 if {![binutils_assemble_flags $srcdir/$subdir/dw5.S tmpdir/dw5.o $hpux]} then {
11bb0c7a 464 unresolved "readelf -wiaoRlL dw5 (failed to assemble)"
a567769b 465 } else {
a567769b 466
11bb0c7a
AM
467 # Download it.
468 if ![is_remote host] {
469 set tempfile tmpdir/dw5.o
470 } else {
471 set tempfile [remote_download host tmpdir/dw5.o]
472 }
473
474 # First, determine the size, so specific output matchers can be used.
a0a8a934 475 readelf_find_size $tempfile 2
a567769b 476
11bb0c7a 477 # Make sure that readelf can decode the contents.
d74b88ed 478 readelf_test -wiaoRlL $tempfile dw5.W {}
11bb0c7a 479 }
a567769b 480}
04914e37
NC
481
482# Assemble the DWARF-5 attributes test file.
483if {![binutils_assemble_flags $srcdir/$subdir/dwarf-attributes.S tmpdir/dwarf-attributes.o ""]} then {
484 unresolved "readelf -wi dwarf-attributes (failed to assemble)"
485} else {
486 # Download it.
487 if ![is_remote host] {
488 set tempfile tmpdir/dwarf-attributes.o
489 } else {
490 set tempfile [remote_download host tmpdir/dwarf-attributes.o]
491 }
492
493 # First, determine the size, so specific output matchers can be used.
a0a8a934 494 readelf_find_size $tempfile 3
04914e37
NC
495
496 # Make sure that readelf can decode the contents.
497 readelf_test -wi $tempfile dwarf-attributes.W {}
498}
dda8d76d
NC
499
500# Check that debug link sections can be dumped.
501if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
d85bf2ba 502 unresolved "readelf --debug-dump=links (failed to assemble debuglink.s)"
dda8d76d
NC
503} else {
504 if ![is_remote host] {
505 set tempfile tmpdir/debuglink.o
506 } else {
507 set tempfile [remote_download host tmpdir/debuglink.o]
508 }
509
510 readelf_test {--debug-dump=links} $tempfile readelf.k {}
dda8d76d 511
d85bf2ba 512 # Check that debug link sections can be followed.
dda8d76d 513 if {![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug]} then {
d85bf2ba 514 unresolved "readelf --debug-dump=follow-links (failed to assemble linkdebug.s)"
dda8d76d 515 } else {
d85bf2ba
NC
516 if [is_remote host] {
517 set tempfile2 [remote_download host tmpdir/linkdebug.debug]
dda8d76d
NC
518 }
519
39f0547e 520 readelf_test {-wKis} $tempfile readelf.wKis {}
dda8d76d
NC
521 }
522}
523
d85bf2ba
NC
524if {![binutils_assemble $srcdir/$subdir/dwo.s tmpdir/dwo.o]} then {
525 unresolved "readelf --debug-dump=links (failed to assemble dwo.s)"
526} else {
527 if ![is_remote host] {
528 set tempfile tmpdir/dwo.o
529 } else {
530 set tempfile [remote_download host tmpdir/dwo.o]
531 }
532
533 readelf_test {--debug-dump=links} $tempfile readelf.k2 {}
534}
535
1b513401
NC
536if {![binutils_assemble $srcdir/$subdir/zero-sec.s tmpdir/zero-sec.o]} then {
537 unresolved "readelf --enable-checks (failed to assemble zero-sec.s)"
538} else {
539 if ![is_remote host] {
540 set tempfile tmpdir/zero-sec.o
541 } else {
542 set tempfile [remote_download host tmpdir/zero-sec.o]
543 }
544
545 readelf_test {--enable-checks --sections --wide} $tempfile zero-sec.r {}
546}
d85bf2ba 547
80fda85c
L
548if ![is_remote host] {
549 set test $srcdir/$subdir/pr26112.o.bz2
550 # We need to strip the ".bz2", but can leave the dirname.
551 set t $subdir/[file tail $test]
552 set testname [file rootname $t]
553 verbose $testname
554 set tempfile tmpdir/pr26112.o
555 if {[catch "system \"bzip2 -dc $test > $tempfile\""] != 0} {
556 untested "bzip2 -dc ($testname)"
557 } else {
558 readelf_test {--debug-dump=macro} $tempfile pr26112.r {}
559 }
560}
This page took 0.973243 seconds and 4 git commands to generate.