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