1 # Copyright (C) 1994-2020 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 {
85 fail "objcopy $type ($testname)"
87 send_log "cmp $t_tempfile $t_copyfile\n"
88 verbose "cmp $t_tempfile $t_copyfile"
90 set src1 tmpdir/bintest
92 remote_upload host $t_tempfile $src1
93 remote_upload host $t_copyfile $src2
98 set status [remote_exec build cmp "${src1} ${src2}"]
99 set exec_output [lindex $status 1]
100 set exec_output [prune_warnings $exec_output]
102 if [string equal "" $exec_output] then {
103 pass "objcopy $type ($testname)"
105 send_log "$exec_output\n"
106 verbose "$exec_output" 1
108 # On OSF/1, this succeeds with gas and fails with /bin/as.
109 setup_xfail "alpha*-*-osf*"
111 fail "objcopy $type ($testname)"
116 setup_xfail "hppa*-*-*"
117 setup_xfail "sh-*-coff*"
118 setup_xfail "tic54x-*-*"
119 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
120 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
121 objcopy_test "simple copy" bintest.s object "" ""
123 # Test verilog data width
124 proc objcopy_test_verilog {testname} {
130 set binfile tmpdir/verilogtest.o
131 set verilog tmpdir/verilog
133 set got [binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]
134 if {![binutils_assemble $srcdir/$subdir/verilogtest.s $binfile]} then {
135 unresolved "objcopy ($testname)"
139 set got [binutils_run $OBJCOPY "-O verilog $binfile $verilog"]
140 if ![string equal "" $got] then {
141 fail "objcopy ($testname)"
144 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"]
145 if ![regexp "verilog data width must be at least 1 byte" $got] then {
146 fail "objcopy ($testname 0) {$got}"
148 pass "objcopy ($testname 0)"
151 foreach width {1 2 4 8} {
152 set got [binutils_run $OBJCOPY "-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
153 if ![string equal "" $got] then {
154 fail "objcopy ($testname $width)"
156 send_log "regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
157 if {! [regexp_diff "$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
158 pass "objcopy ($testname $width)"
160 fail "objcopy ($testname $width)"
165 objcopy_test_verilog "verilog data width"
167 if { [file exists $tempfile] } {
168 # Test reversing bytes in a section.
170 set reversed ${tempfile}-reversed
171 set sect_names [get_standard_section_names]
172 if { $sect_names != "" } {
173 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
175 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
178 if ![string equal "" $got] then {
179 fail "objcopy --reverse-bytes"
181 if [is_remote host] {
182 remote_upload host ${reversed} tmpdir/copy-reversed.o
183 set reversed tmpdir/copy-reversed.o
186 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
187 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
189 set want "^ \[0-9\]+ (\[0-9\]+)"
190 set found_orig [regexp -lineanchor $want $origdata -> origdata]
191 set found_rev [regexp -lineanchor $want $revdata -> revdata]
193 if {$found_orig == 0 || $found_rev == 0} then {
194 # som doesn't have a .data section
195 setup_xfail "hppa*-*-hpux*"
196 clear_xfail "hppa*64*-*-hpux*"
198 fail "objcopy --reverse-bytes"
200 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
201 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
203 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
204 pass "objcopy --reverse-bytes"
206 fail "objcopy --reverse-bytes"
212 # Test interleaved copy of multiple byte width
214 set sequence_file sequence_file
215 set file [open ${sequence_file} w]
216 puts ${file} "12345678"
219 if [is_remote host] {
220 remote_upload host ${sequence_file} tmpdir/sequence_file
221 set sequence_file tmpdir/sequence_file
224 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
226 if ![string equal "" $got] then {
227 fail "objcopy -i --interleave-width"
229 if [is_remote host] {
230 remote_upload host ${copyfile} tmpdir/interleave_output
231 set interleave_output tmpdir/interleave_output
233 set interleave_output ${copyfile}
236 set file [open ${interleave_output} r]
241 if ![string equal "1256" $line] then {
242 fail "objcopy -i --interleave-width"
244 pass "objcopy -i --interleave-width"
249 # Test generating S records.
251 if { [file exists $tempfile] } {
252 # We make the srec filename 8.3 compatible. Note that the header string
253 # matched against depends on the name of the file. Ugh.
255 if [is_remote host] {
256 set srecfile copy.sre
257 set header_string S00B0000636F70792E737265C1
259 set srecfile ${copyfile}.srec
260 set header_string S0130000746D706469722F636F70792E7372656397
263 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
265 if ![string equal "" $got] then {
266 fail "objcopy -O srec"
268 if [is_remote host] {
269 remote_upload host ${srecfile} tmpdir/copy.srec
270 set srecfile tmpdir/copy.srec
272 set file [open ${srecfile} r]
274 # The first S record is fixed by the file name we are using.
278 if ![regexp "$header_string.*" $line] {
279 send_log "bad header\n"
280 fail "objcopy -O srec"
282 while {[gets $file line] != -1 \
283 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
290 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
291 send_log "bad trailer\n"
292 fail "objcopy -O srec"
294 if {[gets $file line] != -1} then {
295 send_log "garbage at end\n"
298 fail "objcopy -O srec"
300 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
301 if ![regexp "file format srec" $got] then {
302 send_log "objdump failed\n"
303 fail "objcopy -O srec"
305 pass "objcopy -O srec"
315 # Test setting and adjusting the start address. We only test this
316 # while generating S records, because we may not be able to set the
317 # start address for other object file formats, and the S record case
318 # is the only useful one anyhow.
320 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
321 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
322 perror "objdump can not recognize bintest.o"
325 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
326 if ![string equal "" $got] then {
327 fail "objcopy --set-start"
329 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
330 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
331 fail "objcopy --set-start"
333 if {$srecstart != 0x7654} then {
334 send_log "$srecstart != 0x7654\n"
335 fail "objcopy --set-start"
337 pass "objcopy --set-start"
342 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
343 if ![string equal "" $got] then {
344 fail "objcopy --adjust-start"
346 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
347 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
348 fail "objcopy --adjust-start"
350 if {$srecstart != $origstart + 0x123} then {
351 send_log "$srecstart != $origstart + 0x123\n"
352 fail "objcopy --adjust-start"
354 pass "objcopy --adjust-start"
360 # Test adjusting the overall VMA, and adjusting the VMA of a
361 # particular section. We again only test this when generating S
367 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
369 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
372 while {[regexp $headers_regexp $got all name size vma rest]} {
376 if {$low == "" || $vma < $low} {
384 if {$low == "" || $origstart == ""} then {
385 perror "objdump can not recognize bintest.o"
387 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
388 if ![string equal "" $got] then {
389 fail "objcopy --adjust-vma"
391 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
392 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
393 if ![regexp $want $got all start vma] then {
394 fail "objcopy --adjust-vma"
397 if {$vma != $low + 0x123} then {
398 send_log "$vma != $low + 0x123\n"
399 fail "objcopy --adjust-vma"
401 if {$start != $origstart + 0x123} then {
402 send_log "$start != $origstart + 0x123\n"
403 fail "objcopy --adjust-vma"
405 pass "objcopy --adjust-vma"
413 while {[regexp $headers_regexp $got all name size vma rest]} {
415 if {$vma == $low} then {
416 set arg "$arg --adjust-section-vma $name+4"
421 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
422 if ![string equal "" $got] then {
423 fail "objcopy --adjust-section-vma +"
425 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
426 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
427 if ![regexp $want $got all vma] then {
428 fail "objcopy --adjust-section-vma +"
431 if {$vma != $low + 4} then {
432 send_log "$vma != $low + 4\n"
433 fail "objcopy --adjust-section-vma +"
435 pass "objcopy --adjust-section-vma +"
440 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
441 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
442 if ![string equal "" $got] then {
443 fail "objcopy --adjust-section-vma ="
445 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
446 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
447 if ![regexp $want $got all vma] then {
448 fail "objcopy --adjust-section-vma ="
451 if {$vma != $low + 4} then {
452 send_log "$vma != $low + 4\n"
453 fail "objcopy --adjust-section-vma ="
455 pass "objcopy --adjust-section-vma ="
461 # Test stripping an object.
463 proc strip_test { } {
476 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
481 set osabi_fail "false"
483 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
484 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
485 if { [lindex $exec_output 0] != 0 } then {
486 unresolved "$test preserving OS/ABI"
487 set osabi_fail "true"
489 verbose -log "grep OS/ABI tmpdir/osabi.in"
490 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
494 if [is_remote host] {
495 set archive libstrip.a
496 set objfile [remote_download host tmpdir/testprog.o]
497 remote_file host delete $archive
499 set archive tmpdir/libstrip.a
500 set objfile tmpdir/testprog.o
503 remote_file build delete tmpdir/libstrip.a
505 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
506 set exec_output [prune_warnings $exec_output]
507 if ![string equal "" $exec_output] {
509 unresolved "$test preserving OS/ABI"
513 set exec_output [binutils_run $STRIP "-g $archive"]
514 set exec_output [prune_warnings $exec_output]
515 if ![string equal "" $exec_output] {
517 unresolved "$test preserving OS/ABI"
521 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
522 set exec_output [prune_warnings $exec_output]
523 if ![string equal "" $exec_output] {
525 unresolved "$test preserving OS/ABI"
529 if { $osabi_fail != "true" && [is_elf_format] } {
530 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
531 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
532 if { [lindex $exec_output 0] != 0 } then {
533 unresolved "$test preserving OS/ABI"
535 verbose -log "grep OS/ABI tmpdir/osabi.out"
536 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
537 if { "$osabi_in" == "$osabi_out" } {
538 pass "$test preserving OS/ABI"
540 fail "$test preserving OS/ABI"
545 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
550 if [is_remote host] {
551 set objfile [remote_download host tmpdir/testprog.o]
553 set objfile tmpdir/testprog.o
556 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
557 set exec_output [prune_warnings $exec_output]
558 if ![string equal "" $exec_output] {
563 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
564 set exec_output [prune_warnings $exec_output]
565 if ![string match "*: no symbols*" $exec_output] {
575 # Test stripping an object file with saving a symbol
577 proc strip_test_with_saving_a_symbol { } {
586 set test "strip with saving a symbol"
588 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
593 if [is_remote host] {
594 set objfile [remote_download host tmpdir/testprog.o]
596 set objfile tmpdir/testprog.o
599 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
600 set exec_output [prune_warnings $exec_output]
601 if ![string equal "" $exec_output] {
606 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
607 set exec_output [prune_warnings $exec_output]
608 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
609 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
617 strip_test_with_saving_a_symbol
619 # Build a final executable.
621 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
622 set test_prog "testprog.exe"
624 set test_prog "testprog"
627 proc copy_setup { } {
634 set res [build_wrapper testglue.o]
637 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } {
642 lappend flags "additional_flags=[lindex $res 1]"
643 set add_libs "testglue.o"
648 if { [istarget *-*-linux*]
649 || [istarget *-*-gnu*] } {
650 foreach i $gcc_gas_flag {
651 set flags "additional_flags=$i $flags"
654 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
658 set result [remote_load target tmpdir/$test_prog]
659 set status [lindex $result 0]
661 if { $status != "pass" } {
662 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
669 # Test copying an executable.
671 proc copy_executable { prog flags test1 test2 } {
674 if [is_remote host] {
675 set testfile [remote_download host tmpdir/$test_prog]
676 set testcopy copyprog
678 set testfile tmpdir/$test_prog
679 set testcopy tmpdir/copyprog
681 remote_file host delete $testcopy
683 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
685 if ![string equal "" $exec_output] {
687 if [string equal "" $test2] {
694 if [is_remote host] {
695 remote_upload host $testcopy tmpdir/copyprog
698 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
699 set exec_output [lindex $status 1]
701 if [string equal "" $exec_output] then {
704 send_log "$exec_output\n"
705 verbose "$exec_output"
707 # This will fail for many reasons. For example, it will most
708 # likely fail if a non-GNU linker is used. Therefore, we do
709 # not insist that it pass. If you are using an assembler and
710 # linker based on the same BFD as objcopy, it is worth
711 # investigating to see why this failure occurs. If we are
712 # cross compiling, we assume that a GNU linker is being used,
713 # and expect it to succeed.
714 if {[isnative]} then {
718 # This also fails for some mips targets. See elf32-mips.c
719 # mips_elf_sym_is_global.
720 if { [is_bad_symtab] } then {
724 setup_xfail "arm*-*-pe"
725 setup_xfail "*-*-mingw*"
726 setup_xfail "*-*-cygwin*"
731 if [string equal "" $test2] {
735 set output [remote_load target tmpdir/copyprog]
736 set status [lindex $output 0]
737 if { $status != "pass" } {
744 # Test stripping an executable
746 proc strip_executable { prog flags test1 test2 } {
751 remote_file build delete tmpdir/striprog
752 remote_download build tmpdir/copyprog tmpdir/striprog
753 if [is_remote host] {
754 set copyfile [remote_download host tmpdir/striprog]
756 set copyfile tmpdir/striprog
759 set osabi_fail "false"
761 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
762 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
763 if { [lindex $exec_output 0] != 0 } then {
764 unresolved "$test1 preserving OS/ABI"
765 set osabi_fail "true"
767 verbose -log "grep OS/ABI tmpdir/osabi.in"
768 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
772 set exec_output [binutils_run $prog "$flags ${copyfile}"]
773 if ![string equal "" $exec_output] {
775 if [string equal "" $test2] {
782 if [is_remote host] {
783 remote_upload host ${copyfile} tmpdir/striprog
786 if { $osabi_fail != "true" && [is_elf_format] } {
787 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
788 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
789 if { [lindex $exec_output 0] != 0 } then {
790 unresolved "$test1 preserving OS/ABI"
792 verbose -log "grep OS/ABI tmpdir/osabi.out"
793 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
794 if { "$osabi_in" == "$osabi_out" } {
795 pass "$test1 preserving OS/ABI"
797 fail "$test1 preserving OS/ABI"
802 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
803 if ![string match "*: no symbols*" $exec_output] {
808 if [string equal "" $test2] {
812 set result [remote_load target tmpdir/striprog]
813 set status [lindex $result 0]
814 if { $status != "pass" } {
822 # Test stripping an executable with saving a symbol
824 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
828 remote_file build delete tmpdir/striprog
829 remote_download build tmpdir/copyprog tmpdir/striprog
830 if [is_remote host] {
831 set copyfile [remote_download host tmpdir/striprog]
833 set copyfile tmpdir/striprog
836 set exec_output [binutils_run $prog "$flags ${copyfile}"]
837 if ![string equal "" $exec_output] {
839 if [string equal "" $test2] {
846 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
847 if { [istarget mmix-knuth-mmixware] } {
848 # Whenever there's a symbol in the mmo format, there's the symbol
849 # Main, so remove it manually from the expected output for sake of
852 # Using "" not {} to get the \n and \r translated.
853 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
856 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
857 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
862 if [string equal "" $test2] {
866 if [is_remote host] {
867 remote_upload host ${copyfile} tmpdir/striprog
870 set result [remote_load target tmpdir/striprog]
871 set status [lindex $result 0]
872 if { $status != "pass" } {
880 # Test keeping only debug symbols of an executable
882 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
883 remote_file build delete tmpdir/striprog
884 remote_download build tmpdir/copyprog tmpdir/striprog
885 if [is_remote host] {
886 set copyfile [remote_download host tmpdir/striprog]
888 set copyfile tmpdir/striprog
891 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
892 if ![string equal "" $exec_output] {
898 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
899 if ![string equal "" $exec_output] {
906 # Tests that in a debug only copy of a file the sections
907 # headers whose types have been changed to NOBITS still
908 # retain their sh_link fields.
910 proc keep_debug_symbols_and_check_links { prog flags test } {
913 remote_file build delete tmpdir/striprog
914 remote_download build tmpdir/copyprog tmpdir/striprog
915 if [is_remote host] {
916 set copyfile [remote_download host tmpdir/striprog]
918 set copyfile tmpdir/striprog
921 set exec_output [binutils_run $prog "$flags ${copyfile}"]
922 if ![string equal "" $exec_output] {
927 set got [binutils_run $READELF "-S --wide ${copyfile}"]
930 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
932 {[^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]+)(.*)} \
933 $got all name link rest]} {
936 # Only some NOBITS sections should have a non-zero sh_link field.
937 # Look for them by name.
938 verbose "NOBITS section .$name has a 0 sh_link field\n"
940 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
941 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
942 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
943 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
944 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
945 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
946 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
947 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
961 set test1 "simple objcopy of executable"
962 set test1r "run objcopy of executable"
963 set test2 "strip executable"
964 set test2r "run stripped executable"
965 set test3 "strip executable with saving a symbol"
966 set test3r "run stripped executable with saving a symbol"
967 set test4 "keep only debug data"
968 set test5 "simple objcopy of debug data"
970 set test6 "NOBITS sections retain sh_link field"
973 switch [copy_setup] {
991 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
993 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
995 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
997 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
998 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1000 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1004 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
1005 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
1006 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
1007 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1008 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1009 if [is_elf_format] {
1010 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1015 proc objcopy_test_readelf {testname srcfile} {
1022 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
1023 unresolved "objcopy ($testname)"
1027 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
1028 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
1029 if { [lindex $exec_output 0] != 0
1030 || ![string equal "" [lindex $exec_output 1]] } then {
1031 fail "objcopy ($testname)"
1035 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
1036 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
1037 if { [lindex $exec_output 0] != 0 } then {
1038 unresolved "objcopy ($testname)"
1041 set exec_output [prune_warnings [lindex $exec_output 1]]
1042 if ![string equal "" $exec_output] then {
1043 unresolved "objcopy ($testname)"
1047 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
1048 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
1049 if { [lindex $exec_output 0] != 0 } then {
1050 unresolved "objcopy ($testname)"
1053 set exec_output [prune_warnings [lindex $exec_output 1]]
1054 if ![string equal "" $exec_output] then {
1055 unresolved "objcopy ($testname)"
1059 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
1060 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
1061 set exec_output [prune_warnings $exec_output]
1063 if [string equal "" $exec_output] then {
1064 pass "objcopy ($testname)"
1066 fail "objcopy ($testname)"
1070 proc objcopy_test_symbol_manipulation {} {
1074 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1075 foreach t $test_list {
1076 # We need to strip the ".d", but can leave the dirname.
1077 verbose [file rootname $t]
1078 run_dump_test [file rootname $t]
1082 proc objcopy_test_elf_common_symbols {} {
1086 # hpux has a non-standard common directive.
1087 if { [istarget "*-*-hpux*"] } then {
1091 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1092 foreach t $test_list {
1093 # We need to strip the ".d", but can leave the dirname.
1094 verbose [file rootname $t]
1095 run_dump_test [file rootname $t]
1099 # ia64 specific tests
1100 if { ([istarget "ia64-*-elf*"]
1101 || [istarget "ia64-*-linux*"]) } {
1102 objcopy_test "ia64 link order" link-order.s object "" ""
1105 # ELF specific tests
1107 if [is_elf_format] {
1108 objcopy_test_symbol_manipulation
1109 objcopy_test_elf_common_symbols
1111 setup_xfail "hppa*-*-*"
1112 setup_xfail "sh-*-coff*"
1113 setup_xfail "tic54x-*-*"
1114 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1115 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1116 objcopy_test "ELF unknown section type" unknown.s object "" ""
1118 objcopy_test_readelf "ELF group 1" group.s
1119 objcopy_test_readelf "ELF group 2" group-2.s
1120 objcopy_test_readelf "ELF group 3" group-3.s
1121 objcopy_test_readelf "ELF group 4" group-4.s
1122 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1123 run_dump_test "group-5"
1124 run_dump_test "group-6"
1125 run_dump_test "group-7a"
1126 run_dump_test "group-7b"
1127 run_dump_test "group-7c"
1128 run_dump_test "copy-1"
1129 run_dump_test "note-1"
1130 # Use copytest.o from the note-1 test to determine ELF32 or ELF64
1131 if [is_elf64 tmpdir/copytest.o] {
1132 set elf64 "--defsym ELF64=1"
1133 run_dump_test "note-2-64"
1134 run_dump_test "note-3-64"
1135 run_dump_test "note-4-64"
1136 run_dump_test "note-6-64"
1138 run_dump_test "note-2-32"
1139 run_dump_test "note-3-32"
1140 run_dump_test "note-4-32"
1141 run_dump_test "note-6-32"
1143 run_dump_test "note-5"
1146 run_dump_test "copy-2"
1147 run_dump_test "copy-3"
1148 run_dump_test "copy-4"
1149 run_dump_test "copy-5"
1150 run_dump_test "copy-6"
1152 # Use bintest.o from the copy-4 test to determine ELF reloc type
1153 set reloc_format rel
1154 if { [is_elf_format] && [is_rela tmpdir/bintest.o] } {
1155 set reloc_format rela
1157 run_dump_test "pr19020a"
1158 run_dump_test "pr19020b"
1160 if [is_elf_format] {
1161 run_dump_test "strip-1"
1162 run_dump_test "strip-2"
1163 run_dump_test "strip-3"
1164 run_dump_test "strip-4"
1165 run_dump_test "strip-5"
1166 run_dump_test "strip-6"
1167 run_dump_test "strip-7"
1168 run_dump_test "strip-8"
1169 run_dump_test "strip-9"
1170 run_dump_test "strip-12"
1172 if { [istarget "mips64*-*-openbsd*"] } {
1173 set reloc_format mips64
1175 # A relocation type not supported by any target
1176 if { [istarget "nds32*-*"] } {
1181 run_dump_test "strip-13" [list \
1182 [list source strip-13${reloc_format}.s] \
1183 [list as "${elf64} --defsym RELOC=${reloc}"]]
1184 # Select a relocation number that corresponds to one actually
1185 # supported by the target and ABI being tested.
1186 if { [istarget "aarch64*-*"] } {
1188 } elseif { [istarget "ia64*-*"] \
1189 || [istarget "m32r*-*"] \
1190 || [istarget "nds32*-*"] \
1191 || [istarget "v850*-*"] } {
1193 } elseif { [istarget "pru-*"] } {
1198 run_dump_test "strip-14" [list \
1199 [list source strip-14${reloc_format}.s] \
1200 [list as "${elf64} --defsym RELOC=${reloc}"]]
1201 run_dump_test "strip-15" [list \
1202 [list source strip-15${reloc_format}.s] \
1203 [list as "${elf64} --defsym RELOC=${reloc}"]]
1205 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1206 if { [supports_gnu_unique] } {
1207 run_dump_test "strip-10"
1209 run_dump_test "strip-11"
1211 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1212 # Check to make sure we don't strip a symbol named in relocations.
1213 set test "objcopy keeps symbols needed by relocs"
1215 set srcfile $srcdir/$subdir/needed-by-reloc.s
1217 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1220 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1222 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1230 # The symbol table for some MIPS targets is sorted differently than
1231 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1233 if { [is_bad_symtab] } then {
1236 run_dump_test "localize-hidden-1"
1237 run_dump_test "testranges"
1238 run_dump_test "testranges-ia64"
1240 run_dump_test "add-section"
1241 run_dump_test "add-symbol"
1242 run_dump_test "add-empty-section"
1244 run_dump_test "exclude-1a"
1245 run_dump_test "exclude-1b"
1247 run_dump_test "only-section-01"
1248 run_dump_test "remove-section-01"
1250 run_dump_test "keep-section-1"
1251 run_dump_test "keep-section-2"
1253 # Test the remove relocation functionality
1254 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1255 foreach t $test_list {
1256 # We need to strip the ".d", but can leave the dirname.
1257 verbose [file rootname $t]
1258 run_dump_test [file rootname $t]
1261 run_dump_test "localize-hidden-2"
1263 # Test objcopying an object file without global symbol
1265 proc objcopy_test_without_global_symbol { } {
1273 set test "strip without global symbol "
1275 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1280 if [is_remote host] {
1281 set objfile [remote_download host tmpdir/pr19547.o]
1283 set objfile tmpdir/pr19547.o
1286 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1287 set exec_output [prune_warnings $exec_output]
1288 if ![string equal "" $exec_output] {
1293 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1294 set exec_output [prune_warnings $exec_output]
1295 if {![regexp "no symbols" $exec_output]} {
1303 # The AArch64 and ARM targets preserve mapping symbols
1304 # in object files, so they will fail this test.
1305 setup_xfail aarch64*-*-* arm*-*-*
1307 objcopy_test_without_global_symbol
1309 # objcopy remove relocation from executable test
1311 proc objcopy_remove_relocations_from_executable { } {
1317 set test "remove-section relocation sections"
1319 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } {
1324 if [is_remote host] {
1325 set objfile [remote_download host tmpdir/pr23611]
1327 set objfile tmpdir/pr23611
1329 set out tmpdir/pr23611.out
1331 set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"]
1332 set exec_output2 [binutils_run $READELF "-S $out"]
1333 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] } {
1340 objcopy_remove_relocations_from_executable
1342 run_dump_test "pr23633"
1344 run_dump_test "set-section-alignment"
1346 setup_xfail "hppa*-*-*"
1347 setup_xfail "sh-*-coff*"
1348 setup_xfail "spu-*-*"
1349 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1350 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "hppa*-*-*elf*"
1351 if { [istarget pdp11-*-*] } {
1352 set src "pr25662-pdp11.s"
1357 objcopy_test "pr25662" $src executable "" "-T$srcdir/$subdir/pr25662.ld"