]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - 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
1 # Expect script for ld-shared tests
2 # Copyright (C) 1994-2021 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
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.
10 #
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.
15 #
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,
19 # MA 02110-1301, USA.
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 # Check to see if the C compiler works
29 if { ![check_compiler_available] } {
30 return
31 }
32
33 # This test can only be run on a couple of ELF platforms.
34 # Square bracket expressions seem to confuse istarget.
35 if { ![istarget hppa*64*-*-hpux*] \
36 && ![istarget hppa*-*-linux*] \
37 && ![istarget i?86-*-sysv4*] \
38 && ![istarget i?86-*-unixware] \
39 && ![istarget i?86-*-elf*] \
40 && ![istarget i?86-*-linux*] \
41 && ![istarget i?86-*-gnu*] \
42 && ![istarget *-*-nacl*] \
43 && ![istarget ia64-*-elf*] \
44 && ![istarget ia64-*-linux*] \
45 && ![istarget m68k-*-linux*] \
46 && ![istarget mips*-*-irix5*] \
47 && ![istarget mips*-*-linux*] \
48 && ![istarget powerpc*-*-elf*] \
49 && ![istarget powerpc*-*-linux*] \
50 && ![istarget powerpc*-*-sysv4*] \
51 && ![istarget sparc*-*-elf] \
52 && ![istarget sparc*-*-solaris2*] \
53 && ![istarget sparc*-*-linux*] \
54 && ![istarget arm*-*-linux*] \
55 && ![istarget alpha*-*-linux*] \
56 && ![istarget rs6000*-*-aix*] \
57 && ![istarget powerpc*-*-aix*] \
58 && ![istarget s390*-*-linux*] \
59 && ![istarget aarch64*-*-linux*] \
60 && ![istarget x86_64-*-linux*] } {
61 return
62 }
63
64 set tmpdir tmpdir
65 set SHCFLAG ""
66 set shared_needs_pic "no"
67
68 # Disable all sanitizers.
69 set old_CFLAGS "$CFLAGS"
70 append CFLAGS " $NOSANITIZE_CFLAGS"
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
102 if [istarget arm*-*-linux*] {
103 # On ARM section anchors can change the symbol pre-emptability for
104 # non-PIC shared libraries, causing these tests to fail. Turn section
105 # anchors off.
106 set SHCFLAG "-fno-section-anchors"
107
108 # On targets that have MOVW the compiler will emit relocations which
109 # the linker doesn't support when compiling -shared without -fpic. The
110 # test to find out whether we want to XFAIL the non-PIC tests requires
111 # a compile - so we pre-calculate it here. We also note that this can
112 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
113 if [istarget arm*-*-*eabi*] {
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
123 # The test procedure.
124 proc shared_test { progname testname main sh1 sh2 dat args } {
125 global CC
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 }
140 if { [is_elf_format] && [check_shared_lib_support] } {
141 append shared " -Wl,-z,notext"
142 }
143 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
144 fail "$testname"
145 return
146 }
147
148 # Link against the shared library. Use -rpath so that the
149 # dynamic linker can locate the shared library at runtime.
150 # On AIX, we must include /lib in -rpath, as otherwise the loader
151 # can not find -lc.
152 set rpath $tmpdir
153 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
154 set rpath /lib:$tmpdir
155 }
156 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
157 fail "$testname"
158 return
159 }
160
161 if ![isnative] {
162 unsupported $testname
163 return
164 }
165
166 # Run the resulting program
167 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
168 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
169 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
170 if ![string match "" $exec_output] then {
171 send_log "$exec_output\n"
172 verbose "$exec_output"
173 fail "$testname"
174 return
175 }
176
177 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
178 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
179 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
180 set exec_output [prune_warnings $exec_output]
181
182 if {![string match "" $exec_output]} then {
183 send_log "$exec_output\n"
184 verbose "$exec_output"
185 fail "$testname"
186 return
187 }
188
189 pass "$testname"
190 }
191
192 # Old version of GCC for MIPS default to enabling -fpic
193 # and get confused if it is used on the command line.
194 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
195 set picflag ""
196 } else {
197 # Unfortunately, the gcc argument is -fpic and the cc argument is
198 # -KPIC. We have to try both.
199 set picflag "-fpic"
200 send_log "$CC $picflag\n"
201 verbose "$CC $picflag"
202 catch "exec $CC $picflag" exec_output
203 send_log "$exec_output\n"
204 verbose "--" "$exec_output"
205 if { [string match "*illegal option*" $exec_output] \
206 || [string match "*option ignored*" $exec_output] \
207 || [string match "*unrecognized option*" $exec_output] \
208 || [string match "*passed to ld*" $exec_output] } {
209 set picflag "-KPIC"
210 }
211 }
212 verbose "Using $picflag to compile PIC code"
213
214 # Compile the main program.
215 if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
216 unresolved "shared (non PIC)"
217 unresolved "shared"
218 } else {
219 # The shared library is composed of two files. First compile them
220 # without using -fpic. That should work on an ELF system,
221 # although it will be less efficient because the dynamic linker
222 # will need to do more relocation work. However, note that not
223 # using -fpic will cause some of the tests to return different
224 # results. Make sure that PLT is used since PLT is expected.
225 global PLT_CFLAGS NOPIE_CFLAGS
226 if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
227 || ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
228 unresolved "shared (non PIC)"
229 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
230 shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
231 } else {
232 # Solaris defaults to -z text.
233 setup_xfail "*-*-solaris2*"
234 setup_xfail "ia64-*-linux*"
235 setup_xfail "alpha*-*-linux*"
236 setup_xfail "powerpc64*-*-*"
237 if { ![istarget hppa*64*-*-linux*] } {
238 setup_xfail "hppa*-*-linux*"
239 }
240 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
241 setup_xfail "sparc*-*-linux*"
242 }
243 if { [is_elf64 $tmpdir/mainnp.o] } {
244 setup_xfail "x86_64-*-linux*"
245 }
246 setup_xfail "x86_64-*-linux-gnux32"
247 setup_xfail "s390x-*-linux*"
248 if [ string match $shared_needs_pic "yes" ] {
249 setup_xfail "arm*-*-linux*"
250 }
251 setup_xfail "aarch64*-*-linux*"
252 shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o shared
253
254 # Test ELF shared library relocations with a non-zero load
255 # address for the library. Near as I can tell, the R_*_RELATIVE
256 # relocations for various targets are broken in the case where
257 # the load address is not zero (which is the default).
258 setup_xfail "*-*-linux*libc1"
259 setup_xfail "powerpc*-*-linux*"
260 setup_xfail "ia64-*-linux*"
261 setup_xfail "alpha*-*-linux*"
262 setup_xfail "mips*-*-linux*"
263 if { ![istarget hppa*64*-*-linux*] } {
264 setup_xfail "hppa*-*-linux*"
265 }
266 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
267 setup_xfail "sparc*-*-linux*"
268 }
269 if { [is_elf64 $tmpdir/mainnp.o] } {
270 setup_xfail "x86_64-*-linux*"
271 }
272 setup_xfail "x86_64-*-linux-gnux32"
273 setup_xfail "s390x-*-linux*"
274 if [ string match $shared_needs_pic "yes" ] {
275 setup_xfail "arm*-*-linux*"
276 }
277 setup_xfail "aarch64*-*-linux*"
278 # Solaris defaults to -z text.
279 setup_xfail "*-*-solaris2*"
280 shared_test shnp "shared (non PIC, load offset)" \
281 mainnp.o sh1np.o sh2np.o shared \
282 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
283 } }
284
285 # Now compile the code using -fpic.
286
287 if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
288 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
289 unresolved "shared"
290 } else {
291 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
292 shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
293 } else {
294 shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
295 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
296 ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
297 shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
298 ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
299 ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
300 }
301 }
302 }
303
304 # Now do the same tests again, but this time compile main.c PIC.
305 if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
306 unresolved "shared (PIC main, non PIC so)"
307 unresolved "shared (PIC main)"
308 } else {
309 if { [file exists $tmpdir/sh1np.o ] && [ file exists $tmpdir/sh2np.o ] } {
310 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
311 shared_test shmpnp "shared (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
312 } else {
313 # Solaris defaults to -z text.
314 setup_xfail "*-*-solaris2*"
315 setup_xfail "ia64-*-linux*"
316 setup_xfail "alpha*-*-linux*"
317 setup_xfail "powerpc64*-*-*"
318 if { ![istarget hppa*64*-*-linux*] } {
319 setup_xfail "hppa*-*-linux*"
320 }
321 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
322 setup_xfail "sparc*-*-linux*"
323 }
324 if { [is_elf64 $tmpdir/mainp.o] } {
325 setup_xfail "x86_64-*-linux*"
326 }
327 setup_xfail "x86_64-*-linux-gnux32"
328 setup_xfail "s390x-*-linux*"
329 if [ string match $shared_needs_pic "yes" ] {
330 setup_xfail "arm*-*-linux*"
331 }
332 setup_xfail "aarch64*-*-linux*"
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 set CFLAGS "$old_CFLAGS"
351
352 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
353 # Remove the temporary directory.
354 catch "exec rm -rf $tmpdir" exec_status
355 }