Blame sysdeps/i386/tlsdesc.c

Packit 6c4009
/* Manage TLS descriptors.  i386 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 4 functions take an entry_check_offset argument.
Packit 6c4009
   It's computed by the caller as an offset between its entry point
Packit 6c4009
   and the call site, such that by adding the built-in return address
Packit 6c4009
   that is implicitly passed to the function with this offset, we can
Packit 6c4009
   easily obtain the caller's entry point to compare with the entry
Packit 6c4009
   point given in the TLS descriptor.  If it's changed, we want to
Packit 6c4009
   return immediately.  */
Packit 6c4009
Packit 6c4009
/* This function is used to lazily resolve TLS_DESC REL relocations
Packit 6c4009
   that reference the *ABS* segment in their own link maps.  The
Packit 6c4009
   argument is the addend originally stored there.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__attribute__ ((regparm (3))) attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_abs_plus_addend_fixup (struct tlsdesc volatile *td,
Packit 6c4009
					   struct link_map *l,
Packit 6c4009
					   ptrdiff_t entry_check_offset)
Packit 6c4009
{
Packit 6c4009
  ptrdiff_t addend = (ptrdiff_t) td->arg;
Packit 6c4009
Packit 6c4009
  if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
Packit 6c4009
					  - entry_check_offset))
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
#ifndef SHARED
Packit 6c4009
  CHECK_STATIC_TLS (l, l);
Packit 6c4009
#else
Packit 6c4009
  if (!TRY_STATIC_TLS (l, l))
Packit 6c4009
    {
Packit 6c4009
      td->arg = _dl_make_tlsdesc_dynamic (l, addend);
Packit 6c4009
      td->entry = _dl_tlsdesc_dynamic;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
    {
Packit 6c4009
      td->arg = (void*) (addend - l->l_tls_offset);
Packit 6c4009
      td->entry = _dl_tlsdesc_return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _dl_tlsdesc_wake_up_held_fixups ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* This function is used to lazily resolve TLS_DESC REL relocations
Packit 6c4009
   that originally had zero addends.  The argument location, that
Packit 6c4009
   originally held the addend, is used to hold a pointer to the
Packit 6c4009
   relocation, but it has to be restored before we call the function
Packit 6c4009
   that applies relocations.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__attribute__ ((regparm (3))) attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_rel_fixup (struct tlsdesc volatile *td,
Packit 6c4009
			       struct link_map *l,
Packit 6c4009
			       ptrdiff_t entry_check_offset)
Packit 6c4009
{
Packit 6c4009
  const ElfW(Rel) *reloc = td->arg;
Packit 6c4009
Packit 6c4009
  if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
Packit 6c4009
					  - entry_check_offset))
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* The code below was borrowed from _dl_fixup(),
Packit 6c4009
     except for checking for STB_LOCAL.  */
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 = 0;
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
	  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
	  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 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__ ((regparm (3))) attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_rela_fixup (struct tlsdesc volatile *td,
Packit 6c4009
				struct link_map *l,
Packit 6c4009
				ptrdiff_t entry_check_offset)
Packit 6c4009
{
Packit 6c4009
  const ElfW(Rela) *reloc = td->arg;
Packit 6c4009
Packit 6c4009
  if (_dl_tlsdesc_resolve_early_return_p (td, __builtin_return_address (0)
Packit 6c4009
					  - entry_check_offset))
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* The code below was borrowed from _dl_fixup(),
Packit 6c4009
     except for checking for STB_LOCAL.  */
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__ ((regparm (3))) attribute_hidden
Packit 6c4009
_dl_tlsdesc_resolve_hold_fixup (struct tlsdesc volatile *td,
Packit 6c4009
				struct link_map *l __attribute__((__unused__)),
Packit 6c4009
				ptrdiff_t entry_check_offset)
Packit 6c4009
{
Packit 6c4009
  /* Maybe we're lucky and can return early.  */
Packit 6c4009
  if (__builtin_return_address (0) - entry_check_offset != 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
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
  if (map->l_mach.tlsdesc_table)
Packit 6c4009
    htab_delete (map->l_mach.tlsdesc_table);
Packit 6c4009
#endif
Packit 6c4009
}