blob: 73974f720287cc932b00138c86b96e5cba791d34 [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
Sean Callanan8dfb68e2013-03-19 00:10:07 +000010#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000011#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000012#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000013#include "llvm/Support/SourceMgr.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000014#include "lldb/Core/DataBufferHeap.h"
15#include "lldb/Core/DataExtractor.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000016#include "lldb/Core/Debugger.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000017#include "lldb/Core/Disassembler.h"
18#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000019#include "lldb/Core/Module.h"
20#include "lldb/Core/Section.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000021#include "lldb/Expression/IRExecutionUnit.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/Target.h"
24
25using namespace lldb_private;
26
Greg Clayton7b0992d2013-04-18 22:45:39 +000027IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
28 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000029 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000030 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000031 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000032 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000033 m_context_ap(context_ap.release()),
34 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000035 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000036 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000037 m_name(name),
38 m_did_jit(false),
39 m_function_load_addr(LLDB_INVALID_ADDRESS),
40 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
41{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000042}
43
44lldb::addr_t
45IRExecutionUnit::WriteNow (const uint8_t *bytes,
46 size_t size,
47 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000048{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000049 lldb::addr_t allocation_process_addr = Malloc (size,
50 8,
51 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
52 eAllocationPolicyMirror,
53 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000054
Sean Callanan5a1af4e2013-04-05 02:22:57 +000055 if (!error.Success())
56 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000057
Sean Callanan5a1af4e2013-04-05 02:22:57 +000058 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000059
Sean Callanan5a1af4e2013-04-05 02:22:57 +000060 if (!error.Success())
61 {
62 Error err;
63 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000064
Sean Callanan5a1af4e2013-04-05 02:22:57 +000065 return LLDB_INVALID_ADDRESS;
66 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000067
Sean Callanan5a1af4e2013-04-05 02:22:57 +000068 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
69 {
70 DataBufferHeap my_buffer(size, 0);
71 Error err;
72 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000073
Sean Callanan5a1af4e2013-04-05 02:22:57 +000074 if (err.Success())
75 {
76 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000077 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000078 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000079 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanan5a1af4e2013-04-05 02:22:57 +000081 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000082}
83
84void
85IRExecutionUnit::FreeNow (lldb::addr_t allocation)
86{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000087 if (allocation == LLDB_INVALID_ADDRESS)
88 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089
Sean Callanan5a1af4e2013-04-05 02:22:57 +000090 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000091
Sean Callanan5a1af4e2013-04-05 02:22:57 +000092 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000093}
94
95Error
96IRExecutionUnit::DisassembleFunction (Stream &stream,
97 lldb::ProcessSP &process_wp)
98{
Greg Clayton5160ce52013-03-27 23:08:40 +000099 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000100
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000101 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000102
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000103 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000104
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000105 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000106
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000107 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
108 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000110 for (JittedFunction &function : m_jitted_functions)
111 {
112 if (strstr(function.m_name.c_str(), m_name.AsCString()))
113 {
114 func_local_addr = function.m_local_addr;
115 func_remote_addr = function.m_remote_addr;
116 }
117 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000118
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000119 if (func_local_addr == LLDB_INVALID_ADDRESS)
120 {
121 ret.SetErrorToGenericError();
122 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
123 return ret;
124 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000125
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000126 if (log)
127 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000128
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000129 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000130
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000131 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000132
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000133 if (func_range.first == 0 && func_range.second == 0)
134 {
135 ret.SetErrorToGenericError();
136 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
137 return ret;
138 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000139
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000140 if (log)
141 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000142
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000143 Target *target = exe_ctx.GetTargetPtr();
144 if (!target)
145 {
146 ret.SetErrorToGenericError();
147 ret.SetErrorString("Couldn't find the target");
148 return ret;
149 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000150
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000151 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000152
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000153 Process *process = exe_ctx.GetProcessPtr();
154 Error err;
155 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000157 if (!err.Success())
158 {
159 ret.SetErrorToGenericError();
160 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
161 return ret;
162 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000163
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000164 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000165
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000166 const char *plugin_name = NULL;
167 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000168 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169
Jim Ingham56d40422013-07-31 02:19:15 +0000170 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000171 {
172 ret.SetErrorToGenericError();
173 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
174 return ret;
175 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000176
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000177 if (!process)
178 {
179 ret.SetErrorToGenericError();
180 ret.SetErrorString("Couldn't find the process");
181 return ret;
182 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000183
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000184 DataExtractor extractor(buffer_sp,
185 process->GetByteOrder(),
186 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000188 if (log)
189 {
190 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000191 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000192 0,
193 extractor.GetByteSize(),
194 func_remote_addr,
195 16,
196 DataExtractor::TypeUInt8);
197 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000198
Jim Ingham56d40422013-07-31 02:19:15 +0000199 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000200
Jim Ingham56d40422013-07-31 02:19:15 +0000201 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000202 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
Jason Molendaaff1b352014-10-10 23:07:36 +0000203 const char *disassemble_format = "${addr-file-or-load}: ";
204 if (exe_ctx.HasTargetScope())
205 {
206 disassemble_format = exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat();
207 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000209 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
210 instruction_index < num_instructions;
211 ++instruction_index)
212 {
213 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
214 instruction->Dump (&stream,
215 max_opcode_byte_size,
216 true,
217 true,
Jason Molendaaff1b352014-10-10 23:07:36 +0000218 &exe_ctx,
219 NULL,
220 NULL,
221 disassemble_format);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000222 stream.PutChar('\n');
223 }
Jim Ingham56d40422013-07-31 02:19:15 +0000224 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
225 // I'll fix that but for now, just clear the list and it will go away nicely.
226 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000227 return ret;
228}
229
Sean Callanan44bc6572013-03-27 03:09:55 +0000230static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
231{
232 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000233
Sean Callanan44bc6572013-03-27 03:09:55 +0000234 if (err && err->Success())
235 {
236 err->SetErrorToGenericError();
237 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
238 }
239}
240
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241void
Jim Ingham27e5fe82015-01-27 18:03:05 +0000242IRExecutionUnit::ReportSymbolLookupError(const ConstString &name)
243{
244 m_failed_lookups.push_back(name);
245}
246
247void
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000248IRExecutionUnit::GetRunnableInfo(Error &error,
249 lldb::addr_t &func_addr,
250 lldb::addr_t &func_end)
251{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000252 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000253
Sean Callanan7c435a52014-02-06 22:25:20 +0000254 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000255
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000256 func_addr = LLDB_INVALID_ADDRESS;
257 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000259 if (!process_sp)
260 {
261 error.SetErrorToGenericError();
262 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
263 return;
264 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000265
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000266 if (m_did_jit)
267 {
268 func_addr = m_function_load_addr;
269 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000270
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000271 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000272 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000273
Sean Callanan7c435a52014-02-06 22:25:20 +0000274 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000277
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000279
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000280 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000281
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000282 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000283 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000284 std::string s;
285 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000286
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000287 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000288
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000289 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000290
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000291 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
292 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000293
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000294 llvm::Triple triple(m_module->getTargetTriple());
295 llvm::Function *function = m_module->getFunction (m_name.AsCString());
296 llvm::Reloc::Model relocModel;
297 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000298
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000299 if (triple.isOSBinFormatELF())
300 {
301 relocModel = llvm::Reloc::Static;
302 // This will be small for 32-bit and large for 64-bit.
303 codeModel = llvm::CodeModel::JITDefault;
304 }
305 else
306 {
307 relocModel = llvm::Reloc::PIC_;
308 codeModel = llvm::CodeModel::Small;
309 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000310
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000311 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Rafael Espindola7c03d952014-08-19 04:27:03 +0000313 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000314
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000315 builder.setEngineKind(llvm::EngineKind::JIT)
316 .setErrorStr(&error_string)
317 .setRelocationModel(relocModel)
David Majnemerb313e462014-12-04 21:26:25 +0000318 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
David Majnemer8faf9372014-09-16 06:34:29 +0000319 .setCodeModel(codeModel)
Reid Kleckner54209532014-09-03 00:40:36 +0000320 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000321
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000322 llvm::StringRef mArch;
323 llvm::StringRef mCPU;
324 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000325
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000326 for (std::string &feature : m_cpu_features)
327 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000328
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000329 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
330 mArch,
331 mCPU,
332 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000333
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000334 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000335
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000336 if (!m_execution_engine_ap.get())
337 {
338 error.SetErrorToGenericError();
339 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000340 return;
341 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000342
Greg Clayton23f8c952014-03-24 23:10:19 +0000343 // Make sure we see all sections, including ones that don't have relocations...
344 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000345
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000346 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000348 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000350 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000351
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000352 if (!error.Success())
353 {
354 // We got an error through our callback!
355 return;
356 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000358 if (!function)
359 {
360 error.SetErrorToGenericError();
361 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
362 return;
363 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000364
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000365 if (!fun_ptr)
366 {
367 error.SetErrorToGenericError();
368 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
369 return;
370 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000372 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000374 CommitAllocations(process_sp);
375 ReportAllocations(*m_execution_engine_ap);
376 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000377
Jim Ingham27e5fe82015-01-27 18:03:05 +0000378 if (m_failed_lookups.size())
379 {
380 StreamString ss;
381
382 ss.PutCString("Couldn't lookup symbols:\n");
383
384 bool emitNewLine = false;
385
386 for (const ConstString &failed_lookup : m_failed_lookups)
387 {
388 if (emitNewLine)
389 ss.PutCString("\n");
390 emitNewLine = true;
391 ss.PutCString(" ");
392 ss.PutCString(Mangled(failed_lookup).GetDemangledName().AsCString());
393 }
394
395 m_failed_lookups.clear();
396
397 error.SetErrorString(ss.GetData());
398
399 return;
400 }
401
402 m_function_load_addr = LLDB_INVALID_ADDRESS;
403 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
404
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000405 for (JittedFunction &jitted_function : m_jitted_functions)
406 {
407 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000409 if (!jitted_function.m_name.compare(m_name.AsCString()))
410 {
411 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
412 m_function_end_load_addr = func_range.first + func_range.second;
413 m_function_load_addr = jitted_function.m_remote_addr;
414 }
415 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000416
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000417 if (log)
418 {
419 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000420
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000421 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000423 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000424
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000425 if (!err.Success())
426 {
427 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
428 }
429 else
430 {
431 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
432 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000433
Sean Callanan4f2c1982014-01-21 00:54:48 +0000434 log->Printf("Sections: ");
435 for (AllocationRecord &record : m_records)
436 {
437 if (record.m_process_address != LLDB_INVALID_ADDRESS)
438 {
439 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callanan4f2c1982014-01-21 00:54:48 +0000441 DataBufferHeap my_buffer(record.m_size, 0);
442 Error err;
443 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444
Sean Callanan4f2c1982014-01-21 00:54:48 +0000445 if (err.Success())
446 {
447 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
448 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
449 }
450 }
451 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000452 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000454 func_addr = m_function_load_addr;
455 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000456
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000457 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000458}
459
460IRExecutionUnit::~IRExecutionUnit ()
461{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000462 m_module_ap.reset();
463 m_execution_engine_ap.reset();
464 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000465}
466
467IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000468 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000469 m_parent (parent)
470{
471}
472
Greg Clayton23f8c952014-03-24 23:10:19 +0000473IRExecutionUnit::MemoryManager::~MemoryManager ()
474{
475}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000476
Greg Clayton23f8c952014-03-24 23:10:19 +0000477lldb::SectionType
478IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
479{
480 lldb::SectionType sect_type = lldb::eSectionTypeCode;
481 switch (alloc_kind)
482 {
483 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
484 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
485 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
486 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
487 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
488 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000489
Greg Clayton23f8c952014-03-24 23:10:19 +0000490 if (!name.empty())
491 {
492 if (name.equals("__text") || name.equals(".text"))
493 sect_type = lldb::eSectionTypeCode;
494 else if (name.equals("__data") || name.equals(".data"))
495 sect_type = lldb::eSectionTypeCode;
496 else if (name.startswith("__debug_") || name.startswith(".debug_"))
497 {
498 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
499 llvm::StringRef dwarf_name(name.substr(name_idx));
500 switch (dwarf_name[0])
501 {
502 case 'a':
503 if (dwarf_name.equals("abbrev"))
504 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
505 else if (dwarf_name.equals("aranges"))
506 sect_type = lldb::eSectionTypeDWARFDebugAranges;
507 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Greg Clayton23f8c952014-03-24 23:10:19 +0000509 case 'f':
510 if (dwarf_name.equals("frame"))
511 sect_type = lldb::eSectionTypeDWARFDebugFrame;
512 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000513
Greg Clayton23f8c952014-03-24 23:10:19 +0000514 case 'i':
515 if (dwarf_name.equals("info"))
516 sect_type = lldb::eSectionTypeDWARFDebugInfo;
517 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000518
Greg Clayton23f8c952014-03-24 23:10:19 +0000519 case 'l':
520 if (dwarf_name.equals("line"))
521 sect_type = lldb::eSectionTypeDWARFDebugLine;
522 else if (dwarf_name.equals("loc"))
523 sect_type = lldb::eSectionTypeDWARFDebugLoc;
524 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000525
Greg Clayton23f8c952014-03-24 23:10:19 +0000526 case 'm':
527 if (dwarf_name.equals("macinfo"))
528 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
529 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000530
Greg Clayton23f8c952014-03-24 23:10:19 +0000531 case 'p':
532 if (dwarf_name.equals("pubnames"))
533 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
534 else if (dwarf_name.equals("pubtypes"))
535 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
536 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000537
Greg Clayton23f8c952014-03-24 23:10:19 +0000538 case 's':
539 if (dwarf_name.equals("str"))
540 sect_type = lldb::eSectionTypeDWARFDebugStr;
541 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000542
Greg Clayton23f8c952014-03-24 23:10:19 +0000543 case 'r':
544 if (dwarf_name.equals("ranges"))
545 sect_type = lldb::eSectionTypeDWARFDebugRanges;
546 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000547
Greg Clayton23f8c952014-03-24 23:10:19 +0000548 default:
549 break;
550 }
551 }
552 else if (name.startswith("__apple_") || name.startswith(".apple_"))
553 {
554#if 0
555 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
556 llvm::StringRef apple_name(name.substr(name_idx));
557 switch (apple_name[0])
558 {
559 case 'n':
560 if (apple_name.equals("names"))
561 sect_type = lldb::eSectionTypeDWARFAppleNames;
562 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
563 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
564 break;
565 case 't':
566 if (apple_name.equals("types"))
567 sect_type = lldb::eSectionTypeDWARFAppleTypes;
568 break;
569 case 'o':
570 if (apple_name.equals("objc"))
571 sect_type = lldb::eSectionTypeDWARFAppleObjC;
572 break;
573 default:
574 break;
575 }
576#else
577 sect_type = lldb::eSectionTypeInvalid;
578#endif
579 }
580 else if (name.equals("__objc_imageinfo"))
581 sect_type = lldb::eSectionTypeOther;
582 }
583 return sect_type;
584}
585
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000586uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000587IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
588 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000589 unsigned SectionID,
590 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000591{
Greg Clayton5160ce52013-03-27 23:08:40 +0000592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000593
Filip Pizlobfcff682013-10-02 01:43:46 +0000594 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000595
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000596 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000597 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000598 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000599 Size,
600 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000601 SectionID,
602 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000603
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000604 if (log)
605 {
606 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
607 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000608 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000609
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000610 return return_value;
611}
612
613uint8_t *
614IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
615 unsigned Alignment,
616 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000617 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000618 bool IsReadOnly)
619{
Greg Clayton5160ce52013-03-27 23:08:40 +0000620 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000621
Filip Pizlobfcff682013-10-02 01:43:46 +0000622 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000623
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000624 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000625 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
626 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000627 Size,
628 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000629 SectionID,
630 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000631 if (log)
632 {
633 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
634 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000635 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
637 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000638}
639
Jim Ingham27e5fe82015-01-27 18:03:05 +0000640uint64_t
641IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name)
642{
643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
644
645 SymbolContextList sc_list;
646
647 ExecutionContextScope *exe_scope = m_parent.GetBestExecutionContextScope();
648
649 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
650
651 const char *name = Name.c_str();
652
653 ConstString bare_name_cs(name);
654 ConstString name_cs;
655
656 if (name[0] == '_')
657 name_cs = ConstString(name + 1);
658
659 if (!target_sp)
660 {
661 if (log)
662 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <no target>",
663 Name.c_str());
664
665 m_parent.ReportSymbolLookupError(name_cs);
666
667 return 0xbad0bad0;
668 }
669
670 uint32_t num_matches = 0;
671 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
672
673 if (!name_cs.IsEmpty())
674 {
675 target_sp->GetImages().FindSymbolsWithNameAndType(name_cs, lldb::eSymbolTypeAny, sc_list);
676 num_matches = sc_list.GetSize();
677 }
678
679 if (!num_matches)
680 {
681 target_sp->GetImages().FindSymbolsWithNameAndType(bare_name_cs, lldb::eSymbolTypeAny, sc_list);
682 num_matches = sc_list.GetSize();
683 }
684
685 lldb::addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
686
687 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
688 {
689 SymbolContext sym_ctx;
690 sc_list.GetContextAtIndex(i, sym_ctx);
691
692 if (sym_ctx.symbol->GetType() == lldb::eSymbolTypeUndefined)
693 continue;
694
695 const Address *sym_address = &sym_ctx.symbol->GetAddress();
696
697 if (!sym_address || !sym_address->IsValid())
698 continue;
699
700 symbol_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target_sp);
701
702 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
703 {
704 symbol_load_addr = sym_ctx.symbol->GetAddress().GetLoadAddress(target_sp.get());
705 }
706 }
707
708 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process_sp && name_cs)
709 {
710 // Try the Objective-C language runtime.
711
712 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
713
714 if (runtime)
715 symbol_load_addr = runtime->LookupRuntimeSymbol(name_cs);
716 }
717
718 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
719 {
720 if (log)
721 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
722 name);
723
724 m_parent.ReportSymbolLookupError(bare_name_cs);
725
726 return 0xbad0bad0;
727 }
728
729 if (log)
730 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
731 name,
732 symbol_load_addr);
733
734 if (symbol_load_addr == 0)
735 return 0xbad00add;
736
737 return symbol_load_addr;
738}
739
740void *
741IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name,
742 bool AbortOnFailure) {
743 assert (sizeof(void *) == 8);
744
745 return (void*)getSymbolAddress(Name);
746}
747
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000748lldb::addr_t
749IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
750{
Sean Callananffae9442013-06-27 01:42:47 +0000751 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
752
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000753 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000754 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000755 if (local_address >= record.m_host_address &&
756 local_address < record.m_host_address + record.m_size)
757 {
758 if (record.m_process_address == LLDB_INVALID_ADDRESS)
759 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000760
Sean Callananffae9442013-06-27 01:42:47 +0000761 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000762
Sean Callananffae9442013-06-27 01:42:47 +0000763 if (log)
764 {
765 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
766 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000767 (uint64_t)record.m_host_address,
768 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000769 ret,
770 record.m_process_address,
771 record.m_process_address + record.m_size);
772 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000773
Sean Callananffae9442013-06-27 01:42:47 +0000774 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000775 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000776 }
777
778 return LLDB_INVALID_ADDRESS;
779}
780
781IRExecutionUnit::AddrRange
782IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
783{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000784 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000785 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000786 if (local_address >= record.m_host_address &&
787 local_address < record.m_host_address + record.m_size)
788 {
789 if (record.m_process_address == LLDB_INVALID_ADDRESS)
790 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000791
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000792 return AddrRange(record.m_process_address, record.m_size);
793 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000794 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000795
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000796 return AddrRange (0, 0);
797}
798
799bool
800IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
801{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000802 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000803
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000804 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000805
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000806 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000807 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000808 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000809 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000810
Greg Clayton23f8c952014-03-24 23:10:19 +0000811 switch (record.m_sect_type)
812 {
813 case lldb::eSectionTypeInvalid:
814 case lldb::eSectionTypeDWARFDebugAbbrev:
815 case lldb::eSectionTypeDWARFDebugAranges:
816 case lldb::eSectionTypeDWARFDebugFrame:
817 case lldb::eSectionTypeDWARFDebugInfo:
818 case lldb::eSectionTypeDWARFDebugLine:
819 case lldb::eSectionTypeDWARFDebugLoc:
820 case lldb::eSectionTypeDWARFDebugMacInfo:
821 case lldb::eSectionTypeDWARFDebugPubNames:
822 case lldb::eSectionTypeDWARFDebugPubTypes:
823 case lldb::eSectionTypeDWARFDebugRanges:
824 case lldb::eSectionTypeDWARFDebugStr:
825 case lldb::eSectionTypeDWARFAppleNames:
826 case lldb::eSectionTypeDWARFAppleTypes:
827 case lldb::eSectionTypeDWARFAppleNamespaces:
828 case lldb::eSectionTypeDWARFAppleObjC:
829 err.Clear();
830 break;
831 default:
832 record.m_process_address = Malloc (record.m_size,
833 record.m_alignment,
834 record.m_permissions,
835 eAllocationPolicyProcessOnly,
836 err);
837 break;
838 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000839
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000840 if (!err.Success())
841 {
842 ret = false;
843 break;
844 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000845 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000846
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000847 if (!ret)
848 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000849 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000850 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000851 if (record.m_process_address != LLDB_INVALID_ADDRESS)
852 {
853 Free(record.m_process_address, err);
854 record.m_process_address = LLDB_INVALID_ADDRESS;
855 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000856 }
857 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000858
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000859 return ret;
860}
861
862void
863IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
864{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000865 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000866 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000867 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000868 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000869
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000870 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000871 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000872
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000873 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000874 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000875
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000876 // Trigger re-application of relocations.
877 engine.finalizeObject();
878}
879
880bool
881IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
882{
Greg Clayton23f8c952014-03-24 23:10:19 +0000883 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000884 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000885 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000886 if (record.m_process_address != LLDB_INVALID_ADDRESS)
887 {
888 lldb_private::Error err;
889 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
890 if (err.Success())
891 wrote_something = true;
892 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000893 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000894 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000895}
896
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000897void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000898IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000899{
900 if (!log)
901 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000902
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000903 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000904 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000905 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000906 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000907 (unsigned)m_alignment,
908 (unsigned)m_section_id);
909}
Greg Clayton23f8c952014-03-24 23:10:19 +0000910
911
912lldb::ByteOrder
913IRExecutionUnit::GetByteOrder () const
914{
915 ExecutionContext exe_ctx (GetBestExecutionContextScope());
916 return exe_ctx.GetByteOrder();
917}
918
919uint32_t
920IRExecutionUnit::GetAddressByteSize () const
921{
922 ExecutionContext exe_ctx (GetBestExecutionContextScope());
923 return exe_ctx.GetAddressByteSize();
924}
925
926void
927IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
928 lldb_private::Symtab &symtab)
929{
930 // No symbols yet...
931}
932
933
934void
935IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
936 lldb_private::SectionList &section_list)
937{
938 for (AllocationRecord &record : m_records)
939 {
940 if (record.m_size > 0)
941 {
942 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
943 obj_file,
944 record.m_section_id,
945 ConstString(record.m_name),
946 record.m_sect_type,
947 record.m_process_address,
948 record.m_size,
949 record.m_host_address, // file_offset (which is the host address for the data)
950 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000951 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000952 record.m_permissions)); // flags
953 section_list.AddSection (section_sp);
954 }
955 }
956}
957
958bool
959IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
960{
961 ExecutionContext exe_ctx (GetBestExecutionContextScope());
962 Target *target = exe_ctx.GetTargetPtr();
963 if (target)
964 arch = target->GetArchitecture();
965 else
966 arch.Clear();
967 return arch.IsValid();
968}
969
970lldb::ModuleSP
971IRExecutionUnit::GetJITModule ()
972{
973 ExecutionContext exe_ctx (GetBestExecutionContextScope());
974 Target *target = exe_ctx.GetTargetPtr();
975 if (target)
976 {
977 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
978 if (jit_module_sp)
979 {
980 bool changed = false;
981 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
982 }
983 return jit_module_sp;
984 }
985 return lldb::ModuleSP();
986}