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