ObjectFile.h revision 81a96aa6242f7b559770f5dc62316253cb8cb0d4
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_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    /// Frees the symbol table.
355    ///
356    /// This function should only be used when an object file is
357    ///
358    /// @return
359    ///     The symbol table for this object file.
360    //------------------------------------------------------------------
361    virtual void
362    ClearSymtab ();
363
364    //------------------------------------------------------------------
365    /// Gets the UUID for this object file.
366    ///
367    /// If the object file format contains a UUID, the value should be
368    /// returned. Else ObjectFile instances should return the MD5
369    /// checksum of all of the bytes for the object file (or memory for
370    /// memory based object files).
371    ///
372    /// @return
373    ///     Returns \b true if a UUID was successfully extracted into
374    ///     \a uuid, \b false otherwise.
375    //------------------------------------------------------------------
376    virtual bool
377    GetUUID (lldb_private::UUID* uuid) = 0;
378
379    //------------------------------------------------------------------
380    /// Gets whether endian swapping should occur when extracting data
381    /// from this object file.
382    ///
383    /// @return
384    ///     Returns \b true if endian swapping is needed, \b false
385    ///     otherwise.
386    //------------------------------------------------------------------
387    virtual lldb::ByteOrder
388    GetByteOrder () const = 0;
389
390    //------------------------------------------------------------------
391    /// Attempts to parse the object header.
392    ///
393    /// This function is used as a test to see if a given plug-in
394    /// instance can parse the header data already contained in
395    /// ObjectFile::m_data. If an object file parser does not
396    /// recognize that magic bytes in a header, false should be returned
397    /// and the next plug-in can attempt to parse an object file.
398    ///
399    /// @return
400    ///     Returns \b true if the header was parsed succesfully, \b
401    ///     false otherwise.
402    //------------------------------------------------------------------
403    virtual bool
404    ParseHeader () = 0;
405
406    //------------------------------------------------------------------
407    /// Returns a reference to the UnwindTable for this ObjectFile
408    ///
409    /// The UnwindTable contains FuncUnwinders objects for any function in
410    /// this ObjectFile.  If a FuncUnwinders object hasn't been created yet
411    /// (i.e. the function has yet to be unwound in a stack walk), it
412    /// will be created when requested.  Specifically, we do not create
413    /// FuncUnwinders objects for functions until they are needed.
414    ///
415    /// @return
416    ///     Returns the unwind table for this object file.
417    //------------------------------------------------------------------
418    virtual lldb_private::UnwindTable&
419    GetUnwindTable () { return m_unwind_table; }
420
421    //------------------------------------------------------------------
422    /// Similar to Process::GetImageInfoAddress().
423    ///
424    /// Some platforms embed auxiliary structures useful to debuggers in the
425    /// address space of the inferior process.  This method returns the address
426    /// of such a structure if the information can be resolved via entries in
427    /// the object file.  ELF, for example, provides a means to hook into the
428    /// runtime linker so that a debugger may monitor the loading and unloading
429    /// of shared libraries.
430    ///
431    /// @return
432    ///     The address of any auxiliary tables, or an invalid address if this
433    ///     object file format does not support or contain such information.
434    virtual lldb_private::Address
435    GetImageInfoAddress () { return Address(); }
436
437    //------------------------------------------------------------------
438    /// Returns the address of the Entry Point in this object file - if
439    /// the object file doesn't have an entry point (because it is not an
440    /// executable file) then an invalid address is returned.
441    ///
442    /// @return
443    ///     Returns the entry address for this module.
444    //------------------------------------------------------------------
445    virtual lldb_private::Address
446    GetEntryPointAddress () { return Address();}
447
448    //------------------------------------------------------------------
449    /// Returns the address that represents the header of this object
450    /// file.
451    ///
452    /// The header address is defined as where the header for the object
453    /// file is that describes the content of the file. If the header
454    /// doesn't appear in a section that is defined in the object file,
455    /// an address with no section is returned that has the file offset
456    /// set in the m_file_offset member of the lldb_private::Address object.
457    ///
458    /// @return
459    ///     Returns the entry address for this module.
460    //------------------------------------------------------------------
461    virtual lldb_private::Address
462    GetHeaderAddress () { return Address(m_memory_addr);}
463
464
465    virtual uint32_t
466    GetNumThreadContexts ()
467    {
468        return 0;
469    }
470
471    virtual lldb::RegisterContextSP
472    GetThreadContextAtIndex (uint32_t idx, lldb_private::Thread &thread)
473    {
474        return lldb::RegisterContextSP();
475    }
476    //------------------------------------------------------------------
477    /// The object file should be able to calculate its type by looking
478    /// at its file header and possibly the sections or other data in
479    /// the object file. The file type is used in the debugger to help
480    /// select the correct plug-ins for the job at hand, so this is
481    /// important to get right. If any eTypeXXX definitions do not match
482    /// up with the type of file you are loading, please feel free to
483    /// add a new enumeration value.
484    ///
485    /// @return
486    ///     The calculated file type for the current object file.
487    //------------------------------------------------------------------
488    virtual Type
489    CalculateType() = 0;
490
491    //------------------------------------------------------------------
492    /// The object file should be able to calculate the strata of the
493    /// object file.
494    ///
495    /// Many object files for platforms might be for either user space
496    /// debugging or for kernel debugging. If your object file subclass
497    /// can figure this out, it will help with debugger plug-in selection
498    /// when it comes time to debug.
499    ///
500    /// @return
501    ///     The calculated object file strata for the current object
502    ///     file.
503    //------------------------------------------------------------------
504    virtual Strata
505    CalculateStrata() = 0;
506
507    //------------------------------------------------------------------
508    /// Get the object file version numbers.
509    ///
510    /// Many object files have a set of version numbers that describe
511    /// the version of the executable or shared library. Typically there
512    /// are major, minor and build, but there may be more. This function
513    /// will extract the versions from object files if they are available.
514    ///
515    /// If \a versions is NULL, or if \a num_versions is 0, the return
516    /// value will indicate how many version numbers are available in
517    /// this object file. Then a subsequent call can be made to this
518    /// function with a value of \a versions and \a num_versions that
519    /// has enough storage to store some or all version numbers.
520    ///
521    /// @param[out] versions
522    ///     A pointer to an array of uint32_t types that is \a num_versions
523    ///     long. If this value is NULL, the return value will indicate
524    ///     how many version numbers are required for a subsequent call
525    ///     to this function so that all versions can be retrieved. If
526    ///     the value is non-NULL, then at most \a num_versions of the
527    ///     existing versions numbers will be filled into \a versions.
528    ///     If there is no version information available, \a versions
529    ///     will be filled with \a num_versions UINT32_MAX values
530    ///     and zero will be returned.
531    ///
532    /// @param[in] num_versions
533    ///     The maximum number of entries to fill into \a versions. If
534    ///     this value is zero, then the return value will indicate
535    ///     how many version numbers there are in total so another call
536    ///     to this function can be make with adequate storage in
537    ///     \a versions to get all of the version numbers. If \a
538    ///     num_versions is less than the actual number of version
539    ///     numbers in this object file, only \a num_versions will be
540    ///     filled into \a versions (if \a versions is non-NULL).
541    ///
542    /// @return
543    ///     This function always returns the number of version numbers
544    ///     that this object file has regardless of the number of
545    ///     version numbers that were copied into \a versions.
546    //------------------------------------------------------------------
547    virtual uint32_t
548    GetVersion (uint32_t *versions, uint32_t num_versions)
549    {
550        if (versions && num_versions)
551        {
552            for (uint32_t i=0; i<num_versions; ++i)
553                versions[i] = UINT32_MAX;
554        }
555        return 0;
556    }
557
558    //------------------------------------------------------------------
559    // Member Functions
560    //------------------------------------------------------------------
561    Type
562    GetType ()
563    {
564        if (m_type == eTypeInvalid)
565            m_type = CalculateType();
566        return m_type;
567    }
568
569    Strata
570    GetStrata ()
571    {
572        if (m_strata == eStrataInvalid)
573            m_strata = CalculateStrata();
574        return m_strata;
575    }
576
577    // When an object file is in memory, subclasses should try and lock
578    // the process weak pointer. If the process weak pointer produces a
579    // valid ProcessSP, then subclasses can call this function to read
580    // memory.
581    static lldb::DataBufferSP
582    ReadMemory (const lldb::ProcessSP &process_sp,
583                lldb::addr_t addr,
584                size_t byte_size);
585
586    size_t
587    GetData (off_t offset, size_t length, DataExtractor &data) const;
588
589    size_t
590    CopyData (off_t offset, size_t length, void *dst) const;
591
592    size_t
593    ReadSectionData (const Section *section,
594                     off_t section_offset,
595                     void *dst,
596                     size_t dst_len) const;
597    size_t
598    ReadSectionData (const Section *section,
599                     DataExtractor& section_data) const;
600
601    size_t
602    MemoryMapSectionData (const Section *section,
603                          DataExtractor& section_data) const;
604
605    bool
606    IsInMemory () const
607    {
608        return m_memory_addr != LLDB_INVALID_ADDRESS;
609    }
610
611protected:
612    //------------------------------------------------------------------
613    // Member variables.
614    //------------------------------------------------------------------
615    FileSpec m_file;
616    Type m_type;
617    Strata m_strata;
618    lldb::addr_t m_file_offset; ///< The offset in bytes into the file, or the address in memory
619    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).
620    DataExtractor m_data; ///< The data for this object file so things can be parsed lazily.
621    lldb_private::UnwindTable m_unwind_table; /// < Table of FuncUnwinders objects created for this ObjectFile's functions
622    lldb::ProcessWP m_process_wp;
623    const lldb::addr_t m_memory_addr;
624    STD_UNIQUE_PTR(lldb_private::SectionList) m_sections_ap;
625    STD_UNIQUE_PTR(lldb_private::Symtab) m_symtab_ap;
626
627    //------------------------------------------------------------------
628    /// Sets the architecture for a module.  At present the architecture
629    /// can only be set if it is invalid.  It is not allowed to switch from
630    /// one concrete architecture to another.
631    ///
632    /// @param[in] new_arch
633    ///     The architecture this module will be set to.
634    ///
635    /// @return
636    ///     Returns \b true if the architecture was changed, \b
637    ///     false otherwise.
638    //------------------------------------------------------------------
639    bool SetModulesArchitecture (const ArchSpec &new_arch);
640
641private:
642    DISALLOW_COPY_AND_ASSIGN (ObjectFile);
643};
644
645} // namespace lldb_private
646
647#endif  // liblldb_ObjectFile_h_
648
649