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 
12 #include "lldb/Core/ModuleChild.h"
14 #include "lldb/Utility/Flags.h"
15 #include "lldb/Utility/UserID.h"
16 #include "lldb/lldb-defines.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-forward.h"
19 #include "lldb/lldb-types.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 namespace lldb_private {
28 class Address;
29 class DataExtractor;
30 class ObjectFile;
31 class Section;
32 class Target;
33 
34 class SectionList {
35 public:
36  typedef std::vector<lldb::SectionSP> collection;
37  typedef collection::iterator iterator;
38  typedef collection::const_iterator const_iterator;
39 
40  const_iterator begin() const { return m_sections.begin(); }
41  const_iterator end() const { return m_sections.end(); }
42  const_iterator begin() { return m_sections.begin(); }
43  const_iterator end() { return m_sections.end(); }
44 
45  /// Create an empty list.
46  SectionList() = default;
47 
48  SectionList &operator=(const SectionList &rhs);
49 
50  size_t AddSection(const lldb::SectionSP &section_sp);
51 
52  size_t AddUniqueSection(const lldb::SectionSP &section_sp);
53 
54  size_t FindSectionIndex(const Section *sect);
55 
56  bool ContainsSection(lldb::user_id_t sect_id) const;
57 
58  void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
59  bool show_header, uint32_t depth) const;
60 
61  lldb::SectionSP FindSectionByName(ConstString section_dstr) const;
62 
63  lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const;
64 
65  lldb::SectionSP FindSectionByType(lldb::SectionType sect_type,
66  bool check_children,
67  size_t start_idx = 0) const;
68 
69  lldb::SectionSP
71  uint32_t depth = UINT32_MAX) const;
72 
73  // Get the number of sections in this list only
74  size_t GetSize() const { return m_sections.size(); }
75 
76  // Get the number of sections in this list, and any contained child sections
77  size_t GetNumSections(uint32_t depth) const;
78 
79  bool ReplaceSection(lldb::user_id_t sect_id,
80  const lldb::SectionSP &section_sp,
81  uint32_t depth = UINT32_MAX);
82 
83  // Warning, this can be slow as it's removing items from a std::vector.
84  bool DeleteSection(size_t idx);
85 
86  lldb::SectionSP GetSectionAtIndex(size_t idx) const;
87 
88  size_t Slide(lldb::addr_t slide_amount, bool slide_children);
89 
90  void Clear() { m_sections.clear(); }
91 
92  /// Get the debug information size from all sections that contain debug
93  /// information. Symbol tables are not considered part of the debug
94  /// information for this call, just known sections that contain debug
95  /// information.
96  uint64_t GetDebugInfoSize() const;
97 
98 protected:
100 };
101 
102 class Section : public std::enable_shared_from_this<Section>,
103  public ModuleChild,
104  public UserID,
105  public Flags {
106 public:
107  // Create a root section (one that has no parent)
108  Section(const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
109  lldb::user_id_t sect_id, ConstString name,
110  lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
111  lldb::addr_t vm_size, lldb::offset_t file_offset,
112  lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
113  uint32_t target_byte_size = 1);
114 
115  // Create a section that is a child of parent_section_sp
116  Section(const lldb::SectionSP &parent_section_sp, // NULL for top level
117  // sections, non-NULL for
118  // child sections
119  const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
120  lldb::user_id_t sect_id, ConstString name,
121  lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
122  lldb::addr_t vm_size, lldb::offset_t file_offset,
123  lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
124  uint32_t target_byte_size = 1);
125 
126  ~Section();
127 
128  static int Compare(const Section &a, const Section &b);
129 
130  bool ContainsFileAddress(lldb::addr_t vm_addr) const;
131 
133 
134  const SectionList &GetChildren() const { return m_children; }
135 
136  void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
137  uint32_t depth) const;
138 
139  void DumpName(llvm::raw_ostream &s) const;
140 
141  lldb::addr_t GetLoadBaseAddress(Target *target) const;
142 
143  bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr,
144  bool allow_section_end = false) const;
145 
147 
148  void SetFileOffset(lldb::offset_t file_offset) {
149  m_file_offset = file_offset;
150  }
151 
153 
154  void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; }
155 
157 
158  bool SetFileAddress(lldb::addr_t file_addr);
159 
160  lldb::addr_t GetOffset() const;
161 
163 
164  void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
165 
166  bool IsFake() const { return m_fake; }
167 
168  void SetIsFake(bool fake) { m_fake = fake; }
169 
170  bool IsEncrypted() const { return m_encrypted; }
171 
172  void SetIsEncrypted(bool b) { m_encrypted = b; }
173 
174  bool IsDescendant(const Section *section);
175 
176  ConstString GetName() const { return m_name; }
177 
178  bool Slide(lldb::addr_t slide_amount, bool slide_children);
179 
180  lldb::SectionType GetType() const { return m_type; }
181 
182  const char *GetTypeAsCString() const;
183 
184  lldb::SectionSP GetParent() const { return m_parent_wp.lock(); }
185 
186  bool IsThreadSpecific() const { return m_thread_specific; }
187 
189 
190  /// Get the permissions as OR'ed bits from lldb::Permissions
191  uint32_t GetPermissions() const;
192 
193  /// Set the permissions using bits OR'ed from lldb::Permissions
194  void SetPermissions(uint32_t permissions);
195 
197  const ObjectFile *GetObjectFile() const { return m_obj_file; }
198 
199  /// Read the section data from the object file that the section
200  /// resides in.
201  ///
202  /// \param[in] dst
203  /// Where to place the data
204  ///
205  /// \param[in] dst_len
206  /// How many bytes of section data to read
207  ///
208  /// \param[in] offset
209  /// The offset in bytes within this section's data at which to
210  /// start copying data from.
211  ///
212  /// \return
213  /// The number of bytes read from the section, or zero if the
214  /// section has no data or \a offset is not a valid offset
215  /// in this section.
216  lldb::offset_t GetSectionData(void *dst, lldb::offset_t dst_len,
217  lldb::offset_t offset = 0);
218 
219  /// Get the shared reference to the section data from the object
220  /// file that the section resides in. No copies of the data will be
221  /// make unless the object file has been read from memory. If the
222  /// object file is on disk, it will shared the mmap data for the
223  /// entire object file.
224  ///
225  /// \param[in] data
226  /// Where to place the data, address byte size, and byte order
227  ///
228  /// \return
229  /// The number of bytes read from the section, or zero if the
230  /// section has no data or \a offset is not a valid offset
231  /// in this section.
233 
235 
236  void SetLog2Align(uint32_t align) { m_log2align = align; }
237 
238  // Get the number of host bytes required to hold a target byte
240 
241  bool IsRelocated() const { return m_relocated; }
242 
243  void SetIsRelocated(bool b) { m_relocated = b; }
244 
245  /// Returns true if this section contains debug information. Symbol tables
246  /// are not considered debug information since some symbols might contain
247  /// debug information (STABS, COFF) but not all symbols do, so to keep this
248  /// fast and simple only sections that contains only debug information should
249  /// return true.
250  bool ContainsOnlyDebugInfo() const;
251 
252 protected:
253  ObjectFile *m_obj_file; // The object file that data for this section should
254  // be read from
255  lldb::SectionType m_type; // The type of this section
256  lldb::SectionWP m_parent_wp; // Weak pointer to parent section
257  ConstString m_name; // Name of this section
258  lldb::addr_t m_file_addr; // The absolute file virtual address range of this
259  // section if m_parent == NULL,
260  // offset from parent file virtual address if m_parent != NULL
261  lldb::addr_t m_byte_size; // Size in bytes that this section will occupy in
262  // memory at runtime
263  lldb::offset_t m_file_offset; // Object file offset (if any)
264  lldb::offset_t m_file_size; // Object file size (can be smaller than
265  // m_byte_size for zero filled sections...)
266  uint32_t m_log2align; // log_2(align) of the section (i.e. section has to be
267  // aligned to 2^m_log2align)
268  SectionList m_children; // Child sections
269  bool m_fake : 1, // If true, then this section only can contain the address if
270  // one of its
271  // children contains an address. This allows for gaps between the
272  // children that are contained in the address range for this section, but
273  // do not produce hits unless the children contain the address.
274  m_encrypted : 1, // Set to true if the contents are encrypted
275  m_thread_specific : 1, // This section is thread specific
276  m_readable : 1, // If this section has read permissions
277  m_writable : 1, // If this section has write permissions
278  m_executable : 1, // If this section has executable permissions
279  m_relocated : 1; // If this section has had relocations applied
280  uint32_t m_target_byte_size; // Some architectures have non-8-bit byte size.
281  // This is specified as
282  // as a multiple number of a host bytes
283 private:
284  Section(const Section &) = delete;
285  const Section &operator=(const Section &) = delete;
286 };
287 
288 } // namespace lldb_private
289 
290 #endif // LLDB_CORE_SECTION_H
lldb_private::SectionList::ReplaceSection
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition: Section.cpp:517
lldb_private::SectionList::FindSectionContainingFileAddress
lldb::SectionSP FindSectionContainingFileAddress(lldb::addr_t addr, uint32_t depth=UINT32_MAX) const
Definition: Section.cpp:613
lldb_private::SectionList::Clear
void Clear()
Definition: Section.h:90
lldb_private::SectionList::FindSectionByName
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
Definition: Section.cpp:554
lldb_private::SectionList::begin
const_iterator begin() const
Definition: Section.h:40
lldb_private::ModuleChild
Definition: ModuleChild.h:19
lldb_private::Section::Compare
static int Compare(const Section &a, const Section &b)
lldb_private::SectionList::Dump
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, bool show_header, uint32_t depth) const
Definition: Section.cpp:640
lldb_private::SectionList::collection
std::vector< lldb::SectionSP > collection
Definition: Section.h:36
lldb_private::Section::m_name
ConstString m_name
Definition: Section.h:257
lldb-defines.h
lldb_private::Section::GetType
lldb::SectionType GetType() const
Definition: Section.h:180
lldb_private::Section::IsRelocated
bool IsRelocated() const
Definition: Section.h:241
lldb_private::Section::m_executable
bool m_executable
Definition: Section.h:278
lldb_private::Section::m_encrypted
bool m_encrypted
Definition: Section.h:274
lldb_private::UserID
Definition: UserID.h:31
lldb_private::SectionList::GetSize
size_t GetSize() const
Definition: Section.h:74
lldb_private::Section::SetIsEncrypted
void SetIsEncrypted(bool b)
Definition: Section.h:172
lldb_private::SectionList::ContainsSection
bool ContainsSection(lldb::user_id_t sect_id) const
Definition: Section.cpp:636
lldb_private::Flags
Definition: Flags.h:22
lldb_private::SectionList::Slide
size_t Slide(lldb::addr_t slide_amount, bool slide_children)
Definition: Section.cpp:663
lldb_private::Section::Dump
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, uint32_t depth) const
Definition: Section.cpp:285
lldb_private::SectionList
Definition: Section.h:34
lldb_private::Section::GetFileSize
lldb::offset_t GetFileSize() const
Definition: Section.h:152
lldb_private::Section::m_file_offset
lldb::offset_t m_file_offset
Definition: Section.h:263
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Section::m_thread_specific
bool m_thread_specific
Definition: Section.h:275
lldb_private::Section::GetTargetByteSize
uint32_t GetTargetByteSize() const
Definition: Section.h:239
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Section::IsDescendant
bool IsDescendant(const Section *section)
Definition: Section.cpp:343
lldb_private::Section::DumpName
void DumpName(llvm::raw_ostream &s) const
Definition: Section.cpp:321
lldb_private::SectionList::GetNumSections
size_t GetNumSections(uint32_t depth) const
Definition: Section.cpp:535
lldb_private::Target
Definition: Target.h:451
lldb_private::Section::SetIsRelocated
void SetIsRelocated(bool b)
Definition: Section.h:243
lldb_private::Section::IsEncrypted
bool IsEncrypted() const
Definition: Section.h:170
lldb_private::Section::GetObjectFile
const ObjectFile * GetObjectFile() const
Definition: Section.h:197
lldb_private::Section::GetParent
lldb::SectionSP GetParent() const
Definition: Section.h:184
lldb_private::Section::m_file_size
lldb::offset_t m_file_size
Definition: Section.h:264
lldb_private::Section::m_file_addr
lldb::addr_t m_file_addr
Definition: Section.h:258
lldb_private::Section::ContainsOnlyDebugInfo
bool ContainsOnlyDebugInfo() const
Returns true if this section contains debug information.
Definition: Section.cpp:399
lldb_private::Section::GetTypeAsCString
const char * GetTypeAsCString() const
Definition: Section.cpp:28
lldb_private::SectionList::AddSection
size_t AddSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:477
lldb_private::SectionList::end
const_iterator end() const
Definition: Section.h:41
lldb_private::Section::GetFileAddress
lldb::addr_t GetFileAddress() const
Definition: Section.cpp:203
lldb_private::Section::m_obj_file
ObjectFile * m_obj_file
Definition: Section.h:253
lldb_private::SectionList::operator=
SectionList & operator=(const SectionList &rhs)
Definition: Section.cpp:471
lldb_private::Section::SetFileAddress
bool SetFileAddress(lldb::addr_t file_addr)
Definition: Section.cpp:215
lldb_private::SectionList::SectionList
SectionList()=default
Create an empty list.
lldb_private::Section::SetIsFake
void SetIsFake(bool fake)
Definition: Section.h:168
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::Section::Section
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)
lldb_private::Section::m_log2align
uint32_t m_log2align
Definition: Section.h:266
lldb_private::Section::SetIsThreadSpecific
void SetIsThreadSpecific(bool b)
Definition: Section.h:188
lldb_private::Section::GetChildren
SectionList & GetChildren()
Definition: Section.h:132
lldb_private::Section::Slide
bool Slide(lldb::addr_t slide_amount, bool slide_children)
Definition: Section.cpp:352
lldb_private::SectionList::GetDebugInfoSize
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition: Section.cpp:673
lldb_private::Section::IsThreadSpecific
bool IsThreadSpecific() const
Definition: Section.h:186
lldb_private::SectionList::FindSectionIndex
size_t FindSectionIndex(const Section *sect)
Definition: Section.cpp:496
lldb_private::Section::SetFileOffset
void SetFileOffset(lldb::offset_t file_offset)
Definition: Section.h:148
lldb_private::Section::m_fake
bool m_fake
Definition: Section.h:269
lldb_private::Section::m_writable
bool m_writable
Definition: Section.h:277
lldb_private::Section::IsFake
bool IsFake() const
Definition: Section.h:166
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Section::GetName
ConstString GetName() const
Definition: Section.h:176
lldb-enumerations.h
lldb_private::Section::m_type
lldb::SectionType m_type
Definition: Section.h:255
lldb_private::Section::GetOffset
lldb::addr_t GetOffset() const
Definition: Section.cpp:228
lldb_private::SectionList::GetSectionAtIndex
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:546
lldb_private::Section::GetLoadBaseAddress
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition: Section.cpp:238
lldb_private::Section::SetPermissions
void SetPermissions(uint32_t permissions)
Set the permissions using bits OR'ed from lldb::Permissions.
Definition: Section.cpp:380
lldb_private::SectionList::DeleteSection
bool DeleteSection(size_t idx)
Definition: Section.cpp:488
UserID.h
lldb_private::Section::m_byte_size
lldb::addr_t m_byte_size
Definition: Section.h:261
lldb_private::Section::m_parent_wp
lldb::SectionWP m_parent_wp
Definition: Section.h:256
lldb_private::Section::GetChildren
const SectionList & GetChildren() const
Definition: Section.h:134
lldb_private::Section
Definition: Section.h:102
lldb-types.h
lldb_private::SectionList::FindSectionByType
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:594
lldb_private::Section::GetByteSize
lldb::addr_t GetByteSize() const
Definition: Section.h:162
lldb_private::Section::GetLog2Align
uint32_t GetLog2Align()
Definition: Section.h:234
uint32_t
lldb_private::Section::SetByteSize
void SetByteSize(lldb::addr_t byte_size)
Definition: Section.h:164
lldb_private::Section::m_children
SectionList m_children
Definition: Section.h:268
lldb_private::Section::SetFileSize
void SetFileSize(lldb::offset_t file_size)
Definition: Section.h:154
lldb::SectionType
SectionType
Definition: lldb-enumerations.h:646
ModuleChild.h
lldb_private::Address
Definition: Address.h:59
lldb_private::Section::m_target_byte_size
uint32_t m_target_byte_size
Definition: Section.h:280
lldb_private::SectionList::const_iterator
collection::const_iterator const_iterator
Definition: Section.h:38
lldb_private::Section::~Section
~Section()
Definition: Section.cpp:199
lldb_private::Section::SetLog2Align
void SetLog2Align(uint32_t align)
Definition: Section.h:236
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Section::m_relocated
bool m_relocated
Definition: Section.h:279
lldb_private::SectionList::m_sections
collection m_sections
Definition: Section.h:99
lldb_private::SectionList::begin
const_iterator begin()
Definition: Section.h:42
lldb-forward.h
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Section::GetFileOffset
lldb::offset_t GetFileOffset() const
Definition: Section.h:146
lldb_private::Section::ResolveContainedAddress
bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr, bool allow_section_end=false) const
Definition: Section.cpp:253
ConstString.h
Flags.h
lldb_private::SectionList::end
const_iterator end()
Definition: Section.h:43
lldb_private::Section::GetObjectFile
ObjectFile * GetObjectFile()
Definition: Section.h:196
lldb_private::SectionList::AddUniqueSection
size_t AddUniqueSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:509
lldb_private::Section::ContainsFileAddress
bool ContainsFileAddress(lldb::addr_t vm_addr) const
Definition: Section.cpp:274
lldb_private::Section::operator=
const Section & operator=(const Section &)=delete
lldb_private::SectionList::iterator
collection::iterator iterator
Definition: Section.h:37
lldb_private::Section::m_readable
bool m_readable
Definition: Section.h:276
lldb_private::ObjectFile
Definition: ObjectFile.h:59
lldb_private::Section::GetSectionData
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:386
lldb_private::SectionList::FindSectionByID
lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const
Definition: Section.cpp:576
lldb_private::Section::GetPermissions
uint32_t GetPermissions() const
Get the permissions as OR'ed bits from lldb::Permissions.
Definition: Section.cpp:368