merge from gcc
[external/binutils.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 #   Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 Ian Lance Taylor (ian@cygnus.com)
23 #            and H.J. Lu (hjl@gnu.org)
24 #
25
26 # Make sure that ld can generate ELF shared libraries with visibility.
27
28 # This test can only be run on a couple of ELF platforms.
29 # Square bracket expressions seem to confuse istarget.
30 if { ![istarget hppa*64*-*-hpux*] \
31      && ![istarget hppa*-*-linux*] \
32      && ![istarget i?86-*-linux*] \
33      && ![istarget ia64-*-linux*] \
34      && ![istarget m68k-*-linux*] \
35      && ![istarget mips*-*-linux*] \
36      && ![istarget powerpc*-*-linux*] \
37      && ![istarget arm*-*-linux*] \
38      && ![istarget alpha*-*-linux*] \
39      && ![istarget sparc*-*-linux*] \
40      && ![istarget s390*-*-linux*] \
41      && ![istarget sh\[34\]*-*-linux*] \
42      && ![istarget x86_64-*-linux*] } {
43     return
44 }
45
46 if { [istarget *-*-linux*aout*] \
47      || [istarget *-*-linux*oldld*] } {
48     return
49 }
50
51 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
52 foreach t $test_list {
53     # We need to strip the ".d", but can leave the dirname.
54     verbose [file rootname $t]
55     run_dump_test [file rootname $t]
56 }
57
58 # The remaining tests can only be run if ld generates native executables.
59 if ![isnative] then {return}
60
61 set tmpdir tmpdir
62 set SHCFLAG ""
63 set shared_needs_pic "no"
64
65 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
66
67     # AIX shared libraries do not seem to support useful features,
68     # like overriding the shared library function or letting the
69     # shared library refer to objects defined in the main program.  We
70     # avoid testing those features.
71     set SHCFLAG "-DXCOFF_TEST"
72
73     # The AIX 3.2.5 loader appears to randomly fail when loading
74     # shared libraries from NSF mounted partitions, so we avoid any
75     # potential problems by using a local directory.
76     catch {exec /bin/sh -c "echo $$"} pid
77     set tmpdir /usr/tmp/ld.$pid
78     catch "exec mkdir $tmpdir" exec_status
79
80     # On AIX, we need to explicitly export the symbols the shared
81     # library is going to provide, and need.
82     set file [open $tmpdir/xcoff.exp w]
83     puts $file shlibvar1
84     puts $file shlibvar2
85     puts $file shlib_shlibvar1
86     puts $file shlib_shlibvar2
87     puts $file shlib_shlibcall
88     puts $file shlib_shlibcalled
89     puts $file shlib_checkfunptr1
90     puts $file shlib_getfunptr1
91     puts $file shlib_check
92     close $file
93 }
94
95 if [istarget arm*-*-linux*] {
96     # On ARM section anchors can change the symbol pre-emptability for
97     # non-PIC shared libraries, causing these tests to fail.  Turn section 
98     # anchors off.
99     set SHCFLAG "-fno-section-anchors"
100
101     # On targets that have MOVW the compiler will emit relocations which 
102     # the linker doesn't support when compiling -shared without -fpic.  The
103     # test to find out whether we want to XFAIL the non-PIC tests requires 
104     # a compile - so we pre-calculate it here.  We also note that this can
105     # only affect arm*-*-*eabi targets as the old ABI doesn't support v7.
106     if [istarget arm*-*-*eabi] {
107         set file [open $tmpdir/movw-detect.c w]
108         puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
109         close $file
110         if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
111             set shared_needs_pic "yes"
112         }
113     }
114 }
115
116 set support_protected "no"
117
118 if [istarget *-*-linux*] {
119     if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
120       if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
121         catch "exec $tmpdir/main" support_protected
122       }
123     }
124 }
125
126 # The test procedure.
127 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
128     global CC
129     global srcdir
130     global subdir
131     global exec_output
132     global link_output
133     global host_triplet
134     global tmpdir
135
136     if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
137
138     # Build the shared library.
139     # On AIX, we need to use an export file.
140     set shared -shared
141     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
142         set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
143     }
144     if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
145         if { [ string match $visibility "hidden_undef" ]
146              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
147              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
148             pass "$testname"
149         } else { if { [ string match $visibility "protected_undef" ]
150              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
151              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
152             pass "$testname"
153         } else {
154             fail "$testname"
155         }}
156         return
157     }
158
159     # Link against the shared library.  Use -rpath so that the
160     # dynamic linker can locate the shared library at runtime.
161     # On AIX, we must include /lib in -rpath, as otherwise the loader
162     # can not find -lc.
163     set rpath $tmpdir
164     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
165         set rpath /lib:$tmpdir
166     }
167     if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
168         if { [ string match $visibility "hidden" ]
169              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
170              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
171             pass "$testname"
172         } else { if { [ string match $visibility "hidden_undef_def" ]
173              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
174              && [regexp "undefined reference to \`visibility_def\'" $link_output]
175              && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
176              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
177             pass "$testname"
178         } else {
179             fail "$testname"
180         }}
181         return
182     }
183
184     if { [ string match $visibility "hidden" ]
185          || [ string match $visibility "hidden_undef" ]
186          || [ string match $visibility "protected_undef" ] } {
187         fail "$testname"
188     }
189
190     # Run the resulting program
191     send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
192     verbose "$tmpdir/$progname >$tmpdir/$progname.out"
193     catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
194     if ![string match "" $exec_output] then {
195         send_log "$exec_output\n"
196         verbose "$exec_output"
197         fail "$testname"
198         return
199     }
200
201     send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
202     verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
203     catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
204     set exec_output [prune_warnings $exec_output]
205
206     if {![string match "" $exec_output]} then {
207         send_log "$exec_output\n"
208         verbose "$exec_output"
209         fail "$testname"
210         return
211     }
212
213     pass "$testname"
214 }
215
216 proc visibility_run {visibility} {
217     global CC
218     global CFLAGS
219     global SHCFLAG
220     global srcdir
221     global subdir
222     global tmpdir
223     global picflag
224     global target_triplet
225     global support_protected
226     global shared_needs_pic
227
228     if [ string match $visibility "hidden" ] {
229         set VSBCFLAG "-DHIDDEN_TEST"
230     } else { if [ string match $visibility "hidden_normal" ] {
231         set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
232     } else { if [ string match $visibility "hidden_undef" ] {
233         set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
234     } else { if [ string match $visibility "hidden_undef_def" ] {
235         set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
236     } else { if [ string match $visibility "hidden_weak" ] {
237         set VSBCFLAG "-DHIDDEN_WEAK_TEST"
238     } else { if [ string match $visibility "protected" ] {
239         set VSBCFLAG "-DPROTECTED_TEST"
240     } else { if [ string match $visibility "protected_undef" ] {
241         set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
242     } else { if [ string match $visibility "protected_undef_def" ] {
243         set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
244     } else { if [ string match $visibility "protected_weak" ] {
245         set VSBCFLAG "-DPROTECTED_WEAK_TEST"
246     } else {
247         set VSBCFLAG ""
248     }}}}}}}}}
249
250     # Compile the main program.
251     if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
252         unresolved "visibility ($visibility) (non PIC)"
253         unresolved "visibility ($visibility)"
254     } else {
255         # The shared library is composed of two files.  First compile them
256         # without using -fpic.  That should work on an ELF system,
257         # although it will be less efficient because the dynamic linker
258         # will need to do more relocation work.  However, note that not
259         # using -fpic will cause some of the tests to return different
260         # results.
261         if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
262              || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
263             unresolved "visibility ($visibility) (non PIC)"
264         } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
265             visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
266         } else {
267             # SunOS non PIC shared libraries don't permit some cases of
268             # overriding.
269             if { [ string match $visibility "protected" ]
270                  || [ string match $visibility "protected_undef_def" ] } {
271                 if [ string match $support_protected "no" ] {
272                     setup_xfail $target_triplet
273                 }
274             } else {
275                 setup_xfail "*-*-sunos4*"
276             }
277
278             # Non-pic code uses name binding rules for applications to
279             # reference variables by gp-relative relocs, which can't be
280             # used with overridable symbols.
281             if { ![ string match $visibility "hidden_undef" ]
282                  && ![ string match $visibility "protected_undef" ] } {
283                 setup_xfail "ia64-*-linux*"
284                 setup_xfail "alpha*-*-linux*"
285             }
286             if { ![ string match $visibility "hidden" ]
287                  && ![ string match $visibility "hidden_undef" ]
288                  && ![ string match $visibility "hidden_undef_def" ]
289                  && ![ string match $visibility "protected_undef" ] } {
290                 setup_xfail "s390x-*-linux*"
291                 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
292                     setup_xfail "sparc*-*-linux*"
293                 }
294             }
295             setup_xfail "x86_64-*-linux*"
296             if { ![istarget hppa*64*-*-linux*] } {
297                 setup_xfail "hppa*-*-linux*"
298             }
299             if [ string match $shared_needs_pic "yes" ] {
300                 setup_xfail "arm*-*-linux*"
301             }
302
303             visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
304
305             # Test ELF shared library relocations with a non-zero load
306             # address for the library.  Near as I can tell, the R_*_RELATIVE
307             # relocations for various targets are broken in the case where
308             # the load address is not zero (which is the default).
309             if { [ string match $visibility "protected" ]
310                  || [ string match $visibility "protected_undef_def" ] } {
311                 if [ string match $support_protected "no" ] {
312                     setup_xfail $target_triplet
313                 }
314             } else {
315                 setup_xfail "*-*-sunos4*"
316                 setup_xfail "*-*-linux*libc1"
317             }
318             if { [ string match $visibility "hidden_normal" ]
319                  || [ string match $visibility "hidden_weak" ]
320                  || [ string match $visibility "protected" ]
321                  || [ string match $visibility "protected_undef_def" ]
322                  || [ string match $visibility "protected_weak" ]
323                  || [ string match $visibility "normal" ] } {
324                 setup_xfail "powerpc-*-linux*"
325                 setup_xfail "s390x-*-linux*"
326                 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
327                     setup_xfail "sparc*-*-linux*"
328                 }
329             }
330             if { ![ string match $visibility "hidden_undef" ]
331                  && ![ string match $visibility "protected_undef" ] } {
332                 setup_xfail "ia64-*-linux*"
333                 setup_xfail "alpha*-*-linux*"
334                 setup_xfail "mips*-*-linux*"
335             }
336             setup_xfail "x86_64-*-linux*"
337             if { ![istarget hppa*64*-*-linux*] } {
338                 setup_xfail "hppa*-*-linux*"
339             }
340             if [ string match $shared_needs_pic "yes" ] {
341                 setup_xfail "arm*-*-linux*"
342             }
343
344             visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
345                 mainnp.o sh1np.o sh2np.o elfvsb \
346                 "-T $srcdir/$subdir/elf-offset.ld"
347         } }
348
349         # Now compile the code using -fpic.
350
351         if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o] 
352             || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
353             unresolved "visibility ($visibility)"
354         } else {
355             if { [ string match $visibility "protected" ]
356                  || [ string match $visibility "protected_undef_def" ] } {
357                 if [ string match $support_protected "no" ] {
358                     setup_xfail $target_triplet
359                 }
360             }
361             # SunOS can not compare function pointers correctly
362             if [istarget "*-*-sunos4*"] {
363                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
364             } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
365                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
366             } else {
367                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
368             } }
369         }
370     }
371
372     # Now do the same tests again, but this time compile main.c PIC.
373     if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
374         unresolved "visibility ($visibility) (PIC main, non PIC so)"
375         unresolved "visibility ($visibility) (PIC main)"
376     } else {
377         if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
378             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
379                 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
380             } else {
381                 # SunOS non PIC shared libraries don't permit some cases of
382                 # overriding.
383                 if { [ string match $visibility "protected" ]
384                      || [ string match $visibility "protected_undef_def" ] } {
385                     if [ string match $support_protected "no" ] {
386                         setup_xfail $target_triplet
387                     }
388                 } else {
389                     setup_xfail "*-*-sunos4*"
390                 }
391                 if { ![ string match $visibility "hidden_undef" ]
392                      && ![ string match $visibility "protected_undef" ] } {
393                     setup_xfail "ia64-*-linux*"
394                     setup_xfail "alpha*-*-linux*"
395                 }
396                 if { ![ string match $visibility "hidden" ]
397                      && ![ string match $visibility "hidden_undef" ]
398                      && ![ string match $visibility "hidden_undef_def" ]
399                      && ![ string match $visibility "protected_undef" ] } {
400                     setup_xfail "s390x-*-linux*"
401                     if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
402                         setup_xfail "sparc*-*-linux*"
403                     }
404                 }
405                 setup_xfail "x86_64-*-linux*"
406                 if { ![istarget hppa*64*-*-linux*] } {
407                     setup_xfail "hppa*-*-linux*"
408                 }
409                 if [ string match $shared_needs_pic "yes" ] {
410                     setup_xfail "arm*-*-linux*"
411                 }
412
413                 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
414             }
415         } else {
416             unresolved "visibility (PIC main, non PIC so)"
417         }
418
419         if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
420             if { [ string match $visibility "protected" ]
421                  || [ string match $visibility "protected_undef_def" ] } {
422                 if [ string match $support_protected "no" ] {
423                     setup_xfail $target_triplet
424                 }
425             }
426             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
427                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
428             } else {
429                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
430             }
431         } else {
432             unresolved "visibility ($visibility) (PIC main)"
433         }
434     }
435 }
436
437 if [istarget mips*-*-*] {
438     set picflag ""
439 } else {
440     # Unfortunately, the gcc argument is -fpic and the cc argument is
441     # -KPIC.  We have to try both.
442     set picflag "-fpic"
443     send_log "$CC $picflag\n"
444     verbose "$CC $picflag"
445     catch "exec $CC $picflag" exec_output
446     send_log "$exec_output\n"
447     verbose "--" "$exec_output"
448     if { [string match "*illegal option*" $exec_output] \
449          || [string match "*option ignored*" $exec_output] \
450          || [string match "*unrecognized option*" $exec_output] \
451          || [string match "*passed to ld*" $exec_output] } {
452         if [istarget *-*-sunos4*] {
453             set picflag "-pic"
454         } else {
455             set picflag "-KPIC"
456         }
457     }
458 }
459 verbose "Using $picflag to compile PIC code"
460
461 visibility_run hidden
462 visibility_run hidden_normal
463 visibility_run hidden_undef
464 visibility_run hidden_undef_def
465 visibility_run hidden_weak
466 visibility_run protected
467 visibility_run protected_undef
468 visibility_run protected_undef_def
469 visibility_run protected_weak
470 visibility_run normal
471
472 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
473     unresolved "common hidden symbol"
474 } else {
475     if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
476         fail "common hidden symbol"
477     } else {
478         pass "common hidden symbol"
479     }
480 }
481
482 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
483     unresolved "weak hidden symbol"
484 } else {
485    if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
486         unresolved "weak hidden symbol"
487     } else {
488         if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
489             fail "weak hidden symbol"
490         } else {
491             if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
492                 fail "weak hidden symbol DSO last"
493             } else {
494                 pass "weak hidden symbol DSO last"
495             }
496             if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
497                 fail "weak hidden symbol DSO first"
498             } else {
499                 pass "weak hidden symbol DSO first"
500             }
501         }
502     }
503 }
504
505 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
506     # Remove the temporary directory.
507     catch "exec rm -rf $tmpdir" exec_status
508 }