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