blob: dba361bd5257131ed0ba31f21935311f8b271e83 [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002//
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// C Includes
11// C++ Includes
12// Other libraries and framework includes
13#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000014#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000015#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000016#include "llvm/Support/SourceMgr.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000017// Project includes
18#include "lldb/Core/DataBufferHeap.h"
19#include "lldb/Core/DataExtractor.h"
20#include "lldb/Core/Disassembler.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Expression/IRExecutionUnit.h"
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Target.h"
25
26using namespace lldb_private;
27
28IRExecutionUnit::IRExecutionUnit (std::auto_ptr<llvm::Module> &module_ap,
29 ConstString &name,
30 lldb::ProcessSP process_sp,
31 std::vector<std::string> &cpu_features) :
Sean Callanan5a1af4e2013-04-05 02:22:57 +000032 IRMemoryMap(process_sp),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000033 m_module_ap(module_ap),
34 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000035 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000036 m_name(name),
37 m_did_jit(false),
38 m_function_load_addr(LLDB_INVALID_ADDRESS),
39 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
40{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000041}
42
43lldb::addr_t
44IRExecutionUnit::WriteNow (const uint8_t *bytes,
45 size_t size,
46 Error &error)
47{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000048 lldb::addr_t allocation_process_addr = Malloc (size,
49 8,
50 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
51 eAllocationPolicyMirror,
52 error);
53
54 if (!error.Success())
55 return LLDB_INVALID_ADDRESS;
56
57 WriteMemory(allocation_process_addr, bytes, size, error);
58
59 if (!error.Success())
60 {
61 Error err;
62 Free (allocation_process_addr, err);
63
64 return LLDB_INVALID_ADDRESS;
65 }
66
67 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
68 {
69 DataBufferHeap my_buffer(size, 0);
70 Error err;
71 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
72
73 if (err.Success())
74 {
75 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000076
Sean Callanan5a1af4e2013-04-05 02:22:57 +000077 StreamString ss;
78
79 my_extractor.Dump(&ss, 0, lldb::eFormatBytesWithASCII, 1, my_buffer.GetByteSize(), 32, allocation_process_addr, 0, 0);
80
81 log->PutCString(ss.GetData());
82 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000083 }
84
Sean Callanan5a1af4e2013-04-05 02:22:57 +000085 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000086}
87
88void
89IRExecutionUnit::FreeNow (lldb::addr_t allocation)
90{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000091 if (allocation == LLDB_INVALID_ADDRESS)
92 return;
93
Sean Callanan5a1af4e2013-04-05 02:22:57 +000094 Error err;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000095
Sean Callanan5a1af4e2013-04-05 02:22:57 +000096 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000097}
98
99Error
100IRExecutionUnit::DisassembleFunction (Stream &stream,
101 lldb::ProcessSP &process_wp)
102{
Greg Clayton5160ce52013-03-27 23:08:40 +0000103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000104
105 ExecutionContext exe_ctx(process_wp);
106
107 Error ret;
108
109 ret.Clear();
110
111 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
112 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
113
114 for (JittedFunction &function : m_jitted_functions)
115 {
116 if (strstr(function.m_name.c_str(), m_name.AsCString()))
117 {
118 func_local_addr = function.m_local_addr;
119 func_remote_addr = function.m_remote_addr;
120 }
121 }
122
123 if (func_local_addr == LLDB_INVALID_ADDRESS)
124 {
125 ret.SetErrorToGenericError();
126 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
127 return ret;
128 }
129
130 if (log)
131 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
132
133 std::pair <lldb::addr_t, lldb::addr_t> func_range;
134
135 func_range = GetRemoteRangeForLocal(func_local_addr);
136
137 if (func_range.first == 0 && func_range.second == 0)
138 {
139 ret.SetErrorToGenericError();
140 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
141 return ret;
142 }
143
144 if (log)
145 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
146
147 Target *target = exe_ctx.GetTargetPtr();
148 if (!target)
149 {
150 ret.SetErrorToGenericError();
151 ret.SetErrorString("Couldn't find the target");
152 return ret;
153 }
154
155 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
156
157 Process *process = exe_ctx.GetProcessPtr();
158 Error err;
159 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
160
161 if (!err.Success())
162 {
163 ret.SetErrorToGenericError();
164 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
165 return ret;
166 }
167
168 ArchSpec arch(target->GetArchitecture());
169
170 const char *plugin_name = NULL;
171 const char *flavor_string = NULL;
172 lldb::DisassemblerSP disassembler = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
173
174 if (!disassembler)
175 {
176 ret.SetErrorToGenericError();
177 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
178 return ret;
179 }
180
181 if (!process)
182 {
183 ret.SetErrorToGenericError();
184 ret.SetErrorString("Couldn't find the process");
185 return ret;
186 }
187
188 DataExtractor extractor(buffer_sp,
189 process->GetByteOrder(),
190 target->GetArchitecture().GetAddressByteSize());
191
192 if (log)
193 {
194 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000195 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000196 0,
197 extractor.GetByteSize(),
198 func_remote_addr,
199 16,
200 DataExtractor::TypeUInt8);
201 }
202
Greg Clayton3faf47c2013-03-28 23:42:53 +0000203 disassembler->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000204
205 InstructionList &instruction_list = disassembler->GetInstructionList();
206 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
207
208 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
209 instruction_index < num_instructions;
210 ++instruction_index)
211 {
212 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
213 instruction->Dump (&stream,
214 max_opcode_byte_size,
215 true,
216 true,
217 &exe_ctx);
218 stream.PutChar('\n');
219 }
220
221 return ret;
222}
223
Sean Callanan44bc6572013-03-27 03:09:55 +0000224static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
225{
226 Error *err = static_cast<Error*>(Context);
227
228 if (err && err->Success())
229 {
230 err->SetErrorToGenericError();
231 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
232 }
233}
234
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000235void
236IRExecutionUnit::GetRunnableInfo(Error &error,
237 lldb::addr_t &func_addr,
238 lldb::addr_t &func_end)
239{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000240 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241
242 func_addr = LLDB_INVALID_ADDRESS;
243 func_end = LLDB_INVALID_ADDRESS;
244
245 if (!process_sp)
246 {
247 error.SetErrorToGenericError();
248 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
249 return;
250 }
251
252 if (m_did_jit)
253 {
254 func_addr = m_function_load_addr;
255 func_end = m_function_end_load_addr;
256
257 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000258 };
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000259
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000260 m_did_jit = true;
261
262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
263
264 std::string error_string;
265
266 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000267 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000268 std::string s;
269 llvm::raw_string_ostream oss(s);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000270
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000271 m_module->print(oss, NULL);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000272
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000273 oss.flush();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000274
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000275 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
276 }
277
278 llvm::Triple triple(m_module->getTargetTriple());
279 llvm::Function *function = m_module->getFunction (m_name.AsCString());
280 llvm::Reloc::Model relocModel;
281 llvm::CodeModel::Model codeModel;
282
283 if (triple.isOSBinFormatELF())
284 {
285 relocModel = llvm::Reloc::Static;
286 // This will be small for 32-bit and large for 64-bit.
287 codeModel = llvm::CodeModel::JITDefault;
288 }
289 else
290 {
291 relocModel = llvm::Reloc::PIC_;
292 codeModel = llvm::CodeModel::Small;
293 }
294
295 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
296
297 llvm::EngineBuilder builder(m_module_ap.get());
298
299 builder.setEngineKind(llvm::EngineKind::JIT)
300 .setErrorStr(&error_string)
301 .setRelocationModel(relocModel)
302 .setJITMemoryManager(new MemoryManager(*this))
303 .setOptLevel(llvm::CodeGenOpt::Less)
304 .setAllocateGVsWithCode(true)
305 .setCodeModel(codeModel)
306 .setUseMCJIT(true);
307
308 llvm::StringRef mArch;
309 llvm::StringRef mCPU;
310 llvm::SmallVector<std::string, 0> mAttrs;
311
312 for (std::string &feature : m_cpu_features)
313 mAttrs.push_back(feature);
314
315 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
316 mArch,
317 mCPU,
318 mAttrs);
319
320 m_execution_engine_ap.reset(builder.create(target_machine));
321
322 if (!m_execution_engine_ap.get())
323 {
324 error.SetErrorToGenericError();
325 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000326 return;
327 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000328 else
329 {
330 m_module_ap.release(); // ownership was transferred
331 }
332
333 m_execution_engine_ap->DisableLazyCompilation();
334
335 // We don't actually need the function pointer here, this just forces it to get resolved.
336
337 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
338
339 if (!error.Success())
340 {
341 // We got an error through our callback!
342 return;
343 }
344
345 if (!function)
346 {
347 error.SetErrorToGenericError();
348 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
349 return;
350 }
351
352 if (!fun_ptr)
353 {
354 error.SetErrorToGenericError();
355 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
356 return;
357 }
358
359 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
360
361 CommitAllocations(process_sp);
362 ReportAllocations(*m_execution_engine_ap);
363 WriteData(process_sp);
364
365 for (JittedFunction &jitted_function : m_jitted_functions)
366 {
367 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
368
369 if (!jitted_function.m_name.compare(m_name.AsCString()))
370 {
371 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
372 m_function_end_load_addr = func_range.first + func_range.second;
373 m_function_load_addr = jitted_function.m_remote_addr;
374 }
375 }
376
377 if (log)
378 {
379 log->Printf("Code can be run in the target.");
380
381 StreamString disassembly_stream;
382
383 Error err = DisassembleFunction(disassembly_stream, process_sp);
384
385 if (!err.Success())
386 {
387 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
388 }
389 else
390 {
391 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
392 }
393 }
394
395 func_addr = m_function_load_addr;
396 func_end = m_function_end_load_addr;
397
398 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000399}
400
401IRExecutionUnit::~IRExecutionUnit ()
402{
403}
404
405IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
406 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
407 m_parent (parent)
408{
409}
410
411void
412IRExecutionUnit::MemoryManager::setMemoryWritable ()
413{
414 m_default_mm_ap->setMemoryWritable();
415}
416
417void
418IRExecutionUnit::MemoryManager::setMemoryExecutable ()
419{
420 m_default_mm_ap->setMemoryExecutable();
421}
422
423
424uint8_t *
425IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
426 uintptr_t &ActualSize)
427{
428 return m_default_mm_ap->startFunctionBody(F, ActualSize);
429}
430
431uint8_t *
432IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
433 unsigned StubSize,
434 unsigned Alignment)
435{
Greg Clayton5160ce52013-03-27 23:08:40 +0000436 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000437
438 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000439
440 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
441 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
442 StubSize,
443 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000444
445 if (log)
446 {
447 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
448 F, StubSize, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000449 }
Sean Callanan2c047352013-03-19 23:03:21 +0000450
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000451 return return_value;
452}
453
454void
455IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
456 uint8_t *FunctionStart,
457 uint8_t *FunctionEnd)
458{
459 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
460}
461
462uint8_t *
463IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
464{
Greg Clayton5160ce52013-03-27 23:08:40 +0000465 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000466
467 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
468
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000469 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
470 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
471 Size,
472 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000473
474 if (log)
475 {
476 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
477 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000478 }
Sean Callanan2c047352013-03-19 23:03:21 +0000479
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000480 return return_value;
481}
482
483uint8_t *
484IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
485 unsigned Alignment,
486 unsigned SectionID)
487{
Greg Clayton5160ce52013-03-27 23:08:40 +0000488 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000489
490 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID);
491
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000492 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
493 lldb::ePermissionsReadable | lldb::ePermissionsWritable | lldb::ePermissionsExecutable,
494 Size,
495 Alignment,
496 SectionID));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000497
498 if (log)
499 {
500 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
501 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000502 }
Sean Callanan2c047352013-03-19 23:03:21 +0000503
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000504 return return_value;
505}
506
507uint8_t *
508IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
509 unsigned Alignment,
510 unsigned SectionID,
511 bool IsReadOnly)
512{
Greg Clayton5160ce52013-03-27 23:08:40 +0000513 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000514
515 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, IsReadOnly);
516
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000517 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
518 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
519 Size,
520 Alignment,
521 SectionID));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000522 if (log)
523 {
524 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
525 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000526 }
Sean Callanan2c047352013-03-19 23:03:21 +0000527
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000528 return return_value;
529}
530
531uint8_t *
532IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
533 unsigned Alignment)
534{
Greg Clayton5160ce52013-03-27 23:08:40 +0000535 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000536
537 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
538
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000539 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
540 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
541 Size,
542 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000543
544 if (log)
545 {
546 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
547 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000548 }
549
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000550 return return_value;
551}
552
553void
554IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
555{
556 m_default_mm_ap->deallocateFunctionBody(Body);
557}
558
559uint8_t*
560IRExecutionUnit::MemoryManager::startExceptionTable(const llvm::Function* F,
561 uintptr_t &ActualSize)
562{
563 return m_default_mm_ap->startExceptionTable(F, ActualSize);
564}
565
566void
567IRExecutionUnit::MemoryManager::endExceptionTable(const llvm::Function *F,
568 uint8_t *TableStart,
569 uint8_t *TableEnd,
570 uint8_t* FrameRegister)
571{
572 m_default_mm_ap->endExceptionTable(F, TableStart, TableEnd, FrameRegister);
573}
574
575void
576IRExecutionUnit::MemoryManager::deallocateExceptionTable(void *ET)
577{
578 m_default_mm_ap->deallocateExceptionTable (ET);
579}
580
581lldb::addr_t
582IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
583{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000584 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000585 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000586 if (local_address >= record.m_host_address &&
587 local_address < record.m_host_address + record.m_size)
588 {
589 if (record.m_process_address == LLDB_INVALID_ADDRESS)
590 return LLDB_INVALID_ADDRESS;
591 }
592
593 return record.m_process_address + (local_address - record.m_host_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000594 }
595
596 return LLDB_INVALID_ADDRESS;
597}
598
599IRExecutionUnit::AddrRange
600IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
601{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000602 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000603 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000604 if (local_address >= record.m_host_address &&
605 local_address < record.m_host_address + record.m_size)
606 {
607 if (record.m_process_address == LLDB_INVALID_ADDRESS)
608 return AddrRange(0, 0);
609
610 return AddrRange(record.m_process_address, record.m_size);
611 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000612 }
613
614 return AddrRange (0, 0);
615}
616
617bool
618IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
619{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000620 bool ret = true;
621
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000622 lldb_private::Error err;
623
624 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000625 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000626 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000627 continue;
628
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000629
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000630 record.m_process_address = Malloc(record.m_size,
631 record.m_alignment,
632 record.m_permissions,
633 eAllocationPolicyProcessOnly,
634 err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000635
636 if (!err.Success())
637 {
638 ret = false;
639 break;
640 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000641 }
642
643 if (!ret)
644 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000645 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000646 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000647 if (record.m_process_address != LLDB_INVALID_ADDRESS)
648 {
649 Free(record.m_process_address, err);
650 record.m_process_address = LLDB_INVALID_ADDRESS;
651 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000652 }
653 }
654
655 return ret;
656}
657
658void
659IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
660{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000661 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000662 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000663 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000664 continue;
665
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000666 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000667 continue;
668
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000669 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000670 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000671
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000672 // Trigger re-application of relocations.
673 engine.finalizeObject();
674}
675
676bool
677IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
678{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000679 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000680 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000681 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000682 return false;
683
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000684 lldb_private::Error err;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000685
686 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000687 }
688
689 return true;
690}
691
692void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000693IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000694{
695 if (!log)
696 return;
697
698 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000699 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000701 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000702 (unsigned)m_alignment,
703 (unsigned)m_section_id);
704}