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