]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/Section.cpp
Update LLDB snapshot to upstream r241361
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / Section.cpp
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 #pragma mark SectionList
323
324 SectionList::SectionList () :
325     m_sections()
326 {
327 }
328
329
330 SectionList::~SectionList ()
331 {
332 }
333
334 SectionList &
335 SectionList::operator = (const SectionList& rhs)
336 {
337     if (this != &rhs)
338         m_sections = rhs.m_sections;
339     return *this;
340 }
341
342 size_t
343 SectionList::AddSection (const lldb::SectionSP& section_sp)
344 {
345     if (section_sp)
346     {
347         size_t section_index = m_sections.size();
348         m_sections.push_back(section_sp);
349         return section_index;
350     }
351
352     return std::numeric_limits<size_t>::max ();
353 }
354
355 // Warning, this can be slow as it's removing items from a std::vector.
356 bool
357 SectionList::DeleteSection (size_t idx)
358 {
359     if (idx < m_sections.size())
360     {
361         m_sections.erase (m_sections.begin() + idx);
362         return true; 
363     }
364     return false;
365 }
366
367 size_t
368 SectionList::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
384 size_t
385 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
386 {
387     size_t sect_idx = FindSectionIndex (sect_sp.get());
388     if (sect_idx == UINT32_MAX)
389     {
390         sect_idx = AddSection (sect_sp);
391     }
392     return sect_idx;
393 }
394
395 bool
396 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
397 {
398     iterator sect_iter, end = m_sections.end();
399     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
400     {
401         if ((*sect_iter)->GetID() == sect_id)
402         {
403             *sect_iter = sect_sp;
404             return true;
405         }
406         else if (depth > 0)
407         {
408             if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
409                 return true;
410         }
411     }
412     return false;
413 }
414
415 size_t
416 SectionList::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
430 SectionSP
431 SectionList::GetSectionAtIndex (size_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
439 SectionSP
440 SectionList::FindSectionByName (const ConstString &section_dstr) const
441 {
442     SectionSP sect_sp;
443     // Check if we have a valid section string
444     if (section_dstr && !m_sections.empty())
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             Section *child_section = sect_iter->get();
451             if (child_section)
452             {
453                 if (child_section->GetName() == section_dstr)
454                 {
455                     sect_sp = *sect_iter;
456                 }
457                 else
458                 {
459                     sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
460                 }
461             }
462         }
463     }
464     return sect_sp;
465 }
466
467 SectionSP
468 SectionList::FindSectionByID (user_id_t sect_id) const
469 {
470     SectionSP sect_sp;
471     if (sect_id)
472     {
473         const_iterator sect_iter;
474         const_iterator end = m_sections.end();
475         for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
476         {
477             if ((*sect_iter)->GetID() == sect_id)
478             {
479                 sect_sp = *sect_iter;
480                 break;
481             }
482             else
483             {
484                 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
485             }
486         }
487     }
488     return sect_sp;
489 }
490
491
492 SectionSP
493 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
494 {
495     SectionSP sect_sp;
496     size_t num_sections = m_sections.size();
497     for (size_t idx = start_idx; idx < num_sections; ++idx)
498     {
499         if (m_sections[idx]->GetType() == sect_type)
500         {
501             sect_sp = m_sections[idx];
502             break;
503         }
504         else if (check_children)
505         {
506             sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
507             if (sect_sp)
508                 break;
509         }
510     }
511     return sect_sp;
512 }
513
514 SectionSP
515 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
516 {
517     SectionSP sect_sp;
518     const_iterator sect_iter;
519     const_iterator end = m_sections.end();
520     for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
521     {
522         Section *sect = sect_iter->get();
523         if (sect->ContainsFileAddress (vm_addr))
524         {
525             // The file address is in this section. We need to make sure one of our child
526             // sections doesn't contain this address as well as obeying the depth limit
527             // that was passed in.
528             if (depth > 0)
529                 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
530
531             if (sect_sp.get() == NULL && !sect->IsFake())
532                 sect_sp = *sect_iter;
533         }
534     }
535     return sect_sp;
536 }
537
538 bool
539 SectionList::ContainsSection(user_id_t sect_id) const
540 {
541     return FindSectionByID (sect_id).get() != NULL;
542 }
543
544 void
545 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
546 {
547     bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
548     if (show_header && !m_sections.empty())
549     {
550         s->Indent();
551         s->Printf(    "SectID     Type             %s Address                             File Off.  File Size  Flags      Section Name\n", target_has_loaded_sections ? "Load" : "File");
552         s->Indent();
553         s->PutCString("---------- ---------------- ---------------------------------------  ---------- ---------- ---------- ----------------------------\n");
554     }
555
556
557     const_iterator sect_iter;
558     const_iterator end = m_sections.end();
559     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
560     {
561         (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
562     }
563
564     if (show_header && !m_sections.empty())
565         s->IndentLess();
566
567 }
568
569 size_t
570 SectionList::Slide (addr_t slide_amount, bool slide_children)
571 {
572     size_t count = 0;
573     const_iterator pos, end = m_sections.end();
574     for (pos = m_sections.begin(); pos != end; ++pos)
575     {
576         if ((*pos)->Slide(slide_amount, slide_children))
577             ++count;
578     }
579     return count;
580 }