1 # Expect script for ld-visibility tests
2 # Copyright (C) 2000-2021 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 arm*-*-linux*] {
65 # On ARM section anchors can change the symbol pre-emptability for
66 # non-PIC shared libraries, causing these tests to fail. Turn section
68 set SHCFLAG "-fno-section-anchors"
70 # On targets that have MOVW the compiler will emit relocations which
71 # the linker doesn't support when compiling -shared without -fpic. The
72 # test to find out whether we want to XFAIL the non-PIC tests requires
73 # a compile - so we pre-calculate it here. We also note that this can
74 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
75 if [istarget arm*-*-*eabi*] {
76 set file [open $tmpdir/movw-detect.c w]
77 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
79 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
80 set shared_needs_pic "yes"
85 set support_protected "no"
87 if { [istarget *-*-linux*]
88 || [istarget *-*-nacl*]
89 || [istarget *-*-gnu*] } {
90 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
91 if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
92 catch "exec $tmpdir/main" support_protected
98 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
107 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
109 # Build the shared library.
111 if { [is_elf_format] && [check_shared_lib_support] } {
112 append shared " -Wl,-z,notext"
114 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
115 if { [ string match $visibility "hidden_undef" ]
116 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
117 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
119 } else { if { [ string match $visibility "protected_undef" ]
120 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
121 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
129 # Link against the shared library. Use -rpath so that the
130 # dynamic linker can locate the shared library at runtime.
132 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
133 if { [ string match $visibility "hidden" ]
134 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
135 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
137 } else { if { [ string match $visibility "hidden_undef_def" ]
138 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
139 && [regexp "undefined reference to \`visibility_def\'" $link_output]
140 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
141 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
149 if { [ string match $visibility "hidden" ]
150 || [ string match $visibility "hidden_undef" ]
151 || [ string match $visibility "protected_undef" ] } {
156 unsupported "$testname"
160 # Run the resulting program
161 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
162 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
163 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
164 if ![string match "" $exec_output] then {
165 send_log "$exec_output\n"
166 verbose "$exec_output"
171 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
172 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
173 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
174 set exec_output [prune_warnings $exec_output]
176 if {![string match "" $exec_output]} then {
177 send_log "$exec_output\n"
178 verbose "$exec_output"
186 proc visibility_run {visibility} {
194 global target_triplet
195 global support_protected
196 global shared_needs_pic
199 global COMPRESS_LDFLAG
200 global NOSANITIZE_CFLAGS
203 if [ string match $visibility "hidden" ] {
204 set VSBCFLAG "-DHIDDEN_TEST"
205 } else { if [ string match $visibility "hidden_normal" ] {
206 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
207 } else { if [ string match $visibility "hidden_undef" ] {
208 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
209 } else { if [ string match $visibility "hidden_undef_def" ] {
210 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
211 } else { if [ string match $visibility "hidden_weak" ] {
212 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
213 } else { if [ string match $visibility "protected" ] {
214 set VSBCFLAG "-DPROTECTED_TEST"
215 } else { if [ string match $visibility "protected_undef" ] {
216 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
217 } else { if [ string match $visibility "protected_undef_def" ] {
218 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
219 } else { if [ string match $visibility "protected_weak" ] {
220 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
225 # Ensure we always start with a clean slate, for the "file exists"
227 remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
229 if { [istarget powerpc*-*-linux*] \
230 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
231 # Testing non-PIC libraries is a waste of effort on any target.
232 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
233 # reasonably that you are not compiling for a shared library.
234 # It can then make optimisations that result in shared library
235 # functions and variables not being overridable. Newer versions
236 # of gcc are more likely to do this.
238 # Compile the main program. Make sure that PLT is used since PLT
240 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
241 unresolved "visibility ($visibility) (non PIC)"
242 unresolved "visibility ($visibility)"
244 # The shared library is composed of two files. First compile them
245 # without using -fpic. That should work on an ELF system,
246 # although it will be less efficient because the dynamic linker
247 # will need to do more relocation work. However, note that not
248 # using -fpic will cause some of the tests to return different
249 # results. Make sure that PLT is used since PLT is expected.
250 if { ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
251 || ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
252 unresolved "visibility ($visibility) (non PIC)"
254 # SunOS non PIC shared libraries don't permit some cases of
256 if { [ string match $visibility "protected" ]
257 || [ string match $visibility "protected_undef_def" ] } {
258 if [ string match $support_protected "no" ] {
259 setup_xfail $target_triplet
263 # Non-pic code uses name binding rules for applications to
264 # reference variables by gp-relative relocs, which can't be
265 # used with overridable symbols.
266 if { ![ string match $visibility "hidden_undef" ]
267 && ![ string match $visibility "protected_undef" ] } {
268 setup_xfail "ia64-*-linux*"
269 setup_xfail "alpha*-*-linux*"
271 if { ![ string match $visibility "hidden" ]
272 && ![ string match $visibility "hidden_undef" ]
273 && ![ string match $visibility "hidden_undef_def" ]
274 && ![ string match $visibility "protected_undef" ] } {
275 setup_xfail "s390x-*-linux*"
276 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
277 setup_xfail "sparc*-*-linux*"
280 if { [is_elf64 $tmpdir/mainnp.o] } {
281 setup_xfail "x86_64-*-linux*"
283 setup_xfail "x86_64-*-linux-gnux32"
284 if { ![istarget hppa*64*-*-linux*] } {
285 setup_xfail "hppa*-*-linux*"
287 if [ string match $shared_needs_pic "yes" ] {
288 setup_xfail "arm*-*-linux*"
291 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
293 # Test ELF shared library relocations with a non-zero load
294 # address for the library. Near as I can tell, the R_*_RELATIVE
295 # relocations for various targets are broken in the case where
296 # the load address is not zero (which is the default).
297 if { [ string match $visibility "protected" ]
298 || [ string match $visibility "protected_undef_def" ] } {
299 if [ string match $support_protected "no" ] {
300 setup_xfail $target_triplet
303 if { [ string match $visibility "hidden_normal" ]
304 || [ string match $visibility "hidden_weak" ]
305 || [ string match $visibility "protected" ]
306 || [ string match $visibility "protected_undef_def" ]
307 || [ string match $visibility "protected_weak" ]
308 || [ string match $visibility "normal" ] } {
309 setup_xfail "powerpc-*-linux*"
310 setup_xfail "s390x-*-linux*"
311 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
312 setup_xfail "sparc*-*-linux*"
315 if { ![ string match $visibility "hidden_undef" ]
316 && ![ string match $visibility "protected_undef" ] } {
317 setup_xfail "ia64-*-linux*"
318 setup_xfail "alpha*-*-linux*"
319 setup_xfail "mips*-*-linux*"
321 if { [is_elf64 $tmpdir/mainnp.o] } {
322 setup_xfail "x86_64-*-linux*"
324 setup_xfail "x86_64-*-linux-gnux32"
325 if { ![istarget hppa*64*-*-linux*] } {
326 setup_xfail "hppa*-*-linux*"
328 if [ string match $shared_needs_pic "yes" ] {
329 setup_xfail "arm*-*-linux*"
332 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
333 mainnp.o sh1np.o sh2np.o elfvsb \
334 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
337 # Now compile the code using -fpic.
339 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
340 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
341 unresolved "visibility ($visibility)"
343 if { [ string match $visibility "protected" ]
344 || [ string match $visibility "protected_undef_def" ] } {
345 if [ string match $support_protected "no" ] {
346 setup_xfail $target_triplet
349 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
353 if { [istarget powerpc*-*-linux*] } {
356 # Now do the same tests again, but this time compile main.c PIC.
357 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
358 unresolved "visibility ($visibility) (PIC main, non PIC so)"
359 unresolved "visibility ($visibility) (PIC main)"
361 if { [ remote_file host exists $tmpdir/sh1np.o ]
362 && [ remote_file host exists $tmpdir/sh2np.o ] } {
363 # SunOS non PIC shared libraries don't permit some cases of
365 if { [ string match $visibility "protected" ]
366 || [ string match $visibility "protected_undef_def" ] } {
367 if [ string match $support_protected "no" ] {
368 setup_xfail $target_triplet
371 if { ![ string match $visibility "hidden_undef" ]
372 && ![ string match $visibility "protected_undef" ] } {
373 setup_xfail "ia64-*-linux*"
374 setup_xfail "alpha*-*-linux*"
376 if { ![ string match $visibility "hidden" ]
377 && ![ string match $visibility "hidden_undef" ]
378 && ![ string match $visibility "hidden_undef_def" ]
379 && ![ string match $visibility "protected_undef" ] } {
380 setup_xfail "s390x-*-linux*"
381 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
382 setup_xfail "sparc*-*-linux*"
385 if { [is_elf64 $tmpdir/mainp.o] } {
386 setup_xfail "x86_64-*-linux*"
388 setup_xfail "x86_64-*-linux-gnux32"
389 if { ![istarget hppa*64*-*-linux*] } {
390 setup_xfail "hppa*-*-linux*"
392 if [ string match $shared_needs_pic "yes" ] {
393 setup_xfail "arm*-*-linux*"
396 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
398 unresolved "visibility (PIC main, non PIC so)"
401 if { [ remote_file host exists $tmpdir/sh1p.o ]
402 && [ remote_file host exists $tmpdir/sh2p.o ] } {
403 if { [ string match $visibility "protected" ]
404 || [ string match $visibility "protected_undef_def" ] } {
405 if [ string match $support_protected "no" ] {
406 setup_xfail $target_triplet
409 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
411 unresolved "visibility ($visibility) (PIC main)"
416 # Old version of GCC for MIPS default to enabling -fpic
417 # and get confused if it is used on the command line.
418 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
421 # Unfortunately, the gcc argument is -fpic and the cc argument is
422 # -KPIC. We have to try both.
424 send_log "$CC $picflag\n"
425 verbose "$CC $picflag"
426 catch "exec $CC $picflag" exec_output
427 send_log "$exec_output\n"
428 verbose "--" "$exec_output"
429 if { [string match "*illegal option*" $exec_output] \
430 || [string match "*option ignored*" $exec_output] \
431 || [string match "*unrecognized option*" $exec_output] \
432 || [string match "*passed to ld*" $exec_output] } {
436 verbose "Using $picflag to compile PIC code"
438 visibility_run hidden
439 visibility_run hidden_normal
440 visibility_run hidden_undef
441 visibility_run hidden_undef_def
442 visibility_run hidden_weak
443 visibility_run protected
444 visibility_run protected_undef
445 visibility_run protected_undef_def
446 visibility_run protected_weak
447 visibility_run normal
449 if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
450 unresolved "common hidden symbol"
452 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
453 fail "common hidden symbol"
455 pass "common hidden symbol"
459 if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
460 unresolved "weak hidden symbol"
462 if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
463 unresolved "weak hidden symbol"
465 if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
466 fail "weak hidden symbol"
468 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
469 fail "weak hidden symbol DSO last"
471 pass "weak hidden symbol DSO last"
473 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
474 fail "weak hidden symbol DSO first"
476 pass "weak hidden symbol DSO first"