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