ld: Pass -z notext to linker for tests with DT_TEXTREL
[deliverable/binutils-gdb.git] / ld / testsuite / ld-elfvsb / elfvsb.exp
1 # Expect script for ld-visibility tests
2 # Copyright (C) 2000-2020 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 # and H.J. Lu (hjl@gnu.org)
23 #
24
25 # Make sure that ld can generate ELF shared libraries with visibility.
26
27 # Check to see if the C compiler works
28 if { ![check_compiler_available] } {
29 return
30 }
31
32 # This test can only be run on a couple of ELF platforms.
33 # Square bracket expressions seem to confuse istarget.
34 if { ![istarget hppa*64*-*-hpux*] \
35 && ![istarget hppa*-*-linux*] \
36 && ![istarget i?86-*-linux*] \
37 && ![istarget i?86-*-gnu*] \
38 && ![istarget *-*-nacl*] \
39 && ![istarget ia64-*-linux*] \
40 && ![istarget m68k-*-linux*] \
41 && ![istarget mips*-*-linux*] \
42 && ![istarget powerpc*-*-linux*] \
43 && ![istarget arm*-*-linux*] \
44 && ![istarget alpha*-*-linux*] \
45 && ![istarget sparc*-*-linux*] \
46 && ![istarget s390*-*-linux*] \
47 && ![istarget sh\[34\]*-*-linux*] \
48 && ![istarget x86_64-*-linux*] } {
49 return
50 }
51
52 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
53 foreach t $test_list {
54 # We need to strip the ".d", but can leave the dirname.
55 verbose [file rootname $t]
56 run_dump_test [file rootname $t]
57 }
58
59 set tmpdir tmpdir
60 set SHCFLAG ""
61 set shared_needs_pic "no"
62 set COMPRESS_LDFLAG "-Wl,--compress-debug-sections=zlib-gabi"
63
64 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
65
66 # AIX shared libraries do not seem to support useful features,
67 # like overriding the shared library function or letting the
68 # shared library refer to objects defined in the main program. We
69 # avoid testing those features.
70 set SHCFLAG "-DXCOFF_TEST"
71
72 # The AIX 3.2.5 loader appears to randomly fail when loading
73 # shared libraries from NSF mounted partitions, so we avoid any
74 # potential problems by using a local directory.
75 catch {exec /bin/sh -c "echo $$"} pid
76 set tmpdir /usr/tmp/ld.$pid
77 catch "exec mkdir $tmpdir" exec_status
78
79 # On AIX, we need to explicitly export the symbols the shared
80 # library is going to provide, and need.
81 set file [open $tmpdir/xcoff.exp w]
82 puts $file shlibvar1
83 puts $file shlibvar2
84 puts $file shlib_shlibvar1
85 puts $file shlib_shlibvar2
86 puts $file shlib_shlibcall
87 puts $file shlib_shlibcalled
88 puts $file shlib_checkfunptr1
89 puts $file shlib_getfunptr1
90 puts $file shlib_check
91 close $file
92 }
93
94 if [istarget arm*-*-linux*] {
95 # On ARM section anchors can change the symbol pre-emptability for
96 # non-PIC shared libraries, causing these tests to fail. Turn section
97 # anchors off.
98 set SHCFLAG "-fno-section-anchors"
99
100 # On targets that have MOVW the compiler will emit relocations which
101 # the linker doesn't support when compiling -shared without -fpic. The
102 # test to find out whether we want to XFAIL the non-PIC tests requires
103 # a compile - so we pre-calculate it here. We also note that this can
104 # only affect arm*-*-*eabi* targets as the old ABI doesn't support v7.
105 if [istarget arm*-*-*eabi*] {
106 set file [open $tmpdir/movw-detect.c w]
107 puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
108 close $file
109 if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
110 set shared_needs_pic "yes"
111 }
112 }
113 }
114
115 set support_protected "no"
116
117 if { [istarget *-*-linux*]
118 || [istarget *-*-nacl*]
119 || [istarget *-*-gnu*] } {
120 if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
121 if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
122 catch "exec $tmpdir/main" support_protected
123 }
124 }
125 }
126
127 # The test procedure.
128 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
129 global CC
130 global srcdir
131 global subdir
132 global exec_output
133 global link_output
134 global host_triplet
135 global tmpdir
136
137 if [llength $args] { set shldflags [lindex $args 0] } else { set shldflags "" }
138
139 # Build the shared library.
140 # On AIX, we need to use an export file.
141 set shared -shared
142 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
143 set shared "-bM:SRE -bE:$tmpdir/xcoff.exp"
144 }
145 if { [is_elf_format] && [check_shared_lib_support] } {
146 append shared " -Wl,-z,notext"
147 }
148 if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
149 if { [ string match $visibility "hidden_undef" ]
150 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
151 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
152 pass "$testname"
153 } else { if { [ string match $visibility "protected_undef" ]
154 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
155 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
156 pass "$testname"
157 } else {
158 fail "$testname"
159 }}
160 return
161 }
162
163 # Link against the shared library. Use -rpath so that the
164 # dynamic linker can locate the shared library at runtime.
165 # On AIX, we must include /lib in -rpath, as otherwise the loader
166 # can not find -lc.
167 set rpath $tmpdir
168 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
169 set rpath /lib:$tmpdir
170 }
171 if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
172 if { [ string match $visibility "hidden" ]
173 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
174 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
175 pass "$testname"
176 } else { if { [ string match $visibility "hidden_undef_def" ]
177 && [regexp "undefined reference to \`\.?visibility\'" $link_output]
178 && [regexp "undefined reference to \`visibility_def\'" $link_output]
179 && [regexp "undefined reference to \`\.?visibility_func\'" $link_output]
180 && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
181 pass "$testname"
182 } else {
183 fail "$testname"
184 }}
185 return
186 }
187
188 if { [ string match $visibility "hidden" ]
189 || [ string match $visibility "hidden_undef" ]
190 || [ string match $visibility "protected_undef" ] } {
191 fail "$testname"
192 }
193
194 if ![isnative] {
195 unsupported "$testname"
196 return
197 }
198
199 # Run the resulting program
200 send_log "$tmpdir/$progname >$tmpdir/$progname.out\n"
201 verbose "$tmpdir/$progname >$tmpdir/$progname.out"
202 catch "exec $tmpdir/$progname >$tmpdir/$progname.out" exec_output
203 if ![string match "" $exec_output] then {
204 send_log "$exec_output\n"
205 verbose "$exec_output"
206 fail "$testname"
207 return
208 }
209
210 send_log "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat\n"
211 verbose "diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat"
212 catch "exec diff $tmpdir/$progname.out $srcdir/$subdir/$dat.dat" exec_output
213 set exec_output [prune_warnings $exec_output]
214
215 if {![string match "" $exec_output]} then {
216 send_log "$exec_output\n"
217 verbose "$exec_output"
218 fail "$testname"
219 return
220 }
221
222 pass "$testname"
223 }
224
225 proc visibility_run {visibility} {
226 global CC
227 global CFLAGS
228 global SHCFLAG
229 global srcdir
230 global subdir
231 global tmpdir
232 global picflag
233 global target_triplet
234 global support_protected
235 global shared_needs_pic
236 global PLT_CFLAGS
237 global NOPIE_CFLAGS
238 global COMPRESS_LDFLAG
239
240 if [ string match $visibility "hidden" ] {
241 set VSBCFLAG "-DHIDDEN_TEST"
242 } else { if [ string match $visibility "hidden_normal" ] {
243 set VSBCFLAG "-DHIDDEN_NORMAL_TEST"
244 } else { if [ string match $visibility "hidden_undef" ] {
245 set VSBCFLAG "-DHIDDEN_UNDEF_TEST"
246 } else { if [ string match $visibility "hidden_undef_def" ] {
247 set VSBCFLAG "-DHIDDEN_UNDEF_TEST -DDSO_DEFINE_TEST"
248 } else { if [ string match $visibility "hidden_weak" ] {
249 set VSBCFLAG "-DHIDDEN_WEAK_TEST"
250 } else { if [ string match $visibility "protected" ] {
251 set VSBCFLAG "-DPROTECTED_TEST"
252 } else { if [ string match $visibility "protected_undef" ] {
253 set VSBCFLAG "-DPROTECTED_UNDEF_TEST"
254 } else { if [ string match $visibility "protected_undef_def" ] {
255 set VSBCFLAG "-DPROTECTED_UNDEF_TEST -DDSO_DEFINE_TEST"
256 } else { if [ string match $visibility "protected_weak" ] {
257 set VSBCFLAG "-DPROTECTED_WEAK_TEST"
258 } else {
259 set VSBCFLAG ""
260 }}}}}}}}}
261
262 # Ensure we always start with a clean slate, for the "file exists"
263 # tests below.
264 remote_file host delete $tmpdir/sh1p.o $tmpdir/sh2p.o $tmpdir/sh1np.o $tmpdir/sh2np.o
265
266 if { [istarget powerpc*-*-linux*] \
267 || ( [istarget mips*-*-linux*] && [at_least_gcc_version 4 3] )} {
268 # Testing non-PIC libraries is a waste of effort on any target.
269 # If you don't pass -fpic or -fPIC to gcc, gcc will assume quite
270 # reasonably that you are not compiling for a shared library.
271 # It can then make optimisations that result in shared library
272 # functions and variables not being overridable. Newer versions
273 # of gcc are more likely to do this.
274 } else {
275 # Compile the main program. Make sure that PLT is used since PLT
276 # is expected.
277 if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
278 unresolved "visibility ($visibility) (non PIC)"
279 unresolved "visibility ($visibility)"
280 } else {
281 # The shared library is composed of two files. First compile them
282 # without using -fpic. That should work on an ELF system,
283 # although it will be less efficient because the dynamic linker
284 # will need to do more relocation work. However, note that not
285 # using -fpic will cause some of the tests to return different
286 # results. Make sure that PLT is used since PLT is expected.
287 if { ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
288 || ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
289 unresolved "visibility ($visibility) (non PIC)"
290 } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
291 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o xcoff
292 } else {
293 # SunOS non PIC shared libraries don't permit some cases of
294 # overriding.
295 if { [ string match $visibility "protected" ]
296 || [ string match $visibility "protected_undef_def" ] } {
297 if [ string match $support_protected "no" ] {
298 setup_xfail $target_triplet
299 }
300 }
301
302 # Non-pic code uses name binding rules for applications to
303 # reference variables by gp-relative relocs, which can't be
304 # used with overridable symbols.
305 if { ![ string match $visibility "hidden_undef" ]
306 && ![ string match $visibility "protected_undef" ] } {
307 setup_xfail "ia64-*-linux*"
308 setup_xfail "alpha*-*-linux*"
309 }
310 if { ![ string match $visibility "hidden" ]
311 && ![ string match $visibility "hidden_undef" ]
312 && ![ string match $visibility "hidden_undef_def" ]
313 && ![ string match $visibility "protected_undef" ] } {
314 setup_xfail "s390x-*-linux*"
315 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
316 setup_xfail "sparc*-*-linux*"
317 }
318 }
319 if { [is_elf64 $tmpdir/mainnp.o] } {
320 setup_xfail "x86_64-*-linux*"
321 }
322 setup_xfail "x86_64-*-linux-gnux32"
323 if { ![istarget hppa*64*-*-linux*] } {
324 setup_xfail "hppa*-*-linux*"
325 }
326 if [ string match $shared_needs_pic "yes" ] {
327 setup_xfail "arm*-*-linux*"
328 }
329
330 visibility_test $visibility vnp "visibility ($visibility) (non PIC)" mainnp.o sh1np.o sh2np.o elfvsb
331
332 # Test ELF shared library relocations with a non-zero load
333 # address for the library. Near as I can tell, the R_*_RELATIVE
334 # relocations for various targets are broken in the case where
335 # the load address is not zero (which is the default).
336 if { [ string match $visibility "protected" ]
337 || [ string match $visibility "protected_undef_def" ] } {
338 if [ string match $support_protected "no" ] {
339 setup_xfail $target_triplet
340 }
341 }
342 if { [ string match $visibility "hidden_normal" ]
343 || [ string match $visibility "hidden_weak" ]
344 || [ string match $visibility "protected" ]
345 || [ string match $visibility "protected_undef_def" ]
346 || [ string match $visibility "protected_weak" ]
347 || [ string match $visibility "normal" ] } {
348 setup_xfail "powerpc-*-linux*"
349 setup_xfail "s390x-*-linux*"
350 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainnp.o] } {
351 setup_xfail "sparc*-*-linux*"
352 }
353 }
354 if { ![ string match $visibility "hidden_undef" ]
355 && ![ string match $visibility "protected_undef" ] } {
356 setup_xfail "ia64-*-linux*"
357 setup_xfail "alpha*-*-linux*"
358 setup_xfail "mips*-*-linux*"
359 }
360 if { [is_elf64 $tmpdir/mainnp.o] } {
361 setup_xfail "x86_64-*-linux*"
362 }
363 setup_xfail "x86_64-*-linux-gnux32"
364 if { ![istarget hppa*64*-*-linux*] } {
365 setup_xfail "hppa*-*-linux*"
366 }
367 if [ string match $shared_needs_pic "yes" ] {
368 setup_xfail "arm*-*-linux*"
369 }
370
371 visibility_test $visibility vnp "visibility ($visibility) (non PIC, load offset)" \
372 mainnp.o sh1np.o sh2np.o elfvsb \
373 "-Wl,-T,$srcdir/$subdir/elf-offset.ld,--hash-style=sysv"
374 } }
375
376 # Now compile the code using -fpic.
377
378 if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
379 || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
380 unresolved "visibility ($visibility)"
381 } else {
382 if { [ string match $visibility "protected" ]
383 || [ string match $visibility "protected_undef_def" ] } {
384 if [ string match $support_protected "no" ] {
385 setup_xfail $target_triplet
386 }
387 }
388 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
389 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o xcoff
390 } else {
391 visibility_test $visibility vp "visibility ($visibility)" mainnp.o sh1p.o sh2p.o elfvsb $COMPRESS_LDFLAG
392 }
393 }
394 }}
395
396 if { [istarget powerpc*-*-linux*] } {
397 # Don't bother.
398 } else {
399 # Now do the same tests again, but this time compile main.c PIC.
400 if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
401 unresolved "visibility ($visibility) (PIC main, non PIC so)"
402 unresolved "visibility ($visibility) (PIC main)"
403 } else {
404 if { [ remote_file host exists $tmpdir/sh1np.o ]
405 && [ remote_file host exists $tmpdir/sh2np.o ] } {
406 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
407 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o xcoff
408 } else {
409 # SunOS non PIC shared libraries don't permit some cases of
410 # overriding.
411 if { [ string match $visibility "protected" ]
412 || [ string match $visibility "protected_undef_def" ] } {
413 if [ string match $support_protected "no" ] {
414 setup_xfail $target_triplet
415 }
416 }
417 if { ![ string match $visibility "hidden_undef" ]
418 && ![ string match $visibility "protected_undef" ] } {
419 setup_xfail "ia64-*-linux*"
420 setup_xfail "alpha*-*-linux*"
421 }
422 if { ![ string match $visibility "hidden" ]
423 && ![ string match $visibility "hidden_undef" ]
424 && ![ string match $visibility "hidden_undef_def" ]
425 && ![ string match $visibility "protected_undef" ] } {
426 setup_xfail "s390x-*-linux*"
427 if { [istarget sparc*-*-linux*] && [is_elf64 $tmpdir/mainp.o] } {
428 setup_xfail "sparc*-*-linux*"
429 }
430 }
431 if { [is_elf64 $tmpdir/mainp.o] } {
432 setup_xfail "x86_64-*-linux*"
433 }
434 setup_xfail "x86_64-*-linux-gnux32"
435 if { ![istarget hppa*64*-*-linux*] } {
436 setup_xfail "hppa*-*-linux*"
437 }
438 if [ string match $shared_needs_pic "yes" ] {
439 setup_xfail "arm*-*-linux*"
440 }
441
442 visibility_test $visibility vmpnp "visibility ($visibility) (PIC main, non PIC so)" mainp.o sh1np.o sh2np.o elfvsb
443 }
444 } else {
445 unresolved "visibility (PIC main, non PIC so)"
446 }
447
448 if { [ remote_file host exists $tmpdir/sh1p.o ]
449 && [ remote_file host exists $tmpdir/sh2p.o ] } {
450 if { [ string match $visibility "protected" ]
451 || [ string match $visibility "protected_undef_def" ] } {
452 if [ string match $support_protected "no" ] {
453 setup_xfail $target_triplet
454 }
455 }
456 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
457 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o xcoff
458 } else {
459 visibility_test $visibility vmpp "visibility ($visibility) (PIC main)" mainp.o sh1p.o sh2p.o elfvsb
460 }
461 } else {
462 unresolved "visibility ($visibility) (PIC main)"
463 }
464 }}
465 }
466
467 # Old version of GCC for MIPS default to enabling -fpic
468 # and get confused if it is used on the command line.
469 if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
470 set picflag ""
471 } else {
472 # Unfortunately, the gcc argument is -fpic and the cc argument is
473 # -KPIC. We have to try both.
474 set picflag "-fpic"
475 send_log "$CC $picflag\n"
476 verbose "$CC $picflag"
477 catch "exec $CC $picflag" exec_output
478 send_log "$exec_output\n"
479 verbose "--" "$exec_output"
480 if { [string match "*illegal option*" $exec_output] \
481 || [string match "*option ignored*" $exec_output] \
482 || [string match "*unrecognized option*" $exec_output] \
483 || [string match "*passed to ld*" $exec_output] } {
484 set picflag "-KPIC"
485 }
486 }
487 verbose "Using $picflag to compile PIC code"
488
489 visibility_run hidden
490 visibility_run hidden_normal
491 visibility_run hidden_undef
492 visibility_run hidden_undef_def
493 visibility_run hidden_weak
494 visibility_run protected
495 visibility_run protected_undef
496 visibility_run protected_undef_def
497 visibility_run protected_weak
498 visibility_run normal
499
500 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
501 unresolved "common hidden symbol"
502 } else {
503 if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
504 fail "common hidden symbol"
505 } else {
506 pass "common hidden symbol"
507 }
508 }
509
510 if { ![ld_compile "$CC -g $CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
511 unresolved "weak hidden symbol"
512 } else {
513 if { ![ld_compile "$CC -g $CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
514 unresolved "weak hidden symbol"
515 } else {
516 if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
517 fail "weak hidden symbol"
518 } else {
519 if ![ld_link $ld tmpdir/weak "tmpdir/test.o tmpdir/sh3.o"] {
520 fail "weak hidden symbol DSO last"
521 } else {
522 pass "weak hidden symbol DSO last"
523 }
524 if ![ld_link $ld tmpdir/weak "tmpdir/sh3.so tmpdir/test.o"] {
525 fail "weak hidden symbol DSO first"
526 } else {
527 pass "weak hidden symbol DSO first"
528 }
529 }
530 }
531 }
532
533 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
534 # Remove the temporary directory.
535 catch "exec rm -rf $tmpdir" exec_status
536 }
This page took 0.040451 seconds and 4 git commands to generate.