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