1 # Expect script for linker support of IFUNC symbols and relocations.
3 # Copyright (C) 2009-2020 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 if { ![is_elf_format] || ![supports_gnu_osabi]
27 || [istarget arc*-*-*]
28 || [istarget am33*-*-*]
29 || [istarget bfin-*-*]
30 || [istarget cris*-*-*]
32 || [istarget lm32-*-*]
33 || [istarget m32r-*-*]
34 || [istarget m68k-*-*]
35 || [istarget microblaze-*-*]
36 || [istarget mips*-*-*]
37 || [istarget mn10300-*-*]
38 || [istarget nds32*-*-*]
39 || [istarget nios2-*-*]
40 || [istarget or1k-*-*]
41 || [istarget riscv*-*-*]
42 || [istarget score*-*-*]
44 || [istarget tic6x-*-*]
45 || [istarget tile*-*-*]
46 || [istarget vax-*-*] } {
47 verbose "IFUNC tests not run - target does not support IFUNC"
51 # Skip targets where -shared is not supported
53 if ![check_shared_lib_support] {
57 set saved_ASFLAGS "$ASFLAGS"
58 if { [istarget "i?86-*-*"] || [istarget "x86_64-*-*"] } {
59 set ASFLAGS "$ASFLAGS -mx86-used-note=no"
62 # This test does not need a compiler...
63 run_dump_test "ifuncmod5"
65 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
66 foreach t $test_list {
67 # We need to strip the ".d", but can leave the dirname.
68 verbose [file rootname $t]
69 run_dump_test [file rootname $t]
72 # We need a working compiler. (Strictly speaking this is
73 # not true, we could use target specific assembler files).
74 if { ![check_compiler_available] } {
75 verbose "IFUNC tests not run - no compiler available"
79 # A procedure to check the OS/ABI field in the ELF header of a binary file.
80 proc check_osabi { binary_file expected_osabi } {
84 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
86 if ![string match "" $got] then {
87 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
91 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
92 [file_contents readelf.out] nil osabi] } {
93 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
97 if { $osabi == $expected_osabi } {
101 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
106 # A procedure to confirm that a file contains the IFUNC symbol.
107 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
108 proc contains_ifunc_symbol { binary_file } {
112 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
114 if ![string match "" $got] then {
115 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
119 # Look for a line like this:
120 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
121 # with perhaps some other info between the visibility and section
123 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
130 # A procedure to confirm that a file contains the R_*_IRELATIVE
132 # Returns -1 upon error, 0 if the relocation was not found and 1 if
134 proc contains_irelative_reloc { binary_file } {
138 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
140 if ![string match "" $got] then {
141 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
145 # Look for a line like this:
146 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
147 # 080496f4 0000002a R_386_IRELATIVE
150 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_(\[_0-9A-Z\]+_IREL(|ATIVE)|PARISC_IPLT)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
157 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
158 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
159 proc contains_ifunc_reloc { binary_file } {
163 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
165 if ![string match "" $got] then {
166 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
170 if [string match "" [file_contents readelf.out]] then {
171 verbose "No relocs found in $binary_file"
175 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
184 # Create the object files, libraries and executables.
185 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
186 fail "Could not create a PIC object file"
187 set fails [expr $fails + 1]
189 if ![ld_compile "$CC -c $NOPIE_CFLAGS" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
190 fail "Could not create a non-PIC object file"
191 set fails [expr $fails + 1]
193 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
194 fail "Could not create a PIC object file containing an IFUNC symbol"
195 set fails [expr $fails + 1]
197 if ![ld_compile "$CC -c $NOPIE_CFLAGS -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
198 fail "Could not create a non-PIC object file containing an IFUNC symbol"
199 set fails [expr $fails + 1]
201 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
202 fail "Could not create an ordinary non-PIC object file"
203 set fails [expr $fails + 1]
205 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
206 fail "Could not create an empty object file"
207 set fails [expr $fails + 1]
209 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
210 fail "Could not create test-1.o"
211 set fails [expr $fails + 1]
213 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
214 fail "Could not create test-2.o"
215 set fails [expr $fails + 1]
222 if ![ld_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
223 fail "Could not create a shared library containing an IFUNC symbol"
224 set fails [expr $fails + 1]
226 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
227 fail "Could not create a static library containing an IFUNC symbol"
228 set fails [expr $fails + 1]
235 if ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
236 fail "Could not link a dynamic executable"
237 set fails [expr $fails + 1]
239 if ![ld_link $CC "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
240 fail "Could not link a dynamic executable using local ifunc"
241 set fails [expr $fails + 1]
243 if ![string match "" $STATIC_LDFLAGS] {
244 if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
245 fail "Could not link a static executable"
246 set fails [expr $fails + 1]
249 if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
250 fail "Could not link a non-ifunc using static executable"
251 set fails [expr $fails + 1]
253 if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
254 fail "Could not link test-1"
255 set fails [expr $fails + 1]
257 if ![ld_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
258 fail "Could not link libtest-2.so"
259 set fails [expr $fails + 1]
261 if ![ld_link $ld "tmpdir/libtest-2-now.so" "-shared -z now tmpdir/test-2.o"] {
262 fail "Could not link libtest-2-now.so"
263 set fails [expr $fails + 1]
267 pass "Building ifunc binaries"
273 # Check the executables and shared libraries
275 # The linked ifunc using executables and the shared library containing
276 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
277 # executable should have an OSABI field of NONE (aka System V).
279 switch -glob $target_triplet {
280 hppa*-*-linux* { set expected_none {UNIX - GNU} }
281 default { set expected_none {UNIX - System V} }
284 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
285 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
286 set fails [expr $fails + 1]
288 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
289 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
290 set fails [expr $fails + 1]
292 if { ![string match "" $STATIC_LDFLAGS] \
293 && ![check_osabi tmpdir/static_prog {UNIX - GNU}]} {
294 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
295 set fails [expr $fails + 1]
297 if {! [check_osabi tmpdir/dynamic_prog $expected_none]} {
298 fail "Dynamic ifunc-using executable does not have an OS/ABI field of $expected_none"
299 set fails [expr $fails + 1]
301 if {! [check_osabi tmpdir/static_nonifunc_prog $expected_none]} {
302 fail "Static non-ifunc-using executable does not have an OS/ABI field of $expected_none"
303 set fails [expr $fails + 1]
306 # The linked ifunc using executables and the shared library containing
307 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
310 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
311 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
312 set fails [expr $fails + 1]
314 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
315 fail "Local ifunc-using executable does not contain an IFUNC symbol"
316 set fails [expr $fails + 1]
318 if { ![string match "" $STATIC_LDFLAGS] \
319 && [contains_ifunc_symbol tmpdir/static_prog] != 1} {
320 fail "Static ifunc-using executable does not contain an IFUNC symbol"
321 set fails [expr $fails + 1]
323 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
324 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
325 set fails [expr $fails + 1]
327 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
328 fail "Static non-ifunc-using executable contains an IFUNC symbol"
329 set fails [expr $fails + 1]
331 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
332 fail "test-1 contains IFUNC symbols"
333 set fails [expr $fails + 1]
335 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
336 fail "libtest-2.so contains IFUNC symbols"
337 set fails [expr $fails + 1]
339 if {[contains_ifunc_symbol tmpdir/libtest-2-now.so] != 0} {
340 fail "libtest-2-now.so contains IFUNC symbols"
341 set fails [expr $fails + 1]
344 # The linked ifunc using executables and shared libraries should contain
345 # a dynamic reloc referencing the IFUNC symbol. (Even the static
346 # executable which should have a dynamic section created for it). The
347 # non-ifunc using executable should not.
349 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
350 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
351 set fails [expr $fails + 1]
353 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
354 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
355 set fails [expr $fails + 1]
357 if { ![string match "" $STATIC_LDFLAGS] \
358 && [contains_irelative_reloc tmpdir/static_prog] != 1} {
359 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
360 set fails [expr $fails + 1]
362 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
363 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
364 set fails [expr $fails + 1]
366 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
367 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
368 set fails [expr $fails + 1]
372 pass "Checking ifunc binaries"
375 run_cc_link_tests [list \
377 "Build libpr16467a.so" \
378 "-shared -Wl,--version-script=pr16467a.map" \
385 "Build libpr16467b.a" \
393 "Build libpr16467b.so" \
394 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
395 -Wl,--version-script=pr16467b.map" \
402 "Build libpr16467c.a" \
410 "Build libpr16467an.so" \
411 "-shared -Wl,-z,now -Wl,--version-script=pr16467a.map" \
418 "Build libpr16467bn.so" \
419 "-shared tmpdir/pr16467b.o tmpdir/libpr16467an.so \
420 -Wl,--version-script=pr16467b.map" \
428 run_ld_link_exec_tests [list \
430 "Common symbol override ifunc test 1a" \
433 { ifunc-common-1a.c ifunc-common-1b.c } \
435 "ifunc-common-1.out" \
439 "Common symbol override ifunc test 1b" \
442 { ifunc-common-1b.c ifunc-common-1a.c } \
444 "ifunc-common-1.out" \
449 # Run-time tests which require working IFUNC support.
450 if { ![check_ifunc_available] } {
454 run_cc_link_tests [list \
456 "Build ifunc-lib.so" \
464 "Build ifunc-libn.so" \
465 "-shared -Wl,-z,now" \
473 run_ld_link_exec_tests [list \
476 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
484 "Run pr16467 (-z now)" \
485 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467bn.so tmpdir/libpr16467an.so" \
494 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
501 "Run ifunc-main with -fpic" \
502 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
510 "Run ifunc-main (-z now)" \
511 "-Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
518 "Run ifunc-main with PIE (-z now)" \
519 "-pie -Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
528 # Run-time tests which require working ifunc attribute support.
529 if { ![check_ifunc_attribute_available] } {
533 run_cc_link_tests [list \
543 "Build libpr18808.so" \
551 "Build libpr18808n.so" \
552 "-shared -Wl,-z,now" \
567 "Build libpr18841b.so" \
575 "Build libpr18841c.so" \
583 "Build libpr18841bn.so" \
584 "-Wl,-z,now -shared" \
591 "Build libpr18841cn.so" \
593 "-Wl,-z,now -fPIC -O0 -g" \
599 "Build libpr23169a.so" \
607 "Build libpr23169b.so" \
608 "-shared -Wl,-z,now" \
616 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
617 "$NOPIE_CFLAGS -O2 -g" \
618 { pr23169b.c pr23169c.c } \
619 {{readelf {--dyn-syms} pr23169a.rd} \
620 {readelf {-r -W} pr23169b.rd}} \
625 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
627 { pr23169b.c pr23169c.c } \
628 {{readelf {--dyn-syms} pr23169c.rd} \
629 {readelf {-r -W} pr23169b.rd}} \
634 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
636 { pr23169b.c pr23169c.c } \
637 {{readelf {--dyn-syms} pr23169c.rd} \
638 {readelf {-r -W} pr23169b.rd}} \
643 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
644 "$NOPIE_CFLAGS -O2 -g" \
645 { pr23169b.c pr23169c.c } \
646 {{readelf {--dyn-syms} pr23169a.rd} \
647 {readelf {-r -W} pr23169b.rd}} \
652 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
654 { pr23169b.c pr23169c.c } \
655 {{readelf {--dyn-syms} pr23169c.rd} \
656 {readelf {-r -W} pr23169b.rd}} \
661 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
663 { pr23169b.c pr23169c.c } \
664 {{readelf {--dyn-syms} pr23169c.rd} \
665 {readelf {-r -W} pr23169b.rd}} \
670 run_ld_link_exec_tests [list \
673 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
680 "Run pr18808 (-z now)" \
681 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808n.so" \
688 "Run pr18841 with libpr18841b.so" \
689 "-Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
696 "Run pr18841 with libpr18841c.so" \
697 "-Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
704 "Run pr18841 with libpr18841bn.so (-z now)" \
705 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841bn.so" \
712 "Run pr18841 with libpr18841cn.so (-z now)" \
713 "-Wl,-z,now -Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841cn.so" \
721 # The pr23169 testcase is not valid. In general, you can't call ifunc
722 # resolvers in another binary unless you know what you're doing. In
723 # particular you must ensure that the binary containing the resolver
724 # is relocated before the resolver is called (for example, the
725 # function addresses returned by the resolver may be loaded from the
727 # That does not happen for the pr23169 testcase where the resolver is
728 # in the executable (which is relocated last by ld.so).
730 && !([istarget "powerpc-*-*"]
731 || [istarget "aarch64*-*-*"]
732 || [istarget "sparc*-*-*"]) } {
733 run_ld_link_exec_tests [list \
736 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
738 { pr23169b.c pr23169c.c } \
741 "$NOPIE_CFLAGS -O2 -g" \
745 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
747 { pr23169b.c pr23169c.c } \
754 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
756 { pr23169b.c pr23169c.c } \
763 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
765 { pr23169b.c pr23169c.c } \
768 "$NOPIE_CFLAGS -O2 -g" \
772 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
774 { pr23169b.c pr23169c.c } \
781 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
783 { pr23169b.c pr23169c.c } \
789 if { $STATIC_PIE_LDFLAGS != "" } then {
790 run_ld_link_exec_tests [list \
793 "$STATIC_PIE_LDFLAGS" \
795 { pr23169a.c pr23169b.c pr23169c.c } \
804 set ASFLAGS "$saved_ASFLAGS"