LCOV - code coverage report
Current view: top level - libelf - gelf_xlate.c (source / functions) Hit Total Coverage
Test: elfutils-0.183 Lines: 4 4 100.0 %
Date: 2021-02-07 19:08:58 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 2 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* Transformation functions for ELF data types.
       2                 :            :    Copyright (C) 1998,1999,2000,2002,2004,2005,2006,2007,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 <byteswap.h>
      35                 :            : #include <stdint.h>
      36                 :            : #include <string.h>
      37                 :            : #include <stdlib.h>
      38                 :            : 
      39                 :            : #include "libelfP.h"
      40                 :            : 
      41                 :            : #ifndef LIBELFBITS
      42                 :            : # define LIBELFBITS     32
      43                 :            : #endif
      44                 :            : 
      45                 :            : 
      46                 :            : /* Well, what shall I say.  Nothing to do here.  */
      47                 :            : #define elf_cvt_Byte(dest, src, n) \
      48                 :            :   (__builtin_constant_p (n) && (n) == 1                                       \
      49                 :            :    ? (void) (*((char *) (dest)) = *((char *) (src)))                          \
      50                 :            :    : Elf32_cvt_Byte (dest, src, n))
      51                 :            : static void
      52                 :     788157 : (elf_cvt_Byte) (void *dest, const void *src, size_t n,
      53                 :            :                 int encode __attribute__ ((unused)))
      54                 :            : {
      55         [ +  + ]:     788157 :   if (n != 0)
      56                 :     788056 :     memmove (dest, src, n);
      57                 :     788157 : }
      58                 :            : 
      59                 :            : 
      60                 :            : /* We'll optimize the definition of the conversion functions here a
      61                 :            :    bit.  We need only functions for 16, 32, and 64 bits.  The
      62                 :            :    functions referenced in the table will be aliases for one of these
      63                 :            :    functions.  Which one is decided by the ELFxx_FSZ_type.  */
      64                 :            : 
      65                 :            : #if ALLOW_UNALIGNED
      66                 :            : 
      67                 :            : #define FETCH(Bits, ptr)        (*(const uint##Bits##_t *) ptr)
      68                 :            : #define STORE(Bits, ptr, val)   (*(uint##Bits##_t *) ptr = val)
      69                 :            : 
      70                 :            : #else
      71                 :            : 
      72                 :            : union unaligned
      73                 :            :   {
      74                 :            :     uint16_t u16;
      75                 :            :     uint32_t u32;
      76                 :            :     uint64_t u64;
      77                 :            :   } attribute_packed;
      78                 :            : 
      79                 :            : #define FETCH(Bits, ptr)        (((const union unaligned *) ptr)->u##Bits)
      80                 :            : #define STORE(Bits, ptr, val)   (((union unaligned *) ptr)->u##Bits = val)
      81                 :            : 
      82                 :            : #endif
      83                 :            : 
      84                 :            : /* Now define the conversion functions for the basic types.  We use here
      85                 :            :    the fact that file and memory types are the same and that we have the
      86                 :            :    ELFxx_FSZ_* macros.
      87                 :            : 
      88                 :            :    At the same time we define inline functions which we will use to
      89                 :            :    convert the complex types.  */
      90                 :            : #define FUNDAMENTAL(NAME, Name, Bits) \
      91                 :            :   INLINE2 (ELFW2(Bits,FSZ_##NAME), ElfW2(Bits,cvt_##Name), ElfW2(Bits,Name))
      92                 :            : #define INLINE2(Bytes, FName, TName) \
      93                 :            :   INLINE3 (Bytes, FName, TName)
      94                 :            : #define INLINE3(Bytes, FName, TName)                                          \
      95                 :            :   static inline void FName##1 (void *dest, const void *ptr)                   \
      96                 :            :   {                                                                           \
      97                 :            :     switch (Bytes)                                                            \
      98                 :            :       {                                                                       \
      99                 :            :       case 2: STORE (16, dest, bswap_16 (FETCH (16, ptr))); break;            \
     100                 :            :       case 4: STORE (32, dest, bswap_32 (FETCH (32, ptr))); break;            \
     101                 :            :       case 8: STORE (64, dest, bswap_64 (FETCH (64, ptr))); break;            \
     102                 :            :       default:                                                                \
     103                 :            :         abort ();                                                             \
     104                 :            :       }                                                                       \
     105                 :            :   }                                                                           \
     106                 :            :                                                                               \
     107                 :            :   static void FName (void *dest, const void *ptr, size_t len,                 \
     108                 :            :                      int encode __attribute__ ((unused)))                     \
     109                 :            :   {                                                                           \
     110                 :            :     size_t n = len / sizeof (TName);                                          \
     111                 :            :     if (dest < ptr)                                                        \
     112                 :            :       while (n-- > 0)                                                              \
     113                 :            :         {                                                                     \
     114                 :            :           FName##1 (dest, ptr);                                               \
     115                 :            :           dest += Bytes;                                                      \
     116                 :            :           ptr += Bytes;                                                       \
     117                 :            :         }                                                                     \
     118                 :            :     else                                                                      \
     119                 :            :       {                                                                       \
     120                 :            :         dest += len;                                                          \
     121                 :            :         ptr += len;                                                           \
     122                 :            :         while (n-- > 0)                                                            \
     123                 :            :           {                                                                   \
     124                 :            :             ptr -= Bytes;                                                     \
     125                 :            :             dest -= Bytes;                                                    \
     126                 :            :             FName##1 (dest, ptr);                                             \
     127                 :            :           }                                                                   \
     128                 :            :       }                                                                       \
     129                 :            :   }
     130                 :            : 
     131                 :            : 
     132                 :            : /* Now the tricky part: define the transformation functions for the
     133                 :            :    complex types.  We will use the definitions of the types in
     134                 :            :    abstract.h.  */
     135                 :            : #define START(Bits, Name, EName) \
     136                 :            :   static void                                                                 \
     137                 :            :   ElfW2 (Bits, cvt_##Name) (void *dest, const void *src, size_t len,          \
     138                 :            :                             int encode __attribute__ ((unused)))              \
     139                 :            :   { ElfW2(Bits, Name) *tdest = (ElfW2(Bits, Name) *) dest;                    \
     140                 :            :     ElfW2(Bits, Name) *tsrc = (ElfW2(Bits, Name) *) src;                      \
     141                 :            :     size_t n;                                                                 \
     142                 :            :     for (n = len / sizeof (ElfW2(Bits, Name)); n > 0; ++tdest, ++tsrc, --n) {
     143                 :            : #define END(Bits, Name) } }
     144                 :            : #define TYPE_EXTRA(Code)
     145                 :            : #define TYPE_XLATE(Code) Code
     146                 :            : #define TYPE_NAME(Type, Name) TYPE_NAME2 (Type, Name)
     147                 :            : #define TYPE_NAME2(Type, Name) Type##1 (&tdest->Name, &tsrc->Name);
     148                 :            : #define TYPE(Name, Bits) TYPE2 (Name, Bits)
     149                 :            : #define TYPE2(Name, Bits) TYPE3 (Name##Bits)
     150                 :            : #define TYPE3(Name) Name (cvt_)
     151                 :            : 
     152                 :            : /* Signal that we are generating conversion functions.  */
     153                 :            : #define GENERATE_CONVERSION
     154                 :            : 
     155                 :            : /* First generate the 32-bit conversion functions.  */
     156                 :            : #define LIBELFBITS 32
     157                 :            : #include "gelf_xlate.h"
     158                 :            : 
     159                 :            : /* Now generate the 64-bit conversion functions.  */
     160                 :            : #define LIBELFBITS 64
     161                 :            : #include "gelf_xlate.h"
     162                 :            : 
     163                 :            : 
     164                 :            : /* We have a few functions which we must create by hand since the sections
     165                 :            :    do not contain records of only one type.  */
     166                 :            : #include "version_xlate.h"
     167                 :            : #include "gnuhash_xlate.h"
     168                 :            : #include "note_xlate.h"
     169                 :            : #include "chdr_xlate.h"
     170                 :            : 
     171                 :            : 
     172                 :            : /* Now the externally visible table with the function pointers.  */
     173                 :            : const xfct_t __elf_xfctstom[ELFCLASSNUM - 1][ELF_T_NUM] =
     174                 :            : {
     175                 :            :       [ELFCLASS32 - 1] = {
     176                 :            : #define define_xfcts(Bits) \
     177                 :            :         [ELF_T_BYTE]    = elf_cvt_Byte,                                       \
     178                 :            :         [ELF_T_ADDR]    = ElfW2(Bits, cvt_Addr),                              \
     179                 :            :         [ELF_T_DYN]     = ElfW2(Bits, cvt_Dyn),                               \
     180                 :            :         [ELF_T_EHDR]    = ElfW2(Bits, cvt_Ehdr),                              \
     181                 :            :         [ELF_T_HALF]    = ElfW2(Bits, cvt_Half),                              \
     182                 :            :         [ELF_T_OFF]     = ElfW2(Bits, cvt_Off),                               \
     183                 :            :         [ELF_T_PHDR]    = ElfW2(Bits, cvt_Phdr),                              \
     184                 :            :         [ELF_T_RELA]    = ElfW2(Bits, cvt_Rela),                              \
     185                 :            :         [ELF_T_REL]     = ElfW2(Bits, cvt_Rel),                               \
     186                 :            :         [ELF_T_SHDR]    = ElfW2(Bits, cvt_Shdr),                              \
     187                 :            :         [ELF_T_SWORD]   = ElfW2(Bits, cvt_Sword),                             \
     188                 :            :         [ELF_T_SYM]     = ElfW2(Bits, cvt_Sym),                               \
     189                 :            :         [ELF_T_WORD]    = ElfW2(Bits, cvt_Word),                              \
     190                 :            :         [ELF_T_XWORD]   = ElfW2(Bits, cvt_Xword),                             \
     191                 :            :         [ELF_T_SXWORD]  = ElfW2(Bits, cvt_Sxword),                            \
     192                 :            :         [ELF_T_VDEF]    = elf_cvt_Verdef,                                     \
     193                 :            :         [ELF_T_VDAUX]   = elf_cvt_Verdef,                                     \
     194                 :            :         [ELF_T_VNEED]   = elf_cvt_Verneed,                                    \
     195                 :            :         [ELF_T_VNAUX]   = elf_cvt_Verneed,                                    \
     196                 :            :         [ELF_T_NHDR]    = elf_cvt_note4,                                      \
     197                 :            :         [ELF_T_NHDR8]   = elf_cvt_note8,                                      \
     198                 :            :         [ELF_T_SYMINFO] = ElfW2(Bits, cvt_Syminfo),                           \
     199                 :            :         [ELF_T_MOVE]    = ElfW2(Bits, cvt_Move),                              \
     200                 :            :         [ELF_T_LIB]     = ElfW2(Bits, cvt_Lib),                               \
     201                 :            :         [ELF_T_AUXV]    = ElfW2(Bits, cvt_auxv_t),                            \
     202                 :            :         [ELF_T_CHDR]    = ElfW2(Bits, cvt_chdr)
     203                 :            :         define_xfcts (32),
     204                 :            :         [ELF_T_GNUHASH] = Elf32_cvt_Word
     205                 :            :       },
     206                 :            :       [ELFCLASS64 - 1] = {
     207                 :            :         define_xfcts (64),
     208                 :            :         [ELF_T_GNUHASH] = elf_cvt_gnuhash
     209                 :            :       }
     210                 :            : };

Generated by: LCOV version 1.13