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