LCOV - code coverage report
Current view: top level - libelf - common.h (source / functions) Hit Total Coverage
Test: elfutils-0.183 Lines: 23 42 54.8 %
Date: 2021-02-07 19:08:58 Functions: 2 4 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 28 42.9 %

           Branch data     Line data    Source code
       1                 :            : /* Common definitions for handling files in memory or only on disk.
       2                 :            :    Copyright (C) 1998, 1999, 2000, 2002, 2005, 2008 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                 :            : #ifndef _COMMON_H
      31                 :            : #define _COMMON_H       1
      32                 :            : 
      33                 :            : #include <ar.h>
      34                 :            : #include <byteswap.h>
      35                 :            : #include <endian.h>
      36                 :            : #include <stdlib.h>
      37                 :            : #include <string.h>
      38                 :            : 
      39                 :            : #include "libelfP.h"
      40                 :            : 
      41                 :            : static inline Elf_Kind
      42                 :            : __attribute__ ((unused))
      43                 :      15361 : determine_kind (void *buf, size_t len)
      44                 :            : {
      45                 :            :   /* First test for an archive.  */
      46 [ +  + ][ +  + ]:      15361 :   if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
      47                 :            :     return ELF_K_AR;
      48                 :            : 
      49                 :            :   /* Next try ELF files.  */
      50 [ +  + ][ +  + ]:      15224 :   if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
      51                 :            :     {
      52                 :            :       /* Could be an ELF file.  */
      53                 :      14917 :       int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
      54                 :      14917 :       int data = (int) ((unsigned char *) buf)[EI_DATA];
      55                 :      14917 :       int version = (int) ((unsigned char *) buf)[EI_VERSION];
      56                 :            : 
      57         [ +  - ]:      14917 :       if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
      58         [ +  - ]:      14917 :           && data > ELFDATANONE && data < ELFDATANUM
      59         [ +  - ]:      14917 :           && version == EV_CURRENT)
      60                 :      14917 :         return ELF_K_ELF;
      61                 :            :     }
      62                 :            : 
      63                 :            :   /* We do not know this file type.  */
      64                 :            :   return ELF_K_NONE;
      65                 :            : }
      66                 :            : 
      67                 :            : 
      68                 :            : /* Allocate an Elf descriptor and fill in the generic information.  */
      69                 :            : static inline Elf *
      70                 :            : __attribute__ ((unused))
      71                 :      15832 : allocate_elf (int fildes, void *map_address, int64_t offset, size_t maxsize,
      72                 :            :               Elf_Cmd cmd, Elf *parent, Elf_Kind kind, size_t extra)
      73                 :            : {
      74                 :      15832 :   Elf *result = (Elf *) calloc (1, sizeof (Elf) + extra);
      75         [ -  + ]:      15832 :   if (result == NULL)
      76                 :          0 :     __libelf_seterrno (ELF_E_NOMEM);
      77                 :            :   else
      78                 :            :     {
      79                 :      15832 :       result->kind = kind;
      80                 :      15832 :       result->ref_count = 1;
      81                 :      15832 :       result->cmd = cmd;
      82                 :      15832 :       result->fildes = fildes;
      83                 :      15832 :       result->start_offset = offset;
      84                 :      15832 :       result->maximum_size = maxsize;
      85                 :      15832 :       result->map_address = map_address;
      86                 :      15832 :       result->parent = parent;
      87                 :            : 
      88                 :      15832 :       rwlock_init (result->lock);
      89                 :            :     }
      90                 :            : 
      91                 :      15832 :   return result;
      92                 :            : }
      93                 :            : 
      94                 :            : 
      95                 :            : /* Acquire lock for the descriptor and all children.  */
      96                 :            : static void
      97                 :            : __attribute__ ((unused))
      98                 :          0 : libelf_acquire_all (Elf *elf)
      99                 :            : {
     100                 :          0 :   rwlock_wrlock (elf->lock);
     101                 :            : 
     102         [ #  # ]:          0 :   if (elf->kind == ELF_K_AR)
     103                 :            :     {
     104                 :          0 :       Elf *child = elf->state.ar.children;
     105                 :            : 
     106         [ #  # ]:          0 :       while (child != NULL)
     107                 :            :         {
     108         [ #  # ]:          0 :           if (child->ref_count != 0)
     109                 :          0 :             libelf_acquire_all (child);
     110                 :          0 :           child = child->next;
     111                 :            :         }
     112                 :            :     }
     113                 :          0 : }
     114                 :            : 
     115                 :            : /* Release own lock and those of the children.  */
     116                 :            : static void
     117                 :            : __attribute__ ((unused))
     118                 :          0 : libelf_release_all (Elf *elf)
     119                 :            : {
     120         [ #  # ]:          0 :   if (elf->kind == ELF_K_AR)
     121                 :            :     {
     122                 :          0 :       Elf *child = elf->state.ar.children;
     123                 :            : 
     124         [ #  # ]:          0 :       while (child != NULL)
     125                 :            :         {
     126         [ #  # ]:          0 :           if (child->ref_count != 0)
     127                 :          0 :             libelf_release_all (child);
     128                 :          0 :           child = child->next;
     129                 :            :         }
     130                 :            :     }
     131                 :            : 
     132                 :          0 :   rwlock_unlock (elf->lock);
     133                 :          0 : }
     134                 :            : 
     135                 :            : 
     136                 :            : /* Macro to convert endianness in place.  It determines the function it
     137                 :            :    has to use itself.  */
     138                 :            : #define CONVERT(Var) \
     139                 :            :   (Var) = (sizeof (Var) == 1                                                  \
     140                 :            :            ? (unsigned char) (Var)                                            \
     141                 :            :            : (sizeof (Var) == 2                                               \
     142                 :            :               ? bswap_16 (Var)                                                \
     143                 :            :               : (sizeof (Var) == 4                                            \
     144                 :            :                  ? bswap_32 (Var)                                             \
     145                 :            :                  : bswap_64 (Var))))
     146                 :            : 
     147                 :            : #define CONVERT_TO(Dst, Var) \
     148                 :            :   (Dst) = (sizeof (Var) == 1                                                  \
     149                 :            :            ? (unsigned char) (Var)                                            \
     150                 :            :            : (sizeof (Var) == 2                                               \
     151                 :            :               ? bswap_16 (Var)                                                \
     152                 :            :               : (sizeof (Var) == 4                                            \
     153                 :            :                  ? bswap_32 (Var)                                             \
     154                 :            :                  : bswap_64 (Var))))
     155                 :            : 
     156                 :            : 
     157                 :            : #if __BYTE_ORDER == __LITTLE_ENDIAN
     158                 :            : # define MY_ELFDATA     ELFDATA2LSB
     159                 :            : #else
     160                 :            : # define MY_ELFDATA     ELFDATA2MSB
     161                 :            : #endif
     162                 :            : 
     163                 :            : #endif  /* common.h */

Generated by: LCOV version 1.13