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