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