various i386-aout and i386-coff target removal
[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-2018 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 verbose "IFUNC tests not run - target does not support IFUNC"
39 return
40 }
41
42 # Skip targets where -shared is not supported
43
44 if ![check_shared_lib_support] {
45 return
46 }
47
48 # This test does not need a compiler...
49 run_dump_test "ifuncmod5"
50
51 # We need a working compiler. (Strictly speaking this is
52 # not true, we could use target specific assembler files).
53 if { [which $CC] == 0 } {
54 verbose "IFUNC tests not run - no compiler available"
55 return
56 }
57
58 # A procedure to check the OS/ABI field in the ELF header of a binary file.
59 proc check_osabi { binary_file expected_osabi } {
60 global READELF
61 global READELFFLAGS
62
63 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
64
65 if ![string match "" $got] then {
66 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
67 return 0
68 }
69
70 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
71 [file_contents readelf.out] nil osabi] } {
72 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
73 return 0
74 }
75
76 if { $osabi == $expected_osabi } {
77 return 1
78 }
79
80 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
81
82 return 0
83 }
84
85 # A procedure to confirm that a file contains the IFUNC symbol.
86 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
87 proc contains_ifunc_symbol { binary_file } {
88 global READELF
89 global READELFFLAGS
90
91 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
92
93 if ![string match "" $got] then {
94 verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
95 return -1
96 }
97
98 # Look for a line like this:
99 # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2
100 # with perhaps some other info between the visibility and section
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 $NOPIE_CFLAGS" "$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 $NOPIE_CFLAGS -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_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 ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
215 fail "Could not link a dynamic executable"
216 set fails [expr $fails + 1]
217 }
218 if ![ld_link $CC "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -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 ![string match "" $STATIC_LDFLAGS] {
223 if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
224 fail "Could not link a static executable"
225 set fails [expr $fails + 1]
226 }
227 if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
228 fail "Could not link a non-ifunc using static executable"
229 set fails [expr $fails + 1]
230 }
231 }
232 if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
233 fail "Could not link test-1"
234 set fails [expr $fails + 1]
235 }
236 if ![ld_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 }
240 if ![ld_link $ld "tmpdir/libtest-2-now.so" "-shared -z now tmpdir/test-2.o"] {
241 fail "Could not link libtest-2-now.so"
242 set fails [expr $fails + 1]
243 }
244
245 if { $fails == 0 } {
246 pass "Building ifunc binaries"
247 set fails 0
248 } else {
249 return
250 }
251
252 # Check the executables and shared libraries
253 #
254 # The linked ifunc using executables and the shared library containing
255 # ifunc should have an OSABI field of GNU. The linked non-ifunc using
256 # executable should have an OSABI field of NONE (aka System V).
257
258 case $target_triplet in {
259 { hppa*-*-linux* } { set expected_none {UNIX - GNU} }
260 default { set expected_none {UNIX - System V} }
261 }
262
263 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
264 fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
265 set fails [expr $fails + 1]
266 }
267 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
268 fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
269 set fails [expr $fails + 1]
270 }
271 if { ![string match "" $STATIC_LDFLAGS] \
272 && ![check_osabi tmpdir/static_prog {UNIX - GNU}]} {
273 fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
274 set fails [expr $fails + 1]
275 }
276 if {! [check_osabi tmpdir/dynamic_prog $expected_none]} {
277 fail "Dynamic ifunc-using executable does not have an OS/ABI field of $expected_none"
278 set fails [expr $fails + 1]
279 }
280 if {! [check_osabi tmpdir/static_nonifunc_prog $expected_none]} {
281 fail "Static non-ifunc-using executable does not have an OS/ABI field of $expected_none"
282 set fails [expr $fails + 1]
283 }
284
285 # The linked ifunc using executables and the shared library containing
286 # ifunc should contain an IFUNC symbol. The non-ifunc using executable
287 # should not.
288
289 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
290 fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
291 set fails [expr $fails + 1]
292 }
293 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
294 fail "Local ifunc-using executable does not contain an IFUNC symbol"
295 set fails [expr $fails + 1]
296 }
297 if { ![string match "" $STATIC_LDFLAGS] \
298 && [contains_ifunc_symbol tmpdir/static_prog] != 1} {
299 fail "Static ifunc-using executable does not contain an IFUNC symbol"
300 set fails [expr $fails + 1]
301 }
302 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
303 fail "Dynamic ifunc-using executable contains an IFUNC symbol"
304 set fails [expr $fails + 1]
305 }
306 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
307 fail "Static non-ifunc-using executable contains an IFUNC symbol"
308 set fails [expr $fails + 1]
309 }
310 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
311 fail "test-1 contains IFUNC symbols"
312 set fails [expr $fails + 1]
313 }
314 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
315 fail "libtest-2.so contains IFUNC symbols"
316 set fails [expr $fails + 1]
317 }
318 if {[contains_ifunc_symbol tmpdir/libtest-2-now.so] != 0} {
319 fail "libtest-2-now.so contains IFUNC symbols"
320 set fails [expr $fails + 1]
321 }
322
323 # The linked ifunc using executables and shared libraries should contain
324 # a dynamic reloc referencing the IFUNC symbol. (Even the static
325 # executable which should have a dynamic section created for it). The
326 # non-ifunc using executable should not.
327
328 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
329 fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
330 set fails [expr $fails + 1]
331 }
332 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
333 fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
334 set fails [expr $fails + 1]
335 }
336 if { ![string match "" $STATIC_LDFLAGS] \
337 && [contains_irelative_reloc tmpdir/static_prog] != 1} {
338 fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
339 set fails [expr $fails + 1]
340 }
341 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
342 fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
343 set fails [expr $fails + 1]
344 }
345 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
346 fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
347 set fails [expr $fails + 1]
348 }
349
350 if { $fails == 0 } {
351 pass "Checking ifunc binaries"
352 }
353
354 run_cc_link_tests [list \
355 [list \
356 "Build libpr16467a.so" \
357 "-shared -Wl,--version-script=pr16467a.map" \
358 "-fPIC" \
359 { pr16467a.c } \
360 {} \
361 "libpr16467a.so" \
362 ] \
363 [list \
364 "Build libpr16467b.a" \
365 "" \
366 "-fPIC" \
367 { pr16467b.c } \
368 {} \
369 "libpr16467b.a" \
370 ] \
371 [list \
372 "Build libpr16467b.so" \
373 "-shared tmpdir/pr16467b.o tmpdir/libpr16467a.so \
374 -Wl,--version-script=pr16467b.map" \
375 "-fPIC" \
376 { dummy.c } \
377 {} \
378 "libpr16467b.so" \
379 ] \
380 [list \
381 "Build libpr16467c.a" \
382 "" \
383 "" \
384 { pr16467c.c } \
385 {} \
386 "libpr16467c.a" \
387 ] \
388 [list \
389 "Build libpr16467an.so" \
390 "-shared -Wl,-z,now -Wl,--version-script=pr16467a.map" \
391 "-fPIC" \
392 { pr16467a.c } \
393 {} \
394 "libpr16467an.so" \
395 ] \
396 [list \
397 "Build libpr16467bn.so" \
398 "-shared tmpdir/pr16467b.o tmpdir/libpr16467an.so \
399 -Wl,--version-script=pr16467b.map" \
400 "-fPIC" \
401 { dummy.c } \
402 {} \
403 "libpr16467bn.so" \
404 ] \
405 ]
406
407 run_ld_link_exec_tests [list \
408 [list \
409 "Common symbol override ifunc test 1a" \
410 "-static" \
411 "" \
412 { ifunc-common-1a.c ifunc-common-1b.c } \
413 "ifunc-common-1a" \
414 "ifunc-common-1.out" \
415 "-g" \
416 ] \
417 [list \
418 "Common symbol override ifunc test 1b" \
419 "-static" \
420 "" \
421 { ifunc-common-1b.c ifunc-common-1a.c } \
422 "ifunc-common-1b" \
423 "ifunc-common-1.out" \
424 "-g" \
425 ] \
426 ]
427
428 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
429 foreach t $test_list {
430 # We need to strip the ".d", but can leave the dirname.
431 verbose [file rootname $t]
432 run_dump_test [file rootname $t]
433 }
434
435 # Run-time tests which require working IFUNC support.
436 if { ![check_ifunc_available] } {
437 return
438 }
439
440 run_cc_link_tests [list \
441 [list \
442 "Build ifunc-lib.so" \
443 "-shared" \
444 "-fPIC" \
445 { ifunc-lib.c } \
446 {} \
447 "libifunc-lib.so" \
448 ] \
449 [list \
450 "Build ifunc-libn.so" \
451 "-shared -Wl,-z,now" \
452 "-fPIC" \
453 { ifunc-lib.c } \
454 {} \
455 "libifunc-libn.so" \
456 ] \
457 ]
458
459 run_ld_link_exec_tests [list \
460 [list \
461 "Run pr16467" \
462 "-Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467b.so tmpdir/libpr16467a.so" \
463 "" \
464 { dummy.c } \
465 "pr16467" \
466 "pr16467.out" \
467 "" \
468 ] \
469 [list \
470 "Run pr16467 (-z now)" \
471 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr16467c.o tmpdir/libpr16467bn.so tmpdir/libpr16467an.so" \
472 "" \
473 { dummy.c } \
474 "pr16467n" \
475 "pr16467.out" \
476 "" \
477 ] \
478 [list \
479 "Run ifunc-main" \
480 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
481 "" \
482 { ifunc-main.c } \
483 "ifunc-main" \
484 "ifunc-main.out" \
485 ] \
486 [list \
487 "Run ifunc-main with -fpic" \
488 "-Wl,--no-as-needed tmpdir/libifunc-lib.so" \
489 "" \
490 { ifunc-main.c } \
491 "ifunc-main" \
492 "ifunc-main.out" \
493 "-fpic" \
494 ] \
495 [list \
496 "Run ifunc-main (-z now)" \
497 "-Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
498 "" \
499 { ifunc-main.c } \
500 "ifunc-mainn" \
501 "ifunc-main.out" \
502 ] \
503 [list \
504 "Run ifunc-main with PIE (-z now)" \
505 "-pie -Wl,-z,now -Wl,--no-as-needed tmpdir/libifunc-libn.so" \
506 "" \
507 { ifunc-main.c } \
508 "ifunc-mainpn" \
509 "ifunc-main.out" \
510 "-fpie" \
511 ] \
512 ]
513
514 # Run-time tests which require working ifunc attribute support.
515 if { ![check_ifunc_attribute_available] } {
516 return
517 }
518
519 run_cc_link_tests [list \
520 [list \
521 "Build pr18808a.o" \
522 "" \
523 "" \
524 { pr18808a.c } \
525 "" \
526 "" \
527 ] \
528 [list \
529 "Build libpr18808.so" \
530 "-shared" \
531 "-fPIC -O2 -g" \
532 { pr18808b.c } \
533 {} \
534 "libpr18808.so" \
535 ] \
536 [list \
537 "Build libpr18808n.so" \
538 "-shared -Wl,-z,now" \
539 "-fPIC -O2 -g" \
540 { pr18808b.c } \
541 {} \
542 "libpr18808n.so" \
543 ] \
544 [list \
545 "Build pr18841a.o" \
546 "" \
547 "" \
548 { pr18841a.c } \
549 "" \
550 "" \
551 ] \
552 [list \
553 "Build libpr18841b.so" \
554 "-shared" \
555 "-fPIC -O0 -g" \
556 { pr18841b.c } \
557 {} \
558 "libpr18841b.so" \
559 ] \
560 [list \
561 "Build libpr18841c.so" \
562 "-shared" \
563 "-fPIC -O0 -g" \
564 { pr18841c.c } \
565 {} \
566 "libpr18841c.so" \
567 ] \
568 [list \
569 "Build libpr18841bn.so" \
570 "-Wl,-z,now -shared" \
571 "-fPIC -O0 -g" \
572 { pr18841b.c } \
573 {} \
574 "libpr18841bn.so" \
575 ] \
576 [list \
577 "Build libpr18841cn.so" \
578 "-shared" \
579 "-Wl,-z,now -fPIC -O0 -g" \
580 { pr18841c.c } \
581 {} \
582 "libpr18841cn.so" \
583 ] \
584 ]
585
586 run_ld_link_exec_tests [list \
587 [list \
588 "Run pr18808" \
589 "-Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808.so" \
590 "" \
591 { dummy.c } \
592 "pr18808" \
593 "pr18808.out" \
594 ] \
595 [list \
596 "Run pr18808 (-z now)" \
597 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18808a.o tmpdir/libpr18808n.so" \
598 "" \
599 { dummy.c } \
600 "pr18808n" \
601 "pr18808.out" \
602 ] \
603 [list \
604 "Run pr18841 with libpr18841b.so" \
605 "-Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
606 "" \
607 { dummy.c } \
608 "pr18841b" \
609 "pr18841.out" \
610 ] \
611 [list \
612 "Run pr18841 with libpr18841c.so" \
613 "-Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
614 "" \
615 { dummy.c } \
616 "pr18841c" \
617 "pr18841.out" \
618 ] \
619 [list \
620 "Run pr18841 with libpr18841bn.so (-z now)" \
621 "-Wl,-z,now -Wl,--no-as-needed tmpdir/pr18841a.o tmpdir/libpr18841bn.so" \
622 "" \
623 { dummy.c } \
624 "pr18841bn" \
625 "pr18841.out" \
626 ] \
627 [list \
628 "Run pr18841 with libpr18841cn.so (-z now)" \
629 "-Wl,-z,now -Wl,--as-needed tmpdir/pr18841a.o tmpdir/libpr18841cn.so" \
630 "" \
631 { dummy.c } \
632 "pr18841cn" \
633 "pr18841.out" \
634 ] \
635 ]
This page took 0.051786 seconds and 4 git commands to generate.