1 # Expect script
for linker support of IFUNC symbols and relocations.
3 # Copyright
2009, 2010 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
,
28 if {!(([istarget
"i?86-*-*"]
29 ||
[istarget
"x86_64-*-*"]
30 ||
[istarget
"powerpc*-*-*"]
31 ||
[istarget
"sparc*-*-*"])
32 && ([istarget
"*-*-elf*"]
33 ||
([istarget
"*-*-linux*"]
34 && ![istarget
"*-*-*aout*"]
35 && ![istarget
"*-*-*oldld*"])
36 ||
([istarget
"*-*-gnu*"]))) } {
37 verbose
"IFUNC tests not run - target does not support IFUNC"
41 # We need a native
system. FIXME
: Strictly speaking this
42 # is not true
, we just need to know how to create a fully
43 # linked executable
, including the C and Z libraries
, using
44 # the linker that is under test.
46 verbose
"IFUNC tests not run - not a native toolchain"
50 # We need a working compiler.
(Strictly speaking this is
51 # not true
, we could use target specific assembler files
).
52 if { [which $CC
] == 0 } {
53 verbose
"IFUNC tests not run - no compiler available"
57 # A procedure to check the OS
/ABI field in the ELF header of a binary file.
58 proc check_osabi
{ binary_file expected_osabi
} {
62 catch
"exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
64 if ![string match
"" $got] then {
65 verbose
"proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
69 if { ![regexp
"\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
70 [file_contents readelf.out
] nil osabi
] } {
71 verbose
"proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
75 if { $osabi
== $expected_osabi
} {
79 verbose
"Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
84 # A procedure to confirm that a file contains the IFUNC symbol.
85 # Returns
-1 upon error
, 0 if the symbol was not found and
1 if it was found.
86 proc contains_ifunc_symbol
{ binary_file
} {
90 catch
"exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
92 if ![string match
"" $got] then {
93 verbose
"proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
97 # Look
for a line like this
:
98 #
58: 0000000000400600 30 IFUNC GLOBAL DEFAULT
12 library_func2
100 if { ![regexp
".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
107 # A procedure to confirm that a file contains the R_
*_IRELATIVE
109 # Returns
-1 upon error
, 0 if the relocation was not found and
1 if
111 proc contains_irelative_reloc
{ binary_file
} {
115 catch
"exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
117 if ![string match
"" $got] then {
118 verbose
"proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
122 # Look
for a line like this
:
123 #
0000000000600ab0
0000000000000025 R_X86_64_IRELATIVE
000000000040061c
124 #
080496f4
0000002a R_386_IRELATIVE
127 if { ![regexp
"\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
134 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
135 # Returns
-1 upon error
, 0 if the reloc was not found and
1 if it was found.
136 proc contains_ifunc_reloc
{ binary_file
} {
140 catch
"exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
142 if ![string match
"" $got] then {
143 verbose
"proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
147 if [string match
"" [file_contents readelf.out]] then {
148 verbose
"No relocs found in $binary_file"
152 if { ![regexp
"\\(\\)" [file_contents readelf.out]] } {
161 # Create the object files
, libraries and executables.
162 if ![ld_compile
"$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
163 fail
"Could not create a PIC object file"
164 set fails
[expr $fails
+ 1]
166 if ![ld_compile
"$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
167 fail
"Could not create a non-PIC object file"
168 set fails
[expr $fails
+ 1]
170 if ![ld_compile
"$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
171 fail
"Could not create a PIC object file containing an IFUNC symbol"
172 set fails
[expr $fails
+ 1]
174 if ![ld_compile
"$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
175 fail
"Could not create a non-PIC object file containing an IFUNC symbol"
176 set fails
[expr $fails
+ 1]
178 if ![ld_compile
"$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
179 fail
"Could not create an ordinary non-PIC object file"
180 set fails
[expr $fails
+ 1]
182 if ![ld_assemble $as
"$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
183 fail
"Could not create an empty object file"
184 set fails
[expr $fails
+ 1]
186 if ![ld_compile
"$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
187 fail
"Could not create test-1.o"
188 set fails
[expr $fails
+ 1]
190 if ![ld_compile
"$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
191 fail
"Could not create test-2.o"
192 set fails
[expr $fails
+ 1]
199 if ![ld_simple_link $
ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
200 fail
"Could not create a shared library containing an IFUNC symbol"
201 set fails
[expr $fails
+ 1]
203 if ![ar_simple_create $ar
"" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
204 fail
"Could not create a static library containing an IFUNC symbol"
205 set fails
[expr $fails
+ 1]
212 if ![default_ld_link $
ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
213 fail
"Could not link a dynamic executable"
214 set fails
[expr $fails
+ 1]
216 if ![default_ld_link $
ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
217 fail
"Could not link a dynamic executable using local ifunc"
218 set fails
[expr $fails
+ 1]
220 if ![default_ld_link $
ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
221 fail
"Could not link a static executable"
222 set fails
[expr $fails
+ 1]
224 if ![ld_simple_link $
ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
225 fail
"Could not link a non-ifunc using static executable"
226 set fails
[expr $fails
+ 1]
228 if ![default_ld_link $
ld "tmpdir/test-1" "tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
229 fail
"Could not link test-1"
230 set fails
[expr $fails
+ 1]
232 if ![ld_simple_link $
ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
233 fail
"Could not link libtest-2.so"
234 set fails
[expr $fails
+ 1]
238 pass
"Building ifunc binaries"
244 # Check the executables and shared libraries
246 # The linked ifunc using executables and the shared library containing
247 # ifunc should have an OSABI field of LINUX. The linked non
-ifunc using
248 # executable should have an OSABI field of
NONE (aka
System V
).
250 if {! [check_osabi tmpdir
/libshared_ifunc.so
{UNIX
- Linux
}]} {
251 fail
"Shared libraries containing ifunc does not have an OS/ABI field of LINUX"
252 set fails
[expr $fails
+ 1]
254 if {! [check_osabi tmpdir
/local_prog
{UNIX
- Linux
}]} {
255 fail
"Local ifunc-using executable does not have an OS/ABI field of LINUX"
256 set fails
[expr $fails
+ 1]
258 if {! [check_osabi tmpdir
/static_prog
{UNIX
- Linux
}]} {
259 fail
"Static ifunc-using executable does not have an OS/ABI field of LINUX"
260 set fails
[expr $fails
+ 1]
262 if {! [check_osabi tmpdir
/dynamic_prog
{UNIX
- System V
}]} {
263 fail
"Dynamic ifunc-using executable does not have an OS/ABI field of System V"
264 set fails
[expr $fails
+ 1]
266 if {! [check_osabi tmpdir
/static_nonifunc_prog
{UNIX
- System V
}]} {
267 fail
"Static non-ifunc-using executable does not have an OS/ABI field of System V"
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 # Clean up
, unless we are being verbose
, in which case we leave the files available.
335 if { $verbose
< 1 } {
336 remote_file host
delete "tmpdir/shared_prog.o"
337 remote_file host
delete "tmpdir/static_prog.o"
338 remote_file host
delete "tmpdir/shared_ifunc.o"
339 remote_file host
delete "tmpdir/static_ifunc.o"
340 remote_file host
delete "tmpdir/static_noifunc.o"
341 remote_file host
delete "tmpdir/libshared_ifunc.so"
342 remote_file host
delete "tmpdir/libifunc.a"
343 remote_file host
delete "tmpdir/dynamic_prog"
344 remote_file host
delete "tmpdir/local_prog"
345 remote_file host
delete "tmpdir/static_prog"
346 remote_file host
delete "tmpdir/static_nonifunc_prog"
349 set test_list
[lsort
[glob
-nocomplain $srcdir
/$subdir
/*.d
]]
350 foreach t $test_list
{
351 # We need to
strip the
".d", but can leave the dirname.
352 verbose
[file rootname $t
]
353 run_dump_test
[file rootname $t
]