blob: a86695d46e7a807f18d84073d620a74469dabc53 [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
Greg Claytondda4f7b2010-06-30 23:03:03 +0000326size_t
327Section::ReadSectionDataFromObjectFile (const ObjectFile* objfile, off_t section_offset, void *dst, size_t dst_len) const
328{
329 if (objfile && dst && dst_len)
330 {
331 const FileSpec& file = objfile->GetFileSpec();
332
333 if (file)
334 {
335 off_t section_file_offset = GetFileOffset() + objfile->GetOffset() + section_offset;
336 return file.ReadFileContents (section_file_offset, dst, dst_len);
337 }
338 }
339 return 0;
340}
341
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342//----------------------------------------------------------------------
343// Get the section data the file on disk
344//----------------------------------------------------------------------
345size_t
346Section::ReadSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const
347{
348 if (objfile == NULL)
349 return 0;
350
351 const FileSpec& file = objfile->GetFileSpec();
352
353 if (file)
354 {
355 size_t section_file_size = GetByteSize();
356 if (section_file_size > 0)
357 {
358 off_t section_file_offset = GetFileOffset() + objfile->GetOffset();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000359 DataBufferSP section_data_sp(file.ReadFileContents(section_file_offset, section_file_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360
361 section_data.SetByteOrder(objfile->GetByteOrder());
362 section_data.SetAddressByteSize(objfile->GetAddressByteSize());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000363 return section_data.SetData (section_data_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 }
365 }
366 return 0;
367}
368
369size_t
370Section::MemoryMapSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const
371{
372 if (objfile == NULL)
373 return 0;
374
375 const FileSpec& file = objfile->GetFileSpec();
376
377 if (file)
378 {
379 size_t section_file_size = GetFileSize();
380 if (section_file_size > 0)
381 {
382 off_t section_file_offset = GetFileOffset() + objfile->GetOffset();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000383 DataBufferSP section_data_sp(file.MemoryMapFileContents(section_file_offset, section_file_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 section_data.SetByteOrder(objfile->GetByteOrder());
385 section_data.SetAddressByteSize(objfile->GetAddressByteSize());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000386 return section_data.SetData (section_data_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 }
388 }
389 return 0;
390}
391
392bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393Section::IsDescendant (const Section *section)
394{
395 if (this == section)
396 return true;
397 if (m_parent)
398 return m_parent->IsDescendant (section);
399 return false;
400}
401
402bool
403Section::Slide (addr_t slide_amount, bool slide_children)
404{
405 if (m_file_addr != LLDB_INVALID_ADDRESS)
406 {
407 if (slide_amount == 0)
408 return true;
409
410 m_file_addr += slide_amount;
411
412 if (slide_children)
413 m_children.Slide (slide_amount, slide_children);
414
415 return true;
416 }
417 return false;
418}
419
420void
421Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset)
422{
423 if (linked_section)
424 m_module = linked_section->GetModule();
425 m_linked_section = linked_section;
426 m_linked_offset = linked_offset;
427}
428
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429#pragma mark SectionList
430
431SectionList::SectionList () :
432 m_sections()
433{
434}
435
436
437SectionList::~SectionList ()
438{
439}
440
441uint32_t
442SectionList::AddSection (SectionSP& sect_sp)
443{
444 uint32_t section_index = m_sections.size();
445 m_sections.push_back(sect_sp);
446 return section_index;
447}
448
449uint32_t
450SectionList::FindSectionIndex (const Section* sect)
451{
452 iterator sect_iter;
453 iterator begin = m_sections.begin();
454 iterator end = m_sections.end();
455 for (sect_iter = begin; sect_iter != end; ++sect_iter)
456 {
457 if (sect_iter->get() == sect)
458 {
459 // The secton was already in this section list
460 return std::distance (begin, sect_iter);
461 }
462 }
463 return UINT32_MAX;
464}
465
466uint32_t
467SectionList::AddUniqueSection (SectionSP& sect_sp)
468{
469 uint32_t sect_idx = FindSectionIndex (sect_sp.get());
470 if (sect_idx == UINT32_MAX)
471 sect_idx = AddSection (sect_sp);
472 return sect_idx;
473}
474
475
476bool
477SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth)
478{
479 iterator sect_iter, end = m_sections.end();
480 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
481 {
482 if ((*sect_iter)->GetID() == sect_id)
483 {
484 *sect_iter = sect_sp;
485 return true;
486 }
487 else if (depth > 0)
488 {
489 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
490 return true;
491 }
492 }
493 return false;
494}
495
496
497size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498SectionList::GetNumSections (uint32_t depth) const
499{
500 size_t count = m_sections.size();
501 if (depth > 0)
502 {
503 const_iterator sect_iter, end = m_sections.end();
504 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
505 {
506 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
507 }
508 }
509 return count;
510}
511
512SectionSP
513SectionList::GetSectionAtIndex (uint32_t idx) const
514{
515 SectionSP sect_sp;
516 if (idx < m_sections.size())
517 sect_sp = m_sections[idx];
518 return sect_sp;
519}
520
521SectionSP
522SectionList::FindSectionByName (const ConstString &section_dstr) const
523{
524 SectionSP sect_sp;
525 // Check if we have a valid section string
526 if (section_dstr)
527 {
528 const_iterator sect_iter;
529 const_iterator end = m_sections.end();
530 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
531 {
532 if ((*sect_iter)->GetName() == section_dstr)
533 {
534 sect_sp = *sect_iter;
535 }
536 else
537 {
538 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr);
539 }
540 }
541 }
542 return sect_sp;
543}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544
545SectionSP
546SectionList::FindSectionByID (user_id_t sect_id) const
547{
548 SectionSP sect_sp;
549 if (sect_id)
550 {
551 const_iterator sect_iter;
552 const_iterator end = m_sections.end();
553 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
554 {
555 if ((*sect_iter)->GetID() == sect_id)
556 {
557 sect_sp = *sect_iter;
558 break;
559 }
560 else
561 {
562 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
563 }
564 }
565 }
566 return sect_sp;
567}
568
Greg Clayton70e33eb2010-07-21 21:49:46 +0000569
570SectionSP
Greg Clayton4ceb9982010-07-21 22:54:26 +0000571SectionList::FindSectionByType (lldb::SectionType sect_type, bool check_children, uint32_t start_idx) const
Greg Clayton70e33eb2010-07-21 21:49:46 +0000572{
573 SectionSP sect_sp;
574 uint32_t num_sections = m_sections.size();
575 for (uint32_t idx = start_idx; idx < num_sections; ++idx)
576 {
577 if (m_sections[idx]->GetType() == sect_type)
578 {
579 sect_sp = m_sections[idx];
580 break;
581 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000582 else if (check_children)
583 {
584 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
585 if (sect_sp)
586 break;
587 }
Greg Clayton70e33eb2010-07-21 21:49:46 +0000588 }
589 return sect_sp;
590}
591
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592SectionSP
593SectionList::GetSharedPointer (const Section *section, bool check_children) const
594{
595 SectionSP sect_sp;
596 if (section)
597 {
598 const_iterator sect_iter;
599 const_iterator end = m_sections.end();
600 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
601 {
602 if (sect_iter->get() == section)
603 {
604 sect_sp = *sect_iter;
605 break;
606 }
607 else if (check_children)
608 {
609 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true);
610 }
611 }
612 }
613 return sect_sp;
614}
615
616
617
618SectionSP
619SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
620{
621 SectionSP sect_sp;
622 const_iterator sect_iter;
623 const_iterator end = m_sections.end();
624 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
625 {
626 Section *sect = sect_iter->get();
627 if (sect->ContainsFileAddress (vm_addr))
628 {
629 // The file address is in this section. We need to make sure one of our child
630 // sections doesn't contain this address as well as obeying the depth limit
631 // that was passed in.
632 if (depth > 0)
633 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
634
635 if (sect_sp.get() == NULL && !sect->IsFake())
636 sect_sp = *sect_iter;
637 }
638 }
639 return sect_sp;
640}
641
642
643SectionSP
644SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr) const
645{
646 SectionSP sect_sp;
647 const_iterator sect_iter;
648 const_iterator end = m_sections.end();
649 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
650 {
651 Section *sect = sect_iter->get();
652 if (sect->ContainsLinkedFileAddress (vm_addr))
653 {
654 sect_sp = *sect_iter;
655 break;
656 }
657 }
658 return sect_sp;
659}
660
661bool
662SectionList::ContainsSection(user_id_t sect_id) const
663{
664 return FindSectionByID (sect_id).get() != NULL;
665}
666
667void
668SectionList::Dump (Stream *s, Process *process, bool show_header) const
669{
670 if (show_header && !m_sections.empty())
671 {
672 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
673 s->Indent();
674 s->PutCString( "SectionList\n");
675 s->IndentMore();
676 s->Printf("%*s", 2*(sizeof(void *) + 2), "");
677 s->Indent();
678 s->Printf("SectID %s Address File Off. File Size Flags Section Name\n", process ? "Load" : "File");
679 s->Printf("%*s", 2*(sizeof(void *) + 2), "");
680 s->Indent();
681 s->PutCString("---------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
682 }
683
684
685 const_iterator sect_iter;
686 const_iterator end = m_sections.end();
687 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
688 {
689 (*sect_iter)->Dump(s, process);
690 }
691
692 if (show_header && !m_sections.empty())
693 s->IndentLess();
694
695}
696
697size_t
698SectionList::Slide (addr_t slide_amount, bool slide_children)
699{
700 size_t count = 0;
701 const_iterator pos, end = m_sections.end();
702 for (pos = m_sections.begin(); pos != end; ++pos)
703 {
704 if ((*pos)->Slide(slide_amount, slide_children))
705 ++count;
706 }
707 return count;
708}
709