blob: d50802ac956e0da2c48b12df8e290ec7621e6e8f [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"
Greg Clayton23f8c952014-03-24 23:10:19 +000022#include "lldb/Core/Module.h"
23#include "lldb/Core/Section.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000024#include "lldb/Expression/IRExecutionUnit.h"
25#include "lldb/Target/ExecutionContext.h"
26#include "lldb/Target/Target.h"
27
28using namespace lldb_private;
29
Greg Clayton7b0992d2013-04-18 22:45:39 +000030IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
31 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000032 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000033 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000035 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000036 m_context_ap(context_ap.release()),
37 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000038 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000039 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000040 m_name(name),
41 m_did_jit(false),
42 m_function_load_addr(LLDB_INVALID_ADDRESS),
43 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
44{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000045}
46
47lldb::addr_t
48IRExecutionUnit::WriteNow (const uint8_t *bytes,
49 size_t size,
50 Error &error)
51{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000052 lldb::addr_t allocation_process_addr = Malloc (size,
53 8,
54 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
55 eAllocationPolicyMirror,
56 error);
57
58 if (!error.Success())
59 return LLDB_INVALID_ADDRESS;
60
61 WriteMemory(allocation_process_addr, bytes, size, error);
62
63 if (!error.Success())
64 {
65 Error err;
66 Free (allocation_process_addr, err);
67
68 return LLDB_INVALID_ADDRESS;
69 }
70
71 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
72 {
73 DataBufferHeap my_buffer(size, 0);
74 Error err;
75 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
76
77 if (err.Success())
78 {
79 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000080 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000081 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000082 }
83
Sean Callanan5a1af4e2013-04-05 02:22:57 +000084 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000085}
86
87void
88IRExecutionUnit::FreeNow (lldb::addr_t allocation)
89{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000090 if (allocation == LLDB_INVALID_ADDRESS)
91 return;
92
Sean Callanan5a1af4e2013-04-05 02:22:57 +000093 Error err;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000094
Sean Callanan5a1af4e2013-04-05 02:22:57 +000095 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000096}
97
98Error
99IRExecutionUnit::DisassembleFunction (Stream &stream,
100 lldb::ProcessSP &process_wp)
101{
Greg Clayton5160ce52013-03-27 23:08:40 +0000102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000103
104 ExecutionContext exe_ctx(process_wp);
105
106 Error ret;
107
108 ret.Clear();
109
110 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
111 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
112
113 for (JittedFunction &function : m_jitted_functions)
114 {
115 if (strstr(function.m_name.c_str(), m_name.AsCString()))
116 {
117 func_local_addr = function.m_local_addr;
118 func_remote_addr = function.m_remote_addr;
119 }
120 }
121
122 if (func_local_addr == LLDB_INVALID_ADDRESS)
123 {
124 ret.SetErrorToGenericError();
125 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
126 return ret;
127 }
128
129 if (log)
130 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
131
132 std::pair <lldb::addr_t, lldb::addr_t> func_range;
133
134 func_range = GetRemoteRangeForLocal(func_local_addr);
135
136 if (func_range.first == 0 && func_range.second == 0)
137 {
138 ret.SetErrorToGenericError();
139 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
140 return ret;
141 }
142
143 if (log)
144 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
145
146 Target *target = exe_ctx.GetTargetPtr();
147 if (!target)
148 {
149 ret.SetErrorToGenericError();
150 ret.SetErrorString("Couldn't find the target");
151 return ret;
152 }
153
154 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
155
156 Process *process = exe_ctx.GetProcessPtr();
157 Error err;
158 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
159
160 if (!err.Success())
161 {
162 ret.SetErrorToGenericError();
163 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
164 return ret;
165 }
166
167 ArchSpec arch(target->GetArchitecture());
168
169 const char *plugin_name = NULL;
170 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000171 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000172
Jim Ingham56d40422013-07-31 02:19:15 +0000173 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000174 {
175 ret.SetErrorToGenericError();
176 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
177 return ret;
178 }
179
180 if (!process)
181 {
182 ret.SetErrorToGenericError();
183 ret.SetErrorString("Couldn't find the process");
184 return ret;
185 }
186
187 DataExtractor extractor(buffer_sp,
188 process->GetByteOrder(),
189 target->GetArchitecture().GetAddressByteSize());
190
191 if (log)
192 {
193 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000194 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000195 0,
196 extractor.GetByteSize(),
197 func_remote_addr,
198 16,
199 DataExtractor::TypeUInt8);
200 }
201
Jim Ingham56d40422013-07-31 02:19:15 +0000202 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000203
Jim Ingham56d40422013-07-31 02:19:15 +0000204 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000205 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
206
207 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
208 instruction_index < num_instructions;
209 ++instruction_index)
210 {
211 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
212 instruction->Dump (&stream,
213 max_opcode_byte_size,
214 true,
215 true,
216 &exe_ctx);
217 stream.PutChar('\n');
218 }
Jim Ingham56d40422013-07-31 02:19:15 +0000219 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
220 // I'll fix that but for now, just clear the list and it will go away nicely.
221 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000222 return ret;
223}
224
Sean Callanan44bc6572013-03-27 03:09:55 +0000225static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
226{
227 Error *err = static_cast<Error*>(Context);
228
229 if (err && err->Success())
230 {
231 err->SetErrorToGenericError();
232 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
233 }
234}
235
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000236void
237IRExecutionUnit::GetRunnableInfo(Error &error,
238 lldb::addr_t &func_addr,
239 lldb::addr_t &func_end)
240{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000241 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000242
Sean Callanan7c435a52014-02-06 22:25:20 +0000243 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
244
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000245 func_addr = LLDB_INVALID_ADDRESS;
246 func_end = LLDB_INVALID_ADDRESS;
247
248 if (!process_sp)
249 {
250 error.SetErrorToGenericError();
251 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
252 return;
253 }
254
255 if (m_did_jit)
256 {
257 func_addr = m_function_load_addr;
258 func_end = m_function_end_load_addr;
259
260 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000261 };
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000262
Sean Callanan7c435a52014-02-06 22:25:20 +0000263 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
264
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000265 m_did_jit = true;
266
267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
268
269 std::string error_string;
270
271 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000272 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000273 std::string s;
274 llvm::raw_string_ostream oss(s);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 m_module->print(oss, NULL);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000277
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 oss.flush();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000279
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000280 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
281 }
282
283 llvm::Triple triple(m_module->getTargetTriple());
284 llvm::Function *function = m_module->getFunction (m_name.AsCString());
285 llvm::Reloc::Model relocModel;
286 llvm::CodeModel::Model codeModel;
287
288 if (triple.isOSBinFormatELF())
289 {
290 relocModel = llvm::Reloc::Static;
291 // This will be small for 32-bit and large for 64-bit.
292 codeModel = llvm::CodeModel::JITDefault;
293 }
294 else
295 {
296 relocModel = llvm::Reloc::PIC_;
297 codeModel = llvm::CodeModel::Small;
298 }
299
300 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
301
302 llvm::EngineBuilder builder(m_module_ap.get());
303
304 builder.setEngineKind(llvm::EngineKind::JIT)
305 .setErrorStr(&error_string)
306 .setRelocationModel(relocModel)
307 .setJITMemoryManager(new MemoryManager(*this))
308 .setOptLevel(llvm::CodeGenOpt::Less)
309 .setAllocateGVsWithCode(true)
310 .setCodeModel(codeModel)
311 .setUseMCJIT(true);
312
313 llvm::StringRef mArch;
314 llvm::StringRef mCPU;
315 llvm::SmallVector<std::string, 0> mAttrs;
316
317 for (std::string &feature : m_cpu_features)
318 mAttrs.push_back(feature);
319
320 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
321 mArch,
322 mCPU,
323 mAttrs);
324
325 m_execution_engine_ap.reset(builder.create(target_machine));
326
327 if (!m_execution_engine_ap.get())
328 {
329 error.SetErrorToGenericError();
330 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000331 return;
332 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000333 else
334 {
335 m_module_ap.release(); // ownership was transferred
336 }
337
Greg Clayton23f8c952014-03-24 23:10:19 +0000338 // Make sure we see all sections, including ones that don't have relocations...
339 m_execution_engine_ap->setProcessAllSections(true);
340
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000341 m_execution_engine_ap->DisableLazyCompilation();
342
343 // We don't actually need the function pointer here, this just forces it to get resolved.
344
345 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
346
347 if (!error.Success())
348 {
349 // We got an error through our callback!
350 return;
351 }
352
353 if (!function)
354 {
355 error.SetErrorToGenericError();
356 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
357 return;
358 }
359
360 if (!fun_ptr)
361 {
362 error.SetErrorToGenericError();
363 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
364 return;
365 }
366
367 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
368
369 CommitAllocations(process_sp);
370 ReportAllocations(*m_execution_engine_ap);
371 WriteData(process_sp);
372
373 for (JittedFunction &jitted_function : m_jitted_functions)
374 {
375 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
376
377 if (!jitted_function.m_name.compare(m_name.AsCString()))
378 {
379 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
380 m_function_end_load_addr = func_range.first + func_range.second;
381 m_function_load_addr = jitted_function.m_remote_addr;
382 }
383 }
384
385 if (log)
386 {
387 log->Printf("Code can be run in the target.");
388
389 StreamString disassembly_stream;
390
391 Error err = DisassembleFunction(disassembly_stream, process_sp);
392
393 if (!err.Success())
394 {
395 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
396 }
397 else
398 {
399 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
400 }
Sean Callanan4f2c1982014-01-21 00:54:48 +0000401
402 log->Printf("Sections: ");
403 for (AllocationRecord &record : m_records)
404 {
405 if (record.m_process_address != LLDB_INVALID_ADDRESS)
406 {
407 record.dump(log);
408
409 DataBufferHeap my_buffer(record.m_size, 0);
410 Error err;
411 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
412
413 if (err.Success())
414 {
415 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
416 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
417 }
418 }
419 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000420 }
421
422 func_addr = m_function_load_addr;
423 func_end = m_function_end_load_addr;
424
425 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000426}
427
428IRExecutionUnit::~IRExecutionUnit ()
429{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000430 m_module_ap.reset();
431 m_execution_engine_ap.reset();
432 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000433}
434
435IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
436 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
437 m_parent (parent)
438{
439}
440
Greg Clayton23f8c952014-03-24 23:10:19 +0000441IRExecutionUnit::MemoryManager::~MemoryManager ()
442{
443}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000444void
445IRExecutionUnit::MemoryManager::setMemoryWritable ()
446{
447 m_default_mm_ap->setMemoryWritable();
448}
449
450void
451IRExecutionUnit::MemoryManager::setMemoryExecutable ()
452{
453 m_default_mm_ap->setMemoryExecutable();
454}
455
456
457uint8_t *
458IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
459 uintptr_t &ActualSize)
460{
461 return m_default_mm_ap->startFunctionBody(F, ActualSize);
462}
463
464uint8_t *
465IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
466 unsigned StubSize,
467 unsigned Alignment)
468{
Greg Clayton5160ce52013-03-27 23:08:40 +0000469 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000470
471 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000472
473 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
474 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000475 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Stub),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000476 StubSize,
Greg Clayton23f8c952014-03-24 23:10:19 +0000477 Alignment,
478 eSectionIDInvalid,
479 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000480
481 if (log)
482 {
483 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
484 F, StubSize, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000485 }
Sean Callanan2c047352013-03-19 23:03:21 +0000486
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000487 return return_value;
488}
489
490void
491IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
492 uint8_t *FunctionStart,
493 uint8_t *FunctionEnd)
494{
495 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
496}
497
Greg Clayton23f8c952014-03-24 23:10:19 +0000498lldb::SectionType
499IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
500{
501 lldb::SectionType sect_type = lldb::eSectionTypeCode;
502 switch (alloc_kind)
503 {
504 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
505 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
506 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
507 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
508 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
509 }
510
511 if (!name.empty())
512 {
513 if (name.equals("__text") || name.equals(".text"))
514 sect_type = lldb::eSectionTypeCode;
515 else if (name.equals("__data") || name.equals(".data"))
516 sect_type = lldb::eSectionTypeCode;
517 else if (name.startswith("__debug_") || name.startswith(".debug_"))
518 {
519 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
520 llvm::StringRef dwarf_name(name.substr(name_idx));
521 switch (dwarf_name[0])
522 {
523 case 'a':
524 if (dwarf_name.equals("abbrev"))
525 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
526 else if (dwarf_name.equals("aranges"))
527 sect_type = lldb::eSectionTypeDWARFDebugAranges;
528 break;
529
530 case 'f':
531 if (dwarf_name.equals("frame"))
532 sect_type = lldb::eSectionTypeDWARFDebugFrame;
533 break;
534
535 case 'i':
536 if (dwarf_name.equals("info"))
537 sect_type = lldb::eSectionTypeDWARFDebugInfo;
538 break;
539
540 case 'l':
541 if (dwarf_name.equals("line"))
542 sect_type = lldb::eSectionTypeDWARFDebugLine;
543 else if (dwarf_name.equals("loc"))
544 sect_type = lldb::eSectionTypeDWARFDebugLoc;
545 break;
546
547 case 'm':
548 if (dwarf_name.equals("macinfo"))
549 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
550 break;
551
552 case 'p':
553 if (dwarf_name.equals("pubnames"))
554 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
555 else if (dwarf_name.equals("pubtypes"))
556 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
557 break;
558
559 case 's':
560 if (dwarf_name.equals("str"))
561 sect_type = lldb::eSectionTypeDWARFDebugStr;
562 break;
563
564 case 'r':
565 if (dwarf_name.equals("ranges"))
566 sect_type = lldb::eSectionTypeDWARFDebugRanges;
567 break;
568
569 default:
570 break;
571 }
572 }
573 else if (name.startswith("__apple_") || name.startswith(".apple_"))
574 {
575#if 0
576 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
577 llvm::StringRef apple_name(name.substr(name_idx));
578 switch (apple_name[0])
579 {
580 case 'n':
581 if (apple_name.equals("names"))
582 sect_type = lldb::eSectionTypeDWARFAppleNames;
583 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
584 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
585 break;
586 case 't':
587 if (apple_name.equals("types"))
588 sect_type = lldb::eSectionTypeDWARFAppleTypes;
589 break;
590 case 'o':
591 if (apple_name.equals("objc"))
592 sect_type = lldb::eSectionTypeDWARFAppleObjC;
593 break;
594 default:
595 break;
596 }
597#else
598 sect_type = lldb::eSectionTypeInvalid;
599#endif
600 }
601 else if (name.equals("__objc_imageinfo"))
602 sect_type = lldb::eSectionTypeOther;
603 }
604 return sect_type;
605}
606
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000607uint8_t *
608IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
609{
Greg Clayton5160ce52013-03-27 23:08:40 +0000610 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000611
612 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
613
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000614 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
615 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000616 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Bytes),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000617 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000618 Alignment,
619 eSectionIDInvalid,
620 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000621
622 if (log)
623 {
624 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
625 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000626 }
Sean Callanan2c047352013-03-19 23:03:21 +0000627
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000628 return return_value;
629}
630
631uint8_t *
632IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
633 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000634 unsigned SectionID,
635 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000636{
Greg Clayton5160ce52013-03-27 23:08:40 +0000637 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000638
Filip Pizlobfcff682013-10-02 01:43:46 +0000639 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000640
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000641 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000642 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000643 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000644 Size,
645 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000646 SectionID,
647 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000648
649 if (log)
650 {
651 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
652 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000653 }
Sean Callanan2c047352013-03-19 23:03:21 +0000654
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000655 return return_value;
656}
657
658uint8_t *
659IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
660 unsigned Alignment,
661 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000662 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000663 bool IsReadOnly)
664{
Greg Clayton5160ce52013-03-27 23:08:40 +0000665 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000666
Filip Pizlobfcff682013-10-02 01:43:46 +0000667 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000668
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000669 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000670 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
671 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000672 Size,
673 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000674 SectionID,
675 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000676 if (log)
677 {
678 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
679 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000680 }
Sean Callanan2c047352013-03-19 23:03:21 +0000681
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000682 return return_value;
683}
684
685uint8_t *
686IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
687 unsigned Alignment)
688{
Greg Clayton5160ce52013-03-27 23:08:40 +0000689 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000690
691 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
692
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000693 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
694 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000695 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Global),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000696 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000697 Alignment,
698 eSectionIDInvalid,
699 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700
701 if (log)
702 {
703 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
704 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000705 }
706
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000707 return return_value;
708}
709
710void
711IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
712{
713 m_default_mm_ap->deallocateFunctionBody(Body);
714}
715
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000716lldb::addr_t
717IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
718{
Sean Callananffae9442013-06-27 01:42:47 +0000719 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
720
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000721 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000722 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000723 if (local_address >= record.m_host_address &&
724 local_address < record.m_host_address + record.m_size)
725 {
726 if (record.m_process_address == LLDB_INVALID_ADDRESS)
727 return LLDB_INVALID_ADDRESS;
Sean Callananffae9442013-06-27 01:42:47 +0000728
729 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
730
731 if (log)
732 {
733 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
734 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000735 (uint64_t)record.m_host_address,
736 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000737 ret,
738 record.m_process_address,
739 record.m_process_address + record.m_size);
740 }
741
742 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000743 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000744 }
745
746 return LLDB_INVALID_ADDRESS;
747}
748
749IRExecutionUnit::AddrRange
750IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
751{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000752 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000753 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000754 if (local_address >= record.m_host_address &&
755 local_address < record.m_host_address + record.m_size)
756 {
757 if (record.m_process_address == LLDB_INVALID_ADDRESS)
758 return AddrRange(0, 0);
759
760 return AddrRange(record.m_process_address, record.m_size);
761 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000762 }
763
764 return AddrRange (0, 0);
765}
766
767bool
768IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
769{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000770 bool ret = true;
771
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000772 lldb_private::Error err;
773
774 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000775 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000776 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000777 continue;
778
Greg Clayton23f8c952014-03-24 23:10:19 +0000779 switch (record.m_sect_type)
780 {
781 case lldb::eSectionTypeInvalid:
782 case lldb::eSectionTypeDWARFDebugAbbrev:
783 case lldb::eSectionTypeDWARFDebugAranges:
784 case lldb::eSectionTypeDWARFDebugFrame:
785 case lldb::eSectionTypeDWARFDebugInfo:
786 case lldb::eSectionTypeDWARFDebugLine:
787 case lldb::eSectionTypeDWARFDebugLoc:
788 case lldb::eSectionTypeDWARFDebugMacInfo:
789 case lldb::eSectionTypeDWARFDebugPubNames:
790 case lldb::eSectionTypeDWARFDebugPubTypes:
791 case lldb::eSectionTypeDWARFDebugRanges:
792 case lldb::eSectionTypeDWARFDebugStr:
793 case lldb::eSectionTypeDWARFAppleNames:
794 case lldb::eSectionTypeDWARFAppleTypes:
795 case lldb::eSectionTypeDWARFAppleNamespaces:
796 case lldb::eSectionTypeDWARFAppleObjC:
797 err.Clear();
798 break;
799 default:
800 record.m_process_address = Malloc (record.m_size,
801 record.m_alignment,
802 record.m_permissions,
803 eAllocationPolicyProcessOnly,
804 err);
805 break;
806 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000807
808 if (!err.Success())
809 {
810 ret = false;
811 break;
812 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000813 }
814
815 if (!ret)
816 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000817 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000818 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000819 if (record.m_process_address != LLDB_INVALID_ADDRESS)
820 {
821 Free(record.m_process_address, err);
822 record.m_process_address = LLDB_INVALID_ADDRESS;
823 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000824 }
825 }
826
827 return ret;
828}
829
830void
831IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
832{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000833 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000834 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000835 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000836 continue;
837
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000838 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000839 continue;
840
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000841 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000842 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000843
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000844 // Trigger re-application of relocations.
845 engine.finalizeObject();
846}
847
848bool
849IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
850{
Greg Clayton23f8c952014-03-24 23:10:19 +0000851 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000852 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000853 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000854 if (record.m_process_address != LLDB_INVALID_ADDRESS)
855 {
856 lldb_private::Error err;
857 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
858 if (err.Success())
859 wrote_something = true;
860 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000861 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000862 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000863}
864
865void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000866IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000867{
868 if (!log)
869 return;
870
871 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000872 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000873 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000874 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000875 (unsigned)m_alignment,
876 (unsigned)m_section_id);
877}
Greg Clayton23f8c952014-03-24 23:10:19 +0000878
879
880lldb::ByteOrder
881IRExecutionUnit::GetByteOrder () const
882{
883 ExecutionContext exe_ctx (GetBestExecutionContextScope());
884 return exe_ctx.GetByteOrder();
885}
886
887uint32_t
888IRExecutionUnit::GetAddressByteSize () const
889{
890 ExecutionContext exe_ctx (GetBestExecutionContextScope());
891 return exe_ctx.GetAddressByteSize();
892}
893
894void
895IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
896 lldb_private::Symtab &symtab)
897{
898 // No symbols yet...
899}
900
901
902void
903IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
904 lldb_private::SectionList &section_list)
905{
906 for (AllocationRecord &record : m_records)
907 {
908 if (record.m_size > 0)
909 {
910 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
911 obj_file,
912 record.m_section_id,
913 ConstString(record.m_name),
914 record.m_sect_type,
915 record.m_process_address,
916 record.m_size,
917 record.m_host_address, // file_offset (which is the host address for the data)
918 record.m_size, // file_size
919 record.m_permissions)); // flags
920 section_list.AddSection (section_sp);
921 }
922 }
923}
924
925bool
926IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
927{
928 ExecutionContext exe_ctx (GetBestExecutionContextScope());
929 Target *target = exe_ctx.GetTargetPtr();
930 if (target)
931 arch = target->GetArchitecture();
932 else
933 arch.Clear();
934 return arch.IsValid();
935}
936
937lldb::ModuleSP
938IRExecutionUnit::GetJITModule ()
939{
940 ExecutionContext exe_ctx (GetBestExecutionContextScope());
941 Target *target = exe_ctx.GetTargetPtr();
942 if (target)
943 {
944 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
945 if (jit_module_sp)
946 {
947 bool changed = false;
948 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
949 }
950 return jit_module_sp;
951 }
952 return lldb::ModuleSP();
953}