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.8llx %-16s ", 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 size_t 311 Section::ReadSectionDataFromObjectFile (const ObjectFile* objfile, off_t section_offset, void *dst, size_t dst_len) const 312 { 313 // The object file now contains a full mmap'ed copy of the object file data, so just use this 314 if (objfile) 315 return objfile->CopyData (GetFileOffset() + section_offset, dst_len, dst); 316 return 0; 317 } 318 319 //---------------------------------------------------------------------- 320 // Get the section data the file on disk 321 //---------------------------------------------------------------------- 322 size_t 323 Section::ReadSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const 324 { 325 // The object file now contains a full mmap'ed copy of the object file data, so just use this 326 return MemoryMapSectionDataFromObjectFile (objfile, section_data); 327 } 328 329 size_t 330 Section::MemoryMapSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const 331 { 332 // The object file now contains a full mmap'ed copy of the object file data, so just use this 333 if (objfile) 334 return objfile->GetData(GetFileOffset(), GetByteSize(), section_data); 335 section_data.Clear(); 336 return 0; 337 } 338 339 bool 340 Section::IsDescendant (const Section *section) 341 { 342 if (this == section) 343 return true; 344 if (m_parent) 345 return m_parent->IsDescendant (section); 346 return false; 347 } 348 349 bool 350 Section::Slide (addr_t slide_amount, bool slide_children) 351 { 352 if (m_file_addr != LLDB_INVALID_ADDRESS) 353 { 354 if (slide_amount == 0) 355 return true; 356 357 m_file_addr += slide_amount; 358 359 if (slide_children) 360 m_children.Slide (slide_amount, slide_children); 361 362 return true; 363 } 364 return false; 365 } 366 367 void 368 Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset) 369 { 370 if (linked_section) 371 m_module = linked_section->GetModule(); 372 m_linked_section = linked_section; 373 m_linked_offset = linked_offset; 374 } 375 376 #pragma mark SectionList 377 378 SectionList::SectionList () : 379 m_sections() 380 { 381 } 382 383 384 SectionList::~SectionList () 385 { 386 } 387 388 uint32_t 389 SectionList::AddSection (SectionSP& sect_sp) 390 { 391 uint32_t section_index = m_sections.size(); 392 m_sections.push_back(sect_sp); 393 return section_index; 394 } 395 396 uint32_t 397 SectionList::FindSectionIndex (const Section* sect) 398 { 399 iterator sect_iter; 400 iterator begin = m_sections.begin(); 401 iterator end = m_sections.end(); 402 for (sect_iter = begin; sect_iter != end; ++sect_iter) 403 { 404 if (sect_iter->get() == sect) 405 { 406 // The secton was already in this section list 407 return std::distance (begin, sect_iter); 408 } 409 } 410 return UINT32_MAX; 411 } 412 413 uint32_t 414 SectionList::AddUniqueSection (SectionSP& sect_sp) 415 { 416 uint32_t sect_idx = FindSectionIndex (sect_sp.get()); 417 if (sect_idx == UINT32_MAX) 418 sect_idx = AddSection (sect_sp); 419 return sect_idx; 420 } 421 422 423 bool 424 SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth) 425 { 426 iterator sect_iter, end = m_sections.end(); 427 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 428 { 429 if ((*sect_iter)->GetID() == sect_id) 430 { 431 *sect_iter = sect_sp; 432 return true; 433 } 434 else if (depth > 0) 435 { 436 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1)) 437 return true; 438 } 439 } 440 return false; 441 } 442 443 444 size_t 445 SectionList::GetNumSections (uint32_t depth) const 446 { 447 size_t count = m_sections.size(); 448 if (depth > 0) 449 { 450 const_iterator sect_iter, end = m_sections.end(); 451 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 452 { 453 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 454 } 455 } 456 return count; 457 } 458 459 SectionSP 460 SectionList::GetSectionAtIndex (uint32_t idx) const 461 { 462 SectionSP sect_sp; 463 if (idx < m_sections.size()) 464 sect_sp = m_sections[idx]; 465 return sect_sp; 466 } 467 468 SectionSP 469 SectionList::FindSectionByName (const ConstString §ion_dstr) const 470 { 471 SectionSP sect_sp; 472 // Check if we have a valid section string 473 if (section_dstr) 474 { 475 const_iterator sect_iter; 476 const_iterator end = m_sections.end(); 477 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 478 { 479 if ((*sect_iter)->GetName() == section_dstr) 480 { 481 sect_sp = *sect_iter; 482 } 483 else 484 { 485 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr); 486 } 487 } 488 } 489 return sect_sp; 490 } 491 492 SectionSP 493 SectionList::FindSectionByID (user_id_t sect_id) const 494 { 495 SectionSP sect_sp; 496 if (sect_id) 497 { 498 const_iterator sect_iter; 499 const_iterator end = m_sections.end(); 500 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 501 { 502 if ((*sect_iter)->GetID() == sect_id) 503 { 504 sect_sp = *sect_iter; 505 break; 506 } 507 else 508 { 509 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id); 510 } 511 } 512 } 513 return sect_sp; 514 } 515 516 517 SectionSP 518 SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const 519 { 520 SectionSP sect_sp; 521 uint32_t num_sections = m_sections.size(); 522 for (uint32_t idx = start_idx; idx < num_sections; ++idx) 523 { 524 if (m_sections[idx]->GetType() == sect_type) 525 { 526 sect_sp = m_sections[idx]; 527 break; 528 } 529 else if (check_children) 530 { 531 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0); 532 if (sect_sp) 533 break; 534 } 535 } 536 return sect_sp; 537 } 538 539 SectionSP 540 SectionList::GetSharedPointer (const Section *section, bool check_children) const 541 { 542 SectionSP sect_sp; 543 if (section) 544 { 545 const_iterator sect_iter; 546 const_iterator end = m_sections.end(); 547 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 548 { 549 if (sect_iter->get() == section) 550 { 551 sect_sp = *sect_iter; 552 break; 553 } 554 else if (check_children) 555 { 556 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true); 557 } 558 } 559 } 560 return sect_sp; 561 } 562 563 564 565 SectionSP 566 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const 567 { 568 SectionSP sect_sp; 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 Section *sect = sect_iter->get(); 574 if (sect->ContainsFileAddress (vm_addr)) 575 { 576 // The file address is in this section. We need to make sure one of our child 577 // sections doesn't contain this address as well as obeying the depth limit 578 // that was passed in. 579 if (depth > 0) 580 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1); 581 582 if (sect_sp.get() == NULL && !sect->IsFake()) 583 sect_sp = *sect_iter; 584 } 585 } 586 return sect_sp; 587 } 588 589 590 SectionSP 591 SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const 592 { 593 SectionSP sect_sp; 594 const_iterator sect_iter; 595 const_iterator end = m_sections.end(); 596 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 597 { 598 Section *sect = sect_iter->get(); 599 if (sect->ContainsLinkedFileAddress (vm_addr)) 600 { 601 sect_sp = *sect_iter; 602 } 603 else if (depth > 0) 604 { 605 sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1); 606 } 607 } 608 return sect_sp; 609 } 610 611 bool 612 SectionList::ContainsSection(user_id_t sect_id) const 613 { 614 return FindSectionByID (sect_id).get() != NULL; 615 } 616 617 void 618 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const 619 { 620 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty(); 621 if (show_header && !m_sections.empty()) 622 { 623 s->Indent(); 624 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File"); 625 s->Indent(); 626 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n"); 627 } 628 629 630 const_iterator sect_iter; 631 const_iterator end = m_sections.end(); 632 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 633 { 634 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 635 } 636 637 if (show_header && !m_sections.empty()) 638 s->IndentLess(); 639 640 } 641 642 size_t 643 SectionList::Slide (addr_t slide_amount, bool slide_children) 644 { 645 size_t count = 0; 646 const_iterator pos, end = m_sections.end(); 647 for (pos = m_sections.begin(); pos != end; ++pos) 648 { 649 if ((*pos)->Slide(slide_amount, slide_children)) 650 ++count; 651 } 652 return count; 653 } 654 655