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 alpha-*-*]
28 || [istarget arc*-*-*]
29 || [istarget am33*-*-*]
30 || [istarget bfin-*-*]
31 || [istarget cris*-*-*]
33 || [istarget lm32-*-*]
34 || [istarget m32r-*-*]
35 || [istarget m68k-*-*]
36 || [istarget microblaze-*-*]
37 || [istarget mips*-*-*]
38 || [istarget mn10300-*-*]
39 || [istarget nds32*-*-*]
40 || [istarget nios2-*-*]
41 || [istarget or1k-*-*]
42 || [istarget riscv*-*-*]
43 || [istarget score*-*-*]
45 || [istarget tic6x-*-*]
46 || [istarget tile*-*-*]
47 || [istarget vax-*-*] } {
48 verbose "IFUNC tests not run - target does not support IFUNC"
52 # Skip targets where -shared is not supported
54 if ![check_shared_lib_support] {
58 set saved_ASFLAGS "$ASFLAGS"
59 if { [istarget "i?86-*-*"] || [istarget "x86_64-*-*"] } {
60 set ASFLAGS "$ASFLAGS -mx86-used-note=no"
63 # This test does not need a compiler...
64 run_dump_test "ifuncmod5"
66 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
67 foreach t $test_list {
68 # We need to strip the ".d", but can leave the dirname.
69 verbose [file rootname $t]
70 run_dump_test [file rootname $t]
73 # We need a working compiler. (Strictly speaking this is
74 # not true, we could use target specific assembler files).
75 if { ![check_compiler_available] } {
76 verbose "IFUNC tests not run - no compiler available"
80 # A procedure to check the OS/ABI field in the ELF header of a binary file.
81 proc check_osabi { binary_file expected_osabi } {
85 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
87 if ![string match "" $got] then {
88 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
92 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
93 [file_contents readelf.out] nil osabi] } {
94 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
98 if { $osabi == $expected_osabi } {
102 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
107 # A procedure to confirm that a file contains the IFUNC symbol.
108 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
109 proc contains_ifunc_symbol { binary_file } {
113 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
115 if ![string match "" $got] then {
116 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
120 # Look for a line like this:
121 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
122 # with perhaps some other info between the visibility and section
124 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
131 # A procedure to confirm that a file contains the R_*_IRELATIVE
133 # Returns -1 upon error, 0 if the relocation was not found and 1 if
135 proc contains_irelative_reloc { binary_file } {
139 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
141 if ![string match "" $got] then {
142 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
146 # Look for a line like this:
147 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
148 # 080496f4 0000002a R_386_IRELATIVE
151 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_(\[_0-9A-Z\]+_IREL(|ATIVE)|PARISC_IPLT)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
158 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
159 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
160 proc contains_ifunc_reloc { binary_file } {
164 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
166 if ![string match "" $got] then {
167 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
171 if [string match "" [file_contents readelf.out]] then {
172 verbose "No relocs found in $binary_file"
176 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
185 # Create the object files, libraries and executables.
186 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
187 fail "Could not create a PIC object file"
188 set fails [expr $fails + 1]
190 if ![ld_compile "$CC -c $NOPIE_CFLAGS" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
191 fail "Could not create a non-PIC object file"
192 set fails [expr $fails + 1]
194 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
195 fail "Could not create a PIC object file containing an IFUNC symbol"
196 set fails [expr $fails + 1]
198 if ![ld_compile "$CC -c $NOPIE_CFLAGS -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
199 fail "Could not create a non-PIC object file containing an IFUNC symbol"
200 set fails [expr $fails + 1]
202 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
203 fail "Could not create an ordinary non-PIC object file"
204 set fails [expr $fails + 1]
206 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
207 fail "Could not create an empty object file"
208 set fails [expr $fails + 1]
210 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
211 fail "Could not create test-1.o"
212 set fails [expr $fails + 1]
214 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
215 fail "Could not create test-2.o"
216 set fails [expr $fails + 1]
223 if ![ld_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
224 fail "Could not create a shared library containing an IFUNC symbol"
225 set fails [expr $fails + 1]
227 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
228 fail "Could not create a static library containing an IFUNC symbol"
229 set fails [expr $fails + 1]
236 if ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
237 fail "Could not link a dynamic executable"
238 set fails [expr $fails + 1]
240 if ![ld_link $CC "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
241 fail "Could not link a dynamic executable using local ifunc"
242 set fails [expr $fails + 1]
244 if ![string match "" $STATIC_LDFLAGS] {
245 if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
246 fail "Could not link a static executable"
247 set fails [expr $fails + 1]
250 if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
251 fail "Could not link a non-ifunc using static executable"
252 set fails [expr $fails + 1]
254 if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
255 fail "Could not link test-1"
256 set fails [expr $fails + 1]
258 if ![ld_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
259 fail "Could not link libtest-2.so"
260 set fails [expr $fails + 1]
262 if ![ld_link $ld "tmpdir/libtest-2-now.so" "-shared -z now tmpdir/test-2.o"] {
263 fail "Could not link libtest-2-now.so"
264 set fails [expr $fails + 1]
268 pass "Building ifunc binaries"
274 # Check the executables and shared libraries
276 # The linked ifunc using executables and the shared library containing
277 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
278 # executable should have an OSABI field of NONE (aka System V).
280 switch -glob $target_triplet {
281 hppa*-*-linux* { set expected_none {UNIX - GNU} }
282 default { set expected_none {UNIX - System V} }
285 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
286 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
287 set fails [expr $fails + 1]
289 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
290 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
291 set fails [expr $fails + 1]
293 if { ![string match "" $STATIC_LDFLAGS] \
294 && ![check_osabi tmpdir/static_prog {UNIX - GNU}]} {
295 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
296 set fails [expr $fails + 1]
298 if {! [check_osabi tmpdir/dynamic_prog $expected_none]} {
299 fail "Dynamic ifunc-using executable does not have an OS/ABI field of $expected_none"
300 set fails [expr $fails + 1]
302 if {! [check_osabi tmpdir/static_nonifunc_prog $expected_none]} {
303 fail "Static non-ifunc-using executable does not have an OS/ABI field of $expected_none"
304 set fails [expr $fails + 1]
307 # The linked ifunc using executables and the shared library containing
308 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
311 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
312 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
313 set fails [expr $fails + 1]
315 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
316 fail "Local ifunc-using executable does not contain an IFUNC symbol"
317 set fails [expr $fails + 1]
319 if { ![string match "" $STATIC_LDFLAGS] \
320 && [contains_ifunc_symbol tmpdir/static_prog] != 1} {
321 fail "Static ifunc-using executable does not contain an IFUNC symbol"
322 set fails [expr $fails + 1]
324 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
325 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
326 set fails [expr $fails + 1]
328 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
329 fail "Static non-ifunc-using executable contains an IFUNC symbol"
330 set fails [expr $fails + 1]
332 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
333 fail "test-1 contains IFUNC symbols"
334 set fails [expr $fails + 1]
336 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
337 fail "libtest-2.so contains IFUNC symbols"
338 set fails [expr $fails + 1]
340 if {[contains_ifunc_symbol tmpdir/libtest-2-now.so] != 0} {
341 fail "libtest-2-now.so contains IFUNC symbols"
342 set fails [expr $fails + 1]
345 # The linked ifunc using executables and shared libraries should contain
346 # a dynamic reloc referencing the IFUNC symbol. (Even the static
347 # executable which should have a dynamic section created for it). The
348 # non-ifunc using executable should not.
350 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
351 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
352 set fails [expr $fails + 1]
354 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
355 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
356 set fails [expr $fails + 1]
358 if { ![string match "" $STATIC_LDFLAGS] \
359 && [contains_irelative_reloc tmpdir/static_prog] != 1} {
360 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
361 set fails [expr $fails + 1]
363 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
364 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
365 set fails [expr $fails + 1]
367 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
368 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
369 set fails [expr $fails + 1]
373 pass "Checking ifunc binaries"
376 run_cc_link_tests [list \
378 "Build libpr16467a.so" \
379 "-shared -Wl,--version-script=pr16467a.map" \
386 "Build libpr16467b.a" \
394 "Build libpr16467b.so" \
395 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
396 -Wl,--version-script=pr16467b.map" \
403 "Build libpr16467c.a" \
411 "Build libpr16467an.so" \
412 "-shared -Wl,-z,now -Wl,--version-script=pr16467a.map" \
419 "Build libpr16467bn.so" \
420 "-shared tmpdir/pr16467b.o tmpdir/libpr16467an.so \
421 -Wl,--version-script=pr16467b.map" \
429 run_ld_link_exec_tests [list \
431 "Common symbol override ifunc test 1a" \
434 { ifunc-common-1a.c ifunc-common-1b.c } \
436 "ifunc-common-1.out" \
440 "Common symbol override ifunc test 1b" \
443 { ifunc-common-1b.c ifunc-common-1a.c } \
445 "ifunc-common-1.out" \
450 # Run-time tests which require working IFUNC support.
451 if { ![check_ifunc_available] } {
455 run_cc_link_tests [list \
457 "Build ifunc-lib.so" \
465 "Build ifunc-libn.so" \
466 "-shared -Wl,-z,now" \
474 run_ld_link_exec_tests [list \
477 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
485 "Run pr16467 (-z now)" \
486 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467bn.so tmpdir/libpr16467an.so" \
495 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
502 "Run ifunc-main with -fpic" \
503 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
511 "Run ifunc-main (-z now)" \
512 "-Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
519 "Run ifunc-main with PIE (-z now)" \
520 "-pie -Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
529 # Run-time tests which require working ifunc attribute support.
530 if { ![check_ifunc_attribute_available] } {
534 run_cc_link_tests [list \
544 "Build libpr18808.so" \
552 "Build libpr18808n.so" \
553 "-shared -Wl,-z,now" \
568 "Build libpr18841b.so" \
576 "Build libpr18841c.so" \
584 "Build libpr18841bn.so" \
585 "-Wl,-z,now -shared" \
592 "Build libpr18841cn.so" \
594 "-Wl,-z,now -fPIC -O0 -g" \
600 "Build libpr23169a.so" \
608 "Build libpr23169b.so" \
609 "-shared -Wl,-z,now" \
617 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
618 "$NOPIE_CFLAGS -O2 -g" \
619 { pr23169b.c pr23169c.c } \
620 {{readelf {--dyn-syms} pr23169a.rd} \
621 {readelf {-r -W} pr23169b.rd}} \
626 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
628 { pr23169b.c pr23169c.c } \
629 {{readelf {--dyn-syms} pr23169c.rd} \
630 {readelf {-r -W} pr23169b.rd}} \
635 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
637 { pr23169b.c pr23169c.c } \
638 {{readelf {--dyn-syms} pr23169c.rd} \
639 {readelf {-r -W} pr23169b.rd}} \
644 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
645 "$NOPIE_CFLAGS -O2 -g" \
646 { pr23169b.c pr23169c.c } \
647 {{readelf {--dyn-syms} pr23169a.rd} \
648 {readelf {-r -W} pr23169b.rd}} \
653 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
655 { pr23169b.c pr23169c.c } \
656 {{readelf {--dyn-syms} pr23169c.rd} \
657 {readelf {-r -W} pr23169b.rd}} \
662 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
664 { pr23169b.c pr23169c.c } \
665 {{readelf {--dyn-syms} pr23169c.rd} \
666 {readelf {-r -W} pr23169b.rd}} \
671 run_ld_link_exec_tests [list \
674 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
681 "Run pr18808 (-z now)" \
682 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808n.so" \
689 "Run pr18841 with libpr18841b.so" \
690 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
697 "Run pr18841 with libpr18841c.so" \
698 "$NOPIE_LDFLAGS -Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
705 "Run pr18841 with libpr18841bn.so (-z now)" \
706 "$NOPIE_LDFLAGS -Wl,-z,now -Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841bn.so" \
713 "Run pr18841 with libpr18841cn.so (-z now)" \
714 "$NOPIE_LDFLAGS -Wl,-z,now -Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841cn.so" \
722 # The pr23169 testcase is not valid. In general, you can't call ifunc
723 # resolvers in another binary unless you know what you're doing. In
724 # particular you must ensure that the binary containing the resolver
725 # is relocated before the resolver is called (for example, the
726 # function addresses returned by the resolver may be loaded from the
728 # That does not happen for the pr23169 testcase where the resolver is
729 # in the executable (which is relocated last by ld.so).
731 && !([istarget "powerpc-*-*"]
732 || [istarget "aarch64*-*-*"]
733 || [istarget "sparc*-*-*"]) } {
734 run_ld_link_exec_tests [list \
737 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
739 { pr23169b.c pr23169c.c } \
742 "$NOPIE_CFLAGS -O2 -g" \
746 "-pie -Wl,--no-as-needed tmpdir/libpr23169a.so" \
748 { pr23169b.c pr23169c.c } \
755 "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libpr23169a.so" \
757 { pr23169b.c pr23169c.c } \
764 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
766 { pr23169b.c pr23169c.c } \
769 "$NOPIE_CFLAGS -O2 -g" \
773 "-pie -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
775 { pr23169b.c pr23169c.c } \
782 "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,now tmpdir/libpr23169b.so" \
784 { pr23169b.c pr23169c.c } \
790 if { $STATIC_PIE_LDFLAGS != "" } then {
791 run_ld_link_exec_tests [list \
794 "$STATIC_PIE_LDFLAGS" \
796 { pr23169a.c pr23169b.c pr23169c.c } \
805 set ASFLAGS "$saved_ASFLAGS"