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