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