1 //===-- 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/Target.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 Section::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 
48 Section::~Section()
49 {
50 }
51 
52 
53 // Get a valid shared pointer to this section object
54 SectionSP
55 Section::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 
75 ConstString&
76 Section::GetName()
77 {
78     if (m_linked_section)
79         return const_cast<Section *>(m_linked_section)->GetName();
80     return m_name;
81 }
82 
83 const ConstString&
84 Section::GetName() const
85 {
86     if (m_linked_section)
87         return m_linked_section->GetName();
88     return m_name;
89 }
90 
91 addr_t
92 Section::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 
105 addr_t
106 Section::GetLinkedFileAddress () const
107 {
108     if (m_linked_section)
109         return m_linked_section->GetFileAddress() + m_linked_offset;
110     return LLDB_INVALID_ADDRESS;
111 }
112 
113 
114 addr_t
115 Section::GetLoadBaseAddress (Target *target) 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(target) + m_linked_offset;
121     }
122     else
123     if (m_parent)
124     {
125         load_base_addr = m_parent->GetLoadBaseAddress (target);
126         if (load_base_addr != LLDB_INVALID_ADDRESS)
127             load_base_addr += GetOffset();
128     }
129     else
130     {
131         load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this);
132     }
133 
134     return load_base_addr;
135 }
136 
137 bool
138 Section::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 
165 bool
166 Section::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 
180 bool
181 Section::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 
195 int
196 Section::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 
224 void
225 Section::Dump (Stream *s, Target *target) const
226 {
227 //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
228     s->Indent();
229     s->Printf("0x%8.8x %-14s ", GetID(), GetSectionTypeAsCString (m_type));
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 (target)
238             addr = GetLoadBaseAddress (target);
239 
240         if (addr == LLDB_INVALID_ADDRESS)
241         {
242             if (target)
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 (target)
262         {
263             addr = m_linked_section->GetLoadBaseAddress(target);
264             if (addr != LLDB_INVALID_ADDRESS)
265                 addr += m_linked_offset;
266         }
267 
268         if (addr == LLDB_INVALID_ADDRESS)
269         {
270             if (target)
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, target, false);
287 }
288 
289 void
290 Section::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 //----------------------------------------------------------------------
313 size_t
314 Section::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 size_t
327 Section::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 
342 //----------------------------------------------------------------------
343 // Get the section data the file on disk
344 //----------------------------------------------------------------------
345 size_t
346 Section::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();
359             DataBufferSP section_data_sp(file.ReadFileContents(section_file_offset, section_file_size));
360 
361             section_data.SetByteOrder(objfile->GetByteOrder());
362             section_data.SetAddressByteSize(objfile->GetAddressByteSize());
363             return section_data.SetData (section_data_sp);
364         }
365     }
366     return 0;
367 }
368 
369 size_t
370 Section::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();
383             DataBufferSP section_data_sp(file.MemoryMapFileContents(section_file_offset, section_file_size));
384             section_data.SetByteOrder(objfile->GetByteOrder());
385             section_data.SetAddressByteSize(objfile->GetAddressByteSize());
386             return section_data.SetData (section_data_sp);
387         }
388     }
389     return 0;
390 }
391 
392 bool
393 Section::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 
402 bool
403 Section::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 
420 void
421 Section::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 
429 #pragma mark SectionList
430 
431 SectionList::SectionList () :
432     m_sections()
433 {
434 }
435 
436 
437 SectionList::~SectionList ()
438 {
439 }
440 
441 uint32_t
442 SectionList::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 
449 uint32_t
450 SectionList::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 
466 uint32_t
467 SectionList::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 
476 bool
477 SectionList::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 
497 size_t
498 SectionList::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 
512 SectionSP
513 SectionList::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 
521 SectionSP
522 SectionList::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 }
544 
545 SectionSP
546 SectionList::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 
569 
570 SectionSP
571 SectionList::FindSectionByType (lldb::SectionType sect_type, bool check_children, uint32_t start_idx) const
572 {
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         }
582         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         }
588     }
589     return sect_sp;
590 }
591 
592 SectionSP
593 SectionList::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 
618 SectionSP
619 SectionList::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 
643 SectionSP
644 SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) 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         }
656         else if (depth > 0)
657         {
658             sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1);
659         }
660     }
661     return sect_sp;
662 }
663 
664 bool
665 SectionList::ContainsSection(user_id_t sect_id) const
666 {
667     return FindSectionByID (sect_id).get() != NULL;
668 }
669 
670 void
671 SectionList::Dump (Stream *s, Target *target, bool show_header) const
672 {
673     if (show_header && !m_sections.empty())
674     {
675 //        s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
676 //        s->Indent();
677 //        s->PutCString(  "SectionList\n");
678 //        s->IndentMore();
679 //        s->Printf("%*s", 2*(sizeof(void *) + 2), "");
680         s->Indent();
681         s->Printf("SectID     Type           %s Address                             File Off.  File Size  Flags      Section Name\n", (target && target->GetSectionLoadList().IsEmpty() == false) ? "Load" : "File");
682 //        s->Printf("%*s", 2*(sizeof(void *) + 2), "");
683         s->Indent();
684         s->PutCString("---------- -------------- ---------------------------------------  ---------- ---------- ---------- ----------------------------\n");
685     }
686 
687 
688     const_iterator sect_iter;
689     const_iterator end = m_sections.end();
690     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
691     {
692         (*sect_iter)->Dump(s, target);
693     }
694 
695     if (show_header && !m_sections.empty())
696         s->IndentLess();
697 
698 }
699 
700 size_t
701 SectionList::Slide (addr_t slide_amount, bool slide_children)
702 {
703     size_t count = 0;
704     const_iterator pos, end = m_sections.end();
705     for (pos = m_sections.begin(); pos != end; ++pos)
706     {
707         if ((*pos)->Slide(slide_amount, slide_children))
708             ++count;
709     }
710     return count;
711 }
712 
713