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