blob: dd2556a49d305a5cf2bc72c88e5aac0b0028863e [file] [log] [blame]
Chris Lattner24943d22010-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 Claytoneea26402010-09-14 23:36:40 +000013#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000014
15using namespace lldb;
16using namespace lldb_private;
17
18Section::Section
19(
20 Section *parent,
21 Module* module,
22 user_id_t sect_id,
23 const ConstString &name,
24 SectionType sect_type,
25 addr_t file_addr,
26 addr_t byte_size,
27 uint64_t file_offset,
28 uint64_t file_size,
29 uint32_t flags
30) :
31 ModuleChild (module),
32 UserID (sect_id),
33 Flags (flags),
34 m_parent (parent),
35 m_name (name),
36 m_type (sect_type),
37 m_file_addr (file_addr),
38 m_byte_size (byte_size),
39 m_file_offset (file_offset),
40 m_file_size (file_size),
41 m_children (),
42 m_fake (false),
43 m_linked_section(NULL),
44 m_linked_offset (0)
45{
46}
47
Chris Lattner24943d22010-06-08 16:52:24 +000048Section::~Section()
49{
50}
51
52
53// Get a valid shared pointer to this section object
54SectionSP
55Section::GetSharedPointer() const
56{
57 SectionSP this_sp;
58 if (m_parent)
59 this_sp = m_parent->GetChildren().GetSharedPointer (this, false);
60 else
61 {
62 ObjectFile *objfile = m_module->GetObjectFile();
63 if (objfile)
64 {
65 SectionList *section_list = objfile->GetSectionList();
66 if (section_list)
67 this_sp = section_list->GetSharedPointer (this, false);
68 }
69 }
70 return this_sp;
71}
72
73
74
75ConstString&
76Section::GetName()
77{
78 if (m_linked_section)
79 return const_cast<Section *>(m_linked_section)->GetName();
80 return m_name;
81}
82
83const ConstString&
84Section::GetName() const
85{
86 if (m_linked_section)
87 return m_linked_section->GetName();
88 return m_name;
89}
90
Chris Lattner24943d22010-06-08 16:52:24 +000091addr_t
92Section::GetFileAddress () const
93{
94 if (m_parent)
95 {
96 // This section has a parent which means m_file_addr is an offset into
97 // the parent section, so the file address for this section is the file
98 // address of the parent plus the offset
99 return m_parent->GetFileAddress() + m_file_addr;
100 }
101 // This section has no parent, so m_file_addr is the file base address
102 return m_file_addr;
103}
104
105addr_t
106Section::GetLinkedFileAddress () const
107{
108 if (m_linked_section)
109 return m_linked_section->GetFileAddress() + m_linked_offset;
110 return LLDB_INVALID_ADDRESS;
111}
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113
114addr_t
Greg Claytoneea26402010-09-14 23:36:40 +0000115Section::GetLoadBaseAddress (Target *target) const
Chris Lattner24943d22010-06-08 16:52:24 +0000116{
117 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
118 if (m_linked_section)
119 {
Greg Clayton37f962e2011-08-22 02:49:39 +0000120 load_base_addr = m_linked_section->GetLoadBaseAddress(target);
121 if (load_base_addr != LLDB_INVALID_ADDRESS)
122 load_base_addr += m_linked_offset;
Chris Lattner24943d22010-06-08 16:52:24 +0000123 }
124 else
125 if (m_parent)
126 {
Greg Claytoneea26402010-09-14 23:36:40 +0000127 load_base_addr = m_parent->GetLoadBaseAddress (target);
Chris Lattner24943d22010-06-08 16:52:24 +0000128 if (load_base_addr != LLDB_INVALID_ADDRESS)
129 load_base_addr += GetOffset();
130 }
131 else
132 {
Greg Claytoneea26402010-09-14 23:36:40 +0000133 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this);
Chris Lattner24943d22010-06-08 16:52:24 +0000134 }
135
136 return load_base_addr;
137}
138
139bool
140Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
141{
142 const uint32_t num_children = m_children.GetSize();
143 if (num_children > 0)
144 {
145 for (uint32_t i=0; i<num_children; i++)
146 {
147 Section* child_section = m_children.GetSectionAtIndex (i).get();
148
149 addr_t child_offset = child_section->GetOffset();
150 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
151 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
152 }
153 }
154 if (m_linked_section)
155 {
156 so_addr.SetOffset(m_linked_offset + offset);
157 so_addr.SetSection(m_linked_section);
158 }
159 else
160 {
161 so_addr.SetOffset(offset);
162 so_addr.SetSection(this);
163 }
164 return true;
165}
166
Chris Lattner24943d22010-06-08 16:52:24 +0000167bool
168Section::ContainsFileAddress (addr_t vm_addr) const
169{
170 const addr_t file_addr = GetFileAddress();
171 if (file_addr != LLDB_INVALID_ADDRESS)
172 {
173 if (file_addr <= vm_addr)
174 {
175 const addr_t offset = vm_addr - file_addr;
176 return offset < GetByteSize();
177 }
178 }
179 return false;
180}
181
182bool
183Section::ContainsLinkedFileAddress (addr_t vm_addr) const
184{
185 const addr_t linked_file_addr = GetLinkedFileAddress();
186 if (linked_file_addr != LLDB_INVALID_ADDRESS)
187 {
188 if (linked_file_addr <= vm_addr)
189 {
190 const addr_t offset = vm_addr - linked_file_addr;
191 return offset < GetByteSize();
192 }
193 }
194 return false;
195}
196
197int
198Section::Compare (const Section& a, const Section& b)
199{
200 if (&a == &b)
201 return 0;
202
203 const Module* a_module = a.GetModule();
204 const Module* b_module = b.GetModule();
205 if (a_module == b_module)
206 {
207 user_id_t a_sect_uid = a.GetID();
208 user_id_t b_sect_uid = b.GetID();
209 if (a_sect_uid < b_sect_uid)
210 return -1;
211 if (a_sect_uid > b_sect_uid)
212 return 1;
213 return 0;
214 }
215 else
216 {
217 // The modules are different, just compare the module pointers
218 if (a_module < b_module)
219 return -1;
220 else
221 return 1; // We already know the modules aren't equal
222 }
223}
224
225
226void
Greg Clayton58e844b2010-12-08 05:08:21 +0000227Section::Dump (Stream *s, Target *target, uint32_t depth) const
Chris Lattner24943d22010-06-08 16:52:24 +0000228{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000229// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner24943d22010-06-08 16:52:24 +0000230 s->Indent();
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000231 s->Printf("0x%8.8llx %-16s ", GetID(), GetSectionTypeAsCString (m_type));
Chris Lattner24943d22010-06-08 16:52:24 +0000232 bool resolved = true;
233 addr_t addr = LLDB_INVALID_ADDRESS;
234
235 if (GetByteSize() == 0)
236 s->Printf("%39s", "");
237 else
238 {
Greg Clayton42c8fdc2010-12-07 18:05:22 +0000239 if (target && m_linked_section == NULL)
Greg Claytoneea26402010-09-14 23:36:40 +0000240 addr = GetLoadBaseAddress (target);
Chris Lattner24943d22010-06-08 16:52:24 +0000241
242 if (addr == LLDB_INVALID_ADDRESS)
243 {
Greg Claytoneea26402010-09-14 23:36:40 +0000244 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000245 resolved = false;
246 addr = GetFileAddress();
247 }
248
249 VMRange range(addr, addr + m_byte_size);
250 range.Dump (s, 0);
251 }
252
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000253 s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
Chris Lattner24943d22010-06-08 16:52:24 +0000254
255 DumpName (s);
256
257 s->EOL();
258
259 if (m_linked_section)
260 {
261 addr = LLDB_INVALID_ADDRESS;
Greg Clayton42c8fdc2010-12-07 18:05:22 +0000262 resolved = true;
Greg Claytoneea26402010-09-14 23:36:40 +0000263 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Greg Claytoneea26402010-09-14 23:36:40 +0000265 addr = m_linked_section->GetLoadBaseAddress(target);
Chris Lattner24943d22010-06-08 16:52:24 +0000266 if (addr != LLDB_INVALID_ADDRESS)
267 addr += m_linked_offset;
268 }
269
270 if (addr == LLDB_INVALID_ADDRESS)
271 {
Greg Claytoneea26402010-09-14 23:36:40 +0000272 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000273 resolved = false;
274 addr = m_linked_section->GetFileAddress() + m_linked_offset;
275 }
276
Greg Clayton9d972a42010-12-07 07:37:38 +0000277 int indent = 26 + s->GetIndentLevel();
Chris Lattner24943d22010-06-08 16:52:24 +0000278 s->Printf("%*.*s", indent, indent, "");
279 VMRange linked_range(addr, addr + m_byte_size);
280 linked_range.Dump (s, 0);
281 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1;
282 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, "");
283
284 m_linked_section->DumpName(s);
285 s->Printf(" + 0x%llx\n", m_linked_offset);
286 }
287
Greg Clayton58e844b2010-12-08 05:08:21 +0000288 if (depth > 0)
289 m_children.Dump(s, target, false, depth - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000290}
291
292void
293Section::DumpName (Stream *s) const
294{
Greg Clayton9d972a42010-12-07 07:37:38 +0000295 if (m_parent == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000296 {
297 // The top most section prints the module basename
298 const char *module_basename = m_module->GetFileSpec().GetFilename().AsCString();
299 if (module_basename && module_basename[0])
300 s->Printf("%s.", module_basename);
301 }
302 else
303 {
304 m_parent->DumpName (s);
305 s->PutChar('.');
306 }
307 m_name.Dump(s);
308}
309
Chris Lattner24943d22010-06-08 16:52:24 +0000310bool
Chris Lattner24943d22010-06-08 16:52:24 +0000311Section::IsDescendant (const Section *section)
312{
313 if (this == section)
314 return true;
315 if (m_parent)
316 return m_parent->IsDescendant (section);
317 return false;
318}
319
320bool
321Section::Slide (addr_t slide_amount, bool slide_children)
322{
323 if (m_file_addr != LLDB_INVALID_ADDRESS)
324 {
325 if (slide_amount == 0)
326 return true;
327
328 m_file_addr += slide_amount;
329
330 if (slide_children)
331 m_children.Slide (slide_amount, slide_children);
332
333 return true;
334 }
335 return false;
336}
337
338void
339Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset)
340{
341 if (linked_section)
342 m_module = linked_section->GetModule();
343 m_linked_section = linked_section;
344 m_linked_offset = linked_offset;
345}
346
Chris Lattner24943d22010-06-08 16:52:24 +0000347#pragma mark SectionList
348
349SectionList::SectionList () :
350 m_sections()
351{
352}
353
354
355SectionList::~SectionList ()
356{
357}
358
359uint32_t
360SectionList::AddSection (SectionSP& sect_sp)
361{
362 uint32_t section_index = m_sections.size();
363 m_sections.push_back(sect_sp);
364 return section_index;
365}
366
367uint32_t
368SectionList::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
384uint32_t
385SectionList::AddUniqueSection (SectionSP& sect_sp)
386{
387 uint32_t sect_idx = FindSectionIndex (sect_sp.get());
388 if (sect_idx == UINT32_MAX)
389 sect_idx = AddSection (sect_sp);
390 return sect_idx;
391}
392
393
394bool
395SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth)
396{
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
414
415size_t
Chris Lattner24943d22010-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
431SectionList::GetSectionAtIndex (uint32_t idx) const
432{
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
444 if (section_dstr)
445 {
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 {
450 if ((*sect_iter)->GetName() == section_dstr)
451 {
452 sect_sp = *sect_iter;
453 }
454 else
455 {
456 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr);
457 }
458 }
459 }
460 return sect_sp;
461}
Chris Lattner24943d22010-06-08 16:52:24 +0000462
463SectionSP
464SectionList::FindSectionByID (user_id_t sect_id) const
465{
466 SectionSP sect_sp;
467 if (sect_id)
468 {
469 const_iterator sect_iter;
470 const_iterator end = m_sections.end();
471 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
472 {
473 if ((*sect_iter)->GetID() == sect_id)
474 {
475 sect_sp = *sect_iter;
476 break;
477 }
478 else
479 {
480 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
481 }
482 }
483 }
484 return sect_sp;
485}
486
Greg Clayton15be8ed2010-07-21 21:49:46 +0000487
488SectionSP
Greg Claytonb3448432011-03-24 21:19:54 +0000489SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const
Greg Clayton15be8ed2010-07-21 21:49:46 +0000490{
491 SectionSP sect_sp;
492 uint32_t num_sections = m_sections.size();
493 for (uint32_t idx = start_idx; idx < num_sections; ++idx)
494 {
495 if (m_sections[idx]->GetType() == sect_type)
496 {
497 sect_sp = m_sections[idx];
498 break;
499 }
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000500 else if (check_children)
501 {
502 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
503 if (sect_sp)
504 break;
505 }
Greg Clayton15be8ed2010-07-21 21:49:46 +0000506 }
507 return sect_sp;
508}
509
Chris Lattner24943d22010-06-08 16:52:24 +0000510SectionSP
511SectionList::GetSharedPointer (const Section *section, bool check_children) const
512{
513 SectionSP sect_sp;
514 if (section)
515 {
516 const_iterator sect_iter;
517 const_iterator end = m_sections.end();
518 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
519 {
520 if (sect_iter->get() == section)
521 {
522 sect_sp = *sect_iter;
523 break;
524 }
525 else if (check_children)
526 {
527 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true);
528 }
529 }
530 }
531 return sect_sp;
532}
533
534
535
536SectionSP
537SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
538{
539 SectionSP sect_sp;
540 const_iterator sect_iter;
541 const_iterator end = m_sections.end();
542 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
543 {
544 Section *sect = sect_iter->get();
545 if (sect->ContainsFileAddress (vm_addr))
546 {
547 // The file address is in this section. We need to make sure one of our child
548 // sections doesn't contain this address as well as obeying the depth limit
549 // that was passed in.
550 if (depth > 0)
551 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
552
553 if (sect_sp.get() == NULL && !sect->IsFake())
554 sect_sp = *sect_iter;
555 }
556 }
557 return sect_sp;
558}
559
560
561SectionSP
Greg Clayton178710c2010-09-14 02:20:48 +0000562SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const
Chris Lattner24943d22010-06-08 16:52:24 +0000563{
564 SectionSP sect_sp;
565 const_iterator sect_iter;
566 const_iterator end = m_sections.end();
567 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
568 {
569 Section *sect = sect_iter->get();
570 if (sect->ContainsLinkedFileAddress (vm_addr))
571 {
572 sect_sp = *sect_iter;
Greg Clayton178710c2010-09-14 02:20:48 +0000573 }
574 else if (depth > 0)
575 {
576 sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000577 }
578 }
579 return sect_sp;
580}
581
582bool
583SectionList::ContainsSection(user_id_t sect_id) const
584{
585 return FindSectionByID (sect_id).get() != NULL;
586}
587
588void
Greg Clayton58e844b2010-12-08 05:08:21 +0000589SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
Chris Lattner24943d22010-06-08 16:52:24 +0000590{
Greg Clayton42c8fdc2010-12-07 18:05:22 +0000591 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
Chris Lattner24943d22010-06-08 16:52:24 +0000592 if (show_header && !m_sections.empty())
593 {
Chris Lattner24943d22010-06-08 16:52:24 +0000594 s->Indent();
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000595 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
Chris Lattner24943d22010-06-08 16:52:24 +0000596 s->Indent();
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000597 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000598 }
599
600
601 const_iterator sect_iter;
602 const_iterator end = m_sections.end();
603 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
604 {
Greg Clayton58e844b2010-12-08 05:08:21 +0000605 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
Chris Lattner24943d22010-06-08 16:52:24 +0000606 }
607
608 if (show_header && !m_sections.empty())
609 s->IndentLess();
610
611}
612
613size_t
614SectionList::Slide (addr_t slide_amount, bool slide_children)
615{
616 size_t count = 0;
617 const_iterator pos, end = m_sections.end();
618 for (pos = m_sections.begin(); pos != end; ++pos)
619 {
620 if ((*pos)->Slide(slide_amount, slide_children))
621 ++count;
622 }
623 return count;
624}
625