Blame sysdeps/generic/unwind-dw2-fde-glibc.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>.
Packit 6c4009
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
/* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
Packit 6c4009
   segment and dl_iterate_phdr to avoid register/deregister calls at
Packit 6c4009
   DSO load/unload.  */
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# include <shlib-compat.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if !defined _LIBC || SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2_5)
Packit 6c4009
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
#define _Unwind_Find_FDE _Unwind_Find_registered_FDE
Packit 6c4009
Packit 6c4009
#include <unwind-dw2-fde.c>
Packit 6c4009
Packit 6c4009
#undef _Unwind_Find_FDE
Packit 6c4009
Packit 6c4009
extern fde * _Unwind_Find_registered_FDE (void *pc,
Packit 6c4009
					  struct dwarf_eh_bases *bases);
Packit 6c4009
extern fde * _Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
Packit 6c4009
Packit 6c4009
struct unw_eh_callback_data
Packit 6c4009
{
Packit 6c4009
  _Unwind_Ptr pc;
Packit 6c4009
  void *tbase;
Packit 6c4009
  void *dbase;
Packit 6c4009
  void *func;
Packit 6c4009
  fde *ret;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
struct unw_eh_frame_hdr
Packit 6c4009
{
Packit 6c4009
  unsigned char version;
Packit 6c4009
  unsigned char eh_frame_ptr_enc;
Packit 6c4009
  unsigned char fde_count_enc;
Packit 6c4009
  unsigned char table_enc;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Like base_of_encoded_value, but take the base from a struct object
Packit 6c4009
   instead of an _Unwind_Context.  */
Packit 6c4009
Packit 6c4009
static _Unwind_Ptr
Packit 6c4009
base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
Packit 6c4009
{
Packit 6c4009
  if (encoding == DW_EH_PE_omit)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  switch (encoding & 0x70)
Packit 6c4009
    {
Packit 6c4009
    case DW_EH_PE_absptr:
Packit 6c4009
    case DW_EH_PE_pcrel:
Packit 6c4009
    case DW_EH_PE_aligned:
Packit 6c4009
      return 0;
Packit 6c4009
Packit 6c4009
    case DW_EH_PE_textrel:
Packit 6c4009
      return (_Unwind_Ptr) data->tbase;
Packit 6c4009
    case DW_EH_PE_datarel:
Packit 6c4009
      return (_Unwind_Ptr) data->dbase;
Packit 6c4009
    }
Packit 6c4009
  abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
Packit 6c4009
{
Packit 6c4009
  struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
Packit 6c4009
  const ElfW(Phdr) *phdr, *p_eh_frame_hdr;
Packit 6c4009
  const ElfW(Phdr) *p_dynamic __attribute__ ((unused));
Packit 6c4009
  long n, match;
Packit 6c4009
  _Unwind_Ptr load_base;
Packit 6c4009
  const unsigned char *p;
Packit 6c4009
  const struct unw_eh_frame_hdr *hdr;
Packit 6c4009
  _Unwind_Ptr eh_frame;
Packit 6c4009
  struct object ob;
Packit 6c4009
Packit 6c4009
  /* Make sure struct dl_phdr_info is at least as big as we need.  */
Packit 6c4009
  if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
Packit 6c4009
	     + sizeof (info->dlpi_phnum))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  match = 0;
Packit 6c4009
  phdr = info->dlpi_phdr;
Packit 6c4009
  load_base = info->dlpi_addr;
Packit 6c4009
  p_eh_frame_hdr = NULL;
Packit 6c4009
  p_dynamic = NULL;
Packit 6c4009
Packit 6c4009
  /* See if PC falls into one of the loaded segments.  Find the eh_frame
Packit 6c4009
     segment at the same time.  */
Packit 6c4009
  for (n = info->dlpi_phnum; --n >= 0; phdr++)
Packit 6c4009
    {
Packit 6c4009
      if (phdr->p_type == PT_LOAD)
Packit 6c4009
	{
Packit 6c4009
	  _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
Packit 6c4009
	  if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
Packit 6c4009
	    match = 1;
Packit 6c4009
	}
Packit 6c4009
      else if (phdr->p_type == PT_GNU_EH_FRAME)
Packit 6c4009
	p_eh_frame_hdr = phdr;
Packit 6c4009
      else if (phdr->p_type == PT_DYNAMIC)
Packit 6c4009
	p_dynamic = phdr;
Packit 6c4009
    }
Packit 6c4009
  if (!match || !p_eh_frame_hdr)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  /* Read .eh_frame_hdr header.  */
Packit 6c4009
  hdr = (const struct unw_eh_frame_hdr *)
Packit 6c4009
	(p_eh_frame_hdr->p_vaddr + load_base);
Packit 6c4009
  if (hdr->version != 1)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
#ifdef CRT_GET_RFIB_DATA
Packit 6c4009
# ifdef __i386__
Packit 6c4009
  data->dbase = NULL;
Packit 6c4009
  if (p_dynamic)
Packit 6c4009
    {
Packit 6c4009
      /* For dynamicly linked executables and shared libraries,
Packit 6c4009
	 DT_PLTGOT is the gp value for that object.  */
Packit 6c4009
      ElfW(Dyn) *dyn = (ElfW(Dyn) *)(p_dynamic->p_vaddr + load_base);
Packit 6c4009
      for (; dyn->d_tag != DT_NULL ; dyn++)
Packit 6c4009
	if (dyn->d_tag == DT_PLTGOT)
Packit 6c4009
	  {
Packit 6c4009
	    /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it.  */
Packit 6c4009
	    data->dbase = (void *) dyn->d_un.d_ptr;
Packit 6c4009
	    break;
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
# else
Packit 6c4009
#  error What is DW_EH_PE_datarel base on this platform?
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
#ifdef CRT_GET_RFIB_TEXT
Packit 6c4009
# error What is DW_EH_PE_textrel base on this platform?
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
Packit 6c4009
				    base_from_cb_data (hdr->eh_frame_ptr_enc,
Packit 6c4009
						       data),
Packit 6c4009
				    (const unsigned char *) (hdr + 1),
Packit 6c4009
				    &eh_frame);
Packit 6c4009
Packit 6c4009
  /* We require here specific table encoding to speed things up.
Packit 6c4009
     Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
Packit 6c4009
     as base, not the processor specific DW_EH_PE_datarel.  */
Packit 6c4009
  if (hdr->fde_count_enc != DW_EH_PE_omit
Packit 6c4009
      && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
Packit 6c4009
    {
Packit 6c4009
      _Unwind_Ptr fde_count;
Packit 6c4009
Packit 6c4009
      p = read_encoded_value_with_base (hdr->fde_count_enc,
Packit 6c4009
					base_from_cb_data (hdr->fde_count_enc,
Packit 6c4009
							   data),
Packit 6c4009
					p, &fde_count);
Packit 6c4009
      /* Shouldn't happen.  */
Packit 6c4009
      if (fde_count == 0)
Packit 6c4009
	return 1;
Packit 6c4009
      if ((((_Unwind_Ptr) p) & 3) == 0)
Packit 6c4009
	{
Packit 6c4009
	  struct fde_table {
Packit 6c4009
	    signed initial_loc __attribute__ ((mode (SI)));
Packit 6c4009
	    signed fde __attribute__ ((mode (SI)));
Packit 6c4009
	  };
Packit 6c4009
	  const struct fde_table *table = (const struct fde_table *) p;
Packit 6c4009
	  size_t lo, hi, mid;
Packit 6c4009
	  _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
Packit 6c4009
	  fde *f;
Packit 6c4009
	  unsigned int f_enc, f_enc_size;
Packit 6c4009
	  _Unwind_Ptr range;
Packit 6c4009
Packit 6c4009
	  mid = fde_count - 1;
Packit 6c4009
	  if (data->pc < table[0].initial_loc + data_base)
Packit 6c4009
	    return 1;
Packit 6c4009
	  else if (data->pc < table[mid].initial_loc + data_base)
Packit 6c4009
	    {
Packit 6c4009
	      lo = 0;
Packit 6c4009
	      hi = mid;
Packit 6c4009
Packit 6c4009
	      while (lo < hi)
Packit 6c4009
		{
Packit 6c4009
		  mid = (lo + hi) / 2;
Packit 6c4009
		  if (data->pc < table[mid].initial_loc + data_base)
Packit 6c4009
		    hi = mid;
Packit 6c4009
		  else if (data->pc >= table[mid + 1].initial_loc + data_base)
Packit 6c4009
		    lo = mid + 1;
Packit 6c4009
		  else
Packit 6c4009
		    break;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (lo >= hi)
Packit 6c4009
		__gxx_abort ();
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  f = (fde *) (table[mid].fde + data_base);
Packit 6c4009
	  f_enc = get_fde_encoding (f);
Packit 6c4009
	  f_enc_size = size_of_encoded_value (f_enc);
Packit 6c4009
	  read_encoded_value_with_base (f_enc & 0x0f, 0,
Packit 6c4009
					&f->pc_begin[f_enc_size], &range);
Packit 6c4009
	  if (data->pc < table[mid].initial_loc + data_base + range)
Packit 6c4009
	    data->ret = f;
Packit 6c4009
	  data->func = (void *) (table[mid].initial_loc + data_base);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We have no sorted search table, so need to go the slow way.
Packit 6c4009
     As soon as GLIBC will provide API so to notify that a library has been
Packit 6c4009
     removed, we could cache this (and thus use search_object).  */
Packit 6c4009
  ob.pc_begin = NULL;
Packit 6c4009
  ob.tbase = data->tbase;
Packit 6c4009
  ob.dbase = data->dbase;
Packit 6c4009
  ob.u.single = (fde *) eh_frame;
Packit 6c4009
  ob.s.i = 0;
Packit 6c4009
  ob.s.b.mixed_encoding = 1;  /* Need to assume worst case.  */
Packit 6c4009
  data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
Packit 6c4009
  if (data->ret != NULL)
Packit 6c4009
    {
Packit 6c4009
      unsigned int encoding = get_fde_encoding (data->ret);
Packit 6c4009
      _Unwind_Ptr func;
Packit 6c4009
      read_encoded_value_with_base (encoding,
Packit 6c4009
				    base_from_cb_data (encoding, data),
Packit 6c4009
				    data->ret->pc_begin, &func);
Packit 6c4009
      data->func = (void *) func;
Packit 6c4009
    }
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# ifdef _LIBC
Packit 6c4009
# define dl_iterate_phdr __dl_iterate_phdr
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
fde *
Packit 6c4009
_Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
Packit 6c4009
{
Packit 6c4009
  struct unw_eh_callback_data data;
Packit 6c4009
  fde *ret;
Packit 6c4009
Packit 6c4009
  ret = _Unwind_Find_registered_FDE (pc, bases);
Packit 6c4009
  if (ret != NULL)
Packit 6c4009
    return ret;
Packit 6c4009
Packit 6c4009
  data.pc = (_Unwind_Ptr) pc;
Packit 6c4009
  data.tbase = NULL;
Packit 6c4009
  data.dbase = NULL;
Packit 6c4009
  data.func = NULL;
Packit 6c4009
  data.ret = NULL;
Packit 6c4009
Packit 6c4009
  if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  if (data.ret)
Packit 6c4009
    {
Packit 6c4009
      bases->tbase = data.tbase;
Packit 6c4009
      bases->dbase = data.dbase;
Packit 6c4009
      bases->func = data.func;
Packit 6c4009
    }
Packit 6c4009
  return data.ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif