1 //===-- Section.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Section_h_
11 #define liblldb_Section_h_
13 #include "lldb/Core/ModuleChild.h"
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/Utility/Flags.h"
16 #include "lldb/Utility/UserID.h"
17 #include "lldb/lldb-defines.h" // for DISALLOW_COPY_AND_ASSIGN
18 #include "lldb/lldb-enumerations.h" // for SectionType
19 #include "lldb/lldb-forward.h" // for SectionSP, ModuleSP, SectionWP
20 #include "lldb/lldb-types.h" // for addr_t, offset_t, user_id_t
22 #include <memory> // for enable_shared_from_this
23 #include <vector> // for vector
25 #include <stddef.h> // for size_t
26 #include <stdint.h> // for uint32_t, UINT32_MAX
28 namespace lldb_private {
31 namespace lldb_private {
34 namespace lldb_private {
37 namespace lldb_private {
40 namespace lldb_private {
43 namespace lldb_private {
47 namespace lldb_private {
51 typedef std::vector<lldb::SectionSP> collection;
52 typedef collection::iterator iterator;
53 typedef collection::const_iterator const_iterator;
59 SectionList &operator=(const SectionList &rhs);
61 size_t AddSection(const lldb::SectionSP §ion_sp);
63 size_t AddUniqueSection(const lldb::SectionSP §ion_sp);
65 size_t FindSectionIndex(const Section *sect);
67 bool ContainsSection(lldb::user_id_t sect_id) const;
69 void Dump(Stream *s, Target *target, bool show_header, uint32_t depth) const;
71 lldb::SectionSP FindSectionByName(const ConstString §ion_dstr) const;
73 lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const;
75 lldb::SectionSP FindSectionByType(lldb::SectionType sect_type,
77 size_t start_idx = 0) const;
80 FindSectionContainingFileAddress(lldb::addr_t addr,
81 uint32_t depth = UINT32_MAX) const;
83 // Get the number of sections in this list only
84 size_t GetSize() const { return m_sections.size(); }
86 // Get the number of sections in this list, and any contained child sections
87 size_t GetNumSections(uint32_t depth) const;
89 bool ReplaceSection(lldb::user_id_t sect_id,
90 const lldb::SectionSP §ion_sp,
91 uint32_t depth = UINT32_MAX);
93 // Warning, this can be slow as it's removing items from a std::vector.
94 bool DeleteSection(size_t idx);
96 lldb::SectionSP GetSectionAtIndex(size_t idx) const;
98 size_t Slide(lldb::addr_t slide_amount, bool slide_children);
100 void Clear() { m_sections.clear(); }
103 collection m_sections;
106 class Section : public std::enable_shared_from_this<Section>,
111 // Create a root section (one that has no parent)
112 Section(const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
113 lldb::user_id_t sect_id, const ConstString &name,
114 lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
115 lldb::addr_t vm_size, lldb::offset_t file_offset,
116 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
117 uint32_t target_byte_size = 1);
119 // Create a section that is a child of parent_section_sp
120 Section(const lldb::SectionSP &parent_section_sp, // NULL for top level
121 // sections, non-NULL for
123 const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
124 lldb::user_id_t sect_id, const ConstString &name,
125 lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
126 lldb::addr_t vm_size, lldb::offset_t file_offset,
127 lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
128 uint32_t target_byte_size = 1);
132 static int Compare(const Section &a, const Section &b);
134 bool ContainsFileAddress(lldb::addr_t vm_addr) const;
136 SectionList &GetChildren() { return m_children; }
138 const SectionList &GetChildren() const { return m_children; }
140 void Dump(Stream *s, Target *target, uint32_t depth) const;
142 void DumpName(Stream *s) const;
144 lldb::addr_t GetLoadBaseAddress(Target *target) const;
146 bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr,
147 bool allow_section_end = false) const;
149 lldb::offset_t GetFileOffset() const { return m_file_offset; }
151 void SetFileOffset(lldb::offset_t file_offset) {
152 m_file_offset = file_offset;
155 lldb::offset_t GetFileSize() const { return m_file_size; }
157 void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; }
159 lldb::addr_t GetFileAddress() const;
161 bool SetFileAddress(lldb::addr_t file_addr);
163 lldb::addr_t GetOffset() const;
165 lldb::addr_t GetByteSize() const { return m_byte_size; }
167 void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
169 bool IsFake() const { return m_fake; }
171 void SetIsFake(bool fake) { m_fake = fake; }
173 bool IsEncrypted() const { return m_encrypted; }
175 void SetIsEncrypted(bool b) { m_encrypted = b; }
177 bool IsDescendant(const Section *section);
179 const ConstString &GetName() const { return m_name; }
181 bool Slide(lldb::addr_t slide_amount, bool slide_children);
183 lldb::SectionType GetType() const { return m_type; }
185 const char *GetTypeAsCString() const;
187 lldb::SectionSP GetParent() const { return m_parent_wp.lock(); }
189 bool IsThreadSpecific() const { return m_thread_specific; }
191 void SetIsThreadSpecific(bool b) { m_thread_specific = b; }
193 //------------------------------------------------------------------
194 /// Get the permissions as OR'ed bits from lldb::Permissions
195 //------------------------------------------------------------------
196 uint32_t GetPermissions() const;
198 //------------------------------------------------------------------
199 /// Set the permissions using bits OR'ed from lldb::Permissions
200 //------------------------------------------------------------------
201 void SetPermissions(uint32_t permissions);
203 ObjectFile *GetObjectFile() { return m_obj_file; }
204 const ObjectFile *GetObjectFile() const { return m_obj_file; }
206 //------------------------------------------------------------------
207 /// Read the section data from the object file that the section
211 /// Where to place the data
213 /// @param[in] dst_len
214 /// How many bytes of section data to read
216 /// @param[in] offset
217 /// The offset in bytes within this section's data at which to
218 /// start copying data from.
221 /// The number of bytes read from the section, or zero if the
222 /// section has no data or \a offset is not a valid offset
224 //------------------------------------------------------------------
225 lldb::offset_t GetSectionData(void *dst, lldb::offset_t dst_len,
226 lldb::offset_t offset = 0);
228 //------------------------------------------------------------------
229 /// Get the shared reference to the section data from the object
230 /// file that the section resides in. No copies of the data will be
231 /// make unless the object file has been read from memory. If the
232 /// object file is on disk, it will shared the mmap data for the
233 /// entire object file.
236 /// Where to place the data, address byte size, and byte order
239 /// The number of bytes read from the section, or zero if the
240 /// section has no data or \a offset is not a valid offset
242 //------------------------------------------------------------------
243 lldb::offset_t GetSectionData(DataExtractor &data);
245 uint32_t GetLog2Align() { return m_log2align; }
247 void SetLog2Align(uint32_t align) { m_log2align = align; }
249 // Get the number of host bytes required to hold a target byte
250 uint32_t GetTargetByteSize() const { return m_target_byte_size; }
252 bool IsRelocated() const { return m_relocated; }
254 void SetIsRelocated(bool b) { m_relocated = b; }
257 ObjectFile *m_obj_file; // The object file that data for this section should
259 lldb::SectionType m_type; // The type of this section
260 lldb::SectionWP m_parent_wp; // Weak pointer to parent section
261 ConstString m_name; // Name of this section
262 lldb::addr_t m_file_addr; // The absolute file virtual address range of this
263 // section if m_parent == NULL,
264 // offset from parent file virtual address if m_parent != NULL
265 lldb::addr_t m_byte_size; // Size in bytes that this section will occupy in
267 lldb::offset_t m_file_offset; // Object file offset (if any)
268 lldb::offset_t m_file_size; // Object file size (can be smaller than
269 // m_byte_size for zero filled sections...)
270 uint32_t m_log2align; // log_2(align) of the section (i.e. section has to be
271 // aligned to 2^m_log2align)
272 SectionList m_children; // Child sections
273 bool m_fake : 1, // If true, then this section only can contain the address if
275 // children contains an address. This allows for gaps between the
276 // children that are contained in the address range for this section, but
277 // do not produce hits unless the children contain the address.
278 m_encrypted : 1, // Set to true if the contents are encrypted
279 m_thread_specific : 1, // This section is thread specific
280 m_readable : 1, // If this section has read permissions
281 m_writable : 1, // If this section has write permissions
282 m_executable : 1, // If this section has executable permissions
283 m_relocated : 1; // If this section has had relocations applied
284 uint32_t m_target_byte_size; // Some architectures have non-8-bit byte size.
285 // This is specified as
286 // as a multiple number of a host bytes
288 DISALLOW_COPY_AND_ASSIGN(Section);
291 } // namespace lldb_private
293 #endif // liblldb_Section_h_