Blame libelf/elf_getdata_rawchunk.c

Packit 032894
/* Return converted data from raw chunk of ELF file.
Packit 032894
   Copyright (C) 2007, 2014, 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 <assert.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.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
Elf_Data *
Packit 032894
elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type)
Packit 032894
{
Packit 032894
  if (unlikely (elf == NULL))
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  if (unlikely (elf->kind != ELF_K_ELF))
Packit 032894
    {
Packit 032894
      /* No valid descriptor.  */
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  if (unlikely (offset < 0 || (uint64_t) offset > elf->maximum_size
Packit 032894
		|| elf->maximum_size - (uint64_t) offset < size))
Packit 032894
Packit 032894
    {
Packit 032894
      /* Invalid request.  */
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_OP);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  if (type >= ELF_T_NUM)
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Get the raw bytes from the file.  */
Packit 032894
  void *rawchunk;
Packit 032894
  int flags = 0;
Packit 032894
  Elf_Data *result = NULL;
Packit 032894
Packit 032894
  rwlock_rdlock (elf->lock);
Packit 032894
Packit 032894
  size_t align = __libelf_type_align (elf->class, type);
Packit 032894
  if (elf->map_address != NULL)
Packit 032894
    {
Packit 032894
    /* If the file is mmap'ed we can use it directly, if aligned for type.  */
Packit 032894
      char *rawdata = elf->map_address + elf->start_offset + offset;
Packit 032894
      if (((uintptr_t) rawdata & (align - 1)) == 0)
Packit 032894
	rawchunk = rawdata;
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* We allocate the memory and memcpy it to get aligned data. */
Packit 032894
	  rawchunk = malloc (size);
Packit 032894
	  if (rawchunk == NULL)
Packit 032894
	    goto nomem;
Packit 032894
	  memcpy (rawchunk, rawdata, size);
Packit 032894
	  flags = ELF_F_MALLOCED;
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      /* We allocate the memory and read the data from the file.  */
Packit 032894
      rawchunk = malloc (size);
Packit 032894
      if (rawchunk == NULL)
Packit 032894
	{
Packit 032894
	nomem:
Packit 032894
	  __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Read the file content.  */
Packit 032894
      if (unlikely ((size_t) pread_retry (elf->fildes, rawchunk, size,
Packit 032894
					  elf->start_offset + offset)
Packit 032894
		    != size))
Packit 032894
	{
Packit 032894
	  /* Something went wrong.  */
Packit 032894
	  free (rawchunk);
Packit 032894
	  __libelf_seterrno (ELF_E_READ_ERROR);
Packit 032894
	  goto out;
Packit 032894
	}
Packit 032894
Packit 032894
      flags = ELF_F_MALLOCED;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Copy and/or convert the data as needed for aligned native-order access.  */
Packit 032894
  void *buffer;
Packit 032894
  if (elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA)
Packit 032894
    {
Packit 032894
      if (((uintptr_t) rawchunk & (align - 1)) == 0)
Packit 032894
	/* No need to copy, we can use the raw data.  */
Packit 032894
	buffer = rawchunk;
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  /* A malloc'd block is always sufficiently aligned.  */
Packit 032894
	  assert (flags == 0);
Packit 032894
Packit 032894
	  buffer = malloc (size);
Packit 032894
	  if (unlikely (buffer == NULL))
Packit 032894
	    goto nomem;
Packit 032894
	  flags = ELF_F_MALLOCED;
Packit 032894
Packit 032894
	  /* The copy will be appropriately aligned for direct access.  */
Packit 032894
	  memcpy (buffer, rawchunk, size);
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      if (flags)
Packit 032894
	buffer = rawchunk;
Packit 032894
      else
Packit 032894
	{
Packit 032894
	  buffer = malloc (size);
Packit 032894
	  if (unlikely (buffer == NULL))
Packit 032894
	    goto nomem;
Packit 032894
	  flags = ELF_F_MALLOCED;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Call the conversion function.  */
Packit 032894
      (*__elf_xfctstom[elf->class - 1][type])(buffer, rawchunk, size, 0);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Allocate the dummy container to point at this buffer.  */
Packit 032894
  Elf_Data_Chunk *chunk = calloc (1, sizeof *chunk);
Packit 032894
  if (chunk == NULL)
Packit 032894
    {
Packit 032894
      if (flags)
Packit 032894
	free (buffer);
Packit 032894
      goto nomem;
Packit 032894
    }
Packit 032894
Packit 032894
  chunk->dummy_scn.elf = elf;
Packit 032894
  chunk->dummy_scn.flags = flags;
Packit 032894
  chunk->data.s = &chunk->dummy_scn;
Packit 032894
  chunk->data.d.d_buf = buffer;
Packit 032894
  chunk->data.d.d_size = size;
Packit 032894
  chunk->data.d.d_type = type;
Packit 032894
  chunk->data.d.d_align = align;
Packit 032894
  chunk->data.d.d_version = EV_CURRENT;
Packit 032894
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
  rwlock_wrlock (elf->lock);
Packit 032894
Packit 032894
  chunk->next = elf->state.elf.rawchunks;
Packit 032894
  elf->state.elf.rawchunks = chunk;
Packit 032894
  result = &chunk->data.d;
Packit 032894
Packit 032894
 out:
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
  return result;
Packit 032894
}