1 //===-- Section.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
22 namespace lldb_private {
26 using namespace lldb_private;
28 const char *Section::GetTypeAsCString() const {
30 case eSectionTypeInvalid:
32 case eSectionTypeCode:
34 case eSectionTypeContainer:
36 case eSectionTypeData:
38 case eSectionTypeDataCString:
40 case eSectionTypeDataCStringPointers:
41 return "data-cstr-ptr";
42 case eSectionTypeDataSymbolAddress:
43 return "data-symbol-addr";
44 case eSectionTypeData4:
46 case eSectionTypeData8:
48 case eSectionTypeData16:
49 return "data-16-byte";
50 case eSectionTypeDataPointers:
52 case eSectionTypeDebug:
54 case eSectionTypeZeroFill:
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:
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:
74 case eSectionTypeDWARFDebugInfo:
76 case eSectionTypeDWARFDebugInfoDwo:
77 return "dwarf-info-dwo";
78 case eSectionTypeDWARFDebugLine:
80 case eSectionTypeDWARFDebugLineStr:
81 return "dwarf-line-str";
82 case eSectionTypeDWARFDebugLoc:
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:
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:
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:
134 case eSectionTypeEHFrame:
136 case eSectionTypeARMexidx:
138 case eSectionTypeARMextab:
140 case eSectionTypeCompactUnwind:
141 return "compact-unwind";
142 case eSectionTypeGoSymtab:
144 case eSectionTypeAbsoluteAddress:
146 case eSectionTypeDWARFGNUDebugAltLink:
147 return "dwarf-gnu-debugaltlink";
148 case eSectionTypeOther:
154 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
155 user_id_t sect_id, ConstString name,
156 SectionType sect_type, addr_t file_addr, addr_t byte_size,
157 lldb::offset_t file_offset, lldb::offset_t file_size,
158 uint32_t log2align, uint32_t flags,
159 uint32_t target_byte_size /*=1*/)
160 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
161 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
162 m_file_addr(file_addr), m_byte_size(byte_size),
163 m_file_offset(file_offset), m_file_size(file_size),
164 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
165 m_thread_specific(false), m_readable(false), m_writable(false),
166 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
169 Section::Section(const lldb::SectionSP &parent_section_sp,
170 const ModuleSP &module_sp, ObjectFile *obj_file,
171 user_id_t sect_id, ConstString name,
172 SectionType sect_type, addr_t file_addr, addr_t byte_size,
173 lldb::offset_t file_offset, lldb::offset_t file_size,
174 uint32_t log2align, uint32_t flags,
175 uint32_t target_byte_size /*=1*/)
176 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
177 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
178 m_file_addr(file_addr), m_byte_size(byte_size),
179 m_file_offset(file_offset), m_file_size(file_size),
180 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
181 m_thread_specific(false), m_readable(false), m_writable(false),
182 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
183 if (parent_section_sp)
184 m_parent_wp = parent_section_sp;
187 Section::~Section() = default;
189 addr_t Section::GetFileAddress() const {
190 SectionSP parent_sp(GetParent());
192 // This section has a parent which means m_file_addr is an offset into the
193 // parent section, so the file address for this section is the file address
194 // of the parent plus the offset
195 return parent_sp->GetFileAddress() + m_file_addr;
197 // This section has no parent, so m_file_addr is the file base address
201 bool Section::SetFileAddress(lldb::addr_t file_addr) {
202 SectionSP parent_sp(GetParent());
204 if (m_file_addr >= file_addr)
205 return parent_sp->SetFileAddress(m_file_addr - file_addr);
208 // This section has no parent, so m_file_addr is the file base address
209 m_file_addr = file_addr;
214 lldb::addr_t Section::GetOffset() const {
215 // This section has a parent which means m_file_addr is an offset.
216 SectionSP parent_sp(GetParent());
220 // This section has no parent, so there is no offset to be had
224 addr_t Section::GetLoadBaseAddress(Target *target) const {
225 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
226 SectionSP parent_sp(GetParent());
228 load_base_addr = parent_sp->GetLoadBaseAddress(target);
229 if (load_base_addr != LLDB_INVALID_ADDRESS)
230 load_base_addr += GetOffset();
232 if (load_base_addr == LLDB_INVALID_ADDRESS) {
233 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
234 const_cast<Section *>(this)->shared_from_this());
236 return load_base_addr;
239 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
240 bool allow_section_end) const {
241 const size_t num_children = m_children.GetSize();
242 for (size_t i = 0; i < num_children; i++) {
243 Section *child_section = m_children.GetSectionAtIndex(i).get();
245 addr_t child_offset = child_section->GetOffset();
246 if (child_offset <= offset &&
247 offset - child_offset <
248 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
249 return child_section->ResolveContainedAddress(offset - child_offset,
250 so_addr, allow_section_end);
252 so_addr.SetOffset(offset);
253 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
255 // Ensure that there are no orphaned (i.e., moduleless) sections.
256 assert(GetModule().get());
260 bool Section::ContainsFileAddress(addr_t vm_addr) const {
261 const addr_t file_addr = GetFileAddress();
262 if (file_addr != LLDB_INVALID_ADDRESS && !IsThreadSpecific()) {
263 if (file_addr <= vm_addr) {
264 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
265 return offset < GetByteSize();
271 void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
272 uint32_t depth) const {
274 s << llvm::format("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
275 bool resolved = true;
276 addr_t addr = LLDB_INVALID_ADDRESS;
278 if (GetByteSize() == 0)
282 addr = GetLoadBaseAddress(target);
284 if (addr == LLDB_INVALID_ADDRESS) {
287 addr = GetFileAddress();
290 VMRange range(addr, addr + m_byte_size);
294 s << llvm::format("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
295 resolved ? ' ' : '*', m_readable ? 'r' : '-',
296 m_writable ? 'w' : '-', m_executable ? 'x' : '-',
297 m_file_offset, m_file_size, Get());
304 m_children.Dump(s, indent, target, false, depth - 1);
307 void Section::DumpName(llvm::raw_ostream &s) const {
308 SectionSP parent_sp(GetParent());
310 parent_sp->DumpName(s);
313 // The top most section prints the module basename
314 const char *name = nullptr;
315 ModuleSP module_sp(GetModule());
318 const FileSpec &file_spec = m_obj_file->GetFileSpec();
319 name = file_spec.GetFilename().AsCString();
321 if ((!name || !name[0]) && module_sp)
322 name = module_sp->GetFileSpec().GetFilename().AsCString();
329 bool Section::IsDescendant(const Section *section) {
332 SectionSP parent_sp(GetParent());
334 return parent_sp->IsDescendant(section);
338 bool Section::Slide(addr_t slide_amount, bool slide_children) {
339 if (m_file_addr != LLDB_INVALID_ADDRESS) {
340 if (slide_amount == 0)
343 m_file_addr += slide_amount;
346 m_children.Slide(slide_amount, slide_children);
353 /// Get the permissions as OR'ed bits from lldb::Permissions
354 uint32_t Section::GetPermissions() const {
355 uint32_t permissions = 0;
357 permissions |= ePermissionsReadable;
359 permissions |= ePermissionsWritable;
361 permissions |= ePermissionsExecutable;
365 /// Set the permissions using bits OR'ed from lldb::Permissions
366 void Section::SetPermissions(uint32_t permissions) {
367 m_readable = (permissions & ePermissionsReadable) != 0;
368 m_writable = (permissions & ePermissionsWritable) != 0;
369 m_executable = (permissions & ePermissionsExecutable) != 0;
372 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
373 lldb::offset_t offset) {
375 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
379 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) {
381 return m_obj_file->ReadSectionData(this, section_data);
385 bool Section::ContainsOnlyDebugInfo() const {
387 case eSectionTypeInvalid:
388 case eSectionTypeCode:
389 case eSectionTypeContainer:
390 case eSectionTypeData:
391 case eSectionTypeDataCString:
392 case eSectionTypeDataCStringPointers:
393 case eSectionTypeDataSymbolAddress:
394 case eSectionTypeData4:
395 case eSectionTypeData8:
396 case eSectionTypeData16:
397 case eSectionTypeDataPointers:
398 case eSectionTypeZeroFill:
399 case eSectionTypeDataObjCMessageRefs:
400 case eSectionTypeDataObjCCFStrings:
401 case eSectionTypeELFSymbolTable:
402 case eSectionTypeELFDynamicSymbols:
403 case eSectionTypeELFRelocationEntries:
404 case eSectionTypeELFDynamicLinkInfo:
405 case eSectionTypeEHFrame:
406 case eSectionTypeARMexidx:
407 case eSectionTypeARMextab:
408 case eSectionTypeCompactUnwind:
409 case eSectionTypeGoSymtab:
410 case eSectionTypeAbsoluteAddress:
411 case eSectionTypeOther:
412 // Used for "__dof_cache" in mach-o or ".debug" for COFF which isn't debug
413 // information that we parse at all. This was causing system files with no
414 // debug info to show debug info byte sizes in the "statistics dump" output
415 // for each module. New "eSectionType" enums should be created for dedicated
416 // debug info that has a predefined format if we wish for these sections to
417 // show up as debug info.
418 case eSectionTypeDebug:
421 case eSectionTypeDWARFDebugAbbrev:
422 case eSectionTypeDWARFDebugAbbrevDwo:
423 case eSectionTypeDWARFDebugAddr:
424 case eSectionTypeDWARFDebugAranges:
425 case eSectionTypeDWARFDebugCuIndex:
426 case eSectionTypeDWARFDebugTuIndex:
427 case eSectionTypeDWARFDebugFrame:
428 case eSectionTypeDWARFDebugInfo:
429 case eSectionTypeDWARFDebugInfoDwo:
430 case eSectionTypeDWARFDebugLine:
431 case eSectionTypeDWARFDebugLineStr:
432 case eSectionTypeDWARFDebugLoc:
433 case eSectionTypeDWARFDebugLocDwo:
434 case eSectionTypeDWARFDebugLocLists:
435 case eSectionTypeDWARFDebugLocListsDwo:
436 case eSectionTypeDWARFDebugMacInfo:
437 case eSectionTypeDWARFDebugMacro:
438 case eSectionTypeDWARFDebugPubNames:
439 case eSectionTypeDWARFDebugPubTypes:
440 case eSectionTypeDWARFDebugRanges:
441 case eSectionTypeDWARFDebugRngLists:
442 case eSectionTypeDWARFDebugRngListsDwo:
443 case eSectionTypeDWARFDebugStr:
444 case eSectionTypeDWARFDebugStrDwo:
445 case eSectionTypeDWARFDebugStrOffsets:
446 case eSectionTypeDWARFDebugStrOffsetsDwo:
447 case eSectionTypeDWARFDebugTypes:
448 case eSectionTypeDWARFDebugTypesDwo:
449 case eSectionTypeDWARFDebugNames:
450 case eSectionTypeDWARFAppleNames:
451 case eSectionTypeDWARFAppleTypes:
452 case eSectionTypeDWARFAppleNamespaces:
453 case eSectionTypeDWARFAppleObjC:
454 case eSectionTypeDWARFGNUDebugAltLink:
461 #pragma mark SectionList
463 SectionList &SectionList::operator=(const SectionList &rhs) {
465 m_sections = rhs.m_sections;
469 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {
471 size_t section_index = m_sections.size();
472 m_sections.push_back(section_sp);
473 return section_index;
476 return std::numeric_limits<size_t>::max();
479 // Warning, this can be slow as it's removing items from a std::vector.
480 bool SectionList::DeleteSection(size_t idx) {
481 if (idx < m_sections.size()) {
482 m_sections.erase(m_sections.begin() + idx);
488 size_t SectionList::FindSectionIndex(const Section *sect) {
490 iterator begin = m_sections.begin();
491 iterator end = m_sections.end();
492 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
493 if (sect_iter->get() == sect) {
494 // The secton was already in this section list
495 return std::distance(begin, sect_iter);
501 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {
502 size_t sect_idx = FindSectionIndex(sect_sp.get());
503 if (sect_idx == UINT32_MAX) {
504 sect_idx = AddSection(sect_sp);
509 bool SectionList::ReplaceSection(user_id_t sect_id,
510 const lldb::SectionSP §_sp,
512 iterator sect_iter, end = m_sections.end();
513 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
514 if ((*sect_iter)->GetID() == sect_id) {
515 *sect_iter = sect_sp;
517 } else if (depth > 0) {
520 .ReplaceSection(sect_id, sect_sp, depth - 1))
527 size_t SectionList::GetNumSections(uint32_t depth) const {
528 size_t count = m_sections.size();
530 const_iterator sect_iter, end = m_sections.end();
531 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
532 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
538 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
540 if (idx < m_sections.size())
541 sect_sp = m_sections[idx];
546 SectionList::FindSectionByName(ConstString section_dstr) const {
548 // Check if we have a valid section string
549 if (section_dstr && !m_sections.empty()) {
550 const_iterator sect_iter;
551 const_iterator end = m_sections.end();
552 for (sect_iter = m_sections.begin();
553 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
554 Section *child_section = sect_iter->get();
556 if (child_section->GetName() == section_dstr) {
557 sect_sp = *sect_iter;
560 child_section->GetChildren().FindSectionByName(section_dstr);
568 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
571 const_iterator sect_iter;
572 const_iterator end = m_sections.end();
573 for (sect_iter = m_sections.begin();
574 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
575 if ((*sect_iter)->GetID() == sect_id) {
576 sect_sp = *sect_iter;
579 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
586 SectionSP SectionList::FindSectionByType(SectionType sect_type,
588 size_t start_idx) const {
590 size_t num_sections = m_sections.size();
591 for (size_t idx = start_idx; idx < num_sections; ++idx) {
592 if (m_sections[idx]->GetType() == sect_type) {
593 sect_sp = m_sections[idx];
595 } else if (check_children) {
596 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
597 sect_type, check_children, 0);
605 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
606 uint32_t depth) const {
608 const_iterator sect_iter;
609 const_iterator end = m_sections.end();
610 for (sect_iter = m_sections.begin();
611 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
612 Section *sect = sect_iter->get();
613 if (sect->ContainsFileAddress(vm_addr)) {
614 // The file address is in this section. We need to make sure one of our
615 // child sections doesn't contain this address as well as obeying the
616 // depth limit that was passed in.
618 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
621 if (sect_sp.get() == nullptr && !sect->IsFake())
622 sect_sp = *sect_iter;
628 bool SectionList::ContainsSection(user_id_t sect_id) const {
629 return FindSectionByID(sect_id).get() != nullptr;
632 void SectionList::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
633 bool show_header, uint32_t depth) const {
634 bool target_has_loaded_sections =
635 target && !target->GetSectionLoadList().IsEmpty();
636 if (show_header && !m_sections.empty()) {
639 "SectID Type {0} Address "
640 " Perm File Off. File Size Flags "
642 target_has_loaded_sections ? "Load" : "File");
644 s << "---------- ---------------- "
645 "--------------------------------------- ---- ---------- "
647 "---------- ----------------------------\n";
650 for (const auto §ion_sp : m_sections)
651 section_sp->Dump(s, indent, target_has_loaded_sections ? target : nullptr,
655 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
657 const_iterator pos, end = m_sections.end();
658 for (pos = m_sections.begin(); pos != end; ++pos) {
659 if ((*pos)->Slide(slide_amount, slide_children))
665 uint64_t SectionList::GetDebugInfoSize() const {
666 uint64_t debug_info_size = 0;
667 for (const auto §ion : m_sections) {
668 const SectionList &sub_sections = section->GetChildren();
669 if (sub_sections.GetSize() > 0)
670 debug_info_size += sub_sections.GetDebugInfoSize();
671 else if (section->ContainsOnlyDebugInfo())
672 debug_info_size += section->GetFileSize();
674 return debug_info_size;