1 //===-- ObjectFileMachO.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 LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
12 #include "lldb/Core/Address.h"
13 #include "lldb/Host/SafeMachO.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/Utility/FileSpecList.h"
17 #include "lldb/Utility/RangeMap.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/Utility/UUID.h"
22 // This class needs to be hidden as eventually belongs in a plugin that
23 // will export the ObjectFile protocol
24 class ObjectFileMachO
: public lldb_private::ObjectFile
{
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
);
31 ObjectFileMachO(const lldb::ModuleSP
&module_sp
,
32 lldb::WritableDataBufferSP data_sp
,
33 const lldb::ProcessSP
&process_sp
, lldb::addr_t header_addr
);
35 ~ObjectFileMachO() override
= default;
38 static void Initialize();
40 static void Terminate();
42 static llvm::StringRef
GetPluginNameStatic() { return "mach-o"; }
44 static llvm::StringRef
GetPluginDescriptionStatic() {
45 return "Mach-o object file reader (32 and 64 bit)";
48 static lldb_private::ObjectFile
*
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
);
53 static lldb_private::ObjectFile
*CreateMemoryInstance(
54 const lldb::ModuleSP
&module_sp
, lldb::WritableDataBufferSP data_sp
,
55 const lldb::ProcessSP
&process_sp
, lldb::addr_t header_addr
);
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
,
62 lldb_private::ModuleSpecList
&specs
);
64 static bool SaveCore(const lldb::ProcessSP
&process_sp
,
65 const lldb_private::FileSpec
&outfile
,
66 lldb::SaveCoreStyle
&core_style
,
67 lldb_private::Status
&error
);
69 static bool MagicBytesMatch(lldb::DataBufferSP data_sp
, lldb::addr_t offset
,
74 bool isA(const void *ClassID
) const override
{
75 return ClassID
== &ID
|| ObjectFile::isA(ClassID
);
77 static bool classof(const ObjectFile
*obj
) { return obj
->isA(&ID
); }
80 bool ParseHeader() override
;
82 bool SetLoadAddress(lldb_private::Target
&target
, lldb::addr_t value
,
83 bool value_is_offset
) override
;
85 lldb::ByteOrder
GetByteOrder() const override
;
87 bool IsExecutable() const override
;
89 bool IsDynamicLoader() const;
91 bool IsSharedCacheBinary() const;
95 uint32_t GetAddressByteSize() const override
;
97 lldb_private::AddressClass
GetAddressClass(lldb::addr_t file_addr
) override
;
99 void ParseSymtab(lldb_private::Symtab
&symtab
) override
;
101 bool IsStripped() override
;
103 void CreateSections(lldb_private::SectionList
&unified_section_list
) override
;
105 void Dump(lldb_private::Stream
*s
) override
;
107 lldb_private::ArchSpec
GetArchitecture() override
;
109 lldb_private::UUID
GetUUID() override
;
111 uint32_t GetDependentModules(lldb_private::FileSpecList
&files
) override
;
113 lldb_private::FileSpecList
GetReExportedLibraries() override
{
114 return m_reexported_dylibs
;
117 lldb_private::Address
GetEntryPointAddress() override
;
119 lldb_private::Address
GetBaseAddress() override
;
121 uint32_t GetNumThreadContexts() override
;
123 std::vector
<std::tuple
<lldb::offset_t
, lldb::offset_t
>>
124 FindLC_NOTEByName(std::string name
);
126 std::string
GetIdentifierString() override
;
128 lldb_private::AddressableBits
GetAddressableBits() override
;
130 bool GetCorefileMainBinaryInfo(lldb::addr_t
&value
, bool &value_is_offset
,
131 lldb_private::UUID
&uuid
,
132 ObjectFile::BinaryType
&type
) override
;
134 bool GetCorefileThreadExtraInfos(std::vector
<lldb::tid_t
> &tids
) override
;
136 bool LoadCoreFileImages(lldb_private::Process
&process
) override
;
138 lldb::RegisterContextSP
139 GetThreadContextAtIndex(uint32_t idx
, lldb_private::Thread
&thread
) override
;
141 ObjectFile::Type
CalculateType() override
;
143 ObjectFile::Strata
CalculateStrata() override
;
145 llvm::VersionTuple
GetVersion() override
;
147 llvm::VersionTuple
GetMinimumOSVersion() override
;
149 llvm::VersionTuple
GetSDKVersion() override
;
151 bool GetIsDynamicLinkEditor() override
;
153 bool CanTrustAddressRanges() override
;
155 static bool ParseHeader(lldb_private::DataExtractor
&data
,
156 lldb::offset_t
*data_offset_ptr
,
157 llvm::MachO::mach_header
&header
);
159 bool AllowAssemblyEmulationUnwindPlans() override
;
161 lldb_private::Section
*GetMachHeaderSection();
163 // PluginInterface protocol
164 llvm::StringRef
GetPluginName() override
{ return GetPluginNameStatic(); }
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
172 static lldb_private::ArchSpec
GetArchitecture(
173 lldb::ModuleSP module_sp
, const llvm::MachO::mach_header
&header
,
174 const lldb_private::DataExtractor
&data
, lldb::offset_t lc_offset
);
176 /// Enumerate all ArchSpecs supported by this Mach-O file.
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
,
187 lldb_private::ModuleSpecList
&all_specs
);
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
194 void GetProcessSharedCacheUUID(lldb_private::Process
*,
195 lldb::addr_t
&base_addr
,
196 lldb_private::UUID
&uuid
);
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.
203 void GetLLDBSharedCacheUUID(lldb::addr_t
&base_addir
, lldb_private::UUID
&uuid
);
205 lldb::addr_t
CalculateSectionLoadAddressForMemoryImage(
206 lldb::addr_t mach_header_load_address
,
207 const lldb_private::Section
*mach_header_section
,
208 const lldb_private::Section
*section
);
211 GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache
,
212 const lldb::ByteOrder byte_order
,
213 const uint32_t addr_byte_size
);
215 size_t ParseSymtab();
217 typedef lldb_private::RangeVector
<uint32_t, uint32_t, 8> EncryptedFileRanges
;
218 EncryptedFileRanges
GetEncryptedFileRanges();
220 struct SegmentParsingContext
;
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
,
229 bool SectionIsLoadable(const lldb_private::Section
*section
);
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.
235 struct MachOCorefileImageEntry
{
236 std::string filename
;
237 lldb_private::UUID uuid
;
238 lldb::addr_t load_address
= LLDB_INVALID_ADDRESS
;
239 lldb::addr_t slide
= 0;
240 bool currently_executing
= false;
241 std::vector
<std::tuple
<lldb_private::ConstString
, lldb::addr_t
>>
242 segment_load_addresses
;
246 LCNoteEntry(uint32_t addr_byte_size
, lldb::ByteOrder byte_order
)
247 : payload(lldb_private::Stream::eBinary
, addr_byte_size
, byte_order
) {}
250 lldb::addr_t payload_file_offset
= 0;
251 lldb_private::StreamString payload
;
254 struct MachOCorefileAllImageInfos
{
255 std::vector
<MachOCorefileImageEntry
> all_image_infos
;
256 bool IsValid() { return all_image_infos
.size() > 0; }
259 /// Get the list of binary images that were present in the process
260 /// when the corefile was produced.
262 /// The MachOCorefileAllImageInfos object returned will have
263 /// IsValid() == false if the information is unavailable.
264 MachOCorefileAllImageInfos
GetCorefileAllImageInfos();
266 llvm::MachO::mach_header m_header
;
267 static lldb_private::ConstString
GetSegmentNameTEXT();
268 static lldb_private::ConstString
GetSegmentNameDATA();
269 static lldb_private::ConstString
GetSegmentNameDATA_DIRTY();
270 static lldb_private::ConstString
GetSegmentNameDATA_CONST();
271 static lldb_private::ConstString
GetSegmentNameOBJC();
272 static lldb_private::ConstString
GetSegmentNameLINKEDIT();
273 static lldb_private::ConstString
GetSegmentNameDWARF();
274 static lldb_private::ConstString
GetSectionNameEHFrame();
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
;
280 typedef lldb_private::RangeVector
<uint32_t, uint32_t> FileRangeArray
;
281 lldb_private::Address m_entry_point_address
;
282 FileRangeArray m_thread_context_offsets
;
283 lldb::offset_t m_linkedit_original_offset
= 0;
284 lldb::addr_t m_text_address
= LLDB_INVALID_ADDRESS
;
285 bool m_thread_context_offsets_valid
;
286 lldb_private::FileSpecList m_reexported_dylibs
;
287 bool m_allow_assembly_emulation_unwind_plans
;
290 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H