Support -z relro on metag
[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#
2571583a 3# Copyright (C) 2009-2017 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,
e44c481a 27# aarch64, sparc, and S/390 so far.
e054468f
AM
28if {!(([istarget "i?86-*-*"]
29 || [istarget "x86_64-*-*"]
d0c9aeb3 30 || [istarget "powerpc*-*-*"]
1419bbe5 31 || [istarget "aarch64*-*-*"]
e44c481a
AK
32 || [istarget "sparc*-*-*"]
33 || [istarget "s390*-*-*"])
e054468f 34 && ([istarget "*-*-elf*"]
5a68afcf 35 || [istarget "*-*-nacl*"]
99753d9d
AM
36 || (([istarget "*-*-linux*"]
37 || [istarget "*-*-gnu*"])
e054468f 38 && ![istarget "*-*-*aout*"]
99753d9d 39 && ![istarget "*-*-*oldld*"]))) } {
d8045f23
NC
40 verbose "IFUNC tests not run - target does not support IFUNC"
41 return
42}
43
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.
48if ![isnative] {
49 verbose "IFUNC tests not run - not a native toolchain"
50 return
51}
52
53# We need a working compiler. (Strictly speaking this is
54# not true, we could use target specific assembler files).
55if { [which $CC] == 0 } {
56 verbose "IFUNC tests not run - no compiler available"
57 return
58}
59
60# A procedure to check the OS/ABI field in the ELF header of a binary file.
61proc check_osabi { binary_file expected_osabi } {
62 global READELF
63 global READELFFLAGS
64
65 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
66
67 if ![string match "" $got] then {
68 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
69 return 0
70 }
71
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"
75 return 0
76 }
77
78 if { $osabi == $expected_osabi } {
79 return 1
80 }
81
82 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
5a68afcf 83
d8045f23
NC
84 return 0
85}
86
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.
89proc contains_ifunc_symbol { binary_file } {
90 global READELF
91 global READELFFLAGS
92
93 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
94
95 if ![string match "" $got] then {
96 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
97 return -1
98 }
99
100 # Look for a line like this:
101 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
4115bfc6 102 # with perhaps some other info between the visibility and section
d8045f23 103
4115bfc6 104 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
d8045f23
NC
105 return 0
106 }
107
108 return 1
109}
110
cbe950e9
L
111# A procedure to confirm that a file contains the R_*_IRELATIVE
112# relocation.
113# Returns -1 upon error, 0 if the relocation was not found and 1 if
114# it was found.
115proc contains_irelative_reloc { binary_file } {
116 global READELF
117 global READELFFLAGS
118
119 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
120
121 if ![string match "" $got] then {
122 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
123 return -1
124 }
125
126 # Look for a line like this:
127 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
128 # 080496f4 0000002a R_386_IRELATIVE
129
130
425621e7 131 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
cbe950e9
L
132 return 0
133 }
134
135 return 1
136}
137
d8045f23
NC
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.
140proc contains_ifunc_reloc { binary_file } {
141 global READELF
142 global READELFFLAGS
143
144 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
145
146 if ![string match "" $got] then {
147 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
148 return -1
149 }
150
151 if [string match "" [file_contents readelf.out]] then {
152 verbose "No relocs found in $binary_file"
153 return 0
154 }
155
156 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
157 return 0
158 }
159
160 return 1
161}
162
163set fails 0
164
165# Create the object files, libraries and executables.
be19bd51
L
166if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
167 fail "Could not create a PIC object file"
d8045f23
NC
168 set fails [expr $fails + 1]
169}
be19bd51
L
170if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
171 fail "Could not create a non-PIC object file"
d8045f23
NC
172 set fails [expr $fails + 1]
173}
be19bd51
L
174if ![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"
d8045f23
NC
176 set fails [expr $fails + 1]
177}
be19bd51
L
178if ![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"
d8045f23
NC
180 set fails [expr $fails + 1]
181}
be19bd51
L
182if ![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]
185}
186if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
187 fail "Could not create an empty object file"
d8045f23
NC
188 set fails [expr $fails + 1]
189}
2955ec4c
L
190if ![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]
193}
194if ![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]
197}
d8045f23
NC
198
199if { $fails != 0 } {
200 return
201}
202
203if ![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]
206}
207if ![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]
210}
211
212if { $fails != 0 } {
213 return
214}
215
216if ![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]
219}
e054468f
AM
220if ![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]
223}
224if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
d8045f23
NC
225 fail "Could not link a static executable"
226 set fails [expr $fails + 1]
227}
be19bd51 228if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
d8045f23
NC
229 fail "Could not link a non-ifunc using static executable"
230 set fails [expr $fails + 1]
231}
2955ec4c
L
232if ![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]
235}
236if ![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]
239}
d8045f23
NC
240
241if { $fails == 0 } {
242 pass "Building ifunc binaries"
243 set fails 0
244} else {
245 return
246}
247
cbe950e9 248# Check the executables and shared libraries
d8045f23 249#
cbe950e9 250# The linked ifunc using executables and the shared library containing
9c55345c 251# ifunc should have an OSABI field of GNU. The linked non-ifunc using
cbe950e9 252# executable should have an OSABI field of NONE (aka System V).
d8045f23 253
9c55345c
TS
254if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
255 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
cbe950e9
L
256 set fails [expr $fails + 1]
257}
9c55345c
TS
258if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
259 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
e054468f
AM
260 set fails [expr $fails + 1]
261}
9c55345c
TS
262if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
263 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
d8045f23
NC
264 set fails [expr $fails + 1]
265}
c16153ae
L
266if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
267 fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
d8045f23
NC
268 set fails [expr $fails + 1]
269}
be19bd51
L
270if {! [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]
273}
d8045f23 274
cbe950e9
L
275# The linked ifunc using executables and the shared library containing
276# ifunc should contain an IFUNC symbol. The non-ifunc using executable
277# should not.
d8045f23 278
cbe950e9
L
279if {[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]
282}
e054468f
AM
283if {[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]
286}
d8045f23
NC
287if {[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]
290}
2955ec4c
L
291if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
292 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
d8045f23
NC
293 set fails [expr $fails + 1]
294}
295if {[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]
298}
2955ec4c
L
299if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
300 fail "test-1 contains IFUNC symbols"
301 set fails [expr $fails + 1]
302}
303if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
304 fail "libtest-2.so contains IFUNC symbols"
305 set fails [expr $fails + 1]
306}
d8045f23 307
cbe950e9
L
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.
d8045f23 312
cbe950e9
L
313if {[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]
316}
e054468f
AM
317if {[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]
320}
cbe950e9
L
321if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
322 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
d8045f23
NC
323 set fails [expr $fails + 1]
324}
2955ec4c
L
325if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
326 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
d8045f23
NC
327 set fails [expr $fails + 1]
328}
329if {[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]
332}
333
334if { $fails == 0 } {
335 pass "Checking ifunc binaries"
336}
337
338# Clean up, unless we are being verbose, in which case we leave the files available.
339if { $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"
e054468f 348 remote_file host delete "tmpdir/local_prog"
d8045f23
NC
349 remote_file host delete "tmpdir/static_prog"
350 remote_file host delete "tmpdir/static_nonifunc_prog"
351}
cbe950e9 352
4584ec12
L
353run_cc_link_tests [list \
354 [list \
355 "Build libpr16467a.so" \
356 "-shared -Wl,--version-script=pr16467a.map" \
357 "-fPIC" \
358 { pr16467a.c } \
359 {} \
360 "libpr16467a.so" \
361 ] \
362 [list \
363 "Build libpr16467b.a" \
364 "" \
365 "-fPIC" \
366 { pr16467b.c } \
367 {} \
368 "libpr16467b.a" \
369 ] \
370 [list \
371 "Build libpr16467b.so" \
372 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
373 -Wl,--version-script=pr16467b.map" \
374 "-fPIC" \
375 { dummy.c } \
376 {} \
377 "libpr16467b.so" \
378 ] \
379 [list \
380 "Build libpr16467c.a" \
381 "" \
382 "" \
383 { pr16467c.c } \
384 {} \
385 "libpr16467c.a" \
386 ] \
387]
388
982c6f26 389run_ld_link_exec_tests [list \
37a9e49a
L
390 [list \
391 "Common symbol override ifunc test 1a" \
392 "-static" \
393 "" \
394 { ifunc-common-1a.c ifunc-common-1b.c } \
395 "ifunc-common-1a" \
396 "ifunc-common-1.out" \
397 "-g" \
398 ] \
399 [list \
400 "Common symbol override ifunc test 1b" \
401 "-static" \
402 "" \
403 { ifunc-common-1b.c ifunc-common-1a.c } \
404 "ifunc-common-1b" \
405 "ifunc-common-1.out" \
406 "-g" \
407 ] \
c22ee0ad
L
408]
409
410set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
411foreach 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]
415}
416
417# Run-time tests which require working IFUNC support.
418if { ![check_ifunc_available] } {
419 return
420}
421
5f7cbeec
L
422run_cc_link_tests [list \
423 [list \
424 "Build ifunc-lib.so" \
425 "-shared" \
426 "-fPIC" \
427 { ifunc-lib.c } \
428 {} \
429 "libifunc-lib.so" \
430 ] \
431]
432
982c6f26 433run_ld_link_exec_tests [list \
4584ec12
L
434 [list \
435 "Run pr16467" \
436 "tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
437 "" \
438 { dummy.c } \
439 "pr16467" \
440 "pr16467.out" \
441 "" \
442 ] \
5f7cbeec
L
443 [list \
444 "Run ifunc-main" \
445 "tmpdir/libifunc-lib.so" \
446 "" \
447 { ifunc-main.c } \
448 "ifunc-main" \
449 "ifunc-main.out" \
450 ] \
451 [list \
452 "Run ifunc-main with -fpic" \
453 "tmpdir/libifunc-lib.so" \
454 "" \
455 { ifunc-main.c } \
456 "ifunc-main" \
457 "ifunc-main.out" \
458 "-fpic" \
459 ] \
37a9e49a 460]
97dc35c8
L
461
462# Run-time tests which require working ifunc attribute support.
463if { ![check_ifunc_attribute_available] } {
464 return
465}
466
467run_cc_link_tests [list \
205ac185
L
468 [list \
469 "Build pr18808a.o" \
470 "" \
471 "" \
472 { pr18808a.c } \
473 "" \
474 "" \
475 ] \
97dc35c8
L
476 [list \
477 "Build libpr18808.so" \
478 "-shared" \
479 "-fPIC -O2 -g" \
480 { pr18808b.c } \
481 {} \
482 "libpr18808.so" \
483 ] \
205ac185
L
484 [list \
485 "Build pr18841a.o" \
486 "" \
487 "" \
488 { pr18841a.c } \
489 "" \
490 "" \
491 ] \
cae1fbbb 492 [list \
4e1626f5 493 "Build libpr18841b.so" \
cae1fbbb
L
494 "-shared" \
495 "-fPIC -O0 -g" \
496 { pr18841b.c } \
497 {} \
4e1626f5
L
498 "libpr18841b.so" \
499 ] \
500 [list \
501 "Build libpr18841c.so" \
502 "-shared" \
503 "-fPIC -O0 -g" \
504 { pr18841c.c } \
505 {} \
506 "libpr18841c.so" \
cae1fbbb 507 ] \
97dc35c8
L
508]
509
982c6f26 510run_ld_link_exec_tests [list \
97dc35c8
L
511 [list \
512 "Run pr18808" \
205ac185 513 "tmpdir/pr18808a.o tmpdir/libpr18808.so" \
97dc35c8 514 "" \
205ac185 515 { dummy.c } \
97dc35c8
L
516 "pr18808" \
517 "pr18808.out" \
518 ] \
cae1fbbb 519 [list \
4e1626f5 520 "Run pr18841 with libpr18841b.so" \
205ac185 521 "tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
4e1626f5 522 "" \
205ac185 523 { dummy.c } \
4e1626f5
L
524 "pr18841b" \
525 "pr18841.out" \
526 ] \
527 [list \
528 "Run pr18841 with libpr18841c.so" \
205ac185 529 "--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
cae1fbbb 530 "" \
205ac185 531 { dummy.c } \
4e1626f5 532 "pr18841c" \
cae1fbbb
L
533 "pr18841.out" \
534 ] \
97dc35c8 535]
This page took 0.377272 seconds and 4 git commands to generate.