* gdb-events.sh: Add selected-frame-level-changed event.
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / objcopy.exp
CommitLineData
70bbfa26 1# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
8c2bc687 2# Free Software Foundation, Inc.
252b5132
RH
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18# Please email any bugs, comments, and/or additions to this file to:
19# bug-dejagnu@prep.ai.mit.edu
20
21# Written by Ian Lance Taylor <ian@cygnus.com>
22
23if ![is_remote host] {
24 if {[which $OBJCOPY] == 0} then {
25 perror "$OBJCOPY does not exist"
26 return
27 }
28}
29
30send_user "Version [binutil_version $OBJCOPY]"
31
32if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
33 perror "unresolved 1"
34 unresolved "objcopy (simple copy)"
35 return
36}
37
38if ![is_remote host] {
39 set tempfile tmpdir/bintest.o;
40 set copyfile tmpdir/copy;
41} else {
42 set tempfile [remote_download host tmpdir/bintest.o]
43 set copyfile copy
44}
45
46# Test that objcopy does not modify a file when copying it.
47
48set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS $tempfile ${copyfile}.o"]
49
50if ![string match "" $got] then {
51 fail "objcopy (simple copy)"
52} else {
53 send_log "cmp $tempfile ${copyfile}.o\n"
54 verbose "cmp $tempfile ${copyfile}.o"
55 if [is_remote host] {
56 set src1 tmpdir/bintest.o
57 set src2 tmpdir/copy.o
58 remote_upload host $tempfile $src1;
59 remote_upload host ${copyfile}.o $src2;
60 } else {
61 set src1 ${tempfile}
62 set src2 ${copyfile}.o
63 }
64 set status [remote_exec build cmp "${src1} ${src2}"];
65 set exec_output [lindex $status 1];
66 set exec_output [prune_warnings $exec_output]
67
68 # On some systems the result of objcopy will not be identical.
69 # Usually this is just because gas isn't using bfd to write the files
70 # in the first place, and may order things a little differently.
71 # Those systems should use setup_xfail here.
72
8de397a5
AM
73 setup_xfail "a29k-*"
74 setup_xfail "h8300-*-rtems*" "h8300-*-coff"
75 setup_xfail "h8500-*-rtems*" "h8500-*-coff"
252b5132 76 setup_xfail "hppa*-*-*"
8de397a5
AM
77 setup_xfail "i960-*"
78 setup_xfail "m68*-*-*coff" "m68*-*-hpux*" "m68*-*-lynxos*"
79 setup_xfail "m68*-*-sysv*" "m68*-apple-aux*"
80 setup_xfail "m8*-*"
81 setup_xfail "or32-*-rtems*" "or32-*-coff"
82 setup_xfail "sh-*-coff*" "sh-*-rtems*"
83 setup_xfail "tic80-*-*" "w65-*" "z8*-*"
84
85 clear_xfail "hppa*64*-*-hpux*" "hppa*-*-linux*" "hppa*-*-lites*"
86 clear_xfail "hppa*-*-*n*bsd*" "hppa*-*-rtems*" "*-*-*elf*" "m68*-*-sysv4*"
252b5132
RH
87
88 if [string match "" $exec_output] then {
89 pass "objcopy (simple copy)"
90 } else {
91 send_log "$exec_output\n"
92 verbose "$exec_output" 1
93
94 # On OSF/1, this succeeds with gas and fails with /bin/as.
95 setup_xfail "alpha*-*-osf*"
96
97 # This fails for COFF i960-vxworks targets.
98 setup_xfail "i960-*-vxworks*"
99
100 fail "objcopy (simple copy)"
101 }
102}
103
104# Test generating S records.
105
106# We make the srec filename 8.3 compatible. Note that the header string
107# matched against depends on the name of the file. Ugh.
108
109if [is_remote host] {
110 set srecfile copy.sre
111 set header_string S00B0000636F70792E737265C1
112} else {
113 set srecfile ${copyfile}.srec
114 set header_string S0130000746D706469722F636F70792E7372656397
115}
116
117set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${srecfile}"]
118
119if ![string match "" $got] then {
120 fail "objcopy -O srec"
121} else {
122 if [is_remote host] {
123 remote_upload host ${srecfile} tmpdir/copy.srec;
124 set srecfile tmpdir/copy.srec;
125 }
126 set file [open ${srecfile} r]
127
128 # The first S record is fixed by the file name we are using.
129 gets $file line
130 send_log "$line\n"
131 verbose $line
132 if ![regexp "$header_string.*" $line] {
133 send_log "bad header\n"
134 fail "objcopy -O srec"
135 } else {
136 while {[gets $file line] != -1 \
137 && [regexp "^S\[123\]\[0-9a-fA-F\]+\[\r\n\]*$" $line]} {
138 send_log "$line\n"
139 verbose $line
140 set line "**EOF**"
141 }
142 send_log "$line\n"
143 verbose $line
144 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\[\r\n\]*$" $line] then {
145 send_log "bad trailer\n"
146 fail "objcopy -O srec"
147 } else {
148 if {[gets $file line] != -1} then {
149 send_log "garbage at end\n"
150 send_log "$line\n"
151 verbose $line
152 fail "objcopy -O srec"
153 } else {
154 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
155 if ![regexp "file format srec" $got] then {
156 send_log "objdump failed\n"
157 fail "objcopy -O srec"
158 } else {
159 pass "objcopy -O srec"
160 }
161 }
162 }
163 }
164
165 close $file
166}
167
168# Test setting and adjusting the start address. We only test this
169# while generating S records, because we may not be able to set the
170# start address for other object file formats, and the S record case
171# is the only useful one anyhow.
172
173set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $tempfile"]
174if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
175 perror "objdump can not recognize bintest.o"
176 set origstart ""
177} else {
178 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --set-start 0x7654"]
179 if ![string match "" $got] then {
180 fail "objcopy --set-start"
181 } else {
182 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
183 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
184 fail "objcopy --set-start"
185 } else {
186 if {$srecstart != 0x7654} then {
187 send_log "$srecstart != 0x7654\n"
188 fail "objcopy --set-start"
189 } else {
190 pass "objcopy --set-start"
191 }
192 }
193 }
194
195 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-start 0x123"]
196 if ![string match "" $got] then {
197 fail "objcopy --adjust-start"
198 } else {
199 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f ${copyfile}.srec"]
200 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
201 fail "objcopy --adjust-start"
202 } else {
203 if {$srecstart != $origstart + 0x123} then {
204 send_log "$srecstart != $origstart + 0x123\n"
205 fail "objcopy --adjust-start"
206 } else {
207 pass "objcopy --adjust-start"
208 }
209 }
210 }
211}
212
213# Test adjusting the overall VMA, and adjusting the VMA of a
214# particular section. We again only test this when generating S
215# records.
216
217set low ""
218set lowname ""
219
220set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $tempfile"]
221
222set headers_regexp "\[ 0-9\]+(\[^ \]+)\[ \]*(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)\[ \]+\[0-9a-fA-F\]+\[ \]+2\[*\]\[*\]\[0-9\]+(.*)"
223
224set got $headers
225while {[regexp $headers_regexp $got all name size vma rest]} {
226 set vma 0x$vma
227 set size 0x$size
228 if {$size != 0} {
229 if {$low == "" || $vma < $low} {
230 set low $vma
231 set lowname $name
232 }
233 }
234 set got $rest
235}
236
237if {$low == "" || $origstart == ""} then {
238 perror "objdump can not recognize bintest.o"
239} else {
240 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec --adjust-vma 0x123"]
241 if ![string match "" $got] then {
242 fail "objcopy --adjust-vma"
243 } else {
244 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh ${copyfile}.srec"]
245 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
246 if ![regexp $want $got all start vma] then {
247 fail "objcopy --adjust-vma"
248 } else {
249 set vma 0x$vma
250 if {$vma != $low + 0x123} then {
251 send_log "$vma != $low + 0x123\n"
252 fail "objcopy --adjust-vma"
253 } else {
254 if {$start != $origstart + 0x123} then {
255 send_log "$start != $origstart + 0x123\n"
256 fail "objcopy --adjust-vma"
257 } else {
258 pass "objcopy --adjust-vma"
259 }
260 }
261 }
262 }
263
264 set arg ""
265 set got $headers
266 while {[regexp $headers_regexp $got all name size vma rest]} {
267 set vma 0x$vma
268 if {$vma == $low} then {
269 set arg "$arg --adjust-section-vma $name+4"
270 }
271 set got $rest
272 }
273
274 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $arg"]
275 if ![string match "" $got] then {
276 fail "objcopy --adjust-section-vma +"
277 } else {
278 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
279 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
280 if ![regexp $want $got all vma] then {
281 fail "objcopy --adjust-section-vma +"
282 } else {
283 set vma 0x$vma
284 if {$vma != $low + 4} then {
285 send_log "$vma != $low + 4\n"
286 fail "objcopy --adjust-section-vma +"
287 } else {
288 pass "objcopy --adjust-section-vma +"
289 }
290 }
291 }
292
293 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
294 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec $tempfile ${copyfile}.srec $argeq"]
295 if ![string match "" $got] then {
296 fail "objcopy --adjust-section-vma ="
297 } else {
298 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h ${copyfile}.srec"]
299 set want "file format srec.*sec1\[ \]+\[0-9a-fA-F\]+\[ \]+(\[0-9a-fA-F\]+)"
300 if ![regexp $want $got all vma] then {
301 fail "objcopy --adjust-section-vma ="
302 } else {
303 set vma 0x$vma
304 if {$vma != $low + 4} then {
305 send_log "$vma != $low + 4\n"
306 fail "objcopy --adjust-section-vma ="
307 } else {
308 pass "objcopy --adjust-section-vma ="
309 }
310 }
311 }
312}
313
314# Test stripping an object.
315
316proc strip_test { } {
1983fbac 317 global AR
252b5132
RH
318 global CC
319 global STRIP
320 global STRIPFLAGS
321 global NM
322 global NMFLAGS
323 global srcdir
324 global subdir
325
326 set test "strip"
327
328 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
329 untested $test
330 return
331 }
332
333 if [is_remote host] {
1983fbac
L
334 set archive libstrip.a
335 set objfile [remote_download host tmpdir/testprog.o];
336 remote_file host delete $archive
337 } else {
338 set archive tmpdir/libstrip.a
339 set objfile tmpdir/testprog.o
340 }
341
342 remote_file build delete tmpdir/libstrip.a
343
344 set exec_output [binutils_run $AR "rc $archive ${objfile}"]
345 if ![string match "" $exec_output] {
346 fail $test
347 return
348 }
349
350 set exec_output [binutils_run $STRIP "$STRIPFLAGS $archive"]
351 if ![string match "" $exec_output] {
352 fail $test
353 return
354 }
355
356 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
357 untested $test
358 return
359 }
360
361 if [is_remote host] {
252b5132
RH
362 set objfile [remote_download host tmpdir/testprog.o];
363 } else {
364 set objfile tmpdir/testprog.o
365 }
366
367 set exec_output [binutils_run $STRIP "$STRIPFLAGS $objfile"]
368 if ![string match "" $exec_output] {
369 fail $test
370 return
371 }
372
373 set exec_output [binutils_run $NM "-a $NMFLAGS $objfile"]
84938e2e 374 if ![string match "" $exec_output] {
252b5132
RH
375 fail $test
376 return
377 }
378
379 pass $test
380}
381
382strip_test
383
384# Test stripping an object file with saving a symbol
385
386proc strip_test_with_saving_a_symbol { } {
387 global CC
388 global STRIP
389 global STRIPFLAGS
390 global NM
391 global NMFLAGS
392 global srcdir
393 global subdir
394
395 set test "strip with saving a symbol"
396
397 if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
398 untested $test
399 return
400 }
401
402 if [is_remote host] {
403 set objfile [remote_download host tmpdir/testprog.o];
404 } else {
405 set objfile tmpdir/testprog.o
406 }
407
408 set exec_output [binutils_run $STRIP "$STRIPFLAGS -K main -K _main $objfile"]
409 if ![string match "" $exec_output] {
410 fail $test
411 return
412 }
413
414 set exec_output [binutils_run $NM "$NMFLAGS $objfile"]
195fe636 415 if {![regexp {^([0-9a-fA-F]+)?[ ]+[TD] main} $exec_output] \
252b5132
RH
416 && ![regexp {^([0-9a-fA-F]+)?[ ]+T _main} $exec_output]} {
417 fail $test
418 return
419 }
420
421 pass $test
422}
423
424strip_test_with_saving_a_symbol
425
426# Build a final executable.
427
428proc copy_setup { } {
429 global srcdir
430 global subdir
9a580d9a 431 global gcc_gas_flag
252b5132
RH
432
433 set res [build_wrapper testglue.o];
434 set flags { debug };
435
436 if { $res != "" } {
437 lappend flags "additional_flags=[lindex $res 1]";
438 set add_libs "testglue.o";
439 } else {
440 set add_libs "";
441 }
442
9a580d9a
L
443 if { [istarget *-*-linux*] } {
444 foreach i $gcc_gas_flag {
445 set flags "additional_flags=$i $flags"
446 }
447 }
252b5132
RH
448 if { [target_compile "$srcdir/$subdir/testprog.c $add_libs" tmpdir/testprog executable $flags] != "" } {
449 return 2
450 }
451
452 set result [remote_load target tmpdir/testprog];
453 set status [lindex $result 0];
454
455 if { $status != "pass" } {
456 perror "unresolved setup, status = $status"
457 return 3
458 }
459
460 return 0
461}
462
463# Test copying an executable.
464
465proc copy_executable { prog flags test1 test2 } {
466
467 if [is_remote host] {
468 set testfile [remote_download host tmpdir/testprog];
469 set testcopy copyprog
470 } else {
471 set testfile tmpdir/testprog
472 set testcopy tmpdir/copyprog
473 }
474 remote_file host delete $testcopy;
475
476 set exec_output [binutils_run $prog "$flags $testfile $testcopy"]
477
478 if ![string match "" $exec_output] {
479 fail $test1
480 fail $test2
481 return
482 }
483
484 if [is_remote host] {
485 remote_upload host $testcopy tmpdir/copyprog
486 }
487
488 set status [remote_exec build "cmp" "tmpdir/testprog tmpdir/copyprog"]
489 set exec_output [lindex $status 1];
490
491 if [string match "" $exec_output] then {
492 pass $test1
493 } else {
494 send_log "$exec_output\n"
495 verbose "$exec_output"
496
497 # This will fail for many reasons. For example, it will most
498 # likely fail if a non-GNU linker is used. Therefore, we do
499 # not insist that it pass. If you are using an assembler and
500 # linker based on the same BFD as objcopy, it is worth
501 # investigating to see why this failure occurs. If we are
502 # cross compiling, we assume that a GNU linker is being used,
503 # and expect it to succeed.
504 if {[isnative]} then {
505 setup_xfail "*-*-*"
506 }
507
508 # This also fails for mips*-*-elf targets. See elf32-mips.c
509 # mips_elf_sym_is_global.
510 setup_xfail "mips*-*-elf"
511
9da7f49f 512 setup_xfail "*arm*-*-coff"
70bbfa26 513 setup_xfail "xscale-*-coff"
11d47adc 514 setup_xfail "arm*-*-pe"
c58c5ee7
NC
515 setup_xfail "thumb*-*-coff"
516 setup_xfail "thumb*-*-pe"
11d47adc 517
252b5132
RH
518 fail $test1
519 }
520
521 set output [remote_load target tmpdir/copyprog]
522 set status [lindex $output 0];
523 if { $status != "pass" } {
524 fail $test2
525 } else {
526 pass $test2
527 }
528}
529
530# Test stripping an executable
531
532proc strip_executable { prog flags test } {
533 global NM
534 global NMFLAGS
535
536 remote_download build tmpdir/copyprog tmpdir/striprog
537 if [is_remote host] {
538 set copyfile [remote_download host tmpdir/striprog];
539 } else {
540 set copyfile tmpdir/striprog
541 }
542
543 set exec_output [binutils_run $prog "$flags ${copyfile}"]
544 if ![string match "" $exec_output] {
545 fail $test
546 return
547 }
548
549 if [is_remote host] {
550 remote_upload host ${copyfile} tmpdir/striprog;
551 }
552
553 set result [remote_load target tmpdir/striprog]
554 set status [lindex $result 0];
555 if { $status != "pass" } {
556 fail $test
557 return
558 }
559
560 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
84938e2e 561 if ![string match "" $exec_output] {
252b5132
RH
562 fail $test
563 return
564 }
565 pass $test
566}
567
568# Test stripping an executable with saving a symbol
569
570proc strip_executable_with_saving_a_symbol { prog flags test } {
571 global NM
572 global NMFLAGS
573
574 remote_download build tmpdir/copyprog tmpdir/striprog
575 if [is_remote host] {
576 set copyfile [remote_download host tmpdir/striprog];
577 } else {
578 set copyfile tmpdir/striprog
579 }
580
581 set exec_output [binutils_run $prog "$flags ${copyfile}"]
582 if ![string match "" $exec_output] {
583 fail $test
584 return
585 }
586
587 if [is_remote host] {
588 remote_upload host ${copyfile} tmpdir/striprog;
589 }
590
591 set result [remote_load target tmpdir/striprog]
592 set status [lindex $result 0];
593 if { $status != "pass" } {
594 fail $test
595 return
596 }
597
598 set exec_output [binutils_run $NM "$NMFLAGS ${copyfile}"]
edc45ab9
HPN
599 if { [istarget mmix-knuth-mmixware] } {
600 # Whenever there's a symbol in the mmo format, there's the symbol
601 # Main, so remove it manually from the expected output for sake of
602 # this test.
603
604 # Using "" not {} to get the \n and \r translated.
605 regsub "^\[0-9a-fA-F\]+\[ \]+T Main\[\n\r\]+" $exec_output "" exec_output
606 }
607
252b5132
RH
608 if {![regexp {^[0-9a-fA-F]+ T main} $exec_output] \
609 && ![regexp {^[0-9a-fA-F]+ T _main} $exec_output]} {
610 fail $test
611 return
612 }
613 pass $test
614}
615
616set test1 "simple objcopy of executable"
617set test2 "run objcopy of executable"
618set test3 "run stripped executable"
619set test4 "run stripped executable with saving a symbol"
620
621switch [copy_setup] {
622 "1" {
623 # do nothing
624 }
625 "2" {
626 untested $test1
627 untested $test2
628 untested $test3
629 untested $test4
630 }
631 "3" {
632 unresolved $test1
633 unresolved $test2
634 unresolved $test3
635 unresolved $test4
636 }
637 "0" {
638 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
639 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
640 strip_executable_with_saving_a_symbol "$STRIP" "-K main -K _main $STRIPFLAGS" "$test4"
641 }
642}
This page took 0.141659 seconds and 4 git commands to generate.