1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
3 // Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
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.
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.
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.
24 #ifndef GOLD_DWARF_READER_H
25 #define GOLD_DWARF_READER_H
30 #include <sys/types.h>
33 #include "elfcpp_swap.h"
40 class Dwarf_info_reader;
41 struct LineStateMachine;
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.
46 class Elf_reloc_mapper
56 // Initialize the relocation tracker for section RELOC_SHNDX.
58 initialize(unsigned int reloc_shndx, unsigned int reloc_type)
59 { return this->do_initialize(reloc_shndx, reloc_type); }
61 // Return the next reloc_offset.
64 { return this->do_next_offset(); }
66 // Advance to the next relocation past OFFSET.
69 { this->do_advance(offset); }
71 // Return the section index and offset within the section of the target
72 // of the relocation for RELOC_OFFSET in the referring section.
74 get_reloc_target(off_t reloc_offset, off_t* target_offset)
75 { return this->do_get_reloc_target(reloc_offset, target_offset); }
77 // Checkpoint the current position in the reloc section.
80 { return this->do_checkpoint(); }
82 // Reset the current position to the CHECKPOINT.
84 reset(uint64_t checkpoint)
85 { this->do_reset(checkpoint); }
89 do_initialize(unsigned int, unsigned int) = 0;
91 // Return the next reloc_offset.
95 // Advance to the next relocation past OFFSET.
97 do_advance(off_t offset) = 0;
100 do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
102 // Checkpoint the current position in the reloc section.
104 do_checkpoint() const = 0;
106 // Reset the current position to the CHECKPOINT.
108 do_reset(uint64_t checkpoint) = 0;
111 template<int size, bool big_endian>
112 class Sized_elf_reloc_mapper : public Elf_reloc_mapper
115 Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
117 : object_(object), symtab_(symtab), symtab_size_(symtab_size),
118 reloc_type_(0), track_relocs_()
123 do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
125 // Return the next reloc_offset.
128 { return this->track_relocs_.next_offset(); }
130 // Advance to the next relocation past OFFSET.
132 do_advance(off_t offset)
133 { this->track_relocs_.advance(offset); }
136 do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
138 // Checkpoint the current position in the reloc section.
140 do_checkpoint() const
141 { return this->track_relocs_.checkpoint(); }
143 // Reset the current position to the CHECKPOINT.
145 do_reset(uint64_t checkpoint)
146 { this->track_relocs_.reset(checkpoint); }
149 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
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.
154 symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
158 // The ELF symbol table.
159 const unsigned char* symtab_;
160 // The size of the ELF symbol table.
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_;
168 // This class is used to read the abbreviations table from the
169 // .debug_abbrev section of the object file.
171 class Dwarf_abbrev_table
174 // An attribute list entry.
177 Attribute(unsigned int a, unsigned int f)
184 // An abbrev code entry.
187 Abbrev_code(unsigned int t, bool hc)
188 : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
190 this->attributes.reserve(10);
194 add_attribute(unsigned int attr, unsigned int form)
196 this->attributes.push_back(Attribute(attr, form));
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;
210 : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
211 owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
213 memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
216 ~Dwarf_abbrev_table()
218 if (this->owns_buffer_ && this->buffer_ != NULL)
219 delete[] this->buffer_;
220 this->clear_abbrev_codes();
223 // Read the abbrev table from an object file.
225 read_abbrevs(Relobj* object,
226 unsigned int abbrev_shndx,
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)
234 return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
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.
241 get_abbrev(unsigned int code)
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);
250 // Read the abbrev table from an object file.
252 do_read_abbrevs(Relobj* object,
253 unsigned int abbrev_shndx,
254 off_t abbrev_offset);
256 // Lookup the abbrev code entry for CODE.
258 do_get_abbrev(unsigned int code);
260 // Store an abbrev code entry for CODE.
262 store_abbrev(unsigned int code, const Abbrev_code* entry)
264 if (code < this->low_abbrev_code_max_)
265 this->low_abbrev_codes_[code] = entry;
267 this->high_abbrev_codes_[code] = entry;
270 // Clear the abbrev code table and release the memory it uses.
272 clear_abbrev_codes();
274 typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
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.
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_;
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.
299 class Dwarf_range_list
304 Range(unsigned int a_shndx, off_t a_start, off_t a_end)
305 : shndx(a_shndx), start(a_start), end(a_end)
318 add(unsigned int shndx, off_t start, off_t end)
319 { this->range_list_.push_back(Range(shndx, start, end)); }
323 { return this->range_list_.size(); }
326 operator[](off_t i) const
327 { return this->range_list_[i]; }
330 std::vector<Range> range_list_;
333 // This class is used to read the ranges table from the
334 // .debug_ranges section of the object file.
336 class Dwarf_ranges_table
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)
345 ~Dwarf_ranges_table()
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_;
353 // Read the ranges table from an object file.
355 read_ranges_table(Relobj* object,
356 const unsigned char* symtab,
358 unsigned int ranges_shndx);
360 // Read the range table from an object file.
362 read_range_list(Relobj* object,
363 const unsigned char* symtab,
365 unsigned int address_size,
366 unsigned int ranges_shndx,
367 off_t ranges_offset);
369 // Look for a relocation at offset OFF in the range table,
370 // and return the section index and offset of the target.
372 lookup_reloc(off_t off, off_t* target_off);
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_;
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.
398 class Dwarf_pubnames_table
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)
407 ~Dwarf_pubnames_table()
409 if (this->owns_buffer_ && this->buffer_ != NULL)
410 delete[] this->buffer_;
413 // Read the pubnames section from the object file, using the symbol
414 // table for relocating it.
416 read_section(Relobj* object, const unsigned char* symbol_table,
419 // Read the header for the set at OFFSET.
421 read_header(off_t offset);
423 // Return the offset to the cu within the info or types section.
426 { return this->cu_offset_; }
428 // Return the size of this subsection of the table. The unit length
429 // doesn't include the size of its own field.
432 { return this->unit_length_; }
434 // Read the next name from the set.
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.
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.
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_;
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_;
468 // This class represents a DWARF Debug Info Entry (DIE).
473 // An attribute value.
474 struct Attribute_value
482 const char* stringval;
483 const unsigned char* blockval;
488 // Section index for reference forms.
490 // Block length for block forms.
491 unsigned int blocklen;
492 // Attribute offset for DW_FORM_strp.
493 unsigned int attr_off;
497 // A list of attribute values.
498 typedef std::vector<Attribute_value> Attributes;
500 Dwarf_die(Dwarf_info_reader* dwinfo,
504 // Return the DWARF tag for this DIE.
508 if (this->abbrev_code_ == NULL)
510 return this->abbrev_code_->tag;
513 // Return true if this DIE has children.
517 gold_assert(this->abbrev_code_ != NULL);
518 return this->abbrev_code_->has_children;
521 // Return true if this DIE has a sibling attribute.
523 has_sibling_attribute() const
525 gold_assert(this->abbrev_code_ != NULL);
526 return this->abbrev_code_->has_sibling_attribute;
529 // Return the value of attribute ATTR.
530 const Attribute_value*
531 attribute(unsigned int attr);
533 // Return the value of the DW_AT_name attribute.
537 if (this->name_ == NULL)
542 // Return the value of the DW_AT_linkage_name
543 // or DW_AT_MIPS_linkage_name attribute.
547 if (this->linkage_name_ == NULL)
548 this->set_linkage_name();
549 return this->linkage_name_;
552 // Return the value of the DW_AT_specification attribute.
556 if (!this->attributes_read_)
557 this->read_attributes();
558 return this->specification_;
561 // Return the value of the DW_AT_abstract_origin attribute.
565 if (!this->attributes_read_)
566 this->read_attributes();
567 return this->abstract_origin_;
570 // Return the value of attribute ATTR as a string.
572 string_attribute(unsigned int attr);
574 // Return the value of attribute ATTR as an integer.
576 int_attribute(unsigned int attr);
578 // Return the value of attribute ATTR as an unsigned integer.
580 uint_attribute(unsigned int attr);
582 // Return the value of attribute ATTR as a reference.
584 ref_attribute(unsigned int attr, unsigned int* shndx);
586 // Return the value of attribute ATTR as a address.
588 address_attribute(unsigned int attr, unsigned int* shndx);
590 // Return the value of attribute ATTR as a flag.
592 flag_attribute(unsigned int attr)
593 { return this->int_attribute(attr) != 0; }
595 // Return true if this DIE is a declaration.
598 { return this->flag_attribute(elfcpp::DW_AT_declaration); }
600 // Return the parent of this DIE.
603 { return this->parent_; }
605 // Return the offset of this DIE.
608 { return this->die_offset_; }
610 // Return the offset of this DIE's first child.
614 // Set the offset of this DIE's next sibling.
616 set_sibling_offset(off_t sibling_offset)
617 { this->sibling_offset_ = sibling_offset; }
619 // Return the offset of this DIE's next sibling.
624 typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
626 // Read all the attributes of the DIE.
630 // Set the name of the DIE if present.
634 // Set the linkage name if present.
638 // Skip all the attributes of the DIE and return the offset
643 // The Dwarf_info_reader, for reading attributes.
644 Dwarf_info_reader* dwinfo_;
645 // The parent of this DIE.
647 // Offset of this DIE within its compilation unit.
649 // Offset of the first attribute, relative to the beginning of the DIE.
651 // Offset of the first child, relative to the compilation unit.
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).
665 // Offset of the name in the string table (for DW_FORM_strp).
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_;
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.
687 class Dwarf_info_reader
690 Dwarf_info_reader(bool is_type_unit,
692 const unsigned char* symtab,
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)
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_;
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.
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); }
727 // Return a pointer to the DWARF info buffer at OFFSET.
729 buffer_at_offset(off_t offset) const
731 const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
732 if (this->check_buffer(p + 1))
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);
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);
747 // Look for a relocation at offset ATTR_OFF in the dwarf info,
748 // and return the section index and offset of the target.
750 lookup_reloc(off_t attr_off, off_t* target_off);
752 // Return a string from the DWARF string table.
754 get_string(off_t str_off, unsigned int string_shndx);
756 // Return the size of a DWARF offset.
759 { return this->offset_size_; }
761 // Return the size of an address.
764 { return this->address_size_; }
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.
770 set_abbrev_shndx(unsigned int abbrev_shndx)
771 { this->abbrev_shndx_ = abbrev_shndx; }
773 // Return a pointer to the object file's ELF symbol table.
776 { return this->symtab_; }
778 // Return the size of the object file's ELF symbol table.
781 { return this->symtab_size_; }
783 // Return the offset of the current compilation unit.
786 { return this->cu_offset_; }
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>
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.
800 // Visit a compilation unit.
802 visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
804 // Visit a type unit.
806 visit_type_unit(off_t tu_offset, off_t type_offset, uint64_t signature,
807 Dwarf_die* root_die);
809 // Read the range table.
811 read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
813 return this->ranges_table_.read_range_list(this->object_,
821 // Return the object.
824 { return this->object_; }
826 // Checkpoint the relocation tracker.
828 get_reloc_checkpoint() const
829 { return this->reloc_mapper_->checkpoint(); }
831 // Reset the relocation tracker to the CHECKPOINT.
833 reset_relocs(uint64_t checkpoint)
834 { this->reloc_mapper_->reset(checkpoint); }
837 // Print a warning about a corrupt debug section.
839 warn_corrupt_debug_section() const;
841 // Check that P is within the bounds of the current section.
843 check_buffer(const unsigned char* p) const
845 if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
847 this->warn_corrupt_debug_section();
853 // Read the DWARF string table.
855 read_string_table(unsigned int string_shndx)
857 // If we've already read this string table, return immediately.
858 if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
860 if (string_shndx == 0 && this->string_shndx_ > 0)
862 return this->do_read_string_table(string_shndx);
866 do_read_string_table(unsigned int string_shndx);
868 // True if this is a type unit; false for a compilation unit.
870 // The object containing the .debug_info or .debug_types input section.
872 // The ELF symbol table.
873 const unsigned char* symtab_;
874 // The size of the ELF symbol table.
876 // Index of the .debug_info or .debug_types section.
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.
891 // Length of the current compilation unit.
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_;
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
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.
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
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;
944 // This class is used to read the line information from the debugging
945 // section of an object file.
947 class Dwarf_line_info
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.
964 addr2line(unsigned int shndx, off_t offset,
965 std::vector<std::string>* other_lines)
966 { return this->do_addr2line(shndx, offset, other_lines); }
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.
975 one_addr2line(Object* object, unsigned int shndx, off_t offset,
976 size_t cache_size, std::vector<std::string>* other_lines);
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.
981 clear_addr2line_cache();
985 do_addr2line(unsigned int shndx, off_t offset,
986 std::vector<std::string>* other_lines) = 0;
989 template<int size, bool big_endian>
990 class Sized_dwarf_line_info : public Dwarf_line_info
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);
999 ~Sized_dwarf_line_info()
1001 if (this->buffer_start_ != NULL)
1002 delete[] this->buffer_start_;
1007 do_addr2line(unsigned int shndx, off_t offset,
1008 std::vector<std::string>* other_lines);
1010 // Formats a file and line number to a string like "dirname/filename:lineno".
1012 format_file_lineno(const Offset_to_lineno_entry& lineno) const;
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.
1018 read_line_mappings(unsigned int shndx);
1020 // Reads the relocation section associated with .debug_line and
1021 // stores relocation information in reloc_map_.
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);
1030 const unsigned char*
1031 read_header_tables(const unsigned char* lineptr);
1033 // Reads the DWARF2/3 line information. If shndx is non-negative,
1034 // discard all line information that doesn't pertain to the given
1036 const unsigned char*
1037 read_lines(const unsigned char* lineptr, unsigned int shndx);
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
1044 process_one_opcode(const unsigned char* start,
1045 struct LineStateMachine* lsm, size_t* len);
1047 // Some parts of processing differ depending on whether the input
1048 // was a .o file or not.
1049 bool input_is_relobj();
1051 // If we saw anything amiss while parsing, we set this to false.
1052 // Then addr2line will always fail (rather than return possibly-
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
1060 struct Dwarf_line_infoHeader
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;
1069 unsigned char opcode_base;
1070 std::vector<unsigned char> std_opcode_lengths;
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
1081 const unsigned char* buffer_start_;
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_;
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_;
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_;
1099 // An index into the current directories_ and files_ vectors.
1100 int current_header_index_;
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> >
1106 Reloc_map reloc_map_;
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> >
1112 Lineno_map line_number_map_;
1115 } // End namespace gold.
1117 #endif // !defined(GOLD_DWARF_READER_H)