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