58bc8f2dca57c5b115d948e1a59b76d649712f62
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 # Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2010
3 # Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
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.
11 #
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.
16 #
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,
20 # MA 02110-1301, USA.
21 #
22 # Written by Ian Lance Taylor (ian@cygnus.com)
23 # and H.J. Lu (hjl@gnu.org)
24 #
25
26 # Make sure that ld can generate ELF shared libraries with visibility.
27
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 ia64-*-linux*] \
35 && ![istarget m68k-*-linux*] \
36 && ![istarget mips*-*-linux*] \
37 && ![istarget powerpc*-*-linux*] \
38 && ![istarget arm*-*-linux*] \
39 && ![istarget alpha*-*-linux*] \
40 && ![istarget sparc*-*-linux*] \
41 && ![istarget s390*-*-linux*] \
42 && ![istarget sh\[34\]*-*-linux*] \
43 && ![istarget x86_64-*-linux*] } {
44 return
45 }
46
47 if { [istarget *-*-linux*aout*] \
48 || [istarget *-*-linux*oldld*] } {
49 return
50 }
51
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]
57 }
58
59 # The remaining tests can only be run if ld generates native executables.
60 if ![isnative] then {return}
61
62 set tmpdir tmpdir
63 set SHCFLAG ""
64 set shared_needs_pic "no"
65
66 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
67
68 # AIX shared libraries do not seem to support useful features,
69 # like overriding the shared library function or letting the
70 # shared library refer to objects defined in the main program. We
71 # avoid testing those features.
72 set SHCFLAG "-DXCOFF_TEST"
73
74 # The AIX 3.2.5 loader appears to randomly fail when loading
75 # shared libraries from NSF mounted partitions, so we avoid any
76 # potential problems by using a local directory.
77 catch {exec /bin/sh -c "echo $$"} pid
78 set tmpdir /usr/tmp/ld.$pid
79 catch "exec mkdir $tmpdir" exec_status
80
81 # On AIX, we need to explicitly export the symbols the shared
82 # library is going to provide, and need.
83 set file [open $tmpdir/xcoff.exp w]
84 puts $file shlibvar1
85 puts $file shlibvar2
86 puts $file shlib_shlibvar1
87 puts $file shlib_shlibvar2
88 puts $file shlib_shlibcall
89 puts $file shlib_shlibcalled
90 puts $file shlib_checkfunptr1
91 puts $file shlib_getfunptr1
92 puts $file shlib_check
93 close $file
94 }
95
96 if [istarget arm*-*-linux*] {
97 # On ARM section anchors can change the symbol pre-emptability for
98 # non-PIC shared libraries, causing these tests to fail. Turn section
99 # anchors off.
100 set SHCFLAG "-fno-section-anchors"
101
102 # On targets that have MOVW the compiler will emit relocations which
103 # the linker doesn't support when compiling -shared without -fpic. The
104 # test to find out whether we want to XFAIL the non-PIC tests requires
105 # a compile - so we pre-calculate it here. We also note that this can
106 # only affect arm*-*-*eabi targets as the old ABI doesn't support v7.
107 if [istarget arm*-*-*eabi] {
108 set file [open $tmpdir/movw-detect.c w]
109 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
110 close $file
111 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
112 set shared_needs_pic "yes"
113 }
114 }
115 }
116
117 set support_protected "no"
118
119 if { [istarget *-*-linux*]
120 || [istarget *-*-gnu*] } {
121 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
122 if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
123 catch "exec $tmpdir/main" support_protected
124 }
125 }
126 }
127
128 # The test procedure.
129 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
130 global CC
131 global srcdir
132 global subdir
133 global exec_output
134 global link_output
135 global host_triplet
136 global tmpdir
137
138 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
139
140 # Build the shared library.
141 # On AIX, we need to use an export file.
142 set shared -shared
143 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
144 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
145 }
146 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
147 if { [ string match $visibility "hidden_undef" ]
148 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
149 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
150 pass "$testname"
151 } else { if { [ string match $visibility "protected_undef" ]
152 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
153 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
154 pass "$testname"
155 } else {
156 fail "$testname"
157 }}
158 return
159 }
160
161 # Link against the shared library. Use -rpath so that the
162 # dynamic linker can locate the shared library at runtime.
163 # On AIX, we must include /lib in -rpath, as otherwise the loader
164 # can not find -lc.
165 set rpath $tmpdir
166 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
167 set rpath /lib:$tmpdir
168 }
169 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
170 if { [ string match $visibility "hidden" ]
171 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
172 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
173 pass "$testname"
174 } else { if { [ string match $visibility "hidden_undef_def" ]
175 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
176 && [regexp "undefined reference to \`visibility_def\'" $link_output]
177 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
178 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
179 pass "$testname"
180 } else {
181 fail "$testname"
182 }}
183 return
184 }
185
186 if { [ string match $visibility "hidden" ]
187 || [ string match $visibility "hidden_undef" ]
188 || [ string match $visibility "protected_undef" ] } {
189 fail "$testname"
190 }
191
192 # Run the resulting program
193 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
194 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
195 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
196 if ![string match "" $exec_output] then {
197 send_log "$exec_output\n"
198 verbose "$exec_output"
199 fail "$testname"
200 return
201 }
202
203 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
204 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
205 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
206 set exec_output [prune_warnings $exec_output]
207
208 if {![string match "" $exec_output]} then {
209 send_log "$exec_output\n"
210 verbose "$exec_output"
211 fail "$testname"
212 return
213 }
214
215 pass "$testname"
216 }
217
218 proc visibility_run {visibility} {
219 global CC
220 global CFLAGS
221 global SHCFLAG
222 global srcdir
223 global subdir
224 global tmpdir
225 global picflag
226 global target_triplet
227 global support_protected
228 global shared_needs_pic
229
230 if [ string match $visibility "hidden" ] {
231 set VSBCFLAG "-DHIDDEN_TEST"
232 } else { if [ string match $visibility "hidden_normal" ] {
233 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
234 } else { if [ string match $visibility "hidden_undef" ] {
235 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
236 } else { if [ string match $visibility "hidden_undef_def" ] {
237 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
238 } else { if [ string match $visibility "hidden_weak" ] {
239 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
240 } else { if [ string match $visibility "protected" ] {
241 set VSBCFLAG "-DPROTECTED_TEST"
242 } else { if [ string match $visibility "protected_undef" ] {
243 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
244 } else { if [ string match $visibility "protected_undef_def" ] {
245 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
246 } else { if [ string match $visibility "protected_weak" ] {
247 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
248 } else {
249 set VSBCFLAG ""
250 }}}}}}}}}
251
252 if { [istarget powerpc*-*-linux*] \
253 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
254 # Testing non-PIC libraries is a waste of effort on any target.
255 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
256 # reasonably that you are not compiling for a shared library.
257 # It can then make optimisations that result in shared library
258 # functions and variables not being overridable. Newer versions
259 # of gcc are more likely to do this.
260 } else {
261 # Compile the main program.
262 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
263 unresolved "visibility ($visibility) (non PIC)"
264 unresolved "visibility ($visibility)"
265 } else {
266 # The shared library is composed of two files. First compile them
267 # without using -fpic. That should work on an ELF system,
268 # although it will be less efficient because the dynamic linker
269 # will need to do more relocation work. However, note that not
270 # using -fpic will cause some of the tests to return different
271 # results.
272 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
273 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
274 unresolved "visibility ($visibility) (non PIC)"
275 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
276 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
277 } else {
278 # SunOS non PIC shared libraries don't permit some cases of
279 # overriding.
280 if { [ string match $visibility "protected" ]
281 || [ string match $visibility "protected_undef_def" ] } {
282 if [ string match $support_protected "no" ] {
283 setup_xfail $target_triplet
284 }
285 } else {
286 setup_xfail "*-*-sunos4*"
287 }
288
289 # Non-pic code uses name binding rules for applications to
290 # reference variables by gp-relative relocs, which can't be
291 # used with overridable symbols.
292 if { ![ string match $visibility "hidden_undef" ]
293 && ![ string match $visibility "protected_undef" ] } {
294 setup_xfail "ia64-*-linux*"
295 setup_xfail "alpha*-*-linux*"
296 }
297 if { ![ string match $visibility "hidden" ]
298 && ![ string match $visibility "hidden_undef" ]
299 && ![ string match $visibility "hidden_undef_def" ]
300 && ![ string match $visibility "protected_undef" ] } {
301 setup_xfail "s390x-*-linux*"
302 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
303 setup_xfail "sparc*-*-linux*"
304 }
305 }
306 if { [is_elf64 $tmpdir/mainnp.o] } {
307 setup_xfail "x86_64-*-linux*"
308 }
309 if { ![istarget hppa*64*-*-linux*] } {
310 setup_xfail "hppa*-*-linux*"
311 }
312 if [ string match $shared_needs_pic "yes" ] {
313 setup_xfail "arm*-*-linux*"
314 }
315
316 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
317
318 # Test ELF shared library relocations with a non-zero load
319 # address for the library. Near as I can tell, the R_*_RELATIVE
320 # relocations for various targets are broken in the case where
321 # the load address is not zero (which is the default).
322 if { [ string match $visibility "protected" ]
323 || [ string match $visibility "protected_undef_def" ] } {
324 if [ string match $support_protected "no" ] {
325 setup_xfail $target_triplet
326 }
327 } else {
328 setup_xfail "*-*-sunos4*"
329 setup_xfail "*-*-linux*libc1"
330 }
331 if { [ string match $visibility "hidden_normal" ]
332 || [ string match $visibility "hidden_weak" ]
333 || [ string match $visibility "protected" ]
334 || [ string match $visibility "protected_undef_def" ]
335 || [ string match $visibility "protected_weak" ]
336 || [ string match $visibility "normal" ] } {
337 setup_xfail "powerpc-*-linux*"
338 setup_xfail "s390x-*-linux*"
339 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
340 setup_xfail "sparc*-*-linux*"
341 }
342 }
343 if { ![ string match $visibility "hidden_undef" ]
344 && ![ string match $visibility "protected_undef" ] } {
345 setup_xfail "ia64-*-linux*"
346 setup_xfail "alpha*-*-linux*"
347 setup_xfail "mips*-*-linux*"
348 }
349 if { [is_elf64 $tmpdir/mainnp.o] } {
350 setup_xfail "x86_64-*-linux*"
351 }
352 if { ![istarget hppa*64*-*-linux*] } {
353 setup_xfail "hppa*-*-linux*"
354 }
355 if [ string match $shared_needs_pic "yes" ] {
356 setup_xfail "arm*-*-linux*"
357 }
358
359 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
360 mainnp.o sh1np.o sh2np.o elfvsb \
361 "-T $srcdir/$subdir/elf-offset.ld"
362 } }
363
364 # Now compile the code using -fpic.
365
366 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
367 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
368 unresolved "visibility ($visibility)"
369 } else {
370 if { [ string match $visibility "protected" ]
371 || [ string match $visibility "protected_undef_def" ] } {
372 if [ string match $support_protected "no" ] {
373 setup_xfail $target_triplet
374 }
375 }
376 # SunOS can not compare function pointers correctly
377 if [istarget "*-*-sunos4*"] {
378 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
379 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
380 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
381 } else {
382 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
383 } }
384 }
385 }}
386
387 if { [istarget powerpc*-*-linux*] } {
388 # Don't bother.
389 } else {
390 # Now do the same tests again, but this time compile main.c PIC.
391 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
392 unresolved "visibility ($visibility) (PIC main, non PIC so)"
393 unresolved "visibility ($visibility) (PIC main)"
394 } else {
395 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
396 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
397 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
398 } else {
399 # SunOS non PIC shared libraries don't permit some cases of
400 # overriding.
401 if { [ string match $visibility "protected" ]
402 || [ string match $visibility "protected_undef_def" ] } {
403 if [ string match $support_protected "no" ] {
404 setup_xfail $target_triplet
405 }
406 } else {
407 setup_xfail "*-*-sunos4*"
408 }
409 if { ![ string match $visibility "hidden_undef" ]
410 && ![ string match $visibility "protected_undef" ] } {
411 setup_xfail "ia64-*-linux*"
412 setup_xfail "alpha*-*-linux*"
413 }
414 if { ![ string match $visibility "hidden" ]
415 && ![ string match $visibility "hidden_undef" ]
416 && ![ string match $visibility "hidden_undef_def" ]
417 && ![ string match $visibility "protected_undef" ] } {
418 setup_xfail "s390x-*-linux*"
419 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
420 setup_xfail "sparc*-*-linux*"
421 }
422 }
423 if { [is_elf64 $tmpdir/mainp.o] } {
424 setup_xfail "x86_64-*-linux*"
425 }
426 if { ![istarget hppa*64*-*-linux*] } {
427 setup_xfail "hppa*-*-linux*"
428 }
429 if [ string match $shared_needs_pic "yes" ] {
430 setup_xfail "arm*-*-linux*"
431 }
432
433 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
434 }
435 } else {
436 unresolved "visibility (PIC main, non PIC so)"
437 }
438
439 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
440 if { [ string match $visibility "protected" ]
441 || [ string match $visibility "protected_undef_def" ] } {
442 if [ string match $support_protected "no" ] {
443 setup_xfail $target_triplet
444 }
445 }
446 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
447 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
448 } else {
449 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
450 }
451 } else {
452 unresolved "visibility ($visibility) (PIC main)"
453 }
454 }}
455 }
456
457 # Old version of GCC for MIPS default to enabling -fpic
458 # and get confused if it is used on the command line.
459 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
460 set picflag ""
461 } else {
462 # Unfortunately, the gcc argument is -fpic and the cc argument is
463 # -KPIC. We have to try both.
464 set picflag "-fpic"
465 send_log "$CC $picflag\n"
466 verbose "$CC $picflag"
467 catch "exec $CC $picflag" exec_output
468 send_log "$exec_output\n"
469 verbose "--" "$exec_output"
470 if { [string match "*illegal option*" $exec_output] \
471 || [string match "*option ignored*" $exec_output] \
472 || [string match "*unrecognized option*" $exec_output] \
473 || [string match "*passed to ld*" $exec_output] } {
474 if [istarget *-*-sunos4*] {
475 set picflag "-pic"
476 } else {
477 set picflag "-KPIC"
478 }
479 }
480 }
481 verbose "Using $picflag to compile PIC code"
482
483 visibility_run hidden
484 visibility_run hidden_normal
485 visibility_run hidden_undef
486 visibility_run hidden_undef_def
487 visibility_run hidden_weak
488 visibility_run protected
489 visibility_run protected_undef
490 visibility_run protected_undef_def
491 visibility_run protected_weak
492 visibility_run normal
493
494 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
495 unresolved "common hidden symbol"
496 } else {
497 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
498 fail "common hidden symbol"
499 } else {
500 pass "common hidden symbol"
501 }
502 }
503
504 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
505 unresolved "weak hidden symbol"
506 } else {
507 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
508 unresolved "weak hidden symbol"
509 } else {
510 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
511 fail "weak hidden symbol"
512 } else {
513 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
514 fail "weak hidden symbol DSO last"
515 } else {
516 pass "weak hidden symbol DSO last"
517 }
518 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
519 fail "weak hidden symbol DSO first"
520 } else {
521 pass "weak hidden symbol DSO first"
522 }
523 }
524 }
525 }
526
527 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
528 # Remove the temporary directory.
529 catch "exec rm -rf $tmpdir" exec_status
530 }
This page took 0.040483 seconds and 3 git commands to generate.