1*a9fa9459Szrj // gdb-index.cc -- generate .gdb_index section for fast debug lookup
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2012-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Cary Coutant <ccoutant@google.com>.
5*a9fa9459Szrj
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj
23*a9fa9459Szrj #include "gold.h"
24*a9fa9459Szrj
25*a9fa9459Szrj #include "gdb-index.h"
26*a9fa9459Szrj #include "dwarf_reader.h"
27*a9fa9459Szrj #include "dwarf.h"
28*a9fa9459Szrj #include "object.h"
29*a9fa9459Szrj #include "output.h"
30*a9fa9459Szrj #include "demangle.h"
31*a9fa9459Szrj
32*a9fa9459Szrj namespace gold
33*a9fa9459Szrj {
34*a9fa9459Szrj
35*a9fa9459Szrj const int gdb_index_version = 7;
36*a9fa9459Szrj
37*a9fa9459Szrj // Sizes of various records in the .gdb_index section.
38*a9fa9459Szrj const int gdb_index_offset_size = 4;
39*a9fa9459Szrj const int gdb_index_hdr_size = 6 * gdb_index_offset_size;
40*a9fa9459Szrj const int gdb_index_cu_size = 16;
41*a9fa9459Szrj const int gdb_index_tu_size = 24;
42*a9fa9459Szrj const int gdb_index_addr_size = 16 + gdb_index_offset_size;
43*a9fa9459Szrj const int gdb_index_sym_size = 2 * gdb_index_offset_size;
44*a9fa9459Szrj
45*a9fa9459Szrj // This class manages the hashed symbol table for the .gdb_index section.
46*a9fa9459Szrj // It is essentially equivalent to the hashtab implementation in libiberty,
47*a9fa9459Szrj // but is copied into gdb sources and here for compatibility because its
48*a9fa9459Szrj // data structure is exposed on disk.
49*a9fa9459Szrj
50*a9fa9459Szrj template <typename T>
51*a9fa9459Szrj class Gdb_hashtab
52*a9fa9459Szrj {
53*a9fa9459Szrj public:
Gdb_hashtab()54*a9fa9459Szrj Gdb_hashtab()
55*a9fa9459Szrj : size_(0), capacity_(0), hashtab_(NULL)
56*a9fa9459Szrj { }
57*a9fa9459Szrj
~Gdb_hashtab()58*a9fa9459Szrj ~Gdb_hashtab()
59*a9fa9459Szrj {
60*a9fa9459Szrj for (size_t i = 0; i < this->capacity_; ++i)
61*a9fa9459Szrj if (this->hashtab_[i] != NULL)
62*a9fa9459Szrj delete this->hashtab_[i];
63*a9fa9459Szrj delete[] this->hashtab_;
64*a9fa9459Szrj }
65*a9fa9459Szrj
66*a9fa9459Szrj // Add a symbol.
67*a9fa9459Szrj T*
add(T * symbol)68*a9fa9459Szrj add(T* symbol)
69*a9fa9459Szrj {
70*a9fa9459Szrj // Resize the hash table if necessary.
71*a9fa9459Szrj if (4 * this->size_ / 3 >= this->capacity_)
72*a9fa9459Szrj this->expand();
73*a9fa9459Szrj
74*a9fa9459Szrj T** slot = this->find_slot(symbol);
75*a9fa9459Szrj if (*slot == NULL)
76*a9fa9459Szrj {
77*a9fa9459Szrj ++this->size_;
78*a9fa9459Szrj *slot = symbol;
79*a9fa9459Szrj }
80*a9fa9459Szrj
81*a9fa9459Szrj return *slot;
82*a9fa9459Szrj }
83*a9fa9459Szrj
84*a9fa9459Szrj // Return the current size.
85*a9fa9459Szrj size_t
size() const86*a9fa9459Szrj size() const
87*a9fa9459Szrj { return this->size_; }
88*a9fa9459Szrj
89*a9fa9459Szrj // Return the current capacity.
90*a9fa9459Szrj size_t
capacity() const91*a9fa9459Szrj capacity() const
92*a9fa9459Szrj { return this->capacity_; }
93*a9fa9459Szrj
94*a9fa9459Szrj // Return the contents of slot N.
95*a9fa9459Szrj T*
operator [](size_t n)96*a9fa9459Szrj operator[](size_t n)
97*a9fa9459Szrj { return this->hashtab_[n]; }
98*a9fa9459Szrj
99*a9fa9459Szrj private:
100*a9fa9459Szrj // Find a symbol in the hash table, or return an empty slot if
101*a9fa9459Szrj // the symbol is not in the table.
102*a9fa9459Szrj T**
find_slot(T * symbol)103*a9fa9459Szrj find_slot(T* symbol)
104*a9fa9459Szrj {
105*a9fa9459Szrj unsigned int index = symbol->hash() & (this->capacity_ - 1);
106*a9fa9459Szrj unsigned int step = ((symbol->hash() * 17) & (this->capacity_ - 1)) | 1;
107*a9fa9459Szrj
108*a9fa9459Szrj for (;;)
109*a9fa9459Szrj {
110*a9fa9459Szrj if (this->hashtab_[index] == NULL
111*a9fa9459Szrj || this->hashtab_[index]->equal(symbol))
112*a9fa9459Szrj return &this->hashtab_[index];
113*a9fa9459Szrj index = (index + step) & (this->capacity_ - 1);
114*a9fa9459Szrj }
115*a9fa9459Szrj }
116*a9fa9459Szrj
117*a9fa9459Szrj // Expand the hash table.
118*a9fa9459Szrj void
expand()119*a9fa9459Szrj expand()
120*a9fa9459Szrj {
121*a9fa9459Szrj if (this->capacity_ == 0)
122*a9fa9459Szrj {
123*a9fa9459Szrj // Allocate the hash table for the first time.
124*a9fa9459Szrj this->capacity_ = Gdb_hashtab::initial_size;
125*a9fa9459Szrj this->hashtab_ = new T*[this->capacity_];
126*a9fa9459Szrj memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
127*a9fa9459Szrj }
128*a9fa9459Szrj else
129*a9fa9459Szrj {
130*a9fa9459Szrj // Expand and rehash.
131*a9fa9459Szrj unsigned int old_cap = this->capacity_;
132*a9fa9459Szrj T** old_hashtab = this->hashtab_;
133*a9fa9459Szrj this->capacity_ *= 2;
134*a9fa9459Szrj this->hashtab_ = new T*[this->capacity_];
135*a9fa9459Szrj memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
136*a9fa9459Szrj for (size_t i = 0; i < old_cap; ++i)
137*a9fa9459Szrj {
138*a9fa9459Szrj if (old_hashtab[i] != NULL)
139*a9fa9459Szrj {
140*a9fa9459Szrj T** slot = this->find_slot(old_hashtab[i]);
141*a9fa9459Szrj *slot = old_hashtab[i];
142*a9fa9459Szrj }
143*a9fa9459Szrj }
144*a9fa9459Szrj delete[] old_hashtab;
145*a9fa9459Szrj }
146*a9fa9459Szrj }
147*a9fa9459Szrj
148*a9fa9459Szrj // Initial size of the hash table; must be a power of 2.
149*a9fa9459Szrj static const int initial_size = 1024;
150*a9fa9459Szrj size_t size_;
151*a9fa9459Szrj size_t capacity_;
152*a9fa9459Szrj T** hashtab_;
153*a9fa9459Szrj };
154*a9fa9459Szrj
155*a9fa9459Szrj // The hash function for strings in the mapped index. This is copied
156*a9fa9459Szrj // directly from gdb/dwarf2read.c.
157*a9fa9459Szrj
158*a9fa9459Szrj static unsigned int
mapped_index_string_hash(const unsigned char * str)159*a9fa9459Szrj mapped_index_string_hash(const unsigned char* str)
160*a9fa9459Szrj {
161*a9fa9459Szrj unsigned int r = 0;
162*a9fa9459Szrj unsigned char c;
163*a9fa9459Szrj
164*a9fa9459Szrj while ((c = *str++) != 0)
165*a9fa9459Szrj {
166*a9fa9459Szrj if (gdb_index_version >= 5)
167*a9fa9459Szrj c = tolower (c);
168*a9fa9459Szrj r = r * 67 + c - 113;
169*a9fa9459Szrj }
170*a9fa9459Szrj
171*a9fa9459Szrj return r;
172*a9fa9459Szrj }
173*a9fa9459Szrj
174*a9fa9459Szrj // A specialization of Dwarf_info_reader, for building the .gdb_index.
175*a9fa9459Szrj
176*a9fa9459Szrj class Gdb_index_info_reader : public Dwarf_info_reader
177*a9fa9459Szrj {
178*a9fa9459Szrj 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)179*a9fa9459Szrj Gdb_index_info_reader(bool is_type_unit,
180*a9fa9459Szrj Relobj* object,
181*a9fa9459Szrj const unsigned char* symbols,
182*a9fa9459Szrj off_t symbols_size,
183*a9fa9459Szrj unsigned int shndx,
184*a9fa9459Szrj unsigned int reloc_shndx,
185*a9fa9459Szrj unsigned int reloc_type,
186*a9fa9459Szrj Gdb_index* gdb_index)
187*a9fa9459Szrj : Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
188*a9fa9459Szrj reloc_shndx, reloc_type),
189*a9fa9459Szrj gdb_index_(gdb_index), cu_index_(0), cu_language_(0)
190*a9fa9459Szrj { }
191*a9fa9459Szrj
~Gdb_index_info_reader()192*a9fa9459Szrj ~Gdb_index_info_reader()
193*a9fa9459Szrj { this->clear_declarations(); }
194*a9fa9459Szrj
195*a9fa9459Szrj // Print usage statistics.
196*a9fa9459Szrj static void
197*a9fa9459Szrj print_stats();
198*a9fa9459Szrj
199*a9fa9459Szrj protected:
200*a9fa9459Szrj // Visit a compilation unit.
201*a9fa9459Szrj virtual void
202*a9fa9459Szrj visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
203*a9fa9459Szrj
204*a9fa9459Szrj // Visit a type unit.
205*a9fa9459Szrj virtual void
206*a9fa9459Szrj visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
207*a9fa9459Szrj uint64_t signature, Dwarf_die*);
208*a9fa9459Szrj
209*a9fa9459Szrj private:
210*a9fa9459Szrj // A map for recording DIEs we've seen that may be referred to be
211*a9fa9459Szrj // later DIEs (via DW_AT_specification or DW_AT_abstract_origin).
212*a9fa9459Szrj // The map is indexed by a DIE offset within the compile unit.
213*a9fa9459Szrj // PARENT_OFFSET_ is the offset of the DIE that represents the
214*a9fa9459Szrj // outer context, and NAME_ is a pointer to a component of the
215*a9fa9459Szrj // fully-qualified name.
216*a9fa9459Szrj // Normally, the names we point to are in a string table, so we don't
217*a9fa9459Szrj // have to manage them, but when we have a fully-qualified name
218*a9fa9459Szrj // computed, we put it in the table, and set PARENT_OFFSET_ to -1
219*a9fa9459Szrj // indicate a string that we are managing.
220*a9fa9459Szrj struct Declaration_pair
221*a9fa9459Szrj {
Declaration_pairgold::Gdb_index_info_reader::Declaration_pair222*a9fa9459Szrj Declaration_pair(off_t parent_offset, const char* name)
223*a9fa9459Szrj : parent_offset_(parent_offset), name_(name)
224*a9fa9459Szrj { }
225*a9fa9459Szrj
226*a9fa9459Szrj off_t parent_offset_;
227*a9fa9459Szrj const char* name_;
228*a9fa9459Szrj };
229*a9fa9459Szrj typedef Unordered_map<off_t, Declaration_pair> Declaration_map;
230*a9fa9459Szrj
231*a9fa9459Szrj // Visit a top-level DIE.
232*a9fa9459Szrj void
233*a9fa9459Szrj visit_top_die(Dwarf_die* die);
234*a9fa9459Szrj
235*a9fa9459Szrj // Visit the children of a DIE.
236*a9fa9459Szrj void
237*a9fa9459Szrj visit_children(Dwarf_die* die, Dwarf_die* context);
238*a9fa9459Szrj
239*a9fa9459Szrj // Visit a DIE.
240*a9fa9459Szrj void
241*a9fa9459Szrj visit_die(Dwarf_die* die, Dwarf_die* context);
242*a9fa9459Szrj
243*a9fa9459Szrj // Visit the children of a DIE.
244*a9fa9459Szrj void
245*a9fa9459Szrj visit_children_for_decls(Dwarf_die* die);
246*a9fa9459Szrj
247*a9fa9459Szrj // Visit a DIE.
248*a9fa9459Szrj void
249*a9fa9459Szrj visit_die_for_decls(Dwarf_die* die, Dwarf_die* context);
250*a9fa9459Szrj
251*a9fa9459Szrj // Guess a fully-qualified name for a class type, based on member function
252*a9fa9459Szrj // linkage names.
253*a9fa9459Szrj std::string
254*a9fa9459Szrj guess_full_class_name(Dwarf_die* die);
255*a9fa9459Szrj
256*a9fa9459Szrj // Add a declaration DIE to the table of declarations.
257*a9fa9459Szrj void
258*a9fa9459Szrj add_declaration(Dwarf_die* die, Dwarf_die* context);
259*a9fa9459Szrj
260*a9fa9459Szrj // Add a declaration whose fully-qualified name is already known.
261*a9fa9459Szrj void
262*a9fa9459Szrj add_declaration_with_full_name(Dwarf_die* die, const char* full_name);
263*a9fa9459Szrj
264*a9fa9459Szrj // Return the context for a DIE whose parent is at DIE_OFFSET.
265*a9fa9459Szrj std::string
266*a9fa9459Szrj get_context(off_t die_offset);
267*a9fa9459Szrj
268*a9fa9459Szrj // Construct a fully-qualified name for DIE.
269*a9fa9459Szrj std::string
270*a9fa9459Szrj get_qualified_name(Dwarf_die* die, Dwarf_die* context);
271*a9fa9459Szrj
272*a9fa9459Szrj // Record the address ranges for a compilation unit.
273*a9fa9459Szrj void
274*a9fa9459Szrj record_cu_ranges(Dwarf_die* die);
275*a9fa9459Szrj
276*a9fa9459Szrj // Wrapper for read_pubtable.
277*a9fa9459Szrj bool
278*a9fa9459Szrj read_pubnames_and_pubtypes(Dwarf_die* die);
279*a9fa9459Szrj
280*a9fa9459Szrj // Read the .debug_pubnames and .debug_pubtypes tables.
281*a9fa9459Szrj bool
282*a9fa9459Szrj read_pubtable(Dwarf_pubnames_table* table, off_t offset);
283*a9fa9459Szrj
284*a9fa9459Szrj // Clear the declarations map.
285*a9fa9459Szrj void
286*a9fa9459Szrj clear_declarations();
287*a9fa9459Szrj
288*a9fa9459Szrj // The Gdb_index section.
289*a9fa9459Szrj Gdb_index* gdb_index_;
290*a9fa9459Szrj // The current CU index (negative for a TU).
291*a9fa9459Szrj int cu_index_;
292*a9fa9459Szrj // The language of the current CU or TU.
293*a9fa9459Szrj unsigned int cu_language_;
294*a9fa9459Szrj // Map from DIE offset to (parent offset, name) pair,
295*a9fa9459Szrj // for DW_AT_specification.
296*a9fa9459Szrj Declaration_map declarations_;
297*a9fa9459Szrj
298*a9fa9459Szrj // Statistics.
299*a9fa9459Szrj // Total number of DWARF compilation units processed.
300*a9fa9459Szrj static unsigned int dwarf_cu_count;
301*a9fa9459Szrj // Number of DWARF compilation units with pubnames/pubtypes.
302*a9fa9459Szrj static unsigned int dwarf_cu_nopubnames_count;
303*a9fa9459Szrj // Total number of DWARF type units processed.
304*a9fa9459Szrj static unsigned int dwarf_tu_count;
305*a9fa9459Szrj // Number of DWARF type units with pubnames/pubtypes.
306*a9fa9459Szrj static unsigned int dwarf_tu_nopubnames_count;
307*a9fa9459Szrj };
308*a9fa9459Szrj
309*a9fa9459Szrj // Total number of DWARF compilation units processed.
310*a9fa9459Szrj unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
311*a9fa9459Szrj // Number of DWARF compilation units without pubnames/pubtypes.
312*a9fa9459Szrj unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
313*a9fa9459Szrj // Total number of DWARF type units processed.
314*a9fa9459Szrj unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
315*a9fa9459Szrj // Number of DWARF type units without pubnames/pubtypes.
316*a9fa9459Szrj unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
317*a9fa9459Szrj
318*a9fa9459Szrj // Process a compilation unit and parse its child DIE.
319*a9fa9459Szrj
320*a9fa9459Szrj void
visit_compilation_unit(off_t cu_offset,off_t cu_length,Dwarf_die * root_die)321*a9fa9459Szrj Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
322*a9fa9459Szrj Dwarf_die* root_die)
323*a9fa9459Szrj {
324*a9fa9459Szrj ++Gdb_index_info_reader::dwarf_cu_count;
325*a9fa9459Szrj this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
326*a9fa9459Szrj this->visit_top_die(root_die);
327*a9fa9459Szrj }
328*a9fa9459Szrj
329*a9fa9459Szrj // Process a type unit and parse its child DIE.
330*a9fa9459Szrj
331*a9fa9459Szrj void
visit_type_unit(off_t tu_offset,off_t,off_t type_offset,uint64_t signature,Dwarf_die * root_die)332*a9fa9459Szrj Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t,
333*a9fa9459Szrj off_t type_offset, uint64_t signature,
334*a9fa9459Szrj Dwarf_die* root_die)
335*a9fa9459Szrj {
336*a9fa9459Szrj ++Gdb_index_info_reader::dwarf_tu_count;
337*a9fa9459Szrj // Use a negative index to flag this as a TU instead of a CU.
338*a9fa9459Szrj this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
339*a9fa9459Szrj signature);
340*a9fa9459Szrj this->visit_top_die(root_die);
341*a9fa9459Szrj }
342*a9fa9459Szrj
343*a9fa9459Szrj // Process a top-level DIE.
344*a9fa9459Szrj // For compile_unit DIEs, record the address ranges. For all
345*a9fa9459Szrj // interesting tags, add qualified names to the symbol table
346*a9fa9459Szrj // and process interesting children. We may need to process
347*a9fa9459Szrj // certain children just for saving declarations that might be
348*a9fa9459Szrj // referenced by later DIEs with a DW_AT_specification attribute.
349*a9fa9459Szrj
350*a9fa9459Szrj void
visit_top_die(Dwarf_die * die)351*a9fa9459Szrj Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
352*a9fa9459Szrj {
353*a9fa9459Szrj this->clear_declarations();
354*a9fa9459Szrj
355*a9fa9459Szrj switch (die->tag())
356*a9fa9459Szrj {
357*a9fa9459Szrj case elfcpp::DW_TAG_compile_unit:
358*a9fa9459Szrj case elfcpp::DW_TAG_type_unit:
359*a9fa9459Szrj this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
360*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_compile_unit)
361*a9fa9459Szrj this->record_cu_ranges(die);
362*a9fa9459Szrj // If there is a pubnames and/or pubtypes section for this
363*a9fa9459Szrj // compilation unit, use those; otherwise, parse the DWARF
364*a9fa9459Szrj // info to extract the names.
365*a9fa9459Szrj if (!this->read_pubnames_and_pubtypes(die))
366*a9fa9459Szrj {
367*a9fa9459Szrj // Check for languages that require specialized knowledge to
368*a9fa9459Szrj // construct fully-qualified names, that we don't yet support.
369*a9fa9459Szrj if (this->cu_language_ == elfcpp::DW_LANG_Ada83
370*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Fortran77
371*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Fortran90
372*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Java
373*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Ada95
374*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Fortran95
375*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Fortran03
376*a9fa9459Szrj || this->cu_language_ == elfcpp::DW_LANG_Fortran08)
377*a9fa9459Szrj {
378*a9fa9459Szrj gold_warning(_("%s: --gdb-index currently supports "
379*a9fa9459Szrj "only C and C++ languages"),
380*a9fa9459Szrj this->object()->name().c_str());
381*a9fa9459Szrj return;
382*a9fa9459Szrj }
383*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_compile_unit)
384*a9fa9459Szrj ++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
385*a9fa9459Szrj else
386*a9fa9459Szrj ++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
387*a9fa9459Szrj this->visit_children(die, NULL);
388*a9fa9459Szrj }
389*a9fa9459Szrj break;
390*a9fa9459Szrj default:
391*a9fa9459Szrj // The top level DIE should be one of the above.
392*a9fa9459Szrj gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
393*a9fa9459Szrj "or DW_TAG_type_unit"),
394*a9fa9459Szrj this->object()->name().c_str());
395*a9fa9459Szrj return;
396*a9fa9459Szrj }
397*a9fa9459Szrj }
398*a9fa9459Szrj
399*a9fa9459Szrj // Visit the children of PARENT, looking for symbols to add to the index.
400*a9fa9459Szrj // CONTEXT points to the DIE to use for constructing the qualified name --
401*a9fa9459Szrj // NULL if PARENT is the top-level DIE; otherwise it is the same as PARENT.
402*a9fa9459Szrj
403*a9fa9459Szrj void
visit_children(Dwarf_die * parent,Dwarf_die * context)404*a9fa9459Szrj Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
405*a9fa9459Szrj {
406*a9fa9459Szrj off_t next_offset = 0;
407*a9fa9459Szrj for (off_t die_offset = parent->child_offset();
408*a9fa9459Szrj die_offset != 0;
409*a9fa9459Szrj die_offset = next_offset)
410*a9fa9459Szrj {
411*a9fa9459Szrj Dwarf_die die(this, die_offset, parent);
412*a9fa9459Szrj if (die.tag() == 0)
413*a9fa9459Szrj break;
414*a9fa9459Szrj this->visit_die(&die, context);
415*a9fa9459Szrj next_offset = die.sibling_offset();
416*a9fa9459Szrj }
417*a9fa9459Szrj }
418*a9fa9459Szrj
419*a9fa9459Szrj // Visit a child DIE, looking for symbols to add to the index.
420*a9fa9459Szrj // CONTEXT is the parent DIE, used for constructing the qualified name;
421*a9fa9459Szrj // it is NULL if the parent DIE is the top-level DIE.
422*a9fa9459Szrj
423*a9fa9459Szrj void
visit_die(Dwarf_die * die,Dwarf_die * context)424*a9fa9459Szrj Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
425*a9fa9459Szrj {
426*a9fa9459Szrj switch (die->tag())
427*a9fa9459Szrj {
428*a9fa9459Szrj case elfcpp::DW_TAG_subprogram:
429*a9fa9459Szrj case elfcpp::DW_TAG_constant:
430*a9fa9459Szrj case elfcpp::DW_TAG_variable:
431*a9fa9459Szrj case elfcpp::DW_TAG_enumerator:
432*a9fa9459Szrj case elfcpp::DW_TAG_base_type:
433*a9fa9459Szrj if (die->is_declaration())
434*a9fa9459Szrj this->add_declaration(die, context);
435*a9fa9459Szrj else
436*a9fa9459Szrj {
437*a9fa9459Szrj // If the DIE is not a declaration, add it to the index.
438*a9fa9459Szrj std::string full_name = this->get_qualified_name(die, context);
439*a9fa9459Szrj if (!full_name.empty())
440*a9fa9459Szrj this->gdb_index_->add_symbol(this->cu_index_,
441*a9fa9459Szrj full_name.c_str(), 0);
442*a9fa9459Szrj }
443*a9fa9459Szrj break;
444*a9fa9459Szrj case elfcpp::DW_TAG_typedef:
445*a9fa9459Szrj case elfcpp::DW_TAG_union_type:
446*a9fa9459Szrj case elfcpp::DW_TAG_class_type:
447*a9fa9459Szrj case elfcpp::DW_TAG_interface_type:
448*a9fa9459Szrj case elfcpp::DW_TAG_structure_type:
449*a9fa9459Szrj case elfcpp::DW_TAG_enumeration_type:
450*a9fa9459Szrj case elfcpp::DW_TAG_subrange_type:
451*a9fa9459Szrj case elfcpp::DW_TAG_namespace:
452*a9fa9459Szrj {
453*a9fa9459Szrj std::string full_name;
454*a9fa9459Szrj
455*a9fa9459Szrj // For classes at the top level, we need to look for a
456*a9fa9459Szrj // member function with a linkage name in order to get
457*a9fa9459Szrj // the properly-canonicalized name.
458*a9fa9459Szrj if (context == NULL
459*a9fa9459Szrj && (die->tag() == elfcpp::DW_TAG_class_type
460*a9fa9459Szrj || die->tag() == elfcpp::DW_TAG_structure_type
461*a9fa9459Szrj || die->tag() == elfcpp::DW_TAG_union_type))
462*a9fa9459Szrj full_name.assign(this->guess_full_class_name(die));
463*a9fa9459Szrj
464*a9fa9459Szrj // Because we will visit the children, we need to add this DIE
465*a9fa9459Szrj // to the declarations table.
466*a9fa9459Szrj if (full_name.empty())
467*a9fa9459Szrj this->add_declaration(die, context);
468*a9fa9459Szrj else
469*a9fa9459Szrj this->add_declaration_with_full_name(die, full_name.c_str());
470*a9fa9459Szrj
471*a9fa9459Szrj // If the DIE is not a declaration, add it to the index.
472*a9fa9459Szrj // Gdb stores a namespace in the index even when it is
473*a9fa9459Szrj // a declaration.
474*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_namespace
475*a9fa9459Szrj || !die->is_declaration())
476*a9fa9459Szrj {
477*a9fa9459Szrj if (full_name.empty())
478*a9fa9459Szrj full_name = this->get_qualified_name(die, context);
479*a9fa9459Szrj if (!full_name.empty())
480*a9fa9459Szrj this->gdb_index_->add_symbol(this->cu_index_,
481*a9fa9459Szrj full_name.c_str(), 0);
482*a9fa9459Szrj }
483*a9fa9459Szrj
484*a9fa9459Szrj // We're interested in the children only for namespaces and
485*a9fa9459Szrj // enumeration types. For enumeration types, we do not include
486*a9fa9459Szrj // the enumeration tag as part of the full name. For other tags,
487*a9fa9459Szrj // visit the children only to collect declarations.
488*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_namespace
489*a9fa9459Szrj || die->tag() == elfcpp::DW_TAG_enumeration_type)
490*a9fa9459Szrj this->visit_children(die, die);
491*a9fa9459Szrj else
492*a9fa9459Szrj this->visit_children_for_decls(die);
493*a9fa9459Szrj }
494*a9fa9459Szrj break;
495*a9fa9459Szrj default:
496*a9fa9459Szrj break;
497*a9fa9459Szrj }
498*a9fa9459Szrj }
499*a9fa9459Szrj
500*a9fa9459Szrj // Visit the children of PARENT, looking only for declarations that
501*a9fa9459Szrj // may be referenced by later specification DIEs.
502*a9fa9459Szrj
503*a9fa9459Szrj void
visit_children_for_decls(Dwarf_die * parent)504*a9fa9459Szrj Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
505*a9fa9459Szrj {
506*a9fa9459Szrj off_t next_offset = 0;
507*a9fa9459Szrj for (off_t die_offset = parent->child_offset();
508*a9fa9459Szrj die_offset != 0;
509*a9fa9459Szrj die_offset = next_offset)
510*a9fa9459Szrj {
511*a9fa9459Szrj Dwarf_die die(this, die_offset, parent);
512*a9fa9459Szrj if (die.tag() == 0)
513*a9fa9459Szrj break;
514*a9fa9459Szrj this->visit_die_for_decls(&die, parent);
515*a9fa9459Szrj next_offset = die.sibling_offset();
516*a9fa9459Szrj }
517*a9fa9459Szrj }
518*a9fa9459Szrj
519*a9fa9459Szrj // Visit a child DIE, looking only for declarations that
520*a9fa9459Szrj // may be referenced by later specification DIEs.
521*a9fa9459Szrj
522*a9fa9459Szrj void
visit_die_for_decls(Dwarf_die * die,Dwarf_die * context)523*a9fa9459Szrj Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
524*a9fa9459Szrj {
525*a9fa9459Szrj switch (die->tag())
526*a9fa9459Szrj {
527*a9fa9459Szrj case elfcpp::DW_TAG_subprogram:
528*a9fa9459Szrj case elfcpp::DW_TAG_constant:
529*a9fa9459Szrj case elfcpp::DW_TAG_variable:
530*a9fa9459Szrj case elfcpp::DW_TAG_enumerator:
531*a9fa9459Szrj case elfcpp::DW_TAG_base_type:
532*a9fa9459Szrj {
533*a9fa9459Szrj if (die->is_declaration())
534*a9fa9459Szrj this->add_declaration(die, context);
535*a9fa9459Szrj }
536*a9fa9459Szrj break;
537*a9fa9459Szrj case elfcpp::DW_TAG_typedef:
538*a9fa9459Szrj case elfcpp::DW_TAG_union_type:
539*a9fa9459Szrj case elfcpp::DW_TAG_class_type:
540*a9fa9459Szrj case elfcpp::DW_TAG_interface_type:
541*a9fa9459Szrj case elfcpp::DW_TAG_structure_type:
542*a9fa9459Szrj case elfcpp::DW_TAG_enumeration_type:
543*a9fa9459Szrj case elfcpp::DW_TAG_subrange_type:
544*a9fa9459Szrj case elfcpp::DW_TAG_namespace:
545*a9fa9459Szrj {
546*a9fa9459Szrj if (die->is_declaration())
547*a9fa9459Szrj this->add_declaration(die, context);
548*a9fa9459Szrj this->visit_children_for_decls(die);
549*a9fa9459Szrj }
550*a9fa9459Szrj break;
551*a9fa9459Szrj default:
552*a9fa9459Szrj break;
553*a9fa9459Szrj }
554*a9fa9459Szrj }
555*a9fa9459Szrj
556*a9fa9459Szrj // Extract the class name from the linkage name of a member function.
557*a9fa9459Szrj // This code is adapted from ../gdb/cp-support.c.
558*a9fa9459Szrj
559*a9fa9459Szrj #define d_left(dc) (dc)->u.s_binary.left
560*a9fa9459Szrj #define d_right(dc) (dc)->u.s_binary.right
561*a9fa9459Szrj
562*a9fa9459Szrj static char*
class_name_from_linkage_name(const char * linkage_name)563*a9fa9459Szrj class_name_from_linkage_name(const char* linkage_name)
564*a9fa9459Szrj {
565*a9fa9459Szrj void* storage;
566*a9fa9459Szrj struct demangle_component* tree =
567*a9fa9459Szrj cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
568*a9fa9459Szrj if (tree == NULL)
569*a9fa9459Szrj return NULL;
570*a9fa9459Szrj
571*a9fa9459Szrj int done = 0;
572*a9fa9459Szrj
573*a9fa9459Szrj // First strip off any qualifiers, if we have a function or
574*a9fa9459Szrj // method.
575*a9fa9459Szrj while (!done)
576*a9fa9459Szrj switch (tree->type)
577*a9fa9459Szrj {
578*a9fa9459Szrj case DEMANGLE_COMPONENT_CONST:
579*a9fa9459Szrj case DEMANGLE_COMPONENT_RESTRICT:
580*a9fa9459Szrj case DEMANGLE_COMPONENT_VOLATILE:
581*a9fa9459Szrj case DEMANGLE_COMPONENT_CONST_THIS:
582*a9fa9459Szrj case DEMANGLE_COMPONENT_RESTRICT_THIS:
583*a9fa9459Szrj case DEMANGLE_COMPONENT_VOLATILE_THIS:
584*a9fa9459Szrj case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
585*a9fa9459Szrj tree = d_left(tree);
586*a9fa9459Szrj break;
587*a9fa9459Szrj default:
588*a9fa9459Szrj done = 1;
589*a9fa9459Szrj break;
590*a9fa9459Szrj }
591*a9fa9459Szrj
592*a9fa9459Szrj // If what we have now is a function, discard the argument list.
593*a9fa9459Szrj if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
594*a9fa9459Szrj tree = d_left(tree);
595*a9fa9459Szrj
596*a9fa9459Szrj // If what we have now is a template, strip off the template
597*a9fa9459Szrj // arguments. The left subtree may be a qualified name.
598*a9fa9459Szrj if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
599*a9fa9459Szrj tree = d_left(tree);
600*a9fa9459Szrj
601*a9fa9459Szrj // What we have now should be a name, possibly qualified.
602*a9fa9459Szrj // Additional qualifiers could live in the left subtree or the right
603*a9fa9459Szrj // subtree. Find the last piece.
604*a9fa9459Szrj done = 0;
605*a9fa9459Szrj struct demangle_component* prev_comp = NULL;
606*a9fa9459Szrj struct demangle_component* cur_comp = tree;
607*a9fa9459Szrj while (!done)
608*a9fa9459Szrj switch (cur_comp->type)
609*a9fa9459Szrj {
610*a9fa9459Szrj case DEMANGLE_COMPONENT_QUAL_NAME:
611*a9fa9459Szrj case DEMANGLE_COMPONENT_LOCAL_NAME:
612*a9fa9459Szrj prev_comp = cur_comp;
613*a9fa9459Szrj cur_comp = d_right(cur_comp);
614*a9fa9459Szrj break;
615*a9fa9459Szrj case DEMANGLE_COMPONENT_TEMPLATE:
616*a9fa9459Szrj case DEMANGLE_COMPONENT_NAME:
617*a9fa9459Szrj case DEMANGLE_COMPONENT_CTOR:
618*a9fa9459Szrj case DEMANGLE_COMPONENT_DTOR:
619*a9fa9459Szrj case DEMANGLE_COMPONENT_OPERATOR:
620*a9fa9459Szrj case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
621*a9fa9459Szrj done = 1;
622*a9fa9459Szrj break;
623*a9fa9459Szrj default:
624*a9fa9459Szrj done = 1;
625*a9fa9459Szrj cur_comp = NULL;
626*a9fa9459Szrj break;
627*a9fa9459Szrj }
628*a9fa9459Szrj
629*a9fa9459Szrj char* ret = NULL;
630*a9fa9459Szrj if (cur_comp != NULL && prev_comp != NULL)
631*a9fa9459Szrj {
632*a9fa9459Szrj // We want to discard the rightmost child of PREV_COMP.
633*a9fa9459Szrj *prev_comp = *d_left(prev_comp);
634*a9fa9459Szrj size_t allocated_size;
635*a9fa9459Szrj ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
636*a9fa9459Szrj }
637*a9fa9459Szrj
638*a9fa9459Szrj free(storage);
639*a9fa9459Szrj return ret;
640*a9fa9459Szrj }
641*a9fa9459Szrj
642*a9fa9459Szrj // Guess a fully-qualified name for a class type, based on member function
643*a9fa9459Szrj // linkage names. This is needed for class/struct/union types at the
644*a9fa9459Szrj // top level, because GCC does not always properly embed them within
645*a9fa9459Szrj // the namespace. As in gdb, we look for a member function with a linkage
646*a9fa9459Szrj // name and extract the qualified name from the demangled name.
647*a9fa9459Szrj
648*a9fa9459Szrj std::string
guess_full_class_name(Dwarf_die * die)649*a9fa9459Szrj Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
650*a9fa9459Szrj {
651*a9fa9459Szrj std::string full_name;
652*a9fa9459Szrj off_t next_offset = 0;
653*a9fa9459Szrj
654*a9fa9459Szrj // This routine scans ahead in the DIE structure, possibly advancing
655*a9fa9459Szrj // the relocation tracker beyond the current DIE. We need to checkpoint
656*a9fa9459Szrj // the tracker and reset it when we're done.
657*a9fa9459Szrj uint64_t checkpoint = this->get_reloc_checkpoint();
658*a9fa9459Szrj
659*a9fa9459Szrj for (off_t child_offset = die->child_offset();
660*a9fa9459Szrj child_offset != 0;
661*a9fa9459Szrj child_offset = next_offset)
662*a9fa9459Szrj {
663*a9fa9459Szrj Dwarf_die child(this, child_offset, die);
664*a9fa9459Szrj if (child.tag() == 0)
665*a9fa9459Szrj break;
666*a9fa9459Szrj if (child.tag() == elfcpp::DW_TAG_subprogram)
667*a9fa9459Szrj {
668*a9fa9459Szrj const char* linkage_name = child.linkage_name();
669*a9fa9459Szrj if (linkage_name != NULL)
670*a9fa9459Szrj {
671*a9fa9459Szrj char* guess = class_name_from_linkage_name(linkage_name);
672*a9fa9459Szrj if (guess != NULL)
673*a9fa9459Szrj {
674*a9fa9459Szrj full_name.assign(guess);
675*a9fa9459Szrj free(guess);
676*a9fa9459Szrj break;
677*a9fa9459Szrj }
678*a9fa9459Szrj }
679*a9fa9459Szrj }
680*a9fa9459Szrj next_offset = child.sibling_offset();
681*a9fa9459Szrj }
682*a9fa9459Szrj
683*a9fa9459Szrj this->reset_relocs(checkpoint);
684*a9fa9459Szrj return full_name;
685*a9fa9459Szrj }
686*a9fa9459Szrj
687*a9fa9459Szrj // Add a declaration DIE to the table of declarations.
688*a9fa9459Szrj
689*a9fa9459Szrj void
add_declaration(Dwarf_die * die,Dwarf_die * context)690*a9fa9459Szrj Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
691*a9fa9459Szrj {
692*a9fa9459Szrj const char* name = die->name();
693*a9fa9459Szrj
694*a9fa9459Szrj off_t parent_offset = context != NULL ? context->offset() : 0;
695*a9fa9459Szrj
696*a9fa9459Szrj // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
697*a9fa9459Szrj // attribute, use the parent and name from the earlier declaration.
698*a9fa9459Szrj off_t spec = die->specification();
699*a9fa9459Szrj if (spec == 0)
700*a9fa9459Szrj spec = die->abstract_origin();
701*a9fa9459Szrj if (spec > 0)
702*a9fa9459Szrj {
703*a9fa9459Szrj Declaration_map::iterator it = this->declarations_.find(spec);
704*a9fa9459Szrj if (it != this->declarations_.end())
705*a9fa9459Szrj {
706*a9fa9459Szrj parent_offset = it->second.parent_offset_;
707*a9fa9459Szrj name = it->second.name_;
708*a9fa9459Szrj }
709*a9fa9459Szrj }
710*a9fa9459Szrj
711*a9fa9459Szrj if (name == NULL)
712*a9fa9459Szrj {
713*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_namespace)
714*a9fa9459Szrj name = "(anonymous namespace)";
715*a9fa9459Szrj else if (die->tag() == elfcpp::DW_TAG_union_type)
716*a9fa9459Szrj name = "(anonymous union)";
717*a9fa9459Szrj else
718*a9fa9459Szrj name = "(unknown)";
719*a9fa9459Szrj }
720*a9fa9459Szrj
721*a9fa9459Szrj Declaration_pair decl(parent_offset, name);
722*a9fa9459Szrj this->declarations_.insert(std::make_pair(die->offset(), decl));
723*a9fa9459Szrj }
724*a9fa9459Szrj
725*a9fa9459Szrj // Add a declaration whose fully-qualified name is already known.
726*a9fa9459Szrj // In the case where we had to get the canonical name by demangling
727*a9fa9459Szrj // a linkage name, this ensures we use that name instead of the one
728*a9fa9459Szrj // provided in DW_AT_name.
729*a9fa9459Szrj
730*a9fa9459Szrj void
add_declaration_with_full_name(Dwarf_die * die,const char * full_name)731*a9fa9459Szrj Gdb_index_info_reader::add_declaration_with_full_name(
732*a9fa9459Szrj Dwarf_die* die,
733*a9fa9459Szrj const char* full_name)
734*a9fa9459Szrj {
735*a9fa9459Szrj // We need to copy the name.
736*a9fa9459Szrj int len = strlen(full_name);
737*a9fa9459Szrj char* copy = new char[len + 1];
738*a9fa9459Szrj memcpy(copy, full_name, len + 1);
739*a9fa9459Szrj
740*a9fa9459Szrj // Flag that we now manage the memory this points to.
741*a9fa9459Szrj Declaration_pair decl(-1, copy);
742*a9fa9459Szrj this->declarations_.insert(std::make_pair(die->offset(), decl));
743*a9fa9459Szrj }
744*a9fa9459Szrj
745*a9fa9459Szrj // Return the context for a DIE whose parent is at DIE_OFFSET.
746*a9fa9459Szrj
747*a9fa9459Szrj std::string
get_context(off_t die_offset)748*a9fa9459Szrj Gdb_index_info_reader::get_context(off_t die_offset)
749*a9fa9459Szrj {
750*a9fa9459Szrj std::string context;
751*a9fa9459Szrj Declaration_map::iterator it = this->declarations_.find(die_offset);
752*a9fa9459Szrj if (it != this->declarations_.end())
753*a9fa9459Szrj {
754*a9fa9459Szrj off_t parent_offset = it->second.parent_offset_;
755*a9fa9459Szrj if (parent_offset > 0)
756*a9fa9459Szrj {
757*a9fa9459Szrj context = get_context(parent_offset);
758*a9fa9459Szrj context.append("::");
759*a9fa9459Szrj }
760*a9fa9459Szrj if (it->second.name_ != NULL)
761*a9fa9459Szrj context.append(it->second.name_);
762*a9fa9459Szrj }
763*a9fa9459Szrj return context;
764*a9fa9459Szrj }
765*a9fa9459Szrj
766*a9fa9459Szrj // Construct the fully-qualified name for DIE.
767*a9fa9459Szrj
768*a9fa9459Szrj std::string
get_qualified_name(Dwarf_die * die,Dwarf_die * context)769*a9fa9459Szrj Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
770*a9fa9459Szrj {
771*a9fa9459Szrj std::string full_name;
772*a9fa9459Szrj const char* name = die->name();
773*a9fa9459Szrj
774*a9fa9459Szrj off_t parent_offset = context != NULL ? context->offset() : 0;
775*a9fa9459Szrj
776*a9fa9459Szrj // If this DIE has a DW_AT_specification or DW_AT_abstract_origin
777*a9fa9459Szrj // attribute, use the parent and name from the earlier declaration.
778*a9fa9459Szrj off_t spec = die->specification();
779*a9fa9459Szrj if (spec == 0)
780*a9fa9459Szrj spec = die->abstract_origin();
781*a9fa9459Szrj if (spec > 0)
782*a9fa9459Szrj {
783*a9fa9459Szrj Declaration_map::iterator it = this->declarations_.find(spec);
784*a9fa9459Szrj if (it != this->declarations_.end())
785*a9fa9459Szrj {
786*a9fa9459Szrj parent_offset = it->second.parent_offset_;
787*a9fa9459Szrj name = it->second.name_;
788*a9fa9459Szrj }
789*a9fa9459Szrj }
790*a9fa9459Szrj
791*a9fa9459Szrj if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
792*a9fa9459Szrj name = "(anonymous namespace)";
793*a9fa9459Szrj else if (name == NULL)
794*a9fa9459Szrj return full_name;
795*a9fa9459Szrj
796*a9fa9459Szrj // If this is an enumerator constant, skip the immediate parent,
797*a9fa9459Szrj // which is the enumeration tag.
798*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_enumerator)
799*a9fa9459Szrj {
800*a9fa9459Szrj Declaration_map::iterator it = this->declarations_.find(parent_offset);
801*a9fa9459Szrj if (it != this->declarations_.end())
802*a9fa9459Szrj parent_offset = it->second.parent_offset_;
803*a9fa9459Szrj }
804*a9fa9459Szrj
805*a9fa9459Szrj if (parent_offset > 0)
806*a9fa9459Szrj {
807*a9fa9459Szrj full_name.assign(this->get_context(parent_offset));
808*a9fa9459Szrj full_name.append("::");
809*a9fa9459Szrj }
810*a9fa9459Szrj full_name.append(name);
811*a9fa9459Szrj
812*a9fa9459Szrj return full_name;
813*a9fa9459Szrj }
814*a9fa9459Szrj
815*a9fa9459Szrj // Record the address ranges for a compilation unit.
816*a9fa9459Szrj
817*a9fa9459Szrj void
record_cu_ranges(Dwarf_die * die)818*a9fa9459Szrj Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
819*a9fa9459Szrj {
820*a9fa9459Szrj unsigned int shndx;
821*a9fa9459Szrj unsigned int shndx2;
822*a9fa9459Szrj
823*a9fa9459Szrj off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
824*a9fa9459Szrj if (ranges_offset != -1)
825*a9fa9459Szrj {
826*a9fa9459Szrj Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
827*a9fa9459Szrj if (ranges != NULL)
828*a9fa9459Szrj this->gdb_index_->add_address_range_list(this->object(),
829*a9fa9459Szrj this->cu_index_, ranges);
830*a9fa9459Szrj return;
831*a9fa9459Szrj }
832*a9fa9459Szrj
833*a9fa9459Szrj off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
834*a9fa9459Szrj off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
835*a9fa9459Szrj if (high_pc == -1)
836*a9fa9459Szrj {
837*a9fa9459Szrj high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
838*a9fa9459Szrj high_pc += low_pc;
839*a9fa9459Szrj shndx2 = shndx;
840*a9fa9459Szrj }
841*a9fa9459Szrj if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
842*a9fa9459Szrj {
843*a9fa9459Szrj if (shndx != shndx2)
844*a9fa9459Szrj {
845*a9fa9459Szrj gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
846*a9fa9459Szrj "are in different sections"),
847*a9fa9459Szrj this->object()->name().c_str());
848*a9fa9459Szrj return;
849*a9fa9459Szrj }
850*a9fa9459Szrj if (shndx == 0 || this->object()->is_section_included(shndx))
851*a9fa9459Szrj {
852*a9fa9459Szrj Dwarf_range_list* ranges = new Dwarf_range_list();
853*a9fa9459Szrj ranges->add(shndx, low_pc, high_pc);
854*a9fa9459Szrj this->gdb_index_->add_address_range_list(this->object(),
855*a9fa9459Szrj this->cu_index_, ranges);
856*a9fa9459Szrj }
857*a9fa9459Szrj }
858*a9fa9459Szrj }
859*a9fa9459Szrj
860*a9fa9459Szrj // Read table and add the relevant names to the index. Returns true
861*a9fa9459Szrj // if any names were added.
862*a9fa9459Szrj
863*a9fa9459Szrj bool
read_pubtable(Dwarf_pubnames_table * table,off_t offset)864*a9fa9459Szrj Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table* table, off_t offset)
865*a9fa9459Szrj {
866*a9fa9459Szrj // If we couldn't read the section when building the cu_pubname_map,
867*a9fa9459Szrj // then we won't find any pubnames now.
868*a9fa9459Szrj if (table == NULL)
869*a9fa9459Szrj return false;
870*a9fa9459Szrj
871*a9fa9459Szrj if (!table->read_header(offset))
872*a9fa9459Szrj return false;
873*a9fa9459Szrj while (true)
874*a9fa9459Szrj {
875*a9fa9459Szrj uint8_t flag_byte;
876*a9fa9459Szrj const char* name = table->next_name(&flag_byte);
877*a9fa9459Szrj if (name == NULL)
878*a9fa9459Szrj break;
879*a9fa9459Szrj
880*a9fa9459Szrj this->gdb_index_->add_symbol(this->cu_index_, name, flag_byte);
881*a9fa9459Szrj }
882*a9fa9459Szrj return true;
883*a9fa9459Szrj }
884*a9fa9459Szrj
885*a9fa9459Szrj // Read the .debug_pubnames and .debug_pubtypes tables for the CU or TU.
886*a9fa9459Szrj // Returns TRUE if either a pubnames or pubtypes section was found.
887*a9fa9459Szrj
888*a9fa9459Szrj bool
read_pubnames_and_pubtypes(Dwarf_die * die)889*a9fa9459Szrj Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
890*a9fa9459Szrj {
891*a9fa9459Szrj // If this is a skeleton debug-type die (generated via
892*a9fa9459Szrj // -gsplit-dwarf), then the associated pubnames should have been
893*a9fa9459Szrj // read along with the corresponding CU. In any case, there isn't
894*a9fa9459Szrj // enough info inside to build a gdb index entry.
895*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_type_unit
896*a9fa9459Szrj && die->string_attribute(elfcpp::DW_AT_GNU_dwo_name))
897*a9fa9459Szrj return true;
898*a9fa9459Szrj
899*a9fa9459Szrj // We use stmt_list_off as a unique identifier for the
900*a9fa9459Szrj // compilation unit and its associated type units.
901*a9fa9459Szrj unsigned int shndx;
902*a9fa9459Szrj off_t stmt_list_off = die->ref_attribute (elfcpp::DW_AT_stmt_list,
903*a9fa9459Szrj &shndx);
904*a9fa9459Szrj // Look for the attr as either a flag or a ref.
905*a9fa9459Szrj off_t offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames, &shndx);
906*a9fa9459Szrj
907*a9fa9459Szrj // Newer versions of GCC generate CUs, but not TUs, with
908*a9fa9459Szrj // DW_AT_FORM_flag_present.
909*a9fa9459Szrj unsigned int flag = die->uint_attribute(elfcpp::DW_AT_GNU_pubnames);
910*a9fa9459Szrj if (offset == -1 && flag == 0)
911*a9fa9459Szrj {
912*a9fa9459Szrj // Didn't find the attribute.
913*a9fa9459Szrj if (die->tag() == elfcpp::DW_TAG_type_unit)
914*a9fa9459Szrj {
915*a9fa9459Szrj // If die is a TU, then it might correspond to a CU which we
916*a9fa9459Szrj // have read. If it does, then no need to read the pubnames.
917*a9fa9459Szrj // If it doesn't, then the caller will have to parse the
918*a9fa9459Szrj // dies manually to find the names.
919*a9fa9459Szrj return this->gdb_index_->pubnames_read(this->object(),
920*a9fa9459Szrj stmt_list_off);
921*a9fa9459Szrj }
922*a9fa9459Szrj else
923*a9fa9459Szrj {
924*a9fa9459Szrj // No attribute on the CU means that no pubnames were read.
925*a9fa9459Szrj return false;
926*a9fa9459Szrj }
927*a9fa9459Szrj }
928*a9fa9459Szrj
929*a9fa9459Szrj // We found the attribute, so we can check if the corresponding
930*a9fa9459Szrj // pubnames have been read.
931*a9fa9459Szrj if (this->gdb_index_->pubnames_read(this->object(), stmt_list_off))
932*a9fa9459Szrj return true;
933*a9fa9459Szrj
934*a9fa9459Szrj this->gdb_index_->set_pubnames_read(this->object(), stmt_list_off);
935*a9fa9459Szrj
936*a9fa9459Szrj // We have an attribute, and the pubnames haven't been read, so read
937*a9fa9459Szrj // them.
938*a9fa9459Szrj bool names = false;
939*a9fa9459Szrj // In some of the cases, we could rely on the previous value of
940*a9fa9459Szrj // offset here, but sorting out which cases complicates the logic
941*a9fa9459Szrj // enough that it isn't worth it. So just look up the offset again.
942*a9fa9459Szrj offset = this->gdb_index_->find_pubname_offset(this->cu_offset());
943*a9fa9459Szrj names = this->read_pubtable(this->gdb_index_->pubnames_table(), offset);
944*a9fa9459Szrj
945*a9fa9459Szrj bool types = false;
946*a9fa9459Szrj offset = this->gdb_index_->find_pubtype_offset(this->cu_offset());
947*a9fa9459Szrj types = this->read_pubtable(this->gdb_index_->pubtypes_table(), offset);
948*a9fa9459Szrj return names || types;
949*a9fa9459Szrj }
950*a9fa9459Szrj
951*a9fa9459Szrj // Clear the declarations map.
952*a9fa9459Szrj void
clear_declarations()953*a9fa9459Szrj Gdb_index_info_reader::clear_declarations()
954*a9fa9459Szrj {
955*a9fa9459Szrj // Free strings in memory we manage.
956*a9fa9459Szrj for (Declaration_map::iterator it = this->declarations_.begin();
957*a9fa9459Szrj it != this->declarations_.end();
958*a9fa9459Szrj ++it)
959*a9fa9459Szrj {
960*a9fa9459Szrj if (it->second.parent_offset_ == -1)
961*a9fa9459Szrj delete[] it->second.name_;
962*a9fa9459Szrj }
963*a9fa9459Szrj
964*a9fa9459Szrj this->declarations_.clear();
965*a9fa9459Szrj }
966*a9fa9459Szrj
967*a9fa9459Szrj // Print usage statistics.
968*a9fa9459Szrj void
print_stats()969*a9fa9459Szrj Gdb_index_info_reader::print_stats()
970*a9fa9459Szrj {
971*a9fa9459Szrj fprintf(stderr, _("%s: DWARF CUs: %u\n"),
972*a9fa9459Szrj program_name, Gdb_index_info_reader::dwarf_cu_count);
973*a9fa9459Szrj fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
974*a9fa9459Szrj program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
975*a9fa9459Szrj fprintf(stderr, _("%s: DWARF TUs: %u\n"),
976*a9fa9459Szrj program_name, Gdb_index_info_reader::dwarf_tu_count);
977*a9fa9459Szrj fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
978*a9fa9459Szrj program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
979*a9fa9459Szrj }
980*a9fa9459Szrj
981*a9fa9459Szrj // Class Gdb_index.
982*a9fa9459Szrj
983*a9fa9459Szrj // Construct the .gdb_index section.
984*a9fa9459Szrj
Gdb_index(Output_section * gdb_index_section)985*a9fa9459Szrj Gdb_index::Gdb_index(Output_section* gdb_index_section)
986*a9fa9459Szrj : Output_section_data(4),
987*a9fa9459Szrj pubnames_table_(NULL),
988*a9fa9459Szrj pubtypes_table_(NULL),
989*a9fa9459Szrj gdb_index_section_(gdb_index_section),
990*a9fa9459Szrj comp_units_(),
991*a9fa9459Szrj type_units_(),
992*a9fa9459Szrj ranges_(),
993*a9fa9459Szrj cu_vector_list_(),
994*a9fa9459Szrj cu_vector_offsets_(NULL),
995*a9fa9459Szrj stringpool_(),
996*a9fa9459Szrj tu_offset_(0),
997*a9fa9459Szrj addr_offset_(0),
998*a9fa9459Szrj symtab_offset_(0),
999*a9fa9459Szrj cu_pool_offset_(0),
1000*a9fa9459Szrj stringpool_offset_(0),
1001*a9fa9459Szrj pubnames_object_(NULL),
1002*a9fa9459Szrj stmt_list_offset_(-1)
1003*a9fa9459Szrj {
1004*a9fa9459Szrj this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
1005*a9fa9459Szrj }
1006*a9fa9459Szrj
~Gdb_index()1007*a9fa9459Szrj Gdb_index::~Gdb_index()
1008*a9fa9459Szrj {
1009*a9fa9459Szrj // Free the memory used by the symbol table.
1010*a9fa9459Szrj delete this->gdb_symtab_;
1011*a9fa9459Szrj // Free the memory used by the CU vectors.
1012*a9fa9459Szrj for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
1013*a9fa9459Szrj delete this->cu_vector_list_[i];
1014*a9fa9459Szrj }
1015*a9fa9459Szrj
1016*a9fa9459Szrj
1017*a9fa9459Szrj // Scan the pubnames and pubtypes sections and build a map of the
1018*a9fa9459Szrj // various cus and tus they refer to, so we can process the entries
1019*a9fa9459Szrj // when we encounter the die for that cu or tu.
1020*a9fa9459Szrj // Return the just-read table so it can be cached.
1021*a9fa9459Szrj
1022*a9fa9459Szrj 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)1023*a9fa9459Szrj Gdb_index::map_pubtable_to_dies(unsigned int attr,
1024*a9fa9459Szrj Gdb_index_info_reader* dwinfo,
1025*a9fa9459Szrj Relobj* object,
1026*a9fa9459Szrj const unsigned char* symbols,
1027*a9fa9459Szrj off_t symbols_size)
1028*a9fa9459Szrj {
1029*a9fa9459Szrj uint64_t section_offset = 0;
1030*a9fa9459Szrj Dwarf_pubnames_table* table;
1031*a9fa9459Szrj Pubname_offset_map* map;
1032*a9fa9459Szrj
1033*a9fa9459Szrj if (attr == elfcpp::DW_AT_GNU_pubnames)
1034*a9fa9459Szrj {
1035*a9fa9459Szrj table = new Dwarf_pubnames_table(dwinfo, false);
1036*a9fa9459Szrj map = &this->cu_pubname_map_;
1037*a9fa9459Szrj }
1038*a9fa9459Szrj else
1039*a9fa9459Szrj {
1040*a9fa9459Szrj table = new Dwarf_pubnames_table(dwinfo, true);
1041*a9fa9459Szrj map = &this->cu_pubtype_map_;
1042*a9fa9459Szrj }
1043*a9fa9459Szrj
1044*a9fa9459Szrj map->clear();
1045*a9fa9459Szrj if (!table->read_section(object, symbols, symbols_size))
1046*a9fa9459Szrj return NULL;
1047*a9fa9459Szrj
1048*a9fa9459Szrj while (table->read_header(section_offset))
1049*a9fa9459Szrj {
1050*a9fa9459Szrj map->insert(std::make_pair(table->cu_offset(), section_offset));
1051*a9fa9459Szrj section_offset += table->subsection_size();
1052*a9fa9459Szrj }
1053*a9fa9459Szrj
1054*a9fa9459Szrj return table;
1055*a9fa9459Szrj }
1056*a9fa9459Szrj
1057*a9fa9459Szrj // Wrapper for map_pubtable_to_dies
1058*a9fa9459Szrj
1059*a9fa9459Szrj void
map_pubnames_and_types_to_dies(Gdb_index_info_reader * dwinfo,Relobj * object,const unsigned char * symbols,off_t symbols_size)1060*a9fa9459Szrj Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
1061*a9fa9459Szrj Relobj* object,
1062*a9fa9459Szrj const unsigned char* symbols,
1063*a9fa9459Szrj off_t symbols_size)
1064*a9fa9459Szrj {
1065*a9fa9459Szrj // This is a new object, so reset the relevant variables.
1066*a9fa9459Szrj this->pubnames_object_ = object;
1067*a9fa9459Szrj this->stmt_list_offset_ = -1;
1068*a9fa9459Szrj
1069*a9fa9459Szrj delete this->pubnames_table_;
1070*a9fa9459Szrj this->pubnames_table_
1071*a9fa9459Szrj = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames, dwinfo,
1072*a9fa9459Szrj object, symbols, symbols_size);
1073*a9fa9459Szrj delete this->pubtypes_table_;
1074*a9fa9459Szrj this->pubtypes_table_
1075*a9fa9459Szrj = this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes, dwinfo,
1076*a9fa9459Szrj object, symbols, symbols_size);
1077*a9fa9459Szrj }
1078*a9fa9459Szrj
1079*a9fa9459Szrj // Given a cu_offset, find the associated section of the pubnames
1080*a9fa9459Szrj // table.
1081*a9fa9459Szrj
1082*a9fa9459Szrj off_t
find_pubname_offset(off_t cu_offset)1083*a9fa9459Szrj Gdb_index::find_pubname_offset(off_t cu_offset)
1084*a9fa9459Szrj {
1085*a9fa9459Szrj Pubname_offset_map::iterator it = this->cu_pubname_map_.find(cu_offset);
1086*a9fa9459Szrj if (it != this->cu_pubname_map_.end())
1087*a9fa9459Szrj return it->second;
1088*a9fa9459Szrj return -1;
1089*a9fa9459Szrj }
1090*a9fa9459Szrj
1091*a9fa9459Szrj // Given a cu_offset, find the associated section of the pubnames
1092*a9fa9459Szrj // table.
1093*a9fa9459Szrj
1094*a9fa9459Szrj off_t
find_pubtype_offset(off_t cu_offset)1095*a9fa9459Szrj Gdb_index::find_pubtype_offset(off_t cu_offset)
1096*a9fa9459Szrj {
1097*a9fa9459Szrj Pubname_offset_map::iterator it = this->cu_pubtype_map_.find(cu_offset);
1098*a9fa9459Szrj if (it != this->cu_pubtype_map_.end())
1099*a9fa9459Szrj return it->second;
1100*a9fa9459Szrj return -1;
1101*a9fa9459Szrj }
1102*a9fa9459Szrj
1103*a9fa9459Szrj // Scan a .debug_info or .debug_types input section.
1104*a9fa9459Szrj
1105*a9fa9459Szrj 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)1106*a9fa9459Szrj Gdb_index::scan_debug_info(bool is_type_unit,
1107*a9fa9459Szrj Relobj* object,
1108*a9fa9459Szrj const unsigned char* symbols,
1109*a9fa9459Szrj off_t symbols_size,
1110*a9fa9459Szrj unsigned int shndx,
1111*a9fa9459Szrj unsigned int reloc_shndx,
1112*a9fa9459Szrj unsigned int reloc_type)
1113*a9fa9459Szrj {
1114*a9fa9459Szrj Gdb_index_info_reader dwinfo(is_type_unit, object,
1115*a9fa9459Szrj symbols, symbols_size,
1116*a9fa9459Szrj shndx, reloc_shndx,
1117*a9fa9459Szrj reloc_type, this);
1118*a9fa9459Szrj if (object != this->pubnames_object_)
1119*a9fa9459Szrj map_pubnames_and_types_to_dies(&dwinfo, object, symbols, symbols_size);
1120*a9fa9459Szrj dwinfo.parse();
1121*a9fa9459Szrj }
1122*a9fa9459Szrj
1123*a9fa9459Szrj // Add a symbol.
1124*a9fa9459Szrj
1125*a9fa9459Szrj void
add_symbol(int cu_index,const char * sym_name,uint8_t flags)1126*a9fa9459Szrj Gdb_index::add_symbol(int cu_index, const char* sym_name, uint8_t flags)
1127*a9fa9459Szrj {
1128*a9fa9459Szrj unsigned int hash = mapped_index_string_hash(
1129*a9fa9459Szrj reinterpret_cast<const unsigned char*>(sym_name));
1130*a9fa9459Szrj Gdb_symbol* sym = new Gdb_symbol();
1131*a9fa9459Szrj this->stringpool_.add(sym_name, true, &sym->name_key);
1132*a9fa9459Szrj sym->hashval = hash;
1133*a9fa9459Szrj sym->cu_vector_index = 0;
1134*a9fa9459Szrj
1135*a9fa9459Szrj Gdb_symbol* found = this->gdb_symtab_->add(sym);
1136*a9fa9459Szrj if (found == sym)
1137*a9fa9459Szrj {
1138*a9fa9459Szrj // New symbol -- allocate a new CU index vector.
1139*a9fa9459Szrj found->cu_vector_index = this->cu_vector_list_.size();
1140*a9fa9459Szrj this->cu_vector_list_.push_back(new Cu_vector());
1141*a9fa9459Szrj }
1142*a9fa9459Szrj else
1143*a9fa9459Szrj {
1144*a9fa9459Szrj // Found an existing symbol -- append to the existing
1145*a9fa9459Szrj // CU index vector.
1146*a9fa9459Szrj delete sym;
1147*a9fa9459Szrj }
1148*a9fa9459Szrj
1149*a9fa9459Szrj // Add the CU index to the vector list for this symbol,
1150*a9fa9459Szrj // if it's not already on the list. We only need to
1151*a9fa9459Szrj // check the last added entry.
1152*a9fa9459Szrj Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
1153*a9fa9459Szrj if (cu_vec->size() == 0
1154*a9fa9459Szrj || cu_vec->back().first != cu_index
1155*a9fa9459Szrj || cu_vec->back().second != flags)
1156*a9fa9459Szrj cu_vec->push_back(std::make_pair(cu_index, flags));
1157*a9fa9459Szrj }
1158*a9fa9459Szrj
1159*a9fa9459Szrj // Return TRUE if we have already processed the pubnames associated
1160*a9fa9459Szrj // with the statement list at the given OFFSET.
1161*a9fa9459Szrj
1162*a9fa9459Szrj bool
pubnames_read(const Relobj * object,off_t offset)1163*a9fa9459Szrj Gdb_index::pubnames_read(const Relobj* object, off_t offset)
1164*a9fa9459Szrj {
1165*a9fa9459Szrj bool ret = (this->pubnames_object_ == object
1166*a9fa9459Szrj && this->stmt_list_offset_ == offset);
1167*a9fa9459Szrj return ret;
1168*a9fa9459Szrj }
1169*a9fa9459Szrj
1170*a9fa9459Szrj // Record that we have processed the pubnames associated with the
1171*a9fa9459Szrj // statement list for OBJECT at the given OFFSET.
1172*a9fa9459Szrj
1173*a9fa9459Szrj void
set_pubnames_read(const Relobj * object,off_t offset)1174*a9fa9459Szrj Gdb_index::set_pubnames_read(const Relobj* object, off_t offset)
1175*a9fa9459Szrj {
1176*a9fa9459Szrj this->pubnames_object_ = object;
1177*a9fa9459Szrj this->stmt_list_offset_ = offset;
1178*a9fa9459Szrj }
1179*a9fa9459Szrj
1180*a9fa9459Szrj // Set the size of the .gdb_index section.
1181*a9fa9459Szrj
1182*a9fa9459Szrj void
set_final_data_size()1183*a9fa9459Szrj Gdb_index::set_final_data_size()
1184*a9fa9459Szrj {
1185*a9fa9459Szrj // Finalize the string pool.
1186*a9fa9459Szrj this->stringpool_.set_string_offsets();
1187*a9fa9459Szrj
1188*a9fa9459Szrj // Compute the total size of the CU vectors.
1189*a9fa9459Szrj // For each CU vector, include one entry for the count at the
1190*a9fa9459Szrj // beginning of the vector.
1191*a9fa9459Szrj unsigned int cu_vector_count = this->cu_vector_list_.size();
1192*a9fa9459Szrj unsigned int cu_vector_size = 0;
1193*a9fa9459Szrj this->cu_vector_offsets_ = new off_t[cu_vector_count];
1194*a9fa9459Szrj for (unsigned int i = 0; i < cu_vector_count; ++i)
1195*a9fa9459Szrj {
1196*a9fa9459Szrj Cu_vector* cu_vec = this->cu_vector_list_[i];
1197*a9fa9459Szrj cu_vector_offsets_[i] = cu_vector_size;
1198*a9fa9459Szrj cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
1199*a9fa9459Szrj }
1200*a9fa9459Szrj
1201*a9fa9459Szrj // Assign relative offsets to each portion of the index,
1202*a9fa9459Szrj // and find the total size of the section.
1203*a9fa9459Szrj section_size_type data_size = gdb_index_hdr_size;
1204*a9fa9459Szrj data_size += this->comp_units_.size() * gdb_index_cu_size;
1205*a9fa9459Szrj this->tu_offset_ = data_size;
1206*a9fa9459Szrj data_size += this->type_units_.size() * gdb_index_tu_size;
1207*a9fa9459Szrj this->addr_offset_ = data_size;
1208*a9fa9459Szrj for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1209*a9fa9459Szrj data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
1210*a9fa9459Szrj this->symtab_offset_ = data_size;
1211*a9fa9459Szrj data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
1212*a9fa9459Szrj this->cu_pool_offset_ = data_size;
1213*a9fa9459Szrj data_size += cu_vector_size;
1214*a9fa9459Szrj this->stringpool_offset_ = data_size;
1215*a9fa9459Szrj data_size += this->stringpool_.get_strtab_size();
1216*a9fa9459Szrj
1217*a9fa9459Szrj this->set_data_size(data_size);
1218*a9fa9459Szrj }
1219*a9fa9459Szrj
1220*a9fa9459Szrj // Write the data to the file.
1221*a9fa9459Szrj
1222*a9fa9459Szrj void
do_write(Output_file * of)1223*a9fa9459Szrj Gdb_index::do_write(Output_file* of)
1224*a9fa9459Szrj {
1225*a9fa9459Szrj const off_t off = this->offset();
1226*a9fa9459Szrj const off_t oview_size = this->data_size();
1227*a9fa9459Szrj unsigned char* const oview = of->get_output_view(off, oview_size);
1228*a9fa9459Szrj unsigned char* pov = oview;
1229*a9fa9459Szrj
1230*a9fa9459Szrj // Write the file header.
1231*a9fa9459Szrj // (1) Version number.
1232*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
1233*a9fa9459Szrj pov += 4;
1234*a9fa9459Szrj // (2) Offset of the CU list.
1235*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
1236*a9fa9459Szrj pov += 4;
1237*a9fa9459Szrj // (3) Offset of the types CU list.
1238*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
1239*a9fa9459Szrj pov += 4;
1240*a9fa9459Szrj // (4) Offset of the address area.
1241*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
1242*a9fa9459Szrj pov += 4;
1243*a9fa9459Szrj // (5) Offset of the symbol table.
1244*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
1245*a9fa9459Szrj pov += 4;
1246*a9fa9459Szrj // (6) Offset of the constant pool.
1247*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
1248*a9fa9459Szrj pov += 4;
1249*a9fa9459Szrj
1250*a9fa9459Szrj gold_assert(pov - oview == gdb_index_hdr_size);
1251*a9fa9459Szrj
1252*a9fa9459Szrj // Write the CU list.
1253*a9fa9459Szrj unsigned int comp_units_count = this->comp_units_.size();
1254*a9fa9459Szrj for (unsigned int i = 0; i < comp_units_count; ++i)
1255*a9fa9459Szrj {
1256*a9fa9459Szrj const Comp_unit& cu = this->comp_units_[i];
1257*a9fa9459Szrj elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
1258*a9fa9459Szrj elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
1259*a9fa9459Szrj pov += 16;
1260*a9fa9459Szrj }
1261*a9fa9459Szrj
1262*a9fa9459Szrj gold_assert(pov - oview == this->tu_offset_);
1263*a9fa9459Szrj
1264*a9fa9459Szrj // Write the types CU list.
1265*a9fa9459Szrj for (unsigned int i = 0; i < this->type_units_.size(); ++i)
1266*a9fa9459Szrj {
1267*a9fa9459Szrj const Type_unit& tu = this->type_units_[i];
1268*a9fa9459Szrj elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
1269*a9fa9459Szrj elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
1270*a9fa9459Szrj elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
1271*a9fa9459Szrj pov += 24;
1272*a9fa9459Szrj }
1273*a9fa9459Szrj
1274*a9fa9459Szrj gold_assert(pov - oview == this->addr_offset_);
1275*a9fa9459Szrj
1276*a9fa9459Szrj // Write the address area.
1277*a9fa9459Szrj for (unsigned int i = 0; i < this->ranges_.size(); ++i)
1278*a9fa9459Szrj {
1279*a9fa9459Szrj int cu_index = this->ranges_[i].cu_index;
1280*a9fa9459Szrj // Translate negative indexes, which refer to a TU, to a
1281*a9fa9459Szrj // logical index into a concatenated CU/TU list.
1282*a9fa9459Szrj if (cu_index < 0)
1283*a9fa9459Szrj cu_index = comp_units_count + (-1 - cu_index);
1284*a9fa9459Szrj Relobj* object = this->ranges_[i].object;
1285*a9fa9459Szrj const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
1286*a9fa9459Szrj for (unsigned int j = 0; j < ranges.size(); ++j)
1287*a9fa9459Szrj {
1288*a9fa9459Szrj const Dwarf_range_list::Range& range = ranges[j];
1289*a9fa9459Szrj uint64_t base = 0;
1290*a9fa9459Szrj if (range.shndx > 0)
1291*a9fa9459Szrj {
1292*a9fa9459Szrj const Output_section* os = object->output_section(range.shndx);
1293*a9fa9459Szrj base = (os->address()
1294*a9fa9459Szrj + object->output_section_offset(range.shndx));
1295*a9fa9459Szrj }
1296*a9fa9459Szrj elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
1297*a9fa9459Szrj elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
1298*a9fa9459Szrj base + range.end);
1299*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
1300*a9fa9459Szrj pov += 20;
1301*a9fa9459Szrj }
1302*a9fa9459Szrj }
1303*a9fa9459Szrj
1304*a9fa9459Szrj gold_assert(pov - oview == this->symtab_offset_);
1305*a9fa9459Szrj
1306*a9fa9459Szrj // Write the symbol table.
1307*a9fa9459Szrj for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
1308*a9fa9459Szrj {
1309*a9fa9459Szrj const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
1310*a9fa9459Szrj section_offset_type name_offset = 0;
1311*a9fa9459Szrj unsigned int cu_vector_offset = 0;
1312*a9fa9459Szrj if (sym != NULL)
1313*a9fa9459Szrj {
1314*a9fa9459Szrj name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
1315*a9fa9459Szrj + this->stringpool_offset_ - this->cu_pool_offset_);
1316*a9fa9459Szrj cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
1317*a9fa9459Szrj }
1318*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, name_offset);
1319*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
1320*a9fa9459Szrj pov += 8;
1321*a9fa9459Szrj }
1322*a9fa9459Szrj
1323*a9fa9459Szrj gold_assert(pov - oview == this->cu_pool_offset_);
1324*a9fa9459Szrj
1325*a9fa9459Szrj // Write the CU vectors into the constant pool.
1326*a9fa9459Szrj for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
1327*a9fa9459Szrj {
1328*a9fa9459Szrj Cu_vector* cu_vec = this->cu_vector_list_[i];
1329*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
1330*a9fa9459Szrj pov += 4;
1331*a9fa9459Szrj for (unsigned int j = 0; j < cu_vec->size(); ++j)
1332*a9fa9459Szrj {
1333*a9fa9459Szrj int cu_index = (*cu_vec)[j].first;
1334*a9fa9459Szrj uint8_t flags = (*cu_vec)[j].second;
1335*a9fa9459Szrj if (cu_index < 0)
1336*a9fa9459Szrj cu_index = comp_units_count + (-1 - cu_index);
1337*a9fa9459Szrj cu_index |= flags << 24;
1338*a9fa9459Szrj elfcpp::Swap<32, false>::writeval(pov, cu_index);
1339*a9fa9459Szrj pov += 4;
1340*a9fa9459Szrj }
1341*a9fa9459Szrj }
1342*a9fa9459Szrj
1343*a9fa9459Szrj gold_assert(pov - oview == this->stringpool_offset_);
1344*a9fa9459Szrj
1345*a9fa9459Szrj // Write the strings into the constant pool.
1346*a9fa9459Szrj this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
1347*a9fa9459Szrj
1348*a9fa9459Szrj of->write_output_view(off, oview_size, oview);
1349*a9fa9459Szrj }
1350*a9fa9459Szrj
1351*a9fa9459Szrj // Print usage statistics.
1352*a9fa9459Szrj void
print_stats()1353*a9fa9459Szrj Gdb_index::print_stats()
1354*a9fa9459Szrj {
1355*a9fa9459Szrj if (parameters->options().gdb_index())
1356*a9fa9459Szrj Gdb_index_info_reader::print_stats();
1357*a9fa9459Szrj }
1358*a9fa9459Szrj
1359*a9fa9459Szrj } // End namespace gold.
1360