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/SectionLoadList.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/ConvertEnum.h"
16 
17 #include <limits>
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 Section::Section (const ModuleSP &module_sp,
23                   ObjectFile *obj_file,
24                   user_id_t sect_id,
25                   const ConstString &name,
26                   SectionType sect_type,
27                   addr_t file_addr,
28                   addr_t byte_size,
29                   lldb::offset_t file_offset,
30                   lldb::offset_t file_size,
31                   uint32_t log2align,
32                   uint32_t flags,
33                   uint32_t target_byte_size/*=1*/) :
34     ModuleChild     (module_sp),
35     UserID          (sect_id),
36     Flags           (flags),
37     m_obj_file      (obj_file),
38     m_type          (sect_type),
39     m_parent_wp     (),
40     m_name          (name),
41     m_file_addr     (file_addr),
42     m_byte_size     (byte_size),
43     m_file_offset   (file_offset),
44     m_file_size     (file_size),
45     m_log2align     (log2align),
46     m_children      (),
47     m_fake          (false),
48     m_encrypted     (false),
49     m_thread_specific (false),
50     m_target_byte_size(target_byte_size)
51 {
52 //    printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
53 //            this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
54 }
55 
56 Section::Section (const lldb::SectionSP &parent_section_sp,
57                   const ModuleSP &module_sp,
58                   ObjectFile *obj_file,
59                   user_id_t sect_id,
60                   const ConstString &name,
61                   SectionType sect_type,
62                   addr_t file_addr,
63                   addr_t byte_size,
64                   lldb::offset_t file_offset,
65                   lldb::offset_t file_size,
66                   uint32_t log2align,
67                   uint32_t flags,
68                   uint32_t target_byte_size/*=1*/) :
69     ModuleChild     (module_sp),
70     UserID          (sect_id),
71     Flags           (flags),
72     m_obj_file      (obj_file),
73     m_type          (sect_type),
74     m_parent_wp     (),
75     m_name          (name),
76     m_file_addr     (file_addr),
77     m_byte_size     (byte_size),
78     m_file_offset   (file_offset),
79     m_file_size     (file_size),
80     m_log2align     (log2align),
81     m_children      (),
82     m_fake          (false),
83     m_encrypted     (false),
84     m_thread_specific (false),
85     m_target_byte_size(target_byte_size)
86 {
87 //    printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
88 //            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());
89     if (parent_section_sp)
90         m_parent_wp = parent_section_sp;
91 }
92 
93 Section::~Section()
94 {
95 //    printf ("Section::~Section(%p)\n", this);
96 }
97 
98 addr_t
99 Section::GetFileAddress () const
100 {
101     SectionSP parent_sp (GetParent ());
102     if (parent_sp)
103     {
104         // This section has a parent which means m_file_addr is an offset into
105         // the parent section, so the file address for this section is the file
106         // address of the parent plus the offset
107         return parent_sp->GetFileAddress() + m_file_addr;
108     }
109     // This section has no parent, so m_file_addr is the file base address
110     return m_file_addr;
111 }
112 
113 bool
114 Section::SetFileAddress (lldb::addr_t file_addr)
115 {
116     SectionSP parent_sp (GetParent ());
117     if (parent_sp)
118     {
119         if (m_file_addr >= file_addr)
120             return parent_sp->SetFileAddress (m_file_addr - file_addr);
121         return false;
122     }
123     else
124     {
125         // This section has no parent, so m_file_addr is the file base address
126         m_file_addr = file_addr;
127         return true;
128     }
129 }
130 
131 lldb::addr_t
132 Section::GetOffset () const
133 {
134     // This section has a parent which means m_file_addr is an offset.
135     SectionSP parent_sp (GetParent ());
136     if (parent_sp)
137         return m_file_addr;
138 
139     // This section has no parent, so there is no offset to be had
140     return 0;
141 }
142 
143 addr_t
144 Section::GetLoadBaseAddress (Target *target) const
145 {
146     addr_t load_base_addr = LLDB_INVALID_ADDRESS;
147     SectionSP parent_sp (GetParent ());
148     if (parent_sp)
149     {
150         load_base_addr = parent_sp->GetLoadBaseAddress (target);
151         if (load_base_addr != LLDB_INVALID_ADDRESS)
152             load_base_addr += GetOffset();
153     }
154     if (load_base_addr == LLDB_INVALID_ADDRESS)
155     {
156         load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
157     }
158     return load_base_addr;
159 }
160 
161 bool
162 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
163 {
164     const size_t num_children = m_children.GetSize();
165     if (num_children > 0)
166     {
167         for (size_t i=0; i<num_children; i++)
168         {
169             Section* child_section = m_children.GetSectionAtIndex (i).get();
170 
171             addr_t child_offset = child_section->GetOffset();
172             if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
173                 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
174         }
175     }
176     so_addr.SetOffset(offset);
177     so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
178 
179 #ifdef LLDB_CONFIGURATION_DEBUG
180     // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
181     assert(GetModule().get());
182 #endif
183     return true;
184 }
185 
186 bool
187 Section::ContainsFileAddress (addr_t vm_addr) const
188 {
189     const addr_t file_addr = GetFileAddress();
190     if (file_addr != LLDB_INVALID_ADDRESS)
191     {
192         if (file_addr <= vm_addr)
193         {
194             const addr_t offset = (vm_addr - file_addr) *  m_target_byte_size;
195             return offset < GetByteSize();
196         }
197     }
198     return false;
199 }
200 
201 int
202 Section::Compare (const Section& a, const Section& b)
203 {
204     if (&a == &b)
205         return 0;
206 
207     const ModuleSP a_module_sp = a.GetModule();
208     const ModuleSP b_module_sp = b.GetModule();
209     if (a_module_sp == b_module_sp)
210     {
211         user_id_t a_sect_uid = a.GetID();
212         user_id_t b_sect_uid = b.GetID();
213         if (a_sect_uid < b_sect_uid)
214             return -1;
215         if (a_sect_uid > b_sect_uid)
216             return 1;
217         return 0;
218     }
219     else
220     {
221         // The modules are different, just compare the module pointers
222         if (a_module_sp.get() < b_module_sp.get())
223             return -1;
224         else
225             return 1;   // We already know the modules aren't equal
226     }
227 }
228 
229 
230 void
231 Section::Dump (Stream *s, Target *target, uint32_t depth) const
232 {
233 //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
234     s->Indent();
235     s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
236     bool resolved = true;
237     addr_t addr = LLDB_INVALID_ADDRESS;
238 
239     if (GetByteSize() == 0)
240         s->Printf("%39s", "");
241     else
242     {
243         if (target)
244             addr = GetLoadBaseAddress (target);
245 
246         if (addr == LLDB_INVALID_ADDRESS)
247         {
248             if (target)
249                 resolved = false;
250             addr = GetFileAddress();
251         }
252 
253         VMRange range(addr, addr + m_byte_size);
254         range.Dump (s, 0);
255     }
256 
257     s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
258 
259     DumpName (s);
260 
261     s->EOL();
262 
263     if (depth > 0)
264         m_children.Dump(s, target, false, depth - 1);
265 }
266 
267 void
268 Section::DumpName (Stream *s) const
269 {
270     SectionSP parent_sp (GetParent ());
271     if (parent_sp)
272     {
273         parent_sp->DumpName (s);
274         s->PutChar('.');
275     }
276     else
277     {
278         // The top most section prints the module basename
279         const char * name = NULL;
280         ModuleSP module_sp (GetModule());
281         const FileSpec &file_spec = m_obj_file->GetFileSpec();
282 
283         if (m_obj_file)
284             name = file_spec.GetFilename().AsCString();
285         if ((!name || !name[0]) && module_sp)
286             name = module_sp->GetFileSpec().GetFilename().AsCString();
287         if (name && name[0])
288             s->Printf("%s.", name);
289     }
290     m_name.Dump(s);
291 }
292 
293 bool
294 Section::IsDescendant (const Section *section)
295 {
296     if (this == section)
297         return true;
298     SectionSP parent_sp (GetParent ());
299     if (parent_sp)
300         return parent_sp->IsDescendant (section);
301     return false;
302 }
303 
304 bool
305 Section::Slide (addr_t slide_amount, bool slide_children)
306 {
307     if (m_file_addr != LLDB_INVALID_ADDRESS)
308     {
309         if (slide_amount == 0)
310             return true;
311 
312         m_file_addr += slide_amount;
313 
314         if (slide_children)
315             m_children.Slide (slide_amount, slide_children);
316 
317         return true;
318     }
319     return false;
320 }
321 
322 lldb::offset_t
323 Section::GetSectionData (void *dst, lldb::offset_t dst_len, lldb::offset_t offset)
324 {
325     if (m_obj_file)
326         return m_obj_file->ReadSectionData (this,
327                                             offset,
328                                             dst,
329                                             dst_len);
330     return 0;
331 }
332 
333 lldb::offset_t
334 Section::GetSectionData (DataExtractor& section_data) const
335 {
336     if (m_obj_file)
337         return m_obj_file->ReadSectionData (this, section_data);
338     return 0;
339 }
340 
341 #pragma mark SectionList
342 
343 SectionList::SectionList () :
344     m_sections()
345 {
346 }
347 
348 
349 SectionList::~SectionList ()
350 {
351 }
352 
353 SectionList &
354 SectionList::operator = (const SectionList& rhs)
355 {
356     if (this != &rhs)
357         m_sections = rhs.m_sections;
358     return *this;
359 }
360 
361 size_t
362 SectionList::AddSection (const lldb::SectionSP& section_sp)
363 {
364     if (section_sp)
365     {
366         size_t section_index = m_sections.size();
367         m_sections.push_back(section_sp);
368         return section_index;
369     }
370 
371     return std::numeric_limits<size_t>::max ();
372 }
373 
374 // Warning, this can be slow as it's removing items from a std::vector.
375 bool
376 SectionList::DeleteSection (size_t idx)
377 {
378     if (idx < m_sections.size())
379     {
380         m_sections.erase (m_sections.begin() + idx);
381         return true;
382     }
383     return false;
384 }
385 
386 size_t
387 SectionList::FindSectionIndex (const Section* sect)
388 {
389     iterator sect_iter;
390     iterator begin = m_sections.begin();
391     iterator end = m_sections.end();
392     for (sect_iter = begin; sect_iter != end; ++sect_iter)
393     {
394         if (sect_iter->get() == sect)
395         {
396             // The secton was already in this section list
397             return std::distance (begin, sect_iter);
398         }
399     }
400     return UINT32_MAX;
401 }
402 
403 size_t
404 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
405 {
406     size_t sect_idx = FindSectionIndex (sect_sp.get());
407     if (sect_idx == UINT32_MAX)
408     {
409         sect_idx = AddSection (sect_sp);
410     }
411     return sect_idx;
412 }
413 
414 bool
415 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
416 {
417     iterator sect_iter, end = m_sections.end();
418     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
419     {
420         if ((*sect_iter)->GetID() == sect_id)
421         {
422             *sect_iter = sect_sp;
423             return true;
424         }
425         else if (depth > 0)
426         {
427             if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
428                 return true;
429         }
430     }
431     return false;
432 }
433 
434 size_t
435 SectionList::GetNumSections (uint32_t depth) const
436 {
437     size_t count = m_sections.size();
438     if (depth > 0)
439     {
440         const_iterator sect_iter, end = m_sections.end();
441         for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
442         {
443             count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
444         }
445     }
446     return count;
447 }
448 
449 SectionSP
450 SectionList::GetSectionAtIndex (size_t idx) const
451 {
452     SectionSP sect_sp;
453     if (idx < m_sections.size())
454         sect_sp = m_sections[idx];
455     return sect_sp;
456 }
457 
458 SectionSP
459 SectionList::FindSectionByName (const ConstString &section_dstr) const
460 {
461     SectionSP sect_sp;
462     // Check if we have a valid section string
463     if (section_dstr && !m_sections.empty())
464     {
465         const_iterator sect_iter;
466         const_iterator end = m_sections.end();
467         for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
468         {
469             Section *child_section = sect_iter->get();
470             if (child_section)
471             {
472                 if (child_section->GetName() == section_dstr)
473                 {
474                     sect_sp = *sect_iter;
475                 }
476                 else
477                 {
478                     sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
479                 }
480             }
481         }
482     }
483     return sect_sp;
484 }
485 
486 SectionSP
487 SectionList::FindSectionByID (user_id_t sect_id) const
488 {
489     SectionSP sect_sp;
490     if (sect_id)
491     {
492         const_iterator sect_iter;
493         const_iterator end = m_sections.end();
494         for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
495         {
496             if ((*sect_iter)->GetID() == sect_id)
497             {
498                 sect_sp = *sect_iter;
499                 break;
500             }
501             else
502             {
503                 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
504             }
505         }
506     }
507     return sect_sp;
508 }
509 
510 
511 SectionSP
512 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
513 {
514     SectionSP sect_sp;
515     size_t num_sections = m_sections.size();
516     for (size_t idx = start_idx; idx < num_sections; ++idx)
517     {
518         if (m_sections[idx]->GetType() == sect_type)
519         {
520             sect_sp = m_sections[idx];
521             break;
522         }
523         else if (check_children)
524         {
525             sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
526             if (sect_sp)
527                 break;
528         }
529     }
530     return sect_sp;
531 }
532 
533 SectionSP
534 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
535 {
536     SectionSP sect_sp;
537     const_iterator sect_iter;
538     const_iterator end = m_sections.end();
539     for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
540     {
541         Section *sect = sect_iter->get();
542         if (sect->ContainsFileAddress (vm_addr))
543         {
544             // The file address is in this section. We need to make sure one of our child
545             // sections doesn't contain this address as well as obeying the depth limit
546             // that was passed in.
547             if (depth > 0)
548                 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
549 
550             if (sect_sp.get() == NULL && !sect->IsFake())
551                 sect_sp = *sect_iter;
552         }
553     }
554     return sect_sp;
555 }
556 
557 bool
558 SectionList::ContainsSection(user_id_t sect_id) const
559 {
560     return FindSectionByID (sect_id).get() != NULL;
561 }
562 
563 void
564 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
565 {
566     bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
567     if (show_header && !m_sections.empty())
568     {
569         s->Indent();
570         s->Printf(    "SectID     Type             %s Address                             File Off.  File Size  Flags      Section Name\n", target_has_loaded_sections ? "Load" : "File");
571         s->Indent();
572         s->PutCString("---------- ---------------- ---------------------------------------  ---------- ---------- ---------- ----------------------------\n");
573     }
574 
575 
576     const_iterator sect_iter;
577     const_iterator end = m_sections.end();
578     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
579     {
580         (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
581     }
582 
583     if (show_header && !m_sections.empty())
584         s->IndentLess();
585 
586 }
587 
588 size_t
589 SectionList::Slide (addr_t slide_amount, bool slide_children)
590 {
591     size_t count = 0;
592     const_iterator pos, end = m_sections.end();
593     for (pos = m_sections.begin(); pos != end; ++pos)
594     {
595         if ((*pos)->Slide(slide_amount, slide_children))
596             ++count;
597     }
598     return count;
599 }
600