]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-vsb/vsb.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-vsb / vsb.exp
CommitLineData
6fc49d28 1# Expect script for ld-visibility tests
d87bef3a 2# Copyright (C) 2000-2023 Free Software Foundation, Inc.
6fc49d28 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
6# This program is free software; you can redistribute it and/or modify
6fc49d28 7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
6fc49d28 9# (at your option) any later version.
f96b4a7b 10#
6fc49d28
L
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.
f96b4a7b 15#
6fc49d28
L
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
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
6fc49d28
L
20#
21# Written by Ian Lance Taylor (ian@cygnus.com)
22# and H.J. Lu (hjl@gnu.org)
23#
24
25# Make sure that ld can generate ELF shared libraries with visibility.
26
d9816402 27# Check to see if the C compiler works
44ed8092 28if { ![check_compiler_available] } {
d9816402
AM
29 return
30}
31
a8bc481f
CC
32# This test can only be run on a couple of ELF platforms or with
33# XCOFF formats.
6fc49d28 34# Square bracket expressions seem to confuse istarget.
19c7c582
AM
35if { ![istarget hppa*64*-*-hpux*] \
36 && ![istarget hppa*-*-linux*] \
37 && ![istarget i?86-*-linux*] \
5940a93c 38 && ![istarget i?86-*-gnu*] \
5a68afcf 39 && ![istarget *-*-nacl*] \
ad995491 40 && ![istarget ia64-*-linux*] \
6fc49d28 41 && ![istarget m68k-*-linux*] \
66517a2f 42 && ![istarget mips*-*-linux*] \
81cacc15 43 && ![istarget powerpc*-*-linux*] \
4f38fc1c 44 && ![istarget arm*-*-linux*] \
2ffd68ef 45 && ![istarget alpha*-*-linux*] \
9147e853
JJ
46 && ![istarget sparc*-*-linux*] \
47 && ![istarget s390*-*-linux*] \
59758b1c 48 && ![istarget sh\[34\]*-*-linux*] \
a8bc481f
CC
49 && ![istarget x86_64-*-linux*] \
50 && ![is_xcoff_format] } {
6fc49d28
L
51 return
52}
53
a8bc481f
CC
54if [is_xcoff_format] {
55 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*-xcoff*.d]]
56} else {
57 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*-elf.d]]
58}
59
04827a14
L
60foreach 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]
64}
65
6fc49d28
L
66set tmpdir tmpdir
67set SHCFLAG ""
a9f844b1 68set shared_needs_pic "no"
91f8bf69 69set COMPRESS_LDFLAG "-Wl,--compress-debug-sections=zlib-gabi"
6fc49d28 70
a8bc481f
CC
71if { [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"
79
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]
84 puts $file "#! ."
85 puts $file mainvar
86 puts $file main_called
87 close $file
88
89 # XCOFF doesn't yet support debug sections compresion.
90 set COMPRESS_LDFLAG ""
91}
92
a9f844b1
NC
93if [istarget arm*-*-linux*] {
94 # On ARM section anchors can change the symbol pre-emptability for
5a68afcf 95 # non-PIC shared libraries, causing these tests to fail. Turn section
a9f844b1
NC
96 # anchors off.
97 set SHCFLAG "-fno-section-anchors"
98
5a68afcf 99 # On targets that have MOVW the compiler will emit relocations which
a9f844b1 100 # the linker doesn't support when compiling -shared without -fpic. The
5a68afcf 101 # test to find out whether we want to XFAIL the non-PIC tests requires
a9f844b1 102 # a compile - so we pre-calculate it here. We also note that this can
0085488a
WN
103 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
104 if [istarget arm*-*-*eabi*] {
a9f844b1
NC
105 set file [open $tmpdir/movw-detect.c w]
106 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
107 close $file
ad77db1c 108 if [run_host_cmd_yesno "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
a9f844b1
NC
109 set shared_needs_pic "yes"
110 }
111 }
112}
113
08c44e65
L
114set support_protected "no"
115
5940a93c 116if { [istarget *-*-linux*]
5a68afcf 117 || [istarget *-*-nacl*]
5940a93c 118 || [istarget *-*-gnu*] } {
669d5115 119 if [ld_compile "$CC_FOR_TARGET -g -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
ad77db1c 120 if [ld_link $CC_FOR_TARGET $tmpdir/main "$tmpdir/main.o"] {
08c44e65
L
121 catch "exec $tmpdir/main" support_protected
122 }
123 }
124}
125
6fc49d28
L
126# The test procedure.
127proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
ad77db1c 128 global CC_FOR_TARGET
6fc49d28
L
129 global srcdir
130 global subdir
131 global exec_output
132 global link_output
133 global host_triplet
134 global tmpdir
135
136 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
137
138 # Build the shared library.
6fc49d28 139 set shared -shared
a8bc481f
CC
140 if { [is_xcoff_format] } {
141 # On AIX, setup imports and exports.
142 append shared " -Wl,-bexpall -Wl,-bI:$tmpdir/xcoff-shared.imp"
143 }
af2c48d8
L
144 if { [is_elf_format] && [check_shared_lib_support] } {
145 append shared " -Wl,-z,notext"
146 }
ad77db1c 147 if {![ld_link $CC_FOR_TARGET $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
7cda33a1 148 if { [ string match $visibility "hidden_undef" ]
1ba54ee0
AM
149 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
150 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
151 pass "$testname"
152 } else { if { [ string match $visibility "protected_undef" ]
1ba54ee0
AM
153 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
154 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
155 pass "$testname"
156 } else {
157 fail "$testname"
158 }}
6fc49d28
L
159 return
160 }
161
162 # Link against the shared library. Use -rpath so that the
163 # dynamic linker can locate the shared library at runtime.
a8bc481f
CC
164 # On AIX, we must include /lib in -rpath, as otherwise the loader
165 # can not find -lc.
6fc49d28 166 set rpath $tmpdir
a8bc481f
CC
167 set exportflag ""
168 if { [is_xcoff_format] } {
169 set rpath /lib:$tmpdir
170 set exportflag " -Wl,-bexpall"
171 }
172 if ![ld_link $CC_FOR_TARGET $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so $exportflag"] {
6fc49d28 173 if { [ string match $visibility "hidden" ]
1ba54ee0
AM
174 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
175 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
176 pass "$testname"
177 } else { if { [ string match $visibility "hidden_undef_def" ]
1ba54ee0
AM
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] } {
6fc49d28
L
182 pass "$testname"
183 } else {
184 fail "$testname"
7cda33a1 185 }}
6fc49d28
L
186 return
187 }
188
7cda33a1
L
189 if { [ string match $visibility "hidden" ]
190 || [ string match $visibility "hidden_undef" ]
191 || [ string match $visibility "protected_undef" ] } {
6fc49d28
L
192 fail "$testname"
193 }
194
d9816402
AM
195 if ![isnative] {
196 unsupported "$testname"
197 return
198 }
199
6fc49d28
L
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"
207 fail "$testname"
208 return
209 }
210
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]
215
216 if {![string match "" $exec_output]} then {
217 send_log "$exec_output\n"
218 verbose "$exec_output"
219 fail "$testname"
220 return
221 }
222
223 pass "$testname"
224}
225
226proc visibility_run {visibility} {
ad77db1c 227 global CC_FOR_TARGET
6fc49d28
L
228 global SHCFLAG
229 global srcdir
230 global subdir
231 global tmpdir
232 global picflag
233 global target_triplet
08c44e65 234 global support_protected
a9f844b1 235 global shared_needs_pic
faebca03 236 global PLT_CFLAGS
922109c7 237 global NOPIE_CFLAGS
91f8bf69 238 global COMPRESS_LDFLAG
e062fcc8 239 global NOSANITIZE_CFLAGS
c6d47bff 240 global NOLTO_CFLAGS
6fc49d28
L
241
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"
7cda33a1
L
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"
6fc49d28
L
252 } else { if [ string match $visibility "protected" ] {
253 set VSBCFLAG "-DPROTECTED_TEST"
7cda33a1
L
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"
6fc49d28
L
260 } else {
261 set VSBCFLAG ""
7cda33a1 262 }}}}}}}}}
6fc49d28 263
37095d07
AM
264 # Ensure we always start with a clean slate, for the "file exists"
265 # tests below.
266 remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
267
a8bc481f
CC
268 set datfile elfvsb
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
272 # a bit different.
273 set datfile xcoffvsb
274 }
275
fb35d3d8
DD
276 if { [istarget powerpc*-*-linux*] \
277 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
2893311c
AM
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.
284 } else {
faebca03
L
285 # Compile the main program. Make sure that PLT is used since PLT
286 # is expected.
669d5115 287 if ![ld_compile "$CC_FOR_TARGET -g $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
5b1f6c95
AM
288 unsupported "visibility ($visibility) (non PIC)"
289 unsupported "visibility ($visibility)"
6fc49d28
L
290 } else {
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
faebca03 296 # results. Make sure that PLT is used since PLT is expected.
669d5115
AM
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] } {
5b1f6c95 299 unsupported "visibility ($visibility) (non PIC)"
6fc49d28
L
300 } else {
301 # SunOS non PIC shared libraries don't permit some cases of
302 # overriding.
1345a0c0
L
303 if { [ string match $visibility "protected" ]
304 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
305 if [ string match $support_protected "no" ] {
306 setup_xfail $target_triplet
307 }
6fc49d28 308 }
212a6b8e
RH
309
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.
ad995491
L
313 if { ![ string match $visibility "hidden_undef" ]
314 && ![ string match $visibility "protected_undef" ] } {
315 setup_xfail "ia64-*-linux*"
212a6b8e 316 setup_xfail "alpha*-*-linux*"
ad995491 317 }
4648dfcf
MS
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*"
4dc570c2
JJ
323 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
324 setup_xfail "sparc*-*-linux*"
325 }
4648dfcf 326 }
8b778942
L
327 if { [is_elf64 $tmpdir/mainnp.o] } {
328 setup_xfail "x86_64-*-linux*"
329 }
3c995545 330 setup_xfail "x86_64-*-linux-gnux32"
c0d48c0b
DA
331 if { ![istarget hppa*64*-*-linux*] } {
332 setup_xfail "hppa*-*-linux*"
333 }
a9f844b1
NC
334 if [ string match $shared_needs_pic "yes" ] {
335 setup_xfail "arm*-*-linux*"
336 }
212a6b8e 337
a8bc481f
CC
338 # XCOFF format doesn't know how to handle weak undefined symbols
339 # in shared objects.
340 if { [ string match $visibility "hidden_weak" ]
341 || [ string match $visibility "protected_weak" ] } {
342 setup_xfail "*-*-aix*"
343 setup_xfail "*-*-beos*"
344 }
345
346 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o $datfile
6fc49d28
L
347
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).
1345a0c0
L
352 if { [ string match $visibility "protected" ]
353 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
354 if [ string match $support_protected "no" ] {
355 setup_xfail $target_triplet
356 }
6fc49d28 357 }
d1d8dddf
L
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*"
8c37241b 365 setup_xfail "s390x-*-linux*"
4dc570c2
JJ
366 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
367 setup_xfail "sparc*-*-linux*"
368 }
d1d8dddf 369 }
ad995491
L
370 if { ![ string match $visibility "hidden_undef" ]
371 && ![ string match $visibility "protected_undef" ] } {
372 setup_xfail "ia64-*-linux*"
212a6b8e 373 setup_xfail "alpha*-*-linux*"
66517a2f 374 setup_xfail "mips*-*-linux*"
ad995491 375 }
8b778942
L
376 if { [is_elf64 $tmpdir/mainnp.o] } {
377 setup_xfail "x86_64-*-linux*"
378 }
3c995545 379 setup_xfail "x86_64-*-linux-gnux32"
c0d48c0b
DA
380 if { ![istarget hppa*64*-*-linux*] } {
381 setup_xfail "hppa*-*-linux*"
382 }
a9f844b1
NC
383 if [ string match $shared_needs_pic "yes" ] {
384 setup_xfail "arm*-*-linux*"
385 }
c0d48c0b 386
a8bc481f
CC
387 # XCOFF format doesn't know how to handle weak undefined symbols
388 # in shared objects.
389 if { [ string match $visibility "hidden_weak" ]
390 || [ string match $visibility "protected_weak" ] } {
391 setup_xfail "*-*-aix*"
392 setup_xfail "*-*-beos*"
393 }
394
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"
399 }
7193487f 400 }
6fc49d28
L
401
402 # Now compile the code using -fpic.
403
669d5115
AM
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] } {
5b1f6c95 406 unsupported "visibility ($visibility)"
6fc49d28 407 } else {
1345a0c0
L
408 if { [ string match $visibility "protected" ]
409 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
410 if [ string match $support_protected "no" ] {
411 setup_xfail $target_triplet
412 }
6fc49d28 413 }
a8bc481f 414 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o $datfile $COMPRESS_LDFLAG
6fc49d28 415 }
2893311c 416 }}
6fc49d28 417
2893311c
AM
418 if { [istarget powerpc*-*-linux*] } {
419 # Don't bother.
420 } else {
6fc49d28 421 # Now do the same tests again, but this time compile main.c PIC.
669d5115 422 if ![ld_compile "$CC_FOR_TARGET -g $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
5b1f6c95
AM
423 unsupported "visibility ($visibility) (PIC main, non PIC so)"
424 unsupported "visibility ($visibility) (PIC main)"
6fc49d28 425 } else {
37095d07
AM
426 if { [ remote_file host exists $tmpdir/sh1np.o ]
427 && [ remote_file host exists $tmpdir/sh2np.o ] } {
7193487f
AM
428 # SunOS non PIC shared libraries don't permit some cases of
429 # overriding.
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
c0d48c0b 434 }
7193487f
AM
435 }
436 if { ![ string match $visibility "hidden_undef" ]
437 && ![ string match $visibility "protected_undef" ] } {
438 setup_xfail "ia64-*-linux*"
439 setup_xfail "alpha*-*-linux*"
440 }
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*"
a9f844b1 448 }
6fc49d28 449 }
7193487f
AM
450 if { [is_elf64 $tmpdir/mainp.o] } {
451 setup_xfail "x86_64-*-linux*"
452 }
453 setup_xfail "x86_64-*-linux-gnux32"
454 if { ![istarget hppa*64*-*-linux*] } {
455 setup_xfail "hppa*-*-linux*"
456 }
457 if [ string match $shared_needs_pic "yes" ] {
458 setup_xfail "arm*-*-linux*"
459 }
460
a8bc481f
CC
461 # XCOFF format doesn't know how to handle weak undefined symbols
462 # in shared objects.
463 if { [ string match $visibility "hidden_weak" ]
464 || [ string match $visibility "protected_weak" ] } {
465 setup_xfail "*-*-aix*"
466 setup_xfail "*-*-beos*"
467 }
468
469 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o $datfile
6fc49d28 470 } else {
5b1f6c95 471 unsupported "visibility (PIC main, non PIC so)"
6fc49d28
L
472 }
473
37095d07
AM
474 if { [ remote_file host exists $tmpdir/sh1p.o ]
475 && [ remote_file host exists $tmpdir/sh2p.o ] } {
1345a0c0
L
476 if { [ string match $visibility "protected" ]
477 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
478 if [ string match $support_protected "no" ] {
479 setup_xfail $target_triplet
480 }
6fc49d28 481 }
a8bc481f
CC
482
483 # XCOFF format doesn't know how to handle weak undefined symbols
484 # in shared objects.
485 if { [ string match $visibility "hidden_weak" ]
486 || [ string match $visibility "protected_weak" ] } {
487 setup_xfail "*-*-aix*"
488 setup_xfail "*-*-beos*"
489 }
490
491 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o $datfile
6fc49d28 492 } else {
5b1f6c95 493 unsupported "visibility ($visibility) (PIC main)"
6fc49d28 494 }
2893311c 495 }}
6fc49d28
L
496}
497
fb35d3d8
DD
498# Old version of GCC for MIPS default to enabling -fpic
499# and get confused if it is used on the command line.
500if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
6fc49d28
L
501 set picflag ""
502} else {
503 # Unfortunately, the gcc argument is -fpic and the cc argument is
504 # -KPIC. We have to try both.
505 set picflag "-fpic"
ad77db1c
AM
506 send_log "$CC_FOR_TARGET $picflag\n"
507 verbose "$CC_FOR_TARGET $picflag"
508 catch "exec $CC_FOR_TARGET $picflag" exec_output
6fc49d28
L
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] } {
c9098af4 515 set picflag "-KPIC"
6fc49d28
L
516 }
517}
518verbose "Using $picflag to compile PIC code"
519
520visibility_run hidden
521visibility_run hidden_normal
7cda33a1
L
522visibility_run hidden_undef
523visibility_run hidden_undef_def
524visibility_run hidden_weak
6fc49d28 525visibility_run protected
7cda33a1
L
526visibility_run protected_undef
527visibility_run protected_undef_def
528visibility_run protected_weak
6fc49d28
L
529visibility_run normal
530
669d5115 531if { ![ld_compile "$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
5b1f6c95 532 unsupported "common hidden symbol"
22d5e339 533} else {
d9816402 534 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
22d5e339
L
535 fail "common hidden symbol"
536 } else {
537 pass "common hidden symbol"
538 }
539}
540
669d5115 541if { ![ld_compile "$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
5b1f6c95 542 unsupported "weak hidden symbol"
22d5e339 543} else {
669d5115 544 if { ![ld_compile "$CC_FOR_TARGET -g $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
5b1f6c95 545 unsupported "weak hidden symbol"
22d5e339 546 } else {
a8bc481f
CC
547 set shared "-shared"
548 if { [is_xcoff_format] } {
549 # On AIX, setup imports and exports.
550 append shared " -bexpall"
551 }
552 if ![ld_link $ld tmpdir/sh3.so "$shared tmpdir/sh3.o"] {
22d5e339
L
553 fail "weak hidden symbol"
554 } else {
eec685f8 555 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.so"] {
22d5e339
L
556 fail "weak hidden symbol DSO last"
557 } else {
558 pass "weak hidden symbol DSO last"
559 }
d9816402 560 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
22d5e339
L
561 fail "weak hidden symbol DSO first"
562 } else {
563 pass "weak hidden symbol DSO first"
564 }
565 }
566 }
567}