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/Address.h" // for Address 12 #include "lldb/Core/Module.h" 13 #include "lldb/Symbol/ObjectFile.h" 14 #include "lldb/Target/SectionLoadList.h" 15 #include "lldb/Target/Target.h" 16 #include "lldb/Utility/FileSpec.h" // for FileSpec 17 #include "lldb/Utility/Stream.h" // for Stream 18 #include "lldb/Utility/VMRange.h" // for VMRange 19 20 #include <inttypes.h> // for PRIx64 21 #include <limits> // for numeric_limits 22 #include <utility> // for distance 23 24 namespace lldb_private { 25 class DataExtractor; 26 } 27 using namespace lldb; 28 using namespace lldb_private; 29 30 const char *Section::GetTypeAsCString() const { 31 switch (m_type) { 32 case eSectionTypeInvalid: 33 return "invalid"; 34 case eSectionTypeCode: 35 return "code"; 36 case eSectionTypeContainer: 37 return "container"; 38 case eSectionTypeData: 39 return "data"; 40 case eSectionTypeDataCString: 41 return "data-cstr"; 42 case eSectionTypeDataCStringPointers: 43 return "data-cstr-ptr"; 44 case eSectionTypeDataSymbolAddress: 45 return "data-symbol-addr"; 46 case eSectionTypeData4: 47 return "data-4-byte"; 48 case eSectionTypeData8: 49 return "data-8-byte"; 50 case eSectionTypeData16: 51 return "data-16-byte"; 52 case eSectionTypeDataPointers: 53 return "data-ptrs"; 54 case eSectionTypeDebug: 55 return "debug"; 56 case eSectionTypeZeroFill: 57 return "zero-fill"; 58 case eSectionTypeDataObjCMessageRefs: 59 return "objc-message-refs"; 60 case eSectionTypeDataObjCCFStrings: 61 return "objc-cfstrings"; 62 case eSectionTypeDWARFDebugAbbrev: 63 return "dwarf-abbrev"; 64 case eSectionTypeDWARFDebugAddr: 65 return "dwarf-addr"; 66 case eSectionTypeDWARFDebugAranges: 67 return "dwarf-aranges"; 68 case eSectionTypeDWARFDebugCuIndex: 69 return "dwarf-cu-index"; 70 case eSectionTypeDWARFDebugFrame: 71 return "dwarf-frame"; 72 case eSectionTypeDWARFDebugInfo: 73 return "dwarf-info"; 74 case eSectionTypeDWARFDebugLine: 75 return "dwarf-line"; 76 case eSectionTypeDWARFDebugLoc: 77 return "dwarf-loc"; 78 case eSectionTypeDWARFDebugMacInfo: 79 return "dwarf-macinfo"; 80 case eSectionTypeDWARFDebugMacro: 81 return "dwarf-macro"; 82 case eSectionTypeDWARFDebugPubNames: 83 return "dwarf-pubnames"; 84 case eSectionTypeDWARFDebugPubTypes: 85 return "dwarf-pubtypes"; 86 case eSectionTypeDWARFDebugRanges: 87 return "dwarf-ranges"; 88 case eSectionTypeDWARFDebugStr: 89 return "dwarf-str"; 90 case eSectionTypeDWARFDebugStrOffsets: 91 return "dwarf-str-offsets"; 92 case eSectionTypeDWARFDebugTypes: 93 return "dwarf-types"; 94 case eSectionTypeDWARFDebugNames: 95 return "dwarf-names"; 96 case eSectionTypeELFSymbolTable: 97 return "elf-symbol-table"; 98 case eSectionTypeELFDynamicSymbols: 99 return "elf-dynamic-symbols"; 100 case eSectionTypeELFRelocationEntries: 101 return "elf-relocation-entries"; 102 case eSectionTypeELFDynamicLinkInfo: 103 return "elf-dynamic-link-info"; 104 case eSectionTypeDWARFAppleNames: 105 return "apple-names"; 106 case eSectionTypeDWARFAppleTypes: 107 return "apple-types"; 108 case eSectionTypeDWARFAppleNamespaces: 109 return "apple-namespaces"; 110 case eSectionTypeDWARFAppleObjC: 111 return "apple-objc"; 112 case eSectionTypeEHFrame: 113 return "eh-frame"; 114 case eSectionTypeARMexidx: 115 return "ARM.exidx"; 116 case eSectionTypeARMextab: 117 return "ARM.extab"; 118 case eSectionTypeCompactUnwind: 119 return "compact-unwind"; 120 case eSectionTypeGoSymtab: 121 return "go-symtab"; 122 case eSectionTypeAbsoluteAddress: 123 return "absolute"; 124 case eSectionTypeDWARFGNUDebugAltLink: 125 return "dwarf-gnu-debugaltlink"; 126 case eSectionTypeOther: 127 return "regular"; 128 } 129 return "unknown"; 130 } 131 132 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file, 133 user_id_t sect_id, const ConstString &name, 134 SectionType sect_type, addr_t file_addr, addr_t byte_size, 135 lldb::offset_t file_offset, lldb::offset_t file_size, 136 uint32_t log2align, uint32_t flags, 137 uint32_t target_byte_size /*=1*/) 138 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 139 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 140 m_file_addr(file_addr), m_byte_size(byte_size), 141 m_file_offset(file_offset), m_file_size(file_size), 142 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 143 m_thread_specific(false), m_readable(false), m_writable(false), 144 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) { 145 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 146 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 147 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n", 148 // this, module_sp.get(), sect_id, file_addr, file_addr + 149 // byte_size, file_offset, file_offset + file_size, flags, 150 // name.GetCString()); 151 } 152 153 Section::Section(const lldb::SectionSP &parent_section_sp, 154 const ModuleSP &module_sp, ObjectFile *obj_file, 155 user_id_t sect_id, const ConstString &name, 156 SectionType sect_type, addr_t file_addr, addr_t byte_size, 157 lldb::offset_t file_offset, lldb::offset_t file_size, 158 uint32_t log2align, uint32_t flags, 159 uint32_t target_byte_size /*=1*/) 160 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 161 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 162 m_file_addr(file_addr), m_byte_size(byte_size), 163 m_file_offset(file_offset), m_file_size(file_size), 164 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 165 m_thread_specific(false), m_readable(false), m_writable(false), 166 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) { 167 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 168 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 169 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n", 170 // this, module_sp.get(), sect_id, file_addr, file_addr + 171 // byte_size, file_offset, file_offset + file_size, flags, 172 // parent_section_sp->GetName().GetCString(), name.GetCString()); 173 if (parent_section_sp) 174 m_parent_wp = parent_section_sp; 175 } 176 177 Section::~Section() { 178 // printf ("Section::~Section(%p)\n", this); 179 } 180 181 addr_t Section::GetFileAddress() const { 182 SectionSP parent_sp(GetParent()); 183 if (parent_sp) { 184 // This section has a parent which means m_file_addr is an offset into the 185 // parent section, so the file address for this section is the file address 186 // of the parent plus the offset 187 return parent_sp->GetFileAddress() + m_file_addr; 188 } 189 // This section has no parent, so m_file_addr is the file base address 190 return m_file_addr; 191 } 192 193 bool Section::SetFileAddress(lldb::addr_t file_addr) { 194 SectionSP parent_sp(GetParent()); 195 if (parent_sp) { 196 if (m_file_addr >= file_addr) 197 return parent_sp->SetFileAddress(m_file_addr - file_addr); 198 return false; 199 } else { 200 // This section has no parent, so m_file_addr is the file base address 201 m_file_addr = file_addr; 202 return true; 203 } 204 } 205 206 lldb::addr_t Section::GetOffset() const { 207 // This section has a parent which means m_file_addr is an offset. 208 SectionSP parent_sp(GetParent()); 209 if (parent_sp) 210 return m_file_addr; 211 212 // This section has no parent, so there is no offset to be had 213 return 0; 214 } 215 216 addr_t Section::GetLoadBaseAddress(Target *target) const { 217 addr_t load_base_addr = LLDB_INVALID_ADDRESS; 218 SectionSP parent_sp(GetParent()); 219 if (parent_sp) { 220 load_base_addr = parent_sp->GetLoadBaseAddress(target); 221 if (load_base_addr != LLDB_INVALID_ADDRESS) 222 load_base_addr += GetOffset(); 223 } 224 if (load_base_addr == LLDB_INVALID_ADDRESS) { 225 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress( 226 const_cast<Section *>(this)->shared_from_this()); 227 } 228 return load_base_addr; 229 } 230 231 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr, 232 bool allow_section_end) const { 233 const size_t num_children = m_children.GetSize(); 234 for (size_t i = 0; i < num_children; i++) { 235 Section *child_section = m_children.GetSectionAtIndex(i).get(); 236 237 addr_t child_offset = child_section->GetOffset(); 238 if (child_offset <= offset && 239 offset - child_offset < 240 child_section->GetByteSize() + (allow_section_end ? 1 : 0)) 241 return child_section->ResolveContainedAddress(offset - child_offset, 242 so_addr, allow_section_end); 243 } 244 so_addr.SetOffset(offset); 245 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this()); 246 247 #ifdef LLDB_CONFIGURATION_DEBUG 248 // For debug builds, ensure that there are no orphaned (i.e., moduleless) 249 // sections. 250 assert(GetModule().get()); 251 #endif 252 return true; 253 } 254 255 bool Section::ContainsFileAddress(addr_t vm_addr) const { 256 const addr_t file_addr = GetFileAddress(); 257 if (file_addr != LLDB_INVALID_ADDRESS) { 258 if (file_addr <= vm_addr) { 259 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size; 260 return offset < GetByteSize(); 261 } 262 } 263 return false; 264 } 265 266 int Section::Compare(const Section &a, const Section &b) { 267 if (&a == &b) 268 return 0; 269 270 const ModuleSP a_module_sp = a.GetModule(); 271 const ModuleSP b_module_sp = b.GetModule(); 272 if (a_module_sp == b_module_sp) { 273 user_id_t a_sect_uid = a.GetID(); 274 user_id_t b_sect_uid = b.GetID(); 275 if (a_sect_uid < b_sect_uid) 276 return -1; 277 if (a_sect_uid > b_sect_uid) 278 return 1; 279 return 0; 280 } else { 281 // The modules are different, just compare the module pointers 282 if (a_module_sp.get() < b_module_sp.get()) 283 return -1; 284 else 285 return 1; // We already know the modules aren't equal 286 } 287 } 288 289 void Section::Dump(Stream *s, Target *target, uint32_t depth) const { 290 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 291 s->Indent(); 292 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString()); 293 bool resolved = true; 294 addr_t addr = LLDB_INVALID_ADDRESS; 295 296 if (GetByteSize() == 0) 297 s->Printf("%39s", ""); 298 else { 299 if (target) 300 addr = GetLoadBaseAddress(target); 301 302 if (addr == LLDB_INVALID_ADDRESS) { 303 if (target) 304 resolved = false; 305 addr = GetFileAddress(); 306 } 307 308 VMRange range(addr, addr + m_byte_size); 309 range.Dump(s, 0); 310 } 311 312 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", 313 resolved ? ' ' : '*', m_readable ? 'r' : '-', 314 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset, 315 m_file_size, Get()); 316 317 DumpName(s); 318 319 s->EOL(); 320 321 if (depth > 0) 322 m_children.Dump(s, target, false, depth - 1); 323 } 324 325 void Section::DumpName(Stream *s) const { 326 SectionSP parent_sp(GetParent()); 327 if (parent_sp) { 328 parent_sp->DumpName(s); 329 s->PutChar('.'); 330 } else { 331 // The top most section prints the module basename 332 const char *name = NULL; 333 ModuleSP module_sp(GetModule()); 334 335 if (m_obj_file) { 336 const FileSpec &file_spec = m_obj_file->GetFileSpec(); 337 name = file_spec.GetFilename().AsCString(); 338 } 339 if ((!name || !name[0]) && module_sp) 340 name = module_sp->GetFileSpec().GetFilename().AsCString(); 341 if (name && name[0]) 342 s->Printf("%s.", name); 343 } 344 m_name.Dump(s); 345 } 346 347 bool Section::IsDescendant(const Section *section) { 348 if (this == section) 349 return true; 350 SectionSP parent_sp(GetParent()); 351 if (parent_sp) 352 return parent_sp->IsDescendant(section); 353 return false; 354 } 355 356 bool Section::Slide(addr_t slide_amount, bool slide_children) { 357 if (m_file_addr != LLDB_INVALID_ADDRESS) { 358 if (slide_amount == 0) 359 return true; 360 361 m_file_addr += slide_amount; 362 363 if (slide_children) 364 m_children.Slide(slide_amount, slide_children); 365 366 return true; 367 } 368 return false; 369 } 370 371 //------------------------------------------------------------------ 372 /// Get the permissions as OR'ed bits from lldb::Permissions 373 //------------------------------------------------------------------ 374 uint32_t Section::GetPermissions() const { 375 uint32_t permissions = 0; 376 if (m_readable) 377 permissions |= ePermissionsReadable; 378 if (m_writable) 379 permissions |= ePermissionsWritable; 380 if (m_executable) 381 permissions |= ePermissionsExecutable; 382 return permissions; 383 } 384 385 //------------------------------------------------------------------ 386 /// Set the permissions using bits OR'ed from lldb::Permissions 387 //------------------------------------------------------------------ 388 void Section::SetPermissions(uint32_t permissions) { 389 m_readable = (permissions & ePermissionsReadable) != 0; 390 m_writable = (permissions & ePermissionsWritable) != 0; 391 m_executable = (permissions & ePermissionsExecutable) != 0; 392 } 393 394 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len, 395 lldb::offset_t offset) { 396 if (m_obj_file) 397 return m_obj_file->ReadSectionData(this, offset, dst, dst_len); 398 return 0; 399 } 400 401 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) { 402 if (m_obj_file) 403 return m_obj_file->ReadSectionData(this, section_data); 404 return 0; 405 } 406 407 #pragma mark SectionList 408 409 SectionList::SectionList() : m_sections() {} 410 411 SectionList::~SectionList() {} 412 413 SectionList &SectionList::operator=(const SectionList &rhs) { 414 if (this != &rhs) 415 m_sections = rhs.m_sections; 416 return *this; 417 } 418 419 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) { 420 if (section_sp) { 421 size_t section_index = m_sections.size(); 422 m_sections.push_back(section_sp); 423 return section_index; 424 } 425 426 return std::numeric_limits<size_t>::max(); 427 } 428 429 // Warning, this can be slow as it's removing items from a std::vector. 430 bool SectionList::DeleteSection(size_t idx) { 431 if (idx < m_sections.size()) { 432 m_sections.erase(m_sections.begin() + idx); 433 return true; 434 } 435 return false; 436 } 437 438 size_t SectionList::FindSectionIndex(const Section *sect) { 439 iterator sect_iter; 440 iterator begin = m_sections.begin(); 441 iterator end = m_sections.end(); 442 for (sect_iter = begin; sect_iter != end; ++sect_iter) { 443 if (sect_iter->get() == sect) { 444 // The secton was already in this section list 445 return std::distance(begin, sect_iter); 446 } 447 } 448 return UINT32_MAX; 449 } 450 451 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) { 452 size_t sect_idx = FindSectionIndex(sect_sp.get()); 453 if (sect_idx == UINT32_MAX) { 454 sect_idx = AddSection(sect_sp); 455 } 456 return sect_idx; 457 } 458 459 bool SectionList::ReplaceSection(user_id_t sect_id, 460 const lldb::SectionSP §_sp, 461 uint32_t depth) { 462 iterator sect_iter, end = m_sections.end(); 463 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 464 if ((*sect_iter)->GetID() == sect_id) { 465 *sect_iter = sect_sp; 466 return true; 467 } else if (depth > 0) { 468 if ((*sect_iter) 469 ->GetChildren() 470 .ReplaceSection(sect_id, sect_sp, depth - 1)) 471 return true; 472 } 473 } 474 return false; 475 } 476 477 size_t SectionList::GetNumSections(uint32_t depth) const { 478 size_t count = m_sections.size(); 479 if (depth > 0) { 480 const_iterator sect_iter, end = m_sections.end(); 481 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 482 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 483 } 484 } 485 return count; 486 } 487 488 SectionSP SectionList::GetSectionAtIndex(size_t idx) const { 489 SectionSP sect_sp; 490 if (idx < m_sections.size()) 491 sect_sp = m_sections[idx]; 492 return sect_sp; 493 } 494 495 SectionSP 496 SectionList::FindSectionByName(const ConstString §ion_dstr) const { 497 SectionSP sect_sp; 498 // Check if we have a valid section string 499 if (section_dstr && !m_sections.empty()) { 500 const_iterator sect_iter; 501 const_iterator end = m_sections.end(); 502 for (sect_iter = m_sections.begin(); 503 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 504 Section *child_section = sect_iter->get(); 505 if (child_section) { 506 if (child_section->GetName() == section_dstr) { 507 sect_sp = *sect_iter; 508 } else { 509 sect_sp = 510 child_section->GetChildren().FindSectionByName(section_dstr); 511 } 512 } 513 } 514 } 515 return sect_sp; 516 } 517 518 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const { 519 SectionSP sect_sp; 520 if (sect_id) { 521 const_iterator sect_iter; 522 const_iterator end = m_sections.end(); 523 for (sect_iter = m_sections.begin(); 524 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 525 if ((*sect_iter)->GetID() == sect_id) { 526 sect_sp = *sect_iter; 527 break; 528 } else { 529 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id); 530 } 531 } 532 } 533 return sect_sp; 534 } 535 536 SectionSP SectionList::FindSectionByType(SectionType sect_type, 537 bool check_children, 538 size_t start_idx) const { 539 SectionSP sect_sp; 540 size_t num_sections = m_sections.size(); 541 for (size_t idx = start_idx; idx < num_sections; ++idx) { 542 if (m_sections[idx]->GetType() == sect_type) { 543 sect_sp = m_sections[idx]; 544 break; 545 } else if (check_children) { 546 sect_sp = m_sections[idx]->GetChildren().FindSectionByType( 547 sect_type, check_children, 0); 548 if (sect_sp) 549 break; 550 } 551 } 552 return sect_sp; 553 } 554 555 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr, 556 uint32_t depth) const { 557 SectionSP sect_sp; 558 const_iterator sect_iter; 559 const_iterator end = m_sections.end(); 560 for (sect_iter = m_sections.begin(); 561 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 562 Section *sect = sect_iter->get(); 563 if (sect->ContainsFileAddress(vm_addr)) { 564 // The file address is in this section. We need to make sure one of our 565 // child sections doesn't contain this address as well as obeying the 566 // depth limit that was passed in. 567 if (depth > 0) 568 sect_sp = sect->GetChildren().FindSectionContainingFileAddress( 569 vm_addr, depth - 1); 570 571 if (sect_sp.get() == NULL && !sect->IsFake()) 572 sect_sp = *sect_iter; 573 } 574 } 575 return sect_sp; 576 } 577 578 bool SectionList::ContainsSection(user_id_t sect_id) const { 579 return FindSectionByID(sect_id).get() != NULL; 580 } 581 582 void SectionList::Dump(Stream *s, Target *target, bool show_header, 583 uint32_t depth) const { 584 bool target_has_loaded_sections = 585 target && !target->GetSectionLoadList().IsEmpty(); 586 if (show_header && !m_sections.empty()) { 587 s->Indent(); 588 s->Printf("SectID Type %s Address " 589 " Perm File Off. File Size Flags " 590 " Section Name\n", 591 target_has_loaded_sections ? "Load" : "File"); 592 s->Indent(); 593 s->PutCString("---------- ---------------- " 594 "--------------------------------------- ---- ---------- " 595 "---------- " 596 "---------- ----------------------------\n"); 597 } 598 599 const_iterator sect_iter; 600 const_iterator end = m_sections.end(); 601 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 602 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 603 } 604 605 if (show_header && !m_sections.empty()) 606 s->IndentLess(); 607 } 608 609 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) { 610 size_t count = 0; 611 const_iterator pos, end = m_sections.end(); 612 for (pos = m_sections.begin(); pos != end; ++pos) { 613 if ((*pos)->Slide(slide_amount, slide_children)) 614 ++count; 615 } 616 return count; 617 } 618