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