Home | History | Annotate | Line # | Download | only in dwarf2
line-header.h revision 1.1.1.3
      1      1.1  christos /* DWARF 2 debugging format support for GDB.
      2      1.1  christos 
      3  1.1.1.3  christos    Copyright (C) 1994-2024 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #ifndef DWARF2_LINE_HEADER_H
     21      1.1  christos #define DWARF2_LINE_HEADER_H
     22      1.1  christos 
     23      1.1  christos /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
     24      1.1  christos    later.  */
     25      1.1  christos typedef int dir_index;
     26      1.1  christos 
     27      1.1  christos /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
     28      1.1  christos    and later.  */
     29      1.1  christos typedef int file_name_index;
     30      1.1  christos 
     31      1.1  christos struct line_header;
     32      1.1  christos 
     33      1.1  christos struct file_entry
     34      1.1  christos {
     35      1.1  christos   file_entry () = default;
     36      1.1  christos 
     37  1.1.1.2  christos   file_entry (const char *name_, file_name_index index_, dir_index d_index_,
     38      1.1  christos 	      unsigned int mod_time_, unsigned int length_)
     39      1.1  christos     : name (name_),
     40  1.1.1.2  christos       index (index_),
     41      1.1  christos       d_index (d_index_),
     42      1.1  christos       mod_time (mod_time_),
     43      1.1  christos       length (length_)
     44      1.1  christos   {}
     45      1.1  christos 
     46      1.1  christos   /* Return the include directory at D_INDEX stored in LH.  Returns
     47      1.1  christos      NULL if D_INDEX is out of bounds.  */
     48      1.1  christos   const char *include_dir (const line_header *lh) const;
     49      1.1  christos 
     50      1.1  christos   /* The file name.  Note this is an observing pointer.  The memory is
     51      1.1  christos      owned by debug_line_buffer.  */
     52      1.1  christos   const char *name {};
     53      1.1  christos 
     54  1.1.1.2  christos   /* The index of this file in the file table.  */
     55  1.1.1.2  christos   file_name_index index {};
     56  1.1.1.2  christos 
     57      1.1  christos   /* The directory index (1-based).  */
     58      1.1  christos   dir_index d_index {};
     59      1.1  christos 
     60      1.1  christos   unsigned int mod_time {};
     61      1.1  christos 
     62      1.1  christos   unsigned int length {};
     63      1.1  christos 
     64      1.1  christos   /* The associated symbol table, if any.  */
     65      1.1  christos   struct symtab *symtab {};
     66      1.1  christos };
     67      1.1  christos 
     68      1.1  christos /* The line number information for a compilation unit (found in the
     69      1.1  christos    .debug_line section) begins with a "statement program header",
     70      1.1  christos    which contains the following information.  */
     71      1.1  christos struct line_header
     72      1.1  christos {
     73  1.1.1.2  christos   /* COMP_DIR is the value of the DW_AT_comp_dir attribute of the compilation
     74  1.1.1.2  christos      unit in the context of which we are reading this line header, or nullptr
     75  1.1.1.2  christos      if unknown or not applicable.  */
     76  1.1.1.2  christos   explicit line_header (const char *comp_dir)
     77  1.1.1.2  christos     : offset_in_dwz {}, m_comp_dir (comp_dir)
     78  1.1.1.2  christos   {}
     79  1.1.1.2  christos 
     80  1.1.1.3  christos   /* This constructor should only be used to create line_header instances to do
     81  1.1.1.2  christos      hash table lookups.  */
     82  1.1.1.2  christos   line_header (sect_offset sect_off, bool offset_in_dwz)
     83  1.1.1.2  christos     : sect_off (sect_off),
     84  1.1.1.2  christos       offset_in_dwz (offset_in_dwz)
     85      1.1  christos   {}
     86      1.1  christos 
     87      1.1  christos   /* Add an entry to the include directory table.  */
     88      1.1  christos   void add_include_dir (const char *include_dir);
     89      1.1  christos 
     90      1.1  christos   /* Add an entry to the file name table.  */
     91      1.1  christos   void add_file_name (const char *name, dir_index d_index,
     92      1.1  christos 		      unsigned int mod_time, unsigned int length);
     93      1.1  christos 
     94      1.1  christos   /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
     95      1.1  christos      Returns NULL if INDEX is out of bounds.  */
     96      1.1  christos   const char *include_dir_at (dir_index index) const
     97      1.1  christos   {
     98      1.1  christos     int vec_index;
     99      1.1  christos     if (version >= 5)
    100      1.1  christos       vec_index = index;
    101      1.1  christos     else
    102      1.1  christos       vec_index = index - 1;
    103      1.1  christos     if (vec_index < 0 || vec_index >= m_include_dirs.size ())
    104      1.1  christos       return NULL;
    105      1.1  christos     return m_include_dirs[vec_index];
    106      1.1  christos   }
    107      1.1  christos 
    108      1.1  christos   bool is_valid_file_index (int file_index) const
    109      1.1  christos   {
    110      1.1  christos     if (version >= 5)
    111      1.1  christos       return 0 <= file_index && file_index < file_names_size ();
    112      1.1  christos     return 1 <= file_index && file_index <= file_names_size ();
    113      1.1  christos   }
    114      1.1  christos 
    115      1.1  christos   /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
    116      1.1  christos      Returns NULL if INDEX is out of bounds.  */
    117      1.1  christos   file_entry *file_name_at (file_name_index index)
    118      1.1  christos   {
    119      1.1  christos     int vec_index;
    120      1.1  christos     if (version >= 5)
    121      1.1  christos       vec_index = index;
    122      1.1  christos     else
    123      1.1  christos       vec_index = index - 1;
    124      1.1  christos     if (vec_index < 0 || vec_index >= m_file_names.size ())
    125      1.1  christos       return NULL;
    126      1.1  christos     return &m_file_names[vec_index];
    127      1.1  christos   }
    128      1.1  christos 
    129      1.1  christos   /* A const overload of the same.  */
    130      1.1  christos   const file_entry *file_name_at (file_name_index index) const
    131      1.1  christos   {
    132      1.1  christos     line_header *lh = const_cast<line_header *> (this);
    133      1.1  christos     return lh->file_name_at (index);
    134      1.1  christos   }
    135      1.1  christos 
    136      1.1  christos   /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
    137      1.1  christos      this method should only be used to iterate through all file entries in an
    138      1.1  christos      index-agnostic manner.  */
    139      1.1  christos   std::vector<file_entry> &file_names ()
    140      1.1  christos   { return m_file_names; }
    141      1.1  christos   /* A const overload of the same.  */
    142      1.1  christos   const std::vector<file_entry> &file_names () const
    143      1.1  christos   { return m_file_names; }
    144      1.1  christos 
    145      1.1  christos   /* Offset of line number information in .debug_line section.  */
    146      1.1  christos   sect_offset sect_off {};
    147      1.1  christos 
    148      1.1  christos   /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile.  */
    149      1.1  christos   unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class.  */
    150      1.1  christos 
    151      1.1  christos   unsigned short version {};
    152      1.1  christos   unsigned char minimum_instruction_length {};
    153      1.1  christos   unsigned char maximum_ops_per_instruction {};
    154      1.1  christos   unsigned char default_is_stmt {};
    155      1.1  christos   int line_base {};
    156      1.1  christos   unsigned char line_range {};
    157      1.1  christos   unsigned char opcode_base {};
    158      1.1  christos 
    159      1.1  christos   /* standard_opcode_lengths[i] is the number of operands for the
    160      1.1  christos      standard opcode whose value is i.  This means that
    161      1.1  christos      standard_opcode_lengths[0] is unused, and the last meaningful
    162      1.1  christos      element is standard_opcode_lengths[opcode_base - 1].  */
    163      1.1  christos   std::unique_ptr<unsigned char[]> standard_opcode_lengths;
    164      1.1  christos 
    165      1.1  christos   int file_names_size () const
    166      1.1  christos   { return m_file_names.size(); }
    167      1.1  christos 
    168      1.1  christos   /* The start and end of the statement program following this
    169      1.1  christos      header.  These point into dwarf2_per_objfile->line_buffer.  */
    170      1.1  christos   const gdb_byte *statement_program_start {}, *statement_program_end {};
    171      1.1  christos 
    172  1.1.1.2  christos   /* Return the most "complete" file name for FILE possible.
    173  1.1.1.2  christos 
    174  1.1.1.2  christos      This means prepending the directory and compilation directory, as needed,
    175  1.1.1.2  christos      until we get an absolute path.  */
    176  1.1.1.2  christos   std::string file_file_name (const file_entry &fe) const;
    177  1.1.1.2  christos 
    178  1.1.1.2  christos   /* Return the compilation directory of the compilation unit in the context of
    179  1.1.1.2  christos      which this line header is read.  Return nullptr if non applicable.  */
    180  1.1.1.2  christos   const char *comp_dir () const
    181  1.1.1.2  christos   { return m_comp_dir; }
    182      1.1  christos 
    183      1.1  christos  private:
    184      1.1  christos   /* The include_directories table.  Note these are observing
    185      1.1  christos      pointers.  The memory is owned by debug_line_buffer.  */
    186      1.1  christos   std::vector<const char *> m_include_dirs;
    187      1.1  christos 
    188      1.1  christos   /* The file_names table. This is private because the meaning of indexes
    189      1.1  christos      differs among DWARF versions (The first valid index is 1 in DWARF 4 and
    190      1.1  christos      before, and is 0 in DWARF 5 and later).  So the client should use
    191      1.1  christos      file_name_at method for access.  */
    192      1.1  christos   std::vector<file_entry> m_file_names;
    193  1.1.1.2  christos 
    194  1.1.1.2  christos   /* Compilation directory of the compilation unit in the context of which this
    195  1.1.1.2  christos      line header is read.  nullptr if unknown or not applicable.  */
    196  1.1.1.2  christos   const char *m_comp_dir = nullptr;
    197      1.1  christos };
    198      1.1  christos 
    199      1.1  christos typedef std::unique_ptr<line_header> line_header_up;
    200      1.1  christos 
    201      1.1  christos inline const char *
    202      1.1  christos file_entry::include_dir (const line_header *lh) const
    203      1.1  christos {
    204      1.1  christos   return lh->include_dir_at (d_index);
    205      1.1  christos }
    206      1.1  christos 
    207      1.1  christos /* Read the statement program header starting at SECT_OFF in SECTION.
    208      1.1  christos    Return line_header.  Returns nullptr if there is a problem reading
    209      1.1  christos    the header, e.g., if it has a version we don't understand.
    210      1.1  christos 
    211      1.1  christos    NOTE: the strings in the include directory and file name tables of
    212      1.1  christos    the returned object point into the dwarf line section buffer,
    213      1.1  christos    and must not be freed.  */
    214      1.1  christos 
    215      1.1  christos extern line_header_up dwarf_decode_line_header
    216      1.1  christos   (sect_offset sect_off, bool is_dwz, dwarf2_per_objfile *per_objfile,
    217  1.1.1.2  christos    struct dwarf2_section_info *section, const struct comp_unit_head *cu_header,
    218  1.1.1.2  christos    const char *comp_dir);
    219      1.1  christos 
    220      1.1  christos #endif /* DWARF2_LINE_HEADER_H */
    221