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