1 //===-- ObjectFile.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ObjectFile_h_
10 #define liblldb_ObjectFile_h_
12 #include "lldb/Core/FileSpecList.h"
13 #include "lldb/Core/ModuleChild.h"
14 #include "lldb/Core/PluginInterface.h"
15 #include "lldb/Symbol/Symtab.h"
16 #include "lldb/Symbol/UnwindTable.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Endian.h"
19 #include "lldb/Utility/FileSpec.h"
20 #include "lldb/Utility/UUID.h"
21 #include "lldb/lldb-private.h"
22 #include "llvm/Support/VersionTuple.h"
24 namespace lldb_private {
26 class ObjectFileJITDelegate {
28 ObjectFileJITDelegate() {}
30 virtual ~ObjectFileJITDelegate() {}
32 virtual lldb::ByteOrder GetByteOrder() const = 0;
34 virtual uint32_t GetAddressByteSize() const = 0;
36 virtual void PopulateSymtab(lldb_private::ObjectFile *obj_file,
37 lldb_private::Symtab &symtab) = 0;
39 virtual void PopulateSectionList(lldb_private::ObjectFile *obj_file,
40 lldb_private::SectionList §ion_list) = 0;
42 virtual ArchSpec GetArchitecture() = 0;
45 /// \class ObjectFile ObjectFile.h "lldb/Symbol/ObjectFile.h"
46 /// A plug-in interface definition class for object file parsers.
48 /// Object files belong to Module objects and know how to extract information
49 /// from executable, shared library, and object (.o) files used by operating
50 /// system runtime. The symbol table and section list for an object file.
52 /// Object files can be represented by the entire file, or by part of a file.
53 /// An example of a partial file ObjectFile is one that contains information
54 /// for one of multiple architectures in the same file.
56 /// Once an architecture is selected the object file information can be
57 /// extracted from this abstract class.
58 class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
59 public PluginInterface,
61 friend class lldb_private::Module;
66 /// A core file that has a checkpoint of a program's execution state.
68 /// A normal executable.
70 /// An object file that contains only debug information.
72 /// The platform's dynamic linker executable.
74 /// An intermediate object file.
76 /// A shared library that can be used during execution.
78 /// A library that can be linked against but not used for execution.
80 /// JIT code that has symbols, sections and possibly debug info.
96 llvm::ArrayRef<uint8_t> Contents;
99 /// Construct with a parent module, offset, and header data.
101 /// Object files belong to modules and a valid module must be supplied upon
102 /// construction. The at an offset within a file for objects that contain
103 /// more than one architecture or object.
104 ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr,
105 lldb::offset_t file_offset, lldb::offset_t length,
106 const lldb::DataBufferSP &data_sp, lldb::offset_t data_offset);
108 ObjectFile(const lldb::ModuleSP &module_sp, const lldb::ProcessSP &process_sp,
109 lldb::addr_t header_addr, lldb::DataBufferSP &data_sp);
113 /// The destructor is virtual since this class is designed to be inherited
114 /// from by the plug-in instance.
115 ~ObjectFile() override;
117 /// Dump a description of this object to a Stream.
119 /// Dump a description of the current contents of this object to the
120 /// supplied stream \a s. The dumping should include the section list if it
121 /// has been parsed, and the symbol table if it has been parsed.
124 /// The stream to which to dump the object description.
125 virtual void Dump(Stream *s) = 0;
127 /// Find a ObjectFile plug-in that can parse \a file_spec.
129 /// Scans all loaded plug-in interfaces that implement versions of the
130 /// ObjectFile plug-in interface and returns the first instance that can
133 /// \param[in] module_sp
134 /// The parent module that owns this object file.
136 /// \param[in] file_spec
137 /// A file specification that indicates which file to use as the
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.
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.
149 /// \see ObjectFile::ParseHeader()
150 static lldb::ObjectFileSP
151 FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec,
152 lldb::offset_t file_offset, lldb::offset_t file_size,
153 lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset);
155 /// Find a ObjectFile plug-in that can parse a file in memory.
157 /// Scans all loaded plug-in interfaces that implement versions of the
158 /// ObjectFile plug-in interface and returns the first instance that can
161 /// \param[in] module_sp
162 /// The parent module that owns this object file.
164 /// \param[in] process_sp
165 /// A shared pointer to the process whose memory space contains
166 /// an object file. This will be stored as a std::weak_ptr.
168 /// \param[in] header_addr
169 /// The address of the header for the object file in memory.
170 static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp,
171 const lldb::ProcessSP &process_sp,
172 lldb::addr_t header_addr,
173 lldb::DataBufferSP &file_data_sp);
175 static size_t GetModuleSpecifications(const FileSpec &file,
176 lldb::offset_t file_offset,
177 lldb::offset_t file_size,
178 ModuleSpecList &specs);
180 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
181 lldb::DataBufferSP &data_sp,
182 lldb::offset_t data_offset,
183 lldb::offset_t file_offset,
184 lldb::offset_t file_size,
185 lldb_private::ModuleSpecList &specs);
186 /// Split a path into a file path with object name.
188 /// For paths like "/tmp/foo.a(bar.o)" we often need to split a path up into
189 /// the actual path name and into the object name so we can make a valid
190 /// object file from it.
192 /// \param[in] path_with_object
193 /// A path that might contain an archive path with a .o file
194 /// specified in parens in the basename of the path.
196 /// \param[out] archive_file
197 /// If \b true is returned, \a file_spec will be filled in with
198 /// the path to the archive.
200 /// \param[out] archive_object
201 /// If \b true is returned, \a object will be filled in with
202 /// the name of the object inside the archive.
205 /// \b true if the path matches the pattern of archive + object
206 /// and \a archive_file and \a archive_object are modified,
207 /// \b false otherwise and \a archive_file and \a archive_object
208 /// are guaranteed to be remain unchanged.
209 static bool SplitArchivePathWithObject(
210 llvm::StringRef path_with_object, lldb_private::FileSpec &archive_file,
211 lldb_private::ConstString &archive_object, bool must_exist);
215 virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
217 /// Gets the address size in bytes for the current object file.
220 /// The size of an address in bytes for the currently selected
221 /// architecture (and object for archives). Returns zero if no
222 /// architecture or object has been selected.
223 virtual uint32_t GetAddressByteSize() const = 0;
225 /// Get the address type given a file address in an object file.
227 /// Many binary file formats know what kinds This is primarily for ARM
228 /// binaries, though it can be applied to any executable file format that
229 /// supports different opcode types within the same binary. ARM binaries
230 /// support having both ARM and Thumb within the same executable container.
231 /// We need to be able to get \return
232 /// The size of an address in bytes for the currently selected
233 /// architecture (and object for archives). Returns zero if no
234 /// architecture or object has been selected.
235 virtual AddressClass GetAddressClass(lldb::addr_t file_addr);
237 /// Extract the dependent modules from an object file.
239 /// If an object file has information about which other images it depends on
240 /// (such as shared libraries), this function will provide the list. Since
241 /// many executables or shared libraries may depend on the same files,
242 /// FileSpecList::AppendIfUnique(const FileSpec &) should be used to make
243 /// sure any files that are added are not already in the list.
245 /// \param[out] file_list
246 /// A list of file specification objects that gets dependent
247 /// files appended to.
250 /// The number of new files that were appended to \a file_list.
252 /// \see FileSpecList::AppendIfUnique(const FileSpec &)
253 virtual uint32_t GetDependentModules(FileSpecList &file_list) = 0;
255 /// Tells whether this object file is capable of being the main executable
259 /// \b true if it is, \b false otherwise.
260 virtual bool IsExecutable() const = 0;
262 /// Returns the offset into a file at which this object resides.
264 /// Some files contain many object files, and this function allows access to
265 /// an object's offset within the file.
268 /// The offset in bytes into the file. Defaults to zero for
269 /// simple object files that a represented by an entire file.
270 virtual lldb::addr_t GetFileOffset() const { return m_file_offset; }
272 virtual lldb::addr_t GetByteSize() const { return m_length; }
274 /// Get accessor to the object file specification.
277 /// The file specification object pointer if there is one, or
278 /// NULL if this object is only from memory.
279 virtual FileSpec &GetFileSpec() { return m_file; }
281 /// Get const accessor to the object file specification.
284 /// The const file specification object pointer if there is one,
285 /// or NULL if this object is only from memory.
286 virtual const FileSpec &GetFileSpec() const { return m_file; }
288 /// Get the ArchSpec for this object file.
291 /// The ArchSpec of this object file. In case of error, an invalid
292 /// ArchSpec object is returned.
293 virtual ArchSpec GetArchitecture() = 0;
295 /// Gets the section list for the currently selected architecture (and
296 /// object for archives).
298 /// Section list parsing can be deferred by ObjectFile instances until this
299 /// accessor is called the first time.
302 /// The list of sections contained in this object file.
303 virtual SectionList *GetSectionList(bool update_module_section_list = true);
305 virtual void CreateSections(SectionList &unified_section_list) = 0;
307 /// Notify the ObjectFile that the file addresses in the Sections for this
308 /// module have been changed.
309 virtual void SectionFileAddressesChanged() {}
311 /// Gets the symbol table for the currently selected architecture (and
312 /// object for archives).
314 /// Symbol table parsing can be deferred by ObjectFile instances until this
315 /// accessor is called the first time.
318 /// The symbol table for this object file.
319 virtual Symtab *GetSymtab() = 0;
321 /// Perform relocations on the section if necessary.
323 virtual void RelocateSection(lldb_private::Section *section);
325 /// Appends a Symbol for the specified so_addr to the symbol table.
327 /// If verify_unique is false, the symbol table is not searched to determine
328 /// if a Symbol found at this address has already been added to the symbol
329 /// table. When verify_unique is true, this method resolves the Symbol as
330 /// the first match in the SymbolTable and appends a Symbol only if
334 /// The resolved symbol or nullptr. Returns nullptr if a
335 /// a Symbol could not be found for the specified so_addr.
336 virtual Symbol *ResolveSymbolForAddress(const Address &so_addr,
337 bool verify_unique) {
338 // Typically overridden to lazily add stripped symbols recoverable from the
339 // exception handling unwind information (i.e. without parsing the entire
342 // The availability of LC_FUNCTION_STARTS allows ObjectFileMachO to
343 // efficiently add stripped symbols when the symbol table is first
344 // constructed. Poorer cousins are PECoff and ELF.
348 /// Detect if this object file has been stripped of local symbols.
349 /// Detect if this object file has been stripped of local symbols.
352 /// Return \b true if the object file has been stripped of local
354 virtual bool IsStripped() = 0;
356 /// Frees the symbol table.
358 /// This function should only be used when an object file is
359 virtual void ClearSymtab();
361 /// Gets the UUID for this object file.
363 /// If the object file format contains a UUID, the value should be returned.
364 /// Else ObjectFile instances should return the MD5 checksum of all of the
365 /// bytes for the object file (or memory for memory based object files).
368 /// The object file's UUID. In case of an error, an empty UUID is
370 virtual UUID GetUUID() = 0;
372 /// Gets the file spec list of libraries re-exported by this object file.
374 /// If the object file format has the notion of one library re-exporting the
375 /// symbols from another, the re-exported libraries will be returned in the
379 /// Returns filespeclist.
380 virtual lldb_private::FileSpecList GetReExportedLibraries() {
381 return FileSpecList();
384 /// Sets the load address for an entire module, assuming a rigid slide of
385 /// sections, if possible in the implementation.
388 /// Returns true iff any section's load address changed.
389 virtual bool SetLoadAddress(Target &target, lldb::addr_t value,
390 bool value_is_offset) {
394 /// Gets whether endian swapping should occur when extracting data from this
398 /// Returns \b true if endian swapping is needed, \b false
400 virtual lldb::ByteOrder GetByteOrder() const = 0;
402 /// Attempts to parse the object header.
404 /// This function is used as a test to see if a given plug-in instance can
405 /// parse the header data already contained in ObjectFile::m_data. If an
406 /// object file parser does not recognize that magic bytes in a header,
407 /// false should be returned and the next plug-in can attempt to parse an
411 /// Returns \b true if the header was parsed successfully, \b
413 virtual bool ParseHeader() = 0;
415 /// Returns if the function bounds for symbols in this symbol file are
418 /// The unwinder can emulate the instructions of functions to understand
419 /// prologue/epilogue code sequences, where registers are spilled on the
420 /// stack, etc. This feature relies on having the correct start addresses
421 /// of all functions. If the ObjectFile has a way to tell that symbols have
422 /// been stripped and there's no way to reconstruct start addresses (e.g.
423 /// LC_FUNCTION_STARTS on Mach-O, or eh_frame unwind info), the ObjectFile
424 /// should indicate that assembly emulation should not be used for this
427 /// It is uncommon for this to return false. An ObjectFile needs to be sure
428 /// that symbol start addresses are unavailable before false is returned.
429 /// If it is unclear, this should return true.
432 /// Returns true if assembly emulation should be used for this
434 /// Only returns false if the ObjectFile is sure that symbol
435 /// addresses are insufficient for accurate assembly emulation.
436 virtual bool AllowAssemblyEmulationUnwindPlans() { return true; }
438 /// Similar to Process::GetImageInfoAddress().
440 /// Some platforms embed auxiliary structures useful to debuggers in the
441 /// address space of the inferior process. This method returns the address
442 /// of such a structure if the information can be resolved via entries in
443 /// the object file. ELF, for example, provides a means to hook into the
444 /// runtime linker so that a debugger may monitor the loading and unloading
445 /// of shared libraries.
448 /// The address of any auxiliary tables, or an invalid address if this
449 /// object file format does not support or contain such information.
450 virtual lldb_private::Address GetImageInfoAddress(Target *target) {
454 /// Returns the address of the Entry Point in this object file - if the
455 /// object file doesn't have an entry point (because it is not an executable
456 /// file) then an invalid address is returned.
459 /// Returns the entry address for this module.
460 virtual lldb_private::Address GetEntryPointAddress() { return Address(); }
462 /// Returns base address of this object file.
464 /// This also sometimes referred to as the "preferred load address" or the
465 /// "image base address". Addresses within object files are often expressed
466 /// relative to this base. If this address corresponds to a specific section
467 /// (usually the first byte of the first section) then the returned address
468 /// will have this section set. Otherwise, the address will just have the
469 /// offset member filled in, indicating that this represents a file address.
470 virtual lldb_private::Address GetBaseAddress() {
471 return Address(m_memory_addr);
474 virtual uint32_t GetNumThreadContexts() { return 0; }
476 /// Some object files may have an identifier string embedded in them, e.g.
477 /// in a Mach-O core file using the LC_IDENT load command (which is
478 /// obsolete, but can still be found in some old files)
481 /// Returns the identifier string if one exists, else an empty
483 virtual std::string GetIdentifierString () {
484 return std::string();
487 /// When the ObjectFile is a core file, lldb needs to locate the "binary" in
488 /// the core file. lldb can iterate over the pages looking for a valid
489 /// binary, but some core files may have metadata describing where the main
490 /// binary is exactly which removes ambiguity when there are multiple
491 /// binaries present in the captured memory pages.
493 /// \param[out] address
494 /// If the address of the binary is specified, this will be set.
495 /// This is an address is the virtual address space of the core file
496 /// memory segments; it is not an offset into the object file.
497 /// If no address is available, will be set to LLDB_INVALID_ADDRESS.
500 /// If the uuid of the binary is specified, this will be set.
501 /// If no UUID is available, will be cleared.
504 /// Returns true if either address or uuid has been set.
505 virtual bool GetCorefileMainBinaryInfo (lldb::addr_t &address, UUID &uuid) {
506 address = LLDB_INVALID_ADDRESS;
511 virtual lldb::RegisterContextSP
512 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) {
513 return lldb::RegisterContextSP();
516 /// The object file should be able to calculate its type by looking at its
517 /// file header and possibly the sections or other data in the object file.
518 /// The file type is used in the debugger to help select the correct plug-
519 /// ins for the job at hand, so this is important to get right. If any
520 /// eTypeXXX definitions do not match up with the type of file you are
521 /// loading, please feel free to add a new enumeration value.
524 /// The calculated file type for the current object file.
525 virtual Type CalculateType() = 0;
527 /// In cases where the type can't be calculated (elf files), this routine
528 /// allows someone to explicitly set it. As an example, SymbolVendorELF uses
529 /// this routine to set eTypeDebugInfo when loading debug link files.
530 virtual void SetType(Type type) { m_type = type; }
532 /// The object file should be able to calculate the strata of the object
535 /// Many object files for platforms might be for either user space debugging
536 /// or for kernel debugging. If your object file subclass can figure this
537 /// out, it will help with debugger plug-in selection when it comes time to
541 /// The calculated object file strata for the current object
543 virtual Strata CalculateStrata() = 0;
545 /// Get the object file version numbers.
547 /// Many object files have a set of version numbers that describe the
548 /// version of the executable or shared library. Typically there are major,
549 /// minor and build, but there may be more. This function will extract the
550 /// versions from object files if they are available.
553 /// This function returns extracted version numbers as a
554 /// llvm::VersionTuple. In case of error an empty VersionTuple is
556 virtual llvm::VersionTuple GetVersion() { return llvm::VersionTuple(); }
558 /// Get the minimum OS version this object file can run on.
560 /// Some object files have information that specifies the minimum OS version
561 /// that they can be used on.
564 /// This function returns extracted version numbers as a
565 /// llvm::VersionTuple. In case of error an empty VersionTuple is
567 virtual llvm::VersionTuple GetMinimumOSVersion() {
568 return llvm::VersionTuple();
571 /// Get the SDK OS version this object file was built with.
574 /// This function returns extracted version numbers as a
575 /// llvm::VersionTuple. In case of error an empty VersionTuple is
577 virtual llvm::VersionTuple GetSDKVersion() { return llvm::VersionTuple(); }
579 /// Return true if this file is a dynamic link editor (dyld)
581 /// Often times dyld has symbols that mirror symbols in libc and other
582 /// shared libraries (like "malloc" and "free") and the user does _not_ want
583 /// to stop in these shared libraries by default. We can ask the ObjectFile
584 /// if it is such a file and should be avoided for things like settings
585 /// breakpoints and doing function lookups for expressions.
586 virtual bool GetIsDynamicLinkEditor() { return false; }
590 if (m_type == eTypeInvalid)
591 m_type = CalculateType();
596 if (m_strata == eStrataInvalid)
597 m_strata = CalculateStrata();
601 // When an object file is in memory, subclasses should try and lock the
602 // process weak pointer. If the process weak pointer produces a valid
603 // ProcessSP, then subclasses can call this function to read memory.
604 static lldb::DataBufferSP ReadMemory(const lldb::ProcessSP &process_sp,
605 lldb::addr_t addr, size_t byte_size);
607 // This function returns raw file contents. Do not use it if you want
608 // transparent decompression of section contents.
609 size_t GetData(lldb::offset_t offset, size_t length,
610 DataExtractor &data) const;
612 // This function returns raw file contents. Do not use it if you want
613 // transparent decompression of section contents.
614 size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const;
616 // This function will transparently decompress section data if the section if
618 virtual size_t ReadSectionData(Section *section,
619 lldb::offset_t section_offset, void *dst,
622 // This function will transparently decompress section data if the section if
623 // compressed. Note that for compressed section the resulting data size may
624 // be larger than what Section::GetFileSize reports.
625 virtual size_t ReadSectionData(Section *section,
626 DataExtractor §ion_data);
628 bool IsInMemory() const { return m_memory_addr != LLDB_INVALID_ADDRESS; }
630 // Strip linker annotations (such as @@VERSION) from symbol names.
631 virtual llvm::StringRef
632 StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
636 static lldb::SymbolType GetSymbolTypeFromName(
637 llvm::StringRef name,
638 lldb::SymbolType symbol_type_hint = lldb::eSymbolTypeUndefined);
640 /// Loads this objfile to memory.
642 /// Loads the bits needed to create an executable image to the memory. It is
643 /// useful with bare-metal targets where target does not have the ability to
644 /// start a process itself.
646 /// \param[in] target
647 /// Target where to load.
648 virtual std::vector<LoadableData> GetLoadableData(Target &target);
650 /// Creates a plugin-specific call frame info
651 virtual std::unique_ptr<CallFrameInfo> CreateCallFrameInfo();
658 lldb::addr_t m_file_offset; ///< The offset in bytes into the file, or the
660 lldb::addr_t m_length; ///< The length of this object file if it is known (can
661 ///be zero if length is unknown or can't be
664 m_data; ///< The data for this object file so things can be parsed lazily.
665 lldb::ProcessWP m_process_wp;
666 const lldb::addr_t m_memory_addr;
667 std::unique_ptr<lldb_private::SectionList> m_sections_up;
668 std::unique_ptr<lldb_private::Symtab> m_symtab_up;
669 uint32_t m_synthetic_symbol_idx;
671 /// Sets the architecture for a module. At present the architecture can
672 /// only be set if it is invalid. It is not allowed to switch from one
673 /// concrete architecture to another.
675 /// \param[in] new_arch
676 /// The architecture this module will be set to.
679 /// Returns \b true if the architecture was changed, \b
681 bool SetModulesArchitecture(const ArchSpec &new_arch);
683 ConstString GetNextSyntheticSymbolName();
685 static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size,
689 DISALLOW_COPY_AND_ASSIGN(ObjectFile);
692 } // namespace lldb_private
695 template <> struct format_provider<lldb_private::ObjectFile::Type> {
696 static void format(const lldb_private::ObjectFile::Type &type,
697 raw_ostream &OS, StringRef Style);
700 template <> struct format_provider<lldb_private::ObjectFile::Strata> {
701 static void format(const lldb_private::ObjectFile::Strata &strata,
702 raw_ostream &OS, StringRef Style);
706 #endif // liblldb_ObjectFile_h_