a6e94c7aca94efc4c8a9df1f363e52c01be32b5d
[deliverable/binutils-gdb.git] / binutils / testsuite / binutils-all / objcopy.exp
1 # Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
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
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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
22 if {[which $OBJCOPY] == 0} then {
23 perror "$OBJCOPY does not exist"
24 return
25 }
26
27 send_user "Version [binutil_version $OBJCOPY]"
28
29 if {![binutils_assemble $AS $srcdir$subdir/bintest.s tmpdir/bintest.o]} then {
30 unresolved "objcopy (simple copy)"
31 return
32 }
33
34 # Test that objcopy does not modify a file when copying it.
35
36 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS tmpdir/bintest.o tmpdir/copy.o"]
37
38 if ![string match "" $got] then {
39 fail "objcopy (simple copy)"
40 } else {
41 send_log "cmp tmpdir/bintest.o tmpdir/copy.o\n"
42 verbose "cmp tmpdir/bintest.o tmpdir/copy.o"
43 catch "exec cmp tmpdir/bintest.o tmpdir/copy.o" exec_output
44 set exec_output [prune_system_crud $host_triplet $exec_output]
45
46 # On some systems the result of objcopy will not be identical.
47 # Usually this is just because gas isn't using bfd to write the files
48 # in the first place, and may order things a little differently.
49 # Those systems should use setup_xfail here.
50
51 setup_xfail "sh-*-coff" "sh-*-hms"
52 setup_xfail "arm-*-pe"
53 setup_xfail "m68*-*-hpux*" "m68*-*-sunos*" "m68*-*-coff" "m68*-*-vxworks*"
54 setup_xfail "m68*-ericsson-ose" "m68k*-motorola-sysv*"
55 setup_xfail "i*86-*-linuxaout*" "i*86-*-aout*"
56 setup_xfail "i*86-*-sysv3" "i*86-*-isc*" "i*86-*-sco*" "i*86-*-coff"
57 setup_xfail "i*86-*-aix*" "i*86-*-go32*"
58 setup_xfail "a29k-*-udi" "a29k-*-coff" "a29k-*-vxworks*"
59 setup_xfail "i960-*-vxworks*" "i960-*-coff"
60 setup_xfail "h8300-*-hms" "h8300-*-coff"
61 setup_xfail "h8500-*-hms" "h8500-*-coff"
62 setup_xfail "hppa*-*-*"
63 clear_xfail "hppa*-*-*elf*"
64 setup_xfail "m88*-*-coff" "m88*-motorola-sysv*"
65 setup_xfail "z8*-*-coff"
66
67 if [string match "" $exec_output] then {
68 pass "objcopy (simple copy)"
69 } else {
70 send_log "$exec_output\n"
71 verbose "$exec_output" 1
72
73 fail "objcopy (simple copy)"
74 }
75 }
76
77 # Test generating S records.
78
79 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec"]
80
81 if ![string match "" $got] then {
82 fail "objcopy -O srec"
83 } else {
84 set file [open tmpdir/copy.srec r]
85
86 # The first S record is fixed by the file name we are using.
87 gets $file line
88 send_log "$line\n"
89 verbose $line
90 if ![string match $line "S0130000746D706469722F636F70792E7372656397\r"] {
91 send_log "bad header\n"
92 fail "objcopy -O srec"
93 } else {
94 while {[gets $file line] != -1 \
95 && [regexp "^S\[123\]\[0-9a-fA-F\]+\r$" $line]} {
96 send_log "$line\n"
97 verbose $line
98 set line "**EOF**"
99 }
100 send_log "$line\n"
101 verbose $line
102 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\r$" $line] then {
103 send_log "bad trailer\n"
104 fail "objcopy -O srec"
105 } else {
106 if {[gets $file line] != -1} then {
107 send_log "garbage at end\n"
108 send_log "$line\n"
109 verbose $line
110 fail "objcopy -O srec"
111 } else {
112 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
113 if ![regexp "file format srec" $got] then {
114 send_log "objdump failed\n"
115 fail "objcopy -O srec"
116 } else {
117 pass "objcopy -O srec"
118 }
119 }
120 }
121 }
122
123 close $file
124 }
125
126 # Test setting and adjusting the start address. We only test this
127 # while generating S records, because we may not be able to set the
128 # start address for other object file formats, and the S record case
129 # is the only useful one anyhow.
130
131 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/bintest.o"]
132 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
133 perror "objdump can not recognize bintest.o"
134 set origstart ""
135 } else {
136 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --set-start 0x7654"]
137 if ![string match "" $got] then {
138 fail "objcopy --set-start"
139 } else {
140 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
141 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
142 fail "objcopy --set-start"
143 } else {
144 if {$srecstart != 0x7654} then {
145 send_log "$srecstart != 0x7654\n"
146 fail "objcopy --set-start"
147 } else {
148 pass "objcopy --set-start"
149 }
150 }
151 }
152
153 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-start 0x123"]
154 if ![string match "" $got] then {
155 fail "objcopy --adjust-start"
156 } else {
157 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
158 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
159 fail "objcopy --adjust-start"
160 } else {
161 if {$srecstart != $origstart + 0x123} then {
162 send_log "$srecstart != $origstart + 0x123\n"
163 fail "objcopy --adjust-start"
164 } else {
165 pass "objcopy --adjust-start"
166 }
167 }
168 }
169 }
170
171 # Test adjusting the overall VMA, and adjusting the VMA of a
172 # particular section. We again only test this when generating S
173 # records.
174
175 set low ""
176 set lowname ""
177
178 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/bintest.o"]
179
180 set headers_regexp "SECTION\[ 0-9\]+\\\[(\[^\]\]*)\\\]\[^\n\r\]*size\[ \]*(\[0-9a-fA-F\]+)\[0-9a-zA-Z \]*lma\[ \]*(\[0-9a-fA-F\]+)(.*)"
181
182 set got $headers
183 while {[regexp $headers_regexp $got all name size vma rest]} {
184 set vma 0x$vma
185 set size 0x$size
186 if {$size != 0} {
187 if {$low == "" || $vma < $low} {
188 set low $vma
189 set lowname $name
190 }
191 }
192 set got $rest
193 }
194
195 if {$low == "" || $origstart == ""} then {
196 perror "objdump can not recognize bintest.o"
197 } else {
198 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-vma 0x123"]
199 if ![string match "" $got] then {
200 fail "objcopy --adjust-vma"
201 } else {
202 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh tmpdir/copy.srec"]
203 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*vma\[ \]*(\[0-9a-fA-F\]+)"
204 if ![regexp $want $got all start vma] then {
205 fail "objcopy --adjust-vma"
206 } else {
207 set vma 0x$vma
208 if {$vma != $low + 0x123} then {
209 send_log "$vma != $low + 0x123\n"
210 fail "objcopy --adjust-vma"
211 } else {
212 if {$start != $origstart + 0x123} then {
213 send_log "$start != $origstart + 0x123\n"
214 fail "objcopy --adjust-vma"
215 } else {
216 pass "objcopy --adjust-vma"
217 }
218 }
219 }
220 }
221
222 set arg ""
223 set got $headers
224 while {[regexp $headers_regexp $got all name size vma rest]} {
225 set vma 0x$vma
226 if {$vma == $low} then {
227 set arg "$arg --adjust-section-vma $name+4"
228 }
229 set got $rest
230 }
231
232 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $arg"]
233 if ![string match "" $got] then {
234 fail "objcopy --adjust-section-vma +"
235 } else {
236 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
237 set want "file format srec.*SECTION\[ \]*0\[^\n\r\]*vma\[ \]*(\[0-9a-fA-F\]+)"
238 if ![regexp $want $got all vma] then {
239 fail "objcopy --adjust-section-vma +"
240 } else {
241 set vma 0x$vma
242 if {$vma != $low + 4} then {
243 send_log "$vma != $low + 4\n"
244 fail "objcopy --adjust-section-vma +"
245 } else {
246 pass "objcopy --adjust-section-vma +"
247 }
248 }
249 }
250
251 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
252 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $argeq"]
253 if ![string match "" $got] then {
254 fail "objcopy --adjust-section-vma ="
255 } else {
256 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
257 set want "file format srec.*SECTION\[ \]*0\[^\n\r\]*vma\[ \]*(\[0-9a-fA-F\]+)"
258 if ![regexp $want $got all vma] then {
259 fail "objcopy --adjust-section-vma ="
260 } else {
261 set vma 0x$vma
262 if {$vma != $low + 4} then {
263 send_log "$vma != $low + 4\n"
264 fail "objcopy --adjust-section-vma ="
265 } else {
266 pass "objcopy --adjust-section-vma ="
267 }
268 }
269 }
270 }
271
272 # Test stripping an object.
273
274 proc strip_test { } {
275 global CC
276 global STRIP
277 global STRIPFLAGS
278 global NM
279 global NMFLAGS
280 global srcdir
281 global subdir
282
283 set test "strip"
284
285 if { [which $CC] == 0 } {
286 untested $test
287 return
288 }
289
290 if ![binutils_compile $CC "-g -c" $srcdir/$subdir/testprog.c tmpdir/testprog.o] {
291 unresolved $test
292 return
293 }
294
295 set exec_output [binutils_run $STRIP "$STRIPFLAGS tmpdir/testprog.o"]
296 if ![string match "" $exec_output] {
297 fail $test
298 return
299 }
300
301 set exec_output [binutils_run $NM "-a $NMFLAGS tmpdir/testprog.o"]
302 if ![string match "No symbols in *" $exec_output] {
303 fail $test
304 return
305 }
306
307 pass $test
308 }
309
310 strip_test
311
312 # Build a final executable.
313
314 proc copy_setup { } {
315 global CC
316 global srcdir
317 global subdir
318
319 if ![isnative] {
320 return 1
321 }
322
323 if { [which $CC] == 0 } {
324 return 2
325 }
326
327 if ![binutils_compile $CC "-g" $srcdir/$subdir/testprog.c tmpdir/testprog] {
328 return 3
329 }
330
331 set exec_output [binutils_run tmpdir/testprog ""]
332 if ![string match "ok" $exec_output] {
333 return 3
334 }
335
336 return 0
337 }
338
339 # Test copying an executable.
340
341 proc copy_executable { prog flags test1 test2 } {
342
343 set exec_output [binutils_run $prog "$flags tmpdir/testprog tmpdir/copyprog"]
344 if ![string match "" $exec_output] {
345 fail $test1
346 fail $test2
347 return
348 }
349
350 set exec_output [binutils_run "cmp" "tmpdir/testprog tmpdir/copyprog"]
351
352 if [string match "" $exec_output] then {
353 pass $test1
354 } else {
355 send_log "$exec_output\n"
356 verbose "$exec_output"
357 # This will fail for many reasons. For example, it will most
358 # likely fail if the system linker is used. Therefore, we do
359 # not insist that it pass. If you are using an assembler and
360 # linker based on the same BFD as objcopy, it is worth
361 # investigating to see why this failure occurs.
362 setup_xfail "*-*-*"
363 fail $test1
364 }
365
366 set exec_output [binutils_run tmpdir/copyprog ""]
367 if ![string match "ok" $exec_output] {
368 fail $test2
369 } else {
370 pass $test2
371 }
372 }
373
374 # Test stripping an executable
375
376 proc strip_executable { prog flags test } {
377
378 set exec_output [binutils_run $prog "$flags tmpdir/copyprog"]
379 if ![string match "" $exec_output] {
380 fail $test
381 return
382 }
383
384 set exec_output [binutils_run tmpdir/copyprog ""]
385 if ![string match "ok" $exec_output] {
386 fail $test
387 } else {
388 pass $test
389 }
390 }
391
392 set test1 "simple objcopy of executable"
393 set test2 "run objcopy of executable"
394 set test3 "run stripped executable"
395
396 switch [copy_setup] {
397 "1" {
398 # do nothing
399 }
400 "2" {
401 untested $test1
402 untested $test2
403 untested $test3
404 }
405 "3" {
406 unresolved $test1
407 unresolved $test2
408 unresolved $test3
409 }
410 "0" {
411 copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
412 strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
413 }
414 }
This page took 0.037602 seconds and 3 git commands to generate.