LCOV - code coverage report
Current view: top level - libdwelf - dwelf_elf_gnu_debuglink.c (source / functions) Coverage Total Hit
Test: elfutils-0.193 Lines: 96.2 % 26 25
Test Date: 2025-08-30 14:31:09 Functions: 100.0 % 1 1
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 61.5 % 26 16

             Branch data     Line data    Source code
       1                 :             : /* Returns the file name and crc stored in the .gnu_debuglink if found.
       2                 :             :    Copyright (C) 2014 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 "libdwelfP.h"
      34                 :             : 
      35                 :             : const char *
      36                 :         454 : dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc)
      37                 :             : {
      38                 :         454 :   size_t shstrndx;
      39         [ -  + ]:         454 :   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
      40                 :             :     return NULL;
      41                 :             : 
      42                 :             :   Elf_Scn *scn = NULL;
      43         [ +  + ]:       11146 :   while ((scn = elf_nextscn (elf, scn)) != NULL)
      44                 :             :     {
      45                 :       11006 :       GElf_Shdr shdr_mem;
      46                 :       11006 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
      47         [ +  - ]:       11006 :       if (shdr == NULL)
      48                 :           0 :         return NULL;
      49                 :             : 
      50                 :       11006 :       const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
      51         [ +  - ]:       11006 :       if (name == NULL)
      52                 :             :         return NULL;
      53                 :             : 
      54         [ +  + ]:       11006 :       if (!strcmp (name, ".gnu_debuglink"))
      55                 :             :         break;
      56                 :             :     }
      57                 :             : 
      58         [ +  + ]:         454 :   if (scn == NULL)
      59                 :             :     return NULL;
      60                 :             : 
      61                 :             :   /* Found the .gnu_debuglink section.  Extract its contents.  */
      62                 :         314 :   Elf_Data *rawdata = elf_rawdata (scn, NULL);
      63   [ -  +  -  + ]:         314 :   if (rawdata == NULL || rawdata->d_buf == NULL)
      64                 :             :     return NULL;
      65                 :             : 
      66                 :             :   /* The CRC comes after the zero-terminated file name,
      67                 :             :      (aligned up to 4 bytes) at the end of the section data.  */
      68         [ -  + ]:         314 :   if (rawdata->d_size <= sizeof *crc
      69         [ -  + ]:         314 :       || memchr (rawdata->d_buf, '\0', rawdata->d_size - sizeof *crc) == NULL)
      70                 :             :     return NULL;
      71                 :             : 
      72                 :         314 :   Elf_Data crcdata =
      73                 :             :     {
      74                 :             :       .d_type = ELF_T_WORD,
      75                 :             :       .d_buf = crc,
      76                 :             :       .d_size = sizeof *crc,
      77                 :             :       .d_version = EV_CURRENT,
      78                 :             :     };
      79                 :         314 :   Elf_Data conv =
      80                 :             :     {
      81                 :             :       .d_type = ELF_T_WORD,
      82                 :         314 :       .d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc,
      83                 :             :       .d_size = sizeof *crc,
      84                 :             :       .d_version = EV_CURRENT,
      85                 :             :     };
      86                 :             : 
      87                 :         314 :   GElf_Ehdr ehdr_mem;
      88                 :         314 :   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
      89         [ -  + ]:         314 :   if (ehdr == NULL)
      90                 :             :     return NULL;
      91                 :             : 
      92                 :         314 :   Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]);
      93         [ -  + ]:         314 :   if (d == NULL)
      94                 :             :     return NULL;
      95         [ -  + ]:         314 :   assert (d == &crcdata);
      96                 :             : 
      97                 :         314 :   return rawdata->d_buf;
      98                 :             : }
      99                 :             : INTDEF(dwelf_elf_gnu_debuglink)
        

Generated by: LCOV version 2.0-1