761 lines · cpp
1//===-- Section.cpp -------------------------------------------------------===//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-exception6//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/VMRange.h"17 18#include <cinttypes>19#include <limits>20#include <utility>21 22namespace lldb_private {23class DataExtractor;24}25using namespace lldb;26using namespace lldb_private;27 28const char *Section::GetTypeAsCString() const {29 switch (m_type) {30 case eSectionTypeInvalid:31 return "invalid";32 case eSectionTypeCode:33 return "code";34 case eSectionTypeContainer:35 return "container";36 case eSectionTypeData:37 return "data";38 case eSectionTypeDataCString:39 return "data-cstr";40 case eSectionTypeDataCStringPointers:41 return "data-cstr-ptr";42 case eSectionTypeDataSymbolAddress:43 return "data-symbol-addr";44 case eSectionTypeData4:45 return "data-4-byte";46 case eSectionTypeData8:47 return "data-8-byte";48 case eSectionTypeData16:49 return "data-16-byte";50 case eSectionTypeDataPointers:51 return "data-ptrs";52 case eSectionTypeDebug:53 return "debug";54 case eSectionTypeZeroFill:55 return "zero-fill";56 case eSectionTypeDataObjCMessageRefs:57 return "objc-message-refs";58 case eSectionTypeDataObjCCFStrings:59 return "objc-cfstrings";60 case eSectionTypeDWARFDebugAbbrev:61 return "dwarf-abbrev";62 case eSectionTypeDWARFDebugAbbrevDwo:63 return "dwarf-abbrev-dwo";64 case eSectionTypeDWARFDebugAddr:65 return "dwarf-addr";66 case eSectionTypeDWARFDebugAranges:67 return "dwarf-aranges";68 case eSectionTypeDWARFDebugCuIndex:69 return "dwarf-cu-index";70 case eSectionTypeDWARFDebugTuIndex:71 return "dwarf-tu-index";72 case eSectionTypeDWARFDebugFrame:73 return "dwarf-frame";74 case eSectionTypeDWARFDebugInfo:75 return "dwarf-info";76 case eSectionTypeDWARFDebugInfoDwo:77 return "dwarf-info-dwo";78 case eSectionTypeDWARFDebugLine:79 return "dwarf-line";80 case eSectionTypeDWARFDebugLineStr:81 return "dwarf-line-str";82 case eSectionTypeDWARFDebugLoc:83 return "dwarf-loc";84 case eSectionTypeDWARFDebugLocDwo:85 return "dwarf-loc-dwo";86 case eSectionTypeDWARFDebugLocLists:87 return "dwarf-loclists";88 case eSectionTypeDWARFDebugLocListsDwo:89 return "dwarf-loclists-dwo";90 case eSectionTypeDWARFDebugMacInfo:91 return "dwarf-macinfo";92 case eSectionTypeDWARFDebugMacro:93 return "dwarf-macro";94 case eSectionTypeDWARFDebugPubNames:95 return "dwarf-pubnames";96 case eSectionTypeDWARFDebugPubTypes:97 return "dwarf-pubtypes";98 case eSectionTypeDWARFDebugRanges:99 return "dwarf-ranges";100 case eSectionTypeDWARFDebugRngLists:101 return "dwarf-rnglists";102 case eSectionTypeDWARFDebugRngListsDwo:103 return "dwarf-rnglists-dwo";104 case eSectionTypeDWARFDebugStr:105 return "dwarf-str";106 case eSectionTypeDWARFDebugStrDwo:107 return "dwarf-str-dwo";108 case eSectionTypeDWARFDebugStrOffsets:109 return "dwarf-str-offsets";110 case eSectionTypeDWARFDebugStrOffsetsDwo:111 return "dwarf-str-offsets-dwo";112 case eSectionTypeDWARFDebugTypes:113 return "dwarf-types";114 case eSectionTypeDWARFDebugTypesDwo:115 return "dwarf-types-dwo";116 case eSectionTypeDWARFDebugNames:117 return "dwarf-names";118 case eSectionTypeELFSymbolTable:119 return "elf-symbol-table";120 case eSectionTypeELFDynamicSymbols:121 return "elf-dynamic-symbols";122 case eSectionTypeELFRelocationEntries:123 return "elf-relocation-entries";124 case eSectionTypeELFDynamicLinkInfo:125 return "elf-dynamic-link-info";126 case eSectionTypeDWARFAppleNames:127 return "apple-names";128 case eSectionTypeDWARFAppleTypes:129 return "apple-types";130 case eSectionTypeDWARFAppleNamespaces:131 return "apple-namespaces";132 case eSectionTypeDWARFAppleObjC:133 return "apple-objc";134 case eSectionTypeEHFrame:135 return "eh-frame";136 case eSectionTypeARMexidx:137 return "ARM.exidx";138 case eSectionTypeARMextab:139 return "ARM.extab";140 case eSectionTypeCompactUnwind:141 return "compact-unwind";142 case eSectionTypeGoSymtab:143 return "go-symtab";144 case eSectionTypeAbsoluteAddress:145 return "absolute";146 case eSectionTypeDWARFGNUDebugAltLink:147 return "dwarf-gnu-debugaltlink";148 case eSectionTypeCTF:149 return "ctf";150 case eSectionTypeLLDBTypeSummaries:151 return "lldb-type-summaries";152 case eSectionTypeLLDBFormatters:153 return "lldb-formatters";154 case eSectionTypeSwiftModules:155 return "swift-modules";156 case eSectionTypeWasmName:157 return "wasm-name";158 case eSectionTypeOther:159 return "regular";160 }161 return "unknown";162}163 164Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,165 user_id_t sect_id, ConstString name, SectionType sect_type,166 addr_t file_addr, addr_t byte_size, lldb::offset_t file_offset,167 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,168 uint32_t target_byte_size /*=1*/)169 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),170 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),171 m_file_addr(file_addr), m_byte_size(byte_size),172 m_file_offset(file_offset), m_file_size(file_size),173 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),174 m_thread_specific(false), m_readable(false), m_writable(false),175 m_executable(false), m_relocated(false),176 m_target_byte_size(target_byte_size) {}177 178Section::Section(const lldb::SectionSP &parent_section_sp,179 const ModuleSP &module_sp, ObjectFile *obj_file,180 user_id_t sect_id, ConstString name, SectionType sect_type,181 addr_t file_addr, addr_t byte_size, lldb::offset_t file_offset,182 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,183 uint32_t target_byte_size /*=1*/)184 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),185 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),186 m_file_addr(file_addr), m_byte_size(byte_size),187 m_file_offset(file_offset), m_file_size(file_size),188 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),189 m_thread_specific(false), m_readable(false), m_writable(false),190 m_executable(false), m_relocated(false),191 m_target_byte_size(target_byte_size) {192 if (parent_section_sp)193 m_parent_wp = parent_section_sp;194}195 196Section::~Section() = default;197 198addr_t Section::GetFileAddress() const {199 SectionSP parent_sp(GetParent());200 if (parent_sp) {201 // This section has a parent which means m_file_addr is an offset into the202 // parent section, so the file address for this section is the file address203 // of the parent plus the offset204 return parent_sp->GetFileAddress() + m_file_addr;205 }206 // This section has no parent, so m_file_addr is the file base address207 return m_file_addr;208}209 210bool Section::SetFileAddress(lldb::addr_t file_addr) {211 SectionSP parent_sp(GetParent());212 if (parent_sp) {213 if (m_file_addr >= file_addr)214 return parent_sp->SetFileAddress(m_file_addr - file_addr);215 return false;216 } else {217 // This section has no parent, so m_file_addr is the file base address218 m_file_addr = file_addr;219 return true;220 }221}222 223lldb::addr_t Section::GetOffset() const {224 // This section has a parent which means m_file_addr is an offset.225 SectionSP parent_sp(GetParent());226 if (parent_sp)227 return m_file_addr;228 229 // This section has no parent, so there is no offset to be had230 return 0;231}232 233addr_t Section::GetLoadBaseAddress(Target *target) const {234 addr_t load_base_addr = LLDB_INVALID_ADDRESS;235 SectionSP parent_sp(GetParent());236 if (parent_sp) {237 load_base_addr = parent_sp->GetLoadBaseAddress(target);238 if (load_base_addr != LLDB_INVALID_ADDRESS)239 load_base_addr += GetOffset();240 }241 if (load_base_addr == LLDB_INVALID_ADDRESS) {242 load_base_addr = target->GetSectionLoadAddress(243 const_cast<Section *>(this)->shared_from_this());244 }245 return load_base_addr;246}247 248bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,249 bool allow_section_end) const {250 const size_t num_children = m_children.GetSize();251 for (size_t i = 0; i < num_children; i++) {252 Section *child_section = m_children.GetSectionAtIndex(i).get();253 254 addr_t child_offset = child_section->GetOffset();255 if (child_offset <= offset &&256 offset - child_offset <257 child_section->GetByteSize() + (allow_section_end ? 1 : 0))258 return child_section->ResolveContainedAddress(offset - child_offset,259 so_addr, allow_section_end);260 }261 so_addr.SetOffset(offset);262 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());263 264 // Ensure that there are no orphaned (i.e., moduleless) sections.265 assert(GetModule().get());266 return true;267}268 269bool Section::ContainsFileAddress(addr_t vm_addr) const {270 const addr_t file_addr = GetFileAddress();271 if (file_addr != LLDB_INVALID_ADDRESS && !IsThreadSpecific()) {272 if (file_addr <= vm_addr) {273 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;274 return offset < GetByteSize();275 }276 }277 return false;278}279 280void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,281 uint32_t depth) const {282 s.indent(indent);283 s << llvm::format("0x%16.16" PRIx64 " %-22s ", GetID(), GetTypeAsCString());284 bool resolved = true;285 addr_t addr = LLDB_INVALID_ADDRESS;286 287 if (GetByteSize() == 0)288 s.indent(39);289 else {290 if (target)291 addr = GetLoadBaseAddress(target);292 293 if (addr == LLDB_INVALID_ADDRESS) {294 if (target)295 resolved = false;296 addr = GetFileAddress();297 }298 299 VMRange range(addr, addr + m_byte_size);300 range.Dump(s, 0);301 }302 303 s << llvm::format("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",304 resolved ? ' ' : '*', m_readable ? 'r' : '-',305 m_writable ? 'w' : '-', m_executable ? 'x' : '-',306 m_file_offset, m_file_size, Get());307 308 DumpName(s);309 310 s << "\n";311 312 if (depth > 0)313 m_children.Dump(s, indent, target, false, depth - 1);314}315 316void Section::DumpName(llvm::raw_ostream &s) const {317 SectionSP parent_sp(GetParent());318 if (parent_sp) {319 parent_sp->DumpName(s);320 s << '.';321 } else {322 // The top most section prints the module basename323 const char *name = nullptr;324 ModuleSP module_sp(GetModule());325 326 if (m_obj_file) {327 const FileSpec &file_spec = m_obj_file->GetFileSpec();328 name = file_spec.GetFilename().AsCString();329 }330 if ((!name || !name[0]) && module_sp)331 name = module_sp->GetFileSpec().GetFilename().AsCString();332 if (name && name[0])333 s << name << '.';334 }335 s << m_name;336}337 338bool Section::IsDescendant(const Section *section) {339 if (this == section)340 return true;341 SectionSP parent_sp(GetParent());342 if (parent_sp)343 return parent_sp->IsDescendant(section);344 return false;345}346 347bool Section::Slide(addr_t slide_amount, bool slide_children) {348 if (m_file_addr != LLDB_INVALID_ADDRESS) {349 if (slide_amount == 0)350 return true;351 352 m_file_addr += slide_amount;353 354 if (slide_children)355 m_children.Slide(slide_amount, slide_children);356 357 return true;358 }359 return false;360}361 362/// Get the permissions as OR'ed bits from lldb::Permissions363uint32_t Section::GetPermissions() const {364 uint32_t permissions = 0;365 if (m_readable)366 permissions |= ePermissionsReadable;367 if (m_writable)368 permissions |= ePermissionsWritable;369 if (m_executable)370 permissions |= ePermissionsExecutable;371 return permissions;372}373 374/// Set the permissions using bits OR'ed from lldb::Permissions375void Section::SetPermissions(uint32_t permissions) {376 m_readable = (permissions & ePermissionsReadable) != 0;377 m_writable = (permissions & ePermissionsWritable) != 0;378 m_executable = (permissions & ePermissionsExecutable) != 0;379}380 381lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,382 lldb::offset_t offset) {383 if (m_obj_file)384 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);385 return 0;386}387 388lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) {389 if (m_obj_file)390 return m_obj_file->ReadSectionData(this, section_data);391 return 0;392}393 394bool Section::ContainsOnlyDebugInfo() const {395 switch (m_type) {396 case eSectionTypeInvalid:397 case eSectionTypeCode:398 case eSectionTypeContainer:399 case eSectionTypeData:400 case eSectionTypeDataCString:401 case eSectionTypeDataCStringPointers:402 case eSectionTypeDataSymbolAddress:403 case eSectionTypeData4:404 case eSectionTypeData8:405 case eSectionTypeData16:406 case eSectionTypeDataPointers:407 case eSectionTypeZeroFill:408 case eSectionTypeDataObjCMessageRefs:409 case eSectionTypeDataObjCCFStrings:410 case eSectionTypeELFSymbolTable:411 case eSectionTypeELFDynamicSymbols:412 case eSectionTypeELFRelocationEntries:413 case eSectionTypeELFDynamicLinkInfo:414 case eSectionTypeEHFrame:415 case eSectionTypeARMexidx:416 case eSectionTypeARMextab:417 case eSectionTypeCompactUnwind:418 case eSectionTypeGoSymtab:419 case eSectionTypeAbsoluteAddress:420 case eSectionTypeWasmName:421 case eSectionTypeOther:422 // Used for "__dof_cache" in mach-o or ".debug" for COFF which isn't debug423 // information that we parse at all. This was causing system files with no424 // debug info to show debug info byte sizes in the "statistics dump" output425 // for each module. New "eSectionType" enums should be created for dedicated426 // debug info that has a predefined format if we wish for these sections to427 // show up as debug info.428 case eSectionTypeDebug:429 return false;430 431 case eSectionTypeDWARFDebugAbbrev:432 case eSectionTypeDWARFDebugAbbrevDwo:433 case eSectionTypeDWARFDebugAddr:434 case eSectionTypeDWARFDebugAranges:435 case eSectionTypeDWARFDebugCuIndex:436 case eSectionTypeDWARFDebugTuIndex:437 case eSectionTypeDWARFDebugFrame:438 case eSectionTypeDWARFDebugInfo:439 case eSectionTypeDWARFDebugInfoDwo:440 case eSectionTypeDWARFDebugLine:441 case eSectionTypeDWARFDebugLineStr:442 case eSectionTypeDWARFDebugLoc:443 case eSectionTypeDWARFDebugLocDwo:444 case eSectionTypeDWARFDebugLocLists:445 case eSectionTypeDWARFDebugLocListsDwo:446 case eSectionTypeDWARFDebugMacInfo:447 case eSectionTypeDWARFDebugMacro:448 case eSectionTypeDWARFDebugPubNames:449 case eSectionTypeDWARFDebugPubTypes:450 case eSectionTypeDWARFDebugRanges:451 case eSectionTypeDWARFDebugRngLists:452 case eSectionTypeDWARFDebugRngListsDwo:453 case eSectionTypeDWARFDebugStr:454 case eSectionTypeDWARFDebugStrDwo:455 case eSectionTypeDWARFDebugStrOffsets:456 case eSectionTypeDWARFDebugStrOffsetsDwo:457 case eSectionTypeDWARFDebugTypes:458 case eSectionTypeDWARFDebugTypesDwo:459 case eSectionTypeDWARFDebugNames:460 case eSectionTypeDWARFAppleNames:461 case eSectionTypeDWARFAppleTypes:462 case eSectionTypeDWARFAppleNamespaces:463 case eSectionTypeDWARFAppleObjC:464 case eSectionTypeDWARFGNUDebugAltLink:465 case eSectionTypeCTF:466 case eSectionTypeLLDBTypeSummaries:467 case eSectionTypeLLDBFormatters:468 case eSectionTypeSwiftModules:469 return true;470 }471 return false;472}473 474bool Section::IsGOTSection() const {475 return GetObjectFile()->IsGOTSection(*this);476}477 478#pragma mark SectionList479 480SectionList::SectionList(const SectionList &rhs) : m_sections(rhs.m_sections) {}481 482SectionList &SectionList::operator=(const SectionList &rhs) {483 if (this != &rhs)484 m_sections = rhs.m_sections;485 return *this;486}487 488size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {489 if (section_sp) {490 size_t section_index = m_sections.size();491 m_sections.push_back(section_sp);492 return section_index;493 }494 495 return std::numeric_limits<size_t>::max();496}497 498// Warning, this can be slow as it's removing items from a std::vector.499bool SectionList::DeleteSection(size_t idx) {500 if (idx < m_sections.size()) {501 m_sections.erase(m_sections.begin() + idx);502 return true;503 }504 return false;505}506 507size_t SectionList::FindSectionIndex(const Section *sect) {508 iterator sect_iter;509 iterator begin = m_sections.begin();510 iterator end = m_sections.end();511 for (sect_iter = begin; sect_iter != end; ++sect_iter) {512 if (sect_iter->get() == sect) {513 // The secton was already in this section list514 return std::distance(begin, sect_iter);515 }516 }517 return UINT32_MAX;518}519 520size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {521 size_t sect_idx = FindSectionIndex(sect_sp.get());522 if (sect_idx == UINT32_MAX) {523 sect_idx = AddSection(sect_sp);524 }525 return sect_idx;526}527 528bool SectionList::ReplaceSection(user_id_t sect_id,529 const lldb::SectionSP §_sp,530 uint32_t depth) {531 iterator sect_iter, end = m_sections.end();532 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {533 if ((*sect_iter)->GetID() == sect_id) {534 *sect_iter = sect_sp;535 return true;536 } else if (depth > 0) {537 if ((*sect_iter)538 ->GetChildren()539 .ReplaceSection(sect_id, sect_sp, depth - 1))540 return true;541 }542 }543 return false;544}545 546size_t SectionList::GetNumSections(uint32_t depth) const {547 size_t count = m_sections.size();548 if (depth > 0) {549 const_iterator sect_iter, end = m_sections.end();550 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {551 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);552 }553 }554 return count;555}556 557SectionSP SectionList::GetSectionAtIndex(size_t idx) const {558 SectionSP sect_sp;559 if (idx < m_sections.size())560 sect_sp = m_sections[idx];561 return sect_sp;562}563 564SectionSP SectionList::FindSectionByName(ConstString section_dstr) const {565 SectionSP sect_sp;566 // Check if we have a valid section string567 if (section_dstr && !m_sections.empty()) {568 const_iterator sect_iter;569 const_iterator end = m_sections.end();570 for (sect_iter = m_sections.begin();571 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {572 Section *child_section = sect_iter->get();573 if (child_section) {574 if (child_section->GetName() == section_dstr) {575 sect_sp = *sect_iter;576 } else {577 sect_sp =578 child_section->GetChildren().FindSectionByName(section_dstr);579 }580 }581 }582 }583 return sect_sp;584}585 586SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {587 SectionSP sect_sp;588 if (sect_id) {589 const_iterator sect_iter;590 const_iterator end = m_sections.end();591 for (sect_iter = m_sections.begin();592 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {593 if ((*sect_iter)->GetID() == sect_id) {594 sect_sp = *sect_iter;595 break;596 } else {597 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);598 }599 }600 }601 return sect_sp;602}603 604SectionSP SectionList::FindSectionByType(SectionType sect_type,605 bool check_children,606 size_t start_idx) const {607 SectionSP sect_sp;608 size_t num_sections = m_sections.size();609 for (size_t idx = start_idx; idx < num_sections; ++idx) {610 if (m_sections[idx]->GetType() == sect_type) {611 sect_sp = m_sections[idx];612 break;613 } else if (check_children) {614 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(615 sect_type, check_children, 0);616 if (sect_sp)617 break;618 }619 }620 return sect_sp;621}622 623SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,624 uint32_t depth) const {625 SectionSP sect_sp;626 const_iterator sect_iter;627 const_iterator end = m_sections.end();628 for (sect_iter = m_sections.begin();629 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {630 Section *sect = sect_iter->get();631 if (sect->ContainsFileAddress(vm_addr)) {632 // The file address is in this section. We need to make sure one of our633 // child sections doesn't contain this address as well as obeying the634 // depth limit that was passed in.635 if (depth > 0)636 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(637 vm_addr, depth - 1);638 639 if (sect_sp.get() == nullptr && !sect->IsFake())640 sect_sp = *sect_iter;641 }642 }643 return sect_sp;644}645 646bool SectionList::ContainsSection(user_id_t sect_id) const {647 return FindSectionByID(sect_id).get() != nullptr;648}649 650void SectionList::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,651 bool show_header, uint32_t depth) const {652 bool target_has_loaded_sections = target && target->HasLoadedSections();653 if (show_header && !m_sections.empty()) {654 s.indent(indent);655 s << llvm::formatv(656 "SectID Type {0} Address "657 " Perm File Off. File Size Flags Section Name\n",658 target_has_loaded_sections ? "Load" : "File");659 s.indent(indent);660 s << "------------------ ---------------------- "661 "--------------------------------------- ---- ---------- ---------- "662 "---------- ----------------------------\n";663 }664 665 for (const auto §ion_sp : m_sections)666 section_sp->Dump(s, indent, target_has_loaded_sections ? target : nullptr,667 depth);668}669 670size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {671 size_t count = 0;672 const_iterator pos, end = m_sections.end();673 for (pos = m_sections.begin(); pos != end; ++pos) {674 if ((*pos)->Slide(slide_amount, slide_children))675 ++count;676 }677 return count;678}679 680uint64_t SectionList::GetDebugInfoSize() const {681 uint64_t debug_info_size = 0;682 for (const auto §ion : m_sections) {683 const SectionList &sub_sections = section->GetChildren();684 if (sub_sections.GetSize() > 0)685 debug_info_size += sub_sections.GetDebugInfoSize();686 else if (section->ContainsOnlyDebugInfo())687 debug_info_size += section->GetFileSize();688 }689 return debug_info_size;690}691 692SectionList SectionList::Merge(SectionList &lhs, SectionList &rhs,693 MergeCallback filter) {694 SectionList output_list;695 696 // Iterate through all the sections in lhs and see if we have matches in697 // the rhs list.698 for (const auto &lhs_section : lhs) {699 auto rhs_section = rhs.FindSectionByName(lhs_section->GetName());700 if (rhs_section)701 output_list.AddSection(filter(lhs_section, rhs_section));702 else703 output_list.AddSection(lhs_section);704 }705 706 // Now that we've visited all possible duplicates, we can iterate over707 // the rhs and take any values not in lhs.708 for (const auto &rhs_section : rhs) {709 auto lhs_section = lhs.FindSectionByName(rhs_section->GetName());710 // Because we already visited everything overlapping between rhs711 // and lhs, any section not in lhs is unique and can be output.712 if (!lhs_section)713 output_list.AddSection(rhs_section);714 }715 716 return output_list;717}718 719namespace llvm {720namespace json {721 722bool fromJSON(const llvm::json::Value &value,723 lldb_private::JSONSection §ion, llvm::json::Path path) {724 llvm::json::ObjectMapper o(value, path);725 return o && o.map("name", section.name) && o.map("type", section.type) &&726 o.map("address", section.address) && o.map("size", section.size) &&727 o.map("read", section.read) && o.map("write", section.write) &&728 o.map("execute", section.execute) &&729 o.mapOptional("subsections", section.subsections) &&730 o.map("user_id", section.user_id) &&731 o.map("file_offset", section.file_offset) &&732 o.map("file_size", section.file_size) &&733 o.map("alignment", section.log2align) &&734 o.map("flags", section.flags) && o.map("fake", section.fake) &&735 o.map("encrypted", section.encrypted) &&736 o.map("thread_specific", section.thread_specific);737}738 739bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type,740 llvm::json::Path path) {741 if (auto str = value.getAsString()) {742 type = llvm::StringSwitch<lldb::SectionType>(*str)743 .Case("code", eSectionTypeCode)744 .Case("container", eSectionTypeContainer)745 .Case("data", eSectionTypeData)746 .Case("debug", eSectionTypeDebug)747 .Default(eSectionTypeInvalid);748 749 if (type == eSectionTypeInvalid) {750 path.report("invalid section type");751 return false;752 }753 754 return true;755 }756 path.report("expected string");757 return false;758}759} // namespace json760} // namespace llvm761