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