1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "elfcpp_swap.h"
31 #include "parameters.h"
33 #include "dwarf_reader.h"
34 #include "int_encoding.h"
35 #include "compressed_output.h"
39 // Class Sized_elf_reloc_mapper
41 // Initialize the relocation tracker for section RELOC_SHNDX.
43 template<int size, bool big_endian>
45 Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
46 unsigned int reloc_shndx, unsigned int reloc_type)
48 this->reloc_type_ = reloc_type;
49 return this->track_relocs_.initialize(this->object_, reloc_shndx,
53 // Looks in the symtab to see what section a symbol is in.
55 template<int size, bool big_endian>
57 Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
58 unsigned int symndx, Address* value, bool* is_ordinary)
60 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
61 gold_assert((symndx + 1) * symsize <= this->symtab_size_);
62 elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
63 *value = elfsym.get_st_value();
64 return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
68 // Return the section index and offset within the section of
69 // the target of the relocation for RELOC_OFFSET.
71 template<int size, bool big_endian>
73 Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
74 off_t reloc_offset, off_t* target_offset)
76 this->track_relocs_.advance(reloc_offset);
77 if (reloc_offset != this->track_relocs_.next_offset())
79 unsigned int symndx = this->track_relocs_.next_symndx();
80 typename elfcpp::Elf_types<size>::Elf_Addr value;
82 unsigned int target_shndx = this->symbol_section(symndx, &value,
86 if (this->reloc_type_ == elfcpp::SHT_RELA)
87 value += this->track_relocs_.next_addend();
88 *target_offset = value;
92 static inline Elf_reloc_mapper*
93 make_elf_reloc_mapper(Object* object, const unsigned char* symtab,
96 switch (parameters->size_and_endianness())
98 #ifdef HAVE_TARGET_32_LITTLE
99 case Parameters::TARGET_32_LITTLE:
100 return new Sized_elf_reloc_mapper<32, false>(object, symtab,
103 #ifdef HAVE_TARGET_32_BIG
104 case Parameters::TARGET_32_BIG:
105 return new Sized_elf_reloc_mapper<32, true>(object, symtab,
108 #ifdef HAVE_TARGET_64_LITTLE
109 case Parameters::TARGET_64_LITTLE:
110 return new Sized_elf_reloc_mapper<64, false>(object, symtab,
113 #ifdef HAVE_TARGET_64_BIG
114 case Parameters::TARGET_64_BIG:
115 return new Sized_elf_reloc_mapper<64, true>(object, symtab,
123 // class Dwarf_abbrev_table
126 Dwarf_abbrev_table::clear_abbrev_codes()
128 for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
130 if (this->low_abbrev_codes_[code] != NULL)
132 delete this->low_abbrev_codes_[code];
133 this->low_abbrev_codes_[code] = NULL;
136 for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
137 it != this->high_abbrev_codes_.end();
140 if (it->second != NULL)
143 this->high_abbrev_codes_.clear();
146 // Read the abbrev table from an object file.
149 Dwarf_abbrev_table::do_read_abbrevs(
151 unsigned int abbrev_shndx,
154 this->clear_abbrev_codes();
156 // If we don't have relocations, abbrev_shndx will be 0, and
157 // we'll have to hunt for the .debug_abbrev section.
158 if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
159 abbrev_shndx = this->abbrev_shndx_;
160 else if (abbrev_shndx == 0)
162 for (unsigned int i = 1; i < object->shnum(); ++i)
164 std::string name = object->section_name(i);
165 if (name == ".debug_abbrev")
168 // Correct the offset. For incremental update links, we have a
169 // relocated offset that is relative to the output section, but
170 // here we need an offset relative to the input section.
171 abbrev_offset -= object->output_section_offset(i);
175 if (abbrev_shndx == 0)
179 // Get the section contents and decompress if necessary.
180 if (abbrev_shndx != this->abbrev_shndx_)
182 if (this->owns_buffer_ && this->buffer_ != NULL)
184 delete[] this->buffer_;
185 this->owns_buffer_ = false;
188 section_size_type buffer_size;
190 object->decompressed_section_contents(abbrev_shndx,
192 &this->owns_buffer_);
193 this->buffer_end_ = this->buffer_ + buffer_size;
194 this->abbrev_shndx_ = abbrev_shndx;
197 this->buffer_pos_ = this->buffer_ + abbrev_offset;
201 // Lookup the abbrev code entry for CODE. This function is called
202 // only when the abbrev code is not in the direct lookup table.
203 // It may be in the hash table, it may not have been read yet,
204 // or it may not exist in the abbrev table.
206 const Dwarf_abbrev_table::Abbrev_code*
207 Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
209 // See if the abbrev code is already in the hash table.
210 Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
211 if (it != this->high_abbrev_codes_.end())
214 // Read and store abbrev code definitions until we find the
215 // one we're looking for.
218 // Read the abbrev code. A zero here indicates the end of the
221 if (this->buffer_pos_ >= this->buffer_end_)
223 uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
226 this->buffer_pos_ = this->buffer_end_;
229 this->buffer_pos_ += len;
232 if (this->buffer_pos_ >= this->buffer_end_)
234 uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
235 this->buffer_pos_ += len;
237 // Read the has_children flag.
238 if (this->buffer_pos_ >= this->buffer_end_)
240 bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
241 this->buffer_pos_ += 1;
243 // Read the list of (attribute, form) pairs.
244 Abbrev_code* entry = new Abbrev_code(tag, has_children);
247 // Read the attribute.
248 if (this->buffer_pos_ >= this->buffer_end_)
250 uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
251 this->buffer_pos_ += len;
254 if (this->buffer_pos_ >= this->buffer_end_)
256 uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
257 this->buffer_pos_ += len;
259 // A (0,0) pair terminates the list.
260 if (attr == 0 && form == 0)
263 if (attr == elfcpp::DW_AT_sibling)
264 entry->has_sibling_attribute = true;
266 entry->add_attribute(attr, form);
269 this->store_abbrev(nextcode, entry);
270 if (nextcode == code)
277 // class Dwarf_ranges_table
279 // Read the ranges table from an object file.
282 Dwarf_ranges_table::read_ranges_table(
284 const unsigned char* symtab,
286 unsigned int ranges_shndx)
288 // If we've already read this abbrev table, return immediately.
289 if (this->ranges_shndx_ > 0
290 && this->ranges_shndx_ == ranges_shndx)
293 // If we don't have relocations, ranges_shndx will be 0, and
294 // we'll have to hunt for the .debug_ranges section.
295 if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
296 ranges_shndx = this->ranges_shndx_;
297 else if (ranges_shndx == 0)
299 for (unsigned int i = 1; i < object->shnum(); ++i)
301 std::string name = object->section_name(i);
302 if (name == ".debug_ranges")
305 this->output_section_offset_ = object->output_section_offset(i);
309 if (ranges_shndx == 0)
313 // Get the section contents and decompress if necessary.
314 if (ranges_shndx != this->ranges_shndx_)
316 if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
318 delete[] this->ranges_buffer_;
319 this->owns_ranges_buffer_ = false;
322 section_size_type buffer_size;
323 this->ranges_buffer_ =
324 object->decompressed_section_contents(ranges_shndx,
326 &this->owns_ranges_buffer_);
327 this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
328 this->ranges_shndx_ = ranges_shndx;
331 if (this->ranges_reloc_mapper_ != NULL)
333 delete this->ranges_reloc_mapper_;
334 this->ranges_reloc_mapper_ = NULL;
337 // For incremental objects, we have no relocations.
338 if (object->is_incremental())
341 // Find the relocation section for ".debug_ranges".
342 unsigned int reloc_shndx = 0;
343 unsigned int reloc_type = 0;
344 for (unsigned int i = 0; i < object->shnum(); ++i)
346 reloc_type = object->section_type(i);
347 if ((reloc_type == elfcpp::SHT_REL
348 || reloc_type == elfcpp::SHT_RELA)
349 && object->section_info(i) == ranges_shndx)
356 this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
358 this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
363 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
366 Dwarf_ranges_table::read_range_list(
368 const unsigned char* symtab,
370 unsigned int addr_size,
371 unsigned int ranges_shndx,
374 Dwarf_range_list* ranges;
376 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
379 // Correct the offset. For incremental update links, we have a
380 // relocated offset that is relative to the output section, but
381 // here we need an offset relative to the input section.
382 offset -= this->output_section_offset_;
384 // Read the range list at OFFSET.
385 ranges = new Dwarf_range_list();
388 this->ranges_buffer_ + offset < this->ranges_buffer_end_;
389 offset += 2 * addr_size)
394 // Read the raw contents of the section.
397 start = read_from_pointer<32>(this->ranges_buffer_ + offset);
398 end = read_from_pointer<32>(this->ranges_buffer_ + offset + 4);
402 start = read_from_pointer<64>(this->ranges_buffer_ + offset);
403 end = read_from_pointer<64>(this->ranges_buffer_ + offset + 8);
406 // Check for relocations and adjust the values.
407 unsigned int shndx1 = 0;
408 unsigned int shndx2 = 0;
409 if (this->ranges_reloc_mapper_ != NULL)
412 this->ranges_reloc_mapper_->get_reloc_target(offset, &start);
414 this->ranges_reloc_mapper_->get_reloc_target(offset + addr_size,
418 // End of list is marked by a pair of zeroes.
419 if (shndx1 == 0 && start == 0 && end == 0)
422 // A "base address selection entry" is identified by
423 // 0xffffffff for the first value of the pair. The second
424 // value is used as a base for subsequent range list entries.
425 if (shndx1 == 0 && start == -1)
427 else if (shndx1 == shndx2)
429 if (shndx1 == 0 || object->is_section_included(shndx1))
430 ranges->add(shndx1, base + start, base + end);
433 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
434 "range list entry are in different sections"),
435 object->name().c_str());
441 // class Dwarf_pubnames_table
443 // Read the pubnames section SHNDX from the object file.
446 Dwarf_pubnames_table::read_section(Relobj* object, unsigned int shndx)
448 section_size_type buffer_size;
450 // If we don't have relocations, shndx will be 0, and
451 // we'll have to hunt for the .debug_pubnames/pubtypes section.
454 const char* name = (this->is_pubtypes_
456 : ".debug_pubnames");
457 for (unsigned int i = 1; i < object->shnum(); ++i)
459 if (object->section_name(i) == name)
462 this->output_section_offset_ = object->output_section_offset(i);
470 this->buffer_ = object->decompressed_section_contents(shndx,
472 &this->owns_buffer_);
473 if (this->buffer_ == NULL)
475 this->buffer_end_ = this->buffer_ + buffer_size;
479 // Read the header for the set at OFFSET.
482 Dwarf_pubnames_table::read_header(off_t offset)
484 // Correct the offset. For incremental update links, we have a
485 // relocated offset that is relative to the output section, but
486 // here we need an offset relative to the input section.
487 offset -= this->output_section_offset_;
489 if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
492 const unsigned char* pinfo = this->buffer_ + offset;
494 // Read the unit_length field.
495 uint32_t unit_length = read_from_pointer<32>(pinfo);
497 if (unit_length == 0xffffffff)
499 unit_length = read_from_pointer<64>(pinfo);
501 this->offset_size_ = 8;
504 this->offset_size_ = 4;
506 // Check the version.
507 unsigned int version = read_from_pointer<16>(pinfo);
512 // Skip the debug_info_offset and debug_info_size fields.
513 pinfo += 2 * this->offset_size_;
515 if (pinfo >= this->buffer_end_)
518 this->pinfo_ = pinfo;
522 // Read the next name from the set.
525 Dwarf_pubnames_table::next_name()
527 const unsigned char* pinfo = this->pinfo_;
529 // Read the offset within the CU. If this is zero, we have reached
530 // the end of the list.
532 if (this->offset_size_ == 4)
533 offset = read_from_pointer<32>(&pinfo);
535 offset = read_from_pointer<64>(&pinfo);
539 // Return a pointer to the string at the current location,
540 // and advance the pointer to the next entry.
541 const char* ret = reinterpret_cast<const char*>(pinfo);
542 while (pinfo < this->buffer_end_ && *pinfo != '\0')
544 if (pinfo < this->buffer_end_)
547 this->pinfo_ = pinfo;
553 Dwarf_die::Dwarf_die(
554 Dwarf_info_reader* dwinfo,
557 : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
558 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
559 attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
560 linkage_name_off_(-1), string_shndx_(0), specification_(0),
564 const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
567 unsigned int code = read_unsigned_LEB_128(pdie, &len);
571 parent->set_sibling_offset(die_offset + len);
574 this->attr_offset_ = len;
576 // Lookup the abbrev code in the abbrev table.
577 this->abbrev_code_ = dwinfo->get_abbrev(code);
580 // Read all the attributes of the DIE.
583 Dwarf_die::read_attributes()
585 if (this->attributes_read_)
588 gold_assert(this->abbrev_code_ != NULL);
590 const unsigned char* pdie =
591 this->dwinfo_->buffer_at_offset(this->die_offset_);
594 const unsigned char* pattr = pdie + this->attr_offset_;
596 unsigned int nattr = this->abbrev_code_->attributes.size();
597 this->attributes_.reserve(nattr);
598 for (unsigned int i = 0; i < nattr; ++i)
601 unsigned int attr = this->abbrev_code_->attributes[i].attr;
602 unsigned int form = this->abbrev_code_->attributes[i].form;
603 if (form == elfcpp::DW_FORM_indirect)
605 form = read_unsigned_LEB_128(pattr, &len);
608 off_t attr_off = this->die_offset_ + (pattr - pdie);
609 bool ref_form = false;
610 Attribute_value attr_value;
611 attr_value.attr = attr;
612 attr_value.form = form;
613 attr_value.aux.shndx = 0;
616 case elfcpp::DW_FORM_null:
617 attr_value.val.intval = 0;
619 case elfcpp::DW_FORM_flag_present:
620 attr_value.val.intval = 1;
622 case elfcpp::DW_FORM_strp:
625 if (this->dwinfo_->offset_size() == 4)
626 str_off = read_from_pointer<32>(&pattr);
628 str_off = read_from_pointer<64>(&pattr);
630 this->dwinfo_->lookup_reloc(attr_off, &str_off);
631 attr_value.aux.shndx = shndx;
632 attr_value.val.refval = str_off;
635 case elfcpp::DW_FORM_sec_offset:
638 if (this->dwinfo_->offset_size() == 4)
639 sec_off = read_from_pointer<32>(&pattr);
641 sec_off = read_from_pointer<64>(&pattr);
643 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
644 attr_value.aux.shndx = shndx;
645 attr_value.val.refval = sec_off;
649 case elfcpp::DW_FORM_addr:
650 case elfcpp::DW_FORM_ref_addr:
653 if (this->dwinfo_->address_size() == 4)
654 sec_off = read_from_pointer<32>(&pattr);
656 sec_off = read_from_pointer<64>(&pattr);
658 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
659 attr_value.aux.shndx = shndx;
660 attr_value.val.refval = sec_off;
664 case elfcpp::DW_FORM_block1:
665 attr_value.aux.blocklen = *pattr++;
666 attr_value.val.blockval = pattr;
667 pattr += attr_value.aux.blocklen;
669 case elfcpp::DW_FORM_block2:
670 attr_value.aux.blocklen = read_from_pointer<16>(&pattr);
671 attr_value.val.blockval = pattr;
672 pattr += attr_value.aux.blocklen;
674 case elfcpp::DW_FORM_block4:
675 attr_value.aux.blocklen = read_from_pointer<32>(&pattr);
676 attr_value.val.blockval = pattr;
677 pattr += attr_value.aux.blocklen;
679 case elfcpp::DW_FORM_block:
680 case elfcpp::DW_FORM_exprloc:
681 attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
682 attr_value.val.blockval = pattr + len;
683 pattr += len + attr_value.aux.blocklen;
685 case elfcpp::DW_FORM_data1:
686 case elfcpp::DW_FORM_flag:
687 attr_value.val.intval = *pattr++;
689 case elfcpp::DW_FORM_ref1:
690 attr_value.val.refval = *pattr++;
693 case elfcpp::DW_FORM_data2:
694 attr_value.val.intval = read_from_pointer<16>(&pattr);
696 case elfcpp::DW_FORM_ref2:
697 attr_value.val.refval = read_from_pointer<16>(&pattr);
700 case elfcpp::DW_FORM_data4:
703 sec_off = read_from_pointer<32>(&pattr);
705 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
706 attr_value.aux.shndx = shndx;
707 attr_value.val.intval = sec_off;
710 case elfcpp::DW_FORM_ref4:
713 sec_off = read_from_pointer<32>(&pattr);
715 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
716 attr_value.aux.shndx = shndx;
717 attr_value.val.refval = sec_off;
721 case elfcpp::DW_FORM_data8:
724 sec_off = read_from_pointer<64>(&pattr);
726 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
727 attr_value.aux.shndx = shndx;
728 attr_value.val.intval = sec_off;
731 case elfcpp::DW_FORM_ref_sig8:
732 attr_value.val.uintval = read_from_pointer<64>(&pattr);
734 case elfcpp::DW_FORM_ref8:
737 sec_off = read_from_pointer<64>(&pattr);
739 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
740 attr_value.aux.shndx = shndx;
741 attr_value.val.refval = sec_off;
745 case elfcpp::DW_FORM_ref_udata:
746 attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
750 case elfcpp::DW_FORM_udata:
751 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
754 case elfcpp::DW_FORM_sdata:
755 attr_value.val.intval = read_signed_LEB_128(pattr, &len);
758 case elfcpp::DW_FORM_string:
759 attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
760 len = strlen(attr_value.val.stringval);
767 // Cache the most frequently-requested attributes.
770 case elfcpp::DW_AT_name:
771 if (form == elfcpp::DW_FORM_string)
772 this->name_ = attr_value.val.stringval;
773 else if (form == elfcpp::DW_FORM_strp)
775 // All indirect strings should refer to the same
776 // string section, so we just save the last one seen.
777 this->string_shndx_ = attr_value.aux.shndx;
778 this->name_off_ = attr_value.val.refval;
781 case elfcpp::DW_AT_linkage_name:
782 case elfcpp::DW_AT_MIPS_linkage_name:
783 if (form == elfcpp::DW_FORM_string)
784 this->linkage_name_ = attr_value.val.stringval;
785 else if (form == elfcpp::DW_FORM_strp)
787 // All indirect strings should refer to the same
788 // string section, so we just save the last one seen.
789 this->string_shndx_ = attr_value.aux.shndx;
790 this->linkage_name_off_ = attr_value.val.refval;
793 case elfcpp::DW_AT_specification:
795 this->specification_ = attr_value.val.refval;
797 case elfcpp::DW_AT_abstract_origin:
799 this->abstract_origin_ = attr_value.val.refval;
801 case elfcpp::DW_AT_sibling:
802 if (ref_form && attr_value.aux.shndx == 0)
803 this->sibling_offset_ = attr_value.val.refval;
808 this->attributes_.push_back(attr_value);
811 // Now that we know where the next DIE begins, record the offset
812 // to avoid later recalculation.
813 if (this->has_children())
814 this->child_offset_ = this->die_offset_ + (pattr - pdie);
816 this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
818 this->attributes_read_ = true;
822 // Skip all the attributes of the DIE and return the offset of the next DIE.
825 Dwarf_die::skip_attributes()
827 typedef Dwarf_abbrev_table::Attribute Attribute;
829 gold_assert(this->abbrev_code_ != NULL);
831 const unsigned char* pdie =
832 this->dwinfo_->buffer_at_offset(this->die_offset_);
835 const unsigned char* pattr = pdie + this->attr_offset_;
837 for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
840 unsigned int form = this->abbrev_code_->attributes[i].form;
841 if (form == elfcpp::DW_FORM_indirect)
843 form = read_unsigned_LEB_128(pattr, &len);
848 case elfcpp::DW_FORM_null:
849 case elfcpp::DW_FORM_flag_present:
851 case elfcpp::DW_FORM_strp:
852 case elfcpp::DW_FORM_sec_offset:
853 pattr += this->dwinfo_->offset_size();
855 case elfcpp::DW_FORM_addr:
856 case elfcpp::DW_FORM_ref_addr:
857 pattr += this->dwinfo_->address_size();
859 case elfcpp::DW_FORM_block1:
862 case elfcpp::DW_FORM_block2:
865 block_size = read_from_pointer<16>(&pattr);
869 case elfcpp::DW_FORM_block4:
872 block_size = read_from_pointer<32>(&pattr);
876 case elfcpp::DW_FORM_block:
877 case elfcpp::DW_FORM_exprloc:
880 block_size = read_unsigned_LEB_128(pattr, &len);
881 pattr += len + block_size;
884 case elfcpp::DW_FORM_data1:
885 case elfcpp::DW_FORM_ref1:
886 case elfcpp::DW_FORM_flag:
889 case elfcpp::DW_FORM_data2:
890 case elfcpp::DW_FORM_ref2:
893 case elfcpp::DW_FORM_data4:
894 case elfcpp::DW_FORM_ref4:
897 case elfcpp::DW_FORM_data8:
898 case elfcpp::DW_FORM_ref8:
899 case elfcpp::DW_FORM_ref_sig8:
902 case elfcpp::DW_FORM_ref_udata:
903 case elfcpp::DW_FORM_udata:
904 read_unsigned_LEB_128(pattr, &len);
907 case elfcpp::DW_FORM_sdata:
908 read_signed_LEB_128(pattr, &len);
911 case elfcpp::DW_FORM_string:
912 len = strlen(reinterpret_cast<const char*>(pattr));
920 return this->die_offset_ + (pattr - pdie);
923 // Get the name of the DIE and cache it.
926 Dwarf_die::set_name()
928 if (this->name_ != NULL || !this->read_attributes())
930 if (this->name_off_ != -1)
931 this->name_ = this->dwinfo_->get_string(this->name_off_,
932 this->string_shndx_);
935 // Get the linkage name of the DIE and cache it.
938 Dwarf_die::set_linkage_name()
940 if (this->linkage_name_ != NULL || !this->read_attributes())
942 if (this->linkage_name_off_ != -1)
943 this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
944 this->string_shndx_);
947 // Return the value of attribute ATTR.
949 const Dwarf_die::Attribute_value*
950 Dwarf_die::attribute(unsigned int attr)
952 if (!this->read_attributes())
954 for (unsigned int i = 0; i < this->attributes_.size(); ++i)
956 if (this->attributes_[i].attr == attr)
957 return &this->attributes_[i];
963 Dwarf_die::string_attribute(unsigned int attr)
965 const Attribute_value* attr_val = this->attribute(attr);
966 if (attr_val == NULL)
968 switch (attr_val->form)
970 case elfcpp::DW_FORM_string:
971 return attr_val->val.stringval;
972 case elfcpp::DW_FORM_strp:
973 return this->dwinfo_->get_string(attr_val->val.refval,
974 attr_val->aux.shndx);
981 Dwarf_die::int_attribute(unsigned int attr)
983 const Attribute_value* attr_val = this->attribute(attr);
984 if (attr_val == NULL)
986 switch (attr_val->form)
988 case elfcpp::DW_FORM_null:
989 case elfcpp::DW_FORM_flag_present:
990 case elfcpp::DW_FORM_data1:
991 case elfcpp::DW_FORM_flag:
992 case elfcpp::DW_FORM_data2:
993 case elfcpp::DW_FORM_data4:
994 case elfcpp::DW_FORM_data8:
995 case elfcpp::DW_FORM_sdata:
996 return attr_val->val.intval;
1003 Dwarf_die::uint_attribute(unsigned int attr)
1005 const Attribute_value* attr_val = this->attribute(attr);
1006 if (attr_val == NULL)
1008 switch (attr_val->form)
1010 case elfcpp::DW_FORM_null:
1011 case elfcpp::DW_FORM_flag_present:
1012 case elfcpp::DW_FORM_data1:
1013 case elfcpp::DW_FORM_flag:
1014 case elfcpp::DW_FORM_data4:
1015 case elfcpp::DW_FORM_data8:
1016 case elfcpp::DW_FORM_ref_sig8:
1017 case elfcpp::DW_FORM_udata:
1018 return attr_val->val.uintval;
1025 Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx)
1027 const Attribute_value* attr_val = this->attribute(attr);
1028 if (attr_val == NULL)
1030 switch (attr_val->form)
1032 case elfcpp::DW_FORM_sec_offset:
1033 case elfcpp::DW_FORM_addr:
1034 case elfcpp::DW_FORM_ref_addr:
1035 case elfcpp::DW_FORM_ref1:
1036 case elfcpp::DW_FORM_ref2:
1037 case elfcpp::DW_FORM_ref4:
1038 case elfcpp::DW_FORM_ref8:
1039 case elfcpp::DW_FORM_ref_udata:
1040 *shndx = attr_val->aux.shndx;
1041 return attr_val->val.refval;
1042 case elfcpp::DW_FORM_ref_sig8:
1043 *shndx = attr_val->aux.shndx;
1044 return attr_val->val.uintval;
1045 case elfcpp::DW_FORM_data4:
1046 case elfcpp::DW_FORM_data8:
1047 *shndx = attr_val->aux.shndx;
1048 return attr_val->val.intval;
1055 Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx)
1057 const Attribute_value* attr_val = this->attribute(attr);
1058 if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr)
1061 *shndx = attr_val->aux.shndx;
1062 return attr_val->val.refval;
1065 // Return the offset of this DIE's first child.
1068 Dwarf_die::child_offset()
1070 gold_assert(this->abbrev_code_ != NULL);
1071 if (!this->has_children())
1073 if (this->child_offset_ == 0)
1074 this->child_offset_ = this->skip_attributes();
1075 return this->child_offset_;
1078 // Return the offset of this DIE's next sibling.
1081 Dwarf_die::sibling_offset()
1083 gold_assert(this->abbrev_code_ != NULL);
1085 if (this->sibling_offset_ != 0)
1086 return this->sibling_offset_;
1088 if (!this->has_children())
1090 this->sibling_offset_ = this->skip_attributes();
1091 return this->sibling_offset_;
1094 if (this->has_sibling_attribute())
1096 if (!this->read_attributes())
1098 if (this->sibling_offset_ != 0)
1099 return this->sibling_offset_;
1102 // Skip over the children.
1103 off_t child_offset = this->child_offset();
1104 while (child_offset > 0)
1106 Dwarf_die die(this->dwinfo_, child_offset, this);
1107 // The Dwarf_die ctor will set this DIE's sibling offset
1108 // when it reads a zero abbrev code.
1111 child_offset = die.sibling_offset();
1114 // This should be set by now. If not, there was a problem reading
1115 // the DWARF info, and we return 0.
1116 return this->sibling_offset_;
1119 // class Dwarf_info_reader
1121 // Check that the pointer P is within the current compilation unit.
1124 Dwarf_info_reader::check_buffer(const unsigned char* p) const
1126 if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
1128 gold_warning(_("%s: corrupt debug info in %s"),
1129 this->object_->name().c_str(),
1130 this->object_->section_name(this->shndx_).c_str());
1136 // Begin parsing the debug info. This calls visit_compilation_unit()
1137 // or visit_type_unit() for each compilation or type unit found in the
1138 // section, and visit_die() for each top-level DIE.
1141 Dwarf_info_reader::parse()
1143 switch (parameters->size_and_endianness())
1145 #ifdef HAVE_TARGET_32_LITTLE
1146 case Parameters::TARGET_32_LITTLE:
1147 this->do_parse<false>();
1150 #ifdef HAVE_TARGET_32_BIG
1151 case Parameters::TARGET_32_BIG:
1152 this->do_parse<true>();
1155 #ifdef HAVE_TARGET_64_LITTLE
1156 case Parameters::TARGET_64_LITTLE:
1157 this->do_parse<false>();
1160 #ifdef HAVE_TARGET_64_BIG
1161 case Parameters::TARGET_64_BIG:
1162 this->do_parse<true>();
1170 template<bool big_endian>
1172 Dwarf_info_reader::do_parse()
1174 // Get the section contents and decompress if necessary.
1175 section_size_type buffer_size;
1177 this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
1180 if (this->buffer_ == NULL || buffer_size == 0)
1182 this->buffer_end_ = this->buffer_ + buffer_size;
1184 // The offset of this input section in the output section.
1185 off_t section_offset = this->object_->output_section_offset(this->shndx_);
1187 // Start tracking relocations for this section.
1188 this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_,
1189 this->symtab_size_);
1190 this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_);
1192 // Loop over compilation units (or type units).
1193 unsigned int abbrev_shndx = 0;
1194 off_t abbrev_offset = 0;
1195 const unsigned char* pinfo = this->buffer_;
1196 while (pinfo < this->buffer_end_)
1198 // Read the compilation (or type) unit header.
1199 const unsigned char* cu_start = pinfo;
1200 this->cu_offset_ = cu_start - this->buffer_;
1201 this->cu_length_ = this->buffer_end_ - cu_start;
1203 // Read unit_length (4 or 12 bytes).
1204 if (!this->check_buffer(pinfo + 4))
1206 uint32_t unit_length =
1207 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1209 if (unit_length == 0xffffffff)
1211 if (!this->check_buffer(pinfo + 8))
1213 unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1215 this->offset_size_ = 8;
1218 this->offset_size_ = 4;
1219 if (!this->check_buffer(pinfo + unit_length))
1221 const unsigned char* cu_end = pinfo + unit_length;
1222 this->cu_length_ = cu_end - cu_start;
1223 if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1))
1226 // Read version (2 bytes).
1228 elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
1231 // Read debug_abbrev_offset (4 or 8 bytes).
1232 if (this->offset_size_ == 4)
1233 abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1235 abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1236 if (this->reloc_shndx_ > 0)
1238 off_t reloc_offset = pinfo - this->buffer_;
1241 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1242 if (abbrev_shndx == 0)
1244 if (this->reloc_type_ == elfcpp::SHT_REL)
1245 abbrev_offset += value;
1247 abbrev_offset = value;
1249 pinfo += this->offset_size_;
1251 // Read address_size (1 byte).
1252 this->address_size_ = *pinfo++;
1254 // For type units, read the two extra fields.
1255 uint64_t signature = 0;
1256 off_t type_offset = 0;
1257 if (this->is_type_unit_)
1259 if (!this->check_buffer(pinfo + 8 + this->offset_size_))
1262 // Read type_signature (8 bytes).
1263 signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1266 // Read type_offset (4 or 8 bytes).
1267 if (this->offset_size_ == 4)
1269 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1272 elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1273 pinfo += this->offset_size_;
1276 // Read the .debug_abbrev table.
1277 this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx,
1280 // Visit the root DIE.
1281 Dwarf_die root_die(this,
1282 pinfo - (this->buffer_ + this->cu_offset_),
1284 if (root_die.tag() != 0)
1286 // Visit the CU or TU.
1287 if (this->is_type_unit_)
1288 this->visit_type_unit(section_offset + this->cu_offset_,
1289 type_offset, signature, &root_die);
1291 this->visit_compilation_unit(section_offset + this->cu_offset_,
1292 cu_end - cu_start, &root_die);
1295 // Advance to the next CU.
1301 delete[] this->buffer_;
1302 this->buffer_ = NULL;
1306 // Read the DWARF string table.
1309 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx)
1311 Relobj* object = this->object_;
1313 // If we don't have relocations, string_shndx will be 0, and
1314 // we'll have to hunt for the .debug_str section.
1315 if (string_shndx == 0)
1317 for (unsigned int i = 1; i < this->object_->shnum(); ++i)
1319 std::string name = object->section_name(i);
1320 if (name == ".debug_str")
1323 this->string_output_section_offset_ =
1324 object->output_section_offset(i);
1328 if (string_shndx == 0)
1332 if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
1334 delete[] this->string_buffer_;
1335 this->owns_string_buffer_ = false;
1338 // Get the secton contents and decompress if necessary.
1339 section_size_type buffer_size;
1340 const unsigned char* buffer =
1341 object->decompressed_section_contents(string_shndx,
1343 &this->owns_string_buffer_);
1344 this->string_buffer_ = reinterpret_cast<const char*>(buffer);
1345 this->string_buffer_end_ = this->string_buffer_ + buffer_size;
1346 this->string_shndx_ = string_shndx;
1350 // Look for a relocation at offset ATTR_OFF in the dwarf info,
1351 // and return the section index and offset of the target.
1354 Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
1357 attr_off += this->cu_offset_;
1358 unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
1361 if (this->reloc_type_ == elfcpp::SHT_REL)
1362 *target_off += value;
1364 *target_off = value;
1368 // Return a string from the DWARF string table.
1371 Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
1373 if (!this->read_string_table(string_shndx))
1376 // Correct the offset. For incremental update links, we have a
1377 // relocated offset that is relative to the output section, but
1378 // here we need an offset relative to the input section.
1379 str_off -= this->string_output_section_offset_;
1381 const char* p = this->string_buffer_ + str_off;
1383 if (p < this->string_buffer_ || p >= this->string_buffer_end_)
1389 // The following are default, do-nothing, implementations of the
1390 // hook methods normally provided by a derived class. We provide
1391 // default implementations rather than no implementation so that
1392 // a derived class needs to implement only the hooks that it needs
1395 // Process a compilation unit and parse its child DIE.
1398 Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
1402 // Process a type unit and parse its child DIE.
1405 Dwarf_info_reader::visit_type_unit(off_t, off_t, uint64_t, Dwarf_die*)
1409 // class Sized_dwarf_line_info
1411 struct LineStateMachine
1417 unsigned int shndx; // the section address refers to
1418 bool is_stmt; // stmt means statement.
1424 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
1429 lsm->column_num = 0;
1431 lsm->is_stmt = default_is_stmt;
1432 lsm->basic_block = false;
1433 lsm->end_sequence = false;
1436 template<int size, bool big_endian>
1437 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
1439 unsigned int read_shndx)
1440 : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
1441 reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(),
1442 current_header_index_(-1)
1444 unsigned int debug_shndx;
1446 for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
1448 // FIXME: do this more efficiently: section_name() isn't super-fast
1449 std::string name = object->section_name(debug_shndx);
1450 if (name == ".debug_line" || name == ".zdebug_line")
1452 section_size_type buffer_size;
1453 bool is_new = false;
1454 this->buffer_ = object->decompressed_section_contents(debug_shndx,
1458 this->buffer_start_ = this->buffer_;
1459 this->buffer_end_ = this->buffer_ + buffer_size;
1463 if (this->buffer_ == NULL)
1466 // Find the relocation section for ".debug_line".
1467 // We expect these for relobjs (.o's) but not dynobjs (.so's).
1468 unsigned int reloc_shndx = 0;
1469 for (unsigned int i = 0; i < object->shnum(); ++i)
1471 unsigned int reloc_sh_type = object->section_type(i);
1472 if ((reloc_sh_type == elfcpp::SHT_REL
1473 || reloc_sh_type == elfcpp::SHT_RELA)
1474 && object->section_info(i) == debug_shndx)
1477 this->track_relocs_type_ = reloc_sh_type;
1482 // Finally, we need the symtab section to interpret the relocs.
1483 if (reloc_shndx != 0)
1485 unsigned int symtab_shndx;
1486 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
1487 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
1489 this->symtab_buffer_ = object->section_contents(
1490 symtab_shndx, &this->symtab_buffer_size_, false);
1493 if (this->symtab_buffer_ == NULL)
1497 this->reloc_mapper_ =
1498 new Sized_elf_reloc_mapper<size, big_endian>(object,
1499 this->symtab_buffer_,
1500 this->symtab_buffer_size_);
1501 if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_))
1504 // Now that we have successfully read all the data, parse the debug
1506 this->data_valid_ = true;
1507 this->read_line_mappings(read_shndx);
1510 // Read the DWARF header.
1512 template<int size, bool big_endian>
1513 const unsigned char*
1514 Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
1515 const unsigned char* lineptr)
1517 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1520 // In DWARF2/3, if the initial length is all 1 bits, then the offset
1521 // size is 8 and we need to read the next 8 bytes for the real length.
1522 if (initial_length == 0xffffffff)
1524 header_.offset_size = 8;
1525 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1529 header_.offset_size = 4;
1531 header_.total_length = initial_length;
1533 gold_assert(lineptr + header_.total_length <= buffer_end_);
1535 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
1538 if (header_.offset_size == 4)
1539 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1541 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1542 lineptr += header_.offset_size;
1544 header_.min_insn_length = *lineptr;
1547 header_.default_is_stmt = *lineptr;
1550 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
1553 header_.line_range = *lineptr;
1556 header_.opcode_base = *lineptr;
1559 header_.std_opcode_lengths.resize(header_.opcode_base + 1);
1560 header_.std_opcode_lengths[0] = 0;
1561 for (int i = 1; i < header_.opcode_base; i++)
1563 header_.std_opcode_lengths[i] = *lineptr;
1570 // The header for a debug_line section is mildly complicated, because
1571 // the line info is very tightly encoded.
1573 template<int size, bool big_endian>
1574 const unsigned char*
1575 Sized_dwarf_line_info<size, big_endian>::read_header_tables(
1576 const unsigned char* lineptr)
1578 ++this->current_header_index_;
1580 // Create a new directories_ entry and a new files_ entry for our new
1581 // header. We initialize each with a single empty element, because
1582 // dwarf indexes directory and filenames starting at 1.
1583 gold_assert(static_cast<int>(this->directories_.size())
1584 == this->current_header_index_);
1585 gold_assert(static_cast<int>(this->files_.size())
1586 == this->current_header_index_);
1587 this->directories_.push_back(std::vector<std::string>(1));
1588 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
1590 // It is legal for the directory entry table to be empty.
1596 const char* dirname = reinterpret_cast<const char*>(lineptr);
1597 gold_assert(dirindex
1598 == static_cast<int>(this->directories_.back().size()));
1599 this->directories_.back().push_back(dirname);
1600 lineptr += this->directories_.back().back().size() + 1;
1606 // It is also legal for the file entry table to be empty.
1613 const char* filename = reinterpret_cast<const char*>(lineptr);
1614 lineptr += strlen(filename) + 1;
1616 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
1619 if (dirindex >= this->directories_.back().size())
1621 int dirindexi = static_cast<int>(dirindex);
1623 read_unsigned_LEB_128(lineptr, &len); // mod_time
1626 read_unsigned_LEB_128(lineptr, &len); // filelength
1629 gold_assert(fileindex
1630 == static_cast<int>(this->files_.back().size()));
1631 this->files_.back().push_back(std::make_pair(dirindexi, filename));
1640 // Process a single opcode in the .debug.line structure.
1642 template<int size, bool big_endian>
1644 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
1645 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
1649 unsigned char opcode = *start;
1653 // If the opcode is great than the opcode_base, it is a special
1654 // opcode. Most line programs consist mainly of special opcodes.
1655 if (opcode >= header_.opcode_base)
1657 opcode -= header_.opcode_base;
1658 const int advance_address = ((opcode / header_.line_range)
1659 * header_.min_insn_length);
1660 lsm->address += advance_address;
1662 const int advance_line = ((opcode % header_.line_range)
1663 + header_.line_base);
1664 lsm->line_num += advance_line;
1665 lsm->basic_block = true;
1670 // Otherwise, we have the regular opcodes
1673 case elfcpp::DW_LNS_copy:
1674 lsm->basic_block = false;
1678 case elfcpp::DW_LNS_advance_pc:
1680 const uint64_t advance_address
1681 = read_unsigned_LEB_128(start, &templen);
1683 lsm->address += header_.min_insn_length * advance_address;
1687 case elfcpp::DW_LNS_advance_line:
1689 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
1691 lsm->line_num += advance_line;
1695 case elfcpp::DW_LNS_set_file:
1697 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
1699 lsm->file_num = fileno;
1703 case elfcpp::DW_LNS_set_column:
1705 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
1707 lsm->column_num = colno;
1711 case elfcpp::DW_LNS_negate_stmt:
1712 lsm->is_stmt = !lsm->is_stmt;
1715 case elfcpp::DW_LNS_set_basic_block:
1716 lsm->basic_block = true;
1719 case elfcpp::DW_LNS_fixed_advance_pc:
1721 int advance_address;
1722 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
1724 lsm->address += advance_address;
1728 case elfcpp::DW_LNS_const_add_pc:
1730 const int advance_address = (header_.min_insn_length
1731 * ((255 - header_.opcode_base)
1732 / header_.line_range));
1733 lsm->address += advance_address;
1737 case elfcpp::DW_LNS_extended_op:
1739 const uint64_t extended_op_len
1740 = read_unsigned_LEB_128(start, &templen);
1742 oplen += templen + extended_op_len;
1744 const unsigned char extended_op = *start;
1747 switch (extended_op)
1749 case elfcpp::DW_LNE_end_sequence:
1750 // This means that the current byte is the one immediately
1751 // after a set of instructions. Record the current line
1752 // for up to one less than the current address.
1754 lsm->end_sequence = true;
1758 case elfcpp::DW_LNE_set_address:
1761 elfcpp::Swap_unaligned<size, big_endian>::readval(start);
1762 typename Reloc_map::const_iterator it
1763 = this->reloc_map_.find(start - this->buffer_);
1764 if (it != reloc_map_.end())
1766 // If this is a SHT_RELA section, then ignore the
1767 // section contents. This assumes that this is a
1768 // straight reloc which just uses the reloc addend.
1769 // The reloc addend has already been included in the
1771 if (this->track_relocs_type_ == elfcpp::SHT_RELA)
1773 // Add in the symbol value.
1774 lsm->address += it->second.second;
1775 lsm->shndx = it->second.first;
1779 // If we're a normal .o file, with relocs, every
1780 // set_address should have an associated relocation.
1781 if (this->input_is_relobj())
1782 this->data_valid_ = false;
1786 case elfcpp::DW_LNE_define_file:
1788 const char* filename = reinterpret_cast<const char*>(start);
1789 templen = strlen(filename) + 1;
1792 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
1794 if (dirindex >= this->directories_.back().size())
1796 int dirindexi = static_cast<int>(dirindex);
1798 // This opcode takes two additional ULEB128 parameters
1799 // (mod_time and filelength), but we don't use those
1800 // values. Because OPLEN already tells us how far to
1801 // skip to the next opcode, we don't need to read
1804 this->files_.back().push_back(std::make_pair(dirindexi,
1814 // Ignore unknown opcode silently
1815 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
1818 read_unsigned_LEB_128(start, &templen);
1829 // Read the debug information at LINEPTR and store it in the line
1832 template<int size, bool big_endian>
1833 unsigned const char*
1834 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
1837 struct LineStateMachine lsm;
1839 // LENGTHSTART is the place the length field is based on. It is the
1840 // point in the header after the initial length field.
1841 const unsigned char* lengthstart = buffer_;
1843 // In 64 bit dwarf, the initial length is 12 bytes, because of the
1844 // 0xffffffff at the start.
1845 if (header_.offset_size == 8)
1850 while (lineptr < lengthstart + header_.total_length)
1852 ResetLineStateMachine(&lsm, header_.default_is_stmt);
1853 while (!lsm.end_sequence)
1856 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
1858 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
1860 Offset_to_lineno_entry entry
1861 = { static_cast<off_t>(lsm.address),
1862 this->current_header_index_,
1863 static_cast<unsigned int>(lsm.file_num),
1864 true, lsm.line_num };
1865 std::vector<Offset_to_lineno_entry>&
1866 map(this->line_number_map_[lsm.shndx]);
1867 // If we see two consecutive entries with the same
1868 // offset and a real line number, then mark the first
1869 // one as non-canonical.
1871 && (map.back().offset == static_cast<off_t>(lsm.address))
1872 && lsm.line_num != -1
1873 && map.back().line_num != -1)
1874 map.back().last_line_for_offset = false;
1875 map.push_back(entry);
1877 lineptr += oplength;
1881 return lengthstart + header_.total_length;
1884 // Read the relocations into a Reloc_map.
1886 template<int size, bool big_endian>
1888 Sized_dwarf_line_info<size, big_endian>::read_relocs()
1890 if (this->symtab_buffer_ == NULL)
1895 while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1)
1897 const unsigned int shndx =
1898 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1900 // There is no reason to record non-ordinary section indexes, or
1901 // SHN_UNDEF, because they will never match the real section.
1903 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
1905 this->reloc_mapper_->advance(reloc_offset + 1);
1909 // Read the line number info.
1911 template<int size, bool big_endian>
1913 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx)
1915 gold_assert(this->data_valid_ == true);
1917 this->read_relocs();
1918 while (this->buffer_ < this->buffer_end_)
1920 const unsigned char* lineptr = this->buffer_;
1921 lineptr = this->read_header_prolog(lineptr);
1922 lineptr = this->read_header_tables(lineptr);
1923 lineptr = this->read_lines(lineptr, shndx);
1924 this->buffer_ = lineptr;
1927 // Sort the lines numbers, so addr2line can use binary search.
1928 for (typename Lineno_map::iterator it = line_number_map_.begin();
1929 it != line_number_map_.end();
1931 // Each vector needs to be sorted by offset.
1932 std::sort(it->second.begin(), it->second.end());
1935 // Some processing depends on whether the input is a .o file or not.
1936 // For instance, .o files have relocs, and have .debug_lines
1937 // information on a per section basis. .so files, on the other hand,
1938 // lack relocs, and offsets are unique, so we can ignore the section
1941 template<int size, bool big_endian>
1943 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
1945 // Only .o files have relocs and the symtab buffer that goes with them.
1946 return this->symtab_buffer_ != NULL;
1949 // Given an Offset_to_lineno_entry vector, and an offset, figure out
1950 // if the offset points into a function according to the vector (see
1951 // comments below for the algorithm). If it does, return an iterator
1952 // into the vector that points to the line-number that contains that
1953 // offset. If not, it returns vector::end().
1955 static std::vector<Offset_to_lineno_entry>::const_iterator
1956 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
1959 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
1961 // lower_bound() returns the smallest offset which is >= lookup_key.
1962 // If no offset in offsets is >= lookup_key, returns end().
1963 std::vector<Offset_to_lineno_entry>::const_iterator it
1964 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
1966 // This code is easiest to understand with a concrete example.
1967 // Here's a possible offsets array:
1968 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
1969 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
1970 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
1971 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
1972 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
1973 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
1974 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
1975 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
1976 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
1977 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
1978 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
1979 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
1980 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
1981 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
1982 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
1983 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
1984 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
1985 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
1986 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
1987 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
1988 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
1989 // The entries with line_num == -1 mark the end of a function: the
1990 // associated offset is one past the last instruction in the
1991 // function. This can correspond to the beginning of the next
1992 // function (as is true for offset 3232); alternately, there can be
1993 // a gap between the end of one function and the start of the next
1994 // (as is true for some others, most obviously from 3236->5764).
1996 // Case 1: lookup_key has offset == 10. lower_bound returns
1997 // offsets[0]. Since it's not an exact match and we're
1998 // at the beginning of offsets, we return end() (invalid).
1999 // Case 2: lookup_key has offset 10000. lower_bound returns
2000 // offset[21] (end()). We return end() (invalid).
2001 // Case 3: lookup_key has offset == 3211. lower_bound matches
2002 // offsets[0] exactly, and that's the entry we return.
2003 // Case 4: lookup_key has offset == 3232. lower_bound returns
2004 // offsets[4]. That's an exact match, but indicates
2005 // end-of-function. We check if offsets[5] is also an
2006 // exact match but not end-of-function. It is, so we
2007 // return offsets[5].
2008 // Case 5: lookup_key has offset == 3214. lower_bound returns
2009 // offsets[1]. Since it's not an exact match, we back
2010 // up to the offset that's < lookup_key, offsets[0].
2011 // We note offsets[0] is a valid entry (not end-of-function),
2012 // so that's the entry we return.
2013 // Case 6: lookup_key has offset == 4000. lower_bound returns
2014 // offsets[8]. Since it's not an exact match, we back
2015 // up to offsets[7]. Since offsets[7] indicates
2016 // end-of-function, we know lookup_key is between
2017 // functions, so we return end() (not a valid offset).
2018 // Case 7: lookup_key has offset == 5794. lower_bound returns
2019 // offsets[19]. Since it's not an exact match, we back
2020 // up to offsets[16]. Note we back up to the *first*
2021 // entry with offset 5793, not just offsets[19-1].
2022 // We note offsets[16] is a valid entry, so we return it.
2023 // If offsets[16] had had line_num == -1, we would have
2024 // checked offsets[17]. The reason for this is that
2025 // 16 and 17 can be in an arbitrary order, since we sort
2026 // only by offset and last_line_for_offset. (Note it
2027 // doesn't help to use line_number as a tertiary sort key,
2028 // since sometimes we want the -1 to be first and sometimes
2029 // we want it to be last.)
2031 // This deals with cases (1) and (2).
2032 if ((it == offsets->begin() && offset < it->offset)
2033 || it == offsets->end())
2034 return offsets->end();
2036 // This deals with cases (3) and (4).
2037 if (offset == it->offset)
2039 while (it != offsets->end()
2040 && it->offset == offset
2041 && it->line_num == -1)
2043 if (it == offsets->end() || it->offset != offset)
2044 return offsets->end();
2049 // This handles the first part of case (7) -- we back up to the
2050 // *first* entry that has the offset that's behind us.
2051 gold_assert(it != offsets->begin());
2052 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
2054 const off_t range_value = it->offset;
2055 while (it != offsets->begin() && (it-1)->offset == range_value)
2058 // This handles cases (5), (6), and (7): if any entry in the
2059 // equal_range [it, range_end) has a line_num != -1, it's a valid
2060 // match. If not, we're not in a function. The line number we saw
2061 // last for an offset will be sorted first, so it'll get returned if
2063 for (; it != range_end; ++it)
2064 if (it->line_num != -1)
2066 return offsets->end();
2069 // Returns the canonical filename:lineno for the address passed in.
2070 // If other_lines is not NULL, appends the non-canonical lines
2071 // assigned to the same address.
2073 template<int size, bool big_endian>
2075 Sized_dwarf_line_info<size, big_endian>::do_addr2line(
2078 std::vector<std::string>* other_lines)
2080 if (this->data_valid_ == false)
2083 const std::vector<Offset_to_lineno_entry>* offsets;
2084 // If we do not have reloc information, then our input is a .so or
2085 // some similar data structure where all the information is held in
2086 // the offset. In that case, we ignore the input shndx.
2087 if (this->input_is_relobj())
2088 offsets = &this->line_number_map_[shndx];
2090 offsets = &this->line_number_map_[-1U];
2091 if (offsets->empty())
2094 typename std::vector<Offset_to_lineno_entry>::const_iterator it
2095 = offset_to_iterator(offsets, offset);
2096 if (it == offsets->end())
2099 std::string result = this->format_file_lineno(*it);
2100 if (other_lines != NULL)
2101 for (++it; it != offsets->end() && it->offset == offset; ++it)
2103 if (it->line_num == -1)
2104 continue; // The end of a previous function.
2105 other_lines->push_back(this->format_file_lineno(*it));
2110 // Convert the file_num + line_num into a string.
2112 template<int size, bool big_endian>
2114 Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
2115 const Offset_to_lineno_entry& loc) const
2119 gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
2120 gold_assert(loc.file_num
2121 < static_cast<unsigned int>(this->files_[loc.header_num].size()));
2122 const std::pair<int, std::string>& filename_pair
2123 = this->files_[loc.header_num][loc.file_num];
2124 const std::string& filename = filename_pair.second;
2126 gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
2127 gold_assert(filename_pair.first
2128 < static_cast<int>(this->directories_[loc.header_num].size()));
2129 const std::string& dirname
2130 = this->directories_[loc.header_num][filename_pair.first];
2132 if (!dirname.empty())
2141 char buffer[64]; // enough to hold a line number
2142 snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
2149 // Dwarf_line_info routines.
2151 static unsigned int next_generation_count = 0;
2153 struct Addr2line_cache_entry
2157 Dwarf_line_info* dwarf_line_info;
2158 unsigned int generation_count;
2159 unsigned int access_count;
2161 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
2162 : object(o), shndx(s), dwarf_line_info(d),
2163 generation_count(next_generation_count), access_count(0)
2165 if (next_generation_count < (1U << 31))
2166 ++next_generation_count;
2169 // We expect this cache to be small, so don't bother with a hashtable
2170 // or priority queue or anything: just use a simple vector.
2171 static std::vector<Addr2line_cache_entry> addr2line_cache;
2174 Dwarf_line_info::one_addr2line(Object* object,
2175 unsigned int shndx, off_t offset,
2177 std::vector<std::string>* other_lines)
2179 Dwarf_line_info* lineinfo = NULL;
2180 std::vector<Addr2line_cache_entry>::iterator it;
2182 // First, check the cache. If we hit, update the counts.
2183 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2185 if (it->object == object && it->shndx == shndx)
2187 lineinfo = it->dwarf_line_info;
2188 it->generation_count = next_generation_count;
2189 // We cap generation_count at 2^31 -1 to avoid overflow.
2190 if (next_generation_count < (1U << 31))
2191 ++next_generation_count;
2192 // We cap access_count at 31 so 2^access_count doesn't overflow
2193 if (it->access_count < 31)
2199 // If we don't hit the cache, create a new object and insert into the
2201 if (lineinfo == NULL)
2203 switch (parameters->size_and_endianness())
2205 #ifdef HAVE_TARGET_32_LITTLE
2206 case Parameters::TARGET_32_LITTLE:
2207 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
2209 #ifdef HAVE_TARGET_32_BIG
2210 case Parameters::TARGET_32_BIG:
2211 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
2213 #ifdef HAVE_TARGET_64_LITTLE
2214 case Parameters::TARGET_64_LITTLE:
2215 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
2217 #ifdef HAVE_TARGET_64_BIG
2218 case Parameters::TARGET_64_BIG:
2219 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
2224 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
2227 // Now that we have our object, figure out the answer
2228 std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
2230 // Finally, if our cache has grown too big, delete old objects. We
2231 // assume the common (probably only) case is deleting only one object.
2232 // We use a pretty simple scheme to evict: function of LRU and MFU.
2233 while (addr2line_cache.size() > cache_size)
2235 unsigned int lowest_score = ~0U;
2236 std::vector<Addr2line_cache_entry>::iterator lowest
2237 = addr2line_cache.end();
2238 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2240 const unsigned int score = (it->generation_count
2241 + (1U << it->access_count));
2242 if (score < lowest_score)
2244 lowest_score = score;
2248 if (lowest != addr2line_cache.end())
2250 delete lowest->dwarf_line_info;
2251 addr2line_cache.erase(lowest);
2259 Dwarf_line_info::clear_addr2line_cache()
2261 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
2262 it != addr2line_cache.end();
2264 delete it->dwarf_line_info;
2265 addr2line_cache.clear();
2268 #ifdef HAVE_TARGET_32_LITTLE
2270 class Sized_dwarf_line_info<32, false>;
2273 #ifdef HAVE_TARGET_32_BIG
2275 class Sized_dwarf_line_info<32, true>;
2278 #ifdef HAVE_TARGET_64_LITTLE
2280 class Sized_dwarf_line_info<64, false>;
2283 #ifdef HAVE_TARGET_64_BIG
2285 class Sized_dwarf_line_info<64, true>;
2288 } // End namespace gold.