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