blob: 3b76dd361ff3f8f3f54b30c205e062fd4313f529 [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
Zachary Turnerdf449882017-02-01 19:45:14 +000030static const char *GetSectionTypeAsCString(lldb::SectionType sect_type) {
31 switch (sect_type) {
32 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";
68 case eSectionTypeDWARFDebugFrame:
69 return "dwarf-frame";
70 case eSectionTypeDWARFDebugInfo:
71 return "dwarf-info";
72 case eSectionTypeDWARFDebugLine:
73 return "dwarf-line";
74 case eSectionTypeDWARFDebugLoc:
75 return "dwarf-loc";
76 case eSectionTypeDWARFDebugMacInfo:
77 return "dwarf-macinfo";
78 case eSectionTypeDWARFDebugMacro:
79 return "dwarf-macro";
80 case eSectionTypeDWARFDebugPubNames:
81 return "dwarf-pubnames";
82 case eSectionTypeDWARFDebugPubTypes:
83 return "dwarf-pubtypes";
84 case eSectionTypeDWARFDebugRanges:
85 return "dwarf-ranges";
86 case eSectionTypeDWARFDebugStr:
87 return "dwarf-str";
88 case eSectionTypeDWARFDebugStrOffsets:
89 return "dwarf-str-offsets";
90 case eSectionTypeELFSymbolTable:
91 return "elf-symbol-table";
92 case eSectionTypeELFDynamicSymbols:
93 return "elf-dynamic-symbols";
94 case eSectionTypeELFRelocationEntries:
95 return "elf-relocation-entries";
96 case eSectionTypeELFDynamicLinkInfo:
97 return "elf-dynamic-link-info";
98 case eSectionTypeDWARFAppleNames:
99 return "apple-names";
100 case eSectionTypeDWARFAppleTypes:
101 return "apple-types";
102 case eSectionTypeDWARFAppleNamespaces:
103 return "apple-namespaces";
104 case eSectionTypeDWARFAppleObjC:
105 return "apple-objc";
106 case eSectionTypeEHFrame:
107 return "eh-frame";
108 case eSectionTypeARMexidx:
109 return "ARM.exidx";
110 case eSectionTypeARMextab:
111 return "ARM.extab";
112 case eSectionTypeCompactUnwind:
113 return "compact-unwind";
114 case eSectionTypeGoSymtab:
115 return "go-symtab";
116 case eSectionTypeAbsoluteAddress:
117 return "absolute";
118 case eSectionTypeOther:
119 return "regular";
120 }
121 return "unknown";
122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
125 user_id_t sect_id, const ConstString &name,
126 SectionType sect_type, addr_t file_addr, addr_t byte_size,
127 lldb::offset_t file_offset, lldb::offset_t file_size,
128 uint32_t log2align, uint32_t flags,
Greg Clayton3385fa02016-06-09 16:34:06 +0000129 uint32_t target_byte_size /*=1*/)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
131 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
132 m_file_addr(file_addr), m_byte_size(byte_size),
133 m_file_offset(file_offset), m_file_size(file_size),
134 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
135 m_thread_specific(false), m_readable(false), m_writable(false),
136 m_executable(false), m_target_byte_size(target_byte_size) {
137 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
138 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
139 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
140 // this, module_sp.get(), sect_id, file_addr, file_addr +
141 // byte_size, file_offset, file_offset + file_size, flags,
142 // name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145Section::Section(const lldb::SectionSP &parent_section_sp,
146 const ModuleSP &module_sp, ObjectFile *obj_file,
147 user_id_t sect_id, const ConstString &name,
148 SectionType sect_type, addr_t file_addr, addr_t byte_size,
149 lldb::offset_t file_offset, lldb::offset_t file_size,
150 uint32_t log2align, uint32_t flags,
151 uint32_t target_byte_size /*=1*/)
152 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
153 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
154 m_file_addr(file_addr), m_byte_size(byte_size),
155 m_file_offset(file_offset), m_file_size(file_size),
156 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
157 m_thread_specific(false), m_readable(false), m_writable(false),
158 m_executable(false), m_target_byte_size(target_byte_size) {
159 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
160 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
161 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
162 // this, module_sp.get(), sect_id, file_addr, file_addr +
163 // byte_size, file_offset, file_offset + file_size, flags,
164 // parent_section_sp->GetName().GetCString(), name.GetCString());
165 if (parent_section_sp)
166 m_parent_wp = parent_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169Section::~Section() {
170 // printf ("Section::~Section(%p)\n", this);
171}
172
173addr_t Section::GetFileAddress() const {
174 SectionSP parent_sp(GetParent());
175 if (parent_sp) {
176 // This section has a parent which means m_file_addr is an offset into
177 // the parent section, so the file address for this section is the file
178 // address of the parent plus the offset
179 return parent_sp->GetFileAddress() + m_file_addr;
180 }
181 // This section has no parent, so m_file_addr is the file base address
182 return m_file_addr;
183}
184
185bool Section::SetFileAddress(lldb::addr_t file_addr) {
186 SectionSP parent_sp(GetParent());
187 if (parent_sp) {
188 if (m_file_addr >= file_addr)
189 return parent_sp->SetFileAddress(m_file_addr - file_addr);
190 return false;
191 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 // This section has no parent, so m_file_addr is the file base address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 m_file_addr = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198lldb::addr_t Section::GetOffset() const {
199 // This section has a parent which means m_file_addr is an offset.
200 SectionSP parent_sp(GetParent());
201 if (parent_sp)
202 return m_file_addr;
203
204 // This section has no parent, so there is no offset to be had
205 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208addr_t Section::GetLoadBaseAddress(Target *target) const {
209 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
210 SectionSP parent_sp(GetParent());
211 if (parent_sp) {
212 load_base_addr = parent_sp->GetLoadBaseAddress(target);
213 if (load_base_addr != LLDB_INVALID_ADDRESS)
214 load_base_addr += GetOffset();
215 }
216 if (load_base_addr == LLDB_INVALID_ADDRESS) {
217 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
218 const_cast<Section *>(this)->shared_from_this());
219 }
220 return load_base_addr;
221}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222
Pavel Labathc3c72122017-06-08 13:26:35 +0000223bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr,
224 bool allow_section_end) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 const size_t num_children = m_children.GetSize();
Pavel Labathc3c72122017-06-08 13:26:35 +0000226 for (size_t i = 0; i < num_children; i++) {
227 Section *child_section = m_children.GetSectionAtIndex(i).get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228
Pavel Labathc3c72122017-06-08 13:26:35 +0000229 addr_t child_offset = child_section->GetOffset();
230 if (child_offset <= offset &&
231 offset - child_offset <
232 child_section->GetByteSize() + (allow_section_end ? 1 : 0))
233 return child_section->ResolveContainedAddress(offset - child_offset,
234 so_addr, allow_section_end);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 }
236 so_addr.SetOffset(offset);
237 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
238
239#ifdef LLDB_CONFIGURATION_DEBUG
240 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
241 // sections.
242 assert(GetModule().get());
243#endif
244 return true;
245}
246
247bool Section::ContainsFileAddress(addr_t vm_addr) const {
248 const addr_t file_addr = GetFileAddress();
249 if (file_addr != LLDB_INVALID_ADDRESS) {
250 if (file_addr <= vm_addr) {
251 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
252 return offset < GetByteSize();
253 }
254 }
255 return false;
256}
257
258int Section::Compare(const Section &a, const Section &b) {
259 if (&a == &b)
260 return 0;
261
262 const ModuleSP a_module_sp = a.GetModule();
263 const ModuleSP b_module_sp = b.GetModule();
264 if (a_module_sp == b_module_sp) {
265 user_id_t a_sect_uid = a.GetID();
266 user_id_t b_sect_uid = b.GetID();
267 if (a_sect_uid < b_sect_uid)
268 return -1;
269 if (a_sect_uid > b_sect_uid)
270 return 1;
271 return 0;
272 } else {
273 // The modules are different, just compare the module pointers
274 if (a_module_sp.get() < b_module_sp.get())
275 return -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 return 1; // We already know the modules aren't equal
278 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279}
280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
282 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
283 s->Indent();
284 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(),
285 GetSectionTypeAsCString(m_type));
286 bool resolved = true;
287 addr_t addr = LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (GetByteSize() == 0)
290 s->Printf("%39s", "");
291 else {
292 if (target)
293 addr = GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 if (addr == LLDB_INVALID_ADDRESS) {
296 if (target)
297 resolved = false;
298 addr = GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 }
300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 VMRange range(addr, addr + m_byte_size);
302 range.Dump(s, 0);
303 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
306 resolved ? ' ' : '*', m_readable ? 'r' : '-',
307 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
308 m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 s->EOL();
313
314 if (depth > 0)
315 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316}
317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318void Section::DumpName(Stream *s) const {
319 SectionSP parent_sp(GetParent());
320 if (parent_sp) {
321 parent_sp->DumpName(s);
322 s->PutChar('.');
323 } else {
324 // The top most section prints the module basename
325 const char *name = NULL;
326 ModuleSP module_sp(GetModule());
327 const FileSpec &file_spec = m_obj_file->GetFileSpec();
Michael Sartaina7499c92013-07-01 19:45:50 +0000328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 if (m_obj_file)
330 name = file_spec.GetFilename().AsCString();
331 if ((!name || !name[0]) && module_sp)
332 name = module_sp->GetFileSpec().GetFilename().AsCString();
333 if (name && name[0])
334 s->Printf("%s.", name);
335 }
336 m_name.Dump(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339bool Section::IsDescendant(const Section *section) {
340 if (this == section)
341 return true;
342 SectionSP parent_sp(GetParent());
343 if (parent_sp)
344 return parent_sp->IsDescendant(section);
345 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348bool Section::Slide(addr_t slide_amount, bool slide_children) {
349 if (m_file_addr != LLDB_INVALID_ADDRESS) {
350 if (slide_amount == 0)
351 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 m_file_addr += slide_amount;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 if (slide_children)
356 m_children.Slide(slide_amount, slide_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 return true;
359 }
360 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361}
362
Greg Clayton3385fa02016-06-09 16:34:06 +0000363//------------------------------------------------------------------
364/// Get the permissions as OR'ed bits from lldb::Permissions
365//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366uint32_t Section::GetPermissions() const {
367 uint32_t permissions = 0;
368 if (m_readable)
369 permissions |= ePermissionsReadable;
370 if (m_writable)
371 permissions |= ePermissionsWritable;
372 if (m_executable)
373 permissions |= ePermissionsExecutable;
374 return permissions;
Greg Clayton3385fa02016-06-09 16:34:06 +0000375}
376
377//------------------------------------------------------------------
378/// Set the permissions using bits OR'ed from lldb::Permissions
379//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380void Section::SetPermissions(uint32_t permissions) {
381 m_readable = (permissions & ePermissionsReadable) != 0;
382 m_writable = (permissions & ePermissionsWritable) != 0;
383 m_executable = (permissions & ePermissionsExecutable) != 0;
Greg Clayton3385fa02016-06-09 16:34:06 +0000384}
385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386lldb::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;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000391}
392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393lldb::offset_t Section::GetSectionData(DataExtractor &section_data) const {
394 if (m_obj_file)
395 return m_obj_file->ReadSectionData(this, section_data);
396 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000397}
398
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399#pragma mark SectionList
400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401SectionList::SectionList() : m_sections() {}
402
403SectionList::~SectionList() {}
404
405SectionList &SectionList::operator=(const SectionList &rhs) {
406 if (this != &rhs)
407 m_sections = rhs.m_sections;
408 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409}
410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411size_t SectionList::AddSection(const lldb::SectionSP &section_sp) {
412 if (section_sp) {
413 size_t section_index = m_sections.size();
414 m_sections.push_back(section_sp);
415 return section_index;
416 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 return std::numeric_limits<size_t>::max();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Michael Sartaina7499c92013-07-01 19:45:50 +0000421// Warning, this can be slow as it's removing items from a std::vector.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422bool SectionList::DeleteSection(size_t idx) {
423 if (idx < m_sections.size()) {
424 m_sections.erase(m_sections.begin() + idx);
425 return true;
426 }
427 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000428}
429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430size_t SectionList::FindSectionIndex(const Section *sect) {
431 iterator sect_iter;
432 iterator begin = m_sections.begin();
433 iterator end = m_sections.end();
434 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
435 if (sect_iter->get() == sect) {
436 // The secton was already in this section list
437 return std::distance(begin, sect_iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 }
440 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441}
442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443size_t SectionList::AddUniqueSection(const lldb::SectionSP &sect_sp) {
444 size_t sect_idx = FindSectionIndex(sect_sp.get());
445 if (sect_idx == UINT32_MAX) {
446 sect_idx = AddSection(sect_sp);
447 }
448 return sect_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449}
450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451bool SectionList::ReplaceSection(user_id_t sect_id,
452 const lldb::SectionSP &sect_sp,
453 uint32_t depth) {
454 iterator sect_iter, end = m_sections.end();
455 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
456 if ((*sect_iter)->GetID() == sect_id) {
457 *sect_iter = sect_sp;
458 return true;
459 } else if (depth > 0) {
460 if ((*sect_iter)
461 ->GetChildren()
462 .ReplaceSection(sect_id, sect_sp, depth - 1))
463 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 }
466 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467}
468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469size_t SectionList::GetNumSections(uint32_t depth) const {
470 size_t count = m_sections.size();
471 if (depth > 0) {
472 const_iterator sect_iter, end = m_sections.end();
473 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
474 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 }
477 return count;
478}
479
480SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
481 SectionSP sect_sp;
482 if (idx < m_sections.size())
483 sect_sp = m_sections[idx];
484 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
487SectionSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488SectionList::FindSectionByName(const ConstString &section_dstr) const {
489 SectionSP sect_sp;
490 // Check if we have a valid section string
491 if (section_dstr && !m_sections.empty()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 const_iterator sect_iter;
493 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 for (sect_iter = m_sections.begin();
495 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
496 Section *child_section = sect_iter->get();
497 if (child_section) {
498 if (child_section->GetName() == section_dstr) {
499 sect_sp = *sect_iter;
500 } else {
501 sect_sp =
502 child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 }
507 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508}
509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
511 SectionSP sect_sp;
512 if (sect_id) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 const_iterator sect_iter;
514 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 for (sect_iter = m_sections.begin();
516 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
517 if ((*sect_iter)->GetID() == sect_id) {
518 sect_sp = *sect_iter;
519 break;
520 } else {
521 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
522 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 }
525 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528SectionSP SectionList::FindSectionByType(SectionType sect_type,
529 bool check_children,
530 size_t start_idx) const {
531 SectionSP sect_sp;
532 size_t num_sections = m_sections.size();
533 for (size_t idx = start_idx; idx < num_sections; ++idx) {
534 if (m_sections[idx]->GetType() == sect_type) {
535 sect_sp = m_sections[idx];
536 break;
537 } else if (check_children) {
538 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
539 sect_type, check_children, 0);
540 if (sect_sp)
541 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 }
544 return sect_sp;
545}
546
547SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
548 uint32_t depth) const {
549 SectionSP sect_sp;
550 const_iterator sect_iter;
551 const_iterator end = m_sections.end();
552 for (sect_iter = m_sections.begin();
553 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
554 Section *sect = sect_iter->get();
555 if (sect->ContainsFileAddress(vm_addr)) {
556 // The file address is in this section. We need to make sure one of our
557 // child
558 // sections doesn't contain this address as well as obeying the depth
559 // limit
560 // that was passed in.
561 if (depth > 0)
562 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
563 vm_addr, depth - 1);
564
565 if (sect_sp.get() == NULL && !sect->IsFake())
566 sect_sp = *sect_iter;
567 }
568 }
569 return sect_sp;
570}
571
572bool SectionList::ContainsSection(user_id_t sect_id) const {
573 return FindSectionByID(sect_id).get() != NULL;
574}
575
576void SectionList::Dump(Stream *s, Target *target, bool show_header,
577 uint32_t depth) const {
578 bool target_has_loaded_sections =
579 target && !target->GetSectionLoadList().IsEmpty();
580 if (show_header && !m_sections.empty()) {
581 s->Indent();
582 s->Printf("SectID Type %s Address "
583 " Perm File Off. File Size Flags "
584 " Section Name\n",
585 target_has_loaded_sections ? "Load" : "File");
586 s->Indent();
587 s->PutCString("---------- ---------------- "
588 "--------------------------------------- ---- ---------- "
589 "---------- "
590 "---------- ----------------------------\n");
591 }
592
593 const_iterator sect_iter;
594 const_iterator end = m_sections.end();
595 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
596 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
597 }
598
599 if (show_header && !m_sections.empty())
600 s->IndentLess();
601}
602
603size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
604 size_t count = 0;
605 const_iterator pos, end = m_sections.end();
606 for (pos = m_sections.begin(); pos != end; ++pos) {
607 if ((*pos)->Slide(slide_amount, slide_children))
608 ++count;
609 }
610 return count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611}