Initial revision
[binutils.git] / binutils / testsuite / binutils-all / objcopy.exp
blob78972f1c0c1db7b41ab996c920eb0bebbde0f3e7
1 # Copyright (C) 1994, 95, 96, 97, 1998 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
20 # Written by Ian Lance Taylor <ian@cygnus.com>
22 if ![is_remote host] {
23 if {[which $OBJCOPY] == 0} then {
24 perror "$OBJCOPY does not exist"
25 return
29 send_user "Version [binutil_version $OBJCOPY]"
31 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
32 perror "unresolved 1"
33 unresolved "objcopy (simple copy)"
34 return
37 if ![is_remote host] {
38 set tempfile tmpdir/bintest.o;
39 set copyfile tmpdir/copy;
40 } else {
41 set tempfile [remote_download host tmpdir/bintest.o]
42 set copyfile copy
45 # Test that objcopy does not modify a file when copying it.
47 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
49 if ![string match "" $got] then {
50 fail "objcopy (simple copy)"
51 } else {
52 send_log "cmp $tempfile ${copyfile}.o\n"
53 verbose "cmp $tempfile ${copyfile}.o"
54 if [is_remote host] {
55 set src1 tmpdir/bintest.o
56 set src2 tmpdir/copy.o
57 remote_upload host $tempfile $src1;
58 remote_upload host ${copyfile}.o $src2;
59 } else {
60 set src1 ${tempfile}
61 set src2 ${copyfile}.o
63 set status [remote_exec build cmp "${src1} ${src2}"];
64 set exec_output [lindex $status 1];
65 set exec_output [prune_warnings $exec_output]
67 # On some systems the result of objcopy will not be identical.
68 # Usually this is just because gas isn't using bfd to write the files
69 # in the first place, and may order things a little differently.
70 # Those systems should use setup_xfail here.
72 setup_xfail "sh-*-coff" "sh-*-hms"
73 setup_xfail "m68*-*-hpux*" "m68*-*-sunos*" "m68*-*-coff" "m68*-*-vxworks*"
74 setup_xfail "m68*-ericsson-ose" "m68k*-motorola-sysv*"
75 setup_xfail "i*86-*-linuxaout*" "i*86-*-aout*"
76 setup_xfail "i*86-*-sysv3" "i*86-*-isc*" "i*86-*-sco*" "i*86-*-coff"
77 setup_xfail "i*86-*-aix*" "i*86-*-go32*"
78 setup_xfail "a29k-*-udi" "a29k-*-coff" "a29k-*-vxworks*"
79 setup_xfail "i960-*-coff"
80 setup_xfail "h8300-*-hms" "h8300-*-coff"
81 setup_xfail "h8500-*-hms" "h8500-*-coff"
82 setup_xfail "hppa*-*-*"
83 clear_xfail "hppa*-*-*elf*"
84 setup_xfail "m88*-*-coff" "m88*-motorola-sysv*"
85 setup_xfail "z8*-*-coff"
87 if [string match "" $exec_output] then {
88 pass "objcopy (simple copy)"
89 } else {
90 send_log "$exec_output\n"
91 verbose "$exec_output" 1
93 # On OSF/1, this succeeds with gas and fails with /bin/as.
94 setup_xfail "alpha*-*-osf*"
96 # This fails for COFF i960-vxworks targets.
97 setup_xfail "i960-*-vxworks*"
99 fail "objcopy (simple copy)"
103 # Test generating S records.
105 # We make the srec filename 8.3 compatible. Note that the header string
106 # matched against depends on the name of the file. Ugh.
108 if [is_remote host] {
109 set srecfile copy.sre
110 set header_string S00B0000636F70792E737265C1
111 } else {
112 set srecfile ${copyfile}.srec
113 set header_string S0130000746D706469722F636F70792E7372656397
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
118 if ![string match "" $got] then {
119 fail "objcopy -O srec"
120 } else {
121 if [is_remote host] {
122 remote_upload host ${srecfile} tmpdir/copy.srec;
123 set srecfile tmpdir/copy.srec;
125 set file [open ${srecfile} r]
127 # The first S record is fixed by the file name we are using.
128 gets $file line
129 send_log "$line\n"
130 verbose $line
131 if ![regexp "$header_string.*" $line] {
132 send_log "bad header\n"
133 fail "objcopy -O srec"
134 } else {
135 while {[gets $file line] != -1 \
136 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
137 send_log "$line\n"
138 verbose $line
139 set line "**EOF**"
141 send_log "$line\n"
142 verbose $line
143 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
144 send_log "bad trailer\n"
145 fail "objcopy -O srec"
146 } else {
147 if {[gets $file line] != -1} then {
148 send_log "garbage at end\n"
149 send_log "$line\n"
150 verbose $line
151 fail "objcopy -O srec"
152 } else {
153 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
154 if ![regexp "file format srec" $got] then {
155 send_log "objdump failed\n"
156 fail "objcopy -O srec"
157 } else {
158 pass "objcopy -O srec"
164 close $file
167 # Test setting and adjusting the start address. We only test this
168 # while generating S records, because we may not be able to set the
169 # start address for other object file formats, and the S record case
170 # is the only useful one anyhow.
172 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
173 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
174 perror "objdump can not recognize bintest.o"
175 set origstart ""
176 } else {
177 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --set-start 0x7654"]
178 if ![string match "" $got] then {
179 fail "objcopy --set-start"
180 } else {
181 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
182 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
183 fail "objcopy --set-start"
184 } else {
185 if {$srecstart != 0x7654} then {
186 send_log "$srecstart != 0x7654\n"
187 fail "objcopy --set-start"
188 } else {
189 pass "objcopy --set-start"
194 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-start 0x123"]
195 if ![string match "" $got] then {
196 fail "objcopy --adjust-start"
197 } else {
198 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
199 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
200 fail "objcopy --adjust-start"
201 } else {
202 if {$srecstart != $origstart + 0x123} then {
203 send_log "$srecstart != $origstart + 0x123\n"
204 fail "objcopy --adjust-start"
205 } else {
206 pass "objcopy --adjust-start"
212 # Test adjusting the overall VMA, and adjusting the VMA of a
213 # particular section. We again only test this when generating S
214 # records.
216 set low ""
217 set lowname ""
219 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
221 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
223 set got $headers
224 while {[regexp $headers_regexp $got all name size vma rest]} {
225 set vma 0x$vma
226 set size 0x$size
227 if {$size != 0} {
228 if {$low == "" || $vma < $low} {
229 set low $vma
230 set lowname $name
233 set got $rest
236 if {$low == "" || $origstart == ""} then {
237 perror "objdump can not recognize bintest.o"
238 } else {
239 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-vma 0x123"]
240 if ![string match "" $got] then {
241 fail "objcopy --adjust-vma"
242 } else {
243 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
244 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
245 if ![regexp $want $got all start vma] then {
246 fail "objcopy --adjust-vma"
247 } else {
248 set vma 0x$vma
249 if {$vma != $low + 0x123} then {
250 send_log "$vma != $low + 0x123\n"
251 fail "objcopy --adjust-vma"
252 } else {
253 if {$start != $origstart + 0x123} then {
254 send_log "$start != $origstart + 0x123\n"
255 fail "objcopy --adjust-vma"
256 } else {
257 pass "objcopy --adjust-vma"
263 set arg ""
264 set got $headers
265 while {[regexp $headers_regexp $got all name size vma rest]} {
266 set vma 0x$vma
267 if {$vma == $low} then {
268 set arg "$arg --adjust-section-vma $name+4"
270 set got $rest
273 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $arg"]
274 if ![string match "" $got] then {
275 fail "objcopy --adjust-section-vma +"
276 } else {
277 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
278 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
279 if ![regexp $want $got all vma] then {
280 fail "objcopy --adjust-section-vma +"
281 } else {
282 set vma 0x$vma
283 if {$vma != $low + 4} then {
284 send_log "$vma != $low + 4\n"
285 fail "objcopy --adjust-section-vma +"
286 } else {
287 pass "objcopy --adjust-section-vma +"
292 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
293 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $argeq"]
294 if ![string match "" $got] then {
295 fail "objcopy --adjust-section-vma ="
296 } else {
297 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
298 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
299 if ![regexp $want $got all vma] then {
300 fail "objcopy --adjust-section-vma ="
301 } else {
302 set vma 0x$vma
303 if {$vma != $low + 4} then {
304 send_log "$vma != $low + 4\n"
305 fail "objcopy --adjust-section-vma ="
306 } else {
307 pass "objcopy --adjust-section-vma ="
313 # Test stripping an object.
315 proc strip_test { } {
316 global CC
317 global STRIP
318 global STRIPFLAGS
319 global NM
320 global NMFLAGS
321 global srcdir
322 global subdir
324 set test "strip"
326 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
327 untested $test
328 return
331 if [is_remote host] {
332 set objfile [remote_download host tmpdir/testprog.o];
333 } else {
334 set objfile tmpdir/testprog.o
337 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
338 if ![string match "" $exec_output] {
339 fail $test
340 return
343 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
344 if ![string match "*: no symbols*" $exec_output] {
345 fail $test
346 return
349 pass $test
352 strip_test
354 # Test stripping an object file with saving a symbol
356 proc strip_test_with_saving_a_symbol { } {
357 global CC
358 global STRIP
359 global STRIPFLAGS
360 global NM
361 global NMFLAGS
362 global srcdir
363 global subdir
365 set test "strip with saving a symbol"
367 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
368 untested $test
369 return
372 if [is_remote host] {
373 set objfile [remote_download host tmpdir/testprog.o];
374 } else {
375 set objfile tmpdir/testprog.o
378 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
379 if ![string match "" $exec_output] {
380 fail $test
381 return
384 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
385 if {![regexp {^([0-9a-fA-F]+)?[ ]+T main} $exec_output] \
386 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
387 fail $test
388 return
391 pass $test
394 strip_test_with_saving_a_symbol
396 # Build a final executable.
398 proc copy_setup { } {
399 global srcdir
400 global subdir
402 set res [build_wrapper testglue.o];
403 set flags { debug };
405 if { $res != "" } {
406 lappend flags "additional_flags=[lindex $res 1]";
407 set add_libs "testglue.o";
408 } else {
409 set add_libs "";
412 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/testprog executable $flags] != "" } {
413 return 2
416 set result [remote_load target tmpdir/testprog];
417 set status [lindex $result 0];
419 if { $status != "pass" } {
420 perror "unresolved setup, status = $status"
421 return 3
424 return 0
427 # Test copying an executable.
429 proc copy_executable { prog flags test1 test2 } {
431 if [is_remote host] {
432 set testfile [remote_download host tmpdir/testprog];
433 set testcopy copyprog
434 } else {
435 set testfile tmpdir/testprog
436 set testcopy tmpdir/copyprog
438 remote_file host delete $testcopy;
440 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
442 if ![string match "" $exec_output] {
443 fail $test1
444 fail $test2
445 return
448 if [is_remote host] {
449 remote_upload host $testcopy tmpdir/copyprog
452 set status [remote_exec build "cmp" "tmpdir/testprog tmpdir/copyprog"]
453 set exec_output [lindex $status 1];
455 if [string match "" $exec_output] then {
456 pass $test1
457 } else {
458 send_log "$exec_output\n"
459 verbose "$exec_output"
461 # This will fail for many reasons. For example, it will most
462 # likely fail if a non-GNU linker is used. Therefore, we do
463 # not insist that it pass. If you are using an assembler and
464 # linker based on the same BFD as objcopy, it is worth
465 # investigating to see why this failure occurs. If we are
466 # cross compiling, we assume that a GNU linker is being used,
467 # and expect it to succeed.
468 if {[isnative]} then {
469 setup_xfail "*-*-*"
472 # This also fails for mips*-*-elf targets. See elf32-mips.c
473 # mips_elf_sym_is_global.
474 setup_xfail "mips*-*-elf"
476 fail $test1
479 set output [remote_load target tmpdir/copyprog]
480 set status [lindex $output 0];
481 if { $status != "pass" } {
482 fail $test2
483 } else {
484 pass $test2
488 # Test stripping an executable
490 proc strip_executable { prog flags test } {
491 global NM
492 global NMFLAGS
494 remote_download build tmpdir/copyprog tmpdir/striprog
495 if [is_remote host] {
496 set copyfile [remote_download host tmpdir/striprog];
497 } else {
498 set copyfile tmpdir/striprog
501 set exec_output [binutils_run $prog "$flags ${copyfile}"]
502 if ![string match "" $exec_output] {
503 fail $test
504 return
507 if [is_remote host] {
508 remote_upload host ${copyfile} tmpdir/striprog;
511 set result [remote_load target tmpdir/striprog]
512 set status [lindex $result 0];
513 if { $status != "pass" } {
514 fail $test
515 return
518 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
519 if ![string match "*: no symbols*" $exec_output] {
520 fail $test
521 return
523 pass $test
526 # Test stripping an executable with saving a symbol
528 proc strip_executable_with_saving_a_symbol { prog flags test } {
529 global NM
530 global NMFLAGS
532 remote_download build tmpdir/copyprog tmpdir/striprog
533 if [is_remote host] {
534 set copyfile [remote_download host tmpdir/striprog];
535 } else {
536 set copyfile tmpdir/striprog
539 set exec_output [binutils_run $prog "$flags ${copyfile}"]
540 if ![string match "" $exec_output] {
541 fail $test
542 return
545 if [is_remote host] {
546 remote_upload host ${copyfile} tmpdir/striprog;
549 set result [remote_load target tmpdir/striprog]
550 set status [lindex $result 0];
551 if { $status != "pass" } {
552 fail $test
553 return
556 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
557 if {![regexp {^[0-9a-fA-F]+ T main} $exec_output] \
558 && ![regexp {^[0-9a-fA-F]+ T _main} $exec_output]} {
559 fail $test
560 return
562 pass $test
565 set test1 "simple objcopy of executable"
566 set test2 "run objcopy of executable"
567 set test3 "run stripped executable"
568 set test4 "run stripped executable with saving a symbol"
570 switch [copy_setup] {
571 "1" {
572 # do nothing
574 "2" {
575 untested $test1
576 untested $test2
577 untested $test3
578 untested $test4
580 "3" {
581 unresolved $test1
582 unresolved $test2
583 unresolved $test3
584 unresolved $test4
586 "0" {
587 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
588 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
589 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"