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 | ||
26 | # IFUNC support has only been implemented for the x86_64 and ix86 so far. | |
27 | if {! ( [istarget "x86_64-*-elf*"] | |
28 | || [istarget "x86_64-*-linux*"] | |
29 | || [istarget "i?86-*-elf*"] | |
30 | || [istarget "i?86-*-linux*"]) } { | |
31 | verbose "IFUNC tests not run - target does not support IFUNC" | |
32 | return | |
33 | } | |
34 | ||
35 | # We need a native system. FIXME: Strictly speaking this | |
36 | # is not true, we just need to know how to create a fully | |
37 | # linked executable, including the C and Z libraries, using | |
38 | # the linker that is under test. | |
39 | if ![isnative] { | |
40 | verbose "IFUNC tests not run - not a native toolchain" | |
41 | return | |
42 | } | |
43 | ||
44 | # We need a working compiler. (Strictly speaking this is | |
45 | # not true, we could use target specific assembler files). | |
46 | if { [which $CC] == 0 } { | |
47 | verbose "IFUNC tests not run - no compiler available" | |
48 | return | |
49 | } | |
50 | ||
51 | # A procedure to check the OS/ABI field in the ELF header of a binary file. | |
52 | proc check_osabi { binary_file expected_osabi } { | |
53 | global READELF | |
54 | global READELFFLAGS | |
55 | ||
56 | catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got | |
57 | ||
58 | if ![string match "" $got] then { | |
59 | verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got" | |
60 | return 0 | |
61 | } | |
62 | ||
63 | if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \ | |
64 | [file_contents readelf.out] nil osabi] } { | |
65 | verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file" | |
66 | return 0 | |
67 | } | |
68 | ||
69 | if { $osabi == $expected_osabi } { | |
70 | return 1 | |
71 | } | |
72 | ||
73 | verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi" | |
74 | ||
75 | return 0 | |
76 | } | |
77 | ||
78 | # A procedure to confirm that a file contains the IFUNC symbol. | |
79 | # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found. | |
80 | proc contains_ifunc_symbol { binary_file } { | |
81 | global READELF | |
82 | global READELFFLAGS | |
83 | ||
84 | catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got | |
85 | ||
86 | if ![string match "" $got] then { | |
87 | verbose "proc contains_ifunc_symbol: Readelf produced unexpected out processing $binary_file: $got" | |
88 | return -1 | |
89 | } | |
90 | ||
91 | # Look for a line like this: | |
92 | # 58: 0000000000400600 30 IFUNC GLOBAL DEFAULT 12 library_func2 | |
93 | ||
94 | if { ![regexp ".*\[ \]*IFUNC\[ \]+GLOBAL\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+library_func2\n" [file_contents readelf.out]] } { | |
95 | return 0 | |
96 | } | |
97 | ||
98 | return 1 | |
99 | } | |
100 | ||
101 | # A procedure to confirm that a file contains a relocation that references an IFUNC symbol. | |
102 | # Returns -1 upon error, 0 if the reloc was not found and 1 if it was found. | |
103 | proc contains_ifunc_reloc { binary_file } { | |
104 | global READELF | |
105 | global READELFFLAGS | |
106 | ||
107 | catch "exec $READELF $READELFFLAGS --relocs $binary_file > readelf.out" got | |
108 | ||
109 | if ![string match "" $got] then { | |
110 | verbose "proc contains_ifunc_reloc: Readelf produced unexpected out processing $binary_file: $got" | |
111 | return -1 | |
112 | } | |
113 | ||
114 | if [string match "" [file_contents readelf.out]] then { | |
115 | verbose "No relocs found in $binary_file" | |
116 | return 0 | |
117 | } | |
118 | ||
119 | if { ![regexp "\\(\\)" [file_contents readelf.out]] } { | |
120 | return 0 | |
121 | } | |
122 | ||
123 | return 1 | |
124 | } | |
125 | ||
126 | set fails 0 | |
127 | ||
128 | # Create the object files, libraries and executables. | |
129 | if ![ld_compile "$CC -c -shared -fPIC" "$srcdir/$subdir/prog.c" "tmpdir/shared_prog.o"] { | |
130 | fail "Could not create a shared object file" | |
131 | set fails [expr $fails + 1] | |
132 | } | |
133 | if ![ld_compile "$CC -c -static" "$srcdir/$subdir/prog.c" "tmpdir/static_prog.o"] { | |
134 | fail "Could not create a static object file" | |
135 | set fails [expr $fails + 1] | |
136 | } | |
137 | if ![ld_compile "$CC -c -shared -fPIC -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/shared_ifunc.o"] { | |
138 | fail "Could not create an object file containing an IFUNC symbol" | |
139 | set fails [expr $fails + 1] | |
140 | } | |
141 | if ![ld_compile "$CC -c -static -DWITH_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_ifunc.o"] { | |
142 | fail "Could not create a static object file containing an IFUNC symbol" | |
143 | set fails [expr $fails + 1] | |
144 | } | |
145 | if ![ld_compile "$CC -c -static -DWITHOUT_IFUNC" "$srcdir/$subdir/lib.c" "tmpdir/static_noifunc.o"] { | |
146 | fail "Could not create an ordinary static object file" | |
147 | set fails [expr $fails + 1] | |
148 | } | |
149 | ||
150 | if { $fails != 0 } { | |
151 | return | |
152 | } | |
153 | ||
154 | if ![ld_simple_link $ld "tmpdir/libshared_ifunc.so" "-shared tmpdir/shared_ifunc.o"] { | |
155 | fail "Could not create a shared library containing an IFUNC symbol" | |
156 | set fails [expr $fails + 1] | |
157 | } | |
158 | if ![ar_simple_create $ar "" "tmpdir/libifunc.a" "tmpdir/static_ifunc.o"] { | |
159 | fail "Could not create a static library containing an IFUNC symbol" | |
160 | set fails [expr $fails + 1] | |
161 | } | |
162 | ||
163 | if { $fails != 0 } { | |
164 | return | |
165 | } | |
166 | ||
167 | if ![default_ld_link $ld "tmpdir/dynamic_prog" "-Ltmpdir tmpdir/shared_prog.o -Bdynamic -lshared_ifunc -rpath ./tmpdir"] { | |
168 | fail "Could not link a dynamic executable" | |
169 | set fails [expr $fails + 1] | |
170 | } | |
171 | if ![default_ld_link $ld "tmpdir/static_prog" "-Ltmpdir tmpdir/static_prog.o -lifunc"] { | |
172 | fail "Could not link a static executable" | |
173 | set fails [expr $fails + 1] | |
174 | } | |
175 | if ![default_ld_link $ld "tmpdir/static_nonifunc_prog" "-Ltmpdir tmpdir/static_prog.o tmpdir/static_noifunc.o"] { | |
176 | fail "Could not link a non-ifunc using static executable" | |
177 | set fails [expr $fails + 1] | |
178 | } | |
179 | ||
180 | if { $fails == 0 } { | |
181 | pass "Building ifunc binaries" | |
182 | set fails 0 | |
183 | } else { | |
184 | return | |
185 | } | |
186 | ||
187 | # Check the executables. | |
188 | # | |
189 | # The linked ifunc using executables should have an OSABI field of LINUX | |
190 | # The linked non-ifunc using executable should have an OSABI field of NONE (aka System V). | |
191 | ||
192 | if {! [check_osabi tmpdir/static_prog {UNIX - Linux}]} { | |
193 | fail "Static ifunc-using executable does not have an OS/ABI field of LINUX" | |
194 | set fails [expr $fails + 1] | |
195 | } | |
196 | if {! [check_osabi tmpdir/dynamic_prog {UNIX - Linux}]} { | |
197 | fail "Dynamic ifunc-using executable does not have an OS/ABI field of LINUX" | |
198 | set fails [expr $fails + 1] | |
199 | } | |
200 | if {! [check_osabi tmpdir/static_nonifunc_prog {UNIX - System V}]} { | |
201 | fail "Static non-ifunc-using executable does not have an OS/ABI field of System V" | |
202 | set fails [expr $fails + 1] | |
203 | } | |
204 | ||
205 | # The linked ifunc using executables should contain an IFUNC symbol, | |
206 | # The non-ifunc using executable should not. | |
207 | ||
208 | if {[contains_ifunc_symbol tmpdir/static_prog] != 1} { | |
209 | fail "Static ifunc-using executable does not contain an IFUNC symbol" | |
210 | set fails [expr $fails + 1] | |
211 | } | |
212 | if {[contains_ifunc_symbol tmpdir/dynamic_prog] != 1} { | |
213 | fail "Dynamic ifunc-using executable does not contain an IFUNC symbol" | |
214 | set fails [expr $fails + 1] | |
215 | } | |
216 | if {[contains_ifunc_symbol tmpdir/static_nonifunc_prog] != 0} { | |
217 | fail "Static non-ifunc-using executable contains an IFUNC symbol" | |
218 | set fails [expr $fails + 1] | |
219 | } | |
220 | ||
221 | # The linked ifunc using executablea should contain a dynamic reloc referencing the IFUNC symbol. | |
222 | # (Even the static executable which should have a dynamic section created for it). | |
223 | # The non-ifunc using executable should not. | |
224 | ||
225 | if {[contains_ifunc_reloc tmpdir/static_prog] != 1} { | |
226 | fail "Static ifunc-using executable does not contain a reloc against an IFUNC symbol" | |
227 | set fails [expr $fails + 1] | |
228 | } | |
229 | if {[contains_ifunc_reloc tmpdir/dynamic_prog] != 1} { | |
230 | fail "Dynamic ifunc-using executable does not contain a reloc against an IFUNC symbol" | |
231 | set fails [expr $fails + 1] | |
232 | } | |
233 | if {[contains_ifunc_reloc tmpdir/static_nonifunc_prog] == 1} { | |
234 | fail "Static non-ifunc-using executable contains a reloc against an IFUNC symbol!" | |
235 | set fails [expr $fails + 1] | |
236 | } | |
237 | ||
238 | if { $fails == 0 } { | |
239 | pass "Checking ifunc binaries" | |
240 | } | |
241 | ||
242 | # Clean up, unless we are being verbose, in which case we leave the files available. | |
243 | if { $verbose < 1 } { | |
244 | remote_file host delete "tmpdir/shared_prog.o" | |
245 | remote_file host delete "tmpdir/static_prog.o" | |
246 | remote_file host delete "tmpdir/shared_ifunc.o" | |
247 | remote_file host delete "tmpdir/static_ifunc.o" | |
248 | remote_file host delete "tmpdir/static_noifunc.o" | |
249 | remote_file host delete "tmpdir/libshared_ifunc.so" | |
250 | remote_file host delete "tmpdir/libifunc.a" | |
251 | remote_file host delete "tmpdir/dynamic_prog" | |
252 | remote_file host delete "tmpdir/static_prog" | |
253 | remote_file host delete "tmpdir/static_nonifunc_prog" | |
254 | } |