* binutils-all/objcopy.exp: If assembly fails, call unresolved.
[platform/upstream/binutils.git] / binutils / testsuite / binutils-all / objcopy.exp
1 #   Copyright (C) 1994 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-*-sysv*" "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         fail "objcopy -O srec (bad header)"
92     } else {
93         while {[gets $file line] != -1 \
94                && [regexp "^S\[123\]\[0-9a-fA-F\]+\r$" $line]} {
95             send_log "$line\n"
96             verbose $line
97             set line "**EOF**"
98         }
99         send_log "$line\n"
100         verbose $line
101         if ![regexp "^S\[789\]\[0-9a-fA-F\]+\r$" $line] then {
102             fail "objcopy -O srec (bad trailer)"
103         } else {
104             if {[gets $file line] != -1} then {
105                 send_log "$line\n"
106                 verbose $line
107                 fail "objcopy -O srec (garbage at end)"
108             } else {
109                 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
110                 if ![regexp "file format srec" $got] then {
111                     fail "objcopy -O srec (objdump failed)"
112                 } else {
113                     pass "objcopy -O srec"
114                 }
115             }
116         }
117     }
118
119     close $file
120 }
121
122 # Test setting and adjusting the start address.  We only test this
123 # while generating S records, because we may not be able to set the
124 # start address for other object file formats, and the S record case
125 # is the only useful one anyhow.
126
127 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/bintest.o"]
128 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
129     perror "objdump can not recognize bintest.o"
130     set origstart ""
131 } else {
132     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --set-start 0x7654"]
133     if ![string match "" $got] then {
134         fail "objcopy --set-start"
135     } else {
136         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
137         if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
138             fail "objcopy --set-start"
139         } else {
140             if {$srecstart != 0x7654} then {
141                 fail "objcopy --set-start ($srecstart != 0x7654)"
142             } else {
143                 pass "objcopy --set-start"
144             }
145         }
146     }
147
148     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-start 0x123"]
149     if ![string match "" $got] then {
150         fail "objcopy --adjust-start"
151     } else {
152         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
153         if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
154             fail "objcopy --adjust-start"
155         } else {
156             if {$srecstart != $origstart + 0x123} then {
157                 fail "objcopy --adjust-start ($srecstart != $origstart + 0x123)"
158             } else {
159                 pass "objcopy --adjust-start"
160             }
161         }
162     }
163 }
164
165 # Test adjusting the overall VMA, and adjusting the VMA of a
166 # particular section.  We again only test this when # generating S
167 # records.
168
169 set low ""
170 set lowname ""
171
172 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/bintest.o"]
173
174 set headers_regexp "SECTION\[   0-9\]+\\\[(\[^\]\]*)\\\]\[^\n\r\]*size\[        \]*(\[0-9a-fA-F\]+)\[   \]*vma\[        \]*(\[0-9a-fA-F\]+)(.*)"
175
176 set got $headers
177 while {[regexp $headers_regexp $got all name size vma rest]} {
178     set vma 0x$vma
179     if {$low == "" || $vma < $low} then {
180         set low $vma
181         set lowname $name
182     }
183     set got $rest
184 }
185
186 if {$low == "" || $origstart == ""} then {
187     perror "objdump can not recognize bintest.o"
188 } else {
189     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-vma 0x123"]
190     if ![string match "" $got] then {
191         fail "objcopy --adjust-vma"
192     } else {
193         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh tmpdir/copy.srec"]
194         set want "file format srec.*start address\[     \]*(\[0-9a-fA-FxX\]+).*vma\[    \]*(\[0-9a-fA-F\]+)"
195         if ![regexp $want $got all start vma] then {
196             fail "objcopy --adjust-vma"
197         } else {
198             set vma 0x$vma
199             if {$vma != $low + 0x123} then {
200                 fail "objcopy --adjust-vma ($vma != $low + 0x123)"
201             } else {
202                 if {$start != $origstart + 0x123} then {
203                     fail "objcopy --adjust-vma ($start != $origstart + 0x123)"
204                 } else {
205                     pass "objcopy --adjust-vma"
206                 }
207             }
208         }
209     }
210
211     set arg ""
212     set got $headers
213     while {[regexp $headers_regexp $got all name size vma rest]} {
214         set vma 0x$vma
215         if {$vma == $low} then {
216             set arg "$arg --adjust-section-vma $name+4"
217         }
218         set got $rest
219     }
220
221     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $arg"]
222     if ![string match "" $got] then {
223         fail "objcopy --adjust-section-vma +"
224     } else {
225         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
226         set want "file format srec.*SECTION\[   \]*0\[^\n\r\]*vma\[     \]*(\[0-9a-fA-F\]+)"
227         if ![regexp $want $got all vma] then {
228             fail "objcopy --adjust-section-vma +"
229         } else {
230             set vma 0x$vma
231             if {$vma != $low + 4} then {
232                 fail "objcopy --adjust-section-vma + ($vma != $low + 4)"
233             } else {
234                 pass "objcopy --adjust-section-vma +"
235             }
236         }
237     }
238
239     regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
240     set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $argeq"]
241     if ![string match "" $got] then {
242         fail "objcopy --adjust-section-vma ="
243     } else {
244         set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
245         set want "file format srec.*SECTION\[   \]*0\[^\n\r\]*vma\[     \]*(\[0-9a-fA-F\]+)"
246         if ![regexp $want $got all vma] then {
247             fail "objcopy --adjust-section-vma ="
248         } else {
249             set vma 0x$vma
250             if {$vma != $low + 4} then {
251                 fail "objcopy --adjust-section-vma = ($vma != $low + 4)"
252             } else {
253                 pass "objcopy --adjust-section-vma ="
254             }
255         }
256     }
257 }
258
259 # Test stripping an object.
260
261 proc strip_test { } {
262     global CC
263     global STRIP
264     global STRIPFLAGS
265     global NM
266     global NMFLAGS
267     global srcdir
268     global subdir
269
270     set test "strip"
271
272     if { [which $CC] == 0 } {
273         untested $test
274         return
275     }
276
277     if ![binutils_compile $CC "-g -c" $srcdir/$subdir/testprog.c tmpdir/testprog.o] {
278         unresolved $test
279         return
280     }
281
282     set exec_output [binutils_run $STRIP "$STRIPFLAGS tmpdir/testprog.o"]
283     if ![string match "" $exec_output] {
284         fail $test
285         return
286     }
287
288     set exec_output [binutils_run $NM "-a $NMFLAGS tmpdir/testprog.o"]
289     if ![string match "No symbols in *" $exec_output] {
290         fail $test
291         return
292     }
293
294     pass $test
295 }
296
297 strip_test
298
299 # Build a final executable.
300
301 proc copy_setup { } {
302     global CC
303     global srcdir
304     global subdir
305
306     if ![isnative] {
307         return 1
308     }
309
310     if { [which $CC] == 0 } {
311         return 2
312     }
313
314     if ![binutils_compile $CC "-g" $srcdir/$subdir/testprog.c tmpdir/testprog] {
315         return 3
316     }
317
318     set exec_output [binutils_run tmpdir/testprog ""]
319     if ![string match "ok" $exec_output] {
320         return 3
321     }
322
323     return 0
324 }
325
326 # Test copying an executable.
327
328 proc copy_executable { prog flags test1 test2 } {
329
330     set exec_output [binutils_run $prog "$flags tmpdir/testprog tmpdir/copyprog"]
331     if ![string match "" $exec_output] {
332         fail $test1
333         fail $test2
334         return
335     }
336
337     set exec_output [binutils_run "cmp" "tmpdir/testprog tmpdir/copyprog"]
338
339     if [string match "" $exec_output] then {
340         pass $test1
341     } else {
342         send_log "$exec_output\n"
343         verbose "$exec_output"
344         # This will fail for many reasons.  For example, it will most
345         # likely fail if the system linker is used.  Therefore, we do
346         # not insist that it pass.  If you are using an assembler and
347         # linker based on the same BFD as objcopy, it is worth
348         # investigating to see why this failure occurs.
349         setup_xfail "*-*-*"
350         fail $test1
351     }
352
353     set exec_output [binutils_run tmpdir/copyprog ""]
354     if ![string match "ok" $exec_output] {
355         fail $test2
356     } else {
357         pass $test2
358     }
359 }
360
361 # Test stripping an executable
362
363 proc strip_executable { prog flags test } {
364
365     set exec_output [binutils_run $prog "$flags tmpdir/copyprog"]
366     if ![string match "" $exec_output] {
367         fail $test
368         return
369     }
370
371     set exec_output [binutils_run tmpdir/copyprog ""]
372     if ![string match "ok" $exec_output] {
373         fail $test
374     } else {
375         pass $test
376     }
377 }
378
379 set test1 "simple objcopy of executable"
380 set test2 "run objcopy of executable"
381 set test3 "run stripped executable"
382
383 switch [copy_setup] {
384     "1" {
385         # do nothing
386     }
387     "2" {
388         untested $test1
389         untested $test2
390         untested $test3
391     }
392     "3" {
393         unresolved $test1
394         unresolved $test2
395         unresolved $test3
396     }
397     "0" {
398         copy_executable "$OBJCOPY" "$OBJCOPYFLAGS" "$test1" "$test2"
399         strip_executable "$STRIP" "$STRIPFLAGS" "$test3"
400     }
401 }