blob: ea33a62b775f266748e9a068057d77756015ffd4 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Todd Fialafd8ae3a2014-05-14 16:15:43 +000016#include <limits>
17
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018using namespace lldb;
19using namespace lldb_private;
20
Greg Claytone72dfb32012-02-24 01:59:29 +000021Section::Section (const ModuleSP &module_sp,
Michael Sartaina7499c92013-07-01 19:45:50 +000022 ObjectFile *obj_file,
Greg Claytone72dfb32012-02-24 01:59:29 +000023 user_id_t sect_id,
24 const ConstString &name,
25 SectionType sect_type,
26 addr_t file_addr,
27 addr_t byte_size,
Greg Clayton9422dd62013-03-04 21:46:16 +000028 lldb::offset_t file_offset,
29 lldb::offset_t file_size,
Greg Clayton48672af2014-06-24 22:22:43 +000030 uint32_t log2align,
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000031 uint32_t flags,
32 uint32_t target_byte_size/*=1*/) :
Greg Claytone72dfb32012-02-24 01:59:29 +000033 ModuleChild (module_sp),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034 UserID (sect_id),
35 Flags (flags),
Michael Sartaina7499c92013-07-01 19:45:50 +000036 m_obj_file (obj_file),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000037 m_type (sect_type),
Greg Claytone72dfb32012-02-24 01:59:29 +000038 m_parent_wp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039 m_name (name),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_file_addr (file_addr),
41 m_byte_size (byte_size),
42 m_file_offset (file_offset),
43 m_file_size (file_size),
Greg Clayton48672af2014-06-24 22:22:43 +000044 m_log2align (log2align),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 m_children (),
46 m_fake (false),
Greg Clayton741f3f92012-03-27 21:10:07 +000047 m_encrypted (false),
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000048 m_thread_specific (false),
49 m_target_byte_size(target_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050{
Daniel Malead01b2952012-11-29 21:49:15 +000051// 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 +000052// this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
53}
54
55Section::Section (const lldb::SectionSP &parent_section_sp,
56 const ModuleSP &module_sp,
Michael Sartaina7499c92013-07-01 19:45:50 +000057 ObjectFile *obj_file,
Greg Claytone72dfb32012-02-24 01:59:29 +000058 user_id_t sect_id,
59 const ConstString &name,
60 SectionType sect_type,
61 addr_t file_addr,
62 addr_t byte_size,
Greg Clayton9422dd62013-03-04 21:46:16 +000063 lldb::offset_t file_offset,
64 lldb::offset_t file_size,
Greg Clayton48672af2014-06-24 22:22:43 +000065 uint32_t log2align,
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000066 uint32_t flags,
67 uint32_t target_byte_size/*=1*/) :
Greg Claytone72dfb32012-02-24 01:59:29 +000068 ModuleChild (module_sp),
69 UserID (sect_id),
70 Flags (flags),
Michael Sartaina7499c92013-07-01 19:45:50 +000071 m_obj_file (obj_file),
Greg Claytond8c3d4b2013-06-19 21:50:28 +000072 m_type (sect_type),
Greg Claytone72dfb32012-02-24 01:59:29 +000073 m_parent_wp (),
74 m_name (name),
Greg Claytone72dfb32012-02-24 01:59:29 +000075 m_file_addr (file_addr),
76 m_byte_size (byte_size),
77 m_file_offset (file_offset),
78 m_file_size (file_size),
Greg Clayton48672af2014-06-24 22:22:43 +000079 m_log2align (log2align),
Greg Claytone72dfb32012-02-24 01:59:29 +000080 m_children (),
81 m_fake (false),
Greg Clayton741f3f92012-03-27 21:10:07 +000082 m_encrypted (false),
Matthew Gardinerf03e6d842014-09-29 08:02:24 +000083 m_thread_specific (false),
84 m_target_byte_size(target_byte_size)
Greg Claytone72dfb32012-02-24 01:59:29 +000085{
Daniel Malead01b2952012-11-29 21:49:15 +000086// 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 +000087// 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090}
91
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092Section::~Section()
93{
Greg Claytone72dfb32012-02-24 01:59:29 +000094// printf ("Section::~Section(%p)\n", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097addr_t
98Section::GetFileAddress () const
99{
Greg Claytone72dfb32012-02-24 01:59:29 +0000100 SectionSP parent_sp (GetParent ());
101 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 {
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
Greg Claytone72dfb32012-02-24 01:59:29 +0000106 return parent_sp->GetFileAddress() + m_file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 }
108 // This section has no parent, so m_file_addr is the file base address
109 return m_file_addr;
110}
111
Jason Molenda20eb31b2013-08-16 03:20:42 +0000112bool
113Section::SetFileAddress (lldb::addr_t file_addr)
114{
115 SectionSP parent_sp (GetParent ());
116 if (parent_sp)
117 {
118 if (m_file_addr >= file_addr)
119 return parent_sp->SetFileAddress (m_file_addr - file_addr);
120 return false;
121 }
122 else
123 {
124 // This section has no parent, so m_file_addr is the file base address
125 m_file_addr = file_addr;
126 return true;
127 }
128}
129
Greg Claytone72dfb32012-02-24 01:59:29 +0000130lldb::addr_t
131Section::GetOffset () const
132{
133 // This section has a parent which means m_file_addr is an offset.
134 SectionSP parent_sp (GetParent ());
135 if (parent_sp)
136 return m_file_addr;
137
138 // This section has no parent, so there is no offset to be had
139 return 0;
140}
141
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000143Section::GetLoadBaseAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144{
145 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
Greg Clayton9422dd62013-03-04 21:46:16 +0000146 SectionSP parent_sp (GetParent ());
147 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000149 load_base_addr = parent_sp->GetLoadBaseAddress (target);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000150 if (load_base_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +0000151 load_base_addr += GetOffset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 }
Greg Clayton48672af2014-06-24 22:22:43 +0000153 if (load_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000155 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 return load_base_addr;
158}
159
160bool
161Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
162{
Greg Claytonc7bece562013-01-25 18:06:21 +0000163 const size_t num_children = m_children.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 if (num_children > 0)
165 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000166 for (size_t i=0; i<num_children; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 {
168 Section* child_section = m_children.GetSectionAtIndex (i).get();
169
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);
173 }
174 }
Greg Clayton9422dd62013-03-04 21:46:16 +0000175 so_addr.SetOffset(offset);
176 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
177
Sean Callanan27e02d02012-07-12 20:44:21 +0000178#ifdef LLDB_CONFIGURATION_DEBUG
Greg Clayton9422dd62013-03-04 21:46:16 +0000179 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
180 assert(GetModule().get());
Sean Callanan27e02d02012-07-12 20:44:21 +0000181#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 return true;
183}
184
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185bool
186Section::ContainsFileAddress (addr_t vm_addr) const
187{
188 const addr_t file_addr = GetFileAddress();
189 if (file_addr != LLDB_INVALID_ADDRESS)
190 {
191 if (file_addr <= vm_addr)
192 {
Matthew Gardinerf03e6d842014-09-29 08:02:24 +0000193 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 return offset < GetByteSize();
195 }
196 }
197 return false;
198}
199
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200int
201Section::Compare (const Section& a, const Section& b)
202{
203 if (&a == &b)
204 return 0;
205
Greg Claytone72dfb32012-02-24 01:59:29 +0000206 const ModuleSP a_module_sp = a.GetModule();
207 const ModuleSP b_module_sp = b.GetModule();
208 if (a_module_sp == b_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 {
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)
213 return -1;
214 if (a_sect_uid > b_sect_uid)
215 return 1;
216 return 0;
217 }
218 else
219 {
220 // The modules are different, just compare the module pointers
Greg Claytone72dfb32012-02-24 01:59:29 +0000221 if (a_module_sp.get() < b_module_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 return -1;
223 else
224 return 1; // We already know the modules aren't equal
225 }
226}
227
228
229void
Greg Clayton10177aa2010-12-08 05:08:21 +0000230Section::Dump (Stream *s, Target *target, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231{
Greg Clayton89411422010-10-08 00:21:05 +0000232// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +0000234 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235 bool resolved = true;
236 addr_t addr = LLDB_INVALID_ADDRESS;
237
238 if (GetByteSize() == 0)
239 s->Printf("%39s", "");
240 else
241 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000242 if (target)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000243 addr = GetLoadBaseAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
245 if (addr == LLDB_INVALID_ADDRESS)
246 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000247 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 resolved = false;
249 addr = GetFileAddress();
250 }
251
252 VMRange range(addr, addr + m_byte_size);
253 range.Dump (s, 0);
254 }
255
Daniel Malead01b2952012-11-29 21:49:15 +0000256 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 +0000257
258 DumpName (s);
259
260 s->EOL();
261
Greg Clayton10177aa2010-12-08 05:08:21 +0000262 if (depth > 0)
263 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
266void
267Section::DumpName (Stream *s) const
268{
Greg Claytone72dfb32012-02-24 01:59:29 +0000269 SectionSP parent_sp (GetParent ());
270 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000272 parent_sp->DumpName (s);
273 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274 }
275 else
276 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000277 // The top most section prints the module basename
Michael Sartaina7499c92013-07-01 19:45:50 +0000278 const char * name = NULL;
Greg Claytone72dfb32012-02-24 01:59:29 +0000279 ModuleSP module_sp (GetModule());
Michael Sartaina7499c92013-07-01 19:45:50 +0000280 const FileSpec &file_spec = m_obj_file->GetFileSpec();
281
282 if (m_obj_file)
283 name = file_spec.GetFilename().AsCString();
284 if ((!name || !name[0]) && module_sp)
285 name = module_sp->GetFileSpec().GetFilename().AsCString();
286 if (name && name[0])
287 s->Printf("%s.", name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 }
289 m_name.Dump(s);
290}
291
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293Section::IsDescendant (const Section *section)
294{
295 if (this == section)
296 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +0000297 SectionSP parent_sp (GetParent ());
298 if (parent_sp)
299 return parent_sp->IsDescendant (section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 return false;
301}
302
303bool
304Section::Slide (addr_t slide_amount, bool slide_children)
305{
306 if (m_file_addr != LLDB_INVALID_ADDRESS)
307 {
308 if (slide_amount == 0)
309 return true;
310
311 m_file_addr += slide_amount;
312
313 if (slide_children)
314 m_children.Slide (slide_amount, slide_children);
315
316 return true;
317 }
318 return false;
319}
320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321#pragma mark SectionList
322
323SectionList::SectionList () :
324 m_sections()
325{
326}
327
328
329SectionList::~SectionList ()
330{
331}
332
Greg Clayton3046e662013-07-10 01:23:25 +0000333SectionList &
334SectionList::operator = (const SectionList& rhs)
Michael Sartaina7499c92013-07-01 19:45:50 +0000335{
Greg Clayton3046e662013-07-10 01:23:25 +0000336 if (this != &rhs)
337 m_sections = rhs.m_sections;
338 return *this;
Michael Sartaina7499c92013-07-01 19:45:50 +0000339}
340
Greg Claytonc7bece562013-01-25 18:06:21 +0000341size_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000342SectionList::AddSection (const lldb::SectionSP& section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343{
Greg Clayton3698a712014-05-14 01:12:09 +0000344 if (section_sp)
345 {
346 size_t section_index = m_sections.size();
347 m_sections.push_back(section_sp);
348 return section_index;
349 }
Todd Fialafd8ae3a2014-05-14 16:15:43 +0000350
351 return std::numeric_limits<size_t>::max ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
Michael Sartaina7499c92013-07-01 19:45:50 +0000354// Warning, this can be slow as it's removing items from a std::vector.
355bool
356SectionList::DeleteSection (size_t idx)
357{
358 if (idx < m_sections.size())
359 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000360 m_sections.erase (m_sections.begin() + idx);
361 return true;
362 }
363 return false;
364}
365
Greg Claytonc7bece562013-01-25 18:06:21 +0000366size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367SectionList::FindSectionIndex (const Section* sect)
368{
369 iterator sect_iter;
370 iterator begin = m_sections.begin();
371 iterator end = m_sections.end();
372 for (sect_iter = begin; sect_iter != end; ++sect_iter)
373 {
374 if (sect_iter->get() == sect)
375 {
376 // The secton was already in this section list
377 return std::distance (begin, sect_iter);
378 }
379 }
380 return UINT32_MAX;
381}
382
Greg Claytonc7bece562013-01-25 18:06:21 +0000383size_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000384SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385{
Greg Claytonc7bece562013-01-25 18:06:21 +0000386 size_t sect_idx = FindSectionIndex (sect_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 if (sect_idx == UINT32_MAX)
Michael Sartaina7499c92013-07-01 19:45:50 +0000388 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 sect_idx = AddSection (sect_sp);
Michael Sartaina7499c92013-07-01 19:45:50 +0000390 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 return sect_idx;
392}
393
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000395SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
397 iterator sect_iter, end = m_sections.end();
398 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
399 {
400 if ((*sect_iter)->GetID() == sect_id)
401 {
402 *sect_iter = sect_sp;
403 return true;
404 }
405 else if (depth > 0)
406 {
407 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
408 return true;
409 }
410 }
411 return false;
412}
413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415SectionList::GetNumSections (uint32_t depth) const
416{
417 size_t count = m_sections.size();
418 if (depth > 0)
419 {
420 const_iterator sect_iter, end = m_sections.end();
421 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
422 {
423 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
424 }
425 }
426 return count;
427}
428
429SectionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000430SectionList::GetSectionAtIndex (size_t idx) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431{
432 SectionSP sect_sp;
433 if (idx < m_sections.size())
434 sect_sp = m_sections[idx];
435 return sect_sp;
436}
437
438SectionSP
439SectionList::FindSectionByName (const ConstString &section_dstr) const
440{
441 SectionSP sect_sp;
442 // Check if we have a valid section string
Greg Claytone72dfb32012-02-24 01:59:29 +0000443 if (section_dstr && !m_sections.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 {
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)
448 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000449 Section *child_section = sect_iter->get();
Greg Clayton3698a712014-05-14 01:12:09 +0000450 if (child_section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 {
Greg Clayton3698a712014-05-14 01:12:09 +0000452 if (child_section->GetName() == section_dstr)
453 {
454 sect_sp = *sect_iter;
455 }
456 else
457 {
458 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
459 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 }
461 }
462 }
463 return sect_sp;
464}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465
466SectionSP
467SectionList::FindSectionByID (user_id_t sect_id) const
468{
469 SectionSP sect_sp;
470 if (sect_id)
471 {
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)
475 {
476 if ((*sect_iter)->GetID() == sect_id)
477 {
478 sect_sp = *sect_iter;
479 break;
480 }
481 else
482 {
483 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
484 }
485 }
486 }
487 return sect_sp;
488}
489
Greg Clayton70e33eb2010-07-21 21:49:46 +0000490
491SectionSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000492SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
Greg Clayton70e33eb2010-07-21 21:49:46 +0000493{
494 SectionSP sect_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +0000495 size_t num_sections = m_sections.size();
496 for (size_t idx = start_idx; idx < num_sections; ++idx)
Greg Clayton70e33eb2010-07-21 21:49:46 +0000497 {
498 if (m_sections[idx]->GetType() == sect_type)
499 {
500 sect_sp = m_sections[idx];
501 break;
502 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000503 else if (check_children)
504 {
505 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
506 if (sect_sp)
507 break;
508 }
Greg Clayton70e33eb2010-07-21 21:49:46 +0000509 }
510 return sect_sp;
511}
512
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
515{
516 SectionSP sect_sp;
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)
520 {
521 Section *sect = sect_iter->get();
522 if (sect->ContainsFileAddress (vm_addr))
523 {
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.
527 if (depth > 0)
528 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
529
530 if (sect_sp.get() == NULL && !sect->IsFake())
531 sect_sp = *sect_iter;
532 }
533 }
534 return sect_sp;
535}
536
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537bool
538SectionList::ContainsSection(user_id_t sect_id) const
539{
540 return FindSectionByID (sect_id).get() != NULL;
541}
542
543void
Greg Clayton10177aa2010-12-08 05:08:21 +0000544SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545{
Greg Claytonf6693582010-12-07 18:05:22 +0000546 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 if (show_header && !m_sections.empty())
548 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000550 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 +0000551 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000552 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 }
554
555
556 const_iterator sect_iter;
557 const_iterator end = m_sections.end();
558 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
559 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000560 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 }
562
563 if (show_header && !m_sections.empty())
564 s->IndentLess();
565
566}
567
568size_t
569SectionList::Slide (addr_t slide_amount, bool slide_children)
570{
571 size_t count = 0;
572 const_iterator pos, end = m_sections.end();
573 for (pos = m_sections.begin(); pos != end; ++pos)
574 {
575 if ((*pos)->Slide(slide_amount, slide_children))
576 ++count;
577 }
578 return count;
579}