gold/
[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), is_pubtypes_(is_pubtypes),
404 output_section_offset_(0), unit_length_(0), cu_offset_(0)
405 { }
406
407 ~Dwarf_pubnames_table()
408 {
409 if (this->owns_buffer_ && this->buffer_ != NULL)
410 delete[] this->buffer_;
411 }
412
413 // Read the pubnames section from the object file, using the symbol
414 // table for relocating it.
415 bool
416 read_section(Relobj* object, const unsigned char* symbol_table,
417 off_t symtab_size);
418
419 // Read the header for the set at OFFSET.
420 bool
421 read_header(off_t offset);
422
423 // Return the offset to the cu within the info or types section.
424 off_t
425 cu_offset()
426 { return this->cu_offset_; }
427
428 // Return the size of this subsection of the table. The unit length
429 // doesn't include the size of its own field.
430 off_t
431 subsection_size()
432 { return this->unit_length_; }
433
434 // Read the next name from the set.
435 const char*
436 next_name();
437
438 private:
439 // The Dwarf_info_reader, for reading data.
440 Dwarf_info_reader* dwinfo_;
441 // The buffer containing the .debug_ranges section.
442 const unsigned char* buffer_;
443 const unsigned char* buffer_end_;
444 // True if this object owns the buffer and needs to delete it.
445 bool owns_buffer_;
446 // The size of a DWARF offset for the current set.
447 unsigned int offset_size_;
448 // The current position within the buffer.
449 const unsigned char* pinfo_;
450 // TRUE if this is a .debug_pubtypes section.
451 bool is_pubtypes_;
452 // For incremental update links, this will hold the offset of the
453 // input section within the output section. Offsets read from
454 // relocated data will be relative to the output section, and need
455 // to be corrected before reading data from the input section.
456 uint64_t output_section_offset_;
457 // Fields read from the header.
458 uint64_t unit_length_;
459 off_t cu_offset_;
460
461 // Track relocations for this table so we can find the CUs that
462 // correspond to the subsections.
463 Elf_reloc_mapper* reloc_mapper_;
464 // Type of the relocation section (SHT_REL or SHT_RELA).
465 unsigned int reloc_type_;
466 };
467
468 // This class represents a DWARF Debug Info Entry (DIE).
469
470 class Dwarf_die
471 {
472 public:
473 // An attribute value.
474 struct Attribute_value
475 {
476 unsigned int attr;
477 unsigned int form;
478 union
479 {
480 int64_t intval;
481 uint64_t uintval;
482 const char* stringval;
483 const unsigned char* blockval;
484 off_t refval;
485 } val;
486 union
487 {
488 // Section index for reference forms.
489 unsigned int shndx;
490 // Block length for block forms.
491 unsigned int blocklen;
492 // Attribute offset for DW_FORM_strp.
493 unsigned int attr_off;
494 } aux;
495 };
496
497 // A list of attribute values.
498 typedef std::vector<Attribute_value> Attributes;
499
500 Dwarf_die(Dwarf_info_reader* dwinfo,
501 off_t die_offset,
502 Dwarf_die* parent);
503
504 // Return the DWARF tag for this DIE.
505 unsigned int
506 tag() const
507 {
508 if (this->abbrev_code_ == NULL)
509 return 0;
510 return this->abbrev_code_->tag;
511 }
512
513 // Return true if this DIE has children.
514 bool
515 has_children() const
516 {
517 gold_assert(this->abbrev_code_ != NULL);
518 return this->abbrev_code_->has_children;
519 }
520
521 // Return true if this DIE has a sibling attribute.
522 bool
523 has_sibling_attribute() const
524 {
525 gold_assert(this->abbrev_code_ != NULL);
526 return this->abbrev_code_->has_sibling_attribute;
527 }
528
529 // Return the value of attribute ATTR.
530 const Attribute_value*
531 attribute(unsigned int attr);
532
533 // Return the value of the DW_AT_name attribute.
534 const char*
535 name()
536 {
537 if (this->name_ == NULL)
538 this->set_name();
539 return this->name_;
540 }
541
542 // Return the value of the DW_AT_linkage_name
543 // or DW_AT_MIPS_linkage_name attribute.
544 const char*
545 linkage_name()
546 {
547 if (this->linkage_name_ == NULL)
548 this->set_linkage_name();
549 return this->linkage_name_;
550 }
551
552 // Return the value of the DW_AT_specification attribute.
553 off_t
554 specification()
555 {
556 if (!this->attributes_read_)
557 this->read_attributes();
558 return this->specification_;
559 }
560
561 // Return the value of the DW_AT_abstract_origin attribute.
562 off_t
563 abstract_origin()
564 {
565 if (!this->attributes_read_)
566 this->read_attributes();
567 return this->abstract_origin_;
568 }
569
570 // Return the value of attribute ATTR as a string.
571 const char*
572 string_attribute(unsigned int attr);
573
574 // Return the value of attribute ATTR as an integer.
575 int64_t
576 int_attribute(unsigned int attr);
577
578 // Return the value of attribute ATTR as an unsigned integer.
579 uint64_t
580 uint_attribute(unsigned int attr);
581
582 // Return the value of attribute ATTR as a reference.
583 off_t
584 ref_attribute(unsigned int attr, unsigned int* shndx);
585
586 // Return the value of attribute ATTR as a address.
587 off_t
588 address_attribute(unsigned int attr, unsigned int* shndx);
589
590 // Return the value of attribute ATTR as a flag.
591 bool
592 flag_attribute(unsigned int attr)
593 { return this->int_attribute(attr) != 0; }
594
595 // Return true if this DIE is a declaration.
596 bool
597 is_declaration()
598 { return this->flag_attribute(elfcpp::DW_AT_declaration); }
599
600 // Return the parent of this DIE.
601 Dwarf_die*
602 parent() const
603 { return this->parent_; }
604
605 // Return the offset of this DIE.
606 off_t
607 offset() const
608 { return this->die_offset_; }
609
610 // Return the offset of this DIE's first child.
611 off_t
612 child_offset();
613
614 // Set the offset of this DIE's next sibling.
615 void
616 set_sibling_offset(off_t sibling_offset)
617 { this->sibling_offset_ = sibling_offset; }
618
619 // Return the offset of this DIE's next sibling.
620 off_t
621 sibling_offset();
622
623 private:
624 typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
625
626 // Read all the attributes of the DIE.
627 bool
628 read_attributes();
629
630 // Set the name of the DIE if present.
631 void
632 set_name();
633
634 // Set the linkage name if present.
635 void
636 set_linkage_name();
637
638 // Skip all the attributes of the DIE and return the offset
639 // of the next DIE.
640 off_t
641 skip_attributes();
642
643 // The Dwarf_info_reader, for reading attributes.
644 Dwarf_info_reader* dwinfo_;
645 // The parent of this DIE.
646 Dwarf_die* parent_;
647 // Offset of this DIE within its compilation unit.
648 off_t die_offset_;
649 // Offset of the first attribute, relative to the beginning of the DIE.
650 off_t attr_offset_;
651 // Offset of the first child, relative to the compilation unit.
652 off_t child_offset_;
653 // Offset of the next sibling, relative to the compilation unit.
654 off_t sibling_offset_;
655 // The abbreviation table entry.
656 const Abbrev_code* abbrev_code_;
657 // The list of attributes.
658 Attributes attributes_;
659 // True if the attributes have been read.
660 bool attributes_read_;
661 // The following fields hold common attributes to avoid a linear
662 // search through the attribute list.
663 // The DIE name (DW_AT_name).
664 const char* name_;
665 // Offset of the name in the string table (for DW_FORM_strp).
666 off_t name_off_;
667 // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
668 const char* linkage_name_;
669 // Offset of the linkage name in the string table (for DW_FORM_strp).
670 off_t linkage_name_off_;
671 // Section index of the string table (for DW_FORM_strp).
672 unsigned int string_shndx_;
673 // The value of a DW_AT_specification attribute.
674 off_t specification_;
675 // The value of a DW_AT_abstract_origin attribute.
676 off_t abstract_origin_;
677 };
678
679 // This class is used to read the debug info from the .debug_info
680 // or .debug_types sections. This is a base class that implements
681 // the generic parsing of the compilation unit header and DIE
682 // structure. The parse() method parses the entire section, and
683 // calls the various visit_xxx() methods for each header. Clients
684 // should derive a new class from this one and implement the
685 // visit_compilation_unit() and visit_type_unit() functions.
686
687 class Dwarf_info_reader
688 {
689 public:
690 Dwarf_info_reader(bool is_type_unit,
691 Relobj* object,
692 const unsigned char* symtab,
693 off_t symtab_size,
694 unsigned int shndx,
695 unsigned int reloc_shndx,
696 unsigned int reloc_type)
697 : is_type_unit_(is_type_unit), object_(object), symtab_(symtab),
698 symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx),
699 reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0),
700 buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0),
701 offset_size_(0), address_size_(0), cu_version_(0),
702 abbrev_table_(), ranges_table_(this),
703 reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL),
704 owns_string_buffer_(false), string_output_section_offset_(0)
705 { }
706
707 virtual
708 ~Dwarf_info_reader()
709 {
710 if (this->reloc_mapper_ != NULL)
711 delete this->reloc_mapper_;
712 if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
713 delete[] this->string_buffer_;
714 }
715
716 // Begin parsing the debug info. This calls visit_compilation_unit()
717 // or visit_type_unit() for each compilation or type unit found in the
718 // section, and visit_die() for each top-level DIE.
719 void
720 parse();
721
722 // Return the abbrev code entry for a CODE.
723 const Dwarf_abbrev_table::Abbrev_code*
724 get_abbrev(unsigned int code)
725 { return this->abbrev_table_.get_abbrev(code); }
726
727 // Return a pointer to the DWARF info buffer at OFFSET.
728 const unsigned char*
729 buffer_at_offset(off_t offset) const
730 {
731 const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
732 if (this->check_buffer(p + 1))
733 return p;
734 return NULL;
735 }
736
737 // Read a possibly unaligned integer of SIZE.
738 template <int valsize>
739 inline typename elfcpp::Valtype_base<valsize>::Valtype
740 read_from_pointer(const unsigned char* source);
741
742 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
743 template <int valsize>
744 inline typename elfcpp::Valtype_base<valsize>::Valtype
745 read_from_pointer(const unsigned char** source);
746
747 // Look for a relocation at offset ATTR_OFF in the dwarf info,
748 // and return the section index and offset of the target.
749 unsigned int
750 lookup_reloc(off_t attr_off, off_t* target_off);
751
752 // Return a string from the DWARF string table.
753 const char*
754 get_string(off_t str_off, unsigned int string_shndx);
755
756 // Return the size of a DWARF offset.
757 unsigned int
758 offset_size() const
759 { return this->offset_size_; }
760
761 // Return the size of an address.
762 unsigned int
763 address_size() const
764 { return this->address_size_; }
765
766 // Set the section index of the .debug_abbrev section.
767 // We use this if there are no relocations for the .debug_info section.
768 // If not set, the code parse() routine will search for the section by name.
769 void
770 set_abbrev_shndx(unsigned int abbrev_shndx)
771 { this->abbrev_shndx_ = abbrev_shndx; }
772
773 // Return a pointer to the object file's ELF symbol table.
774 const unsigned char*
775 symtab() const
776 { return this->symtab_; }
777
778 // Return the size of the object file's ELF symbol table.
779 off_t
780 symtab_size() const
781 { return this->symtab_size_; }
782
783 // Return the offset of the current compilation unit.
784 off_t
785 cu_offset() const
786 { return this->cu_offset_; }
787
788 protected:
789 // Begin parsing the debug info. This calls visit_compilation_unit()
790 // or visit_type_unit() for each compilation or type unit found in the
791 // section, and visit_die() for each top-level DIE.
792 template<bool big_endian>
793 void
794 do_parse();
795
796 // The following methods are hooks that are meant to be implemented
797 // by a derived class. A default, do-nothing, implementation of
798 // each is provided for this base class.
799
800 // Visit a compilation unit.
801 virtual void
802 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
803
804 // Visit a type unit.
805 virtual void
806 visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature,
807 Dwarf_die* root_die);
808
809 // Read the range table.
810 Dwarf_range_list*
811 read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
812 {
813 return this->ranges_table_.read_range_list(this->object_,
814 this->symtab_,
815 this->symtab_size_,
816 this->address_size_,
817 ranges_shndx,
818 ranges_offset);
819 }
820
821 // Return the object.
822 Relobj*
823 object() const
824 { return this->object_; }
825
826 // Checkpoint the relocation tracker.
827 uint64_t
828 get_reloc_checkpoint() const
829 { return this->reloc_mapper_->checkpoint(); }
830
831 // Reset the relocation tracker to the CHECKPOINT.
832 void
833 reset_relocs(uint64_t checkpoint)
834 { this->reloc_mapper_->reset(checkpoint); }
835
836 private:
837 // Print a warning about a corrupt debug section.
838 void
839 warn_corrupt_debug_section() const;
840
841 // Check that P is within the bounds of the current section.
842 bool
843 check_buffer(const unsigned char* p) const
844 {
845 if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
846 {
847 this->warn_corrupt_debug_section();
848 return false;
849 }
850 return true;
851 }
852
853 // Read the DWARF string table.
854 bool
855 read_string_table(unsigned int string_shndx)
856 {
857 // If we've already read this string table, return immediately.
858 if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
859 return true;
860 if (string_shndx == 0 && this->string_shndx_ > 0)
861 return true;
862 return this->do_read_string_table(string_shndx);
863 }
864
865 bool
866 do_read_string_table(unsigned int string_shndx);
867
868 // True if this is a type unit; false for a compilation unit.
869 bool is_type_unit_;
870 // The object containing the .debug_info or .debug_types input section.
871 Relobj* object_;
872 // The ELF symbol table.
873 const unsigned char* symtab_;
874 // The size of the ELF symbol table.
875 off_t symtab_size_;
876 // Index of the .debug_info or .debug_types section.
877 unsigned int shndx_;
878 // Index of the relocation section.
879 unsigned int reloc_shndx_;
880 // Type of the relocation section (SHT_REL or SHT_RELA).
881 unsigned int reloc_type_;
882 // Index of the .debug_abbrev section (0 if not known).
883 unsigned int abbrev_shndx_;
884 // Index of the .debug_str section.
885 unsigned int string_shndx_;
886 // The buffer for the debug info.
887 const unsigned char* buffer_;
888 const unsigned char* buffer_end_;
889 // Offset of the current compilation unit.
890 off_t cu_offset_;
891 // Length of the current compilation unit.
892 off_t cu_length_;
893 // Size of a DWARF offset for the current compilation unit.
894 unsigned int offset_size_;
895 // Size of an address for the target architecture.
896 unsigned int address_size_;
897 // Compilation unit version number.
898 unsigned int cu_version_;
899 // Abbreviations table for current compilation unit.
900 Dwarf_abbrev_table abbrev_table_;
901 // Ranges table for the current compilation unit.
902 Dwarf_ranges_table ranges_table_;
903 // Relocation mapper for the section.
904 Elf_reloc_mapper* reloc_mapper_;
905 // The buffer for the debug string table.
906 const char* string_buffer_;
907 const char* string_buffer_end_;
908 // True if this object owns the buffer and needs to delete it.
909 bool owns_string_buffer_;
910 // For incremental update links, this will hold the offset of the
911 // input .debug_str section within the output section. Offsets read
912 // from relocated data will be relative to the output section, and need
913 // to be corrected before reading data from the input section.
914 uint64_t string_output_section_offset_;
915 };
916
917 // We can't do better than to keep the offsets in a sorted vector.
918 // Here, offset is the key, and file_num/line_num is the value.
919 struct Offset_to_lineno_entry
920 {
921 off_t offset;
922 int header_num; // which file-list to use (i.e. which .o file are we in)
923 // A pointer into files_.
924 unsigned int file_num : sizeof(int) * CHAR_BIT - 1;
925 // True if this was the last entry for the current offset, meaning
926 // it's the line that actually applies.
927 unsigned int last_line_for_offset : 1;
928 // The line number in the source file. -1 to indicate end-of-function.
929 int line_num;
930
931 // This sorts by offsets first, and then puts the correct line to
932 // report for a given offset at the beginning of the run of equal
933 // offsets (so that asking for 1 line gives the best answer). This
934 // is not a total ordering.
935 bool operator<(const Offset_to_lineno_entry& that) const
936 {
937 if (this->offset != that.offset)
938 return this->offset < that.offset;
939 // Note the '>' which makes this sort 'true' first.
940 return this->last_line_for_offset > that.last_line_for_offset;
941 }
942 };
943
944 // This class is used to read the line information from the debugging
945 // section of an object file.
946
947 class Dwarf_line_info
948 {
949 public:
950 Dwarf_line_info()
951 { }
952
953 virtual
954 ~Dwarf_line_info()
955 { }
956
957 // Given a section number and an offset, returns the associated
958 // file and line-number, as a string: "file:lineno". If unable
959 // to do the mapping, returns the empty string. You must call
960 // read_line_mappings() before calling this function. If
961 // 'other_lines' is non-NULL, fills that in with other line
962 // numbers assigned to the same offset.
963 std::string
964 addr2line(unsigned int shndx, off_t offset,
965 std::vector<std::string>* other_lines)
966 { return this->do_addr2line(shndx, offset, other_lines); }
967
968 // A helper function for a single addr2line lookup. It also keeps a
969 // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
970 // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
971 // chance this routine won't have to re-create a Dwarf_line_info
972 // object for its addr2line computation; such creations are slow.
973 // NOTE: Not thread-safe, so only call from one thread at a time.
974 static std::string
975 one_addr2line(Object* object, unsigned int shndx, off_t offset,
976 size_t cache_size, std::vector<std::string>* other_lines);
977
978 // This reclaims all the memory that one_addr2line may have cached.
979 // Use this when you know you will not be calling one_addr2line again.
980 static void
981 clear_addr2line_cache();
982
983 private:
984 virtual std::string
985 do_addr2line(unsigned int shndx, off_t offset,
986 std::vector<std::string>* other_lines) = 0;
987 };
988
989 template<int size, bool big_endian>
990 class Sized_dwarf_line_info : public Dwarf_line_info
991 {
992 public:
993 // Initializes a .debug_line reader for a given object file.
994 // If SHNDX is specified and non-negative, only read the debug
995 // information that pertains to the specified section.
996 Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
997
998 virtual
999 ~Sized_dwarf_line_info()
1000 {
1001 if (this->buffer_start_ != NULL)
1002 delete[] this->buffer_start_;
1003 }
1004
1005 private:
1006 std::string
1007 do_addr2line(unsigned int shndx, off_t offset,
1008 std::vector<std::string>* other_lines);
1009
1010 // Formats a file and line number to a string like "dirname/filename:lineno".
1011 std::string
1012 format_file_lineno(const Offset_to_lineno_entry& lineno) const;
1013
1014 // Start processing line info, and populates the offset_map_.
1015 // If SHNDX is non-negative, only store debug information that
1016 // pertains to the specified section.
1017 void
1018 read_line_mappings(unsigned int shndx);
1019
1020 // Reads the relocation section associated with .debug_line and
1021 // stores relocation information in reloc_map_.
1022 void
1023 read_relocs();
1024
1025 // Reads the DWARF2/3 header for this line info. Each takes as input
1026 // a starting buffer position, and returns the ending position.
1027 const unsigned char*
1028 read_header_prolog(const unsigned char* lineptr);
1029
1030 const unsigned char*
1031 read_header_tables(const unsigned char* lineptr);
1032
1033 // Reads the DWARF2/3 line information. If shndx is non-negative,
1034 // discard all line information that doesn't pertain to the given
1035 // section.
1036 const unsigned char*
1037 read_lines(const unsigned char* lineptr, unsigned int shndx);
1038
1039 // Process a single line info opcode at START using the state
1040 // machine at LSM. Return true if we should define a line using the
1041 // current state of the line state machine. Place the length of the
1042 // opcode in LEN.
1043 bool
1044 process_one_opcode(const unsigned char* start,
1045 struct LineStateMachine* lsm, size_t* len);
1046
1047 // Some parts of processing differ depending on whether the input
1048 // was a .o file or not.
1049 bool input_is_relobj();
1050
1051 // If we saw anything amiss while parsing, we set this to false.
1052 // Then addr2line will always fail (rather than return possibly-
1053 // corrupt data).
1054 bool data_valid_;
1055
1056 // A DWARF2/3 line info header. This is not the same size as in the
1057 // actual file, as the one in the file may have a 32 bit or 64 bit
1058 // lengths.
1059
1060 struct Dwarf_line_infoHeader
1061 {
1062 off_t total_length;
1063 int version;
1064 off_t prologue_length;
1065 int min_insn_length; // insn stands for instructin
1066 bool default_is_stmt; // stmt stands for statement
1067 signed char line_base;
1068 int line_range;
1069 unsigned char opcode_base;
1070 std::vector<unsigned char> std_opcode_lengths;
1071 int offset_size;
1072 } header_;
1073
1074 // buffer is the buffer for our line info, starting at exactly where
1075 // the line info to read is.
1076 const unsigned char* buffer_;
1077 const unsigned char* buffer_end_;
1078 // If the buffer was allocated temporarily, and therefore must be
1079 // deallocated in the dtor, this contains a pointer to the start
1080 // of the buffer.
1081 const unsigned char* buffer_start_;
1082
1083 // This has relocations that point into buffer.
1084 Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_;
1085 // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
1086 unsigned int track_relocs_type_;
1087
1088 // This is used to figure out what section to apply a relocation to.
1089 const unsigned char* symtab_buffer_;
1090 section_size_type symtab_buffer_size_;
1091
1092 // Holds the directories and files as we see them. We have an array
1093 // of directory-lists, one for each .o file we're reading (usually
1094 // there will just be one, but there may be more if input is a .so).
1095 std::vector<std::vector<std::string> > directories_;
1096 // The first part is an index into directories_, the second the filename.
1097 std::vector<std::vector< std::pair<int, std::string> > > files_;
1098
1099 // An index into the current directories_ and files_ vectors.
1100 int current_header_index_;
1101
1102 // A sorted map from offset of the relocation target to the shndx
1103 // and addend for the relocation.
1104 typedef std::map<off_t, std::pair<unsigned int, off_t> >
1105 Reloc_map;
1106 Reloc_map reloc_map_;
1107
1108 // We have a vector of offset->lineno entries for every input section.
1109 typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
1110 Lineno_map;
1111
1112 Lineno_map line_number_map_;
1113 };
1114
1115 } // End namespace gold.
1116
1117 #endif // !defined(GOLD_DWARF_READER_H)
This page took 0.051881 seconds and 5 git commands to generate.