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 liblldb_ObjectFileMachO_h_ 10 #define liblldb_ObjectFileMachO_h_ 11 12 #include "lldb/Core/Address.h" 13 #include "lldb/Core/FileSpecList.h" 14 #include "lldb/Core/RangeMap.h" 15 #include "lldb/Host/SafeMachO.h" 16 #include "lldb/Symbol/ObjectFile.h" 17 #include "lldb/Utility/FileSpec.h" 18 #include "lldb/Utility/UUID.h" 19 20 //---------------------------------------------------------------------- 21 // This class needs to be hidden as eventually belongs in a plugin that 22 // will export the ObjectFile protocol 23 //---------------------------------------------------------------------- 24 class ObjectFileMachO : public lldb_private::ObjectFile { 25 public: 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); 30 31 ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 32 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 33 34 ~ObjectFileMachO() override = default; 35 36 //------------------------------------------------------------------ 37 // Static Functions 38 //------------------------------------------------------------------ 39 static void Initialize(); 40 41 static void Terminate(); 42 43 static lldb_private::ConstString GetPluginNameStatic(); 44 45 static const char *GetPluginDescriptionStatic(); 46 47 static lldb_private::ObjectFile * 48 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 49 lldb::offset_t data_offset, const lldb_private::FileSpec *file, 50 lldb::offset_t file_offset, lldb::offset_t length); 51 52 static lldb_private::ObjectFile *CreateMemoryInstance( 53 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 54 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); 55 56 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, 57 lldb::DataBufferSP &data_sp, 58 lldb::offset_t data_offset, 59 lldb::offset_t file_offset, 60 lldb::offset_t length, 61 lldb_private::ModuleSpecList &specs); 62 63 static bool SaveCore(const lldb::ProcessSP &process_sp, 64 const lldb_private::FileSpec &outfile, 65 lldb_private::Status &error); 66 67 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset, 68 lldb::addr_t length); 69 70 //------------------------------------------------------------------ 71 // Member Functions 72 //------------------------------------------------------------------ 73 bool ParseHeader() override; 74 75 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, 76 bool value_is_offset) override; 77 78 lldb::ByteOrder GetByteOrder() const override; 79 80 bool IsExecutable() const override; 81 82 uint32_t GetAddressByteSize() const override; 83 84 lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override; 85 86 lldb_private::Symtab *GetSymtab() override; 87 88 bool IsStripped() override; 89 90 void CreateSections(lldb_private::SectionList &unified_section_list) override; 91 92 void Dump(lldb_private::Stream *s) override; 93 94 lldb_private::ArchSpec GetArchitecture() override; 95 96 bool GetUUID(lldb_private::UUID *uuid) override; 97 98 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; 99 100 lldb_private::FileSpecList GetReExportedLibraries() override { 101 return m_reexported_dylibs; 102 } 103 104 lldb_private::Address GetEntryPointAddress() override; 105 106 lldb_private::Address GetBaseAddress() override; 107 108 uint32_t GetNumThreadContexts() override; 109 110 std::string GetIdentifierString() override; 111 112 bool GetCorefileMainBinaryInfo (lldb::addr_t &address, lldb_private::UUID &uuid) override; 113 114 lldb::RegisterContextSP 115 GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override; 116 117 ObjectFile::Type CalculateType() override; 118 119 ObjectFile::Strata CalculateStrata() override; 120 121 llvm::VersionTuple GetVersion() override; 122 123 llvm::VersionTuple GetMinimumOSVersion() override; 124 125 uint32_t GetSDKVersion(uint32_t *versions, uint32_t num_versions) override; 126 127 bool GetIsDynamicLinkEditor() override; 128 129 static bool ParseHeader(lldb_private::DataExtractor &data, 130 lldb::offset_t *data_offset_ptr, 131 llvm::MachO::mach_header &header); 132 133 bool AllowAssemblyEmulationUnwindPlans() override; 134 135 //------------------------------------------------------------------ 136 // PluginInterface protocol 137 //------------------------------------------------------------------ 138 lldb_private::ConstString GetPluginName() override; 139 140 uint32_t GetPluginVersion() override; 141 142 protected: 143 static bool 144 GetUUID(const llvm::MachO::mach_header &header, 145 const lldb_private::DataExtractor &data, 146 lldb::offset_t lc_offset, // Offset to the first load command 147 lldb_private::UUID &uuid); 148 149 static lldb_private::ArchSpec 150 GetArchitecture(const llvm::MachO::mach_header &header, 151 const lldb_private::DataExtractor &data, 152 lldb::offset_t lc_offset); 153 154 // Intended for same-host arm device debugging where lldb needs to 155 // detect libraries in the shared cache and augment the nlist entries 156 // with an on-disk dyld_shared_cache file. The process will record 157 // the shared cache UUID so the on-disk cache can be matched or rejected 158 // correctly. 159 void GetProcessSharedCacheUUID(lldb_private::Process *, lldb::addr_t &base_addr, lldb_private::UUID &uuid); 160 161 // Intended for same-host arm device debugging where lldb will read 162 // shared cache libraries out of its own memory instead of the remote 163 // process' memory as an optimization. If lldb's shared cache UUID 164 // does not match the process' shared cache UUID, this optimization 165 // should not be used. 166 void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid); 167 168 lldb_private::Section *GetMachHeaderSection(); 169 170 lldb::addr_t CalculateSectionLoadAddressForMemoryImage( 171 lldb::addr_t mach_header_load_address, 172 const lldb_private::Section *mach_header_section, 173 const lldb_private::Section *section); 174 175 lldb_private::UUID 176 GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache, 177 const lldb::ByteOrder byte_order, 178 const uint32_t addr_byte_size); 179 180 size_t ParseSymtab(); 181 182 typedef lldb_private::RangeArray<uint32_t, uint32_t, 8> EncryptedFileRanges; 183 EncryptedFileRanges GetEncryptedFileRanges(); 184 185 struct SegmentParsingContext; 186 void ProcessDysymtabCommand(const llvm::MachO::load_command &load_cmd, 187 lldb::offset_t offset); 188 void ProcessSegmentCommand(const llvm::MachO::load_command &load_cmd, 189 lldb::offset_t offset, uint32_t cmd_idx, 190 SegmentParsingContext &context); 191 void SanitizeSegmentCommand(llvm::MachO::segment_command_64 &seg_cmd, 192 uint32_t cmd_idx); 193 194 bool SectionIsLoadable(const lldb_private::Section *section); 195 196 llvm::MachO::mach_header m_header; 197 static const lldb_private::ConstString &GetSegmentNameTEXT(); 198 static const lldb_private::ConstString &GetSegmentNameDATA(); 199 static const lldb_private::ConstString &GetSegmentNameDATA_DIRTY(); 200 static const lldb_private::ConstString &GetSegmentNameDATA_CONST(); 201 static const lldb_private::ConstString &GetSegmentNameOBJC(); 202 static const lldb_private::ConstString &GetSegmentNameLINKEDIT(); 203 static const lldb_private::ConstString &GetSegmentNameDWARF(); 204 static const lldb_private::ConstString &GetSectionNameEHFrame(); 205 206 llvm::MachO::dysymtab_command m_dysymtab; 207 std::vector<llvm::MachO::segment_command_64> m_mach_segments; 208 std::vector<llvm::MachO::section_64> m_mach_sections; 209 llvm::Optional<llvm::VersionTuple> m_min_os_version; 210 std::vector<uint32_t> m_sdk_versions; 211 typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray; 212 lldb_private::Address m_entry_point_address; 213 FileRangeArray m_thread_context_offsets; 214 bool m_thread_context_offsets_valid; 215 lldb_private::FileSpecList m_reexported_dylibs; 216 bool m_allow_assembly_emulation_unwind_plans; 217 }; 218 219 #endif // liblldb_ObjectFileMachO_h_ 220