1 # Copyright (C) 1994-2023 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 # Written by Ian Lance Taylor <ian@cygnus.com>
22 if ![is_remote host] {
23 if {[which $OBJCOPY] == 0} then {
24 perror "$OBJCOPY does not exist"
29 send_user "Version [binutil_version $OBJCOPY]"
31 if ![is_remote host] {
32 set tempfile tmpdir/bintest.o
33 set copyfile tmpdir/copy
35 set tempfile [remote_download host tmpdir/bintest.o]
39 # Test that objcopy does not modify a file when copying it.
40 # "object" or "executable" values for type are supported.
42 proc objcopy_test {testname srcfile type asflags ldflags} {
49 set t_tempfile $tempfile
50 set t_copyfile ${copyfile}.o
52 if { $type != "object" && $type != "executable" } {
53 error "objcopy_test accepts only \"object\" or \"executable\" values for type"
56 if {![binutils_assemble_flags $srcdir/$subdir/${srcfile} $t_tempfile "$asflags"]} then {
57 unsupported "objcopy $type ($testname)"
58 remote_file host delete $t_tempfile
63 if { $type == "executable" } {
65 # Check that LD exists
66 if {[which $LD] == 0} then {
67 untested "objcopy $type ($testname)"
71 # Use tempfile and copyfile without the .o extension for executable files
72 set t_tempfile [string range $tempfile 0 end-2]
73 set t_copyfile $copyfile
74 set got [binutils_run $LD "$tempfile -o $t_tempfile $ldflags"]
75 if { ![string equal "" $got] } then {
76 unsupported "objcopy $type ($testname)"
79 set xflags "--preserve-dates"
83 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $xflags $t_tempfile $t_copyfile"]
85 if ![string equal "" $got] then {
87 fail "objcopy $type ($testname)"
89 send_log "cmp $t_tempfile $t_copyfile\n"
90 verbose "cmp $t_tempfile $t_copyfile"
92 set src1 tmpdir/bintest
94 remote_upload host $t_tempfile $src1
95 remote_upload host $t_copyfile $src2
100 set status [remote_exec build cmp "${src1} ${src2}"]
101 set exec_output [lindex $status 1]
102 set exec_output [prune_warnings $exec_output]
104 if [string equal "" $exec_output] then {
105 pass "objcopy $type ($testname)"
106 if { $type == "executable" } {
107 set dir [file dirname $t_copyfile]
108 set f2 [file tail $t_copyfile]
109 set status [remote_exec host find "$dir -name $f2 -newer $t_tempfile -print"]
110 set exec_output [lindex $status 1]
111 if [string equal "" $exec_output] then {
112 pass "objcopy $type ($testname) timestamp"
114 fail "objcopy $type ($testname) timestamp"
118 send_log "$exec_output\n"
119 verbose "$exec_output" 1
121 # On OSF/1, this succeeds with gas and fails with /bin/as.
122 setup_xfail "alpha*-*-osf*"
124 fail "objcopy $type ($testname)"
129 setup_xfail "hppa*-*-*"
130 setup_xfail "sh-*-coff*"
131 setup_xfail "tic54x-*-*"
132 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
133 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
134 objcopy_test "simple copy" bintest.s object "" ""
136 # Test verilog data width
137 proc objcopy_test_verilog {testname} {
143 set binfile tmpdir/verilogtest.o
144 set verilog tmpdir/verilog
146 set got [binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]
147 if {![binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]} then {
148 unsupported "objcopy ($testname)"
152 set got [binutils_run $OBJCOPY "-O verilog $binfile $verilog"]
153 if ![string equal "" $got] then {
154 fail "objcopy ($testname)"
157 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"]
158 if ![regexp "error: verilog data width must be 1, 2, 4, 8 or 16" $got] then {
159 fail "objcopy ($testname 0) {$got}"
161 pass "objcopy ($testname 0)"
164 foreach width {1 2} {
165 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
166 if ![string equal "" $got] then {
167 fail "objcopy ($testname $width)"
169 send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
170 if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
171 pass "objcopy ($testname $width)"
173 fail "objcopy ($testname $width)"
177 # 16-bit little-endian targets fail the following tests because the
178 # verilog backend does not convert from 16-bits to 32-bits before it
179 # converts from internal format to little endian format.
180 if { [istarget tic54*-*-*] || [istarget pdp11-*-*] } {
181 untested "verilog width-4 and width-8 tests"
185 foreach width {4 8} {
186 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
187 if ![string equal "" $got] then {
188 fail "objcopy ($testname $width)"
190 send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
191 if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
192 pass "objcopy ($testname $width)"
194 fail "objcopy ($testname $width)"
198 # Test generating endian correct output.
199 set testname "objcopy (verilog output endian-ness == input endian-ness)"
200 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 4 $binfile $verilog-I4.hex"]
201 if ![string equal "" $got] then {
204 send_log "regexp_diff $verilog-I4.hex $srcdir/$subdir/verilog-I4.hex\n"
205 if {! [regexp_diff "$verilog-I4.hex" "$srcdir/$subdir/verilog-I4.hex"]} {
212 objcopy_test_verilog "verilog data width"
214 if { [file exists $tempfile] } {
215 # Test reversing bytes in a section.
217 set reversed ${tempfile}-reversed
218 set sect_names [get_standard_section_names]
219 if { $sect_names != "" } {
220 if { [istarget hppa*-*-hpux*] } {
221 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j \$PRIVATE\$ -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
223 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
226 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
229 if ![string equal "" $got] then {
230 fail "objcopy --reverse-bytes"
232 if [is_remote host] {
233 remote_upload host ${reversed} tmpdir/copy-reversed.o
234 set reversed tmpdir/copy-reversed.o
237 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
238 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
240 set want "^ \[0-9\]+ (\[0-9\]+)"
241 set found_orig [regexp -lineanchor $want $origdata -> origdata]
242 set found_rev [regexp -lineanchor $want $revdata -> revdata]
244 if {$found_orig == 0 || $found_rev == 0} then {
245 fail "objcopy --reverse-bytes"
247 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
248 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
250 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
251 pass "objcopy --reverse-bytes"
253 fail "objcopy --reverse-bytes"
259 # Test interleaved copy of multiple byte width
261 set sequence_file sequence_file
262 set file [open ${sequence_file} w]
263 puts ${file} "12345678"
266 if [is_remote host] {
267 remote_upload host ${sequence_file} tmpdir/sequence_file
268 set sequence_file tmpdir/sequence_file
271 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
273 if ![string equal "" $got] then {
274 fail "objcopy -i --interleave-width"
276 if [is_remote host] {
277 remote_upload host ${copyfile} tmpdir/interleave_output
278 set interleave_output tmpdir/interleave_output
280 set interleave_output ${copyfile}
283 set file [open ${interleave_output} r]
288 if ![string equal "1256" $line] then {
289 fail "objcopy -i --interleave-width"
291 pass "objcopy -i --interleave-width"
296 # Test generating S records.
298 if { [file exists $tempfile] } {
299 # We make the srec filename 8.3 compatible. Note that the header string
300 # matched against depends on the name of the file. Ugh.
302 if [is_remote host] {
303 set srecfile copy.sre
304 set header_string S00B0000636F70792E737265C1
306 set srecfile ${copyfile}.srec
307 set header_string S0130000746D706469722F636F70792E7372656397
310 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
312 if ![string equal "" $got] then {
313 fail "objcopy -O srec"
315 if [is_remote host] {
316 remote_upload host ${srecfile} tmpdir/copy.srec
317 set srecfile tmpdir/copy.srec
319 set file [open ${srecfile} r]
321 # The first S record is fixed by the file name we are using.
325 if ![regexp "$header_string.*" $line] {
326 send_log "bad header\n"
327 fail "objcopy -O srec"
329 while {[gets $file line] != -1 \
330 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
337 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
338 send_log "bad trailer\n"
339 fail "objcopy -O srec"
341 if {[gets $file line] != -1} then {
342 send_log "garbage at end\n"
345 fail "objcopy -O srec"
347 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
348 if ![regexp "file format srec" $got] then {
349 send_log "objdump failed\n"
350 fail "objcopy -O srec"
352 pass "objcopy -O srec"
362 # Test setting and adjusting the start address. We only test this
363 # while generating S records, because we may not be able to set the
364 # start address for other object file formats, and the S record case
365 # is the only useful one anyhow.
367 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
368 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
369 perror "objdump can not recognize bintest.o"
372 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
373 if ![string equal "" $got] then {
374 fail "objcopy --set-start"
376 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
377 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
378 fail "objcopy --set-start"
380 if {$srecstart != 0x7654} then {
381 send_log "$srecstart != 0x7654\n"
382 fail "objcopy --set-start"
384 pass "objcopy --set-start"
389 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
390 if ![string equal "" $got] then {
391 fail "objcopy --adjust-start"
393 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
394 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
395 fail "objcopy --adjust-start"
397 if {$srecstart != $origstart + 0x123} then {
398 send_log "$srecstart != $origstart + 0x123\n"
399 fail "objcopy --adjust-start"
401 pass "objcopy --adjust-start"
407 # Test adjusting the overall VMA, and adjusting the VMA of a
408 # particular section. We again only test this when generating S
414 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
416 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
419 while {[regexp $headers_regexp $got all name size vma rest]} {
423 if {$low == "" || $vma < $low} {
431 if {$low == "" || $origstart == ""} then {
432 perror "objdump can not recognize bintest.o"
434 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
435 if ![string equal "" $got] then {
436 fail "objcopy --adjust-vma"
438 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
439 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
440 if ![regexp $want $got all start vma] then {
441 fail "objcopy --adjust-vma"
444 if {$vma != $low + 0x123} then {
445 send_log "$vma != $low + 0x123\n"
446 fail "objcopy --adjust-vma"
448 if {$start != $origstart + 0x123} then {
449 send_log "$start != $origstart + 0x123\n"
450 fail "objcopy --adjust-vma"
452 pass "objcopy --adjust-vma"
460 while {[regexp $headers_regexp $got all name size vma rest]} {
462 if {$vma == $low} then {
463 set arg "$arg --adjust-section-vma $name+4"
468 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
469 if ![string equal "" $got] then {
470 fail "objcopy --adjust-section-vma +"
472 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
473 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
474 if ![regexp $want $got all vma] then {
475 fail "objcopy --adjust-section-vma +"
478 if {$vma != $low + 4} then {
479 send_log "$vma != $low + 4\n"
480 fail "objcopy --adjust-section-vma +"
482 pass "objcopy --adjust-section-vma +"
487 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
488 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
489 if ![string equal "" $got] then {
490 fail "objcopy --adjust-section-vma ="
492 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
493 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
494 if ![regexp $want $got all vma] then {
495 fail "objcopy --adjust-section-vma ="
498 if {$vma != $low + 4} then {
499 send_log "$vma != $low + 4\n"
500 fail "objcopy --adjust-section-vma ="
502 pass "objcopy --adjust-section-vma ="
508 # Test stripping an object.
510 proc strip_test { } {
523 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
528 set osabi_fail "false"
530 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
531 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
532 if { [lindex $exec_output 0] != 0 } then {
533 fail "$test preserving OS/ABI"
534 set osabi_fail "true"
536 verbose -log "grep OS/ABI tmpdir/osabi.in"
537 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
541 if [is_remote host] {
542 set archive libstrip.a
543 set objfile [remote_download host tmpdir/testprog.o]
544 remote_file host delete $archive
546 set archive tmpdir/libstrip.a
547 set objfile tmpdir/testprog.o
550 remote_file build delete tmpdir/libstrip.a
552 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
553 set exec_output [prune_warnings $exec_output]
554 if ![string equal "" $exec_output] {
556 unresolved "$test preserving OS/ABI"
560 set exec_output [binutils_run $STRIP "-g $archive"]
561 set exec_output [prune_warnings $exec_output]
562 if ![string equal "" $exec_output] {
564 unresolved "$test preserving OS/ABI"
568 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
569 set exec_output [prune_warnings $exec_output]
570 if ![string equal "" $exec_output] {
572 unresolved "$test preserving OS/ABI"
576 if { $osabi_fail != "true" && [is_elf_format] } {
577 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
578 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
579 if { [lindex $exec_output 0] != 0 } then {
580 fail "$test preserving OS/ABI"
582 verbose -log "grep OS/ABI tmpdir/osabi.out"
583 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
584 if { "$osabi_in" == "$osabi_out" } {
585 pass "$test preserving OS/ABI"
587 fail "$test preserving OS/ABI"
592 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
597 if [is_remote host] {
598 set objfile [remote_download host tmpdir/testprog.o]
600 set objfile tmpdir/testprog.o
603 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
604 set exec_output [prune_warnings $exec_output]
605 if ![string equal "" $exec_output] {
610 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
611 set exec_output [prune_warnings $exec_output]
612 if ![string match "*: no symbols*" $exec_output] {
622 # Test stripping an object file with saving a symbol
624 proc strip_test_with_saving_a_symbol { } {
633 set test "strip with saving a symbol"
635 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
640 if [is_remote host] {
641 set objfile [remote_download host tmpdir/testprog.o]
643 set objfile tmpdir/testprog.o
646 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
647 set exec_output [prune_warnings $exec_output]
648 if ![string equal "" $exec_output] {
653 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
654 set exec_output [prune_warnings $exec_output]
655 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TtDd] _?main} $exec_output]} {
663 strip_test_with_saving_a_symbol
665 # Build a final executable.
668 set test_prog "testprog${exe}"
670 proc copy_setup { } {
677 if {[target_info exists needs_status_wrapper] && \
678 [target_info needs_status_wrapper] != "0"} {
679 set res [build_wrapper testglue.o]
685 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } {
690 lappend flags "additional_flags=[lindex $res 1]"
691 set add_libs "testglue.o"
696 if { [istarget *-*-linux*]
697 || [istarget *-*-gnu*] } {
698 foreach i $gcc_gas_flag {
699 set flags "additional_flags=$i $flags"
702 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
706 set result [remote_load target tmpdir/$test_prog]
707 set status [lindex $result 0]
709 if { $status != "pass" } {
710 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
717 # Test copying an executable.
719 proc copy_executable { prog flags test1 test2 } {
722 if [is_remote host] {
723 set testfile [remote_download host tmpdir/$test_prog]
724 set testcopy copyprog
726 set testfile tmpdir/$test_prog
727 set testcopy tmpdir/copyprog
729 remote_file host delete $testcopy
731 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
733 if ![string equal "" $exec_output] {
735 if [string equal "" $test2] {
742 if [is_remote host] {
743 remote_upload host $testcopy tmpdir/copyprog
746 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
747 set exec_output [lindex $status 1]
749 if [string equal "" $exec_output] then {
752 send_log "$exec_output\n"
753 verbose "$exec_output"
755 # This will fail for many reasons. For example, it will most
756 # likely fail if a non-GNU linker is used. Therefore, we do
757 # not insist that it pass. If you are using an assembler and
758 # linker based on the same BFD as objcopy, it is worth
759 # investigating to see why this failure occurs. If we are
760 # cross compiling, we assume that a GNU linker is being used,
761 # and expect it to succeed.
762 if {[isnative]} then {
766 # This also fails for some mips targets. See elf32-mips.c
767 # mips_elf_sym_is_global.
768 if { [is_bad_symtab] } then {
772 setup_xfail "arm*-*-pe"
773 setup_xfail "*-*-mingw*"
774 setup_xfail "*-*-cygwin*"
779 if [string equal "" $test2] {
783 set output [remote_load target tmpdir/copyprog]
784 set status [lindex $output 0]
785 if { $status != "pass" } {
792 # Test stripping an executable
794 proc strip_executable { prog flags test1 test2 } {
799 remote_file build delete tmpdir/striprog
800 remote_download build tmpdir/copyprog tmpdir/striprog
801 if [is_remote host] {
802 set copyfile [remote_download host tmpdir/striprog]
804 set copyfile tmpdir/striprog
807 set osabi_fail "false"
809 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
810 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
811 if { [lindex $exec_output 0] != 0 } then {
812 fail "$test1 preserving OS/ABI"
813 set osabi_fail "true"
815 verbose -log "grep OS/ABI tmpdir/osabi.in"
816 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
820 set exec_output [binutils_run $prog "$flags ${copyfile}"]
821 if ![string equal "" $exec_output] {
823 if [string equal "" $test2] {
830 if [is_remote host] {
831 remote_upload host ${copyfile} tmpdir/striprog
834 if { $osabi_fail != "true" && [is_elf_format] } {
835 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
836 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
837 if { [lindex $exec_output 0] != 0 } then {
838 fail "$test1 preserving OS/ABI"
840 verbose -log "grep OS/ABI tmpdir/osabi.out"
841 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
842 if { "$osabi_in" == "$osabi_out" } {
843 pass "$test1 preserving OS/ABI"
845 fail "$test1 preserving OS/ABI"
850 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
851 if ![string match "*: no symbols*" $exec_output] {
856 if [string equal "" $test2] {
860 set result [remote_load target tmpdir/striprog]
861 set status [lindex $result 0]
862 if { $status != "pass" } {
870 # Test stripping an executable with saving a symbol
872 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
876 remote_file build delete tmpdir/striprog
877 remote_download build tmpdir/copyprog tmpdir/striprog
878 if [is_remote host] {
879 set copyfile [remote_download host tmpdir/striprog]
881 set copyfile tmpdir/striprog
884 set exec_output [binutils_run $prog "$flags ${copyfile}"]
885 if ![string equal "" $exec_output] {
887 if [string equal "" $test2] {
894 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
895 if { [istarget mmix-knuth-mmixware] } {
896 # Whenever there's a symbol in the mmo format, there's the symbol
897 # Main, so remove it manually from the expected output for sake of
900 # Using "" not {} to get the \n and \r translated.
901 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
904 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TtDd] _?main} $exec_output]} {
909 if [string equal "" $test2] {
913 if [is_remote host] {
914 remote_upload host ${copyfile} tmpdir/striprog
917 set result [remote_load target tmpdir/striprog]
918 set status [lindex $result 0]
919 if { $status != "pass" } {
927 # Test keeping only debug symbols of an executable
929 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
930 remote_file build delete tmpdir/striprog
931 remote_download build tmpdir/copyprog tmpdir/striprog
932 if [is_remote host] {
933 set copyfile [remote_download host tmpdir/striprog]
935 set copyfile tmpdir/striprog
938 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
939 if ![string equal "" $exec_output] {
945 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
946 if ![string equal "" $exec_output] {
953 # Tests that in a debug only copy of a file the sections
954 # headers whose types have been changed to NOBITS still
955 # retain their sh_link fields.
957 proc keep_debug_symbols_and_check_links { prog flags test } {
960 remote_file build delete tmpdir/striprog
961 remote_download build tmpdir/copyprog tmpdir/striprog
962 if [is_remote host] {
963 set copyfile [remote_download host tmpdir/striprog]
965 set copyfile tmpdir/striprog
968 set exec_output [binutils_run $prog "$flags ${copyfile}"]
969 if ![string equal "" $exec_output] {
974 set got [binutils_run $READELF "-S --wide ${copyfile}"]
977 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
979 {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ ]+NOBITS[ ]+[0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9]+[ A]+([0-9]+)(.*)} \
980 $got all name link rest]} {
983 # Only some NOBITS sections should have a non-zero sh_link field.
984 # Look for them by name.
985 verbose "NOBITS section .$name has a 0 sh_link field\n"
987 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
988 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
989 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
990 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
991 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
992 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
993 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
994 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
1007 # For ELF relocatables, test that --only-keep-debug
1008 # marks non-debug output sections as NOBITS.
1010 proc keep_debug_symbols_for_elf_relocatable { prog flags test } {
1015 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
1020 if [is_remote host] {
1021 set relocatable [remote_download host tmpdir/testprog.o]
1023 set relocatable tmpdir/testprog.o
1026 set non_debug_sections {}
1027 set got [binutils_run $READELF "-S --wide ${relocatable}"]
1029 {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ \t]+([A-Z]+)[ \t]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ [0-9]+[ \t]+([A-Z]*)[ \t]+[0-9]+(.*)} \
1030 $got all name type flag rest] } {
1031 if { $type != "NOTE" && [regexp {[AG]} $flag] } {
1032 lappend non_debug_sections $name
1037 set exec_output [binutils_run $prog "$flags ${relocatable}"]
1038 if ![string equal "" $exec_output] {
1044 set got [binutils_run $READELF "-S --wide ${relocatable}"]
1046 {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ \t]+([A-Z]+)[ \t]+[0-9a-f]+ [0-9a-f]+ [0-9a-f]+ [0-9]+[ \t]+[A-Z]*[ \t]+[0-9]+(.*)} \
1047 $got all name type rest] } {
1048 if { [lsearch -exact $non_debug_sections $name] >= 0 && $type != "NOBITS" } {
1050 send_log "Expected SHT_NOBITS type for .$name\n"
1054 if { $fails == 1 } {
1062 set test1 "simple objcopy of executable"
1063 set test1r "run objcopy of executable"
1064 set test2 "strip executable"
1065 set test2r "run stripped executable"
1066 set test3 "strip executable with saving a symbol"
1067 set test3r "run stripped executable with saving a symbol"
1068 set test4 "keep only debug data"
1069 set test5 "simple objcopy of debug data"
1070 if [is_elf_format] {
1071 set test6 "NOBITS sections retain sh_link field"
1072 set test7 "--only-keep-debug for ELF relocatables"
1075 switch [copy_setup] {
1088 if [is_elf_format] {
1093 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
1095 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
1097 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
1099 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1100 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1101 if [is_elf_format] {
1102 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1103 keep_debug_symbols_for_elf_relocatable "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test7"
1107 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
1108 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
1109 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
1110 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1111 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1112 if [is_elf_format] {
1113 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1114 keep_debug_symbols_for_elf_relocatable "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test7"
1119 proc objcopy_test_readelf {testname srcfile} {
1126 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
1127 unsupported "objcopy ($testname)"
1131 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
1132 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
1133 if { [lindex $exec_output 0] != 0
1134 || ![string equal "" [lindex $exec_output 1]] } then {
1135 fail "objcopy ($testname)"
1139 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
1140 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
1141 if { [lindex $exec_output 0] != 0 } then {
1142 fail "objcopy ($testname)"
1145 set exec_output [prune_warnings [lindex $exec_output 1]]
1146 if ![string equal "" $exec_output] then {
1147 unresolved "objcopy ($testname)"
1151 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
1152 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
1153 if { [lindex $exec_output 0] != 0 } then {
1154 fail "objcopy ($testname)"
1157 set exec_output [prune_warnings [lindex $exec_output 1]]
1158 if ![string equal "" $exec_output] then {
1159 unresolved "objcopy ($testname)"
1163 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
1164 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
1165 set exec_output [prune_warnings $exec_output]
1167 if [string equal "" $exec_output] then {
1168 pass "objcopy ($testname)"
1170 fail "objcopy ($testname)"
1174 proc objcopy_test_symbol_manipulation {} {
1178 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1179 foreach t $test_list {
1180 # We need to strip the ".d", but can leave the dirname.
1181 verbose [file rootname $t]
1182 run_dump_test [file rootname $t]
1185 if { [supports_gnu_unique] } {
1186 run_dump_test "weaken-gnu-unique"
1190 proc objcopy_test_elf_common_symbols {} {
1194 # hpux has a non-standard common directive.
1195 if { [istarget "*-*-hpux*"] } then {
1199 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1200 foreach t $test_list {
1201 # We need to strip the ".d", but can leave the dirname.
1202 verbose [file rootname $t]
1203 run_dump_test [file rootname $t]
1207 # ia64 specific tests
1208 if { ([istarget "ia64-*-elf*"]
1209 || [istarget "ia64-*-linux*"]) } {
1210 objcopy_test "ia64 link order" link-order.s object "" ""
1213 # ELF specific tests
1215 if [is_elf_format] {
1216 objcopy_test_symbol_manipulation
1217 objcopy_test_elf_common_symbols
1219 setup_xfail "hppa*-*-*"
1220 setup_xfail "sh-*-coff*"
1221 setup_xfail "tic54x-*-*"
1222 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1223 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1224 objcopy_test "ELF unknown section type" unknown.s object "" ""
1226 objcopy_test_readelf "ELF group 1" group.s
1227 objcopy_test_readelf "ELF group 2" group-2.s
1228 objcopy_test_readelf "ELF group 3" group-3.s
1229 objcopy_test_readelf "ELF group 4" group-4.s
1230 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1231 run_dump_test "group-5"
1232 run_dump_test "group-6"
1233 run_dump_test "group-7a"
1234 run_dump_test "group-7b"
1235 run_dump_test "group-7c"
1236 run_dump_test "copy-1"
1237 run_dump_test "note-1"
1238 # Use copytest.o from the note-1 test to determine ELF32 or ELF64
1239 if [is_elf64 tmpdir/copytest.o] {
1240 set elf64 "--defsym ELF64=1"
1241 run_dump_test "note-2-64"
1242 run_dump_test "note-3-64"
1243 run_dump_test "note-4-64"
1244 run_dump_test "note-6-64"
1246 run_dump_test "note-2-32"
1247 run_dump_test "note-3-32"
1248 run_dump_test "note-4-32"
1249 run_dump_test "note-6-32"
1251 run_dump_test "note-5"
1254 run_dump_test "copy-2"
1255 run_dump_test "copy-3"
1256 run_dump_test "copy-4"
1257 run_dump_test "copy-5"
1258 run_dump_test "copy-6"
1260 # Use bintest.o from the copy-4 test to determine ELF reloc type
1261 set reloc_format rel
1262 if { [is_elf_format] && [is_rela tmpdir/bintest.o] } {
1263 set reloc_format rela
1265 run_dump_test "pr19020a"
1266 run_dump_test "pr19020b"
1268 if [is_elf_format] {
1269 run_dump_test "strip-1"
1270 run_dump_test "strip-2"
1271 run_dump_test "strip-3"
1272 run_dump_test "strip-4"
1273 run_dump_test "strip-5"
1274 run_dump_test "strip-6"
1275 run_dump_test "strip-7"
1276 run_dump_test "strip-8"
1277 run_dump_test "strip-9"
1278 run_dump_test "strip-12"
1280 if { [istarget "mips64*-*-openbsd*"] } {
1281 set reloc_format mips64
1283 # A relocation type not supported by any target
1284 if { [istarget "nds32*-*"] } {
1289 run_dump_test "strip-13" [list \
1290 [list source strip-13${reloc_format}.s] \
1291 [list as "${elf64} --defsym RELOC=${reloc}"]]
1292 # Select a relocation number that corresponds to one actually
1293 # supported by the target and ABI being tested.
1294 if { [istarget "aarch64*-*"] } {
1296 } elseif { [istarget "ia64*-*"] \
1297 || [istarget "m32r*-*"] \
1298 || [istarget "nds32*-*"] \
1299 || [istarget "v850*-*"] } {
1301 } elseif { [istarget "pru-*"] } {
1306 run_dump_test "strip-14" [list \
1307 [list source strip-14${reloc_format}.s] \
1308 [list as "${elf64} --defsym RELOC=${reloc}"]]
1309 run_dump_test "strip-15" [list \
1310 [list source strip-15${reloc_format}.s] \
1311 [list as "${elf64} --defsym RELOC=${reloc}"]]
1313 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1314 if { [supports_gnu_unique] } {
1315 run_dump_test "strip-10"
1317 run_dump_test "strip-11"
1319 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1320 # Check to make sure we don't strip a symbol named in relocations.
1321 set test "objcopy keeps symbols needed by relocs"
1323 set srcfile $srcdir/$subdir/needed-by-reloc.s
1325 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1328 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1330 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1338 # The symbol table for some MIPS targets is sorted differently than
1339 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1341 if { [is_bad_symtab] } then {
1344 run_dump_test "localize-hidden-1"
1345 run_dump_test "testranges"
1346 run_dump_test "testranges-ia64"
1348 run_dump_test "add-section"
1349 run_dump_test "add-symbol"
1350 run_dump_test "add-empty-section"
1352 run_dump_test "exclude-1a"
1353 run_dump_test "exclude-1b"
1355 run_dump_test "only-section-01"
1356 run_dump_test "remove-section-01"
1358 run_dump_test "keep-section-1"
1359 run_dump_test "keep-section-2"
1361 # Test the remove relocation functionality
1362 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1363 foreach t $test_list {
1364 # We need to strip the ".d", but can leave the dirname.
1365 verbose [file rootname $t]
1366 run_dump_test [file rootname $t]
1369 run_dump_test "localize-hidden-2"
1371 # Test objcopying an object file without global symbol
1373 proc objcopy_test_without_global_symbol { } {
1381 set test "strip without global symbol "
1383 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1388 if [is_remote host] {
1389 set objfile [remote_download host tmpdir/pr19547.o]
1391 set objfile tmpdir/pr19547.o
1394 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1395 set exec_output [prune_warnings $exec_output]
1396 if ![string equal "" $exec_output] {
1401 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1402 set exec_output [prune_warnings $exec_output]
1403 if {![regexp "no symbols" $exec_output]} {
1411 # The AArch64 and ARM targets preserve mapping symbols
1412 # in object files, so they will fail this test.
1413 setup_xfail aarch64*-*-* arm*-*-*
1415 objcopy_test_without_global_symbol
1417 # objcopy remove relocation from executable test
1419 proc objcopy_remove_relocations_from_executable { } {
1425 set test "remove-section relocation sections"
1427 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } {
1432 if [is_remote host] {
1433 set objfile [remote_download host tmpdir/pr23611]
1435 set objfile tmpdir/pr23611
1437 set out tmpdir/pr23611.out
1439 set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"]
1441 set exec_output1 [prune_warnings $exec_output1]
1442 if ![string equal "" $exec_output1] {
1447 set exec_output2 [binutils_run $READELF "-S $out"]
1448 if { [string match "*.rel.plt*" $exec_output2] || [string match "*.rela.plt*" $exec_output2] || [string match "*.rel.dyn*" $exec_output2] || [string match "*.rela.dyn*" $exec_output2] } {
1455 if [is_elf_format] {
1456 objcopy_remove_relocations_from_executable
1459 run_dump_test "pr23633"
1461 run_dump_test "set-section-alignment"
1463 setup_xfail "hppa*-*-*"
1464 setup_xfail "sh-*-coff*"
1465 setup_xfail "spu-*-*"
1466 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1467 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1468 if { [istarget pdp11-*-*] } {
1469 set src "pr25662-pdp11.s"
1474 set ldflags "-T$srcdir/$subdir/pr25662.ld"
1475 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
1476 append ldflags " --disable-reloc-section"
1479 #xcoff doesn't support arbitrary sections
1480 if { ![is_xcoff_format] } {
1481 objcopy_test "pr25662" $src executable "" $ldflags
1484 run_dump_test "rename-section-01"