]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/testsuite/binutils-all/readelf.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / testsuite / binutils-all / readelf.exp
1 # Copyright (C) 1999-2022 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 # 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>.
23
24 # Exclude non-ELF targets.
25 if ![is_elf_format] {
26 verbose "$READELF is only intended for ELF targets" 2
27 return
28 }
29
30 # First some helpful procedures, then the tests themselves
31
32 # Return the contents of the filename given
33 proc file_contents { filename } {
34 set file [open $filename r]
35 set contents [read $file]
36 close $file
37 return $contents
38 }
39
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.
43 proc readelf_find_size { binary_file test_iteration } {
44 global READELF
45 global READELFFLAGS
46 global readelf_size
47
48 set readelf_size ""
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"
53 }
54
55 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]]} then {
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
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.
78
79 proc readelf_test { options binary_file regexp_file } {
80
81 global READELF
82 global READELFFLAGS
83 global readelf_size
84 global srcdir
85 global subdir
86
87 set testname "readelf $options [file rootname [file tail $binary_file]]"
88
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"]
91
92 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
93 fail "$testname (reason: unexpected output)"
94 send_log $got
95 send_log "\n"
96 return
97 }
98
99 if { [is_elf_unused_section_symbols ] } {
100 set target_machine unused
101 } else {
102 set target_machine ""
103 }
104 if [istarget "mips*-*-*"] then {
105 if [is_bad_symtab] then {
106 set target_machine mips
107 } else {
108 set target_machine tmips
109 }
110 }
111
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
118 }
119
120 if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
121 send_log [file_contents readelf.out]
122 send_log "\n"
123 fail $testname
124 return
125 }
126
127 pass $testname
128 }
129
130 # Simple proc to skip certain expected warning messages.
131
132 proc prune_readelf_wi_warnings { text } {
133 regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
134 return $text
135 }
136
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.
143
144 proc readelf_wi_test {} {
145 global READELF
146 global READELFFLAGS
147 global srcdir
148 global subdir
149
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"
154 return
155 }
156
157 # Download it.
158 set tempfile [remote_download host tmpdir/testprog.o]
159
160 # Run "readelf -wi" on it.
161 set got [remote_exec host "$READELF $READELFFLAGS -wi $tempfile" "" "/dev/null" "readelf.out"]
162
163 # Upload the results.
164 set output [remote_upload host readelf.out]
165
166 file_on_host delete $tempfile
167
168 # Strip any superflous warnings.
169 set got [prune_readelf_wi_warnings [lindex $got 1]]
170
171 if ![string match "" $got] then {
172 fail "readelf $READELFFLAGS -wi (reason: unexpected output)"
173 send_log $got
174 send_log "\n"
175 return
176 }
177
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"
183 return
184 }
185
186 # Search for strings that should be in the output.
187 set sought {
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).*"
194 ".*DW_AT_name.*fn.*"
195 ".*DW_AT_name.*(main|indirect string).*"
196 ".*\(DW_OP_addr: 0\).*"
197 }
198
199 # The MSP430 in LARGE mode does not generate a DW_OP_addr.
200 setup_xfail msp430*-*-*
201
202 foreach looked_for $sought {
203 set lines [grep $output $looked_for]
204 if ![llength $lines] then {
205 fail "readelf -wi: missing: $looked_for"
206 send_log readelf.out
207 return
208 }
209 }
210
211 file_on_host delete $output
212
213 # All done.
214 pass "readelf -wi"
215 }
216
217 # This tests "readelf -wa", but on a file with a compressed
218 # .debug_abbrev section.
219
220 proc readelf_compressed_wa_test {} {
221 global READELF
222 global READELFFLAGS
223 global srcdir
224 global subdir
225
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)"
230 return
231 }
232
233 # Download it.
234 set tempfile [remote_download host tmpdir/dw2-compressed.o]
235
236 # Run "readelf -wa" on it.
237 set got [remote_exec host "$READELF $READELFFLAGS -wa $tempfile" "" "/dev/null" "readelf.out"]
238
239 # Upload the results.
240 set output [remote_upload host readelf.out]
241
242 file_on_host delete $tempfile
243
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
248 return
249 }
250
251 pass "readelf -wa (compressed)"
252 }
253
254 # Test readelf's dumping abilities.
255
256 proc readelf_dump_test {} {
257 global READELF
258 global READELFFLAGS
259 global srcdir
260 global subdir
261
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"
265 return
266 }
267 # Download it.
268 set tempfile [remote_download host tmpdir/dumptest.o]
269
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"]
274 } else {
275 set got [remote_exec host "$READELF $READELFFLAGS -p.data $tempfile" "" "/dev/null" "readelf.out"]
276 }
277 set got [lindex $got 1]
278
279 # Upload the results.
280 set output [remote_upload host readelf.out]
281
282 # Check for something going wrong.
283 if ![string match "" $got] then {
284 fail "readelf -p: unexpected output"
285 send_log $got
286 send_log "\n"
287 return
288 }
289
290 # Search for strings that should be in the output.
291 set sought {
292 ".*test_string.*"
293 }
294
295 foreach looked_for $sought {
296 set lines [grep $output $looked_for]
297 if ![llength $lines] then {
298 fail "readelf -p: missing: $looked_for"
299 send_log readelf.out
300 return
301 }
302 }
303
304 file_on_host delete $tempfile
305 file_on_host delete $output
306
307 # All done.
308 pass "readelf -p"
309
310 # XXX FIXME: Add test of readelf -x here
311 }
312
313 # Tests whether readelf can read thin archives
314 proc readelf_thin_archive_test {} {
315 global AR
316
317 if ![is_remote host] {
318 set tempfile tmpdir/bintest.o
319 set templib tmpdir/bintest.thin.a
320 set libname tmpdir/bintest.thin.a
321 } else {
322 set tempfile [remote_download host tmpdir/bintest.o]
323 set templib [remote_download host tmpdir/bintest.thin.a]
324 set libname bintest.thin.a
325 }
326
327 set testname "readelf -h bintest.thin"
328 set got [binutils_run $AR "rcT $libname ${tempfile}"]
329 if ![string match "" $got] {
330 fail $testname
331 return
332 }
333
334 readelf_test -h $templib readelf.h.thin
335
336 pass $testname
337 }
338
339 if ![is_remote host] {
340 if {[which $READELF] == 0} then {
341 perror "$READELF does not exist"
342 return
343 }
344 }
345
346 send_user "Version [binutil_version $READELF]"
347
348 # Assemble the test file.
349 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
350 unsupported "readelf -h bintest (failed to assemble)"
351 unsupported "readelf -S bintest (failed to assemble)"
352 unsupported "readelf -s bintest (failed to assemble)"
353 unsupported "readelf -r bintest (failed to assemble)"
354 global readelf_size
355 set readelf_size ""
356 } else {
357
358 if ![is_remote host] {
359 set tempfile tmpdir/bintest.o
360 } else {
361 set tempfile [remote_download host tmpdir/bintest.o]
362 }
363
364 # First, determine the size, so specific output matchers can be used.
365 readelf_find_size $tempfile 1
366
367 # Run the tests.
368 readelf_test -h $tempfile readelf.h
369 readelf_test -S $tempfile readelf.s
370 setup_xfail "mips-*-*irix*"
371 readelf_test -s $tempfile readelf.ss
372 readelf_test -r $tempfile readelf.r
373
374 readelf_thin_archive_test
375 }
376
377 # Test demangling symbol names.
378 if {![binutils_assemble $srcdir/$subdir/mangled.s tmpdir/mangled.o]} then {
379 unsupported "readelf -s -C bintest (failed to assemble)"
380 } else {
381
382 if ![is_remote host] {
383 set tempfile tmpdir/mangled.o
384 } else {
385 set tempfile [remote_download host tmpdir/mangled.o]
386 }
387
388 # Run the test.
389 readelf_test {--syms --demangle --wide} $tempfile readelf.demangled
390 }
391
392 readelf_wi_test
393 readelf_compressed_wa_test
394
395 readelf_dump_test
396
397 # These dump tests require an assembler.
398 if {[which $AS] != 0} then {
399 run_dump_test "pr25543"
400 run_dump_test "retain1a"
401 run_dump_test "retain1b"
402 run_dump_test "readelf-maskos-1a"
403 run_dump_test "readelf-maskos-1b"
404 if {![istarget *-*-hpux*]} then {
405 run_dump_test pr26548
406 if {![binutils_assemble_flags $srcdir/$subdir/pr26548.s tmpdir/pr26548e.o {--defsym ERROR=1}]} then {
407 unsupported "pr26548e (failed to assemble)"
408 } else {
409 if ![is_remote host] {
410 set tempfile tmpdir/pr26548e.o
411 } else {
412 set tempfile [remote_download host tmpdir/pr26548e.o]
413 }
414 readelf_test -Wwi $tempfile pr26548e.d
415 }
416 }
417 }
418
419 # PR 13482 - Check for off-by-one errors when dumping .note sections.
420 if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
421 unsupported "readelf -n version (failed to assemble)"
422 } else {
423
424 if ![is_remote host] {
425 set tempfile tmpdir/version.o
426 } else {
427 set tempfile [remote_download host tmpdir/version.o]
428 }
429
430 readelf_test -n $tempfile readelf.n
431 }
432
433
434 # PR 18374 - Check that relocations against the .debug_loc section
435 # do not prevent readelf from displaying all the location lists.
436 if {![binutils_assemble $srcdir/$subdir/pr18374.s tmpdir/pr18374.o]} then {
437 unsupported "readelf --debug-dump=loc pr18374 (failed to assemble)"
438 } else {
439
440 if ![is_remote host] {
441 set tempfile tmpdir/pr18374.o
442 } else {
443 set tempfile [remote_download host tmpdir/pr18374.o]
444 }
445
446 readelf_test --debug-dump=loc $tempfile readelf.pr18374
447 }
448
449
450 # locview - Check dumping of location lists with location views.
451 if {![binutils_assemble $srcdir/$subdir/locview-1.s tmpdir/locview-1.o]} then {
452 unsupported "readelf --debug-dump=loc locview-1 (failed to assemble)"
453 } else {
454
455 if ![is_remote host] {
456 set tempfile tmpdir/locview-1.o
457 } else {
458 set tempfile [remote_download host tmpdir/locview-1.o]
459 }
460
461 readelf_test --debug-dump=loc $tempfile readelf.locview-1
462 }
463 if {![binutils_assemble $srcdir/$subdir/locview-2.s tmpdir/locview-2.o]} then {
464 unsupported "readelf --debug-dump=loc locview-2 (failed to assemble)"
465 } else {
466
467 if ![is_remote host] {
468 set tempfile tmpdir/locview-2.o
469 } else {
470 set tempfile [remote_download host tmpdir/locview-2.o]
471 }
472
473 readelf_test --debug-dump=loc $tempfile readelf.locview-2
474 }
475
476
477 # Check that decompressed dumps work.
478 if {![binutils_assemble $srcdir/$subdir/z.s tmpdir/z.o]} then {
479 unsupported "readelf --decompress --hex-dump .debug_loc z (failed to assemble)"
480 } else {
481
482 if ![is_remote host] {
483 set tempfile tmpdir/z.o
484 } else {
485 set tempfile [remote_download host tmpdir/z.o]
486 }
487
488 readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z
489 }
490
491 # Skip the next test for the RISCV architectures because they
492 # do not support .ULEB128 pseudo-ops with non-constant values.
493 if ![istarget "riscv*-*-*"] then {
494
495 set hpux ""
496 if [istarget "hppa*64*-*-hpux*"] {
497 set hpux "--defsym HPUX=1"
498 }
499
500 # Assemble the DWARF-5 test file.
501 if {![binutils_assemble_flags $srcdir/$subdir/dw5.S tmpdir/dw5.o $hpux]} then {
502 unsupported "readelf -wiaoRlL dw5 (failed to assemble)"
503 } else {
504
505 # Download it.
506 if ![is_remote host] {
507 set tempfile tmpdir/dw5.o
508 } else {
509 set tempfile [remote_download host tmpdir/dw5.o]
510 }
511
512 # First, determine the size, so specific output matchers can be used.
513 readelf_find_size $tempfile 2
514
515 # Make sure that readelf can decode the contents.
516 readelf_test -wiaoRlL $tempfile dw5.W
517 }
518 }
519
520 # Assemble the DWARF-5 attributes test file.
521 if {![binutils_assemble_flags $srcdir/$subdir/dwarf-attributes.S tmpdir/dwarf-attributes.o ""]} then {
522 unsupported "readelf -wi dwarf-attributes (failed to assemble)"
523 } else {
524 # Download it.
525 if ![is_remote host] {
526 set tempfile tmpdir/dwarf-attributes.o
527 } else {
528 set tempfile [remote_download host tmpdir/dwarf-attributes.o]
529 }
530
531 # First, determine the size, so specific output matchers can be used.
532 readelf_find_size $tempfile 3
533
534 # Make sure that readelf can decode the contents.
535 readelf_test -wi $tempfile dwarf-attributes.W
536 }
537
538 # Check that debug link sections can be dumped.
539 if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
540 unsupported "readelf --debug-dump=links (failed to assemble debuglink.s)"
541 } else {
542 if ![is_remote host] {
543 set tempfile tmpdir/debuglink.o
544 } else {
545 set tempfile [remote_download host tmpdir/debuglink.o]
546 }
547
548 readelf_test {--debug-dump=links -wN} $tempfile readelf.k
549
550 # Check that debug link sections can be followed.
551 if {![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug]} then {
552 unsupported "readelf --debug-dump=follow-links (failed to assemble linkdebug.s)"
553 } else {
554 if [is_remote host] {
555 set tempfile2 [remote_download host tmpdir/linkdebug.debug]
556 }
557
558 readelf_test "-wKis -P" $tempfile readelf.wKis
559 }
560 }
561
562 if {![binutils_assemble $srcdir/$subdir/dwo.s tmpdir/dwo.o]} then {
563 unsupported "readelf --debug-dump=links (failed to assemble dwo.s)"
564 } else {
565 if ![is_remote host] {
566 set tempfile tmpdir/dwo.o
567 } else {
568 set tempfile [remote_download host tmpdir/dwo.o]
569 }
570
571 readelf_test {--debug-dump=links --debug-dump=no-follow-links} $tempfile readelf.k2
572 }
573
574 if {![binutils_assemble $srcdir/$subdir/zero-sec.s tmpdir/zero-sec.o]} then {
575 unsupported "readelf --enable-checks (failed to assemble zero-sec.s)"
576 } else {
577 if ![is_remote host] {
578 set tempfile tmpdir/zero-sec.o
579 } else {
580 set tempfile [remote_download host tmpdir/zero-sec.o]
581 }
582
583 readelf_test {--enable-checks --sections --wide} $tempfile zero-sec.r
584 }
585
586 if ![is_remote host] {
587 set test $srcdir/$subdir/pr26112.o.bz2
588 # We need to strip the ".bz2", but can leave the dirname.
589 set t $subdir/[file tail $test]
590 set testname [file rootname $t]
591 verbose $testname
592 set tempfile tmpdir/pr26112.o
593 if {[catch "system \"bzip2 -dc $test > $tempfile\""] != 0} {
594 untested "bzip2 -dc ($testname)"
595 } else {
596 readelf_test {--debug-dump=macro -wN} $tempfile pr26112.r
597 }
598 }