2009-07-10 H.J. Lu <hongjiu.lu@intel.com>
[external/binutils.git] / ld / testsuite / ld-ifunc / ifunc.exp
1 # Expect script for linker support of IFUNC symbols and relocations.
2 #
3 #   Copyright 2009  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 and powerpc
27 # so far.
28 if {!(([istarget "i?86-*-*"]
29        || [istarget "x86_64-*-*"]
30        || [istarget "powerpc*-*-*"])
31       && ([istarget "*-*-elf*"]
32           || ([istarget "*-*-linux*"]
33               && ![istarget "*-*-*aout*"]
34               && ![istarget "*-*-*oldld*"]))) } {
35     verbose "IFUNC tests not run - target does not support IFUNC"
36     return
37 }
38
39 # We need a native system.  FIXME: Strictly speaking this
40 # is not true, we just need to know how to create a fully
41 # linked executable, including the C and Z libraries, using
42 # the linker that is under test.
43 if ![isnative] {
44     verbose "IFUNC tests not run - not a native toolchain"
45     return
46 }
47
48 # We need a working compiler.  (Strictly speaking this is
49 # not true, we could use target specific assembler files).
50 if { [which $CC] == 0 } {
51     verbose "IFUNC tests not run - no compiler available"
52     return
53 }
54
55 # A procedure to check the OS/ABI field in the ELF header of a binary file.
56 proc check_osabi { binary_file expected_osabi } {
57     global READELF
58     global READELFFLAGS
59
60     catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
61
62     if ![string match "" $got] then {
63         verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
64         return 0
65     }
66
67     if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
68            [file_contents readelf.out] nil osabi] } {
69         verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
70         return 0
71     }
72
73     if { $osabi == $expected_osabi } {
74         return 1
75     }
76
77     verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
78     
79     return 0
80 }
81
82 # A procedure to confirm that a file contains the IFUNC symbol.
83 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
84 proc contains_ifunc_symbol { binary_file } {
85     global READELF
86     global READELFFLAGS
87
88     catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
89
90     if ![string match "" $got] then {
91         verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got"
92         return -1
93     }
94
95     # Look for a line like this:
96     #    58: 0000000000400600    30 IFUNC   GLOBAL DEFAULT   12 library_func2
97
98     if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } {
99         return 0
100     }
101
102     return 1
103 }
104
105 # A procedure to confirm that a file contains the R_*_IRELATIVE
106 # relocation.
107 # Returns -1 upon error, 0 if the relocation was not found and 1 if
108 # it was found.
109 proc contains_irelative_reloc { binary_file } {
110     global READELF
111     global READELFFLAGS
112
113     catch "exec $READELF $READELFFLAGS --relocs --wide $binary_file > readelf.out" got
114
115     if ![string match "" $got] then {
116         verbose "proc contains_irelative_reloc: Readelf produced unexpected out processing $binary_file: $got"
117         return -1
118     }
119
120     # Look for a line like this:
121     #    0000000000600ab0  0000000000000025 R_X86_64_IRELATIVE      000000000040061c
122     #    080496f4  0000002a R_386_IRELATIVE
123
124
125     if { ![regexp "\[0-9a-f\]+\[ \]+\[0-9a-f\]+\[ \]+R_\[_0-9A-Z\]+_IRELATIVE\[ \]*\[0-9a-f\]*\n" [file_contents readelf.out]] } {
126         return 0
127     }
128
129     return 1
130 }
131
132 # A procedure to confirm that a file contains a relocation that references an IFUNC symbol.
133 # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found.
134 proc contains_ifunc_reloc { binary_file } {
135     global READELF
136     global READELFFLAGS
137
138     catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got
139
140     if ![string match "" $got] then {
141         verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got"
142         return -1
143     }
144
145     if [string match "" [file_contents readelf.out]] then {
146         verbose "No relocs found in $binary_file"
147         return 0
148     }
149
150     if { ![regexp "\\(\\)" [file_contents readelf.out]] } {
151         return 0
152     }
153
154     return 1
155 }
156
157 set fails 0
158
159 # Create the object files, libraries and executables.
160 if ![ld_compile "$CC -c -shared -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] {
161     fail "Could not create a shared object file"
162     set fails [expr $fails + 1]
163 }
164 if ![ld_compile "$CC -c -static" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] {
165     fail "Could not create a static object file"
166     set fails [expr $fails + 1]
167 }
168 if ![ld_compile "$CC -c -shared -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] {
169     fail "Could not create an object file containing an IFUNC symbol"
170     set fails [expr $fails + 1]
171 }
172 if ![ld_compile "$CC -c -static -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] {
173     fail "Could not create a static object file containing an IFUNC symbol"
174     set fails [expr $fails + 1]
175 }
176 if ![ld_compile "$CC -c -static -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] {
177     fail "Could not create an ordinary static object file"
178     set fails [expr $fails + 1]
179 }
180
181 if { $fails != 0 } {
182     return
183 }
184
185 if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] {
186     fail "Could not create a shared library containing an IFUNC symbol"
187     set fails [expr $fails + 1]
188 }
189 if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] {
190     fail "Could not create a static library containing an IFUNC symbol"
191     set fails [expr $fails + 1]
192 }
193
194 if { $fails != 0 } {
195     return
196 }
197
198 if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] {
199     fail "Could not link a dynamic executable"
200     set fails [expr $fails + 1]
201 }
202 if ![default_ld_link $ld "tmpdir/local_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] {
203     fail "Could not link a dynamic executable using local ifunc"
204     set fails [expr $fails + 1]
205 }
206 if ![default_ld_link $ld "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
207     fail "Could not link a static executable"
208     set fails [expr $fails + 1]
209 }
210 if ![default_ld_link $ld "tmpdir/static_nonifunc_prog" "-static -Ltmpdir tmpdir/static_prog.o tmpdir/static_noifunc.o"] {
211     fail "Could not link a non-ifunc using static executable"
212     set fails [expr $fails + 1]
213 }
214
215 if { $fails == 0 } {
216   pass "Building ifunc binaries"
217   set fails 0
218 } else {
219     return
220 }
221
222 # Check the executables and shared libraries
223 #
224 # The linked ifunc using executables and the shared library containing
225 # ifunc should have an OSABI field of LINUX.  The linked non-ifunc using
226 # executable should have an OSABI field of NONE (aka System V).
227
228 if {! [check_osabi tmpdir/libshared_ifunc.so {UNIX - Linux}]} {
229     fail "Shared libraries containing ifunc does not have an OS/ABI field of LINUX"
230     set fails [expr $fails + 1]
231 }
232 if {! [check_osabi tmpdir/local_prog {UNIX - Linux}]} {
233     fail "Local ifunc-using executable does not have an OS/ABI field of LINUX"
234     set fails [expr $fails + 1]
235 }
236 if {! [check_osabi tmpdir/static_prog {UNIX - Linux}]} {
237     fail "Static ifunc-using executable does not have an OS/ABI field of LINUX"
238     set fails [expr $fails + 1]
239 }
240 if {! [check_osabi tmpdir/dynamic_prog {UNIX - Linux}]} {
241     fail "Dynamic ifunc-using executable does not have an OS/ABI field of LINUX"
242     set fails [expr $fails + 1]
243 }
244 # FIXME: This test will fail if libc.a has ifunc functons.
245 #if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} {
246 #    fail "Static non-ifunc-using executable does not have an OS/ABI field of System V"
247 #    set fails [expr $fails + 1]
248 #}
249
250 # The linked ifunc using executables and the shared library containing
251 # ifunc should contain an IFUNC symbol.  The non-ifunc using executable
252 # should not.
253
254 if {[contains_ifunc_symbol tmpdir/libshared_ifunc.so] != 1} {
255     fail "Shared libraries containing ifunc does not contain an IFUNC symbol"
256     set fails [expr $fails + 1]
257 }
258 if {[contains_ifunc_symbol tmpdir/local_prog] != 1} {
259     fail "Local ifunc-using executable does not contain an IFUNC symbol"
260     set fails [expr $fails + 1]
261 }
262 if {[contains_ifunc_symbol tmpdir/static_prog] != 1} {
263     fail "Static ifunc-using executable does not contain an IFUNC symbol"
264     set fails [expr $fails + 1]
265 }
266 if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 1} {
267     fail "Dynamic ifunc-using executable does not contain an IFUNC symbol"
268     set fails [expr $fails + 1]
269 }
270 if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} {
271     fail "Static non-ifunc-using executable contains an IFUNC symbol"
272     set fails [expr $fails + 1]
273 }
274
275 # The linked ifunc using executables and shared libraries should contain
276 # a dynamic reloc referencing the IFUNC symbol.  (Even the static
277 # executable which should have a dynamic section created for it).  The
278 # non-ifunc using executable should not.
279
280 if {[contains_irelative_reloc tmpdir/libshared_ifunc.so] != 1} {
281     fail "ifunc-using shared library does not contain R_*_IRELATIVE relocation"
282     set fails [expr $fails + 1]
283 }
284 if {[contains_irelative_reloc tmpdir/local_prog] != 1} {
285     fail "Local ifunc-using executable does not contain R_*_IRELATIVE relocation"
286     set fails [expr $fails + 1]
287 }
288 if {[contains_irelative_reloc tmpdir/static_prog] != 1} {
289     fail "Static ifunc-using executable does not contain R_*_IRELATIVE relocation"
290     set fails [expr $fails + 1]
291 }
292 if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 1} {
293     fail "Dynamic ifunc-using executable does not contain a reloc against an IFUNC symbol"
294     set fails [expr $fails + 1]
295 }
296 if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} {
297     fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!"
298     set fails [expr $fails + 1]
299 }
300
301 if { $fails == 0 } {
302   pass "Checking ifunc binaries"
303 }
304
305 # Clean up, unless we are being verbose, in which case we leave the files available.
306 if { $verbose < 1 } {
307     remote_file host delete "tmpdir/shared_prog.o"
308     remote_file host delete "tmpdir/static_prog.o"
309     remote_file host delete "tmpdir/shared_ifunc.o"
310     remote_file host delete "tmpdir/static_ifunc.o"
311     remote_file host delete "tmpdir/static_noifunc.o"
312     remote_file host delete "tmpdir/libshared_ifunc.so"
313     remote_file host delete "tmpdir/libifunc.a"
314     remote_file host delete "tmpdir/dynamic_prog"
315     remote_file host delete "tmpdir/local_prog"
316     remote_file host delete "tmpdir/static_prog"
317     remote_file host delete "tmpdir/static_nonifunc_prog"
318 }
319
320 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
321 foreach t $test_list {
322     # We need to strip the ".d", but can leave the dirname.
323     verbose [file rootname $t]
324     run_dump_test [file rootname $t]
325 }