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