1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2020 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/>. */
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"
29 line_header::add_include_dir (const char *include_dir
)
31 if (dwarf_line_debug
>= 2)
35 new_size
= m_include_dirs
.size ();
37 new_size
= m_include_dirs
.size () + 1;
38 fprintf_unfiltered (gdb_stdlog
, "Adding dir %zu: %s\n",
39 new_size
, include_dir
);
41 m_include_dirs
.push_back (include_dir
);
45 line_header::add_file_name (const char *name
,
47 unsigned int mod_time
,
50 if (dwarf_line_debug
>= 2)
54 new_size
= file_names_size ();
56 new_size
= file_names_size () + 1;
57 fprintf_unfiltered (gdb_stdlog
, "Adding file %zu: %s\n",
60 m_file_names
.emplace_back (name
, d_index
, mod_time
, length
);
63 gdb::unique_xmalloc_ptr
<char>
64 line_header::file_file_name (int file
) const
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
))
70 const file_entry
*fe
= file_name_at (file
);
72 if (!IS_ABSOLUTE_PATH (fe
->name
))
74 const char *dir
= fe
->include_dir (this);
76 return gdb::unique_xmalloc_ptr
<char> (concat (dir
, SLASH_STRING
,
80 return make_unique_xstrdup (fe
->name
);
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. */
89 xsnprintf (fake_name
, sizeof (fake_name
),
90 "<bad macro file number %d>", file
);
92 complaint (_("bad file number in macro information (%d)"),
95 return make_unique_xstrdup (fake_name
);
99 gdb::unique_xmalloc_ptr
<char>
100 line_header::file_full_name (int file
, const char *comp_dir
) const
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
))
106 gdb::unique_xmalloc_ptr
<char> relative
= file_file_name (file
);
108 if (IS_ABSOLUTE_PATH (relative
.get ()) || comp_dir
== NULL
)
110 return gdb::unique_xmalloc_ptr
<char> (concat (comp_dir
, SLASH_STRING
,
115 return file_file_name (file
);
119 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
121 complaint (_("statement list doesn't fit in .debug_line section"));
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
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. */
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
)
138 LONGEST length
= read_initial_length (abfd
, buf
, bytes_read
);
140 gdb_assert (cu_header
->initial_length_size
== 4
141 || cu_header
->initial_length_size
== 8
142 || cu_header
->initial_length_size
== 12);
144 if (cu_header
->initial_length_size
!= *bytes_read
)
145 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
147 *offset_size
= (*bytes_read
== 4) ? 4 : 8;
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
157 read_formatted_entries (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
158 const gdb_byte
**bufp
, struct line_header
*lh
,
159 const struct comp_unit_head
*cu_header
,
160 void (*callback
) (struct line_header
*lh
,
163 unsigned int mod_time
,
164 unsigned int length
))
166 gdb_byte format_count
, formati
;
167 ULONGEST data_count
, datai
;
168 const gdb_byte
*buf
= *bufp
;
169 const gdb_byte
*format_header_data
;
170 unsigned int bytes_read
;
172 format_count
= read_1_byte (abfd
, buf
);
174 format_header_data
= buf
;
175 for (formati
= 0; formati
< format_count
; formati
++)
177 read_unsigned_leb128 (abfd
, buf
, &bytes_read
);
179 read_unsigned_leb128 (abfd
, buf
, &bytes_read
);
183 data_count
= read_unsigned_leb128 (abfd
, buf
, &bytes_read
);
185 for (datai
= 0; datai
< data_count
; datai
++)
187 const gdb_byte
*format
= format_header_data
;
188 struct file_entry fe
;
190 for (formati
= 0; formati
< format_count
; formati
++)
192 ULONGEST content_type
= read_unsigned_leb128 (abfd
, format
, &bytes_read
);
193 format
+= bytes_read
;
195 ULONGEST form
= read_unsigned_leb128 (abfd
, format
, &bytes_read
);
196 format
+= bytes_read
;
198 gdb::optional
<const char *> string
;
199 gdb::optional
<unsigned int> uint
;
204 string
.emplace (read_direct_string (abfd
, buf
, &bytes_read
));
208 case DW_FORM_line_strp
:
210 (per_objfile
->read_line_string (buf
, cu_header
, &bytes_read
));
215 uint
.emplace (read_1_byte (abfd
, buf
));
220 uint
.emplace (read_2_bytes (abfd
, buf
));
225 uint
.emplace (read_4_bytes (abfd
, buf
));
230 uint
.emplace (read_8_bytes (abfd
, buf
));
235 /* This is used for MD5, but file_entry does not record MD5s. */
240 uint
.emplace (read_unsigned_leb128 (abfd
, buf
, &bytes_read
));
245 /* It is valid only for DW_LNCT_timestamp which is ignored by
250 switch (content_type
)
253 if (string
.has_value ())
256 case DW_LNCT_directory_index
:
257 if (uint
.has_value ())
258 fe
.d_index
= (dir_index
) *uint
;
260 case DW_LNCT_timestamp
:
261 if (uint
.has_value ())
265 if (uint
.has_value ())
271 complaint (_("Unknown format content type %s"),
272 pulongest (content_type
));
276 callback (lh
, fe
.name
, fe
.d_index
, fe
.mod_time
, fe
.length
);
282 /* See line-header.h. */
285 dwarf_decode_line_header (sect_offset sect_off
, bool is_dwz
,
286 dwarf2_per_objfile
*per_objfile
,
287 struct dwarf2_section_info
*section
,
288 const struct comp_unit_head
*cu_header
)
290 const gdb_byte
*line_ptr
;
291 unsigned int bytes_read
, offset_size
;
293 const char *cur_dir
, *cur_file
;
295 bfd
*abfd
= section
->get_bfd_owner ();
297 /* Make sure that at least there's room for the total_length field.
298 That could be 12 bytes long, but we're just going to fudge that. */
299 if (to_underlying (sect_off
) + 4 >= section
->size
)
301 dwarf2_statement_list_fits_in_line_number_section_complaint ();
305 line_header_up
lh (new line_header ());
307 lh
->sect_off
= sect_off
;
308 lh
->offset_in_dwz
= is_dwz
;
310 line_ptr
= section
->buffer
+ to_underlying (sect_off
);
312 /* Read in the header. */
314 read_checked_initial_length_and_offset (abfd
, line_ptr
, cu_header
,
315 &bytes_read
, &offset_size
);
316 line_ptr
+= bytes_read
;
318 const gdb_byte
*start_here
= line_ptr
;
320 if (line_ptr
+ lh
->total_length
> (section
->buffer
+ section
->size
))
322 dwarf2_statement_list_fits_in_line_number_section_complaint ();
325 lh
->statement_program_end
= start_here
+ lh
->total_length
;
326 lh
->version
= read_2_bytes (abfd
, line_ptr
);
330 /* This is a version we don't understand. The format could have
331 changed in ways we don't handle properly so just punt. */
332 complaint (_("unsupported version in .debug_line section"));
335 if (lh
->version
>= 5)
337 gdb_byte segment_selector_size
;
339 /* Skip address size. */
340 read_1_byte (abfd
, line_ptr
);
343 segment_selector_size
= read_1_byte (abfd
, line_ptr
);
345 if (segment_selector_size
!= 0)
347 complaint (_("unsupported segment selector size %u "
348 "in .debug_line section"),
349 segment_selector_size
);
353 lh
->header_length
= read_offset (abfd
, line_ptr
, offset_size
);
354 line_ptr
+= offset_size
;
355 lh
->statement_program_start
= line_ptr
+ lh
->header_length
;
356 lh
->minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
358 if (lh
->version
>= 4)
360 lh
->maximum_ops_per_instruction
= read_1_byte (abfd
, line_ptr
);
364 lh
->maximum_ops_per_instruction
= 1;
366 if (lh
->maximum_ops_per_instruction
== 0)
368 lh
->maximum_ops_per_instruction
= 1;
369 complaint (_("invalid maximum_ops_per_instruction "
370 "in `.debug_line' section"));
373 lh
->default_is_stmt
= read_1_byte (abfd
, line_ptr
);
375 lh
->line_base
= read_1_signed_byte (abfd
, line_ptr
);
377 lh
->line_range
= read_1_byte (abfd
, line_ptr
);
379 lh
->opcode_base
= read_1_byte (abfd
, line_ptr
);
381 lh
->standard_opcode_lengths
.reset (new unsigned char[lh
->opcode_base
]);
383 lh
->standard_opcode_lengths
[0] = 1; /* This should never be used anyway. */
384 for (i
= 1; i
< lh
->opcode_base
; ++i
)
386 lh
->standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
390 if (lh
->version
>= 5)
392 /* Read directory table. */
393 read_formatted_entries (per_objfile
, abfd
, &line_ptr
, lh
.get (),
395 [] (struct line_header
*header
, const char *name
,
396 dir_index d_index
, unsigned int mod_time
,
399 header
->add_include_dir (name
);
402 /* Read file name table. */
403 read_formatted_entries (per_objfile
, abfd
, &line_ptr
, lh
.get (),
405 [] (struct line_header
*header
, const char *name
,
406 dir_index d_index
, unsigned int mod_time
,
409 header
->add_file_name (name
, d_index
, mod_time
, length
);
414 /* Read directory table. */
415 while ((cur_dir
= read_direct_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
417 line_ptr
+= bytes_read
;
418 lh
->add_include_dir (cur_dir
);
420 line_ptr
+= bytes_read
;
422 /* Read file name table. */
423 while ((cur_file
= read_direct_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
425 unsigned int mod_time
, length
;
428 line_ptr
+= bytes_read
;
429 d_index
= (dir_index
) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
430 line_ptr
+= bytes_read
;
431 mod_time
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
432 line_ptr
+= bytes_read
;
433 length
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
434 line_ptr
+= bytes_read
;
436 lh
->add_file_name (cur_file
, d_index
, mod_time
, length
);
438 line_ptr
+= bytes_read
;
441 if (line_ptr
> (section
->buffer
+ section
->size
))
442 complaint (_("line number info header doesn't "
443 "fit in `.debug_line' section"));