Add .gdb_index version 7 support.
[deliverable/binutils-gdb.git] / gold / dwarf_reader.h
1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
2
3 // Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
6
7 // This file is part of gold.
8
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
23
24 #ifndef GOLD_DWARF_READER_H
25 #define GOLD_DWARF_READER_H
26
27 #include <vector>
28 #include <map>
29 #include <limits.h>
30 #include <sys/types.h>
31
32 #include "elfcpp.h"
33 #include "elfcpp_swap.h"
34 #include "dwarf.h"
35 #include "reloc.h"
36
37 namespace gold
38 {
39
40 class Dwarf_info_reader;
41 struct LineStateMachine;
42
43 // This class is used to extract the section index and offset of
44 // the target of a relocation for a given offset within the section.
45
46 class Elf_reloc_mapper
47 {
48 public:
49 Elf_reloc_mapper()
50 { }
51
52 virtual
53 ~Elf_reloc_mapper()
54 { }
55
56 // Initialize the relocation tracker for section RELOC_SHNDX.
57 bool
58 initialize(unsigned int reloc_shndx, unsigned int reloc_type)
59 { return this->do_initialize(reloc_shndx, reloc_type); }
60
61 // Return the next reloc_offset.
62 off_t
63 next_offset()
64 { return this->do_next_offset(); }
65
66 // Advance to the next relocation past OFFSET.
67 void
68 advance(off_t offset)
69 { this->do_advance(offset); }
70
71 // Return the section index and offset within the section of the target
72 // of the relocation for RELOC_OFFSET in the referring section.
73 unsigned int
74 get_reloc_target(off_t reloc_offset, off_t* target_offset)
75 { return this->do_get_reloc_target(reloc_offset, target_offset); }
76
77 // Checkpoint the current position in the reloc section.
78 uint64_t
79 checkpoint() const
80 { return this->do_checkpoint(); }
81
82 // Reset the current position to the CHECKPOINT.
83 void
84 reset(uint64_t checkpoint)
85 { this->do_reset(checkpoint); }
86
87 protected:
88 virtual bool
89 do_initialize(unsigned int, unsigned int) = 0;
90
91 // Return the next reloc_offset.
92 virtual off_t
93 do_next_offset() = 0;
94
95 // Advance to the next relocation past OFFSET.
96 virtual void
97 do_advance(off_t offset) = 0;
98
99 virtual unsigned int
100 do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
101
102 // Checkpoint the current position in the reloc section.
103 virtual uint64_t
104 do_checkpoint() const = 0;
105
106 // Reset the current position to the CHECKPOINT.
107 virtual void
108 do_reset(uint64_t checkpoint) = 0;
109 };
110
111 template<int size, bool big_endian>
112 class Sized_elf_reloc_mapper : public Elf_reloc_mapper
113 {
114 public:
115 Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
116 off_t symtab_size)
117 : object_(object), symtab_(symtab), symtab_size_(symtab_size),
118 reloc_type_(0), track_relocs_()
119 { }
120
121 protected:
122 bool
123 do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
124
125 // Return the next reloc_offset.
126 virtual off_t
127 do_next_offset()
128 { return this->track_relocs_.next_offset(); }
129
130 // Advance to the next relocation past OFFSET.
131 virtual void
132 do_advance(off_t offset)
133 { this->track_relocs_.advance(offset); }
134
135 unsigned int
136 do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
137
138 // Checkpoint the current position in the reloc section.
139 uint64_t
140 do_checkpoint() const
141 { return this->track_relocs_.checkpoint(); }
142
143 // Reset the current position to the CHECKPOINT.
144 void
145 do_reset(uint64_t checkpoint)
146 { this->track_relocs_.reset(checkpoint); }
147
148 private:
149 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
150
151 // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
152 // Set *IS_ORDINARY true if the section index is an ordinary section index.
153 unsigned int
154 symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
155
156 // The object file.
157 Object* object_;
158 // The ELF symbol table.
159 const unsigned char* symtab_;
160 // The size of the ELF symbol table.
161 off_t symtab_size_;
162 // Type of the relocation section (SHT_REL or SHT_RELA).
163 unsigned int reloc_type_;
164 // Relocations for the referring section.
165 Track_relocs<size, big_endian> track_relocs_;
166 };
167
168 // This class is used to read the abbreviations table from the
169 // .debug_abbrev section of the object file.
170
171 class Dwarf_abbrev_table
172 {
173 public:
174 // An attribute list entry.
175 struct Attribute
176 {
177 Attribute(unsigned int a, unsigned int f)
178 : attr(a), form(f)
179 { }
180 unsigned int attr;
181 unsigned int form;
182 };
183
184 // An abbrev code entry.
185 struct Abbrev_code
186 {
187 Abbrev_code(unsigned int t, bool hc)
188 : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
189 {
190 this->attributes.reserve(10);
191 }
192
193 void
194 add_attribute(unsigned int attr, unsigned int form)
195 {
196 this->attributes.push_back(Attribute(attr, form));
197 }
198
199 // The DWARF tag.
200 unsigned int tag;
201 // True if the DIE has children.
202 bool has_children : 1;
203 // True if the DIE has a sibling attribute.
204 bool has_sibling_attribute : 1;
205 // The list of attributes and forms.
206 std::vector<Attribute> attributes;
207 };
208
209 Dwarf_abbrev_table()
210 : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
211 owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
212 {
213 memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
214 }
215
216 ~Dwarf_abbrev_table()
217 {
218 if (this->owns_buffer_ && this->buffer_ != NULL)
219 delete[] this->buffer_;
220 this->clear_abbrev_codes();
221 }
222
223 // Read the abbrev table from an object file.
224 bool
225 read_abbrevs(Relobj* object,
226 unsigned int abbrev_shndx,
227 off_t abbrev_offset)
228 {
229 // If we've already read this abbrev table, return immediately.
230 if (this->abbrev_shndx_ > 0
231 && this->abbrev_shndx_ == abbrev_shndx
232 && this->abbrev_offset_ == abbrev_offset)
233 return true;
234 return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
235 }
236
237 // Return the abbrev code entry for CODE. This is a fast path for
238 // abbrev codes that are in the direct lookup table. If not found
239 // there, we call do_get_abbrev() to do the hard work.
240 const Abbrev_code*
241 get_abbrev(unsigned int code)
242 {
243 if (code < this->low_abbrev_code_max_
244 && this->low_abbrev_codes_[code] != NULL)
245 return this->low_abbrev_codes_[code];
246 return this->do_get_abbrev(code);
247 }
248
249 private:
250 // Read the abbrev table from an object file.
251 bool
252 do_read_abbrevs(Relobj* object,
253 unsigned int abbrev_shndx,
254 off_t abbrev_offset);
255
256 // Lookup the abbrev code entry for CODE.
257 const Abbrev_code*
258 do_get_abbrev(unsigned int code);
259
260 // Store an abbrev code entry for CODE.
261 void
262 store_abbrev(unsigned int code, const Abbrev_code* entry)
263 {
264 if (code < this->low_abbrev_code_max_)
265 this->low_abbrev_codes_[code] = entry;
266 else
267 this->high_abbrev_codes_[code] = entry;
268 }
269
270 // Clear the abbrev code table and release the memory it uses.
271 void
272 clear_abbrev_codes();
273
274 typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
275
276 // The section index of the current abbrev table.
277 unsigned int abbrev_shndx_;
278 // The offset within the section of the current abbrev table.
279 off_t abbrev_offset_;
280 // The buffer containing the .debug_abbrev section.
281 const unsigned char* buffer_;
282 const unsigned char* buffer_end_;
283 // True if this object owns the buffer and needs to delete it.
284 bool owns_buffer_;
285 // Pointer to the current position in the buffer.
286 const unsigned char* buffer_pos_;
287 // The table of abbrev codes.
288 // We use a direct-lookup array for low abbrev codes,
289 // and store the rest in a hash table.
290 static const unsigned int low_abbrev_code_max_ = 256;
291 const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_];
292 Abbrev_code_table high_abbrev_codes_;
293 };
294
295 // A DWARF range list. The start and end offsets are relative
296 // to the input section SHNDX. Each range must lie entirely
297 // within a single section.
298
299 class Dwarf_range_list
300 {
301 public:
302 struct Range
303 {
304 Range(unsigned int a_shndx, off_t a_start, off_t a_end)
305 : shndx(a_shndx), start(a_start), end(a_end)
306 { }
307
308 unsigned int shndx;
309 off_t start;
310 off_t end;
311 };
312
313 Dwarf_range_list()
314 : range_list_()
315 { }
316
317 void
318 add(unsigned int shndx, off_t start, off_t end)
319 { this->range_list_.push_back(Range(shndx, start, end)); }
320
321 size_t
322 size() const
323 { return this->range_list_.size(); }
324
325 const Range&
326 operator[](off_t i) const
327 { return this->range_list_[i]; }
328
329 private:
330 std::vector<Range> range_list_;
331 };
332
333 // This class is used to read the ranges table from the
334 // .debug_ranges section of the object file.
335
336 class Dwarf_ranges_table
337 {
338 public:
339 Dwarf_ranges_table(Dwarf_info_reader* dwinfo)
340 : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL),
341 ranges_buffer_end_(NULL), owns_ranges_buffer_(false),
342 ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0)
343 { }
344
345 ~Dwarf_ranges_table()
346 {
347 if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
348 delete[] this->ranges_buffer_;
349 if (this->ranges_reloc_mapper_ != NULL)
350 delete this->ranges_reloc_mapper_;
351 }
352
353 // Read the ranges table from an object file.
354 bool
355 read_ranges_table(Relobj* object,
356 const unsigned char* symtab,
357 off_t symtab_size,
358 unsigned int ranges_shndx);
359
360 // Read the range table from an object file.
361 Dwarf_range_list*
362 read_range_list(Relobj* object,
363 const unsigned char* symtab,
364 off_t symtab_size,
365 unsigned int address_size,
366 unsigned int ranges_shndx,
367 off_t ranges_offset);
368
369 // Look for a relocation at offset OFF in the range table,
370 // and return the section index and offset of the target.
371 unsigned int
372 lookup_reloc(off_t off, off_t* target_off);
373
374 private:
375 // The Dwarf_info_reader, for reading data.
376 Dwarf_info_reader* dwinfo_;
377 // The section index of the ranges table.
378 unsigned int ranges_shndx_;
379 // The buffer containing the .debug_ranges section.
380 const unsigned char* ranges_buffer_;
381 const unsigned char* ranges_buffer_end_;
382 // True if this object owns the buffer and needs to delete it.
383 bool owns_ranges_buffer_;
384 // Relocation mapper for the .debug_ranges section.
385 Elf_reloc_mapper* ranges_reloc_mapper_;
386 // Type of the relocation section (SHT_REL or SHT_RELA).
387 unsigned int reloc_type_;
388 // For incremental update links, this will hold the offset of the
389 // input section within the output section. Offsets read from
390 // relocated data will be relative to the output section, and need
391 // to be corrected before reading data from the input section.
392 uint64_t output_section_offset_;
393 };
394
395 // This class is used to read the pubnames and pubtypes tables from the
396 // .debug_pubnames and .debug_pubtypes sections of the object file.
397
398 class Dwarf_pubnames_table
399 {
400 public:
401 Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes)
402 : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false),
403 offset_size_(0), pinfo_(NULL), end_of_table_(NULL),
404 is_pubtypes_(is_pubtypes), is_gnu_style_(false),
405 output_section_offset_(0), unit_length_(0), cu_offset_(0)
406 { }
407
408 ~Dwarf_pubnames_table()
409 {
410 if (this->owns_buffer_ && this->buffer_ != NULL)
411 delete[] this->buffer_;
412 }
413
414 // Read the pubnames section from the object file, using the symbol
415 // table for relocating it.
416 bool
417 read_section(Relobj* object, const unsigned char* symbol_table,
418 off_t symtab_size);
419
420 // Read the header for the set at OFFSET.
421 bool
422 read_header(off_t offset);
423
424 // Return the offset to the cu within the info or types section.
425 off_t
426 cu_offset()
427 { return this->cu_offset_; }
428
429 // Return the size of this subsection of the table. The unit length
430 // doesn't include the size of its own field.
431 off_t
432 subsection_size()
433 { return this->unit_length_; }
434
435 // Read the next name from the set. If the pubname table is gnu-style,
436 // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
437 const char*
438 next_name(uint8_t* flag_byte);
439
440 private:
441 // The Dwarf_info_reader, for reading data.
442 Dwarf_info_reader* dwinfo_;
443 // The buffer containing the .debug_ranges section.
444 const unsigned char* buffer_;
445 const unsigned char* buffer_end_;
446 // True if this object owns the buffer and needs to delete it.
447 bool owns_buffer_;
448 // The size of a DWARF offset for the current set.
449 unsigned int offset_size_;
450 // The current position within the buffer.
451 const unsigned char* pinfo_;
452 // The end of the current pubnames table.
453 const unsigned char* end_of_table_;
454 // TRUE if this is a .debug_pubtypes section.
455 bool is_pubtypes_;
456 // Gnu-style pubnames table. This style has an extra flag byte between the
457 // offset and the name, and is used for generating version 7 of gdb-index.
458 bool is_gnu_style_;
459 // For incremental update links, this will hold the offset of the
460 // input section within the output section. Offsets read from
461 // relocated data will be relative to the output section, and need
462 // to be corrected before reading data from the input section.
463 uint64_t output_section_offset_;
464 // Fields read from the header.
465 uint64_t unit_length_;
466 off_t cu_offset_;
467
468 // Track relocations for this table so we can find the CUs that
469 // correspond to the subsections.
470 Elf_reloc_mapper* reloc_mapper_;
471 // Type of the relocation section (SHT_REL or SHT_RELA).
472 unsigned int reloc_type_;
473 };
474
475 // This class represents a DWARF Debug Info Entry (DIE).
476
477 class Dwarf_die
478 {
479 public:
480 // An attribute value.
481 struct Attribute_value
482 {
483 unsigned int attr;
484 unsigned int form;
485 union
486 {
487 int64_t intval;
488 uint64_t uintval;
489 const char* stringval;
490 const unsigned char* blockval;
491 off_t refval;
492 } val;
493 union
494 {
495 // Section index for reference forms.
496 unsigned int shndx;
497 // Block length for block forms.
498 unsigned int blocklen;
499 // Attribute offset for DW_FORM_strp.
500 unsigned int attr_off;
501 } aux;
502 };
503
504 // A list of attribute values.
505 typedef std::vector<Attribute_value> Attributes;
506
507 Dwarf_die(Dwarf_info_reader* dwinfo,
508 off_t die_offset,
509 Dwarf_die* parent);
510
511 // Return the DWARF tag for this DIE.
512 unsigned int
513 tag() const
514 {
515 if (this->abbrev_code_ == NULL)
516 return 0;
517 return this->abbrev_code_->tag;
518 }
519
520 // Return true if this DIE has children.
521 bool
522 has_children() const
523 {
524 gold_assert(this->abbrev_code_ != NULL);
525 return this->abbrev_code_->has_children;
526 }
527
528 // Return true if this DIE has a sibling attribute.
529 bool
530 has_sibling_attribute() const
531 {
532 gold_assert(this->abbrev_code_ != NULL);
533 return this->abbrev_code_->has_sibling_attribute;
534 }
535
536 // Return the value of attribute ATTR.
537 const Attribute_value*
538 attribute(unsigned int attr);
539
540 // Return the value of the DW_AT_name attribute.
541 const char*
542 name()
543 {
544 if (this->name_ == NULL)
545 this->set_name();
546 return this->name_;
547 }
548
549 // Return the value of the DW_AT_linkage_name
550 // or DW_AT_MIPS_linkage_name attribute.
551 const char*
552 linkage_name()
553 {
554 if (this->linkage_name_ == NULL)
555 this->set_linkage_name();
556 return this->linkage_name_;
557 }
558
559 // Return the value of the DW_AT_specification attribute.
560 off_t
561 specification()
562 {
563 if (!this->attributes_read_)
564 this->read_attributes();
565 return this->specification_;
566 }
567
568 // Return the value of the DW_AT_abstract_origin attribute.
569 off_t
570 abstract_origin()
571 {
572 if (!this->attributes_read_)
573 this->read_attributes();
574 return this->abstract_origin_;
575 }
576
577 // Return the value of attribute ATTR as a string.
578 const char*
579 string_attribute(unsigned int attr);
580
581 // Return the value of attribute ATTR as an integer.
582 int64_t
583 int_attribute(unsigned int attr);
584
585 // Return the value of attribute ATTR as an unsigned integer.
586 uint64_t
587 uint_attribute(unsigned int attr);
588
589 // Return the value of attribute ATTR as a reference.
590 off_t
591 ref_attribute(unsigned int attr, unsigned int* shndx);
592
593 // Return the value of attribute ATTR as a address.
594 off_t
595 address_attribute(unsigned int attr, unsigned int* shndx);
596
597 // Return the value of attribute ATTR as a flag.
598 bool
599 flag_attribute(unsigned int attr)
600 { return this->int_attribute(attr) != 0; }
601
602 // Return true if this DIE is a declaration.
603 bool
604 is_declaration()
605 { return this->flag_attribute(elfcpp::DW_AT_declaration); }
606
607 // Return the parent of this DIE.
608 Dwarf_die*
609 parent() const
610 { return this->parent_; }
611
612 // Return the offset of this DIE.
613 off_t
614 offset() const
615 { return this->die_offset_; }
616
617 // Return the offset of this DIE's first child.
618 off_t
619 child_offset();
620
621 // Set the offset of this DIE's next sibling.
622 void
623 set_sibling_offset(off_t sibling_offset)
624 { this->sibling_offset_ = sibling_offset; }
625
626 // Return the offset of this DIE's next sibling.
627 off_t
628 sibling_offset();
629
630 private:
631 typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
632
633 // Read all the attributes of the DIE.
634 bool
635 read_attributes();
636
637 // Set the name of the DIE if present.
638 void
639 set_name();
640
641 // Set the linkage name if present.
642 void
643 set_linkage_name();
644
645 // Skip all the attributes of the DIE and return the offset
646 // of the next DIE.
647 off_t
648 skip_attributes();
649
650 // The Dwarf_info_reader, for reading attributes.
651 Dwarf_info_reader* dwinfo_;
652 // The parent of this DIE.
653 Dwarf_die* parent_;
654 // Offset of this DIE within its compilation unit.
655 off_t die_offset_;
656 // Offset of the first attribute, relative to the beginning of the DIE.
657 off_t attr_offset_;
658 // Offset of the first child, relative to the compilation unit.
659 off_t child_offset_;
660 // Offset of the next sibling, relative to the compilation unit.
661 off_t sibling_offset_;
662 // The abbreviation table entry.
663 const Abbrev_code* abbrev_code_;
664 // The list of attributes.
665 Attributes attributes_;
666 // True if the attributes have been read.
667 bool attributes_read_;
668 // The following fields hold common attributes to avoid a linear
669 // search through the attribute list.
670 // The DIE name (DW_AT_name).
671 const char* name_;
672 // Offset of the name in the string table (for DW_FORM_strp).
673 off_t name_off_;
674 // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
675 const char* linkage_name_;
676 // Offset of the linkage name in the string table (for DW_FORM_strp).
677 off_t linkage_name_off_;
678 // Section index of the string table (for DW_FORM_strp).
679 unsigned int string_shndx_;
680 // The value of a DW_AT_specification attribute.
681 off_t specification_;
682 // The value of a DW_AT_abstract_origin attribute.
683 off_t abstract_origin_;
684 };
685
686 // This class is used to read the debug info from the .debug_info
687 // or .debug_types sections. This is a base class that implements
688 // the generic parsing of the compilation unit header and DIE
689 // structure. The parse() method parses the entire section, and
690 // calls the various visit_xxx() methods for each header. Clients
691 // should derive a new class from this one and implement the
692 // visit_compilation_unit() and visit_type_unit() functions.
693
694 class Dwarf_info_reader
695 {
696 public:
697 Dwarf_info_reader(bool is_type_unit,
698 Relobj* object,
699 const unsigned char* symtab,
700 off_t symtab_size,
701 unsigned int shndx,
702 unsigned int reloc_shndx,
703 unsigned int reloc_type)
704 : is_type_unit_(is_type_unit), object_(object), symtab_(symtab),
705 symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx),
706 reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0),
707 buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0),
708 offset_size_(0), address_size_(0), cu_version_(0),
709 abbrev_table_(), ranges_table_(this),
710 reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL),
711 owns_string_buffer_(false), string_output_section_offset_(0)
712 { }
713
714 virtual
715 ~Dwarf_info_reader()
716 {
717 if (this->reloc_mapper_ != NULL)
718 delete this->reloc_mapper_;
719 if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
720 delete[] this->string_buffer_;
721 }
722
723 // Begin parsing the debug info. This calls visit_compilation_unit()
724 // or visit_type_unit() for each compilation or type unit found in the
725 // section, and visit_die() for each top-level DIE.
726 void
727 parse();
728
729 // Return the abbrev code entry for a CODE.
730 const Dwarf_abbrev_table::Abbrev_code*
731 get_abbrev(unsigned int code)
732 { return this->abbrev_table_.get_abbrev(code); }
733
734 // Return a pointer to the DWARF info buffer at OFFSET.
735 const unsigned char*
736 buffer_at_offset(off_t offset) const
737 {
738 const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
739 if (this->check_buffer(p + 1))
740 return p;
741 return NULL;
742 }
743
744 // Read a possibly unaligned integer of SIZE.
745 template <int valsize>
746 inline typename elfcpp::Valtype_base<valsize>::Valtype
747 read_from_pointer(const unsigned char* source);
748
749 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
750 template <int valsize>
751 inline typename elfcpp::Valtype_base<valsize>::Valtype
752 read_from_pointer(const unsigned char** source);
753
754 // Look for a relocation at offset ATTR_OFF in the dwarf info,
755 // and return the section index and offset of the target.
756 unsigned int
757 lookup_reloc(off_t attr_off, off_t* target_off);
758
759 // Return a string from the DWARF string table.
760 const char*
761 get_string(off_t str_off, unsigned int string_shndx);
762
763 // Return the size of a DWARF offset.
764 unsigned int
765 offset_size() const
766 { return this->offset_size_; }
767
768 // Return the size of an address.
769 unsigned int
770 address_size() const
771 { return this->address_size_; }
772
773 // Set the section index of the .debug_abbrev section.
774 // We use this if there are no relocations for the .debug_info section.
775 // If not set, the code parse() routine will search for the section by name.
776 void
777 set_abbrev_shndx(unsigned int abbrev_shndx)
778 { this->abbrev_shndx_ = abbrev_shndx; }
779
780 // Return a pointer to the object file's ELF symbol table.
781 const unsigned char*
782 symtab() const
783 { return this->symtab_; }
784
785 // Return the size of the object file's ELF symbol table.
786 off_t
787 symtab_size() const
788 { return this->symtab_size_; }
789
790 // Return the offset of the current compilation unit.
791 off_t
792 cu_offset() const
793 { return this->cu_offset_; }
794
795 protected:
796 // Begin parsing the debug info. This calls visit_compilation_unit()
797 // or visit_type_unit() for each compilation or type unit found in the
798 // section, and visit_die() for each top-level DIE.
799 template<bool big_endian>
800 void
801 do_parse();
802
803 // The following methods are hooks that are meant to be implemented
804 // by a derived class. A default, do-nothing, implementation of
805 // each is provided for this base class.
806
807 // Visit a compilation unit.
808 virtual void
809 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
810
811 // Visit a type unit.
812 virtual void
813 visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
814 uint64_t signature, Dwarf_die* root_die);
815
816 // Read the range table.
817 Dwarf_range_list*
818 read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
819 {
820 return this->ranges_table_.read_range_list(this->object_,
821 this->symtab_,
822 this->symtab_size_,
823 this->address_size_,
824 ranges_shndx,
825 ranges_offset);
826 }
827
828 // Return the object.
829 Relobj*
830 object() const
831 { return this->object_; }
832
833 // Checkpoint the relocation tracker.
834 uint64_t
835 get_reloc_checkpoint() const
836 { return this->reloc_mapper_->checkpoint(); }
837
838 // Reset the relocation tracker to the CHECKPOINT.
839 void
840 reset_relocs(uint64_t checkpoint)
841 { this->reloc_mapper_->reset(checkpoint); }
842
843 private:
844 // Print a warning about a corrupt debug section.
845 void
846 warn_corrupt_debug_section() const;
847
848 // Check that P is within the bounds of the current section.
849 bool
850 check_buffer(const unsigned char* p) const
851 {
852 if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
853 {
854 this->warn_corrupt_debug_section();
855 return false;
856 }
857 return true;
858 }
859
860 // Read the DWARF string table.
861 bool
862 read_string_table(unsigned int string_shndx)
863 {
864 // If we've already read this string table, return immediately.
865 if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
866 return true;
867 if (string_shndx == 0 && this->string_shndx_ > 0)
868 return true;
869 return this->do_read_string_table(string_shndx);
870 }
871
872 bool
873 do_read_string_table(unsigned int string_shndx);
874
875 // True if this is a type unit; false for a compilation unit.
876 bool is_type_unit_;
877 // The object containing the .debug_info or .debug_types input section.
878 Relobj* object_;
879 // The ELF symbol table.
880 const unsigned char* symtab_;
881 // The size of the ELF symbol table.
882 off_t symtab_size_;
883 // Index of the .debug_info or .debug_types section.
884 unsigned int shndx_;
885 // Index of the relocation section.
886 unsigned int reloc_shndx_;
887 // Type of the relocation section (SHT_REL or SHT_RELA).
888 unsigned int reloc_type_;
889 // Index of the .debug_abbrev section (0 if not known).
890 unsigned int abbrev_shndx_;
891 // Index of the .debug_str section.
892 unsigned int string_shndx_;
893 // The buffer for the debug info.
894 const unsigned char* buffer_;
895 const unsigned char* buffer_end_;
896 // Offset of the current compilation unit.
897 off_t cu_offset_;
898 // Length of the current compilation unit.
899 off_t cu_length_;
900 // Size of a DWARF offset for the current compilation unit.
901 unsigned int offset_size_;
902 // Size of an address for the target architecture.
903 unsigned int address_size_;
904 // Compilation unit version number.
905 unsigned int cu_version_;
906 // Abbreviations table for current compilation unit.
907 Dwarf_abbrev_table abbrev_table_;
908 // Ranges table for the current compilation unit.
909 Dwarf_ranges_table ranges_table_;
910 // Relocation mapper for the section.
911 Elf_reloc_mapper* reloc_mapper_;
912 // The buffer for the debug string table.
913 const char* string_buffer_;
914 const char* string_buffer_end_;
915 // True if this object owns the buffer and needs to delete it.
916 bool owns_string_buffer_;
917 // For incremental update links, this will hold the offset of the
918 // input .debug_str section within the output section. Offsets read
919 // from relocated data will be relative to the output section, and need
920 // to be corrected before reading data from the input section.
921 uint64_t string_output_section_offset_;
922 };
923
924 // We can't do better than to keep the offsets in a sorted vector.
925 // Here, offset is the key, and file_num/line_num is the value.
926 struct Offset_to_lineno_entry
927 {
928 off_t offset;
929 int header_num; // which file-list to use (i.e. which .o file are we in)
930 // A pointer into files_.
931 unsigned int file_num : sizeof(int) * CHAR_BIT - 1;
932 // True if this was the last entry for the current offset, meaning
933 // it's the line that actually applies.
934 unsigned int last_line_for_offset : 1;
935 // The line number in the source file. -1 to indicate end-of-function.
936 int line_num;
937
938 // This sorts by offsets first, and then puts the correct line to
939 // report for a given offset at the beginning of the run of equal
940 // offsets (so that asking for 1 line gives the best answer). This
941 // is not a total ordering.
942 bool operator<(const Offset_to_lineno_entry& that) const
943 {
944 if (this->offset != that.offset)
945 return this->offset < that.offset;
946 // Note the '>' which makes this sort 'true' first.
947 return this->last_line_for_offset > that.last_line_for_offset;
948 }
949 };
950
951 // This class is used to read the line information from the debugging
952 // section of an object file.
953
954 class Dwarf_line_info
955 {
956 public:
957 Dwarf_line_info()
958 { }
959
960 virtual
961 ~Dwarf_line_info()
962 { }
963
964 // Given a section number and an offset, returns the associated
965 // file and line-number, as a string: "file:lineno". If unable
966 // to do the mapping, returns the empty string. You must call
967 // read_line_mappings() before calling this function. If
968 // 'other_lines' is non-NULL, fills that in with other line
969 // numbers assigned to the same offset.
970 std::string
971 addr2line(unsigned int shndx, off_t offset,
972 std::vector<std::string>* other_lines)
973 { return this->do_addr2line(shndx, offset, other_lines); }
974
975 // A helper function for a single addr2line lookup. It also keeps a
976 // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
977 // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
978 // chance this routine won't have to re-create a Dwarf_line_info
979 // object for its addr2line computation; such creations are slow.
980 // NOTE: Not thread-safe, so only call from one thread at a time.
981 static std::string
982 one_addr2line(Object* object, unsigned int shndx, off_t offset,
983 size_t cache_size, std::vector<std::string>* other_lines);
984
985 // This reclaims all the memory that one_addr2line may have cached.
986 // Use this when you know you will not be calling one_addr2line again.
987 static void
988 clear_addr2line_cache();
989
990 private:
991 virtual std::string
992 do_addr2line(unsigned int shndx, off_t offset,
993 std::vector<std::string>* other_lines) = 0;
994 };
995
996 template<int size, bool big_endian>
997 class Sized_dwarf_line_info : public Dwarf_line_info
998 {
999 public:
1000 // Initializes a .debug_line reader for a given object file.
1001 // If SHNDX is specified and non-negative, only read the debug
1002 // information that pertains to the specified section.
1003 Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
1004
1005 virtual
1006 ~Sized_dwarf_line_info()
1007 {
1008 if (this->buffer_start_ != NULL)
1009 delete[] this->buffer_start_;
1010 }
1011
1012 private:
1013 std::string
1014 do_addr2line(unsigned int shndx, off_t offset,
1015 std::vector<std::string>* other_lines);
1016
1017 // Formats a file and line number to a string like "dirname/filename:lineno".
1018 std::string
1019 format_file_lineno(const Offset_to_lineno_entry& lineno) const;
1020
1021 // Start processing line info, and populates the offset_map_.
1022 // If SHNDX is non-negative, only store debug information that
1023 // pertains to the specified section.
1024 void
1025 read_line_mappings(unsigned int shndx);
1026
1027 // Reads the relocation section associated with .debug_line and
1028 // stores relocation information in reloc_map_.
1029 void
1030 read_relocs();
1031
1032 // Reads the DWARF2/3 header for this line info. Each takes as input
1033 // a starting buffer position, and returns the ending position.
1034 const unsigned char*
1035 read_header_prolog(const unsigned char* lineptr);
1036
1037 const unsigned char*
1038 read_header_tables(const unsigned char* lineptr);
1039
1040 // Reads the DWARF2/3 line information. If shndx is non-negative,
1041 // discard all line information that doesn't pertain to the given
1042 // section.
1043 const unsigned char*
1044 read_lines(const unsigned char* lineptr, unsigned int shndx);
1045
1046 // Process a single line info opcode at START using the state
1047 // machine at LSM. Return true if we should define a line using the
1048 // current state of the line state machine. Place the length of the
1049 // opcode in LEN.
1050 bool
1051 process_one_opcode(const unsigned char* start,
1052 struct LineStateMachine* lsm, size_t* len);
1053
1054 // Some parts of processing differ depending on whether the input
1055 // was a .o file or not.
1056 bool input_is_relobj();
1057
1058 // If we saw anything amiss while parsing, we set this to false.
1059 // Then addr2line will always fail (rather than return possibly-
1060 // corrupt data).
1061 bool data_valid_;
1062
1063 // A DWARF2/3 line info header. This is not the same size as in the
1064 // actual file, as the one in the file may have a 32 bit or 64 bit
1065 // lengths.
1066
1067 struct Dwarf_line_infoHeader
1068 {
1069 off_t total_length;
1070 int version;
1071 off_t prologue_length;
1072 int min_insn_length; // insn stands for instructin
1073 bool default_is_stmt; // stmt stands for statement
1074 signed char line_base;
1075 int line_range;
1076 unsigned char opcode_base;
1077 std::vector<unsigned char> std_opcode_lengths;
1078 int offset_size;
1079 } header_;
1080
1081 // buffer is the buffer for our line info, starting at exactly where
1082 // the line info to read is.
1083 const unsigned char* buffer_;
1084 const unsigned char* buffer_end_;
1085 // If the buffer was allocated temporarily, and therefore must be
1086 // deallocated in the dtor, this contains a pointer to the start
1087 // of the buffer.
1088 const unsigned char* buffer_start_;
1089
1090 // This has relocations that point into buffer.
1091 Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_;
1092 // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
1093 unsigned int track_relocs_type_;
1094
1095 // This is used to figure out what section to apply a relocation to.
1096 const unsigned char* symtab_buffer_;
1097 section_size_type symtab_buffer_size_;
1098
1099 // Holds the directories and files as we see them. We have an array
1100 // of directory-lists, one for each .o file we're reading (usually
1101 // there will just be one, but there may be more if input is a .so).
1102 std::vector<std::vector<std::string> > directories_;
1103 // The first part is an index into directories_, the second the filename.
1104 std::vector<std::vector< std::pair<int, std::string> > > files_;
1105
1106 // An index into the current directories_ and files_ vectors.
1107 int current_header_index_;
1108
1109 // A sorted map from offset of the relocation target to the shndx
1110 // and addend for the relocation.
1111 typedef std::map<off_t, std::pair<unsigned int, off_t> >
1112 Reloc_map;
1113 Reloc_map reloc_map_;
1114
1115 // We have a vector of offset->lineno entries for every input section.
1116 typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
1117 Lineno_map;
1118
1119 Lineno_map line_number_map_;
1120 };
1121
1122 } // End namespace gold.
1123
1124 #endif // !defined(GOLD_DWARF_READER_H)
This page took 0.057563 seconds and 5 git commands to generate.