1 # Expect script for ld-shared tests
2 # Copyright (C) 1994-2017 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 # Written by Ian Lance Taylor (ian@cygnus.com)
24 # Make sure that ld can generate ELF shared libraries.
25 # Note that linking against ELF shared libraries is tested by the
28 # Check to see if the C compiler works
29 if { [which $CC] == 0 } {
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*] } {
65 if { [istarget *-*-linux*aout*] \
66 || [istarget *-*-linux*oldld*] } {
72 set shared_needs_pic "no"
74 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
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"
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
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]
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
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
108 set SHCFLAG "-fno-section-anchors"
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\"); }"
119 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
120 set shared_needs_pic "yes"
125 # The test procedure.
126 proc shared_test { progname testname main sh1 sh2 dat args } {
134 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
136 # Build the shared library.
137 # On AIX, we need to use an export file.
139 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
140 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
142 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
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
152 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
153 set rpath /lib:$tmpdir
155 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
161 unsupported $testname
165 # Run the resulting program
166 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
167 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
168 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
169 if ![string match "" $exec_output] then {
170 send_log "$exec_output\n"
171 verbose "$exec_output"
176 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
177 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
178 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
179 set exec_output [prune_warnings $exec_output]
181 if {![string match "" $exec_output]} then {
182 send_log "$exec_output\n"
183 verbose "$exec_output"
191 # Old version of GCC for MIPS default to enabling -fpic
192 # and get confused if it is used on the command line.
193 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
196 # Unfortunately, the gcc argument is -fpic and the cc argument is
197 # -KPIC. We have to try both.
199 send_log "$CC $picflag\n"
200 verbose "$CC $picflag"
201 catch "exec $CC $picflag" exec_output
202 send_log "$exec_output\n"
203 verbose "--" "$exec_output"
204 if { [string match "*illegal option*" $exec_output] \
205 || [string match "*option ignored*" $exec_output] \
206 || [string match "*unrecognized option*" $exec_output] \
207 || [string match "*passed to ld*" $exec_output] } {
208 if [istarget *-*-sunos4*] {
215 verbose "Using $picflag to compile PIC code"
217 # Compile the main program.
218 if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
219 unresolved "shared (non PIC)"
222 # The shared library is composed of two files. First compile them
223 # without using -fpic. That should work on an ELF system,
224 # although it will be less efficient because the dynamic linker
225 # will need to do more relocation work. However, note that not
226 # using -fpic will cause some of the tests to return different
227 # results. Make sure that PLT is used since PLT is expected.
229 if { ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
230 || ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
231 unresolved "shared (non PIC)"
232 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
233 shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
235 # SunOS non PIC shared libraries don't permit some cases of
237 setup_xfail "*-*-sunos4*"
238 setup_xfail "ia64-*-linux*"
239 setup_xfail "alpha*-*-linux*"
240 setup_xfail "powerpc64*-*-*"
241 if { ![istarget hppa*64*-*-linux*] } {
242 setup_xfail "hppa*-*-linux*"
244 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
245 setup_xfail "sparc*-*-linux*"
247 if { [is_elf64 $tmpdir/mainnp.o] } {
248 setup_xfail "x86_64-*-linux*"
250 setup_xfail "x86_64-*-linux-gnux32"
251 setup_xfail "s390x-*-linux*"
252 if [ string match $shared_needs_pic "yes" ] {
253 setup_xfail "arm*-*-linux*"
255 setup_xfail "aarch64*-*-linux*"
256 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
258 # Test ELF shared library relocations with a non-zero load
259 # address for the library. Near as I can tell, the R_*_RELATIVE
260 # relocations for various targets are broken in the case where
261 # the load address is not zero (which is the default).
262 setup_xfail "*-*-sunos4*"
263 setup_xfail "*-*-linux*libc1"
264 setup_xfail "powerpc*-*-linux*"
265 setup_xfail "ia64-*-linux*"
266 setup_xfail "alpha*-*-linux*"
267 setup_xfail "mips*-*-linux*"
268 if { ![istarget hppa*64*-*-linux*] } {
269 setup_xfail "hppa*-*-linux*"
271 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
272 setup_xfail "sparc*-*-linux*"
274 if { [is_elf64 $tmpdir/mainnp.o] } {
275 setup_xfail "x86_64-*-linux*"
277 setup_xfail "x86_64-*-linux-gnux32"
278 setup_xfail "s390x-*-linux*"
279 if [ string match $shared_needs_pic "yes" ] {
280 setup_xfail "arm*-*-linux*"
282 setup_xfail "aarch64*-*-linux*"
283 shared_test shnp "shared (non PIC, load offset)" \
284 mainnp.o sh1np.o sh2np.o shared \
285 "-T $srcdir/$subdir/elf-offset.ld"
288 # Now compile the code using -fpic.
290 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
291 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
294 # SunOS can not compare function pointers correctly
295 if [istarget "*-*-sunos4*"] {
296 shared_test shp "shared" mainnp.o sh1p.o sh2p.o sun4
297 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
298 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
300 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
301 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
302 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
303 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
304 ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
305 ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
310 # Now do the same tests again, but this time compile main.c PIC.
311 if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
312 unresolved "shared (PIC main, non PIC so)"
313 unresolved "shared (PIC main)"
315 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
316 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
317 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
319 # SunOS non PIC shared libraries don't permit some cases of
321 setup_xfail "*-*-sunos4*"
322 setup_xfail "ia64-*-linux*"
323 setup_xfail "alpha*-*-linux*"
324 setup_xfail "powerpc64*-*-*"
325 if { ![istarget hppa*64*-*-linux*] } {
326 setup_xfail "hppa*-*-linux*"
328 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
329 setup_xfail "sparc*-*-linux*"
331 if { [is_elf64 $tmpdir/mainp.o] } {
332 setup_xfail "x86_64-*-linux*"
334 setup_xfail "x86_64-*-linux-gnux32"
335 setup_xfail "s390x-*-linux*"
336 if [ string match $shared_needs_pic "yes" ] {
337 setup_xfail "arm*-*-linux*"
339 setup_xfail "aarch64*-*-linux*"
340 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
343 unresolved "shared (PIC main, non PIC so)"
346 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
347 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
348 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
350 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
353 unresolved "shared (PIC main)"
357 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
358 # Remove the temporary directory.
359 catch "exec rm -rf $tmpdir" exec_status