1 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-dejagnu@prep.ai.mit.edu
21 # Written by Ian Lance Taylor <ian@cygnus.com>
23 if ![is_remote host] {
24 if {[which $OBJCOPY] == 0} then {
25 perror "$OBJCOPY does not exist"
30 send_user "Version [binutil_version $OBJCOPY]"
32 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
34 unresolved "objcopy (simple copy)"
38 if ![is_remote host] {
39 set tempfile tmpdir/bintest.o;
40 set copyfile tmpdir/copy;
42 set tempfile [remote_download host tmpdir/bintest.o]
46 # Test that objcopy does not modify a file when copying it.
48 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
50 if ![string match "" $got] then {
51 fail "objcopy (simple copy)"
53 send_log "cmp $tempfile ${copyfile}.o\n"
54 verbose "cmp $tempfile ${copyfile}.o"
56 set src1 tmpdir/bintest.o
57 set src2 tmpdir/copy.o
58 remote_upload host $tempfile $src1;
59 remote_upload host ${copyfile}.o $src2;
62 set src2 ${copyfile}.o
64 set status [remote_exec build cmp "${src1} ${src2}"];
65 set exec_output [lindex $status 1];
66 set exec_output [prune_warnings $exec_output]
68 # On some systems the result of objcopy will not be identical.
69 # Usually this is just because gas isn't using bfd to write the files
70 # in the first place, and may order things a little differently.
71 # Those systems should use setup_xfail here.
74 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
75 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
76 setup_xfail "hppa*-*-*"
78 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
79 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
81 setup_xfail "or32-*-rtems*" "or32-*-coff"
82 setup_xfail "sh-*-coff*" "sh-*-rtems*"
83 setup_xfail "tic4x-*-*" "tic80-*-*" "w65-*" "z8*-*"
85 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
86 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*" "m68*-*-sysv4*"
88 if [string match "" $exec_output] then {
89 pass "objcopy (simple copy)"
91 send_log "$exec_output\n"
92 verbose "$exec_output" 1
94 # On OSF/1, this succeeds with gas and fails with /bin/as.
95 setup_xfail "alpha*-*-osf*"
97 # This fails for COFF i960-vxworks targets.
98 setup_xfail "i960-*-vxworks*"
100 fail "objcopy (simple copy)"
104 # Test generating S records.
106 # We make the srec filename 8.3 compatible. Note that the header string
107 # matched against depends on the name of the file. Ugh.
109 if [is_remote host] {
110 set srecfile copy.sre
111 set header_string S00B0000636F70792E737265C1
113 set srecfile ${copyfile}.srec
114 set header_string S0130000746D706469722F636F70792E7372656397
117 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
119 if ![string match "" $got] then {
120 fail "objcopy -O srec"
122 if [is_remote host] {
123 remote_upload host ${srecfile} tmpdir/copy.srec;
124 set srecfile tmpdir/copy.srec;
126 set file [open ${srecfile} r]
128 # The first S record is fixed by the file name we are using.
132 if ![regexp "$header_string.*" $line] {
133 send_log "bad header\n"
134 fail "objcopy -O srec"
136 while {[gets $file line] != -1 \
137 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
144 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
145 send_log "bad trailer\n"
146 fail "objcopy -O srec"
148 if {[gets $file line] != -1} then {
149 send_log "garbage at end\n"
152 fail "objcopy -O srec"
154 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
155 if ![regexp "file format srec" $got] then {
156 send_log "objdump failed\n"
157 fail "objcopy -O srec"
159 pass "objcopy -O srec"
168 # Test setting and adjusting the start address. We only test this
169 # while generating S records, because we may not be able to set the
170 # start address for other object file formats, and the S record case
171 # is the only useful one anyhow.
173 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
174 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
175 perror "objdump can not recognize bintest.o"
178 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --set-start 0x7654"]
179 if ![string match "" $got] then {
180 fail "objcopy --set-start"
182 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
183 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
184 fail "objcopy --set-start"
186 if {$srecstart != 0x7654} then {
187 send_log "$srecstart != 0x7654\n"
188 fail "objcopy --set-start"
190 pass "objcopy --set-start"
195 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-start 0x123"]
196 if ![string match "" $got] then {
197 fail "objcopy --adjust-start"
199 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
200 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
201 fail "objcopy --adjust-start"
203 if {$srecstart != $origstart + 0x123} then {
204 send_log "$srecstart != $origstart + 0x123\n"
205 fail "objcopy --adjust-start"
207 pass "objcopy --adjust-start"
213 # Test adjusting the overall VMA, and adjusting the VMA of a
214 # particular section. We again only test this when generating S
220 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
222 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
225 while {[regexp $headers_regexp $got all name size vma rest]} {
229 if {$low == "" || $vma < $low} {
237 if {$low == "" || $origstart == ""} then {
238 perror "objdump can not recognize bintest.o"
240 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-vma 0x123"]
241 if ![string match "" $got] then {
242 fail "objcopy --adjust-vma"
244 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
245 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
246 if ![regexp $want $got all start vma] then {
247 fail "objcopy --adjust-vma"
250 if {$vma != $low + 0x123} then {
251 send_log "$vma != $low + 0x123\n"
252 fail "objcopy --adjust-vma"
254 if {$start != $origstart + 0x123} then {
255 send_log "$start != $origstart + 0x123\n"
256 fail "objcopy --adjust-vma"
258 pass "objcopy --adjust-vma"
266 while {[regexp $headers_regexp $got all name size vma rest]} {
268 if {$vma == $low} then {
269 set arg "$arg --adjust-section-vma $name+4"
274 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $arg"]
275 if ![string match "" $got] then {
276 fail "objcopy --adjust-section-vma +"
278 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
279 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
280 if ![regexp $want $got all vma] then {
281 fail "objcopy --adjust-section-vma +"
284 if {$vma != $low + 4} then {
285 send_log "$vma != $low + 4\n"
286 fail "objcopy --adjust-section-vma +"
288 pass "objcopy --adjust-section-vma +"
293 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
294 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $argeq"]
295 if ![string match "" $got] then {
296 fail "objcopy --adjust-section-vma ="
298 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
299 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
300 if ![regexp $want $got all vma] then {
301 fail "objcopy --adjust-section-vma ="
304 if {$vma != $low + 4} then {
305 send_log "$vma != $low + 4\n"
306 fail "objcopy --adjust-section-vma ="
308 pass "objcopy --adjust-section-vma ="
314 # Test stripping an object.
316 proc strip_test { } {
328 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
333 if [is_remote host] {
334 set archive libstrip.a
335 set objfile [remote_download host tmpdir/testprog.o];
336 remote_file host delete $archive
338 set archive tmpdir/libstrip.a
339 set objfile tmpdir/testprog.o
342 remote_file build delete tmpdir/libstrip.a
344 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
345 if ![string match "" $exec_output] {
350 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
351 if ![string match "" $exec_output] {
356 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
361 if [is_remote host] {
362 set objfile [remote_download host tmpdir/testprog.o];
364 set objfile tmpdir/testprog.o
367 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
368 if ![string match "" $exec_output] {
373 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
374 if ![string match "*: no symbols*" $exec_output] {
384 # Test stripping an object file with saving a symbol
386 proc strip_test_with_saving_a_symbol { } {
395 set test "strip with saving a symbol"
397 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
402 if [is_remote host] {
403 set objfile [remote_download host tmpdir/testprog.o];
405 set objfile tmpdir/testprog.o
408 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
409 if ![string match "" $exec_output] {
414 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
415 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
416 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
424 strip_test_with_saving_a_symbol
426 # Build a final executable.
428 if { [istarget *-*-cygwin] } {
429 set test_prog "testprog.exe"
431 set test_prog "testprog"
434 proc copy_setup { } {
440 set res [build_wrapper testglue.o];
444 lappend flags "additional_flags=[lindex $res 1]";
445 set add_libs "testglue.o";
450 if { [istarget *-*-linux*] } {
451 foreach i $gcc_gas_flag {
452 set flags "additional_flags=$i $flags"
455 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
459 set result [remote_load target tmpdir/$test_prog];
460 set status [lindex $result 0];
462 if { $status != "pass" } {
463 perror "unresolved setup, status = $status"
470 # Test copying an executable.
472 proc copy_executable { prog flags test1 test2 } {
475 if [is_remote host] {
476 set testfile [remote_download host tmpdir/$test_prog];
477 set testcopy copyprog
479 set testfile tmpdir/$test_prog
480 set testcopy tmpdir/copyprog
482 remote_file host delete $testcopy;
484 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
486 if ![string match "" $exec_output] {
492 if [is_remote host] {
493 remote_upload host $testcopy tmpdir/copyprog
496 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
497 set exec_output [lindex $status 1];
499 if [string match "" $exec_output] then {
502 send_log "$exec_output\n"
503 verbose "$exec_output"
505 # This will fail for many reasons. For example, it will most
506 # likely fail if a non-GNU linker is used. Therefore, we do
507 # not insist that it pass. If you are using an assembler and
508 # linker based on the same BFD as objcopy, it is worth
509 # investigating to see why this failure occurs. If we are
510 # cross compiling, we assume that a GNU linker is being used,
511 # and expect it to succeed.
512 if {[isnative]} then {
516 # This also fails for mips*-*-elf targets. See elf32-mips.c
517 # mips_elf_sym_is_global.
518 setup_xfail "mips*-*-elf"
520 setup_xfail "*arm*-*-coff"
521 setup_xfail "xscale-*-coff"
522 setup_xfail "arm*-*-pe"
523 setup_xfail "thumb*-*-coff"
524 setup_xfail "thumb*-*-pe"
529 set output [remote_load target tmpdir/copyprog]
530 set status [lindex $output 0];
531 if { $status != "pass" } {
538 # Test stripping an executable
540 proc strip_executable { prog flags test } {
544 remote_download build tmpdir/copyprog tmpdir/striprog
545 if [is_remote host] {
546 set copyfile [remote_download host tmpdir/striprog];
548 set copyfile tmpdir/striprog
551 set exec_output [binutils_run $prog "$flags ${copyfile}"]
552 if ![string match "" $exec_output] {
557 if [is_remote host] {
558 remote_upload host ${copyfile} tmpdir/striprog;
561 set result [remote_load target tmpdir/striprog]
562 set status [lindex $result 0];
563 if { $status != "pass" } {
568 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
569 if ![string match "*: no symbols*" $exec_output] {
576 # Test stripping an executable with saving a symbol
578 proc strip_executable_with_saving_a_symbol { prog flags test } {
582 remote_download build tmpdir/copyprog tmpdir/striprog
583 if [is_remote host] {
584 set copyfile [remote_download host tmpdir/striprog];
586 set copyfile tmpdir/striprog
589 set exec_output [binutils_run $prog "$flags ${copyfile}"]
590 if ![string match "" $exec_output] {
595 if [is_remote host] {
596 remote_upload host ${copyfile} tmpdir/striprog;
599 set result [remote_load target tmpdir/striprog]
600 set status [lindex $result 0];
601 if { $status != "pass" } {
606 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
607 if { [istarget mmix-knuth-mmixware] } {
608 # Whenever there's a symbol in the mmo format, there's the symbol
609 # Main, so remove it manually from the expected output for sake of
612 # Using "" not {} to get the \n and \r translated.
613 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
616 if {![regexp {^[0-9a-fA-F]+ T main} $exec_output] \
617 && ![regexp {^[0-9a-fA-F]+ T _main} $exec_output]} {
624 set test1 "simple objcopy of executable"
625 set test2 "run objcopy of executable"
626 set test3 "run stripped executable"
627 set test4 "run stripped executable with saving a symbol"
629 switch [copy_setup] {
646 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
647 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
648 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"