blob: 17bd03ae6cb56e01269aa892d1a5b4e8b34aedcb [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
Greg Clayton7b0992d2013-04-18 22:45:39 +000028IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
29 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000030 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000031 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000032 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000033 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000034 m_context_ap(context_ap.release()),
35 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000036 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000037 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000038 m_name(name),
39 m_did_jit(false),
40 m_function_load_addr(LLDB_INVALID_ADDRESS),
41 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
42{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000043}
44
45lldb::addr_t
46IRExecutionUnit::WriteNow (const uint8_t *bytes,
47 size_t size,
48 Error &error)
49{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000050 lldb::addr_t allocation_process_addr = Malloc (size,
51 8,
52 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
53 eAllocationPolicyMirror,
54 error);
55
56 if (!error.Success())
57 return LLDB_INVALID_ADDRESS;
58
59 WriteMemory(allocation_process_addr, bytes, size, error);
60
61 if (!error.Success())
62 {
63 Error err;
64 Free (allocation_process_addr, err);
65
66 return LLDB_INVALID_ADDRESS;
67 }
68
69 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
70 {
71 DataBufferHeap my_buffer(size, 0);
72 Error err;
73 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
74
75 if (err.Success())
76 {
77 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000078 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000079 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000080 }
81
Sean Callanan5a1af4e2013-04-05 02:22:57 +000082 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000083}
84
85void
86IRExecutionUnit::FreeNow (lldb::addr_t allocation)
87{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000088 if (allocation == LLDB_INVALID_ADDRESS)
89 return;
90
Sean Callanan5a1af4e2013-04-05 02:22:57 +000091 Error err;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000092
Sean Callanan5a1af4e2013-04-05 02:22:57 +000093 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000094}
95
96Error
97IRExecutionUnit::DisassembleFunction (Stream &stream,
98 lldb::ProcessSP &process_wp)
99{
Greg Clayton5160ce52013-03-27 23:08:40 +0000100 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000101
102 ExecutionContext exe_ctx(process_wp);
103
104 Error ret;
105
106 ret.Clear();
107
108 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
109 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
110
111 for (JittedFunction &function : m_jitted_functions)
112 {
113 if (strstr(function.m_name.c_str(), m_name.AsCString()))
114 {
115 func_local_addr = function.m_local_addr;
116 func_remote_addr = function.m_remote_addr;
117 }
118 }
119
120 if (func_local_addr == LLDB_INVALID_ADDRESS)
121 {
122 ret.SetErrorToGenericError();
123 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
124 return ret;
125 }
126
127 if (log)
128 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
129
130 std::pair <lldb::addr_t, lldb::addr_t> func_range;
131
132 func_range = GetRemoteRangeForLocal(func_local_addr);
133
134 if (func_range.first == 0 && func_range.second == 0)
135 {
136 ret.SetErrorToGenericError();
137 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
138 return ret;
139 }
140
141 if (log)
142 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
143
144 Target *target = exe_ctx.GetTargetPtr();
145 if (!target)
146 {
147 ret.SetErrorToGenericError();
148 ret.SetErrorString("Couldn't find the target");
149 return ret;
150 }
151
152 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
153
154 Process *process = exe_ctx.GetProcessPtr();
155 Error err;
156 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
157
158 if (!err.Success())
159 {
160 ret.SetErrorToGenericError();
161 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
162 return ret;
163 }
164
165 ArchSpec arch(target->GetArchitecture());
166
167 const char *plugin_name = NULL;
168 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000169 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000170
Jim Ingham56d40422013-07-31 02:19:15 +0000171 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000172 {
173 ret.SetErrorToGenericError();
174 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
175 return ret;
176 }
177
178 if (!process)
179 {
180 ret.SetErrorToGenericError();
181 ret.SetErrorString("Couldn't find the process");
182 return ret;
183 }
184
185 DataExtractor extractor(buffer_sp,
186 process->GetByteOrder(),
187 target->GetArchitecture().GetAddressByteSize());
188
189 if (log)
190 {
191 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000192 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000193 0,
194 extractor.GetByteSize(),
195 func_remote_addr,
196 16,
197 DataExtractor::TypeUInt8);
198 }
199
Jim Ingham56d40422013-07-31 02:19:15 +0000200 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000201
Jim Ingham56d40422013-07-31 02:19:15 +0000202 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000203 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
204
205 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
206 instruction_index < num_instructions;
207 ++instruction_index)
208 {
209 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
210 instruction->Dump (&stream,
211 max_opcode_byte_size,
212 true,
213 true,
214 &exe_ctx);
215 stream.PutChar('\n');
216 }
Jim Ingham56d40422013-07-31 02:19:15 +0000217 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
218 // I'll fix that but for now, just clear the list and it will go away nicely.
219 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000220 return ret;
221}
222
Sean Callanan44bc6572013-03-27 03:09:55 +0000223static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
224{
225 Error *err = static_cast<Error*>(Context);
226
227 if (err && err->Success())
228 {
229 err->SetErrorToGenericError();
230 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
231 }
232}
233
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000234void
235IRExecutionUnit::GetRunnableInfo(Error &error,
236 lldb::addr_t &func_addr,
237 lldb::addr_t &func_end)
238{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000239 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000240
Sean Callanan7c435a52014-02-06 22:25:20 +0000241 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
242
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000243 func_addr = LLDB_INVALID_ADDRESS;
244 func_end = LLDB_INVALID_ADDRESS;
245
246 if (!process_sp)
247 {
248 error.SetErrorToGenericError();
249 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
250 return;
251 }
252
253 if (m_did_jit)
254 {
255 func_addr = m_function_load_addr;
256 func_end = m_function_end_load_addr;
257
258 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000259 };
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000260
Sean Callanan7c435a52014-02-06 22:25:20 +0000261 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
262
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000263 m_did_jit = true;
264
265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
266
267 std::string error_string;
268
269 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000270 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000271 std::string s;
272 llvm::raw_string_ostream oss(s);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000273
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000274 m_module->print(oss, NULL);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 oss.flush();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000277
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
279 }
280
281 llvm::Triple triple(m_module->getTargetTriple());
282 llvm::Function *function = m_module->getFunction (m_name.AsCString());
283 llvm::Reloc::Model relocModel;
284 llvm::CodeModel::Model codeModel;
285
286 if (triple.isOSBinFormatELF())
287 {
288 relocModel = llvm::Reloc::Static;
289 // This will be small for 32-bit and large for 64-bit.
290 codeModel = llvm::CodeModel::JITDefault;
291 }
292 else
293 {
294 relocModel = llvm::Reloc::PIC_;
295 codeModel = llvm::CodeModel::Small;
296 }
297
298 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
299
300 llvm::EngineBuilder builder(m_module_ap.get());
301
302 builder.setEngineKind(llvm::EngineKind::JIT)
303 .setErrorStr(&error_string)
304 .setRelocationModel(relocModel)
305 .setJITMemoryManager(new MemoryManager(*this))
306 .setOptLevel(llvm::CodeGenOpt::Less)
307 .setAllocateGVsWithCode(true)
308 .setCodeModel(codeModel)
309 .setUseMCJIT(true);
310
311 llvm::StringRef mArch;
312 llvm::StringRef mCPU;
313 llvm::SmallVector<std::string, 0> mAttrs;
314
315 for (std::string &feature : m_cpu_features)
316 mAttrs.push_back(feature);
317
318 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
319 mArch,
320 mCPU,
321 mAttrs);
322
323 m_execution_engine_ap.reset(builder.create(target_machine));
324
325 if (!m_execution_engine_ap.get())
326 {
327 error.SetErrorToGenericError();
328 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000329 return;
330 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000331 else
332 {
333 m_module_ap.release(); // ownership was transferred
334 }
335
336 m_execution_engine_ap->DisableLazyCompilation();
337
338 // We don't actually need the function pointer here, this just forces it to get resolved.
339
340 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
341
342 if (!error.Success())
343 {
344 // We got an error through our callback!
345 return;
346 }
347
348 if (!function)
349 {
350 error.SetErrorToGenericError();
351 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
352 return;
353 }
354
355 if (!fun_ptr)
356 {
357 error.SetErrorToGenericError();
358 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
359 return;
360 }
361
362 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
363
364 CommitAllocations(process_sp);
365 ReportAllocations(*m_execution_engine_ap);
366 WriteData(process_sp);
367
368 for (JittedFunction &jitted_function : m_jitted_functions)
369 {
370 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
371
372 if (!jitted_function.m_name.compare(m_name.AsCString()))
373 {
374 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
375 m_function_end_load_addr = func_range.first + func_range.second;
376 m_function_load_addr = jitted_function.m_remote_addr;
377 }
378 }
379
380 if (log)
381 {
382 log->Printf("Code can be run in the target.");
383
384 StreamString disassembly_stream;
385
386 Error err = DisassembleFunction(disassembly_stream, process_sp);
387
388 if (!err.Success())
389 {
390 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
391 }
392 else
393 {
394 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
395 }
Sean Callanan4f2c1982014-01-21 00:54:48 +0000396
397 log->Printf("Sections: ");
398 for (AllocationRecord &record : m_records)
399 {
400 if (record.m_process_address != LLDB_INVALID_ADDRESS)
401 {
402 record.dump(log);
403
404 DataBufferHeap my_buffer(record.m_size, 0);
405 Error err;
406 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
407
408 if (err.Success())
409 {
410 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
411 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
412 }
413 }
414 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000415 }
416
417 func_addr = m_function_load_addr;
418 func_end = m_function_end_load_addr;
419
420 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000421}
422
423IRExecutionUnit::~IRExecutionUnit ()
424{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000425 m_module_ap.reset();
426 m_execution_engine_ap.reset();
427 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000428}
429
430IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
431 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
432 m_parent (parent)
433{
434}
435
436void
437IRExecutionUnit::MemoryManager::setMemoryWritable ()
438{
439 m_default_mm_ap->setMemoryWritable();
440}
441
442void
443IRExecutionUnit::MemoryManager::setMemoryExecutable ()
444{
445 m_default_mm_ap->setMemoryExecutable();
446}
447
448
449uint8_t *
450IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
451 uintptr_t &ActualSize)
452{
453 return m_default_mm_ap->startFunctionBody(F, ActualSize);
454}
455
456uint8_t *
457IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
458 unsigned StubSize,
459 unsigned Alignment)
460{
Greg Clayton5160ce52013-03-27 23:08:40 +0000461 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000462
463 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000464
465 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
466 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
467 StubSize,
468 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000469
470 if (log)
471 {
472 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
473 F, StubSize, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000474 }
Sean Callanan2c047352013-03-19 23:03:21 +0000475
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000476 return return_value;
477}
478
479void
480IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
481 uint8_t *FunctionStart,
482 uint8_t *FunctionEnd)
483{
484 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
485}
486
487uint8_t *
488IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
489{
Greg Clayton5160ce52013-03-27 23:08:40 +0000490 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000491
492 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
493
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000494 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
495 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
496 Size,
497 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000498
499 if (log)
500 {
501 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
502 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000503 }
Sean Callanan2c047352013-03-19 23:03:21 +0000504
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000505 return return_value;
506}
507
508uint8_t *
509IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
510 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000511 unsigned SectionID,
512 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000513{
Greg Clayton5160ce52013-03-27 23:08:40 +0000514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000515
Filip Pizlobfcff682013-10-02 01:43:46 +0000516 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000517
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000518 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000519 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000520 Size,
521 Alignment,
522 SectionID));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000523
524 if (log)
525 {
526 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
527 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000528 }
Sean Callanan2c047352013-03-19 23:03:21 +0000529
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000530 return return_value;
531}
532
533uint8_t *
534IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
535 unsigned Alignment,
536 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000537 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000538 bool IsReadOnly)
539{
Greg Clayton5160ce52013-03-27 23:08:40 +0000540 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000541
Filip Pizlobfcff682013-10-02 01:43:46 +0000542 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000543
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000544 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
545 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
546 Size,
547 Alignment,
548 SectionID));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000549 if (log)
550 {
551 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
552 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000553 }
Sean Callanan2c047352013-03-19 23:03:21 +0000554
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000555 return return_value;
556}
557
558uint8_t *
559IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
560 unsigned Alignment)
561{
Greg Clayton5160ce52013-03-27 23:08:40 +0000562 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000563
564 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
565
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000566 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
567 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
568 Size,
569 Alignment));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000570
571 if (log)
572 {
573 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
574 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000575 }
576
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000577 return return_value;
578}
579
580void
581IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
582{
583 m_default_mm_ap->deallocateFunctionBody(Body);
584}
585
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000586lldb::addr_t
587IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
588{
Sean Callananffae9442013-06-27 01:42:47 +0000589 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
590
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000591 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000592 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000593 if (local_address >= record.m_host_address &&
594 local_address < record.m_host_address + record.m_size)
595 {
596 if (record.m_process_address == LLDB_INVALID_ADDRESS)
597 return LLDB_INVALID_ADDRESS;
Sean Callananffae9442013-06-27 01:42:47 +0000598
599 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
600
601 if (log)
602 {
603 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
604 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000605 (uint64_t)record.m_host_address,
606 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000607 ret,
608 record.m_process_address,
609 record.m_process_address + record.m_size);
610 }
611
612 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000613 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000614 }
615
616 return LLDB_INVALID_ADDRESS;
617}
618
619IRExecutionUnit::AddrRange
620IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
621{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000622 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000623 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000624 if (local_address >= record.m_host_address &&
625 local_address < record.m_host_address + record.m_size)
626 {
627 if (record.m_process_address == LLDB_INVALID_ADDRESS)
628 return AddrRange(0, 0);
629
630 return AddrRange(record.m_process_address, record.m_size);
631 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000632 }
633
634 return AddrRange (0, 0);
635}
636
637bool
638IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
639{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000640 bool ret = true;
641
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000642 lldb_private::Error err;
643
644 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000645 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000646 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000647 continue;
648
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000649
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000650 record.m_process_address = Malloc(record.m_size,
651 record.m_alignment,
652 record.m_permissions,
653 eAllocationPolicyProcessOnly,
654 err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000655
656 if (!err.Success())
657 {
658 ret = false;
659 break;
660 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000661 }
662
663 if (!ret)
664 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000665 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000666 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000667 if (record.m_process_address != LLDB_INVALID_ADDRESS)
668 {
669 Free(record.m_process_address, err);
670 record.m_process_address = LLDB_INVALID_ADDRESS;
671 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000672 }
673 }
674
675 return ret;
676}
677
678void
679IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
680{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000681 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000682 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000683 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000684 continue;
685
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000686 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000687 continue;
688
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000689 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000690 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000691
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000692 // Trigger re-application of relocations.
693 engine.finalizeObject();
694}
695
696bool
697IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
698{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000699 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000701 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000702 return false;
703
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000704 lldb_private::Error err;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000705
706 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000707 }
708
709 return true;
710}
711
712void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000713IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000714{
715 if (!log)
716 return;
717
718 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000719 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000720 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000721 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000722 (unsigned)m_alignment,
723 (unsigned)m_section_id);
724}