1*fae548d3Szrj // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*- 2*fae548d3Szrj 3*fae548d3Szrj // Copyright (C) 2007-2020 Free Software Foundation, Inc. 4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>. 5*fae548d3Szrj 6*fae548d3Szrj // This file is part of gold. 7*fae548d3Szrj 8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify 9*fae548d3Szrj // it under the terms of the GNU General Public License as published by 10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or 11*fae548d3Szrj // (at your option) any later version. 12*fae548d3Szrj 13*fae548d3Szrj // This program is distributed in the hope that it will be useful, 14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*fae548d3Szrj // GNU General Public License for more details. 17*fae548d3Szrj 18*fae548d3Szrj // You should have received a copy of the GNU General Public License 19*fae548d3Szrj // along with this program; if not, write to the Free Software 20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21*fae548d3Szrj // MA 02110-1301, USA. 22*fae548d3Szrj 23*fae548d3Szrj #ifndef GOLD_DWARF_READER_H 24*fae548d3Szrj #define GOLD_DWARF_READER_H 25*fae548d3Szrj 26*fae548d3Szrj #include <vector> 27*fae548d3Szrj #include <map> 28*fae548d3Szrj #include <limits.h> 29*fae548d3Szrj #include <sys/types.h> 30*fae548d3Szrj 31*fae548d3Szrj #include "elfcpp.h" 32*fae548d3Szrj #include "elfcpp_swap.h" 33*fae548d3Szrj #include "dwarf.h" 34*fae548d3Szrj #include "reloc.h" 35*fae548d3Szrj 36*fae548d3Szrj namespace gold 37*fae548d3Szrj { 38*fae548d3Szrj 39*fae548d3Szrj class Dwarf_info_reader; 40*fae548d3Szrj struct LineStateMachine; 41*fae548d3Szrj 42*fae548d3Szrj // This class is used to extract the section index and offset of 43*fae548d3Szrj // the target of a relocation for a given offset within the section. 44*fae548d3Szrj 45*fae548d3Szrj class Elf_reloc_mapper 46*fae548d3Szrj { 47*fae548d3Szrj public: Elf_reloc_mapper()48*fae548d3Szrj Elf_reloc_mapper() 49*fae548d3Szrj { } 50*fae548d3Szrj 51*fae548d3Szrj virtual ~Elf_reloc_mapper()52*fae548d3Szrj ~Elf_reloc_mapper() 53*fae548d3Szrj { } 54*fae548d3Szrj 55*fae548d3Szrj // Initialize the relocation tracker for section RELOC_SHNDX. 56*fae548d3Szrj bool initialize(unsigned int reloc_shndx,unsigned int reloc_type)57*fae548d3Szrj initialize(unsigned int reloc_shndx, unsigned int reloc_type) 58*fae548d3Szrj { return this->do_initialize(reloc_shndx, reloc_type); } 59*fae548d3Szrj 60*fae548d3Szrj // Return the next reloc_offset. 61*fae548d3Szrj off_t next_offset()62*fae548d3Szrj next_offset() 63*fae548d3Szrj { return this->do_next_offset(); } 64*fae548d3Szrj 65*fae548d3Szrj // Advance to the next relocation past OFFSET. 66*fae548d3Szrj void advance(off_t offset)67*fae548d3Szrj advance(off_t offset) 68*fae548d3Szrj { this->do_advance(offset); } 69*fae548d3Szrj 70*fae548d3Szrj // Return the section index and offset within the section of the target 71*fae548d3Szrj // of the relocation for RELOC_OFFSET in the referring section. 72*fae548d3Szrj unsigned int get_reloc_target(off_t reloc_offset,off_t * target_offset)73*fae548d3Szrj get_reloc_target(off_t reloc_offset, off_t* target_offset) 74*fae548d3Szrj { return this->do_get_reloc_target(reloc_offset, target_offset); } 75*fae548d3Szrj 76*fae548d3Szrj // Checkpoint the current position in the reloc section. 77*fae548d3Szrj uint64_t checkpoint()78*fae548d3Szrj checkpoint() const 79*fae548d3Szrj { return this->do_checkpoint(); } 80*fae548d3Szrj 81*fae548d3Szrj // Reset the current position to the CHECKPOINT. 82*fae548d3Szrj void reset(uint64_t checkpoint)83*fae548d3Szrj reset(uint64_t checkpoint) 84*fae548d3Szrj { this->do_reset(checkpoint); } 85*fae548d3Szrj 86*fae548d3Szrj protected: 87*fae548d3Szrj virtual bool 88*fae548d3Szrj do_initialize(unsigned int, unsigned int) = 0; 89*fae548d3Szrj 90*fae548d3Szrj // Return the next reloc_offset. 91*fae548d3Szrj virtual off_t 92*fae548d3Szrj do_next_offset() = 0; 93*fae548d3Szrj 94*fae548d3Szrj // Advance to the next relocation past OFFSET. 95*fae548d3Szrj virtual void 96*fae548d3Szrj do_advance(off_t offset) = 0; 97*fae548d3Szrj 98*fae548d3Szrj virtual unsigned int 99*fae548d3Szrj do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0; 100*fae548d3Szrj 101*fae548d3Szrj // Checkpoint the current position in the reloc section. 102*fae548d3Szrj virtual uint64_t 103*fae548d3Szrj do_checkpoint() const = 0; 104*fae548d3Szrj 105*fae548d3Szrj // Reset the current position to the CHECKPOINT. 106*fae548d3Szrj virtual void 107*fae548d3Szrj do_reset(uint64_t checkpoint) = 0; 108*fae548d3Szrj }; 109*fae548d3Szrj 110*fae548d3Szrj template<int size, bool big_endian> 111*fae548d3Szrj class Sized_elf_reloc_mapper : public Elf_reloc_mapper 112*fae548d3Szrj { 113*fae548d3Szrj public: Sized_elf_reloc_mapper(Object * object,const unsigned char * symtab,off_t symtab_size)114*fae548d3Szrj Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab, 115*fae548d3Szrj off_t symtab_size) 116*fae548d3Szrj : object_(object), symtab_(symtab), symtab_size_(symtab_size), 117*fae548d3Szrj reloc_type_(0), track_relocs_() 118*fae548d3Szrj { } 119*fae548d3Szrj 120*fae548d3Szrj protected: 121*fae548d3Szrj bool 122*fae548d3Szrj do_initialize(unsigned int reloc_shndx, unsigned int reloc_type); 123*fae548d3Szrj 124*fae548d3Szrj // Return the next reloc_offset. 125*fae548d3Szrj virtual off_t do_next_offset()126*fae548d3Szrj do_next_offset() 127*fae548d3Szrj { return this->track_relocs_.next_offset(); } 128*fae548d3Szrj 129*fae548d3Szrj // Advance to the next relocation past OFFSET. 130*fae548d3Szrj virtual void do_advance(off_t offset)131*fae548d3Szrj do_advance(off_t offset) 132*fae548d3Szrj { this->track_relocs_.advance(offset); } 133*fae548d3Szrj 134*fae548d3Szrj unsigned int 135*fae548d3Szrj do_get_reloc_target(off_t reloc_offset, off_t* target_offset); 136*fae548d3Szrj 137*fae548d3Szrj // Checkpoint the current position in the reloc section. 138*fae548d3Szrj uint64_t do_checkpoint()139*fae548d3Szrj do_checkpoint() const 140*fae548d3Szrj { return this->track_relocs_.checkpoint(); } 141*fae548d3Szrj 142*fae548d3Szrj // Reset the current position to the CHECKPOINT. 143*fae548d3Szrj void do_reset(uint64_t checkpoint)144*fae548d3Szrj do_reset(uint64_t checkpoint) 145*fae548d3Szrj { this->track_relocs_.reset(checkpoint); } 146*fae548d3Szrj 147*fae548d3Szrj private: 148*fae548d3Szrj typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 149*fae548d3Szrj 150*fae548d3Szrj // Return the section index of symbol SYMNDX, and copy its value to *VALUE. 151*fae548d3Szrj // Set *IS_ORDINARY true if the section index is an ordinary section index. 152*fae548d3Szrj unsigned int 153*fae548d3Szrj symbol_section(unsigned int symndx, Address* value, bool* is_ordinary); 154*fae548d3Szrj 155*fae548d3Szrj // The object file. 156*fae548d3Szrj Object* object_; 157*fae548d3Szrj // The ELF symbol table. 158*fae548d3Szrj const unsigned char* symtab_; 159*fae548d3Szrj // The size of the ELF symbol table. 160*fae548d3Szrj off_t symtab_size_; 161*fae548d3Szrj // Type of the relocation section (SHT_REL or SHT_RELA). 162*fae548d3Szrj unsigned int reloc_type_; 163*fae548d3Szrj // Relocations for the referring section. 164*fae548d3Szrj Track_relocs<size, big_endian> track_relocs_; 165*fae548d3Szrj }; 166*fae548d3Szrj 167*fae548d3Szrj // This class is used to read the abbreviations table from the 168*fae548d3Szrj // .debug_abbrev section of the object file. 169*fae548d3Szrj 170*fae548d3Szrj class Dwarf_abbrev_table 171*fae548d3Szrj { 172*fae548d3Szrj public: 173*fae548d3Szrj // An attribute list entry. 174*fae548d3Szrj struct Attribute 175*fae548d3Szrj { AttributeAttribute176*fae548d3Szrj Attribute(unsigned int a, unsigned int f) 177*fae548d3Szrj : attr(a), form(f) 178*fae548d3Szrj { } 179*fae548d3Szrj unsigned int attr; 180*fae548d3Szrj unsigned int form; 181*fae548d3Szrj }; 182*fae548d3Szrj 183*fae548d3Szrj // An abbrev code entry. 184*fae548d3Szrj struct Abbrev_code 185*fae548d3Szrj { Abbrev_codeAbbrev_code186*fae548d3Szrj Abbrev_code(unsigned int t, bool hc) 187*fae548d3Szrj : tag(t), has_children(hc), has_sibling_attribute(false), attributes() 188*fae548d3Szrj { 189*fae548d3Szrj this->attributes.reserve(10); 190*fae548d3Szrj } 191*fae548d3Szrj 192*fae548d3Szrj void add_attributeAbbrev_code193*fae548d3Szrj add_attribute(unsigned int attr, unsigned int form) 194*fae548d3Szrj { 195*fae548d3Szrj this->attributes.push_back(Attribute(attr, form)); 196*fae548d3Szrj } 197*fae548d3Szrj 198*fae548d3Szrj // The DWARF tag. 199*fae548d3Szrj unsigned int tag; 200*fae548d3Szrj // True if the DIE has children. 201*fae548d3Szrj bool has_children : 1; 202*fae548d3Szrj // True if the DIE has a sibling attribute. 203*fae548d3Szrj bool has_sibling_attribute : 1; 204*fae548d3Szrj // The list of attributes and forms. 205*fae548d3Szrj std::vector<Attribute> attributes; 206*fae548d3Szrj }; 207*fae548d3Szrj Dwarf_abbrev_table()208*fae548d3Szrj Dwarf_abbrev_table() 209*fae548d3Szrj : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL), 210*fae548d3Szrj owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_() 211*fae548d3Szrj { 212*fae548d3Szrj memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_)); 213*fae548d3Szrj } 214*fae548d3Szrj ~Dwarf_abbrev_table()215*fae548d3Szrj ~Dwarf_abbrev_table() 216*fae548d3Szrj { 217*fae548d3Szrj if (this->owns_buffer_ && this->buffer_ != NULL) 218*fae548d3Szrj delete[] this->buffer_; 219*fae548d3Szrj this->clear_abbrev_codes(); 220*fae548d3Szrj } 221*fae548d3Szrj 222*fae548d3Szrj // Read the abbrev table from an object file. 223*fae548d3Szrj bool read_abbrevs(Relobj * object,unsigned int abbrev_shndx,off_t abbrev_offset)224*fae548d3Szrj read_abbrevs(Relobj* object, 225*fae548d3Szrj unsigned int abbrev_shndx, 226*fae548d3Szrj off_t abbrev_offset) 227*fae548d3Szrj { 228*fae548d3Szrj // If we've already read this abbrev table, return immediately. 229*fae548d3Szrj if (this->abbrev_shndx_ > 0 230*fae548d3Szrj && this->abbrev_shndx_ == abbrev_shndx 231*fae548d3Szrj && this->abbrev_offset_ == abbrev_offset) 232*fae548d3Szrj return true; 233*fae548d3Szrj return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset); 234*fae548d3Szrj } 235*fae548d3Szrj 236*fae548d3Szrj // Return the abbrev code entry for CODE. This is a fast path for 237*fae548d3Szrj // abbrev codes that are in the direct lookup table. If not found 238*fae548d3Szrj // there, we call do_get_abbrev() to do the hard work. 239*fae548d3Szrj const Abbrev_code* get_abbrev(unsigned int code)240*fae548d3Szrj get_abbrev(unsigned int code) 241*fae548d3Szrj { 242*fae548d3Szrj if (code < this->low_abbrev_code_max_ 243*fae548d3Szrj && this->low_abbrev_codes_[code] != NULL) 244*fae548d3Szrj return this->low_abbrev_codes_[code]; 245*fae548d3Szrj return this->do_get_abbrev(code); 246*fae548d3Szrj } 247*fae548d3Szrj 248*fae548d3Szrj private: 249*fae548d3Szrj // Read the abbrev table from an object file. 250*fae548d3Szrj bool 251*fae548d3Szrj do_read_abbrevs(Relobj* object, 252*fae548d3Szrj unsigned int abbrev_shndx, 253*fae548d3Szrj off_t abbrev_offset); 254*fae548d3Szrj 255*fae548d3Szrj // Lookup the abbrev code entry for CODE. 256*fae548d3Szrj const Abbrev_code* 257*fae548d3Szrj do_get_abbrev(unsigned int code); 258*fae548d3Szrj 259*fae548d3Szrj // Store an abbrev code entry for CODE. 260*fae548d3Szrj void store_abbrev(unsigned int code,const Abbrev_code * entry)261*fae548d3Szrj store_abbrev(unsigned int code, const Abbrev_code* entry) 262*fae548d3Szrj { 263*fae548d3Szrj if (code < this->low_abbrev_code_max_) 264*fae548d3Szrj this->low_abbrev_codes_[code] = entry; 265*fae548d3Szrj else 266*fae548d3Szrj this->high_abbrev_codes_[code] = entry; 267*fae548d3Szrj } 268*fae548d3Szrj 269*fae548d3Szrj // Clear the abbrev code table and release the memory it uses. 270*fae548d3Szrj void 271*fae548d3Szrj clear_abbrev_codes(); 272*fae548d3Szrj 273*fae548d3Szrj typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table; 274*fae548d3Szrj 275*fae548d3Szrj // The section index of the current abbrev table. 276*fae548d3Szrj unsigned int abbrev_shndx_; 277*fae548d3Szrj // The offset within the section of the current abbrev table. 278*fae548d3Szrj off_t abbrev_offset_; 279*fae548d3Szrj // The buffer containing the .debug_abbrev section. 280*fae548d3Szrj const unsigned char* buffer_; 281*fae548d3Szrj const unsigned char* buffer_end_; 282*fae548d3Szrj // True if this object owns the buffer and needs to delete it. 283*fae548d3Szrj bool owns_buffer_; 284*fae548d3Szrj // Pointer to the current position in the buffer. 285*fae548d3Szrj const unsigned char* buffer_pos_; 286*fae548d3Szrj // The table of abbrev codes. 287*fae548d3Szrj // We use a direct-lookup array for low abbrev codes, 288*fae548d3Szrj // and store the rest in a hash table. 289*fae548d3Szrj static const unsigned int low_abbrev_code_max_ = 256; 290*fae548d3Szrj const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_]; 291*fae548d3Szrj Abbrev_code_table high_abbrev_codes_; 292*fae548d3Szrj }; 293*fae548d3Szrj 294*fae548d3Szrj // A DWARF range list. The start and end offsets are relative 295*fae548d3Szrj // to the input section SHNDX. Each range must lie entirely 296*fae548d3Szrj // within a single section. 297*fae548d3Szrj 298*fae548d3Szrj class Dwarf_range_list 299*fae548d3Szrj { 300*fae548d3Szrj public: 301*fae548d3Szrj struct Range 302*fae548d3Szrj { RangeRange303*fae548d3Szrj Range(unsigned int a_shndx, off_t a_start, off_t a_end) 304*fae548d3Szrj : shndx(a_shndx), start(a_start), end(a_end) 305*fae548d3Szrj { } 306*fae548d3Szrj 307*fae548d3Szrj unsigned int shndx; 308*fae548d3Szrj off_t start; 309*fae548d3Szrj off_t end; 310*fae548d3Szrj }; 311*fae548d3Szrj Dwarf_range_list()312*fae548d3Szrj Dwarf_range_list() 313*fae548d3Szrj : range_list_() 314*fae548d3Szrj { } 315*fae548d3Szrj 316*fae548d3Szrj void add(unsigned int shndx,off_t start,off_t end)317*fae548d3Szrj add(unsigned int shndx, off_t start, off_t end) 318*fae548d3Szrj { this->range_list_.push_back(Range(shndx, start, end)); } 319*fae548d3Szrj 320*fae548d3Szrj size_t size()321*fae548d3Szrj size() const 322*fae548d3Szrj { return this->range_list_.size(); } 323*fae548d3Szrj 324*fae548d3Szrj const Range& 325*fae548d3Szrj operator[](off_t i) const 326*fae548d3Szrj { return this->range_list_[i]; } 327*fae548d3Szrj 328*fae548d3Szrj private: 329*fae548d3Szrj std::vector<Range> range_list_; 330*fae548d3Szrj }; 331*fae548d3Szrj 332*fae548d3Szrj // This class is used to read the ranges table from the 333*fae548d3Szrj // .debug_ranges section of the object file. 334*fae548d3Szrj 335*fae548d3Szrj class Dwarf_ranges_table 336*fae548d3Szrj { 337*fae548d3Szrj public: Dwarf_ranges_table(Dwarf_info_reader * dwinfo)338*fae548d3Szrj Dwarf_ranges_table(Dwarf_info_reader* dwinfo) 339*fae548d3Szrj : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL), 340*fae548d3Szrj ranges_buffer_end_(NULL), owns_ranges_buffer_(false), 341*fae548d3Szrj ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0) 342*fae548d3Szrj { } 343*fae548d3Szrj ~Dwarf_ranges_table()344*fae548d3Szrj ~Dwarf_ranges_table() 345*fae548d3Szrj { 346*fae548d3Szrj if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL) 347*fae548d3Szrj delete[] this->ranges_buffer_; 348*fae548d3Szrj if (this->ranges_reloc_mapper_ != NULL) 349*fae548d3Szrj delete this->ranges_reloc_mapper_; 350*fae548d3Szrj } 351*fae548d3Szrj 352*fae548d3Szrj // Read the ranges table from an object file. 353*fae548d3Szrj bool 354*fae548d3Szrj read_ranges_table(Relobj* object, 355*fae548d3Szrj const unsigned char* symtab, 356*fae548d3Szrj off_t symtab_size, 357*fae548d3Szrj unsigned int ranges_shndx); 358*fae548d3Szrj 359*fae548d3Szrj // Read the range table from an object file. 360*fae548d3Szrj Dwarf_range_list* 361*fae548d3Szrj read_range_list(Relobj* object, 362*fae548d3Szrj const unsigned char* symtab, 363*fae548d3Szrj off_t symtab_size, 364*fae548d3Szrj unsigned int address_size, 365*fae548d3Szrj unsigned int ranges_shndx, 366*fae548d3Szrj off_t ranges_offset); 367*fae548d3Szrj 368*fae548d3Szrj // Look for a relocation at offset OFF in the range table, 369*fae548d3Szrj // and return the section index and offset of the target. 370*fae548d3Szrj unsigned int 371*fae548d3Szrj lookup_reloc(off_t off, off_t* target_off); 372*fae548d3Szrj 373*fae548d3Szrj private: 374*fae548d3Szrj // The Dwarf_info_reader, for reading data. 375*fae548d3Szrj Dwarf_info_reader* dwinfo_; 376*fae548d3Szrj // The section index of the ranges table. 377*fae548d3Szrj unsigned int ranges_shndx_; 378*fae548d3Szrj // The buffer containing the .debug_ranges section. 379*fae548d3Szrj const unsigned char* ranges_buffer_; 380*fae548d3Szrj const unsigned char* ranges_buffer_end_; 381*fae548d3Szrj // True if this object owns the buffer and needs to delete it. 382*fae548d3Szrj bool owns_ranges_buffer_; 383*fae548d3Szrj // Relocation mapper for the .debug_ranges section. 384*fae548d3Szrj Elf_reloc_mapper* ranges_reloc_mapper_; 385*fae548d3Szrj // Type of the relocation section (SHT_REL or SHT_RELA). 386*fae548d3Szrj unsigned int reloc_type_; 387*fae548d3Szrj // For incremental update links, this will hold the offset of the 388*fae548d3Szrj // input section within the output section. Offsets read from 389*fae548d3Szrj // relocated data will be relative to the output section, and need 390*fae548d3Szrj // to be corrected before reading data from the input section. 391*fae548d3Szrj uint64_t output_section_offset_; 392*fae548d3Szrj }; 393*fae548d3Szrj 394*fae548d3Szrj // This class is used to read the pubnames and pubtypes tables from the 395*fae548d3Szrj // .debug_pubnames and .debug_pubtypes sections of the object file. 396*fae548d3Szrj 397*fae548d3Szrj class Dwarf_pubnames_table 398*fae548d3Szrj { 399*fae548d3Szrj public: Dwarf_pubnames_table(Dwarf_info_reader * dwinfo,bool is_pubtypes)400*fae548d3Szrj Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes) 401*fae548d3Szrj : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false), 402*fae548d3Szrj offset_size_(0), pinfo_(NULL), end_of_table_(NULL), 403*fae548d3Szrj is_pubtypes_(is_pubtypes), is_gnu_style_(false), 404*fae548d3Szrj unit_length_(0), cu_offset_(0) 405*fae548d3Szrj { } 406*fae548d3Szrj ~Dwarf_pubnames_table()407*fae548d3Szrj ~Dwarf_pubnames_table() 408*fae548d3Szrj { 409*fae548d3Szrj if (this->owns_buffer_ && this->buffer_ != NULL) 410*fae548d3Szrj delete[] this->buffer_; 411*fae548d3Szrj } 412*fae548d3Szrj 413*fae548d3Szrj // Read the pubnames section from the object file, using the symbol 414*fae548d3Szrj // table for relocating it. 415*fae548d3Szrj bool 416*fae548d3Szrj read_section(Relobj* object, const unsigned char* symbol_table, 417*fae548d3Szrj off_t symtab_size); 418*fae548d3Szrj 419*fae548d3Szrj // Read the header for the set at OFFSET. 420*fae548d3Szrj bool 421*fae548d3Szrj read_header(off_t offset); 422*fae548d3Szrj 423*fae548d3Szrj // Return the offset to the cu within the info or types section. 424*fae548d3Szrj off_t cu_offset()425*fae548d3Szrj cu_offset() 426*fae548d3Szrj { return this->cu_offset_; } 427*fae548d3Szrj 428*fae548d3Szrj // Return the size of this subsection of the table. The unit length 429*fae548d3Szrj // doesn't include the size of its own field. 430*fae548d3Szrj off_t subsection_size()431*fae548d3Szrj subsection_size() 432*fae548d3Szrj { return this->unit_length_; } 433*fae548d3Szrj 434*fae548d3Szrj // Read the next name from the set. If the pubname table is gnu-style, 435*fae548d3Szrj // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index. 436*fae548d3Szrj const char* 437*fae548d3Szrj next_name(uint8_t* flag_byte); 438*fae548d3Szrj 439*fae548d3Szrj private: 440*fae548d3Szrj // The Dwarf_info_reader, for reading data. 441*fae548d3Szrj Dwarf_info_reader* dwinfo_; 442*fae548d3Szrj // The buffer containing the .debug_ranges section. 443*fae548d3Szrj const unsigned char* buffer_; 444*fae548d3Szrj const unsigned char* buffer_end_; 445*fae548d3Szrj // True if this object owns the buffer and needs to delete it. 446*fae548d3Szrj bool owns_buffer_; 447*fae548d3Szrj // The size of a DWARF offset for the current set. 448*fae548d3Szrj unsigned int offset_size_; 449*fae548d3Szrj // The current position within the buffer. 450*fae548d3Szrj const unsigned char* pinfo_; 451*fae548d3Szrj // The end of the current pubnames table. 452*fae548d3Szrj const unsigned char* end_of_table_; 453*fae548d3Szrj // TRUE if this is a .debug_pubtypes section. 454*fae548d3Szrj bool is_pubtypes_; 455*fae548d3Szrj // Gnu-style pubnames table. This style has an extra flag byte between the 456*fae548d3Szrj // offset and the name, and is used for generating version 7 of gdb-index. 457*fae548d3Szrj bool is_gnu_style_; 458*fae548d3Szrj // Fields read from the header. 459*fae548d3Szrj uint64_t unit_length_; 460*fae548d3Szrj off_t cu_offset_; 461*fae548d3Szrj 462*fae548d3Szrj // Track relocations for this table so we can find the CUs that 463*fae548d3Szrj // correspond to the subsections. 464*fae548d3Szrj Elf_reloc_mapper* reloc_mapper_; 465*fae548d3Szrj // Type of the relocation section (SHT_REL or SHT_RELA). 466*fae548d3Szrj unsigned int reloc_type_; 467*fae548d3Szrj }; 468*fae548d3Szrj 469*fae548d3Szrj // This class represents a DWARF Debug Info Entry (DIE). 470*fae548d3Szrj 471*fae548d3Szrj class Dwarf_die 472*fae548d3Szrj { 473*fae548d3Szrj public: 474*fae548d3Szrj // An attribute value. 475*fae548d3Szrj struct Attribute_value 476*fae548d3Szrj { 477*fae548d3Szrj unsigned int attr; 478*fae548d3Szrj unsigned int form; 479*fae548d3Szrj union 480*fae548d3Szrj { 481*fae548d3Szrj int64_t intval; 482*fae548d3Szrj uint64_t uintval; 483*fae548d3Szrj const char* stringval; 484*fae548d3Szrj const unsigned char* blockval; 485*fae548d3Szrj off_t refval; 486*fae548d3Szrj } val; 487*fae548d3Szrj union 488*fae548d3Szrj { 489*fae548d3Szrj // Section index for reference forms. 490*fae548d3Szrj unsigned int shndx; 491*fae548d3Szrj // Block length for block forms. 492*fae548d3Szrj unsigned int blocklen; 493*fae548d3Szrj // Attribute offset for DW_FORM_strp. 494*fae548d3Szrj unsigned int attr_off; 495*fae548d3Szrj } aux; 496*fae548d3Szrj }; 497*fae548d3Szrj 498*fae548d3Szrj // A list of attribute values. 499*fae548d3Szrj typedef std::vector<Attribute_value> Attributes; 500*fae548d3Szrj 501*fae548d3Szrj Dwarf_die(Dwarf_info_reader* dwinfo, 502*fae548d3Szrj off_t die_offset, 503*fae548d3Szrj Dwarf_die* parent); 504*fae548d3Szrj 505*fae548d3Szrj // Return the DWARF tag for this DIE. 506*fae548d3Szrj unsigned int tag()507*fae548d3Szrj tag() const 508*fae548d3Szrj { 509*fae548d3Szrj if (this->abbrev_code_ == NULL) 510*fae548d3Szrj return 0; 511*fae548d3Szrj return this->abbrev_code_->tag; 512*fae548d3Szrj } 513*fae548d3Szrj 514*fae548d3Szrj // Return true if this DIE has children. 515*fae548d3Szrj bool has_children()516*fae548d3Szrj has_children() const 517*fae548d3Szrj { 518*fae548d3Szrj gold_assert(this->abbrev_code_ != NULL); 519*fae548d3Szrj return this->abbrev_code_->has_children; 520*fae548d3Szrj } 521*fae548d3Szrj 522*fae548d3Szrj // Return true if this DIE has a sibling attribute. 523*fae548d3Szrj bool has_sibling_attribute()524*fae548d3Szrj has_sibling_attribute() const 525*fae548d3Szrj { 526*fae548d3Szrj gold_assert(this->abbrev_code_ != NULL); 527*fae548d3Szrj return this->abbrev_code_->has_sibling_attribute; 528*fae548d3Szrj } 529*fae548d3Szrj 530*fae548d3Szrj // Return the value of attribute ATTR. 531*fae548d3Szrj const Attribute_value* 532*fae548d3Szrj attribute(unsigned int attr); 533*fae548d3Szrj 534*fae548d3Szrj // Return the value of the DW_AT_name attribute. 535*fae548d3Szrj const char* name()536*fae548d3Szrj name() 537*fae548d3Szrj { 538*fae548d3Szrj if (this->name_ == NULL) 539*fae548d3Szrj this->set_name(); 540*fae548d3Szrj return this->name_; 541*fae548d3Szrj } 542*fae548d3Szrj 543*fae548d3Szrj // Return the value of the DW_AT_linkage_name 544*fae548d3Szrj // or DW_AT_MIPS_linkage_name attribute. 545*fae548d3Szrj const char* linkage_name()546*fae548d3Szrj linkage_name() 547*fae548d3Szrj { 548*fae548d3Szrj if (this->linkage_name_ == NULL) 549*fae548d3Szrj this->set_linkage_name(); 550*fae548d3Szrj return this->linkage_name_; 551*fae548d3Szrj } 552*fae548d3Szrj 553*fae548d3Szrj // Return the value of the DW_AT_specification attribute. 554*fae548d3Szrj off_t specification()555*fae548d3Szrj specification() 556*fae548d3Szrj { 557*fae548d3Szrj if (!this->attributes_read_) 558*fae548d3Szrj this->read_attributes(); 559*fae548d3Szrj return this->specification_; 560*fae548d3Szrj } 561*fae548d3Szrj 562*fae548d3Szrj // Return the value of the DW_AT_abstract_origin attribute. 563*fae548d3Szrj off_t abstract_origin()564*fae548d3Szrj abstract_origin() 565*fae548d3Szrj { 566*fae548d3Szrj if (!this->attributes_read_) 567*fae548d3Szrj this->read_attributes(); 568*fae548d3Szrj return this->abstract_origin_; 569*fae548d3Szrj } 570*fae548d3Szrj 571*fae548d3Szrj // Return the value of attribute ATTR as a string. 572*fae548d3Szrj const char* 573*fae548d3Szrj string_attribute(unsigned int attr); 574*fae548d3Szrj 575*fae548d3Szrj // Return the value of attribute ATTR as an integer. 576*fae548d3Szrj int64_t 577*fae548d3Szrj int_attribute(unsigned int attr); 578*fae548d3Szrj 579*fae548d3Szrj // Return the value of attribute ATTR as an unsigned integer. 580*fae548d3Szrj uint64_t 581*fae548d3Szrj uint_attribute(unsigned int attr); 582*fae548d3Szrj 583*fae548d3Szrj // Return the value of attribute ATTR as a reference. 584*fae548d3Szrj off_t 585*fae548d3Szrj ref_attribute(unsigned int attr, unsigned int* shndx); 586*fae548d3Szrj 587*fae548d3Szrj // Return the value of attribute ATTR as a address. 588*fae548d3Szrj off_t 589*fae548d3Szrj address_attribute(unsigned int attr, unsigned int* shndx); 590*fae548d3Szrj 591*fae548d3Szrj // Return the value of attribute ATTR as a flag. 592*fae548d3Szrj bool flag_attribute(unsigned int attr)593*fae548d3Szrj flag_attribute(unsigned int attr) 594*fae548d3Szrj { return this->int_attribute(attr) != 0; } 595*fae548d3Szrj 596*fae548d3Szrj // Return true if this DIE is a declaration. 597*fae548d3Szrj bool is_declaration()598*fae548d3Szrj is_declaration() 599*fae548d3Szrj { return this->flag_attribute(elfcpp::DW_AT_declaration); } 600*fae548d3Szrj 601*fae548d3Szrj // Return the parent of this DIE. 602*fae548d3Szrj Dwarf_die* parent()603*fae548d3Szrj parent() const 604*fae548d3Szrj { return this->parent_; } 605*fae548d3Szrj 606*fae548d3Szrj // Return the offset of this DIE. 607*fae548d3Szrj off_t offset()608*fae548d3Szrj offset() const 609*fae548d3Szrj { return this->die_offset_; } 610*fae548d3Szrj 611*fae548d3Szrj // Return the offset of this DIE's first child. 612*fae548d3Szrj off_t 613*fae548d3Szrj child_offset(); 614*fae548d3Szrj 615*fae548d3Szrj // Set the offset of this DIE's next sibling. 616*fae548d3Szrj void set_sibling_offset(off_t sibling_offset)617*fae548d3Szrj set_sibling_offset(off_t sibling_offset) 618*fae548d3Szrj { this->sibling_offset_ = sibling_offset; } 619*fae548d3Szrj 620*fae548d3Szrj // Return the offset of this DIE's next sibling. 621*fae548d3Szrj off_t 622*fae548d3Szrj sibling_offset(); 623*fae548d3Szrj 624*fae548d3Szrj private: 625*fae548d3Szrj typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code; 626*fae548d3Szrj 627*fae548d3Szrj // Read all the attributes of the DIE. 628*fae548d3Szrj bool 629*fae548d3Szrj read_attributes(); 630*fae548d3Szrj 631*fae548d3Szrj // Set the name of the DIE if present. 632*fae548d3Szrj void 633*fae548d3Szrj set_name(); 634*fae548d3Szrj 635*fae548d3Szrj // Set the linkage name if present. 636*fae548d3Szrj void 637*fae548d3Szrj set_linkage_name(); 638*fae548d3Szrj 639*fae548d3Szrj // Skip all the attributes of the DIE and return the offset 640*fae548d3Szrj // of the next DIE. 641*fae548d3Szrj off_t 642*fae548d3Szrj skip_attributes(); 643*fae548d3Szrj 644*fae548d3Szrj // The Dwarf_info_reader, for reading attributes. 645*fae548d3Szrj Dwarf_info_reader* dwinfo_; 646*fae548d3Szrj // The parent of this DIE. 647*fae548d3Szrj Dwarf_die* parent_; 648*fae548d3Szrj // Offset of this DIE within its compilation unit. 649*fae548d3Szrj off_t die_offset_; 650*fae548d3Szrj // Offset of the first attribute, relative to the beginning of the DIE. 651*fae548d3Szrj off_t attr_offset_; 652*fae548d3Szrj // Offset of the first child, relative to the compilation unit. 653*fae548d3Szrj off_t child_offset_; 654*fae548d3Szrj // Offset of the next sibling, relative to the compilation unit. 655*fae548d3Szrj off_t sibling_offset_; 656*fae548d3Szrj // The abbreviation table entry. 657*fae548d3Szrj const Abbrev_code* abbrev_code_; 658*fae548d3Szrj // The list of attributes. 659*fae548d3Szrj Attributes attributes_; 660*fae548d3Szrj // True if the attributes have been read. 661*fae548d3Szrj bool attributes_read_; 662*fae548d3Szrj // The following fields hold common attributes to avoid a linear 663*fae548d3Szrj // search through the attribute list. 664*fae548d3Szrj // The DIE name (DW_AT_name). 665*fae548d3Szrj const char* name_; 666*fae548d3Szrj // Offset of the name in the string table (for DW_FORM_strp). 667*fae548d3Szrj off_t name_off_; 668*fae548d3Szrj // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name). 669*fae548d3Szrj const char* linkage_name_; 670*fae548d3Szrj // Offset of the linkage name in the string table (for DW_FORM_strp). 671*fae548d3Szrj off_t linkage_name_off_; 672*fae548d3Szrj // Section index of the string table (for DW_FORM_strp). 673*fae548d3Szrj unsigned int string_shndx_; 674*fae548d3Szrj // The value of a DW_AT_specification attribute. 675*fae548d3Szrj off_t specification_; 676*fae548d3Szrj // The value of a DW_AT_abstract_origin attribute. 677*fae548d3Szrj off_t abstract_origin_; 678*fae548d3Szrj }; 679*fae548d3Szrj 680*fae548d3Szrj // This class is used to read the debug info from the .debug_info 681*fae548d3Szrj // or .debug_types sections. This is a base class that implements 682*fae548d3Szrj // the generic parsing of the compilation unit header and DIE 683*fae548d3Szrj // structure. The parse() method parses the entire section, and 684*fae548d3Szrj // calls the various visit_xxx() methods for each header. Clients 685*fae548d3Szrj // should derive a new class from this one and implement the 686*fae548d3Szrj // visit_compilation_unit() and visit_type_unit() functions. 687*fae548d3Szrj 688*fae548d3Szrj class Dwarf_info_reader 689*fae548d3Szrj { 690*fae548d3Szrj public: Dwarf_info_reader(bool is_type_unit,Relobj * object,const unsigned char * symtab,off_t symtab_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type)691*fae548d3Szrj Dwarf_info_reader(bool is_type_unit, 692*fae548d3Szrj Relobj* object, 693*fae548d3Szrj const unsigned char* symtab, 694*fae548d3Szrj off_t symtab_size, 695*fae548d3Szrj unsigned int shndx, 696*fae548d3Szrj unsigned int reloc_shndx, 697*fae548d3Szrj unsigned int reloc_type) 698*fae548d3Szrj : is_type_unit_(is_type_unit), object_(object), symtab_(symtab), 699*fae548d3Szrj symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx), 700*fae548d3Szrj reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0), 701*fae548d3Szrj buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0), 702*fae548d3Szrj offset_size_(0), address_size_(0), cu_version_(0), 703*fae548d3Szrj abbrev_table_(), ranges_table_(this), 704*fae548d3Szrj reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL), 705*fae548d3Szrj owns_string_buffer_(false), string_output_section_offset_(0) 706*fae548d3Szrj { } 707*fae548d3Szrj 708*fae548d3Szrj virtual ~Dwarf_info_reader()709*fae548d3Szrj ~Dwarf_info_reader() 710*fae548d3Szrj { 711*fae548d3Szrj if (this->reloc_mapper_ != NULL) 712*fae548d3Szrj delete this->reloc_mapper_; 713*fae548d3Szrj if (this->owns_string_buffer_ && this->string_buffer_ != NULL) 714*fae548d3Szrj delete[] this->string_buffer_; 715*fae548d3Szrj } 716*fae548d3Szrj 717*fae548d3Szrj // Begin parsing the debug info. This calls visit_compilation_unit() 718*fae548d3Szrj // or visit_type_unit() for each compilation or type unit found in the 719*fae548d3Szrj // section, and visit_die() for each top-level DIE. 720*fae548d3Szrj void 721*fae548d3Szrj parse(); 722*fae548d3Szrj 723*fae548d3Szrj // Return the abbrev code entry for a CODE. 724*fae548d3Szrj const Dwarf_abbrev_table::Abbrev_code* get_abbrev(unsigned int code)725*fae548d3Szrj get_abbrev(unsigned int code) 726*fae548d3Szrj { return this->abbrev_table_.get_abbrev(code); } 727*fae548d3Szrj 728*fae548d3Szrj // Return a pointer to the DWARF info buffer at OFFSET. 729*fae548d3Szrj const unsigned char* buffer_at_offset(off_t offset)730*fae548d3Szrj buffer_at_offset(off_t offset) const 731*fae548d3Szrj { 732*fae548d3Szrj const unsigned char* p = this->buffer_ + this->cu_offset_ + offset; 733*fae548d3Szrj if (this->check_buffer(p + 1)) 734*fae548d3Szrj return p; 735*fae548d3Szrj return NULL; 736*fae548d3Szrj } 737*fae548d3Szrj 738*fae548d3Szrj // Read a possibly unaligned integer of SIZE. 739*fae548d3Szrj template <int valsize> 740*fae548d3Szrj inline typename elfcpp::Valtype_base<valsize>::Valtype 741*fae548d3Szrj read_from_pointer(const unsigned char* source); 742*fae548d3Szrj 743*fae548d3Szrj // Read a possibly unaligned integer of SIZE. Update SOURCE after read. 744*fae548d3Szrj template <int valsize> 745*fae548d3Szrj inline typename elfcpp::Valtype_base<valsize>::Valtype 746*fae548d3Szrj read_from_pointer(const unsigned char** source); 747*fae548d3Szrj 748*fae548d3Szrj // Look for a relocation at offset ATTR_OFF in the dwarf info, 749*fae548d3Szrj // and return the section index and offset of the target. 750*fae548d3Szrj unsigned int 751*fae548d3Szrj lookup_reloc(off_t attr_off, off_t* target_off); 752*fae548d3Szrj 753*fae548d3Szrj // Return a string from the DWARF string table. 754*fae548d3Szrj const char* 755*fae548d3Szrj get_string(off_t str_off, unsigned int string_shndx); 756*fae548d3Szrj 757*fae548d3Szrj // Return the size of a DWARF offset. 758*fae548d3Szrj unsigned int offset_size()759*fae548d3Szrj offset_size() const 760*fae548d3Szrj { return this->offset_size_; } 761*fae548d3Szrj 762*fae548d3Szrj // Return the size of an address. 763*fae548d3Szrj unsigned int address_size()764*fae548d3Szrj address_size() const 765*fae548d3Szrj { return this->address_size_; } 766*fae548d3Szrj 767*fae548d3Szrj // Return the size of a DW_FORM_ref_addr. 768*fae548d3Szrj // In DWARF v2, this was the size of an address; in DWARF v3 and later, 769*fae548d3Szrj // it is the size of an DWARF offset. 770*fae548d3Szrj unsigned int ref_addr_size()771*fae548d3Szrj ref_addr_size() const 772*fae548d3Szrj { return this->cu_version_ > 2 ? this->offset_size_ : this->address_size_; } 773*fae548d3Szrj 774*fae548d3Szrj // Set the section index of the .debug_abbrev section. 775*fae548d3Szrj // We use this if there are no relocations for the .debug_info section. 776*fae548d3Szrj // If not set, the code parse() routine will search for the section by name. 777*fae548d3Szrj void set_abbrev_shndx(unsigned int abbrev_shndx)778*fae548d3Szrj set_abbrev_shndx(unsigned int abbrev_shndx) 779*fae548d3Szrj { this->abbrev_shndx_ = abbrev_shndx; } 780*fae548d3Szrj 781*fae548d3Szrj // Return a pointer to the object file's ELF symbol table. 782*fae548d3Szrj const unsigned char* symtab()783*fae548d3Szrj symtab() const 784*fae548d3Szrj { return this->symtab_; } 785*fae548d3Szrj 786*fae548d3Szrj // Return the size of the object file's ELF symbol table. 787*fae548d3Szrj off_t symtab_size()788*fae548d3Szrj symtab_size() const 789*fae548d3Szrj { return this->symtab_size_; } 790*fae548d3Szrj 791*fae548d3Szrj // Return the offset of the current compilation unit. 792*fae548d3Szrj off_t cu_offset()793*fae548d3Szrj cu_offset() const 794*fae548d3Szrj { return this->cu_offset_; } 795*fae548d3Szrj 796*fae548d3Szrj protected: 797*fae548d3Szrj // Begin parsing the debug info. This calls visit_compilation_unit() 798*fae548d3Szrj // or visit_type_unit() for each compilation or type unit found in the 799*fae548d3Szrj // section, and visit_die() for each top-level DIE. 800*fae548d3Szrj template<bool big_endian> 801*fae548d3Szrj void 802*fae548d3Szrj do_parse(); 803*fae548d3Szrj 804*fae548d3Szrj // The following methods are hooks that are meant to be implemented 805*fae548d3Szrj // by a derived class. A default, do-nothing, implementation of 806*fae548d3Szrj // each is provided for this base class. 807*fae548d3Szrj 808*fae548d3Szrj // Visit a compilation unit. 809*fae548d3Szrj virtual void 810*fae548d3Szrj visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die); 811*fae548d3Szrj 812*fae548d3Szrj // Visit a type unit. 813*fae548d3Szrj virtual void 814*fae548d3Szrj visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset, 815*fae548d3Szrj uint64_t signature, Dwarf_die* root_die); 816*fae548d3Szrj 817*fae548d3Szrj // Read the range table. 818*fae548d3Szrj Dwarf_range_list* read_range_list(unsigned int ranges_shndx,off_t ranges_offset)819*fae548d3Szrj read_range_list(unsigned int ranges_shndx, off_t ranges_offset) 820*fae548d3Szrj { 821*fae548d3Szrj return this->ranges_table_.read_range_list(this->object_, 822*fae548d3Szrj this->symtab_, 823*fae548d3Szrj this->symtab_size_, 824*fae548d3Szrj this->address_size_, 825*fae548d3Szrj ranges_shndx, 826*fae548d3Szrj ranges_offset); 827*fae548d3Szrj } 828*fae548d3Szrj 829*fae548d3Szrj // Return the object. 830*fae548d3Szrj Relobj* object()831*fae548d3Szrj object() const 832*fae548d3Szrj { return this->object_; } 833*fae548d3Szrj 834*fae548d3Szrj // Checkpoint the relocation tracker. 835*fae548d3Szrj uint64_t get_reloc_checkpoint()836*fae548d3Szrj get_reloc_checkpoint() const 837*fae548d3Szrj { return this->reloc_mapper_->checkpoint(); } 838*fae548d3Szrj 839*fae548d3Szrj // Reset the relocation tracker to the CHECKPOINT. 840*fae548d3Szrj void reset_relocs(uint64_t checkpoint)841*fae548d3Szrj reset_relocs(uint64_t checkpoint) 842*fae548d3Szrj { this->reloc_mapper_->reset(checkpoint); } 843*fae548d3Szrj 844*fae548d3Szrj private: 845*fae548d3Szrj // Print a warning about a corrupt debug section. 846*fae548d3Szrj void 847*fae548d3Szrj warn_corrupt_debug_section() const; 848*fae548d3Szrj 849*fae548d3Szrj // Check that P is within the bounds of the current section. 850*fae548d3Szrj bool check_buffer(const unsigned char * p)851*fae548d3Szrj check_buffer(const unsigned char* p) const 852*fae548d3Szrj { 853*fae548d3Szrj if (p > this->buffer_ + this->cu_offset_ + this->cu_length_) 854*fae548d3Szrj { 855*fae548d3Szrj this->warn_corrupt_debug_section(); 856*fae548d3Szrj return false; 857*fae548d3Szrj } 858*fae548d3Szrj return true; 859*fae548d3Szrj } 860*fae548d3Szrj 861*fae548d3Szrj // Read the DWARF string table. 862*fae548d3Szrj bool read_string_table(unsigned int string_shndx)863*fae548d3Szrj read_string_table(unsigned int string_shndx) 864*fae548d3Szrj { 865*fae548d3Szrj // If we've already read this string table, return immediately. 866*fae548d3Szrj if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx) 867*fae548d3Szrj return true; 868*fae548d3Szrj if (string_shndx == 0 && this->string_shndx_ > 0) 869*fae548d3Szrj return true; 870*fae548d3Szrj return this->do_read_string_table(string_shndx); 871*fae548d3Szrj } 872*fae548d3Szrj 873*fae548d3Szrj bool 874*fae548d3Szrj do_read_string_table(unsigned int string_shndx); 875*fae548d3Szrj 876*fae548d3Szrj // True if this is a type unit; false for a compilation unit. 877*fae548d3Szrj bool is_type_unit_; 878*fae548d3Szrj // The object containing the .debug_info or .debug_types input section. 879*fae548d3Szrj Relobj* object_; 880*fae548d3Szrj // The ELF symbol table. 881*fae548d3Szrj const unsigned char* symtab_; 882*fae548d3Szrj // The size of the ELF symbol table. 883*fae548d3Szrj off_t symtab_size_; 884*fae548d3Szrj // Index of the .debug_info or .debug_types section. 885*fae548d3Szrj unsigned int shndx_; 886*fae548d3Szrj // Index of the relocation section. 887*fae548d3Szrj unsigned int reloc_shndx_; 888*fae548d3Szrj // Type of the relocation section (SHT_REL or SHT_RELA). 889*fae548d3Szrj unsigned int reloc_type_; 890*fae548d3Szrj // Index of the .debug_abbrev section (0 if not known). 891*fae548d3Szrj unsigned int abbrev_shndx_; 892*fae548d3Szrj // Index of the .debug_str section. 893*fae548d3Szrj unsigned int string_shndx_; 894*fae548d3Szrj // The buffer for the debug info. 895*fae548d3Szrj const unsigned char* buffer_; 896*fae548d3Szrj const unsigned char* buffer_end_; 897*fae548d3Szrj // Offset of the current compilation unit. 898*fae548d3Szrj off_t cu_offset_; 899*fae548d3Szrj // Length of the current compilation unit. 900*fae548d3Szrj off_t cu_length_; 901*fae548d3Szrj // Size of a DWARF offset for the current compilation unit. 902*fae548d3Szrj unsigned int offset_size_; 903*fae548d3Szrj // Size of an address for the target architecture. 904*fae548d3Szrj unsigned int address_size_; 905*fae548d3Szrj // Compilation unit version number. 906*fae548d3Szrj unsigned int cu_version_; 907*fae548d3Szrj // Abbreviations table for current compilation unit. 908*fae548d3Szrj Dwarf_abbrev_table abbrev_table_; 909*fae548d3Szrj // Ranges table for the current compilation unit. 910*fae548d3Szrj Dwarf_ranges_table ranges_table_; 911*fae548d3Szrj // Relocation mapper for the section. 912*fae548d3Szrj Elf_reloc_mapper* reloc_mapper_; 913*fae548d3Szrj // The buffer for the debug string table. 914*fae548d3Szrj const char* string_buffer_; 915*fae548d3Szrj const char* string_buffer_end_; 916*fae548d3Szrj // True if this object owns the buffer and needs to delete it. 917*fae548d3Szrj bool owns_string_buffer_; 918*fae548d3Szrj // For incremental update links, this will hold the offset of the 919*fae548d3Szrj // input .debug_str section within the output section. Offsets read 920*fae548d3Szrj // from relocated data will be relative to the output section, and need 921*fae548d3Szrj // to be corrected before reading data from the input section. 922*fae548d3Szrj uint64_t string_output_section_offset_; 923*fae548d3Szrj }; 924*fae548d3Szrj 925*fae548d3Szrj // We can't do better than to keep the offsets in a sorted vector. 926*fae548d3Szrj // Here, offset is the key, and file_num/line_num is the value. 927*fae548d3Szrj struct Offset_to_lineno_entry 928*fae548d3Szrj { 929*fae548d3Szrj off_t offset; 930*fae548d3Szrj int header_num; // which file-list to use (i.e. which .o file are we in) 931*fae548d3Szrj // A pointer into files_. 932*fae548d3Szrj unsigned int file_num : sizeof(int) * CHAR_BIT - 1; 933*fae548d3Szrj // True if this was the last entry for the current offset, meaning 934*fae548d3Szrj // it's the line that actually applies. 935*fae548d3Szrj unsigned int last_line_for_offset : 1; 936*fae548d3Szrj // The line number in the source file. -1 to indicate end-of-function. 937*fae548d3Szrj int line_num; 938*fae548d3Szrj 939*fae548d3Szrj // This sorts by offsets first, and then puts the correct line to 940*fae548d3Szrj // report for a given offset at the beginning of the run of equal 941*fae548d3Szrj // offsets (so that asking for 1 line gives the best answer). This 942*fae548d3Szrj // is not a total ordering. 943*fae548d3Szrj bool operator<(const Offset_to_lineno_entry& that) const 944*fae548d3Szrj { 945*fae548d3Szrj if (this->offset != that.offset) 946*fae548d3Szrj return this->offset < that.offset; 947*fae548d3Szrj // Note the '>' which makes this sort 'true' first. 948*fae548d3Szrj return this->last_line_for_offset > that.last_line_for_offset; 949*fae548d3Szrj } 950*fae548d3Szrj }; 951*fae548d3Szrj 952*fae548d3Szrj // This class is used to read the line information from the debugging 953*fae548d3Szrj // section of an object file. 954*fae548d3Szrj 955*fae548d3Szrj class Dwarf_line_info 956*fae548d3Szrj { 957*fae548d3Szrj public: Dwarf_line_info()958*fae548d3Szrj Dwarf_line_info() 959*fae548d3Szrj { } 960*fae548d3Szrj 961*fae548d3Szrj virtual ~Dwarf_line_info()962*fae548d3Szrj ~Dwarf_line_info() 963*fae548d3Szrj { } 964*fae548d3Szrj 965*fae548d3Szrj // Given a section number and an offset, returns the associated 966*fae548d3Szrj // file and line-number, as a string: "file:lineno". If unable 967*fae548d3Szrj // to do the mapping, returns the empty string. You must call 968*fae548d3Szrj // read_line_mappings() before calling this function. If 969*fae548d3Szrj // 'other_lines' is non-NULL, fills that in with other line 970*fae548d3Szrj // numbers assigned to the same offset. 971*fae548d3Szrj std::string addr2line(unsigned int shndx,off_t offset,std::vector<std::string> * other_lines)972*fae548d3Szrj addr2line(unsigned int shndx, off_t offset, 973*fae548d3Szrj std::vector<std::string>* other_lines) 974*fae548d3Szrj { return this->do_addr2line(shndx, offset, other_lines); } 975*fae548d3Szrj 976*fae548d3Szrj // A helper function for a single addr2line lookup. It also keeps a 977*fae548d3Szrj // cache of the last CACHE_SIZE Dwarf_line_info objects it created; 978*fae548d3Szrj // set to 0 not to cache at all. The larger CACHE_SIZE is, the more 979*fae548d3Szrj // chance this routine won't have to re-create a Dwarf_line_info 980*fae548d3Szrj // object for its addr2line computation; such creations are slow. 981*fae548d3Szrj // NOTE: Not thread-safe, so only call from one thread at a time. 982*fae548d3Szrj static std::string 983*fae548d3Szrj one_addr2line(Object* object, unsigned int shndx, off_t offset, 984*fae548d3Szrj size_t cache_size, std::vector<std::string>* other_lines); 985*fae548d3Szrj 986*fae548d3Szrj // This reclaims all the memory that one_addr2line may have cached. 987*fae548d3Szrj // Use this when you know you will not be calling one_addr2line again. 988*fae548d3Szrj static void 989*fae548d3Szrj clear_addr2line_cache(); 990*fae548d3Szrj 991*fae548d3Szrj private: 992*fae548d3Szrj virtual std::string 993*fae548d3Szrj do_addr2line(unsigned int shndx, off_t offset, 994*fae548d3Szrj std::vector<std::string>* other_lines) = 0; 995*fae548d3Szrj }; 996*fae548d3Szrj 997*fae548d3Szrj template<int size, bool big_endian> 998*fae548d3Szrj class Sized_dwarf_line_info : public Dwarf_line_info 999*fae548d3Szrj { 1000*fae548d3Szrj public: 1001*fae548d3Szrj // Initializes a .debug_line reader for a given object file. 1002*fae548d3Szrj // If SHNDX is specified and non-negative, only read the debug 1003*fae548d3Szrj // information that pertains to the specified section. 1004*fae548d3Szrj Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U); 1005*fae548d3Szrj 1006*fae548d3Szrj virtual ~Sized_dwarf_line_info()1007*fae548d3Szrj ~Sized_dwarf_line_info() 1008*fae548d3Szrj { 1009*fae548d3Szrj if (this->buffer_start_ != NULL) 1010*fae548d3Szrj delete[] this->buffer_start_; 1011*fae548d3Szrj } 1012*fae548d3Szrj 1013*fae548d3Szrj private: 1014*fae548d3Szrj std::string 1015*fae548d3Szrj do_addr2line(unsigned int shndx, off_t offset, 1016*fae548d3Szrj std::vector<std::string>* other_lines); 1017*fae548d3Szrj 1018*fae548d3Szrj // Formats a file and line number to a string like "dirname/filename:lineno". 1019*fae548d3Szrj std::string 1020*fae548d3Szrj format_file_lineno(const Offset_to_lineno_entry& lineno) const; 1021*fae548d3Szrj 1022*fae548d3Szrj // Start processing line info, and populates the offset_map_. 1023*fae548d3Szrj // If SHNDX is non-negative, only store debug information that 1024*fae548d3Szrj // pertains to the specified section. 1025*fae548d3Szrj void 1026*fae548d3Szrj read_line_mappings(unsigned int shndx); 1027*fae548d3Szrj 1028*fae548d3Szrj // Reads the relocation section associated with .debug_line and 1029*fae548d3Szrj // stores relocation information in reloc_map_. 1030*fae548d3Szrj void 1031*fae548d3Szrj read_relocs(); 1032*fae548d3Szrj 1033*fae548d3Szrj // Reads the DWARF2/3 header for this line info. Each takes as input 1034*fae548d3Szrj // a starting buffer position, and returns the ending position. 1035*fae548d3Szrj const unsigned char* 1036*fae548d3Szrj read_header_prolog(const unsigned char* lineptr); 1037*fae548d3Szrj 1038*fae548d3Szrj const unsigned char* 1039*fae548d3Szrj read_header_tables(const unsigned char* lineptr); 1040*fae548d3Szrj 1041*fae548d3Szrj // Reads the DWARF2/3 line information. If shndx is non-negative, 1042*fae548d3Szrj // discard all line information that doesn't pertain to the given 1043*fae548d3Szrj // section. 1044*fae548d3Szrj const unsigned char* 1045*fae548d3Szrj read_lines(const unsigned char* lineptr, unsigned int shndx); 1046*fae548d3Szrj 1047*fae548d3Szrj // Process a single line info opcode at START using the state 1048*fae548d3Szrj // machine at LSM. Return true if we should define a line using the 1049*fae548d3Szrj // current state of the line state machine. Place the length of the 1050*fae548d3Szrj // opcode in LEN. 1051*fae548d3Szrj bool 1052*fae548d3Szrj process_one_opcode(const unsigned char* start, 1053*fae548d3Szrj struct LineStateMachine* lsm, size_t* len); 1054*fae548d3Szrj 1055*fae548d3Szrj // Some parts of processing differ depending on whether the input 1056*fae548d3Szrj // was a .o file or not. 1057*fae548d3Szrj bool input_is_relobj(); 1058*fae548d3Szrj 1059*fae548d3Szrj // If we saw anything amiss while parsing, we set this to false. 1060*fae548d3Szrj // Then addr2line will always fail (rather than return possibly- 1061*fae548d3Szrj // corrupt data). 1062*fae548d3Szrj bool data_valid_; 1063*fae548d3Szrj 1064*fae548d3Szrj // A DWARF2/3 line info header. This is not the same size as in the 1065*fae548d3Szrj // actual file, as the one in the file may have a 32 bit or 64 bit 1066*fae548d3Szrj // lengths. 1067*fae548d3Szrj 1068*fae548d3Szrj struct Dwarf_line_infoHeader 1069*fae548d3Szrj { 1070*fae548d3Szrj off_t total_length; 1071*fae548d3Szrj int version; 1072*fae548d3Szrj off_t prologue_length; 1073*fae548d3Szrj int min_insn_length; // insn stands for instruction 1074*fae548d3Szrj int max_ops_per_insn; // Added in DWARF-4. 1075*fae548d3Szrj bool default_is_stmt; // stmt stands for statement 1076*fae548d3Szrj signed char line_base; 1077*fae548d3Szrj int line_range; 1078*fae548d3Szrj unsigned char opcode_base; 1079*fae548d3Szrj std::vector<unsigned char> std_opcode_lengths; 1080*fae548d3Szrj int offset_size; 1081*fae548d3Szrj } header_; 1082*fae548d3Szrj 1083*fae548d3Szrj // buffer is the buffer for our line info, starting at exactly where 1084*fae548d3Szrj // the line info to read is. 1085*fae548d3Szrj const unsigned char* buffer_; 1086*fae548d3Szrj const unsigned char* buffer_end_; 1087*fae548d3Szrj // If the buffer was allocated temporarily, and therefore must be 1088*fae548d3Szrj // deallocated in the dtor, this contains a pointer to the start 1089*fae548d3Szrj // of the buffer. 1090*fae548d3Szrj const unsigned char* buffer_start_; 1091*fae548d3Szrj 1092*fae548d3Szrj // This has relocations that point into buffer. 1093*fae548d3Szrj Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_; 1094*fae548d3Szrj // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA. 1095*fae548d3Szrj unsigned int track_relocs_type_; 1096*fae548d3Szrj 1097*fae548d3Szrj // This is used to figure out what section to apply a relocation to. 1098*fae548d3Szrj const unsigned char* symtab_buffer_; 1099*fae548d3Szrj section_size_type symtab_buffer_size_; 1100*fae548d3Szrj 1101*fae548d3Szrj // Holds the directories and files as we see them. We have an array 1102*fae548d3Szrj // of directory-lists, one for each .o file we're reading (usually 1103*fae548d3Szrj // there will just be one, but there may be more if input is a .so). 1104*fae548d3Szrj std::vector<std::vector<std::string> > directories_; 1105*fae548d3Szrj // The first part is an index into directories_, the second the filename. 1106*fae548d3Szrj std::vector<std::vector< std::pair<int, std::string> > > files_; 1107*fae548d3Szrj 1108*fae548d3Szrj // An index into the current directories_ and files_ vectors. 1109*fae548d3Szrj int current_header_index_; 1110*fae548d3Szrj 1111*fae548d3Szrj // A sorted map from offset of the relocation target to the shndx 1112*fae548d3Szrj // and addend for the relocation. 1113*fae548d3Szrj typedef std::map<off_t, std::pair<unsigned int, off_t> > 1114*fae548d3Szrj Reloc_map; 1115*fae548d3Szrj Reloc_map reloc_map_; 1116*fae548d3Szrj 1117*fae548d3Szrj // We have a vector of offset->lineno entries for every input section. 1118*fae548d3Szrj typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> > 1119*fae548d3Szrj Lineno_map; 1120*fae548d3Szrj 1121*fae548d3Szrj Lineno_map line_number_map_; 1122*fae548d3Szrj }; 1123*fae548d3Szrj 1124*fae548d3Szrj } // End namespace gold. 1125*fae548d3Szrj 1126*fae548d3Szrj #endif // !defined(GOLD_DWARF_READER_H) 1127