2000-06-05 H.J. Lu (hjl@gnu.org)
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
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.
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 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
39 if { [istarget *-*-linux*aout*] \
40 || [istarget *-*-linux*oldld*] } {
41 return
42 }
43
44 set tmpdir tmpdir
45 set SHCFLAG ""
46
47 if { [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.
78 proc 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 if { [ string match $visibility "hidden_undef" ]
97 && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
98 && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
99 pass "$testname"
100 } else { if { [ string match $visibility "protected_undef" ]
101 && [regexp ".*/sh1.c.*: undefined reference to \`visibility\'" $link_output]
102 && [regexp ".*/sh1.c.*: undefined reference to \`visibility_var\'" $link_output] } {
103 pass "$testname"
104 } else {
105 fail "$testname"
106 }}
107 return
108 }
109
110 # Link against the shared library. Use -rpath so that the
111 # dynamic linker can locate the shared library at runtime.
112 # On AIX, we must include /lib in -rpath, as otherwise the loader
113 # can not find -lc.
114 set rpath $tmpdir
115 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
116 set rpath /lib:$tmpdir
117 }
118 if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] {
119 if { [ string match $visibility "hidden" ]
120 && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
121 && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
122 pass "$testname"
123 } else { if { [ string match $visibility "hidden_undef_def" ]
124 && [regexp ".*/main.c.*: undefined reference to \`visibility\'" $link_output]
125 && [regexp ".*/main.c.*: undefined reference to \`visibility_var\'" $link_output] } {
126 pass "$testname"
127 } else {
128 fail "$testname"
129 }}
130 return
131 }
132
133 if { [ string match $visibility "hidden" ]
134 || [ string match $visibility "hidden_undef" ]
135 || [ string match $visibility "protected_undef" ] } {
136 fail "$testname"
137 }
138
139 # Run the resulting program
140 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
141 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
142 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
143 if ![string match "" $exec_output] then {
144 send_log "$exec_output\n"
145 verbose "$exec_output"
146 fail "$testname"
147 return
148 }
149
150 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
151 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
152 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
153 set exec_output [prune_warnings $exec_output]
154
155 if {![string match "" $exec_output]} then {
156 send_log "$exec_output\n"
157 verbose "$exec_output"
158 fail "$testname"
159 return
160 }
161
162 pass "$testname"
163 }
164
165 proc visibility_run {visibility} {
166 global CC
167 global CFLAGS
168 global SHCFLAG
169 global srcdir
170 global subdir
171 global tmpdir
172 global picflag
173 global target_triplet
174
175 if [ string match $visibility "hidden" ] {
176 set VSBCFLAG "-DHIDDEN_TEST"
177 } else { if [ string match $visibility "hidden_normal" ] {
178 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
179 } else { if [ string match $visibility "hidden_undef" ] {
180 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
181 } else { if [ string match $visibility "hidden_undef_def" ] {
182 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
183 } else { if [ string match $visibility "hidden_weak" ] {
184 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
185 } else { if [ string match $visibility "protected" ] {
186 set VSBCFLAG "-DPROTECTED_TEST"
187 } else { if [ string match $visibility "protected_undef" ] {
188 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
189 } else { if [ string match $visibility "protected_undef_def" ] {
190 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
191 } else { if [ string match $visibility "protected_weak" ] {
192 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
193 } else {
194 set VSBCFLAG ""
195 }}}}}}}}}
196
197 # Compile the main program.
198 if ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
199 unresolved "visibility ($visibility) (non PIC)"
200 unresolved "visibility ($visibility)"
201 } else {
202 # The shared library is composed of two files. First compile them
203 # without using -fpic. That should work on an ELF system,
204 # although it will be less efficient because the dynamic linker
205 # will need to do more relocation work. However, note that not
206 # using -fpic will cause some of the tests to return different
207 # results.
208 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
209 || ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
210 unresolved "visibility ($visibility) (non PIC)"
211 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
212 visibility_test $visibility vnp "visibility ($visibility) (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
213 } else {
214 # SunOS non PIC shared libraries don't permit some cases of
215 # overriding.
216 if [ string match $visibility "protected" ] {
217 setup_xfail $target_triplet
218 } else {
219 setup_xfail "*-*-sunos4*"
220 }
221 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
222
223 # Test ELF shared library relocations with a non-zero load
224 # address for the library. Near as I can tell, the R_*_RELATIVE
225 # relocations for various targets are broken in the case where
226 # the load address is not zero (which is the default).
227 if [ string match $visibility "protected" ] {
228 setup_xfail $target_triplet
229 } else {
230 setup_xfail "*-*-sunos4*"
231 setup_xfail "*-*-linux*libc1"
232 }
233 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
234 mainnp.o sh1np.o sh2np.o elfvsb \
235 "-T $srcdir/$subdir/elf-offset.ld"
236 } }
237
238 # Now compile the code using -fpic.
239
240 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
241 || ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
242 unresolved "visibility ($visibility)"
243 } else {
244 if [ string match $visibility "protected" ] {
245 setup_xfail $target_triplet
246 }
247 # SunOS can not compare function pointers correctly
248 if [istarget "*-*-sunos4*"] {
249 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o sun4
250 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
251 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
252 } else {
253 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb
254 } }
255 }
256 }
257
258 # Now do the same tests again, but this time compile main.c PIC.
259 if ![ld_compile "$CC $CFLAGS $SHCFLAG $VSBCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
260 unresolved "visibility ($visibility) (PIC main, non PIC so)"
261 unresolved "visibility ($visibility) (PIC main)"
262 } else {
263 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
264 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
265 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
266 } else {
267 # SunOS non PIC shared libraries don't permit some cases of
268 # overriding.
269 if [ string match $visibility "protected" ] {
270 setup_xfail $target_triplet
271 } else {
272 setup_xfail "*-*-sunos4*"
273 }
274 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
275 }
276 } else {
277 unresolved "visibility (PIC main, non PIC so)"
278 }
279
280 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
281 if [ string match $visibility "protected" ] {
282 setup_xfail $target_triplet
283 }
284 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
285 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
286 } else {
287 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
288 }
289 } else {
290 unresolved "visibility ($visibility) (PIC main)"
291 }
292 }
293 }
294
295 if [istarget mips*-*-*] {
296 set picflag ""
297 } else {
298 # Unfortunately, the gcc argument is -fpic and the cc argument is
299 # -KPIC. We have to try both.
300 set picflag "-fpic"
301 send_log "$CC $picflag\n"
302 verbose "$CC $picflag"
303 catch "exec $CC $picflag" exec_output
304 send_log "$exec_output\n"
305 verbose "--" "$exec_output"
306 if { [string match "*illegal option*" $exec_output] \
307 || [string match "*option ignored*" $exec_output] \
308 || [string match "*unrecognized option*" $exec_output] \
309 || [string match "*passed to ld*" $exec_output] } {
310 if [istarget *-*-sunos4*] {
311 set picflag "-pic"
312 } else {
313 set picflag "-KPIC"
314 }
315 }
316 }
317 verbose "Using $picflag to compile PIC code"
318
319 visibility_run hidden
320 visibility_run hidden_normal
321 visibility_run hidden_undef
322 visibility_run hidden_undef_def
323 visibility_run hidden_weak
324 visibility_run protected
325 visibility_run protected_undef
326 visibility_run protected_undef_def
327 visibility_run protected_weak
328 visibility_run normal
329
330 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
331 # Remove the temporary directory.
332 catch "exec rm -rf $tmpdir" exec_status
333 }
This page took 0.038566 seconds and 5 git commands to generate.