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