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"
14#include "lldb/Symbol/ObjectFile.h"
15#include "lldb/Symbol/SaveCoreOptions.h"
16#include "lldb/Utility/FileSpec.h"
17#include "lldb/Utility/FileSpecList.h"
18#include "lldb/Utility/RangeMap.h"
19#include "lldb/Utility/StreamString.h"
20#include "lldb/Utility/UUID.h"
21#include <optional>
22
23// This class needs to be hidden as eventually belongs in a plugin that
24// will export the ObjectFile protocol
25class ObjectFileMachO : public lldb_private::ObjectFile {
26public:
27 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
28 lldb::offset_t data_offset,
29 const lldb_private::FileSpec *file, lldb::offset_t offset,
30 lldb::offset_t length);
31
32 ObjectFileMachO(const lldb::ModuleSP &module_sp,
33 lldb::WritableDataBufferSP data_sp,
34 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
35
36 ~ObjectFileMachO() override = default;
37
38 // Static Functions
39 static void Initialize();
40
41 static void Terminate();
42
43 static llvm::StringRef GetPluginNameStatic() { return "mach-o"; }
44
45 static llvm::StringRef GetPluginDescriptionStatic() {
46 return "Mach-o object file reader (32 and 64 bit)";
47 }
48
49 static lldb_private::ObjectFile *
50 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
51 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
52 lldb::offset_t file_offset, lldb::offset_t length);
53
54 static lldb_private::ObjectFile *CreateMemoryInstance(
55 const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
56 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
57
58 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
59 lldb::DataBufferSP &data_sp,
60 lldb::offset_t data_offset,
61 lldb::offset_t file_offset,
62 lldb::offset_t length,
63 lldb_private::ModuleSpecList &specs);
64
65 static bool SaveCore(const lldb::ProcessSP &process_sp,
66 lldb_private::SaveCoreOptions &options,
67 lldb_private::Status &error);
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(ClassID: &ID); }
78
79 // Member Functions
80 bool ParseHeader() override;
81
82 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
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
97 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
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
107 lldb_private::ArchSpec GetArchitecture() override;
108
109 lldb_private::UUID GetUUID() override;
110
111 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
112
113 lldb_private::FileSpecList GetReExportedLibraries() override {
114 return m_reexported_dylibs;
115 }
116
117 lldb_private::Address GetEntryPointAddress() override;
118
119 lldb_private::Address GetBaseAddress() override;
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
128 lldb_private::AddressableBits GetAddressableBits() override;
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 lldb_private::StructuredData::ObjectSP GetCorefileProcessMetadata() override;
137
138 bool LoadCoreFileImages(lldb_private::Process &process) override;
139
140 lldb::RegisterContextSP
141 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
142
143 ObjectFile::Type CalculateType() override;
144
145 ObjectFile::Strata CalculateStrata() override;
146
147 llvm::VersionTuple GetVersion() override;
148
149 llvm::VersionTuple GetMinimumOSVersion() override;
150
151 llvm::VersionTuple GetSDKVersion() override;
152
153 bool GetIsDynamicLinkEditor() override;
154
155 bool CanTrustAddressRanges() override;
156
157 static bool ParseHeader(lldb_private::DataExtractor &data,
158 lldb::offset_t *data_offset_ptr,
159 llvm::MachO::mach_header &header);
160
161 bool AllowAssemblyEmulationUnwindPlans() override;
162
163 lldb_private::Section *GetMachHeaderSection();
164
165 // PluginInterface protocol
166 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
167
168protected:
169 static lldb_private::UUID
170 GetUUID(const llvm::MachO::mach_header &header,
171 const lldb_private::DataExtractor &data,
172 lldb::offset_t lc_offset); // Offset to the first load command
173
174 static lldb_private::ArchSpec GetArchitecture(
175 lldb::ModuleSP module_sp, const llvm::MachO::mach_header &header,
176 const lldb_private::DataExtractor &data, lldb::offset_t lc_offset);
177
178 /// Enumerate all ArchSpecs supported by this Mach-O file.
179 ///
180 /// On macOS one Mach-O slice can contain multiple load commands:
181 /// One load command for being loaded into a macOS process and one
182 /// load command for being loaded into a macCatalyst process. In
183 /// contrast to ObjectContainerUniversalMachO, this is the same
184 /// binary that can be loaded into different contexts.
185 static void GetAllArchSpecs(const llvm::MachO::mach_header &header,
186 const lldb_private::DataExtractor &data,
187 lldb::offset_t lc_offset,
188 lldb_private::ModuleSpec &base_spec,
189 lldb_private::ModuleSpecList &all_specs);
190
191 /// Intended for same-host arm device debugging where lldb needs to
192 /// detect libraries in the shared cache and augment the nlist entries
193 /// with an on-disk dyld_shared_cache file. The process will record
194 /// the shared cache UUID so the on-disk cache can be matched or rejected
195 /// correctly.
196 void GetProcessSharedCacheUUID(lldb_private::Process *,
197 lldb::addr_t &base_addr,
198 lldb_private::UUID &uuid);
199
200 /// Intended for same-host arm device debugging where lldb will read
201 /// shared cache libraries out of its own memory instead of the remote
202 /// process' memory as an optimization. If lldb's shared cache UUID
203 /// does not match the process' shared cache UUID, this optimization
204 /// should not be used.
205 void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid);
206
207 lldb::addr_t CalculateSectionLoadAddressForMemoryImage(
208 lldb::addr_t mach_header_load_address,
209 const lldb_private::Section *mach_header_section,
210 const lldb_private::Section *section);
211
212 lldb_private::UUID
213 GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache,
214 const lldb::ByteOrder byte_order,
215 const uint32_t addr_byte_size);
216
217 size_t ParseSymtab();
218
219 typedef lldb_private::RangeVector<uint32_t, uint32_t, 8> EncryptedFileRanges;
220 EncryptedFileRanges GetEncryptedFileRanges();
221
222 struct SegmentParsingContext;
223 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd,
224 lldb::offset_t offset);
225 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd,
226 lldb::offset_t offset, uint32_t cmd_idx,
227 SegmentParsingContext &context);
228 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd,
229 uint32_t cmd_idx);
230
231 bool SectionIsLoadable(const lldb_private::Section *section);
232
233 /// A corefile may include metadata about all of the binaries that were
234 /// present in the process when the corefile was taken. This is only
235 /// implemented for Mach-O files for now; we'll generalize it when we
236 /// have other systems that can include the same.
237 struct MachOCorefileImageEntry {
238 std::string filename;
239 lldb_private::UUID uuid;
240 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
241 lldb::addr_t slide = 0;
242 bool currently_executing = false;
243 std::vector<std::tuple<lldb_private::ConstString, lldb::addr_t>>
244 segment_load_addresses;
245 };
246
247 struct LCNoteEntry {
248 LCNoteEntry(uint32_t addr_byte_size, lldb::ByteOrder byte_order)
249 : payload(lldb_private::Stream::eBinary, addr_byte_size, byte_order) {}
250
251 std::string name;
252 lldb::addr_t payload_file_offset = 0;
253 lldb_private::StreamString payload;
254 };
255
256 struct MachOCorefileAllImageInfos {
257 std::vector<MachOCorefileImageEntry> all_image_infos;
258 bool IsValid() { return all_image_infos.size() > 0; }
259 };
260
261 // The LC_SYMTAB's symtab_command structure uses 32-bit file offsets
262 // for two fields, but ObjectFileMachO needs to calculate the offsets
263 // in virtual address layout from the start of the TEXT segment, and
264 // that span may be larger than 4GB.
265 struct SymtabCommandLargeOffsets {
266 uint32_t cmd = 0; /* LC_SYMTAB */
267 uint32_t cmdsize = 0; /* sizeof(struct symtab_command) */
268 lldb::offset_t symoff = 0; /* symbol table offset */
269 uint32_t nsyms = 0; /* number of symbol table entries */
270 lldb::offset_t stroff = 0; /* string table offset */
271 uint32_t strsize = 0; /* string table size in bytes */
272 };
273
274 /// Get the list of binary images that were present in the process
275 /// when the corefile was produced.
276 /// \return
277 /// The MachOCorefileAllImageInfos object returned will have
278 /// IsValid() == false if the information is unavailable.
279 MachOCorefileAllImageInfos GetCorefileAllImageInfos();
280
281 llvm::MachO::mach_header m_header;
282 static lldb_private::ConstString GetSegmentNameTEXT();
283 static lldb_private::ConstString GetSegmentNameDATA();
284 static lldb_private::ConstString GetSegmentNameDATA_DIRTY();
285 static lldb_private::ConstString GetSegmentNameDATA_CONST();
286 static lldb_private::ConstString GetSegmentNameOBJC();
287 static lldb_private::ConstString GetSegmentNameLINKEDIT();
288 static lldb_private::ConstString GetSegmentNameDWARF();
289 static lldb_private::ConstString GetSegmentNameLLVM_COV();
290 static lldb_private::ConstString GetSectionNameEHFrame();
291 static lldb_private::ConstString GetSectionNameLLDBNoNlist();
292
293 llvm::MachO::dysymtab_command m_dysymtab;
294 std::vector<llvm::MachO::section_64> m_mach_sections;
295 std::optional<llvm::VersionTuple> m_min_os_version;
296 std::optional<llvm::VersionTuple> m_sdk_versions;
297 typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
298 lldb_private::Address m_entry_point_address;
299 FileRangeArray m_thread_context_offsets;
300 lldb::offset_t m_linkedit_original_offset = 0;
301 lldb::addr_t m_text_address = LLDB_INVALID_ADDRESS;
302 bool m_thread_context_offsets_valid;
303 lldb_private::FileSpecList m_reexported_dylibs;
304 bool m_allow_assembly_emulation_unwind_plans;
305};
306
307#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H
308

source code of lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h