Use better test for usable compiler in ld testsuite.
[external/binutils.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 #   Copyright (C) 2000-2019 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 { ![check_compiler_available] } {
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 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 set tmpdir tmpdir
60 set SHCFLAG ""
61 set shared_needs_pic "no"
62 set COMPRESS_LDFLAG "-Wl,--compress-debug-sections=zlib-gabi"
63
64 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
65
66     # AIX shared libraries do not seem to support useful features,
67     # like overriding the shared library function or letting the
68     # shared library refer to objects defined in the main program.  We
69     # avoid testing those features.
70     set SHCFLAG "-DXCOFF_TEST"
71
72     # The AIX 3.2.5 loader appears to randomly fail when loading
73     # shared libraries from NSF mounted partitions, so we avoid any
74     # potential problems by using a local directory.
75     catch {exec /bin/sh -c "echo $$"} pid
76     set tmpdir /usr/tmp/ld.$pid
77     catch "exec mkdir $tmpdir" exec_status
78
79     # On AIX, we need to explicitly export the symbols the shared
80     # library is going to provide, and need.
81     set file [open $tmpdir/xcoff.exp w]
82     puts $file shlibvar1
83     puts $file shlibvar2
84     puts $file shlib_shlibvar1
85     puts $file shlib_shlibvar2
86     puts $file shlib_shlibcall
87     puts $file shlib_shlibcalled
88     puts $file shlib_checkfunptr1
89     puts $file shlib_getfunptr1
90     puts $file shlib_check
91     close $file
92 }
93
94 if [istarget arm*-*-linux*] {
95     # On ARM section anchors can change the symbol pre-emptability for
96     # non-PIC shared libraries, causing these tests to fail.  Turn section
97     # anchors off.
98     set SHCFLAG "-fno-section-anchors"
99
100     # On targets that have MOVW the compiler will emit relocations which
101     # the linker doesn't support when compiling -shared without -fpic.  The
102     # test to find out whether we want to XFAIL the non-PIC tests requires
103     # a compile - so we pre-calculate it here.  We also note that this can
104     # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
105     if [istarget arm*-*-*eabi*] {
106         set file [open $tmpdir/movw-detect.c w]
107         puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
108         close $file
109         if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
110             set shared_needs_pic "yes"
111         }
112     }
113 }
114
115 set support_protected "no"
116
117 if { [istarget *-*-linux*]
118      || [istarget *-*-nacl*]
119      || [istarget *-*-gnu*] } {
120     if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
121       if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
122         catch "exec $tmpdir/main" support_protected
123       }
124     }
125 }
126
127 # The test procedure.
128 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
129     global CC
130     global srcdir
131     global subdir
132     global exec_output
133     global link_output
134     global host_triplet
135     global tmpdir
136
137     if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
138
139     # Build the shared library.
140     # On AIX, we need to use an export file.
141     set shared -shared
142     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
143         set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
144     }
145     if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
146         if { [ string match $visibility "hidden_undef" ]
147              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
148              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
149             pass "$testname"
150         } else { if { [ string match $visibility "protected_undef" ]
151              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
152              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
153             pass "$testname"
154         } else {
155             fail "$testname"
156         }}
157         return
158     }
159
160     # Link against the shared library.  Use -rpath so that the
161     # dynamic linker can locate the shared library at runtime.
162     # On AIX, we must include /lib in -rpath, as otherwise the loader
163     # can not find -lc.
164     set rpath $tmpdir
165     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
166         set rpath /lib:$tmpdir
167     }
168     if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
169         if { [ string match $visibility "hidden" ]
170              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
171              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
172             pass "$testname"
173         } else { if { [ string match $visibility "hidden_undef_def" ]
174              && [regexp "undefined reference to \`\.?visibility\'" $link_output]
175              && [regexp "undefined reference to \`visibility_def\'" $link_output]
176              && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
177              && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
178             pass "$testname"
179         } else {
180             fail "$testname"
181         }}
182         return
183     }
184
185     if { [ string match $visibility "hidden" ]
186          || [ string match $visibility "hidden_undef" ]
187          || [ string match $visibility "protected_undef" ] } {
188         fail "$testname"
189     }
190
191     if ![isnative] {
192         unsupported "$testname"
193         return
194     }
195
196     # Run the resulting program
197     send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
198     verbose "$tmpdir/$progname >$tmpdir/$progname.out"
199     catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
200     if ![string match "" $exec_output] then {
201         send_log "$exec_output\n"
202         verbose "$exec_output"
203         fail "$testname"
204         return
205     }
206
207     send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
208     verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
209     catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
210     set exec_output [prune_warnings $exec_output]
211
212     if {![string match "" $exec_output]} then {
213         send_log "$exec_output\n"
214         verbose "$exec_output"
215         fail "$testname"
216         return
217     }
218
219     pass "$testname"
220 }
221
222 proc visibility_run {visibility} {
223     global CC
224     global CFLAGS
225     global SHCFLAG
226     global srcdir
227     global subdir
228     global tmpdir
229     global picflag
230     global target_triplet
231     global support_protected
232     global shared_needs_pic
233     global PLT_CFLAGS
234     global NOPIE_CFLAGS
235     global COMPRESS_LDFLAG
236
237     if [ string match $visibility "hidden" ] {
238         set VSBCFLAG "-DHIDDEN_TEST"
239     } else { if [ string match $visibility "hidden_normal" ] {
240         set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
241     } else { if [ string match $visibility "hidden_undef" ] {
242         set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
243     } else { if [ string match $visibility "hidden_undef_def" ] {
244         set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
245     } else { if [ string match $visibility "hidden_weak" ] {
246         set VSBCFLAG "-DHIDDEN_WEAK_TEST"
247     } else { if [ string match $visibility "protected" ] {
248         set VSBCFLAG "-DPROTECTED_TEST"
249     } else { if [ string match $visibility "protected_undef" ] {
250         set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
251     } else { if [ string match $visibility "protected_undef_def" ] {
252         set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
253     } else { if [ string match $visibility "protected_weak" ] {
254         set VSBCFLAG "-DPROTECTED_WEAK_TEST"
255     } else {
256         set VSBCFLAG ""
257     }}}}}}}}}
258
259     # Ensure we always start with a clean slate, for the "file exists"
260     # tests below.
261     remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
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 $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
285              || ![ld_compile "$CC -g $NOPIE_CFLAGS $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             }
298
299             # Non-pic code uses name binding rules for applications to
300             # reference variables by gp-relative relocs, which can't be
301             # used with overridable symbols.
302             if { ![ string match $visibility "hidden_undef" ]
303                  && ![ string match $visibility "protected_undef" ] } {
304                 setup_xfail "ia64-*-linux*"
305                 setup_xfail "alpha*-*-linux*"
306             }
307             if { ![ string match $visibility "hidden" ]
308                  && ![ string match $visibility "hidden_undef" ]
309                  && ![ string match $visibility "hidden_undef_def" ]
310                  && ![ string match $visibility "protected_undef" ] } {
311                 setup_xfail "s390x-*-linux*"
312                 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
313                     setup_xfail "sparc*-*-linux*"
314                 }
315             }
316             if { [is_elf64 $tmpdir/mainnp.o] } {
317                 setup_xfail "x86_64-*-linux*"
318             }
319             setup_xfail "x86_64-*-linux-gnux32"
320             if { ![istarget hppa*64*-*-linux*] } {
321                 setup_xfail "hppa*-*-linux*"
322             }
323             if [ string match $shared_needs_pic "yes" ] {
324                 setup_xfail "arm*-*-linux*"
325             }
326
327             visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
328
329             # Test ELF shared library relocations with a non-zero load
330             # address for the library.  Near as I can tell, the R_*_RELATIVE
331             # relocations for various targets are broken in the case where
332             # the load address is not zero (which is the default).
333             if { [ string match $visibility "protected" ]
334                  || [ string match $visibility "protected_undef_def" ] } {
335                 if [ string match $support_protected "no" ] {
336                     setup_xfail $target_triplet
337                 }
338             }
339             if { [ string match $visibility "hidden_normal" ]
340                  || [ string match $visibility "hidden_weak" ]
341                  || [ string match $visibility "protected" ]
342                  || [ string match $visibility "protected_undef_def" ]
343                  || [ string match $visibility "protected_weak" ]
344                  || [ string match $visibility "normal" ] } {
345                 setup_xfail "powerpc-*-linux*"
346                 setup_xfail "s390x-*-linux*"
347                 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
348                     setup_xfail "sparc*-*-linux*"
349                 }
350             }
351             if { ![ string match $visibility "hidden_undef" ]
352                  && ![ string match $visibility "protected_undef" ] } {
353                 setup_xfail "ia64-*-linux*"
354                 setup_xfail "alpha*-*-linux*"
355                 setup_xfail "mips*-*-linux*"
356             }
357             if { [is_elf64 $tmpdir/mainnp.o] } {
358                 setup_xfail "x86_64-*-linux*"
359             }
360             setup_xfail "x86_64-*-linux-gnux32"
361             if { ![istarget hppa*64*-*-linux*] } {
362                 setup_xfail "hppa*-*-linux*"
363             }
364             if [ string match $shared_needs_pic "yes" ] {
365                 setup_xfail "arm*-*-linux*"
366             }
367
368             visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
369                 mainnp.o sh1np.o sh2np.o elfvsb \
370                 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
371         } }
372
373         # Now compile the code using -fpic.
374
375         if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
376             || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
377             unresolved "visibility ($visibility)"
378         } else {
379             if { [ string match $visibility "protected" ]
380                  || [ string match $visibility "protected_undef_def" ] } {
381                 if [ string match $support_protected "no" ] {
382                     setup_xfail $target_triplet
383                 }
384             }
385             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
386                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
387             } else {
388                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
389             }
390         }
391     }}
392
393     if { [istarget powerpc*-*-linux*] } {
394         # Don't bother.
395     } else {
396     # Now do the same tests again, but this time compile main.c PIC.
397     if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
398         unresolved "visibility ($visibility) (PIC main, non PIC so)"
399         unresolved "visibility ($visibility) (PIC main)"
400     } else {
401         if { [ remote_file host exists $tmpdir/sh1np.o ]
402              && [ remote_file host exists $tmpdir/sh2np.o ] } {
403             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
404                 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
405             } else {
406                 # SunOS non PIC shared libraries don't permit some cases of
407                 # overriding.
408                 if { [ string match $visibility "protected" ]
409                      || [ string match $visibility "protected_undef_def" ] } {
410                     if [ string match $support_protected "no" ] {
411                         setup_xfail $target_triplet
412                     }
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 { [ remote_file host exists $tmpdir/sh1p.o ]
446              && [ remote_file host exists $tmpdir/sh2p.o ] } {
447             if { [ string match $visibility "protected" ]
448                  || [ string match $visibility "protected_undef_def" ] } {
449                 if [ string match $support_protected "no" ] {
450                     setup_xfail $target_triplet
451                 }
452             }
453             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
454                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
455             } else {
456                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
457             }
458         } else {
459             unresolved "visibility ($visibility) (PIC main)"
460         }
461     }}
462 }
463
464 # Old version of GCC for MIPS default to enabling -fpic
465 # and get confused if it is used on the command line.
466 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
467     set picflag ""
468 } else {
469     # Unfortunately, the gcc argument is -fpic and the cc argument is
470     # -KPIC.  We have to try both.
471     set picflag "-fpic"
472     send_log "$CC $picflag\n"
473     verbose "$CC $picflag"
474     catch "exec $CC $picflag" exec_output
475     send_log "$exec_output\n"
476     verbose "--" "$exec_output"
477     if { [string match "*illegal option*" $exec_output] \
478          || [string match "*option ignored*" $exec_output] \
479          || [string match "*unrecognized option*" $exec_output] \
480          || [string match "*passed to ld*" $exec_output] } {
481         set picflag "-KPIC"
482     }
483 }
484 verbose "Using $picflag to compile PIC code"
485
486 visibility_run hidden
487 visibility_run hidden_normal
488 visibility_run hidden_undef
489 visibility_run hidden_undef_def
490 visibility_run hidden_weak
491 visibility_run protected
492 visibility_run protected_undef
493 visibility_run protected_undef_def
494 visibility_run protected_weak
495 visibility_run normal
496
497 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
498     unresolved "common hidden symbol"
499 } else {
500     if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
501         fail "common hidden symbol"
502     } else {
503         pass "common hidden symbol"
504     }
505 }
506
507 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
508     unresolved "weak hidden symbol"
509 } else {
510    if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
511         unresolved "weak hidden symbol"
512     } else {
513         if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
514             fail "weak hidden symbol"
515         } else {
516             if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
517                 fail "weak hidden symbol DSO last"
518             } else {
519                 pass "weak hidden symbol DSO last"
520             }
521             if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
522                 fail "weak hidden symbol DSO first"
523             } else {
524                 pass "weak hidden symbol DSO first"
525             }
526         }
527     }
528 }
529
530 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
531     # Remove the temporary directory.
532     catch "exec rm -rf $tmpdir" exec_status
533 }