LCOV - code coverage report
Current view: top level - libelf - gelf_update_ehdr.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 30 38 78.9 %
Date: 2024-04-17 19:42:11 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 16 56.2 %

           Branch data     Line data    Source code
       1                 :            : /* Update ELF header.
       2                 :            :    Copyright (C) 2000, 2001, 2002, 2010 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            :    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
       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 <string.h>
      36                 :            : 
      37                 :            : #include "libelfP.h"
      38                 :            : 
      39                 :            : 
      40                 :            : int
      41                 :       2804 : gelf_update_ehdr (Elf *elf, GElf_Ehdr *src)
      42                 :            : {
      43                 :       2804 :   int result = 0;
      44                 :            : 
      45         [ -  + ]:       2804 :   if (elf == NULL)
      46                 :            :     return 0;
      47                 :            : 
      48         [ -  + ]:       2804 :   if (unlikely (elf->kind != ELF_K_ELF))
      49                 :            :     {
      50                 :          0 :       __libelf_seterrno (ELF_E_INVALID_HANDLE);
      51                 :          0 :       return 0;
      52                 :            :     }
      53                 :            : 
      54                 :       2804 :   rwlock_wrlock (elf->lock);
      55                 :            : 
      56         [ +  + ]:       2804 :   if (elf->class == ELFCLASS32)
      57                 :            :     {
      58                 :       1186 :       Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
      59                 :            : 
      60         [ -  + ]:       1186 :       if (ehdr == NULL)
      61                 :            :         {
      62                 :          0 :           __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
      63                 :          0 :           goto out;
      64                 :            :         }
      65                 :            : 
      66                 :            :       /* We have to convert the data to the 32 bit format.  This might
      67                 :            :          overflow some fields so we have to test for this case before
      68                 :            :          copying.  */
      69         [ +  - ]:       1186 :       if (unlikely (src->e_entry > 0xffffffffull)
      70         [ +  - ]:       1186 :           || unlikely (src->e_phoff > 0xffffffffull)
      71         [ -  + ]:       1186 :           || unlikely (src->e_shoff > 0xffffffffull))
      72                 :            :         {
      73                 :          0 :           __libelf_seterrno (ELF_E_INVALID_DATA);
      74                 :          0 :           goto out;
      75                 :            :         }
      76                 :            : 
      77                 :            :       /* Copy the data.  */
      78                 :       1186 :       memcpy (ehdr->e_ident, src->e_ident, EI_NIDENT);
      79                 :            : #define COPY(name) \
      80                 :            :       ehdr->name = src->name
      81                 :       1186 :       COPY (e_type);
      82                 :       1186 :       COPY (e_machine);
      83                 :       1186 :       COPY (e_version);
      84                 :       1186 :       COPY (e_entry);
      85                 :       1186 :       COPY (e_phoff);
      86                 :       1186 :       COPY (e_shoff);
      87                 :       1186 :       COPY (e_flags);
      88                 :       1186 :       COPY (e_ehsize);
      89                 :       1186 :       COPY (e_phentsize);
      90                 :       1186 :       COPY (e_phnum);
      91                 :       1186 :       COPY (e_shentsize);
      92                 :       1186 :       COPY (e_shnum);
      93                 :       1186 :       COPY (e_shstrndx);
      94                 :            :     }
      95                 :            :   else
      96                 :            :     {
      97                 :       1618 :       Elf64_Ehdr *ehdr = elf->state.elf64.ehdr;
      98                 :            : 
      99         [ -  + ]:       1618 :       if (ehdr == NULL)
     100                 :            :         {
     101                 :          0 :           __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
     102                 :          0 :           goto out;
     103                 :            :         }
     104                 :            : 
     105                 :            :       /* Just copy the data.  */
     106                 :       1618 :       memcpy (ehdr, src, sizeof (Elf64_Ehdr));
     107                 :            :     }
     108                 :            : 
     109                 :            :   /* Mark the ELF header as modified.  */
     110                 :       2804 :   elf->state.elf.ehdr_flags |= ELF_F_DIRTY;
     111                 :            : 
     112                 :       2804 :   result = 1;
     113                 :            : 
     114                 :            :  out:
     115                 :            :   rwlock_unlock (elf->lock);
     116                 :            : 
     117                 :            :   return result;
     118                 :            : }

Generated by: LCOV version 1.16