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