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 uint32_t target_byte_size/*=1*/) :
33 ModuleChild (module_sp),
36 m_obj_file (obj_file),
40 m_file_addr (file_addr),
41 m_byte_size (byte_size),
42 m_file_offset (file_offset),
43 m_file_size (file_size),
44 m_log2align (log2align),
48 m_thread_specific (false),
49 m_target_byte_size(target_byte_size)
51 // 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",
52 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
55 Section::Section (const lldb::SectionSP &parent_section_sp,
56 const ModuleSP &module_sp,
59 const ConstString &name,
60 SectionType sect_type,
63 lldb::offset_t file_offset,
64 lldb::offset_t file_size,
67 uint32_t target_byte_size/*=1*/) :
68 ModuleChild (module_sp),
71 m_obj_file (obj_file),
75 m_file_addr (file_addr),
76 m_byte_size (byte_size),
77 m_file_offset (file_offset),
78 m_file_size (file_size),
79 m_log2align (log2align),
83 m_thread_specific (false),
84 m_target_byte_size(target_byte_size)
86 // 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",
87 // 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());
88 if (parent_section_sp)
89 m_parent_wp = parent_section_sp;
94 // printf ("Section::~Section(%p)\n", this);
98 Section::GetFileAddress () const
100 SectionSP parent_sp (GetParent ());
103 // This section has a parent which means m_file_addr is an offset into
104 // the parent section, so the file address for this section is the file
105 // address of the parent plus the offset
106 return parent_sp->GetFileAddress() + m_file_addr;
108 // This section has no parent, so m_file_addr is the file base address
113 Section::SetFileAddress (lldb::addr_t file_addr)
115 SectionSP parent_sp (GetParent ());
118 if (m_file_addr >= file_addr)
119 return parent_sp->SetFileAddress (m_file_addr - file_addr);
124 // This section has no parent, so m_file_addr is the file base address
125 m_file_addr = file_addr;
131 Section::GetOffset () const
133 // This section has a parent which means m_file_addr is an offset.
134 SectionSP parent_sp (GetParent ());
138 // This section has no parent, so there is no offset to be had
143 Section::GetLoadBaseAddress (Target *target) const
145 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
146 SectionSP parent_sp (GetParent ());
149 load_base_addr = parent_sp->GetLoadBaseAddress (target);
150 if (load_base_addr != LLDB_INVALID_ADDRESS)
151 load_base_addr += GetOffset();
153 if (load_base_addr == LLDB_INVALID_ADDRESS)
155 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
157 return load_base_addr;
161 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
163 const size_t num_children = m_children.GetSize();
164 if (num_children > 0)
166 for (size_t i=0; i<num_children; i++)
168 Section* child_section = m_children.GetSectionAtIndex (i).get();
170 addr_t child_offset = child_section->GetOffset();
171 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
172 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
175 so_addr.SetOffset(offset);
176 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
178 #ifdef LLDB_CONFIGURATION_DEBUG
179 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
180 assert(GetModule().get());
186 Section::ContainsFileAddress (addr_t vm_addr) const
188 const addr_t file_addr = GetFileAddress();
189 if (file_addr != LLDB_INVALID_ADDRESS)
191 if (file_addr <= vm_addr)
193 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
194 return offset < GetByteSize();
201 Section::Compare (const Section& a, const Section& b)
206 const ModuleSP a_module_sp = a.GetModule();
207 const ModuleSP b_module_sp = b.GetModule();
208 if (a_module_sp == b_module_sp)
210 user_id_t a_sect_uid = a.GetID();
211 user_id_t b_sect_uid = b.GetID();
212 if (a_sect_uid < b_sect_uid)
214 if (a_sect_uid > b_sect_uid)
220 // The modules are different, just compare the module pointers
221 if (a_module_sp.get() < b_module_sp.get())
224 return 1; // We already know the modules aren't equal
230 Section::Dump (Stream *s, Target *target, uint32_t depth) const
232 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
234 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
235 bool resolved = true;
236 addr_t addr = LLDB_INVALID_ADDRESS;
238 if (GetByteSize() == 0)
239 s->Printf("%39s", "");
243 addr = GetLoadBaseAddress (target);
245 if (addr == LLDB_INVALID_ADDRESS)
249 addr = GetFileAddress();
252 VMRange range(addr, addr + m_byte_size);
256 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
263 m_children.Dump(s, target, false, depth - 1);
267 Section::DumpName (Stream *s) const
269 SectionSP parent_sp (GetParent ());
272 parent_sp->DumpName (s);
277 // The top most section prints the module basename
278 const char * name = NULL;
279 ModuleSP module_sp (GetModule());
280 const FileSpec &file_spec = m_obj_file->GetFileSpec();
283 name = file_spec.GetFilename().AsCString();
284 if ((!name || !name[0]) && module_sp)
285 name = module_sp->GetFileSpec().GetFilename().AsCString();
287 s->Printf("%s.", name);
293 Section::IsDescendant (const Section *section)
297 SectionSP parent_sp (GetParent ());
299 return parent_sp->IsDescendant (section);
304 Section::Slide (addr_t slide_amount, bool slide_children)
306 if (m_file_addr != LLDB_INVALID_ADDRESS)
308 if (slide_amount == 0)
311 m_file_addr += slide_amount;
314 m_children.Slide (slide_amount, slide_children);
321 #pragma mark SectionList
323 SectionList::SectionList () :
329 SectionList::~SectionList ()
334 SectionList::operator = (const SectionList& rhs)
337 m_sections = rhs.m_sections;
342 SectionList::AddSection (const lldb::SectionSP& section_sp)
346 size_t section_index = m_sections.size();
347 m_sections.push_back(section_sp);
348 return section_index;
351 return std::numeric_limits<size_t>::max ();
354 // Warning, this can be slow as it's removing items from a std::vector.
356 SectionList::DeleteSection (size_t idx)
358 if (idx < m_sections.size())
360 m_sections.erase (m_sections.begin() + idx);
367 SectionList::FindSectionIndex (const Section* sect)
370 iterator begin = m_sections.begin();
371 iterator end = m_sections.end();
372 for (sect_iter = begin; sect_iter != end; ++sect_iter)
374 if (sect_iter->get() == sect)
376 // The secton was already in this section list
377 return std::distance (begin, sect_iter);
384 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
386 size_t sect_idx = FindSectionIndex (sect_sp.get());
387 if (sect_idx == UINT32_MAX)
389 sect_idx = AddSection (sect_sp);
395 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
397 iterator sect_iter, end = m_sections.end();
398 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
400 if ((*sect_iter)->GetID() == sect_id)
402 *sect_iter = sect_sp;
407 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
415 SectionList::GetNumSections (uint32_t depth) const
417 size_t count = m_sections.size();
420 const_iterator sect_iter, end = m_sections.end();
421 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
423 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
430 SectionList::GetSectionAtIndex (size_t idx) const
433 if (idx < m_sections.size())
434 sect_sp = m_sections[idx];
439 SectionList::FindSectionByName (const ConstString §ion_dstr) const
442 // Check if we have a valid section string
443 if (section_dstr && !m_sections.empty())
445 const_iterator sect_iter;
446 const_iterator end = m_sections.end();
447 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
449 Section *child_section = sect_iter->get();
452 if (child_section->GetName() == section_dstr)
454 sect_sp = *sect_iter;
458 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
467 SectionList::FindSectionByID (user_id_t sect_id) const
472 const_iterator sect_iter;
473 const_iterator end = m_sections.end();
474 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
476 if ((*sect_iter)->GetID() == sect_id)
478 sect_sp = *sect_iter;
483 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
492 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
495 size_t num_sections = m_sections.size();
496 for (size_t idx = start_idx; idx < num_sections; ++idx)
498 if (m_sections[idx]->GetType() == sect_type)
500 sect_sp = m_sections[idx];
503 else if (check_children)
505 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
514 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
517 const_iterator sect_iter;
518 const_iterator end = m_sections.end();
519 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
521 Section *sect = sect_iter->get();
522 if (sect->ContainsFileAddress (vm_addr))
524 // The file address is in this section. We need to make sure one of our child
525 // sections doesn't contain this address as well as obeying the depth limit
526 // that was passed in.
528 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
530 if (sect_sp.get() == NULL && !sect->IsFake())
531 sect_sp = *sect_iter;
538 SectionList::ContainsSection(user_id_t sect_id) const
540 return FindSectionByID (sect_id).get() != NULL;
544 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
546 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
547 if (show_header && !m_sections.empty())
550 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
552 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
556 const_iterator sect_iter;
557 const_iterator end = m_sections.end();
558 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
560 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
563 if (show_header && !m_sections.empty())
569 SectionList::Slide (addr_t slide_amount, bool slide_children)
572 const_iterator pos, end = m_sections.end();
573 for (pos = m_sections.begin(); pos != end; ++pos)
575 if ((*pos)->Slide(slide_amount, slide_children))