LCOV - code coverage report
Current view: top level - libdw - cie.c (source / functions) Coverage Total Hit
Test: elfutils-0.197 Lines: 80.6 % 98 79
Test Date: 2026-10-19 20:07:25 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 68.1 % 47 32

             Branch data     Line data    Source code
       1                 :             : /* CIE reading.
       2                 :             :    Copyright (C) 2009-2010 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 "cfi.h"
      34                 :             : #include "encoded-value.h"
      35                 :             : #include <assert.h>
      36                 :             : #include <stdlib.h>
      37                 :             : #include "eu-search.h"
      38                 :             : 
      39                 :             : 
      40                 :             : static int
      41                 :       70303 : compare_cie (const void *a, const void *b)
      42                 :             : {
      43                 :       70303 :   const struct dwarf_cie *cie1 = a;
      44                 :       70303 :   const struct dwarf_cie *cie2 = b;
      45         [ +  + ]:       70303 :   if (cie1->offset < cie2->offset)
      46                 :             :     return -1;
      47         [ +  + ]:       57611 :   if (cie1->offset > cie2->offset)
      48                 :        4708 :     return 1;
      49                 :             :   return 0;
      50                 :             : }
      51                 :             : 
      52                 :             : /* There is no CIE at OFFSET in the tree.  Add it.  */
      53                 :             : static struct dwarf_cie *
      54                 :         328 : intern_new_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
      55                 :             : {
      56                 :         328 :   struct dwarf_cie *cie = malloc (sizeof (struct dwarf_cie));
      57         [ -  + ]:         328 :   if (cie == NULL)
      58                 :             :     {
      59                 :           0 :       __libdw_seterrno (DWARF_E_NOMEM);
      60                 :           0 :       return NULL;
      61                 :             :     }
      62                 :             : 
      63                 :         328 :   cie->offset = offset;
      64                 :         328 :   cie->code_alignment_factor = info->code_alignment_factor;
      65                 :         328 :   cie->data_alignment_factor = info->data_alignment_factor;
      66                 :         328 :   cie->return_address_register = info->return_address_register;
      67                 :             : 
      68                 :         328 :   cie->fde_augmentation_data_size = 0;
      69                 :         328 :   cie->sized_augmentation_data = false;
      70                 :         328 :   cie->signal_frame = false;
      71                 :             : 
      72                 :         328 :   cie->fde_encoding = DW_EH_PE_absptr;
      73                 :         328 :   cie->lsda_encoding = DW_EH_PE_omit;
      74                 :             : 
      75                 :             :   /* Grok the augmentation string and its data.  */
      76                 :         328 :   const uint8_t *data = info->augmentation_data;
      77                 :         328 :   const uint8_t *const limit = data + info->augmentation_data_size;
      78                 :         328 :   const char *ap = info->augmentation;
      79                 :             :   /* If present, 'z' must be the first char.  */
      80         [ +  + ]:         328 :   if (*ap == 'z')
      81                 :             :     {
      82                 :         242 :       cie->sized_augmentation_data = true;
      83                 :         242 :       ap++;
      84                 :             :     }
      85         [ +  + ]:         646 :   for (; *ap != '\0'; ++ap)
      86                 :             :     {
      87                 :         318 :       uint8_t encoding;
      88   [ +  +  +  +  :         318 :       switch (*ap)
                      - ]
      89                 :             :         {
      90                 :           6 :         case 'S':
      91                 :           6 :           cie->signal_frame = true;
      92                 :           6 :           continue;
      93                 :             : 
      94                 :          34 :         case 'L':               /* LSDA pointer encoding byte.  */
      95         [ +  - ]:          34 :           if (cie->sized_augmentation_data)
      96                 :             :             {
      97         [ -  + ]:          34 :               if (data >= limit)
      98                 :           0 :                 goto invalid;
      99                 :          34 :               cie->lsda_encoding = *data++;
     100                 :             :             }
     101         [ -  + ]:          34 :           if (!cie->sized_augmentation_data)
     102                 :           0 :             cie->fde_augmentation_data_size
     103                 :           0 :               += encoded_value_size (&cache->data->d, cache->e_ident,
     104                 :             :                                      cie->lsda_encoding, NULL);
     105                 :          34 :           continue;
     106                 :             : 
     107                 :         236 :         case 'R':               /* FDE address encoding byte.  */
     108         [ +  - ]:         236 :           if (cie->sized_augmentation_data)
     109                 :             :             {
     110         [ -  + ]:         236 :               if (data >= limit)
     111                 :           0 :                 goto invalid;
     112                 :         236 :               cie->fde_encoding = *data++;
     113                 :             :             }
     114                 :         236 :           continue;
     115                 :             : 
     116                 :          42 :         case 'P':               /* Skip personality routine.  */
     117         [ +  - ]:          42 :           if (cie->sized_augmentation_data)
     118                 :             :             {
     119         [ -  + ]:          42 :               if (data >= limit)
     120                 :           0 :                 goto invalid;
     121                 :          42 :               encoding = *data++;
     122                 :          42 :               data += encoded_value_size (&cache->data->d, cache->e_ident,
     123                 :             :                                           encoding, data);
     124                 :             :             }
     125                 :          42 :           continue;
     126                 :             : 
     127                 :           0 :         default:
     128                 :             :           /* Unknown augmentation string.  If we have 'z' we can ignore it,
     129                 :             :              otherwise we must bail out.  */
     130         [ #  # ]:           0 :           if (cie->sized_augmentation_data)
     131                 :           0 :             continue;
     132                 :             :         }
     133                 :             :       /* We only get here when we need to bail out.  */
     134                 :             :       break;
     135                 :             :     }
     136                 :             : 
     137         [ +  + ]:         328 :   if ((cie->fde_encoding & 0x0f) == DW_EH_PE_absptr)
     138                 :             :     {
     139                 :             :       /* Canonicalize encoding to a specific size.  */
     140                 :          92 :       assert (DW_EH_PE_absptr == 0);
     141                 :             : 
     142                 :             :       /* XXX should get from dwarf_next_cfi with v4 header.  */
     143                 :          92 :       uint_fast8_t address_size
     144         [ +  + ]:          92 :         = cache->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
     145                 :          92 :       switch (address_size)
     146                 :             :         {
     147                 :             :         case 8:
     148                 :          56 :           cie->fde_encoding |= DW_EH_PE_udata8;
     149                 :          56 :           break;
     150                 :             :         case 4:
     151                 :          36 :           cie->fde_encoding |= DW_EH_PE_udata4;
     152                 :          36 :           break;
     153                 :             :         default:
     154                 :             :           free (cie);
     155                 :             :           __libdw_seterrno (DWARF_E_INVALID_DWARF);
     156                 :             :           return NULL;
     157                 :             :         }
     158                 :             :     }
     159                 :             : 
     160                 :             :   /* Save the initial instructions to be played out into initial state.  */
     161                 :         328 :   cie->initial_instructions = info->initial_instructions;
     162                 :         328 :   cie->initial_instructions_end = info->initial_instructions_end;
     163                 :         328 :   cie->initial_state = NULL;
     164                 :             : 
     165                 :             :   /* Add the new entry to the search tree.  */
     166         [ -  + ]:         328 :   if (eu_tsearch (cie, &cache->cie_tree, &compare_cie) == NULL)
     167                 :             :     {
     168                 :           0 :       free (cie);
     169                 :           0 :       __libdw_seterrno (DWARF_E_NOMEM);
     170                 :           0 :       return NULL;
     171                 :             :     }
     172                 :             : 
     173                 :             :   return cie;
     174                 :             : 
     175                 :           0 :  invalid:
     176                 :           0 :   free (cie);
     177                 :           0 :   __libdw_seterrno (DWARF_E_INVALID_DWARF);
     178                 :           0 :   return NULL;
     179                 :             : }
     180                 :             : 
     181                 :             : /* Look up a CIE_pointer for random access.  */
     182                 :             : struct dwarf_cie *
     183                 :             : internal_function
     184                 :       53035 : __libdw_find_cie (Dwarf_CFI *cache, Dwarf_Off offset)
     185                 :             : {
     186                 :       53035 :   const struct dwarf_cie cie_key = { .offset = offset };
     187                 :       53035 :   struct dwarf_cie **found = eu_tfind (&cie_key, &cache->cie_tree,
     188                 :             :                                        &compare_cie);
     189         [ +  + ]:       53035 :   if (found != NULL)
     190                 :       52903 :     return *found;
     191                 :             : 
     192                 :             :   /* We have not read this CIE yet.  Go find it.  */
     193                 :         132 :   Dwarf_Off next_offset = offset;
     194                 :         132 :   Dwarf_CFI_Entry entry;
     195                 :         264 :   int result = INTUSE(dwarf_next_cfi) (cache->e_ident,
     196                 :         132 :                                        &cache->data->d, CFI_IS_EH (cache),
     197                 :             :                                        offset, &next_offset, &entry);
     198   [ +  -  -  + ]:         132 :   if (result != 0 || entry.cie.CIE_id != DW_CIE_ID_64)
     199                 :             :     {
     200                 :           0 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     201                 :           0 :       return NULL;
     202                 :             :     }
     203                 :             : 
     204                 :             :   /* If this happened to be what we would have read next, notice it.  */
     205         [ +  + ]:         132 :   if (cache->next_offset == offset)
     206                 :          86 :     cache->next_offset = next_offset;
     207                 :             : 
     208                 :         132 :   return intern_new_cie (cache, offset, &entry.cie);
     209                 :             : }
     210                 :             : 
     211                 :             : /* Enter a CIE encountered while reading through for FDEs.  */
     212                 :             : void
     213                 :             : internal_function
     214                 :         196 : __libdw_intern_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
     215                 :             : {
     216                 :         196 :   const struct dwarf_cie cie_key = { .offset = offset };
     217                 :         196 :   struct dwarf_cie **found = eu_tfind (&cie_key, &cache->cie_tree,
     218                 :             :                                        &compare_cie);
     219         [ +  - ]:         196 :   if (found == NULL)
     220                 :             :     /* We have not read this CIE yet.  Enter it.  */
     221                 :         196 :     (void) intern_new_cie (cache, offset, info);
     222                 :         196 : }
        

Generated by: LCOV version 2.0-1