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