bfd/
[deliverable/binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
CommitLineData
252b5132 1# Expect script for ld-shared tests
aef6203b 2# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
aa820537 3# 2004, 2005, 2007, 2008, 2009
a2b64bed 4# Free Software Foundation, Inc.
252b5132 5#
f96b4a7b
NC
6# This file is part of the GNU Binutils.
7#
8# This program is free software; you can redistribute it and/or modify
252b5132 9# it under the terms of the GNU General Public License as published by
f96b4a7b 10# the Free Software Foundation; either version 3 of the License, or
252b5132 11# (at your option) any later version.
f96b4a7b 12#
252b5132
RH
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.
f96b4a7b 17#
252b5132
RH
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
f96b4a7b
NC
20# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21# MA 02110-1301, USA.
252b5132
RH
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.
31if ![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.
19c7c582
AM
35if { ![istarget hppa*64*-*-hpux*] \
36 && ![istarget hppa*-*-linux*] \
37 && ![istarget i?86-*-sysv4*] \
ad995491
L
38 && ![istarget i?86-*-unixware] \
39 && ![istarget i?86-*-elf*] \
40 && ![istarget i?86-*-linux*] \
41 && ![istarget ia64-*-elf*] \
42 && ![istarget ia64-*-linux*] \
252b5132
RH
43 && ![istarget m68k-*-linux*] \
44 && ![istarget mips*-*-irix5*] \
66517a2f 45 && ![istarget mips*-*-linux*] \
24b01a74
AM
46 && ![istarget powerpc*-*-elf*] \
47 && ![istarget powerpc*-*-linux*] \
48 && ![istarget powerpc*-*-sysv4*] \
252b5132
RH
49 && ![istarget sparc*-*-elf] \
50 && ![istarget sparc*-*-solaris2*] \
51 && ![istarget sparc*-*-sunos4*] \
b33b6e45 52 && ![istarget sparc*-*-linux*] \
4f38fc1c 53 && ![istarget arm*-*-linux*] \
2ffd68ef 54 && ![istarget alpha*-*-linux*] \
252b5132 55 && ![istarget rs6000*-*-aix*] \
9147e853
JJ
56 && ![istarget powerpc*-*-aix*] \
57 && ![istarget s390*-*-linux*] \
58 && ![istarget x86_64-*-linux*] } {
252b5132
RH
59 return
60}
61
99c262f8
ILT
62if { [istarget *-*-linux*aout*] \
63 || [istarget *-*-linux*oldld*] } {
252b5132
RH
64 return
65}
66
67set tmpdir tmpdir
68set SHCFLAG ""
a9f844b1 69set shared_needs_pic "no"
252b5132
RH
70
71if { [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
a9f844b1
NC
101if [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
252b5132
RH
122# The test procedure.
123proc shared_test { progname testname main sh1 sh2 dat args } {
b765d4e3 124 global CC
252b5132
RH
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 }
b765d4e3 139 if {![ld_simple_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
252b5132
RH
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 }
b765d4e3 152 if ![ld_simple_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
252b5132
RH
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
183if [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}
205verbose "Using $picflag to compile PIC code"
206
207# Compile the main program.
208if ![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*"
ad995491 227 setup_xfail "ia64-*-linux*"
212a6b8e 228 setup_xfail "alpha*-*-linux*"
24b01a74 229 setup_xfail "powerpc64*-*-*"
c0d48c0b
DA
230 if { ![istarget hppa*64*-*-linux*] } {
231 setup_xfail "hppa*-*-linux*"
232 }
4dc570c2
JJ
233 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
234 setup_xfail "sparc*-*-linux*"
235 }
9147e853 236 setup_xfail "x86_64-*-linux*"
4648dfcf 237 setup_xfail "s390x-*-linux*"
a9f844b1
NC
238 if [ string match $shared_needs_pic "yes" ] {
239 setup_xfail "arm*-*-linux*"
240 }
252b5132
RH
241 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
242
243 # Test ELF shared library relocations with a non-zero load
244 # address for the library. Near as I can tell, the R_*_RELATIVE
245 # relocations for various targets are broken in the case where
246 # the load address is not zero (which is the default).
247 setup_xfail "*-*-sunos4*"
b7be1db6 248 setup_xfail "*-*-linux*libc1"
24b01a74 249 setup_xfail "powerpc*-*-linux*"
ad995491 250 setup_xfail "ia64-*-linux*"
212a6b8e 251 setup_xfail "alpha*-*-linux*"
66517a2f 252 setup_xfail "mips*-*-linux*"
c0d48c0b
DA
253 if { ![istarget hppa*64*-*-linux*] } {
254 setup_xfail "hppa*-*-linux*"
255 }
4dc570c2
JJ
256 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
257 setup_xfail "sparc*-*-linux*"
258 }
9147e853 259 setup_xfail "x86_64-*-linux*"
8c37241b 260 setup_xfail "s390x-*-linux*"
a9f844b1
NC
261 if [ string match $shared_needs_pic "yes" ] {
262 setup_xfail "arm*-*-linux*"
263 }
252b5132
RH
264 shared_test shnp "shared (non PIC, load offset)" \
265 mainnp.o sh1np.o sh2np.o shared \
266 "-T $srcdir/$subdir/elf-offset.ld"
267 } }
268
269 # Now compile the code using -fpic.
270
271 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
272 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
273 unresolved "shared"
274 } else {
275 # SunOS can not compare function pointers correctly
276 if [istarget "*-*-sunos4*"] {
277 shared_test shp "shared" mainnp.o sh1p.o sh2p.o sun4
278 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
279 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
280 } else {
281 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
66517a2f
L
282 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
283 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
f0f077e5
L
284 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
285 ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
286 ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
252b5132
RH
287 } }
288 }
289}
290
291# Now do the same tests again, but this time compile main.c PIC.
292if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
293 unresolved "shared (PIC main, non PIC so)"
294 unresolved "shared (PIC main)"
295} else {
296 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
297 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
298 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
299 } else {
300 # SunOS non PIC shared libraries don't permit some cases of
301 # overriding.
302 setup_xfail "*-*-sunos4*"
ad995491 303 setup_xfail "ia64-*-linux*"
212a6b8e 304 setup_xfail "alpha*-*-linux*"
24b01a74 305 setup_xfail "powerpc64*-*-*"
c0d48c0b
DA
306 if { ![istarget hppa*64*-*-linux*] } {
307 setup_xfail "hppa*-*-linux*"
308 }
4dc570c2
JJ
309 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
310 setup_xfail "sparc*-*-linux*"
311 }
9147e853 312 setup_xfail "x86_64-*-linux*"
4648dfcf 313 setup_xfail "s390x-*-linux*"
a9f844b1
NC
314 if [ string match $shared_needs_pic "yes" ] {
315 setup_xfail "arm*-*-linux*"
316 }
252b5132
RH
317 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
318 }
319 } else {
320 unresolved "shared (PIC main, non PIC so)"
321 }
322
323 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
324 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
325 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
326 } else {
327 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
328 }
329 } else {
330 unresolved "shared (PIC main)"
331 }
332}
333
334if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
335 # Remove the temporary directory.
336 catch "exec rm -rf $tmpdir" exec_status
337}
This page took 0.487753 seconds and 4 git commands to generate.