349
/* Return an array of useful/necessary hardware capability names. */
350
const struct r_strlenpair *
352
_dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
353
size_t *max_capstrlen)
355
/* Determine how many important bits are set. */
356
uint64_t masked = GLRO(dl_hwcap) & GLRO(dl_hwcap_mask);
357
size_t cnt = platform != NULL;
360
struct r_strlenpair *temp;
361
struct r_strlenpair *result;
362
struct r_strlenpair *rp;
365
/* Count the number of bits set in the masked value. */
366
for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n)
367
if ((masked & (1ULL << n)) != 0)
370
#if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
371
/* The system-supplied DSO can contain a note of type 2, vendor "GNU".
372
This gives us a list of names to treat as fake hwcap bits. */
374
const char *dsocaps = NULL;
375
size_t dsocapslen = 0;
376
if (GLRO(dl_sysinfo_map) != NULL)
378
const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
379
const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
380
for (uint_fast16_t i = 0; i < phnum; ++i)
381
if (phdr[i].p_type == PT_NOTE)
383
const ElfW(Addr) start = (phdr[i].p_vaddr
384
+ GLRO(dl_sysinfo_map)->l_addr);
387
ElfW(Word) vendorlen;
390
} *note = (const void *) start;
391
while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
393
#define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
394
if (note->type == NT_GNU_HWCAP
395
&& note->vendorlen == sizeof "GNU"
396
&& !memcmp ((note + 1), "GNU", sizeof "GNU")
397
&& note->datalen > 2 * sizeof (ElfW(Word)) + 2)
399
const ElfW(Word) *p = ((const void *) (note + 1)
400
+ ROUND (sizeof "GNU"));
402
++p; /* Skip mask word. */
403
dsocaps = (const char *) p;
404
dsocapslen = note->datalen - sizeof *p * 2;
407
note = ((const void *) (note + 1)
408
+ ROUND (note->vendorlen) + ROUND (note->datalen));
417
/* For TLS enabled builds always add 'tls'. */
420
if (__access ("/etc/ld.so.nohwcap", F_OK) == 0)
422
/* If hwcap is disabled, we only have the base directory to search. */
423
result = (struct r_strlenpair *) malloc (sizeof (*result));
427
result[0].str = (char *) result; /* Does not really matter. */
434
/* Create temporary data structure to generate result table. */
435
temp = (struct r_strlenpair *) alloca (cnt * sizeof (*temp));
437
#if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
440
const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
441
GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
442
/* Note that we add the dsocaps to the set already chosen by the
443
LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
444
So there is no way to request ignoring an OS-supplied dsocap
445
string and bit like you can ignore an OS-supplied HWCAP bit. */
446
GLRO(dl_hwcap_mask) |= (uint64_t) mask << _DL_FIRST_EXTRA;
448
for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
450
uint_fast8_t bit = *p++;
453
/* Skip entries that are not enabled in the mask word. */
454
if (__builtin_expect (mask & ((ElfW(Word)) 1 << bit), 1))
465
for (n = 0; masked != 0; ++n)
466
if ((masked & (1ULL << n)) != 0)
468
temp[m].str = _dl_hwcap_string (n);
469
temp[m].len = strlen (temp[m].str);
473
if (platform != NULL)
475
temp[m].str = platform;
476
temp[m].len = platform_len;
486
/* Determine the total size of all strings together. */
488
total = temp[0].len + 1;
491
total = temp[0].len + temp[cnt - 1].len + 2;
495
for (n = 1; n + 1 < cnt; ++n)
496
total += temp[n].len + 1;
498
&& (cnt >= sizeof (size_t) * 8
499
|| total + (sizeof (*result) << 3)
500
>= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
501
_dl_signal_error (ENOMEM, NULL, NULL,
502
N_("cannot create capability list"));
508
/* The result structure: we use a very compressed way to store the
509
various combinations of capability names. */
511
result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
515
_dl_signal_error (ENOMEM, NULL, NULL,
516
N_("cannot create capability list"));
521
result[0].str = (char *) (result + *sz);
522
result[0].len = temp[0].len + 1;
523
result[1].str = (char *) (result + *sz);
525
cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
528
*max_capstrlen = result[0].len;
533
/* Fill in the information. This follows the following scheme
534
(indeces from TEMP for four strings):
535
entry #0: 0, 1, 2, 3 binary: 1111
539
This allows the representation of all possible combinations of
540
capability names in the string. First generate the strings. */
541
result[1].str = result[0].str = cp = (char *) (result + *sz);
543
cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
556
/* We always add the last string. */
559
/* Add the strings which have the bit set in N. */
560
for (m = cnt - 2; m > 0; --m)
561
if ((n & (1 << m)) != 0)
564
/* Always add the first string. */
571
/* Now we are ready to install the string pointers and length. */
572
for (n = 0; n < (1UL << cnt); ++n)
577
size_t mask = 1 << --n;
580
for (m = 1 << cnt; m > 0; ++rp)
581
if ((--m & mask) != 0)
582
rp->len += temp[n].len + 1;
586
/* The first half of the strings all include the first string. */
589
while (n != (1UL << (cnt - 1)))
592
rp[0].str = rp[-2].str + rp[-2].len;
594
rp[0].str = rp[-1].str;
598
/* The second half starts right after the first part of the string of
599
the corresponding entry in the first half. */
602
rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
607
/* The maximum string length. */
608
*max_capstrlen = result[0].len;