Commit | Line | Data |
---|---|---|
4057dfde TT |
1 | /* DWARF 2 debugging format support for GDB. |
2 | ||
88b9d363 | 3 | Copyright (C) 1994-2022 Free Software Foundation, Inc. |
4057dfde TT |
4 | |
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 | |
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 "defs.h" | |
cd53fa40 | 28 | #include "dwarf2/comp-unit-head.h" |
4057dfde TT |
29 | #include "dwarf2/leb.h" |
30 | #include "dwarf2/read.h" | |
31 | #include "dwarf2/section.h" | |
15310fd4 | 32 | #include "dwarf2/stringify.h" |
4057dfde | 33 | |
cd53fa40 | 34 | /* See comp-unit-head.h. */ |
4057dfde TT |
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->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; | |
7e7a35fb TT |
51 | unsigned version = read_2_bytes (abfd, info_ptr); |
52 | if (version < 2 || version > 5) | |
4057dfde TT |
53 | error (_("Dwarf Error: wrong version in compilation unit header " |
54 | "(is %d, should be 2, 3, 4 or 5) [in module %s]"), | |
7e7a35fb TT |
55 | version, filename); |
56 | cu_header->version = version; | |
4057dfde TT |
57 | info_ptr += 2; |
58 | if (cu_header->version < 5) | |
59 | switch (section_kind) | |
60 | { | |
61 | case rcuh_kind::COMPILE: | |
62 | cu_header->unit_type = DW_UT_compile; | |
63 | break; | |
64 | case rcuh_kind::TYPE: | |
65 | cu_header->unit_type = DW_UT_type; | |
66 | break; | |
67 | default: | |
68 | internal_error (__FILE__, __LINE__, | |
69 | _("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: 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); | |
87 | break; | |
88 | case DW_UT_type: | |
89 | case DW_UT_split_type: | |
90 | section_kind = rcuh_kind::TYPE; | |
91 | break; | |
92 | default: | |
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); | |
101 | } | |
102 | ||
103 | cu_header->addr_size = read_1_byte (abfd, info_ptr); | |
104 | info_ptr += 1; | |
105 | } | |
8266302d TT |
106 | cu_header->abbrev_sect_off |
107 | = (sect_offset) cu_header->read_offset (abfd, info_ptr, &bytes_read); | |
4057dfde TT |
108 | info_ptr += bytes_read; |
109 | if (cu_header->version < 5) | |
110 | { | |
111 | cu_header->addr_size = read_1_byte (abfd, info_ptr); | |
112 | info_ptr += 1; | |
113 | } | |
114 | signed_addr = bfd_get_sign_extend_vma (abfd); | |
115 | if (signed_addr < 0) | |
116 | internal_error (__FILE__, __LINE__, | |
117 | _("read_comp_unit_head: dwarf from non elf file")); | |
118 | cu_header->signed_addr_p = signed_addr; | |
119 | ||
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; | |
123 | ||
124 | if (header_has_signature) | |
125 | { | |
126 | cu_header->signature = read_8_bytes (abfd, info_ptr); | |
127 | info_ptr += 8; | |
128 | } | |
129 | ||
130 | if (section_kind == rcuh_kind::TYPE) | |
131 | { | |
132 | LONGEST type_offset; | |
8266302d | 133 | type_offset = cu_header->read_offset (abfd, info_ptr, &bytes_read); |
4057dfde TT |
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), | |
139 | filename); | |
140 | } | |
141 | ||
142 | return info_ptr; | |
143 | } | |
144 | ||
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. */ | |
148 | ||
149 | static void | |
976ca316 | 150 | error_check_comp_unit_head (dwarf2_per_objfile *per_objfile, |
4057dfde TT |
151 | struct comp_unit_head *header, |
152 | struct dwarf2_section_info *section, | |
153 | struct dwarf2_section_info *abbrev_section) | |
154 | { | |
155 | const char *filename = section->get_file_name (); | |
156 | ||
157 | if (to_underlying (header->abbrev_sect_off) | |
976ca316 | 158 | >= abbrev_section->get_size (per_objfile->objfile)) |
4057dfde TT |
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), | |
163 | filename); | |
164 | ||
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 ()) | |
168 | > section->size) | |
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), | |
172 | filename); | |
173 | } | |
174 | ||
cd53fa40 | 175 | /* See comp-unit-head.h. */ |
4057dfde TT |
176 | |
177 | const gdb_byte * | |
976ca316 | 178 | read_and_check_comp_unit_head (dwarf2_per_objfile *per_objfile, |
4057dfde TT |
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) | |
184 | { | |
185 | const gdb_byte *beg_of_comp_unit = info_ptr; | |
186 | ||
187 | header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer); | |
188 | ||
189 | info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind); | |
190 | ||
191 | header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit); | |
192 | ||
976ca316 | 193 | error_check_comp_unit_head (per_objfile, header, section, abbrev_section); |
4057dfde TT |
194 | |
195 | return info_ptr; | |
196 | } | |
c8a7a66f TT |
197 | |
198 | CORE_ADDR | |
199 | comp_unit_head::read_address (bfd *abfd, const gdb_byte *buf, | |
200 | unsigned int *bytes_read) const | |
201 | { | |
202 | CORE_ADDR retval = 0; | |
203 | ||
204 | if (signed_addr_p) | |
205 | { | |
206 | switch (addr_size) | |
207 | { | |
208 | case 2: | |
209 | retval = bfd_get_signed_16 (abfd, buf); | |
210 | break; | |
211 | case 4: | |
212 | retval = bfd_get_signed_32 (abfd, buf); | |
213 | break; | |
214 | case 8: | |
215 | retval = bfd_get_signed_64 (abfd, buf); | |
216 | break; | |
217 | default: | |
218 | internal_error (__FILE__, __LINE__, | |
219 | _("read_address: bad switch, signed [in module %s]"), | |
220 | bfd_get_filename (abfd)); | |
221 | } | |
222 | } | |
223 | else | |
224 | { | |
225 | switch (addr_size) | |
226 | { | |
227 | case 2: | |
228 | retval = bfd_get_16 (abfd, buf); | |
229 | break; | |
230 | case 4: | |
231 | retval = bfd_get_32 (abfd, buf); | |
232 | break; | |
233 | case 8: | |
234 | retval = bfd_get_64 (abfd, buf); | |
235 | break; | |
236 | default: | |
237 | internal_error (__FILE__, __LINE__, | |
238 | _("read_address: bad switch, " | |
239 | "unsigned [in module %s]"), | |
240 | bfd_get_filename (abfd)); | |
241 | } | |
242 | } | |
243 | ||
244 | *bytes_read = addr_size; | |
245 | return retval; | |
246 | } |