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