2001-09-14 H.J. Lu <hjl@gnu.org>
[external/binutils.git] / ld / testsuite / ld-elfweak / elfweak.exp
1 # Expect script for ld-weak tests
2 #   Copyright 2001 Free Software Foundation, Inc.
3 #
4 # This file is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #
18 # Written by H.J. Lu (hjl@gnu.org)
19 #            Eric Youngdale (eric@andante.jic.com)
20 #
21
22 # This test can only be run if ld generates native executables.
23 if ![isnative] then {return}
24
25 # This test can only be run on a couple of ELF platforms.
26 # Square bracket expressions seem to confuse istarget.
27 # This is similar to the test that is used in ld-shared, BTW.
28 if { ![istarget i?86-*-sysv4*] \
29      && ![istarget i?86-*-unixware] \
30      && ![istarget i?86-*-elf*] \
31      && ![istarget i?86-*-linux*] \
32      && ![istarget ia64-*-elf*] \
33      && ![istarget ia64-*-linux*] \
34      && ![istarget m68k-*-linux*] \
35      && ![istarget mips*-*-irix5*] \
36      && ![istarget powerpc-*-elf*] \
37      && ![istarget powerpc-*-linux*] \
38      && ![istarget powerpc-*-sysv4*] \
39      && ![istarget sparc*-*-elf] \
40      && ![istarget sparc*-*-solaris2*] \
41      && ![istarget sparc*-*-linux*] \
42      && ![istarget arm*-*-linux*] \
43      && ![istarget mips*-*-linux*] \
44      && ![istarget alpha*-*-linux*] } {
45     return
46 }
47
48 if { [istarget i?86-*-linux*aout*] \
49      || [istarget i?86-*-linux*oldld*] \
50      || [istarget m68k-*-linux*aout*] } {
51     return
52 }
53
54 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
55     return
56 }
57
58 set diff diff
59 set tmpdir tmpdir
60 set DOBJDUMP_FLAGS --dynamic-syms
61 set SOBJDUMP_FLAGS --syms
62 set shared --shared
63
64 #
65 # objdump_symstuff
66 #       Dump non-dynamic symbol stuff and make sure that it is sane.
67 #
68 proc objdump_symstuff { objdump object expectfile } {
69     global SOBJDUMP_FLAGS
70     global version_output
71     global diff
72     global tmpdir
73
74     if ![info exists SOBJDUMP_FLAGS] { set SOBJDUMP_FLAGS "" }
75
76     verbose -log "$objdump $SOBJDUMP_FLAGS $object | grep foo$  > $tmpdir/objdump.out"
77
78     catch "exec $objdump $SOBJDUMP_FLAGS $object | grep foo$  > $tmpdir/objdump.out" exec_output
79     set exec_output [prune_warnings $exec_output]
80     if [string match "" $exec_output] then {
81
82 # Now do a line-by-line comparison to effectively diff the darned things
83 # The stuff coming from the expectfile is actually a regex, so we can
84 # skip over the actual addresses and so forth.  This is currently very
85 # simpleminded - it expects a one-to-one correspondence in terms of line
86 # numbers.
87
88         if [file exists $expectfile] then {
89             set file_a [open $expectfile r]
90         } else {
91             perror "$expectfile doesn't exist"
92             return 0
93         }
94         
95         if [file exists $tmpdir/objdump.out] then {
96             set file_b [open $tmpdir/objdump.out r]
97         } else {
98             perror "$tmpdir/objdump.out doesn't exist"
99             return 0
100         }
101
102         verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
103
104         set eof -1
105         set differences 0
106
107         while { [gets $file_a line] != $eof } {
108             if [regexp "^#.*$" $line] then {
109                 continue
110             } else {
111                 lappend list_a $line
112             }
113         }
114         close $file_a
115
116         while { [gets $file_b line] != $eof } {
117             if [regexp "^#.*$" $line] then {
118                 continue
119             } else {
120                 lappend list_b $line
121             }
122         }
123         close $file_b
124         
125         for { set i 0 } { $i < [llength $list_a] } { incr i } {
126             set line_a [lindex $list_a $i]
127             set line_b [lindex $list_b $i]
128             
129
130             verbose "\t$expectfile: $i: $line_a" 3
131             verbose "\t/tmp/objdump.out: $i: $line_b" 3
132             if [regexp $line_a $line_b] then {
133                 continue
134             } else {
135                 verbose -log "\t$expectfile: $i: $line_a"
136                 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
137                 
138                 return 0
139             }
140         }
141         
142         if { [llength $list_a] != [llength $list_b] } {
143             verbose -log "Line count"
144             return 0
145         }
146         
147         if $differences<1 then {
148             return 1
149         }
150         
151         return 0
152     } else {
153         verbose -log "$exec_output"
154         return 0
155     }
156
157 }
158
159 #
160 # objdump_dymsymstuff
161 #       Dump dynamic symbol stuff and make sure that it is sane.
162 #
163 proc objdump_dynsymstuff { objdump object expectfile } {
164     global DOBJDUMP_FLAGS
165     global version_output
166     global diff
167     global tmpdir
168
169     if ![info exists DOBJDUMP_FLAGS] { set DOBJDUMP_FLAGS "" }
170
171     verbose -log "$objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out"
172
173     catch "exec $objdump $DOBJDUMP_FLAGS $object | grep foo$ > $tmpdir/objdump.out" exec_output
174     set exec_output [prune_warnings $exec_output]
175     if [string match "" $exec_output] then {
176
177 # Now do a line-by-line comparison to effectively diff the darned things
178 # The stuff coming from the expectfile is actually a regex, so we can
179 # skip over the actual addresses and so forth.  This is currently very
180 # simpleminded - it expects a one-to-one correspondence in terms of line
181 # numbers.
182
183         if [file exists $expectfile] then {
184             set file_a [open $expectfile r]
185         } else {
186             warning "$expectfile doesn't exist"
187             return 0
188         }
189         
190         if [file exists $tmpdir/objdump.out] then {
191             set file_b [open $tmpdir/objdump.out r]
192         } else {
193             fail "$tmpdir/objdump.out doesn't exist"
194             return 0
195         }
196
197         verbose "# Diff'ing: $expectfile $tmpdir/objdump.out" 2
198
199         set eof -1
200         set differences 0
201
202         while { [gets $file_a line] != $eof } {
203             if [regexp "^#.*$" $line] then {
204                 continue
205             } else {
206                 lappend list_a $line
207             }
208         }
209         close $file_a
210
211         while { [gets $file_b line] != $eof } {
212             if [regexp "^#.*$" $line] then {
213                 continue
214             } else {
215                 lappend list_b $line
216             }
217         }
218         close $file_b
219         
220         for { set i 0 } { $i < [llength $list_b] } { incr i } {
221             set line_b [lindex $list_b $i]
222             
223 # The tests are rigged so that we should never export a symbol with the
224 # word 'hide' in it.  Thus we just search for it, and bail if we find it.
225             if [regexp "hide" $line_b] then {
226                 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
227                 
228                 return 0
229             }
230
231             verbose "\t$expectfile: $i: $line_b" 3
232
233             # We can't assume that the sort is consistent across
234             # systems, so we must check each regexp.  When we find a
235             # regexp, we null it out, so we don't match it twice.
236             for { set j 0 } { $j < [llength $list_a] } { incr j } {
237                 set line_a [lindex $list_a $j]
238
239                 if [regexp $line_a $line_b] then {
240                     lreplace $list_a $j $j "CAN NOT MATCH"
241                     break
242                 }
243             }
244
245             if { $j >= [llength $list_a] } {
246                 verbose -log "\t$tmpdir/objdump.out: $i: $line_b"
247                 
248                 return 0
249             }
250         }
251         
252         if { [llength $list_a] != [llength $list_b] } {
253             verbose -log "Line count"
254             return 0
255         }
256         
257         if $differences<1 then {
258             return 1
259         }
260         
261         return 0
262     } else {
263         verbose -log "$exec_output"
264         return 0
265     }
266
267 }
268
269 proc build_lib {test libname objs dynsymexp} {
270     global ld
271     global objdump
272     global tmpdir
273     global shared
274     global srcdir
275     global subdir
276
277     set files ""
278     foreach obj $objs {
279       set files "$files $tmpdir/$obj"
280     }
281
282     if {![ld_link $ld $tmpdir/$libname.so "$shared $files"]} {
283         fail $test
284         return
285     }
286
287     if {![objdump_dynsymstuff $objdump $tmpdir/$libname.so $srcdir/$subdir/$dynsymexp]} {
288         fail $test
289         return
290     }
291     pass $test
292 }
293
294 proc build_exec { test execname objs flags dat dynsymexp symexp} {
295     global ld
296     global objdump
297     global tmpdir
298     global shared
299     global srcdir
300     global subdir
301     global exec_output
302
303     set files ""
304     foreach obj $objs {
305       set files "$files $tmpdir/$obj"
306     }
307
308     if {![ld_link $ld $tmpdir/$execname "$flags $files"]} {
309         fail "$test"
310         return
311     }
312
313     if {![string match "" $dynsymexp]} then {
314         if {![objdump_dynsymstuff $objdump $tmpdir/$execname $srcdir/$subdir/$dynsymexp]} {
315             fail $test
316             return
317         }
318     }
319
320     if {![string match "" $symexp]} then {
321         if {![objdump_symstuff $objdump $tmpdir/$execname $srcdir/$subdir/$symexp]} {
322             fail $test
323             return
324         }
325     }
326
327     # Run the resulting program
328     send_log "$tmpdir/$execname >$tmpdir/$execname.out\n"
329     verbose "$tmpdir/$execname >$tmpdir/$execname.out"
330     catch "exec $tmpdir/$execname >$tmpdir/$execname.out" exec_output
331     if ![string match "" $exec_output] then {
332         send_log "$exec_output\n"
333         verbose "$exec_output"
334         fail $test
335         return
336     }
337
338     send_log "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat\n"
339     verbose "diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat"
340     catch "exec diff $tmpdir/$execname.out $srcdir/$subdir/$dat.dat" exec_output
341     set exec_output [prune_warnings $exec_output]
342
343     if {![string match "" $exec_output]} then {
344         send_log "$exec_output\n"
345         verbose "$exec_output"
346         fail $test
347         return
348     }
349
350     pass $test
351 }
352
353 if [istarget mips*-*-*] {
354     set picflag ""
355 } else {
356     # Unfortunately, the gcc argument is -fpic and the cc argument is
357     # -KPIC.  We have to try both.
358     set picflag "-fpic"
359     send_log "$CC $picflag\n"
360     verbose "$CC $picflag"
361     catch "exec $CC $picflag" exec_output
362     send_log "$exec_output\n"
363     verbose "--" "$exec_output"
364     if { [string match "*illegal option*" $exec_output] \
365          || [string match "*option ignored*" $exec_output] \
366          || [string match "*unrecognized option*" $exec_output] \
367          || [string match "*passed to ld*" $exec_output] } {
368         if [istarget *-*-sunos4*] {
369             set picflag "-pic"
370         } else {
371             set picflag "-KPIC"
372         }
373     }
374 }
375 verbose "Using $picflag to compile PIC code"
376
377 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o] {
378     unresolved "ELF weak"
379     return
380 }
381
382 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o] {
383     unresolved "ELF weak"
384     return
385 }
386
387 if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o] {
388     unresolved "ELF weak"
389     return
390 }
391
392 if {![ld_link $ld $tmpdir/libbar.so "$shared $tmpdir/bar.o"]} {
393     fail "ELF weak"
394     return
395 }
396
397 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o] {
398     unresolved "ELF weak"
399     return
400 }
401
402 if ![ld_compile "$CC $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o] {
403     unresolved "ELF weak"
404     return
405 }
406
407 if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o] {
408     unresolved "ELF weak"
409     return
410 }
411
412 if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o] {
413     unresolved "ELF weak"
414     return
415 }
416
417 if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o] {
418     unresolved "ELF weak"
419     return
420 }
421
422 if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main1.c $tmpdir/main1.o] {
423     unresolved "ELF weak"
424     return
425 }
426
427 if {![ld_link $ld $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]} {
428     fail "ELF weak"
429     return
430 }
431
432 if {![ld_link $ld $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]} {
433     fail "ELF weak"
434     return
435 }
436
437 if {![ld_link $ld $tmpdir/libbar1a.so "$shared $tmpdir/bar1a.o $tmpdir/libfoo1a.so"]} {
438     fail "ELF weak"
439     return
440 }
441
442 build_lib "ELF DSO weak func first" libfoo "foo.o bar.o" dso.dsym
443 build_lib "ELF DSO weak func last" libfoo "bar.o foo.o" dso.dsym
444 setup_xfail "*-*-*"
445 build_lib "ELF DSO weak func first DSO" libfoo "foo.o libbar.so" dsow.dsym
446 setup_xfail "*-*-*"
447 build_lib "ELF DSO weak func last DSO" libfoo "libbar.so foo.o" dsow.dsym
448 build_exec "ELF weak func first" foo "main.o bar.o" "" strong "" strong.sym
449 build_exec "ELF weak func last" foo "bar.o main.o" "" strong "" strong.sym
450 setup_xfail "*-*-*"
451 build_exec "ELF weak func first DSO" foo "main.o libbar.so" "-rpath ." weak weak.dsym ""
452 setup_xfail "*-*-*"
453 build_exec "ELF weak func last DSO" foo "libbar.so main.o" "-rpath ." weak weak.dsym ""
454
455 build_lib "ELF DSO weak data first" libfoo "bar1a.o foo1a.o" dsodata.dsym
456 build_lib "ELF DSO weak data last" libfoo "foo1a.o bar1a.o" dsodata.dsym
457 setup_xfail "*-*-*"
458 build_lib "ELF DSO weak data first DSO" libfoo "main1.o libfoo1a.so" dsowdata.dsym
459 setup_xfail "*-*-*"
460 build_lib "ELF DSO weak data last DSO" libfoo "libfoo1a.so main1.o" dsowdata.dsym
461 setup_xfail "*-*-*"
462 build_lib "ELF DSO weak data first DSO common" libfoo "main1.o libfoo1b.so" dsowdata.dsym
463 setup_xfail "*-*-*"
464 build_lib "ELF DSO weak data last DSO common" libfoo "libfoo1b.so main1.o" dsowdata.dsym
465 build_exec "ELF weak data first" foo "main1.o bar1a.o foo1a.o" "" strongdata "" strongdata.sym
466 build_exec "ELF weak data last" foo "foo1a.o main1.o bar1a.o" "" strongdata "" strongdata.sym
467 build_exec "ELF weak data first common" foo "main1.o bar1a.o foo1b.o" "" strongdata "" strongcomm.sym
468 build_exec "ELF weak data last common" foo "foo1b.o main1.o bar1a.o" "" strongdata "" strongcomm.sym
469 setup_xfail "*-*-*"
470 build_exec "ELF weak data first DSO" foo "main1.o libbar1a.so libfoo1a.so" "-rpath ." weakdata weakdata.dsym ""
471 setup_xfail "*-*-*"
472 build_exec "ELF weak data last DSO" foo "libfoo1a.so main1.o libbar1a.so" "-rpath ." weakdata weakdata.dsym ""
473 setup_xfail "*-*-*"
474 build_exec "ELF weak data first DSO common" foo "main1.o libbar1a.so libfoo1b.so" "-rpath ." weakdata weakdata.dsym ""
475 setup_xfail "*-*-*"
476 build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-rpath ." weakdata weakdata.dsym ""