Blame libelf/elf_readall.c

Packit 032894
/* Read all of the file associated with the descriptor.
Packit 032894
   Copyright (C) 1998-2009, 2015 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Contributed 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 <unistd.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
Packit 032894
#include <system.h>
Packit 032894
#include "libelfP.h"
Packit 032894
#include "common.h"
Packit 032894
Packit 032894
Packit 032894
static void
Packit 032894
set_address (Elf *elf, size_t offset)
Packit 032894
{
Packit 032894
  if (elf->kind == ELF_K_AR)
Packit 032894
    {
Packit 032894
      Elf *child = elf->state.ar.children;
Packit 032894
Packit 032894
      while (child != NULL)
Packit 032894
	{
Packit 032894
	  if (child->map_address == NULL)
Packit 032894
	    {
Packit 032894
	      child->map_address = elf->map_address;
Packit 032894
	      child->start_offset -= offset;
Packit 032894
	      if (child->kind == ELF_K_AR)
Packit 032894
		child->state.ar.offset -= offset;
Packit 032894
Packit 032894
	      set_address (child, offset);
Packit 032894
	    }
Packit 032894
Packit 032894
	  child = child->next;
Packit 032894
	}
Packit 032894
    }
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
char *
Packit 032894
internal_function
Packit 032894
__libelf_readall (Elf *elf)
Packit 032894
{
Packit 032894
  /* Get the file.  */
Packit 032894
  rwlock_wrlock (elf->lock);
Packit 032894
Packit 032894
  if (elf->map_address == NULL && unlikely (elf->fildes == -1))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
Packit 032894
      rwlock_unlock (elf->lock);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* If the file is not mmap'ed and not previously loaded, do it now.  */
Packit 032894
  if (elf->map_address == NULL)
Packit 032894
    {
Packit 032894
      char *mem = NULL;
Packit 032894
Packit 032894
      /* If this is an archive and we have derived descriptors get the
Packit 032894
	 locks for all of them.  */
Packit 032894
      libelf_acquire_all (elf);
Packit 032894
Packit 032894
      if (elf->maximum_size == ~((size_t) 0))
Packit 032894
	{
Packit 032894
	  /* We don't yet know how large the file is.   Determine that now.  */
Packit 032894
	  struct stat st;
Packit 032894
Packit 032894
	  if (fstat (elf->fildes, &st) < 0)
Packit 032894
	    goto read_error;
Packit 032894
Packit 032894
	  if (sizeof (size_t) >= sizeof (st.st_size)
Packit 032894
	      || st.st_size <= ~((size_t) 0))
Packit 032894
	    elf->maximum_size = (size_t) st.st_size;
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      errno = EOVERFLOW;
Packit 032894
	      goto read_error;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
Packit 032894
      /* Allocate all the memory we need.  */
Packit 032894
      mem = (char *) malloc (elf->maximum_size);
Packit 032894
      if (mem != NULL)
Packit 032894
	{
Packit 032894
	  /* Read the file content.  */
Packit 032894
	  if (unlikely ((size_t) pread_retry (elf->fildes, mem,
Packit 032894
					      elf->maximum_size,
Packit 032894
					      elf->start_offset)
Packit 032894
			!= elf->maximum_size))
Packit 032894
	    {
Packit 032894
	      /* Something went wrong.  */
Packit 032894
	    read_error:
Packit 032894
	      __libelf_seterrno (ELF_E_READ_ERROR);
Packit 032894
	      free (mem);
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      /* Remember the address.  */
Packit 032894
	      elf->map_address = mem;
Packit 032894
Packit 032894
	      /* Also remember that we allocated the memory.  */
Packit 032894
	      elf->flags |= ELF_F_MALLOCED;
Packit 032894
Packit 032894
	      /* Propagate the information down to all children and
Packit 032894
		 their children.  */
Packit 032894
	      set_address (elf, elf->start_offset);
Packit 032894
Packit 032894
	      /* Correct the own offsets.  */
Packit 032894
	      if (elf->kind == ELF_K_AR)
Packit 032894
		elf->state.ar.offset -= elf->start_offset;
Packit 032894
	      elf->start_offset = 0;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
      else
Packit 032894
	__libelf_seterrno (ELF_E_NOMEM);
Packit 032894
Packit 032894
      /* Free the locks on the children.  */
Packit 032894
      libelf_release_all (elf);
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
Packit 032894
  return (char *) elf->map_address;
Packit 032894
}