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