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"
20 using namespace lldb_private;
22 Section::Section (const ModuleSP &module_sp,
25 const ConstString &name,
26 SectionType sect_type,
29 lldb::offset_t file_offset,
30 lldb::offset_t file_size,
33 uint32_t target_byte_size/*=1*/) :
34 ModuleChild (module_sp),
37 m_obj_file (obj_file),
41 m_file_addr (file_addr),
42 m_byte_size (byte_size),
43 m_file_offset (file_offset),
44 m_file_size (file_size),
45 m_log2align (log2align),
49 m_thread_specific (false),
50 m_target_byte_size(target_byte_size)
52 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
53 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
56 Section::Section (const lldb::SectionSP &parent_section_sp,
57 const ModuleSP &module_sp,
60 const ConstString &name,
61 SectionType sect_type,
64 lldb::offset_t file_offset,
65 lldb::offset_t file_size,
68 uint32_t target_byte_size/*=1*/) :
69 ModuleChild (module_sp),
72 m_obj_file (obj_file),
76 m_file_addr (file_addr),
77 m_byte_size (byte_size),
78 m_file_offset (file_offset),
79 m_file_size (file_size),
80 m_log2align (log2align),
84 m_thread_specific (false),
85 m_target_byte_size(target_byte_size)
87 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
88 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, parent_section_sp->GetName().GetCString(), name.GetCString());
89 if (parent_section_sp)
90 m_parent_wp = parent_section_sp;
95 // printf ("Section::~Section(%p)\n", this);
99 Section::GetFileAddress () const
101 SectionSP parent_sp (GetParent ());
104 // This section has a parent which means m_file_addr is an offset into
105 // the parent section, so the file address for this section is the file
106 // address of the parent plus the offset
107 return parent_sp->GetFileAddress() + m_file_addr;
109 // This section has no parent, so m_file_addr is the file base address
114 Section::SetFileAddress (lldb::addr_t file_addr)
116 SectionSP parent_sp (GetParent ());
119 if (m_file_addr >= file_addr)
120 return parent_sp->SetFileAddress (m_file_addr - file_addr);
125 // This section has no parent, so m_file_addr is the file base address
126 m_file_addr = file_addr;
132 Section::GetOffset () const
134 // This section has a parent which means m_file_addr is an offset.
135 SectionSP parent_sp (GetParent ());
139 // This section has no parent, so there is no offset to be had
144 Section::GetLoadBaseAddress (Target *target) const
146 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
147 SectionSP parent_sp (GetParent ());
150 load_base_addr = parent_sp->GetLoadBaseAddress (target);
151 if (load_base_addr != LLDB_INVALID_ADDRESS)
152 load_base_addr += GetOffset();
154 if (load_base_addr == LLDB_INVALID_ADDRESS)
156 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
158 return load_base_addr;
162 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
164 const size_t num_children = m_children.GetSize();
165 if (num_children > 0)
167 for (size_t i=0; i<num_children; i++)
169 Section* child_section = m_children.GetSectionAtIndex (i).get();
171 addr_t child_offset = child_section->GetOffset();
172 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
173 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
176 so_addr.SetOffset(offset);
177 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
179 #ifdef LLDB_CONFIGURATION_DEBUG
180 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
181 assert(GetModule().get());
187 Section::ContainsFileAddress (addr_t vm_addr) const
189 const addr_t file_addr = GetFileAddress();
190 if (file_addr != LLDB_INVALID_ADDRESS)
192 if (file_addr <= vm_addr)
194 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
195 return offset < GetByteSize();
202 Section::Compare (const Section& a, const Section& b)
207 const ModuleSP a_module_sp = a.GetModule();
208 const ModuleSP b_module_sp = b.GetModule();
209 if (a_module_sp == b_module_sp)
211 user_id_t a_sect_uid = a.GetID();
212 user_id_t b_sect_uid = b.GetID();
213 if (a_sect_uid < b_sect_uid)
215 if (a_sect_uid > b_sect_uid)
221 // The modules are different, just compare the module pointers
222 if (a_module_sp.get() < b_module_sp.get())
225 return 1; // We already know the modules aren't equal
231 Section::Dump (Stream *s, Target *target, uint32_t depth) const
233 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
235 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
236 bool resolved = true;
237 addr_t addr = LLDB_INVALID_ADDRESS;
239 if (GetByteSize() == 0)
240 s->Printf("%39s", "");
244 addr = GetLoadBaseAddress (target);
246 if (addr == LLDB_INVALID_ADDRESS)
250 addr = GetFileAddress();
253 VMRange range(addr, addr + m_byte_size);
257 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
264 m_children.Dump(s, target, false, depth - 1);
268 Section::DumpName (Stream *s) const
270 SectionSP parent_sp (GetParent ());
273 parent_sp->DumpName (s);
278 // The top most section prints the module basename
279 const char * name = NULL;
280 ModuleSP module_sp (GetModule());
281 const FileSpec &file_spec = m_obj_file->GetFileSpec();
284 name = file_spec.GetFilename().AsCString();
285 if ((!name || !name[0]) && module_sp)
286 name = module_sp->GetFileSpec().GetFilename().AsCString();
288 s->Printf("%s.", name);
294 Section::IsDescendant (const Section *section)
298 SectionSP parent_sp (GetParent ());
300 return parent_sp->IsDescendant (section);
305 Section::Slide (addr_t slide_amount, bool slide_children)
307 if (m_file_addr != LLDB_INVALID_ADDRESS)
309 if (slide_amount == 0)
312 m_file_addr += slide_amount;
315 m_children.Slide (slide_amount, slide_children);
322 #pragma mark SectionList
324 SectionList::SectionList () :
330 SectionList::~SectionList ()
335 SectionList::operator = (const SectionList& rhs)
338 m_sections = rhs.m_sections;
343 SectionList::AddSection (const lldb::SectionSP& section_sp)
347 size_t section_index = m_sections.size();
348 m_sections.push_back(section_sp);
349 return section_index;
352 return std::numeric_limits<size_t>::max ();
355 // Warning, this can be slow as it's removing items from a std::vector.
357 SectionList::DeleteSection (size_t idx)
359 if (idx < m_sections.size())
361 m_sections.erase (m_sections.begin() + idx);
368 SectionList::FindSectionIndex (const Section* sect)
371 iterator begin = m_sections.begin();
372 iterator end = m_sections.end();
373 for (sect_iter = begin; sect_iter != end; ++sect_iter)
375 if (sect_iter->get() == sect)
377 // The secton was already in this section list
378 return std::distance (begin, sect_iter);
385 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
387 size_t sect_idx = FindSectionIndex (sect_sp.get());
388 if (sect_idx == UINT32_MAX)
390 sect_idx = AddSection (sect_sp);
396 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
398 iterator sect_iter, end = m_sections.end();
399 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
401 if ((*sect_iter)->GetID() == sect_id)
403 *sect_iter = sect_sp;
408 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
416 SectionList::GetNumSections (uint32_t depth) const
418 size_t count = m_sections.size();
421 const_iterator sect_iter, end = m_sections.end();
422 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
424 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
431 SectionList::GetSectionAtIndex (size_t idx) const
434 if (idx < m_sections.size())
435 sect_sp = m_sections[idx];
440 SectionList::FindSectionByName (const ConstString §ion_dstr) const
443 // Check if we have a valid section string
444 if (section_dstr && !m_sections.empty())
446 const_iterator sect_iter;
447 const_iterator end = m_sections.end();
448 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
450 Section *child_section = sect_iter->get();
453 if (child_section->GetName() == section_dstr)
455 sect_sp = *sect_iter;
459 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
468 SectionList::FindSectionByID (user_id_t sect_id) const
473 const_iterator sect_iter;
474 const_iterator end = m_sections.end();
475 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
477 if ((*sect_iter)->GetID() == sect_id)
479 sect_sp = *sect_iter;
484 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
493 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
496 size_t num_sections = m_sections.size();
497 for (size_t idx = start_idx; idx < num_sections; ++idx)
499 if (m_sections[idx]->GetType() == sect_type)
501 sect_sp = m_sections[idx];
504 else if (check_children)
506 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
515 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
518 const_iterator sect_iter;
519 const_iterator end = m_sections.end();
520 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
522 Section *sect = sect_iter->get();
523 if (sect->ContainsFileAddress (vm_addr))
525 // The file address is in this section. We need to make sure one of our child
526 // sections doesn't contain this address as well as obeying the depth limit
527 // that was passed in.
529 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
531 if (sect_sp.get() == NULL && !sect->IsFake())
532 sect_sp = *sect_iter;
539 SectionList::ContainsSection(user_id_t sect_id) const
541 return FindSectionByID (sect_id).get() != NULL;
545 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
547 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
548 if (show_header && !m_sections.empty())
551 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
553 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
557 const_iterator sect_iter;
558 const_iterator end = m_sections.end();
559 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
561 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
564 if (show_header && !m_sections.empty())
570 SectionList::Slide (addr_t slide_amount, bool slide_children)
573 const_iterator pos, end = m_sections.end();
574 for (pos = m_sections.begin(); pos != end; ++pos)
576 if ((*pos)->Slide(slide_amount, slide_children))