Blame elf/dl-hwcaps.c

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 6c4009
#ifdef _DL_FIRST_PLATFORM
Packit 6c4009
# define _DL_FIRST_EXTRA (_DL_FIRST_PLATFORM + _DL_PLATFORMS_COUNT)
Packit 6c4009
#else
Packit 6c4009
# define _DL_FIRST_EXTRA _DL_HWCAP_COUNT
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return an array of useful/necessary hardware capability names.  */
Packit 6c4009
const struct r_strlenpair *
Packit 6c4009
_dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
Packit 6c4009
		      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 6c4009
  size_t cnt = platform != NULL;
Packit 6c4009
  size_t n, m;
Packit 6c4009
  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 6c4009
#ifdef NEED_DL_SYSINFO_DSO
Packit 6c4009
  /* The system-supplied DSO can contain a note of type 2, vendor "GNU".
Packit 6c4009
     This gives us a list of names to treat as fake hwcap bits.  */
Packit 6c4009
Packit 6c4009
  const char *dsocaps = NULL;
Packit 6c4009
  size_t dsocapslen = 0;
Packit 6c4009
  if (GLRO(dl_sysinfo_map) != NULL)
Packit 6c4009
    {
Packit 6c4009
      const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
Packit 6c4009
      const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
Packit 6c4009
      for (uint_fast16_t i = 0; i < phnum; ++i)
Packit 6c4009
	if (phdr[i].p_type == PT_NOTE)
Packit 6c4009
	  {
Packit 6c4009
	    const ElfW(Addr) start = (phdr[i].p_vaddr
Packit 6c4009
				      + GLRO(dl_sysinfo_map)->l_addr);
Packit 6c4009
	    /* NB: Some PT_NOTE segment may have alignment value of 0
Packit 6c4009
	       or 1.  gABI specifies that PT_NOTE segments should be
Packit 6c4009
	       aligned to 4 bytes in 32-bit objects and to 8 bytes in
Packit 6c4009
	       64-bit objects.  As a Linux extension, we also support
Packit 6c4009
	       4 byte alignment in 64-bit objects.  If p_align is less
Packit 6c4009
	       than 4, we treate alignment as 4 bytes since some note
Packit 6c4009
	       segments have 0 or 1 byte alignment.   */
Packit 6c4009
	    ElfW(Addr) align = phdr[i].p_align;
Packit 6c4009
	    if (align < 4)
Packit 6c4009
	      align = 4;
Packit 6c4009
	    else if (align != 4 && align != 8)
Packit 6c4009
	      continue;
Packit 6c4009
	    /* The standard ELF note layout is exactly as the anonymous struct.
Packit 6c4009
	       The next element is a variable length vendor name of length
Packit 6c4009
	       VENDORLEN (with a real length rounded to ElfW(Word)), followed
Packit 6c4009
	       by the data of length DATALEN (with a real length rounded to
Packit 6c4009
	       ElfW(Word)).  */
Packit 6c4009
	    const struct
Packit 6c4009
	    {
Packit 6c4009
	      ElfW(Word) vendorlen;
Packit 6c4009
	      ElfW(Word) datalen;
Packit 6c4009
	      ElfW(Word) type;
Packit 6c4009
	    } *note = (const void *) start;
Packit 6c4009
	    while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
Packit 6c4009
	      {
Packit 6c4009
		/* The layout of the type 2, vendor "GNU" note is as follows:
Packit 6c4009
		   .long <Number of capabilities enabled by this note>
Packit 6c4009
		   .long <Capabilities mask> (as mask >> _DL_FIRST_EXTRA).
Packit 6c4009
		   .byte <The bit number for the next capability>
Packit 6c4009
		   .asciz <The name of the capability>.  */
Packit 6c4009
		if (note->type == NT_GNU_HWCAP
Packit 6c4009
		    && note->vendorlen == sizeof "GNU"
Packit 6c4009
		    && !memcmp ((note + 1), "GNU", sizeof "GNU")
Packit 6c4009
		    && note->datalen > 2 * sizeof (ElfW(Word)) + 2)
Packit 6c4009
		  {
Packit 6c4009
		    const ElfW(Word) *p
Packit 6c4009
		      = ((const void *) note
Packit 6c4009
			 + ELF_NOTE_DESC_OFFSET (sizeof "GNU", align));
Packit 6c4009
		    cnt += *p++;
Packit 6c4009
		    ++p;	/* Skip mask word.  */
Packit 6c4009
		    dsocaps = (const char *) p; /* Pseudo-string "name"  */
Packit 6c4009
		    dsocapslen = note->datalen - sizeof *p * 2;
Packit 6c4009
		    break;
Packit 6c4009
		  }
Packit 6c4009
		note = ((const void *) note
Packit 6c4009
			+ ELF_NOTE_NEXT_OFFSET (note->vendorlen,
Packit 6c4009
						note->datalen, align));
Packit 6c4009
	      }
Packit 6c4009
	    if (dsocaps != NULL)
Packit 6c4009
	      break;
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
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 6c4009
#ifdef NEED_DL_SYSINFO_DSO
Packit 6c4009
  if (dsocaps != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* dsocaps points to the .asciz string, and -1 points to the mask
Packit 6c4009
         .long just before the string.  */
Packit 6c4009
      const ElfW(Word) mask = ((const ElfW(Word) *) dsocaps)[-1];
Packit 6c4009
      GLRO(dl_hwcap) |= (uint64_t) mask << _DL_FIRST_EXTRA;
Packit 6c4009
      /* Note that we add the dsocaps to the set already chosen by the
Packit 6c4009
	 LD_HWCAP_MASK environment variable (or default HWCAP_IMPORTANT).
Packit 6c4009
	 So there is no way to request ignoring an OS-supplied dsocap
Packit 6c4009
	 string and bit like you can ignore an OS-supplied HWCAP bit.  */
Packit 6c4009
      hwcap_mask |= (uint64_t) mask << _DL_FIRST_EXTRA;
Packit 6c4009
#if HAVE_TUNABLES
Packit 6c4009
      TUNABLE_SET (glibc, tune, hwcap_mask, uint64_t, hwcap_mask);
Packit 6c4009
#else
Packit 6c4009
      GLRO(dl_hwcap_mask) = hwcap_mask;
Packit 6c4009
#endif
Packit 6c4009
      size_t len;
Packit 6c4009
      for (const char *p = dsocaps; p < dsocaps + dsocapslen; p += len + 1)
Packit 6c4009
	{
Packit 6c4009
	  uint_fast8_t bit = *p++;
Packit 6c4009
	  len = strlen (p);
Packit 6c4009
Packit 6c4009
	  /* Skip entries that are not enabled in the mask word.  */
Packit 6c4009
	  if (__glibc_likely (mask & ((ElfW(Word)) 1 << bit)))
Packit 6c4009
	    {
Packit 6c4009
	      temp[m].str = p;
Packit 6c4009
	      temp[m].len = len;
Packit 6c4009
	      ++m;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    --cnt;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
#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 6c4009
  if (platform != NULL)
Packit 6c4009
    {
Packit 6c4009
      temp[m].str = platform;
Packit 6c4009
      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 6c4009
    total = temp[0].len + 1;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      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 6c4009
  /* The result structure: we use a very compressed way to store the
Packit 6c4009
     various combinations of capability names.  */
Packit 6c4009
  *sz = 1 << cnt;
Packit 6c4009
  result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
Packit 6c4009
  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 6c4009
      result[0].str = (char *) (result + *sz);
Packit 6c4009
      result[0].len = temp[0].len + 1;
Packit 6c4009
      result[1].str = (char *) (result + *sz);
Packit 6c4009
      result[1].len = 0;
Packit 6c4009
      cp = __mempcpy ((char *) (result + *sz), temp[0].str, temp[0].len);
Packit 6c4009
      *cp = '/';
Packit 6c4009
      *sz = 2;
Packit 6c4009
      *max_capstrlen = result[0].len;
Packit 6c4009
Packit 6c4009
      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 6c4009
  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 6c4009
  *max_capstrlen = result[0].len;
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}