1 //===-- ObjectFileMachO.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ObjectFileMachO_h_
11 #define liblldb_ObjectFileMachO_h_
15 // Other libraries and framework includes
17 #include "lldb/Core/Address.h"
18 #include "lldb/Core/FileSpecList.h"
19 #include "lldb/Core/RangeMap.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/SafeMachO.h"
23 #include "lldb/Utility/UUID.h"
25 //----------------------------------------------------------------------
26 // This class needs to be hidden as eventually belongs in a plugin that
27 // will export the ObjectFile protocol
28 //----------------------------------------------------------------------
29 class ObjectFileMachO : public lldb_private::ObjectFile {
31 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
32 lldb::offset_t data_offset,
33 const lldb_private::FileSpec *file, lldb::offset_t offset,
34 lldb::offset_t length);
36 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
37 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
39 ~ObjectFileMachO() override = default;
41 //------------------------------------------------------------------
43 //------------------------------------------------------------------
44 static void Initialize();
46 static void Terminate();
48 static lldb_private::ConstString GetPluginNameStatic();
50 static const char *GetPluginDescriptionStatic();
52 static lldb_private::ObjectFile *
53 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
54 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
55 lldb::offset_t file_offset, lldb::offset_t length);
57 static lldb_private::ObjectFile *CreateMemoryInstance(
58 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
59 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
61 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
62 lldb::DataBufferSP &data_sp,
63 lldb::offset_t data_offset,
64 lldb::offset_t file_offset,
65 lldb::offset_t length,
66 lldb_private::ModuleSpecList &specs);
68 static bool SaveCore(const lldb::ProcessSP &process_sp,
69 const lldb_private::FileSpec &outfile,
70 lldb_private::Status &error);
72 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
75 //------------------------------------------------------------------
77 //------------------------------------------------------------------
78 bool ParseHeader() override;
80 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
81 bool value_is_offset) override;
83 lldb::ByteOrder GetByteOrder() const override;
85 bool IsExecutable() const override;
87 uint32_t GetAddressByteSize() const override;
89 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
91 lldb_private::Symtab *GetSymtab() override;
93 bool IsStripped() override;
95 void CreateSections(lldb_private::SectionList &unified_section_list) override;
97 void Dump(lldb_private::Stream *s) override;
99 bool GetArchitecture(lldb_private::ArchSpec &arch) override;
101 bool GetUUID(lldb_private::UUID *uuid) override;
103 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
105 lldb_private::FileSpecList GetReExportedLibraries() override {
106 return m_reexported_dylibs;
109 lldb_private::Address GetEntryPointAddress() override;
111 lldb_private::Address GetHeaderAddress() override;
113 uint32_t GetNumThreadContexts() override;
115 std::string GetIdentifierString() override;
117 bool GetCorefileMainBinaryInfo (lldb::addr_t &address, lldb_private::UUID &uuid) override;
119 lldb::RegisterContextSP
120 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
122 ObjectFile::Type CalculateType() override;
124 ObjectFile::Strata CalculateStrata() override;
126 llvm::VersionTuple GetVersion() override;
128 llvm::VersionTuple GetMinimumOSVersion() override;
130 uint32_t GetSDKVersion(uint32_t *versions, uint32_t num_versions) override;
132 bool GetIsDynamicLinkEditor() override;
134 static bool ParseHeader(lldb_private::DataExtractor &data,
135 lldb::offset_t *data_offset_ptr,
136 llvm::MachO::mach_header &header);
138 bool AllowAssemblyEmulationUnwindPlans() override;
140 //------------------------------------------------------------------
141 // PluginInterface protocol
142 //------------------------------------------------------------------
143 lldb_private::ConstString GetPluginName() override;
145 uint32_t GetPluginVersion() override;
149 GetUUID(const llvm::MachO::mach_header &header,
150 const lldb_private::DataExtractor &data,
151 lldb::offset_t lc_offset, // Offset to the first load command
152 lldb_private::UUID &uuid);
154 static bool GetArchitecture(const llvm::MachO::mach_header &header,
155 const lldb_private::DataExtractor &data,
156 lldb::offset_t lc_offset,
157 lldb_private::ArchSpec &arch);
159 // Intended for same-host arm device debugging where lldb needs to
160 // detect libraries in the shared cache and augment the nlist entries
161 // with an on-disk dyld_shared_cache file. The process will record
162 // the shared cache UUID so the on-disk cache can be matched or rejected
164 void GetProcessSharedCacheUUID(lldb_private::Process *, lldb::addr_t &base_addr, lldb_private::UUID &uuid);
166 // Intended for same-host arm device debugging where lldb will read
167 // shared cache libraries out of its own memory instead of the remote
168 // process' memory as an optimization. If lldb's shared cache UUID
169 // does not match the process' shared cache UUID, this optimization
170 // should not be used.
171 void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid);
173 lldb_private::Section *GetMachHeaderSection();
175 lldb::addr_t CalculateSectionLoadAddressForMemoryImage(
176 lldb::addr_t mach_header_load_address,
177 const lldb_private::Section *mach_header_section,
178 const lldb_private::Section *section);
181 GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache,
182 const lldb::ByteOrder byte_order,
183 const uint32_t addr_byte_size);
185 size_t ParseSymtab();
187 typedef lldb_private::RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges;
188 EncryptedFileRanges GetEncryptedFileRanges();
190 struct SegmentParsingContext;
191 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd,
192 lldb::offset_t offset);
193 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd,
194 lldb::offset_t offset, uint32_t cmd_idx,
195 SegmentParsingContext &context);
196 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd,
199 llvm::MachO::mach_header m_header;
200 static const lldb_private::ConstString &GetSegmentNameTEXT();
201 static const lldb_private::ConstString &GetSegmentNameDATA();
202 static const lldb_private::ConstString &GetSegmentNameDATA_DIRTY();
203 static const lldb_private::ConstString &GetSegmentNameDATA_CONST();
204 static const lldb_private::ConstString &GetSegmentNameOBJC();
205 static const lldb_private::ConstString &GetSegmentNameLINKEDIT();
206 static const lldb_private::ConstString &GetSectionNameEHFrame();
208 llvm::MachO::dysymtab_command m_dysymtab;
209 std::vector<llvm::MachO::segment_command_64> m_mach_segments;
210 std::vector<llvm::MachO::section_64> m_mach_sections;
211 llvm::Optional<llvm::VersionTuple> m_min_os_version;
212 std::vector<uint32_t> m_sdk_versions;
213 typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
214 lldb_private::Address m_entry_point_address;
215 FileRangeArray m_thread_context_offsets;
216 bool m_thread_context_offsets_valid;
217 lldb_private::FileSpecList m_reexported_dylibs;
218 bool m_allow_assembly_emulation_unwind_plans;
221 #endif // liblldb_ObjectFileMachO_h_