ObjectFile.h revision 13d24fb1817faa7ccc4cfd799113ba1a2b8968eb
1//===-- ObjectFile.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_ObjectFile_h_
11#define liblldb_ObjectFile_h_
12
13#include "lldb/lldb-private.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Host/FileSpec.h"
16#include "lldb/Core/ModuleChild.h"
17#include "lldb/Core/PluginInterface.h"
18#include "lldb/Host/Endian.h"
19#include "lldb/Symbol/Symtab.h"
20#include "lldb/Symbol/UnwindTable.h"
21
22namespace lldb_private {
23
24//----------------------------------------------------------------------
25/// @class ObjectFile ObjectFile.h "lldb/Symbol/ObjectFile.h"
26/// @brief A plug-in interface definition class for object file parsers.
27///
28/// Object files belong to Module objects and know how to extract
29/// information from executable, shared library, and object (.o) files
30/// used by operating system runtime. The symbol table and section list
31/// for an object file.
32///
33/// Object files can be represented by the entire file, or by part of a
34/// file. Examples of object files that are part of a file include
35/// object files that contain information for multiple architectures in
36/// the same file, or archive files that contain multiple objects
37/// (ranlib archives) (possibly for multiple architectures as well).
38///
39/// Object archive files (e.g. ranlib archives) can contain
40/// multiple .o (object) files that must be selected by index or by name.
41/// The number of objects that an ObjectFile contains can be determined
42/// using the ObjectFile::GetNumObjects() const
43/// function, and followed by a call to
44/// ObjectFile::SelectObjectAtIndex (uint32_t) to change the currently
45/// selected object. Objects can also be selected by name using the
46/// ObjectFile::SelectObject(const char *) function.
47///
48/// Once an architecture is selected (and an object is selected for
49/// for archives), the object file information can be extracted from
50/// this abstract class.
51//----------------------------------------------------------------------
52class ObjectFile:
53    public std::tr1::enable_shared_from_this<ObjectFile>,
54    public PluginInterface,
55    public ModuleChild
56{
57friend class lldb_private::Module;
58
59public:
60    typedef enum
61    {
62        eTypeInvalid = 0,
63        eTypeCoreFile,      /// A core file that has a checkpoint of a program's execution state
64        eTypeExecutable,    /// A normal executable
65        eTypeDebugInfo,     /// An object file that contains only debug information
66        eTypeDynamicLinker, /// The platform's dynamic linker executable
67        eTypeObjectFile,    /// An intermediate object file
68        eTypeSharedLibrary, /// A shared library that can be used during execution
69        eTypeStubLibrary,   /// A library that can be linked against but not used for execution
70        eTypeUnknown
71    } Type;
72
73    typedef enum
74    {
75        eStrataInvalid = 0,
76        eStrataUnknown,
77        eStrataUser,
78        eStrataKernel
79    } Strata;
80
81    //------------------------------------------------------------------
82    /// Construct with a parent module, offset, and header data.
83    ///
84    /// Object files belong to modules and a valid module must be
85    /// supplied upon construction. The at an offset within a file for
86    /// objects that contain more than one architecture or object.
87    //------------------------------------------------------------------
88    ObjectFile (Module* module,
89                const FileSpec *file_spec_ptr,
90                lldb::addr_t offset,
91                lldb::addr_t length,
92                lldb::DataBufferSP& headerDataSP);
93
94    //------------------------------------------------------------------
95    /// Destructor.
96    ///
97    /// The destructor is virtual since this class is designed to be
98    /// inherited from by the plug-in instance.
99    //------------------------------------------------------------------
100    virtual
101    ~ObjectFile();
102
103    //------------------------------------------------------------------
104    /// Dump a description of this object to a Stream.
105    ///
106    /// Dump a description of the current contents of this object
107    /// to the supplied stream \a s. The dumping should include the
108    /// section list if it has been parsed, and the symbol table
109    /// if it has been parsed.
110    ///
111    /// @param[in] s
112    ///     The stream to which to dump the object descripton.
113    //------------------------------------------------------------------
114    virtual void
115    Dump (Stream *s) = 0;
116
117    //------------------------------------------------------------------
118    /// Find a ObjectFile plug-in that can parse \a file_spec.
119    ///
120    /// Scans all loaded plug-in interfaces that implement versions of
121    /// the ObjectFile plug-in interface and returns the first
122    /// instance that can parse the file.
123    ///
124    /// @param[in] module
125    ///     The parent module that owns this object file.
126    ///
127    /// @param[in] file_spec
128    ///     A file specification that indicates which file to use as the
129    ///     object file.
130    ///
131    /// @param[in] file_offset
132    ///     The offset into the file at which to start parsing the
133    ///     object. This is for files that contain multiple
134    ///     architectures or objects.
135    ///
136    /// @param[in] file_size
137    ///     The size of the current object file if it can be determined
138    ///     or if it is known. This can be zero.
139    ///
140    /// @see ObjectFile::ParseHeader()
141    //------------------------------------------------------------------
142    static lldb::ObjectFileSP
143    FindPlugin (Module* module,
144                const FileSpec* file_spec,
145                lldb::addr_t file_offset,
146                lldb::addr_t file_size,
147                lldb::DataBufferSP &data_sp);
148
149    //------------------------------------------------------------------
150    /// Gets the address size in bytes for the current object file.
151    ///
152    /// @return
153    ///     The size of an address in bytes for the currently selected
154    ///     architecture (and object for archives). Returns zero if no
155    ///     architecture or object has been selected.
156    //------------------------------------------------------------------
157    virtual size_t
158    GetAddressByteSize ()  const = 0;
159
160    //------------------------------------------------------------------
161    /// Get the address type given a file address in an object file.
162    ///
163    /// Many binary file formats know what kinds
164    /// This is primarily for ARM binaries, though it can be applied to
165    /// any executable file format that supports different opcode types
166    /// within the same binary. ARM binaries support having both ARM and
167    /// Thumb within the same executable container. We need to be able
168    /// to get
169    /// @return
170    ///     The size of an address in bytes for the currently selected
171    ///     architecture (and object for archives). Returns zero if no
172    ///     architecture or object has been selected.
173    //------------------------------------------------------------------
174    virtual AddressClass
175    GetAddressClass (lldb::addr_t file_addr);
176
177    //------------------------------------------------------------------
178    /// Extract the dependent modules from an object file.
179    ///
180    /// If an object file has information about which other images it
181    /// depends on (such as shared libraries), this function will
182    /// provide the list. Since many executables or shared libraries
183    /// may depend on the same files,
184    /// FileSpecList::AppendIfUnique(const FileSpec &) should be
185    /// used to make sure any files that are added are not already in
186    /// the list.
187    ///
188    /// @param[out] file_list
189    ///     A list of file specification objects that gets dependent
190    ///     files appended to.
191    ///
192    /// @return
193    ///     The number of new files that were appended to \a file_list.
194    ///
195    /// @see FileSpecList::AppendIfUnique(const FileSpec &)
196    //------------------------------------------------------------------
197    virtual uint32_t
198    GetDependentModules (FileSpecList& file_list) = 0;
199
200    //------------------------------------------------------------------
201    /// Tells whether this object file is capable of being the main executable
202    /// for a process.
203    ///
204    /// @return
205    ///     \b true if it is, \b false otherwise.
206    //------------------------------------------------------------------
207    virtual bool
208    IsExecutable () const = 0;
209
210    //------------------------------------------------------------------
211    /// Returns the offset into a file at which this object resides.
212    ///
213    /// Some files contain many object files, and this function allows
214    /// access to an object's offset within the file.
215    ///
216    /// @return
217    ///     The offset in bytes into the file. Defaults to zero for
218    ///     simple object files that a represented by an entire file.
219    //------------------------------------------------------------------
220    virtual lldb::addr_t
221    GetOffset () const
222    { return m_offset; }
223
224    virtual lldb::addr_t
225    GetByteSize () const
226    { return m_length; }
227
228    //------------------------------------------------------------------
229    /// Get accessor to the object file specification.
230    ///
231    /// @return
232    ///     The file specification object pointer if there is one, or
233    ///     NULL if this object is only from memory.
234    //------------------------------------------------------------------
235    virtual FileSpec&
236    GetFileSpec() { return m_file; }
237
238    //------------------------------------------------------------------
239    /// Get const accessor to the object file specification.
240    ///
241    /// @return
242    ///     The const file specification object pointer if there is one,
243    ///     or NULL if this object is only from memory.
244    //------------------------------------------------------------------
245    virtual const FileSpec&
246    GetFileSpec() const { return m_file; }
247
248    //------------------------------------------------------------------
249    /// Get the name of the cpu, vendor and OS for this object file.
250    ///
251    /// This value is a string that represents the target triple where
252    /// the cpu type, the vendor and the OS are encoded into a string.
253    ///
254    /// @param[out] target_triple
255    ///     The string value of the target triple.
256    ///
257    /// @return
258    ///     \b True if the target triple was able to be computed, \b
259    ///     false otherwise.
260    //------------------------------------------------------------------
261    virtual bool
262    GetArchitecture (ArchSpec &arch) = 0;
263
264    //------------------------------------------------------------------
265    /// Gets the section list for the currently selected architecture
266    /// (and object for archives).
267    ///
268    /// Section list parsing can be deferred by ObjectFile instances
269    /// until this accessor is called the first time.
270    ///
271    /// @return
272    ///     The list of sections contained in this object file.
273    //------------------------------------------------------------------
274    virtual SectionList *
275    GetSectionList () = 0;
276
277    //------------------------------------------------------------------
278    /// Gets the symbol table for the currently selected architecture
279    /// (and object for archives).
280    ///
281    /// Symbol table parsing can be deferred by ObjectFile instances
282    /// until this accessor is called the first time.
283    ///
284    /// @return
285    ///     The symbol table for this object file.
286    //------------------------------------------------------------------
287    virtual Symtab *
288    GetSymtab () = 0;
289
290    //------------------------------------------------------------------
291    /// Gets the UUID for this object file.
292    ///
293    /// If the object file format contains a UUID, the value should be
294    /// returned. Else ObjectFile instances should return the MD5
295    /// checksum of all of the bytes for the object file (or memory for
296    /// memory based object files).
297    ///
298    /// @return
299    ///     Returns \b true if a UUID was successfully extracted into
300    ///     \a uuid, \b false otherwise.
301    //------------------------------------------------------------------
302    virtual bool
303    GetUUID (lldb_private::UUID* uuid) = 0;
304
305    //------------------------------------------------------------------
306    /// Gets whether endian swapping should occur when extracting data
307    /// from this object file.
308    ///
309    /// @return
310    ///     Returns \b true if endian swapping is needed, \b false
311    ///     otherwise.
312    //------------------------------------------------------------------
313    virtual lldb::ByteOrder
314    GetByteOrder () const = 0;
315
316    //------------------------------------------------------------------
317    /// Attempts to parse the object header.
318    ///
319    /// This function is used as a test to see if a given plug-in
320    /// instance can parse the header data already contained in
321    /// ObjectFile::m_data. If an object file parser does not
322    /// recognize that magic bytes in a header, false should be returned
323    /// and the next plug-in can attempt to parse an object file.
324    ///
325    /// @return
326    ///     Returns \b true if the header was parsed succesfully, \b
327    ///     false otherwise.
328    //------------------------------------------------------------------
329    virtual bool
330    ParseHeader () = 0;
331
332    //------------------------------------------------------------------
333    /// Returns a reference to the UnwindTable for this ObjectFile
334    ///
335    /// The UnwindTable contains FuncUnwinders objects for any function in
336    /// this ObjectFile.  If a FuncUnwinders object hasn't been created yet
337    /// (i.e. the function has yet to be unwound in a stack walk), it
338    /// will be created when requested.  Specifically, we do not create
339    /// FuncUnwinders objects for functions until they are needed.
340    ///
341    /// @return
342    ///     Returns the unwind table for this object file.
343    //------------------------------------------------------------------
344    virtual lldb_private::UnwindTable&
345    GetUnwindTable () { return m_unwind_table; }
346
347    //------------------------------------------------------------------
348    /// Similar to Process::GetImageInfoAddress().
349    ///
350    /// Some platforms embed auxiliary structures useful to debuggers in the
351    /// address space of the inferior process.  This method returns the address
352    /// of such a structure if the information can be resolved via entries in
353    /// the object file.  ELF, for example, provides a means to hook into the
354    /// runtime linker so that a debugger may monitor the loading and unloading
355    /// of shared libraries.
356    ///
357    /// @return
358    ///     The address of any auxiliary tables, or an invalid address if this
359    ///     object file format does not support or contain such information.
360    virtual lldb_private::Address
361    GetImageInfoAddress () { return Address(); }
362
363    //------------------------------------------------------------------
364    /// Returns the address of the Entry Point in this object file - if
365    /// the object file doesn't have an entry point (because it is not an
366    /// executable file) then an invalid address is returned.
367    ///
368    /// @return
369    ///     Returns the entry address for this module.
370    //------------------------------------------------------------------
371    virtual lldb_private::Address
372    GetEntryPointAddress () { return Address();}
373
374    //------------------------------------------------------------------
375    /// The object file should be able to calculate its type by looking
376    /// at its file header and possibly the sections or other data in
377    /// the object file. The file type is used in the debugger to help
378    /// select the correct plug-ins for the job at hand, so this is
379    /// important to get right. If any eTypeXXX definitions do not match
380    /// up with the type of file you are loading, please feel free to
381    /// add a new enumeration value.
382    ///
383    /// @return
384    ///     The calculated file type for the current object file.
385    //------------------------------------------------------------------
386    virtual Type
387    CalculateType() = 0;
388
389    //------------------------------------------------------------------
390    /// The object file should be able to calculate the strata of the
391    /// object file.
392    ///
393    /// Many object files for platforms might be for either user space
394    /// debugging or for kernel debugging. If your object file subclass
395    /// can figure this out, it will help with debugger plug-in selection
396    /// when it comes time to debug.
397    ///
398    /// @return
399    ///     The calculated object file strata for the current object
400    ///     file.
401    //------------------------------------------------------------------
402    virtual Strata
403    CalculateStrata() = 0;
404
405    //------------------------------------------------------------------
406    // Member Functions
407    //------------------------------------------------------------------
408    Type
409    GetType ()
410    {
411        if (m_type == eTypeInvalid)
412            m_type = CalculateType();
413        return m_type;
414    }
415
416    Strata
417    GetStrata ()
418    {
419        if (m_strata == eStrataInvalid)
420            m_strata = CalculateStrata();
421        return m_strata;
422    }
423
424    size_t
425    GetData (off_t offset, size_t length, DataExtractor &data) const;
426
427    size_t
428    CopyData (off_t offset, size_t length, void *dst) const;
429
430protected:
431    //------------------------------------------------------------------
432    // Member variables.
433    //------------------------------------------------------------------
434    FileSpec m_file;
435    Type m_type;
436    Strata m_strata;
437    lldb::addr_t m_offset; ///< The offset in bytes into the file, or the address in memory
438    lldb::addr_t m_length; ///< The length of this object file if it is known (can be zero if length is unknown or can't be determined).
439    DataExtractor m_data; ///< The data for this object file so things can be parsed lazily.
440    lldb_private::UnwindTable m_unwind_table; /// < Table of FuncUnwinders objects created for this ObjectFile's functions
441
442    //------------------------------------------------------------------
443    /// Sets the architecture for a module.  At present the architecture
444    /// can only be set if it is invalid.  It is not allowed to switch from
445    /// one concrete architecture to another.
446    ///
447    /// @param[in] new_arch
448    ///     The architecture this module will be set to.
449    ///
450    /// @return
451    ///     Returns \b true if the architecture was changed, \b
452    ///     false otherwise.
453    //------------------------------------------------------------------
454    bool SetModulesArchitecture (const ArchSpec &new_arch);
455
456private:
457    DISALLOW_COPY_AND_ASSIGN (ObjectFile);
458};
459
460} // namespace lldb_private
461
462#endif  // liblldb_ObjectFile_h_
463
464