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