1 /* Reading code for .debug_names
3 Copyright (C) 2023-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 "read-debug-names.h"
21 #include "dwarf2/aranges.h"
22 #include "dwarf2/cooked-index.h"
24 #include "complaints.h"
25 #include "cp-support.h"
27 #include "mapped-index.h"
29 #include "stringify.h"
30 #include "extract-store-integer.h"
32 /* This is just like cooked_index_functions, but overrides a single
33 method so the test suite can distinguish the .debug_names case from
35 struct dwarf2_debug_names_index
: public cooked_index_functions
37 /* This dumps minimal information about .debug_names. It is called
38 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
39 uses this to verify that .debug_names has been loaded. */
40 void dump (struct objfile
*objfile
) override
42 gdb_printf (".debug_names: exists\n");
43 /* This could call the superclass method if that's useful. */
47 /* This is like a cooked index, but as it has been ingested from
48 .debug_names, it can't be used to write out an index. */
49 class debug_names_index
: public cooked_index
53 using cooked_index::cooked_index
;
55 cooked_index
*index_for_writing () override
58 quick_symbol_functions_up
make_quick_functions () const override
59 { return quick_symbol_functions_up (new dwarf2_debug_names_index
); }
62 /* A description of the mapped .debug_names. */
64 struct mapped_debug_names_reader
66 const gdb_byte
*scan_one_entry (const char *name
,
67 const gdb_byte
*entry
,
68 cooked_index_entry
**result
,
69 std::optional
<ULONGEST
> &parent
);
70 void scan_entries (uint32_t index
, const char *name
, const gdb_byte
*entry
);
71 void scan_all_names ();
73 dwarf2_per_objfile
*per_objfile
= nullptr;
75 bfd_endian dwarf5_byte_order
{};
76 bool dwarf5_is_dwarf64
= false;
77 bool augmentation_is_gdb
= false;
78 uint8_t offset_size
= 0;
79 uint32_t cu_count
= 0;
80 uint32_t tu_count
= 0, bucket_count
= 0, name_count
= 0;
81 const gdb_byte
*cu_table_reordered
= nullptr;
82 const gdb_byte
*tu_table_reordered
= nullptr;
83 const uint32_t *bucket_table_reordered
= nullptr;
84 const uint32_t *hash_table_reordered
= nullptr;
85 const gdb_byte
*name_table_string_offs_reordered
= nullptr;
86 const gdb_byte
*name_table_entry_offs_reordered
= nullptr;
87 const gdb_byte
*entry_pool
= nullptr;
94 /* Attribute name DW_IDX_*. */
97 /* Attribute form DW_FORM_*. */
100 /* Value if FORM is DW_FORM_implicit_const. */
101 LONGEST implicit_const
;
103 std::vector
<attr
> attr_vec
;
106 std::unordered_map
<ULONGEST
, index_val
> abbrev_map
;
108 std::unique_ptr
<cooked_index_shard
> shard
;
109 std::vector
<std::pair
<cooked_index_entry
*, ULONGEST
>> needs_parent
;
110 std::vector
<std::vector
<cooked_index_entry
*>> all_entries
;
113 /* Scan a single entry from the entries table. Set *RESULT and PARENT
114 (if needed) and return the updated pointer on success, or return
115 nullptr on error, or at the end of the table. */
118 mapped_debug_names_reader::scan_one_entry (const char *name
,
119 const gdb_byte
*entry
,
120 cooked_index_entry
**result
,
121 std::optional
<ULONGEST
> &parent
)
123 unsigned int bytes_read
;
124 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, entry
, &bytes_read
);
129 const auto indexval_it
= abbrev_map
.find (abbrev
);
130 if (indexval_it
== abbrev_map
.cend ())
132 complaint (_("Wrong .debug_names undefined abbrev code %s "
134 pulongest (abbrev
), bfd_get_filename (abfd
));
138 const auto &indexval
= indexval_it
->second
;
139 cooked_index_flag flags
= 0;
140 sect_offset die_offset
{};
141 enum language lang
= language_unknown
;
142 dwarf2_per_cu_data
*per_cu
= nullptr;
143 for (const auto &attr
: indexval
.attr_vec
)
148 case DW_FORM_implicit_const
:
149 ull
= attr
.implicit_const
;
151 case DW_FORM_flag_present
:
155 ull
= read_unsigned_leb128 (abfd
, entry
, &bytes_read
);
158 case DW_FORM_ref_addr
:
159 ull
= read_offset (abfd
, entry
, offset_size
);
160 entry
+= offset_size
;
163 ull
= read_4_bytes (abfd
, entry
);
167 ull
= read_8_bytes (abfd
, entry
);
170 case DW_FORM_ref_sig8
:
171 ull
= read_8_bytes (abfd
, entry
);
175 complaint (_("Unsupported .debug_names form %s [in module %s]"),
176 dwarf_form_name (attr
.form
),
177 bfd_get_filename (abfd
));
182 case DW_IDX_compile_unit
:
184 /* Don't crash on bad data. */
185 if (ull
>= per_objfile
->per_bfd
->all_comp_units
.size ())
187 complaint (_(".debug_names entry has bad CU index %s"
190 bfd_get_filename (abfd
));
194 per_cu
= per_objfile
->per_bfd
->get_cu (ull
);
196 case DW_IDX_type_unit
:
197 /* Don't crash on bad data. */
198 if (ull
>= per_objfile
->per_bfd
->all_type_units
.size ())
200 complaint (_(".debug_names entry has bad TU index %s"
203 bfd_get_filename (abfd
));
207 int nr_cus
= per_objfile
->per_bfd
->all_comp_units
.size ();
208 per_cu
= per_objfile
->per_bfd
->get_cu (nr_cus
+ ull
);
211 case DW_IDX_die_offset
:
212 die_offset
= sect_offset (ull
);
213 /* In a per-CU index (as opposed to a per-module index), index
214 entries without CU attribute implicitly refer to the single CU. */
216 per_cu
= per_objfile
->per_bfd
->get_cu (0);
221 case DW_IDX_GNU_internal
:
222 if (augmentation_is_gdb
&& ull
!= 0)
225 case DW_IDX_GNU_main
:
226 if (augmentation_is_gdb
&& ull
!= 0)
229 case DW_IDX_GNU_language
:
230 if (augmentation_is_gdb
)
231 lang
= dwarf_lang_to_enum_language (ull
);
233 case DW_IDX_GNU_linkage_name
:
234 if (augmentation_is_gdb
&& ull
!= 0)
240 /* Skip if we couldn't find a valid CU/TU index. */
241 if (per_cu
!= nullptr)
242 *result
= shard
->add (die_offset
, (dwarf_tag
) indexval
.dwarf_tag
, flags
,
243 lang
, name
, nullptr, per_cu
);
247 /* Scan all the entries for NAME, at name slot INDEX. */
250 mapped_debug_names_reader::scan_entries (uint32_t index
,
252 const gdb_byte
*entry
)
254 std::vector
<cooked_index_entry
*> these_entries
;
258 std::optional
<ULONGEST
> parent
;
259 cooked_index_entry
*this_entry
;
260 entry
= scan_one_entry (name
, entry
, &this_entry
, parent
);
262 if (entry
== nullptr)
265 these_entries
.push_back (this_entry
);
266 if (parent
.has_value ())
267 needs_parent
.emplace_back (this_entry
, *parent
);
270 all_entries
[index
] = std::move (these_entries
);
273 /* Scan the name table and create all the entries. */
276 mapped_debug_names_reader::scan_all_names ()
278 all_entries
.resize (name_count
);
280 /* In the first pass, create all the entries. */
281 for (uint32_t i
= 0; i
< name_count
; ++i
)
283 const ULONGEST namei_string_offs
284 = extract_unsigned_integer ((name_table_string_offs_reordered
286 offset_size
, dwarf5_byte_order
);
287 const char *name
= read_indirect_string_at_offset (per_objfile
,
290 const ULONGEST namei_entry_offs
291 = extract_unsigned_integer ((name_table_entry_offs_reordered
293 offset_size
, dwarf5_byte_order
);
294 const gdb_byte
*entry
= entry_pool
+ namei_entry_offs
;
296 scan_entries (i
, name
, entry
);
299 /* Now update the parent pointers for all entries. This has to be
300 done in a funny way because DWARF specifies the parent entry to
301 point to a name -- but we don't know which specific one. */
302 for (auto [entry
, parent_idx
] : needs_parent
)
304 /* Name entries are indexed from 1 in DWARF. */
305 std::vector
<cooked_index_entry
*> &entries
= all_entries
[parent_idx
- 1];
306 for (const auto &parent
: entries
)
307 if (parent
->lang
== entry
->lang
)
309 entry
->set_parent (parent
);
315 /* A reader for .debug_names. */
317 struct cooked_index_debug_names
: public cooked_index_worker
319 cooked_index_debug_names (dwarf2_per_objfile
*per_objfile
,
320 mapped_debug_names_reader
&&map
)
321 : cooked_index_worker (per_objfile
),
322 m_map (std::move (map
))
325 void do_reading () override
;
327 mapped_debug_names_reader m_map
;
331 cooked_index_debug_names::do_reading ()
333 complaint_interceptor complaint_handler
;
334 std::vector
<gdb_exception
> exceptions
;
337 m_map
.scan_all_names ();
339 catch (const gdb_exception
&exc
)
341 exceptions
.push_back (std::move (exc
));
344 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
345 per_bfd
->quick_file_names_table
346 = create_quick_file_names_table (per_bfd
->all_units
.size ());
347 m_results
.emplace_back (nullptr,
348 complaint_handler
.release (),
349 std::move (exceptions
),
351 std::vector
<std::unique_ptr
<cooked_index_shard
>> indexes
;
352 indexes
.push_back (std::move (m_map
.shard
));
354 = (gdb::checked_static_cast
<cooked_index
*>
355 (per_bfd
->index_table
.get ()));
356 /* Note that this code never uses IS_PARENT_DEFERRED, so it is safe
357 to pass nullptr here. */
358 table
->set_contents (std::move (indexes
), &m_warnings
, nullptr);
360 bfd_thread_cleanup ();
363 /* Check the signatured type hash table from .debug_names. */
366 check_signatured_type_table_from_debug_names
367 (dwarf2_per_objfile
*per_objfile
,
368 const mapped_debug_names_reader
&map
,
369 struct dwarf2_section_info
*section
)
371 struct objfile
*objfile
= per_objfile
->objfile
;
372 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
373 int nr_cus
= per_bfd
->all_comp_units
.size ();
374 int nr_cus_tus
= per_bfd
->all_units
.size ();
376 section
->read (objfile
);
379 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
382 = (sect_offset
) (extract_unsigned_integer
383 (map
.tu_table_reordered
+ i
* map
.offset_size
,
385 map
.dwarf5_byte_order
));
388 for (; j
< nr_cus_tus
; j
++)
389 if (per_bfd
->get_cu (j
)->sect_off
== sect_off
)
396 warning (_("Section .debug_names has incorrect entry in TU table,"
397 " ignoring .debug_names."));
400 per_bfd
->all_comp_units_index_tus
.push_back (per_bfd
->get_cu (j
));
405 /* DWARF-5 debug_names reader. */
407 /* The old, no-longer-supported GDB augmentation. */
408 static const gdb_byte old_gdb_augmentation
[]
409 = { 'G', 'D', 'B', 0 };
410 static_assert (sizeof (old_gdb_augmentation
) % 4 == 0);
412 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. This
413 must have a size that is a multiple of 4. */
414 const gdb_byte dwarf5_augmentation
[8] = { 'G', 'D', 'B', '2', 0, 0, 0, 0 };
415 static_assert (sizeof (dwarf5_augmentation
) % 4 == 0);
417 /* A helper function that reads the .debug_names section in SECTION
418 and fills in MAP. FILENAME is the name of the file containing the
419 section; it is used for error reporting.
421 Returns true if all went well, false otherwise. */
424 read_debug_names_from_section (dwarf2_per_objfile
*per_objfile
,
425 const char *filename
,
426 struct dwarf2_section_info
*section
,
427 mapped_debug_names_reader
&map
)
429 struct objfile
*objfile
= per_objfile
->objfile
;
431 if (section
->empty ())
434 /* Older elfutils strip versions could keep the section in the main
435 executable while splitting it for the separate debug info file. */
436 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
439 section
->read (objfile
);
441 map
.per_objfile
= per_objfile
;
442 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
444 const gdb_byte
*addr
= section
->buffer
;
446 bfd
*abfd
= section
->get_bfd_owner ();
449 unsigned int bytes_read
;
450 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
453 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
454 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
455 if (bytes_read
+ length
!= section
->size
)
457 /* There may be multiple per-CU indices. */
458 warning (_("Section .debug_names in %s length %s does not match "
459 "section length %s, ignoring .debug_names."),
460 filename
, plongest (bytes_read
+ length
),
461 pulongest (section
->size
));
465 /* The version number. */
466 uint16_t version
= read_2_bytes (abfd
, addr
);
470 warning (_("Section .debug_names in %s has unsupported version %d, "
471 "ignoring .debug_names."),
477 uint16_t padding
= read_2_bytes (abfd
, addr
);
481 warning (_("Section .debug_names in %s has unsupported padding %d, "
482 "ignoring .debug_names."),
487 /* comp_unit_count - The number of CUs in the CU list. */
488 map
.cu_count
= read_4_bytes (abfd
, addr
);
491 /* local_type_unit_count - The number of TUs in the local TU
493 map
.tu_count
= read_4_bytes (abfd
, addr
);
496 /* foreign_type_unit_count - The number of TUs in the foreign TU
498 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
500 if (foreign_tu_count
!= 0)
502 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
503 "ignoring .debug_names."),
504 filename
, static_cast<unsigned long> (foreign_tu_count
));
508 /* bucket_count - The number of hash buckets in the hash lookup
510 map
.bucket_count
= read_4_bytes (abfd
, addr
);
513 /* name_count - The number of unique names in the index. */
514 map
.name_count
= read_4_bytes (abfd
, addr
);
517 /* abbrev_table_size - The size in bytes of the abbreviations
519 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
522 /* augmentation_string_size - The size in bytes of the augmentation
523 string. This value is rounded up to a multiple of 4. */
524 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
526 augmentation_string_size
+= (-augmentation_string_size
) & 3;
528 if (augmentation_string_size
== sizeof (old_gdb_augmentation
)
529 && memcmp (addr
, old_gdb_augmentation
,
530 sizeof (old_gdb_augmentation
)) == 0)
532 warning (_(".debug_names created by an old version of gdb; ignoring"));
536 map
.augmentation_is_gdb
= ((augmentation_string_size
537 == sizeof (dwarf5_augmentation
))
538 && memcmp (addr
, dwarf5_augmentation
,
539 sizeof (dwarf5_augmentation
)) == 0);
541 if (!map
.augmentation_is_gdb
)
543 warning (_(".debug_names not created by gdb; ignoring"));
547 addr
+= augmentation_string_size
;
550 map
.cu_table_reordered
= addr
;
551 addr
+= map
.cu_count
* map
.offset_size
;
553 /* List of Local TUs */
554 map
.tu_table_reordered
= addr
;
555 addr
+= map
.tu_count
* map
.offset_size
;
557 /* Hash Lookup Table */
558 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
559 addr
+= map
.bucket_count
* 4;
560 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
561 if (map
.bucket_count
!= 0)
562 addr
+= map
.name_count
* 4;
565 map
.name_table_string_offs_reordered
= addr
;
566 addr
+= map
.name_count
* map
.offset_size
;
567 map
.name_table_entry_offs_reordered
= addr
;
568 addr
+= map
.name_count
* map
.offset_size
;
570 const gdb_byte
*abbrev_table_start
= addr
;
573 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
578 const auto insertpair
579 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names_reader::index_val ());
580 if (!insertpair
.second
)
582 warning (_("Section .debug_names in %s has duplicate index %s, "
583 "ignoring .debug_names."),
584 filename
, pulongest (index_num
));
587 mapped_debug_names_reader::index_val
&indexval
= insertpair
.first
->second
;
588 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
593 mapped_debug_names_reader::index_val::attr attr
;
594 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
596 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
598 if (attr
.form
== DW_FORM_implicit_const
)
600 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
604 if (attr
.dw_idx
== 0 && attr
.form
== 0)
606 indexval
.attr_vec
.push_back (std::move (attr
));
609 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
611 warning (_("Section .debug_names in %s has abbreviation_table "
612 "of size %s vs. written as %u, ignoring .debug_names."),
613 filename
, plongest (addr
- abbrev_table_start
),
617 map
.entry_pool
= addr
;
622 /* A helper for check_cus_from_debug_names that handles the MAP's CU
626 check_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
627 const mapped_debug_names_reader
&map
,
628 dwarf2_section_info
§ion
,
631 int nr_cus
= per_bfd
->all_comp_units
.size ();
633 if (!map
.augmentation_is_gdb
)
636 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
639 = (sect_offset
) (extract_unsigned_integer
640 (map
.cu_table_reordered
+ i
* map
.offset_size
,
642 map
.dwarf5_byte_order
));
644 for (; j
< nr_cus
; j
++)
645 if (per_bfd
->get_cu (j
)->sect_off
== sect_off
)
652 warning (_("Section .debug_names has incorrect entry in CU table,"
653 " ignoring .debug_names."));
656 per_bfd
->all_comp_units_index_cus
.push_back (per_bfd
->get_cu (j
));
661 if (map
.cu_count
!= nr_cus
)
663 warning (_("Section .debug_names has incorrect number of CUs in CU table,"
664 " ignoring .debug_names."));
668 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
671 = (sect_offset
) (extract_unsigned_integer
672 (map
.cu_table_reordered
+ i
* map
.offset_size
,
674 map
.dwarf5_byte_order
));
675 if (sect_off
!= per_bfd
->get_cu (i
)->sect_off
)
677 warning (_("Section .debug_names has incorrect entry in CU table,"
678 " ignoring .debug_names."));
686 /* Read the CU list from the mapped index, and use it to create all
687 the CU objects for this dwarf2_per_objfile. */
690 check_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
691 const mapped_debug_names_reader
&map
,
692 const mapped_debug_names_reader
&dwz_map
)
694 if (!check_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->infos
[0],
698 if (dwz_map
.cu_count
== 0)
701 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
702 return check_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
706 /* This does all the work for dwarf2_read_debug_names, but putting it
707 into a separate function makes some cleanup a bit simpler. */
710 do_dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
712 mapped_debug_names_reader map
;
713 mapped_debug_names_reader dwz_map
;
714 struct objfile
*objfile
= per_objfile
->objfile
;
715 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
717 if (!read_debug_names_from_section (per_objfile
, objfile_name (objfile
),
718 &per_bfd
->debug_names
, map
))
721 /* Don't use the index if it's empty. */
722 if (map
.name_count
== 0)
725 /* If there is a .dwz file, read it so we can get its CU list as
727 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
730 if (!read_debug_names_from_section (per_objfile
,
731 bfd_get_filename (dwz
->dwz_bfd
.get ()),
732 &dwz
->debug_names
, dwz_map
))
734 warning (_("could not read '.debug_names' section from %s; skipping"),
735 bfd_get_filename (dwz
->dwz_bfd
.get ()));
740 create_all_units (per_objfile
);
741 if (!check_cus_from_debug_names (per_bfd
, map
, dwz_map
))
744 if (map
.tu_count
!= 0)
746 /* We can only handle a single .debug_info and .debug_types when we have
748 if (per_bfd
->infos
.size () > 1
749 || per_bfd
->types
.size () > 1)
752 dwarf2_section_info
*section
753 = (per_bfd
->types
.size () == 1
755 : &per_bfd
->infos
[0]);
757 if (!check_signatured_type_table_from_debug_names (per_objfile
,
762 per_bfd
->debug_aranges
.read (per_objfile
->objfile
);
763 addrmap_mutable addrmap
;
764 deferred_warnings warnings
;
765 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
766 &addrmap
, &warnings
);
769 map
.shard
= std::make_unique
<cooked_index_shard
> ();
770 map
.shard
->install_addrmap (&addrmap
);
773 = new debug_names_index (per_objfile
,
774 (std::make_unique
<cooked_index_debug_names
>
775 (per_objfile
, std::move (map
))));
776 per_bfd
->index_table
.reset (idx
);
778 idx
->start_reading ();
783 /* See read-debug-names.h. */
786 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
788 bool result
= do_dwarf2_read_debug_names (per_objfile
);
790 per_objfile
->per_bfd
->all_units
.clear ();