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::FileSpec &outfile,
66 lldb::SaveCoreStyle &core_style,
68
69 static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset,
70 lldb::addr_t length);
71
72 // LLVM RTTI support
73 static char ID;
74 bool isA(const void *ClassID) const override {
75 return ClassID == &ID || ObjectFile::isA(ClassID);
76 }
77 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
78
79 // Member Functions
80 bool ParseHeader() override;
81
83 bool value_is_offset) override;
84
85 lldb::ByteOrder GetByteOrder() const override;
86
87 bool IsExecutable() const override;
88
89 bool IsDynamicLoader() const;
90
91 bool IsSharedCacheBinary() const;
92
93 bool IsKext() const;
94
95 uint32_t GetAddressByteSize() const override;
96
98
99 void ParseSymtab(lldb_private::Symtab &symtab) override;
100
101 bool IsStripped() override;
102
103 void CreateSections(lldb_private::SectionList &unified_section_list) override;
104
105 void Dump(lldb_private::Stream *s) override;
106
108
109 lldb_private::UUID GetUUID() override;
110
111 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
112
114 return m_reexported_dylibs;
115 }
116
118
120
121 uint32_t GetNumThreadContexts() override;
122
123 std::vector<std::tuple<lldb::offset_t, lldb::offset_t>>
124 FindLC_NOTEByName(std::string name);
125
126 std::string GetIdentifierString() override;
127
129
130 bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset,
131 lldb_private::UUID &uuid,
132 ObjectFile::BinaryType &type) override;
133
134 bool GetCorefileThreadExtraInfos(std::vector<lldb::tid_t> &tids) override;
135
136 bool LoadCoreFileImages(lldb_private::Process &process) override;
137
139 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
140
141 ObjectFile::Type CalculateType() override;
142
143 ObjectFile::Strata CalculateStrata() override;
144
145 llvm::VersionTuple GetVersion() override;
146
147 llvm::VersionTuple GetMinimumOSVersion() override;
148
149 llvm::VersionTuple GetSDKVersion() override;
150
151 bool GetIsDynamicLinkEditor() override;
152
153 bool CanTrustAddressRanges() override;
154
156 lldb::offset_t *data_offset_ptr,
157 llvm::MachO::mach_header &header);
158
159 bool AllowAssemblyEmulationUnwindPlans() override;
160
162
163 // PluginInterface protocol
164 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
165
166protected:
167 static lldb_private::UUID
168 GetUUID(const llvm::MachO::mach_header &header,
169 const lldb_private::DataExtractor &data,
170 lldb::offset_t lc_offset); // Offset to the first load command
171
173 lldb::ModuleSP module_sp, const llvm::MachO::mach_header &header,
174 const lldb_private::DataExtractor &data, lldb::offset_t lc_offset);
175
176 /// Enumerate all ArchSpecs supported by this Mach-O file.
177 ///
178 /// On macOS one Mach-O slice can contain multiple load commands:
179 /// One load command for being loaded into a macOS process and one
180 /// load command for being loaded into a macCatalyst process. In
181 /// contrast to ObjectContainerUniversalMachO, this is the same
182 /// binary that can be loaded into different contexts.
183 static void GetAllArchSpecs(const llvm::MachO::mach_header &header,
184 const lldb_private::DataExtractor &data,
185 lldb::offset_t lc_offset,
186 lldb_private::ModuleSpec &base_spec,
188
189 /// Intended for same-host arm device debugging where lldb needs to
190 /// detect libraries in the shared cache and augment the nlist entries
191 /// with an on-disk dyld_shared_cache file. The process will record
192 /// the shared cache UUID so the on-disk cache can be matched or rejected
193 /// correctly.
195 lldb::addr_t &base_addr,
196 lldb_private::UUID &uuid);
197
198 /// Intended for same-host arm device debugging where lldb will read
199 /// shared cache libraries out of its own memory instead of the remote
200 /// process' memory as an optimization. If lldb's shared cache UUID
201 /// does not match the process' shared cache UUID, this optimization
202 /// should not be used.
204
206 lldb::addr_t mach_header_load_address,
207 const lldb_private::Section *mach_header_section,
208 const lldb_private::Section *section);
209
212 const lldb::ByteOrder byte_order,
213 const uint32_t addr_byte_size);
214
215 size_t ParseSymtab();
216
219
221 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd,
222 lldb::offset_t offset);
223 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd,
224 lldb::offset_t offset, uint32_t cmd_idx,
225 SegmentParsingContext &context);
226 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd,
227 uint32_t cmd_idx);
228
229 bool SectionIsLoadable(const lldb_private::Section *section);
230
231 /// A corefile may include metadata about all of the binaries that were
232 /// present in the process when the corefile was taken. This is only
233 /// implemented for Mach-O files for now; we'll generalize it when we
234 /// have other systems that can include the same.
236 std::string filename;
241 std::vector<std::tuple<lldb_private::ConstString, lldb::addr_t>>
243 };
244
245 struct LCNoteEntry {
246 LCNoteEntry(uint32_t addr_byte_size, lldb::ByteOrder byte_order)
247 : payload(lldb_private::Stream::eBinary, addr_byte_size, byte_order) {}
248
249 std::string name;
252 };
253
255 std::vector<MachOCorefileImageEntry> all_image_infos;
256 bool IsValid() { return all_image_infos.size() > 0; }
257 };
258
259 /// Get the list of binary images that were present in the process
260 /// when the corefile was produced.
261 /// \return
262 /// The MachOCorefileAllImageInfos object returned will have
263 /// IsValid() == false if the information is unavailable.
264 MachOCorefileAllImageInfos GetCorefileAllImageInfos();
265
266 llvm::MachO::mach_header m_header;
276
277 llvm::MachO::dysymtab_command m_dysymtab;
278 std::vector<llvm::MachO::section_64> m_mach_sections;
279 std::optional<llvm::VersionTuple> m_min_os_version;
280 std::optional<llvm::VersionTuple> m_sdk_versions;
289};
290
291#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
static bool SaveCore(const lldb::ProcessSP &process_sp, const lldb_private::FileSpec &outfile, lldb::SaveCoreStyle &core_style, lldb_private::Status &error)
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.
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:241
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.
Definition: SBAttachInfo.h:14
uint64_t offset_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:329
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:386
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365
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