* inutils-all/objcopy.exp (strip_executable): Delete remote dest
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / objcopy.exp
CommitLineData
aef6203b 1# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
312aaa3c 2# 2004, 2006, 2007
8c2bc687 3# Free Software Foundation, Inc.
252b5132
RH
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
32866df7 7# the Free Software Foundation; either version 3 of the License, or
252b5132
RH
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
b43b5d5f 17# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
252b5132
RH
18
19# Please email any bugs, comments, and/or additions to this file to:
20# bug-dejagnu@prep.ai.mit.edu
21
22# Written by Ian Lance Taylor <ian@cygnus.com>
23
24if ![is_remote host] {
25 if {[which $OBJCOPY] == 0} then {
26 perror "$OBJCOPY does not exist"
27 return
28 }
29}
30
31send_user "Version [binutil_version $OBJCOPY]"
32
252b5132 33if ![is_remote host] {
8d263650
BE
34 set tempfile tmpdir/bintest.o
35 set copyfile tmpdir/copy
252b5132
RH
36} else {
37 set tempfile [remote_download host tmpdir/bintest.o]
38 set copyfile copy
39}
40
41# Test that objcopy does not modify a file when copying it.
42
64bb95af
L
43proc objcopy_test {testname srcfile} {
44 global OBJCOPY
45 global OBJCOPYFLAGS
46 global srcdir
47 global subdir
48 global tempfile
49 global copyfile
252b5132 50
64bb95af
L
51 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
52 perror "unresolved $testname"
53 unresolved "objcopy ($testname)"
54 return
252b5132 55 }
252b5132 56
64bb95af
L
57 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
58
59 if ![string match "" $got] then {
60 fail "objcopy ($testname)"
252b5132 61 } else {
64bb95af
L
62 send_log "cmp $tempfile ${copyfile}.o\n"
63 verbose "cmp $tempfile ${copyfile}.o"
64 if [is_remote host] {
65 set src1 tmpdir/bintest.o
66 set src2 tmpdir/copy.o
67 remote_upload host $tempfile $src1
68 remote_upload host ${copyfile}.o $src2
69 } else {
70 set src1 ${tempfile}
71 set src2 ${copyfile}.o
72 }
73 set status [remote_exec build cmp "${src1} ${src2}"]
74 set exec_output [lindex $status 1]
75 set exec_output [prune_warnings $exec_output]
76
77 # On some systems the result of objcopy will not be identical.
78 # Usually this is just because gas isn't using bfd to write the
79 # files in the first place, and may order things a little
80 # differently. Those systems should use setup_xfail here.
81
82 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
83 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
84 setup_xfail "hppa*-*-*"
85 setup_xfail "i960-*"
86 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
87 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
88 setup_xfail "m8*-*"
89 setup_xfail "or32-*-rtems*" "or32-*-coff"
90 setup_xfail "sh-*-coff*" "sh-*-rtems*"
91 setup_xfail "tic4x-*-*" "tic80-*-*" "w65-*"
92
93 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
94 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*"
95 clear_xfail "m68*-*-sysv4*"
96
97 if [string match "" $exec_output] then {
98 pass "objcopy ($testname)"
99 } else {
100 send_log "$exec_output\n"
101 verbose "$exec_output" 1
252b5132 102
64bb95af
L
103 # On OSF/1, this succeeds with gas and fails with /bin/as.
104 setup_xfail "alpha*-*-osf*"
252b5132 105
64bb95af
L
106 fail "objcopy ($testname)"
107 }
252b5132
RH
108 }
109}
110
64bb95af
L
111objcopy_test "simple copy" bintest.s
112
9e48b4c6
NC
113# Test reversing bytes in a section.
114
115set reversed ${tempfile}-reversed
116set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -j .data --reverse-bytes=4 $tempfile $reversed"]
117
118if ![string match "" $got] then {
119 fail "objcopy --reverse-bytes"
120} else {
121 if [is_remote host] {
122 remote_upload host ${reversed} tmpdir/copy-reversed.o
123 set reversed tmpdir/copy-reversed.o
124 }
125
126 set origdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $tempfile"]
127 set revdata [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s -j .data $reversed"]
128
129 set want "^ \[0-9\]+ (\[0-9\]+)"
130 set found_orig [regexp -lineanchor $want $origdata -> origdata]
131 set found_rev [regexp -lineanchor $want $revdata -> revdata]
132
133 if {$found_orig == 0 || $found_rev == 0} then {
134 fail "objcopy --reverse-bytes"
135 } else {
136 scan $origdata "%2x%2x%2x%2x" b1 b2 b3 b4
137 scan $revdata "%2x%2x%2x%2x" c4 c3 c2 c1
138
139 if {$b1 == $c1 && $b2 == $c2 && $b3 == $c3 && $b4 == $c4} then {
140 pass "objcopy --reverse-bytes"
141 } else {
142 fail "objcopy --reverse-bytes"
143 }
144 }
145}
146
252b5132
RH
147# Test generating S records.
148
149# We make the srec filename 8.3 compatible. Note that the header string
150# matched against depends on the name of the file. Ugh.
151
152if [is_remote host] {
153 set srecfile copy.sre
154 set header_string S00B0000636F70792E737265C1
155} else {
156 set srecfile ${copyfile}.srec
157 set header_string S0130000746D706469722F636F70792E7372656397
158}
159
160set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
161
162if ![string match "" $got] then {
163 fail "objcopy -O srec"
164} else {
165 if [is_remote host] {
8d263650
BE
166 remote_upload host ${srecfile} tmpdir/copy.srec
167 set srecfile tmpdir/copy.srec
252b5132
RH
168 }
169 set file [open ${srecfile} r]
170
171 # The first S record is fixed by the file name we are using.
172 gets $file line
173 send_log "$line\n"
174 verbose $line
175 if ![regexp "$header_string.*" $line] {
176 send_log "bad header\n"
177 fail "objcopy -O srec"
178 } else {
179 while {[gets $file line] != -1 \
180 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
181 send_log "$line\n"
182 verbose $line
183 set line "**EOF**"
184 }
185 send_log "$line\n"
186 verbose $line
187 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
188 send_log "bad trailer\n"
189 fail "objcopy -O srec"
190 } else {
191 if {[gets $file line] != -1} then {
192 send_log "garbage at end\n"
193 send_log "$line\n"
194 verbose $line
195 fail "objcopy -O srec"
196 } else {
7f6a71ff 197 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${srecfile}"]
252b5132
RH
198 if ![regexp "file format srec" $got] then {
199 send_log "objdump failed\n"
200 fail "objcopy -O srec"
201 } else {
202 pass "objcopy -O srec"
203 }
204 }
205 }
206 }
207
208 close $file
209}
210
211# Test setting and adjusting the start address. We only test this
212# while generating S records, because we may not be able to set the
213# start address for other object file formats, and the S record case
214# is the only useful one anyhow.
215
216set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
217if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
218 perror "objdump can not recognize bintest.o"
219 set origstart ""
220} else {
627fe3fb 221 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --set-start 0x7654 $tempfile ${copyfile}.srec"]
252b5132
RH
222 if ![string match "" $got] then {
223 fail "objcopy --set-start"
224 } else {
225 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
226 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
227 fail "objcopy --set-start"
228 } else {
229 if {$srecstart != 0x7654} then {
230 send_log "$srecstart != 0x7654\n"
231 fail "objcopy --set-start"
232 } else {
233 pass "objcopy --set-start"
234 }
235 }
236 }
237
627fe3fb 238 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-start 0x123 $tempfile ${copyfile}.srec"]
252b5132
RH
239 if ![string match "" $got] then {
240 fail "objcopy --adjust-start"
241 } else {
242 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
243 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
244 fail "objcopy --adjust-start"
245 } else {
246 if {$srecstart != $origstart + 0x123} then {
247 send_log "$srecstart != $origstart + 0x123\n"
248 fail "objcopy --adjust-start"
249 } else {
250 pass "objcopy --adjust-start"
251 }
252 }
253 }
254}
255
256# Test adjusting the overall VMA, and adjusting the VMA of a
257# particular section. We again only test this when generating S
258# records.
259
260set low ""
261set lowname ""
262
263set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
264
265set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
266
267set got $headers
268while {[regexp $headers_regexp $got all name size vma rest]} {
269 set vma 0x$vma
270 set size 0x$size
271 if {$size != 0} {
272 if {$low == "" || $vma < $low} {
273 set low $vma
274 set lowname $name
275 }
276 }
277 set got $rest
278}
279
280if {$low == "" || $origstart == ""} then {
281 perror "objdump can not recognize bintest.o"
282} else {
627fe3fb 283 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec --adjust-vma 0x123 $tempfile ${copyfile}.srec"]
252b5132
RH
284 if ![string match "" $got] then {
285 fail "objcopy --adjust-vma"
286 } else {
287 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
288 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
289 if ![regexp $want $got all start vma] then {
290 fail "objcopy --adjust-vma"
291 } else {
292 set vma 0x$vma
293 if {$vma != $low + 0x123} then {
294 send_log "$vma != $low + 0x123\n"
295 fail "objcopy --adjust-vma"
296 } else {
297 if {$start != $origstart + 0x123} then {
298 send_log "$start != $origstart + 0x123\n"
299 fail "objcopy --adjust-vma"
300 } else {
301 pass "objcopy --adjust-vma"
302 }
303 }
304 }
305 }
306
307 set arg ""
308 set got $headers
309 while {[regexp $headers_regexp $got all name size vma rest]} {
310 set vma 0x$vma
311 if {$vma == $low} then {
312 set arg "$arg --adjust-section-vma $name+4"
313 }
314 set got $rest
315 }
316
627fe3fb 317 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $arg $tempfile ${copyfile}.srec"]
252b5132
RH
318 if ![string match "" $got] then {
319 fail "objcopy --adjust-section-vma +"
320 } else {
321 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
322 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
323 if ![regexp $want $got all vma] then {
324 fail "objcopy --adjust-section-vma +"
325 } else {
326 set vma 0x$vma
327 if {$vma != $low + 4} then {
328 send_log "$vma != $low + 4\n"
329 fail "objcopy --adjust-section-vma +"
330 } else {
331 pass "objcopy --adjust-section-vma +"
332 }
333 }
334 }
335
336 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
627fe3fb 337 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $argeq $tempfile ${copyfile}.srec"]
252b5132
RH
338 if ![string match "" $got] then {
339 fail "objcopy --adjust-section-vma ="
340 } else {
341 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
342 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
343 if ![regexp $want $got all vma] then {
344 fail "objcopy --adjust-section-vma ="
345 } else {
346 set vma 0x$vma
347 if {$vma != $low + 4} then {
348 send_log "$vma != $low + 4\n"
349 fail "objcopy --adjust-section-vma ="
350 } else {
351 pass "objcopy --adjust-section-vma ="
352 }
353 }
354 }
355}
356
357# Test stripping an object.
358
359proc strip_test { } {
1983fbac 360 global AR
252b5132
RH
361 global CC
362 global STRIP
363 global STRIPFLAGS
364 global NM
365 global NMFLAGS
366 global srcdir
367 global subdir
368
369 set test "strip"
370
371 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
372 untested $test
373 return
374 }
375
376 if [is_remote host] {
1983fbac 377 set archive libstrip.a
8d263650 378 set objfile [remote_download host tmpdir/testprog.o]
1983fbac
L
379 remote_file host delete $archive
380 } else {
381 set archive tmpdir/libstrip.a
382 set objfile tmpdir/testprog.o
383 }
384
385 remote_file build delete tmpdir/libstrip.a
386
387 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
388 if ![string match "" $exec_output] {
389 fail $test
390 return
391 }
392
8648f88f
L
393 set exec_output [binutils_run $STRIP "-g $archive"]
394 if ![string match "" $exec_output] {
395 fail $test
396 return
397 }
398
1983fbac
L
399 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
400 if ![string match "" $exec_output] {
401 fail $test
402 return
403 }
404
405 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
406 untested $test
407 return
408 }
409
410 if [is_remote host] {
8d263650 411 set objfile [remote_download host tmpdir/testprog.o]
252b5132
RH
412 } else {
413 set objfile tmpdir/testprog.o
414 }
415
416 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
417 if ![string match "" $exec_output] {
418 fail $test
419 return
420 }
421
422 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
68a4c073 423 if ![string match "*: no symbols*" $exec_output] {
252b5132
RH
424 fail $test
425 return
426 }
427
428 pass $test
429}
430
431strip_test
432
433# Test stripping an object file with saving a symbol
434
435proc strip_test_with_saving_a_symbol { } {
436 global CC
437 global STRIP
438 global STRIPFLAGS
439 global NM
440 global NMFLAGS
441 global srcdir
442 global subdir
443
444 set test "strip with saving a symbol"
445
446 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
447 untested $test
448 return
449 }
450
451 if [is_remote host] {
8d263650 452 set objfile [remote_download host tmpdir/testprog.o]
252b5132
RH
453 } else {
454 set objfile tmpdir/testprog.o
455 }
456
457 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
458 if ![string match "" $exec_output] {
459 fail $test
460 return
461 }
462
463 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
195fe636 464 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
252b5132
RH
465 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
466 fail $test
467 return
468 }
469
470 pass $test
471}
472
473strip_test_with_saving_a_symbol
474
475# Build a final executable.
476
99ad8390 477if { [istarget *-*-cygwin] || [istarget *-*-mingw*] } {
f8a7038a
NC
478 set test_prog "testprog.exe"
479} else {
480 set test_prog "testprog"
481}
482
252b5132
RH
483proc copy_setup { } {
484 global srcdir
485 global subdir
9a580d9a 486 global gcc_gas_flag
f8a7038a 487 global test_prog
72d4c02f 488 global host_triplet
252b5132 489
8d263650
BE
490 set res [build_wrapper testglue.o]
491 set flags { debug }
252b5132 492
903b1f5b
NS
493 if { [istarget *-*-uclinux*] } {
494 return 1
495 }
496
252b5132 497 if { $res != "" } {
8d263650
BE
498 lappend flags "additional_flags=[lindex $res 1]"
499 set add_libs "testglue.o"
252b5132 500 } else {
8d263650 501 set add_libs ""
252b5132
RH
502 }
503
9a580d9a
L
504 if { [istarget *-*-linux*] } {
505 foreach i $gcc_gas_flag {
506 set flags "additional_flags=$i $flags"
507 }
508 }
f8a7038a 509 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/$test_prog executable $flags] != "" } {
252b5132
RH
510 return 2
511 }
512
8d263650
BE
513 set result [remote_load target tmpdir/$test_prog]
514 set status [lindex $result 0]
252b5132
RH
515
516 if { $status != "pass" } {
72d4c02f
KT
517 send_log "cannot run executable, status = ${status} on ${host_triplet}\n"
518 if { [istarget $host_triplet] } {
519 return 3
520 }
521 set status "pass"
252b5132
RH
522 }
523
524 return 0
525}
526
527# Test copying an executable.
528
529proc copy_executable { prog flags test1 test2 } {
f8a7038a 530 global test_prog
72d4c02f 531 global host_triplet
252b5132
RH
532
533 if [is_remote host] {
8d263650 534 set testfile [remote_download host tmpdir/$test_prog]
252b5132
RH
535 set testcopy copyprog
536 } else {
f8a7038a 537 set testfile tmpdir/$test_prog
252b5132
RH
538 set testcopy tmpdir/copyprog
539 }
8d263650 540 remote_file host delete $testcopy
252b5132
RH
541
542 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
543
544 if ![string match "" $exec_output] {
545 fail $test1
0d063f63
AM
546 if [string match "" $test2] {
547 return
548 }
252b5132
RH
549 fail $test2
550 return
551 }
552
553 if [is_remote host] {
554 remote_upload host $testcopy tmpdir/copyprog
555 }
556
f8a7038a 557 set status [remote_exec build "cmp" "tmpdir/$test_prog tmpdir/copyprog"]
8d263650 558 set exec_output [lindex $status 1]
252b5132
RH
559
560 if [string match "" $exec_output] then {
561 pass $test1
562 } else {
563 send_log "$exec_output\n"
564 verbose "$exec_output"
565
566 # This will fail for many reasons. For example, it will most
567 # likely fail if a non-GNU linker is used. Therefore, we do
568 # not insist that it pass. If you are using an assembler and
569 # linker based on the same BFD as objcopy, it is worth
570 # investigating to see why this failure occurs. If we are
571 # cross compiling, we assume that a GNU linker is being used,
572 # and expect it to succeed.
573 if {[isnative]} then {
574 setup_xfail "*-*-*"
575 }
576
577 # This also fails for mips*-*-elf targets. See elf32-mips.c
578 # mips_elf_sym_is_global.
579 setup_xfail "mips*-*-elf"
580
9da7f49f 581 setup_xfail "*arm*-*-coff"
70bbfa26 582 setup_xfail "xscale-*-coff"
11d47adc 583 setup_xfail "arm*-*-pe"
c58c5ee7
NC
584 setup_xfail "thumb*-*-coff"
585 setup_xfail "thumb*-*-pe"
72d4c02f 586 setup_xfail "x86_64-*-mingw*"
11d47adc 587
252b5132
RH
588 fail $test1
589 }
590
0d063f63
AM
591 if [string match "" $test2] {
592 return
593 }
594
252b5132 595 set output [remote_load target tmpdir/copyprog]
8d263650 596 set status [lindex $output 0]
72d4c02f
KT
597 if { ![istarget $host_triplet] } {
598 set status "pass"
599 }
252b5132
RH
600 if { $status != "pass" } {
601 fail $test2
602 } else {
603 pass $test2
604 }
605}
606
607# Test stripping an executable
608
609proc strip_executable { prog flags test } {
610 global NM
611 global NMFLAGS
72d4c02f 612 global host_triplet
252b5132 613
0fcc17f8 614 remote_file build delete tmpdir/striprog
252b5132
RH
615 remote_download build tmpdir/copyprog tmpdir/striprog
616 if [is_remote host] {
8d263650 617 set copyfile [remote_download host tmpdir/striprog]
252b5132
RH
618 } else {
619 set copyfile tmpdir/striprog
620 }
621
622 set exec_output [binutils_run $prog "$flags ${copyfile}"]
623 if ![string match "" $exec_output] {
624 fail $test
625 return
626 }
627
628 if [is_remote host] {
8d263650 629 remote_upload host ${copyfile} tmpdir/striprog
252b5132
RH
630 }
631
632 set result [remote_load target tmpdir/striprog]
8d263650 633 set status [lindex $result 0]
72d4c02f
KT
634 if { ![istarget $host_triplet] } {
635 set status "pass"
636 }
252b5132
RH
637 if { $status != "pass" } {
638 fail $test
639 return
640 }
641
642 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
68a4c073 643 if ![string match "*: no symbols*" $exec_output] {
252b5132
RH
644 fail $test
645 return
646 }
647 pass $test
648}
649
650# Test stripping an executable with saving a symbol
651
652proc strip_executable_with_saving_a_symbol { prog flags test } {
653 global NM
654 global NMFLAGS
72d4c02f 655 global host_triplet
252b5132 656
0fcc17f8 657 remote_file build delete tmpdir/striprog
252b5132
RH
658 remote_download build tmpdir/copyprog tmpdir/striprog
659 if [is_remote host] {
8d263650 660 set copyfile [remote_download host tmpdir/striprog]
252b5132
RH
661 } else {
662 set copyfile tmpdir/striprog
663 }
664
665 set exec_output [binutils_run $prog "$flags ${copyfile}"]
666 if ![string match "" $exec_output] {
667 fail $test
668 return
669 }
670
671 if [is_remote host] {
8d263650 672 remote_upload host ${copyfile} tmpdir/striprog
252b5132
RH
673 }
674
675 set result [remote_load target tmpdir/striprog]
8d263650 676 set status [lindex $result 0]
72d4c02f
KT
677 if { ![istarget $host_triplet] } {
678 set status "pass"
679 }
252b5132
RH
680 if { $status != "pass" } {
681 fail $test
682 return
683 }
684
685 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
edc45ab9
HPN
686 if { [istarget mmix-knuth-mmixware] } {
687 # Whenever there's a symbol in the mmo format, there's the symbol
688 # Main, so remove it manually from the expected output for sake of
689 # this test.
690
691 # Using "" not {} to get the \n and \r translated.
692 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
693 }
694
adacfc81
JJ
695 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
696 && ![regexp {^([0-9a-fA-F]+)?[ ]+[TD] _main} $exec_output]} {
252b5132
RH
697 fail $test
698 return
699 }
700 pass $test
701}
702
11701589
JK
703# Test keeping only debug symbols of an executable
704
705proc keep_debug_symbols_and_test_copy { prog1 flags1 test1 prog2 flags2 test2 } {
706 global NM
707 global NMFLAGS
708
0fcc17f8 709 remote_file build delete tmpdir/striprog
11701589
JK
710 remote_download build tmpdir/copyprog tmpdir/striprog
711 if [is_remote host] {
712 set copyfile [remote_download host tmpdir/striprog]
713 } else {
714 set copyfile tmpdir/striprog
715 }
716
717 set exec_output [binutils_run $prog1 "$flags1 ${copyfile}"]
718 if ![string match "" $exec_output] {
719 fail $test1
720 return
721 }
722 pass $test1
723
724 set exec_output [binutils_run $prog2 "$flags2 ${copyfile}"]
725 if ![string match "" $exec_output] {
726 fail $test2
727 return
728 }
729 pass $test2
730}
731
252b5132
RH
732set test1 "simple objcopy of executable"
733set test2 "run objcopy of executable"
734set test3 "run stripped executable"
735set test4 "run stripped executable with saving a symbol"
11701589
JK
736set test5 "keep only debug data"
737set test6 "simple objcopy of debug data"
252b5132
RH
738
739switch [copy_setup] {
740 "1" {
741 # do nothing
742 }
743 "2" {
744 untested $test1
745 untested $test2
746 untested $test3
747 untested $test4
11701589
JK
748 untested $test5
749 untested $test6
252b5132
RH
750 }
751 "3" {
0d063f63
AM
752 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" ""
753 unsupported $test2
754 unsupported $test3
755 unsupported $test4
11701589
JK
756 unsupported $test5
757 unsupported $test6
252b5132
RH
758 }
759 "0" {
760 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
761 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
762 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
11701589
JK
763 keep_debug_symbols_and_test_copy "$STRIP" "--only-keep-debug $STRIPFLAGS" "$test5" \
764 "$OBJCOPY" "$OBJCOPYFLAGS" "$test6"
252b5132
RH
765 }
766}
ad2fb2cd 767
6a0d0afd
L
768proc objcopy_test_readelf {testname srcfile} {
769 global OBJCOPY
770 global OBJCOPYFLAGS
771 global READELF
772 global srcdir
773 global subdir
774
775 if {![binutils_assemble $srcdir/$subdir/${srcfile} tmpdir/bintest.o]} then {
776 unresolved "objcopy ($testname)"
777 return
778 }
779
780 verbose -log "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"
7f6a71ff
JM
781 set exec_output [remote_exec host "$OBJCOPY $OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
782 if { [lindex $exec_output 0] != 0
783 || ![string match "" [lindex $exec_output 1]] } then {
6a0d0afd 784 fail "objcopy ($testname)"
004314cc 785 return
6a0d0afd
L
786 }
787
788 verbose -log "$READELF -a tmpdir/bintest.o > tmpdir/bintest.o.out"
7f6a71ff
JM
789 set exec_output [remote_exec host "$READELF -a tmpdir/bintest.o" "" "/dev/null" "tmpdir/bintest.o.out"]
790 if { [lindex $exec_output 0] != 0 } then {
791 unresolved "objcopy ($testname)"
792 return
793 }
794 set exec_output [prune_warnings [lindex $exec_output 1]]
6a0d0afd
L
795 if ![string match "" $exec_output] then {
796 unresolved "objcopy ($testname)"
797 return
798 }
799
800 verbose -log "$READELF -a tmpdir/copy.o > tmpdir/copy.o.out"
7f6a71ff
JM
801 set exec_output [remote_exec host "$READELF -a tmpdir/copy.o" "" "/dev/null" "tmpdir/copy.o.out"]
802 if { [lindex $exec_output 0] != 0 } then {
803 unresolved "objcopy ($testname)"
804 return
805 }
806 set exec_output [prune_warnings [lindex $exec_output 1]]
6a0d0afd
L
807 if ![string match "" $exec_output] then {
808 unresolved "objcopy ($testname)"
809 return
810 }
811
812 verbose -log "diff tmpdir/bintest.o.out tmpdir/copy.o.out"
813 catch "exec diff tmpdir/bintest.o.out tmpdir/copy.o.out" exec_output
814 set exec_output [prune_warnings $exec_output]
815
816 if [string match "" $exec_output] then {
817 pass "objcopy ($testname)"
818 } else {
819 fail "objcopy ($testname)"
820 }
821}
822
ad2fb2cd
L
823# ia64 specific tests
824if { ([istarget "ia64-*-elf*"]
825 || [istarget "ia64-*-linux*"]) } {
64bb95af
L
826 objcopy_test "ia64 link order" link-order.s
827}
ad2fb2cd 828
64bb95af
L
829# ELF specific tests
830if [is_elf_format] {
831 objcopy_test "ELF unknown section type" unknown.s
6a0d0afd 832 objcopy_test_readelf "ELF group" group.s
eb3980ce 833 objcopy_test_readelf "ELF group" group-2.s
f206e905 834 objcopy_test_readelf "ELF group" group-3.s
e1e87d1e 835 objcopy_test_readelf "ELF group" group-4.s
85d7f0b9 836 run_dump_test "copy-1"
ad2fb2cd 837}
af3c5dea 838
af3c5dea 839run_dump_test "copy-2"
81fc501a 840run_dump_test "copy-3"
0691f7af 841run_dump_test "copy-4"
d58c2e3a
RS
842
843if [is_elf_format] {
748fc5e9
L
844 run_dump_test "strip-1"
845 run_dump_test "strip-2"
1aa9ef63 846 run_dump_test "strip-3"
eb3980ce
L
847 run_dump_test "strip-4"
848 run_dump_test "strip-5"
f206e905
L
849 run_dump_test "strip-6"
850 run_dump_test "strip-7"
e1e87d1e
L
851 run_dump_test "strip-8"
852 run_dump_test "strip-9"
312aaa3c 853
b8b6abe0
AM
854 if { [istarget "i*86-*"] || [istarget "x86_64-*-*"] } {
855 # Check to make sure we don't strip a symbol named in relocations.
856 set test "objcopy keeps symbols needed by relocs"
312aaa3c 857
b8b6abe0 858 set srcfile $srcdir/$subdir/needed-by-reloc.s
312aaa3c 859
b8b6abe0
AM
860 if {![binutils_assemble $srcfile tmpdir/bintest.o]} then {
861 unresolved $test
862 } else {
863 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS --strip-symbol=foo tmpdir/bintest.o ${copyfile}.o"]
312aaa3c 864
b8b6abe0
AM
865 if [regexp "not stripping symbol `foo' because it is named in a relocation" $got] {
866 pass $test
867 } else {
868 fail $test
869 }
870 }
312aaa3c 871 }
b8b6abe0
AM
872
873 run_dump_test "localize-hidden-1"
312aaa3c 874}
b8b6abe0 875run_dump_test "localize-hidden-2"
This page took 0.405702 seconds and 4 git commands to generate.