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