blob: 63add34ef2f97f89cb9f0c28fdcc5b04955ecb44 [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"
11#include "lldb/Core/Module.h"
12#include "lldb/Symbol/ObjectFile.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000013#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000014#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Zachary Turnerdf449882017-02-01 19:45:14 +000019static const char *GetSectionTypeAsCString(lldb::SectionType sect_type) {
20 switch (sect_type) {
21 case eSectionTypeInvalid:
22 return "invalid";
23 case eSectionTypeCode:
24 return "code";
25 case eSectionTypeContainer:
26 return "container";
27 case eSectionTypeData:
28 return "data";
29 case eSectionTypeDataCString:
30 return "data-cstr";
31 case eSectionTypeDataCStringPointers:
32 return "data-cstr-ptr";
33 case eSectionTypeDataSymbolAddress:
34 return "data-symbol-addr";
35 case eSectionTypeData4:
36 return "data-4-byte";
37 case eSectionTypeData8:
38 return "data-8-byte";
39 case eSectionTypeData16:
40 return "data-16-byte";
41 case eSectionTypeDataPointers:
42 return "data-ptrs";
43 case eSectionTypeDebug:
44 return "debug";
45 case eSectionTypeZeroFill:
46 return "zero-fill";
47 case eSectionTypeDataObjCMessageRefs:
48 return "objc-message-refs";
49 case eSectionTypeDataObjCCFStrings:
50 return "objc-cfstrings";
51 case eSectionTypeDWARFDebugAbbrev:
52 return "dwarf-abbrev";
53 case eSectionTypeDWARFDebugAddr:
54 return "dwarf-addr";
55 case eSectionTypeDWARFDebugAranges:
56 return "dwarf-aranges";
57 case eSectionTypeDWARFDebugFrame:
58 return "dwarf-frame";
59 case eSectionTypeDWARFDebugInfo:
60 return "dwarf-info";
61 case eSectionTypeDWARFDebugLine:
62 return "dwarf-line";
63 case eSectionTypeDWARFDebugLoc:
64 return "dwarf-loc";
65 case eSectionTypeDWARFDebugMacInfo:
66 return "dwarf-macinfo";
67 case eSectionTypeDWARFDebugMacro:
68 return "dwarf-macro";
69 case eSectionTypeDWARFDebugPubNames:
70 return "dwarf-pubnames";
71 case eSectionTypeDWARFDebugPubTypes:
72 return "dwarf-pubtypes";
73 case eSectionTypeDWARFDebugRanges:
74 return "dwarf-ranges";
75 case eSectionTypeDWARFDebugStr:
76 return "dwarf-str";
77 case eSectionTypeDWARFDebugStrOffsets:
78 return "dwarf-str-offsets";
79 case eSectionTypeELFSymbolTable:
80 return "elf-symbol-table";
81 case eSectionTypeELFDynamicSymbols:
82 return "elf-dynamic-symbols";
83 case eSectionTypeELFRelocationEntries:
84 return "elf-relocation-entries";
85 case eSectionTypeELFDynamicLinkInfo:
86 return "elf-dynamic-link-info";
87 case eSectionTypeDWARFAppleNames:
88 return "apple-names";
89 case eSectionTypeDWARFAppleTypes:
90 return "apple-types";
91 case eSectionTypeDWARFAppleNamespaces:
92 return "apple-namespaces";
93 case eSectionTypeDWARFAppleObjC:
94 return "apple-objc";
95 case eSectionTypeEHFrame:
96 return "eh-frame";
97 case eSectionTypeARMexidx:
98 return "ARM.exidx";
99 case eSectionTypeARMextab:
100 return "ARM.extab";
101 case eSectionTypeCompactUnwind:
102 return "compact-unwind";
103 case eSectionTypeGoSymtab:
104 return "go-symtab";
105 case eSectionTypeAbsoluteAddress:
106 return "absolute";
107 case eSectionTypeOther:
108 return "regular";
109 }
110 return "unknown";
111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file,
114 user_id_t sect_id, const ConstString &name,
115 SectionType sect_type, addr_t file_addr, addr_t byte_size,
116 lldb::offset_t file_offset, lldb::offset_t file_size,
117 uint32_t log2align, uint32_t flags,
Greg Clayton3385fa02016-06-09 16:34:06 +0000118 uint32_t target_byte_size /*=1*/)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
120 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
121 m_file_addr(file_addr), m_byte_size(byte_size),
122 m_file_offset(file_offset), m_file_size(file_size),
123 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
124 m_thread_specific(false), m_readable(false), m_writable(false),
125 m_executable(false), m_target_byte_size(target_byte_size) {
126 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
127 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
128 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
129 // this, module_sp.get(), sect_id, file_addr, file_addr +
130 // byte_size, file_offset, file_offset + file_size, flags,
131 // name.GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134Section::Section(const lldb::SectionSP &parent_section_sp,
135 const ModuleSP &module_sp, ObjectFile *obj_file,
136 user_id_t sect_id, const ConstString &name,
137 SectionType sect_type, addr_t file_addr, addr_t byte_size,
138 lldb::offset_t file_offset, lldb::offset_t file_size,
139 uint32_t log2align, uint32_t flags,
140 uint32_t target_byte_size /*=1*/)
141 : ModuleChild(module_sp), UserID(sect_id), Flags(flags),
142 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name),
143 m_file_addr(file_addr), m_byte_size(byte_size),
144 m_file_offset(file_offset), m_file_size(file_size),
145 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false),
146 m_thread_specific(false), m_readable(false), m_writable(false),
147 m_executable(false), m_target_byte_size(target_byte_size) {
148 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ",
149 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 "
150 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
151 // this, module_sp.get(), sect_id, file_addr, file_addr +
152 // byte_size, file_offset, file_offset + file_size, flags,
153 // parent_section_sp->GetName().GetCString(), name.GetCString());
154 if (parent_section_sp)
155 m_parent_wp = parent_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158Section::~Section() {
159 // printf ("Section::~Section(%p)\n", this);
160}
161
162addr_t Section::GetFileAddress() const {
163 SectionSP parent_sp(GetParent());
164 if (parent_sp) {
165 // This section has a parent which means m_file_addr is an offset into
166 // the parent section, so the file address for this section is the file
167 // address of the parent plus the offset
168 return parent_sp->GetFileAddress() + m_file_addr;
169 }
170 // This section has no parent, so m_file_addr is the file base address
171 return m_file_addr;
172}
173
174bool Section::SetFileAddress(lldb::addr_t file_addr) {
175 SectionSP parent_sp(GetParent());
176 if (parent_sp) {
177 if (m_file_addr >= file_addr)
178 return parent_sp->SetFileAddress(m_file_addr - file_addr);
179 return false;
180 } else {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 // This section has no parent, so m_file_addr is the file base address
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 m_file_addr = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187lldb::addr_t Section::GetOffset() const {
188 // This section has a parent which means m_file_addr is an offset.
189 SectionSP parent_sp(GetParent());
190 if (parent_sp)
191 return m_file_addr;
192
193 // This section has no parent, so there is no offset to be had
194 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197addr_t Section::GetLoadBaseAddress(Target *target) const {
198 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
199 SectionSP parent_sp(GetParent());
200 if (parent_sp) {
201 load_base_addr = parent_sp->GetLoadBaseAddress(target);
202 if (load_base_addr != LLDB_INVALID_ADDRESS)
203 load_base_addr += GetOffset();
204 }
205 if (load_base_addr == LLDB_INVALID_ADDRESS) {
206 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress(
207 const_cast<Section *>(this)->shared_from_this());
208 }
209 return load_base_addr;
210}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr) const {
213 const size_t num_children = m_children.GetSize();
214 if (num_children > 0) {
215 for (size_t i = 0; i < num_children; i++) {
216 Section *child_section = m_children.GetSectionAtIndex(i).get();
217
218 addr_t child_offset = child_section->GetOffset();
219 if (child_offset <= offset &&
220 offset - child_offset < child_section->GetByteSize())
221 return child_section->ResolveContainedAddress(offset - child_offset,
222 so_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 }
225 so_addr.SetOffset(offset);
226 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
227
228#ifdef LLDB_CONFIGURATION_DEBUG
229 // For debug builds, ensure that there are no orphaned (i.e., moduleless)
230 // sections.
231 assert(GetModule().get());
232#endif
233 return true;
234}
235
236bool Section::ContainsFileAddress(addr_t vm_addr) const {
237 const addr_t file_addr = GetFileAddress();
238 if (file_addr != LLDB_INVALID_ADDRESS) {
239 if (file_addr <= vm_addr) {
240 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size;
241 return offset < GetByteSize();
242 }
243 }
244 return false;
245}
246
247int Section::Compare(const Section &a, const Section &b) {
248 if (&a == &b)
249 return 0;
250
251 const ModuleSP a_module_sp = a.GetModule();
252 const ModuleSP b_module_sp = b.GetModule();
253 if (a_module_sp == b_module_sp) {
254 user_id_t a_sect_uid = a.GetID();
255 user_id_t b_sect_uid = b.GetID();
256 if (a_sect_uid < b_sect_uid)
257 return -1;
258 if (a_sect_uid > b_sect_uid)
259 return 1;
260 return 0;
261 } else {
262 // The modules are different, just compare the module pointers
263 if (a_module_sp.get() < b_module_sp.get())
264 return -1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 return 1; // We already know the modules aren't equal
267 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270void Section::Dump(Stream *s, Target *target, uint32_t depth) const {
271 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
272 s->Indent();
273 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(),
274 GetSectionTypeAsCString(m_type));
275 bool resolved = true;
276 addr_t addr = LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (GetByteSize() == 0)
279 s->Printf("%39s", "");
280 else {
281 if (target)
282 addr = GetLoadBaseAddress(target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 if (addr == LLDB_INVALID_ADDRESS) {
285 if (target)
286 resolved = false;
287 addr = GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 }
289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 VMRange range(addr, addr + m_byte_size);
291 range.Dump(s, 0);
292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ",
295 resolved ? ' ' : '*', m_readable ? 'r' : '-',
296 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset,
297 m_file_size, Get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 DumpName(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 s->EOL();
302
303 if (depth > 0)
304 m_children.Dump(s, target, false, depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305}
306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307void Section::DumpName(Stream *s) const {
308 SectionSP parent_sp(GetParent());
309 if (parent_sp) {
310 parent_sp->DumpName(s);
311 s->PutChar('.');
312 } else {
313 // The top most section prints the module basename
314 const char *name = NULL;
315 ModuleSP module_sp(GetModule());
316 const FileSpec &file_spec = m_obj_file->GetFileSpec();
Michael Sartaina7499c92013-07-01 19:45:50 +0000317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 if (m_obj_file)
319 name = file_spec.GetFilename().AsCString();
320 if ((!name || !name[0]) && module_sp)
321 name = module_sp->GetFileSpec().GetFilename().AsCString();
322 if (name && name[0])
323 s->Printf("%s.", name);
324 }
325 m_name.Dump(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328bool Section::IsDescendant(const Section *section) {
329 if (this == section)
330 return true;
331 SectionSP parent_sp(GetParent());
332 if (parent_sp)
333 return parent_sp->IsDescendant(section);
334 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335}
336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337bool Section::Slide(addr_t slide_amount, bool slide_children) {
338 if (m_file_addr != LLDB_INVALID_ADDRESS) {
339 if (slide_amount == 0)
340 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 m_file_addr += slide_amount;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (slide_children)
345 m_children.Slide(slide_amount, slide_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 return true;
348 }
349 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Greg Clayton3385fa02016-06-09 16:34:06 +0000352//------------------------------------------------------------------
353/// Get the permissions as OR'ed bits from lldb::Permissions
354//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355uint32_t Section::GetPermissions() const {
356 uint32_t permissions = 0;
357 if (m_readable)
358 permissions |= ePermissionsReadable;
359 if (m_writable)
360 permissions |= ePermissionsWritable;
361 if (m_executable)
362 permissions |= ePermissionsExecutable;
363 return permissions;
Greg Clayton3385fa02016-06-09 16:34:06 +0000364}
365
366//------------------------------------------------------------------
367/// Set the permissions using bits OR'ed from lldb::Permissions
368//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369void Section::SetPermissions(uint32_t permissions) {
370 m_readable = (permissions & ePermissionsReadable) != 0;
371 m_writable = (permissions & ePermissionsWritable) != 0;
372 m_executable = (permissions & ePermissionsExecutable) != 0;
Greg Clayton3385fa02016-06-09 16:34:06 +0000373}
374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len,
376 lldb::offset_t offset) {
377 if (m_obj_file)
378 return m_obj_file->ReadSectionData(this, offset, dst, dst_len);
379 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000380}
381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382lldb::offset_t Section::GetSectionData(DataExtractor &section_data) const {
383 if (m_obj_file)
384 return m_obj_file->ReadSectionData(this, section_data);
385 return 0;
Jim Ingham1c58d5a2015-11-04 01:02:43 +0000386}
387
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388#pragma mark SectionList
389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390SectionList::SectionList() : m_sections() {}
391
392SectionList::~SectionList() {}
393
394SectionList &SectionList::operator=(const SectionList &rhs) {
395 if (this != &rhs)
396 m_sections = rhs.m_sections;
397 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400size_t SectionList::AddSection(const lldb::SectionSP &section_sp) {
401 if (section_sp) {
402 size_t section_index = m_sections.size();
403 m_sections.push_back(section_sp);
404 return section_index;
405 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 return std::numeric_limits<size_t>::max();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408}
409
Michael Sartaina7499c92013-07-01 19:45:50 +0000410// Warning, this can be slow as it's removing items from a std::vector.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411bool SectionList::DeleteSection(size_t idx) {
412 if (idx < m_sections.size()) {
413 m_sections.erase(m_sections.begin() + idx);
414 return true;
415 }
416 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000417}
418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419size_t SectionList::FindSectionIndex(const Section *sect) {
420 iterator sect_iter;
421 iterator begin = m_sections.begin();
422 iterator end = m_sections.end();
423 for (sect_iter = begin; sect_iter != end; ++sect_iter) {
424 if (sect_iter->get() == sect) {
425 // The secton was already in this section list
426 return std::distance(begin, sect_iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 }
429 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432size_t SectionList::AddUniqueSection(const lldb::SectionSP &sect_sp) {
433 size_t sect_idx = FindSectionIndex(sect_sp.get());
434 if (sect_idx == UINT32_MAX) {
435 sect_idx = AddSection(sect_sp);
436 }
437 return sect_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440bool SectionList::ReplaceSection(user_id_t sect_id,
441 const lldb::SectionSP &sect_sp,
442 uint32_t depth) {
443 iterator sect_iter, end = m_sections.end();
444 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
445 if ((*sect_iter)->GetID() == sect_id) {
446 *sect_iter = sect_sp;
447 return true;
448 } else if (depth > 0) {
449 if ((*sect_iter)
450 ->GetChildren()
451 .ReplaceSection(sect_id, sect_sp, depth - 1))
452 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 }
455 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458size_t SectionList::GetNumSections(uint32_t depth) const {
459 size_t count = m_sections.size();
460 if (depth > 0) {
461 const_iterator sect_iter, end = m_sections.end();
462 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
463 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 }
466 return count;
467}
468
469SectionSP SectionList::GetSectionAtIndex(size_t idx) const {
470 SectionSP sect_sp;
471 if (idx < m_sections.size())
472 sect_sp = m_sections[idx];
473 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
476SectionSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477SectionList::FindSectionByName(const ConstString &section_dstr) const {
478 SectionSP sect_sp;
479 // Check if we have a valid section string
480 if (section_dstr && !m_sections.empty()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 const_iterator sect_iter;
482 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 for (sect_iter = m_sections.begin();
484 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
485 Section *child_section = sect_iter->get();
486 if (child_section) {
487 if (child_section->GetName() == section_dstr) {
488 sect_sp = *sect_iter;
489 } else {
490 sect_sp =
491 child_section->GetChildren().FindSectionByName(section_dstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 }
496 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499SectionSP SectionList::FindSectionByID(user_id_t sect_id) const {
500 SectionSP sect_sp;
501 if (sect_id) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 const_iterator sect_iter;
503 const_iterator end = m_sections.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 for (sect_iter = m_sections.begin();
505 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
506 if ((*sect_iter)->GetID() == sect_id) {
507 sect_sp = *sect_iter;
508 break;
509 } else {
510 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id);
511 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 }
514 return sect_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515}
516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517SectionSP SectionList::FindSectionByType(SectionType sect_type,
518 bool check_children,
519 size_t start_idx) const {
520 SectionSP sect_sp;
521 size_t num_sections = m_sections.size();
522 for (size_t idx = start_idx; idx < num_sections; ++idx) {
523 if (m_sections[idx]->GetType() == sect_type) {
524 sect_sp = m_sections[idx];
525 break;
526 } else if (check_children) {
527 sect_sp = m_sections[idx]->GetChildren().FindSectionByType(
528 sect_type, check_children, 0);
529 if (sect_sp)
530 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 }
533 return sect_sp;
534}
535
536SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr,
537 uint32_t depth) const {
538 SectionSP sect_sp;
539 const_iterator sect_iter;
540 const_iterator end = m_sections.end();
541 for (sect_iter = m_sections.begin();
542 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) {
543 Section *sect = sect_iter->get();
544 if (sect->ContainsFileAddress(vm_addr)) {
545 // The file address is in this section. We need to make sure one of our
546 // child
547 // sections doesn't contain this address as well as obeying the depth
548 // limit
549 // that was passed in.
550 if (depth > 0)
551 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(
552 vm_addr, depth - 1);
553
554 if (sect_sp.get() == NULL && !sect->IsFake())
555 sect_sp = *sect_iter;
556 }
557 }
558 return sect_sp;
559}
560
561bool SectionList::ContainsSection(user_id_t sect_id) const {
562 return FindSectionByID(sect_id).get() != NULL;
563}
564
565void SectionList::Dump(Stream *s, Target *target, bool show_header,
566 uint32_t depth) const {
567 bool target_has_loaded_sections =
568 target && !target->GetSectionLoadList().IsEmpty();
569 if (show_header && !m_sections.empty()) {
570 s->Indent();
571 s->Printf("SectID Type %s Address "
572 " Perm File Off. File Size Flags "
573 " Section Name\n",
574 target_has_loaded_sections ? "Load" : "File");
575 s->Indent();
576 s->PutCString("---------- ---------------- "
577 "--------------------------------------- ---- ---------- "
578 "---------- "
579 "---------- ----------------------------\n");
580 }
581
582 const_iterator sect_iter;
583 const_iterator end = m_sections.end();
584 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) {
585 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
586 }
587
588 if (show_header && !m_sections.empty())
589 s->IndentLess();
590}
591
592size_t SectionList::Slide(addr_t slide_amount, bool slide_children) {
593 size_t count = 0;
594 const_iterator pos, end = m_sections.end();
595 for (pos = m_sections.begin(); pos != end; ++pos) {
596 if ((*pos)->Slide(slide_amount, slide_children))
597 ++count;
598 }
599 return count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}