]>
Commit | Line | Data |
---|---|---|
53cea63e | 1 | /* Hardware capability support for run-time dynamic loader. |
bfff8b1b | 2 | Copyright (C) 2012-2017 Free Software Foundation, Inc. |
53cea63e L |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <assert.h> | |
20 | #include <elf.h> | |
21 | #include <errno.h> | |
22 | #include <libintl.h> | |
23 | #include <unistd.h> | |
24 | #include <ldsodefs.h> | |
25 | ||
26 | #include <dl-procinfo.h> | |
ff08fc59 | 27 | #include <dl-hwcaps.h> |
53cea63e L |
28 | |
29 | #ifdef _DL_FIRST_PLATFORM | |
30 | # define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT) | |
31 | #else | |
32 | # define _DL_FIRST_EXTRA _DL_HWCAP_COUNT | |
33 | #endif | |
34 | ||
35 | /* Return an array of useful/necessary hardware capability names. */ | |
36 | const struct r_strlenpair * | |
37 | internal_function | |
38 | _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz, | |
39 | size_t *max_capstrlen) | |
40 | { | |
ff08fc59 | 41 | uint64_t hwcap_mask = GET_HWCAP_MASK(); |
53cea63e | 42 | /* Determine how many important bits are set. */ |
ff08fc59 | 43 | uint64_t masked = GLRO(dl_hwcap) & hwcap_mask; |
53cea63e L |
44 | size_t cnt = platform != NULL; |
45 | size_t n, m; | |
46 | size_t total; | |
53cea63e L |
47 | struct r_strlenpair *result; |
48 | struct r_strlenpair *rp; | |
49 | char *cp; | |
50 | ||
51 | /* Count the number of bits set in the masked value. */ | |
52 | for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n) | |
53 | if ((masked & (1ULL << n)) != 0) | |
54 | ++cnt; | |
55 | ||
7775448e | 56 | #ifdef NEED_DL_SYSINFO_DSO |
53cea63e L |
57 | /* The system-supplied DSO can contain a note of type 2, vendor "GNU". |
58 | This gives us a list of names to treat as fake hwcap bits. */ | |
59 | ||
60 | const char *dsocaps = NULL; | |
61 | size_t dsocapslen = 0; | |
62 | if (GLRO(dl_sysinfo_map) != NULL) | |
63 | { | |
64 | const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr; | |
65 | const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum; | |
66 | for (uint_fast16_t i = 0; i < phnum; ++i) | |
67 | if (phdr[i].p_type == PT_NOTE) | |
68 | { | |
69 | const ElfW(Addr) start = (phdr[i].p_vaddr | |
70 | + GLRO(dl_sysinfo_map)->l_addr); | |
141af660 CD |
71 | /* The standard ELF note layout is exactly as the anonymous struct. |
72 | The next element is a variable length vendor name of length | |
13e23af7 | 73 | VENDORLEN (with a real length rounded to ElfW(Word)), followed |
141af660 | 74 | by the data of length DATALEN (with a real length rounded to |
13e23af7 | 75 | ElfW(Word)). */ |
53cea63e L |
76 | const struct |
77 | { | |
78 | ElfW(Word) vendorlen; | |
79 | ElfW(Word) datalen; | |
80 | ElfW(Word) type; | |
81 | } *note = (const void *) start; | |
82 | while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz) | |
83 | { | |
84 | #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word))) | |
141af660 CD |
85 | /* The layout of the type 2, vendor "GNU" note is as follows: |
86 | .long <Number of capabilities enabled by this note> | |
87 | .long <Capabilities mask> (as mask >> _DL_FIRST_EXTRA). | |
88 | .byte <The bit number for the next capability> | |
89 | .asciz <The name of the capability>. */ | |
53cea63e L |
90 | if (note->type == NT_GNU_HWCAP |
91 | && note->vendorlen == sizeof "GNU" | |
92 | && !memcmp ((note + 1), "GNU", sizeof "GNU") | |
93 | && note->datalen > 2 * sizeof (ElfW(Word)) + 2) | |
94 | { | |
95 | const ElfW(Word) *p = ((const void *) (note + 1) | |
96 | + ROUND (sizeof "GNU")); | |
97 | cnt += *p++; | |
98 | ++p; /* Skip mask word. */ | |
141af660 | 99 | dsocaps = (const char *) p; /* Pseudo-string "<b>name" */ |
53cea63e L |
100 | dsocapslen = note->datalen - sizeof *p * 2; |
101 | break; | |
102 | } | |
103 | note = ((const void *) (note + 1) | |
104 | + ROUND (note->vendorlen) + ROUND (note->datalen)); | |
105 | #undef ROUND | |
106 | } | |
107 | if (dsocaps != NULL) | |
108 | break; | |
109 | } | |
110 | } | |
111 | #endif | |
112 | ||
113 | /* For TLS enabled builds always add 'tls'. */ | |
114 | ++cnt; | |
115 | ||
116 | /* Create temporary data structure to generate result table. */ | |
573c29b2 | 117 | struct r_strlenpair temp[cnt]; |
53cea63e | 118 | m = 0; |
7775448e | 119 | #ifdef NEED_DL_SYSINFO_DSO |
53cea63e L |
120 | if (dsocaps != NULL) |
121 | { | |
141af660 CD |
122 | /* dsocaps points to the .asciz string, and -1 points to the mask |
123 | .long just before the string. */ | |
53cea63e L |
124 | const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1]; |
125 | GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA; | |
126 | /* Note that we add the dsocaps to the set already chosen by the | |
127 | LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT). | |
128 | So there is no way to request ignoring an OS-supplied dsocap | |
129 | string and bit like you can ignore an OS-supplied HWCAP bit. */ | |
ff08fc59 SP |
130 | hwcap_mask |= (uint64_t) mask << _DL_FIRST_EXTRA; |
131 | #if HAVE_TUNABLES | |
132 | TUNABLE_SET (glibc, tune, hwcap_mask, uint64_t, hwcap_mask); | |
133 | #else | |
134 | GLRO(dl_hwcap_mask) = hwcap_mask; | |
135 | #endif | |
53cea63e L |
136 | size_t len; |
137 | for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1) | |
138 | { | |
139 | uint_fast8_t bit = *p++; | |
140 | len = strlen (p); | |
141 | ||
142 | /* Skip entries that are not enabled in the mask word. */ | |
a1ffb40e | 143 | if (__glibc_likely (mask & ((ElfW(Word)) 1 << bit))) |
53cea63e L |
144 | { |
145 | temp[m].str = p; | |
146 | temp[m].len = len; | |
147 | ++m; | |
148 | } | |
149 | else | |
150 | --cnt; | |
151 | } | |
152 | } | |
153 | #endif | |
154 | for (n = 0; masked != 0; ++n) | |
155 | if ((masked & (1ULL << n)) != 0) | |
156 | { | |
157 | temp[m].str = _dl_hwcap_string (n); | |
158 | temp[m].len = strlen (temp[m].str); | |
159 | masked ^= 1ULL << n; | |
160 | ++m; | |
161 | } | |
162 | if (platform != NULL) | |
163 | { | |
164 | temp[m].str = platform; | |
165 | temp[m].len = platform_len; | |
166 | ++m; | |
167 | } | |
168 | ||
169 | temp[m].str = "tls"; | |
170 | temp[m].len = 3; | |
171 | ++m; | |
172 | ||
173 | assert (m == cnt); | |
174 | ||
175 | /* Determine the total size of all strings together. */ | |
176 | if (cnt == 1) | |
177 | total = temp[0].len + 1; | |
178 | else | |
179 | { | |
180 | total = temp[0].len + temp[cnt - 1].len + 2; | |
181 | if (cnt > 2) | |
182 | { | |
183 | total <<= 1; | |
184 | for (n = 1; n + 1 < cnt; ++n) | |
185 | total += temp[n].len + 1; | |
186 | if (cnt > 3 | |
187 | && (cnt >= sizeof (size_t) * 8 | |
188 | || total + (sizeof (*result) << 3) | |
189 | >= (1UL << (sizeof (size_t) * 8 - cnt + 3)))) | |
190 | _dl_signal_error (ENOMEM, NULL, NULL, | |
191 | N_("cannot create capability list")); | |
192 | ||
193 | total <<= cnt - 3; | |
194 | } | |
195 | } | |
196 | ||
197 | /* The result structure: we use a very compressed way to store the | |
198 | various combinations of capability names. */ | |
199 | *sz = 1 << cnt; | |
200 | result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total); | |
201 | if (result == NULL) | |
202 | _dl_signal_error (ENOMEM, NULL, NULL, | |
203 | N_("cannot create capability list")); | |
204 | ||
205 | if (cnt == 1) | |
206 | { | |
207 | result[0].str = (char *) (result + *sz); | |
208 | result[0].len = temp[0].len + 1; | |
209 | result[1].str = (char *) (result + *sz); | |
210 | result[1].len = 0; | |
211 | cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len); | |
212 | *cp = '/'; | |
213 | *sz = 2; | |
214 | *max_capstrlen = result[0].len; | |
215 | ||
216 | return result; | |
217 | } | |
218 | ||
219 | /* Fill in the information. This follows the following scheme | |
573c29b2 | 220 | (indices from TEMP for four strings): |
53cea63e L |
221 | entry #0: 0, 1, 2, 3 binary: 1111 |
222 | #1: 0, 1, 3 1101 | |
223 | #2: 0, 2, 3 1011 | |
224 | #3: 0, 3 1001 | |
225 | This allows the representation of all possible combinations of | |
226 | capability names in the string. First generate the strings. */ | |
227 | result[1].str = result[0].str = cp = (char *) (result + *sz); | |
228 | #define add(idx) \ | |
229 | cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1); | |
230 | if (cnt == 2) | |
231 | { | |
232 | add (1); | |
233 | add (0); | |
234 | } | |
235 | else | |
236 | { | |
237 | n = 1 << (cnt - 1); | |
238 | do | |
239 | { | |
240 | n -= 2; | |
241 | ||
242 | /* We always add the last string. */ | |
243 | add (cnt - 1); | |
244 | ||
245 | /* Add the strings which have the bit set in N. */ | |
246 | for (m = cnt - 2; m > 0; --m) | |
247 | if ((n & (1 << m)) != 0) | |
248 | add (m); | |
249 | ||
250 | /* Always add the first string. */ | |
251 | add (0); | |
252 | } | |
253 | while (n != 0); | |
254 | } | |
255 | #undef add | |
256 | ||
257 | /* Now we are ready to install the string pointers and length. */ | |
258 | for (n = 0; n < (1UL << cnt); ++n) | |
259 | result[n].len = 0; | |
260 | n = cnt; | |
261 | do | |
262 | { | |
263 | size_t mask = 1 << --n; | |
264 | ||
265 | rp = result; | |
266 | for (m = 1 << cnt; m > 0; ++rp) | |
267 | if ((--m & mask) != 0) | |
268 | rp->len += temp[n].len + 1; | |
269 | } | |
270 | while (n != 0); | |
271 | ||
272 | /* The first half of the strings all include the first string. */ | |
273 | n = (1 << cnt) - 2; | |
274 | rp = &result[2]; | |
275 | while (n != (1UL << (cnt - 1))) | |
276 | { | |
277 | if ((--n & 1) != 0) | |
278 | rp[0].str = rp[-2].str + rp[-2].len; | |
279 | else | |
280 | rp[0].str = rp[-1].str; | |
281 | ++rp; | |
282 | } | |
283 | ||
284 | /* The second half starts right after the first part of the string of | |
285 | the corresponding entry in the first half. */ | |
286 | do | |
287 | { | |
288 | rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1; | |
289 | ++rp; | |
290 | } | |
291 | while (--n != 0); | |
292 | ||
293 | /* The maximum string length. */ | |
294 | *max_capstrlen = result[0].len; | |
295 | ||
296 | return result; | |
297 | } |