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