1 // dwarf_reader.cc -- parse dwarf2/3 debug information 2 3 // Copyright (C) 2007-2016 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 #include "gold.h" 24 25 #include <algorithm> 26 #include <utility> 27 #include <vector> 28 29 #include "elfcpp_swap.h" 30 #include "dwarf.h" 31 #include "object.h" 32 #include "reloc.h" 33 #include "dwarf_reader.h" 34 #include "int_encoding.h" 35 #include "compressed_output.h" 36 37 namespace gold { 38 39 // Class Sized_elf_reloc_mapper 40 41 // Initialize the relocation tracker for section RELOC_SHNDX. 42 43 template<int size, bool big_endian> 44 bool 45 Sized_elf_reloc_mapper<size, big_endian>::do_initialize( 46 unsigned int reloc_shndx, unsigned int reloc_type) 47 { 48 this->reloc_type_ = reloc_type; 49 return this->track_relocs_.initialize(this->object_, reloc_shndx, 50 reloc_type); 51 } 52 53 // Looks in the symtab to see what section a symbol is in. 54 55 template<int size, bool big_endian> 56 unsigned int 57 Sized_elf_reloc_mapper<size, big_endian>::symbol_section( 58 unsigned int symndx, Address* value, bool* is_ordinary) 59 { 60 const int symsize = elfcpp::Elf_sizes<size>::sym_size; 61 gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_); 62 elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize); 63 *value = elfsym.get_st_value(); 64 return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(), 65 is_ordinary); 66 } 67 68 // Return the section index and offset within the section of 69 // the target of the relocation for RELOC_OFFSET. 70 71 template<int size, bool big_endian> 72 unsigned int 73 Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target( 74 off_t reloc_offset, off_t* target_offset) 75 { 76 this->track_relocs_.advance(reloc_offset); 77 if (reloc_offset != this->track_relocs_.next_offset()) 78 return 0; 79 unsigned int symndx = this->track_relocs_.next_symndx(); 80 typename elfcpp::Elf_types<size>::Elf_Addr value; 81 bool is_ordinary; 82 unsigned int target_shndx = this->symbol_section(symndx, &value, 83 &is_ordinary); 84 if (!is_ordinary) 85 return 0; 86 if (this->reloc_type_ == elfcpp::SHT_RELA) 87 value += this->track_relocs_.next_addend(); 88 *target_offset = value; 89 return target_shndx; 90 } 91 92 static inline Elf_reloc_mapper* 93 make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab, 94 off_t symtab_size) 95 { 96 if (object->elfsize() == 32) 97 { 98 if (object->is_big_endian()) 99 { 100 #ifdef HAVE_TARGET_32_BIG 101 return new Sized_elf_reloc_mapper<32, true>(object, symtab, 102 symtab_size); 103 #else 104 gold_unreachable(); 105 #endif 106 } 107 else 108 { 109 #ifdef HAVE_TARGET_32_LITTLE 110 return new Sized_elf_reloc_mapper<32, false>(object, symtab, 111 symtab_size); 112 #else 113 gold_unreachable(); 114 #endif 115 } 116 } 117 else if (object->elfsize() == 64) 118 { 119 if (object->is_big_endian()) 120 { 121 #ifdef HAVE_TARGET_64_BIG 122 return new Sized_elf_reloc_mapper<64, true>(object, symtab, 123 symtab_size); 124 #else 125 gold_unreachable(); 126 #endif 127 } 128 else 129 { 130 #ifdef HAVE_TARGET_64_LITTLE 131 return new Sized_elf_reloc_mapper<64, false>(object, symtab, 132 symtab_size); 133 #else 134 gold_unreachable(); 135 #endif 136 } 137 } 138 else 139 gold_unreachable(); 140 } 141 142 // class Dwarf_abbrev_table 143 144 void 145 Dwarf_abbrev_table::clear_abbrev_codes() 146 { 147 for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code) 148 { 149 if (this->low_abbrev_codes_[code] != NULL) 150 { 151 delete this->low_abbrev_codes_[code]; 152 this->low_abbrev_codes_[code] = NULL; 153 } 154 } 155 for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin(); 156 it != this->high_abbrev_codes_.end(); 157 ++it) 158 { 159 if (it->second != NULL) 160 delete it->second; 161 } 162 this->high_abbrev_codes_.clear(); 163 } 164 165 // Read the abbrev table from an object file. 166 167 bool 168 Dwarf_abbrev_table::do_read_abbrevs( 169 Relobj* object, 170 unsigned int abbrev_shndx, 171 off_t abbrev_offset) 172 { 173 this->clear_abbrev_codes(); 174 175 // If we don't have relocations, abbrev_shndx will be 0, and 176 // we'll have to hunt for the .debug_abbrev section. 177 if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0) 178 abbrev_shndx = this->abbrev_shndx_; 179 else if (abbrev_shndx == 0) 180 { 181 for (unsigned int i = 1; i < object->shnum(); ++i) 182 { 183 std::string name = object->section_name(i); 184 if (name == ".debug_abbrev" || name == ".zdebug_abbrev") 185 { 186 abbrev_shndx = i; 187 // Correct the offset. For incremental update links, we have a 188 // relocated offset that is relative to the output section, but 189 // here we need an offset relative to the input section. 190 abbrev_offset -= object->output_section_offset(i); 191 break; 192 } 193 } 194 if (abbrev_shndx == 0) 195 return false; 196 } 197 198 // Get the section contents and decompress if necessary. 199 if (abbrev_shndx != this->abbrev_shndx_) 200 { 201 if (this->owns_buffer_ && this->buffer_ != NULL) 202 { 203 delete[] this->buffer_; 204 this->owns_buffer_ = false; 205 } 206 207 section_size_type buffer_size; 208 this->buffer_ = 209 object->decompressed_section_contents(abbrev_shndx, 210 &buffer_size, 211 &this->owns_buffer_); 212 this->buffer_end_ = this->buffer_ + buffer_size; 213 this->abbrev_shndx_ = abbrev_shndx; 214 } 215 216 this->buffer_pos_ = this->buffer_ + abbrev_offset; 217 return true; 218 } 219 220 // Lookup the abbrev code entry for CODE. This function is called 221 // only when the abbrev code is not in the direct lookup table. 222 // It may be in the hash table, it may not have been read yet, 223 // or it may not exist in the abbrev table. 224 225 const Dwarf_abbrev_table::Abbrev_code* 226 Dwarf_abbrev_table::do_get_abbrev(unsigned int code) 227 { 228 // See if the abbrev code is already in the hash table. 229 Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code); 230 if (it != this->high_abbrev_codes_.end()) 231 return it->second; 232 233 // Read and store abbrev code definitions until we find the 234 // one we're looking for. 235 for (;;) 236 { 237 // Read the abbrev code. A zero here indicates the end of the 238 // abbrev table. 239 size_t len; 240 if (this->buffer_pos_ >= this->buffer_end_) 241 return NULL; 242 uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len); 243 if (nextcode == 0) 244 { 245 this->buffer_pos_ = this->buffer_end_; 246 return NULL; 247 } 248 this->buffer_pos_ += len; 249 250 // Read the tag. 251 if (this->buffer_pos_ >= this->buffer_end_) 252 return NULL; 253 uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len); 254 this->buffer_pos_ += len; 255 256 // Read the has_children flag. 257 if (this->buffer_pos_ >= this->buffer_end_) 258 return NULL; 259 bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes; 260 this->buffer_pos_ += 1; 261 262 // Read the list of (attribute, form) pairs. 263 Abbrev_code* entry = new Abbrev_code(tag, has_children); 264 for (;;) 265 { 266 // Read the attribute. 267 if (this->buffer_pos_ >= this->buffer_end_) 268 return NULL; 269 uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len); 270 this->buffer_pos_ += len; 271 272 // Read the form. 273 if (this->buffer_pos_ >= this->buffer_end_) 274 return NULL; 275 uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len); 276 this->buffer_pos_ += len; 277 278 // A (0,0) pair terminates the list. 279 if (attr == 0 && form == 0) 280 break; 281 282 if (attr == elfcpp::DW_AT_sibling) 283 entry->has_sibling_attribute = true; 284 285 entry->add_attribute(attr, form); 286 } 287 288 this->store_abbrev(nextcode, entry); 289 if (nextcode == code) 290 return entry; 291 } 292 293 return NULL; 294 } 295 296 // class Dwarf_ranges_table 297 298 // Read the ranges table from an object file. 299 300 bool 301 Dwarf_ranges_table::read_ranges_table( 302 Relobj* object, 303 const unsigned char* symtab, 304 off_t symtab_size, 305 unsigned int ranges_shndx) 306 { 307 // If we've already read this abbrev table, return immediately. 308 if (this->ranges_shndx_ > 0 309 && this->ranges_shndx_ == ranges_shndx) 310 return true; 311 312 // If we don't have relocations, ranges_shndx will be 0, and 313 // we'll have to hunt for the .debug_ranges section. 314 if (ranges_shndx == 0 && this->ranges_shndx_ > 0) 315 ranges_shndx = this->ranges_shndx_; 316 else if (ranges_shndx == 0) 317 { 318 for (unsigned int i = 1; i < object->shnum(); ++i) 319 { 320 std::string name = object->section_name(i); 321 if (name == ".debug_ranges" || name == ".zdebug_ranges") 322 { 323 ranges_shndx = i; 324 this->output_section_offset_ = object->output_section_offset(i); 325 break; 326 } 327 } 328 if (ranges_shndx == 0) 329 return false; 330 } 331 332 // Get the section contents and decompress if necessary. 333 if (ranges_shndx != this->ranges_shndx_) 334 { 335 if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL) 336 { 337 delete[] this->ranges_buffer_; 338 this->owns_ranges_buffer_ = false; 339 } 340 341 section_size_type buffer_size; 342 this->ranges_buffer_ = 343 object->decompressed_section_contents(ranges_shndx, 344 &buffer_size, 345 &this->owns_ranges_buffer_); 346 this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size; 347 this->ranges_shndx_ = ranges_shndx; 348 } 349 350 if (this->ranges_reloc_mapper_ != NULL) 351 { 352 delete this->ranges_reloc_mapper_; 353 this->ranges_reloc_mapper_ = NULL; 354 } 355 356 // For incremental objects, we have no relocations. 357 if (object->is_incremental()) 358 return true; 359 360 // Find the relocation section for ".debug_ranges". 361 unsigned int reloc_shndx = 0; 362 unsigned int reloc_type = 0; 363 for (unsigned int i = 0; i < object->shnum(); ++i) 364 { 365 reloc_type = object->section_type(i); 366 if ((reloc_type == elfcpp::SHT_REL 367 || reloc_type == elfcpp::SHT_RELA) 368 && object->section_info(i) == ranges_shndx) 369 { 370 reloc_shndx = i; 371 break; 372 } 373 } 374 375 this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab, 376 symtab_size); 377 this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type); 378 this->reloc_type_ = reloc_type; 379 380 return true; 381 } 382 383 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET. 384 385 Dwarf_range_list* 386 Dwarf_ranges_table::read_range_list( 387 Relobj* object, 388 const unsigned char* symtab, 389 off_t symtab_size, 390 unsigned int addr_size, 391 unsigned int ranges_shndx, 392 off_t offset) 393 { 394 Dwarf_range_list* ranges; 395 396 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx)) 397 return NULL; 398 399 // Correct the offset. For incremental update links, we have a 400 // relocated offset that is relative to the output section, but 401 // here we need an offset relative to the input section. 402 offset -= this->output_section_offset_; 403 404 // Read the range list at OFFSET. 405 ranges = new Dwarf_range_list(); 406 off_t base = 0; 407 for (; 408 this->ranges_buffer_ + offset < this->ranges_buffer_end_; 409 offset += 2 * addr_size) 410 { 411 off_t start; 412 off_t end; 413 414 // Read the raw contents of the section. 415 if (addr_size == 4) 416 { 417 start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_ 418 + offset); 419 end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_ 420 + offset + 4); 421 } 422 else 423 { 424 start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_ 425 + offset); 426 end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_ 427 + offset + 8); 428 } 429 430 // Check for relocations and adjust the values. 431 unsigned int shndx1 = 0; 432 unsigned int shndx2 = 0; 433 if (this->ranges_reloc_mapper_ != NULL) 434 { 435 shndx1 = this->lookup_reloc(offset, &start); 436 shndx2 = this->lookup_reloc(offset + addr_size, &end); 437 } 438 439 // End of list is marked by a pair of zeroes. 440 if (shndx1 == 0 && start == 0 && end == 0) 441 break; 442 443 // A "base address selection entry" is identified by 444 // 0xffffffff for the first value of the pair. The second 445 // value is used as a base for subsequent range list entries. 446 if (shndx1 == 0 && start == -1) 447 base = end; 448 else if (shndx1 == shndx2) 449 { 450 if (shndx1 == 0 || object->is_section_included(shndx1)) 451 ranges->add(shndx1, base + start, base + end); 452 } 453 else 454 gold_warning(_("%s: DWARF info may be corrupt; offsets in a " 455 "range list entry are in different sections"), 456 object->name().c_str()); 457 } 458 459 return ranges; 460 } 461 462 // Look for a relocation at offset OFF in the range table, 463 // and return the section index and offset of the target. 464 465 unsigned int 466 Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off) 467 { 468 off_t value; 469 unsigned int shndx = 470 this->ranges_reloc_mapper_->get_reloc_target(off, &value); 471 if (shndx == 0) 472 return 0; 473 if (this->reloc_type_ == elfcpp::SHT_REL) 474 *target_off += value; 475 else 476 *target_off = value; 477 return shndx; 478 } 479 480 // class Dwarf_pubnames_table 481 482 // Read the pubnames section from the object file. 483 484 bool 485 Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab, 486 off_t symtab_size) 487 { 488 section_size_type buffer_size; 489 unsigned int shndx = 0; 490 const char* name = this->is_pubtypes_ ? "pubtypes" : "pubnames"; 491 const char* gnu_name = (this->is_pubtypes_ 492 ? "gnu_pubtypes" 493 : "gnu_pubnames"); 494 495 for (unsigned int i = 1; i < object->shnum(); ++i) 496 { 497 std::string section_name = object->section_name(i); 498 const char* section_name_suffix = section_name.c_str(); 499 if (is_prefix_of(".debug_", section_name_suffix)) 500 section_name_suffix += 7; 501 else if (is_prefix_of(".zdebug_", section_name_suffix)) 502 section_name_suffix += 8; 503 else 504 continue; 505 if (strcmp(section_name_suffix, name) == 0) 506 { 507 shndx = i; 508 break; 509 } 510 else if (strcmp(section_name_suffix, gnu_name) == 0) 511 { 512 shndx = i; 513 this->is_gnu_style_ = true; 514 break; 515 } 516 } 517 if (shndx == 0) 518 return false; 519 520 this->buffer_ = object->decompressed_section_contents(shndx, 521 &buffer_size, 522 &this->owns_buffer_); 523 if (this->buffer_ == NULL) 524 return false; 525 this->buffer_end_ = this->buffer_ + buffer_size; 526 527 // For incremental objects, we have no relocations. 528 if (object->is_incremental()) 529 return true; 530 531 // Find the relocation section 532 unsigned int reloc_shndx = 0; 533 unsigned int reloc_type = 0; 534 for (unsigned int i = 0; i < object->shnum(); ++i) 535 { 536 reloc_type = object->section_type(i); 537 if ((reloc_type == elfcpp::SHT_REL 538 || reloc_type == elfcpp::SHT_RELA) 539 && object->section_info(i) == shndx) 540 { 541 reloc_shndx = i; 542 break; 543 } 544 } 545 546 this->reloc_mapper_ = make_elf_reloc_mapper(object, symtab, symtab_size); 547 this->reloc_mapper_->initialize(reloc_shndx, reloc_type); 548 this->reloc_type_ = reloc_type; 549 550 return true; 551 } 552 553 // Read the header for the set at OFFSET. 554 555 bool 556 Dwarf_pubnames_table::read_header(off_t offset) 557 { 558 // Make sure we have actually read the section. 559 gold_assert(this->buffer_ != NULL); 560 561 if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_) 562 return false; 563 564 const unsigned char* pinfo = this->buffer_ + offset; 565 566 // Read the unit_length field. 567 uint64_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo); 568 pinfo += 4; 569 if (unit_length == 0xffffffff) 570 { 571 unit_length = this->dwinfo_->read_from_pointer<64>(pinfo); 572 this->unit_length_ = unit_length + 12; 573 pinfo += 8; 574 this->offset_size_ = 8; 575 } 576 else 577 { 578 this->unit_length_ = unit_length + 4; 579 this->offset_size_ = 4; 580 } 581 this->end_of_table_ = pinfo + unit_length; 582 583 // If unit_length is too big, maybe we should reject the whole table, 584 // but in cases we know about, it seems OK to assume that the table 585 // is valid through the actual end of the section. 586 if (this->end_of_table_ > this->buffer_end_) 587 this->end_of_table_ = this->buffer_end_; 588 589 // Check the version. 590 unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo); 591 pinfo += 2; 592 if (version != 2) 593 return false; 594 595 this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_, 596 &this->cu_offset_); 597 598 // Skip the debug_info_offset and debug_info_size fields. 599 pinfo += 2 * this->offset_size_; 600 601 if (pinfo >= this->buffer_end_) 602 return false; 603 604 this->pinfo_ = pinfo; 605 return true; 606 } 607 608 // Read the next name from the set. 609 610 const char* 611 Dwarf_pubnames_table::next_name(uint8_t* flag_byte) 612 { 613 const unsigned char* pinfo = this->pinfo_; 614 615 // Check for end of list. The table should be terminated by an 616 // entry containing nothing but a DIE offset of 0. 617 if (pinfo + this->offset_size_ >= this->end_of_table_) 618 return NULL; 619 620 // Skip the offset within the CU. If this is zero, but we're not 621 // at the end of the table, then we have a real pubnames entry 622 // whose DIE offset is 0 (likely to be a GCC bug). Since we 623 // don't actually use the DIE offset in building .gdb_index, 624 // it's harmless. 625 pinfo += this->offset_size_; 626 627 if (this->is_gnu_style_) 628 *flag_byte = *pinfo++; 629 else 630 *flag_byte = 0; 631 632 // Return a pointer to the string at the current location, 633 // and advance the pointer to the next entry. 634 const char* ret = reinterpret_cast<const char*>(pinfo); 635 while (pinfo < this->buffer_end_ && *pinfo != '\0') 636 ++pinfo; 637 if (pinfo < this->buffer_end_) 638 ++pinfo; 639 640 this->pinfo_ = pinfo; 641 return ret; 642 } 643 644 // class Dwarf_die 645 646 Dwarf_die::Dwarf_die( 647 Dwarf_info_reader* dwinfo, 648 off_t die_offset, 649 Dwarf_die* parent) 650 : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset), 651 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(), 652 attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL), 653 linkage_name_off_(-1), string_shndx_(0), specification_(0), 654 abstract_origin_(0) 655 { 656 size_t len; 657 const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset); 658 if (pdie == NULL) 659 return; 660 unsigned int code = read_unsigned_LEB_128(pdie, &len); 661 if (code == 0) 662 { 663 if (parent != NULL) 664 parent->set_sibling_offset(die_offset + len); 665 return; 666 } 667 this->attr_offset_ = len; 668 669 // Lookup the abbrev code in the abbrev table. 670 this->abbrev_code_ = dwinfo->get_abbrev(code); 671 } 672 673 // Read all the attributes of the DIE. 674 675 bool 676 Dwarf_die::read_attributes() 677 { 678 if (this->attributes_read_) 679 return true; 680 681 gold_assert(this->abbrev_code_ != NULL); 682 683 const unsigned char* pdie = 684 this->dwinfo_->buffer_at_offset(this->die_offset_); 685 if (pdie == NULL) 686 return false; 687 const unsigned char* pattr = pdie + this->attr_offset_; 688 689 unsigned int nattr = this->abbrev_code_->attributes.size(); 690 this->attributes_.reserve(nattr); 691 for (unsigned int i = 0; i < nattr; ++i) 692 { 693 size_t len; 694 unsigned int attr = this->abbrev_code_->attributes[i].attr; 695 unsigned int form = this->abbrev_code_->attributes[i].form; 696 if (form == elfcpp::DW_FORM_indirect) 697 { 698 form = read_unsigned_LEB_128(pattr, &len); 699 pattr += len; 700 } 701 off_t attr_off = this->die_offset_ + (pattr - pdie); 702 bool ref_form = false; 703 Attribute_value attr_value; 704 attr_value.attr = attr; 705 attr_value.form = form; 706 attr_value.aux.shndx = 0; 707 switch(form) 708 { 709 case elfcpp::DW_FORM_flag_present: 710 attr_value.val.intval = 1; 711 break; 712 case elfcpp::DW_FORM_strp: 713 { 714 off_t str_off; 715 if (this->dwinfo_->offset_size() == 4) 716 str_off = this->dwinfo_->read_from_pointer<32>(&pattr); 717 else 718 str_off = this->dwinfo_->read_from_pointer<64>(&pattr); 719 unsigned int shndx = 720 this->dwinfo_->lookup_reloc(attr_off, &str_off); 721 attr_value.aux.shndx = shndx; 722 attr_value.val.refval = str_off; 723 break; 724 } 725 case elfcpp::DW_FORM_sec_offset: 726 { 727 off_t sec_off; 728 if (this->dwinfo_->offset_size() == 4) 729 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr); 730 else 731 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr); 732 unsigned int shndx = 733 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 734 attr_value.aux.shndx = shndx; 735 attr_value.val.refval = sec_off; 736 ref_form = true; 737 break; 738 } 739 case elfcpp::DW_FORM_addr: 740 case elfcpp::DW_FORM_ref_addr: 741 { 742 off_t sec_off; 743 if (this->dwinfo_->address_size() == 4) 744 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr); 745 else 746 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr); 747 unsigned int shndx = 748 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 749 attr_value.aux.shndx = shndx; 750 attr_value.val.refval = sec_off; 751 ref_form = true; 752 break; 753 } 754 case elfcpp::DW_FORM_block1: 755 attr_value.aux.blocklen = *pattr++; 756 attr_value.val.blockval = pattr; 757 pattr += attr_value.aux.blocklen; 758 break; 759 case elfcpp::DW_FORM_block2: 760 attr_value.aux.blocklen = 761 this->dwinfo_->read_from_pointer<16>(&pattr); 762 attr_value.val.blockval = pattr; 763 pattr += attr_value.aux.blocklen; 764 break; 765 case elfcpp::DW_FORM_block4: 766 attr_value.aux.blocklen = 767 this->dwinfo_->read_from_pointer<32>(&pattr); 768 attr_value.val.blockval = pattr; 769 pattr += attr_value.aux.blocklen; 770 break; 771 case elfcpp::DW_FORM_block: 772 case elfcpp::DW_FORM_exprloc: 773 attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len); 774 attr_value.val.blockval = pattr + len; 775 pattr += len + attr_value.aux.blocklen; 776 break; 777 case elfcpp::DW_FORM_data1: 778 case elfcpp::DW_FORM_flag: 779 attr_value.val.intval = *pattr++; 780 break; 781 case elfcpp::DW_FORM_ref1: 782 attr_value.val.refval = *pattr++; 783 ref_form = true; 784 break; 785 case elfcpp::DW_FORM_data2: 786 attr_value.val.intval = 787 this->dwinfo_->read_from_pointer<16>(&pattr); 788 break; 789 case elfcpp::DW_FORM_ref2: 790 attr_value.val.refval = 791 this->dwinfo_->read_from_pointer<16>(&pattr); 792 ref_form = true; 793 break; 794 case elfcpp::DW_FORM_data4: 795 { 796 off_t sec_off; 797 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr); 798 unsigned int shndx = 799 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 800 attr_value.aux.shndx = shndx; 801 attr_value.val.intval = sec_off; 802 break; 803 } 804 case elfcpp::DW_FORM_ref4: 805 { 806 off_t sec_off; 807 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr); 808 unsigned int shndx = 809 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 810 attr_value.aux.shndx = shndx; 811 attr_value.val.refval = sec_off; 812 ref_form = true; 813 break; 814 } 815 case elfcpp::DW_FORM_data8: 816 { 817 off_t sec_off; 818 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr); 819 unsigned int shndx = 820 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 821 attr_value.aux.shndx = shndx; 822 attr_value.val.intval = sec_off; 823 break; 824 } 825 case elfcpp::DW_FORM_ref_sig8: 826 attr_value.val.uintval = 827 this->dwinfo_->read_from_pointer<64>(&pattr); 828 break; 829 case elfcpp::DW_FORM_ref8: 830 { 831 off_t sec_off; 832 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr); 833 unsigned int shndx = 834 this->dwinfo_->lookup_reloc(attr_off, &sec_off); 835 attr_value.aux.shndx = shndx; 836 attr_value.val.refval = sec_off; 837 ref_form = true; 838 break; 839 } 840 case elfcpp::DW_FORM_ref_udata: 841 attr_value.val.refval = read_unsigned_LEB_128(pattr, &len); 842 ref_form = true; 843 pattr += len; 844 break; 845 case elfcpp::DW_FORM_udata: 846 case elfcpp::DW_FORM_GNU_addr_index: 847 case elfcpp::DW_FORM_GNU_str_index: 848 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len); 849 pattr += len; 850 break; 851 case elfcpp::DW_FORM_sdata: 852 attr_value.val.intval = read_signed_LEB_128(pattr, &len); 853 pattr += len; 854 break; 855 case elfcpp::DW_FORM_string: 856 attr_value.val.stringval = reinterpret_cast<const char*>(pattr); 857 len = strlen(attr_value.val.stringval); 858 pattr += len + 1; 859 break; 860 default: 861 return false; 862 } 863 864 // Cache the most frequently-requested attributes. 865 switch (attr) 866 { 867 case elfcpp::DW_AT_name: 868 if (form == elfcpp::DW_FORM_string) 869 this->name_ = attr_value.val.stringval; 870 else if (form == elfcpp::DW_FORM_strp) 871 { 872 // All indirect strings should refer to the same 873 // string section, so we just save the last one seen. 874 this->string_shndx_ = attr_value.aux.shndx; 875 this->name_off_ = attr_value.val.refval; 876 } 877 break; 878 case elfcpp::DW_AT_linkage_name: 879 case elfcpp::DW_AT_MIPS_linkage_name: 880 if (form == elfcpp::DW_FORM_string) 881 this->linkage_name_ = attr_value.val.stringval; 882 else if (form == elfcpp::DW_FORM_strp) 883 { 884 // All indirect strings should refer to the same 885 // string section, so we just save the last one seen. 886 this->string_shndx_ = attr_value.aux.shndx; 887 this->linkage_name_off_ = attr_value.val.refval; 888 } 889 break; 890 case elfcpp::DW_AT_specification: 891 if (ref_form) 892 this->specification_ = attr_value.val.refval; 893 break; 894 case elfcpp::DW_AT_abstract_origin: 895 if (ref_form) 896 this->abstract_origin_ = attr_value.val.refval; 897 break; 898 case elfcpp::DW_AT_sibling: 899 if (ref_form && attr_value.aux.shndx == 0) 900 this->sibling_offset_ = attr_value.val.refval; 901 default: 902 break; 903 } 904 905 this->attributes_.push_back(attr_value); 906 } 907 908 // Now that we know where the next DIE begins, record the offset 909 // to avoid later recalculation. 910 if (this->has_children()) 911 this->child_offset_ = this->die_offset_ + (pattr - pdie); 912 else 913 this->sibling_offset_ = this->die_offset_ + (pattr - pdie); 914 915 this->attributes_read_ = true; 916 return true; 917 } 918 919 // Skip all the attributes of the DIE and return the offset of the next DIE. 920 921 off_t 922 Dwarf_die::skip_attributes() 923 { 924 gold_assert(this->abbrev_code_ != NULL); 925 926 const unsigned char* pdie = 927 this->dwinfo_->buffer_at_offset(this->die_offset_); 928 if (pdie == NULL) 929 return 0; 930 const unsigned char* pattr = pdie + this->attr_offset_; 931 932 for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i) 933 { 934 size_t len; 935 unsigned int form = this->abbrev_code_->attributes[i].form; 936 if (form == elfcpp::DW_FORM_indirect) 937 { 938 form = read_unsigned_LEB_128(pattr, &len); 939 pattr += len; 940 } 941 switch(form) 942 { 943 case elfcpp::DW_FORM_flag_present: 944 break; 945 case elfcpp::DW_FORM_strp: 946 case elfcpp::DW_FORM_sec_offset: 947 pattr += this->dwinfo_->offset_size(); 948 break; 949 case elfcpp::DW_FORM_addr: 950 case elfcpp::DW_FORM_ref_addr: 951 pattr += this->dwinfo_->address_size(); 952 break; 953 case elfcpp::DW_FORM_block1: 954 pattr += 1 + *pattr; 955 break; 956 case elfcpp::DW_FORM_block2: 957 { 958 uint16_t block_size; 959 block_size = this->dwinfo_->read_from_pointer<16>(&pattr); 960 pattr += block_size; 961 break; 962 } 963 case elfcpp::DW_FORM_block4: 964 { 965 uint32_t block_size; 966 block_size = this->dwinfo_->read_from_pointer<32>(&pattr); 967 pattr += block_size; 968 break; 969 } 970 case elfcpp::DW_FORM_block: 971 case elfcpp::DW_FORM_exprloc: 972 { 973 uint64_t block_size; 974 block_size = read_unsigned_LEB_128(pattr, &len); 975 pattr += len + block_size; 976 break; 977 } 978 case elfcpp::DW_FORM_data1: 979 case elfcpp::DW_FORM_ref1: 980 case elfcpp::DW_FORM_flag: 981 pattr += 1; 982 break; 983 case elfcpp::DW_FORM_data2: 984 case elfcpp::DW_FORM_ref2: 985 pattr += 2; 986 break; 987 case elfcpp::DW_FORM_data4: 988 case elfcpp::DW_FORM_ref4: 989 pattr += 4; 990 break; 991 case elfcpp::DW_FORM_data8: 992 case elfcpp::DW_FORM_ref8: 993 case elfcpp::DW_FORM_ref_sig8: 994 pattr += 8; 995 break; 996 case elfcpp::DW_FORM_ref_udata: 997 case elfcpp::DW_FORM_udata: 998 case elfcpp::DW_FORM_GNU_addr_index: 999 case elfcpp::DW_FORM_GNU_str_index: 1000 read_unsigned_LEB_128(pattr, &len); 1001 pattr += len; 1002 break; 1003 case elfcpp::DW_FORM_sdata: 1004 read_signed_LEB_128(pattr, &len); 1005 pattr += len; 1006 break; 1007 case elfcpp::DW_FORM_string: 1008 len = strlen(reinterpret_cast<const char*>(pattr)); 1009 pattr += len + 1; 1010 break; 1011 default: 1012 return 0; 1013 } 1014 } 1015 1016 return this->die_offset_ + (pattr - pdie); 1017 } 1018 1019 // Get the name of the DIE and cache it. 1020 1021 void 1022 Dwarf_die::set_name() 1023 { 1024 if (this->name_ != NULL || !this->read_attributes()) 1025 return; 1026 if (this->name_off_ != -1) 1027 this->name_ = this->dwinfo_->get_string(this->name_off_, 1028 this->string_shndx_); 1029 } 1030 1031 // Get the linkage name of the DIE and cache it. 1032 1033 void 1034 Dwarf_die::set_linkage_name() 1035 { 1036 if (this->linkage_name_ != NULL || !this->read_attributes()) 1037 return; 1038 if (this->linkage_name_off_ != -1) 1039 this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_, 1040 this->string_shndx_); 1041 } 1042 1043 // Return the value of attribute ATTR. 1044 1045 const Dwarf_die::Attribute_value* 1046 Dwarf_die::attribute(unsigned int attr) 1047 { 1048 if (!this->read_attributes()) 1049 return NULL; 1050 for (unsigned int i = 0; i < this->attributes_.size(); ++i) 1051 { 1052 if (this->attributes_[i].attr == attr) 1053 return &this->attributes_[i]; 1054 } 1055 return NULL; 1056 } 1057 1058 const char* 1059 Dwarf_die::string_attribute(unsigned int attr) 1060 { 1061 const Attribute_value* attr_val = this->attribute(attr); 1062 if (attr_val == NULL) 1063 return NULL; 1064 switch (attr_val->form) 1065 { 1066 case elfcpp::DW_FORM_string: 1067 return attr_val->val.stringval; 1068 case elfcpp::DW_FORM_strp: 1069 return this->dwinfo_->get_string(attr_val->val.refval, 1070 attr_val->aux.shndx); 1071 default: 1072 return NULL; 1073 } 1074 } 1075 1076 int64_t 1077 Dwarf_die::int_attribute(unsigned int attr) 1078 { 1079 const Attribute_value* attr_val = this->attribute(attr); 1080 if (attr_val == NULL) 1081 return 0; 1082 switch (attr_val->form) 1083 { 1084 case elfcpp::DW_FORM_flag_present: 1085 case elfcpp::DW_FORM_data1: 1086 case elfcpp::DW_FORM_flag: 1087 case elfcpp::DW_FORM_data2: 1088 case elfcpp::DW_FORM_data4: 1089 case elfcpp::DW_FORM_data8: 1090 case elfcpp::DW_FORM_sdata: 1091 return attr_val->val.intval; 1092 default: 1093 return 0; 1094 } 1095 } 1096 1097 uint64_t 1098 Dwarf_die::uint_attribute(unsigned int attr) 1099 { 1100 const Attribute_value* attr_val = this->attribute(attr); 1101 if (attr_val == NULL) 1102 return 0; 1103 switch (attr_val->form) 1104 { 1105 case elfcpp::DW_FORM_flag_present: 1106 case elfcpp::DW_FORM_data1: 1107 case elfcpp::DW_FORM_flag: 1108 case elfcpp::DW_FORM_data4: 1109 case elfcpp::DW_FORM_data8: 1110 case elfcpp::DW_FORM_ref_sig8: 1111 case elfcpp::DW_FORM_udata: 1112 return attr_val->val.uintval; 1113 default: 1114 return 0; 1115 } 1116 } 1117 1118 off_t 1119 Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx) 1120 { 1121 const Attribute_value* attr_val = this->attribute(attr); 1122 if (attr_val == NULL) 1123 return -1; 1124 switch (attr_val->form) 1125 { 1126 case elfcpp::DW_FORM_sec_offset: 1127 case elfcpp::DW_FORM_addr: 1128 case elfcpp::DW_FORM_ref_addr: 1129 case elfcpp::DW_FORM_ref1: 1130 case elfcpp::DW_FORM_ref2: 1131 case elfcpp::DW_FORM_ref4: 1132 case elfcpp::DW_FORM_ref8: 1133 case elfcpp::DW_FORM_ref_udata: 1134 *shndx = attr_val->aux.shndx; 1135 return attr_val->val.refval; 1136 case elfcpp::DW_FORM_ref_sig8: 1137 *shndx = attr_val->aux.shndx; 1138 return attr_val->val.uintval; 1139 case elfcpp::DW_FORM_data4: 1140 case elfcpp::DW_FORM_data8: 1141 *shndx = attr_val->aux.shndx; 1142 return attr_val->val.intval; 1143 default: 1144 return -1; 1145 } 1146 } 1147 1148 off_t 1149 Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx) 1150 { 1151 const Attribute_value* attr_val = this->attribute(attr); 1152 if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr) 1153 return -1; 1154 1155 *shndx = attr_val->aux.shndx; 1156 return attr_val->val.refval; 1157 } 1158 1159 // Return the offset of this DIE's first child. 1160 1161 off_t 1162 Dwarf_die::child_offset() 1163 { 1164 gold_assert(this->abbrev_code_ != NULL); 1165 if (!this->has_children()) 1166 return 0; 1167 if (this->child_offset_ == 0) 1168 this->child_offset_ = this->skip_attributes(); 1169 return this->child_offset_; 1170 } 1171 1172 // Return the offset of this DIE's next sibling. 1173 1174 off_t 1175 Dwarf_die::sibling_offset() 1176 { 1177 gold_assert(this->abbrev_code_ != NULL); 1178 1179 if (this->sibling_offset_ != 0) 1180 return this->sibling_offset_; 1181 1182 if (!this->has_children()) 1183 { 1184 this->sibling_offset_ = this->skip_attributes(); 1185 return this->sibling_offset_; 1186 } 1187 1188 if (this->has_sibling_attribute()) 1189 { 1190 if (!this->read_attributes()) 1191 return 0; 1192 if (this->sibling_offset_ != 0) 1193 return this->sibling_offset_; 1194 } 1195 1196 // Skip over the children. 1197 off_t child_offset = this->child_offset(); 1198 while (child_offset > 0) 1199 { 1200 Dwarf_die die(this->dwinfo_, child_offset, this); 1201 // The Dwarf_die ctor will set this DIE's sibling offset 1202 // when it reads a zero abbrev code. 1203 if (die.tag() == 0) 1204 break; 1205 child_offset = die.sibling_offset(); 1206 } 1207 1208 // This should be set by now. If not, there was a problem reading 1209 // the DWARF info, and we return 0. 1210 return this->sibling_offset_; 1211 } 1212 1213 // class Dwarf_info_reader 1214 1215 // Begin parsing the debug info. This calls visit_compilation_unit() 1216 // or visit_type_unit() for each compilation or type unit found in the 1217 // section, and visit_die() for each top-level DIE. 1218 1219 void 1220 Dwarf_info_reader::parse() 1221 { 1222 if (this->object_->is_big_endian()) 1223 { 1224 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) 1225 this->do_parse<true>(); 1226 #else 1227 gold_unreachable(); 1228 #endif 1229 } 1230 else 1231 { 1232 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) 1233 this->do_parse<false>(); 1234 #else 1235 gold_unreachable(); 1236 #endif 1237 } 1238 } 1239 1240 template<bool big_endian> 1241 void 1242 Dwarf_info_reader::do_parse() 1243 { 1244 // Get the section contents and decompress if necessary. 1245 section_size_type buffer_size; 1246 bool buffer_is_new; 1247 this->buffer_ = this->object_->decompressed_section_contents(this->shndx_, 1248 &buffer_size, 1249 &buffer_is_new); 1250 if (this->buffer_ == NULL || buffer_size == 0) 1251 return; 1252 this->buffer_end_ = this->buffer_ + buffer_size; 1253 1254 // The offset of this input section in the output section. 1255 off_t section_offset = this->object_->output_section_offset(this->shndx_); 1256 1257 // Start tracking relocations for this section. 1258 this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_, 1259 this->symtab_size_); 1260 this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_); 1261 1262 // Loop over compilation units (or type units). 1263 unsigned int abbrev_shndx = this->abbrev_shndx_; 1264 off_t abbrev_offset = 0; 1265 const unsigned char* pinfo = this->buffer_; 1266 while (pinfo < this->buffer_end_) 1267 { 1268 // Read the compilation (or type) unit header. 1269 const unsigned char* cu_start = pinfo; 1270 this->cu_offset_ = cu_start - this->buffer_; 1271 this->cu_length_ = this->buffer_end_ - cu_start; 1272 1273 // Read unit_length (4 or 12 bytes). 1274 if (!this->check_buffer(pinfo + 4)) 1275 break; 1276 uint32_t unit_length = 1277 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo); 1278 pinfo += 4; 1279 if (unit_length == 0xffffffff) 1280 { 1281 if (!this->check_buffer(pinfo + 8)) 1282 break; 1283 unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo); 1284 pinfo += 8; 1285 this->offset_size_ = 8; 1286 } 1287 else 1288 this->offset_size_ = 4; 1289 if (!this->check_buffer(pinfo + unit_length)) 1290 break; 1291 const unsigned char* cu_end = pinfo + unit_length; 1292 this->cu_length_ = cu_end - cu_start; 1293 if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1)) 1294 break; 1295 1296 // Read version (2 bytes). 1297 this->cu_version_ = 1298 elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo); 1299 pinfo += 2; 1300 1301 // Read debug_abbrev_offset (4 or 8 bytes). 1302 if (this->offset_size_ == 4) 1303 abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo); 1304 else 1305 abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo); 1306 if (this->reloc_shndx_ > 0) 1307 { 1308 off_t reloc_offset = pinfo - this->buffer_; 1309 off_t value; 1310 abbrev_shndx = 1311 this->reloc_mapper_->get_reloc_target(reloc_offset, &value); 1312 if (abbrev_shndx == 0) 1313 return; 1314 if (this->reloc_type_ == elfcpp::SHT_REL) 1315 abbrev_offset += value; 1316 else 1317 abbrev_offset = value; 1318 } 1319 pinfo += this->offset_size_; 1320 1321 // Read address_size (1 byte). 1322 this->address_size_ = *pinfo++; 1323 1324 // For type units, read the two extra fields. 1325 uint64_t signature = 0; 1326 off_t type_offset = 0; 1327 if (this->is_type_unit_) 1328 { 1329 if (!this->check_buffer(pinfo + 8 + this->offset_size_)) 1330 break; 1331 1332 // Read type_signature (8 bytes). 1333 signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo); 1334 pinfo += 8; 1335 1336 // Read type_offset (4 or 8 bytes). 1337 if (this->offset_size_ == 4) 1338 type_offset = 1339 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo); 1340 else 1341 type_offset = 1342 elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo); 1343 pinfo += this->offset_size_; 1344 } 1345 1346 // Read the .debug_abbrev table. 1347 this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx, 1348 abbrev_offset); 1349 1350 // Visit the root DIE. 1351 Dwarf_die root_die(this, 1352 pinfo - (this->buffer_ + this->cu_offset_), 1353 NULL); 1354 if (root_die.tag() != 0) 1355 { 1356 // Visit the CU or TU. 1357 if (this->is_type_unit_) 1358 this->visit_type_unit(section_offset + this->cu_offset_, 1359 cu_end - cu_start, type_offset, signature, 1360 &root_die); 1361 else 1362 this->visit_compilation_unit(section_offset + this->cu_offset_, 1363 cu_end - cu_start, &root_die); 1364 } 1365 1366 // Advance to the next CU. 1367 pinfo = cu_end; 1368 } 1369 1370 if (buffer_is_new) 1371 { 1372 delete[] this->buffer_; 1373 this->buffer_ = NULL; 1374 } 1375 } 1376 1377 // Read the DWARF string table. 1378 1379 bool 1380 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx) 1381 { 1382 Relobj* object = this->object_; 1383 1384 // If we don't have relocations, string_shndx will be 0, and 1385 // we'll have to hunt for the .debug_str section. 1386 if (string_shndx == 0) 1387 { 1388 for (unsigned int i = 1; i < this->object_->shnum(); ++i) 1389 { 1390 std::string name = object->section_name(i); 1391 if (name == ".debug_str" || name == ".zdebug_str") 1392 { 1393 string_shndx = i; 1394 this->string_output_section_offset_ = 1395 object->output_section_offset(i); 1396 break; 1397 } 1398 } 1399 if (string_shndx == 0) 1400 return false; 1401 } 1402 1403 if (this->owns_string_buffer_ && this->string_buffer_ != NULL) 1404 { 1405 delete[] this->string_buffer_; 1406 this->owns_string_buffer_ = false; 1407 } 1408 1409 // Get the secton contents and decompress if necessary. 1410 section_size_type buffer_size; 1411 const unsigned char* buffer = 1412 object->decompressed_section_contents(string_shndx, 1413 &buffer_size, 1414 &this->owns_string_buffer_); 1415 this->string_buffer_ = reinterpret_cast<const char*>(buffer); 1416 this->string_buffer_end_ = this->string_buffer_ + buffer_size; 1417 this->string_shndx_ = string_shndx; 1418 return true; 1419 } 1420 1421 // Read a possibly unaligned integer of SIZE. 1422 template <int valsize> 1423 inline typename elfcpp::Valtype_base<valsize>::Valtype 1424 Dwarf_info_reader::read_from_pointer(const unsigned char* source) 1425 { 1426 typename elfcpp::Valtype_base<valsize>::Valtype return_value; 1427 if (this->object_->is_big_endian()) 1428 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source); 1429 else 1430 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source); 1431 return return_value; 1432 } 1433 1434 // Read a possibly unaligned integer of SIZE. Update SOURCE after read. 1435 template <int valsize> 1436 inline typename elfcpp::Valtype_base<valsize>::Valtype 1437 Dwarf_info_reader::read_from_pointer(const unsigned char** source) 1438 { 1439 typename elfcpp::Valtype_base<valsize>::Valtype return_value; 1440 if (this->object_->is_big_endian()) 1441 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source); 1442 else 1443 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source); 1444 *source += valsize / 8; 1445 return return_value; 1446 } 1447 1448 // Look for a relocation at offset ATTR_OFF in the dwarf info, 1449 // and return the section index and offset of the target. 1450 1451 unsigned int 1452 Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off) 1453 { 1454 off_t value; 1455 attr_off += this->cu_offset_; 1456 unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value); 1457 if (shndx == 0) 1458 return 0; 1459 if (this->reloc_type_ == elfcpp::SHT_REL) 1460 *target_off += value; 1461 else 1462 *target_off = value; 1463 return shndx; 1464 } 1465 1466 // Return a string from the DWARF string table. 1467 1468 const char* 1469 Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx) 1470 { 1471 if (!this->read_string_table(string_shndx)) 1472 return NULL; 1473 1474 // Correct the offset. For incremental update links, we have a 1475 // relocated offset that is relative to the output section, but 1476 // here we need an offset relative to the input section. 1477 str_off -= this->string_output_section_offset_; 1478 1479 const char* p = this->string_buffer_ + str_off; 1480 1481 if (p < this->string_buffer_ || p >= this->string_buffer_end_) 1482 return NULL; 1483 1484 return p; 1485 } 1486 1487 // The following are default, do-nothing, implementations of the 1488 // hook methods normally provided by a derived class. We provide 1489 // default implementations rather than no implementation so that 1490 // a derived class needs to implement only the hooks that it needs 1491 // to use. 1492 1493 // Process a compilation unit and parse its child DIE. 1494 1495 void 1496 Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*) 1497 { 1498 } 1499 1500 // Process a type unit and parse its child DIE. 1501 1502 void 1503 Dwarf_info_reader::visit_type_unit(off_t, off_t, off_t, uint64_t, Dwarf_die*) 1504 { 1505 } 1506 1507 // Print a warning about a corrupt debug section. 1508 1509 void 1510 Dwarf_info_reader::warn_corrupt_debug_section() const 1511 { 1512 gold_warning(_("%s: corrupt debug info in %s"), 1513 this->object_->name().c_str(), 1514 this->object_->section_name(this->shndx_).c_str()); 1515 } 1516 1517 // class Sized_dwarf_line_info 1518 1519 struct LineStateMachine 1520 { 1521 int file_num; 1522 uint64_t address; 1523 int line_num; 1524 int column_num; 1525 unsigned int shndx; // the section address refers to 1526 bool is_stmt; // stmt means statement. 1527 bool basic_block; 1528 bool end_sequence; 1529 }; 1530 1531 static void 1532 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt) 1533 { 1534 lsm->file_num = 1; 1535 lsm->address = 0; 1536 lsm->line_num = 1; 1537 lsm->column_num = 0; 1538 lsm->shndx = -1U; 1539 lsm->is_stmt = default_is_stmt; 1540 lsm->basic_block = false; 1541 lsm->end_sequence = false; 1542 } 1543 1544 template<int size, bool big_endian> 1545 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info( 1546 Object* object, 1547 unsigned int read_shndx) 1548 : data_valid_(false), buffer_(NULL), buffer_start_(NULL), 1549 reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(), 1550 current_header_index_(-1) 1551 { 1552 unsigned int debug_shndx; 1553 1554 for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx) 1555 { 1556 // FIXME: do this more efficiently: section_name() isn't super-fast 1557 std::string name = object->section_name(debug_shndx); 1558 if (name == ".debug_line" || name == ".zdebug_line") 1559 { 1560 section_size_type buffer_size; 1561 bool is_new = false; 1562 this->buffer_ = object->decompressed_section_contents(debug_shndx, 1563 &buffer_size, 1564 &is_new); 1565 if (is_new) 1566 this->buffer_start_ = this->buffer_; 1567 this->buffer_end_ = this->buffer_ + buffer_size; 1568 break; 1569 } 1570 } 1571 if (this->buffer_ == NULL) 1572 return; 1573 1574 // Find the relocation section for ".debug_line". 1575 // We expect these for relobjs (.o's) but not dynobjs (.so's). 1576 unsigned int reloc_shndx = 0; 1577 for (unsigned int i = 0; i < object->shnum(); ++i) 1578 { 1579 unsigned int reloc_sh_type = object->section_type(i); 1580 if ((reloc_sh_type == elfcpp::SHT_REL 1581 || reloc_sh_type == elfcpp::SHT_RELA) 1582 && object->section_info(i) == debug_shndx) 1583 { 1584 reloc_shndx = i; 1585 this->track_relocs_type_ = reloc_sh_type; 1586 break; 1587 } 1588 } 1589 1590 // Finally, we need the symtab section to interpret the relocs. 1591 if (reloc_shndx != 0) 1592 { 1593 unsigned int symtab_shndx; 1594 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx) 1595 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB) 1596 { 1597 this->symtab_buffer_ = object->section_contents( 1598 symtab_shndx, &this->symtab_buffer_size_, false); 1599 break; 1600 } 1601 if (this->symtab_buffer_ == NULL) 1602 return; 1603 } 1604 1605 this->reloc_mapper_ = 1606 new Sized_elf_reloc_mapper<size, big_endian>(object, 1607 this->symtab_buffer_, 1608 this->symtab_buffer_size_); 1609 if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_)) 1610 return; 1611 1612 // Now that we have successfully read all the data, parse the debug 1613 // info. 1614 this->data_valid_ = true; 1615 this->read_line_mappings(read_shndx); 1616 } 1617 1618 // Read the DWARF header. 1619 1620 template<int size, bool big_endian> 1621 const unsigned char* 1622 Sized_dwarf_line_info<size, big_endian>::read_header_prolog( 1623 const unsigned char* lineptr) 1624 { 1625 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); 1626 lineptr += 4; 1627 1628 // In DWARF2/3, if the initial length is all 1 bits, then the offset 1629 // size is 8 and we need to read the next 8 bytes for the real length. 1630 if (initial_length == 0xffffffff) 1631 { 1632 header_.offset_size = 8; 1633 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); 1634 lineptr += 8; 1635 } 1636 else 1637 header_.offset_size = 4; 1638 1639 header_.total_length = initial_length; 1640 1641 gold_assert(lineptr + header_.total_length <= buffer_end_); 1642 1643 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr); 1644 lineptr += 2; 1645 1646 if (header_.offset_size == 4) 1647 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); 1648 else 1649 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); 1650 lineptr += header_.offset_size; 1651 1652 header_.min_insn_length = *lineptr; 1653 lineptr += 1; 1654 1655 if (header_.version < 4) 1656 header_.max_ops_per_insn = 1; 1657 else 1658 { 1659 // DWARF 4 added the maximum_operations_per_instruction field. 1660 header_.max_ops_per_insn = *lineptr; 1661 lineptr += 1; 1662 // TODO: Add support for values other than 1. 1663 gold_assert(header_.max_ops_per_insn == 1); 1664 } 1665 1666 header_.default_is_stmt = *lineptr; 1667 lineptr += 1; 1668 1669 header_.line_base = *reinterpret_cast<const signed char*>(lineptr); 1670 lineptr += 1; 1671 1672 header_.line_range = *lineptr; 1673 lineptr += 1; 1674 1675 header_.opcode_base = *lineptr; 1676 lineptr += 1; 1677 1678 header_.std_opcode_lengths.resize(header_.opcode_base + 1); 1679 header_.std_opcode_lengths[0] = 0; 1680 for (int i = 1; i < header_.opcode_base; i++) 1681 { 1682 header_.std_opcode_lengths[i] = *lineptr; 1683 lineptr += 1; 1684 } 1685 1686 return lineptr; 1687 } 1688 1689 // The header for a debug_line section is mildly complicated, because 1690 // the line info is very tightly encoded. 1691 1692 template<int size, bool big_endian> 1693 const unsigned char* 1694 Sized_dwarf_line_info<size, big_endian>::read_header_tables( 1695 const unsigned char* lineptr) 1696 { 1697 ++this->current_header_index_; 1698 1699 // Create a new directories_ entry and a new files_ entry for our new 1700 // header. We initialize each with a single empty element, because 1701 // dwarf indexes directory and filenames starting at 1. 1702 gold_assert(static_cast<int>(this->directories_.size()) 1703 == this->current_header_index_); 1704 gold_assert(static_cast<int>(this->files_.size()) 1705 == this->current_header_index_); 1706 this->directories_.push_back(std::vector<std::string>(1)); 1707 this->files_.push_back(std::vector<std::pair<int, std::string> >(1)); 1708 1709 // It is legal for the directory entry table to be empty. 1710 if (*lineptr) 1711 { 1712 int dirindex = 1; 1713 while (*lineptr) 1714 { 1715 const char* dirname = reinterpret_cast<const char*>(lineptr); 1716 gold_assert(dirindex 1717 == static_cast<int>(this->directories_.back().size())); 1718 this->directories_.back().push_back(dirname); 1719 lineptr += this->directories_.back().back().size() + 1; 1720 dirindex++; 1721 } 1722 } 1723 lineptr++; 1724 1725 // It is also legal for the file entry table to be empty. 1726 if (*lineptr) 1727 { 1728 int fileindex = 1; 1729 size_t len; 1730 while (*lineptr) 1731 { 1732 const char* filename = reinterpret_cast<const char*>(lineptr); 1733 lineptr += strlen(filename) + 1; 1734 1735 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len); 1736 lineptr += len; 1737 1738 if (dirindex >= this->directories_.back().size()) 1739 dirindex = 0; 1740 int dirindexi = static_cast<int>(dirindex); 1741 1742 read_unsigned_LEB_128(lineptr, &len); // mod_time 1743 lineptr += len; 1744 1745 read_unsigned_LEB_128(lineptr, &len); // filelength 1746 lineptr += len; 1747 1748 gold_assert(fileindex 1749 == static_cast<int>(this->files_.back().size())); 1750 this->files_.back().push_back(std::make_pair(dirindexi, filename)); 1751 fileindex++; 1752 } 1753 } 1754 lineptr++; 1755 1756 return lineptr; 1757 } 1758 1759 // Process a single opcode in the .debug.line structure. 1760 1761 template<int size, bool big_endian> 1762 bool 1763 Sized_dwarf_line_info<size, big_endian>::process_one_opcode( 1764 const unsigned char* start, struct LineStateMachine* lsm, size_t* len) 1765 { 1766 size_t oplen = 0; 1767 size_t templen; 1768 unsigned char opcode = *start; 1769 oplen++; 1770 start++; 1771 1772 // If the opcode is great than the opcode_base, it is a special 1773 // opcode. Most line programs consist mainly of special opcodes. 1774 if (opcode >= header_.opcode_base) 1775 { 1776 opcode -= header_.opcode_base; 1777 const int advance_address = ((opcode / header_.line_range) 1778 * header_.min_insn_length); 1779 lsm->address += advance_address; 1780 1781 const int advance_line = ((opcode % header_.line_range) 1782 + header_.line_base); 1783 lsm->line_num += advance_line; 1784 lsm->basic_block = true; 1785 *len = oplen; 1786 return true; 1787 } 1788 1789 // Otherwise, we have the regular opcodes 1790 switch (opcode) 1791 { 1792 case elfcpp::DW_LNS_copy: 1793 lsm->basic_block = false; 1794 *len = oplen; 1795 return true; 1796 1797 case elfcpp::DW_LNS_advance_pc: 1798 { 1799 const uint64_t advance_address 1800 = read_unsigned_LEB_128(start, &templen); 1801 oplen += templen; 1802 lsm->address += header_.min_insn_length * advance_address; 1803 } 1804 break; 1805 1806 case elfcpp::DW_LNS_advance_line: 1807 { 1808 const uint64_t advance_line = read_signed_LEB_128(start, &templen); 1809 oplen += templen; 1810 lsm->line_num += advance_line; 1811 } 1812 break; 1813 1814 case elfcpp::DW_LNS_set_file: 1815 { 1816 const uint64_t fileno = read_unsigned_LEB_128(start, &templen); 1817 oplen += templen; 1818 lsm->file_num = fileno; 1819 } 1820 break; 1821 1822 case elfcpp::DW_LNS_set_column: 1823 { 1824 const uint64_t colno = read_unsigned_LEB_128(start, &templen); 1825 oplen += templen; 1826 lsm->column_num = colno; 1827 } 1828 break; 1829 1830 case elfcpp::DW_LNS_negate_stmt: 1831 lsm->is_stmt = !lsm->is_stmt; 1832 break; 1833 1834 case elfcpp::DW_LNS_set_basic_block: 1835 lsm->basic_block = true; 1836 break; 1837 1838 case elfcpp::DW_LNS_fixed_advance_pc: 1839 { 1840 int advance_address; 1841 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start); 1842 oplen += 2; 1843 lsm->address += advance_address; 1844 } 1845 break; 1846 1847 case elfcpp::DW_LNS_const_add_pc: 1848 { 1849 const int advance_address = (header_.min_insn_length 1850 * ((255 - header_.opcode_base) 1851 / header_.line_range)); 1852 lsm->address += advance_address; 1853 } 1854 break; 1855 1856 case elfcpp::DW_LNS_extended_op: 1857 { 1858 const uint64_t extended_op_len 1859 = read_unsigned_LEB_128(start, &templen); 1860 start += templen; 1861 oplen += templen + extended_op_len; 1862 1863 const unsigned char extended_op = *start; 1864 start++; 1865 1866 switch (extended_op) 1867 { 1868 case elfcpp::DW_LNE_end_sequence: 1869 // This means that the current byte is the one immediately 1870 // after a set of instructions. Record the current line 1871 // for up to one less than the current address. 1872 lsm->line_num = -1; 1873 lsm->end_sequence = true; 1874 *len = oplen; 1875 return true; 1876 1877 case elfcpp::DW_LNE_set_address: 1878 { 1879 lsm->address = 1880 elfcpp::Swap_unaligned<size, big_endian>::readval(start); 1881 typename Reloc_map::const_iterator it 1882 = this->reloc_map_.find(start - this->buffer_); 1883 if (it != reloc_map_.end()) 1884 { 1885 // If this is a SHT_RELA section, then ignore the 1886 // section contents. This assumes that this is a 1887 // straight reloc which just uses the reloc addend. 1888 // The reloc addend has already been included in the 1889 // symbol value. 1890 if (this->track_relocs_type_ == elfcpp::SHT_RELA) 1891 lsm->address = 0; 1892 // Add in the symbol value. 1893 lsm->address += it->second.second; 1894 lsm->shndx = it->second.first; 1895 } 1896 else 1897 { 1898 // If we're a normal .o file, with relocs, every 1899 // set_address should have an associated relocation. 1900 if (this->input_is_relobj()) 1901 this->data_valid_ = false; 1902 } 1903 break; 1904 } 1905 case elfcpp::DW_LNE_define_file: 1906 { 1907 const char* filename = reinterpret_cast<const char*>(start); 1908 templen = strlen(filename) + 1; 1909 start += templen; 1910 1911 uint64_t dirindex = read_unsigned_LEB_128(start, &templen); 1912 1913 if (dirindex >= this->directories_.back().size()) 1914 dirindex = 0; 1915 int dirindexi = static_cast<int>(dirindex); 1916 1917 // This opcode takes two additional ULEB128 parameters 1918 // (mod_time and filelength), but we don't use those 1919 // values. Because OPLEN already tells us how far to 1920 // skip to the next opcode, we don't need to read 1921 // them at all. 1922 1923 this->files_.back().push_back(std::make_pair(dirindexi, 1924 filename)); 1925 } 1926 break; 1927 } 1928 } 1929 break; 1930 1931 default: 1932 { 1933 // Ignore unknown opcode silently 1934 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++) 1935 { 1936 size_t templen; 1937 read_unsigned_LEB_128(start, &templen); 1938 start += templen; 1939 oplen += templen; 1940 } 1941 } 1942 break; 1943 } 1944 *len = oplen; 1945 return false; 1946 } 1947 1948 // Read the debug information at LINEPTR and store it in the line 1949 // number map. 1950 1951 template<int size, bool big_endian> 1952 unsigned const char* 1953 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr, 1954 unsigned int shndx) 1955 { 1956 struct LineStateMachine lsm; 1957 1958 // LENGTHSTART is the place the length field is based on. It is the 1959 // point in the header after the initial length field. 1960 const unsigned char* lengthstart = buffer_; 1961 1962 // In 64 bit dwarf, the initial length is 12 bytes, because of the 1963 // 0xffffffff at the start. 1964 if (header_.offset_size == 8) 1965 lengthstart += 12; 1966 else 1967 lengthstart += 4; 1968 1969 while (lineptr < lengthstart + header_.total_length) 1970 { 1971 ResetLineStateMachine(&lsm, header_.default_is_stmt); 1972 while (!lsm.end_sequence) 1973 { 1974 size_t oplength; 1975 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength); 1976 if (add_line 1977 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx)) 1978 { 1979 Offset_to_lineno_entry entry 1980 = { static_cast<off_t>(lsm.address), 1981 this->current_header_index_, 1982 static_cast<unsigned int>(lsm.file_num), 1983 true, lsm.line_num }; 1984 std::vector<Offset_to_lineno_entry>& 1985 map(this->line_number_map_[lsm.shndx]); 1986 // If we see two consecutive entries with the same 1987 // offset and a real line number, then mark the first 1988 // one as non-canonical. 1989 if (!map.empty() 1990 && (map.back().offset == static_cast<off_t>(lsm.address)) 1991 && lsm.line_num != -1 1992 && map.back().line_num != -1) 1993 map.back().last_line_for_offset = false; 1994 map.push_back(entry); 1995 } 1996 lineptr += oplength; 1997 } 1998 } 1999 2000 return lengthstart + header_.total_length; 2001 } 2002 2003 // Read the relocations into a Reloc_map. 2004 2005 template<int size, bool big_endian> 2006 void 2007 Sized_dwarf_line_info<size, big_endian>::read_relocs() 2008 { 2009 if (this->symtab_buffer_ == NULL) 2010 return; 2011 2012 off_t value; 2013 off_t reloc_offset; 2014 while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1) 2015 { 2016 const unsigned int shndx = 2017 this->reloc_mapper_->get_reloc_target(reloc_offset, &value); 2018 2019 // There is no reason to record non-ordinary section indexes, or 2020 // SHN_UNDEF, because they will never match the real section. 2021 if (shndx != 0) 2022 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value); 2023 2024 this->reloc_mapper_->advance(reloc_offset + 1); 2025 } 2026 } 2027 2028 // Read the line number info. 2029 2030 template<int size, bool big_endian> 2031 void 2032 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx) 2033 { 2034 gold_assert(this->data_valid_ == true); 2035 2036 this->read_relocs(); 2037 while (this->buffer_ < this->buffer_end_) 2038 { 2039 const unsigned char* lineptr = this->buffer_; 2040 lineptr = this->read_header_prolog(lineptr); 2041 lineptr = this->read_header_tables(lineptr); 2042 lineptr = this->read_lines(lineptr, shndx); 2043 this->buffer_ = lineptr; 2044 } 2045 2046 // Sort the lines numbers, so addr2line can use binary search. 2047 for (typename Lineno_map::iterator it = line_number_map_.begin(); 2048 it != line_number_map_.end(); 2049 ++it) 2050 // Each vector needs to be sorted by offset. 2051 std::sort(it->second.begin(), it->second.end()); 2052 } 2053 2054 // Some processing depends on whether the input is a .o file or not. 2055 // For instance, .o files have relocs, and have .debug_lines 2056 // information on a per section basis. .so files, on the other hand, 2057 // lack relocs, and offsets are unique, so we can ignore the section 2058 // information. 2059 2060 template<int size, bool big_endian> 2061 bool 2062 Sized_dwarf_line_info<size, big_endian>::input_is_relobj() 2063 { 2064 // Only .o files have relocs and the symtab buffer that goes with them. 2065 return this->symtab_buffer_ != NULL; 2066 } 2067 2068 // Given an Offset_to_lineno_entry vector, and an offset, figure out 2069 // if the offset points into a function according to the vector (see 2070 // comments below for the algorithm). If it does, return an iterator 2071 // into the vector that points to the line-number that contains that 2072 // offset. If not, it returns vector::end(). 2073 2074 static std::vector<Offset_to_lineno_entry>::const_iterator 2075 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets, 2076 off_t offset) 2077 { 2078 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 }; 2079 2080 // lower_bound() returns the smallest offset which is >= lookup_key. 2081 // If no offset in offsets is >= lookup_key, returns end(). 2082 std::vector<Offset_to_lineno_entry>::const_iterator it 2083 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key); 2084 2085 // This code is easiest to understand with a concrete example. 2086 // Here's a possible offsets array: 2087 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0 2088 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1 2089 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2 2090 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3 2091 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4 2092 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5 2093 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6 2094 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7 2095 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8 2096 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9 2097 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10 2098 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11 2099 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12 2100 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13 2101 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14 2102 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15 2103 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16 2104 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17 2105 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18 2106 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19 2107 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20 2108 // The entries with line_num == -1 mark the end of a function: the 2109 // associated offset is one past the last instruction in the 2110 // function. This can correspond to the beginning of the next 2111 // function (as is true for offset 3232); alternately, there can be 2112 // a gap between the end of one function and the start of the next 2113 // (as is true for some others, most obviously from 3236->5764). 2114 // 2115 // Case 1: lookup_key has offset == 10. lower_bound returns 2116 // offsets[0]. Since it's not an exact match and we're 2117 // at the beginning of offsets, we return end() (invalid). 2118 // Case 2: lookup_key has offset 10000. lower_bound returns 2119 // offset[21] (end()). We return end() (invalid). 2120 // Case 3: lookup_key has offset == 3211. lower_bound matches 2121 // offsets[0] exactly, and that's the entry we return. 2122 // Case 4: lookup_key has offset == 3232. lower_bound returns 2123 // offsets[4]. That's an exact match, but indicates 2124 // end-of-function. We check if offsets[5] is also an 2125 // exact match but not end-of-function. It is, so we 2126 // return offsets[5]. 2127 // Case 5: lookup_key has offset == 3214. lower_bound returns 2128 // offsets[1]. Since it's not an exact match, we back 2129 // up to the offset that's < lookup_key, offsets[0]. 2130 // We note offsets[0] is a valid entry (not end-of-function), 2131 // so that's the entry we return. 2132 // Case 6: lookup_key has offset == 4000. lower_bound returns 2133 // offsets[8]. Since it's not an exact match, we back 2134 // up to offsets[7]. Since offsets[7] indicates 2135 // end-of-function, we know lookup_key is between 2136 // functions, so we return end() (not a valid offset). 2137 // Case 7: lookup_key has offset == 5794. lower_bound returns 2138 // offsets[19]. Since it's not an exact match, we back 2139 // up to offsets[16]. Note we back up to the *first* 2140 // entry with offset 5793, not just offsets[19-1]. 2141 // We note offsets[16] is a valid entry, so we return it. 2142 // If offsets[16] had had line_num == -1, we would have 2143 // checked offsets[17]. The reason for this is that 2144 // 16 and 17 can be in an arbitrary order, since we sort 2145 // only by offset and last_line_for_offset. (Note it 2146 // doesn't help to use line_number as a tertiary sort key, 2147 // since sometimes we want the -1 to be first and sometimes 2148 // we want it to be last.) 2149 2150 // This deals with cases (1) and (2). 2151 if ((it == offsets->begin() && offset < it->offset) 2152 || it == offsets->end()) 2153 return offsets->end(); 2154 2155 // This deals with cases (3) and (4). 2156 if (offset == it->offset) 2157 { 2158 while (it != offsets->end() 2159 && it->offset == offset 2160 && it->line_num == -1) 2161 ++it; 2162 if (it == offsets->end() || it->offset != offset) 2163 return offsets->end(); 2164 else 2165 return it; 2166 } 2167 2168 // This handles the first part of case (7) -- we back up to the 2169 // *first* entry that has the offset that's behind us. 2170 gold_assert(it != offsets->begin()); 2171 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it; 2172 --it; 2173 const off_t range_value = it->offset; 2174 while (it != offsets->begin() && (it-1)->offset == range_value) 2175 --it; 2176 2177 // This handles cases (5), (6), and (7): if any entry in the 2178 // equal_range [it, range_end) has a line_num != -1, it's a valid 2179 // match. If not, we're not in a function. The line number we saw 2180 // last for an offset will be sorted first, so it'll get returned if 2181 // it's present. 2182 for (; it != range_end; ++it) 2183 if (it->line_num != -1) 2184 return it; 2185 return offsets->end(); 2186 } 2187 2188 // Returns the canonical filename:lineno for the address passed in. 2189 // If other_lines is not NULL, appends the non-canonical lines 2190 // assigned to the same address. 2191 2192 template<int size, bool big_endian> 2193 std::string 2194 Sized_dwarf_line_info<size, big_endian>::do_addr2line( 2195 unsigned int shndx, 2196 off_t offset, 2197 std::vector<std::string>* other_lines) 2198 { 2199 if (this->data_valid_ == false) 2200 return ""; 2201 2202 const std::vector<Offset_to_lineno_entry>* offsets; 2203 // If we do not have reloc information, then our input is a .so or 2204 // some similar data structure where all the information is held in 2205 // the offset. In that case, we ignore the input shndx. 2206 if (this->input_is_relobj()) 2207 offsets = &this->line_number_map_[shndx]; 2208 else 2209 offsets = &this->line_number_map_[-1U]; 2210 if (offsets->empty()) 2211 return ""; 2212 2213 typename std::vector<Offset_to_lineno_entry>::const_iterator it 2214 = offset_to_iterator(offsets, offset); 2215 if (it == offsets->end()) 2216 return ""; 2217 2218 std::string result = this->format_file_lineno(*it); 2219 gold_debug(DEBUG_LOCATION, "do_addr2line: canonical result: %s", 2220 result.c_str()); 2221 if (other_lines != NULL) 2222 { 2223 unsigned int last_file_num = it->file_num; 2224 int last_line_num = it->line_num; 2225 // Return up to 4 more locations from the beginning of the function 2226 // for fuzzy matching. 2227 for (++it; it != offsets->end(); ++it) 2228 { 2229 if (it->offset == offset && it->line_num == -1) 2230 continue; // The end of a previous function. 2231 if (it->line_num == -1) 2232 break; // The end of the current function. 2233 if (it->file_num != last_file_num || it->line_num != last_line_num) 2234 { 2235 other_lines->push_back(this->format_file_lineno(*it)); 2236 gold_debug(DEBUG_LOCATION, "do_addr2line: other: %s", 2237 other_lines->back().c_str()); 2238 last_file_num = it->file_num; 2239 last_line_num = it->line_num; 2240 } 2241 if (it->offset > offset && other_lines->size() >= 4) 2242 break; 2243 } 2244 } 2245 2246 return result; 2247 } 2248 2249 // Convert the file_num + line_num into a string. 2250 2251 template<int size, bool big_endian> 2252 std::string 2253 Sized_dwarf_line_info<size, big_endian>::format_file_lineno( 2254 const Offset_to_lineno_entry& loc) const 2255 { 2256 std::string ret; 2257 2258 gold_assert(loc.header_num < static_cast<int>(this->files_.size())); 2259 gold_assert(loc.file_num 2260 < static_cast<unsigned int>(this->files_[loc.header_num].size())); 2261 const std::pair<int, std::string>& filename_pair 2262 = this->files_[loc.header_num][loc.file_num]; 2263 const std::string& filename = filename_pair.second; 2264 2265 gold_assert(loc.header_num < static_cast<int>(this->directories_.size())); 2266 gold_assert(filename_pair.first 2267 < static_cast<int>(this->directories_[loc.header_num].size())); 2268 const std::string& dirname 2269 = this->directories_[loc.header_num][filename_pair.first]; 2270 2271 if (!dirname.empty()) 2272 { 2273 ret += dirname; 2274 ret += "/"; 2275 } 2276 ret += filename; 2277 if (ret.empty()) 2278 ret = "(unknown)"; 2279 2280 char buffer[64]; // enough to hold a line number 2281 snprintf(buffer, sizeof(buffer), "%d", loc.line_num); 2282 ret += ":"; 2283 ret += buffer; 2284 2285 return ret; 2286 } 2287 2288 // Dwarf_line_info routines. 2289 2290 static unsigned int next_generation_count = 0; 2291 2292 struct Addr2line_cache_entry 2293 { 2294 Object* object; 2295 unsigned int shndx; 2296 Dwarf_line_info* dwarf_line_info; 2297 unsigned int generation_count; 2298 unsigned int access_count; 2299 2300 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d) 2301 : object(o), shndx(s), dwarf_line_info(d), 2302 generation_count(next_generation_count), access_count(0) 2303 { 2304 if (next_generation_count < (1U << 31)) 2305 ++next_generation_count; 2306 } 2307 }; 2308 // We expect this cache to be small, so don't bother with a hashtable 2309 // or priority queue or anything: just use a simple vector. 2310 static std::vector<Addr2line_cache_entry> addr2line_cache; 2311 2312 std::string 2313 Dwarf_line_info::one_addr2line(Object* object, 2314 unsigned int shndx, off_t offset, 2315 size_t cache_size, 2316 std::vector<std::string>* other_lines) 2317 { 2318 Dwarf_line_info* lineinfo = NULL; 2319 std::vector<Addr2line_cache_entry>::iterator it; 2320 2321 // First, check the cache. If we hit, update the counts. 2322 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) 2323 { 2324 if (it->object == object && it->shndx == shndx) 2325 { 2326 lineinfo = it->dwarf_line_info; 2327 it->generation_count = next_generation_count; 2328 // We cap generation_count at 2^31 -1 to avoid overflow. 2329 if (next_generation_count < (1U << 31)) 2330 ++next_generation_count; 2331 // We cap access_count at 31 so 2^access_count doesn't overflow 2332 if (it->access_count < 31) 2333 ++it->access_count; 2334 break; 2335 } 2336 } 2337 2338 // If we don't hit the cache, create a new object and insert into the 2339 // cache. 2340 if (lineinfo == NULL) 2341 { 2342 switch (parameters->size_and_endianness()) 2343 { 2344 #ifdef HAVE_TARGET_32_LITTLE 2345 case Parameters::TARGET_32_LITTLE: 2346 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break; 2347 #endif 2348 #ifdef HAVE_TARGET_32_BIG 2349 case Parameters::TARGET_32_BIG: 2350 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break; 2351 #endif 2352 #ifdef HAVE_TARGET_64_LITTLE 2353 case Parameters::TARGET_64_LITTLE: 2354 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break; 2355 #endif 2356 #ifdef HAVE_TARGET_64_BIG 2357 case Parameters::TARGET_64_BIG: 2358 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break; 2359 #endif 2360 default: 2361 gold_unreachable(); 2362 } 2363 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo)); 2364 } 2365 2366 // Now that we have our object, figure out the answer 2367 std::string retval = lineinfo->addr2line(shndx, offset, other_lines); 2368 2369 // Finally, if our cache has grown too big, delete old objects. We 2370 // assume the common (probably only) case is deleting only one object. 2371 // We use a pretty simple scheme to evict: function of LRU and MFU. 2372 while (addr2line_cache.size() > cache_size) 2373 { 2374 unsigned int lowest_score = ~0U; 2375 std::vector<Addr2line_cache_entry>::iterator lowest 2376 = addr2line_cache.end(); 2377 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) 2378 { 2379 const unsigned int score = (it->generation_count 2380 + (1U << it->access_count)); 2381 if (score < lowest_score) 2382 { 2383 lowest_score = score; 2384 lowest = it; 2385 } 2386 } 2387 if (lowest != addr2line_cache.end()) 2388 { 2389 delete lowest->dwarf_line_info; 2390 addr2line_cache.erase(lowest); 2391 } 2392 } 2393 2394 return retval; 2395 } 2396 2397 void 2398 Dwarf_line_info::clear_addr2line_cache() 2399 { 2400 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin(); 2401 it != addr2line_cache.end(); 2402 ++it) 2403 delete it->dwarf_line_info; 2404 addr2line_cache.clear(); 2405 } 2406 2407 #ifdef HAVE_TARGET_32_LITTLE 2408 template 2409 class Sized_dwarf_line_info<32, false>; 2410 #endif 2411 2412 #ifdef HAVE_TARGET_32_BIG 2413 template 2414 class Sized_dwarf_line_info<32, true>; 2415 #endif 2416 2417 #ifdef HAVE_TARGET_64_LITTLE 2418 template 2419 class Sized_dwarf_line_info<64, false>; 2420 #endif 2421 2422 #ifdef HAVE_TARGET_64_BIG 2423 template 2424 class Sized_dwarf_line_info<64, true>; 2425 #endif 2426 2427 } // End namespace gold. 2428