Run more ld tests when not native
[deliverable/binutils-gdb.git] / ld / testsuite / ld-ifunc / ifunc.exp
1 # Expect script for linker support of IFUNC symbols and relocations.
2 #
3 # Copyright (C) 2009-2017 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, 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"
41 return
42 }
43
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"
48 return
49 }
50
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 } {
53 global READELF
54 global READELFFLAGS
55
56 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
57
58 if ![string match "" $got] then {
59 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
60 return 0
61 }
62
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"
66 return 0
67 }
68
69 if { $osabi == $expected_osabi } {
70 return 1
71 }
72
73 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
74
75 return 0
76 }
77
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 } {
81 global READELF
82 global READELFFLAGS
83
84 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
85
86 if ![string match "" $got] then {
87 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
88 return -1
89 }
90
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
94
95 if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
96 return 0
97 }
98
99 return 1
100 }
101
102 # A procedure to confirm that a file contains the R_*_IRELATIVE
103 # relocation.
104 # Returns -1 upon error, 0 if the relocation was not found and 1 if
105 # it was found.
106 proc contains_irelative_reloc { binary_file } {
107 global READELF
108 global READELFFLAGS
109
110 catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
111
112 if ![string match "" $got] then {
113 verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
114 return -1
115 }
116
117 # Look for a line like this:
118 # 0000000000600ab0 0000000000000025 R_X86_64_IRELATIVE 000000000040061c
119 # 080496f4 0000002a R_386_IRELATIVE
120
121
122 if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
123 return 0
124 }
125
126 return 1
127 }
128
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 } {
132 global READELF
133 global READELFFLAGS
134
135 catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
136
137 if ![string match "" $got] then {
138 verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
139 return -1
140 }
141
142 if [string match "" [file_contents readelf.out]] then {
143 verbose "No relocs found in $binary_file"
144 return 0
145 }
146
147 if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
148 return 0
149 }
150
151 return 1
152 }
153
154 set fails 0
155
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]
160 }
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]
164 }
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]
168 }
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]
172 }
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]
176 }
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]
180 }
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]
184 }
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]
188 }
189
190 if { $fails != 0 } {
191 return
192 }
193
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]
197 }
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]
201 }
202
203 if { $fails != 0 } {
204 return
205 }
206
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]
210 }
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]
214 }
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]
218 }
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]
222 }
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]
226 }
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]
230 }
231
232 if { $fails == 0 } {
233 pass "Building ifunc binaries"
234 set fails 0
235 } else {
236 return
237 }
238
239 # Check the executables and shared libraries
240 #
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).
244
245 case $target_triplet in {
246 { hppa*-*-linux* } { set expected_none {UNIX - GNU} }
247 default { set expected_none {UNIX - System V} }
248 }
249
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]
253 }
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]
257 }
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]
261 }
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]
265 }
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]
269 }
270
271 # The linked ifunc using executables and the shared library containing
272 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
273 # should not.
274
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]
278 }
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]
282 }
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]
286 }
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]
290 }
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]
294 }
295 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
296 fail "test-1 contains IFUNC symbols"
297 set fails [expr $fails + 1]
298 }
299 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
300 fail "libtest-2.so contains IFUNC symbols"
301 set fails [expr $fails + 1]
302 }
303
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.
308
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]
312 }
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]
316 }
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]
320 }
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]
324 }
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]
328 }
329
330 if { $fails == 0 } {
331 pass "Checking ifunc binaries"
332 }
333
334 run_cc_link_tests [list \
335 [list \
336 "Build libpr16467a.so" \
337 "-shared -Wl,--version-script=pr16467a.map" \
338 "-fPIC" \
339 { pr16467a.c } \
340 {} \
341 "libpr16467a.so" \
342 ] \
343 [list \
344 "Build libpr16467b.a" \
345 "" \
346 "-fPIC" \
347 { pr16467b.c } \
348 {} \
349 "libpr16467b.a" \
350 ] \
351 [list \
352 "Build libpr16467b.so" \
353 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
354 -Wl,--version-script=pr16467b.map" \
355 "-fPIC" \
356 { dummy.c } \
357 {} \
358 "libpr16467b.so" \
359 ] \
360 [list \
361 "Build libpr16467c.a" \
362 "" \
363 "" \
364 { pr16467c.c } \
365 {} \
366 "libpr16467c.a" \
367 ] \
368 ]
369
370 run_ld_link_exec_tests [list \
371 [list \
372 "Common symbol override ifunc test 1a" \
373 "-static" \
374 "" \
375 { ifunc-common-1a.c ifunc-common-1b.c } \
376 "ifunc-common-1a" \
377 "ifunc-common-1.out" \
378 "-g" \
379 ] \
380 [list \
381 "Common symbol override ifunc test 1b" \
382 "-static" \
383 "" \
384 { ifunc-common-1b.c ifunc-common-1a.c } \
385 "ifunc-common-1b" \
386 "ifunc-common-1.out" \
387 "-g" \
388 ] \
389 ]
390
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]
396 }
397
398 # Run-time tests which require working IFUNC support.
399 if { ![check_ifunc_available] } {
400 return
401 }
402
403 run_cc_link_tests [list \
404 [list \
405 "Build ifunc-lib.so" \
406 "-shared" \
407 "-fPIC" \
408 { ifunc-lib.c } \
409 {} \
410 "libifunc-lib.so" \
411 ] \
412 ]
413
414 run_ld_link_exec_tests [list \
415 [list \
416 "Run pr16467" \
417 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
418 "" \
419 { dummy.c } \
420 "pr16467" \
421 "pr16467.out" \
422 "" \
423 ] \
424 [list \
425 "Run ifunc-main" \
426 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
427 "" \
428 { ifunc-main.c } \
429 "ifunc-main" \
430 "ifunc-main.out" \
431 ] \
432 [list \
433 "Run ifunc-main with -fpic" \
434 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
435 "" \
436 { ifunc-main.c } \
437 "ifunc-main" \
438 "ifunc-main.out" \
439 "-fpic" \
440 ] \
441 ]
442
443 # Run-time tests which require working ifunc attribute support.
444 if { ![check_ifunc_attribute_available] } {
445 return
446 }
447
448 run_cc_link_tests [list \
449 [list \
450 "Build pr18808a.o" \
451 "" \
452 "" \
453 { pr18808a.c } \
454 "" \
455 "" \
456 ] \
457 [list \
458 "Build libpr18808.so" \
459 "-shared" \
460 "-fPIC -O2 -g" \
461 { pr18808b.c } \
462 {} \
463 "libpr18808.so" \
464 ] \
465 [list \
466 "Build pr18841a.o" \
467 "" \
468 "" \
469 { pr18841a.c } \
470 "" \
471 "" \
472 ] \
473 [list \
474 "Build libpr18841b.so" \
475 "-shared" \
476 "-fPIC -O0 -g" \
477 { pr18841b.c } \
478 {} \
479 "libpr18841b.so" \
480 ] \
481 [list \
482 "Build libpr18841c.so" \
483 "-shared" \
484 "-fPIC -O0 -g" \
485 { pr18841c.c } \
486 {} \
487 "libpr18841c.so" \
488 ] \
489 ]
490
491 run_ld_link_exec_tests [list \
492 [list \
493 "Run pr18808" \
494 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
495 "" \
496 { dummy.c } \
497 "pr18808" \
498 "pr18808.out" \
499 ] \
500 [list \
501 "Run pr18841 with libpr18841b.so" \
502 "-Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
503 "" \
504 { dummy.c } \
505 "pr18841b" \
506 "pr18841.out" \
507 ] \
508 [list \
509 "Run pr18841 with libpr18841c.so" \
510 "-Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
511 "" \
512 { dummy.c } \
513 "pr18841c" \
514 "pr18841.out" \
515 ] \
516 ]
This page took 0.041326 seconds and 5 git commands to generate.