daily update
[binutils.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
blob7931c667076a3dd2e9a4bbb3a32b62140814e1c5
1 # Expect script for ld-visibility tests
2 # Copyright 2000, 2001, 2002, 2003, 2004, 2007 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,
19 # MA 02110-1301, USA.
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 # This test can only be run on a couple of ELF platforms.
28 # Square bracket expressions seem to confuse istarget.
29 if { ![istarget hppa*64*-*-hpux*] \
30 && ![istarget hppa*-*-linux*] \
31 && ![istarget i?86-*-linux*] \
32 && ![istarget ia64-*-linux*] \
33 && ![istarget m68k-*-linux*] \
34 && ![istarget mips*-*-linux*] \
35 && ![istarget powerpc*-*-linux*] \
36 && ![istarget arm*-*-linux*] \
37 && ![istarget alpha*-*-linux*] \
38 && ![istarget sparc*-*-linux*] \
39 && ![istarget s390*-*-linux*] \
40 && ![istarget sh\[34\]*-*-linux*] \
41 && ![istarget x86_64-*-linux*] } {
42 return
45 if { [istarget *-*-linux*aout*] \
46 || [istarget *-*-linux*oldld*] } {
47 return
50 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
51 foreach 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]
57 # The remaining tests can only be run if ld generates native executables.
58 if ![isnative] then {return}
60 set tmpdir tmpdir
61 set SHCFLAG ""
63 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
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"
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
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
93 set support_protected "no"
95 if [istarget *-*-linux*] {
96 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
97 if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
98 catch "exec $tmpdir/main" support_protected
103 # The test procedure.
104 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
105 global CC
106 global srcdir
107 global subdir
108 global exec_output
109 global link_output
110 global host_triplet
111 global tmpdir
113 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
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"
121 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
122 if { [ string match $visibility "hidden_undef" ]
123 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
124 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
125 pass "$testname"
126 } else { if { [ string match $visibility "protected_undef" ]
127 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
128 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
129 pass "$testname"
130 } else {
131 fail "$testname"
133 return
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
144 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
145 if { [ string match $visibility "hidden" ]
146 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
147 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
148 pass "$testname"
149 } else { if { [ string match $visibility "hidden_undef_def" ]
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] } {
154 pass "$testname"
155 } else {
156 fail "$testname"
158 return
161 if { [ string match $visibility "hidden" ]
162 || [ string match $visibility "hidden_undef" ]
163 || [ string match $visibility "protected_undef" ] } {
164 fail "$testname"
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
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]
183 if {![string match "" $exec_output]} then {
184 send_log "$exec_output\n"
185 verbose "$exec_output"
186 fail "$testname"
187 return
190 pass "$testname"
193 proc 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
202 global support_protected
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"
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"
214 } else { if [ string match $visibility "protected" ] {
215 set VSBCFLAG "-DPROTECTED_TEST"
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"
222 } else {
223 set VSBCFLAG ""
224 }}}}}}}}}
226 # Compile the main program.
227 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
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.
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] } {
239 unresolved "visibility ($visibility) (non PIC)"
240 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
241 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
242 } else {
243 # SunOS non PIC shared libraries don't permit some cases of
244 # overriding.
245 if { [ string match $visibility "protected" ]
246 || [ string match $visibility "protected_undef_def" ] } {
247 if [ string match $support_protected "no" ] {
248 setup_xfail $target_triplet
250 } else {
251 setup_xfail "*-*-sunos4*"
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.
257 if { ![ string match $visibility "hidden_undef" ]
258 && ![ string match $visibility "protected_undef" ] } {
259 setup_xfail "ia64-*-linux*"
260 setup_xfail "alpha*-*-linux*"
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*"
267 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
268 setup_xfail "sparc*-*-linux*"
271 setup_xfail "x86_64-*-linux*"
272 if { ![istarget hppa*64*-*-linux*] } {
273 setup_xfail "hppa*-*-linux*"
276 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
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).
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
287 } else {
288 setup_xfail "*-*-sunos4*"
289 setup_xfail "*-*-linux*libc1"
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*"
298 setup_xfail "s390x-*-linux*"
299 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
300 setup_xfail "sparc*-*-linux*"
303 if { ![ string match $visibility "hidden_undef" ]
304 && ![ string match $visibility "protected_undef" ] } {
305 setup_xfail "ia64-*-linux*"
306 setup_xfail "alpha*-*-linux*"
307 setup_xfail "mips*-*-linux*"
309 setup_xfail "x86_64-*-linux*"
310 if { ![istarget hppa*64*-*-linux*] } {
311 setup_xfail "hppa*-*-linux*"
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"
319 # Now compile the code using -fpic.
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] } {
323 unresolved "visibility ($visibility)"
324 } else {
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 # 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
342 # Now do the same tests again, but this time compile main.c PIC.
343 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
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.
353 if { [ string match $visibility "protected" ]
354 || [ string match $visibility "protected_undef_def" ] } {
355 if [ string match $support_protected "no" ] {
356 setup_xfail $target_triplet
358 } else {
359 setup_xfail "*-*-sunos4*"
361 if { ![ string match $visibility "hidden_undef" ]
362 && ![ string match $visibility "protected_undef" ] } {
363 setup_xfail "ia64-*-linux*"
364 setup_xfail "alpha*-*-linux*"
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*"
371 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
372 setup_xfail "sparc*-*-linux*"
375 setup_xfail "x86_64-*-linux*"
376 if { ![istarget hppa*64*-*-linux*] } {
377 setup_xfail "hppa*-*-linux*"
380 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
382 } else {
383 unresolved "visibility (PIC main, non PIC so)"
386 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
387 if { [ string match $visibility "protected" ]
388 || [ string match $visibility "protected_undef_def" ] } {
389 if [ string match $support_protected "no" ] {
390 setup_xfail $target_triplet
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
398 } else {
399 unresolved "visibility ($visibility) (PIC main)"
404 if [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"
426 verbose "Using $picflag to compile PIC code"
428 visibility_run hidden
429 visibility_run hidden_normal
430 visibility_run hidden_undef
431 visibility_run hidden_undef_def
432 visibility_run hidden_weak
433 visibility_run protected
434 visibility_run protected_undef
435 visibility_run protected_undef_def
436 visibility_run protected_weak
437 visibility_run normal
439 if { ![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"
449 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
450 unresolved "weak hidden symbol"
451 } else {
452 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
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"
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"
472 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
473 # Remove the temporary directory.
474 catch "exec rm -rf $tmpdir" exec_status