LCOV - code coverage report
Current view: top level - libdw - dwarf_getmacros.c (source / functions) Hit Total Coverage
Test: elfutils-0.193 Lines: 206 254 81.1 %
Date: 2025-06-03 13:14:14 Functions: 13 13 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 102 160 63.8 %

           Branch data     Line data    Source code
       1                 :            : /* Get macro information.
       2                 :            :    Copyright (C) 2002-2009, 2014, 2017, 2018 Red Hat, Inc.
       3                 :            :    This file is part of elfutils.
       4                 :            : 
       5                 :            :    This file is free software; you can redistribute it and/or modify
       6                 :            :    it under the terms of either
       7                 :            : 
       8                 :            :      * the GNU Lesser General Public License as published by the Free
       9                 :            :        Software Foundation; either version 3 of the License, or (at
      10                 :            :        your option) any later version
      11                 :            : 
      12                 :            :    or
      13                 :            : 
      14                 :            :      * the GNU General Public License as published by the Free
      15                 :            :        Software Foundation; either version 2 of the License, or (at
      16                 :            :        your option) any later version
      17                 :            : 
      18                 :            :    or both in parallel, as here.
      19                 :            : 
      20                 :            :    elfutils is distributed in the hope that it will be useful, but
      21                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23                 :            :    General Public License for more details.
      24                 :            : 
      25                 :            :    You should have received copies of the GNU General Public License and
      26                 :            :    the GNU Lesser General Public License along with this program.  If
      27                 :            :    not, see <http://www.gnu.org/licenses/>.  */
      28                 :            : 
      29                 :            : #ifdef HAVE_CONFIG_H
      30                 :            : # include <config.h>
      31                 :            : #endif
      32                 :            : 
      33                 :            : #include <assert.h>
      34                 :            : #include <dwarf.h>
      35                 :            : #include <stdlib.h>
      36                 :            : #include <string.h>
      37                 :            : 
      38                 :            : #include <libdwP.h>
      39                 :            : #include "eu-search.h"
      40                 :            : 
      41                 :            : static int
      42                 :       9476 : get_offset_from (Dwarf_Die *die, int name, Dwarf_Word *retp)
      43                 :            : {
      44                 :            :   /* Get the appropriate attribute.  */
      45                 :       9476 :   Dwarf_Attribute attr;
      46         [ +  + ]:       9476 :   if (INTUSE(dwarf_attr) (die, name, &attr) == NULL)
      47                 :            :     return -1;
      48                 :            : 
      49                 :            :   /* Offset into the corresponding section.  */
      50         [ -  + ]:       6674 :   if (INTUSE(dwarf_formudata) (&attr, retp) != 0)
      51                 :            :     return -1;
      52                 :            : 
      53                 :       6674 :   Dwarf_Off offset;
      54         [ -  + ]:       6674 :   if (INTUSE(dwarf_cu_dwp_section_info) (die->cu, DW_SECT_MACRO, &offset, NULL)
      55                 :            :       != 0)
      56                 :            :     return -1;
      57                 :       6674 :   *retp += offset;
      58                 :       6674 :   return 0;
      59                 :            : }
      60                 :            : 
      61                 :            : static int
      62                 :      11680 : macro_op_compare (const void *p1, const void *p2)
      63                 :            : {
      64                 :      11680 :   const Dwarf_Macro_Op_Table *t1 = (const Dwarf_Macro_Op_Table *) p1;
      65                 :      11680 :   const Dwarf_Macro_Op_Table *t2 = (const Dwarf_Macro_Op_Table *) p2;
      66                 :            : 
      67         [ +  + ]:      11680 :   if (t1->offset < t2->offset)
      68                 :            :     return -1;
      69         [ +  + ]:      11600 :   if (t1->offset > t2->offset)
      70                 :            :     return 1;
      71                 :            : 
      72         [ +  - ]:       6986 :   if (t1->sec_index < t2->sec_index)
      73                 :            :     return -1;
      74         [ -  + ]:       6986 :   if (t1->sec_index > t2->sec_index)
      75                 :          0 :     return 1;
      76                 :            : 
      77                 :            :   return 0;
      78                 :            : }
      79                 :            : 
      80                 :            : static void
      81                 :         45 : build_table (Dwarf_Macro_Op_Table *table,
      82                 :            :              Dwarf_Macro_Op_Proto op_protos[static 255])
      83                 :            : {
      84                 :         45 :   unsigned ct = 0;
      85         [ +  + ]:      11520 :   for (unsigned i = 1; i < 256; ++i)
      86         [ +  + ]:      11475 :     if (op_protos[i - 1].forms != NULL)
      87                 :        439 :       table->table[table->opcodes[i - 1] = ct++] = op_protos[i - 1];
      88                 :            :     else
      89                 :      11036 :       table->opcodes[i - 1] = 0xff;
      90                 :         45 : }
      91                 :            : 
      92                 :            : #define MACRO_PROTO(NAME, ...)                                  \
      93                 :            :   Dwarf_Macro_Op_Proto NAME = ({                                \
      94                 :            :       static const uint8_t proto[] = {__VA_ARGS__};             \
      95                 :            :       (Dwarf_Macro_Op_Proto) {sizeof proto, proto};             \
      96                 :            :     })
      97                 :            : 
      98                 :            : enum { macinfo_data_size = offsetof (Dwarf_Macro_Op_Table, table[5]) };
      99                 :            : static unsigned char macinfo_data[macinfo_data_size]
     100                 :            :         __attribute__ ((aligned (__alignof (Dwarf_Macro_Op_Table))));
     101                 :            : 
     102                 :            : static __attribute__ ((constructor)) void
     103                 :         15 : init_macinfo_table (void)
     104                 :            : {
     105                 :         15 :   MACRO_PROTO (p_udata_str, DW_FORM_udata, DW_FORM_string);
     106                 :         15 :   MACRO_PROTO (p_udata_udata, DW_FORM_udata, DW_FORM_udata);
     107                 :         15 :   MACRO_PROTO (p_none);
     108                 :            : 
     109                 :         15 :   Dwarf_Macro_Op_Proto op_protos[255] =
     110                 :            :     {
     111                 :            :       [DW_MACINFO_define - 1] = p_udata_str,
     112                 :            :       [DW_MACINFO_undef - 1] = p_udata_str,
     113                 :            :       [DW_MACINFO_vendor_ext - 1] = p_udata_str,
     114                 :            :       [DW_MACINFO_start_file - 1] = p_udata_udata,
     115                 :            :       [DW_MACINFO_end_file - 1] = p_none,
     116                 :            :       /* If you are adding more elements to this array, increase
     117                 :            :          MACINFO_DATA_SIZE above.  */
     118                 :            :     };
     119                 :            : 
     120                 :         15 :   Dwarf_Macro_Op_Table *macinfo_table = (void *) macinfo_data;
     121                 :         15 :   memset (macinfo_table, 0, sizeof macinfo_data);
     122                 :         15 :   build_table (macinfo_table, op_protos);
     123                 :         15 :   macinfo_table->sec_index = IDX_debug_macinfo;
     124                 :         15 : }
     125                 :            : 
     126                 :            : static Dwarf_Macro_Op_Table *
     127                 :         10 : get_macinfo_table (Dwarf *dbg, Dwarf_Word macoff, Dwarf_Die *cudie)
     128                 :            : {
     129         [ -  + ]:         10 :   assert (cudie != NULL);
     130                 :            : 
     131                 :         10 :   Dwarf_Attribute attr_mem, *attr
     132                 :         10 :     = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &attr_mem);
     133                 :         10 :   Dwarf_Off line_offset = (Dwarf_Off) -1;
     134         [ +  + ]:         10 :   if (attr != NULL)
     135                 :            :     {
     136         [ -  + ]:          4 :       if (unlikely (INTUSE(dwarf_formudata) (attr, &line_offset) != 0))
     137                 :            :         return NULL;
     138                 :            :     }
     139         [ +  - ]:          6 :   else if (cudie->cu->unit_type == DW_UT_split_compile
     140         [ +  - ]:          6 :            && dbg->sectiondata[IDX_debug_line] != NULL)
     141                 :          6 :     line_offset = 0;
     142         [ +  - ]:         10 :   if (line_offset != (Dwarf_Off) -1)
     143                 :            :     {
     144                 :         10 :       Dwarf_Off dwp_offset;
     145         [ -  + ]:         10 :       if (INTUSE(dwarf_cu_dwp_section_info) (cudie->cu, DW_SECT_LINE,
     146                 :            :                                              &dwp_offset, NULL) != 0)
     147                 :          0 :         return NULL;
     148                 :         10 :       line_offset += dwp_offset;
     149                 :            :     }
     150                 :            : 
     151         [ -  + ]:         10 :   Dwarf_Macro_Op_Table *table = libdw_alloc (dbg, Dwarf_Macro_Op_Table,
     152                 :            :                                              macinfo_data_size, 1);
     153                 :         10 :   memcpy (table, macinfo_data, macinfo_data_size);
     154                 :            : 
     155                 :         10 :   table->dbg = dbg;
     156                 :         10 :   table->offset = macoff;
     157                 :         10 :   table->sec_index = IDX_debug_macinfo;
     158                 :         10 :   table->line_offset = line_offset;
     159                 :         10 :   table->address_size = cudie->cu->address_size;
     160                 :         10 :   table->offset_size = cudie->cu->offset_size;
     161                 :         10 :   table->comp_dir = __libdw_getcompdir (cudie);
     162                 :            : 
     163                 :         10 :   return table;
     164                 :            : }
     165                 :            : 
     166                 :            : static Dwarf_Macro_Op_Table *
     167                 :         30 : get_table_for_offset (Dwarf *dbg, Dwarf_Word macoff,
     168                 :            :                       const unsigned char *readp,
     169                 :            :                       const unsigned char *const endp,
     170                 :            :                       Dwarf_Die *cudie)
     171                 :            : {
     172                 :         30 :   const unsigned char *startp = readp;
     173                 :            : 
     174                 :            :   /* Request at least 3 bytes for header.  */
     175         [ -  + ]:         30 :   if (readp + 3 > endp)
     176                 :            :     {
     177                 :          0 :     invalid_dwarf:
     178                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     179                 :          0 :       return NULL;
     180                 :            :     }
     181                 :            : 
     182         [ -  + ]:         30 :   uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
     183         [ -  + ]:         30 :   if (version != 4 && version != 5)
     184                 :            :     {
     185                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_VERSION);
     186                 :          0 :       return NULL;
     187                 :            :     }
     188                 :            : 
     189                 :         30 :   uint8_t flags = *readp++;
     190                 :         30 :   bool is_64bit = (flags & 0x1) != 0;
     191                 :            : 
     192                 :         30 :   Dwarf_Off line_offset = (Dwarf_Off) -1;
     193         [ +  + ]:         30 :   if ((flags & 0x2) != 0)
     194                 :            :     {
     195   [ +  -  -  +  :         12 :       line_offset = read_addr_unaligned_inc (is_64bit ? 8 : 4, dbg, readp);
                   -  - ]
     196         [ -  + ]:         12 :       if (readp > endp)
     197                 :          0 :         goto invalid_dwarf;
     198                 :            :     }
     199         [ -  + ]:         18 :   else if (cudie != NULL)
     200                 :            :     {
     201                 :          0 :       Dwarf_Attribute attr_mem, *attr
     202                 :          0 :         = INTUSE(dwarf_attr) (cudie, DW_AT_stmt_list, &attr_mem);
     203         [ #  # ]:          0 :       if (attr != NULL)
     204         [ #  # ]:          0 :         if (unlikely (INTUSE(dwarf_formudata) (attr, &line_offset) != 0))
     205                 :          0 :           return NULL;
     206                 :            :     }
     207   [ +  +  +  - ]:         30 :   if (line_offset != (Dwarf_Off) -1 && cudie != NULL)
     208                 :            :     {
     209                 :         12 :       Dwarf_Off dwp_offset;
     210         [ -  + ]:         12 :       if (INTUSE(dwarf_cu_dwp_section_info) (cudie->cu, DW_SECT_LINE,
     211                 :            :                                              &dwp_offset, NULL) != 0)
     212                 :          0 :         return NULL;
     213                 :         12 :       line_offset += dwp_offset;
     214                 :            :     }
     215                 :            : 
     216                 :         30 :   uint8_t address_size;
     217         [ +  + ]:         30 :   if (cudie != NULL)
     218                 :         12 :     address_size = cudie->cu->address_size;
     219                 :            :   else
     220                 :            :     {
     221                 :         18 :       char *ident = elf_getident (dbg->elf, NULL);
     222         [ +  - ]:         18 :       address_size = ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
     223                 :            :     }
     224                 :            : 
     225                 :            :   /* """The macinfo entry types defined in this standard may, but
     226                 :            :      might not, be described in the table""".
     227                 :            : 
     228                 :            :      I.e. these may be present.  It's tempting to simply skip them,
     229                 :            :      but it's probably more correct to tolerate that a producer tweaks
     230                 :            :      the way certain opcodes are encoded, for whatever reasons.  */
     231                 :            : 
     232                 :         30 :   MACRO_PROTO (p_udata_str, DW_FORM_udata, DW_FORM_string);
     233                 :         30 :   MACRO_PROTO (p_udata_strp, DW_FORM_udata, DW_FORM_strp);
     234                 :         30 :   MACRO_PROTO (p_udata_strsup, DW_FORM_udata, DW_FORM_strp_sup);
     235                 :         30 :   MACRO_PROTO (p_udata_strx, DW_FORM_udata, DW_FORM_strx);
     236                 :         30 :   MACRO_PROTO (p_udata_udata, DW_FORM_udata, DW_FORM_udata);
     237                 :         30 :   MACRO_PROTO (p_secoffset, DW_FORM_sec_offset);
     238                 :         30 :   MACRO_PROTO (p_none);
     239                 :            : 
     240                 :         30 :   Dwarf_Macro_Op_Proto op_protos[255] =
     241                 :            :     {
     242                 :            :       [DW_MACRO_define - 1] = p_udata_str,
     243                 :            :       [DW_MACRO_undef - 1] = p_udata_str,
     244                 :            :       [DW_MACRO_define_strp - 1] = p_udata_strp,
     245                 :            :       [DW_MACRO_undef_strp - 1] = p_udata_strp,
     246                 :            :       [DW_MACRO_start_file - 1] = p_udata_udata,
     247                 :            :       [DW_MACRO_end_file - 1] = p_none,
     248                 :            :       [DW_MACRO_import - 1] = p_secoffset,
     249                 :            :       [DW_MACRO_define_sup - 1] = p_udata_strsup,
     250                 :            :       [DW_MACRO_undef_sup - 1] = p_udata_strsup,
     251                 :            :       [DW_MACRO_import_sup - 1] = p_secoffset, /* XXX - but in sup!. */
     252                 :            :       [DW_MACRO_define_strx - 1] = p_udata_strx,
     253                 :            :       [DW_MACRO_undef_strx - 1] = p_udata_strx,
     254                 :            :     };
     255                 :            : 
     256         [ +  + ]:         30 :   if ((flags & 0x4) != 0)
     257                 :            :     {
     258                 :          4 :       unsigned count = *readp++;
     259         [ +  + ]:          8 :       for (unsigned i = 0; i < count; ++i)
     260                 :            :         {
     261                 :          4 :           unsigned opcode = *readp++;
     262                 :            : 
     263                 :          4 :           Dwarf_Macro_Op_Proto e;
     264         [ -  + ]:          4 :           if (readp >= endp)
     265                 :          0 :             goto invalid;
     266                 :          4 :           get_uleb128 (e.nforms, readp, endp);
     267                 :          4 :           e.forms = readp;
     268                 :          4 :           op_protos[opcode - 1] = e;
     269                 :            : 
     270                 :          4 :           readp += e.nforms;
     271         [ -  + ]:          4 :           if (readp > endp)
     272                 :            :             {
     273                 :          0 :             invalid:
     274                 :          0 :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
     275                 :          0 :               return NULL;
     276                 :            :             }
     277                 :            :         }
     278                 :            :     }
     279                 :            : 
     280                 :         30 :   size_t ct = 0;
     281         [ +  + ]:       7680 :   for (unsigned i = 1; i < 256; ++i)
     282         [ +  + ]:       7650 :     if (op_protos[i - 1].forms != NULL)
     283                 :        364 :       ++ct;
     284                 :            : 
     285                 :            :   /* We support at most 0xfe opcodes defined in the table, as 0xff is
     286                 :            :      a value that means that given opcode is not stored at all.  But
     287                 :            :      that should be fine, as opcode 0 is not allocated.  */
     288         [ -  + ]:         30 :   assert (ct < 0xff);
     289                 :            : 
     290                 :         30 :   size_t macop_table_size = offsetof (Dwarf_Macro_Op_Table, table[ct]);
     291                 :            : 
     292         [ +  + ]:         30 :   Dwarf_Macro_Op_Table *table = libdw_alloc (dbg, Dwarf_Macro_Op_Table,
     293                 :            :                                              macop_table_size, 1);
     294                 :            : 
     295                 :         90 :   *table = (Dwarf_Macro_Op_Table) {
     296                 :            :     .dbg = dbg,
     297                 :            :     .offset = macoff,
     298                 :            :     .sec_index = IDX_debug_macro,
     299                 :            :     .line_offset = line_offset,
     300         [ +  - ]:         30 :     .header_len = readp - startp,
     301                 :            :     .version = version,
     302                 :            :     .address_size = address_size,
     303                 :            :     .offset_size = is_64bit ? 8 : 4,
     304                 :            : 
     305                 :            :     /* NULL if CUDIE is NULL or DW_AT_comp_dir is absent.  */
     306                 :         30 :     .comp_dir = __libdw_getcompdir (cudie),
     307                 :            :   };
     308                 :         30 :   build_table (table, op_protos);
     309                 :            : 
     310                 :         30 :   return table;
     311                 :            : }
     312                 :            : 
     313                 :            : static Dwarf_Macro_Op_Table *
     314                 :       7026 : cache_op_table (Dwarf *dbg, int sec_index, Dwarf_Off macoff,
     315                 :            :                 const unsigned char *startp,
     316                 :            :                 const unsigned char *const endp,
     317                 :            :                 Dwarf_Die *cudie)
     318                 :            : {
     319                 :       7026 :   Dwarf_Macro_Op_Table fake = { .offset = macoff, .sec_index = sec_index };
     320                 :       7026 :   Dwarf_Macro_Op_Table **found = eu_tfind (&fake, &dbg->macro_ops_tree,
     321                 :            :                                         macro_op_compare);
     322         [ +  + ]:       7026 :   if (found != NULL)
     323                 :       6986 :     return *found;
     324                 :            : 
     325                 :         40 :   mutex_lock (dbg->macro_lock);
     326                 :            : 
     327                 :         40 :   found = eu_tfind_nolock (&fake, &dbg->macro_ops_tree, macro_op_compare);
     328         [ -  + ]:         40 :   if (found != NULL)
     329                 :            :     {
     330                 :          0 :       mutex_unlock (dbg->macro_lock);
     331                 :          0 :       return *found;
     332                 :            :     }
     333                 :            : 
     334                 :         80 :   Dwarf_Macro_Op_Table *table = sec_index == IDX_debug_macro
     335                 :         30 :     ? get_table_for_offset (dbg, macoff, startp, endp, cudie)
     336         [ +  + ]:         40 :     : get_macinfo_table (dbg, macoff, cudie);
     337                 :            : 
     338         [ -  + ]:         40 :   if (table == NULL)
     339                 :            :     {
     340                 :            :       mutex_unlock (dbg->macro_lock);
     341                 :            :       return NULL;
     342                 :            :     }
     343                 :            : 
     344                 :         40 :   Dwarf_Macro_Op_Table **ret = eu_tsearch_nolock (table, &dbg->macro_ops_tree,
     345                 :            :                                                   macro_op_compare);
     346                 :         40 :   mutex_unlock (dbg->macro_lock);
     347                 :            : 
     348         [ -  + ]:         40 :   if (unlikely (ret == NULL))
     349                 :            :     {
     350                 :          0 :       __libdw_seterrno (DWARF_E_NOMEM);
     351                 :          0 :       return NULL;
     352                 :            :     }
     353                 :            : 
     354                 :         40 :   return *ret;
     355                 :            : }
     356                 :            : 
     357                 :            : static ptrdiff_t
     358                 :       7026 : read_macros (Dwarf *dbg, int sec_index,
     359                 :            :              Dwarf_Off macoff, int (*callback) (Dwarf_Macro *, void *),
     360                 :            :              void *arg, ptrdiff_t offset, bool accept_0xff,
     361                 :            :              Dwarf_Die *cudie)
     362                 :            : {
     363                 :       7026 :   Elf_Data *d = dbg->sectiondata[sec_index];
     364   [ +  -  -  + ]:       7026 :   if (unlikely (d == NULL || d->d_buf == NULL))
     365                 :            :     {
     366                 :          0 :       __libdw_seterrno (DWARF_E_NO_ENTRY);
     367                 :          0 :       return -1;
     368                 :            :     }
     369                 :            : 
     370         [ -  + ]:       7026 :   if (unlikely (macoff >= d->d_size))
     371                 :            :     {
     372                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     373                 :          0 :       return -1;
     374                 :            :     }
     375                 :            : 
     376                 :       7026 :   const unsigned char *const startp = d->d_buf + macoff;
     377                 :       7026 :   const unsigned char *const endp = d->d_buf + d->d_size;
     378                 :            : 
     379                 :       7026 :   Dwarf_Macro_Op_Table *table = cache_op_table (dbg, sec_index, macoff,
     380                 :            :                                                 startp, endp, cudie);
     381         [ -  + ]:       7026 :   if (table == NULL)
     382                 :            :     return -1;
     383                 :            : 
     384         [ +  + ]:       7026 :   if (offset == 0)
     385                 :         40 :     offset = table->header_len;
     386                 :            : 
     387         [ -  + ]:       7026 :   assert (offset >= 0);
     388         [ -  + ]:       7026 :   assert (offset < endp - startp);
     389                 :       7026 :   const unsigned char *readp = startp + offset;
     390                 :            : 
     391         [ +  - ]:       7026 :   while (readp < endp)
     392                 :            :     {
     393                 :       7026 :       unsigned int opcode = *readp++;
     394         [ +  + ]:       7026 :       if (opcode == 0)
     395                 :            :         /* Nothing more to do.  */
     396                 :       7026 :         return 0;
     397                 :            : 
     398         [ +  + ]:       6988 :       if (unlikely (opcode == 0xff && ! accept_0xff))
     399                 :            :         {
     400                 :            :           /* See comment below at dwarf_getmacros for explanation of
     401                 :            :              why we are doing this.  */
     402                 :          2 :           __libdw_seterrno (DWARF_E_INVALID_OPCODE);
     403                 :          2 :           return -1;
     404                 :            :         }
     405                 :            : 
     406                 :       6986 :       unsigned int idx = table->opcodes[opcode - 1];
     407         [ -  + ]:       6986 :       if (idx == 0xff)
     408                 :            :         {
     409                 :          0 :           __libdw_seterrno (DWARF_E_INVALID_OPCODE);
     410                 :          0 :           return -1;
     411                 :            :         }
     412                 :            : 
     413                 :       6986 :       Dwarf_Macro_Op_Proto *proto = &table->table[idx];
     414                 :            : 
     415                 :            :       /* A fake CU with bare minimum data to fool dwarf_formX into
     416                 :            :          doing the right thing with the attributes that we put out.
     417                 :            :          We pretend it is the same version as the actual table.
     418                 :            :          Version 4 for the old GNU extension, version 5 for DWARF5.
     419                 :            :          To handle DW_FORM_strx[1234] we set the .str_offsets_base
     420                 :            :          from the given CU.
     421                 :            :          XXX We will need to deal with DW_MACRO_import_sup and change
     422                 :            :          out the dbg somehow for the DW_FORM_sec_offset to make sense.  */
     423                 :      13972 :       Dwarf_CU fake_cu = {
     424                 :            :         .dbg = dbg,
     425                 :            :         .sec_idx = sec_index,
     426                 :       6986 :         .version = table->version,
     427                 :       6986 :         .offset_size = table->offset_size,
     428         [ +  + ]:       6986 :         .str_off_base = str_offsets_base_off (dbg, (cudie != NULL
     429                 :            :                                                     ? cudie->cu: NULL)),
     430                 :            :         .startp = (void *) startp + offset,
     431                 :            :         .endp = (void *) endp,
     432                 :            :       };
     433                 :            : 
     434                 :       6986 :       Dwarf_Attribute *attributes;
     435                 :       6986 :       Dwarf_Attribute *attributesp = NULL;
     436                 :       6986 :       Dwarf_Attribute nattributes[8];
     437         [ -  + ]:       6986 :       if (unlikely (proto->nforms > 8))
     438                 :            :         {
     439                 :          0 :           attributesp = malloc (sizeof (Dwarf_Attribute) * proto->nforms);
     440         [ #  # ]:          0 :           if (attributesp == NULL)
     441                 :            :             {
     442                 :          0 :               __libdw_seterrno (DWARF_E_NOMEM);
     443                 :          0 :               return -1;
     444                 :            :             }
     445                 :            :           attributes = attributesp;
     446                 :            :         }
     447                 :            :       else
     448                 :            :         attributes = &nattributes[0];
     449                 :            : 
     450         [ +  + ]:      20812 :       for (Dwarf_Word i = 0; i < proto->nforms; ++i)
     451                 :            :         {
     452                 :            :           /* We pretend this is a DW_AT[_GNU]_macros attribute so that
     453                 :            :              DW_FORM_sec_offset forms get correctly interpreted as
     454                 :            :              offset into .debug_macro.  XXX Deal with DW_MACRO_import_sup
     455                 :            :              (swap .dbg) for DW_FORM_sec_offset? */
     456                 :      27652 :           attributes[i].code = (fake_cu.version == 4 ? DW_AT_GNU_macros
     457         [ +  + ]:      13826 :                                                      : DW_AT_macros);
     458                 :      13826 :           attributes[i].form = proto->forms[i];
     459                 :      13826 :           attributes[i].valp = (void *) readp;
     460                 :      13826 :           attributes[i].cu = &fake_cu;
     461                 :            : 
     462                 :            :           /* We don't want forms that aren't allowed because they could
     463                 :            :              read from the "abbrev" like DW_FORM_implicit_const.  */
     464         [ -  + ]:      13826 :           if (! libdw_valid_user_form (attributes[i].form))
     465                 :            :             {
     466                 :          0 :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
     467                 :          0 :               free (attributesp);
     468                 :          0 :               return -1;
     469                 :            :             }
     470                 :            : 
     471                 :      13826 :           size_t len = __libdw_form_val_len (&fake_cu, proto->forms[i], readp);
     472         [ -  + ]:      13826 :           if (unlikely (len == (size_t) -1))
     473                 :            :             {
     474                 :          0 :               free (attributesp);
     475                 :          0 :               return -1;
     476                 :            :             }
     477                 :            : 
     478                 :      13826 :           readp += len;
     479                 :            :         }
     480                 :            : 
     481                 :       6986 :       Dwarf_Macro macro = {
     482                 :            :         .table = table,
     483                 :            :         .opcode = opcode,
     484                 :            :         .attributes = attributes,
     485                 :            :       };
     486                 :            : 
     487                 :       6986 :       int res = callback (&macro, arg);
     488         [ -  + ]:       6986 :       if (unlikely (attributesp != NULL))
     489                 :          0 :         free (attributesp);
     490                 :            : 
     491         [ +  - ]:       6986 :       if (res != DWARF_CB_OK)
     492                 :       6986 :         return readp - startp;
     493                 :            :     }
     494                 :            : 
     495                 :            :   return 0;
     496                 :            : }
     497                 :            : 
     498                 :            : /* Token layout:
     499                 :            : 
     500                 :            :    - The highest bit is used for distinguishing between callers that
     501                 :            :      know that opcode 0xff may have one of two incompatible meanings.
     502                 :            :      The mask that we use for selecting this bit is
     503                 :            :      DWARF_GETMACROS_START.
     504                 :            : 
     505                 :            :    - The rest of the token (31 or 63 bits) encodes address inside the
     506                 :            :      macro unit.
     507                 :            : 
     508                 :            :    Besides, token value of 0 signals end of iteration and -1 is
     509                 :            :    reserved for signaling errors.  That means it's impossible to
     510                 :            :    represent maximum offset of a .debug_macro unit to new-style
     511                 :            :    callers (which in practice decreases the permissible macro unit
     512                 :            :    size by another 1 byte).  */
     513                 :            : 
     514                 :            : static ptrdiff_t
     515                 :       7026 : token_from_offset (ptrdiff_t offset, bool accept_0xff)
     516                 :            : {
     517         [ +  + ]:       7026 :   if (offset == -1 || offset == 0)
     518                 :            :     return offset;
     519                 :            : 
     520                 :            :   /* Make sure the offset didn't overflow into the flag bit.  */
     521         [ -  + ]:       6986 :   if ((offset & DWARF_GETMACROS_START) != 0)
     522                 :            :     {
     523                 :          0 :       __libdw_seterrno (DWARF_E_TOO_BIG);
     524                 :          0 :       return -1;
     525                 :            :     }
     526                 :            : 
     527         [ +  + ]:       6986 :   if (accept_0xff)
     528                 :       5934 :     offset |= DWARF_GETMACROS_START;
     529                 :            : 
     530                 :            :   return offset;
     531                 :            : }
     532                 :            : 
     533                 :            : static ptrdiff_t
     534                 :       7026 : offset_from_token (ptrdiff_t token, bool *accept_0xffp)
     535                 :            : {
     536                 :       7026 :   *accept_0xffp = (token & DWARF_GETMACROS_START) != 0;
     537                 :       7026 :   token &= ~DWARF_GETMACROS_START;
     538                 :            : 
     539                 :       7026 :   return token;
     540                 :            : }
     541                 :            : 
     542                 :            : static ptrdiff_t
     543                 :       3692 : gnu_macros_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
     544                 :            :                           int (*callback) (Dwarf_Macro *, void *),
     545                 :            :                           void *arg, ptrdiff_t offset, bool accept_0xff,
     546                 :            :                           Dwarf_Die *cudie)
     547                 :            : {
     548         [ -  + ]:       3692 :   assert (offset >= 0);
     549                 :            : 
     550         [ -  + ]:       3692 :   if (macoff >= dbg->sectiondata[IDX_debug_macro]->d_size)
     551                 :            :     {
     552                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
     553                 :          0 :       return -1;
     554                 :            :     }
     555                 :            : 
     556                 :       3692 :   return read_macros (dbg, IDX_debug_macro, macoff,
     557                 :            :                       callback, arg, offset, accept_0xff, cudie);
     558                 :            : }
     559                 :            : 
     560                 :            : static ptrdiff_t
     561                 :       3334 : macro_info_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
     562                 :            :                           int (*callback) (Dwarf_Macro *, void *),
     563                 :            :                           void *arg, ptrdiff_t offset, Dwarf_Die *cudie)
     564                 :            : {
     565         [ -  + ]:       3334 :   assert (offset >= 0);
     566                 :            : 
     567                 :       3334 :   return read_macros (dbg, IDX_debug_macinfo, macoff,
     568                 :            :                       callback, arg, offset, true, cudie);
     569                 :            : }
     570                 :            : 
     571                 :            : ptrdiff_t
     572                 :        352 : dwarf_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
     573                 :            :                      int (*callback) (Dwarf_Macro *, void *),
     574                 :            :                      void *arg, ptrdiff_t token)
     575                 :            : {
     576         [ -  + ]:        352 :   if (dbg == NULL)
     577                 :            :     {
     578                 :          0 :       __libdw_seterrno (DWARF_E_NO_DWARF);
     579                 :          0 :       return -1;
     580                 :            :     }
     581                 :            : 
     582                 :        352 :   bool accept_0xff;
     583                 :        352 :   ptrdiff_t offset = offset_from_token (token, &accept_0xff);
     584         [ -  + ]:        352 :   assert (accept_0xff);
     585                 :            : 
     586                 :        352 :   offset = gnu_macros_getmacros_off (dbg, macoff, callback, arg, offset,
     587                 :            :                                      accept_0xff, NULL);
     588                 :            : 
     589                 :        352 :   return token_from_offset (offset, accept_0xff);
     590                 :            : }
     591                 :            : 
     592                 :            : ptrdiff_t
     593                 :       6674 : dwarf_getmacros (Dwarf_Die *cudie, int (*callback) (Dwarf_Macro *, void *),
     594                 :            :                  void *arg, ptrdiff_t token)
     595                 :            : {
     596         [ -  + ]:       6674 :   if (cudie == NULL)
     597                 :            :     {
     598                 :          0 :       __libdw_seterrno (DWARF_E_NO_DWARF);
     599                 :          0 :       return -1;
     600                 :            :     }
     601                 :            : 
     602                 :            :   /* This function might be called from a code that expects to see
     603                 :            :      DW_MACINFO_* opcodes, not DW_MACRO_{GNU_,}* ones.  It is fine to
     604                 :            :      serve most DW_MACRO_{GNU_,}* opcodes to such code, because those
     605                 :            :      whose values are the same as DW_MACINFO_* ones also have the same
     606                 :            :      behavior.  It is not very likely that a .debug_macro section
     607                 :            :      would only use the part of opcode space that it shares with
     608                 :            :      .debug_macinfo, but it is possible.  Serving the opcodes that are
     609                 :            :      only valid in DW_MACRO_{GNU_,}* domain is OK as well, because
     610                 :            :      clients in general need to be ready that newer standards define
     611                 :            :      more opcodes, and have coping mechanisms for unfamiliar opcodes.
     612                 :            : 
     613                 :            :      The one exception to the above rule is opcode 0xff, which has
     614                 :            :      concrete semantics in .debug_macinfo, but falls into vendor block
     615                 :            :      in .debug_macro, and can be assigned to do whatever.  There is
     616                 :            :      some small probability that the two opcodes would look
     617                 :            :      superficially similar enough that a client would be confused and
     618                 :            :      misbehave as a result.  For this reason, we refuse to serve
     619                 :            :      through this interface 0xff's originating from .debug_macro
     620                 :            :      unless the TOKEN that we obtained indicates the call originates
     621                 :            :      from a new-style caller.  See above for details on what
     622                 :            :      information is encoded into tokens.  */
     623                 :            : 
     624                 :       6674 :   bool accept_0xff;
     625                 :       6674 :   ptrdiff_t offset = offset_from_token (token, &accept_0xff);
     626                 :            : 
     627                 :            :   /* DW_AT_macro_info */
     628         [ +  + ]:       6674 :   if (dwarf_hasattr (cudie, DW_AT_macro_info))
     629                 :            :     {
     630                 :       3334 :       Dwarf_Word macoff;
     631         [ -  + ]:       3334 :       if (get_offset_from (cudie, DW_AT_macro_info, &macoff) != 0)
     632                 :          0 :         return -1;
     633                 :       3334 :       offset = macro_info_getmacros_off (cudie->cu->dbg, macoff,
     634                 :            :                                          callback, arg, offset, cudie);
     635                 :            :     }
     636                 :            :   else
     637                 :            :     {
     638                 :            :       /* DW_AT_GNU_macros, DW_AT_macros */
     639                 :       3340 :       Dwarf_Word macoff;
     640         [ +  + ]:       3340 :       if (get_offset_from (cudie, DW_AT_GNU_macros, &macoff) != 0
     641         [ -  + ]:       2802 :           && get_offset_from (cudie, DW_AT_macros, &macoff) != 0)
     642                 :          0 :         return -1;
     643                 :       3340 :       offset = gnu_macros_getmacros_off (cudie->cu->dbg, macoff,
     644                 :            :                                          callback, arg, offset, accept_0xff,
     645                 :            :                                          cudie);
     646                 :            :     }
     647                 :            : 
     648                 :       6674 :   return token_from_offset (offset, accept_0xff);
     649                 :            : }

Generated by: LCOV version 1.16