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