Home | History | Annotate | Line # | Download | only in dwarf2
      1 /* DWARF 2 debugging format support for GDB.
      2 
      3    Copyright (C) 1994-2024 Free Software Foundation, Inc.
      4 
      5    Adapted by Gary Funck (gary (at) intrepid.com), Intrepid Technology,
      6    Inc.  with support from Florida State University (under contract
      7    with the Ada Joint Program Office), and Silicon Graphics, Inc.
      8    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
      9    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
     10    support.
     11 
     12    This file is part of GDB.
     13 
     14    This program is free software; you can redistribute it and/or modify
     15    it under the terms of the GNU General Public License as published by
     16    the Free Software Foundation; either version 3 of the License, or
     17    (at your option) any later version.
     18 
     19    This program is distributed in the hope that it will be useful,
     20    but WITHOUT ANY WARRANTY; without even the implied warranty of
     21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22    GNU General Public License for more details.
     23 
     24    You should have received a copy of the GNU General Public License
     25    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     26 
     27 #include "dwarf2/comp-unit-head.h"
     28 #include "dwarf2/leb.h"
     29 #include "dwarf2/read.h"
     30 #include "dwarf2/section.h"
     31 #include "dwarf2/stringify.h"
     32 #include "dwarf2/error.h"
     33 
     34 /* See comp-unit-head.h.  */
     35 
     36 const gdb_byte *
     37 read_comp_unit_head (struct comp_unit_head *cu_header,
     38 		     const gdb_byte *info_ptr,
     39 		     struct dwarf2_section_info *section,
     40 		     rcuh_kind section_kind)
     41 {
     42   int signed_addr;
     43   unsigned int bytes_read;
     44   const char *filename = section->get_file_name ();
     45   bfd *abfd = section->get_bfd_owner ();
     46 
     47   cu_header->set_length (read_initial_length (abfd, info_ptr, &bytes_read));
     48   cu_header->initial_length_size = bytes_read;
     49   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
     50   info_ptr += bytes_read;
     51   unsigned version = read_2_bytes (abfd, info_ptr);
     52   if (version < 2 || version > 5)
     53     error (_(DWARF_ERROR_PREFIX
     54 	     "wrong version in compilation unit header "
     55 	     "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
     56 	   version, filename);
     57   cu_header->version = version;
     58   info_ptr += 2;
     59   if (cu_header->version < 5)
     60     switch (section_kind)
     61       {
     62       case rcuh_kind::COMPILE:
     63 	cu_header->unit_type = DW_UT_compile;
     64 	break;
     65       case rcuh_kind::TYPE:
     66 	cu_header->unit_type = DW_UT_type;
     67 	break;
     68       default:
     69 	internal_error (_("read_comp_unit_head: invalid section_kind"));
     70       }
     71   else
     72     {
     73       cu_header->unit_type = static_cast<enum dwarf_unit_type>
     74 						 (read_1_byte (abfd, info_ptr));
     75       info_ptr += 1;
     76       switch (cu_header->unit_type)
     77 	{
     78 	case DW_UT_compile:
     79 	case DW_UT_partial:
     80 	case DW_UT_skeleton:
     81 	case DW_UT_split_compile:
     82 	  if (section_kind != rcuh_kind::COMPILE)
     83 	    error (_(DWARF_ERROR_PREFIX
     84 		     "wrong unit_type in compilation unit header "
     85 		     "(is %s, should be %s) [in module %s]"),
     86 		   dwarf_unit_type_name (cu_header->unit_type),
     87 		   dwarf_unit_type_name (DW_UT_type), filename);
     88 	  break;
     89 	case DW_UT_type:
     90 	case DW_UT_split_type:
     91 	  section_kind = rcuh_kind::TYPE;
     92 	  break;
     93 	default:
     94 	  error (_(DWARF_ERROR_PREFIX
     95 		   "wrong unit_type in compilation unit header "
     96 		   "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
     97 		   "[in module %s]"),
     98 		 cu_header->unit_type, dwarf_unit_type_name (DW_UT_compile),
     99 		 dwarf_unit_type_name (DW_UT_skeleton),
    100 		 dwarf_unit_type_name (DW_UT_split_compile),
    101 		 dwarf_unit_type_name (DW_UT_type),
    102 		 dwarf_unit_type_name (DW_UT_split_type), filename);
    103 	}
    104 
    105       cu_header->addr_size = read_1_byte (abfd, info_ptr);
    106       info_ptr += 1;
    107     }
    108   cu_header->abbrev_sect_off
    109     = (sect_offset) cu_header->read_offset (abfd, info_ptr, &bytes_read);
    110   info_ptr += bytes_read;
    111   if (cu_header->version < 5)
    112     {
    113       cu_header->addr_size = read_1_byte (abfd, info_ptr);
    114       info_ptr += 1;
    115     }
    116   signed_addr = bfd_get_sign_extend_vma (abfd);
    117   if (signed_addr < 0)
    118     internal_error (_("read_comp_unit_head: dwarf from non elf file"));
    119   cu_header->signed_addr_p = signed_addr;
    120 
    121   bool header_has_signature = section_kind == rcuh_kind::TYPE
    122     || cu_header->unit_type == DW_UT_skeleton
    123     || cu_header->unit_type == DW_UT_split_compile;
    124 
    125   if (header_has_signature)
    126     {
    127       cu_header->signature = read_8_bytes (abfd, info_ptr);
    128       info_ptr += 8;
    129     }
    130 
    131   if (section_kind == rcuh_kind::TYPE)
    132     {
    133       LONGEST type_offset;
    134       type_offset = cu_header->read_offset (abfd, info_ptr, &bytes_read);
    135       info_ptr += bytes_read;
    136       cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
    137       if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
    138 	error (_(DWARF_ERROR_PREFIX
    139 		 "Too big type_offset in compilation unit "
    140 		 "header (is %s) [in module %s]"),
    141 	       plongest (type_offset), filename);
    142     }
    143 
    144   return info_ptr;
    145 }
    146 
    147 /* Subroutine of read_and_check_comp_unit_head and
    148    read_and_check_type_unit_head to simplify them.
    149    Perform various error checking on the header.  */
    150 
    151 static void
    152 error_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
    153 			    struct comp_unit_head *header,
    154 			    struct dwarf2_section_info *section,
    155 			    struct dwarf2_section_info *abbrev_section)
    156 {
    157   const char *filename = section->get_file_name ();
    158 
    159   if (to_underlying (header->abbrev_sect_off)
    160       >= abbrev_section->get_size (per_objfile->objfile))
    161     error (_(DWARF_ERROR_PREFIX
    162 	     "bad offset (%s) in compilation unit header "
    163 	     "(offset %s + 6) [in module %s]"),
    164 	   sect_offset_str (header->abbrev_sect_off),
    165 	   sect_offset_str (header->sect_off),
    166 	   filename);
    167 
    168   /* Cast to ULONGEST to use 64-bit arithmetic when possible to
    169      avoid potential 32-bit overflow.  */
    170   if (((ULONGEST) header->sect_off + header->get_length_with_initial ())
    171       > section->size)
    172     error (_(DWARF_ERROR_PREFIX
    173 	     "bad length (0x%x) in compilation unit header "
    174 	     "(offset %s + 0) [in module %s]"),
    175 	   header->get_length_without_initial (), sect_offset_str (header->sect_off),
    176 	   filename);
    177 }
    178 
    179 /* See comp-unit-head.h.  */
    180 
    181 const gdb_byte *
    182 read_and_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
    183 			       struct comp_unit_head *header,
    184 			       struct dwarf2_section_info *section,
    185 			       struct dwarf2_section_info *abbrev_section,
    186 			       const gdb_byte *info_ptr,
    187 			       rcuh_kind section_kind)
    188 {
    189   const gdb_byte *beg_of_comp_unit = info_ptr;
    190 
    191   header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
    192 
    193   info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
    194 
    195   header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
    196 
    197   error_check_comp_unit_head (per_objfile, header, section, abbrev_section);
    198 
    199   return info_ptr;
    200 }
    201 
    202 unrelocated_addr
    203 comp_unit_head::read_address (bfd *abfd, const gdb_byte *buf,
    204 			      unsigned int *bytes_read) const
    205 {
    206   ULONGEST retval = 0;
    207 
    208   if (signed_addr_p)
    209     {
    210       switch (addr_size)
    211 	{
    212 	case 2:
    213 	  retval = bfd_get_signed_16 (abfd, buf);
    214 	  break;
    215 	case 4:
    216 	  retval = bfd_get_signed_32 (abfd, buf);
    217 	  break;
    218 	case 8:
    219 	  retval = bfd_get_signed_64 (abfd, buf);
    220 	  break;
    221 	default:
    222 	  internal_error (_("read_address: bad switch, signed [in module %s]"),
    223 			  bfd_get_filename (abfd));
    224 	}
    225     }
    226   else
    227     {
    228       switch (addr_size)
    229 	{
    230 	case 2:
    231 	  retval = bfd_get_16 (abfd, buf);
    232 	  break;
    233 	case 4:
    234 	  retval = bfd_get_32 (abfd, buf);
    235 	  break;
    236 	case 8:
    237 	  retval = bfd_get_64 (abfd, buf);
    238 	  break;
    239 	default:
    240 	  internal_error (_("read_address: bad switch, "
    241 			    "unsigned [in module %s]"),
    242 			  bfd_get_filename (abfd));
    243 	}
    244     }
    245 
    246   *bytes_read = addr_size;
    247   return (unrelocated_addr) retval;
    248 }
    249