1 # Copyright
(C
) 1994-2024 Free Software Foundation
, Inc.
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.
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.
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.
17 # Please email
any bugs
, comments
, and
/or additions to this file to
:
18 # bug
-dejagnu@prep.ai.mit.edu
20 # Written by Ian Lance Taylor
<ian@cygnus.com
>
22 if ![is_remote host
] {
23 if {[which $OBJCOPY
] == 0} then {
24 perror
"$OBJCOPY does not exist"
29 send_user
"Version [binutil_version $OBJCOPY]"
31 if ![is_remote host
] {
32 set tempfile tmpdir
/bintest.o
33 set copyfile tmpdir
/copy
35 set tempfile
[remote_download host tmpdir
/bintest.o
]
39 # Test that objcopy does not modify a file when copying it.
40 #
"object" or "executable" values for type are supported.
42 proc objcopy_test
{testname srcfile type asflags ldflags
} {
49 set t_tempfile $tempfile
50 set t_copyfile $
{copyfile
}.o
52 if { $type
!= "object" && $type != "executable" } {
53 error
"objcopy_test accepts only \"object\" or \"executable\" values for type"
56 if {![binutils_assemble_flags $srcdir
/$subdir
/$
{srcfile
} $t_tempfile
"$asflags"]} then {
57 unsupported
"objcopy $type ($testname)"
58 remote_file host
delete $t_tempfile
63 if { $type
== "executable" } {
65 # Check that
LD exists
66 if {[which $
LD] == 0} then {
67 untested
"objcopy $type ($testname)"
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 unsupported
"objcopy $type ($testname)"
79 set xflags
"--preserve-dates"
83 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS $xflags $t_tempfile $t_copyfile"]
85 if ![string equal
"" $got] then {
87 fail
"objcopy $type ($testname)"
89 send_log
"cmp $t_tempfile $t_copyfile\n"
90 verbose
"cmp $t_tempfile $t_copyfile"
92 set src1 tmpdir
/bintest
94 remote_upload host $t_tempfile $src1
95 remote_upload host $t_copyfile $src2
100 set status [remote_exec build cmp
"${src1} ${src2}"]
101 set exec_output
[lindex $
status 1]
102 set exec_output
[prune_warnings $exec_output
]
104 if [string equal
"" $exec_output] then {
105 pass
"objcopy $type ($testname)"
106 if { $type
== "executable" } {
107 set dir [file dirname $t_copyfile
]
108 set f2
[file tail $t_copyfile
]
109 set status [remote_exec host find
"$dir -name $f2 -newer $t_tempfile -print"]
110 set exec_output
[lindex $
status 1]
111 if [string equal
"" $exec_output] then {
112 pass
"objcopy $type ($testname) timestamp"
114 fail
"objcopy $type ($testname) timestamp"
118 send_log
"$exec_output\n"
119 verbose
"$exec_output" 1
121 #
On OSF
/1, this succeeds with gas and fails with
/bin
/as.
122 setup_xfail
"alpha*-*-osf*"
124 fail
"objcopy $type ($testname)"
129 setup_xfail
"hppa*-*-*"
130 setup_xfail
"tic54x-*-*"
131 clear_xfail
"hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
132 clear_xfail
"hppa*-*-*n*bsd*" "hppa*-*-*elf*"
133 objcopy_test
"simple copy" bintest.s object "" ""
135 # Test verilog data width
136 proc objcopy_test_verilog
{testname
} {
142 set binfile tmpdir
/verilogtest.o
143 set verilog tmpdir
/verilog
145 set got
[binutils_assemble $srcdir
/$subdir
/verilogtest.s $binfile
]
146 if {![binutils_assemble $srcdir
/$subdir
/verilogtest.s $binfile
]} then {
147 unsupported
"objcopy ($testname)"
151 set got
[binutils_run $OBJCOPY
"-O verilog $binfile $verilog"]
152 if ![string equal
"" $got] then {
153 fail
"objcopy ($testname)"
156 set got
[binutils_run $OBJCOPY
"-O verilog --verilog-data-width 0 $binfile $verilog-0.hex"]
157 if ![regexp
"error: verilog data width must be 1, 2, 4, 8 or 16" $got] then {
158 fail
"objcopy ($testname 0) {$got}"
160 pass
"objcopy ($testname 0)"
163 foreach width
{1 2} {
164 set got
[binutils_run $OBJCOPY
"-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
165 if ![string equal
"" $got] then {
166 fail
"objcopy ($testname $width)"
168 send_log
"regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
169 if {! [regexp_diff
"$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
170 pass
"objcopy ($testname $width)"
172 fail
"objcopy ($testname $width)"
176 #
16-bit little
-endian targets fail the following tests because the
177 # verilog backend does not
convert from
16-bits to
32-bits before it
178 # converts from internal format to little endian format.
179 if { [istarget tic54
*-*-*] ||
[istarget pdp11
-*-*] } {
180 untested
"verilog width-4 and width-8 tests"
184 foreach width
{4 8} {
185 set got
[binutils_run $OBJCOPY
"-O verilog --verilog-data-width $width $binfile $verilog-$width.hex"]
186 if ![string equal
"" $got] then {
187 fail
"objcopy ($testname $width)"
189 send_log
"regexp_diff $verilog-$width.hex $srcdir/$subdir/verilog-$width.hex\n"
190 if {! [regexp_diff
"$verilog-$width.hex" "$srcdir/$subdir/verilog-$width.hex"]} {
191 pass
"objcopy ($testname $width)"
193 fail
"objcopy ($testname $width)"
197 # Test generating endian correct output.
198 set testname
"objcopy (verilog output endian-ness == input endian-ness)"
199 set got
[binutils_run $OBJCOPY
"-O verilog --verilog-data-width 4 $binfile $verilog-I4.hex"]
200 if ![string equal
"" $got] then {
203 send_log
"regexp_diff $verilog-I4.hex $srcdir/$subdir/verilog-I4.hex\n"
204 if {! [regexp_diff
"$verilog-I4.hex" "$srcdir/$subdir/verilog-I4.hex"]} {
211 objcopy_test_verilog
"verilog data width"
213 if { [file
exists $tempfile
] } {
214 # Test reversing bytes in a section.
216 set reversed $
{tempfile
}-reversed
217 set sect_names
[get_standard_section_names
]
218 if { $sect_names
!= "" } {
219 if { [istarget hppa
*-*-hpux
*] } {
220 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -j \$PRIVATE\$ -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
222 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -j [lindex $sect_names 1] --reverse-bytes=4 $tempfile $reversed"]
225 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
228 if ![string equal
"" $got] then {
229 fail
"objcopy --reverse-bytes"
231 if [is_remote host
] {
232 remote_upload host $
{reversed
} tmpdir
/copy
-reversed.o
233 set reversed tmpdir
/copy
-reversed.o
236 set origdata
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -s -j .data $tempfile"]
237 set revdata
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -s -j .data $reversed"]
239 set want
"^ \[0-9\]+ (\[0-9\]+)"
240 set found_orig
[regexp
-lineanchor $want $origdata
-> origdata
]
241 set found_rev
[regexp
-lineanchor $want $revdata
-> revdata
]
243 if {$found_orig
== 0 || $found_rev
== 0} then {
244 fail
"objcopy --reverse-bytes"
246 scan $origdata
"%2x%2x%2x%2x" b1 b2 b3 b4
247 scan $revdata
"%2x%2x%2x%2x" c4 c3 c2 c1
249 if {$b1
== $c1
&& $b2
== $c2
&& $b3
== $c3
&& $b4
== $c4
} then {
250 pass
"objcopy --reverse-bytes"
252 fail
"objcopy --reverse-bytes"
258 # Test interleaved copy of multiple byte width
260 set sequence_file sequence_file
261 set file
[open $
{sequence_file
} w
]
262 puts $
{file
} "12345678"
265 if [is_remote host
] {
266 remote_upload host $
{sequence_file
} tmpdir
/sequence_file
267 set sequence_file tmpdir
/sequence_file
270 set got
[binutils_run $OBJCOPY
"-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
272 if ![string equal
"" $got] then {
273 fail
"objcopy -i --interleave-width"
275 if [is_remote host
] {
276 remote_upload host $
{copyfile
} tmpdir
/interleave_output
277 set interleave_output tmpdir
/interleave_output
279 set interleave_output $
{copyfile
}
282 set file
[open $
{interleave_output
} r
]
287 if ![string equal
"1256" $line] then {
288 fail
"objcopy -i --interleave-width"
290 pass
"objcopy -i --interleave-width"
295 # Test generating S records.
297 if { [file
exists $tempfile
] } {
298 # We make the srec filename
8.3 compatible. Note that the header string
299 # matched against depends
on the
name of the file. Ugh.
301 if [is_remote host
] {
302 set srecfile copy.sre
303 set header_string S00B0000636F70792E737265C1
305 set srecfile $
{copyfile
}.srec
306 set header_string S0130000746D706469722F636F70792E7372656397
309 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
311 if ![string equal
"" $got] then {
312 fail
"objcopy -O srec"
314 if [is_remote host
] {
315 remote_upload host $
{srecfile
} tmpdir
/copy.srec
316 set srecfile tmpdir
/copy.srec
318 set file
[open $
{srecfile
} r
]
320 # The first S record is fixed by the file
name we are using.
324 if ![regexp
"$header_string.*" $line] {
325 send_log
"bad header\n"
326 fail
"objcopy -O srec"
328 while {[gets $file line
] != -1 \
329 && [regexp
"^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
336 if ![regexp
"^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
337 send_log
"bad trailer\n"
338 fail
"objcopy -O srec"
340 if {[gets $file line
] != -1} then {
341 send_log
"garbage at end\n"
344 fail
"objcopy -O srec"
346 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -f ${srecfile}"]
347 if ![regexp
"file format srec" $got] then {
348 send_log
"objdump failed\n"
349 fail
"objcopy -O srec"
351 pass
"objcopy -O srec"
361 # Test setting and adjusting the start address. We only test this
362 #
while generating S records
, because we may not be able to
set the
363 # start address
for other object file formats
, and the S record case
364 # is the only useful one anyhow.
366 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -f $tempfile"]
367 if ![regexp
"start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
368 perror
"objdump can not recognize bintest.o"
371 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
372 if ![string equal
"" $got] then {
373 fail
"objcopy --set-start"
375 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -f ${copyfile}.srec"]
376 if ![regexp
"file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
377 fail
"objcopy --set-start"
379 if {$srecstart
!= 0x7654} then {
380 send_log
"$srecstart != 0x7654\n"
381 fail
"objcopy --set-start"
383 pass
"objcopy --set-start"
388 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
389 if ![string equal
"" $got] then {
390 fail
"objcopy --adjust-start"
392 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -f ${copyfile}.srec"]
393 if ![regexp
"file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
394 fail
"objcopy --adjust-start"
396 if {$srecstart
!= $origstart
+ 0x123} then {
397 send_log
"$srecstart != $origstart + 0x123\n"
398 fail
"objcopy --adjust-start"
400 pass
"objcopy --adjust-start"
406 # Test adjusting the overall VMA
, and adjusting the VMA of a
407 # particular section. We again only test this when generating S
413 set headers
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -h $tempfile"]
415 set headers_regexp
"\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
418 while {[regexp $headers_regexp $got all
name size vma rest
]} {
422 if {$low
== "" || $vma < $low} {
430 if {$low
== "" || $origstart == ""} then {
431 perror
"objdump can not recognize bintest.o"
433 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
434 if ![string equal
"" $got] then {
435 fail
"objcopy --adjust-vma"
437 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -fh ${copyfile}.srec"]
438 set want
"file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
439 if ![regexp $want $got all start vma
] then {
440 fail
"objcopy --adjust-vma"
443 if {$vma
!= $low
+ 0x123} then {
444 send_log
"$vma != $low + 0x123\n"
445 fail
"objcopy --adjust-vma"
447 if {$start
!= $origstart
+ 0x123} then {
448 send_log
"$start != $origstart + 0x123\n"
449 fail
"objcopy --adjust-vma"
451 pass
"objcopy --adjust-vma"
459 while {[regexp $headers_regexp $got all
name size vma rest
]} {
461 if {$vma
== $low
} then {
462 set arg "$arg --adjust-section-vma $name+4"
467 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
468 if ![string equal
"" $got] then {
469 fail
"objcopy --adjust-section-vma +"
471 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -h ${copyfile}.srec"]
472 set want
"file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
473 if ![regexp $want $got all vma
] then {
474 fail
"objcopy --adjust-section-vma +"
477 if {$vma
!= $low
+ 4} then {
478 send_log
"$vma != $low + 4\n"
479 fail
"objcopy --adjust-section-vma +"
481 pass
"objcopy --adjust-section-vma +"
486 regsub
-all
"\\+4" $arg "=[expr $low + 4]" argeq
487 set got
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
488 if ![string equal
"" $got] then {
489 fail
"objcopy --adjust-section-vma ="
491 set got
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -h ${copyfile}.srec"]
492 set want
"file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
493 if ![regexp $want $got all vma
] then {
494 fail
"objcopy --adjust-section-vma ="
497 if {$vma
!= $low
+ 4} then {
498 send_log
"$vma != $low + 4\n"
499 fail
"objcopy --adjust-section-vma ="
501 pass
"objcopy --adjust-section-vma ="
507 # Test stripping an object.
509 proc strip_test
{ } {
522 if { [target_compile $srcdir
/$subdir
/testprog.c tmpdir
/testprog.o object debug
] != "" } {
527 set osabi_fail
"false"
529 verbose
-log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
530 set exec_output
[remote_exec host
"$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
531 if { [lindex $exec_output
0] != 0 } then {
532 fail
"$test preserving OS/ABI"
533 set osabi_fail
"true"
535 verbose
-log "grep OS/ABI tmpdir/osabi.in"
536 catch
"exec grep OS/ABI tmpdir/osabi.in" osabi_in
540 if [is_remote host
] {
541 set archive libstrip.a
542 set objfile
[remote_download host tmpdir
/testprog.o
]
543 remote_file host
delete $archive
545 set archive tmpdir
/libstrip.a
546 set objfile tmpdir
/testprog.o
549 remote_file build
delete tmpdir
/libstrip.a
551 set exec_output
[binutils_run $AR
"rc $archive ${objfile}"]
552 set exec_output
[prune_warnings $exec_output
]
553 if ![string equal
"" $exec_output] {
555 unresolved
"$test preserving OS/ABI"
559 set exec_output
[binutils_run $
STRIP "-g $archive"]
560 set exec_output
[prune_warnings $exec_output
]
561 if ![string equal
"" $exec_output] {
563 unresolved
"$test preserving OS/ABI"
567 set exec_output
[binutils_run $
STRIP "$STRIPFLAGS $archive"]
568 set exec_output
[prune_warnings $exec_output
]
569 if ![string equal
"" $exec_output] {
571 unresolved
"$test preserving OS/ABI"
575 if { $osabi_fail
!= "true" && [is_elf_format] } {
576 verbose
-log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
577 set exec_output
[remote_exec host
"$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
578 if { [lindex $exec_output
0] != 0 } then {
579 fail
"$test preserving OS/ABI"
581 verbose
-log "grep OS/ABI tmpdir/osabi.out"
582 catch
"exec grep OS/ABI tmpdir/osabi.out" osabi_out
583 if { "$osabi_in" == "$osabi_out" } {
584 pass
"$test preserving OS/ABI"
586 fail
"$test preserving OS/ABI"
591 if { [target_compile $srcdir
/$subdir
/testprog.c tmpdir
/testprog.o object debug
] != "" } {
596 if [is_remote host
] {
597 set objfile
[remote_download host tmpdir
/testprog.o
]
599 set objfile tmpdir
/testprog.o
602 set exec_output
[binutils_run $
STRIP "$STRIPFLAGS $objfile"]
603 set exec_output
[prune_warnings $exec_output
]
604 if ![string equal
"" $exec_output] {
609 set exec_output
[binutils_run $NM
"-a $NMFLAGS $objfile"]
610 set exec_output
[prune_warnings $exec_output
]
611 if ![string match
"*: no symbols*" $exec_output] {
621 # Test stripping an object file with saving a symbol
623 proc strip_test_with_saving_a_symbol
{ } {
632 set test
"strip with saving a symbol"
634 if { [target_compile $srcdir
/$subdir
/testprog.c tmpdir
/testprog.o object debug
] != "" } {
639 if [is_remote host
] {
640 set objfile
[remote_download host tmpdir
/testprog.o
]
642 set objfile tmpdir
/testprog.o
645 set exec_output
[binutils_run $
STRIP "$STRIPFLAGS -K main -K _main $objfile"]
646 set exec_output
[prune_warnings $exec_output
]
647 if ![string equal
"" $exec_output] {
652 set exec_output
[binutils_run $NM
"$NMFLAGS $objfile"]
653 set exec_output
[prune_warnings $exec_output
]
654 if {![regexp
{^
([0-9a
-fA
-F
]+)?
[ ]+[TtDd
] _?main
} $exec_output
]} {
662 strip_test_with_saving_a_symbol
664 # Build a final executable.
667 set test_prog
"testprog${exe}"
669 proc copy_setup
{ } {
676 if {[target_info
exists needs_status_wrapper
] && \
677 [target_info needs_status_wrapper
] != "0"} {
678 set res
[build_wrapper testglue.o
]
684 if { [istarget
*-*-uclinux
*] && ![istarget tic6x
-*-*] && ![istarget arm
*-*-uclinuxfdpiceabi
] } {
689 lappend flags
"additional_flags=[lindex $res 1]"
690 set add_libs
"testglue.o"
695 if { [istarget
*-*-linux
*]
696 ||
[istarget kvx
-*-*]
697 ||
[istarget
*-*-gnu
*] } {
698 foreach i $gcc_gas_flag
{
699 set flags
"additional_flags=$i $flags"
702 if { [target_compile
"$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
706 set result
[remote_load target tmpdir
/$test_prog
]
707 set status [lindex $result
0]
709 if { $
status != "pass" } {
710 send_log
"cannot run executable, status = ${status} on ${host_triplet}\n"
717 # Test copying an executable.
719 proc copy_executable
{ prog flags test1 test2
} {
722 if [is_remote host
] {
723 set testfile
[remote_download host tmpdir
/$test_prog
]
724 set testcopy copyprog
726 set testfile tmpdir
/$test_prog
727 set testcopy tmpdir
/copyprog
729 remote_file host
delete $testcopy
731 set exec_output
[binutils_run $prog
"$flags $testfile $testcopy"]
733 if ![string equal
"" $exec_output] {
735 if [string equal
"" $test2] {
742 if [is_remote host
] {
743 remote_upload host $testcopy tmpdir
/copyprog
746 set status [remote_exec build
"cmp" "tmpdir/$test_prog tmpdir/copyprog"]
747 set exec_output
[lindex $
status 1]
749 if [string equal
"" $exec_output] then {
752 send_log
"$exec_output\n"
753 verbose
"$exec_output"
755 # This will fail
for many reasons.
For example
, it will most
756 # likely fail
if a non
-GNU linker is used. Therefore
, we
do
757 # not insist that it pass.
If you are using an assembler and
758 # linker based
on the same BFD as objcopy
, it is worth
759 # investigating to see why this failure occurs.
If we are
760 # cross compiling
, we assume that a GNU linker is being used
,
761 # and expect it to succeed.
762 if {[isnative
]} then {
766 # This also fails
for some mips targets. See elf32
-mips.c
767 # mips_elf_sym_is_global.
768 if { [is_bad_symtab
] } then {
772 setup_xfail
"arm*-*-pe"
773 setup_xfail
"*-*-mingw*"
774 setup_xfail
"*-*-cygwin*"
779 if [string equal
"" $test2] {
783 set output
[remote_load target tmpdir
/copyprog
]
784 set status [lindex $output
0]
785 if { $
status != "pass" } {
792 # Test stripping an executable
794 proc strip_executable
{ prog flags test1 test2
} {
799 remote_file build
delete tmpdir
/striprog
800 remote_download build tmpdir
/copyprog tmpdir
/striprog
801 if [is_remote host
] {
802 set copyfile
[remote_download host tmpdir
/striprog
]
804 set copyfile tmpdir
/striprog
807 set osabi_fail
"false"
809 verbose
-log "$READELF -a ${copyfile} > tmpdir/osabi.in"
810 set exec_output
[remote_exec host
"$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
811 if { [lindex $exec_output
0] != 0 } then {
812 fail
"$test1 preserving OS/ABI"
813 set osabi_fail
"true"
815 verbose
-log "grep OS/ABI tmpdir/osabi.in"
816 catch
"exec grep OS/ABI tmpdir/osabi.in" osabi_in
820 set exec_output
[binutils_run $prog
"$flags ${copyfile}"]
821 if ![string equal
"" $exec_output] {
823 if [string equal
"" $test2] {
830 if [is_remote host
] {
831 remote_upload host $
{copyfile
} tmpdir
/striprog
834 if { $osabi_fail
!= "true" && [is_elf_format] } {
835 verbose
-log "$READELF -a ${copyfile} > tmpdir/osabi.out"
836 set exec_output
[remote_exec host
"$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
837 if { [lindex $exec_output
0] != 0 } then {
838 fail
"$test1 preserving OS/ABI"
840 verbose
-log "grep OS/ABI tmpdir/osabi.out"
841 catch
"exec grep OS/ABI tmpdir/osabi.out" osabi_out
842 if { "$osabi_in" == "$osabi_out" } {
843 pass
"$test1 preserving OS/ABI"
845 fail
"$test1 preserving OS/ABI"
850 set exec_output
[binutils_run $NM
"$NMFLAGS ${copyfile}"]
851 if ![string match
"*: no symbols*" $exec_output] {
856 if [string equal
"" $test2] {
860 set result
[remote_load target tmpdir
/striprog
]
861 set status [lindex $result
0]
862 if { $
status != "pass" } {
870 # Test stripping an executable with saving a symbol
872 proc strip_executable_with_saving_a_symbol
{ prog flags test1 test2
} {
876 remote_file build
delete tmpdir
/striprog
877 remote_download build tmpdir
/copyprog tmpdir
/striprog
878 if [is_remote host
] {
879 set copyfile
[remote_download host tmpdir
/striprog
]
881 set copyfile tmpdir
/striprog
884 set exec_output
[binutils_run $prog
"$flags ${copyfile}"]
885 if ![string equal
"" $exec_output] {
887 if [string equal
"" $test2] {
894 set exec_output
[binutils_run $NM
"$NMFLAGS ${copyfile}"]
895 if { [istarget mmix
-knuth
-mmixware
] } {
896 # Whenever there
's a symbol in the mmo format, there's the symbol
897 # Main
, so remove it manually from the expected output
for sake of
900 # Using
"" not {} to get the \n and \r translated.
901 regsub
"^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
904 if {![regexp
{^
([0-9a
-fA
-F
]+)?
[ ]+[TtDd
] _?main
} $exec_output
]} {
909 if [string equal
"" $test2] {
913 if [is_remote host
] {
914 remote_upload host $
{copyfile
} tmpdir
/striprog
917 set result
[remote_load target tmpdir
/striprog
]
918 set status [lindex $result
0]
919 if { $
status != "pass" } {
927 # Test keeping only debug symbols of an executable
929 proc keep_debug_symbols_and_test_copy
{ prog1 flags1 test1 prog2 flags2 test2
} {
930 remote_file build
delete tmpdir
/striprog
931 remote_download build tmpdir
/copyprog tmpdir
/striprog
932 if [is_remote host
] {
933 set copyfile
[remote_download host tmpdir
/striprog
]
935 set copyfile tmpdir
/striprog
938 set exec_output
[binutils_run $prog1
"$flags1 ${copyfile}"]
939 if ![string equal
"" $exec_output] {
945 set exec_output
[binutils_run $prog2
"$flags2 ${copyfile}"]
946 if ![string equal
"" $exec_output] {
953 # Tests that in a debug only copy of a file the sections
954 # headers whose types have been changed to NOBITS still
955 # retain their sh_link fields.
957 proc keep_debug_symbols_and_check_links
{ prog flags test
} {
960 remote_file build
delete tmpdir
/striprog
961 remote_download build tmpdir
/copyprog tmpdir
/striprog
962 if [is_remote host
] {
963 set copyfile
[remote_download host tmpdir
/striprog
]
965 set copyfile tmpdir
/striprog
968 set exec_output
[binutils_run $prog
"$flags ${copyfile}"]
969 if ![string equal
"" $exec_output] {
974 set got
[binutils_run $READELF
"-S --wide ${copyfile}"]
977 # Regexp to match a section with NOBITS type and extract its
name and sh_link fields
979 {[^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]+)(.
*)} \
980 $got all
name link rest
]} {
983 # Only some NOBITS sections should have a non
-zero sh_link field.
984 # Look
for them by
name.
985 verbose
"NOBITS section .$name has a 0 sh_link field\n"
987 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
988 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
989 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
990 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
991 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
992 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
993 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
994 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
1007 #
For ELF relocatables
, test that
--only
-keep
-debug
1008 # marks non
-debug output sections as NOBITS.
1010 proc keep_debug_symbols_for_elf_relocatable
{ prog flags test
} {
1015 if { [target_compile $srcdir
/$subdir
/testprog.c tmpdir
/testprog.o object debug
] != "" } {
1020 if [is_remote host
] {
1021 set relocatable
[remote_download host tmpdir
/testprog.o
]
1023 set relocatable tmpdir
/testprog.o
1026 set non_debug_sections
{}
1027 set got
[binutils_run $READELF
"-S --wide ${relocatable}"]
1029 {[^a
-zA
-Z
]+([a
-zA
-Z0
-9_\.
]+)[ \t]+([A
-Z
]+)[ \t]+[0-9a
-f
]+ [0-9a
-f
]+ [0-9a
-f
]+ [0-9]+[ \t]+([A
-Z
]*)[ \t]+[0-9]+(.
*)} \
1030 $got all
name type flag rest
] } {
1031 if { $type
!= "NOTE" && [regexp {[AG]} $flag] } {
1032 # PR
30699: Some debug sections can be in a group
, so
1033 # exclude sections whose
name includes
"debug_"
1034 if { ! [regexp
{debug_
} $
name] } {
1035 lappend non_debug_sections $
name
1041 set exec_output
[binutils_run $prog
"$flags ${relocatable}"]
1042 if ![string equal
"" $exec_output] {
1048 set got
[binutils_run $READELF
"-S --wide ${relocatable}"]
1050 {[^a
-zA
-Z
]+([a
-zA
-Z0
-9_\.
]+)[ \t]+([A
-Z
]+)[ \t]+[0-9a
-f
]+ [0-9a
-f
]+ [0-9a
-f
]+ [0-9]+[ \t]+[A
-Z
]*[ \t]+[0-9]+(.
*)} \
1051 $got all
name type rest
] } {
1052 if { [lsearch
-exact $non_debug_sections $
name] >= 0 && $type
!= "NOBITS" } {
1054 send_log
"Expected SHT_NOBITS type for .$name\n"
1058 if { $fails
== 1 } {
1066 set test1
"simple objcopy of executable"
1067 set test1r
"run objcopy of executable"
1068 set test2
"strip executable"
1069 set test2r
"run stripped executable"
1070 set test3
"strip executable with saving a symbol"
1071 set test3r
"run stripped executable with saving a symbol"
1072 set test4
"keep only debug data"
1073 set test5
"simple objcopy of debug data"
1074 if [is_elf_format
] {
1075 set test6
"NOBITS sections retain sh_link field"
1076 set test7
"--only-keep-debug for ELF relocatables"
1079 switch [copy_setup
] {
1092 if [is_elf_format
] {
1098 copy_executable
"$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
1100 strip_executable
"$STRIP" "$STRIPFLAGS" "$test2" ""
1102 strip_executable_with_saving_a_symbol
"$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
1104 keep_debug_symbols_and_test_copy
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1105 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1106 if [is_elf_format
] {
1107 keep_debug_symbols_and_check_links
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1108 keep_debug_symbols_for_elf_relocatable
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test7"
1112 copy_executable
"$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
1113 strip_executable
"$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
1114 strip_executable_with_saving_a_symbol
"$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
1115 keep_debug_symbols_and_test_copy
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
1116 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
1117 if [is_elf_format
] {
1118 keep_debug_symbols_and_check_links
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
1119 keep_debug_symbols_for_elf_relocatable
"$STRIP" "--only-keep-debug $STRIPFLAGS" "$test7"
1124 proc objcopy_test_readelf
{testname srcfile
} {
1131 if {![binutils_assemble $srcdir
/$subdir
/$
{srcfile
} tmpdir
/bintest.o
]} then {
1132 unsupported
"objcopy ($testname)"
1136 verbose
-log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
1137 set exec_output
[remote_exec host
"$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
1138 if { [lindex $exec_output
0] != 0
1139 ||
![string equal
"" [lindex $exec_output 1]] } then {
1140 fail
"objcopy ($testname)"
1144 verbose
-log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
1145 set exec_output
[remote_exec host
"$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
1146 if { [lindex $exec_output
0] != 0 } then {
1147 fail
"objcopy ($testname)"
1150 set exec_output
[prune_warnings
[lindex $exec_output
1]]
1151 if ![string equal
"" $exec_output] then {
1152 unresolved
"objcopy ($testname)"
1156 verbose
-log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
1157 set exec_output
[remote_exec host
"$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
1158 if { [lindex $exec_output
0] != 0 } then {
1159 fail
"objcopy ($testname)"
1162 set exec_output
[prune_warnings
[lindex $exec_output
1]]
1163 if ![string equal
"" $exec_output] then {
1164 unresolved
"objcopy ($testname)"
1168 verbose
-log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
1169 catch
"exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
1170 set exec_output
[prune_warnings $exec_output
]
1172 if [string equal
"" $exec_output] then {
1173 pass
"objcopy ($testname)"
1175 fail
"objcopy ($testname)"
1179 proc objcopy_test_symbol_manipulation
{} {
1183 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/symbols
-*.d
]]
1184 foreach t $test_list
{
1185 # We need to
strip the
".d", but can leave the dirname.
1186 verbose
[file rootname $t
]
1187 run_dump_test
[file rootname $t
]
1190 if { [supports_gnu_unique
] } {
1191 run_dump_test
"weaken-gnu-unique"
1195 proc objcopy_test_elf_common_symbols
{} {
1199 # hpux has a non
-standard common directive.
1200 if { [istarget
"*-*-hpux*"] } then {
1204 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/common
-*.d
]]
1205 foreach t $test_list
{
1206 # We need to
strip the
".d", but can leave the dirname.
1207 verbose
[file rootname $t
]
1208 run_dump_test
[file rootname $t
]
1212 # ia64 specific tests
1213 if { ([istarget
"ia64-*-elf*"]
1214 ||
[istarget
"ia64-*-linux*"]) } {
1215 objcopy_test
"ia64 link order" link-order.s object "" ""
1218 # ELF specific tests
1220 if [is_elf_format
] {
1221 objcopy_test_symbol_manipulation
1222 objcopy_test_elf_common_symbols
1223 objcopy_test
"ELF unknown section type" unknown.s object "" ""
1224 objcopy_test_readelf
"ELF group 1" group.s
1225 objcopy_test_readelf
"ELF group 2" group-2.s
1226 objcopy_test_readelf
"ELF group 3" group-3.s
1227 objcopy_test_readelf
"ELF group 4" group-4.s
1228 objcopy_test_readelf
"GNU_MBIND section" mbind1.s
1229 run_dump_test
"group-5"
1230 run_dump_test
"group-6"
1231 run_dump_test
"group-7a"
1232 run_dump_test
"group-7b"
1233 run_dump_test
"group-7c"
1234 run_dump_test
"copy-1"
1235 run_dump_test
"note-1"
1236 # Use copytest.o from the note
-1 test to determine ELF32 or ELF64
1237 if [is_elf64 tmpdir
/copytest.o
] {
1238 set elf64
"--defsym ELF64=1"
1239 run_dump_test
"note-2-64"
1240 run_dump_test
"note-3-64"
1241 run_dump_test
"note-4-64"
1242 run_dump_test
"note-6-64"
1244 run_dump_test
"note-2-32"
1245 run_dump_test
"note-3-32"
1246 run_dump_test
"note-4-32"
1247 run_dump_test
"note-6-32"
1249 run_dump_test
"note-5"
1252 run_dump_test
"copy-2"
1253 run_dump_test
"copy-3"
1254 run_dump_test
"copy-4"
1255 run_dump_test
"copy-5"
1256 run_dump_test
"copy-6"
1258 # Use bintest.o from the copy
-4 test to determine ELF reloc type
1259 set reloc_format rel
1260 if { [is_elf_format
] && [is_rela tmpdir
/bintest.o
] } {
1261 set reloc_format rela
1263 run_dump_test
"pr19020a"
1264 run_dump_test
"pr19020b"
1266 if [is_elf_format
] {
1267 run_dump_test
"strip-1"
1268 run_dump_test
"strip-2"
1269 run_dump_test
"strip-3"
1270 run_dump_test
"strip-4"
1271 run_dump_test
"strip-5"
1272 run_dump_test
"strip-6"
1273 run_dump_test
"strip-7"
1274 run_dump_test
"strip-8"
1275 run_dump_test
"strip-9"
1276 run_dump_test
"strip-12"
1278 if { [istarget
"mips64*-*-openbsd*"] \
1279 ||
[istarget
"mips64*-*-*-gnuabi64"] } {
1280 set reloc_format mips64
1282 # A relocation type not supported by
any target
1283 if { [istarget
"nds32*-*"] } {
1288 run_dump_test
"strip-13" [list \
1289 [list source
strip-13$
{reloc_format
}.s
] \
1290 [list as
"${elf64} --defsym RELOC=${reloc}"]]
1291 # Select a relocation number that corresponds to one actually
1292 # supported by the target and ABI being tested.
1293 if { [istarget
"aarch64*-*"] } {
1295 } elseif
{ [istarget
"ia64*-*"] \
1296 ||
[istarget
"m32r*-*"] \
1297 ||
[istarget
"nds32*-*"] \
1298 ||
[istarget
"v850*-*"] } {
1300 } elseif
{ [istarget
"pru-*"] } {
1305 run_dump_test
"strip-14" [list \
1306 [list source
strip-14$
{reloc_format
}.s
] \
1307 [list as
"${elf64} --defsym RELOC=${reloc}"]]
1308 run_dump_test
"strip-15" [list \
1309 [list source
strip-15$
{reloc_format
}.s
] \
1310 [list as
"${elf64} --defsym RELOC=${reloc}"]]
1312 # This requires STB_GNU_UNIQUE support with OSABI
set to GNU.
1313 if { [supports_gnu_unique
] } {
1314 run_dump_test
"strip-10"
1316 run_dump_test
"strip-11"
1318 if { [istarget
"i*86-*"] || [istarget "x86_64-*-*"] } {
1319 # Check to make sure we don
't strip a symbol named in relocations.
1320 set test "objcopy keeps symbols needed by relocs"
1322 set srcfile $srcdir/$subdir/needed-by-reloc.s
1324 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1327 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
1329 if [regexp "not stripping symbol `foo' because it is named in a relocation
" $got] {
1337 # The symbol table
for some MIPS targets is sorted differently than
1338 # the ELF canonical order
, so the regexps in localize
-hidden
-1.d fail
1340 if { [is_bad_symtab
] } then {
1343 run_dump_test
"localize-hidden-1"
1344 run_dump_test
"testranges"
1345 run_dump_test
"testranges-ia64"
1347 run_dump_test
"add-section"
1348 run_dump_test
"add-symbol"
1349 run_dump_test
"add-empty-section"
1351 run_dump_test
"exclude-1a"
1352 run_dump_test
"exclude-1b"
1354 run_dump_test
"only-section-01"
1355 run_dump_test
"remove-section-01"
1357 run_dump_test
"keep-section-1"
1358 run_dump_test
"keep-section-2"
1360 # Test the remove relocation functionality
1361 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/remove
-relocs
-*.d
]]
1362 foreach t $test_list
{
1363 # We need to
strip the
".d", but can leave the dirname.
1364 verbose
[file rootname $t
]
1365 run_dump_test
[file rootname $t
]
1368 # Test
--strip-section
-headers
1369 run_dump_test
"strip-section-headers-1" [list \
1370 [list source
strip-15$
{reloc_format
}.s
] \
1371 [list as
"${elf64} --defsym RELOC=${reloc}"]]
1373 run_dump_test
"localize-hidden-2"
1375 # Test objcopying an object file without global symbol
1377 proc objcopy_test_without_global_symbol
{ } {
1385 set test
"strip without global symbol "
1387 if { [target_compile $srcdir
/$subdir
/pr19547.c tmpdir
/pr19547.o object debug
] != "" } {
1392 if [is_remote host
] {
1393 set objfile
[remote_download host tmpdir
/pr19547.o
]
1395 set objfile tmpdir
/pr19547.o
1398 set exec_output
[binutils_run $OBJCOPY
"$OBJCOPYFLAGS --strip-unneeded $objfile"]
1399 set exec_output
[prune_warnings $exec_output
]
1400 if ![string equal
"" $exec_output] {
1405 set exec_output
[binutils_run $OBJDUMP
"$OBJDUMPFLAGS -t $objfile"]
1406 set exec_output
[prune_warnings $exec_output
]
1407 if {![regexp
"no symbols" $exec_output]} {
1415 # The AArch64 and ARM targets preserve mapping symbols
1416 # in object files
, so they will fail this test.
1417 setup_xfail aarch64
*-*-* arm
*-*-*
1419 objcopy_test_without_global_symbol
1421 # objcopy remove relocation from executable test
1423 proc objcopy_remove_relocations_from_executable
{ } {
1429 set test
"remove-section relocation sections"
1431 if { [target_compile $srcdir
/$subdir
/testprog.c tmpdir
/pr23611 executable debug
] != "" } {
1436 if [is_remote host
] {
1437 set objfile
[remote_download host tmpdir
/pr23611
]
1439 set objfile tmpdir
/pr23611
1441 set out tmpdir
/pr23611.out
1443 set exec_output1
[binutils_run $OBJCOPY
"-R .rela.plt -R .rela.dyn -R .rel.plt -R .rel.dyn $objfile $out"]
1445 set exec_output1
[prune_warnings $exec_output1
]
1446 if ![string equal
"" $exec_output1] {
1451 set exec_output2
[binutils_run $READELF
"-S $out"]
1452 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] } {
1459 if [is_elf_format
] {
1460 objcopy_remove_relocations_from_executable
1463 run_dump_test
"pr23633"
1465 run_dump_test
"set-section-alignment"
1466 run_dump_test
"section-alignment"
1468 setup_xfail
"hppa*-*-*"
1469 setup_xfail
"spu-*-*"
1470 clear_xfail
"hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
1471 clear_xfail
"hppa*-*-*n*bsd*" "hppa*-*-*elf*"
1472 if { [istarget pdp11
-*-*] } {
1473 set src
"pr25662-pdp11.s"
1478 set ldflags
"-T$srcdir/$subdir/pr25662.ld"
1479 if { [istarget
*-*-cygwin
] ||
[istarget
*-*-mingw
*] } {
1480 append ldflags
" --disable-reloc-section"
1483 #xcoff doesn
't support arbitrary sections
1484 if { ![is_xcoff_format] } {
1485 objcopy_test "pr25662" $src executable "" $ldflags
1488 run_dump_test "rename-section-01"