]>
Commit | Line | Data |
---|---|---|
8fdd972c TT |
1 | /* DWARF 2 debugging format support for GDB. |
2 | ||
4a94e368 | 3 | Copyright (C) 1994-2022 Free Software Foundation, Inc. |
8fdd972c TT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
cd53fa40 | 21 | #include "dwarf2/comp-unit-head.h" |
0df7ad3a | 22 | #include "dwarf2/leb.h" |
8fdd972c TT |
23 | #include "dwarf2/line-header.h" |
24 | #include "dwarf2/read.h" | |
25 | #include "complaints.h" | |
26 | #include "filenames.h" | |
ffaebc19 | 27 | #include "gdbsupport/pathstuff.h" |
8fdd972c TT |
28 | |
29 | void | |
30 | line_header::add_include_dir (const char *include_dir) | |
31 | { | |
32 | if (dwarf_line_debug >= 2) | |
33 | { | |
34 | size_t new_size; | |
35 | if (version >= 5) | |
dda83cd7 | 36 | new_size = m_include_dirs.size (); |
8fdd972c | 37 | else |
dda83cd7 | 38 | new_size = m_include_dirs.size () + 1; |
6cb06a8c TT |
39 | gdb_printf (gdb_stdlog, "Adding dir %zu: %s\n", |
40 | new_size, include_dir); | |
8fdd972c TT |
41 | } |
42 | m_include_dirs.push_back (include_dir); | |
43 | } | |
44 | ||
45 | void | |
46 | line_header::add_file_name (const char *name, | |
47 | dir_index d_index, | |
48 | unsigned int mod_time, | |
49 | unsigned int length) | |
50 | { | |
51 | if (dwarf_line_debug >= 2) | |
52 | { | |
53 | size_t new_size; | |
54 | if (version >= 5) | |
dda83cd7 | 55 | new_size = file_names_size (); |
8fdd972c | 56 | else |
dda83cd7 | 57 | new_size = file_names_size () + 1; |
6cb06a8c TT |
58 | gdb_printf (gdb_stdlog, "Adding file %zu: %s\n", |
59 | new_size, name); | |
8fdd972c TT |
60 | } |
61 | m_file_names.emplace_back (name, d_index, mod_time, length); | |
62 | } | |
63 | ||
d3a76a55 | 64 | std::string |
8844c11b | 65 | line_header::file_file_name (int file) const |
8fdd972c TT |
66 | { |
67 | /* Is the file number a valid index into the line header's file name | |
68 | table? Remember that file numbers start with one, not zero. */ | |
69 | if (is_valid_file_index (file)) | |
70 | { | |
71 | const file_entry *fe = file_name_at (file); | |
72 | ||
73 | if (!IS_ABSOLUTE_PATH (fe->name)) | |
74 | { | |
75 | const char *dir = fe->include_dir (this); | |
76 | if (dir != NULL) | |
ffaebc19 | 77 | return path_join (dir, fe->name); |
8fdd972c | 78 | } |
d3a76a55 SM |
79 | |
80 | return fe->name; | |
8fdd972c TT |
81 | } |
82 | else | |
83 | { | |
84 | /* The compiler produced a bogus file number. We can at least | |
dda83cd7 SM |
85 | record the macro definitions made in the file, even if we |
86 | won't be able to find the file by name. */ | |
8fdd972c | 87 | complaint (_("bad file number in macro information (%d)"), |
dda83cd7 | 88 | file); |
8fdd972c | 89 | |
d3a76a55 | 90 | return string_printf ("<bad macro file number %d>", file); |
8fdd972c TT |
91 | } |
92 | } | |
93 | ||
0df7ad3a TT |
94 | static void |
95 | dwarf2_statement_list_fits_in_line_number_section_complaint (void) | |
96 | { | |
97 | complaint (_("statement list doesn't fit in .debug_line section")); | |
98 | } | |
99 | ||
100 | /* Cover function for read_initial_length. | |
101 | Returns the length of the object at BUF, and stores the size of the | |
102 | initial length in *BYTES_READ and stores the size that offsets will be in | |
103 | *OFFSET_SIZE. | |
104 | If the initial length size is not equivalent to that specified in | |
105 | CU_HEADER then issue a complaint. | |
106 | This is useful when reading non-comp-unit headers. */ | |
107 | ||
108 | static LONGEST | |
109 | read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf, | |
110 | const struct comp_unit_head *cu_header, | |
111 | unsigned int *bytes_read, | |
112 | unsigned int *offset_size) | |
113 | { | |
114 | LONGEST length = read_initial_length (abfd, buf, bytes_read); | |
115 | ||
116 | gdb_assert (cu_header->initial_length_size == 4 | |
117 | || cu_header->initial_length_size == 8 | |
118 | || cu_header->initial_length_size == 12); | |
119 | ||
120 | if (cu_header->initial_length_size != *bytes_read) | |
121 | complaint (_("intermixed 32-bit and 64-bit DWARF sections")); | |
122 | ||
123 | *offset_size = (*bytes_read == 4) ? 4 : 8; | |
124 | return length; | |
125 | } | |
126 | ||
127 | /* Read directory or file name entry format, starting with byte of | |
128 | format count entries, ULEB128 pairs of entry formats, ULEB128 of | |
129 | entries count and the entries themselves in the described entry | |
130 | format. */ | |
131 | ||
132 | static void | |
976ca316 SM |
133 | read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd, |
134 | const gdb_byte **bufp, struct line_header *lh, | |
bab31d14 | 135 | unsigned int offset_size, |
0df7ad3a TT |
136 | void (*callback) (struct line_header *lh, |
137 | const char *name, | |
138 | dir_index d_index, | |
139 | unsigned int mod_time, | |
140 | unsigned int length)) | |
141 | { | |
142 | gdb_byte format_count, formati; | |
143 | ULONGEST data_count, datai; | |
144 | const gdb_byte *buf = *bufp; | |
145 | const gdb_byte *format_header_data; | |
146 | unsigned int bytes_read; | |
147 | ||
148 | format_count = read_1_byte (abfd, buf); | |
149 | buf += 1; | |
150 | format_header_data = buf; | |
151 | for (formati = 0; formati < format_count; formati++) | |
152 | { | |
153 | read_unsigned_leb128 (abfd, buf, &bytes_read); | |
154 | buf += bytes_read; | |
155 | read_unsigned_leb128 (abfd, buf, &bytes_read); | |
156 | buf += bytes_read; | |
157 | } | |
158 | ||
159 | data_count = read_unsigned_leb128 (abfd, buf, &bytes_read); | |
160 | buf += bytes_read; | |
161 | for (datai = 0; datai < data_count; datai++) | |
162 | { | |
163 | const gdb_byte *format = format_header_data; | |
164 | struct file_entry fe; | |
165 | ||
166 | for (formati = 0; formati < format_count; formati++) | |
167 | { | |
168 | ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read); | |
169 | format += bytes_read; | |
170 | ||
171 | ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read); | |
172 | format += bytes_read; | |
173 | ||
174 | gdb::optional<const char *> string; | |
175 | gdb::optional<unsigned int> uint; | |
176 | ||
177 | switch (form) | |
178 | { | |
179 | case DW_FORM_string: | |
180 | string.emplace (read_direct_string (abfd, buf, &bytes_read)); | |
181 | buf += bytes_read; | |
182 | break; | |
183 | ||
184 | case DW_FORM_line_strp: | |
bab31d14 TV |
185 | { |
186 | const char *str | |
187 | = per_objfile->read_line_string (buf, offset_size); | |
188 | string.emplace (str); | |
189 | buf += offset_size; | |
190 | } | |
0df7ad3a TT |
191 | break; |
192 | ||
193 | case DW_FORM_data1: | |
194 | uint.emplace (read_1_byte (abfd, buf)); | |
195 | buf += 1; | |
196 | break; | |
197 | ||
198 | case DW_FORM_data2: | |
199 | uint.emplace (read_2_bytes (abfd, buf)); | |
200 | buf += 2; | |
201 | break; | |
202 | ||
203 | case DW_FORM_data4: | |
204 | uint.emplace (read_4_bytes (abfd, buf)); | |
205 | buf += 4; | |
206 | break; | |
207 | ||
208 | case DW_FORM_data8: | |
209 | uint.emplace (read_8_bytes (abfd, buf)); | |
210 | buf += 8; | |
211 | break; | |
212 | ||
213 | case DW_FORM_data16: | |
214 | /* This is used for MD5, but file_entry does not record MD5s. */ | |
215 | buf += 16; | |
216 | break; | |
217 | ||
218 | case DW_FORM_udata: | |
219 | uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read)); | |
220 | buf += bytes_read; | |
221 | break; | |
222 | ||
223 | case DW_FORM_block: | |
224 | /* It is valid only for DW_LNCT_timestamp which is ignored by | |
225 | current GDB. */ | |
226 | break; | |
227 | } | |
228 | ||
229 | switch (content_type) | |
230 | { | |
231 | case DW_LNCT_path: | |
232 | if (string.has_value ()) | |
233 | fe.name = *string; | |
234 | break; | |
235 | case DW_LNCT_directory_index: | |
236 | if (uint.has_value ()) | |
237 | fe.d_index = (dir_index) *uint; | |
238 | break; | |
239 | case DW_LNCT_timestamp: | |
240 | if (uint.has_value ()) | |
241 | fe.mod_time = *uint; | |
242 | break; | |
243 | case DW_LNCT_size: | |
244 | if (uint.has_value ()) | |
245 | fe.length = *uint; | |
246 | break; | |
247 | case DW_LNCT_MD5: | |
248 | break; | |
249 | default: | |
250 | complaint (_("Unknown format content type %s"), | |
251 | pulongest (content_type)); | |
252 | } | |
253 | } | |
254 | ||
255 | callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length); | |
256 | } | |
257 | ||
258 | *bufp = buf; | |
259 | } | |
260 | ||
261 | /* See line-header.h. */ | |
262 | ||
263 | line_header_up | |
264 | dwarf_decode_line_header (sect_offset sect_off, bool is_dwz, | |
976ca316 | 265 | dwarf2_per_objfile *per_objfile, |
0df7ad3a TT |
266 | struct dwarf2_section_info *section, |
267 | const struct comp_unit_head *cu_header) | |
268 | { | |
269 | const gdb_byte *line_ptr; | |
270 | unsigned int bytes_read, offset_size; | |
271 | int i; | |
272 | const char *cur_dir, *cur_file; | |
273 | ||
274 | bfd *abfd = section->get_bfd_owner (); | |
275 | ||
276 | /* Make sure that at least there's room for the total_length field. | |
277 | That could be 12 bytes long, but we're just going to fudge that. */ | |
278 | if (to_underlying (sect_off) + 4 >= section->size) | |
279 | { | |
280 | dwarf2_statement_list_fits_in_line_number_section_complaint (); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | line_header_up lh (new line_header ()); | |
285 | ||
286 | lh->sect_off = sect_off; | |
287 | lh->offset_in_dwz = is_dwz; | |
288 | ||
289 | line_ptr = section->buffer + to_underlying (sect_off); | |
290 | ||
291 | /* Read in the header. */ | |
ebf58f8e SM |
292 | LONGEST unit_length |
293 | = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header, | |
294 | &bytes_read, &offset_size); | |
0df7ad3a TT |
295 | line_ptr += bytes_read; |
296 | ||
297 | const gdb_byte *start_here = line_ptr; | |
298 | ||
ebf58f8e | 299 | if (line_ptr + unit_length > (section->buffer + section->size)) |
0df7ad3a TT |
300 | { |
301 | dwarf2_statement_list_fits_in_line_number_section_complaint (); | |
302 | return 0; | |
303 | } | |
ebf58f8e | 304 | lh->statement_program_end = start_here + unit_length; |
0df7ad3a TT |
305 | lh->version = read_2_bytes (abfd, line_ptr); |
306 | line_ptr += 2; | |
307 | if (lh->version > 5) | |
308 | { | |
309 | /* This is a version we don't understand. The format could have | |
310 | changed in ways we don't handle properly so just punt. */ | |
311 | complaint (_("unsupported version in .debug_line section")); | |
312 | return NULL; | |
313 | } | |
314 | if (lh->version >= 5) | |
315 | { | |
316 | gdb_byte segment_selector_size; | |
317 | ||
318 | /* Skip address size. */ | |
319 | read_1_byte (abfd, line_ptr); | |
320 | line_ptr += 1; | |
321 | ||
322 | segment_selector_size = read_1_byte (abfd, line_ptr); | |
323 | line_ptr += 1; | |
324 | if (segment_selector_size != 0) | |
325 | { | |
326 | complaint (_("unsupported segment selector size %u " | |
327 | "in .debug_line section"), | |
328 | segment_selector_size); | |
329 | return NULL; | |
330 | } | |
331 | } | |
332 | lh->header_length = read_offset (abfd, line_ptr, offset_size); | |
333 | line_ptr += offset_size; | |
334 | lh->statement_program_start = line_ptr + lh->header_length; | |
335 | lh->minimum_instruction_length = read_1_byte (abfd, line_ptr); | |
336 | line_ptr += 1; | |
337 | if (lh->version >= 4) | |
338 | { | |
339 | lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr); | |
340 | line_ptr += 1; | |
341 | } | |
342 | else | |
343 | lh->maximum_ops_per_instruction = 1; | |
344 | ||
345 | if (lh->maximum_ops_per_instruction == 0) | |
346 | { | |
347 | lh->maximum_ops_per_instruction = 1; | |
348 | complaint (_("invalid maximum_ops_per_instruction " | |
349 | "in `.debug_line' section")); | |
350 | } | |
351 | ||
352 | lh->default_is_stmt = read_1_byte (abfd, line_ptr); | |
353 | line_ptr += 1; | |
354 | lh->line_base = read_1_signed_byte (abfd, line_ptr); | |
355 | line_ptr += 1; | |
356 | lh->line_range = read_1_byte (abfd, line_ptr); | |
357 | line_ptr += 1; | |
358 | lh->opcode_base = read_1_byte (abfd, line_ptr); | |
359 | line_ptr += 1; | |
360 | lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]); | |
361 | ||
362 | lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */ | |
363 | for (i = 1; i < lh->opcode_base; ++i) | |
364 | { | |
365 | lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); | |
366 | line_ptr += 1; | |
367 | } | |
368 | ||
369 | if (lh->version >= 5) | |
370 | { | |
371 | /* Read directory table. */ | |
976ca316 | 372 | read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (), |
bab31d14 | 373 | offset_size, |
0df7ad3a TT |
374 | [] (struct line_header *header, const char *name, |
375 | dir_index d_index, unsigned int mod_time, | |
376 | unsigned int length) | |
377 | { | |
378 | header->add_include_dir (name); | |
379 | }); | |
380 | ||
381 | /* Read file name table. */ | |
976ca316 | 382 | read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (), |
bab31d14 | 383 | offset_size, |
0df7ad3a TT |
384 | [] (struct line_header *header, const char *name, |
385 | dir_index d_index, unsigned int mod_time, | |
386 | unsigned int length) | |
387 | { | |
388 | header->add_file_name (name, d_index, mod_time, length); | |
389 | }); | |
390 | } | |
391 | else | |
392 | { | |
393 | /* Read directory table. */ | |
394 | while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL) | |
395 | { | |
396 | line_ptr += bytes_read; | |
397 | lh->add_include_dir (cur_dir); | |
398 | } | |
399 | line_ptr += bytes_read; | |
400 | ||
401 | /* Read file name table. */ | |
402 | while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL) | |
403 | { | |
404 | unsigned int mod_time, length; | |
405 | dir_index d_index; | |
406 | ||
407 | line_ptr += bytes_read; | |
408 | d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
409 | line_ptr += bytes_read; | |
410 | mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
411 | line_ptr += bytes_read; | |
412 | length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
413 | line_ptr += bytes_read; | |
414 | ||
415 | lh->add_file_name (cur_file, d_index, mod_time, length); | |
416 | } | |
417 | line_ptr += bytes_read; | |
418 | } | |
419 | ||
420 | if (line_ptr > (section->buffer + section->size)) | |
421 | complaint (_("line number info header doesn't " | |
422 | "fit in `.debug_line' section")); | |
423 | ||
424 | return lh; | |
425 | } |