Commit | Line | Data |
---|---|---|
252b5132 | 1 | # Expect script for ld-shared tests |
2571583a | 2 | # Copyright (C) 1994-2017 Free Software Foundation, Inc. |
252b5132 | 3 | # |
f96b4a7b NC |
4 | # This file is part of the GNU Binutils. |
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify | |
252b5132 | 7 | # it under the terms of the GNU General Public License as published by |
f96b4a7b | 8 | # the Free Software Foundation; either version 3 of the License, or |
252b5132 | 9 | # (at your option) any later version. |
f96b4a7b | 10 | # |
252b5132 RH |
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. | |
f96b4a7b | 15 | # |
252b5132 RH |
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 | |
f96b4a7b NC |
18 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | # MA 02110-1301, USA. | |
252b5132 RH |
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 | ||
d9816402 AM |
28 | # Check to see if the C compiler works |
29 | if { [which $CC] == 0 } { | |
30 | return | |
31 | } | |
252b5132 RH |
32 | |
33 | # This test can only be run on a couple of ELF platforms. | |
34 | # Square bracket expressions seem to confuse istarget. | |
19c7c582 AM |
35 | if { ![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*] \ | |
5940a93c | 41 | && ![istarget i?86-*-gnu*] \ |
5a68afcf | 42 | && ![istarget *-*-nacl*] \ |
ad995491 L |
43 | && ![istarget ia64-*-elf*] \ |
44 | && ![istarget ia64-*-linux*] \ | |
252b5132 RH |
45 | && ![istarget m68k-*-linux*] \ |
46 | && ![istarget mips*-*-irix5*] \ | |
66517a2f | 47 | && ![istarget mips*-*-linux*] \ |
24b01a74 AM |
48 | && ![istarget powerpc*-*-elf*] \ |
49 | && ![istarget powerpc*-*-linux*] \ | |
50 | && ![istarget powerpc*-*-sysv4*] \ | |
252b5132 RH |
51 | && ![istarget sparc*-*-elf] \ |
52 | && ![istarget sparc*-*-solaris2*] \ | |
53 | && ![istarget sparc*-*-sunos4*] \ | |
b33b6e45 | 54 | && ![istarget sparc*-*-linux*] \ |
4f38fc1c | 55 | && ![istarget arm*-*-linux*] \ |
2ffd68ef | 56 | && ![istarget alpha*-*-linux*] \ |
252b5132 | 57 | && ![istarget rs6000*-*-aix*] \ |
9147e853 JJ |
58 | && ![istarget powerpc*-*-aix*] \ |
59 | && ![istarget s390*-*-linux*] \ | |
a06ea964 | 60 | && ![istarget aarch64*-*-linux*] \ |
9147e853 | 61 | && ![istarget x86_64-*-linux*] } { |
252b5132 RH |
62 | return |
63 | } | |
64 | ||
99c262f8 ILT |
65 | if { [istarget *-*-linux*aout*] \ |
66 | || [istarget *-*-linux*oldld*] } { | |
252b5132 RH |
67 | return |
68 | } | |
69 | ||
70 | set tmpdir tmpdir | |
71 | set SHCFLAG "" | |
a9f844b1 | 72 | set shared_needs_pic "no" |
252b5132 RH |
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 | ||
a9f844b1 NC |
104 | if [istarget arm*-*-linux*] { |
105 | # On ARM section anchors can change the symbol pre-emptability for | |
5a68afcf | 106 | # non-PIC shared libraries, causing these tests to fail. Turn section |
a9f844b1 NC |
107 | # anchors off. |
108 | set SHCFLAG "-fno-section-anchors" | |
109 | ||
5a68afcf | 110 | # On targets that have MOVW the compiler will emit relocations which |
a9f844b1 | 111 | # the linker doesn't support when compiling -shared without -fpic. The |
5a68afcf | 112 | # test to find out whether we want to XFAIL the non-PIC tests requires |
a9f844b1 | 113 | # a compile - so we pre-calculate it here. We also note that this can |
0085488a WN |
114 | # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7. |
115 | if [istarget arm*-*-*eabi*] { | |
a9f844b1 NC |
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 | ||
252b5132 RH |
125 | # The test procedure. |
126 | proc shared_test { progname testname main sh1 sh2 dat args } { | |
b765d4e3 | 127 | global CC |
252b5132 RH |
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 | } | |
d9816402 | 142 | if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} { |
252b5132 RH |
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 | } | |
d9816402 | 155 | if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] { |
252b5132 RH |
156 | fail "$testname" |
157 | return | |
158 | } | |
159 | ||
d9816402 AM |
160 | if ![isnative] { |
161 | unsupported $testname | |
162 | return | |
163 | } | |
164 | ||
252b5132 RH |
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 | ||
fb35d3d8 DD |
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 { | |
252b5132 RH |
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 | |
cda77e9e | 227 | # results. Make sure that PLT is used since PLT is expected. |
9d1c54ed L |
228 | global PLT_CFLAGS NOPIE_CFLAGS |
229 | if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o] | |
cda77e9e | 230 | || ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } { |
252b5132 RH |
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*" | |
ad995491 | 238 | setup_xfail "ia64-*-linux*" |
212a6b8e | 239 | setup_xfail "alpha*-*-linux*" |
24b01a74 | 240 | setup_xfail "powerpc64*-*-*" |
c0d48c0b DA |
241 | if { ![istarget hppa*64*-*-linux*] } { |
242 | setup_xfail "hppa*-*-linux*" | |
243 | } | |
4dc570c2 JJ |
244 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } { |
245 | setup_xfail "sparc*-*-linux*" | |
246 | } | |
8b778942 L |
247 | if { [is_elf64 $tmpdir/mainnp.o] } { |
248 | setup_xfail "x86_64-*-linux*" | |
249 | } | |
3c995545 | 250 | setup_xfail "x86_64-*-linux-gnux32" |
4648dfcf | 251 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
252 | if [ string match $shared_needs_pic "yes" ] { |
253 | setup_xfail "arm*-*-linux*" | |
254 | } | |
273dc279 | 255 | setup_xfail "aarch64*-*-linux*" |
252b5132 RH |
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*" | |
b7be1db6 | 263 | setup_xfail "*-*-linux*libc1" |
24b01a74 | 264 | setup_xfail "powerpc*-*-linux*" |
ad995491 | 265 | setup_xfail "ia64-*-linux*" |
212a6b8e | 266 | setup_xfail "alpha*-*-linux*" |
66517a2f | 267 | setup_xfail "mips*-*-linux*" |
c0d48c0b DA |
268 | if { ![istarget hppa*64*-*-linux*] } { |
269 | setup_xfail "hppa*-*-linux*" | |
270 | } | |
4dc570c2 JJ |
271 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } { |
272 | setup_xfail "sparc*-*-linux*" | |
273 | } | |
8b778942 L |
274 | if { [is_elf64 $tmpdir/mainnp.o] } { |
275 | setup_xfail "x86_64-*-linux*" | |
276 | } | |
3c995545 | 277 | setup_xfail "x86_64-*-linux-gnux32" |
8c37241b | 278 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
279 | if [ string match $shared_needs_pic "yes" ] { |
280 | setup_xfail "arm*-*-linux*" | |
281 | } | |
273dc279 | 282 | setup_xfail "aarch64*-*-linux*" |
252b5132 RH |
283 | shared_test shnp "shared (non PIC, load offset)" \ |
284 | mainnp.o sh1np.o sh2np.o shared \ | |
bab55c05 | 285 | "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv" |
252b5132 RH |
286 | } } |
287 | ||
288 | # Now compile the code using -fpic. | |
289 | ||
5a68afcf | 290 | if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o] |
252b5132 RH |
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 | |
66517a2f L |
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 | |
5a68afcf | 303 | shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic" |
f0f077e5 L |
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 | |
252b5132 RH |
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*" | |
ad995491 | 322 | setup_xfail "ia64-*-linux*" |
212a6b8e | 323 | setup_xfail "alpha*-*-linux*" |
24b01a74 | 324 | setup_xfail "powerpc64*-*-*" |
c0d48c0b DA |
325 | if { ![istarget hppa*64*-*-linux*] } { |
326 | setup_xfail "hppa*-*-linux*" | |
327 | } | |
4dc570c2 JJ |
328 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } { |
329 | setup_xfail "sparc*-*-linux*" | |
330 | } | |
8b778942 L |
331 | if { [is_elf64 $tmpdir/mainp.o] } { |
332 | setup_xfail "x86_64-*-linux*" | |
333 | } | |
3c995545 | 334 | setup_xfail "x86_64-*-linux-gnux32" |
4648dfcf | 335 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
336 | if [ string match $shared_needs_pic "yes" ] { |
337 | setup_xfail "arm*-*-linux*" | |
338 | } | |
273dc279 | 339 | setup_xfail "aarch64*-*-linux*" |
252b5132 RH |
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 | } |