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/Module.h"
12 #include "lldb/Symbol/ObjectFile.h"
13 #include "lldb/Target/SectionLoadList.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/ConvertEnum.h"
18 using namespace lldb_private;
20 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
21 user_id_t sect_id, const ConstString &name,
22 SectionType sect_type, addr_t file_addr, addr_t byte_size,
23 lldb::offset_t file_offset, lldb::offset_t file_size,
24 uint32_t log2align, uint32_t flags,
25 uint32_t target_byte_size /*=1*/)
26 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
27 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
28 m_file_addr(file_addr), m_byte_size(byte_size),
29 m_file_offset(file_offset), m_file_size(file_size),
30 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
31 m_thread_specific(false), m_readable(false), m_writable(false),
32 m_executable(false), m_target_byte_size(target_byte_size) {
33 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
34 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
35 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
36 // this, module_sp.get(), sect_id, file_addr, file_addr +
37 // byte_size, file_offset, file_offset + file_size, flags,
38 // name.GetCString());
41 Section::Section(const lldb::SectionSP &parent_section_sp,
42 const ModuleSP &module_sp, ObjectFile *obj_file,
43 user_id_t sect_id, const ConstString &name,
44 SectionType sect_type, addr_t file_addr, addr_t byte_size,
45 lldb::offset_t file_offset, lldb::offset_t file_size,
46 uint32_t log2align, uint32_t flags,
47 uint32_t target_byte_size /*=1*/)
48 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
49 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
50 m_file_addr(file_addr), m_byte_size(byte_size),
51 m_file_offset(file_offset), m_file_size(file_size),
52 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
53 m_thread_specific(false), m_readable(false), m_writable(false),
54 m_executable(false), m_target_byte_size(target_byte_size) {
55 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
56 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
57 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
58 // this, module_sp.get(), sect_id, file_addr, file_addr +
59 // byte_size, file_offset, file_offset + file_size, flags,
60 // parent_section_sp->GetName().GetCString(), name.GetCString());
61 if (parent_section_sp)
62 m_parent_wp = parent_section_sp;
66 // printf ("Section::~Section(%p)\n", this);
69 addr_t Section::GetFileAddress() const {
70 SectionSP parent_sp(GetParent());
72 // This section has a parent which means m_file_addr is an offset into
73 // the parent section, so the file address for this section is the file
74 // address of the parent plus the offset
75 return parent_sp->GetFileAddress() + m_file_addr;
77 // This section has no parent, so m_file_addr is the file base address
81 bool Section::SetFileAddress(lldb::addr_t file_addr) {
82 SectionSP parent_sp(GetParent());
84 if (m_file_addr >= file_addr)
85 return parent_sp->SetFileAddress(m_file_addr - file_addr);
88 // This section has no parent, so m_file_addr is the file base address
89 m_file_addr = file_addr;
94 lldb::addr_t Section::GetOffset() const {
95 // This section has a parent which means m_file_addr is an offset.
96 SectionSP parent_sp(GetParent());
100 // This section has no parent, so there is no offset to be had
104 addr_t Section::GetLoadBaseAddress(Target *target) const {
105 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
106 SectionSP parent_sp(GetParent());
108 load_base_addr = parent_sp->GetLoadBaseAddress(target);
109 if (load_base_addr != LLDB_INVALID_ADDRESS)
110 load_base_addr += GetOffset();
112 if (load_base_addr == LLDB_INVALID_ADDRESS) {
113 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
114 const_cast<Section *>(this)->shared_from_this());
116 return load_base_addr;
119 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr) const {
120 const size_t num_children = m_children.GetSize();
121 if (num_children > 0) {
122 for (size_t i = 0; i < num_children; i++) {
123 Section *child_section = m_children.GetSectionAtIndex(i).get();
125 addr_t child_offset = child_section->GetOffset();
126 if (child_offset <= offset &&
127 offset - child_offset < child_section->GetByteSize())
128 return child_section->ResolveContainedAddress(offset - child_offset,
132 so_addr.SetOffset(offset);
133 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
135 #ifdef LLDB_CONFIGURATION_DEBUG
136 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
138 assert(GetModule().get());
143 bool Section::ContainsFileAddress(addr_t vm_addr) const {
144 const addr_t file_addr = GetFileAddress();
145 if (file_addr != LLDB_INVALID_ADDRESS) {
146 if (file_addr <= vm_addr) {
147 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
148 return offset < GetByteSize();
154 int Section::Compare(const Section &a, const Section &b) {
158 const ModuleSP a_module_sp = a.GetModule();
159 const ModuleSP b_module_sp = b.GetModule();
160 if (a_module_sp == b_module_sp) {
161 user_id_t a_sect_uid = a.GetID();
162 user_id_t b_sect_uid = b.GetID();
163 if (a_sect_uid < b_sect_uid)
165 if (a_sect_uid > b_sect_uid)
169 // The modules are different, just compare the module pointers
170 if (a_module_sp.get() < b_module_sp.get())
173 return 1; // We already know the modules aren't equal
177 void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
178 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
180 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(),
181 GetSectionTypeAsCString(m_type));
182 bool resolved = true;
183 addr_t addr = LLDB_INVALID_ADDRESS;
185 if (GetByteSize() == 0)
186 s->Printf("%39s", "");
189 addr = GetLoadBaseAddress(target);
191 if (addr == LLDB_INVALID_ADDRESS) {
194 addr = GetFileAddress();
197 VMRange range(addr, addr + m_byte_size);
201 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
202 resolved ? ' ' : '*', m_readable ? 'r' : '-',
203 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
211 m_children.Dump(s, target, false, depth - 1);
214 void Section::DumpName(Stream *s) const {
215 SectionSP parent_sp(GetParent());
217 parent_sp->DumpName(s);
220 // The top most section prints the module basename
221 const char *name = NULL;
222 ModuleSP module_sp(GetModule());
223 const FileSpec &file_spec = m_obj_file->GetFileSpec();
226 name = file_spec.GetFilename().AsCString();
227 if ((!name || !name[0]) && module_sp)
228 name = module_sp->GetFileSpec().GetFilename().AsCString();
230 s->Printf("%s.", name);
235 bool Section::IsDescendant(const Section *section) {
238 SectionSP parent_sp(GetParent());
240 return parent_sp->IsDescendant(section);
244 bool Section::Slide(addr_t slide_amount, bool slide_children) {
245 if (m_file_addr != LLDB_INVALID_ADDRESS) {
246 if (slide_amount == 0)
249 m_file_addr += slide_amount;
252 m_children.Slide(slide_amount, slide_children);
259 //------------------------------------------------------------------
260 /// Get the permissions as OR'ed bits from lldb::Permissions
261 //------------------------------------------------------------------
262 uint32_t Section::GetPermissions() const {
263 uint32_t permissions = 0;
265 permissions |= ePermissionsReadable;
267 permissions |= ePermissionsWritable;
269 permissions |= ePermissionsExecutable;
273 //------------------------------------------------------------------
274 /// Set the permissions using bits OR'ed from lldb::Permissions
275 //------------------------------------------------------------------
276 void Section::SetPermissions(uint32_t permissions) {
277 m_readable = (permissions & ePermissionsReadable) != 0;
278 m_writable = (permissions & ePermissionsWritable) != 0;
279 m_executable = (permissions & ePermissionsExecutable) != 0;
282 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
283 lldb::offset_t offset) {
285 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
289 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) const {
291 return m_obj_file->ReadSectionData(this, section_data);
295 #pragma mark SectionList
297 SectionList::SectionList() : m_sections() {}
299 SectionList::~SectionList() {}
301 SectionList &SectionList::operator=(const SectionList &rhs) {
303 m_sections = rhs.m_sections;
307 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) {
309 size_t section_index = m_sections.size();
310 m_sections.push_back(section_sp);
311 return section_index;
314 return std::numeric_limits<size_t>::max();
317 // Warning, this can be slow as it's removing items from a std::vector.
318 bool SectionList::DeleteSection(size_t idx) {
319 if (idx < m_sections.size()) {
320 m_sections.erase(m_sections.begin() + idx);
326 size_t SectionList::FindSectionIndex(const Section *sect) {
328 iterator begin = m_sections.begin();
329 iterator end = m_sections.end();
330 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
331 if (sect_iter->get() == sect) {
332 // The secton was already in this section list
333 return std::distance(begin, sect_iter);
339 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) {
340 size_t sect_idx = FindSectionIndex(sect_sp.get());
341 if (sect_idx == UINT32_MAX) {
342 sect_idx = AddSection(sect_sp);
347 bool SectionList::ReplaceSection(user_id_t sect_id,
348 const lldb::SectionSP §_sp,
350 iterator sect_iter, end = m_sections.end();
351 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
352 if ((*sect_iter)->GetID() == sect_id) {
353 *sect_iter = sect_sp;
355 } else if (depth > 0) {
358 .ReplaceSection(sect_id, sect_sp, depth - 1))
365 size_t SectionList::GetNumSections(uint32_t depth) const {
366 size_t count = m_sections.size();
368 const_iterator sect_iter, end = m_sections.end();
369 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
370 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
376 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
378 if (idx < m_sections.size())
379 sect_sp = m_sections[idx];
384 SectionList::FindSectionByName(const ConstString §ion_dstr) const {
386 // Check if we have a valid section string
387 if (section_dstr && !m_sections.empty()) {
388 const_iterator sect_iter;
389 const_iterator end = m_sections.end();
390 for (sect_iter = m_sections.begin();
391 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
392 Section *child_section = sect_iter->get();
394 if (child_section->GetName() == section_dstr) {
395 sect_sp = *sect_iter;
398 child_section->GetChildren().FindSectionByName(section_dstr);
406 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
409 const_iterator sect_iter;
410 const_iterator end = m_sections.end();
411 for (sect_iter = m_sections.begin();
412 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
413 if ((*sect_iter)->GetID() == sect_id) {
414 sect_sp = *sect_iter;
417 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
424 SectionSP SectionList::FindSectionByType(SectionType sect_type,
426 size_t start_idx) const {
428 size_t num_sections = m_sections.size();
429 for (size_t idx = start_idx; idx < num_sections; ++idx) {
430 if (m_sections[idx]->GetType() == sect_type) {
431 sect_sp = m_sections[idx];
433 } else if (check_children) {
434 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
435 sect_type, check_children, 0);
443 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
444 uint32_t depth) const {
446 const_iterator sect_iter;
447 const_iterator end = m_sections.end();
448 for (sect_iter = m_sections.begin();
449 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
450 Section *sect = sect_iter->get();
451 if (sect->ContainsFileAddress(vm_addr)) {
452 // The file address is in this section. We need to make sure one of our
454 // sections doesn't contain this address as well as obeying the depth
456 // that was passed in.
458 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
461 if (sect_sp.get() == NULL && !sect->IsFake())
462 sect_sp = *sect_iter;
468 bool SectionList::ContainsSection(user_id_t sect_id) const {
469 return FindSectionByID(sect_id).get() != NULL;
472 void SectionList::Dump(Stream *s, Target *target, bool show_header,
473 uint32_t depth) const {
474 bool target_has_loaded_sections =
475 target && !target->GetSectionLoadList().IsEmpty();
476 if (show_header && !m_sections.empty()) {
478 s->Printf("SectID Type %s Address "
479 " Perm File Off. File Size Flags "
481 target_has_loaded_sections ? "Load" : "File");
483 s->PutCString("---------- ---------------- "
484 "--------------------------------------- ---- ---------- "
486 "---------- ----------------------------\n");
489 const_iterator sect_iter;
490 const_iterator end = m_sections.end();
491 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
492 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
495 if (show_header && !m_sections.empty())
499 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
501 const_iterator pos, end = m_sections.end();
502 for (pos = m_sections.begin(); pos != end; ++pos) {
503 if ((*pos)->Slide(slide_amount, slide_children))