1 | //===-- Section.cpp -------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Core/Section.h" |
10 | #include "lldb/Core/Address.h" |
11 | #include "lldb/Core/Module.h" |
12 | #include "lldb/Symbol/ObjectFile.h" |
13 | #include "lldb/Target/SectionLoadList.h" |
14 | #include "lldb/Target/Target.h" |
15 | #include "lldb/Utility/FileSpec.h" |
16 | #include "lldb/Utility/VMRange.h" |
17 | |
18 | #include <cinttypes> |
19 | #include <limits> |
20 | #include <utility> |
21 | |
22 | namespace lldb_private { |
23 | class ; |
24 | } |
25 | using namespace lldb; |
26 | using namespace lldb_private; |
27 | |
28 | const char *Section::GetTypeAsCString() const { |
29 | switch (m_type) { |
30 | case eSectionTypeInvalid: |
31 | return "invalid" ; |
32 | case eSectionTypeCode: |
33 | return "code" ; |
34 | case eSectionTypeContainer: |
35 | return "container" ; |
36 | case eSectionTypeData: |
37 | return "data" ; |
38 | case eSectionTypeDataCString: |
39 | return "data-cstr" ; |
40 | case eSectionTypeDataCStringPointers: |
41 | return "data-cstr-ptr" ; |
42 | case eSectionTypeDataSymbolAddress: |
43 | return "data-symbol-addr" ; |
44 | case eSectionTypeData4: |
45 | return "data-4-byte" ; |
46 | case eSectionTypeData8: |
47 | return "data-8-byte" ; |
48 | case eSectionTypeData16: |
49 | return "data-16-byte" ; |
50 | case eSectionTypeDataPointers: |
51 | return "data-ptrs" ; |
52 | case eSectionTypeDebug: |
53 | return "debug" ; |
54 | case eSectionTypeZeroFill: |
55 | return "zero-fill" ; |
56 | case eSectionTypeDataObjCMessageRefs: |
57 | return "objc-message-refs" ; |
58 | case eSectionTypeDataObjCCFStrings: |
59 | return "objc-cfstrings" ; |
60 | case eSectionTypeDWARFDebugAbbrev: |
61 | return "dwarf-abbrev" ; |
62 | case eSectionTypeDWARFDebugAbbrevDwo: |
63 | return "dwarf-abbrev-dwo" ; |
64 | case eSectionTypeDWARFDebugAddr: |
65 | return "dwarf-addr" ; |
66 | case eSectionTypeDWARFDebugAranges: |
67 | return "dwarf-aranges" ; |
68 | case eSectionTypeDWARFDebugCuIndex: |
69 | return "dwarf-cu-index" ; |
70 | case eSectionTypeDWARFDebugTuIndex: |
71 | return "dwarf-tu-index" ; |
72 | case eSectionTypeDWARFDebugFrame: |
73 | return "dwarf-frame" ; |
74 | case eSectionTypeDWARFDebugInfo: |
75 | return "dwarf-info" ; |
76 | case eSectionTypeDWARFDebugInfoDwo: |
77 | return "dwarf-info-dwo" ; |
78 | case eSectionTypeDWARFDebugLine: |
79 | return "dwarf-line" ; |
80 | case eSectionTypeDWARFDebugLineStr: |
81 | return "dwarf-line-str" ; |
82 | case eSectionTypeDWARFDebugLoc: |
83 | return "dwarf-loc" ; |
84 | case eSectionTypeDWARFDebugLocDwo: |
85 | return "dwarf-loc-dwo" ; |
86 | case eSectionTypeDWARFDebugLocLists: |
87 | return "dwarf-loclists" ; |
88 | case eSectionTypeDWARFDebugLocListsDwo: |
89 | return "dwarf-loclists-dwo" ; |
90 | case eSectionTypeDWARFDebugMacInfo: |
91 | return "dwarf-macinfo" ; |
92 | case eSectionTypeDWARFDebugMacro: |
93 | return "dwarf-macro" ; |
94 | case eSectionTypeDWARFDebugPubNames: |
95 | return "dwarf-pubnames" ; |
96 | case eSectionTypeDWARFDebugPubTypes: |
97 | return "dwarf-pubtypes" ; |
98 | case eSectionTypeDWARFDebugRanges: |
99 | return "dwarf-ranges" ; |
100 | case eSectionTypeDWARFDebugRngLists: |
101 | return "dwarf-rnglists" ; |
102 | case eSectionTypeDWARFDebugRngListsDwo: |
103 | return "dwarf-rnglists-dwo" ; |
104 | case eSectionTypeDWARFDebugStr: |
105 | return "dwarf-str" ; |
106 | case eSectionTypeDWARFDebugStrDwo: |
107 | return "dwarf-str-dwo" ; |
108 | case eSectionTypeDWARFDebugStrOffsets: |
109 | return "dwarf-str-offsets" ; |
110 | case eSectionTypeDWARFDebugStrOffsetsDwo: |
111 | return "dwarf-str-offsets-dwo" ; |
112 | case eSectionTypeDWARFDebugTypes: |
113 | return "dwarf-types" ; |
114 | case eSectionTypeDWARFDebugTypesDwo: |
115 | return "dwarf-types-dwo" ; |
116 | case eSectionTypeDWARFDebugNames: |
117 | return "dwarf-names" ; |
118 | case eSectionTypeELFSymbolTable: |
119 | return "elf-symbol-table" ; |
120 | case eSectionTypeELFDynamicSymbols: |
121 | return "elf-dynamic-symbols" ; |
122 | case eSectionTypeELFRelocationEntries: |
123 | return "elf-relocation-entries" ; |
124 | case eSectionTypeELFDynamicLinkInfo: |
125 | return "elf-dynamic-link-info" ; |
126 | case eSectionTypeDWARFAppleNames: |
127 | return "apple-names" ; |
128 | case eSectionTypeDWARFAppleTypes: |
129 | return "apple-types" ; |
130 | case eSectionTypeDWARFAppleNamespaces: |
131 | return "apple-namespaces" ; |
132 | case eSectionTypeDWARFAppleObjC: |
133 | return "apple-objc" ; |
134 | case eSectionTypeEHFrame: |
135 | return "eh-frame" ; |
136 | case eSectionTypeARMexidx: |
137 | return "ARM.exidx" ; |
138 | case eSectionTypeARMextab: |
139 | return "ARM.extab" ; |
140 | case eSectionTypeCompactUnwind: |
141 | return "compact-unwind" ; |
142 | case eSectionTypeGoSymtab: |
143 | return "go-symtab" ; |
144 | case eSectionTypeAbsoluteAddress: |
145 | return "absolute" ; |
146 | case eSectionTypeDWARFGNUDebugAltLink: |
147 | return "dwarf-gnu-debugaltlink" ; |
148 | case eSectionTypeCTF: |
149 | return "ctf" ; |
150 | case eSectionTypeLLDBTypeSummaries: |
151 | return "lldb-type-summaries" ; |
152 | case eSectionTypeLLDBFormatters: |
153 | return "lldb-formatters" ; |
154 | case eSectionTypeSwiftModules: |
155 | return "swift-modules" ; |
156 | case eSectionTypeOther: |
157 | return "regular" ; |
158 | } |
159 | return "unknown" ; |
160 | } |
161 | |
162 | Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file, |
163 | user_id_t sect_id, ConstString name, SectionType sect_type, |
164 | addr_t file_addr, addr_t byte_size, lldb::offset_t file_offset, |
165 | lldb::offset_t file_size, uint32_t log2align, uint32_t flags, |
166 | uint32_t target_byte_size /*=1*/) |
167 | : ModuleChild(module_sp), UserID(sect_id), Flags(flags), |
168 | m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), |
169 | m_file_addr(file_addr), m_byte_size(byte_size), |
170 | m_file_offset(file_offset), m_file_size(file_size), |
171 | m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), |
172 | m_thread_specific(false), m_readable(false), m_writable(false), |
173 | m_executable(false), m_relocated(false), |
174 | m_target_byte_size(target_byte_size) {} |
175 | |
176 | Section::Section(const lldb::SectionSP &parent_section_sp, |
177 | const ModuleSP &module_sp, ObjectFile *obj_file, |
178 | user_id_t sect_id, ConstString name, SectionType sect_type, |
179 | addr_t file_addr, addr_t byte_size, lldb::offset_t file_offset, |
180 | lldb::offset_t file_size, uint32_t log2align, uint32_t flags, |
181 | uint32_t target_byte_size /*=1*/) |
182 | : ModuleChild(module_sp), UserID(sect_id), Flags(flags), |
183 | m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), |
184 | m_file_addr(file_addr), m_byte_size(byte_size), |
185 | m_file_offset(file_offset), m_file_size(file_size), |
186 | m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), |
187 | m_thread_specific(false), m_readable(false), m_writable(false), |
188 | m_executable(false), m_relocated(false), |
189 | m_target_byte_size(target_byte_size) { |
190 | if (parent_section_sp) |
191 | m_parent_wp = parent_section_sp; |
192 | } |
193 | |
194 | Section::~Section() = default; |
195 | |
196 | addr_t Section::GetFileAddress() const { |
197 | SectionSP parent_sp(GetParent()); |
198 | if (parent_sp) { |
199 | // This section has a parent which means m_file_addr is an offset into the |
200 | // parent section, so the file address for this section is the file address |
201 | // of the parent plus the offset |
202 | return parent_sp->GetFileAddress() + m_file_addr; |
203 | } |
204 | // This section has no parent, so m_file_addr is the file base address |
205 | return m_file_addr; |
206 | } |
207 | |
208 | bool Section::SetFileAddress(lldb::addr_t file_addr) { |
209 | SectionSP parent_sp(GetParent()); |
210 | if (parent_sp) { |
211 | if (m_file_addr >= file_addr) |
212 | return parent_sp->SetFileAddress(m_file_addr - file_addr); |
213 | return false; |
214 | } else { |
215 | // This section has no parent, so m_file_addr is the file base address |
216 | m_file_addr = file_addr; |
217 | return true; |
218 | } |
219 | } |
220 | |
221 | lldb::addr_t Section::GetOffset() const { |
222 | // This section has a parent which means m_file_addr is an offset. |
223 | SectionSP parent_sp(GetParent()); |
224 | if (parent_sp) |
225 | return m_file_addr; |
226 | |
227 | // This section has no parent, so there is no offset to be had |
228 | return 0; |
229 | } |
230 | |
231 | addr_t Section::GetLoadBaseAddress(Target *target) const { |
232 | addr_t load_base_addr = LLDB_INVALID_ADDRESS; |
233 | SectionSP parent_sp(GetParent()); |
234 | if (parent_sp) { |
235 | load_base_addr = parent_sp->GetLoadBaseAddress(target); |
236 | if (load_base_addr != LLDB_INVALID_ADDRESS) |
237 | load_base_addr += GetOffset(); |
238 | } |
239 | if (load_base_addr == LLDB_INVALID_ADDRESS) { |
240 | load_base_addr = target->GetSectionLoadAddress( |
241 | section_sp: const_cast<Section *>(this)->shared_from_this()); |
242 | } |
243 | return load_base_addr; |
244 | } |
245 | |
246 | bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr, |
247 | bool allow_section_end) const { |
248 | const size_t num_children = m_children.GetSize(); |
249 | for (size_t i = 0; i < num_children; i++) { |
250 | Section *child_section = m_children.GetSectionAtIndex(idx: i).get(); |
251 | |
252 | addr_t child_offset = child_section->GetOffset(); |
253 | if (child_offset <= offset && |
254 | offset - child_offset < |
255 | child_section->GetByteSize() + (allow_section_end ? 1 : 0)) |
256 | return child_section->ResolveContainedAddress(offset: offset - child_offset, |
257 | so_addr, allow_section_end); |
258 | } |
259 | so_addr.SetOffset(offset); |
260 | so_addr.SetSection(const_cast<Section *>(this)->shared_from_this()); |
261 | |
262 | // Ensure that there are no orphaned (i.e., moduleless) sections. |
263 | assert(GetModule().get()); |
264 | return true; |
265 | } |
266 | |
267 | bool Section::ContainsFileAddress(addr_t vm_addr) const { |
268 | const addr_t file_addr = GetFileAddress(); |
269 | if (file_addr != LLDB_INVALID_ADDRESS && !IsThreadSpecific()) { |
270 | if (file_addr <= vm_addr) { |
271 | const addr_t offset = (vm_addr - file_addr) * m_target_byte_size; |
272 | return offset < GetByteSize(); |
273 | } |
274 | } |
275 | return false; |
276 | } |
277 | |
278 | void Section::Dump(llvm::raw_ostream &s, unsigned indent, Target *target, |
279 | uint32_t depth) const { |
280 | s.indent(NumSpaces: indent); |
281 | s << llvm::format(Fmt: "0x%16.16" PRIx64 " %-22s " , Vals: GetID(), Vals: GetTypeAsCString()); |
282 | bool resolved = true; |
283 | addr_t addr = LLDB_INVALID_ADDRESS; |
284 | |
285 | if (GetByteSize() == 0) |
286 | s.indent(NumSpaces: 39); |
287 | else { |
288 | if (target) |
289 | addr = GetLoadBaseAddress(target); |
290 | |
291 | if (addr == LLDB_INVALID_ADDRESS) { |
292 | if (target) |
293 | resolved = false; |
294 | addr = GetFileAddress(); |
295 | } |
296 | |
297 | VMRange range(addr, addr + m_byte_size); |
298 | range.Dump(s, base_addr: 0); |
299 | } |
300 | |
301 | s << llvm::format(Fmt: "%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x " , |
302 | Vals: resolved ? ' ' : '*', Vals: m_readable ? 'r' : '-', |
303 | Vals: m_writable ? 'w' : '-', Vals: m_executable ? 'x' : '-', |
304 | Vals: m_file_offset, Vals: m_file_size, Vals: Get()); |
305 | |
306 | DumpName(s); |
307 | |
308 | s << "\n" ; |
309 | |
310 | if (depth > 0) |
311 | m_children.Dump(s, indent, target, show_header: false, depth: depth - 1); |
312 | } |
313 | |
314 | void Section::DumpName(llvm::raw_ostream &s) const { |
315 | SectionSP parent_sp(GetParent()); |
316 | if (parent_sp) { |
317 | parent_sp->DumpName(s); |
318 | s << '.'; |
319 | } else { |
320 | // The top most section prints the module basename |
321 | const char *name = nullptr; |
322 | ModuleSP module_sp(GetModule()); |
323 | |
324 | if (m_obj_file) { |
325 | const FileSpec &file_spec = m_obj_file->GetFileSpec(); |
326 | name = file_spec.GetFilename().AsCString(); |
327 | } |
328 | if ((!name || !name[0]) && module_sp) |
329 | name = module_sp->GetFileSpec().GetFilename().AsCString(); |
330 | if (name && name[0]) |
331 | s << name << '.'; |
332 | } |
333 | s << m_name; |
334 | } |
335 | |
336 | bool Section::IsDescendant(const Section *section) { |
337 | if (this == section) |
338 | return true; |
339 | SectionSP parent_sp(GetParent()); |
340 | if (parent_sp) |
341 | return parent_sp->IsDescendant(section); |
342 | return false; |
343 | } |
344 | |
345 | bool Section::Slide(addr_t slide_amount, bool slide_children) { |
346 | if (m_file_addr != LLDB_INVALID_ADDRESS) { |
347 | if (slide_amount == 0) |
348 | return true; |
349 | |
350 | m_file_addr += slide_amount; |
351 | |
352 | if (slide_children) |
353 | m_children.Slide(slide_amount, slide_children); |
354 | |
355 | return true; |
356 | } |
357 | return false; |
358 | } |
359 | |
360 | /// Get the permissions as OR'ed bits from lldb::Permissions |
361 | uint32_t Section::GetPermissions() const { |
362 | uint32_t permissions = 0; |
363 | if (m_readable) |
364 | permissions |= ePermissionsReadable; |
365 | if (m_writable) |
366 | permissions |= ePermissionsWritable; |
367 | if (m_executable) |
368 | permissions |= ePermissionsExecutable; |
369 | return permissions; |
370 | } |
371 | |
372 | /// Set the permissions using bits OR'ed from lldb::Permissions |
373 | void Section::SetPermissions(uint32_t permissions) { |
374 | m_readable = (permissions & ePermissionsReadable) != 0; |
375 | m_writable = (permissions & ePermissionsWritable) != 0; |
376 | m_executable = (permissions & ePermissionsExecutable) != 0; |
377 | } |
378 | |
379 | lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len, |
380 | lldb::offset_t offset) { |
381 | if (m_obj_file) |
382 | return m_obj_file->ReadSectionData(section: this, section_offset: offset, dst, dst_len); |
383 | return 0; |
384 | } |
385 | |
386 | lldb::offset_t Section::(DataExtractor §ion_data) { |
387 | if (m_obj_file) |
388 | return m_obj_file->ReadSectionData(section: this, section_data); |
389 | return 0; |
390 | } |
391 | |
392 | bool Section::ContainsOnlyDebugInfo() const { |
393 | switch (m_type) { |
394 | case eSectionTypeInvalid: |
395 | case eSectionTypeCode: |
396 | case eSectionTypeContainer: |
397 | case eSectionTypeData: |
398 | case eSectionTypeDataCString: |
399 | case eSectionTypeDataCStringPointers: |
400 | case eSectionTypeDataSymbolAddress: |
401 | case eSectionTypeData4: |
402 | case eSectionTypeData8: |
403 | case eSectionTypeData16: |
404 | case eSectionTypeDataPointers: |
405 | case eSectionTypeZeroFill: |
406 | case eSectionTypeDataObjCMessageRefs: |
407 | case eSectionTypeDataObjCCFStrings: |
408 | case eSectionTypeELFSymbolTable: |
409 | case eSectionTypeELFDynamicSymbols: |
410 | case eSectionTypeELFRelocationEntries: |
411 | case eSectionTypeELFDynamicLinkInfo: |
412 | case eSectionTypeEHFrame: |
413 | case eSectionTypeARMexidx: |
414 | case eSectionTypeARMextab: |
415 | case eSectionTypeCompactUnwind: |
416 | case eSectionTypeGoSymtab: |
417 | case eSectionTypeAbsoluteAddress: |
418 | case eSectionTypeOther: |
419 | // Used for "__dof_cache" in mach-o or ".debug" for COFF which isn't debug |
420 | // information that we parse at all. This was causing system files with no |
421 | // debug info to show debug info byte sizes in the "statistics dump" output |
422 | // for each module. New "eSectionType" enums should be created for dedicated |
423 | // debug info that has a predefined format if we wish for these sections to |
424 | // show up as debug info. |
425 | case eSectionTypeDebug: |
426 | return false; |
427 | |
428 | case eSectionTypeDWARFDebugAbbrev: |
429 | case eSectionTypeDWARFDebugAbbrevDwo: |
430 | case eSectionTypeDWARFDebugAddr: |
431 | case eSectionTypeDWARFDebugAranges: |
432 | case eSectionTypeDWARFDebugCuIndex: |
433 | case eSectionTypeDWARFDebugTuIndex: |
434 | case eSectionTypeDWARFDebugFrame: |
435 | case eSectionTypeDWARFDebugInfo: |
436 | case eSectionTypeDWARFDebugInfoDwo: |
437 | case eSectionTypeDWARFDebugLine: |
438 | case eSectionTypeDWARFDebugLineStr: |
439 | case eSectionTypeDWARFDebugLoc: |
440 | case eSectionTypeDWARFDebugLocDwo: |
441 | case eSectionTypeDWARFDebugLocLists: |
442 | case eSectionTypeDWARFDebugLocListsDwo: |
443 | case eSectionTypeDWARFDebugMacInfo: |
444 | case eSectionTypeDWARFDebugMacro: |
445 | case eSectionTypeDWARFDebugPubNames: |
446 | case eSectionTypeDWARFDebugPubTypes: |
447 | case eSectionTypeDWARFDebugRanges: |
448 | case eSectionTypeDWARFDebugRngLists: |
449 | case eSectionTypeDWARFDebugRngListsDwo: |
450 | case eSectionTypeDWARFDebugStr: |
451 | case eSectionTypeDWARFDebugStrDwo: |
452 | case eSectionTypeDWARFDebugStrOffsets: |
453 | case eSectionTypeDWARFDebugStrOffsetsDwo: |
454 | case eSectionTypeDWARFDebugTypes: |
455 | case eSectionTypeDWARFDebugTypesDwo: |
456 | case eSectionTypeDWARFDebugNames: |
457 | case eSectionTypeDWARFAppleNames: |
458 | case eSectionTypeDWARFAppleTypes: |
459 | case eSectionTypeDWARFAppleNamespaces: |
460 | case eSectionTypeDWARFAppleObjC: |
461 | case eSectionTypeDWARFGNUDebugAltLink: |
462 | case eSectionTypeCTF: |
463 | case eSectionTypeLLDBTypeSummaries: |
464 | case eSectionTypeLLDBFormatters: |
465 | case eSectionTypeSwiftModules: |
466 | return true; |
467 | } |
468 | return false; |
469 | } |
470 | |
471 | #pragma mark SectionList |
472 | |
473 | SectionList &SectionList::operator=(const SectionList &rhs) { |
474 | if (this != &rhs) |
475 | m_sections = rhs.m_sections; |
476 | return *this; |
477 | } |
478 | |
479 | size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) { |
480 | if (section_sp) { |
481 | size_t section_index = m_sections.size(); |
482 | m_sections.push_back(x: section_sp); |
483 | return section_index; |
484 | } |
485 | |
486 | return std::numeric_limits<size_t>::max(); |
487 | } |
488 | |
489 | // Warning, this can be slow as it's removing items from a std::vector. |
490 | bool SectionList::DeleteSection(size_t idx) { |
491 | if (idx < m_sections.size()) { |
492 | m_sections.erase(position: m_sections.begin() + idx); |
493 | return true; |
494 | } |
495 | return false; |
496 | } |
497 | |
498 | size_t SectionList::FindSectionIndex(const Section *sect) { |
499 | iterator sect_iter; |
500 | iterator begin = m_sections.begin(); |
501 | iterator end = m_sections.end(); |
502 | for (sect_iter = begin; sect_iter != end; ++sect_iter) { |
503 | if (sect_iter->get() == sect) { |
504 | // The secton was already in this section list |
505 | return std::distance(first: begin, last: sect_iter); |
506 | } |
507 | } |
508 | return UINT32_MAX; |
509 | } |
510 | |
511 | size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) { |
512 | size_t sect_idx = FindSectionIndex(sect: sect_sp.get()); |
513 | if (sect_idx == UINT32_MAX) { |
514 | sect_idx = AddSection(section_sp: sect_sp); |
515 | } |
516 | return sect_idx; |
517 | } |
518 | |
519 | bool SectionList::ReplaceSection(user_id_t sect_id, |
520 | const lldb::SectionSP §_sp, |
521 | uint32_t depth) { |
522 | iterator sect_iter, end = m_sections.end(); |
523 | for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { |
524 | if ((*sect_iter)->GetID() == sect_id) { |
525 | *sect_iter = sect_sp; |
526 | return true; |
527 | } else if (depth > 0) { |
528 | if ((*sect_iter) |
529 | ->GetChildren() |
530 | .ReplaceSection(sect_id, sect_sp, depth: depth - 1)) |
531 | return true; |
532 | } |
533 | } |
534 | return false; |
535 | } |
536 | |
537 | size_t SectionList::GetNumSections(uint32_t depth) const { |
538 | size_t count = m_sections.size(); |
539 | if (depth > 0) { |
540 | const_iterator sect_iter, end = m_sections.end(); |
541 | for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { |
542 | count += (*sect_iter)->GetChildren().GetNumSections(depth: depth - 1); |
543 | } |
544 | } |
545 | return count; |
546 | } |
547 | |
548 | SectionSP SectionList::GetSectionAtIndex(size_t idx) const { |
549 | SectionSP sect_sp; |
550 | if (idx < m_sections.size()) |
551 | sect_sp = m_sections[idx]; |
552 | return sect_sp; |
553 | } |
554 | |
555 | SectionSP SectionList::FindSectionByName(ConstString section_dstr) const { |
556 | SectionSP sect_sp; |
557 | // Check if we have a valid section string |
558 | if (section_dstr && !m_sections.empty()) { |
559 | const_iterator sect_iter; |
560 | const_iterator end = m_sections.end(); |
561 | for (sect_iter = m_sections.begin(); |
562 | sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) { |
563 | Section *child_section = sect_iter->get(); |
564 | if (child_section) { |
565 | if (child_section->GetName() == section_dstr) { |
566 | sect_sp = *sect_iter; |
567 | } else { |
568 | sect_sp = |
569 | child_section->GetChildren().FindSectionByName(section_dstr); |
570 | } |
571 | } |
572 | } |
573 | } |
574 | return sect_sp; |
575 | } |
576 | |
577 | SectionSP SectionList::FindSectionByID(user_id_t sect_id) const { |
578 | SectionSP sect_sp; |
579 | if (sect_id) { |
580 | const_iterator sect_iter; |
581 | const_iterator end = m_sections.end(); |
582 | for (sect_iter = m_sections.begin(); |
583 | sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) { |
584 | if ((*sect_iter)->GetID() == sect_id) { |
585 | sect_sp = *sect_iter; |
586 | break; |
587 | } else { |
588 | sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id); |
589 | } |
590 | } |
591 | } |
592 | return sect_sp; |
593 | } |
594 | |
595 | SectionSP SectionList::FindSectionByType(SectionType sect_type, |
596 | bool check_children, |
597 | size_t start_idx) const { |
598 | SectionSP sect_sp; |
599 | size_t num_sections = m_sections.size(); |
600 | for (size_t idx = start_idx; idx < num_sections; ++idx) { |
601 | if (m_sections[idx]->GetType() == sect_type) { |
602 | sect_sp = m_sections[idx]; |
603 | break; |
604 | } else if (check_children) { |
605 | sect_sp = m_sections[idx]->GetChildren().FindSectionByType( |
606 | sect_type, check_children, start_idx: 0); |
607 | if (sect_sp) |
608 | break; |
609 | } |
610 | } |
611 | return sect_sp; |
612 | } |
613 | |
614 | SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr, |
615 | uint32_t depth) const { |
616 | SectionSP sect_sp; |
617 | const_iterator sect_iter; |
618 | const_iterator end = m_sections.end(); |
619 | for (sect_iter = m_sections.begin(); |
620 | sect_iter != end && sect_sp.get() == nullptr; ++sect_iter) { |
621 | Section *sect = sect_iter->get(); |
622 | if (sect->ContainsFileAddress(vm_addr)) { |
623 | // The file address is in this section. We need to make sure one of our |
624 | // child sections doesn't contain this address as well as obeying the |
625 | // depth limit that was passed in. |
626 | if (depth > 0) |
627 | sect_sp = sect->GetChildren().FindSectionContainingFileAddress( |
628 | vm_addr, depth: depth - 1); |
629 | |
630 | if (sect_sp.get() == nullptr && !sect->IsFake()) |
631 | sect_sp = *sect_iter; |
632 | } |
633 | } |
634 | return sect_sp; |
635 | } |
636 | |
637 | bool SectionList::ContainsSection(user_id_t sect_id) const { |
638 | return FindSectionByID(sect_id).get() != nullptr; |
639 | } |
640 | |
641 | void SectionList::Dump(llvm::raw_ostream &s, unsigned indent, Target *target, |
642 | bool , uint32_t depth) const { |
643 | bool target_has_loaded_sections = target && target->HasLoadedSections(); |
644 | if (show_header && !m_sections.empty()) { |
645 | s.indent(NumSpaces: indent); |
646 | s << llvm::formatv( |
647 | Fmt: "SectID Type {0} Address " |
648 | " Perm File Off. File Size Flags Section Name\n" , |
649 | Vals: target_has_loaded_sections ? "Load" : "File" ); |
650 | s.indent(NumSpaces: indent); |
651 | s << "------------------ ---------------------- " |
652 | "--------------------------------------- ---- ---------- ---------- " |
653 | "---------- ----------------------------\n" ; |
654 | } |
655 | |
656 | for (const auto §ion_sp : m_sections) |
657 | section_sp->Dump(s, indent, target: target_has_loaded_sections ? target : nullptr, |
658 | depth); |
659 | } |
660 | |
661 | size_t SectionList::Slide(addr_t slide_amount, bool slide_children) { |
662 | size_t count = 0; |
663 | const_iterator pos, end = m_sections.end(); |
664 | for (pos = m_sections.begin(); pos != end; ++pos) { |
665 | if ((*pos)->Slide(slide_amount, slide_children)) |
666 | ++count; |
667 | } |
668 | return count; |
669 | } |
670 | |
671 | uint64_t SectionList::GetDebugInfoSize() const { |
672 | uint64_t debug_info_size = 0; |
673 | for (const auto §ion : m_sections) { |
674 | const SectionList &sub_sections = section->GetChildren(); |
675 | if (sub_sections.GetSize() > 0) |
676 | debug_info_size += sub_sections.GetDebugInfoSize(); |
677 | else if (section->ContainsOnlyDebugInfo()) |
678 | debug_info_size += section->GetFileSize(); |
679 | } |
680 | return debug_info_size; |
681 | } |
682 | |
683 | namespace llvm { |
684 | namespace json { |
685 | |
686 | bool fromJSON(const llvm::json::Value &value, |
687 | lldb_private::JSONSection §ion, llvm::json::Path path) { |
688 | llvm::json::ObjectMapper o(value, path); |
689 | return o && o.map(Prop: "name" , Out&: section.name) && o.map(Prop: "type" , Out&: section.type) && |
690 | o.map(Prop: "address" , Out&: section.address) && o.map(Prop: "size" , Out&: section.size) && |
691 | o.map(Prop: "read" , Out&: section.read) && o.map(Prop: "write" , Out&: section.write) && |
692 | o.map(Prop: "execute" , Out&: section.execute) && |
693 | o.mapOptional(Prop: "subsections" , Out&: section.subsections) && |
694 | o.map(Prop: "user_id" , Out&: section.user_id) && |
695 | o.map(Prop: "file_offset" , Out&: section.file_offset) && |
696 | o.map(Prop: "file_size" , Out&: section.file_size) && |
697 | o.map(Prop: "alignment" , Out&: section.log2align) && |
698 | o.map(Prop: "flags" , Out&: section.flags) && o.map(Prop: "fake" , Out&: section.fake) && |
699 | o.map(Prop: "encrypted" , Out&: section.encrypted) && |
700 | o.map(Prop: "thread_specific" , Out&: section.thread_specific); |
701 | } |
702 | |
703 | bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type, |
704 | llvm::json::Path path) { |
705 | if (auto str = value.getAsString()) { |
706 | type = llvm::StringSwitch<lldb::SectionType>(*str) |
707 | .Case(S: "code" , Value: eSectionTypeCode) |
708 | .Case(S: "container" , Value: eSectionTypeContainer) |
709 | .Case(S: "data" , Value: eSectionTypeData) |
710 | .Case(S: "debug" , Value: eSectionTypeDebug) |
711 | .Default(Value: eSectionTypeInvalid); |
712 | |
713 | if (type == eSectionTypeInvalid) { |
714 | path.report(Message: "invalid section type" ); |
715 | return false; |
716 | } |
717 | |
718 | return true; |
719 | } |
720 | path.report(Message: "expected string" ); |
721 | return false; |
722 | } |
723 | } // namespace json |
724 | } // namespace llvm |
725 | |