Add new templates for IEEE wrappers
[glibc.git] / elf / dl-hwcaps.c
blobc437397a8809ba9eed50d7e1bf4ec6f7d03584de
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.
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.
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.
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/>. */
19 #include <assert.h>
20 #include <elf.h>
21 #include <errno.h>
22 #include <libintl.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
26 #include <dl-procinfo.h>
28 #ifdef _DL_FIRST_PLATFORM
29 # define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT)
30 #else
31 # define _DL_FIRST_EXTRA _DL_HWCAP_COUNT
32 #endif
34 /* Return an array of useful/necessary hardware capability names. */
35 const struct r_strlenpair *
36 internal_function
37 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
38 size_t *max_capstrlen)
40 /* Determine how many important bits are set. */
41 uint64_t masked = GLRO(dl_hwcap) & GLRO(dl_hwcap_mask);
42 size_t cnt = platform != NULL;
43 size_t n, m;
44 size_t total;
45 struct r_strlenpair *result;
46 struct r_strlenpair *rp;
47 char *cp;
49 /* Count the number of bits set in the masked value. */
50 for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n)
51 if ((masked & (1ULL << n)) != 0)
52 ++cnt;
54 #ifdef NEED_DL_SYSINFO_DSO
55 /* The system-supplied DSO can contain a note of type 2, vendor "GNU".
56 This gives us a list of names to treat as fake hwcap bits. */
58 const char *dsocaps = NULL;
59 size_t dsocapslen = 0;
60 if (GLRO(dl_sysinfo_map) != NULL)
62 const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
63 const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
64 for (uint_fast16_t i = 0; i < phnum; ++i)
65 if (phdr[i].p_type == PT_NOTE)
67 const ElfW(Addr) start = (phdr[i].p_vaddr
68 + GLRO(dl_sysinfo_map)->l_addr);
69 /* The standard ELF note layout is exactly as the anonymous struct.
70 The next element is a variable length vendor name of length
71 VENDORLEN (with a real length rounded to ElfW(Word)), followed
72 by the data of length DATALEN (with a real length rounded to
73 ElfW(Word)). */
74 const struct
76 ElfW(Word) vendorlen;
77 ElfW(Word) datalen;
78 ElfW(Word) type;
79 } *note = (const void *) start;
80 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
82 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
83 /* The layout of the type 2, vendor "GNU" note is as follows:
84 .long <Number of capabilities enabled by this note>
85 .long <Capabilities mask> (as mask >> _DL_FIRST_EXTRA).
86 .byte <The bit number for the next capability>
87 .asciz <The name of the capability>. */
88 if (note->type == NT_GNU_HWCAP
89 && note->vendorlen == sizeof "GNU"
90 && !memcmp ((note + 1), "GNU", sizeof "GNU")
91 && note->datalen > 2 * sizeof (ElfW(Word)) + 2)
93 const ElfW(Word) *p = ((const void *) (note + 1)
94 + ROUND (sizeof "GNU"));
95 cnt += *p++;
96 ++p; /* Skip mask word. */
97 dsocaps = (const char *) p; /* Pseudo-string "<b>name" */
98 dsocapslen = note->datalen - sizeof *p * 2;
99 break;
101 note = ((const void *) (note + 1)
102 + ROUND (note->vendorlen) + ROUND (note->datalen));
103 #undef ROUND
105 if (dsocaps != NULL)
106 break;
109 #endif
111 /* For TLS enabled builds always add 'tls'. */
112 ++cnt;
114 /* Create temporary data structure to generate result table. */
115 struct r_strlenpair temp[cnt];
116 m = 0;
117 #ifdef NEED_DL_SYSINFO_DSO
118 if (dsocaps != NULL)
120 /* dsocaps points to the .asciz string, and -1 points to the mask
121 .long just before the string. */
122 const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
123 GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
124 /* Note that we add the dsocaps to the set already chosen by the
125 LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
126 So there is no way to request ignoring an OS-supplied dsocap
127 string and bit like you can ignore an OS-supplied HWCAP bit. */
128 GLRO(dl_hwcap_mask) |= (uint64_t) mask << _DL_FIRST_EXTRA;
129 size_t len;
130 for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
132 uint_fast8_t bit = *p++;
133 len = strlen (p);
135 /* Skip entries that are not enabled in the mask word. */
136 if (__glibc_likely (mask & ((ElfW(Word)) 1 << bit)))
138 temp[m].str = p;
139 temp[m].len = len;
140 ++m;
142 else
143 --cnt;
146 #endif
147 for (n = 0; masked != 0; ++n)
148 if ((masked & (1ULL << n)) != 0)
150 temp[m].str = _dl_hwcap_string (n);
151 temp[m].len = strlen (temp[m].str);
152 masked ^= 1ULL << n;
153 ++m;
155 if (platform != NULL)
157 temp[m].str = platform;
158 temp[m].len = platform_len;
159 ++m;
162 temp[m].str = "tls";
163 temp[m].len = 3;
164 ++m;
166 assert (m == cnt);
168 /* Determine the total size of all strings together. */
169 if (cnt == 1)
170 total = temp[0].len + 1;
171 else
173 total = temp[0].len + temp[cnt - 1].len + 2;
174 if (cnt > 2)
176 total <<= 1;
177 for (n = 1; n + 1 < cnt; ++n)
178 total += temp[n].len + 1;
179 if (cnt > 3
180 && (cnt >= sizeof (size_t) * 8
181 || total + (sizeof (*result) << 3)
182 >= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
183 _dl_signal_error (ENOMEM, NULL, NULL,
184 N_("cannot create capability list"));
186 total <<= cnt - 3;
190 /* The result structure: we use a very compressed way to store the
191 various combinations of capability names. */
192 *sz = 1 << cnt;
193 result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
194 if (result == NULL)
195 _dl_signal_error (ENOMEM, NULL, NULL,
196 N_("cannot create capability list"));
198 if (cnt == 1)
200 result[0].str = (char *) (result + *sz);
201 result[0].len = temp[0].len + 1;
202 result[1].str = (char *) (result + *sz);
203 result[1].len = 0;
204 cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
205 *cp = '/';
206 *sz = 2;
207 *max_capstrlen = result[0].len;
209 return result;
212 /* Fill in the information. This follows the following scheme
213 (indices from TEMP for four strings):
214 entry #0: 0, 1, 2, 3 binary: 1111
215 #1: 0, 1, 3 1101
216 #2: 0, 2, 3 1011
217 #3: 0, 3 1001
218 This allows the representation of all possible combinations of
219 capability names in the string. First generate the strings. */
220 result[1].str = result[0].str = cp = (char *) (result + *sz);
221 #define add(idx) \
222 cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
223 if (cnt == 2)
225 add (1);
226 add (0);
228 else
230 n = 1 << (cnt - 1);
233 n -= 2;
235 /* We always add the last string. */
236 add (cnt - 1);
238 /* Add the strings which have the bit set in N. */
239 for (m = cnt - 2; m > 0; --m)
240 if ((n & (1 << m)) != 0)
241 add (m);
243 /* Always add the first string. */
244 add (0);
246 while (n != 0);
248 #undef add
250 /* Now we are ready to install the string pointers and length. */
251 for (n = 0; n < (1UL << cnt); ++n)
252 result[n].len = 0;
253 n = cnt;
256 size_t mask = 1 << --n;
258 rp = result;
259 for (m = 1 << cnt; m > 0; ++rp)
260 if ((--m & mask) != 0)
261 rp->len += temp[n].len + 1;
263 while (n != 0);
265 /* The first half of the strings all include the first string. */
266 n = (1 << cnt) - 2;
267 rp = &result[2];
268 while (n != (1UL << (cnt - 1)))
270 if ((--n & 1) != 0)
271 rp[0].str = rp[-2].str + rp[-2].len;
272 else
273 rp[0].str = rp[-1].str;
274 ++rp;
277 /* The second half starts right after the first part of the string of
278 the corresponding entry in the first half. */
281 rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
282 ++rp;
284 while (--n != 0);
286 /* The maximum string length. */
287 *max_capstrlen = result[0].len;
289 return result;