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