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