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/Host/FileSpec.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Utility/SafeMachO.h"
23 
24 //----------------------------------------------------------------------
25 // This class needs to be hidden as eventually belongs in a plugin that
26 // will export the ObjectFile protocol
27 //----------------------------------------------------------------------
28 class ObjectFileMachO : public lldb_private::ObjectFile {
29 public:
30   ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
31                   lldb::offset_t data_offset,
32                   const lldb_private::FileSpec *file, lldb::offset_t offset,
33                   lldb::offset_t length);
34 
35   ObjectFileMachO(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
36                   const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
37 
38   ~ObjectFileMachO() override = default;
39 
40   //------------------------------------------------------------------
41   // Static Functions
42   //------------------------------------------------------------------
43   static void Initialize();
44 
45   static void Terminate();
46 
47   static lldb_private::ConstString GetPluginNameStatic();
48 
49   static const char *GetPluginDescriptionStatic();
50 
51   static lldb_private::ObjectFile *
52   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
53                  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
54                  lldb::offset_t file_offset, lldb::offset_t length);
55 
56   static lldb_private::ObjectFile *CreateMemoryInstance(
57       const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
58       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
59 
60   static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
61                                         lldb::DataBufferSP &data_sp,
62                                         lldb::offset_t data_offset,
63                                         lldb::offset_t file_offset,
64                                         lldb::offset_t length,
65                                         lldb_private::ModuleSpecList &specs);
66 
67   static bool SaveCore(const lldb::ProcessSP &process_sp,
68                        const lldb_private::FileSpec &outfile,
69                        lldb_private::Error &error);
70 
71   static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
72                               lldb::addr_t length);
73 
74   //------------------------------------------------------------------
75   // Member Functions
76   //------------------------------------------------------------------
77   bool ParseHeader() override;
78 
79   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
80                       bool value_is_offset) override;
81 
82   lldb::ByteOrder GetByteOrder() const override;
83 
84   bool IsExecutable() const override;
85 
86   uint32_t GetAddressByteSize() const override;
87 
88   lldb::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   bool GetArchitecture(lldb_private::ArchSpec &arch) override;
99 
100   bool GetUUID(lldb_private::UUID *uuid) 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 GetHeaderAddress() override;
111 
112   uint32_t GetNumThreadContexts() 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   uint32_t GetVersion(uint32_t *versions, uint32_t num_versions) override;
122 
123   uint32_t GetMinimumOSVersion(uint32_t *versions,
124                                uint32_t num_versions) 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   lldb_private::UUID GetProcessSharedCacheUUID(lldb_private::Process *);
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   lldb_private::UUID GetLLDBSharedCacheUUID();
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   llvm::MachO::mach_header m_header;
184   static const lldb_private::ConstString &GetSegmentNameTEXT();
185   static const lldb_private::ConstString &GetSegmentNameDATA();
186   static const lldb_private::ConstString &GetSegmentNameDATA_DIRTY();
187   static const lldb_private::ConstString &GetSegmentNameDATA_CONST();
188   static const lldb_private::ConstString &GetSegmentNameOBJC();
189   static const lldb_private::ConstString &GetSegmentNameLINKEDIT();
190   static const lldb_private::ConstString &GetSectionNameEHFrame();
191 
192   llvm::MachO::dysymtab_command m_dysymtab;
193   std::vector<llvm::MachO::segment_command_64> m_mach_segments;
194   std::vector<llvm::MachO::section_64> m_mach_sections;
195   std::vector<uint32_t> m_min_os_versions;
196   std::vector<uint32_t> m_sdk_versions;
197   typedef lldb_private::RangeVector<uint32_t, uint32_t> FileRangeArray;
198   lldb_private::Address m_entry_point_address;
199   FileRangeArray m_thread_context_offsets;
200   bool m_thread_context_offsets_valid;
201   lldb_private::FileSpecList m_reexported_dylibs;
202   bool m_allow_assembly_emulation_unwind_plans;
203 };
204 
205 #endif // liblldb_ObjectFileMachO_h_
206