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