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