Blame elf/get-dynamic-info.h

Packit 6c4009
/* Read the dynamic section at DYN and fill in INFO with indices DT_*.
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
/* This file is included multiple times and therefore lacks a header
Packit 6c4009
   file inclusion guard.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <libc-diag.h>
Packit 6c4009
Packit 6c4009
#ifndef RESOLVE_MAP
Packit 6c4009
static
Packit 6c4009
#else
Packit 6c4009
auto
Packit 6c4009
#endif
Packit 6c4009
inline void __attribute__ ((unused, always_inline))
Packit 6c4009
elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
Packit 6c4009
{
Packit 6c4009
  ElfW(Dyn) *dyn = l->l_ld;
Packit 6c4009
  ElfW(Dyn) **info;
Packit 6c4009
#if __ELF_NATIVE_CLASS == 32
Packit 6c4009
  typedef Elf32_Word d_tag_utype;
Packit 6c4009
#elif __ELF_NATIVE_CLASS == 64
Packit 6c4009
  typedef Elf64_Xword d_tag_utype;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if !defined RTLD_BOOTSTRAP && !defined STATIC_PIE_BOOTSTRAP
Packit 6c4009
  if (dyn == NULL)
Packit 6c4009
    return;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  info = l->l_info;
Packit 6c4009
Packit 6c4009
  while (dyn->d_tag != DT_NULL)
Packit 6c4009
    {
Packit 6c4009
      if ((d_tag_utype) dyn->d_tag < DT_NUM)
Packit 6c4009
	info[dyn->d_tag] = dyn;
Packit 6c4009
      else if (dyn->d_tag >= DT_LOPROC &&
Packit 6c4009
	       dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
Packit 6c4009
	{
Packit 6c4009
	  /* This does not violate the array bounds of l->l_info, but
Packit 6c4009
	     gcc 4.6 on sparc somehow does not see this.  */
Packit 6c4009
	  DIAG_PUSH_NEEDS_COMMENT;
Packit 6c4009
	  DIAG_IGNORE_NEEDS_COMMENT (4.6,
Packit 6c4009
				     "-Warray-bounds");
Packit 6c4009
	  info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
Packit 6c4009
	  DIAG_POP_NEEDS_COMMENT;
Packit 6c4009
	}
Packit 6c4009
      else if ((d_tag_utype) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
Packit 6c4009
	info[VERSYMIDX (dyn->d_tag)] = dyn;
Packit 6c4009
      else if ((d_tag_utype) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
Packit 6c4009
	info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
Packit 6c4009
	     + DT_VERSIONTAGNUM] = dyn;
Packit 6c4009
      else if ((d_tag_utype) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
Packit 6c4009
	info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
Packit 6c4009
	     + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
Packit 6c4009
      else if ((d_tag_utype) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
Packit 6c4009
	info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
Packit 6c4009
	     + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
Packit 6c4009
      ++dyn;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#define DL_RO_DYN_TEMP_CNT	8
Packit 6c4009
Packit 6c4009
#ifndef DL_RO_DYN_SECTION
Packit 6c4009
  /* Don't adjust .dynamic unnecessarily.  */
Packit 6c4009
  if (l->l_addr != 0)
Packit 6c4009
    {
Packit 6c4009
      ElfW(Addr) l_addr = l->l_addr;
Packit 6c4009
      int cnt = 0;
Packit 6c4009
Packit 6c4009
# define ADJUST_DYN_INFO(tag) \
Packit 6c4009
      do								      \
Packit 6c4009
	if (info[tag] != NULL)						      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    if (temp)							      \
Packit 6c4009
	      {								      \
Packit 6c4009
		temp[cnt].d_tag = info[tag]->d_tag;			      \
Packit 6c4009
		temp[cnt].d_un.d_ptr = info[tag]->d_un.d_ptr + l_addr;	      \
Packit 6c4009
		info[tag] = temp + cnt++;				      \
Packit 6c4009
	      }								      \
Packit 6c4009
	    else							      \
Packit 6c4009
	      info[tag]->d_un.d_ptr += l_addr;				      \
Packit 6c4009
	  }								      \
Packit 6c4009
      while (0)
Packit 6c4009
Packit 6c4009
      ADJUST_DYN_INFO (DT_HASH);
Packit 6c4009
      ADJUST_DYN_INFO (DT_PLTGOT);
Packit 6c4009
      ADJUST_DYN_INFO (DT_STRTAB);
Packit 6c4009
      ADJUST_DYN_INFO (DT_SYMTAB);
Packit 6c4009
# if ! ELF_MACHINE_NO_RELA
Packit 6c4009
      ADJUST_DYN_INFO (DT_RELA);
Packit 6c4009
# endif
Packit 6c4009
# if ! ELF_MACHINE_NO_REL
Packit 6c4009
      ADJUST_DYN_INFO (DT_REL);
Packit 6c4009
# endif
Packit 6c4009
      ADJUST_DYN_INFO (DT_JMPREL);
Packit 6c4009
      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
Packit 6c4009
      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
Packit 6c4009
# undef ADJUST_DYN_INFO
Packit 6c4009
      assert (cnt <= DL_RO_DYN_TEMP_CNT);
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
  if (info[DT_PLTREL] != NULL)
Packit 6c4009
    {
Packit 6c4009
#if ELF_MACHINE_NO_RELA
Packit 6c4009
      assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
Packit 6c4009
#elif ELF_MACHINE_NO_REL
Packit 6c4009
      assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
Packit 6c4009
#else
Packit 6c4009
      assert (info[DT_PLTREL]->d_un.d_val == DT_REL
Packit 6c4009
	      || info[DT_PLTREL]->d_un.d_val == DT_RELA);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
#if ! ELF_MACHINE_NO_RELA
Packit 6c4009
  if (info[DT_RELA] != NULL)
Packit 6c4009
    assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
Packit 6c4009
# endif
Packit 6c4009
# if ! ELF_MACHINE_NO_REL
Packit 6c4009
  if (info[DT_REL] != NULL)
Packit 6c4009
    assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
Packit 6c4009
#endif
Packit 6c4009
#ifdef RTLD_BOOTSTRAP
Packit 6c4009
  /* Only the bind now flags are allowed.  */
Packit 6c4009
  assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL
Packit 6c4009
	  || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0);
Packit 6c4009
  /* Flags must not be set for ld.so.  */
Packit 6c4009
  assert (info[DT_FLAGS] == NULL
Packit 6c4009
	  || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0);
Packit 6c4009
#endif
Packit 6c4009
#if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP
Packit 6c4009
  assert (info[DT_RUNPATH] == NULL);
Packit 6c4009
  assert (info[DT_RPATH] == NULL);
Packit 6c4009
#else
Packit 6c4009
  if (info[DT_FLAGS] != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Flags are used.  Translate to the old form where available.
Packit 6c4009
	 Since these l_info entries are only tested for NULL pointers it
Packit 6c4009
	 is ok if they point to the DT_FLAGS entry.  */
Packit 6c4009
      l->l_flags = info[DT_FLAGS]->d_un.d_val;
Packit 6c4009
Packit 6c4009
      if (l->l_flags & DF_SYMBOLIC)
Packit 6c4009
	info[DT_SYMBOLIC] = info[DT_FLAGS];
Packit 6c4009
      if (l->l_flags & DF_TEXTREL)
Packit 6c4009
	info[DT_TEXTREL] = info[DT_FLAGS];
Packit 6c4009
      if (l->l_flags & DF_BIND_NOW)
Packit 6c4009
	info[DT_BIND_NOW] = info[DT_FLAGS];
Packit 6c4009
    }
Packit 6c4009
  if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
Packit 6c4009
    {
Packit 6c4009
      l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
Packit 6c4009
Packit 6c4009
      /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like
Packit 6c4009
	 to assert this, but we can't. Users have been setting
Packit 6c4009
	 unsupported DF_1_* flags for a long time and glibc has ignored
Packit 6c4009
	 them. Therefore to avoid breaking existing applications the
Packit 6c4009
	 best we can do is add a warning during debugging with the
Packit 6c4009
	 intent of notifying the user of the problem.  */
Packit 6c4009
      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
Packit 6c4009
	  && l->l_flags_1 & ~DT_1_SUPPORTED_MASK)
Packit 6c4009
	_dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x in DT_FLAGS_1.\n",
Packit 6c4009
			  l->l_flags_1 & ~DT_1_SUPPORTED_MASK);
Packit 6c4009
Packit 6c4009
      if (l->l_flags_1 & DF_1_NOW)
Packit 6c4009
	info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)];
Packit 6c4009
    }
Packit 6c4009
  if (info[DT_RUNPATH] != NULL)
Packit 6c4009
    /* If both RUNPATH and RPATH are given, the latter is ignored.  */
Packit 6c4009
    info[DT_RPATH] = NULL;
Packit 6c4009
#endif
Packit 6c4009
}