2000-10-31 Philip Blundell <philb@gnu.org>
[binutils.git] / ld / testsuite / lib / ld-lib.exp
blob11185d825fd8521e9a561a6f3d47719159ca9123
2 # default_ld_version
3 # extract and print the version number of ld
5 proc default_ld_version { ld } {
6 global host_triplet
8 if { [which $ld] == 0 } then {
9 perror "$ld does not exist"
10 exit 1
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"
22 # default_ld_relocate
23 # link an object using relocation
25 proc default_ld_relocate { ld target objects } {
26 global HOSTING_EMU
27 global host_triplet
29 if { [which $ld] == 0 } then {
30 perror "$ld does not exist"
31 return 0
34 verbose -log "$ld $HOSTING_EMU -o $target -r $objects"
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
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.
55 proc big_or_little_endian {} {
57 if [board_info [target_info name] exists multilib_flags] {
58 set tmp_flags " [board_info [target_info name] multilib_flags]";
60 foreach x $tmp_flags {
61 case $x in {
62 {*big*endian eb EB} {
63 set flags " -EB"
64 return $flags
66 {*little*endian el EL} {
67 set flags " -EL"
68 return $flags
74 set flags ""
75 return $flags
79 # default_ld_link
80 # link a program using ld
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
90 set objs "$HOSTING_CRT0 $objects"
91 set libs "$LIBS $HOSTING_LIBS"
93 if { [which $ld] == 0 } then {
94 perror "$ld does not exist"
95 return 0
98 set flags [big_or_little_endian]
100 verbose -log "$ld $HOSTING_EMU $flags -o $target $objs $libs"
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
113 # default_ld_simple_link
114 # link a program using ld, without including any libraries
116 proc default_ld_simple_link { ld target objects } {
117 global host_triplet
118 global link_output
120 if { [which $ld] == 0 } then {
121 perror "$ld does not exist"
122 return 0
125 set flags [big_or_little_endian]
127 verbose -log "$ld $flags -o $target $objects"
129 catch "exec $ld $flags -o $target $objects" link_output
130 set exec_output [prune_warnings $link_output]
132 # We don't care if we get a warning about a non-existent start
133 # symbol, since the default linker script might use ENTRY.
134 regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
136 if [string match "" $exec_output] then {
137 return 1
138 } else {
139 verbose -log "$exec_output"
140 return 0
145 # default_ld_compile
146 # compile an object using cc
148 proc default_ld_compile { cc source object } {
149 global CFLAGS
150 global srcdir
151 global subdir
152 global host_triplet
153 global gcc_gas_flag
155 set cc_prog $cc
156 if {[llength $cc_prog] > 1} then {
157 set cc_prog [lindex $cc_prog 0]
159 if {[which $cc_prog] == 0} then {
160 perror "$cc_prog does not exist"
161 return 0
164 catch "exec rm -f $object" exec_output
166 set flags "-I$srcdir/$subdir $CFLAGS"
168 # If we are compiling with gcc, we want to add gcc_gas_flag to
169 # flags. Rather than determine this in some complex way, we guess
170 # based on the name of the compiler.
171 if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then {
172 set flags "$gcc_gas_flag $flags"
175 if [board_info [target_info name] exists multilib_flags] {
176 append flags " [board_info [target_info name] multilib_flags]";
179 verbose -log "$cc $flags -c $source -o $object"
181 catch "exec $cc $flags -c $source -o $object" exec_output
182 set exec_output [prune_warnings $exec_output]
183 if [string match "" $exec_output] then {
184 if {![file exists $object]} then {
185 regexp ".*/(\[^/\]*)$" $source all dobj
186 regsub "\\.c" $dobj ".o" realobj
187 verbose "looking for $realobj"
188 if {[file exists $realobj]} then {
189 verbose -log "mv $realobj $object"
190 catch "exec mv $realobj $object" exec_output
191 set exec_output [prune_warnings $exec_output]
192 if {![string match "" $exec_output]} then {
193 verbose -log "$exec_output"
194 perror "could not move $realobj to $object"
195 return 0
197 } else {
198 perror "$object not found after compilation"
199 return 0
202 return 1
203 } else {
204 verbose -log "$exec_output"
205 perror "$source: compilation failed"
206 return 0
211 # default_ld_assemble
212 # assemble a file
214 proc default_ld_assemble { as source object } {
215 global ASFLAGS
216 global host_triplet
218 if {[which $as] == 0} then {
219 perror "$as does not exist"
220 return 0
223 if ![info exists ASFLAGS] { set ASFLAGS "" }
225 set flags [big_or_little_endian]
227 verbose -log "$as $flags $ASFLAGS -o $object $source"
229 catch "exec $as $flags $ASFLAGS -o $object $source" exec_output
230 set exec_output [prune_warnings $exec_output]
231 if [string match "" $exec_output] then {
232 return 1
233 } else {
234 verbose -log "$exec_output"
235 perror "$source: assembly failed"
236 return 0
241 # default_ld_nm
242 # run nm on a file, putting the result in the array nm_output
244 proc default_ld_nm { nm object } {
245 global NMFLAGS
246 global nm_output
247 global host_triplet
249 if {[which $nm] == 0} then {
250 perror "$nm does not exist"
251 return 0
254 if {[info exists nm_output]} {
255 unset nm_output
258 if ![info exists NMFLAGS] { set NMFLAGS "" }
260 verbose -log "$nm $NMFLAGS $object >tmpdir/nm.out"
262 catch "exec $nm $NMFLAGS $object >tmpdir/nm.out" exec_output
263 set exec_output [prune_warnings $exec_output]
264 if [string match "" $exec_output] then {
265 set file [open tmpdir/nm.out r]
266 while { [gets $file line] != -1 } {
267 verbose "$line" 2
268 if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] (.+)$" $line whole value name] {
269 set name [string trimleft $name "_"]
270 verbose "Setting nm_output($name) to 0x$value" 2
271 set nm_output($name) 0x$value
274 close $file
275 return 1
276 } else {
277 verbose -log "$exec_output"
278 perror "$object: nm failed"
279 return 0
284 # simple_diff
285 # compares two files line-by-line
286 # returns differences if exist
287 # returns null if file(s) cannot be opened
289 proc simple_diff { file_1 file_2 } {
290 global target
292 set eof -1
293 set differences 0
295 if [file exists $file_1] then {
296 set file_a [open $file_1 r]
297 } else {
298 warning "$file_1 doesn't exist"
299 return
302 if [file exists $file_2] then {
303 set file_b [open $file_2 r]
304 } else {
305 fail "$file_2 doesn't exist"
306 return
309 verbose "# Diff'ing: $file_1 $file_2\n" 2
311 while { [gets $file_a line] != $eof } {
312 if [regexp "^#.*$" $line] then {
313 continue
314 } else {
315 lappend list_a $line
318 close $file_a
320 while { [gets $file_b line] != $eof } {
321 if [regexp "^#.*$" $line] then {
322 continue
323 } else {
324 lappend list_b $line
327 close $file_b
329 for { set i 0 } { $i < [llength $list_a] } { incr i } {
330 set line_a [lindex $list_a $i]
331 set line_b [lindex $list_b $i]
333 verbose "\t$file_1: $i: $line_a\n" 3
334 verbose "\t$file_2: $i: $line_b\n" 3
335 if [string compare $line_a $line_b] then {
336 verbose -log "\t$file_1: $i: $line_a\n"
337 verbose -log "\t$file_2: $i: $line_b\n"
339 fail "Test: $target"
340 return
344 if { [llength $list_a] != [llength $list_b] } {
345 fail "Test: $target"
346 return
349 if $differences<1 then {
350 pass "Test: $target"
354 # This definition is taken from an unreleased version of DejaGnu. Once
355 # that version gets released, and has been out in the world for a few
356 # months at least, it may be safe to delete this copy.
357 if ![string length [info proc prune_warnings]] {
359 # prune_warnings -- delete various system verbosities from TEXT
361 # An example is:
362 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
364 # Sites with particular verbose os's may wish to override this in site.exp.
366 proc prune_warnings { text } {
367 # This is from sun4's. Do it for all machines for now.
368 # The "\\1" is to try to preserve a "\n" but only if necessary.
369 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
371 # It might be tempting to get carried away and delete blank lines, etc.
372 # Just delete *exactly* what we're ask to, and that's it.
373 return $text