1 //===-- ObjectFileJIT.cpp ---------------------------------------*- 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 #include "llvm/ADT/StringRef.h"
11 
12 #include "ObjectFileJIT.h"
13 
14 #include "lldb/lldb-private-log.h"
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBuffer.h"
17 #include "lldb/Core/DataBufferHeap.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/FileSpecList.h"
20 #include "lldb/Core/Log.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/ModuleSpec.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/RangeMap.h"
25 #include "lldb/Core/Section.h"
26 #include "lldb/Core/StreamFile.h"
27 #include "lldb/Core/StreamString.h"
28 #include "lldb/Core/Timer.h"
29 #include "lldb/Core/UUID.h"
30 #include "lldb/Host/Host.h"
31 #include "lldb/Host/FileSpec.h"
32 #include "lldb/Symbol/ObjectFile.h"
33 #include "lldb/Target/Platform.h"
34 #include "lldb/Target/Process.h"
35 #include "lldb/Target/SectionLoadList.h"
36 #include "lldb/Target/Target.h"
37 
38 #ifndef __APPLE__
39 #include "Utility/UuidCompatibility.h"
40 #endif
41 
42 using namespace lldb;
43 using namespace lldb_private;
44 
45 
46 void
47 ObjectFileJIT::Initialize()
48 {
49     PluginManager::RegisterPlugin (GetPluginNameStatic(),
50                                    GetPluginDescriptionStatic(),
51                                    CreateInstance,
52                                    CreateMemoryInstance,
53                                    GetModuleSpecifications);
54 }
55 
56 void
57 ObjectFileJIT::Terminate()
58 {
59     PluginManager::UnregisterPlugin (CreateInstance);
60 }
61 
62 
63 lldb_private::ConstString
64 ObjectFileJIT::GetPluginNameStatic()
65 {
66     static ConstString g_name("jit");
67     return g_name;
68 }
69 
70 const char *
71 ObjectFileJIT::GetPluginDescriptionStatic()
72 {
73     return "JIT code object file";
74 }
75 
76 ObjectFile *
77 ObjectFileJIT::CreateInstance (const lldb::ModuleSP &module_sp,
78                                  DataBufferSP& data_sp,
79                                  lldb::offset_t data_offset,
80                                  const FileSpec* file,
81                                  lldb::offset_t file_offset,
82                                  lldb::offset_t length)
83 {
84     // JIT'ed object file is backed by the ObjectFileJITDelegate, never
85     // read from a file
86     return NULL;
87 }
88 
89 ObjectFile *
90 ObjectFileJIT::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
91                                      DataBufferSP& data_sp,
92                                      const ProcessSP &process_sp,
93                                      lldb::addr_t header_addr)
94 {
95     // JIT'ed object file is backed by the ObjectFileJITDelegate, never
96     // read from memory
97     return NULL;
98 }
99 
100 size_t
101 ObjectFileJIT::GetModuleSpecifications (const lldb_private::FileSpec& file,
102                                         lldb::DataBufferSP& data_sp,
103                                         lldb::offset_t data_offset,
104                                         lldb::offset_t file_offset,
105                                         lldb::offset_t length,
106                                         lldb_private::ModuleSpecList &specs)
107 {
108     // JIT'ed object file can't be read from a file on disk
109     return 0;
110 }
111 
112 ObjectFileJIT::ObjectFileJIT (const lldb::ModuleSP &module_sp,
113                               const ObjectFileJITDelegateSP &delegate_sp) :
114     ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0),
115     m_delegate_wp ()
116 {
117     if (delegate_sp)
118     {
119         m_delegate_wp = delegate_sp;
120         m_data.SetByteOrder(delegate_sp->GetByteOrder());
121         m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
122     }
123 }
124 
125 ObjectFileJIT::~ObjectFileJIT()
126 {
127 }
128 
129 
130 bool
131 ObjectFileJIT::ParseHeader ()
132 {
133     // JIT code is never in a file, nor is it required to have any header
134     return false;
135 }
136 
137 ByteOrder
138 ObjectFileJIT::GetByteOrder () const
139 {
140     return m_data.GetByteOrder();
141 }
142 
143 bool
144 ObjectFileJIT::IsExecutable() const
145 {
146     return false;
147 }
148 
149 uint32_t
150 ObjectFileJIT::GetAddressByteSize () const
151 {
152     return m_data.GetAddressByteSize();
153 }
154 
155 
156 Symtab *
157 ObjectFileJIT::GetSymtab()
158 {
159     ModuleSP module_sp(GetModule());
160     if (module_sp)
161     {
162         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
163         if (m_symtab_ap.get() == NULL)
164         {
165             m_symtab_ap.reset(new Symtab(this));
166             Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
167             ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
168             if (delegate_sp)
169                 delegate_sp->PopulateSymtab(this, *m_symtab_ap);
170             // TODO: get symbols from delegate
171             m_symtab_ap->Finalize ();
172         }
173     }
174     return m_symtab_ap.get();
175 }
176 
177 bool
178 ObjectFileJIT::IsStripped ()
179 {
180     return false; // JIT code that is in a module is never stripped
181 }
182 
183 void
184 ObjectFileJIT::CreateSections (SectionList &unified_section_list)
185 {
186     if (!m_sections_ap.get())
187     {
188         m_sections_ap.reset(new SectionList());
189         ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
190         if (delegate_sp)
191         {
192             delegate_sp->PopulateSectionList(this, *m_sections_ap);
193             unified_section_list = *m_sections_ap;
194         }
195     }
196 }
197 
198 void
199 ObjectFileJIT::Dump (Stream *s)
200 {
201     ModuleSP module_sp(GetModule());
202     if (module_sp)
203     {
204         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
205         s->Printf("%p: ", static_cast<void*>(this));
206         s->Indent();
207         s->PutCString("ObjectFileJIT");
208 
209         ArchSpec arch;
210         if (GetArchitecture(arch))
211             *s << ", arch = " << arch.GetArchitectureName();
212 
213         s->EOL();
214 
215         SectionList *sections = GetSectionList();
216         if (sections)
217             sections->Dump(s, NULL, true, UINT32_MAX);
218 
219         if (m_symtab_ap.get())
220             m_symtab_ap->Dump(s, NULL, eSortOrderNone);
221     }
222 }
223 
224 bool
225 ObjectFileJIT::GetUUID (lldb_private::UUID* uuid)
226 {
227     // TODO: maybe get from delegate, not needed for first pass
228     return false;
229 }
230 
231 
232 uint32_t
233 ObjectFileJIT::GetDependentModules (FileSpecList& files)
234 {
235     // JIT modules don't have dependencies, but they could
236     // if external functions are called and we know where they are
237     files.Clear();
238     return 0;
239 }
240 
241 lldb_private::Address
242 ObjectFileJIT::GetEntryPointAddress ()
243 {
244     return Address();
245 }
246 
247 lldb_private::Address
248 ObjectFileJIT::GetHeaderAddress ()
249 {
250     return Address();
251 }
252 
253 
254 
255 ObjectFile::Type
256 ObjectFileJIT::CalculateType()
257 {
258     return eTypeJIT;
259 }
260 
261 ObjectFile::Strata
262 ObjectFileJIT::CalculateStrata()
263 {
264     return eStrataJIT;
265 }
266 
267 
268 bool
269 ObjectFileJIT::GetArchitecture (ArchSpec &arch)
270 {
271     ObjectFileJITDelegateSP delegate_sp (m_delegate_wp.lock());
272     if (delegate_sp)
273         return delegate_sp->GetArchitecture(arch);
274     return false;
275 }
276 
277 //------------------------------------------------------------------
278 // PluginInterface protocol
279 //------------------------------------------------------------------
280 lldb_private::ConstString
281 ObjectFileJIT::GetPluginName()
282 {
283     return GetPluginNameStatic();
284 }
285 
286 uint32_t
287 ObjectFileJIT::GetPluginVersion()
288 {
289     return 1;
290 }
291 
292 
293 bool
294 ObjectFileJIT::SetLoadAddress (Target &target,
295                                lldb::addr_t value,
296                                bool value_is_offset)
297 {
298     size_t num_loaded_sections = 0;
299     SectionList *section_list = GetSectionList ();
300     if (section_list)
301     {
302         const size_t num_sections = section_list->GetSize();
303         // "value" is an offset to apply to each top level segment
304         for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
305         {
306             // Iterate through the object file sections to find all
307             // of the sections that size on disk (to avoid __PAGEZERO)
308             // and load them
309             SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
310             if (section_sp &&
311                 section_sp->GetFileSize() > 0 &&
312                 section_sp->IsThreadSpecific() == false)
313             {
314                 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
315                     ++num_loaded_sections;
316             }
317         }
318     }
319     return num_loaded_sections > 0;
320 }
321 
322 
323 size_t
324 ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
325                                 off_t section_offset,
326                                 void *dst,
327                                 size_t dst_len) const
328 {
329     lldb::offset_t file_size = section->GetFileSize();
330     if (section_offset < static_cast<off_t>(file_size))
331     {
332         uint64_t src_len = file_size - section_offset;
333         if (src_len > dst_len)
334             src_len = dst_len;
335         const uint8_t *src = ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
336 
337         memcpy (dst, src, src_len);
338         return src_len;
339     }
340     return 0;
341 }
342 size_t
343 ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
344                                 lldb_private::DataExtractor& section_data) const
345 {
346     if (section->GetFileSize())
347     {
348         const void *src = (void *)(uintptr_t)section->GetFileOffset();
349 
350         DataBufferSP data_sp (new lldb_private::DataBufferHeap(src, section->GetFileSize()));
351         if (data_sp)
352         {
353             section_data.SetData (data_sp, 0, data_sp->GetByteSize());
354             section_data.SetByteOrder (GetByteOrder());
355             section_data.SetAddressByteSize (GetAddressByteSize());
356             return section_data.GetByteSize();
357         }
358     }
359     section_data.Clear();
360     return 0;
361 }
362 
363