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
151 // Create a section that is a child of parent_section_sp
152 Section(const lldb::SectionSP &parent_section_sp, // NULL for top level
153 // sections, non-NULL for
154 // child sections
155 const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
156 lldb::user_id_t sect_id, ConstString name,
157 lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
158 lldb::addr_t vm_size, lldb::offset_t file_offset,
159 lldb::offset_t file_size, uint32_t log2align, uint32_t flags);
160
162
163 static int Compare(const Section &a, const Section &b);
164
165 bool ContainsFileAddress(lldb::addr_t vm_addr) const;
166
168
169 const SectionList &GetChildren() const { return m_children; }
170
171 void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
172 uint32_t depth) const;
173
174 void DumpName(llvm::raw_ostream &s) const;
175
177
178 bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr,
179 bool allow_section_end = false) const;
180
182
183 void SetFileOffset(lldb::offset_t file_offset) {
184 m_file_offset = file_offset;
185 }
186
188
189 void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; }
190
192
193 bool SetFileAddress(lldb::addr_t file_addr);
194
195 lldb::addr_t GetOffset() const;
196
198
199 void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
200
201 bool IsFake() const { return m_fake; }
202
203 void SetIsFake(bool fake) { m_fake = fake; }
204
205 bool IsEncrypted() const { return m_encrypted; }
206
207 void SetIsEncrypted(bool b) { m_encrypted = b; }
208
209 bool IsDescendant(const Section *section);
210
211 ConstString GetName() const { return m_name; }
212
213 bool Slide(lldb::addr_t slide_amount, bool slide_children);
214
215 lldb::SectionType GetType() const { return m_type; }
216
217 const char *GetTypeAsCString() const;
218
219 lldb::SectionSP GetParent() const { return m_parent_wp.lock(); }
220
221 bool IsThreadSpecific() const { return m_thread_specific; }
222
224
225 /// Get the permissions as OR'ed bits from lldb::Permissions
226 uint32_t GetPermissions() const;
227
228 /// Set the permissions using bits OR'ed from lldb::Permissions
229 void SetPermissions(uint32_t permissions);
230
232 const ObjectFile *GetObjectFile() const { return m_obj_file; }
233
234 /// Read the section data from the object file that the section
235 /// resides in.
236 ///
237 /// \param[in] dst
238 /// Where to place the data
239 ///
240 /// \param[in] dst_len
241 /// How many bytes of section data to read
242 ///
243 /// \param[in] offset
244 /// The offset in bytes within this section's data at which to
245 /// start copying data from.
246 ///
247 /// \return
248 /// The number of bytes read from the section, or zero if the
249 /// section has no data or \a offset is not a valid offset
250 /// in this section.
252 lldb::offset_t offset = 0);
253
254 /// Get the shared reference to the section data from the object
255 /// file that the section resides in. No copies of the data will be
256 /// make unless the object file has been read from memory. If the
257 /// object file is on disk, it will shared the mmap data for the
258 /// entire object file.
259 ///
260 /// \param[in] data
261 /// Where to place the data, address byte size, and byte order
262 ///
263 /// \return
264 /// The number of bytes read from the section, or zero if the
265 /// section has no data or \a offset is not a valid offset
266 /// in this section.
268
269 uint32_t GetLog2Align() { return m_log2align; }
270
271 void SetLog2Align(uint32_t align) { m_log2align = align; }
272
273 bool IsRelocated() const { return m_relocated; }
274
275 void SetIsRelocated(bool b) { m_relocated = b; }
276
277 /// Returns true if this section contains debug information. Symbol tables
278 /// are not considered debug information since some symbols might contain
279 /// debug information (STABS, COFF) but not all symbols do, so to keep this
280 /// fast and simple only sections that contains only debug information should
281 /// return true.
282 bool ContainsOnlyDebugInfo() const;
283
284 /// Returns true if this is a global offset table section.
285 bool IsGOTSection() const;
286
287protected:
288 ObjectFile *m_obj_file; // The object file that data for this section should
289 // be read from
290 lldb::SectionType m_type; // The type of this section
291 lldb::SectionWP m_parent_wp; // Weak pointer to parent section
292 ConstString m_name; // Name of this section
293 lldb::addr_t m_file_addr; // The absolute file virtual address range of this
294 // section if m_parent == NULL,
295 // offset from parent file virtual address if m_parent != NULL
296 lldb::addr_t m_byte_size; // Size in bytes that this section will occupy in
297 // memory at runtime
298 lldb::offset_t m_file_offset; // Object file offset (if any)
299 lldb::offset_t m_file_size; // Object file size (can be smaller than
300 // m_byte_size for zero filled sections...)
301 uint32_t m_log2align; // log_2(align) of the section (i.e. section has to be
302 // aligned to 2^m_log2align)
303 SectionList m_children; // Child sections
304 bool m_fake : 1, // If true, then this section only can contain the address if
305 // one of its
306 // children contains an address. This allows for gaps between the
307 // children that are contained in the address range for this section, but
308 // do not produce hits unless the children contain the address.
309 m_encrypted : 1, // Set to true if the contents are encrypted
310 m_thread_specific : 1, // This section is thread specific
311 m_readable : 1, // If this section has read permissions
312 m_writable : 1, // If this section has write permissions
313 m_executable : 1, // If this section has executable permissions
314 m_relocated : 1; // If this section has had relocations applied
315private:
316 Section(const Section &) = delete;
317 const Section &operator=(const Section &) = delete;
318};
319
320} // namespace lldb_private
321
322namespace llvm {
323namespace json {
324
325bool fromJSON(const llvm::json::Value &value,
326 lldb_private::JSONSection &section, llvm::json::Path path);
327
328bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type,
329 llvm::json::Path path);
330
331} // namespace json
332} // namespace llvm
333
334#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:46
const_iterator begin() const
Definition Section.h:41
bool ContainsSection(lldb::user_id_t sect_id) const
Definition Section.cpp:642
const_iterator end() const
Definition Section.h:42
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
Definition Section.cpp:560
collection::iterator iterator
Definition Section.h:38
static SectionList Merge(SectionList &lhs, SectionList &rhs, MergeCallback filter)
Definition Section.cpp:688
size_t GetNumSections(uint32_t depth) const
Definition Section.cpp:542
lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const
Definition Section.cpp:582
SectionList()=default
Create an empty list.
size_t Slide(lldb::addr_t slide_amount, bool slide_children)
Definition Section.cpp:666
lldb::SectionSP FindSectionContainingFileAddress(lldb::addr_t addr, uint32_t depth=UINT32_MAX) const
Definition Section.cpp:619
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition Section.cpp:676
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:495
std::vector< lldb::SectionSP > collection
Definition Section.h:37
size_t AddSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:484
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition Section.cpp:524
SectionList & operator=(const SectionList &rhs)
Definition Section.cpp:478
size_t FindSectionIndex(const Section *sect)
Definition Section.cpp:503
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition Section.cpp:600
size_t AddUniqueSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:516
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:646
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:553
collection::const_iterator const_iterator
Definition Section.h:39
bool IsThreadSpecific() const
Definition Section.h:221
bool ContainsFileAddress(lldb::addr_t vm_addr) const
Definition Section.cpp:265
lldb::addr_t m_byte_size
Definition Section.h:296
const SectionList & GetChildren() const
Definition Section.h:169
lldb::addr_t GetOffset() const
Definition Section.cpp:219
const Section & operator=(const Section &)=delete
const ObjectFile * GetObjectFile() const
Definition Section.h:232
lldb::addr_t m_file_addr
Definition Section.h:293
void SetPermissions(uint32_t permissions)
Set the permissions using bits OR'ed from lldb::Permissions.
Definition Section.cpp:371
ConstString GetName() const
Definition Section.h:211
void SetIsFake(bool fake)
Definition Section.h:203
bool IsFake() const
Definition Section.h:201
void DumpName(llvm::raw_ostream &s) const
Definition Section.cpp:312
void SetByteSize(lldb::addr_t byte_size)
Definition Section.h:199
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:377
bool IsDescendant(const Section *section)
Definition Section.cpp:334
void SetFileOffset(lldb::offset_t file_offset)
Definition Section.h:183
bool IsGOTSection() const
Returns true if this is a global offset table section.
Definition Section.cpp:470
ObjectFile * m_obj_file
Definition Section.h:288
void SetIsRelocated(bool b)
Definition Section.h:275
lldb::SectionSP GetParent() const
Definition Section.h:219
uint32_t GetPermissions() const
Get the permissions as OR'ed bits from lldb::Permissions.
Definition Section.cpp:359
bool SetFileAddress(lldb::addr_t file_addr)
Definition Section.cpp:206
lldb::offset_t GetFileOffset() const
Definition Section.h:181
lldb::SectionWP m_parent_wp
Definition Section.h:291
ConstString m_name
Definition Section.h:292
lldb::addr_t GetFileAddress() const
Definition Section.cpp:194
SectionList & GetChildren()
Definition Section.h:167
lldb::SectionType m_type
Definition Section.h:290
ObjectFile * GetObjectFile()
Definition Section.h:231
bool ContainsOnlyDebugInfo() const
Returns true if this section contains debug information.
Definition Section.cpp:390
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, uint32_t depth) const
Definition Section.cpp:276
void SetFileSize(lldb::offset_t file_size)
Definition Section.h:189
uint32_t GetLog2Align()
Definition Section.h:269
const char * GetTypeAsCString() const
Definition Section.cpp:28
lldb::SectionType GetType() const
Definition Section.h:215
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:229
bool Slide(lldb::addr_t slide_amount, bool slide_children)
Definition Section.cpp:343
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:244
void SetLog2Align(uint32_t align)
Definition Section.h:271
lldb::offset_t m_file_offset
Definition Section.h:298
lldb::offset_t m_file_size
Definition Section.h:299
lldb::addr_t GetByteSize() const
Definition Section.h:197
SectionList m_children
Definition Section.h:303
void SetIsThreadSpecific(bool b)
Definition Section.h:223
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)
Definition Section.cpp:164
void SetIsEncrypted(bool b)
Definition Section.h:207
lldb::offset_t GetFileSize() const
Definition Section.h:187
bool IsEncrypted() const
Definition Section.h:205
bool IsRelocated() const
Definition Section.h:273
#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:718
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