blob: 7b1627ddd7348f1c19cbf3ab9f97db81d49bb249 [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
Greg Clayton23f8c952014-03-24 23:10:19 +000014#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
41using namespace lldb;
42using namespace lldb_private;
43
44
45void
46ObjectFileJIT::Initialize()
47{
48 PluginManager::RegisterPlugin (GetPluginNameStatic(),
49 GetPluginDescriptionStatic(),
50 CreateInstance,
51 CreateMemoryInstance,
52 GetModuleSpecifications);
53}
54
55void
56ObjectFileJIT::Terminate()
57{
58 PluginManager::UnregisterPlugin (CreateInstance);
59}
60
61
62lldb_private::ConstString
63ObjectFileJIT::GetPluginNameStatic()
64{
65 static ConstString g_name("jit");
66 return g_name;
67}
68
69const char *
70ObjectFileJIT::GetPluginDescriptionStatic()
71{
72 return "JIT code object file";
73}
74
75ObjectFile *
76ObjectFileJIT::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
88ObjectFile *
89ObjectFileJIT::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
99size_t
100ObjectFileJIT::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
111ObjectFileJIT::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
124ObjectFileJIT::~ObjectFileJIT()
125{
126}
127
128
129bool
130ObjectFileJIT::ParseHeader ()
131{
132 // JIT code is never in a file, nor is it required to have any header
133 return false;
134}
135
136ByteOrder
137ObjectFileJIT::GetByteOrder () const
138{
139 return m_data.GetByteOrder();
140}
141
142bool
143ObjectFileJIT::IsExecutable() const
144{
145 return false;
146}
147
148uint32_t
149ObjectFileJIT::GetAddressByteSize () const
150{
151 return m_data.GetAddressByteSize();
152}
153
154
155Symtab *
156ObjectFileJIT::GetSymtab()
157{
158 ModuleSP module_sp(GetModule());
159 if (module_sp)
160 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000161 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Greg Clayton23f8c952014-03-24 23:10:19 +0000162 if (m_symtab_ap.get() == NULL)
163 {
164 m_symtab_ap.reset(new Symtab(this));
165 Mutex::Locker symtab_locker (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
176bool
177ObjectFileJIT::IsStripped ()
178{
179 return false; // JIT code that is in a module is never stripped
180}
181
182void
183ObjectFileJIT::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
197void
198ObjectFileJIT::Dump (Stream *s)
199{
200 ModuleSP module_sp(GetModule());
201 if (module_sp)
202 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000203 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000204 s->Printf("%p: ", static_cast<void*>(this));
Greg Clayton23f8c952014-03-24 23:10:19 +0000205 s->Indent();
206 s->PutCString("ObjectFileJIT");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000207
Greg Clayton23f8c952014-03-24 23:10:19 +0000208 ArchSpec arch;
209 if (GetArchitecture(arch))
210 *s << ", arch = " << arch.GetArchitectureName();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000211
Greg Clayton23f8c952014-03-24 23:10:19 +0000212 s->EOL();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000213
Greg Clayton23f8c952014-03-24 23:10:19 +0000214 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
223bool
224ObjectFileJIT::GetUUID (lldb_private::UUID* uuid)
225{
226 // TODO: maybe get from delegate, not needed for first pass
227 return false;
228}
229
230
231uint32_t
232ObjectFileJIT::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
240lldb_private::Address
241ObjectFileJIT::GetEntryPointAddress ()
242{
243 return Address();
244}
245
246lldb_private::Address
247ObjectFileJIT::GetHeaderAddress ()
248{
249 return Address();
250}
251
252
253
254ObjectFile::Type
255ObjectFileJIT::CalculateType()
256{
257 return eTypeJIT;
258}
259
260ObjectFile::Strata
261ObjectFileJIT::CalculateStrata()
262{
263 return eStrataJIT;
264}
265
266
267bool
268ObjectFileJIT::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//------------------------------------------------------------------
279lldb_private::ConstString
280ObjectFileJIT::GetPluginName()
281{
282 return GetPluginNameStatic();
283}
284
285uint32_t
286ObjectFileJIT::GetPluginVersion()
287{
288 return 1;
289}
290
291
292bool
293ObjectFileJIT::SetLoadAddress (Target &target,
294 lldb::addr_t value,
295 bool value_is_offset)
296{
Greg Clayton23f8c952014-03-24 23:10:19 +0000297 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 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000318 return num_loaded_sections > 0;
319}
320
321
322size_t
323ObjectFileJIT::ReadSectionData (const lldb_private::Section *section,
Zachary Turnera746e8e2014-07-02 17:24:07 +0000324 lldb::offset_t section_offset,
Greg Clayton23f8c952014-03-24 23:10:19 +0000325 void *dst,
326 size_t dst_len) const
327{
328 lldb::offset_t file_size = section->GetFileSize();
Zachary Turnera746e8e2014-07-02 17:24:07 +0000329 if (section_offset < file_size)
Greg Clayton23f8c952014-03-24 23:10:19 +0000330 {
Zachary Turnera746e8e2014-07-02 17:24:07 +0000331 size_t src_len = file_size - section_offset;
Greg Clayton23f8c952014-03-24 23:10:19 +0000332 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}
Zachary Turnera746e8e2014-07-02 17:24:07 +0000341
Greg Clayton23f8c952014-03-24 23:10:19 +0000342size_t
343ObjectFileJIT::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