LCOV - code coverage report
Current view: top level - libdw - libdw_findcu.c (source / functions) Hit Total Coverage
Test: elfutils-0.189 Lines: 130 141 92.2 %
Date: 2023-04-14 15:57:06 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 72 92 78.3 %

           Branch data     Line data    Source code
       1                 :            : /* Find CU for given offset.
       2                 :            :    Copyright (C) 2003-2010, 2014, 2016, 2017, 2018 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 <assert.h>
      35                 :            : #include <search.h>
      36                 :            : #include "libdwP.h"
      37                 :            : 
      38                 :            : static int
      39                 :    6628293 : findcu_cb (const void *arg1, const void *arg2)
      40                 :            : {
      41                 :    6628293 :   struct Dwarf_CU *cu1 = (struct Dwarf_CU *) arg1;
      42                 :    6628293 :   struct Dwarf_CU *cu2 = (struct Dwarf_CU *) arg2;
      43                 :            : 
      44                 :            :   /* Find out which of the two arguments is the search value.  It has
      45                 :            :      end offset 0.  */
      46         [ +  + ]:    6628293 :   if (cu1->end == 0)
      47                 :            :     {
      48         [ +  + ]:    6405246 :       if (cu1->start < cu2->start)
      49                 :            :         return -1;
      50         [ +  + ]:    6391936 :       if (cu1->start >= cu2->end)
      51                 :    5758974 :         return 1;
      52                 :            :     }
      53                 :            :   else
      54                 :            :     {
      55         [ -  + ]:     223047 :       if (cu2->start < cu1->start)
      56                 :            :         return 1;
      57         [ #  # ]:          0 :       if (cu2->start >= cu1->end)
      58                 :          0 :         return -1;
      59                 :            :     }
      60                 :            : 
      61                 :            :   return 0;
      62                 :            : }
      63                 :            : 
      64                 :            : int
      65                 :        237 : __libdw_finddbg_cb (const void *arg1, const void *arg2)
      66                 :            : {
      67                 :        237 :   Dwarf *dbg1 = (Dwarf *) arg1;
      68                 :        237 :   Dwarf *dbg2 = (Dwarf *) arg2;
      69                 :            : 
      70                 :        237 :   Elf_Data *dbg1_data = dbg1->sectiondata[IDX_debug_info];
      71                 :        237 :   unsigned char *dbg1_start = dbg1_data->d_buf;
      72                 :        237 :   size_t dbg1_size = dbg1_data->d_size;
      73                 :            : 
      74                 :        237 :   Elf_Data *dbg2_data = dbg2->sectiondata[IDX_debug_info];
      75                 :        237 :   unsigned char *dbg2_start = dbg2_data->d_buf;
      76                 :        237 :   size_t dbg2_size = dbg2_data->d_size;
      77                 :            : 
      78                 :            :   /* Find out which of the two arguments is the search value.  It has
      79                 :            :      a size of 0.  */
      80         [ +  + ]:        237 :   if (dbg1_size == 0)
      81                 :            :     {
      82         [ +  - ]:        220 :       if (dbg1_start < dbg2_start)
      83                 :            :         return -1;
      84         [ +  + ]:        220 :       if (dbg1_start >= dbg2_start + dbg2_size)
      85                 :         72 :         return 1;
      86                 :            :     }
      87                 :            :   else
      88                 :            :     {
      89         [ +  + ]:         17 :       if (dbg2_start < dbg1_start)
      90                 :            :         return 1;
      91         [ +  - ]:          4 :       if (dbg2_start >= dbg1_start + dbg1_size)
      92                 :          4 :         return -1;
      93                 :            :     }
      94                 :            : 
      95                 :            :   return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : struct Dwarf_CU *
      99                 :            : internal_function
     100                 :      22421 : __libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
     101                 :            : {
     102                 :      44842 :   Dwarf_Off *const offsetp
     103         [ +  + ]:      22421 :     = debug_types ? &dbg->next_tu_offset : &dbg->next_cu_offset;
     104         [ +  + ]:      22421 :   void **tree = debug_types ? &dbg->tu_tree : &dbg->cu_tree;
     105                 :            : 
     106                 :      22421 :   Dwarf_Off oldoff = *offsetp;
     107                 :      22421 :   uint16_t version;
     108                 :      22421 :   uint8_t unit_type;
     109                 :      22421 :   uint8_t address_size;
     110                 :      22421 :   uint8_t offset_size;
     111                 :      22421 :   Dwarf_Off abbrev_offset;
     112                 :      22421 :   uint64_t unit_id8;
     113                 :      22421 :   Dwarf_Off subdie_offset;
     114                 :            : 
     115         [ +  + ]:      22421 :   if (__libdw_next_unit (dbg, debug_types, oldoff, offsetp, NULL,
     116                 :            :                          &version, &unit_type, &abbrev_offset,
     117                 :            :                          &address_size, &offset_size,
     118                 :            :                          &unit_id8, &subdie_offset) != 0)
     119                 :            :     /* No more entries.  */
     120                 :            :     return NULL;
     121                 :            : 
     122                 :            :   /* We only know how to handle the DWARF version 2 through 5 formats.
     123                 :            :      For v4 debug types we only handle version 4.  */
     124   [ +  -  +  - ]:      22416 :   if (unlikely (version < 2) || unlikely (version > 5)
     125   [ +  +  -  + ]:      22416 :       || (debug_types && unlikely (version != 4)))
     126                 :            :     {
     127                 :          0 :       __libdw_seterrno (DWARF_E_VERSION);
     128                 :          0 :       return NULL;
     129                 :            :     }
     130                 :            : 
     131                 :            :   /* We only handle 32 or 64 bit (4 or 8 byte) addresses and offsets.
     132                 :            :      Just assume we are dealing with 64bit in case the size is "unknown".
     133                 :            :      Too much code assumes if it isn't 4 then it is 8 (or the other way
     134                 :            :      around).  */
     135         [ -  + ]:      22416 :   if (unlikely (address_size != 4 && address_size != 8))
     136                 :          0 :     address_size = 8;
     137         [ -  + ]:      22416 :   if (unlikely (offset_size != 4 && offset_size != 8))
     138                 :          0 :     offset_size = 8;
     139                 :            : 
     140                 :            :   /* Invalid or truncated debug section data?  */
     141                 :      22416 :   size_t sec_idx = debug_types ? IDX_debug_types : IDX_debug_info;
     142                 :      22416 :   Elf_Data *data = dbg->sectiondata[sec_idx];
     143         [ -  + ]:      22416 :   if (unlikely (*offsetp > data->d_size))
     144                 :          0 :     *offsetp = data->d_size;
     145                 :            : 
     146                 :            :   /* Create an entry for this CU.  */
     147         [ +  + ]:      22416 :   struct Dwarf_CU *newp = libdw_typed_alloc (dbg, struct Dwarf_CU);
     148                 :            : 
     149                 :      22416 :   newp->dbg = dbg;
     150                 :      22416 :   newp->sec_idx = sec_idx;
     151                 :      22416 :   newp->start = oldoff;
     152                 :      22416 :   newp->end = *offsetp;
     153                 :      22416 :   newp->address_size = address_size;
     154                 :      22416 :   newp->offset_size = offset_size;
     155                 :      22416 :   newp->version = version;
     156                 :      22416 :   newp->unit_id8 = unit_id8;
     157                 :      22416 :   newp->subdie_offset = subdie_offset;
     158                 :      22416 :   Dwarf_Abbrev_Hash_init (&newp->abbrev_hash, 41);
     159                 :      22416 :   newp->orig_abbrev_offset = newp->last_abbrev_offset = abbrev_offset;
     160                 :      22416 :   newp->files = NULL;
     161                 :      22416 :   newp->lines = NULL;
     162                 :      22416 :   newp->locs = NULL;
     163                 :      22416 :   newp->split = (Dwarf_CU *) -1;
     164                 :      22416 :   newp->base_address = (Dwarf_Addr) -1;
     165                 :      22416 :   newp->addr_base = (Dwarf_Off) -1;
     166                 :      22416 :   newp->str_off_base = (Dwarf_Off) -1;
     167                 :      22416 :   newp->ranges_base = (Dwarf_Off) -1;
     168                 :      22416 :   newp->locs_base = (Dwarf_Off) -1;
     169                 :            : 
     170                 :      22416 :   newp->startp = data->d_buf + newp->start;
     171                 :      22416 :   newp->endp = data->d_buf + newp->end;
     172                 :            : 
     173                 :            :   /* v4 debug type units have version == 4 and unit_type == DW_UT_type.  */
     174         [ +  + ]:      22416 :   if (debug_types)
     175                 :         18 :     newp->unit_type = DW_UT_type;
     176         [ +  + ]:      22398 :   else if (version < 5)
     177                 :            :     {
     178                 :            :       /* This is a reasonable guess (and needed to get the CUDIE).  */
     179                 :      22302 :       newp->unit_type = DW_UT_compile;
     180                 :            : 
     181                 :            :       /* But set it correctly from the actual CUDIE tag.  */
     182                 :      22302 :       Dwarf_Die cudie = CUDIE (newp);
     183                 :      22302 :       int tag = INTUSE(dwarf_tag) (&cudie);
     184         [ +  + ]:      22302 :       if (tag == DW_TAG_compile_unit)
     185                 :            :         {
     186                 :      22277 :           Dwarf_Attribute dwo_id;
     187         [ +  + ]:      22277 :           if (INTUSE(dwarf_attr) (&cudie, DW_AT_GNU_dwo_id, &dwo_id) != NULL)
     188                 :            :             {
     189                 :         71 :               Dwarf_Word id8;
     190         [ +  - ]:         71 :               if (INTUSE(dwarf_formudata) (&dwo_id, &id8) == 0)
     191                 :            :                 {
     192         [ +  + ]:         71 :                   if (INTUSE(dwarf_haschildren) (&cudie) == 0
     193         [ +  - ]:         34 :                       && INTUSE(dwarf_hasattr) (&cudie,
     194                 :            :                                                 DW_AT_GNU_dwo_name) == 1)
     195                 :         34 :                     newp->unit_type = DW_UT_skeleton;
     196                 :            :                   else
     197                 :         37 :                     newp->unit_type = DW_UT_split_compile;
     198                 :            : 
     199                 :         71 :                   newp->unit_id8 = id8;
     200                 :            :                 }
     201                 :            :             }
     202                 :            :         }
     203         [ +  + ]:         25 :       else if (tag == DW_TAG_partial_unit)
     204                 :         23 :         newp->unit_type = DW_UT_partial;
     205         [ -  + ]:          2 :       else if (tag == DW_TAG_type_unit)
     206                 :          0 :         newp->unit_type = DW_UT_type;
     207                 :            :     }
     208                 :            :   else
     209                 :         96 :     newp->unit_type = unit_type;
     210                 :            : 
     211                 :            :   /* Store a reference to any type unit ids in the hash for quick lookup.  */
     212         [ +  + ]:      22416 :   if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
     213                 :         18 :     Dwarf_Sig8_Hash_insert (&dbg->sig8_hash, unit_id8, newp);
     214                 :            : 
     215                 :            :   /* Add the new entry to the search tree.  */
     216         [ -  + ]:      22416 :   if (tsearch (newp, tree, findcu_cb) == NULL)
     217                 :            :     {
     218                 :            :       /* Something went wrong.  Undo the operation.  */
     219                 :          0 :       *offsetp = oldoff;
     220                 :          0 :       __libdw_seterrno (DWARF_E_NOMEM);
     221                 :          0 :       return NULL;
     222                 :            :     }
     223                 :            : 
     224                 :            :   return newp;
     225                 :            : }
     226                 :            : 
     227                 :            : struct Dwarf_CU *
     228                 :            : internal_function
     229                 :      39170 : __libdw_findcu (Dwarf *dbg, Dwarf_Off start, bool v4_debug_types)
     230                 :            : {
     231         [ +  + ]:      39170 :   void **tree = v4_debug_types ? &dbg->tu_tree : &dbg->cu_tree;
     232                 :      78340 :   Dwarf_Off *next_offset
     233         [ +  + ]:      39170 :     = v4_debug_types ? &dbg->next_tu_offset : &dbg->next_cu_offset;
     234                 :            : 
     235                 :            :   /* Maybe we already know that CU.  */
     236                 :      39170 :   struct Dwarf_CU fake = { .start = start, .end = 0 };
     237                 :      39170 :   struct Dwarf_CU **found = tfind (&fake, tree, findcu_cb);
     238         [ +  + ]:      39170 :   if (found != NULL)
     239                 :      16751 :     return *found;
     240                 :            : 
     241         [ +  + ]:      22419 :   if (start < *next_offset)
     242                 :            :     {
     243                 :          5 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     244                 :          5 :       return NULL;
     245                 :            :     }
     246                 :            : 
     247                 :            :   /* No.  Then read more CUs.  */
     248                 :      22419 :   while (1)
     249                 :            :     {
     250                 :      22419 :       struct Dwarf_CU *newp = __libdw_intern_next_unit (dbg, v4_debug_types);
     251         [ +  + ]:      22419 :       if (newp == NULL)
     252                 :            :         return NULL;
     253                 :            : 
     254                 :            :       /* Is this the one we are looking for?  */
     255   [ +  +  +  - ]:      22415 :       if (start < *next_offset || start == newp->start)
     256                 :      22410 :         return newp;
     257                 :            :     }
     258                 :            :   /* NOTREACHED */
     259                 :            : }
     260                 :            : 
     261                 :            : struct Dwarf_CU *
     262                 :            : internal_function
     263                 :     616374 : __libdw_findcu_addr (Dwarf *dbg, void *addr)
     264                 :            : {
     265                 :     616374 :   void **tree;
     266                 :     616374 :   Dwarf_Off start;
     267         [ +  + ]:     616374 :   if (addr >= dbg->sectiondata[IDX_debug_info]->d_buf
     268                 :     616226 :       && addr < (dbg->sectiondata[IDX_debug_info]->d_buf
     269         [ +  + ]:     616226 :                  + dbg->sectiondata[IDX_debug_info]->d_size))
     270                 :            :     {
     271                 :     616195 :       tree = &dbg->cu_tree;
     272                 :     616195 :       start = addr - dbg->sectiondata[IDX_debug_info]->d_buf;
     273                 :            :     }
     274         [ +  + ]:        179 :   else if (dbg->sectiondata[IDX_debug_types] != NULL
     275         [ +  - ]:         16 :            && addr >= dbg->sectiondata[IDX_debug_types]->d_buf
     276                 :         16 :            && addr < (dbg->sectiondata[IDX_debug_types]->d_buf
     277         [ +  - ]:         16 :                       + dbg->sectiondata[IDX_debug_types]->d_size))
     278                 :            :     {
     279                 :         16 :       tree = &dbg->tu_tree;
     280                 :         16 :       start = addr - dbg->sectiondata[IDX_debug_types]->d_buf;
     281                 :            :     }
     282                 :            :   else
     283                 :            :     return NULL;
     284                 :            : 
     285                 :     616211 :   struct Dwarf_CU fake = { .start = start, .end = 0 };
     286                 :     616211 :   struct Dwarf_CU **found = tfind (&fake, tree, findcu_cb);
     287                 :            : 
     288         [ +  - ]:     616211 :   if (found != NULL)
     289                 :     616211 :     return *found;
     290                 :            : 
     291                 :            :   return NULL;
     292                 :            : }
     293                 :            : 
     294                 :            : Dwarf *
     295                 :            : internal_function
     296                 :        148 : __libdw_find_split_dbg_addr (Dwarf *dbg, void *addr)
     297                 :            : {
     298                 :            :   /* XXX Assumes split DWARF only has CUs in main IDX_debug_info.  */
     299                 :        148 :   Elf_Data fake_data = { .d_buf = addr, .d_size = 0 };
     300                 :        148 :   Dwarf fake = { .sectiondata[IDX_debug_info] = &fake_data };
     301                 :        148 :   Dwarf **found = tfind (&fake, &dbg->split_tree, __libdw_finddbg_cb);
     302                 :            : 
     303         [ +  - ]:        148 :   if (found != NULL)
     304                 :        148 :     return *found;
     305                 :            : 
     306                 :            :   return NULL;
     307                 :            : }

Generated by: LCOV version 1.14