LLDB mainline
ObjectFileMachO.h
Go to the documentation of this file.
1//===-- ObjectFileMachO.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_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
10#define LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
11
12#include "lldb/Core/Address.h"
13#include "lldb/Host/SafeMachO.h"
19#include "lldb/Utility/UUID.h"
20#include <optional>
21
22// This class needs to be hidden as eventually belongs in a plugin that
23// will export the ObjectFile protocol
25public:
26 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
27 lldb::offset_t data_offset,
28 const lldb_private::FileSpec *file, lldb::offset_t offset,
29 lldb::offset_t length);
30
31 ObjectFileMachO(const lldb::ModuleSP &module_sp,
33 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
34
35 ~ObjectFileMachO() override = default;
36
37 // Static Functions
38 static void Initialize();
39
40 static void Terminate();
41
42 static llvm::StringRef GetPluginNameStatic() { return "mach-o"; }
43
44 static llvm::StringRef GetPluginDescriptionStatic() {
45 return "Mach-o object file reader (32 and 64 bit)";
46 }
47
49 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
50 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
51 lldb::offset_t file_offset, lldb::offset_t length);
52
54 const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
55 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
56
57 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
58 lldb::DataBufferSP &data_sp,
59 lldb::offset_t data_offset,
60 lldb::offset_t file_offset,
61 lldb::offset_t length,
63
64 static bool SaveCore(const lldb::ProcessSP &process_sp,
65 const lldb_private::SaveCoreOptions &options,
67
68 static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset,
69 lldb::addr_t length);
70
71 // LLVM RTTI support
72 static char ID;
73 bool isA(const void *ClassID) const override {
74 return ClassID == &ID || ObjectFile::isA(ClassID);
75 }
76 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
77
78 // Member Functions
79 bool ParseHeader() override;
80
82 bool value_is_offset) override;
83
84 lldb::ByteOrder GetByteOrder() const override;
85
86 bool IsExecutable() const override;
87
88 bool IsDynamicLoader() const;
89
90 bool IsSharedCacheBinary() const;
91
92 bool IsKext() const;
93
94 uint32_t GetAddressByteSize() const override;
95
97
98 void ParseSymtab(lldb_private::Symtab &symtab) override;
99
100 bool IsStripped() override;
101
102 void CreateSections(lldb_private::SectionList &unified_section_list) override;
103
104 void Dump(lldb_private::Stream *s) override;
105
107
108 lldb_private::UUID GetUUID() override;
109
110 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
111
113 return m_reexported_dylibs;
114 }
115
117
119
120 uint32_t GetNumThreadContexts() override;
121
122 std::vector<std::tuple<lldb::offset_t, lldb::offset_t>>
123 FindLC_NOTEByName(std::string name);
124
125 std::string GetIdentifierString() override;
126
128
129 bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset,
130 lldb_private::UUID &uuid,
131 ObjectFile::BinaryType &type) override;
132
133 bool GetCorefileThreadExtraInfos(std::vector<lldb::tid_t> &tids) override;
134
135 bool LoadCoreFileImages(lldb_private::Process &process) override;
136
138 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
139
140 ObjectFile::Type CalculateType() override;
141
142 ObjectFile::Strata CalculateStrata() override;
143
144 llvm::VersionTuple GetVersion() override;
145
146 llvm::VersionTuple GetMinimumOSVersion() override;
147
148 llvm::VersionTuple GetSDKVersion() override;
149
150 bool GetIsDynamicLinkEditor() override;
151
152 bool CanTrustAddressRanges() override;
153
155 lldb::offset_t *data_offset_ptr,
156 llvm::MachO::mach_header &header);
157
158 bool AllowAssemblyEmulationUnwindPlans() override;
159
161
162 // PluginInterface protocol
163 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
164
165protected:
166 static lldb_private::UUID
167 GetUUID(const llvm::MachO::mach_header &header,
168 const lldb_private::DataExtractor &data,
169 lldb::offset_t lc_offset); // Offset to the first load command
170
172 lldb::ModuleSP module_sp, const llvm::MachO::mach_header &header,
173 const lldb_private::DataExtractor &data, lldb::offset_t lc_offset);
174
175 /// Enumerate all ArchSpecs supported by this Mach-O file.
176 ///
177 /// On macOS one Mach-O slice can contain multiple load commands:
178 /// One load command for being loaded into a macOS process and one
179 /// load command for being loaded into a macCatalyst process. In
180 /// contrast to ObjectContainerUniversalMachO, this is the same
181 /// binary that can be loaded into different contexts.
182 static void GetAllArchSpecs(const llvm::MachO::mach_header &header,
183 const lldb_private::DataExtractor &data,
184 lldb::offset_t lc_offset,
185 lldb_private::ModuleSpec &base_spec,
187
188 /// Intended for same-host arm device debugging where lldb needs to
189 /// detect libraries in the shared cache and augment the nlist entries
190 /// with an on-disk dyld_shared_cache file. The process will record
191 /// the shared cache UUID so the on-disk cache can be matched or rejected
192 /// correctly.
194 lldb::addr_t &base_addr,
195 lldb_private::UUID &uuid);
196
197 /// Intended for same-host arm device debugging where lldb will read
198 /// shared cache libraries out of its own memory instead of the remote
199 /// process' memory as an optimization. If lldb's shared cache UUID
200 /// does not match the process' shared cache UUID, this optimization
201 /// should not be used.
203
205 lldb::addr_t mach_header_load_address,
206 const lldb_private::Section *mach_header_section,
207 const lldb_private::Section *section);
208
211 const lldb::ByteOrder byte_order,
212 const uint32_t addr_byte_size);
213
214 size_t ParseSymtab();
215
218
220 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd,
221 lldb::offset_t offset);
222 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd,
223 lldb::offset_t offset, uint32_t cmd_idx,
224 SegmentParsingContext &context);
225 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd,
226 uint32_t cmd_idx);
227
228 bool SectionIsLoadable(const lldb_private::Section *section);
229
230 /// A corefile may include metadata about all of the binaries that were
231 /// present in the process when the corefile was taken. This is only
232 /// implemented for Mach-O files for now; we'll generalize it when we
233 /// have other systems that can include the same.
235 std::string filename;
240 std::vector<std::tuple<lldb_private::ConstString, lldb::addr_t>>
242 };
243
244 struct LCNoteEntry {
245 LCNoteEntry(uint32_t addr_byte_size, lldb::ByteOrder byte_order)
246 : payload(lldb_private::Stream::eBinary, addr_byte_size, byte_order) {}
247
248 std::string name;
251 };
252
254 std::vector<MachOCorefileImageEntry> all_image_infos;
255 bool IsValid() { return all_image_infos.size() > 0; }
256 };
257
258 /// Get the list of binary images that were present in the process
259 /// when the corefile was produced.
260 /// \return
261 /// The MachOCorefileAllImageInfos object returned will have
262 /// IsValid() == false if the information is unavailable.
263 MachOCorefileAllImageInfos GetCorefileAllImageInfos();
264
265 llvm::MachO::mach_header m_header;
275
276 llvm::MachO::dysymtab_command m_dysymtab;
277 std::vector<llvm::MachO::section_64> m_mach_sections;
278 std::optional<llvm::VersionTuple> m_min_os_version;
279 std::optional<llvm::VersionTuple> m_sdk_versions;
288};
289
290#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
static llvm::raw_ostream & error(Stream &strm)
bool SectionIsLoadable(const lldb_private::Section *section)
llvm::MachO::mach_header m_header
bool m_allow_assembly_emulation_unwind_plans
std::optional< llvm::VersionTuple > m_min_os_version
lldb_private::UUID GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache, const lldb::ByteOrder byte_order, const uint32_t addr_byte_size)
lldb_private::AddressableBits GetAddressableBits() override
Some object files may have the number of bits used for addressing embedded in them,...
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override
Extract the dependent modules from an object file.
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
FileRangeArray m_thread_context_offsets
lldb_private::RangeVector< uint32_t, uint32_t > FileRangeArray
ObjectFile::Type CalculateType() override
The object file should be able to calculate its type by looking at its file header and possibly the s...
lldb_private::RangeVector< uint32_t, uint32_t, 8 > EncryptedFileRanges
static lldb_private::ConstString GetSegmentNameLINKEDIT()
~ObjectFileMachO() override=default
static lldb_private::ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
std::vector< std::tuple< lldb::offset_t, lldb::offset_t > > FindLC_NOTEByName(std::string name)
void Dump(lldb_private::Stream *s) override
Dump a description of this object to a Stream.
bool AllowAssemblyEmulationUnwindPlans() override
Returns if the function bounds for symbols in this symbol file are likely accurate.
std::string GetIdentifierString() override
Some object files may have an identifier string embedded in them, e.g.
void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd, lldb::offset_t offset, uint32_t cmd_idx, SegmentParsingContext &context)
std::vector< llvm::MachO::section_64 > m_mach_sections
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override
Sets the load address for an entire module, assuming a rigid slide of sections, if possible in the im...
void GetProcessSharedCacheUUID(lldb_private::Process *, lldb::addr_t &base_addr, lldb_private::UUID &uuid)
Intended for same-host arm device debugging where lldb needs to detect libraries in the shared cache ...
llvm::StringRef GetPluginName() override
bool GetIsDynamicLinkEditor() override
Return true if this file is a dynamic link editor (dyld)
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
bool ParseHeader() override
Attempts to parse the object header.
static lldb_private::ConstString GetSegmentNameDATA_DIRTY()
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
static lldb_private::ConstString GetSegmentNameTEXT()
bool isA(const void *ClassID) const override
static char ID
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
llvm::VersionTuple GetMinimumOSVersion() override
Get the minimum OS version this object file can run on.
static llvm::StringRef GetPluginDescriptionStatic()
static lldb_private::ConstString GetSegmentNameOBJC()
static llvm::StringRef GetPluginNameStatic()
lldb::RegisterContextSP GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override
static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset, lldb::addr_t length)
lldb_private::FileSpecList m_reexported_dylibs
static void GetAllArchSpecs(const llvm::MachO::mach_header &header, const lldb_private::DataExtractor &data, lldb::offset_t lc_offset, lldb_private::ModuleSpec &base_spec, lldb_private::ModuleSpecList &all_specs)
Enumerate all ArchSpecs supported by this Mach-O file.
static bool classof(const ObjectFile *obj)
bool GetCorefileThreadExtraInfos(std::vector< lldb::tid_t > &tids) override
Get metadata about threads from the corefile.
bool IsDynamicLoader() const
static lldb_private::ConstString GetSegmentNameDWARF()
static void Terminate()
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
lldb_private::Address GetEntryPointAddress() override
Returns the address of the Entry Point in this object file - if the object file doesn't have an entry...
lldb_private::Address m_entry_point_address
static void Initialize()
bool LoadCoreFileImages(lldb_private::Process &process) override
Load binaries listed in a corefile.
bool CanTrustAddressRanges() override
Can we trust the address ranges accelerator associated with this object file to be complete.
void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx)
bool IsSharedCacheBinary() const
llvm::VersionTuple GetSDKVersion() override
Get the SDK OS version this object file was built with.
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
static lldb_private::ConstString GetSegmentNameDATA()
lldb_private::FileSpecList GetReExportedLibraries() override
Gets the file spec list of libraries re-exported by this object file.
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
size_t ParseSymtab()
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
lldb::addr_t m_text_address
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
llvm::MachO::dysymtab_command m_dysymtab
bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset, lldb_private::UUID &uuid, ObjectFile::BinaryType &type) override
void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd, lldb::offset_t offset)
MachOCorefileAllImageInfos GetCorefileAllImageInfos()
Get the list of binary images that were present in the process when the corefile was produced.
static bool SaveCore(const lldb::ProcessSP &process_sp, const lldb_private::SaveCoreOptions &options, lldb_private::Status &error)
lldb::addr_t CalculateSectionLoadAddressForMemoryImage(lldb::addr_t mach_header_load_address, const lldb_private::Section *mach_header_section, const lldb_private::Section *section)
static lldb_private::ConstString GetSegmentNameLLVM_COV()
bool m_thread_context_offsets_valid
ObjectFile::Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
void CreateSections(lldb_private::SectionList &unified_section_list) override
static lldb_private::ConstString GetSegmentNameDATA_CONST()
lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override
Get the address type given a file address in an object file.
std::optional< llvm::VersionTuple > m_sdk_versions
void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid)
Intended for same-host arm device debugging where lldb will read shared cache libraries out of its ow...
llvm::VersionTuple GetVersion() override
Get the object file version numbers.
EncryptedFileRanges GetEncryptedFileRanges()
uint32_t GetNumThreadContexts() override
static lldb_private::ConstString GetSectionNameEHFrame()
lldb::offset_t m_linkedit_original_offset
lldb_private::Section * GetMachHeaderSection()
A section + offset based address class.
Definition: Address.h:62
A class which holds the metadata from a remote stub/corefile note about how many bits are used for ad...
An architecture specification class.
Definition: ArchSpec.h:31
A uniqued constant string class.
Definition: ConstString.h:40
An data extractor class.
Definition: DataExtractor.h:48
A file collection class.
Definition: FileSpecList.h:85
A file utility class.
Definition: FileSpec.h:56
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataBufferSP data_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
Definition: ObjectFile.cpp:250
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
An error handling class.
Definition: Status.h:44
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:386
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:333
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:334
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:391
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:370
lldb_private::StreamString payload
LCNoteEntry(uint32_t addr_byte_size, lldb::ByteOrder byte_order)
std::vector< MachOCorefileImageEntry > all_image_infos
A corefile may include metadata about all of the binaries that were present in the process when the c...
std::vector< std::tuple< lldb_private::ConstString, lldb::addr_t > > segment_load_addresses