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