1 //===-- Section.cpp ---------------------------------------------*- C++ -*-===//
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/Stream.h"
17 #include "lldb/Utility/VMRange.h"
23 namespace lldb_private {
27 using namespace lldb_private;
29 const char *Section::GetTypeAsCString() const {
31 case eSectionTypeInvalid:
33 case eSectionTypeCode:
35 case eSectionTypeContainer:
37 case eSectionTypeData:
39 case eSectionTypeDataCString:
41 case eSectionTypeDataCStringPointers:
42 return "data-cstr-ptr";
43 case eSectionTypeDataSymbolAddress:
44 return "data-symbol-addr";
45 case eSectionTypeData4:
47 case eSectionTypeData8:
49 case eSectionTypeData16:
50 return "data-16-byte";
51 case eSectionTypeDataPointers:
53 case eSectionTypeDebug:
55 case eSectionTypeZeroFill:
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:
67 case eSectionTypeDWARFDebugAranges:
68 return "dwarf-aranges";
69 case eSectionTypeDWARFDebugCuIndex:
70 return "dwarf-cu-index";
71 case eSectionTypeDWARFDebugFrame:
73 case eSectionTypeDWARFDebugInfo:
75 case eSectionTypeDWARFDebugInfoDwo:
76 return "dwarf-info-dwo";
77 case eSectionTypeDWARFDebugLine:
79 case eSectionTypeDWARFDebugLineStr:
80 return "dwarf-line-str";
81 case eSectionTypeDWARFDebugLoc:
83 case eSectionTypeDWARFDebugLocLists:
84 return "dwarf-loclists";
85 case eSectionTypeDWARFDebugMacInfo:
86 return "dwarf-macinfo";
87 case eSectionTypeDWARFDebugMacro:
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:
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:
127 case eSectionTypeEHFrame:
129 case eSectionTypeARMexidx:
131 case eSectionTypeARMextab:
133 case eSectionTypeCompactUnwind:
134 return "compact-unwind";
135 case eSectionTypeGoSymtab:
137 case eSectionTypeAbsoluteAddress:
139 case eSectionTypeDWARFGNUDebugAltLink:
140 return "dwarf-gnu-debugaltlink";
141 case eSectionTypeOther:
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());
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;
192 Section::~Section() {
193 // printf ("Section::~Section(%p)\n", this);
196 addr_t Section::GetFileAddress() const {
197 SectionSP parent_sp(GetParent());
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;
204 // This section has no parent, so m_file_addr is the file base address
208 bool Section::SetFileAddress(lldb::addr_t file_addr) {
209 SectionSP parent_sp(GetParent());
211 if (m_file_addr >= file_addr)
212 return parent_sp->SetFileAddress(m_file_addr - file_addr);
215 // This section has no parent, so m_file_addr is the file base address
216 m_file_addr = file_addr;
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());
227 // This section has no parent, so there is no offset to be had
231 addr_t Section::GetLoadBaseAddress(Target *target) const {
232 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
233 SectionSP parent_sp(GetParent());
235 load_base_addr = parent_sp->GetLoadBaseAddress(target);
236 if (load_base_addr != LLDB_INVALID_ADDRESS)
237 load_base_addr += GetOffset();
239 if (load_base_addr == LLDB_INVALID_ADDRESS) {
240 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
241 const_cast<Section *>(this)->shared_from_this());
243 return load_base_addr;
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();
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);
259 so_addr.SetOffset(offset);
260 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
262 #ifdef LLDB_CONFIGURATION_DEBUG
263 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
265 assert(GetModule().get());
270 bool Section::ContainsFileAddress(addr_t vm_addr) const {
271 const addr_t file_addr = GetFileAddress();
272 if (file_addr != LLDB_INVALID_ADDRESS) {
273 if (file_addr <= vm_addr) {
274 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
275 return offset < GetByteSize();
281 int Section::Compare(const Section &a, const Section &b) {
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)
292 if (a_sect_uid > b_sect_uid)
296 // The modules are different, just compare the module pointers
297 if (a_module_sp.get() < b_module_sp.get())
300 return 1; // We already know the modules aren't equal
304 void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
305 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
307 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
308 bool resolved = true;
309 addr_t addr = LLDB_INVALID_ADDRESS;
311 if (GetByteSize() == 0)
312 s->Printf("%39s", "");
315 addr = GetLoadBaseAddress(target);
317 if (addr == LLDB_INVALID_ADDRESS) {
320 addr = GetFileAddress();
323 VMRange range(addr, addr + m_byte_size);
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,
337 m_children.Dump(s, target, false, depth - 1);
340 void Section::DumpName(Stream *s) const {
341 SectionSP parent_sp(GetParent());
343 parent_sp->DumpName(s);
346 // The top most section prints the module basename
347 const char *name = nullptr;
348 ModuleSP module_sp(GetModule());
351 const FileSpec &file_spec = m_obj_file->GetFileSpec();
352 name = file_spec.GetFilename().AsCString();
354 if ((!name || !name[0]) && module_sp)
355 name = module_sp->GetFileSpec().GetFilename().AsCString();
357 s->Printf("%s.", name);
362 bool Section::IsDescendant(const Section *section) {
365 SectionSP parent_sp(GetParent());
367 return parent_sp->IsDescendant(section);
371 bool Section::Slide(addr_t slide_amount, bool slide_children) {
372 if (m_file_addr != LLDB_INVALID_ADDRESS) {
373 if (slide_amount == 0)
376 m_file_addr += slide_amount;
379 m_children.Slide(slide_amount, slide_children);
386 /// Get the permissions as OR'ed bits from lldb::Permissions
387 uint32_t Section::GetPermissions() const {
388 uint32_t permissions = 0;
390 permissions |= ePermissionsReadable;
392 permissions |= ePermissionsWritable;
394 permissions |= ePermissionsExecutable;
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;
405 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
406 lldb::offset_t offset) {
408 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
412 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) {
414 return m_obj_file->ReadSectionData(this, section_data);
418 #pragma mark SectionList
420 SectionList::SectionList() : m_sections() {}
422 SectionList::~SectionList() {}
424 SectionList &SectionList::operator=(const SectionList &rhs) {
426 m_sections = rhs.m_sections;
430 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {
432 size_t section_index = m_sections.size();
433 m_sections.push_back(section_sp);
434 return section_index;
437 return std::numeric_limits<size_t>::max();
440 // Warning, this can be slow as it's removing items from a std::vector.
441 bool SectionList::DeleteSection(size_t idx) {
442 if (idx < m_sections.size()) {
443 m_sections.erase(m_sections.begin() + idx);
449 size_t SectionList::FindSectionIndex(const Section *sect) {
451 iterator begin = m_sections.begin();
452 iterator end = m_sections.end();
453 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
454 if (sect_iter->get() == sect) {
455 // The secton was already in this section list
456 return std::distance(begin, sect_iter);
462 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {
463 size_t sect_idx = FindSectionIndex(sect_sp.get());
464 if (sect_idx == UINT32_MAX) {
465 sect_idx = AddSection(sect_sp);
470 bool SectionList::ReplaceSection(user_id_t sect_id,
471 const lldb::SectionSP §_sp,
473 iterator sect_iter, end = m_sections.end();
474 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
475 if ((*sect_iter)->GetID() == sect_id) {
476 *sect_iter = sect_sp;
478 } else if (depth > 0) {
481 .ReplaceSection(sect_id, sect_sp, depth - 1))
488 size_t SectionList::GetNumSections(uint32_t depth) const {
489 size_t count = m_sections.size();
491 const_iterator sect_iter, end = m_sections.end();
492 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
493 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
499 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
501 if (idx < m_sections.size())
502 sect_sp = m_sections[idx];
507 SectionList::FindSectionByName(ConstString section_dstr) const {
509 // Check if we have a valid section string
510 if (section_dstr && !m_sections.empty()) {
511 const_iterator sect_iter;
512 const_iterator end = m_sections.end();
513 for (sect_iter = m_sections.begin();
514 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
515 Section *child_section = sect_iter->get();
517 if (child_section->GetName() == section_dstr) {
518 sect_sp = *sect_iter;
521 child_section->GetChildren().FindSectionByName(section_dstr);
529 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
532 const_iterator sect_iter;
533 const_iterator end = m_sections.end();
534 for (sect_iter = m_sections.begin();
535 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
536 if ((*sect_iter)->GetID() == sect_id) {
537 sect_sp = *sect_iter;
540 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
547 SectionSP SectionList::FindSectionByType(SectionType sect_type,
549 size_t start_idx) const {
551 size_t num_sections = m_sections.size();
552 for (size_t idx = start_idx; idx < num_sections; ++idx) {
553 if (m_sections[idx]->GetType() == sect_type) {
554 sect_sp = m_sections[idx];
556 } else if (check_children) {
557 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
558 sect_type, check_children, 0);
566 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
567 uint32_t depth) const {
569 const_iterator sect_iter;
570 const_iterator end = m_sections.end();
571 for (sect_iter = m_sections.begin();
572 sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
573 Section *sect = sect_iter->get();
574 if (sect->ContainsFileAddress(vm_addr)) {
575 // The file address is in this section. We need to make sure one of our
576 // child sections doesn't contain this address as well as obeying the
577 // depth limit that was passed in.
579 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
582 if (sect_sp.get() == nullptr && !sect->IsFake())
583 sect_sp = *sect_iter;
589 bool SectionList::ContainsSection(user_id_t sect_id) const {
590 return FindSectionByID(sect_id).get() != nullptr;
593 void SectionList::Dump(Stream *s, Target *target, bool show_header,
594 uint32_t depth) const {
595 bool target_has_loaded_sections =
596 target && !target->GetSectionLoadList().IsEmpty();
597 if (show_header && !m_sections.empty()) {
599 s->Printf("SectID Type %s Address "
600 " Perm File Off. File Size Flags "
602 target_has_loaded_sections ? "Load" : "File");
604 s->PutCString("---------- ---------------- "
605 "--------------------------------------- ---- ---------- "
607 "---------- ----------------------------\n");
610 const_iterator sect_iter;
611 const_iterator end = m_sections.end();
612 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
613 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : nullptr, depth);
616 if (show_header && !m_sections.empty())
620 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
622 const_iterator pos, end = m_sections.end();
623 for (pos = m_sections.begin(); pos != end; ++pos) {
624 if ((*pos)->Slide(slide_amount, slide_children))