LCOV - code coverage report
Current view: top level - libdw - libdwP.h (source / functions) Hit Total Coverage
Test: elfutils-0.190 Lines: 203 265 76.6 %
Date: 2024-02-29 22:46:41 Functions: 18 18 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 127 222 57.2 %

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

Generated by: LCOV version 1.16