blob: 6ed47730c6f9cca9d5ce2c6dc80d5e612e9e570b [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"
13#include "lldb/Target/Process.h"
14
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 Lattner30fdc8d2010-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 Lattner30fdc8d2010-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 Lattner30fdc8d2010-06-08 16:52:24 +0000113
114addr_t
115Section::GetLoadBaseAddress (Process *process) const
116{
117 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
118 if (m_linked_section)
119 {
120 load_base_addr = m_linked_section->GetLoadBaseAddress(process) + m_linked_offset;
121 }
122 else
123 if (m_parent)
124 {
125 load_base_addr = m_parent->GetLoadBaseAddress (process);
126 if (load_base_addr != LLDB_INVALID_ADDRESS)
127 load_base_addr += GetOffset();
128 }
129 else
130 {
131 load_base_addr = process->GetSectionLoadAddress(this);
132 }
133
134 return load_base_addr;
135}
136
137bool
138Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
139{
140 const uint32_t num_children = m_children.GetSize();
141 if (num_children > 0)
142 {
143 for (uint32_t i=0; i<num_children; i++)
144 {
145 Section* child_section = m_children.GetSectionAtIndex (i).get();
146
147 addr_t child_offset = child_section->GetOffset();
148 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
149 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
150 }
151 }
152 if (m_linked_section)
153 {
154 so_addr.SetOffset(m_linked_offset + offset);
155 so_addr.SetSection(m_linked_section);
156 }
157 else
158 {
159 so_addr.SetOffset(offset);
160 so_addr.SetSection(this);
161 }
162 return true;
163}
164
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165bool
166Section::ContainsFileAddress (addr_t vm_addr) const
167{
168 const addr_t file_addr = GetFileAddress();
169 if (file_addr != LLDB_INVALID_ADDRESS)
170 {
171 if (file_addr <= vm_addr)
172 {
173 const addr_t offset = vm_addr - file_addr;
174 return offset < GetByteSize();
175 }
176 }
177 return false;
178}
179
180bool
181Section::ContainsLinkedFileAddress (addr_t vm_addr) const
182{
183 const addr_t linked_file_addr = GetLinkedFileAddress();
184 if (linked_file_addr != LLDB_INVALID_ADDRESS)
185 {
186 if (linked_file_addr <= vm_addr)
187 {
188 const addr_t offset = vm_addr - linked_file_addr;
189 return offset < GetByteSize();
190 }
191 }
192 return false;
193}
194
195int
196Section::Compare (const Section& a, const Section& b)
197{
198 if (&a == &b)
199 return 0;
200
201 const Module* a_module = a.GetModule();
202 const Module* b_module = b.GetModule();
203 if (a_module == b_module)
204 {
205 user_id_t a_sect_uid = a.GetID();
206 user_id_t b_sect_uid = b.GetID();
207 if (a_sect_uid < b_sect_uid)
208 return -1;
209 if (a_sect_uid > b_sect_uid)
210 return 1;
211 return 0;
212 }
213 else
214 {
215 // The modules are different, just compare the module pointers
216 if (a_module < b_module)
217 return -1;
218 else
219 return 1; // We already know the modules aren't equal
220 }
221}
222
223
224void
225Section::Dump(Stream *s, Process *process) const
226{
227 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
228 s->Indent();
229 s->Printf("0x%8.8x ", GetID());
230 bool resolved = true;
231 addr_t addr = LLDB_INVALID_ADDRESS;
232
233 if (GetByteSize() == 0)
234 s->Printf("%39s", "");
235 else
236 {
237 if (process)
238 addr = GetLoadBaseAddress (process);
239
240 if (addr == LLDB_INVALID_ADDRESS)
241 {
242 if (process)
243 resolved = false;
244 addr = GetFileAddress();
245 }
246
247 VMRange range(addr, addr + m_byte_size);
248 range.Dump (s, 0);
249 }
250
251 s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, GetAllFlagBits());
252
253 DumpName (s);
254
255 s->EOL();
256
257 if (m_linked_section)
258 {
259 addr = LLDB_INVALID_ADDRESS;
260
261 if (process)
262 {
263 addr = m_linked_section->GetLoadBaseAddress(process);
264 if (addr != LLDB_INVALID_ADDRESS)
265 addr += m_linked_offset;
266 }
267
268 if (addr == LLDB_INVALID_ADDRESS)
269 {
270 if (process)
271 resolved = false;
272 addr = m_linked_section->GetFileAddress() + m_linked_offset;
273 }
274
275 int indent = (sizeof(void*) + 1 + sizeof(user_id_t) + 1) * 2 + 3 + s->GetIndentLevel();
276 s->Printf("%*.*s", indent, indent, "");
277 VMRange linked_range(addr, addr + m_byte_size);
278 linked_range.Dump (s, 0);
279 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1;
280 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, "");
281
282 m_linked_section->DumpName(s);
283 s->Printf(" + 0x%llx\n", m_linked_offset);
284 }
285
286 m_children.Dump(s, process, false);
287}
288
289void
290Section::DumpName (Stream *s) const
291{
292 if (m_linked_section)
293 return m_linked_section->DumpName(s);
294 else if (m_parent == NULL)
295 {
296 // The top most section prints the module basename
297 const char *module_basename = m_module->GetFileSpec().GetFilename().AsCString();
298 if (module_basename && module_basename[0])
299 s->Printf("%s.", module_basename);
300 }
301 else
302 {
303 m_parent->DumpName (s);
304 s->PutChar('.');
305 }
306 m_name.Dump(s);
307}
308
309//----------------------------------------------------------------------
310// Get the section data from a complete contiguous copy of the
311// entire executable image.
312//----------------------------------------------------------------------
313size_t
314Section::GetSectionDataFromImage (const DataExtractor& image_data, DataExtractor& section_data) const
315{
316 size_t file_size = GetByteSize();
317 if (file_size > 0)
318 {
319 off_t file_offset = GetFileOffset();
320 if (section_data.SetData (image_data, file_offset, file_size) == file_size)
321 return true;
322 }
323 return false;
324}
325
326//----------------------------------------------------------------------
327// Get the section data the file on disk
328//----------------------------------------------------------------------
329size_t
330Section::ReadSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const
331{
332 if (objfile == NULL)
333 return 0;
334
335 const FileSpec& file = objfile->GetFileSpec();
336
337 if (file)
338 {
339 size_t section_file_size = GetByteSize();
340 if (section_file_size > 0)
341 {
342 off_t section_file_offset = GetFileOffset() + objfile->GetOffset();
343 DataBufferSP sectionDataSP(file.ReadFileContents(section_file_offset, section_file_size));
344
345 section_data.SetByteOrder(objfile->GetByteOrder());
346 section_data.SetAddressByteSize(objfile->GetAddressByteSize());
347 return section_data.SetData (sectionDataSP);
348 }
349 }
350 return 0;
351}
352
353size_t
354Section::MemoryMapSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const
355{
356 if (objfile == NULL)
357 return 0;
358
359 const FileSpec& file = objfile->GetFileSpec();
360
361 if (file)
362 {
363 size_t section_file_size = GetFileSize();
364 if (section_file_size > 0)
365 {
366 off_t section_file_offset = GetFileOffset() + objfile->GetOffset();
367 DataBufferSP sectionDataSP(file.MemoryMapFileContents(section_file_offset, section_file_size));
368 section_data.SetByteOrder(objfile->GetByteOrder());
369 section_data.SetAddressByteSize(objfile->GetAddressByteSize());
370 return section_data.SetData (sectionDataSP);
371 }
372 }
373 return 0;
374}
375
376bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377Section::IsDescendant (const Section *section)
378{
379 if (this == section)
380 return true;
381 if (m_parent)
382 return m_parent->IsDescendant (section);
383 return false;
384}
385
386bool
387Section::Slide (addr_t slide_amount, bool slide_children)
388{
389 if (m_file_addr != LLDB_INVALID_ADDRESS)
390 {
391 if (slide_amount == 0)
392 return true;
393
394 m_file_addr += slide_amount;
395
396 if (slide_children)
397 m_children.Slide (slide_amount, slide_children);
398
399 return true;
400 }
401 return false;
402}
403
404void
405Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset)
406{
407 if (linked_section)
408 m_module = linked_section->GetModule();
409 m_linked_section = linked_section;
410 m_linked_offset = linked_offset;
411}
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413#pragma mark SectionList
414
415SectionList::SectionList () :
416 m_sections()
417{
418}
419
420
421SectionList::~SectionList ()
422{
423}
424
425uint32_t
426SectionList::AddSection (SectionSP& sect_sp)
427{
428 uint32_t section_index = m_sections.size();
429 m_sections.push_back(sect_sp);
430 return section_index;
431}
432
433uint32_t
434SectionList::FindSectionIndex (const Section* sect)
435{
436 iterator sect_iter;
437 iterator begin = m_sections.begin();
438 iterator end = m_sections.end();
439 for (sect_iter = begin; sect_iter != end; ++sect_iter)
440 {
441 if (sect_iter->get() == sect)
442 {
443 // The secton was already in this section list
444 return std::distance (begin, sect_iter);
445 }
446 }
447 return UINT32_MAX;
448}
449
450uint32_t
451SectionList::AddUniqueSection (SectionSP& sect_sp)
452{
453 uint32_t sect_idx = FindSectionIndex (sect_sp.get());
454 if (sect_idx == UINT32_MAX)
455 sect_idx = AddSection (sect_sp);
456 return sect_idx;
457}
458
459
460bool
461SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth)
462{
463 iterator sect_iter, end = m_sections.end();
464 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
465 {
466 if ((*sect_iter)->GetID() == sect_id)
467 {
468 *sect_iter = sect_sp;
469 return true;
470 }
471 else if (depth > 0)
472 {
473 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
474 return true;
475 }
476 }
477 return false;
478}
479
480
481size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482SectionList::GetNumSections (uint32_t depth) const
483{
484 size_t count = m_sections.size();
485 if (depth > 0)
486 {
487 const_iterator sect_iter, end = m_sections.end();
488 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
489 {
490 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
491 }
492 }
493 return count;
494}
495
496SectionSP
497SectionList::GetSectionAtIndex (uint32_t idx) const
498{
499 SectionSP sect_sp;
500 if (idx < m_sections.size())
501 sect_sp = m_sections[idx];
502 return sect_sp;
503}
504
505SectionSP
506SectionList::FindSectionByName (const ConstString &section_dstr) const
507{
508 SectionSP sect_sp;
509 // Check if we have a valid section string
510 if (section_dstr)
511 {
512 const_iterator sect_iter;
513 const_iterator end = m_sections.end();
514 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
515 {
516 if ((*sect_iter)->GetName() == section_dstr)
517 {
518 sect_sp = *sect_iter;
519 }
520 else
521 {
522 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr);
523 }
524 }
525 }
526 return sect_sp;
527}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
529SectionSP
530SectionList::FindSectionByID (user_id_t sect_id) const
531{
532 SectionSP sect_sp;
533 if (sect_id)
534 {
535 const_iterator sect_iter;
536 const_iterator end = m_sections.end();
537 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
538 {
539 if ((*sect_iter)->GetID() == sect_id)
540 {
541 sect_sp = *sect_iter;
542 break;
543 }
544 else
545 {
546 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
547 }
548 }
549 }
550 return sect_sp;
551}
552
553SectionSP
554SectionList::GetSharedPointer (const Section *section, bool check_children) const
555{
556 SectionSP sect_sp;
557 if (section)
558 {
559 const_iterator sect_iter;
560 const_iterator end = m_sections.end();
561 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
562 {
563 if (sect_iter->get() == section)
564 {
565 sect_sp = *sect_iter;
566 break;
567 }
568 else if (check_children)
569 {
570 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true);
571 }
572 }
573 }
574 return sect_sp;
575}
576
577
578
579SectionSP
580SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
581{
582 SectionSP sect_sp;
583 const_iterator sect_iter;
584 const_iterator end = m_sections.end();
585 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
586 {
587 Section *sect = sect_iter->get();
588 if (sect->ContainsFileAddress (vm_addr))
589 {
590 // The file address is in this section. We need to make sure one of our child
591 // sections doesn't contain this address as well as obeying the depth limit
592 // that was passed in.
593 if (depth > 0)
594 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
595
596 if (sect_sp.get() == NULL && !sect->IsFake())
597 sect_sp = *sect_iter;
598 }
599 }
600 return sect_sp;
601}
602
603
604SectionSP
605SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr) const
606{
607 SectionSP sect_sp;
608 const_iterator sect_iter;
609 const_iterator end = m_sections.end();
610 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
611 {
612 Section *sect = sect_iter->get();
613 if (sect->ContainsLinkedFileAddress (vm_addr))
614 {
615 sect_sp = *sect_iter;
616 break;
617 }
618 }
619 return sect_sp;
620}
621
622bool
623SectionList::ContainsSection(user_id_t sect_id) const
624{
625 return FindSectionByID (sect_id).get() != NULL;
626}
627
628void
629SectionList::Dump (Stream *s, Process *process, bool show_header) const
630{
631 if (show_header && !m_sections.empty())
632 {
633 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
634 s->Indent();
635 s->PutCString( "SectionList\n");
636 s->IndentMore();
637 s->Printf("%*s", 2*(sizeof(void *) + 2), "");
638 s->Indent();
639 s->Printf("SectID %s Address File Off. File Size Flags Section Name\n", process ? "Load" : "File");
640 s->Printf("%*s", 2*(sizeof(void *) + 2), "");
641 s->Indent();
642 s->PutCString("---------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
643 }
644
645
646 const_iterator sect_iter;
647 const_iterator end = m_sections.end();
648 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
649 {
650 (*sect_iter)->Dump(s, process);
651 }
652
653 if (show_header && !m_sections.empty())
654 s->IndentLess();
655
656}
657
658size_t
659SectionList::Slide (addr_t slide_amount, bool slide_children)
660{
661 size_t count = 0;
662 const_iterator pos, end = m_sections.end();
663 for (pos = m_sections.begin(); pos != end; ++pos)
664 {
665 if ((*pos)->Slide(slide_amount, slide_children))
666 ++count;
667 }
668 return count;
669}
670