|
Packit |
6c4009 |
/* Hardware capability support for run-time dynamic loader.
|
|
Packit |
6c4009 |
Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <assert.h>
|
|
Packit |
6c4009 |
#include <elf.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <libintl.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <ldsodefs.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <dl-procinfo.h>
|
|
Packit |
6c4009 |
#include <dl-hwcaps.h>
|
|
Packit |
6c4009 |
|
|
Packit Service |
1f32a9 |
#ifdef _DL_FIRST_PLATFORM
|
|
Packit Service |
1f32a9 |
# define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT)
|
|
Packit Service |
1f32a9 |
#else
|
|
Packit Service |
1f32a9 |
# define _DL_FIRST_EXTRA _DL_HWCAP_COUNT
|
|
Packit Service |
1f32a9 |
#endif
|
|
Packit Service |
77b91b |
|
|
Packit |
6c4009 |
/* Return an array of useful/necessary hardware capability names. */
|
|
Packit |
6c4009 |
const struct r_strlenpair *
|
|
Packit Service |
1f32a9 |
_dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
|
|
Packit Service |
1f32a9 |
size_t *max_capstrlen)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint64_t hwcap_mask = GET_HWCAP_MASK();
|
|
Packit |
6c4009 |
/* Determine how many important bits are set. */
|
|
Packit |
6c4009 |
uint64_t masked = GLRO(dl_hwcap) & hwcap_mask;
|
|
Packit Service |
1f32a9 |
size_t cnt = platform != NULL;
|
|
Packit |
6c4009 |
size_t n, m;
|
|
Packit Service |
1f32a9 |
size_t total;
|
|
Packit |
6c4009 |
struct r_strlenpair *result;
|
|
Packit |
6c4009 |
struct r_strlenpair *rp;
|
|
Packit |
6c4009 |
char *cp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Count the number of bits set in the masked value. */
|
|
Packit |
6c4009 |
for (n = 0; (~((1ULL << n) - 1) & masked) != 0; ++n)
|
|
Packit |
6c4009 |
if ((masked & (1ULL << n)) != 0)
|
|
Packit |
6c4009 |
++cnt;
|
|
Packit |
6c4009 |
|
|
Packit Service |
1f32a9 |
#ifdef NEED_DL_SYSINFO_DSO
|
|
Packit Service |
1f32a9 |
/* The system-supplied DSO can contain a note of type 2, vendor "GNU".
|
|
Packit Service |
1f32a9 |
This gives us a list of names to treat as fake hwcap bits. */
|
|
Packit Service |
1f32a9 |
|
|
Packit Service |
1f32a9 |
const char *dsocaps = NULL;
|
|
Packit Service |
1f32a9 |
size_t dsocapslen = 0;
|
|
Packit Service |
1f32a9 |
if (GLRO(dl_sysinfo_map) != NULL)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
|
|
Packit Service |
1f32a9 |
const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
|
|
Packit Service |
1f32a9 |
for (uint_fast16_t i = 0; i < phnum; ++i)
|
|
Packit Service |
1f32a9 |
if (phdr[i].p_type == PT_NOTE)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
const ElfW(Addr) start = (phdr[i].p_vaddr
|
|
Packit Service |
1f32a9 |
+ GLRO(dl_sysinfo_map)->l_addr);
|
|
Packit Service |
1f32a9 |
/* NB: Some PT_NOTE segment may have alignment value of 0
|
|
Packit Service |
1f32a9 |
or 1. gABI specifies that PT_NOTE segments should be
|
|
Packit Service |
1f32a9 |
aligned to 4 bytes in 32-bit objects and to 8 bytes in
|
|
Packit Service |
1f32a9 |
64-bit objects. As a Linux extension, we also support
|
|
Packit Service |
1f32a9 |
4 byte alignment in 64-bit objects. If p_align is less
|
|
Packit Service |
1f32a9 |
than 4, we treate alignment as 4 bytes since some note
|
|
Packit Service |
1f32a9 |
segments have 0 or 1 byte alignment. */
|
|
Packit Service |
1f32a9 |
ElfW(Addr) align = phdr[i].p_align;
|
|
Packit Service |
1f32a9 |
if (align < 4)
|
|
Packit Service |
1f32a9 |
align = 4;
|
|
Packit Service |
1f32a9 |
else if (align != 4 && align != 8)
|
|
Packit Service |
1f32a9 |
continue;
|
|
Packit Service |
1f32a9 |
/* The standard ELF note layout is exactly as the anonymous struct.
|
|
Packit Service |
1f32a9 |
The next element is a variable length vendor name of length
|
|
Packit Service |
1f32a9 |
VENDORLEN (with a real length rounded to ElfW(Word)), followed
|
|
Packit Service |
1f32a9 |
by the data of length DATALEN (with a real length rounded to
|
|
Packit Service |
1f32a9 |
ElfW(Word)). */
|
|
Packit Service |
1f32a9 |
const struct
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
ElfW(Word) vendorlen;
|
|
Packit Service |
1f32a9 |
ElfW(Word) datalen;
|
|
Packit Service |
1f32a9 |
ElfW(Word) type;
|
|
Packit Service |
1f32a9 |
} *note = (const void *) start;
|
|
Packit Service |
1f32a9 |
while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
/* The layout of the type 2, vendor "GNU" note is as follows:
|
|
Packit Service |
1f32a9 |
.long <Number of capabilities enabled by this note>
|
|
Packit Service |
1f32a9 |
.long <Capabilities mask> (as mask >> _DL_FIRST_EXTRA).
|
|
Packit Service |
1f32a9 |
.byte <The bit number for the next capability>
|
|
Packit Service |
1f32a9 |
.asciz <The name of the capability>. */
|
|
Packit Service |
1f32a9 |
if (note->type == NT_GNU_HWCAP
|
|
Packit Service |
1f32a9 |
&& note->vendorlen == sizeof "GNU"
|
|
Packit Service |
1f32a9 |
&& !memcmp ((note + 1), "GNU", sizeof "GNU")
|
|
Packit Service |
1f32a9 |
&& note->datalen > 2 * sizeof (ElfW(Word)) + 2)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
const ElfW(Word) *p
|
|
Packit Service |
1f32a9 |
= ((const void *) note
|
|
Packit Service |
1f32a9 |
+ ELF_NOTE_DESC_OFFSET (sizeof "GNU", align));
|
|
Packit Service |
1f32a9 |
cnt += *p++;
|
|
Packit Service |
1f32a9 |
++p; /* Skip mask word. */
|
|
Packit Service |
1f32a9 |
dsocaps = (const char *) p; /* Pseudo-string "name" */
|
|
Packit Service |
1f32a9 |
dsocapslen = note->datalen - sizeof *p * 2;
|
|
Packit Service |
1f32a9 |
break;
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
note = ((const void *) note
|
|
Packit Service |
1f32a9 |
+ ELF_NOTE_NEXT_OFFSET (note->vendorlen,
|
|
Packit Service |
1f32a9 |
note->datalen, align));
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
if (dsocaps != NULL)
|
|
Packit Service |
1f32a9 |
break;
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
#endif
|
|
Packit Service |
1f32a9 |
|
|
Packit |
6c4009 |
/* For TLS enabled builds always add 'tls'. */
|
|
Packit |
6c4009 |
++cnt;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Create temporary data structure to generate result table. */
|
|
Packit |
6c4009 |
struct r_strlenpair temp[cnt];
|
|
Packit |
6c4009 |
m = 0;
|
|
Packit Service |
1f32a9 |
#ifdef NEED_DL_SYSINFO_DSO
|
|
Packit Service |
1f32a9 |
if (dsocaps != NULL)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
/* dsocaps points to the .asciz string, and -1 points to the mask
|
|
Packit Service |
1f32a9 |
.long just before the string. */
|
|
Packit Service |
1f32a9 |
const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
|
|
Packit Service |
1f32a9 |
GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
|
|
Packit Service |
1f32a9 |
/* Note that we add the dsocaps to the set already chosen by the
|
|
Packit Service |
1f32a9 |
LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
|
|
Packit Service |
1f32a9 |
So there is no way to request ignoring an OS-supplied dsocap
|
|
Packit Service |
1f32a9 |
string and bit like you can ignore an OS-supplied HWCAP bit. */
|
|
Packit Service |
1f32a9 |
hwcap_mask |= (uint64_t) mask << _DL_FIRST_EXTRA;
|
|
Packit Service |
1f32a9 |
#if HAVE_TUNABLES
|
|
Packit Service |
1f32a9 |
TUNABLE_SET (glibc, tune, hwcap_mask, uint64_t, hwcap_mask);
|
|
Packit Service |
1f32a9 |
#else
|
|
Packit Service |
1f32a9 |
GLRO(dl_hwcap_mask) = hwcap_mask;
|
|
Packit Service |
1f32a9 |
#endif
|
|
Packit Service |
1f32a9 |
size_t len;
|
|
Packit Service |
1f32a9 |
for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
uint_fast8_t bit = *p++;
|
|
Packit Service |
1f32a9 |
len = strlen (p);
|
|
Packit Service |
1f32a9 |
|
|
Packit Service |
1f32a9 |
/* Skip entries that are not enabled in the mask word. */
|
|
Packit Service |
1f32a9 |
if (__glibc_likely (mask & ((ElfW(Word)) 1 << bit)))
|
|
Packit Service |
1f32a9 |
{
|
|
Packit Service |
1f32a9 |
temp[m].str = p;
|
|
Packit Service |
1f32a9 |
temp[m].len = len;
|
|
Packit Service |
1f32a9 |
++m;
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
else
|
|
Packit Service |
1f32a9 |
--cnt;
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
}
|
|
Packit Service |
1f32a9 |
#endif
|
|
Packit |
6c4009 |
for (n = 0; masked != 0; ++n)
|
|
Packit |
6c4009 |
if ((masked & (1ULL << n)) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
temp[m].str = _dl_hwcap_string (n);
|
|
Packit |
6c4009 |
temp[m].len = strlen (temp[m].str);
|
|
Packit |
6c4009 |
masked ^= 1ULL << n;
|
|
Packit |
6c4009 |
++m;
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
1f32a9 |
if (platform != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
1f32a9 |
temp[m].str = platform;
|
|
Packit Service |
1f32a9 |
temp[m].len = platform_len;
|
|
Packit |
6c4009 |
++m;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
temp[m].str = "tls";
|
|
Packit |
6c4009 |
temp[m].len = 3;
|
|
Packit |
6c4009 |
++m;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
assert (m == cnt);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Determine the total size of all strings together. */
|
|
Packit |
6c4009 |
if (cnt == 1)
|
|
Packit Service |
1f32a9 |
total = temp[0].len + 1;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
1f32a9 |
total = temp[0].len + temp[cnt - 1].len + 2;
|
|
Packit |
6c4009 |
if (cnt > 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
total <<= 1;
|
|
Packit |
6c4009 |
for (n = 1; n + 1 < cnt; ++n)
|
|
Packit |
6c4009 |
total += temp[n].len + 1;
|
|
Packit |
6c4009 |
if (cnt > 3
|
|
Packit |
6c4009 |
&& (cnt >= sizeof (size_t) * 8
|
|
Packit |
6c4009 |
|| total + (sizeof (*result) << 3)
|
|
Packit |
6c4009 |
>= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
|
|
Packit |
6c4009 |
_dl_signal_error (ENOMEM, NULL, NULL,
|
|
Packit |
6c4009 |
N_("cannot create capability list"));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
total <<= cnt - 3;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit Service |
1f32a9 |
/* The result structure: we use a very compressed way to store the
|
|
Packit Service |
1f32a9 |
various combinations of capability names. */
|
|
Packit Service |
1f32a9 |
*sz = 1 << cnt;
|
|
Packit Service |
1f32a9 |
result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
|
|
Packit Service |
1f32a9 |
if (result == NULL)
|
|
Packit |
6c4009 |
_dl_signal_error (ENOMEM, NULL, NULL,
|
|
Packit |
6c4009 |
N_("cannot create capability list"));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (cnt == 1)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
1f32a9 |
result[0].str = (char *) (result + *sz);
|
|
Packit |
6c4009 |
result[0].len = temp[0].len + 1;
|
|
Packit Service |
1f32a9 |
result[1].str = (char *) (result + *sz);
|
|
Packit |
6c4009 |
result[1].len = 0;
|
|
Packit Service |
1f32a9 |
cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
|
|
Packit |
6c4009 |
*cp = '/';
|
|
Packit Service |
1f32a9 |
*sz = 2;
|
|
Packit Service |
1f32a9 |
*max_capstrlen = result[0].len;
|
|
Packit |
6c4009 |
|
|
Packit Service |
1f32a9 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Fill in the information. This follows the following scheme
|
|
Packit |
6c4009 |
(indices from TEMP for four strings):
|
|
Packit |
6c4009 |
entry #0: 0, 1, 2, 3 binary: 1111
|
|
Packit |
6c4009 |
#1: 0, 1, 3 1101
|
|
Packit |
6c4009 |
#2: 0, 2, 3 1011
|
|
Packit |
6c4009 |
#3: 0, 3 1001
|
|
Packit |
6c4009 |
This allows the representation of all possible combinations of
|
|
Packit |
6c4009 |
capability names in the string. First generate the strings. */
|
|
Packit Service |
1f32a9 |
result[1].str = result[0].str = cp = (char *) (result + *sz);
|
|
Packit |
6c4009 |
#define add(idx) \
|
|
Packit |
6c4009 |
cp = __mempcpy (__mempcpy (cp, temp[idx].str, temp[idx].len), "/", 1);
|
|
Packit |
6c4009 |
if (cnt == 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
add (1);
|
|
Packit |
6c4009 |
add (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
n = 1 << (cnt - 1);
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
n -= 2;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We always add the last string. */
|
|
Packit |
6c4009 |
add (cnt - 1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Add the strings which have the bit set in N. */
|
|
Packit |
6c4009 |
for (m = cnt - 2; m > 0; --m)
|
|
Packit |
6c4009 |
if ((n & (1 << m)) != 0)
|
|
Packit |
6c4009 |
add (m);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Always add the first string. */
|
|
Packit |
6c4009 |
add (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
while (n != 0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#undef add
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now we are ready to install the string pointers and length. */
|
|
Packit |
6c4009 |
for (n = 0; n < (1UL << cnt); ++n)
|
|
Packit |
6c4009 |
result[n].len = 0;
|
|
Packit |
6c4009 |
n = cnt;
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t mask = 1 << --n;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rp = result;
|
|
Packit |
6c4009 |
for (m = 1 << cnt; m > 0; ++rp)
|
|
Packit |
6c4009 |
if ((--m & mask) != 0)
|
|
Packit |
6c4009 |
rp->len += temp[n].len + 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
while (n != 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The first half of the strings all include the first string. */
|
|
Packit |
6c4009 |
n = (1 << cnt) - 2;
|
|
Packit |
6c4009 |
rp = &result[2];
|
|
Packit |
6c4009 |
while (n != (1UL << (cnt - 1)))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if ((--n & 1) != 0)
|
|
Packit |
6c4009 |
rp[0].str = rp[-2].str + rp[-2].len;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
rp[0].str = rp[-1].str;
|
|
Packit |
6c4009 |
++rp;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The second half starts right after the first part of the string of
|
|
Packit |
6c4009 |
the corresponding entry in the first half. */
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rp[0].str = rp[-(1 << (cnt - 1))].str + temp[cnt - 1].len + 1;
|
|
Packit |
6c4009 |
++rp;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
while (--n != 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The maximum string length. */
|
|
Packit Service |
1f32a9 |
*max_capstrlen = result[0].len;
|
|
Packit |
6c4009 |
|
|
Packit Service |
1f32a9 |
return result;
|
|
Packit |
6c4009 |
}
|