Commit | Line | Data |
---|---|---|
252b5132 | 1 | # Expect script for ld-shared tests |
b3adc24a | 2 | # Copyright (C) 1994-2020 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 | 28 | # Check to see if the C compiler works |
44ed8092 | 29 | if { ![check_compiler_available] } { |
d9816402 AM |
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*] \ | |
b33b6e45 | 53 | && ![istarget sparc*-*-linux*] \ |
4f38fc1c | 54 | && ![istarget arm*-*-linux*] \ |
2ffd68ef | 55 | && ![istarget alpha*-*-linux*] \ |
252b5132 | 56 | && ![istarget rs6000*-*-aix*] \ |
9147e853 JJ |
57 | && ![istarget powerpc*-*-aix*] \ |
58 | && ![istarget s390*-*-linux*] \ | |
a06ea964 | 59 | && ![istarget aarch64*-*-linux*] \ |
9147e853 | 60 | && ![istarget x86_64-*-linux*] } { |
252b5132 | 61 | return |
252b5132 RH |
62 | } |
63 | ||
64 | set tmpdir tmpdir | |
65 | set SHCFLAG "" | |
a9f844b1 | 66 | set shared_needs_pic "no" |
252b5132 RH |
67 | |
68 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
69 | ||
70 | # AIX shared libraries do not seem to support useful features, | |
71 | # like overriding the shared library function or letting the | |
72 | # shared library refer to objects defined in the main program. We | |
73 | # avoid testing those features. | |
74 | set SHCFLAG "-DXCOFF_TEST" | |
75 | ||
76 | # The AIX 3.2.5 loader appears to randomly fail when loading | |
77 | # shared libraries from NSF mounted partitions, so we avoid any | |
78 | # potential problems by using a local directory. | |
79 | catch {exec /bin/sh -c "echo $$"} pid | |
80 | set tmpdir /usr/tmp/ld.$pid | |
81 | catch "exec mkdir $tmpdir" exec_status | |
82 | ||
83 | # On AIX, we need to explicitly export the symbols the shared | |
84 | # library is going to provide, and need. | |
85 | set file [open $tmpdir/xcoff.exp w] | |
86 | puts $file shlibvar1 | |
87 | puts $file shlibvar2 | |
88 | puts $file shlib_shlibvar1 | |
89 | puts $file shlib_shlibvar2 | |
90 | puts $file shlib_shlibcall | |
91 | puts $file shlib_shlibcalled | |
92 | puts $file shlib_checkfunptr1 | |
93 | puts $file shlib_getfunptr1 | |
94 | puts $file shlib_check | |
95 | close $file | |
96 | } | |
97 | ||
a9f844b1 NC |
98 | if [istarget arm*-*-linux*] { |
99 | # On ARM section anchors can change the symbol pre-emptability for | |
5a68afcf | 100 | # non-PIC shared libraries, causing these tests to fail. Turn section |
a9f844b1 NC |
101 | # anchors off. |
102 | set SHCFLAG "-fno-section-anchors" | |
103 | ||
5a68afcf | 104 | # On targets that have MOVW the compiler will emit relocations which |
a9f844b1 | 105 | # the linker doesn't support when compiling -shared without -fpic. The |
5a68afcf | 106 | # test to find out whether we want to XFAIL the non-PIC tests requires |
a9f844b1 | 107 | # a compile - so we pre-calculate it here. We also note that this can |
0085488a WN |
108 | # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7. |
109 | if [istarget arm*-*-*eabi*] { | |
a9f844b1 NC |
110 | set file [open $tmpdir/movw-detect.c w] |
111 | puts $file "void foo(void) { __asm (\"movw r0, #0\"); }" | |
112 | close $file | |
113 | if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] { | |
114 | set shared_needs_pic "yes" | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
252b5132 RH |
119 | # The test procedure. |
120 | proc shared_test { progname testname main sh1 sh2 dat args } { | |
b765d4e3 | 121 | global CC |
252b5132 RH |
122 | global srcdir |
123 | global subdir | |
124 | global exec_output | |
125 | global host_triplet | |
126 | global tmpdir | |
127 | ||
128 | if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" } | |
129 | ||
130 | # Build the shared library. | |
131 | # On AIX, we need to use an export file. | |
132 | set shared -shared | |
133 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
134 | set shared "-bM:SRE -bE:$tmpdir/xcoff.exp" | |
135 | } | |
af2c48d8 L |
136 | if { [is_elf_format] && [check_shared_lib_support] } { |
137 | append shared " -Wl,-z,notext" | |
138 | } | |
d9816402 | 139 | if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} { |
252b5132 RH |
140 | fail "$testname" |
141 | return | |
142 | } | |
143 | ||
144 | # Link against the shared library. Use -rpath so that the | |
145 | # dynamic linker can locate the shared library at runtime. | |
146 | # On AIX, we must include /lib in -rpath, as otherwise the loader | |
147 | # can not find -lc. | |
148 | set rpath $tmpdir | |
149 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
150 | set rpath /lib:$tmpdir | |
151 | } | |
d9816402 | 152 | if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] { |
252b5132 RH |
153 | fail "$testname" |
154 | return | |
155 | } | |
156 | ||
d9816402 AM |
157 | if ![isnative] { |
158 | unsupported $testname | |
159 | return | |
160 | } | |
161 | ||
252b5132 RH |
162 | # Run the resulting program |
163 | send_log "$tmpdir/$progname >$tmpdir/$progname.out\n" | |
164 | verbose "$tmpdir/$progname >$tmpdir/$progname.out" | |
165 | catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output | |
166 | if ![string match "" $exec_output] then { | |
167 | send_log "$exec_output\n" | |
168 | verbose "$exec_output" | |
169 | fail "$testname" | |
170 | return | |
171 | } | |
172 | ||
173 | send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n" | |
174 | verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" | |
175 | catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output | |
176 | set exec_output [prune_warnings $exec_output] | |
177 | ||
178 | if {![string match "" $exec_output]} then { | |
179 | send_log "$exec_output\n" | |
180 | verbose "$exec_output" | |
181 | fail "$testname" | |
182 | return | |
183 | } | |
184 | ||
185 | pass "$testname" | |
186 | } | |
187 | ||
fb35d3d8 DD |
188 | # Old version of GCC for MIPS default to enabling -fpic |
189 | # and get confused if it is used on the command line. | |
190 | if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then { | |
252b5132 RH |
191 | set picflag "" |
192 | } else { | |
193 | # Unfortunately, the gcc argument is -fpic and the cc argument is | |
194 | # -KPIC. We have to try both. | |
195 | set picflag "-fpic" | |
196 | send_log "$CC $picflag\n" | |
197 | verbose "$CC $picflag" | |
198 | catch "exec $CC $picflag" exec_output | |
199 | send_log "$exec_output\n" | |
200 | verbose "--" "$exec_output" | |
201 | if { [string match "*illegal option*" $exec_output] \ | |
202 | || [string match "*option ignored*" $exec_output] \ | |
203 | || [string match "*unrecognized option*" $exec_output] \ | |
204 | || [string match "*passed to ld*" $exec_output] } { | |
c9098af4 | 205 | set picflag "-KPIC" |
252b5132 RH |
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 | 220 | # results. Make sure that PLT is used since PLT is expected. |
9d1c54ed L |
221 | global PLT_CFLAGS NOPIE_CFLAGS |
222 | if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o] | |
cda77e9e | 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 { | |
a5d21497 RO |
228 | # Solaris defaults to -z text. |
229 | setup_xfail "*-*-solaris2*" | |
ad995491 | 230 | setup_xfail "ia64-*-linux*" |
212a6b8e | 231 | setup_xfail "alpha*-*-linux*" |
24b01a74 | 232 | setup_xfail "powerpc64*-*-*" |
c0d48c0b DA |
233 | if { ![istarget hppa*64*-*-linux*] } { |
234 | setup_xfail "hppa*-*-linux*" | |
235 | } | |
4dc570c2 JJ |
236 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } { |
237 | setup_xfail "sparc*-*-linux*" | |
238 | } | |
8b778942 L |
239 | if { [is_elf64 $tmpdir/mainnp.o] } { |
240 | setup_xfail "x86_64-*-linux*" | |
241 | } | |
3c995545 | 242 | setup_xfail "x86_64-*-linux-gnux32" |
4648dfcf | 243 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
244 | if [ string match $shared_needs_pic "yes" ] { |
245 | setup_xfail "arm*-*-linux*" | |
246 | } | |
273dc279 | 247 | setup_xfail "aarch64*-*-linux*" |
252b5132 RH |
248 | shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared |
249 | ||
250 | # Test ELF shared library relocations with a non-zero load | |
251 | # address for the library. Near as I can tell, the R_*_RELATIVE | |
252 | # relocations for various targets are broken in the case where | |
253 | # the load address is not zero (which is the default). | |
b7be1db6 | 254 | setup_xfail "*-*-linux*libc1" |
24b01a74 | 255 | setup_xfail "powerpc*-*-linux*" |
ad995491 | 256 | setup_xfail "ia64-*-linux*" |
212a6b8e | 257 | setup_xfail "alpha*-*-linux*" |
66517a2f | 258 | setup_xfail "mips*-*-linux*" |
c0d48c0b DA |
259 | if { ![istarget hppa*64*-*-linux*] } { |
260 | setup_xfail "hppa*-*-linux*" | |
261 | } | |
4dc570c2 JJ |
262 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } { |
263 | setup_xfail "sparc*-*-linux*" | |
264 | } | |
8b778942 L |
265 | if { [is_elf64 $tmpdir/mainnp.o] } { |
266 | setup_xfail "x86_64-*-linux*" | |
267 | } | |
3c995545 | 268 | setup_xfail "x86_64-*-linux-gnux32" |
8c37241b | 269 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
270 | if [ string match $shared_needs_pic "yes" ] { |
271 | setup_xfail "arm*-*-linux*" | |
272 | } | |
273dc279 | 273 | setup_xfail "aarch64*-*-linux*" |
a5d21497 RO |
274 | # Solaris defaults to -z text. |
275 | setup_xfail "*-*-solaris2*" | |
252b5132 RH |
276 | shared_test shnp "shared (non PIC, load offset)" \ |
277 | mainnp.o sh1np.o sh2np.o shared \ | |
bab55c05 | 278 | "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv" |
252b5132 RH |
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 { | |
c9098af4 | 287 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { |
252b5132 RH |
288 | shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff |
289 | } else { | |
290 | shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared | |
66517a2f L |
291 | ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o |
292 | ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o | |
5a68afcf | 293 | shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic" |
f0f077e5 L |
294 | ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o |
295 | ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o | |
c9098af4 | 296 | } |
252b5132 RH |
297 | } |
298 | } | |
299 | ||
300 | # Now do the same tests again, but this time compile main.c PIC. | |
301 | if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] { | |
302 | unresolved "shared (PIC main, non PIC so)" | |
303 | unresolved "shared (PIC main)" | |
304 | } else { | |
305 | if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } { | |
306 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
307 | shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff | |
308 | } else { | |
a5d21497 RO |
309 | # Solaris defaults to -z text. |
310 | setup_xfail "*-*-solaris2*" | |
ad995491 | 311 | setup_xfail "ia64-*-linux*" |
212a6b8e | 312 | setup_xfail "alpha*-*-linux*" |
24b01a74 | 313 | setup_xfail "powerpc64*-*-*" |
c0d48c0b DA |
314 | if { ![istarget hppa*64*-*-linux*] } { |
315 | setup_xfail "hppa*-*-linux*" | |
316 | } | |
4dc570c2 JJ |
317 | if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } { |
318 | setup_xfail "sparc*-*-linux*" | |
319 | } | |
8b778942 L |
320 | if { [is_elf64 $tmpdir/mainp.o] } { |
321 | setup_xfail "x86_64-*-linux*" | |
322 | } | |
3c995545 | 323 | setup_xfail "x86_64-*-linux-gnux32" |
4648dfcf | 324 | setup_xfail "s390x-*-linux*" |
a9f844b1 NC |
325 | if [ string match $shared_needs_pic "yes" ] { |
326 | setup_xfail "arm*-*-linux*" | |
327 | } | |
273dc279 | 328 | setup_xfail "aarch64*-*-linux*" |
252b5132 RH |
329 | shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared |
330 | } | |
331 | } else { | |
332 | unresolved "shared (PIC main, non PIC so)" | |
333 | } | |
334 | ||
335 | if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } { | |
336 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
337 | shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff | |
338 | } else { | |
339 | shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared | |
340 | } | |
341 | } else { | |
342 | unresolved "shared (PIC main)" | |
343 | } | |
344 | } | |
345 | ||
346 | if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } { | |
347 | # Remove the temporary directory. | |
348 | catch "exec rm -rf $tmpdir" exec_status | |
349 | } |