1 // dwarf_reader.cc -- parse dwarf2/3 debug information 2 3 // Copyright 2007, 2008 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 <vector> 27 28 #include "elfcpp_swap.h" 29 #include "dwarf.h" 30 #include "object.h" 31 #include "parameters.h" 32 #include "reloc.h" 33 #include "dwarf_reader.h" 34 35 namespace gold { 36 37 // Read an unsigned LEB128 number. Each byte contains 7 bits of 38 // information, plus one bit saying whether the number continues or 39 // not. 40 41 uint64_t 42 read_unsigned_LEB_128(const unsigned char* buffer, size_t* len) 43 { 44 uint64_t result = 0; 45 size_t num_read = 0; 46 unsigned int shift = 0; 47 unsigned char byte; 48 49 do 50 { 51 if (num_read >= 64 / 7) 52 { 53 gold_warning(_("Unusually large LEB128 decoded, " 54 "debug information may be corrupted")); 55 break; 56 } 57 byte = *buffer++; 58 num_read++; 59 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift; 60 shift += 7; 61 } 62 while (byte & 0x80); 63 64 *len = num_read; 65 66 return result; 67 } 68 69 // Read a signed LEB128 number. These are like regular LEB128 70 // numbers, except the last byte may have a sign bit set. 71 72 int64_t 73 read_signed_LEB_128(const unsigned char* buffer, size_t* len) 74 { 75 int64_t result = 0; 76 int shift = 0; 77 size_t num_read = 0; 78 unsigned char byte; 79 80 do 81 { 82 if (num_read >= 64 / 7) 83 { 84 gold_warning(_("Unusually large LEB128 decoded, " 85 "debug information may be corrupted")); 86 break; 87 } 88 byte = *buffer++; 89 num_read++; 90 result |= (static_cast<uint64_t>(byte & 0x7f) << shift); 91 shift += 7; 92 } 93 while (byte & 0x80); 94 95 if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40)) 96 result |= -((static_cast<int64_t>(1)) << shift); 97 *len = num_read; 98 return result; 99 } 100 101 // This is the format of a DWARF2/3 line state machine that we process 102 // opcodes using. There is no need for anything outside the lineinfo 103 // processor to know how this works. 104 105 struct LineStateMachine 106 { 107 int file_num; 108 uint64_t address; 109 int line_num; 110 int column_num; 111 unsigned int shndx; // the section address refers to 112 bool is_stmt; // stmt means statement. 113 bool basic_block; 114 bool end_sequence; 115 }; 116 117 static void 118 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt) 119 { 120 lsm->file_num = 1; 121 lsm->address = 0; 122 lsm->line_num = 1; 123 lsm->column_num = 0; 124 lsm->shndx = -1U; 125 lsm->is_stmt = default_is_stmt; 126 lsm->basic_block = false; 127 lsm->end_sequence = false; 128 } 129 130 template<int size, bool big_endian> 131 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object, 132 off_t read_shndx) 133 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL), 134 directories_(), files_(), current_header_index_(-1) 135 { 136 unsigned int debug_shndx; 137 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx) 138 // FIXME: do this more efficiently: section_name() isn't super-fast 139 if (object->section_name(debug_shndx) == ".debug_line") 140 { 141 section_size_type buffer_size; 142 this->buffer_ = object->section_contents(debug_shndx, &buffer_size, 143 false); 144 this->buffer_end_ = this->buffer_ + buffer_size; 145 break; 146 } 147 if (this->buffer_ == NULL) 148 return; 149 150 // Find the relocation section for ".debug_line". 151 // We expect these for relobjs (.o's) but not dynobjs (.so's). 152 bool got_relocs = false; 153 for (unsigned int reloc_shndx = 0; 154 reloc_shndx < object->shnum(); 155 ++reloc_shndx) 156 { 157 unsigned int reloc_sh_type = object->section_type(reloc_shndx); 158 if ((reloc_sh_type == elfcpp::SHT_REL 159 || reloc_sh_type == elfcpp::SHT_RELA) 160 && object->section_info(reloc_shndx) == debug_shndx) 161 { 162 got_relocs = this->track_relocs_.initialize(object, reloc_shndx, 163 reloc_sh_type); 164 break; 165 } 166 } 167 168 // Finally, we need the symtab section to interpret the relocs. 169 if (got_relocs) 170 { 171 unsigned int symtab_shndx; 172 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx) 173 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB) 174 { 175 this->symtab_buffer_ = object->section_contents( 176 symtab_shndx, &this->symtab_buffer_size_, false); 177 break; 178 } 179 if (this->symtab_buffer_ == NULL) 180 return; 181 } 182 183 // Now that we have successfully read all the data, parse the debug 184 // info. 185 this->data_valid_ = true; 186 this->read_line_mappings(object, read_shndx); 187 } 188 189 // Read the DWARF header. 190 191 template<int size, bool big_endian> 192 const unsigned char* 193 Sized_dwarf_line_info<size, big_endian>::read_header_prolog( 194 const unsigned char* lineptr) 195 { 196 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); 197 lineptr += 4; 198 199 // In DWARF2/3, if the initial length is all 1 bits, then the offset 200 // size is 8 and we need to read the next 8 bytes for the real length. 201 if (initial_length == 0xffffffff) 202 { 203 header_.offset_size = 8; 204 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); 205 lineptr += 8; 206 } 207 else 208 header_.offset_size = 4; 209 210 header_.total_length = initial_length; 211 212 gold_assert(lineptr + header_.total_length <= buffer_end_); 213 214 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr); 215 lineptr += 2; 216 217 if (header_.offset_size == 4) 218 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); 219 else 220 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); 221 lineptr += header_.offset_size; 222 223 header_.min_insn_length = *lineptr; 224 lineptr += 1; 225 226 header_.default_is_stmt = *lineptr; 227 lineptr += 1; 228 229 header_.line_base = *reinterpret_cast<const signed char*>(lineptr); 230 lineptr += 1; 231 232 header_.line_range = *lineptr; 233 lineptr += 1; 234 235 header_.opcode_base = *lineptr; 236 lineptr += 1; 237 238 header_.std_opcode_lengths.reserve(header_.opcode_base + 1); 239 header_.std_opcode_lengths[0] = 0; 240 for (int i = 1; i < header_.opcode_base; i++) 241 { 242 header_.std_opcode_lengths[i] = *lineptr; 243 lineptr += 1; 244 } 245 246 return lineptr; 247 } 248 249 // The header for a debug_line section is mildly complicated, because 250 // the line info is very tightly encoded. 251 252 template<int size, bool big_endian> 253 const unsigned char* 254 Sized_dwarf_line_info<size, big_endian>::read_header_tables( 255 const unsigned char* lineptr) 256 { 257 ++this->current_header_index_; 258 259 // Create a new directories_ entry and a new files_ entry for our new 260 // header. We initialize each with a single empty element, because 261 // dwarf indexes directory and filenames starting at 1. 262 gold_assert(static_cast<int>(this->directories_.size()) 263 == this->current_header_index_); 264 gold_assert(static_cast<int>(this->files_.size()) 265 == this->current_header_index_); 266 this->directories_.push_back(std::vector<std::string>(1)); 267 this->files_.push_back(std::vector<std::pair<int, std::string> >(1)); 268 269 // It is legal for the directory entry table to be empty. 270 if (*lineptr) 271 { 272 int dirindex = 1; 273 while (*lineptr) 274 { 275 const char* dirname = reinterpret_cast<const char*>(lineptr); 276 gold_assert(dirindex 277 == static_cast<int>(this->directories_.back().size())); 278 this->directories_.back().push_back(dirname); 279 lineptr += this->directories_.back().back().size() + 1; 280 dirindex++; 281 } 282 } 283 lineptr++; 284 285 // It is also legal for the file entry table to be empty. 286 if (*lineptr) 287 { 288 int fileindex = 1; 289 size_t len; 290 while (*lineptr) 291 { 292 const char* filename = reinterpret_cast<const char*>(lineptr); 293 lineptr += strlen(filename) + 1; 294 295 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len); 296 lineptr += len; 297 298 if (dirindex >= this->directories_.back().size()) 299 dirindex = 0; 300 int dirindexi = static_cast<int>(dirindex); 301 302 read_unsigned_LEB_128(lineptr, &len); // mod_time 303 lineptr += len; 304 305 read_unsigned_LEB_128(lineptr, &len); // filelength 306 lineptr += len; 307 308 gold_assert(fileindex 309 == static_cast<int>(this->files_.back().size())); 310 this->files_.back().push_back(std::make_pair(dirindexi, filename)); 311 fileindex++; 312 } 313 } 314 lineptr++; 315 316 return lineptr; 317 } 318 319 // Process a single opcode in the .debug.line structure. 320 321 // Templating on size and big_endian would yield more efficient (and 322 // simpler) code, but would bloat the binary. Speed isn't important 323 // here. 324 325 template<int size, bool big_endian> 326 bool 327 Sized_dwarf_line_info<size, big_endian>::process_one_opcode( 328 const unsigned char* start, struct LineStateMachine* lsm, size_t* len) 329 { 330 size_t oplen = 0; 331 size_t templen; 332 unsigned char opcode = *start; 333 oplen++; 334 start++; 335 336 // If the opcode is great than the opcode_base, it is a special 337 // opcode. Most line programs consist mainly of special opcodes. 338 if (opcode >= header_.opcode_base) 339 { 340 opcode -= header_.opcode_base; 341 const int advance_address = ((opcode / header_.line_range) 342 * header_.min_insn_length); 343 lsm->address += advance_address; 344 345 const int advance_line = ((opcode % header_.line_range) 346 + header_.line_base); 347 lsm->line_num += advance_line; 348 lsm->basic_block = true; 349 *len = oplen; 350 return true; 351 } 352 353 // Otherwise, we have the regular opcodes 354 switch (opcode) 355 { 356 case elfcpp::DW_LNS_copy: 357 lsm->basic_block = false; 358 *len = oplen; 359 return true; 360 361 case elfcpp::DW_LNS_advance_pc: 362 { 363 const uint64_t advance_address 364 = read_unsigned_LEB_128(start, &templen); 365 oplen += templen; 366 lsm->address += header_.min_insn_length * advance_address; 367 } 368 break; 369 370 case elfcpp::DW_LNS_advance_line: 371 { 372 const uint64_t advance_line = read_signed_LEB_128(start, &templen); 373 oplen += templen; 374 lsm->line_num += advance_line; 375 } 376 break; 377 378 case elfcpp::DW_LNS_set_file: 379 { 380 const uint64_t fileno = read_unsigned_LEB_128(start, &templen); 381 oplen += templen; 382 lsm->file_num = fileno; 383 } 384 break; 385 386 case elfcpp::DW_LNS_set_column: 387 { 388 const uint64_t colno = read_unsigned_LEB_128(start, &templen); 389 oplen += templen; 390 lsm->column_num = colno; 391 } 392 break; 393 394 case elfcpp::DW_LNS_negate_stmt: 395 lsm->is_stmt = !lsm->is_stmt; 396 break; 397 398 case elfcpp::DW_LNS_set_basic_block: 399 lsm->basic_block = true; 400 break; 401 402 case elfcpp::DW_LNS_fixed_advance_pc: 403 { 404 int advance_address; 405 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start); 406 oplen += 2; 407 lsm->address += advance_address; 408 } 409 break; 410 411 case elfcpp::DW_LNS_const_add_pc: 412 { 413 const int advance_address = (header_.min_insn_length 414 * ((255 - header_.opcode_base) 415 / header_.line_range)); 416 lsm->address += advance_address; 417 } 418 break; 419 420 case elfcpp::DW_LNS_extended_op: 421 { 422 const uint64_t extended_op_len 423 = read_unsigned_LEB_128(start, &templen); 424 start += templen; 425 oplen += templen + extended_op_len; 426 427 const unsigned char extended_op = *start; 428 start++; 429 430 switch (extended_op) 431 { 432 case elfcpp::DW_LNE_end_sequence: 433 // This means that the current byte is the one immediately 434 // after a set of instructions. Record the current line 435 // for up to one less than the current address. 436 lsm->line_num = -1; 437 lsm->end_sequence = true; 438 *len = oplen; 439 return true; 440 441 case elfcpp::DW_LNE_set_address: 442 { 443 lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start); 444 typename Reloc_map::const_iterator it 445 = reloc_map_.find(start - this->buffer_); 446 if (it != reloc_map_.end()) 447 { 448 // value + addend. 449 lsm->address += it->second.second; 450 lsm->shndx = it->second.first; 451 } 452 else 453 { 454 // If we're a normal .o file, with relocs, every 455 // set_address should have an associated relocation. 456 if (this->input_is_relobj()) 457 this->data_valid_ = false; 458 } 459 break; 460 } 461 case elfcpp::DW_LNE_define_file: 462 { 463 const char* filename = reinterpret_cast<const char*>(start); 464 templen = strlen(filename) + 1; 465 start += templen; 466 467 uint64_t dirindex = read_unsigned_LEB_128(start, &templen); 468 oplen += templen; 469 470 if (dirindex >= this->directories_.back().size()) 471 dirindex = 0; 472 int dirindexi = static_cast<int>(dirindex); 473 474 read_unsigned_LEB_128(start, &templen); // mod_time 475 oplen += templen; 476 477 read_unsigned_LEB_128(start, &templen); // filelength 478 oplen += templen; 479 480 this->files_.back().push_back(std::make_pair(dirindexi, 481 filename)); 482 } 483 break; 484 } 485 } 486 break; 487 488 default: 489 { 490 // Ignore unknown opcode silently 491 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++) 492 { 493 size_t templen; 494 read_unsigned_LEB_128(start, &templen); 495 start += templen; 496 oplen += templen; 497 } 498 } 499 break; 500 } 501 *len = oplen; 502 return false; 503 } 504 505 // Read the debug information at LINEPTR and store it in the line 506 // number map. 507 508 template<int size, bool big_endian> 509 unsigned const char* 510 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr, 511 off_t shndx) 512 { 513 struct LineStateMachine lsm; 514 515 // LENGTHSTART is the place the length field is based on. It is the 516 // point in the header after the initial length field. 517 const unsigned char* lengthstart = buffer_; 518 519 // In 64 bit dwarf, the initial length is 12 bytes, because of the 520 // 0xffffffff at the start. 521 if (header_.offset_size == 8) 522 lengthstart += 12; 523 else 524 lengthstart += 4; 525 526 while (lineptr < lengthstart + header_.total_length) 527 { 528 ResetLineStateMachine(&lsm, header_.default_is_stmt); 529 while (!lsm.end_sequence) 530 { 531 size_t oplength; 532 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength); 533 if (add_line 534 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx)) 535 { 536 Offset_to_lineno_entry entry 537 = { lsm.address, this->current_header_index_, 538 lsm.file_num, lsm.line_num }; 539 line_number_map_[lsm.shndx].push_back(entry); 540 } 541 lineptr += oplength; 542 } 543 } 544 545 return lengthstart + header_.total_length; 546 } 547 548 // Looks in the symtab to see what section a symbol is in. 549 550 template<int size, bool big_endian> 551 unsigned int 552 Sized_dwarf_line_info<size, big_endian>::symbol_section( 553 Object* object, 554 unsigned int sym, 555 typename elfcpp::Elf_types<size>::Elf_Addr* value, 556 bool* is_ordinary) 557 { 558 const int symsize = elfcpp::Elf_sizes<size>::sym_size; 559 gold_assert(sym * symsize < this->symtab_buffer_size_); 560 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize); 561 *value = elfsym.get_st_value(); 562 return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); 563 } 564 565 // Read the relocations into a Reloc_map. 566 567 template<int size, bool big_endian> 568 void 569 Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object) 570 { 571 if (this->symtab_buffer_ == NULL) 572 return; 573 574 typename elfcpp::Elf_types<size>::Elf_Addr value; 575 off_t reloc_offset; 576 while ((reloc_offset = this->track_relocs_.next_offset()) != -1) 577 { 578 const unsigned int sym = this->track_relocs_.next_symndx(); 579 580 bool is_ordinary; 581 const unsigned int shndx = this->symbol_section(object, sym, &value, 582 &is_ordinary); 583 584 // There is no reason to record non-ordinary section indexes, or 585 // SHN_UNDEF, because they will never match the real section. 586 if (is_ordinary && shndx != elfcpp::SHN_UNDEF) 587 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value); 588 589 this->track_relocs_.advance(reloc_offset + 1); 590 } 591 } 592 593 // Read the line number info. 594 595 template<int size, bool big_endian> 596 void 597 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object, 598 off_t shndx) 599 { 600 gold_assert(this->data_valid_ == true); 601 602 this->read_relocs(object); 603 while (this->buffer_ < this->buffer_end_) 604 { 605 const unsigned char* lineptr = this->buffer_; 606 lineptr = this->read_header_prolog(lineptr); 607 lineptr = this->read_header_tables(lineptr); 608 lineptr = this->read_lines(lineptr, shndx); 609 this->buffer_ = lineptr; 610 } 611 612 // Sort the lines numbers, so addr2line can use binary search. 613 for (typename Lineno_map::iterator it = line_number_map_.begin(); 614 it != line_number_map_.end(); 615 ++it) 616 // Each vector needs to be sorted by offset. 617 std::sort(it->second.begin(), it->second.end()); 618 } 619 620 // Some processing depends on whether the input is a .o file or not. 621 // For instance, .o files have relocs, and have .debug_lines 622 // information on a per section basis. .so files, on the other hand, 623 // lack relocs, and offsets are unique, so we can ignore the section 624 // information. 625 626 template<int size, bool big_endian> 627 bool 628 Sized_dwarf_line_info<size, big_endian>::input_is_relobj() 629 { 630 // Only .o files have relocs and the symtab buffer that goes with them. 631 return this->symtab_buffer_ != NULL; 632 } 633 634 // Given an Offset_to_lineno_entry vector, and an offset, figure out 635 // if the offset points into a function according to the vector (see 636 // comments below for the algorithm). If it does, return an iterator 637 // into the vector that points to the line-number that contains that 638 // offset. If not, it returns vector::end(). 639 640 static std::vector<Offset_to_lineno_entry>::const_iterator 641 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets, 642 off_t offset) 643 { 644 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 }; 645 646 // lower_bound() returns the smallest offset which is >= lookup_key. 647 // If no offset in offsets is >= lookup_key, returns end(). 648 std::vector<Offset_to_lineno_entry>::const_iterator it 649 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key); 650 651 // This code is easiest to understand with a concrete example. 652 // Here's a possible offsets array: 653 // {{offset = 3211, header_num = 0, file_num = 1, line_num = 16}, // 0 654 // {offset = 3224, header_num = 0, file_num = 1, line_num = 20}, // 1 655 // {offset = 3226, header_num = 0, file_num = 1, line_num = 22}, // 2 656 // {offset = 3231, header_num = 0, file_num = 1, line_num = 25}, // 3 657 // {offset = 3232, header_num = 0, file_num = 1, line_num = -1}, // 4 658 // {offset = 3232, header_num = 0, file_num = 1, line_num = 65}, // 5 659 // {offset = 3235, header_num = 0, file_num = 1, line_num = 66}, // 6 660 // {offset = 3236, header_num = 0, file_num = 1, line_num = -1}, // 7 661 // {offset = 5764, header_num = 0, file_num = 1, line_num = 47}, // 8 662 // {offset = 5765, header_num = 0, file_num = 1, line_num = 48}, // 9 663 // {offset = 5767, header_num = 0, file_num = 1, line_num = 49}, // 10 664 // {offset = 5768, header_num = 0, file_num = 1, line_num = 50}, // 11 665 // {offset = 5773, header_num = 0, file_num = 1, line_num = -1}, // 12 666 // {offset = 5787, header_num = 1, file_num = 1, line_num = 19}, // 13 667 // {offset = 5790, header_num = 1, file_num = 1, line_num = 20}, // 14 668 // {offset = 5793, header_num = 1, file_num = 1, line_num = 67}, // 15 669 // {offset = 5793, header_num = 1, file_num = 1, line_num = -1}, // 16 670 // {offset = 5795, header_num = 1, file_num = 1, line_num = 68}, // 17 671 // {offset = 5798, header_num = 1, file_num = 1, line_num = -1}, // 18 672 // The entries with line_num == -1 mark the end of a function: the 673 // associated offset is one past the last instruction in the 674 // function. This can correspond to the beginning of the next 675 // function (as is true for offset 3232); alternately, there can be 676 // a gap between the end of one function and the start of the next 677 // (as is true for some others, most obviously from 3236->5764). 678 // 679 // Case 1: lookup_key has offset == 10. lower_bound returns 680 // offsets[0]. Since it's not an exact match and we're 681 // at the beginning of offsets, we return end() (invalid). 682 // Case 2: lookup_key has offset 10000. lower_bound returns 683 // offset[19] (end()). We return end() (invalid). 684 // Case 3: lookup_key has offset == 3211. lower_bound matches 685 // offsets[0] exactly, and that's the entry we return. 686 // Case 4: lookup_key has offset == 3232. lower_bound returns 687 // offsets[4]. That's an exact match, but indicates 688 // end-of-function. We check if offsets[5] is also an 689 // exact match but not end-of-function. It is, so we 690 // return offsets[5]. 691 // Case 5: lookup_key has offset == 3214. lower_bound returns 692 // offsets[1]. Since it's not an exact match, we back 693 // up to the offset that's < lookup_key, offsets[0]. 694 // We note offsets[0] is a valid entry (not end-of-function), 695 // so that's the entry we return. 696 // Case 6: lookup_key has offset == 4000. lower_bound returns 697 // offsets[8]. Since it's not an exact match, we back 698 // up to offsets[7]. Since offsets[7] indicates 699 // end-of-function, we know lookup_key is between 700 // functions, so we return end() (not a valid offset). 701 // Case 7: lookup_key has offset == 5794. lower_bound returns 702 // offsets[17]. Since it's not an exact match, we back 703 // up to offsets[15]. Note we back up to the *first* 704 // entry with offset 5793, not just offsets[17-1]. 705 // We note offsets[15] is a valid entry, so we return it. 706 // If offsets[15] had had line_num == -1, we would have 707 // checked offsets[16]. The reason for this is that 708 // 15 and 16 can be in an arbitrary order, since we sort 709 // only by offset. (Note it doesn't help to use line_number 710 // as a secondary sort key, since sometimes we want the -1 711 // to be first and sometimes we want it to be last.) 712 713 // This deals with cases (1) and (2). 714 if ((it == offsets->begin() && offset < it->offset) 715 || it == offsets->end()) 716 return offsets->end(); 717 718 // This deals with cases (3) and (4). 719 if (offset == it->offset) 720 { 721 while (it != offsets->end() 722 && it->offset == offset 723 && it->line_num == -1) 724 ++it; 725 if (it == offsets->end() || it->offset != offset) 726 return offsets->end(); 727 else 728 return it; 729 } 730 731 // This handles the first part of case (7) -- we back up to the 732 // *first* entry that has the offset that's behind us. 733 gold_assert(it != offsets->begin()); 734 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it; 735 --it; 736 const off_t range_value = it->offset; 737 while (it != offsets->begin() && (it-1)->offset == range_value) 738 --it; 739 740 // This handles cases (5), (6), and (7): if any entry in the 741 // equal_range [it, range_end) has a line_num != -1, it's a valid 742 // match. If not, we're not in a function. 743 for (; it != range_end; ++it) 744 if (it->line_num != -1) 745 return it; 746 return offsets->end(); 747 } 748 749 // Return a string for a file name and line number. 750 751 template<int size, bool big_endian> 752 std::string 753 Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx, 754 off_t offset) 755 { 756 if (this->data_valid_ == false) 757 return ""; 758 759 const std::vector<Offset_to_lineno_entry>* offsets; 760 // If we do not have reloc information, then our input is a .so or 761 // some similar data structure where all the information is held in 762 // the offset. In that case, we ignore the input shndx. 763 if (this->input_is_relobj()) 764 offsets = &this->line_number_map_[shndx]; 765 else 766 offsets = &this->line_number_map_[-1U]; 767 if (offsets->empty()) 768 return ""; 769 770 typename std::vector<Offset_to_lineno_entry>::const_iterator it 771 = offset_to_iterator(offsets, offset); 772 if (it == offsets->end()) 773 return ""; 774 775 // Convert the file_num + line_num into a string. 776 std::string ret; 777 778 gold_assert(it->header_num < static_cast<int>(this->files_.size())); 779 gold_assert(it->file_num 780 < static_cast<int>(this->files_[it->header_num].size())); 781 const std::pair<int, std::string>& filename_pair 782 = this->files_[it->header_num][it->file_num]; 783 const std::string& filename = filename_pair.second; 784 785 gold_assert(it->header_num < static_cast<int>(this->directories_.size())); 786 gold_assert(filename_pair.first 787 < static_cast<int>(this->directories_[it->header_num].size())); 788 const std::string& dirname 789 = this->directories_[it->header_num][filename_pair.first]; 790 791 if (!dirname.empty()) 792 { 793 ret += dirname; 794 ret += "/"; 795 } 796 ret += filename; 797 if (ret.empty()) 798 ret = "(unknown)"; 799 800 char buffer[64]; // enough to hold a line number 801 snprintf(buffer, sizeof(buffer), "%d", it->line_num); 802 ret += ":"; 803 ret += buffer; 804 805 return ret; 806 } 807 808 // Dwarf_line_info routines. 809 810 static unsigned int next_generation_count = 0; 811 812 struct Addr2line_cache_entry 813 { 814 Object* object; 815 unsigned int shndx; 816 Dwarf_line_info* dwarf_line_info; 817 unsigned int generation_count; 818 unsigned int access_count; 819 820 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d) 821 : object(o), shndx(s), dwarf_line_info(d), 822 generation_count(next_generation_count), access_count(0) 823 { 824 if (next_generation_count < (1U << 31)) 825 ++next_generation_count; 826 } 827 }; 828 // We expect this cache to be small, so don't bother with a hashtable 829 // or priority queue or anything: just use a simple vector. 830 static std::vector<Addr2line_cache_entry> addr2line_cache; 831 832 std::string 833 Dwarf_line_info::one_addr2line(Object* object, 834 unsigned int shndx, off_t offset, 835 size_t cache_size) 836 { 837 Dwarf_line_info* lineinfo = NULL; 838 std::vector<Addr2line_cache_entry>::iterator it; 839 840 // First, check the cache. If we hit, update the counts. 841 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) 842 { 843 if (it->object == object && it->shndx == shndx) 844 { 845 lineinfo = it->dwarf_line_info; 846 it->generation_count = next_generation_count; 847 // We cap generation_count at 2^31 -1 to avoid overflow. 848 if (next_generation_count < (1U << 31)) 849 ++next_generation_count; 850 // We cap access_count at 31 so 2^access_count doesn't overflow 851 if (it->access_count < 31) 852 ++it->access_count; 853 break; 854 } 855 } 856 857 // If we don't hit the cache, create a new object and insert into the 858 // cache. 859 if (lineinfo == NULL) 860 { 861 switch (parameters->size_and_endianness()) 862 { 863 #ifdef HAVE_TARGET_32_LITTLE 864 case Parameters::TARGET_32_LITTLE: 865 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break; 866 #endif 867 #ifdef HAVE_TARGET_32_BIG 868 case Parameters::TARGET_32_BIG: 869 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break; 870 #endif 871 #ifdef HAVE_TARGET_64_LITTLE 872 case Parameters::TARGET_64_LITTLE: 873 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break; 874 #endif 875 #ifdef HAVE_TARGET_64_BIG 876 case Parameters::TARGET_64_BIG: 877 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break; 878 #endif 879 default: 880 gold_unreachable(); 881 } 882 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo)); 883 } 884 885 // Now that we have our object, figure out the answer 886 std::string retval = lineinfo->addr2line(shndx, offset); 887 888 // Finally, if our cache has grown too big, delete old objects. We 889 // assume the common (probably only) case is deleting only one object. 890 // We use a pretty simple scheme to evict: function of LRU and MFU. 891 while (addr2line_cache.size() > cache_size) 892 { 893 unsigned int lowest_score = ~0U; 894 std::vector<Addr2line_cache_entry>::iterator lowest 895 = addr2line_cache.end(); 896 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) 897 { 898 const unsigned int score = (it->generation_count 899 + (1U << it->access_count)); 900 if (score < lowest_score) 901 { 902 lowest_score = score; 903 lowest = it; 904 } 905 } 906 if (lowest != addr2line_cache.end()) 907 { 908 delete lowest->dwarf_line_info; 909 addr2line_cache.erase(lowest); 910 } 911 } 912 913 return retval; 914 } 915 916 void 917 Dwarf_line_info::clear_addr2line_cache() 918 { 919 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin(); 920 it != addr2line_cache.end(); 921 ++it) 922 delete it->dwarf_line_info; 923 addr2line_cache.clear(); 924 } 925 926 #ifdef HAVE_TARGET_32_LITTLE 927 template 928 class Sized_dwarf_line_info<32, false>; 929 #endif 930 931 #ifdef HAVE_TARGET_32_BIG 932 template 933 class Sized_dwarf_line_info<32, true>; 934 #endif 935 936 #ifdef HAVE_TARGET_64_LITTLE 937 template 938 class Sized_dwarf_line_info<64, false>; 939 #endif 940 941 #ifdef HAVE_TARGET_64_BIG 942 template 943 class Sized_dwarf_line_info<64, true>; 944 #endif 945 946 } // End namespace gold. 947