Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:46

0001 //===-- Section.h -----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_CORE_SECTION_H
0010 #define LLDB_CORE_SECTION_H
0011 
0012 #include "lldb/Core/ModuleChild.h"
0013 #include "lldb/Utility/ConstString.h"
0014 #include "lldb/Utility/Flags.h"
0015 #include "lldb/Utility/UserID.h"
0016 #include "lldb/lldb-defines.h"
0017 #include "lldb/lldb-enumerations.h"
0018 #include "lldb/lldb-forward.h"
0019 #include "lldb/lldb-types.h"
0020 #include "llvm/Support/JSON.h"
0021 
0022 #include <memory>
0023 #include <vector>
0024 
0025 #include <cstddef>
0026 #include <cstdint>
0027 
0028 namespace lldb_private {
0029 class Address;
0030 class DataExtractor;
0031 class ObjectFile;
0032 class Section;
0033 class Target;
0034 
0035 class SectionList {
0036 public:
0037   typedef std::vector<lldb::SectionSP> collection;
0038   typedef collection::iterator iterator;
0039   typedef collection::const_iterator const_iterator;
0040 
0041   const_iterator begin() const { return m_sections.begin(); }
0042   const_iterator end() const { return m_sections.end(); }
0043   const_iterator begin() { return m_sections.begin(); }
0044   const_iterator end() { return m_sections.end(); }
0045 
0046   /// Create an empty list.
0047   SectionList() = default;
0048 
0049   SectionList &operator=(const SectionList &rhs);
0050 
0051   size_t AddSection(const lldb::SectionSP &section_sp);
0052 
0053   size_t AddUniqueSection(const lldb::SectionSP &section_sp);
0054 
0055   size_t FindSectionIndex(const Section *sect);
0056 
0057   bool ContainsSection(lldb::user_id_t sect_id) const;
0058 
0059   void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
0060             bool show_header, uint32_t depth) const;
0061 
0062   lldb::SectionSP FindSectionByName(ConstString section_dstr) const;
0063 
0064   lldb::SectionSP FindSectionByID(lldb::user_id_t sect_id) const;
0065 
0066   lldb::SectionSP FindSectionByType(lldb::SectionType sect_type,
0067                                     bool check_children,
0068                                     size_t start_idx = 0) const;
0069 
0070   lldb::SectionSP
0071   FindSectionContainingFileAddress(lldb::addr_t addr,
0072                                    uint32_t depth = UINT32_MAX) const;
0073 
0074   // Get the number of sections in this list only
0075   size_t GetSize() const { return m_sections.size(); }
0076 
0077   // Get the number of sections in this list, and any contained child sections
0078   size_t GetNumSections(uint32_t depth) const;
0079 
0080   bool ReplaceSection(lldb::user_id_t sect_id,
0081                       const lldb::SectionSP &section_sp,
0082                       uint32_t depth = UINT32_MAX);
0083 
0084   // Warning, this can be slow as it's removing items from a std::vector.
0085   bool DeleteSection(size_t idx);
0086 
0087   lldb::SectionSP GetSectionAtIndex(size_t idx) const;
0088 
0089   size_t Slide(lldb::addr_t slide_amount, bool slide_children);
0090 
0091   void Clear() { m_sections.clear(); }
0092 
0093   /// Get the debug information size from all sections that contain debug
0094   /// information. Symbol tables are not considered part of the debug
0095   /// information for this call, just known sections that contain debug
0096   /// information.
0097   uint64_t GetDebugInfoSize() const;
0098 
0099 protected:
0100   collection m_sections;
0101 };
0102 
0103 struct JSONSection {
0104   std::string name;
0105   std::optional<lldb::SectionType> type;
0106   std::optional<uint64_t> address;
0107   std::optional<uint64_t> size;
0108 };
0109 
0110 class Section : public std::enable_shared_from_this<Section>,
0111                 public ModuleChild,
0112                 public UserID,
0113                 public Flags {
0114 public:
0115   // Create a root section (one that has no parent)
0116   Section(const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
0117           lldb::user_id_t sect_id, ConstString name,
0118           lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
0119           lldb::addr_t vm_size, lldb::offset_t file_offset,
0120           lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
0121           uint32_t target_byte_size = 1);
0122 
0123   // Create a section that is a child of parent_section_sp
0124   Section(const lldb::SectionSP &parent_section_sp, // NULL for top level
0125                                                     // sections, non-NULL for
0126                                                     // child sections
0127           const lldb::ModuleSP &module_sp, ObjectFile *obj_file,
0128           lldb::user_id_t sect_id, ConstString name,
0129           lldb::SectionType sect_type, lldb::addr_t file_vm_addr,
0130           lldb::addr_t vm_size, lldb::offset_t file_offset,
0131           lldb::offset_t file_size, uint32_t log2align, uint32_t flags,
0132           uint32_t target_byte_size = 1);
0133 
0134   ~Section();
0135 
0136   static int Compare(const Section &a, const Section &b);
0137 
0138   bool ContainsFileAddress(lldb::addr_t vm_addr) const;
0139 
0140   SectionList &GetChildren() { return m_children; }
0141 
0142   const SectionList &GetChildren() const { return m_children; }
0143 
0144   void Dump(llvm::raw_ostream &s, unsigned indent, Target *target,
0145             uint32_t depth) const;
0146 
0147   void DumpName(llvm::raw_ostream &s) const;
0148 
0149   lldb::addr_t GetLoadBaseAddress(Target *target) const;
0150 
0151   bool ResolveContainedAddress(lldb::addr_t offset, Address &so_addr,
0152                                bool allow_section_end = false) const;
0153 
0154   lldb::offset_t GetFileOffset() const { return m_file_offset; }
0155 
0156   void SetFileOffset(lldb::offset_t file_offset) {
0157     m_file_offset = file_offset;
0158   }
0159 
0160   lldb::offset_t GetFileSize() const { return m_file_size; }
0161 
0162   void SetFileSize(lldb::offset_t file_size) { m_file_size = file_size; }
0163 
0164   lldb::addr_t GetFileAddress() const;
0165 
0166   bool SetFileAddress(lldb::addr_t file_addr);
0167 
0168   lldb::addr_t GetOffset() const;
0169 
0170   lldb::addr_t GetByteSize() const { return m_byte_size; }
0171 
0172   void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
0173 
0174   bool IsFake() const { return m_fake; }
0175 
0176   void SetIsFake(bool fake) { m_fake = fake; }
0177 
0178   bool IsEncrypted() const { return m_encrypted; }
0179 
0180   void SetIsEncrypted(bool b) { m_encrypted = b; }
0181 
0182   bool IsDescendant(const Section *section);
0183 
0184   ConstString GetName() const { return m_name; }
0185 
0186   bool Slide(lldb::addr_t slide_amount, bool slide_children);
0187 
0188   lldb::SectionType GetType() const { return m_type; }
0189 
0190   const char *GetTypeAsCString() const;
0191 
0192   lldb::SectionSP GetParent() const { return m_parent_wp.lock(); }
0193 
0194   bool IsThreadSpecific() const { return m_thread_specific; }
0195 
0196   void SetIsThreadSpecific(bool b) { m_thread_specific = b; }
0197 
0198   /// Get the permissions as OR'ed bits from lldb::Permissions
0199   uint32_t GetPermissions() const;
0200 
0201   /// Set the permissions using bits OR'ed from lldb::Permissions
0202   void SetPermissions(uint32_t permissions);
0203 
0204   ObjectFile *GetObjectFile() { return m_obj_file; }
0205   const ObjectFile *GetObjectFile() const { return m_obj_file; }
0206 
0207   /// Read the section data from the object file that the section
0208   /// resides in.
0209   ///
0210   /// \param[in] dst
0211   ///     Where to place the data
0212   ///
0213   /// \param[in] dst_len
0214   ///     How many bytes of section data to read
0215   ///
0216   /// \param[in] offset
0217   ///     The offset in bytes within this section's data at which to
0218   ///     start copying data from.
0219   ///
0220   /// \return
0221   ///     The number of bytes read from the section, or zero if the
0222   ///     section has no data or \a offset is not a valid offset
0223   ///     in this section.
0224   lldb::offset_t GetSectionData(void *dst, lldb::offset_t dst_len,
0225                                 lldb::offset_t offset = 0);
0226 
0227   /// Get the shared reference to the section data from the object
0228   /// file that the section resides in. No copies of the data will be
0229   /// make unless the object file has been read from memory. If the
0230   /// object file is on disk, it will shared the mmap data for the
0231   /// entire object file.
0232   ///
0233   /// \param[in] data
0234   ///     Where to place the data, address byte size, and byte order
0235   ///
0236   /// \return
0237   ///     The number of bytes read from the section, or zero if the
0238   ///     section has no data or \a offset is not a valid offset
0239   ///     in this section.
0240   lldb::offset_t GetSectionData(DataExtractor &data);
0241 
0242   uint32_t GetLog2Align() { return m_log2align; }
0243 
0244   void SetLog2Align(uint32_t align) { m_log2align = align; }
0245 
0246   // Get the number of host bytes required to hold a target byte
0247   uint32_t GetTargetByteSize() const { return m_target_byte_size; }
0248 
0249   bool IsRelocated() const { return m_relocated; }
0250 
0251   void SetIsRelocated(bool b) { m_relocated = b; }
0252 
0253   /// Returns true if this section contains debug information. Symbol tables
0254   /// are not considered debug information since some symbols might contain
0255   /// debug information (STABS, COFF) but not all symbols do, so to keep this
0256   /// fast and simple only sections that contains only debug information should
0257   /// return true.
0258   bool ContainsOnlyDebugInfo() const;
0259 
0260 protected:
0261   ObjectFile *m_obj_file;   // The object file that data for this section should
0262                             // be read from
0263   lldb::SectionType m_type; // The type of this section
0264   lldb::SectionWP m_parent_wp; // Weak pointer to parent section
0265   ConstString m_name;          // Name of this section
0266   lldb::addr_t m_file_addr; // The absolute file virtual address range of this
0267                             // section if m_parent == NULL,
0268   // offset from parent file virtual address if m_parent != NULL
0269   lldb::addr_t m_byte_size; // Size in bytes that this section will occupy in
0270                             // memory at runtime
0271   lldb::offset_t m_file_offset; // Object file offset (if any)
0272   lldb::offset_t m_file_size;   // Object file size (can be smaller than
0273                                 // m_byte_size for zero filled sections...)
0274   uint32_t m_log2align;   // log_2(align) of the section (i.e. section has to be
0275                           // aligned to 2^m_log2align)
0276   SectionList m_children; // Child sections
0277   bool m_fake : 1, // If true, then this section only can contain the address if
0278                    // one of its
0279       // children contains an address. This allows for gaps between the
0280       // children that are contained in the address range for this section, but
0281       // do not produce hits unless the children contain the address.
0282       m_encrypted : 1,         // Set to true if the contents are encrypted
0283       m_thread_specific : 1,   // This section is thread specific
0284       m_readable : 1,          // If this section has read permissions
0285       m_writable : 1,          // If this section has write permissions
0286       m_executable : 1,        // If this section has executable permissions
0287       m_relocated : 1;         // If this section has had relocations applied
0288   uint32_t m_target_byte_size; // Some architectures have non-8-bit byte size.
0289                                // This is specified as
0290                                // as a multiple number of a host bytes
0291 private:
0292   Section(const Section &) = delete;
0293   const Section &operator=(const Section &) = delete;
0294 };
0295 
0296 } // namespace lldb_private
0297 
0298 namespace llvm {
0299 namespace json {
0300 
0301 bool fromJSON(const llvm::json::Value &value,
0302               lldb_private::JSONSection &section, llvm::json::Path path);
0303 
0304 bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type,
0305               llvm::json::Path path);
0306 
0307 } // namespace json
0308 } // namespace llvm
0309 
0310 #endif // LLDB_CORE_SECTION_H