1 # Copyright (C) 1994-2016 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.
41 proc objcopy_test {testname srcfile} {
49 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
50 perror "unresolved $testname"
51 unresolved "objcopy ($testname)"
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 "h8300-*-coff"
81 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
82 setup_xfail "hppa*-*-*"
84 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
85 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
87 setup_xfail "sh-*-coff*"
88 setup_xfail "tic80-*-*" "w65-*"
90 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
91 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
92 clear_xfail "m68*-*-sysv4*"
94 if [string equal "" $exec_output] then {
95 pass "objcopy ($testname)"
97 send_log "$exec_output\n"
98 verbose "$exec_output" 1
100 # On OSF/1, this succeeds with gas and fails with /bin/as.
101 setup_xfail "alpha*-*-osf*"
103 fail "objcopy ($testname)"
108 objcopy_test "simple copy" bintest.s
110 # Test reversing bytes in a section.
112 set reversed ${tempfile}-reversed
113 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
115 if ![string equal "" $got] then {
116 fail "objcopy --reverse-bytes"
118 if [is_remote host] {
119 remote_upload host ${reversed} tmpdir/copy-reversed.o
120 set reversed tmpdir/copy-reversed.o
123 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
124 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
126 set want "^ \[0-9\]+ (\[0-9\]+)"
127 set found_orig [regexp -lineanchor $want $origdata -> origdata]
128 set found_rev [regexp -lineanchor $want $revdata -> revdata]
130 if {$found_orig == 0 || $found_rev == 0} then {
131 # som doesn't have a .data section
132 setup_xfail "hppa*-*-hpux*"
133 clear_xfail "hppa*64*-*-hpux*"
135 fail "objcopy --reverse-bytes"
137 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
138 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
140 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
141 pass "objcopy --reverse-bytes"
143 fail "objcopy --reverse-bytes"
148 # Test interleaved copy of multiple byte width
150 set sequence_file sequence_file
151 set file [open ${sequence_file} w]
152 puts ${file} "12345678"
155 if [is_remote host] {
156 remote_upload host ${sequence_file} tmpdir/sequence_file
157 set sequence_file tmpdir/sequence_file
160 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
162 if ![string equal "" $got] then {
163 fail "objcopy -i --interleave-width"
165 if [is_remote host] {
166 remote_upload host ${copyfile} tmpdir/interleave_output
167 set interleave_output tmpdir/interleave_output
169 set interleave_output ${copyfile}
172 set file [open ${interleave_output} r]
177 if ![string equal "1256" $line] then {
178 fail "objcopy -i --interleave-width"
180 pass "objcopy -i --interleave-width"
185 # Test generating S records.
187 # We make the srec filename 8.3 compatible. Note that the header string
188 # matched against depends on the name of the file. Ugh.
190 if [is_remote host] {
191 set srecfile copy.sre
192 set header_string S00B0000636F70792E737265C1
194 set srecfile ${copyfile}.srec
195 set header_string S0130000746D706469722F636F70792E7372656397
198 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
200 if ![string equal "" $got] then {
201 fail "objcopy -O srec"
203 if [is_remote host] {
204 remote_upload host ${srecfile} tmpdir/copy.srec
205 set srecfile tmpdir/copy.srec
207 set file [open ${srecfile} r]
209 # The first S record is fixed by the file name we are using.
213 if ![regexp "$header_string.*" $line] {
214 send_log "bad header\n"
215 fail "objcopy -O srec"
217 while {[gets $file line] != -1 \
218 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
225 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
226 send_log "bad trailer\n"
227 fail "objcopy -O srec"
229 if {[gets $file line] != -1} then {
230 send_log "garbage at end\n"
233 fail "objcopy -O srec"
235 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
236 if ![regexp "file format srec" $got] then {
237 send_log "objdump failed\n"
238 fail "objcopy -O srec"
240 pass "objcopy -O srec"
249 # Test setting and adjusting the start address. We only test this
250 # while generating S records, because we may not be able to set the
251 # start address for other object file formats, and the S record case
252 # is the only useful one anyhow.
254 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
255 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
256 perror "objdump can not recognize bintest.o"
259 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
260 if ![string equal "" $got] then {
261 fail "objcopy --set-start"
263 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
264 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
265 fail "objcopy --set-start"
267 if {$srecstart != 0x7654} then {
268 send_log "$srecstart != 0x7654\n"
269 fail "objcopy --set-start"
271 pass "objcopy --set-start"
276 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
277 if ![string equal "" $got] then {
278 fail "objcopy --adjust-start"
280 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
281 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
282 fail "objcopy --adjust-start"
284 if {$srecstart != $origstart + 0x123} then {
285 send_log "$srecstart != $origstart + 0x123\n"
286 fail "objcopy --adjust-start"
288 pass "objcopy --adjust-start"
294 # Test adjusting the overall VMA, and adjusting the VMA of a
295 # particular section. We again only test this when generating S
301 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
303 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
306 while {[regexp $headers_regexp $got all name size vma rest]} {
310 if {$low == "" || $vma < $low} {
318 if {$low == "" || $origstart == ""} then {
319 perror "objdump can not recognize bintest.o"
321 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
322 if ![string equal "" $got] then {
323 fail "objcopy --adjust-vma"
325 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
326 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
327 if ![regexp $want $got all start vma] then {
328 fail "objcopy --adjust-vma"
331 if {$vma != $low + 0x123} then {
332 send_log "$vma != $low + 0x123\n"
333 fail "objcopy --adjust-vma"
335 if {$start != $origstart + 0x123} then {
336 send_log "$start != $origstart + 0x123\n"
337 fail "objcopy --adjust-vma"
339 pass "objcopy --adjust-vma"
347 while {[regexp $headers_regexp $got all name size vma rest]} {
349 if {$vma == $low} then {
350 set arg "$arg --adjust-section-vma $name+4"
355 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
356 if ![string equal "" $got] then {
357 fail "objcopy --adjust-section-vma +"
359 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
360 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
361 if ![regexp $want $got all vma] then {
362 fail "objcopy --adjust-section-vma +"
365 if {$vma != $low + 4} then {
366 send_log "$vma != $low + 4\n"
367 fail "objcopy --adjust-section-vma +"
369 pass "objcopy --adjust-section-vma +"
374 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
375 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
376 if ![string equal "" $got] then {
377 fail "objcopy --adjust-section-vma ="
379 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
380 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
381 if ![regexp $want $got all vma] then {
382 fail "objcopy --adjust-section-vma ="
385 if {$vma != $low + 4} then {
386 send_log "$vma != $low + 4\n"
387 fail "objcopy --adjust-section-vma ="
389 pass "objcopy --adjust-section-vma ="
395 # Test stripping an object.
397 proc strip_test { } {
410 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
415 set osabi_fail "false"
417 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
418 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
419 if { [lindex $exec_output 0] != 0 } then {
420 unresolved "$test preserving OS/ABI"
421 set osabi_fail "true"
423 verbose -log "grep OS/ABI tmpdir/osabi.in"
424 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
428 if [is_remote host] {
429 set archive libstrip.a
430 set objfile [remote_download host tmpdir/testprog.o]
431 remote_file host delete $archive
433 set archive tmpdir/libstrip.a
434 set objfile tmpdir/testprog.o
437 remote_file build delete tmpdir/libstrip.a
439 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
440 if ![string equal "" $exec_output] {
442 unresolved "$test preserving OS/ABI"
446 set exec_output [binutils_run $STRIP "-g $archive"]
447 if ![string equal "" $exec_output] {
449 unresolved "$test preserving OS/ABI"
453 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
454 if ![string equal "" $exec_output] {
456 unresolved "$test preserving OS/ABI"
460 if { $osabi_fail != "true" && [is_elf_format] } {
461 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
462 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
463 if { [lindex $exec_output 0] != 0 } then {
464 unresolved "$test preserving OS/ABI"
466 verbose -log "grep OS/ABI tmpdir/osabi.out"
467 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
468 if { "$osabi_in" == "$osabi_out" } {
469 pass "$test preserving OS/ABI"
471 fail "$test preserving OS/ABI"
476 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
481 if [is_remote host] {
482 set objfile [remote_download host tmpdir/testprog.o]
484 set objfile tmpdir/testprog.o
487 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
488 if ![string equal "" $exec_output] {
493 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
494 if ![string match "*: no symbols*" $exec_output] {
504 # Test stripping an object file with saving a symbol
506 proc strip_test_with_saving_a_symbol { } {
515 set test "strip with saving a symbol"
517 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
522 if [is_remote host] {
523 set objfile [remote_download host tmpdir/testprog.o]
525 set objfile tmpdir/testprog.o
528 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
529 if ![string equal "" $exec_output] {
534 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
535 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
536 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
544 strip_test_with_saving_a_symbol
546 # Build a final executable.
548 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
549 set test_prog "testprog.exe"
551 set test_prog "testprog"
554 proc copy_setup { } {
561 set res [build_wrapper testglue.o]
564 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
569 lappend flags "additional_flags=[lindex $res 1]"
570 set add_libs "testglue.o"
575 if { [istarget *-*-linux*]
576 || [istarget *-*-gnu*] } {
577 foreach i $gcc_gas_flag {
578 set flags "additional_flags=$i $flags"
581 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
585 set result [remote_load target tmpdir/$test_prog]
586 set status [lindex $result 0]
588 if { $status != "pass" } {
589 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
596 # Test copying an executable.
598 proc copy_executable { prog flags test1 test2 } {
601 if [is_remote host] {
602 set testfile [remote_download host tmpdir/$test_prog]
603 set testcopy copyprog
605 set testfile tmpdir/$test_prog
606 set testcopy tmpdir/copyprog
608 remote_file host delete $testcopy
610 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
612 if ![string equal "" $exec_output] {
614 if [string equal "" $test2] {
621 if [is_remote host] {
622 remote_upload host $testcopy tmpdir/copyprog
625 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
626 set exec_output [lindex $status 1]
628 if [string equal "" $exec_output] then {
631 send_log "$exec_output\n"
632 verbose "$exec_output"
634 # This will fail for many reasons. For example, it will most
635 # likely fail if a non-GNU linker is used. Therefore, we do
636 # not insist that it pass. If you are using an assembler and
637 # linker based on the same BFD as objcopy, it is worth
638 # investigating to see why this failure occurs. If we are
639 # cross compiling, we assume that a GNU linker is being used,
640 # and expect it to succeed.
641 if {[isnative]} then {
645 # This also fails for mips*-*-elf targets. See elf32-mips.c
646 # mips_elf_sym_is_global.
647 setup_xfail "mips*-*-elf"
649 setup_xfail "arm*-*-coff"
650 setup_xfail "arm*-*-pe"
651 setup_xfail "*-*-mingw*"
652 setup_xfail "*-*-cygwin*"
657 if [string equal "" $test2] {
661 set output [remote_load target tmpdir/copyprog]
662 set status [lindex $output 0]
663 if { $status != "pass" } {
670 # Test stripping an executable
672 proc strip_executable { prog flags test1 test2 } {
677 remote_file build delete tmpdir/striprog
678 remote_download build tmpdir/copyprog tmpdir/striprog
679 if [is_remote host] {
680 set copyfile [remote_download host tmpdir/striprog]
682 set copyfile tmpdir/striprog
685 set osabi_fail "false"
687 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
688 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
689 if { [lindex $exec_output 0] != 0 } then {
690 unresolved "$test1 preserving OS/ABI"
691 set osabi_fail "true"
693 verbose -log "grep OS/ABI tmpdir/osabi.in"
694 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
698 set exec_output [binutils_run $prog "$flags ${copyfile}"]
699 if ![string equal "" $exec_output] {
701 if [string equal "" $test2] {
708 if [is_remote host] {
709 remote_upload host ${copyfile} tmpdir/striprog
712 if { $osabi_fail != "true" && [is_elf_format] } {
713 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
714 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
715 if { [lindex $exec_output 0] != 0 } then {
716 unresolved "$test1 preserving OS/ABI"
718 verbose -log "grep OS/ABI tmpdir/osabi.out"
719 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
720 if { "$osabi_in" == "$osabi_out" } {
721 pass "$test1 preserving OS/ABI"
723 fail "$test1 preserving OS/ABI"
728 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
729 if ![string match "*: no symbols*" $exec_output] {
734 if [string equal "" $test2] {
738 set result [remote_load target tmpdir/striprog]
739 set status [lindex $result 0]
740 if { $status != "pass" } {
748 # Test stripping an executable with saving a symbol
750 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
754 remote_file build delete tmpdir/striprog
755 remote_download build tmpdir/copyprog tmpdir/striprog
756 if [is_remote host] {
757 set copyfile [remote_download host tmpdir/striprog]
759 set copyfile tmpdir/striprog
762 set exec_output [binutils_run $prog "$flags ${copyfile}"]
763 if ![string equal "" $exec_output] {
765 if [string equal "" $test2] {
772 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
773 if { [istarget mmix-knuth-mmixware] } {
774 # Whenever there's a symbol in the mmo format, there's the symbol
775 # Main, so remove it manually from the expected output for sake of
778 # Using "" not {} to get the \n and \r translated.
779 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
782 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
783 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
788 if [string equal "" $test2] {
792 if [is_remote host] {
793 remote_upload host ${copyfile} tmpdir/striprog
796 set result [remote_load target tmpdir/striprog]
797 set status [lindex $result 0]
798 if { $status != "pass" } {
806 # Test keeping only debug symbols of an executable
808 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
809 remote_file build delete tmpdir/striprog
810 remote_download build tmpdir/copyprog tmpdir/striprog
811 if [is_remote host] {
812 set copyfile [remote_download host tmpdir/striprog]
814 set copyfile tmpdir/striprog
817 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
818 if ![string equal "" $exec_output] {
824 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
825 if ![string equal "" $exec_output] {
832 # Tests that in a debug only copy of a file the sections
833 # headers whoes types have been changed to NOBITS still
834 # retain their sh_link fields.
836 proc keep_debug_symbols_and_check_links { prog flags test } {
839 remote_file build delete tmpdir/striprog
840 remote_download build tmpdir/copyprog tmpdir/striprog
841 if [is_remote host] {
842 set copyfile [remote_download host tmpdir/striprog]
844 set copyfile tmpdir/striprog
847 set exec_output [binutils_run $prog "$flags ${copyfile}"]
848 if ![string equal "" $exec_output] {
853 set got [binutils_run $READELF "-S --wide ${copyfile}"]
856 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
858 {[^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]+)(.*)} \
859 $got all name link rest]} {
862 # Only some NOBITS sections should have a non-zero sh_link field.
863 # Look for them by name.
864 verbose "NOBITS section .$name has a 0 sh_link field\n"
866 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
867 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
868 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
869 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
870 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
871 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
872 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
873 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
887 set test1 "simple objcopy of executable"
888 set test1r "run objcopy of executable"
889 set test2 "strip executable"
890 set test2r "run stripped executable"
891 set test3 "strip executable with saving a symbol"
892 set test3r "run stripped executable with saving a symbol"
893 set test4 "keep only debug data"
894 set test5 "simple objcopy of debug data"
896 set test6 "NOBITS sections retain sh_link field"
899 switch [copy_setup] {
917 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
919 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
921 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
923 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
924 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
926 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
930 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
931 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
932 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
933 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
934 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
936 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
941 proc objcopy_test_readelf {testname srcfile} {
948 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
949 unresolved "objcopy ($testname)"
953 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
954 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
955 if { [lindex $exec_output 0] != 0
956 || ![string equal "" [lindex $exec_output 1]] } then {
957 fail "objcopy ($testname)"
961 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
962 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
963 if { [lindex $exec_output 0] != 0 } then {
964 unresolved "objcopy ($testname)"
967 set exec_output [prune_warnings [lindex $exec_output 1]]
968 if ![string equal "" $exec_output] then {
969 unresolved "objcopy ($testname)"
973 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
974 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
975 if { [lindex $exec_output 0] != 0 } then {
976 unresolved "objcopy ($testname)"
979 set exec_output [prune_warnings [lindex $exec_output 1]]
980 if ![string equal "" $exec_output] then {
981 unresolved "objcopy ($testname)"
985 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
986 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
987 set exec_output [prune_warnings $exec_output]
989 if [string equal "" $exec_output] then {
990 pass "objcopy ($testname)"
992 fail "objcopy ($testname)"
996 proc objcopy_test_symbol_manipulation {} {
1000 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1001 foreach t $test_list {
1002 # We need to strip the ".d", but can leave the dirname.
1003 verbose [file rootname $t]
1004 run_dump_test [file rootname $t]
1008 proc objcopy_test_elf_common_symbols {} {
1012 # hpux has a non-standard common directive.
1013 if { [istarget "*-*-hpux*"] } then {
1017 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1018 foreach t $test_list {
1019 # We need to strip the ".d", but can leave the dirname.
1020 verbose [file rootname $t]
1021 run_dump_test [file rootname $t]
1025 # ia64 specific tests
1026 if { ([istarget "ia64-*-elf*"]
1027 || [istarget "ia64-*-linux*"]) } {
1028 objcopy_test "ia64 link order" link-order.s
1031 # ELF specific tests
1032 if [is_elf_format] {
1033 objcopy_test_symbol_manipulation
1034 objcopy_test_elf_common_symbols
1035 objcopy_test "ELF unknown section type" unknown.s
1036 objcopy_test_readelf "ELF group" group.s
1037 objcopy_test_readelf "ELF group" group-2.s
1038 objcopy_test_readelf "ELF group" group-3.s
1039 objcopy_test_readelf "ELF group" group-4.s
1040 run_dump_test "group-5"
1041 run_dump_test "group-6"
1042 run_dump_test "copy-1"
1043 run_dump_test "note-1"
1046 run_dump_test "copy-2"
1047 run_dump_test "copy-3"
1048 run_dump_test "copy-4"
1049 run_dump_test "pr19020a"
1050 run_dump_test "pr19020b"
1052 if [is_elf_format] {
1053 run_dump_test "strip-1"
1054 run_dump_test "strip-2"
1055 run_dump_test "strip-3"
1056 run_dump_test "strip-4"
1057 run_dump_test "strip-5"
1058 run_dump_test "strip-6"
1059 run_dump_test "strip-7"
1060 run_dump_test "strip-8"
1061 run_dump_test "strip-9"
1062 run_dump_test "strip-12"
1063 # Non-EABI ARM targets will set OSABI to ARM
1064 # The V850 sets the OSABI to UNIX - System V
1065 if { ![istarget "*-*-hpux*"]
1066 && ![istarget "msp*-*-*"]
1067 && ![istarget "visium-*-*"]
1068 && ![istarget "v850*-*-*"]
1069 && !([istarget "arm*-*-*"] && ![istarget "arm-*-*eabi*"])} {
1070 run_dump_test "strip-10"
1072 set extra_strip11 ""
1073 if { [istarget "sh64*-*"] } {
1075 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1077 run_dump_test "strip-11" $extra_strip11
1079 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1080 # Check to make sure we don't strip a symbol named in relocations.
1081 set test "objcopy keeps symbols needed by relocs"
1083 set srcfile $srcdir/$subdir/needed-by-reloc.s
1085 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1088 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1090 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1098 # The symbol table for some MIPS targets is sorted differently than
1099 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1100 # to match. These tests must be matched to targets for which
1101 # targ_defvec=mips_elf32_be_vec,
1102 # targ_defvec=mips_elf32_le_vec,
1103 # targ_defvec=mips_elf32_n_be_vec or
1104 # targ_defvec=mips_elf32_n_le_vec in config.bfd. When syncing,
1105 # don't forget that earlier case-matches trump later ones.
1106 if { ![istarget "mips*-sde-elf*"] && ![istarget "mips*-mti-elf*"]
1107 && ![istarget "mips*-img-elf*"]
1108 && ![istarget "mips64*-*-openbsd*"] } {
1109 setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-elf*" \
1110 "mips*-*-rtems*" "mips*-*-windiss" "mips*-*-none" \
1111 "mips*-*-openbsd*" "mips*-*-chorus*"
1113 run_dump_test "localize-hidden-1"
1114 run_dump_test "testranges"
1115 run_dump_test "testranges-ia64"
1117 run_dump_test "add-section"
1118 run_dump_test "add-symbol"
1119 run_dump_test "add-empty-section"
1121 run_dump_test "exclude-1a"
1122 run_dump_test "exclude-1b"
1124 run_dump_test "localize-hidden-2"
1126 # Test objcopying an object file without global symbol
1128 proc objcopy_test_without_global_symbol { } {
1136 set test "strip without global symbol "
1138 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1143 if [is_remote host] {
1144 set objfile [remote_download host tmpdir/pr19547.o]
1146 set objfile tmpdir/pr19547.o
1149 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1150 if ![string equal "" $exec_output] {
1155 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1156 if {![regexp "no symbols" $exec_output]} {
1164 objcopy_test_without_global_symbol