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