1 # Expect script for ld-visibility tests
2 # Copyright (C) 2000-2019 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.
33 # Square bracket expressions seem to confuse istarget.
34 if { ![istarget hppa*64*-*-hpux*] \
35 && ![istarget hppa*-*-linux*] \
36 && ![istarget i?86-*-linux*] \
37 && ![istarget i?86-*-gnu*] \
38 && ![istarget *-*-nacl*] \
39 && ![istarget ia64-*-linux*] \
40 && ![istarget m68k-*-linux*] \
41 && ![istarget mips*-*-linux*] \
42 && ![istarget powerpc*-*-linux*] \
43 && ![istarget arm*-*-linux*] \
44 && ![istarget alpha*-*-linux*] \
45 && ![istarget sparc*-*-linux*] \
46 && ![istarget s390*-*-linux*] \
47 && ![istarget sh\[34\]*-*-linux*] \
48 && ![istarget x86_64-*-linux*] } {
52 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
53 foreach t $test_list {
54 # We need to strip the ".d", but can leave the dirname.
55 verbose [file rootname $t]
56 run_dump_test [file rootname $t]
61 set shared_needs_pic "no"
62 set COMPRESS_LDFLAG "-Wl,--compress-debug-sections=zlib-gabi"
64 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
66 # AIX shared libraries do not seem to support useful features,
67 # like overriding the shared library function or letting the
68 # shared library refer to objects defined in the main program. We
69 # avoid testing those features.
70 set SHCFLAG "-DXCOFF_TEST"
72 # The AIX 3.2.5 loader appears to randomly fail when loading
73 # shared libraries from NSF mounted partitions, so we avoid any
74 # potential problems by using a local directory.
75 catch {exec /bin/sh -c "echo $$"} pid
76 set tmpdir /usr/tmp/ld.$pid
77 catch "exec mkdir $tmpdir" exec_status
79 # On AIX, we need to explicitly export the symbols the shared
80 # library is going to provide, and need.
81 set file [open $tmpdir/xcoff.exp w]
84 puts $file shlib_shlibvar1
85 puts $file shlib_shlibvar2
86 puts $file shlib_shlibcall
87 puts $file shlib_shlibcalled
88 puts $file shlib_checkfunptr1
89 puts $file shlib_getfunptr1
90 puts $file shlib_check
94 if [istarget arm*-*-linux*] {
95 # On ARM section anchors can change the symbol pre-emptability for
96 # non-PIC shared libraries, causing these tests to fail. Turn section
98 set SHCFLAG "-fno-section-anchors"
100 # On targets that have MOVW the compiler will emit relocations which
101 # the linker doesn't support when compiling -shared without -fpic. The
102 # test to find out whether we want to XFAIL the non-PIC tests requires
103 # a compile - so we pre-calculate it here. We also note that this can
104 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
105 if [istarget arm*-*-*eabi*] {
106 set file [open $tmpdir/movw-detect.c w]
107 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
109 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
110 set shared_needs_pic "yes"
115 set support_protected "no"
117 if { [istarget *-*-linux*]
118 || [istarget *-*-nacl*]
119 || [istarget *-*-gnu*] } {
120 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
121 if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
122 catch "exec $tmpdir/main" support_protected
127 # The test procedure.
128 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
137 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
139 # Build the shared library.
140 # On AIX, we need to use an export file.
142 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
143 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
145 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
146 if { [ string match $visibility "hidden_undef" ]
147 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
148 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
150 } else { if { [ string match $visibility "protected_undef" ]
151 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
152 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
160 # Link against the shared library. Use -rpath so that the
161 # dynamic linker can locate the shared library at runtime.
162 # On AIX, we must include /lib in -rpath, as otherwise the loader
165 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
166 set rpath /lib:$tmpdir
168 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
169 if { [ string match $visibility "hidden" ]
170 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
171 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
173 } else { if { [ string match $visibility "hidden_undef_def" ]
174 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
175 && [regexp "undefined reference to \`visibility_def\'" $link_output]
176 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
177 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
185 if { [ string match $visibility "hidden" ]
186 || [ string match $visibility "hidden_undef" ]
187 || [ string match $visibility "protected_undef" ] } {
192 unsupported "$testname"
196 # Run the resulting program
197 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
198 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
199 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
200 if ![string match "" $exec_output] then {
201 send_log "$exec_output\n"
202 verbose "$exec_output"
207 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
208 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
209 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
210 set exec_output [prune_warnings $exec_output]
212 if {![string match "" $exec_output]} then {
213 send_log "$exec_output\n"
214 verbose "$exec_output"
222 proc visibility_run {visibility} {
230 global target_triplet
231 global support_protected
232 global shared_needs_pic
235 global COMPRESS_LDFLAG
237 if [ string match $visibility "hidden" ] {
238 set VSBCFLAG "-DHIDDEN_TEST"
239 } else { if [ string match $visibility "hidden_normal" ] {
240 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
241 } else { if [ string match $visibility "hidden_undef" ] {
242 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
243 } else { if [ string match $visibility "hidden_undef_def" ] {
244 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
245 } else { if [ string match $visibility "hidden_weak" ] {
246 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
247 } else { if [ string match $visibility "protected" ] {
248 set VSBCFLAG "-DPROTECTED_TEST"
249 } else { if [ string match $visibility "protected_undef" ] {
250 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
251 } else { if [ string match $visibility "protected_undef_def" ] {
252 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
253 } else { if [ string match $visibility "protected_weak" ] {
254 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
259 # Ensure we always start with a clean slate, for the "file exists"
261 remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
263 if { [istarget powerpc*-*-linux*] \
264 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
265 # Testing non-PIC libraries is a waste of effort on any target.
266 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
267 # reasonably that you are not compiling for a shared library.
268 # It can then make optimisations that result in shared library
269 # functions and variables not being overridable. Newer versions
270 # of gcc are more likely to do this.
272 # Compile the main program. Make sure that PLT is used since PLT
274 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
275 unresolved "visibility ($visibility) (non PIC)"
276 unresolved "visibility ($visibility)"
278 # The shared library is composed of two files. First compile them
279 # without using -fpic. That should work on an ELF system,
280 # although it will be less efficient because the dynamic linker
281 # will need to do more relocation work. However, note that not
282 # using -fpic will cause some of the tests to return different
283 # results. Make sure that PLT is used since PLT is expected.
284 if { ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
285 || ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
286 unresolved "visibility ($visibility) (non PIC)"
287 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
288 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
290 # SunOS non PIC shared libraries don't permit some cases of
292 if { [ string match $visibility "protected" ]
293 || [ string match $visibility "protected_undef_def" ] } {
294 if [ string match $support_protected "no" ] {
295 setup_xfail $target_triplet
299 # Non-pic code uses name binding rules for applications to
300 # reference variables by gp-relative relocs, which can't be
301 # used with overridable symbols.
302 if { ![ string match $visibility "hidden_undef" ]
303 && ![ string match $visibility "protected_undef" ] } {
304 setup_xfail "ia64-*-linux*"
305 setup_xfail "alpha*-*-linux*"
307 if { ![ string match $visibility "hidden" ]
308 && ![ string match $visibility "hidden_undef" ]
309 && ![ string match $visibility "hidden_undef_def" ]
310 && ![ string match $visibility "protected_undef" ] } {
311 setup_xfail "s390x-*-linux*"
312 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
313 setup_xfail "sparc*-*-linux*"
316 if { [is_elf64 $tmpdir/mainnp.o] } {
317 setup_xfail "x86_64-*-linux*"
319 setup_xfail "x86_64-*-linux-gnux32"
320 if { ![istarget hppa*64*-*-linux*] } {
321 setup_xfail "hppa*-*-linux*"
323 if [ string match $shared_needs_pic "yes" ] {
324 setup_xfail "arm*-*-linux*"
327 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
329 # Test ELF shared library relocations with a non-zero load
330 # address for the library. Near as I can tell, the R_*_RELATIVE
331 # relocations for various targets are broken in the case where
332 # the load address is not zero (which is the default).
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 if { [ string match $visibility "hidden_normal" ]
340 || [ string match $visibility "hidden_weak" ]
341 || [ string match $visibility "protected" ]
342 || [ string match $visibility "protected_undef_def" ]
343 || [ string match $visibility "protected_weak" ]
344 || [ string match $visibility "normal" ] } {
345 setup_xfail "powerpc-*-linux*"
346 setup_xfail "s390x-*-linux*"
347 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
348 setup_xfail "sparc*-*-linux*"
351 if { ![ string match $visibility "hidden_undef" ]
352 && ![ string match $visibility "protected_undef" ] } {
353 setup_xfail "ia64-*-linux*"
354 setup_xfail "alpha*-*-linux*"
355 setup_xfail "mips*-*-linux*"
357 if { [is_elf64 $tmpdir/mainnp.o] } {
358 setup_xfail "x86_64-*-linux*"
360 setup_xfail "x86_64-*-linux-gnux32"
361 if { ![istarget hppa*64*-*-linux*] } {
362 setup_xfail "hppa*-*-linux*"
364 if [ string match $shared_needs_pic "yes" ] {
365 setup_xfail "arm*-*-linux*"
368 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
369 mainnp.o sh1np.o sh2np.o elfvsb \
370 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
373 # Now compile the code using -fpic.
375 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
376 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
377 unresolved "visibility ($visibility)"
379 if { [ string match $visibility "protected" ]
380 || [ string match $visibility "protected_undef_def" ] } {
381 if [ string match $support_protected "no" ] {
382 setup_xfail $target_triplet
385 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
386 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
388 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
393 if { [istarget powerpc*-*-linux*] } {
396 # Now do the same tests again, but this time compile main.c PIC.
397 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
398 unresolved "visibility ($visibility) (PIC main, non PIC so)"
399 unresolved "visibility ($visibility) (PIC main)"
401 if { [ remote_file host exists $tmpdir/sh1np.o ]
402 && [ remote_file host exists $tmpdir/sh2np.o ] } {
403 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
404 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
406 # SunOS non PIC shared libraries don't permit some cases of
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 if { ![ string match $visibility "hidden_undef" ]
415 && ![ string match $visibility "protected_undef" ] } {
416 setup_xfail "ia64-*-linux*"
417 setup_xfail "alpha*-*-linux*"
419 if { ![ string match $visibility "hidden" ]
420 && ![ string match $visibility "hidden_undef" ]
421 && ![ string match $visibility "hidden_undef_def" ]
422 && ![ string match $visibility "protected_undef" ] } {
423 setup_xfail "s390x-*-linux*"
424 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
425 setup_xfail "sparc*-*-linux*"
428 if { [is_elf64 $tmpdir/mainp.o] } {
429 setup_xfail "x86_64-*-linux*"
431 setup_xfail "x86_64-*-linux-gnux32"
432 if { ![istarget hppa*64*-*-linux*] } {
433 setup_xfail "hppa*-*-linux*"
435 if [ string match $shared_needs_pic "yes" ] {
436 setup_xfail "arm*-*-linux*"
439 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
442 unresolved "visibility (PIC main, non PIC so)"
445 if { [ remote_file host exists $tmpdir/sh1p.o ]
446 && [ remote_file host exists $tmpdir/sh2p.o ] } {
447 if { [ string match $visibility "protected" ]
448 || [ string match $visibility "protected_undef_def" ] } {
449 if [ string match $support_protected "no" ] {
450 setup_xfail $target_triplet
453 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
454 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
456 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
459 unresolved "visibility ($visibility) (PIC main)"
464 # Old version of GCC for MIPS default to enabling -fpic
465 # and get confused if it is used on the command line.
466 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
469 # Unfortunately, the gcc argument is -fpic and the cc argument is
470 # -KPIC. We have to try both.
472 send_log "$CC $picflag\n"
473 verbose "$CC $picflag"
474 catch "exec $CC $picflag" exec_output
475 send_log "$exec_output\n"
476 verbose "--" "$exec_output"
477 if { [string match "*illegal option*" $exec_output] \
478 || [string match "*option ignored*" $exec_output] \
479 || [string match "*unrecognized option*" $exec_output] \
480 || [string match "*passed to ld*" $exec_output] } {
484 verbose "Using $picflag to compile PIC code"
486 visibility_run hidden
487 visibility_run hidden_normal
488 visibility_run hidden_undef
489 visibility_run hidden_undef_def
490 visibility_run hidden_weak
491 visibility_run protected
492 visibility_run protected_undef
493 visibility_run protected_undef_def
494 visibility_run protected_weak
495 visibility_run normal
497 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
498 unresolved "common hidden symbol"
500 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
501 fail "common hidden symbol"
503 pass "common hidden symbol"
507 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
508 unresolved "weak hidden symbol"
510 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
511 unresolved "weak hidden symbol"
513 if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
514 fail "weak hidden symbol"
516 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
517 fail "weak hidden symbol DSO last"
519 pass "weak hidden symbol DSO last"
521 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
522 fail "weak hidden symbol DSO first"
524 pass "weak hidden symbol DSO first"
530 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
531 # Remove the temporary directory.
532 catch "exec rm -rf $tmpdir" exec_status