1 //===-- Section.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/Section.h" 11 #include "lldb/Core/Module.h" 12 #include "lldb/Symbol/ObjectFile.h" 13 #include "lldb/Target/Target.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 Section::Section 19 ( 20 Section *parent, 21 Module* module, 22 user_id_t sect_id, 23 const ConstString &name, 24 SectionType sect_type, 25 addr_t file_addr, 26 addr_t byte_size, 27 uint64_t file_offset, 28 uint64_t file_size, 29 uint32_t flags 30 ) : 31 ModuleChild (module), 32 UserID (sect_id), 33 Flags (flags), 34 m_parent (parent), 35 m_name (name), 36 m_type (sect_type), 37 m_file_addr (file_addr), 38 m_byte_size (byte_size), 39 m_file_offset (file_offset), 40 m_file_size (file_size), 41 m_children (), 42 m_fake (false), 43 m_linked_section(NULL), 44 m_linked_offset (0) 45 { 46 } 47 48 Section::~Section() 49 { 50 } 51 52 53 // Get a valid shared pointer to this section object 54 SectionSP 55 Section::GetSharedPointer() const 56 { 57 SectionSP this_sp; 58 if (m_parent) 59 this_sp = m_parent->GetChildren().GetSharedPointer (this, false); 60 else 61 { 62 ObjectFile *objfile = m_module->GetObjectFile(); 63 if (objfile) 64 { 65 SectionList *section_list = objfile->GetSectionList(); 66 if (section_list) 67 this_sp = section_list->GetSharedPointer (this, false); 68 } 69 } 70 return this_sp; 71 } 72 73 74 75 ConstString& 76 Section::GetName() 77 { 78 if (m_linked_section) 79 return const_cast<Section *>(m_linked_section)->GetName(); 80 return m_name; 81 } 82 83 const ConstString& 84 Section::GetName() const 85 { 86 if (m_linked_section) 87 return m_linked_section->GetName(); 88 return m_name; 89 } 90 91 addr_t 92 Section::GetFileAddress () const 93 { 94 if (m_parent) 95 { 96 // This section has a parent which means m_file_addr is an offset into 97 // the parent section, so the file address for this section is the file 98 // address of the parent plus the offset 99 return m_parent->GetFileAddress() + m_file_addr; 100 } 101 // This section has no parent, so m_file_addr is the file base address 102 return m_file_addr; 103 } 104 105 addr_t 106 Section::GetLinkedFileAddress () const 107 { 108 if (m_linked_section) 109 return m_linked_section->GetFileAddress() + m_linked_offset; 110 return LLDB_INVALID_ADDRESS; 111 } 112 113 114 addr_t 115 Section::GetLoadBaseAddress (Target *target) const 116 { 117 addr_t load_base_addr = LLDB_INVALID_ADDRESS; 118 if (m_linked_section) 119 { 120 load_base_addr = m_linked_section->GetLoadBaseAddress(target); 121 if (load_base_addr != LLDB_INVALID_ADDRESS) 122 load_base_addr += m_linked_offset; 123 } 124 else 125 if (m_parent) 126 { 127 load_base_addr = m_parent->GetLoadBaseAddress (target); 128 if (load_base_addr != LLDB_INVALID_ADDRESS) 129 load_base_addr += GetOffset(); 130 } 131 else 132 { 133 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this); 134 } 135 136 return load_base_addr; 137 } 138 139 bool 140 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const 141 { 142 const uint32_t num_children = m_children.GetSize(); 143 if (num_children > 0) 144 { 145 for (uint32_t i=0; i<num_children; i++) 146 { 147 Section* child_section = m_children.GetSectionAtIndex (i).get(); 148 149 addr_t child_offset = child_section->GetOffset(); 150 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize()) 151 return child_section->ResolveContainedAddress (offset - child_offset, so_addr); 152 } 153 } 154 if (m_linked_section) 155 { 156 so_addr.SetOffset(m_linked_offset + offset); 157 so_addr.SetSection(m_linked_section); 158 } 159 else 160 { 161 so_addr.SetOffset(offset); 162 so_addr.SetSection(this); 163 } 164 return true; 165 } 166 167 bool 168 Section::ContainsFileAddress (addr_t vm_addr) const 169 { 170 const addr_t file_addr = GetFileAddress(); 171 if (file_addr != LLDB_INVALID_ADDRESS) 172 { 173 if (file_addr <= vm_addr) 174 { 175 const addr_t offset = vm_addr - file_addr; 176 return offset < GetByteSize(); 177 } 178 } 179 return false; 180 } 181 182 bool 183 Section::ContainsLinkedFileAddress (addr_t vm_addr) const 184 { 185 const addr_t linked_file_addr = GetLinkedFileAddress(); 186 if (linked_file_addr != LLDB_INVALID_ADDRESS) 187 { 188 if (linked_file_addr <= vm_addr) 189 { 190 const addr_t offset = vm_addr - linked_file_addr; 191 return offset < GetByteSize(); 192 } 193 } 194 return false; 195 } 196 197 int 198 Section::Compare (const Section& a, const Section& b) 199 { 200 if (&a == &b) 201 return 0; 202 203 const Module* a_module = a.GetModule(); 204 const Module* b_module = b.GetModule(); 205 if (a_module == b_module) 206 { 207 user_id_t a_sect_uid = a.GetID(); 208 user_id_t b_sect_uid = b.GetID(); 209 if (a_sect_uid < b_sect_uid) 210 return -1; 211 if (a_sect_uid > b_sect_uid) 212 return 1; 213 return 0; 214 } 215 else 216 { 217 // The modules are different, just compare the module pointers 218 if (a_module < b_module) 219 return -1; 220 else 221 return 1; // We already know the modules aren't equal 222 } 223 } 224 225 226 void 227 Section::Dump (Stream *s, Target *target, uint32_t depth) const 228 { 229 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 230 s->Indent(); 231 s->Printf("0x%8.8x %-14s ", GetID(), GetSectionTypeAsCString (m_type)); 232 bool resolved = true; 233 addr_t addr = LLDB_INVALID_ADDRESS; 234 235 if (GetByteSize() == 0) 236 s->Printf("%39s", ""); 237 else 238 { 239 if (target && m_linked_section == NULL) 240 addr = GetLoadBaseAddress (target); 241 242 if (addr == LLDB_INVALID_ADDRESS) 243 { 244 if (target) 245 resolved = false; 246 addr = GetFileAddress(); 247 } 248 249 VMRange range(addr, addr + m_byte_size); 250 range.Dump (s, 0); 251 } 252 253 s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get()); 254 255 DumpName (s); 256 257 s->EOL(); 258 259 if (m_linked_section) 260 { 261 addr = LLDB_INVALID_ADDRESS; 262 resolved = true; 263 if (target) 264 { 265 addr = m_linked_section->GetLoadBaseAddress(target); 266 if (addr != LLDB_INVALID_ADDRESS) 267 addr += m_linked_offset; 268 } 269 270 if (addr == LLDB_INVALID_ADDRESS) 271 { 272 if (target) 273 resolved = false; 274 addr = m_linked_section->GetFileAddress() + m_linked_offset; 275 } 276 277 int indent = 26 + s->GetIndentLevel(); 278 s->Printf("%*.*s", indent, indent, ""); 279 VMRange linked_range(addr, addr + m_byte_size); 280 linked_range.Dump (s, 0); 281 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1; 282 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, ""); 283 284 m_linked_section->DumpName(s); 285 s->Printf(" + 0x%llx\n", m_linked_offset); 286 } 287 288 if (depth > 0) 289 m_children.Dump(s, target, false, depth - 1); 290 } 291 292 void 293 Section::DumpName (Stream *s) const 294 { 295 if (m_parent == NULL) 296 { 297 // The top most section prints the module basename 298 const char *module_basename = m_module->GetFileSpec().GetFilename().AsCString(); 299 if (module_basename && module_basename[0]) 300 s->Printf("%s.", module_basename); 301 } 302 else 303 { 304 m_parent->DumpName (s); 305 s->PutChar('.'); 306 } 307 m_name.Dump(s); 308 } 309 310 //---------------------------------------------------------------------- 311 // Get the section data from a complete contiguous copy of the 312 // entire executable image. 313 //---------------------------------------------------------------------- 314 size_t 315 Section::GetSectionDataFromImage (const DataExtractor& image_data, DataExtractor& section_data) const 316 { 317 size_t file_size = GetByteSize(); 318 if (file_size > 0) 319 { 320 off_t file_offset = GetFileOffset(); 321 if (section_data.SetData (image_data, file_offset, file_size) == file_size) 322 return true; 323 } 324 return false; 325 } 326 327 size_t 328 Section::ReadSectionDataFromObjectFile (const ObjectFile* objfile, off_t section_offset, void *dst, size_t dst_len) const 329 { 330 if (objfile && dst && dst_len) 331 { 332 const FileSpec& file = objfile->GetFileSpec(); 333 334 if (file) 335 { 336 size_t bytes_left = dst_len; 337 size_t bytes_read = 0; 338 const uint64_t file_size = GetFileSize(); 339 if (section_offset < file_size) 340 { 341 off_t section_file_offset = objfile->GetOffset() + GetFileOffset() + section_offset; 342 bytes_read = file.ReadFileContents (section_file_offset, dst, dst_len); 343 if (bytes_read >= dst_len) 344 return bytes_read; 345 bytes_left -= bytes_read; 346 } 347 348 const uint64_t byte_size = GetByteSize(); 349 if (section_offset + bytes_read < byte_size) 350 { 351 memset ((uint8_t*)dst + bytes_read, 0, bytes_left); 352 bytes_read += bytes_left; 353 } 354 return bytes_read; 355 } 356 } 357 return 0; 358 } 359 360 //---------------------------------------------------------------------- 361 // Get the section data the file on disk 362 //---------------------------------------------------------------------- 363 size_t 364 Section::ReadSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const 365 { 366 if (objfile == NULL) 367 return 0; 368 369 const FileSpec& file = objfile->GetFileSpec(); 370 371 if (file) 372 { 373 size_t section_file_size = GetByteSize(); 374 if (section_file_size > 0) 375 { 376 off_t section_file_offset = GetFileOffset() + objfile->GetOffset(); 377 DataBufferSP section_data_sp(file.ReadFileContents(section_file_offset, section_file_size)); 378 379 section_data.SetByteOrder(objfile->GetByteOrder()); 380 section_data.SetAddressByteSize(objfile->GetAddressByteSize()); 381 return section_data.SetData (section_data_sp); 382 } 383 } 384 return 0; 385 } 386 387 size_t 388 Section::MemoryMapSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const 389 { 390 if (objfile == NULL) 391 return 0; 392 393 const FileSpec& file = objfile->GetFileSpec(); 394 395 if (file) 396 { 397 size_t section_file_size = GetFileSize(); 398 if (section_file_size > 0) 399 { 400 off_t section_file_offset = GetFileOffset() + objfile->GetOffset(); 401 DataBufferSP section_data_sp(file.MemoryMapFileContents(section_file_offset, section_file_size)); 402 section_data.SetByteOrder(objfile->GetByteOrder()); 403 section_data.SetAddressByteSize(objfile->GetAddressByteSize()); 404 return section_data.SetData (section_data_sp); 405 } 406 } 407 return 0; 408 } 409 410 bool 411 Section::IsDescendant (const Section *section) 412 { 413 if (this == section) 414 return true; 415 if (m_parent) 416 return m_parent->IsDescendant (section); 417 return false; 418 } 419 420 bool 421 Section::Slide (addr_t slide_amount, bool slide_children) 422 { 423 if (m_file_addr != LLDB_INVALID_ADDRESS) 424 { 425 if (slide_amount == 0) 426 return true; 427 428 m_file_addr += slide_amount; 429 430 if (slide_children) 431 m_children.Slide (slide_amount, slide_children); 432 433 return true; 434 } 435 return false; 436 } 437 438 void 439 Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset) 440 { 441 if (linked_section) 442 m_module = linked_section->GetModule(); 443 m_linked_section = linked_section; 444 m_linked_offset = linked_offset; 445 } 446 447 #pragma mark SectionList 448 449 SectionList::SectionList () : 450 m_sections() 451 { 452 } 453 454 455 SectionList::~SectionList () 456 { 457 } 458 459 uint32_t 460 SectionList::AddSection (SectionSP& sect_sp) 461 { 462 uint32_t section_index = m_sections.size(); 463 m_sections.push_back(sect_sp); 464 return section_index; 465 } 466 467 uint32_t 468 SectionList::FindSectionIndex (const Section* sect) 469 { 470 iterator sect_iter; 471 iterator begin = m_sections.begin(); 472 iterator end = m_sections.end(); 473 for (sect_iter = begin; sect_iter != end; ++sect_iter) 474 { 475 if (sect_iter->get() == sect) 476 { 477 // The secton was already in this section list 478 return std::distance (begin, sect_iter); 479 } 480 } 481 return UINT32_MAX; 482 } 483 484 uint32_t 485 SectionList::AddUniqueSection (SectionSP& sect_sp) 486 { 487 uint32_t sect_idx = FindSectionIndex (sect_sp.get()); 488 if (sect_idx == UINT32_MAX) 489 sect_idx = AddSection (sect_sp); 490 return sect_idx; 491 } 492 493 494 bool 495 SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth) 496 { 497 iterator sect_iter, end = m_sections.end(); 498 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 499 { 500 if ((*sect_iter)->GetID() == sect_id) 501 { 502 *sect_iter = sect_sp; 503 return true; 504 } 505 else if (depth > 0) 506 { 507 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1)) 508 return true; 509 } 510 } 511 return false; 512 } 513 514 515 size_t 516 SectionList::GetNumSections (uint32_t depth) const 517 { 518 size_t count = m_sections.size(); 519 if (depth > 0) 520 { 521 const_iterator sect_iter, end = m_sections.end(); 522 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 523 { 524 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 525 } 526 } 527 return count; 528 } 529 530 SectionSP 531 SectionList::GetSectionAtIndex (uint32_t idx) const 532 { 533 SectionSP sect_sp; 534 if (idx < m_sections.size()) 535 sect_sp = m_sections[idx]; 536 return sect_sp; 537 } 538 539 SectionSP 540 SectionList::FindSectionByName (const ConstString §ion_dstr) const 541 { 542 SectionSP sect_sp; 543 // Check if we have a valid section string 544 if (section_dstr) 545 { 546 const_iterator sect_iter; 547 const_iterator end = m_sections.end(); 548 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 549 { 550 if ((*sect_iter)->GetName() == section_dstr) 551 { 552 sect_sp = *sect_iter; 553 } 554 else 555 { 556 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr); 557 } 558 } 559 } 560 return sect_sp; 561 } 562 563 SectionSP 564 SectionList::FindSectionByID (user_id_t sect_id) const 565 { 566 SectionSP sect_sp; 567 if (sect_id) 568 { 569 const_iterator sect_iter; 570 const_iterator end = m_sections.end(); 571 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 572 { 573 if ((*sect_iter)->GetID() == sect_id) 574 { 575 sect_sp = *sect_iter; 576 break; 577 } 578 else 579 { 580 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id); 581 } 582 } 583 } 584 return sect_sp; 585 } 586 587 588 SectionSP 589 SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const 590 { 591 SectionSP sect_sp; 592 uint32_t num_sections = m_sections.size(); 593 for (uint32_t idx = start_idx; idx < num_sections; ++idx) 594 { 595 if (m_sections[idx]->GetType() == sect_type) 596 { 597 sect_sp = m_sections[idx]; 598 break; 599 } 600 else if (check_children) 601 { 602 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0); 603 if (sect_sp) 604 break; 605 } 606 } 607 return sect_sp; 608 } 609 610 SectionSP 611 SectionList::GetSharedPointer (const Section *section, bool check_children) const 612 { 613 SectionSP sect_sp; 614 if (section) 615 { 616 const_iterator sect_iter; 617 const_iterator end = m_sections.end(); 618 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 619 { 620 if (sect_iter->get() == section) 621 { 622 sect_sp = *sect_iter; 623 break; 624 } 625 else if (check_children) 626 { 627 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true); 628 } 629 } 630 } 631 return sect_sp; 632 } 633 634 635 636 SectionSP 637 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const 638 { 639 SectionSP sect_sp; 640 const_iterator sect_iter; 641 const_iterator end = m_sections.end(); 642 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 643 { 644 Section *sect = sect_iter->get(); 645 if (sect->ContainsFileAddress (vm_addr)) 646 { 647 // The file address is in this section. We need to make sure one of our child 648 // sections doesn't contain this address as well as obeying the depth limit 649 // that was passed in. 650 if (depth > 0) 651 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1); 652 653 if (sect_sp.get() == NULL && !sect->IsFake()) 654 sect_sp = *sect_iter; 655 } 656 } 657 return sect_sp; 658 } 659 660 661 SectionSP 662 SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const 663 { 664 SectionSP sect_sp; 665 const_iterator sect_iter; 666 const_iterator end = m_sections.end(); 667 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 668 { 669 Section *sect = sect_iter->get(); 670 if (sect->ContainsLinkedFileAddress (vm_addr)) 671 { 672 sect_sp = *sect_iter; 673 } 674 else if (depth > 0) 675 { 676 sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1); 677 } 678 } 679 return sect_sp; 680 } 681 682 bool 683 SectionList::ContainsSection(user_id_t sect_id) const 684 { 685 return FindSectionByID (sect_id).get() != NULL; 686 } 687 688 void 689 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const 690 { 691 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty(); 692 if (show_header && !m_sections.empty()) 693 { 694 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 695 // s->Indent(); 696 // s->PutCString( "SectionList\n"); 697 // s->IndentMore(); 698 // s->Printf("%*s", 2*(sizeof(void *) + 2), ""); 699 s->Indent(); 700 s->Printf("SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File"); 701 // s->Printf("%*s", 2*(sizeof(void *) + 2), ""); 702 s->Indent(); 703 s->PutCString("---------- -------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n"); 704 } 705 706 707 const_iterator sect_iter; 708 const_iterator end = m_sections.end(); 709 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 710 { 711 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 712 } 713 714 if (show_header && !m_sections.empty()) 715 s->IndentLess(); 716 717 } 718 719 size_t 720 SectionList::Slide (addr_t slide_amount, bool slide_children) 721 { 722 size_t count = 0; 723 const_iterator pos, end = m_sections.end(); 724 for (pos = m_sections.begin(); pos != end; ++pos) 725 { 726 if ((*pos)->Slide(slide_amount, slide_children)) 727 ++count; 728 } 729 return count; 730 } 731 732