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