1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
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 (_("read_comp_unit_head: invalid section_kind"));
72 cu_header->unit_type = static_cast<enum dwarf_unit_type>
73 (read_1_byte (abfd, info_ptr));
75 switch (cu_header->unit_type)
80 case DW_UT_split_compile:
81 if (section_kind != rcuh_kind::COMPILE)
82 error (_("Dwarf Error: wrong unit_type in compilation unit header "
83 "(is %s, should be %s) [in module %s]"),
84 dwarf_unit_type_name (cu_header->unit_type),
85 dwarf_unit_type_name (DW_UT_type), filename);
88 case DW_UT_split_type:
89 section_kind = rcuh_kind::TYPE;
92 error (_("Dwarf Error: wrong unit_type in compilation unit header "
93 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
94 "[in module %s]"), cu_header->unit_type,
95 dwarf_unit_type_name (DW_UT_compile),
96 dwarf_unit_type_name (DW_UT_skeleton),
97 dwarf_unit_type_name (DW_UT_split_compile),
98 dwarf_unit_type_name (DW_UT_type),
99 dwarf_unit_type_name (DW_UT_split_type), filename);
102 cu_header->addr_size = read_1_byte (abfd, info_ptr);
105 cu_header->abbrev_sect_off
106 = (sect_offset) cu_header->read_offset (abfd, info_ptr, &bytes_read);
107 info_ptr += bytes_read;
108 if (cu_header->version < 5)
110 cu_header->addr_size = read_1_byte (abfd, info_ptr);
113 signed_addr = bfd_get_sign_extend_vma (abfd);
115 internal_error (_("read_comp_unit_head: dwarf from non elf file"));
116 cu_header->signed_addr_p = signed_addr;
118 bool header_has_signature = section_kind == rcuh_kind::TYPE
119 || cu_header->unit_type == DW_UT_skeleton
120 || cu_header->unit_type == DW_UT_split_compile;
122 if (header_has_signature)
124 cu_header->signature = read_8_bytes (abfd, info_ptr);
128 if (section_kind == rcuh_kind::TYPE)
131 type_offset = cu_header->read_offset (abfd, info_ptr, &bytes_read);
132 info_ptr += bytes_read;
133 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
134 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
135 error (_("Dwarf Error: Too big type_offset in compilation unit "
136 "header (is %s) [in module %s]"), plongest (type_offset),
143 /* Subroutine of read_and_check_comp_unit_head and
144 read_and_check_type_unit_head to simplify them.
145 Perform various error checking on the header. */
148 error_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
149 struct comp_unit_head *header,
150 struct dwarf2_section_info *section,
151 struct dwarf2_section_info *abbrev_section)
153 const char *filename = section->get_file_name ();
155 if (to_underlying (header->abbrev_sect_off)
156 >= abbrev_section->get_size (per_objfile->objfile))
157 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
158 "(offset %s + 6) [in module %s]"),
159 sect_offset_str (header->abbrev_sect_off),
160 sect_offset_str (header->sect_off),
163 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
164 avoid potential 32-bit overflow. */
165 if (((ULONGEST) header->sect_off + header->get_length ())
167 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
168 "(offset %s + 0) [in module %s]"),
169 header->length, sect_offset_str (header->sect_off),
173 /* See comp-unit-head.h. */
176 read_and_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
177 struct comp_unit_head *header,
178 struct dwarf2_section_info *section,
179 struct dwarf2_section_info *abbrev_section,
180 const gdb_byte *info_ptr,
181 rcuh_kind section_kind)
183 const gdb_byte *beg_of_comp_unit = info_ptr;
185 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
187 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
189 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
191 error_check_comp_unit_head (per_objfile, header, section, abbrev_section);
197 comp_unit_head::read_address (bfd *abfd, const gdb_byte *buf,
198 unsigned int *bytes_read) const
200 CORE_ADDR retval = 0;
207 retval = bfd_get_signed_16 (abfd, buf);
210 retval = bfd_get_signed_32 (abfd, buf);
213 retval = bfd_get_signed_64 (abfd, buf);
216 internal_error (_("read_address: bad switch, signed [in module %s]"),
217 bfd_get_filename (abfd));
225 retval = bfd_get_16 (abfd, buf);
228 retval = bfd_get_32 (abfd, buf);
231 retval = bfd_get_64 (abfd, buf);
234 internal_error (_("read_address: bad switch, "
235 "unsigned [in module %s]"),
236 bfd_get_filename (abfd));
240 *bytes_read = addr_size;