+ virtual
+ ~Elf_reloc_mapper()
+ { }
+
+ // Initialize the relocation tracker for section RELOC_SHNDX.
+ bool
+ initialize(unsigned int reloc_shndx, unsigned int reloc_type)
+ { return this->do_initialize(reloc_shndx, reloc_type); }
+
+ // Return the next reloc_offset.
+ off_t
+ next_offset()
+ { return this->do_next_offset(); }
+
+ // Advance to the next relocation past OFFSET.
+ void
+ advance(off_t offset)
+ { this->do_advance(offset); }
+
+ // Return the section index and offset within the section of the target
+ // of the relocation for RELOC_OFFSET in the referring section.
+ unsigned int
+ get_reloc_target(off_t reloc_offset, off_t* target_offset)
+ { return this->do_get_reloc_target(reloc_offset, target_offset); }
+
+ // Checkpoint the current position in the reloc section.
+ uint64_t
+ checkpoint() const
+ { return this->do_checkpoint(); }
+
+ // Reset the current position to the CHECKPOINT.
+ void
+ reset(uint64_t checkpoint)
+ { this->do_reset(checkpoint); }
+
+ protected:
+ virtual bool
+ do_initialize(unsigned int, unsigned int) = 0;
+
+ // Return the next reloc_offset.
+ virtual off_t
+ do_next_offset() = 0;
+
+ // Advance to the next relocation past OFFSET.
+ virtual void
+ do_advance(off_t offset) = 0;
+
+ virtual unsigned int
+ do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
+
+ // Checkpoint the current position in the reloc section.
+ virtual uint64_t
+ do_checkpoint() const = 0;
+
+ // Reset the current position to the CHECKPOINT.
+ virtual void
+ do_reset(uint64_t checkpoint) = 0;
+};
+
+template<int size, bool big_endian>
+class Sized_elf_reloc_mapper : public Elf_reloc_mapper
+{
+ public:
+ Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
+ off_t symtab_size)
+ : object_(object), symtab_(symtab), symtab_size_(symtab_size),
+ reloc_type_(0), track_relocs_()
+ { }
+
+ protected:
+ bool
+ do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
+
+ // Return the next reloc_offset.
+ virtual off_t
+ do_next_offset()
+ { return this->track_relocs_.next_offset(); }
+
+ // Advance to the next relocation past OFFSET.
+ virtual void
+ do_advance(off_t offset)
+ { this->track_relocs_.advance(offset); }
+
+ unsigned int
+ do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
+
+ // Checkpoint the current position in the reloc section.
+ uint64_t
+ do_checkpoint() const
+ { return this->track_relocs_.checkpoint(); }
+
+ // Reset the current position to the CHECKPOINT.
+ void
+ do_reset(uint64_t checkpoint)
+ { this->track_relocs_.reset(checkpoint); }
+
+ private:
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+
+ // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
+ // Set *IS_ORDINARY true if the section index is an ordinary section index.
+ unsigned int
+ symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
+
+ // The object file.
+ Object* object_;
+ // The ELF symbol table.
+ const unsigned char* symtab_;
+ // The size of the ELF symbol table.
+ off_t symtab_size_;
+ // Type of the relocation section (SHT_REL or SHT_RELA).
+ unsigned int reloc_type_;
+ // Relocations for the referring section.
+ Track_relocs<size, big_endian> track_relocs_;
+};
+
+// This class is used to read the abbreviations table from the
+// .debug_abbrev section of the object file.
+
+class Dwarf_abbrev_table
+{
+ public:
+ // An attribute list entry.
+ struct Attribute
+ {
+ Attribute(unsigned int a, unsigned int f)
+ : attr(a), form(f)
+ { }
+ unsigned int attr;
+ unsigned int form;
+ };
+
+ // An abbrev code entry.
+ struct Abbrev_code
+ {
+ Abbrev_code(unsigned int t, bool hc)
+ : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
+ {
+ this->attributes.reserve(10);
+ }
+
+ void
+ add_attribute(unsigned int attr, unsigned int form)
+ {
+ this->attributes.push_back(Attribute(attr, form));
+ }
+
+ // The DWARF tag.
+ unsigned int tag;
+ // True if the DIE has children.
+ bool has_children : 1;
+ // True if the DIE has a sibling attribute.
+ bool has_sibling_attribute : 1;
+ // The list of attributes and forms.
+ std::vector<Attribute> attributes;
+ };
+
+ Dwarf_abbrev_table()
+ : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
+ owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
+ {
+ memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
+ }
+
+ ~Dwarf_abbrev_table()
+ {
+ if (this->owns_buffer_ && this->buffer_ != NULL)
+ delete[] this->buffer_;
+ this->clear_abbrev_codes();
+ }
+
+ // Read the abbrev table from an object file.
+ bool
+ read_abbrevs(Relobj* object,
+ unsigned int abbrev_shndx,
+ off_t abbrev_offset)
+ {
+ // If we've already read this abbrev table, return immediately.
+ if (this->abbrev_shndx_ > 0
+ && this->abbrev_shndx_ == abbrev_shndx
+ && this->abbrev_offset_ == abbrev_offset)
+ return true;
+ return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
+ }
+
+ // Return the abbrev code entry for CODE. This is a fast path for
+ // abbrev codes that are in the direct lookup table. If not found
+ // there, we call do_get_abbrev() to do the hard work.
+ const Abbrev_code*
+ get_abbrev(unsigned int code)
+ {
+ if (code < this->low_abbrev_code_max_
+ && this->low_abbrev_codes_[code] != NULL)
+ return this->low_abbrev_codes_[code];
+ return this->do_get_abbrev(code);
+ }
+
+ private:
+ // Read the abbrev table from an object file.
+ bool
+ do_read_abbrevs(Relobj* object,
+ unsigned int abbrev_shndx,
+ off_t abbrev_offset);
+
+ // Lookup the abbrev code entry for CODE.
+ const Abbrev_code*
+ do_get_abbrev(unsigned int code);
+
+ // Store an abbrev code entry for CODE.
+ void
+ store_abbrev(unsigned int code, const Abbrev_code* entry)
+ {
+ if (code < this->low_abbrev_code_max_)
+ this->low_abbrev_codes_[code] = entry;
+ else
+ this->high_abbrev_codes_[code] = entry;
+ }
+
+ // Clear the abbrev code table and release the memory it uses.
+ void
+ clear_abbrev_codes();
+
+ typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
+
+ // The section index of the current abbrev table.
+ unsigned int abbrev_shndx_;
+ // The offset within the section of the current abbrev table.
+ off_t abbrev_offset_;
+ // The buffer containing the .debug_abbrev section.
+ const unsigned char* buffer_;
+ const unsigned char* buffer_end_;
+ // True if this object owns the buffer and needs to delete it.
+ bool owns_buffer_;
+ // Pointer to the current position in the buffer.
+ const unsigned char* buffer_pos_;
+ // The table of abbrev codes.
+ // We use a direct-lookup array for low abbrev codes,
+ // and store the rest in a hash table.
+ static const unsigned int low_abbrev_code_max_ = 256;
+ const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_];
+ Abbrev_code_table high_abbrev_codes_;
+};
+
+// A DWARF range list. The start and end offsets are relative
+// to the input section SHNDX. Each range must lie entirely
+// within a single section.
+
+class Dwarf_range_list
+{
+ public:
+ struct Range
+ {
+ Range(unsigned int a_shndx, off_t a_start, off_t a_end)
+ : shndx(a_shndx), start(a_start), end(a_end)
+ { }
+
+ unsigned int shndx;
+ off_t start;
+ off_t end;
+ };
+
+ Dwarf_range_list()
+ : range_list_()
+ { }
+
+ void
+ add(unsigned int shndx, off_t start, off_t end)
+ { this->range_list_.push_back(Range(shndx, start, end)); }
+
+ size_t
+ size() const
+ { return this->range_list_.size(); }
+
+ const Range&
+ operator[](off_t i) const
+ { return this->range_list_[i]; }
+
+ private:
+ std::vector<Range> range_list_;
+};
+
+// This class is used to read the ranges table from the
+// .debug_ranges section of the object file.
+
+class Dwarf_ranges_table
+{
+ public:
+ Dwarf_ranges_table(Dwarf_info_reader* dwinfo)
+ : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL),
+ ranges_buffer_end_(NULL), owns_ranges_buffer_(false),
+ ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0)
+ { }
+
+ ~Dwarf_ranges_table()
+ {
+ if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
+ delete[] this->ranges_buffer_;
+ if (this->ranges_reloc_mapper_ != NULL)
+ delete this->ranges_reloc_mapper_;
+ }
+
+ // Read the ranges table from an object file.
+ bool
+ read_ranges_table(Relobj* object,
+ const unsigned char* symtab,
+ off_t symtab_size,
+ unsigned int ranges_shndx);
+
+ // Read the range table from an object file.
+ Dwarf_range_list*
+ read_range_list(Relobj* object,
+ const unsigned char* symtab,
+ off_t symtab_size,
+ unsigned int address_size,
+ unsigned int ranges_shndx,
+ off_t ranges_offset);
+
+ // Look for a relocation at offset OFF in the range table,
+ // and return the section index and offset of the target.
+ unsigned int
+ lookup_reloc(off_t off, off_t* target_off);
+
+ private:
+ // The Dwarf_info_reader, for reading data.
+ Dwarf_info_reader* dwinfo_;
+ // The section index of the ranges table.
+ unsigned int ranges_shndx_;
+ // The buffer containing the .debug_ranges section.
+ const unsigned char* ranges_buffer_;
+ const unsigned char* ranges_buffer_end_;
+ // True if this object owns the buffer and needs to delete it.
+ bool owns_ranges_buffer_;
+ // Relocation mapper for the .debug_ranges section.
+ Elf_reloc_mapper* ranges_reloc_mapper_;
+ // Type of the relocation section (SHT_REL or SHT_RELA).
+ unsigned int reloc_type_;
+ // For incremental update links, this will hold the offset of the
+ // input section within the output section. Offsets read from
+ // relocated data will be relative to the output section, and need
+ // to be corrected before reading data from the input section.
+ uint64_t output_section_offset_;
+};
+
+// This class is used to read the pubnames and pubtypes tables from the
+// .debug_pubnames and .debug_pubtypes sections of the object file.
+
+class Dwarf_pubnames_table
+{
+ public:
+ Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes)
+ : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false),
+ offset_size_(0), pinfo_(NULL), end_of_table_(NULL),
+ is_pubtypes_(is_pubtypes), is_gnu_style_(false),
+ unit_length_(0), cu_offset_(0)
+ { }
+
+ ~Dwarf_pubnames_table()
+ {
+ if (this->owns_buffer_ && this->buffer_ != NULL)
+ delete[] this->buffer_;
+ }
+
+ // Read the pubnames section from the object file, using the symbol
+ // table for relocating it.
+ bool
+ read_section(Relobj* object, const unsigned char* symbol_table,
+ off_t symtab_size);
+
+ // Read the header for the set at OFFSET.
+ bool
+ read_header(off_t offset);
+
+ // Return the offset to the cu within the info or types section.
+ off_t
+ cu_offset()
+ { return this->cu_offset_; }
+
+ // Return the size of this subsection of the table. The unit length
+ // doesn't include the size of its own field.
+ off_t
+ subsection_size()
+ { return this->unit_length_; }
+
+ // Read the next name from the set. If the pubname table is gnu-style,
+ // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
+ const char*
+ next_name(uint8_t* flag_byte);
+
+ private:
+ // The Dwarf_info_reader, for reading data.
+ Dwarf_info_reader* dwinfo_;
+ // The buffer containing the .debug_ranges section.
+ const unsigned char* buffer_;
+ const unsigned char* buffer_end_;
+ // True if this object owns the buffer and needs to delete it.
+ bool owns_buffer_;
+ // The size of a DWARF offset for the current set.
+ unsigned int offset_size_;
+ // The current position within the buffer.
+ const unsigned char* pinfo_;
+ // The end of the current pubnames table.
+ const unsigned char* end_of_table_;
+ // TRUE if this is a .debug_pubtypes section.
+ bool is_pubtypes_;
+ // Gnu-style pubnames table. This style has an extra flag byte between the
+ // offset and the name, and is used for generating version 7 of gdb-index.
+ bool is_gnu_style_;
+ // Fields read from the header.
+ uint64_t unit_length_;
+ off_t cu_offset_;
+
+ // Track relocations for this table so we can find the CUs that
+ // correspond to the subsections.
+ Elf_reloc_mapper* reloc_mapper_;
+ // Type of the relocation section (SHT_REL or SHT_RELA).
+ unsigned int reloc_type_;
+};
+
+// This class represents a DWARF Debug Info Entry (DIE).
+
+class Dwarf_die
+{
+ public:
+ // An attribute value.
+ struct Attribute_value
+ {
+ unsigned int attr;
+ unsigned int form;
+ union
+ {
+ int64_t intval;
+ uint64_t uintval;
+ const char* stringval;
+ const unsigned char* blockval;
+ off_t refval;
+ } val;
+ union
+ {
+ // Section index for reference forms.
+ unsigned int shndx;
+ // Block length for block forms.
+ unsigned int blocklen;
+ // Attribute offset for DW_FORM_strp.
+ unsigned int attr_off;
+ } aux;
+ };
+
+ // A list of attribute values.
+ typedef std::vector<Attribute_value> Attributes;
+
+ Dwarf_die(Dwarf_info_reader* dwinfo,
+ off_t die_offset,
+ Dwarf_die* parent);
+
+ // Return the DWARF tag for this DIE.
+ unsigned int
+ tag() const
+ {
+ if (this->abbrev_code_ == NULL)
+ return 0;
+ return this->abbrev_code_->tag;
+ }
+
+ // Return true if this DIE has children.
+ bool
+ has_children() const
+ {
+ gold_assert(this->abbrev_code_ != NULL);
+ return this->abbrev_code_->has_children;
+ }
+
+ // Return true if this DIE has a sibling attribute.
+ bool
+ has_sibling_attribute() const
+ {
+ gold_assert(this->abbrev_code_ != NULL);
+ return this->abbrev_code_->has_sibling_attribute;
+ }
+
+ // Return the value of attribute ATTR.
+ const Attribute_value*
+ attribute(unsigned int attr);
+
+ // Return the value of the DW_AT_name attribute.
+ const char*
+ name()
+ {
+ if (this->name_ == NULL)
+ this->set_name();
+ return this->name_;
+ }
+
+ // Return the value of the DW_AT_linkage_name
+ // or DW_AT_MIPS_linkage_name attribute.
+ const char*
+ linkage_name()
+ {
+ if (this->linkage_name_ == NULL)
+ this->set_linkage_name();
+ return this->linkage_name_;
+ }
+
+ // Return the value of the DW_AT_specification attribute.
+ off_t
+ specification()
+ {
+ if (!this->attributes_read_)
+ this->read_attributes();
+ return this->specification_;
+ }
+
+ // Return the value of the DW_AT_abstract_origin attribute.
+ off_t
+ abstract_origin()
+ {
+ if (!this->attributes_read_)
+ this->read_attributes();
+ return this->abstract_origin_;
+ }
+
+ // Return the value of attribute ATTR as a string.
+ const char*
+ string_attribute(unsigned int attr);
+
+ // Return the value of attribute ATTR as an integer.
+ int64_t
+ int_attribute(unsigned int attr);
+
+ // Return the value of attribute ATTR as an unsigned integer.
+ uint64_t
+ uint_attribute(unsigned int attr);
+
+ // Return the value of attribute ATTR as a reference.
+ off_t
+ ref_attribute(unsigned int attr, unsigned int* shndx);
+
+ // Return the value of attribute ATTR as a address.
+ off_t
+ address_attribute(unsigned int attr, unsigned int* shndx);
+
+ // Return the value of attribute ATTR as a flag.
+ bool
+ flag_attribute(unsigned int attr)
+ { return this->int_attribute(attr) != 0; }
+
+ // Return true if this DIE is a declaration.
+ bool
+ is_declaration()
+ { return this->flag_attribute(elfcpp::DW_AT_declaration); }
+
+ // Return the parent of this DIE.
+ Dwarf_die*
+ parent() const
+ { return this->parent_; }
+
+ // Return the offset of this DIE.
+ off_t
+ offset() const
+ { return this->die_offset_; }
+
+ // Return the offset of this DIE's first child.
+ off_t
+ child_offset();
+
+ // Set the offset of this DIE's next sibling.