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