LCOV - code coverage report
Current view: top level - libelf - elf32_xlatetom.c (source / functions) Hit Total Coverage
Test: elfutils-0.173 Lines: 14 20 70.0 %
Date: 2018-06-29 23:49:12 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Convert from file to memory representation.
       2             :    Copyright (C) 1998, 1999, 2000, 2002, 2012, 2015 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             :    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
       5             : 
       6             :    This file is free software; you can redistribute it and/or modify
       7             :    it under the terms of either
       8             : 
       9             :      * the GNU Lesser General Public License as published by the Free
      10             :        Software Foundation; either version 3 of the License, or (at
      11             :        your option) any later version
      12             : 
      13             :    or
      14             : 
      15             :      * the GNU General Public License as published by the Free
      16             :        Software Foundation; either version 2 of the License, or (at
      17             :        your option) any later version
      18             : 
      19             :    or both in parallel, as here.
      20             : 
      21             :    elfutils is distributed in the hope that it will be useful, but
      22             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      23             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24             :    General Public License for more details.
      25             : 
      26             :    You should have received copies of the GNU General Public License and
      27             :    the GNU Lesser General Public License along with this program.  If
      28             :    not, see <http://www.gnu.org/licenses/>.  */
      29             : 
      30             : #ifdef HAVE_CONFIG_H
      31             : # include <config.h>
      32             : #endif
      33             : 
      34             : #include <assert.h>
      35             : #include <endian.h>
      36             : #include <string.h>
      37             : 
      38             : #include "libelfP.h"
      39             : 
      40             : #ifndef LIBELFBITS
      41             : # define LIBELFBITS     32
      42             : #endif
      43             : 
      44             : 
      45             : Elf_Data *
      46        6789 : elfw2(LIBELFBITS, xlatetom) (Elf_Data *dest, const Elf_Data *src,
      47             :                              unsigned int encode)
      48             : {
      49             :   /* First test whether the input data is really suitable for this
      50             :      type.  This means, whether there is an integer number of records.
      51             :      Note that for this implementation the memory and file size of the
      52             :      data types are identical.  */
      53             : #if EV_NUM != 2
      54             :   size_t recsize = __libelf_type_sizes[src->d_version - 1][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
      55             : #else
      56        6789 :   size_t recsize = __libelf_type_sizes[0][ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
      57             : #endif
      58             : 
      59             : 
      60             :   /* We shouldn't require integer number of records when processing
      61             :      notes.  Payload bytes follow the header immediately, it's not an
      62             :      array of records as is the case otherwise.  */
      63        6789 :   if (src->d_type != ELF_T_NHDR
      64        6767 :       && src->d_size % recsize != 0)
      65             :     {
      66           0 :       __libelf_seterrno (ELF_E_INVALID_DATA);
      67           0 :       return NULL;
      68             :     }
      69             : 
      70             :   /* Next see whether the converted data fits in the output buffer.  */
      71        6789 :   if (src->d_size > dest->d_size)
      72             :     {
      73           0 :       __libelf_seterrno (ELF_E_DEST_SIZE);
      74           0 :       return NULL;
      75             :     }
      76             : 
      77             :   /* Test the encode parameter.  */
      78        6789 :   if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
      79             :     {
      80           0 :       __libelf_seterrno (ELF_E_INVALID_ENCODING);
      81           0 :       return NULL;
      82             :     }
      83             : 
      84             :   /* Determine the translation function to use.
      85             : 
      86             :      At this point we make an assumption which is valid for all
      87             :      existing implementations so far: the memory and file sizes are
      88             :      the same.  This has very important consequences:
      89             :      a) The requirement that the source and destination buffer can
      90             :         overlap can easily be fulfilled.
      91             :      b) We need only one function to convert from and memory to file
      92             :         and vice versa since the function only has to copy and/or
      93             :         change the byte order.
      94             :   */
      95        6789 :   if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
      96             :       || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
      97             :     {
      98             :       /* We simply have to copy since the byte order is the same.  */
      99        6456 :       if (src->d_buf != dest->d_buf)
     100        6456 :         memmove (dest->d_buf, src->d_buf, src->d_size);
     101             :     }
     102             :   else
     103             :     {
     104             :       xfct_t fctp;
     105             : 
     106             :       /* Get a pointer to the transformation functions.  The `#ifdef' is
     107             :          a small optimization since we don't anticipate another ELF
     108             :          version and so would waste "precious" code.  */
     109             : #if EV_NUM != 2
     110             :       fctp = __elf_xfctstom[src->d_version - 1][dest->d_version - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
     111             : #else
     112         333 :       fctp = __elf_xfctstom[0][0][ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
     113             : #endif
     114             : 
     115             :       /* Do the real work.  */
     116         333 :       (*fctp) (dest->d_buf, src->d_buf, src->d_size, 0);
     117             :     }
     118             : 
     119             :   /* Now set the real destination type and length since the operation was
     120             :      successful.  */
     121        6789 :   dest->d_type = src->d_type;
     122        6789 :   dest->d_size = src->d_size;
     123             : 
     124        6789 :   return dest;
     125             : }
     126             : INTDEF(elfw2(LIBELFBITS, xlatetom))

Generated by: LCOV version 1.13