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