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