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"
19 using namespace lldb_private;
21 Section::Section (const ModuleSP &module_sp,
24 const ConstString &name,
25 SectionType sect_type,
28 lldb::offset_t file_offset,
29 lldb::offset_t file_size,
32 ModuleChild (module_sp),
35 m_obj_file (obj_file),
39 m_file_addr (file_addr),
40 m_byte_size (byte_size),
41 m_file_offset (file_offset),
42 m_file_size (file_size),
43 m_log2align (log2align),
47 m_thread_specific (false)
49 // 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",
50 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
53 Section::Section (const lldb::SectionSP &parent_section_sp,
54 const ModuleSP &module_sp,
57 const ConstString &name,
58 SectionType sect_type,
61 lldb::offset_t file_offset,
62 lldb::offset_t file_size,
65 ModuleChild (module_sp),
68 m_obj_file (obj_file),
72 m_file_addr (file_addr),
73 m_byte_size (byte_size),
74 m_file_offset (file_offset),
75 m_file_size (file_size),
76 m_log2align (log2align),
80 m_thread_specific (false)
82 // 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",
83 // 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());
84 if (parent_section_sp)
85 m_parent_wp = parent_section_sp;
90 // printf ("Section::~Section(%p)\n", this);
94 Section::GetFileAddress () const
96 SectionSP parent_sp (GetParent ());
99 // This section has a parent which means m_file_addr is an offset into
100 // the parent section, so the file address for this section is the file
101 // address of the parent plus the offset
102 return parent_sp->GetFileAddress() + m_file_addr;
104 // This section has no parent, so m_file_addr is the file base address
109 Section::SetFileAddress (lldb::addr_t file_addr)
111 SectionSP parent_sp (GetParent ());
114 if (m_file_addr >= file_addr)
115 return parent_sp->SetFileAddress (m_file_addr - file_addr);
120 // This section has no parent, so m_file_addr is the file base address
121 m_file_addr = file_addr;
127 Section::GetOffset () const
129 // This section has a parent which means m_file_addr is an offset.
130 SectionSP parent_sp (GetParent ());
134 // This section has no parent, so there is no offset to be had
139 Section::GetLoadBaseAddress (Target *target) const
141 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
142 SectionSP parent_sp (GetParent ());
145 load_base_addr = parent_sp->GetLoadBaseAddress (target);
146 if (load_base_addr != LLDB_INVALID_ADDRESS)
147 load_base_addr += GetOffset();
149 if (load_base_addr == LLDB_INVALID_ADDRESS)
151 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
153 return load_base_addr;
157 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
159 const size_t num_children = m_children.GetSize();
160 if (num_children > 0)
162 for (size_t i=0; i<num_children; i++)
164 Section* child_section = m_children.GetSectionAtIndex (i).get();
166 addr_t child_offset = child_section->GetOffset();
167 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
168 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
171 so_addr.SetOffset(offset);
172 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
174 #ifdef LLDB_CONFIGURATION_DEBUG
175 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
176 assert(GetModule().get());
182 Section::ContainsFileAddress (addr_t vm_addr) const
184 const addr_t file_addr = GetFileAddress();
185 if (file_addr != LLDB_INVALID_ADDRESS)
187 if (file_addr <= vm_addr)
189 const addr_t offset = vm_addr - file_addr;
190 return offset < GetByteSize();
197 Section::Compare (const Section& a, const Section& b)
202 const ModuleSP a_module_sp = a.GetModule();
203 const ModuleSP b_module_sp = b.GetModule();
204 if (a_module_sp == b_module_sp)
206 user_id_t a_sect_uid = a.GetID();
207 user_id_t b_sect_uid = b.GetID();
208 if (a_sect_uid < b_sect_uid)
210 if (a_sect_uid > b_sect_uid)
216 // The modules are different, just compare the module pointers
217 if (a_module_sp.get() < b_module_sp.get())
220 return 1; // We already know the modules aren't equal
226 Section::Dump (Stream *s, Target *target, uint32_t depth) const
228 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
230 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
231 bool resolved = true;
232 addr_t addr = LLDB_INVALID_ADDRESS;
234 if (GetByteSize() == 0)
235 s->Printf("%39s", "");
239 addr = GetLoadBaseAddress (target);
241 if (addr == LLDB_INVALID_ADDRESS)
245 addr = GetFileAddress();
248 VMRange range(addr, addr + m_byte_size);
252 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
259 m_children.Dump(s, target, false, depth - 1);
263 Section::DumpName (Stream *s) const
265 SectionSP parent_sp (GetParent ());
268 parent_sp->DumpName (s);
273 // The top most section prints the module basename
274 const char * name = NULL;
275 ModuleSP module_sp (GetModule());
276 const FileSpec &file_spec = m_obj_file->GetFileSpec();
279 name = file_spec.GetFilename().AsCString();
280 if ((!name || !name[0]) && module_sp)
281 name = module_sp->GetFileSpec().GetFilename().AsCString();
283 s->Printf("%s.", name);
289 Section::IsDescendant (const Section *section)
293 SectionSP parent_sp (GetParent ());
295 return parent_sp->IsDescendant (section);
300 Section::Slide (addr_t slide_amount, bool slide_children)
302 if (m_file_addr != LLDB_INVALID_ADDRESS)
304 if (slide_amount == 0)
307 m_file_addr += slide_amount;
310 m_children.Slide (slide_amount, slide_children);
317 #pragma mark SectionList
319 SectionList::SectionList () :
325 SectionList::~SectionList ()
330 SectionList::operator = (const SectionList& rhs)
333 m_sections = rhs.m_sections;
338 SectionList::AddSection (const lldb::SectionSP& section_sp)
342 size_t section_index = m_sections.size();
343 m_sections.push_back(section_sp);
344 return section_index;
347 return std::numeric_limits<size_t>::max ();
350 // Warning, this can be slow as it's removing items from a std::vector.
352 SectionList::DeleteSection (size_t idx)
354 if (idx < m_sections.size())
356 m_sections.erase (m_sections.begin() + idx);
363 SectionList::FindSectionIndex (const Section* sect)
366 iterator begin = m_sections.begin();
367 iterator end = m_sections.end();
368 for (sect_iter = begin; sect_iter != end; ++sect_iter)
370 if (sect_iter->get() == sect)
372 // The secton was already in this section list
373 return std::distance (begin, sect_iter);
380 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
382 size_t sect_idx = FindSectionIndex (sect_sp.get());
383 if (sect_idx == UINT32_MAX)
385 sect_idx = AddSection (sect_sp);
391 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
393 iterator sect_iter, end = m_sections.end();
394 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
396 if ((*sect_iter)->GetID() == sect_id)
398 *sect_iter = sect_sp;
403 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
411 SectionList::GetNumSections (uint32_t depth) const
413 size_t count = m_sections.size();
416 const_iterator sect_iter, end = m_sections.end();
417 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
419 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
426 SectionList::GetSectionAtIndex (size_t idx) const
429 if (idx < m_sections.size())
430 sect_sp = m_sections[idx];
435 SectionList::FindSectionByName (const ConstString §ion_dstr) const
438 // Check if we have a valid section string
439 if (section_dstr && !m_sections.empty())
441 const_iterator sect_iter;
442 const_iterator end = m_sections.end();
443 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
445 Section *child_section = sect_iter->get();
448 if (child_section->GetName() == section_dstr)
450 sect_sp = *sect_iter;
454 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
463 SectionList::FindSectionByID (user_id_t sect_id) const
468 const_iterator sect_iter;
469 const_iterator end = m_sections.end();
470 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
472 if ((*sect_iter)->GetID() == sect_id)
474 sect_sp = *sect_iter;
479 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
488 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
491 size_t num_sections = m_sections.size();
492 for (size_t idx = start_idx; idx < num_sections; ++idx)
494 if (m_sections[idx]->GetType() == sect_type)
496 sect_sp = m_sections[idx];
499 else if (check_children)
501 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
510 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
513 const_iterator sect_iter;
514 const_iterator end = m_sections.end();
515 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
517 Section *sect = sect_iter->get();
518 if (sect->ContainsFileAddress (vm_addr))
520 // The file address is in this section. We need to make sure one of our child
521 // sections doesn't contain this address as well as obeying the depth limit
522 // that was passed in.
524 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
526 if (sect_sp.get() == NULL && !sect->IsFake())
527 sect_sp = *sect_iter;
534 SectionList::ContainsSection(user_id_t sect_id) const
536 return FindSectionByID (sect_id).get() != NULL;
540 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
542 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
543 if (show_header && !m_sections.empty())
546 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
548 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
552 const_iterator sect_iter;
553 const_iterator end = m_sections.end();
554 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
556 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
559 if (show_header && !m_sections.empty())
565 SectionList::Slide (addr_t slide_amount, bool slide_children)
568 const_iterator pos, end = m_sections.end();
569 for (pos = m_sections.begin(); pos != end; ++pos)
571 if ((*pos)->Slide(slide_amount, slide_children))