]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Symbol/ObjectFile.h
Vendor import of lldb trunk r338150:
[FreeBSD/FreeBSD.git] / include / lldb / Symbol / ObjectFile.h
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/Core/FileSpecList.h"
14 #include "lldb/Core/ModuleChild.h"
15 #include "lldb/Core/PluginInterface.h"
16 #include "lldb/Symbol/Symtab.h"
17 #include "lldb/Symbol/UnwindTable.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Endian.h"
20 #include "lldb/Utility/FileSpec.h"
21 #include "lldb/Utility/UUID.h"
22 #include "lldb/lldb-private.h"
23 #include "llvm/Support/VersionTuple.h"
24
25 namespace lldb_private {
26
27 class ObjectFileJITDelegate {
28 public:
29   ObjectFileJITDelegate() {}
30
31   virtual ~ObjectFileJITDelegate() {}
32
33   virtual lldb::ByteOrder GetByteOrder() const = 0;
34
35   virtual uint32_t GetAddressByteSize() const = 0;
36
37   virtual void PopulateSymtab(lldb_private::ObjectFile *obj_file,
38                               lldb_private::Symtab &symtab) = 0;
39
40   virtual void PopulateSectionList(lldb_private::ObjectFile *obj_file,
41                                    lldb_private::SectionList &section_list) = 0;
42
43   virtual bool GetArchitecture(lldb_private::ArchSpec &arch) = 0;
44 };
45
46 //----------------------------------------------------------------------
47 /// @class ObjectFile ObjectFile.h "lldb/Symbol/ObjectFile.h"
48 /// A plug-in interface definition class for object file parsers.
49 ///
50 /// Object files belong to Module objects and know how to extract information
51 /// from executable, shared library, and object (.o) files used by operating
52 /// system runtime. The symbol table and section list for an object file.
53 ///
54 /// Object files can be represented by the entire file, or by part of a file.
55 /// An example of a partial file ObjectFile is one that contains information
56 /// for one of multiple architectures in the same file.
57 ///
58 /// Once an architecture is selected the object file information can be
59 /// extracted from this abstract class.
60 //----------------------------------------------------------------------
61 class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
62                    public PluginInterface,
63                    public ModuleChild {
64   friend class lldb_private::Module;
65
66 public:
67   typedef enum {
68     eTypeInvalid = 0,
69     eTypeCoreFile,      /// A core file that has a checkpoint of a program's
70                         /// execution state
71     eTypeExecutable,    /// A normal executable
72     eTypeDebugInfo,     /// An object file that contains only debug information
73     eTypeDynamicLinker, /// The platform's dynamic linker executable
74     eTypeObjectFile,    /// An intermediate object file
75     eTypeSharedLibrary, /// A shared library that can be used during execution
76     eTypeStubLibrary, /// A library that can be linked against but not used for
77                       /// execution
78     eTypeJIT, /// JIT code that has symbols, sections and possibly debug info
79     eTypeUnknown
80   } Type;
81
82   typedef enum {
83     eStrataInvalid = 0,
84     eStrataUnknown,
85     eStrataUser,
86     eStrataKernel,
87     eStrataRawImage,
88     eStrataJIT
89   } Strata;
90
91   struct LoadableData {
92     lldb::addr_t Dest;
93     llvm::ArrayRef<uint8_t> Contents;
94   };
95
96   //------------------------------------------------------------------
97   /// Construct with a parent module, offset, and header data.
98   ///
99   /// Object files belong to modules and a valid module must be supplied upon
100   /// construction. The at an offset within a file for objects that contain
101   /// more than one architecture or object.
102   //------------------------------------------------------------------
103   ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr,
104              lldb::offset_t file_offset, lldb::offset_t length,
105              const lldb::DataBufferSP &data_sp, lldb::offset_t data_offset);
106
107   ObjectFile(const lldb::ModuleSP &module_sp, const lldb::ProcessSP &process_sp,
108              lldb::addr_t header_addr, lldb::DataBufferSP &data_sp);
109
110   //------------------------------------------------------------------
111   /// Destructor.
112   ///
113   /// The destructor is virtual since this class is designed to be inherited
114   /// from by the plug-in instance.
115   //------------------------------------------------------------------
116   ~ObjectFile() override;
117
118   //------------------------------------------------------------------
119   /// Dump a description of this object to a Stream.
120   ///
121   /// Dump a description of the current contents of this object to the
122   /// supplied stream \a s. The dumping should include the section list if it
123   /// has been parsed, and the symbol table if it has been parsed.
124   ///
125   /// @param[in] s
126   ///     The stream to which to dump the object description.
127   //------------------------------------------------------------------
128   virtual void Dump(Stream *s) = 0;
129
130   //------------------------------------------------------------------
131   /// Find a ObjectFile plug-in that can parse \a file_spec.
132   ///
133   /// Scans all loaded plug-in interfaces that implement versions of the
134   /// ObjectFile plug-in interface and returns the first instance that can
135   /// parse the file.
136   ///
137   /// @param[in] module
138   ///     The parent module that owns this object file.
139   ///
140   /// @param[in] file_spec
141   ///     A file specification that indicates which file to use as the
142   ///     object file.
143   ///
144   /// @param[in] file_offset
145   ///     The offset into the file at which to start parsing the
146   ///     object. This is for files that contain multiple
147   ///     architectures or objects.
148   ///
149   /// @param[in] file_size
150   ///     The size of the current object file if it can be determined
151   ///     or if it is known. This can be zero.
152   ///
153   /// @see ObjectFile::ParseHeader()
154   //------------------------------------------------------------------
155   static lldb::ObjectFileSP
156   FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec,
157              lldb::offset_t file_offset, lldb::offset_t file_size,
158              lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset);
159
160   //------------------------------------------------------------------
161   /// Find a ObjectFile plug-in that can parse a file in memory.
162   ///
163   /// Scans all loaded plug-in interfaces that implement versions of the
164   /// ObjectFile plug-in interface and returns the first instance that can
165   /// parse the file.
166   ///
167   /// @param[in] module
168   ///     The parent module that owns this object file.
169   ///
170   /// @param[in] process_sp
171   ///     A shared pointer to the process whose memory space contains
172   ///     an object file. This will be stored as a std::weak_ptr.
173   ///
174   /// @param[in] header_addr
175   ///     The address of the header for the object file in memory.
176   //------------------------------------------------------------------
177   static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp,
178                                        const lldb::ProcessSP &process_sp,
179                                        lldb::addr_t header_addr,
180                                        lldb::DataBufferSP &file_data_sp);
181
182   static size_t GetModuleSpecifications(const FileSpec &file,
183                                         lldb::offset_t file_offset,
184                                         lldb::offset_t file_size,
185                                         ModuleSpecList &specs);
186
187   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
188                                         lldb::DataBufferSP &data_sp,
189                                         lldb::offset_t data_offset,
190                                         lldb::offset_t file_offset,
191                                         lldb::offset_t file_size,
192                                         lldb_private::ModuleSpecList &specs);
193   //------------------------------------------------------------------
194   /// Split a path into a file path with object name.
195   ///
196   /// For paths like "/tmp/foo.a(bar.o)" we often need to split a path up into
197   /// the actual path name and into the object name so we can make a valid
198   /// object file from it.
199   ///
200   /// @param[in] path_with_object
201   ///     A path that might contain an archive path with a .o file
202   ///     specified in parens in the basename of the path.
203   ///
204   /// @param[out] archive_file
205   ///     If \b true is returned, \a file_spec will be filled in with
206   ///     the path to the archive.
207   ///
208   /// @param[out] archive_object
209   ///     If \b true is returned, \a object will be filled in with
210   ///     the name of the object inside the archive.
211   ///
212   /// @return
213   ///     \b true if the path matches the pattern of archive + object
214   ///     and \a archive_file and \a archive_object are modified,
215   ///     \b false otherwise and \a archive_file and \a archive_object
216   ///     are guaranteed to be remain unchanged.
217   //------------------------------------------------------------------
218   static bool SplitArchivePathWithObject(
219       const char *path_with_object, lldb_private::FileSpec &archive_file,
220       lldb_private::ConstString &archive_object, bool must_exist);
221
222   //------------------------------------------------------------------
223   /// Gets the address size in bytes for the current object file.
224   ///
225   /// @return
226   ///     The size of an address in bytes for the currently selected
227   ///     architecture (and object for archives). Returns zero if no
228   ///     architecture or object has been selected.
229   //------------------------------------------------------------------
230   virtual uint32_t GetAddressByteSize() const = 0;
231
232   //------------------------------------------------------------------
233   /// Get the address type given a file address in an object file.
234   ///
235   /// Many binary file formats know what kinds This is primarily for ARM
236   /// binaries, though it can be applied to any executable file format that
237   /// supports different opcode types within the same binary. ARM binaries
238   /// support having both ARM and Thumb within the same executable container.
239   /// We need to be able to get @return
240   ///     The size of an address in bytes for the currently selected
241   ///     architecture (and object for archives). Returns zero if no
242   ///     architecture or object has been selected.
243   //------------------------------------------------------------------
244   virtual AddressClass GetAddressClass(lldb::addr_t file_addr);
245
246   //------------------------------------------------------------------
247   /// Extract the dependent modules from an object file.
248   ///
249   /// If an object file has information about which other images it depends on
250   /// (such as shared libraries), this function will provide the list. Since
251   /// many executables or shared libraries may depend on the same files,
252   /// FileSpecList::AppendIfUnique(const FileSpec &) should be used to make
253   /// sure any files that are added are not already in the list.
254   ///
255   /// @param[out] file_list
256   ///     A list of file specification objects that gets dependent
257   ///     files appended to.
258   ///
259   /// @return
260   ///     The number of new files that were appended to \a file_list.
261   ///
262   /// @see FileSpecList::AppendIfUnique(const FileSpec &)
263   //------------------------------------------------------------------
264   virtual uint32_t GetDependentModules(FileSpecList &file_list) = 0;
265
266   //------------------------------------------------------------------
267   /// Tells whether this object file is capable of being the main executable
268   /// for a process.
269   ///
270   /// @return
271   ///     \b true if it is, \b false otherwise.
272   //------------------------------------------------------------------
273   virtual bool IsExecutable() const = 0;
274
275   //------------------------------------------------------------------
276   /// Returns the offset into a file at which this object resides.
277   ///
278   /// Some files contain many object files, and this function allows access to
279   /// an object's offset within the file.
280   ///
281   /// @return
282   ///     The offset in bytes into the file. Defaults to zero for
283   ///     simple object files that a represented by an entire file.
284   //------------------------------------------------------------------
285   virtual lldb::addr_t GetFileOffset() const { return m_file_offset; }
286
287   virtual lldb::addr_t GetByteSize() const { return m_length; }
288
289   //------------------------------------------------------------------
290   /// Get accessor to the object file specification.
291   ///
292   /// @return
293   ///     The file specification object pointer if there is one, or
294   ///     NULL if this object is only from memory.
295   //------------------------------------------------------------------
296   virtual FileSpec &GetFileSpec() { return m_file; }
297
298   //------------------------------------------------------------------
299   /// Get const accessor to the object file specification.
300   ///
301   /// @return
302   ///     The const file specification object pointer if there is one,
303   ///     or NULL if this object is only from memory.
304   //------------------------------------------------------------------
305   virtual const FileSpec &GetFileSpec() const { return m_file; }
306
307   //------------------------------------------------------------------
308   /// Get the name of the cpu, vendor and OS for this object file.
309   ///
310   /// This value is a string that represents the target triple where the cpu
311   /// type, the vendor and the OS are encoded into a string.
312   ///
313   /// @param[out] target_triple
314   ///     The string value of the target triple.
315   ///
316   /// @return
317   ///     \b True if the target triple was able to be computed, \b
318   ///     false otherwise.
319   //------------------------------------------------------------------
320   virtual bool GetArchitecture(ArchSpec &arch) = 0;
321
322   //------------------------------------------------------------------
323   /// Gets the section list for the currently selected architecture (and
324   /// object for archives).
325   ///
326   /// Section list parsing can be deferred by ObjectFile instances until this
327   /// accessor is called the first time.
328   ///
329   /// @return
330   ///     The list of sections contained in this object file.
331   //------------------------------------------------------------------
332   virtual SectionList *GetSectionList(bool update_module_section_list = true);
333
334   virtual void CreateSections(SectionList &unified_section_list) = 0;
335
336   //------------------------------------------------------------------
337   /// Notify the ObjectFile that the file addresses in the Sections for this
338   /// module have been changed.
339   //------------------------------------------------------------------
340   virtual void SectionFileAddressesChanged() {}
341
342   //------------------------------------------------------------------
343   /// Gets the symbol table for the currently selected architecture (and
344   /// object for archives).
345   ///
346   /// Symbol table parsing can be deferred by ObjectFile instances until this
347   /// accessor is called the first time.
348   ///
349   /// @return
350   ///     The symbol table for this object file.
351   //------------------------------------------------------------------
352   virtual Symtab *GetSymtab() = 0;
353
354   //------------------------------------------------------------------
355   /// Perform relocations on the section if necessary.
356   ///
357   //------------------------------------------------------------------
358   virtual void RelocateSection(lldb_private::Section *section);
359
360   //------------------------------------------------------------------
361   /// Appends a Symbol for the specified so_addr to the symbol table.
362   ///
363   /// If verify_unique is false, the symbol table is not searched to determine
364   /// if a Symbol found at this address has already been added to the symbol
365   /// table.  When verify_unique is true, this method resolves the Symbol as
366   /// the first match in the SymbolTable and appends a Symbol only if
367   /// required/found.
368   ///
369   /// @return
370   ///     The resolved symbol or nullptr.  Returns nullptr if a
371   ///     a Symbol could not be found for the specified so_addr.
372   //------------------------------------------------------------------
373   virtual Symbol *ResolveSymbolForAddress(const Address &so_addr,
374                                           bool verify_unique) {
375     // Typically overridden to lazily add stripped symbols recoverable from the
376     // exception handling unwind information (i.e. without parsing the entire
377     // eh_frame section.
378     //
379     // The availability of LC_FUNCTION_STARTS allows ObjectFileMachO to
380     // efficiently add stripped symbols when the symbol table is first
381     // constructed.  Poorer cousins are PECoff and ELF.
382     return nullptr;
383   }
384
385   //------------------------------------------------------------------
386   /// Detect if this object file has been stripped of local symbols.
387   //------------------------------------------------------------------
388   /// Detect if this object file has been stripped of local symbols.
389   ///
390   /// @return
391   ///     Return \b true if the object file has been stripped of local
392   ///     symbols.
393   //------------------------------------------------------------------
394   virtual bool IsStripped() = 0;
395
396   //------------------------------------------------------------------
397   /// Frees the symbol table.
398   ///
399   /// This function should only be used when an object file is
400   ///
401   /// @param[in] flags
402   ///     eSymtabFromUnifiedSectionList: Whether to clear symbol table
403   ///     for unified module section list, or object file.
404   ///
405   /// @return
406   ///     The symbol table for this object file.
407   //------------------------------------------------------------------
408   virtual void ClearSymtab();
409
410   //------------------------------------------------------------------
411   /// Gets the UUID for this object file.
412   ///
413   /// If the object file format contains a UUID, the value should be returned.
414   /// Else ObjectFile instances should return the MD5 checksum of all of the
415   /// bytes for the object file (or memory for memory based object files).
416   ///
417   /// @return
418   ///     Returns \b true if a UUID was successfully extracted into
419   ///     \a uuid, \b false otherwise.
420   //------------------------------------------------------------------
421   virtual bool GetUUID(lldb_private::UUID *uuid) = 0;
422
423   //------------------------------------------------------------------
424   /// Gets the symbol file spec list for this object file.
425   ///
426   /// If the object file format contains a debug symbol file link, the values
427   /// will be returned in the FileSpecList.
428   ///
429   /// @return
430   ///     Returns filespeclist.
431   //------------------------------------------------------------------
432   virtual lldb_private::FileSpecList GetDebugSymbolFilePaths() {
433     return FileSpecList();
434   }
435
436   //------------------------------------------------------------------
437   /// Gets the file spec list of libraries re-exported by this object file.
438   ///
439   /// If the object file format has the notion of one library re-exporting the
440   /// symbols from another, the re-exported libraries will be returned in the
441   /// FileSpecList.
442   ///
443   /// @return
444   ///     Returns filespeclist.
445   //------------------------------------------------------------------
446   virtual lldb_private::FileSpecList GetReExportedLibraries() {
447     return FileSpecList();
448   }
449
450   //------------------------------------------------------------------
451   /// Sets the load address for an entire module, assuming a rigid slide of
452   /// sections, if possible in the implementation.
453   ///
454   /// @return
455   ///     Returns true iff any section's load address changed.
456   //------------------------------------------------------------------
457   virtual bool SetLoadAddress(Target &target, lldb::addr_t value,
458                               bool value_is_offset) {
459     return false;
460   }
461
462   //------------------------------------------------------------------
463   /// Gets whether endian swapping should occur when extracting data from this
464   /// object file.
465   ///
466   /// @return
467   ///     Returns \b true if endian swapping is needed, \b false
468   ///     otherwise.
469   //------------------------------------------------------------------
470   virtual lldb::ByteOrder GetByteOrder() const = 0;
471
472   //------------------------------------------------------------------
473   /// Attempts to parse the object header.
474   ///
475   /// This function is used as a test to see if a given plug-in instance can
476   /// parse the header data already contained in ObjectFile::m_data. If an
477   /// object file parser does not recognize that magic bytes in a header,
478   /// false should be returned and the next plug-in can attempt to parse an
479   /// object file.
480   ///
481   /// @return
482   ///     Returns \b true if the header was parsed successfully, \b
483   ///     false otherwise.
484   //------------------------------------------------------------------
485   virtual bool ParseHeader() = 0;
486
487   //------------------------------------------------------------------
488   /// Returns a reference to the UnwindTable for this ObjectFile
489   ///
490   /// The UnwindTable contains FuncUnwinders objects for any function in this
491   /// ObjectFile.  If a FuncUnwinders object hasn't been created yet (i.e. the
492   /// function has yet to be unwound in a stack walk), it will be created when
493   /// requested.  Specifically, we do not create FuncUnwinders objects for
494   /// functions until they are needed.
495   ///
496   /// @return
497   ///     Returns the unwind table for this object file.
498   //------------------------------------------------------------------
499   virtual lldb_private::UnwindTable &GetUnwindTable() { return m_unwind_table; }
500
501   //------------------------------------------------------------------
502   /// Returns if the function bounds for symbols in this symbol file are
503   /// likely accurate.
504   ///
505   /// The unwinder can emulate the instructions of functions to understand
506   /// prologue/epilogue code sequences, where registers are spilled on the
507   /// stack, etc.  This feature relies on having the correct start addresses
508   /// of all functions.  If the ObjectFile has a way to tell that symbols have
509   /// been stripped and there's no way to reconstruct start addresses (e.g.
510   /// LC_FUNCTION_STARTS on Mach-O, or eh_frame unwind info), the ObjectFile
511   /// should indicate that assembly emulation should not be used for this
512   /// module.
513   ///
514   /// It is uncommon for this to return false.  An ObjectFile needs to be sure
515   /// that symbol start addresses are unavailable before false is returned.
516   /// If it is unclear, this should return true.
517   ///
518   /// @return
519   ///     Returns true if assembly emulation should be used for this
520   ///     module.
521   ///     Only returns false if the ObjectFile is sure that symbol
522   ///     addresses are insufficient for accurate assembly emulation.
523   //------------------------------------------------------------------
524   virtual bool AllowAssemblyEmulationUnwindPlans() { return true; }
525
526   //------------------------------------------------------------------
527   /// Similar to Process::GetImageInfoAddress().
528   ///
529   /// Some platforms embed auxiliary structures useful to debuggers in the
530   /// address space of the inferior process.  This method returns the address
531   /// of such a structure if the information can be resolved via entries in
532   /// the object file.  ELF, for example, provides a means to hook into the
533   /// runtime linker so that a debugger may monitor the loading and unloading
534   /// of shared libraries.
535   ///
536   /// @return
537   ///     The address of any auxiliary tables, or an invalid address if this
538   ///     object file format does not support or contain such information.
539   virtual lldb_private::Address GetImageInfoAddress(Target *target) {
540     return Address();
541   }
542
543   //------------------------------------------------------------------
544   /// Returns the address of the Entry Point in this object file - if the
545   /// object file doesn't have an entry point (because it is not an executable
546   /// file) then an invalid address is returned.
547   ///
548   /// @return
549   ///     Returns the entry address for this module.
550   //------------------------------------------------------------------
551   virtual lldb_private::Address GetEntryPointAddress() { return Address(); }
552
553   //------------------------------------------------------------------
554   /// Returns the address that represents the header of this object file.
555   ///
556   /// The header address is defined as where the header for the object file is
557   /// that describes the content of the file. If the header doesn't appear in
558   /// a section that is defined in the object file, an address with no section
559   /// is returned that has the file offset set in the m_file_offset member of
560   /// the lldb_private::Address object.
561   ///
562   /// @return
563   ///     Returns the entry address for this module.
564   //------------------------------------------------------------------
565   virtual lldb_private::Address GetHeaderAddress() {
566     return Address(m_memory_addr);
567   }
568
569   virtual uint32_t GetNumThreadContexts() { return 0; }
570
571   //------------------------------------------------------------------
572   /// Some object files may have an identifier string embedded in them, e.g.
573   /// in a Mach-O core file using the LC_IDENT load command (which  is
574   /// obsolete, but can still be found in some old files)
575   ///
576   /// @return
577   ///     Returns the identifier string if one exists, else an empty
578   ///     string.
579   //------------------------------------------------------------------
580   virtual std::string GetIdentifierString () { 
581       return std::string(); 
582   }
583
584   //------------------------------------------------------------------
585   /// When the ObjectFile is a core file, lldb needs to locate the "binary" in
586   /// the core file.  lldb can iterate over the pages looking for a valid
587   /// binary, but some core files may have metadata  describing where the main
588   /// binary is exactly which removes ambiguity when there are multiple
589   /// binaries present in the captured memory pages.
590   ///
591   /// @param[out] address
592   ///   If the address of the binary is specified, this will be set.
593   ///   This is an address is the virtual address space of the core file
594   ///   memory segments; it is not an offset into the object file.
595   ///   If no address is available, will be set to LLDB_INVALID_ADDRESS.
596   ///
597   /// @param[out] uuid
598   ///   If the uuid of the binary is specified, this will be set.
599   ///   If no UUID is available, will be cleared.
600   ///
601   /// @return
602   ///   Returns true if either address or uuid has been set.
603   //------------------------------------------------------------------
604   virtual bool GetCorefileMainBinaryInfo (lldb::addr_t &address, UUID &uuid) {
605       address = LLDB_INVALID_ADDRESS;
606       uuid.Clear();
607       return false;
608   }
609
610   virtual lldb::RegisterContextSP
611   GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) {
612     return lldb::RegisterContextSP();
613   }
614
615   //------------------------------------------------------------------
616   /// The object file should be able to calculate its type by looking at its
617   /// file header and possibly the sections or other data in the object file.
618   /// The file type is used in the debugger to help select the correct plug-
619   /// ins for the job at hand, so this is important to get right. If any
620   /// eTypeXXX definitions do not match up with the type of file you are
621   /// loading, please feel free to add a new enumeration value.
622   ///
623   /// @return
624   ///     The calculated file type for the current object file.
625   //------------------------------------------------------------------
626   virtual Type CalculateType() = 0;
627
628   //------------------------------------------------------------------
629   /// In cases where the type can't be calculated (elf files), this routine
630   /// allows someone to explicitly set it. As an example, SymbolVendorELF uses
631   /// this routine to set eTypeDebugInfo when loading debug link files.
632   virtual void SetType(Type type) { m_type = type; }
633
634   //------------------------------------------------------------------
635   /// The object file should be able to calculate the strata of the object
636   /// file.
637   ///
638   /// Many object files for platforms might be for either user space debugging
639   /// or for kernel debugging. If your object file subclass can figure this
640   /// out, it will help with debugger plug-in selection when it comes time to
641   /// debug.
642   ///
643   /// @return
644   ///     The calculated object file strata for the current object
645   ///     file.
646   //------------------------------------------------------------------
647   virtual Strata CalculateStrata() = 0;
648
649   //------------------------------------------------------------------
650   /// Get the object file version numbers.
651   ///
652   /// Many object files have a set of version numbers that describe the
653   /// version of the executable or shared library. Typically there are major,
654   /// minor and build, but there may be more. This function will extract the
655   /// versions from object files if they are available.
656   ///
657   /// @return
658   ///     This function returns extracted version numbers as a
659   ///     llvm::VersionTuple. In case of error an empty VersionTuple is
660   ///     returned.
661   //------------------------------------------------------------------
662   virtual llvm::VersionTuple GetVersion() { return llvm::VersionTuple(); }
663
664   //------------------------------------------------------------------
665   /// Get the minimum OS version this object file can run on.
666   ///
667   /// Some object files have information that specifies the minimum OS version
668   /// that they can be used on.
669   ///
670   /// @return
671   ///     This function returns extracted version numbers as a
672   ///     llvm::VersionTuple. In case of error an empty VersionTuple is
673   ///     returned.
674   //------------------------------------------------------------------
675   virtual llvm::VersionTuple GetMinimumOSVersion() {
676     return llvm::VersionTuple();
677   }
678
679   //------------------------------------------------------------------
680   /// Get the SDK OS version this object file was built with.
681   ///
682   /// The versions arguments and returns values are the same as the
683   /// GetMinimumOSVersion()
684   //------------------------------------------------------------------
685   virtual uint32_t GetSDKVersion(uint32_t *versions, uint32_t num_versions) {
686     if (versions && num_versions) {
687       for (uint32_t i = 0; i < num_versions; ++i)
688         versions[i] = UINT32_MAX;
689     }
690     return 0;
691   }
692
693   //------------------------------------------------------------------
694   /// Return true if this file is a dynamic link editor (dyld)
695   ///
696   /// Often times dyld has symbols that mirror symbols in libc and other
697   /// shared libraries (like "malloc" and "free") and the user does _not_ want
698   /// to stop in these shared libraries by default. We can ask the ObjectFile
699   /// if it is such a file and should be avoided for things like settings
700   /// breakpoints and doing function lookups for expressions.
701   //------------------------------------------------------------------
702   virtual bool GetIsDynamicLinkEditor() { return false; }
703
704   //------------------------------------------------------------------
705   // Member Functions
706   //------------------------------------------------------------------
707   Type GetType() {
708     if (m_type == eTypeInvalid)
709       m_type = CalculateType();
710     return m_type;
711   }
712
713   Strata GetStrata() {
714     if (m_strata == eStrataInvalid)
715       m_strata = CalculateStrata();
716     return m_strata;
717   }
718
719   // When an object file is in memory, subclasses should try and lock the
720   // process weak pointer. If the process weak pointer produces a valid
721   // ProcessSP, then subclasses can call this function to read memory.
722   static lldb::DataBufferSP ReadMemory(const lldb::ProcessSP &process_sp,
723                                        lldb::addr_t addr, size_t byte_size);
724
725   // This function returns raw file contents. Do not use it if you want
726   // transparent decompression of section contents.
727   size_t GetData(lldb::offset_t offset, size_t length,
728                  DataExtractor &data) const;
729
730   // This function returns raw file contents. Do not use it if you want
731   // transparent decompression of section contents.
732   size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const;
733
734   // This function will transparently decompress section data if the section if
735   // compressed.
736   virtual size_t ReadSectionData(Section *section,
737                                  lldb::offset_t section_offset, void *dst,
738                                  size_t dst_len);
739
740   // This function will transparently decompress section data if the section if
741   // compressed. Note that for compressed section the resulting data size may
742   // be larger than what Section::GetFileSize reports.
743   virtual size_t ReadSectionData(Section *section,
744                                  DataExtractor &section_data);
745
746   bool IsInMemory() const { return m_memory_addr != LLDB_INVALID_ADDRESS; }
747
748   // Strip linker annotations (such as @@VERSION) from symbol names.
749   virtual llvm::StringRef
750   StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
751     return symbol_name;
752   }
753
754   static lldb::SymbolType GetSymbolTypeFromName(
755       llvm::StringRef name,
756       lldb::SymbolType symbol_type_hint = lldb::eSymbolTypeUndefined);
757
758   //------------------------------------------------------------------
759   /// Loads this objfile to memory.
760   ///
761   /// Loads the bits needed to create an executable image to the memory. It is
762   /// useful with bare-metal targets where target does not have the ability to
763   /// start a process itself.
764   ///
765   /// @param[in] target
766   ///     Target where to load.
767   ///
768   /// @return
769   //------------------------------------------------------------------
770   virtual std::vector<LoadableData> GetLoadableData(Target &target);
771
772 protected:
773   //------------------------------------------------------------------
774   // Member variables.
775   //------------------------------------------------------------------
776   FileSpec m_file;
777   Type m_type;
778   Strata m_strata;
779   lldb::addr_t m_file_offset; ///< The offset in bytes into the file, or the
780                               ///address in memory
781   lldb::addr_t m_length; ///< The length of this object file if it is known (can
782                          ///be zero if length is unknown or can't be
783                          ///determined).
784   DataExtractor
785       m_data; ///< The data for this object file so things can be parsed lazily.
786   lldb_private::UnwindTable m_unwind_table; /// < Table of FuncUnwinders objects
787                                             /// created for this ObjectFile's
788                                             /// functions
789   lldb::ProcessWP m_process_wp;
790   const lldb::addr_t m_memory_addr;
791   std::unique_ptr<lldb_private::SectionList> m_sections_ap;
792   std::unique_ptr<lldb_private::Symtab> m_symtab_ap;
793   uint32_t m_synthetic_symbol_idx;
794
795   //------------------------------------------------------------------
796   /// Sets the architecture for a module.  At present the architecture can
797   /// only be set if it is invalid.  It is not allowed to switch from one
798   /// concrete architecture to another.
799   ///
800   /// @param[in] new_arch
801   ///     The architecture this module will be set to.
802   ///
803   /// @return
804   ///     Returns \b true if the architecture was changed, \b
805   ///     false otherwise.
806   //------------------------------------------------------------------
807   bool SetModulesArchitecture(const ArchSpec &new_arch);
808
809   ConstString GetNextSyntheticSymbolName();
810
811   static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size,
812                                         uint64_t Offset);
813
814 private:
815   DISALLOW_COPY_AND_ASSIGN(ObjectFile);
816 };
817
818 } // namespace lldb_private
819
820 #endif // liblldb_ObjectFile_h_