Pass $PLT_CFLAGS to ld_compile
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 # Copyright (C) 2000-2015 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 # 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 i?86-*-gnu*] \
33 && ![istarget *-*-nacl*] \
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 *-*-nacl*]
121 || [istarget *-*-gnu*] } {
122 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
123 if [ld_simple_link $CC $tmpdir/main "$tmpdir/main.o"] {
124 catch "exec $tmpdir/main" support_protected
125 }
126 }
127 }
128
129 # The test procedure.
130 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
131 global CC
132 global srcdir
133 global subdir
134 global exec_output
135 global link_output
136 global host_triplet
137 global tmpdir
138
139 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
140
141 # Build the shared library.
142 # On AIX, we need to use an export file.
143 set shared -shared
144 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
145 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
146 }
147 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
148 if { [ string match $visibility "hidden_undef" ]
149 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
150 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
151 pass "$testname"
152 } else { if { [ string match $visibility "protected_undef" ]
153 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
154 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
155 pass "$testname"
156 } else {
157 fail "$testname"
158 }}
159 return
160 }
161
162 # Link against the shared library. Use -rpath so that the
163 # dynamic linker can locate the shared library at runtime.
164 # On AIX, we must include /lib in -rpath, as otherwise the loader
165 # can not find -lc.
166 set rpath $tmpdir
167 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
168 set rpath /lib:$tmpdir
169 }
170 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
171 if { [ string match $visibility "hidden" ]
172 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
173 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
174 pass "$testname"
175 } else { if { [ string match $visibility "hidden_undef_def" ]
176 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
177 && [regexp "undefined reference to \`visibility_def\'" $link_output]
178 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
179 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
180 pass "$testname"
181 } else {
182 fail "$testname"
183 }}
184 return
185 }
186
187 if { [ string match $visibility "hidden" ]
188 || [ string match $visibility "hidden_undef" ]
189 || [ string match $visibility "protected_undef" ] } {
190 fail "$testname"
191 }
192
193 # Run the resulting program
194 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
195 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
196 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
197 if ![string match "" $exec_output] then {
198 send_log "$exec_output\n"
199 verbose "$exec_output"
200 fail "$testname"
201 return
202 }
203
204 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
205 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
206 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
207 set exec_output [prune_warnings $exec_output]
208
209 if {![string match "" $exec_output]} then {
210 send_log "$exec_output\n"
211 verbose "$exec_output"
212 fail "$testname"
213 return
214 }
215
216 pass "$testname"
217 }
218
219 proc visibility_run {visibility} {
220 global CC
221 global CFLAGS
222 global SHCFLAG
223 global srcdir
224 global subdir
225 global tmpdir
226 global picflag
227 global target_triplet
228 global support_protected
229 global shared_needs_pic
230 global PLT_CFLAGS
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. Make sure that PLT is used since PLT
264 # is expected.
265 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
266 unresolved "visibility ($visibility) (non PIC)"
267 unresolved "visibility ($visibility)"
268 } else {
269 # The shared library is composed of two files. First compile them
270 # without using -fpic. That should work on an ELF system,
271 # although it will be less efficient because the dynamic linker
272 # will need to do more relocation work. However, note that not
273 # using -fpic will cause some of the tests to return different
274 # results. Make sure that PLT is used since PLT is expected.
275 if { ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
276 || ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
277 unresolved "visibility ($visibility) (non PIC)"
278 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
279 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
280 } else {
281 # SunOS non PIC shared libraries don't permit some cases of
282 # overriding.
283 if { [ string match $visibility "protected" ]
284 || [ string match $visibility "protected_undef_def" ] } {
285 if [ string match $support_protected "no" ] {
286 setup_xfail $target_triplet
287 }
288 } else {
289 setup_xfail "*-*-sunos4*"
290 }
291
292 # Non-pic code uses name binding rules for applications to
293 # reference variables by gp-relative relocs, which can't be
294 # used with overridable symbols.
295 if { ![ string match $visibility "hidden_undef" ]
296 && ![ string match $visibility "protected_undef" ] } {
297 setup_xfail "ia64-*-linux*"
298 setup_xfail "alpha*-*-linux*"
299 }
300 if { ![ string match $visibility "hidden" ]
301 && ![ string match $visibility "hidden_undef" ]
302 && ![ string match $visibility "hidden_undef_def" ]
303 && ![ string match $visibility "protected_undef" ] } {
304 setup_xfail "s390x-*-linux*"
305 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
306 setup_xfail "sparc*-*-linux*"
307 }
308 }
309 if { [is_elf64 $tmpdir/mainnp.o] } {
310 setup_xfail "x86_64-*-linux*"
311 }
312 setup_xfail "x86_64-*-linux-gnux32"
313 if { ![istarget hppa*64*-*-linux*] } {
314 setup_xfail "hppa*-*-linux*"
315 }
316 if [ string match $shared_needs_pic "yes" ] {
317 setup_xfail "arm*-*-linux*"
318 }
319
320 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
321
322 # Test ELF shared library relocations with a non-zero load
323 # address for the library. Near as I can tell, the R_*_RELATIVE
324 # relocations for various targets are broken in the case where
325 # the load address is not zero (which is the default).
326 if { [ string match $visibility "protected" ]
327 || [ string match $visibility "protected_undef_def" ] } {
328 if [ string match $support_protected "no" ] {
329 setup_xfail $target_triplet
330 }
331 } else {
332 setup_xfail "*-*-sunos4*"
333 setup_xfail "*-*-linux*libc1"
334 }
335 if { [ string match $visibility "hidden_normal" ]
336 || [ string match $visibility "hidden_weak" ]
337 || [ string match $visibility "protected" ]
338 || [ string match $visibility "protected_undef_def" ]
339 || [ string match $visibility "protected_weak" ]
340 || [ string match $visibility "normal" ] } {
341 setup_xfail "powerpc-*-linux*"
342 setup_xfail "s390x-*-linux*"
343 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
344 setup_xfail "sparc*-*-linux*"
345 }
346 }
347 if { ![ string match $visibility "hidden_undef" ]
348 && ![ string match $visibility "protected_undef" ] } {
349 setup_xfail "ia64-*-linux*"
350 setup_xfail "alpha*-*-linux*"
351 setup_xfail "mips*-*-linux*"
352 }
353 if { [is_elf64 $tmpdir/mainnp.o] } {
354 setup_xfail "x86_64-*-linux*"
355 }
356 setup_xfail "x86_64-*-linux-gnux32"
357 if { ![istarget hppa*64*-*-linux*] } {
358 setup_xfail "hppa*-*-linux*"
359 }
360 if [ string match $shared_needs_pic "yes" ] {
361 setup_xfail "arm*-*-linux*"
362 }
363
364 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
365 mainnp.o sh1np.o sh2np.o elfvsb \
366 "-T $srcdir/$subdir/elf-offset.ld"
367 } }
368
369 # Now compile the code using -fpic.
370
371 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
372 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
373 unresolved "visibility ($visibility)"
374 } else {
375 if { [ string match $visibility "protected" ]
376 || [ string match $visibility "protected_undef_def" ] } {
377 if [ string match $support_protected "no" ] {
378 setup_xfail $target_triplet
379 }
380 }
381 # SunOS can not compare function pointers correctly
382 if [istarget "*-*-sunos4*"] {
383 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
384 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
385 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
386 } else {
387 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
388 } }
389 }
390 }}
391
392 if { [istarget powerpc*-*-linux*] } {
393 # Don't bother.
394 } else {
395 # Now do the same tests again, but this time compile main.c PIC.
396 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
397 unresolved "visibility ($visibility) (PIC main, non PIC so)"
398 unresolved "visibility ($visibility) (PIC main)"
399 } else {
400 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
401 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
402 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
403 } else {
404 # SunOS non PIC shared libraries don't permit some cases of
405 # overriding.
406 if { [ string match $visibility "protected" ]
407 || [ string match $visibility "protected_undef_def" ] } {
408 if [ string match $support_protected "no" ] {
409 setup_xfail $target_triplet
410 }
411 } else {
412 setup_xfail "*-*-sunos4*"
413 }
414 if { ![ string match $visibility "hidden_undef" ]
415 && ![ string match $visibility "protected_undef" ] } {
416 setup_xfail "ia64-*-linux*"
417 setup_xfail "alpha*-*-linux*"
418 }
419 if { ![ string match $visibility "hidden" ]
420 && ![ string match $visibility "hidden_undef" ]
421 && ![ string match $visibility "hidden_undef_def" ]
422 && ![ string match $visibility "protected_undef" ] } {
423 setup_xfail "s390x-*-linux*"
424 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
425 setup_xfail "sparc*-*-linux*"
426 }
427 }
428 if { [is_elf64 $tmpdir/mainp.o] } {
429 setup_xfail "x86_64-*-linux*"
430 }
431 setup_xfail "x86_64-*-linux-gnux32"
432 if { ![istarget hppa*64*-*-linux*] } {
433 setup_xfail "hppa*-*-linux*"
434 }
435 if [ string match $shared_needs_pic "yes" ] {
436 setup_xfail "arm*-*-linux*"
437 }
438
439 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
440 }
441 } else {
442 unresolved "visibility (PIC main, non PIC so)"
443 }
444
445 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
446 if { [ string match $visibility "protected" ]
447 || [ string match $visibility "protected_undef_def" ] } {
448 if [ string match $support_protected "no" ] {
449 setup_xfail $target_triplet
450 }
451 }
452 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
453 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
454 } else {
455 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
456 }
457 } else {
458 unresolved "visibility ($visibility) (PIC main)"
459 }
460 }}
461 }
462
463 # Old version of GCC for MIPS default to enabling -fpic
464 # and get confused if it is used on the command line.
465 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
466 set picflag ""
467 } else {
468 # Unfortunately, the gcc argument is -fpic and the cc argument is
469 # -KPIC. We have to try both.
470 set picflag "-fpic"
471 send_log "$CC $picflag\n"
472 verbose "$CC $picflag"
473 catch "exec $CC $picflag" exec_output
474 send_log "$exec_output\n"
475 verbose "--" "$exec_output"
476 if { [string match "*illegal option*" $exec_output] \
477 || [string match "*option ignored*" $exec_output] \
478 || [string match "*unrecognized option*" $exec_output] \
479 || [string match "*passed to ld*" $exec_output] } {
480 if [istarget *-*-sunos4*] {
481 set picflag "-pic"
482 } else {
483 set picflag "-KPIC"
484 }
485 }
486 }
487 verbose "Using $picflag to compile PIC code"
488
489 visibility_run hidden
490 visibility_run hidden_normal
491 visibility_run hidden_undef
492 visibility_run hidden_undef_def
493 visibility_run hidden_weak
494 visibility_run protected
495 visibility_run protected_undef
496 visibility_run protected_undef_def
497 visibility_run protected_weak
498 visibility_run normal
499
500 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
501 unresolved "common hidden symbol"
502 } else {
503 if ![ld_simple_link $ld tmpdir/common "tmpdir/common.o"] {
504 fail "common hidden symbol"
505 } else {
506 pass "common hidden symbol"
507 }
508 }
509
510 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
511 unresolved "weak hidden symbol"
512 } else {
513 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
514 unresolved "weak hidden symbol"
515 } else {
516 if ![ld_simple_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
517 fail "weak hidden symbol"
518 } else {
519 if ![ld_simple_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
520 fail "weak hidden symbol DSO last"
521 } else {
522 pass "weak hidden symbol DSO last"
523 }
524 if ![ld_simple_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
525 fail "weak hidden symbol DSO first"
526 } else {
527 pass "weak hidden symbol DSO first"
528 }
529 }
530 }
531 }
532
533 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
534 # Remove the temporary directory.
535 catch "exec rm -rf $tmpdir" exec_status
536 }
This page took 0.052145 seconds and 5 git commands to generate.