1 // gdb-index.cc -- generate .gdb_index section for fast debug lookup
3 // Copyright 2012 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 = 5;
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 type_offset, uint64_t signature,
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 // Read the .debug_pubnames and .debug_pubtypes tables.
278 read_pubnames_and_pubtypes(Dwarf_die* die);
280 // Clear the declarations map.
282 clear_declarations();
284 // The Gdb_index section.
285 Gdb_index* gdb_index_;
286 // The current CU index (negative for a TU).
288 // The language of the current CU or TU.
289 unsigned int cu_language_;
290 // Map from DIE offset to (parent offset, name) pair,
291 // for DW_AT_specification.
292 Declaration_map declarations_;
295 // Total number of DWARF compilation units processed.
296 static unsigned int dwarf_cu_count;
297 // Number of DWARF compilation units with pubnames/pubtypes.
298 static unsigned int dwarf_cu_nopubnames_count;
299 // Total number of DWARF type units processed.
300 static unsigned int dwarf_tu_count;
301 // Number of DWARF type units with pubnames/pubtypes.
302 static unsigned int dwarf_tu_nopubnames_count;
305 // Total number of DWARF compilation units processed.
306 unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
307 // Number of DWARF compilation units without pubnames/pubtypes.
308 unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
309 // Total number of DWARF type units processed.
310 unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
311 // Number of DWARF type units without pubnames/pubtypes.
312 unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
314 // Process a compilation unit and parse its child DIE.
317 Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
320 ++Gdb_index_info_reader::dwarf_cu_count;
321 this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
322 this->visit_top_die(root_die);
325 // Process a type unit and parse its child DIE.
328 Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t type_offset,
329 uint64_t signature, Dwarf_die* root_die)
331 ++Gdb_index_info_reader::dwarf_tu_count;
332 // Use a negative index to flag this as a TU instead of a CU.
333 this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
335 this->visit_top_die(root_die);
338 // Process a top-level DIE.
339 // For compile_unit DIEs, record the address ranges. For all
340 // interesting tags, add qualified names to the symbol table
341 // and process interesting children. We may need to process
342 // certain children just for saving declarations that might be
343 // referenced by later DIEs with a DW_AT_specification attribute.
346 Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
348 this->clear_declarations();
352 case elfcpp::DW_TAG_compile_unit:
353 case elfcpp::DW_TAG_type_unit:
354 this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
355 // Check for languages that require specialized knowledge to
356 // construct fully-qualified names, that we don't yet support.
357 if (this->cu_language_ == elfcpp::DW_LANG_Ada83
358 || this->cu_language_ == elfcpp::DW_LANG_Fortran77
359 || this->cu_language_ == elfcpp::DW_LANG_Fortran90
360 || this->cu_language_ == elfcpp::DW_LANG_Java
361 || this->cu_language_ == elfcpp::DW_LANG_Ada95
362 || this->cu_language_ == elfcpp::DW_LANG_Fortran95)
364 gold_warning(_("%s: --gdb-index currently supports "
365 "only C and C++ languages"),
366 this->object()->name().c_str());
369 if (die->tag() == elfcpp::DW_TAG_compile_unit)
370 this->record_cu_ranges(die);
371 // If there is a pubnames and/or pubtypes section for this
372 // compilation unit, use those; otherwise, parse the DWARF
373 // info to extract the names.
374 if (!this->read_pubnames_and_pubtypes(die))
376 if (die->tag() == elfcpp::DW_TAG_compile_unit)
377 ++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
379 ++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
380 this->visit_children(die, NULL);
384 // The top level DIE should be one of the above.
385 gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
386 "or DW_TAG_type_unit"),
387 this->object()->name().c_str());
393 // Visit the children of PARENT, looking for symbols to add to the index.
394 // CONTEXT points to the DIE to use for constructing the qualified name --
395 // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
398 Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
400 off_t next_offset = 0;
401 for (off_t die_offset = parent->child_offset();
403 die_offset = next_offset)
405 Dwarf_die die(this, die_offset, parent);
408 this->visit_die(&die, context);
409 next_offset = die.sibling_offset();
413 // Visit a child DIE, looking for symbols to add to the index.
414 // CONTEXT is the parent DIE, used for constructing the qualified name;
415 // it is NULL if the parent DIE is the top-level DIE.
418 Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
422 case elfcpp::DW_TAG_subprogram:
423 case elfcpp::DW_TAG_constant:
424 case elfcpp::DW_TAG_variable:
425 case elfcpp::DW_TAG_enumerator:
426 case elfcpp::DW_TAG_base_type:
427 if (die->is_declaration())
428 this->add_declaration(die, context);
431 // If the DIE is not a declaration, add it to the index.
432 std::string full_name = this->get_qualified_name(die, context);
433 if (!full_name.empty())
434 this->gdb_index_->add_symbol(this->cu_index_, full_name.c_str());
437 case elfcpp::DW_TAG_typedef:
438 case elfcpp::DW_TAG_union_type:
439 case elfcpp::DW_TAG_class_type:
440 case elfcpp::DW_TAG_interface_type:
441 case elfcpp::DW_TAG_structure_type:
442 case elfcpp::DW_TAG_enumeration_type:
443 case elfcpp::DW_TAG_subrange_type:
444 case elfcpp::DW_TAG_namespace:
446 std::string full_name;
448 // For classes at the top level, we need to look for a
449 // member function with a linkage name in order to get
450 // the properly-canonicalized name.
452 && (die->tag() == elfcpp::DW_TAG_class_type
453 || die->tag() == elfcpp::DW_TAG_structure_type
454 || die->tag() == elfcpp::DW_TAG_union_type))
455 full_name.assign(this->guess_full_class_name(die));
457 // Because we will visit the children, we need to add this DIE
458 // to the declarations table.
459 if (full_name.empty())
460 this->add_declaration(die, context);
462 this->add_declaration_with_full_name(die, full_name.c_str());
464 // If the DIE is not a declaration, add it to the index.
465 // Gdb stores a namespace in the index even when it is
467 if (die->tag() == elfcpp::DW_TAG_namespace
468 || !die->is_declaration())
470 if (full_name.empty())
471 full_name = this->get_qualified_name(die, context);
472 if (!full_name.empty())
473 this->gdb_index_->add_symbol(this->cu_index_,
477 // We're interested in the children only for namespaces and
478 // enumeration types. For enumeration types, we do not include
479 // the enumeration tag as part of the full name. For other tags,
480 // visit the children only to collect declarations.
481 if (die->tag() == elfcpp::DW_TAG_namespace
482 || die->tag() == elfcpp::DW_TAG_enumeration_type)
483 this->visit_children(die, die);
485 this->visit_children_for_decls(die);
493 // Visit the children of PARENT, looking only for declarations that
494 // may be referenced by later specification DIEs.
497 Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
499 off_t next_offset = 0;
500 for (off_t die_offset = parent->child_offset();
502 die_offset = next_offset)
504 Dwarf_die die(this, die_offset, parent);
507 this->visit_die_for_decls(&die, parent);
508 next_offset = die.sibling_offset();
512 // Visit a child DIE, looking only for declarations that
513 // may be referenced by later specification DIEs.
516 Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
520 case elfcpp::DW_TAG_subprogram:
521 case elfcpp::DW_TAG_constant:
522 case elfcpp::DW_TAG_variable:
523 case elfcpp::DW_TAG_enumerator:
524 case elfcpp::DW_TAG_base_type:
526 if (die->is_declaration())
527 this->add_declaration(die, context);
530 case elfcpp::DW_TAG_typedef:
531 case elfcpp::DW_TAG_union_type:
532 case elfcpp::DW_TAG_class_type:
533 case elfcpp::DW_TAG_interface_type:
534 case elfcpp::DW_TAG_structure_type:
535 case elfcpp::DW_TAG_enumeration_type:
536 case elfcpp::DW_TAG_subrange_type:
537 case elfcpp::DW_TAG_namespace:
539 if (die->is_declaration())
540 this->add_declaration(die, context);
541 this->visit_children_for_decls(die);
549 // Extract the class name from the linkage name of a member function.
550 // This code is adapted from ../gdb/cp-support.c.
552 #define d_left(dc) (dc)->u.s_binary.left
553 #define d_right(dc) (dc)->u.s_binary.right
556 class_name_from_linkage_name(const char* linkage_name)
559 struct demangle_component* tree =
560 cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
566 // First strip off any qualifiers, if we have a function or
571 case DEMANGLE_COMPONENT_CONST:
572 case DEMANGLE_COMPONENT_RESTRICT:
573 case DEMANGLE_COMPONENT_VOLATILE:
574 case DEMANGLE_COMPONENT_CONST_THIS:
575 case DEMANGLE_COMPONENT_RESTRICT_THIS:
576 case DEMANGLE_COMPONENT_VOLATILE_THIS:
577 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
585 // If what we have now is a function, discard the argument list.
586 if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
589 // If what we have now is a template, strip off the template
590 // arguments. The left subtree may be a qualified name.
591 if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
594 // What we have now should be a name, possibly qualified.
595 // Additional qualifiers could live in the left subtree or the right
596 // subtree. Find the last piece.
598 struct demangle_component* prev_comp = NULL;
599 struct demangle_component* cur_comp = tree;
601 switch (cur_comp->type)
603 case DEMANGLE_COMPONENT_QUAL_NAME:
604 case DEMANGLE_COMPONENT_LOCAL_NAME:
605 prev_comp = cur_comp;
606 cur_comp = d_right(cur_comp);
608 case DEMANGLE_COMPONENT_TEMPLATE:
609 case DEMANGLE_COMPONENT_NAME:
610 case DEMANGLE_COMPONENT_CTOR:
611 case DEMANGLE_COMPONENT_DTOR:
612 case DEMANGLE_COMPONENT_OPERATOR:
613 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
623 if (cur_comp != NULL && prev_comp != NULL)
625 // We want to discard the rightmost child of PREV_COMP.
626 *prev_comp = *d_left(prev_comp);
627 size_t allocated_size;
628 ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
635 // Guess a fully-qualified name for a class type, based on member function
636 // linkage names. This is needed for class/struct/union types at the
637 // top level, because GCC does not always properly embed them within
638 // the namespace. As in gdb, we look for a member function with a linkage
639 // name and extract the qualified name from the demangled name.
642 Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
644 std::string full_name;
645 off_t next_offset = 0;
647 // This routine scans ahead in the DIE structure, possibly advancing
648 // the relocation tracker beyond the current DIE. We need to checkpoint
649 // the tracker and reset it when we're done.
650 uint64_t checkpoint = this->get_reloc_checkpoint();
652 for (off_t child_offset = die->child_offset();
654 child_offset = next_offset)
656 Dwarf_die child(this, child_offset, die);
657 if (child.tag() == 0)
659 if (child.tag() == elfcpp::DW_TAG_subprogram)
661 const char* linkage_name = child.linkage_name();
662 if (linkage_name != NULL)
664 char* guess = class_name_from_linkage_name(linkage_name);
667 full_name.assign(guess);
673 next_offset = child.sibling_offset();
676 this->reset_relocs(checkpoint);
680 // Add a declaration DIE to the table of declarations.
683 Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
685 const char* name = die->name();
687 off_t parent_offset = context != NULL ? context->offset() : 0;
689 // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
690 // attribute, use the parent and name from the earlier declaration.
691 off_t spec = die->specification();
693 spec = die->abstract_origin();
696 Declaration_map::iterator it = this->declarations_.find(spec);
697 if (it != this->declarations_.end())
699 parent_offset = it->second.parent_offset_;
700 name = it->second.name_;
706 if (die->tag() == elfcpp::DW_TAG_namespace)
707 name = "(anonymous namespace)";
708 else if (die->tag() == elfcpp::DW_TAG_union_type)
709 name = "(anonymous union)";
714 Declaration_pair decl(parent_offset, name);
715 this->declarations_.insert(std::make_pair(die->offset(), decl));
718 // Add a declaration whose fully-qualified name is already known.
719 // In the case where we had to get the canonical name by demangling
720 // a linkage name, this ensures we use that name instead of the one
721 // provided in DW_AT_name.
724 Gdb_index_info_reader::add_declaration_with_full_name(
726 const char* full_name)
728 // We need to copy the name.
729 int len = strlen(full_name);
730 char* copy = new char[len + 1];
731 memcpy(copy, full_name, len + 1);
733 // Flag that we now manage the memory this points to.
734 Declaration_pair decl(-1, copy);
735 this->declarations_.insert(std::make_pair(die->offset(), decl));
738 // Return the context for a DIE whose parent is at DIE_OFFSET.
741 Gdb_index_info_reader::get_context(off_t die_offset)
744 Declaration_map::iterator it = this->declarations_.find(die_offset);
745 if (it != this->declarations_.end())
747 off_t parent_offset = it->second.parent_offset_;
748 if (parent_offset > 0)
750 context = get_context(parent_offset);
751 context.append("::");
753 if (it->second.name_ != NULL)
754 context.append(it->second.name_);
759 // Construct the fully-qualified name for DIE.
762 Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
764 std::string full_name;
765 const char* name = die->name();
767 off_t parent_offset = context != NULL ? context->offset() : 0;
769 // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
770 // attribute, use the parent and name from the earlier declaration.
771 off_t spec = die->specification();
773 spec = die->abstract_origin();
776 Declaration_map::iterator it = this->declarations_.find(spec);
777 if (it != this->declarations_.end())
779 parent_offset = it->second.parent_offset_;
780 name = it->second.name_;
784 if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
785 name = "(anonymous namespace)";
786 else if (name == NULL)
789 // If this is an enumerator constant, skip the immediate parent,
790 // which is the enumeration tag.
791 if (die->tag() == elfcpp::DW_TAG_enumerator)
793 Declaration_map::iterator it = this->declarations_.find(parent_offset);
794 if (it != this->declarations_.end())
795 parent_offset = it->second.parent_offset_;
798 if (parent_offset > 0)
800 full_name.assign(this->get_context(parent_offset));
801 full_name.append("::");
803 full_name.append(name);
808 // Record the address ranges for a compilation unit.
811 Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
816 off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
817 if (ranges_offset != -1)
819 Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
821 this->gdb_index_->add_address_range_list(this->object(),
822 this->cu_index_, ranges);
826 off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
827 off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
830 high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
834 if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
838 gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
839 "are in different sections"),
840 this->object()->name().c_str());
843 if (shndx == 0 || this->object()->is_section_included(shndx))
845 Dwarf_range_list* ranges = new Dwarf_range_list();
846 ranges->add(shndx, low_pc, high_pc);
847 this->gdb_index_->add_address_range_list(this->object(),
848 this->cu_index_, ranges);
853 // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
854 // Returns TRUE if either a pubnames or pubtypes section was found.
857 Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
861 // If we find a DW_AT_GNU_pubnames attribute, read the pubnames table.
862 unsigned int pubnames_shndx;
863 off_t pubnames_offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames,
865 if (pubnames_offset != -1)
867 if (this->gdb_index_->pubnames_read(this->object(), pubnames_shndx,
872 Dwarf_pubnames_table pubnames(this, false);
873 if (!pubnames.read_section(this->object(), pubnames_shndx))
875 if (!pubnames.read_header(pubnames_offset))
879 const char* name = pubnames.next_name();
882 this->gdb_index_->add_symbol(this->cu_index_, name);
888 // If we find a DW_AT_GNU_pubtypes attribute, read the pubtypes table.
889 unsigned int pubtypes_shndx;
890 off_t pubtypes_offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubtypes,
892 if (pubtypes_offset != -1)
894 if (this->gdb_index_->pubtypes_read(this->object(),
895 pubtypes_shndx, pubtypes_offset))
899 Dwarf_pubnames_table pubtypes(this, true);
900 if (!pubtypes.read_section(this->object(), pubtypes_shndx))
902 if (!pubtypes.read_header(pubtypes_offset))
906 const char* name = pubtypes.next_name();
909 this->gdb_index_->add_symbol(this->cu_index_, name);
918 // Clear the declarations map.
920 Gdb_index_info_reader::clear_declarations()
922 // Free strings in memory we manage.
923 for (Declaration_map::iterator it = this->declarations_.begin();
924 it != this->declarations_.end();
927 if (it->second.parent_offset_ == -1)
928 delete[] it->second.name_;
931 this->declarations_.clear();
934 // Print usage statistics.
936 Gdb_index_info_reader::print_stats()
938 fprintf(stderr, _("%s: DWARF CUs: %u\n"),
939 program_name, Gdb_index_info_reader::dwarf_cu_count);
940 fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
941 program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
942 fprintf(stderr, _("%s: DWARF TUs: %u\n"),
943 program_name, Gdb_index_info_reader::dwarf_tu_count);
944 fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
945 program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
950 // Construct the .gdb_index section.
952 Gdb_index::Gdb_index(Output_section* gdb_index_section)
953 : Output_section_data(4),
954 gdb_index_section_(gdb_index_section),
959 cu_vector_offsets_(NULL),
965 stringpool_offset_(0),
966 pubnames_object_(NULL),
969 pubtypes_object_(NULL),
973 this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
976 Gdb_index::~Gdb_index()
978 // Free the memory used by the symbol table.
979 delete this->gdb_symtab_;
980 // Free the memory used by the CU vectors.
981 for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
982 delete this->cu_vector_list_[i];
985 // Scan a .debug_info or .debug_types input section.
988 Gdb_index::scan_debug_info(bool is_type_unit,
990 const unsigned char* symbols,
993 unsigned int reloc_shndx,
994 unsigned int reloc_type)
996 Gdb_index_info_reader dwinfo(is_type_unit, object,
997 symbols, symbols_size,
1006 Gdb_index::add_symbol(int cu_index, const char* sym_name)
1008 unsigned int hash = mapped_index_string_hash(
1009 reinterpret_cast<const unsigned char*>(sym_name));
1010 Gdb_symbol* sym = new Gdb_symbol();
1011 this->stringpool_.add(sym_name, true, &sym->name_key);
1012 sym->hashval = hash;
1013 sym->cu_vector_index = 0;
1015 Gdb_symbol* found = this->gdb_symtab_->add(sym);
1018 // New symbol -- allocate a new CU index vector.
1019 found->cu_vector_index = this->cu_vector_list_.size();
1020 this->cu_vector_list_.push_back(new Cu_vector());
1024 // Found an existing symbol -- append to the existing
1029 // Add the CU index to the vector list for this symbol,
1030 // if it's not already on the list. We only need to
1031 // check the last added entry.
1032 Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
1033 if (cu_vec->size() == 0 || cu_vec->back() != cu_index)
1034 cu_vec->push_back(cu_index);
1037 // Return TRUE if we have already processed the pubnames set at
1038 // OFFSET in section SHNDX
1041 Gdb_index::pubnames_read(const Relobj* object, unsigned int shndx, off_t offset)
1043 bool ret = (this->pubnames_object_ == object
1044 && this->pubnames_shndx_ == shndx
1045 && this->pubnames_offset_ == offset);
1046 this->pubnames_object_ = object;
1047 this->pubnames_shndx_ = shndx;
1048 this->pubnames_offset_ = offset;
1052 // Return TRUE if we have already processed the pubtypes set at
1053 // OFFSET in section SHNDX
1056 Gdb_index::pubtypes_read(const Relobj* object, unsigned int shndx, off_t offset)
1058 bool ret = (this->pubtypes_object_ == object
1059 && this->pubtypes_shndx_ == shndx
1060 && this->pubtypes_offset_ == offset);
1061 this->pubtypes_object_ = object;
1062 this->pubtypes_shndx_ = shndx;
1063 this->pubtypes_offset_ = offset;
1067 // Set the size of the .gdb_index section.
1070 Gdb_index::set_final_data_size()
1072 // Finalize the string pool.
1073 this->stringpool_.set_string_offsets();
1075 // Compute the total size of the CU vectors.
1076 // For each CU vector, include one entry for the count at the
1077 // beginning of the vector.
1078 unsigned int cu_vector_count = this->cu_vector_list_.size();
1079 unsigned int cu_vector_size = 0;
1080 this->cu_vector_offsets_ = new off_t[cu_vector_count];
1081 for (unsigned int i = 0; i < cu_vector_count; ++i)
1083 Cu_vector* cu_vec = this->cu_vector_list_[i];
1084 cu_vector_offsets_[i] = cu_vector_size;
1085 cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
1088 // Assign relative offsets to each portion of the index,
1089 // and find the total size of the section.
1090 section_size_type data_size = gdb_index_hdr_size;
1091 data_size += this->comp_units_.size() * gdb_index_cu_size;
1092 this->tu_offset_ = data_size;
1093 data_size += this->type_units_.size() * gdb_index_tu_size;
1094 this->addr_offset_ = data_size;
1095 for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1096 data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
1097 this->symtab_offset_ = data_size;
1098 data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
1099 this->cu_pool_offset_ = data_size;
1100 data_size += cu_vector_size;
1101 this->stringpool_offset_ = data_size;
1102 data_size += this->stringpool_.get_strtab_size();
1104 this->set_data_size(data_size);
1107 // Write the data to the file.
1110 Gdb_index::do_write(Output_file* of)
1112 const off_t off = this->offset();
1113 const off_t oview_size = this->data_size();
1114 unsigned char* const oview = of->get_output_view(off, oview_size);
1115 unsigned char* pov = oview;
1117 // Write the file header.
1118 // (1) Version number.
1119 elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
1121 // (2) Offset of the CU list.
1122 elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
1124 // (3) Offset of the types CU list.
1125 elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
1127 // (4) Offset of the address area.
1128 elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
1130 // (5) Offset of the symbol table.
1131 elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
1133 // (6) Offset of the constant pool.
1134 elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
1137 gold_assert(pov - oview == gdb_index_hdr_size);
1139 // Write the CU list.
1140 unsigned int comp_units_count = this->comp_units_.size();
1141 for (unsigned int i = 0; i < comp_units_count; ++i)
1143 const Comp_unit& cu = this->comp_units_[i];
1144 elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
1145 elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
1149 gold_assert(pov - oview == this->tu_offset_);
1151 // Write the types CU list.
1152 for (unsigned int i = 0; i < this->type_units_.size(); ++i)
1154 const Type_unit& tu = this->type_units_[i];
1155 elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
1156 elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
1157 elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
1161 gold_assert(pov - oview == this->addr_offset_);
1163 // Write the address area.
1164 for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1166 int cu_index = this->ranges_[i].cu_index;
1167 // Translate negative indexes, which refer to a TU, to a
1168 // logical index into a concatenated CU/TU list.
1170 cu_index = comp_units_count + (-1 - cu_index);
1171 Relobj* object = this->ranges_[i].object;
1172 const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
1173 for (unsigned int j = 0; j < ranges.size(); ++j)
1175 const Dwarf_range_list::Range& range = ranges[j];
1177 if (range.shndx > 0)
1179 const Output_section* os = object->output_section(range.shndx);
1180 base = (os->address()
1181 + object->output_section_offset(range.shndx));
1183 elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
1184 elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
1186 elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
1191 gold_assert(pov - oview == this->symtab_offset_);
1193 // Write the symbol table.
1194 for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
1196 const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
1197 section_offset_type name_offset = 0;
1198 unsigned int cu_vector_offset = 0;
1201 name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
1202 + this->stringpool_offset_ - this->cu_pool_offset_);
1203 cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
1205 elfcpp::Swap<32, false>::writeval(pov, name_offset);
1206 elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
1210 gold_assert(pov - oview == this->cu_pool_offset_);
1212 // Write the CU vectors into the constant pool.
1213 for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
1215 Cu_vector* cu_vec = this->cu_vector_list_[i];
1216 elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
1218 for (unsigned int j = 0; j < cu_vec->size(); ++j)
1220 int cu_index = (*cu_vec)[j];
1222 cu_index = comp_units_count + (-1 - cu_index);
1223 elfcpp::Swap<32, false>::writeval(pov, cu_index);
1228 gold_assert(pov - oview == this->stringpool_offset_);
1230 // Write the strings into the constant pool.
1231 this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
1233 of->write_output_view(off, oview_size, oview);
1236 // Print usage statistics.
1238 Gdb_index::print_stats()
1240 if (parameters->options().gdb_index())
1241 Gdb_index_info_reader::print_stats();
1244 } // End namespace gold.