1 # Expect script for ld-visibility tests
2 # Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Written by Ian Lance Taylor (ian@cygnus.com)
19 # and H.J. Lu (hjl@gnu.org)
22 # Make sure that ld can generate ELF shared libraries with visibility.
24 # This test can only be run on a couple of ELF platforms.
25 # Square bracket expressions seem to confuse istarget.
26 if { ![istarget hppa*64*-*-hpux*] \
27 && ![istarget hppa*-*-linux*] \
28 && ![istarget i?86-*-linux*] \
29 && ![istarget ia64-*-linux*] \
30 && ![istarget m68k-*-linux*] \
31 && ![istarget mips*-*-linux*] \
32 && ![istarget powerpc-*-linux*] \
33 && ![istarget arm*-*-linux*] \
34 && ![istarget alpha*-*-linux*] \
35 && ![istarget sparc*-*-linux*] \
36 && ![istarget s390*-*-linux*] \
37 && ![istarget x86_64-*-linux*] } {
41 if { [istarget *-*-linux*aout*] \
42 || [istarget *-*-linux*oldld*] } {
46 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
47 foreach t $test_list {
48 # We need to strip the ".d", but can leave the dirname.
49 verbose [file rootname $t]
50 run_dump_test [file rootname $t]
53 # The remaining tests can only be run if ld generates native executables.
54 if ![isnative] then {return}
59 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
61 # AIX shared libraries do not seem to support useful features,
62 # like overriding the shared library function or letting the
63 # shared library refer to objects defined in the main program. We
64 # avoid testing those features.
65 set SHCFLAG "-DXCOFF_TEST"
67 # The AIX 3.2.5 loader appears to randomly fail when loading
68 # shared libraries from NSF mounted partitions, so we avoid any
69 # potential problems by using a local directory.
70 catch {exec /bin/sh -c "echo $$"} pid
71 set tmpdir /usr/tmp/ld.$pid
72 catch "exec mkdir $tmpdir" exec_status
74 # On AIX, we need to explicitly export the symbols the shared
75 # library is going to provide, and need.
76 set file [open $tmpdir/xcoff.exp w]
79 puts $file shlib_shlibvar1
80 puts $file shlib_shlibvar2
81 puts $file shlib_shlibcall
82 puts $file shlib_shlibcalled
83 puts $file shlib_checkfunptr1
84 puts $file shlib_getfunptr1
85 puts $file shlib_check
89 set support_protected "no"
91 if [istarget *-*-linux*] {
92 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
93 if [ld_link $ld $tmpdir/main "$tmpdir/main.o"] {
94 catch "exec $tmpdir/main" support_protected
100 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
109 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
111 # Build the shared library.
112 # On AIX, we need to use an export file.
114 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
115 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
117 if {![ld_simple_link $ld $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
118 if { [ string match $visibility "hidden_undef" ]
119 && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
120 && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
122 } else { if { [ string match $visibility "protected_undef" ]
123 && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
124 && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
132 # Link against the shared library. Use -rpath so that the
133 # dynamic linker can locate the shared library at runtime.
134 # On AIX, we must include /lib in -rpath, as otherwise the loader
137 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
138 set rpath /lib:$tmpdir
140 if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] {
141 if { [ string match $visibility "hidden" ]
142 && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
143 && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
145 } else { if { [ string match $visibility "hidden_undef_def" ]
146 && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
147 && [regexp ".*/main.c.*: undefined reference to \`visibility_def\'" $link_output]
148 && [regexp ".*/main.c.*: undefined reference to \`visibility_func\'" $link_output]
149 && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
157 if { [ string match $visibility "hidden" ]
158 || [ string match $visibility "hidden_undef" ]
159 || [ string match $visibility "protected_undef" ] } {
163 # Run the resulting program
164 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
165 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
166 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
167 if ![string match "" $exec_output] then {
168 send_log "$exec_output\n"
169 verbose "$exec_output"
174 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
175 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
176 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
177 set exec_output [prune_warnings $exec_output]
179 if {![string match "" $exec_output]} then {
180 send_log "$exec_output\n"
181 verbose "$exec_output"
189 proc visibility_run {visibility} {
197 global target_triplet
198 global support_protected
200 if [ string match $visibility "hidden" ] {
201 set VSBCFLAG "-DHIDDEN_TEST"
202 } else { if [ string match $visibility "hidden_normal" ] {
203 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
204 } else { if [ string match $visibility "hidden_undef" ] {
205 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
206 } else { if [ string match $visibility "hidden_undef_def" ] {
207 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
208 } else { if [ string match $visibility "hidden_weak" ] {
209 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
210 } else { if [ string match $visibility "protected" ] {
211 set VSBCFLAG "-DPROTECTED_TEST"
212 } else { if [ string match $visibility "protected_undef" ] {
213 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
214 } else { if [ string match $visibility "protected_undef_def" ] {
215 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
216 } else { if [ string match $visibility "protected_weak" ] {
217 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
222 # Compile the main program.
223 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
224 unresolved "visibility ($visibility) (non PIC)"
225 unresolved "visibility ($visibility)"
227 # The shared library is composed of two files. First compile them
228 # without using -fpic. That should work on an ELF system,
229 # although it will be less efficient because the dynamic linker
230 # will need to do more relocation work. However, note that not
231 # using -fpic will cause some of the tests to return different
233 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
234 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
235 unresolved "visibility ($visibility) (non PIC)"
236 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
237 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
239 # SunOS non PIC shared libraries don't permit some cases of
241 if { [ string match $visibility "protected" ]
242 || [ string match $visibility "protected_undef_def" ] } {
243 if [ string match $support_protected "no" ] {
244 setup_xfail $target_triplet
247 setup_xfail "*-*-sunos4*"
249 if { [ string match $visibility "hidden_weak" ]
250 || [ string match $visibility "protected_weak" ] } {
251 setup_xfail "powerpc-*-linux*"
254 # Non-pic code uses name binding rules for applications to
255 # reference variables by gp-relative relocs, which can't be
256 # used with overridable symbols.
257 if { ![ string match $visibility "hidden_undef" ]
258 && ![ string match $visibility "protected_undef" ] } {
259 setup_xfail "ia64-*-linux*"
260 setup_xfail "alpha*-*-linux*"
262 setup_xfail "x86_64-*-linux*"
264 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
266 # Test ELF shared library relocations with a non-zero load
267 # address for the library. Near as I can tell, the R_*_RELATIVE
268 # relocations for various targets are broken in the case where
269 # the load address is not zero (which is the default).
270 if { [ string match $visibility "protected" ]
271 || [ string match $visibility "protected_undef_def" ] } {
272 if [ string match $support_protected "no" ] {
273 setup_xfail $target_triplet
276 setup_xfail "*-*-sunos4*"
277 setup_xfail "*-*-linux*libc1"
279 if { [ string match $visibility "hidden_normal" ]
280 || [ string match $visibility "hidden_weak" ]
281 || [ string match $visibility "protected" ]
282 || [ string match $visibility "protected_undef_def" ]
283 || [ string match $visibility "protected_weak" ]
284 || [ string match $visibility "normal" ] } {
285 setup_xfail "powerpc-*-linux*"
287 if { ![ string match $visibility "hidden_undef" ]
288 && ![ string match $visibility "protected_undef" ] } {
289 setup_xfail "ia64-*-linux*"
290 setup_xfail "alpha*-*-linux*"
291 setup_xfail "mips*-*-linux*"
293 setup_xfail "x86_64-*-linux*"
294 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
295 mainnp.o sh1np.o sh2np.o elfvsb \
296 "-T $srcdir/$subdir/elf-offset.ld"
299 # Now compile the code using -fpic.
301 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
302 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
303 unresolved "visibility ($visibility)"
305 if { [ string match $visibility "protected" ]
306 || [ string match $visibility "protected_undef_def" ] } {
307 if [ string match $support_protected "no" ] {
308 setup_xfail $target_triplet
311 # SunOS can not compare function pointers correctly
312 if [istarget "*-*-sunos4*"] {
313 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
314 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
315 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
317 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
322 # Now do the same tests again, but this time compile main.c PIC.
323 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
324 unresolved "visibility ($visibility) (PIC main, non PIC so)"
325 unresolved "visibility ($visibility) (PIC main)"
327 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
328 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
329 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
331 # SunOS non PIC shared libraries don't permit some cases of
333 if { [ string match $visibility "protected" ]
334 || [ string match $visibility "protected_undef_def" ] } {
335 if [ string match $support_protected "no" ] {
336 setup_xfail $target_triplet
339 setup_xfail "*-*-sunos4*"
341 if { [ string match $visibility "hidden_weak" ]
342 || [ string match $visibility "protected_weak" ] } {
343 setup_xfail "powerpc-*-linux*"
345 if { ![ string match $visibility "hidden_undef" ]
346 && ![ string match $visibility "protected_undef" ] } {
347 setup_xfail "ia64-*-linux*"
348 setup_xfail "alpha*-*-linux*"
350 setup_xfail "x86_64-*-linux*"
351 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
354 unresolved "visibility (PIC main, non PIC so)"
357 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
358 if { [ string match $visibility "protected" ]
359 || [ string match $visibility "protected_undef_def" ] } {
360 if [ string match $support_protected "no" ] {
361 setup_xfail $target_triplet
364 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
365 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
367 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
370 unresolved "visibility ($visibility) (PIC main)"
375 if [istarget mips*-*-*] {
378 # Unfortunately, the gcc argument is -fpic and the cc argument is
379 # -KPIC. We have to try both.
381 send_log "$CC $picflag\n"
382 verbose "$CC $picflag"
383 catch "exec $CC $picflag" exec_output
384 send_log "$exec_output\n"
385 verbose "--" "$exec_output"
386 if { [string match "*illegal option*" $exec_output] \
387 || [string match "*option ignored*" $exec_output] \
388 || [string match "*unrecognized option*" $exec_output] \
389 || [string match "*passed to ld*" $exec_output] } {
390 if [istarget *-*-sunos4*] {
397 verbose "Using $picflag to compile PIC code"
399 visibility_run hidden
400 visibility_run hidden_normal
401 visibility_run hidden_undef
402 visibility_run hidden_undef_def
403 visibility_run hidden_weak
404 visibility_run protected
405 visibility_run protected_undef
406 visibility_run protected_undef_def
407 visibility_run protected_weak
408 visibility_run normal
410 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
411 # Remove the temporary directory.
412 catch "exec rm -rf $tmpdir" exec_status