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