aarch64: Add support for GNU indirect functions.
[deliverable/binutils-gdb.git] / ld / testsuite / ld-ifunc / ifunc.exp
1 # Expect script for linker support of IFUNC symbols and relocations.
2 #
3 # Copyright 2009, 2010, 2012 Free Software Foundation, Inc.
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
26 # IFUNC support has only been implemented for the ix86, x86_64, powerpc,
27 # aarch64 and sparc so far.
28 if {!(([istarget "i?86-*-*"]
29 || [istarget "x86_64-*-*"]
30 || [istarget "powerpc*-*-*"]
31 || [istarget "aarch64*-*-*"]
32 || [istarget "sparc*-*-*"])
33 && ([istarget "*-*-elf*"]
34 || [istarget "*-*-nacl*"]
35 || (([istarget "*-*-linux*"]
36 || [istarget "*-*-gnu*"])
37 && ![istarget "*-*-*aout*"]
38 && ![istarget "*-*-*oldld*"]))) } {
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.
47 if ![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).
54 if { [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.
60 proc 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"
82
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.
88 proc 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
101
102 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
103 return 0
104 }
105
106 return 1
107 }
108
109 # A procedure to confirm that a file contains the R_*_IRELATIVE
110 # relocation.
111 # Returns -1 upon error, 0 if the relocation was not found and 1 if
112 # it was found.
113 proc contains_irelative_reloc { binary_file } {
114 global READELF
115 global READELFFLAGS
116
117 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
118
119 if ![string match "" $got] then {
120 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
121 return -1
122 }
123
124 # Look for a line like this:
125 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
126 # 080496f4 0000002a R_386_IRELATIVE
127
128
129 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
130 return 0
131 }
132
133 return 1
134 }
135
136 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
137 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
138 proc contains_ifunc_reloc { binary_file } {
139 global READELF
140 global READELFFLAGS
141
142 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
143
144 if ![string match "" $got] then {
145 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
146 return -1
147 }
148
149 if [string match "" [file_contents readelf.out]] then {
150 verbose "No relocs found in $binary_file"
151 return 0
152 }
153
154 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
155 return 0
156 }
157
158 return 1
159 }
160
161 set fails 0
162
163 # Create the object files, libraries and executables.
164 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
165 fail "Could not create a PIC object file"
166 set fails [expr $fails + 1]
167 }
168 if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
169 fail "Could not create a non-PIC object file"
170 set fails [expr $fails + 1]
171 }
172 if ![ld_compile "$CC -c -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
173 fail "Could not create a PIC object file containing an IFUNC symbol"
174 set fails [expr $fails + 1]
175 }
176 if ![ld_compile "$CC -c -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
177 fail "Could not create a non-PIC object file containing an IFUNC symbol"
178 set fails [expr $fails + 1]
179 }
180 if ![ld_compile "$CC -c -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
181 fail "Could not create an ordinary non-PIC object file"
182 set fails [expr $fails + 1]
183 }
184 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
185 fail "Could not create an empty object file"
186 set fails [expr $fails + 1]
187 }
188 if ![ld_compile "$CC -c" "$srcdir/$subdir/test-1.c" "tmpdir/test-1.o"] {
189 fail "Could not create test-1.o"
190 set fails [expr $fails + 1]
191 }
192 if ![ld_compile "$CC -fPIC -c" "$srcdir/$subdir/test-2.c" "tmpdir/test-2.o"] {
193 fail "Could not create test-2.o"
194 set fails [expr $fails + 1]
195 }
196
197 if { $fails != 0 } {
198 return
199 }
200
201 if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
202 fail "Could not create a shared library containing an IFUNC symbol"
203 set fails [expr $fails + 1]
204 }
205 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
206 fail "Could not create a static library containing an IFUNC symbol"
207 set fails [expr $fails + 1]
208 }
209
210 if { $fails != 0 } {
211 return
212 }
213
214 if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
215 fail "Could not link a dynamic executable"
216 set fails [expr $fails + 1]
217 }
218 if ![default_ld_link $ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
219 fail "Could not link a dynamic executable using local ifunc"
220 set fails [expr $fails + 1]
221 }
222 if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
223 fail "Could not link a static executable"
224 set fails [expr $fails + 1]
225 }
226 if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
227 fail "Could not link a non-ifunc using static executable"
228 set fails [expr $fails + 1]
229 }
230 if ![default_ld_link $ld "tmpdir/test-1" "tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
231 fail "Could not link test-1"
232 set fails [expr $fails + 1]
233 }
234 if ![ld_simple_link $ld "tmpdir/libtest-2.so" "-shared tmpdir/test-2.o"] {
235 fail "Could not link libtest-2.so"
236 set fails [expr $fails + 1]
237 }
238
239 if { $fails == 0 } {
240 pass "Building ifunc binaries"
241 set fails 0
242 } else {
243 return
244 }
245
246 # Check the executables and shared libraries
247 #
248 # The linked ifunc using executables and the shared library containing
249 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
250 # executable should have an OSABI field of NONE (aka System V).
251
252 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
253 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
254 set fails [expr $fails + 1]
255 }
256 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
257 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
258 set fails [expr $fails + 1]
259 }
260 if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
261 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
262 set fails [expr $fails + 1]
263 }
264 if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
265 fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
266 set fails [expr $fails + 1]
267 }
268 if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} {
269 fail "Static non-ifunc-using executable does not have an OS/ABI field of System V"
270 set fails [expr $fails + 1]
271 }
272
273 # The linked ifunc using executables and the shared library containing
274 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
275 # should not.
276
277 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
278 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
279 set fails [expr $fails + 1]
280 }
281 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
282 fail "Local ifunc-using executable does not contain an IFUNC symbol"
283 set fails [expr $fails + 1]
284 }
285 if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
286 fail "Static ifunc-using executable does not contain an IFUNC symbol"
287 set fails [expr $fails + 1]
288 }
289 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
290 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
291 set fails [expr $fails + 1]
292 }
293 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
294 fail "Static non-ifunc-using executable contains an IFUNC symbol"
295 set fails [expr $fails + 1]
296 }
297 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
298 fail "test-1 contains IFUNC symbols"
299 set fails [expr $fails + 1]
300 }
301 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
302 fail "libtest-2.so contains IFUNC symbols"
303 set fails [expr $fails + 1]
304 }
305
306 # The linked ifunc using executables and shared libraries should contain
307 # a dynamic reloc referencing the IFUNC symbol. (Even the static
308 # executable which should have a dynamic section created for it). The
309 # non-ifunc using executable should not.
310
311 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
312 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
313 set fails [expr $fails + 1]
314 }
315 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
316 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
317 set fails [expr $fails + 1]
318 }
319 if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
320 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
321 set fails [expr $fails + 1]
322 }
323 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
324 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
325 set fails [expr $fails + 1]
326 }
327 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
328 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
329 set fails [expr $fails + 1]
330 }
331
332 if { $fails == 0 } {
333 pass "Checking ifunc binaries"
334 }
335
336 # Clean up, unless we are being verbose, in which case we leave the files available.
337 if { $verbose < 1 } {
338 remote_file host delete "tmpdir/shared_prog.o"
339 remote_file host delete "tmpdir/static_prog.o"
340 remote_file host delete "tmpdir/shared_ifunc.o"
341 remote_file host delete "tmpdir/static_ifunc.o"
342 remote_file host delete "tmpdir/static_noifunc.o"
343 remote_file host delete "tmpdir/libshared_ifunc.so"
344 remote_file host delete "tmpdir/libifunc.a"
345 remote_file host delete "tmpdir/dynamic_prog"
346 remote_file host delete "tmpdir/local_prog"
347 remote_file host delete "tmpdir/static_prog"
348 remote_file host delete "tmpdir/static_nonifunc_prog"
349 }
350
351 run_ld_link_exec_tests [] [list \
352 [list \
353 "Common symbol override ifunc test 1a" \
354 "-static" \
355 "" \
356 { ifunc-common-1a.c ifunc-common-1b.c } \
357 "ifunc-common-1a" \
358 "ifunc-common-1.out" \
359 "-g" \
360 ] \
361 [list \
362 "Common symbol override ifunc test 1b" \
363 "-static" \
364 "" \
365 { ifunc-common-1b.c ifunc-common-1a.c } \
366 "ifunc-common-1b" \
367 "ifunc-common-1.out" \
368 "-g" \
369 ] \
370 ]
371
372 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
373 foreach t $test_list {
374 # We need to strip the ".d", but can leave the dirname.
375 verbose [file rootname $t]
376 run_dump_test [file rootname $t]
377 }
This page took 0.038267 seconds and 5 git commands to generate.