Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjectFileMachO.h ---------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_ObjectFileMachO_h_ |
| 10 | #define liblldb_ObjectFileMachO_h_ |
| 11 | |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Address.h" |
Jim Ingham | fbe0b9a | 2014-05-21 03:58:03 +0000 | [diff] [blame] | 13 | #include "lldb/Core/FileSpecList.h" |
Pavel Labath | 7704473 | 2018-09-12 12:26:05 +0000 | [diff] [blame] | 14 | #include "lldb/Host/SafeMachO.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/FileSpec.h" |
Pavel Labath | b809331 | 2019-03-06 14:41:43 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/RangeMap.h" |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/UUID.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | // This class needs to be hidden as eventually belongs in a plugin that |
| 21 | // will export the ObjectFile protocol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | class ObjectFileMachO : public lldb_private::ObjectFile { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, |
| 30 | const lldb::ProcessSP &process_sp, lldb::addr_t header_addr); |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | ~ObjectFileMachO() override = default; |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | // Static Functions |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | static void Initialize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | static void Terminate(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | static lldb_private::ConstString GetPluginNameStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | static const char *GetPluginDescriptionStatic(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | static bool SaveCore(const lldb::ProcessSP &process_sp, |
| 60 | const lldb_private::FileSpec &outfile, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 61 | lldb_private::Status &error); |
Greg Clayton | a2715cf | 2014-06-13 00:54:12 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset, |
| 64 | lldb::addr_t length); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | |
Pavel Labath | e84f784 | 2019-07-31 11:57:34 +0000 | [diff] [blame^] | 66 | // LLVM RTTI support |
| 67 | static char ID; |
| 68 | bool isA(const void *ClassID) const override { |
| 69 | return ClassID == &ID || ObjectFile::isA(ClassID); |
| 70 | } |
| 71 | static bool classof(const ObjectFile *obj) { return obj->isA(&ID); } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | // Member Functions |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | bool ParseHeader() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, |
| 77 | bool value_is_offset) override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | lldb::ByteOrder GetByteOrder() const override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | bool IsExecutable() const override; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 82 | |
Jason Molenda | 956761a | 2019-07-18 20:55:24 +0000 | [diff] [blame] | 83 | bool IsDynamicLoader() const; |
| 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | uint32_t GetAddressByteSize() const override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | |
Tatyana Krasnukha | 9e1a117 | 2018-06-27 06:50:10 +0000 | [diff] [blame] | 87 | lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | lldb_private::Symtab *GetSymtab() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | bool IsStripped() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | void CreateSections(lldb_private::SectionList &unified_section_list) override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | void Dump(lldb_private::Stream *s) override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame] | 97 | lldb_private::ArchSpec GetArchitecture() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
Pavel Labath | bd334ef | 2019-02-11 16:14:02 +0000 | [diff] [blame] | 99 | lldb_private::UUID GetUUID() override; |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | uint32_t GetDependentModules(lldb_private::FileSpecList &files) override; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | lldb_private::FileSpecList GetReExportedLibraries() override { |
| 104 | return m_reexported_dylibs; |
| 105 | } |
Greg Clayton | c2ff931 | 2012-02-22 19:41:02 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | lldb_private::Address GetEntryPointAddress() override; |
Greg Clayton | 08928f3 | 2015-02-05 02:01:34 +0000 | [diff] [blame] | 108 | |
Pavel Labath | d1e3fe2 | 2018-12-11 15:21:15 +0000 | [diff] [blame] | 109 | lldb_private::Address GetBaseAddress() override; |
Greg Clayton | 08928f3 | 2015-02-05 02:01:34 +0000 | [diff] [blame] | 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | uint32_t GetNumThreadContexts() override; |
Jason Molenda | 955dcf2 | 2016-05-04 03:09:40 +0000 | [diff] [blame] | 112 | |
Jason Molenda | 3533cec | 2017-04-06 01:50:11 +0000 | [diff] [blame] | 113 | std::string GetIdentifierString() override; |
| 114 | |
Jason Molenda | 9b7fcdc | 2017-04-12 23:33:30 +0000 | [diff] [blame] | 115 | bool GetCorefileMainBinaryInfo (lldb::addr_t &address, lldb_private::UUID &uuid) override; |
| 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | lldb::RegisterContextSP |
| 118 | GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override; |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | ObjectFile::Type CalculateType() override; |
| 121 | |
| 122 | ObjectFile::Strata CalculateStrata() override; |
| 123 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 124 | llvm::VersionTuple GetVersion() override; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 126 | llvm::VersionTuple GetMinimumOSVersion() override; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | |
Raphael Isemann | e0afcd8 | 2019-07-03 22:21:10 +0000 | [diff] [blame] | 128 | llvm::VersionTuple GetSDKVersion() override; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | |
| 130 | bool GetIsDynamicLinkEditor() override; |
| 131 | |
| 132 | static bool ParseHeader(lldb_private::DataExtractor &data, |
| 133 | lldb::offset_t *data_offset_ptr, |
| 134 | llvm::MachO::mach_header &header); |
| 135 | |
| 136 | bool AllowAssemblyEmulationUnwindPlans() override; |
| 137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | // PluginInterface protocol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | lldb_private::ConstString GetPluginName() override; |
| 140 | |
| 141 | uint32_t GetPluginVersion() override; |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 142 | |
Greg Clayton | 9cbd3c6 | 2014-03-07 19:24:39 +0000 | [diff] [blame] | 143 | protected: |
Pavel Labath | bd334ef | 2019-02-11 16:14:02 +0000 | [diff] [blame] | 144 | static lldb_private::UUID |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | GetUUID(const llvm::MachO::mach_header &header, |
| 146 | const lldb_private::DataExtractor &data, |
Pavel Labath | bd334ef | 2019-02-11 16:14:02 +0000 | [diff] [blame] | 147 | lldb::offset_t lc_offset); // Offset to the first load command |
Greg Clayton | 7ab7f89 | 2014-05-29 21:33:45 +0000 | [diff] [blame] | 148 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame] | 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); |
Jason Molenda | 0e0954c | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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. |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 159 | void GetProcessSharedCacheUUID(lldb_private::Process *, lldb::addr_t &base_addr, lldb_private::UUID &uuid); |
Jason Molenda | 0e0954c | 2013-04-16 06:24:42 +0000 | [diff] [blame] | 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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. |
Jason Molenda | 07580ff | 2018-05-04 00:59:37 +0000 | [diff] [blame] | 166 | void GetLLDBSharedCacheUUID(lldb::addr_t &base_addir, lldb_private::UUID &uuid); |
Greg Clayton | 0734737 | 2015-06-08 21:53:11 +0000 | [diff] [blame] | 167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | lldb_private::Section *GetMachHeaderSection(); |
Greg Clayton | 0734737 | 2015-06-08 21:53:11 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Jason Molenda | 649a607 | 2015-11-10 05:21:54 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | size_t ParseSymtab(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | |
Pavel Labath | b417eea | 2018-03-06 13:53:26 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 57577c0 | 2018-12-21 17:04:18 +0000 | [diff] [blame] | 194 | bool SectionIsLoadable(const lldb_private::Section *section); |
| 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | llvm::MachO::mach_header m_header; |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 197 | static lldb_private::ConstString GetSegmentNameTEXT(); |
| 198 | static lldb_private::ConstString GetSegmentNameDATA(); |
| 199 | static lldb_private::ConstString GetSegmentNameDATA_DIRTY(); |
| 200 | static lldb_private::ConstString GetSegmentNameDATA_CONST(); |
| 201 | static lldb_private::ConstString GetSegmentNameOBJC(); |
| 202 | static lldb_private::ConstString GetSegmentNameLINKEDIT(); |
| 203 | static lldb_private::ConstString GetSegmentNameDWARF(); |
| 204 | static lldb_private::ConstString GetSectionNameEHFrame(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 209 | llvm::Optional<llvm::VersionTuple> m_min_os_version; |
Raphael Isemann | e0afcd8 | 2019-07-03 22:21:10 +0000 | [diff] [blame] | 210 | llvm::Optional<llvm::VersionTuple> m_sdk_versions; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Eugene Zelenko | 8157a88 | 2015-10-23 16:56:07 +0000 | [diff] [blame] | 219 | #endif // liblldb_ObjectFileMachO_h_ |