LCOV - code coverage report
Current view: top level - libdw - dwarf_siblingof.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 42 45 93.3 %
Date: 2024-04-17 19:42:11 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 25 30 83.3 %

           Branch data     Line data    Source code
       1                 :            : /* Return sibling of given DIE.
       2                 :            :    Copyright (C) 2003-2010, 2014, 2015 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            :    Written by Ulrich Drepper <drepper@redhat.com>, 2003.
       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 "libdwP.h"
      35                 :            : #include <dwarf.h>
      36                 :            : #include <string.h>
      37                 :            : 
      38                 :            : 
      39                 :            : int
      40                 :   25272334 : dwarf_siblingof (Dwarf_Die *die, Dwarf_Die *result)
      41                 :            : {
      42                 :            :   /* Ignore previous errors.  */
      43         [ -  + ]:   25272334 :   if (die == NULL)
      44                 :            :     return -1;
      45                 :            : 
      46                 :            :   /* result is declared NN */
      47                 :            : 
      48         [ +  + ]:   25272334 :   if (result != die)
      49                 :    2687412 :     result->addr = NULL;
      50                 :            : 
      51                 :   25272334 :   unsigned int level = 0;
      52                 :            : 
      53                 :            :   /* Copy of the current DIE.  */
      54                 :   25272334 :   Dwarf_Die this_die = *die;
      55                 :            :   /* Temporary attributes we create.  */
      56                 :   25272334 :   Dwarf_Attribute sibattr;
      57                 :            :   /* Copy of the CU in the request.  */
      58                 :   25272334 :   sibattr.cu = this_die.cu;
      59                 :            :   /* That's the address we start looking.  */
      60                 :   31439172 :   unsigned char *addr;
      61                 :            : 
      62                 :            :   /* Search for the beginning of the next die on this level.  We
      63                 :            :      must not return the dies for children of the given die.  */
      64                 :   31439172 :   do
      65                 :            :     {
      66                 :            :       /* Find the end of the DIE or the sibling attribute.  */
      67                 :   31439172 :       addr = __libdw_find_attr (&this_die, DW_AT_sibling, &sibattr.code,
      68                 :            :                                 &sibattr.form);
      69   [ +  +  +  + ]:   31439172 :       if (addr != NULL && sibattr.code == DW_AT_sibling)
      70                 :    5051882 :         {
      71                 :    5051882 :           Dwarf_Off offset;
      72                 :    5051882 :           sibattr.valp = addr;
      73         [ +  - ]:    5051882 :           if (unlikely (__libdw_formref (&sibattr, &offset) != 0))
      74                 :            :             /* Something went wrong.  */
      75                 :          0 :             return -1;
      76                 :            : 
      77                 :            :           /* The sibling attribute should point after this DIE in the CU.
      78                 :            :              But not after the end of the CU.  */
      79                 :    5051882 :           size_t size = sibattr.cu->endp - sibattr.cu->startp;
      80                 :    5051882 :           size_t die_off = this_die.addr - this_die.cu->startp;
      81   [ +  -  -  + ]:    5051882 :           if (unlikely (offset >= size || offset <= die_off))
      82                 :            :             {
      83                 :          0 :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
      84                 :          0 :               return -1;
      85                 :            :             }
      86                 :            : 
      87                 :            :           /* Compute the next address.  */
      88                 :    5051882 :           addr = sibattr.cu->startp + offset;
      89                 :            :         }
      90         [ +  + ]:   26387290 :       else if (unlikely (addr == NULL)
      91         [ -  + ]:   26384932 :                || unlikely (this_die.abbrev == DWARF_END_ABBREV))
      92                 :            :         return -1;
      93         [ +  + ]:   26384932 :       else if (this_die.abbrev->has_children)
      94                 :            :         /* This abbreviation has children.  */
      95                 :    2007688 :         ++level;
      96                 :            : 
      97                 :            :       /* End of the buffer.  */
      98                 :   31436814 :       unsigned char *endp = sibattr.cu->endp;
      99                 :            : 
     100                 :   35452190 :       while (1)
     101                 :            :         {
     102                 :            :           /* Make sure we are still in range.  Some producers might skip
     103                 :            :              the trailing NUL bytes.  */
     104         [ +  + ]:   33444502 :           if (addr >= endp)
     105                 :            :             return 1;
     106                 :            : 
     107         [ +  + ]:   33436008 :           if (*addr != '\0')
     108                 :            :             break;
     109                 :            : 
     110         [ +  + ]:    5560324 :           if (level-- == 0)
     111                 :            :             {
     112         [ +  + ]:    3552636 :               if (result != die)
     113                 :     364466 :                 result->addr = addr;
     114                 :            :               /* No more sibling at all.  */
     115                 :    3552636 :               return 1;
     116                 :            :             }
     117                 :            : 
     118                 :    2007688 :           ++addr;
     119                 :            :         }
     120                 :            : 
     121                 :            :       /* Initialize the 'current DIE'.  */
     122                 :   27875684 :       this_die.addr = addr;
     123                 :   27875684 :       this_die.abbrev = NULL;
     124                 :            :     }
     125         [ +  + ]:   27875684 :   while (level > 0);
     126                 :            : 
     127                 :            :   /* Maybe we reached the end of the CU.  */
     128                 :   21708846 :   unsigned char *endp = sibattr.cu->endp;
     129                 :   21708846 :   if (addr >= endp)
     130                 :            :     return 1;
     131                 :            : 
     132                 :            :   /* Clear the entire DIE structure.  This signals we have not yet
     133                 :            :      determined any of the information.  */
     134                 :   21708846 :   memset (result, '\0', sizeof (Dwarf_Die));
     135                 :            : 
     136                 :            :   /* We have the address.  */
     137                 :   21708846 :   result->addr = addr;
     138                 :            : 
     139                 :            :   /* Same CU as the parent.  */
     140                 :   21708846 :   result->cu = sibattr.cu;
     141                 :            : 
     142                 :   21708846 :   return 0;
     143                 :            : }
     144                 :            : INTDEF(dwarf_siblingof)

Generated by: LCOV version 1.16