1 # Expect script for linker support of IFUNC symbols and relocations.
3 # Copyright (C) 2009-2016 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 && ![istarget "*-*-*aout*"]
39 && ![istarget "*-*-*oldld*"]))) } {
40 verbose "IFUNC tests not run - target does not support IFUNC"
44 # We need a native system. FIXME: Strictly speaking this
45 # is not true, we just need to know how to create a fully
46 # linked executable, including the C and Z libraries, using
47 # the linker that is under test.
49 verbose "IFUNC tests not run - not a native toolchain"
53 # We need a working compiler. (Strictly speaking this is
54 # not true, we could use target specific assembler files).
55 if { [which $CC] == 0 } {
56 verbose "IFUNC tests not run - no compiler available"
60 # A procedure to check the OS/ABI field in the ELF header of a binary file.
61 proc check_osabi { binary_file expected_osabi } {
65 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
67 if ![string match "" $got] then {
68 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
72 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
73 [file_contents readelf.out] nil osabi] } {
74 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
78 if { $osabi == $expected_osabi } {
82 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
87 # A procedure to confirm that a file contains the IFUNC symbol.
88 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
89 proc contains_ifunc_symbol { binary_file } {
93 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
95 if ![string match "" $got] then {
96 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
100 # Look for a line like this:
101 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
102 # with perhaps some other info between the visibility and section
104 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
111 # A procedure to confirm that a file contains the R_*_IRELATIVE
113 # Returns -1 upon error, 0 if the relocation was not found and 1 if
115 proc contains_irelative_reloc { binary_file } {
119 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
121 if ![string match "" $got] then {
122 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
126 # Look for a line like this:
127 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
128 # 080496f4 0000002a R_386_IRELATIVE
131 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
138 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
139 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
140 proc contains_ifunc_reloc { binary_file } {
144 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
146 if ![string match "" $got] then {
147 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
151 if [string match "" [file_contents readelf.out]] then {
152 verbose "No relocs found in $binary_file"
156 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
165 # Create the object files, libraries and executables.
166 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
167 fail "Could not create a PIC object file"
168 set fails [expr $fails + 1]
170 if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
171 fail "Could not create a non-PIC object file"
172 set fails [expr $fails + 1]
174 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
175 fail "Could not create a PIC object file containing an IFUNC symbol"
176 set fails [expr $fails + 1]
178 if ![ld_compile "$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
179 fail "Could not create a non-PIC object file containing an IFUNC symbol"
180 set fails [expr $fails + 1]
182 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
183 fail "Could not create an ordinary non-PIC object file"
184 set fails [expr $fails + 1]
186 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
187 fail "Could not create an empty object file"
188 set fails [expr $fails + 1]
190 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
191 fail "Could not create test-1.o"
192 set fails [expr $fails + 1]
194 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
195 fail "Could not create test-2.o"
196 set fails [expr $fails + 1]
203 if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
204 fail "Could not create a shared library containing an IFUNC symbol"
205 set fails [expr $fails + 1]
207 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
208 fail "Could not create a static library containing an IFUNC symbol"
209 set fails [expr $fails + 1]
216 if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
217 fail "Could not link a dynamic executable"
218 set fails [expr $fails + 1]
220 if ![default_ld_link $ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
221 fail "Could not link a dynamic executable using local ifunc"
222 set fails [expr $fails + 1]
224 if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
225 fail "Could not link a static executable"
226 set fails [expr $fails + 1]
228 if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
229 fail "Could not link a non-ifunc using static executable"
230 set fails [expr $fails + 1]
232 if ![default_ld_link $ld "tmpdir/test-1" "tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
233 fail "Could not link test-1"
234 set fails [expr $fails + 1]
236 if ![ld_simple_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
237 fail "Could not link libtest-2.so"
238 set fails [expr $fails + 1]
242 pass "Building ifunc binaries"
248 # Check the executables and shared libraries
250 # The linked ifunc using executables and the shared library containing
251 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
252 # executable should have an OSABI field of NONE (aka System V).
254 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
255 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
256 set fails [expr $fails + 1]
258 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
259 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
260 set fails [expr $fails + 1]
262 if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
263 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
264 set fails [expr $fails + 1]
266 if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
267 fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
268 set fails [expr $fails + 1]
270 if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} {
271 fail "Static non-ifunc-using executable does not have an OS/ABI field of System V"
272 set fails [expr $fails + 1]
275 # The linked ifunc using executables and the shared library containing
276 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
279 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
280 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
281 set fails [expr $fails + 1]
283 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
284 fail "Local ifunc-using executable does not contain an IFUNC symbol"
285 set fails [expr $fails + 1]
287 if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
288 fail "Static ifunc-using executable does not contain an IFUNC symbol"
289 set fails [expr $fails + 1]
291 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
292 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
293 set fails [expr $fails + 1]
295 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
296 fail "Static non-ifunc-using executable contains an IFUNC symbol"
297 set fails [expr $fails + 1]
299 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
300 fail "test-1 contains IFUNC symbols"
301 set fails [expr $fails + 1]
303 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
304 fail "libtest-2.so contains IFUNC symbols"
305 set fails [expr $fails + 1]
308 # The linked ifunc using executables and shared libraries should contain
309 # a dynamic reloc referencing the IFUNC symbol. (Even the static
310 # executable which should have a dynamic section created for it). The
311 # non-ifunc using executable should not.
313 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
314 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
315 set fails [expr $fails + 1]
317 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
318 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
319 set fails [expr $fails + 1]
321 if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
322 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
323 set fails [expr $fails + 1]
325 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
326 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
327 set fails [expr $fails + 1]
329 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
330 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
331 set fails [expr $fails + 1]
335 pass "Checking ifunc binaries"
338 # Clean up, unless we are being verbose, in which case we leave the files available.
339 if { $verbose < 1 } {
340 remote_file host delete "tmpdir/shared_prog.o"
341 remote_file host delete "tmpdir/static_prog.o"
342 remote_file host delete "tmpdir/shared_ifunc.o"
343 remote_file host delete "tmpdir/static_ifunc.o"
344 remote_file host delete "tmpdir/static_noifunc.o"
345 remote_file host delete "tmpdir/libshared_ifunc.so"
346 remote_file host delete "tmpdir/libifunc.a"
347 remote_file host delete "tmpdir/dynamic_prog"
348 remote_file host delete "tmpdir/local_prog"
349 remote_file host delete "tmpdir/static_prog"
350 remote_file host delete "tmpdir/static_nonifunc_prog"
353 run_cc_link_tests [list \
355 "Build libpr16467a.so" \
356 "-shared -Wl,--version-script=pr16467a.map" \
363 "Build libpr16467b.a" \
371 "Build libpr16467b.so" \
372 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
373 -Wl,--version-script=pr16467b.map" \
380 "Build libpr16467c.a" \
389 run_ld_link_exec_tests [] [list \
391 "Common symbol override ifunc test 1a" \
394 { ifunc-common-1a.c ifunc-common-1b.c } \
396 "ifunc-common-1.out" \
400 "Common symbol override ifunc test 1b" \
403 { ifunc-common-1b.c ifunc-common-1a.c } \
405 "ifunc-common-1.out" \
410 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
411 foreach t $test_list {
412 # We need to strip the ".d", but can leave the dirname.
413 verbose [file rootname $t]
414 run_dump_test [file rootname $t]
417 # Run-time tests which require working IFUNC support.
418 if { ![check_ifunc_available] } {
422 run_cc_link_tests [list \
424 "Build ifunc-lib.so" \
433 run_ld_link_exec_tests [] [list \
436 "tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
445 "tmpdir/libifunc-lib.so" \
452 "Run ifunc-main with -fpic" \
453 "tmpdir/libifunc-lib.so" \
462 # Run-time tests which require working ifunc attribute support.
463 if { ![check_ifunc_attribute_available] } {
467 run_cc_link_tests [list \
477 "Build libpr18808.so" \
493 "Build libpr18841b.so" \
501 "Build libpr18841c.so" \
509 "Build libpr19784a.so" \
510 "-shared -Wl,-Bsymbolic-functions" \
512 { pr19784b.c pr19784c.c } \
517 "Build libpr19784b.so" \
518 "-shared -Wl,-Bsymbolic-functions" \
520 { pr19784c.c pr19784b.c } \
534 run_ld_link_exec_tests [] [list \
537 "tmpdir/pr18808a.o tmpdir/libpr18808.so" \
544 "Run pr18841 with libpr18841b.so" \
545 "tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
552 "Run pr18841 with libpr18841c.so" \
553 "--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
561 "tmpdir/pr19784a.o tmpdir/libpr19784a.so" \
569 "--as-needed tmpdir/pr19784a.o tmpdir/libpr19784b.so" \