[memprof] Move YAML traits to MemProf.h (NFC) (#118668)
[llvm-project.git] / lldb / source / Plugins / ObjectFile / Mach-O / ObjectFileMachO.h
blob27b2078b5a3fcd8c8e8beaf8cfbcbf8c0a499414
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 //===----------------------------------------------------------------------===//
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/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>
23 // This class needs to be hidden as eventually belongs in a plugin that
24 // will export the ObjectFile protocol
25 class ObjectFileMachO : public lldb_private::ObjectFile {
26 public:
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);
32 ObjectFileMachO(const lldb::ModuleSP &module_sp,
33 lldb::WritableDataBufferSP data_sp,
34 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
36 ~ObjectFileMachO() override = default;
38 // Static Functions
39 static void Initialize();
41 static void Terminate();
43 static llvm::StringRef GetPluginNameStatic() { return "mach-o"; }
45 static llvm::StringRef GetPluginDescriptionStatic() {
46 return "Mach-o object file reader (32 and 64 bit)";
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);
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);
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);
65 static bool SaveCore(const lldb::ProcessSP &process_sp,
66 lldb_private::SaveCoreOptions &options,
67 lldb_private::Status &error);
69 static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset,
70 lldb::addr_t length);
72 // LLVM RTTI support
73 static char ID;
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); }
79 // Member Functions
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;
93 bool IsKext() 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(); }
166 protected:
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
193 /// correctly.
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);
210 lldb_private::UUID
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,
227 uint32_t cmd_idx);
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;
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) {}
249 std::string name;
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 // The LC_SYMTAB's symtab_command structure uses 32-bit file offsets
260 // for two fields, but ObjectFileMachO needs to calculate the offsets
261 // in virtual address layout from the start of the TEXT segment, and
262 // that span may be larger than 4GB.
263 struct SymtabCommandLargeOffsets {
264 uint32_t cmd = 0; /* LC_SYMTAB */
265 uint32_t cmdsize = 0; /* sizeof(struct symtab_command) */
266 lldb::offset_t symoff = 0; /* symbol table offset */
267 uint32_t nsyms = 0; /* number of symbol table entries */
268 lldb::offset_t stroff = 0; /* string table offset */
269 uint32_t strsize = 0; /* string table size in bytes */
272 /// Get the list of binary images that were present in the process
273 /// when the corefile was produced.
274 /// \return
275 /// The MachOCorefileAllImageInfos object returned will have
276 /// IsValid() == false if the information is unavailable.
277 MachOCorefileAllImageInfos GetCorefileAllImageInfos();
279 llvm::MachO::mach_header m_header;
280 static lldb_private::ConstString GetSegmentNameTEXT();
281 static lldb_private::ConstString GetSegmentNameDATA();
282 static lldb_private::ConstString GetSegmentNameDATA_DIRTY();
283 static lldb_private::ConstString GetSegmentNameDATA_CONST();
284 static lldb_private::ConstString GetSegmentNameOBJC();
285 static lldb_private::ConstString GetSegmentNameLINKEDIT();
286 static lldb_private::ConstString GetSegmentNameDWARF();
287 static lldb_private::ConstString GetSegmentNameLLVM_COV();
288 static lldb_private::ConstString GetSectionNameEHFrame();
290 llvm::MachO::dysymtab_command m_dysymtab;
291 std::vector<llvm::MachO::section_64> m_mach_sections;
292 std::optional<llvm::VersionTuple> m_min_os_version;
293 std::optional<llvm::VersionTuple> m_sdk_versions;
294 typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
295 lldb_private::Address m_entry_point_address;
296 FileRangeArray m_thread_context_offsets;
297 lldb::offset_t m_linkedit_original_offset = 0;
298 lldb::addr_t m_text_address = LLDB_INVALID_ADDRESS;
299 bool m_thread_context_offsets_valid;
300 lldb_private::FileSpecList m_reexported_dylibs;
301 bool m_allow_assembly_emulation_unwind_plans;
304 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H