Update sources to GPLv3
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
CommitLineData
6fc49d28 1# Expect script for ld-visibility tests
f96b4a7b 2# Copyright 2000, 2001, 2002, 2003, 2004, 2007 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
6fc49d28
L
27# This test can only be run on a couple of ELF platforms.
28# Square bracket expressions seem to confuse istarget.
19c7c582
AM
29if { ![istarget hppa*64*-*-hpux*] \
30 && ![istarget hppa*-*-linux*] \
31 && ![istarget i?86-*-linux*] \
ad995491 32 && ![istarget ia64-*-linux*] \
6fc49d28 33 && ![istarget m68k-*-linux*] \
66517a2f 34 && ![istarget mips*-*-linux*] \
81cacc15 35 && ![istarget powerpc*-*-linux*] \
4f38fc1c 36 && ![istarget arm*-*-linux*] \
2ffd68ef 37 && ![istarget alpha*-*-linux*] \
9147e853
JJ
38 && ![istarget sparc*-*-linux*] \
39 && ![istarget s390*-*-linux*] \
59758b1c 40 && ![istarget sh\[34\]*-*-linux*] \
9147e853 41 && ![istarget x86_64-*-linux*] } {
6fc49d28
L
42 return
43}
44
45if { [istarget *-*-linux*aout*] \
46 || [istarget *-*-linux*oldld*] } {
47 return
48}
49
04827a14
L
50set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
51foreach t $test_list {
52 # We need to strip the ".d", but can leave the dirname.
53 verbose [file rootname $t]
54 run_dump_test [file rootname $t]
55}
56
3a8260b2
AS
57# The remaining tests can only be run if ld generates native executables.
58if ![isnative] then {return}
59
6fc49d28
L
60set tmpdir tmpdir
61set SHCFLAG ""
62
63if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
64
65 # AIX shared libraries do not seem to support useful features,
66 # like overriding the shared library function or letting the
67 # shared library refer to objects defined in the main program. We
68 # avoid testing those features.
69 set SHCFLAG "-DXCOFF_TEST"
70
71 # The AIX 3.2.5 loader appears to randomly fail when loading
72 # shared libraries from NSF mounted partitions, so we avoid any
73 # potential problems by using a local directory.
74 catch {exec /bin/sh -c "echo $$"} pid
75 set tmpdir /usr/tmp/ld.$pid
76 catch "exec mkdir $tmpdir" exec_status
77
78 # On AIX, we need to explicitly export the symbols the shared
79 # library is going to provide, and need.
80 set file [open $tmpdir/xcoff.exp w]
81 puts $file shlibvar1
82 puts $file shlibvar2
83 puts $file shlib_shlibvar1
84 puts $file shlib_shlibvar2
85 puts $file shlib_shlibcall
86 puts $file shlib_shlibcalled
87 puts $file shlib_checkfunptr1
88 puts $file shlib_getfunptr1
89 puts $file shlib_check
90 close $file
91}
92
08c44e65
L
93set support_protected "no"
94
95if [istarget *-*-linux*] {
360e9586 96 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
b765d4e3 97 if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
08c44e65
L
98 catch "exec $tmpdir/main" support_protected
99 }
100 }
101}
102
6fc49d28
L
103# The test procedure.
104proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
b765d4e3 105 global CC
6fc49d28
L
106 global srcdir
107 global subdir
108 global exec_output
109 global link_output
110 global host_triplet
111 global tmpdir
112
113 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
114
115 # Build the shared library.
116 # On AIX, we need to use an export file.
117 set shared -shared
118 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
119 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
120 }
b765d4e3 121 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
7cda33a1 122 if { [ string match $visibility "hidden_undef" ]
1ba54ee0
AM
123 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
124 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
125 pass "$testname"
126 } else { if { [ string match $visibility "protected_undef" ]
1ba54ee0
AM
127 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
128 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
129 pass "$testname"
130 } else {
131 fail "$testname"
132 }}
6fc49d28
L
133 return
134 }
135
136 # Link against the shared library. Use -rpath so that the
137 # dynamic linker can locate the shared library at runtime.
138 # On AIX, we must include /lib in -rpath, as otherwise the loader
139 # can not find -lc.
140 set rpath $tmpdir
141 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
142 set rpath /lib:$tmpdir
143 }
b765d4e3 144 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
6fc49d28 145 if { [ string match $visibility "hidden" ]
1ba54ee0
AM
146 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
147 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
7cda33a1
L
148 pass "$testname"
149 } else { if { [ string match $visibility "hidden_undef_def" ]
1ba54ee0
AM
150 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
151 && [regexp "undefined reference to \`visibility_def\'" $link_output]
152 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
153 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
6fc49d28
L
154 pass "$testname"
155 } else {
156 fail "$testname"
7cda33a1 157 }}
6fc49d28
L
158 return
159 }
160
7cda33a1
L
161 if { [ string match $visibility "hidden" ]
162 || [ string match $visibility "hidden_undef" ]
163 || [ string match $visibility "protected_undef" ] } {
6fc49d28
L
164 fail "$testname"
165 }
166
167 # Run the resulting program
168 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
169 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
170 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
171 if ![string match "" $exec_output] then {
172 send_log "$exec_output\n"
173 verbose "$exec_output"
174 fail "$testname"
175 return
176 }
177
178 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
179 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
180 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
181 set exec_output [prune_warnings $exec_output]
182
183 if {![string match "" $exec_output]} then {
184 send_log "$exec_output\n"
185 verbose "$exec_output"
186 fail "$testname"
187 return
188 }
189
190 pass "$testname"
191}
192
193proc visibility_run {visibility} {
194 global CC
195 global CFLAGS
196 global SHCFLAG
197 global srcdir
198 global subdir
199 global tmpdir
200 global picflag
201 global target_triplet
08c44e65 202 global support_protected
6fc49d28
L
203
204 if [ string match $visibility "hidden" ] {
205 set VSBCFLAG "-DHIDDEN_TEST"
206 } else { if [ string match $visibility "hidden_normal" ] {
207 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
7cda33a1
L
208 } else { if [ string match $visibility "hidden_undef" ] {
209 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
210 } else { if [ string match $visibility "hidden_undef_def" ] {
211 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
212 } else { if [ string match $visibility "hidden_weak" ] {
213 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
6fc49d28
L
214 } else { if [ string match $visibility "protected" ] {
215 set VSBCFLAG "-DPROTECTED_TEST"
7cda33a1
L
216 } else { if [ string match $visibility "protected_undef" ] {
217 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
218 } else { if [ string match $visibility "protected_undef_def" ] {
219 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
220 } else { if [ string match $visibility "protected_weak" ] {
221 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
6fc49d28
L
222 } else {
223 set VSBCFLAG ""
7cda33a1 224 }}}}}}}}}
6fc49d28
L
225
226 # Compile the main program.
360e9586 227 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
6fc49d28
L
228 unresolved "visibility ($visibility) (non PIC)"
229 unresolved "visibility ($visibility)"
230 } else {
231 # The shared library is composed of two files. First compile them
232 # without using -fpic. That should work on an ELF system,
233 # although it will be less efficient because the dynamic linker
234 # will need to do more relocation work. However, note that not
235 # using -fpic will cause some of the tests to return different
236 # results.
360e9586
L
237 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
238 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
6fc49d28
L
239 unresolved "visibility ($visibility) (non PIC)"
240 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
ad995491 241 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
6fc49d28
L
242 } else {
243 # SunOS non PIC shared libraries don't permit some cases of
244 # overriding.
1345a0c0
L
245 if { [ string match $visibility "protected" ]
246 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
247 if [ string match $support_protected "no" ] {
248 setup_xfail $target_triplet
249 }
6fc49d28
L
250 } else {
251 setup_xfail "*-*-sunos4*"
252 }
212a6b8e
RH
253
254 # Non-pic code uses name binding rules for applications to
255 # reference variables by gp-relative relocs, which can't be
256 # used with overridable symbols.
ad995491
L
257 if { ![ string match $visibility "hidden_undef" ]
258 && ![ string match $visibility "protected_undef" ] } {
259 setup_xfail "ia64-*-linux*"
212a6b8e 260 setup_xfail "alpha*-*-linux*"
ad995491 261 }
4648dfcf
MS
262 if { ![ string match $visibility "hidden" ]
263 && ![ string match $visibility "hidden_undef" ]
264 && ![ string match $visibility "hidden_undef_def" ]
265 && ![ string match $visibility "protected_undef" ] } {
266 setup_xfail "s390x-*-linux*"
4dc570c2
JJ
267 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
268 setup_xfail "sparc*-*-linux*"
269 }
4648dfcf 270 }
9147e853 271 setup_xfail "x86_64-*-linux*"
c0d48c0b
DA
272 if { ![istarget hppa*64*-*-linux*] } {
273 setup_xfail "hppa*-*-linux*"
274 }
212a6b8e 275
6fc49d28
L
276 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
277
278 # Test ELF shared library relocations with a non-zero load
279 # address for the library. Near as I can tell, the R_*_RELATIVE
280 # relocations for various targets are broken in the case where
281 # the load address is not zero (which is the default).
1345a0c0
L
282 if { [ string match $visibility "protected" ]
283 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
284 if [ string match $support_protected "no" ] {
285 setup_xfail $target_triplet
286 }
6fc49d28
L
287 } else {
288 setup_xfail "*-*-sunos4*"
289 setup_xfail "*-*-linux*libc1"
290 }
d1d8dddf
L
291 if { [ string match $visibility "hidden_normal" ]
292 || [ string match $visibility "hidden_weak" ]
293 || [ string match $visibility "protected" ]
294 || [ string match $visibility "protected_undef_def" ]
295 || [ string match $visibility "protected_weak" ]
296 || [ string match $visibility "normal" ] } {
297 setup_xfail "powerpc-*-linux*"
8c37241b 298 setup_xfail "s390x-*-linux*"
4dc570c2
JJ
299 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
300 setup_xfail "sparc*-*-linux*"
301 }
d1d8dddf 302 }
ad995491
L
303 if { ![ string match $visibility "hidden_undef" ]
304 && ![ string match $visibility "protected_undef" ] } {
305 setup_xfail "ia64-*-linux*"
212a6b8e 306 setup_xfail "alpha*-*-linux*"
66517a2f 307 setup_xfail "mips*-*-linux*"
ad995491 308 }
9147e853 309 setup_xfail "x86_64-*-linux*"
c0d48c0b
DA
310 if { ![istarget hppa*64*-*-linux*] } {
311 setup_xfail "hppa*-*-linux*"
312 }
313
6fc49d28
L
314 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
315 mainnp.o sh1np.o sh2np.o elfvsb \
316 "-T $srcdir/$subdir/elf-offset.ld"
317 } }
318
319 # Now compile the code using -fpic.
320
125c6493
AM
321 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
322 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
6fc49d28
L
323 unresolved "visibility ($visibility)"
324 } else {
1345a0c0
L
325 if { [ string match $visibility "protected" ]
326 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
327 if [ string match $support_protected "no" ] {
328 setup_xfail $target_triplet
329 }
6fc49d28
L
330 }
331 # SunOS can not compare function pointers correctly
332 if [istarget "*-*-sunos4*"] {
333 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
334 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
335 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
336 } else {
337 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
338 } }
339 }
340 }
341
342 # Now do the same tests again, but this time compile main.c PIC.
125c6493 343 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
6fc49d28
L
344 unresolved "visibility ($visibility) (PIC main, non PIC so)"
345 unresolved "visibility ($visibility) (PIC main)"
346 } else {
347 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
348 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
349 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
350 } else {
351 # SunOS non PIC shared libraries don't permit some cases of
352 # overriding.
1345a0c0
L
353 if { [ string match $visibility "protected" ]
354 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
355 if [ string match $support_protected "no" ] {
356 setup_xfail $target_triplet
357 }
6fc49d28
L
358 } else {
359 setup_xfail "*-*-sunos4*"
360 }
ad995491
L
361 if { ![ string match $visibility "hidden_undef" ]
362 && ![ string match $visibility "protected_undef" ] } {
363 setup_xfail "ia64-*-linux*"
212a6b8e 364 setup_xfail "alpha*-*-linux*"
ad995491 365 }
4648dfcf
MS
366 if { ![ string match $visibility "hidden" ]
367 && ![ string match $visibility "hidden_undef" ]
368 && ![ string match $visibility "hidden_undef_def" ]
369 && ![ string match $visibility "protected_undef" ] } {
370 setup_xfail "s390x-*-linux*"
4dc570c2
JJ
371 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
372 setup_xfail "sparc*-*-linux*"
373 }
4648dfcf 374 }
9147e853 375 setup_xfail "x86_64-*-linux*"
c0d48c0b
DA
376 if { ![istarget hppa*64*-*-linux*] } {
377 setup_xfail "hppa*-*-linux*"
378 }
379
6fc49d28
L
380 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
381 }
382 } else {
383 unresolved "visibility (PIC main, non PIC so)"
384 }
385
386 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
1345a0c0
L
387 if { [ string match $visibility "protected" ]
388 || [ string match $visibility "protected_undef_def" ] } {
08c44e65
L
389 if [ string match $support_protected "no" ] {
390 setup_xfail $target_triplet
391 }
6fc49d28
L
392 }
393 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
394 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
395 } else {
396 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
397 }
398 } else {
399 unresolved "visibility ($visibility) (PIC main)"
400 }
401 }
402}
403
404if [istarget mips*-*-*] {
405 set picflag ""
406} else {
407 # Unfortunately, the gcc argument is -fpic and the cc argument is
408 # -KPIC. We have to try both.
409 set picflag "-fpic"
410 send_log "$CC $picflag\n"
411 verbose "$CC $picflag"
412 catch "exec $CC $picflag" exec_output
413 send_log "$exec_output\n"
414 verbose "--" "$exec_output"
415 if { [string match "*illegal option*" $exec_output] \
416 || [string match "*option ignored*" $exec_output] \
417 || [string match "*unrecognized option*" $exec_output] \
418 || [string match "*passed to ld*" $exec_output] } {
419 if [istarget *-*-sunos4*] {
420 set picflag "-pic"
421 } else {
422 set picflag "-KPIC"
423 }
424 }
425}
426verbose "Using $picflag to compile PIC code"
427
428visibility_run hidden
429visibility_run hidden_normal
7cda33a1
L
430visibility_run hidden_undef
431visibility_run hidden_undef_def
432visibility_run hidden_weak
6fc49d28 433visibility_run protected
7cda33a1
L
434visibility_run protected_undef
435visibility_run protected_undef_def
436visibility_run protected_weak
6fc49d28
L
437visibility_run normal
438
22d5e339
L
439if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
440 unresolved "common hidden symbol"
441} else {
442 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
443 fail "common hidden symbol"
444 } else {
445 pass "common hidden symbol"
446 }
447}
448
449if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
450 unresolved "weak hidden symbol"
451} else {
125c6493 452 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
22d5e339
L
453 unresolved "weak hidden symbol"
454 } else {
455 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
456 fail "weak hidden symbol"
457 } else {
458 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
459 fail "weak hidden symbol DSO last"
460 } else {
461 pass "weak hidden symbol DSO last"
462 }
463 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
464 fail "weak hidden symbol DSO first"
465 } else {
466 pass "weak hidden symbol DSO first"
467 }
468 }
469 }
470}
471
6fc49d28
L
472if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
473 # Remove the temporary directory.
474 catch "exec rm -rf $tmpdir" exec_status
475}
This page took 0.451325 seconds and 4 git commands to generate.