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