LCOV - code coverage report
Current view: top level - libdw - dwarf_getaranges.c (source / functions) Hit Total Coverage
Test: elfutils-0.190 Lines: 86 118 72.9 %
Date: 2023-11-16 11:50:13 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 48 84 57.1 %

           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 <assert.h>
      37                 :            : #include "libdwP.h"
      38                 :            : #include <dwarf.h>
      39                 :            : 
      40                 :            : struct arangelist
      41                 :            : {
      42                 :            :   Dwarf_Arange arange;
      43                 :            :   struct arangelist *next;
      44                 :            : };
      45                 :            : 
      46                 :            : /* Compare by Dwarf_Arange.addr, given pointers into an array of pointeers.  */
      47                 :            : static int
      48                 :         92 : compare_aranges (const void *a, const void *b)
      49                 :            : {
      50                 :         92 :   struct arangelist *const *p1 = a, *const *p2 = b;
      51                 :         92 :   struct arangelist *l1 = *p1, *l2 = *p2;
      52         [ +  - ]:         92 :   if (l1->arange.addr != l2->arange.addr)
      53         [ +  + ]:         92 :     return (l1->arange.addr < l2->arange.addr) ? -1 : 1;
      54                 :            :   return 0;
      55                 :            : }
      56                 :            : 
      57                 :            : int
      58                 :        170 : dwarf_getaranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges)
      59                 :            : {
      60         [ +  - ]:        170 :   if (dbg == NULL)
      61                 :            :     return -1;
      62                 :            : 
      63         [ +  + ]:        170 :   if (dbg->aranges != NULL)
      64                 :            :     {
      65                 :         26 :       *aranges = dbg->aranges;
      66         [ +  - ]:         26 :       if (naranges != NULL)
      67                 :         26 :         *naranges = dbg->aranges->naranges;
      68                 :         26 :       return 0;
      69                 :            :     }
      70                 :            : 
      71         [ +  + ]:        144 :   if (dbg->sectiondata[IDX_debug_aranges] == NULL)
      72                 :            :     {
      73                 :            :       /* No such section.  */
      74                 :         16 :       *aranges = NULL;
      75         [ +  - ]:         16 :       if (naranges != NULL)
      76                 :         16 :         *naranges = 0;
      77                 :         16 :       return 0;
      78                 :            :     }
      79                 :            : 
      80         [ +  - ]:        128 :   if (dbg->sectiondata[IDX_debug_aranges]->d_buf == NULL)
      81                 :            :     return -1;
      82                 :            : 
      83                 :        128 :   struct arangelist *arangelist = NULL;
      84                 :        128 :   unsigned int narangelist = 0;
      85                 :            : 
      86                 :        128 :   const unsigned char *readp = dbg->sectiondata[IDX_debug_aranges]->d_buf;
      87                 :        128 :   const unsigned char *readendp
      88                 :        128 :     = readp + dbg->sectiondata[IDX_debug_aranges]->d_size;
      89                 :            : 
      90         [ +  + ]:        304 :   while (readp < readendp)
      91                 :            :     {
      92                 :        176 :       const unsigned char *hdrstart = readp;
      93                 :            : 
      94                 :            :       /* Each entry starts with a header:
      95                 :            : 
      96                 :            :          1. A 4-byte or 12-byte length containing the length of the
      97                 :            :          set of entries for this compilation unit, not including the
      98                 :            :          length field itself. [...]
      99                 :            : 
     100                 :            :          2. A 2-byte version identifier containing the value 2 for
     101                 :            :          DWARF Version 2.1.
     102                 :            : 
     103                 :            :          3. A 4-byte or 8-byte offset into the .debug_info section. [...]
     104                 :            : 
     105                 :            :          4. A 1-byte unsigned integer containing the size in bytes of
     106                 :            :          an address (or the offset portion of an address for segmented
     107                 :            :          addressing) on the target system.
     108                 :            : 
     109                 :            :          5. A 1-byte unsigned integer containing the size in bytes of
     110                 :            :          a segment descriptor on the target system.  */
     111         [ -  + ]:        176 :       if (unlikely (readp + 4 > readendp))
     112                 :          0 :         goto invalid;
     113                 :            : 
     114         [ +  + ]:        176 :       Dwarf_Word length = read_4ubyte_unaligned_inc (dbg, readp);
     115                 :        176 :       unsigned int length_bytes = 4;
     116         [ -  + ]:        176 :       if (length == DWARF3_LENGTH_64_BIT)
     117                 :            :         {
     118         [ #  # ]:          0 :           if (unlikely (readp + 8 > readendp))
     119                 :          0 :             goto invalid;
     120                 :            : 
     121         [ #  # ]:          0 :           length = read_8ubyte_unaligned_inc (dbg, readp);
     122                 :          0 :           length_bytes = 8;
     123                 :            :         }
     124         [ -  + ]:        176 :       else if (unlikely (length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
     125                 :            :                          && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
     126                 :          0 :         goto invalid;
     127                 :            : 
     128                 :        176 :       const unsigned char *endp = readp + length;
     129         [ -  + ]:        176 :       if (unlikely (endp > readendp))
     130                 :          0 :         goto invalid;
     131                 :            : 
     132         [ -  + ]:        176 :       if (unlikely (readp + 2 > readendp))
     133                 :          0 :         goto invalid;
     134                 :            : 
     135         [ +  + ]:        176 :       unsigned int version = read_2ubyte_unaligned_inc (dbg, readp);
     136         [ -  + ]:        176 :       if (version != 2)
     137                 :            :         {
     138                 :          0 :         invalid:
     139                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_DWARF);
     140                 :            :         fail:
     141         [ #  # ]:          0 :           while (arangelist != NULL)
     142                 :            :             {
     143                 :          0 :               struct arangelist *next = arangelist->next;
     144                 :          0 :               free (arangelist);
     145                 :          0 :               arangelist = next;
     146                 :            :             }
     147                 :          0 :           return -1;
     148                 :            :         }
     149                 :            : 
     150                 :        176 :       Dwarf_Word offset = 0;
     151         [ -  + ]:        176 :       if (__libdw_read_offset_inc (dbg,
     152                 :            :                                    IDX_debug_aranges, &readp,
     153                 :            :                                    length_bytes, &offset, IDX_debug_info, 4))
     154                 :          0 :         goto fail;
     155                 :            : 
     156                 :            :       /* Next up two bytes for address and segment size.  */
     157         [ -  + ]:        176 :       if (readp + 2 > readendp)
     158                 :          0 :         goto invalid;
     159                 :            : 
     160                 :        176 :       unsigned int address_size = *readp++;
     161         [ -  + ]:        176 :       if (unlikely (address_size != 4 && address_size != 8))
     162                 :          0 :         goto invalid;
     163                 :            : 
     164                 :            :       /* We don't actually support segment selectors.  */
     165                 :        176 :       unsigned int segment_size = *readp++;
     166         [ -  + ]:        176 :       if (segment_size != 0)
     167                 :          0 :         goto invalid;
     168                 :            : 
     169                 :            :       /* Round the address to the next multiple of 2*address_size.  */
     170                 :        176 :       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
     171                 :        176 :                 % (2 * address_size));
     172                 :            : 
     173                 :        378 :       while (1)
     174                 :            :         {
     175                 :        378 :           Dwarf_Word range_address;
     176                 :        378 :           Dwarf_Word range_length;
     177                 :            : 
     178         [ -  + ]:        378 :           if (__libdw_read_address_inc (dbg, IDX_debug_aranges, &readp,
     179                 :            :                                         address_size, &range_address))
     180                 :          0 :             goto fail;
     181                 :            : 
     182         [ -  + ]:        378 :           if (readp + address_size > readendp)
     183                 :          0 :             goto invalid;
     184                 :            : 
     185         [ +  + ]:        378 :           if (address_size == 4)
     186         [ +  + ]:        176 :             range_length = read_4ubyte_unaligned_inc (dbg, readp);
     187                 :            :           else
     188         [ -  + ]:        202 :             range_length = read_8ubyte_unaligned_inc (dbg, readp);
     189                 :            : 
     190                 :            :           /* Two zero values mark the end.  But in some cases (bugs)
     191                 :            :              there might be such entries in the middle of the table.
     192                 :            :              Ignore and continue, we'll check the actual length of
     193                 :            :              the table to see if we are really at the end.  */
     194         [ +  + ]:        378 :           if (range_address == 0 && range_length == 0)
     195                 :            :             {
     196         [ -  + ]:        176 :               if (readp >= endp)
     197                 :            :                 break;
     198                 :            :               else
     199                 :          0 :                 continue;
     200                 :            :             }
     201                 :            : 
     202                 :            :           /* We don't use alloca for these temporary structures because
     203                 :            :              the total number of them can be quite large.  */
     204                 :        202 :           struct arangelist *new_arange = malloc (sizeof *new_arange);
     205         [ -  + ]:        202 :           if (unlikely (new_arange == NULL))
     206                 :            :             {
     207                 :          0 :               __libdw_seterrno (DWARF_E_NOMEM);
     208                 :          0 :               goto fail;
     209                 :            :             }
     210                 :            : 
     211                 :        202 :           new_arange->arange.addr = range_address;
     212                 :        202 :           new_arange->arange.length = range_length;
     213                 :            : 
     214                 :            :           /* We store the actual CU DIE offset, not the CU header offset.  */
     215                 :        202 :           Dwarf_CU *cu = __libdw_findcu (dbg, offset, false);
     216         [ -  + ]:        202 :           if (unlikely (cu == NULL))
     217                 :            :             {
     218                 :            :               /* We haven't gotten a chance to link in the new_arange
     219                 :            :                  into the arangelist, don't leak it.  */
     220                 :          0 :               free (new_arange);
     221                 :          0 :               goto fail;
     222                 :            :             }
     223                 :        202 :           new_arange->arange.offset = __libdw_first_die_off_from_cu (cu);
     224                 :            : 
     225                 :        202 :           new_arange->next = arangelist;
     226                 :        202 :           arangelist = new_arange;
     227                 :        202 :           ++narangelist;
     228                 :            : 
     229                 :            :           /* Sanity-check the data.  */
     230         [ -  + ]:        202 :           if (unlikely (new_arange->arange.offset
     231                 :            :                         >= dbg->sectiondata[IDX_debug_info]->d_size))
     232                 :          0 :             goto invalid;
     233                 :            :         }
     234                 :            :     }
     235                 :            : 
     236         [ -  + ]:        128 :   if (narangelist == 0)
     237                 :            :     {
     238         [ #  # ]:          0 :       assert (arangelist == NULL);
     239         [ #  # ]:          0 :       if (naranges != NULL)
     240                 :          0 :         *naranges = 0;
     241                 :          0 :       *aranges = NULL;
     242                 :          0 :       return 0;
     243                 :            :     }
     244                 :            : 
     245                 :            :   /* Allocate the array for the result.  */
     246         [ -  + ]:        128 :   void *buf = libdw_alloc (dbg, Dwarf_Aranges,
     247                 :            :                            sizeof (Dwarf_Aranges)
     248                 :            :                            + narangelist * sizeof (Dwarf_Arange), 1);
     249                 :            : 
     250                 :            :   /* First use the buffer for the pointers, and sort the entries.
     251                 :            :      We'll write the pointers in the end of the buffer, and then
     252                 :            :      copy into the buffer from the beginning so the overlap works.  */
     253                 :        128 :   assert (sizeof (Dwarf_Arange) >= sizeof (Dwarf_Arange *));
     254                 :        128 :   struct arangelist **sortaranges
     255                 :            :     = (buf + sizeof (Dwarf_Aranges)
     256                 :        128 :        + ((sizeof (Dwarf_Arange) - sizeof sortaranges[0]) * narangelist));
     257                 :            : 
     258                 :            :   /* The list is in LIFO order and usually they come in clumps with
     259                 :            :      ascending addresses.  So fill from the back to probably start with
     260                 :            :      runs already in order before we sort.  */
     261                 :        128 :   unsigned int i = narangelist;
     262         [ +  + ]:        330 :   while (i-- > 0)
     263                 :            :     {
     264                 :        202 :       sortaranges[i] = arangelist;
     265                 :        202 :       arangelist = arangelist->next;
     266                 :            :     }
     267         [ -  + ]:        128 :   assert (arangelist == NULL);
     268                 :            : 
     269                 :            :   /* Sort by ascending address.  */
     270                 :        128 :   qsort (sortaranges, narangelist, sizeof sortaranges[0], &compare_aranges);
     271                 :            : 
     272                 :            :   /* Now that they are sorted, put them in the final array.
     273                 :            :      The buffers overlap, so we've clobbered the early elements
     274                 :            :      of SORTARANGES by the time we're reading the later ones.  */
     275                 :        128 :   *aranges = buf;
     276                 :        128 :   (*aranges)->dbg = dbg;
     277                 :        128 :   (*aranges)->naranges = narangelist;
     278                 :        128 :   dbg->aranges = *aranges;
     279         [ +  - ]:        128 :   if (naranges != NULL)
     280                 :        128 :     *naranges = narangelist;
     281         [ +  + ]:        330 :   for (i = 0; i < narangelist; ++i)
     282                 :            :     {
     283                 :        202 :       struct arangelist *elt = sortaranges[i];
     284                 :        202 :       (*aranges)->info[i] = elt->arange;
     285                 :        202 :       free (elt);
     286                 :            :     }
     287                 :            : 
     288                 :            :   return 0;
     289                 :            : }
     290                 :            : INTDEF(dwarf_getaranges)

Generated by: LCOV version 1.16