2001-06-06 H.J. Lu <hjl@gnu.org>
[external/binutils.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         shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
199
200         # Test ELF shared library relocations with a non-zero load
201         # address for the library.  Near as I can tell, the R_*_RELATIVE
202         # relocations for various targets are broken in the case where
203         # the load address is not zero (which is the default).
204         setup_xfail "*-*-sunos4*"
205         setup_xfail "*-*-linux*libc1"
206         setup_xfail "powerpc-*-linux*"
207         setup_xfail "ia64-*-linux*"
208         setup_xfail "mips*-*-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             ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
228             ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
229             shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic" 
230             ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
231             ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
232         } }
233     }
234 }
235
236 # Now do the same tests again, but this time compile main.c PIC.
237 if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
238     unresolved "shared (PIC main, non PIC so)"
239     unresolved "shared (PIC main)"
240 } else {
241     if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
242         if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
243             shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
244         } else {
245             # SunOS non PIC shared libraries don't permit some cases of
246             # overriding.
247             setup_xfail "*-*-sunos4*"
248             setup_xfail "ia64-*-linux*"
249             shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
250         }
251     } else {
252         unresolved "shared (PIC main, non PIC so)"
253     }
254
255     if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
256         if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
257             shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
258         } else {
259             shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
260         }
261     } else {
262         unresolved "shared (PIC main)"
263     }
264 }
265
266 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
267     # Remove the temporary directory.
268     catch "exec rm -rf $tmpdir" exec_status
269 }