Blame elf/tlsdeschtab.h

Packit 6c4009
/* Hash table for TLS descriptors.
Packit 6c4009
   Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Alexandre Oliva  <aoliva@redhat.com>
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
#ifndef TLSDESCHTAB_H
Packit 6c4009
# define TLSDESCHTAB_H 1
Packit 6c4009
Packit 6c4009
#include <atomic.h>
Packit 6c4009
Packit 6c4009
# ifdef SHARED
Packit 6c4009
Packit 6c4009
#  include <inline-hashtab.h>
Packit 6c4009
Packit 6c4009
inline static int
Packit 6c4009
hash_tlsdesc (void *p)
Packit 6c4009
{
Packit 6c4009
  struct tlsdesc_dynamic_arg *td = p;
Packit 6c4009
Packit 6c4009
  /* We know all entries are for the same module, so ti_offset is the
Packit 6c4009
     only distinguishing entry.  */
Packit 6c4009
  return td->tlsinfo.ti_offset;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
inline static int
Packit 6c4009
eq_tlsdesc (void *p, void *q)
Packit 6c4009
{
Packit 6c4009
  struct tlsdesc_dynamic_arg *tdp = p, *tdq = q;
Packit 6c4009
Packit 6c4009
  return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
inline static size_t
Packit 6c4009
map_generation (struct link_map *map)
Packit 6c4009
{
Packit 6c4009
  size_t idx = map->l_tls_modid;
Packit 6c4009
  struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list);
Packit 6c4009
Packit 6c4009
  /* Find the place in the dtv slotinfo list.  */
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      /* Does it fit in the array of this list element?  */
Packit 6c4009
      if (idx < listp->len)
Packit 6c4009
	{
Packit 6c4009
	  /* We should never get here for a module in static TLS, so
Packit 6c4009
	     we can assume that, if the generation count is zero, we
Packit 6c4009
	     still haven't determined the generation count for this
Packit 6c4009
	     module.  */
Packit 6c4009
	  if (listp->slotinfo[idx].map == map && listp->slotinfo[idx].gen)
Packit 6c4009
	    return listp->slotinfo[idx].gen;
Packit 6c4009
	  else
Packit 6c4009
	    break;
Packit 6c4009
	}
Packit 6c4009
      idx -= listp->len;
Packit 6c4009
      listp = listp->next;
Packit 6c4009
    }
Packit 6c4009
  while (listp != NULL);
Packit 6c4009
Packit 6c4009
  /* If we get to this point, the module still hasn't been assigned an
Packit 6c4009
     entry in the dtv slotinfo data structures, and it will when we're
Packit 6c4009
     done with relocations.  At that point, the module will get a
Packit 6c4009
     generation number that is one past the current generation, so
Packit 6c4009
     return exactly that.  */
Packit 6c4009
  return GL(dl_tls_generation) + 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
_dl_make_tlsdesc_dynamic (struct link_map *map, size_t ti_offset)
Packit 6c4009
{
Packit 6c4009
  struct hashtab *ht;
Packit 6c4009
  void **entry;
Packit 6c4009
  struct tlsdesc_dynamic_arg *td, test;
Packit 6c4009
Packit 6c4009
  /* FIXME: We could use a per-map lock here, but is it worth it?  */
Packit 6c4009
  __rtld_lock_lock_recursive (GL(dl_load_lock));
Packit 6c4009
Packit 6c4009
  ht = map->l_mach.tlsdesc_table;
Packit 6c4009
  if (! ht)
Packit 6c4009
    {
Packit 6c4009
      ht = htab_create ();
Packit 6c4009
      if (! ht)
Packit 6c4009
	{
Packit 6c4009
	  __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      map->l_mach.tlsdesc_table = ht;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  test.tlsinfo.ti_module = map->l_tls_modid;
Packit 6c4009
  test.tlsinfo.ti_offset = ti_offset;
Packit 6c4009
  entry = htab_find_slot (ht, &test, 1, hash_tlsdesc, eq_tlsdesc);
Packit 6c4009
  if (! entry)
Packit 6c4009
    {
Packit 6c4009
      __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (*entry)
Packit 6c4009
    {
Packit 6c4009
      td = *entry;
Packit 6c4009
      __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
      return td;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *entry = td = malloc (sizeof (struct tlsdesc_dynamic_arg));
Packit 6c4009
  /* This may be higher than the map's generation, but it doesn't
Packit 6c4009
     matter much.  Worst case, we'll have one extra DTV update per
Packit 6c4009
     thread.  */
Packit 6c4009
  td->gen_count = map_generation (map);
Packit 6c4009
  td->tlsinfo = test.tlsinfo;
Packit 6c4009
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
  return td;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# endif /* SHARED */
Packit 6c4009
Packit 6c4009
/* The idea of the following two functions is to stop multiple threads
Packit 6c4009
   from attempting to resolve the same TLS descriptor without busy
Packit 6c4009
   waiting.  Ideally, we should be able to release the lock right
Packit 6c4009
   after changing td->entry, and then using say a condition variable
Packit 6c4009
   or a futex wake to wake up any waiting threads, but let's try to
Packit 6c4009
   avoid introducing such dependencies.  */
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
__attribute__ ((unused))
Packit 6c4009
_dl_tlsdesc_resolve_early_return_p (struct tlsdesc volatile *td, void *caller)
Packit 6c4009
{
Packit 6c4009
  if (caller != atomic_load_relaxed (&td->entry))
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  __rtld_lock_lock_recursive (GL(dl_load_lock));
Packit 6c4009
  if (caller != atomic_load_relaxed (&td->entry))
Packit 6c4009
    {
Packit 6c4009
      __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  atomic_store_relaxed (&td->entry, _dl_tlsdesc_resolve_hold);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((unused))
Packit 6c4009
_dl_tlsdesc_wake_up_held_fixups (void)
Packit 6c4009
{
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif