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