bfd/
[deliverable/binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
CommitLineData
252b5132 1# Expect script for ld-shared tests
9147e853 2# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003
a2b64bed 3# Free Software Foundation, Inc.
252b5132
RH
4#
5# This file is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18#
19# Written by Ian Lance Taylor (ian@cygnus.com)
20#
21
22# Make sure that ld can generate ELF shared libraries.
23# Note that linking against ELF shared libraries is tested by the
24# bootstrap test.
25
26# This test can only be run if ld generates native executables.
27if ![isnative] then {return}
28
29# This test can only be run on a couple of ELF platforms.
30# Square bracket expressions seem to confuse istarget.
19c7c582
AM
31if { ![istarget hppa*64*-*-hpux*] \
32 && ![istarget hppa*-*-linux*] \
33 && ![istarget i?86-*-sysv4*] \
ad995491
L
34 && ![istarget i?86-*-unixware] \
35 && ![istarget i?86-*-elf*] \
36 && ![istarget i?86-*-linux*] \
37 && ![istarget ia64-*-elf*] \
38 && ![istarget ia64-*-linux*] \
252b5132
RH
39 && ![istarget m68k-*-linux*] \
40 && ![istarget mips*-*-irix5*] \
66517a2f 41 && ![istarget mips*-*-linux*] \
252b5132
RH
42 && ![istarget powerpc-*-elf*] \
43 && ![istarget powerpc-*-linux*] \
44 && ![istarget powerpc-*-sysv4*] \
45 && ![istarget sparc*-*-elf] \
46 && ![istarget sparc*-*-solaris2*] \
47 && ![istarget sparc*-*-sunos4*] \
b33b6e45 48 && ![istarget sparc*-*-linux*] \
4f38fc1c 49 && ![istarget arm*-*-linux*] \
2ffd68ef 50 && ![istarget alpha*-*-linux*] \
252b5132 51 && ![istarget rs6000*-*-aix*] \
9147e853
JJ
52 && ![istarget powerpc*-*-aix*] \
53 && ![istarget s390*-*-linux*] \
54 && ![istarget x86_64-*-linux*] } {
252b5132
RH
55 return
56}
57
99c262f8
ILT
58if { [istarget *-*-linux*aout*] \
59 || [istarget *-*-linux*oldld*] } {
252b5132
RH
60 return
61}
62
63set tmpdir tmpdir
64set SHCFLAG ""
65
66if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
67
68 # AIX shared libraries do not seem to support useful features,
69 # like overriding the shared library function or letting the
70 # shared library refer to objects defined in the main program. We
71 # avoid testing those features.
72 set SHCFLAG "-DXCOFF_TEST"
73
74 # The AIX 3.2.5 loader appears to randomly fail when loading
75 # shared libraries from NSF mounted partitions, so we avoid any
76 # potential problems by using a local directory.
77 catch {exec /bin/sh -c "echo $$"} pid
78 set tmpdir /usr/tmp/ld.$pid
79 catch "exec mkdir $tmpdir" exec_status
80
81 # On AIX, we need to explicitly export the symbols the shared
82 # library is going to provide, and need.
83 set file [open $tmpdir/xcoff.exp w]
84 puts $file shlibvar1
85 puts $file shlibvar2
86 puts $file shlib_shlibvar1
87 puts $file shlib_shlibvar2
88 puts $file shlib_shlibcall
89 puts $file shlib_shlibcalled
90 puts $file shlib_checkfunptr1
91 puts $file shlib_getfunptr1
92 puts $file shlib_check
93 close $file
94}
95
96# The test procedure.
97proc shared_test { progname testname main sh1 sh2 dat args } {
98 global ld
99 global srcdir
100 global subdir
101 global exec_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 fail "$testname"
115 return
116 }
117
118 # Link against the shared library. Use -rpath so that the
119 # dynamic linker can locate the shared library at runtime.
120 # On AIX, we must include /lib in -rpath, as otherwise the loader
121 # can not find -lc.
122 set rpath $tmpdir
123 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
124 set rpath /lib:$tmpdir
125 }
126 if ![ld_link $ld $tmpdir/$progname "-rpath $rpath $tmpdir/$main $tmpdir/$progname.so"] {
127 fail "$testname"
128 return
129 }
130
131 # Run the resulting program
132 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
133 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
134 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
135 if ![string match "" $exec_output] then {
136 send_log "$exec_output\n"
137 verbose "$exec_output"
138 fail "$testname"
139 return
140 }
141
142 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
143 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
144 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
145 set exec_output [prune_warnings $exec_output]
146
147 if {![string match "" $exec_output]} then {
148 send_log "$exec_output\n"
149 verbose "$exec_output"
150 fail "$testname"
151 return
152 }
153
154 pass "$testname"
155}
156
157if [istarget mips*-*-*] {
158 set picflag ""
159} else {
160 # Unfortunately, the gcc argument is -fpic and the cc argument is
161 # -KPIC. We have to try both.
162 set picflag "-fpic"
163 send_log "$CC $picflag\n"
164 verbose "$CC $picflag"
165 catch "exec $CC $picflag" exec_output
166 send_log "$exec_output\n"
167 verbose "--" "$exec_output"
168 if { [string match "*illegal option*" $exec_output] \
169 || [string match "*option ignored*" $exec_output] \
170 || [string match "*unrecognized option*" $exec_output] \
171 || [string match "*passed to ld*" $exec_output] } {
172 if [istarget *-*-sunos4*] {
173 set picflag "-pic"
174 } else {
175 set picflag "-KPIC"
176 }
177 }
178}
179verbose "Using $picflag to compile PIC code"
180
181# Compile the main program.
182if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
183 unresolved "shared (non PIC)"
184 unresolved "shared"
185} else {
186 # The shared library is composed of two files. First compile them
187 # without using -fpic. That should work on an ELF system,
188 # although it will be less efficient because the dynamic linker
189 # will need to do more relocation work. However, note that not
190 # using -fpic will cause some of the tests to return different
191 # results.
192 if { ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
193 || ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
194 unresolved "shared (non PIC)"
195 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
196 shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
197 } else {
198 # SunOS non PIC shared libraries don't permit some cases of
199 # overriding.
200 setup_xfail "*-*-sunos4*"
ad995491 201 setup_xfail "ia64-*-linux*"
212a6b8e 202 setup_xfail "alpha*-*-linux*"
9147e853 203 setup_xfail "x86_64-*-linux*"
252b5132
RH
204 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
205
206 # Test ELF shared library relocations with a non-zero load
207 # address for the library. Near as I can tell, the R_*_RELATIVE
208 # relocations for various targets are broken in the case where
209 # the load address is not zero (which is the default).
210 setup_xfail "*-*-sunos4*"
b7be1db6 211 setup_xfail "*-*-linux*libc1"
d1d8dddf 212 setup_xfail "powerpc-*-linux*"
ad995491 213 setup_xfail "ia64-*-linux*"
212a6b8e 214 setup_xfail "alpha*-*-linux*"
66517a2f 215 setup_xfail "mips*-*-linux*"
9147e853 216 setup_xfail "x86_64-*-linux*"
252b5132
RH
217 shared_test shnp "shared (non PIC, load offset)" \
218 mainnp.o sh1np.o sh2np.o shared \
219 "-T $srcdir/$subdir/elf-offset.ld"
220 } }
221
222 # Now compile the code using -fpic.
223
224 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
225 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
226 unresolved "shared"
227 } else {
228 # SunOS can not compare function pointers correctly
229 if [istarget "*-*-sunos4*"] {
230 shared_test shp "shared" mainnp.o sh1p.o sh2p.o sun4
231 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
232 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
233 } else {
234 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
66517a2f
L
235 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
236 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
f0f077e5
L
237 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
238 ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
239 ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
252b5132
RH
240 } }
241 }
242}
243
244# Now do the same tests again, but this time compile main.c PIC.
245if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
246 unresolved "shared (PIC main, non PIC so)"
247 unresolved "shared (PIC main)"
248} else {
249 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
250 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
251 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
252 } else {
253 # SunOS non PIC shared libraries don't permit some cases of
254 # overriding.
255 setup_xfail "*-*-sunos4*"
ad995491 256 setup_xfail "ia64-*-linux*"
212a6b8e 257 setup_xfail "alpha*-*-linux*"
9147e853 258 setup_xfail "x86_64-*-linux*"
252b5132
RH
259 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
260 }
261 } else {
262 unresolved "shared (PIC main, non PIC so)"
263 }
264
265 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
266 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
267 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
268 } else {
269 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
270 }
271 } else {
272 unresolved "shared (PIC main)"
273 }
274}
275
276if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
277 # Remove the temporary directory.
278 catch "exec rm -rf $tmpdir" exec_status
279}
This page took 0.29387 seconds and 4 git commands to generate.