125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Return converted data from raw chunk of ELF file.
225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Copyright (C) 2007 Red Hat, Inc.
325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   This file is part of Red Hat elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   it under the terms of the GNU General Public License as published by the
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Free Software Foundation; version 2 of the License.
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   You should have received a copy of the GNU General Public License along
1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   additional right to link the code of Red Hat elfutils with code licensed
2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   under any Open Source Initiative certified open source license
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   (http://www.opensource.org/licenses/index.php) which requires the
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribution of source code with any binary distribution and to
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   distribute linked combinations of the two.  Non-GPL Code permitted under
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this exception must only link to the code of Red Hat elfutils through
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   those well defined interfaces identified in the file named EXCEPTION
2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   found in the source code files (the "Approved Interfaces").  The files
2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   of Non-GPL Code may instantiate templates or use macros or inline
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   functions from the Approved Interfaces without causing the resulting
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   work to be covered by the GNU General Public License.  Only Red Hat,
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc. may make changes or additions to the list of Approved Interfaces.
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat's grant of this exception is conditioned upon your not adding
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   any new exceptions.  If you wish to add a new Approved Interface or
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception, please contact Red Hat.  You must obey the GNU General Public
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   License in all respects for all of the Red Hat elfutils code and other
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   code used in conjunction with Red Hat elfutils except the Non-GPL Code
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   covered by this exception.  If you modify this file, you may extend this
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception to your version of the file, but you are not obligated to do
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   so.  If you do not wish to provide this exception without modification,
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   you must delete this exception statement from your version and license
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this file solely under the GPL without exception.
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is an included package of the Open Invention Network.
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   An included package of the Open Invention Network is a package for which
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Open Invention Network licensees cross-license their patents.  No patent
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   license is granted, either expressly or impliedly, by designation as an
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   included package.  Should you wish to participate in the Open Invention
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Network licensing program, please visit www.openinventionnetwork.com
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   <http://www.openinventionnetwork.com>.  */
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <assert.h>
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <errno.h>
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdlib.h>
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <string.h>
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <unistd.h>
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <system.h>
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libelfP.h"
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "common.h"
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6425b3c049e70834cf33790a28643ab058b507b35cBen ChengElf_Data *
6525b3c049e70834cf33790a28643ab058b507b35cBen Chengelf_getdata_rawchunk (elf, offset, size, type)
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng     Elf *elf;
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng     off64_t offset;
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng     size_t size;
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng     Elf_Type type;
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (unlikely (elf == NULL))
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return NULL;
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (unlikely (elf->kind != ELF_K_ELF))
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* No valid descriptor.  */
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      __libelf_seterrno (ELF_E_INVALID_HANDLE);
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return NULL;
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (unlikely (offset < 0 || offset + (off64_t) size < offset
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng		|| offset + size > elf->maximum_size))
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Invalid request.  */
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      __libelf_seterrno (ELF_E_INVALID_OP);
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return NULL;
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (type >= ELF_T_NUM)
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      __libelf_seterrno (ELF_E_UNKNOWN_TYPE);
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return NULL;
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Get the raw bytes from the file.  */
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  void *rawchunk;
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  int flags = 0;
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Elf_Data *result = NULL;
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  rwlock_rdlock (elf->lock);
10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* If the file is mmap'ed we can use it directly.  */
10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (elf->map_address != NULL)
10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    rawchunk = elf->map_address + elf->start_offset + offset;
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  else
10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* We allocate the memory and read the data from the file.  */
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      rawchunk = malloc (size);
10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (rawchunk == NULL)
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	nomem:
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  __libelf_seterrno (ELF_E_NOMEM);
11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  goto out;
11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Read the file content.  */
11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (unlikely ((size_t) pread_retry (elf->fildes, rawchunk, size,
11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng					  elf->start_offset + offset)
11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		    != size))
12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  /* Something went wrong.  */
12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  free (rawchunk);
12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  __libelf_seterrno (ELF_E_READ_ERROR);
12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  goto out;
12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      flags = ELF_F_MALLOCED;
12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Copy and/or convert the data as needed for aligned native-order access.  */
13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  size_t align = __libelf_type_align (elf->class, type);
13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  void *buffer;
13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA)
13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (((uintptr_t) rawchunk & (align - 1)) == 0)
13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	/* No need to copy, we can use the raw data.  */
13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	buffer = rawchunk;
13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      else
13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  /* A malloc'd block is always sufficiently aligned.  */
14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  assert (flags == 0);
14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  buffer = malloc (size);
14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (unlikely (buffer == NULL))
14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    goto nomem;
14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  flags = ELF_F_MALLOCED;
14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  /* The copy will be appropriately aligned for direct access.  */
14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  memcpy (buffer, rawchunk, size);
15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  else
15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (flags)
15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	buffer = rawchunk;
15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      else
15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  buffer = malloc (size);
15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (unlikely (buffer == NULL))
16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    goto nomem;
16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  flags = ELF_F_MALLOCED;
16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Call the conversion function.  */
16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      (*__elf_xfctstom[LIBELF_EV_IDX][LIBELF_EV_IDX][elf->class - 1][type])
16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	(buffer, rawchunk, size, 0);
16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Allocate the dummy container to point at this buffer.  */
17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Elf_Data_Chunk *chunk = calloc (1, sizeof *chunk);
17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (chunk == NULL)
17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (flags)
17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	free (buffer);
17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      goto nomem;
17625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
17725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
17825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->dummy_scn.elf = elf;
17925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->dummy_scn.flags = flags;
18025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.s = &chunk->dummy_scn;
18125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.d.d_buf = buffer;
18225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.d.d_size = size;
18325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.d.d_type = type;
18425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.d.d_align = align;
18525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->data.d.d_version = __libelf_version;
18625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
18725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  rwlock_unlock (elf->lock);
18825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  rwlock_wrlock (elf->lock);
18925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
19025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  chunk->next = elf->state.elf.rawchunks;
19125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  elf->state.elf.rawchunks = chunk;
19225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  result = &chunk->data.d;
19325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
19425b3c049e70834cf33790a28643ab058b507b35cBen Cheng out:
19525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  rwlock_unlock (elf->lock);
19625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return result;
19725b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
198