blob: 530e11af22c179d9616cd7aad05e0f1b265f7569 [file] [log] [blame]
Greg Clayton23f8c952014-03-24 23:10:19 +00001//===-- 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
42using namespace lldb;
43using namespace lldb_private;
44
45
46void
47ObjectFileJIT::Initialize()
48{
49 PluginManager::RegisterPlugin (GetPluginNameStatic(),
50 GetPluginDescriptionStatic(),
51 CreateInstance,
52 CreateMemoryInstance,
53 GetModuleSpecifications);
54}
55
56void
57ObjectFileJIT::Terminate()
58{
59 PluginManager::UnregisterPlugin (CreateInstance);
60}
61
62
63lldb_private::ConstString
64ObjectFileJIT::GetPluginNameStatic()
65{
66 static ConstString g_name("jit");
67 return g_name;
68}
69
70const char *
71ObjectFileJIT::GetPluginDescriptionStatic()
72{
73 return "JIT code object file";
74}
75
76ObjectFile *
77ObjectFileJIT::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
89ObjectFile *
90ObjectFileJIT::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
100size_t
101ObjectFileJIT::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
112ObjectFileJIT::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
125ObjectFileJIT::~ObjectFileJIT()
126{
127}
128
129
130bool
131ObjectFileJIT::ParseHeader ()
132{
133 // JIT code is never in a file, nor is it required to have any header
134 return false;
135}
136
137ByteOrder
138ObjectFileJIT::GetByteOrder () const
139{
140 return m_data.GetByteOrder();
141}
142
143bool
144ObjectFileJIT::IsExecutable() const
145{
146 return false;
147}
148
149uint32_t
150ObjectFileJIT::GetAddressByteSize () const
151{
152 return m_data.GetAddressByteSize();
153}
154
155
156Symtab *
157ObjectFileJIT::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
177bool
178ObjectFileJIT::IsStripped ()
179{
180 return false; // JIT code that is in a module is never stripped
181}
182
183void
184ObjectFileJIT::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
198void
199ObjectFileJIT::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: ", 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
224bool
225ObjectFileJIT::GetUUID (lldb_private::UUID* uuid)
226{
227 // TODO: maybe get from delegate, not needed for first pass
228 return false;
229}
230
231
232uint32_t
233ObjectFileJIT::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
241lldb_private::Address
242ObjectFileJIT::GetEntryPointAddress ()
243{
244 return Address();
245}
246
247lldb_private::Address
248ObjectFileJIT::GetHeaderAddress ()
249{
250 return Address();
251}
252
253
254
255ObjectFile::Type
256ObjectFileJIT::CalculateType()
257{
258 return eTypeJIT;
259}
260
261ObjectFile::Strata
262ObjectFileJIT::CalculateStrata()
263{
264 return eStrataJIT;
265}
266
267
268bool
269ObjectFileJIT::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//------------------------------------------------------------------
280lldb_private::ConstString
281ObjectFileJIT::GetPluginName()
282{
283 return GetPluginNameStatic();
284}
285
286uint32_t
287ObjectFileJIT::GetPluginVersion()
288{
289 return 1;
290}
291
292
293bool
294ObjectFileJIT::SetLoadAddress (Target &target,
295 lldb::addr_t value,
296 bool value_is_offset)
297{
298 bool changed = false;
299 size_t num_loaded_sections = 0;
300 SectionList *section_list = GetSectionList ();
301 if (section_list)
302 {
303 const size_t num_sections = section_list->GetSize();
304 // "value" is an offset to apply to each top level segment
305 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
306 {
307 // Iterate through the object file sections to find all
308 // of the sections that size on disk (to avoid __PAGEZERO)
309 // and load them
310 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
311 if (section_sp &&
312 section_sp->GetFileSize() > 0 &&
313 section_sp->IsThreadSpecific() == false)
314 {
315 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
316 ++num_loaded_sections;
317 }
318 }
319 }
320 changed = num_loaded_sections > 0;
321 return num_loaded_sections > 0;
322}
323
324
325size_t
326ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
327 off_t section_offset,
328 void *dst,
329 size_t dst_len) const
330{
331 lldb::offset_t file_size = section->GetFileSize();
332 if (section_offset < file_size)
333 {
334 uint64_t src_len = file_size - section_offset;
335 if (src_len > dst_len)
336 src_len = dst_len;
337 const uint8_t *src = ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
338
339 memcpy (dst, src, src_len);
340 return src_len;
341 }
342 return 0;
343}
344size_t
345ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
346 lldb_private::DataExtractor& section_data) const
347{
348 if (section->GetFileSize())
349 {
350 const void *src = (void *)(uintptr_t)section->GetFileOffset();
351
352 DataBufferSP data_sp (new lldb_private::DataBufferHeap(src, section->GetFileSize()));
353 if (data_sp)
354 {
355 section_data.SetData (data_sp, 0, data_sp->GetByteSize());
356 section_data.SetByteOrder (GetByteOrder());
357 section_data.SetAddressByteSize (GetAddressByteSize());
358 return section_data.GetByteSize();
359 }
360 }
361 section_data.Clear();
362 return 0;
363}
364