LCOV - code coverage report
Current view: top level - libdw - dwarf_getaranges.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 113 168 67.3 %
Date: 2024-06-08 18:04:06 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 57 108 52.8 %

           Branch data     Line data    Source code
       1                 :            : /* Return list address ranges.
       2                 :            :    Copyright (C) 2000-2010, 2016, 2017 Red Hat, Inc.
       3                 :            :    Copyright (C) 2023 Mark J. Wielaard <mark@klomp.org>
       4                 :            :    This file is part of elfutils.
       5                 :            :    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
       6                 :            : 
       7                 :            :    This file is free software; you can redistribute it and/or modify
       8                 :            :    it under the terms of either
       9                 :            : 
      10                 :            :      * the GNU Lesser General Public License as published by the Free
      11                 :            :        Software Foundation; either version 3 of the License, or (at
      12                 :            :        your option) any later version
      13                 :            : 
      14                 :            :    or
      15                 :            : 
      16                 :            :      * the GNU General Public License as published by the Free
      17                 :            :        Software Foundation; either version 2 of the License, or (at
      18                 :            :        your option) any later version
      19                 :            : 
      20                 :            :    or both in parallel, as here.
      21                 :            : 
      22                 :            :    elfutils is distributed in the hope that it will be useful, but
      23                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      24                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      25                 :            :    General Public License for more details.
      26                 :            : 
      27                 :            :    You should have received copies of the GNU General Public License and
      28                 :            :    the GNU Lesser General Public License along with this program.  If
      29                 :            :    not, see <http://www.gnu.org/licenses/>.  */
      30                 :            : 
      31                 :            : #ifdef HAVE_CONFIG_H
      32                 :            : # include <config.h>
      33                 :            : #endif
      34                 :            : 
      35                 :            : #include <stdlib.h>
      36                 :            : #include "libdwP.h"
      37                 :            : #include <dwarf.h>
      38                 :            : 
      39                 :            : struct arangelist
      40                 :            : {
      41                 :            :   Dwarf_Arange arange;
      42                 :            :   struct arangelist *next;
      43                 :            : };
      44                 :            : 
      45                 :            : /* Compare by Dwarf_Arange.addr, given pointers into an array of pointeers.  */
      46                 :            : static int
      47                 :         74 : compare_aranges (const void *a, const void *b)
      48                 :            : {
      49                 :         74 :   struct arangelist *const *p1 = a, *const *p2 = b;
      50                 :         74 :   struct arangelist *l1 = *p1, *l2 = *p2;
      51         [ +  - ]:         74 :   if (l1->arange.addr != l2->arange.addr)
      52         [ +  + ]:         74 :     return (l1->arange.addr < l2->arange.addr) ? -1 : 1;
      53                 :            :   return 0;
      54                 :            : }
      55                 :            : 
      56                 :            : /* Convert ARANGELIST into Dwarf_Aranges and store at ARANGES.  */
      57                 :            : static bool
      58                 :        142 : finalize_aranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges,
      59                 :            :                   struct arangelist *arangelist, unsigned int narangelist)
      60                 :            : {
      61                 :            :   /* Allocate the array for the result.  */
      62         [ -  + ]:        142 :   void *buf = libdw_alloc (dbg, Dwarf_Aranges,
      63                 :            :                            sizeof (Dwarf_Aranges)
      64                 :            :                            + narangelist * sizeof (Dwarf_Arange), 1);
      65                 :            : 
      66                 :            :   /* First use the buffer for the pointers, and sort the entries.
      67                 :            :      We'll write the pointers in the end of the buffer, and then
      68                 :            :      copy into the buffer from the beginning so the overlap works.  */
      69                 :        142 :   eu_static_assert (sizeof (Dwarf_Arange) >= sizeof (Dwarf_Arange *));
      70                 :        142 :   struct arangelist **sortaranges
      71                 :            :     = (buf + sizeof (Dwarf_Aranges)
      72                 :        142 :        + ((sizeof (Dwarf_Arange) - sizeof sortaranges[0]) * narangelist));
      73                 :            : 
      74                 :            :   /* The list is in LIFO order and usually they come in clumps with
      75                 :            :      ascending addresses.  So fill from the back to probably start with
      76                 :            :      runs already in order before we sort.  */
      77                 :        142 :   unsigned int i = narangelist;
      78         [ +  + ]:        352 :   while (i-- > 0)
      79                 :            :     {
      80                 :        210 :       sortaranges[i] = arangelist;
      81                 :        210 :       arangelist = arangelist->next;
      82                 :            :     }
      83                 :            : 
      84                 :            :   /* Something went wrong if narangelist is less then the actual length
      85                 :            :      of arangelist. */
      86         [ -  + ]:        142 :   if (arangelist != NULL)
      87                 :            :     {
      88                 :          0 :       __libdw_seterrno (DWARF_E_UNKNOWN_ERROR);
      89                 :          0 :       return false;
      90                 :            :     }
      91                 :            : 
      92                 :            :   /* Sort by ascending address.  */
      93                 :        142 :   qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges);
      94                 :            : 
      95                 :            :   /* Now that they are sorted, put them in the final array.
      96                 :            :      The buffers overlap, so we've clobbered the early elements
      97                 :            :      of SORTARANGES by the time we're reading the later ones.  */
      98                 :        142 :   *aranges = buf;
      99                 :        142 :   (*aranges)->dbg = dbg;
     100                 :        142 :   (*aranges)->naranges = narangelist;
     101         [ +  - ]:        142 :   if (naranges != NULL)
     102                 :        142 :     *naranges = narangelist;
     103         [ +  + ]:        352 :   for (i = 0; i < narangelist; ++i)
     104                 :            :     {
     105                 :        210 :       struct arangelist *elt = sortaranges[i];
     106                 :        210 :       (*aranges)->info[i] = elt->arange;
     107                 :        210 :       free (elt);
     108                 :            :     }
     109                 :            : 
     110                 :            :   return true;
     111                 :            : }
     112                 :            : 
     113                 :            : int
     114                 :        188 : __libdw_getdieranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges)
     115                 :            : {
     116         [ -  + ]:        188 :   if (dbg == NULL)
     117                 :            :     return -1;
     118                 :            : 
     119         [ +  + ]:        188 :   if (dbg->dieranges != NULL)
     120                 :            :     {
     121                 :         52 :       *aranges = dbg->dieranges;
     122         [ +  - ]:         52 :       if (naranges != NULL)
     123                 :         52 :         *naranges = dbg->dieranges->naranges;
     124                 :         52 :       return 0;
     125                 :            :     }
     126                 :            : 
     127                 :        136 :   struct arangelist *arangelist = NULL;
     128                 :        136 :   unsigned int narangelist = 0;
     129                 :            : 
     130                 :        136 :   Dwarf_CU *cu = NULL;
     131         [ +  + ]:        314 :   while (INTUSE(dwarf_get_units) (dbg, cu, &cu, NULL, NULL, NULL, NULL) == 0)
     132                 :            :     {
     133                 :        178 :       Dwarf_Addr base;
     134                 :        178 :       Dwarf_Addr low;
     135                 :        178 :       Dwarf_Addr high;
     136                 :            : 
     137                 :        178 :       Dwarf_Die cudie = CUDIE (cu);
     138                 :            : 
     139                 :            :       /* Skip CUs that only contain type information.  */
     140         [ +  + ]:        178 :       if (!INTUSE(dwarf_hasattr) (&cudie, DW_AT_low_pc)
     141         [ -  + ]:          6 :           && !INTUSE(dwarf_hasattr) (&cudie, DW_AT_ranges))
     142                 :          6 :         continue;
     143                 :            : 
     144                 :            :       ptrdiff_t offset = 0;
     145                 :            : 
     146                 :            :       /* Add arange for each range list entry or high_pc and low_pc.  */
     147                 :        720 :       while ((offset = INTUSE(dwarf_ranges) (&cudie, offset,
     148         [ +  + ]:        360 :                                              &base, &low, &high)) > 0)
     149                 :            :         {
     150                 :        188 :           if (offset == -1)
     151                 :            :             {
     152                 :            :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
     153                 :          0 :               goto fail;
     154                 :            :             }
     155                 :            : 
     156                 :        188 :           struct arangelist *new_arange = malloc (sizeof *new_arange);
     157         [ -  + ]:        188 :           if (unlikely (new_arange == NULL))
     158                 :            :             {
     159                 :          0 :               __libdw_seterrno (DWARF_E_NOMEM);
     160                 :          0 :               goto fail;
     161                 :            :             }
     162                 :            : 
     163                 :        188 :           new_arange->arange.addr = low;
     164                 :        188 :           new_arange->arange.length = (Dwarf_Word) (high - low);
     165                 :        188 :           new_arange->arange.offset = __libdw_first_die_off_from_cu (cu);
     166                 :            : 
     167                 :        188 :           new_arange->next = arangelist;
     168                 :        188 :           arangelist = new_arange;
     169                 :        188 :           ++narangelist;
     170                 :            :         }
     171                 :            :     }
     172                 :            : 
     173         [ -  + ]:        136 :   if (narangelist == 0)
     174                 :            :     {
     175         [ #  # ]:          0 :       if (naranges != NULL)
     176                 :          0 :         *naranges = 0;
     177                 :          0 :       *aranges = NULL;
     178                 :          0 :       return 0;
     179                 :            :     }
     180                 :            : 
     181         [ -  + ]:        136 :   if (!finalize_aranges (dbg, aranges, naranges, arangelist, narangelist))
     182                 :          0 :     goto fail;
     183                 :            : 
     184                 :        136 :   dbg->dieranges = *aranges;
     185                 :        136 :   return 0;
     186                 :            : 
     187                 :            : fail:
     188         [ #  # ]:          0 :   while (arangelist != NULL)
     189                 :            :     {
     190                 :          0 :       struct arangelist *next = arangelist->next;
     191                 :          0 :       free (arangelist);
     192                 :          0 :       arangelist = next;
     193                 :            :     }
     194                 :            :   return -1;
     195                 :            : }
     196                 :            : 
     197                 :            : int
     198                 :          6 : dwarf_getaranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges)
     199                 :            : {
     200         [ +  - ]:          6 :   if (dbg == NULL)
     201                 :            :     return -1;
     202                 :            : 
     203         [ -  + ]:          6 :   if (dbg->aranges != NULL)
     204                 :            :     {
     205                 :          0 :       *aranges = dbg->aranges;
     206         [ #  # ]:          0 :       if (naranges != NULL)
     207                 :          0 :         *naranges = dbg->aranges->naranges;
     208                 :          0 :       return 0;
     209                 :            :     }
     210                 :            : 
     211         [ -  + ]:          6 :   if (dbg->sectiondata[IDX_debug_aranges] == NULL)
     212                 :            :     {
     213                 :            :       /* No such section.  */
     214                 :          0 :       *aranges = NULL;
     215         [ #  # ]:          0 :       if (naranges != NULL)
     216                 :          0 :         *naranges = 0;
     217                 :          0 :       return 0;
     218                 :            :     }
     219                 :            : 
     220         [ +  - ]:          6 :   if (dbg->sectiondata[IDX_debug_aranges]->d_buf == NULL)
     221                 :            :     return -1;
     222                 :            : 
     223                 :          6 :   struct arangelist *arangelist = NULL;
     224                 :          6 :   unsigned int narangelist = 0;
     225                 :            : 
     226                 :          6 :   const unsigned char *readp = dbg->sectiondata[IDX_debug_aranges]->d_buf;
     227                 :          6 :   const unsigned char *readendp
     228                 :          6 :     = readp + dbg->sectiondata[IDX_debug_aranges]->d_size;
     229                 :            : 
     230         [ +  + ]:         24 :   while (readp < readendp)
     231                 :            :     {
     232                 :         18 :       const unsigned char *hdrstart = readp;
     233                 :            : 
     234                 :            :       /* Each entry starts with a header:
     235                 :            : 
     236                 :            :          1. A 4-byte or 12-byte length containing the length of the
     237                 :            :          set of entries for this compilation unit, not including the
     238                 :            :          length field itself. [...]
     239                 :            : 
     240                 :            :          2. A 2-byte version identifier containing the value 2 for
     241                 :            :          DWARF Version 2.1.
     242                 :            : 
     243                 :            :          3. A 4-byte or 8-byte offset into the .debug_info section. [...]
     244                 :            : 
     245                 :            :          4. A 1-byte unsigned integer containing the size in bytes of
     246                 :            :          an address (or the offset portion of an address for segmented
     247                 :            :          addressing) on the target system.
     248                 :            : 
     249                 :            :          5. A 1-byte unsigned integer containing the size in bytes of
     250                 :            :          a segment descriptor on the target system.  */
     251         [ -  + ]:         18 :       if (unlikely (readp + 4 > readendp))
     252                 :          0 :         goto invalid;
     253                 :            : 
     254         [ +  + ]:         18 :       Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
     255                 :         18 :       unsigned int length_bytes = 4;
     256         [ -  + ]:         18 :       if (length == DWARF3_LENGTH_64_BIT)
     257                 :            :         {
     258         [ #  # ]:          0 :           if (unlikely (readp + 8 > readendp))
     259                 :          0 :             goto invalid;
     260                 :            : 
     261         [ #  # ]:          0 :           length = read_8ubyte_unaligned_inc (dbg, readp);
     262                 :          0 :           length_bytes = 8;
     263                 :            :         }
     264         [ -  + ]:         18 :       else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
     265                 :            :                          && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
     266                 :          0 :         goto invalid;
     267                 :            : 
     268                 :         18 :       const unsigned char *endp = readp + length;
     269         [ -  + ]:         18 :       if (unlikely (endp > readendp))
     270                 :          0 :         goto invalid;
     271                 :            : 
     272         [ -  + ]:         18 :       if (unlikely (readp + 2 > readendp))
     273                 :          0 :         goto invalid;
     274                 :            : 
     275         [ +  + ]:         18 :       unsigned int version = read_2ubyte_unaligned_inc (dbg, readp);
     276         [ -  + ]:         18 :       if (version != 2)
     277                 :            :         {
     278                 :          0 :         invalid:
     279                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_DWARF);
     280                 :            :         fail:
     281         [ #  # ]:          0 :           while (arangelist != NULL)
     282                 :            :             {
     283                 :          0 :               struct arangelist *next = arangelist->next;
     284                 :          0 :               free (arangelist);
     285                 :          0 :               arangelist = next;
     286                 :            :             }
     287                 :          0 :           return -1;
     288                 :            :         }
     289                 :            : 
     290                 :         18 :       Dwarf_Word offset = 0;
     291         [ -  + ]:         18 :       if (__libdw_read_offset_inc (dbg,
     292                 :            :                                    IDX_debug_aranges, &readp,
     293                 :            :                                    length_bytes, &offset, IDX_debug_info, 4))
     294                 :          0 :         goto fail;
     295                 :            : 
     296                 :            :       /* Next up two bytes for address and segment size.  */
     297         [ -  + ]:         18 :       if (readp + 2 > readendp)
     298                 :          0 :         goto invalid;
     299                 :            : 
     300                 :         18 :       unsigned int address_size = *readp++;
     301         [ -  + ]:         18 :       if (unlikely (address_size != 4 && address_size != 8))
     302                 :          0 :         goto invalid;
     303                 :            : 
     304                 :            :       /* We don't actually support segment selectors.  */
     305                 :         18 :       unsigned int segment_size = *readp++;
     306         [ -  + ]:         18 :       if (segment_size != 0)
     307                 :          0 :         goto invalid;
     308                 :            : 
     309                 :            :       /* Round the address to the next multiple of 2*address_size.  */
     310                 :         18 :       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
     311                 :         18 :                 % (2 * address_size));
     312                 :            : 
     313                 :         40 :       while (1)
     314                 :            :         {
     315                 :         40 :           Dwarf_Word range_address;
     316                 :         40 :           Dwarf_Word range_length;
     317                 :            : 
     318         [ -  + ]:         40 :           if (__libdw_read_address_inc (dbg, IDX_debug_aranges, &readp,
     319                 :            :                                         address_size, &range_address))
     320                 :          0 :             goto fail;
     321                 :            : 
     322         [ -  + ]:         40 :           if (readp + address_size > readendp)
     323                 :          0 :             goto invalid;
     324                 :            : 
     325         [ +  - ]:         40 :           if (address_size == 4)
     326         [ +  + ]:         40 :             range_length = read_4ubyte_unaligned_inc (dbg, readp);
     327                 :            :           else
     328         [ #  # ]:          0 :             range_length = read_8ubyte_unaligned_inc (dbg, readp);
     329                 :            : 
     330                 :            :           /* Two zero values mark the end.  But in some cases (bugs)
     331                 :            :              there might be such entries in the middle of the table.
     332                 :            :              Ignore and continue, we'll check the actual length of
     333                 :            :              the table to see if we are really at the end.  */
     334         [ +  + ]:         40 :           if (range_address == 0 && range_length == 0)
     335                 :            :             {
     336         [ -  + ]:         18 :               if (readp >= endp)
     337                 :            :                 break;
     338                 :            :               else
     339                 :          0 :                 continue;
     340                 :            :             }
     341                 :            : 
     342                 :            :           /* We don't use alloca for these temporary structures because
     343                 :            :              the total number of them can be quite large.  */
     344                 :         22 :           struct arangelist *new_arange = malloc (sizeof *new_arange);
     345         [ -  + ]:         22 :           if (unlikely (new_arange == NULL))
     346                 :            :             {
     347                 :          0 :               __libdw_seterrno (DWARF_E_NOMEM);
     348                 :          0 :               goto fail;
     349                 :            :             }
     350                 :            : 
     351                 :         22 :           new_arange->arange.addr = range_address;
     352                 :         22 :           new_arange->arange.length = range_length;
     353                 :            : 
     354                 :            :           /* We store the actual CU DIE offset, not the CU header offset.  */
     355                 :         22 :           Dwarf_CU *cu = __libdw_findcu (dbg, offset, false);
     356         [ -  + ]:         22 :           if (unlikely (cu == NULL))
     357                 :            :             {
     358                 :            :               /* We haven't gotten a chance to link in the new_arange
     359                 :            :                  into the arangelist, don't leak it.  */
     360                 :          0 :               free (new_arange);
     361                 :          0 :               goto fail;
     362                 :            :             }
     363                 :         22 :           new_arange->arange.offset = __libdw_first_die_off_from_cu (cu);
     364                 :            : 
     365                 :         22 :           new_arange->next = arangelist;
     366                 :         22 :           arangelist = new_arange;
     367                 :         22 :           ++narangelist;
     368                 :            : 
     369                 :            :           /* Sanity-check the data.  */
     370         [ -  + ]:         22 :           if (unlikely (new_arange->arange.offset
     371                 :            :                         >= dbg->sectiondata[IDX_debug_info]->d_size))
     372                 :          0 :             goto invalid;
     373                 :            :         }
     374                 :            :     }
     375                 :            : 
     376         [ -  + ]:          6 :   if (narangelist == 0)
     377                 :            :     {
     378         [ #  # ]:          0 :       if (naranges != NULL)
     379                 :          0 :         *naranges = 0;
     380                 :          0 :       *aranges = NULL;
     381                 :          0 :       return 0;
     382                 :            :     }
     383                 :            : 
     384         [ -  + ]:          6 :   if (!finalize_aranges (dbg, aranges, naranges, arangelist, narangelist))
     385                 :          0 :     goto fail;
     386                 :            : 
     387                 :          6 :   dbg->aranges = *aranges;
     388                 :          6 :   return 0;
     389                 :            : }
     390                 :            : INTDEF(dwarf_getaranges)

Generated by: LCOV version 1.16