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