blob: d516b63bcb72373cc41ff7a559117a2babb93c02 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Zachary Turner2f3df612017-04-06 21:28:29 +000011#include "lldb/Core/Address.h" // for Address
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
13#include "lldb/Symbol/ObjectFile.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000014#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000015#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000016#include "lldb/Utility/FileSpec.h" // for FileSpec
17#include "lldb/Utility/Stream.h" // for Stream
18#include "lldb/Utility/VMRange.h" // for VMRange
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
Zachary Turner2f3df612017-04-06 21:28:29 +000020#include <inttypes.h> // for PRIx64
21#include <limits> // for numeric_limits
22#include <utility> // for distance
23
24namespace lldb_private {
25class DataExtractor;
26}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027using namespace lldb;
28using namespace lldb_private;
29
Jan Kratochvile4777a92018-04-29 19:47:48 +000030const char *Section::GetTypeAsCString() const {
31 switch (m_type) {
Zachary Turnerdf449882017-02-01 19:45:14 +000032 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 eSectionTypeDWARFDebugAddr:
65 return "dwarf-addr";
66 case eSectionTypeDWARFDebugAranges:
67 return "dwarf-aranges";
Tamas Berghammer963ce482017-08-25 13:56:14 +000068 case eSectionTypeDWARFDebugCuIndex:
69 return "dwarf-cu-index";
Zachary Turnerdf449882017-02-01 19:45:14 +000070 case eSectionTypeDWARFDebugFrame:
71 return "dwarf-frame";
72 case eSectionTypeDWARFDebugInfo:
73 return "dwarf-info";
74 case eSectionTypeDWARFDebugLine:
75 return "dwarf-line";
George Rimarc6c7bfc2018-09-13 17:06:47 +000076 case eSectionTypeDWARFDebugLineStr:
77 return "dwarf-line-str";
Zachary Turnerdf449882017-02-01 19:45:14 +000078 case eSectionTypeDWARFDebugLoc:
79 return "dwarf-loc";
80 case eSectionTypeDWARFDebugMacInfo:
81 return "dwarf-macinfo";
82 case eSectionTypeDWARFDebugMacro:
83 return "dwarf-macro";
84 case eSectionTypeDWARFDebugPubNames:
85 return "dwarf-pubnames";
86 case eSectionTypeDWARFDebugPubTypes:
87 return "dwarf-pubtypes";
88 case eSectionTypeDWARFDebugRanges:
89 return "dwarf-ranges";
George Rimar6e357122018-10-10 08:11:15 +000090 case eSectionTypeDWARFDebugRngLists:
91 return "dwarf-rnglists";
Zachary Turnerdf449882017-02-01 19:45:14 +000092 case eSectionTypeDWARFDebugStr:
93 return "dwarf-str";
94 case eSectionTypeDWARFDebugStrOffsets:
95 return "dwarf-str-offsets";
Greg Clayton2550ca12018-05-08 17:19:24 +000096 case eSectionTypeDWARFDebugTypes:
97 return "dwarf-types";
Pavel Labatha041d842018-06-01 12:06:45 +000098 case eSectionTypeDWARFDebugNames:
99 return "dwarf-names";
Zachary Turnerdf449882017-02-01 19:45:14 +0000100 case eSectionTypeELFSymbolTable:
101 return "elf-symbol-table";
102 case eSectionTypeELFDynamicSymbols:
103 return "elf-dynamic-symbols";
104 case eSectionTypeELFRelocationEntries:
105 return "elf-relocation-entries";
106 case eSectionTypeELFDynamicLinkInfo:
107 return "elf-dynamic-link-info";
108 case eSectionTypeDWARFAppleNames:
109 return "apple-names";
110 case eSectionTypeDWARFAppleTypes:
111 return "apple-types";
112 case eSectionTypeDWARFAppleNamespaces:
113 return "apple-namespaces";
114 case eSectionTypeDWARFAppleObjC:
115 return "apple-objc";
116 case eSectionTypeEHFrame:
117 return "eh-frame";
118 case eSectionTypeARMexidx:
119 return "ARM.exidx";
120 case eSectionTypeARMextab:
121 return "ARM.extab";
122 case eSectionTypeCompactUnwind:
123 return "compact-unwind";
124 case eSectionTypeGoSymtab:
125 return "go-symtab";
126 case eSectionTypeAbsoluteAddress:
127 return "absolute";
Jan Kratochvile4777a92018-04-29 19:47:48 +0000128 case eSectionTypeDWARFGNUDebugAltLink:
129 return "dwarf-gnu-debugaltlink";
Zachary Turnerdf449882017-02-01 19:45:14 +0000130 case eSectionTypeOther:
131 return "regular";
132 }
133 return "unknown";
134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
137 user_id_t sect_id, const ConstString &name,
138 SectionType sect_type, addr_t file_addr, addr_t byte_size,
139 lldb::offset_t file_offset, lldb::offset_t file_size,
140 uint32_t log2align, uint32_t flags,
Greg Clayton3385fa02016-06-09 16:34:06 +0000141 uint32_t target_byte_size /*=1*/)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
143 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
144 m_file_addr(file_addr), m_byte_size(byte_size),
145 m_file_offset(file_offset), m_file_size(file_size),
146 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
147 m_thread_specific(false), m_readable(false), m_writable(false),
Ed Masted13f6912017-10-02 14:35:07 +0000148 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
150 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
151 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
152 // this, module_sp.get(), sect_id, file_addr, file_addr +
153 // byte_size, file_offset, file_offset + file_size, flags,
154 // name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157Section::Section(const lldb::SectionSP &parent_section_sp,
158 const ModuleSP &module_sp, ObjectFile *obj_file,
159 user_id_t sect_id, const ConstString &name,
160 SectionType sect_type, addr_t file_addr, addr_t byte_size,
161 lldb::offset_t file_offset, lldb::offset_t file_size,
162 uint32_t log2align, uint32_t flags,
163 uint32_t target_byte_size /*=1*/)
164 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
165 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
166 m_file_addr(file_addr), m_byte_size(byte_size),
167 m_file_offset(file_offset), m_file_size(file_size),
168 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
169 m_thread_specific(false), m_readable(false), m_writable(false),
Ed Masted13f6912017-10-02 14:35:07 +0000170 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
172 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
173 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
174 // this, module_sp.get(), sect_id, file_addr, file_addr +
175 // byte_size, file_offset, file_offset + file_size, flags,
176 // parent_section_sp->GetName().GetCString(), name.GetCString());
177 if (parent_section_sp)
178 m_parent_wp = parent_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181Section::~Section() {
182 // printf ("Section::~Section(%p)\n", this);
183}
184
185addr_t Section::GetFileAddress() const {
186 SectionSP parent_sp(GetParent());
187 if (parent_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000188 // This section has a parent which means m_file_addr is an offset into the
189 // parent section, so the file address for this section is the file address
190 // of the parent plus the offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 return parent_sp->GetFileAddress() + m_file_addr;
192 }
193 // This section has no parent, so m_file_addr is the file base address
194 return m_file_addr;
195}
196
197bool Section::SetFileAddress(lldb::addr_t file_addr) {
198 SectionSP parent_sp(GetParent());
199 if (parent_sp) {
200 if (m_file_addr >= file_addr)
201 return parent_sp->SetFileAddress(m_file_addr - file_addr);
202 return false;
203 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 // This section has no parent, so m_file_addr is the file base address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 m_file_addr = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208}
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210lldb::addr_t Section::GetOffset() const {
211 // This section has a parent which means m_file_addr is an offset.
212 SectionSP parent_sp(GetParent());
213 if (parent_sp)
214 return m_file_addr;
215
216 // This section has no parent, so there is no offset to be had
217 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218}
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220addr_t Section::GetLoadBaseAddress(Target *target) const {
221 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
222 SectionSP parent_sp(GetParent());
223 if (parent_sp) {
224 load_base_addr = parent_sp->GetLoadBaseAddress(target);
225 if (load_base_addr != LLDB_INVALID_ADDRESS)
226 load_base_addr += GetOffset();
227 }
228 if (load_base_addr == LLDB_INVALID_ADDRESS) {
229 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
230 const_cast<Section *>(this)->shared_from_this());
231 }
232 return load_base_addr;
233}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234
Pavel Labathc3c72122017-06-08 13:26:35 +0000235bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
236 bool allow_section_end) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 const size_t num_children = m_children.GetSize();
Pavel Labathc3c72122017-06-08 13:26:35 +0000238 for (size_t i = 0; i < num_children; i++) {
239 Section *child_section = m_children.GetSectionAtIndex(i).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240
Pavel Labathc3c72122017-06-08 13:26:35 +0000241 addr_t child_offset = child_section->GetOffset();
242 if (child_offset <= offset &&
243 offset - child_offset <
244 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
245 return child_section->ResolveContainedAddress(offset - child_offset,
246 so_addr, allow_section_end);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 }
248 so_addr.SetOffset(offset);
249 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
250
251#ifdef LLDB_CONFIGURATION_DEBUG
252 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
253 // sections.
254 assert(GetModule().get());
255#endif
256 return true;
257}
258
259bool Section::ContainsFileAddress(addr_t vm_addr) const {
260 const addr_t file_addr = GetFileAddress();
261 if (file_addr != LLDB_INVALID_ADDRESS) {
262 if (file_addr <= vm_addr) {
263 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
264 return offset < GetByteSize();
265 }
266 }
267 return false;
268}
269
270int Section::Compare(const Section &a, const Section &b) {
271 if (&a == &b)
272 return 0;
273
274 const ModuleSP a_module_sp = a.GetModule();
275 const ModuleSP b_module_sp = b.GetModule();
276 if (a_module_sp == b_module_sp) {
277 user_id_t a_sect_uid = a.GetID();
278 user_id_t b_sect_uid = b.GetID();
279 if (a_sect_uid < b_sect_uid)
280 return -1;
281 if (a_sect_uid > b_sect_uid)
282 return 1;
283 return 0;
284 } else {
285 // The modules are different, just compare the module pointers
286 if (a_module_sp.get() < b_module_sp.get())
287 return -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 return 1; // We already know the modules aren't equal
290 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291}
292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
294 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
295 s->Indent();
Jan Kratochvile4777a92018-04-29 19:47:48 +0000296 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 bool resolved = true;
298 addr_t addr = LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 if (GetByteSize() == 0)
301 s->Printf("%39s", "");
302 else {
303 if (target)
304 addr = GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 if (addr == LLDB_INVALID_ADDRESS) {
307 if (target)
308 resolved = false;
309 addr = GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 }
311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 VMRange range(addr, addr + m_byte_size);
313 range.Dump(s, 0);
314 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
317 resolved ? ' ' : '*', m_readable ? 'r' : '-',
318 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
319 m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 s->EOL();
324
325 if (depth > 0)
326 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327}
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329void Section::DumpName(Stream *s) const {
330 SectionSP parent_sp(GetParent());
331 if (parent_sp) {
332 parent_sp->DumpName(s);
333 s->PutChar('.');
334 } else {
335 // The top most section prints the module basename
336 const char *name = NULL;
337 ModuleSP module_sp(GetModule());
Michael Sartaina7499c92013-07-01 19:45:50 +0000338
Leonard Mosescu47196a22018-04-18 23:10:46 +0000339 if (m_obj_file) {
340 const FileSpec &file_spec = m_obj_file->GetFileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 name = file_spec.GetFilename().AsCString();
Leonard Mosescu47196a22018-04-18 23:10:46 +0000342 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 if ((!name || !name[0]) && module_sp)
344 name = module_sp->GetFileSpec().GetFilename().AsCString();
345 if (name && name[0])
346 s->Printf("%s.", name);
347 }
348 m_name.Dump(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351bool Section::IsDescendant(const Section *section) {
352 if (this == section)
353 return true;
354 SectionSP parent_sp(GetParent());
355 if (parent_sp)
356 return parent_sp->IsDescendant(section);
357 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360bool Section::Slide(addr_t slide_amount, bool slide_children) {
361 if (m_file_addr != LLDB_INVALID_ADDRESS) {
362 if (slide_amount == 0)
363 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 m_file_addr += slide_amount;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 if (slide_children)
368 m_children.Slide(slide_amount, slide_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 return true;
371 }
372 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373}
374
Greg Clayton3385fa02016-06-09 16:34:06 +0000375//------------------------------------------------------------------
376/// Get the permissions as OR'ed bits from lldb::Permissions
377//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378uint32_t Section::GetPermissions() const {
379 uint32_t permissions = 0;
380 if (m_readable)
381 permissions |= ePermissionsReadable;
382 if (m_writable)
383 permissions |= ePermissionsWritable;
384 if (m_executable)
385 permissions |= ePermissionsExecutable;
386 return permissions;
Greg Clayton3385fa02016-06-09 16:34:06 +0000387}
388
389//------------------------------------------------------------------
390/// Set the permissions using bits OR'ed from lldb::Permissions
391//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392void Section::SetPermissions(uint32_t permissions) {
393 m_readable = (permissions & ePermissionsReadable) != 0;
394 m_writable = (permissions & ePermissionsWritable) != 0;
395 m_executable = (permissions & ePermissionsExecutable) != 0;
Greg Clayton3385fa02016-06-09 16:34:06 +0000396}
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
399 lldb::offset_t offset) {
400 if (m_obj_file)
401 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
402 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000403}
404
Ed Masted13f6912017-10-02 14:35:07 +0000405lldb::offset_t Section::GetSectionData(DataExtractor &section_data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 if (m_obj_file)
407 return m_obj_file->ReadSectionData(this, section_data);
408 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000409}
410
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411#pragma mark SectionList
412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413SectionList::SectionList() : m_sections() {}
414
415SectionList::~SectionList() {}
416
417SectionList &SectionList::operator=(const SectionList &rhs) {
418 if (this != &rhs)
419 m_sections = rhs.m_sections;
420 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421}
422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423size_t SectionList::AddSection(const lldb::SectionSP &section_sp) {
424 if (section_sp) {
425 size_t section_index = m_sections.size();
426 m_sections.push_back(section_sp);
427 return section_index;
428 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 return std::numeric_limits<size_t>::max();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431}
432
Michael Sartaina7499c92013-07-01 19:45:50 +0000433// Warning, this can be slow as it's removing items from a std::vector.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434bool SectionList::DeleteSection(size_t idx) {
435 if (idx < m_sections.size()) {
436 m_sections.erase(m_sections.begin() + idx);
437 return true;
438 }
439 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000440}
441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442size_t SectionList::FindSectionIndex(const Section *sect) {
443 iterator sect_iter;
444 iterator begin = m_sections.begin();
445 iterator end = m_sections.end();
446 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
447 if (sect_iter->get() == sect) {
448 // The secton was already in this section list
449 return std::distance(begin, sect_iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
452 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453}
454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455size_t SectionList::AddUniqueSection(const lldb::SectionSP &sect_sp) {
456 size_t sect_idx = FindSectionIndex(sect_sp.get());
457 if (sect_idx == UINT32_MAX) {
458 sect_idx = AddSection(sect_sp);
459 }
460 return sect_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463bool SectionList::ReplaceSection(user_id_t sect_id,
464 const lldb::SectionSP &sect_sp,
465 uint32_t depth) {
466 iterator sect_iter, end = m_sections.end();
467 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
468 if ((*sect_iter)->GetID() == sect_id) {
469 *sect_iter = sect_sp;
470 return true;
471 } else if (depth > 0) {
472 if ((*sect_iter)
473 ->GetChildren()
474 .ReplaceSection(sect_id, sect_sp, depth - 1))
475 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
478 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481size_t SectionList::GetNumSections(uint32_t depth) const {
482 size_t count = m_sections.size();
483 if (depth > 0) {
484 const_iterator sect_iter, end = m_sections.end();
485 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
486 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 }
489 return count;
490}
491
492SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
493 SectionSP sect_sp;
494 if (idx < m_sections.size())
495 sect_sp = m_sections[idx];
496 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
499SectionSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500SectionList::FindSectionByName(const ConstString &section_dstr) const {
501 SectionSP sect_sp;
502 // Check if we have a valid section string
503 if (section_dstr && !m_sections.empty()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 const_iterator sect_iter;
505 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 for (sect_iter = m_sections.begin();
507 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
508 Section *child_section = sect_iter->get();
509 if (child_section) {
510 if (child_section->GetName() == section_dstr) {
511 sect_sp = *sect_iter;
512 } else {
513 sect_sp =
514 child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 }
519 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520}
521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
523 SectionSP sect_sp;
524 if (sect_id) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 const_iterator sect_iter;
526 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 for (sect_iter = m_sections.begin();
528 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
529 if ((*sect_iter)->GetID() == sect_id) {
530 sect_sp = *sect_iter;
531 break;
532 } else {
533 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
534 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 }
537 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538}
539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540SectionSP SectionList::FindSectionByType(SectionType sect_type,
541 bool check_children,
542 size_t start_idx) const {
543 SectionSP sect_sp;
544 size_t num_sections = m_sections.size();
545 for (size_t idx = start_idx; idx < num_sections; ++idx) {
546 if (m_sections[idx]->GetType() == sect_type) {
547 sect_sp = m_sections[idx];
548 break;
549 } else if (check_children) {
550 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
551 sect_type, check_children, 0);
552 if (sect_sp)
553 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 }
556 return sect_sp;
557}
558
559SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
560 uint32_t depth) const {
561 SectionSP sect_sp;
562 const_iterator sect_iter;
563 const_iterator end = m_sections.end();
564 for (sect_iter = m_sections.begin();
565 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
566 Section *sect = sect_iter->get();
567 if (sect->ContainsFileAddress(vm_addr)) {
568 // The file address is in this section. We need to make sure one of our
Adrian Prantl05097242018-04-30 16:49:04 +0000569 // child sections doesn't contain this address as well as obeying the
570 // depth limit that was passed in.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (depth > 0)
572 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
573 vm_addr, depth - 1);
574
575 if (sect_sp.get() == NULL && !sect->IsFake())
576 sect_sp = *sect_iter;
577 }
578 }
579 return sect_sp;
580}
581
582bool SectionList::ContainsSection(user_id_t sect_id) const {
583 return FindSectionByID(sect_id).get() != NULL;
584}
585
586void SectionList::Dump(Stream *s, Target *target, bool show_header,
587 uint32_t depth) const {
588 bool target_has_loaded_sections =
589 target && !target->GetSectionLoadList().IsEmpty();
590 if (show_header && !m_sections.empty()) {
591 s->Indent();
592 s->Printf("SectID Type %s Address "
593 " Perm File Off. File Size Flags "
594 " Section Name\n",
595 target_has_loaded_sections ? "Load" : "File");
596 s->Indent();
597 s->PutCString("---------- ---------------- "
598 "--------------------------------------- ---- ---------- "
599 "---------- "
600 "---------- ----------------------------\n");
601 }
602
603 const_iterator sect_iter;
604 const_iterator end = m_sections.end();
605 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
606 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
607 }
608
609 if (show_header && !m_sections.empty())
610 s->IndentLess();
611}
612
613size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
614 size_t count = 0;
615 const_iterator pos, end = m_sections.end();
616 for (pos = m_sections.begin(); pos != end; ++pos) {
617 if ((*pos)->Slide(slide_amount, slide_children))
618 ++count;
619 }
620 return count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}