LCOV - code coverage report
Current view: top level - libdw - dwarf_child.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 57 68 83.8 %
Date: 2024-03-01 16:42:08 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 34 48 70.8 %

           Branch data     Line data    Source code
       1                 :            : /* Return child of current DIE.
       2                 :            :    Copyright (C) 2003-2011, 2014, 2017 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 <string.h>
      36                 :            : 
      37                 :            : /* Some arbitrary value not conflicting with any existing code.  */
      38                 :            : #define INVALID 0xffffe444
      39                 :            : 
      40                 :            : 
      41                 :            : unsigned char *
      42                 :            : internal_function
      43                 :   18023126 : __libdw_find_attr (Dwarf_Die *die, unsigned int search_name,
      44                 :            :                    unsigned int *codep, unsigned int *formp)
      45                 :            : {
      46                 :   18023126 :   const unsigned char *readp = NULL;
      47                 :            : 
      48                 :            :   /* Find the abbreviation entry.  */
      49                 :   18023126 :   Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &readp);
      50         [ +  + ]:   18023126 :   if (unlikely (abbrevp == DWARF_END_ABBREV))
      51                 :            :     {
      52                 :      11520 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
      53                 :      11520 :       return NULL;
      54                 :            :     }
      55                 :            : 
      56                 :   18011606 :   const unsigned char *endp = die->cu->endp;
      57                 :            : 
      58                 :            :   /* Search the name attribute.  Attribute has been checked when
      59                 :            :      Dwarf_Abbrev was created, we can read unchecked.  */
      60                 :   18011606 :   const unsigned char *attrp = abbrevp->attrp;
      61                 :            :   while (1)
      62                 :   65154182 :     {
      63                 :            :       /* Get attribute name and form.  */
      64                 :            :       unsigned int attr_name;
      65                 :   83165788 :       get_uleb128_unchecked (attr_name, attrp);
      66                 :            : 
      67                 :            :       unsigned int attr_form;
      68                 :   83165788 :       get_uleb128_unchecked (attr_form, attrp);
      69                 :            : 
      70                 :            :       /* We can stop if we found the attribute with value zero.  */
      71   [ +  +  +  - ]:   83165788 :       if (attr_name == 0 && attr_form == 0)
      72                 :   14432036 :         break;
      73                 :            : 
      74         [ +  + ]:   68733752 :       if (attr_form == DW_FORM_indirect)
      75                 :            :         {
      76         [ -  + ]:         34 :           if (readp >= endp)
      77                 :          0 :             goto invalid;
      78                 :         34 :           get_uleb128 (attr_form, readp, endp);
      79   [ -  +  -  + ]:         34 :           if (attr_form == DW_FORM_indirect ||
      80                 :            :               attr_form == DW_FORM_implicit_const)
      81                 :            :             {
      82                 :          0 :             invalid:
      83                 :          0 :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
      84                 :          0 :               return NULL;
      85                 :            :             }
      86                 :            :         }
      87                 :            : 
      88                 :            :       /* Is this the name attribute?  */
      89   [ +  +  +  - ]:   68733752 :       if (attr_name == search_name && search_name != INVALID)
      90                 :            :         {
      91         [ +  - ]:    3579570 :           if (codep != NULL)
      92                 :    3579570 :             *codep = attr_name;
      93         [ +  - ]:    3579570 :           if (formp != NULL)
      94                 :    3579570 :             *formp = attr_form;
      95                 :            : 
      96                 :            :           /* Normally the attribute data comes from the DIE/info,
      97                 :            :              except for implicit_form, where it comes from the abbrev.  */
      98         [ +  + ]:    3579570 :           if (attr_form == DW_FORM_implicit_const)
      99                 :      28480 :             return (unsigned char *) attrp;
     100                 :            :           else
     101                 :    3551090 :             return (unsigned char *) readp;
     102                 :            :         }
     103                 :            : 
     104                 :            :       /* Skip over the rest of this attribute (if there is any).  */
     105         [ +  - ]:   65154182 :       if (attr_form != 0)
     106                 :            :         {
     107                 :   65154182 :           size_t len = __libdw_form_val_len (die->cu, attr_form, readp);
     108         [ -  + ]:   65154182 :           if (unlikely (len == (size_t) -1l))
     109                 :            :             {
     110                 :          0 :               readp = NULL;
     111                 :          0 :               break;
     112                 :            :             }
     113                 :            : 
     114                 :            :           // __libdw_form_val_len will have done a bounds check.
     115                 :   65154182 :           readp += len;
     116                 :            : 
     117                 :            :           // If the value is in the abbrev data, skip it.
     118         [ +  + ]:   65154182 :           if (attr_form == DW_FORM_implicit_const)
     119                 :            :             {
     120                 :            :               int64_t attr_value __attribute__((__unused__));
     121                 :    4544374 :               get_sleb128_unchecked (attr_value, attrp);
     122                 :            :             }
     123                 :            :         }
     124                 :            :     }
     125                 :            : 
     126                 :            :   // XXX Do we need other values?
     127         [ +  + ]:   14432036 :   if (codep != NULL)
     128                 :   13756584 :     *codep = INVALID;
     129         [ +  + ]:   14432036 :   if (formp != NULL)
     130                 :   13756584 :     *formp = INVALID;
     131                 :            : 
     132                 :   14432036 :   return (unsigned char *) readp;
     133                 :            : }
     134                 :            : 
     135                 :            : 
     136                 :            : int
     137                 :    3244940 : dwarf_child (Dwarf_Die *die, Dwarf_Die *result)
     138                 :            : {
     139                 :            :   /* Ignore previous errors.  */
     140         [ -  + ]:    3244940 :   if (die == NULL)
     141                 :          0 :     return -1;
     142                 :            : 
     143                 :            :   /* Find the abbreviation entry.  */
     144                 :    3244940 :   Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, NULL);
     145         [ +  + ]:    3244940 :   if (unlikely (abbrevp == DWARF_END_ABBREV))
     146                 :            :     {
     147                 :       2304 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     148                 :       2304 :       return -1;
     149                 :            :     }
     150                 :            : 
     151                 :            :   /* If there are no children, do not search.  */
     152         [ +  + ]:    3242636 :   if (! abbrevp->has_children)
     153                 :    2567184 :     return 1;
     154                 :            : 
     155                 :            :   /* Skip past the last attribute.  */
     156                 :     675452 :   void *addr = __libdw_find_attr (die, INVALID, NULL, NULL);
     157                 :            : 
     158         [ -  + ]:     675452 :   if (addr == NULL)
     159                 :          0 :     return -1;
     160                 :            : 
     161                 :            :   /* RESULT can be the same as DIE.  So preserve what we need.  */
     162                 :     675452 :   struct Dwarf_CU *cu = die->cu;
     163                 :            : 
     164                 :            :   /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3).
     165                 :            :      So if this starts with ULEB128 of 0 (even with silly encoding of 0),
     166                 :            :      it is a kosher null entry and we do not really have any children.  */
     167                 :     675452 :   const unsigned char *code = addr;
     168                 :     675452 :   const unsigned char *endp = cu->endp;
     169                 :            :   while (1)
     170                 :            :     {
     171         [ -  + ]:     675452 :       if (unlikely (code >= endp)) /* Truncated section.  */
     172                 :          0 :         return 1;
     173         [ -  + ]:     675452 :       if (unlikely (*code == 0x80))
     174                 :          0 :         ++code;
     175                 :            :       else
     176                 :     675452 :         break;
     177                 :            :     }
     178         [ -  + ]:     675452 :   if (unlikely (*code == '\0'))
     179                 :          0 :     return 1;
     180                 :            : 
     181                 :            :   /* Clear the entire DIE structure.  This signals we have not yet
     182                 :            :      determined any of the information.  */
     183                 :     675452 :   memset (result, '\0', sizeof (Dwarf_Die));
     184                 :            : 
     185                 :            :   /* We have the address.  */
     186                 :     675452 :   result->addr = addr;
     187                 :            : 
     188                 :            :   /* Same CU as the parent.  */
     189                 :     675452 :   result->cu = cu;
     190                 :            : 
     191                 :     675452 :   return 0;
     192                 :            : }
     193                 :            : INTDEF(dwarf_child)

Generated by: LCOV version 1.14