1 # Expect script
for ld-visibility tests
2 # Copyright
2000, 2001, 2002, 2003, 2004 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.
, 51 Franklin Street
- Fifth Floor
, Boston
, MA
02110-1301, 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 sh\
[34\
]*-*-linux
*] \
38 && ![istarget x86_64
-*-linux
*] } {
42 if { [istarget
*-*-linux
*aout
*] \
43 ||
[istarget
*-*-linux
*oldld
*] } {
47 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/*.d
]]
48 foreach t $test_list
{
49 # We need to
strip the
".d", but can leave the dirname.
50 verbose
[file rootname $t
]
51 run_dump_test
[file rootname $t
]
54 # The remaining tests can only be run
if ld generates native executables.
55 if ![isnative
] then {return}
60 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
62 # AIX shared libraries
do not seem to support useful features
,
63 # like overriding the shared library function or letting the
64 # shared library refer to objects defined in the main
program. We
65 # avoid testing those features.
66 set SHCFLAG
"-DXCOFF_TEST"
68 # The AIX
3.2.5 loader appears to randomly fail when loading
69 # shared libraries from NSF mounted partitions
, so we avoid
any
70 # potential problems by using a local directory.
71 catch
{exec
/bin
/sh
-c
"echo $$"} pid
72 set tmpdir
/usr
/tmp
/ld.$pid
73 catch
"exec mkdir $tmpdir" exec_status
75 #
On AIX
, we need to explicitly
export the symbols the shared
76 # library is going to provide
, and need.
77 set file
[open $tmpdir
/xcoff.exp w
]
80 puts $file shlib_shlibvar1
81 puts $file shlib_shlibvar2
82 puts $file shlib_shlibcall
83 puts $file shlib_shlibcalled
84 puts $file shlib_checkfunptr1
85 puts $file shlib_getfunptr1
86 puts $file shlib_check
90 set support_protected
"no"
92 if [istarget
*-*-linux
*] {
93 if [ld_compile
"$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
94 if [ld_simple_link $CC $tmpdir
/main
"$tmpdir/main.o"] {
95 catch
"exec $tmpdir/main" support_protected
100 # The test procedure.
101 proc visibility_test
{ visibility progname testname main sh1 sh2 dat
args } {
110 if [llength $
args] { set shldflags
[lindex $
args 0] } else { set shldflags
"" }
112 # Build the shared library.
113 #
On AIX
, we need to use an
export file.
115 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
116 set shared
"-bM:SRE -bE:$tmpdir/xcoff.exp"
118 if {![ld_simple_link $CC $tmpdir
/$progname.so
"$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
119 if { [ string match $visibility
"hidden_undef" ]
120 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
121 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
123 } else { if { [ string match $visibility
"protected_undef" ]
124 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
125 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
133 # Link against the shared library. Use
-rpath so that the
134 # dynamic linker can locate the shared library at runtime.
135 #
On AIX
, we must
include /lib in
-rpath
, as otherwise the loader
138 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
139 set rpath
/lib
:$tmpdir
141 if ![ld_simple_link $CC $tmpdir
/$progname
"-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
142 if { [ string match $visibility
"hidden" ]
143 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
144 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
146 } else { if { [ string match $visibility
"hidden_undef_def" ]
147 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
148 && [regexp
"undefined reference to \`visibility_def\'" $link_output]
149 && [regexp
"undefined reference to \`\.?visibility_func\'" $link_output]
150 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
158 if { [ string match $visibility
"hidden" ]
159 ||
[ string match $visibility
"hidden_undef" ]
160 ||
[ string match $visibility
"protected_undef" ] } {
164 # Run the resulting
program
165 send_log
"$tmpdir/$progname >$tmpdir/$progname.out\n"
166 verbose
"$tmpdir/$progname >$tmpdir/$progname.out"
167 catch
"exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
168 if ![string match
"" $exec_output] then {
169 send_log
"$exec_output\n"
170 verbose
"$exec_output"
175 send_log
"diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
176 verbose
"diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
177 catch
"exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
178 set exec_output
[prune_warnings $exec_output
]
180 if {![string match
"" $exec_output]} then {
181 send_log
"$exec_output\n"
182 verbose
"$exec_output"
190 proc visibility_run
{visibility
} {
198 global target_triplet
199 global support_protected
201 if [ string match $visibility
"hidden" ] {
202 set VSBCFLAG
"-DHIDDEN_TEST"
203 } else { if [ string match $visibility
"hidden_normal" ] {
204 set VSBCFLAG
"-DHIDDEN_NORMAL_TEST"
205 } else { if [ string match $visibility
"hidden_undef" ] {
206 set VSBCFLAG
"-DHIDDEN_UNDEF_TEST"
207 } else { if [ string match $visibility
"hidden_undef_def" ] {
208 set VSBCFLAG
"-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
209 } else { if [ string match $visibility
"hidden_weak" ] {
210 set VSBCFLAG
"-DHIDDEN_WEAK_TEST"
211 } else { if [ string match $visibility
"protected" ] {
212 set VSBCFLAG
"-DPROTECTED_TEST"
213 } else { if [ string match $visibility
"protected_undef" ] {
214 set VSBCFLAG
"-DPROTECTED_UNDEF_TEST"
215 } else { if [ string match $visibility
"protected_undef_def" ] {
216 set VSBCFLAG
"-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
217 } else { if [ string match $visibility
"protected_weak" ] {
218 set VSBCFLAG
"-DPROTECTED_WEAK_TEST"
223 #
Compile the main
program.
224 if ![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
225 unresolved
"visibility ($visibility) (non PIC)"
226 unresolved
"visibility ($visibility)"
228 # The shared library is composed of two files. First
compile them
229 # without using
-fpic. That should work
on an ELF
system,
230 # although it will be less efficient because the dynamic linker
231 # will need to
do more relocation work. However
, note that not
232 # using
-fpic will cause some of the tests to
return different
234 if { ![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
235 ||
![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
236 unresolved
"visibility ($visibility) (non PIC)"
237 } else { if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
238 visibility_test $visibility vnp
"visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
240 # SunOS non PIC shared libraries don
't permit some cases of
242 if { [ string match $visibility "protected" ]
243 || [ string match $visibility "protected_undef_def" ] } {
244 if [ string match $support_protected "no" ] {
245 setup_xfail $target_triplet
248 setup_xfail "*-*-sunos4*"
251 # Non-pic code uses name binding rules for applications to
252 # reference variables by gp-relative relocs, which can't be
253 # used with overridable symbols.
254 if { ![ string match $visibility
"hidden_undef" ]
255 && ![ string match $visibility
"protected_undef" ] } {
256 setup_xfail
"ia64-*-linux*"
257 setup_xfail
"alpha*-*-linux*"
259 if { ![ string match $visibility
"hidden" ]
260 && ![ string match $visibility
"hidden_undef" ]
261 && ![ string match $visibility
"hidden_undef_def" ]
262 && ![ string match $visibility
"protected_undef" ] } {
263 setup_xfail
"s390x-*-linux*"
264 if { [istarget sparc
*-*-linux
*] && [is_elf64 $tmpdir
/mainnp.o
] } {
265 setup_xfail
"sparc*-*-linux*"
268 setup_xfail
"x86_64-*-linux*"
269 if { ![istarget hppa
*64*-*-linux
*] } {
270 setup_xfail
"hppa*-*-linux*"
273 visibility_test $visibility vnp
"visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
275 # Test ELF shared library relocations with a non
-zero
load
276 # address
for the library. Near as I can tell
, the R_
*_RELATIVE
277 # relocations
for various targets are broken in the case where
278 # the
load address is not zero
(which is the default
).
279 if { [ string match $visibility
"protected" ]
280 ||
[ string match $visibility
"protected_undef_def" ] } {
281 if [ string match $support_protected
"no" ] {
282 setup_xfail $target_triplet
285 setup_xfail
"*-*-sunos4*"
286 setup_xfail
"*-*-linux*libc1"
288 if { [ string match $visibility
"hidden_normal" ]
289 ||
[ string match $visibility
"hidden_weak" ]
290 ||
[ string match $visibility
"protected" ]
291 ||
[ string match $visibility
"protected_undef_def" ]
292 ||
[ string match $visibility
"protected_weak" ]
293 ||
[ string match $visibility
"normal" ] } {
294 setup_xfail
"powerpc-*-linux*"
295 setup_xfail
"s390x-*-linux*"
296 if { [istarget sparc
*-*-linux
*] && [is_elf64 $tmpdir
/mainnp.o
] } {
297 setup_xfail
"sparc*-*-linux*"
300 if { ![ string match $visibility
"hidden_undef" ]
301 && ![ string match $visibility
"protected_undef" ] } {
302 setup_xfail
"ia64-*-linux*"
303 setup_xfail
"alpha*-*-linux*"
304 setup_xfail
"mips*-*-linux*"
306 setup_xfail
"x86_64-*-linux*"
307 if { ![istarget hppa
*64*-*-linux
*] } {
308 setup_xfail
"hppa*-*-linux*"
311 visibility_test $visibility vnp
"visibility ($visibility) (non PIC, load offset)" \
312 mainnp.o sh1np.o sh2np.o elfvsb \
313 "-T $srcdir/$subdir/elf-offset.ld"
316 # Now
compile the code using
-fpic.
318 if { ![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
319 ||
![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
320 unresolved
"visibility ($visibility)"
322 if { [ string match $visibility
"protected" ]
323 ||
[ string match $visibility
"protected_undef_def" ] } {
324 if [ string match $support_protected
"no" ] {
325 setup_xfail $target_triplet
328 # SunOS can not compare function pointers correctly
329 if [istarget
"*-*-sunos4*"] {
330 visibility_test $visibility vp
"visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
331 } else { if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
332 visibility_test $visibility vp
"visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
334 visibility_test $visibility vp
"visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
339 # Now
do the same tests again
, but this time
compile main.c PIC.
340 if ![ld_compile
"$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
341 unresolved
"visibility ($visibility) (PIC main, non PIC so)"
342 unresolved
"visibility ($visibility) (PIC main)"
344 if { [file
exists $tmpdir
/sh1np.o
] && [ file
exists $tmpdir
/sh2np.o
] } {
345 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
346 visibility_test $visibility vmpnp
"visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
348 # SunOS non PIC shared libraries don
't permit some cases of
350 if { [ string match $visibility "protected" ]
351 || [ string match $visibility "protected_undef_def" ] } {
352 if [ string match $support_protected "no" ] {
353 setup_xfail $target_triplet
356 setup_xfail "*-*-sunos4*"
358 if { ![ string match $visibility "hidden_undef" ]
359 && ![ string match $visibility "protected_undef" ] } {
360 setup_xfail "ia64-*-linux*"
361 setup_xfail "alpha*-*-linux*"
363 if { ![ string match $visibility "hidden" ]
364 && ![ string match $visibility "hidden_undef" ]
365 && ![ string match $visibility "hidden_undef_def" ]
366 && ![ string match $visibility "protected_undef" ] } {
367 setup_xfail "s390x-*-linux*"
368 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
369 setup_xfail "sparc*-*-linux*"
372 setup_xfail "x86_64-*-linux*"
373 if { ![istarget hppa*64*-*-linux*] } {
374 setup_xfail "hppa*-*-linux*"
377 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
380 unresolved "visibility (PIC main, non PIC so)"
383 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
384 if { [ string match $visibility "protected" ]
385 || [ string match $visibility "protected_undef_def" ] } {
386 if [ string match $support_protected "no" ] {
387 setup_xfail $target_triplet
390 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
391 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
393 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
396 unresolved "visibility ($visibility) (PIC main)"
401 if [istarget mips*-*-*] {
404 # Unfortunately, the gcc argument is -fpic and the cc argument is
405 # -KPIC. We have to try both.
407 send_log "$CC $picflag\n"
408 verbose "$CC $picflag"
409 catch "exec $CC $picflag" exec_output
410 send_log "$exec_output\n"
411 verbose "--" "$exec_output"
412 if { [string match "*illegal option*" $exec_output] \
413 || [string match "*option ignored*" $exec_output] \
414 || [string match "*unrecognized option*" $exec_output] \
415 || [string match "*passed to ld*" $exec_output] } {
416 if [istarget *-*-sunos4*] {
423 verbose "Using $picflag to compile PIC code"
425 visibility_run hidden
426 visibility_run hidden_normal
427 visibility_run hidden_undef
428 visibility_run hidden_undef_def
429 visibility_run hidden_weak
430 visibility_run protected
431 visibility_run protected_undef
432 visibility_run protected_undef_def
433 visibility_run protected_weak
434 visibility_run normal
436 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
437 unresolved "common hidden symbol"
439 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
440 fail "common hidden symbol"
442 pass "common hidden symbol"
446 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
447 unresolved "weak hidden symbol"
449 if { ![ld_compile "$CC -g $CFLAGS $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
450 unresolved "weak hidden symbol"
452 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
453 fail "weak hidden symbol"
455 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
456 fail "weak hidden symbol DSO last"
458 pass "weak hidden symbol DSO last"
460 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
461 fail "weak hidden symbol DSO first"
463 pass "weak hidden symbol DSO first"
469 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
470 # Remove the temporary directory.
471 catch "exec rm -rf $tmpdir" exec_status