blob: ec198808e40536a7f25dfe96ef26b496b834d55e [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 Clayton741f3f92012-03-27 21:10:07 +000039 m_encrypted (false),
40 m_thread_specific (false),
Greg Claytone72dfb32012-02-24 01:59:29 +000041 m_linked_section_wp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042 m_linked_offset (0)
43{
Greg Claytone72dfb32012-02-24 01:59:29 +000044// 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",
45// this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
46}
47
48Section::Section (const lldb::SectionSP &parent_section_sp,
49 const ModuleSP &module_sp,
50 user_id_t sect_id,
51 const ConstString &name,
52 SectionType sect_type,
53 addr_t file_addr,
54 addr_t byte_size,
55 uint64_t file_offset,
56 uint64_t file_size,
57 uint32_t flags) :
58 ModuleChild (module_sp),
59 UserID (sect_id),
60 Flags (flags),
61 m_parent_wp (),
62 m_name (name),
63 m_type (sect_type),
64 m_file_addr (file_addr),
65 m_byte_size (byte_size),
66 m_file_offset (file_offset),
67 m_file_size (file_size),
68 m_children (),
69 m_fake (false),
Greg Clayton741f3f92012-03-27 21:10:07 +000070 m_encrypted (false),
71 m_thread_specific (false),
Greg Claytone72dfb32012-02-24 01:59:29 +000072 m_linked_section_wp(),
73 m_linked_offset (0)
74{
75// 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",
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081Section::~Section()
82{
Greg Claytone72dfb32012-02-24 01:59:29 +000083// printf ("Section::~Section(%p)\n", this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
86const ConstString&
87Section::GetName() const
88{
Greg Claytone72dfb32012-02-24 01:59:29 +000089 SectionSP linked_section_sp (m_linked_section_wp.lock());
90 if (linked_section_sp)
91 return linked_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 return m_name;
93}
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095addr_t
96Section::GetFileAddress () const
97{
Greg Claytone72dfb32012-02-24 01:59:29 +000098 SectionSP parent_sp (GetParent ());
99 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 {
101 // This section has a parent which means m_file_addr is an offset into
102 // the parent section, so the file address for this section is the file
103 // address of the parent plus the offset
Greg Claytone72dfb32012-02-24 01:59:29 +0000104 return parent_sp->GetFileAddress() + m_file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 }
106 // This section has no parent, so m_file_addr is the file base address
107 return m_file_addr;
108}
109
Greg Claytone72dfb32012-02-24 01:59:29 +0000110lldb::addr_t
111Section::GetOffset () const
112{
113 // This section has a parent which means m_file_addr is an offset.
114 SectionSP parent_sp (GetParent ());
115 if (parent_sp)
116 return m_file_addr;
117
118 // This section has no parent, so there is no offset to be had
119 return 0;
120}
121
122
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123addr_t
124Section::GetLinkedFileAddress () const
125{
Greg Claytone72dfb32012-02-24 01:59:29 +0000126 SectionSP linked_section_sp (m_linked_section_wp.lock());
127 if (linked_section_sp)
128 return linked_section_sp->GetFileAddress() + m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 return LLDB_INVALID_ADDRESS;
130}
131
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
133addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000134Section::GetLoadBaseAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135{
136 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
Greg Claytone72dfb32012-02-24 01:59:29 +0000137 SectionSP linked_section_sp (m_linked_section_wp.lock());
138 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000140 load_base_addr = linked_section_sp->GetLoadBaseAddress(target);
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000141 if (load_base_addr != LLDB_INVALID_ADDRESS)
142 load_base_addr += m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 }
144 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000146 SectionSP parent_sp (GetParent ());
147 if (parent_sp)
148 {
149 load_base_addr = parent_sp->GetLoadBaseAddress (target);
150 if (load_base_addr != LLDB_INVALID_ADDRESS)
151 load_base_addr += GetOffset();
152 }
153 else
154 {
Greg Clayton7820bd12012-07-07 01:24:12 +0000155 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
Greg Claytone72dfb32012-02-24 01:59:29 +0000156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 }
158
159 return load_base_addr;
160}
161
162bool
163Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
164{
165 const uint32_t num_children = m_children.GetSize();
166 if (num_children > 0)
167 {
168 for (uint32_t i=0; i<num_children; i++)
169 {
170 Section* child_section = m_children.GetSectionAtIndex (i).get();
171
172 addr_t child_offset = child_section->GetOffset();
173 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
174 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
175 }
176 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000177 SectionSP linked_section_sp (m_linked_section_wp.lock());
178 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 {
180 so_addr.SetOffset(m_linked_offset + offset);
Greg Claytone72dfb32012-02-24 01:59:29 +0000181 so_addr.SetSection(linked_section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 }
183 else
184 {
185 so_addr.SetOffset(offset);
Greg Claytone72dfb32012-02-24 01:59:29 +0000186 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 }
188 return true;
189}
190
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191bool
192Section::ContainsFileAddress (addr_t vm_addr) const
193{
194 const addr_t file_addr = GetFileAddress();
195 if (file_addr != LLDB_INVALID_ADDRESS)
196 {
197 if (file_addr <= vm_addr)
198 {
199 const addr_t offset = vm_addr - file_addr;
200 return offset < GetByteSize();
201 }
202 }
203 return false;
204}
205
206bool
207Section::ContainsLinkedFileAddress (addr_t vm_addr) const
208{
209 const addr_t linked_file_addr = GetLinkedFileAddress();
210 if (linked_file_addr != LLDB_INVALID_ADDRESS)
211 {
212 if (linked_file_addr <= vm_addr)
213 {
214 const addr_t offset = vm_addr - linked_file_addr;
215 return offset < GetByteSize();
216 }
217 }
218 return false;
219}
220
221int
222Section::Compare (const Section& a, const Section& b)
223{
224 if (&a == &b)
225 return 0;
226
Greg Claytone72dfb32012-02-24 01:59:29 +0000227 const ModuleSP a_module_sp = a.GetModule();
228 const ModuleSP b_module_sp = b.GetModule();
229 if (a_module_sp == b_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230 {
231 user_id_t a_sect_uid = a.GetID();
232 user_id_t b_sect_uid = b.GetID();
233 if (a_sect_uid < b_sect_uid)
234 return -1;
235 if (a_sect_uid > b_sect_uid)
236 return 1;
237 return 0;
238 }
239 else
240 {
241 // The modules are different, just compare the module pointers
Greg Claytone72dfb32012-02-24 01:59:29 +0000242 if (a_module_sp.get() < b_module_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 return -1;
244 else
245 return 1; // We already know the modules aren't equal
246 }
247}
248
249
250void
Greg Clayton10177aa2010-12-08 05:08:21 +0000251Section::Dump (Stream *s, Target *target, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252{
Greg Clayton89411422010-10-08 00:21:05 +0000253// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000255 s->Printf("0x%8.8llx %-16s ", GetID(), GetSectionTypeAsCString (m_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 bool resolved = true;
257 addr_t addr = LLDB_INVALID_ADDRESS;
258
Greg Claytone72dfb32012-02-24 01:59:29 +0000259 SectionSP linked_section_sp (m_linked_section_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260 if (GetByteSize() == 0)
261 s->Printf("%39s", "");
262 else
263 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000264 if (target && linked_section_sp.get() == NULL)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000265 addr = GetLoadBaseAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266
267 if (addr == LLDB_INVALID_ADDRESS)
268 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000269 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 resolved = false;
271 addr = GetFileAddress();
272 }
273
274 VMRange range(addr, addr + m_byte_size);
275 range.Dump (s, 0);
276 }
277
Greg Clayton73b472d2010-10-27 03:32:59 +0000278 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 +0000279
280 DumpName (s);
281
282 s->EOL();
283
Greg Claytone72dfb32012-02-24 01:59:29 +0000284 if (linked_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 {
286 addr = LLDB_INVALID_ADDRESS;
Greg Claytonf6693582010-12-07 18:05:22 +0000287 resolved = true;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000288 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000290 addr = linked_section_sp->GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 if (addr != LLDB_INVALID_ADDRESS)
292 addr += m_linked_offset;
293 }
294
295 if (addr == LLDB_INVALID_ADDRESS)
296 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000297 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 resolved = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000299 addr = linked_section_sp->GetFileAddress() + m_linked_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 }
301
Greg Clayton65e364e2010-12-07 07:37:38 +0000302 int indent = 26 + s->GetIndentLevel();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 s->Printf("%*.*s", indent, indent, "");
304 VMRange linked_range(addr, addr + m_byte_size);
305 linked_range.Dump (s, 0);
306 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1;
307 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, "");
308
Greg Claytone72dfb32012-02-24 01:59:29 +0000309 linked_section_sp->DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 s->Printf(" + 0x%llx\n", m_linked_offset);
311 }
312
Greg Clayton10177aa2010-12-08 05:08:21 +0000313 if (depth > 0)
314 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
317void
318Section::DumpName (Stream *s) const
319{
Greg Claytone72dfb32012-02-24 01:59:29 +0000320 SectionSP parent_sp (GetParent ());
321 if (parent_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000323 parent_sp->DumpName (s);
324 s->PutChar('.');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 }
326 else
327 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000328 // The top most section prints the module basename
329 ModuleSP module_sp (GetModule());
330 if (module_sp)
331 {
332 const char *module_basename = module_sp->GetFileSpec().GetFilename().AsCString();
333 if (module_basename && module_basename[0])
334 s->Printf("%s.", module_basename);
335 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 }
337 m_name.Dump(s);
338}
339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341Section::IsDescendant (const Section *section)
342{
343 if (this == section)
344 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +0000345 SectionSP parent_sp (GetParent ());
346 if (parent_sp)
347 return parent_sp->IsDescendant (section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 return false;
349}
350
351bool
352Section::Slide (addr_t slide_amount, bool slide_children)
353{
354 if (m_file_addr != LLDB_INVALID_ADDRESS)
355 {
356 if (slide_amount == 0)
357 return true;
358
359 m_file_addr += slide_amount;
360
361 if (slide_children)
362 m_children.Slide (slide_amount, slide_children);
363
364 return true;
365 }
366 return false;
367}
368
369void
Greg Claytone72dfb32012-02-24 01:59:29 +0000370Section::SetLinkedLocation (const lldb::SectionSP &linked_section_sp, uint64_t linked_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371{
Greg Claytone72dfb32012-02-24 01:59:29 +0000372 if (linked_section_sp)
373 m_module_wp = linked_section_sp->GetModule();
374 m_linked_section_wp = linked_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 m_linked_offset = linked_offset;
376}
377
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378#pragma mark SectionList
379
380SectionList::SectionList () :
381 m_sections()
Sean Callanan56775362012-06-08 02:16:08 +0000382#ifdef LLDB_CONFIGURATION_DEBUG
383 , m_finalized(false)
384#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385{
386}
387
388
389SectionList::~SectionList ()
390{
391}
392
393uint32_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000394SectionList::AddSection (const lldb::SectionSP& section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395{
Greg Claytone72dfb32012-02-24 01:59:29 +0000396 assert (section_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 uint32_t section_index = m_sections.size();
Greg Claytone72dfb32012-02-24 01:59:29 +0000398 m_sections.push_back(section_sp);
Sean Callanan56775362012-06-08 02:16:08 +0000399 InvalidateRangeCache();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 return section_index;
401}
402
403uint32_t
404SectionList::FindSectionIndex (const Section* sect)
405{
406 iterator sect_iter;
407 iterator begin = m_sections.begin();
408 iterator end = m_sections.end();
409 for (sect_iter = begin; sect_iter != end; ++sect_iter)
410 {
411 if (sect_iter->get() == sect)
412 {
413 // The secton was already in this section list
414 return std::distance (begin, sect_iter);
415 }
416 }
417 return UINT32_MAX;
418}
419
420uint32_t
Greg Claytone72dfb32012-02-24 01:59:29 +0000421SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422{
423 uint32_t sect_idx = FindSectionIndex (sect_sp.get());
424 if (sect_idx == UINT32_MAX)
425 sect_idx = AddSection (sect_sp);
426 return sect_idx;
427}
428
429
430bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000431SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432{
433 iterator sect_iter, end = m_sections.end();
434 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
435 {
436 if ((*sect_iter)->GetID() == sect_id)
437 {
438 *sect_iter = sect_sp;
Sean Callanan56775362012-06-08 02:16:08 +0000439 InvalidateRangeCache();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 return true;
441 }
442 else if (depth > 0)
443 {
444 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
445 return true;
446 }
447 }
448 return false;
449}
450
451
452size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453SectionList::GetNumSections (uint32_t depth) const
454{
455 size_t count = m_sections.size();
456 if (depth > 0)
457 {
458 const_iterator sect_iter, end = m_sections.end();
459 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
460 {
461 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
462 }
463 }
464 return count;
465}
466
467SectionSP
468SectionList::GetSectionAtIndex (uint32_t idx) const
469{
470 SectionSP sect_sp;
471 if (idx < m_sections.size())
472 sect_sp = m_sections[idx];
473 return sect_sp;
474}
475
476SectionSP
477SectionList::FindSectionByName (const ConstString &section_dstr) const
478{
479 SectionSP sect_sp;
480 // Check if we have a valid section string
Greg Claytone72dfb32012-02-24 01:59:29 +0000481 if (section_dstr && !m_sections.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 {
483 const_iterator sect_iter;
484 const_iterator end = m_sections.end();
485 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
486 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000487 Section *child_section = sect_iter->get();
488 assert (child_section);
489 if (child_section->GetName() == section_dstr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 {
491 sect_sp = *sect_iter;
492 }
493 else
494 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000495 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 }
497 }
498 }
499 return sect_sp;
500}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501
502SectionSP
503SectionList::FindSectionByID (user_id_t sect_id) const
504{
505 SectionSP sect_sp;
506 if (sect_id)
507 {
508 const_iterator sect_iter;
509 const_iterator end = m_sections.end();
510 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
511 {
512 if ((*sect_iter)->GetID() == sect_id)
513 {
514 sect_sp = *sect_iter;
515 break;
516 }
517 else
518 {
519 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
520 }
521 }
522 }
523 return sect_sp;
524}
525
Greg Clayton70e33eb2010-07-21 21:49:46 +0000526
527SectionSP
Greg Claytone0d378b2011-03-24 21:19:54 +0000528SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const
Greg Clayton70e33eb2010-07-21 21:49:46 +0000529{
530 SectionSP sect_sp;
531 uint32_t num_sections = m_sections.size();
532 for (uint32_t idx = start_idx; idx < num_sections; ++idx)
533 {
534 if (m_sections[idx]->GetType() == sect_type)
535 {
536 sect_sp = m_sections[idx];
537 break;
538 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000539 else if (check_children)
540 {
541 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
542 if (sect_sp)
543 break;
544 }
Greg Clayton70e33eb2010-07-21 21:49:46 +0000545 }
546 return sect_sp;
547}
548
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549SectionSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
551{
552 SectionSP sect_sp;
553 const_iterator sect_iter;
554 const_iterator end = m_sections.end();
555 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
556 {
557 Section *sect = sect_iter->get();
558 if (sect->ContainsFileAddress (vm_addr))
559 {
560 // The file address is in this section. We need to make sure one of our child
561 // sections doesn't contain this address as well as obeying the depth limit
562 // that was passed in.
563 if (depth > 0)
564 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
565
566 if (sect_sp.get() == NULL && !sect->IsFake())
567 sect_sp = *sect_iter;
568 }
569 }
570 return sect_sp;
571}
572
Sean Callanan56775362012-06-08 02:16:08 +0000573void
574SectionList::BuildRangeCache() const
575{
576 m_range_cache.Clear();
577
578 for (collection::size_type idx = 0, last_idx = m_sections.size();
579 idx < last_idx;
580 ++idx)
581 {
582 Section *sect = m_sections[idx].get();
583
584 addr_t linked_file_address = sect->GetLinkedFileAddress();
585
586 if (linked_file_address != LLDB_INVALID_ADDRESS)
587 m_range_cache.Append(SectionRangeCache::Entry(linked_file_address, sect->GetByteSize(), idx));
588 }
589
590 m_range_cache.Sort();
591
592#ifdef LLDB_CONFIGURATION_DEBUG
593 m_finalized = true;
594#endif
595}
596
597void
598SectionList::InvalidateRangeCache() const
599{
600#ifdef LLDB_CONFIGURATION_DEBUG
601 assert(!m_finalized);
602#endif
603 m_range_cache.Clear();
604}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605
606SectionSP
Greg Clayton016a95e2010-09-14 02:20:48 +0000607SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608{
Sean Callanan56775362012-06-08 02:16:08 +0000609 //if (m_range_cache.IsEmpty())
610 // BuildRangeCache();
611#ifdef LLDB_CONFIGURATION_DEBUG
612 assert(m_finalized);
613#endif
614
615 SectionRangeCache::Entry *entry = m_range_cache.FindEntryThatContains(vm_addr);
616
617 if (entry)
618 return m_sections[entry->data];
619
620 if (depth == 0)
621 return SectionSP();
622
623 for (const_iterator si = m_sections.begin(), se = m_sections.end();
624 si != se;
625 ++si)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 {
Sean Callanan56775362012-06-08 02:16:08 +0000627 Section *sect = si->get();
628
629 SectionSP sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress(vm_addr, depth - 1);
630
631 if (sect_sp)
632 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
Sean Callanan56775362012-06-08 02:16:08 +0000634
635 return SectionSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636}
637
638bool
639SectionList::ContainsSection(user_id_t sect_id) const
640{
641 return FindSectionByID (sect_id).get() != NULL;
642}
643
644void
Greg Clayton10177aa2010-12-08 05:08:21 +0000645SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646{
Greg Claytonf6693582010-12-07 18:05:22 +0000647 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 if (show_header && !m_sections.empty())
649 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000651 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 +0000652 s->Indent();
Greg Clayton44435ed2012-01-12 05:25:17 +0000653 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 }
655
656
657 const_iterator sect_iter;
658 const_iterator end = m_sections.end();
659 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
660 {
Greg Clayton10177aa2010-12-08 05:08:21 +0000661 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 }
663
664 if (show_header && !m_sections.empty())
665 s->IndentLess();
666
667}
668
669size_t
670SectionList::Slide (addr_t slide_amount, bool slide_children)
671{
672 size_t count = 0;
673 const_iterator pos, end = m_sections.end();
674 for (pos = m_sections.begin(); pos != end; ++pos)
675 {
676 if ((*pos)->Slide(slide_amount, slide_children))
677 ++count;
678 }
Sean Callanan56775362012-06-08 02:16:08 +0000679 InvalidateRangeCache();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 return count;
681}
682
Sean Callanan56775362012-06-08 02:16:08 +0000683void
684SectionList::Finalize ()
685{
686 BuildRangeCache();
687
688 for (const_iterator si = m_sections.begin(), se = m_sections.end();
689 si != se;
690 ++si)
691 {
692 Section *sect = si->get();
693
694 sect->GetChildren().Finalize();
695 }
696}
697