Remove h8300-coff support
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / objcopy.exp
1 # Copyright (C) 1994-2018 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} $tempfile]} then {
50 unresolved "objcopy ($testname)"
51 remote_file host delete $tempfile
52 return
53 }
54
55 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
56
57 if ![string equal "" $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 "h8500-*-rtems*" "h8500-*-coff"
81 setup_xfail "hppa*-*-*"
82 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
83 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
84 setup_xfail "m8*-*"
85 setup_xfail "sh-*-coff*"
86 setup_xfail "tic80-*-*" "w65-*"
87
88 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
89 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
90 clear_xfail "m68*-*-sysv4*"
91
92 if [string equal "" $exec_output] then {
93 pass "objcopy ($testname)"
94 } else {
95 send_log "$exec_output\n"
96 verbose "$exec_output" 1
97
98 # On OSF/1, this succeeds with gas and fails with /bin/as.
99 setup_xfail "alpha*-*-osf*"
100
101 fail "objcopy ($testname)"
102 }
103 }
104 }
105
106 objcopy_test "simple copy" bintest.s
107
108 if { [file exists $tempfile] } {
109 # Test reversing bytes in a section.
110
111 set reversed ${tempfile}-reversed
112 set sect_names [get_standard_section_names]
113 if { $sect_names != "" } {
114 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
115 } else {
116 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
117 }
118
119 if ![string equal "" $got] then {
120 fail "objcopy --reverse-bytes"
121 } else {
122 if [is_remote host] {
123 remote_upload host ${reversed} tmpdir/copy-reversed.o
124 set reversed tmpdir/copy-reversed.o
125 }
126
127 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
128 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
129
130 set want "^ \[0-9\]+ (\[0-9\]+)"
131 set found_orig [regexp -lineanchor $want $origdata -> origdata]
132 set found_rev [regexp -lineanchor $want $revdata -> revdata]
133
134 if {$found_orig == 0 || $found_rev == 0} then {
135 # som doesn't have a .data section
136 setup_xfail "hppa*-*-hpux*"
137 clear_xfail "hppa*64*-*-hpux*"
138
139 fail "objcopy --reverse-bytes"
140 } else {
141 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
142 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
143
144 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
145 pass "objcopy --reverse-bytes"
146 } else {
147 fail "objcopy --reverse-bytes"
148 }
149 }
150 }
151 }
152
153 # Test interleaved copy of multiple byte width
154
155 set sequence_file sequence_file
156 set file [open ${sequence_file} w]
157 puts ${file} "12345678"
158 close ${file}
159
160 if [is_remote host] {
161 remote_upload host ${sequence_file} tmpdir/sequence_file
162 set sequence_file tmpdir/sequence_file
163 }
164
165 set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
166
167 if ![string equal "" $got] then {
168 fail "objcopy -i --interleave-width"
169 } else {
170 if [is_remote host] {
171 remote_upload host ${copyfile} tmpdir/interleave_output
172 set interleave_output tmpdir/interleave_output
173 } else {
174 set interleave_output ${copyfile}
175 }
176
177 set file [open ${interleave_output} r]
178 gets $file line
179 send_log "$line\n"
180 verbose $line
181
182 if ![string equal "1256" $line] then {
183 fail "objcopy -i --interleave-width"
184 }
185 pass "objcopy -i --interleave-width"
186
187 close $file
188 }
189
190 # Test generating S records.
191
192 if { [file exists $tempfile] } {
193 # We make the srec filename 8.3 compatible. Note that the header string
194 # matched against depends on the name of the file. Ugh.
195
196 if [is_remote host] {
197 set srecfile copy.sre
198 set header_string S00B0000636F70792E737265C1
199 } else {
200 set srecfile ${copyfile}.srec
201 set header_string S0130000746D706469722F636F70792E7372656397
202 }
203
204 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
205
206 if ![string equal "" $got] then {
207 fail "objcopy -O srec"
208 } else {
209 if [is_remote host] {
210 remote_upload host ${srecfile} tmpdir/copy.srec
211 set srecfile tmpdir/copy.srec
212 }
213 set file [open ${srecfile} r]
214
215 # The first S record is fixed by the file name we are using.
216 gets $file line
217 send_log "$line\n"
218 verbose $line
219 if ![regexp "$header_string.*" $line] {
220 send_log "bad header\n"
221 fail "objcopy -O srec"
222 } else {
223 while {[gets $file line] != -1 \
224 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
225 send_log "$line\n"
226 verbose $line
227 set line "**EOF**"
228 }
229 send_log "$line\n"
230 verbose $line
231 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
232 send_log "bad trailer\n"
233 fail "objcopy -O srec"
234 } else {
235 if {[gets $file line] != -1} then {
236 send_log "garbage at end\n"
237 send_log "$line\n"
238 verbose $line
239 fail "objcopy -O srec"
240 } else {
241 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
242 if ![regexp "file format srec" $got] then {
243 send_log "objdump failed\n"
244 fail "objcopy -O srec"
245 } else {
246 pass "objcopy -O srec"
247 }
248 }
249 }
250 }
251
252 close $file
253 }
254 }
255
256 # Test setting and adjusting the start address. We only test this
257 # while generating S records, because we may not be able to set the
258 # start address for other object file formats, and the S record case
259 # is the only useful one anyhow.
260
261 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
262 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
263 perror "objdump can not recognize bintest.o"
264 set origstart ""
265 } else {
266 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
267 if ![string equal "" $got] then {
268 fail "objcopy --set-start"
269 } else {
270 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
271 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
272 fail "objcopy --set-start"
273 } else {
274 if {$srecstart != 0x7654} then {
275 send_log "$srecstart != 0x7654\n"
276 fail "objcopy --set-start"
277 } else {
278 pass "objcopy --set-start"
279 }
280 }
281 }
282
283 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
284 if ![string equal "" $got] then {
285 fail "objcopy --adjust-start"
286 } else {
287 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
288 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
289 fail "objcopy --adjust-start"
290 } else {
291 if {$srecstart != $origstart + 0x123} then {
292 send_log "$srecstart != $origstart + 0x123\n"
293 fail "objcopy --adjust-start"
294 } else {
295 pass "objcopy --adjust-start"
296 }
297 }
298 }
299 }
300
301 # Test adjusting the overall VMA, and adjusting the VMA of a
302 # particular section. We again only test this when generating S
303 # records.
304
305 set low ""
306 set lowname ""
307
308 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
309
310 set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
311
312 set got $headers
313 while {[regexp $headers_regexp $got all name size vma rest]} {
314 set vma 0x$vma
315 set size 0x$size
316 if {$size != 0} {
317 if {$low == "" || $vma < $low} {
318 set low $vma
319 set lowname $name
320 }
321 }
322 set got $rest
323 }
324
325 if {$low == "" || $origstart == ""} then {
326 perror "objdump can not recognize bintest.o"
327 } else {
328 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
329 if ![string equal "" $got] then {
330 fail "objcopy --adjust-vma"
331 } else {
332 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
333 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
334 if ![regexp $want $got all start vma] then {
335 fail "objcopy --adjust-vma"
336 } else {
337 set vma 0x$vma
338 if {$vma != $low + 0x123} then {
339 send_log "$vma != $low + 0x123\n"
340 fail "objcopy --adjust-vma"
341 } else {
342 if {$start != $origstart + 0x123} then {
343 send_log "$start != $origstart + 0x123\n"
344 fail "objcopy --adjust-vma"
345 } else {
346 pass "objcopy --adjust-vma"
347 }
348 }
349 }
350 }
351
352 set arg ""
353 set got $headers
354 while {[regexp $headers_regexp $got all name size vma rest]} {
355 set vma 0x$vma
356 if {$vma == $low} then {
357 set arg "$arg --adjust-section-vma $name+4"
358 }
359 set got $rest
360 }
361
362 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
363 if ![string equal "" $got] then {
364 fail "objcopy --adjust-section-vma +"
365 } else {
366 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
367 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
368 if ![regexp $want $got all vma] then {
369 fail "objcopy --adjust-section-vma +"
370 } else {
371 set vma 0x$vma
372 if {$vma != $low + 4} then {
373 send_log "$vma != $low + 4\n"
374 fail "objcopy --adjust-section-vma +"
375 } else {
376 pass "objcopy --adjust-section-vma +"
377 }
378 }
379 }
380
381 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
382 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
383 if ![string equal "" $got] then {
384 fail "objcopy --adjust-section-vma ="
385 } else {
386 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
387 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
388 if ![regexp $want $got all vma] then {
389 fail "objcopy --adjust-section-vma ="
390 } else {
391 set vma 0x$vma
392 if {$vma != $low + 4} then {
393 send_log "$vma != $low + 4\n"
394 fail "objcopy --adjust-section-vma ="
395 } else {
396 pass "objcopy --adjust-section-vma ="
397 }
398 }
399 }
400 }
401
402 # Test stripping an object.
403
404 proc strip_test { } {
405 global AR
406 global CC
407 global STRIP
408 global STRIPFLAGS
409 global NM
410 global NMFLAGS
411 global srcdir
412 global subdir
413 global READELF
414
415 set test "strip"
416
417 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
418 untested $test
419 return
420 }
421
422 set osabi_fail "false"
423 if [is_elf_format] {
424 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
425 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
426 if { [lindex $exec_output 0] != 0 } then {
427 unresolved "$test preserving OS/ABI"
428 set osabi_fail "true"
429 } else {
430 verbose -log "grep OS/ABI tmpdir/osabi.in"
431 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
432 }
433 }
434
435 if [is_remote host] {
436 set archive libstrip.a
437 set objfile [remote_download host tmpdir/testprog.o]
438 remote_file host delete $archive
439 } else {
440 set archive tmpdir/libstrip.a
441 set objfile tmpdir/testprog.o
442 }
443
444 remote_file build delete tmpdir/libstrip.a
445
446 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
447 if ![string equal "" $exec_output] {
448 fail $test
449 unresolved "$test preserving OS/ABI"
450 return
451 }
452
453 set exec_output [binutils_run $STRIP "-g $archive"]
454 if ![string equal "" $exec_output] {
455 fail $test
456 unresolved "$test preserving OS/ABI"
457 return
458 }
459
460 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
461 if ![string equal "" $exec_output] {
462 fail $test
463 unresolved "$test preserving OS/ABI"
464 return
465 }
466
467 if { $osabi_fail != "true" && [is_elf_format] } {
468 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
469 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
470 if { [lindex $exec_output 0] != 0 } then {
471 unresolved "$test preserving OS/ABI"
472 } else {
473 verbose -log "grep OS/ABI tmpdir/osabi.out"
474 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
475 if { "$osabi_in" == "$osabi_out" } {
476 pass "$test preserving OS/ABI"
477 } else {
478 fail "$test preserving OS/ABI"
479 }
480 }
481 }
482
483 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
484 untested $test
485 return
486 }
487
488 if [is_remote host] {
489 set objfile [remote_download host tmpdir/testprog.o]
490 } else {
491 set objfile tmpdir/testprog.o
492 }
493
494 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
495 if ![string equal "" $exec_output] {
496 fail $test
497 return
498 }
499
500 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
501 if ![string match "*: no symbols*" $exec_output] {
502 fail $test
503 return
504 }
505
506 pass $test
507 }
508
509 strip_test
510
511 # Test stripping an object file with saving a symbol
512
513 proc strip_test_with_saving_a_symbol { } {
514 global CC
515 global STRIP
516 global STRIPFLAGS
517 global NM
518 global NMFLAGS
519 global srcdir
520 global subdir
521
522 set test "strip with saving a symbol"
523
524 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
525 untested $test
526 return
527 }
528
529 if [is_remote host] {
530 set objfile [remote_download host tmpdir/testprog.o]
531 } else {
532 set objfile tmpdir/testprog.o
533 }
534
535 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
536 if ![string equal "" $exec_output] {
537 fail $test
538 return
539 }
540
541 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
542 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
543 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
544 fail $test
545 return
546 }
547
548 pass $test
549 }
550
551 strip_test_with_saving_a_symbol
552
553 # Build a final executable.
554
555 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
556 set test_prog "testprog.exe"
557 } else {
558 set test_prog "testprog"
559 }
560
561 proc copy_setup { } {
562 global srcdir
563 global subdir
564 global gcc_gas_flag
565 global test_prog
566 global host_triplet
567
568 set res [build_wrapper testglue.o]
569 set flags { debug }
570
571 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
572 return 1
573 }
574
575 if { $res != "" } {
576 lappend flags "additional_flags=[lindex $res 1]"
577 set add_libs "testglue.o"
578 } else {
579 set add_libs ""
580 }
581
582 if { [istarget *-*-linux*]
583 || [istarget *-*-gnu*] } {
584 foreach i $gcc_gas_flag {
585 set flags "additional_flags=$i $flags"
586 }
587 }
588 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
589 return 2
590 }
591
592 set result [remote_load target tmpdir/$test_prog]
593 set status [lindex $result 0]
594
595 if { $status != "pass" } {
596 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
597 return 3
598 }
599
600 return 0
601 }
602
603 # Test copying an executable.
604
605 proc copy_executable { prog flags test1 test2 } {
606 global test_prog
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 equal "" $exec_output] {
620 fail $test1
621 if [string equal "" $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 equal "" $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 some mips targets. See elf32-mips.c
653 # mips_elf_sym_is_global.
654 if { [is_bad_symtab] } then {
655 setup_xfail "*-*-*"
656 }
657
658 setup_xfail "arm*-*-coff"
659 setup_xfail "arm*-*-pe"
660 setup_xfail "*-*-mingw*"
661 setup_xfail "*-*-cygwin*"
662
663 fail $test1
664 }
665
666 if [string equal "" $test2] {
667 return
668 }
669
670 set output [remote_load target tmpdir/copyprog]
671 set status [lindex $output 0]
672 if { $status != "pass" } {
673 fail $test2
674 } else {
675 pass $test2
676 }
677 }
678
679 # Test stripping an executable
680
681 proc strip_executable { prog flags test1 test2 } {
682 global NM
683 global NMFLAGS
684 global READELF
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 "$test1 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 equal "" $exec_output] {
709 fail $test1
710 if [string equal "" $test2] {
711 return
712 }
713 fail $test2
714 return
715 }
716
717 if [is_remote host] {
718 remote_upload host ${copyfile} tmpdir/striprog
719 }
720
721 if { $osabi_fail != "true" && [is_elf_format] } {
722 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
723 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
724 if { [lindex $exec_output 0] != 0 } then {
725 unresolved "$test1 preserving OS/ABI"
726 } else {
727 verbose -log "grep OS/ABI tmpdir/osabi.out"
728 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
729 if { "$osabi_in" == "$osabi_out" } {
730 pass "$test1 preserving OS/ABI"
731 } else {
732 fail "$test1 preserving OS/ABI"
733 }
734 }
735 }
736
737 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
738 if ![string match "*: no symbols*" $exec_output] {
739 fail $test1
740 return
741 }
742
743 if [string equal "" $test2] {
744 return
745 }
746
747 set result [remote_load target tmpdir/striprog]
748 set status [lindex $result 0]
749 if { $status != "pass" } {
750 fail $test2
751 return
752 }
753
754 pass $test2
755 }
756
757 # Test stripping an executable with saving a symbol
758
759 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
760 global NM
761 global NMFLAGS
762
763 remote_file build delete tmpdir/striprog
764 remote_download build tmpdir/copyprog tmpdir/striprog
765 if [is_remote host] {
766 set copyfile [remote_download host tmpdir/striprog]
767 } else {
768 set copyfile tmpdir/striprog
769 }
770
771 set exec_output [binutils_run $prog "$flags ${copyfile}"]
772 if ![string equal "" $exec_output] {
773 fail $test1
774 if [string equal "" $test2] {
775 return
776 }
777 fail $test2
778 return
779 }
780
781 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
782 if { [istarget mmix-knuth-mmixware] } {
783 # Whenever there's a symbol in the mmo format, there's the symbol
784 # Main, so remove it manually from the expected output for sake of
785 # this test.
786
787 # Using "" not {} to get the \n and \r translated.
788 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
789 }
790
791 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
792 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
793 fail $test1
794 return
795 }
796
797 if [string equal "" $test2] {
798 return
799 }
800
801 if [is_remote host] {
802 remote_upload host ${copyfile} tmpdir/striprog
803 }
804
805 set result [remote_load target tmpdir/striprog]
806 set status [lindex $result 0]
807 if { $status != "pass" } {
808 fail $test2
809 return
810 }
811
812 pass $test2
813 }
814
815 # Test keeping only debug symbols of an executable
816
817 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
818 remote_file build delete tmpdir/striprog
819 remote_download build tmpdir/copyprog tmpdir/striprog
820 if [is_remote host] {
821 set copyfile [remote_download host tmpdir/striprog]
822 } else {
823 set copyfile tmpdir/striprog
824 }
825
826 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
827 if ![string equal "" $exec_output] {
828 fail $test1
829 return
830 }
831 pass $test1
832
833 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
834 if ![string equal "" $exec_output] {
835 fail $test2
836 return
837 }
838 pass $test2
839 }
840
841 # Tests that in a debug only copy of a file the sections
842 # headers whose types have been changed to NOBITS still
843 # retain their sh_link fields.
844
845 proc keep_debug_symbols_and_check_links { prog flags test } {
846 global READELF
847
848 remote_file build delete tmpdir/striprog
849 remote_download build tmpdir/copyprog tmpdir/striprog
850 if [is_remote host] {
851 set copyfile [remote_download host tmpdir/striprog]
852 } else {
853 set copyfile tmpdir/striprog
854 }
855
856 set exec_output [binutils_run $prog "$flags ${copyfile}"]
857 if ![string equal "" $exec_output] {
858 fail $test
859 return
860 }
861
862 set got [binutils_run $READELF "-S --wide ${copyfile}"]
863
864 set fails 0
865 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
866 while {[regexp \
867 {[^a-zA-Z]+([a-zA-Z0-9_\.]+)[ ]+NOBITS[ ]+[0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9]+[ A]+([0-9]+)(.*)} \
868 $got all name link rest]} {
869 set sh_link 0x$link
870 if {$sh_link == 0} {
871 # Only some NOBITS sections should have a non-zero sh_link field.
872 # Look for them by name.
873 verbose "NOBITS section .$name has a 0 sh_link field\n"
874 switch $name {
875 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
876 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
877 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
882 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
883 }
884 }
885 set got $rest
886 }
887
888 if {$fails == 0} {
889 pass $test
890 } else {
891 fail $test
892 }
893 }
894
895
896 set test1 "simple objcopy of executable"
897 set test1r "run objcopy of executable"
898 set test2 "strip executable"
899 set test2r "run stripped executable"
900 set test3 "strip executable with saving a symbol"
901 set test3r "run stripped executable with saving a symbol"
902 set test4 "keep only debug data"
903 set test5 "simple objcopy of debug data"
904 if [is_elf_format] {
905 set test6 "NOBITS sections retain sh_link field"
906 }
907
908 switch [copy_setup] {
909 "1" {
910 # do nothing
911 }
912 "2" {
913 untested $test1
914 untested $test1r
915 untested $test2
916 untested $test2r
917 untested $test3
918 untested $test3r
919 untested $test4
920 untested $test5
921 if [is_elf_format] {
922 untested $test6
923 }
924 }
925 "3" {
926 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
927 unsupported $test1r
928 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
929 unsupported $test2r
930 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
931 unsupported $test3r
932 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
933 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
934 if [is_elf_format] {
935 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
936 }
937 }
938 "0" {
939 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
940 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
941 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
942 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
943 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
944 if [is_elf_format] {
945 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
946 }
947 }
948 }
949
950 proc objcopy_test_readelf {testname srcfile} {
951 global OBJCOPY
952 global OBJCOPYFLAGS
953 global READELF
954 global srcdir
955 global subdir
956
957 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
958 unresolved "objcopy ($testname)"
959 return
960 }
961
962 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
963 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
964 if { [lindex $exec_output 0] != 0
965 || ![string equal "" [lindex $exec_output 1]] } then {
966 fail "objcopy ($testname)"
967 return
968 }
969
970 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
971 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
972 if { [lindex $exec_output 0] != 0 } then {
973 unresolved "objcopy ($testname)"
974 return
975 }
976 set exec_output [prune_warnings [lindex $exec_output 1]]
977 if ![string equal "" $exec_output] then {
978 unresolved "objcopy ($testname)"
979 return
980 }
981
982 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
983 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
984 if { [lindex $exec_output 0] != 0 } then {
985 unresolved "objcopy ($testname)"
986 return
987 }
988 set exec_output [prune_warnings [lindex $exec_output 1]]
989 if ![string equal "" $exec_output] then {
990 unresolved "objcopy ($testname)"
991 return
992 }
993
994 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
995 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
996 set exec_output [prune_warnings $exec_output]
997
998 if [string equal "" $exec_output] then {
999 pass "objcopy ($testname)"
1000 } else {
1001 fail "objcopy ($testname)"
1002 }
1003 }
1004
1005 proc objcopy_test_symbol_manipulation {} {
1006 global srcdir
1007 global subdir
1008
1009 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1010 foreach t $test_list {
1011 # We need to strip the ".d", but can leave the dirname.
1012 verbose [file rootname $t]
1013 run_dump_test [file rootname $t]
1014 }
1015 }
1016
1017 proc objcopy_test_elf_common_symbols {} {
1018 global srcdir
1019 global subdir
1020
1021 # hpux has a non-standard common directive.
1022 if { [istarget "*-*-hpux*"] } then {
1023 return
1024 }
1025
1026 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1027 foreach t $test_list {
1028 # We need to strip the ".d", but can leave the dirname.
1029 verbose [file rootname $t]
1030 run_dump_test [file rootname $t]
1031 }
1032 }
1033
1034 # ia64 specific tests
1035 if { ([istarget "ia64-*-elf*"]
1036 || [istarget "ia64-*-linux*"]) } {
1037 objcopy_test "ia64 link order" link-order.s
1038 }
1039
1040 # ELF specific tests
1041 if [is_elf_format] {
1042 objcopy_test_symbol_manipulation
1043 objcopy_test_elf_common_symbols
1044 objcopy_test "ELF unknown section type" unknown.s
1045 objcopy_test_readelf "ELF group" group.s
1046 objcopy_test_readelf "ELF group" group-2.s
1047 objcopy_test_readelf "ELF group" group-3.s
1048 objcopy_test_readelf "ELF group" group-4.s
1049 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1050 run_dump_test "group-5"
1051 run_dump_test "group-6"
1052 run_dump_test "group-7a"
1053 run_dump_test "group-7b"
1054 run_dump_test "group-7c"
1055 run_dump_test "copy-1"
1056 run_dump_test "note-1"
1057 if [is_elf64 tmpdir/bintest.o] {
1058 run_dump_test "note-2-64"
1059 run_dump_test "note-3-64"
1060 run_dump_test "note-4-64"
1061 } else {
1062 run_dump_test "note-2-32"
1063 run_dump_test "note-3-32"
1064 run_dump_test "note-4-32"
1065 }
1066 }
1067
1068 run_dump_test "copy-2"
1069 run_dump_test "copy-3"
1070 run_dump_test "copy-4"
1071 run_dump_test "pr19020a"
1072 run_dump_test "pr19020b"
1073
1074 if [is_elf_format] {
1075 run_dump_test "strip-1"
1076 run_dump_test "strip-2"
1077 run_dump_test "strip-3"
1078 run_dump_test "strip-4"
1079 run_dump_test "strip-5"
1080 run_dump_test "strip-6"
1081 run_dump_test "strip-7"
1082 run_dump_test "strip-8"
1083 run_dump_test "strip-9"
1084 run_dump_test "strip-12"
1085
1086 if { [istarget "mips64*-*-openbsd*"] } {
1087 set reloc_format mips64
1088 } elseif { [istarget "arm-*"] \
1089 || [istarget "d10v-*"] \
1090 || [istarget "dlx-*"] \
1091 || [istarget "i*86-*"] \
1092 || [istarget "m681*-*"] \
1093 || [istarget "m68hc1*-*"] \
1094 || ([istarget "mips*-*"] \
1095 && ![istarget "mips64*-ps2-elf*"] \
1096 && ![istarget "*-*-irix6*"] \
1097 && ![istarget "mips64*-*-freebsd*"] \
1098 && ![istarget "mips64*-*-kfreebsd*-gnu"] \
1099 && ![istarget "mips64*-*-linux*"]) \
1100 || [istarget "score*-*"] \
1101 || [istarget "xgate-*"] } {
1102 set reloc_format rel
1103 } else {
1104 set reloc_format rela
1105 }
1106 run_dump_test "strip-13" [list [list source strip-13${reloc_format}.s]]
1107 # Select a relocation number that corresponds to one actually
1108 # supported by the target and ABI being tested.
1109 if { [istarget "aarch64*-*"] } {
1110 set reloc 259
1111 } elseif { [istarget "ia64*-*"] \
1112 || [istarget "m32r*-*"] \
1113 || [istarget "nds32*-*"] \
1114 || [istarget "v850*-*"] } {
1115 set reloc 50
1116 } elseif { [istarget "pru-*"] } {
1117 set reloc 11
1118 } else {
1119 set reloc 1
1120 }
1121 run_dump_test "strip-14" [list \
1122 [list source strip-14${reloc_format}.s] \
1123 [list as "--defsym RELOC=${reloc}"] \
1124 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1125 ? "--defsym ELF64=1" : ""}]]]
1126 run_dump_test "strip-15" [list \
1127 [list source strip-15${reloc_format}.s] \
1128 [list as "--defsym RELOC=${reloc}"] \
1129 [list as [expr {[is_elf64 tmpdir/bintest.o] \
1130 ? "--defsym ELF64=1" : ""}]]]
1131
1132 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1133 if { [supports_gnu_unique] } {
1134 run_dump_test "strip-10"
1135 }
1136 set extra_strip11 ""
1137 if { [istarget "sh64*-*"] } {
1138 # pr17755 testcase
1139 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1140 }
1141 run_dump_test "strip-11" $extra_strip11
1142
1143 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1144 # Check to make sure we don't strip a symbol named in relocations.
1145 set test "objcopy keeps symbols needed by relocs"
1146
1147 set srcfile $srcdir/$subdir/needed-by-reloc.s
1148
1149 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1150 unresolved $test
1151 } else {
1152 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1153
1154 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1155 pass $test
1156 } else {
1157 fail $test
1158 }
1159 }
1160 }
1161
1162 # The symbol table for some MIPS targets is sorted differently than
1163 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1164 # to match.
1165 if { [is_bad_symtab] } then {
1166 setup_xfail "*-*-*"
1167 }
1168 run_dump_test "localize-hidden-1"
1169 run_dump_test "testranges"
1170 run_dump_test "testranges-ia64"
1171
1172 run_dump_test "add-section"
1173 run_dump_test "add-symbol"
1174 run_dump_test "add-empty-section"
1175
1176 run_dump_test "exclude-1a"
1177 run_dump_test "exclude-1b"
1178
1179 run_dump_test "only-section-01"
1180 run_dump_test "remove-section-01"
1181
1182 # Test the remove relocation functionality
1183 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1184 foreach t $test_list {
1185 # We need to strip the ".d", but can leave the dirname.
1186 verbose [file rootname $t]
1187 run_dump_test [file rootname $t]
1188 }
1189 }
1190 run_dump_test "localize-hidden-2"
1191
1192 # Test objcopying an object file without global symbol
1193
1194 proc objcopy_test_without_global_symbol { } {
1195 global OBJCOPY
1196 global OBJCOPYFLAGS
1197 global OBJDUMP
1198 global OBJDUMPFLAGS
1199 global srcdir
1200 global subdir
1201
1202 set test "strip without global symbol "
1203
1204 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1205 untested $test
1206 return
1207 }
1208
1209 if [is_remote host] {
1210 set objfile [remote_download host tmpdir/pr19547.o]
1211 } else {
1212 set objfile tmpdir/pr19547.o
1213 }
1214
1215 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1216 if ![string equal "" $exec_output] {
1217 fail $test
1218 return
1219 }
1220
1221 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1222 if {![regexp "no symbols" $exec_output]} {
1223 fail $test
1224 return
1225 }
1226
1227 pass $test
1228 }
1229
1230 # The AArch64 and ARM targets preserve mapping symbols
1231 # in object files, so they will fail this test.
1232 setup_xfail aarch64*-*-* arm*-*-*
1233
1234 objcopy_test_without_global_symbol
This page took 0.055972 seconds and 4 git commands to generate.