update copyright dates
[binutils.git] / binutils / testsuite / binutils-all / objcopy.exp
blobdb6c8568811a42160e4f5f47c625d47e617fb433
1 # Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 # 2004
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.
9 #
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"
27 return
31 send_user "Version [binutil_version $OBJCOPY]"
33 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
34 perror "unresolved 1"
35 unresolved "objcopy (simple copy)"
36 return
39 if ![is_remote host] {
40 set tempfile tmpdir/bintest.o
41 set copyfile tmpdir/copy
42 } else {
43 set tempfile [remote_download host tmpdir/bintest.o]
44 set copyfile copy
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)"
53 } else {
54 send_log "cmp $tempfile ${copyfile}.o\n"
55 verbose "cmp $tempfile ${copyfile}.o"
56 if [is_remote host] {
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
61 } else {
62 set src1 ${tempfile}
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.
74 setup_xfail "a29k-*"
75 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
76 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
77 setup_xfail "hppa*-*-*"
78 setup_xfail "i960-*"
79 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
80 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
81 setup_xfail "m8*-*"
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)"
91 } else {
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
113 } else {
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"
122 } else {
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.
130 gets $file line
131 send_log "$line\n"
132 verbose $line
133 if ![regexp "$header_string.*" $line] {
134 send_log "bad header\n"
135 fail "objcopy -O srec"
136 } else {
137 while {[gets $file line] != -1 \
138 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
139 send_log "$line\n"
140 verbose $line
141 set line "**EOF**"
143 send_log "$line\n"
144 verbose $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"
148 } else {
149 if {[gets $file line] != -1} then {
150 send_log "garbage at end\n"
151 send_log "$line\n"
152 verbose $line
153 fail "objcopy -O srec"
154 } else {
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"
159 } else {
160 pass "objcopy -O srec"
166 close $file
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"
177 set origstart ""
178 } else {
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"
182 } else {
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"
186 } else {
187 if {$srecstart != 0x7654} then {
188 send_log "$srecstart != 0x7654\n"
189 fail "objcopy --set-start"
190 } else {
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"
199 } else {
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"
203 } else {
204 if {$srecstart != $origstart + 0x123} then {
205 send_log "$srecstart != $origstart + 0x123\n"
206 fail "objcopy --adjust-start"
207 } else {
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
216 # records.
218 set low ""
219 set lowname ""
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\]+(.*)"
225 set got $headers
226 while {[regexp $headers_regexp $got all name size vma rest]} {
227 set vma 0x$vma
228 set size 0x$size
229 if {$size != 0} {
230 if {$low == "" || $vma < $low} {
231 set low $vma
232 set lowname $name
235 set got $rest
238 if {$low == "" || $origstart == ""} then {
239 perror "objdump can not recognize bintest.o"
240 } else {
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"
244 } else {
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"
249 } else {
250 set vma 0x$vma
251 if {$vma != $low + 0x123} then {
252 send_log "$vma != $low + 0x123\n"
253 fail "objcopy --adjust-vma"
254 } else {
255 if {$start != $origstart + 0x123} then {
256 send_log "$start != $origstart + 0x123\n"
257 fail "objcopy --adjust-vma"
258 } else {
259 pass "objcopy --adjust-vma"
265 set arg ""
266 set got $headers
267 while {[regexp $headers_regexp $got all name size vma rest]} {
268 set vma 0x$vma
269 if {$vma == $low} then {
270 set arg "$arg --adjust-section-vma $name+4"
272 set got $rest
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 +"
278 } else {
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 +"
283 } else {
284 set vma 0x$vma
285 if {$vma != $low + 4} then {
286 send_log "$vma != $low + 4\n"
287 fail "objcopy --adjust-section-vma +"
288 } else {
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 ="
298 } else {
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 ="
303 } else {
304 set vma 0x$vma
305 if {$vma != $low + 4} then {
306 send_log "$vma != $low + 4\n"
307 fail "objcopy --adjust-section-vma ="
308 } else {
309 pass "objcopy --adjust-section-vma ="
315 # Test stripping an object.
317 proc strip_test { } {
318 global AR
319 global CC
320 global STRIP
321 global STRIPFLAGS
322 global NM
323 global NMFLAGS
324 global srcdir
325 global subdir
327 set test "strip"
329 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
330 untested $test
331 return
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
338 } else {
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] {
347 fail $test
348 return
351 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
352 if ![string match "" $exec_output] {
353 fail $test
354 return
357 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
358 untested $test
359 return
362 if [is_remote host] {
363 set objfile [remote_download host tmpdir/testprog.o]
364 } else {
365 set objfile tmpdir/testprog.o
368 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
369 if ![string match "" $exec_output] {
370 fail $test
371 return
374 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
375 if ![string match "*: no symbols*" $exec_output] {
376 fail $test
377 return
380 pass $test
383 strip_test
385 # Test stripping an object file with saving a symbol
387 proc strip_test_with_saving_a_symbol { } {
388 global CC
389 global STRIP
390 global STRIPFLAGS
391 global NM
392 global NMFLAGS
393 global srcdir
394 global subdir
396 set test "strip with saving a symbol"
398 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
399 untested $test
400 return
403 if [is_remote host] {
404 set objfile [remote_download host tmpdir/testprog.o]
405 } else {
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] {
411 fail $test
412 return
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]} {
418 fail $test
419 return
422 pass $test
425 strip_test_with_saving_a_symbol
427 # Build a final executable.
429 if { [istarget *-*-cygwin] || [istarget *-*-mingw32] } {
430 set test_prog "testprog.exe"
431 } else {
432 set test_prog "testprog"
435 proc copy_setup { } {
436 global srcdir
437 global subdir
438 global gcc_gas_flag
439 global test_prog
441 set res [build_wrapper testglue.o]
442 set flags { debug }
444 if { $res != "" } {
445 lappend flags "additional_flags=[lindex $res 1]"
446 set add_libs "testglue.o"
447 } else {
448 set add_libs ""
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] != "" } {
457 return 2
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"
465 return 3
468 return 0
471 # Test copying an executable.
473 proc copy_executable { prog flags test1 test2 } {
474 global test_prog
476 if [is_remote host] {
477 set testfile [remote_download host tmpdir/$test_prog]
478 set testcopy copyprog
479 } else {
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] {
488 fail $test1
489 fail $test2
490 return
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 {
501 pass $test1
502 } else {
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 {
514 setup_xfail "*-*-*"
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"
527 fail $test1
530 set output [remote_load target tmpdir/copyprog]
531 set status [lindex $output 0]
532 if { $status != "pass" } {
533 fail $test2
534 } else {
535 pass $test2
539 # Test stripping an executable
541 proc strip_executable { prog flags test } {
542 global NM
543 global NMFLAGS
545 remote_download build tmpdir/copyprog tmpdir/striprog
546 if [is_remote host] {
547 set copyfile [remote_download host tmpdir/striprog]
548 } else {
549 set copyfile tmpdir/striprog
552 set exec_output [binutils_run $prog "$flags ${copyfile}"]
553 if ![string match "" $exec_output] {
554 fail $test
555 return
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" } {
565 fail $test
566 return
569 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
570 if ![string match "*: no symbols*" $exec_output] {
571 fail $test
572 return
574 pass $test
577 # Test stripping an executable with saving a symbol
579 proc strip_executable_with_saving_a_symbol { prog flags test } {
580 global NM
581 global NMFLAGS
583 remote_download build tmpdir/copyprog tmpdir/striprog
584 if [is_remote host] {
585 set copyfile [remote_download host tmpdir/striprog]
586 } else {
587 set copyfile tmpdir/striprog
590 set exec_output [binutils_run $prog "$flags ${copyfile}"]
591 if ![string match "" $exec_output] {
592 fail $test
593 return
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" } {
603 fail $test
604 return
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
611 # this test.
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]} {
619 fail $test
620 return
622 pass $test
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] {
631 "1" {
632 # do nothing
634 "2" {
635 untested $test1
636 untested $test2
637 untested $test3
638 untested $test4
640 "3" {
641 unresolved $test1
642 unresolved $test2
643 unresolved $test3
644 unresolved $test4
646 "0" {
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"