ObjectContainer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===-- ObjectContainer.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. #ifndef LLDB_SYMBOL_OBJECTCONTAINER_H
  9. #define LLDB_SYMBOL_OBJECTCONTAINER_H
  10. #include "lldb/Core/ModuleChild.h"
  11. #include "lldb/Core/PluginInterface.h"
  12. #include "lldb/Utility/DataExtractor.h"
  13. #include "lldb/Utility/Endian.h"
  14. #include "lldb/Utility/FileSpec.h"
  15. #include "lldb/lldb-private.h"
  16. namespace lldb_private {
  17. /// \class ObjectContainer ObjectContainer.h "lldb/Symbol/ObjectContainer.h"
  18. /// A plug-in interface definition class for object containers.
  19. ///
  20. /// Object containers contain object files from one or more architectures, and
  21. /// also can contain one or more named objects.
  22. ///
  23. /// Typical object containers are static libraries (.a files) that contain
  24. /// multiple named object files, and universal files that contain multiple
  25. /// architectures.
  26. class ObjectContainer : public PluginInterface, public ModuleChild {
  27. public:
  28. /// Construct with a parent module, offset, and header data.
  29. ///
  30. /// Object files belong to modules and a valid module must be supplied upon
  31. /// construction. The at an offset within a file for objects that contain
  32. /// more than one architecture or object.
  33. ObjectContainer(const lldb::ModuleSP &module_sp, const FileSpec *file,
  34. lldb::offset_t file_offset, lldb::offset_t length,
  35. lldb::DataBufferSP &data_sp, lldb::offset_t data_offset)
  36. : ModuleChild(module_sp),
  37. m_file(), // This file can be different than the module's file spec
  38. m_offset(file_offset), m_length(length), m_data() {
  39. if (file)
  40. m_file = *file;
  41. if (data_sp)
  42. m_data.SetData(data_sp, data_offset, length);
  43. }
  44. /// Destructor.
  45. ///
  46. /// The destructor is virtual since this class is designed to be inherited
  47. /// from by the plug-in instance.
  48. ~ObjectContainer() override = default;
  49. /// Dump a description of this object to a Stream.
  50. ///
  51. /// Dump a description of the current contents of this object to the
  52. /// supplied stream \a s. The dumping should include the section list if it
  53. /// has been parsed, and the symbol table if it has been parsed.
  54. ///
  55. /// \param[in] s
  56. /// The stream to which to dump the object description.
  57. virtual void Dump(Stream *s) const = 0;
  58. /// Gets the architecture given an index.
  59. ///
  60. /// Copies the architecture specification for index \a idx.
  61. ///
  62. /// \param[in] idx
  63. /// The architecture index to extract.
  64. ///
  65. /// \param[out] arch
  66. /// A architecture object that will be filled in if \a idx is a
  67. /// architecture valid index.
  68. ///
  69. /// \return
  70. /// Returns \b true if \a idx is valid and \a arch has been
  71. /// filled in, \b false otherwise.
  72. ///
  73. /// \see ObjectContainer::GetNumArchitectures() const
  74. virtual bool GetArchitectureAtIndex(uint32_t idx, ArchSpec &arch) const {
  75. return false;
  76. }
  77. /// Returns the offset into a file at which this object resides.
  78. ///
  79. /// Some files contain many object files, and this function allows access to
  80. /// an object's offset within the file.
  81. ///
  82. /// \return
  83. /// The offset in bytes into the file. Defaults to zero for
  84. /// simple object files that a represented by an entire file.
  85. virtual lldb::addr_t GetOffset() const { return m_offset; }
  86. virtual lldb::addr_t GetByteSize() const { return m_length; }
  87. /// Get the number of objects within this object file (archives).
  88. ///
  89. /// \return
  90. /// Zero for object files that are not archives, or the number
  91. /// of objects contained in the archive.
  92. virtual size_t GetNumObjects() const { return 0; }
  93. /// Get the number of architectures in this object file.
  94. ///
  95. /// The default implementation returns 1 as for object files that contain a
  96. /// single architecture. ObjectContainer instances that contain more than
  97. /// one architecture should override this function and return an appropriate
  98. /// value.
  99. ///
  100. /// \return
  101. /// The number of architectures contained in this object file.
  102. virtual size_t GetNumArchitectures() const { return 0; }
  103. /// Attempts to parse the object header.
  104. ///
  105. /// This function is used as a test to see if a given plug-in instance can
  106. /// parse the header data already contained in ObjectContainer::m_data. If
  107. /// an object file parser does not recognize that magic bytes in a header,
  108. /// false should be returned and the next plug-in can attempt to parse an
  109. /// object file.
  110. ///
  111. /// \return
  112. /// Returns \b true if the header was parsed successfully, \b
  113. /// false otherwise.
  114. virtual bool ParseHeader() = 0;
  115. /// Selects an architecture in an object file.
  116. ///
  117. /// Object files that contain a single architecture should verify that the
  118. /// specified \a arch matches the architecture in in object file and return
  119. /// \b true or \b false accordingly.
  120. ///
  121. /// Object files that contain more than one architecture should attempt to
  122. /// select that architecture, and if successful, clear out any previous
  123. /// state from any previously selected architecture and prepare to return
  124. /// information for the new architecture.
  125. ///
  126. /// \return
  127. /// Returns a pointer to the object file of the requested \a
  128. /// arch and optional \a name. Returns nullptr of no such object
  129. /// file exists in the container.
  130. virtual lldb::ObjectFileSP GetObjectFile(const FileSpec *file) = 0;
  131. virtual bool ObjectAtIndexIsContainer(uint32_t object_idx) { return false; }
  132. virtual ObjectFile *GetObjectFileAtIndex(uint32_t object_idx) {
  133. return nullptr;
  134. }
  135. virtual ObjectContainer *GetObjectContainerAtIndex(uint32_t object_idx) {
  136. return nullptr;
  137. }
  138. virtual const char *GetObjectNameAtIndex(uint32_t object_idx) const {
  139. return nullptr;
  140. }
  141. protected:
  142. // Member variables.
  143. FileSpec m_file; ///< The file that represents this container objects (which
  144. ///can be different from the module's file).
  145. lldb::addr_t
  146. m_offset; ///< The offset in bytes into the file, or the address in memory
  147. lldb::addr_t m_length; ///< The size in bytes if known (can be zero).
  148. DataExtractor
  149. m_data; ///< The data for this object file so things can be parsed lazily.
  150. private:
  151. ObjectContainer(const ObjectContainer &) = delete;
  152. const ObjectContainer &operator=(const ObjectContainer &) = delete;
  153. };
  154. } // namespace lldb_private
  155. #endif // LLDB_SYMBOL_OBJECTCONTAINER_H