]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/testsuite/ld-elfvsb/elfvsb.exp
Run more ld tests when not native
[thirdparty/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 if { [istarget powerpc*-*-linux*] \
264 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
265 # Testing non-PIC libraries is a waste of effort on any target.
266 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
267 # reasonably that you are not compiling for a shared library.
268 # It can then make optimisations that result in shared library
269 # functions and variables not being overridable. Newer versions
270 # of gcc are more likely to do this.
271 } else {
272 # Compile the main program. Make sure that PLT is used since PLT
273 # is expected.
274 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
275 unresolved "visibility ($visibility) (non PIC)"
276 unresolved "visibility ($visibility)"
277 } else {
278 # The shared library is composed of two files. First compile them
279 # without using -fpic. That should work on an ELF system,
280 # although it will be less efficient because the dynamic linker
281 # will need to do more relocation work. However, note that not
282 # using -fpic will cause some of the tests to return different
283 # results. Make sure that PLT is used since PLT is expected.
284 if { ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
285 || ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
286 unresolved "visibility ($visibility) (non PIC)"
287 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
288 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
289 } else {
290 # SunOS non PIC shared libraries don't permit some cases of
291 # overriding.
292 if { [ string match $visibility "protected" ]
293 || [ string match $visibility "protected_undef_def" ] } {
294 if [ string match $support_protected "no" ] {
295 setup_xfail $target_triplet
296 }
297 } else {
298 setup_xfail "*-*-sunos4*"
299 }
300
301 # Non-pic code uses name binding rules for applications to
302 # reference variables by gp-relative relocs, which can't be
303 # used with overridable symbols.
304 if { ![ string match $visibility "hidden_undef" ]
305 && ![ string match $visibility "protected_undef" ] } {
306 setup_xfail "ia64-*-linux*"
307 setup_xfail "alpha*-*-linux*"
308 }
309 if { ![ string match $visibility "hidden" ]
310 && ![ string match $visibility "hidden_undef" ]
311 && ![ string match $visibility "hidden_undef_def" ]
312 && ![ string match $visibility "protected_undef" ] } {
313 setup_xfail "s390x-*-linux*"
314 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
315 setup_xfail "sparc*-*-linux*"
316 }
317 }
318 if { [is_elf64 $tmpdir/mainnp.o] } {
319 setup_xfail "x86_64-*-linux*"
320 }
321 setup_xfail "x86_64-*-linux-gnux32"
322 if { ![istarget hppa*64*-*-linux*] } {
323 setup_xfail "hppa*-*-linux*"
324 }
325 if [ string match $shared_needs_pic "yes" ] {
326 setup_xfail "arm*-*-linux*"
327 }
328
329 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
330
331 # Test ELF shared library relocations with a non-zero load
332 # address for the library. Near as I can tell, the R_*_RELATIVE
333 # relocations for various targets are broken in the case where
334 # the load address is not zero (which is the default).
335 if { [ string match $visibility "protected" ]
336 || [ string match $visibility "protected_undef_def" ] } {
337 if [ string match $support_protected "no" ] {
338 setup_xfail $target_triplet
339 }
340 } else {
341 setup_xfail "*-*-sunos4*"
342 setup_xfail "*-*-linux*libc1"
343 }
344 if { [ string match $visibility "hidden_normal" ]
345 || [ string match $visibility "hidden_weak" ]
346 || [ string match $visibility "protected" ]
347 || [ string match $visibility "protected_undef_def" ]
348 || [ string match $visibility "protected_weak" ]
349 || [ string match $visibility "normal" ] } {
350 setup_xfail "powerpc-*-linux*"
351 setup_xfail "s390x-*-linux*"
352 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
353 setup_xfail "sparc*-*-linux*"
354 }
355 }
356 if { ![ string match $visibility "hidden_undef" ]
357 && ![ string match $visibility "protected_undef" ] } {
358 setup_xfail "ia64-*-linux*"
359 setup_xfail "alpha*-*-linux*"
360 setup_xfail "mips*-*-linux*"
361 }
362 if { [is_elf64 $tmpdir/mainnp.o] } {
363 setup_xfail "x86_64-*-linux*"
364 }
365 setup_xfail "x86_64-*-linux-gnux32"
366 if { ![istarget hppa*64*-*-linux*] } {
367 setup_xfail "hppa*-*-linux*"
368 }
369 if [ string match $shared_needs_pic "yes" ] {
370 setup_xfail "arm*-*-linux*"
371 }
372
373 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
374 mainnp.o sh1np.o sh2np.o elfvsb \
375 "-T $srcdir/$subdir/elf-offset.ld"
376 } }
377
378 # Now compile the code using -fpic.
379
380 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
381 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
382 unresolved "visibility ($visibility)"
383 } else {
384 if { [ string match $visibility "protected" ]
385 || [ string match $visibility "protected_undef_def" ] } {
386 if [ string match $support_protected "no" ] {
387 setup_xfail $target_triplet
388 }
389 }
390 # SunOS can not compare function pointers correctly
391 if [istarget "*-*-sunos4*"] {
392 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
393 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
394 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
395 } else {
396 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
397 } }
398 }
399 }}
400
401 if { [istarget powerpc*-*-linux*] } {
402 # Don't bother.
403 } else {
404 # Now do the same tests again, but this time compile main.c PIC.
405 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
406 unresolved "visibility ($visibility) (PIC main, non PIC so)"
407 unresolved "visibility ($visibility) (PIC main)"
408 } else {
409 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
410 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
411 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
412 } else {
413 # SunOS non PIC shared libraries don't permit some cases of
414 # overriding.
415 if { [ string match $visibility "protected" ]
416 || [ string match $visibility "protected_undef_def" ] } {
417 if [ string match $support_protected "no" ] {
418 setup_xfail $target_triplet
419 }
420 } else {
421 setup_xfail "*-*-sunos4*"
422 }
423 if { ![ string match $visibility "hidden_undef" ]
424 && ![ string match $visibility "protected_undef" ] } {
425 setup_xfail "ia64-*-linux*"
426 setup_xfail "alpha*-*-linux*"
427 }
428 if { ![ string match $visibility "hidden" ]
429 && ![ string match $visibility "hidden_undef" ]
430 && ![ string match $visibility "hidden_undef_def" ]
431 && ![ string match $visibility "protected_undef" ] } {
432 setup_xfail "s390x-*-linux*"
433 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
434 setup_xfail "sparc*-*-linux*"
435 }
436 }
437 if { [is_elf64 $tmpdir/mainp.o] } {
438 setup_xfail "x86_64-*-linux*"
439 }
440 setup_xfail "x86_64-*-linux-gnux32"
441 if { ![istarget hppa*64*-*-linux*] } {
442 setup_xfail "hppa*-*-linux*"
443 }
444 if [ string match $shared_needs_pic "yes" ] {
445 setup_xfail "arm*-*-linux*"
446 }
447
448 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
449 }
450 } else {
451 unresolved "visibility (PIC main, non PIC so)"
452 }
453
454 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
455 if { [ string match $visibility "protected" ]
456 || [ string match $visibility "protected_undef_def" ] } {
457 if [ string match $support_protected "no" ] {
458 setup_xfail $target_triplet
459 }
460 }
461 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
462 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
463 } else {
464 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
465 }
466 } else {
467 unresolved "visibility ($visibility) (PIC main)"
468 }
469 }}
470 }
471
472 # Old version of GCC for MIPS default to enabling -fpic
473 # and get confused if it is used on the command line.
474 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
475 set picflag ""
476 } else {
477 # Unfortunately, the gcc argument is -fpic and the cc argument is
478 # -KPIC. We have to try both.
479 set picflag "-fpic"
480 send_log "$CC $picflag\n"
481 verbose "$CC $picflag"
482 catch "exec $CC $picflag" exec_output
483 send_log "$exec_output\n"
484 verbose "--" "$exec_output"
485 if { [string match "*illegal option*" $exec_output] \
486 || [string match "*option ignored*" $exec_output] \
487 || [string match "*unrecognized option*" $exec_output] \
488 || [string match "*passed to ld*" $exec_output] } {
489 if [istarget *-*-sunos4*] {
490 set picflag "-pic"
491 } else {
492 set picflag "-KPIC"
493 }
494 }
495 }
496 verbose "Using $picflag to compile PIC code"
497
498 visibility_run hidden
499 visibility_run hidden_normal
500 visibility_run hidden_undef
501 visibility_run hidden_undef_def
502 visibility_run hidden_weak
503 visibility_run protected
504 visibility_run protected_undef
505 visibility_run protected_undef_def
506 visibility_run protected_weak
507 visibility_run normal
508
509 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
510 unresolved "common hidden symbol"
511 } else {
512 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
513 fail "common hidden symbol"
514 } else {
515 pass "common hidden symbol"
516 }
517 }
518
519 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
520 unresolved "weak hidden symbol"
521 } else {
522 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
523 unresolved "weak hidden symbol"
524 } else {
525 if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
526 fail "weak hidden symbol"
527 } else {
528 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
529 fail "weak hidden symbol DSO last"
530 } else {
531 pass "weak hidden symbol DSO last"
532 }
533 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
534 fail "weak hidden symbol DSO first"
535 } else {
536 pass "weak hidden symbol DSO first"
537 }
538 }
539 }
540 }
541
542 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
543 # Remove the temporary directory.
544 catch "exec rm -rf $tmpdir" exec_status
545 }