b53b96a4d04b6f7379020c65cbc192afee105571
[external/binutils.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 #   Copyright 2000, 2001, 2003 Free Software Foundation, Inc.
3 #
4 # This file is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #
18 # Written by Ian Lance Taylor (ian@cygnus.com)
19 #            and H.J. Lu (hjl@gnu.org)
20 #
21
22 # Make sure that ld can generate ELF shared libraries with visibility.
23
24 # This test can only be run on a couple of ELF platforms.
25 # Square bracket expressions seem to confuse istarget.
26 if { ![istarget hppa*64*-*-hpux*] \
27      && ![istarget hppa*-*-linux*] \
28      && ![istarget i?86-*-linux*] \
29      && ![istarget ia64-*-linux*] \
30      && ![istarget m68k-*-linux*] \
31      && ![istarget mips*-*-linux*] \
32      && ![istarget powerpc-*-linux*] \
33      && ![istarget arm*-*-linux*] \
34      && ![istarget alpha*-*-linux*] \
35      && ![istarget sparc*-*-linux*] \
36      && ![istarget s390*-*-linux*] \
37      && ![istarget x86_64-*-linux*] } {
38     return
39 }
40
41 if { [istarget *-*-linux*aout*] \
42      || [istarget *-*-linux*oldld*] } {
43     return
44 }
45
46 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
47 foreach t $test_list {
48     # We need to strip the ".d", but can leave the dirname.
49     verbose [file rootname $t]
50     run_dump_test [file rootname $t]
51 }
52
53 # The remaining tests can only be run if ld generates native executables.
54 if ![isnative] then {return}
55
56 set tmpdir tmpdir
57 set SHCFLAG ""
58
59 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
60
61     # AIX shared libraries do not seem to support useful features,
62     # like overriding the shared library function or letting the
63     # shared library refer to objects defined in the main program.  We
64     # avoid testing those features.
65     set SHCFLAG "-DXCOFF_TEST"
66
67     # The AIX 3.2.5 loader appears to randomly fail when loading
68     # shared libraries from NSF mounted partitions, so we avoid any
69     # potential problems by using a local directory.
70     catch {exec /bin/sh -c "echo $$"} pid
71     set tmpdir /usr/tmp/ld.$pid
72     catch "exec mkdir $tmpdir" exec_status
73
74     # On AIX, we need to explicitly export the symbols the shared
75     # library is going to provide, and need.
76     set file [open $tmpdir/xcoff.exp w]
77     puts $file shlibvar1
78     puts $file shlibvar2
79     puts $file shlib_shlibvar1
80     puts $file shlib_shlibvar2
81     puts $file shlib_shlibcall
82     puts $file shlib_shlibcalled
83     puts $file shlib_checkfunptr1
84     puts $file shlib_getfunptr1
85     puts $file shlib_check
86     close $file
87 }
88
89 set support_protected "no"
90
91 if [istarget *-*-linux*] {
92     if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
93       if [ld_link $ld $tmpdir/main "$tmpdir/main.o"] {
94         catch "exec $tmpdir/main" support_protected
95       }
96     }
97 }
98
99 # The test procedure.
100 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
101     global ld
102     global srcdir
103     global subdir
104     global exec_output
105     global link_output
106     global host_triplet
107     global tmpdir
108
109     if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
110
111     # Build the shared library.
112     # On AIX, we need to use an export file.
113     set shared -shared
114     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
115         set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
116     }
117     if {![ld_simple_link $ld $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
118         if { [ string match $visibility "hidden_undef" ]
119              && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
120              && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
121             pass "$testname"
122         } else { if { [ string match $visibility "protected_undef" ]
123              && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
124              && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
125             pass "$testname"
126         } else {
127             fail "$testname"
128         }}
129         return
130     }
131
132     # Link against the shared library.  Use -rpath so that the
133     # dynamic linker can locate the shared library at runtime.
134     # On AIX, we must include /lib in -rpath, as otherwise the loader
135     # can not find -lc.
136     set rpath $tmpdir
137     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
138         set rpath /lib:$tmpdir
139     }
140     if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] {
141         if { [ string match $visibility "hidden" ]
142              && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
143              && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
144             pass "$testname"
145         } else { if { [ string match $visibility "hidden_undef_def" ]
146              && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
147              && [regexp ".*/main.c.*: undefined reference to \`visibility_def\'" $link_output]
148              && [regexp ".*/main.c.*: undefined reference to \`visibility_func\'" $link_output]
149              && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
150             pass "$testname"
151         } else {
152             fail "$testname"
153         }}
154         return
155     }
156
157     if { [ string match $visibility "hidden" ]
158          || [ string match $visibility "hidden_undef" ]
159          || [ string match $visibility "protected_undef" ] } {
160         fail "$testname"
161     }
162
163     # Run the resulting program
164     send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
165     verbose "$tmpdir/$progname >$tmpdir/$progname.out"
166     catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
167     if ![string match "" $exec_output] then {
168         send_log "$exec_output\n"
169         verbose "$exec_output"
170         fail "$testname"
171         return
172     }
173
174     send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
175     verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
176     catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
177     set exec_output [prune_warnings $exec_output]
178
179     if {![string match "" $exec_output]} then {
180         send_log "$exec_output\n"
181         verbose "$exec_output"
182         fail "$testname"
183         return
184     }
185
186     pass "$testname"
187 }
188
189 proc visibility_run {visibility} {
190     global CC
191     global CFLAGS
192     global SHCFLAG
193     global srcdir
194     global subdir
195     global tmpdir
196     global picflag
197     global target_triplet
198     global support_protected
199
200     if [ string match $visibility "hidden" ] {
201         set VSBCFLAG "-DHIDDEN_TEST"
202     } else { if [ string match $visibility "hidden_normal" ] {
203         set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
204     } else { if [ string match $visibility "hidden_undef" ] {
205         set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
206     } else { if [ string match $visibility "hidden_undef_def" ] {
207         set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
208     } else { if [ string match $visibility "hidden_weak" ] {
209         set VSBCFLAG "-DHIDDEN_WEAK_TEST"
210     } else { if [ string match $visibility "protected" ] {
211         set VSBCFLAG "-DPROTECTED_TEST"
212     } else { if [ string match $visibility "protected_undef" ] {
213         set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
214     } else { if [ string match $visibility "protected_undef_def" ] {
215         set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
216     } else { if [ string match $visibility "protected_weak" ] {
217         set VSBCFLAG "-DPROTECTED_WEAK_TEST"
218     } else {
219         set VSBCFLAG ""
220     }}}}}}}}}
221
222     # Compile the main program.
223     if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
224         unresolved "visibility ($visibility) (non PIC)"
225         unresolved "visibility ($visibility)"
226     } else {
227         # The shared library is composed of two files.  First compile them
228         # without using -fpic.  That should work on an ELF system,
229         # although it will be less efficient because the dynamic linker
230         # will need to do more relocation work.  However, note that not
231         # using -fpic will cause some of the tests to return different
232         # results.
233         if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
234              || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
235             unresolved "visibility ($visibility) (non PIC)"
236         } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
237             visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
238         } else {
239             # SunOS non PIC shared libraries don't permit some cases of
240             # overriding.
241             if { [ string match $visibility "protected" ]
242                  || [ string match $visibility "protected_undef_def" ] } {
243                 if [ string match $support_protected "no" ] {
244                     setup_xfail $target_triplet
245                 }
246             } else {
247                 setup_xfail "*-*-sunos4*"
248             }
249             if { [ string match $visibility "hidden_weak" ]
250                  || [ string match $visibility "protected_weak" ] } {
251                 setup_xfail "powerpc-*-linux*"
252             }
253
254             # Non-pic code uses name binding rules for applications to
255             # reference variables by gp-relative relocs, which can't be
256             # used with overridable symbols.
257             if { ![ string match $visibility "hidden_undef" ]
258                  && ![ string match $visibility "protected_undef" ] } {
259                 setup_xfail "ia64-*-linux*"
260                 setup_xfail "alpha*-*-linux*"
261             }
262             setup_xfail "x86_64-*-linux*"
263
264             visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
265
266             # Test ELF shared library relocations with a non-zero load
267             # address for the library.  Near as I can tell, the R_*_RELATIVE
268             # relocations for various targets are broken in the case where
269             # the load address is not zero (which is the default).
270             if { [ string match $visibility "protected" ]
271                  || [ string match $visibility "protected_undef_def" ] } {
272                 if [ string match $support_protected "no" ] {
273                     setup_xfail $target_triplet
274                 }
275             } else {
276                 setup_xfail "*-*-sunos4*"
277                 setup_xfail "*-*-linux*libc1"
278             }
279             if { [ string match $visibility "hidden_normal" ]
280                  || [ string match $visibility "hidden_weak" ]
281                  || [ string match $visibility "protected" ]
282                  || [ string match $visibility "protected_undef_def" ]
283                  || [ string match $visibility "protected_weak" ]
284                  || [ string match $visibility "normal" ] } {
285                 setup_xfail "powerpc-*-linux*"
286             }
287             if { ![ string match $visibility "hidden_undef" ]
288                  && ![ string match $visibility "protected_undef" ] } {
289                 setup_xfail "ia64-*-linux*"
290                 setup_xfail "alpha*-*-linux*"
291                 setup_xfail "mips*-*-linux*"
292             }
293             setup_xfail "x86_64-*-linux*"
294             visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
295                 mainnp.o sh1np.o sh2np.o elfvsb \
296                 "-T $srcdir/$subdir/elf-offset.ld"
297         } }
298
299         # Now compile the code using -fpic.
300
301         if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o] 
302             || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
303             unresolved "visibility ($visibility)"
304         } else {
305             if { [ string match $visibility "protected" ]
306                  || [ string match $visibility "protected_undef_def" ] } {
307                 if [ string match $support_protected "no" ] {
308                     setup_xfail $target_triplet
309                 }
310             }
311             # SunOS can not compare function pointers correctly
312             if [istarget "*-*-sunos4*"] {
313                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
314             } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
315                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
316             } else {
317                 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
318             } }
319         }
320     }
321
322     # Now do the same tests again, but this time compile main.c PIC.
323     if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
324         unresolved "visibility ($visibility) (PIC main, non PIC so)"
325         unresolved "visibility ($visibility) (PIC main)"
326     } else {
327         if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
328             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
329                 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
330             } else {
331                 # SunOS non PIC shared libraries don't permit some cases of
332                 # overriding.
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                 } else {
339                     setup_xfail "*-*-sunos4*"
340                 }
341                 if { [ string match $visibility "hidden_weak" ]
342                      || [ string match $visibility "protected_weak" ] } {
343                     setup_xfail "powerpc-*-linux*"
344                 }
345                 if { ![ string match $visibility "hidden_undef" ]
346                      && ![ string match $visibility "protected_undef" ] } {
347                     setup_xfail "ia64-*-linux*"
348                     setup_xfail "alpha*-*-linux*"
349                 }
350                 setup_xfail "x86_64-*-linux*"
351                 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
352             }
353         } else {
354             unresolved "visibility (PIC main, non PIC so)"
355         }
356
357         if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
358             if { [ string match $visibility "protected" ]
359                  || [ string match $visibility "protected_undef_def" ] } {
360                 if [ string match $support_protected "no" ] {
361                     setup_xfail $target_triplet
362                 }
363             }
364             if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
365                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
366             } else {
367                 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
368             }
369         } else {
370             unresolved "visibility ($visibility) (PIC main)"
371         }
372     }
373 }
374
375 if [istarget mips*-*-*] {
376     set picflag ""
377 } else {
378     # Unfortunately, the gcc argument is -fpic and the cc argument is
379     # -KPIC.  We have to try both.
380     set picflag "-fpic"
381     send_log "$CC $picflag\n"
382     verbose "$CC $picflag"
383     catch "exec $CC $picflag" exec_output
384     send_log "$exec_output\n"
385     verbose "--" "$exec_output"
386     if { [string match "*illegal option*" $exec_output] \
387          || [string match "*option ignored*" $exec_output] \
388          || [string match "*unrecognized option*" $exec_output] \
389          || [string match "*passed to ld*" $exec_output] } {
390         if [istarget *-*-sunos4*] {
391             set picflag "-pic"
392         } else {
393             set picflag "-KPIC"
394         }
395     }
396 }
397 verbose "Using $picflag to compile PIC code"
398
399 visibility_run hidden
400 visibility_run hidden_normal
401 visibility_run hidden_undef
402 visibility_run hidden_undef_def
403 visibility_run hidden_weak
404 visibility_run protected
405 visibility_run protected_undef
406 visibility_run protected_undef_def
407 visibility_run protected_weak
408 visibility_run normal
409
410 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
411     # Remove the temporary directory.
412     catch "exec rm -rf $tmpdir" exec_status
413 }