1 # Copyright (C) 1994-2021 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 unresolved "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 unresolved "objcopy $type ($testname)"
79 set xflags "--preserve-dates"
82 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $xflags $t_tempfile $t_copyfile"]
84 if ![string equal "" $got] then {
86 fail "objcopy $type ($testname)"
88 send_log "cmp $t_tempfile $t_copyfile\n"
89 verbose "cmp $t_tempfile $t_copyfile"
91 set src1 tmpdir/bintest
93 remote_upload host $t_tempfile $src1
94 remote_upload host $t_copyfile $src2
99 set status [remote_exec build cmp "${src1} ${src2}"]
100 set exec_output [lindex $status 1]
101 set exec_output [prune_warnings $exec_output]
103 if [string equal "" $exec_output] then {
104 pass "objcopy $type ($testname)"
106 send_log "$exec_output\n"
107 verbose "$exec_output" 1
109 # On OSF/1, this succeeds with gas and fails with /bin/as.
110 setup_xfail "alpha*-*-osf*"
112 fail "objcopy $type ($testname)"
117 setup_xfail "hppa*-*-*"
118 setup_xfail "sh-*-coff*"
119 setup_xfail "tic54x-*-*"
120 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
121 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
122 objcopy_test "simple copy" bintest.s object "" ""
124 # Test verilog data width
125 proc objcopy_test_verilog {testname} {
131 set binfile tmpdir/verilogtest.o
132 set verilog tmpdir/verilog
134 set got [binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]
135 if {![binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]} then {
136 unresolved "objcopy ($testname)"
140 set got [binutils_run $OBJCOPY "-O verilog $binfile $verilog"]
141 if ![string equal "" $got] then {
142 fail "objcopy ($testname)"
145 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"]
146 if ![regexp "verilog data width must be at least 1 byte" $got] then {
147 fail "objcopy ($testname 0) {$got}"
149 pass "objcopy ($testname 0)"
152 foreach width {1 2 4 8} {
153 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
154 if ![string equal "" $got] then {
155 fail "objcopy ($testname $width)"
157 send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
158 if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
159 pass "objcopy ($testname $width)"
161 fail "objcopy ($testname $width)"
166 objcopy_test_verilog "verilog data width"
168 if { [file exists $tempfile] } {
169 # Test reversing bytes in a section.
171 set reversed ${tempfile}-reversed
172 set sect_names [get_standard_section_names]
173 if { $sect_names != "" } {
174 if { [istarget hppa*-*-hpux*] } {
175 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j \$PRIVATE\$ -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
177 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
180 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
183 if ![string equal "" $got] then {
184 fail "objcopy --reverse-bytes"
186 if [is_remote host] {
187 remote_upload host ${reversed} tmpdir/copy-reversed.o
188 set reversed tmpdir/copy-reversed.o
191 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
192 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
194 set want "^ \[0-9\]+ (\[0-9\]+)"
195 set found_orig [regexp -lineanchor $want $origdata -> origdata]
196 set found_rev [regexp -lineanchor $want $revdata -> revdata]
198 if {$found_orig == 0 || $found_rev == 0} then {
199 fail "objcopy --reverse-bytes"
201 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
202 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
204 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
205 pass "objcopy --reverse-bytes"
207 fail "objcopy --reverse-bytes"
213 # Test interleaved copy of multiple byte width
215 set sequence_file sequence_file
216 set file [open ${sequence_file} w]
217 puts ${file} "12345678"
220 if [is_remote host] {
221 remote_upload host ${sequence_file} tmpdir/sequence_file
222 set sequence_file tmpdir/sequence_file
225 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
227 if ![string equal "" $got] then {
228 fail "objcopy -i --interleave-width"
230 if [is_remote host] {
231 remote_upload host ${copyfile} tmpdir/interleave_output
232 set interleave_output tmpdir/interleave_output
234 set interleave_output ${copyfile}
237 set file [open ${interleave_output} r]
242 if ![string equal "1256" $line] then {
243 fail "objcopy -i --interleave-width"
245 pass "objcopy -i --interleave-width"
250 # Test generating S records.
252 if { [file exists $tempfile] } {
253 # We make the srec filename 8.3 compatible. Note that the header string
254 # matched against depends on the name of the file. Ugh.
256 if [is_remote host] {
257 set srecfile copy.sre
258 set header_string S00B0000636F70792E737265C1
260 set srecfile ${copyfile}.srec
261 set header_string S0130000746D706469722F636F70792E7372656397
264 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
266 if ![string equal "" $got] then {
267 fail "objcopy -O srec"
269 if [is_remote host] {
270 remote_upload host ${srecfile} tmpdir/copy.srec
271 set srecfile tmpdir/copy.srec
273 set file [open ${srecfile} r]
275 # The first S record is fixed by the file name we are using.
279 if ![regexp "$header_string.*" $line] {
280 send_log "bad header\n"
281 fail "objcopy -O srec"
283 while {[gets $file line] != -1 \
284 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
291 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
292 send_log "bad trailer\n"
293 fail "objcopy -O srec"
295 if {[gets $file line] != -1} then {
296 send_log "garbage at end\n"
299 fail "objcopy -O srec"
301 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
302 if ![regexp "file format srec" $got] then {
303 send_log "objdump failed\n"
304 fail "objcopy -O srec"
306 pass "objcopy -O srec"
316 # Test setting and adjusting the start address. We only test this
317 # while generating S records, because we may not be able to set the
318 # start address for other object file formats, and the S record case
319 # is the only useful one anyhow.
321 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
322 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
323 perror "objdump can not recognize bintest.o"
326 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
327 if ![string equal "" $got] then {
328 fail "objcopy --set-start"
330 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
331 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
332 fail "objcopy --set-start"
334 if {$srecstart != 0x7654} then {
335 send_log "$srecstart != 0x7654\n"
336 fail "objcopy --set-start"
338 pass "objcopy --set-start"
343 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
344 if ![string equal "" $got] then {
345 fail "objcopy --adjust-start"
347 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
348 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
349 fail "objcopy --adjust-start"
351 if {$srecstart != $origstart + 0x123} then {
352 send_log "$srecstart != $origstart + 0x123\n"
353 fail "objcopy --adjust-start"
355 pass "objcopy --adjust-start"
361 # Test adjusting the overall VMA, and adjusting the VMA of a
362 # particular section. We again only test this when generating S
368 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
370 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
373 while {[regexp $headers_regexp $got all name size vma rest]} {
377 if {$low == "" || $vma < $low} {
385 if {$low == "" || $origstart == ""} then {
386 perror "objdump can not recognize bintest.o"
388 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
389 if ![string equal "" $got] then {
390 fail "objcopy --adjust-vma"
392 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
393 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
394 if ![regexp $want $got all start vma] then {
395 fail "objcopy --adjust-vma"
398 if {$vma != $low + 0x123} then {
399 send_log "$vma != $low + 0x123\n"
400 fail "objcopy --adjust-vma"
402 if {$start != $origstart + 0x123} then {
403 send_log "$start != $origstart + 0x123\n"
404 fail "objcopy --adjust-vma"
406 pass "objcopy --adjust-vma"
414 while {[regexp $headers_regexp $got all name size vma rest]} {
416 if {$vma == $low} then {
417 set arg "$arg --adjust-section-vma $name+4"
422 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
423 if ![string equal "" $got] then {
424 fail "objcopy --adjust-section-vma +"
426 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
427 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
428 if ![regexp $want $got all vma] then {
429 fail "objcopy --adjust-section-vma +"
432 if {$vma != $low + 4} then {
433 send_log "$vma != $low + 4\n"
434 fail "objcopy --adjust-section-vma +"
436 pass "objcopy --adjust-section-vma +"
441 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
442 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
443 if ![string equal "" $got] then {
444 fail "objcopy --adjust-section-vma ="
446 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
447 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
448 if ![regexp $want $got all vma] then {
449 fail "objcopy --adjust-section-vma ="
452 if {$vma != $low + 4} then {
453 send_log "$vma != $low + 4\n"
454 fail "objcopy --adjust-section-vma ="
456 pass "objcopy --adjust-section-vma ="
462 # Test stripping an object.
464 proc strip_test { } {
477 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
482 set osabi_fail "false"
484 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
485 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
486 if { [lindex $exec_output 0] != 0 } then {
487 unresolved "$test preserving OS/ABI"
488 set osabi_fail "true"
490 verbose -log "grep OS/ABI tmpdir/osabi.in"
491 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
495 if [is_remote host] {
496 set archive libstrip.a
497 set objfile [remote_download host tmpdir/testprog.o]
498 remote_file host delete $archive
500 set archive tmpdir/libstrip.a
501 set objfile tmpdir/testprog.o
504 remote_file build delete tmpdir/libstrip.a
506 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
507 set exec_output [prune_warnings $exec_output]
508 if ![string equal "" $exec_output] {
510 unresolved "$test preserving OS/ABI"
514 set exec_output [binutils_run $STRIP "-g $archive"]
515 set exec_output [prune_warnings $exec_output]
516 if ![string equal "" $exec_output] {
518 unresolved "$test preserving OS/ABI"
522 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
523 set exec_output [prune_warnings $exec_output]
524 if ![string equal "" $exec_output] {
526 unresolved "$test preserving OS/ABI"
530 if { $osabi_fail != "true" && [is_elf_format] } {
531 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
532 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
533 if { [lindex $exec_output 0] != 0 } then {
534 unresolved "$test preserving OS/ABI"
536 verbose -log "grep OS/ABI tmpdir/osabi.out"
537 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
538 if { "$osabi_in" == "$osabi_out" } {
539 pass "$test preserving OS/ABI"
541 fail "$test preserving OS/ABI"
546 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
551 if [is_remote host] {
552 set objfile [remote_download host tmpdir/testprog.o]
554 set objfile tmpdir/testprog.o
557 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
558 set exec_output [prune_warnings $exec_output]
559 if ![string equal "" $exec_output] {
564 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
565 set exec_output [prune_warnings $exec_output]
566 if ![string match "*: no symbols*" $exec_output] {
576 # Test stripping an object file with saving a symbol
578 proc strip_test_with_saving_a_symbol { } {
587 set test "strip with saving a symbol"
589 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
594 if [is_remote host] {
595 set objfile [remote_download host tmpdir/testprog.o]
597 set objfile tmpdir/testprog.o
600 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
601 set exec_output [prune_warnings $exec_output]
602 if ![string equal "" $exec_output] {
607 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
608 set exec_output [prune_warnings $exec_output]
609 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
610 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
618 strip_test_with_saving_a_symbol
620 # Build a final executable.
622 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
623 set test_prog "testprog.exe"
625 set test_prog "testprog"
628 proc copy_setup { } {
635 set res [build_wrapper testglue.o]
638 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } {
643 lappend flags "additional_flags=[lindex $res 1]"
644 set add_libs "testglue.o"
649 if { [istarget *-*-linux*]
650 || [istarget *-*-gnu*] } {
651 foreach i $gcc_gas_flag {
652 set flags "additional_flags=$i $flags"
655 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
659 set result [remote_load target tmpdir/$test_prog]
660 set status [lindex $result 0]
662 if { $status != "pass" } {
663 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
670 # Test copying an executable.
672 proc copy_executable { prog flags test1 test2 } {
675 if [is_remote host] {
676 set testfile [remote_download host tmpdir/$test_prog]
677 set testcopy copyprog
679 set testfile tmpdir/$test_prog
680 set testcopy tmpdir/copyprog
682 remote_file host delete $testcopy
684 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
686 if ![string equal "" $exec_output] {
688 if [string equal "" $test2] {
695 if [is_remote host] {
696 remote_upload host $testcopy tmpdir/copyprog
699 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
700 set exec_output [lindex $status 1]
702 if [string equal "" $exec_output] then {
705 send_log "$exec_output\n"
706 verbose "$exec_output"
708 # This will fail for many reasons. For example, it will most
709 # likely fail if a non-GNU linker is used. Therefore, we do
710 # not insist that it pass. If you are using an assembler and
711 # linker based on the same BFD as objcopy, it is worth
712 # investigating to see why this failure occurs. If we are
713 # cross compiling, we assume that a GNU linker is being used,
714 # and expect it to succeed.
715 if {[isnative]} then {
719 # This also fails for some mips targets. See elf32-mips.c
720 # mips_elf_sym_is_global.
721 if { [is_bad_symtab] } then {
725 setup_xfail "arm*-*-pe"
726 setup_xfail "*-*-mingw*"
727 setup_xfail "*-*-cygwin*"
732 if [string equal "" $test2] {
736 set output [remote_load target tmpdir/copyprog]
737 set status [lindex $output 0]
738 if { $status != "pass" } {
745 # Test stripping an executable
747 proc strip_executable { prog flags test1 test2 } {
752 remote_file build delete tmpdir/striprog
753 remote_download build tmpdir/copyprog tmpdir/striprog
754 if [is_remote host] {
755 set copyfile [remote_download host tmpdir/striprog]
757 set copyfile tmpdir/striprog
760 set osabi_fail "false"
762 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
763 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
764 if { [lindex $exec_output 0] != 0 } then {
765 unresolved "$test1 preserving OS/ABI"
766 set osabi_fail "true"
768 verbose -log "grep OS/ABI tmpdir/osabi.in"
769 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
773 set exec_output [binutils_run $prog "$flags ${copyfile}"]
774 if ![string equal "" $exec_output] {
776 if [string equal "" $test2] {
783 if [is_remote host] {
784 remote_upload host ${copyfile} tmpdir/striprog
787 if { $osabi_fail != "true" && [is_elf_format] } {
788 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
789 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
790 if { [lindex $exec_output 0] != 0 } then {
791 unresolved "$test1 preserving OS/ABI"
793 verbose -log "grep OS/ABI tmpdir/osabi.out"
794 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
795 if { "$osabi_in" == "$osabi_out" } {
796 pass "$test1 preserving OS/ABI"
798 fail "$test1 preserving OS/ABI"
803 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
804 if ![string match "*: no symbols*" $exec_output] {
809 if [string equal "" $test2] {
813 set result [remote_load target tmpdir/striprog]
814 set status [lindex $result 0]
815 if { $status != "pass" } {
823 # Test stripping an executable with saving a symbol
825 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
829 remote_file build delete tmpdir/striprog
830 remote_download build tmpdir/copyprog tmpdir/striprog
831 if [is_remote host] {
832 set copyfile [remote_download host tmpdir/striprog]
834 set copyfile tmpdir/striprog
837 set exec_output [binutils_run $prog "$flags ${copyfile}"]
838 if ![string equal "" $exec_output] {
840 if [string equal "" $test2] {
847 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
848 if { [istarget mmix-knuth-mmixware] } {
849 # Whenever there's a symbol in the mmo format, there's the symbol
850 # Main, so remove it manually from the expected output for sake of
853 # Using "" not {} to get the \n and \r translated.
854 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
857 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
858 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
863 if [string equal "" $test2] {
867 if [is_remote host] {
868 remote_upload host ${copyfile} tmpdir/striprog
871 set result [remote_load target tmpdir/striprog]
872 set status [lindex $result 0]
873 if { $status != "pass" } {
881 # Test keeping only debug symbols of an executable
883 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
884 remote_file build delete tmpdir/striprog
885 remote_download build tmpdir/copyprog tmpdir/striprog
886 if [is_remote host] {
887 set copyfile [remote_download host tmpdir/striprog]
889 set copyfile tmpdir/striprog
892 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
893 if ![string equal "" $exec_output] {
899 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
900 if ![string equal "" $exec_output] {
907 # Tests that in a debug only copy of a file the sections
908 # headers whose types have been changed to NOBITS still
909 # retain their sh_link fields.
911 proc keep_debug_symbols_and_check_links { prog flags test } {
914 remote_file build delete tmpdir/striprog
915 remote_download build tmpdir/copyprog tmpdir/striprog
916 if [is_remote host] {
917 set copyfile [remote_download host tmpdir/striprog]
919 set copyfile tmpdir/striprog
922 set exec_output [binutils_run $prog "$flags ${copyfile}"]
923 if ![string equal "" $exec_output] {
928 set got [binutils_run $READELF "-S --wide ${copyfile}"]
931 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
933 {[^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]+)(.*)} \
934 $got all name link rest]} {
937 # Only some NOBITS sections should have a non-zero sh_link field.
938 # Look for them by name.
939 verbose "NOBITS section .$name has a 0 sh_link field\n"
941 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
942 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
943 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
944 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
945 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
946 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
947 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
948 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
962 set test1 "simple objcopy of executable"
963 set test1r "run objcopy of executable"
964 set test2 "strip executable"
965 set test2r "run stripped executable"
966 set test3 "strip executable with saving a symbol"
967 set test3r "run stripped executable with saving a symbol"
968 set test4 "keep only debug data"
969 set test5 "simple objcopy of debug data"
971 set test6 "NOBITS sections retain sh_link field"
974 switch [copy_setup] {
992 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
994 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
996 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
998 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
999 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1000 if [is_elf_format] {
1001 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1005 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
1006 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
1007 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
1008 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1009 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1010 if [is_elf_format] {
1011 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1016 proc objcopy_test_readelf {testname srcfile} {
1023 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
1024 unresolved "objcopy ($testname)"
1028 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
1029 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
1030 if { [lindex $exec_output 0] != 0
1031 || ![string equal "" [lindex $exec_output 1]] } then {
1032 fail "objcopy ($testname)"
1036 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
1037 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
1038 if { [lindex $exec_output 0] != 0 } then {
1039 unresolved "objcopy ($testname)"
1042 set exec_output [prune_warnings [lindex $exec_output 1]]
1043 if ![string equal "" $exec_output] then {
1044 unresolved "objcopy ($testname)"
1048 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
1049 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
1050 if { [lindex $exec_output 0] != 0 } then {
1051 unresolved "objcopy ($testname)"
1054 set exec_output [prune_warnings [lindex $exec_output 1]]
1055 if ![string equal "" $exec_output] then {
1056 unresolved "objcopy ($testname)"
1060 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
1061 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
1062 set exec_output [prune_warnings $exec_output]
1064 if [string equal "" $exec_output] then {
1065 pass "objcopy ($testname)"
1067 fail "objcopy ($testname)"
1071 proc objcopy_test_symbol_manipulation {} {
1075 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1076 foreach t $test_list {
1077 # We need to strip the ".d", but can leave the dirname.
1078 verbose [file rootname $t]
1079 run_dump_test [file rootname $t]
1083 proc objcopy_test_elf_common_symbols {} {
1087 # hpux has a non-standard common directive.
1088 if { [istarget "*-*-hpux*"] } then {
1092 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1093 foreach t $test_list {
1094 # We need to strip the ".d", but can leave the dirname.
1095 verbose [file rootname $t]
1096 run_dump_test [file rootname $t]
1100 # ia64 specific tests
1101 if { ([istarget "ia64-*-elf*"]
1102 || [istarget "ia64-*-linux*"]) } {
1103 objcopy_test "ia64 link order" link-order.s object "" ""
1106 # ELF specific tests
1108 if [is_elf_format] {
1109 objcopy_test_symbol_manipulation
1110 objcopy_test_elf_common_symbols
1112 setup_xfail "hppa*-*-*"
1113 setup_xfail "sh-*-coff*"
1114 setup_xfail "tic54x-*-*"
1115 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1116 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1117 objcopy_test "ELF unknown section type" unknown.s object "" ""
1119 objcopy_test_readelf "ELF group 1" group.s
1120 objcopy_test_readelf "ELF group 2" group-2.s
1121 objcopy_test_readelf "ELF group 3" group-3.s
1122 objcopy_test_readelf "ELF group 4" group-4.s
1123 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1124 run_dump_test "group-5"
1125 run_dump_test "group-6"
1126 run_dump_test "group-7a"
1127 run_dump_test "group-7b"
1128 run_dump_test "group-7c"
1129 run_dump_test "copy-1"
1130 run_dump_test "note-1"
1131 # Use copytest.o from the note-1 test to determine ELF32 or ELF64
1132 if [is_elf64 tmpdir/copytest.o] {
1133 set elf64 "--defsym ELF64=1"
1134 run_dump_test "note-2-64"
1135 run_dump_test "note-3-64"
1136 run_dump_test "note-4-64"
1137 run_dump_test "note-6-64"
1139 run_dump_test "note-2-32"
1140 run_dump_test "note-3-32"
1141 run_dump_test "note-4-32"
1142 run_dump_test "note-6-32"
1144 run_dump_test "note-5"
1147 run_dump_test "copy-2"
1148 run_dump_test "copy-3"
1149 run_dump_test "copy-4"
1150 run_dump_test "copy-5"
1151 run_dump_test "copy-6"
1153 # Use bintest.o from the copy-4 test to determine ELF reloc type
1154 set reloc_format rel
1155 if { [is_elf_format] && [is_rela tmpdir/bintest.o] } {
1156 set reloc_format rela
1158 run_dump_test "pr19020a"
1159 run_dump_test "pr19020b"
1161 if [is_elf_format] {
1162 run_dump_test "strip-1"
1163 run_dump_test "strip-2"
1164 run_dump_test "strip-3"
1165 run_dump_test "strip-4"
1166 run_dump_test "strip-5"
1167 run_dump_test "strip-6"
1168 run_dump_test "strip-7"
1169 run_dump_test "strip-8"
1170 run_dump_test "strip-9"
1171 run_dump_test "strip-12"
1173 if { [istarget "mips64*-*-openbsd*"] } {
1174 set reloc_format mips64
1176 # A relocation type not supported by any target
1177 if { [istarget "nds32*-*"] } {
1182 run_dump_test "strip-13" [list \
1183 [list source strip-13${reloc_format}.s] \
1184 [list as "${elf64} --defsym RELOC=${reloc}"]]
1185 # Select a relocation number that corresponds to one actually
1186 # supported by the target and ABI being tested.
1187 if { [istarget "aarch64*-*"] } {
1189 } elseif { [istarget "ia64*-*"] \
1190 || [istarget "m32r*-*"] \
1191 || [istarget "nds32*-*"] \
1192 || [istarget "v850*-*"] } {
1194 } elseif { [istarget "pru-*"] } {
1199 run_dump_test "strip-14" [list \
1200 [list source strip-14${reloc_format}.s] \
1201 [list as "${elf64} --defsym RELOC=${reloc}"]]
1202 run_dump_test "strip-15" [list \
1203 [list source strip-15${reloc_format}.s] \
1204 [list as "${elf64} --defsym RELOC=${reloc}"]]
1206 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1207 if { [supports_gnu_unique] } {
1208 run_dump_test "strip-10"
1210 run_dump_test "strip-11"
1212 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1213 # Check to make sure we don't strip a symbol named in relocations.
1214 set test "objcopy keeps symbols needed by relocs"
1216 set srcfile $srcdir/$subdir/needed-by-reloc.s
1218 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1221 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1223 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1231 # The symbol table for some MIPS targets is sorted differently than
1232 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1234 if { [is_bad_symtab] } then {
1237 run_dump_test "localize-hidden-1"
1238 run_dump_test "testranges"
1239 run_dump_test "testranges-ia64"
1241 run_dump_test "add-section"
1242 run_dump_test "add-symbol"
1243 run_dump_test "add-empty-section"
1245 run_dump_test "exclude-1a"
1246 run_dump_test "exclude-1b"
1248 run_dump_test "only-section-01"
1249 run_dump_test "remove-section-01"
1251 run_dump_test "keep-section-1"
1252 run_dump_test "keep-section-2"
1254 # Test the remove relocation functionality
1255 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1256 foreach t $test_list {
1257 # We need to strip the ".d", but can leave the dirname.
1258 verbose [file rootname $t]
1259 run_dump_test [file rootname $t]
1262 run_dump_test "localize-hidden-2"
1264 # Test objcopying an object file without global symbol
1266 proc objcopy_test_without_global_symbol { } {
1274 set test "strip without global symbol "
1276 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1281 if [is_remote host] {
1282 set objfile [remote_download host tmpdir/pr19547.o]
1284 set objfile tmpdir/pr19547.o
1287 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1288 set exec_output [prune_warnings $exec_output]
1289 if ![string equal "" $exec_output] {
1294 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1295 set exec_output [prune_warnings $exec_output]
1296 if {![regexp "no symbols" $exec_output]} {
1304 # The AArch64 and ARM targets preserve mapping symbols
1305 # in object files, so they will fail this test.
1306 setup_xfail aarch64*-*-* arm*-*-*
1308 objcopy_test_without_global_symbol
1310 # objcopy remove relocation from executable test
1312 proc objcopy_remove_relocations_from_executable { } {
1318 set test "remove-section relocation sections"
1320 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } {
1325 if [is_remote host] {
1326 set objfile [remote_download host tmpdir/pr23611]
1328 set objfile tmpdir/pr23611
1330 set out tmpdir/pr23611.out
1332 set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"]
1333 set exec_output2 [binutils_run $READELF "-S $out"]
1334 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] } {
1341 objcopy_remove_relocations_from_executable
1343 run_dump_test "pr23633"
1345 run_dump_test "set-section-alignment"
1347 setup_xfail "hppa*-*-*"
1348 setup_xfail "sh-*-coff*"
1349 setup_xfail "spu-*-*"
1350 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1351 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1352 if { [istarget pdp11-*-*] } {
1353 set src "pr25662-pdp11.s"
1358 set ldflags "-T$srcdir/$subdir/pr25662.ld"
1359 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
1360 append ldflags " --disable-reloc-section"
1363 #xcoff doesn't support arbitrary sections
1364 if { ![is_xcoff_format] } {
1365 objcopy_test "pr25662" $src executable "" $ldflags