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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
35 unresolved "objcopy (simple copy)"
39 if ![is_remote host] {
40 set tempfile tmpdir/bintest.o
41 set copyfile tmpdir/copy
43 set tempfile [remote_download host tmpdir/bintest.o]
47 # Test that objcopy does not modify a file when copying it.
49 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
51 if ![string match "" $got] then {
52 fail "objcopy (simple copy)"
54 send_log "cmp $tempfile ${copyfile}.o\n"
55 verbose "cmp $tempfile ${copyfile}.o"
57 set src1 tmpdir/bintest.o
58 set src2 tmpdir/copy.o
59 remote_upload host $tempfile $src1
60 remote_upload host ${copyfile}.o $src2
63 set src2 ${copyfile}.o
65 set status [remote_exec build cmp "${src1} ${src2}"]
66 set exec_output [lindex $status 1]
67 set exec_output [prune_warnings $exec_output]
69 # On some systems the result of objcopy will not be identical.
70 # Usually this is just because gas isn't using bfd to write the files
71 # in the first place, and may order things a little differently.
72 # Those systems should use setup_xfail here.
75 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
76 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
77 setup_xfail "hppa*-*-*"
79 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
80 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
82 setup_xfail "or32-*-rtems*" "or32-*-coff"
83 setup_xfail "sh-*-coff*" "sh-*-rtems*"
84 setup_xfail "tic4x-*-*" "tic80-*-*" "w65-*" "z8*-*"
86 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
87 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*" "m68*-*-sysv4*"
89 if [string match "" $exec_output] then {
90 pass "objcopy (simple copy)"
92 send_log "$exec_output\n"
93 verbose "$exec_output" 1
95 # On OSF/1, this succeeds with gas and fails with /bin/as.
96 setup_xfail "alpha*-*-osf*"
98 # This fails for COFF i960-vxworks targets.
99 setup_xfail "i960-*-vxworks*"
101 fail "objcopy (simple copy)"
105 # Test generating S records.
107 # We make the srec filename 8.3 compatible. Note that the header string
108 # matched against depends on the name of the file. Ugh.
110 if [is_remote host] {
111 set srecfile copy.sre
112 set header_string S00B0000636F70792E737265C1
114 set srecfile ${copyfile}.srec
115 set header_string S0130000746D706469722F636F70792E7372656397
118 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
120 if ![string match "" $got] then {
121 fail "objcopy -O srec"
123 if [is_remote host] {
124 remote_upload host ${srecfile} tmpdir/copy.srec
125 set srecfile tmpdir/copy.srec
127 set file [open ${srecfile} r]
129 # The first S record is fixed by the file name we are using.
133 if ![regexp "$header_string.*" $line] {
134 send_log "bad header\n"
135 fail "objcopy -O srec"
137 while {[gets $file line] != -1 \
138 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
145 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
146 send_log "bad trailer\n"
147 fail "objcopy -O srec"
149 if {[gets $file line] != -1} then {
150 send_log "garbage at end\n"
153 fail "objcopy -O srec"
155 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
156 if ![regexp "file format srec" $got] then {
157 send_log "objdump failed\n"
158 fail "objcopy -O srec"
160 pass "objcopy -O srec"
169 # Test setting and adjusting the start address. We only test this
170 # while generating S records, because we may not be able to set the
171 # start address for other object file formats, and the S record case
172 # is the only useful one anyhow.
174 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
175 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
176 perror "objdump can not recognize bintest.o"
179 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
180 if ![string match "" $got] then {
181 fail "objcopy --set-start"
183 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
184 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
185 fail "objcopy --set-start"
187 if {$srecstart != 0x7654} then {
188 send_log "$srecstart != 0x7654\n"
189 fail "objcopy --set-start"
191 pass "objcopy --set-start"
196 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
197 if ![string match "" $got] then {
198 fail "objcopy --adjust-start"
200 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
201 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
202 fail "objcopy --adjust-start"
204 if {$srecstart != $origstart + 0x123} then {
205 send_log "$srecstart != $origstart + 0x123\n"
206 fail "objcopy --adjust-start"
208 pass "objcopy --adjust-start"
214 # Test adjusting the overall VMA, and adjusting the VMA of a
215 # particular section. We again only test this when generating S
221 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
223 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
226 while {[regexp $headers_regexp $got all name size vma rest]} {
230 if {$low == "" || $vma < $low} {
238 if {$low == "" || $origstart == ""} then {
239 perror "objdump can not recognize bintest.o"
241 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
242 if ![string match "" $got] then {
243 fail "objcopy --adjust-vma"
245 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
246 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
247 if ![regexp $want $got all start vma] then {
248 fail "objcopy --adjust-vma"
251 if {$vma != $low + 0x123} then {
252 send_log "$vma != $low + 0x123\n"
253 fail "objcopy --adjust-vma"
255 if {$start != $origstart + 0x123} then {
256 send_log "$start != $origstart + 0x123\n"
257 fail "objcopy --adjust-vma"
259 pass "objcopy --adjust-vma"
267 while {[regexp $headers_regexp $got all name size vma rest]} {
269 if {$vma == $low} then {
270 set arg "$arg --adjust-section-vma $name+4"
275 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
276 if ![string match "" $got] then {
277 fail "objcopy --adjust-section-vma +"
279 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
280 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
281 if ![regexp $want $got all vma] then {
282 fail "objcopy --adjust-section-vma +"
285 if {$vma != $low + 4} then {
286 send_log "$vma != $low + 4\n"
287 fail "objcopy --adjust-section-vma +"
289 pass "objcopy --adjust-section-vma +"
294 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
295 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
296 if ![string match "" $got] then {
297 fail "objcopy --adjust-section-vma ="
299 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
300 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
301 if ![regexp $want $got all vma] then {
302 fail "objcopy --adjust-section-vma ="
305 if {$vma != $low + 4} then {
306 send_log "$vma != $low + 4\n"
307 fail "objcopy --adjust-section-vma ="
309 pass "objcopy --adjust-section-vma ="
315 # Test stripping an object.
317 proc strip_test { } {
329 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
334 if [is_remote host] {
335 set archive libstrip.a
336 set objfile [remote_download host tmpdir/testprog.o]
337 remote_file host delete $archive
339 set archive tmpdir/libstrip.a
340 set objfile tmpdir/testprog.o
343 remote_file build delete tmpdir/libstrip.a
345 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
346 if ![string match "" $exec_output] {
351 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
352 if ![string match "" $exec_output] {
357 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
362 if [is_remote host] {
363 set objfile [remote_download host tmpdir/testprog.o]
365 set objfile tmpdir/testprog.o
368 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
369 if ![string match "" $exec_output] {
374 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
375 if ![string match "*: no symbols*" $exec_output] {
385 # Test stripping an object file with saving a symbol
387 proc strip_test_with_saving_a_symbol { } {
396 set test "strip with saving a symbol"
398 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
403 if [is_remote host] {
404 set objfile [remote_download host tmpdir/testprog.o]
406 set objfile tmpdir/testprog.o
409 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
410 if ![string match "" $exec_output] {
415 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
416 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
417 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
425 strip_test_with_saving_a_symbol
427 # Build a final executable.
429 if { [istarget *-*-cygwin] || [istarget *-*-mingw32] } {
430 set test_prog "testprog.exe"
432 set test_prog "testprog"
435 proc copy_setup { } {
441 set res [build_wrapper testglue.o]
445 lappend flags "additional_flags=[lindex $res 1]"
446 set add_libs "testglue.o"
451 if { [istarget *-*-linux*] } {
452 foreach i $gcc_gas_flag {
453 set flags "additional_flags=$i $flags"
456 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
460 set result [remote_load target tmpdir/$test_prog]
461 set status [lindex $result 0]
463 if { $status != "pass" } {
464 perror "unresolved setup, status = $status"
471 # Test copying an executable.
473 proc copy_executable { prog flags test1 test2 } {
476 if [is_remote host] {
477 set testfile [remote_download host tmpdir/$test_prog]
478 set testcopy copyprog
480 set testfile tmpdir/$test_prog
481 set testcopy tmpdir/copyprog
483 remote_file host delete $testcopy
485 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
487 if ![string match "" $exec_output] {
493 if [is_remote host] {
494 remote_upload host $testcopy tmpdir/copyprog
497 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
498 set exec_output [lindex $status 1]
500 if [string match "" $exec_output] then {
503 send_log "$exec_output\n"
504 verbose "$exec_output"
506 # This will fail for many reasons. For example, it will most
507 # likely fail if a non-GNU linker is used. Therefore, we do
508 # not insist that it pass. If you are using an assembler and
509 # linker based on the same BFD as objcopy, it is worth
510 # investigating to see why this failure occurs. If we are
511 # cross compiling, we assume that a GNU linker is being used,
512 # and expect it to succeed.
513 if {[isnative]} then {
517 # This also fails for mips*-*-elf targets. See elf32-mips.c
518 # mips_elf_sym_is_global.
519 setup_xfail "mips*-*-elf"
521 setup_xfail "*arm*-*-coff"
522 setup_xfail "xscale-*-coff"
523 setup_xfail "arm*-*-pe"
524 setup_xfail "thumb*-*-coff"
525 setup_xfail "thumb*-*-pe"
530 set output [remote_load target tmpdir/copyprog]
531 set status [lindex $output 0]
532 if { $status != "pass" } {
539 # Test stripping an executable
541 proc strip_executable { prog flags test } {
545 remote_download build tmpdir/copyprog tmpdir/striprog
546 if [is_remote host] {
547 set copyfile [remote_download host tmpdir/striprog]
549 set copyfile tmpdir/striprog
552 set exec_output [binutils_run $prog "$flags ${copyfile}"]
553 if ![string match "" $exec_output] {
558 if [is_remote host] {
559 remote_upload host ${copyfile} tmpdir/striprog
562 set result [remote_load target tmpdir/striprog]
563 set status [lindex $result 0]
564 if { $status != "pass" } {
569 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
570 if ![string match "*: no symbols*" $exec_output] {
577 # Test stripping an executable with saving a symbol
579 proc strip_executable_with_saving_a_symbol { prog flags test } {
583 remote_download build tmpdir/copyprog tmpdir/striprog
584 if [is_remote host] {
585 set copyfile [remote_download host tmpdir/striprog]
587 set copyfile tmpdir/striprog
590 set exec_output [binutils_run $prog "$flags ${copyfile}"]
591 if ![string match "" $exec_output] {
596 if [is_remote host] {
597 remote_upload host ${copyfile} tmpdir/striprog
600 set result [remote_load target tmpdir/striprog]
601 set status [lindex $result 0]
602 if { $status != "pass" } {
607 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
608 if { [istarget mmix-knuth-mmixware] } {
609 # Whenever there's a symbol in the mmo format, there's the symbol
610 # Main, so remove it manually from the expected output for sake of
613 # Using "" not {} to get the \n and \r translated.
614 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
617 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
618 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
625 set test1 "simple objcopy of executable"
626 set test2 "run objcopy of executable"
627 set test3 "run stripped executable"
628 set test4 "run stripped executable with saving a symbol"
630 switch [copy_setup] {
647 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
648 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
649 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"