blob: f1f4dc112716466d1e1a9eeb57d938d54709f2bc [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 Claytonf5e56de2010-09-14 23:36:40 +000013#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15using namespace lldb;
16using namespace lldb_private;
17
Greg Claytone72dfb32012-02-24 01:59:29 +000018Section::Section (const ModuleSP &module_sp,
19 user_id_t sect_id,
20 const ConstString &name,
21 SectionType sect_type,
22 addr_t file_addr,
23 addr_t byte_size,
24 uint64_t file_offset,
25 uint64_t file_size,
26 uint32_t flags) :
27 ModuleChild (module_sp),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028 UserID (sect_id),
29 Flags (flags),
Greg Claytone72dfb32012-02-24 01:59:29 +000030 m_parent_wp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 m_name (name),
32 m_type (sect_type),
33 m_file_addr (file_addr),
34 m_byte_size (byte_size),
35 m_file_offset (file_offset),
36 m_file_size (file_size),
37 m_children (),
38 m_fake (false),
Greg Claytone72dfb32012-02-24 01:59:29 +000039 m_linked_section_wp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_linked_offset (0)
41{
Greg Claytone72dfb32012-02-24 01:59:29 +000042// printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16llx, addr=[0x%16.16llx - 0x%16.16llx), file [0x%16.16llx - 0x%16.16llx), flags = 0x%8.8x, name = %s\n",
43// this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
44}
45
46Section::Section (const lldb::SectionSP &parent_section_sp,
47 const ModuleSP &module_sp,
48 user_id_t sect_id,
49 const ConstString &name,
50 SectionType sect_type,
51 addr_t file_addr,
52 addr_t byte_size,
53 uint64_t file_offset,
54 uint64_t file_size,
55 uint32_t flags) :
56 ModuleChild (module_sp),
57 UserID (sect_id),
58 Flags (flags),
59 m_parent_wp (),
60 m_name (name),
61 m_type (sect_type),
62 m_file_addr (file_addr),
63 m_byte_size (byte_size),
64 m_file_offset (file_offset),
65 m_file_size (file_size),
66 m_children (),
67 m_fake (false),
68 m_linked_section_wp(),
69 m_linked_offset (0)
70{
71// printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16llx, addr=[0x%16.16llx - 0x%16.16llx), file [0x%16.16llx - 0x%16.16llx), flags = 0x%8.8x, name = %s.%s\n",
72// 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());
73 if (parent_section_sp)
74 m_parent_wp = parent_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077Section::~Section()
78{
Greg Claytone72dfb32012-02-24 01:59:29 +000079// printf ("Section::~Section(%p)\n", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
82const ConstString&
83Section::GetName() const
84{
Greg Claytone72dfb32012-02-24 01:59:29 +000085 SectionSP linked_section_sp (m_linked_section_wp.lock());
86 if (linked_section_sp)
87 return linked_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 return m_name;
89}
90
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091addr_t
92Section::GetFileAddress () const
93{
Greg Claytone72dfb32012-02-24 01:59:29 +000094 SectionSP parent_sp (GetParent ());
95 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 {
97 // This section has a parent which means m_file_addr is an offset into
98 // the parent section, so the file address for this section is the file
99 // address of the parent plus the offset
Greg Claytone72dfb32012-02-24 01:59:29 +0000100 return parent_sp->GetFileAddress() + m_file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 }
102 // This section has no parent, so m_file_addr is the file base address
103 return m_file_addr;
104}
105
Greg Claytone72dfb32012-02-24 01:59:29 +0000106lldb::addr_t
107Section::GetOffset () const
108{
109 // This section has a parent which means m_file_addr is an offset.
110 SectionSP parent_sp (GetParent ());
111 if (parent_sp)
112 return m_file_addr;
113
114 // This section has no parent, so there is no offset to be had
115 return 0;
116}
117
118
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119addr_t
120Section::GetLinkedFileAddress () const
121{
Greg Claytone72dfb32012-02-24 01:59:29 +0000122 SectionSP linked_section_sp (m_linked_section_wp.lock());
123 if (linked_section_sp)
124 return linked_section_sp->GetFileAddress() + m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 return LLDB_INVALID_ADDRESS;
126}
127
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128
129addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000130Section::GetLoadBaseAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131{
132 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
Greg Claytone72dfb32012-02-24 01:59:29 +0000133 SectionSP linked_section_sp (m_linked_section_wp.lock());
134 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000136 load_base_addr = linked_section_sp->GetLoadBaseAddress(target);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000137 if (load_base_addr != LLDB_INVALID_ADDRESS)
138 load_base_addr += m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 }
140 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000142 SectionSP parent_sp (GetParent ());
143 if (parent_sp)
144 {
145 load_base_addr = parent_sp->GetLoadBaseAddress (target);
146 if (load_base_addr != LLDB_INVALID_ADDRESS)
147 load_base_addr += GetOffset();
148 }
149 else
150 {
151 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this);
152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 }
154
155 return load_base_addr;
156}
157
158bool
159Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
160{
161 const uint32_t num_children = m_children.GetSize();
162 if (num_children > 0)
163 {
164 for (uint32_t i=0; i<num_children; i++)
165 {
166 Section* child_section = m_children.GetSectionAtIndex (i).get();
167
168 addr_t child_offset = child_section->GetOffset();
169 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
170 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
171 }
172 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000173 SectionSP linked_section_sp (m_linked_section_wp.lock());
174 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 {
176 so_addr.SetOffset(m_linked_offset + offset);
Greg Claytone72dfb32012-02-24 01:59:29 +0000177 so_addr.SetSection(linked_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 }
179 else
180 {
181 so_addr.SetOffset(offset);
Greg Claytone72dfb32012-02-24 01:59:29 +0000182 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 }
184 return true;
185}
186
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187bool
188Section::ContainsFileAddress (addr_t vm_addr) const
189{
190 const addr_t file_addr = GetFileAddress();
191 if (file_addr != LLDB_INVALID_ADDRESS)
192 {
193 if (file_addr <= vm_addr)
194 {
195 const addr_t offset = vm_addr - file_addr;
196 return offset < GetByteSize();
197 }
198 }
199 return false;
200}
201
202bool
203Section::ContainsLinkedFileAddress (addr_t vm_addr) const
204{
205 const addr_t linked_file_addr = GetLinkedFileAddress();
206 if (linked_file_addr != LLDB_INVALID_ADDRESS)
207 {
208 if (linked_file_addr <= vm_addr)
209 {
210 const addr_t offset = vm_addr - linked_file_addr;
211 return offset < GetByteSize();
212 }
213 }
214 return false;
215}
216
217int
218Section::Compare (const Section& a, const Section& b)
219{
220 if (&a == &b)
221 return 0;
222
Greg Claytone72dfb32012-02-24 01:59:29 +0000223 const ModuleSP a_module_sp = a.GetModule();
224 const ModuleSP b_module_sp = b.GetModule();
225 if (a_module_sp == b_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 {
227 user_id_t a_sect_uid = a.GetID();
228 user_id_t b_sect_uid = b.GetID();
229 if (a_sect_uid < b_sect_uid)
230 return -1;
231 if (a_sect_uid > b_sect_uid)
232 return 1;
233 return 0;
234 }
235 else
236 {
237 // The modules are different, just compare the module pointers
Greg Claytone72dfb32012-02-24 01:59:29 +0000238 if (a_module_sp.get() < b_module_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 return -1;
240 else
241 return 1; // We already know the modules aren't equal
242 }
243}
244
245
246void
Greg Clayton10177aa2010-12-08 05:08:21 +0000247Section::Dump (Stream *s, Target *target, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248{
Greg Clayton89411422010-10-08 00:21:05 +0000249// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000251 s->Printf("0x%8.8llx %-16s ", GetID(), GetSectionTypeAsCString (m_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 bool resolved = true;
253 addr_t addr = LLDB_INVALID_ADDRESS;
254
Greg Claytone72dfb32012-02-24 01:59:29 +0000255 SectionSP linked_section_sp (m_linked_section_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 if (GetByteSize() == 0)
257 s->Printf("%39s", "");
258 else
259 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000260 if (target && linked_section_sp.get() == NULL)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000261 addr = GetLoadBaseAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262
263 if (addr == LLDB_INVALID_ADDRESS)
264 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000265 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266 resolved = false;
267 addr = GetFileAddress();
268 }
269
270 VMRange range(addr, addr + m_byte_size);
271 range.Dump (s, 0);
272 }
273
Greg Clayton73b472d2010-10-27 03:32:59 +0000274 s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
276 DumpName (s);
277
278 s->EOL();
279
Greg Claytone72dfb32012-02-24 01:59:29 +0000280 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 {
282 addr = LLDB_INVALID_ADDRESS;
Greg Claytonf6693582010-12-07 18:05:22 +0000283 resolved = true;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000284 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000286 addr = linked_section_sp->GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 if (addr != LLDB_INVALID_ADDRESS)
288 addr += m_linked_offset;
289 }
290
291 if (addr == LLDB_INVALID_ADDRESS)
292 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000293 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 resolved = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000295 addr = linked_section_sp->GetFileAddress() + m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 }
297
Greg Clayton65e364e2010-12-07 07:37:38 +0000298 int indent = 26 + s->GetIndentLevel();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 s->Printf("%*.*s", indent, indent, "");
300 VMRange linked_range(addr, addr + m_byte_size);
301 linked_range.Dump (s, 0);
302 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1;
303 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, "");
304
Greg Claytone72dfb32012-02-24 01:59:29 +0000305 linked_section_sp->DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 s->Printf(" + 0x%llx\n", m_linked_offset);
307 }
308
Greg Clayton10177aa2010-12-08 05:08:21 +0000309 if (depth > 0)
310 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311}
312
313void
314Section::DumpName (Stream *s) const
315{
Greg Claytone72dfb32012-02-24 01:59:29 +0000316 SectionSP parent_sp (GetParent ());
317 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000319 parent_sp->DumpName (s);
320 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
322 else
323 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000324 // The top most section prints the module basename
325 ModuleSP module_sp (GetModule());
326 if (module_sp)
327 {
328 const char *module_basename = module_sp->GetFileSpec().GetFilename().AsCString();
329 if (module_basename && module_basename[0])
330 s->Printf("%s.", module_basename);
331 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 }
333 m_name.Dump(s);
334}
335
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337Section::IsDescendant (const Section *section)
338{
339 if (this == section)
340 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +0000341 SectionSP parent_sp (GetParent ());
342 if (parent_sp)
343 return parent_sp->IsDescendant (section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 return false;
345}
346
347bool
348Section::Slide (addr_t slide_amount, bool slide_children)
349{
350 if (m_file_addr != LLDB_INVALID_ADDRESS)
351 {
352 if (slide_amount == 0)
353 return true;
354
355 m_file_addr += slide_amount;
356
357 if (slide_children)
358 m_children.Slide (slide_amount, slide_children);
359
360 return true;
361 }
362 return false;
363}
364
365void
Greg Claytone72dfb32012-02-24 01:59:29 +0000366Section::SetLinkedLocation (const lldb::SectionSP &linked_section_sp, uint64_t linked_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367{
Greg Claytone72dfb32012-02-24 01:59:29 +0000368 if (linked_section_sp)
369 m_module_wp = linked_section_sp->GetModule();
370 m_linked_section_wp = linked_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 m_linked_offset = linked_offset;
372}
373
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374#pragma mark SectionList
375
376SectionList::SectionList () :
377 m_sections()
378{
379}
380
381
382SectionList::~SectionList ()
383{
384}
385
386uint32_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000387SectionList::AddSection (const lldb::SectionSP& section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388{
Greg Claytone72dfb32012-02-24 01:59:29 +0000389 assert (section_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 uint32_t section_index = m_sections.size();
Greg Claytone72dfb32012-02-24 01:59:29 +0000391 m_sections.push_back(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 return section_index;
393}
394
395uint32_t
396SectionList::FindSectionIndex (const Section* sect)
397{
398 iterator sect_iter;
399 iterator begin = m_sections.begin();
400 iterator end = m_sections.end();
401 for (sect_iter = begin; sect_iter != end; ++sect_iter)
402 {
403 if (sect_iter->get() == sect)
404 {
405 // The secton was already in this section list
406 return std::distance (begin, sect_iter);
407 }
408 }
409 return UINT32_MAX;
410}
411
412uint32_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000413SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
415 uint32_t sect_idx = FindSectionIndex (sect_sp.get());
416 if (sect_idx == UINT32_MAX)
417 sect_idx = AddSection (sect_sp);
418 return sect_idx;
419}
420
421
422bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000423SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424{
425 iterator sect_iter, end = m_sections.end();
426 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
427 {
428 if ((*sect_iter)->GetID() == sect_id)
429 {
430 *sect_iter = sect_sp;
431 return true;
432 }
433 else if (depth > 0)
434 {
435 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
436 return true;
437 }
438 }
439 return false;
440}
441
442
443size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444SectionList::GetNumSections (uint32_t depth) const
445{
446 size_t count = m_sections.size();
447 if (depth > 0)
448 {
449 const_iterator sect_iter, end = m_sections.end();
450 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
451 {
452 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
453 }
454 }
455 return count;
456}
457
458SectionSP
459SectionList::GetSectionAtIndex (uint32_t idx) const
460{
461 SectionSP sect_sp;
462 if (idx < m_sections.size())
463 sect_sp = m_sections[idx];
464 return sect_sp;
465}
466
467SectionSP
468SectionList::FindSectionByName (const ConstString &section_dstr) const
469{
470 SectionSP sect_sp;
471 // Check if we have a valid section string
Greg Claytone72dfb32012-02-24 01:59:29 +0000472 if (section_dstr && !m_sections.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 {
474 const_iterator sect_iter;
475 const_iterator end = m_sections.end();
476 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
477 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000478 Section *child_section = sect_iter->get();
479 assert (child_section);
480 if (child_section->GetName() == section_dstr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 {
482 sect_sp = *sect_iter;
483 }
484 else
485 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000486 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
488 }
489 }
490 return sect_sp;
491}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
493SectionSP
494SectionList::FindSectionByID (user_id_t sect_id) const
495{
496 SectionSP sect_sp;
497 if (sect_id)
498 {
499 const_iterator sect_iter;
500 const_iterator end = m_sections.end();
501 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
502 {
503 if ((*sect_iter)->GetID() == sect_id)
504 {
505 sect_sp = *sect_iter;
506 break;
507 }
508 else
509 {
510 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
511 }
512 }
513 }
514 return sect_sp;
515}
516
Greg Clayton70e33eb2010-07-21 21:49:46 +0000517
518SectionSP
Greg Claytone0d378b2011-03-24 21:19:54 +0000519SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const
Greg Clayton70e33eb2010-07-21 21:49:46 +0000520{
521 SectionSP sect_sp;
522 uint32_t num_sections = m_sections.size();
523 for (uint32_t idx = start_idx; idx < num_sections; ++idx)
524 {
525 if (m_sections[idx]->GetType() == sect_type)
526 {
527 sect_sp = m_sections[idx];
528 break;
529 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000530 else if (check_children)
531 {
532 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
533 if (sect_sp)
534 break;
535 }
Greg Clayton70e33eb2010-07-21 21:49:46 +0000536 }
537 return sect_sp;
538}
539
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
542{
543 SectionSP sect_sp;
544 const_iterator sect_iter;
545 const_iterator end = m_sections.end();
546 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
547 {
548 Section *sect = sect_iter->get();
549 if (sect->ContainsFileAddress (vm_addr))
550 {
551 // The file address is in this section. We need to make sure one of our child
552 // sections doesn't contain this address as well as obeying the depth limit
553 // that was passed in.
554 if (depth > 0)
555 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
556
557 if (sect_sp.get() == NULL && !sect->IsFake())
558 sect_sp = *sect_iter;
559 }
560 }
561 return sect_sp;
562}
563
564
565SectionSP
Greg Clayton016a95e2010-09-14 02:20:48 +0000566SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567{
568 SectionSP sect_sp;
569 const_iterator sect_iter;
570 const_iterator end = m_sections.end();
571 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
572 {
573 Section *sect = sect_iter->get();
574 if (sect->ContainsLinkedFileAddress (vm_addr))
575 {
576 sect_sp = *sect_iter;
Greg Clayton016a95e2010-09-14 02:20:48 +0000577 }
578 else if (depth > 0)
579 {
580 sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 }
582 }
583 return sect_sp;
584}
585
586bool
587SectionList::ContainsSection(user_id_t sect_id) const
588{
589 return FindSectionByID (sect_id).get() != NULL;
590}
591
592void
Greg Clayton10177aa2010-12-08 05:08:21 +0000593SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594{
Greg Claytonf6693582010-12-07 18:05:22 +0000595 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 if (show_header && !m_sections.empty())
597 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000599 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 +0000600 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000601 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 }
603
604
605 const_iterator sect_iter;
606 const_iterator end = m_sections.end();
607 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
608 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000609 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 }
611
612 if (show_header && !m_sections.empty())
613 s->IndentLess();
614
615}
616
617size_t
618SectionList::Slide (addr_t slide_amount, bool slide_children)
619{
620 size_t count = 0;
621 const_iterator pos, end = m_sections.end();
622 for (pos = m_sections.begin(); pos != end; ++pos)
623 {
624 if ((*pos)->Slide(slide_amount, slide_children))
625 ++count;
626 }
627 return count;
628}
629