175fd0b74Schristos // gdb-index.cc -- generate .gdb_index section for fast debug lookup
275fd0b74Schristos
3*e992f068Schristos // Copyright (C) 2012-2022 Free Software Foundation, Inc.
475fd0b74Schristos // Written by Cary Coutant <ccoutant@google.com>.
575fd0b74Schristos
675fd0b74Schristos // This file is part of gold.
775fd0b74Schristos
875fd0b74Schristos // This program is free software; you can redistribute it and/or modify
975fd0b74Schristos // it under the terms of the GNU General Public License as published by
1075fd0b74Schristos // the Free Software Foundation; either version 3 of the License, or
1175fd0b74Schristos // (at your option) any later version.
1275fd0b74Schristos
1375fd0b74Schristos // This program is distributed in the hope that it will be useful,
1475fd0b74Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
1575fd0b74Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1675fd0b74Schristos // GNU General Public License for more details.
1775fd0b74Schristos
1875fd0b74Schristos // You should have received a copy of the GNU General Public License
1975fd0b74Schristos // along with this program; if not, write to the Free Software
2075fd0b74Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2175fd0b74Schristos // MA 02110-1301, USA.
2275fd0b74Schristos
2375fd0b74Schristos #include "gold.h"
2475fd0b74Schristos
2575fd0b74Schristos #include "gdb-index.h"
2675fd0b74Schristos #include "dwarf_reader.h"
2775fd0b74Schristos #include "dwarf.h"
2875fd0b74Schristos #include "object.h"
2975fd0b74Schristos #include "output.h"
3075fd0b74Schristos #include "demangle.h"
3175fd0b74Schristos
3275fd0b74Schristos namespace gold
3375fd0b74Schristos {
3475fd0b74Schristos
3575fd0b74Schristos const int gdb_index_version = 7;
3675fd0b74Schristos
3775fd0b74Schristos // Sizes of various records in the .gdb_index section.
3875fd0b74Schristos const int gdb_index_offset_size = 4;
3975fd0b74Schristos const int gdb_index_hdr_size = 6 * gdb_index_offset_size;
4075fd0b74Schristos const int gdb_index_cu_size = 16;
4175fd0b74Schristos const int gdb_index_tu_size = 24;
4275fd0b74Schristos const int gdb_index_addr_size = 16 + gdb_index_offset_size;
4375fd0b74Schristos const int gdb_index_sym_size = 2 * gdb_index_offset_size;
4475fd0b74Schristos
4575fd0b74Schristos // This class manages the hashed symbol table for the .gdb_index section.
4675fd0b74Schristos // It is essentially equivalent to the hashtab implementation in libiberty,
4775fd0b74Schristos // but is copied into gdb sources and here for compatibility because its
4875fd0b74Schristos // data structure is exposed on disk.
4975fd0b74Schristos
5075fd0b74Schristos template <typename T>
5175fd0b74Schristos class Gdb_hashtab
5275fd0b74Schristos {
5375fd0b74Schristos public:
Gdb_hashtab()5475fd0b74Schristos Gdb_hashtab()
5575fd0b74Schristos : size_(0), capacity_(0), hashtab_(NULL)
5675fd0b74Schristos { }
5775fd0b74Schristos
~Gdb_hashtab()5875fd0b74Schristos ~Gdb_hashtab()
5975fd0b74Schristos {
6075fd0b74Schristos for (size_t i = 0; i < this->capacity_; ++i)
6175fd0b74Schristos if (this->hashtab_[i] != NULL)
6275fd0b74Schristos delete this->hashtab_[i];
6375fd0b74Schristos delete[] this->hashtab_;
6475fd0b74Schristos }
6575fd0b74Schristos
6675fd0b74Schristos // Add a symbol.
6775fd0b74Schristos T*
add(T * symbol)6875fd0b74Schristos add(T* symbol)
6975fd0b74Schristos {
7075fd0b74Schristos // Resize the hash table if necessary.
7175fd0b74Schristos if (4 * this->size_ / 3 >= this->capacity_)
7275fd0b74Schristos this->expand();
7375fd0b74Schristos
7475fd0b74Schristos T** slot = this->find_slot(symbol);
7575fd0b74Schristos if (*slot == NULL)
7675fd0b74Schristos {
7775fd0b74Schristos ++this->size_;
7875fd0b74Schristos *slot = symbol;
7975fd0b74Schristos }
8075fd0b74Schristos
8175fd0b74Schristos return *slot;
8275fd0b74Schristos }
8375fd0b74Schristos
8475fd0b74Schristos // Return the current size.
8575fd0b74Schristos size_t
size() const8675fd0b74Schristos size() const
8775fd0b74Schristos { return this->size_; }
8875fd0b74Schristos
8975fd0b74Schristos // Return the current capacity.
9075fd0b74Schristos size_t
capacity() const9175fd0b74Schristos capacity() const
9275fd0b74Schristos { return this->capacity_; }
9375fd0b74Schristos
9475fd0b74Schristos // Return the contents of slot N.
9575fd0b74Schristos T*
operator [](size_t n)9675fd0b74Schristos operator[](size_t n)
9775fd0b74Schristos { return this->hashtab_[n]; }
9875fd0b74Schristos
9975fd0b74Schristos private:
10075fd0b74Schristos // Find a symbol in the hash table, or return an empty slot if
10175fd0b74Schristos // the symbol is not in the table.
10275fd0b74Schristos T**
find_slot(T * symbol)10375fd0b74Schristos find_slot(T* symbol)
10475fd0b74Schristos {
10575fd0b74Schristos unsigned int index = symbol->hash() & (this->capacity_ - 1);
10675fd0b74Schristos unsigned int step = ((symbol->hash() * 17) & (this->capacity_ - 1)) | 1;
10775fd0b74Schristos
10875fd0b74Schristos for (;;)
10975fd0b74Schristos {
11075fd0b74Schristos if (this->hashtab_[index] == NULL
11175fd0b74Schristos || this->hashtab_[index]->equal(symbol))
11275fd0b74Schristos return &this->hashtab_[index];
11375fd0b74Schristos index = (index + step) & (this->capacity_ - 1);
11475fd0b74Schristos }
11575fd0b74Schristos }
11675fd0b74Schristos
11775fd0b74Schristos // Expand the hash table.
11875fd0b74Schristos void
expand()11975fd0b74Schristos expand()
12075fd0b74Schristos {
12175fd0b74Schristos if (this->capacity_ == 0)
12275fd0b74Schristos {
12375fd0b74Schristos // Allocate the hash table for the first time.
12475fd0b74Schristos this->capacity_ = Gdb_hashtab::initial_size;
12575fd0b74Schristos this->hashtab_ = new T*[this->capacity_];
12675fd0b74Schristos memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
12775fd0b74Schristos }
12875fd0b74Schristos else
12975fd0b74Schristos {
13075fd0b74Schristos // Expand and rehash.
13175fd0b74Schristos unsigned int old_cap = this->capacity_;
13275fd0b74Schristos T** old_hashtab = this->hashtab_;
13375fd0b74Schristos this->capacity_ *= 2;
13475fd0b74Schristos this->hashtab_ = new T*[this->capacity_];
13575fd0b74Schristos memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
13675fd0b74Schristos for (size_t i = 0; i < old_cap; ++i)
13775fd0b74Schristos {
13875fd0b74Schristos if (old_hashtab[i] != NULL)
13975fd0b74Schristos {
14075fd0b74Schristos T** slot = this->find_slot(old_hashtab[i]);
14175fd0b74Schristos *slot = old_hashtab[i];
14275fd0b74Schristos }
14375fd0b74Schristos }
14475fd0b74Schristos delete[] old_hashtab;
14575fd0b74Schristos }
14675fd0b74Schristos }
14775fd0b74Schristos
14875fd0b74Schristos // Initial size of the hash table; must be a power of 2.
14975fd0b74Schristos static const int initial_size = 1024;
15075fd0b74Schristos size_t size_;
15175fd0b74Schristos size_t capacity_;
15275fd0b74Schristos T** hashtab_;
15375fd0b74Schristos };
15475fd0b74Schristos
15575fd0b74Schristos // The hash function for strings in the mapped index. This is copied
15675fd0b74Schristos // directly from gdb/dwarf2read.c.
15775fd0b74Schristos
15875fd0b74Schristos static unsigned int
mapped_index_string_hash(const unsigned char * str)15975fd0b74Schristos mapped_index_string_hash(const unsigned char* str)
16075fd0b74Schristos {
16175fd0b74Schristos unsigned int r = 0;
16275fd0b74Schristos unsigned char c;
16375fd0b74Schristos
16475fd0b74Schristos while ((c = *str++) != 0)
16575fd0b74Schristos {
16675fd0b74Schristos if (gdb_index_version >= 5)
16775fd0b74Schristos c = tolower (c);
16875fd0b74Schristos r = r * 67 + c - 113;
16975fd0b74Schristos }
17075fd0b74Schristos
17175fd0b74Schristos return r;
17275fd0b74Schristos }
17375fd0b74Schristos
17475fd0b74Schristos // A specialization of Dwarf_info_reader, for building the .gdb_index.
17575fd0b74Schristos
17675fd0b74Schristos class Gdb_index_info_reader : public Dwarf_info_reader
17775fd0b74Schristos {
17875fd0b74Schristos public:
Gdb_index_info_reader(bool is_type_unit,Relobj * object,const unsigned char * symbols,off_t symbols_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type,Gdb_index * gdb_index)17975fd0b74Schristos Gdb_index_info_reader(bool is_type_unit,
18075fd0b74Schristos Relobj* object,
18175fd0b74Schristos const unsigned char* symbols,
18275fd0b74Schristos off_t symbols_size,
18375fd0b74Schristos unsigned int shndx,
18475fd0b74Schristos unsigned int reloc_shndx,
18575fd0b74Schristos unsigned int reloc_type,
18675fd0b74Schristos Gdb_index* gdb_index)
18775fd0b74Schristos : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
18875fd0b74Schristos reloc_shndx, reloc_type),
18975fd0b74Schristos gdb_index_(gdb_index), cu_index_(0), cu_language_(0)
19075fd0b74Schristos { }
19175fd0b74Schristos
~Gdb_index_info_reader()19275fd0b74Schristos ~Gdb_index_info_reader()
19375fd0b74Schristos { this->clear_declarations(); }
19475fd0b74Schristos
19575fd0b74Schristos // Print usage statistics.
19675fd0b74Schristos static void
19775fd0b74Schristos print_stats();
19875fd0b74Schristos
19975fd0b74Schristos protected:
20075fd0b74Schristos // Visit a compilation unit.
20175fd0b74Schristos virtual void
20275fd0b74Schristos visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
20375fd0b74Schristos
20475fd0b74Schristos // Visit a type unit.
20575fd0b74Schristos virtual void
20675fd0b74Schristos visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
20775fd0b74Schristos uint64_t signature, Dwarf_die*);
20875fd0b74Schristos
20975fd0b74Schristos private:
21075fd0b74Schristos // A map for recording DIEs we've seen that may be referred to be
21175fd0b74Schristos // later DIEs (via DW_AT_specification or DW_AT_abstract_origin).
21275fd0b74Schristos // The map is indexed by a DIE offset within the compile unit.
21375fd0b74Schristos // PARENT_OFFSET_ is the offset of the DIE that represents the
21475fd0b74Schristos // outer context, and NAME_ is a pointer to a component of the
21575fd0b74Schristos // fully-qualified name.
21675fd0b74Schristos // Normally, the names we point to are in a string table, so we don't
21775fd0b74Schristos // have to manage them, but when we have a fully-qualified name
21875fd0b74Schristos // computed, we put it in the table, and set PARENT_OFFSET_ to -1
21975fd0b74Schristos // indicate a string that we are managing.
22075fd0b74Schristos struct Declaration_pair
22175fd0b74Schristos {
Declaration_pairgold::Gdb_index_info_reader::Declaration_pair22275fd0b74Schristos Declaration_pair(off_t parent_offset, const char* name)
22375fd0b74Schristos : parent_offset_(parent_offset), name_(name)
22475fd0b74Schristos { }
22575fd0b74Schristos
22675fd0b74Schristos off_t parent_offset_;
22775fd0b74Schristos const char* name_;
22875fd0b74Schristos };
22975fd0b74Schristos typedef Unordered_map<off_t, Declaration_pair> Declaration_map;
23075fd0b74Schristos
23175fd0b74Schristos // Visit a top-level DIE.
23275fd0b74Schristos void
23375fd0b74Schristos visit_top_die(Dwarf_die* die);
23475fd0b74Schristos
23575fd0b74Schristos // Visit the children of a DIE.
23675fd0b74Schristos void
23775fd0b74Schristos visit_children(Dwarf_die* die, Dwarf_die* context);
23875fd0b74Schristos
23975fd0b74Schristos // Visit a DIE.
24075fd0b74Schristos void
24175fd0b74Schristos visit_die(Dwarf_die* die, Dwarf_die* context);
24275fd0b74Schristos
24375fd0b74Schristos // Visit the children of a DIE.
24475fd0b74Schristos void
24575fd0b74Schristos visit_children_for_decls(Dwarf_die* die);
24675fd0b74Schristos
24775fd0b74Schristos // Visit a DIE.
24875fd0b74Schristos void
24975fd0b74Schristos visit_die_for_decls(Dwarf_die* die, Dwarf_die* context);
25075fd0b74Schristos
25175fd0b74Schristos // Guess a fully-qualified name for a class type, based on member function
25275fd0b74Schristos // linkage names.
25375fd0b74Schristos std::string
25475fd0b74Schristos guess_full_class_name(Dwarf_die* die);
25575fd0b74Schristos
25675fd0b74Schristos // Add a declaration DIE to the table of declarations.
25775fd0b74Schristos void
25875fd0b74Schristos add_declaration(Dwarf_die* die, Dwarf_die* context);
25975fd0b74Schristos
26075fd0b74Schristos // Add a declaration whose fully-qualified name is already known.
26175fd0b74Schristos void
26275fd0b74Schristos add_declaration_with_full_name(Dwarf_die* die, const char* full_name);
26375fd0b74Schristos
26475fd0b74Schristos // Return the context for a DIE whose parent is at DIE_OFFSET.
26575fd0b74Schristos std::string
26675fd0b74Schristos get_context(off_t die_offset);
26775fd0b74Schristos
26875fd0b74Schristos // Construct a fully-qualified name for DIE.
26975fd0b74Schristos std::string
27075fd0b74Schristos get_qualified_name(Dwarf_die* die, Dwarf_die* context);
27175fd0b74Schristos
27275fd0b74Schristos // Record the address ranges for a compilation unit.
27375fd0b74Schristos void
27475fd0b74Schristos record_cu_ranges(Dwarf_die* die);
27575fd0b74Schristos
27675fd0b74Schristos // Wrapper for read_pubtable.
27775fd0b74Schristos bool
27875fd0b74Schristos read_pubnames_and_pubtypes(Dwarf_die* die);
27975fd0b74Schristos
28075fd0b74Schristos // Read the .debug_pubnames and .debug_pubtypes tables.
28175fd0b74Schristos bool
28275fd0b74Schristos read_pubtable(Dwarf_pubnames_table* table, off_t offset);
28375fd0b74Schristos
28475fd0b74Schristos // Clear the declarations map.
28575fd0b74Schristos void
28675fd0b74Schristos clear_declarations();
28775fd0b74Schristos
28875fd0b74Schristos // The Gdb_index section.
28975fd0b74Schristos Gdb_index* gdb_index_;
29075fd0b74Schristos // The current CU index (negative for a TU).
29175fd0b74Schristos int cu_index_;
29275fd0b74Schristos // The language of the current CU or TU.
29375fd0b74Schristos unsigned int cu_language_;
29475fd0b74Schristos // Map from DIE offset to (parent offset, name) pair,
29575fd0b74Schristos // for DW_AT_specification.
29675fd0b74Schristos Declaration_map declarations_;
29775fd0b74Schristos
29875fd0b74Schristos // Statistics.
29975fd0b74Schristos // Total number of DWARF compilation units processed.
30075fd0b74Schristos static unsigned int dwarf_cu_count;
30175fd0b74Schristos // Number of DWARF compilation units with pubnames/pubtypes.
30275fd0b74Schristos static unsigned int dwarf_cu_nopubnames_count;
30375fd0b74Schristos // Total number of DWARF type units processed.
30475fd0b74Schristos static unsigned int dwarf_tu_count;
30575fd0b74Schristos // Number of DWARF type units with pubnames/pubtypes.
30675fd0b74Schristos static unsigned int dwarf_tu_nopubnames_count;
30775fd0b74Schristos };
30875fd0b74Schristos
30975fd0b74Schristos // Total number of DWARF compilation units processed.
31075fd0b74Schristos unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
31175fd0b74Schristos // Number of DWARF compilation units without pubnames/pubtypes.
31275fd0b74Schristos unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
31375fd0b74Schristos // Total number of DWARF type units processed.
31475fd0b74Schristos unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
31575fd0b74Schristos // Number of DWARF type units without pubnames/pubtypes.
31675fd0b74Schristos unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
31775fd0b74Schristos
31875fd0b74Schristos // Process a compilation unit and parse its child DIE.
31975fd0b74Schristos
32075fd0b74Schristos void
visit_compilation_unit(off_t cu_offset,off_t cu_length,Dwarf_die * root_die)32175fd0b74Schristos Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
32275fd0b74Schristos Dwarf_die* root_die)
32375fd0b74Schristos {
32475fd0b74Schristos ++Gdb_index_info_reader::dwarf_cu_count;
32575fd0b74Schristos this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
32675fd0b74Schristos this->visit_top_die(root_die);
32775fd0b74Schristos }
32875fd0b74Schristos
32975fd0b74Schristos // Process a type unit and parse its child DIE.
33075fd0b74Schristos
33175fd0b74Schristos void
visit_type_unit(off_t tu_offset,off_t,off_t type_offset,uint64_t signature,Dwarf_die * root_die)33275fd0b74Schristos Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t,
33375fd0b74Schristos off_t type_offset, uint64_t signature,
33475fd0b74Schristos Dwarf_die* root_die)
33575fd0b74Schristos {
33675fd0b74Schristos ++Gdb_index_info_reader::dwarf_tu_count;
33775fd0b74Schristos // Use a negative index to flag this as a TU instead of a CU.
33875fd0b74Schristos this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
33975fd0b74Schristos signature);
34075fd0b74Schristos this->visit_top_die(root_die);
34175fd0b74Schristos }
34275fd0b74Schristos
34375fd0b74Schristos // Process a top-level DIE.
34475fd0b74Schristos // For compile_unit DIEs, record the address ranges. For all
34575fd0b74Schristos // interesting tags, add qualified names to the symbol table
34675fd0b74Schristos // and process interesting children. We may need to process
34775fd0b74Schristos // certain children just for saving declarations that might be
34875fd0b74Schristos // referenced by later DIEs with a DW_AT_specification attribute.
34975fd0b74Schristos
35075fd0b74Schristos void
visit_top_die(Dwarf_die * die)35175fd0b74Schristos Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
35275fd0b74Schristos {
35375fd0b74Schristos this->clear_declarations();
35475fd0b74Schristos
35575fd0b74Schristos switch (die->tag())
35675fd0b74Schristos {
35775fd0b74Schristos case elfcpp::DW_TAG_compile_unit:
35875fd0b74Schristos case elfcpp::DW_TAG_type_unit:
35975fd0b74Schristos this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
36075fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_compile_unit)
36175fd0b74Schristos this->record_cu_ranges(die);
36275fd0b74Schristos // If there is a pubnames and/or pubtypes section for this
36375fd0b74Schristos // compilation unit, use those; otherwise, parse the DWARF
36475fd0b74Schristos // info to extract the names.
36575fd0b74Schristos if (!this->read_pubnames_and_pubtypes(die))
36675fd0b74Schristos {
36775fd0b74Schristos // Check for languages that require specialized knowledge to
36875fd0b74Schristos // construct fully-qualified names, that we don't yet support.
36975fd0b74Schristos if (this->cu_language_ == elfcpp::DW_LANG_Ada83
37075fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Fortran77
37175fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Fortran90
37275fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Java
37375fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Ada95
37475fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Fortran95
37575fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Fortran03
37675fd0b74Schristos || this->cu_language_ == elfcpp::DW_LANG_Fortran08)
37775fd0b74Schristos {
37875fd0b74Schristos gold_warning(_("%s: --gdb-index currently supports "
37975fd0b74Schristos "only C and C++ languages"),
38075fd0b74Schristos this->object()->name().c_str());
38175fd0b74Schristos return;
38275fd0b74Schristos }
38375fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_compile_unit)
38475fd0b74Schristos ++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
38575fd0b74Schristos else
38675fd0b74Schristos ++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
38775fd0b74Schristos this->visit_children(die, NULL);
38875fd0b74Schristos }
38975fd0b74Schristos break;
39075fd0b74Schristos default:
39175fd0b74Schristos // The top level DIE should be one of the above.
39275fd0b74Schristos gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
39375fd0b74Schristos "or DW_TAG_type_unit"),
39475fd0b74Schristos this->object()->name().c_str());
39575fd0b74Schristos return;
39675fd0b74Schristos }
39775fd0b74Schristos }
39875fd0b74Schristos
39975fd0b74Schristos // Visit the children of PARENT, looking for symbols to add to the index.
40075fd0b74Schristos // CONTEXT points to the DIE to use for constructing the qualified name --
40175fd0b74Schristos // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
40275fd0b74Schristos
40375fd0b74Schristos void
visit_children(Dwarf_die * parent,Dwarf_die * context)40475fd0b74Schristos Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
40575fd0b74Schristos {
40675fd0b74Schristos off_t next_offset = 0;
40775fd0b74Schristos for (off_t die_offset = parent->child_offset();
40875fd0b74Schristos die_offset != 0;
40975fd0b74Schristos die_offset = next_offset)
41075fd0b74Schristos {
41175fd0b74Schristos Dwarf_die die(this, die_offset, parent);
41275fd0b74Schristos if (die.tag() == 0)
41375fd0b74Schristos break;
41475fd0b74Schristos this->visit_die(&die, context);
41575fd0b74Schristos next_offset = die.sibling_offset();
41675fd0b74Schristos }
41775fd0b74Schristos }
41875fd0b74Schristos
41975fd0b74Schristos // Visit a child DIE, looking for symbols to add to the index.
42075fd0b74Schristos // CONTEXT is the parent DIE, used for constructing the qualified name;
42175fd0b74Schristos // it is NULL if the parent DIE is the top-level DIE.
42275fd0b74Schristos
42375fd0b74Schristos void
visit_die(Dwarf_die * die,Dwarf_die * context)42475fd0b74Schristos Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
42575fd0b74Schristos {
42675fd0b74Schristos switch (die->tag())
42775fd0b74Schristos {
42875fd0b74Schristos case elfcpp::DW_TAG_subprogram:
42975fd0b74Schristos case elfcpp::DW_TAG_constant:
43075fd0b74Schristos case elfcpp::DW_TAG_variable:
43175fd0b74Schristos case elfcpp::DW_TAG_enumerator:
43275fd0b74Schristos case elfcpp::DW_TAG_base_type:
43375fd0b74Schristos if (die->is_declaration())
43475fd0b74Schristos this->add_declaration(die, context);
43575fd0b74Schristos else
43675fd0b74Schristos {
43775fd0b74Schristos // If the DIE is not a declaration, add it to the index.
43875fd0b74Schristos std::string full_name = this->get_qualified_name(die, context);
43975fd0b74Schristos if (!full_name.empty())
44075fd0b74Schristos this->gdb_index_->add_symbol(this->cu_index_,
44175fd0b74Schristos full_name.c_str(), 0);
44275fd0b74Schristos }
44375fd0b74Schristos break;
44475fd0b74Schristos case elfcpp::DW_TAG_typedef:
44575fd0b74Schristos case elfcpp::DW_TAG_union_type:
44675fd0b74Schristos case elfcpp::DW_TAG_class_type:
44775fd0b74Schristos case elfcpp::DW_TAG_interface_type:
44875fd0b74Schristos case elfcpp::DW_TAG_structure_type:
44975fd0b74Schristos case elfcpp::DW_TAG_enumeration_type:
45075fd0b74Schristos case elfcpp::DW_TAG_subrange_type:
45175fd0b74Schristos case elfcpp::DW_TAG_namespace:
45275fd0b74Schristos {
45375fd0b74Schristos std::string full_name;
45475fd0b74Schristos
45575fd0b74Schristos // For classes at the top level, we need to look for a
45675fd0b74Schristos // member function with a linkage name in order to get
45775fd0b74Schristos // the properly-canonicalized name.
45875fd0b74Schristos if (context == NULL
45975fd0b74Schristos && (die->tag() == elfcpp::DW_TAG_class_type
46075fd0b74Schristos || die->tag() == elfcpp::DW_TAG_structure_type
46175fd0b74Schristos || die->tag() == elfcpp::DW_TAG_union_type))
46275fd0b74Schristos full_name.assign(this->guess_full_class_name(die));
46375fd0b74Schristos
46475fd0b74Schristos // Because we will visit the children, we need to add this DIE
46575fd0b74Schristos // to the declarations table.
46675fd0b74Schristos if (full_name.empty())
46775fd0b74Schristos this->add_declaration(die, context);
46875fd0b74Schristos else
46975fd0b74Schristos this->add_declaration_with_full_name(die, full_name.c_str());
47075fd0b74Schristos
47175fd0b74Schristos // If the DIE is not a declaration, add it to the index.
47275fd0b74Schristos // Gdb stores a namespace in the index even when it is
47375fd0b74Schristos // a declaration.
47475fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_namespace
47575fd0b74Schristos || !die->is_declaration())
47675fd0b74Schristos {
47775fd0b74Schristos if (full_name.empty())
47875fd0b74Schristos full_name = this->get_qualified_name(die, context);
47975fd0b74Schristos if (!full_name.empty())
48075fd0b74Schristos this->gdb_index_->add_symbol(this->cu_index_,
48175fd0b74Schristos full_name.c_str(), 0);
48275fd0b74Schristos }
48375fd0b74Schristos
48475fd0b74Schristos // We're interested in the children only for namespaces and
48575fd0b74Schristos // enumeration types. For enumeration types, we do not include
48675fd0b74Schristos // the enumeration tag as part of the full name. For other tags,
48775fd0b74Schristos // visit the children only to collect declarations.
48875fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_namespace
48975fd0b74Schristos || die->tag() == elfcpp::DW_TAG_enumeration_type)
49075fd0b74Schristos this->visit_children(die, die);
49175fd0b74Schristos else
49275fd0b74Schristos this->visit_children_for_decls(die);
49375fd0b74Schristos }
49475fd0b74Schristos break;
49575fd0b74Schristos default:
49675fd0b74Schristos break;
49775fd0b74Schristos }
49875fd0b74Schristos }
49975fd0b74Schristos
50075fd0b74Schristos // Visit the children of PARENT, looking only for declarations that
50175fd0b74Schristos // may be referenced by later specification DIEs.
50275fd0b74Schristos
50375fd0b74Schristos void
visit_children_for_decls(Dwarf_die * parent)50475fd0b74Schristos Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
50575fd0b74Schristos {
50675fd0b74Schristos off_t next_offset = 0;
50775fd0b74Schristos for (off_t die_offset = parent->child_offset();
50875fd0b74Schristos die_offset != 0;
50975fd0b74Schristos die_offset = next_offset)
51075fd0b74Schristos {
51175fd0b74Schristos Dwarf_die die(this, die_offset, parent);
51275fd0b74Schristos if (die.tag() == 0)
51375fd0b74Schristos break;
51475fd0b74Schristos this->visit_die_for_decls(&die, parent);
51575fd0b74Schristos next_offset = die.sibling_offset();
51675fd0b74Schristos }
51775fd0b74Schristos }
51875fd0b74Schristos
51975fd0b74Schristos // Visit a child DIE, looking only for declarations that
52075fd0b74Schristos // may be referenced by later specification DIEs.
52175fd0b74Schristos
52275fd0b74Schristos void
visit_die_for_decls(Dwarf_die * die,Dwarf_die * context)52375fd0b74Schristos Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
52475fd0b74Schristos {
52575fd0b74Schristos switch (die->tag())
52675fd0b74Schristos {
52775fd0b74Schristos case elfcpp::DW_TAG_subprogram:
52875fd0b74Schristos case elfcpp::DW_TAG_constant:
52975fd0b74Schristos case elfcpp::DW_TAG_variable:
53075fd0b74Schristos case elfcpp::DW_TAG_enumerator:
53175fd0b74Schristos case elfcpp::DW_TAG_base_type:
53275fd0b74Schristos {
53375fd0b74Schristos if (die->is_declaration())
53475fd0b74Schristos this->add_declaration(die, context);
53575fd0b74Schristos }
53675fd0b74Schristos break;
53775fd0b74Schristos case elfcpp::DW_TAG_typedef:
53875fd0b74Schristos case elfcpp::DW_TAG_union_type:
53975fd0b74Schristos case elfcpp::DW_TAG_class_type:
54075fd0b74Schristos case elfcpp::DW_TAG_interface_type:
54175fd0b74Schristos case elfcpp::DW_TAG_structure_type:
54275fd0b74Schristos case elfcpp::DW_TAG_enumeration_type:
54375fd0b74Schristos case elfcpp::DW_TAG_subrange_type:
54475fd0b74Schristos case elfcpp::DW_TAG_namespace:
54575fd0b74Schristos {
54675fd0b74Schristos if (die->is_declaration())
54775fd0b74Schristos this->add_declaration(die, context);
54875fd0b74Schristos this->visit_children_for_decls(die);
54975fd0b74Schristos }
55075fd0b74Schristos break;
55175fd0b74Schristos default:
55275fd0b74Schristos break;
55375fd0b74Schristos }
55475fd0b74Schristos }
55575fd0b74Schristos
55675fd0b74Schristos // Extract the class name from the linkage name of a member function.
55775fd0b74Schristos // This code is adapted from ../gdb/cp-support.c.
55875fd0b74Schristos
55975fd0b74Schristos #define d_left(dc) (dc)->u.s_binary.left
56075fd0b74Schristos #define d_right(dc) (dc)->u.s_binary.right
56175fd0b74Schristos
56275fd0b74Schristos static char*
class_name_from_linkage_name(const char * linkage_name)56375fd0b74Schristos class_name_from_linkage_name(const char* linkage_name)
56475fd0b74Schristos {
56575fd0b74Schristos void* storage;
56675fd0b74Schristos struct demangle_component* tree =
56775fd0b74Schristos cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
56875fd0b74Schristos if (tree == NULL)
56975fd0b74Schristos return NULL;
57075fd0b74Schristos
57175fd0b74Schristos int done = 0;
57275fd0b74Schristos
57375fd0b74Schristos // First strip off any qualifiers, if we have a function or
57475fd0b74Schristos // method.
57575fd0b74Schristos while (!done)
57675fd0b74Schristos switch (tree->type)
57775fd0b74Schristos {
57875fd0b74Schristos case DEMANGLE_COMPONENT_CONST:
57975fd0b74Schristos case DEMANGLE_COMPONENT_RESTRICT:
58075fd0b74Schristos case DEMANGLE_COMPONENT_VOLATILE:
58175fd0b74Schristos case DEMANGLE_COMPONENT_CONST_THIS:
58275fd0b74Schristos case DEMANGLE_COMPONENT_RESTRICT_THIS:
58375fd0b74Schristos case DEMANGLE_COMPONENT_VOLATILE_THIS:
58475fd0b74Schristos case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
58575fd0b74Schristos tree = d_left(tree);
58675fd0b74Schristos break;
58775fd0b74Schristos default:
58875fd0b74Schristos done = 1;
58975fd0b74Schristos break;
59075fd0b74Schristos }
59175fd0b74Schristos
59275fd0b74Schristos // If what we have now is a function, discard the argument list.
59375fd0b74Schristos if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
59475fd0b74Schristos tree = d_left(tree);
59575fd0b74Schristos
59675fd0b74Schristos // If what we have now is a template, strip off the template
59775fd0b74Schristos // arguments. The left subtree may be a qualified name.
59875fd0b74Schristos if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
59975fd0b74Schristos tree = d_left(tree);
60075fd0b74Schristos
60175fd0b74Schristos // What we have now should be a name, possibly qualified.
60275fd0b74Schristos // Additional qualifiers could live in the left subtree or the right
60375fd0b74Schristos // subtree. Find the last piece.
60475fd0b74Schristos done = 0;
60575fd0b74Schristos struct demangle_component* prev_comp = NULL;
60675fd0b74Schristos struct demangle_component* cur_comp = tree;
60775fd0b74Schristos while (!done)
60875fd0b74Schristos switch (cur_comp->type)
60975fd0b74Schristos {
61075fd0b74Schristos case DEMANGLE_COMPONENT_QUAL_NAME:
61175fd0b74Schristos case DEMANGLE_COMPONENT_LOCAL_NAME:
61275fd0b74Schristos prev_comp = cur_comp;
61375fd0b74Schristos cur_comp = d_right(cur_comp);
61475fd0b74Schristos break;
61575fd0b74Schristos case DEMANGLE_COMPONENT_TEMPLATE:
61675fd0b74Schristos case DEMANGLE_COMPONENT_NAME:
61775fd0b74Schristos case DEMANGLE_COMPONENT_CTOR:
61875fd0b74Schristos case DEMANGLE_COMPONENT_DTOR:
61975fd0b74Schristos case DEMANGLE_COMPONENT_OPERATOR:
62075fd0b74Schristos case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
62175fd0b74Schristos done = 1;
62275fd0b74Schristos break;
62375fd0b74Schristos default:
62475fd0b74Schristos done = 1;
62575fd0b74Schristos cur_comp = NULL;
62675fd0b74Schristos break;
62775fd0b74Schristos }
62875fd0b74Schristos
62975fd0b74Schristos char* ret = NULL;
63075fd0b74Schristos if (cur_comp != NULL && prev_comp != NULL)
63175fd0b74Schristos {
63275fd0b74Schristos // We want to discard the rightmost child of PREV_COMP.
63375fd0b74Schristos *prev_comp = *d_left(prev_comp);
63475fd0b74Schristos size_t allocated_size;
63575fd0b74Schristos ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
63675fd0b74Schristos }
63775fd0b74Schristos
63875fd0b74Schristos free(storage);
63975fd0b74Schristos return ret;
64075fd0b74Schristos }
64175fd0b74Schristos
64275fd0b74Schristos // Guess a fully-qualified name for a class type, based on member function
64375fd0b74Schristos // linkage names. This is needed for class/struct/union types at the
64475fd0b74Schristos // top level, because GCC does not always properly embed them within
64575fd0b74Schristos // the namespace. As in gdb, we look for a member function with a linkage
64675fd0b74Schristos // name and extract the qualified name from the demangled name.
64775fd0b74Schristos
64875fd0b74Schristos std::string
guess_full_class_name(Dwarf_die * die)64975fd0b74Schristos Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
65075fd0b74Schristos {
65175fd0b74Schristos std::string full_name;
65275fd0b74Schristos off_t next_offset = 0;
65375fd0b74Schristos
65475fd0b74Schristos // This routine scans ahead in the DIE structure, possibly advancing
65575fd0b74Schristos // the relocation tracker beyond the current DIE. We need to checkpoint
65675fd0b74Schristos // the tracker and reset it when we're done.
65775fd0b74Schristos uint64_t checkpoint = this->get_reloc_checkpoint();
65875fd0b74Schristos
65975fd0b74Schristos for (off_t child_offset = die->child_offset();
66075fd0b74Schristos child_offset != 0;
66175fd0b74Schristos child_offset = next_offset)
66275fd0b74Schristos {
66375fd0b74Schristos Dwarf_die child(this, child_offset, die);
66475fd0b74Schristos if (child.tag() == 0)
66575fd0b74Schristos break;
66675fd0b74Schristos if (child.tag() == elfcpp::DW_TAG_subprogram)
66775fd0b74Schristos {
66875fd0b74Schristos const char* linkage_name = child.linkage_name();
66975fd0b74Schristos if (linkage_name != NULL)
67075fd0b74Schristos {
67175fd0b74Schristos char* guess = class_name_from_linkage_name(linkage_name);
67275fd0b74Schristos if (guess != NULL)
67375fd0b74Schristos {
67475fd0b74Schristos full_name.assign(guess);
67575fd0b74Schristos free(guess);
67675fd0b74Schristos break;
67775fd0b74Schristos }
67875fd0b74Schristos }
67975fd0b74Schristos }
68075fd0b74Schristos next_offset = child.sibling_offset();
68175fd0b74Schristos }
68275fd0b74Schristos
68375fd0b74Schristos this->reset_relocs(checkpoint);
68475fd0b74Schristos return full_name;
68575fd0b74Schristos }
68675fd0b74Schristos
68775fd0b74Schristos // Add a declaration DIE to the table of declarations.
68875fd0b74Schristos
68975fd0b74Schristos void
add_declaration(Dwarf_die * die,Dwarf_die * context)69075fd0b74Schristos Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
69175fd0b74Schristos {
69275fd0b74Schristos const char* name = die->name();
69375fd0b74Schristos
69475fd0b74Schristos off_t parent_offset = context != NULL ? context->offset() : 0;
69575fd0b74Schristos
69675fd0b74Schristos // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
69775fd0b74Schristos // attribute, use the parent and name from the earlier declaration.
69875fd0b74Schristos off_t spec = die->specification();
69975fd0b74Schristos if (spec == 0)
70075fd0b74Schristos spec = die->abstract_origin();
70175fd0b74Schristos if (spec > 0)
70275fd0b74Schristos {
70375fd0b74Schristos Declaration_map::iterator it = this->declarations_.find(spec);
70475fd0b74Schristos if (it != this->declarations_.end())
70575fd0b74Schristos {
70675fd0b74Schristos parent_offset = it->second.parent_offset_;
70775fd0b74Schristos name = it->second.name_;
70875fd0b74Schristos }
70975fd0b74Schristos }
71075fd0b74Schristos
71175fd0b74Schristos if (name == NULL)
71275fd0b74Schristos {
71375fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_namespace)
71475fd0b74Schristos name = "(anonymous namespace)";
71575fd0b74Schristos else if (die->tag() == elfcpp::DW_TAG_union_type)
71675fd0b74Schristos name = "(anonymous union)";
71775fd0b74Schristos else
71875fd0b74Schristos name = "(unknown)";
71975fd0b74Schristos }
72075fd0b74Schristos
72175fd0b74Schristos Declaration_pair decl(parent_offset, name);
72275fd0b74Schristos this->declarations_.insert(std::make_pair(die->offset(), decl));
72375fd0b74Schristos }
72475fd0b74Schristos
72575fd0b74Schristos // Add a declaration whose fully-qualified name is already known.
72675fd0b74Schristos // In the case where we had to get the canonical name by demangling
72775fd0b74Schristos // a linkage name, this ensures we use that name instead of the one
72875fd0b74Schristos // provided in DW_AT_name.
72975fd0b74Schristos
73075fd0b74Schristos void
add_declaration_with_full_name(Dwarf_die * die,const char * full_name)73175fd0b74Schristos Gdb_index_info_reader::add_declaration_with_full_name(
73275fd0b74Schristos Dwarf_die* die,
73375fd0b74Schristos const char* full_name)
73475fd0b74Schristos {
73575fd0b74Schristos // We need to copy the name.
73675fd0b74Schristos int len = strlen(full_name);
73775fd0b74Schristos char* copy = new char[len + 1];
73875fd0b74Schristos memcpy(copy, full_name, len + 1);
73975fd0b74Schristos
74075fd0b74Schristos // Flag that we now manage the memory this points to.
74175fd0b74Schristos Declaration_pair decl(-1, copy);
74275fd0b74Schristos this->declarations_.insert(std::make_pair(die->offset(), decl));
74375fd0b74Schristos }
74475fd0b74Schristos
74575fd0b74Schristos // Return the context for a DIE whose parent is at DIE_OFFSET.
74675fd0b74Schristos
74775fd0b74Schristos std::string
get_context(off_t die_offset)74875fd0b74Schristos Gdb_index_info_reader::get_context(off_t die_offset)
74975fd0b74Schristos {
75075fd0b74Schristos std::string context;
75175fd0b74Schristos Declaration_map::iterator it = this->declarations_.find(die_offset);
75275fd0b74Schristos if (it != this->declarations_.end())
75375fd0b74Schristos {
75475fd0b74Schristos off_t parent_offset = it->second.parent_offset_;
75575fd0b74Schristos if (parent_offset > 0)
75675fd0b74Schristos {
75775fd0b74Schristos context = get_context(parent_offset);
75875fd0b74Schristos context.append("::");
75975fd0b74Schristos }
76075fd0b74Schristos if (it->second.name_ != NULL)
76175fd0b74Schristos context.append(it->second.name_);
76275fd0b74Schristos }
76375fd0b74Schristos return context;
76475fd0b74Schristos }
76575fd0b74Schristos
76675fd0b74Schristos // Construct the fully-qualified name for DIE.
76775fd0b74Schristos
76875fd0b74Schristos std::string
get_qualified_name(Dwarf_die * die,Dwarf_die * context)76975fd0b74Schristos Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
77075fd0b74Schristos {
77175fd0b74Schristos std::string full_name;
77275fd0b74Schristos const char* name = die->name();
77375fd0b74Schristos
77475fd0b74Schristos off_t parent_offset = context != NULL ? context->offset() : 0;
77575fd0b74Schristos
77675fd0b74Schristos // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
77775fd0b74Schristos // attribute, use the parent and name from the earlier declaration.
77875fd0b74Schristos off_t spec = die->specification();
77975fd0b74Schristos if (spec == 0)
78075fd0b74Schristos spec = die->abstract_origin();
78175fd0b74Schristos if (spec > 0)
78275fd0b74Schristos {
78375fd0b74Schristos Declaration_map::iterator it = this->declarations_.find(spec);
78475fd0b74Schristos if (it != this->declarations_.end())
78575fd0b74Schristos {
78675fd0b74Schristos parent_offset = it->second.parent_offset_;
78775fd0b74Schristos name = it->second.name_;
78875fd0b74Schristos }
78975fd0b74Schristos }
79075fd0b74Schristos
79175fd0b74Schristos if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
79275fd0b74Schristos name = "(anonymous namespace)";
79375fd0b74Schristos else if (name == NULL)
79475fd0b74Schristos return full_name;
79575fd0b74Schristos
79675fd0b74Schristos // If this is an enumerator constant, skip the immediate parent,
79775fd0b74Schristos // which is the enumeration tag.
79875fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_enumerator)
79975fd0b74Schristos {
80075fd0b74Schristos Declaration_map::iterator it = this->declarations_.find(parent_offset);
80175fd0b74Schristos if (it != this->declarations_.end())
80275fd0b74Schristos parent_offset = it->second.parent_offset_;
80375fd0b74Schristos }
80475fd0b74Schristos
80575fd0b74Schristos if (parent_offset > 0)
80675fd0b74Schristos {
80775fd0b74Schristos full_name.assign(this->get_context(parent_offset));
80875fd0b74Schristos full_name.append("::");
80975fd0b74Schristos }
81075fd0b74Schristos full_name.append(name);
81175fd0b74Schristos
81275fd0b74Schristos return full_name;
81375fd0b74Schristos }
81475fd0b74Schristos
81575fd0b74Schristos // Record the address ranges for a compilation unit.
81675fd0b74Schristos
81775fd0b74Schristos void
record_cu_ranges(Dwarf_die * die)81875fd0b74Schristos Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
81975fd0b74Schristos {
82075fd0b74Schristos unsigned int shndx;
82175fd0b74Schristos unsigned int shndx2;
82275fd0b74Schristos
82375fd0b74Schristos off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
82475fd0b74Schristos if (ranges_offset != -1)
82575fd0b74Schristos {
82675fd0b74Schristos Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
82775fd0b74Schristos if (ranges != NULL)
82875fd0b74Schristos this->gdb_index_->add_address_range_list(this->object(),
82975fd0b74Schristos this->cu_index_, ranges);
83075fd0b74Schristos return;
83175fd0b74Schristos }
83275fd0b74Schristos
83375fd0b74Schristos off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
83475fd0b74Schristos off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
83575fd0b74Schristos if (high_pc == -1)
83675fd0b74Schristos {
83775fd0b74Schristos high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
83875fd0b74Schristos high_pc += low_pc;
83975fd0b74Schristos shndx2 = shndx;
84075fd0b74Schristos }
84175fd0b74Schristos if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
84275fd0b74Schristos {
84375fd0b74Schristos if (shndx != shndx2)
84475fd0b74Schristos {
84575fd0b74Schristos gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
84675fd0b74Schristos "are in different sections"),
84775fd0b74Schristos this->object()->name().c_str());
84875fd0b74Schristos return;
84975fd0b74Schristos }
85075fd0b74Schristos if (shndx == 0 || this->object()->is_section_included(shndx))
85175fd0b74Schristos {
85275fd0b74Schristos Dwarf_range_list* ranges = new Dwarf_range_list();
85375fd0b74Schristos ranges->add(shndx, low_pc, high_pc);
85475fd0b74Schristos this->gdb_index_->add_address_range_list(this->object(),
85575fd0b74Schristos this->cu_index_, ranges);
85675fd0b74Schristos }
85775fd0b74Schristos }
85875fd0b74Schristos }
85975fd0b74Schristos
86075fd0b74Schristos // Read table and add the relevant names to the index. Returns true
86175fd0b74Schristos // if any names were added.
86275fd0b74Schristos
86375fd0b74Schristos bool
read_pubtable(Dwarf_pubnames_table * table,off_t offset)86475fd0b74Schristos Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table* table, off_t offset)
86575fd0b74Schristos {
86675fd0b74Schristos // If we couldn't read the section when building the cu_pubname_map,
86775fd0b74Schristos // then we won't find any pubnames now.
86875fd0b74Schristos if (table == NULL)
86975fd0b74Schristos return false;
87075fd0b74Schristos
87175fd0b74Schristos if (!table->read_header(offset))
87275fd0b74Schristos return false;
87375fd0b74Schristos while (true)
87475fd0b74Schristos {
87575fd0b74Schristos uint8_t flag_byte;
87675fd0b74Schristos const char* name = table->next_name(&flag_byte);
87775fd0b74Schristos if (name == NULL)
87875fd0b74Schristos break;
87975fd0b74Schristos
88075fd0b74Schristos this->gdb_index_->add_symbol(this->cu_index_, name, flag_byte);
88175fd0b74Schristos }
88275fd0b74Schristos return true;
88375fd0b74Schristos }
88475fd0b74Schristos
88575fd0b74Schristos // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
88675fd0b74Schristos // Returns TRUE if either a pubnames or pubtypes section was found.
88775fd0b74Schristos
88875fd0b74Schristos bool
read_pubnames_and_pubtypes(Dwarf_die * die)88975fd0b74Schristos Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
89075fd0b74Schristos {
89175fd0b74Schristos // If this is a skeleton debug-type die (generated via
89275fd0b74Schristos // -gsplit-dwarf), then the associated pubnames should have been
89375fd0b74Schristos // read along with the corresponding CU. In any case, there isn't
89475fd0b74Schristos // enough info inside to build a gdb index entry.
89575fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_type_unit
89675fd0b74Schristos && die->string_attribute(elfcpp::DW_AT_GNU_dwo_name))
89775fd0b74Schristos return true;
89875fd0b74Schristos
89975fd0b74Schristos // We use stmt_list_off as a unique identifier for the
90075fd0b74Schristos // compilation unit and its associated type units.
90175fd0b74Schristos unsigned int shndx;
90275fd0b74Schristos off_t stmt_list_off = die->ref_attribute (elfcpp::DW_AT_stmt_list,
90375fd0b74Schristos &shndx);
90475fd0b74Schristos // Look for the attr as either a flag or a ref.
90575fd0b74Schristos off_t offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames, &shndx);
90675fd0b74Schristos
90775fd0b74Schristos // Newer versions of GCC generate CUs, but not TUs, with
90875fd0b74Schristos // DW_AT_FORM_flag_present.
90975fd0b74Schristos unsigned int flag = die->uint_attribute(elfcpp::DW_AT_GNU_pubnames);
91075fd0b74Schristos if (offset == -1 && flag == 0)
91175fd0b74Schristos {
91275fd0b74Schristos // Didn't find the attribute.
91375fd0b74Schristos if (die->tag() == elfcpp::DW_TAG_type_unit)
91475fd0b74Schristos {
91575fd0b74Schristos // If die is a TU, then it might correspond to a CU which we
91675fd0b74Schristos // have read. If it does, then no need to read the pubnames.
91775fd0b74Schristos // If it doesn't, then the caller will have to parse the
91875fd0b74Schristos // dies manually to find the names.
91975fd0b74Schristos return this->gdb_index_->pubnames_read(this->object(),
92075fd0b74Schristos stmt_list_off);
92175fd0b74Schristos }
92275fd0b74Schristos else
92375fd0b74Schristos {
92475fd0b74Schristos // No attribute on the CU means that no pubnames were read.
92575fd0b74Schristos return false;
92675fd0b74Schristos }
92775fd0b74Schristos }
92875fd0b74Schristos
92975fd0b74Schristos // We found the attribute, so we can check if the corresponding
93075fd0b74Schristos // pubnames have been read.
93175fd0b74Schristos if (this->gdb_index_->pubnames_read(this->object(), stmt_list_off))
93275fd0b74Schristos return true;
93375fd0b74Schristos
93475fd0b74Schristos this->gdb_index_->set_pubnames_read(this->object(), stmt_list_off);
93575fd0b74Schristos
93675fd0b74Schristos // We have an attribute, and the pubnames haven't been read, so read
93775fd0b74Schristos // them.
93875fd0b74Schristos bool names = false;
93975fd0b74Schristos // In some of the cases, we could rely on the previous value of
94075fd0b74Schristos // offset here, but sorting out which cases complicates the logic
94175fd0b74Schristos // enough that it isn't worth it. So just look up the offset again.
94275fd0b74Schristos offset = this->gdb_index_->find_pubname_offset(this->cu_offset());
94375fd0b74Schristos names = this->read_pubtable(this->gdb_index_->pubnames_table(), offset);
94475fd0b74Schristos
94575fd0b74Schristos bool types = false;
94675fd0b74Schristos offset = this->gdb_index_->find_pubtype_offset(this->cu_offset());
94775fd0b74Schristos types = this->read_pubtable(this->gdb_index_->pubtypes_table(), offset);
94875fd0b74Schristos return names || types;
94975fd0b74Schristos }
95075fd0b74Schristos
95175fd0b74Schristos // Clear the declarations map.
95275fd0b74Schristos void
clear_declarations()95375fd0b74Schristos Gdb_index_info_reader::clear_declarations()
95475fd0b74Schristos {
95575fd0b74Schristos // Free strings in memory we manage.
95675fd0b74Schristos for (Declaration_map::iterator it = this->declarations_.begin();
95775fd0b74Schristos it != this->declarations_.end();
95875fd0b74Schristos ++it)
95975fd0b74Schristos {
96075fd0b74Schristos if (it->second.parent_offset_ == -1)
96175fd0b74Schristos delete[] it->second.name_;
96275fd0b74Schristos }
96375fd0b74Schristos
96475fd0b74Schristos this->declarations_.clear();
96575fd0b74Schristos }
96675fd0b74Schristos
96775fd0b74Schristos // Print usage statistics.
96875fd0b74Schristos void
print_stats()96975fd0b74Schristos Gdb_index_info_reader::print_stats()
97075fd0b74Schristos {
97175fd0b74Schristos fprintf(stderr, _("%s: DWARF CUs: %u\n"),
97275fd0b74Schristos program_name, Gdb_index_info_reader::dwarf_cu_count);
97375fd0b74Schristos fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
97475fd0b74Schristos program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
97575fd0b74Schristos fprintf(stderr, _("%s: DWARF TUs: %u\n"),
97675fd0b74Schristos program_name, Gdb_index_info_reader::dwarf_tu_count);
97775fd0b74Schristos fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
97875fd0b74Schristos program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
97975fd0b74Schristos }
98075fd0b74Schristos
98175fd0b74Schristos // Class Gdb_index.
98275fd0b74Schristos
98375fd0b74Schristos // Construct the .gdb_index section.
98475fd0b74Schristos
Gdb_index(Output_section * gdb_index_section)98575fd0b74Schristos Gdb_index::Gdb_index(Output_section* gdb_index_section)
98675fd0b74Schristos : Output_section_data(4),
98775fd0b74Schristos pubnames_table_(NULL),
98875fd0b74Schristos pubtypes_table_(NULL),
98975fd0b74Schristos gdb_index_section_(gdb_index_section),
99075fd0b74Schristos comp_units_(),
99175fd0b74Schristos type_units_(),
99275fd0b74Schristos ranges_(),
99375fd0b74Schristos cu_vector_list_(),
99475fd0b74Schristos cu_vector_offsets_(NULL),
99575fd0b74Schristos stringpool_(),
99675fd0b74Schristos tu_offset_(0),
99775fd0b74Schristos addr_offset_(0),
99875fd0b74Schristos symtab_offset_(0),
99975fd0b74Schristos cu_pool_offset_(0),
100075fd0b74Schristos stringpool_offset_(0),
100175fd0b74Schristos pubnames_object_(NULL),
100275fd0b74Schristos stmt_list_offset_(-1)
100375fd0b74Schristos {
100475fd0b74Schristos this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
100575fd0b74Schristos }
100675fd0b74Schristos
~Gdb_index()100775fd0b74Schristos Gdb_index::~Gdb_index()
100875fd0b74Schristos {
100975fd0b74Schristos // Free the memory used by the symbol table.
101075fd0b74Schristos delete this->gdb_symtab_;
101175fd0b74Schristos // Free the memory used by the CU vectors.
101275fd0b74Schristos for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
101375fd0b74Schristos delete this->cu_vector_list_[i];
101475fd0b74Schristos }
101575fd0b74Schristos
101675fd0b74Schristos
101775fd0b74Schristos // Scan the pubnames and pubtypes sections and build a map of the
101875fd0b74Schristos // various cus and tus they refer to, so we can process the entries
101975fd0b74Schristos // when we encounter the die for that cu or tu.
102075fd0b74Schristos // Return the just-read table so it can be cached.
102175fd0b74Schristos
102275fd0b74Schristos Dwarf_pubnames_table*
map_pubtable_to_dies(unsigned int attr,Gdb_index_info_reader * dwinfo,Relobj * object,const unsigned char * symbols,off_t symbols_size)102375fd0b74Schristos Gdb_index::map_pubtable_to_dies(unsigned int attr,
102475fd0b74Schristos Gdb_index_info_reader* dwinfo,
102575fd0b74Schristos Relobj* object,
102675fd0b74Schristos const unsigned char* symbols,
102775fd0b74Schristos off_t symbols_size)
102875fd0b74Schristos {
102975fd0b74Schristos uint64_t section_offset = 0;
103075fd0b74Schristos Dwarf_pubnames_table* table;
103175fd0b74Schristos Pubname_offset_map* map;
103275fd0b74Schristos
103375fd0b74Schristos if (attr == elfcpp::DW_AT_GNU_pubnames)
103475fd0b74Schristos {
103575fd0b74Schristos table = new Dwarf_pubnames_table(dwinfo, false);
103675fd0b74Schristos map = &this->cu_pubname_map_;
103775fd0b74Schristos }
103875fd0b74Schristos else
103975fd0b74Schristos {
104075fd0b74Schristos table = new Dwarf_pubnames_table(dwinfo, true);
104175fd0b74Schristos map = &this->cu_pubtype_map_;
104275fd0b74Schristos }
104375fd0b74Schristos
104475fd0b74Schristos map->clear();
104575fd0b74Schristos if (!table->read_section(object, symbols, symbols_size))
104675fd0b74Schristos return NULL;
104775fd0b74Schristos
104875fd0b74Schristos while (table->read_header(section_offset))
104975fd0b74Schristos {
105075fd0b74Schristos map->insert(std::make_pair(table->cu_offset(), section_offset));
105175fd0b74Schristos section_offset += table->subsection_size();
105275fd0b74Schristos }
105375fd0b74Schristos
105475fd0b74Schristos return table;
105575fd0b74Schristos }
105675fd0b74Schristos
105775fd0b74Schristos // Wrapper for map_pubtable_to_dies
105875fd0b74Schristos
105975fd0b74Schristos void
map_pubnames_and_types_to_dies(Gdb_index_info_reader * dwinfo,Relobj * object,const unsigned char * symbols,off_t symbols_size)106075fd0b74Schristos Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
106175fd0b74Schristos Relobj* object,
106275fd0b74Schristos const unsigned char* symbols,
106375fd0b74Schristos off_t symbols_size)
106475fd0b74Schristos {
106575fd0b74Schristos // This is a new object, so reset the relevant variables.
106675fd0b74Schristos this->pubnames_object_ = object;
106775fd0b74Schristos this->stmt_list_offset_ = -1;
106875fd0b74Schristos
106975fd0b74Schristos delete this->pubnames_table_;
107075fd0b74Schristos this->pubnames_table_
107175fd0b74Schristos = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames, dwinfo,
107275fd0b74Schristos object, symbols, symbols_size);
107375fd0b74Schristos delete this->pubtypes_table_;
107475fd0b74Schristos this->pubtypes_table_
107575fd0b74Schristos = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes, dwinfo,
107675fd0b74Schristos object, symbols, symbols_size);
107775fd0b74Schristos }
107875fd0b74Schristos
107975fd0b74Schristos // Given a cu_offset, find the associated section of the pubnames
108075fd0b74Schristos // table.
108175fd0b74Schristos
108275fd0b74Schristos off_t
find_pubname_offset(off_t cu_offset)108375fd0b74Schristos Gdb_index::find_pubname_offset(off_t cu_offset)
108475fd0b74Schristos {
108575fd0b74Schristos Pubname_offset_map::iterator it = this->cu_pubname_map_.find(cu_offset);
108675fd0b74Schristos if (it != this->cu_pubname_map_.end())
108775fd0b74Schristos return it->second;
108875fd0b74Schristos return -1;
108975fd0b74Schristos }
109075fd0b74Schristos
109175fd0b74Schristos // Given a cu_offset, find the associated section of the pubnames
109275fd0b74Schristos // table.
109375fd0b74Schristos
109475fd0b74Schristos off_t
find_pubtype_offset(off_t cu_offset)109575fd0b74Schristos Gdb_index::find_pubtype_offset(off_t cu_offset)
109675fd0b74Schristos {
109775fd0b74Schristos Pubname_offset_map::iterator it = this->cu_pubtype_map_.find(cu_offset);
109875fd0b74Schristos if (it != this->cu_pubtype_map_.end())
109975fd0b74Schristos return it->second;
110075fd0b74Schristos return -1;
110175fd0b74Schristos }
110275fd0b74Schristos
110375fd0b74Schristos // Scan a .debug_info or .debug_types input section.
110475fd0b74Schristos
110575fd0b74Schristos void
scan_debug_info(bool is_type_unit,Relobj * object,const unsigned char * symbols,off_t symbols_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type)110675fd0b74Schristos Gdb_index::scan_debug_info(bool is_type_unit,
110775fd0b74Schristos Relobj* object,
110875fd0b74Schristos const unsigned char* symbols,
110975fd0b74Schristos off_t symbols_size,
111075fd0b74Schristos unsigned int shndx,
111175fd0b74Schristos unsigned int reloc_shndx,
111275fd0b74Schristos unsigned int reloc_type)
111375fd0b74Schristos {
111475fd0b74Schristos Gdb_index_info_reader dwinfo(is_type_unit, object,
111575fd0b74Schristos symbols, symbols_size,
111675fd0b74Schristos shndx, reloc_shndx,
111775fd0b74Schristos reloc_type, this);
111875fd0b74Schristos if (object != this->pubnames_object_)
111975fd0b74Schristos map_pubnames_and_types_to_dies(&dwinfo, object, symbols, symbols_size);
112075fd0b74Schristos dwinfo.parse();
112175fd0b74Schristos }
112275fd0b74Schristos
112375fd0b74Schristos // Add a symbol.
112475fd0b74Schristos
112575fd0b74Schristos void
add_symbol(int cu_index,const char * sym_name,uint8_t flags)112675fd0b74Schristos Gdb_index::add_symbol(int cu_index, const char* sym_name, uint8_t flags)
112775fd0b74Schristos {
112875fd0b74Schristos unsigned int hash = mapped_index_string_hash(
112975fd0b74Schristos reinterpret_cast<const unsigned char*>(sym_name));
113075fd0b74Schristos Gdb_symbol* sym = new Gdb_symbol();
113175fd0b74Schristos this->stringpool_.add(sym_name, true, &sym->name_key);
113275fd0b74Schristos sym->hashval = hash;
113375fd0b74Schristos sym->cu_vector_index = 0;
113475fd0b74Schristos
113575fd0b74Schristos Gdb_symbol* found = this->gdb_symtab_->add(sym);
113675fd0b74Schristos if (found == sym)
113775fd0b74Schristos {
113875fd0b74Schristos // New symbol -- allocate a new CU index vector.
113975fd0b74Schristos found->cu_vector_index = this->cu_vector_list_.size();
114075fd0b74Schristos this->cu_vector_list_.push_back(new Cu_vector());
114175fd0b74Schristos }
114275fd0b74Schristos else
114375fd0b74Schristos {
114475fd0b74Schristos // Found an existing symbol -- append to the existing
114575fd0b74Schristos // CU index vector.
114675fd0b74Schristos delete sym;
114775fd0b74Schristos }
114875fd0b74Schristos
114975fd0b74Schristos // Add the CU index to the vector list for this symbol,
115075fd0b74Schristos // if it's not already on the list. We only need to
115175fd0b74Schristos // check the last added entry.
115275fd0b74Schristos Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
115375fd0b74Schristos if (cu_vec->size() == 0
115475fd0b74Schristos || cu_vec->back().first != cu_index
115575fd0b74Schristos || cu_vec->back().second != flags)
115675fd0b74Schristos cu_vec->push_back(std::make_pair(cu_index, flags));
115775fd0b74Schristos }
115875fd0b74Schristos
115975fd0b74Schristos // Return TRUE if we have already processed the pubnames associated
116075fd0b74Schristos // with the statement list at the given OFFSET.
116175fd0b74Schristos
116275fd0b74Schristos bool
pubnames_read(const Relobj * object,off_t offset)116375fd0b74Schristos Gdb_index::pubnames_read(const Relobj* object, off_t offset)
116475fd0b74Schristos {
116575fd0b74Schristos bool ret = (this->pubnames_object_ == object
116675fd0b74Schristos && this->stmt_list_offset_ == offset);
116775fd0b74Schristos return ret;
116875fd0b74Schristos }
116975fd0b74Schristos
117075fd0b74Schristos // Record that we have processed the pubnames associated with the
117175fd0b74Schristos // statement list for OBJECT at the given OFFSET.
117275fd0b74Schristos
117375fd0b74Schristos void
set_pubnames_read(const Relobj * object,off_t offset)117475fd0b74Schristos Gdb_index::set_pubnames_read(const Relobj* object, off_t offset)
117575fd0b74Schristos {
117675fd0b74Schristos this->pubnames_object_ = object;
117775fd0b74Schristos this->stmt_list_offset_ = offset;
117875fd0b74Schristos }
117975fd0b74Schristos
118075fd0b74Schristos // Set the size of the .gdb_index section.
118175fd0b74Schristos
118275fd0b74Schristos void
set_final_data_size()118375fd0b74Schristos Gdb_index::set_final_data_size()
118475fd0b74Schristos {
118575fd0b74Schristos // Finalize the string pool.
118675fd0b74Schristos this->stringpool_.set_string_offsets();
118775fd0b74Schristos
118875fd0b74Schristos // Compute the total size of the CU vectors.
118975fd0b74Schristos // For each CU vector, include one entry for the count at the
119075fd0b74Schristos // beginning of the vector.
119175fd0b74Schristos unsigned int cu_vector_count = this->cu_vector_list_.size();
119275fd0b74Schristos unsigned int cu_vector_size = 0;
119375fd0b74Schristos this->cu_vector_offsets_ = new off_t[cu_vector_count];
119475fd0b74Schristos for (unsigned int i = 0; i < cu_vector_count; ++i)
119575fd0b74Schristos {
119675fd0b74Schristos Cu_vector* cu_vec = this->cu_vector_list_[i];
119775fd0b74Schristos cu_vector_offsets_[i] = cu_vector_size;
119875fd0b74Schristos cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
119975fd0b74Schristos }
120075fd0b74Schristos
120175fd0b74Schristos // Assign relative offsets to each portion of the index,
120275fd0b74Schristos // and find the total size of the section.
120375fd0b74Schristos section_size_type data_size = gdb_index_hdr_size;
120475fd0b74Schristos data_size += this->comp_units_.size() * gdb_index_cu_size;
120575fd0b74Schristos this->tu_offset_ = data_size;
120675fd0b74Schristos data_size += this->type_units_.size() * gdb_index_tu_size;
120775fd0b74Schristos this->addr_offset_ = data_size;
120875fd0b74Schristos for (unsigned int i = 0; i < this->ranges_.size(); ++i)
120975fd0b74Schristos data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
121075fd0b74Schristos this->symtab_offset_ = data_size;
121175fd0b74Schristos data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
121275fd0b74Schristos this->cu_pool_offset_ = data_size;
121375fd0b74Schristos data_size += cu_vector_size;
121475fd0b74Schristos this->stringpool_offset_ = data_size;
121575fd0b74Schristos data_size += this->stringpool_.get_strtab_size();
121675fd0b74Schristos
121775fd0b74Schristos this->set_data_size(data_size);
121875fd0b74Schristos }
121975fd0b74Schristos
122075fd0b74Schristos // Write the data to the file.
122175fd0b74Schristos
122275fd0b74Schristos void
do_write(Output_file * of)122375fd0b74Schristos Gdb_index::do_write(Output_file* of)
122475fd0b74Schristos {
122575fd0b74Schristos const off_t off = this->offset();
122675fd0b74Schristos const off_t oview_size = this->data_size();
122775fd0b74Schristos unsigned char* const oview = of->get_output_view(off, oview_size);
122875fd0b74Schristos unsigned char* pov = oview;
122975fd0b74Schristos
123075fd0b74Schristos // Write the file header.
123175fd0b74Schristos // (1) Version number.
123275fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
123375fd0b74Schristos pov += 4;
123475fd0b74Schristos // (2) Offset of the CU list.
123575fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
123675fd0b74Schristos pov += 4;
123775fd0b74Schristos // (3) Offset of the types CU list.
123875fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
123975fd0b74Schristos pov += 4;
124075fd0b74Schristos // (4) Offset of the address area.
124175fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
124275fd0b74Schristos pov += 4;
124375fd0b74Schristos // (5) Offset of the symbol table.
124475fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
124575fd0b74Schristos pov += 4;
124675fd0b74Schristos // (6) Offset of the constant pool.
124775fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
124875fd0b74Schristos pov += 4;
124975fd0b74Schristos
125075fd0b74Schristos gold_assert(pov - oview == gdb_index_hdr_size);
125175fd0b74Schristos
125275fd0b74Schristos // Write the CU list.
125375fd0b74Schristos unsigned int comp_units_count = this->comp_units_.size();
125475fd0b74Schristos for (unsigned int i = 0; i < comp_units_count; ++i)
125575fd0b74Schristos {
125675fd0b74Schristos const Comp_unit& cu = this->comp_units_[i];
125775fd0b74Schristos elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
125875fd0b74Schristos elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
125975fd0b74Schristos pov += 16;
126075fd0b74Schristos }
126175fd0b74Schristos
126275fd0b74Schristos gold_assert(pov - oview == this->tu_offset_);
126375fd0b74Schristos
126475fd0b74Schristos // Write the types CU list.
126575fd0b74Schristos for (unsigned int i = 0; i < this->type_units_.size(); ++i)
126675fd0b74Schristos {
126775fd0b74Schristos const Type_unit& tu = this->type_units_[i];
126875fd0b74Schristos elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
126975fd0b74Schristos elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
127075fd0b74Schristos elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
127175fd0b74Schristos pov += 24;
127275fd0b74Schristos }
127375fd0b74Schristos
127475fd0b74Schristos gold_assert(pov - oview == this->addr_offset_);
127575fd0b74Schristos
127675fd0b74Schristos // Write the address area.
127775fd0b74Schristos for (unsigned int i = 0; i < this->ranges_.size(); ++i)
127875fd0b74Schristos {
127975fd0b74Schristos int cu_index = this->ranges_[i].cu_index;
128075fd0b74Schristos // Translate negative indexes, which refer to a TU, to a
128175fd0b74Schristos // logical index into a concatenated CU/TU list.
128275fd0b74Schristos if (cu_index < 0)
128375fd0b74Schristos cu_index = comp_units_count + (-1 - cu_index);
128475fd0b74Schristos Relobj* object = this->ranges_[i].object;
128575fd0b74Schristos const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
128675fd0b74Schristos for (unsigned int j = 0; j < ranges.size(); ++j)
128775fd0b74Schristos {
128875fd0b74Schristos const Dwarf_range_list::Range& range = ranges[j];
128975fd0b74Schristos uint64_t base = 0;
129075fd0b74Schristos if (range.shndx > 0)
129175fd0b74Schristos {
129275fd0b74Schristos const Output_section* os = object->output_section(range.shndx);
129375fd0b74Schristos base = (os->address()
129475fd0b74Schristos + object->output_section_offset(range.shndx));
129575fd0b74Schristos }
129675fd0b74Schristos elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
129775fd0b74Schristos elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
129875fd0b74Schristos base + range.end);
129975fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
130075fd0b74Schristos pov += 20;
130175fd0b74Schristos }
130275fd0b74Schristos }
130375fd0b74Schristos
130475fd0b74Schristos gold_assert(pov - oview == this->symtab_offset_);
130575fd0b74Schristos
130675fd0b74Schristos // Write the symbol table.
130775fd0b74Schristos for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
130875fd0b74Schristos {
130975fd0b74Schristos const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
131075fd0b74Schristos section_offset_type name_offset = 0;
131175fd0b74Schristos unsigned int cu_vector_offset = 0;
131275fd0b74Schristos if (sym != NULL)
131375fd0b74Schristos {
131475fd0b74Schristos name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
131575fd0b74Schristos + this->stringpool_offset_ - this->cu_pool_offset_);
131675fd0b74Schristos cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
131775fd0b74Schristos }
131875fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, name_offset);
131975fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
132075fd0b74Schristos pov += 8;
132175fd0b74Schristos }
132275fd0b74Schristos
132375fd0b74Schristos gold_assert(pov - oview == this->cu_pool_offset_);
132475fd0b74Schristos
132575fd0b74Schristos // Write the CU vectors into the constant pool.
132675fd0b74Schristos for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
132775fd0b74Schristos {
132875fd0b74Schristos Cu_vector* cu_vec = this->cu_vector_list_[i];
132975fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
133075fd0b74Schristos pov += 4;
133175fd0b74Schristos for (unsigned int j = 0; j < cu_vec->size(); ++j)
133275fd0b74Schristos {
133375fd0b74Schristos int cu_index = (*cu_vec)[j].first;
133475fd0b74Schristos uint8_t flags = (*cu_vec)[j].second;
133575fd0b74Schristos if (cu_index < 0)
133675fd0b74Schristos cu_index = comp_units_count + (-1 - cu_index);
133775fd0b74Schristos cu_index |= flags << 24;
133875fd0b74Schristos elfcpp::Swap<32, false>::writeval(pov, cu_index);
133975fd0b74Schristos pov += 4;
134075fd0b74Schristos }
134175fd0b74Schristos }
134275fd0b74Schristos
134375fd0b74Schristos gold_assert(pov - oview == this->stringpool_offset_);
134475fd0b74Schristos
134575fd0b74Schristos // Write the strings into the constant pool.
134675fd0b74Schristos this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
134775fd0b74Schristos
134875fd0b74Schristos of->write_output_view(off, oview_size, oview);
134975fd0b74Schristos }
135075fd0b74Schristos
135175fd0b74Schristos // Print usage statistics.
135275fd0b74Schristos void
print_stats()135375fd0b74Schristos Gdb_index::print_stats()
135475fd0b74Schristos {
135575fd0b74Schristos if (parameters->options().gdb_index())
135675fd0b74Schristos Gdb_index_info_reader::print_stats();
135775fd0b74Schristos }
135875fd0b74Schristos
135975fd0b74Schristos } // End namespace gold.
1360