2000-05-13 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     global link_output
89     
90     set objs "$HOSTING_CRT0 $objects"
91     set libs "$LIBS $HOSTING_LIBS"
92     
93     if { [which $ld] == 0 } then {
94         perror "$ld does not exist"
95         return 0
96     }
97     
98     set flags [big_or_little_endian]
99     
100     verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs"
101     
102     catch "exec $ld $HOSTING_EMU $flags -o $target $objs $libs" link_output
103     set exec_output [prune_warnings $link_output]
104     if [string match "" $link_output] then {
105         return 1
106     } else {
107         verbose -log "$link_output"
108         return 0
109     }
110 }
111
112 #
113 # default_ld_simple_link 
114 #       link a program using ld, without including any libraries
115 #
116 proc default_ld_simple_link { ld target objects } {
117     global host_triplet
118     
119     if { [which $ld] == 0 } then {
120         perror "$ld does not exist"
121         return 0
122     }
123     
124     set flags [big_or_little_endian]
125     
126     verbose -log "$ld $flags -o $target $objects"
127     
128     catch "exec $ld $flags -o $target $objects" exec_output
129     set exec_output [prune_warnings $exec_output]
130
131     # We don't care if we get a warning about a non-existent start
132     # symbol, since the default linker script might use ENTRY.
133     regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
134
135     if [string match "" $exec_output] then {
136         return 1
137     } else {
138         verbose -log "$exec_output"
139         return 0
140     }
141 }
142
143 #
144 # default_ld_compile 
145 #       compile an object using cc
146 #
147 proc default_ld_compile { cc source object } {
148     global CFLAGS
149     global srcdir
150     global subdir
151     global host_triplet
152     global gcc_gas_flag
153
154     set cc_prog $cc
155     if {[llength $cc_prog] > 1} then {
156         set cc_prog [lindex $cc_prog 0]
157     }
158     if {[which $cc_prog] == 0} then {
159         perror "$cc_prog does not exist"
160         return 0
161     }
162
163     catch "exec rm -f $object" exec_output
164
165     set flags "-I$srcdir/$subdir $CFLAGS"
166
167     # If we are compiling with gcc, we want to add gcc_gas_flag to
168     # flags.  Rather than determine this in some complex way, we guess
169     # based on the name of the compiler.
170     if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then {
171         set flags "$gcc_gas_flag $flags"
172     }
173
174     if [board_info [target_info name] exists multilib_flags] {
175         append flags " [board_info [target_info name] multilib_flags]";
176     }
177
178     verbose -log "$cc $flags -c $source -o $object"
179
180     catch "exec $cc $flags -c $source -o $object" exec_output
181     set exec_output [prune_warnings $exec_output]
182     if [string match "" $exec_output] then {
183         if {![file exists $object]} then {
184             regexp ".*/(\[^/\]*)$" $source all dobj
185             regsub "\\.c" $dobj ".o" realobj
186             verbose "looking for $realobj"
187             if {[file exists $realobj]} then {
188                 verbose -log "mv $realobj $object"
189                 catch "exec mv $realobj $object" exec_output
190                 set exec_output [prune_warnings $exec_output]
191                 if {![string match "" $exec_output]} then {
192                     verbose -log "$exec_output"
193                     perror "could not move $realobj to $object"
194                     return 0
195                 }
196             } else {
197                 perror "$object not found after compilation"
198                 return 0
199             }
200         }
201         return 1
202     } else {
203         verbose -log "$exec_output"
204         perror "$source: compilation failed"
205         return 0
206     }
207 }
208
209 #
210 # default_ld_assemble
211 #       assemble a file
212 #
213 proc default_ld_assemble { as source object } {
214     global ASFLAGS
215     global host_triplet
216     
217     if {[which $as] == 0} then {
218         perror "$as does not exist"
219         return 0
220     }
221
222     if ![info exists ASFLAGS] { set ASFLAGS "" }
223
224     set flags [big_or_little_endian]
225
226     verbose -log "$as $flags $ASFLAGS -o $object $source"
227
228     catch "exec $as $flags $ASFLAGS -o $object $source" exec_output
229     set exec_output [prune_warnings $exec_output]
230     if [string match "" $exec_output] then {
231         return 1
232     } else {
233         verbose -log "$exec_output"
234         perror "$source: assembly failed"
235         return 0
236     }
237 }
238
239 #
240 # default_ld_nm
241 #       run nm on a file, putting the result in the array nm_output
242 #
243 proc default_ld_nm { nm object } {
244     global NMFLAGS
245     global nm_output
246     global host_triplet
247
248     if {[which $nm] == 0} then {
249         perror "$nm does not exist"
250         return 0
251     }
252
253     if {[info exists nm_output]} {
254       unset nm_output
255     }
256
257     if ![info exists NMFLAGS] { set NMFLAGS "" }
258
259     verbose -log "$nm $NMFLAGS $object >tmpdir/nm.out"
260
261     catch "exec $nm $NMFLAGS $object >tmpdir/nm.out" exec_output
262     set exec_output [prune_warnings $exec_output]
263     if [string match "" $exec_output] then {
264         set file [open tmpdir/nm.out r]
265         while { [gets $file line] != -1 } {
266             verbose "$line" 2
267             if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] (.+)$" $line whole value name] {
268                 set name [string trimleft $name "_"]
269                 verbose "Setting nm_output($name) to 0x$value" 2
270                 set nm_output($name) 0x$value
271             }
272         }
273         close $file
274         return 1
275     } else {
276         verbose -log "$exec_output"
277         perror "$object: nm failed"
278         return 0
279     }
280 }
281
282 #
283 # simple_diff
284 #       compares two files line-by-line
285 #       returns differences if exist
286 #       returns null if file(s) cannot be opened
287 #
288 proc simple_diff { file_1 file_2 } {
289     global target
290         
291     set eof -1
292     set differences 0
293     
294     if [file exists $file_1] then {
295         set file_a [open $file_1 r]
296     } else {
297         warning "$file_1 doesn't exist"
298         return
299     }
300     
301     if [file exists $file_2] then {
302         set file_b [open $file_2 r]
303     } else {
304         fail "$file_2 doesn't exist"
305         return
306     }
307     
308     verbose "# Diff'ing: $file_1 $file_2\n" 2
309     
310     while { [gets $file_a line] != $eof } {
311         if [regexp "^#.*$" $line] then {
312             continue
313         } else {
314             lappend list_a $line
315         }
316     }
317     close $file_a
318     
319     while { [gets $file_b line] != $eof } {
320         if [regexp "^#.*$" $line] then {
321             continue
322         } else {
323             lappend list_b $line
324         }
325     }
326     close $file_b
327
328     for { set i 0 } { $i < [llength $list_a] } { incr i } {
329         set line_a [lindex $list_a $i]
330         set line_b [lindex $list_b $i]
331
332         verbose "\t$file_1: $i: $line_a\n" 3
333         verbose "\t$file_2: $i: $line_b\n" 3
334         if [string compare $line_a $line_b] then {
335             verbose -log "\t$file_1: $i: $line_a\n"
336             verbose -log "\t$file_2: $i: $line_b\n"
337
338             fail "Test: $target"
339             return
340         }
341     }
342     
343     if { [llength $list_a] != [llength $list_b] } {
344         fail "Test: $target"
345         return
346     }
347
348     if $differences<1 then {
349         pass "Test: $target"
350     }
351 }
352
353 # This definition is taken from an unreleased version of DejaGnu.  Once
354 # that version gets released, and has been out in the world for a few
355 # months at least, it may be safe to delete this copy.
356 if ![string length [info proc prune_warnings]] {
357     #
358     # prune_warnings -- delete various system verbosities from TEXT
359     #
360     # An example is:
361     # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
362     #
363     # Sites with particular verbose os's may wish to override this in site.exp.
364     #
365     proc prune_warnings { text } {
366         # This is from sun4's.  Do it for all machines for now.
367         # The "\\1" is to try to preserve a "\n" but only if necessary.
368         regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
369
370         # It might be tempting to get carried away and delete blank lines, etc.
371         # Just delete *exactly* what we're ask to, and that's it.
372         return $text
373     }
374 }