Prevent the --keep-global-symbol and --globalize-symbol options from being used together.
[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 set exec_output [prune_warnings $exec_output]
444 if ![string equal "" $exec_output] {
445 fail $test
446 unresolved "$test preserving OS/ABI"
447 return
448 }
449
450 set exec_output [binutils_run $STRIP "-g $archive"]
451 set exec_output [prune_warnings $exec_output]
452 if ![string equal "" $exec_output] {
453 fail $test
454 unresolved "$test preserving OS/ABI"
455 return
456 }
457
458 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
459 set exec_output [prune_warnings $exec_output]
460 if ![string equal "" $exec_output] {
461 fail $test
462 unresolved "$test preserving OS/ABI"
463 return
464 }
465
466 if { $osabi_fail != "true" && [is_elf_format] } {
467 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
468 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
469 if { [lindex $exec_output 0] != 0 } then {
470 unresolved "$test preserving OS/ABI"
471 } else {
472 verbose -log "grep OS/ABI tmpdir/osabi.out"
473 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
474 if { "$osabi_in" == "$osabi_out" } {
475 pass "$test preserving OS/ABI"
476 } else {
477 fail "$test preserving OS/ABI"
478 }
479 }
480 }
481
482 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
483 untested $test
484 return
485 }
486
487 if [is_remote host] {
488 set objfile [remote_download host tmpdir/testprog.o]
489 } else {
490 set objfile tmpdir/testprog.o
491 }
492
493 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
494 set exec_output [prune_warnings $exec_output]
495 if ![string equal "" $exec_output] {
496 fail $test
497 return
498 }
499
500 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
501 set exec_output [prune_warnings $exec_output]
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 set exec_output [prune_warnings $exec_output]
538 if ![string equal "" $exec_output] {
539 fail $test
540 return
541 }
542
543 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
544 set exec_output [prune_warnings $exec_output]
545 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
546 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
547 fail $test
548 return
549 }
550
551 pass $test
552 }
553
554 strip_test_with_saving_a_symbol
555
556 # Build a final executable.
557
558 if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
559 set test_prog "testprog.exe"
560 } else {
561 set test_prog "testprog"
562 }
563
564 proc copy_setup { } {
565 global srcdir
566 global subdir
567 global gcc_gas_flag
568 global test_prog
569 global host_triplet
570
571 set res [build_wrapper testglue.o]
572 set flags { debug }
573
574 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] && ![istarget arm*-*-uclinuxfdpiceabi] } {
575 return 1
576 }
577
578 if { $res != "" } {
579 lappend flags "additional_flags=[lindex $res 1]"
580 set add_libs "testglue.o"
581 } else {
582 set add_libs ""
583 }
584
585 if { [istarget *-*-linux*]
586 || [istarget *-*-gnu*] } {
587 foreach i $gcc_gas_flag {
588 set flags "additional_flags=$i $flags"
589 }
590 }
591 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
592 return 2
593 }
594
595 set result [remote_load target tmpdir/$test_prog]
596 set status [lindex $result 0]
597
598 if { $status != "pass" } {
599 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
600 return 3
601 }
602
603 return 0
604 }
605
606 # Test copying an executable.
607
608 proc copy_executable { prog flags test1 test2 } {
609 global test_prog
610
611 if [is_remote host] {
612 set testfile [remote_download host tmpdir/$test_prog]
613 set testcopy copyprog
614 } else {
615 set testfile tmpdir/$test_prog
616 set testcopy tmpdir/copyprog
617 }
618 remote_file host delete $testcopy
619
620 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
621
622 if ![string equal "" $exec_output] {
623 fail $test1
624 if [string equal "" $test2] {
625 return
626 }
627 fail $test2
628 return
629 }
630
631 if [is_remote host] {
632 remote_upload host $testcopy tmpdir/copyprog
633 }
634
635 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
636 set exec_output [lindex $status 1]
637
638 if [string equal "" $exec_output] then {
639 pass $test1
640 } else {
641 send_log "$exec_output\n"
642 verbose "$exec_output"
643
644 # This will fail for many reasons. For example, it will most
645 # likely fail if a non-GNU linker is used. Therefore, we do
646 # not insist that it pass. If you are using an assembler and
647 # linker based on the same BFD as objcopy, it is worth
648 # investigating to see why this failure occurs. If we are
649 # cross compiling, we assume that a GNU linker is being used,
650 # and expect it to succeed.
651 if {[isnative]} then {
652 setup_xfail "*-*-*"
653 }
654
655 # This also fails for some mips targets. See elf32-mips.c
656 # mips_elf_sym_is_global.
657 if { [is_bad_symtab] } then {
658 setup_xfail "*-*-*"
659 }
660
661 setup_xfail "arm*-*-pe"
662 setup_xfail "*-*-mingw*"
663 setup_xfail "*-*-cygwin*"
664
665 fail $test1
666 }
667
668 if [string equal "" $test2] {
669 return
670 }
671
672 set output [remote_load target tmpdir/copyprog]
673 set status [lindex $output 0]
674 if { $status != "pass" } {
675 fail $test2
676 } else {
677 pass $test2
678 }
679 }
680
681 # Test stripping an executable
682
683 proc strip_executable { prog flags test1 test2 } {
684 global NM
685 global NMFLAGS
686 global READELF
687
688 remote_file build delete tmpdir/striprog
689 remote_download build tmpdir/copyprog tmpdir/striprog
690 if [is_remote host] {
691 set copyfile [remote_download host tmpdir/striprog]
692 } else {
693 set copyfile tmpdir/striprog
694 }
695
696 set osabi_fail "false"
697 if [is_elf_format] {
698 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
699 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
700 if { [lindex $exec_output 0] != 0 } then {
701 unresolved "$test1 preserving OS/ABI"
702 set osabi_fail "true"
703 } else {
704 verbose -log "grep OS/ABI tmpdir/osabi.in"
705 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
706 }
707 }
708
709 set exec_output [binutils_run $prog "$flags ${copyfile}"]
710 if ![string equal "" $exec_output] {
711 fail $test1
712 if [string equal "" $test2] {
713 return
714 }
715 fail $test2
716 return
717 }
718
719 if [is_remote host] {
720 remote_upload host ${copyfile} tmpdir/striprog
721 }
722
723 if { $osabi_fail != "true" && [is_elf_format] } {
724 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
725 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
726 if { [lindex $exec_output 0] != 0 } then {
727 unresolved "$test1 preserving OS/ABI"
728 } else {
729 verbose -log "grep OS/ABI tmpdir/osabi.out"
730 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
731 if { "$osabi_in" == "$osabi_out" } {
732 pass "$test1 preserving OS/ABI"
733 } else {
734 fail "$test1 preserving OS/ABI"
735 }
736 }
737 }
738
739 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
740 if ![string match "*: no symbols*" $exec_output] {
741 fail $test1
742 return
743 }
744
745 if [string equal "" $test2] {
746 return
747 }
748
749 set result [remote_load target tmpdir/striprog]
750 set status [lindex $result 0]
751 if { $status != "pass" } {
752 fail $test2
753 return
754 }
755
756 pass $test2
757 }
758
759 # Test stripping an executable with saving a symbol
760
761 proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
762 global NM
763 global NMFLAGS
764
765 remote_file build delete tmpdir/striprog
766 remote_download build tmpdir/copyprog tmpdir/striprog
767 if [is_remote host] {
768 set copyfile [remote_download host tmpdir/striprog]
769 } else {
770 set copyfile tmpdir/striprog
771 }
772
773 set exec_output [binutils_run $prog "$flags ${copyfile}"]
774 if ![string equal "" $exec_output] {
775 fail $test1
776 if [string equal "" $test2] {
777 return
778 }
779 fail $test2
780 return
781 }
782
783 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
784 if { [istarget mmix-knuth-mmixware] } {
785 # Whenever there's a symbol in the mmo format, there's the symbol
786 # Main, so remove it manually from the expected output for sake of
787 # this test.
788
789 # Using "" not {} to get the \n and \r translated.
790 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
791 }
792
793 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
794 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
795 fail $test1
796 return
797 }
798
799 if [string equal "" $test2] {
800 return
801 }
802
803 if [is_remote host] {
804 remote_upload host ${copyfile} tmpdir/striprog
805 }
806
807 set result [remote_load target tmpdir/striprog]
808 set status [lindex $result 0]
809 if { $status != "pass" } {
810 fail $test2
811 return
812 }
813
814 pass $test2
815 }
816
817 # Test keeping only debug symbols of an executable
818
819 proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
820 remote_file build delete tmpdir/striprog
821 remote_download build tmpdir/copyprog tmpdir/striprog
822 if [is_remote host] {
823 set copyfile [remote_download host tmpdir/striprog]
824 } else {
825 set copyfile tmpdir/striprog
826 }
827
828 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
829 if ![string equal "" $exec_output] {
830 fail $test1
831 return
832 }
833 pass $test1
834
835 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
836 if ![string equal "" $exec_output] {
837 fail $test2
838 return
839 }
840 pass $test2
841 }
842
843 # Tests that in a debug only copy of a file the sections
844 # headers whose types have been changed to NOBITS still
845 # retain their sh_link fields.
846
847 proc keep_debug_symbols_and_check_links { prog flags test } {
848 global READELF
849
850 remote_file build delete tmpdir/striprog
851 remote_download build tmpdir/copyprog tmpdir/striprog
852 if [is_remote host] {
853 set copyfile [remote_download host tmpdir/striprog]
854 } else {
855 set copyfile tmpdir/striprog
856 }
857
858 set exec_output [binutils_run $prog "$flags ${copyfile}"]
859 if ![string equal "" $exec_output] {
860 fail $test
861 return
862 }
863
864 set got [binutils_run $READELF "-S --wide ${copyfile}"]
865
866 set fails 0
867 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
868 while {[regexp \
869 {[^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]+)(.*)} \
870 $got all name link rest]} {
871 set sh_link 0x$link
872 if {$sh_link == 0} {
873 # Only some NOBITS sections should have a non-zero sh_link field.
874 # Look for them by name.
875 verbose "NOBITS section .$name has a 0 sh_link field\n"
876 switch $name {
877 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
878 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
879 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
880 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
881 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
882 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
883 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
884 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
885 }
886 }
887 set got $rest
888 }
889
890 if {$fails == 0} {
891 pass $test
892 } else {
893 fail $test
894 }
895 }
896
897
898 set test1 "simple objcopy of executable"
899 set test1r "run objcopy of executable"
900 set test2 "strip executable"
901 set test2r "run stripped executable"
902 set test3 "strip executable with saving a symbol"
903 set test3r "run stripped executable with saving a symbol"
904 set test4 "keep only debug data"
905 set test5 "simple objcopy of debug data"
906 if [is_elf_format] {
907 set test6 "NOBITS sections retain sh_link field"
908 }
909
910 switch [copy_setup] {
911 "1" {
912 # do nothing
913 }
914 "2" {
915 untested $test1
916 untested $test1r
917 untested $test2
918 untested $test2r
919 untested $test3
920 untested $test3r
921 untested $test4
922 untested $test5
923 if [is_elf_format] {
924 untested $test6
925 }
926 }
927 "3" {
928 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
929 unsupported $test1r
930 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
931 unsupported $test2r
932 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
933 unsupported $test3r
934 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
935 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
936 if [is_elf_format] {
937 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
938 }
939 }
940 "0" {
941 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
942 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
943 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
944 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
945 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
946 if [is_elf_format] {
947 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
948 }
949 }
950 }
951
952 proc objcopy_test_readelf {testname srcfile} {
953 global OBJCOPY
954 global OBJCOPYFLAGS
955 global READELF
956 global srcdir
957 global subdir
958
959 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
960 unresolved "objcopy ($testname)"
961 return
962 }
963
964 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
965 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
966 if { [lindex $exec_output 0] != 0
967 || ![string equal "" [lindex $exec_output 1]] } then {
968 fail "objcopy ($testname)"
969 return
970 }
971
972 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
973 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
974 if { [lindex $exec_output 0] != 0 } then {
975 unresolved "objcopy ($testname)"
976 return
977 }
978 set exec_output [prune_warnings [lindex $exec_output 1]]
979 if ![string equal "" $exec_output] then {
980 unresolved "objcopy ($testname)"
981 return
982 }
983
984 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
985 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
986 if { [lindex $exec_output 0] != 0 } then {
987 unresolved "objcopy ($testname)"
988 return
989 }
990 set exec_output [prune_warnings [lindex $exec_output 1]]
991 if ![string equal "" $exec_output] then {
992 unresolved "objcopy ($testname)"
993 return
994 }
995
996 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
997 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
998 set exec_output [prune_warnings $exec_output]
999
1000 if [string equal "" $exec_output] then {
1001 pass "objcopy ($testname)"
1002 } else {
1003 fail "objcopy ($testname)"
1004 }
1005 }
1006
1007 proc objcopy_test_symbol_manipulation {} {
1008 global srcdir
1009 global subdir
1010
1011 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1012 foreach t $test_list {
1013 # We need to strip the ".d", but can leave the dirname.
1014 verbose [file rootname $t]
1015 run_dump_test [file rootname $t]
1016 }
1017 }
1018
1019 proc objcopy_test_elf_common_symbols {} {
1020 global srcdir
1021 global subdir
1022
1023 # hpux has a non-standard common directive.
1024 if { [istarget "*-*-hpux*"] } then {
1025 return
1026 }
1027
1028 set test_list [lsort [glob -nocomplain $srcdir/$subdir/common-*.d]]
1029 foreach t $test_list {
1030 # We need to strip the ".d", but can leave the dirname.
1031 verbose [file rootname $t]
1032 run_dump_test [file rootname $t]
1033 }
1034 }
1035
1036 # ia64 specific tests
1037 if { ([istarget "ia64-*-elf*"]
1038 || [istarget "ia64-*-linux*"]) } {
1039 objcopy_test "ia64 link order" link-order.s
1040 }
1041
1042 # ELF specific tests
1043 set elf64 ""
1044 if [is_elf_format] {
1045 objcopy_test_symbol_manipulation
1046 objcopy_test_elf_common_symbols
1047 objcopy_test "ELF unknown section type" unknown.s
1048 objcopy_test_readelf "ELF group 1" group.s
1049 objcopy_test_readelf "ELF group 2" group-2.s
1050 objcopy_test_readelf "ELF group 3" group-3.s
1051 objcopy_test_readelf "ELF group 4" group-4.s
1052 objcopy_test_readelf "GNU_MBIND section" mbind1.s
1053 run_dump_test "group-5"
1054 run_dump_test "group-6"
1055 run_dump_test "group-7a"
1056 run_dump_test "group-7b"
1057 run_dump_test "group-7c"
1058 run_dump_test "copy-1"
1059 run_dump_test "note-1"
1060 # Use bintest.o from the note-1 test to determine ELF32 or ELF64
1061 if [is_elf64 tmpdir/bintest.o] {
1062 set elf64 "--defsym ELF64=1"
1063 run_dump_test "note-2-64"
1064 run_dump_test "note-3-64"
1065 run_dump_test "note-4-64"
1066 } else {
1067 run_dump_test "note-2-32"
1068 run_dump_test "note-3-32"
1069 run_dump_test "note-4-32"
1070 }
1071 run_dump_test "note-5"
1072 }
1073
1074 run_dump_test "copy-2"
1075 run_dump_test "copy-3"
1076 run_dump_test "copy-4"
1077 run_dump_test "copy-5"
1078
1079 # Use bintest.o from the copy-4 test to determine ELF reloc type
1080 set reloc_format rel
1081 if { [is_elf_format] && [is_rela tmpdir/bintest.o] } {
1082 set reloc_format rela
1083 }
1084 run_dump_test "pr19020a"
1085 run_dump_test "pr19020b"
1086
1087 if [is_elf_format] {
1088 run_dump_test "strip-1"
1089 run_dump_test "strip-2"
1090 run_dump_test "strip-3"
1091 run_dump_test "strip-4"
1092 run_dump_test "strip-5"
1093 run_dump_test "strip-6"
1094 run_dump_test "strip-7"
1095 run_dump_test "strip-8"
1096 run_dump_test "strip-9"
1097 run_dump_test "strip-12"
1098
1099 if { [istarget "mips64*-*-openbsd*"] } {
1100 set reloc_format mips64
1101 }
1102 # A relocation type not supported by any target
1103 if { [istarget "nds32*-*"] } {
1104 set reloc 255
1105 } else {
1106 set reloc 215
1107 }
1108 run_dump_test "strip-13" [list \
1109 [list source strip-13${reloc_format}.s] \
1110 [list as "${elf64} --defsym RELOC=${reloc}"]]
1111 # Select a relocation number that corresponds to one actually
1112 # supported by the target and ABI being tested.
1113 if { [istarget "aarch64*-*"] } {
1114 set reloc 259
1115 } elseif { [istarget "ia64*-*"] \
1116 || [istarget "m32r*-*"] \
1117 || [istarget "nds32*-*"] \
1118 || [istarget "v850*-*"] } {
1119 set reloc 50
1120 } elseif { [istarget "pru-*"] } {
1121 set reloc 11
1122 } else {
1123 set reloc 1
1124 }
1125 run_dump_test "strip-14" [list \
1126 [list source strip-14${reloc_format}.s] \
1127 [list as "${elf64} --defsym RELOC=${reloc}"]]
1128 run_dump_test "strip-15" [list \
1129 [list source strip-15${reloc_format}.s] \
1130 [list as "${elf64} --defsym RELOC=${reloc}"]]
1131
1132 # This requires STB_GNU_UNIQUE support with OSABI set to GNU.
1133 if { [supports_gnu_unique] } {
1134 run_dump_test "strip-10"
1135 }
1136 run_dump_test "strip-11"
1137
1138 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1139 # Check to make sure we don't strip a symbol named in relocations.
1140 set test "objcopy keeps symbols needed by relocs"
1141
1142 set srcfile $srcdir/$subdir/needed-by-reloc.s
1143
1144 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1145 unresolved $test
1146 } else {
1147 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1148
1149 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1150 pass $test
1151 } else {
1152 fail $test
1153 }
1154 }
1155 }
1156
1157 # The symbol table for some MIPS targets is sorted differently than
1158 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1159 # to match.
1160 if { [is_bad_symtab] } then {
1161 setup_xfail "*-*-*"
1162 }
1163 run_dump_test "localize-hidden-1"
1164 run_dump_test "testranges"
1165 run_dump_test "testranges-ia64"
1166
1167 run_dump_test "add-section"
1168 run_dump_test "add-symbol"
1169 run_dump_test "add-empty-section"
1170
1171 run_dump_test "exclude-1a"
1172 run_dump_test "exclude-1b"
1173
1174 run_dump_test "only-section-01"
1175 run_dump_test "remove-section-01"
1176
1177 # Test the remove relocation functionality
1178 set test_list [lsort [glob -nocomplain $srcdir/$subdir/remove-relocs-*.d]]
1179 foreach t $test_list {
1180 # We need to strip the ".d", but can leave the dirname.
1181 verbose [file rootname $t]
1182 run_dump_test [file rootname $t]
1183 }
1184 }
1185 run_dump_test "localize-hidden-2"
1186
1187 # Test objcopying an object file without global symbol
1188
1189 proc objcopy_test_without_global_symbol { } {
1190 global OBJCOPY
1191 global OBJCOPYFLAGS
1192 global OBJDUMP
1193 global OBJDUMPFLAGS
1194 global srcdir
1195 global subdir
1196
1197 set test "strip without global symbol "
1198
1199 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1200 untested $test
1201 return
1202 }
1203
1204 if [is_remote host] {
1205 set objfile [remote_download host tmpdir/pr19547.o]
1206 } else {
1207 set objfile tmpdir/pr19547.o
1208 }
1209
1210 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1211 set exec_output [prune_warnings $exec_output]
1212 if ![string equal "" $exec_output] {
1213 fail $test
1214 return
1215 }
1216
1217 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1218 set exec_output [prune_warnings $exec_output]
1219 if {![regexp "no symbols" $exec_output]} {
1220 fail $test
1221 return
1222 }
1223
1224 pass $test
1225 }
1226
1227 # The AArch64 and ARM targets preserve mapping symbols
1228 # in object files, so they will fail this test.
1229 setup_xfail aarch64*-*-* arm*-*-*
1230
1231 objcopy_test_without_global_symbol
1232
1233 # objcopy remove relocation from executable test
1234
1235 proc objcopy_remove_relocations_from_executable { } {
1236 global OBJCOPY
1237 global srcdir
1238 global subdir
1239 global READELF
1240
1241 set test "remove-section relocation sections"
1242
1243 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/pr23611 executable debug] != "" } {
1244 untested $test
1245 return
1246 }
1247
1248 if [is_remote host] {
1249 set objfile [remote_download host tmpdir/pr23611]
1250 } else {
1251 set objfile tmpdir/pr23611
1252 }
1253 set out tmpdir/pr23611.out
1254
1255 set exec_output1 [binutils_run $OBJCOPY "-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"]
1256 set exec_output2 [binutils_run $READELF "-S $out"]
1257 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] } {
1258 fail $test
1259 return
1260 }
1261 pass $test
1262 }
1263
1264 objcopy_remove_relocations_from_executable
1265
1266 run_dump_test "pr23633"
This page took 0.078073 seconds and 5 git commands to generate.