Blame libelf/elf32_newphdr.c

Packit 032894
/* Create new ELF program header table.
Packit 032894
   Copyright (C) 1999-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 <assert.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
Packit 032894
#include "libelfP.h"
Packit 032894
Packit 032894
#ifndef LIBELFBITS
Packit 032894
# define LIBELFBITS 32
Packit 032894
#endif
Packit 032894
Packit 032894
Packit 032894
ElfW2(LIBELFBITS,Phdr) *
Packit 032894
elfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
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
  /* This check is correct, it is for sh_info, which is either
Packit 032894
     Elf32_Word or Elf64_Word, both being 32 bits.  But count is size_t
Packit 032894
     so might not fit on 32bit ELF files.  */
Packit 032894
  if (unlikely ((ElfW2(LIBELFBITS,Word)) count != count))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_OPERAND);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_wrlock (elf->lock);
Packit 032894
Packit 032894
  if (elf->class == 0)
Packit 032894
    elf->class = ELFW(ELFCLASS,LIBELFBITS);
Packit 032894
  else if (unlikely (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 (unlikely (elf->state.ELFW(elf,LIBELFBITS).ehdr == NULL))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
Packit 032894
      result = NULL;
Packit 032894
      goto out;
Packit 032894
    }
Packit 032894
Packit 032894
  /* A COUNT of zero means remove existing table.  */
Packit 032894
  if (count == 0)
Packit 032894
    {
Packit 032894
      /* Free the old program header.  */
Packit 032894
      if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
Packit 032894
	{
Packit 032894
	  if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
Packit 032894
	    free (elf->state.ELFW(elf,LIBELFBITS).phdr);
Packit 032894
Packit 032894
	  /* Set the pointer to NULL.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
Packit 032894
	  /* Set the `e_phnum' member to the new value.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = 0;
Packit 032894
	  /* Also clear any old PN_XNUM extended value.  */
Packit 032894
	  if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0)
Packit 032894
	    elf->state.ELFW(elf,LIBELFBITS).scns.data[0]
Packit 032894
	      .shdr.ELFW(e,LIBELFBITS)->sh_info = 0;
Packit 032894
	  /* Also set the size.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
Packit 032894
	    sizeof (ElfW2(LIBELFBITS,Phdr));
Packit 032894
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
Packit 032894
	  elf->flags |= ELF_F_DIRTY;
Packit 032894
	  __libelf_seterrno (ELF_E_NOERROR);
Packit 032894
	}
Packit 032894
Packit 032894
      result = NULL;
Packit 032894
    }
Packit 032894
  else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
Packit 032894
	   || count == PN_XNUM
Packit 032894
	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
Packit 032894
    {
Packit 032894
      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
Packit 032894
	{
Packit 032894
	  __libelf_seterrno (ELF_E_INVALID_INDEX);
Packit 032894
	  result = NULL;
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
Packit 032894
      if (unlikely (count >= PN_XNUM && scn0->shdr.ELFW(e,LIBELFBITS) == NULL))
Packit 032894
	{
Packit 032894
	  /* Something is wrong with section zero, but we need it to write
Packit 032894
	     the extended phdr count.  */
Packit 032894
	  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
	  result = NULL;
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Allocate a new program header with the appropriate number of
Packit 032894
	 elements.  */
Packit 032894
      result = (ElfW2(LIBELFBITS,Phdr) *)
Packit 032894
	realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
Packit 032894
		 count * sizeof (ElfW2(LIBELFBITS,Phdr)));
Packit 032894
      if (result == NULL)
Packit 032894
	__libelf_seterrno (ELF_E_NOMEM);
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* Now set the result.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr = result;
Packit 032894
	  if (count >= PN_XNUM)
Packit 032894
	    {
Packit 032894
	      /* We have to write COUNT into the zeroth section's sh_info.  */
Packit 032894
	      if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt == 0)
Packit 032894
		{
Packit 032894
		  assert (elf->state.ELFW(elf,LIBELFBITS).scns.max > 0);
Packit 032894
		  elf->state.ELFW(elf,LIBELFBITS).scns.cnt = 1;
Packit 032894
		}
Packit 032894
	      scn0->shdr.ELFW(e,LIBELFBITS)->sh_info = count;
Packit 032894
	      scn0->shdr_flags |= ELF_F_DIRTY;
Packit 032894
	      elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = PN_XNUM;
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    /* Set the `e_phnum' member to the new value.  */
Packit 032894
	    elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = count;
Packit 032894
	  /* Clear the whole memory.  */
Packit 032894
	  memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
Packit 032894
	  /* Also set the size.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
Packit 032894
	    elf_typesize (LIBELFBITS, ELF_T_PHDR, 1);
Packit 032894
	  /* Remember we allocated the array and mark the structure is
Packit 032894
	     modified.  */
Packit 032894
	  elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
Packit 032894
	    ELF_F_DIRTY | ELF_F_MALLOCED;
Packit 032894
	  /* We have to rewrite the entire file if the size of the
Packit 032894
	     program header is changed.  */
Packit 032894
	  elf->flags |= ELF_F_DIRTY;
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      /* We have the same number of entries.  Just clear the array.  */
Packit 032894
      assert (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize
Packit 032894
	      == elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
Packit 032894
Packit 032894
      /* Mark the structure as modified.  */
Packit 032894
      elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
Packit 032894
Packit 032894
      result = elf->state.ELFW(elf,LIBELFBITS).phdr;
Packit 032894
      memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
Packit 032894
    }
Packit 032894
Packit 032894
 out:
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
INTDEF(elfw2(LIBELFBITS,newphdr))