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