1 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 # Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19 # Please email any bugs, comments, and/or additions to this file to:
20 # bug-dejagnu@prep.ai.mit.edu
22 # Written by Ian Lance Taylor <ian@cygnus.com>
24 if ![is_remote host] {
25 if {[which $OBJCOPY] == 0} then {
26 perror "$OBJCOPY does not exist"
31 send_user "Version [binutil_version $OBJCOPY]"
33 if ![is_remote host] {
34 set tempfile tmpdir/bintest.o
35 set copyfile tmpdir/copy
37 set tempfile [remote_download host tmpdir/bintest.o]
41 # Test that objcopy does not modify a file when copying it.
43 proc objcopy_test {testname srcfile} {
51 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
52 perror "unresolved $testname"
53 unresolved "objcopy ($testname)"
57 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
59 if ![string match "" $got] then {
60 fail "objcopy ($testname)"
62 send_log "cmp $tempfile ${copyfile}.o\n"
63 verbose "cmp $tempfile ${copyfile}.o"
65 set src1 tmpdir/bintest.o
66 set src2 tmpdir/copy.o
67 remote_upload host $tempfile $src1
68 remote_upload host ${copyfile}.o $src2
71 set src2 ${copyfile}.o
73 set status [remote_exec build cmp "${src1} ${src2}"]
74 set exec_output [lindex $status 1]
75 set exec_output [prune_warnings $exec_output]
77 # On some systems the result of objcopy will not be identical.
78 # Usually this is just because gas isn't using bfd to write the
79 # files in the first place, and may order things a little
80 # differently. Those systems should use setup_xfail here.
82 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
83 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
84 setup_xfail "hppa*-*-*"
86 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
87 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
89 setup_xfail "or32-*-rtems*" "or32-*-coff"
90 setup_xfail "sh-*-coff*" "sh-*-rtems*"
91 setup_xfail "tic4x-*-*" "tic80-*-*" "w65-*"
93 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
94 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
95 clear_xfail "m68*-*-sysv4*"
97 if [string match "" $exec_output] then {
98 pass "objcopy ($testname)"
100 send_log "$exec_output\n"
101 verbose "$exec_output" 1
103 # On OSF/1, this succeeds with gas and fails with /bin/as.
104 setup_xfail "alpha*-*-osf*"
106 # This fails for COFF i960-vxworks targets.
107 setup_xfail "i960-*-vxworks*"
109 fail "objcopy ($testname)"
114 objcopy_test "simple copy" bintest.s
116 # Test reversing bytes in a section.
118 set reversed ${tempfile}-reversed
119 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
121 if ![string match "" $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 fail "objcopy --reverse-bytes"
139 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
140 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
142 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
143 pass "objcopy --reverse-bytes"
145 fail "objcopy --reverse-bytes"
150 # Test generating S records.
152 # We make the srec filename 8.3 compatible. Note that the header string
153 # matched against depends on the name of the file. Ugh.
155 if [is_remote host] {
156 set srecfile copy.sre
157 set header_string S00B0000636F70792E737265C1
159 set srecfile ${copyfile}.srec
160 set header_string S0130000746D706469722F636F70792E7372656397
163 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
165 if ![string match "" $got] then {
166 fail "objcopy -O srec"
168 if [is_remote host] {
169 remote_upload host ${srecfile} tmpdir/copy.srec
170 set srecfile tmpdir/copy.srec
172 set file [open ${srecfile} r]
174 # The first S record is fixed by the file name we are using.
178 if ![regexp "$header_string.*" $line] {
179 send_log "bad header\n"
180 fail "objcopy -O srec"
182 while {[gets $file line] != -1 \
183 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
190 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
191 send_log "bad trailer\n"
192 fail "objcopy -O srec"
194 if {[gets $file line] != -1} then {
195 send_log "garbage at end\n"
198 fail "objcopy -O srec"
200 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
201 if ![regexp "file format srec" $got] then {
202 send_log "objdump failed\n"
203 fail "objcopy -O srec"
205 pass "objcopy -O srec"
214 # Test setting and adjusting the start address. We only test this
215 # while generating S records, because we may not be able to set the
216 # start address for other object file formats, and the S record case
217 # is the only useful one anyhow.
219 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
220 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
221 perror "objdump can not recognize bintest.o"
224 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
225 if ![string match "" $got] then {
226 fail "objcopy --set-start"
228 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
229 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
230 fail "objcopy --set-start"
232 if {$srecstart != 0x7654} then {
233 send_log "$srecstart != 0x7654\n"
234 fail "objcopy --set-start"
236 pass "objcopy --set-start"
241 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
242 if ![string match "" $got] then {
243 fail "objcopy --adjust-start"
245 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
246 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
247 fail "objcopy --adjust-start"
249 if {$srecstart != $origstart + 0x123} then {
250 send_log "$srecstart != $origstart + 0x123\n"
251 fail "objcopy --adjust-start"
253 pass "objcopy --adjust-start"
259 # Test adjusting the overall VMA, and adjusting the VMA of a
260 # particular section. We again only test this when generating S
266 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
268 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
271 while {[regexp $headers_regexp $got all name size vma rest]} {
275 if {$low == "" || $vma < $low} {
283 if {$low == "" || $origstart == ""} then {
284 perror "objdump can not recognize bintest.o"
286 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
287 if ![string match "" $got] then {
288 fail "objcopy --adjust-vma"
290 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
291 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
292 if ![regexp $want $got all start vma] then {
293 fail "objcopy --adjust-vma"
296 if {$vma != $low + 0x123} then {
297 send_log "$vma != $low + 0x123\n"
298 fail "objcopy --adjust-vma"
300 if {$start != $origstart + 0x123} then {
301 send_log "$start != $origstart + 0x123\n"
302 fail "objcopy --adjust-vma"
304 pass "objcopy --adjust-vma"
312 while {[regexp $headers_regexp $got all name size vma rest]} {
314 if {$vma == $low} then {
315 set arg "$arg --adjust-section-vma $name+4"
320 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
321 if ![string match "" $got] then {
322 fail "objcopy --adjust-section-vma +"
324 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
325 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
326 if ![regexp $want $got all vma] then {
327 fail "objcopy --adjust-section-vma +"
330 if {$vma != $low + 4} then {
331 send_log "$vma != $low + 4\n"
332 fail "objcopy --adjust-section-vma +"
334 pass "objcopy --adjust-section-vma +"
339 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
340 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
341 if ![string match "" $got] then {
342 fail "objcopy --adjust-section-vma ="
344 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
345 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
346 if ![regexp $want $got all vma] then {
347 fail "objcopy --adjust-section-vma ="
350 if {$vma != $low + 4} then {
351 send_log "$vma != $low + 4\n"
352 fail "objcopy --adjust-section-vma ="
354 pass "objcopy --adjust-section-vma ="
360 # Test stripping an object.
362 proc strip_test { } {
374 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
379 if [is_remote host] {
380 set archive libstrip.a
381 set objfile [remote_download host tmpdir/testprog.o]
382 remote_file host delete $archive
384 set archive tmpdir/libstrip.a
385 set objfile tmpdir/testprog.o
388 remote_file build delete tmpdir/libstrip.a
390 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
391 if ![string match "" $exec_output] {
396 set exec_output [binutils_run $STRIP "-g $archive"]
397 if ![string match "" $exec_output] {
402 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
403 if ![string match "" $exec_output] {
408 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
413 if [is_remote host] {
414 set objfile [remote_download host tmpdir/testprog.o]
416 set objfile tmpdir/testprog.o
419 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
420 if ![string match "" $exec_output] {
425 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
426 if ![string match "*: no symbols*" $exec_output] {
436 # Test stripping an object file with saving a symbol
438 proc strip_test_with_saving_a_symbol { } {
447 set test "strip with saving a symbol"
449 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
454 if [is_remote host] {
455 set objfile [remote_download host tmpdir/testprog.o]
457 set objfile tmpdir/testprog.o
460 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
461 if ![string match "" $exec_output] {
466 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
467 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
468 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
476 strip_test_with_saving_a_symbol
478 # Build a final executable.
480 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
481 set test_prog "testprog.exe"
483 set test_prog "testprog"
486 proc copy_setup { } {
492 set res [build_wrapper testglue.o]
495 if { [istarget *-*-uclinux*] } {
500 lappend flags "additional_flags=[lindex $res 1]"
501 set add_libs "testglue.o"
506 if { [istarget *-*-linux*] } {
507 foreach i $gcc_gas_flag {
508 set flags "additional_flags=$i $flags"
511 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
515 set result [remote_load target tmpdir/$test_prog]
516 set status [lindex $result 0]
518 if { $status != "pass" } {
519 perror "unresolved setup, status = $status"
526 # Test copying an executable.
528 proc copy_executable { prog flags test1 test2 } {
531 if [is_remote host] {
532 set testfile [remote_download host tmpdir/$test_prog]
533 set testcopy copyprog
535 set testfile tmpdir/$test_prog
536 set testcopy tmpdir/copyprog
538 remote_file host delete $testcopy
540 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
542 if ![string match "" $exec_output] {
548 if [is_remote host] {
549 remote_upload host $testcopy tmpdir/copyprog
552 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
553 set exec_output [lindex $status 1]
555 if [string match "" $exec_output] then {
558 send_log "$exec_output\n"
559 verbose "$exec_output"
561 # This will fail for many reasons. For example, it will most
562 # likely fail if a non-GNU linker is used. Therefore, we do
563 # not insist that it pass. If you are using an assembler and
564 # linker based on the same BFD as objcopy, it is worth
565 # investigating to see why this failure occurs. If we are
566 # cross compiling, we assume that a GNU linker is being used,
567 # and expect it to succeed.
568 if {[isnative]} then {
572 # This also fails for mips*-*-elf targets. See elf32-mips.c
573 # mips_elf_sym_is_global.
574 setup_xfail "mips*-*-elf"
576 setup_xfail "*arm*-*-coff"
577 setup_xfail "xscale-*-coff"
578 setup_xfail "arm*-*-pe"
579 setup_xfail "thumb*-*-coff"
580 setup_xfail "thumb*-*-pe"
585 set output [remote_load target tmpdir/copyprog]
586 set status [lindex $output 0]
587 if { $status != "pass" } {
594 # Test stripping an executable
596 proc strip_executable { prog flags test } {
600 remote_download build tmpdir/copyprog tmpdir/striprog
601 if [is_remote host] {
602 set copyfile [remote_download host tmpdir/striprog]
604 set copyfile tmpdir/striprog
607 set exec_output [binutils_run $prog "$flags ${copyfile}"]
608 if ![string match "" $exec_output] {
613 if [is_remote host] {
614 remote_upload host ${copyfile} tmpdir/striprog
617 set result [remote_load target tmpdir/striprog]
618 set status [lindex $result 0]
619 if { $status != "pass" } {
624 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
625 if ![string match "*: no symbols*" $exec_output] {
632 # Test stripping an executable with saving a symbol
634 proc strip_executable_with_saving_a_symbol { prog flags test } {
638 remote_download build tmpdir/copyprog tmpdir/striprog
639 if [is_remote host] {
640 set copyfile [remote_download host tmpdir/striprog]
642 set copyfile tmpdir/striprog
645 set exec_output [binutils_run $prog "$flags ${copyfile}"]
646 if ![string match "" $exec_output] {
651 if [is_remote host] {
652 remote_upload host ${copyfile} tmpdir/striprog
655 set result [remote_load target tmpdir/striprog]
656 set status [lindex $result 0]
657 if { $status != "pass" } {
662 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
663 if { [istarget mmix-knuth-mmixware] } {
664 # Whenever there's a symbol in the mmo format, there's the symbol
665 # Main, so remove it manually from the expected output for sake of
668 # Using "" not {} to get the \n and \r translated.
669 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
672 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
673 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
680 set test1 "simple objcopy of executable"
681 set test2 "run objcopy of executable"
682 set test3 "run stripped executable"
683 set test4 "run stripped executable with saving a symbol"
685 switch [copy_setup] {
702 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
703 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
704 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
708 proc objcopy_test_readelf {testname srcfile} {
715 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
716 unresolved "objcopy ($testname)"
720 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
721 catch "exec $OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o" exec_output
722 if ![string match "" $exec_output] then {
723 fail "objcopy ($testname)"
727 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
728 catch "exec $READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out" exec_output
729 set exec_output [prune_warnings $exec_output]
730 if ![string match "" $exec_output] then {
731 unresolved "objcopy ($testname)"
735 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
736 catch "exec $READELF -a tmpdir/copy.o > tmpdir/copy.o.out" exec_output
737 set exec_output [prune_warnings $exec_output]
738 if ![string match "" $exec_output] then {
739 unresolved "objcopy ($testname)"
743 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
744 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
745 set exec_output [prune_warnings $exec_output]
747 if [string match "" $exec_output] then {
748 pass "objcopy ($testname)"
750 fail "objcopy ($testname)"
754 # ia64 specific tests
755 if { ([istarget "ia64-*-elf*"]
756 || [istarget "ia64-*-linux*"]) } {
757 objcopy_test "ia64 link order" link-order.s
762 objcopy_test "ELF unknown section type" unknown.s
763 objcopy_test_readelf "ELF group" group.s
764 run_dump_test "copy-1"
767 run_dump_test "copy-2"
768 run_dump_test "copy-3"
771 run_dump_test "localize-hidden-1"
772 run_dump_test "strip-1"
773 run_dump_test "strip-2"
774 run_dump_test "strip-3"
776 run_dump_test "localize-hidden-2"