1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@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
12 This file is part of GDB.
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.
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.
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/>. */
28 #include "dwarf2/comp-unit-head.h"
29 #include "dwarf2/leb.h"
30 #include "dwarf2/read.h"
31 #include "dwarf2/section.h"
32 #include "dwarf2/stringify.h"
34 /* See comp-unit-head.h. */
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
)
43 unsigned int bytes_read
;
44 const char *filename
= section
->get_file_name ();
45 bfd
*abfd
= section
->get_bfd_owner ();
47 cu_header
->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: wrong version in compilation unit header "
54 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
56 cu_header
->version
= version
;
58 if (cu_header
->version
< 5)
61 case rcuh_kind::COMPILE
:
62 cu_header
->unit_type
= DW_UT_compile
;
65 cu_header
->unit_type
= DW_UT_type
;
68 internal_error (__FILE__
, __LINE__
,
69 _("read_comp_unit_head: invalid section_kind"));
73 cu_header
->unit_type
= static_cast<enum dwarf_unit_type
>
74 (read_1_byte (abfd
, info_ptr
));
76 switch (cu_header
->unit_type
)
81 case DW_UT_split_compile
:
82 if (section_kind
!= rcuh_kind::COMPILE
)
83 error (_("Dwarf Error: wrong unit_type in compilation unit header "
84 "(is %s, should be %s) [in module %s]"),
85 dwarf_unit_type_name (cu_header
->unit_type
),
86 dwarf_unit_type_name (DW_UT_type
), filename
);
89 case DW_UT_split_type
:
90 section_kind
= rcuh_kind::TYPE
;
93 error (_("Dwarf Error: wrong unit_type in compilation unit header "
94 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
95 "[in module %s]"), cu_header
->unit_type
,
96 dwarf_unit_type_name (DW_UT_compile
),
97 dwarf_unit_type_name (DW_UT_skeleton
),
98 dwarf_unit_type_name (DW_UT_split_compile
),
99 dwarf_unit_type_name (DW_UT_type
),
100 dwarf_unit_type_name (DW_UT_split_type
), filename
);
103 cu_header
->addr_size
= read_1_byte (abfd
, info_ptr
);
106 cu_header
->abbrev_sect_off
107 = (sect_offset
) cu_header
->read_offset (abfd
, info_ptr
, &bytes_read
);
108 info_ptr
+= bytes_read
;
109 if (cu_header
->version
< 5)
111 cu_header
->addr_size
= read_1_byte (abfd
, info_ptr
);
114 signed_addr
= bfd_get_sign_extend_vma (abfd
);
116 internal_error (__FILE__
, __LINE__
,
117 _("read_comp_unit_head: dwarf from non elf file"));
118 cu_header
->signed_addr_p
= signed_addr
;
120 bool header_has_signature
= section_kind
== rcuh_kind::TYPE
121 || cu_header
->unit_type
== DW_UT_skeleton
122 || cu_header
->unit_type
== DW_UT_split_compile
;
124 if (header_has_signature
)
126 cu_header
->signature
= read_8_bytes (abfd
, info_ptr
);
130 if (section_kind
== rcuh_kind::TYPE
)
133 type_offset
= cu_header
->read_offset (abfd
, info_ptr
, &bytes_read
);
134 info_ptr
+= bytes_read
;
135 cu_header
->type_cu_offset_in_tu
= (cu_offset
) type_offset
;
136 if (to_underlying (cu_header
->type_cu_offset_in_tu
) != type_offset
)
137 error (_("Dwarf Error: Too big type_offset in compilation unit "
138 "header (is %s) [in module %s]"), plongest (type_offset
),
145 /* Subroutine of read_and_check_comp_unit_head and
146 read_and_check_type_unit_head to simplify them.
147 Perform various error checking on the header. */
150 error_check_comp_unit_head (dwarf2_per_objfile
*per_objfile
,
151 struct comp_unit_head
*header
,
152 struct dwarf2_section_info
*section
,
153 struct dwarf2_section_info
*abbrev_section
)
155 const char *filename
= section
->get_file_name ();
157 if (to_underlying (header
->abbrev_sect_off
)
158 >= abbrev_section
->get_size (per_objfile
->objfile
))
159 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
160 "(offset %s + 6) [in module %s]"),
161 sect_offset_str (header
->abbrev_sect_off
),
162 sect_offset_str (header
->sect_off
),
165 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
166 avoid potential 32-bit overflow. */
167 if (((ULONGEST
) header
->sect_off
+ header
->get_length ())
169 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
170 "(offset %s + 0) [in module %s]"),
171 header
->length
, sect_offset_str (header
->sect_off
),
175 /* See comp-unit-head.h. */
178 read_and_check_comp_unit_head (dwarf2_per_objfile
*per_objfile
,
179 struct comp_unit_head
*header
,
180 struct dwarf2_section_info
*section
,
181 struct dwarf2_section_info
*abbrev_section
,
182 const gdb_byte
*info_ptr
,
183 rcuh_kind section_kind
)
185 const gdb_byte
*beg_of_comp_unit
= info_ptr
;
187 header
->sect_off
= (sect_offset
) (beg_of_comp_unit
- section
->buffer
);
189 info_ptr
= read_comp_unit_head (header
, info_ptr
, section
, section_kind
);
191 header
->first_die_cu_offset
= (cu_offset
) (info_ptr
- beg_of_comp_unit
);
193 error_check_comp_unit_head (per_objfile
, header
, section
, abbrev_section
);
199 comp_unit_head::read_address (bfd
*abfd
, const gdb_byte
*buf
,
200 unsigned int *bytes_read
) const
202 CORE_ADDR retval
= 0;
209 retval
= bfd_get_signed_16 (abfd
, buf
);
212 retval
= bfd_get_signed_32 (abfd
, buf
);
215 retval
= bfd_get_signed_64 (abfd
, buf
);
218 internal_error (__FILE__
, __LINE__
,
219 _("read_address: bad switch, signed [in module %s]"),
220 bfd_get_filename (abfd
));
228 retval
= bfd_get_16 (abfd
, buf
);
231 retval
= bfd_get_32 (abfd
, buf
);
234 retval
= bfd_get_64 (abfd
, buf
);
237 internal_error (__FILE__
, __LINE__
,
238 _("read_address: bad switch, "
239 "unsigned [in module %s]"),
240 bfd_get_filename (abfd
));
244 *bytes_read
= addr_size
;