Blame libelf/elf32_xlatetom.c

Packit 032894
/* Convert from file to memory representation.
Packit 032894
   Copyright (C) 1998, 1999, 2000, 2002, 2012, 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 <endian.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
Elf_Data *
Packit 032894
elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
Packit 032894
			     unsigned int encode)
Packit 032894
{
Packit 032894
  /* First test whether the input data is really suitable for this
Packit 032894
     type.  This means, whether there is an integer number of records.
Packit 032894
     Note that for this implementation the memory and file size of the
Packit 032894
     data types are identical.  */
Packit 032894
  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
Packit 032894
Packit 032894
Packit 032894
  /* We shouldn't require integer number of records when processing
Packit 032894
     notes.  Payload bytes follow the header immediately, it's not an
Packit 032894
     array of records as is the case otherwise.  */
Packit 032894
  if (src->d_type != ELF_T_NHDR && src->d_type != ELF_T_NHDR8
Packit 032894
      && src->d_size % recsize != 0)
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_DATA);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Next see whether the converted data fits in the output buffer.  */
Packit 032894
  if (src->d_size > dest->d_size)
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_DEST_SIZE);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Test the encode parameter.  */
Packit 032894
  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_ENCODING);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Determine the translation function to use.
Packit 032894
Packit 032894
     At this point we make an assumption which is valid for all
Packit 032894
     existing implementations so far: the memory and file sizes are
Packit 032894
     the same.  This has very important consequences:
Packit 032894
     a) The requirement that the source and destination buffer can
Packit 032894
	overlap can easily be fulfilled.
Packit 032894
     b) We need only one function to convert from and memory to file
Packit 032894
	and vice versa since the function only has to copy and/or
Packit 032894
	change the byte order.
Packit 032894
  */
Packit 032894
  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
Packit 032894
      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
Packit 032894
    {
Packit 032894
      /* We simply have to copy since the byte order is the same.  */
Packit 032894
      if (src->d_buf != dest->d_buf)
Packit 032894
	memmove (dest->d_buf, src->d_buf, src->d_size);
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      xfct_t fctp;
Packit 032894
      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
Packit 032894
Packit 032894
      /* Do the real work.  */
Packit 032894
      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Now set the real destination type and length since the operation was
Packit 032894
     successful.  */
Packit 032894
  dest->d_type = src->d_type;
Packit 032894
  dest->d_size = src->d_size;
Packit 032894
Packit 032894
  return dest;
Packit 032894
}
Packit 032894
INTDEF(elfw2(LIBELFBITS, xlatetom))