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