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