504007ae700485f7420a5eb50e50a38064399014
[external/binutils.git] / ld / testsuite / ld-ifunc / ifunc.exp
1 # Expect script for linker support of IFUNC symbols and relocations.
2 #
3 #   Copyright (C) 2009-2016 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 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.
48 if ![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).
55 if { [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.
61 proc 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"
83
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.
89 proc 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
102     # with perhaps some other info between the visibility and section
103
104     if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT .* \[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
105         return 0
106     }
107
108     return 1
109 }
110
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.
115 proc 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
131     if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IREL(|ATIVE)\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
132         return 0
133     }
134
135     return 1
136 }
137
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.
140 proc 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
163 set fails 0
164
165 # Create the object files, libraries and executables.
166 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
167     fail "Could not create a PIC object file"
168     set fails [expr $fails + 1]
169 }
170 if ![ld_compile "$CC -c" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
171     fail "Could not create a non-PIC object file"
172     set fails [expr $fails + 1]
173 }
174 if ![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"
176     set fails [expr $fails + 1]
177 }
178 if ![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"
180     set fails [expr $fails + 1]
181 }
182 if ![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 }
186 if ![ld_assemble $as "$srcdir/ld-elf/empty.s" "tmpdir/empty.o"] {
187     fail "Could not create an empty object file"
188     set fails [expr $fails + 1]
189 }
190 if ![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 }
194 if ![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 }
198
199 if { $fails != 0 } {
200     return
201 }
202
203 if ![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 }
207 if ![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
212 if { $fails != 0 } {
213     return
214 }
215
216 if ![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 }
220 if ![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 }
224 if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
225     fail "Could not link a static executable"
226     set fails [expr $fails + 1]
227 }
228 if ![ld_simple_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
229     fail "Could not link a non-ifunc using static executable"
230     set fails [expr $fails + 1]
231 }
232 if ![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 }
236 if ![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 }
240
241 if { $fails == 0 } {
242   pass "Building ifunc binaries"
243   set fails 0
244 } else {
245     return
246 }
247
248 # Check the executables and shared libraries
249 #
250 # The linked ifunc using executables and the shared library containing
251 # ifunc should have an OSABI field of GNU.  The linked non-ifunc using
252 # executable should have an OSABI field of NONE (aka System V).
253
254 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - GNU}]} {
255     fail "Shared libraries containing ifunc does not have an OS/ABI field of GNU"
256     set fails [expr $fails + 1]
257 }
258 if {! [check_osabi tmpdir/local_prog {UNIX - GNU}]} {
259     fail "Local ifunc-using executable does not have an OS/ABI field of GNU"
260     set fails [expr $fails + 1]
261 }
262 if {! [check_osabi tmpdir/static_prog {UNIX - GNU}]} {
263     fail "Static ifunc-using executable does not have an OS/ABI field of GNU"
264     set fails [expr $fails + 1]
265 }
266 if {! [check_osabi tmpdir/dynamic_prog {UNIX - System V}]} {
267     fail "Dynamic ifunc-using executable does not have an OS/ABI field of System V"
268     set fails [expr $fails + 1]
269 }
270 if {! [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 }
274
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.
278
279 if {[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 }
283 if {[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 }
287 if {[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 }
291 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 0} {
292     fail "Dynamic ifunc-using executable contains an IFUNC symbol"
293     set fails [expr $fails + 1]
294 }
295 if {[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 }
299 if {[contains_ifunc_symbol tmpdir/test-1] != 0} {
300     fail "test-1 contains IFUNC symbols"
301     set fails [expr $fails + 1]
302 }
303 if {[contains_ifunc_symbol tmpdir/libtest-2.so] != 0} {
304     fail "libtest-2.so contains IFUNC symbols"
305     set fails [expr $fails + 1]
306 }
307
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.
312
313 if {[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 }
317 if {[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 }
321 if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
322     fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
323     set fails [expr $fails + 1]
324 }
325 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 0} {
326     fail "Dynamic ifunc-using executable contains a reloc against an IFUNC symbol"
327     set fails [expr $fails + 1]
328 }
329 if {[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
334 if { $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.
339 if { $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"
348     remote_file host delete "tmpdir/local_prog"
349     remote_file host delete "tmpdir/static_prog"
350     remote_file host delete "tmpdir/static_nonifunc_prog"
351 }
352
353 run_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
389 run_ld_link_exec_tests [list \
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     ] \
408 ]
409
410 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
411 foreach 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.
418 if { ![check_ifunc_available] } {
419     return
420 }
421
422 run_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
433 run_ld_link_exec_tests [list \
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     ] \
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     ] \
460 ]
461
462 # Run-time tests which require working ifunc attribute support.
463 if { ![check_ifunc_attribute_available] } {
464     return
465 }
466
467 run_cc_link_tests [list \
468     [list \
469         "Build pr18808a.o" \
470         "" \
471         "" \
472         { pr18808a.c } \
473         "" \
474         "" \
475     ] \
476     [list \
477         "Build libpr18808.so" \
478         "-shared" \
479         "-fPIC -O2 -g" \
480         { pr18808b.c } \
481         {} \
482         "libpr18808.so" \
483     ] \
484     [list \
485         "Build pr18841a.o" \
486         "" \
487         "" \
488         { pr18841a.c } \
489         "" \
490         "" \
491     ] \
492     [list \
493         "Build libpr18841b.so" \
494         "-shared" \
495         "-fPIC -O0 -g" \
496         { pr18841b.c } \
497         {} \
498         "libpr18841b.so" \
499     ] \
500     [list \
501         "Build libpr18841c.so" \
502         "-shared" \
503         "-fPIC -O0 -g" \
504         { pr18841c.c } \
505         {} \
506         "libpr18841c.so" \
507     ] \
508 ]
509
510 run_ld_link_exec_tests [list \
511     [list \
512         "Run pr18808" \
513         "tmpdir/pr18808a.o tmpdir/libpr18808.so" \
514         "" \
515         { dummy.c } \
516         "pr18808" \
517         "pr18808.out" \
518     ] \
519     [list \
520         "Run pr18841 with libpr18841b.so" \
521         "tmpdir/pr18841a.o tmpdir/libpr18841b.so" \
522         "" \
523         { dummy.c } \
524         "pr18841b" \
525         "pr18841.out" \
526     ] \
527     [list \
528         "Run pr18841 with libpr18841c.so" \
529         "--as-needed tmpdir/pr18841a.o tmpdir/libpr18841c.so" \
530         "" \
531         { dummy.c } \
532         "pr18841c" \
533         "pr18841.out" \
534     ] \
535 ]