xref: /freebsd-src/contrib/llvm-project/lldb/source/Core/Section.cpp (revision 8cc087a1eee9ec1ca9f7ac1e63ad51bdb5a682eb)
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 eSectionTypeOther:
149     return "regular";
150   }
151   return "unknown";
152 }
153 
154 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
155                  user_id_t sect_id, ConstString name,
156                  SectionType sect_type, addr_t file_addr, addr_t byte_size,
157                  lldb::offset_t file_offset, lldb::offset_t file_size,
158                  uint32_t log2align, uint32_t flags,
159                  uint32_t target_byte_size /*=1*/)
160     : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
161       m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
162       m_file_addr(file_addr), m_byte_size(byte_size),
163       m_file_offset(file_offset), m_file_size(file_size),
164       m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
165       m_thread_specific(false), m_readable(false), m_writable(false),
166       m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
167   //    printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
168   //    addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
169   //    - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
170   //            this, module_sp.get(), sect_id, file_addr, file_addr +
171   //            byte_size, file_offset, file_offset + file_size, flags,
172   //            name.GetCString());
173 }
174 
175 Section::Section(const lldb::SectionSP &parent_section_sp,
176                  const ModuleSP &module_sp, ObjectFile *obj_file,
177                  user_id_t sect_id, ConstString name,
178                  SectionType sect_type, addr_t file_addr, addr_t byte_size,
179                  lldb::offset_t file_offset, lldb::offset_t file_size,
180                  uint32_t log2align, uint32_t flags,
181                  uint32_t target_byte_size /*=1*/)
182     : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
183       m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
184       m_file_addr(file_addr), m_byte_size(byte_size),
185       m_file_offset(file_offset), m_file_size(file_size),
186       m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
187       m_thread_specific(false), m_readable(false), m_writable(false),
188       m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
189   //    printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
190   //    addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
191   //    - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
192   //            this, module_sp.get(), sect_id, file_addr, file_addr +
193   //            byte_size, file_offset, file_offset + file_size, flags,
194   //            parent_section_sp->GetName().GetCString(), name.GetCString());
195   if (parent_section_sp)
196     m_parent_wp = parent_section_sp;
197 }
198 
199 Section::~Section() {
200   //    printf ("Section::~Section(%p)\n", this);
201 }
202 
203 addr_t Section::GetFileAddress() const {
204   SectionSP parent_sp(GetParent());
205   if (parent_sp) {
206     // This section has a parent which means m_file_addr is an offset into the
207     // parent section, so the file address for this section is the file address
208     // of the parent plus the offset
209     return parent_sp->GetFileAddress() + m_file_addr;
210   }
211   // This section has no parent, so m_file_addr is the file base address
212   return m_file_addr;
213 }
214 
215 bool Section::SetFileAddress(lldb::addr_t file_addr) {
216   SectionSP parent_sp(GetParent());
217   if (parent_sp) {
218     if (m_file_addr >= file_addr)
219       return parent_sp->SetFileAddress(m_file_addr - file_addr);
220     return false;
221   } else {
222     // This section has no parent, so m_file_addr is the file base address
223     m_file_addr = file_addr;
224     return true;
225   }
226 }
227 
228 lldb::addr_t Section::GetOffset() const {
229   // This section has a parent which means m_file_addr is an offset.
230   SectionSP parent_sp(GetParent());
231   if (parent_sp)
232     return m_file_addr;
233 
234   // This section has no parent, so there is no offset to be had
235   return 0;
236 }
237 
238 addr_t Section::GetLoadBaseAddress(Target *target) const {
239   addr_t load_base_addr = LLDB_INVALID_ADDRESS;
240   SectionSP parent_sp(GetParent());
241   if (parent_sp) {
242     load_base_addr = parent_sp->GetLoadBaseAddress(target);
243     if (load_base_addr != LLDB_INVALID_ADDRESS)
244       load_base_addr += GetOffset();
245   }
246   if (load_base_addr == LLDB_INVALID_ADDRESS) {
247     load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
248         const_cast<Section *>(this)->shared_from_this());
249   }
250   return load_base_addr;
251 }
252 
253 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
254                                       bool allow_section_end) const {
255   const size_t num_children = m_children.GetSize();
256   for (size_t i = 0; i < num_children; i++) {
257     Section *child_section = m_children.GetSectionAtIndex(i).get();
258 
259     addr_t child_offset = child_section->GetOffset();
260     if (child_offset <= offset &&
261         offset - child_offset <
262             child_section->GetByteSize() + (allow_section_end ? 1 : 0))
263       return child_section->ResolveContainedAddress(offset - child_offset,
264                                                     so_addr, allow_section_end);
265   }
266   so_addr.SetOffset(offset);
267   so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
268 
269   // Ensure that there are no orphaned (i.e., moduleless) sections.
270   assert(GetModule().get());
271   return true;
272 }
273 
274 bool Section::ContainsFileAddress(addr_t vm_addr) const {
275   const addr_t file_addr = GetFileAddress();
276   if (file_addr != LLDB_INVALID_ADDRESS && !IsThreadSpecific()) {
277     if (file_addr <= vm_addr) {
278       const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
279       return offset < GetByteSize();
280     }
281   }
282   return false;
283 }
284 
285 void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
286                    uint32_t depth) const {
287   s.indent(indent);
288   s << llvm::format("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
289   bool resolved = true;
290   addr_t addr = LLDB_INVALID_ADDRESS;
291 
292   if (GetByteSize() == 0)
293     s.indent(39);
294   else {
295     if (target)
296       addr = GetLoadBaseAddress(target);
297 
298     if (addr == LLDB_INVALID_ADDRESS) {
299       if (target)
300         resolved = false;
301       addr = GetFileAddress();
302     }
303 
304     VMRange range(addr, addr + m_byte_size);
305     range.Dump(s, 0);
306   }
307 
308   s << llvm::format("%c %c%c%c  0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
309                     resolved ? ' ' : '*', m_readable ? 'r' : '-',
310                     m_writable ? 'w' : '-', m_executable ? 'x' : '-',
311                     m_file_offset, m_file_size, Get());
312 
313   DumpName(s);
314 
315   s << "\n";
316 
317   if (depth > 0)
318     m_children.Dump(s, indent, target, false, depth - 1);
319 }
320 
321 void Section::DumpName(llvm::raw_ostream &s) const {
322   SectionSP parent_sp(GetParent());
323   if (parent_sp) {
324     parent_sp->DumpName(s);
325     s << '.';
326   } else {
327     // The top most section prints the module basename
328     const char *name = nullptr;
329     ModuleSP module_sp(GetModule());
330 
331     if (m_obj_file) {
332       const FileSpec &file_spec = m_obj_file->GetFileSpec();
333       name = file_spec.GetFilename().AsCString();
334     }
335     if ((!name || !name[0]) && module_sp)
336       name = module_sp->GetFileSpec().GetFilename().AsCString();
337     if (name && name[0])
338       s << name << '.';
339   }
340   s << m_name;
341 }
342 
343 bool Section::IsDescendant(const Section *section) {
344   if (this == section)
345     return true;
346   SectionSP parent_sp(GetParent());
347   if (parent_sp)
348     return parent_sp->IsDescendant(section);
349   return false;
350 }
351 
352 bool Section::Slide(addr_t slide_amount, bool slide_children) {
353   if (m_file_addr != LLDB_INVALID_ADDRESS) {
354     if (slide_amount == 0)
355       return true;
356 
357     m_file_addr += slide_amount;
358 
359     if (slide_children)
360       m_children.Slide(slide_amount, slide_children);
361 
362     return true;
363   }
364   return false;
365 }
366 
367 /// Get the permissions as OR'ed bits from lldb::Permissions
368 uint32_t Section::GetPermissions() const {
369   uint32_t permissions = 0;
370   if (m_readable)
371     permissions |= ePermissionsReadable;
372   if (m_writable)
373     permissions |= ePermissionsWritable;
374   if (m_executable)
375     permissions |= ePermissionsExecutable;
376   return permissions;
377 }
378 
379 /// Set the permissions using bits OR'ed from lldb::Permissions
380 void Section::SetPermissions(uint32_t permissions) {
381   m_readable = (permissions & ePermissionsReadable) != 0;
382   m_writable = (permissions & ePermissionsWritable) != 0;
383   m_executable = (permissions & ePermissionsExecutable) != 0;
384 }
385 
386 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
387                                        lldb::offset_t offset) {
388   if (m_obj_file)
389     return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
390   return 0;
391 }
392 
393 lldb::offset_t Section::GetSectionData(DataExtractor &section_data) {
394   if (m_obj_file)
395     return m_obj_file->ReadSectionData(this, section_data);
396   return 0;
397 }
398 
399 bool Section::ContainsOnlyDebugInfo() const {
400   switch (m_type) {
401   case eSectionTypeInvalid:
402   case eSectionTypeCode:
403   case eSectionTypeContainer:
404   case eSectionTypeData:
405   case eSectionTypeDataCString:
406   case eSectionTypeDataCStringPointers:
407   case eSectionTypeDataSymbolAddress:
408   case eSectionTypeData4:
409   case eSectionTypeData8:
410   case eSectionTypeData16:
411   case eSectionTypeDataPointers:
412   case eSectionTypeZeroFill:
413   case eSectionTypeDataObjCMessageRefs:
414   case eSectionTypeDataObjCCFStrings:
415   case eSectionTypeELFSymbolTable:
416   case eSectionTypeELFDynamicSymbols:
417   case eSectionTypeELFRelocationEntries:
418   case eSectionTypeELFDynamicLinkInfo:
419   case eSectionTypeEHFrame:
420   case eSectionTypeARMexidx:
421   case eSectionTypeARMextab:
422   case eSectionTypeCompactUnwind:
423   case eSectionTypeGoSymtab:
424   case eSectionTypeAbsoluteAddress:
425   case eSectionTypeOther:
426     return false;
427 
428   case eSectionTypeDebug:
429   case eSectionTypeDWARFDebugAbbrev:
430   case eSectionTypeDWARFDebugAbbrevDwo:
431   case eSectionTypeDWARFDebugAddr:
432   case eSectionTypeDWARFDebugAranges:
433   case eSectionTypeDWARFDebugCuIndex:
434   case eSectionTypeDWARFDebugTuIndex:
435   case eSectionTypeDWARFDebugFrame:
436   case eSectionTypeDWARFDebugInfo:
437   case eSectionTypeDWARFDebugInfoDwo:
438   case eSectionTypeDWARFDebugLine:
439   case eSectionTypeDWARFDebugLineStr:
440   case eSectionTypeDWARFDebugLoc:
441   case eSectionTypeDWARFDebugLocDwo:
442   case eSectionTypeDWARFDebugLocLists:
443   case eSectionTypeDWARFDebugLocListsDwo:
444   case eSectionTypeDWARFDebugMacInfo:
445   case eSectionTypeDWARFDebugMacro:
446   case eSectionTypeDWARFDebugPubNames:
447   case eSectionTypeDWARFDebugPubTypes:
448   case eSectionTypeDWARFDebugRanges:
449   case eSectionTypeDWARFDebugRngLists:
450   case eSectionTypeDWARFDebugRngListsDwo:
451   case eSectionTypeDWARFDebugStr:
452   case eSectionTypeDWARFDebugStrDwo:
453   case eSectionTypeDWARFDebugStrOffsets:
454   case eSectionTypeDWARFDebugStrOffsetsDwo:
455   case eSectionTypeDWARFDebugTypes:
456   case eSectionTypeDWARFDebugTypesDwo:
457   case eSectionTypeDWARFDebugNames:
458   case eSectionTypeDWARFAppleNames:
459   case eSectionTypeDWARFAppleTypes:
460   case eSectionTypeDWARFAppleNamespaces:
461   case eSectionTypeDWARFAppleObjC:
462   case eSectionTypeDWARFGNUDebugAltLink:
463     return true;
464   }
465   return false;
466 }
467 
468 
469 #pragma mark SectionList
470 
471 SectionList &SectionList::operator=(const SectionList &rhs) {
472   if (this != &rhs)
473     m_sections = rhs.m_sections;
474   return *this;
475 }
476 
477 size_t SectionList::AddSection(const lldb::SectionSP &section_sp) {
478   if (section_sp) {
479     size_t section_index = m_sections.size();
480     m_sections.push_back(section_sp);
481     return section_index;
482   }
483 
484   return std::numeric_limits<size_t>::max();
485 }
486 
487 // Warning, this can be slow as it's removing items from a std::vector.
488 bool SectionList::DeleteSection(size_t idx) {
489   if (idx < m_sections.size()) {
490     m_sections.erase(m_sections.begin() + idx);
491     return true;
492   }
493   return false;
494 }
495 
496 size_t SectionList::FindSectionIndex(const Section *sect) {
497   iterator sect_iter;
498   iterator begin = m_sections.begin();
499   iterator end = m_sections.end();
500   for (sect_iter = begin; sect_iter != end; ++sect_iter) {
501     if (sect_iter->get() == sect) {
502       // The secton was already in this section list
503       return std::distance(begin, sect_iter);
504     }
505   }
506   return UINT32_MAX;
507 }
508 
509 size_t SectionList::AddUniqueSection(const lldb::SectionSP &sect_sp) {
510   size_t sect_idx = FindSectionIndex(sect_sp.get());
511   if (sect_idx == UINT32_MAX) {
512     sect_idx = AddSection(sect_sp);
513   }
514   return sect_idx;
515 }
516 
517 bool SectionList::ReplaceSection(user_id_t sect_id,
518                                  const lldb::SectionSP &sect_sp,
519                                  uint32_t depth) {
520   iterator sect_iter, end = m_sections.end();
521   for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
522     if ((*sect_iter)->GetID() == sect_id) {
523       *sect_iter = sect_sp;
524       return true;
525     } else if (depth > 0) {
526       if ((*sect_iter)
527               ->GetChildren()
528               .ReplaceSection(sect_id, sect_sp, depth - 1))
529         return true;
530     }
531   }
532   return false;
533 }
534 
535 size_t SectionList::GetNumSections(uint32_t depth) const {
536   size_t count = m_sections.size();
537   if (depth > 0) {
538     const_iterator sect_iter, end = m_sections.end();
539     for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
540       count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
541     }
542   }
543   return count;
544 }
545 
546 SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
547   SectionSP sect_sp;
548   if (idx < m_sections.size())
549     sect_sp = m_sections[idx];
550   return sect_sp;
551 }
552 
553 SectionSP
554 SectionList::FindSectionByName(ConstString section_dstr) const {
555   SectionSP sect_sp;
556   // Check if we have a valid section string
557   if (section_dstr && !m_sections.empty()) {
558     const_iterator sect_iter;
559     const_iterator end = m_sections.end();
560     for (sect_iter = m_sections.begin();
561          sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
562       Section *child_section = sect_iter->get();
563       if (child_section) {
564         if (child_section->GetName() == section_dstr) {
565           sect_sp = *sect_iter;
566         } else {
567           sect_sp =
568               child_section->GetChildren().FindSectionByName(section_dstr);
569         }
570       }
571     }
572   }
573   return sect_sp;
574 }
575 
576 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
577   SectionSP sect_sp;
578   if (sect_id) {
579     const_iterator sect_iter;
580     const_iterator end = m_sections.end();
581     for (sect_iter = m_sections.begin();
582          sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
583       if ((*sect_iter)->GetID() == sect_id) {
584         sect_sp = *sect_iter;
585         break;
586       } else {
587         sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
588       }
589     }
590   }
591   return sect_sp;
592 }
593 
594 SectionSP SectionList::FindSectionByType(SectionType sect_type,
595                                          bool check_children,
596                                          size_t start_idx) const {
597   SectionSP sect_sp;
598   size_t num_sections = m_sections.size();
599   for (size_t idx = start_idx; idx < num_sections; ++idx) {
600     if (m_sections[idx]->GetType() == sect_type) {
601       sect_sp = m_sections[idx];
602       break;
603     } else if (check_children) {
604       sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
605           sect_type, check_children, 0);
606       if (sect_sp)
607         break;
608     }
609   }
610   return sect_sp;
611 }
612 
613 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
614                                                         uint32_t depth) const {
615   SectionSP sect_sp;
616   const_iterator sect_iter;
617   const_iterator end = m_sections.end();
618   for (sect_iter = m_sections.begin();
619        sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) {
620     Section *sect = sect_iter->get();
621     if (sect->ContainsFileAddress(vm_addr)) {
622       // The file address is in this section. We need to make sure one of our
623       // child sections doesn't contain this address as well as obeying the
624       // depth limit that was passed in.
625       if (depth > 0)
626         sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
627             vm_addr, depth - 1);
628 
629       if (sect_sp.get() == nullptr && !sect->IsFake())
630         sect_sp = *sect_iter;
631     }
632   }
633   return sect_sp;
634 }
635 
636 bool SectionList::ContainsSection(user_id_t sect_id) const {
637   return FindSectionByID(sect_id).get() != nullptr;
638 }
639 
640 void SectionList::Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
641                        bool show_header, uint32_t depth) const {
642   bool target_has_loaded_sections =
643       target && !target->GetSectionLoadList().IsEmpty();
644   if (show_header && !m_sections.empty()) {
645     s.indent(indent);
646     s << llvm::formatv(
647         "SectID     Type             {0} Address                          "
648         "   Perm File Off.  File Size  Flags "
649         "     Section Name\n",
650         target_has_loaded_sections ? "Load" : "File");
651     s.indent(indent);
652     s << "---------- ---------------- "
653          "---------------------------------------  ---- ---------- "
654          "---------- "
655          "---------- ----------------------------\n";
656   }
657 
658   for (const auto &section_sp : m_sections)
659     section_sp->Dump(s, indent, target_has_loaded_sections ? target : nullptr,
660                      depth);
661 }
662 
663 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
664   size_t count = 0;
665   const_iterator pos, end = m_sections.end();
666   for (pos = m_sections.begin(); pos != end; ++pos) {
667     if ((*pos)->Slide(slide_amount, slide_children))
668       ++count;
669   }
670   return count;
671 }
672 
673 uint64_t SectionList::GetDebugInfoSize() const {
674   uint64_t debug_info_size = 0;
675   for (const auto &section : m_sections) {
676     const SectionList &sub_sections = section->GetChildren();
677     if (sub_sections.GetSize() > 0)
678       debug_info_size += sub_sections.GetDebugInfoSize();
679     else if (section->ContainsOnlyDebugInfo())
680       debug_info_size += section->GetFileSize();
681   }
682   return debug_info_size;
683 }
684