+// Read the header for the set at OFFSET.
+
+bool
+Dwarf_pubnames_table::read_header(off_t offset)
+{
+ // Correct the offset. For incremental update links, we have a
+ // relocated offset that is relative to the output section, but
+ // here we need an offset relative to the input section.
+ offset -= this->output_section_offset_;
+
+ if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
+ return false;
+
+ const unsigned char* pinfo = this->buffer_ + offset;
+
+ // Read the unit_length field.
+ uint32_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
+ pinfo += 4;
+ if (unit_length == 0xffffffff)
+ {
+ unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
+ pinfo += 8;
+ this->offset_size_ = 8;
+ }
+ else
+ this->offset_size_ = 4;
+
+ // Check the version.
+ unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
+ pinfo += 2;
+ if (version != 2)
+ return false;
+
+ // Skip the debug_info_offset and debug_info_size fields.
+ pinfo += 2 * this->offset_size_;
+
+ if (pinfo >= this->buffer_end_)
+ return false;
+
+ this->pinfo_ = pinfo;
+ return true;
+}
+
+// Read the next name from the set.
+
+const char*
+Dwarf_pubnames_table::next_name()
+{
+ const unsigned char* pinfo = this->pinfo_;
+
+ // Read the offset within the CU. If this is zero, we have reached
+ // the end of the list.
+ uint32_t offset;
+ if (this->offset_size_ == 4)
+ offset = this->dwinfo_->read_from_pointer<32>(&pinfo);
+ else
+ offset = this->dwinfo_->read_from_pointer<64>(&pinfo);
+ if (offset == 0)
+ return NULL;
+
+ // Return a pointer to the string at the current location,
+ // and advance the pointer to the next entry.
+ const char* ret = reinterpret_cast<const char*>(pinfo);
+ while (pinfo < this->buffer_end_ && *pinfo != '\0')
+ ++pinfo;
+ if (pinfo < this->buffer_end_)
+ ++pinfo;
+
+ this->pinfo_ = pinfo;
+ return ret;
+}
+
+// class Dwarf_die
+
+Dwarf_die::Dwarf_die(
+ Dwarf_info_reader* dwinfo,
+ off_t die_offset,
+ Dwarf_die* parent)
+ : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
+ child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
+ attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
+ linkage_name_off_(-1), string_shndx_(0), specification_(0),
+ abstract_origin_(0)
+{
+ size_t len;
+ const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
+ if (pdie == NULL)
+ return;
+ unsigned int code = read_unsigned_LEB_128(pdie, &len);
+ if (code == 0)
+ {
+ if (parent != NULL)
+ parent->set_sibling_offset(die_offset + len);
+ return;
+ }
+ this->attr_offset_ = len;
+
+ // Lookup the abbrev code in the abbrev table.
+ this->abbrev_code_ = dwinfo->get_abbrev(code);
+}
+
+// Read all the attributes of the DIE.
+
+bool
+Dwarf_die::read_attributes()
+{
+ if (this->attributes_read_)
+ return true;
+
+ gold_assert(this->abbrev_code_ != NULL);
+
+ const unsigned char* pdie =
+ this->dwinfo_->buffer_at_offset(this->die_offset_);
+ if (pdie == NULL)
+ return false;
+ const unsigned char* pattr = pdie + this->attr_offset_;
+
+ unsigned int nattr = this->abbrev_code_->attributes.size();
+ this->attributes_.reserve(nattr);
+ for (unsigned int i = 0; i < nattr; ++i)
+ {
+ size_t len;
+ unsigned int attr = this->abbrev_code_->attributes[i].attr;
+ unsigned int form = this->abbrev_code_->attributes[i].form;
+ if (form == elfcpp::DW_FORM_indirect)
+ {
+ form = read_unsigned_LEB_128(pattr, &len);
+ pattr += len;
+ }
+ off_t attr_off = this->die_offset_ + (pattr - pdie);
+ bool ref_form = false;
+ Attribute_value attr_value;
+ attr_value.attr = attr;
+ attr_value.form = form;
+ attr_value.aux.shndx = 0;
+ switch(form)
+ {
+ case elfcpp::DW_FORM_flag_present:
+ attr_value.val.intval = 1;
+ break;
+ case elfcpp::DW_FORM_strp:
+ {
+ off_t str_off;
+ if (this->dwinfo_->offset_size() == 4)
+ str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
+ else
+ str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &str_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.refval = str_off;
+ break;
+ }
+ case elfcpp::DW_FORM_sec_offset:
+ {
+ off_t sec_off;
+ if (this->dwinfo_->offset_size() == 4)
+ sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
+ else
+ sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.refval = sec_off;
+ ref_form = true;
+ break;
+ }
+ case elfcpp::DW_FORM_addr:
+ case elfcpp::DW_FORM_ref_addr:
+ {
+ off_t sec_off;
+ if (this->dwinfo_->address_size() == 4)
+ sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
+ else
+ sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.refval = sec_off;
+ ref_form = true;
+ break;
+ }
+ case elfcpp::DW_FORM_block1:
+ attr_value.aux.blocklen = *pattr++;
+ attr_value.val.blockval = pattr;
+ pattr += attr_value.aux.blocklen;
+ break;
+ case elfcpp::DW_FORM_block2:
+ attr_value.aux.blocklen =
+ this->dwinfo_->read_from_pointer<16>(&pattr);
+ attr_value.val.blockval = pattr;
+ pattr += attr_value.aux.blocklen;
+ break;
+ case elfcpp::DW_FORM_block4:
+ attr_value.aux.blocklen =
+ this->dwinfo_->read_from_pointer<32>(&pattr);
+ attr_value.val.blockval = pattr;
+ pattr += attr_value.aux.blocklen;
+ break;
+ case elfcpp::DW_FORM_block:
+ case elfcpp::DW_FORM_exprloc:
+ attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
+ attr_value.val.blockval = pattr + len;
+ pattr += len + attr_value.aux.blocklen;
+ break;
+ case elfcpp::DW_FORM_data1:
+ case elfcpp::DW_FORM_flag:
+ attr_value.val.intval = *pattr++;
+ break;
+ case elfcpp::DW_FORM_ref1:
+ attr_value.val.refval = *pattr++;
+ ref_form = true;
+ break;
+ case elfcpp::DW_FORM_data2:
+ attr_value.val.intval =
+ this->dwinfo_->read_from_pointer<16>(&pattr);
+ break;
+ case elfcpp::DW_FORM_ref2:
+ attr_value.val.refval =
+ this->dwinfo_->read_from_pointer<16>(&pattr);
+ ref_form = true;
+ break;
+ case elfcpp::DW_FORM_data4:
+ {
+ off_t sec_off;
+ sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.intval = sec_off;
+ break;
+ }
+ case elfcpp::DW_FORM_ref4:
+ {
+ off_t sec_off;
+ sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.refval = sec_off;
+ ref_form = true;
+ break;
+ }
+ case elfcpp::DW_FORM_data8:
+ {
+ off_t sec_off;
+ sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.intval = sec_off;
+ break;
+ }
+ case elfcpp::DW_FORM_ref_sig8:
+ attr_value.val.uintval =
+ this->dwinfo_->read_from_pointer<64>(&pattr);
+ break;
+ case elfcpp::DW_FORM_ref8:
+ {
+ off_t sec_off;
+ sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
+ unsigned int shndx =
+ this->dwinfo_->lookup_reloc(attr_off, &sec_off);
+ attr_value.aux.shndx = shndx;
+ attr_value.val.refval = sec_off;
+ ref_form = true;
+ break;
+ }
+ case elfcpp::DW_FORM_ref_udata:
+ attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
+ ref_form = true;
+ pattr += len;
+ break;
+ case elfcpp::DW_FORM_udata:
+ case elfcpp::DW_FORM_GNU_addr_index:
+ case elfcpp::DW_FORM_GNU_str_index:
+ attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
+ pattr += len;
+ break;
+ case elfcpp::DW_FORM_sdata:
+ attr_value.val.intval = read_signed_LEB_128(pattr, &len);
+ pattr += len;
+ break;
+ case elfcpp::DW_FORM_string:
+ attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
+ len = strlen(attr_value.val.stringval);
+ pattr += len + 1;
+ break;
+ default:
+ return false;
+ }
+
+ // Cache the most frequently-requested attributes.
+ switch (attr)
+ {
+ case elfcpp::DW_AT_name:
+ if (form == elfcpp::DW_FORM_string)
+ this->name_ = attr_value.val.stringval;
+ else if (form == elfcpp::DW_FORM_strp)
+ {
+ // All indirect strings should refer to the same
+ // string section, so we just save the last one seen.
+ this->string_shndx_ = attr_value.aux.shndx;
+ this->name_off_ = attr_value.val.refval;
+ }
+ break;
+ case elfcpp::DW_AT_linkage_name:
+ case elfcpp::DW_AT_MIPS_linkage_name:
+ if (form == elfcpp::DW_FORM_string)
+ this->linkage_name_ = attr_value.val.stringval;
+ else if (form == elfcpp::DW_FORM_strp)
+ {
+ // All indirect strings should refer to the same
+ // string section, so we just save the last one seen.
+ this->string_shndx_ = attr_value.aux.shndx;
+ this->linkage_name_off_ = attr_value.val.refval;
+ }
+ break;
+ case elfcpp::DW_AT_specification:
+ if (ref_form)
+ this->specification_ = attr_value.val.refval;
+ break;
+ case elfcpp::DW_AT_abstract_origin:
+ if (ref_form)
+ this->abstract_origin_ = attr_value.val.refval;
+ break;
+ case elfcpp::DW_AT_sibling:
+ if (ref_form && attr_value.aux.shndx == 0)
+ this->sibling_offset_ = attr_value.val.refval;
+ default:
+ break;
+ }
+
+ this->attributes_.push_back(attr_value);
+ }
+
+ // Now that we know where the next DIE begins, record the offset
+ // to avoid later recalculation.
+ if (this->has_children())
+ this->child_offset_ = this->die_offset_ + (pattr - pdie);
+ else
+ this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
+
+ this->attributes_read_ = true;
+ return true;
+}
+
+// Skip all the attributes of the DIE and return the offset of the next DIE.
+
+off_t
+Dwarf_die::skip_attributes()
+{
+ gold_assert(this->abbrev_code_ != NULL);
+
+ const unsigned char* pdie =
+ this->dwinfo_->buffer_at_offset(this->die_offset_);
+ if (pdie == NULL)
+ return 0;
+ const unsigned char* pattr = pdie + this->attr_offset_;
+
+ for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
+ {
+ size_t len;
+ unsigned int form = this->abbrev_code_->attributes[i].form;
+ if (form == elfcpp::DW_FORM_indirect)
+ {
+ form = read_unsigned_LEB_128(pattr, &len);
+ pattr += len;
+ }
+ switch(form)
+ {
+ case elfcpp::DW_FORM_flag_present:
+ break;
+ case elfcpp::DW_FORM_strp:
+ case elfcpp::DW_FORM_sec_offset:
+ pattr += this->dwinfo_->offset_size();
+ break;
+ case elfcpp::DW_FORM_addr:
+ case elfcpp::DW_FORM_ref_addr:
+ pattr += this->dwinfo_->address_size();
+ break;
+ case elfcpp::DW_FORM_block1:
+ pattr += 1 + *pattr;
+ break;
+ case elfcpp::DW_FORM_block2:
+ {
+ uint16_t block_size;
+ block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
+ pattr += block_size;
+ break;
+ }
+ case elfcpp::DW_FORM_block4:
+ {
+ uint32_t block_size;
+ block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
+ pattr += block_size;
+ break;
+ }
+ case elfcpp::DW_FORM_block:
+ case elfcpp::DW_FORM_exprloc:
+ {
+ uint64_t block_size;
+ block_size = read_unsigned_LEB_128(pattr, &len);
+ pattr += len + block_size;
+ break;
+ }
+ case elfcpp::DW_FORM_data1:
+ case elfcpp::DW_FORM_ref1:
+ case elfcpp::DW_FORM_flag:
+ pattr += 1;
+ break;
+ case elfcpp::DW_FORM_data2:
+ case elfcpp::DW_FORM_ref2:
+ pattr += 2;
+ break;
+ case elfcpp::DW_FORM_data4:
+ case elfcpp::DW_FORM_ref4:
+ pattr += 4;
+ break;
+ case elfcpp::DW_FORM_data8:
+ case elfcpp::DW_FORM_ref8:
+ case elfcpp::DW_FORM_ref_sig8:
+ pattr += 8;
+ break;
+ case elfcpp::DW_FORM_ref_udata:
+ case elfcpp::DW_FORM_udata:
+ case elfcpp::DW_FORM_GNU_addr_index:
+ case elfcpp::DW_FORM_GNU_str_index:
+ read_unsigned_LEB_128(pattr, &len);
+ pattr += len;
+ break;
+ case elfcpp::DW_FORM_sdata:
+ read_signed_LEB_128(pattr, &len);
+ pattr += len;
+ break;
+ case elfcpp::DW_FORM_string:
+ len = strlen(reinterpret_cast<const char*>(pattr));
+ pattr += len + 1;
+ break;
+ default:
+ return 0;
+ }
+ }
+
+ return this->die_offset_ + (pattr - pdie);
+}
+
+// Get the name of the DIE and cache it.
+
+void
+Dwarf_die::set_name()
+{
+ if (this->name_ != NULL || !this->read_attributes())
+ return;
+ if (this->name_off_ != -1)
+ this->name_ = this->dwinfo_->get_string(this->name_off_,
+ this->string_shndx_);
+}
+
+// Get the linkage name of the DIE and cache it.
+
+void
+Dwarf_die::set_linkage_name()
+{
+ if (this->linkage_name_ != NULL || !this->read_attributes())
+ return;
+ if (this->linkage_name_off_ != -1)
+ this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
+ this->string_shndx_);
+}
+
+// Return the value of attribute ATTR.
+
+const Dwarf_die::Attribute_value*
+Dwarf_die::attribute(unsigned int attr)
+{
+ if (!this->read_attributes())
+ return NULL;
+ for (unsigned int i = 0; i < this->attributes_.size(); ++i)
+ {
+ if (this->attributes_[i].attr == attr)
+ return &this->attributes_[i];
+ }
+ return NULL;
+}
+
+const char*
+Dwarf_die::string_attribute(unsigned int attr)
+{
+ const Attribute_value* attr_val = this->attribute(attr);
+ if (attr_val == NULL)
+ return NULL;
+ switch (attr_val->form)
+ {
+ case elfcpp::DW_FORM_string:
+ return attr_val->val.stringval;
+ case elfcpp::DW_FORM_strp:
+ return this->dwinfo_->get_string(attr_val->val.refval,
+ attr_val->aux.shndx);
+ default:
+ return NULL;
+ }
+}