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