blob: 9a3f220beb445bd8482f51806f866017009dd3de [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Section.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/Section.h"
11#include "lldb/Core/Module.h"
12#include "lldb/Symbol/ObjectFile.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000013#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000014#include "lldb/Target/Target.h"
Zachary Turner50232572015-03-18 21:31:45 +000015#include "lldb/Utility/ConvertEnum.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
Todd Fialafd8ae3a2014-05-14 16:15:43 +000017#include <limits>
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019using namespace lldb;
20using namespace lldb_private;
21
Greg Claytone72dfb32012-02-24 01:59:29 +000022Section::Section (const ModuleSP &module_sp,
Michael Sartaina7499c92013-07-01 19:45:50 +000023 ObjectFile *obj_file,
Greg Claytone72dfb32012-02-24 01:59:29 +000024 user_id_t sect_id,
25 const ConstString &name,
26 SectionType sect_type,
27 addr_t file_addr,
28 addr_t byte_size,
Greg Clayton9422dd62013-03-04 21:46:16 +000029 lldb::offset_t file_offset,
30 lldb::offset_t file_size,
Greg Clayton48672af2014-06-24 22:22:43 +000031 uint32_t log2align,
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000032 uint32_t flags,
33 uint32_t target_byte_size/*=1*/) :
Greg Claytone72dfb32012-02-24 01:59:29 +000034 ModuleChild (module_sp),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 UserID (sect_id),
36 Flags (flags),
Michael Sartaina7499c92013-07-01 19:45:50 +000037 m_obj_file (obj_file),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000038 m_type (sect_type),
Greg Claytone72dfb32012-02-24 01:59:29 +000039 m_parent_wp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_name (name),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 m_file_addr (file_addr),
42 m_byte_size (byte_size),
43 m_file_offset (file_offset),
44 m_file_size (file_size),
Greg Clayton48672af2014-06-24 22:22:43 +000045 m_log2align (log2align),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_children (),
47 m_fake (false),
Greg Clayton741f3f92012-03-27 21:10:07 +000048 m_encrypted (false),
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000049 m_thread_specific (false),
50 m_target_byte_size(target_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051{
Daniel Malead01b2952012-11-29 21:49:15 +000052// 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",
Greg Claytone72dfb32012-02-24 01:59:29 +000053// this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
54}
55
56Section::Section (const lldb::SectionSP &parent_section_sp,
57 const ModuleSP &module_sp,
Michael Sartaina7499c92013-07-01 19:45:50 +000058 ObjectFile *obj_file,
Greg Claytone72dfb32012-02-24 01:59:29 +000059 user_id_t sect_id,
60 const ConstString &name,
61 SectionType sect_type,
62 addr_t file_addr,
63 addr_t byte_size,
Greg Clayton9422dd62013-03-04 21:46:16 +000064 lldb::offset_t file_offset,
65 lldb::offset_t file_size,
Greg Clayton48672af2014-06-24 22:22:43 +000066 uint32_t log2align,
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000067 uint32_t flags,
68 uint32_t target_byte_size/*=1*/) :
Greg Claytone72dfb32012-02-24 01:59:29 +000069 ModuleChild (module_sp),
70 UserID (sect_id),
71 Flags (flags),
Michael Sartaina7499c92013-07-01 19:45:50 +000072 m_obj_file (obj_file),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000073 m_type (sect_type),
Greg Claytone72dfb32012-02-24 01:59:29 +000074 m_parent_wp (),
75 m_name (name),
Greg Claytone72dfb32012-02-24 01:59:29 +000076 m_file_addr (file_addr),
77 m_byte_size (byte_size),
78 m_file_offset (file_offset),
79 m_file_size (file_size),
Greg Clayton48672af2014-06-24 22:22:43 +000080 m_log2align (log2align),
Greg Claytone72dfb32012-02-24 01:59:29 +000081 m_children (),
82 m_fake (false),
Greg Clayton741f3f92012-03-27 21:10:07 +000083 m_encrypted (false),
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000084 m_thread_specific (false),
85 m_target_byte_size(target_byte_size)
Greg Claytone72dfb32012-02-24 01:59:29 +000086{
Daniel Malead01b2952012-11-29 21:49:15 +000087// 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",
Greg Claytone72dfb32012-02-24 01:59:29 +000088// 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091}
92
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093Section::~Section()
94{
Greg Claytone72dfb32012-02-24 01:59:29 +000095// printf ("Section::~Section(%p)\n", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096}
97
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098addr_t
99Section::GetFileAddress () const
100{
Greg Claytone72dfb32012-02-24 01:59:29 +0000101 SectionSP parent_sp (GetParent ());
102 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 {
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
Greg Claytone72dfb32012-02-24 01:59:29 +0000107 return parent_sp->GetFileAddress() + m_file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 }
109 // This section has no parent, so m_file_addr is the file base address
110 return m_file_addr;
111}
112
Jason Molenda20eb31b2013-08-16 03:20:42 +0000113bool
114Section::SetFileAddress (lldb::addr_t file_addr)
115{
116 SectionSP parent_sp (GetParent ());
117 if (parent_sp)
118 {
119 if (m_file_addr >= file_addr)
120 return parent_sp->SetFileAddress (m_file_addr - file_addr);
121 return false;
122 }
123 else
124 {
125 // This section has no parent, so m_file_addr is the file base address
126 m_file_addr = file_addr;
127 return true;
128 }
129}
130
Greg Claytone72dfb32012-02-24 01:59:29 +0000131lldb::addr_t
132Section::GetOffset () const
133{
134 // This section has a parent which means m_file_addr is an offset.
135 SectionSP parent_sp (GetParent ());
136 if (parent_sp)
137 return m_file_addr;
138
139 // This section has no parent, so there is no offset to be had
140 return 0;
141}
142
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000144Section::GetLoadBaseAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145{
146 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
Greg Clayton9422dd62013-03-04 21:46:16 +0000147 SectionSP parent_sp (GetParent ());
148 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000150 load_base_addr = parent_sp->GetLoadBaseAddress (target);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000151 if (load_base_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +0000152 load_base_addr += GetOffset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 }
Greg Clayton48672af2014-06-24 22:22:43 +0000154 if (load_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000156 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 return load_base_addr;
159}
160
161bool
162Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
163{
Greg Claytonc7bece562013-01-25 18:06:21 +0000164 const size_t num_children = m_children.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 if (num_children > 0)
166 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000167 for (size_t i=0; i<num_children; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 {
169 Section* child_section = m_children.GetSectionAtIndex (i).get();
170
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);
174 }
175 }
Greg Clayton9422dd62013-03-04 21:46:16 +0000176 so_addr.SetOffset(offset);
177 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
178
Sean Callanan27e02d02012-07-12 20:44:21 +0000179#ifdef LLDB_CONFIGURATION_DEBUG
Greg Clayton9422dd62013-03-04 21:46:16 +0000180 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
181 assert(GetModule().get());
Sean Callanan27e02d02012-07-12 20:44:21 +0000182#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 return true;
184}
185
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186bool
187Section::ContainsFileAddress (addr_t vm_addr) const
188{
189 const addr_t file_addr = GetFileAddress();
190 if (file_addr != LLDB_INVALID_ADDRESS)
191 {
192 if (file_addr <= vm_addr)
193 {
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000194 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 return offset < GetByteSize();
196 }
197 }
198 return false;
199}
200
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201int
202Section::Compare (const Section& a, const Section& b)
203{
204 if (&a == &b)
205 return 0;
206
Greg Claytone72dfb32012-02-24 01:59:29 +0000207 const ModuleSP a_module_sp = a.GetModule();
208 const ModuleSP b_module_sp = b.GetModule();
209 if (a_module_sp == b_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 {
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)
214 return -1;
215 if (a_sect_uid > b_sect_uid)
216 return 1;
217 return 0;
218 }
219 else
220 {
221 // The modules are different, just compare the module pointers
Greg Claytone72dfb32012-02-24 01:59:29 +0000222 if (a_module_sp.get() < b_module_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 return -1;
224 else
225 return 1; // We already know the modules aren't equal
226 }
227}
228
229
230void
Greg Clayton10177aa2010-12-08 05:08:21 +0000231Section::Dump (Stream *s, Target *target, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232{
Greg Clayton89411422010-10-08 00:21:05 +0000233// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +0000235 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 bool resolved = true;
237 addr_t addr = LLDB_INVALID_ADDRESS;
238
239 if (GetByteSize() == 0)
240 s->Printf("%39s", "");
241 else
242 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000243 if (target)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000244 addr = GetLoadBaseAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
246 if (addr == LLDB_INVALID_ADDRESS)
247 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000248 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 resolved = false;
250 addr = GetFileAddress();
251 }
252
253 VMRange range(addr, addr + m_byte_size);
254 range.Dump (s, 0);
255 }
256
Daniel Malead01b2952012-11-29 21:49:15 +0000257 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258
259 DumpName (s);
260
261 s->EOL();
262
Greg Clayton10177aa2010-12-08 05:08:21 +0000263 if (depth > 0)
264 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265}
266
267void
268Section::DumpName (Stream *s) const
269{
Greg Claytone72dfb32012-02-24 01:59:29 +0000270 SectionSP parent_sp (GetParent ());
271 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000273 parent_sp->DumpName (s);
274 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 }
276 else
277 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000278 // The top most section prints the module basename
Michael Sartaina7499c92013-07-01 19:45:50 +0000279 const char * name = NULL;
Greg Claytone72dfb32012-02-24 01:59:29 +0000280 ModuleSP module_sp (GetModule());
Michael Sartaina7499c92013-07-01 19:45:50 +0000281 const FileSpec &file_spec = m_obj_file->GetFileSpec();
282
283 if (m_obj_file)
284 name = file_spec.GetFilename().AsCString();
285 if ((!name || !name[0]) && module_sp)
286 name = module_sp->GetFileSpec().GetFilename().AsCString();
287 if (name && name[0])
288 s->Printf("%s.", name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 }
290 m_name.Dump(s);
291}
292
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294Section::IsDescendant (const Section *section)
295{
296 if (this == section)
297 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +0000298 SectionSP parent_sp (GetParent ());
299 if (parent_sp)
300 return parent_sp->IsDescendant (section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 return false;
302}
303
304bool
305Section::Slide (addr_t slide_amount, bool slide_children)
306{
307 if (m_file_addr != LLDB_INVALID_ADDRESS)
308 {
309 if (slide_amount == 0)
310 return true;
311
312 m_file_addr += slide_amount;
313
314 if (slide_children)
315 m_children.Slide (slide_amount, slide_children);
316
317 return true;
318 }
319 return false;
320}
321
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322#pragma mark SectionList
323
324SectionList::SectionList () :
325 m_sections()
326{
327}
328
329
330SectionList::~SectionList ()
331{
332}
333
Greg Clayton3046e662013-07-10 01:23:25 +0000334SectionList &
335SectionList::operator = (const SectionList& rhs)
Michael Sartaina7499c92013-07-01 19:45:50 +0000336{
Greg Clayton3046e662013-07-10 01:23:25 +0000337 if (this != &rhs)
338 m_sections = rhs.m_sections;
339 return *this;
Michael Sartaina7499c92013-07-01 19:45:50 +0000340}
341
Greg Claytonc7bece562013-01-25 18:06:21 +0000342size_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000343SectionList::AddSection (const lldb::SectionSP& section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344{
Greg Clayton3698a712014-05-14 01:12:09 +0000345 if (section_sp)
346 {
347 size_t section_index = m_sections.size();
348 m_sections.push_back(section_sp);
349 return section_index;
350 }
Todd Fialafd8ae3a2014-05-14 16:15:43 +0000351
352 return std::numeric_limits<size_t>::max ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
Michael Sartaina7499c92013-07-01 19:45:50 +0000355// Warning, this can be slow as it's removing items from a std::vector.
356bool
357SectionList::DeleteSection (size_t idx)
358{
359 if (idx < m_sections.size())
360 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000361 m_sections.erase (m_sections.begin() + idx);
362 return true;
363 }
364 return false;
365}
366
Greg Claytonc7bece562013-01-25 18:06:21 +0000367size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368SectionList::FindSectionIndex (const Section* sect)
369{
370 iterator sect_iter;
371 iterator begin = m_sections.begin();
372 iterator end = m_sections.end();
373 for (sect_iter = begin; sect_iter != end; ++sect_iter)
374 {
375 if (sect_iter->get() == sect)
376 {
377 // The secton was already in this section list
378 return std::distance (begin, sect_iter);
379 }
380 }
381 return UINT32_MAX;
382}
383
Greg Claytonc7bece562013-01-25 18:06:21 +0000384size_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000385SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386{
Greg Claytonc7bece562013-01-25 18:06:21 +0000387 size_t sect_idx = FindSectionIndex (sect_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 if (sect_idx == UINT32_MAX)
Michael Sartaina7499c92013-07-01 19:45:50 +0000389 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 sect_idx = AddSection (sect_sp);
Michael Sartaina7499c92013-07-01 19:45:50 +0000391 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 return sect_idx;
393}
394
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000396SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397{
398 iterator sect_iter, end = m_sections.end();
399 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
400 {
401 if ((*sect_iter)->GetID() == sect_id)
402 {
403 *sect_iter = sect_sp;
404 return true;
405 }
406 else if (depth > 0)
407 {
408 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
409 return true;
410 }
411 }
412 return false;
413}
414
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416SectionList::GetNumSections (uint32_t depth) const
417{
418 size_t count = m_sections.size();
419 if (depth > 0)
420 {
421 const_iterator sect_iter, end = m_sections.end();
422 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
423 {
424 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
425 }
426 }
427 return count;
428}
429
430SectionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000431SectionList::GetSectionAtIndex (size_t idx) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432{
433 SectionSP sect_sp;
434 if (idx < m_sections.size())
435 sect_sp = m_sections[idx];
436 return sect_sp;
437}
438
439SectionSP
440SectionList::FindSectionByName (const ConstString &section_dstr) const
441{
442 SectionSP sect_sp;
443 // Check if we have a valid section string
Greg Claytone72dfb32012-02-24 01:59:29 +0000444 if (section_dstr && !m_sections.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 {
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)
449 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000450 Section *child_section = sect_iter->get();
Greg Clayton3698a712014-05-14 01:12:09 +0000451 if (child_section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 {
Greg Clayton3698a712014-05-14 01:12:09 +0000453 if (child_section->GetName() == section_dstr)
454 {
455 sect_sp = *sect_iter;
456 }
457 else
458 {
459 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
460 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
462 }
463 }
464 return sect_sp;
465}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
467SectionSP
468SectionList::FindSectionByID (user_id_t sect_id) const
469{
470 SectionSP sect_sp;
471 if (sect_id)
472 {
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)
476 {
477 if ((*sect_iter)->GetID() == sect_id)
478 {
479 sect_sp = *sect_iter;
480 break;
481 }
482 else
483 {
484 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
485 }
486 }
487 }
488 return sect_sp;
489}
490
Greg Clayton70e33eb2010-07-21 21:49:46 +0000491
492SectionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000493SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
Greg Clayton70e33eb2010-07-21 21:49:46 +0000494{
495 SectionSP sect_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +0000496 size_t num_sections = m_sections.size();
497 for (size_t idx = start_idx; idx < num_sections; ++idx)
Greg Clayton70e33eb2010-07-21 21:49:46 +0000498 {
499 if (m_sections[idx]->GetType() == sect_type)
500 {
501 sect_sp = m_sections[idx];
502 break;
503 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000504 else if (check_children)
505 {
506 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
507 if (sect_sp)
508 break;
509 }
Greg Clayton70e33eb2010-07-21 21:49:46 +0000510 }
511 return sect_sp;
512}
513
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
516{
517 SectionSP sect_sp;
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)
521 {
522 Section *sect = sect_iter->get();
523 if (sect->ContainsFileAddress (vm_addr))
524 {
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.
528 if (depth > 0)
529 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
530
531 if (sect_sp.get() == NULL && !sect->IsFake())
532 sect_sp = *sect_iter;
533 }
534 }
535 return sect_sp;
536}
537
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538bool
539SectionList::ContainsSection(user_id_t sect_id) const
540{
541 return FindSectionByID (sect_id).get() != NULL;
542}
543
544void
Greg Clayton10177aa2010-12-08 05:08:21 +0000545SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546{
Greg Claytonf6693582010-12-07 18:05:22 +0000547 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 if (show_header && !m_sections.empty())
549 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000551 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000553 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 }
555
556
557 const_iterator sect_iter;
558 const_iterator end = m_sections.end();
559 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
560 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000561 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 }
563
564 if (show_header && !m_sections.empty())
565 s->IndentLess();
566
567}
568
569size_t
570SectionList::Slide (addr_t slide_amount, bool slide_children)
571{
572 size_t count = 0;
573 const_iterator pos, end = m_sections.end();
574 for (pos = m_sections.begin(); pos != end; ++pos)
575 {
576 if ((*pos)->Slide(slide_amount, slide_children))
577 ++count;
578 }
579 return count;
580}