Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | # Test linking directly to S-records. |
2 | # By Ian Lance Taylor, Cygnus Support. | |
f96b4a7b | 3 | # Copyright 1999, 2000, 2001, 2002, 2003, 2006, 2007 |
a2b64bed NC |
4 | # Free Software Foundation, Inc. |
5 | # | |
f96b4a7b NC |
6 | # This file is part of the GNU Binutils. |
7 | # | |
8 | # This program is free software; you can redistribute it and/or modify | |
a2b64bed | 9 | # it under the terms of the GNU General Public License as published by |
f96b4a7b | 10 | # the Free Software Foundation; either version 3 of the License, or |
a2b64bed | 11 | # (at your option) any later version. |
f96b4a7b | 12 | # |
a2b64bed NC |
13 | # This program is distributed in the hope that it will be useful, |
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | # GNU General Public License for more details. | |
f96b4a7b | 17 | # |
a2b64bed NC |
18 | # You should have received a copy of the GNU General Public License |
19 | # along with this program; if not, write to the Free Software | |
f96b4a7b NC |
20 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
21 | # MA 02110-1301, USA. | |
252b5132 RH |
22 | |
23 | # Get the offset from an S-record line to the start of the data. | |
24 | ||
25 | proc srec_off { l } { | |
26 | if [string match "S1*" $l] { | |
27 | return 8 | |
28 | } else { if [string match "S2*" $l] { | |
29 | return 10 | |
30 | } else { if [string match "S3*" $l] { | |
31 | return 12 | |
32 | } else { | |
33 | return -1 | |
34 | } } } | |
35 | } | |
36 | ||
37 | # See if an S-record line contains only zero data. | |
38 | ||
39 | proc srec_zero { l } { | |
40 | if [string match "S\[0789\]*" $l] { | |
41 | return 1 | |
42 | } | |
43 | ||
44 | # Strip the address and checksum. | |
45 | if [string match "S\[123\]*" $l] { | |
46 | set l [string range $l [srec_off $l] [expr [string length $l] - 3]] | |
47 | } else { | |
48 | return 0 | |
49 | } | |
50 | ||
51 | # The rest must be zero. | |
52 | return [string match "" [string trim $l "0"]] | |
53 | } | |
54 | ||
55 | # Get the address of an S-record line. | |
56 | ||
57 | proc srec_addr { l } { | |
58 | if [string match "S\[123\]*" $l] { | |
59 | set addr [string range $l 4 [expr [srec_off $l] - 1]] | |
60 | } else { | |
61 | return -1 | |
62 | } | |
63 | ||
64 | return "0x$addr" | |
65 | } | |
66 | ||
67 | # Get the number of data bytes in an S-record line. | |
68 | ||
69 | proc srec_len { l } { | |
70 | if ![string match "S\[123\]*" $l] { | |
71 | return 0 | |
72 | } | |
73 | ||
74 | return [expr "0x[string range $l 2 3]" - ([srec_off $l] - 4) / 2 - 1] | |
75 | } | |
76 | ||
77 | # Extract bytes from an S-record line. | |
78 | ||
79 | proc srec_extract { l start len } { | |
80 | set off [srec_off $l] | |
81 | set rlen [srec_len $l] | |
82 | set stop [expr $start + $len] | |
83 | if { $stop > $rlen } { | |
84 | set stop [expr $rlen] | |
85 | } | |
86 | set start [expr $start * 2 + $off] | |
87 | set stop [expr $stop * 2 + $off - 1] | |
88 | return [string range $l $start $stop] | |
89 | } | |
90 | ||
91 | # See if a range of bytes in an S-record line is all zeroes. | |
92 | ||
93 | proc srec_zero_range { l start len } { | |
94 | return [string match "" [string trim [srec_extract $l $start $len] "0"]] | |
95 | } | |
96 | ||
97 | # Trim an S-record line such that the specified number of bytes remain | |
98 | # at the end. | |
99 | ||
100 | proc srec_trim { l leave } { | |
101 | set off [srec_off $l] | |
102 | set addr [srec_addr $l] | |
103 | set len [srec_len $l] | |
104 | ||
105 | if { $leave >= $len } { | |
106 | return $l | |
107 | } | |
108 | ||
109 | set s1 [string range $l 0 1] | |
110 | set s2 [format "%02x" [expr ($off - 4) / 2 + $leave + 1]] | |
111 | set s3 [format "%0[expr $off - 4]x" [expr $addr + $len - $leave]] | |
112 | set s4 [string range $l [expr [string length $l] - ($leave * 2) - 2] end] | |
113 | set s "${s1}${s2}${s3}${s4}" | |
114 | ||
115 | verbose "srec_trim { '$l' $leave } returning '$s'" 2 | |
116 | ||
117 | return $s | |
118 | } | |
119 | ||
120 | # Report failure when comparing S-record lines | |
121 | ||
122 | proc srec_compare_fail { which l1 l2 } { | |
123 | send_log "comparison failure $which:\n$l1\n$l2\n" | |
124 | verbose "comparison failure $which:\n$l1\n$l2" | |
125 | } | |
126 | ||
127 | # Compare S-record files. We don't want to fuss about things like | |
128 | # extra zeroes. Note that BFD always sorts S-records by address. | |
129 | ||
130 | proc srec_compare { f1 f2 } { | |
131 | set e1 [gets $f1 l1] | |
132 | set e2 [gets $f2 l2] | |
133 | ||
134 | while { $e1 != -1 } { | |
135 | set l1 [string trimright $l1 "\r\n"] | |
136 | set l2 [string trimright $l2 "\r\n"] | |
137 | if { $e2 == -1 } { | |
138 | # If l1 contains data, it must be zero. | |
139 | if ![srec_zero $l1] { | |
140 | send_log "data after EOF: $l1\n" | |
141 | verbose "data after EOF: $l1" | |
142 | return 0 | |
143 | } | |
144 | } else { if { [string compare $l1 $l2] == 0 } { | |
145 | set e1 [gets $f1 l1] | |
146 | set e2 [gets $f2 l2] | |
147 | } else { if { [srec_zero $l1] } { | |
148 | set e1 [gets $f1 l1] | |
149 | } else { if { [srec_zero $l2] } { | |
150 | set e2 [gets $f2 l2] | |
151 | } else { | |
152 | # The strings are not the same, and neither is all zeroes. | |
153 | set a1 [srec_addr $l1] | |
154 | set n1 [srec_len $l1] | |
155 | set a2 [srec_addr $l2] | |
156 | set n2 [srec_len $l2] | |
157 | ||
158 | if { $a1 < $a2 && ![srec_zero_range $l1 0 [expr $a2 - $a1]] } { | |
159 | verbose "$a1 $a2 [srec_extract $l1 0 [expr $a2 - $a1]]" 2 | |
160 | srec_compare_fail 1 $l1 $l2 | |
161 | return 0 | |
162 | } | |
163 | if { $a2 < $a1 && ![srec_zero_range $l2 0 [expr $a1 - $a2]] } { | |
164 | srec_compare_fail 2 $l1 $l2 | |
165 | return 0 | |
166 | } | |
167 | ||
168 | # Here we know that any initial data in both lines is | |
169 | # zero. Now make sure that any overlapping data matches. | |
170 | if { $a1 < $a2 } { | |
171 | set os1 [expr $a2 - $a1] | |
172 | set os2 0 | |
173 | } else { | |
174 | set os1 0 | |
175 | set os2 [expr $a1 - $a2] | |
176 | } | |
177 | if { $a1 + $n1 < $a2 + $n2 } { | |
178 | set ol [expr $n1 - $os1] | |
179 | } else { | |
180 | set ol [expr $n2 - $os2] | |
181 | } | |
182 | ||
183 | set x1 [srec_extract $l1 $os1 $ol] | |
184 | set x2 [srec_extract $l2 $os2 $ol] | |
185 | if { [string compare $x1 $x2] != 0 } { | |
186 | verbose "$os1 $ol $x1" 2 | |
187 | verbose "$os2 $ol $x2" 2 | |
188 | srec_compare_fail 3 $l1 $l2 | |
189 | return 0 | |
190 | } | |
191 | ||
192 | # These strings match. Trim the data from the larger | |
193 | # string, read a new copy of the smaller string, and | |
194 | # continue. | |
195 | if { $a1 + $n1 < $a2 + $n2 } { | |
196 | set l2 [srec_trim $l2 [expr ($a2 + $n2) - ($a1 + $n1)]] | |
197 | set e1 [gets $f1 l1] | |
198 | } else { if { $a1 + $n1 > $a2 + $n2 } { | |
199 | set l1 [srec_trim $l1 [expr ($a1 + $n1) - ($a2 + $n2)]] | |
200 | set e2 [gets $f2 l2] | |
201 | } else { | |
202 | set e1 [gets $f1 l1] | |
203 | set e2 [gets $f2 l2] | |
204 | } } | |
205 | } } } } | |
206 | } | |
207 | ||
208 | # We've reached the end of the first file. The remainder of the | |
209 | # second file must contain only zeroes. | |
210 | while { $e2 != -1 } { | |
211 | set l2 [string trimright $l2 "\r\n"] | |
212 | if ![srec_zero $l2] { | |
213 | send_log "data after EOF: $l2\n" | |
214 | verbose "data after EOF: $l2" | |
215 | return 0 | |
216 | } | |
217 | set e2 [gets $f2 l2] | |
218 | } | |
219 | ||
220 | return 1 | |
221 | } | |
222 | ||
223 | # Link twice, objcopy, and compare | |
224 | ||
225 | proc run_srec_test { test objs } { | |
226 | global ld | |
227 | global objcopy | |
228 | global sizeof_headers | |
229 | global host_triplet | |
230 | ||
d2823f55 AM |
231 | # Tell the ELF linker to not do anything clever with .eh_frame. |
232 | set flags "--traditional-format" | |
252b5132 RH |
233 | |
234 | # If the linker script uses SIZEOF_HEADERS, use a -Ttext argument | |
235 | # to force both the normal link and the S-record link to be put in | |
236 | # the same place. We don't always use -Ttext because it interacts | |
237 | # poorly with a.out. | |
238 | ||
239 | if { $sizeof_headers } { | |
240 | set flags "$flags -Ttext 0x1000" | |
241 | } | |
242 | ||
1b19eb81 AO |
243 | if [istarget sh64*-*-elf] { |
244 | # This is what gcc passes to ld by default. | |
245 | set flags "-mshelf32" | |
246 | # SH64 targets cannot convert format in the linker | |
247 | # using the -oformat command line switch. | |
248 | setup_xfail "sh64*-*-*" | |
249 | } | |
250 | ||
bad19f8f NC |
251 | if {[istarget arm*-*-*] || \ |
252 | [istarget strongarm*-*-*] || \ | |
253 | [istarget xscale*-*-*] || \ | |
254 | [istarget thumb-*-*] } { | |
252b5132 | 255 | |
bad19f8f NC |
256 | # ARM targets call __gccmain |
257 | set flags "$flags --defsym __gccmain=0" | |
258 | ||
259 | # ARM targets cannot convert format in the linker | |
0fc3347a | 260 | # using the --oformat command line switch |
bad19f8f NC |
261 | setup_xfail "*arm*-*-*" |
262 | setup_xfail "xscale-*-*" | |
263 | setup_xfail "thumb-*-*" | |
252b5132 RH |
264 | } |
265 | ||
266 | # PowerPC EABI code calls __eabi. | |
267 | if [istarget powerpc*-*-eabi*] { | |
268 | set flags "$flags --defsym __eabi=0" | |
269 | } | |
270 | ||
271 | # mn10200 code calls __truncsipsi2_d0_d2. | |
272 | if {[istarget mn10200*-*-*]} then { | |
273 | set flags "$flags --defsym __truncsipsi2_d0_d2=0" | |
274 | } | |
275 | ||
fef67c28 SC |
276 | # m6811/m6812 code has references to soft registers. |
277 | if {[istarget m6811-*-*] || [istarget m6812-*-*]} { | |
278 | set flags "$flags --defsym _.frame=0 --defsym _.d1=0 --defsym _.d2=0" | |
279 | set flags "$flags --defsym _.d3=0 --defsym _.d4=0" | |
48e83334 | 280 | set flags "$flags --defsym _.tmp=0 --defsym _.xy=0 --defsym _.z=0" |
fef67c28 SC |
281 | } |
282 | ||
252b5132 RH |
283 | # V850 targets need libgcc.a |
284 | if [istarget v850*-*-elf] { | |
285 | set objs "$objs -L ../gcc -lgcc" | |
286 | } | |
e0001a05 NC |
287 | |
288 | # Xtensa ELF targets relax by default; S-Record linker does not | |
289 | if [istarget xtensa*-*-*] { | |
290 | set flags "$flags -no-relax" | |
291 | } | |
907dcf3f MK |
292 | |
293 | # Some OpenBSD targets have ProPolice and reference __guard and | |
294 | # __stack_smash_handler. | |
295 | if [istarget *-*-openbsd*] { | |
296 | set flags "$flags --defsym __guard=0" | |
297 | set flags "$flags --defsym __stack_smash_handler=0" | |
298 | } | |
299 | ||
252b5132 | 300 | if { ![ld_simple_link $ld tmpdir/sr1 "$flags $objs"] \ |
0fc3347a | 301 | || ![ld_simple_link $ld tmpdir/sr2.sr "$flags --oformat srec $objs"] } { |
252b5132 RH |
302 | fail $test |
303 | return | |
304 | } | |
305 | ||
306 | send_log "$objcopy -O srec tmpdir/sr1 tmpdir/sr1.sr\n" | |
307 | verbose "$objcopy -O srec tmpdir/sr1 tmpdir/sr1.sr" | |
308 | catch "exec $objcopy -O srec tmpdir/sr1 tmpdir/sr1.sr" exec_output | |
309 | set exec_output [prune_warnings $exec_output] | |
310 | if ![string match "" $exec_output] { | |
311 | send_log "$exec_output\n" | |
312 | verbose "$exec_output" | |
313 | unresolved $test | |
314 | return | |
315 | } | |
316 | ||
317 | set f1 [open tmpdir/sr1.sr r] | |
318 | set f2 [open tmpdir/sr2.sr r] | |
319 | if [srec_compare $f1 $f2] { | |
320 | pass $test | |
321 | } else { | |
322 | fail $test | |
323 | } | |
324 | close $f1 | |
325 | close $f2 | |
326 | } | |
327 | ||
328 | set test1 "S-records" | |
329 | set test2 "S-records with constructors" | |
330 | ||
331 | # See whether the default linker script uses SIZEOF_HEADERS. | |
332 | catch "exec $ld --verbose" exec_output | |
333 | set sizeof_headers [string match "*SIZEOF_HEADERS*" $exec_output] | |
334 | ||
335 | # First test linking a C program. We don't require any libraries. We | |
336 | # link it normally, and objcopy to the S-record format, and then link | |
337 | # directly to the S-record format, and require that the two files | |
338 | # contain the same data. | |
339 | ||
340 | if { [which $CC] == 0 } { | |
341 | untested $test1 | |
342 | untested $test2 | |
343 | return | |
344 | } | |
345 | ||
346 | if { ![ld_compile $CC $srcdir/$subdir/sr1.c tmpdir/sr1.o] \ | |
347 | || ![ld_compile $CC $srcdir/$subdir/sr2.c tmpdir/sr2.o] } { | |
348 | unresolved $test1 | |
349 | unresolved $test2 | |
350 | return | |
351 | } | |
352 | ||
353 | # The i386-aout target is confused: the linker does not put the | |
354 | # sections where objdump finds them. I don't know which is wrong. | |
355 | setup_xfail "i*86-*-aout*" | |
356 | ||
357 | # These tests fail on the native MIPS ELF targets because the GP value | |
358 | # in the .reginfo section is not updated when the S-record version is | |
359 | # written out. The mips-elf target itself does not use a .reginfo section. | |
66517a2f | 360 | setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-linux*" |
252b5132 RH |
361 | |
362 | # The S-record linker doesn't do the magic TOC handling that XCOFF | |
363 | # linkers do. | |
364 | setup_xfail "*-*-aix*" "*-*-xcoff*" | |
365 | ||
366 | # The S-record linker doesn't build ARM/Thumb stubs. | |
367 | setup_xfail "arm-*-coff" | |
bad19f8f NC |
368 | setup_xfail "strongarm*-*-coff" |
369 | setup_xfail "xscale*-*-coff" | |
252b5132 RH |
370 | setup_xfail "arm-*-pe*" |
371 | # setup_xfail "arm-*elf*" | |
372 | setup_xfail "thumb-*-coff*" | |
373 | setup_xfail "thumb-*-pe*" | |
374 | setup_xfail "thumb-*-elf*" | |
abdbda5e | 375 | setup_xfail "arm*-*-linux*" |
252b5132 | 376 | |
55231b6c DJ |
377 | # The S-record linker doesn't put common symbols in small data |
378 | # the way the ELF linker does. | |
379 | setup_xfail "powerpc*-*-*" | |
252b5132 RH |
380 | |
381 | # The S-record linker doesn't include the .{zda} sections. | |
382 | setup_xfail "v850*-*-elf" | |
383 | ||
384 | # The S-record linker doesn't handle Alpha Elf relaxation. | |
ee58dd1e | 385 | setup_xfail "alpha*-*-elf*" "alpha*-*-linux-*" "alpha*-*-gnu*" |
252b5132 RH |
386 | setup_xfail "alpha*-*-netbsd*" |
387 | ||
36a3dc51 AM |
388 | # The S-record linker hasn't any hope of coping with HPPA relocs. |
389 | setup_xfail "hppa*-*-*" | |
390 | ||
d416627c L |
391 | # The S-record linker doesn't handle IA64 Elf relaxation. |
392 | setup_xfail "ia64-*-*" | |
393 | ||
69f868fa DD |
394 | # The S-record linker doesn't support the special PE headers - the PE |
395 | # emulation tries to write pe-specific information to the PE headers | |
396 | # in the output bfd, but it's not a PE bfd (it's an srec bfd) | |
397 | setup_xfail "*-*-cygwin*" "*-*-mingw*" "*-*-pe*" "*-*-winnt*" | |
1c0d3aa6 | 398 | setup_xfail "score-*-*" |
69f868fa | 399 | |
252b5132 RH |
400 | run_srec_test $test1 "tmpdir/sr1.o tmpdir/sr2.o" |
401 | ||
402 | # Now try linking a C++ program with global constructors and | |
403 | # destructors. Note that since we are not linking against any | |
404 | # libraries, this program won't actually work or anything. | |
405 | ||
406 | if { [which $CXX] == 0 } { | |
407 | untested $test2 | |
408 | return | |
409 | } | |
410 | ||
da04927b | 411 | if ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/sr3.cc tmpdir/sr3.o] { |
252b5132 RH |
412 | unresolved $test2 |
413 | return | |
414 | } | |
415 | ||
416 | # See above. | |
417 | setup_xfail "i*86-*-aout*" | |
66517a2f | 418 | setup_xfail "mips*-*-irix5*" "mips*-*-irix6*" "mips*-*-linux*" |
252b5132 | 419 | setup_xfail "*-*-aix*" "*-*-xcoff*" |
abdbda5e NC |
420 | setup_xfail "arm*-*-*" |
421 | setup_xfail "strongarm*-*-*" | |
252b5132 RH |
422 | setup_xfail "thumb-*-*" |
423 | setup_xfail "powerpc*-*-eabi*" | |
424 | setup_xfail "v850*-*-elf" | |
ee58dd1e | 425 | setup_xfail "alpha*-*-elf*" "alpha*-*-linux-*" "alpha*-*-gnu*" |
252b5132 | 426 | setup_xfail "alpha*-*-netbsd*" |
36a3dc51 | 427 | setup_xfail "hppa*-*-*" |
d416627c | 428 | setup_xfail "ia64-*-*" |
69f868fa | 429 | setup_xfail "*-*-cygwin*" "*-*-mingw*" "*-*-pe*" "*-*-winnt*" |
1c0d3aa6 | 430 | setup_xfail "score-*-*" |
252b5132 RH |
431 | |
432 | run_srec_test $test2 "tmpdir/sr3.o" |