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