Fix GCC 11 -Warray-parameter warning for __sigsetjmp (bug 26647)
[glibc.git] / elf / dl-hwcaps.c
blob6df9efb2558b759b0bcfb81f80bf217f77e75b7a
1 /* Hardware capability support for run-time dynamic loader.
2 Copyright (C) 2012-2020 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 <https://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>
27 #include <dl-hwcaps.h>
29 /* Return an array of useful/necessary hardware capability names. */
30 const struct r_strlenpair *
31 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
32 size_t *max_capstrlen)
34 uint64_t hwcap_mask = GET_HWCAP_MASK();
35 /* Determine how many important bits are set. */
36 uint64_t masked = GLRO(dl_hwcap) & hwcap_mask;
37 size_t cnt = platform != NULL;
38 size_t n, m;
39 size_t total;
40 struct r_strlenpair *result;
41 struct r_strlenpair *rp;
42 char *cp;
44 /* Count the number of bits set in the masked value. */
45 for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n)
46 if ((masked & (1ULL << n)) != 0)
47 ++cnt;
49 /* For TLS enabled builds always add 'tls'. */
50 ++cnt;
52 /* Create temporary data structure to generate result table. */
53 struct r_strlenpair temp[cnt];
54 m = 0;
55 for (n = 0; masked != 0; ++n)
56 if ((masked & (1ULL << n)) != 0)
58 temp[m].str = _dl_hwcap_string (n);
59 temp[m].len = strlen (temp[m].str);
60 masked ^= 1ULL << n;
61 ++m;
63 if (platform != NULL)
65 temp[m].str = platform;
66 temp[m].len = platform_len;
67 ++m;
70 temp[m].str = "tls";
71 temp[m].len = 3;
72 ++m;
74 assert (m == cnt);
76 /* Determine the total size of all strings together. */
77 if (cnt == 1)
78 total = temp[0].len + 1;
79 else
81 total = temp[0].len + temp[cnt - 1].len + 2;
82 if (cnt > 2)
84 total <<= 1;
85 for (n = 1; n + 1 < cnt; ++n)
86 total += temp[n].len + 1;
87 if (cnt > 3
88 && (cnt >= sizeof (size_t) * 8
89 || total + (sizeof (*result) << 3)
90 >= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
91 _dl_signal_error (ENOMEM, NULL, NULL,
92 N_("cannot create capability list"));
94 total <<= cnt - 3;
98 /* The result structure: we use a very compressed way to store the
99 various combinations of capability names. */
100 *sz = 1 << cnt;
101 result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
102 if (result == NULL)
103 _dl_signal_error (ENOMEM, NULL, NULL,
104 N_("cannot create capability list"));
106 if (cnt == 1)
108 result[0].str = (char *) (result + *sz);
109 result[0].len = temp[0].len + 1;
110 result[1].str = (char *) (result + *sz);
111 result[1].len = 0;
112 cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
113 *cp = '/';
114 *sz = 2;
115 *max_capstrlen = result[0].len;
117 return result;
120 /* Fill in the information. This follows the following scheme
121 (indices from TEMP for four strings):
122 entry #0: 0, 1, 2, 3 binary: 1111
123 #1: 0, 1, 3 1101
124 #2: 0, 2, 3 1011
125 #3: 0, 3 1001
126 This allows the representation of all possible combinations of
127 capability names in the string. First generate the strings. */
128 result[1].str = result[0].str = cp = (char *) (result + *sz);
129 #define add(idx) \
130 cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
131 if (cnt == 2)
133 add (1);
134 add (0);
136 else
138 n = 1 << (cnt - 1);
141 n -= 2;
143 /* We always add the last string. */
144 add (cnt - 1);
146 /* Add the strings which have the bit set in N. */
147 for (m = cnt - 2; m > 0; --m)
148 if ((n & (1 << m)) != 0)
149 add (m);
151 /* Always add the first string. */
152 add (0);
154 while (n != 0);
156 #undef add
158 /* Now we are ready to install the string pointers and length. */
159 for (n = 0; n < (1UL << cnt); ++n)
160 result[n].len = 0;
161 n = cnt;
164 size_t mask = 1 << --n;
166 rp = result;
167 for (m = 1 << cnt; m > 0; ++rp)
168 if ((--m & mask) != 0)
169 rp->len += temp[n].len + 1;
171 while (n != 0);
173 /* The first half of the strings all include the first string. */
174 n = (1 << cnt) - 2;
175 rp = &result[2];
176 while (n != (1UL << (cnt - 1)))
178 if ((--n & 1) != 0)
179 rp[0].str = rp[-2].str + rp[-2].len;
180 else
181 rp[0].str = rp[-1].str;
182 ++rp;
185 /* The second half starts right after the first part of the string of
186 the corresponding entry in the first half. */
189 rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
190 ++rp;
192 while (--n != 0);
194 /* The maximum string length. */
195 *max_capstrlen = result[0].len;
197 return result;