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