]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/testsuite/ld-shared/shared.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / testsuite / ld-shared / shared.exp
CommitLineData
252b5132 1# Expect script for ld-shared tests
d87bef3a 2# Copyright (C) 1994-2023 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 29if { ![check_compiler_available] } {
d9816402
AM
30 return
31}
252b5132 32
8bfb6941
CC
33# Skip targets where -shared is not supported
34if { ![check_shared_lib_support] } {
35 return
36}
37
252b5132
RH
38# This test can only be run on a couple of ELF platforms.
39# Square bracket expressions seem to confuse istarget.
19c7c582
AM
40if { ![istarget hppa*64*-*-hpux*] \
41 && ![istarget hppa*-*-linux*] \
42 && ![istarget i?86-*-sysv4*] \
ad995491
L
43 && ![istarget i?86-*-unixware] \
44 && ![istarget i?86-*-elf*] \
45 && ![istarget i?86-*-linux*] \
5940a93c 46 && ![istarget i?86-*-gnu*] \
5a68afcf 47 && ![istarget *-*-nacl*] \
ad995491
L
48 && ![istarget ia64-*-elf*] \
49 && ![istarget ia64-*-linux*] \
252b5132
RH
50 && ![istarget m68k-*-linux*] \
51 && ![istarget mips*-*-irix5*] \
66517a2f 52 && ![istarget mips*-*-linux*] \
24b01a74
AM
53 && ![istarget powerpc*-*-elf*] \
54 && ![istarget powerpc*-*-linux*] \
55 && ![istarget powerpc*-*-sysv4*] \
252b5132
RH
56 && ![istarget sparc*-*-elf] \
57 && ![istarget sparc*-*-solaris2*] \
b33b6e45 58 && ![istarget sparc*-*-linux*] \
4f38fc1c 59 && ![istarget arm*-*-linux*] \
2ffd68ef 60 && ![istarget alpha*-*-linux*] \
74044dc8 61 && ![is_xcoff_format] \
9147e853 62 && ![istarget s390*-*-linux*] \
a06ea964 63 && ![istarget aarch64*-*-linux*] \
9147e853 64 && ![istarget x86_64-*-linux*] } {
252b5132 65 return
252b5132
RH
66}
67
68set tmpdir tmpdir
69set SHCFLAG ""
a9f844b1 70set shared_needs_pic "no"
252b5132 71
42afa120 72# Disable all sanitizers.
ad77db1c
AM
73set old_CFLAGS "$CFLAGS_FOR_TARGET"
74append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS"
42afa120 75
74044dc8
CC
76if { [is_xcoff_format] } {
77 # Not all the useful features are available with AIX shared
78 # libraries by default.
79 # We can manage to simulate some of them with export/import
80 # files but the overriding of shared library functions or
81 # variables by the main program doesn't seem possible.
82 # We avoid testing those features.
252b5132
RH
83 set SHCFLAG "-DXCOFF_TEST"
84
74044dc8
CC
85 # In order to avoid listing every symbols in an export file,
86 # the export will be done with -bexpall flag.
87 # However for imports, we must create the import file.
88 set file [open $tmpdir/xcoff-shared.imp w]
89 puts $file "#! ."
90 puts $file mainvar
91 puts $file main_called
252b5132
RH
92 close $file
93}
94
a9f844b1
NC
95if [istarget arm*-*-linux*] {
96 # On ARM section anchors can change the symbol pre-emptability for
5a68afcf 97 # non-PIC shared libraries, causing these tests to fail. Turn section
a9f844b1
NC
98 # anchors off.
99 set SHCFLAG "-fno-section-anchors"
100
5a68afcf 101 # On targets that have MOVW the compiler will emit relocations which
a9f844b1 102 # the linker doesn't support when compiling -shared without -fpic. The
5a68afcf 103 # test to find out whether we want to XFAIL the non-PIC tests requires
a9f844b1 104 # a compile - so we pre-calculate it here. We also note that this can
0085488a
WN
105 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
106 if [istarget arm*-*-*eabi*] {
a9f844b1
NC
107 set file [open $tmpdir/movw-detect.c w]
108 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
109 close $file
ad77db1c 110 if [run_host_cmd_yesno "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
a9f844b1
NC
111 set shared_needs_pic "yes"
112 }
113 }
114}
115
252b5132
RH
116# The test procedure.
117proc shared_test { progname testname main sh1 sh2 dat args } {
ad77db1c 118 global CC_FOR_TARGET
252b5132
RH
119 global srcdir
120 global subdir
121 global exec_output
122 global host_triplet
123 global tmpdir
124
125 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
126
127 # Build the shared library.
252b5132 128 set shared -shared
74044dc8
CC
129 if { [is_xcoff_format] } {
130 # On AIX, setup imports and exports.
131 append shared " -Wl,-bexpall -Wl,-bI:$tmpdir/xcoff-shared.imp"
252b5132 132 }
af2c48d8
L
133 if { [is_elf_format] && [check_shared_lib_support] } {
134 append shared " -Wl,-z,notext"
135 }
ad77db1c 136 if {![ld_link $CC_FOR_TARGET $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
252b5132
RH
137 fail "$testname"
138 return
139 }
140
141 # Link against the shared library. Use -rpath so that the
142 # dynamic linker can locate the shared library at runtime.
143 # On AIX, we must include /lib in -rpath, as otherwise the loader
144 # can not find -lc.
145 set rpath $tmpdir
74044dc8
CC
146 set exportflag ""
147 if { [is_xcoff_format] } {
252b5132 148 set rpath /lib:$tmpdir
74044dc8 149 set exportflag " -Wl,-bexpall"
252b5132 150 }
74044dc8 151 if ![ld_link $CC_FOR_TARGET $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so $exportflag"] {
252b5132
RH
152 fail "$testname"
153 return
154 }
155
d9816402
AM
156 if ![isnative] {
157 unsupported $testname
158 return
159 }
160
252b5132
RH
161 # Run the resulting program
162 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
163 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
164 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
165 if ![string match "" $exec_output] then {
166 send_log "$exec_output\n"
167 verbose "$exec_output"
168 fail "$testname"
169 return
170 }
171
172 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
173 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
174 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
175 set exec_output [prune_warnings $exec_output]
176
177 if {![string match "" $exec_output]} then {
178 send_log "$exec_output\n"
179 verbose "$exec_output"
180 fail "$testname"
181 return
182 }
183
184 pass "$testname"
185}
186
fb35d3d8
DD
187# Old version of GCC for MIPS default to enabling -fpic
188# and get confused if it is used on the command line.
189if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
252b5132
RH
190 set picflag ""
191} else {
192 # Unfortunately, the gcc argument is -fpic and the cc argument is
193 # -KPIC. We have to try both.
194 set picflag "-fpic"
ad77db1c
AM
195 send_log "$CC_FOR_TARGET $picflag\n"
196 verbose "$CC_FOR_TARGET $picflag"
197 catch "exec $CC_FOR_TARGET $picflag" exec_output
252b5132
RH
198 send_log "$exec_output\n"
199 verbose "--" "$exec_output"
200 if { [string match "*illegal option*" $exec_output] \
201 || [string match "*option ignored*" $exec_output] \
202 || [string match "*unrecognized option*" $exec_output] \
203 || [string match "*passed to ld*" $exec_output] } {
c9098af4 204 set picflag "-KPIC"
252b5132
RH
205 }
206}
207verbose "Using $picflag to compile PIC code"
208
209# Compile the main program.
669d5115 210if ![ld_compile "$CC_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
5b1f6c95
AM
211 unsupported "shared (non PIC)"
212 unsupported "shared"
252b5132
RH
213} else {
214 # The shared library is composed of two files. First compile them
215 # without using -fpic. That should work on an ELF system,
216 # although it will be less efficient because the dynamic linker
217 # will need to do more relocation work. However, note that not
218 # using -fpic will cause some of the tests to return different
cda77e9e 219 # results. Make sure that PLT is used since PLT is expected.
9d1c54ed 220 global PLT_CFLAGS NOPIE_CFLAGS
669d5115
AM
221 if { ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
222 || ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
5b1f6c95 223 unsupported "shared (non PIC)"
74044dc8 224 } else { if { [is_xcoff_format] } {
252b5132
RH
225 shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
226 } else {
a5d21497
RO
227 # Solaris defaults to -z text.
228 setup_xfail "*-*-solaris2*"
ad995491 229 setup_xfail "ia64-*-linux*"
212a6b8e 230 setup_xfail "alpha*-*-linux*"
24b01a74 231 setup_xfail "powerpc64*-*-*"
c0d48c0b
DA
232 if { ![istarget hppa*64*-*-linux*] } {
233 setup_xfail "hppa*-*-linux*"
234 }
4dc570c2
JJ
235 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
236 setup_xfail "sparc*-*-linux*"
237 }
8b778942
L
238 if { [is_elf64 $tmpdir/mainnp.o] } {
239 setup_xfail "x86_64-*-linux*"
240 }
3c995545 241 setup_xfail "x86_64-*-linux-gnux32"
4648dfcf 242 setup_xfail "s390x-*-linux*"
a9f844b1
NC
243 if [ string match $shared_needs_pic "yes" ] {
244 setup_xfail "arm*-*-linux*"
245 }
273dc279 246 setup_xfail "aarch64*-*-linux*"
252b5132
RH
247 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
248
249 # Test ELF shared library relocations with a non-zero load
250 # address for the library. Near as I can tell, the R_*_RELATIVE
251 # relocations for various targets are broken in the case where
252 # the load address is not zero (which is the default).
b7be1db6 253 setup_xfail "*-*-linux*libc1"
24b01a74 254 setup_xfail "powerpc*-*-linux*"
ad995491 255 setup_xfail "ia64-*-linux*"
212a6b8e 256 setup_xfail "alpha*-*-linux*"
66517a2f 257 setup_xfail "mips*-*-linux*"
c0d48c0b
DA
258 if { ![istarget hppa*64*-*-linux*] } {
259 setup_xfail "hppa*-*-linux*"
260 }
4dc570c2
JJ
261 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
262 setup_xfail "sparc*-*-linux*"
263 }
8b778942
L
264 if { [is_elf64 $tmpdir/mainnp.o] } {
265 setup_xfail "x86_64-*-linux*"
266 }
3c995545 267 setup_xfail "x86_64-*-linux-gnux32"
8c37241b 268 setup_xfail "s390x-*-linux*"
a9f844b1
NC
269 if [ string match $shared_needs_pic "yes" ] {
270 setup_xfail "arm*-*-linux*"
271 }
273dc279 272 setup_xfail "aarch64*-*-linux*"
a5d21497
RO
273 # Solaris defaults to -z text.
274 setup_xfail "*-*-solaris2*"
252b5132
RH
275 shared_test shnp "shared (non PIC, load offset)" \
276 mainnp.o sh1np.o sh2np.o shared \
bab55c05 277 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
252b5132
RH
278 } }
279
280 # Now compile the code using -fpic.
281
669d5115
AM
282 if { ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
283 || ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
5b1f6c95 284 unsupported "shared"
252b5132 285 } else {
74044dc8 286 if { [is_xcoff_format] } {
252b5132
RH
287 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
288 } else {
289 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
74044dc8
CC
290 ld_compile "$CC_FOR_TARGET -DSYMBOLIC_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
291 ld_compile "$CC_FOR_TARGET -DSYMBOLIC_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
5a68afcf 292 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
669d5115
AM
293 ld_compile "$CC_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
294 ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
c9098af4 295 }
252b5132
RH
296 }
297}
298
299# Now do the same tests again, but this time compile main.c PIC.
669d5115 300if ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
5b1f6c95
AM
301 unsupported "shared (PIC main, non PIC so)"
302 unsupported "shared (PIC main)"
252b5132
RH
303} else {
304 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
74044dc8 305 if { [is_xcoff_format] } {
252b5132
RH
306 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
307 } else {
a5d21497
RO
308 # Solaris defaults to -z text.
309 setup_xfail "*-*-solaris2*"
ad995491 310 setup_xfail "ia64-*-linux*"
212a6b8e 311 setup_xfail "alpha*-*-linux*"
24b01a74 312 setup_xfail "powerpc64*-*-*"
c0d48c0b
DA
313 if { ![istarget hppa*64*-*-linux*] } {
314 setup_xfail "hppa*-*-linux*"
315 }
4dc570c2
JJ
316 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
317 setup_xfail "sparc*-*-linux*"
318 }
8b778942
L
319 if { [is_elf64 $tmpdir/mainp.o] } {
320 setup_xfail "x86_64-*-linux*"
321 }
3c995545 322 setup_xfail "x86_64-*-linux-gnux32"
4648dfcf 323 setup_xfail "s390x-*-linux*"
a9f844b1
NC
324 if [ string match $shared_needs_pic "yes" ] {
325 setup_xfail "arm*-*-linux*"
326 }
273dc279 327 setup_xfail "aarch64*-*-linux*"
252b5132
RH
328 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
329 }
330 } else {
5b1f6c95 331 unsupported "shared (PIC main, non PIC so)"
252b5132
RH
332 }
333
334 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
74044dc8 335 if { [is_xcoff_format] } {
252b5132
RH
336 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
337 } else {
338 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
339 }
340 } else {
5b1f6c95 341 unsupported "shared (PIC main)"
252b5132
RH
342 }
343}
344
ad77db1c 345set CFLAGS_FOR_TARGET "$old_CFLAGS"