Blame libelf/elf32_getphdr.c

Packit 032894
/* Get ELF program header table.
Packit 032894
   Copyright (C) 1998-2010, 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 <errno.h>
Packit 032894
#include <stdbool.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <assert.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
ElfW2(LIBELFBITS,Phdr) *
Packit 032894
__elfw2(LIBELFBITS,getphdr_wrlock) (Elf *elf)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Phdr) *result;
Packit 032894
Packit 032894
  /* If the program header entry has already been filled in the code
Packit 032894
     below must already have been run.  So the class is set, too.  No
Packit 032894
     need to waste any more time here.  */
Packit 032894
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
Packit 032894
  if (likely (result != NULL))
Packit 032894
    return result;
Packit 032894
Packit 032894
  if (elf->class == 0)
Packit 032894
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
Packit 032894
  else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_CLASS);
Packit 032894
      result = NULL;
Packit 032894
      goto out;
Packit 032894
    }
Packit 032894
Packit 032894
  if (likely (result == NULL))
Packit 032894
    {
Packit 032894
      /* Read the section header table.  */
Packit 032894
      ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
Packit 032894
Packit 032894
      /* If no program header exists return NULL.  */
Packit 032894
      size_t phnum;
Packit 032894
      if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
Packit 032894
	goto out;
Packit 032894
      if (phnum == 0 || ehdr->e_phoff == 0)
Packit 032894
	{
Packit 032894
	  __libelf_seterrno (ELF_E_NO_PHDR);
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Check this doesn't overflow.  */
Packit 032894
      size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
Packit 032894
Packit 032894
      if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))
Packit 032894
	  || ehdr->e_phoff > elf->maximum_size
Packit 032894
	  || elf->maximum_size - ehdr->e_phoff < size)
Packit 032894
	{
Packit 032894
	  __libelf_seterrno (ELF_E_INVALID_DATA);
Packit 032894
	  goto out;
Packit 032894
	}
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_phoff >= elf->maximum_size)
Packit 032894
	      || unlikely (elf->maximum_size - ehdr->e_phoff < size))
Packit 032894
	    {
Packit 032894
	      /* Something is wrong.  */
Packit 032894
	      __libelf_seterrno (ELF_E_INVALID_PHDR);
Packit 032894
	      goto out;
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* All the data is already mapped.  Use it.  */
Packit 032894
	  void *file_phdr = ((char *) elf->map_address
Packit 032894
			     + elf->start_offset + ehdr->e_phoff);
Packit 032894
	  if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
Packit 032894
	      && (ALLOW_UNALIGNED
Packit 032894
		  || ((uintptr_t) file_phdr
Packit 032894
		      & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
Packit 032894
	    /* Simply use the mapped data.  */
Packit 032894
	    elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      ElfW2(LIBELFBITS,Phdr) *notcvt;
Packit 032894
	      ElfW2(LIBELFBITS,Phdr) *phdr;
Packit 032894
Packit 032894
	      /* Allocate memory for the program headers.  We know the number
Packit 032894
		 of entries from the ELF header.  */
Packit 032894
	      phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
Packit 032894
		(ElfW2(LIBELFBITS,Phdr) *) malloc (size);
Packit 032894
	      if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
Packit 032894
		{
Packit 032894
		  __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
		  goto out;
Packit 032894
		}
Packit 032894
	      elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
Packit 032894
		ELF_F_MALLOCED | ELF_F_DIRTY;
Packit 032894
Packit 032894
	      /* Now copy the data and at the same time convert the
Packit 032894
		 byte order.  */
Packit 032894
Packit 032894
	      if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
Packit 032894
		{
Packit 032894
		  assert (! ALLOW_UNALIGNED);
Packit 032894
		  memcpy (phdr, file_phdr, size);
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  bool copy = ! (ALLOW_UNALIGNED
Packit 032894
				 || ((uintptr_t) file_phdr
Packit 032894
				     & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
Packit 032894
					- 1)) == 0);
Packit 032894
		  if (! copy)
Packit 032894
		    notcvt = file_phdr;
Packit 032894
		  else
Packit 032894
		    {
Packit 032894
		      notcvt = (ElfW2(LIBELFBITS,Phdr) *) 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, file_phdr, size);
Packit 032894
		    }
Packit 032894
Packit 032894
		  for (size_t cnt = 0; cnt < phnum; ++cnt)
Packit 032894
		    {
Packit 032894
		      CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
Packit 032894
		      CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
Packit 032894
		    }
Packit 032894
Packit 032894
		  if (copy)
Packit 032894
		    free (notcvt);
Packit 032894
		}
Packit 032894
	    }
Packit 032894
	}
Packit 032894
      else if (likely (elf->fildes != -1))
Packit 032894
	{
Packit 032894
	  /* Allocate memory for the program headers.  We know the number
Packit 032894
	     of entries from the ELF header.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr =
Packit 032894
	    (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
Packit 032894
	  if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
Packit 032894
	    {
Packit 032894
	      __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
	      goto out;
Packit 032894
	    }
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
Packit 032894
Packit 032894
	  /* Read the header.  */
Packit 032894
	  ssize_t n = pread_retry (elf->fildes,
Packit 032894
				   elf->state.ELFW(elf,LIBELFBITS).phdr, size,
Packit 032894
				   elf->start_offset + ehdr->e_phoff);
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
	      free (elf->state.ELFW(elf,LIBELFBITS).phdr);
Packit 032894
	      elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
Packit 032894
	      goto 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
	    {
Packit 032894
	      ElfW2(LIBELFBITS,Phdr) *phdr
Packit 032894
		= elf->state.ELFW(elf,LIBELFBITS).phdr;
Packit 032894
Packit 032894
	      for (size_t cnt = 0; cnt < phnum; ++cnt)
Packit 032894
		{
Packit 032894
		  CONVERT (phdr[cnt].p_type);
Packit 032894
		  CONVERT (phdr[cnt].p_offset);
Packit 032894
		  CONVERT (phdr[cnt].p_vaddr);
Packit 032894
		  CONVERT (phdr[cnt].p_paddr);
Packit 032894
		  CONVERT (phdr[cnt].p_filesz);
Packit 032894
		  CONVERT (phdr[cnt].p_memsz);
Packit 032894
		  CONVERT (phdr[cnt].p_flags);
Packit 032894
		  CONVERT (phdr[cnt].p_align);
Packit 032894
		}
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.  */
Packit 032894
	  __libelf_seterrno (ELF_E_FD_DISABLED);
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
Packit 032894
    }
Packit 032894
Packit 032894
 out:
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
ElfW2(LIBELFBITS,Phdr) *
Packit 032894
elfw2(LIBELFBITS,getphdr) (Elf *elf)
Packit 032894
{
Packit 032894
  ElfW2(LIBELFBITS,Phdr) *result;
Packit 032894
Packit 032894
  if (elf == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  if (unlikely (elf->kind != ELF_K_ELF))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* If the program header entry has already been filled in the code
Packit 032894
   * in getphdr_wrlock must already have been run.  So the class is
Packit 032894
   * set, too.  No need to waste any more time here.  */
Packit 032894
  result = elf->state.ELFW(elf,LIBELFBITS).phdr;
Packit 032894
  if (likely (result != NULL))
Packit 032894
    return result;
Packit 032894
Packit 032894
  rwlock_wrlock (elf->lock);
Packit 032894
  result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
INTDEF(elfw2(LIBELFBITS,getphdr))