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