1 // gdb-index.cc -- generate .gdb_index section for fast debug lookup
3 // Copyright (C) 2012-2025 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
25 #include "gdb-index.h"
26 #include "dwarf_reader.h"
35 const int gdb_index_version
= 7;
37 // Sizes of various records in the .gdb_index section.
38 const int gdb_index_offset_size
= 4;
39 const int gdb_index_hdr_size
= 6 * gdb_index_offset_size
;
40 const int gdb_index_cu_size
= 16;
41 const int gdb_index_tu_size
= 24;
42 const int gdb_index_addr_size
= 16 + gdb_index_offset_size
;
43 const int gdb_index_sym_size
= 2 * gdb_index_offset_size
;
45 // This class manages the hashed symbol table for the .gdb_index section.
46 // It is essentially equivalent to the hashtab implementation in libiberty,
47 // but is copied into gdb sources and here for compatibility because its
48 // data structure is exposed on disk.
55 : size_(0), capacity_(0), hashtab_(NULL
)
60 for (size_t i
= 0; i
< this->capacity_
; ++i
)
61 if (this->hashtab_
[i
] != NULL
)
62 delete this->hashtab_
[i
];
63 delete[] this->hashtab_
;
70 // Resize the hash table if necessary.
71 if (4 * this->size_
/ 3 >= this->capacity_
)
74 T
** slot
= this->find_slot(symbol
);
84 // Return the current size.
87 { return this->size_
; }
89 // Return the current capacity.
92 { return this->capacity_
; }
94 // Return the contents of slot N.
97 { return this->hashtab_
[n
]; }
100 // Find a symbol in the hash table, or return an empty slot if
101 // the symbol is not in the table.
105 unsigned int index
= symbol
->hash() & (this->capacity_
- 1);
106 unsigned int step
= ((symbol
->hash() * 17) & (this->capacity_
- 1)) | 1;
110 if (this->hashtab_
[index
] == NULL
111 || this->hashtab_
[index
]->equal(symbol
))
112 return &this->hashtab_
[index
];
113 index
= (index
+ step
) & (this->capacity_
- 1);
117 // Expand the hash table.
121 if (this->capacity_
== 0)
123 // Allocate the hash table for the first time.
124 this->capacity_
= Gdb_hashtab::initial_size
;
125 this->hashtab_
= new T
*[this->capacity_
];
126 memset(this->hashtab_
, 0, this->capacity_
* sizeof(T
*));
130 // Expand and rehash.
131 unsigned int old_cap
= this->capacity_
;
132 T
** old_hashtab
= this->hashtab_
;
133 this->capacity_
*= 2;
134 this->hashtab_
= new T
*[this->capacity_
];
135 memset(this->hashtab_
, 0, this->capacity_
* sizeof(T
*));
136 for (size_t i
= 0; i
< old_cap
; ++i
)
138 if (old_hashtab
[i
] != NULL
)
140 T
** slot
= this->find_slot(old_hashtab
[i
]);
141 *slot
= old_hashtab
[i
];
144 delete[] old_hashtab
;
148 // Initial size of the hash table; must be a power of 2.
149 static const int initial_size
= 1024;
155 // The hash function for strings in the mapped index. This is copied
156 // directly from gdb/dwarf2read.c.
159 mapped_index_string_hash(const unsigned char* str
)
164 while ((c
= *str
++) != 0)
166 if (gdb_index_version
>= 5)
168 r
= r
* 67 + c
- 113;
174 // A specialization of Dwarf_info_reader, for building the .gdb_index.
176 class Gdb_index_info_reader
: public Dwarf_info_reader
179 Gdb_index_info_reader(bool is_type_unit
,
181 const unsigned char* symbols
,
184 unsigned int reloc_shndx
,
185 unsigned int reloc_type
,
186 Gdb_index
* gdb_index
)
187 : Dwarf_info_reader(is_type_unit
, object
, symbols
, symbols_size
, shndx
,
188 reloc_shndx
, reloc_type
),
189 gdb_index_(gdb_index
), cu_index_(0), cu_language_(0)
192 ~Gdb_index_info_reader()
193 { this->clear_declarations(); }
195 // Print usage statistics.
200 // Visit a compilation unit.
202 visit_compilation_unit(off_t cu_offset
, off_t cu_length
, Dwarf_die
*);
204 // Visit a type unit.
206 visit_type_unit(off_t tu_offset
, off_t tu_length
, off_t type_offset
,
207 uint64_t signature
, Dwarf_die
*);
210 // A map for recording DIEs we've seen that may be referred to be
211 // later DIEs (via DW_AT_specification or DW_AT_abstract_origin).
212 // The map is indexed by a DIE offset within the compile unit.
213 // PARENT_OFFSET_ is the offset of the DIE that represents the
214 // outer context, and NAME_ is a pointer to a component of the
215 // fully-qualified name.
216 // Normally, the names we point to are in a string table, so we don't
217 // have to manage them, but when we have a fully-qualified name
218 // computed, we put it in the table, and set PARENT_OFFSET_ to -1
219 // indicate a string that we are managing.
220 struct Declaration_pair
222 Declaration_pair(off_t parent_offset
, const char* name
)
223 : parent_offset_(parent_offset
), name_(name
)
226 off_t parent_offset_
;
229 typedef Unordered_map
<off_t
, Declaration_pair
> Declaration_map
;
231 // Visit a top-level DIE.
233 visit_top_die(Dwarf_die
* die
);
235 // Visit the children of a DIE.
237 visit_children(Dwarf_die
* die
, Dwarf_die
* context
);
241 visit_die(Dwarf_die
* die
, Dwarf_die
* context
);
243 // Visit the children of a DIE.
245 visit_children_for_decls(Dwarf_die
* die
);
249 visit_die_for_decls(Dwarf_die
* die
, Dwarf_die
* context
);
251 // Guess a fully-qualified name for a class type, based on member function
254 guess_full_class_name(Dwarf_die
* die
);
256 // Add a declaration DIE to the table of declarations.
258 add_declaration(Dwarf_die
* die
, Dwarf_die
* context
);
260 // Add a declaration whose fully-qualified name is already known.
262 add_declaration_with_full_name(Dwarf_die
* die
, const char* full_name
);
264 // Return the context for a DIE whose parent is at DIE_OFFSET.
266 get_context(off_t die_offset
);
268 // Construct a fully-qualified name for DIE.
270 get_qualified_name(Dwarf_die
* die
, Dwarf_die
* context
);
272 // Record the address ranges for a compilation unit.
274 record_cu_ranges(Dwarf_die
* die
);
276 // Wrapper for read_pubtable.
278 read_pubnames_and_pubtypes(Dwarf_die
* die
);
280 // Read the .debug_pubnames and .debug_pubtypes tables.
282 read_pubtable(Dwarf_pubnames_table
* table
, off_t offset
);
284 // Clear the declarations map.
286 clear_declarations();
288 // The Gdb_index section.
289 Gdb_index
* gdb_index_
;
290 // The current CU index (negative for a TU).
292 // The language of the current CU or TU.
293 unsigned int cu_language_
;
294 // Map from DIE offset to (parent offset, name) pair,
295 // for DW_AT_specification.
296 Declaration_map declarations_
;
299 // Total number of DWARF compilation units processed.
300 static unsigned int dwarf_cu_count
;
301 // Number of DWARF compilation units with pubnames/pubtypes.
302 static unsigned int dwarf_cu_nopubnames_count
;
303 // Total number of DWARF type units processed.
304 static unsigned int dwarf_tu_count
;
305 // Number of DWARF type units with pubnames/pubtypes.
306 static unsigned int dwarf_tu_nopubnames_count
;
309 // Total number of DWARF compilation units processed.
310 unsigned int Gdb_index_info_reader::dwarf_cu_count
= 0;
311 // Number of DWARF compilation units without pubnames/pubtypes.
312 unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count
= 0;
313 // Total number of DWARF type units processed.
314 unsigned int Gdb_index_info_reader::dwarf_tu_count
= 0;
315 // Number of DWARF type units without pubnames/pubtypes.
316 unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count
= 0;
318 // Process a compilation unit and parse its child DIE.
321 Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset
, off_t cu_length
,
324 ++Gdb_index_info_reader::dwarf_cu_count
;
325 this->cu_index_
= this->gdb_index_
->add_comp_unit(cu_offset
, cu_length
);
326 this->visit_top_die(root_die
);
329 // Process a type unit and parse its child DIE.
332 Gdb_index_info_reader::visit_type_unit(off_t tu_offset
, off_t
,
333 off_t type_offset
, uint64_t signature
,
336 ++Gdb_index_info_reader::dwarf_tu_count
;
337 // Use a negative index to flag this as a TU instead of a CU.
338 this->cu_index_
= -1 - this->gdb_index_
->add_type_unit(tu_offset
, type_offset
,
340 this->visit_top_die(root_die
);
343 // Process a top-level DIE.
344 // For compile_unit DIEs, record the address ranges. For all
345 // interesting tags, add qualified names to the symbol table
346 // and process interesting children. We may need to process
347 // certain children just for saving declarations that might be
348 // referenced by later DIEs with a DW_AT_specification attribute.
351 Gdb_index_info_reader::visit_top_die(Dwarf_die
* die
)
353 this->clear_declarations();
357 case elfcpp::DW_TAG_compile_unit
:
358 case elfcpp::DW_TAG_type_unit
:
359 this->cu_language_
= die
->int_attribute(elfcpp::DW_AT_language
);
360 if (die
->tag() == elfcpp::DW_TAG_compile_unit
)
361 this->record_cu_ranges(die
);
362 // If there is a pubnames and/or pubtypes section for this
363 // compilation unit, use those; otherwise, parse the DWARF
364 // info to extract the names.
365 if (!this->read_pubnames_and_pubtypes(die
))
367 // Check for languages that require specialized knowledge to
368 // construct fully-qualified names, that we don't yet support.
369 if (this->cu_language_
== elfcpp::DW_LANG_Ada83
370 || this->cu_language_
== elfcpp::DW_LANG_Fortran77
371 || this->cu_language_
== elfcpp::DW_LANG_Fortran90
372 || this->cu_language_
== elfcpp::DW_LANG_Java
373 || this->cu_language_
== elfcpp::DW_LANG_Ada95
374 || this->cu_language_
== elfcpp::DW_LANG_Ada2005
375 || this->cu_language_
== elfcpp::DW_LANG_Ada2012
376 || this->cu_language_
== elfcpp::DW_LANG_HIP
377 || this->cu_language_
== elfcpp::DW_LANG_Assembly
378 || this->cu_language_
== elfcpp::DW_LANG_C_sharp
379 || this->cu_language_
== elfcpp::DW_LANG_Mojo
380 || this->cu_language_
== elfcpp::DW_LANG_GLSL
381 || this->cu_language_
== elfcpp::DW_LANG_GLSL_ES
382 || this->cu_language_
== elfcpp::DW_LANG_HLSL
383 || this->cu_language_
== elfcpp::DW_LANG_Odin
384 || this->cu_language_
== elfcpp::DW_LANG_P4
385 || this->cu_language_
== elfcpp::DW_LANG_Metal
386 || this->cu_language_
== elfcpp::DW_LANG_Fortran95
387 || this->cu_language_
== elfcpp::DW_LANG_Fortran03
388 || this->cu_language_
== elfcpp::DW_LANG_Fortran08
389 || this->cu_language_
== elfcpp::DW_LANG_Fortran18
390 || this->cu_language_
== elfcpp::DW_LANG_Fortran23
391 || this->cu_language_
== elfcpp::DW_LANG_Ruby
392 || this->cu_language_
== elfcpp::DW_LANG_Move
393 || this->cu_language_
== elfcpp::DW_LANG_Hylo
)
395 gold_warning(_("%s: --gdb-index currently supports "
396 "only C and C++ languages"),
397 this->object()->name().c_str());
400 if (die
->tag() == elfcpp::DW_TAG_compile_unit
)
401 ++Gdb_index_info_reader::dwarf_cu_nopubnames_count
;
403 ++Gdb_index_info_reader::dwarf_tu_nopubnames_count
;
404 this->visit_children(die
, NULL
);
408 // The top level DIE should be one of the above.
409 gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
410 "or DW_TAG_type_unit"),
411 this->object()->name().c_str());
416 // Visit the children of PARENT, looking for symbols to add to the index.
417 // CONTEXT points to the DIE to use for constructing the qualified name --
418 // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
421 Gdb_index_info_reader::visit_children(Dwarf_die
* parent
, Dwarf_die
* context
)
423 off_t next_offset
= 0;
424 for (off_t die_offset
= parent
->child_offset();
426 die_offset
= next_offset
)
428 Dwarf_die
die(this, die_offset
, parent
);
431 this->visit_die(&die
, context
);
432 next_offset
= die
.sibling_offset();
436 // Visit a child DIE, looking for symbols to add to the index.
437 // CONTEXT is the parent DIE, used for constructing the qualified name;
438 // it is NULL if the parent DIE is the top-level DIE.
441 Gdb_index_info_reader::visit_die(Dwarf_die
* die
, Dwarf_die
* context
)
445 case elfcpp::DW_TAG_subprogram
:
446 case elfcpp::DW_TAG_constant
:
447 case elfcpp::DW_TAG_variable
:
448 case elfcpp::DW_TAG_enumerator
:
449 case elfcpp::DW_TAG_base_type
:
450 if (die
->is_declaration())
451 this->add_declaration(die
, context
);
454 // If the DIE is not a declaration, add it to the index.
455 std::string full_name
= this->get_qualified_name(die
, context
);
456 if (!full_name
.empty())
457 this->gdb_index_
->add_symbol(this->cu_index_
,
458 full_name
.c_str(), 0);
461 case elfcpp::DW_TAG_typedef
:
462 case elfcpp::DW_TAG_union_type
:
463 case elfcpp::DW_TAG_class_type
:
464 case elfcpp::DW_TAG_interface_type
:
465 case elfcpp::DW_TAG_structure_type
:
466 case elfcpp::DW_TAG_enumeration_type
:
467 case elfcpp::DW_TAG_subrange_type
:
468 case elfcpp::DW_TAG_namespace
:
470 std::string full_name
;
472 // For classes at the top level, we need to look for a
473 // member function with a linkage name in order to get
474 // the properly-canonicalized name.
476 && (die
->tag() == elfcpp::DW_TAG_class_type
477 || die
->tag() == elfcpp::DW_TAG_structure_type
478 || die
->tag() == elfcpp::DW_TAG_union_type
))
479 full_name
.assign(this->guess_full_class_name(die
));
481 // Because we will visit the children, we need to add this DIE
482 // to the declarations table.
483 if (full_name
.empty())
484 this->add_declaration(die
, context
);
486 this->add_declaration_with_full_name(die
, full_name
.c_str());
488 // If the DIE is not a declaration, add it to the index.
489 // Gdb stores a namespace in the index even when it is
491 if (die
->tag() == elfcpp::DW_TAG_namespace
492 || !die
->is_declaration())
494 if (full_name
.empty())
495 full_name
= this->get_qualified_name(die
, context
);
496 if (!full_name
.empty())
497 this->gdb_index_
->add_symbol(this->cu_index_
,
498 full_name
.c_str(), 0);
501 // We're interested in the children only for namespaces and
502 // enumeration types. For enumeration types, we do not include
503 // the enumeration tag as part of the full name. For other tags,
504 // visit the children only to collect declarations.
505 if (die
->tag() == elfcpp::DW_TAG_namespace
506 || die
->tag() == elfcpp::DW_TAG_enumeration_type
)
507 this->visit_children(die
, die
);
509 this->visit_children_for_decls(die
);
517 // Visit the children of PARENT, looking only for declarations that
518 // may be referenced by later specification DIEs.
521 Gdb_index_info_reader::visit_children_for_decls(Dwarf_die
* parent
)
523 off_t next_offset
= 0;
524 for (off_t die_offset
= parent
->child_offset();
526 die_offset
= next_offset
)
528 Dwarf_die
die(this, die_offset
, parent
);
531 this->visit_die_for_decls(&die
, parent
);
532 next_offset
= die
.sibling_offset();
536 // Visit a child DIE, looking only for declarations that
537 // may be referenced by later specification DIEs.
540 Gdb_index_info_reader::visit_die_for_decls(Dwarf_die
* die
, Dwarf_die
* context
)
544 case elfcpp::DW_TAG_subprogram
:
545 case elfcpp::DW_TAG_constant
:
546 case elfcpp::DW_TAG_variable
:
547 case elfcpp::DW_TAG_enumerator
:
548 case elfcpp::DW_TAG_base_type
:
550 if (die
->is_declaration())
551 this->add_declaration(die
, context
);
554 case elfcpp::DW_TAG_typedef
:
555 case elfcpp::DW_TAG_union_type
:
556 case elfcpp::DW_TAG_class_type
:
557 case elfcpp::DW_TAG_interface_type
:
558 case elfcpp::DW_TAG_structure_type
:
559 case elfcpp::DW_TAG_enumeration_type
:
560 case elfcpp::DW_TAG_subrange_type
:
561 case elfcpp::DW_TAG_namespace
:
563 if (die
->is_declaration())
564 this->add_declaration(die
, context
);
565 this->visit_children_for_decls(die
);
573 // Extract the class name from the linkage name of a member function.
574 // This code is adapted from ../gdb/cp-support.c.
576 #define d_left(dc) (dc)->u.s_binary.left
577 #define d_right(dc) (dc)->u.s_binary.right
580 class_name_from_linkage_name(const char* linkage_name
)
583 struct demangle_component
* tree
=
584 cplus_demangle_v3_components(linkage_name
, DMGL_NO_OPTS
, &storage
);
590 // First strip off any qualifiers, if we have a function or
595 case DEMANGLE_COMPONENT_CONST
:
596 case DEMANGLE_COMPONENT_RESTRICT
:
597 case DEMANGLE_COMPONENT_VOLATILE
:
598 case DEMANGLE_COMPONENT_CONST_THIS
:
599 case DEMANGLE_COMPONENT_RESTRICT_THIS
:
600 case DEMANGLE_COMPONENT_VOLATILE_THIS
:
601 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL
:
609 // If what we have now is a function, discard the argument list.
610 if (tree
->type
== DEMANGLE_COMPONENT_TYPED_NAME
)
613 // If what we have now is a template, strip off the template
614 // arguments. The left subtree may be a qualified name.
615 if (tree
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
618 // What we have now should be a name, possibly qualified.
619 // Additional qualifiers could live in the left subtree or the right
620 // subtree. Find the last piece.
622 struct demangle_component
* prev_comp
= NULL
;
623 struct demangle_component
* cur_comp
= tree
;
625 switch (cur_comp
->type
)
627 case DEMANGLE_COMPONENT_QUAL_NAME
:
628 case DEMANGLE_COMPONENT_LOCAL_NAME
:
629 prev_comp
= cur_comp
;
630 cur_comp
= d_right(cur_comp
);
632 case DEMANGLE_COMPONENT_TEMPLATE
:
633 case DEMANGLE_COMPONENT_NAME
:
634 case DEMANGLE_COMPONENT_CTOR
:
635 case DEMANGLE_COMPONENT_DTOR
:
636 case DEMANGLE_COMPONENT_OPERATOR
:
637 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR
:
647 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
649 // We want to discard the rightmost child of PREV_COMP.
650 *prev_comp
= *d_left(prev_comp
);
651 size_t allocated_size
;
652 ret
= cplus_demangle_print(DMGL_NO_OPTS
, tree
, 30, &allocated_size
);
659 // Guess a fully-qualified name for a class type, based on member function
660 // linkage names. This is needed for class/struct/union types at the
661 // top level, because GCC does not always properly embed them within
662 // the namespace. As in gdb, we look for a member function with a linkage
663 // name and extract the qualified name from the demangled name.
666 Gdb_index_info_reader::guess_full_class_name(Dwarf_die
* die
)
668 std::string full_name
;
669 off_t next_offset
= 0;
671 // This routine scans ahead in the DIE structure, possibly advancing
672 // the relocation tracker beyond the current DIE. We need to checkpoint
673 // the tracker and reset it when we're done.
674 uint64_t checkpoint
= this->get_reloc_checkpoint();
676 for (off_t child_offset
= die
->child_offset();
678 child_offset
= next_offset
)
680 Dwarf_die
child(this, child_offset
, die
);
681 if (child
.tag() == 0)
683 if (child
.tag() == elfcpp::DW_TAG_subprogram
)
685 const char* linkage_name
= child
.linkage_name();
686 if (linkage_name
!= NULL
)
688 char* guess
= class_name_from_linkage_name(linkage_name
);
691 full_name
.assign(guess
);
697 next_offset
= child
.sibling_offset();
700 this->reset_relocs(checkpoint
);
704 // Add a declaration DIE to the table of declarations.
707 Gdb_index_info_reader::add_declaration(Dwarf_die
* die
, Dwarf_die
* context
)
709 const char* name
= die
->name();
711 off_t parent_offset
= context
!= NULL
? context
->offset() : 0;
713 // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
714 // attribute, use the parent and name from the earlier declaration.
715 off_t spec
= die
->specification();
717 spec
= die
->abstract_origin();
720 Declaration_map::iterator it
= this->declarations_
.find(spec
);
721 if (it
!= this->declarations_
.end())
723 parent_offset
= it
->second
.parent_offset_
;
724 name
= it
->second
.name_
;
730 if (die
->tag() == elfcpp::DW_TAG_namespace
)
731 name
= "(anonymous namespace)";
732 else if (die
->tag() == elfcpp::DW_TAG_union_type
)
733 name
= "(anonymous union)";
738 Declaration_pair
decl(parent_offset
, name
);
739 this->declarations_
.insert(std::make_pair(die
->offset(), decl
));
742 // Add a declaration whose fully-qualified name is already known.
743 // In the case where we had to get the canonical name by demangling
744 // a linkage name, this ensures we use that name instead of the one
745 // provided in DW_AT_name.
748 Gdb_index_info_reader::add_declaration_with_full_name(
750 const char* full_name
)
752 // We need to copy the name.
753 int len
= strlen(full_name
);
754 char* copy
= new char[len
+ 1];
755 memcpy(copy
, full_name
, len
+ 1);
757 // Flag that we now manage the memory this points to.
758 Declaration_pair
decl(-1, copy
);
759 this->declarations_
.insert(std::make_pair(die
->offset(), decl
));
762 // Return the context for a DIE whose parent is at DIE_OFFSET.
765 Gdb_index_info_reader::get_context(off_t die_offset
)
768 Declaration_map::iterator it
= this->declarations_
.find(die_offset
);
769 if (it
!= this->declarations_
.end())
771 off_t parent_offset
= it
->second
.parent_offset_
;
772 if (parent_offset
> 0)
774 context
= get_context(parent_offset
);
775 context
.append("::");
777 if (it
->second
.name_
!= NULL
)
778 context
.append(it
->second
.name_
);
783 // Construct the fully-qualified name for DIE.
786 Gdb_index_info_reader::get_qualified_name(Dwarf_die
* die
, Dwarf_die
* context
)
788 std::string full_name
;
789 const char* name
= die
->name();
791 off_t parent_offset
= context
!= NULL
? context
->offset() : 0;
793 // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
794 // attribute, use the parent and name from the earlier declaration.
795 off_t spec
= die
->specification();
797 spec
= die
->abstract_origin();
800 Declaration_map::iterator it
= this->declarations_
.find(spec
);
801 if (it
!= this->declarations_
.end())
803 parent_offset
= it
->second
.parent_offset_
;
804 name
= it
->second
.name_
;
808 if (name
== NULL
&& die
->tag() == elfcpp::DW_TAG_namespace
)
809 name
= "(anonymous namespace)";
810 else if (name
== NULL
)
813 // If this is an enumerator constant, skip the immediate parent,
814 // which is the enumeration tag.
815 if (die
->tag() == elfcpp::DW_TAG_enumerator
)
817 Declaration_map::iterator it
= this->declarations_
.find(parent_offset
);
818 if (it
!= this->declarations_
.end())
819 parent_offset
= it
->second
.parent_offset_
;
822 if (parent_offset
> 0)
824 full_name
.assign(this->get_context(parent_offset
));
825 full_name
.append("::");
827 full_name
.append(name
);
832 // Record the address ranges for a compilation unit.
835 Gdb_index_info_reader::record_cu_ranges(Dwarf_die
* die
)
840 off_t ranges_offset
= die
->ref_attribute(elfcpp::DW_AT_ranges
, &shndx
);
841 if (ranges_offset
!= -1)
843 Dwarf_range_list
* ranges
= this->read_range_list(shndx
, ranges_offset
);
845 this->gdb_index_
->add_address_range_list(this->object(),
846 this->cu_index_
, ranges
);
850 off_t low_pc
= die
->address_attribute(elfcpp::DW_AT_low_pc
, &shndx
);
851 off_t high_pc
= die
->address_attribute(elfcpp::DW_AT_high_pc
, &shndx2
);
854 high_pc
= die
->uint_attribute(elfcpp::DW_AT_high_pc
);
858 if ((low_pc
!= 0 || high_pc
!= 0) && low_pc
!= -1)
862 gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
863 "are in different sections"),
864 this->object()->name().c_str());
867 if (shndx
== 0 || this->object()->is_section_included(shndx
))
869 Dwarf_range_list
* ranges
= new Dwarf_range_list();
870 ranges
->add(shndx
, low_pc
, high_pc
);
871 this->gdb_index_
->add_address_range_list(this->object(),
872 this->cu_index_
, ranges
);
877 // Read table and add the relevant names to the index. Returns true
878 // if any names were added.
881 Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table
* table
, off_t offset
)
883 // If we couldn't read the section when building the cu_pubname_map,
884 // then we won't find any pubnames now.
888 if (!table
->read_header(offset
))
893 const char* name
= table
->next_name(&flag_byte
);
897 this->gdb_index_
->add_symbol(this->cu_index_
, name
, flag_byte
);
902 // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
903 // Returns TRUE if either a pubnames or pubtypes section was found.
906 Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die
* die
)
908 // If this is a skeleton debug-type die (generated via
909 // -gsplit-dwarf), then the associated pubnames should have been
910 // read along with the corresponding CU. In any case, there isn't
911 // enough info inside to build a gdb index entry.
912 if (die
->tag() == elfcpp::DW_TAG_type_unit
913 && die
->string_attribute(elfcpp::DW_AT_GNU_dwo_name
))
916 // We use stmt_list_off as a unique identifier for the
917 // compilation unit and its associated type units.
919 off_t stmt_list_off
= die
->ref_attribute (elfcpp::DW_AT_stmt_list
,
921 // Look for the attr as either a flag or a ref.
922 off_t offset
= die
->ref_attribute(elfcpp::DW_AT_GNU_pubnames
, &shndx
);
924 // Newer versions of GCC generate CUs, but not TUs, with
925 // DW_AT_FORM_flag_present.
926 unsigned int flag
= die
->uint_attribute(elfcpp::DW_AT_GNU_pubnames
);
927 if (offset
== -1 && flag
== 0)
929 // Didn't find the attribute.
930 if (die
->tag() == elfcpp::DW_TAG_type_unit
)
932 // If die is a TU, then it might correspond to a CU which we
933 // have read. If it does, then no need to read the pubnames.
934 // If it doesn't, then the caller will have to parse the
935 // dies manually to find the names.
936 return this->gdb_index_
->pubnames_read(this->object(),
941 // No attribute on the CU means that no pubnames were read.
946 // We found the attribute, so we can check if the corresponding
947 // pubnames have been read.
948 if (this->gdb_index_
->pubnames_read(this->object(), stmt_list_off
))
951 this->gdb_index_
->set_pubnames_read(this->object(), stmt_list_off
);
953 // We have an attribute, and the pubnames haven't been read, so read
956 // In some of the cases, we could rely on the previous value of
957 // offset here, but sorting out which cases complicates the logic
958 // enough that it isn't worth it. So just look up the offset again.
959 offset
= this->gdb_index_
->find_pubname_offset(this->cu_offset());
960 names
= this->read_pubtable(this->gdb_index_
->pubnames_table(), offset
);
963 offset
= this->gdb_index_
->find_pubtype_offset(this->cu_offset());
964 types
= this->read_pubtable(this->gdb_index_
->pubtypes_table(), offset
);
965 return names
|| types
;
968 // Clear the declarations map.
970 Gdb_index_info_reader::clear_declarations()
972 // Free strings in memory we manage.
973 for (Declaration_map::iterator it
= this->declarations_
.begin();
974 it
!= this->declarations_
.end();
977 if (it
->second
.parent_offset_
== -1)
978 delete[] it
->second
.name_
;
981 this->declarations_
.clear();
984 // Print usage statistics.
986 Gdb_index_info_reader::print_stats()
988 fprintf(stderr
, _("%s: DWARF CUs: %u\n"),
989 program_name
, Gdb_index_info_reader::dwarf_cu_count
);
990 fprintf(stderr
, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
991 program_name
, Gdb_index_info_reader::dwarf_cu_nopubnames_count
);
992 fprintf(stderr
, _("%s: DWARF TUs: %u\n"),
993 program_name
, Gdb_index_info_reader::dwarf_tu_count
);
994 fprintf(stderr
, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
995 program_name
, Gdb_index_info_reader::dwarf_tu_nopubnames_count
);
1000 // Construct the .gdb_index section.
1002 Gdb_index::Gdb_index(Output_section
* gdb_index_section
)
1003 : Output_section_data(4),
1004 pubnames_table_(NULL
),
1005 pubtypes_table_(NULL
),
1006 gdb_index_section_(gdb_index_section
),
1011 cu_vector_offsets_(NULL
),
1017 stringpool_offset_(0),
1018 pubnames_object_(NULL
),
1019 stmt_list_offset_(-1)
1021 this->gdb_symtab_
= new Gdb_hashtab
<Gdb_symbol
>();
1024 Gdb_index::~Gdb_index()
1026 // Free the memory used by the symbol table.
1027 delete this->gdb_symtab_
;
1028 // Free the memory used by the CU vectors.
1029 for (unsigned int i
= 0; i
< this->cu_vector_list_
.size(); ++i
)
1030 delete this->cu_vector_list_
[i
];
1034 // Scan the pubnames and pubtypes sections and build a map of the
1035 // various cus and tus they refer to, so we can process the entries
1036 // when we encounter the die for that cu or tu.
1037 // Return the just-read table so it can be cached.
1039 Dwarf_pubnames_table
*
1040 Gdb_index::map_pubtable_to_dies(unsigned int attr
,
1041 Gdb_index_info_reader
* dwinfo
,
1043 const unsigned char* symbols
,
1046 uint64_t section_offset
= 0;
1047 Dwarf_pubnames_table
* table
;
1048 Pubname_offset_map
* map
;
1050 if (attr
== elfcpp::DW_AT_GNU_pubnames
)
1052 table
= new Dwarf_pubnames_table(dwinfo
, false);
1053 map
= &this->cu_pubname_map_
;
1057 table
= new Dwarf_pubnames_table(dwinfo
, true);
1058 map
= &this->cu_pubtype_map_
;
1062 if (!table
->read_section(object
, symbols
, symbols_size
))
1065 while (table
->read_header(section_offset
))
1067 map
->insert(std::make_pair(table
->cu_offset(), section_offset
));
1068 section_offset
+= table
->subsection_size();
1074 // Wrapper for map_pubtable_to_dies
1077 Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader
* dwinfo
,
1079 const unsigned char* symbols
,
1082 // This is a new object, so reset the relevant variables.
1083 this->pubnames_object_
= object
;
1084 this->stmt_list_offset_
= -1;
1086 delete this->pubnames_table_
;
1087 this->pubnames_table_
1088 = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames
, dwinfo
,
1089 object
, symbols
, symbols_size
);
1090 delete this->pubtypes_table_
;
1091 this->pubtypes_table_
1092 = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes
, dwinfo
,
1093 object
, symbols
, symbols_size
);
1096 // Given a cu_offset, find the associated section of the pubnames
1100 Gdb_index::find_pubname_offset(off_t cu_offset
)
1102 Pubname_offset_map::iterator it
= this->cu_pubname_map_
.find(cu_offset
);
1103 if (it
!= this->cu_pubname_map_
.end())
1108 // Given a cu_offset, find the associated section of the pubnames
1112 Gdb_index::find_pubtype_offset(off_t cu_offset
)
1114 Pubname_offset_map::iterator it
= this->cu_pubtype_map_
.find(cu_offset
);
1115 if (it
!= this->cu_pubtype_map_
.end())
1120 // Scan a .debug_info or .debug_types input section.
1123 Gdb_index::scan_debug_info(bool is_type_unit
,
1125 const unsigned char* symbols
,
1128 unsigned int reloc_shndx
,
1129 unsigned int reloc_type
)
1131 Gdb_index_info_reader
dwinfo(is_type_unit
, object
,
1132 symbols
, symbols_size
,
1135 if (object
!= this->pubnames_object_
)
1136 map_pubnames_and_types_to_dies(&dwinfo
, object
, symbols
, symbols_size
);
1143 Gdb_index::add_symbol(int cu_index
, const char* sym_name
, uint8_t flags
)
1145 unsigned int hash
= mapped_index_string_hash(
1146 reinterpret_cast<const unsigned char*>(sym_name
));
1147 Gdb_symbol
* sym
= new Gdb_symbol();
1148 this->stringpool_
.add(sym_name
, true, &sym
->name_key
);
1149 sym
->hashval
= hash
;
1150 sym
->cu_vector_index
= 0;
1152 Gdb_symbol
* found
= this->gdb_symtab_
->add(sym
);
1155 // New symbol -- allocate a new CU index vector.
1156 found
->cu_vector_index
= this->cu_vector_list_
.size();
1157 this->cu_vector_list_
.push_back(new Cu_vector());
1161 // Found an existing symbol -- append to the existing
1166 // Add the CU index to the vector list for this symbol,
1167 // if it's not already on the list. We only need to
1168 // check the last added entry.
1169 Cu_vector
* cu_vec
= this->cu_vector_list_
[found
->cu_vector_index
];
1170 if (cu_vec
->size() == 0
1171 || cu_vec
->back().first
!= cu_index
1172 || cu_vec
->back().second
!= flags
)
1173 cu_vec
->push_back(std::make_pair(cu_index
, flags
));
1176 // Return TRUE if we have already processed the pubnames associated
1177 // with the statement list at the given OFFSET.
1180 Gdb_index::pubnames_read(const Relobj
* object
, off_t offset
)
1182 bool ret
= (this->pubnames_object_
== object
1183 && this->stmt_list_offset_
== offset
);
1187 // Record that we have processed the pubnames associated with the
1188 // statement list for OBJECT at the given OFFSET.
1191 Gdb_index::set_pubnames_read(const Relobj
* object
, off_t offset
)
1193 this->pubnames_object_
= object
;
1194 this->stmt_list_offset_
= offset
;
1197 // Set the size of the .gdb_index section.
1200 Gdb_index::set_final_data_size()
1202 // Finalize the string pool.
1203 this->stringpool_
.set_string_offsets();
1205 // Compute the total size of the CU vectors.
1206 // For each CU vector, include one entry for the count at the
1207 // beginning of the vector.
1208 unsigned int cu_vector_count
= this->cu_vector_list_
.size();
1209 unsigned int cu_vector_size
= 0;
1210 this->cu_vector_offsets_
= new off_t
[cu_vector_count
];
1211 for (unsigned int i
= 0; i
< cu_vector_count
; ++i
)
1213 Cu_vector
* cu_vec
= this->cu_vector_list_
[i
];
1214 cu_vector_offsets_
[i
] = cu_vector_size
;
1215 cu_vector_size
+= gdb_index_offset_size
* (cu_vec
->size() + 1);
1218 // Assign relative offsets to each portion of the index,
1219 // and find the total size of the section.
1220 section_size_type data_size
= gdb_index_hdr_size
;
1221 data_size
+= this->comp_units_
.size() * gdb_index_cu_size
;
1222 this->tu_offset_
= data_size
;
1223 data_size
+= this->type_units_
.size() * gdb_index_tu_size
;
1224 this->addr_offset_
= data_size
;
1225 for (unsigned int i
= 0; i
< this->ranges_
.size(); ++i
)
1226 data_size
+= this->ranges_
[i
].ranges
->size() * gdb_index_addr_size
;
1227 this->symtab_offset_
= data_size
;
1228 data_size
+= this->gdb_symtab_
->capacity() * gdb_index_sym_size
;
1229 this->cu_pool_offset_
= data_size
;
1230 data_size
+= cu_vector_size
;
1231 this->stringpool_offset_
= data_size
;
1232 data_size
+= this->stringpool_
.get_strtab_size();
1234 this->set_data_size(data_size
);
1237 // Write the data to the file.
1240 Gdb_index::do_write(Output_file
* of
)
1242 const off_t off
= this->offset();
1243 const off_t oview_size
= this->data_size();
1244 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1245 unsigned char* pov
= oview
;
1247 // Write the file header.
1248 // (1) Version number.
1249 elfcpp::Swap
<32, false>::writeval(pov
, gdb_index_version
);
1251 // (2) Offset of the CU list.
1252 elfcpp::Swap
<32, false>::writeval(pov
, gdb_index_hdr_size
);
1254 // (3) Offset of the types CU list.
1255 elfcpp::Swap
<32, false>::writeval(pov
, this->tu_offset_
);
1257 // (4) Offset of the address area.
1258 elfcpp::Swap
<32, false>::writeval(pov
, this->addr_offset_
);
1260 // (5) Offset of the symbol table.
1261 elfcpp::Swap
<32, false>::writeval(pov
, this->symtab_offset_
);
1263 // (6) Offset of the constant pool.
1264 elfcpp::Swap
<32, false>::writeval(pov
, this->cu_pool_offset_
);
1267 gold_assert(pov
- oview
== gdb_index_hdr_size
);
1269 // Write the CU list.
1270 unsigned int comp_units_count
= this->comp_units_
.size();
1271 for (unsigned int i
= 0; i
< comp_units_count
; ++i
)
1273 const Comp_unit
& cu
= this->comp_units_
[i
];
1274 elfcpp::Swap
<64, false>::writeval(pov
, cu
.cu_offset
);
1275 elfcpp::Swap
<64, false>::writeval(pov
+ 8, cu
.cu_length
);
1279 gold_assert(pov
- oview
== this->tu_offset_
);
1281 // Write the types CU list.
1282 for (unsigned int i
= 0; i
< this->type_units_
.size(); ++i
)
1284 const Type_unit
& tu
= this->type_units_
[i
];
1285 elfcpp::Swap
<64, false>::writeval(pov
, tu
.tu_offset
);
1286 elfcpp::Swap
<64, false>::writeval(pov
+ 8, tu
.type_offset
);
1287 elfcpp::Swap
<64, false>::writeval(pov
+ 16, tu
.type_signature
);
1291 gold_assert(pov
- oview
== this->addr_offset_
);
1293 // Write the address area.
1294 for (unsigned int i
= 0; i
< this->ranges_
.size(); ++i
)
1296 int cu_index
= this->ranges_
[i
].cu_index
;
1297 // Translate negative indexes, which refer to a TU, to a
1298 // logical index into a concatenated CU/TU list.
1300 cu_index
= comp_units_count
+ (-1 - cu_index
);
1301 Relobj
* object
= this->ranges_
[i
].object
;
1302 const Dwarf_range_list
& ranges
= *this->ranges_
[i
].ranges
;
1303 for (unsigned int j
= 0; j
< ranges
.size(); ++j
)
1305 const Dwarf_range_list::Range
& range
= ranges
[j
];
1307 if (range
.shndx
> 0)
1309 const Output_section
* os
= object
->output_section(range
.shndx
);
1310 base
= (os
->address()
1311 + object
->output_section_offset(range
.shndx
));
1313 elfcpp::Swap_aligned32
<64, false>::writeval(pov
, base
+ range
.start
);
1314 elfcpp::Swap_aligned32
<64, false>::writeval(pov
+ 8,
1316 elfcpp::Swap
<32, false>::writeval(pov
+ 16, cu_index
);
1321 gold_assert(pov
- oview
== this->symtab_offset_
);
1323 // Write the symbol table.
1324 for (unsigned int i
= 0; i
< this->gdb_symtab_
->capacity(); ++i
)
1326 const Gdb_symbol
* sym
= (*this->gdb_symtab_
)[i
];
1327 section_offset_type name_offset
= 0;
1328 unsigned int cu_vector_offset
= 0;
1331 name_offset
= (this->stringpool_
.get_offset_from_key(sym
->name_key
)
1332 + this->stringpool_offset_
- this->cu_pool_offset_
);
1333 cu_vector_offset
= this->cu_vector_offsets_
[sym
->cu_vector_index
];
1335 elfcpp::Swap
<32, false>::writeval(pov
, name_offset
);
1336 elfcpp::Swap
<32, false>::writeval(pov
+ 4, cu_vector_offset
);
1340 gold_assert(pov
- oview
== this->cu_pool_offset_
);
1342 // Write the CU vectors into the constant pool.
1343 for (unsigned int i
= 0; i
< this->cu_vector_list_
.size(); ++i
)
1345 Cu_vector
* cu_vec
= this->cu_vector_list_
[i
];
1346 elfcpp::Swap
<32, false>::writeval(pov
, cu_vec
->size());
1348 for (unsigned int j
= 0; j
< cu_vec
->size(); ++j
)
1350 int cu_index
= (*cu_vec
)[j
].first
;
1351 uint8_t flags
= (*cu_vec
)[j
].second
;
1353 cu_index
= comp_units_count
+ (-1 - cu_index
);
1354 cu_index
|= flags
<< 24;
1355 elfcpp::Swap
<32, false>::writeval(pov
, cu_index
);
1360 gold_assert(pov
- oview
== this->stringpool_offset_
);
1362 // Write the strings into the constant pool.
1363 this->stringpool_
.write_to_buffer(pov
, oview_size
- this->stringpool_offset_
);
1365 of
->write_output_view(off
, oview_size
, oview
);
1368 // Print usage statistics.
1370 Gdb_index::print_stats()
1372 if (parameters
->options().gdb_index())
1373 Gdb_index_info_reader::print_stats();
1376 } // End namespace gold.