Blame sysdeps/x86_64/tlsdesc.c

Packit 6c4009
/* Manage TLS descriptors.  x86_64 version.
Packit 6c4009
   Copyright (C) 2005-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 <link.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
#include <elf/dynamic-link.h>
Packit 6c4009
#include <tls.h>
Packit 6c4009
#include <dl-tlsdesc.h>
Packit 6c4009
#include <dl-unmap-segments.h>
Packit 6c4009
#include <tlsdeschtab.h>
Packit 6c4009
Packit 6c4009
/* The following 2 functions take a caller argument, that contains the
Packit 6c4009
   address expected to be in the TLS descriptor.  If it's changed, we
Packit 6c4009
   want to return immediately.  */
Packit 6c4009
Packit 6c4009
/* This function is used to lazily resolve TLS_DESC RELA relocations.
Packit 6c4009
   The argument location is used to hold a pointer to the relocation.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
Packit 6c4009
				struct link_map *l)
Packit 6c4009
{
Packit 6c4009
  const ElfW(Rela) *reloc = td->arg;
Packit 6c4009
Packit 6c4009
  if (_dl_tlsdesc_resolve_early_return_p
Packit 6c4009
      (td, (void*)(D_PTR (l, l_info[ADDRIDX (DT_TLSDESC_PLT)]) + l->l_addr)))
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* The code below was borrowed from _dl_fixup().  */
Packit 6c4009
  const ElfW(Sym) *const symtab
Packit 6c4009
    = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
Packit 6c4009
  const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
Packit 6c4009
  const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
Packit 6c4009
  lookup_t result;
Packit 6c4009
Packit 6c4009
   /* Look up the target symbol.  If the normal lookup rules are not
Packit 6c4009
      used don't look in the global scope.  */
Packit 6c4009
  if (ELFW(ST_BIND) (sym->st_info) != STB_LOCAL
Packit 6c4009
      && __builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
Packit 6c4009
    {
Packit 6c4009
      const struct r_found_version *version = NULL;
Packit 6c4009
Packit 6c4009
      if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
Packit 6c4009
	{
Packit 6c4009
	  const ElfW(Half) *vernum =
Packit 6c4009
	    (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
Packit 6c4009
	  ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
Packit 6c4009
	  version = &l->l_versions[ndx];
Packit 6c4009
	  if (version->hash == 0)
Packit 6c4009
	    version = NULL;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
Packit 6c4009
				    l->l_scope, version, ELF_RTYPE_CLASS_PLT,
Packit 6c4009
				    DL_LOOKUP_ADD_DEPENDENCY, NULL);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* We already found the symbol.  The module (and therefore its load
Packit 6c4009
	 address) is also known.  */
Packit 6c4009
      result = l;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (! sym)
Packit 6c4009
    {
Packit 6c4009
      td->arg = (void*)reloc->r_addend;
Packit 6c4009
      td->entry = _dl_tlsdesc_undefweak;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
#  ifndef SHARED
Packit 6c4009
      CHECK_STATIC_TLS (l, result);
Packit 6c4009
#  else
Packit 6c4009
      if (!TRY_STATIC_TLS (l, result))
Packit 6c4009
	{
Packit 6c4009
	  td->arg = _dl_make_tlsdesc_dynamic (result, sym->st_value
Packit 6c4009
					      + reloc->r_addend);
Packit 6c4009
	  td->entry = _dl_tlsdesc_dynamic;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
#  endif
Packit 6c4009
	{
Packit 6c4009
	  td->arg = (void*)(sym->st_value - result->l_tls_offset
Packit 6c4009
			    + reloc->r_addend);
Packit 6c4009
	  td->entry = _dl_tlsdesc_return;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _dl_tlsdesc_wake_up_held_fixups ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* This function is used to avoid busy waiting for other threads to
Packit 6c4009
   complete the lazy relocation.  Once another thread wins the race to
Packit 6c4009
   relocate a TLS descriptor, it sets the descriptor up such that this
Packit 6c4009
   function is called to wait until the resolver releases the
Packit 6c4009
   lock.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
Packit 6c4009
				void *caller)
Packit 6c4009
{
Packit 6c4009
  /* Maybe we're lucky and can return early.  */
Packit 6c4009
  if (caller != td->entry)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Locking here will stop execution until the running resolver runs
Packit 6c4009
     _dl_tlsdesc_wake_up_held_fixups(), releasing the lock.
Packit 6c4009
Packit 6c4009
     FIXME: We'd be better off waiting on a condition variable, such
Packit 6c4009
     that we didn't have to hold the lock throughout the relocation
Packit 6c4009
     processing.  */
Packit 6c4009
  __rtld_lock_lock_recursive (GL(dl_load_lock));
Packit 6c4009
  __rtld_lock_unlock_recursive (GL(dl_load_lock));
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Unmap the dynamic object, but also release its TLS descriptor table
Packit 6c4009
   if there is one.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_unmap (struct link_map *map)
Packit 6c4009
{
Packit 6c4009
  _dl_unmap_segments (map);
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
  /* _dl_unmap is only called for dlopen()ed libraries, for which
Packit 6c4009
     calling free() is safe, or before we've completed the initial
Packit 6c4009
     relocation, in which case calling free() is probably pointless,
Packit 6c4009
     but still safe.  */
Packit 6c4009
  if (map->l_mach.tlsdesc_table)
Packit 6c4009
    htab_delete (map->l_mach.tlsdesc_table);
Packit 6c4009
#endif
Packit 6c4009
}