6d7a0d6c92291c65a17df6945468412a3ba040a3
[deliverable/binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
1 # Expect script for ld-shared tests
2 # Copyright (C) 1994-2017 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
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.
10 #
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.
15 #
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,
19 # MA 02110-1301, USA.
20 #
21 # Written by Ian Lance Taylor (ian@cygnus.com)
22 #
23
24 # Make sure that ld can generate ELF shared libraries.
25 # Note that linking against ELF shared libraries is tested by the
26 # bootstrap test.
27
28 # Check to see if the C compiler works
29 if { [which $CC] == 0 } {
30 return
31 }
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_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_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
156 fail "$testname"
157 return
158 }
159
160 if ![isnative] {
161 unsupported $testname
162 return
163 }
164
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"
172 fail "$testname"
173 return
174 }
175
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]
180
181 if {![string match "" $exec_output]} then {
182 send_log "$exec_output\n"
183 verbose "$exec_output"
184 fail "$testname"
185 return
186 }
187
188 pass "$testname"
189 }
190
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 {
194 set picflag ""
195 } else {
196 # Unfortunately, the gcc argument is -fpic and the cc argument is
197 # -KPIC. We have to try both.
198 set picflag "-fpic"
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*] {
209 set picflag "-pic"
210 } else {
211 set picflag "-KPIC"
212 }
213 }
214 }
215 verbose "Using $picflag to compile PIC code"
216
217 # Compile the main program.
218 if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
219 unresolved "shared (non PIC)"
220 unresolved "shared"
221 } else {
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.
228 global PLT_CFLAGS NOPIE_CFLAGS
229 if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_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
234 } else {
235 # SunOS non PIC shared libraries don't permit some cases of
236 # overriding.
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*"
243 }
244 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
245 setup_xfail "sparc*-*-linux*"
246 }
247 if { [is_elf64 $tmpdir/mainnp.o] } {
248 setup_xfail "x86_64-*-linux*"
249 }
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*"
254 }
255 setup_xfail "aarch64*-*-linux*"
256 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
257
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*"
270 }
271 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
272 setup_xfail "sparc*-*-linux*"
273 }
274 if { [is_elf64 $tmpdir/mainnp.o] } {
275 setup_xfail "x86_64-*-linux*"
276 }
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*"
281 }
282 setup_xfail "aarch64*-*-linux*"
283 shared_test shnp "shared (non PIC, load offset)" \
284 mainnp.o sh1np.o sh2np.o shared \
285 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
286 } }
287
288 # Now compile the code using -fpic.
289
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] } {
292 unresolved "shared"
293 } else {
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
299 } else {
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
306 } }
307 }
308 }
309
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)"
314 } else {
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
318 } else {
319 # SunOS non PIC shared libraries don't permit some cases of
320 # overriding.
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*"
327 }
328 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
329 setup_xfail "sparc*-*-linux*"
330 }
331 if { [is_elf64 $tmpdir/mainp.o] } {
332 setup_xfail "x86_64-*-linux*"
333 }
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*"
338 }
339 setup_xfail "aarch64*-*-linux*"
340 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
341 }
342 } else {
343 unresolved "shared (PIC main, non PIC so)"
344 }
345
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
349 } else {
350 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
351 }
352 } else {
353 unresolved "shared (PIC main)"
354 }
355 }
356
357 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
358 # Remove the temporary directory.
359 catch "exec rm -rf $tmpdir" exec_status
360 }
This page took 0.03732 seconds and 3 git commands to generate.