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