1 //===-- Section.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Section.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/SectionLoadList.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/VMRange.h"
24 namespace lldb_private {
28 using namespace lldb_private;
30 const char *Section::GetTypeAsCString() const {
32 case eSectionTypeInvalid:
34 case eSectionTypeCode:
36 case eSectionTypeContainer:
38 case eSectionTypeData:
40 case eSectionTypeDataCString:
42 case eSectionTypeDataCStringPointers:
43 return "data-cstr-ptr";
44 case eSectionTypeDataSymbolAddress:
45 return "data-symbol-addr";
46 case eSectionTypeData4:
48 case eSectionTypeData8:
50 case eSectionTypeData16:
51 return "data-16-byte";
52 case eSectionTypeDataPointers:
54 case eSectionTypeDebug:
56 case eSectionTypeZeroFill:
58 case eSectionTypeDataObjCMessageRefs:
59 return "objc-message-refs";
60 case eSectionTypeDataObjCCFStrings:
61 return "objc-cfstrings";
62 case eSectionTypeDWARFDebugAbbrev:
63 return "dwarf-abbrev";
64 case eSectionTypeDWARFDebugAbbrevDwo:
65 return "dwarf-abbrev-dwo";
66 case eSectionTypeDWARFDebugAddr:
68 case eSectionTypeDWARFDebugAranges:
69 return "dwarf-aranges";
70 case eSectionTypeDWARFDebugCuIndex:
71 return "dwarf-cu-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 eSectionTypeDWARFDebugLocLists:
85 return "dwarf-loclists";
86 case eSectionTypeDWARFDebugMacInfo:
87 return "dwarf-macinfo";
88 case eSectionTypeDWARFDebugMacro:
90 case eSectionTypeDWARFDebugPubNames:
91 return "dwarf-pubnames";
92 case eSectionTypeDWARFDebugPubTypes:
93 return "dwarf-pubtypes";
94 case eSectionTypeDWARFDebugRanges:
95 return "dwarf-ranges";
96 case eSectionTypeDWARFDebugRngLists:
97 return "dwarf-rnglists";
98 case eSectionTypeDWARFDebugStr:
100 case eSectionTypeDWARFDebugStrDwo:
101 return "dwarf-str-dwo";
102 case eSectionTypeDWARFDebugStrOffsets:
103 return "dwarf-str-offsets";
104 case eSectionTypeDWARFDebugStrOffsetsDwo:
105 return "dwarf-str-offsets-dwo";
106 case eSectionTypeDWARFDebugTypes:
107 return "dwarf-types";
108 case eSectionTypeDWARFDebugNames:
109 return "dwarf-names";
110 case eSectionTypeELFSymbolTable:
111 return "elf-symbol-table";
112 case eSectionTypeELFDynamicSymbols:
113 return "elf-dynamic-symbols";
114 case eSectionTypeELFRelocationEntries:
115 return "elf-relocation-entries";
116 case eSectionTypeELFDynamicLinkInfo:
117 return "elf-dynamic-link-info";
118 case eSectionTypeDWARFAppleNames:
119 return "apple-names";
120 case eSectionTypeDWARFAppleTypes:
121 return "apple-types";
122 case eSectionTypeDWARFAppleNamespaces:
123 return "apple-namespaces";
124 case eSectionTypeDWARFAppleObjC:
126 case eSectionTypeEHFrame:
128 case eSectionTypeARMexidx:
130 case eSectionTypeARMextab:
132 case eSectionTypeCompactUnwind:
133 return "compact-unwind";
134 case eSectionTypeGoSymtab:
136 case eSectionTypeAbsoluteAddress:
138 case eSectionTypeDWARFGNUDebugAltLink:
139 return "dwarf-gnu-debugaltlink";
140 case eSectionTypeOther:
146 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
147 user_id_t sect_id, const ConstString &name,
148 SectionType sect_type, addr_t file_addr, addr_t byte_size,
149 lldb::offset_t file_offset, lldb::offset_t file_size,
150 uint32_t log2align, uint32_t flags,
151 uint32_t target_byte_size /*=1*/)
152 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
153 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
154 m_file_addr(file_addr), m_byte_size(byte_size),
155 m_file_offset(file_offset), m_file_size(file_size),
156 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
157 m_thread_specific(false), m_readable(false), m_writable(false),
158 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
159 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
160 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
161 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
162 // this, module_sp.get(), sect_id, file_addr, file_addr +
163 // byte_size, file_offset, file_offset + file_size, flags,
164 // name.GetCString());
167 Section::Section(const lldb::SectionSP &parent_section_sp,
168 const ModuleSP &module_sp, ObjectFile *obj_file,
169 user_id_t sect_id, const ConstString &name,
170 SectionType sect_type, addr_t file_addr, addr_t byte_size,
171 lldb::offset_t file_offset, lldb::offset_t file_size,
172 uint32_t log2align, uint32_t flags,
173 uint32_t target_byte_size /*=1*/)
174 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
175 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
176 m_file_addr(file_addr), m_byte_size(byte_size),
177 m_file_offset(file_offset), m_file_size(file_size),
178 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
179 m_thread_specific(false), m_readable(false), m_writable(false),
180 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
181 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
182 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
183 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
184 // this, module_sp.get(), sect_id, file_addr, file_addr +
185 // byte_size, file_offset, file_offset + file_size, flags,
186 // parent_section_sp->GetName().GetCString(), name.GetCString());
187 if (parent_section_sp)
188 m_parent_wp = parent_section_sp;
191 Section::~Section() {
192 // printf ("Section::~Section(%p)\n", this);
195 addr_t Section::GetFileAddress() const {
196 SectionSP parent_sp(GetParent());
198 // This section has a parent which means m_file_addr is an offset into the
199 // parent section, so the file address for this section is the file address
200 // of the parent plus the offset
201 return parent_sp->GetFileAddress() + m_file_addr;
203 // This section has no parent, so m_file_addr is the file base address
207 bool Section::SetFileAddress(lldb::addr_t file_addr) {
208 SectionSP parent_sp(GetParent());
210 if (m_file_addr >= file_addr)
211 return parent_sp->SetFileAddress(m_file_addr - file_addr);
214 // This section has no parent, so m_file_addr is the file base address
215 m_file_addr = file_addr;
220 lldb::addr_t Section::GetOffset() const {
221 // This section has a parent which means m_file_addr is an offset.
222 SectionSP parent_sp(GetParent());
226 // This section has no parent, so there is no offset to be had
230 addr_t Section::GetLoadBaseAddress(Target *target) const {
231 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
232 SectionSP parent_sp(GetParent());
234 load_base_addr = parent_sp->GetLoadBaseAddress(target);
235 if (load_base_addr != LLDB_INVALID_ADDRESS)
236 load_base_addr += GetOffset();
238 if (load_base_addr == LLDB_INVALID_ADDRESS) {
239 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
240 const_cast<Section *>(this)->shared_from_this());
242 return load_base_addr;
245 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
246 bool allow_section_end) const {
247 const size_t num_children = m_children.GetSize();
248 for (size_t i = 0; i < num_children; i++) {
249 Section *child_section = m_children.GetSectionAtIndex(i).get();
251 addr_t child_offset = child_section->GetOffset();
252 if (child_offset <= offset &&
253 offset - child_offset <
254 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
255 return child_section->ResolveContainedAddress(offset - child_offset,
256 so_addr, allow_section_end);
258 so_addr.SetOffset(offset);
259 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
261 #ifdef LLDB_CONFIGURATION_DEBUG
262 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
264 assert(GetModule().get());
269 bool Section::ContainsFileAddress(addr_t vm_addr) const {
270 const addr_t file_addr = GetFileAddress();
271 if (file_addr != LLDB_INVALID_ADDRESS) {
272 if (file_addr <= vm_addr) {
273 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
274 return offset < GetByteSize();
280 int Section::Compare(const Section &a, const Section &b) {
284 const ModuleSP a_module_sp = a.GetModule();
285 const ModuleSP b_module_sp = b.GetModule();
286 if (a_module_sp == b_module_sp) {
287 user_id_t a_sect_uid = a.GetID();
288 user_id_t b_sect_uid = b.GetID();
289 if (a_sect_uid < b_sect_uid)
291 if (a_sect_uid > b_sect_uid)
295 // The modules are different, just compare the module pointers
296 if (a_module_sp.get() < b_module_sp.get())
299 return 1; // We already know the modules aren't equal
303 void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
304 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
306 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
307 bool resolved = true;
308 addr_t addr = LLDB_INVALID_ADDRESS;
310 if (GetByteSize() == 0)
311 s->Printf("%39s", "");
314 addr = GetLoadBaseAddress(target);
316 if (addr == LLDB_INVALID_ADDRESS) {
319 addr = GetFileAddress();
322 VMRange range(addr, addr + m_byte_size);
326 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
327 resolved ? ' ' : '*', m_readable ? 'r' : '-',
328 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
336 m_children.Dump(s, target, false, depth - 1);
339 void Section::DumpName(Stream *s) const {
340 SectionSP parent_sp(GetParent());
342 parent_sp->DumpName(s);
345 // The top most section prints the module basename
346 const char *name = NULL;
347 ModuleSP module_sp(GetModule());
350 const FileSpec &file_spec = m_obj_file->GetFileSpec();
351 name = file_spec.GetFilename().AsCString();
353 if ((!name || !name[0]) && module_sp)
354 name = module_sp->GetFileSpec().GetFilename().AsCString();
356 s->Printf("%s.", name);
361 bool Section::IsDescendant(const Section *section) {
364 SectionSP parent_sp(GetParent());
366 return parent_sp->IsDescendant(section);
370 bool Section::Slide(addr_t slide_amount, bool slide_children) {
371 if (m_file_addr != LLDB_INVALID_ADDRESS) {
372 if (slide_amount == 0)
375 m_file_addr += slide_amount;
378 m_children.Slide(slide_amount, slide_children);
385 //------------------------------------------------------------------
386 /// Get the permissions as OR'ed bits from lldb::Permissions
387 //------------------------------------------------------------------
388 uint32_t Section::GetPermissions() const {
389 uint32_t permissions = 0;
391 permissions |= ePermissionsReadable;
393 permissions |= ePermissionsWritable;
395 permissions |= ePermissionsExecutable;
399 //------------------------------------------------------------------
400 /// Set the permissions using bits OR'ed from lldb::Permissions
401 //------------------------------------------------------------------
402 void Section::SetPermissions(uint32_t permissions) {
403 m_readable = (permissions & ePermissionsReadable) != 0;
404 m_writable = (permissions & ePermissionsWritable) != 0;
405 m_executable = (permissions & ePermissionsExecutable) != 0;
408 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
409 lldb::offset_t offset) {
411 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
415 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) {
417 return m_obj_file->ReadSectionData(this, section_data);
421 #pragma mark SectionList
423 SectionList::SectionList() : m_sections() {}
425 SectionList::~SectionList() {}
427 SectionList &SectionList::operator=(const SectionList &rhs) {
429 m_sections = rhs.m_sections;
433 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {
435 size_t section_index = m_sections.size();
436 m_sections.push_back(section_sp);
437 return section_index;
440 return std::numeric_limits<size_t>::max();
443 // Warning, this can be slow as it's removing items from a std::vector.
444 bool SectionList::DeleteSection(size_t idx) {
445 if (idx < m_sections.size()) {
446 m_sections.erase(m_sections.begin() + idx);
452 size_t SectionList::FindSectionIndex(const Section *sect) {
454 iterator begin = m_sections.begin();
455 iterator end = m_sections.end();
456 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
457 if (sect_iter->get() == sect) {
458 // The secton was already in this section list
459 return std::distance(begin, sect_iter);
465 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {
466 size_t sect_idx = FindSectionIndex(sect_sp.get());
467 if (sect_idx == UINT32_MAX) {
468 sect_idx = AddSection(sect_sp);
473 bool SectionList::ReplaceSection(user_id_t sect_id,
474 const lldb::SectionSP §_sp,
476 iterator sect_iter, end = m_sections.end();
477 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
478 if ((*sect_iter)->GetID() == sect_id) {
479 *sect_iter = sect_sp;
481 } else if (depth > 0) {
484 .ReplaceSection(sect_id, sect_sp, depth - 1))
491 size_t SectionList::GetNumSections(uint32_t depth) const {
492 size_t count = m_sections.size();
494 const_iterator sect_iter, end = m_sections.end();
495 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
496 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
502 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
504 if (idx < m_sections.size())
505 sect_sp = m_sections[idx];
510 SectionList::FindSectionByName(const ConstString §ion_dstr) const {
512 // Check if we have a valid section string
513 if (section_dstr && !m_sections.empty()) {
514 const_iterator sect_iter;
515 const_iterator end = m_sections.end();
516 for (sect_iter = m_sections.begin();
517 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
518 Section *child_section = sect_iter->get();
520 if (child_section->GetName() == section_dstr) {
521 sect_sp = *sect_iter;
524 child_section->GetChildren().FindSectionByName(section_dstr);
532 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
535 const_iterator sect_iter;
536 const_iterator end = m_sections.end();
537 for (sect_iter = m_sections.begin();
538 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
539 if ((*sect_iter)->GetID() == sect_id) {
540 sect_sp = *sect_iter;
543 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
550 SectionSP SectionList::FindSectionByType(SectionType sect_type,
552 size_t start_idx) const {
554 size_t num_sections = m_sections.size();
555 for (size_t idx = start_idx; idx < num_sections; ++idx) {
556 if (m_sections[idx]->GetType() == sect_type) {
557 sect_sp = m_sections[idx];
559 } else if (check_children) {
560 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
561 sect_type, check_children, 0);
569 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
570 uint32_t depth) const {
572 const_iterator sect_iter;
573 const_iterator end = m_sections.end();
574 for (sect_iter = m_sections.begin();
575 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
576 Section *sect = sect_iter->get();
577 if (sect->ContainsFileAddress(vm_addr)) {
578 // The file address is in this section. We need to make sure one of our
579 // child sections doesn't contain this address as well as obeying the
580 // depth limit that was passed in.
582 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
585 if (sect_sp.get() == NULL && !sect->IsFake())
586 sect_sp = *sect_iter;
592 bool SectionList::ContainsSection(user_id_t sect_id) const {
593 return FindSectionByID(sect_id).get() != NULL;
596 void SectionList::Dump(Stream *s, Target *target, bool show_header,
597 uint32_t depth) const {
598 bool target_has_loaded_sections =
599 target && !target->GetSectionLoadList().IsEmpty();
600 if (show_header && !m_sections.empty()) {
602 s->Printf("SectID Type %s Address "
603 " Perm File Off. File Size Flags "
605 target_has_loaded_sections ? "Load" : "File");
607 s->PutCString("---------- ---------------- "
608 "--------------------------------------- ---- ---------- "
610 "---------- ----------------------------\n");
613 const_iterator sect_iter;
614 const_iterator end = m_sections.end();
615 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
616 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
619 if (show_header && !m_sections.empty())
623 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
625 const_iterator pos, end = m_sections.end();
626 for (pos = m_sections.begin(); pos != end; ++pos) {
627 if ((*pos)->Slide(slide_amount, slide_children))