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 generating S records.
118 # We make the srec filename 8.3 compatible. Note that the header string
119 # matched against depends on the name of the file. Ugh.
121 if [is_remote host] {
122 set srecfile copy.sre
123 set header_string S00B0000636F70792E737265C1
125 set srecfile ${copyfile}.srec
126 set header_string S0130000746D706469722F636F70792E7372656397
129 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
131 if ![string match "" $got] then {
132 fail "objcopy -O srec"
134 if [is_remote host] {
135 remote_upload host ${srecfile} tmpdir/copy.srec
136 set srecfile tmpdir/copy.srec
138 set file [open ${srecfile} r]
140 # The first S record is fixed by the file name we are using.
144 if ![regexp "$header_string.*" $line] {
145 send_log "bad header\n"
146 fail "objcopy -O srec"
148 while {[gets $file line] != -1 \
149 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
156 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
157 send_log "bad trailer\n"
158 fail "objcopy -O srec"
160 if {[gets $file line] != -1} then {
161 send_log "garbage at end\n"
164 fail "objcopy -O srec"
166 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
167 if ![regexp "file format srec" $got] then {
168 send_log "objdump failed\n"
169 fail "objcopy -O srec"
171 pass "objcopy -O srec"
180 # Test setting and adjusting the start address. We only test this
181 # while generating S records, because we may not be able to set the
182 # start address for other object file formats, and the S record case
183 # is the only useful one anyhow.
185 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
186 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
187 perror "objdump can not recognize bintest.o"
190 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
191 if ![string match "" $got] then {
192 fail "objcopy --set-start"
194 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
195 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
196 fail "objcopy --set-start"
198 if {$srecstart != 0x7654} then {
199 send_log "$srecstart != 0x7654\n"
200 fail "objcopy --set-start"
202 pass "objcopy --set-start"
207 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
208 if ![string match "" $got] then {
209 fail "objcopy --adjust-start"
211 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
212 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
213 fail "objcopy --adjust-start"
215 if {$srecstart != $origstart + 0x123} then {
216 send_log "$srecstart != $origstart + 0x123\n"
217 fail "objcopy --adjust-start"
219 pass "objcopy --adjust-start"
225 # Test adjusting the overall VMA, and adjusting the VMA of a
226 # particular section. We again only test this when generating S
232 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
234 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
237 while {[regexp $headers_regexp $got all name size vma rest]} {
241 if {$low == "" || $vma < $low} {
249 if {$low == "" || $origstart == ""} then {
250 perror "objdump can not recognize bintest.o"
252 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
253 if ![string match "" $got] then {
254 fail "objcopy --adjust-vma"
256 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
257 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
258 if ![regexp $want $got all start vma] then {
259 fail "objcopy --adjust-vma"
262 if {$vma != $low + 0x123} then {
263 send_log "$vma != $low + 0x123\n"
264 fail "objcopy --adjust-vma"
266 if {$start != $origstart + 0x123} then {
267 send_log "$start != $origstart + 0x123\n"
268 fail "objcopy --adjust-vma"
270 pass "objcopy --adjust-vma"
278 while {[regexp $headers_regexp $got all name size vma rest]} {
280 if {$vma == $low} then {
281 set arg "$arg --adjust-section-vma $name+4"
286 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
287 if ![string match "" $got] then {
288 fail "objcopy --adjust-section-vma +"
290 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
291 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
292 if ![regexp $want $got all vma] then {
293 fail "objcopy --adjust-section-vma +"
296 if {$vma != $low + 4} then {
297 send_log "$vma != $low + 4\n"
298 fail "objcopy --adjust-section-vma +"
300 pass "objcopy --adjust-section-vma +"
305 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
306 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
307 if ![string match "" $got] then {
308 fail "objcopy --adjust-section-vma ="
310 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
311 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
312 if ![regexp $want $got all vma] then {
313 fail "objcopy --adjust-section-vma ="
316 if {$vma != $low + 4} then {
317 send_log "$vma != $low + 4\n"
318 fail "objcopy --adjust-section-vma ="
320 pass "objcopy --adjust-section-vma ="
326 # Test stripping an object.
328 proc strip_test { } {
340 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
345 if [is_remote host] {
346 set archive libstrip.a
347 set objfile [remote_download host tmpdir/testprog.o]
348 remote_file host delete $archive
350 set archive tmpdir/libstrip.a
351 set objfile tmpdir/testprog.o
354 remote_file build delete tmpdir/libstrip.a
356 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
357 if ![string match "" $exec_output] {
362 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
363 if ![string match "" $exec_output] {
368 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
373 if [is_remote host] {
374 set objfile [remote_download host tmpdir/testprog.o]
376 set objfile tmpdir/testprog.o
379 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
380 if ![string match "" $exec_output] {
385 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
386 if ![string match "*: no symbols*" $exec_output] {
396 # Test stripping an object file with saving a symbol
398 proc strip_test_with_saving_a_symbol { } {
407 set test "strip with saving a symbol"
409 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
414 if [is_remote host] {
415 set objfile [remote_download host tmpdir/testprog.o]
417 set objfile tmpdir/testprog.o
420 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
421 if ![string match "" $exec_output] {
426 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
427 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
428 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
436 strip_test_with_saving_a_symbol
438 # Build a final executable.
440 if { [istarget *-*-cygwin] || [istarget *-*-mingw32] } {
441 set test_prog "testprog.exe"
443 set test_prog "testprog"
446 proc copy_setup { } {
452 set res [build_wrapper testglue.o]
456 lappend flags "additional_flags=[lindex $res 1]"
457 set add_libs "testglue.o"
462 if { [istarget *-*-linux*] } {
463 foreach i $gcc_gas_flag {
464 set flags "additional_flags=$i $flags"
467 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
471 set result [remote_load target tmpdir/$test_prog]
472 set status [lindex $result 0]
474 if { $status != "pass" } {
475 perror "unresolved setup, status = $status"
482 # Test copying an executable.
484 proc copy_executable { prog flags test1 test2 } {
487 if [is_remote host] {
488 set testfile [remote_download host tmpdir/$test_prog]
489 set testcopy copyprog
491 set testfile tmpdir/$test_prog
492 set testcopy tmpdir/copyprog
494 remote_file host delete $testcopy
496 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
498 if ![string match "" $exec_output] {
504 if [is_remote host] {
505 remote_upload host $testcopy tmpdir/copyprog
508 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
509 set exec_output [lindex $status 1]
511 if [string match "" $exec_output] then {
514 send_log "$exec_output\n"
515 verbose "$exec_output"
517 # This will fail for many reasons. For example, it will most
518 # likely fail if a non-GNU linker is used. Therefore, we do
519 # not insist that it pass. If you are using an assembler and
520 # linker based on the same BFD as objcopy, it is worth
521 # investigating to see why this failure occurs. If we are
522 # cross compiling, we assume that a GNU linker is being used,
523 # and expect it to succeed.
524 if {[isnative]} then {
528 # This also fails for mips*-*-elf targets. See elf32-mips.c
529 # mips_elf_sym_is_global.
530 setup_xfail "mips*-*-elf"
532 setup_xfail "*arm*-*-coff"
533 setup_xfail "xscale-*-coff"
534 setup_xfail "arm*-*-pe"
535 setup_xfail "thumb*-*-coff"
536 setup_xfail "thumb*-*-pe"
541 set output [remote_load target tmpdir/copyprog]
542 set status [lindex $output 0]
543 if { $status != "pass" } {
550 # Test stripping an executable
552 proc strip_executable { prog flags test } {
556 remote_download build tmpdir/copyprog tmpdir/striprog
557 if [is_remote host] {
558 set copyfile [remote_download host tmpdir/striprog]
560 set copyfile tmpdir/striprog
563 set exec_output [binutils_run $prog "$flags ${copyfile}"]
564 if ![string match "" $exec_output] {
569 if [is_remote host] {
570 remote_upload host ${copyfile} tmpdir/striprog
573 set result [remote_load target tmpdir/striprog]
574 set status [lindex $result 0]
575 if { $status != "pass" } {
580 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
581 if ![string match "*: no symbols*" $exec_output] {
588 # Test stripping an executable with saving a symbol
590 proc strip_executable_with_saving_a_symbol { prog flags test } {
594 remote_download build tmpdir/copyprog tmpdir/striprog
595 if [is_remote host] {
596 set copyfile [remote_download host tmpdir/striprog]
598 set copyfile tmpdir/striprog
601 set exec_output [binutils_run $prog "$flags ${copyfile}"]
602 if ![string match "" $exec_output] {
607 if [is_remote host] {
608 remote_upload host ${copyfile} tmpdir/striprog
611 set result [remote_load target tmpdir/striprog]
612 set status [lindex $result 0]
613 if { $status != "pass" } {
618 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
619 if { [istarget mmix-knuth-mmixware] } {
620 # Whenever there's a symbol in the mmo format, there's the symbol
621 # Main, so remove it manually from the expected output for sake of
624 # Using "" not {} to get the \n and \r translated.
625 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
628 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
629 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
636 set test1 "simple objcopy of executable"
637 set test2 "run objcopy of executable"
638 set test3 "run stripped executable"
639 set test4 "run stripped executable with saving a symbol"
641 switch [copy_setup] {
658 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
659 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
660 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
664 proc objcopy_test_readelf {testname srcfile} {
671 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
672 unresolved "objcopy ($testname)"
676 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
677 catch "exec $OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o" exec_output
678 if ![string match "" $exec_output] then {
679 fail "objcopy ($testname)"
683 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
684 catch "exec $READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out" exec_output
685 set exec_output [prune_warnings $exec_output]
686 if ![string match "" $exec_output] then {
687 unresolved "objcopy ($testname)"
691 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
692 catch "exec $READELF -a tmpdir/copy.o > tmpdir/copy.o.out" exec_output
693 set exec_output [prune_warnings $exec_output]
694 if ![string match "" $exec_output] then {
695 unresolved "objcopy ($testname)"
699 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
700 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
701 set exec_output [prune_warnings $exec_output]
703 if [string match "" $exec_output] then {
704 pass "objcopy ($testname)"
706 fail "objcopy ($testname)"
710 # ia64 specific tests
711 if { ([istarget "ia64-*-elf*"]
712 || [istarget "ia64-*-linux*"]) } {
713 objcopy_test "ia64 link order" link-order.s
718 objcopy_test "ELF unknown section type" unknown.s
719 objcopy_test_readelf "ELF group" group.s