LCOV - code coverage report
Current view: top level - libelf - elf_getphdrnum.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 25 40 62.5 %
Date: 2017-05-05 10:17:09 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Return number of program headers in the ELF file.
       2             :    Copyright (C) 2010, 2014, 2015, 2016 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             : 
       5             :    This file is free software; you can redistribute it and/or modify
       6             :    it under the terms of either
       7             : 
       8             :      * the GNU Lesser General Public License as published by the Free
       9             :        Software Foundation; either version 3 of the License, or (at
      10             :        your option) any later version
      11             : 
      12             :    or
      13             : 
      14             :      * the GNU General Public License as published by the Free
      15             :        Software Foundation; either version 2 of the License, or (at
      16             :        your option) any later version
      17             : 
      18             :    or both in parallel, as here.
      19             : 
      20             :    elfutils is distributed in the hope that it will be useful, but
      21             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23             :    General Public License for more details.
      24             : 
      25             :    You should have received copies of the GNU General Public License and
      26             :    the GNU Lesser General Public License along with this program.  If
      27             :    not, see <http://www.gnu.org/licenses/>.  */
      28             : 
      29             : #ifdef HAVE_CONFIG_H
      30             : # include <config.h>
      31             : #endif
      32             : 
      33             : #include <assert.h>
      34             : #include <gelf.h>
      35             : #include <stddef.h>
      36             : 
      37             : #include "libelfP.h"
      38             : 
      39             : 
      40             : int
      41             : internal_function
      42       52291 : __elf_getphdrnum_rdlock (Elf *elf, size_t *dst)
      43             : {
      44       52291 :  if (unlikely (elf->state.elf64.ehdr == NULL))
      45             :    {
      46             :      /* Maybe no ELF header was created yet.  */
      47           0 :      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
      48           0 :      return -1;
      49             :    }
      50             : 
      51      104582 :  *dst = (elf->class == ELFCLASS32
      52        7491 :          ? elf->state.elf32.ehdr->e_phnum
      53       59782 :          : elf->state.elf64.ehdr->e_phnum);
      54             : 
      55       52291 :  if (*dst == PN_XNUM)
      56             :    {
      57           0 :      const Elf_ScnList *const scns = (elf->class == ELFCLASS32
      58             :                                       ? &elf->state.elf32.scns
      59             :                                       : &elf->state.elf64.scns);
      60             : 
      61             :      /* If there are no section headers, perhaps this is really just 65536
      62             :         written without PN_XNUM support.  Either that or it's bad data.  */
      63             : 
      64           0 :      if (elf->class == ELFCLASS32)
      65             :        {
      66           0 :          if (likely (scns->cnt > 0
      67             :                      && elf->state.elf32.scns.data[0].shdr.e32 != NULL))
      68           0 :            *dst = scns->data[0].shdr.e32->sh_info;
      69             :        }
      70             :      else
      71             :        {
      72           0 :          if (likely (scns->cnt > 0
      73             :                      && elf->state.elf64.scns.data[0].shdr.e64 != NULL))
      74           0 :            *dst = scns->data[0].shdr.e64->sh_info;
      75             :        }
      76             :    }
      77             : 
      78             :  return 0;
      79             : }
      80             : 
      81             : int
      82             : internal_function
      83       45744 : __elf_getphdrnum_chk_rdlock (Elf *elf, size_t *dst)
      84             : {
      85       45744 :   int result = __elf_getphdrnum_rdlock (elf, dst);
      86             : 
      87             :   /* If the phdrs haven't been created or read in yet then do some
      88             :      sanity checking to make sure phnum and phoff are consistent.  */
      89       45744 :   if (elf->state.elf.phdr == NULL)
      90             :     {
      91       12384 :       Elf64_Off off = (elf->class == ELFCLASS32
      92         325 :                        ? elf->state.elf32.ehdr->e_phoff
      93        6517 :                        : elf->state.elf64.ehdr->e_phoff);
      94        6192 :       if (unlikely (off == 0))
      95             :         {
      96         448 :           *dst = 0;
      97         448 :           return result;
      98             :         }
      99             : 
     100        5744 :       if (unlikely (off >= elf->maximum_size))
     101             :         {
     102           0 :           __libelf_seterrno (ELF_E_INVALID_DATA);
     103           0 :           return -1;
     104             :         }
     105             : 
     106             :       /* Check for too many sections.  */
     107        5744 :       size_t phdr_size = (elf->class == ELFCLASS32
     108        5744 :                           ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr));
     109        5744 :       if (unlikely (*dst > SIZE_MAX / phdr_size))
     110             :         {
     111           0 :           __libelf_seterrno (ELF_E_INVALID_DATA);
     112           0 :           return -1;
     113             :         }
     114             : 
     115             :       /* Truncated file?  Don't return more than can be indexed.  */
     116        5744 :       if (unlikely (elf->maximum_size - off < *dst * phdr_size))
     117           0 :         *dst = (elf->maximum_size - off) / phdr_size;
     118             :     }
     119             : 
     120             :   return result;
     121             : }
     122             : 
     123             : int
     124        7010 : elf_getphdrnum (Elf *elf, size_t *dst)
     125             : {
     126             :   int result;
     127             : 
     128        7010 :   if (elf == NULL)
     129             :     return -1;
     130             : 
     131        7010 :   if (unlikely (elf->kind != ELF_K_ELF))
     132             :     {
     133           0 :       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     134           0 :       return -1;
     135             :     }
     136             : 
     137             :   rwlock_rdlock (elf->lock);
     138        7010 :   result = __elf_getphdrnum_chk_rdlock (elf, dst);
     139             :   rwlock_unlock (elf->lock);
     140             : 
     141        7010 :   return result;
     142             : }

Generated by: LCOV version 1.12