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