1 # Expect script
for ld-visibility tests
2 # Copyright
(C
) 2000-2024 Free Software Foundation
, Inc.
4 # This file is part of the GNU Binutils.
6 # This
program is free software
; you can redistribute it and
/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation
; either version
3 of the License
, or
9 #
(at your option
) any later version.
11 # This
program is distributed in the hope that it will be useful
,
12 # but WITHOUT
ANY WARRANTY
; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License
for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this
program; if not
, write to the Free Software
18 # Foundation
, Inc.
, 51 Franklin Street
- Fifth Floor
, Boston
,
21 # Written by Ian Lance Taylor
(ian@cygnus.com
)
22 # and H.J. Lu
(hjl@gnu.org
)
25 # Make sure that
ld can generate ELF shared libraries with visibility.
27 # Check to see
if the C compiler works
28 if { ![check_compiler_available
] } {
32 # This test can only be run
on a couple of ELF platforms or with
34 # Square bracket expressions seem to confuse istarget.
35 if { ![istarget hppa
*64*-*-hpux
*] \
36 && ![istarget hppa
*-*-linux
*] \
37 && ![istarget i?
86-*-linux
*] \
38 && ![istarget i?
86-*-gnu
*] \
39 && ![istarget
*-*-nacl
*] \
40 && ![istarget ia64
-*-linux
*] \
41 && ![istarget m68k
-*-linux
*] \
42 && ![istarget mips
*-*-linux
*] \
43 && ![istarget powerpc
*-*-linux
*] \
44 && ![istarget arm
*-*-linux
*] \
45 && ![istarget alpha
*-*-linux
*] \
46 && ![istarget sparc
*-*-linux
*] \
47 && ![istarget s390
*-*-linux
*] \
48 && ![istarget sh\
[34\
]*-*-linux
*] \
49 && ![istarget x86_64
-*-linux
*] \
50 && ![is_xcoff_format
] } {
54 if [is_xcoff_format
] {
55 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/*-xcoff
*.d
]]
57 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/*-elf.d
]]
60 foreach t $test_list
{
61 # We need to
strip the
".d", but can leave the dirname.
62 verbose
[file rootname $t
]
63 run_dump_test
[file rootname $t
]
68 set shared_needs_pic
"no"
69 set COMPRESS_LDFLAG
"-Wl,--compress-debug-sections=zlib-gabi"
71 if { [is_xcoff_format
] } {
72 # Not all the useful features are available with AIX shared
73 # libraries by default.
74 # We can manage to simulate some of them with
export/import
75 # files but the overriding of shared library functions or
76 # variables by the main
program doesn
't seem possible.
77 # We avoid testing those features.
78 set SHCFLAG "-DXCOFF_TEST"
80 # In order to avoid listing every symbols in an export file,
81 # the export will be done with -bexpall flag.
82 # However for imports, we must create the import file.
83 set file [open $tmpdir/xcoff-shared.imp w]
86 puts $file main_called
89 # XCOFF doesn't yet support debug sections compresion.
90 set COMPRESS_LDFLAG
""
93 if [istarget arm
*-*-linux
*] {
94 #
On ARM section anchors can change the symbol pre
-emptability
for
95 # non
-PIC shared libraries
, causing these tests to fail. Turn section
97 set SHCFLAG
"-fno-section-anchors"
99 #
On targets that have MOVW the compiler will emit relocations which
100 # the linker doesn
't support when compiling -shared without -fpic. The
101 # test to find out whether we want to XFAIL the non-PIC tests requires
102 # a compile - so we pre-calculate it here. We also note that this can
103 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
104 if [istarget arm
*-*-*eabi
*] {
105 set file
[open $tmpdir
/movw
-detect.c w
]
106 puts $file
"void foo(void) { __asm (\"movw r0, #0\"); }"
108 if [run_host_cmd_yesno
"$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
109 set shared_needs_pic
"yes"
114 set support_protected
"no"
116 if { [istarget
*-*-linux
*]
117 ||
[istarget
*-*-nacl
*]
118 ||
[istarget
*-*-gnu
*] } {
119 if [ld_compile
"$CC_FOR_TARGET -g -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
120 if [ld_link $CC_FOR_TARGET $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.
140 if { [is_xcoff_format
] } {
141 #
On AIX
, setup imports and exports.
142 append shared
" -Wl,-bexpall -Wl,-bI:$tmpdir/xcoff-shared.imp"
144 if { [is_elf_format
] && [check_shared_lib_support
] } {
145 append shared
" -Wl,-z,notext"
147 if {![ld_link $CC_FOR_TARGET $tmpdir
/$progname.so
"$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
148 if { [ string match $visibility
"hidden_undef" ]
149 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
150 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
152 } else { if { [ string match $visibility
"protected_undef" ]
153 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
154 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
162 # Link against the shared library. Use
-rpath so that the
163 # dynamic linker can locate the shared library at runtime.
164 #
On AIX
, we must
include /lib in
-rpath
, as otherwise the loader
168 if { [is_xcoff_format
] } {
169 set rpath
/lib
:$tmpdir
170 set exportflag
" -Wl,-bexpall"
172 if ![ld_link $CC_FOR_TARGET $tmpdir
/$progname
"-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so $exportflag"] {
173 if { [ string match $visibility
"hidden" ]
174 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
175 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
177 } else { if { [ string match $visibility
"hidden_undef_def" ]
178 && [regexp
"undefined reference to \`\.?visibility\'" $link_output]
179 && [regexp
"undefined reference to \`visibility_def\'" $link_output]
180 && [regexp
"undefined reference to \`\.?visibility_func\'" $link_output]
181 && [regexp
"undefined reference to \`visibility_var\'" $link_output] } {
189 if { [ string match $visibility
"hidden" ]
190 ||
[ string match $visibility
"hidden_undef" ]
191 ||
[ string match $visibility
"protected_undef" ] } {
196 unsupported
"$testname"
200 # Run the resulting
program
201 send_log
"$tmpdir/$progname >$tmpdir/$progname.out\n"
202 verbose
"$tmpdir/$progname >$tmpdir/$progname.out"
203 catch
"exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
204 if ![string match
"" $exec_output] then {
205 send_log
"$exec_output\n"
206 verbose
"$exec_output"
211 send_log
"diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
212 verbose
"diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
213 catch
"exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
214 set exec_output
[prune_warnings $exec_output
]
216 if {![string match
"" $exec_output]} then {
217 send_log
"$exec_output\n"
218 verbose
"$exec_output"
226 proc visibility_run
{visibility
} {
233 global target_triplet
234 global support_protected
235 global shared_needs_pic
238 global COMPRESS_LDFLAG
239 global NOSANITIZE_CFLAGS
242 if [ string match $visibility
"hidden" ] {
243 set VSBCFLAG
"-DHIDDEN_TEST"
244 } else { if [ string match $visibility
"hidden_normal" ] {
245 set VSBCFLAG
"-DHIDDEN_NORMAL_TEST"
246 } else { if [ string match $visibility
"hidden_undef" ] {
247 set VSBCFLAG
"-DHIDDEN_UNDEF_TEST"
248 } else { if [ string match $visibility
"hidden_undef_def" ] {
249 set VSBCFLAG
"-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
250 } else { if [ string match $visibility
"hidden_weak" ] {
251 set VSBCFLAG
"-DHIDDEN_WEAK_TEST"
252 } else { if [ string match $visibility
"protected" ] {
253 set VSBCFLAG
"-DPROTECTED_TEST"
254 } else { if [ string match $visibility
"protected_undef" ] {
255 set VSBCFLAG
"-DPROTECTED_UNDEF_TEST"
256 } else { if [ string match $visibility
"protected_undef_def" ] {
257 set VSBCFLAG
"-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
258 } else { if [ string match $visibility
"protected_weak" ] {
259 set VSBCFLAG
"-DPROTECTED_WEAK_TEST"
264 # Ensure we always start with a clean slate
, for the
"file exists"
266 remote_file host
delete $tmpdir
/sh1p.o $tmpdir
/sh2p.o $tmpdir
/sh1np.o $tmpdir
/sh2np.o
269 if { [is_xcoff_format
] } {
270 # As explained above
, XCOFF shared libraries doesn
't support
271 # all the ELF features. Thus, the output of the tests are
276 if { [istarget powerpc*-*-linux*] \
277 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
278 # Testing non-PIC libraries is a waste of effort on any target.
279 # If you don't pass
-fpic or
-fPIC to gcc
, gcc will assume quite
280 # reasonably that you are not compiling
for a shared library.
281 # It can
then make optimisations that result in shared library
282 # functions and variables not being overridable. Newer versions
283 # of gcc are more likely to
do this.
285 #
Compile the main
program. Make sure that PLT is used since PLT
287 if ![ld_compile
"$CC_FOR_TARGET -g $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
288 unsupported
"visibility ($visibility) (non PIC)"
289 unsupported
"visibility ($visibility)"
291 # The shared library is composed of two files. First
compile them
292 # without using
-fpic. That should work
on an ELF
system,
293 # although it will be less efficient because the dynamic linker
294 # will need to
do more relocation work. However
, note that not
295 # using
-fpic will cause some of the tests to
return different
296 # results. Make sure that PLT is used since PLT is expected.
297 if { ![ld_compile
"$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
298 ||
![ld_compile
"$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
299 unsupported
"visibility ($visibility) (non PIC)"
301 # SunOS non PIC shared libraries don
't permit some cases of
303 if { [ string match $visibility "protected" ]
304 || [ string match $visibility "protected_undef_def" ] } {
305 if [ string match $support_protected "no" ] {
306 setup_xfail $target_triplet
310 # Non-pic code uses name binding rules for applications to
311 # reference variables by gp-relative relocs, which can't be
312 # used with overridable symbols.
313 if { ![ string match $visibility
"hidden_undef" ]
314 && ![ string match $visibility
"protected_undef" ] } {
315 setup_xfail
"ia64-*-linux*"
316 setup_xfail
"alpha*-*-linux*"
318 if { ![ string match $visibility
"hidden" ]
319 && ![ string match $visibility
"hidden_undef" ]
320 && ![ string match $visibility
"hidden_undef_def" ]
321 && ![ string match $visibility
"protected_undef" ] } {
322 setup_xfail
"s390x-*-linux*"
323 if { [istarget sparc
*-*-linux
*] && [is_elf64 $tmpdir
/mainnp.o
] } {
324 setup_xfail
"sparc*-*-linux*"
327 if { [is_elf64 $tmpdir
/mainnp.o
] } {
328 setup_xfail
"x86_64-*-linux*"
330 setup_xfail
"x86_64-*-linux-gnux32"
331 if { ![istarget hppa
*64*-*-linux
*] } {
332 setup_xfail
"hppa*-*-linux*"
334 if [ string match $shared_needs_pic
"yes" ] {
335 setup_xfail
"arm*-*-linux*"
338 # XCOFF format doesn
't know how to handle weak undefined symbols
340 if { [ string match $visibility "hidden_weak" ]
341 || [ string match $visibility "protected_weak" ] } {
342 setup_xfail "*-*-aix*"
343 setup_xfail "*-*-beos*"
346 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o $datfile
348 # Test ELF shared library relocations with a non-zero load
349 # address for the library. Near as I can tell, the R_*_RELATIVE
350 # relocations for various targets are broken in the case where
351 # the load address is not zero (which is the default).
352 if { [ string match $visibility "protected" ]
353 || [ string match $visibility "protected_undef_def" ] } {
354 if [ string match $support_protected "no" ] {
355 setup_xfail $target_triplet
358 if { [ string match $visibility "hidden_normal" ]
359 || [ string match $visibility "hidden_weak" ]
360 || [ string match $visibility "protected" ]
361 || [ string match $visibility "protected_undef_def" ]
362 || [ string match $visibility "protected_weak" ]
363 || [ string match $visibility "normal" ] } {
364 setup_xfail "powerpc-*-linux*"
365 setup_xfail "s390x-*-linux*"
366 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
367 setup_xfail "sparc*-*-linux*"
370 if { ![ string match $visibility "hidden_undef" ]
371 && ![ string match $visibility "protected_undef" ] } {
372 setup_xfail "ia64-*-linux*"
373 setup_xfail "alpha*-*-linux*"
374 setup_xfail "mips*-*-linux*"
376 if { [is_elf64 $tmpdir/mainnp.o] } {
377 setup_xfail "x86_64-*-linux*"
379 setup_xfail "x86_64-*-linux-gnux32"
380 if { ![istarget hppa*64*-*-linux*] } {
381 setup_xfail "hppa*-*-linux*"
383 if [ string match $shared_needs_pic "yes" ] {
384 setup_xfail "arm*-*-linux*"
387 # XCOFF format doesn't know how to handle weak undefined symbols
389 if { [ string match $visibility
"hidden_weak" ]
390 ||
[ string match $visibility
"protected_weak" ] } {
391 setup_xfail
"*-*-aix*"
392 setup_xfail
"*-*-beos*"
395 if { ![is_xcoff_format
] } {
396 visibility_test $visibility vnp
"visibility ($visibility) (non PIC, load offset)" \
397 mainnp.o sh1np.o sh2np.o $datfile \
398 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
402 # Now
compile the code using
-fpic.
404 if { ![ld_compile
"$CC_FOR_TARGET -g $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
405 ||
![ld_compile
"$CC_FOR_TARGET -g $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
406 unsupported
"visibility ($visibility)"
408 if { [ string match $visibility
"protected" ]
409 ||
[ string match $visibility
"protected_undef_def" ] } {
410 if [ string match $support_protected
"no" ] {
411 setup_xfail $target_triplet
414 visibility_test $visibility vp
"visibility ($visibility)" mainnp.o sh1p.o sh2p.o $datfile $COMPRESS_LDFLAG
418 if { [istarget powerpc
*-*-linux
*] } {
421 # Now do the same tests again, but this time compile main.c PIC.
422 if ![ld_compile "$CC_FOR_TARGET -g $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
423 unsupported "visibility ($visibility) (PIC main, non PIC so)"
424 unsupported "visibility ($visibility) (PIC main)"
426 if { [ remote_file host exists $tmpdir/sh1np.o ]
427 && [ remote_file host exists $tmpdir/sh2np.o ] } {
428 # SunOS non PIC shared libraries don't
permit some cases of
430 if { [ string match $visibility
"protected" ]
431 ||
[ string match $visibility
"protected_undef_def" ] } {
432 if [ string match $support_protected
"no" ] {
433 setup_xfail $target_triplet
436 if { ![ string match $visibility
"hidden_undef" ]
437 && ![ string match $visibility
"protected_undef" ] } {
438 setup_xfail
"ia64-*-linux*"
439 setup_xfail
"alpha*-*-linux*"
441 if { ![ string match $visibility
"hidden" ]
442 && ![ string match $visibility
"hidden_undef" ]
443 && ![ string match $visibility
"hidden_undef_def" ]
444 && ![ string match $visibility
"protected_undef" ] } {
445 setup_xfail
"s390x-*-linux*"
446 if { [istarget sparc
*-*-linux
*] && [is_elf64 $tmpdir
/mainp.o
] } {
447 setup_xfail
"sparc*-*-linux*"
450 if { [is_elf64 $tmpdir
/mainp.o
] } {
451 setup_xfail
"x86_64-*-linux*"
453 setup_xfail
"x86_64-*-linux-gnux32"
454 if { ![istarget hppa
*64*-*-linux
*] } {
455 setup_xfail
"hppa*-*-linux*"
457 if [ string match $shared_needs_pic
"yes" ] {
458 setup_xfail
"arm*-*-linux*"
461 # XCOFF format doesn
't know how to handle weak undefined symbols
463 if { [ string match $visibility "hidden_weak" ]
464 || [ string match $visibility "protected_weak" ] } {
465 setup_xfail "*-*-aix*"
466 setup_xfail "*-*-beos*"
469 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o $datfile
471 unsupported "visibility (PIC main, non PIC so)"
474 if { [ remote_file host exists $tmpdir/sh1p.o ]
475 && [ remote_file host exists $tmpdir/sh2p.o ] } {
476 if { [ string match $visibility "protected" ]
477 || [ string match $visibility "protected_undef_def" ] } {
478 if [ string match $support_protected "no" ] {
479 setup_xfail $target_triplet
483 # XCOFF format doesn't know how to handle weak undefined symbols
485 if { [ string match $visibility
"hidden_weak" ]
486 ||
[ string match $visibility
"protected_weak" ] } {
487 setup_xfail
"*-*-aix*"
488 setup_xfail
"*-*-beos*"
491 visibility_test $visibility vmpp
"visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o $datfile
493 unsupported
"visibility ($visibility) (PIC main)"
498 # Old version of GCC
for MIPS default to enabling
-fpic
499 # and
get confused
if it is used
on the command line.
500 if { [istarget mips
*-*-*] && ! [at_least_gcc_version
4 3] } then {
503 # Unfortunately
, the gcc
argument is
-fpic and the cc
argument is
504 #
-KPIC. We have to try both.
506 send_log
"$CC_FOR_TARGET $picflag\n"
507 verbose
"$CC_FOR_TARGET $picflag"
508 catch
"exec $CC_FOR_TARGET $picflag" exec_output
509 send_log
"$exec_output\n"
510 verbose
"--" "$exec_output"
511 if { [string match
"*illegal option*" $exec_output] \
512 ||
[string match
"*option ignored*" $exec_output] \
513 ||
[string match
"*unrecognized option*" $exec_output] \
514 ||
[string match
"*passed to ld*" $exec_output] } {
518 verbose
"Using $picflag to compile PIC code"
520 visibility_run hidden
521 visibility_run hidden_normal
522 visibility_run hidden_undef
523 visibility_run hidden_undef_def
524 visibility_run hidden_weak
525 visibility_run protected
526 visibility_run protected_undef
527 visibility_run protected_undef_def
528 visibility_run protected_weak
529 visibility_run
normal
531 if { ![ld_compile
"$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
532 unsupported
"common hidden symbol"
534 if ![ld_link $
ld tmpdir
/common
"tmpdir/common.o"] {
535 fail
"common hidden symbol"
537 pass
"common hidden symbol"
541 if { ![ld_compile
"$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
542 unsupported
"weak hidden symbol"
544 if { ![ld_compile
"$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
545 unsupported
"weak hidden symbol"
548 if { [is_xcoff_format
] } {
549 #
On AIX
, setup imports and exports.
550 append shared
" -bexpall"
552 if ![ld_link $
ld tmpdir
/sh3.so
"$shared tmpdir/sh3.o"] {
553 fail
"weak hidden symbol"
555 if ![ld_link $
ld tmpdir
/weak
"tmpdir/test.o tmpdir/sh3.so"] {
556 fail
"weak hidden symbol DSO last"
558 pass
"weak hidden symbol DSO last"
560 if ![ld_link $
ld tmpdir
/weak
"tmpdir/sh3.so tmpdir/test.o"] {
561 fail
"weak hidden symbol DSO first"
563 pass
"weak hidden symbol DSO first"