Blame elf/dynamic-link.h

Packit 6c4009
/* Inline functions for dynamic linking.
Packit 6c4009
   Copyright (C) 1995-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
/* This macro is used as a callback from elf_machine_rel{a,} when a
Packit 6c4009
   static TLS reloc is about to be performed.  Since (in dl-load.c) we
Packit 6c4009
   permit dynamic loading of objects that might use such relocs, we
Packit 6c4009
   have to check whether each use is actually doable.  If the object
Packit 6c4009
   whose TLS segment the reference resolves to was allocated space in
Packit 6c4009
   the static TLS block at startup, then it's ok.  Otherwise, we make
Packit 6c4009
   an attempt to allocate it in surplus space on the fly.  If that
Packit 6c4009
   can't be done, we fall back to the error that DF_STATIC_TLS is
Packit 6c4009
   intended to produce.  */
Packit 6c4009
#define HAVE_STATIC_TLS(map, sym_map)					\
Packit 6c4009
    (__builtin_expect ((sym_map)->l_tls_offset != NO_TLS_OFFSET		\
Packit 6c4009
		       && ((sym_map)->l_tls_offset			\
Packit 6c4009
			   != FORCED_DYNAMIC_TLS_OFFSET), 1))
Packit 6c4009
Packit 6c4009
#define CHECK_STATIC_TLS(map, sym_map)					\
Packit 6c4009
    do {								\
Packit 6c4009
      if (!HAVE_STATIC_TLS (map, sym_map))				\
Packit 6c4009
	_dl_allocate_static_tls (sym_map);				\
Packit 6c4009
    } while (0)
Packit 6c4009
Packit 6c4009
#define TRY_STATIC_TLS(map, sym_map)					\
Packit 6c4009
    (__builtin_expect ((sym_map)->l_tls_offset				\
Packit 6c4009
		       != FORCED_DYNAMIC_TLS_OFFSET, 1)			\
Packit 6c4009
     && (__builtin_expect ((sym_map)->l_tls_offset != NO_TLS_OFFSET, 1)	\
Packit Service 7c630d
	 || _dl_try_allocate_static_tls (sym_map, true) == 0))
Packit 6c4009
Packit Service 7c630d
int _dl_try_allocate_static_tls (struct link_map *map, bool optional)
Packit Service 7c630d
  attribute_hidden;
Packit 6c4009
Packit 6c4009
#include <elf.h>
Packit 6c4009
Packit 6c4009
#ifdef RESOLVE_MAP
Packit 6c4009
/* We pass reloc_addr as a pointer to void, as opposed to a pointer to
Packit 6c4009
   ElfW(Addr), because not all architectures can assume that the
Packit 6c4009
   relocated address is properly aligned, whereas the compiler is
Packit 6c4009
   entitled to assume that a pointer to a type is properly aligned for
Packit 6c4009
   the type.  Even if we cast the pointer back to some other type with
Packit 6c4009
   less strict alignment requirements, the compiler might still
Packit 6c4009
   remember that the pointer was originally more aligned, thereby
Packit 6c4009
   optimizing away alignment tests or using word instructions for
Packit 6c4009
   copying memory, breaking the very code written to handle the
Packit 6c4009
   unaligned cases.  */
Packit 6c4009
# if ! ELF_MACHINE_NO_REL
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
Packit 6c4009
		 const ElfW(Sym) *sym, const struct r_found_version *version,
Packit 6c4009
		 void *const reloc_addr, int skip_ifunc);
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
Packit 6c4009
			  void *const reloc_addr);
Packit 6c4009
# endif
Packit 6c4009
# if ! ELF_MACHINE_NO_RELA
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
Packit 6c4009
		  const ElfW(Sym) *sym, const struct r_found_version *version,
Packit 6c4009
		  void *const reloc_addr, int skip_ifunc);
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
Packit 6c4009
			   void *const reloc_addr);
Packit 6c4009
# endif
Packit 6c4009
# if ELF_MACHINE_NO_RELA || defined ELF_MACHINE_PLT_REL
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_lazy_rel (struct link_map *map,
Packit 6c4009
		      ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
Packit 6c4009
		      int skip_ifunc);
Packit 6c4009
# else
Packit 6c4009
auto inline void __attribute__((always_inline))
Packit 6c4009
elf_machine_lazy_rel (struct link_map *map,
Packit 6c4009
		      ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
Packit 6c4009
		      int skip_ifunc);
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <dl-machine.h>
Packit 6c4009
Packit 6c4009
#include "get-dynamic-info.h"
Packit 6c4009
Packit 6c4009
#ifdef RESOLVE_MAP
Packit 6c4009
Packit 6c4009
# if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP
Packit 6c4009
#  define ELF_DURING_STARTUP (1)
Packit 6c4009
# else
Packit 6c4009
#  define ELF_DURING_STARTUP (0)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
Packit 6c4009
   These functions are almost identical, so we use cpp magic to avoid
Packit 6c4009
   duplicating their code.  It cannot be done in a more general function
Packit 6c4009
   because we must be able to completely inline.  */
Packit 6c4009
Packit 6c4009
/* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
Packit 6c4009
   range.  Note that according to the ELF spec, this is completely legal!
Packit 6c4009
Packit 6c4009
   We are guarenteed that we have one of three situations.  Either DT_JMPREL
Packit 6c4009
   comes immediately after DT_REL*, or there is overlap and DT_JMPREL
Packit 6c4009
   consumes precisely the very end of the DT_REL*, or DT_JMPREL and DT_REL*
Packit 6c4009
   are completely separate and there is a gap between them.  */
Packit 6c4009
Packit 6c4009
# define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, skip_ifunc, test_rel) \
Packit 6c4009
  do {									      \
Packit 6c4009
    struct { ElfW(Addr) start, size;					      \
Packit 6c4009
	     __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; }  \
Packit 6c4009
      ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };			      \
Packit 6c4009
									      \
Packit 6c4009
    if ((map)->l_info[DT_##RELOC])					      \
Packit 6c4009
      {									      \
Packit 6c4009
	ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);		      \
Packit 6c4009
	ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;	      \
Packit 6c4009
	if (map->l_info[VERSYMIDX (DT_##RELOC##COUNT)] != NULL)		      \
Packit 6c4009
	  ranges[0].nrelative						      \
Packit 6c4009
	    = map->l_info[VERSYMIDX (DT_##RELOC##COUNT)]->d_un.d_val;	      \
Packit 6c4009
      }									      \
Packit 6c4009
    if ((map)->l_info[DT_PLTREL]					      \
Packit 6c4009
	&& (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
Packit 6c4009
      {									      \
Packit 6c4009
	ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]);		      \
Packit 6c4009
	ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;	      \
Packit 6c4009
									      \
Packit 6c4009
	if (ranges[0].start + ranges[0].size == (start + size))		      \
Packit 6c4009
	  ranges[0].size -= size;					      \
Packit 6c4009
	if (ELF_DURING_STARTUP						      \
Packit 6c4009
	    || (!(do_lazy)						      \
Packit 6c4009
		&& (ranges[0].start + ranges[0].size) == start))	      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    /* Combine processing the sections.  */			      \
Packit 6c4009
	    ranges[0].size += size;					      \
Packit 6c4009
	  }								      \
Packit 6c4009
	else								      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    ranges[1].start = start;					      \
Packit 6c4009
	    ranges[1].size = size;					      \
Packit 6c4009
	    ranges[1].lazy = (do_lazy);					      \
Packit 6c4009
	  }								      \
Packit 6c4009
      }									      \
Packit 6c4009
									      \
Packit 6c4009
    if (ELF_DURING_STARTUP)						      \
Packit 6c4009
      elf_dynamic_do_##reloc ((map), ranges[0].start, ranges[0].size,	      \
Packit 6c4009
			      ranges[0].nrelative, 0, skip_ifunc);	      \
Packit 6c4009
    else								      \
Packit 6c4009
      {									      \
Packit 6c4009
	int ranges_index;						      \
Packit 6c4009
	for (ranges_index = 0; ranges_index < 2; ++ranges_index)	      \
Packit 6c4009
	  elf_dynamic_do_##reloc ((map),				      \
Packit 6c4009
				  ranges[ranges_index].start,		      \
Packit 6c4009
				  ranges[ranges_index].size,		      \
Packit 6c4009
				  ranges[ranges_index].nrelative,	      \
Packit 6c4009
				  ranges[ranges_index].lazy,		      \
Packit 6c4009
				  skip_ifunc);				      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
# if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
Packit 6c4009
#  define _ELF_CHECK_REL 0
Packit 6c4009
# else
Packit 6c4009
#  define _ELF_CHECK_REL 1
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# if ! ELF_MACHINE_NO_REL
Packit 6c4009
#  include "do-rel.h"
Packit 6c4009
#  define ELF_DYNAMIC_DO_REL(map, lazy, skip_ifunc) \
Packit 6c4009
  _ELF_DYNAMIC_DO_RELOC (REL, Rel, map, lazy, skip_ifunc, _ELF_CHECK_REL)
Packit 6c4009
# else
Packit 6c4009
#  define ELF_DYNAMIC_DO_REL(map, lazy, skip_ifunc) /* Nothing to do.  */
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# if ! ELF_MACHINE_NO_RELA
Packit 6c4009
#  define DO_RELA
Packit 6c4009
#  include "do-rel.h"
Packit 6c4009
#  define ELF_DYNAMIC_DO_RELA(map, lazy, skip_ifunc) \
Packit 6c4009
  _ELF_DYNAMIC_DO_RELOC (RELA, Rela, map, lazy, skip_ifunc, _ELF_CHECK_REL)
Packit 6c4009
# else
Packit 6c4009
#  define ELF_DYNAMIC_DO_RELA(map, lazy, skip_ifunc) /* Nothing to do.  */
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
/* This can't just be an inline function because GCC is too dumb
Packit 6c4009
   to inline functions containing inlines themselves.  */
Packit 6c4009
# define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile, skip_ifunc) \
Packit 6c4009
  do {									      \
Packit 6c4009
    int edr_lazy = elf_machine_runtime_setup ((map), (lazy),		      \
Packit 6c4009
					      (consider_profile));	      \
Packit 6c4009
    ELF_DYNAMIC_DO_REL ((map), edr_lazy, skip_ifunc);			      \
Packit 6c4009
    ELF_DYNAMIC_DO_RELA ((map), edr_lazy, skip_ifunc);			      \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
#endif