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