1 # Expect script for linker support of IFUNC symbols and relocations.
3 # Copyright (C) 2009-2018 Free Software Foundation, Inc.
4 # Contributed by Red Hat.
6 # This file is part of the GNU Binutils.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 # Written by Nick Clifton <nickc@redhat.com>
26 # IFUNC support has only been implemented for the ix86, x86_64, powerpc,
27 # aarch64, sparc, and S/390 so far.
28 if {!(([istarget "i?86-*-*"]
29 || [istarget "x86_64-*-*"]
30 || [istarget "powerpc*-*-*"]
31 || [istarget "aarch64*-*-*"]
32 || [istarget "sparc*-*-*"]
33 || [istarget "s390*-*-*"])
34 && ([istarget "*-*-elf*"]
35 || [istarget "*-*-nacl*"]
36 || [istarget "*-*-linux*"]
37 || [istarget "*-*-gnu*"])) } {
38 verbose "IFUNC tests not run - target does not support IFUNC"
42 # Skip targets where -shared is not supported
44 if ![check_shared_lib_support] {
48 # This test does not need a compiler...
49 run_dump_test "ifuncmod5"
51 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
52 foreach t $test_list {
53 # We need to strip the ".d", but can leave the dirname.
54 verbose [file rootname $t]
55 run_dump_test [file rootname $t]
58 # We need a working compiler. (Strictly speaking this is
59 # not true, we could use target specific assembler files).
60 if { [which $CC] == 0 } {
61 verbose "IFUNC tests not run - no compiler available"
65 # A procedure to check the OS/ABI field in the ELF header of a binary file.
66 proc check_osabi { binary_file expected_osabi } {
70 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
72 if ![string match "" $got] then {
73 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
77 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
78 [file_contents readelf.out] nil osabi] } {
79 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
83 if { $osabi == $expected_osabi } {
87 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
92 # A procedure to confirm that a file contains the IFUNC symbol.
93 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
94 proc contains_ifunc_symbol { binary_file } {
98 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
100 if ![string match "" $got] then {
101 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
105 # Look for a line like this:
106 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
107 # with perhaps some other info between the visibility and section
109 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
116 # A procedure to confirm that a file contains the R_*_IRELATIVE
118 # Returns -1 upon error, 0 if the relocation was not found and 1 if
120 proc contains_irelative_reloc { binary_file } {
124 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
126 if ![string match "" $got] then {
127 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
131 # Look for a line like this:
132 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
133 # 080496f4 0000002a R_386_IRELATIVE
136 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
143 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
144 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
145 proc contains_ifunc_reloc { binary_file } {
149 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
151 if ![string match "" $got] then {
152 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
156 if [string match "" [file_contents readelf.out]] then {
157 verbose "No relocs found in $binary_file"
161 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
170 # Create the object files, libraries and executables.
171 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
172 fail "Could not create a PIC object file"
173 set fails [expr $fails + 1]
175 if ![ld_compile "$CC -c $NOPIE_CFLAGS" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
176 fail "Could not create a non-PIC object file"
177 set fails [expr $fails + 1]
179 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
180 fail "Could not create a PIC object file containing an IFUNC symbol"
181 set fails [expr $fails + 1]
183 if ![ld_compile "$CC -c $NOPIE_CFLAGS -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
184 fail "Could not create a non-PIC object file containing an IFUNC symbol"
185 set fails [expr $fails + 1]
187 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
188 fail "Could not create an ordinary non-PIC object file"
189 set fails [expr $fails + 1]
191 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
192 fail "Could not create an empty object file"
193 set fails [expr $fails + 1]
195 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
196 fail "Could not create test-1.o"
197 set fails [expr $fails + 1]
199 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
200 fail "Could not create test-2.o"
201 set fails [expr $fails + 1]
208 if ![ld_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
209 fail "Could not create a shared library containing an IFUNC symbol"
210 set fails [expr $fails + 1]
212 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
213 fail "Could not create a static library containing an IFUNC symbol"
214 set fails [expr $fails + 1]
221 if ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
222 fail "Could not link a dynamic executable"
223 set fails [expr $fails + 1]
225 if ![ld_link $CC "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
226 fail "Could not link a dynamic executable using local ifunc"
227 set fails [expr $fails + 1]
229 if ![string match "" $STATIC_LDFLAGS] {
230 if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
231 fail "Could not link a static executable"
232 set fails [expr $fails + 1]
234 if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
235 fail "Could not link a non-ifunc using static executable"
236 set fails [expr $fails + 1]
239 if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
240 fail "Could not link test-1"
241 set fails [expr $fails + 1]
243 if ![ld_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
244 fail "Could not link libtest-2.so"
245 set fails [expr $fails + 1]
247 if ![ld_link $ld "tmpdir/libtest-2-now.so" "-shared -z now tmpdir/test-2.o"] {
248 fail "Could not link libtest-2-now.so"
249 set fails [expr $fails + 1]
253 pass "Building ifunc binaries"
259 # Check the executables and shared libraries
261 # The linked ifunc using executables and the shared library containing
262 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
263 # executable should have an OSABI field of NONE (aka System V).
265 case $target_triplet in {
266 { hppa*-*-linux* } { set expected_none {UNIX - GNU} }
267 default { set expected_none {UNIX - System V} }
270 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
271 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
272 set fails [expr $fails + 1]
274 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
275 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
276 set fails [expr $fails + 1]
278 if { ![string match "" $STATIC_LDFLAGS] \
279 && ![check_osabi tmpdir/static_prog {UNIX - GNU}]} {
280 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
281 set fails [expr $fails + 1]
283 if {! [check_osabi tmpdir/dynamic_prog $expected_none]} {
284 fail "Dynamic ifunc-using executable does not have an OS/ABI field of $expected_none"
285 set fails [expr $fails + 1]
287 if {! [check_osabi tmpdir/static_nonifunc_prog $expected_none]} {
288 fail "Static non-ifunc-using executable does not have an OS/ABI field of $expected_none"
289 set fails [expr $fails + 1]
292 # The linked ifunc using executables and the shared library containing
293 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
296 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
297 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
298 set fails [expr $fails + 1]
300 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
301 fail "Local ifunc-using executable does not contain an IFUNC symbol"
302 set fails [expr $fails + 1]
304 if { ![string match "" $STATIC_LDFLAGS] \
305 && [contains_ifunc_symbol tmpdir/static_prog] != 1} {
306 fail "Static ifunc-using executable does not contain an IFUNC symbol"
307 set fails [expr $fails + 1]
309 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
310 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
311 set fails [expr $fails + 1]
313 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
314 fail "Static non-ifunc-using executable contains an IFUNC symbol"
315 set fails [expr $fails + 1]
317 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
318 fail "test-1 contains IFUNC symbols"
319 set fails [expr $fails + 1]
321 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
322 fail "libtest-2.so contains IFUNC symbols"
323 set fails [expr $fails + 1]
325 if {[contains_ifunc_symbol tmpdir/libtest-2-now.so] != 0} {
326 fail "libtest-2-now.so contains IFUNC symbols"
327 set fails [expr $fails + 1]
330 # The linked ifunc using executables and shared libraries should contain
331 # a dynamic reloc referencing the IFUNC symbol. (Even the static
332 # executable which should have a dynamic section created for it). The
333 # non-ifunc using executable should not.
335 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
336 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
337 set fails [expr $fails + 1]
339 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
340 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
341 set fails [expr $fails + 1]
343 if { ![string match "" $STATIC_LDFLAGS] \
344 && [contains_irelative_reloc tmpdir/static_prog] != 1} {
345 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
346 set fails [expr $fails + 1]
348 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
349 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
350 set fails [expr $fails + 1]
352 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
353 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
354 set fails [expr $fails + 1]
358 pass "Checking ifunc binaries"
361 run_cc_link_tests [list \
363 "Build libpr16467a.so" \
364 "-shared -Wl,--version-script=pr16467a.map" \
371 "Build libpr16467b.a" \
379 "Build libpr16467b.so" \
380 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
381 -Wl,--version-script=pr16467b.map" \
388 "Build libpr16467c.a" \
396 "Build libpr16467an.so" \
397 "-shared -Wl,-z,now -Wl,--version-script=pr16467a.map" \
404 "Build libpr16467bn.so" \
405 "-shared tmpdir/pr16467b.o tmpdir/libpr16467an.so \
406 -Wl,--version-script=pr16467b.map" \
414 run_ld_link_exec_tests [list \
416 "Common symbol override ifunc test 1a" \
419 { ifunc-common-1a.c ifunc-common-1b.c } \
421 "ifunc-common-1.out" \
425 "Common symbol override ifunc test 1b" \
428 { ifunc-common-1b.c ifunc-common-1a.c } \
430 "ifunc-common-1.out" \
435 # Run-time tests which require working IFUNC support.
436 if { ![check_ifunc_available] } {
440 run_cc_link_tests [list \
442 "Build ifunc-lib.so" \
450 "Build ifunc-libn.so" \
451 "-shared -Wl,-z,now" \
459 run_ld_link_exec_tests [list \
462 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
470 "Run pr16467 (-z now)" \
471 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467bn.so tmpdir/libpr16467an.so" \
480 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
487 "Run ifunc-main with -fpic" \
488 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
496 "Run ifunc-main (-z now)" \
497 "-Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
504 "Run ifunc-main with PIE (-z now)" \
505 "-pie -Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
514 # Run-time tests which require working ifunc attribute support.
515 if { ![check_ifunc_attribute_available] } {
519 run_cc_link_tests [list \
529 "Build libpr18808.so" \
537 "Build libpr18808n.so" \
538 "-shared -Wl,-z,now" \
553 "Build libpr18841b.so" \
561 "Build libpr18841c.so" \
569 "Build libpr18841bn.so" \
570 "-Wl,-z,now -shared" \
577 "Build libpr18841cn.so" \
579 "-Wl,-z,now -fPIC -O0 -g" \
585 "Build libpr23169a.so" \
593 "Build libpr23169b.so" \
594 "-shared -Wl,-z,now" \
602 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
603 "$NOPIE_CFLAGS -O2 -g" \
604 { pr23169b.c pr23169c.c } \
605 {{readelf {--dyn-syms} pr23169a.rd} \
606 {readelf {-r -W} pr23169b.rd}} \
611 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
613 { pr23169b.c pr23169c.c } \
614 {{readelf {--dyn-syms} pr23169c.rd} \
615 {readelf {-r -W} pr23169b.rd}} \
620 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
622 { pr23169b.c pr23169c.c } \
623 {{readelf {--dyn-syms} pr23169c.rd} \
624 {readelf {-r -W} pr23169b.rd}} \
629 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
630 "$NOPIE_CFLAGS -O2 -g" \
631 { pr23169b.c pr23169c.c } \
632 {{readelf {--dyn-syms} pr23169a.rd} \
633 {readelf {-r -W} pr23169b.rd}} \
638 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
640 { pr23169b.c pr23169c.c } \
641 {{readelf {--dyn-syms} pr23169c.rd} \
642 {readelf {-r -W} pr23169b.rd}} \
647 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
649 { pr23169b.c pr23169c.c } \
650 {{readelf {--dyn-syms} pr23169c.rd} \
651 {readelf {-r -W} pr23169b.rd}} \
656 run_ld_link_exec_tests [list \
659 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
666 "Run pr18808 (-z now)" \
667 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808n.so" \
674 "Run pr18841 with libpr18841b.so" \
675 "-Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
682 "Run pr18841 with libpr18841c.so" \
683 "-Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
690 "Run pr18841 with libpr18841bn.so (-z now)" \
691 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841bn.so" \
698 "Run pr18841 with libpr18841cn.so (-z now)" \
699 "-Wl,-z,now -Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841cn.so" \
707 # The pr23169 testcase is not valid. In general, you can't call ifunc
708 # resolvers in another binary unless you know what you're doing. In
709 # particular you must ensure that the binary containing the resolver
710 # is relocated before the resolver is called (for example, the
711 # function addresses returned by the resolver may be loaded from the
713 # That does not happen for the pr23169 testcase where the resolver is
714 # in the executable (which is relocated last by ld.so).
716 && ![istarget "powerpc-*-*"] } {
717 run_ld_link_exec_tests [list \
720 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
722 { pr23169b.c pr23169c.c } \
725 "$NOPIE_CFLAGS -O2 -g" \
729 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
731 { pr23169b.c pr23169c.c } \
738 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
740 { pr23169b.c pr23169c.c } \
747 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
749 { pr23169b.c pr23169c.c } \
752 "$NOPIE_CFLAGS -O2 -g" \
756 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
758 { pr23169b.c pr23169c.c } \
765 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
767 { pr23169b.c pr23169c.c } \