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