1 # Copyright (C) 1994-2018 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:
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.
41 proc objcopy_test {testname srcfile} {
49 if {![binutils_assemble $srcdir/$subdir/${srcfile} $tempfile]} then {
50 unresolved "objcopy ($testname)"
51 remote_file host delete $tempfile
55 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
57 if ![string equal "" $got] then {
58 fail "objcopy ($testname)"
60 send_log "cmp $tempfile ${copyfile}.o\n"
61 verbose "cmp $tempfile ${copyfile}.o"
63 set src1 tmpdir/bintest.o
64 set src2 tmpdir/copy.o
65 remote_upload host $tempfile $src1
66 remote_upload host ${copyfile}.o $src2
69 set src2 ${copyfile}.o
71 set status [remote_exec build cmp "${src1} ${src2}"]
72 set exec_output [lindex $status 1]
73 set exec_output [prune_warnings $exec_output]
75 # On some systems the result of objcopy will not be identical.
76 # Usually this is just because gas isn't using bfd to write the
77 # files in the first place, and may order things a little
78 # differently. Those systems should use setup_xfail here.
80 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
81 setup_xfail "hppa*-*-*"
82 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
83 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
85 setup_xfail "sh-*-coff*"
86 setup_xfail "tic80-*-*" "w65-*"
88 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
89 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
90 clear_xfail "m68*-*-sysv4*"
92 if [string equal "" $exec_output] then {
93 pass "objcopy ($testname)"
95 send_log "$exec_output\n"
96 verbose "$exec_output" 1
98 # On OSF/1, this succeeds with gas and fails with /bin/as.
99 setup_xfail "alpha*-*-osf*"
101 fail "objcopy ($testname)"
106 objcopy_test "simple copy" bintest.s
108 if { [file exists $tempfile] } {
109 # Test reversing bytes in a section.
111 set reversed ${tempfile}-reversed
112 set sect_names [get_standard_section_names]
113 if { $sect_names != "" } {
114 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
119 if ![string equal "" $got] then {
120 fail "objcopy --reverse-bytes"
122 if [is_remote host] {
123 remote_upload host ${reversed} tmpdir/copy-reversed.o
124 set reversed tmpdir/copy-reversed.o
127 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
128 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
130 set want "^ \[0-9\]+ (\[0-9\]+)"
131 set found_orig [regexp -lineanchor $want $origdata -> origdata]
132 set found_rev [regexp -lineanchor $want $revdata -> revdata]
134 if {$found_orig == 0 || $found_rev == 0} then {
135 # som doesn't have a .data section
136 setup_xfail "hppa*-*-hpux*"
137 clear_xfail "hppa*64*-*-hpux*"
139 fail "objcopy --reverse-bytes"
141 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
142 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
144 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
145 pass "objcopy --reverse-bytes"
147 fail "objcopy --reverse-bytes"
153 # Test interleaved copy of multiple byte width
155 set sequence_file sequence_file
156 set file [open ${sequence_file} w]
157 puts ${file} "12345678"
160 if [is_remote host] {
161 remote_upload host ${sequence_file} tmpdir/sequence_file
162 set sequence_file tmpdir/sequence_file
165 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
167 if ![string equal "" $got] then {
168 fail "objcopy -i --interleave-width"
170 if [is_remote host] {
171 remote_upload host ${copyfile} tmpdir/interleave_output
172 set interleave_output tmpdir/interleave_output
174 set interleave_output ${copyfile}
177 set file [open ${interleave_output} r]
182 if ![string equal "1256" $line] then {
183 fail "objcopy -i --interleave-width"
185 pass "objcopy -i --interleave-width"
190 # Test generating S records.
192 if { [file exists $tempfile] } {
193 # We make the srec filename 8.3 compatible. Note that the header string
194 # matched against depends on the name of the file. Ugh.
196 if [is_remote host] {
197 set srecfile copy.sre
198 set header_string S00B0000636F70792E737265C1
200 set srecfile ${copyfile}.srec
201 set header_string S0130000746D706469722F636F70792E7372656397
204 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
206 if ![string equal "" $got] then {
207 fail "objcopy -O srec"
209 if [is_remote host] {
210 remote_upload host ${srecfile} tmpdir/copy.srec
211 set srecfile tmpdir/copy.srec
213 set file [open ${srecfile} r]
215 # The first S record is fixed by the file name we are using.
219 if ![regexp "$header_string.*" $line] {
220 send_log "bad header\n"
221 fail "objcopy -O srec"
223 while {[gets $file line] != -1 \
224 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
231 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
232 send_log "bad trailer\n"
233 fail "objcopy -O srec"
235 if {[gets $file line] != -1} then {
236 send_log "garbage at end\n"
239 fail "objcopy -O srec"
241 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
242 if ![regexp "file format srec" $got] then {
243 send_log "objdump failed\n"
244 fail "objcopy -O srec"
246 pass "objcopy -O srec"
256 # Test setting and adjusting the start address. We only test this
257 # while generating S records, because we may not be able to set the
258 # start address for other object file formats, and the S record case
259 # is the only useful one anyhow.
261 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
262 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
263 perror "objdump can not recognize bintest.o"
266 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
267 if ![string equal "" $got] then {
268 fail "objcopy --set-start"
270 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
271 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
272 fail "objcopy --set-start"
274 if {$srecstart != 0x7654} then {
275 send_log "$srecstart != 0x7654\n"
276 fail "objcopy --set-start"
278 pass "objcopy --set-start"
283 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
284 if ![string equal "" $got] then {
285 fail "objcopy --adjust-start"
287 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
288 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
289 fail "objcopy --adjust-start"
291 if {$srecstart != $origstart + 0x123} then {
292 send_log "$srecstart != $origstart + 0x123\n"
293 fail "objcopy --adjust-start"
295 pass "objcopy --adjust-start"
301 # Test adjusting the overall VMA, and adjusting the VMA of a
302 # particular section. We again only test this when generating S
308 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
310 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
313 while {[regexp $headers_regexp $got all name size vma rest]} {
317 if {$low == "" || $vma < $low} {
325 if {$low == "" || $origstart == ""} then {
326 perror "objdump can not recognize bintest.o"
328 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
329 if ![string equal "" $got] then {
330 fail "objcopy --adjust-vma"
332 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
333 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
334 if ![regexp $want $got all start vma] then {
335 fail "objcopy --adjust-vma"
338 if {$vma != $low + 0x123} then {
339 send_log "$vma != $low + 0x123\n"
340 fail "objcopy --adjust-vma"
342 if {$start != $origstart + 0x123} then {
343 send_log "$start != $origstart + 0x123\n"
344 fail "objcopy --adjust-vma"
346 pass "objcopy --adjust-vma"
354 while {[regexp $headers_regexp $got all name size vma rest]} {
356 if {$vma == $low} then {
357 set arg "$arg --adjust-section-vma $name+4"
362 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
363 if ![string equal "" $got] then {
364 fail "objcopy --adjust-section-vma +"
366 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
367 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
368 if ![regexp $want $got all vma] then {
369 fail "objcopy --adjust-section-vma +"
372 if {$vma != $low + 4} then {
373 send_log "$vma != $low + 4\n"
374 fail "objcopy --adjust-section-vma +"
376 pass "objcopy --adjust-section-vma +"
381 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
382 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
383 if ![string equal "" $got] then {
384 fail "objcopy --adjust-section-vma ="
386 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
387 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
388 if ![regexp $want $got all vma] then {
389 fail "objcopy --adjust-section-vma ="
392 if {$vma != $low + 4} then {
393 send_log "$vma != $low + 4\n"
394 fail "objcopy --adjust-section-vma ="
396 pass "objcopy --adjust-section-vma ="
402 # Test stripping an object.
404 proc strip_test { } {
417 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
422 set osabi_fail "false"
424 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
425 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
426 if { [lindex $exec_output 0] != 0 } then {
427 unresolved "$test preserving OS/ABI"
428 set osabi_fail "true"
430 verbose -log "grep OS/ABI tmpdir/osabi.in"
431 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
435 if [is_remote host] {
436 set archive libstrip.a
437 set objfile [remote_download host tmpdir/testprog.o]
438 remote_file host delete $archive
440 set archive tmpdir/libstrip.a
441 set objfile tmpdir/testprog.o
444 remote_file build delete tmpdir/libstrip.a
446 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
447 if ![string equal "" $exec_output] {
449 unresolved "$test preserving OS/ABI"
453 set exec_output [binutils_run $STRIP "-g $archive"]
454 if ![string equal "" $exec_output] {
456 unresolved "$test preserving OS/ABI"
460 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
461 if ![string equal "" $exec_output] {
463 unresolved "$test preserving OS/ABI"
467 if { $osabi_fail != "true" && [is_elf_format] } {
468 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
469 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
470 if { [lindex $exec_output 0] != 0 } then {
471 unresolved "$test preserving OS/ABI"
473 verbose -log "grep OS/ABI tmpdir/osabi.out"
474 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
475 if { "$osabi_in" == "$osabi_out" } {
476 pass "$test preserving OS/ABI"
478 fail "$test preserving OS/ABI"
483 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
488 if [is_remote host] {
489 set objfile [remote_download host tmpdir/testprog.o]
491 set objfile tmpdir/testprog.o
494 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
495 if ![string equal "" $exec_output] {
500 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
501 if ![string match "*: no symbols*" $exec_output] {
511 # Test stripping an object file with saving a symbol
513 proc strip_test_with_saving_a_symbol { } {
522 set test "strip with saving a symbol"
524 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
529 if [is_remote host] {
530 set objfile [remote_download host tmpdir/testprog.o]
532 set objfile tmpdir/testprog.o
535 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
536 if ![string equal "" $exec_output] {
541 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
542 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
543 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
551 strip_test_with_saving_a_symbol
553 # Build a final executable.
555 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
556 set test_prog "testprog.exe"
558 set test_prog "testprog"
561 proc copy_setup { } {
568 set res [build_wrapper testglue.o]
571 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
576 lappend flags "additional_flags=[lindex $res 1]"
577 set add_libs "testglue.o"
582 if { [istarget *-*-linux*]
583 || [istarget *-*-gnu*] } {
584 foreach i $gcc_gas_flag {
585 set flags "additional_flags=$i $flags"
588 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
592 set result [remote_load target tmpdir/$test_prog]
593 set status [lindex $result 0]
595 if { $status != "pass" } {
596 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
603 # Test copying an executable.
605 proc copy_executable { prog flags test1 test2 } {
608 if [is_remote host] {
609 set testfile [remote_download host tmpdir/$test_prog]
610 set testcopy copyprog
612 set testfile tmpdir/$test_prog
613 set testcopy tmpdir/copyprog
615 remote_file host delete $testcopy
617 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
619 if ![string equal "" $exec_output] {
621 if [string equal "" $test2] {
628 if [is_remote host] {
629 remote_upload host $testcopy tmpdir/copyprog
632 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
633 set exec_output [lindex $status 1]
635 if [string equal "" $exec_output] then {
638 send_log "$exec_output\n"
639 verbose "$exec_output"
641 # This will fail for many reasons. For example, it will most
642 # likely fail if a non-GNU linker is used. Therefore, we do
643 # not insist that it pass. If you are using an assembler and
644 # linker based on the same BFD as objcopy, it is worth
645 # investigating to see why this failure occurs. If we are
646 # cross compiling, we assume that a GNU linker is being used,
647 # and expect it to succeed.
648 if {[isnative]} then {
652 # This also fails for some mips targets. See elf32-mips.c
653 # mips_elf_sym_is_global.
654 if { [is_bad_symtab] } then {
658 setup_xfail "arm*-*-coff"
659 setup_xfail "arm*-*-pe"
660 setup_xfail "*-*-mingw*"
661 setup_xfail "*-*-cygwin*"
666 if [string equal "" $test2] {
670 set output [remote_load target tmpdir/copyprog]
671 set status [lindex $output 0]
672 if { $status != "pass" } {
679 # Test stripping an executable
681 proc strip_executable { prog flags test1 test2 } {
686 remote_file build delete tmpdir/striprog
687 remote_download build tmpdir/copyprog tmpdir/striprog
688 if [is_remote host] {
689 set copyfile [remote_download host tmpdir/striprog]
691 set copyfile tmpdir/striprog
694 set osabi_fail "false"
696 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
697 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
698 if { [lindex $exec_output 0] != 0 } then {
699 unresolved "$test1 preserving OS/ABI"
700 set osabi_fail "true"
702 verbose -log "grep OS/ABI tmpdir/osabi.in"
703 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
707 set exec_output [binutils_run $prog "$flags ${copyfile}"]
708 if ![string equal "" $exec_output] {
710 if [string equal "" $test2] {
717 if [is_remote host] {
718 remote_upload host ${copyfile} tmpdir/striprog
721 if { $osabi_fail != "true" && [is_elf_format] } {
722 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
723 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
724 if { [lindex $exec_output 0] != 0 } then {
725 unresolved "$test1 preserving OS/ABI"
727 verbose -log "grep OS/ABI tmpdir/osabi.out"
728 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
729 if { "$osabi_in" == "$osabi_out" } {
730 pass "$test1 preserving OS/ABI"
732 fail "$test1 preserving OS/ABI"
737 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
738 if ![string match "*: no symbols*" $exec_output] {
743 if [string equal "" $test2] {
747 set result [remote_load target tmpdir/striprog]
748 set status [lindex $result 0]
749 if { $status != "pass" } {
757 # Test stripping an executable with saving a symbol
759 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
763 remote_file build delete tmpdir/striprog
764 remote_download build tmpdir/copyprog tmpdir/striprog
765 if [is_remote host] {
766 set copyfile [remote_download host tmpdir/striprog]
768 set copyfile tmpdir/striprog
771 set exec_output [binutils_run $prog "$flags ${copyfile}"]
772 if ![string equal "" $exec_output] {
774 if [string equal "" $test2] {
781 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
782 if { [istarget mmix-knuth-mmixware] } {
783 # Whenever there's a symbol in the mmo format, there's the symbol
784 # Main, so remove it manually from the expected output for sake of
787 # Using "" not {} to get the \n and \r translated.
788 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
791 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
792 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
797 if [string equal "" $test2] {
801 if [is_remote host] {
802 remote_upload host ${copyfile} tmpdir/striprog
805 set result [remote_load target tmpdir/striprog]
806 set status [lindex $result 0]
807 if { $status != "pass" } {
815 # Test keeping only debug symbols of an executable
817 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
818 remote_file build delete tmpdir/striprog
819 remote_download build tmpdir/copyprog tmpdir/striprog
820 if [is_remote host] {
821 set copyfile [remote_download host tmpdir/striprog]
823 set copyfile tmpdir/striprog
826 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
827 if ![string equal "" $exec_output] {
833 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
834 if ![string equal "" $exec_output] {
841 # Tests that in a debug only copy of a file the sections
842 # headers whose types have been changed to NOBITS still
843 # retain their sh_link fields.
845 proc keep_debug_symbols_and_check_links { prog flags test } {
848 remote_file build delete tmpdir/striprog
849 remote_download build tmpdir/copyprog tmpdir/striprog
850 if [is_remote host] {
851 set copyfile [remote_download host tmpdir/striprog]
853 set copyfile tmpdir/striprog
856 set exec_output [binutils_run $prog "$flags ${copyfile}"]
857 if ![string equal "" $exec_output] {
862 set got [binutils_run $READELF "-S --wide ${copyfile}"]
865 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
867 {[^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]+)(.*)} \
868 $got all name link rest]} {
871 # Only some NOBITS sections should have a non-zero sh_link field.
872 # Look for them by name.
873 verbose "NOBITS section .$name has a 0 sh_link field\n"
875 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
876 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
877 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
882 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
896 set test1 "simple objcopy of executable"
897 set test1r "run objcopy of executable"
898 set test2 "strip executable"
899 set test2r "run stripped executable"
900 set test3 "strip executable with saving a symbol"
901 set test3r "run stripped executable with saving a symbol"
902 set test4 "keep only debug data"
903 set test5 "simple objcopy of debug data"
905 set test6 "NOBITS sections retain sh_link field"
908 switch [copy_setup] {
926 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
928 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
930 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
932 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
933 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
935 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
939 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
940 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
941 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
942 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
943 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
945 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
950 proc objcopy_test_readelf {testname srcfile} {
957 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
958 unresolved "objcopy ($testname)"
962 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
963 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
964 if { [lindex $exec_output 0] != 0
965 || ![string equal "" [lindex $exec_output 1]] } then {
966 fail "objcopy ($testname)"
970 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
971 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
972 if { [lindex $exec_output 0] != 0 } then {
973 unresolved "objcopy ($testname)"
976 set exec_output [prune_warnings [lindex $exec_output 1]]
977 if ![string equal "" $exec_output] then {
978 unresolved "objcopy ($testname)"
982 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
983 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
984 if { [lindex $exec_output 0] != 0 } then {
985 unresolved "objcopy ($testname)"
988 set exec_output [prune_warnings [lindex $exec_output 1]]
989 if ![string equal "" $exec_output] then {
990 unresolved "objcopy ($testname)"
994 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
995 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
996 set exec_output [prune_warnings $exec_output]
998 if [string equal "" $exec_output] then {
999 pass "objcopy ($testname)"
1001 fail "objcopy ($testname)"
1005 proc objcopy_test_symbol_manipulation {} {
1009 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1010 foreach t $test_list {
1011 # We need to strip the ".d", but can leave the dirname.
1012 verbose [file rootname $t]
1013 run_dump_test [file rootname $t]
1017 proc objcopy_test_elf_common_symbols {} {
1021 # hpux has a non-standard common directive.
1022 if { [istarget "*-*-hpux*"] } then {
1026 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1027 foreach t $test_list {
1028 # We need to strip the ".d", but can leave the dirname.
1029 verbose [file rootname $t]
1030 run_dump_test [file rootname $t]
1034 # ia64 specific tests
1035 if { ([istarget "ia64-*-elf*"]
1036 || [istarget "ia64-*-linux*"]) } {
1037 objcopy_test "ia64 link order" link-order.s
1040 # ELF specific tests
1041 if [is_elf_format] {
1042 objcopy_test_symbol_manipulation
1043 objcopy_test_elf_common_symbols
1044 objcopy_test "ELF unknown section type" unknown.s
1045 objcopy_test_readelf "ELF group" group.s
1046 objcopy_test_readelf "ELF group" group-2.s
1047 objcopy_test_readelf "ELF group" group-3.s
1048 objcopy_test_readelf "ELF group" group-4.s
1049 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1050 run_dump_test "group-5"
1051 run_dump_test "group-6"
1052 run_dump_test "group-7a"
1053 run_dump_test "group-7b"
1054 run_dump_test "group-7c"
1055 run_dump_test "copy-1"
1056 run_dump_test "note-1"
1057 if [is_elf64 tmpdir/bintest.o] {
1058 run_dump_test "note-2-64"
1059 run_dump_test "note-3-64"
1060 run_dump_test "note-4-64"
1062 run_dump_test "note-2-32"
1063 run_dump_test "note-3-32"
1064 run_dump_test "note-4-32"
1068 run_dump_test "copy-2"
1069 run_dump_test "copy-3"
1070 run_dump_test "copy-4"
1071 run_dump_test "pr19020a"
1072 run_dump_test "pr19020b"
1074 if [is_elf_format] {
1075 run_dump_test "strip-1"
1076 run_dump_test "strip-2"
1077 run_dump_test "strip-3"
1078 run_dump_test "strip-4"
1079 run_dump_test "strip-5"
1080 run_dump_test "strip-6"
1081 run_dump_test "strip-7"
1082 run_dump_test "strip-8"
1083 run_dump_test "strip-9"
1084 run_dump_test "strip-12"
1086 if { [istarget "mips64*-*-openbsd*"] } {
1087 set reloc_format mips64
1088 } elseif { [istarget "arm-*"] \
1089 || [istarget "d10v-*"] \
1090 || [istarget "dlx-*"] \
1091 || [istarget "i*86-*"] \
1092 || [istarget "m681*-*"] \
1093 || [istarget "m68hc1*-*"] \
1094 || ([istarget "mips*-*"] \
1095 && ![istarget "mips64*-ps2-elf*"] \
1096 && ![istarget "*-*-irix6*"] \
1097 && ![istarget "mips64*-*-freebsd*"] \
1098 && ![istarget "mips64*-*-kfreebsd*-gnu"] \
1099 && ![istarget "mips64*-*-linux*"]) \
1100 || [istarget "score*-*"] \
1101 || [istarget "xgate-*"] } {
1102 set reloc_format rel
1104 set reloc_format rela
1106 run_dump_test "strip-13" [list [list source strip-13${reloc_format}.s]]
1107 # Select a relocation number that corresponds to one actually
1108 # supported by the target and ABI being tested.
1109 if { [istarget "aarch64*-*"] } {
1111 } elseif { [istarget "ia64*-*"] \
1112 || [istarget "m32r*-*"] \
1113 || [istarget "nds32*-*"] \
1114 || [istarget "v850*-*"] } {
1116 } elseif { [istarget "pru-*"] } {
1121 run_dump_test "strip-14" [list \
1122 [list source strip-14${reloc_format}.s] \
1123 [list as "--defsym RELOC=${reloc}"] \
1124 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1125 ? "--defsym ELF64=1" : ""}]]]
1126 run_dump_test "strip-15" [list \
1127 [list source strip-15${reloc_format}.s] \
1128 [list as "--defsym RELOC=${reloc}"] \
1129 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1130 ? "--defsym ELF64=1" : ""}]]]
1132 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1133 if { [supports_gnu_unique] } {
1134 run_dump_test "strip-10"
1136 set extra_strip11 ""
1137 if { [istarget "sh64*-*"] } {
1139 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1141 run_dump_test "strip-11" $extra_strip11
1143 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1144 # Check to make sure we don't strip a symbol named in relocations.
1145 set test "objcopy keeps symbols needed by relocs"
1147 set srcfile $srcdir/$subdir/needed-by-reloc.s
1149 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1152 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1154 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1162 # The symbol table for some MIPS targets is sorted differently than
1163 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1165 if { [is_bad_symtab] } then {
1168 run_dump_test "localize-hidden-1"
1169 run_dump_test "testranges"
1170 run_dump_test "testranges-ia64"
1172 run_dump_test "add-section"
1173 run_dump_test "add-symbol"
1174 run_dump_test "add-empty-section"
1176 run_dump_test "exclude-1a"
1177 run_dump_test "exclude-1b"
1179 run_dump_test "only-section-01"
1180 run_dump_test "remove-section-01"
1182 # Test the remove relocation functionality
1183 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1184 foreach t $test_list {
1185 # We need to strip the ".d", but can leave the dirname.
1186 verbose [file rootname $t]
1187 run_dump_test [file rootname $t]
1190 run_dump_test "localize-hidden-2"
1192 # Test objcopying an object file without global symbol
1194 proc objcopy_test_without_global_symbol { } {
1202 set test "strip without global symbol "
1204 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1209 if [is_remote host] {
1210 set objfile [remote_download host tmpdir/pr19547.o]
1212 set objfile tmpdir/pr19547.o
1215 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1216 if ![string equal "" $exec_output] {
1221 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1222 if {![regexp "no symbols" $exec_output]} {
1230 # The AArch64 and ARM targets preserve mapping symbols
1231 # in object files, so they will fail this test.
1232 setup_xfail aarch64*-*-* arm*-*-*
1234 objcopy_test_without_global_symbol