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