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