Home | History | Annotate | Line # | Download | only in dwarf2
      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.1.2  christos #include "dwarf2/comp-unit-head.h"
     21      1.1  christos #include "dwarf2/leb.h"
     22      1.1  christos #include "dwarf2/line-header.h"
     23      1.1  christos #include "dwarf2/read.h"
     24      1.1  christos #include "complaints.h"
     25      1.1  christos #include "filenames.h"
     26  1.1.1.2  christos #include "gdbsupport/pathstuff.h"
     27      1.1  christos 
     28      1.1  christos void
     29      1.1  christos line_header::add_include_dir (const char *include_dir)
     30      1.1  christos {
     31      1.1  christos   if (dwarf_line_debug >= 2)
     32      1.1  christos     {
     33      1.1  christos       size_t new_size;
     34      1.1  christos       if (version >= 5)
     35  1.1.1.2  christos 	new_size = m_include_dirs.size ();
     36      1.1  christos       else
     37  1.1.1.2  christos 	new_size = m_include_dirs.size () + 1;
     38  1.1.1.2  christos       gdb_printf (gdb_stdlog, "Adding dir %zu: %s\n",
     39  1.1.1.2  christos 		  new_size, include_dir);
     40      1.1  christos     }
     41      1.1  christos   m_include_dirs.push_back (include_dir);
     42      1.1  christos }
     43      1.1  christos 
     44      1.1  christos void
     45      1.1  christos line_header::add_file_name (const char *name,
     46      1.1  christos 			    dir_index d_index,
     47      1.1  christos 			    unsigned int mod_time,
     48      1.1  christos 			    unsigned int length)
     49      1.1  christos {
     50  1.1.1.2  christos   file_name_index index
     51  1.1.1.2  christos     = version >= 5 ? file_names_size (): file_names_size () + 1;
     52  1.1.1.2  christos 
     53      1.1  christos   if (dwarf_line_debug >= 2)
     54  1.1.1.2  christos     gdb_printf (gdb_stdlog, "Adding file %d: %s\n", index, name);
     55  1.1.1.2  christos 
     56  1.1.1.2  christos   m_file_names.emplace_back (name, index, d_index, mod_time, length);
     57      1.1  christos }
     58      1.1  christos 
     59  1.1.1.2  christos std::string
     60  1.1.1.2  christos line_header::file_file_name (const file_entry &fe) const
     61      1.1  christos {
     62  1.1.1.2  christos   gdb_assert (is_valid_file_index (fe.index));
     63      1.1  christos 
     64  1.1.1.2  christos   std::string ret = fe.name;
     65      1.1  christos 
     66  1.1.1.2  christos   if (IS_ABSOLUTE_PATH (ret))
     67  1.1.1.2  christos     return ret;
     68      1.1  christos 
     69  1.1.1.2  christos   const char *dir = fe.include_dir (this);
     70  1.1.1.2  christos   if (dir != nullptr)
     71  1.1.1.2  christos     ret = path_join (dir, ret.c_str ());
     72      1.1  christos 
     73  1.1.1.2  christos   if (IS_ABSOLUTE_PATH (ret))
     74  1.1.1.2  christos     return ret;
     75      1.1  christos 
     76  1.1.1.2  christos   if (m_comp_dir != nullptr)
     77  1.1.1.2  christos     ret = path_join (m_comp_dir, ret.c_str ());
     78  1.1.1.2  christos 
     79  1.1.1.2  christos   return ret;
     80      1.1  christos }
     81      1.1  christos 
     82      1.1  christos static void
     83      1.1  christos dwarf2_statement_list_fits_in_line_number_section_complaint (void)
     84      1.1  christos {
     85      1.1  christos   complaint (_("statement list doesn't fit in .debug_line section"));
     86      1.1  christos }
     87      1.1  christos 
     88      1.1  christos /* Cover function for read_initial_length.
     89      1.1  christos    Returns the length of the object at BUF, and stores the size of the
     90      1.1  christos    initial length in *BYTES_READ and stores the size that offsets will be in
     91      1.1  christos    *OFFSET_SIZE.
     92      1.1  christos    If the initial length size is not equivalent to that specified in
     93      1.1  christos    CU_HEADER then issue a complaint.
     94      1.1  christos    This is useful when reading non-comp-unit headers.  */
     95      1.1  christos 
     96      1.1  christos static LONGEST
     97      1.1  christos read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
     98      1.1  christos 					const struct comp_unit_head *cu_header,
     99      1.1  christos 					unsigned int *bytes_read,
    100      1.1  christos 					unsigned int *offset_size)
    101      1.1  christos {
    102      1.1  christos   LONGEST length = read_initial_length (abfd, buf, bytes_read);
    103      1.1  christos 
    104      1.1  christos   gdb_assert (cu_header->initial_length_size == 4
    105      1.1  christos 	      || cu_header->initial_length_size == 8
    106      1.1  christos 	      || cu_header->initial_length_size == 12);
    107      1.1  christos 
    108      1.1  christos   if (cu_header->initial_length_size != *bytes_read)
    109      1.1  christos     complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
    110      1.1  christos 
    111      1.1  christos   *offset_size = (*bytes_read == 4) ? 4 : 8;
    112      1.1  christos   return length;
    113      1.1  christos }
    114      1.1  christos 
    115      1.1  christos /* Read directory or file name entry format, starting with byte of
    116      1.1  christos    format count entries, ULEB128 pairs of entry formats, ULEB128 of
    117      1.1  christos    entries count and the entries themselves in the described entry
    118      1.1  christos    format.  */
    119      1.1  christos 
    120      1.1  christos static void
    121      1.1  christos read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
    122      1.1  christos 			const gdb_byte **bufp, struct line_header *lh,
    123  1.1.1.2  christos 			unsigned int offset_size,
    124      1.1  christos 			void (*callback) (struct line_header *lh,
    125      1.1  christos 					  const char *name,
    126      1.1  christos 					  dir_index d_index,
    127      1.1  christos 					  unsigned int mod_time,
    128      1.1  christos 					  unsigned int length))
    129      1.1  christos {
    130      1.1  christos   gdb_byte format_count, formati;
    131      1.1  christos   ULONGEST data_count, datai;
    132      1.1  christos   const gdb_byte *buf = *bufp;
    133      1.1  christos   const gdb_byte *format_header_data;
    134      1.1  christos   unsigned int bytes_read;
    135      1.1  christos 
    136      1.1  christos   format_count = read_1_byte (abfd, buf);
    137      1.1  christos   buf += 1;
    138      1.1  christos   format_header_data = buf;
    139      1.1  christos   for (formati = 0; formati < format_count; formati++)
    140      1.1  christos     {
    141      1.1  christos       read_unsigned_leb128 (abfd, buf, &bytes_read);
    142      1.1  christos       buf += bytes_read;
    143      1.1  christos       read_unsigned_leb128 (abfd, buf, &bytes_read);
    144      1.1  christos       buf += bytes_read;
    145      1.1  christos     }
    146      1.1  christos 
    147      1.1  christos   data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
    148      1.1  christos   buf += bytes_read;
    149      1.1  christos   for (datai = 0; datai < data_count; datai++)
    150      1.1  christos     {
    151      1.1  christos       const gdb_byte *format = format_header_data;
    152      1.1  christos       struct file_entry fe;
    153      1.1  christos 
    154      1.1  christos       for (formati = 0; formati < format_count; formati++)
    155      1.1  christos 	{
    156      1.1  christos 	  ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
    157      1.1  christos 	  format += bytes_read;
    158      1.1  christos 
    159      1.1  christos 	  ULONGEST form  = read_unsigned_leb128 (abfd, format, &bytes_read);
    160      1.1  christos 	  format += bytes_read;
    161      1.1  christos 
    162  1.1.1.3  christos 	  std::optional<const char *> string;
    163  1.1.1.3  christos 	  std::optional<unsigned int> uint;
    164      1.1  christos 
    165      1.1  christos 	  switch (form)
    166      1.1  christos 	    {
    167      1.1  christos 	    case DW_FORM_string:
    168      1.1  christos 	      string.emplace (read_direct_string (abfd, buf, &bytes_read));
    169      1.1  christos 	      buf += bytes_read;
    170      1.1  christos 	      break;
    171      1.1  christos 
    172      1.1  christos 	    case DW_FORM_line_strp:
    173  1.1.1.2  christos 	      {
    174  1.1.1.2  christos 		const char *str
    175  1.1.1.2  christos 		  = per_objfile->read_line_string (buf, offset_size);
    176  1.1.1.2  christos 		string.emplace (str);
    177  1.1.1.2  christos 		buf += offset_size;
    178  1.1.1.2  christos 	      }
    179      1.1  christos 	      break;
    180      1.1  christos 
    181      1.1  christos 	    case DW_FORM_data1:
    182      1.1  christos 	      uint.emplace (read_1_byte (abfd, buf));
    183      1.1  christos 	      buf += 1;
    184      1.1  christos 	      break;
    185      1.1  christos 
    186      1.1  christos 	    case DW_FORM_data2:
    187      1.1  christos 	      uint.emplace (read_2_bytes (abfd, buf));
    188      1.1  christos 	      buf += 2;
    189      1.1  christos 	      break;
    190      1.1  christos 
    191      1.1  christos 	    case DW_FORM_data4:
    192      1.1  christos 	      uint.emplace (read_4_bytes (abfd, buf));
    193      1.1  christos 	      buf += 4;
    194      1.1  christos 	      break;
    195      1.1  christos 
    196      1.1  christos 	    case DW_FORM_data8:
    197      1.1  christos 	      uint.emplace (read_8_bytes (abfd, buf));
    198      1.1  christos 	      buf += 8;
    199      1.1  christos 	      break;
    200      1.1  christos 
    201      1.1  christos 	    case DW_FORM_data16:
    202      1.1  christos 	      /*  This is used for MD5, but file_entry does not record MD5s. */
    203      1.1  christos 	      buf += 16;
    204      1.1  christos 	      break;
    205      1.1  christos 
    206      1.1  christos 	    case DW_FORM_udata:
    207      1.1  christos 	      uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
    208      1.1  christos 	      buf += bytes_read;
    209      1.1  christos 	      break;
    210      1.1  christos 
    211      1.1  christos 	    case DW_FORM_block:
    212      1.1  christos 	      /* It is valid only for DW_LNCT_timestamp which is ignored by
    213      1.1  christos 		 current GDB.  */
    214      1.1  christos 	      break;
    215      1.1  christos 	    }
    216      1.1  christos 
    217  1.1.1.2  christos 	  /* Normalize nullptr string.  */
    218  1.1.1.2  christos 	  if (string.has_value () && *string == nullptr)
    219  1.1.1.2  christos 	    string.emplace ("");
    220  1.1.1.2  christos 
    221      1.1  christos 	  switch (content_type)
    222      1.1  christos 	    {
    223      1.1  christos 	    case DW_LNCT_path:
    224      1.1  christos 	      if (string.has_value ())
    225      1.1  christos 		fe.name = *string;
    226      1.1  christos 	      break;
    227      1.1  christos 	    case DW_LNCT_directory_index:
    228      1.1  christos 	      if (uint.has_value ())
    229      1.1  christos 		fe.d_index = (dir_index) *uint;
    230      1.1  christos 	      break;
    231      1.1  christos 	    case DW_LNCT_timestamp:
    232      1.1  christos 	      if (uint.has_value ())
    233      1.1  christos 		fe.mod_time = *uint;
    234      1.1  christos 	      break;
    235      1.1  christos 	    case DW_LNCT_size:
    236      1.1  christos 	      if (uint.has_value ())
    237      1.1  christos 		fe.length = *uint;
    238      1.1  christos 	      break;
    239      1.1  christos 	    case DW_LNCT_MD5:
    240      1.1  christos 	      break;
    241      1.1  christos 	    default:
    242      1.1  christos 	      complaint (_("Unknown format content type %s"),
    243      1.1  christos 			 pulongest (content_type));
    244      1.1  christos 	    }
    245      1.1  christos 	}
    246      1.1  christos 
    247      1.1  christos       callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
    248      1.1  christos     }
    249      1.1  christos 
    250      1.1  christos   *bufp = buf;
    251      1.1  christos }
    252      1.1  christos 
    253      1.1  christos /* See line-header.h.  */
    254      1.1  christos 
    255      1.1  christos line_header_up
    256      1.1  christos dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
    257      1.1  christos 			   dwarf2_per_objfile *per_objfile,
    258      1.1  christos 			   struct dwarf2_section_info *section,
    259  1.1.1.2  christos 			   const struct comp_unit_head *cu_header,
    260  1.1.1.2  christos 			   const char *comp_dir)
    261      1.1  christos {
    262      1.1  christos   const gdb_byte *line_ptr;
    263      1.1  christos   unsigned int bytes_read, offset_size;
    264      1.1  christos   int i;
    265      1.1  christos   const char *cur_dir, *cur_file;
    266      1.1  christos 
    267      1.1  christos   bfd *abfd = section->get_bfd_owner ();
    268      1.1  christos 
    269      1.1  christos   /* Make sure that at least there's room for the total_length field.
    270      1.1  christos      That could be 12 bytes long, but we're just going to fudge that.  */
    271      1.1  christos   if (to_underlying (sect_off) + 4 >= section->size)
    272      1.1  christos     {
    273      1.1  christos       dwarf2_statement_list_fits_in_line_number_section_complaint ();
    274      1.1  christos       return 0;
    275      1.1  christos     }
    276      1.1  christos 
    277  1.1.1.2  christos   line_header_up lh (new line_header (comp_dir));
    278      1.1  christos 
    279      1.1  christos   lh->sect_off = sect_off;
    280      1.1  christos   lh->offset_in_dwz = is_dwz;
    281      1.1  christos 
    282      1.1  christos   line_ptr = section->buffer + to_underlying (sect_off);
    283      1.1  christos 
    284      1.1  christos   /* Read in the header.  */
    285  1.1.1.2  christos   LONGEST unit_length
    286  1.1.1.2  christos     = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
    287  1.1.1.2  christos 					      &bytes_read, &offset_size);
    288      1.1  christos   line_ptr += bytes_read;
    289      1.1  christos 
    290      1.1  christos   const gdb_byte *start_here = line_ptr;
    291      1.1  christos 
    292  1.1.1.2  christos   if (line_ptr + unit_length > (section->buffer + section->size))
    293      1.1  christos     {
    294      1.1  christos       dwarf2_statement_list_fits_in_line_number_section_complaint ();
    295      1.1  christos       return 0;
    296      1.1  christos     }
    297  1.1.1.2  christos   lh->statement_program_end = start_here + unit_length;
    298      1.1  christos   lh->version = read_2_bytes (abfd, line_ptr);
    299      1.1  christos   line_ptr += 2;
    300      1.1  christos   if (lh->version > 5)
    301      1.1  christos     {
    302      1.1  christos       /* This is a version we don't understand.  The format could have
    303      1.1  christos 	 changed in ways we don't handle properly so just punt.  */
    304      1.1  christos       complaint (_("unsupported version in .debug_line section"));
    305      1.1  christos       return NULL;
    306      1.1  christos     }
    307      1.1  christos   if (lh->version >= 5)
    308      1.1  christos     {
    309      1.1  christos       gdb_byte segment_selector_size;
    310      1.1  christos 
    311      1.1  christos       /* Skip address size.  */
    312      1.1  christos       read_1_byte (abfd, line_ptr);
    313      1.1  christos       line_ptr += 1;
    314      1.1  christos 
    315      1.1  christos       segment_selector_size = read_1_byte (abfd, line_ptr);
    316      1.1  christos       line_ptr += 1;
    317      1.1  christos       if (segment_selector_size != 0)
    318      1.1  christos 	{
    319      1.1  christos 	  complaint (_("unsupported segment selector size %u "
    320      1.1  christos 		       "in .debug_line section"),
    321      1.1  christos 		     segment_selector_size);
    322      1.1  christos 	  return NULL;
    323      1.1  christos 	}
    324      1.1  christos     }
    325  1.1.1.2  christos 
    326  1.1.1.2  christos   LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
    327      1.1  christos   line_ptr += offset_size;
    328  1.1.1.2  christos   lh->statement_program_start = line_ptr + header_length;
    329      1.1  christos   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
    330      1.1  christos   line_ptr += 1;
    331  1.1.1.2  christos 
    332      1.1  christos   if (lh->version >= 4)
    333      1.1  christos     {
    334      1.1  christos       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
    335      1.1  christos       line_ptr += 1;
    336      1.1  christos     }
    337      1.1  christos   else
    338      1.1  christos     lh->maximum_ops_per_instruction = 1;
    339      1.1  christos 
    340      1.1  christos   if (lh->maximum_ops_per_instruction == 0)
    341      1.1  christos     {
    342      1.1  christos       lh->maximum_ops_per_instruction = 1;
    343      1.1  christos       complaint (_("invalid maximum_ops_per_instruction "
    344      1.1  christos 		   "in `.debug_line' section"));
    345      1.1  christos     }
    346      1.1  christos 
    347      1.1  christos   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
    348      1.1  christos   line_ptr += 1;
    349      1.1  christos   lh->line_base = read_1_signed_byte (abfd, line_ptr);
    350      1.1  christos   line_ptr += 1;
    351      1.1  christos   lh->line_range = read_1_byte (abfd, line_ptr);
    352      1.1  christos   line_ptr += 1;
    353      1.1  christos   lh->opcode_base = read_1_byte (abfd, line_ptr);
    354      1.1  christos   line_ptr += 1;
    355      1.1  christos   lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
    356      1.1  christos 
    357      1.1  christos   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
    358      1.1  christos   for (i = 1; i < lh->opcode_base; ++i)
    359      1.1  christos     {
    360      1.1  christos       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
    361      1.1  christos       line_ptr += 1;
    362      1.1  christos     }
    363      1.1  christos 
    364      1.1  christos   if (lh->version >= 5)
    365      1.1  christos     {
    366      1.1  christos       /* Read directory table.  */
    367      1.1  christos       read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
    368  1.1.1.2  christos 			      offset_size,
    369      1.1  christos 			      [] (struct line_header *header, const char *name,
    370      1.1  christos 				  dir_index d_index, unsigned int mod_time,
    371      1.1  christos 				  unsigned int length)
    372      1.1  christos 	{
    373      1.1  christos 	  header->add_include_dir (name);
    374      1.1  christos 	});
    375      1.1  christos 
    376      1.1  christos       /* Read file name table.  */
    377      1.1  christos       read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
    378  1.1.1.2  christos 			      offset_size,
    379      1.1  christos 			      [] (struct line_header *header, const char *name,
    380      1.1  christos 				  dir_index d_index, unsigned int mod_time,
    381      1.1  christos 				  unsigned int length)
    382      1.1  christos 	{
    383      1.1  christos 	  header->add_file_name (name, d_index, mod_time, length);
    384      1.1  christos 	});
    385      1.1  christos     }
    386      1.1  christos   else
    387      1.1  christos     {
    388      1.1  christos       /* Read directory table.  */
    389      1.1  christos       while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
    390      1.1  christos 	{
    391      1.1  christos 	  line_ptr += bytes_read;
    392      1.1  christos 	  lh->add_include_dir (cur_dir);
    393      1.1  christos 	}
    394      1.1  christos       line_ptr += bytes_read;
    395      1.1  christos 
    396      1.1  christos       /* Read file name table.  */
    397      1.1  christos       while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
    398      1.1  christos 	{
    399      1.1  christos 	  unsigned int mod_time, length;
    400      1.1  christos 	  dir_index d_index;
    401      1.1  christos 
    402      1.1  christos 	  line_ptr += bytes_read;
    403      1.1  christos 	  d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
    404      1.1  christos 	  line_ptr += bytes_read;
    405      1.1  christos 	  mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
    406      1.1  christos 	  line_ptr += bytes_read;
    407      1.1  christos 	  length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
    408      1.1  christos 	  line_ptr += bytes_read;
    409      1.1  christos 
    410      1.1  christos 	  lh->add_file_name (cur_file, d_index, mod_time, length);
    411      1.1  christos 	}
    412      1.1  christos       line_ptr += bytes_read;
    413      1.1  christos     }
    414      1.1  christos 
    415      1.1  christos   if (line_ptr > (section->buffer + section->size))
    416      1.1  christos     complaint (_("line number info header doesn't "
    417      1.1  christos 		 "fit in `.debug_line' section"));
    418      1.1  christos 
    419      1.1  christos   return lh;
    420      1.1  christos }
    421