1883529b6Schristos // gdb-index.cc -- generate .gdb_index section for fast debug lookup
2883529b6Schristos
3*cb63e24eSchristos // Copyright (C) 2012-2024 Free Software Foundation, Inc.
4883529b6Schristos // Written by Cary Coutant <ccoutant@google.com>.
5883529b6Schristos
6883529b6Schristos // This file is part of gold.
7883529b6Schristos
8883529b6Schristos // This program is free software; you can redistribute it and/or modify
9883529b6Schristos // it under the terms of the GNU General Public License as published by
10883529b6Schristos // the Free Software Foundation; either version 3 of the License, or
11883529b6Schristos // (at your option) any later version.
12883529b6Schristos
13883529b6Schristos // This program is distributed in the hope that it will be useful,
14883529b6Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
15883529b6Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16883529b6Schristos // GNU General Public License for more details.
17883529b6Schristos
18883529b6Schristos // You should have received a copy of the GNU General Public License
19883529b6Schristos // along with this program; if not, write to the Free Software
20883529b6Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21883529b6Schristos // MA 02110-1301, USA.
22883529b6Schristos
23883529b6Schristos #include "gold.h"
24883529b6Schristos
25883529b6Schristos #include "gdb-index.h"
26883529b6Schristos #include "dwarf_reader.h"
27883529b6Schristos #include "dwarf.h"
28883529b6Schristos #include "object.h"
29883529b6Schristos #include "output.h"
30883529b6Schristos #include "demangle.h"
31883529b6Schristos
32883529b6Schristos namespace gold
33883529b6Schristos {
34883529b6Schristos
359573673dSchristos const int gdb_index_version = 7;
36883529b6Schristos
37883529b6Schristos // Sizes of various records in the .gdb_index section.
38883529b6Schristos const int gdb_index_offset_size = 4;
39883529b6Schristos const int gdb_index_hdr_size = 6 * gdb_index_offset_size;
40883529b6Schristos const int gdb_index_cu_size = 16;
41883529b6Schristos const int gdb_index_tu_size = 24;
42883529b6Schristos const int gdb_index_addr_size = 16 + gdb_index_offset_size;
43883529b6Schristos const int gdb_index_sym_size = 2 * gdb_index_offset_size;
44883529b6Schristos
45883529b6Schristos // This class manages the hashed symbol table for the .gdb_index section.
46883529b6Schristos // It is essentially equivalent to the hashtab implementation in libiberty,
47883529b6Schristos // but is copied into gdb sources and here for compatibility because its
48883529b6Schristos // data structure is exposed on disk.
49883529b6Schristos
50883529b6Schristos template <typename T>
51883529b6Schristos class Gdb_hashtab
52883529b6Schristos {
53883529b6Schristos public:
Gdb_hashtab()54883529b6Schristos Gdb_hashtab()
55883529b6Schristos : size_(0), capacity_(0), hashtab_(NULL)
56883529b6Schristos { }
57883529b6Schristos
~Gdb_hashtab()58883529b6Schristos ~Gdb_hashtab()
59883529b6Schristos {
60883529b6Schristos for (size_t i = 0; i < this->capacity_; ++i)
61883529b6Schristos if (this->hashtab_[i] != NULL)
62883529b6Schristos delete this->hashtab_[i];
63883529b6Schristos delete[] this->hashtab_;
64883529b6Schristos }
65883529b6Schristos
66883529b6Schristos // Add a symbol.
67883529b6Schristos T*
add(T * symbol)68883529b6Schristos add(T* symbol)
69883529b6Schristos {
70883529b6Schristos // Resize the hash table if necessary.
71883529b6Schristos if (4 * this->size_ / 3 >= this->capacity_)
72883529b6Schristos this->expand();
73883529b6Schristos
74883529b6Schristos T** slot = this->find_slot(symbol);
75883529b6Schristos if (*slot == NULL)
76883529b6Schristos {
77883529b6Schristos ++this->size_;
78883529b6Schristos *slot = symbol;
79883529b6Schristos }
80883529b6Schristos
81883529b6Schristos return *slot;
82883529b6Schristos }
83883529b6Schristos
84883529b6Schristos // Return the current size.
85883529b6Schristos size_t
size() const86883529b6Schristos size() const
87883529b6Schristos { return this->size_; }
88883529b6Schristos
89883529b6Schristos // Return the current capacity.
90883529b6Schristos size_t
capacity() const91883529b6Schristos capacity() const
92883529b6Schristos { return this->capacity_; }
93883529b6Schristos
94883529b6Schristos // Return the contents of slot N.
95883529b6Schristos T*
operator [](size_t n)96883529b6Schristos operator[](size_t n)
97883529b6Schristos { return this->hashtab_[n]; }
98883529b6Schristos
99883529b6Schristos private:
100883529b6Schristos // Find a symbol in the hash table, or return an empty slot if
101883529b6Schristos // the symbol is not in the table.
102883529b6Schristos T**
find_slot(T * symbol)103883529b6Schristos find_slot(T* symbol)
104883529b6Schristos {
105883529b6Schristos unsigned int index = symbol->hash() & (this->capacity_ - 1);
106883529b6Schristos unsigned int step = ((symbol->hash() * 17) & (this->capacity_ - 1)) | 1;
107883529b6Schristos
108883529b6Schristos for (;;)
109883529b6Schristos {
110883529b6Schristos if (this->hashtab_[index] == NULL
111883529b6Schristos || this->hashtab_[index]->equal(symbol))
112883529b6Schristos return &this->hashtab_[index];
113883529b6Schristos index = (index + step) & (this->capacity_ - 1);
114883529b6Schristos }
115883529b6Schristos }
116883529b6Schristos
117883529b6Schristos // Expand the hash table.
118883529b6Schristos void
expand()119883529b6Schristos expand()
120883529b6Schristos {
121883529b6Schristos if (this->capacity_ == 0)
122883529b6Schristos {
123883529b6Schristos // Allocate the hash table for the first time.
124883529b6Schristos this->capacity_ = Gdb_hashtab::initial_size;
125883529b6Schristos this->hashtab_ = new T*[this->capacity_];
126883529b6Schristos memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
127883529b6Schristos }
128883529b6Schristos else
129883529b6Schristos {
130883529b6Schristos // Expand and rehash.
131883529b6Schristos unsigned int old_cap = this->capacity_;
132883529b6Schristos T** old_hashtab = this->hashtab_;
133883529b6Schristos this->capacity_ *= 2;
134883529b6Schristos this->hashtab_ = new T*[this->capacity_];
135883529b6Schristos memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
136883529b6Schristos for (size_t i = 0; i < old_cap; ++i)
137883529b6Schristos {
138883529b6Schristos if (old_hashtab[i] != NULL)
139883529b6Schristos {
140883529b6Schristos T** slot = this->find_slot(old_hashtab[i]);
141883529b6Schristos *slot = old_hashtab[i];
142883529b6Schristos }
143883529b6Schristos }
144883529b6Schristos delete[] old_hashtab;
145883529b6Schristos }
146883529b6Schristos }
147883529b6Schristos
148883529b6Schristos // Initial size of the hash table; must be a power of 2.
149883529b6Schristos static const int initial_size = 1024;
150883529b6Schristos size_t size_;
151883529b6Schristos size_t capacity_;
152883529b6Schristos T** hashtab_;
153883529b6Schristos };
154883529b6Schristos
155883529b6Schristos // The hash function for strings in the mapped index. This is copied
156883529b6Schristos // directly from gdb/dwarf2read.c.
157883529b6Schristos
158883529b6Schristos static unsigned int
mapped_index_string_hash(const unsigned char * str)159883529b6Schristos mapped_index_string_hash(const unsigned char* str)
160883529b6Schristos {
161883529b6Schristos unsigned int r = 0;
162883529b6Schristos unsigned char c;
163883529b6Schristos
164883529b6Schristos while ((c = *str++) != 0)
165883529b6Schristos {
166883529b6Schristos if (gdb_index_version >= 5)
167883529b6Schristos c = tolower (c);
168883529b6Schristos r = r * 67 + c - 113;
169883529b6Schristos }
170883529b6Schristos
171883529b6Schristos return r;
172883529b6Schristos }
173883529b6Schristos
174883529b6Schristos // A specialization of Dwarf_info_reader, for building the .gdb_index.
175883529b6Schristos
176883529b6Schristos class Gdb_index_info_reader : public Dwarf_info_reader
177883529b6Schristos {
178883529b6Schristos 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)179883529b6Schristos Gdb_index_info_reader(bool is_type_unit,
180883529b6Schristos Relobj* object,
181883529b6Schristos const unsigned char* symbols,
182883529b6Schristos off_t symbols_size,
183883529b6Schristos unsigned int shndx,
184883529b6Schristos unsigned int reloc_shndx,
185883529b6Schristos unsigned int reloc_type,
186883529b6Schristos Gdb_index* gdb_index)
187883529b6Schristos : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
188883529b6Schristos reloc_shndx, reloc_type),
189883529b6Schristos gdb_index_(gdb_index), cu_index_(0), cu_language_(0)
190883529b6Schristos { }
191883529b6Schristos
~Gdb_index_info_reader()192883529b6Schristos ~Gdb_index_info_reader()
193883529b6Schristos { this->clear_declarations(); }
194883529b6Schristos
195883529b6Schristos // Print usage statistics.
196883529b6Schristos static void
197883529b6Schristos print_stats();
198883529b6Schristos
199883529b6Schristos protected:
200883529b6Schristos // Visit a compilation unit.
201883529b6Schristos virtual void
202883529b6Schristos visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
203883529b6Schristos
204883529b6Schristos // Visit a type unit.
205883529b6Schristos virtual void
2069573673dSchristos visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
2079573673dSchristos uint64_t signature, Dwarf_die*);
208883529b6Schristos
209883529b6Schristos private:
210883529b6Schristos // A map for recording DIEs we've seen that may be referred to be
211883529b6Schristos // later DIEs (via DW_AT_specification or DW_AT_abstract_origin).
212883529b6Schristos // The map is indexed by a DIE offset within the compile unit.
213883529b6Schristos // PARENT_OFFSET_ is the offset of the DIE that represents the
214883529b6Schristos // outer context, and NAME_ is a pointer to a component of the
215883529b6Schristos // fully-qualified name.
216883529b6Schristos // Normally, the names we point to are in a string table, so we don't
217883529b6Schristos // have to manage them, but when we have a fully-qualified name
218883529b6Schristos // computed, we put it in the table, and set PARENT_OFFSET_ to -1
219883529b6Schristos // indicate a string that we are managing.
220883529b6Schristos struct Declaration_pair
221883529b6Schristos {
Declaration_pairgold::Gdb_index_info_reader::Declaration_pair222883529b6Schristos Declaration_pair(off_t parent_offset, const char* name)
223883529b6Schristos : parent_offset_(parent_offset), name_(name)
224883529b6Schristos { }
225883529b6Schristos
226883529b6Schristos off_t parent_offset_;
227883529b6Schristos const char* name_;
228883529b6Schristos };
229883529b6Schristos typedef Unordered_map<off_t, Declaration_pair> Declaration_map;
230883529b6Schristos
231883529b6Schristos // Visit a top-level DIE.
232883529b6Schristos void
233883529b6Schristos visit_top_die(Dwarf_die* die);
234883529b6Schristos
235883529b6Schristos // Visit the children of a DIE.
236883529b6Schristos void
237883529b6Schristos visit_children(Dwarf_die* die, Dwarf_die* context);
238883529b6Schristos
239883529b6Schristos // Visit a DIE.
240883529b6Schristos void
241883529b6Schristos visit_die(Dwarf_die* die, Dwarf_die* context);
242883529b6Schristos
243883529b6Schristos // Visit the children of a DIE.
244883529b6Schristos void
245883529b6Schristos visit_children_for_decls(Dwarf_die* die);
246883529b6Schristos
247883529b6Schristos // Visit a DIE.
248883529b6Schristos void
249883529b6Schristos visit_die_for_decls(Dwarf_die* die, Dwarf_die* context);
250883529b6Schristos
251883529b6Schristos // Guess a fully-qualified name for a class type, based on member function
252883529b6Schristos // linkage names.
253883529b6Schristos std::string
254883529b6Schristos guess_full_class_name(Dwarf_die* die);
255883529b6Schristos
256883529b6Schristos // Add a declaration DIE to the table of declarations.
257883529b6Schristos void
258883529b6Schristos add_declaration(Dwarf_die* die, Dwarf_die* context);
259883529b6Schristos
260883529b6Schristos // Add a declaration whose fully-qualified name is already known.
261883529b6Schristos void
262883529b6Schristos add_declaration_with_full_name(Dwarf_die* die, const char* full_name);
263883529b6Schristos
264883529b6Schristos // Return the context for a DIE whose parent is at DIE_OFFSET.
265883529b6Schristos std::string
266883529b6Schristos get_context(off_t die_offset);
267883529b6Schristos
268883529b6Schristos // Construct a fully-qualified name for DIE.
269883529b6Schristos std::string
270883529b6Schristos get_qualified_name(Dwarf_die* die, Dwarf_die* context);
271883529b6Schristos
272883529b6Schristos // Record the address ranges for a compilation unit.
273883529b6Schristos void
274883529b6Schristos record_cu_ranges(Dwarf_die* die);
275883529b6Schristos
2769573673dSchristos // Wrapper for read_pubtable.
277883529b6Schristos bool
278883529b6Schristos read_pubnames_and_pubtypes(Dwarf_die* die);
279883529b6Schristos
2809573673dSchristos // Read the .debug_pubnames and .debug_pubtypes tables.
2819573673dSchristos bool
2829573673dSchristos read_pubtable(Dwarf_pubnames_table* table, off_t offset);
2839573673dSchristos
284883529b6Schristos // Clear the declarations map.
285883529b6Schristos void
286883529b6Schristos clear_declarations();
287883529b6Schristos
288883529b6Schristos // The Gdb_index section.
289883529b6Schristos Gdb_index* gdb_index_;
290883529b6Schristos // The current CU index (negative for a TU).
291883529b6Schristos int cu_index_;
292883529b6Schristos // The language of the current CU or TU.
293883529b6Schristos unsigned int cu_language_;
294883529b6Schristos // Map from DIE offset to (parent offset, name) pair,
295883529b6Schristos // for DW_AT_specification.
296883529b6Schristos Declaration_map declarations_;
297883529b6Schristos
298883529b6Schristos // Statistics.
299883529b6Schristos // Total number of DWARF compilation units processed.
300883529b6Schristos static unsigned int dwarf_cu_count;
301883529b6Schristos // Number of DWARF compilation units with pubnames/pubtypes.
302883529b6Schristos static unsigned int dwarf_cu_nopubnames_count;
303883529b6Schristos // Total number of DWARF type units processed.
304883529b6Schristos static unsigned int dwarf_tu_count;
305883529b6Schristos // Number of DWARF type units with pubnames/pubtypes.
306883529b6Schristos static unsigned int dwarf_tu_nopubnames_count;
307883529b6Schristos };
308883529b6Schristos
309883529b6Schristos // Total number of DWARF compilation units processed.
310883529b6Schristos unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
311883529b6Schristos // Number of DWARF compilation units without pubnames/pubtypes.
312883529b6Schristos unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
313883529b6Schristos // Total number of DWARF type units processed.
314883529b6Schristos unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
315883529b6Schristos // Number of DWARF type units without pubnames/pubtypes.
316883529b6Schristos unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
317883529b6Schristos
318883529b6Schristos // Process a compilation unit and parse its child DIE.
319883529b6Schristos
320883529b6Schristos void
visit_compilation_unit(off_t cu_offset,off_t cu_length,Dwarf_die * root_die)321883529b6Schristos Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
322883529b6Schristos Dwarf_die* root_die)
323883529b6Schristos {
324883529b6Schristos ++Gdb_index_info_reader::dwarf_cu_count;
325883529b6Schristos this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
326883529b6Schristos this->visit_top_die(root_die);
327883529b6Schristos }
328883529b6Schristos
329883529b6Schristos // Process a type unit and parse its child DIE.
330883529b6Schristos
331883529b6Schristos void
visit_type_unit(off_t tu_offset,off_t,off_t type_offset,uint64_t signature,Dwarf_die * root_die)3329573673dSchristos Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t,
3339573673dSchristos off_t type_offset, uint64_t signature,
3349573673dSchristos Dwarf_die* root_die)
335883529b6Schristos {
336883529b6Schristos ++Gdb_index_info_reader::dwarf_tu_count;
337883529b6Schristos // Use a negative index to flag this as a TU instead of a CU.
338883529b6Schristos this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
339883529b6Schristos signature);
340883529b6Schristos this->visit_top_die(root_die);
341883529b6Schristos }
342883529b6Schristos
343883529b6Schristos // Process a top-level DIE.
344883529b6Schristos // For compile_unit DIEs, record the address ranges. For all
345883529b6Schristos // interesting tags, add qualified names to the symbol table
346883529b6Schristos // and process interesting children. We may need to process
347883529b6Schristos // certain children just for saving declarations that might be
348883529b6Schristos // referenced by later DIEs with a DW_AT_specification attribute.
349883529b6Schristos
350883529b6Schristos void
visit_top_die(Dwarf_die * die)351883529b6Schristos Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
352883529b6Schristos {
353883529b6Schristos this->clear_declarations();
354883529b6Schristos
355883529b6Schristos switch (die->tag())
356883529b6Schristos {
357883529b6Schristos case elfcpp::DW_TAG_compile_unit:
358883529b6Schristos case elfcpp::DW_TAG_type_unit:
359883529b6Schristos this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
360883529b6Schristos if (die->tag() == elfcpp::DW_TAG_compile_unit)
361883529b6Schristos this->record_cu_ranges(die);
362883529b6Schristos // If there is a pubnames and/or pubtypes section for this
363883529b6Schristos // compilation unit, use those; otherwise, parse the DWARF
364883529b6Schristos // info to extract the names.
365883529b6Schristos if (!this->read_pubnames_and_pubtypes(die))
366883529b6Schristos {
3679573673dSchristos // Check for languages that require specialized knowledge to
3689573673dSchristos // construct fully-qualified names, that we don't yet support.
3699573673dSchristos if (this->cu_language_ == elfcpp::DW_LANG_Ada83
3709573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Fortran77
3719573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Fortran90
3729573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Java
3739573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Ada95
3749573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Fortran95
3759573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Fortran03
3769573673dSchristos || this->cu_language_ == elfcpp::DW_LANG_Fortran08)
3779573673dSchristos {
3789573673dSchristos gold_warning(_("%s: --gdb-index currently supports "
3799573673dSchristos "only C and C++ languages"),
3809573673dSchristos this->object()->name().c_str());
3819573673dSchristos return;
3829573673dSchristos }
383883529b6Schristos if (die->tag() == elfcpp::DW_TAG_compile_unit)
384883529b6Schristos ++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
385883529b6Schristos else
386883529b6Schristos ++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
387883529b6Schristos this->visit_children(die, NULL);
388883529b6Schristos }
389883529b6Schristos break;
390883529b6Schristos default:
391883529b6Schristos // The top level DIE should be one of the above.
392883529b6Schristos gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
393883529b6Schristos "or DW_TAG_type_unit"),
394883529b6Schristos this->object()->name().c_str());
395883529b6Schristos return;
396883529b6Schristos }
397883529b6Schristos }
398883529b6Schristos
399883529b6Schristos // Visit the children of PARENT, looking for symbols to add to the index.
400883529b6Schristos // CONTEXT points to the DIE to use for constructing the qualified name --
401883529b6Schristos // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
402883529b6Schristos
403883529b6Schristos void
visit_children(Dwarf_die * parent,Dwarf_die * context)404883529b6Schristos Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
405883529b6Schristos {
406883529b6Schristos off_t next_offset = 0;
407883529b6Schristos for (off_t die_offset = parent->child_offset();
408883529b6Schristos die_offset != 0;
409883529b6Schristos die_offset = next_offset)
410883529b6Schristos {
411883529b6Schristos Dwarf_die die(this, die_offset, parent);
412883529b6Schristos if (die.tag() == 0)
413883529b6Schristos break;
414883529b6Schristos this->visit_die(&die, context);
415883529b6Schristos next_offset = die.sibling_offset();
416883529b6Schristos }
417883529b6Schristos }
418883529b6Schristos
419883529b6Schristos // Visit a child DIE, looking for symbols to add to the index.
420883529b6Schristos // CONTEXT is the parent DIE, used for constructing the qualified name;
421883529b6Schristos // it is NULL if the parent DIE is the top-level DIE.
422883529b6Schristos
423883529b6Schristos void
visit_die(Dwarf_die * die,Dwarf_die * context)424883529b6Schristos Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
425883529b6Schristos {
426883529b6Schristos switch (die->tag())
427883529b6Schristos {
428883529b6Schristos case elfcpp::DW_TAG_subprogram:
429883529b6Schristos case elfcpp::DW_TAG_constant:
430883529b6Schristos case elfcpp::DW_TAG_variable:
431883529b6Schristos case elfcpp::DW_TAG_enumerator:
432883529b6Schristos case elfcpp::DW_TAG_base_type:
433883529b6Schristos if (die->is_declaration())
434883529b6Schristos this->add_declaration(die, context);
435883529b6Schristos else
436883529b6Schristos {
437883529b6Schristos // If the DIE is not a declaration, add it to the index.
438883529b6Schristos std::string full_name = this->get_qualified_name(die, context);
439883529b6Schristos if (!full_name.empty())
4409573673dSchristos this->gdb_index_->add_symbol(this->cu_index_,
4419573673dSchristos full_name.c_str(), 0);
442883529b6Schristos }
443883529b6Schristos break;
444883529b6Schristos case elfcpp::DW_TAG_typedef:
445883529b6Schristos case elfcpp::DW_TAG_union_type:
446883529b6Schristos case elfcpp::DW_TAG_class_type:
447883529b6Schristos case elfcpp::DW_TAG_interface_type:
448883529b6Schristos case elfcpp::DW_TAG_structure_type:
449883529b6Schristos case elfcpp::DW_TAG_enumeration_type:
450883529b6Schristos case elfcpp::DW_TAG_subrange_type:
451883529b6Schristos case elfcpp::DW_TAG_namespace:
452883529b6Schristos {
453883529b6Schristos std::string full_name;
454883529b6Schristos
455883529b6Schristos // For classes at the top level, we need to look for a
456883529b6Schristos // member function with a linkage name in order to get
457883529b6Schristos // the properly-canonicalized name.
458883529b6Schristos if (context == NULL
459883529b6Schristos && (die->tag() == elfcpp::DW_TAG_class_type
460883529b6Schristos || die->tag() == elfcpp::DW_TAG_structure_type
461883529b6Schristos || die->tag() == elfcpp::DW_TAG_union_type))
462883529b6Schristos full_name.assign(this->guess_full_class_name(die));
463883529b6Schristos
464883529b6Schristos // Because we will visit the children, we need to add this DIE
465883529b6Schristos // to the declarations table.
466883529b6Schristos if (full_name.empty())
467883529b6Schristos this->add_declaration(die, context);
468883529b6Schristos else
469883529b6Schristos this->add_declaration_with_full_name(die, full_name.c_str());
470883529b6Schristos
471883529b6Schristos // If the DIE is not a declaration, add it to the index.
472883529b6Schristos // Gdb stores a namespace in the index even when it is
473883529b6Schristos // a declaration.
474883529b6Schristos if (die->tag() == elfcpp::DW_TAG_namespace
475883529b6Schristos || !die->is_declaration())
476883529b6Schristos {
477883529b6Schristos if (full_name.empty())
478883529b6Schristos full_name = this->get_qualified_name(die, context);
479883529b6Schristos if (!full_name.empty())
480883529b6Schristos this->gdb_index_->add_symbol(this->cu_index_,
4819573673dSchristos full_name.c_str(), 0);
482883529b6Schristos }
483883529b6Schristos
484883529b6Schristos // We're interested in the children only for namespaces and
485883529b6Schristos // enumeration types. For enumeration types, we do not include
486883529b6Schristos // the enumeration tag as part of the full name. For other tags,
487883529b6Schristos // visit the children only to collect declarations.
488883529b6Schristos if (die->tag() == elfcpp::DW_TAG_namespace
489883529b6Schristos || die->tag() == elfcpp::DW_TAG_enumeration_type)
490883529b6Schristos this->visit_children(die, die);
491883529b6Schristos else
492883529b6Schristos this->visit_children_for_decls(die);
493883529b6Schristos }
494883529b6Schristos break;
495883529b6Schristos default:
496883529b6Schristos break;
497883529b6Schristos }
498883529b6Schristos }
499883529b6Schristos
500883529b6Schristos // Visit the children of PARENT, looking only for declarations that
501883529b6Schristos // may be referenced by later specification DIEs.
502883529b6Schristos
503883529b6Schristos void
visit_children_for_decls(Dwarf_die * parent)504883529b6Schristos Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
505883529b6Schristos {
506883529b6Schristos off_t next_offset = 0;
507883529b6Schristos for (off_t die_offset = parent->child_offset();
508883529b6Schristos die_offset != 0;
509883529b6Schristos die_offset = next_offset)
510883529b6Schristos {
511883529b6Schristos Dwarf_die die(this, die_offset, parent);
512883529b6Schristos if (die.tag() == 0)
513883529b6Schristos break;
514883529b6Schristos this->visit_die_for_decls(&die, parent);
515883529b6Schristos next_offset = die.sibling_offset();
516883529b6Schristos }
517883529b6Schristos }
518883529b6Schristos
519883529b6Schristos // Visit a child DIE, looking only for declarations that
520883529b6Schristos // may be referenced by later specification DIEs.
521883529b6Schristos
522883529b6Schristos void
visit_die_for_decls(Dwarf_die * die,Dwarf_die * context)523883529b6Schristos Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
524883529b6Schristos {
525883529b6Schristos switch (die->tag())
526883529b6Schristos {
527883529b6Schristos case elfcpp::DW_TAG_subprogram:
528883529b6Schristos case elfcpp::DW_TAG_constant:
529883529b6Schristos case elfcpp::DW_TAG_variable:
530883529b6Schristos case elfcpp::DW_TAG_enumerator:
531883529b6Schristos case elfcpp::DW_TAG_base_type:
532883529b6Schristos {
533883529b6Schristos if (die->is_declaration())
534883529b6Schristos this->add_declaration(die, context);
535883529b6Schristos }
536883529b6Schristos break;
537883529b6Schristos case elfcpp::DW_TAG_typedef:
538883529b6Schristos case elfcpp::DW_TAG_union_type:
539883529b6Schristos case elfcpp::DW_TAG_class_type:
540883529b6Schristos case elfcpp::DW_TAG_interface_type:
541883529b6Schristos case elfcpp::DW_TAG_structure_type:
542883529b6Schristos case elfcpp::DW_TAG_enumeration_type:
543883529b6Schristos case elfcpp::DW_TAG_subrange_type:
544883529b6Schristos case elfcpp::DW_TAG_namespace:
545883529b6Schristos {
546883529b6Schristos if (die->is_declaration())
547883529b6Schristos this->add_declaration(die, context);
548883529b6Schristos this->visit_children_for_decls(die);
549883529b6Schristos }
550883529b6Schristos break;
551883529b6Schristos default:
552883529b6Schristos break;
553883529b6Schristos }
554883529b6Schristos }
555883529b6Schristos
556883529b6Schristos // Extract the class name from the linkage name of a member function.
557883529b6Schristos // This code is adapted from ../gdb/cp-support.c.
558883529b6Schristos
559883529b6Schristos #define d_left(dc) (dc)->u.s_binary.left
560883529b6Schristos #define d_right(dc) (dc)->u.s_binary.right
561883529b6Schristos
562883529b6Schristos static char*
class_name_from_linkage_name(const char * linkage_name)563883529b6Schristos class_name_from_linkage_name(const char* linkage_name)
564883529b6Schristos {
565883529b6Schristos void* storage;
566883529b6Schristos struct demangle_component* tree =
567883529b6Schristos cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
568883529b6Schristos if (tree == NULL)
569883529b6Schristos return NULL;
570883529b6Schristos
571883529b6Schristos int done = 0;
572883529b6Schristos
573883529b6Schristos // First strip off any qualifiers, if we have a function or
574883529b6Schristos // method.
575883529b6Schristos while (!done)
576883529b6Schristos switch (tree->type)
577883529b6Schristos {
578883529b6Schristos case DEMANGLE_COMPONENT_CONST:
579883529b6Schristos case DEMANGLE_COMPONENT_RESTRICT:
580883529b6Schristos case DEMANGLE_COMPONENT_VOLATILE:
581883529b6Schristos case DEMANGLE_COMPONENT_CONST_THIS:
582883529b6Schristos case DEMANGLE_COMPONENT_RESTRICT_THIS:
583883529b6Schristos case DEMANGLE_COMPONENT_VOLATILE_THIS:
584883529b6Schristos case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
585883529b6Schristos tree = d_left(tree);
586883529b6Schristos break;
587883529b6Schristos default:
588883529b6Schristos done = 1;
589883529b6Schristos break;
590883529b6Schristos }
591883529b6Schristos
592883529b6Schristos // If what we have now is a function, discard the argument list.
593883529b6Schristos if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
594883529b6Schristos tree = d_left(tree);
595883529b6Schristos
596883529b6Schristos // If what we have now is a template, strip off the template
597883529b6Schristos // arguments. The left subtree may be a qualified name.
598883529b6Schristos if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
599883529b6Schristos tree = d_left(tree);
600883529b6Schristos
601883529b6Schristos // What we have now should be a name, possibly qualified.
602883529b6Schristos // Additional qualifiers could live in the left subtree or the right
603883529b6Schristos // subtree. Find the last piece.
604883529b6Schristos done = 0;
605883529b6Schristos struct demangle_component* prev_comp = NULL;
606883529b6Schristos struct demangle_component* cur_comp = tree;
607883529b6Schristos while (!done)
608883529b6Schristos switch (cur_comp->type)
609883529b6Schristos {
610883529b6Schristos case DEMANGLE_COMPONENT_QUAL_NAME:
611883529b6Schristos case DEMANGLE_COMPONENT_LOCAL_NAME:
612883529b6Schristos prev_comp = cur_comp;
613883529b6Schristos cur_comp = d_right(cur_comp);
614883529b6Schristos break;
615883529b6Schristos case DEMANGLE_COMPONENT_TEMPLATE:
616883529b6Schristos case DEMANGLE_COMPONENT_NAME:
617883529b6Schristos case DEMANGLE_COMPONENT_CTOR:
618883529b6Schristos case DEMANGLE_COMPONENT_DTOR:
619883529b6Schristos case DEMANGLE_COMPONENT_OPERATOR:
620883529b6Schristos case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
621883529b6Schristos done = 1;
622883529b6Schristos break;
623883529b6Schristos default:
624883529b6Schristos done = 1;
625883529b6Schristos cur_comp = NULL;
626883529b6Schristos break;
627883529b6Schristos }
628883529b6Schristos
629883529b6Schristos char* ret = NULL;
630883529b6Schristos if (cur_comp != NULL && prev_comp != NULL)
631883529b6Schristos {
632883529b6Schristos // We want to discard the rightmost child of PREV_COMP.
633883529b6Schristos *prev_comp = *d_left(prev_comp);
634883529b6Schristos size_t allocated_size;
635883529b6Schristos ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
636883529b6Schristos }
637883529b6Schristos
638883529b6Schristos free(storage);
639883529b6Schristos return ret;
640883529b6Schristos }
641883529b6Schristos
642883529b6Schristos // Guess a fully-qualified name for a class type, based on member function
643883529b6Schristos // linkage names. This is needed for class/struct/union types at the
644883529b6Schristos // top level, because GCC does not always properly embed them within
645883529b6Schristos // the namespace. As in gdb, we look for a member function with a linkage
646883529b6Schristos // name and extract the qualified name from the demangled name.
647883529b6Schristos
648883529b6Schristos std::string
guess_full_class_name(Dwarf_die * die)649883529b6Schristos Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
650883529b6Schristos {
651883529b6Schristos std::string full_name;
652883529b6Schristos off_t next_offset = 0;
653883529b6Schristos
654883529b6Schristos // This routine scans ahead in the DIE structure, possibly advancing
655883529b6Schristos // the relocation tracker beyond the current DIE. We need to checkpoint
656883529b6Schristos // the tracker and reset it when we're done.
657883529b6Schristos uint64_t checkpoint = this->get_reloc_checkpoint();
658883529b6Schristos
659883529b6Schristos for (off_t child_offset = die->child_offset();
660883529b6Schristos child_offset != 0;
661883529b6Schristos child_offset = next_offset)
662883529b6Schristos {
663883529b6Schristos Dwarf_die child(this, child_offset, die);
664883529b6Schristos if (child.tag() == 0)
665883529b6Schristos break;
666883529b6Schristos if (child.tag() == elfcpp::DW_TAG_subprogram)
667883529b6Schristos {
668883529b6Schristos const char* linkage_name = child.linkage_name();
669883529b6Schristos if (linkage_name != NULL)
670883529b6Schristos {
671883529b6Schristos char* guess = class_name_from_linkage_name(linkage_name);
672883529b6Schristos if (guess != NULL)
673883529b6Schristos {
674883529b6Schristos full_name.assign(guess);
675883529b6Schristos free(guess);
676883529b6Schristos break;
677883529b6Schristos }
678883529b6Schristos }
679883529b6Schristos }
680883529b6Schristos next_offset = child.sibling_offset();
681883529b6Schristos }
682883529b6Schristos
683883529b6Schristos this->reset_relocs(checkpoint);
684883529b6Schristos return full_name;
685883529b6Schristos }
686883529b6Schristos
687883529b6Schristos // Add a declaration DIE to the table of declarations.
688883529b6Schristos
689883529b6Schristos void
add_declaration(Dwarf_die * die,Dwarf_die * context)690883529b6Schristos Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
691883529b6Schristos {
692883529b6Schristos const char* name = die->name();
693883529b6Schristos
694883529b6Schristos off_t parent_offset = context != NULL ? context->offset() : 0;
695883529b6Schristos
696883529b6Schristos // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
697883529b6Schristos // attribute, use the parent and name from the earlier declaration.
698883529b6Schristos off_t spec = die->specification();
699883529b6Schristos if (spec == 0)
700883529b6Schristos spec = die->abstract_origin();
701883529b6Schristos if (spec > 0)
702883529b6Schristos {
703883529b6Schristos Declaration_map::iterator it = this->declarations_.find(spec);
704883529b6Schristos if (it != this->declarations_.end())
705883529b6Schristos {
706883529b6Schristos parent_offset = it->second.parent_offset_;
707883529b6Schristos name = it->second.name_;
708883529b6Schristos }
709883529b6Schristos }
710883529b6Schristos
711883529b6Schristos if (name == NULL)
712883529b6Schristos {
713883529b6Schristos if (die->tag() == elfcpp::DW_TAG_namespace)
714883529b6Schristos name = "(anonymous namespace)";
715883529b6Schristos else if (die->tag() == elfcpp::DW_TAG_union_type)
716883529b6Schristos name = "(anonymous union)";
717883529b6Schristos else
718883529b6Schristos name = "(unknown)";
719883529b6Schristos }
720883529b6Schristos
721883529b6Schristos Declaration_pair decl(parent_offset, name);
722883529b6Schristos this->declarations_.insert(std::make_pair(die->offset(), decl));
723883529b6Schristos }
724883529b6Schristos
725883529b6Schristos // Add a declaration whose fully-qualified name is already known.
726883529b6Schristos // In the case where we had to get the canonical name by demangling
727883529b6Schristos // a linkage name, this ensures we use that name instead of the one
728883529b6Schristos // provided in DW_AT_name.
729883529b6Schristos
730883529b6Schristos void
add_declaration_with_full_name(Dwarf_die * die,const char * full_name)731883529b6Schristos Gdb_index_info_reader::add_declaration_with_full_name(
732883529b6Schristos Dwarf_die* die,
733883529b6Schristos const char* full_name)
734883529b6Schristos {
735883529b6Schristos // We need to copy the name.
736883529b6Schristos int len = strlen(full_name);
737883529b6Schristos char* copy = new char[len + 1];
738883529b6Schristos memcpy(copy, full_name, len + 1);
739883529b6Schristos
740883529b6Schristos // Flag that we now manage the memory this points to.
741883529b6Schristos Declaration_pair decl(-1, copy);
742883529b6Schristos this->declarations_.insert(std::make_pair(die->offset(), decl));
743883529b6Schristos }
744883529b6Schristos
745883529b6Schristos // Return the context for a DIE whose parent is at DIE_OFFSET.
746883529b6Schristos
747883529b6Schristos std::string
get_context(off_t die_offset)748883529b6Schristos Gdb_index_info_reader::get_context(off_t die_offset)
749883529b6Schristos {
750883529b6Schristos std::string context;
751883529b6Schristos Declaration_map::iterator it = this->declarations_.find(die_offset);
752883529b6Schristos if (it != this->declarations_.end())
753883529b6Schristos {
754883529b6Schristos off_t parent_offset = it->second.parent_offset_;
755883529b6Schristos if (parent_offset > 0)
756883529b6Schristos {
757883529b6Schristos context = get_context(parent_offset);
758883529b6Schristos context.append("::");
759883529b6Schristos }
760883529b6Schristos if (it->second.name_ != NULL)
761883529b6Schristos context.append(it->second.name_);
762883529b6Schristos }
763883529b6Schristos return context;
764883529b6Schristos }
765883529b6Schristos
766883529b6Schristos // Construct the fully-qualified name for DIE.
767883529b6Schristos
768883529b6Schristos std::string
get_qualified_name(Dwarf_die * die,Dwarf_die * context)769883529b6Schristos Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
770883529b6Schristos {
771883529b6Schristos std::string full_name;
772883529b6Schristos const char* name = die->name();
773883529b6Schristos
774883529b6Schristos off_t parent_offset = context != NULL ? context->offset() : 0;
775883529b6Schristos
776883529b6Schristos // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
777883529b6Schristos // attribute, use the parent and name from the earlier declaration.
778883529b6Schristos off_t spec = die->specification();
779883529b6Schristos if (spec == 0)
780883529b6Schristos spec = die->abstract_origin();
781883529b6Schristos if (spec > 0)
782883529b6Schristos {
783883529b6Schristos Declaration_map::iterator it = this->declarations_.find(spec);
784883529b6Schristos if (it != this->declarations_.end())
785883529b6Schristos {
786883529b6Schristos parent_offset = it->second.parent_offset_;
787883529b6Schristos name = it->second.name_;
788883529b6Schristos }
789883529b6Schristos }
790883529b6Schristos
791883529b6Schristos if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
792883529b6Schristos name = "(anonymous namespace)";
793883529b6Schristos else if (name == NULL)
794883529b6Schristos return full_name;
795883529b6Schristos
796883529b6Schristos // If this is an enumerator constant, skip the immediate parent,
797883529b6Schristos // which is the enumeration tag.
798883529b6Schristos if (die->tag() == elfcpp::DW_TAG_enumerator)
799883529b6Schristos {
800883529b6Schristos Declaration_map::iterator it = this->declarations_.find(parent_offset);
801883529b6Schristos if (it != this->declarations_.end())
802883529b6Schristos parent_offset = it->second.parent_offset_;
803883529b6Schristos }
804883529b6Schristos
805883529b6Schristos if (parent_offset > 0)
806883529b6Schristos {
807883529b6Schristos full_name.assign(this->get_context(parent_offset));
808883529b6Schristos full_name.append("::");
809883529b6Schristos }
810883529b6Schristos full_name.append(name);
811883529b6Schristos
812883529b6Schristos return full_name;
813883529b6Schristos }
814883529b6Schristos
815883529b6Schristos // Record the address ranges for a compilation unit.
816883529b6Schristos
817883529b6Schristos void
record_cu_ranges(Dwarf_die * die)818883529b6Schristos Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
819883529b6Schristos {
820883529b6Schristos unsigned int shndx;
821883529b6Schristos unsigned int shndx2;
822883529b6Schristos
823883529b6Schristos off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
824883529b6Schristos if (ranges_offset != -1)
825883529b6Schristos {
826883529b6Schristos Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
827883529b6Schristos if (ranges != NULL)
828883529b6Schristos this->gdb_index_->add_address_range_list(this->object(),
829883529b6Schristos this->cu_index_, ranges);
830883529b6Schristos return;
831883529b6Schristos }
832883529b6Schristos
833883529b6Schristos off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
834883529b6Schristos off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
835883529b6Schristos if (high_pc == -1)
836883529b6Schristos {
837883529b6Schristos high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
838883529b6Schristos high_pc += low_pc;
839883529b6Schristos shndx2 = shndx;
840883529b6Schristos }
841883529b6Schristos if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
842883529b6Schristos {
843883529b6Schristos if (shndx != shndx2)
844883529b6Schristos {
845883529b6Schristos gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
846883529b6Schristos "are in different sections"),
847883529b6Schristos this->object()->name().c_str());
848883529b6Schristos return;
849883529b6Schristos }
850883529b6Schristos if (shndx == 0 || this->object()->is_section_included(shndx))
851883529b6Schristos {
852883529b6Schristos Dwarf_range_list* ranges = new Dwarf_range_list();
853883529b6Schristos ranges->add(shndx, low_pc, high_pc);
854883529b6Schristos this->gdb_index_->add_address_range_list(this->object(),
855883529b6Schristos this->cu_index_, ranges);
856883529b6Schristos }
857883529b6Schristos }
858883529b6Schristos }
859883529b6Schristos
8609573673dSchristos // Read table and add the relevant names to the index. Returns true
8619573673dSchristos // if any names were added.
8629573673dSchristos
8639573673dSchristos bool
read_pubtable(Dwarf_pubnames_table * table,off_t offset)8649573673dSchristos Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table* table, off_t offset)
8659573673dSchristos {
8669573673dSchristos // If we couldn't read the section when building the cu_pubname_map,
8679573673dSchristos // then we won't find any pubnames now.
8689573673dSchristos if (table == NULL)
8699573673dSchristos return false;
8709573673dSchristos
8719573673dSchristos if (!table->read_header(offset))
8729573673dSchristos return false;
8739573673dSchristos while (true)
8749573673dSchristos {
8759573673dSchristos uint8_t flag_byte;
8769573673dSchristos const char* name = table->next_name(&flag_byte);
8779573673dSchristos if (name == NULL)
8789573673dSchristos break;
8799573673dSchristos
8809573673dSchristos this->gdb_index_->add_symbol(this->cu_index_, name, flag_byte);
8819573673dSchristos }
8829573673dSchristos return true;
8839573673dSchristos }
8849573673dSchristos
885883529b6Schristos // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
886883529b6Schristos // Returns TRUE if either a pubnames or pubtypes section was found.
887883529b6Schristos
888883529b6Schristos bool
read_pubnames_and_pubtypes(Dwarf_die * die)889883529b6Schristos Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
890883529b6Schristos {
8919573673dSchristos // If this is a skeleton debug-type die (generated via
8929573673dSchristos // -gsplit-dwarf), then the associated pubnames should have been
8939573673dSchristos // read along with the corresponding CU. In any case, there isn't
8949573673dSchristos // enough info inside to build a gdb index entry.
8959573673dSchristos if (die->tag() == elfcpp::DW_TAG_type_unit
8969573673dSchristos && die->string_attribute(elfcpp::DW_AT_GNU_dwo_name))
8979573673dSchristos return true;
898883529b6Schristos
8999573673dSchristos // We use stmt_list_off as a unique identifier for the
9009573673dSchristos // compilation unit and its associated type units.
9019573673dSchristos unsigned int shndx;
9029573673dSchristos off_t stmt_list_off = die->ref_attribute (elfcpp::DW_AT_stmt_list,
9039573673dSchristos &shndx);
9049573673dSchristos // Look for the attr as either a flag or a ref.
9059573673dSchristos off_t offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames, &shndx);
9069573673dSchristos
9079573673dSchristos // Newer versions of GCC generate CUs, but not TUs, with
9089573673dSchristos // DW_AT_FORM_flag_present.
9099573673dSchristos unsigned int flag = die->uint_attribute(elfcpp::DW_AT_GNU_pubnames);
9109573673dSchristos if (offset == -1 && flag == 0)
911883529b6Schristos {
9129573673dSchristos // Didn't find the attribute.
9139573673dSchristos if (die->tag() == elfcpp::DW_TAG_type_unit)
9149573673dSchristos {
9159573673dSchristos // If die is a TU, then it might correspond to a CU which we
9169573673dSchristos // have read. If it does, then no need to read the pubnames.
9179573673dSchristos // If it doesn't, then the caller will have to parse the
9189573673dSchristos // dies manually to find the names.
9199573673dSchristos return this->gdb_index_->pubnames_read(this->object(),
9209573673dSchristos stmt_list_off);
9219573673dSchristos }
922883529b6Schristos else
923883529b6Schristos {
9249573673dSchristos // No attribute on the CU means that no pubnames were read.
925883529b6Schristos return false;
926883529b6Schristos }
927883529b6Schristos }
928883529b6Schristos
9299573673dSchristos // We found the attribute, so we can check if the corresponding
9309573673dSchristos // pubnames have been read.
9319573673dSchristos if (this->gdb_index_->pubnames_read(this->object(), stmt_list_off))
9329573673dSchristos return true;
933883529b6Schristos
9349573673dSchristos this->gdb_index_->set_pubnames_read(this->object(), stmt_list_off);
9359573673dSchristos
9369573673dSchristos // We have an attribute, and the pubnames haven't been read, so read
9379573673dSchristos // them.
9389573673dSchristos bool names = false;
9399573673dSchristos // In some of the cases, we could rely on the previous value of
9409573673dSchristos // offset here, but sorting out which cases complicates the logic
9419573673dSchristos // enough that it isn't worth it. So just look up the offset again.
9429573673dSchristos offset = this->gdb_index_->find_pubname_offset(this->cu_offset());
9439573673dSchristos names = this->read_pubtable(this->gdb_index_->pubnames_table(), offset);
9449573673dSchristos
9459573673dSchristos bool types = false;
9469573673dSchristos offset = this->gdb_index_->find_pubtype_offset(this->cu_offset());
9479573673dSchristos types = this->read_pubtable(this->gdb_index_->pubtypes_table(), offset);
9489573673dSchristos return names || types;
949883529b6Schristos }
950883529b6Schristos
951883529b6Schristos // Clear the declarations map.
952883529b6Schristos void
clear_declarations()953883529b6Schristos Gdb_index_info_reader::clear_declarations()
954883529b6Schristos {
955883529b6Schristos // Free strings in memory we manage.
956883529b6Schristos for (Declaration_map::iterator it = this->declarations_.begin();
957883529b6Schristos it != this->declarations_.end();
958883529b6Schristos ++it)
959883529b6Schristos {
960883529b6Schristos if (it->second.parent_offset_ == -1)
961883529b6Schristos delete[] it->second.name_;
962883529b6Schristos }
963883529b6Schristos
964883529b6Schristos this->declarations_.clear();
965883529b6Schristos }
966883529b6Schristos
967883529b6Schristos // Print usage statistics.
968883529b6Schristos void
print_stats()969883529b6Schristos Gdb_index_info_reader::print_stats()
970883529b6Schristos {
971883529b6Schristos fprintf(stderr, _("%s: DWARF CUs: %u\n"),
972883529b6Schristos program_name, Gdb_index_info_reader::dwarf_cu_count);
973883529b6Schristos fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
974883529b6Schristos program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
975883529b6Schristos fprintf(stderr, _("%s: DWARF TUs: %u\n"),
976883529b6Schristos program_name, Gdb_index_info_reader::dwarf_tu_count);
977883529b6Schristos fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
978883529b6Schristos program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
979883529b6Schristos }
980883529b6Schristos
981883529b6Schristos // Class Gdb_index.
982883529b6Schristos
983883529b6Schristos // Construct the .gdb_index section.
984883529b6Schristos
Gdb_index(Output_section * gdb_index_section)985883529b6Schristos Gdb_index::Gdb_index(Output_section* gdb_index_section)
986883529b6Schristos : Output_section_data(4),
9879573673dSchristos pubnames_table_(NULL),
9889573673dSchristos pubtypes_table_(NULL),
989883529b6Schristos gdb_index_section_(gdb_index_section),
990883529b6Schristos comp_units_(),
991883529b6Schristos type_units_(),
992883529b6Schristos ranges_(),
993883529b6Schristos cu_vector_list_(),
994883529b6Schristos cu_vector_offsets_(NULL),
995883529b6Schristos stringpool_(),
996883529b6Schristos tu_offset_(0),
997883529b6Schristos addr_offset_(0),
998883529b6Schristos symtab_offset_(0),
999883529b6Schristos cu_pool_offset_(0),
1000883529b6Schristos stringpool_offset_(0),
10019573673dSchristos pubnames_object_(NULL),
10029573673dSchristos stmt_list_offset_(-1)
1003883529b6Schristos {
1004883529b6Schristos this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
1005883529b6Schristos }
1006883529b6Schristos
~Gdb_index()1007883529b6Schristos Gdb_index::~Gdb_index()
1008883529b6Schristos {
1009883529b6Schristos // Free the memory used by the symbol table.
1010883529b6Schristos delete this->gdb_symtab_;
1011883529b6Schristos // Free the memory used by the CU vectors.
1012883529b6Schristos for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
1013883529b6Schristos delete this->cu_vector_list_[i];
1014883529b6Schristos }
1015883529b6Schristos
10169573673dSchristos
10179573673dSchristos // Scan the pubnames and pubtypes sections and build a map of the
10189573673dSchristos // various cus and tus they refer to, so we can process the entries
10199573673dSchristos // when we encounter the die for that cu or tu.
10209573673dSchristos // Return the just-read table so it can be cached.
10219573673dSchristos
10229573673dSchristos 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)10239573673dSchristos Gdb_index::map_pubtable_to_dies(unsigned int attr,
10249573673dSchristos Gdb_index_info_reader* dwinfo,
10259573673dSchristos Relobj* object,
10269573673dSchristos const unsigned char* symbols,
10279573673dSchristos off_t symbols_size)
10289573673dSchristos {
10299573673dSchristos uint64_t section_offset = 0;
10309573673dSchristos Dwarf_pubnames_table* table;
10319573673dSchristos Pubname_offset_map* map;
10329573673dSchristos
10339573673dSchristos if (attr == elfcpp::DW_AT_GNU_pubnames)
10349573673dSchristos {
10359573673dSchristos table = new Dwarf_pubnames_table(dwinfo, false);
10369573673dSchristos map = &this->cu_pubname_map_;
10379573673dSchristos }
10389573673dSchristos else
10399573673dSchristos {
10409573673dSchristos table = new Dwarf_pubnames_table(dwinfo, true);
10419573673dSchristos map = &this->cu_pubtype_map_;
10429573673dSchristos }
10439573673dSchristos
10449573673dSchristos map->clear();
10459573673dSchristos if (!table->read_section(object, symbols, symbols_size))
10469573673dSchristos return NULL;
10479573673dSchristos
10489573673dSchristos while (table->read_header(section_offset))
10499573673dSchristos {
10509573673dSchristos map->insert(std::make_pair(table->cu_offset(), section_offset));
10519573673dSchristos section_offset += table->subsection_size();
10529573673dSchristos }
10539573673dSchristos
10549573673dSchristos return table;
10559573673dSchristos }
10569573673dSchristos
10579573673dSchristos // Wrapper for map_pubtable_to_dies
10589573673dSchristos
10599573673dSchristos void
map_pubnames_and_types_to_dies(Gdb_index_info_reader * dwinfo,Relobj * object,const unsigned char * symbols,off_t symbols_size)10609573673dSchristos Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
10619573673dSchristos Relobj* object,
10629573673dSchristos const unsigned char* symbols,
10639573673dSchristos off_t symbols_size)
10649573673dSchristos {
10659573673dSchristos // This is a new object, so reset the relevant variables.
10669573673dSchristos this->pubnames_object_ = object;
10679573673dSchristos this->stmt_list_offset_ = -1;
10689573673dSchristos
10699573673dSchristos delete this->pubnames_table_;
10709573673dSchristos this->pubnames_table_
10719573673dSchristos = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames, dwinfo,
10729573673dSchristos object, symbols, symbols_size);
10739573673dSchristos delete this->pubtypes_table_;
10749573673dSchristos this->pubtypes_table_
10759573673dSchristos = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes, dwinfo,
10769573673dSchristos object, symbols, symbols_size);
10779573673dSchristos }
10789573673dSchristos
10799573673dSchristos // Given a cu_offset, find the associated section of the pubnames
10809573673dSchristos // table.
10819573673dSchristos
10829573673dSchristos off_t
find_pubname_offset(off_t cu_offset)10839573673dSchristos Gdb_index::find_pubname_offset(off_t cu_offset)
10849573673dSchristos {
10859573673dSchristos Pubname_offset_map::iterator it = this->cu_pubname_map_.find(cu_offset);
10869573673dSchristos if (it != this->cu_pubname_map_.end())
10879573673dSchristos return it->second;
10889573673dSchristos return -1;
10899573673dSchristos }
10909573673dSchristos
10919573673dSchristos // Given a cu_offset, find the associated section of the pubnames
10929573673dSchristos // table.
10939573673dSchristos
10949573673dSchristos off_t
find_pubtype_offset(off_t cu_offset)10959573673dSchristos Gdb_index::find_pubtype_offset(off_t cu_offset)
10969573673dSchristos {
10979573673dSchristos Pubname_offset_map::iterator it = this->cu_pubtype_map_.find(cu_offset);
10989573673dSchristos if (it != this->cu_pubtype_map_.end())
10999573673dSchristos return it->second;
11009573673dSchristos return -1;
11019573673dSchristos }
11029573673dSchristos
1103883529b6Schristos // Scan a .debug_info or .debug_types input section.
1104883529b6Schristos
1105883529b6Schristos 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)1106883529b6Schristos Gdb_index::scan_debug_info(bool is_type_unit,
1107883529b6Schristos Relobj* object,
1108883529b6Schristos const unsigned char* symbols,
1109883529b6Schristos off_t symbols_size,
1110883529b6Schristos unsigned int shndx,
1111883529b6Schristos unsigned int reloc_shndx,
1112883529b6Schristos unsigned int reloc_type)
1113883529b6Schristos {
1114883529b6Schristos Gdb_index_info_reader dwinfo(is_type_unit, object,
1115883529b6Schristos symbols, symbols_size,
1116883529b6Schristos shndx, reloc_shndx,
1117883529b6Schristos reloc_type, this);
11189573673dSchristos if (object != this->pubnames_object_)
11199573673dSchristos map_pubnames_and_types_to_dies(&dwinfo, object, symbols, symbols_size);
1120883529b6Schristos dwinfo.parse();
1121883529b6Schristos }
1122883529b6Schristos
1123883529b6Schristos // Add a symbol.
1124883529b6Schristos
1125883529b6Schristos void
add_symbol(int cu_index,const char * sym_name,uint8_t flags)11269573673dSchristos Gdb_index::add_symbol(int cu_index, const char* sym_name, uint8_t flags)
1127883529b6Schristos {
1128883529b6Schristos unsigned int hash = mapped_index_string_hash(
1129883529b6Schristos reinterpret_cast<const unsigned char*>(sym_name));
1130883529b6Schristos Gdb_symbol* sym = new Gdb_symbol();
1131883529b6Schristos this->stringpool_.add(sym_name, true, &sym->name_key);
1132883529b6Schristos sym->hashval = hash;
1133883529b6Schristos sym->cu_vector_index = 0;
1134883529b6Schristos
1135883529b6Schristos Gdb_symbol* found = this->gdb_symtab_->add(sym);
1136883529b6Schristos if (found == sym)
1137883529b6Schristos {
1138883529b6Schristos // New symbol -- allocate a new CU index vector.
1139883529b6Schristos found->cu_vector_index = this->cu_vector_list_.size();
1140883529b6Schristos this->cu_vector_list_.push_back(new Cu_vector());
1141883529b6Schristos }
1142883529b6Schristos else
1143883529b6Schristos {
1144883529b6Schristos // Found an existing symbol -- append to the existing
1145883529b6Schristos // CU index vector.
1146883529b6Schristos delete sym;
1147883529b6Schristos }
1148883529b6Schristos
1149883529b6Schristos // Add the CU index to the vector list for this symbol,
1150883529b6Schristos // if it's not already on the list. We only need to
1151883529b6Schristos // check the last added entry.
1152883529b6Schristos Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
11539573673dSchristos if (cu_vec->size() == 0
11549573673dSchristos || cu_vec->back().first != cu_index
11559573673dSchristos || cu_vec->back().second != flags)
11569573673dSchristos cu_vec->push_back(std::make_pair(cu_index, flags));
1157883529b6Schristos }
1158883529b6Schristos
11599573673dSchristos // Return TRUE if we have already processed the pubnames associated
11609573673dSchristos // with the statement list at the given OFFSET.
1161883529b6Schristos
1162883529b6Schristos bool
pubnames_read(const Relobj * object,off_t offset)11639573673dSchristos Gdb_index::pubnames_read(const Relobj* object, off_t offset)
1164883529b6Schristos {
11659573673dSchristos bool ret = (this->pubnames_object_ == object
11669573673dSchristos && this->stmt_list_offset_ == offset);
1167883529b6Schristos return ret;
1168883529b6Schristos }
1169883529b6Schristos
11709573673dSchristos // Record that we have processed the pubnames associated with the
11719573673dSchristos // statement list for OBJECT at the given OFFSET.
1172883529b6Schristos
11739573673dSchristos void
set_pubnames_read(const Relobj * object,off_t offset)11749573673dSchristos Gdb_index::set_pubnames_read(const Relobj* object, off_t offset)
1175883529b6Schristos {
11769573673dSchristos this->pubnames_object_ = object;
11779573673dSchristos this->stmt_list_offset_ = offset;
1178883529b6Schristos }
1179883529b6Schristos
1180883529b6Schristos // Set the size of the .gdb_index section.
1181883529b6Schristos
1182883529b6Schristos void
set_final_data_size()1183883529b6Schristos Gdb_index::set_final_data_size()
1184883529b6Schristos {
1185883529b6Schristos // Finalize the string pool.
1186883529b6Schristos this->stringpool_.set_string_offsets();
1187883529b6Schristos
1188883529b6Schristos // Compute the total size of the CU vectors.
1189883529b6Schristos // For each CU vector, include one entry for the count at the
1190883529b6Schristos // beginning of the vector.
1191883529b6Schristos unsigned int cu_vector_count = this->cu_vector_list_.size();
1192883529b6Schristos unsigned int cu_vector_size = 0;
1193883529b6Schristos this->cu_vector_offsets_ = new off_t[cu_vector_count];
1194883529b6Schristos for (unsigned int i = 0; i < cu_vector_count; ++i)
1195883529b6Schristos {
1196883529b6Schristos Cu_vector* cu_vec = this->cu_vector_list_[i];
1197883529b6Schristos cu_vector_offsets_[i] = cu_vector_size;
1198883529b6Schristos cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
1199883529b6Schristos }
1200883529b6Schristos
1201883529b6Schristos // Assign relative offsets to each portion of the index,
1202883529b6Schristos // and find the total size of the section.
1203883529b6Schristos section_size_type data_size = gdb_index_hdr_size;
1204883529b6Schristos data_size += this->comp_units_.size() * gdb_index_cu_size;
1205883529b6Schristos this->tu_offset_ = data_size;
1206883529b6Schristos data_size += this->type_units_.size() * gdb_index_tu_size;
1207883529b6Schristos this->addr_offset_ = data_size;
1208883529b6Schristos for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1209883529b6Schristos data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
1210883529b6Schristos this->symtab_offset_ = data_size;
1211883529b6Schristos data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
1212883529b6Schristos this->cu_pool_offset_ = data_size;
1213883529b6Schristos data_size += cu_vector_size;
1214883529b6Schristos this->stringpool_offset_ = data_size;
1215883529b6Schristos data_size += this->stringpool_.get_strtab_size();
1216883529b6Schristos
1217883529b6Schristos this->set_data_size(data_size);
1218883529b6Schristos }
1219883529b6Schristos
1220883529b6Schristos // Write the data to the file.
1221883529b6Schristos
1222883529b6Schristos void
do_write(Output_file * of)1223883529b6Schristos Gdb_index::do_write(Output_file* of)
1224883529b6Schristos {
1225883529b6Schristos const off_t off = this->offset();
1226883529b6Schristos const off_t oview_size = this->data_size();
1227883529b6Schristos unsigned char* const oview = of->get_output_view(off, oview_size);
1228883529b6Schristos unsigned char* pov = oview;
1229883529b6Schristos
1230883529b6Schristos // Write the file header.
1231883529b6Schristos // (1) Version number.
1232883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
1233883529b6Schristos pov += 4;
1234883529b6Schristos // (2) Offset of the CU list.
1235883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
1236883529b6Schristos pov += 4;
1237883529b6Schristos // (3) Offset of the types CU list.
1238883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
1239883529b6Schristos pov += 4;
1240883529b6Schristos // (4) Offset of the address area.
1241883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
1242883529b6Schristos pov += 4;
1243883529b6Schristos // (5) Offset of the symbol table.
1244883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
1245883529b6Schristos pov += 4;
1246883529b6Schristos // (6) Offset of the constant pool.
1247883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
1248883529b6Schristos pov += 4;
1249883529b6Schristos
1250883529b6Schristos gold_assert(pov - oview == gdb_index_hdr_size);
1251883529b6Schristos
1252883529b6Schristos // Write the CU list.
1253883529b6Schristos unsigned int comp_units_count = this->comp_units_.size();
1254883529b6Schristos for (unsigned int i = 0; i < comp_units_count; ++i)
1255883529b6Schristos {
1256883529b6Schristos const Comp_unit& cu = this->comp_units_[i];
1257883529b6Schristos elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
1258883529b6Schristos elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
1259883529b6Schristos pov += 16;
1260883529b6Schristos }
1261883529b6Schristos
1262883529b6Schristos gold_assert(pov - oview == this->tu_offset_);
1263883529b6Schristos
1264883529b6Schristos // Write the types CU list.
1265883529b6Schristos for (unsigned int i = 0; i < this->type_units_.size(); ++i)
1266883529b6Schristos {
1267883529b6Schristos const Type_unit& tu = this->type_units_[i];
1268883529b6Schristos elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
1269883529b6Schristos elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
1270883529b6Schristos elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
1271883529b6Schristos pov += 24;
1272883529b6Schristos }
1273883529b6Schristos
1274883529b6Schristos gold_assert(pov - oview == this->addr_offset_);
1275883529b6Schristos
1276883529b6Schristos // Write the address area.
1277883529b6Schristos for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1278883529b6Schristos {
1279883529b6Schristos int cu_index = this->ranges_[i].cu_index;
1280883529b6Schristos // Translate negative indexes, which refer to a TU, to a
1281883529b6Schristos // logical index into a concatenated CU/TU list.
1282883529b6Schristos if (cu_index < 0)
1283883529b6Schristos cu_index = comp_units_count + (-1 - cu_index);
1284883529b6Schristos Relobj* object = this->ranges_[i].object;
1285883529b6Schristos const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
1286883529b6Schristos for (unsigned int j = 0; j < ranges.size(); ++j)
1287883529b6Schristos {
1288883529b6Schristos const Dwarf_range_list::Range& range = ranges[j];
1289883529b6Schristos uint64_t base = 0;
1290883529b6Schristos if (range.shndx > 0)
1291883529b6Schristos {
1292883529b6Schristos const Output_section* os = object->output_section(range.shndx);
1293883529b6Schristos base = (os->address()
1294883529b6Schristos + object->output_section_offset(range.shndx));
1295883529b6Schristos }
1296883529b6Schristos elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
1297883529b6Schristos elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
1298883529b6Schristos base + range.end);
1299883529b6Schristos elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
1300883529b6Schristos pov += 20;
1301883529b6Schristos }
1302883529b6Schristos }
1303883529b6Schristos
1304883529b6Schristos gold_assert(pov - oview == this->symtab_offset_);
1305883529b6Schristos
1306883529b6Schristos // Write the symbol table.
1307883529b6Schristos for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
1308883529b6Schristos {
1309883529b6Schristos const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
1310883529b6Schristos section_offset_type name_offset = 0;
1311883529b6Schristos unsigned int cu_vector_offset = 0;
1312883529b6Schristos if (sym != NULL)
1313883529b6Schristos {
1314883529b6Schristos name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
1315883529b6Schristos + this->stringpool_offset_ - this->cu_pool_offset_);
1316883529b6Schristos cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
1317883529b6Schristos }
1318883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, name_offset);
1319883529b6Schristos elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
1320883529b6Schristos pov += 8;
1321883529b6Schristos }
1322883529b6Schristos
1323883529b6Schristos gold_assert(pov - oview == this->cu_pool_offset_);
1324883529b6Schristos
1325883529b6Schristos // Write the CU vectors into the constant pool.
1326883529b6Schristos for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
1327883529b6Schristos {
1328883529b6Schristos Cu_vector* cu_vec = this->cu_vector_list_[i];
1329883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
1330883529b6Schristos pov += 4;
1331883529b6Schristos for (unsigned int j = 0; j < cu_vec->size(); ++j)
1332883529b6Schristos {
13339573673dSchristos int cu_index = (*cu_vec)[j].first;
13349573673dSchristos uint8_t flags = (*cu_vec)[j].second;
1335883529b6Schristos if (cu_index < 0)
1336883529b6Schristos cu_index = comp_units_count + (-1 - cu_index);
13379573673dSchristos cu_index |= flags << 24;
1338883529b6Schristos elfcpp::Swap<32, false>::writeval(pov, cu_index);
1339883529b6Schristos pov += 4;
1340883529b6Schristos }
1341883529b6Schristos }
1342883529b6Schristos
1343883529b6Schristos gold_assert(pov - oview == this->stringpool_offset_);
1344883529b6Schristos
1345883529b6Schristos // Write the strings into the constant pool.
1346883529b6Schristos this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
1347883529b6Schristos
1348883529b6Schristos of->write_output_view(off, oview_size, oview);
1349883529b6Schristos }
1350883529b6Schristos
1351883529b6Schristos // Print usage statistics.
1352883529b6Schristos void
print_stats()1353883529b6Schristos Gdb_index::print_stats()
1354883529b6Schristos {
1355883529b6Schristos if (parameters->options().gdb_index())
1356883529b6Schristos Gdb_index_info_reader::print_stats();
1357883529b6Schristos }
1358883529b6Schristos
1359883529b6Schristos } // End namespace gold.
1360