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/Target.h"
16 using namespace lldb_private;
18 Section::Section (const ModuleSP &module_sp,
21 const ConstString &name,
22 SectionType sect_type,
25 lldb::offset_t file_offset,
26 lldb::offset_t file_size,
28 ModuleChild (module_sp),
31 m_obj_file (obj_file),
35 m_file_addr (file_addr),
36 m_byte_size (byte_size),
37 m_file_offset (file_offset),
38 m_file_size (file_size),
42 m_thread_specific (false)
44 // 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",
45 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
48 Section::Section (const lldb::SectionSP &parent_section_sp,
49 const ModuleSP &module_sp,
52 const ConstString &name,
53 SectionType sect_type,
56 lldb::offset_t file_offset,
57 lldb::offset_t file_size,
59 ModuleChild (module_sp),
62 m_obj_file (obj_file),
66 m_file_addr (file_addr),
67 m_byte_size (byte_size),
68 m_file_offset (file_offset),
69 m_file_size (file_size),
73 m_thread_specific (false)
75 // 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",
76 // 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());
77 if (parent_section_sp)
78 m_parent_wp = parent_section_sp;
83 // printf ("Section::~Section(%p)\n", this);
87 Section::GetFileAddress () const
89 SectionSP parent_sp (GetParent ());
92 // This section has a parent which means m_file_addr is an offset into
93 // the parent section, so the file address for this section is the file
94 // address of the parent plus the offset
95 return parent_sp->GetFileAddress() + m_file_addr;
97 // This section has no parent, so m_file_addr is the file base address
102 Section::SetFileAddress (lldb::addr_t file_addr)
104 SectionSP parent_sp (GetParent ());
107 if (m_file_addr >= file_addr)
108 return parent_sp->SetFileAddress (m_file_addr - file_addr);
113 // This section has no parent, so m_file_addr is the file base address
114 m_file_addr = file_addr;
120 Section::GetOffset () const
122 // This section has a parent which means m_file_addr is an offset.
123 SectionSP parent_sp (GetParent ());
127 // This section has no parent, so there is no offset to be had
132 Section::GetLoadBaseAddress (Target *target) const
134 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
135 SectionSP parent_sp (GetParent ());
138 load_base_addr = parent_sp->GetLoadBaseAddress (target);
139 if (load_base_addr != LLDB_INVALID_ADDRESS)
140 load_base_addr += GetOffset();
144 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
146 return load_base_addr;
150 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
152 const size_t num_children = m_children.GetSize();
153 if (num_children > 0)
155 for (size_t i=0; i<num_children; i++)
157 Section* child_section = m_children.GetSectionAtIndex (i).get();
159 addr_t child_offset = child_section->GetOffset();
160 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
161 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
164 so_addr.SetOffset(offset);
165 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
167 #ifdef LLDB_CONFIGURATION_DEBUG
168 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
169 assert(GetModule().get());
175 Section::ContainsFileAddress (addr_t vm_addr) const
177 const addr_t file_addr = GetFileAddress();
178 if (file_addr != LLDB_INVALID_ADDRESS)
180 if (file_addr <= vm_addr)
182 const addr_t offset = vm_addr - file_addr;
183 return offset < GetByteSize();
190 Section::Compare (const Section& a, const Section& b)
195 const ModuleSP a_module_sp = a.GetModule();
196 const ModuleSP b_module_sp = b.GetModule();
197 if (a_module_sp == b_module_sp)
199 user_id_t a_sect_uid = a.GetID();
200 user_id_t b_sect_uid = b.GetID();
201 if (a_sect_uid < b_sect_uid)
203 if (a_sect_uid > b_sect_uid)
209 // The modules are different, just compare the module pointers
210 if (a_module_sp.get() < b_module_sp.get())
213 return 1; // We already know the modules aren't equal
219 Section::Dump (Stream *s, Target *target, uint32_t depth) const
221 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
223 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
224 bool resolved = true;
225 addr_t addr = LLDB_INVALID_ADDRESS;
227 if (GetByteSize() == 0)
228 s->Printf("%39s", "");
232 addr = GetLoadBaseAddress (target);
234 if (addr == LLDB_INVALID_ADDRESS)
238 addr = GetFileAddress();
241 VMRange range(addr, addr + m_byte_size);
245 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
252 m_children.Dump(s, target, false, depth - 1);
256 Section::DumpName (Stream *s) const
258 SectionSP parent_sp (GetParent ());
261 parent_sp->DumpName (s);
266 // The top most section prints the module basename
267 const char * name = NULL;
268 ModuleSP module_sp (GetModule());
269 const FileSpec &file_spec = m_obj_file->GetFileSpec();
272 name = file_spec.GetFilename().AsCString();
273 if ((!name || !name[0]) && module_sp)
274 name = module_sp->GetFileSpec().GetFilename().AsCString();
276 s->Printf("%s.", name);
282 Section::IsDescendant (const Section *section)
286 SectionSP parent_sp (GetParent ());
288 return parent_sp->IsDescendant (section);
293 Section::Slide (addr_t slide_amount, bool slide_children)
295 if (m_file_addr != LLDB_INVALID_ADDRESS)
297 if (slide_amount == 0)
300 m_file_addr += slide_amount;
303 m_children.Slide (slide_amount, slide_children);
310 #pragma mark SectionList
312 SectionList::SectionList () :
318 SectionList::~SectionList ()
323 SectionList::operator = (const SectionList& rhs)
326 m_sections = rhs.m_sections;
331 SectionList::AddSection (const lldb::SectionSP& section_sp)
333 assert (section_sp.get());
334 size_t section_index = m_sections.size();
335 m_sections.push_back(section_sp);
336 return section_index;
339 // Warning, this can be slow as it's removing items from a std::vector.
341 SectionList::DeleteSection (size_t idx)
343 if (idx < m_sections.size())
345 m_sections.erase (m_sections.begin() + idx);
352 SectionList::FindSectionIndex (const Section* sect)
355 iterator begin = m_sections.begin();
356 iterator end = m_sections.end();
357 for (sect_iter = begin; sect_iter != end; ++sect_iter)
359 if (sect_iter->get() == sect)
361 // The secton was already in this section list
362 return std::distance (begin, sect_iter);
369 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
371 size_t sect_idx = FindSectionIndex (sect_sp.get());
372 if (sect_idx == UINT32_MAX)
374 sect_idx = AddSection (sect_sp);
380 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
382 iterator sect_iter, end = m_sections.end();
383 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
385 if ((*sect_iter)->GetID() == sect_id)
387 *sect_iter = sect_sp;
392 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
400 SectionList::GetNumSections (uint32_t depth) const
402 size_t count = m_sections.size();
405 const_iterator sect_iter, end = m_sections.end();
406 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
408 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
415 SectionList::GetSectionAtIndex (size_t idx) const
418 if (idx < m_sections.size())
419 sect_sp = m_sections[idx];
424 SectionList::FindSectionByName (const ConstString §ion_dstr) const
427 // Check if we have a valid section string
428 if (section_dstr && !m_sections.empty())
430 const_iterator sect_iter;
431 const_iterator end = m_sections.end();
432 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
434 Section *child_section = sect_iter->get();
435 assert (child_section);
436 if (child_section->GetName() == section_dstr)
438 sect_sp = *sect_iter;
442 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
450 SectionList::FindSectionByID (user_id_t sect_id) const
455 const_iterator sect_iter;
456 const_iterator end = m_sections.end();
457 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
459 if ((*sect_iter)->GetID() == sect_id)
461 sect_sp = *sect_iter;
466 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
475 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
478 size_t num_sections = m_sections.size();
479 for (size_t idx = start_idx; idx < num_sections; ++idx)
481 if (m_sections[idx]->GetType() == sect_type)
483 sect_sp = m_sections[idx];
486 else if (check_children)
488 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
497 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
500 const_iterator sect_iter;
501 const_iterator end = m_sections.end();
502 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
504 Section *sect = sect_iter->get();
505 if (sect->ContainsFileAddress (vm_addr))
507 // The file address is in this section. We need to make sure one of our child
508 // sections doesn't contain this address as well as obeying the depth limit
509 // that was passed in.
511 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
513 if (sect_sp.get() == NULL && !sect->IsFake())
514 sect_sp = *sect_iter;
521 SectionList::ContainsSection(user_id_t sect_id) const
523 return FindSectionByID (sect_id).get() != NULL;
527 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
529 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
530 if (show_header && !m_sections.empty())
533 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
535 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
539 const_iterator sect_iter;
540 const_iterator end = m_sections.end();
541 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
543 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
546 if (show_header && !m_sections.empty())
552 SectionList::Slide (addr_t slide_amount, bool slide_children)
555 const_iterator pos, end = m_sections.end();
556 for (pos = m_sections.begin(); pos != end; ++pos)
558 if ((*pos)->Slide(slide_amount, slide_children))