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