1 # Expect script
for ld-shared tests
2 # Copyright
(C
) 1994-2025 Free Software Foundation
, Inc.
4 # This file is part of the GNU Binutils.
6 # This
program is free software
; you can redistribute it and
/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation
; either version
3 of the License
, or
9 #
(at your option
) any later version.
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.
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
18 # Foundation
, Inc.
, 51 Franklin Street
- Fifth Floor
, Boston
,
21 # Written by Ian Lance Taylor
(ian@cygnus.com
)
24 # Make sure that
ld can generate ELF shared libraries.
25 # Note that linking against ELF shared libraries is tested by the
28 # Check to see
if the C compiler works
29 if { ![check_compiler_available
] } {
33 # Skip targets where
-shared is not supported
34 if { ![check_shared_lib_support
] } {
38 # This test can only be run
on a couple of ELF platforms.
39 # Square bracket expressions seem to confuse istarget.
40 if { ![istarget hppa
*64*-*-hpux
*] \
41 && ![istarget hppa
*-*-linux
*] \
42 && ![istarget i?
86-*-elf
*] \
43 && ![istarget i?
86-*-linux
*] \
44 && ![istarget i?
86-*-gnu
*] \
45 && ![istarget
*-*-nacl
*] \
46 && ![istarget ia64
-*-elf
*] \
47 && ![istarget ia64
-*-linux
*] \
48 && ![istarget m68k
-*-linux
*] \
49 && ![istarget mips
*-*-irix5
*] \
50 && ![istarget mips
*-*-linux
*] \
51 && ![istarget powerpc
*-*-elf
*] \
52 && ![istarget powerpc
*-*-linux
*] \
53 && ![istarget powerpc
*-*-sysv4
*] \
54 && ![istarget sparc
*-*-elf
] \
55 && ![istarget sparc
*-*-solaris2
*] \
56 && ![istarget sparc
*-*-linux
*] \
57 && ![istarget arm
*-*-linux
*] \
58 && ![istarget alpha
*-*-linux
*] \
59 && ![is_xcoff_format
] \
60 && ![istarget s390
*-*-linux
*] \
61 && ![istarget aarch64
*-*-linux
*] \
62 && ![istarget x86_64
-*-linux
*] } {
68 set shared_needs_pic
"no"
70 # Disable all sanitizers.
71 set old_CFLAGS
"$CFLAGS_FOR_TARGET"
72 append CFLAGS_FOR_TARGET
" $NOSANITIZE_CFLAGS"
74 if { [is_xcoff_format
] } {
75 # Not all the useful features are available with AIX shared
76 # libraries by default.
77 # We can manage to simulate some of them with
export/import
78 # files but the overriding of shared library functions or
79 # variables by the main
program doesn
't seem possible.
80 # We avoid testing those features.
81 set SHCFLAG "-DXCOFF_TEST"
83 # In order to avoid listing every symbols in an export file,
84 # the export will be done with -bexpall flag.
85 # However for imports, we must create the import file.
86 set file [open $tmpdir/xcoff-shared.imp w]
89 puts $file main_called
93 if [istarget arm*-*-linux*] {
94 # On ARM section anchors can change the symbol pre-emptability for
95 # non-PIC shared libraries, causing these tests to fail. Turn section
97 set SHCFLAG "-fno-section-anchors"
99 # On targets that have MOVW the compiler will emit relocations which
100 # the linker doesn't support when compiling
-shared without
-fpic. The
101 # test to find out whether we want to XFAIL the non
-PIC tests requires
102 # a
compile - so we pre
-calculate it here. We also note that this can
103 # only affect arm
*-*-*eabi
* targets as the old ABI doesn
't support v7.
104 if [istarget arm*-*-*eabi*] {
105 set file [open $tmpdir/movw-detect.c w]
106 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
108 if [run_host_cmd_yesno "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
109 set shared_needs_pic "yes"
114 # The test procedure.
115 proc shared_test { progname testname main sh1 sh2 dat args } {
123 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
125 # Build the shared library.
127 if { [is_xcoff_format] } {
128 # On AIX, setup imports and exports.
129 append shared " -Wl,-bexpall -Wl,-bI:$tmpdir/xcoff-shared.imp"
131 if { [is_elf_format] && [check_shared_lib_support] } {
132 append shared " -Wl,-z,notext"
134 if {![ld_link $CC_FOR_TARGET $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
139 # Link against the shared library. Use -rpath so that the
140 # dynamic linker can locate the shared library at runtime.
141 # On AIX, we must include /lib in -rpath, as otherwise the loader
145 if { [is_xcoff_format] } {
146 set rpath /lib:$tmpdir
147 set exportflag " -Wl,-bexpall"
149 if ![ld_link $CC_FOR_TARGET $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so $exportflag"] {
155 unsupported $testname
159 # Run the resulting program
160 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
161 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
162 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
163 if ![string match "" $exec_output] then {
164 send_log "$exec_output\n"
165 verbose "$exec_output"
170 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
171 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
172 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
173 set exec_output [prune_warnings $exec_output]
175 if {![string match "" $exec_output]} then {
176 send_log "$exec_output\n"
177 verbose "$exec_output"
185 # Old version of GCC for MIPS default to enabling -fpic
186 # and get confused if it is used on the command line.
187 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
190 # Unfortunately, the gcc argument is -fpic and the cc argument is
191 # -KPIC. We have to try both.
193 send_log "$CC_FOR_TARGET $picflag\n"
194 verbose "$CC_FOR_TARGET $picflag"
195 catch "exec $CC_FOR_TARGET $picflag" exec_output
196 send_log "$exec_output\n"
197 verbose "--" "$exec_output"
198 if { [string match "*illegal option*" $exec_output] \
199 || [string match "*option ignored*" $exec_output] \
200 || [string match "*unrecognized option*" $exec_output] \
201 || [string match "*passed to ld*" $exec_output] } {
205 verbose "Using $picflag to compile PIC code"
207 # Compile the main program.
208 if ![ld_compile "$CC_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
209 unsupported "shared (non PIC)"
212 # The shared library is composed of two files. First compile them
213 # without using -fpic. That should work on an ELF system,
214 # although it will be less efficient because the dynamic linker
215 # will need to do more relocation work. However, note that not
216 # using -fpic will cause some of the tests to return different
217 # results. Make sure that PLT is used since PLT is expected.
218 global PLT_CFLAGS NOPIE_CFLAGS
219 if { ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
220 || ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
221 unsupported "shared (non PIC)"
222 } else { if { [is_xcoff_format] } {
223 shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
225 # Solaris defaults to -z text.
226 setup_xfail "*-*-solaris2*"
227 setup_xfail "ia64-*-linux*"
228 setup_xfail "alpha*-*-linux*"
229 setup_xfail "powerpc-*-linux-musl"
230 setup_xfail "powerpc64*-*-*"
231 if { ![istarget hppa*64*-*-linux*] } {
232 setup_xfail "hppa*-*-linux*"
234 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
235 setup_xfail "sparc*-*-linux*"
237 if { [is_elf64 $tmpdir/mainnp.o] } {
238 setup_xfail "x86_64-*-linux*"
240 setup_xfail "x86_64-*-linux-gnux32"
241 setup_xfail "s390x-*-linux*"
242 if [ string match $shared_needs_pic "yes" ] {
243 setup_xfail "arm*-*-linux*"
245 setup_xfail "aarch64*-*-linux*"
246 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
248 # Test ELF shared library relocations with a non-zero load
249 # address for the library. Near as I can tell, the R_*_RELATIVE
250 # relocations for various targets are broken in the case where
251 # the load address is not zero (which is the default).
252 setup_xfail "*-*-linux*libc1"
253 setup_xfail "powerpc*-*-linux*"
254 setup_xfail "ia64-*-linux*"
255 setup_xfail "alpha*-*-linux*"
256 setup_xfail "mips*-*-linux*"
257 if { ![istarget hppa*64*-*-linux*] } {
258 setup_xfail "hppa*-*-linux*"
260 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
261 setup_xfail "sparc*-*-linux*"
263 if { [is_elf64 $tmpdir/mainnp.o] } {
264 setup_xfail "x86_64-*-linux*"
266 setup_xfail "x86_64-*-linux-gnux32"
267 setup_xfail "s390x-*-linux*"
268 if [ string match $shared_needs_pic "yes" ] {
269 setup_xfail "arm*-*-linux*"
271 setup_xfail "aarch64*-*-linux*"
272 # Solaris defaults to -z text.
273 setup_xfail "*-*-solaris2*"
274 shared_test shnp "shared (non PIC, load offset)" \
275 mainnp.o sh1np.o sh2np.o shared \
276 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
279 # Now compile the code using -fpic.
281 if { ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
282 || ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
285 if { [is_xcoff_format] } {
286 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
288 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
289 ld_compile "$CC_FOR_TARGET -DSYMBOLIC_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
290 ld_compile "$CC_FOR_TARGET -DSYMBOLIC_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
291 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
292 ld_compile "$CC_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
293 ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
298 # Now do the same tests again, but this time compile main.c PIC.
299 if ![ld_compile "$CC_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
300 unsupported "shared (PIC main, non PIC so)"
301 unsupported "shared (PIC main)"
303 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
304 if { [is_xcoff_format] } {
305 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
307 # Solaris defaults to -z text.
308 setup_xfail "*-*-solaris2*"
309 setup_xfail "ia64-*-linux*"
310 setup_xfail "alpha*-*-linux*"
311 setup_xfail "powerpc-*-linux-musl"
312 setup_xfail "powerpc64*-*-*"
313 if { ![istarget hppa*64*-*-linux*] } {
314 setup_xfail "hppa*-*-linux*"
316 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
317 setup_xfail "sparc*-*-linux*"
319 if { [is_elf64 $tmpdir/mainp.o] } {
320 setup_xfail "x86_64-*-linux*"
322 setup_xfail "x86_64-*-linux-gnux32"
323 setup_xfail "s390x-*-linux*"
324 if [ string match $shared_needs_pic "yes" ] {
325 setup_xfail "arm*-*-linux*"
327 setup_xfail "aarch64*-*-linux*"
328 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o shared
331 unsupported "shared (PIC main, non PIC so)"
334 if { [file exists $tmpdir/sh1p.o ] && [ file exists $tmpdir/sh2p.o ] } {
335 if { [is_xcoff_format] } {
336 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o xcoff
338 shared_test shmpp "shared (PIC main)" mainp.o sh1p.o sh2p.o shared
341 unsupported "shared (PIC main)"
345 set CFLAGS_FOR_TARGET "$old_CFLAGS"