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