LCOV - code coverage report
Current view: top level - libdw - libdwP.h (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 237 274 86.5 %
Date: 2024-03-24 18:28:45 Functions: 18 18 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 150 232 64.7 %

           Branch data     Line data    Source code
       1                 :            : /* Internal definitions for libdw.
       2                 :            :    Copyright (C) 2002-2011, 2013-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                 :            : #ifndef _LIBDWP_H
      30                 :            : #define _LIBDWP_H 1
      31                 :            : 
      32                 :            : #include <stdbool.h>
      33                 :            : #include <pthread.h>
      34                 :            : 
      35                 :            : #include <libdw.h>
      36                 :            : #include <dwarf.h>
      37                 :            : 
      38                 :            : 
      39                 :            : /* Known location expressions already decoded.  */
      40                 :            : struct loc_s
      41                 :            : {
      42                 :            :   void *addr;
      43                 :            :   Dwarf_Op *loc;
      44                 :            :   size_t nloc;
      45                 :            : };
      46                 :            : 
      47                 :            : /* Known DW_OP_implicit_value blocks already decoded.
      48                 :            :    This overlaps struct loc_s exactly, but only the
      49                 :            :    first member really has to match.  */
      50                 :            : struct loc_block_s
      51                 :            : {
      52                 :            :   void *addr;
      53                 :            :   unsigned char *data;
      54                 :            :   size_t length;
      55                 :            : };
      56                 :            : 
      57                 :            : /* Already decoded .debug_line units.  */
      58                 :            : struct files_lines_s
      59                 :            : {
      60                 :            :   Dwarf_Off debug_line_offset;
      61                 :            :   Dwarf_Files *files;
      62                 :            :   Dwarf_Lines *lines;
      63                 :            : };
      64                 :            : 
      65                 :            : /* Valid indices for the section data.  */
      66                 :            : enum
      67                 :            :   {
      68                 :            :     IDX_debug_info = 0,
      69                 :            :     IDX_debug_types,
      70                 :            :     IDX_debug_abbrev,
      71                 :            :     IDX_debug_aranges,
      72                 :            :     IDX_debug_addr,
      73                 :            :     IDX_debug_line,
      74                 :            :     IDX_debug_line_str,
      75                 :            :     IDX_debug_frame,
      76                 :            :     IDX_debug_loc,
      77                 :            :     IDX_debug_loclists,
      78                 :            :     IDX_debug_pubnames,
      79                 :            :     IDX_debug_str,
      80                 :            :     IDX_debug_str_offsets,
      81                 :            :     IDX_debug_macinfo,
      82                 :            :     IDX_debug_macro,
      83                 :            :     IDX_debug_ranges,
      84                 :            :     IDX_debug_rnglists,
      85                 :            :     IDX_debug_cu_index,
      86                 :            :     IDX_debug_tu_index,
      87                 :            :     IDX_gnu_debugaltlink,
      88                 :            :     IDX_last
      89                 :            :   };
      90                 :            : 
      91                 :            : /* Valid indices for the string section's information.  */
      92                 :            : enum string_section_index
      93                 :            :   {
      94                 :            :     STR_SCN_IDX_debug_line_str,
      95                 :            :     STR_SCN_IDX_debug_str,
      96                 :            :     STR_SCN_IDX_last
      97                 :            :   };
      98                 :            : 
      99                 :            : /* Error values.  */
     100                 :            : enum
     101                 :            : {
     102                 :            :   DWARF_E_NOERROR = 0,
     103                 :            :   DWARF_E_UNKNOWN_ERROR,
     104                 :            :   DWARF_E_INVALID_ACCESS,
     105                 :            :   DWARF_E_NO_REGFILE,
     106                 :            :   DWARF_E_IO_ERROR,
     107                 :            :   DWARF_E_INVALID_ELF,
     108                 :            :   DWARF_E_NO_DWARF,
     109                 :            :   DWARF_E_COMPRESSED_ERROR,
     110                 :            :   DWARF_E_NOELF,
     111                 :            :   DWARF_E_GETEHDR_ERROR,
     112                 :            :   DWARF_E_NOMEM,
     113                 :            :   DWARF_E_UNIMPL,
     114                 :            :   DWARF_E_INVALID_CMD,
     115                 :            :   DWARF_E_INVALID_VERSION,
     116                 :            :   DWARF_E_INVALID_FILE,
     117                 :            :   DWARF_E_NO_ENTRY,
     118                 :            :   DWARF_E_INVALID_DWARF,
     119                 :            :   DWARF_E_NO_STRING,
     120                 :            :   DWARF_E_NO_DEBUG_STR,
     121                 :            :   DWARF_E_NO_DEBUG_LINE_STR,
     122                 :            :   DWARF_E_NO_STR_OFFSETS,
     123                 :            :   DWARF_E_NO_ADDR,
     124                 :            :   DWARF_E_NO_CONSTANT,
     125                 :            :   DWARF_E_NO_REFERENCE,
     126                 :            :   DWARF_E_INVALID_REFERENCE,
     127                 :            :   DWARF_E_NO_DEBUG_LINE,
     128                 :            :   DWARF_E_INVALID_DEBUG_LINE,
     129                 :            :   DWARF_E_TOO_BIG,
     130                 :            :   DWARF_E_VERSION,
     131                 :            :   DWARF_E_INVALID_DIR_IDX,
     132                 :            :   DWARF_E_ADDR_OUTOFRANGE,
     133                 :            :   DWARF_E_NO_DEBUG_LOC,
     134                 :            :   DWARF_E_NO_DEBUG_LOCLISTS,
     135                 :            :   DWARF_E_NO_LOC_VALUE,
     136                 :            :   DWARF_E_NO_BLOCK,
     137                 :            :   DWARF_E_INVALID_LINE_IDX,
     138                 :            :   DWARF_E_INVALID_ARANGE_IDX,
     139                 :            :   DWARF_E_NO_MATCH,
     140                 :            :   DWARF_E_NO_FLAG,
     141                 :            :   DWARF_E_INVALID_OFFSET,
     142                 :            :   DWARF_E_NO_DEBUG_RANGES,
     143                 :            :   DWARF_E_NO_DEBUG_RNGLISTS,
     144                 :            :   DWARF_E_INVALID_CFI,
     145                 :            :   DWARF_E_NO_ALT_DEBUGLINK,
     146                 :            :   DWARF_E_INVALID_OPCODE,
     147                 :            :   DWARF_E_NOT_CUDIE,
     148                 :            :   DWARF_E_UNKNOWN_LANGUAGE,
     149                 :            :   DWARF_E_NO_DEBUG_ADDR,
     150                 :            :   DWARF_E_UNKNOWN_SECTION,
     151                 :            : };
     152                 :            : 
     153                 :            : 
     154                 :            : #include "dwarf_sig8_hash.h"
     155                 :            : 
     156                 :            : /* The type of Dwarf object, sorted by preference
     157                 :            :    (if there is a higher order type, we pick that one over the others).  */
     158                 :            : enum dwarf_type
     159                 :            :   {
     160                 :            :     TYPE_UNKNOWN = 0,
     161                 :            :     TYPE_GNU_LTO = 16,
     162                 :            :     TYPE_DWO = 32,
     163                 :            :     TYPE_PLAIN = 64,
     164                 :            :   };
     165                 :            : 
     166                 :            : /* This is the structure representing the debugging state.  */
     167                 :            : struct Dwarf
     168                 :            : {
     169                 :            :   /* The underlying ELF file.  */
     170                 :            :   Elf *elf;
     171                 :            : 
     172                 :            :   /* The (absolute) path to the ELF file, if known.  To help locating
     173                 :            :      dwp files.  */
     174                 :            :   char *elfpath;
     175                 :            : 
     176                 :            :   /* The (absolute) path to the ELF dir, if known.  To help locating
     177                 :            :      alt and dwo files.  */
     178                 :            :   char *debugdir;
     179                 :            : 
     180                 :            :   /* dwz alternate DWARF file.  */
     181                 :            :   Dwarf *alt_dwarf;
     182                 :            : 
     183                 :            :   /* DWARF package file.  */
     184                 :            :   Dwarf *dwp_dwarf;
     185                 :            : 
     186                 :            :   /* The section data.  */
     187                 :            :   Elf_Data *sectiondata[IDX_last];
     188                 :            : 
     189                 :            :   /* Size of a prefix of string sections, where any string will be
     190                 :            :      null-terminated. */
     191                 :            :   size_t string_section_size[STR_SCN_IDX_last];
     192                 :            : 
     193                 :            :   /* True if the file has a byte order different from the host.  */
     194                 :            :   bool other_byte_order;
     195                 :            : 
     196                 :            :   /* If true, we allocated the ELF descriptor ourselves.  */
     197                 :            :   bool free_elf;
     198                 :            : 
     199                 :            :   /* If >= 0, we allocated the alt_dwarf ourselves and must end it and
     200                 :            :      close this file descriptor.  */
     201                 :            :   int alt_fd;
     202                 :            : 
     203                 :            :   /* File descriptor of DWARF package file.  */
     204                 :            :   int dwp_fd;
     205                 :            : 
     206                 :            :   /* Information for traversing the .debug_pubnames section.  This is
     207                 :            :      an array and separately allocated with malloc.  */
     208                 :            :   struct pubnames_s
     209                 :            :   {
     210                 :            :     Dwarf_Off cu_offset;
     211                 :            :     Dwarf_Off set_start;
     212                 :            :     unsigned int cu_header_size;
     213                 :            :     int address_len;
     214                 :            :   } *pubnames_sets;
     215                 :            :   size_t pubnames_nsets;
     216                 :            : 
     217                 :            :   /* Search tree for the CUs.  */
     218                 :            :   void *cu_tree;
     219                 :            :   Dwarf_Off next_cu_offset;
     220                 :            : 
     221                 :            :   /* Search tree and sig8 hash table for .debug_types type units.  */
     222                 :            :   void *tu_tree;
     223                 :            :   Dwarf_Off next_tu_offset;
     224                 :            :   Dwarf_Sig8_Hash sig8_hash;
     225                 :            : 
     226                 :            :   /* Search tree for split Dwarf associated with CUs in this debug.  */
     227                 :            :   void *split_tree;
     228                 :            : 
     229                 :            :   /* Search tree for .debug_macro operator tables.  */
     230                 :            :   void *macro_ops;
     231                 :            : 
     232                 :            :   /* Search tree for decoded .debug_line units.  */
     233                 :            :   void *files_lines;
     234                 :            : 
     235                 :            :   /* Address ranges read from .debug_aranges.  */
     236                 :            :   Dwarf_Aranges *aranges;
     237                 :            : 
     238                 :            :   /* Address ranges inferred from CUs.  */
     239                 :            :   Dwarf_Aranges *dieranges;
     240                 :            : 
     241                 :            :   /* Cached info from the CFI section.  */
     242                 :            :   struct Dwarf_CFI_s *cfi;
     243                 :            : 
     244                 :            :   /* DWARF package file CU index section.  */
     245                 :            :   struct Dwarf_Package_Index_s *cu_index;
     246                 :            :   /* DWARF package file TU index section.  */
     247                 :            :   struct Dwarf_Package_Index_s *tu_index;
     248                 :            : 
     249                 :            :   /* Fake loc CU.  Used when synthesizing attributes for Dwarf_Ops that
     250                 :            :      came from a location list entry in dwarf_getlocation_attr.
     251                 :            :      Depending on version this is the .debug_loc or .debug_loclists
     252                 :            :      section (could be both if mixing CUs with different DWARF versions).  */
     253                 :            :   struct Dwarf_CU *fake_loc_cu;
     254                 :            :   struct Dwarf_CU *fake_loclists_cu;
     255                 :            : 
     256                 :            :   /* Similar for addrx/constx, which will come from .debug_addr section.  */
     257                 :            :   struct Dwarf_CU *fake_addr_cu;
     258                 :            : 
     259                 :            :   enum dwarf_type type;
     260                 :            : 
     261                 :            :   /* Supporting lock for internal memory handling.  Ensures threads that have
     262                 :            :      an entry in the mem_tails array are not disturbed by new threads doing
     263                 :            :      allocations for this Dwarf.  */
     264                 :            :   pthread_rwlock_t mem_rwl;
     265                 :            : 
     266                 :            :   /* Internal memory handling.  This is basically a simplified thread-local
     267                 :            :      reimplementation of obstacks.  Unfortunately the standard obstack
     268                 :            :      implementation is not usable in libraries.  */
     269                 :            :   size_t mem_stacks;
     270                 :            :   struct libdw_memblock
     271                 :            :   {
     272                 :            :     size_t size;
     273                 :            :     size_t remaining;
     274                 :            :     struct libdw_memblock *prev;
     275                 :            :     char mem[0];
     276                 :            :   } **mem_tails;
     277                 :            : 
     278                 :            :   /* Default size of allocated memory blocks.  */
     279                 :            :   size_t mem_default_size;
     280                 :            : 
     281                 :            :   /* Registered OOM handler.  */
     282                 :            :   Dwarf_OOM oom_handler;
     283                 :            : };
     284                 :            : 
     285                 :            : 
     286                 :            : /* Abbreviation representation.  */
     287                 :            : struct Dwarf_Abbrev
     288                 :            : {
     289                 :            :   Dwarf_Off offset;       /* Offset to start of abbrev into .debug_abbrev.  */
     290                 :            :   unsigned char *attrp;   /* Pointer to start of attribute name/form pairs. */
     291                 :            :   bool has_children : 1;  /* Whether or not the DIE has children. */
     292                 :            :   unsigned int code : 31; /* The (unique) abbrev code.  */
     293                 :            :   unsigned int tag;       /* The tag of the DIE. */
     294                 :            : } attribute_packed;
     295                 :            : 
     296                 :            : #include "dwarf_abbrev_hash.h"
     297                 :            : 
     298                 :            : 
     299                 :            : /* Files in line information records.  */
     300                 :            : struct Dwarf_Files_s
     301                 :            :   {
     302                 :            :     unsigned int ndirs;
     303                 :            :     unsigned int nfiles;
     304                 :            :     struct Dwarf_Fileinfo_s
     305                 :            :     {
     306                 :            :       char *name;
     307                 :            :       Dwarf_Word mtime;
     308                 :            :       Dwarf_Word length;
     309                 :            :     } info[0];
     310                 :            :     /* nfiles of those, followed by char *[ndirs].  */
     311                 :            :   };
     312                 :            : typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
     313                 :            : 
     314                 :            : 
     315                 :            : /* Representation of a row in the line table.  */
     316                 :            : 
     317                 :            : struct Dwarf_Line_s
     318                 :            : {
     319                 :            :   Dwarf_Files *files;
     320                 :            : 
     321                 :            :   Dwarf_Addr addr;
     322                 :            :   unsigned int file;
     323                 :            :   int line;
     324                 :            :   unsigned short int column;
     325                 :            :   unsigned int is_stmt:1;
     326                 :            :   unsigned int basic_block:1;
     327                 :            :   unsigned int end_sequence:1;
     328                 :            :   unsigned int prologue_end:1;
     329                 :            :   unsigned int epilogue_begin:1;
     330                 :            :   /* The remaining bit fields are not flags, but hold values presumed to be
     331                 :            :      small.  All the flags and other bit fields should add up to 48 bits
     332                 :            :      to give the whole struct a nice round size.  */
     333                 :            :   unsigned int op_index:8;
     334                 :            :   unsigned int isa:8;
     335                 :            :   unsigned int discriminator:24;
     336                 :            :   /* These are currently only used for the NVIDIA extensions.  */
     337                 :            :   unsigned int context;
     338                 :            :   unsigned int function_name;
     339                 :            : };
     340                 :            : 
     341                 :            : struct Dwarf_Lines_s
     342                 :            : {
     343                 :            :   size_t nlines;
     344                 :            :   struct Dwarf_Line_s info[0];
     345                 :            : };
     346                 :            : 
     347                 :            : /* Representation of address ranges.  */
     348                 :            : struct Dwarf_Aranges_s
     349                 :            : {
     350                 :            :   Dwarf *dbg;
     351                 :            :   size_t naranges;
     352                 :            : 
     353                 :            :   struct Dwarf_Arange_s
     354                 :            :   {
     355                 :            :     Dwarf_Addr addr;
     356                 :            :     Dwarf_Word length;
     357                 :            :     Dwarf_Off offset;
     358                 :            :   } info[0];
     359                 :            : };
     360                 :            : 
     361                 :            : /* DWARF package file unit index.  */
     362                 :            : typedef struct Dwarf_Package_Index_s
     363                 :            : {
     364                 :            :   Dwarf *dbg;
     365                 :            :   uint32_t section_count;
     366                 :            :   uint32_t unit_count;
     367                 :            :   uint32_t slot_count;
     368                 :            :   /* Mapping from DW_SECT_* - 1 to column number in the section tables, or
     369                 :            :      UINT32_MAX if not present.  */
     370                 :            :   uint32_t sections[DW_SECT_RNGLISTS];
     371                 :            :   /* Row number of last unit found in the index.  */
     372                 :            :   uint32_t last_unit_found;
     373                 :            :   const unsigned char *hash_table;
     374                 :            :   const unsigned char *indices;
     375                 :            :   const unsigned char *section_offsets;
     376                 :            :   const unsigned char *section_sizes;
     377                 :            :   /* If DW_SECT_INFO section offsets were truncated to 32 bits, recovered
     378                 :            :      64-bit offsets.  */
     379                 :            :   Dwarf_Off *debug_info_offsets;
     380                 :            : } Dwarf_Package_Index;
     381                 :            : 
     382                 :            : /* CU representation.  */
     383                 :            : struct Dwarf_CU
     384                 :            : {
     385                 :            :   Dwarf *dbg;
     386                 :            :   Dwarf_Off start;
     387                 :            :   Dwarf_Off end;
     388                 :            :   /* Row number of this unit in DWARF package file index.  */
     389                 :            :   uint32_t dwp_row;
     390                 :            :   uint8_t address_size;
     391                 :            :   uint8_t offset_size;
     392                 :            :   uint16_t version;
     393                 :            : 
     394                 :            :   size_t sec_idx; /* Normally .debug_info, could be .debug_type or "fake". */
     395                 :            : 
     396                 :            :   /* The unit type if version >= 5.  Otherwise 0 for normal CUs (from
     397                 :            :      .debug_info) or 1 for v4 type units (from .debug_types).  */
     398                 :            :   uint8_t unit_type;
     399                 :            : 
     400                 :            :   /* Zero if the unit type doesn't support a die/type offset and/or id/sig.
     401                 :            :      Nonzero if it is a v4 type unit or for DWARFv5 units depending on
     402                 :            :      unit_type.  */
     403                 :            :   size_t subdie_offset;
     404                 :            :   uint64_t unit_id8;
     405                 :            : 
     406                 :            :   /* If this is a skeleton unit this points to the split compile unit.
     407                 :            :      Or the other way around if this is a split compile unit.  Set to -1
     408                 :            :      if not yet searched.  Always use __libdw_find_split_unit to access
     409                 :            :      this field.  */
     410                 :            :   struct Dwarf_CU *split;
     411                 :            : 
     412                 :            :   /* Hash table for the abbreviations.  */
     413                 :            :   Dwarf_Abbrev_Hash abbrev_hash;
     414                 :            :   /* Offset of the first abbreviation.  */
     415                 :            :   size_t orig_abbrev_offset;
     416                 :            :   /* Offset past last read abbreviation.  */
     417                 :            :   size_t last_abbrev_offset;
     418                 :            : 
     419                 :            :   /* The srcline information.  */
     420                 :            :   Dwarf_Lines *lines;
     421                 :            : 
     422                 :            :   /* The source file information.  */
     423                 :            :   Dwarf_Files *files;
     424                 :            : 
     425                 :            :   /* Known location lists.  */
     426                 :            :   void *locs;
     427                 :            : 
     428                 :            :   /* Base address for use with ranges and locs.
     429                 :            :      Don't access directly, call __libdw_cu_base_address.  */
     430                 :            :   Dwarf_Addr base_address;
     431                 :            : 
     432                 :            :   /* The offset into the .debug_addr section where index zero begins.
     433                 :            :      Don't access directly, call __libdw_cu_addr_base.  */
     434                 :            :   Dwarf_Off addr_base;
     435                 :            : 
     436                 :            :   /* The offset into the .debug_str_offsets section where index zero begins.
     437                 :            :      Don't access directly, call __libdw_cu_str_off_base.  */
     438                 :            :   Dwarf_Off str_off_base;
     439                 :            : 
     440                 :            :   /* The offset into the .debug_ranges section to use for GNU
     441                 :            :      DebugFission split units.  Don't access directly, call
     442                 :            :      __libdw_cu_ranges_base.  */
     443                 :            :   Dwarf_Off ranges_base;
     444                 :            : 
     445                 :            :   /* The start of the offset table in .debug_loclists.
     446                 :            :      Don't access directly, call __libdw_cu_locs_base.  */
     447                 :            :   Dwarf_Off locs_base;
     448                 :            : 
     449                 :            :   /* Memory boundaries of this CU.  */
     450                 :            :   void *startp;
     451                 :            :   void *endp;
     452                 :            : };
     453                 :            : 
     454                 :            : /* Aliases to avoid PLTs.  */
     455                 :            : INTDECL (dwarf_aggregate_size)
     456                 :            : INTDECL (dwarf_attr)
     457                 :            : INTDECL (dwarf_attr_integrate)
     458                 :            : INTDECL (dwarf_begin)
     459                 :            : INTDECL (dwarf_begin_elf)
     460                 :            : INTDECL (dwarf_child)
     461                 :            : INTDECL (dwarf_cu_dwp_section_info)
     462                 :            : INTDECL (dwarf_default_lower_bound)
     463                 :            : INTDECL (dwarf_dieoffset)
     464                 :            : INTDECL (dwarf_diename)
     465                 :            : INTDECL (dwarf_end)
     466                 :            : INTDECL (dwarf_entrypc)
     467                 :            : INTDECL (dwarf_errmsg)
     468                 :            : INTDECL (dwarf_formaddr)
     469                 :            : INTDECL (dwarf_formblock)
     470                 :            : INTDECL (dwarf_formref_die)
     471                 :            : INTDECL (dwarf_formsdata)
     472                 :            : INTDECL (dwarf_formstring)
     473                 :            : INTDECL (dwarf_formudata)
     474                 :            : INTDECL (dwarf_getabbrevattr_data)
     475                 :            : INTDECL (dwarf_getalt)
     476                 :            : INTDECL (dwarf_getarange_addr)
     477                 :            : INTDECL (dwarf_getarangeinfo)
     478                 :            : INTDECL (dwarf_getaranges)
     479                 :            : INTDECL (dwarf_getlocation_die)
     480                 :            : INTDECL (dwarf_getsrcfiles)
     481                 :            : INTDECL (dwarf_getsrclines)
     482                 :            : INTDECL (dwarf_get_units)
     483                 :            : INTDECL (dwarf_hasattr)
     484                 :            : INTDECL (dwarf_haschildren)
     485                 :            : INTDECL (dwarf_haspc)
     486                 :            : INTDECL (dwarf_highpc)
     487                 :            : INTDECL (dwarf_lowpc)
     488                 :            : INTDECL (dwarf_nextcu)
     489                 :            : INTDECL (dwarf_next_unit)
     490                 :            : INTDECL (dwarf_offdie)
     491                 :            : INTDECL (dwarf_peel_type)
     492                 :            : INTDECL (dwarf_ranges)
     493                 :            : INTDECL (dwarf_setalt)
     494                 :            : INTDECL (dwarf_siblingof)
     495                 :            : INTDECL (dwarf_srclang)
     496                 :            : INTDECL (dwarf_tag)
     497                 :            : 
     498                 :            : #define ISV4TU(cu) ((cu)->version == 4 && (cu)->sec_idx == IDX_debug_types)
     499                 :            : 
     500                 :            : /* Compute the offset of a CU's first DIE from the CU offset.
     501                 :            :    CU must be a valid/known version/unit_type.  */
     502                 :            : static inline Dwarf_Off
     503                 :    7643286 : __libdw_first_die_from_cu_start (Dwarf_Off cu_start,
     504                 :            :                                  uint8_t offset_size,
     505                 :            :                                  uint16_t version,
     506                 :            :                                  uint8_t unit_type)
     507                 :            : {
     508                 :            : /*
     509                 :            :   assert (offset_size == 4 || offset_size == 8);
     510                 :            :   assert (version >= 2 && version <= 5);
     511                 :            :   assert (unit_type == DW_UT_compile
     512                 :            :           || unit_type == DW_UT_partial
     513                 :            :           || unit_type == DW_UT_skeleton
     514                 :            :           || unit_type == DW_UT_split_compile
     515                 :            :           || unit_type == DW_UT_type
     516                 :            :           || unit_type == DW_UT_split_type);
     517                 :            : */
     518                 :            : 
     519                 :    7643286 :   Dwarf_Off off = cu_start;
     520         [ +  + ]:    7643286 :   if (version < 5)
     521                 :            :     {
     522                 :            :    /*
     523                 :            :         LEN       VER     OFFSET    ADDR
     524                 :            :       4-bytes + 2-bytes + 4-bytes + 1-byte  for 32-bit dwarf
     525                 :            :      12-bytes + 2-bytes + 8-bytes + 1-byte  for 64-bit dwarf
     526                 :            :    or in .debug_types,                       SIGNATURE TYPE-OFFSET
     527                 :            :       4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes  for 32-bit
     528                 :            :      12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes  for 64-bit
     529                 :            : 
     530                 :            :    Note the trick in the computation.  If the offset_size is 4
     531                 :            :    the '- 4' term changes the '3 *' (or '4 *') into a '2 *' (or '3 *).
     532                 :            :    If the offset_size is 8 it accounts for the 4-byte escape value
     533                 :            :    used at the start of the length.  */
     534         [ +  + ]:     209746 :       if (unit_type != DW_UT_type)
     535                 :     209632 :         off += 3 * offset_size - 4 + 3;
     536                 :            :       else
     537                 :        114 :         off += 4 * offset_size - 4 + 3 + 8;
     538                 :            :     }
     539                 :            :   else
     540                 :            :     {
     541                 :            :      /*
     542                 :            :         LEN       VER      TYPE     ADDR     OFFSET   SIGNATURE  TYPE-OFFSET
     543                 :            :       4-bytes + 2-bytes + 1-byte + 1-byte + 4-bytes + 8-bytes + 4-bytes 32-bit
     544                 :            :      12-bytes + 2-bytes + 1-byte + 1-byte + 8-bytes + 8-bytes + 8-bytes 64-bit
     545                 :            :         Both signature and type offset are optional.
     546                 :            : 
     547                 :            :         Note same 4/8 offset size trick as above.
     548                 :            :         We explicitly ignore unknown unit types (see asserts above).  */
     549                 :    7433540 :       off += 3 * offset_size - 4 + 4;
     550                 :    7433540 :       if (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
     551   [ +  +  +  + ]:    7433540 :           || unit_type == DW_UT_type || unit_type == DW_UT_split_type)
     552                 :            :         {
     553                 :       3064 :           off += 8;
     554         [ +  + ]:       3064 :           if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
     555                 :         54 :             off += offset_size;
     556                 :            :         }
     557                 :            :     }
     558                 :            : 
     559                 :    7643286 :   return off;
     560                 :            : }
     561                 :            : 
     562                 :            : static inline Dwarf_Off
     563                 :    7498186 : __libdw_first_die_off_from_cu (struct Dwarf_CU *cu)
     564                 :            : {
     565                 :    7498186 :   return __libdw_first_die_from_cu_start (cu->start,
     566                 :    7498186 :                                           cu->offset_size,
     567                 :    7498186 :                                           cu->version,
     568                 :    7498186 :                                           cu->unit_type);
     569                 :            : }
     570                 :            : 
     571                 :            : #define CUDIE(fromcu)                                                         \
     572                 :            :   ((Dwarf_Die)                                                                \
     573                 :            :    {                                                                          \
     574                 :            :      .cu = (fromcu),                                                          \
     575                 :            :      .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
     576                 :            :               + __libdw_first_die_off_from_cu (fromcu))                       \
     577                 :            :    })
     578                 :            : 
     579                 :            : #define SUBDIE(fromcu)                                                        \
     580                 :            :   ((Dwarf_Die)                                                                \
     581                 :            :    {                                                                          \
     582                 :            :      .cu = (fromcu),                                                          \
     583                 :            :      .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
     584                 :            :               + (fromcu)->start + (fromcu)->subdie_offset)                      \
     585                 :            :    })
     586                 :            : 
     587                 :            : 
     588                 :            : /* Prototype of a single .debug_macro operator.  */
     589                 :            : typedef struct
     590                 :            : {
     591                 :            :   Dwarf_Word nforms;
     592                 :            :   unsigned char const *forms;
     593                 :            : } Dwarf_Macro_Op_Proto;
     594                 :            : 
     595                 :            : /* Prototype table.  */
     596                 :            : typedef struct
     597                 :            : {
     598                 :            :   Dwarf *dbg;
     599                 :            : 
     600                 :            :   /* Offset of .debug_macro section.  */
     601                 :            :   Dwarf_Off offset;
     602                 :            : 
     603                 :            :   /* Offset of associated .debug_line section.  */
     604                 :            :   Dwarf_Off line_offset;
     605                 :            : 
     606                 :            :   /* The source file information.  */
     607                 :            :   Dwarf_Files *files;
     608                 :            : 
     609                 :            :   /* If this macro unit was opened through dwarf_getmacros or
     610                 :            :      dwarf_getmacros_die, this caches value of DW_AT_comp_dir, if
     611                 :            :      present.  */
     612                 :            :   const char *comp_dir;
     613                 :            : 
     614                 :            :   /* Header length.  */
     615                 :            :   Dwarf_Half header_len;
     616                 :            : 
     617                 :            :   uint16_t version;
     618                 :            :   uint8_t address_size;
     619                 :            :   uint8_t offset_size;
     620                 :            :   uint8_t sec_index;    /* IDX_debug_macro or IDX_debug_macinfo.  */
     621                 :            : 
     622                 :            :   /* Shows where in TABLE each opcode is defined.  Since opcode 0 is
     623                 :            :      never used, it stores index of opcode X in X-1'th element.  The
     624                 :            :      value of 0xff means not stored at all.  */
     625                 :            :   unsigned char opcodes[255];
     626                 :            : 
     627                 :            :   /* Individual opcode prototypes.  */
     628                 :            :   Dwarf_Macro_Op_Proto table[];
     629                 :            : } Dwarf_Macro_Op_Table;
     630                 :            : 
     631                 :            : struct Dwarf_Macro_s
     632                 :            : {
     633                 :            :   Dwarf_Macro_Op_Table *table;
     634                 :            :   Dwarf_Attribute *attributes;
     635                 :            :   uint8_t opcode;
     636                 :            : };
     637                 :            : 
     638                 :            : static inline Dwarf_Word
     639                 :       6852 : libdw_macro_nforms (Dwarf_Macro *macro)
     640                 :            : {
     641         [ +  - ]:       6852 :   return macro->table->table[macro->table->opcodes[macro->opcode - 1]].nforms;
     642                 :            : }
     643                 :            : 
     644                 :            : /* Returns true for any allowed FORM in the opcode_operands_table as
     645                 :            :    mentioned in the DWARF5 spec (6.3.1 Macro Information Header).
     646                 :            :    Or those mentioned in DWARF5 spec (6.2.4.2 Vendor-defined Content
     647                 :            :    Descriptions) for the directory/file table (plus DW_FORM_strp_sup).  */
     648                 :            : static inline bool
     649                 :     168778 : libdw_valid_user_form (int form)
     650                 :            : {
     651         [ -  + ]:     168778 :   switch (form)
     652                 :            :     {
     653                 :            :       case DW_FORM_block:
     654                 :            :       case DW_FORM_block1:
     655                 :            :       case DW_FORM_block2:
     656                 :            :       case DW_FORM_block4:
     657                 :            :       case DW_FORM_data1:
     658                 :            :       case DW_FORM_data2:
     659                 :            :       case DW_FORM_data4:
     660                 :            :       case DW_FORM_data8:
     661                 :            :       case DW_FORM_data16:
     662                 :            :       case DW_FORM_flag:
     663                 :            :       case DW_FORM_line_strp:
     664                 :            :       case DW_FORM_sdata:
     665                 :            :       case DW_FORM_sec_offset:
     666                 :            :       case DW_FORM_string:
     667                 :            :       case DW_FORM_strp:
     668                 :            :       case DW_FORM_strp_sup:
     669                 :            :       case DW_FORM_strx:
     670                 :            :       case DW_FORM_strx1:
     671                 :            :       case DW_FORM_strx2:
     672                 :            :       case DW_FORM_strx3:
     673                 :            :       case DW_FORM_strx4:
     674                 :            :       case DW_FORM_udata:
     675                 :            :         return true;
     676                 :          0 :       default:
     677                 :          0 :         return false;
     678                 :            :     }
     679                 :            : }
     680                 :            : 
     681                 :            : 
     682                 :            : /* We have to include the file at this point because the inline
     683                 :            :    functions access internals of the Dwarf structure.  */
     684                 :            : #include "memory-access.h"
     685                 :            : 
     686                 :            : 
     687                 :            : /* Set error value.  */
     688                 :            : extern void __libdw_seterrno (int value) internal_function;
     689                 :            : 
     690                 :            : 
     691                 :            : /* Memory handling, the easy parts.  */
     692                 :            : #define libdw_alloc(dbg, type, tsize, cnt) \
     693                 :            :   ({ struct libdw_memblock *_tail = __libdw_alloc_tail(dbg);                  \
     694                 :            :      size_t _required = (tsize) * (cnt);                                      \
     695                 :            :      type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
     696                 :            :      size_t _padding = ((__alignof (type)                                     \
     697                 :            :                          - ((uintptr_t) _result & (__alignof (type) - 1)))    \
     698                 :            :                         & (__alignof (type) - 1));                        \
     699                 :            :      if (unlikely (_tail->remaining < _required + _padding))                    \
     700                 :            :        _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
     701                 :            :      else                                                                     \
     702                 :            :        {                                                                      \
     703                 :            :          _required += _padding;                                               \
     704                 :            :          _result = (type *) ((char *) _result + _padding);                    \
     705                 :            :          _tail->remaining -= _required;                                            \
     706                 :            :        }                                                                      \
     707                 :            :      _result; })
     708                 :            : 
     709                 :            : #define libdw_typed_alloc(dbg, type) \
     710                 :            :   libdw_alloc (dbg, type, sizeof (type), 1)
     711                 :            : 
     712                 :            : /* Can only be used to undo the last libdw_alloc.  */
     713                 :            : #define libdw_unalloc(dbg, type, tsize, cnt) \
     714                 :            :   ({ struct libdw_memblock *_tail = __libdw_thread_tail (dbg);                \
     715                 :            :      size_t _required = (tsize) * (cnt);                                      \
     716                 :            :      /* We cannot know the padding, it is lost.  */                           \
     717                 :            :      _tail->remaining += _required; })                                             \
     718                 :            : 
     719                 :            : #define libdw_typed_unalloc(dbg, type) \
     720                 :            :   libdw_unalloc (dbg, type, sizeof (type), 1)
     721                 :            : 
     722                 :            : /* Callback to choose a thread-local memory allocation stack.  */
     723                 :            : extern struct libdw_memblock *__libdw_alloc_tail (Dwarf* dbg)
     724                 :            :      __nonnull_attribute__ (1);
     725                 :            : 
     726                 :            : extern struct libdw_memblock *__libdw_thread_tail (Dwarf* dbg)
     727                 :            :      __nonnull_attribute__ (1);
     728                 :            : 
     729                 :            : /* Callback to allocate more.  */
     730                 :            : extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
     731                 :            :      __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
     732                 :            : 
     733                 :            : /* Default OOM handler.  */
     734                 :            : extern void __libdw_oom (void) __attribute ((noreturn)) attribute_hidden;
     735                 :            : 
     736                 :            : /* Read next unit (or v4 debug type) and return next offset.  Doesn't
     737                 :            :    create an actual Dwarf_CU just provides necessary header fields.  */
     738                 :            : extern int
     739                 :            : internal_function
     740                 :            : __libdw_next_unit (Dwarf *dbg, bool v4_debug_types, Dwarf_Off off,
     741                 :            :                    Dwarf_Off *next_off, size_t *header_sizep,
     742                 :            :                    Dwarf_Half *versionp, uint8_t *unit_typep,
     743                 :            :                    Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
     744                 :            :                    uint8_t *offset_sizep, uint64_t *unit_id8p,
     745                 :            :                    Dwarf_Off *subdie_offsetp)
     746                 :            :      __nonnull_attribute__ (4) internal_function;
     747                 :            : 
     748                 :            : /* Allocate the internal data for a unit not seen before.  */
     749                 :            : extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
     750                 :            :      __nonnull_attribute__ (1) internal_function;
     751                 :            : 
     752                 :            : /* Find CU for given offset.  */
     753                 :            : extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
     754                 :            :      __nonnull_attribute__ (1) internal_function;
     755                 :            : 
     756                 :            : /* Find CU for given DIE address.  */
     757                 :            : extern struct Dwarf_CU *__libdw_findcu_addr (Dwarf *dbg, void *addr)
     758                 :            :      __nonnull_attribute__ (1) internal_function;
     759                 :            : 
     760                 :            : /* Find split Dwarf for given DIE address.  */
     761                 :            : extern struct Dwarf *__libdw_find_split_dbg_addr (Dwarf *dbg, void *addr)
     762                 :            :      __nonnull_attribute__ (1) internal_function;
     763                 :            : 
     764                 :            : /* Find the split (or skeleton) unit.  */
     765                 :            : extern struct Dwarf_CU *__libdw_find_split_unit (Dwarf_CU *cu)
     766                 :            :      internal_function;
     767                 :            : 
     768                 :            : /* Find a unit in a DWARF package file for __libdw_intern_next_unit.  */
     769                 :            : extern int __libdw_dwp_find_unit (Dwarf *dbg, bool debug_types, Dwarf_Off off,
     770                 :            :                                   uint16_t version, uint8_t unit_type,
     771                 :            :                                   uint64_t unit_id8, uint32_t *unit_rowp,
     772                 :            :                                   Dwarf_Off *abbrev_offsetp)
     773                 :            :      __nonnull_attribute__ (1, 7, 8) internal_function;
     774                 :            : 
     775                 :            : /* Find the compilation unit in a DWARF package file with the given id.  */
     776                 :            : extern Dwarf_CU *__libdw_dwp_findcu_id (Dwarf *dbg, uint64_t unit_id8)
     777                 :            :      __nonnull_attribute__ (1) internal_function;
     778                 :            : 
     779                 :            : /* Get abbreviation with given code.  */
     780                 :            : extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
     781                 :            :                                          unsigned int code)
     782                 :            :      __nonnull_attribute__ (1) internal_function;
     783                 :            : 
     784                 :            : /* Get abbreviation at given offset.  */
     785                 :            : extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
     786                 :            :                                         Dwarf_Off offset, size_t *lengthp,
     787                 :            :                                         Dwarf_Abbrev *result)
     788                 :            :      __nonnull_attribute__ (1) internal_function;
     789                 :            : 
     790                 :            : /* Get abbreviation of given DIE, and optionally set *READP to the DIE memory
     791                 :            :    just past the abbreviation code.  */
     792                 :            : static inline Dwarf_Abbrev *
     793                 :            : __nonnull_attribute__ (1)
     794                 :  133658310 : __libdw_dieabbrev (Dwarf_Die *die, const unsigned char **readp)
     795                 :            : {
     796                 :            :   /* Do we need to get the abbreviation, or need to read after the code?  */
     797   [ +  +  +  + ]:  133658310 :   if (die->abbrev == NULL || readp != NULL)
     798                 :            :     {
     799                 :            :       /* Get the abbreviation code.  */
     800                 :   72732266 :       unsigned int code;
     801                 :   72732266 :       const unsigned char *addr = die->addr;
     802   [ +  +  -  + ]:   72732266 :       if (unlikely (die->cu == NULL
     803                 :            :                     || addr >= (const unsigned char *) die->cu->endp))
     804                 :      22996 :         return die->abbrev = DWARF_END_ABBREV;
     805                 :   72709270 :       get_uleb128 (code, addr, die->cu->endp);
     806         [ +  + ]:   72709270 :       if (readp != NULL)
     807                 :   47746626 :         *readp = addr;
     808                 :            : 
     809                 :            :       /* Find the abbreviation.  */
     810         [ +  + ]:   72709270 :       if (die->abbrev == NULL)
     811                 :   32450916 :         die->abbrev = __libdw_findabbrev (die->cu, code);
     812                 :            :     }
     813                 :  133635314 :   return die->abbrev;
     814                 :            : }
     815                 :            : 
     816                 :            : /* Helper functions for form handling.  */
     817                 :            : extern size_t __libdw_form_val_compute_len (struct Dwarf_CU *cu,
     818                 :            :                                             unsigned int form,
     819                 :            :                                             const unsigned char *valp)
     820                 :            :      __nonnull_attribute__ (1, 3) internal_function;
     821                 :            : 
     822                 :            : /* Find the length of a form attribute in DIE/info data.  */
     823                 :            : static inline size_t
     824                 :            : __nonnull_attribute__ (1, 3)
     825                 :  188167176 : __libdw_form_val_len (struct Dwarf_CU *cu, unsigned int form,
     826                 :            :                       const unsigned char *valp)
     827                 :            : {
     828                 :            :   /* Small lookup table of forms with fixed lengths.  Absent indexes are
     829                 :            :      initialized 0, so any truly desired 0 is set to 0x80 and masked.  */
     830                 :  188167176 :   static const uint8_t form_lengths[] =
     831                 :            :     {
     832                 :            :       [DW_FORM_flag_present] = 0x80,
     833                 :            :       [DW_FORM_implicit_const] = 0x80, /* Value is in abbrev, not in info.  */
     834                 :            : 
     835                 :            :       [DW_FORM_flag] = 1,
     836                 :            :       [DW_FORM_data1] = 1, [DW_FORM_ref1] = 1,
     837                 :            :       [DW_FORM_addrx1] = 1, [DW_FORM_strx1] = 1,
     838                 :            : 
     839                 :            :       [DW_FORM_data2] = 2, [DW_FORM_ref2] = 2,
     840                 :            :       [DW_FORM_addrx2] = 2, [DW_FORM_strx2] = 2,
     841                 :            : 
     842                 :            :       [DW_FORM_addrx3] = 3, [DW_FORM_strx3] = 3,
     843                 :            : 
     844                 :            :       [DW_FORM_data4] = 4, [DW_FORM_ref4] = 4, [DW_FORM_ref_sup4] = 4,
     845                 :            :       [DW_FORM_addrx4] = 4, [DW_FORM_strx4] = 4,
     846                 :            : 
     847                 :            :       [DW_FORM_ref_sig8] = 8,
     848                 :            :       [DW_FORM_data8] = 8, [DW_FORM_ref8] = 8, [DW_FORM_ref_sup8] = 8,
     849                 :            : 
     850                 :            :       [DW_FORM_data16] = 16,
     851                 :            :     };
     852                 :            : 
     853                 :            :   /* Return immediately for forms with fixed lengths.  */
     854         [ +  + ]:  188167176 :   if (form < sizeof form_lengths / sizeof form_lengths[0])
     855                 :            :     {
     856                 :  188150684 :       uint8_t len = form_lengths[form];
     857         [ +  + ]:  188150684 :       if (len != 0)
     858                 :            :         {
     859                 :  135399032 :           const unsigned char *endp = cu->endp;
     860                 :  135399032 :           len &= 0x7f; /* Mask to allow 0x80 -> 0.  */
     861         [ -  + ]:  135399032 :           if (unlikely (len > (size_t) (endp - valp)))
     862                 :            :             {
     863                 :          0 :               __libdw_seterrno (DWARF_E_INVALID_DWARF);
     864                 :          0 :               return -1;
     865                 :            :             }
     866                 :            :           return len;
     867                 :            :         }
     868                 :            :     }
     869                 :            : 
     870                 :            :   /* Other forms require some computation.  */
     871                 :   52768144 :   return __libdw_form_val_compute_len (cu, form, valp);
     872                 :            : }
     873                 :            : 
     874                 :            : /* Helper function for DW_FORM_ref* handling.  */
     875                 :            : extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
     876                 :            :      __nonnull_attribute__ (1, 2) internal_function;
     877                 :            : 
     878                 :            : 
     879                 :            : /* Helper function to locate attribute.  */
     880                 :            : extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
     881                 :            :                                          unsigned int search_name,
     882                 :            :                                          unsigned int *codep,
     883                 :            :                                          unsigned int *formp)
     884                 :            :      __nonnull_attribute__ (1) internal_function;
     885                 :            : 
     886                 :            : /* Helper function to access integer attribute.  */
     887                 :            : extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
     888                 :            :      __nonnull_attribute__ (1, 2) internal_function;
     889                 :            : 
     890                 :            : /* Helper function to walk scopes.  */
     891                 :            : struct Dwarf_Die_Chain
     892                 :            : {
     893                 :            :   Dwarf_Die die;
     894                 :            :   struct Dwarf_Die_Chain *parent;
     895                 :            :   bool prune;                   /* The PREVISIT function can set this.  */
     896                 :            : };
     897                 :            : extern int __libdw_visit_scopes (unsigned int depth,
     898                 :            :                                  struct Dwarf_Die_Chain *root,
     899                 :            :                                  struct Dwarf_Die_Chain *imports,
     900                 :            :                                  int (*previsit) (unsigned int depth,
     901                 :            :                                                   struct Dwarf_Die_Chain *,
     902                 :            :                                                   void *arg),
     903                 :            :                                  int (*postvisit) (unsigned int depth,
     904                 :            :                                                    struct Dwarf_Die_Chain *,
     905                 :            :                                                    void *arg),
     906                 :            :                                  void *arg)
     907                 :            :   __nonnull_attribute__ (2, 4) internal_function;
     908                 :            : 
     909                 :            : /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
     910                 :            :    and cache the result (via tsearch).  */
     911                 :            : extern int __libdw_intern_expression (Dwarf *dbg,
     912                 :            :                                       bool other_byte_order,
     913                 :            :                                       unsigned int address_size,
     914                 :            :                                       unsigned int ref_size,
     915                 :            :                                       void **cache, const Dwarf_Block *block,
     916                 :            :                                       bool cfap, bool valuep,
     917                 :            :                                       Dwarf_Op **llbuf, size_t *listlen,
     918                 :            :                                       int sec_index)
     919                 :            :   __nonnull_attribute__ (5, 6, 9, 10) internal_function;
     920                 :            : 
     921                 :            : extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
     922                 :            :                                   Dwarf_Die *result, bool debug_types)
     923                 :            :   internal_function;
     924                 :            : 
     925                 :            : 
     926                 :            : /* Return error code of last failing function call.  This value is kept
     927                 :            :    separately for each thread.  */
     928                 :            : extern int __dwarf_errno_internal (void);
     929                 :            : 
     930                 :            : 
     931                 :            : /* Reader hooks.  */
     932                 :            : 
     933                 :            : /* Relocation hooks return -1 on error (in that case the error code
     934                 :            :    must already have been set), 0 if there is no relocation and 1 if a
     935                 :            :    relocation was present.*/
     936                 :            : 
     937                 :            : static inline int
     938                 :            : __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
     939                 :            :                           int sec_index __attribute__ ((unused)),
     940                 :            :                           const void *addr __attribute__ ((unused)),
     941                 :            :                           int width __attribute__ ((unused)),
     942                 :            :                           Dwarf_Addr *val __attribute__ ((unused)))
     943                 :            : {
     944                 :            :   return 0;
     945                 :            : }
     946                 :            : 
     947                 :            : static inline int
     948                 :            : __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
     949                 :            :                          int sec_index __attribute__ ((unused)),
     950                 :            :                          const void *addr __attribute__ ((unused)),
     951                 :            :                          int width __attribute__ ((unused)),
     952                 :            :                          Dwarf_Off *val __attribute__ ((unused)))
     953                 :            : {
     954                 :            :   return 0;
     955                 :            : }
     956                 :            : 
     957                 :            : static inline Elf_Data *
     958                 :    9844792 : __libdw_checked_get_data (Dwarf *dbg, int sec_index)
     959                 :            : {
     960                 :    9844792 :   Elf_Data *data = dbg->sectiondata[sec_index];
     961         [ +  - ]:    9844792 :   if (unlikely (data == NULL)
     962         [ -  + ]:    9844792 :       || unlikely (data->d_buf == NULL))
     963                 :            :     {
     964                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_DWARF);
     965                 :          0 :       return NULL;
     966                 :            :     }
     967                 :            :   return data;
     968                 :            : }
     969                 :            : 
     970                 :            : static inline int
     971                 :    4606868 : __libdw_offset_in_section (Dwarf *dbg, int sec_index,
     972                 :            :                            Dwarf_Off offset, size_t size)
     973                 :            : {
     974                 :    4606868 :   Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
     975         [ -  + ]:    4606868 :   if (data == NULL)
     976                 :            :     return -1;
     977         [ +  - ]:    4606868 :   if (unlikely (offset > data->d_size)
     978         [ +  - ]:    4606868 :       || unlikely (data->d_size < size)
     979         [ -  + ]:    4606868 :       || unlikely (offset > data->d_size - size))
     980                 :            :     {
     981                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
     982                 :          0 :       return -1;
     983                 :            :     }
     984                 :            : 
     985                 :            :   return 0;
     986                 :            : }
     987                 :            : 
     988                 :            : static inline bool
     989                 :    5187656 : __libdw_in_section (Dwarf *dbg, int sec_index,
     990                 :            :                     const void *addr, size_t size)
     991                 :            : {
     992                 :    5187656 :   Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
     993         [ -  + ]:    5187656 :   if (data == NULL)
     994                 :            :     return false;
     995         [ +  - ]:    5187656 :   if (unlikely (addr < data->d_buf)
     996         [ +  - ]:    5187656 :       || unlikely (data->d_size < size)
     997         [ -  + ]:    5187656 :       || unlikely ((size_t)(addr - data->d_buf) > data->d_size - size))
     998                 :            :     {
     999                 :          0 :       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
    1000                 :          0 :       return false;
    1001                 :            :     }
    1002                 :            : 
    1003                 :            :   return true;
    1004                 :            : }
    1005                 :            : 
    1006                 :            : #define READ_AND_RELOCATE(RELOC_HOOK, VAL)                              \
    1007                 :            :   ({                                                                    \
    1008                 :            :     if (!__libdw_in_section (dbg, sec_index, addr, width))              \
    1009                 :            :       return -1;                                                        \
    1010                 :            :                                                                         \
    1011                 :            :     const unsigned char *orig_addr = addr;                              \
    1012                 :            :     if (width == 4)                                                     \
    1013                 :            :       VAL = read_4ubyte_unaligned_inc (dbg, addr);                      \
    1014                 :            :     else                                                                \
    1015                 :            :       VAL = read_8ubyte_unaligned_inc (dbg, addr);                      \
    1016                 :            :                                                                         \
    1017                 :            :     int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL);       \
    1018                 :            :     if (status < 0)                                                  \
    1019                 :            :       return status;                                                    \
    1020                 :            :     status > 0;                                                              \
    1021                 :            :    })
    1022                 :            : 
    1023                 :            : static inline int
    1024                 :     228278 : __libdw_read_address_inc (Dwarf *dbg,
    1025                 :            :                           int sec_index, const unsigned char **addrp,
    1026                 :            :                           int width, Dwarf_Addr *ret)
    1027                 :            : {
    1028                 :     228278 :   const unsigned char *addr = *addrp;
    1029   [ +  -  +  +  :     228278 :   READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
             +  +  +  + ]
    1030                 :     228278 :   *addrp = addr;
    1031                 :     228278 :   return 0;
    1032                 :            : }
    1033                 :            : 
    1034                 :            : static inline int
    1035                 :     402006 : __libdw_read_address (Dwarf *dbg,
    1036                 :            :                       int sec_index, const unsigned char *addr,
    1037                 :            :                       int width, Dwarf_Addr *ret)
    1038                 :            : {
    1039   [ +  -  +  +  :     402006 :   READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
             +  +  +  + ]
    1040                 :            :   return 0;
    1041                 :            : }
    1042                 :            : 
    1043                 :            : static inline int
    1044                 :     152582 : __libdw_read_offset_inc (Dwarf *dbg,
    1045                 :            :                          int sec_index, const unsigned char **addrp,
    1046                 :            :                          int width, Dwarf_Off *ret, int sec_ret,
    1047                 :            :                          size_t size)
    1048                 :            : {
    1049                 :     152582 :   const unsigned char *addr = *addrp;
    1050   [ +  -  +  -  :     152582 :   READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
             +  +  -  - ]
    1051                 :     152582 :   *addrp = addr;
    1052                 :     152582 :   return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
    1053                 :            : }
    1054                 :            : 
    1055                 :            : static inline int
    1056                 :    4403814 : __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
    1057                 :            :                      int sec_index, const unsigned char *addr,
    1058                 :            :                      int width, Dwarf_Off *ret, int sec_ret,
    1059                 :            :                      size_t size)
    1060                 :            : {
    1061   [ +  -  +  -  :    4403814 :   READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
             +  +  -  - ]
    1062                 :    4403814 :   return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
    1063                 :            : }
    1064                 :            : 
    1065                 :            : static inline size_t
    1066                 :   12104662 : cu_sec_idx (struct Dwarf_CU *cu)
    1067                 :            : {
    1068         [ +  + ]:   10310120 :   return cu->sec_idx;
    1069                 :            : }
    1070                 :            : 
    1071                 :            : static inline bool
    1072                 :    6034468 : is_cudie (Dwarf_Die *cudie)
    1073                 :            : {
    1074   [ +  +  +  + ]:    6034468 :   return cudie->cu != NULL && CUDIE (cudie->cu).addr == cudie->addr;
    1075                 :            : }
    1076                 :            : 
    1077                 :            : /* Read up begin/end pair and increment read pointer.
    1078                 :            :     - If it's normal range record, set up *BEGINP and *ENDP and return 0.
    1079                 :            :     - If it's base address selection record, set up *BASEP and return 1.
    1080                 :            :     - If it's end of rangelist, don't set anything and return 2
    1081                 :            :     - If an error occurs, don't set anything and return <0.  */
    1082                 :            : int __libdw_read_begin_end_pair_inc (Dwarf_CU *cu, int sec_index,
    1083                 :            :                                      const unsigned char **readp,
    1084                 :            :                                      const unsigned char *readend,
    1085                 :            :                                      int width,
    1086                 :            :                                      Dwarf_Addr *beginp, Dwarf_Addr *endp,
    1087                 :            :                                      Dwarf_Addr *basep)
    1088                 :            :   internal_function;
    1089                 :            : 
    1090                 :            : const unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
    1091                 :            :                                        int err_nodata,
    1092                 :            :                                        const unsigned char **endpp,
    1093                 :            :                                        Dwarf_Off *offsetp)
    1094                 :            :   internal_function;
    1095                 :            : 
    1096                 :            : /* Fills in the given attribute to point at an empty location expression.  */
    1097                 :            : void __libdw_empty_loc_attr (Dwarf_Attribute *attr)
    1098                 :            :   internal_function;
    1099                 :            : 
    1100                 :            : /* Load .debug_line unit at DEBUG_LINE_OFFSET.  COMP_DIR is a value of
    1101                 :            :    DW_AT_comp_dir or NULL if that attribute is not available.  Caches
    1102                 :            :    the loaded unit and optionally set *LINESP and/or *FILESP (if not
    1103                 :            :    NULL) with loaded information.  Returns 0 for success or a negative
    1104                 :            :    value for failure.  */
    1105                 :            : int __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
    1106                 :            :                          const char *comp_dir, unsigned address_size,
    1107                 :            :                          Dwarf_Lines **linesp, Dwarf_Files **filesp)
    1108                 :            :   internal_function
    1109                 :            :   __nonnull_attribute__ (1);
    1110                 :            : 
    1111                 :            : /* Load and return value of DW_AT_comp_dir from CUDIE.  */
    1112                 :            : const char *__libdw_getcompdir (Dwarf_Die *cudie);
    1113                 :            : 
    1114                 :            : /* Get the base address for the CU, fetches it when not yet set.
    1115                 :            :    This is used as initial base address for ranges and loclists.  */
    1116                 :            : Dwarf_Addr __libdw_cu_base_address (Dwarf_CU *cu);
    1117                 :            : 
    1118                 :            : /* Get the address base for the CU, fetches it when not yet set.  */
    1119                 :            : static inline Dwarf_Off
    1120                 :       1932 : __libdw_cu_addr_base (Dwarf_CU *cu)
    1121                 :            : {
    1122         [ +  + ]:       1932 :   if (cu->addr_base == (Dwarf_Off) -1)
    1123                 :            :     {
    1124                 :        194 :       Dwarf_Die cu_die = CUDIE(cu);
    1125                 :        194 :       Dwarf_Attribute attr;
    1126                 :        194 :       Dwarf_Off offset = 0;
    1127         [ +  + ]:        194 :       if (dwarf_attr (&cu_die, DW_AT_GNU_addr_base, &attr) != NULL
    1128         [ +  + ]:         96 :           || dwarf_attr (&cu_die, DW_AT_addr_base, &attr) != NULL)
    1129                 :            :         {
    1130                 :        186 :           Dwarf_Word off;
    1131         [ +  - ]:        186 :           if (dwarf_formudata (&attr, &off) == 0)
    1132                 :        186 :             offset = off;
    1133                 :            :         }
    1134                 :        194 :       cu->addr_base = offset;
    1135                 :            :     }
    1136                 :            : 
    1137                 :       1932 :   return cu->addr_base;
    1138                 :            : }
    1139                 :            : 
    1140                 :            : /* Gets the .debug_str_offsets base offset to use.  static inline to
    1141                 :            :    be shared between libdw and eu-readelf.  */
    1142                 :            : static inline Dwarf_Off
    1143                 :      13774 : str_offsets_base_off (Dwarf *dbg, Dwarf_CU *cu)
    1144                 :            : {
    1145                 :            :   /* If we don't have a CU, then find and use the first one in the
    1146                 :            :      debug file (when we support .dwp files, we must actually find the
    1147                 :            :      one matching our "caller" - aka macro or line).  If we (now) have
    1148                 :            :      a cu and str_offsets_base attribute, just use that.  Otherwise
    1149                 :            :      use the first offset.  But we might have to parse the header
    1150                 :            :      first, but only if this is version 5.  Assume if all else fails,
    1151                 :            :      this is version 4, without header.  */
    1152                 :            : 
    1153         [ +  + ]:      13774 :   if (cu == NULL && dbg != NULL)
    1154                 :            :     {
    1155                 :       3772 :       Dwarf_CU *first_cu;
    1156         [ +  + ]:       3772 :       if (dwarf_get_units (dbg, NULL, &first_cu, NULL, NULL, NULL, NULL) == 0)
    1157                 :       3770 :         cu = first_cu;
    1158                 :            :     }
    1159                 :            : 
    1160                 :      13774 :   Dwarf_Off off = 0;
    1161         [ +  + ]:      13774 :   if (cu != NULL)
    1162                 :            :     {
    1163         [ +  + ]:      13772 :       if (cu->str_off_base == (Dwarf_Off) -1)
    1164                 :            :         {
    1165                 :        294 :           Dwarf_Off dwp_offset;
    1166         [ +  - ]:        294 :           if (dwarf_cu_dwp_section_info (cu, DW_SECT_STR_OFFSETS, &dwp_offset,
    1167                 :            :                                          NULL) == 0)
    1168                 :        294 :             off = dwp_offset;
    1169                 :        294 :           Dwarf_Die cu_die = CUDIE(cu);
    1170                 :        294 :           Dwarf_Attribute attr;
    1171         [ +  + ]:        294 :           if (dwarf_attr (&cu_die, DW_AT_str_offsets_base, &attr) != NULL)
    1172                 :            :             {
    1173                 :          6 :               Dwarf_Word base;
    1174         [ +  - ]:          6 :               if (dwarf_formudata (&attr, &base) == 0)
    1175                 :            :                 {
    1176                 :          6 :                   cu->str_off_base = off + base;
    1177                 :          6 :                   return cu->str_off_base;
    1178                 :            :                 }
    1179                 :            :             }
    1180                 :            :           /* For older DWARF simply assume zero (no header).  */
    1181         [ +  + ]:        288 :           if (cu->version < 5)
    1182                 :            :             {
    1183                 :        176 :               cu->str_off_base = off;
    1184                 :        176 :               return cu->str_off_base;
    1185                 :            :             }
    1186                 :            : 
    1187         [ +  + ]:        112 :           if (dbg == NULL)
    1188                 :         78 :             dbg = cu->dbg;
    1189                 :            :         }
    1190                 :            :       else
    1191                 :            :         return cu->str_off_base;
    1192                 :            :     }
    1193                 :            : 
    1194                 :            :   /* No str_offsets_base attribute, we have to assume "zero".
    1195                 :            :      But there could be a header first.  */
    1196         [ -  + ]:        114 :   if (dbg == NULL)
    1197                 :          0 :     goto no_header;
    1198                 :            : 
    1199                 :        114 :   Elf_Data *data =  dbg->sectiondata[IDX_debug_str_offsets];
    1200         [ +  + ]:        114 :   if (data == NULL)
    1201                 :         36 :     goto no_header;
    1202                 :            : 
    1203                 :         78 :   const unsigned char *start;
    1204                 :         78 :   const unsigned char *readp;
    1205                 :         78 :   const unsigned char *readendp;
    1206                 :         78 :   start = readp = (const unsigned char *) data->d_buf;
    1207                 :         78 :   readendp = (const unsigned char *) data->d_buf + data->d_size;
    1208                 :            : 
    1209                 :         78 :   uint64_t unit_length;
    1210                 :         78 :   uint16_t version;
    1211                 :            : 
    1212         [ -  + ]:         78 :   unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    1213         [ -  + ]:         78 :   if (unlikely (unit_length == 0xffffffff))
    1214                 :            :     {
    1215         [ #  # ]:          0 :       if (unlikely (readendp - readp < 8))
    1216                 :          0 :         goto no_header;
    1217         [ #  # ]:          0 :       unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    1218                 :            :       /* In theory the offset size could be different
    1219                 :            :          between CU and str_offsets unit.  But we just
    1220                 :            :          ignore that here. */
    1221                 :            :     }
    1222                 :            : 
    1223                 :            :   /* We need at least 2-bytes (version) + 2-bytes (padding) =
    1224                 :            :      4 bytes to complete the header.  And this unit cannot go
    1225                 :            :      beyond the section data.  */
    1226         [ +  - ]:         78 :   if (readendp - readp < 4
    1227         [ +  - ]:         78 :       || unit_length < 4
    1228         [ -  + ]:         78 :       || (uint64_t) (readendp - readp) < unit_length)
    1229                 :          0 :     goto no_header;
    1230                 :            : 
    1231         [ -  + ]:         78 :   version = read_2ubyte_unaligned_inc (dbg, readp);
    1232         [ -  + ]:         78 :   if (version != 5)
    1233                 :          0 :     goto no_header;
    1234                 :            :   /* padding */
    1235                 :         78 :   read_2ubyte_unaligned_inc (dbg, readp);
    1236                 :            : 
    1237                 :         78 :   off += (Dwarf_Off) (readp - start);
    1238                 :            : 
    1239                 :        114 :  no_header:
    1240         [ +  + ]:        114 :   if (cu != NULL)
    1241                 :        112 :     cu->str_off_base = off;
    1242                 :            : 
    1243                 :            :   return off;
    1244                 :            : }
    1245                 :            : 
    1246                 :            : 
    1247                 :            : /* Get the string offsets base for the CU, fetches it when not yet set.  */
    1248                 :       3342 : static inline Dwarf_Off __libdw_cu_str_off_base (Dwarf_CU *cu)
    1249                 :            : {
    1250                 :       3342 :   return str_offsets_base_off (NULL, cu);
    1251                 :            : }
    1252                 :            : 
    1253                 :            : 
    1254                 :            : /* Either a direct offset into .debug_ranges for version < 5, or the
    1255                 :            :    start of the offset table in .debug_rnglists for version > 5.  */
    1256                 :            : static inline Dwarf_Off
    1257                 :         90 : __libdw_cu_ranges_base (Dwarf_CU *cu)
    1258                 :            : {
    1259         [ +  + ]:         90 :   if (cu->ranges_base == (Dwarf_Off) -1)
    1260                 :            :     {
    1261                 :         18 :       Dwarf_Off offset = 0;
    1262                 :         18 :       Dwarf_Die cu_die = CUDIE(cu);
    1263                 :         18 :       Dwarf_Attribute attr;
    1264         [ +  + ]:         18 :       if (cu->version < 5)
    1265                 :            :         {
    1266         [ +  - ]:         12 :           if (dwarf_attr (&cu_die, DW_AT_GNU_ranges_base, &attr) != NULL)
    1267                 :            :             {
    1268                 :         12 :               Dwarf_Word off;
    1269         [ +  - ]:         12 :               if (dwarf_formudata (&attr, &off) == 0)
    1270                 :         12 :                 offset = off;
    1271                 :            :             }
    1272                 :            :         }
    1273                 :            :       else
    1274                 :            :         {
    1275                 :          6 :           Dwarf_Off dwp_offset;
    1276         [ +  - ]:          6 :           if (dwarf_cu_dwp_section_info (cu, DW_SECT_RNGLISTS, &dwp_offset,
    1277                 :            :                                          NULL) == 0)
    1278                 :          6 :             offset = dwp_offset;
    1279                 :            : 
    1280         [ +  + ]:          6 :           if (dwarf_attr (&cu_die, DW_AT_rnglists_base, &attr) != NULL)
    1281                 :            :             {
    1282                 :          2 :               Dwarf_Word off;
    1283         [ +  - ]:          2 :               if (dwarf_formudata (&attr, &off) == 0)
    1284                 :          2 :                 offset += off;
    1285                 :            :             }
    1286                 :            : 
    1287                 :            :           /* There wasn't an rnglists_base, if the Dwarf does have a
    1288                 :            :              .debug_rnglists section, then it might be we need the
    1289                 :            :              base after the first header. */
    1290                 :          6 :           Elf_Data *data = cu->dbg->sectiondata[IDX_debug_rnglists];
    1291   [ +  +  +  - ]:          6 :           if (offset == dwp_offset && data != NULL)
    1292                 :            :             {
    1293                 :          4 :               Dwarf *dbg = cu->dbg;
    1294                 :          4 :               const unsigned char *readp = data->d_buf;
    1295                 :          4 :               const unsigned char *const dataend
    1296                 :          4 :                 = (unsigned char *) data->d_buf + data->d_size;
    1297                 :            : 
    1298         [ -  + ]:          4 :               uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    1299                 :          4 :               unsigned int offset_size = 4;
    1300         [ -  + ]:          4 :               if (unlikely (unit_length == 0xffffffff))
    1301                 :            :                 {
    1302         [ #  # ]:          0 :                   if (unlikely (readp > dataend - 8))
    1303                 :          0 :                     goto no_header;
    1304                 :            : 
    1305         [ #  # ]:          0 :                   unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    1306                 :          0 :                   offset_size = 8;
    1307                 :            :                 }
    1308                 :            : 
    1309         [ +  - ]:          4 :               if (readp > dataend - 8
    1310         [ +  - ]:          4 :                   || unit_length < 8
    1311         [ -  + ]:          4 :                   || unit_length > (uint64_t) (dataend - readp))
    1312                 :          0 :                 goto no_header;
    1313                 :            : 
    1314         [ -  + ]:          4 :               uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    1315         [ -  + ]:          4 :               if (version != 5)
    1316                 :          0 :                 goto no_header;
    1317                 :            : 
    1318                 :          4 :               uint8_t address_size = *readp++;
    1319         [ -  + ]:          4 :               if (address_size != 4 && address_size != 8)
    1320                 :          0 :                 goto no_header;
    1321                 :            : 
    1322                 :          4 :               uint8_t segment_size = *readp++;
    1323         [ -  + ]:          4 :               if (segment_size != 0)
    1324                 :          0 :                 goto no_header;
    1325                 :            : 
    1326                 :          4 :               uint32_t offset_entry_count;
    1327         [ -  + ]:          4 :               offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    1328                 :            : 
    1329                 :          4 :               const unsigned char *offset_array_start = readp;
    1330         [ -  + ]:          4 :               if (offset_entry_count <= 0)
    1331                 :          0 :                 goto no_header;
    1332                 :            : 
    1333                 :          4 :               uint64_t needed = offset_entry_count * offset_size;
    1334         [ -  + ]:          4 :               if (unit_length - 8 < needed)
    1335                 :          0 :                 goto no_header;
    1336                 :            : 
    1337                 :          4 :               offset += (Dwarf_Off) (offset_array_start
    1338                 :          4 :                                      - (unsigned char *) data->d_buf);
    1339                 :            :             }
    1340                 :            :         }
    1341                 :         18 :     no_header:
    1342                 :         18 :       cu->ranges_base = offset;
    1343                 :            :     }
    1344                 :            : 
    1345                 :         90 :   return cu->ranges_base;
    1346                 :            : }
    1347                 :            : 
    1348                 :            : 
    1349                 :            : /* The start of the offset table in .debug_loclists for DWARF5.  */
    1350                 :            : static inline Dwarf_Off
    1351                 :        218 : __libdw_cu_locs_base (Dwarf_CU *cu)
    1352                 :            : {
    1353         [ +  + ]:        218 :   if (cu->locs_base == (Dwarf_Off) -1)
    1354                 :            :     {
    1355                 :          8 :       Dwarf_Off offset = 0;
    1356                 :          8 :       Dwarf_Off dwp_offset;
    1357         [ +  - ]:          8 :       if (dwarf_cu_dwp_section_info (cu, DW_SECT_LOCLISTS, &dwp_offset, NULL)
    1358                 :            :           == 0)
    1359                 :          8 :         offset = dwp_offset;
    1360                 :            : 
    1361                 :          8 :       Dwarf_Die cu_die = CUDIE(cu);
    1362                 :          8 :       Dwarf_Attribute attr;
    1363         [ +  + ]:          8 :       if (dwarf_attr (&cu_die, DW_AT_loclists_base, &attr) != NULL)
    1364                 :            :         {
    1365                 :          4 :           Dwarf_Word off;
    1366         [ +  - ]:          4 :           if (dwarf_formudata (&attr, &off) == 0)
    1367                 :          4 :             offset += off;
    1368                 :            :         }
    1369                 :            : 
    1370                 :            :       /* There wasn't an loclists_base, if the Dwarf does have a
    1371                 :            :          .debug_loclists section, then it might be we need the
    1372                 :            :          base after the first header. */
    1373                 :          8 :       Elf_Data *data = cu->dbg->sectiondata[IDX_debug_loclists];
    1374   [ -  +  -  + ]:          8 :       if (offset == dwp_offset && data != NULL)
    1375                 :            :         {
    1376                 :          8 :           Dwarf *dbg = cu->dbg;
    1377                 :          8 :           const unsigned char *readp = data->d_buf;
    1378                 :          8 :           const unsigned char *const dataend
    1379                 :          8 :             = (unsigned char *) data->d_buf + data->d_size;
    1380                 :            : 
    1381         [ -  + ]:          8 :           uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
    1382                 :          8 :           unsigned int offset_size = 4;
    1383         [ -  + ]:          8 :           if (unlikely (unit_length == 0xffffffff))
    1384                 :            :             {
    1385         [ #  # ]:          0 :               if (unlikely (readp > dataend - 8))
    1386                 :          0 :                 goto no_header;
    1387                 :            : 
    1388         [ #  # ]:          0 :               unit_length = read_8ubyte_unaligned_inc (dbg, readp);
    1389                 :          0 :               offset_size = 8;
    1390                 :            :             }
    1391                 :            : 
    1392         [ +  - ]:          8 :           if (readp > dataend - 8
    1393         [ +  - ]:          8 :               || unit_length < 8
    1394         [ -  + ]:          8 :               || unit_length > (uint64_t) (dataend - readp))
    1395                 :          0 :             goto no_header;
    1396                 :            : 
    1397         [ -  + ]:          8 :           uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
    1398         [ -  + ]:          8 :           if (version != 5)
    1399                 :          0 :             goto no_header;
    1400                 :            : 
    1401                 :          8 :           uint8_t address_size = *readp++;
    1402         [ -  + ]:          8 :           if (address_size != 4 && address_size != 8)
    1403                 :          0 :             goto no_header;
    1404                 :            : 
    1405                 :          8 :           uint8_t segment_size = *readp++;
    1406         [ -  + ]:          8 :           if (segment_size != 0)
    1407                 :          0 :             goto no_header;
    1408                 :            : 
    1409                 :          8 :           uint32_t offset_entry_count;
    1410         [ -  + ]:          8 :           offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
    1411                 :            : 
    1412                 :          8 :           const unsigned char *offset_array_start = readp;
    1413         [ -  + ]:          8 :           if (offset_entry_count <= 0)
    1414                 :          0 :             goto no_header;
    1415                 :            : 
    1416                 :          8 :           uint64_t needed = offset_entry_count * offset_size;
    1417         [ -  + ]:          8 :           if (unit_length - 8 < needed)
    1418                 :          0 :             goto no_header;
    1419                 :            : 
    1420                 :          8 :           offset += (Dwarf_Off) (offset_array_start
    1421                 :          8 :                                  - (unsigned char *) data->d_buf);
    1422                 :            :         }
    1423                 :            : 
    1424                 :          0 :     no_header:
    1425                 :          8 :       cu->locs_base = offset;
    1426                 :            :     }
    1427                 :            : 
    1428                 :        218 :   return cu->locs_base;
    1429                 :            : }
    1430                 :            : 
    1431                 :            : /* Helper function for tsearch/tfind split_tree Dwarf.  */
    1432                 :            : int __libdw_finddbg_cb (const void *arg1, const void *arg2);
    1433                 :            : 
    1434                 :            : /* Link skeleton and split compile units.  */
    1435                 :            : static inline void
    1436                 :        190 : __libdw_link_skel_split (Dwarf_CU *skel, Dwarf_CU *split)
    1437                 :            : {
    1438                 :        190 :   skel->split = split;
    1439                 :        190 :   split->split = skel;
    1440                 :            : 
    1441                 :            :   /* Get .debug_addr and addr_base greedy.
    1442                 :            :      We also need it for the fake addr cu.
    1443                 :            :      This needs to be done for each split unit (one per .dwo file, or multiple
    1444                 :            :      per .dwp file).  */
    1445                 :        190 :   Dwarf *dbg = skel->dbg;
    1446                 :        190 :   Dwarf *sdbg = split->dbg;
    1447         [ +  - ]:        190 :   if (dbg->sectiondata[IDX_debug_addr] != NULL
    1448                 :            :       /* If this split file hasn't been linked yet...  */
    1449         [ +  + ]:        190 :       && (sdbg->sectiondata[IDX_debug_addr] == NULL
    1450                 :            :           /* ... or it was linked to the same skeleton file for another
    1451                 :            :              unit...  */
    1452         [ +  - ]:         60 :           || (sdbg->sectiondata[IDX_debug_addr]
    1453                 :            :               == dbg->sectiondata[IDX_debug_addr])))
    1454                 :            :     {
    1455                 :            :       /* ... then link the address information for this file and unit.  */
    1456                 :        190 :       sdbg->sectiondata[IDX_debug_addr]
    1457                 :        190 :         = dbg->sectiondata[IDX_debug_addr];
    1458                 :        190 :       split->addr_base = __libdw_cu_addr_base (skel);
    1459                 :        190 :       sdbg->fake_addr_cu = dbg->fake_addr_cu;
    1460                 :            :     }
    1461                 :        190 : }
    1462                 :            : 
    1463                 :            : 
    1464                 :            : /* Given an address index for a CU return the address.
    1465                 :            :    Returns -1 and sets libdw_errno if an error occurs.  */
    1466                 :            : int __libdw_addrx (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr);
    1467                 :            : 
    1468                 :            : 
    1469                 :            : /* Helper function to set elfpath field in Dwarf, used from dwarf_begin_elf
    1470                 :            :    and libdwfl process_file.  */
    1471                 :            : char * __libdw_elfpath (int fd);
    1472                 :            : 
    1473                 :            : /* Helper function to set debugdir field in Dwarf after elfpath field has been
    1474                 :            :    set.  */
    1475                 :            : void __libdw_set_debugdir (Dwarf *dbg);
    1476                 :            : 
    1477                 :            : 
    1478                 :            : /* Given the directory of a debug file, an absolute or relative dir
    1479                 :            :    to look in, and file returns a full path.
    1480                 :            : 
    1481                 :            :    If the file is absolute (starts with a /) a copy of file is returned.
    1482                 :            :    the file isn't absolute, but dir is absolute, then a path that is
    1483                 :            :    the concatenation of dir and file is returned.  If neither file,
    1484                 :            :    nor dir is absolute, the path will be constructed using dir (if not
    1485                 :            :    NULL) and file relative to the debugdir (if valid).
    1486                 :            : 
    1487                 :            :    The debugdir and the dir may be NULL (in which case they aren't used).
    1488                 :            :    If file is NULL, or no full path can be constructed NULL is returned.
    1489                 :            : 
    1490                 :            :    The caller is responsible for freeing the result if not NULL.  */
    1491                 :            : char * __libdw_filepath (const char *debugdir, const char *dir,
    1492                 :            :                          const char *file)
    1493                 :            :   internal_function;
    1494                 :            : 
    1495                 :            : /* Like dwarf_getaranges but aranges are generated from CU address
    1496                 :            :    ranges instead of being read from .debug_aranges.
    1497                 :            : 
    1498                 :            :    Returns 0 if successful and updates ARANGES and NARANGES.  Otherwise
    1499                 :            :    returns -1 and sets libdw_errno.
    1500                 :            : */
    1501                 :            : int __libdw_getdieranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges);
    1502                 :            : #endif  /* libdwP.h */

Generated by: LCOV version 1.16