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