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" // for Address
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" // for FileSpec
17 #include "lldb/Utility/Stream.h" // for Stream
18 #include "lldb/Utility/VMRange.h" // for VMRange
20 #include <inttypes.h> // for PRIx64
21 #include <limits> // for numeric_limits
22 #include <utility> // for distance
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 eSectionTypeDWARFDebugAddr:
66 case eSectionTypeDWARFDebugAranges:
67 return "dwarf-aranges";
68 case eSectionTypeDWARFDebugCuIndex:
69 return "dwarf-cu-index";
70 case eSectionTypeDWARFDebugFrame:
72 case eSectionTypeDWARFDebugInfo:
74 case eSectionTypeDWARFDebugLine:
76 case eSectionTypeDWARFDebugLoc:
78 case eSectionTypeDWARFDebugMacInfo:
79 return "dwarf-macinfo";
80 case eSectionTypeDWARFDebugMacro:
82 case eSectionTypeDWARFDebugPubNames:
83 return "dwarf-pubnames";
84 case eSectionTypeDWARFDebugPubTypes:
85 return "dwarf-pubtypes";
86 case eSectionTypeDWARFDebugRanges:
87 return "dwarf-ranges";
88 case eSectionTypeDWARFDebugStr:
90 case eSectionTypeDWARFDebugStrOffsets:
91 return "dwarf-str-offsets";
92 case eSectionTypeDWARFDebugTypes:
94 case eSectionTypeDWARFDebugNames:
96 case eSectionTypeELFSymbolTable:
97 return "elf-symbol-table";
98 case eSectionTypeELFDynamicSymbols:
99 return "elf-dynamic-symbols";
100 case eSectionTypeELFRelocationEntries:
101 return "elf-relocation-entries";
102 case eSectionTypeELFDynamicLinkInfo:
103 return "elf-dynamic-link-info";
104 case eSectionTypeDWARFAppleNames:
105 return "apple-names";
106 case eSectionTypeDWARFAppleTypes:
107 return "apple-types";
108 case eSectionTypeDWARFAppleNamespaces:
109 return "apple-namespaces";
110 case eSectionTypeDWARFAppleObjC:
112 case eSectionTypeEHFrame:
114 case eSectionTypeARMexidx:
116 case eSectionTypeARMextab:
118 case eSectionTypeCompactUnwind:
119 return "compact-unwind";
120 case eSectionTypeGoSymtab:
122 case eSectionTypeAbsoluteAddress:
124 case eSectionTypeDWARFGNUDebugAltLink:
125 return "dwarf-gnu-debugaltlink";
126 case eSectionTypeOther:
132 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
133 user_id_t sect_id, const ConstString &name,
134 SectionType sect_type, addr_t file_addr, addr_t byte_size,
135 lldb::offset_t file_offset, lldb::offset_t file_size,
136 uint32_t log2align, uint32_t flags,
137 uint32_t target_byte_size /*=1*/)
138 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
139 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
140 m_file_addr(file_addr), m_byte_size(byte_size),
141 m_file_offset(file_offset), m_file_size(file_size),
142 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
143 m_thread_specific(false), m_readable(false), m_writable(false),
144 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
145 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
146 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
147 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
148 // this, module_sp.get(), sect_id, file_addr, file_addr +
149 // byte_size, file_offset, file_offset + file_size, flags,
150 // name.GetCString());
153 Section::Section(const lldb::SectionSP &parent_section_sp,
154 const ModuleSP &module_sp, ObjectFile *obj_file,
155 user_id_t sect_id, const 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) {
167 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
168 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
169 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
170 // this, module_sp.get(), sect_id, file_addr, file_addr +
171 // byte_size, file_offset, file_offset + file_size, flags,
172 // parent_section_sp->GetName().GetCString(), name.GetCString());
173 if (parent_section_sp)
174 m_parent_wp = parent_section_sp;
177 Section::~Section() {
178 // printf ("Section::~Section(%p)\n", this);
181 addr_t Section::GetFileAddress() const {
182 SectionSP parent_sp(GetParent());
184 // This section has a parent which means m_file_addr is an offset into the
185 // parent section, so the file address for this section is the file address
186 // of the parent plus the offset
187 return parent_sp->GetFileAddress() + m_file_addr;
189 // This section has no parent, so m_file_addr is the file base address
193 bool Section::SetFileAddress(lldb::addr_t file_addr) {
194 SectionSP parent_sp(GetParent());
196 if (m_file_addr >= file_addr)
197 return parent_sp->SetFileAddress(m_file_addr - file_addr);
200 // This section has no parent, so m_file_addr is the file base address
201 m_file_addr = file_addr;
206 lldb::addr_t Section::GetOffset() const {
207 // This section has a parent which means m_file_addr is an offset.
208 SectionSP parent_sp(GetParent());
212 // This section has no parent, so there is no offset to be had
216 addr_t Section::GetLoadBaseAddress(Target *target) const {
217 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
218 SectionSP parent_sp(GetParent());
220 load_base_addr = parent_sp->GetLoadBaseAddress(target);
221 if (load_base_addr != LLDB_INVALID_ADDRESS)
222 load_base_addr += GetOffset();
224 if (load_base_addr == LLDB_INVALID_ADDRESS) {
225 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
226 const_cast<Section *>(this)->shared_from_this());
228 return load_base_addr;
231 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
232 bool allow_section_end) const {
233 const size_t num_children = m_children.GetSize();
234 for (size_t i = 0; i < num_children; i++) {
235 Section *child_section = m_children.GetSectionAtIndex(i).get();
237 addr_t child_offset = child_section->GetOffset();
238 if (child_offset <= offset &&
239 offset - child_offset <
240 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
241 return child_section->ResolveContainedAddress(offset - child_offset,
242 so_addr, allow_section_end);
244 so_addr.SetOffset(offset);
245 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
247 #ifdef LLDB_CONFIGURATION_DEBUG
248 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
250 assert(GetModule().get());
255 bool Section::ContainsFileAddress(addr_t vm_addr) const {
256 const addr_t file_addr = GetFileAddress();
257 if (file_addr != LLDB_INVALID_ADDRESS) {
258 if (file_addr <= vm_addr) {
259 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
260 return offset < GetByteSize();
266 int Section::Compare(const Section &a, const Section &b) {
270 const ModuleSP a_module_sp = a.GetModule();
271 const ModuleSP b_module_sp = b.GetModule();
272 if (a_module_sp == b_module_sp) {
273 user_id_t a_sect_uid = a.GetID();
274 user_id_t b_sect_uid = b.GetID();
275 if (a_sect_uid < b_sect_uid)
277 if (a_sect_uid > b_sect_uid)
281 // The modules are different, just compare the module pointers
282 if (a_module_sp.get() < b_module_sp.get())
285 return 1; // We already know the modules aren't equal
289 void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
290 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
292 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
293 bool resolved = true;
294 addr_t addr = LLDB_INVALID_ADDRESS;
296 if (GetByteSize() == 0)
297 s->Printf("%39s", "");
300 addr = GetLoadBaseAddress(target);
302 if (addr == LLDB_INVALID_ADDRESS) {
305 addr = GetFileAddress();
308 VMRange range(addr, addr + m_byte_size);
312 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
313 resolved ? ' ' : '*', m_readable ? 'r' : '-',
314 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
322 m_children.Dump(s, target, false, depth - 1);
325 void Section::DumpName(Stream *s) const {
326 SectionSP parent_sp(GetParent());
328 parent_sp->DumpName(s);
331 // The top most section prints the module basename
332 const char *name = NULL;
333 ModuleSP module_sp(GetModule());
336 const FileSpec &file_spec = m_obj_file->GetFileSpec();
337 name = file_spec.GetFilename().AsCString();
339 if ((!name || !name[0]) && module_sp)
340 name = module_sp->GetFileSpec().GetFilename().AsCString();
342 s->Printf("%s.", name);
347 bool Section::IsDescendant(const Section *section) {
350 SectionSP parent_sp(GetParent());
352 return parent_sp->IsDescendant(section);
356 bool Section::Slide(addr_t slide_amount, bool slide_children) {
357 if (m_file_addr != LLDB_INVALID_ADDRESS) {
358 if (slide_amount == 0)
361 m_file_addr += slide_amount;
364 m_children.Slide(slide_amount, slide_children);
371 //------------------------------------------------------------------
372 /// Get the permissions as OR'ed bits from lldb::Permissions
373 //------------------------------------------------------------------
374 uint32_t Section::GetPermissions() const {
375 uint32_t permissions = 0;
377 permissions |= ePermissionsReadable;
379 permissions |= ePermissionsWritable;
381 permissions |= ePermissionsExecutable;
385 //------------------------------------------------------------------
386 /// Set the permissions using bits OR'ed from lldb::Permissions
387 //------------------------------------------------------------------
388 void Section::SetPermissions(uint32_t permissions) {
389 m_readable = (permissions & ePermissionsReadable) != 0;
390 m_writable = (permissions & ePermissionsWritable) != 0;
391 m_executable = (permissions & ePermissionsExecutable) != 0;
394 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
395 lldb::offset_t offset) {
397 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
401 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) {
403 return m_obj_file->ReadSectionData(this, section_data);
407 #pragma mark SectionList
409 SectionList::SectionList() : m_sections() {}
411 SectionList::~SectionList() {}
413 SectionList &SectionList::operator=(const SectionList &rhs) {
415 m_sections = rhs.m_sections;
419 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {
421 size_t section_index = m_sections.size();
422 m_sections.push_back(section_sp);
423 return section_index;
426 return std::numeric_limits<size_t>::max();
429 // Warning, this can be slow as it's removing items from a std::vector.
430 bool SectionList::DeleteSection(size_t idx) {
431 if (idx < m_sections.size()) {
432 m_sections.erase(m_sections.begin() + idx);
438 size_t SectionList::FindSectionIndex(const Section *sect) {
440 iterator begin = m_sections.begin();
441 iterator end = m_sections.end();
442 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
443 if (sect_iter->get() == sect) {
444 // The secton was already in this section list
445 return std::distance(begin, sect_iter);
451 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {
452 size_t sect_idx = FindSectionIndex(sect_sp.get());
453 if (sect_idx == UINT32_MAX) {
454 sect_idx = AddSection(sect_sp);
459 bool SectionList::ReplaceSection(user_id_t sect_id,
460 const lldb::SectionSP §_sp,
462 iterator sect_iter, end = m_sections.end();
463 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
464 if ((*sect_iter)->GetID() == sect_id) {
465 *sect_iter = sect_sp;
467 } else if (depth > 0) {
470 .ReplaceSection(sect_id, sect_sp, depth - 1))
477 size_t SectionList::GetNumSections(uint32_t depth) const {
478 size_t count = m_sections.size();
480 const_iterator sect_iter, end = m_sections.end();
481 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
482 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
488 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
490 if (idx < m_sections.size())
491 sect_sp = m_sections[idx];
496 SectionList::FindSectionByName(const ConstString §ion_dstr) const {
498 // Check if we have a valid section string
499 if (section_dstr && !m_sections.empty()) {
500 const_iterator sect_iter;
501 const_iterator end = m_sections.end();
502 for (sect_iter = m_sections.begin();
503 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
504 Section *child_section = sect_iter->get();
506 if (child_section->GetName() == section_dstr) {
507 sect_sp = *sect_iter;
510 child_section->GetChildren().FindSectionByName(section_dstr);
518 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
521 const_iterator sect_iter;
522 const_iterator end = m_sections.end();
523 for (sect_iter = m_sections.begin();
524 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
525 if ((*sect_iter)->GetID() == sect_id) {
526 sect_sp = *sect_iter;
529 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
536 SectionSP SectionList::FindSectionByType(SectionType sect_type,
538 size_t start_idx) const {
540 size_t num_sections = m_sections.size();
541 for (size_t idx = start_idx; idx < num_sections; ++idx) {
542 if (m_sections[idx]->GetType() == sect_type) {
543 sect_sp = m_sections[idx];
545 } else if (check_children) {
546 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
547 sect_type, check_children, 0);
555 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
556 uint32_t depth) const {
558 const_iterator sect_iter;
559 const_iterator end = m_sections.end();
560 for (sect_iter = m_sections.begin();
561 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
562 Section *sect = sect_iter->get();
563 if (sect->ContainsFileAddress(vm_addr)) {
564 // The file address is in this section. We need to make sure one of our
565 // child sections doesn't contain this address as well as obeying the
566 // depth limit that was passed in.
568 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
571 if (sect_sp.get() == NULL && !sect->IsFake())
572 sect_sp = *sect_iter;
578 bool SectionList::ContainsSection(user_id_t sect_id) const {
579 return FindSectionByID(sect_id).get() != NULL;
582 void SectionList::Dump(Stream *s, Target *target, bool show_header,
583 uint32_t depth) const {
584 bool target_has_loaded_sections =
585 target && !target->GetSectionLoadList().IsEmpty();
586 if (show_header && !m_sections.empty()) {
588 s->Printf("SectID Type %s Address "
589 " Perm File Off. File Size Flags "
591 target_has_loaded_sections ? "Load" : "File");
593 s->PutCString("---------- ---------------- "
594 "--------------------------------------- ---- ---------- "
596 "---------- ----------------------------\n");
599 const_iterator sect_iter;
600 const_iterator end = m_sections.end();
601 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
602 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
605 if (show_header && !m_sections.empty())
609 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
611 const_iterator pos, end = m_sections.end();
612 for (pos = m_sections.begin(); pos != end; ++pos) {
613 if ((*pos)->Slide(slide_amount, slide_children))