* lib/utils-lib.exp (prune_system_crud): Define if not defined.
[binutils-gdb.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
30 if {![binutils_assemble $AS $srcdir$subdir/bintest.s tmpdir/bintest.o]} then {
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"
55 setup_xfail "i*86-*-linuxaout*" "i*86-*-aout*"
56 setup_xfail "a29k-*-udi" "a29k-*-coff" "a29k-*-vxworks*"
57 setup_xfail "i960-*-vxworks*" "i960-*-coff"
58 setup_xfail "h8300-*-hms" "h8300-*-coff"
59 setup_xfail "h8500-*-hms" "h8500-*-coff"
60 setup_xfail "hppa*-*-*"
61 clear_xfail "hppa*-*-*elf*"
62 setup_xfail "m88*-*-coff"
63 setup_xfail "z8*-*-coff"
64
65 if [string match "" $exec_output] then {
66 pass "objcopy (simple copy)"
67 } else {
68 send_log "$exec_output\n"
69 verbose "$exec_output" 1
70
71 fail "objcopy (simple copy)"
72 }
73 }
74
75 # Test generating S records.
76
77 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec"]
78
79 if ![string match "" $got] then {
80 fail "objcopy -O srec"
81 } else {
82 set file [open tmpdir/copy.srec r]
83
84 # The first S record is fixed by the file name we are using.
85 gets $file line
86 send_log "$line\n"
87 verbose $line
88 if ![string match $line "S0130000746D706469722F636F70792E7372656397\r"] {
89 fail "objcopy -O srec (bad header)"
90 } else {
91 while {[gets $file line] != -1 \
92 && [regexp "^S\[123\]\[0-9a-fA-F\]+\r$" $line]} {
93 send_log "$line\n"
94 verbose $line
95 set line "**EOF**"
96 }
97 send_log "$line\n"
98 verbose $line
99 if ![regexp "^S\[789\]\[0-9a-fA-F\]+\r$" $line] then {
100 fail "objcopy -O srec (bad trailer)"
101 } else {
102 if {[gets $file line] != -1} then {
103 send_log "$line\n"
104 verbose $line
105 fail "objcopy -O srec (garbage at end)"
106 } else {
107 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
108 if ![regexp "file format srec" $got] then {
109 fail "objcopy -O srec (objdump failed)"
110 } else {
111 pass "objcopy -O srec"
112 }
113 }
114 }
115 }
116
117 close $file
118 }
119
120 # Test setting and adjusting the start address. We only test this
121 # while generating S records, because we may not be able to set the
122 # start address for other object file formats, and the S record case
123 # is the only useful one anyhow.
124
125 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/bintest.o"]
126 if ![regexp "start address (\[0-9a-fA-FxX\]+)" $got all origstart] then {
127 perror "objdump can not recognize bintest.o"
128 set origstart ""
129 } else {
130 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --set-start 0x7654"]
131 if ![string match "" $got] then {
132 fail "objcopy --set-start"
133 } else {
134 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
135 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
136 fail "objcopy --set-start"
137 } else {
138 if {$srecstart != 0x7654} then {
139 fail "objcopy --set-start ($srecstart != 0x7654)"
140 } else {
141 pass "objcopy --set-start"
142 }
143 }
144 }
145
146 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-start 0x123"]
147 if ![string match "" $got] then {
148 fail "objcopy --adjust-start"
149 } else {
150 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f tmpdir/copy.srec"]
151 if ![regexp "file format srec.*start address (\[0-9a-fA-FxX\]+)" $got all srecstart] then {
152 fail "objcopy --adjust-start"
153 } else {
154 if {$srecstart != $origstart + 0x123} then {
155 fail "objcopy --adjust-start ($srecstart != $origstart + 0x123)"
156 } else {
157 pass "objcopy --adjust-start"
158 }
159 }
160 }
161 }
162
163 # Test adjusting the overall VMA, and adjusting the VMA of a
164 # particular section. We again only test this when # generating S
165 # records.
166
167 set low ""
168 set lowname ""
169
170 set headers [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/bintest.o"]
171
172 set headers_regexp "SECTION\[ 0-9\]+\\\[(\[^\]\]*)\\\]\[^\n\r\]*size\[ \]*(\[0-9a-fA-F\]+)\[ \]*vma\[ \]*(\[0-9a-fA-F\]+)(.*)"
173
174 set got $headers
175 while {[regexp $headers_regexp $got all name size vma rest]} {
176 set vma 0x$vma
177 if {$low == "" || $vma < $low} then {
178 set low $vma
179 set lowname $name
180 }
181 set got $rest
182 }
183
184 if {$low == "" || $origstart == ""} then {
185 perror "objdump can not recognize bintest.o"
186 } else {
187 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec --adjust-vma 0x123"]
188 if ![string match "" $got] then {
189 fail "objcopy --adjust-vma"
190 } else {
191 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -fh tmpdir/copy.srec"]
192 set want "file format srec.*start address\[ \]*(\[0-9a-fA-FxX\]+).*vma\[ \]*(\[0-9a-fA-F\]+)"
193 if ![regexp $want $got all start vma] then {
194 fail "objcopy --adjust-vma"
195 } else {
196 set vma 0x$vma
197 if {$vma != $low + 0x123} then {
198 fail "objcopy --adjust-vma ($vma != $low + 0x123)"
199 } else {
200 if {$start != $origstart + 0x123} then {
201 fail "objcopy --adjust-vma ($start != $origstart + 0x123)"
202 } else {
203 pass "objcopy --adjust-vma"
204 }
205 }
206 }
207 }
208
209 set arg ""
210 set got $headers
211 while {[regexp $headers_regexp $got all name size vma rest]} {
212 set vma 0x$vma
213 if {$vma == $low} then {
214 set arg "$arg --adjust-section-vma $name+4"
215 }
216 set got $rest
217 }
218
219 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $arg"]
220 if ![string match "" $got] then {
221 fail "objcopy --adjust-section-vma +"
222 } else {
223 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
224 set want "file format srec.*SECTION\[ \]*0\[^\n\r\]*vma\[ \]*(\[0-9a-fA-F\]+)"
225 if ![regexp $want $got all vma] then {
226 fail "objcopy --adjust-section-vma +"
227 } else {
228 set vma 0x$vma
229 if {$vma != $low + 4} then {
230 fail "objcopy --adjust-section-vma + ($vma != $low + 4)"
231 } else {
232 pass "objcopy --adjust-section-vma +"
233 }
234 }
235 }
236
237 regsub -all "\\+4" $arg "=[expr $low + 4]" argeq
238 set got [binutils_run $OBJCOPY "$OBJCOPYFLAGS -O srec tmpdir/bintest.o tmpdir/copy.srec $argeq"]
239 if ![string match "" $got] then {
240 fail "objcopy --adjust-section-vma ="
241 } else {
242 set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h tmpdir/copy.srec"]
243 set want "file format srec.*SECTION\[ \]*0\[^\n\r\]*vma\[ \]*(\[0-9a-fA-F\]+)"
244 if ![regexp $want $got all vma] then {
245 fail "objcopy --adjust-section-vma ="
246 } else {
247 set vma 0x$vma
248 if {$vma != $low + 4} then {
249 fail "objcopy --adjust-section-vma = ($vma != $low + 4)"
250 } else {
251 pass "objcopy --adjust-section-vma ="
252 }
253 }
254 }
255 }