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