ObjectFile.h revision cbe61bd26db663fa3036866dc33315c6ffc37910
1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===-- ObjectFile.h --------------------------------------------*- C++ -*-===//
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//                     The LLVM Compiler Infrastructure
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is distributed under the University of Illinois Open Source
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// License. See LICENSE.TXT for details.
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//===----------------------------------------------------------------------===//
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef liblldb_ObjectFile_h_
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define liblldb_ObjectFile_h_
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "lldb/lldb-private.h"
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "lldb/Core/DataExtractor.h"
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "lldb/Host/FileSpec.h"
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#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_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        eStrataRawImage
80    } Strata;
81
82    //------------------------------------------------------------------
83    /// Construct with a parent module, offset, and header data.
84    ///
85    /// Object files belong to modules and a valid module must be
86    /// supplied upon construction. The at an offset within a file for
87    /// objects that contain more than one architecture or object.
88    //------------------------------------------------------------------
89    ObjectFile (const lldb::ModuleSP &module_sp,
90                const FileSpec *file_spec_ptr,
91                lldb::offset_t file_offset,
92                lldb::offset_t length,
93                lldb::DataBufferSP& data_sp,
94                lldb::offset_t data_offset);
95
96    ObjectFile (const lldb::ModuleSP &module_sp,
97                const lldb::ProcessSP &process_sp,
98                lldb::addr_t header_addr,
99                lldb::DataBufferSP& data_sp);
100
101    //------------------------------------------------------------------
102    /// Destructor.
103    ///
104    /// The destructor is virtual since this class is designed to be
105    /// inherited from by the plug-in instance.
106    //------------------------------------------------------------------
107    virtual
108    ~ObjectFile();
109
110    //------------------------------------------------------------------
111    /// Dump a description of this object to a Stream.
112    ///
113    /// Dump a description of the current contents of this object
114    /// to the supplied stream \a s. The dumping should include the
115    /// section list if it has been parsed, and the symbol table
116    /// if it has been parsed.
117    ///
118    /// @param[in] s
119    ///     The stream to which to dump the object descripton.
120    //------------------------------------------------------------------
121    virtual void
122    Dump (Stream *s) = 0;
123
124    //------------------------------------------------------------------
125    /// Find a ObjectFile plug-in that can parse \a file_spec.
126    ///
127    /// Scans all loaded plug-in interfaces that implement versions of
128    /// the ObjectFile plug-in interface and returns the first
129    /// instance that can parse the file.
130    ///
131    /// @param[in] module
132    ///     The parent module that owns this object file.
133    ///
134    /// @param[in] file_spec
135    ///     A file specification that indicates which file to use as the
136    ///     object file.
137    ///
138    /// @param[in] file_offset
139    ///     The offset into the file at which to start parsing the
140    ///     object. This is for files that contain multiple
141    ///     architectures or objects.
142    ///
143    /// @param[in] file_size
144    ///     The size of the current object file if it can be determined
145    ///     or if it is known. This can be zero.
146    ///
147    /// @see ObjectFile::ParseHeader()
148    //------------------------------------------------------------------
149    static lldb::ObjectFileSP
150    FindPlugin (const lldb::ModuleSP &module_sp,
151                const FileSpec* file_spec,
152                lldb::offset_t file_offset,
153                lldb::offset_t file_size,
154                lldb::DataBufferSP &data_sp,
155                lldb::offset_t &data_offset);
156
157    //------------------------------------------------------------------
158    /// Find a ObjectFile plug-in that can parse a file in memory.
159    ///
160    /// Scans all loaded plug-in interfaces that implement versions of
161    /// the ObjectFile plug-in interface and returns the first
162    /// instance that can parse the file.
163    ///
164    /// @param[in] module
165    ///     The parent module that owns this object file.
166    ///
167    /// @param[in] process_sp
168    ///     A shared pointer to the process whose memory space contains
169    ///     an object file. This will be stored as a std::weak_ptr.
170    ///
171    /// @param[in] header_addr
172    ///     The address of the header for the object file in memory.
173    //------------------------------------------------------------------
174    static lldb::ObjectFileSP
175    FindPlugin (const lldb::ModuleSP &module_sp,
176                const lldb::ProcessSP &process_sp,
177                lldb::addr_t header_addr,
178                lldb::DataBufferSP &file_data_sp);
179
180
181    //------------------------------------------------------------------
182    /// Split a path into a file path with object name.
183    ///
184    /// For paths like "/tmp/foo.a(bar.o)" we often need to split a path
185    /// up into the actual path name and into the object name so we can
186    /// make a valid object file from it.
187    ///
188    /// @param[in] path_with_object
189    ///     A path that might contain an archive path with a .o file
190    ///     specified in parens in the basename of the path.
191    ///
192    /// @param[out] archive_file
193    ///     If \b true is returned, \a file_spec will be filled in with
194    ///     the path to the archive.
195    ///
196    /// @param[out] archive_object
197    ///     If \b true is returned, \a object will be filled in with
198    ///     the name of the object inside the archive.
199    ///
200    /// @return
201    ///     \b true if the path matches the pattern of archive + object
202    ///     and \a archive_file and \a archive_object are modified,
203    ///     \b false otherwise and \a archive_file and \a archive_object
204    ///     are guaranteed to be remain unchanged.
205    //------------------------------------------------------------------
206    static bool
207    SplitArchivePathWithObject (const char *path_with_object,
208                                lldb_private::FileSpec &archive_file,
209                                lldb_private::ConstString &archive_object,
210                                bool must_exist);
211
212    //------------------------------------------------------------------
213    /// Gets the address size in bytes for the current object file.
214    ///
215    /// @return
216    ///     The size of an address in bytes for the currently selected
217    ///     architecture (and object for archives). Returns zero if no
218    ///     architecture or object has been selected.
219    //------------------------------------------------------------------
220    virtual uint32_t
221    GetAddressByteSize ()  const = 0;
222
223    //------------------------------------------------------------------
224    /// Get the address type given a file address in an object file.
225    ///
226    /// Many binary file formats know what kinds
227    /// This is primarily for ARM binaries, though it can be applied to
228    /// any executable file format that supports different opcode types
229    /// within the same binary. ARM binaries support having both ARM and
230    /// Thumb within the same executable container. We need to be able
231    /// to get
232    /// @return
233    ///     The size of an address in bytes for the currently selected
234    ///     architecture (and object for archives). Returns zero if no
235    ///     architecture or object has been selected.
236    //------------------------------------------------------------------
237    virtual lldb::AddressClass
238    GetAddressClass (lldb::addr_t file_addr);
239
240    //------------------------------------------------------------------
241    /// Extract the dependent modules from an object file.
242    ///
243    /// If an object file has information about which other images it
244    /// depends on (such as shared libraries), this function will
245    /// provide the list. Since many executables or shared libraries
246    /// may depend on the same files,
247    /// FileSpecList::AppendIfUnique(const FileSpec &) should be
248    /// used to make sure any files that are added are not already in
249    /// the list.
250    ///
251    /// @param[out] file_list
252    ///     A list of file specification objects that gets dependent
253    ///     files appended to.
254    ///
255    /// @return
256    ///     The number of new files that were appended to \a file_list.
257    ///
258    /// @see FileSpecList::AppendIfUnique(const FileSpec &)
259    //------------------------------------------------------------------
260    virtual uint32_t
261    GetDependentModules (FileSpecList& file_list) = 0;
262
263    //------------------------------------------------------------------
264    /// Tells whether this object file is capable of being the main executable
265    /// for a process.
266    ///
267    /// @return
268    ///     \b true if it is, \b false otherwise.
269    //------------------------------------------------------------------
270    virtual bool
271    IsExecutable () const = 0;
272
273    //------------------------------------------------------------------
274    /// Returns the offset into a file at which this object resides.
275    ///
276    /// Some files contain many object files, and this function allows
277    /// access to an object's offset within the file.
278    ///
279    /// @return
280    ///     The offset in bytes into the file. Defaults to zero for
281    ///     simple object files that a represented by an entire file.
282    //------------------------------------------------------------------
283    virtual lldb::addr_t
284    GetFileOffset () const
285    { return m_file_offset; }
286
287    virtual lldb::addr_t
288    GetByteSize () const
289    { return m_length; }
290
291    //------------------------------------------------------------------
292    /// Get accessor to the object file specification.
293    ///
294    /// @return
295    ///     The file specification object pointer if there is one, or
296    ///     NULL if this object is only from memory.
297    //------------------------------------------------------------------
298    virtual FileSpec&
299    GetFileSpec() { return m_file; }
300
301    //------------------------------------------------------------------
302    /// Get const accessor to the object file specification.
303    ///
304    /// @return
305    ///     The const file specification object pointer if there is one,
306    ///     or NULL if this object is only from memory.
307    //------------------------------------------------------------------
308    virtual const FileSpec&
309    GetFileSpec() const { return m_file; }
310
311    //------------------------------------------------------------------
312    /// Get the name of the cpu, vendor and OS for this object file.
313    ///
314    /// This value is a string that represents the target triple where
315    /// the cpu type, the vendor and the OS are encoded into a string.
316    ///
317    /// @param[out] target_triple
318    ///     The string value of the target triple.
319    ///
320    /// @return
321    ///     \b True if the target triple was able to be computed, \b
322    ///     false otherwise.
323    //------------------------------------------------------------------
324    virtual bool
325    GetArchitecture (ArchSpec &arch) = 0;
326
327    //------------------------------------------------------------------
328    /// Gets the section list for the currently selected architecture
329    /// (and object for archives).
330    ///
331    /// Section list parsing can be deferred by ObjectFile instances
332    /// until this accessor is called the first time.
333    ///
334    /// @return
335    ///     The list of sections contained in this object file.
336    //------------------------------------------------------------------
337    virtual SectionList *
338    GetSectionList () = 0;
339
340    //------------------------------------------------------------------
341    /// Gets the symbol table for the currently selected architecture
342    /// (and object for archives).
343    ///
344    /// Symbol table parsing can be deferred by ObjectFile instances
345    /// until this accessor is called the first time.
346    ///
347    /// @return
348    ///     The symbol table for this object file.
349    //------------------------------------------------------------------
350    virtual Symtab *
351    GetSymtab () = 0;
352
353    //------------------------------------------------------------------
354    /// Gets the UUID for this object file.
355    ///
356    /// If the object file format contains a UUID, the value should be
357    /// returned. Else ObjectFile instances should return the MD5
358    /// checksum of all of the bytes for the object file (or memory for
359    /// memory based object files).
360    ///
361    /// @return
362    ///     Returns \b true if a UUID was successfully extracted into
363    ///     \a uuid, \b false otherwise.
364    //------------------------------------------------------------------
365    virtual bool
366    GetUUID (lldb_private::UUID* uuid) = 0;
367
368    //------------------------------------------------------------------
369    /// Gets whether endian swapping should occur when extracting data
370    /// from this object file.
371    ///
372    /// @return
373    ///     Returns \b true if endian swapping is needed, \b false
374    ///     otherwise.
375    //------------------------------------------------------------------
376    virtual lldb::ByteOrder
377    GetByteOrder () const = 0;
378
379    //------------------------------------------------------------------
380    /// Attempts to parse the object header.
381    ///
382    /// This function is used as a test to see if a given plug-in
383    /// instance can parse the header data already contained in
384    /// ObjectFile::m_data. If an object file parser does not
385    /// recognize that magic bytes in a header, false should be returned
386    /// and the next plug-in can attempt to parse an object file.
387    ///
388    /// @return
389    ///     Returns \b true if the header was parsed succesfully, \b
390    ///     false otherwise.
391    //------------------------------------------------------------------
392    virtual bool
393    ParseHeader () = 0;
394
395    //------------------------------------------------------------------
396    /// Returns a reference to the UnwindTable for this ObjectFile
397    ///
398    /// The UnwindTable contains FuncUnwinders objects for any function in
399    /// this ObjectFile.  If a FuncUnwinders object hasn't been created yet
400    /// (i.e. the function has yet to be unwound in a stack walk), it
401    /// will be created when requested.  Specifically, we do not create
402    /// FuncUnwinders objects for functions until they are needed.
403    ///
404    /// @return
405    ///     Returns the unwind table for this object file.
406    //------------------------------------------------------------------
407    virtual lldb_private::UnwindTable&
408    GetUnwindTable () { return m_unwind_table; }
409
410    //------------------------------------------------------------------
411    /// Similar to Process::GetImageInfoAddress().
412    ///
413    /// Some platforms embed auxiliary structures useful to debuggers in the
414    /// address space of the inferior process.  This method returns the address
415    /// of such a structure if the information can be resolved via entries in
416    /// the object file.  ELF, for example, provides a means to hook into the
417    /// runtime linker so that a debugger may monitor the loading and unloading
418    /// of shared libraries.
419    ///
420    /// @return
421    ///     The address of any auxiliary tables, or an invalid address if this
422    ///     object file format does not support or contain such information.
423    virtual lldb_private::Address
424    GetImageInfoAddress () { return Address(); }
425
426    //------------------------------------------------------------------
427    /// Returns the address of the Entry Point in this object file - if
428    /// the object file doesn't have an entry point (because it is not an
429    /// executable file) then an invalid address is returned.
430    ///
431    /// @return
432    ///     Returns the entry address for this module.
433    //------------------------------------------------------------------
434    virtual lldb_private::Address
435    GetEntryPointAddress () { return Address();}
436
437    //------------------------------------------------------------------
438    /// Returns the address that represents the header of this object
439    /// file.
440    ///
441    /// The header address is defined as where the header for the object
442    /// file is that describes the content of the file. If the header
443    /// doesn't appear in a section that is defined in the object file,
444    /// an address with no section is returned that has the file offset
445    /// set in the m_file_offset member of the lldb_private::Address object.
446    ///
447    /// @return
448    ///     Returns the entry address for this module.
449    //------------------------------------------------------------------
450    virtual lldb_private::Address
451    GetHeaderAddress () { return Address(m_memory_addr);}
452
453
454    virtual uint32_t
455    GetNumThreadContexts ()
456    {
457        return 0;
458    }
459
460    virtual lldb::RegisterContextSP
461    GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
462    {
463        return lldb::RegisterContextSP();
464    }
465    //------------------------------------------------------------------
466    /// The object file should be able to calculate its type by looking
467    /// at its file header and possibly the sections or other data in
468    /// the object file. The file type is used in the debugger to help
469    /// select the correct plug-ins for the job at hand, so this is
470    /// important to get right. If any eTypeXXX definitions do not match
471    /// up with the type of file you are loading, please feel free to
472    /// add a new enumeration value.
473    ///
474    /// @return
475    ///     The calculated file type for the current object file.
476    //------------------------------------------------------------------
477    virtual Type
478    CalculateType() = 0;
479
480    //------------------------------------------------------------------
481    /// The object file should be able to calculate the strata of the
482    /// object file.
483    ///
484    /// Many object files for platforms might be for either user space
485    /// debugging or for kernel debugging. If your object file subclass
486    /// can figure this out, it will help with debugger plug-in selection
487    /// when it comes time to debug.
488    ///
489    /// @return
490    ///     The calculated object file strata for the current object
491    ///     file.
492    //------------------------------------------------------------------
493    virtual Strata
494    CalculateStrata() = 0;
495
496    //------------------------------------------------------------------
497    /// Get the object file version numbers.
498    ///
499    /// Many object files have a set of version numbers that describe
500    /// the version of the executable or shared library. Typically there
501    /// are major, minor and build, but there may be more. This function
502    /// will extract the versions from object files if they are available.
503    ///
504    /// If \a versions is NULL, or if \a num_versions is 0, the return
505    /// value will indicate how many version numbers are available in
506    /// this object file. Then a subsequent call can be made to this
507    /// function with a value of \a versions and \a num_versions that
508    /// has enough storage to store some or all version numbers.
509    ///
510    /// @param[out] versions
511    ///     A pointer to an array of uint32_t types that is \a num_versions
512    ///     long. If this value is NULL, the return value will indicate
513    ///     how many version numbers are required for a subsequent call
514    ///     to this function so that all versions can be retrieved. If
515    ///     the value is non-NULL, then at most \a num_versions of the
516    ///     existing versions numbers will be filled into \a versions.
517    ///     If there is no version information available, \a versions
518    ///     will be filled with \a num_versions UINT32_MAX values
519    ///     and zero will be returned.
520    ///
521    /// @param[in] num_versions
522    ///     The maximum number of entries to fill into \a versions. If
523    ///     this value is zero, then the return value will indicate
524    ///     how many version numbers there are in total so another call
525    ///     to this function can be make with adequate storage in
526    ///     \a versions to get all of the version numbers. If \a
527    ///     num_versions is less than the actual number of version
528    ///     numbers in this object file, only \a num_versions will be
529    ///     filled into \a versions (if \a versions is non-NULL).
530    ///
531    /// @return
532    ///     This function always returns the number of version numbers
533    ///     that this object file has regardless of the number of
534    ///     version numbers that were copied into \a versions.
535    //------------------------------------------------------------------
536    virtual uint32_t
537    GetVersion (uint32_t *versions, uint32_t num_versions)
538    {
539        if (versions && num_versions)
540        {
541            for (uint32_t i=0; i<num_versions; ++i)
542                versions[i] = UINT32_MAX;
543        }
544        return 0;
545    }
546
547    //------------------------------------------------------------------
548    // Member Functions
549    //------------------------------------------------------------------
550    Type
551    GetType ()
552    {
553        if (m_type == eTypeInvalid)
554            m_type = CalculateType();
555        return m_type;
556    }
557
558    Strata
559    GetStrata ()
560    {
561        if (m_strata == eStrataInvalid)
562            m_strata = CalculateStrata();
563        return m_strata;
564    }
565
566    // When an object file is in memory, subclasses should try and lock
567    // the process weak pointer. If the process weak pointer produces a
568    // valid ProcessSP, then subclasses can call this function to read
569    // memory.
570    static lldb::DataBufferSP
571    ReadMemory (const lldb::ProcessSP &process_sp,
572                lldb::addr_t addr,
573                size_t byte_size);
574
575    size_t
576    GetData (off_t offset, size_t length, DataExtractor &data) const;
577
578    size_t
579    CopyData (off_t offset, size_t length, void *dst) const;
580
581    size_t
582    ReadSectionData (const Section *section,
583                     off_t section_offset,
584                     void *dst,
585                     size_t dst_len) const;
586    size_t
587    ReadSectionData (const Section *section,
588                     DataExtractor& section_data) const;
589
590    size_t
591    MemoryMapSectionData (const Section *section,
592                          DataExtractor& section_data) const;
593
594    bool
595    IsInMemory () const
596    {
597        return m_memory_addr != LLDB_INVALID_ADDRESS;
598    }
599
600protected:
601    //------------------------------------------------------------------
602    // Member variables.
603    //------------------------------------------------------------------
604    FileSpec m_file;
605    Type m_type;
606    Strata m_strata;
607    lldb::addr_t m_file_offset; ///< The offset in bytes into the file, or the address in memory
608    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).
609    DataExtractor m_data; ///< The data for this object file so things can be parsed lazily.
610    lldb_private::UnwindTable m_unwind_table; /// < Table of FuncUnwinders objects created for this ObjectFile's functions
611    lldb::ProcessWP m_process_wp;
612    const lldb::addr_t m_memory_addr;
613
614    //------------------------------------------------------------------
615    /// Sets the architecture for a module.  At present the architecture
616    /// can only be set if it is invalid.  It is not allowed to switch from
617    /// one concrete architecture to another.
618    ///
619    /// @param[in] new_arch
620    ///     The architecture this module will be set to.
621    ///
622    /// @return
623    ///     Returns \b true if the architecture was changed, \b
624    ///     false otherwise.
625    //------------------------------------------------------------------
626    bool SetModulesArchitecture (const ArchSpec &new_arch);
627
628private:
629    DISALLOW_COPY_AND_ASSIGN (ObjectFile);
630};
631
632} // namespace lldb_private
633
634#endif  // liblldb_ObjectFile_h_
635
636