1 # Expect script for linker support of IFUNC symbols and relocations.
3 # Copyright (C) 2009-2017 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 working compiler. (Strictly speaking this is
45 # not true, we could use target specific assembler files).
46 if { [which $CC] == 0 } {
47 verbose "IFUNC tests not run - no compiler available"
51 # A procedure to check the OS/ABI field in the ELF header of a binary file.
52 proc check_osabi { binary_file expected_osabi } {
56 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
58 if ![string match "" $got] then {
59 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
63 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
64 [file_contents readelf.out] nil osabi] } {
65 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
69 if { $osabi == $expected_osabi } {
73 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
78 # A procedure to confirm that a file contains the IFUNC symbol.
79 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
80 proc contains_ifunc_symbol { binary_file } {
84 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
86 if ![string match "" $got] then {
87 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
91 # Look for a line like this:
92 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
93 # with perhaps some other info between the visibility and section
95 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
102 # A procedure to confirm that a file contains the R_*_IRELATIVE
104 # Returns -1 upon error, 0 if the relocation was not found and 1 if
106 proc contains_irelative_reloc { binary_file } {
110 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
112 if ![string match "" $got] then {
113 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
117 # Look for a line like this:
118 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
119 # 080496f4 0000002a R_386_IRELATIVE
122 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
129 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
130 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
131 proc contains_ifunc_reloc { binary_file } {
135 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
137 if ![string match "" $got] then {
138 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
142 if [string match "" [file_contents readelf.out]] then {
143 verbose "No relocs found in $binary_file"
147 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
156 # Create the object files, libraries and executables.
157 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
158 fail "Could not create a PIC object file"
159 set fails [expr $fails + 1]
161 if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
162 fail "Could not create a non-PIC object file"
163 set fails [expr $fails + 1]
165 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
166 fail "Could not create a PIC object file containing an IFUNC symbol"
167 set fails [expr $fails + 1]
169 if ![ld_compile "$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
170 fail "Could not create a non-PIC object file containing an IFUNC symbol"
171 set fails [expr $fails + 1]
173 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
174 fail "Could not create an ordinary non-PIC object file"
175 set fails [expr $fails + 1]
177 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
178 fail "Could not create an empty object file"
179 set fails [expr $fails + 1]
181 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
182 fail "Could not create test-1.o"
183 set fails [expr $fails + 1]
185 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
186 fail "Could not create test-2.o"
187 set fails [expr $fails + 1]
194 if ![ld_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
195 fail "Could not create a shared library containing an IFUNC symbol"
196 set fails [expr $fails + 1]
198 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
199 fail "Could not create a static library containing an IFUNC symbol"
200 set fails [expr $fails + 1]
207 if ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
208 fail "Could not link a dynamic executable"
209 set fails [expr $fails + 1]
211 if ![ld_link $CC "tmpdir/local_prog" "-Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
212 fail "Could not link a dynamic executable using local ifunc"
213 set fails [expr $fails + 1]
215 if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
216 fail "Could not link a static executable"
217 set fails [expr $fails + 1]
219 if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
220 fail "Could not link a non-ifunc using static executable"
221 set fails [expr $fails + 1]
223 if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
224 fail "Could not link test-1"
225 set fails [expr $fails + 1]
227 if ![ld_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
228 fail "Could not link libtest-2.so"
229 set fails [expr $fails + 1]
233 pass "Building ifunc binaries"
239 # Check the executables and shared libraries
241 # The linked ifunc using executables and the shared library containing
242 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
243 # executable should have an OSABI field of NONE (aka System V).
245 case $target_triplet in {
246 { hppa*-*-linux* } { set expected_none {UNIX - GNU} }
247 default { set expected_none {UNIX - System V} }
250 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
251 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
252 set fails [expr $fails + 1]
254 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
255 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
256 set fails [expr $fails + 1]
258 if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
259 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
260 set fails [expr $fails + 1]
262 if {! [check_osabi tmpdir/dynamic_prog $expected_none]} {
263 fail "Dynamic ifunc-using executable does not have an OS/ABI field of $expected_none"
264 set fails [expr $fails + 1]
266 if {! [check_osabi tmpdir/static_nonifunc_prog $expected_none]} {
267 fail "Static non-ifunc-using executable does not have an OS/ABI field of $expected_none"
268 set fails [expr $fails + 1]
271 # The linked ifunc using executables and the shared library containing
272 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
275 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
276 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
277 set fails [expr $fails + 1]
279 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
280 fail "Local ifunc-using executable does not contain an IFUNC symbol"
281 set fails [expr $fails + 1]
283 if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
284 fail "Static ifunc-using executable does not contain an IFUNC symbol"
285 set fails [expr $fails + 1]
287 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
288 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
289 set fails [expr $fails + 1]
291 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
292 fail "Static non-ifunc-using executable contains an IFUNC symbol"
293 set fails [expr $fails + 1]
295 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
296 fail "test-1 contains IFUNC symbols"
297 set fails [expr $fails + 1]
299 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
300 fail "libtest-2.so contains IFUNC symbols"
301 set fails [expr $fails + 1]
304 # The linked ifunc using executables and shared libraries should contain
305 # a dynamic reloc referencing the IFUNC symbol. (Even the static
306 # executable which should have a dynamic section created for it). The
307 # non-ifunc using executable should not.
309 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
310 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
311 set fails [expr $fails + 1]
313 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
314 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
315 set fails [expr $fails + 1]
317 if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
318 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
319 set fails [expr $fails + 1]
321 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
322 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
323 set fails [expr $fails + 1]
325 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
326 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
327 set fails [expr $fails + 1]
331 pass "Checking ifunc binaries"
334 run_cc_link_tests [list \
336 "Build libpr16467a.so" \
337 "-shared -Wl,--version-script=pr16467a.map" \
344 "Build libpr16467b.a" \
352 "Build libpr16467b.so" \
353 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
354 -Wl,--version-script=pr16467b.map" \
361 "Build libpr16467c.a" \
370 run_ld_link_exec_tests [list \
372 "Common symbol override ifunc test 1a" \
375 { ifunc-common-1a.c ifunc-common-1b.c } \
377 "ifunc-common-1.out" \
381 "Common symbol override ifunc test 1b" \
384 { ifunc-common-1b.c ifunc-common-1a.c } \
386 "ifunc-common-1.out" \
391 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
392 foreach t $test_list {
393 # We need to strip the ".d", but can leave the dirname.
394 verbose [file rootname $t]
395 run_dump_test [file rootname $t]
398 # Run-time tests which require working IFUNC support.
399 if { ![check_ifunc_available] } {
403 run_cc_link_tests [list \
405 "Build ifunc-lib.so" \
414 run_ld_link_exec_tests [list \
417 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
426 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
433 "Run ifunc-main with -fpic" \
434 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
443 # Run-time tests which require working ifunc attribute support.
444 if { ![check_ifunc_attribute_available] } {
448 run_cc_link_tests [list \
458 "Build libpr18808.so" \
474 "Build libpr18841b.so" \
482 "Build libpr18841c.so" \
491 run_ld_link_exec_tests [list \
494 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
501 "Run pr18841 with libpr18841b.so" \
502 "-Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
509 "Run pr18841 with libpr18841c.so" \
510 "-Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \