LCOV - code coverage report
Current view: top level - libdwelf - dwelf_elf_gnu_build_id.c (source / functions) Hit Total Coverage
Test: elfutils-0.172 Lines: 41 49 83.7 %
Date: 2018-06-11 22:52:14 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Returns the build id if found in a NT_GNU_BUILD_ID note.
       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             : #include "libdwflP.h"
      35             : 
      36             : #define NO_VADDR        ((GElf_Addr) -1l)
      37             : 
      38             : static int
      39         328 : check_notes (Elf_Data *data, GElf_Addr data_elfaddr,
      40             :              const void **build_id_bits, GElf_Addr *build_id_elfaddr,
      41             :              int *build_id_len)
      42             : {
      43         328 :   size_t pos = 0;
      44             :   GElf_Nhdr nhdr;
      45             :   size_t name_pos;
      46             :   size_t desc_pos;
      47         769 :   while ((pos = gelf_getnote (data, pos, &nhdr, &name_pos, &desc_pos)) > 0)
      48         332 :     if (nhdr.n_type == NT_GNU_BUILD_ID
      49         219 :         && nhdr.n_namesz == sizeof "GNU"
      50         219 :         && !memcmp (data->d_buf + name_pos, "GNU", sizeof "GNU"))
      51             :         {
      52         219 :           *build_id_bits = data->d_buf + desc_pos;
      53         219 :           *build_id_elfaddr = (data_elfaddr == NO_VADDR
      54         219 :                               ? 0 : data_elfaddr + desc_pos);
      55         219 :           *build_id_len = nhdr.n_descsz;
      56         219 :           return 1;
      57             :         }
      58             :   return 0;
      59             : }
      60             : 
      61             : /* Defined here for reuse. The dwelf interface doesn't care about the
      62             :    address of the note, but libdwfl does.  */
      63             : static int
      64         234 : find_elf_build_id (Dwfl_Module *mod, int e_type, Elf *elf,
      65             :                    const void **build_id_bits, GElf_Addr *build_id_elfaddr,
      66             :                    int *build_id_len)
      67             : {
      68         234 :   size_t shstrndx = SHN_UNDEF;
      69         234 :   int result = 0;
      70             : 
      71         234 :   Elf_Scn *scn = elf_nextscn (elf, NULL);
      72             : 
      73         234 :   if (scn == NULL)
      74             :     {
      75             :       /* No sections, have to look for phdrs.  */
      76             :       size_t phnum;
      77           7 :       if (unlikely (elf_getphdrnum (elf, &phnum) != 0))
      78             :         {
      79           0 :           if (mod != NULL)
      80           0 :             __libdwfl_seterrno (DWFL_E_LIBELF);
      81           0 :           return -1;
      82             :         }
      83          29 :       for (size_t i = 0; result == 0 && i < phnum; ++i)
      84             :         {
      85             :           GElf_Phdr phdr_mem;
      86          29 :           GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
      87          29 :           if (likely (phdr != NULL) && phdr->p_type == PT_NOTE)
      88          14 :             result = check_notes (elf_getdata_rawchunk (elf,
      89           7 :                                                         phdr->p_offset,
      90             :                                                         phdr->p_filesz,
      91             :                                                         ELF_T_NHDR),
      92             :                                   phdr->p_vaddr,
      93             :                                   build_id_bits,
      94             :                                   build_id_elfaddr,
      95             :                                   build_id_len);
      96             :         }
      97             :     }
      98             :   else
      99             :     do
     100             :       {
     101             :         GElf_Shdr shdr_mem;
     102         712 :         GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     103         712 :         if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE)
     104             :           {
     105             :             /* Determine the right sh_addr in this module.  */
     106         321 :             GElf_Addr vaddr = 0;
     107         321 :             if (!(shdr->sh_flags & SHF_ALLOC))
     108           8 :               vaddr = NO_VADDR;
     109         313 :             else if (mod == NULL || e_type != ET_REL)
     110         313 :               vaddr = shdr->sh_addr;
     111           0 :             else if (__libdwfl_relocate_value (mod, elf, &shstrndx,
     112           0 :                                                elf_ndxscn (scn), &vaddr))
     113           0 :               vaddr = NO_VADDR;
     114         321 :             result = check_notes (elf_getdata (scn, NULL), vaddr,
     115             :                                   build_id_bits,
     116             :                                   build_id_elfaddr,
     117             :                                   build_id_len);
     118             :           }
     119             :       }
     120         712 :     while (result == 0 && (scn = elf_nextscn (elf, scn)) != NULL);
     121             : 
     122             :   return result;
     123             : }
     124             : 
     125             : int
     126             : internal_function
     127         157 : __libdwfl_find_elf_build_id (Dwfl_Module *mod, Elf *elf,
     128             :                              const void **build_id_bits,
     129             :                              GElf_Addr *build_id_elfaddr, int *build_id_len)
     130             : {
     131         157 :   GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
     132         157 :   if (unlikely (ehdr == NULL))
     133             :     {
     134           0 :       __libdwfl_seterrno (DWFL_E_LIBELF);
     135           0 :       return -1;
     136             :     }
     137             :   // MOD->E_TYPE is zero here.
     138         157 :   assert (ehdr->e_type != ET_REL || mod != NULL);
     139             : 
     140         157 :   return find_elf_build_id (mod, ehdr->e_type, elf,
     141             :                             build_id_bits, build_id_elfaddr, build_id_len);
     142             : }
     143             : 
     144             : ssize_t
     145          77 : dwelf_elf_gnu_build_id (Elf *elf, const void **build_idp)
     146             : {
     147             :   GElf_Addr build_id_elfaddr;
     148             :   int build_id_len;
     149          77 :   int result = find_elf_build_id (NULL, ET_NONE, elf, build_idp,
     150             :                                   &build_id_elfaddr, &build_id_len);
     151          77 :   if (result > 0)
     152          76 :     return build_id_len;
     153             : 
     154           1 :   return result;
     155             : }
     156             : INTDEF(dwelf_elf_gnu_build_id)

Generated by: LCOV version 1.13