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