1254721Semaste//===-- ObjectContainer.h ---------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_ObjectContainer_h_
11254721Semaste#define liblldb_ObjectContainer_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste// C++ Includes
15254721Semaste// Other libraries and framework includes
16254721Semaste// Project includes
17254721Semaste
18254721Semaste#include "lldb/lldb-private.h"
19254721Semaste#include "lldb/Core/DataExtractor.h"
20254721Semaste#include "lldb/Host/FileSpec.h"
21254721Semaste#include "lldb/Core/ModuleChild.h"
22254721Semaste#include "lldb/Core/PluginInterface.h"
23254721Semaste#include "lldb/Host/Endian.h"
24254721Semaste
25254721Semastenamespace lldb_private {
26254721Semaste
27254721Semaste//----------------------------------------------------------------------
28254721Semaste/// @class ObjectContainer ObjectContainer.h "lldb/Symbol/ObjectContainer.h"
29254721Semaste/// @brief A plug-in interface definition class for object containers.
30254721Semaste///
31254721Semaste/// Object containers contain object files from one or more
32254721Semaste/// architectures, and also can contain one or more named objects.
33254721Semaste///
34254721Semaste/// Typical object containers are static libraries (.a files) that
35254721Semaste/// contain multiple named object files, and universal files that contain
36254721Semaste/// multiple architectures.
37254721Semaste//----------------------------------------------------------------------
38254721Semasteclass ObjectContainer :
39254721Semaste    public PluginInterface,
40254721Semaste    public ModuleChild
41254721Semaste{
42254721Semastepublic:
43254721Semaste    //------------------------------------------------------------------
44254721Semaste    /// Construct with a parent module, offset, and header data.
45254721Semaste    ///
46254721Semaste    /// Object files belong to modules and a valid module must be
47254721Semaste    /// supplied upon construction. The at an offset within a file for
48254721Semaste    /// objects that contain more than one architecture or object.
49254721Semaste    //------------------------------------------------------------------
50254721Semaste    ObjectContainer (const lldb::ModuleSP &module_sp,
51254721Semaste                     const FileSpec *file,
52254721Semaste                     lldb::offset_t file_offset,
53254721Semaste                     lldb::offset_t length,
54254721Semaste                     lldb::DataBufferSP& data_sp,
55254721Semaste                     lldb::offset_t data_offset) :
56254721Semaste        ModuleChild (module_sp),
57254721Semaste        m_file (),  // This file can be different than the module's file spec
58254721Semaste        m_offset (file_offset),
59254721Semaste        m_length (length),
60254721Semaste        m_data ()
61254721Semaste    {
62254721Semaste        if (file)
63254721Semaste            m_file = *file;
64254721Semaste        if (data_sp)
65254721Semaste            m_data.SetData (data_sp, data_offset, length);
66254721Semaste    }
67254721Semaste
68254721Semaste    //------------------------------------------------------------------
69254721Semaste    /// Destructor.
70254721Semaste    ///
71254721Semaste    /// The destructor is virtual since this class is designed to be
72254721Semaste    /// inherited from by the plug-in instance.
73254721Semaste    //------------------------------------------------------------------
74254721Semaste    virtual
75254721Semaste    ~ObjectContainer()
76254721Semaste    {
77254721Semaste    }
78254721Semaste
79254721Semaste    //------------------------------------------------------------------
80254721Semaste    /// Dump a description of this object to a Stream.
81254721Semaste    ///
82254721Semaste    /// Dump a description of the current contents of this object
83254721Semaste    /// to the supplied stream \a s. The dumping should include the
84254721Semaste    /// section list if it has been parsed, and the symbol table
85254721Semaste    /// if it has been parsed.
86254721Semaste    ///
87254721Semaste    /// @param[in] s
88254721Semaste    ///     The stream to which to dump the object descripton.
89254721Semaste    //------------------------------------------------------------------
90254721Semaste    virtual void
91254721Semaste    Dump (Stream *s) const = 0;
92254721Semaste
93254721Semaste    //------------------------------------------------------------------
94254721Semaste    /// Gets the architecture given an index.
95254721Semaste    ///
96254721Semaste    /// Copies the architecture specification for index \a idx.
97254721Semaste    ///
98254721Semaste    /// @param[in] idx
99254721Semaste    ///     The architecture index to extract.
100254721Semaste    ///
101254721Semaste    /// @param[out] arch
102254721Semaste    ///     A architecture object that will be filled in if \a idx is a
103254721Semaste    ///     architecture valid index.
104254721Semaste    ///
105254721Semaste    /// @return
106254721Semaste    ///     Returns \b true if \a idx is valid and \a arch has been
107254721Semaste    ///     filled in, \b false otherwise.
108254721Semaste    ///
109254721Semaste    /// @see ObjectContainer::GetNumArchitectures() const
110254721Semaste    //------------------------------------------------------------------
111254721Semaste    virtual bool
112254721Semaste    GetArchitectureAtIndex (uint32_t idx, ArchSpec& arch) const
113254721Semaste    {
114254721Semaste        return false;
115254721Semaste    }
116254721Semaste
117254721Semaste    //------------------------------------------------------------------
118254721Semaste    /// Returns the offset into a file at which this object resides.
119254721Semaste    ///
120254721Semaste    /// Some files contain many object files, and this function allows
121254721Semaste    /// access to an object's offset within the file.
122254721Semaste    ///
123254721Semaste    /// @return
124254721Semaste    ///     The offset in bytes into the file. Defaults to zero for
125254721Semaste    ///     simple object files that a represented by an entire file.
126254721Semaste    //------------------------------------------------------------------
127254721Semaste    virtual lldb::addr_t
128254721Semaste    GetOffset () const
129254721Semaste    { return m_offset; }
130254721Semaste
131254721Semaste    virtual lldb::addr_t
132254721Semaste    GetByteSize () const
133254721Semaste    { return m_length; }
134254721Semaste
135254721Semaste    //------------------------------------------------------------------
136254721Semaste    /// Get the number of objects within this object file (archives).
137254721Semaste    ///
138254721Semaste    /// @return
139254721Semaste    ///     Zero for object files that are not archives, or the number
140254721Semaste    ///     of objects contained in the archive.
141254721Semaste    //------------------------------------------------------------------
142254721Semaste    virtual size_t
143254721Semaste    GetNumObjects () const
144254721Semaste    { return 0; }
145254721Semaste
146254721Semaste    //------------------------------------------------------------------
147254721Semaste    /// Get the number of architectures in this object file.
148254721Semaste    ///
149254721Semaste    /// The default implementation returns 1 as for object files that
150254721Semaste    /// contain a single architecture. ObjectContainer instances that
151254721Semaste    /// contain more than one architecture should override this function
152254721Semaste    /// and return an appropriate value.
153254721Semaste    ///
154254721Semaste    /// @return
155254721Semaste    ///     The number of architectures contained in this object file.
156254721Semaste    //------------------------------------------------------------------
157254721Semaste    virtual size_t
158254721Semaste    GetNumArchitectures () const
159254721Semaste    { return 0; }
160254721Semaste
161254721Semaste    //------------------------------------------------------------------
162254721Semaste    /// Attempts to parse the object header.
163254721Semaste    ///
164254721Semaste    /// This function is used as a test to see if a given plug-in
165254721Semaste    /// instance can parse the header data already contained in
166254721Semaste    /// ObjectContainer::m_data. If an object file parser does not
167254721Semaste    /// recognize that magic bytes in a header, false should be returned
168254721Semaste    /// and the next plug-in can attempt to parse an object file.
169254721Semaste    ///
170254721Semaste    /// @return
171254721Semaste    ///     Returns \b true if the header was parsed succesfully, \b
172254721Semaste    ///     false otherwise.
173254721Semaste    //------------------------------------------------------------------
174254721Semaste    virtual bool
175254721Semaste    ParseHeader () = 0;
176254721Semaste
177254721Semaste    //------------------------------------------------------------------
178254721Semaste    /// Selects an architecture in an object file.
179254721Semaste    ///
180254721Semaste    /// Object files that contain a single architecture should verify
181254721Semaste    /// that the specified \a arch matches the architecture in in
182254721Semaste    /// object file and return \b true or \b false accordingly.
183254721Semaste    ///
184254721Semaste    /// Object files that contain more than one architecture should
185254721Semaste    /// attempt to select that architecture, and if successful, clear
186254721Semaste    /// out any previous state from any previously selected architecture
187254721Semaste    /// and prepare to return information for the new architecture.
188254721Semaste    ///
189254721Semaste    /// @return
190254721Semaste    ///     Returns a pointer to the object file of the requested \a
191254721Semaste    ///     arch and optional \a name. Returns NULL of no such object
192254721Semaste    ///     file exists in the container.
193254721Semaste    //------------------------------------------------------------------
194254721Semaste    virtual lldb::ObjectFileSP
195254721Semaste    GetObjectFile (const FileSpec *file) = 0;
196254721Semaste
197254721Semaste    virtual bool
198254721Semaste    ObjectAtIndexIsContainer (uint32_t object_idx)
199254721Semaste    {
200254721Semaste        return false;
201254721Semaste    }
202254721Semaste
203254721Semaste    virtual ObjectFile *
204254721Semaste    GetObjectFileAtIndex (uint32_t object_idx)
205254721Semaste    {
206254721Semaste        return NULL;
207254721Semaste    }
208254721Semaste
209254721Semaste    virtual ObjectContainer *
210254721Semaste    GetObjectContainerAtIndex (uint32_t object_idx)
211254721Semaste    {
212254721Semaste        return NULL;
213254721Semaste    }
214254721Semaste
215254721Semaste    virtual const char *
216254721Semaste    GetObjectNameAtIndex (uint32_t object_idx) const
217254721Semaste    {
218254721Semaste        return NULL;
219254721Semaste    }
220254721Semaste
221254721Semasteprotected:
222254721Semaste    //------------------------------------------------------------------
223254721Semaste    // Member variables.
224254721Semaste    //------------------------------------------------------------------
225254721Semaste    FileSpec m_file; ///< The file that represents this container objects (which can be different from the module's file).
226254721Semaste    lldb::addr_t m_offset; ///< The offset in bytes into the file, or the address in memory
227254721Semaste    lldb::addr_t m_length; ///< The size in bytes if known (can be zero).
228254721Semaste    DataExtractor m_data; ///< The data for this object file so things can be parsed lazily.
229254721Semaste
230254721Semasteprivate:
231254721Semaste    DISALLOW_COPY_AND_ASSIGN (ObjectContainer);
232254721Semaste};
233254721Semaste
234254721Semaste} // namespace lldb_private
235254721Semaste
236254721Semaste#endif  // liblldb_ObjectContainer_h_
237