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