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:
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} $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 "hppa*-*-*"
81 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
82 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
84 setup_xfail "sh-*-coff*"
85 setup_xfail "tic80-*-*" "w65-*"
87 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
88 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
89 clear_xfail "m68*-*-sysv4*"
91 if [string equal "" $exec_output] then {
92 pass "objcopy ($testname)"
94 send_log "$exec_output\n"
95 verbose "$exec_output" 1
97 # On OSF/1, this succeeds with gas and fails with /bin/as.
98 setup_xfail "alpha*-*-osf*"
100 fail "objcopy ($testname)"
105 objcopy_test "simple copy" bintest.s
107 if { [file exists $tempfile] } {
108 # Test reversing bytes in a section.
110 set reversed ${tempfile}-reversed
111 set sect_names [get_standard_section_names]
112 if { $sect_names != "" } {
113 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
115 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
118 if ![string equal "" $got] then {
119 fail "objcopy --reverse-bytes"
121 if [is_remote host] {
122 remote_upload host ${reversed} tmpdir/copy-reversed.o
123 set reversed tmpdir/copy-reversed.o
126 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
127 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
129 set want "^ \[0-9\]+ (\[0-9\]+)"
130 set found_orig [regexp -lineanchor $want $origdata -> origdata]
131 set found_rev [regexp -lineanchor $want $revdata -> revdata]
133 if {$found_orig == 0 || $found_rev == 0} then {
134 # som doesn't have a .data section
135 setup_xfail "hppa*-*-hpux*"
136 clear_xfail "hppa*64*-*-hpux*"
138 fail "objcopy --reverse-bytes"
140 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
141 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
143 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
144 pass "objcopy --reverse-bytes"
146 fail "objcopy --reverse-bytes"
152 # Test interleaved copy of multiple byte width
154 set sequence_file sequence_file
155 set file [open ${sequence_file} w]
156 puts ${file} "12345678"
159 if [is_remote host] {
160 remote_upload host ${sequence_file} tmpdir/sequence_file
161 set sequence_file tmpdir/sequence_file
164 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
166 if ![string equal "" $got] then {
167 fail "objcopy -i --interleave-width"
169 if [is_remote host] {
170 remote_upload host ${copyfile} tmpdir/interleave_output
171 set interleave_output tmpdir/interleave_output
173 set interleave_output ${copyfile}
176 set file [open ${interleave_output} r]
181 if ![string equal "1256" $line] then {
182 fail "objcopy -i --interleave-width"
184 pass "objcopy -i --interleave-width"
189 # Test generating S records.
191 if { [file exists $tempfile] } {
192 # We make the srec filename 8.3 compatible. Note that the header string
193 # matched against depends on the name of the file. Ugh.
195 if [is_remote host] {
196 set srecfile copy.sre
197 set header_string S00B0000636F70792E737265C1
199 set srecfile ${copyfile}.srec
200 set header_string S0130000746D706469722F636F70792E7372656397
203 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
205 if ![string equal "" $got] then {
206 fail "objcopy -O srec"
208 if [is_remote host] {
209 remote_upload host ${srecfile} tmpdir/copy.srec
210 set srecfile tmpdir/copy.srec
212 set file [open ${srecfile} r]
214 # The first S record is fixed by the file name we are using.
218 if ![regexp "$header_string.*" $line] {
219 send_log "bad header\n"
220 fail "objcopy -O srec"
222 while {[gets $file line] != -1 \
223 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
230 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
231 send_log "bad trailer\n"
232 fail "objcopy -O srec"
234 if {[gets $file line] != -1} then {
235 send_log "garbage at end\n"
238 fail "objcopy -O srec"
240 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
241 if ![regexp "file format srec" $got] then {
242 send_log "objdump failed\n"
243 fail "objcopy -O srec"
245 pass "objcopy -O srec"
255 # Test setting and adjusting the start address. We only test this
256 # while generating S records, because we may not be able to set the
257 # start address for other object file formats, and the S record case
258 # is the only useful one anyhow.
260 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
261 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
262 perror "objdump can not recognize bintest.o"
265 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
266 if ![string equal "" $got] then {
267 fail "objcopy --set-start"
269 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
270 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
271 fail "objcopy --set-start"
273 if {$srecstart != 0x7654} then {
274 send_log "$srecstart != 0x7654\n"
275 fail "objcopy --set-start"
277 pass "objcopy --set-start"
282 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
283 if ![string equal "" $got] then {
284 fail "objcopy --adjust-start"
286 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
287 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
288 fail "objcopy --adjust-start"
290 if {$srecstart != $origstart + 0x123} then {
291 send_log "$srecstart != $origstart + 0x123\n"
292 fail "objcopy --adjust-start"
294 pass "objcopy --adjust-start"
300 # Test adjusting the overall VMA, and adjusting the VMA of a
301 # particular section. We again only test this when generating S
307 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
309 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
312 while {[regexp $headers_regexp $got all name size vma rest]} {
316 if {$low == "" || $vma < $low} {
324 if {$low == "" || $origstart == ""} then {
325 perror "objdump can not recognize bintest.o"
327 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
328 if ![string equal "" $got] then {
329 fail "objcopy --adjust-vma"
331 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
332 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
333 if ![regexp $want $got all start vma] then {
334 fail "objcopy --adjust-vma"
337 if {$vma != $low + 0x123} then {
338 send_log "$vma != $low + 0x123\n"
339 fail "objcopy --adjust-vma"
341 if {$start != $origstart + 0x123} then {
342 send_log "$start != $origstart + 0x123\n"
343 fail "objcopy --adjust-vma"
345 pass "objcopy --adjust-vma"
353 while {[regexp $headers_regexp $got all name size vma rest]} {
355 if {$vma == $low} then {
356 set arg "$arg --adjust-section-vma $name+4"
361 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
362 if ![string equal "" $got] then {
363 fail "objcopy --adjust-section-vma +"
365 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
366 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
367 if ![regexp $want $got all vma] then {
368 fail "objcopy --adjust-section-vma +"
371 if {$vma != $low + 4} then {
372 send_log "$vma != $low + 4\n"
373 fail "objcopy --adjust-section-vma +"
375 pass "objcopy --adjust-section-vma +"
380 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
381 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
382 if ![string equal "" $got] then {
383 fail "objcopy --adjust-section-vma ="
385 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
386 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
387 if ![regexp $want $got all vma] then {
388 fail "objcopy --adjust-section-vma ="
391 if {$vma != $low + 4} then {
392 send_log "$vma != $low + 4\n"
393 fail "objcopy --adjust-section-vma ="
395 pass "objcopy --adjust-section-vma ="
401 # Test stripping an object.
403 proc strip_test { } {
416 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
421 set osabi_fail "false"
423 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
424 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
425 if { [lindex $exec_output 0] != 0 } then {
426 unresolved "$test preserving OS/ABI"
427 set osabi_fail "true"
429 verbose -log "grep OS/ABI tmpdir/osabi.in"
430 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
434 if [is_remote host] {
435 set archive libstrip.a
436 set objfile [remote_download host tmpdir/testprog.o]
437 remote_file host delete $archive
439 set archive tmpdir/libstrip.a
440 set objfile tmpdir/testprog.o
443 remote_file build delete tmpdir/libstrip.a
445 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
446 if ![string equal "" $exec_output] {
448 unresolved "$test preserving OS/ABI"
452 set exec_output [binutils_run $STRIP "-g $archive"]
453 if ![string equal "" $exec_output] {
455 unresolved "$test preserving OS/ABI"
459 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
460 if ![string equal "" $exec_output] {
462 unresolved "$test preserving OS/ABI"
466 if { $osabi_fail != "true" && [is_elf_format] } {
467 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
468 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
469 if { [lindex $exec_output 0] != 0 } then {
470 unresolved "$test preserving OS/ABI"
472 verbose -log "grep OS/ABI tmpdir/osabi.out"
473 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
474 if { "$osabi_in" == "$osabi_out" } {
475 pass "$test preserving OS/ABI"
477 fail "$test preserving OS/ABI"
482 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
487 if [is_remote host] {
488 set objfile [remote_download host tmpdir/testprog.o]
490 set objfile tmpdir/testprog.o
493 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
494 if ![string equal "" $exec_output] {
499 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
500 if ![string match "*: no symbols*" $exec_output] {
510 # Test stripping an object file with saving a symbol
512 proc strip_test_with_saving_a_symbol { } {
521 set test "strip with saving a symbol"
523 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
528 if [is_remote host] {
529 set objfile [remote_download host tmpdir/testprog.o]
531 set objfile tmpdir/testprog.o
534 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
535 if ![string equal "" $exec_output] {
540 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
541 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
542 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
550 strip_test_with_saving_a_symbol
552 # Build a final executable.
554 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
555 set test_prog "testprog.exe"
557 set test_prog "testprog"
560 proc copy_setup { } {
567 set res [build_wrapper testglue.o]
570 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
575 lappend flags "additional_flags=[lindex $res 1]"
576 set add_libs "testglue.o"
581 if { [istarget *-*-linux*]
582 || [istarget *-*-gnu*] } {
583 foreach i $gcc_gas_flag {
584 set flags "additional_flags=$i $flags"
587 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
591 set result [remote_load target tmpdir/$test_prog]
592 set status [lindex $result 0]
594 if { $status != "pass" } {
595 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
602 # Test copying an executable.
604 proc copy_executable { prog flags test1 test2 } {
607 if [is_remote host] {
608 set testfile [remote_download host tmpdir/$test_prog]
609 set testcopy copyprog
611 set testfile tmpdir/$test_prog
612 set testcopy tmpdir/copyprog
614 remote_file host delete $testcopy
616 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
618 if ![string equal "" $exec_output] {
620 if [string equal "" $test2] {
627 if [is_remote host] {
628 remote_upload host $testcopy tmpdir/copyprog
631 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
632 set exec_output [lindex $status 1]
634 if [string equal "" $exec_output] then {
637 send_log "$exec_output\n"
638 verbose "$exec_output"
640 # This will fail for many reasons. For example, it will most
641 # likely fail if a non-GNU linker is used. Therefore, we do
642 # not insist that it pass. If you are using an assembler and
643 # linker based on the same BFD as objcopy, it is worth
644 # investigating to see why this failure occurs. If we are
645 # cross compiling, we assume that a GNU linker is being used,
646 # and expect it to succeed.
647 if {[isnative]} then {
651 # This also fails for some mips targets. See elf32-mips.c
652 # mips_elf_sym_is_global.
653 if { [is_bad_symtab] } then {
657 setup_xfail "arm*-*-coff"
658 setup_xfail "arm*-*-pe"
659 setup_xfail "*-*-mingw*"
660 setup_xfail "*-*-cygwin*"
665 if [string equal "" $test2] {
669 set output [remote_load target tmpdir/copyprog]
670 set status [lindex $output 0]
671 if { $status != "pass" } {
678 # Test stripping an executable
680 proc strip_executable { prog flags test1 test2 } {
685 remote_file build delete tmpdir/striprog
686 remote_download build tmpdir/copyprog tmpdir/striprog
687 if [is_remote host] {
688 set copyfile [remote_download host tmpdir/striprog]
690 set copyfile tmpdir/striprog
693 set osabi_fail "false"
695 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
696 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
697 if { [lindex $exec_output 0] != 0 } then {
698 unresolved "$test1 preserving OS/ABI"
699 set osabi_fail "true"
701 verbose -log "grep OS/ABI tmpdir/osabi.in"
702 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
706 set exec_output [binutils_run $prog "$flags ${copyfile}"]
707 if ![string equal "" $exec_output] {
709 if [string equal "" $test2] {
716 if [is_remote host] {
717 remote_upload host ${copyfile} tmpdir/striprog
720 if { $osabi_fail != "true" && [is_elf_format] } {
721 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
722 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
723 if { [lindex $exec_output 0] != 0 } then {
724 unresolved "$test1 preserving OS/ABI"
726 verbose -log "grep OS/ABI tmpdir/osabi.out"
727 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
728 if { "$osabi_in" == "$osabi_out" } {
729 pass "$test1 preserving OS/ABI"
731 fail "$test1 preserving OS/ABI"
736 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
737 if ![string match "*: no symbols*" $exec_output] {
742 if [string equal "" $test2] {
746 set result [remote_load target tmpdir/striprog]
747 set status [lindex $result 0]
748 if { $status != "pass" } {
756 # Test stripping an executable with saving a symbol
758 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
762 remote_file build delete tmpdir/striprog
763 remote_download build tmpdir/copyprog tmpdir/striprog
764 if [is_remote host] {
765 set copyfile [remote_download host tmpdir/striprog]
767 set copyfile tmpdir/striprog
770 set exec_output [binutils_run $prog "$flags ${copyfile}"]
771 if ![string equal "" $exec_output] {
773 if [string equal "" $test2] {
780 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
781 if { [istarget mmix-knuth-mmixware] } {
782 # Whenever there's a symbol in the mmo format, there's the symbol
783 # Main, so remove it manually from the expected output for sake of
786 # Using "" not {} to get the \n and \r translated.
787 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
790 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
791 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
796 if [string equal "" $test2] {
800 if [is_remote host] {
801 remote_upload host ${copyfile} tmpdir/striprog
804 set result [remote_load target tmpdir/striprog]
805 set status [lindex $result 0]
806 if { $status != "pass" } {
814 # Test keeping only debug symbols of an executable
816 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
817 remote_file build delete tmpdir/striprog
818 remote_download build tmpdir/copyprog tmpdir/striprog
819 if [is_remote host] {
820 set copyfile [remote_download host tmpdir/striprog]
822 set copyfile tmpdir/striprog
825 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
826 if ![string equal "" $exec_output] {
832 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
833 if ![string equal "" $exec_output] {
840 # Tests that in a debug only copy of a file the sections
841 # headers whose types have been changed to NOBITS still
842 # retain their sh_link fields.
844 proc keep_debug_symbols_and_check_links { prog flags test } {
847 remote_file build delete tmpdir/striprog
848 remote_download build tmpdir/copyprog tmpdir/striprog
849 if [is_remote host] {
850 set copyfile [remote_download host tmpdir/striprog]
852 set copyfile tmpdir/striprog
855 set exec_output [binutils_run $prog "$flags ${copyfile}"]
856 if ![string equal "" $exec_output] {
861 set got [binutils_run $READELF "-S --wide ${copyfile}"]
864 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
866 {[^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]+)(.*)} \
867 $got all name link rest]} {
870 # Only some NOBITS sections should have a non-zero sh_link field.
871 # Look for them by name.
872 verbose "NOBITS section .$name has a 0 sh_link field\n"
874 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
875 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
876 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
877 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
895 set test1 "simple objcopy of executable"
896 set test1r "run objcopy of executable"
897 set test2 "strip executable"
898 set test2r "run stripped executable"
899 set test3 "strip executable with saving a symbol"
900 set test3r "run stripped executable with saving a symbol"
901 set test4 "keep only debug data"
902 set test5 "simple objcopy of debug data"
904 set test6 "NOBITS sections retain sh_link field"
907 switch [copy_setup] {
925 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
927 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
929 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
931 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
932 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
934 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
938 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
939 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
940 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
941 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
942 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
944 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
949 proc objcopy_test_readelf {testname srcfile} {
956 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
957 unresolved "objcopy ($testname)"
961 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
962 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
963 if { [lindex $exec_output 0] != 0
964 || ![string equal "" [lindex $exec_output 1]] } then {
965 fail "objcopy ($testname)"
969 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
970 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
971 if { [lindex $exec_output 0] != 0 } then {
972 unresolved "objcopy ($testname)"
975 set exec_output [prune_warnings [lindex $exec_output 1]]
976 if ![string equal "" $exec_output] then {
977 unresolved "objcopy ($testname)"
981 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
982 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
983 if { [lindex $exec_output 0] != 0 } then {
984 unresolved "objcopy ($testname)"
987 set exec_output [prune_warnings [lindex $exec_output 1]]
988 if ![string equal "" $exec_output] then {
989 unresolved "objcopy ($testname)"
993 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
994 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
995 set exec_output [prune_warnings $exec_output]
997 if [string equal "" $exec_output] then {
998 pass "objcopy ($testname)"
1000 fail "objcopy ($testname)"
1004 proc objcopy_test_symbol_manipulation {} {
1008 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1009 foreach t $test_list {
1010 # We need to strip the ".d", but can leave the dirname.
1011 verbose [file rootname $t]
1012 run_dump_test [file rootname $t]
1016 proc objcopy_test_elf_common_symbols {} {
1020 # hpux has a non-standard common directive.
1021 if { [istarget "*-*-hpux*"] } then {
1025 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1026 foreach t $test_list {
1027 # We need to strip the ".d", but can leave the dirname.
1028 verbose [file rootname $t]
1029 run_dump_test [file rootname $t]
1033 # ia64 specific tests
1034 if { ([istarget "ia64-*-elf*"]
1035 || [istarget "ia64-*-linux*"]) } {
1036 objcopy_test "ia64 link order" link-order.s
1039 # ELF specific tests
1040 if [is_elf_format] {
1041 objcopy_test_symbol_manipulation
1042 objcopy_test_elf_common_symbols
1043 objcopy_test "ELF unknown section type" unknown.s
1044 objcopy_test_readelf "ELF group" group.s
1045 objcopy_test_readelf "ELF group" group-2.s
1046 objcopy_test_readelf "ELF group" group-3.s
1047 objcopy_test_readelf "ELF group" group-4.s
1048 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1049 run_dump_test "group-5"
1050 run_dump_test "group-6"
1051 run_dump_test "group-7a"
1052 run_dump_test "group-7b"
1053 run_dump_test "group-7c"
1054 run_dump_test "copy-1"
1055 run_dump_test "note-1"
1056 if [is_elf64 tmpdir/bintest.o] {
1057 run_dump_test "note-2-64"
1058 run_dump_test "note-3-64"
1059 run_dump_test "note-4-64"
1061 run_dump_test "note-2-32"
1062 run_dump_test "note-3-32"
1063 run_dump_test "note-4-32"
1067 run_dump_test "copy-2"
1068 run_dump_test "copy-3"
1069 run_dump_test "copy-4"
1070 run_dump_test "pr19020a"
1071 run_dump_test "pr19020b"
1073 if [is_elf_format] {
1074 run_dump_test "strip-1"
1075 run_dump_test "strip-2"
1076 run_dump_test "strip-3"
1077 run_dump_test "strip-4"
1078 run_dump_test "strip-5"
1079 run_dump_test "strip-6"
1080 run_dump_test "strip-7"
1081 run_dump_test "strip-8"
1082 run_dump_test "strip-9"
1083 run_dump_test "strip-12"
1085 if { [istarget "mips64*-*-openbsd*"] } {
1086 set reloc_format mips64
1087 } elseif { [istarget "arm-*"] \
1088 || [istarget "d10v-*"] \
1089 || [istarget "dlx-*"] \
1090 || [istarget "i*86-*"] \
1091 || [istarget "m681*-*"] \
1092 || [istarget "m68hc1*-*"] \
1093 || ([istarget "mips*-*"] \
1094 && ![istarget "mips64*-ps2-elf*"] \
1095 && ![istarget "*-*-irix6*"] \
1096 && ![istarget "mips64*-*-freebsd*"] \
1097 && ![istarget "mips64*-*-kfreebsd*-gnu"] \
1098 && ![istarget "mips64*-*-linux*"]) \
1099 || [istarget "score*-*"] \
1100 || [istarget "xgate-*"] } {
1101 set reloc_format rel
1103 set reloc_format rela
1105 run_dump_test "strip-13" [list [list source strip-13${reloc_format}.s]]
1106 # Select a relocation number that corresponds to one actually
1107 # supported by the target and ABI being tested.
1108 if { [istarget "aarch64*-*"] } {
1110 } elseif { [istarget "ia64*-*"] \
1111 || [istarget "m32r*-*"] \
1112 || [istarget "nds32*-*"] \
1113 || [istarget "v850*-*"] } {
1115 } elseif { [istarget "pru-*"] } {
1120 run_dump_test "strip-14" [list \
1121 [list source strip-14${reloc_format}.s] \
1122 [list as "--defsym RELOC=${reloc}"] \
1123 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1124 ? "--defsym ELF64=1" : ""}]]]
1125 run_dump_test "strip-15" [list \
1126 [list source strip-15${reloc_format}.s] \
1127 [list as "--defsym RELOC=${reloc}"] \
1128 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1129 ? "--defsym ELF64=1" : ""}]]]
1131 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1132 if { [supports_gnu_unique] } {
1133 run_dump_test "strip-10"
1135 set extra_strip11 ""
1136 if { [istarget "sh64*-*"] } {
1138 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1140 run_dump_test "strip-11" $extra_strip11
1142 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1143 # Check to make sure we don't strip a symbol named in relocations.
1144 set test "objcopy keeps symbols needed by relocs"
1146 set srcfile $srcdir/$subdir/needed-by-reloc.s
1148 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1151 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1153 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1161 # The symbol table for some MIPS targets is sorted differently than
1162 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1164 if { [is_bad_symtab] } then {
1167 run_dump_test "localize-hidden-1"
1168 run_dump_test "testranges"
1169 run_dump_test "testranges-ia64"
1171 run_dump_test "add-section"
1172 run_dump_test "add-symbol"
1173 run_dump_test "add-empty-section"
1175 run_dump_test "exclude-1a"
1176 run_dump_test "exclude-1b"
1178 run_dump_test "only-section-01"
1179 run_dump_test "remove-section-01"
1181 # Test the remove relocation functionality
1182 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1183 foreach t $test_list {
1184 # We need to strip the ".d", but can leave the dirname.
1185 verbose [file rootname $t]
1186 run_dump_test [file rootname $t]
1189 run_dump_test "localize-hidden-2"
1191 # Test objcopying an object file without global symbol
1193 proc objcopy_test_without_global_symbol { } {
1201 set test "strip without global symbol "
1203 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1208 if [is_remote host] {
1209 set objfile [remote_download host tmpdir/pr19547.o]
1211 set objfile tmpdir/pr19547.o
1214 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1215 if ![string equal "" $exec_output] {
1220 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1221 if {![regexp "no symbols" $exec_output]} {
1229 # The AArch64 and ARM targets preserve mapping symbols
1230 # in object files, so they will fail this test.
1231 setup_xfail aarch64*-*-* arm*-*-*
1233 objcopy_test_without_global_symbol