ChangeLog rotatation and copyright year update
[deliverable/binutils-gdb.git] / ld / testsuite / ld-ifunc / ifunc.exp
CommitLineData
d8045f23
NC
1# Expect script for linker support of IFUNC symbols and relocations.
2#
b90efa5b 3# Copyright (C) 2009-2015 Free Software Foundation, Inc.
d8045f23
NC
4# Contributed by Red Hat.
5#
6# This file is part of the GNU Binutils.
7#
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.
12#
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.
17#
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,
21# MA 02110-1301, USA.
22#
23# Written by Nick Clifton <nickc@redhat.com>
24
25
d0c9aeb3 26# IFUNC support has only been implemented for the ix86, x86_64, powerpc,
1419bbe5 27# aarch64 and sparc so far.
e054468f
AM
28if {!(([istarget "i?86-*-*"]
29 || [istarget "x86_64-*-*"]
d0c9aeb3 30 || [istarget "powerpc*-*-*"]
1419bbe5 31 || [istarget "aarch64*-*-*"]
d0c9aeb3 32 || [istarget "sparc*-*-*"])
e054468f 33 && ([istarget "*-*-elf*"]
5a68afcf 34 || [istarget "*-*-nacl*"]
99753d9d
AM
35 || (([istarget "*-*-linux*"]
36 || [istarget "*-*-gnu*"])
e054468f 37 && ![istarget "*-*-*aout*"]
99753d9d 38 && ![istarget "*-*-*oldld*"]))) } {
d8045f23
NC
39 verbose "IFUNC tests not run - target does not support IFUNC"
40 return
41}
42
43# We need a native system. FIXME: Strictly speaking this
44# is not true, we just need to know how to create a fully
45# linked executable, including the C and Z libraries, using
46# the linker that is under test.
47if ![isnative] {
48 verbose "IFUNC tests not run - not a native toolchain"
49 return
50}
51
52# We need a working compiler. (Strictly speaking this is
53# not true, we could use target specific assembler files).
54if { [which $CC] == 0 } {
55 verbose "IFUNC tests not run - no compiler available"
56 return
57}
58
59# A procedure to check the OS/ABI field in the ELF header of a binary file.
60proc check_osabi { binary_file expected_osabi } {
61 global READELF
62 global READELFFLAGS
63
64 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
65
66 if ![string match "" $got] then {
67 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
68 return 0
69 }
70
71 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
72 [file_contents readelf.out] nil osabi] } {
73 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
74 return 0
75 }
76
77 if { $osabi == $expected_osabi } {
78 return 1
79 }
80
81 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
5a68afcf 82
d8045f23
NC
83 return 0
84}
85
86# A procedure to confirm that a file contains the IFUNC symbol.
87# Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
88proc contains_ifunc_symbol { binary_file } {
89 global READELF
90 global READELFFLAGS
91
92 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
93
94 if ![string match "" $got] then {
95 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
96 return -1
97 }
98
99 # Look for a line like this:
100 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
4115bfc6 101 # with perhaps some other info between the visibility and section
d8045f23 102
4115bfc6 103 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
d8045f23
NC
104 return 0
105 }
106
107 return 1
108}
109
cbe950e9
L
110# A procedure to confirm that a file contains the R_*_IRELATIVE
111# relocation.
112# Returns -1 upon error, 0 if the relocation was not found and 1 if
113# it was found.
114proc contains_irelative_reloc { binary_file } {
115 global READELF
116 global READELFFLAGS
117
118 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
119
120 if ![string match "" $got] then {
121 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
122 return -1
123 }
124
125 # Look for a line like this:
126 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
127 # 080496f4 0000002a R_386_IRELATIVE
128
129
425621e7 130 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
cbe950e9
L
131 return 0
132 }
133
134 return 1
135}
136
d8045f23
NC
137# A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
138# Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
139proc contains_ifunc_reloc { binary_file } {
140 global READELF
141 global READELFFLAGS
142
143 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
144
145 if ![string match "" $got] then {
146 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
147 return -1
148 }
149
150 if [string match "" [file_contents readelf.out]] then {
151 verbose "No relocs found in $binary_file"
152 return 0
153 }
154
155 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
156 return 0
157 }
158
159 return 1
160}
161
162set fails 0
163
164# Create the object files, libraries and executables.
be19bd51
L
165if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
166 fail "Could not create a PIC object file"
d8045f23
NC
167 set fails [expr $fails + 1]
168}
be19bd51
L
169if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
170 fail "Could not create a non-PIC object file"
d8045f23
NC
171 set fails [expr $fails + 1]
172}
be19bd51
L
173if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
174 fail "Could not create a PIC object file containing an IFUNC symbol"
d8045f23
NC
175 set fails [expr $fails + 1]
176}
be19bd51
L
177if ![ld_compile "$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
178 fail "Could not create a non-PIC object file containing an IFUNC symbol"
d8045f23
NC
179 set fails [expr $fails + 1]
180}
be19bd51
L
181if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
182 fail "Could not create an ordinary non-PIC object file"
183 set fails [expr $fails + 1]
184}
185if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
186 fail "Could not create an empty object file"
d8045f23
NC
187 set fails [expr $fails + 1]
188}
2955ec4c
L
189if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
190 fail "Could not create test-1.o"
191 set fails [expr $fails + 1]
192}
193if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
194 fail "Could not create test-2.o"
195 set fails [expr $fails + 1]
196}
d8045f23
NC
197
198if { $fails != 0 } {
199 return
200}
201
202if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
203 fail "Could not create a shared library containing an IFUNC symbol"
204 set fails [expr $fails + 1]
205}
206if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
207 fail "Could not create a static library containing an IFUNC symbol"
208 set fails [expr $fails + 1]
209}
210
211if { $fails != 0 } {
212 return
213}
214
215if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
216 fail "Could not link a dynamic executable"
217 set fails [expr $fails + 1]
218}
e054468f
AM
219if ![default_ld_link $ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
220 fail "Could not link a dynamic executable using local ifunc"
221 set fails [expr $fails + 1]
222}
223if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
d8045f23
NC
224 fail "Could not link a static executable"
225 set fails [expr $fails + 1]
226}
be19bd51 227if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
d8045f23
NC
228 fail "Could not link a non-ifunc using static executable"
229 set fails [expr $fails + 1]
230}
2955ec4c
L
231if ![default_ld_link $ld "tmpdir/test-1" "tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
232 fail "Could not link test-1"
233 set fails [expr $fails + 1]
234}
235if ![ld_simple_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
236 fail "Could not link libtest-2.so"
237 set fails [expr $fails + 1]
238}
d8045f23
NC
239
240if { $fails == 0 } {
241 pass "Building ifunc binaries"
242 set fails 0
243} else {
244 return
245}
246
cbe950e9 247# Check the executables and shared libraries
d8045f23 248#
cbe950e9 249# The linked ifunc using executables and the shared library containing
9c55345c 250# ifunc should have an OSABI field of GNU. The linked non-ifunc using
cbe950e9 251# executable should have an OSABI field of NONE (aka System V).
d8045f23 252
9c55345c
TS
253if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
254 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
cbe950e9
L
255 set fails [expr $fails + 1]
256}
9c55345c
TS
257if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
258 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
e054468f
AM
259 set fails [expr $fails + 1]
260}
9c55345c
TS
261if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
262 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
d8045f23
NC
263 set fails [expr $fails + 1]
264}
c16153ae
L
265if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
266 fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
d8045f23
NC
267 set fails [expr $fails + 1]
268}
be19bd51
L
269if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} {
270 fail "Static non-ifunc-using executable does not have an OS/ABI field of System V"
271 set fails [expr $fails + 1]
272}
d8045f23 273
cbe950e9
L
274# The linked ifunc using executables and the shared library containing
275# ifunc should contain an IFUNC symbol. The non-ifunc using executable
276# should not.
d8045f23 277
cbe950e9
L
278if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
279 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
280 set fails [expr $fails + 1]
281}
e054468f
AM
282if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
283 fail "Local ifunc-using executable does not contain an IFUNC symbol"
284 set fails [expr $fails + 1]
285}
d8045f23
NC
286if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
287 fail "Static ifunc-using executable does not contain an IFUNC symbol"
288 set fails [expr $fails + 1]
289}
2955ec4c
L
290if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
291 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
d8045f23
NC
292 set fails [expr $fails + 1]
293}
294if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
295 fail "Static non-ifunc-using executable contains an IFUNC symbol"
296 set fails [expr $fails + 1]
297}
2955ec4c
L
298if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
299 fail "test-1 contains IFUNC symbols"
300 set fails [expr $fails + 1]
301}
302if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
303 fail "libtest-2.so contains IFUNC symbols"
304 set fails [expr $fails + 1]
305}
d8045f23 306
cbe950e9
L
307# The linked ifunc using executables and shared libraries should contain
308# a dynamic reloc referencing the IFUNC symbol. (Even the static
309# executable which should have a dynamic section created for it). The
310# non-ifunc using executable should not.
d8045f23 311
cbe950e9
L
312if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
313 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
314 set fails [expr $fails + 1]
315}
e054468f
AM
316if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
317 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
318 set fails [expr $fails + 1]
319}
cbe950e9
L
320if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
321 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
d8045f23
NC
322 set fails [expr $fails + 1]
323}
2955ec4c
L
324if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
325 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
d8045f23
NC
326 set fails [expr $fails + 1]
327}
328if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
329 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
330 set fails [expr $fails + 1]
331}
332
333if { $fails == 0 } {
334 pass "Checking ifunc binaries"
335}
336
337# Clean up, unless we are being verbose, in which case we leave the files available.
338if { $verbose < 1 } {
339 remote_file host delete "tmpdir/shared_prog.o"
340 remote_file host delete "tmpdir/static_prog.o"
341 remote_file host delete "tmpdir/shared_ifunc.o"
342 remote_file host delete "tmpdir/static_ifunc.o"
343 remote_file host delete "tmpdir/static_noifunc.o"
344 remote_file host delete "tmpdir/libshared_ifunc.so"
345 remote_file host delete "tmpdir/libifunc.a"
346 remote_file host delete "tmpdir/dynamic_prog"
e054468f 347 remote_file host delete "tmpdir/local_prog"
d8045f23
NC
348 remote_file host delete "tmpdir/static_prog"
349 remote_file host delete "tmpdir/static_nonifunc_prog"
350}
cbe950e9 351
4584ec12
L
352run_cc_link_tests [list \
353 [list \
354 "Build libpr16467a.so" \
355 "-shared -Wl,--version-script=pr16467a.map" \
356 "-fPIC" \
357 { pr16467a.c } \
358 {} \
359 "libpr16467a.so" \
360 ] \
361 [list \
362 "Build libpr16467b.a" \
363 "" \
364 "-fPIC" \
365 { pr16467b.c } \
366 {} \
367 "libpr16467b.a" \
368 ] \
369 [list \
370 "Build libpr16467b.so" \
371 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
372 -Wl,--version-script=pr16467b.map" \
373 "-fPIC" \
374 { dummy.c } \
375 {} \
376 "libpr16467b.so" \
377 ] \
378 [list \
379 "Build libpr16467c.a" \
380 "" \
381 "" \
382 { pr16467c.c } \
383 {} \
384 "libpr16467c.a" \
385 ] \
386]
387
37a9e49a
L
388run_ld_link_exec_tests [] [list \
389 [list \
390 "Common symbol override ifunc test 1a" \
391 "-static" \
392 "" \
393 { ifunc-common-1a.c ifunc-common-1b.c } \
394 "ifunc-common-1a" \
395 "ifunc-common-1.out" \
396 "-g" \
397 ] \
398 [list \
399 "Common symbol override ifunc test 1b" \
400 "-static" \
401 "" \
402 { ifunc-common-1b.c ifunc-common-1a.c } \
403 "ifunc-common-1b" \
404 "ifunc-common-1.out" \
405 "-g" \
406 ] \
c22ee0ad
L
407]
408
409set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
410foreach t $test_list {
411 # We need to strip the ".d", but can leave the dirname.
412 verbose [file rootname $t]
413 run_dump_test [file rootname $t]
414}
415
416# Run-time tests which require working IFUNC support.
417if { ![check_ifunc_available] } {
418 return
419}
420
5f7cbeec
L
421run_cc_link_tests [list \
422 [list \
423 "Build ifunc-lib.so" \
424 "-shared" \
425 "-fPIC" \
426 { ifunc-lib.c } \
427 {} \
428 "libifunc-lib.so" \
429 ] \
430]
431
c22ee0ad 432run_ld_link_exec_tests [] [list \
4584ec12
L
433 [list \
434 "Run pr16467" \
435 "tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
436 "" \
437 { dummy.c } \
438 "pr16467" \
439 "pr16467.out" \
440 "" \
441 ] \
5f7cbeec
L
442 [list \
443 "Run ifunc-main" \
444 "tmpdir/libifunc-lib.so" \
445 "" \
446 { ifunc-main.c } \
447 "ifunc-main" \
448 "ifunc-main.out" \
449 ] \
450 [list \
451 "Run ifunc-main with -fpic" \
452 "tmpdir/libifunc-lib.so" \
453 "" \
454 { ifunc-main.c } \
455 "ifunc-main" \
456 "ifunc-main.out" \
457 "-fpic" \
458 ] \
37a9e49a 459]
This page took 0.337483 seconds and 4 git commands to generate.