LLDB mainline
Section.h
Go to the documentation of this file.
1//===-- Section.h -----------------------------------------------*- C++ -*-===//
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#ifndef LLDB_CORE_SECTION_H
10#define LLDB_CORE_SECTION_H
11
14#include "lldb/Utility/Flags.h"
15#include "lldb/Utility/UserID.h"
16#include "lldb/lldb-defines.h"
18#include "lldb/lldb-forward.h"
19#include "lldb/lldb-types.h"
20#include "llvm/Support/JSON.h"
21
22#include <memory>
23#include <vector>
24
25#include <cstddef>
26#include <cstdint>
27
28namespace lldb_private {
29class Address;
30class DataExtractor;
31class ObjectFile;
32class Section;
33class Target;
34
36public:
37 typedef std::vector<lldb::SectionSP> collection;
38 typedef collection::iterator iterator;
39 typedef collection::const_iterator const_iterator;
40
41 const_iterator begin() const { return m_sections.begin(); }
42 const_iterator end() const { return m_sections.end(); }
43 const_iterator begin() { return m_sections.begin(); }
44 const_iterator end() { return m_sections.end(); }
45
46 /// Create an empty list.
47 SectionList() = default;
48
49 SectionList(const SectionList &lhs);
50
52
53 size_t AddSection(const lldb::SectionSP &section_sp);
54
55 size_t AddUniqueSection(const lldb::SectionSP &section_sp);
56
57 size_t FindSectionIndex(const Section *sect);
58
59 bool ContainsSection(lldb::user_id_t sect_id) const;
60
61 void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
62 bool show_header, uint32_t depth) const;
63
65
67
69 bool check_children,
70 size_t start_idx = 0) const;
71
74 uint32_t depth = UINT32_MAX) const;
75
76 // Get the number of sections in this list only
77 size_t GetSize() const { return m_sections.size(); }
78
79 // Get the number of sections in this list, and any contained child sections
80 size_t GetNumSections(uint32_t depth) const;
81
83 const lldb::SectionSP &section_sp,
84 uint32_t depth = UINT32_MAX);
85
86 // Warning, this can be slow as it's removing items from a std::vector.
87 bool DeleteSection(size_t idx);
88
89 lldb::SectionSP GetSectionAtIndex(size_t idx) const;
90
91 size_t Slide(lldb::addr_t slide_amount, bool slide_children);
92
93 void Clear() { m_sections.clear(); }
94
95 /// Get the debug information size from all sections that contain debug
96 /// information. Symbol tables are not considered part of the debug
97 /// information for this call, just known sections that contain debug
98 /// information.
99 uint64_t GetDebugInfoSize() const;
100
101 // Callback to decide which of two matching sections should be used in the
102 // merged output.
105
106 // Function that merges two different sections into a new output list. All
107 // unique sections will be checked for conflict and resolved using the
108 // supplied merging callback.
109 static SectionList Merge(SectionList &lhs, SectionList &rhs,
110 MergeCallback filter);
111
112protected:
114};
115
117 std::optional<lldb::user_id_t> user_id;
118 std::string name;
119 std::optional<lldb::SectionType> type;
120 std::optional<uint64_t> address;
121 std::optional<uint64_t> size;
122 std::optional<uint64_t> file_offset;
123 std::optional<uint64_t> file_size;
124 std::optional<uint64_t> log2align;
125 std::optional<uint64_t> flags;
126
127 // Section permissions;
128 std::optional<bool> read;
129 std::optional<bool> write;
130 std::optional<bool> execute;
131
132 std::optional<bool> fake;
133 std::optional<bool> encrypted;
134 std::optional<bool> thread_specific;
135
136 std::vector<JSONSection> subsections;
137};
138
139class Section : public std::enable_shared_from_this<Section>,
140 public ModuleChild,
141 public UserID,
142 public Flags {
143public:
144 // Create a root section (one that has no parent)
145 Section(const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
146 lldb::user_id_t sect_id, ConstString name,
147 lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
148 lldb::addr_t vm_size, lldb::offset_t file_offset,
149 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
150 uint32_t target_byte_size = 1);
151
152 // Create a section that is a child of parent_section_sp
153 Section(const lldb::SectionSP &parent_section_sp, // NULL for top level
154 // sections, non-NULL for
155 // child sections
156 const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
157 lldb::user_id_t sect_id, ConstString name,
158 lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
159 lldb::addr_t vm_size, lldb::offset_t file_offset,
160 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
161 uint32_t target_byte_size = 1);
162
164
165 static int Compare(const Section &a, const Section &b);
166
167 bool ContainsFileAddress(lldb::addr_t vm_addr) const;
168
170
171 const SectionList &GetChildren() const { return m_children; }
172
173 void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
174 uint32_t depth) const;
175
176 void DumpName(llvm::raw_ostream &s) const;
177
179
180 bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr,
181 bool allow_section_end = false) const;
182
184
185 void SetFileOffset(lldb::offset_t file_offset) {
186 m_file_offset = file_offset;
187 }
188
190
191 void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; }
192
194
195 bool SetFileAddress(lldb::addr_t file_addr);
196
197 lldb::addr_t GetOffset() const;
198
200
201 void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
202
203 bool IsFake() const { return m_fake; }
204
205 void SetIsFake(bool fake) { m_fake = fake; }
206
207 bool IsEncrypted() const { return m_encrypted; }
208
209 void SetIsEncrypted(bool b) { m_encrypted = b; }
210
211 bool IsDescendant(const Section *section);
212
213 ConstString GetName() const { return m_name; }
214
215 bool Slide(lldb::addr_t slide_amount, bool slide_children);
216
217 lldb::SectionType GetType() const { return m_type; }
218
219 const char *GetTypeAsCString() const;
220
221 lldb::SectionSP GetParent() const { return m_parent_wp.lock(); }
222
223 bool IsThreadSpecific() const { return m_thread_specific; }
224
226
227 /// Get the permissions as OR'ed bits from lldb::Permissions
228 uint32_t GetPermissions() const;
229
230 /// Set the permissions using bits OR'ed from lldb::Permissions
231 void SetPermissions(uint32_t permissions);
232
234 const ObjectFile *GetObjectFile() const { return m_obj_file; }
235
236 /// Read the section data from the object file that the section
237 /// resides in.
238 ///
239 /// \param[in] dst
240 /// Where to place the data
241 ///
242 /// \param[in] dst_len
243 /// How many bytes of section data to read
244 ///
245 /// \param[in] offset
246 /// The offset in bytes within this section's data at which to
247 /// start copying data from.
248 ///
249 /// \return
250 /// The number of bytes read from the section, or zero if the
251 /// section has no data or \a offset is not a valid offset
252 /// in this section.
254 lldb::offset_t offset = 0);
255
256 /// Get the shared reference to the section data from the object
257 /// file that the section resides in. No copies of the data will be
258 /// make unless the object file has been read from memory. If the
259 /// object file is on disk, it will shared the mmap data for the
260 /// entire object file.
261 ///
262 /// \param[in] data
263 /// Where to place the data, address byte size, and byte order
264 ///
265 /// \return
266 /// The number of bytes read from the section, or zero if the
267 /// section has no data or \a offset is not a valid offset
268 /// in this section.
270
271 uint32_t GetLog2Align() { return m_log2align; }
272
273 void SetLog2Align(uint32_t align) { m_log2align = align; }
274
275 // Get the number of host bytes required to hold a target byte
276 uint32_t GetTargetByteSize() const { return m_target_byte_size; }
277
278 bool IsRelocated() const { return m_relocated; }
279
280 void SetIsRelocated(bool b) { m_relocated = b; }
281
282 /// Returns true if this section contains debug information. Symbol tables
283 /// are not considered debug information since some symbols might contain
284 /// debug information (STABS, COFF) but not all symbols do, so to keep this
285 /// fast and simple only sections that contains only debug information should
286 /// return true.
287 bool ContainsOnlyDebugInfo() const;
288
289 /// Returns true if this is a global offset table section.
290 bool IsGOTSection() const;
291
292protected:
293 ObjectFile *m_obj_file; // The object file that data for this section should
294 // be read from
295 lldb::SectionType m_type; // The type of this section
296 lldb::SectionWP m_parent_wp; // Weak pointer to parent section
297 ConstString m_name; // Name of this section
298 lldb::addr_t m_file_addr; // The absolute file virtual address range of this
299 // section if m_parent == NULL,
300 // offset from parent file virtual address if m_parent != NULL
301 lldb::addr_t m_byte_size; // Size in bytes that this section will occupy in
302 // memory at runtime
303 lldb::offset_t m_file_offset; // Object file offset (if any)
304 lldb::offset_t m_file_size; // Object file size (can be smaller than
305 // m_byte_size for zero filled sections...)
306 uint32_t m_log2align; // log_2(align) of the section (i.e. section has to be
307 // aligned to 2^m_log2align)
308 SectionList m_children; // Child sections
309 bool m_fake : 1, // If true, then this section only can contain the address if
310 // one of its
311 // children contains an address. This allows for gaps between the
312 // children that are contained in the address range for this section, but
313 // do not produce hits unless the children contain the address.
314 m_encrypted : 1, // Set to true if the contents are encrypted
315 m_thread_specific : 1, // This section is thread specific
316 m_readable : 1, // If this section has read permissions
317 m_writable : 1, // If this section has write permissions
318 m_executable : 1, // If this section has executable permissions
319 m_relocated : 1; // If this section has had relocations applied
320 uint32_t m_target_byte_size; // Some architectures have non-8-bit byte size.
321 // This is specified as
322 // as a multiple number of a host bytes
323private:
324 Section(const Section &) = delete;
325 const Section &operator=(const Section &) = delete;
326};
327
328} // namespace lldb_private
329
330namespace llvm {
331namespace json {
332
333bool fromJSON(const llvm::json::Value &value,
334 lldb_private::JSONSection &section, llvm::json::Path path);
335
336bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type,
337 llvm::json::Path path);
338
339} // namespace json
340} // namespace llvm
341
342#endif // LLDB_CORE_SECTION_H
A section + offset based address class.
Definition Address.h:62
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
Flags(ValueType flags=0)
Construct with initial flag bit values.
Definition Flags.h:34
ModuleChild(const lldb::ModuleSP &module_sp)
Construct with owning module.
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:45
const_iterator begin() const
Definition Section.h:41
bool ContainsSection(lldb::user_id_t sect_id) const
Definition Section.cpp:646
const_iterator end() const
Definition Section.h:42
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
Definition Section.cpp:564
collection::iterator iterator
Definition Section.h:38
static SectionList Merge(SectionList &lhs, SectionList &rhs, MergeCallback filter)
Definition Section.cpp:692
size_t GetNumSections(uint32_t depth) const
Definition Section.cpp:546
lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const
Definition Section.cpp:586
SectionList()=default
Create an empty list.
size_t Slide(lldb::addr_t slide_amount, bool slide_children)
Definition Section.cpp:670
lldb::SectionSP FindSectionContainingFileAddress(lldb::addr_t addr, uint32_t depth=UINT32_MAX) const
Definition Section.cpp:623
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition Section.cpp:680
size_t GetSize() const
Definition Section.h:77
const_iterator end()
Definition Section.h:44
std::function< lldb::SectionSP(lldb::SectionSP, lldb::SectionSP)> MergeCallback
Definition Section.h:103
bool DeleteSection(size_t idx)
Definition Section.cpp:499
std::vector< lldb::SectionSP > collection
Definition Section.h:37
size_t AddSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:488
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition Section.cpp:528
SectionList & operator=(const SectionList &rhs)
Definition Section.cpp:482
size_t FindSectionIndex(const Section *sect)
Definition Section.cpp:507
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition Section.cpp:604
size_t AddUniqueSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:520
const_iterator begin()
Definition Section.h:43
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, bool show_header, uint32_t depth) const
Definition Section.cpp:650
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:557
collection::const_iterator const_iterator
Definition Section.h:39
bool IsThreadSpecific() const
Definition Section.h:223
bool ContainsFileAddress(lldb::addr_t vm_addr) const
Definition Section.cpp:269
lldb::addr_t m_byte_size
Definition Section.h:301
const SectionList & GetChildren() const
Definition Section.h:171
uint32_t GetTargetByteSize() const
Definition Section.h:276
lldb::addr_t GetOffset() const
Definition Section.cpp:223
const Section & operator=(const Section &)=delete
const ObjectFile * GetObjectFile() const
Definition Section.h:234
lldb::addr_t m_file_addr
Definition Section.h:298
void SetPermissions(uint32_t permissions)
Set the permissions using bits OR'ed from lldb::Permissions.
Definition Section.cpp:375
ConstString GetName() const
Definition Section.h:213
void SetIsFake(bool fake)
Definition Section.h:205
bool IsFake() const
Definition Section.h:203
void DumpName(llvm::raw_ostream &s) const
Definition Section.cpp:316
void SetByteSize(lldb::addr_t byte_size)
Definition Section.h:201
lldb::offset_t GetSectionData(void *dst, lldb::offset_t dst_len, lldb::offset_t offset=0)
Read the section data from the object file that the section resides in.
Definition Section.cpp:381
bool IsDescendant(const Section *section)
Definition Section.cpp:338
void SetFileOffset(lldb::offset_t file_offset)
Definition Section.h:185
bool IsGOTSection() const
Returns true if this is a global offset table section.
Definition Section.cpp:474
ObjectFile * m_obj_file
Definition Section.h:293
void SetIsRelocated(bool b)
Definition Section.h:280
lldb::SectionSP GetParent() const
Definition Section.h:221
uint32_t GetPermissions() const
Get the permissions as OR'ed bits from lldb::Permissions.
Definition Section.cpp:363
bool SetFileAddress(lldb::addr_t file_addr)
Definition Section.cpp:210
lldb::offset_t GetFileOffset() const
Definition Section.h:183
Section(const lldb::ModuleSP &module_sp, ObjectFile *obj_file, lldb::user_id_t sect_id, ConstString name, lldb::SectionType sect_type, lldb::addr_t file_vm_addr, lldb::addr_t vm_size, lldb::offset_t file_offset, lldb::offset_t file_size, uint32_t log2align, uint32_t flags, uint32_t target_byte_size=1)
Definition Section.cpp:164
lldb::SectionWP m_parent_wp
Definition Section.h:296
ConstString m_name
Definition Section.h:297
lldb::addr_t GetFileAddress() const
Definition Section.cpp:198
SectionList & GetChildren()
Definition Section.h:169
lldb::SectionType m_type
Definition Section.h:295
ObjectFile * GetObjectFile()
Definition Section.h:233
bool ContainsOnlyDebugInfo() const
Returns true if this section contains debug information.
Definition Section.cpp:394
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, uint32_t depth) const
Definition Section.cpp:280
void SetFileSize(lldb::offset_t file_size)
Definition Section.h:191
uint32_t GetLog2Align()
Definition Section.h:271
const char * GetTypeAsCString() const
Definition Section.cpp:28
lldb::SectionType GetType() const
Definition Section.h:217
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:233
bool Slide(lldb::addr_t slide_amount, bool slide_children)
Definition Section.cpp:347
Section(const Section &)=delete
static int Compare(const Section &a, const Section &b)
bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr, bool allow_section_end=false) const
Definition Section.cpp:248
void SetLog2Align(uint32_t align)
Definition Section.h:273
lldb::offset_t m_file_offset
Definition Section.h:303
lldb::offset_t m_file_size
Definition Section.h:304
lldb::addr_t GetByteSize() const
Definition Section.h:199
SectionList m_children
Definition Section.h:308
void SetIsThreadSpecific(bool b)
Definition Section.h:225
void SetIsEncrypted(bool b)
Definition Section.h:209
uint32_t m_target_byte_size
Definition Section.h:320
lldb::offset_t GetFileSize() const
Definition Section.h:189
bool IsEncrypted() const
Definition Section.h:207
bool IsRelocated() const
Definition Section.h:278
#define UINT32_MAX
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
std::weak_ptr< lldb_private::Section > SectionWP
bool fromJSON(const llvm::json::Value &value, lldb_private::JSONSection &section, llvm::json::Path path)
Definition Section.cpp:722
std::optional< lldb::user_id_t > user_id
Definition Section.h:117
std::optional< uint64_t > log2align
Definition Section.h:124
std::optional< uint64_t > size
Definition Section.h:121
std::optional< uint64_t > flags
Definition Section.h:125
std::optional< bool > encrypted
Definition Section.h:133
std::optional< bool > fake
Definition Section.h:132
std::optional< lldb::SectionType > type
Definition Section.h:119
std::optional< bool > write
Definition Section.h:129
std::optional< bool > execute
Definition Section.h:130
std::optional< uint64_t > file_offset
Definition Section.h:122
std::optional< uint64_t > address
Definition Section.h:120
std::optional< bool > thread_specific
Definition Section.h:134
std::optional< uint64_t > file_size
Definition Section.h:123
std::vector< JSONSection > subsections
Definition Section.h:136
std::optional< bool > read
Definition Section.h:128
UserID(lldb::user_id_t uid=LLDB_INVALID_UID)
Construct with optional user ID.
Definition UserID.h:33