* elf64-ppc.c (dec_dynrel_count): Don't error when elf_gc_sweep_symbol
[external/binutils.git] / binutils / testsuite / binutils-all / objcopy.exp
1 #   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 #   2004, 2006, 2007, 2009, 2010, 2011
3 #   Free Software Foundation, Inc.
4
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 3 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.
14
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # Please email any bugs, comments, and/or additions to this file to:
20 # bug-dejagnu@prep.ai.mit.edu
21
22 # Written by Ian Lance Taylor <ian@cygnus.com>
23
24 if ![is_remote host] {
25     if {[which $OBJCOPY] == 0} then {
26         perror "$OBJCOPY does not exist"
27         return
28     }
29 }
30
31 send_user "Version [binutil_version $OBJCOPY]"
32
33 if ![is_remote host] {
34     set tempfile tmpdir/bintest.o
35     set copyfile tmpdir/copy
36 } else {
37     set tempfile [remote_download host tmpdir/bintest.o]
38     set copyfile copy
39 }
40
41 # Test that objcopy does not modify a file when copying it.
42
43 proc objcopy_test {testname srcfile} {
44     global OBJCOPY
45     global OBJCOPYFLAGS
46     global srcdir
47     global subdir
48     global tempfile
49     global copyfile
50
51     if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
52         perror "unresolved $testname"
53         unresolved "objcopy ($testname)"
54         return
55     }
56
57     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
58
59     if ![string match "" $got] then {
60         fail "objcopy ($testname)"
61     } else {
62         send_log "cmp $tempfile ${copyfile}.o\n"
63         verbose "cmp $tempfile ${copyfile}.o"
64         if [is_remote host] {
65             set src1 tmpdir/bintest.o
66             set src2 tmpdir/copy.o
67             remote_upload host $tempfile $src1
68             remote_upload host ${copyfile}.o $src2
69         } else {
70             set src1 ${tempfile}
71             set src2 ${copyfile}.o
72         }
73         set status [remote_exec build cmp "${src1} ${src2}"]
74         set exec_output [lindex $status 1]
75         set exec_output [prune_warnings $exec_output]
76
77         # On some systems the result of objcopy will not be identical.
78         # Usually this is just because gas isn't using bfd to write the
79         # files in the first place, and may order things a little
80         # differently. Those systems should use setup_xfail here.
81
82         setup_xfail "h8300-*-coff"
83         setup_xfail "h8500-*-rtems*" "h8500-*-coff"
84         setup_xfail "hppa*-*-*"
85         setup_xfail "i960-*"
86         setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
87         setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
88         setup_xfail "m8*-*"
89         setup_xfail "or32-*-rtems*" "or32-*-coff"
90         setup_xfail "sh-*-coff*"
91         setup_xfail "tic80-*-*" "w65-*"
92
93         clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
94         clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
95         clear_xfail "m68*-*-sysv4*"
96
97         if [string match "" $exec_output] then {
98             pass "objcopy ($testname)"
99         } else {
100             send_log "$exec_output\n"
101             verbose "$exec_output" 1
102
103             # On OSF/1, this succeeds with gas and fails with /bin/as.
104             setup_xfail "alpha*-*-osf*"
105
106             fail "objcopy ($testname)"
107         }
108     }
109 }
110
111 objcopy_test "simple copy" bintest.s
112
113 # Test reversing bytes in a section.
114
115 set reversed ${tempfile}-reversed
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
117
118 if ![string match "" $got] then {
119     fail "objcopy --reverse-bytes"
120 } else {
121     if [is_remote host] {
122         remote_upload host ${reversed} tmpdir/copy-reversed.o
123         set reversed tmpdir/copy-reversed.o
124     }
125
126     set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
127     set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
128
129     set want "^ \[0-9\]+ (\[0-9\]+)"
130     set found_orig [regexp -lineanchor $want $origdata -> origdata]
131     set found_rev [regexp -lineanchor $want $revdata -> revdata]
132
133     if {$found_orig == 0 || $found_rev == 0} then {
134         # som doesn't have a .data section
135         setup_xfail "hppa*-*-hpux*"
136         clear_xfail "hppa*64*-*-hpux*"
137
138         fail "objcopy --reverse-bytes"
139     } else {
140         scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
141         scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
142
143         if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
144             pass "objcopy --reverse-bytes"
145         } else {
146             fail "objcopy --reverse-bytes"
147         }
148     }
149 }
150
151 # Test interleaved copy of multiple byte width
152
153 set sequence_file sequence_file
154 set file [open ${sequence_file} w]
155 puts ${file} "12345678"
156 close ${file}
157
158 if [is_remote host] {
159     remote_upload host ${sequence_file} tmpdir/sequence_file
160     set sequence_file tmpdir/sequence_file
161 }
162
163 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
164
165 if ![string match "" $got] then {
166     fail "objcopy -i --interleave-width"
167 } else {
168     if [is_remote host] {
169         remote_upload host ${copyfile} tmpdir/interleave_output
170         set interleave_output tmpdir/interleave_output
171     } else {
172         set interleave_output ${copyfile}
173     }
174
175     set file [open ${interleave_output} r]
176     gets $file line
177     send_log "$line\n"
178     verbose $line
179
180     if ![string match "1256" $line] then {
181         fail "objcopy -i --interleave-width"
182     }
183     pass "objcopy -i --interleave-width"
184
185     close $file
186 }
187
188 # Test generating S records.
189
190 # We make the srec filename 8.3 compatible. Note that the header string
191 # matched against depends on the name of the file. Ugh.
192
193 if [is_remote host] {
194     set srecfile copy.sre
195     set header_string S00B0000636F70792E737265C1
196 } else {
197     set srecfile ${copyfile}.srec
198     set header_string S0130000746D706469722F636F70792E7372656397
199 }
200
201 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
202
203 if ![string match "" $got] then {
204     fail "objcopy -O srec"
205 } else {
206     if [is_remote host] {
207         remote_upload host ${srecfile} tmpdir/copy.srec
208         set srecfile tmpdir/copy.srec
209     }
210     set file [open ${srecfile} r]
211
212     # The first S record is fixed by the file name we are using.
213     gets $file line
214     send_log "$line\n"
215     verbose $line
216     if ![regexp "$header_string.*" $line] {
217         send_log "bad header\n"
218         fail "objcopy -O srec"
219     } else {
220         while {[gets $file line] != -1 \
221                && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
222             send_log "$line\n"
223             verbose $line
224             set line "**EOF**"
225         }
226         send_log "$line\n"
227         verbose $line
228         if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
229             send_log "bad trailer\n"
230             fail "objcopy -O srec"
231         } else {
232             if {[gets $file line] != -1} then {
233                 send_log "garbage at end\n"
234                 send_log "$line\n"
235                 verbose $line
236                 fail "objcopy -O srec"
237             } else {
238                 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
239                 if ![regexp "file format srec" $got] then {
240                     send_log "objdump failed\n"
241                     fail "objcopy -O srec"
242                 } else {
243                     pass "objcopy -O srec"
244                 }
245             }
246         }
247     }
248
249     close $file
250 }
251
252 # Test setting and adjusting the start address.  We only test this
253 # while generating S records, because we may not be able to set the
254 # start address for other object file formats, and the S record case
255 # is the only useful one anyhow.
256
257 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
258 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
259     perror "objdump can not recognize bintest.o"
260     set origstart ""
261 } else {
262     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
263     if ![string match "" $got] then {
264         fail "objcopy --set-start"
265     } else {
266         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
267         if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
268             fail "objcopy --set-start"
269         } else {
270             if {$srecstart != 0x7654} then {
271                 send_log "$srecstart != 0x7654\n"
272                 fail "objcopy --set-start"
273             } else {
274                 pass "objcopy --set-start"
275             }
276         }
277     }
278
279     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
280     if ![string match "" $got] then {
281         fail "objcopy --adjust-start"
282     } else {
283         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
284         if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
285             fail "objcopy --adjust-start"
286         } else {
287             if {$srecstart != $origstart + 0x123} then {
288                 send_log "$srecstart != $origstart + 0x123\n"
289                 fail "objcopy --adjust-start"
290             } else {
291                 pass "objcopy --adjust-start"
292             }
293         }
294     }
295 }
296
297 # Test adjusting the overall VMA, and adjusting the VMA of a
298 # particular section.  We again only test this when generating S
299 # records.
300
301 set low ""
302 set lowname ""
303
304 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
305
306 set headers_regexp "\[  0-9\]+(\[^      \]+)\[  \]*(\[0-9a-fA-F\]+)\[   \]+\[0-9a-fA-F\]+\[     \]+(\[0-9a-fA-F\]+)\[   \]+\[0-9a-fA-F\]+\[     \]+2\[*\]\[*\]\[0-9\]+(.*)"
307
308 set got $headers
309 while {[regexp $headers_regexp $got all name size vma rest]} {
310     set vma 0x$vma
311     set size 0x$size
312     if {$size != 0} {
313         if {$low == "" || $vma < $low} {
314             set low $vma
315             set lowname $name
316         }
317     }
318     set got $rest
319 }
320
321 if {$low == "" || $origstart == ""} then {
322     perror "objdump can not recognize bintest.o"
323 } else {
324     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
325     if ![string match "" $got] then {
326         fail "objcopy --adjust-vma"
327     } else {
328         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
329         set want "file format srec.*start address\[     \]*(\[0-9a-fA-FxX\]+).*sec1\[   \]+\[0-9a-fA-F\]+\[     \]+(\[0-9a-fA-F\]+)"
330         if ![regexp $want $got all start vma] then {
331             fail "objcopy --adjust-vma"
332         } else {
333             set vma 0x$vma
334             if {$vma != $low + 0x123} then {
335                 send_log "$vma != $low + 0x123\n"
336                 fail "objcopy --adjust-vma"
337             } else {
338                 if {$start != $origstart + 0x123} then {
339                     send_log "$start != $origstart + 0x123\n"
340                     fail "objcopy --adjust-vma"
341                 } else {
342                     pass "objcopy --adjust-vma"
343                 }
344             }
345         }
346     }
347
348     set arg ""
349     set got $headers
350     while {[regexp $headers_regexp $got all name size vma rest]} {
351         set vma 0x$vma
352         if {$vma == $low} then {
353             set arg "$arg --adjust-section-vma $name+4"
354         }
355         set got $rest
356     }
357
358     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
359     if ![string match "" $got] then {
360         fail "objcopy --adjust-section-vma +"
361     } else {
362         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
363         set want "file format srec.*sec1\[      \]+\[0-9a-fA-F\]+\[     \]+(\[0-9a-fA-F\]+)"
364         if ![regexp $want $got all vma] then {
365             fail "objcopy --adjust-section-vma +"
366         } else {
367             set vma 0x$vma
368             if {$vma != $low + 4} then {
369                 send_log "$vma != $low + 4\n"
370                 fail "objcopy --adjust-section-vma +"
371             } else {
372                 pass "objcopy --adjust-section-vma +"
373             }
374         }
375     }
376
377     regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
378     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
379     if ![string match "" $got] then {
380         fail "objcopy --adjust-section-vma ="
381     } else {
382         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
383         set want "file format srec.*sec1\[      \]+\[0-9a-fA-F\]+\[     \]+(\[0-9a-fA-F\]+)"
384         if ![regexp $want $got all vma] then {
385             fail "objcopy --adjust-section-vma ="
386         } else {
387             set vma 0x$vma
388             if {$vma != $low + 4} then {
389                 send_log "$vma != $low + 4\n"
390                 fail "objcopy --adjust-section-vma ="
391             } else {
392                 pass "objcopy --adjust-section-vma ="
393             }
394         }
395     }
396 }
397
398 # Test stripping an object.
399
400 proc strip_test { } {
401     global AR
402     global CC
403     global STRIP
404     global STRIPFLAGS
405     global NM
406     global NMFLAGS
407     global srcdir
408     global subdir
409     global READELF
410
411     set test "strip"
412
413     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
414         untested $test
415         return
416     }
417
418     set osabi_fail "false"
419     if [is_elf_format] {
420         verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
421         set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
422         if { [lindex $exec_output 0] != 0 } then {
423             unresolved "$test preserving OS/ABI"        
424             set osabi_fail "true"
425         } else {
426             verbose -log "grep OS/ABI tmpdir/osabi.in"
427             catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
428         }
429     }
430
431     if [is_remote host] {
432         set archive libstrip.a
433         set objfile [remote_download host tmpdir/testprog.o]
434         remote_file host delete $archive
435     } else {
436         set archive tmpdir/libstrip.a
437         set objfile tmpdir/testprog.o
438     }
439
440     remote_file build delete tmpdir/libstrip.a
441
442     set exec_output [binutils_run $AR "rc $archive ${objfile}"]
443     if ![string match "" $exec_output] {
444         fail $test
445         unresolved "$test preserving OS/ABI"    
446         return
447     }
448
449     set exec_output [binutils_run $STRIP "-g $archive"]
450     if ![string match "" $exec_output] {
451         fail $test
452         unresolved "$test preserving OS/ABI"    
453         return
454     }
455
456     set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
457     if ![string match "" $exec_output] {
458         fail $test
459         unresolved "$test preserving OS/ABI"    
460         return
461     }
462
463     if { $osabi_fail != "true" && [is_elf_format] } {
464         verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
465         set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
466         if { [lindex $exec_output 0] != 0 } then {
467             unresolved "$test preserving OS/ABI"        
468         } else {
469             verbose -log "grep OS/ABI tmpdir/osabi.out"
470             catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
471             if { "$osabi_in" == "$osabi_out" } {
472                 pass "$test preserving OS/ABI"
473             } else {
474                 fail "$test preserving OS/ABI"
475             }
476         }
477     }
478
479     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
480         untested $test
481         return
482     }
483
484     if [is_remote host] {
485         set objfile [remote_download host tmpdir/testprog.o]
486     } else {
487         set objfile tmpdir/testprog.o
488     }
489
490     set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
491     if ![string match "" $exec_output] {
492         fail $test
493         return
494     }
495
496     set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
497     if ![string match "*: no symbols*" $exec_output] {
498         fail $test
499         return
500     }
501
502     pass $test
503 }
504
505 strip_test
506
507 # Test stripping an object file with saving a symbol
508
509 proc strip_test_with_saving_a_symbol { } {
510     global CC
511     global STRIP
512     global STRIPFLAGS
513     global NM
514     global NMFLAGS
515     global srcdir
516     global subdir
517
518     set test "strip with saving a symbol"
519
520     if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
521         untested $test
522         return
523     }
524
525     if [is_remote host] {
526         set objfile [remote_download host tmpdir/testprog.o]
527     } else {
528         set objfile tmpdir/testprog.o
529     }
530
531     set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
532     if ![string match "" $exec_output] {
533         fail $test
534         return
535     }
536
537     set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
538     if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
539          && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
540         fail $test
541         return
542     }
543
544     pass $test
545 }
546
547 strip_test_with_saving_a_symbol
548
549 # Build a final executable.
550
551 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
552     set test_prog "testprog.exe"
553 } else {
554     set test_prog "testprog"
555 }
556
557 proc copy_setup { } {
558     global srcdir
559     global subdir
560     global gcc_gas_flag
561     global test_prog
562     global host_triplet
563     
564     set res [build_wrapper testglue.o]
565     set flags { debug }
566     
567     if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
568         return 1
569     }
570     
571     if { $res != "" } {
572         lappend flags "additional_flags=[lindex $res 1]"
573         set add_libs "testglue.o"
574     } else {
575         set add_libs ""
576     }
577
578     if { [istarget *-*-linux*]
579          || [istarget *-*-gnu*] } {
580         foreach i $gcc_gas_flag {
581             set flags "additional_flags=$i $flags"
582         }
583     }
584     if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags]  != "" } {
585         return 2
586     }
587
588     set result [remote_load target tmpdir/$test_prog]
589     set status [lindex $result 0]
590
591     if { $status != "pass"  } {
592         send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
593         if { [istarget $host_triplet] } {
594           return 3
595         }
596       set status "pass"
597     }
598
599     return 0
600 }
601
602 # Test copying an executable.
603
604 proc copy_executable { prog flags test1 test2 } {
605     global test_prog
606     global host_triplet
607
608     if [is_remote host] {
609         set testfile [remote_download host tmpdir/$test_prog]
610         set testcopy copyprog
611     } else {
612         set testfile tmpdir/$test_prog
613         set testcopy tmpdir/copyprog
614     }
615     remote_file host delete $testcopy
616
617     set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
618
619     if ![string match "" $exec_output] {
620         fail $test1
621         if [string match "" $test2] {
622             return
623         }
624         fail $test2
625         return
626     }
627
628     if [is_remote host] {
629         remote_upload host $testcopy tmpdir/copyprog
630     }
631
632     set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
633     set exec_output [lindex $status 1]
634
635     if [string match "" $exec_output] then {
636         pass $test1
637     } else {
638         send_log "$exec_output\n"
639         verbose "$exec_output"
640
641         # This will fail for many reasons.  For example, it will most
642         # likely fail if a non-GNU linker is used.  Therefore, we do
643         # not insist that it pass.  If you are using an assembler and
644         # linker based on the same BFD as objcopy, it is worth
645         # investigating to see why this failure occurs.  If we are
646         # cross compiling, we assume that a GNU linker is being used,
647         # and expect it to succeed.
648         if {[isnative]} then {
649             setup_xfail "*-*-*"
650         }
651
652         # This also fails for mips*-*-elf targets.  See elf32-mips.c
653         # mips_elf_sym_is_global.
654         setup_xfail "mips*-*-elf"
655
656         setup_xfail "arm*-*-coff"
657         setup_xfail "arm*-*-pe"
658         setup_xfail "*-*-mingw*"
659         setup_xfail "*-*-cygwin*"
660     
661         fail $test1
662     }
663
664     if [string match "" $test2] {
665         return
666     }
667
668     set output [remote_load target tmpdir/copyprog]
669     set status [lindex $output 0]
670     if { ![istarget $host_triplet] } {
671       set status "pass"
672     }
673     if { $status != "pass" } {
674         fail $test2
675     } else {
676         pass $test2
677     }
678 }
679
680 # Test stripping an executable
681
682 proc strip_executable { prog flags test } {
683     global NM
684     global NMFLAGS
685     global READELF
686     global host_triplet
687
688     remote_file build delete tmpdir/striprog
689     remote_download build tmpdir/copyprog tmpdir/striprog
690     if [is_remote host] {
691         set copyfile [remote_download host tmpdir/striprog]
692     } else {
693         set copyfile tmpdir/striprog
694     }
695
696     set osabi_fail "false"
697     if [is_elf_format] {
698         verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
699         set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
700         if { [lindex $exec_output 0] != 0 } then {
701             unresolved "$test preserving OS/ABI"        
702             set osabi_fail "true"
703         } else {
704             verbose -log "grep OS/ABI tmpdir/osabi.in"
705             catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
706         }
707      }
708
709     set exec_output [binutils_run $prog "$flags ${copyfile}"]
710     if ![string match "" $exec_output] {
711         fail $test
712         return
713     }
714
715     if [is_remote host] {
716         remote_upload host ${copyfile} tmpdir/striprog
717     }
718
719     set result [remote_load target tmpdir/striprog]
720     set status [lindex $result 0]
721     if { ![istarget $host_triplet] } {
722       set status "pass"
723     }
724     if { $status != "pass" } {
725         fail $test
726         return
727     }
728
729     if { $osabi_fail != "true" && [is_elf_format] } {
730         verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
731         set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
732         if { [lindex $exec_output 0] != 0 } then {
733             unresolved "$test preserving OS/ABI"        
734         } else {
735             verbose -log "grep OS/ABI tmpdir/osabi.out"
736             catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
737             if { "$osabi_in" == "$osabi_out" } {
738                 pass "$test preserving OS/ABI"
739             } else {
740                 fail "$test preserving OS/ABI"
741             }
742         }
743     }
744
745     set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
746     if ![string match "*: no symbols*" $exec_output] {
747         fail $test
748         return
749     }
750     pass $test
751 }
752
753 # Test stripping an executable with saving a symbol
754
755 proc strip_executable_with_saving_a_symbol { prog flags test } {
756     global NM
757     global NMFLAGS
758     global host_triplet
759
760     remote_file build delete tmpdir/striprog
761     remote_download build tmpdir/copyprog tmpdir/striprog
762     if [is_remote host] {
763         set copyfile [remote_download host tmpdir/striprog]
764     } else {
765         set copyfile tmpdir/striprog
766     }
767
768     set exec_output [binutils_run $prog "$flags ${copyfile}"]
769     if ![string match "" $exec_output] {
770         fail $test
771         return
772     }
773
774     if [is_remote host] {
775         remote_upload host ${copyfile} tmpdir/striprog
776     }
777
778     set result [remote_load target tmpdir/striprog]
779     set status [lindex $result 0]
780     if { ![istarget $host_triplet] } {
781       set status "pass"
782     }
783     if { $status != "pass" } {
784         fail $test
785         return
786     }
787
788     set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
789     if { [istarget mmix-knuth-mmixware] } {
790         # Whenever there's a symbol in the mmo format, there's the symbol
791         # Main, so remove it manually from the expected output for sake of
792         # this test.
793
794         # Using "" not {} to get the \n and \r translated.
795         regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
796     }
797
798     if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
799          && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
800         fail $test
801         return
802     }
803     pass $test
804 }
805
806 # Test keeping only debug symbols of an executable
807
808 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
809     global NM
810     global NMFLAGS
811
812     remote_file build delete tmpdir/striprog
813     remote_download build tmpdir/copyprog tmpdir/striprog
814     if [is_remote host] {
815         set copyfile [remote_download host tmpdir/striprog]
816     } else {
817         set copyfile tmpdir/striprog
818     }
819
820     set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
821     if ![string match "" $exec_output] {
822         fail $test1
823         return
824     }
825     pass $test1
826
827     set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
828     if ![string match "" $exec_output] {
829         fail $test2
830         return
831     }
832     pass $test2
833 }
834
835 set test1 "simple objcopy of executable"
836 set test2 "run objcopy of executable"
837 set test3 "run stripped executable"
838 set test4 "run stripped executable with saving a symbol"
839 set test5 "keep only debug data"
840 set test6 "simple objcopy of debug data"
841
842 switch [copy_setup] {
843     "1" {
844         # do nothing
845     }
846     "2" {
847         untested $test1
848         untested $test2
849         untested $test3
850         untested $test4
851         untested $test5
852         untested $test6
853     }
854     "3" {
855         copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
856         unsupported $test2
857         unsupported $test3
858         unsupported $test4
859         unsupported $test5
860         unsupported $test6
861     }
862     "0" {
863         copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
864         strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
865         strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
866         keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test5" \
867                                          "$OBJCOPY" "$OBJCOPYFLAGS" "$test6"
868     }
869 }
870
871 proc objcopy_test_readelf {testname srcfile} {
872     global OBJCOPY
873     global OBJCOPYFLAGS
874     global READELF
875     global srcdir
876     global subdir
877
878     if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
879         unresolved "objcopy ($testname)"
880         return
881     }
882
883     verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
884     set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
885     if { [lindex $exec_output 0] != 0
886          || ![string match "" [lindex $exec_output 1]] } then {
887         fail "objcopy ($testname)"
888         return
889     }
890
891     verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
892     set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
893     if { [lindex $exec_output 0] != 0 } then {
894         unresolved "objcopy ($testname)"
895         return
896     }
897     set exec_output [prune_warnings [lindex $exec_output 1]]
898     if ![string match "" $exec_output] then {
899         unresolved "objcopy ($testname)"
900         return
901     }
902
903     verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
904     set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
905     if { [lindex $exec_output 0] != 0 } then {
906         unresolved "objcopy ($testname)"
907         return
908     }
909     set exec_output [prune_warnings [lindex $exec_output 1]]
910     if ![string match "" $exec_output] then {
911         unresolved "objcopy ($testname)"
912         return
913     }
914
915     verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
916     catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
917     set exec_output [prune_warnings $exec_output]
918
919     if [string match "" $exec_output] then {
920         pass "objcopy ($testname)"
921     } else {
922         fail "objcopy ($testname)"
923     }
924 }
925
926 # ia64 specific tests
927 if { ([istarget "ia64-*-elf*"]
928        || [istarget "ia64-*-linux*"]) } {
929     objcopy_test "ia64 link order" link-order.s
930 }
931
932 # ELF specific tests
933 if [is_elf_format] {
934     objcopy_test "ELF unknown section type" unknown.s
935     objcopy_test_readelf "ELF group" group.s
936     objcopy_test_readelf "ELF group" group-2.s
937     objcopy_test_readelf "ELF group" group-3.s
938     objcopy_test_readelf "ELF group" group-4.s
939     run_dump_test "group-5"
940     run_dump_test "group-6"
941     run_dump_test "copy-1"
942     run_dump_test "note-1"
943 }
944
945 run_dump_test "copy-2"
946 run_dump_test "copy-3"
947 run_dump_test "copy-4"
948
949 if [is_elf_format] {
950     run_dump_test "strip-1"
951     run_dump_test "strip-2"
952     run_dump_test "strip-3"
953     run_dump_test "strip-4"
954     run_dump_test "strip-5"
955     run_dump_test "strip-6"
956     run_dump_test "strip-7"
957     run_dump_test "strip-8"
958     run_dump_test "strip-9"
959     if { ![istarget "*-*-hpux*"]
960          && ![istarget "msp*-*-*"] } {
961         run_dump_test "strip-10"
962     }
963
964     if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
965         # Check to make sure we don't strip a symbol named in relocations.
966         set test "objcopy keeps symbols needed by relocs"
967
968         set srcfile $srcdir/$subdir/needed-by-reloc.s
969
970         if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
971             unresolved $test
972         } else {
973             set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
974
975             if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
976                 pass $test
977             } else {
978                 fail $test
979             }
980         }
981     }
982
983     # The symbol table for some MIPS targets is sorted differently than
984     # the ELF canonical order, so the regexps in localize-hidden-1.d fail
985     # to match.  These tests must be matched to targets for which
986     # targ_defvec=bfd_elf32_bigmips_vec,
987     # targ_defvec=bfd_elf32_littlemips_vec,
988     # targ_defvec=bfd_elf32_nbigmips_vec or
989     # targ_defvec=bfd_elf32_nlittlemips_vec in config.bfd.  When syncing,
990     # don't forget that earlier case-matches trump later ones.
991     if { ![istarget "mips*-sde-elf*"] && ![istarget "mips64*-*-openbsd*"] } {
992         setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-elf*" \
993             "mips*-*-rtems*" "mips*-*-windiss" "mips*-*-none" \
994             "mips*-*-openbsd*" "mips*-*-chorus*"
995     }
996     run_dump_test "localize-hidden-1"
997     run_dump_test "testranges"
998     run_dump_test "testranges-ia64"
999
1000     run_dump_test "add-section"
1001     run_dump_test "add-empty-section"
1002
1003     run_dump_test "exclude-1a"
1004     run_dump_test "exclude-1b"
1005 }
1006 run_dump_test "localize-hidden-2"