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} $tempfile]} then {
50 perror "unresolved $testname"
51 unresolved "objcopy ($testname)"
52 remote_file host delete $tempfile
56 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
58 if ![string equal "" $got] then {
59 fail "objcopy ($testname)"
61 send_log "cmp $tempfile ${copyfile}.o\n"
62 verbose "cmp $tempfile ${copyfile}.o"
64 set src1 tmpdir/bintest.o
65 set src2 tmpdir/copy.o
66 remote_upload host $tempfile $src1
67 remote_upload host ${copyfile}.o $src2
70 set src2 ${copyfile}.o
72 set status [remote_exec build cmp "${src1} ${src2}"]
73 set exec_output [lindex $status 1]
74 set exec_output [prune_warnings $exec_output]
76 # On some systems the result of objcopy will not be identical.
77 # Usually this is just because gas isn't using bfd to write the
78 # files in the first place, and may order things a little
79 # differently. Those systems should use setup_xfail here.
81 setup_xfail "h8300-*-coff"
82 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
83 setup_xfail "hppa*-*-*"
85 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
86 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
88 setup_xfail "sh-*-coff*"
89 setup_xfail "tic80-*-*" "w65-*"
91 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
92 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
93 clear_xfail "m68*-*-sysv4*"
95 if [string equal "" $exec_output] then {
96 pass "objcopy ($testname)"
98 send_log "$exec_output\n"
99 verbose "$exec_output" 1
101 # On OSF/1, this succeeds with gas and fails with /bin/as.
102 setup_xfail "alpha*-*-osf*"
104 fail "objcopy ($testname)"
109 objcopy_test "simple copy" bintest.s
111 if { [file exists $tempfile] } {
112 # Test reversing bytes in a section.
114 set reversed ${tempfile}-reversed
115 set sect_names [get_standard_section_names]
116 if { $sect_names != "" } {
117 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
119 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
122 if ![string equal "" $got] then {
123 fail "objcopy --reverse-bytes"
125 if [is_remote host] {
126 remote_upload host ${reversed} tmpdir/copy-reversed.o
127 set reversed tmpdir/copy-reversed.o
130 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
131 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
133 set want "^ \[0-9\]+ (\[0-9\]+)"
134 set found_orig [regexp -lineanchor $want $origdata -> origdata]
135 set found_rev [regexp -lineanchor $want $revdata -> revdata]
137 if {$found_orig == 0 || $found_rev == 0} then {
138 # som doesn't have a .data section
139 setup_xfail "hppa*-*-hpux*"
140 clear_xfail "hppa*64*-*-hpux*"
142 fail "objcopy --reverse-bytes"
144 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
145 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
147 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
148 pass "objcopy --reverse-bytes"
150 fail "objcopy --reverse-bytes"
156 # Test interleaved copy of multiple byte width
158 set sequence_file sequence_file
159 set file [open ${sequence_file} w]
160 puts ${file} "12345678"
163 if [is_remote host] {
164 remote_upload host ${sequence_file} tmpdir/sequence_file
165 set sequence_file tmpdir/sequence_file
168 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
170 if ![string equal "" $got] then {
171 fail "objcopy -i --interleave-width"
173 if [is_remote host] {
174 remote_upload host ${copyfile} tmpdir/interleave_output
175 set interleave_output tmpdir/interleave_output
177 set interleave_output ${copyfile}
180 set file [open ${interleave_output} r]
185 if ![string equal "1256" $line] then {
186 fail "objcopy -i --interleave-width"
188 pass "objcopy -i --interleave-width"
193 # Test generating S records.
195 if { [file exists $tempfile] } {
196 # We make the srec filename 8.3 compatible. Note that the header string
197 # matched against depends on the name of the file. Ugh.
199 if [is_remote host] {
200 set srecfile copy.sre
201 set header_string S00B0000636F70792E737265C1
203 set srecfile ${copyfile}.srec
204 set header_string S0130000746D706469722F636F70792E7372656397
207 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
209 if ![string equal "" $got] then {
210 fail "objcopy -O srec"
212 if [is_remote host] {
213 remote_upload host ${srecfile} tmpdir/copy.srec
214 set srecfile tmpdir/copy.srec
216 set file [open ${srecfile} r]
218 # The first S record is fixed by the file name we are using.
222 if ![regexp "$header_string.*" $line] {
223 send_log "bad header\n"
224 fail "objcopy -O srec"
226 while {[gets $file line] != -1 \
227 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
234 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
235 send_log "bad trailer\n"
236 fail "objcopy -O srec"
238 if {[gets $file line] != -1} then {
239 send_log "garbage at end\n"
242 fail "objcopy -O srec"
244 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
245 if ![regexp "file format srec" $got] then {
246 send_log "objdump failed\n"
247 fail "objcopy -O srec"
249 pass "objcopy -O srec"
259 # Test setting and adjusting the start address. We only test this
260 # while generating S records, because we may not be able to set the
261 # start address for other object file formats, and the S record case
262 # is the only useful one anyhow.
264 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
265 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
266 perror "objdump can not recognize bintest.o"
269 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
270 if ![string equal "" $got] then {
271 fail "objcopy --set-start"
273 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
274 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
275 fail "objcopy --set-start"
277 if {$srecstart != 0x7654} then {
278 send_log "$srecstart != 0x7654\n"
279 fail "objcopy --set-start"
281 pass "objcopy --set-start"
286 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
287 if ![string equal "" $got] then {
288 fail "objcopy --adjust-start"
290 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
291 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
292 fail "objcopy --adjust-start"
294 if {$srecstart != $origstart + 0x123} then {
295 send_log "$srecstart != $origstart + 0x123\n"
296 fail "objcopy --adjust-start"
298 pass "objcopy --adjust-start"
304 # Test adjusting the overall VMA, and adjusting the VMA of a
305 # particular section. We again only test this when generating S
311 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
313 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
316 while {[regexp $headers_regexp $got all name size vma rest]} {
320 if {$low == "" || $vma < $low} {
328 if {$low == "" || $origstart == ""} then {
329 perror "objdump can not recognize bintest.o"
331 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
332 if ![string equal "" $got] then {
333 fail "objcopy --adjust-vma"
335 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
336 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
337 if ![regexp $want $got all start vma] then {
338 fail "objcopy --adjust-vma"
341 if {$vma != $low + 0x123} then {
342 send_log "$vma != $low + 0x123\n"
343 fail "objcopy --adjust-vma"
345 if {$start != $origstart + 0x123} then {
346 send_log "$start != $origstart + 0x123\n"
347 fail "objcopy --adjust-vma"
349 pass "objcopy --adjust-vma"
357 while {[regexp $headers_regexp $got all name size vma rest]} {
359 if {$vma == $low} then {
360 set arg "$arg --adjust-section-vma $name+4"
365 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
366 if ![string equal "" $got] then {
367 fail "objcopy --adjust-section-vma +"
369 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
370 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
371 if ![regexp $want $got all vma] then {
372 fail "objcopy --adjust-section-vma +"
375 if {$vma != $low + 4} then {
376 send_log "$vma != $low + 4\n"
377 fail "objcopy --adjust-section-vma +"
379 pass "objcopy --adjust-section-vma +"
384 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
385 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
386 if ![string equal "" $got] then {
387 fail "objcopy --adjust-section-vma ="
389 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
390 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
391 if ![regexp $want $got all vma] then {
392 fail "objcopy --adjust-section-vma ="
395 if {$vma != $low + 4} then {
396 send_log "$vma != $low + 4\n"
397 fail "objcopy --adjust-section-vma ="
399 pass "objcopy --adjust-section-vma ="
405 # Test stripping an object.
407 proc strip_test { } {
420 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
425 set osabi_fail "false"
427 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
428 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
429 if { [lindex $exec_output 0] != 0 } then {
430 unresolved "$test preserving OS/ABI"
431 set osabi_fail "true"
433 verbose -log "grep OS/ABI tmpdir/osabi.in"
434 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
438 if [is_remote host] {
439 set archive libstrip.a
440 set objfile [remote_download host tmpdir/testprog.o]
441 remote_file host delete $archive
443 set archive tmpdir/libstrip.a
444 set objfile tmpdir/testprog.o
447 remote_file build delete tmpdir/libstrip.a
449 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
450 if ![string equal "" $exec_output] {
452 unresolved "$test preserving OS/ABI"
456 set exec_output [binutils_run $STRIP "-g $archive"]
457 if ![string equal "" $exec_output] {
459 unresolved "$test preserving OS/ABI"
463 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
464 if ![string equal "" $exec_output] {
466 unresolved "$test preserving OS/ABI"
470 if { $osabi_fail != "true" && [is_elf_format] } {
471 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
472 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
473 if { [lindex $exec_output 0] != 0 } then {
474 unresolved "$test preserving OS/ABI"
476 verbose -log "grep OS/ABI tmpdir/osabi.out"
477 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
478 if { "$osabi_in" == "$osabi_out" } {
479 pass "$test preserving OS/ABI"
481 fail "$test preserving OS/ABI"
486 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
491 if [is_remote host] {
492 set objfile [remote_download host tmpdir/testprog.o]
494 set objfile tmpdir/testprog.o
497 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
498 if ![string equal "" $exec_output] {
503 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
504 if ![string match "*: no symbols*" $exec_output] {
514 # Test stripping an object file with saving a symbol
516 proc strip_test_with_saving_a_symbol { } {
525 set test "strip with saving a symbol"
527 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
532 if [is_remote host] {
533 set objfile [remote_download host tmpdir/testprog.o]
535 set objfile tmpdir/testprog.o
538 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
539 if ![string equal "" $exec_output] {
544 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
545 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
546 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
554 strip_test_with_saving_a_symbol
556 # Build a final executable.
558 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
559 set test_prog "testprog.exe"
561 set test_prog "testprog"
564 proc copy_setup { } {
571 set res [build_wrapper testglue.o]
574 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
579 lappend flags "additional_flags=[lindex $res 1]"
580 set add_libs "testglue.o"
585 if { [istarget *-*-linux*]
586 || [istarget *-*-gnu*] } {
587 foreach i $gcc_gas_flag {
588 set flags "additional_flags=$i $flags"
591 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
595 set result [remote_load target tmpdir/$test_prog]
596 set status [lindex $result 0]
598 if { $status != "pass" } {
599 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
606 # Test copying an executable.
608 proc copy_executable { prog flags test1 test2 } {
611 if [is_remote host] {
612 set testfile [remote_download host tmpdir/$test_prog]
613 set testcopy copyprog
615 set testfile tmpdir/$test_prog
616 set testcopy tmpdir/copyprog
618 remote_file host delete $testcopy
620 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
622 if ![string equal "" $exec_output] {
624 if [string equal "" $test2] {
631 if [is_remote host] {
632 remote_upload host $testcopy tmpdir/copyprog
635 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
636 set exec_output [lindex $status 1]
638 if [string equal "" $exec_output] then {
641 send_log "$exec_output\n"
642 verbose "$exec_output"
644 # This will fail for many reasons. For example, it will most
645 # likely fail if a non-GNU linker is used. Therefore, we do
646 # not insist that it pass. If you are using an assembler and
647 # linker based on the same BFD as objcopy, it is worth
648 # investigating to see why this failure occurs. If we are
649 # cross compiling, we assume that a GNU linker is being used,
650 # and expect it to succeed.
651 if {[isnative]} then {
655 # This also fails for mips*-*-elf targets. See elf32-mips.c
656 # mips_elf_sym_is_global.
657 setup_xfail "mips*-*-elf" "tx39-*-*"
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 whoes 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 run_dump_test "group-5"
1051 run_dump_test "group-6"
1052 run_dump_test "copy-1"
1053 run_dump_test "note-1"
1056 run_dump_test "copy-2"
1057 run_dump_test "copy-3"
1058 run_dump_test "copy-4"
1059 run_dump_test "pr19020a"
1060 run_dump_test "pr19020b"
1062 if [is_elf_format] {
1063 run_dump_test "strip-1"
1064 run_dump_test "strip-2"
1065 run_dump_test "strip-3"
1066 run_dump_test "strip-4"
1067 run_dump_test "strip-5"
1068 run_dump_test "strip-6"
1069 run_dump_test "strip-7"
1070 run_dump_test "strip-8"
1071 run_dump_test "strip-9"
1072 run_dump_test "strip-12"
1073 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1074 if { [supports_gnu_unique] } {
1075 run_dump_test "strip-10"
1077 set extra_strip11 ""
1078 if { [istarget "sh64*-*"] } {
1080 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1082 run_dump_test "strip-11" $extra_strip11
1084 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1085 # Check to make sure we don't strip a symbol named in relocations.
1086 set test "objcopy keeps symbols needed by relocs"
1088 set srcfile $srcdir/$subdir/needed-by-reloc.s
1090 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1093 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1095 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1103 # The symbol table for some MIPS targets is sorted differently than
1104 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1105 # to match. These tests must be matched to targets for which
1106 # targ_defvec=mips_elf32_be_vec,
1107 # targ_defvec=mips_elf32_le_vec,
1108 # targ_defvec=mips_elf32_n_be_vec or
1109 # targ_defvec=mips_elf32_n_le_vec in config.bfd. When syncing,
1110 # don't forget that earlier case-matches trump later ones.
1111 if { ![istarget "mips*-sde-elf*"] && ![istarget "mips*-mti-elf*"]
1112 && ![istarget "mips*-img-elf*"]
1113 && ![istarget "mips64*-*-openbsd*"] } {
1114 setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-elf*" \
1115 "mips*-*-rtems*" "mips*-*-windiss" "mips*-*-none" \
1116 "mips*-*-openbsd*" "mips*-*-chorus*"
1118 run_dump_test "localize-hidden-1"
1119 run_dump_test "testranges"
1120 run_dump_test "testranges-ia64"
1122 run_dump_test "add-section"
1123 run_dump_test "add-symbol"
1124 run_dump_test "add-empty-section"
1126 run_dump_test "exclude-1a"
1127 run_dump_test "exclude-1b"
1129 run_dump_test "localize-hidden-2"
1131 # Test objcopying an object file without global symbol
1133 proc objcopy_test_without_global_symbol { } {
1141 set test "strip without global symbol "
1143 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1148 if [is_remote host] {
1149 set objfile [remote_download host tmpdir/pr19547.o]
1151 set objfile tmpdir/pr19547.o
1154 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1155 if ![string equal "" $exec_output] {
1160 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1161 if {![regexp "no symbols" $exec_output]} {
1169 objcopy_test_without_global_symbol