2000-02-27 H.J. Lu (hjl@gnu.org)
[platform/upstream/binutils.git] / ld / testsuite / lib / ld-lib.exp
1 #
2 # default_ld_version 
3 #       extract and print the version number of ld
4 #
5 proc default_ld_version { ld } {
6     global host_triplet
7
8     if { [which $ld] == 0 } then {
9         perror "$ld does not exist"
10         exit 1
11     }
12     
13     catch "exec $ld --version" tmp
14     set tmp [prune_warnings $tmp]
15     regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
16     if [info exists number] then {
17         clone_output "$ld $number\n"
18     }
19 }
20
21 #
22 # default_ld_relocate 
23 #       link an object using relocation
24 #
25 proc default_ld_relocate { ld target objects } {
26     global HOSTING_EMU
27     global host_triplet
28     
29     if { [which $ld] == 0 } then {
30         perror "$ld does not exist"
31         return 0
32     }
33     
34     verbose -log "$ld $HOSTING_EMU -o $target -r $objects"
35     
36     catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output
37     set exec_output [prune_warnings $exec_output]
38     if [string match "" $exec_output] then {
39         return 1
40     } else {
41         verbose -log "$exec_output"
42         return 0
43     }
44 }
45
46 # Look for big-endian or little-endian switches in the multlib
47 # options and translate these into a -EB or -EL switch.  Note
48 # we cannot rely upon proc process_multilib_options to do this
49 # for us because for some targets the compiler does not support
50 # -EB/-EL but it does support -mbig-endian/-mlittle-endian, and
51 # the site.exp file will include the switch "-mbig-endian"
52 # (rather than "big-endian") which is not detected by proc
53 # process_multilib_options.
54
55 proc big_or_little_endian {} {
56     
57     if [board_info [target_info name] exists multilib_flags] {
58         set tmp_flags " [board_info [target_info name] multilib_flags]";
59
60         foreach x $tmp_flags {
61             case $x in {
62                 {*big*endian eb EB} {
63                     set flags " -EB"
64                     return $flags
65                 }
66                 {*little*endian el EL} {
67                     set flags " -EL"
68                     return $flags
69                 }
70             }
71         }
72     }
73
74     set flags ""
75     return $flags
76 }
77
78 #
79 # default_ld_link 
80 #       link a program using ld
81 #
82 proc default_ld_link { ld target objects } {
83     global HOSTING_EMU
84     global HOSTING_CRT0
85     global HOSTING_LIBS
86     global LIBS
87     global host_triplet
88     
89     set objs "$HOSTING_CRT0 $objects"
90     set libs "$LIBS $HOSTING_LIBS"
91     
92     if { [which $ld] == 0 } then {
93         perror "$ld does not exist"
94         return 0
95     }
96     
97     set flags [big_or_little_endian]
98     
99     verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs"
100     
101     catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" exec_output
102     set exec_output [prune_warnings $exec_output]
103     if [string match "" $exec_output] then {
104         return 1
105     } else {
106         verbose -log "$exec_output"
107         return 0
108     }
109 }
110
111 #
112 # default_ld_simple_link 
113 #       link a program using ld, without including any libraries
114 #
115 proc default_ld_simple_link { ld target objects } {
116     global host_triplet
117     
118     if { [which $ld] == 0 } then {
119         perror "$ld does not exist"
120         return 0
121     }
122     
123     set flags [big_or_little_endian]
124     
125     verbose -log "$ld $flags -o $target $objects"
126     
127     catch "exec $ld $flags -o $target $objects" exec_output
128     set exec_output [prune_warnings $exec_output]
129
130     # We don't care if we get a warning about a non-existent start
131     # symbol, since the default linker script might use ENTRY.
132     regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
133
134     if [string match "" $exec_output] then {
135         return 1
136     } else {
137         verbose -log "$exec_output"
138         return 0
139     }
140 }
141
142 #
143 # default_ld_compile 
144 #       compile an object using cc
145 #
146 proc default_ld_compile { cc source object } {
147     global CFLAGS
148     global srcdir
149     global subdir
150     global host_triplet
151     global gcc_gas_flag
152
153     set cc_prog $cc
154     if {[llength $cc_prog] > 1} then {
155         set cc_prog [lindex $cc_prog 0]
156     }
157     if {[which $cc_prog] == 0} then {
158         perror "$cc_prog does not exist"
159         return 0
160     }
161
162     catch "exec rm -f $object" exec_output
163
164     set flags "-I$srcdir/$subdir $CFLAGS"
165
166     # If we are compiling with gcc, we want to add gcc_gas_flag to
167     # flags.  Rather than determine this in some complex way, we guess
168     # based on the name of the compiler.
169     if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then {
170         set flags "$gcc_gas_flag $flags"
171     }
172
173     if [board_info [target_info name] exists multilib_flags] {
174         append flags " [board_info [target_info name] multilib_flags]";
175     }
176
177     verbose -log "$cc $flags -c $source -o $object"
178
179     catch "exec $cc $flags -c $source -o $object" exec_output
180     set exec_output [prune_warnings $exec_output]
181     if [string match "" $exec_output] then {
182         if {![file exists $object]} then {
183             regexp ".*/(\[^/\]*)$" $source all dobj
184             regsub "\\.c" $dobj ".o" realobj
185             verbose "looking for $realobj"
186             if {[file exists $realobj]} then {
187                 verbose -log "mv $realobj $object"
188                 catch "exec mv $realobj $object" exec_output
189                 set exec_output [prune_warnings $exec_output]
190                 if {![string match "" $exec_output]} then {
191                     verbose -log "$exec_output"
192                     perror "could not move $realobj to $object"
193                     return 0
194                 }
195             } else {
196                 perror "$object not found after compilation"
197                 return 0
198             }
199         }
200         return 1
201     } else {
202         verbose -log "$exec_output"
203         perror "$source: compilation failed"
204         return 0
205     }
206 }
207
208 #
209 # default_ld_assemble
210 #       assemble a file
211 #
212 proc default_ld_assemble { as source object } {
213     global ASFLAGS
214     global host_triplet
215     
216     if {[which $as] == 0} then {
217         perror "$as does not exist"
218         return 0
219     }
220
221     if ![info exists ASFLAGS] { set ASFLAGS "" }
222
223     set flags [big_or_little_endian]
224
225     verbose -log "$as $flags $ASFLAGS -o $object $source"
226
227     catch "exec $as $flags $ASFLAGS -o $object $source" exec_output
228     set exec_output [prune_warnings $exec_output]
229     if [string match "" $exec_output] then {
230         return 1
231     } else {
232         verbose -log "$exec_output"
233         perror "$source: assembly failed"
234         return 0
235     }
236 }
237
238 #
239 # default_ld_nm
240 #       run nm on a file, putting the result in the array nm_output
241 #
242 proc default_ld_nm { nm object } {
243     global NMFLAGS
244     global nm_output
245     global host_triplet
246
247     if {[which $nm] == 0} then {
248         perror "$nm does not exist"
249         return 0
250     }
251
252     if {[info exists nm_output]} {
253       unset nm_output
254     }
255
256     if ![info exists NMFLAGS] { set NMFLAGS "" }
257
258     verbose -log "$nm $NMFLAGS $object >tmpdir/nm.out"
259
260     catch "exec $nm $NMFLAGS $object >tmpdir/nm.out" exec_output
261     set exec_output [prune_warnings $exec_output]
262     if [string match "" $exec_output] then {
263         set file [open tmpdir/nm.out r]
264         while { [gets $file line] != -1 } {
265             verbose "$line" 2
266             if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] (.+)$" $line whole value name] {
267                 set name [string trimleft $name "_"]
268                 verbose "Setting nm_output($name) to 0x$value" 2
269                 set nm_output($name) 0x$value
270             }
271         }
272         close $file
273         return 1
274     } else {
275         verbose -log "$exec_output"
276         perror "$object: nm failed"
277         return 0
278     }
279 }
280
281 #
282 # simple_diff
283 #       compares two files line-by-line
284 #       returns differences if exist
285 #       returns null if file(s) cannot be opened
286 #
287 proc simple_diff { file_1 file_2 } {
288     global target
289         
290     set eof -1
291     set differences 0
292     
293     if [file exists $file_1] then {
294         set file_a [open $file_1 r]
295     } else {
296         warning "$file_1 doesn't exist"
297         return
298     }
299     
300     if [file exists $file_2] then {
301         set file_b [open $file_2 r]
302     } else {
303         fail "$file_2 doesn't exist"
304         return
305     }
306     
307     verbose "# Diff'ing: $file_1 $file_2\n" 2
308     
309     while { [gets $file_a line] != $eof } {
310         if [regexp "^#.*$" $line] then {
311             continue
312         } else {
313             lappend list_a $line
314         }
315     }
316     close $file_a
317     
318     while { [gets $file_b line] != $eof } {
319         if [regexp "^#.*$" $line] then {
320             continue
321         } else {
322             lappend list_b $line
323         }
324     }
325     close $file_b
326
327     for { set i 0 } { $i < [llength $list_a] } { incr i } {
328         set line_a [lindex $list_a $i]
329         set line_b [lindex $list_b $i]
330
331         verbose "\t$file_1: $i: $line_a\n" 3
332         verbose "\t$file_2: $i: $line_b\n" 3
333         if [string compare $line_a $line_b] then {
334             verbose -log "\t$file_1: $i: $line_a\n"
335             verbose -log "\t$file_2: $i: $line_b\n"
336
337             fail "Test: $target"
338             return
339         }
340     }
341     
342     if { [llength $list_a] != [llength $list_b] } {
343         fail "Test: $target"
344         return
345     }
346
347     if $differences<1 then {
348         pass "Test: $target"
349     }
350 }
351
352 # This definition is taken from an unreleased version of DejaGnu.  Once
353 # that version gets released, and has been out in the world for a few
354 # months at least, it may be safe to delete this copy.
355 if ![string length [info proc prune_warnings]] {
356     #
357     # prune_warnings -- delete various system verbosities from TEXT
358     #
359     # An example is:
360     # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
361     #
362     # Sites with particular verbose os's may wish to override this in site.exp.
363     #
364     proc prune_warnings { text } {
365         # This is from sun4's.  Do it for all machines for now.
366         # The "\\1" is to try to preserve a "\n" but only if necessary.
367         regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
368
369         # It might be tempting to get carried away and delete blank lines, etc.
370         # Just delete *exactly* what we're ask to, and that's it.
371         return $text
372     }
373 }