Move BZ#11261 from 2.18 to 2.16 bug list.
[glibc.git] / elf / dl-hwcaps.c
blob8d49383d764e40a3714697452e435a6e8a567ecd
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 *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 const struct
71 ElfW(Word) vendorlen;
72 ElfW(Word) datalen;
73 ElfW(Word) type;
74 } *note = (const void *) start;
75 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
77 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
78 if (note->type == NT_GNU_HWCAP
79 && note->vendorlen == sizeof "GNU"
80 && !memcmp ((note + 1), "GNU", sizeof "GNU")
81 && note->datalen > 2 * sizeof (ElfW(Word)) + 2)
83 const ElfW(Word) *p = ((const void *) (note + 1)
84 + ROUND (sizeof "GNU"));
85 cnt += *p++;
86 ++p; /* Skip mask word. */
87 dsocaps = (const char *) p;
88 dsocapslen = note->datalen - sizeof *p * 2;
89 break;
91 note = ((const void *) (note + 1)
92 + ROUND (note->vendorlen) + ROUND (note->datalen));
93 #undef ROUND
95 if (dsocaps != NULL)
96 break;
99 #endif
101 /* For TLS enabled builds always add 'tls'. */
102 ++cnt;
104 /* Create temporary data structure to generate result table. */
105 struct r_strlenpair temp[cnt];
106 m = 0;
107 #ifdef NEED_DL_SYSINFO_DSO
108 if (dsocaps != NULL)
110 const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
111 GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
112 /* Note that we add the dsocaps to the set already chosen by the
113 LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
114 So there is no way to request ignoring an OS-supplied dsocap
115 string and bit like you can ignore an OS-supplied HWCAP bit. */
116 GLRO(dl_hwcap_mask) |= (uint64_t) mask << _DL_FIRST_EXTRA;
117 size_t len;
118 for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
120 uint_fast8_t bit = *p++;
121 len = strlen (p);
123 /* Skip entries that are not enabled in the mask word. */
124 if (__builtin_expect (mask & ((ElfW(Word)) 1 << bit), 1))
126 temp[m].str = p;
127 temp[m].len = len;
128 ++m;
130 else
131 --cnt;
134 #endif
135 for (n = 0; masked != 0; ++n)
136 if ((masked & (1ULL << n)) != 0)
138 temp[m].str = _dl_hwcap_string (n);
139 temp[m].len = strlen (temp[m].str);
140 masked ^= 1ULL << n;
141 ++m;
143 if (platform != NULL)
145 temp[m].str = platform;
146 temp[m].len = platform_len;
147 ++m;
150 temp[m].str = "tls";
151 temp[m].len = 3;
152 ++m;
154 assert (m == cnt);
156 /* Determine the total size of all strings together. */
157 if (cnt == 1)
158 total = temp[0].len + 1;
159 else
161 total = temp[0].len + temp[cnt - 1].len + 2;
162 if (cnt > 2)
164 total <<= 1;
165 for (n = 1; n + 1 < cnt; ++n)
166 total += temp[n].len + 1;
167 if (cnt > 3
168 && (cnt >= sizeof (size_t) * 8
169 || total + (sizeof (*result) << 3)
170 >= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
171 _dl_signal_error (ENOMEM, NULL, NULL,
172 N_("cannot create capability list"));
174 total <<= cnt - 3;
178 /* The result structure: we use a very compressed way to store the
179 various combinations of capability names. */
180 *sz = 1 << cnt;
181 result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
182 if (result == NULL)
183 _dl_signal_error (ENOMEM, NULL, NULL,
184 N_("cannot create capability list"));
186 if (cnt == 1)
188 result[0].str = (char *) (result + *sz);
189 result[0].len = temp[0].len + 1;
190 result[1].str = (char *) (result + *sz);
191 result[1].len = 0;
192 cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
193 *cp = '/';
194 *sz = 2;
195 *max_capstrlen = result[0].len;
197 return result;
200 /* Fill in the information. This follows the following scheme
201 (indices from TEMP for four strings):
202 entry #0: 0, 1, 2, 3 binary: 1111
203 #1: 0, 1, 3 1101
204 #2: 0, 2, 3 1011
205 #3: 0, 3 1001
206 This allows the representation of all possible combinations of
207 capability names in the string. First generate the strings. */
208 result[1].str = result[0].str = cp = (char *) (result + *sz);
209 #define add(idx) \
210 cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
211 if (cnt == 2)
213 add (1);
214 add (0);
216 else
218 n = 1 << (cnt - 1);
221 n -= 2;
223 /* We always add the last string. */
224 add (cnt - 1);
226 /* Add the strings which have the bit set in N. */
227 for (m = cnt - 2; m > 0; --m)
228 if ((n & (1 << m)) != 0)
229 add (m);
231 /* Always add the first string. */
232 add (0);
234 while (n != 0);
236 #undef add
238 /* Now we are ready to install the string pointers and length. */
239 for (n = 0; n < (1UL << cnt); ++n)
240 result[n].len = 0;
241 n = cnt;
244 size_t mask = 1 << --n;
246 rp = result;
247 for (m = 1 << cnt; m > 0; ++rp)
248 if ((--m & mask) != 0)
249 rp->len += temp[n].len + 1;
251 while (n != 0);
253 /* The first half of the strings all include the first string. */
254 n = (1 << cnt) - 2;
255 rp = &result[2];
256 while (n != (1UL << (cnt - 1)))
258 if ((--n & 1) != 0)
259 rp[0].str = rp[-2].str + rp[-2].len;
260 else
261 rp[0].str = rp[-1].str;
262 ++rp;
265 /* The second half starts right after the first part of the string of
266 the corresponding entry in the first half. */
269 rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
270 ++rp;
272 while (--n != 0);
274 /* The maximum string length. */
275 *max_capstrlen = result[0].len;
277 return result;