This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / binutils / testsuite / binutils-all / objcopy.exp
1 #   Copyright (C) 1994, 95, 96, 97, 1998 Free Software Foundation, Inc.
2
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.
12
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.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-dejagnu@prep.ai.mit.edu
19
20 # Written by Ian Lance Taylor <ian@cygnus.com>
21
22 if ![is_remote host] {
23     if {[which $OBJCOPY] == 0} then {
24         perror "$OBJCOPY does not exist"
25         return
26     }
27 }
28
29 send_user "Version [binutil_version $OBJCOPY]"
30
31 if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
32     perror "unresolved 1"
33     unresolved "objcopy (simple copy)"
34     return
35 }
36
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
43 }
44
45 # Test that objcopy does not modify a file when copying it.
46
47 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
48
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
62     }
63     set status [remote_exec build cmp "${src1} ${src2}"];
64     set exec_output [lindex $status 1];
65     set exec_output [prune_warnings $exec_output]
66
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.
71
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*" "i*86-*-msdos*"
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*" "hppa*-*-linux*"
84     setup_xfail "m88*-*-coff" "m88*-motorola-sysv*"
85     setup_xfail "z8*-*-coff"
86
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
92
93         # On OSF/1, this succeeds with gas and fails with /bin/as.
94         setup_xfail "alpha*-*-osf*"
95
96         # This fails for COFF i960-vxworks targets.
97         setup_xfail "i960-*-vxworks*"
98
99         fail "objcopy (simple copy)"
100     }
101 }
102
103 # Test generating S records.
104
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.
107
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
114 }
115
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
117
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;
124     }
125     set file [open ${srecfile} r]
126
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**"
140         }
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"
159                 }
160             }
161         }
162     }
163
164     close $file
165 }
166
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.
171
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"
190             }
191         }
192     }
193
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"
207             }
208         }
209     }
210 }
211
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.
215
216 set low ""
217 set lowname ""
218
219 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
220
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\]+(.*)"
222
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
231         }
232     }
233     set got $rest
234 }
235
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"
258                 }
259             }
260         }
261     }
262
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"
269         }
270         set got $rest
271     }
272
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 +"
288             }
289         }
290     }
291
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 ="
308             }
309         }
310     }
311 }
312
313 # Test stripping an object.
314
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
323
324     set test "strip"
325
326     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
327         untested $test
328         return
329     }
330
331     if [is_remote host] {
332         set objfile [remote_download host tmpdir/testprog.o];
333     } else {
334         set objfile tmpdir/testprog.o
335     }
336
337     set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
338     if ![string match "" $exec_output] {
339         fail $test
340         return
341     }
342
343     set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
344     if ![string match "*: no symbols*" $exec_output] {
345         fail $test
346         return
347     }
348
349     pass $test
350 }
351
352 strip_test
353
354 # Test stripping an object file with saving a symbol
355
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
364
365     set test "strip with saving a symbol"
366
367     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
368         untested $test
369         return
370     }
371
372     if [is_remote host] {
373         set objfile [remote_download host tmpdir/testprog.o];
374     } else {
375         set objfile tmpdir/testprog.o
376     }
377
378     set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
379     if ![string match "" $exec_output] {
380         fail $test
381         return
382     }
383
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
389     }
390
391     pass $test
392 }
393
394 strip_test_with_saving_a_symbol
395
396 # Build a final executable.
397
398 proc copy_setup { } {
399     global srcdir
400     global subdir
401     
402     set res [build_wrapper testglue.o];
403     set flags { debug };
404     
405     if { $res != "" } {
406         lappend flags "additional_flags=[lindex $res 1]";
407         set add_libs "testglue.o";
408     } else {
409         set add_libs "";
410     }
411
412     if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/testprog executable $flags]  != "" } {
413         return 2
414     }
415
416     set result [remote_load target tmpdir/testprog];
417     set status [lindex $result 0];
418
419     if { $status != "pass"  } {
420         perror "unresolved setup, status = $status"
421         return 3
422     }
423
424     return 0
425 }
426
427 # Test copying an executable.
428
429 proc copy_executable { prog flags test1 test2 } {
430
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
437     }
438     remote_file host delete $testcopy;
439
440     set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
441
442     if ![string match "" $exec_output] {
443         fail $test1
444         fail $test2
445         return
446     }
447
448     if [is_remote host] {
449         remote_upload host $testcopy tmpdir/copyprog
450     }
451
452     set status [remote_exec build "cmp" "tmpdir/testprog tmpdir/copyprog"]
453     set exec_output [lindex $status 1];
454
455     if [string match "" $exec_output] then {
456         pass $test1
457     } else {
458         send_log "$exec_output\n"
459         verbose "$exec_output"
460
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 "*-*-*"
470         }
471
472         # This also fails for mips*-*-elf targets.  See elf32-mips.c
473         # mips_elf_sym_is_global.
474         setup_xfail "mips*-*-elf"
475
476         setup_xfail "*arm*-*-coff"
477         setup_xfail "arm*-*-pe"
478         setup_xfail "thumb*-*-coff"
479         setup_xfail "thumb*-*-pe"
480     
481         fail $test1
482     }
483
484     set output [remote_load target tmpdir/copyprog]
485     set status [lindex $output 0];
486     if { $status != "pass" } {
487         fail $test2
488     } else {
489         pass $test2
490     }
491 }
492
493 # Test stripping an executable
494
495 proc strip_executable { prog flags test } {
496     global NM
497     global NMFLAGS
498
499     remote_download build tmpdir/copyprog tmpdir/striprog
500     if [is_remote host] {
501         set copyfile [remote_download host tmpdir/striprog];
502     } else {
503         set copyfile tmpdir/striprog
504     }
505
506     set exec_output [binutils_run $prog "$flags ${copyfile}"]
507     if ![string match "" $exec_output] {
508         fail $test
509         return
510     }
511
512     if [is_remote host] {
513         remote_upload host ${copyfile} tmpdir/striprog;
514     }
515
516     set result [remote_load target tmpdir/striprog]
517     set status [lindex $result 0];
518     if { $status != "pass" } {
519         fail $test
520         return
521     }
522
523     set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
524     if ![string match "*: no symbols*" $exec_output] {
525         fail $test
526         return
527     }
528     pass $test
529 }
530
531 # Test stripping an executable with saving a symbol
532
533 proc strip_executable_with_saving_a_symbol { prog flags test } {
534     global NM
535     global NMFLAGS
536
537     remote_download build tmpdir/copyprog tmpdir/striprog
538     if [is_remote host] {
539         set copyfile [remote_download host tmpdir/striprog];
540     } else {
541         set copyfile tmpdir/striprog
542     }
543
544     set exec_output [binutils_run $prog "$flags ${copyfile}"]
545     if ![string match "" $exec_output] {
546         fail $test
547         return
548     }
549
550     if [is_remote host] {
551         remote_upload host ${copyfile} tmpdir/striprog;
552     }
553
554     set result [remote_load target tmpdir/striprog]
555     set status [lindex $result 0];
556     if { $status != "pass" } {
557         fail $test
558         return
559     }
560
561     set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
562     if {![regexp {^[0-9a-fA-F]+ T main} $exec_output] \
563          && ![regexp {^[0-9a-fA-F]+ T _main} $exec_output]} {
564         fail $test
565         return
566     }
567     pass $test
568 }
569
570 set test1 "simple objcopy of executable"
571 set test2 "run objcopy of executable"
572 set test3 "run stripped executable"
573 set test4 "run stripped executable with saving a symbol"
574
575 switch [copy_setup] {
576     "1" {
577         # do nothing
578     }
579     "2" {
580         untested $test1
581         untested $test2
582         untested $test3
583         untested $test4
584     }
585     "3" {
586         unresolved $test1
587         unresolved $test2
588         unresolved $test3
589         unresolved $test4
590     }
591     "0" {
592         copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
593         strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
594         strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
595     }
596 }