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/Core/FileSpecList.h" 14 #include "lldb/Host/SafeMachO.h" 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "lldb/Utility/FileSpec.h" 17 #include "lldb/Utility/RangeMap.h" 18 #include "lldb/Utility/UUID.h" 19 20 // This class needs to be hidden as eventually belongs in a plugin that 21 // will export the ObjectFile protocol 22 class ObjectFileMachO : public lldb_private::ObjectFile { 23 public: 24 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 25 lldb::offset_t data_offset, 26 const lldb_private::FileSpec *file, lldb::offset_t offset, 27 lldb::offset_t length); 28 29 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 30 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 31 32 ~ObjectFileMachO() override = default; 33 34 // Static Functions 35 static void Initialize(); 36 37 static void Terminate(); 38 39 static lldb_private::ConstString GetPluginNameStatic(); 40 41 static const char *GetPluginDescriptionStatic(); 42 43 static lldb_private::ObjectFile * 44 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 45 lldb::offset_t data_offset, const lldb_private::FileSpec *file, 46 lldb::offset_t file_offset, lldb::offset_t length); 47 48 static lldb_private::ObjectFile *CreateMemoryInstance( 49 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 50 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 51 52 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, 53 lldb::DataBufferSP &data_sp, 54 lldb::offset_t data_offset, 55 lldb::offset_t file_offset, 56 lldb::offset_t length, 57 lldb_private::ModuleSpecList &specs); 58 59 static bool SaveCore(const lldb::ProcessSP &process_sp, 60 const lldb_private::FileSpec &outfile, 61 lldb::SaveCoreStyle &core_style, 62 lldb_private::Status &error); 63 64 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset, 65 lldb::addr_t length); 66 67 // LLVM RTTI support 68 static char ID; 69 bool isA(const void *ClassID) const override { 70 return ClassID == &ID || ObjectFile::isA(ClassID); 71 } 72 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); } 73 74 // Member Functions 75 bool ParseHeader() override; 76 77 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, 78 bool value_is_offset) override; 79 80 lldb::ByteOrder GetByteOrder() const override; 81 82 bool IsExecutable() const override; 83 84 bool IsDynamicLoader() const; 85 86 uint32_t GetAddressByteSize() const override; 87 88 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override; 89 90 lldb_private::Symtab *GetSymtab() override; 91 92 bool IsStripped() override; 93 94 void CreateSections(lldb_private::SectionList &unified_section_list) override; 95 96 void Dump(lldb_private::Stream *s) override; 97 98 lldb_private::ArchSpec GetArchitecture() override; 99 100 lldb_private::UUID GetUUID() override; 101 102 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; 103 104 lldb_private::FileSpecList GetReExportedLibraries() override { 105 return m_reexported_dylibs; 106 } 107 108 lldb_private::Address GetEntryPointAddress() override; 109 110 lldb_private::Address GetBaseAddress() override; 111 112 uint32_t GetNumThreadContexts() override; 113 114 std::string GetIdentifierString() override; 115 116 bool GetCorefileMainBinaryInfo(lldb::addr_t &address, 117 lldb_private::UUID &uuid, 118 ObjectFile::BinaryType &type) override; 119 120 bool LoadCoreFileImages(lldb_private::Process &process) override; 121 122 lldb::RegisterContextSP 123 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override; 124 125 ObjectFile::Type CalculateType() override; 126 127 ObjectFile::Strata CalculateStrata() override; 128 129 llvm::VersionTuple GetVersion() override; 130 131 llvm::VersionTuple GetMinimumOSVersion() override; 132 133 llvm::VersionTuple GetSDKVersion() override; 134 135 bool GetIsDynamicLinkEditor() override; 136 137 static bool ParseHeader(lldb_private::DataExtractor &data, 138 lldb::offset_t *data_offset_ptr, 139 llvm::MachO::mach_header &header); 140 141 bool AllowAssemblyEmulationUnwindPlans() override; 142 143 // PluginInterface protocol 144 lldb_private::ConstString GetPluginName() override; 145 146 uint32_t GetPluginVersion() override; 147 148 protected: 149 static lldb_private::UUID 150 GetUUID(const llvm::MachO::mach_header &header, 151 const lldb_private::DataExtractor &data, 152 lldb::offset_t lc_offset); // Offset to the first load command 153 154 static lldb_private::ArchSpec GetArchitecture( 155 lldb::ModuleSP module_sp, const llvm::MachO::mach_header &header, 156 const lldb_private::DataExtractor &data, lldb::offset_t lc_offset); 157 158 /// Enumerate all ArchSpecs supported by this Mach-O file. 159 /// 160 /// On macOS one Mach-O slice can contain multiple load commands: 161 /// One load command for being loaded into a macOS process and one 162 /// load command for being loaded into a macCatalyst process. In 163 /// contrast to ObjectContainerUniversalMachO, this is the same 164 /// binary that can be loaded into different contexts. 165 static void GetAllArchSpecs(const llvm::MachO::mach_header &header, 166 const lldb_private::DataExtractor &data, 167 lldb::offset_t lc_offset, 168 lldb_private::ModuleSpec &base_spec, 169 lldb_private::ModuleSpecList &all_specs); 170 171 /// Intended for same-host arm device debugging where lldb needs to 172 /// detect libraries in the shared cache and augment the nlist entries 173 /// with an on-disk dyld_shared_cache file. The process will record 174 /// the shared cache UUID so the on-disk cache can be matched or rejected 175 /// correctly. 176 void GetProcessSharedCacheUUID(lldb_private::Process *, 177 lldb::addr_t &base_addr, 178 lldb_private::UUID &uuid); 179 180 /// Intended for same-host arm device debugging where lldb will read 181 /// shared cache libraries out of its own memory instead of the remote 182 /// process' memory as an optimization. If lldb's shared cache UUID 183 /// does not match the process' shared cache UUID, this optimization 184 /// should not be used. 185 void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid); 186 187 lldb_private::Section *GetMachHeaderSection(); 188 189 lldb::addr_t CalculateSectionLoadAddressForMemoryImage( 190 lldb::addr_t mach_header_load_address, 191 const lldb_private::Section *mach_header_section, 192 const lldb_private::Section *section); 193 194 lldb_private::UUID 195 GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache, 196 const lldb::ByteOrder byte_order, 197 const uint32_t addr_byte_size); 198 199 size_t ParseSymtab(); 200 201 typedef lldb_private::RangeVector<uint32_t, uint32_t, 8> EncryptedFileRanges; 202 EncryptedFileRanges GetEncryptedFileRanges(); 203 204 struct SegmentParsingContext; 205 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd, 206 lldb::offset_t offset); 207 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd, 208 lldb::offset_t offset, uint32_t cmd_idx, 209 SegmentParsingContext &context); 210 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd, 211 uint32_t cmd_idx); 212 213 bool SectionIsLoadable(const lldb_private::Section *section); 214 215 /// A corefile may include metadata about all of the binaries that were 216 /// present in the process when the corefile was taken. This is only 217 /// implemented for Mach-O files for now; we'll generalize it when we 218 /// have other systems that can include the same. 219 struct MachOCorefileImageEntry { 220 std::string filename; 221 lldb_private::UUID uuid; 222 lldb::addr_t load_address = LLDB_INVALID_ADDRESS; 223 bool currently_executing; 224 std::vector<std::tuple<lldb_private::ConstString, lldb::addr_t>> 225 segment_load_addresses; 226 }; 227 228 struct MachOCorefileAllImageInfos { 229 std::vector<MachOCorefileImageEntry> all_image_infos; 230 bool IsValid() { return all_image_infos.size() > 0; } 231 }; 232 233 /// Get the list of binary images that were present in the process 234 /// when the corefile was produced. 235 /// \return 236 /// The MachOCorefileAllImageInfos object returned will have 237 /// IsValid() == false if the information is unavailable. 238 MachOCorefileAllImageInfos GetCorefileAllImageInfos(); 239 240 llvm::MachO::mach_header m_header; 241 static lldb_private::ConstString GetSegmentNameTEXT(); 242 static lldb_private::ConstString GetSegmentNameDATA(); 243 static lldb_private::ConstString GetSegmentNameDATA_DIRTY(); 244 static lldb_private::ConstString GetSegmentNameDATA_CONST(); 245 static lldb_private::ConstString GetSegmentNameOBJC(); 246 static lldb_private::ConstString GetSegmentNameLINKEDIT(); 247 static lldb_private::ConstString GetSegmentNameDWARF(); 248 static lldb_private::ConstString GetSectionNameEHFrame(); 249 250 llvm::MachO::dysymtab_command m_dysymtab; 251 std::vector<llvm::MachO::segment_command_64> m_mach_segments; 252 std::vector<llvm::MachO::section_64> m_mach_sections; 253 llvm::Optional<llvm::VersionTuple> m_min_os_version; 254 llvm::Optional<llvm::VersionTuple> m_sdk_versions; 255 typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray; 256 lldb_private::Address m_entry_point_address; 257 FileRangeArray m_thread_context_offsets; 258 lldb::offset_t m_linkedit_original_offset; 259 lldb::addr_t m_text_address; 260 bool m_thread_context_offsets_valid; 261 lldb_private::FileSpecList m_reexported_dylibs; 262 bool m_allow_assembly_emulation_unwind_plans; 263 }; 264 265 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_MACH_O_OBJECTFILEMACHO_H 266