1 # Expect script for ld-visibility tests
2 # Copyright (C) 2000-2017 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 { [which $CC] == 0 } {
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 if { [istarget *-*-linux*aout*] \
53 || [istarget *-*-linux*oldld*] } {
57 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
58 foreach t $test_list {
59 # We need to strip the ".d", but can leave the dirname.
60 verbose [file rootname $t]
61 run_dump_test [file rootname $t]
66 set shared_needs_pic "no"
67 set COMPRESS_LDFLAG "-Wl,--compress-debug-sections=zlib-gabi"
69 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
71 # AIX shared libraries do not seem to support useful features,
72 # like overriding the shared library function or letting the
73 # shared library refer to objects defined in the main program. We
74 # avoid testing those features.
75 set SHCFLAG "-DXCOFF_TEST"
77 # The AIX 3.2.5 loader appears to randomly fail when loading
78 # shared libraries from NSF mounted partitions, so we avoid any
79 # potential problems by using a local directory.
80 catch {exec /bin/sh -c "echo $$"} pid
81 set tmpdir /usr/tmp/ld.$pid
82 catch "exec mkdir $tmpdir" exec_status
84 # On AIX, we need to explicitly export the symbols the shared
85 # library is going to provide, and need.
86 set file [open $tmpdir/xcoff.exp w]
89 puts $file shlib_shlibvar1
90 puts $file shlib_shlibvar2
91 puts $file shlib_shlibcall
92 puts $file shlib_shlibcalled
93 puts $file shlib_checkfunptr1
94 puts $file shlib_getfunptr1
95 puts $file shlib_check
99 if [istarget arm*-*-linux*] {
100 # On ARM section anchors can change the symbol pre-emptability for
101 # non-PIC shared libraries, causing these tests to fail. Turn section
103 set SHCFLAG "-fno-section-anchors"
105 # On targets that have MOVW the compiler will emit relocations which
106 # the linker doesn't support when compiling -shared without -fpic. The
107 # test to find out whether we want to XFAIL the non-PIC tests requires
108 # a compile - so we pre-calculate it here. We also note that this can
109 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
110 if [istarget arm*-*-*eabi*] {
111 set file [open $tmpdir/movw-detect.c w]
112 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
114 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
115 set shared_needs_pic "yes"
120 set support_protected "no"
122 if { [istarget *-*-linux*]
123 || [istarget *-*-nacl*]
124 || [istarget *-*-gnu*] } {
125 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
126 if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
127 catch "exec $tmpdir/main" support_protected
132 # The test procedure.
133 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
142 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
144 # Build the shared library.
145 # On AIX, we need to use an export file.
147 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
148 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
150 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
151 if { [ string match $visibility "hidden_undef" ]
152 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
153 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
155 } else { if { [ string match $visibility "protected_undef" ]
156 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
157 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
165 # Link against the shared library. Use -rpath so that the
166 # dynamic linker can locate the shared library at runtime.
167 # On AIX, we must include /lib in -rpath, as otherwise the loader
170 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
171 set rpath /lib:$tmpdir
173 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
174 if { [ string match $visibility "hidden" ]
175 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
176 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
178 } else { if { [ string match $visibility "hidden_undef_def" ]
179 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
180 && [regexp "undefined reference to \`visibility_def\'" $link_output]
181 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
182 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
190 if { [ string match $visibility "hidden" ]
191 || [ string match $visibility "hidden_undef" ]
192 || [ string match $visibility "protected_undef" ] } {
197 unsupported "$testname"
201 # Run the resulting program
202 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
203 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
204 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
205 if ![string match "" $exec_output] then {
206 send_log "$exec_output\n"
207 verbose "$exec_output"
212 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
213 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
214 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
215 set exec_output [prune_warnings $exec_output]
217 if {![string match "" $exec_output]} then {
218 send_log "$exec_output\n"
219 verbose "$exec_output"
227 proc visibility_run {visibility} {
235 global target_triplet
236 global support_protected
237 global shared_needs_pic
239 global COMPRESS_LDFLAG
241 if [ string match $visibility "hidden" ] {
242 set VSBCFLAG "-DHIDDEN_TEST"
243 } else { if [ string match $visibility "hidden_normal" ] {
244 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
245 } else { if [ string match $visibility "hidden_undef" ] {
246 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
247 } else { if [ string match $visibility "hidden_undef_def" ] {
248 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
249 } else { if [ string match $visibility "hidden_weak" ] {
250 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
251 } else { if [ string match $visibility "protected" ] {
252 set VSBCFLAG "-DPROTECTED_TEST"
253 } else { if [ string match $visibility "protected_undef" ] {
254 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
255 } else { if [ string match $visibility "protected_undef_def" ] {
256 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
257 } else { if [ string match $visibility "protected_weak" ] {
258 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
263 # Ensure we always start with a clean slate, for the "file exists"
265 remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
267 if { [istarget powerpc*-*-linux*] \
268 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
269 # Testing non-PIC libraries is a waste of effort on any target.
270 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
271 # reasonably that you are not compiling for a shared library.
272 # It can then make optimisations that result in shared library
273 # functions and variables not being overridable. Newer versions
274 # of gcc are more likely to do this.
276 # Compile the main program. Make sure that PLT is used since PLT
278 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
279 unresolved "visibility ($visibility) (non PIC)"
280 unresolved "visibility ($visibility)"
282 # The shared library is composed of two files. First compile them
283 # without using -fpic. That should work on an ELF system,
284 # although it will be less efficient because the dynamic linker
285 # will need to do more relocation work. However, note that not
286 # using -fpic will cause some of the tests to return different
287 # results. Make sure that PLT is used since PLT is expected.
288 if { ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
289 || ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
290 unresolved "visibility ($visibility) (non PIC)"
291 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
292 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
294 # SunOS non PIC shared libraries don't permit some cases of
296 if { [ string match $visibility "protected" ]
297 || [ string match $visibility "protected_undef_def" ] } {
298 if [ string match $support_protected "no" ] {
299 setup_xfail $target_triplet
302 setup_xfail "*-*-sunos4*"
305 # Non-pic code uses name binding rules for applications to
306 # reference variables by gp-relative relocs, which can't be
307 # used with overridable symbols.
308 if { ![ string match $visibility "hidden_undef" ]
309 && ![ string match $visibility "protected_undef" ] } {
310 setup_xfail "ia64-*-linux*"
311 setup_xfail "alpha*-*-linux*"
313 if { ![ string match $visibility "hidden" ]
314 && ![ string match $visibility "hidden_undef" ]
315 && ![ string match $visibility "hidden_undef_def" ]
316 && ![ string match $visibility "protected_undef" ] } {
317 setup_xfail "s390x-*-linux*"
318 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
319 setup_xfail "sparc*-*-linux*"
322 if { [is_elf64 $tmpdir/mainnp.o] } {
323 setup_xfail "x86_64-*-linux*"
325 setup_xfail "x86_64-*-linux-gnux32"
326 if { ![istarget hppa*64*-*-linux*] } {
327 setup_xfail "hppa*-*-linux*"
329 if [ string match $shared_needs_pic "yes" ] {
330 setup_xfail "arm*-*-linux*"
333 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
335 # Test ELF shared library relocations with a non-zero load
336 # address for the library. Near as I can tell, the R_*_RELATIVE
337 # relocations for various targets are broken in the case where
338 # the load address is not zero (which is the default).
339 if { [ string match $visibility "protected" ]
340 || [ string match $visibility "protected_undef_def" ] } {
341 if [ string match $support_protected "no" ] {
342 setup_xfail $target_triplet
345 setup_xfail "*-*-sunos4*"
346 setup_xfail "*-*-linux*libc1"
348 if { [ string match $visibility "hidden_normal" ]
349 || [ string match $visibility "hidden_weak" ]
350 || [ string match $visibility "protected" ]
351 || [ string match $visibility "protected_undef_def" ]
352 || [ string match $visibility "protected_weak" ]
353 || [ string match $visibility "normal" ] } {
354 setup_xfail "powerpc-*-linux*"
355 setup_xfail "s390x-*-linux*"
356 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
357 setup_xfail "sparc*-*-linux*"
360 if { ![ string match $visibility "hidden_undef" ]
361 && ![ string match $visibility "protected_undef" ] } {
362 setup_xfail "ia64-*-linux*"
363 setup_xfail "alpha*-*-linux*"
364 setup_xfail "mips*-*-linux*"
366 if { [is_elf64 $tmpdir/mainnp.o] } {
367 setup_xfail "x86_64-*-linux*"
369 setup_xfail "x86_64-*-linux-gnux32"
370 if { ![istarget hppa*64*-*-linux*] } {
371 setup_xfail "hppa*-*-linux*"
373 if [ string match $shared_needs_pic "yes" ] {
374 setup_xfail "arm*-*-linux*"
377 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
378 mainnp.o sh1np.o sh2np.o elfvsb \
379 "-T $srcdir/$subdir/elf-offset.ld"
382 # Now compile the code using -fpic.
384 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
385 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
386 unresolved "visibility ($visibility)"
388 if { [ string match $visibility "protected" ]
389 || [ string match $visibility "protected_undef_def" ] } {
390 if [ string match $support_protected "no" ] {
391 setup_xfail $target_triplet
394 # SunOS can not compare function pointers correctly
395 if [istarget "*-*-sunos4*"] {
396 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
397 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
398 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
400 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
405 if { [istarget powerpc*-*-linux*] } {
408 # Now do the same tests again, but this time compile main.c PIC.
409 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
410 unresolved "visibility ($visibility) (PIC main, non PIC so)"
411 unresolved "visibility ($visibility) (PIC main)"
413 if { [ remote_file host exists $tmpdir/sh1np.o ]
414 && [ remote_file host exists $tmpdir/sh2np.o ] } {
415 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
416 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
418 # SunOS non PIC shared libraries don't permit some cases of
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 setup_xfail "*-*-sunos4*"
428 if { ![ string match $visibility "hidden_undef" ]
429 && ![ string match $visibility "protected_undef" ] } {
430 setup_xfail "ia64-*-linux*"
431 setup_xfail "alpha*-*-linux*"
433 if { ![ string match $visibility "hidden" ]
434 && ![ string match $visibility "hidden_undef" ]
435 && ![ string match $visibility "hidden_undef_def" ]
436 && ![ string match $visibility "protected_undef" ] } {
437 setup_xfail "s390x-*-linux*"
438 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
439 setup_xfail "sparc*-*-linux*"
442 if { [is_elf64 $tmpdir/mainp.o] } {
443 setup_xfail "x86_64-*-linux*"
445 setup_xfail "x86_64-*-linux-gnux32"
446 if { ![istarget hppa*64*-*-linux*] } {
447 setup_xfail "hppa*-*-linux*"
449 if [ string match $shared_needs_pic "yes" ] {
450 setup_xfail "arm*-*-linux*"
453 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
456 unresolved "visibility (PIC main, non PIC so)"
459 if { [ remote_file host exists $tmpdir/sh1p.o ]
460 && [ remote_file host exists $tmpdir/sh2p.o ] } {
461 if { [ string match $visibility "protected" ]
462 || [ string match $visibility "protected_undef_def" ] } {
463 if [ string match $support_protected "no" ] {
464 setup_xfail $target_triplet
467 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
468 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
470 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
473 unresolved "visibility ($visibility) (PIC main)"
478 # Old version of GCC for MIPS default to enabling -fpic
479 # and get confused if it is used on the command line.
480 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
483 # Unfortunately, the gcc argument is -fpic and the cc argument is
484 # -KPIC. We have to try both.
486 send_log "$CC $picflag\n"
487 verbose "$CC $picflag"
488 catch "exec $CC $picflag" exec_output
489 send_log "$exec_output\n"
490 verbose "--" "$exec_output"
491 if { [string match "*illegal option*" $exec_output] \
492 || [string match "*option ignored*" $exec_output] \
493 || [string match "*unrecognized option*" $exec_output] \
494 || [string match "*passed to ld*" $exec_output] } {
495 if [istarget *-*-sunos4*] {
502 verbose "Using $picflag to compile PIC code"
504 visibility_run hidden
505 visibility_run hidden_normal
506 visibility_run hidden_undef
507 visibility_run hidden_undef_def
508 visibility_run hidden_weak
509 visibility_run protected
510 visibility_run protected_undef
511 visibility_run protected_undef_def
512 visibility_run protected_weak
513 visibility_run normal
515 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
516 unresolved "common hidden symbol"
518 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
519 fail "common hidden symbol"
521 pass "common hidden symbol"
525 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
526 unresolved "weak hidden symbol"
528 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
529 unresolved "weak hidden symbol"
531 if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
532 fail "weak hidden symbol"
534 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
535 fail "weak hidden symbol DSO last"
537 pass "weak hidden symbol DSO last"
539 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
540 fail "weak hidden symbol DSO first"
542 pass "weak hidden symbol DSO first"
548 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
549 # Remove the temporary directory.
550 catch "exec rm -rf $tmpdir" exec_status