Automatic date update in version.in
[binutils-gdb.git] / gdb / dwarf2 / line-header.c
blobeddb2ef7ae801e38a1e816514d165db170cff8e2
1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "dwarf2/comp-unit-head.h"
21 #include "dwarf2/leb.h"
22 #include "dwarf2/line-header.h"
23 #include "dwarf2/read.h"
24 #include "complaints.h"
25 #include "filenames.h"
26 #include "gdbsupport/pathstuff.h"
28 void
29 line_header::add_include_dir (const char *include_dir)
31 if (dwarf_line_debug >= 2)
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 gdb_printf (gdb_stdlog, "Adding dir %zu: %s\n",
39 new_size, include_dir);
41 m_include_dirs.push_back (include_dir);
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)
50 file_name_index index
51 = version >= 5 ? file_names_size (): file_names_size () + 1;
53 if (dwarf_line_debug >= 2)
54 gdb_printf (gdb_stdlog, "Adding file %d: %s\n", index, name);
56 m_file_names.emplace_back (name, index, d_index, mod_time, length);
59 std::string
60 line_header::file_file_name (const file_entry &fe) const
62 gdb_assert (is_valid_file_index (fe.index));
64 std::string ret = fe.name;
66 if (IS_ABSOLUTE_PATH (ret))
67 return ret;
69 const char *dir = fe.include_dir (this);
70 if (dir != nullptr)
71 ret = path_join (dir, ret.c_str ());
73 if (IS_ABSOLUTE_PATH (ret))
74 return ret;
76 if (m_comp_dir != nullptr)
77 ret = path_join (m_comp_dir, ret.c_str ());
79 return ret;
82 static void
83 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
85 complaint (_("statement list doesn't fit in .debug_line section"));
88 /* Cover function for read_initial_length.
89 Returns the length of the object at BUF, and stores the size of the
90 initial length in *BYTES_READ and stores the size that offsets will be in
91 *OFFSET_SIZE.
92 If the initial length size is not equivalent to that specified in
93 CU_HEADER then issue a complaint.
94 This is useful when reading non-comp-unit headers. */
96 static LONGEST
97 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
98 const struct comp_unit_head *cu_header,
99 unsigned int *bytes_read,
100 unsigned int *offset_size)
102 LONGEST length = read_initial_length (abfd, buf, bytes_read);
104 gdb_assert (cu_header->initial_length_size == 4
105 || cu_header->initial_length_size == 8
106 || cu_header->initial_length_size == 12);
108 if (cu_header->initial_length_size != *bytes_read)
109 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
111 *offset_size = (*bytes_read == 4) ? 4 : 8;
112 return length;
115 /* Read directory or file name entry format, starting with byte of
116 format count entries, ULEB128 pairs of entry formats, ULEB128 of
117 entries count and the entries themselves in the described entry
118 format. */
120 static void
121 read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd,
122 const gdb_byte **bufp, struct line_header *lh,
123 unsigned int offset_size,
124 void (*callback) (struct line_header *lh,
125 const char *name,
126 dir_index d_index,
127 unsigned int mod_time,
128 unsigned int length))
130 gdb_byte format_count, formati;
131 ULONGEST data_count, datai;
132 const gdb_byte *buf = *bufp;
133 const gdb_byte *format_header_data;
134 unsigned int bytes_read;
136 format_count = read_1_byte (abfd, buf);
137 buf += 1;
138 format_header_data = buf;
139 for (formati = 0; formati < format_count; formati++)
141 read_unsigned_leb128 (abfd, buf, &bytes_read);
142 buf += bytes_read;
143 read_unsigned_leb128 (abfd, buf, &bytes_read);
144 buf += bytes_read;
147 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
148 buf += bytes_read;
149 for (datai = 0; datai < data_count; datai++)
151 const gdb_byte *format = format_header_data;
152 struct file_entry fe;
154 for (formati = 0; formati < format_count; formati++)
156 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
157 format += bytes_read;
159 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
160 format += bytes_read;
162 std::optional<const char *> string;
163 std::optional<unsigned int> uint;
165 switch (form)
167 case DW_FORM_string:
168 string.emplace (read_direct_string (abfd, buf, &bytes_read));
169 buf += bytes_read;
170 break;
172 case DW_FORM_line_strp:
174 const char *str
175 = per_objfile->read_line_string (buf, offset_size);
176 string.emplace (str);
177 buf += offset_size;
179 break;
181 case DW_FORM_data1:
182 uint.emplace (read_1_byte (abfd, buf));
183 buf += 1;
184 break;
186 case DW_FORM_data2:
187 uint.emplace (read_2_bytes (abfd, buf));
188 buf += 2;
189 break;
191 case DW_FORM_data4:
192 uint.emplace (read_4_bytes (abfd, buf));
193 buf += 4;
194 break;
196 case DW_FORM_data8:
197 uint.emplace (read_8_bytes (abfd, buf));
198 buf += 8;
199 break;
201 case DW_FORM_data16:
202 /* This is used for MD5, but file_entry does not record MD5s. */
203 buf += 16;
204 break;
206 case DW_FORM_udata:
207 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
208 buf += bytes_read;
209 break;
211 case DW_FORM_block:
212 /* It is valid only for DW_LNCT_timestamp which is ignored by
213 current GDB. */
214 break;
217 /* Normalize nullptr string. */
218 if (string.has_value () && *string == nullptr)
219 string.emplace ("");
221 switch (content_type)
223 case DW_LNCT_path:
224 if (string.has_value ())
225 fe.name = *string;
226 break;
227 case DW_LNCT_directory_index:
228 if (uint.has_value ())
229 fe.d_index = (dir_index) *uint;
230 break;
231 case DW_LNCT_timestamp:
232 if (uint.has_value ())
233 fe.mod_time = *uint;
234 break;
235 case DW_LNCT_size:
236 if (uint.has_value ())
237 fe.length = *uint;
238 break;
239 case DW_LNCT_MD5:
240 break;
241 default:
242 complaint (_("Unknown format content type %s"),
243 pulongest (content_type));
247 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
250 *bufp = buf;
253 /* See line-header.h. */
255 line_header_up
256 dwarf_decode_line_header (sect_offset sect_off, bool is_dwz,
257 dwarf2_per_objfile *per_objfile,
258 struct dwarf2_section_info *section,
259 const struct comp_unit_head *cu_header,
260 const char *comp_dir)
262 const gdb_byte *line_ptr;
263 unsigned int bytes_read, offset_size;
264 int i;
265 const char *cur_dir, *cur_file;
267 bfd *abfd = section->get_bfd_owner ();
269 /* Make sure that at least there's room for the total_length field.
270 That could be 12 bytes long, but we're just going to fudge that. */
271 if (to_underlying (sect_off) + 4 >= section->size)
273 dwarf2_statement_list_fits_in_line_number_section_complaint ();
274 return 0;
277 line_header_up lh (new line_header (comp_dir));
279 lh->sect_off = sect_off;
280 lh->offset_in_dwz = is_dwz;
282 line_ptr = section->buffer + to_underlying (sect_off);
284 /* Read in the header. */
285 LONGEST unit_length
286 = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header,
287 &bytes_read, &offset_size);
288 line_ptr += bytes_read;
290 const gdb_byte *start_here = line_ptr;
292 if (line_ptr + unit_length > (section->buffer + section->size))
294 dwarf2_statement_list_fits_in_line_number_section_complaint ();
295 return 0;
297 lh->statement_program_end = start_here + unit_length;
298 lh->version = read_2_bytes (abfd, line_ptr);
299 line_ptr += 2;
300 if (lh->version > 5)
302 /* This is a version we don't understand. The format could have
303 changed in ways we don't handle properly so just punt. */
304 complaint (_("unsupported version in .debug_line section"));
305 return NULL;
307 if (lh->version >= 5)
309 gdb_byte segment_selector_size;
311 /* Skip address size. */
312 read_1_byte (abfd, line_ptr);
313 line_ptr += 1;
315 segment_selector_size = read_1_byte (abfd, line_ptr);
316 line_ptr += 1;
317 if (segment_selector_size != 0)
319 complaint (_("unsupported segment selector size %u "
320 "in .debug_line section"),
321 segment_selector_size);
322 return NULL;
326 LONGEST header_length = read_offset (abfd, line_ptr, offset_size);
327 line_ptr += offset_size;
328 lh->statement_program_start = line_ptr + header_length;
329 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
330 line_ptr += 1;
332 if (lh->version >= 4)
334 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
335 line_ptr += 1;
337 else
338 lh->maximum_ops_per_instruction = 1;
340 if (lh->maximum_ops_per_instruction == 0)
342 lh->maximum_ops_per_instruction = 1;
343 complaint (_("invalid maximum_ops_per_instruction "
344 "in `.debug_line' section"));
347 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
348 line_ptr += 1;
349 lh->line_base = read_1_signed_byte (abfd, line_ptr);
350 line_ptr += 1;
351 lh->line_range = read_1_byte (abfd, line_ptr);
352 line_ptr += 1;
353 lh->opcode_base = read_1_byte (abfd, line_ptr);
354 line_ptr += 1;
355 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
357 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
358 for (i = 1; i < lh->opcode_base; ++i)
360 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
361 line_ptr += 1;
364 if (lh->version >= 5)
366 /* Read directory table. */
367 read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
368 offset_size,
369 [] (struct line_header *header, const char *name,
370 dir_index d_index, unsigned int mod_time,
371 unsigned int length)
373 header->add_include_dir (name);
376 /* Read file name table. */
377 read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (),
378 offset_size,
379 [] (struct line_header *header, const char *name,
380 dir_index d_index, unsigned int mod_time,
381 unsigned int length)
383 header->add_file_name (name, d_index, mod_time, length);
386 else
388 /* Read directory table. */
389 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
391 line_ptr += bytes_read;
392 lh->add_include_dir (cur_dir);
394 line_ptr += bytes_read;
396 /* Read file name table. */
397 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
399 unsigned int mod_time, length;
400 dir_index d_index;
402 line_ptr += bytes_read;
403 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
404 line_ptr += bytes_read;
405 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
406 line_ptr += bytes_read;
407 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
408 line_ptr += bytes_read;
410 lh->add_file_name (cur_file, d_index, mod_time, length);
412 line_ptr += bytes_read;
415 if (line_ptr > (section->buffer + section->size))
416 complaint (_("line number info header doesn't "
417 "fit in `.debug_line' section"));
419 return lh;