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