]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-elfvsb/elfvsb.exp
2000-05-13 H.J. Lu (hjl@gnu.org)
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
CommitLineData
6fc49d28
L
1# Expect script for ld-visibility tests
2# Copyright (C) 2000 Free Software Foundation
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.
25if ![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.
29if { ![istarget i386-*-linux*] \
30 && ![istarget i486-*-linux*] \
31 && ![istarget i586-*-linux*] \
32 && ![istarget i686-*-linux*] \
33 && ![istarget m68k-*-linux*] \
34 && ![istarget powerpc-*-linux*] \
35 && ![istarget sparc*-*-linux*] } {
36 return
37}
38
39if { [istarget *-*-linux*aout*] \
40 || [istarget *-*-linux*oldld*] } {
41 return
42}
43
44set tmpdir tmpdir
45set SHCFLAG ""
46
47if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
48
49 # AIX shared libraries do not seem to support useful features,
50 # like overriding the shared library function or letting the
51 # shared library refer to objects defined in the main program. We
52 # avoid testing those features.
53 set SHCFLAG "-DXCOFF_TEST"
54
55 # The AIX 3.2.5 loader appears to randomly fail when loading
56 # shared libraries from NSF mounted partitions, so we avoid any
57 # potential problems by using a local directory.
58 catch {exec /bin/sh -c "echo $$"} pid
59 set tmpdir /usr/tmp/ld.$pid
60 catch "exec mkdir $tmpdir" exec_status
61
62 # On AIX, we need to explicitly export the symbols the shared
63 # library is going to provide, and need.
64 set file [open $tmpdir/xcoff.exp w]
65 puts $file shlibvar1
66 puts $file shlibvar2
67 puts $file shlib_shlibvar1
68 puts $file shlib_shlibvar2
69 puts $file shlib_shlibcall
70 puts $file shlib_shlibcalled
71 puts $file shlib_checkfunptr1
72 puts $file shlib_getfunptr1
73 puts $file shlib_check
74 close $file
75}
76
77# The test procedure.
78proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
79 global ld
80 global srcdir
81 global subdir
82 global exec_output
83 global link_output
84 global host_triplet
85 global tmpdir
86
87 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
88
89 # Build the shared library.
90 # On AIX, we need to use an export file.
91 set shared -shared
92 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
93 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
94 }
95 if {![ld_simple_link $ld $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
96 fail "$testname"
97 return
98 }
99
100 # Link against the shared library. Use -rpath so that the
101 # dynamic linker can locate the shared library at runtime.
102 # On AIX, we must include /lib in -rpath, as otherwise the loader
103 # can not find -lc.
104 set rpath $tmpdir
105 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
106 set rpath /lib:$tmpdir
107 }
108 if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] {
109 if { [ string match $visibility "hidden" ]
110 && [string match "*/main.c*: undefined reference to \`visibility\'" $link_output] } {
111 pass "$testname"
112 } else {
113 fail "$testname"
114 }
115 return
116 }
117
118 if [ string match $visibility "hidden" ] {
119 fail "$testname"
120 }
121
122 # Run the resulting program
123 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
124 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
125 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
126 if ![string match "" $exec_output] then {
127 send_log "$exec_output\n"
128 verbose "$exec_output"
129 fail "$testname"
130 return
131 }
132
133 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
134 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
135 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
136 set exec_output [prune_warnings $exec_output]
137
138 if {![string match "" $exec_output]} then {
139 send_log "$exec_output\n"
140 verbose "$exec_output"
141 fail "$testname"
142 return
143 }
144
145 pass "$testname"
146}
147
148proc visibility_run {visibility} {
149 global CC
150 global CFLAGS
151 global SHCFLAG
152 global srcdir
153 global subdir
154 global tmpdir
155 global picflag
156 global target_triplet
157
158 if [ string match $visibility "hidden" ] {
159 set VSBCFLAG "-DHIDDEN_TEST"
160 } else { if [ string match $visibility "hidden_normal" ] {
161 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
162 } else { if [ string match $visibility "protected" ] {
163 set VSBCFLAG "-DPROTECTED_TEST"
164 } else {
165 set VSBCFLAG ""
166 }}}
167
168 # Compile the main program.
169 if ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
170 unresolved "visibility ($visibility) (non PIC)"
171 unresolved "visibility ($visibility)"
172 } else {
173 # The shared library is composed of two files. First compile them
174 # without using -fpic. That should work on an ELF system,
175 # although it will be less efficient because the dynamic linker
176 # will need to do more relocation work. However, note that not
177 # using -fpic will cause some of the tests to return different
178 # results.
179 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
180 || ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
181 unresolved "visibility ($visibility) (non PIC)"
182 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
183 visibility_test $visibility vnp "visibility ($visibility) (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
184 } else {
185 # SunOS non PIC shared libraries don't permit some cases of
186 # overriding.
187 if [ string match $visibility "protected" ] {
188 setup_xfail $target_triplet
189 } else {
190 setup_xfail "*-*-sunos4*"
191 }
192 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
193
194 # Test ELF shared library relocations with a non-zero load
195 # address for the library. Near as I can tell, the R_*_RELATIVE
196 # relocations for various targets are broken in the case where
197 # the load address is not zero (which is the default).
198 if [ string match $visibility "protected" ] {
199 setup_xfail $target_triplet
200 } else {
201 setup_xfail "*-*-sunos4*"
202 setup_xfail "*-*-linux*libc1"
203 }
204 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
205 mainnp.o sh1np.o sh2np.o elfvsb \
206 "-T $srcdir/$subdir/elf-offset.ld"
207 } }
208
209 # Now compile the code using -fpic.
210
211 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
212 || ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
213 unresolved "visibility ($visibility)"
214 } else {
215 if [ string match $visibility "protected" ] {
216 setup_xfail $target_triplet
217 }
218 # SunOS can not compare function pointers correctly
219 if [istarget "*-*-sunos4*"] {
220 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
221 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
222 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
223 } else {
224 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
225 } }
226 }
227 }
228
229 # Now do the same tests again, but this time compile main.c PIC.
230 if ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
231 unresolved "visibility ($visibility) (PIC main, non PIC so)"
232 unresolved "visibility ($visibility) (PIC main)"
233 } else {
234 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
235 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
236 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
237 } else {
238 # SunOS non PIC shared libraries don't permit some cases of
239 # overriding.
240 if [ string match $visibility "protected" ] {
241 setup_xfail $target_triplet
242 } else {
243 setup_xfail "*-*-sunos4*"
244 }
245 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
246 }
247 } else {
248 unresolved "visibility (PIC main, non PIC so)"
249 }
250
251 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
252 if [ string match $visibility "protected" ] {
253 setup_xfail $target_triplet
254 }
255 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
256 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
257 } else {
258 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
259 }
260 } else {
261 unresolved "visibility ($visibility) (PIC main)"
262 }
263 }
264}
265
266if [istarget mips*-*-*] {
267 set picflag ""
268} else {
269 # Unfortunately, the gcc argument is -fpic and the cc argument is
270 # -KPIC. We have to try both.
271 set picflag "-fpic"
272 send_log "$CC $picflag\n"
273 verbose "$CC $picflag"
274 catch "exec $CC $picflag" exec_output
275 send_log "$exec_output\n"
276 verbose "--" "$exec_output"
277 if { [string match "*illegal option*" $exec_output] \
278 || [string match "*option ignored*" $exec_output] \
279 || [string match "*unrecognized option*" $exec_output] \
280 || [string match "*passed to ld*" $exec_output] } {
281 if [istarget *-*-sunos4*] {
282 set picflag "-pic"
283 } else {
284 set picflag "-KPIC"
285 }
286 }
287}
288verbose "Using $picflag to compile PIC code"
289
290visibility_run hidden
291visibility_run hidden_normal
292visibility_run protected
293visibility_run normal
294
295if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
296 # Remove the temporary directory.
297 catch "exec rm -rf $tmpdir" exec_status
298}