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