LCOV - code coverage report
Current view: top level - libelf - elf32_newphdr.c (source / functions) Hit Total Coverage
Test: elfutils-0.173 Lines: 30 65 46.2 %
Date: 2018-06-29 23:49:12 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Create new ELF program header table.
       2             :    Copyright (C) 1999-2010, 2014, 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 <assert.h>
      35             : #include <stdlib.h>
      36             : #include <string.h>
      37             : 
      38             : #include "libelfP.h"
      39             : 
      40             : #ifndef LIBELFBITS
      41             : # define LIBELFBITS 32
      42             : #endif
      43             : 
      44             : 
      45             : ElfW2(LIBELFBITS,Phdr) *
      46         254 : elfw2(LIBELFBITS,newphdr) (Elf *elf, size_t count)
      47             : {
      48             :   ElfW2(LIBELFBITS,Phdr) *result;
      49             : 
      50         254 :   if (elf == NULL)
      51             :     return NULL;
      52             : 
      53         254 :   if (unlikely (elf->kind != ELF_K_ELF))
      54             :     {
      55           0 :       __libelf_seterrno (ELF_E_INVALID_HANDLE);
      56           0 :       return NULL;
      57             :     }
      58             : 
      59         254 :   if (unlikely ((ElfW2(LIBELFBITS,Word)) count != count))
      60             :     {
      61           0 :       __libelf_seterrno (ELF_E_INVALID_OPERAND);
      62           0 :       return NULL;
      63             :     }
      64             : 
      65             :   rwlock_wrlock (elf->lock);
      66             : 
      67         254 :   if (elf->class == 0)
      68           0 :     elf->class = ELFW(ELFCLASS,LIBELFBITS);
      69         254 :   else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
      70             :     {
      71           0 :       __libelf_seterrno (ELF_E_INVALID_CLASS);
      72           0 :       result = NULL;
      73           0 :       goto out;
      74             :     }
      75             : 
      76         254 :   if (unlikely (elf->state.ELFW(elf,LIBELFBITS).ehdr == NULL))
      77             :     {
      78           0 :       __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
      79           0 :       result = NULL;
      80           0 :       goto out;
      81             :     }
      82             : 
      83             :   /* A COUNT of zero means remove existing table.  */
      84         254 :   if (count == 0)
      85             :     {
      86             :       /* Free the old program header.  */
      87           0 :       if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
      88             :         {
      89           0 :           if (elf->state.ELFW(elf,LIBELFBITS).phdr_flags & ELF_F_MALLOCED)
      90           0 :             free (elf->state.ELFW(elf,LIBELFBITS).phdr);
      91             : 
      92             :           /* Set the pointer to NULL.  */
      93           0 :           elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
      94             :           /* Set the `e_phnum' member to the new value.  */
      95           0 :           elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = 0;
      96             :           /* Also clear any old PN_XNUM extended value.  */
      97           0 :           if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt > 0)
      98             :             elf->state.ELFW(elf,LIBELFBITS).scns.data[0]
      99           0 :               .shdr.ELFW(e,LIBELFBITS)->sh_info = 0;
     100             :           /* Also set the size.  */
     101           0 :           elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
     102             :             sizeof (ElfW2(LIBELFBITS,Phdr));
     103             : 
     104           0 :           elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
     105           0 :           elf->flags |= ELF_F_DIRTY;
     106           0 :           __libelf_seterrno (ELF_E_NOERROR);
     107             :         }
     108             : 
     109             :       result = NULL;
     110             :     }
     111         254 :   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
     112          21 :            || count == PN_XNUM
     113          21 :            || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
     114             :     {
     115         243 :       if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
     116             :         {
     117           0 :           __libelf_seterrno (ELF_E_INVALID_INDEX);
     118           0 :           result = NULL;
     119           0 :           goto out;
     120             :         }
     121             : 
     122         243 :       Elf_Scn *scn0 = &elf->state.ELFW(elf,LIBELFBITS).scns.data[0];
     123         243 :       if (unlikely (count >= PN_XNUM && scn0->shdr.ELFW(e,LIBELFBITS) == NULL))
     124             :         {
     125             :           /* Something is wrong with section zero, but we need it to write
     126             :              the extended phdr count.  */
     127           0 :           __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
     128           0 :           result = NULL;
     129           0 :           goto out;
     130             :         }
     131             : 
     132             :       /* Allocate a new program header with the appropriate number of
     133             :          elements.  */
     134         243 :       result = (ElfW2(LIBELFBITS,Phdr) *)
     135         243 :         realloc (elf->state.ELFW(elf,LIBELFBITS).phdr,
     136             :                  count * sizeof (ElfW2(LIBELFBITS,Phdr)));
     137         243 :       if (result == NULL)
     138           0 :         __libelf_seterrno (ELF_E_NOMEM);
     139             :       else
     140             :         {
     141             :           /* Now set the result.  */
     142         243 :           elf->state.ELFW(elf,LIBELFBITS).phdr = result;
     143         243 :           if (count >= PN_XNUM)
     144             :             {
     145             :               /* We have to write COUNT into the zeroth section's sh_info.  */
     146           0 :               if (elf->state.ELFW(elf,LIBELFBITS).scns.cnt == 0)
     147             :                 {
     148           0 :                   assert (elf->state.ELFW(elf,LIBELFBITS).scns.max > 0);
     149           0 :                   elf->state.ELFW(elf,LIBELFBITS).scns.cnt = 1;
     150             :                 }
     151           0 :               scn0->shdr.ELFW(e,LIBELFBITS)->sh_info = count;
     152           0 :               scn0->shdr_flags |= ELF_F_DIRTY;
     153           0 :               elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = PN_XNUM;
     154             :             }
     155             :           else
     156             :             /* Set the `e_phnum' member to the new value.  */
     157         243 :             elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum = count;
     158             :           /* Clear the whole memory.  */
     159         243 :           memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
     160             :           /* Also set the size.  */
     161         486 :           elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize =
     162         243 :             elf_typesize (LIBELFBITS, ELF_T_PHDR, 1);
     163             :           /* Remember we allocated the array and mark the structure is
     164             :              modified.  */
     165         243 :           elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
     166             :             ELF_F_DIRTY | ELF_F_MALLOCED;
     167             :           /* We have to rewrite the entire file if the size of the
     168             :              program header is changed.  */
     169         243 :           elf->flags |= ELF_F_DIRTY;
     170             :         }
     171             :     }
     172             :   else
     173             :     {
     174             :       /* We have the same number of entries.  Just clear the array.  */
     175          11 :       assert (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phentsize
     176             :               == elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
     177             : 
     178             :       /* Mark the structure as modified.  */
     179          11 :       elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_DIRTY;
     180             : 
     181          11 :       result = elf->state.ELFW(elf,LIBELFBITS).phdr;
     182          11 :       memset (result, '\0', count * sizeof (ElfW2(LIBELFBITS,Phdr)));
     183             :     }
     184             : 
     185             :  out:
     186             :   rwlock_unlock (elf->lock);
     187             : 
     188         254 :   return result;
     189             : }
     190             : INTDEF(elfw2(LIBELFBITS,newphdr))

Generated by: LCOV version 1.13