| 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. | 
|---|
| 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 | <https://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 | /* 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) | 
|---|
| 33 | { | 
|---|
| 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; | 
|---|
| 43 |  | 
|---|
| 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; | 
|---|
| 48 |  | 
|---|
| 49 | /* For TLS enabled builds always add 'tls'.  */ | 
|---|
| 50 | ++cnt; | 
|---|
| 51 |  | 
|---|
| 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) | 
|---|
| 57 | { | 
|---|
| 58 | temp[m].str = _dl_hwcap_string (n); | 
|---|
| 59 | temp[m].len = strlen (temp[m].str); | 
|---|
| 60 | masked ^= 1ULL << n; | 
|---|
| 61 | ++m; | 
|---|
| 62 | } | 
|---|
| 63 | if (platform != NULL) | 
|---|
| 64 | { | 
|---|
| 65 | temp[m].str = platform; | 
|---|
| 66 | temp[m].len = platform_len; | 
|---|
| 67 | ++m; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | temp[m].str = "tls"; | 
|---|
| 71 | temp[m].len = 3; | 
|---|
| 72 | ++m; | 
|---|
| 73 |  | 
|---|
| 74 | assert (m == cnt); | 
|---|
| 75 |  | 
|---|
| 76 | /* Determine the total size of all strings together.  */ | 
|---|
| 77 | if (cnt == 1) | 
|---|
| 78 | total = temp[0].len + 1; | 
|---|
| 79 | else | 
|---|
| 80 | { | 
|---|
| 81 | total = temp[0].len + temp[cnt - 1].len + 2; | 
|---|
| 82 | if (cnt > 2) | 
|---|
| 83 | { | 
|---|
| 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")); | 
|---|
| 93 |  | 
|---|
| 94 | total <<= cnt - 3; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 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")); | 
|---|
| 105 |  | 
|---|
| 106 | if (cnt == 1) | 
|---|
| 107 | { | 
|---|
| 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; | 
|---|
| 116 |  | 
|---|
| 117 | return result; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 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) | 
|---|
| 132 | { | 
|---|
| 133 | add (1); | 
|---|
| 134 | add (0); | 
|---|
| 135 | } | 
|---|
| 136 | else | 
|---|
| 137 | { | 
|---|
| 138 | n = 1 << (cnt - 1); | 
|---|
| 139 | do | 
|---|
| 140 | { | 
|---|
| 141 | n -= 2; | 
|---|
| 142 |  | 
|---|
| 143 | /* We always add the last string.  */ | 
|---|
| 144 | add (cnt - 1); | 
|---|
| 145 |  | 
|---|
| 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); | 
|---|
| 150 |  | 
|---|
| 151 | /* Always add the first string.  */ | 
|---|
| 152 | add (0); | 
|---|
| 153 | } | 
|---|
| 154 | while (n != 0); | 
|---|
| 155 | } | 
|---|
| 156 | #undef add | 
|---|
| 157 |  | 
|---|
| 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; | 
|---|
| 162 | do | 
|---|
| 163 | { | 
|---|
| 164 | size_t mask = 1 << --n; | 
|---|
| 165 |  | 
|---|
| 166 | rp = result; | 
|---|
| 167 | for (m = 1 << cnt; m > 0; ++rp) | 
|---|
| 168 | if ((--m & mask) != 0) | 
|---|
| 169 | rp->len += temp[n].len + 1; | 
|---|
| 170 | } | 
|---|
| 171 | while (n != 0); | 
|---|
| 172 |  | 
|---|
| 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))) | 
|---|
| 177 | { | 
|---|
| 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; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | /* The second half starts right after the first part of the string of | 
|---|
| 186 | the corresponding entry in the first half.  */ | 
|---|
| 187 | do | 
|---|
| 188 | { | 
|---|
| 189 | rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1; | 
|---|
| 190 | ++rp; | 
|---|
| 191 | } | 
|---|
| 192 | while (--n != 0); | 
|---|
| 193 |  | 
|---|
| 194 | /* The maximum string length.  */ | 
|---|
| 195 | *max_capstrlen = result[0].len; | 
|---|
| 196 |  | 
|---|
| 197 | return result; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|