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