Blame libdw/cie.c

Packit 032894
/* CIE reading.
Packit 032894
   Copyright (C) 2009-2010 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include "cfi.h"
Packit 032894
#include "encoded-value.h"
Packit 032894
#include <assert.h>
Packit 032894
#include <search.h>
Packit 032894
#include <stdlib.h>
Packit 032894
Packit 032894
Packit 032894
static int
Packit 032894
compare_cie (const void *a, const void *b)
Packit 032894
{
Packit 032894
  const struct dwarf_cie *cie1 = a;
Packit 032894
  const struct dwarf_cie *cie2 = b;
Packit 032894
  if (cie1->offset < cie2->offset)
Packit 032894
    return -1;
Packit 032894
  if (cie1->offset > cie2->offset)
Packit 032894
    return 1;
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
/* There is no CIE at OFFSET in the tree.  Add it.  */
Packit 032894
static struct dwarf_cie *
Packit 032894
intern_new_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
Packit 032894
{
Packit 032894
  struct dwarf_cie *cie = malloc (sizeof (struct dwarf_cie));
Packit 032894
  if (cie == NULL)
Packit 032894
    {
Packit 032894
      __libdw_seterrno (DWARF_E_NOMEM);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  cie->offset = offset;
Packit 032894
  cie->code_alignment_factor = info->code_alignment_factor;
Packit 032894
  cie->data_alignment_factor = info->data_alignment_factor;
Packit 032894
  cie->return_address_register = info->return_address_register;
Packit 032894
Packit 032894
  cie->fde_augmentation_data_size = 0;
Packit 032894
  cie->sized_augmentation_data = false;
Packit 032894
  cie->signal_frame = false;
Packit 032894
Packit 032894
  cie->fde_encoding = DW_EH_PE_absptr;
Packit 032894
  cie->lsda_encoding = DW_EH_PE_omit;
Packit 032894
Packit 032894
  /* Grok the augmentation string and its data.  */
Packit 032894
  const uint8_t *data = info->augmentation_data;
Packit 032894
  for (const char *ap = info->augmentation; *ap != '\0'; ++ap)
Packit 032894
    {
Packit 032894
      uint8_t encoding;
Packit 032894
      switch (*ap)
Packit 032894
	{
Packit 032894
	case 'z':
Packit 032894
	  cie->sized_augmentation_data = true;
Packit 032894
	  continue;
Packit 032894
Packit 032894
	case 'S':
Packit 032894
	  cie->signal_frame = true;
Packit 032894
	  continue;
Packit 032894
Packit 032894
	case 'L':		/* LSDA pointer encoding byte.  */
Packit 032894
	  cie->lsda_encoding = *data++;
Packit 032894
	  if (!cie->sized_augmentation_data)
Packit 032894
	    cie->fde_augmentation_data_size
Packit 032894
	      += encoded_value_size (&cache->data->d, cache->e_ident,
Packit 032894
				     cie->lsda_encoding, NULL);
Packit 032894
	  continue;
Packit 032894
Packit 032894
	case 'R':		/* FDE address encoding byte.  */
Packit 032894
	  cie->fde_encoding = *data++;
Packit 032894
	  continue;
Packit 032894
Packit 032894
	case 'P':		/* Skip personality routine.  */
Packit 032894
	  encoding = *data++;
Packit 032894
	  data += encoded_value_size (&cache->data->d, cache->e_ident,
Packit 032894
				      encoding, data);
Packit 032894
	  continue;
Packit 032894
Packit 032894
	default:
Packit 032894
	  /* Unknown augmentation string.  If we have 'z' we can ignore it,
Packit 032894
	     otherwise we must bail out.  */
Packit 032894
	  if (cie->sized_augmentation_data)
Packit 032894
	    continue;
Packit 032894
	}
Packit 032894
      /* We only get here when we need to bail out.  */
Packit 032894
      break;
Packit 032894
    }
Packit 032894
Packit 032894
  if ((cie->fde_encoding & 0x0f) == DW_EH_PE_absptr)
Packit 032894
    {
Packit 032894
      /* Canonicalize encoding to a specific size.  */
Packit 032894
      assert (DW_EH_PE_absptr == 0);
Packit 032894
Packit 032894
      /* XXX should get from dwarf_next_cfi with v4 header.  */
Packit 032894
      uint_fast8_t address_size
Packit 032894
	= cache->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
Packit 032894
      switch (address_size)
Packit 032894
	{
Packit 032894
	case 8:
Packit 032894
	  cie->fde_encoding |= DW_EH_PE_udata8;
Packit 032894
	  break;
Packit 032894
	case 4:
Packit 032894
	  cie->fde_encoding |= DW_EH_PE_udata4;
Packit 032894
	  break;
Packit 032894
	default:
Packit 032894
	  free (cie);
Packit 032894
	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit 032894
	  return NULL;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  /* Save the initial instructions to be played out into initial state.  */
Packit 032894
  cie->initial_instructions = info->initial_instructions;
Packit 032894
  cie->initial_instructions_end = info->initial_instructions_end;
Packit 032894
  cie->initial_state = NULL;
Packit 032894
Packit 032894
  /* Add the new entry to the search tree.  */
Packit 032894
  if (tsearch (cie, &cache->cie_tree, &compare_cie) == NULL)
Packit 032894
    {
Packit 032894
      free (cie);
Packit 032894
      __libdw_seterrno (DWARF_E_NOMEM);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  return cie;
Packit 032894
}
Packit 032894
Packit 032894
/* Look up a CIE_pointer for random access.  */
Packit 032894
struct dwarf_cie *
Packit 032894
internal_function
Packit 032894
__libdw_find_cie (Dwarf_CFI *cache, Dwarf_Off offset)
Packit 032894
{
Packit 032894
  const struct dwarf_cie cie_key = { .offset = offset };
Packit 032894
  struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
Packit 032894
  if (found != NULL)
Packit 032894
    return *found;
Packit 032894
Packit 032894
  /* We have not read this CIE yet.  Go find it.  */
Packit 032894
  Dwarf_Off next_offset = offset;
Packit 032894
  Dwarf_CFI_Entry entry;
Packit 032894
  int result = INTUSE(dwarf_next_cfi) (cache->e_ident,
Packit 032894
				       &cache->data->d, CFI_IS_EH (cache),
Packit 032894
				       offset, &next_offset, &entry);
Packit 032894
  if (result != 0 || entry.cie.CIE_id != DW_CIE_ID_64)
Packit 032894
    {
Packit 032894
      __libdw_seterrno (DWARF_E_INVALID_DWARF);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* If this happened to be what we would have read next, notice it.  */
Packit 032894
  if (cache->next_offset == offset)
Packit 032894
    cache->next_offset = next_offset;
Packit 032894
Packit 032894
  return intern_new_cie (cache, offset, &entry.cie);
Packit 032894
}
Packit 032894
Packit 032894
/* Enter a CIE encountered while reading through for FDEs.  */
Packit 032894
void
Packit 032894
internal_function
Packit 032894
__libdw_intern_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
Packit 032894
{
Packit 032894
  const struct dwarf_cie cie_key = { .offset = offset };
Packit 032894
  struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
Packit 032894
  if (found == NULL)
Packit 032894
    /* We have not read this CIE yet.  Enter it.  */
Packit 032894
    (void) intern_new_cie (cache, offset, info);
Packit 032894
}