OSDN Git Service

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