blob: 24dbb428a07e753bd627e9ff36dd17b0cafa8182 [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";
George Rimare4dee262018-10-23 09:46:15 +000080 case eSectionTypeDWARFDebugLocLists:
81 return "dwarf-loclists";
Zachary Turnerdf449882017-02-01 19:45:14 +000082 case eSectionTypeDWARFDebugMacInfo:
83 return "dwarf-macinfo";
84 case eSectionTypeDWARFDebugMacro:
85 return "dwarf-macro";
86 case eSectionTypeDWARFDebugPubNames:
87 return "dwarf-pubnames";
88 case eSectionTypeDWARFDebugPubTypes:
89 return "dwarf-pubtypes";
90 case eSectionTypeDWARFDebugRanges:
91 return "dwarf-ranges";
George Rimar6e357122018-10-10 08:11:15 +000092 case eSectionTypeDWARFDebugRngLists:
93 return "dwarf-rnglists";
Zachary Turnerdf449882017-02-01 19:45:14 +000094 case eSectionTypeDWARFDebugStr:
95 return "dwarf-str";
96 case eSectionTypeDWARFDebugStrOffsets:
97 return "dwarf-str-offsets";
Greg Clayton2550ca12018-05-08 17:19:24 +000098 case eSectionTypeDWARFDebugTypes:
99 return "dwarf-types";
Pavel Labatha041d842018-06-01 12:06:45 +0000100 case eSectionTypeDWARFDebugNames:
101 return "dwarf-names";
Zachary Turnerdf449882017-02-01 19:45:14 +0000102 case eSectionTypeELFSymbolTable:
103 return "elf-symbol-table";
104 case eSectionTypeELFDynamicSymbols:
105 return "elf-dynamic-symbols";
106 case eSectionTypeELFRelocationEntries:
107 return "elf-relocation-entries";
108 case eSectionTypeELFDynamicLinkInfo:
109 return "elf-dynamic-link-info";
110 case eSectionTypeDWARFAppleNames:
111 return "apple-names";
112 case eSectionTypeDWARFAppleTypes:
113 return "apple-types";
114 case eSectionTypeDWARFAppleNamespaces:
115 return "apple-namespaces";
116 case eSectionTypeDWARFAppleObjC:
117 return "apple-objc";
118 case eSectionTypeEHFrame:
119 return "eh-frame";
120 case eSectionTypeARMexidx:
121 return "ARM.exidx";
122 case eSectionTypeARMextab:
123 return "ARM.extab";
124 case eSectionTypeCompactUnwind:
125 return "compact-unwind";
126 case eSectionTypeGoSymtab:
127 return "go-symtab";
128 case eSectionTypeAbsoluteAddress:
129 return "absolute";
Jan Kratochvile4777a92018-04-29 19:47:48 +0000130 case eSectionTypeDWARFGNUDebugAltLink:
131 return "dwarf-gnu-debugaltlink";
Zachary Turnerdf449882017-02-01 19:45:14 +0000132 case eSectionTypeOther:
133 return "regular";
134 }
135 return "unknown";
136}
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
139 user_id_t sect_id, const ConstString &name,
140 SectionType sect_type, addr_t file_addr, addr_t byte_size,
141 lldb::offset_t file_offset, lldb::offset_t file_size,
142 uint32_t log2align, uint32_t flags,
Greg Clayton3385fa02016-06-09 16:34:06 +0000143 uint32_t target_byte_size /*=1*/)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
145 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
146 m_file_addr(file_addr), m_byte_size(byte_size),
147 m_file_offset(file_offset), m_file_size(file_size),
148 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
149 m_thread_specific(false), m_readable(false), m_writable(false),
Ed Masted13f6912017-10-02 14:35:07 +0000150 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
152 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
153 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
154 // this, module_sp.get(), sect_id, file_addr, file_addr +
155 // byte_size, file_offset, file_offset + file_size, flags,
156 // name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157}
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159Section::Section(const lldb::SectionSP &parent_section_sp,
160 const ModuleSP &module_sp, ObjectFile *obj_file,
161 user_id_t sect_id, const ConstString &name,
162 SectionType sect_type, addr_t file_addr, addr_t byte_size,
163 lldb::offset_t file_offset, lldb::offset_t file_size,
164 uint32_t log2align, uint32_t flags,
165 uint32_t target_byte_size /*=1*/)
166 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
167 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
168 m_file_addr(file_addr), m_byte_size(byte_size),
169 m_file_offset(file_offset), m_file_size(file_size),
170 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
171 m_thread_specific(false), m_readable(false), m_writable(false),
Ed Masted13f6912017-10-02 14:35:07 +0000172 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
174 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
175 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
176 // this, module_sp.get(), sect_id, file_addr, file_addr +
177 // byte_size, file_offset, file_offset + file_size, flags,
178 // parent_section_sp->GetName().GetCString(), name.GetCString());
179 if (parent_section_sp)
180 m_parent_wp = parent_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183Section::~Section() {
184 // printf ("Section::~Section(%p)\n", this);
185}
186
187addr_t Section::GetFileAddress() const {
188 SectionSP parent_sp(GetParent());
189 if (parent_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000190 // This section has a parent which means m_file_addr is an offset into the
191 // parent section, so the file address for this section is the file address
192 // of the parent plus the offset
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 return parent_sp->GetFileAddress() + m_file_addr;
194 }
195 // This section has no parent, so m_file_addr is the file base address
196 return m_file_addr;
197}
198
199bool Section::SetFileAddress(lldb::addr_t file_addr) {
200 SectionSP parent_sp(GetParent());
201 if (parent_sp) {
202 if (m_file_addr >= file_addr)
203 return parent_sp->SetFileAddress(m_file_addr - file_addr);
204 return false;
205 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 // This section has no parent, so m_file_addr is the file base address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 m_file_addr = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212lldb::addr_t Section::GetOffset() const {
213 // This section has a parent which means m_file_addr is an offset.
214 SectionSP parent_sp(GetParent());
215 if (parent_sp)
216 return m_file_addr;
217
218 // This section has no parent, so there is no offset to be had
219 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222addr_t Section::GetLoadBaseAddress(Target *target) const {
223 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
224 SectionSP parent_sp(GetParent());
225 if (parent_sp) {
226 load_base_addr = parent_sp->GetLoadBaseAddress(target);
227 if (load_base_addr != LLDB_INVALID_ADDRESS)
228 load_base_addr += GetOffset();
229 }
230 if (load_base_addr == LLDB_INVALID_ADDRESS) {
231 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
232 const_cast<Section *>(this)->shared_from_this());
233 }
234 return load_base_addr;
235}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236
Pavel Labathc3c72122017-06-08 13:26:35 +0000237bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
238 bool allow_section_end) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 const size_t num_children = m_children.GetSize();
Pavel Labathc3c72122017-06-08 13:26:35 +0000240 for (size_t i = 0; i < num_children; i++) {
241 Section *child_section = m_children.GetSectionAtIndex(i).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242
Pavel Labathc3c72122017-06-08 13:26:35 +0000243 addr_t child_offset = child_section->GetOffset();
244 if (child_offset <= offset &&
245 offset - child_offset <
246 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
247 return child_section->ResolveContainedAddress(offset - child_offset,
248 so_addr, allow_section_end);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 }
250 so_addr.SetOffset(offset);
251 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
252
253#ifdef LLDB_CONFIGURATION_DEBUG
254 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
255 // sections.
256 assert(GetModule().get());
257#endif
258 return true;
259}
260
261bool Section::ContainsFileAddress(addr_t vm_addr) const {
262 const addr_t file_addr = GetFileAddress();
263 if (file_addr != LLDB_INVALID_ADDRESS) {
264 if (file_addr <= vm_addr) {
265 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
266 return offset < GetByteSize();
267 }
268 }
269 return false;
270}
271
272int Section::Compare(const Section &a, const Section &b) {
273 if (&a == &b)
274 return 0;
275
276 const ModuleSP a_module_sp = a.GetModule();
277 const ModuleSP b_module_sp = b.GetModule();
278 if (a_module_sp == b_module_sp) {
279 user_id_t a_sect_uid = a.GetID();
280 user_id_t b_sect_uid = b.GetID();
281 if (a_sect_uid < b_sect_uid)
282 return -1;
283 if (a_sect_uid > b_sect_uid)
284 return 1;
285 return 0;
286 } else {
287 // The modules are different, just compare the module pointers
288 if (a_module_sp.get() < b_module_sp.get())
289 return -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 return 1; // We already know the modules aren't equal
292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293}
294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
296 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
297 s->Indent();
Jan Kratochvile4777a92018-04-29 19:47:48 +0000298 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetTypeAsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 bool resolved = true;
300 addr_t addr = LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 if (GetByteSize() == 0)
303 s->Printf("%39s", "");
304 else {
305 if (target)
306 addr = GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 if (addr == LLDB_INVALID_ADDRESS) {
309 if (target)
310 resolved = false;
311 addr = GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 }
313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 VMRange range(addr, addr + m_byte_size);
315 range.Dump(s, 0);
316 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
319 resolved ? ' ' : '*', m_readable ? 'r' : '-',
320 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
321 m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 s->EOL();
326
327 if (depth > 0)
328 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331void Section::DumpName(Stream *s) const {
332 SectionSP parent_sp(GetParent());
333 if (parent_sp) {
334 parent_sp->DumpName(s);
335 s->PutChar('.');
336 } else {
337 // The top most section prints the module basename
338 const char *name = NULL;
339 ModuleSP module_sp(GetModule());
Michael Sartaina7499c92013-07-01 19:45:50 +0000340
Leonard Mosescu47196a22018-04-18 23:10:46 +0000341 if (m_obj_file) {
342 const FileSpec &file_spec = m_obj_file->GetFileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 name = file_spec.GetFilename().AsCString();
Leonard Mosescu47196a22018-04-18 23:10:46 +0000344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 if ((!name || !name[0]) && module_sp)
346 name = module_sp->GetFileSpec().GetFilename().AsCString();
347 if (name && name[0])
348 s->Printf("%s.", name);
349 }
350 m_name.Dump(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353bool Section::IsDescendant(const Section *section) {
354 if (this == section)
355 return true;
356 SectionSP parent_sp(GetParent());
357 if (parent_sp)
358 return parent_sp->IsDescendant(section);
359 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360}
361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362bool Section::Slide(addr_t slide_amount, bool slide_children) {
363 if (m_file_addr != LLDB_INVALID_ADDRESS) {
364 if (slide_amount == 0)
365 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 m_file_addr += slide_amount;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if (slide_children)
370 m_children.Slide(slide_amount, slide_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 return true;
373 }
374 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375}
376
Greg Clayton3385fa02016-06-09 16:34:06 +0000377//------------------------------------------------------------------
378/// Get the permissions as OR'ed bits from lldb::Permissions
379//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380uint32_t Section::GetPermissions() const {
381 uint32_t permissions = 0;
382 if (m_readable)
383 permissions |= ePermissionsReadable;
384 if (m_writable)
385 permissions |= ePermissionsWritable;
386 if (m_executable)
387 permissions |= ePermissionsExecutable;
388 return permissions;
Greg Clayton3385fa02016-06-09 16:34:06 +0000389}
390
391//------------------------------------------------------------------
392/// Set the permissions using bits OR'ed from lldb::Permissions
393//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394void Section::SetPermissions(uint32_t permissions) {
395 m_readable = (permissions & ePermissionsReadable) != 0;
396 m_writable = (permissions & ePermissionsWritable) != 0;
397 m_executable = (permissions & ePermissionsExecutable) != 0;
Greg Clayton3385fa02016-06-09 16:34:06 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
401 lldb::offset_t offset) {
402 if (m_obj_file)
403 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
404 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000405}
406
Ed Masted13f6912017-10-02 14:35:07 +0000407lldb::offset_t Section::GetSectionData(DataExtractor &section_data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 if (m_obj_file)
409 return m_obj_file->ReadSectionData(this, section_data);
410 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000411}
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413#pragma mark SectionList
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415SectionList::SectionList() : m_sections() {}
416
417SectionList::~SectionList() {}
418
419SectionList &SectionList::operator=(const SectionList &rhs) {
420 if (this != &rhs)
421 m_sections = rhs.m_sections;
422 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423}
424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425size_t SectionList::AddSection(const lldb::SectionSP &section_sp) {
426 if (section_sp) {
427 size_t section_index = m_sections.size();
428 m_sections.push_back(section_sp);
429 return section_index;
430 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 return std::numeric_limits<size_t>::max();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
Michael Sartaina7499c92013-07-01 19:45:50 +0000435// Warning, this can be slow as it's removing items from a std::vector.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436bool SectionList::DeleteSection(size_t idx) {
437 if (idx < m_sections.size()) {
438 m_sections.erase(m_sections.begin() + idx);
439 return true;
440 }
441 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000442}
443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444size_t SectionList::FindSectionIndex(const Section *sect) {
445 iterator sect_iter;
446 iterator begin = m_sections.begin();
447 iterator end = m_sections.end();
448 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
449 if (sect_iter->get() == sect) {
450 // The secton was already in this section list
451 return std::distance(begin, sect_iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 }
454 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455}
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457size_t SectionList::AddUniqueSection(const lldb::SectionSP &sect_sp) {
458 size_t sect_idx = FindSectionIndex(sect_sp.get());
459 if (sect_idx == UINT32_MAX) {
460 sect_idx = AddSection(sect_sp);
461 }
462 return sect_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465bool SectionList::ReplaceSection(user_id_t sect_id,
466 const lldb::SectionSP &sect_sp,
467 uint32_t depth) {
468 iterator sect_iter, end = m_sections.end();
469 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
470 if ((*sect_iter)->GetID() == sect_id) {
471 *sect_iter = sect_sp;
472 return true;
473 } else if (depth > 0) {
474 if ((*sect_iter)
475 ->GetChildren()
476 .ReplaceSection(sect_id, sect_sp, depth - 1))
477 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 }
480 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483size_t SectionList::GetNumSections(uint32_t depth) const {
484 size_t count = m_sections.size();
485 if (depth > 0) {
486 const_iterator sect_iter, end = m_sections.end();
487 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
488 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 }
491 return count;
492}
493
494SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
495 SectionSP sect_sp;
496 if (idx < m_sections.size())
497 sect_sp = m_sections[idx];
498 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499}
500
501SectionSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502SectionList::FindSectionByName(const ConstString &section_dstr) const {
503 SectionSP sect_sp;
504 // Check if we have a valid section string
505 if (section_dstr && !m_sections.empty()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 const_iterator sect_iter;
507 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 for (sect_iter = m_sections.begin();
509 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
510 Section *child_section = sect_iter->get();
511 if (child_section) {
512 if (child_section->GetName() == section_dstr) {
513 sect_sp = *sect_iter;
514 } else {
515 sect_sp =
516 child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 }
521 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522}
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
525 SectionSP sect_sp;
526 if (sect_id) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 const_iterator sect_iter;
528 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 for (sect_iter = m_sections.begin();
530 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
531 if ((*sect_iter)->GetID() == sect_id) {
532 sect_sp = *sect_iter;
533 break;
534 } else {
535 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
536 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 }
539 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540}
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542SectionSP SectionList::FindSectionByType(SectionType sect_type,
543 bool check_children,
544 size_t start_idx) const {
545 SectionSP sect_sp;
546 size_t num_sections = m_sections.size();
547 for (size_t idx = start_idx; idx < num_sections; ++idx) {
548 if (m_sections[idx]->GetType() == sect_type) {
549 sect_sp = m_sections[idx];
550 break;
551 } else if (check_children) {
552 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
553 sect_type, check_children, 0);
554 if (sect_sp)
555 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 }
558 return sect_sp;
559}
560
561SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
562 uint32_t depth) const {
563 SectionSP sect_sp;
564 const_iterator sect_iter;
565 const_iterator end = m_sections.end();
566 for (sect_iter = m_sections.begin();
567 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
568 Section *sect = sect_iter->get();
569 if (sect->ContainsFileAddress(vm_addr)) {
570 // The file address is in this section. We need to make sure one of our
Adrian Prantl05097242018-04-30 16:49:04 +0000571 // child sections doesn't contain this address as well as obeying the
572 // depth limit that was passed in.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 if (depth > 0)
574 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
575 vm_addr, depth - 1);
576
577 if (sect_sp.get() == NULL && !sect->IsFake())
578 sect_sp = *sect_iter;
579 }
580 }
581 return sect_sp;
582}
583
584bool SectionList::ContainsSection(user_id_t sect_id) const {
585 return FindSectionByID(sect_id).get() != NULL;
586}
587
588void SectionList::Dump(Stream *s, Target *target, bool show_header,
589 uint32_t depth) const {
590 bool target_has_loaded_sections =
591 target && !target->GetSectionLoadList().IsEmpty();
592 if (show_header && !m_sections.empty()) {
593 s->Indent();
594 s->Printf("SectID Type %s Address "
595 " Perm File Off. File Size Flags "
596 " Section Name\n",
597 target_has_loaded_sections ? "Load" : "File");
598 s->Indent();
599 s->PutCString("---------- ---------------- "
600 "--------------------------------------- ---- ---------- "
601 "---------- "
602 "---------- ----------------------------\n");
603 }
604
605 const_iterator sect_iter;
606 const_iterator end = m_sections.end();
607 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
608 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
609 }
610
611 if (show_header && !m_sections.empty())
612 s->IndentLess();
613}
614
615size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
616 size_t count = 0;
617 const_iterator pos, end = m_sections.end();
618 for (pos = m_sections.begin(); pos != end; ++pos) {
619 if ((*pos)->Slide(slide_amount, slide_children))
620 ++count;
621 }
622 return count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623}