Blame libelf/elf32_getshdr.c

Packit 032894
/* Return section header.
Packit 032894
   Copyright (C) 1998-2002, 2005, 2007, 2009, 2012, 2014, 2015 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
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 <assert.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <stdbool.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include <system.h>
Packit 032894
#include "libelfP.h"
Packit 032894
#include "common.h"
Packit 032894
Packit 032894
#ifndef LIBELFBITS
Packit 032894
# define LIBELFBITS 32
Packit 032894
#endif
Packit 032894
Packit 032894
Packit 032894
static ElfW2(LIBELFBITS,Shdr) *
Packit 032894
load_shdr_wrlock (Elf_Scn *scn)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Shdr) *result;
Packit 032894
Packit 032894
  /* Read the section header table.  */
Packit 032894
  Elf *elf = scn->elf;
Packit 032894
  ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
Packit 032894
Packit 032894
  /* Try again, maybe the data is there now.  */
Packit 032894
  result = scn->shdr.ELFW(e,LIBELFBITS);
Packit 032894
  if (result != NULL)
Packit 032894
    goto out;
Packit 032894
Packit 032894
  size_t shnum;
Packit 032894
  if (__elf_getshdrnum_rdlock (elf, &shnum) != 0
Packit 032894
      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
Packit 032894
    goto out;
Packit 032894
  size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
Packit 032894
Packit 032894
  /* Allocate memory for the section headers.  We know the number
Packit 032894
     of entries from the ELF header.  */
Packit 032894
  ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
Packit 032894
    (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
Packit 032894
  if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
      goto out;
Packit 032894
    }
Packit 032894
  elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
Packit 032894
Packit 032894
  if (elf->map_address != NULL)
Packit 032894
    {
Packit 032894
      /* First see whether the information in the ELF header is
Packit 032894
	 valid and it does not ask for too much.  */
Packit 032894
      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
Packit 032894
	  || unlikely (elf->maximum_size - ehdr->e_shoff < size))
Packit 032894
	{
Packit 032894
	  /* Something is wrong.  */
Packit 032894
	  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
	  goto free_and_out;
Packit 032894
	}
Packit 032894
Packit 032894
      ElfW2(LIBELFBITS,Shdr) *notcvt;
Packit 032894
Packit 032894
      /* All the data is already mapped.  If we could use it
Packit 032894
	 directly this would already have happened.  Unless
Packit 032894
	 we allocated the memory ourselves and the ELF_F_MALLOCED
Packit 032894
	 flag is set.  */
Packit 032894
      void *file_shdr = ((char *) elf->map_address
Packit 032894
			 + elf->start_offset + ehdr->e_shoff);
Packit 032894
Packit 032894
      assert ((elf->flags & ELF_F_MALLOCED)
Packit 032894
	      || ehdr->e_ident[EI_DATA] != MY_ELFDATA
Packit 032894
	      || elf->cmd == ELF_C_READ_MMAP
Packit 032894
	      || (! ALLOW_UNALIGNED
Packit 032894
		  && ((uintptr_t) file_shdr
Packit 032894
		      & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
Packit 032894
Packit 032894
      /* Now copy the data and at the same time convert the byte order.  */
Packit 032894
      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
Packit 032894
	{
Packit 032894
	  assert ((elf->flags & ELF_F_MALLOCED)
Packit 032894
		  || elf->cmd == ELF_C_READ_MMAP
Packit 032894
		  || ! ALLOW_UNALIGNED);
Packit 032894
	  memcpy (shdr, file_shdr, size);
Packit 032894
	}
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  bool copy = ! (ALLOW_UNALIGNED
Packit 032894
			 || ((uintptr_t) file_shdr
Packit 032894
			     & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1))
Packit 032894
			     == 0);
Packit 032894
	  if (! copy)
Packit 032894
	    notcvt = (ElfW2(LIBELFBITS,Shdr) *)
Packit 032894
	      ((char *) elf->map_address
Packit 032894
	       + elf->start_offset + ehdr->e_shoff);
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
Packit 032894
	      if (unlikely (notcvt == NULL))
Packit 032894
		{
Packit 032894
		  __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
		  goto out;
Packit 032894
		}
Packit 032894
	      memcpy (notcvt, ((char *) elf->map_address
Packit 032894
			       + elf->start_offset + ehdr->e_shoff),
Packit 032894
		      size);
Packit 032894
	    }
Packit 032894
Packit 032894
	  for (size_t cnt = 0; cnt < shnum; ++cnt)
Packit 032894
	    {
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_addralign,
Packit 032894
			  notcvt[cnt].sh_addralign);
Packit 032894
	      CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
Packit 032894
Packit 032894
	      /* If this is a section with an extended index add a
Packit 032894
		 reference in the section which uses the extended
Packit 032894
		 index.  */
Packit 032894
	      if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
Packit 032894
		  && shdr[cnt].sh_link < shnum)
Packit 032894
		elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
Packit 032894
		  = cnt;
Packit 032894
Packit 032894
	      /* Set the own shndx_index field in case it has not yet
Packit 032894
		 been set.  */
Packit 032894
	      if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
Packit 032894
		elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
Packit 032894
		  = -1;
Packit 032894
	    }
Packit 032894
Packit 032894
	  if (copy)
Packit 032894
	    free (notcvt);
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else if (likely (elf->fildes != -1))
Packit 032894
    {
Packit 032894
      /* Read the header.  */
Packit 032894
      ssize_t n = pread_retry (elf->fildes,
Packit 032894
			       elf->state.ELFW(elf,LIBELFBITS).shdr, size,
Packit 032894
			       elf->start_offset + ehdr->e_shoff);
Packit 032894
      if (unlikely ((size_t) n != size))
Packit 032894
	{
Packit 032894
	  /* Severe problems.  We cannot read the data.  */
Packit 032894
	  __libelf_seterrno (ELF_E_READ_ERROR);
Packit 032894
	  goto free_and_out;
Packit 032894
	}
Packit 032894
Packit 032894
      /* If the byte order of the file is not the same as the one
Packit 032894
	 of the host convert the data now.  */
Packit 032894
      if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
Packit 032894
	for (size_t cnt = 0; cnt < shnum; ++cnt)
Packit 032894
	  {
Packit 032894
	    CONVERT (shdr[cnt].sh_name);
Packit 032894
	    CONVERT (shdr[cnt].sh_type);
Packit 032894
	    CONVERT (shdr[cnt].sh_flags);
Packit 032894
	    CONVERT (shdr[cnt].sh_addr);
Packit 032894
	    CONVERT (shdr[cnt].sh_offset);
Packit 032894
	    CONVERT (shdr[cnt].sh_size);
Packit 032894
	    CONVERT (shdr[cnt].sh_link);
Packit 032894
	    CONVERT (shdr[cnt].sh_info);
Packit 032894
	    CONVERT (shdr[cnt].sh_addralign);
Packit 032894
	    CONVERT (shdr[cnt].sh_entsize);
Packit 032894
	  }
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      /* The file descriptor was already enabled and not all data was
Packit 032894
	 read.  Undo the allocation.  */
Packit 032894
      __libelf_seterrno (ELF_E_FD_DISABLED);
Packit 032894
Packit 032894
    free_and_out:
Packit 032894
      free (shdr);
Packit 032894
      elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
Packit 032894
      elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
Packit 032894
Packit 032894
      goto out;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Set the pointers in the `scn's.  */
Packit 032894
  for (size_t cnt = 0; cnt < shnum; ++cnt)
Packit 032894
    elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
Packit 032894
      = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
Packit 032894
Packit 032894
  result = scn->shdr.ELFW(e,LIBELFBITS);
Packit 032894
  assert (result != NULL);
Packit 032894
Packit 032894
out:
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
static bool
Packit 032894
scn_valid (Elf_Scn *scn)
Packit 032894
{
Packit 032894
  if (scn == NULL)
Packit 032894
    return false;
Packit 032894
Packit 032894
  if (unlikely (scn->elf->state.elf.ehdr == NULL))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
Packit 032894
      return false;
Packit 032894
    }
Packit 032894
Packit 032894
  if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_CLASS);
Packit 032894
      return false;
Packit 032894
    }
Packit 032894
Packit 032894
  return true;
Packit 032894
}
Packit 032894
Packit 032894
ElfW2(LIBELFBITS,Shdr) *
Packit 032894
internal_function
Packit 032894
__elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Shdr) *result;
Packit 032894
Packit 032894
  if (!scn_valid (scn))
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  result = scn->shdr.ELFW(e,LIBELFBITS);
Packit 032894
  if (result == NULL)
Packit 032894
    {
Packit 032894
      rwlock_unlock (scn->elf->lock);
Packit 032894
      rwlock_wrlock (scn->elf->lock);
Packit 032894
      result = scn->shdr.ELFW(e,LIBELFBITS);
Packit 032894
      if (result == NULL)
Packit 032894
	result = load_shdr_wrlock (scn);
Packit 032894
    }
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
ElfW2(LIBELFBITS,Shdr) *
Packit 032894
internal_function
Packit 032894
__elfw2(LIBELFBITS,getshdr_wrlock) (Elf_Scn *scn)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Shdr) *result;
Packit 032894
Packit 032894
  if (!scn_valid (scn))
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  result = scn->shdr.ELFW(e,LIBELFBITS);
Packit 032894
  if (result == NULL)
Packit 032894
    result = load_shdr_wrlock (scn);
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
ElfW2(LIBELFBITS,Shdr) *
Packit 032894
elfw2(LIBELFBITS,getshdr) (Elf_Scn *scn)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Shdr) *result;
Packit 032894
Packit 032894
  if (!scn_valid (scn))
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  rwlock_rdlock (scn->elf->lock);
Packit 032894
  result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
Packit 032894
  rwlock_unlock (scn->elf->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}