Blame libdwfl/derelocate.c

Packit 032894
/* Recover relocatibility for addresses computed from debug information.
Packit 032894
   Copyright (C) 2005-2010, 2013, 2015 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 "libdwflP.h"
Packit 032894
Packit 032894
struct dwfl_relocation
Packit 032894
{
Packit 032894
  size_t count;
Packit 032894
  struct
Packit 032894
  {
Packit 032894
    Elf_Scn *scn;
Packit 032894
    Elf_Scn *relocs;
Packit 032894
    const char *name;
Packit 032894
    GElf_Addr start, end;
Packit 032894
  } refs[0];
Packit 032894
};
Packit 032894
Packit 032894
Packit 032894
struct secref
Packit 032894
{
Packit 032894
  struct secref *next;
Packit 032894
  Elf_Scn *scn;
Packit 032894
  Elf_Scn *relocs;
Packit 032894
  const char *name;
Packit 032894
  GElf_Addr start, end;
Packit 032894
};
Packit 032894
Packit 032894
static int
Packit 032894
compare_secrefs (const void *a, const void *b)
Packit 032894
{
Packit 032894
  struct secref *const *p1 = a;
Packit 032894
  struct secref *const *p2 = b;
Packit 032894
Packit 032894
  /* No signed difference calculation is correct here, since the
Packit 032894
     terms are unsigned and could be more than INT64_MAX apart.  */
Packit 032894
  if ((*p1)->start < (*p2)->start)
Packit 032894
    return -1;
Packit 032894
  if ((*p1)->start > (*p2)->start)
Packit 032894
    return 1;
Packit 032894
Packit 032894
  if ((*p1)->end < (*p2)->end)
Packit 032894
    return -1;
Packit 032894
  if ((*p1)->end > (*p2)->end)
Packit 032894
    return 1;
Packit 032894
Packit 032894
  /* Same start/end, then just compare which section came first.  */
Packit 032894
  return elf_ndxscn ((*p1)->scn) - elf_ndxscn ((*p2)->scn);
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
cache_sections (Dwfl_Module *mod)
Packit 032894
{
Packit 032894
  if (likely (mod->reloc_info != NULL))
Packit 032894
    return mod->reloc_info->count;
Packit 032894
Packit 032894
  struct secref *refs = NULL;
Packit 032894
  size_t nrefs = 0;
Packit 032894
Packit 032894
  size_t shstrndx;
Packit 032894
  if (unlikely (elf_getshdrstrndx (mod->main.elf, &shstrndx) < 0))
Packit 032894
    {
Packit 032894
    elf_error:
Packit 032894
      __libdwfl_seterrno (DWFL_E_LIBELF);
Packit 032894
      nrefs = -1;
Packit 032894
      goto free_refs;
Packit 032894
    }
Packit 032894
Packit 032894
  bool check_reloc_sections = false;
Packit 032894
  Elf_Scn *scn = NULL;
Packit 032894
  while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
Packit 032894
    {
Packit 032894
      GElf_Shdr shdr_mem;
Packit 032894
      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
      if (shdr == NULL)
Packit 032894
	goto elf_error;
Packit 032894
Packit 032894
      if ((shdr->sh_flags & SHF_ALLOC) && shdr->sh_addr == 0
Packit 032894
	  && mod->e_type == ET_REL)
Packit 032894
	{
Packit 032894
	  /* This section might not yet have been looked at.  */
Packit 032894
	  if (__libdwfl_relocate_value (mod, mod->main.elf, &shstrndx,
Packit 032894
					elf_ndxscn (scn),
Packit 032894
					&shdr->sh_addr) != DWFL_E_NOERROR)
Packit 032894
	    continue;
Packit 032894
	  shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
	  if (unlikely (shdr == NULL))
Packit 032894
	    goto elf_error;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_flags & SHF_ALLOC)
Packit 032894
	{
Packit 032894
	  const char *name = elf_strptr (mod->main.elf, shstrndx,
Packit 032894
					 shdr->sh_name);
Packit 032894
	  if (unlikely (name == NULL))
Packit 032894
	    goto elf_error;
Packit 032894
Packit 032894
	  struct secref *newref = malloc (sizeof *newref);
Packit 032894
	  if (unlikely (newref == NULL))
Packit 032894
	    {
Packit 032894
	    nomem:
Packit 032894
	      __libdwfl_seterrno (DWFL_E_NOMEM);
Packit 032894
	      nrefs = -1;
Packit 032894
	      goto free_refs;
Packit 032894
	    }
Packit 032894
Packit 032894
	  newref->scn = scn;
Packit 032894
	  newref->relocs = NULL;
Packit 032894
	  newref->name = name;
Packit 032894
	  newref->start = dwfl_adjusted_address (mod, shdr->sh_addr);
Packit 032894
	  newref->end = newref->start + shdr->sh_size;
Packit 032894
	  newref->next = refs;
Packit 032894
	  refs = newref;
Packit 032894
	  ++nrefs;
Packit 032894
	}
Packit 032894
Packit 032894
      if (mod->e_type == ET_REL
Packit 032894
	  && shdr->sh_size != 0
Packit 032894
	  && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
Packit 032894
	  && mod->dwfl->callbacks->section_address != NULL)
Packit 032894
	{
Packit 032894
	  if (shdr->sh_info < elf_ndxscn (scn))
Packit 032894
	    {
Packit 032894
	      /* We've already looked at the section these relocs apply to.  */
Packit 032894
	      Elf_Scn *tscn = elf_getscn (mod->main.elf, shdr->sh_info);
Packit 032894
	      if (likely (tscn != NULL))
Packit 032894
		for (struct secref *sec = refs; sec != NULL; sec = sec->next)
Packit 032894
		  if (sec->scn == tscn)
Packit 032894
		    {
Packit 032894
		      sec->relocs = scn;
Packit 032894
		      break;
Packit 032894
		    }
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    /* We'll have to do a second pass.  */
Packit 032894
	    check_reloc_sections = true;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  mod->reloc_info = malloc (offsetof (struct dwfl_relocation, refs[nrefs]));
Packit 032894
  if (unlikely (mod->reloc_info == NULL))
Packit 032894
    goto nomem;
Packit 032894
Packit 032894
  struct secref **sortrefs = malloc (nrefs * sizeof sortrefs[0]);
Packit 032894
  if (unlikely (sortrefs == NULL))
Packit 032894
    goto nomem;
Packit 032894
Packit 032894
  for (size_t i = nrefs; i-- > 0; refs = refs->next)
Packit 032894
    sortrefs[i] = refs;
Packit 032894
  assert (refs == NULL);
Packit 032894
Packit 032894
  qsort (sortrefs, nrefs, sizeof sortrefs[0], &compare_secrefs);
Packit 032894
Packit 032894
  mod->reloc_info->count = nrefs;
Packit 032894
  for (size_t i = 0; i < nrefs; ++i)
Packit 032894
    {
Packit 032894
      mod->reloc_info->refs[i].name = sortrefs[i]->name;
Packit 032894
      mod->reloc_info->refs[i].scn = sortrefs[i]->scn;
Packit 032894
      mod->reloc_info->refs[i].relocs = sortrefs[i]->relocs;
Packit 032894
      mod->reloc_info->refs[i].start = sortrefs[i]->start;
Packit 032894
      mod->reloc_info->refs[i].end = sortrefs[i]->end;
Packit 032894
      free (sortrefs[i]);
Packit 032894
    }
Packit 032894
Packit 032894
  free (sortrefs);
Packit 032894
Packit 032894
  if (unlikely (check_reloc_sections))
Packit 032894
    {
Packit 032894
      /* There was a reloc section that preceded its target section.
Packit 032894
	 So we have to scan again now that we have cached all the
Packit 032894
	 possible target sections we care about.  */
Packit 032894
Packit 032894
      scn = NULL;
Packit 032894
      while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
Packit 032894
	{
Packit 032894
	  GElf_Shdr shdr_mem;
Packit 032894
	  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
	  if (shdr == NULL)
Packit 032894
	    goto elf_error;
Packit 032894
Packit 032894
      	  if (shdr->sh_size != 0
Packit 032894
	      && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA))
Packit 032894
	    {
Packit 032894
	      Elf_Scn *tscn = elf_getscn (mod->main.elf, shdr->sh_info);
Packit 032894
	      if (likely (tscn != NULL))
Packit 032894
		for (size_t i = 0; i < nrefs; ++i)
Packit 032894
		  if (mod->reloc_info->refs[i].scn == tscn)
Packit 032894
		    {
Packit 032894
		      mod->reloc_info->refs[i].relocs = scn;
Packit 032894
		      break;
Packit 032894
		    }
Packit 032894
	    }
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
free_refs:
Packit 032894
  while (refs != NULL)
Packit 032894
    {
Packit 032894
      struct secref *ref = refs;
Packit 032894
      refs = ref->next;
Packit 032894
      free (ref);
Packit 032894
    }
Packit 032894
Packit 032894
  return nrefs;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
dwfl_module_relocations (Dwfl_Module *mod)
Packit 032894
{
Packit 032894
  if (mod == NULL)
Packit 032894
    return -1;
Packit 032894
Packit 032894
  switch (mod->e_type)
Packit 032894
    {
Packit 032894
    case ET_REL:
Packit 032894
      return cache_sections (mod);
Packit 032894
Packit 032894
    case ET_DYN:
Packit 032894
      return 1;
Packit 032894
Packit 032894
    case ET_EXEC:
Packit 032894
      assert (mod->main.vaddr == mod->low_addr);
Packit 032894
      break;
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
const char *
Packit 032894
dwfl_module_relocation_info (Dwfl_Module *mod, unsigned int idx,
Packit 032894
			     Elf32_Word *shndxp)
Packit 032894
{
Packit 032894
  if (mod == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  switch (mod->e_type)
Packit 032894
    {
Packit 032894
    case ET_REL:
Packit 032894
      break;
Packit 032894
Packit 032894
    case ET_DYN:
Packit 032894
      if (idx != 0)
Packit 032894
	return NULL;
Packit 032894
      if (shndxp)
Packit 032894
	*shndxp = SHN_ABS;
Packit 032894
      return "";
Packit 032894
Packit 032894
    default:
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  if (cache_sections (mod) < 0)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  struct dwfl_relocation *sections = mod->reloc_info;
Packit 032894
Packit 032894
  if (idx >= sections->count)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  if (shndxp)
Packit 032894
    *shndxp = elf_ndxscn (sections->refs[idx].scn);
Packit 032894
Packit 032894
  return sections->refs[idx].name;
Packit 032894
}
Packit 032894
Packit 032894
/* Check that MOD is valid and make sure its relocation has been done.  */
Packit 032894
static bool
Packit 032894
check_module (Dwfl_Module *mod)
Packit 032894
{
Packit 032894
  if (mod == NULL)
Packit 032894
    return true;
Packit 032894
Packit 032894
  if (INTUSE(dwfl_module_getsymtab) (mod) < 0)
Packit 032894
    {
Packit 032894
      Dwfl_Error error = dwfl_errno ();
Packit 032894
      if (error != DWFL_E_NO_SYMTAB)
Packit 032894
	{
Packit 032894
	  __libdwfl_seterrno (error);
Packit 032894
	  return true;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  if (mod->dw == NULL)
Packit 032894
    {
Packit 032894
      Dwarf_Addr bias;
Packit 032894
      if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
Packit 032894
	{
Packit 032894
	  Dwfl_Error error = dwfl_errno ();
Packit 032894
	  if (error != DWFL_E_NO_DWARF)
Packit 032894
	    {
Packit 032894
	      __libdwfl_seterrno (error);
Packit 032894
	      return true;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  return false;
Packit 032894
}
Packit 032894
Packit 032894
/* Find the index in MOD->reloc_info.refs containing *ADDR.  */
Packit 032894
static int
Packit 032894
find_section (Dwfl_Module *mod, Dwarf_Addr *addr)
Packit 032894
{
Packit 032894
  if (cache_sections (mod) < 0)
Packit 032894
    return -1;
Packit 032894
Packit 032894
  struct dwfl_relocation *sections = mod->reloc_info;
Packit 032894
Packit 032894
  /* The sections are sorted by address, so we can use binary search.  */
Packit 032894
  size_t l = 0, u = sections->count;
Packit 032894
  while (l < u)
Packit 032894
    {
Packit 032894
      size_t idx = (l + u) / 2;
Packit 032894
      if (*addr < sections->refs[idx].start)
Packit 032894
	u = idx;
Packit 032894
      else if (*addr > sections->refs[idx].end)
Packit 032894
	l = idx + 1;
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* Consider the limit of a section to be inside it, unless it's
Packit 032894
	     inside the next one.  A section limit address can appear in
Packit 032894
	     line records.  */
Packit 032894
	  if (*addr == sections->refs[idx].end
Packit 032894
	      && idx + 1 < sections->count
Packit 032894
	      && *addr == sections->refs[idx + 1].start)
Packit 032894
	    ++idx;
Packit 032894
Packit 032894
	  *addr -= sections->refs[idx].start;
Packit 032894
	  return idx;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  __libdwfl_seterrno (DWFL_E (LIBDW, DWARF_E_NO_MATCH));
Packit 032894
  return -1;
Packit 032894
}
Packit 032894
Packit 032894
size_t
Packit 032894
internal_function
Packit 032894
__libdwfl_find_section_ndx (Dwfl_Module *mod, Dwarf_Addr *addr)
Packit 032894
{
Packit 032894
  int idx = find_section (mod, addr);
Packit 032894
  if (unlikely (idx == -1))
Packit 032894
    return SHN_UNDEF;
Packit 032894
Packit 032894
  return elf_ndxscn (mod->reloc_info->refs[idx].scn);
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
dwfl_module_relocate_address (Dwfl_Module *mod, Dwarf_Addr *addr)
Packit 032894
{
Packit 032894
  if (unlikely (check_module (mod)))
Packit 032894
    return -1;
Packit 032894
Packit 032894
  switch (mod->e_type)
Packit 032894
    {
Packit 032894
    case ET_REL:
Packit 032894
      return find_section (mod, addr);
Packit 032894
Packit 032894
    case ET_DYN:
Packit 032894
      /* All relative to first and only relocation base: module start.  */
Packit 032894
      *addr -= mod->low_addr;
Packit 032894
      break;
Packit 032894
Packit 032894
    default:
Packit 032894
      /* Already absolute, dwfl_module_relocations returned zero.  We
Packit 032894
	 shouldn't really have been called, but it's a harmless no-op.  */
Packit 032894
      break;
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
INTDEF (dwfl_module_relocate_address)
Packit 032894
Packit 032894
Elf_Scn *
Packit 032894
dwfl_module_address_section (Dwfl_Module *mod, Dwarf_Addr *address,
Packit 032894
			     Dwarf_Addr *bias)
Packit 032894
{
Packit 032894
  if (check_module (mod))
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  int idx = find_section (mod, address);
Packit 032894
  if (idx < 0)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  if (mod->reloc_info->refs[idx].relocs != NULL)
Packit 032894
    {
Packit 032894
      assert (mod->e_type == ET_REL);
Packit 032894
Packit 032894
      Elf_Scn *tscn = mod->reloc_info->refs[idx].scn;
Packit 032894
      Elf_Scn *relocscn = mod->reloc_info->refs[idx].relocs;
Packit 032894
      Dwfl_Error result = __libdwfl_relocate_section (mod, mod->main.elf,
Packit 032894
						      relocscn, tscn, true);
Packit 032894
      if (likely (result == DWFL_E_NOERROR))
Packit 032894
	mod->reloc_info->refs[idx].relocs = NULL;
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  __libdwfl_seterrno (result);
Packit 032894
	  return NULL;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  *bias = dwfl_adjusted_address (mod, 0);
Packit 032894
  return mod->reloc_info->refs[idx].scn;
Packit 032894
}
Packit 032894
INTDEF (dwfl_module_address_section)