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