Clear HAS_RELOC if there are no relocations
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / objcopy.exp
CommitLineData
6f2750fe 1# Copyright (C) 1994-2016 Free Software Foundation, Inc.
252b5132
RH
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
32866df7 5# the Free Software Foundation; either version 3 of the License, or
252b5132 6# (at your option) any later version.
21c40443 7#
252b5132
RH
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.
21c40443 12#
252b5132
RH
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
b43b5d5f 15# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
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
22if ![is_remote host] {
23 if {[which $OBJCOPY] == 0} then {
24 perror "$OBJCOPY does not exist"
25 return
26 }
27}
28
29send_user "Version [binutil_version $OBJCOPY]"
30
252b5132 31if ![is_remote host] {
8d263650
BE
32 set tempfile tmpdir/bintest.o
33 set copyfile tmpdir/copy
252b5132
RH
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
64bb95af
L
41proc objcopy_test {testname srcfile} {
42 global OBJCOPY
43 global OBJCOPYFLAGS
44 global srcdir
45 global subdir
46 global tempfile
47 global copyfile
252b5132 48
64bb95af
L
49 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
50 perror "unresolved $testname"
51 unresolved "objcopy ($testname)"
52 return
252b5132 53 }
252b5132 54
64bb95af
L
55 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
56
21c40443 57 if ![string equal "" $got] then {
64bb95af 58 fail "objcopy ($testname)"
252b5132 59 } else {
64bb95af
L
60 send_log "cmp $tempfile ${copyfile}.o\n"
61 verbose "cmp $tempfile ${copyfile}.o"
62 if [is_remote host] {
63 set src1 tmpdir/bintest.o
64 set src2 tmpdir/copy.o
65 remote_upload host $tempfile $src1
66 remote_upload host ${copyfile}.o $src2
67 } else {
68 set src1 ${tempfile}
69 set src2 ${copyfile}.o
70 }
71 set status [remote_exec build cmp "${src1} ${src2}"]
72 set exec_output [lindex $status 1]
73 set exec_output [prune_warnings $exec_output]
74
75 # On some systems the result of objcopy will not be identical.
76 # Usually this is just because gas isn't using bfd to write the
77 # files in the first place, and may order things a little
78 # differently. Those systems should use setup_xfail here.
79
4cd28456 80 setup_xfail "h8300-*-coff"
64bb95af
L
81 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
82 setup_xfail "hppa*-*-*"
83 setup_xfail "i960-*"
84 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
85 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
86 setup_xfail "m8*-*"
b0a0c50a
AM
87 setup_xfail "sh-*-coff*"
88 setup_xfail "tic80-*-*" "w65-*"
64bb95af
L
89
90 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
91 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
92 clear_xfail "m68*-*-sysv4*"
93
21c40443 94 if [string equal "" $exec_output] then {
64bb95af
L
95 pass "objcopy ($testname)"
96 } else {
97 send_log "$exec_output\n"
98 verbose "$exec_output" 1
252b5132 99
64bb95af
L
100 # On OSF/1, this succeeds with gas and fails with /bin/as.
101 setup_xfail "alpha*-*-osf*"
252b5132 102
64bb95af
L
103 fail "objcopy ($testname)"
104 }
252b5132
RH
105 }
106}
107
64bb95af
L
108objcopy_test "simple copy" bintest.s
109
9e48b4c6
NC
110# Test reversing bytes in a section.
111
112set reversed ${tempfile}-reversed
113set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
114
21c40443 115if ![string equal "" $got] then {
9e48b4c6
NC
116 fail "objcopy --reverse-bytes"
117} else {
118 if [is_remote host] {
119 remote_upload host ${reversed} tmpdir/copy-reversed.o
120 set reversed tmpdir/copy-reversed.o
121 }
122
123 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
124 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
125
126 set want "^ \[0-9\]+ (\[0-9\]+)"
127 set found_orig [regexp -lineanchor $want $origdata -> origdata]
128 set found_rev [regexp -lineanchor $want $revdata -> revdata]
129
130 if {$found_orig == 0 || $found_rev == 0} then {
8941d5e8
DA
131 # som doesn't have a .data section
132 setup_xfail "hppa*-*-hpux*"
133 clear_xfail "hppa*64*-*-hpux*"
134
9e48b4c6
NC
135 fail "objcopy --reverse-bytes"
136 } else {
137 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
138 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
139
140 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
141 pass "objcopy --reverse-bytes"
142 } else {
143 fail "objcopy --reverse-bytes"
144 }
145 }
146}
147
b7dd81f7
NC
148# Test interleaved copy of multiple byte width
149
150set sequence_file sequence_file
151set file [open ${sequence_file} w]
152puts ${file} "12345678"
153close ${file}
154
155if [is_remote host] {
156 remote_upload host ${sequence_file} tmpdir/sequence_file
157 set sequence_file tmpdir/sequence_file
158}
159
160set got [binutils_run $OBJCOPY "-I binary -i 4 -b 0 --interleave-width 2 ${sequence_file} ${copyfile}"]
161
21c40443 162if ![string equal "" $got] then {
b7dd81f7
NC
163 fail "objcopy -i --interleave-width"
164} else {
165 if [is_remote host] {
166 remote_upload host ${copyfile} tmpdir/interleave_output
167 set interleave_output tmpdir/interleave_output
168 } else {
169 set interleave_output ${copyfile}
170 }
171
172 set file [open ${interleave_output} r]
173 gets $file line
174 send_log "$line\n"
175 verbose $line
176
21c40443 177 if ![string equal "1256" $line] then {
b7dd81f7
NC
178 fail "objcopy -i --interleave-width"
179 }
180 pass "objcopy -i --interleave-width"
181
182 close $file
183}
184
252b5132
RH
185# Test generating S records.
186
187# We make the srec filename 8.3 compatible. Note that the header string
188# matched against depends on the name of the file. Ugh.
189
190if [is_remote host] {
191 set srecfile copy.sre
192 set header_string S00B0000636F70792E737265C1
193} else {
194 set srecfile ${copyfile}.srec
195 set header_string S0130000746D706469722F636F70792E7372656397
196}
197
198set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
199
21c40443 200if ![string equal "" $got] then {
252b5132
RH
201 fail "objcopy -O srec"
202} else {
203 if [is_remote host] {
8d263650
BE
204 remote_upload host ${srecfile} tmpdir/copy.srec
205 set srecfile tmpdir/copy.srec
252b5132
RH
206 }
207 set file [open ${srecfile} r]
208
209 # The first S record is fixed by the file name we are using.
210 gets $file line
211 send_log "$line\n"
212 verbose $line
213 if ![regexp "$header_string.*" $line] {
214 send_log "bad header\n"
215 fail "objcopy -O srec"
216 } else {
217 while {[gets $file line] != -1 \
218 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
219 send_log "$line\n"
220 verbose $line
221 set line "**EOF**"
222 }
223 send_log "$line\n"
224 verbose $line
225 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
226 send_log "bad trailer\n"
227 fail "objcopy -O srec"
228 } else {
229 if {[gets $file line] != -1} then {
230 send_log "garbage at end\n"
231 send_log "$line\n"
232 verbose $line
233 fail "objcopy -O srec"
234 } else {
7f6a71ff 235 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
252b5132
RH
236 if ![regexp "file format srec" $got] then {
237 send_log "objdump failed\n"
238 fail "objcopy -O srec"
239 } else {
240 pass "objcopy -O srec"
241 }
242 }
243 }
244 }
245
246 close $file
247}
248
249# Test setting and adjusting the start address. We only test this
250# while generating S records, because we may not be able to set the
251# start address for other object file formats, and the S record case
252# is the only useful one anyhow.
253
254set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
255if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
256 perror "objdump can not recognize bintest.o"
257 set origstart ""
258} else {
627fe3fb 259 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
21c40443 260 if ![string equal "" $got] then {
252b5132
RH
261 fail "objcopy --set-start"
262 } else {
263 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
264 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
265 fail "objcopy --set-start"
266 } else {
267 if {$srecstart != 0x7654} then {
268 send_log "$srecstart != 0x7654\n"
269 fail "objcopy --set-start"
270 } else {
271 pass "objcopy --set-start"
272 }
273 }
274 }
275
627fe3fb 276 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
21c40443 277 if ![string equal "" $got] then {
252b5132
RH
278 fail "objcopy --adjust-start"
279 } else {
280 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
281 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
282 fail "objcopy --adjust-start"
283 } else {
284 if {$srecstart != $origstart + 0x123} then {
285 send_log "$srecstart != $origstart + 0x123\n"
286 fail "objcopy --adjust-start"
287 } else {
288 pass "objcopy --adjust-start"
289 }
290 }
291 }
292}
293
294# Test adjusting the overall VMA, and adjusting the VMA of a
295# particular section. We again only test this when generating S
296# records.
297
298set low ""
299set lowname ""
300
301set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
302
303set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
304
305set got $headers
306while {[regexp $headers_regexp $got all name size vma rest]} {
307 set vma 0x$vma
308 set size 0x$size
309 if {$size != 0} {
310 if {$low == "" || $vma < $low} {
311 set low $vma
312 set lowname $name
313 }
314 }
315 set got $rest
316}
317
318if {$low == "" || $origstart == ""} then {
319 perror "objdump can not recognize bintest.o"
320} else {
627fe3fb 321 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
21c40443 322 if ![string equal "" $got] then {
252b5132
RH
323 fail "objcopy --adjust-vma"
324 } else {
325 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
326 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
327 if ![regexp $want $got all start vma] then {
328 fail "objcopy --adjust-vma"
329 } else {
330 set vma 0x$vma
331 if {$vma != $low + 0x123} then {
332 send_log "$vma != $low + 0x123\n"
333 fail "objcopy --adjust-vma"
334 } else {
335 if {$start != $origstart + 0x123} then {
336 send_log "$start != $origstart + 0x123\n"
337 fail "objcopy --adjust-vma"
338 } else {
339 pass "objcopy --adjust-vma"
340 }
341 }
342 }
343 }
344
345 set arg ""
346 set got $headers
347 while {[regexp $headers_regexp $got all name size vma rest]} {
348 set vma 0x$vma
349 if {$vma == $low} then {
350 set arg "$arg --adjust-section-vma $name+4"
351 }
352 set got $rest
353 }
354
627fe3fb 355 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
21c40443 356 if ![string equal "" $got] then {
252b5132
RH
357 fail "objcopy --adjust-section-vma +"
358 } else {
359 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
360 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
361 if ![regexp $want $got all vma] then {
362 fail "objcopy --adjust-section-vma +"
363 } else {
364 set vma 0x$vma
365 if {$vma != $low + 4} then {
366 send_log "$vma != $low + 4\n"
367 fail "objcopy --adjust-section-vma +"
368 } else {
369 pass "objcopy --adjust-section-vma +"
370 }
371 }
372 }
373
374 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
627fe3fb 375 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
21c40443 376 if ![string equal "" $got] then {
252b5132
RH
377 fail "objcopy --adjust-section-vma ="
378 } else {
379 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
380 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
381 if ![regexp $want $got all vma] then {
382 fail "objcopy --adjust-section-vma ="
383 } else {
384 set vma 0x$vma
385 if {$vma != $low + 4} then {
386 send_log "$vma != $low + 4\n"
387 fail "objcopy --adjust-section-vma ="
388 } else {
389 pass "objcopy --adjust-section-vma ="
390 }
391 }
392 }
393}
394
395# Test stripping an object.
396
397proc strip_test { } {
1983fbac 398 global AR
252b5132
RH
399 global CC
400 global STRIP
401 global STRIPFLAGS
402 global NM
403 global NMFLAGS
404 global srcdir
405 global subdir
28902555 406 global READELF
252b5132
RH
407
408 set test "strip"
409
410 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
411 untested $test
412 return
413 }
414
28902555
BS
415 set osabi_fail "false"
416 if [is_elf_format] {
417 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.in"
418 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.in"]
419 if { [lindex $exec_output 0] != 0 } then {
21c40443 420 unresolved "$test preserving OS/ABI"
28902555
BS
421 set osabi_fail "true"
422 } else {
423 verbose -log "grep OS/ABI tmpdir/osabi.in"
424 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
425 }
426 }
427
252b5132 428 if [is_remote host] {
1983fbac 429 set archive libstrip.a
8d263650 430 set objfile [remote_download host tmpdir/testprog.o]
1983fbac
L
431 remote_file host delete $archive
432 } else {
433 set archive tmpdir/libstrip.a
434 set objfile tmpdir/testprog.o
435 }
436
437 remote_file build delete tmpdir/libstrip.a
438
439 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
21c40443 440 if ![string equal "" $exec_output] {
1983fbac 441 fail $test
21c40443 442 unresolved "$test preserving OS/ABI"
1983fbac
L
443 return
444 }
445
8648f88f 446 set exec_output [binutils_run $STRIP "-g $archive"]
21c40443 447 if ![string equal "" $exec_output] {
8648f88f 448 fail $test
21c40443 449 unresolved "$test preserving OS/ABI"
8648f88f
L
450 return
451 }
452
1983fbac 453 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
21c40443 454 if ![string equal "" $exec_output] {
1983fbac 455 fail $test
21c40443 456 unresolved "$test preserving OS/ABI"
1983fbac
L
457 return
458 }
459
28902555
BS
460 if { $osabi_fail != "true" && [is_elf_format] } {
461 verbose -log "$READELF -a tmpdir/testprog.o > tmpdir/osabi.out"
462 set exec_output [remote_exec host "$READELF -h tmpdir/testprog.o" "" "/dev/null" "tmpdir/osabi.out"]
463 if { [lindex $exec_output 0] != 0 } then {
21c40443 464 unresolved "$test preserving OS/ABI"
28902555
BS
465 } else {
466 verbose -log "grep OS/ABI tmpdir/osabi.out"
467 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
468 if { "$osabi_in" == "$osabi_out" } {
469 pass "$test preserving OS/ABI"
470 } else {
471 fail "$test preserving OS/ABI"
472 }
473 }
474 }
475
1983fbac
L
476 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
477 untested $test
478 return
479 }
480
481 if [is_remote host] {
8d263650 482 set objfile [remote_download host tmpdir/testprog.o]
252b5132
RH
483 } else {
484 set objfile tmpdir/testprog.o
485 }
486
487 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
21c40443 488 if ![string equal "" $exec_output] {
252b5132
RH
489 fail $test
490 return
491 }
492
493 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
68a4c073 494 if ![string match "*: no symbols*" $exec_output] {
252b5132
RH
495 fail $test
496 return
497 }
498
499 pass $test
500}
501
502strip_test
503
504# Test stripping an object file with saving a symbol
505
506proc strip_test_with_saving_a_symbol { } {
507 global CC
508 global STRIP
509 global STRIPFLAGS
510 global NM
511 global NMFLAGS
512 global srcdir
513 global subdir
514
515 set test "strip with saving a symbol"
516
517 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
518 untested $test
519 return
520 }
521
522 if [is_remote host] {
8d263650 523 set objfile [remote_download host tmpdir/testprog.o]
252b5132
RH
524 } else {
525 set objfile tmpdir/testprog.o
526 }
527
528 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
21c40443 529 if ![string equal "" $exec_output] {
252b5132
RH
530 fail $test
531 return
532 }
533
534 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
195fe636 535 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
252b5132
RH
536 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
537 fail $test
538 return
539 }
540
541 pass $test
542}
543
544strip_test_with_saving_a_symbol
545
546# Build a final executable.
547
99ad8390 548if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
f8a7038a
NC
549 set test_prog "testprog.exe"
550} else {
551 set test_prog "testprog"
552}
553
252b5132
RH
554proc copy_setup { } {
555 global srcdir
556 global subdir
9a580d9a 557 global gcc_gas_flag
f8a7038a 558 global test_prog
72d4c02f 559 global host_triplet
21c40443 560
8d263650
BE
561 set res [build_wrapper testglue.o]
562 set flags { debug }
21c40443 563
28902555 564 if { [istarget *-*-uclinux*] && ![istarget tic6x-*-*] } {
903b1f5b
NS
565 return 1
566 }
21c40443 567
252b5132 568 if { $res != "" } {
8d263650
BE
569 lappend flags "additional_flags=[lindex $res 1]"
570 set add_libs "testglue.o"
252b5132 571 } else {
8d263650 572 set add_libs ""
252b5132
RH
573 }
574
5940a93c
TS
575 if { [istarget *-*-linux*]
576 || [istarget *-*-gnu*] } {
9a580d9a
L
577 foreach i $gcc_gas_flag {
578 set flags "additional_flags=$i $flags"
579 }
580 }
f8a7038a 581 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
252b5132
RH
582 return 2
583 }
584
8d263650
BE
585 set result [remote_load target tmpdir/$test_prog]
586 set status [lindex $result 0]
252b5132
RH
587
588 if { $status != "pass" } {
72d4c02f 589 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
21c40443 590 return 3
252b5132
RH
591 }
592
593 return 0
594}
595
596# Test copying an executable.
597
598proc copy_executable { prog flags test1 test2 } {
f8a7038a 599 global test_prog
252b5132
RH
600
601 if [is_remote host] {
8d263650 602 set testfile [remote_download host tmpdir/$test_prog]
252b5132
RH
603 set testcopy copyprog
604 } else {
f8a7038a 605 set testfile tmpdir/$test_prog
252b5132
RH
606 set testcopy tmpdir/copyprog
607 }
8d263650 608 remote_file host delete $testcopy
252b5132
RH
609
610 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
611
21c40443 612 if ![string equal "" $exec_output] {
252b5132 613 fail $test1
21c40443 614 if [string equal "" $test2] {
0d063f63
AM
615 return
616 }
252b5132
RH
617 fail $test2
618 return
619 }
620
621 if [is_remote host] {
622 remote_upload host $testcopy tmpdir/copyprog
623 }
624
f8a7038a 625 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
8d263650 626 set exec_output [lindex $status 1]
252b5132 627
21c40443 628 if [string equal "" $exec_output] then {
252b5132
RH
629 pass $test1
630 } else {
631 send_log "$exec_output\n"
632 verbose "$exec_output"
633
634 # This will fail for many reasons. For example, it will most
635 # likely fail if a non-GNU linker is used. Therefore, we do
636 # not insist that it pass. If you are using an assembler and
637 # linker based on the same BFD as objcopy, it is worth
638 # investigating to see why this failure occurs. If we are
639 # cross compiling, we assume that a GNU linker is being used,
640 # and expect it to succeed.
641 if {[isnative]} then {
642 setup_xfail "*-*-*"
643 }
644
645 # This also fails for mips*-*-elf targets. See elf32-mips.c
646 # mips_elf_sym_is_global.
647 setup_xfail "mips*-*-elf"
648
8c5fc800 649 setup_xfail "arm*-*-coff"
11d47adc 650 setup_xfail "arm*-*-pe"
88f14853 651 setup_xfail "*-*-mingw*"
899396fe 652 setup_xfail "*-*-cygwin*"
21c40443 653
252b5132
RH
654 fail $test1
655 }
656
21c40443 657 if [string equal "" $test2] {
0d063f63
AM
658 return
659 }
660
252b5132 661 set output [remote_load target tmpdir/copyprog]
8d263650 662 set status [lindex $output 0]
252b5132
RH
663 if { $status != "pass" } {
664 fail $test2
665 } else {
666 pass $test2
667 }
668}
669
670# Test stripping an executable
671
21c40443 672proc strip_executable { prog flags test1 test2 } {
252b5132
RH
673 global NM
674 global NMFLAGS
28902555 675 global READELF
252b5132 676
0fcc17f8 677 remote_file build delete tmpdir/striprog
252b5132
RH
678 remote_download build tmpdir/copyprog tmpdir/striprog
679 if [is_remote host] {
8d263650 680 set copyfile [remote_download host tmpdir/striprog]
252b5132
RH
681 } else {
682 set copyfile tmpdir/striprog
683 }
684
28902555
BS
685 set osabi_fail "false"
686 if [is_elf_format] {
687 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.in"
688 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.in"]
689 if { [lindex $exec_output 0] != 0 } then {
21c40443 690 unresolved "$test1 preserving OS/ABI"
28902555
BS
691 set osabi_fail "true"
692 } else {
693 verbose -log "grep OS/ABI tmpdir/osabi.in"
694 catch "exec grep OS/ABI tmpdir/osabi.in" osabi_in
695 }
696 }
697
252b5132 698 set exec_output [binutils_run $prog "$flags ${copyfile}"]
21c40443
AM
699 if ![string equal "" $exec_output] {
700 fail $test1
701 if [string equal "" $test2] {
702 return
703 }
704 fail $test2
252b5132
RH
705 return
706 }
707
708 if [is_remote host] {
8d263650 709 remote_upload host ${copyfile} tmpdir/striprog
252b5132
RH
710 }
711
28902555
BS
712 if { $osabi_fail != "true" && [is_elf_format] } {
713 verbose -log "$READELF -a ${copyfile} > tmpdir/osabi.out"
714 set exec_output [remote_exec host "$READELF -h ${copyfile}" "" "/dev/null" "tmpdir/osabi.out"]
715 if { [lindex $exec_output 0] != 0 } then {
21c40443 716 unresolved "$test1 preserving OS/ABI"
28902555
BS
717 } else {
718 verbose -log "grep OS/ABI tmpdir/osabi.out"
719 catch "exec grep OS/ABI tmpdir/osabi.out" osabi_out
720 if { "$osabi_in" == "$osabi_out" } {
21c40443 721 pass "$test1 preserving OS/ABI"
28902555 722 } else {
21c40443 723 fail "$test1 preserving OS/ABI"
28902555
BS
724 }
725 }
726 }
727
252b5132 728 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
68a4c073 729 if ![string match "*: no symbols*" $exec_output] {
21c40443 730 fail $test1
252b5132
RH
731 return
732 }
21c40443
AM
733
734 if [string equal "" $test2] {
735 return
736 }
737
738 set result [remote_load target tmpdir/striprog]
739 set status [lindex $result 0]
740 if { $status != "pass" } {
741 fail $test2
742 return
743 }
744
745 pass $test2
252b5132
RH
746}
747
748# Test stripping an executable with saving a symbol
749
21c40443 750proc strip_executable_with_saving_a_symbol { prog flags test1 test2 } {
252b5132
RH
751 global NM
752 global NMFLAGS
753
0fcc17f8 754 remote_file build delete tmpdir/striprog
252b5132
RH
755 remote_download build tmpdir/copyprog tmpdir/striprog
756 if [is_remote host] {
8d263650 757 set copyfile [remote_download host tmpdir/striprog]
252b5132
RH
758 } else {
759 set copyfile tmpdir/striprog
760 }
761
762 set exec_output [binutils_run $prog "$flags ${copyfile}"]
21c40443
AM
763 if ![string equal "" $exec_output] {
764 fail $test1
765 if [string equal "" $test2] {
766 return
767 }
768 fail $test2
252b5132
RH
769 return
770 }
771
252b5132 772 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
edc45ab9
HPN
773 if { [istarget mmix-knuth-mmixware] } {
774 # Whenever there's a symbol in the mmo format, there's the symbol
775 # Main, so remove it manually from the expected output for sake of
776 # this test.
777
778 # Using "" not {} to get the \n and \r translated.
779 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
780 }
781
adacfc81
JJ
782 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
783 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
21c40443 784 fail $test1
252b5132
RH
785 return
786 }
21c40443
AM
787
788 if [string equal "" $test2] {
789 return
790 }
791
792 if [is_remote host] {
793 remote_upload host ${copyfile} tmpdir/striprog
794 }
795
796 set result [remote_load target tmpdir/striprog]
797 set status [lindex $result 0]
798 if { $status != "pass" } {
799 fail $test2
800 return
801 }
802
803 pass $test2
252b5132
RH
804}
805
11701589
JK
806# Test keeping only debug symbols of an executable
807
808proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
0fcc17f8 809 remote_file build delete tmpdir/striprog
11701589
JK
810 remote_download build tmpdir/copyprog tmpdir/striprog
811 if [is_remote host] {
812 set copyfile [remote_download host tmpdir/striprog]
813 } else {
814 set copyfile tmpdir/striprog
815 }
816
817 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
21c40443 818 if ![string equal "" $exec_output] {
11701589
JK
819 fail $test1
820 return
821 }
822 pass $test1
823
824 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
21c40443 825 if ![string equal "" $exec_output] {
11701589
JK
826 fail $test2
827 return
828 }
829 pass $test2
830}
831
63b9bbb7
NC
832# Tests that in a debug only copy of a file the sections
833# headers whoes types have been changed to NOBITS still
834# retain their sh_link fields.
835
836proc keep_debug_symbols_and_check_links { prog flags test } {
837 global READELF
838
839 remote_file build delete tmpdir/striprog
840 remote_download build tmpdir/copyprog tmpdir/striprog
841 if [is_remote host] {
842 set copyfile [remote_download host tmpdir/striprog]
843 } else {
844 set copyfile tmpdir/striprog
845 }
846
847 set exec_output [binutils_run $prog "$flags ${copyfile}"]
21c40443 848 if ![string equal "" $exec_output] {
63b9bbb7
NC
849 fail $test
850 return
851 }
852
853 set got [binutils_run $READELF "-S --wide ${copyfile}"]
854
855 set fails 0
856 # Regexp to match a section with NOBITS type and extract its name and sh_link fields
857 while {[regexp \
858 {[^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]+)(.*)} \
859 $got all name link rest]} {
860 set sh_link 0x$link
861 if {$sh_link == 0} {
862 # Only some NOBITS sections should have a non-zero sh_link field.
863 # Look for them by name.
864 verbose "NOBITS section .$name has a 0 sh_link field\n"
865 switch $name {
866 "rela.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
867 "rel.*" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
868 "hash" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
869 "gnu_version" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
870 "dynsym" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
871 "gnu.version_r" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
872 "dynamic" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
873 "symtab" { set fails 1 ; send_log "Expected non-zero sh_link for .$name\n" }
874 }
875 }
876 set got $rest
877 }
878
879 if {$fails == 0} {
880 pass $test
881 } else {
882 fail $test
883 }
884}
885
886
252b5132 887set test1 "simple objcopy of executable"
21c40443
AM
888set test1r "run objcopy of executable"
889set test2 "strip executable"
890set test2r "run stripped executable"
891set test3 "strip executable with saving a symbol"
892set test3r "run stripped executable with saving a symbol"
893set test4 "keep only debug data"
894set test5 "simple objcopy of debug data"
63b9bbb7 895if [is_elf_format] {
21c40443 896 set test6 "NOBITS sections retain sh_link field"
63b9bbb7 897}
252b5132
RH
898
899switch [copy_setup] {
900 "1" {
901 # do nothing
902 }
903 "2" {
904 untested $test1
21c40443 905 untested $test1r
252b5132 906 untested $test2
21c40443 907 untested $test2r
252b5132 908 untested $test3
21c40443 909 untested $test3r
252b5132 910 untested $test4
11701589 911 untested $test5
63b9bbb7 912 if [is_elf_format] {
21c40443 913 untested $test6
63b9bbb7 914 }
252b5132
RH
915 }
916 "3" {
0d063f63 917 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
21c40443
AM
918 unsupported $test1r
919 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" ""
920 unsupported $test2r
921 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" ""
922 unsupported $test3r
923 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
924 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
63b9bbb7 925 if [is_elf_format] {
21c40443 926 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
63b9bbb7 927 }
252b5132
RH
928 }
929 "0" {
21c40443
AM
930 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test1r"
931 strip_executable "$STRIP" "$STRIPFLAGS" "$test2" "$test2r"
932 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test3" "$test3r"
933 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test4" \
934 "$OBJCOPY" "$OBJCOPYFLAGS" "$test5"
63b9bbb7 935 if [is_elf_format] {
21c40443 936 keep_debug_symbols_and_check_links "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test6"
63b9bbb7 937 }
252b5132
RH
938 }
939}
ad2fb2cd 940
6a0d0afd
L
941proc objcopy_test_readelf {testname srcfile} {
942 global OBJCOPY
943 global OBJCOPYFLAGS
944 global READELF
945 global srcdir
946 global subdir
947
948 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
949 unresolved "objcopy ($testname)"
950 return
951 }
952
953 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
7f6a71ff
JM
954 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
955 if { [lindex $exec_output 0] != 0
21c40443 956 || ![string equal "" [lindex $exec_output 1]] } then {
6a0d0afd 957 fail "objcopy ($testname)"
004314cc 958 return
6a0d0afd
L
959 }
960
961 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
7f6a71ff
JM
962 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
963 if { [lindex $exec_output 0] != 0 } then {
964 unresolved "objcopy ($testname)"
965 return
966 }
967 set exec_output [prune_warnings [lindex $exec_output 1]]
21c40443 968 if ![string equal "" $exec_output] then {
6a0d0afd
L
969 unresolved "objcopy ($testname)"
970 return
971 }
972
973 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
7f6a71ff
JM
974 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
975 if { [lindex $exec_output 0] != 0 } then {
976 unresolved "objcopy ($testname)"
977 return
978 }
979 set exec_output [prune_warnings [lindex $exec_output 1]]
21c40443 980 if ![string equal "" $exec_output] then {
6a0d0afd
L
981 unresolved "objcopy ($testname)"
982 return
983 }
984
985 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
986 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
987 set exec_output [prune_warnings $exec_output]
988
21c40443 989 if [string equal "" $exec_output] then {
6a0d0afd
L
990 pass "objcopy ($testname)"
991 } else {
992 fail "objcopy ($testname)"
993 }
994}
995
0b45135e
AB
996proc objcopy_test_symbol_manipulation {} {
997 global srcdir
998 global subdir
999
1000 set test_list [lsort [glob -nocomplain $srcdir/$subdir/symbols-*.d]]
1001 foreach t $test_list {
1002 # We need to strip the ".d", but can leave the dirname.
1003 verbose [file rootname $t]
1004 run_dump_test [file rootname $t]
1005 }
1006}
1007
ad2fb2cd
L
1008# ia64 specific tests
1009if { ([istarget "ia64-*-elf*"]
1010 || [istarget "ia64-*-linux*"]) } {
64bb95af
L
1011 objcopy_test "ia64 link order" link-order.s
1012}
ad2fb2cd 1013
64bb95af
L
1014# ELF specific tests
1015if [is_elf_format] {
0b45135e 1016 objcopy_test_symbol_manipulation
64bb95af 1017 objcopy_test "ELF unknown section type" unknown.s
6a0d0afd 1018 objcopy_test_readelf "ELF group" group.s
eb3980ce 1019 objcopy_test_readelf "ELF group" group-2.s
f206e905 1020 objcopy_test_readelf "ELF group" group-3.s
e1e87d1e 1021 objcopy_test_readelf "ELF group" group-4.s
b2fc24d4 1022 run_dump_test "group-5"
4c8e8a7e 1023 run_dump_test "group-6"
85d7f0b9 1024 run_dump_test "copy-1"
0930eddd 1025 run_dump_test "note-1"
ad2fb2cd 1026}
af3c5dea 1027
af3c5dea 1028run_dump_test "copy-2"
81fc501a 1029run_dump_test "copy-3"
0691f7af 1030run_dump_test "copy-4"
cbd44e24
L
1031run_dump_test "pr19020a"
1032run_dump_test "pr19020b"
d58c2e3a
RS
1033
1034if [is_elf_format] {
748fc5e9
L
1035 run_dump_test "strip-1"
1036 run_dump_test "strip-2"
1aa9ef63 1037 run_dump_test "strip-3"
eb3980ce
L
1038 run_dump_test "strip-4"
1039 run_dump_test "strip-5"
f206e905
L
1040 run_dump_test "strip-6"
1041 run_dump_test "strip-7"
e1e87d1e
L
1042 run_dump_test "strip-8"
1043 run_dump_test "strip-9"
d4ac1f87 1044 run_dump_test "strip-12"
15635cff 1045 # Non-EABI ARM targets will set OSABI to ARM
685080f2 1046 # The V850 sets the OSABI to UNIX - System V
4cd28456 1047 if { ![istarget "*-*-hpux*"]
15635cff 1048 && ![istarget "msp*-*-*"]
619ed720 1049 && ![istarget "visium-*-*"]
685080f2 1050 && ![istarget "v850*-*-*"]
15635cff 1051 && !([istarget "arm*-*-*"] && ![istarget "arm-*-*eabi*"])} {
4cd28456
AM
1052 run_dump_test "strip-10"
1053 }
0abb10c8
AM
1054 set extra_strip11 ""
1055 if { [istarget "sh64*-*"] } {
1056 # pr17755 testcase
1057 set extra_strip11 { { "as" "--isa=SHmedia --abi=64" } }
1058 }
1059 run_dump_test "strip-11" $extra_strip11
312aaa3c 1060
b8b6abe0
AM
1061 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
1062 # Check to make sure we don't strip a symbol named in relocations.
1063 set test "objcopy keeps symbols needed by relocs"
312aaa3c 1064
b8b6abe0 1065 set srcfile $srcdir/$subdir/needed-by-reloc.s
312aaa3c 1066
b8b6abe0
AM
1067 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
1068 unresolved $test
1069 } else {
1070 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
312aaa3c 1071
b8b6abe0
AM
1072 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
1073 pass $test
1074 } else {
1075 fail $test
1076 }
1077 }
312aaa3c 1078 }
b8b6abe0 1079
8560e02a
HPN
1080 # The symbol table for some MIPS targets is sorted differently than
1081 # the ELF canonical order, so the regexps in localize-hidden-1.d fail
1082 # to match. These tests must be matched to targets for which
6d00b590
AM
1083 # targ_defvec=mips_elf32_be_vec,
1084 # targ_defvec=mips_elf32_le_vec,
1085 # targ_defvec=mips_elf32_n_be_vec or
1086 # targ_defvec=mips_elf32_n_le_vec in config.bfd. When syncing,
8560e02a 1087 # don't forget that earlier case-matches trump later ones.
d4a43794 1088 if { ![istarget "mips*-sde-elf*"] && ![istarget "mips*-mti-elf*"]
a9d58c06 1089 && ![istarget "mips*-img-elf*"]
d4a43794 1090 && ![istarget "mips64*-*-openbsd*"] } {
8560e02a
HPN
1091 setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-elf*" \
1092 "mips*-*-rtems*" "mips*-*-windiss" "mips*-*-none" \
1093 "mips*-*-openbsd*" "mips*-*-chorus*"
1094 }
b8b6abe0 1095 run_dump_test "localize-hidden-1"
afeb3d7f
L
1096 run_dump_test "testranges"
1097 run_dump_test "testranges-ia64"
500ee42e
ILT
1098
1099 run_dump_test "add-section"
2b35fb28 1100 run_dump_test "add-symbol"
500ee42e 1101 run_dump_test "add-empty-section"
18ae9cc1
L
1102
1103 run_dump_test "exclude-1a"
1104 run_dump_test "exclude-1b"
312aaa3c 1105}
b8b6abe0 1106run_dump_test "localize-hidden-2"
14f2c699
L
1107
1108# Test objcopying an object file without global symbol
1109
1110proc objcopy_test_without_global_symbol { } {
1111 global OBJCOPY
1112 global OBJCOPYFLAGS
1113 global OBJDUMP
1114 global OBJDUMPFLAGS
1115 global srcdir
1116 global subdir
1117
1118 set test "strip without global symbol "
1119
1120 if { [target_compile $srcdir/$subdir/pr19547.c tmpdir/pr19547.o object debug] != "" } {
1121 untested $test
1122 return
1123 }
1124
1125 if [is_remote host] {
1126 set objfile [remote_download host tmpdir/pr19547.o]
1127 } else {
1128 set objfile tmpdir/pr19547.o
1129 }
1130
1131 set exec_output [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-unneeded $objfile"]
1132 if ![string equal "" $exec_output] {
1133 fail $test
1134 return
1135 }
1136
1137 set exec_output [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $objfile"]
1138 if {![regexp "no symbols" $exec_output]} {
1139 fail $test
1140 return
1141 }
1142
1143 pass $test
1144}
1145
1146objcopy_test_without_global_symbol
This page took 0.861393 seconds and 4 git commands to generate.