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