Update copyright notices with scripts/update-copyrights.
[glibc.git] / elf / dl-hwcaps.c
blob3805949e8afe90e7c7553987bfc870bf16b40bd3
1 /* Hardware capability support for run-time dynamic loader.
2 Copyright (C) 2012-2013 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 *temp;
46 struct r_strlenpair *result;
47 struct r_strlenpair *rp;
48 char *cp;
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;
55 #if defined NEED_DL_SYSINFO || defined 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. */
59 const char *dsocaps = NULL;
60 size_t dsocapslen = 0;
61 if (GLRO(dl_sysinfo_map) != NULL)
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)
68 const ElfW(Addr) start = (phdr[i].p_vaddr
69 + GLRO(dl_sysinfo_map)->l_addr);
70 const struct
72 ElfW(Word) vendorlen;
73 ElfW(Word) datalen;
74 ElfW(Word) type;
75 } *note = (const void *) start;
76 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
78 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
79 if (note->type == NT_GNU_HWCAP
80 && note->vendorlen == sizeof "GNU"
81 && !memcmp ((note + 1), "GNU", sizeof "GNU")
82 && note->datalen > 2 * sizeof (ElfW(Word)) + 2)
84 const ElfW(Word) *p = ((const void *) (note + 1)
85 + ROUND (sizeof "GNU"));
86 cnt += *p++;
87 ++p; /* Skip mask word. */
88 dsocaps = (const char *) p;
89 dsocapslen = note->datalen - sizeof *p * 2;
90 break;
92 note = ((const void *) (note + 1)
93 + ROUND (note->vendorlen) + ROUND (note->datalen));
94 #undef ROUND
96 if (dsocaps != NULL)
97 break;
100 #endif
102 /* For TLS enabled builds always add 'tls'. */
103 ++cnt;
105 /* Create temporary data structure to generate result table. */
106 temp = (struct r_strlenpair *) alloca (cnt * sizeof (*temp));
107 m = 0;
108 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
109 if (dsocaps != NULL)
111 const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
112 GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
113 /* Note that we add the dsocaps to the set already chosen by the
114 LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
115 So there is no way to request ignoring an OS-supplied dsocap
116 string and bit like you can ignore an OS-supplied HWCAP bit. */
117 GLRO(dl_hwcap_mask) |= (uint64_t) mask << _DL_FIRST_EXTRA;
118 size_t len;
119 for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
121 uint_fast8_t bit = *p++;
122 len = strlen (p);
124 /* Skip entries that are not enabled in the mask word. */
125 if (__builtin_expect (mask & ((ElfW(Word)) 1 << bit), 1))
127 temp[m].str = p;
128 temp[m].len = len;
129 ++m;
131 else
132 --cnt;
135 #endif
136 for (n = 0; masked != 0; ++n)
137 if ((masked & (1ULL << n)) != 0)
139 temp[m].str = _dl_hwcap_string (n);
140 temp[m].len = strlen (temp[m].str);
141 masked ^= 1ULL << n;
142 ++m;
144 if (platform != NULL)
146 temp[m].str = platform;
147 temp[m].len = platform_len;
148 ++m;
151 temp[m].str = "tls";
152 temp[m].len = 3;
153 ++m;
155 assert (m == cnt);
157 /* Determine the total size of all strings together. */
158 if (cnt == 1)
159 total = temp[0].len + 1;
160 else
162 total = temp[0].len + temp[cnt - 1].len + 2;
163 if (cnt > 2)
165 total <<= 1;
166 for (n = 1; n + 1 < cnt; ++n)
167 total += temp[n].len + 1;
168 if (cnt > 3
169 && (cnt >= sizeof (size_t) * 8
170 || total + (sizeof (*result) << 3)
171 >= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
172 _dl_signal_error (ENOMEM, NULL, NULL,
173 N_("cannot create capability list"));
175 total <<= cnt - 3;
179 /* The result structure: we use a very compressed way to store the
180 various combinations of capability names. */
181 *sz = 1 << cnt;
182 result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
183 if (result == NULL)
184 _dl_signal_error (ENOMEM, NULL, NULL,
185 N_("cannot create capability list"));
187 if (cnt == 1)
189 result[0].str = (char *) (result + *sz);
190 result[0].len = temp[0].len + 1;
191 result[1].str = (char *) (result + *sz);
192 result[1].len = 0;
193 cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
194 *cp = '/';
195 *sz = 2;
196 *max_capstrlen = result[0].len;
198 return result;
201 /* Fill in the information. This follows the following scheme
202 (indeces from TEMP for four strings):
203 entry #0: 0, 1, 2, 3 binary: 1111
204 #1: 0, 1, 3 1101
205 #2: 0, 2, 3 1011
206 #3: 0, 3 1001
207 This allows the representation of all possible combinations of
208 capability names in the string. First generate the strings. */
209 result[1].str = result[0].str = cp = (char *) (result + *sz);
210 #define add(idx) \
211 cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
212 if (cnt == 2)
214 add (1);
215 add (0);
217 else
219 n = 1 << (cnt - 1);
222 n -= 2;
224 /* We always add the last string. */
225 add (cnt - 1);
227 /* Add the strings which have the bit set in N. */
228 for (m = cnt - 2; m > 0; --m)
229 if ((n & (1 << m)) != 0)
230 add (m);
232 /* Always add the first string. */
233 add (0);
235 while (n != 0);
237 #undef add
239 /* Now we are ready to install the string pointers and length. */
240 for (n = 0; n < (1UL << cnt); ++n)
241 result[n].len = 0;
242 n = cnt;
245 size_t mask = 1 << --n;
247 rp = result;
248 for (m = 1 << cnt; m > 0; ++rp)
249 if ((--m & mask) != 0)
250 rp->len += temp[n].len + 1;
252 while (n != 0);
254 /* The first half of the strings all include the first string. */
255 n = (1 << cnt) - 2;
256 rp = &result[2];
257 while (n != (1UL << (cnt - 1)))
259 if ((--n & 1) != 0)
260 rp[0].str = rp[-2].str + rp[-2].len;
261 else
262 rp[0].str = rp[-1].str;
263 ++rp;
266 /* The second half starts right after the first part of the string of
267 the corresponding entry in the first half. */
270 rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
271 ++rp;
273 while (--n != 0);
275 /* The maximum string length. */
276 *max_capstrlen = result[0].len;
278 return result;