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 Service a0468f
/* This is the result of counting the substrings in a colon-separated
Packit Service a0468f
   hwcaps string.  */
Packit Service a0468f
struct hwcaps_counts
Packit Service a0468f
{
Packit Service a0468f
  /* Number of substrings.  */
Packit Service a0468f
  size_t count;
Packit Service a0468f
Packit Service a0468f
  /* Sum of the individual substring lengths (without separators or
Packit Service a0468f
     null terminators).  */
Packit Service a0468f
  size_t total_length;
Packit Service a0468f
Packit Service a0468f
  /* Maximum length of an individual substring.  */
Packit Service a0468f
  size_t maximum_length;
Packit Service a0468f
};
Packit Service a0468f
Packit Service a0468f
/* Update *COUNTS according to the contents of HWCAPS.  Skip over
Packit Service a0468f
   entries whose bit is not set in MASK.  */
Packit Service a0468f
static void
Packit Service a0468f
update_hwcaps_counts (struct hwcaps_counts *counts, const char *hwcaps,
Packit Service a0468f
		      uint32_t bitmask, const char *mask)
Packit Service a0468f
{
Packit Service a0468f
  struct dl_hwcaps_split_masked sp;
Packit Service a0468f
  _dl_hwcaps_split_masked_init (&sp, hwcaps, bitmask, mask);
Packit Service a0468f
  while (_dl_hwcaps_split_masked (&sp))
Packit Service a0468f
    {
Packit Service a0468f
      ++counts->count;
Packit Service a0468f
      counts->total_length += sp.split.length;
Packit Service a0468f
      if (sp.split.length > counts->maximum_length)
Packit Service a0468f
	counts->maximum_length = sp.split.length;
Packit Service a0468f
    }
Packit Service a0468f
}
Packit Service a0468f
Packit Service a0468f
/* State for copy_hwcaps.  Must be initialized to point to
Packit Service a0468f
   the storage areas for the array and the strings themselves.  */
Packit Service a0468f
struct copy_hwcaps
Packit Service a0468f
{
Packit Service a0468f
  struct r_strlenpair *next_pair;
Packit Service a0468f
  char *next_string;
Packit Service a0468f
};
Packit Service a0468f
Packit Service a0468f
/* Copy HWCAPS into the string pairs and strings, advancing *TARGET.
Packit Service a0468f
   Skip over entries whose bit is not set in MASK.  */
Packit Service a0468f
static void
Packit Service a0468f
copy_hwcaps (struct copy_hwcaps *target, const char *hwcaps,
Packit Service a0468f
	     uint32_t bitmask, const char *mask)
Packit Service a0468f
{
Packit Service a0468f
  struct dl_hwcaps_split_masked sp;
Packit Service a0468f
  _dl_hwcaps_split_masked_init (&sp, hwcaps, bitmask, mask);
Packit Service a0468f
  while (_dl_hwcaps_split_masked (&sp))
Packit Service a0468f
    {
Packit Service a0468f
      target->next_pair->str = target->next_string;
Packit Service a0468f
      char *slash = __mempcpy (__mempcpy (target->next_string,
Packit Service a0468f
					  GLIBC_HWCAPS_PREFIX,
Packit Service a0468f
					  strlen (GLIBC_HWCAPS_PREFIX)),
Packit Service a0468f
			       sp.split.segment, sp.split.length);
Packit Service a0468f
      *slash = '/';
Packit Service a0468f
      target->next_pair->len
Packit Service a0468f
	= strlen (GLIBC_HWCAPS_PREFIX) + sp.split.length + 1;
Packit Service a0468f
      ++target->next_pair;
Packit Service a0468f
      target->next_string = slash + 1;
Packit Service a0468f
    }
Packit Service a0468f
}
Packit Service a0468f
Packit Service 4fe48f
struct dl_hwcaps_priority *_dl_hwcaps_priorities;
Packit Service 4fe48f
uint32_t _dl_hwcaps_priorities_length;
Packit Service 4fe48f
Packit Service 4fe48f
/* Allocate _dl_hwcaps_priorities and fill it with data.  */
Packit Service 4fe48f
static void
Packit Service 4fe48f
compute_priorities (size_t total_count, const char *prepend,
Packit Service 4fe48f
		    uint32_t bitmask, const char *mask)
Packit Service 4fe48f
{
Packit Service 4fe48f
  _dl_hwcaps_priorities = malloc (total_count
Packit Service 4fe48f
				  * sizeof (*_dl_hwcaps_priorities));
Packit Service 4fe48f
  if (_dl_hwcaps_priorities == NULL)
Packit Service 4fe48f
    _dl_signal_error (ENOMEM, NULL, NULL,
Packit Service 4fe48f
		      N_("cannot create HWCAP priorities"));
Packit Service 4fe48f
  _dl_hwcaps_priorities_length = total_count;
Packit Service 4fe48f
Packit Service 4fe48f
  /* First the prepended subdirectories.  */
Packit Service 4fe48f
  size_t i = 0;
Packit Service 4fe48f
  {
Packit Service 4fe48f
    struct dl_hwcaps_split sp;
Packit Service 4fe48f
    _dl_hwcaps_split_init (&sp, prepend);
Packit Service 4fe48f
    while (_dl_hwcaps_split (&sp))
Packit Service 4fe48f
      {
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].name = sp.segment;
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].name_length = sp.length;
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].priority = i + 1;
Packit Service 4fe48f
	++i;
Packit Service 4fe48f
      }
Packit Service 4fe48f
  }
Packit Service 4fe48f
Packit Service 4fe48f
  /* Then the built-in subdirectories that are actually active.  */
Packit Service 4fe48f
  {
Packit Service 4fe48f
    struct dl_hwcaps_split_masked sp;
Packit Service 4fe48f
    _dl_hwcaps_split_masked_init (&sp, _dl_hwcaps_subdirs, bitmask, mask);
Packit Service 4fe48f
    while (_dl_hwcaps_split_masked (&sp))
Packit Service 4fe48f
      {
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].name = sp.split.segment;
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].name_length = sp.split.length;
Packit Service 4fe48f
	_dl_hwcaps_priorities[i].priority = i + 1;
Packit Service 4fe48f
	++i;
Packit Service 4fe48f
      }
Packit Service 4fe48f
  }
Packit Service 4fe48f
  assert (i == total_count);
Packit Service 4fe48f
}
Packit Service 4fe48f
Packit Service 4fe48f
/* Sort the _dl_hwcaps_priorities array by name.  */
Packit Service 4fe48f
static void
Packit Service 4fe48f
sort_priorities_by_name (void)
Packit Service 4fe48f
{
Packit Service 4fe48f
  /* Insertion sort.  There is no need to link qsort into the dynamic
Packit Service 4fe48f
     loader for such a short array.  */
Packit Service 4fe48f
  for (size_t i = 1; i < _dl_hwcaps_priorities_length; ++i)
Packit Service 4fe48f
    for (size_t j = i; j > 0; --j)
Packit Service 4fe48f
      {
Packit Service 4fe48f
	struct dl_hwcaps_priority *previous = _dl_hwcaps_priorities + j - 1;
Packit Service 4fe48f
	struct dl_hwcaps_priority *current = _dl_hwcaps_priorities + j;
Packit Service 4fe48f
Packit Service 4fe48f
	/* Bail out if current is greater or equal to the previous
Packit Service 4fe48f
	   value.  */
Packit Service 4fe48f
	uint32_t to_compare;
Packit Service 4fe48f
	if (current->name_length < previous->name_length)
Packit Service 4fe48f
	  to_compare = current->name_length;
Packit Service 4fe48f
	else
Packit Service 4fe48f
	  to_compare = previous->name_length;
Packit Service 4fe48f
	int cmp = memcmp (current->name, previous->name, to_compare);
Packit Service cb4246
	if (cmp > 0
Packit Service 4fe48f
	    || (cmp == 0 && current->name_length >= previous->name_length))
Packit Service 4fe48f
	  break;
Packit Service 4fe48f
Packit Service 4fe48f
	/* Swap *previous and *current.  */
Packit Service 4fe48f
	struct dl_hwcaps_priority tmp = *previous;
Packit Service 4fe48f
	*previous = *current;
Packit Service 4fe48f
	*current = tmp;
Packit Service 4fe48f
      }
Packit Service 4fe48f
}
Packit Service 4fe48f
Packit 6c4009
/* Return an array of useful/necessary hardware capability names.  */
Packit 6c4009
const struct r_strlenpair *
Packit Service a0468f
_dl_important_hwcaps (const char *glibc_hwcaps_prepend,
Packit Service a0468f
		      const char *glibc_hwcaps_mask,
Packit Service a0468f
		      size_t *sz, 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 68b29c
  size_t cnt = GLRO (dl_platform) != NULL;
Packit 6c4009
  size_t n, m;
Packit 6c4009
  struct r_strlenpair *result;
Packit 6c4009
  struct r_strlenpair *rp;
Packit 6c4009
  char *cp;
Packit 6c4009
Packit Service a0468f
  /* glibc-hwcaps subdirectories.  These are exempted from the power
Packit Service a0468f
     set construction below.  */
Packit Service a0468f
  uint32_t hwcaps_subdirs_active = _dl_hwcaps_subdirs_active ();
Packit Service a0468f
  struct hwcaps_counts hwcaps_counts =  { 0, };
Packit Service a0468f
  update_hwcaps_counts (&hwcaps_counts, glibc_hwcaps_prepend, -1, NULL);
Packit Service a0468f
  update_hwcaps_counts (&hwcaps_counts, _dl_hwcaps_subdirs,
Packit Service a0468f
			hwcaps_subdirs_active, glibc_hwcaps_mask);
Packit Service 4fe48f
  compute_priorities (hwcaps_counts.count, glibc_hwcaps_prepend,
Packit Service 4fe48f
		      hwcaps_subdirs_active, glibc_hwcaps_mask);
Packit Service 4fe48f
  sort_priorities_by_name ();
Packit Service a0468f
Packit Service a0468f
  /* Each hwcaps subdirectory has a GLIBC_HWCAPS_PREFIX string prefix
Packit Service a0468f
     and a "/" suffix once stored in the result.  */
Packit Service cbef9a
  hwcaps_counts.maximum_length += strlen (GLIBC_HWCAPS_PREFIX) + 1;
Packit Service a0468f
  size_t total = (hwcaps_counts.count * (strlen (GLIBC_HWCAPS_PREFIX) + 1)
Packit Service a0468f
		  + hwcaps_counts.total_length);
Packit Service a0468f
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
  /* 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
  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 68b29c
  if (GLRO (dl_platform) != NULL)
Packit 6c4009
    {
Packit Service 68b29c
      temp[m].str = GLRO (dl_platform);
Packit Service 68b29c
      temp[m].len = GLRO (dl_platformlen);
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 a0468f
    total += temp[0].len + 1;
Packit 6c4009
  else
Packit 6c4009
    {
Packit Service a0468f
      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 a0468f
  *sz = hwcaps_counts.count + (1 << cnt);
Packit Service a0468f
Packit Service a0468f
  /* This is the overall result, including both glibc-hwcaps
Packit Service a0468f
     subdirectories and the legacy hwcaps subdirectories using the
Packit Service a0468f
     power set construction.  */
Packit Service a0468f
  struct r_strlenpair *overall_result
Packit Service a0468f
    = malloc (*sz * sizeof (*result) + total);
Packit Service a0468f
  if (overall_result == NULL)
Packit 6c4009
    _dl_signal_error (ENOMEM, NULL, NULL,
Packit 6c4009
		      N_("cannot create capability list"));
Packit 6c4009
Packit Service a0468f
  /* Fill in the glibc-hwcaps subdirectories.  */
Packit Service a0468f
  {
Packit Service a0468f
    struct copy_hwcaps target;
Packit Service a0468f
    target.next_pair = overall_result;
Packit Service a0468f
    target.next_string = (char *) (overall_result + *sz);
Packit Service a0468f
    copy_hwcaps (&target, glibc_hwcaps_prepend, -1, NULL);
Packit Service a0468f
    copy_hwcaps (&target, _dl_hwcaps_subdirs,
Packit Service a0468f
		 hwcaps_subdirs_active, glibc_hwcaps_mask);
Packit Service a0468f
    /* Set up the write target for the power set construction.  */
Packit Service a0468f
    result = target.next_pair;
Packit Service a0468f
    cp = target.next_string;
Packit Service a0468f
  }
Packit Service a0468f
Packit Service a0468f
Packit Service a0468f
  /* Power set construction begins here.  We use a very compressed way
Packit Service a0468f
     to store the various combinations of capability names.  */
Packit Service a0468f
Packit 6c4009
  if (cnt == 1)
Packit 6c4009
    {
Packit Service a0468f
      result[0].str = cp;
Packit 6c4009
      result[0].len = temp[0].len + 1;
Packit Service a0468f
      result[1].str = cp;
Packit 6c4009
      result[1].len = 0;
Packit Service a0468f
      cp = __mempcpy (cp, temp[0].str, temp[0].len);
Packit 6c4009
      *cp = '/';
Packit Service a0468f
      if (result[0].len > hwcaps_counts.maximum_length)
Packit Service a0468f
	*max_capstrlen = result[0].len;
Packit Service a0468f
      else
Packit Service a0468f
	*max_capstrlen = hwcaps_counts.maximum_length;
Packit 6c4009
Packit Service a0468f
      return overall_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 a0468f
  result[1].str = result[0].str = cp;
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 a0468f
  if (result[0].len > hwcaps_counts.maximum_length)
Packit Service a0468f
    *max_capstrlen = result[0].len;
Packit Service a0468f
  else
Packit Service a0468f
    *max_capstrlen = hwcaps_counts.maximum_length;
Packit 6c4009
Packit Service a0468f
  return overall_result;
Packit 6c4009
}