LCOV - code coverage report
Current view: top level - elfutils/libelf - gelf_getehdr.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 28 31 90.3 %
Date: 2012-10-31 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 6 10 60.0 %

           Branch data     Line data    Source code
       1                 :            : /* Get ELF header.
       2                 :            :    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 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 <gelf.h>
      35                 :            : #include <stddef.h>
      36                 :            : #include <stdlib.h>
      37                 :            : #include <string.h>
      38                 :            : 
      39                 :            : #include "libelfP.h"
      40                 :            : 
      41                 :            : 
      42                 :            : GElf_Ehdr *
      43                 :      12998 : __gelf_getehdr_rdlock (elf, dest)
      44                 :            :      Elf *elf;
      45                 :            :      GElf_Ehdr *dest;
      46                 :            : {
      47                 :      12998 :   GElf_Ehdr *result = NULL;
      48                 :            : 
      49         [ +  - ]:      12998 :   if (elf == NULL)
      50                 :            :     return NULL;
      51                 :            : 
      52         [ -  + ]:      12998 :   if (unlikely (elf->kind != ELF_K_ELF))
      53                 :            :     {
      54                 :          0 :       __libelf_seterrno (ELF_E_INVALID_HANDLE);
      55                 :          0 :       return NULL;
      56                 :            :     }
      57                 :            : 
      58                 :            :   /* The following is an optimization: the ehdr element is at the same
      59                 :            :      position in both the elf32 and elf64 structure.  */
      60                 :            :   if (offsetof (struct Elf, state.elf32.ehdr)
      61                 :            :       != offsetof (struct Elf, state.elf64.ehdr))
      62                 :            :     abort ();
      63                 :            :   /* Just pick one of the values.  */
      64         [ -  + ]:      12998 :  if (unlikely (elf->state.elf64.ehdr == NULL))
      65                 :            :     /* Maybe no ELF header was created yet.  */
      66                 :          0 :     __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
      67         [ +  + ]:      12998 :   else if (elf->class == ELFCLASS32)
      68                 :            :     {
      69                 :        308 :       Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
      70                 :            : 
      71                 :            :       /* Convert the 32-bit struct to an 64-bit one.  */
      72                 :        308 :       memcpy (dest->e_ident, ehdr->e_ident, EI_NIDENT);
      73                 :            : #define COPY(name) \
      74                 :            :       dest->name = ehdr->name
      75                 :        308 :       COPY (e_type);
      76                 :        308 :       COPY (e_machine);
      77                 :        308 :       COPY (e_version);
      78                 :        308 :       COPY (e_entry);
      79                 :        308 :       COPY (e_phoff);
      80                 :        308 :       COPY (e_shoff);
      81                 :        308 :       COPY (e_flags);
      82                 :        308 :       COPY (e_ehsize);
      83                 :        308 :       COPY (e_phentsize);
      84                 :        308 :       COPY (e_phnum);
      85                 :        308 :       COPY (e_shentsize);
      86                 :        308 :       COPY (e_shnum);
      87                 :        308 :       COPY (e_shstrndx);
      88                 :            : 
      89                 :        308 :       result = dest;
      90                 :            :     }
      91                 :            :   else
      92                 :      12690 :     result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
      93                 :            : 
      94                 :      12998 :   return result;
      95                 :            : }
      96                 :            : 
      97                 :            : GElf_Ehdr *
      98                 :      12943 : gelf_getehdr (elf, dest)
      99                 :            :      Elf *elf;
     100                 :            :      GElf_Ehdr *dest;
     101                 :            : {
     102                 :            :   GElf_Ehdr *result;
     103         [ +  - ]:      12943 :   if (elf == NULL)
     104                 :            :     return NULL;
     105                 :            : 
     106                 :            :   rwlock_rdlock (elf->lock);
     107                 :      12943 :   result = __gelf_getehdr_rdlock (elf, dest);
     108                 :            :   rwlock_unlock (elf->lock);
     109                 :            : 
     110                 :      12943 :   return result;
     111                 :            : }

Generated by: LCOV version 1.9