1 //===-- Section.h -----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Section_h_ 11 #define liblldb_Section_h_ 12 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" 18 #include "lldb/lldb-enumerations.h" 19 #include "lldb/lldb-forward.h" 20 #include "lldb/lldb-types.h" 21 22 #include <memory> 23 #include <vector> 24 25 #include <stddef.h> 26 #include <stdint.h> 27 28 namespace lldb_private { 29 class Address; 30 } 31 namespace lldb_private { 32 class DataExtractor; 33 } 34 namespace lldb_private { 35 class ObjectFile; 36 } 37 namespace lldb_private { 38 class Section; 39 } 40 namespace lldb_private { 41 class Stream; 42 } 43 namespace lldb_private { 44 class Target; 45 } 46 47 namespace lldb_private { 48 49 class SectionList { 50 public: 51 typedef std::vector<lldb::SectionSP> collection; 52 typedef collection::iterator iterator; 53 typedef collection::const_iterator const_iterator; 54 55 SectionList(); 56 57 ~SectionList(); 58 59 SectionList &operator=(const SectionList &rhs); 60 61 size_t AddSection(const lldb::SectionSP §ion_sp); 62 63 size_t AddUniqueSection(const lldb::SectionSP §ion_sp); 64 65 size_t FindSectionIndex(const Section *sect); 66 67 bool ContainsSection(lldb::user_id_t sect_id) const; 68 69 void Dump(Stream *s, Target *target, bool show_header, uint32_t depth) const; 70 71 lldb::SectionSP FindSectionByName(const ConstString §ion_dstr) const; 72 73 lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const; 74 75 lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, 76 bool check_children, 77 size_t start_idx = 0) const; 78 79 lldb::SectionSP 80 FindSectionContainingFileAddress(lldb::addr_t addr, 81 uint32_t depth = UINT32_MAX) const; 82 83 // Get the number of sections in this list only GetSize()84 size_t GetSize() const { return m_sections.size(); } 85 86 // Get the number of sections in this list, and any contained child sections 87 size_t GetNumSections(uint32_t depth) const; 88 89 bool ReplaceSection(lldb::user_id_t sect_id, 90 const lldb::SectionSP §ion_sp, 91 uint32_t depth = UINT32_MAX); 92 93 // Warning, this can be slow as it's removing items from a std::vector. 94 bool DeleteSection(size_t idx); 95 96 lldb::SectionSP GetSectionAtIndex(size_t idx) const; 97 98 size_t Slide(lldb::addr_t slide_amount, bool slide_children); 99 Clear()100 void Clear() { m_sections.clear(); } 101 102 protected: 103 collection m_sections; 104 }; 105 106 class Section : public std::enable_shared_from_this<Section>, 107 public ModuleChild, 108 public UserID, 109 public Flags { 110 public: 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); 118 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 122 // child sections 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); 129 130 ~Section(); 131 132 static int Compare(const Section &a, const Section &b); 133 134 bool ContainsFileAddress(lldb::addr_t vm_addr) const; 135 GetChildren()136 SectionList &GetChildren() { return m_children; } 137 GetChildren()138 const SectionList &GetChildren() const { return m_children; } 139 140 void Dump(Stream *s, Target *target, uint32_t depth) const; 141 142 void DumpName(Stream *s) const; 143 144 lldb::addr_t GetLoadBaseAddress(Target *target) const; 145 146 bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr, 147 bool allow_section_end = false) const; 148 GetFileOffset()149 lldb::offset_t GetFileOffset() const { return m_file_offset; } 150 SetFileOffset(lldb::offset_t file_offset)151 void SetFileOffset(lldb::offset_t file_offset) { 152 m_file_offset = file_offset; 153 } 154 GetFileSize()155 lldb::offset_t GetFileSize() const { return m_file_size; } 156 SetFileSize(lldb::offset_t file_size)157 void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; } 158 159 lldb::addr_t GetFileAddress() const; 160 161 bool SetFileAddress(lldb::addr_t file_addr); 162 163 lldb::addr_t GetOffset() const; 164 GetByteSize()165 lldb::addr_t GetByteSize() const { return m_byte_size; } 166 SetByteSize(lldb::addr_t byte_size)167 void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; } 168 IsFake()169 bool IsFake() const { return m_fake; } 170 SetIsFake(bool fake)171 void SetIsFake(bool fake) { m_fake = fake; } 172 IsEncrypted()173 bool IsEncrypted() const { return m_encrypted; } 174 SetIsEncrypted(bool b)175 void SetIsEncrypted(bool b) { m_encrypted = b; } 176 177 bool IsDescendant(const Section *section); 178 GetName()179 const ConstString &GetName() const { return m_name; } 180 181 bool Slide(lldb::addr_t slide_amount, bool slide_children); 182 GetType()183 lldb::SectionType GetType() const { return m_type; } 184 185 const char *GetTypeAsCString() const; 186 GetParent()187 lldb::SectionSP GetParent() const { return m_parent_wp.lock(); } 188 IsThreadSpecific()189 bool IsThreadSpecific() const { return m_thread_specific; } 190 SetIsThreadSpecific(bool b)191 void SetIsThreadSpecific(bool b) { m_thread_specific = b; } 192 193 //------------------------------------------------------------------ 194 /// Get the permissions as OR'ed bits from lldb::Permissions 195 //------------------------------------------------------------------ 196 uint32_t GetPermissions() const; 197 198 //------------------------------------------------------------------ 199 /// Set the permissions using bits OR'ed from lldb::Permissions 200 //------------------------------------------------------------------ 201 void SetPermissions(uint32_t permissions); 202 GetObjectFile()203 ObjectFile *GetObjectFile() { return m_obj_file; } GetObjectFile()204 const ObjectFile *GetObjectFile() const { return m_obj_file; } 205 206 //------------------------------------------------------------------ 207 /// Read the section data from the object file that the section 208 /// resides in. 209 /// 210 /// @param[in] dst 211 /// Where to place the data 212 /// 213 /// @param[in] dst_len 214 /// How many bytes of section data to read 215 /// 216 /// @param[in] offset 217 /// The offset in bytes within this section's data at which to 218 /// start copying data from. 219 /// 220 /// @return 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 223 /// in this section. 224 //------------------------------------------------------------------ 225 lldb::offset_t GetSectionData(void *dst, lldb::offset_t dst_len, 226 lldb::offset_t offset = 0); 227 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. 234 /// 235 /// @param[in] data 236 /// Where to place the data, address byte size, and byte order 237 /// 238 /// @return 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 241 /// in this section. 242 //------------------------------------------------------------------ 243 lldb::offset_t GetSectionData(DataExtractor &data); 244 GetLog2Align()245 uint32_t GetLog2Align() { return m_log2align; } 246 SetLog2Align(uint32_t align)247 void SetLog2Align(uint32_t align) { m_log2align = align; } 248 249 // Get the number of host bytes required to hold a target byte GetTargetByteSize()250 uint32_t GetTargetByteSize() const { return m_target_byte_size; } 251 IsRelocated()252 bool IsRelocated() const { return m_relocated; } 253 SetIsRelocated(bool b)254 void SetIsRelocated(bool b) { m_relocated = b; } 255 256 protected: 257 ObjectFile *m_obj_file; // The object file that data for this section should 258 // be read from 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 266 // memory at runtime 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 274 // one of its 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 287 private: 288 DISALLOW_COPY_AND_ASSIGN(Section); 289 }; 290 291 } // namespace lldb_private 292 293 #endif // liblldb_Section_h_ 294