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