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"
17 using namespace lldb_private;
19 Section::Section (const ModuleSP &module_sp,
22 const ConstString &name,
23 SectionType sect_type,
26 lldb::offset_t file_offset,
27 lldb::offset_t file_size,
29 ModuleChild (module_sp),
32 m_obj_file (obj_file),
36 m_file_addr (file_addr),
37 m_byte_size (byte_size),
38 m_file_offset (file_offset),
39 m_file_size (file_size),
43 m_thread_specific (false)
45 // 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",
46 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
49 Section::Section (const lldb::SectionSP &parent_section_sp,
50 const ModuleSP &module_sp,
53 const ConstString &name,
54 SectionType sect_type,
57 lldb::offset_t file_offset,
58 lldb::offset_t file_size,
60 ModuleChild (module_sp),
63 m_obj_file (obj_file),
67 m_file_addr (file_addr),
68 m_byte_size (byte_size),
69 m_file_offset (file_offset),
70 m_file_size (file_size),
74 m_thread_specific (false)
76 // 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",
77 // 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());
78 if (parent_section_sp)
79 m_parent_wp = parent_section_sp;
84 // printf ("Section::~Section(%p)\n", this);
88 Section::GetFileAddress () const
90 SectionSP parent_sp (GetParent ());
93 // This section has a parent which means m_file_addr is an offset into
94 // the parent section, so the file address for this section is the file
95 // address of the parent plus the offset
96 return parent_sp->GetFileAddress() + m_file_addr;
98 // This section has no parent, so m_file_addr is the file base address
103 Section::SetFileAddress (lldb::addr_t file_addr)
105 SectionSP parent_sp (GetParent ());
108 if (m_file_addr >= file_addr)
109 return parent_sp->SetFileAddress (m_file_addr - file_addr);
114 // This section has no parent, so m_file_addr is the file base address
115 m_file_addr = file_addr;
121 Section::GetOffset () const
123 // This section has a parent which means m_file_addr is an offset.
124 SectionSP parent_sp (GetParent ());
128 // This section has no parent, so there is no offset to be had
133 Section::GetLoadBaseAddress (Target *target) const
135 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
136 SectionSP parent_sp (GetParent ());
139 load_base_addr = parent_sp->GetLoadBaseAddress (target);
140 if (load_base_addr != LLDB_INVALID_ADDRESS)
141 load_base_addr += GetOffset();
145 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
147 return load_base_addr;
151 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
153 const size_t num_children = m_children.GetSize();
154 if (num_children > 0)
156 for (size_t i=0; i<num_children; i++)
158 Section* child_section = m_children.GetSectionAtIndex (i).get();
160 addr_t child_offset = child_section->GetOffset();
161 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
162 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
165 so_addr.SetOffset(offset);
166 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
168 #ifdef LLDB_CONFIGURATION_DEBUG
169 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
170 assert(GetModule().get());
176 Section::ContainsFileAddress (addr_t vm_addr) const
178 const addr_t file_addr = GetFileAddress();
179 if (file_addr != LLDB_INVALID_ADDRESS)
181 if (file_addr <= vm_addr)
183 const addr_t offset = vm_addr - file_addr;
184 return offset < GetByteSize();
191 Section::Compare (const Section& a, const Section& b)
196 const ModuleSP a_module_sp = a.GetModule();
197 const ModuleSP b_module_sp = b.GetModule();
198 if (a_module_sp == b_module_sp)
200 user_id_t a_sect_uid = a.GetID();
201 user_id_t b_sect_uid = b.GetID();
202 if (a_sect_uid < b_sect_uid)
204 if (a_sect_uid > b_sect_uid)
210 // The modules are different, just compare the module pointers
211 if (a_module_sp.get() < b_module_sp.get())
214 return 1; // We already know the modules aren't equal
220 Section::Dump (Stream *s, Target *target, uint32_t depth) const
222 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
224 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
225 bool resolved = true;
226 addr_t addr = LLDB_INVALID_ADDRESS;
228 if (GetByteSize() == 0)
229 s->Printf("%39s", "");
233 addr = GetLoadBaseAddress (target);
235 if (addr == LLDB_INVALID_ADDRESS)
239 addr = GetFileAddress();
242 VMRange range(addr, addr + m_byte_size);
246 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
253 m_children.Dump(s, target, false, depth - 1);
257 Section::DumpName (Stream *s) const
259 SectionSP parent_sp (GetParent ());
262 parent_sp->DumpName (s);
267 // The top most section prints the module basename
268 const char * name = NULL;
269 ModuleSP module_sp (GetModule());
270 const FileSpec &file_spec = m_obj_file->GetFileSpec();
273 name = file_spec.GetFilename().AsCString();
274 if ((!name || !name[0]) && module_sp)
275 name = module_sp->GetFileSpec().GetFilename().AsCString();
277 s->Printf("%s.", name);
283 Section::IsDescendant (const Section *section)
287 SectionSP parent_sp (GetParent ());
289 return parent_sp->IsDescendant (section);
294 Section::Slide (addr_t slide_amount, bool slide_children)
296 if (m_file_addr != LLDB_INVALID_ADDRESS)
298 if (slide_amount == 0)
301 m_file_addr += slide_amount;
304 m_children.Slide (slide_amount, slide_children);
311 #pragma mark SectionList
313 SectionList::SectionList () :
319 SectionList::~SectionList ()
324 SectionList::operator = (const SectionList& rhs)
327 m_sections = rhs.m_sections;
332 SectionList::AddSection (const lldb::SectionSP& section_sp)
334 assert (section_sp.get());
335 size_t section_index = m_sections.size();
336 m_sections.push_back(section_sp);
337 return section_index;
340 // Warning, this can be slow as it's removing items from a std::vector.
342 SectionList::DeleteSection (size_t idx)
344 if (idx < m_sections.size())
346 m_sections.erase (m_sections.begin() + idx);
353 SectionList::FindSectionIndex (const Section* sect)
356 iterator begin = m_sections.begin();
357 iterator end = m_sections.end();
358 for (sect_iter = begin; sect_iter != end; ++sect_iter)
360 if (sect_iter->get() == sect)
362 // The secton was already in this section list
363 return std::distance (begin, sect_iter);
370 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
372 size_t sect_idx = FindSectionIndex (sect_sp.get());
373 if (sect_idx == UINT32_MAX)
375 sect_idx = AddSection (sect_sp);
381 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
383 iterator sect_iter, end = m_sections.end();
384 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
386 if ((*sect_iter)->GetID() == sect_id)
388 *sect_iter = sect_sp;
393 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
401 SectionList::GetNumSections (uint32_t depth) const
403 size_t count = m_sections.size();
406 const_iterator sect_iter, end = m_sections.end();
407 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
409 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
416 SectionList::GetSectionAtIndex (size_t idx) const
419 if (idx < m_sections.size())
420 sect_sp = m_sections[idx];
425 SectionList::FindSectionByName (const ConstString §ion_dstr) const
428 // Check if we have a valid section string
429 if (section_dstr && !m_sections.empty())
431 const_iterator sect_iter;
432 const_iterator end = m_sections.end();
433 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
435 Section *child_section = sect_iter->get();
436 assert (child_section);
437 if (child_section->GetName() == section_dstr)
439 sect_sp = *sect_iter;
443 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
451 SectionList::FindSectionByID (user_id_t sect_id) const
456 const_iterator sect_iter;
457 const_iterator end = m_sections.end();
458 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
460 if ((*sect_iter)->GetID() == sect_id)
462 sect_sp = *sect_iter;
467 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
476 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
479 size_t num_sections = m_sections.size();
480 for (size_t idx = start_idx; idx < num_sections; ++idx)
482 if (m_sections[idx]->GetType() == sect_type)
484 sect_sp = m_sections[idx];
487 else if (check_children)
489 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
498 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
501 const_iterator sect_iter;
502 const_iterator end = m_sections.end();
503 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
505 Section *sect = sect_iter->get();
506 if (sect->ContainsFileAddress (vm_addr))
508 // The file address is in this section. We need to make sure one of our child
509 // sections doesn't contain this address as well as obeying the depth limit
510 // that was passed in.
512 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
514 if (sect_sp.get() == NULL && !sect->IsFake())
515 sect_sp = *sect_iter;
522 SectionList::ContainsSection(user_id_t sect_id) const
524 return FindSectionByID (sect_id).get() != NULL;
528 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
530 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
531 if (show_header && !m_sections.empty())
534 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
536 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
540 const_iterator sect_iter;
541 const_iterator end = m_sections.end();
542 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
544 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
547 if (show_header && !m_sections.empty())
553 SectionList::Slide (addr_t slide_amount, bool slide_children)
556 const_iterator pos, end = m_sections.end();
557 for (pos = m_sections.begin(); pos != end; ++pos)
559 if ((*pos)->Slide(slide_amount, slide_children))