1 # Expect script
for ld-visibility tests
2 # Copyright
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 # Free Software Foundation
, Inc.
5 # This file is part of the GNU Binutils.
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.
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.
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
,
22 # Written by Ian Lance Taylor
(ian@cygnus.com
)
23 # and H.J. Lu
(hjl@gnu.org
)
26 # Make sure that
ld can generate ELF shared libraries with visibility.
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
*] } {
46 if { [istarget
*-*-linux
*aout
*] \
47 ||
[istarget
*-*-linux
*oldld
*] } {
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
]
58 # The remaining tests can only be run
if ld generates native executables.
59 if ![isnative
] then {return}
63 set shared_needs_pic
"no"
65 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
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"
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
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
]
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
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
99 set SHCFLAG
"-fno-section-anchors"
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\"); }"
110 if [run_host_cmd_yesno
"$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
111 set shared_needs_pic
"yes"
116 set support_protected
"no"
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
126 # The test procedure.
127 proc visibility_test
{ visibility progname testname main sh1 sh2 dat
args } {
136 if [llength $
args] { set shldflags
[lindex $
args 0] } else { set shldflags
"" }
138 # Build the shared library.
139 #
On AIX
, we need to use an
export file.
141 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
142 set shared
"-bM:SRE -bE:$tmpdir/xcoff.exp"
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] } {
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] } {
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
164 if { [istarget rs6000
*-*-aix
*] ||
[istarget powerpc
*-*-aix
*] } {
165 set rpath
/lib
:$tmpdir
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] } {
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] } {
184 if { [ string match $visibility
"hidden" ]
185 ||
[ string match $visibility
"hidden_undef" ]
186 ||
[ string match $visibility
"protected_undef" ] } {
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"
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
]
206 if {![string match
"" $exec_output]} then {
207 send_log
"$exec_output\n"
208 verbose
"$exec_output"
216 proc visibility_run
{visibility
} {
224 global target_triplet
225 global support_protected
226 global shared_needs_pic
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"
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)"
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
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
267 # SunOS non PIC shared libraries don
't permit some cases of
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
275 setup_xfail "*-*-sunos4*"
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*"
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*"
295 setup_xfail
"x86_64-*-linux*"
296 if { ![istarget hppa
*64*-*-linux
*] } {
297 setup_xfail
"hppa*-*-linux*"
299 if [ string match $shared_needs_pic
"yes" ] {
300 setup_xfail
"arm*-*-linux*"
303 visibility_test $visibility vnp
"visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
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
315 setup_xfail
"*-*-sunos4*"
316 setup_xfail
"*-*-linux*libc1"
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*"
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*"
336 setup_xfail
"x86_64-*-linux*"
337 if { ![istarget hppa
*64*-*-linux
*] } {
338 setup_xfail
"hppa*-*-linux*"
340 if [ string match $shared_needs_pic
"yes" ] {
341 setup_xfail
"arm*-*-linux*"
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"
349 # Now
compile the code using
-fpic.
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)"
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
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
367 visibility_test $visibility vp
"visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
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)"
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
381 # SunOS non PIC shared libraries don
't permit some cases of
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
389 setup_xfail "*-*-sunos4*"
391 if { ![ string match $visibility "hidden_undef" ]
392 && ![ string match $visibility "protected_undef" ] } {
393 setup_xfail "ia64-*-linux*"
394 setup_xfail "alpha*-*-linux*"
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*"
405 setup_xfail "x86_64-*-linux*"
406 if { ![istarget hppa*64*-*-linux*] } {
407 setup_xfail "hppa*-*-linux*"
409 if [ string match $shared_needs_pic "yes" ] {
410 setup_xfail "arm*-*-linux*"
413 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
416 unresolved "visibility (PIC main, non PIC so)"
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
426 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
427 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
429 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
432 unresolved "visibility ($visibility) (PIC main)"
437 if [istarget mips*-*-*] {
440 # Unfortunately, the gcc argument is -fpic and the cc argument is
441 # -KPIC. We have to try both.
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*] {
459 verbose "Using $picflag to compile PIC code"
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
472 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
473 unresolved "common hidden symbol"
475 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
476 fail "common hidden symbol"
478 pass "common hidden symbol"
482 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
483 unresolved "weak hidden symbol"
485 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
486 unresolved "weak hidden symbol"
488 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
489 fail "weak hidden symbol"
491 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
492 fail "weak hidden symbol DSO last"
494 pass "weak hidden symbol DSO last"
496 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
497 fail "weak hidden symbol DSO first"
499 pass "weak hidden symbol DSO first"
505 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
506 # Remove the temporary directory.
507 catch "exec rm -rf $tmpdir" exec_status