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