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: 2013-03-08 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                 :      13071 : __gelf_getehdr_rdlock (elf, dest)
      44                 :            :      Elf *elf;
      45                 :            :      GElf_Ehdr *dest;
      46                 :            : {
      47                 :      13071 :   GElf_Ehdr *result = NULL;
      48                 :            : 
      49         [ +  - ]:      13071 :   if (elf == NULL)
      50                 :            :     return NULL;
      51                 :            : 
      52         [ -  + ]:      13071 :   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         [ -  + ]:      13071 :  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         [ +  + ]:      13071 :   else if (elf->class == ELFCLASS32)
      68                 :            :     {
      69                 :        311 :       Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
      70                 :            : 
      71                 :            :       /* Convert the 32-bit struct to an 64-bit one.  */
      72                 :        311 :       memcpy (dest->e_ident, ehdr->e_ident, EI_NIDENT);
      73                 :            : #define COPY(name) \
      74                 :            :       dest->name = ehdr->name
      75                 :        311 :       COPY (e_type);
      76                 :        311 :       COPY (e_machine);
      77                 :        311 :       COPY (e_version);
      78                 :        311 :       COPY (e_entry);
      79                 :        311 :       COPY (e_phoff);
      80                 :        311 :       COPY (e_shoff);
      81                 :        311 :       COPY (e_flags);
      82                 :        311 :       COPY (e_ehsize);
      83                 :        311 :       COPY (e_phentsize);
      84                 :        311 :       COPY (e_phnum);
      85                 :        311 :       COPY (e_shentsize);
      86                 :        311 :       COPY (e_shnum);
      87                 :        311 :       COPY (e_shstrndx);
      88                 :            : 
      89                 :        311 :       result = dest;
      90                 :            :     }
      91                 :            :   else
      92                 :      12760 :     result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
      93                 :            : 
      94                 :      13071 :   return result;
      95                 :            : }
      96                 :            : 
      97                 :            : GElf_Ehdr *
      98                 :      13016 : gelf_getehdr (elf, dest)
      99                 :            :      Elf *elf;
     100                 :            :      GElf_Ehdr *dest;
     101                 :            : {
     102                 :            :   GElf_Ehdr *result;
     103         [ +  - ]:      13016 :   if (elf == NULL)
     104                 :            :     return NULL;
     105                 :            : 
     106                 :            :   rwlock_rdlock (elf->lock);
     107                 :      13016 :   result = __gelf_getehdr_rdlock (elf, dest);
     108                 :            :   rwlock_unlock (elf->lock);
     109                 :            : 
     110                 :      13016 :   return result;
     111                 :            : }

Generated by: LCOV version 1.9