1 # Expect script for ld-visibility tests
2 # Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2010, 2012
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 i?86-*-gnu*] \
34 && ![istarget *-*-nacl*] \
35 && ![istarget ia64-*-linux*] \
36 && ![istarget m68k-*-linux*] \
37 && ![istarget mips*-*-linux*] \
38 && ![istarget powerpc*-*-linux*] \
39 && ![istarget arm*-*-linux*] \
40 && ![istarget alpha*-*-linux*] \
41 && ![istarget sparc*-*-linux*] \
42 && ![istarget s390*-*-linux*] \
43 && ![istarget sh\[34\]*-*-linux*] \
44 && ![istarget x86_64-*-linux*] } {
48 if { [istarget *-*-linux*aout*] \
49 || [istarget *-*-linux*oldld*] } {
53 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
54 foreach t $test_list {
55 # We need to strip the ".d", but can leave the dirname.
56 verbose [file rootname $t]
57 run_dump_test [file rootname $t]
60 # The remaining tests can only be run if ld generates native executables.
61 if ![isnative] then {return}
65 set shared_needs_pic "no"
67 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
69 # AIX shared libraries do not seem to support useful features,
70 # like overriding the shared library function or letting the
71 # shared library refer to objects defined in the main program. We
72 # avoid testing those features.
73 set SHCFLAG "-DXCOFF_TEST"
75 # The AIX 3.2.5 loader appears to randomly fail when loading
76 # shared libraries from NSF mounted partitions, so we avoid any
77 # potential problems by using a local directory.
78 catch {exec /bin/sh -c "echo $$"} pid
79 set tmpdir /usr/tmp/ld.$pid
80 catch "exec mkdir $tmpdir" exec_status
82 # On AIX, we need to explicitly export the symbols the shared
83 # library is going to provide, and need.
84 set file [open $tmpdir/xcoff.exp w]
87 puts $file shlib_shlibvar1
88 puts $file shlib_shlibvar2
89 puts $file shlib_shlibcall
90 puts $file shlib_shlibcalled
91 puts $file shlib_checkfunptr1
92 puts $file shlib_getfunptr1
93 puts $file shlib_check
97 if [istarget arm*-*-linux*] {
98 # On ARM section anchors can change the symbol pre-emptability for
99 # non-PIC shared libraries, causing these tests to fail. Turn section
101 set SHCFLAG "-fno-section-anchors"
103 # On targets that have MOVW the compiler will emit relocations which
104 # the linker doesn't support when compiling -shared without -fpic. The
105 # test to find out whether we want to XFAIL the non-PIC tests requires
106 # a compile - so we pre-calculate it here. We also note that this can
107 # only affect arm*-*-*eabi targets as the old ABI doesn't support v7.
108 if [istarget arm*-*-*eabi] {
109 set file [open $tmpdir/movw-detect.c w]
110 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
112 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
113 set shared_needs_pic "yes"
118 set support_protected "no"
120 if { [istarget *-*-linux*]
121 || [istarget *-*-nacl*]
122 || [istarget *-*-gnu*] } {
123 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
124 if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
125 catch "exec $tmpdir/main" support_protected
130 # The test procedure.
131 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
140 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
142 # Build the shared library.
143 # On AIX, we need to use an export file.
145 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
146 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
148 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
149 if { [ string match $visibility "hidden_undef" ]
150 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
151 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
153 } else { if { [ string match $visibility "protected_undef" ]
154 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
155 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
163 # Link against the shared library. Use -rpath so that the
164 # dynamic linker can locate the shared library at runtime.
165 # On AIX, we must include /lib in -rpath, as otherwise the loader
168 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
169 set rpath /lib:$tmpdir
171 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
172 if { [ string match $visibility "hidden" ]
173 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
174 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
176 } else { if { [ string match $visibility "hidden_undef_def" ]
177 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
178 && [regexp "undefined reference to \`visibility_def\'" $link_output]
179 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
180 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
188 if { [ string match $visibility "hidden" ]
189 || [ string match $visibility "hidden_undef" ]
190 || [ string match $visibility "protected_undef" ] } {
194 # Run the resulting program
195 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
196 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
197 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
198 if ![string match "" $exec_output] then {
199 send_log "$exec_output\n"
200 verbose "$exec_output"
205 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
206 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
207 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
208 set exec_output [prune_warnings $exec_output]
210 if {![string match "" $exec_output]} then {
211 send_log "$exec_output\n"
212 verbose "$exec_output"
220 proc visibility_run {visibility} {
228 global target_triplet
229 global support_protected
230 global shared_needs_pic
232 if [ string match $visibility "hidden" ] {
233 set VSBCFLAG "-DHIDDEN_TEST"
234 } else { if [ string match $visibility "hidden_normal" ] {
235 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
236 } else { if [ string match $visibility "hidden_undef" ] {
237 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
238 } else { if [ string match $visibility "hidden_undef_def" ] {
239 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
240 } else { if [ string match $visibility "hidden_weak" ] {
241 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
242 } else { if [ string match $visibility "protected" ] {
243 set VSBCFLAG "-DPROTECTED_TEST"
244 } else { if [ string match $visibility "protected_undef" ] {
245 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
246 } else { if [ string match $visibility "protected_undef_def" ] {
247 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
248 } else { if [ string match $visibility "protected_weak" ] {
249 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
254 if { [istarget powerpc*-*-linux*] \
255 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
256 # Testing non-PIC libraries is a waste of effort on any target.
257 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
258 # reasonably that you are not compiling for a shared library.
259 # It can then make optimisations that result in shared library
260 # functions and variables not being overridable. Newer versions
261 # of gcc are more likely to do this.
263 # Compile the main program.
264 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
265 unresolved "visibility ($visibility) (non PIC)"
266 unresolved "visibility ($visibility)"
268 # The shared library is composed of two files. First compile them
269 # without using -fpic. That should work on an ELF system,
270 # although it will be less efficient because the dynamic linker
271 # will need to do more relocation work. However, note that not
272 # using -fpic will cause some of the tests to return different
274 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
275 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
276 unresolved "visibility ($visibility) (non PIC)"
277 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
278 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
280 # SunOS non PIC shared libraries don't permit some cases of
282 if { [ string match $visibility "protected" ]
283 || [ string match $visibility "protected_undef_def" ] } {
284 if [ string match $support_protected "no" ] {
285 setup_xfail $target_triplet
288 setup_xfail "*-*-sunos4*"
291 # Non-pic code uses name binding rules for applications to
292 # reference variables by gp-relative relocs, which can't be
293 # used with overridable symbols.
294 if { ![ string match $visibility "hidden_undef" ]
295 && ![ string match $visibility "protected_undef" ] } {
296 setup_xfail "ia64-*-linux*"
297 setup_xfail "alpha*-*-linux*"
299 if { ![ string match $visibility "hidden" ]
300 && ![ string match $visibility "hidden_undef" ]
301 && ![ string match $visibility "hidden_undef_def" ]
302 && ![ string match $visibility "protected_undef" ] } {
303 setup_xfail "s390x-*-linux*"
304 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
305 setup_xfail "sparc*-*-linux*"
308 if { [is_elf64 $tmpdir/mainnp.o] } {
309 setup_xfail "x86_64-*-linux*"
311 setup_xfail "x86_64-*-linux-gnux32"
312 if { ![istarget hppa*64*-*-linux*] } {
313 setup_xfail "hppa*-*-linux*"
315 if [ string match $shared_needs_pic "yes" ] {
316 setup_xfail "arm*-*-linux*"
319 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
321 # Test ELF shared library relocations with a non-zero load
322 # address for the library. Near as I can tell, the R_*_RELATIVE
323 # relocations for various targets are broken in the case where
324 # the load address is not zero (which is the default).
325 if { [ string match $visibility "protected" ]
326 || [ string match $visibility "protected_undef_def" ] } {
327 if [ string match $support_protected "no" ] {
328 setup_xfail $target_triplet
331 setup_xfail "*-*-sunos4*"
332 setup_xfail "*-*-linux*libc1"
334 if { [ string match $visibility "hidden_normal" ]
335 || [ string match $visibility "hidden_weak" ]
336 || [ string match $visibility "protected" ]
337 || [ string match $visibility "protected_undef_def" ]
338 || [ string match $visibility "protected_weak" ]
339 || [ string match $visibility "normal" ] } {
340 setup_xfail "powerpc-*-linux*"
341 setup_xfail "s390x-*-linux*"
342 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
343 setup_xfail "sparc*-*-linux*"
346 if { ![ string match $visibility "hidden_undef" ]
347 && ![ string match $visibility "protected_undef" ] } {
348 setup_xfail "ia64-*-linux*"
349 setup_xfail "alpha*-*-linux*"
350 setup_xfail "mips*-*-linux*"
352 if { [is_elf64 $tmpdir/mainnp.o] } {
353 setup_xfail "x86_64-*-linux*"
355 setup_xfail "x86_64-*-linux-gnux32"
356 if { ![istarget hppa*64*-*-linux*] } {
357 setup_xfail "hppa*-*-linux*"
359 if [ string match $shared_needs_pic "yes" ] {
360 setup_xfail "arm*-*-linux*"
363 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
364 mainnp.o sh1np.o sh2np.o elfvsb \
365 "-T $srcdir/$subdir/elf-offset.ld"
368 # Now compile the code using -fpic.
370 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
371 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
372 unresolved "visibility ($visibility)"
374 if { [ string match $visibility "protected" ]
375 || [ string match $visibility "protected_undef_def" ] } {
376 if [ string match $support_protected "no" ] {
377 setup_xfail $target_triplet
380 # SunOS can not compare function pointers correctly
381 if [istarget "*-*-sunos4*"] {
382 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
383 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
384 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
386 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
391 if { [istarget powerpc*-*-linux*] } {
394 # Now do the same tests again, but this time compile main.c PIC.
395 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
396 unresolved "visibility ($visibility) (PIC main, non PIC so)"
397 unresolved "visibility ($visibility) (PIC main)"
399 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
400 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
401 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
403 # SunOS non PIC shared libraries don't permit some cases of
405 if { [ string match $visibility "protected" ]
406 || [ string match $visibility "protected_undef_def" ] } {
407 if [ string match $support_protected "no" ] {
408 setup_xfail $target_triplet
411 setup_xfail "*-*-sunos4*"
413 if { ![ string match $visibility "hidden_undef" ]
414 && ![ string match $visibility "protected_undef" ] } {
415 setup_xfail "ia64-*-linux*"
416 setup_xfail "alpha*-*-linux*"
418 if { ![ string match $visibility "hidden" ]
419 && ![ string match $visibility "hidden_undef" ]
420 && ![ string match $visibility "hidden_undef_def" ]
421 && ![ string match $visibility "protected_undef" ] } {
422 setup_xfail "s390x-*-linux*"
423 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
424 setup_xfail "sparc*-*-linux*"
427 if { [is_elf64 $tmpdir/mainp.o] } {
428 setup_xfail "x86_64-*-linux*"
430 setup_xfail "x86_64-*-linux-gnux32"
431 if { ![istarget hppa*64*-*-linux*] } {
432 setup_xfail "hppa*-*-linux*"
434 if [ string match $shared_needs_pic "yes" ] {
435 setup_xfail "arm*-*-linux*"
438 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
441 unresolved "visibility (PIC main, non PIC so)"
444 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
445 if { [ string match $visibility "protected" ]
446 || [ string match $visibility "protected_undef_def" ] } {
447 if [ string match $support_protected "no" ] {
448 setup_xfail $target_triplet
451 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
452 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
454 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
457 unresolved "visibility ($visibility) (PIC main)"
462 # Old version of GCC for MIPS default to enabling -fpic
463 # and get confused if it is used on the command line.
464 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
467 # Unfortunately, the gcc argument is -fpic and the cc argument is
468 # -KPIC. We have to try both.
470 send_log "$CC $picflag\n"
471 verbose "$CC $picflag"
472 catch "exec $CC $picflag" exec_output
473 send_log "$exec_output\n"
474 verbose "--" "$exec_output"
475 if { [string match "*illegal option*" $exec_output] \
476 || [string match "*option ignored*" $exec_output] \
477 || [string match "*unrecognized option*" $exec_output] \
478 || [string match "*passed to ld*" $exec_output] } {
479 if [istarget *-*-sunos4*] {
486 verbose "Using $picflag to compile PIC code"
488 visibility_run hidden
489 visibility_run hidden_normal
490 visibility_run hidden_undef
491 visibility_run hidden_undef_def
492 visibility_run hidden_weak
493 visibility_run protected
494 visibility_run protected_undef
495 visibility_run protected_undef_def
496 visibility_run protected_weak
497 visibility_run normal
499 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
500 unresolved "common hidden symbol"
502 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
503 fail "common hidden symbol"
505 pass "common hidden symbol"
509 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
510 unresolved "weak hidden symbol"
512 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
513 unresolved "weak hidden symbol"
515 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
516 fail "weak hidden symbol"
518 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
519 fail "weak hidden symbol DSO last"
521 pass "weak hidden symbol DSO last"
523 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
524 fail "weak hidden symbol DSO first"
526 pass "weak hidden symbol DSO first"
532 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
533 # Remove the temporary directory.
534 catch "exec rm -rf $tmpdir" exec_status