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 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project 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"
24 
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 {
30 public:
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);
35 
36   ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
37                   const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
38 
39   ~ObjectFileMachO() override = default;
40 
41   //------------------------------------------------------------------
42   // Static Functions
43   //------------------------------------------------------------------
44   static void Initialize();
45 
46   static void Terminate();
47 
48   static lldb_private::ConstString GetPluginNameStatic();
49 
50   static const char *GetPluginDescriptionStatic();
51 
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);
56 
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);
60 
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);
67 
68   static bool SaveCore(const lldb::ProcessSP &process_sp,
69                        const lldb_private::FileSpec &outfile,
70                        lldb_private::Status &error);
71 
72   static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
73                               lldb::addr_t length);
74 
75   //------------------------------------------------------------------
76   // Member Functions
77   //------------------------------------------------------------------
78   bool ParseHeader() override;
79 
80   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
81                       bool value_is_offset) override;
82 
83   lldb::ByteOrder GetByteOrder() const override;
84 
85   bool IsExecutable() const override;
86 
87   uint32_t GetAddressByteSize() const override;
88 
89   lldb::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
90 
91   lldb_private::Symtab *GetSymtab() override;
92 
93   bool IsStripped() override;
94 
95   void CreateSections(lldb_private::SectionList &unified_section_list) override;
96 
97   void Dump(lldb_private::Stream *s) override;
98 
99   bool GetArchitecture(lldb_private::ArchSpec &arch) override;
100 
101   bool GetUUID(lldb_private::UUID *uuid) override;
102 
103   uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
104 
105   lldb_private::FileSpecList GetReExportedLibraries() override {
106     return m_reexported_dylibs;
107   }
108 
109   lldb_private::Address GetEntryPointAddress() override;
110 
111   lldb_private::Address GetHeaderAddress() override;
112 
113   uint32_t GetNumThreadContexts() override;
114 
115   std::string GetIdentifierString() override;
116 
117   bool GetCorefileMainBinaryInfo (lldb::addr_t &address, lldb_private::UUID &uuid) override;
118 
119   lldb::RegisterContextSP
120   GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) override;
121 
122   ObjectFile::Type CalculateType() override;
123 
124   ObjectFile::Strata CalculateStrata() override;
125 
126   uint32_t GetVersion(uint32_t *versions, uint32_t num_versions) override;
127 
128   uint32_t GetMinimumOSVersion(uint32_t *versions,
129                                uint32_t num_versions) override;
130 
131   uint32_t GetSDKVersion(uint32_t *versions, uint32_t num_versions) override;
132 
133   bool GetIsDynamicLinkEditor() override;
134 
135   static bool ParseHeader(lldb_private::DataExtractor &data,
136                           lldb::offset_t *data_offset_ptr,
137                           llvm::MachO::mach_header &header);
138 
139   bool AllowAssemblyEmulationUnwindPlans() override;
140 
141   //------------------------------------------------------------------
142   // PluginInterface protocol
143   //------------------------------------------------------------------
144   lldb_private::ConstString GetPluginName() override;
145 
146   uint32_t GetPluginVersion() override;
147 
148 protected:
149   static bool
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           lldb_private::UUID &uuid);
154 
155   static bool GetArchitecture(const llvm::MachO::mach_header &header,
156                               const lldb_private::DataExtractor &data,
157                               lldb::offset_t lc_offset,
158                               lldb_private::ArchSpec &arch);
159 
160   // Intended for same-host arm device debugging where lldb needs to
161   // detect libraries in the shared cache and augment the nlist entries
162   // with an on-disk dyld_shared_cache file.  The process will record
163   // the shared cache UUID so the on-disk cache can be matched or rejected
164   // correctly.
165   lldb_private::UUID GetProcessSharedCacheUUID(lldb_private::Process *);
166 
167   // Intended for same-host arm device debugging where lldb will read
168   // shared cache libraries out of its own memory instead of the remote
169   // process' memory as an optimization.  If lldb's shared cache UUID
170   // does not match the process' shared cache UUID, this optimization
171   // should not be used.
172   lldb_private::UUID GetLLDBSharedCacheUUID();
173 
174   lldb_private::Section *GetMachHeaderSection();
175 
176   lldb::addr_t CalculateSectionLoadAddressForMemoryImage(
177       lldb::addr_t mach_header_load_address,
178       const lldb_private::Section *mach_header_section,
179       const lldb_private::Section *section);
180 
181   lldb_private::UUID
182   GetSharedCacheUUID(lldb_private::FileSpec dyld_shared_cache,
183                      const lldb::ByteOrder byte_order,
184                      const uint32_t addr_byte_size);
185 
186   size_t ParseSymtab();
187 
188   llvm::MachO::mach_header m_header;
189   static const lldb_private::ConstString &GetSegmentNameTEXT();
190   static const lldb_private::ConstString &GetSegmentNameDATA();
191   static const lldb_private::ConstString &GetSegmentNameDATA_DIRTY();
192   static const lldb_private::ConstString &GetSegmentNameDATA_CONST();
193   static const lldb_private::ConstString &GetSegmentNameOBJC();
194   static const lldb_private::ConstString &GetSegmentNameLINKEDIT();
195   static const lldb_private::ConstString &GetSectionNameEHFrame();
196 
197   llvm::MachO::dysymtab_command m_dysymtab;
198   std::vector<llvm::MachO::segment_command_64> m_mach_segments;
199   std::vector<llvm::MachO::section_64> m_mach_sections;
200   std::vector<uint32_t> m_min_os_versions;
201   std::vector<uint32_t> m_sdk_versions;
202   typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
203   lldb_private::Address m_entry_point_address;
204   FileRangeArray m_thread_context_offsets;
205   bool m_thread_context_offsets_valid;
206   lldb_private::FileSpecList m_reexported_dylibs;
207   bool m_allow_assembly_emulation_unwind_plans;
208 };
209 
210 #endif // liblldb_ObjectFileMachO_h_
211