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