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 "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 if { [file exists $tempfile] } {
111 # Test reversing bytes in a section.
113 set reversed ${tempfile}-reversed
114 set sect_names [get_standard_section_names]
115 if { $sect_names != "" } {
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
118 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
121 if ![string equal "" $got] then {
122 fail "objcopy --reverse-bytes"
124 if [is_remote host] {
125 remote_upload host ${reversed} tmpdir/copy-reversed.o
126 set reversed tmpdir/copy-reversed.o
129 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
130 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
132 set want "^ \[0-9\]+ (\[0-9\]+)"
133 set found_orig [regexp -lineanchor $want $origdata -> origdata]
134 set found_rev [regexp -lineanchor $want $revdata -> revdata]
136 if {$found_orig == 0 || $found_rev == 0} then {
137 # som doesn't have a .data section
138 setup_xfail "hppa*-*-hpux*"
139 clear_xfail "hppa*64*-*-hpux*"
141 fail "objcopy --reverse-bytes"
143 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
144 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
146 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
147 pass "objcopy --reverse-bytes"
149 fail "objcopy --reverse-bytes"
155 # Test interleaved copy of multiple byte width
157 set sequence_file sequence_file
158 set file [open ${sequence_file} w]
159 puts ${file} "12345678"
162 if [is_remote host] {
163 remote_upload host ${sequence_file} tmpdir/sequence_file
164 set sequence_file tmpdir/sequence_file
167 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
169 if ![string equal "" $got] then {
170 fail "objcopy -i --interleave-width"
172 if [is_remote host] {
173 remote_upload host ${copyfile} tmpdir/interleave_output
174 set interleave_output tmpdir/interleave_output
176 set interleave_output ${copyfile}
179 set file [open ${interleave_output} r]
184 if ![string equal "1256" $line] then {
185 fail "objcopy -i --interleave-width"
187 pass "objcopy -i --interleave-width"
192 # Test generating S records.
194 if { [file exists $tempfile] } {
195 # We make the srec filename 8.3 compatible. Note that the header string
196 # matched against depends on the name of the file. Ugh.
198 if [is_remote host] {
199 set srecfile copy.sre
200 set header_string S00B0000636F70792E737265C1
202 set srecfile ${copyfile}.srec
203 set header_string S0130000746D706469722F636F70792E7372656397
206 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
208 if ![string equal "" $got] then {
209 fail "objcopy -O srec"
211 if [is_remote host] {
212 remote_upload host ${srecfile} tmpdir/copy.srec
213 set srecfile tmpdir/copy.srec
215 set file [open ${srecfile} r]
217 # The first S record is fixed by the file name we are using.
221 if ![regexp "$header_string.*" $line] {
222 send_log "bad header\n"
223 fail "objcopy -O srec"
225 while {[gets $file line] != -1 \
226 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
233 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
234 send_log "bad trailer\n"
235 fail "objcopy -O srec"
237 if {[gets $file line] != -1} then {
238 send_log "garbage at end\n"
241 fail "objcopy -O srec"
243 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
244 if ![regexp "file format srec" $got] then {
245 send_log "objdump failed\n"
246 fail "objcopy -O srec"
248 pass "objcopy -O srec"
258 # Test setting and adjusting the start address. We only test this
259 # while generating S records, because we may not be able to set the
260 # start address for other object file formats, and the S record case
261 # is the only useful one anyhow.
263 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
264 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
265 perror "objdump can not recognize bintest.o"
268 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
269 if ![string equal "" $got] then {
270 fail "objcopy --set-start"
272 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
273 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
274 fail "objcopy --set-start"
276 if {$srecstart != 0x7654} then {
277 send_log "$srecstart != 0x7654\n"
278 fail "objcopy --set-start"
280 pass "objcopy --set-start"
285 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
286 if ![string equal "" $got] then {
287 fail "objcopy --adjust-start"
289 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
290 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
291 fail "objcopy --adjust-start"
293 if {$srecstart != $origstart + 0x123} then {
294 send_log "$srecstart != $origstart + 0x123\n"
295 fail "objcopy --adjust-start"
297 pass "objcopy --adjust-start"
303 # Test adjusting the overall VMA, and adjusting the VMA of a
304 # particular section. We again only test this when generating S
310 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
312 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
315 while {[regexp $headers_regexp $got all name size vma rest]} {
319 if {$low == "" || $vma < $low} {
327 if {$low == "" || $origstart == ""} then {
328 perror "objdump can not recognize bintest.o"
330 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
331 if ![string equal "" $got] then {
332 fail "objcopy --adjust-vma"
334 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
335 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
336 if ![regexp $want $got all start vma] then {
337 fail "objcopy --adjust-vma"
340 if {$vma != $low + 0x123} then {
341 send_log "$vma != $low + 0x123\n"
342 fail "objcopy --adjust-vma"
344 if {$start != $origstart + 0x123} then {
345 send_log "$start != $origstart + 0x123\n"
346 fail "objcopy --adjust-vma"
348 pass "objcopy --adjust-vma"
356 while {[regexp $headers_regexp $got all name size vma rest]} {
358 if {$vma == $low} then {
359 set arg "$arg --adjust-section-vma $name+4"
364 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
365 if ![string equal "" $got] then {
366 fail "objcopy --adjust-section-vma +"
368 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
369 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
370 if ![regexp $want $got all vma] then {
371 fail "objcopy --adjust-section-vma +"
374 if {$vma != $low + 4} then {
375 send_log "$vma != $low + 4\n"
376 fail "objcopy --adjust-section-vma +"
378 pass "objcopy --adjust-section-vma +"
383 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
384 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
385 if ![string equal "" $got] then {
386 fail "objcopy --adjust-section-vma ="
388 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
389 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
390 if ![regexp $want $got all vma] then {
391 fail "objcopy --adjust-section-vma ="
394 if {$vma != $low + 4} then {
395 send_log "$vma != $low + 4\n"
396 fail "objcopy --adjust-section-vma ="
398 pass "objcopy --adjust-section-vma ="
404 # Test stripping an object.
406 proc strip_test { } {
419 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
424 set osabi_fail "false"
426 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
427 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
428 if { [lindex $exec_output 0] != 0 } then {
429 unresolved "$test preserving OS/ABI"
430 set osabi_fail "true"
432 verbose -log "grep OS/ABI tmpdir/osabi.in"
433 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
437 if [is_remote host] {
438 set archive libstrip.a
439 set objfile [remote_download host tmpdir/testprog.o]
440 remote_file host delete $archive
442 set archive tmpdir/libstrip.a
443 set objfile tmpdir/testprog.o
446 remote_file build delete tmpdir/libstrip.a
448 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
449 if ![string equal "" $exec_output] {
451 unresolved "$test preserving OS/ABI"
455 set exec_output [binutils_run $STRIP "-g $archive"]
456 if ![string equal "" $exec_output] {
458 unresolved "$test preserving OS/ABI"
462 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
463 if ![string equal "" $exec_output] {
465 unresolved "$test preserving OS/ABI"
469 if { $osabi_fail != "true" && [is_elf_format] } {
470 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
471 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
472 if { [lindex $exec_output 0] != 0 } then {
473 unresolved "$test preserving OS/ABI"
475 verbose -log "grep OS/ABI tmpdir/osabi.out"
476 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
477 if { "$osabi_in" == "$osabi_out" } {
478 pass "$test preserving OS/ABI"
480 fail "$test preserving OS/ABI"
485 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
490 if [is_remote host] {
491 set objfile [remote_download host tmpdir/testprog.o]
493 set objfile tmpdir/testprog.o
496 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
497 if ![string equal "" $exec_output] {
502 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
503 if ![string match "*: no symbols*" $exec_output] {
513 # Test stripping an object file with saving a symbol
515 proc strip_test_with_saving_a_symbol { } {
524 set test "strip with saving a symbol"
526 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
531 if [is_remote host] {
532 set objfile [remote_download host tmpdir/testprog.o]
534 set objfile tmpdir/testprog.o
537 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
538 if ![string equal "" $exec_output] {
543 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
544 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
545 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
553 strip_test_with_saving_a_symbol
555 # Build a final executable.
557 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
558 set test_prog "testprog.exe"
560 set test_prog "testprog"
563 proc copy_setup { } {
570 set res [build_wrapper testglue.o]
573 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
578 lappend flags "additional_flags=[lindex $res 1]"
579 set add_libs "testglue.o"
584 if { [istarget *-*-linux*]
585 || [istarget *-*-gnu*] } {
586 foreach i $gcc_gas_flag {
587 set flags "additional_flags=$i $flags"
590 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
594 set result [remote_load target tmpdir/$test_prog]
595 set status [lindex $result 0]
597 if { $status != "pass" } {
598 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
605 # Test copying an executable.
607 proc copy_executable { prog flags test1 test2 } {
610 if [is_remote host] {
611 set testfile [remote_download host tmpdir/$test_prog]
612 set testcopy copyprog
614 set testfile tmpdir/$test_prog
615 set testcopy tmpdir/copyprog
617 remote_file host delete $testcopy
619 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
621 if ![string equal "" $exec_output] {
623 if [string equal "" $test2] {
630 if [is_remote host] {
631 remote_upload host $testcopy tmpdir/copyprog
634 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
635 set exec_output [lindex $status 1]
637 if [string equal "" $exec_output] then {
640 send_log "$exec_output\n"
641 verbose "$exec_output"
643 # This will fail for many reasons. For example, it will most
644 # likely fail if a non-GNU linker is used. Therefore, we do
645 # not insist that it pass. If you are using an assembler and
646 # linker based on the same BFD as objcopy, it is worth
647 # investigating to see why this failure occurs. If we are
648 # cross compiling, we assume that a GNU linker is being used,
649 # and expect it to succeed.
650 if {[isnative]} then {
654 # This also fails for some mips targets. See elf32-mips.c
655 # mips_elf_sym_is_global.
656 if { [is_bad_symtab] } then {
660 setup_xfail "arm*-*-coff"
661 setup_xfail "arm*-*-pe"
662 setup_xfail "*-*-mingw*"
663 setup_xfail "*-*-cygwin*"
668 if [string equal "" $test2] {
672 set output [remote_load target tmpdir/copyprog]
673 set status [lindex $output 0]
674 if { $status != "pass" } {
681 # Test stripping an executable
683 proc strip_executable { prog flags test1 test2 } {
688 remote_file build delete tmpdir/striprog
689 remote_download build tmpdir/copyprog tmpdir/striprog
690 if [is_remote host] {
691 set copyfile [remote_download host tmpdir/striprog]
693 set copyfile tmpdir/striprog
696 set osabi_fail "false"
698 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
699 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
700 if { [lindex $exec_output 0] != 0 } then {
701 unresolved "$test1 preserving OS/ABI"
702 set osabi_fail "true"
704 verbose -log "grep OS/ABI tmpdir/osabi.in"
705 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
709 set exec_output [binutils_run $prog "$flags ${copyfile}"]
710 if ![string equal "" $exec_output] {
712 if [string equal "" $test2] {
719 if [is_remote host] {
720 remote_upload host ${copyfile} tmpdir/striprog
723 if { $osabi_fail != "true" && [is_elf_format] } {
724 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
725 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
726 if { [lindex $exec_output 0] != 0 } then {
727 unresolved "$test1 preserving OS/ABI"
729 verbose -log "grep OS/ABI tmpdir/osabi.out"
730 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
731 if { "$osabi_in" == "$osabi_out" } {
732 pass "$test1 preserving OS/ABI"
734 fail "$test1 preserving OS/ABI"
739 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
740 if ![string match "*: no symbols*" $exec_output] {
745 if [string equal "" $test2] {
749 set result [remote_load target tmpdir/striprog]
750 set status [lindex $result 0]
751 if { $status != "pass" } {
759 # Test stripping an executable with saving a symbol
761 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
765 remote_file build delete tmpdir/striprog
766 remote_download build tmpdir/copyprog tmpdir/striprog
767 if [is_remote host] {
768 set copyfile [remote_download host tmpdir/striprog]
770 set copyfile tmpdir/striprog
773 set exec_output [binutils_run $prog "$flags ${copyfile}"]
774 if ![string equal "" $exec_output] {
776 if [string equal "" $test2] {
783 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
784 if { [istarget mmix-knuth-mmixware] } {
785 # Whenever there's a symbol in the mmo format, there's the symbol
786 # Main, so remove it manually from the expected output for sake of
789 # Using "" not {} to get the \n and \r translated.
790 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
793 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
794 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
799 if [string equal "" $test2] {
803 if [is_remote host] {
804 remote_upload host ${copyfile} tmpdir/striprog
807 set result [remote_load target tmpdir/striprog]
808 set status [lindex $result 0]
809 if { $status != "pass" } {
817 # Test keeping only debug symbols of an executable
819 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
820 remote_file build delete tmpdir/striprog
821 remote_download build tmpdir/copyprog tmpdir/striprog
822 if [is_remote host] {
823 set copyfile [remote_download host tmpdir/striprog]
825 set copyfile tmpdir/striprog
828 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
829 if ![string equal "" $exec_output] {
835 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
836 if ![string equal "" $exec_output] {
843 # Tests that in a debug only copy of a file the sections
844 # headers whose types have been changed to NOBITS still
845 # retain their sh_link fields.
847 proc keep_debug_symbols_and_check_links { prog flags test } {
850 remote_file build delete tmpdir/striprog
851 remote_download build tmpdir/copyprog tmpdir/striprog
852 if [is_remote host] {
853 set copyfile [remote_download host tmpdir/striprog]
855 set copyfile tmpdir/striprog
858 set exec_output [binutils_run $prog "$flags ${copyfile}"]
859 if ![string equal "" $exec_output] {
864 set got [binutils_run $READELF "-S --wide ${copyfile}"]
867 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
869 {[^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]+)(.*)} \
870 $got all name link rest]} {
873 # Only some NOBITS sections should have a non-zero sh_link field.
874 # Look for them by name.
875 verbose "NOBITS section .$name has a 0 sh_link field\n"
877 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
882 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
883 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
884 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
898 set test1 "simple objcopy of executable"
899 set test1r "run objcopy of executable"
900 set test2 "strip executable"
901 set test2r "run stripped executable"
902 set test3 "strip executable with saving a symbol"
903 set test3r "run stripped executable with saving a symbol"
904 set test4 "keep only debug data"
905 set test5 "simple objcopy of debug data"
907 set test6 "NOBITS sections retain sh_link field"
910 switch [copy_setup] {
928 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
930 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
932 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
934 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
935 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
937 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
941 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
942 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
943 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
944 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
945 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
947 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
952 proc objcopy_test_readelf {testname srcfile} {
959 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
960 unresolved "objcopy ($testname)"
964 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
965 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
966 if { [lindex $exec_output 0] != 0
967 || ![string equal "" [lindex $exec_output 1]] } then {
968 fail "objcopy ($testname)"
972 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
973 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
974 if { [lindex $exec_output 0] != 0 } then {
975 unresolved "objcopy ($testname)"
978 set exec_output [prune_warnings [lindex $exec_output 1]]
979 if ![string equal "" $exec_output] then {
980 unresolved "objcopy ($testname)"
984 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
985 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
986 if { [lindex $exec_output 0] != 0 } then {
987 unresolved "objcopy ($testname)"
990 set exec_output [prune_warnings [lindex $exec_output 1]]
991 if ![string equal "" $exec_output] then {
992 unresolved "objcopy ($testname)"
996 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
997 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
998 set exec_output [prune_warnings $exec_output]
1000 if [string equal "" $exec_output] then {
1001 pass "objcopy ($testname)"
1003 fail "objcopy ($testname)"
1007 proc objcopy_test_symbol_manipulation {} {
1011 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1012 foreach t $test_list {
1013 # We need to strip the ".d", but can leave the dirname.
1014 verbose [file rootname $t]
1015 run_dump_test [file rootname $t]
1019 proc objcopy_test_elf_common_symbols {} {
1023 # hpux has a non-standard common directive.
1024 if { [istarget "*-*-hpux*"] } then {
1028 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1029 foreach t $test_list {
1030 # We need to strip the ".d", but can leave the dirname.
1031 verbose [file rootname $t]
1032 run_dump_test [file rootname $t]
1036 # ia64 specific tests
1037 if { ([istarget "ia64-*-elf*"]
1038 || [istarget "ia64-*-linux*"]) } {
1039 objcopy_test "ia64 link order" link-order.s
1042 # ELF specific tests
1043 if [is_elf_format] {
1044 objcopy_test_symbol_manipulation
1045 objcopy_test_elf_common_symbols
1046 objcopy_test "ELF unknown section type" unknown.s
1047 objcopy_test_readelf "ELF group" group.s
1048 objcopy_test_readelf "ELF group" group-2.s
1049 objcopy_test_readelf "ELF group" group-3.s
1050 objcopy_test_readelf "ELF group" group-4.s
1051 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1052 run_dump_test "group-5"
1053 run_dump_test "group-6"
1054 run_dump_test "group-7a"
1055 run_dump_test "group-7b"
1056 run_dump_test "group-7c"
1057 run_dump_test "copy-1"
1058 run_dump_test "note-1"
1059 if [is_elf64 tmpdir/bintest.o] {
1060 run_dump_test "note-2-64"
1061 run_dump_test "note-3-64"
1062 run_dump_test "note-4-64"
1064 run_dump_test "note-2-32"
1065 run_dump_test "note-3-32"
1066 run_dump_test "note-4-32"
1070 run_dump_test "copy-2"
1071 run_dump_test "copy-3"
1072 run_dump_test "copy-4"
1073 run_dump_test "pr19020a"
1074 run_dump_test "pr19020b"
1076 if [is_elf_format] {
1077 run_dump_test "strip-1"
1078 run_dump_test "strip-2"
1079 run_dump_test "strip-3"
1080 run_dump_test "strip-4"
1081 run_dump_test "strip-5"
1082 run_dump_test "strip-6"
1083 run_dump_test "strip-7"
1084 run_dump_test "strip-8"
1085 run_dump_test "strip-9"
1086 run_dump_test "strip-12"
1088 if { [istarget "mips64*-*-openbsd*"] } {
1089 set reloc_format mips64
1090 } elseif { [istarget "arm-*"] \
1091 || [istarget "d10v-*"] \
1092 || [istarget "dlx-*"] \
1093 || [istarget "i*86-*"] \
1094 || [istarget "i960-*"] \
1095 || [istarget "m681*-*"] \
1096 || [istarget "m68hc1*-*"] \
1097 || ([istarget "mips*-*"] \
1098 && ![istarget "mips64*-ps2-elf*"] \
1099 && ![istarget "*-*-irix6*"] \
1100 && ![istarget "mips64*-*-freebsd*"] \
1101 && ![istarget "mips64*-*-kfreebsd*-gnu"] \
1102 && ![istarget "mips64*-*-linux*"]) \
1103 || [istarget "score*-*"] \
1104 || [istarget "xgate-*"] } {
1105 set reloc_format rel
1107 set reloc_format rela
1109 run_dump_test "strip-13" [list [list source strip-13${reloc_format}.s]]
1110 # Select a relocation number that corresponds to one actually
1111 # supported by the target and ABI being tested.
1112 if { [istarget "aarch64*-*"] } {
1114 } elseif { [istarget "ia64*-*"] \
1115 || [istarget "m32r*-*"] \
1116 || [istarget "nds32*-*"] \
1117 || [istarget "v850*-*"] } {
1119 } elseif { [istarget "pru-*"] } {
1124 run_dump_test "strip-14" [list \
1125 [list source strip-14${reloc_format}.s] \
1126 [list as "--defsym RELOC=${reloc}"] \
1127 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1128 ? "--defsym ELF64=1" : ""}]]]
1129 run_dump_test "strip-15" [list \
1130 [list source strip-15${reloc_format}.s] \
1131 [list as "--defsym RELOC=${reloc}"] \
1132 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1133 ? "--defsym ELF64=1" : ""}]]]
1135 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1136 if { [supports_gnu_unique] } {
1137 run_dump_test "strip-10"
1139 set extra_strip11 ""
1140 if { [istarget "sh64*-*"] } {
1142 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1144 run_dump_test "strip-11" $extra_strip11
1146 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1147 # Check to make sure we don't strip a symbol named in relocations.
1148 set test "objcopy keeps symbols needed by relocs"
1150 set srcfile $srcdir/$subdir/needed-by-reloc.s
1152 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1155 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1157 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1165 # The symbol table for some MIPS targets is sorted differently than
1166 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1168 if { [is_bad_symtab] } then {
1171 run_dump_test "localize-hidden-1"
1172 run_dump_test "testranges"
1173 run_dump_test "testranges-ia64"
1175 run_dump_test "add-section"
1176 run_dump_test "add-symbol"
1177 run_dump_test "add-empty-section"
1179 run_dump_test "exclude-1a"
1180 run_dump_test "exclude-1b"
1182 run_dump_test "only-section-01"
1183 run_dump_test "remove-section-01"
1185 # Test the remove relocation functionality
1186 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1187 foreach t $test_list {
1188 # We need to strip the ".d", but can leave the dirname.
1189 verbose [file rootname $t]
1190 run_dump_test [file rootname $t]
1193 run_dump_test "localize-hidden-2"
1195 # Test objcopying an object file without global symbol
1197 proc objcopy_test_without_global_symbol { } {
1205 set test "strip without global symbol "
1207 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1212 if [is_remote host] {
1213 set objfile [remote_download host tmpdir/pr19547.o]
1215 set objfile tmpdir/pr19547.o
1218 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1219 if ![string equal "" $exec_output] {
1224 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1225 if {![regexp "no symbols" $exec_output]} {
1233 # The AArch64 and ARM targets preserve mapping symbols
1234 # in object files, so they will fail this test.
1235 setup_xfail aarch64*-*-* arm*-*-*
1237 objcopy_test_without_global_symbol