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*-*-*"
83 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
84 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
86 setup_xfail "sh-*-coff*"
87 setup_xfail "tic80-*-*" "w65-*"
89 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
90 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
91 clear_xfail "m68*-*-sysv4*"
93 if [string equal "" $exec_output] then {
94 pass "objcopy ($testname)"
96 send_log "$exec_output\n"
97 verbose "$exec_output" 1
99 # On OSF/1, this succeeds with gas and fails with /bin/as.
100 setup_xfail "alpha*-*-osf*"
102 fail "objcopy ($testname)"
107 objcopy_test "simple copy" bintest.s
109 if { [file exists $tempfile] } {
110 # Test reversing bytes in a section.
112 set reversed ${tempfile}-reversed
113 set sect_names [get_standard_section_names]
114 if { $sect_names != "" } {
115 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
117 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
120 if ![string equal "" $got] then {
121 fail "objcopy --reverse-bytes"
123 if [is_remote host] {
124 remote_upload host ${reversed} tmpdir/copy-reversed.o
125 set reversed tmpdir/copy-reversed.o
128 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
129 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
131 set want "^ \[0-9\]+ (\[0-9\]+)"
132 set found_orig [regexp -lineanchor $want $origdata -> origdata]
133 set found_rev [regexp -lineanchor $want $revdata -> revdata]
135 if {$found_orig == 0 || $found_rev == 0} then {
136 # som doesn't have a .data section
137 setup_xfail "hppa*-*-hpux*"
138 clear_xfail "hppa*64*-*-hpux*"
140 fail "objcopy --reverse-bytes"
142 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
143 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
145 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
146 pass "objcopy --reverse-bytes"
148 fail "objcopy --reverse-bytes"
154 # Test interleaved copy of multiple byte width
156 set sequence_file sequence_file
157 set file [open ${sequence_file} w]
158 puts ${file} "12345678"
161 if [is_remote host] {
162 remote_upload host ${sequence_file} tmpdir/sequence_file
163 set sequence_file tmpdir/sequence_file
166 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
168 if ![string equal "" $got] then {
169 fail "objcopy -i --interleave-width"
171 if [is_remote host] {
172 remote_upload host ${copyfile} tmpdir/interleave_output
173 set interleave_output tmpdir/interleave_output
175 set interleave_output ${copyfile}
178 set file [open ${interleave_output} r]
183 if ![string equal "1256" $line] then {
184 fail "objcopy -i --interleave-width"
186 pass "objcopy -i --interleave-width"
191 # Test generating S records.
193 if { [file exists $tempfile] } {
194 # We make the srec filename 8.3 compatible. Note that the header string
195 # matched against depends on the name of the file. Ugh.
197 if [is_remote host] {
198 set srecfile copy.sre
199 set header_string S00B0000636F70792E737265C1
201 set srecfile ${copyfile}.srec
202 set header_string S0130000746D706469722F636F70792E7372656397
205 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
207 if ![string equal "" $got] then {
208 fail "objcopy -O srec"
210 if [is_remote host] {
211 remote_upload host ${srecfile} tmpdir/copy.srec
212 set srecfile tmpdir/copy.srec
214 set file [open ${srecfile} r]
216 # The first S record is fixed by the file name we are using.
220 if ![regexp "$header_string.*" $line] {
221 send_log "bad header\n"
222 fail "objcopy -O srec"
224 while {[gets $file line] != -1 \
225 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
232 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
233 send_log "bad trailer\n"
234 fail "objcopy -O srec"
236 if {[gets $file line] != -1} then {
237 send_log "garbage at end\n"
240 fail "objcopy -O srec"
242 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
243 if ![regexp "file format srec" $got] then {
244 send_log "objdump failed\n"
245 fail "objcopy -O srec"
247 pass "objcopy -O srec"
257 # Test setting and adjusting the start address. We only test this
258 # while generating S records, because we may not be able to set the
259 # start address for other object file formats, and the S record case
260 # is the only useful one anyhow.
262 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
263 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
264 perror "objdump can not recognize bintest.o"
267 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
268 if ![string equal "" $got] then {
269 fail "objcopy --set-start"
271 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
272 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
273 fail "objcopy --set-start"
275 if {$srecstart != 0x7654} then {
276 send_log "$srecstart != 0x7654\n"
277 fail "objcopy --set-start"
279 pass "objcopy --set-start"
284 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
285 if ![string equal "" $got] then {
286 fail "objcopy --adjust-start"
288 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
289 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
290 fail "objcopy --adjust-start"
292 if {$srecstart != $origstart + 0x123} then {
293 send_log "$srecstart != $origstart + 0x123\n"
294 fail "objcopy --adjust-start"
296 pass "objcopy --adjust-start"
302 # Test adjusting the overall VMA, and adjusting the VMA of a
303 # particular section. We again only test this when generating S
309 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
311 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
314 while {[regexp $headers_regexp $got all name size vma rest]} {
318 if {$low == "" || $vma < $low} {
326 if {$low == "" || $origstart == ""} then {
327 perror "objdump can not recognize bintest.o"
329 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
330 if ![string equal "" $got] then {
331 fail "objcopy --adjust-vma"
333 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
334 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
335 if ![regexp $want $got all start vma] then {
336 fail "objcopy --adjust-vma"
339 if {$vma != $low + 0x123} then {
340 send_log "$vma != $low + 0x123\n"
341 fail "objcopy --adjust-vma"
343 if {$start != $origstart + 0x123} then {
344 send_log "$start != $origstart + 0x123\n"
345 fail "objcopy --adjust-vma"
347 pass "objcopy --adjust-vma"
355 while {[regexp $headers_regexp $got all name size vma rest]} {
357 if {$vma == $low} then {
358 set arg "$arg --adjust-section-vma $name+4"
363 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
364 if ![string equal "" $got] then {
365 fail "objcopy --adjust-section-vma +"
367 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
368 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
369 if ![regexp $want $got all vma] then {
370 fail "objcopy --adjust-section-vma +"
373 if {$vma != $low + 4} then {
374 send_log "$vma != $low + 4\n"
375 fail "objcopy --adjust-section-vma +"
377 pass "objcopy --adjust-section-vma +"
382 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
383 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
384 if ![string equal "" $got] then {
385 fail "objcopy --adjust-section-vma ="
387 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
388 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
389 if ![regexp $want $got all vma] then {
390 fail "objcopy --adjust-section-vma ="
393 if {$vma != $low + 4} then {
394 send_log "$vma != $low + 4\n"
395 fail "objcopy --adjust-section-vma ="
397 pass "objcopy --adjust-section-vma ="
403 # Test stripping an object.
405 proc strip_test { } {
418 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
423 set osabi_fail "false"
425 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
426 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
427 if { [lindex $exec_output 0] != 0 } then {
428 unresolved "$test preserving OS/ABI"
429 set osabi_fail "true"
431 verbose -log "grep OS/ABI tmpdir/osabi.in"
432 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
436 if [is_remote host] {
437 set archive libstrip.a
438 set objfile [remote_download host tmpdir/testprog.o]
439 remote_file host delete $archive
441 set archive tmpdir/libstrip.a
442 set objfile tmpdir/testprog.o
445 remote_file build delete tmpdir/libstrip.a
447 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
448 if ![string equal "" $exec_output] {
450 unresolved "$test preserving OS/ABI"
454 set exec_output [binutils_run $STRIP "-g $archive"]
455 if ![string equal "" $exec_output] {
457 unresolved "$test preserving OS/ABI"
461 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
462 if ![string equal "" $exec_output] {
464 unresolved "$test preserving OS/ABI"
468 if { $osabi_fail != "true" && [is_elf_format] } {
469 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
470 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
471 if { [lindex $exec_output 0] != 0 } then {
472 unresolved "$test preserving OS/ABI"
474 verbose -log "grep OS/ABI tmpdir/osabi.out"
475 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
476 if { "$osabi_in" == "$osabi_out" } {
477 pass "$test preserving OS/ABI"
479 fail "$test preserving OS/ABI"
484 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
489 if [is_remote host] {
490 set objfile [remote_download host tmpdir/testprog.o]
492 set objfile tmpdir/testprog.o
495 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
496 if ![string equal "" $exec_output] {
501 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
502 if ![string match "*: no symbols*" $exec_output] {
512 # Test stripping an object file with saving a symbol
514 proc strip_test_with_saving_a_symbol { } {
523 set test "strip with saving a symbol"
525 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
530 if [is_remote host] {
531 set objfile [remote_download host tmpdir/testprog.o]
533 set objfile tmpdir/testprog.o
536 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
537 if ![string equal "" $exec_output] {
542 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
543 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
544 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
552 strip_test_with_saving_a_symbol
554 # Build a final executable.
556 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
557 set test_prog "testprog.exe"
559 set test_prog "testprog"
562 proc copy_setup { } {
569 set res [build_wrapper testglue.o]
572 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
577 lappend flags "additional_flags=[lindex $res 1]"
578 set add_libs "testglue.o"
583 if { [istarget *-*-linux*]
584 || [istarget *-*-gnu*] } {
585 foreach i $gcc_gas_flag {
586 set flags "additional_flags=$i $flags"
589 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
593 set result [remote_load target tmpdir/$test_prog]
594 set status [lindex $result 0]
596 if { $status != "pass" } {
597 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
604 # Test copying an executable.
606 proc copy_executable { prog flags test1 test2 } {
609 if [is_remote host] {
610 set testfile [remote_download host tmpdir/$test_prog]
611 set testcopy copyprog
613 set testfile tmpdir/$test_prog
614 set testcopy tmpdir/copyprog
616 remote_file host delete $testcopy
618 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
620 if ![string equal "" $exec_output] {
622 if [string equal "" $test2] {
629 if [is_remote host] {
630 remote_upload host $testcopy tmpdir/copyprog
633 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
634 set exec_output [lindex $status 1]
636 if [string equal "" $exec_output] then {
639 send_log "$exec_output\n"
640 verbose "$exec_output"
642 # This will fail for many reasons. For example, it will most
643 # likely fail if a non-GNU linker is used. Therefore, we do
644 # not insist that it pass. If you are using an assembler and
645 # linker based on the same BFD as objcopy, it is worth
646 # investigating to see why this failure occurs. If we are
647 # cross compiling, we assume that a GNU linker is being used,
648 # and expect it to succeed.
649 if {[isnative]} then {
653 # This also fails for some mips targets. See elf32-mips.c
654 # mips_elf_sym_is_global.
655 if { [is_bad_symtab] } then {
659 setup_xfail "arm*-*-coff"
660 setup_xfail "arm*-*-pe"
661 setup_xfail "*-*-mingw*"
662 setup_xfail "*-*-cygwin*"
667 if [string equal "" $test2] {
671 set output [remote_load target tmpdir/copyprog]
672 set status [lindex $output 0]
673 if { $status != "pass" } {
680 # Test stripping an executable
682 proc strip_executable { prog flags test1 test2 } {
687 remote_file build delete tmpdir/striprog
688 remote_download build tmpdir/copyprog tmpdir/striprog
689 if [is_remote host] {
690 set copyfile [remote_download host tmpdir/striprog]
692 set copyfile tmpdir/striprog
695 set osabi_fail "false"
697 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
698 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
699 if { [lindex $exec_output 0] != 0 } then {
700 unresolved "$test1 preserving OS/ABI"
701 set osabi_fail "true"
703 verbose -log "grep OS/ABI tmpdir/osabi.in"
704 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
708 set exec_output [binutils_run $prog "$flags ${copyfile}"]
709 if ![string equal "" $exec_output] {
711 if [string equal "" $test2] {
718 if [is_remote host] {
719 remote_upload host ${copyfile} tmpdir/striprog
722 if { $osabi_fail != "true" && [is_elf_format] } {
723 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
724 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
725 if { [lindex $exec_output 0] != 0 } then {
726 unresolved "$test1 preserving OS/ABI"
728 verbose -log "grep OS/ABI tmpdir/osabi.out"
729 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
730 if { "$osabi_in" == "$osabi_out" } {
731 pass "$test1 preserving OS/ABI"
733 fail "$test1 preserving OS/ABI"
738 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
739 if ![string match "*: no symbols*" $exec_output] {
744 if [string equal "" $test2] {
748 set result [remote_load target tmpdir/striprog]
749 set status [lindex $result 0]
750 if { $status != "pass" } {
758 # Test stripping an executable with saving a symbol
760 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
764 remote_file build delete tmpdir/striprog
765 remote_download build tmpdir/copyprog tmpdir/striprog
766 if [is_remote host] {
767 set copyfile [remote_download host tmpdir/striprog]
769 set copyfile tmpdir/striprog
772 set exec_output [binutils_run $prog "$flags ${copyfile}"]
773 if ![string equal "" $exec_output] {
775 if [string equal "" $test2] {
782 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
783 if { [istarget mmix-knuth-mmixware] } {
784 # Whenever there's a symbol in the mmo format, there's the symbol
785 # Main, so remove it manually from the expected output for sake of
788 # Using "" not {} to get the \n and \r translated.
789 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
792 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
793 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
798 if [string equal "" $test2] {
802 if [is_remote host] {
803 remote_upload host ${copyfile} tmpdir/striprog
806 set result [remote_load target tmpdir/striprog]
807 set status [lindex $result 0]
808 if { $status != "pass" } {
816 # Test keeping only debug symbols of an executable
818 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
819 remote_file build delete tmpdir/striprog
820 remote_download build tmpdir/copyprog tmpdir/striprog
821 if [is_remote host] {
822 set copyfile [remote_download host tmpdir/striprog]
824 set copyfile tmpdir/striprog
827 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
828 if ![string equal "" $exec_output] {
834 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
835 if ![string equal "" $exec_output] {
842 # Tests that in a debug only copy of a file the sections
843 # headers whose types have been changed to NOBITS still
844 # retain their sh_link fields.
846 proc keep_debug_symbols_and_check_links { prog flags test } {
849 remote_file build delete tmpdir/striprog
850 remote_download build tmpdir/copyprog tmpdir/striprog
851 if [is_remote host] {
852 set copyfile [remote_download host tmpdir/striprog]
854 set copyfile tmpdir/striprog
857 set exec_output [binutils_run $prog "$flags ${copyfile}"]
858 if ![string equal "" $exec_output] {
863 set got [binutils_run $READELF "-S --wide ${copyfile}"]
866 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
868 {[^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]+)(.*)} \
869 $got all name link rest]} {
872 # Only some NOBITS sections should have a non-zero sh_link field.
873 # Look for them by name.
874 verbose "NOBITS section .$name has a 0 sh_link field\n"
876 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
877 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
882 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
883 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
897 set test1 "simple objcopy of executable"
898 set test1r "run objcopy of executable"
899 set test2 "strip executable"
900 set test2r "run stripped executable"
901 set test3 "strip executable with saving a symbol"
902 set test3r "run stripped executable with saving a symbol"
903 set test4 "keep only debug data"
904 set test5 "simple objcopy of debug data"
906 set test6 "NOBITS sections retain sh_link field"
909 switch [copy_setup] {
927 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
929 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
931 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
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"
940 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
941 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
942 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
943 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
944 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
946 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
951 proc objcopy_test_readelf {testname srcfile} {
958 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
959 unresolved "objcopy ($testname)"
963 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
964 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
965 if { [lindex $exec_output 0] != 0
966 || ![string equal "" [lindex $exec_output 1]] } then {
967 fail "objcopy ($testname)"
971 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
972 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
973 if { [lindex $exec_output 0] != 0 } then {
974 unresolved "objcopy ($testname)"
977 set exec_output [prune_warnings [lindex $exec_output 1]]
978 if ![string equal "" $exec_output] then {
979 unresolved "objcopy ($testname)"
983 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
984 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
985 if { [lindex $exec_output 0] != 0 } then {
986 unresolved "objcopy ($testname)"
989 set exec_output [prune_warnings [lindex $exec_output 1]]
990 if ![string equal "" $exec_output] then {
991 unresolved "objcopy ($testname)"
995 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
996 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
997 set exec_output [prune_warnings $exec_output]
999 if [string equal "" $exec_output] then {
1000 pass "objcopy ($testname)"
1002 fail "objcopy ($testname)"
1006 proc objcopy_test_symbol_manipulation {} {
1010 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1011 foreach t $test_list {
1012 # We need to strip the ".d", but can leave the dirname.
1013 verbose [file rootname $t]
1014 run_dump_test [file rootname $t]
1018 proc objcopy_test_elf_common_symbols {} {
1022 # hpux has a non-standard common directive.
1023 if { [istarget "*-*-hpux*"] } then {
1027 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1028 foreach t $test_list {
1029 # We need to strip the ".d", but can leave the dirname.
1030 verbose [file rootname $t]
1031 run_dump_test [file rootname $t]
1035 # ia64 specific tests
1036 if { ([istarget "ia64-*-elf*"]
1037 || [istarget "ia64-*-linux*"]) } {
1038 objcopy_test "ia64 link order" link-order.s
1041 # ELF specific tests
1042 if [is_elf_format] {
1043 objcopy_test_symbol_manipulation
1044 objcopy_test_elf_common_symbols
1045 objcopy_test "ELF unknown section type" unknown.s
1046 objcopy_test_readelf "ELF group" group.s
1047 objcopy_test_readelf "ELF group" group-2.s
1048 objcopy_test_readelf "ELF group" group-3.s
1049 objcopy_test_readelf "ELF group" group-4.s
1050 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1051 run_dump_test "group-5"
1052 run_dump_test "group-6"
1053 run_dump_test "group-7a"
1054 run_dump_test "group-7b"
1055 run_dump_test "group-7c"
1056 run_dump_test "copy-1"
1057 run_dump_test "note-1"
1058 if [is_elf64 tmpdir/bintest.o] {
1059 run_dump_test "note-2-64"
1060 run_dump_test "note-3-64"
1061 run_dump_test "note-4-64"
1063 run_dump_test "note-2-32"
1064 run_dump_test "note-3-32"
1065 run_dump_test "note-4-32"
1069 run_dump_test "copy-2"
1070 run_dump_test "copy-3"
1071 run_dump_test "copy-4"
1072 run_dump_test "pr19020a"
1073 run_dump_test "pr19020b"
1075 if [is_elf_format] {
1076 run_dump_test "strip-1"
1077 run_dump_test "strip-2"
1078 run_dump_test "strip-3"
1079 run_dump_test "strip-4"
1080 run_dump_test "strip-5"
1081 run_dump_test "strip-6"
1082 run_dump_test "strip-7"
1083 run_dump_test "strip-8"
1084 run_dump_test "strip-9"
1085 run_dump_test "strip-12"
1087 if { [istarget "mips64*-*-openbsd*"] } {
1088 set reloc_format mips64
1089 } elseif { [istarget "arm-*"] \
1090 || [istarget "d10v-*"] \
1091 || [istarget "dlx-*"] \
1092 || [istarget "i*86-*"] \
1093 || [istarget "m681*-*"] \
1094 || [istarget "m68hc1*-*"] \
1095 || ([istarget "mips*-*"] \
1096 && ![istarget "mips64*-ps2-elf*"] \
1097 && ![istarget "*-*-irix6*"] \
1098 && ![istarget "mips64*-*-freebsd*"] \
1099 && ![istarget "mips64*-*-kfreebsd*-gnu"] \
1100 && ![istarget "mips64*-*-linux*"]) \
1101 || [istarget "score*-*"] \
1102 || [istarget "xgate-*"] } {
1103 set reloc_format rel
1105 set reloc_format rela
1107 run_dump_test "strip-13" [list [list source strip-13${reloc_format}.s]]
1108 # Select a relocation number that corresponds to one actually
1109 # supported by the target and ABI being tested.
1110 if { [istarget "aarch64*-*"] } {
1112 } elseif { [istarget "ia64*-*"] \
1113 || [istarget "m32r*-*"] \
1114 || [istarget "nds32*-*"] \
1115 || [istarget "v850*-*"] } {
1117 } elseif { [istarget "pru-*"] } {
1122 run_dump_test "strip-14" [list \
1123 [list source strip-14${reloc_format}.s] \
1124 [list as "--defsym RELOC=${reloc}"] \
1125 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1126 ? "--defsym ELF64=1" : ""}]]]
1127 run_dump_test "strip-15" [list \
1128 [list source strip-15${reloc_format}.s] \
1129 [list as "--defsym RELOC=${reloc}"] \
1130 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1131 ? "--defsym ELF64=1" : ""}]]]
1133 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1134 if { [supports_gnu_unique] } {
1135 run_dump_test "strip-10"
1137 set extra_strip11 ""
1138 if { [istarget "sh64*-*"] } {
1140 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1142 run_dump_test "strip-11" $extra_strip11
1144 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1145 # Check to make sure we don't strip a symbol named in relocations.
1146 set test "objcopy keeps symbols needed by relocs"
1148 set srcfile $srcdir/$subdir/needed-by-reloc.s
1150 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1153 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1155 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1163 # The symbol table for some MIPS targets is sorted differently than
1164 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1166 if { [is_bad_symtab] } then {
1169 run_dump_test "localize-hidden-1"
1170 run_dump_test "testranges"
1171 run_dump_test "testranges-ia64"
1173 run_dump_test "add-section"
1174 run_dump_test "add-symbol"
1175 run_dump_test "add-empty-section"
1177 run_dump_test "exclude-1a"
1178 run_dump_test "exclude-1b"
1180 run_dump_test "only-section-01"
1181 run_dump_test "remove-section-01"
1183 # Test the remove relocation functionality
1184 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1185 foreach t $test_list {
1186 # We need to strip the ".d", but can leave the dirname.
1187 verbose [file rootname $t]
1188 run_dump_test [file rootname $t]
1191 run_dump_test "localize-hidden-2"
1193 # Test objcopying an object file without global symbol
1195 proc objcopy_test_without_global_symbol { } {
1203 set test "strip without global symbol "
1205 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1210 if [is_remote host] {
1211 set objfile [remote_download host tmpdir/pr19547.o]
1213 set objfile tmpdir/pr19547.o
1216 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1217 if ![string equal "" $exec_output] {
1222 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1223 if {![regexp "no symbols" $exec_output]} {
1231 # The AArch64 and ARM targets preserve mapping symbols
1232 # in object files, so they will fail this test.
1233 setup_xfail aarch64*-*-* arm*-*-*
1235 objcopy_test_without_global_symbol