blob: fb8ea1a358757b45a66a1dc35947da4ed3a06746 [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"
16#include "lldb/Core/Disassembler.h"
17#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/Section.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000020#include "lldb/Expression/IRExecutionUnit.h"
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Target.h"
23
24using namespace lldb_private;
25
Greg Clayton7b0992d2013-04-18 22:45:39 +000026IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
27 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000029 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000030 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000031 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000032 m_context_ap(context_ap.release()),
33 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000035 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000036 m_name(name),
37 m_did_jit(false),
38 m_function_load_addr(LLDB_INVALID_ADDRESS),
39 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
40{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000041}
42
43lldb::addr_t
44IRExecutionUnit::WriteNow (const uint8_t *bytes,
45 size_t size,
46 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000047{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000048 lldb::addr_t allocation_process_addr = Malloc (size,
49 8,
50 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
51 eAllocationPolicyMirror,
52 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000053
Sean Callanan5a1af4e2013-04-05 02:22:57 +000054 if (!error.Success())
55 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000056
Sean Callanan5a1af4e2013-04-05 02:22:57 +000057 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000058
Sean Callanan5a1af4e2013-04-05 02:22:57 +000059 if (!error.Success())
60 {
61 Error err;
62 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Sean Callanan5a1af4e2013-04-05 02:22:57 +000064 return LLDB_INVALID_ADDRESS;
65 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000066
Sean Callanan5a1af4e2013-04-05 02:22:57 +000067 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
68 {
69 DataBufferHeap my_buffer(size, 0);
70 Error err;
71 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000072
Sean Callanan5a1af4e2013-04-05 02:22:57 +000073 if (err.Success())
74 {
75 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000076 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000077 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000078 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000079
Sean Callanan5a1af4e2013-04-05 02:22:57 +000080 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000081}
82
83void
84IRExecutionUnit::FreeNow (lldb::addr_t allocation)
85{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000086 if (allocation == LLDB_INVALID_ADDRESS)
87 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000088
Sean Callanan5a1af4e2013-04-05 02:22:57 +000089 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000090
Sean Callanan5a1af4e2013-04-05 02:22:57 +000091 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000092}
93
94Error
95IRExecutionUnit::DisassembleFunction (Stream &stream,
96 lldb::ProcessSP &process_wp)
97{
Greg Clayton5160ce52013-03-27 23:08:40 +000098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000099
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000100 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000101
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000102 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000103
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000104 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000105
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000106 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
107 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109 for (JittedFunction &function : m_jitted_functions)
110 {
111 if (strstr(function.m_name.c_str(), m_name.AsCString()))
112 {
113 func_local_addr = function.m_local_addr;
114 func_remote_addr = function.m_remote_addr;
115 }
116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000118 if (func_local_addr == LLDB_INVALID_ADDRESS)
119 {
120 ret.SetErrorToGenericError();
121 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
122 return ret;
123 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000125 if (log)
126 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 +0000127
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000128 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000130 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000131
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000132 if (func_range.first == 0 && func_range.second == 0)
133 {
134 ret.SetErrorToGenericError();
135 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
136 return ret;
137 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000138
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000139 if (log)
140 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000141
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000142 Target *target = exe_ctx.GetTargetPtr();
143 if (!target)
144 {
145 ret.SetErrorToGenericError();
146 ret.SetErrorString("Couldn't find the target");
147 return ret;
148 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000149
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000150 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000151
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000152 Process *process = exe_ctx.GetProcessPtr();
153 Error err;
154 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000155
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000156 if (!err.Success())
157 {
158 ret.SetErrorToGenericError();
159 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
160 return ret;
161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000163 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000164
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000165 const char *plugin_name = NULL;
166 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000167 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000168
Jim Ingham56d40422013-07-31 02:19:15 +0000169 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000170 {
171 ret.SetErrorToGenericError();
172 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
173 return ret;
174 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000176 if (!process)
177 {
178 ret.SetErrorToGenericError();
179 ret.SetErrorString("Couldn't find the process");
180 return ret;
181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000182
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000183 DataExtractor extractor(buffer_sp,
184 process->GetByteOrder(),
185 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000186
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000187 if (log)
188 {
189 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000190 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000191 0,
192 extractor.GetByteSize(),
193 func_remote_addr,
194 16,
195 DataExtractor::TypeUInt8);
196 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197
Jim Ingham56d40422013-07-31 02:19:15 +0000198 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000199
Jim Ingham56d40422013-07-31 02:19:15 +0000200 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000201 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000203 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
204 instruction_index < num_instructions;
205 ++instruction_index)
206 {
207 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
208 instruction->Dump (&stream,
209 max_opcode_byte_size,
210 true,
211 true,
212 &exe_ctx);
213 stream.PutChar('\n');
214 }
Jim Ingham56d40422013-07-31 02:19:15 +0000215 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
216 // I'll fix that but for now, just clear the list and it will go away nicely.
217 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000218 return ret;
219}
220
Sean Callanan44bc6572013-03-27 03:09:55 +0000221static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
222{
223 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan44bc6572013-03-27 03:09:55 +0000225 if (err && err->Success())
226 {
227 err->SetErrorToGenericError();
228 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
229 }
230}
231
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000232void
233IRExecutionUnit::GetRunnableInfo(Error &error,
234 lldb::addr_t &func_addr,
235 lldb::addr_t &func_end)
236{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000237 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan7c435a52014-02-06 22:25:20 +0000239 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241 func_addr = LLDB_INVALID_ADDRESS;
242 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000244 if (!process_sp)
245 {
246 error.SetErrorToGenericError();
247 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
248 return;
249 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000250
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000251 if (m_did_jit)
252 {
253 func_addr = m_function_load_addr;
254 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000255
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000256 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000257 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Sean Callanan7c435a52014-02-06 22:25:20 +0000259 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000260
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000261 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000262
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000264
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000265 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000267 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000268 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000269 std::string s;
270 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000272 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000273
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000274 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000279 llvm::Triple triple(m_module->getTargetTriple());
280 llvm::Function *function = m_module->getFunction (m_name.AsCString());
281 llvm::Reloc::Model relocModel;
282 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000283
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000284 if (triple.isOSBinFormatELF())
285 {
286 relocModel = llvm::Reloc::Static;
287 // This will be small for 32-bit and large for 64-bit.
288 codeModel = llvm::CodeModel::JITDefault;
289 }
290 else
291 {
292 relocModel = llvm::Reloc::PIC_;
293 codeModel = llvm::CodeModel::Small;
294 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000295
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000296 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000298 llvm::EngineBuilder builder(m_module_ap.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000299
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000300 builder.setEngineKind(llvm::EngineKind::JIT)
301 .setErrorStr(&error_string)
302 .setRelocationModel(relocModel)
303 .setJITMemoryManager(new MemoryManager(*this))
304 .setOptLevel(llvm::CodeGenOpt::Less)
Zachary Turnerc3494342014-08-07 16:50:45 +0000305 .setCodeModel(codeModel);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000306
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000307 llvm::StringRef mArch;
308 llvm::StringRef mCPU;
309 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000310
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000311 for (std::string &feature : m_cpu_features)
312 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000313
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000314 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
315 mArch,
316 mCPU,
317 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000318
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000319 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000321 if (!m_execution_engine_ap.get())
322 {
323 error.SetErrorToGenericError();
324 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000325 return;
326 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000327 else
328 {
329 m_module_ap.release(); // ownership was transferred
330 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000331
Greg Clayton23f8c952014-03-24 23:10:19 +0000332 // Make sure we see all sections, including ones that don't have relocations...
333 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000334
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000335 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000337 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000338
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000339 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000341 if (!error.Success())
342 {
343 // We got an error through our callback!
344 return;
345 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000346
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000347 if (!function)
348 {
349 error.SetErrorToGenericError();
350 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
351 return;
352 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000353
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000354 if (!fun_ptr)
355 {
356 error.SetErrorToGenericError();
357 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
358 return;
359 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000360
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000361 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000362
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000363 CommitAllocations(process_sp);
364 ReportAllocations(*m_execution_engine_ap);
365 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000366
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000367 for (JittedFunction &jitted_function : m_jitted_functions)
368 {
369 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000370
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000371 if (!jitted_function.m_name.compare(m_name.AsCString()))
372 {
373 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
374 m_function_end_load_addr = func_range.first + func_range.second;
375 m_function_load_addr = jitted_function.m_remote_addr;
376 }
377 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000378
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000379 if (log)
380 {
381 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000383 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000384
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000385 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000386
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000387 if (!err.Success())
388 {
389 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
390 }
391 else
392 {
393 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
394 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000395
Sean Callanan4f2c1982014-01-21 00:54:48 +0000396 log->Printf("Sections: ");
397 for (AllocationRecord &record : m_records)
398 {
399 if (record.m_process_address != LLDB_INVALID_ADDRESS)
400 {
401 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000402
Sean Callanan4f2c1982014-01-21 00:54:48 +0000403 DataBufferHeap my_buffer(record.m_size, 0);
404 Error err;
405 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406
Sean Callanan4f2c1982014-01-21 00:54:48 +0000407 if (err.Success())
408 {
409 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
410 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
411 }
412 }
413 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000414 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000416 func_addr = m_function_load_addr;
417 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000418
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000419 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000420}
421
422IRExecutionUnit::~IRExecutionUnit ()
423{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000424 m_module_ap.reset();
425 m_execution_engine_ap.reset();
426 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000427}
428
429IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
430 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
431 m_parent (parent)
432{
433}
434
Greg Clayton23f8c952014-03-24 23:10:19 +0000435IRExecutionUnit::MemoryManager::~MemoryManager ()
436{
437}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000438void
439IRExecutionUnit::MemoryManager::setMemoryWritable ()
440{
441 m_default_mm_ap->setMemoryWritable();
442}
443
444void
445IRExecutionUnit::MemoryManager::setMemoryExecutable ()
446{
447 m_default_mm_ap->setMemoryExecutable();
448}
449
450
451uint8_t *
452IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
453 uintptr_t &ActualSize)
454{
455 return m_default_mm_ap->startFunctionBody(F, ActualSize);
456}
457
458uint8_t *
459IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
460 unsigned StubSize,
461 unsigned Alignment)
462{
Greg Clayton5160ce52013-03-27 23:08:40 +0000463 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000464
465 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000466
467 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
468 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000469 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Stub),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000470 StubSize,
Greg Clayton23f8c952014-03-24 23:10:19 +0000471 Alignment,
472 eSectionIDInvalid,
473 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000474
475 if (log)
476 {
477 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000478 static_cast<const void*>(F), StubSize, Alignment,
479 static_cast<void*>(return_value));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000480 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000481
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000482 return return_value;
483}
484
485void
486IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
487 uint8_t *FunctionStart,
488 uint8_t *FunctionEnd)
489{
490 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
491}
492
Greg Clayton23f8c952014-03-24 23:10:19 +0000493lldb::SectionType
494IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
495{
496 lldb::SectionType sect_type = lldb::eSectionTypeCode;
497 switch (alloc_kind)
498 {
499 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
500 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
501 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
502 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
503 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
504 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000505
Greg Clayton23f8c952014-03-24 23:10:19 +0000506 if (!name.empty())
507 {
508 if (name.equals("__text") || name.equals(".text"))
509 sect_type = lldb::eSectionTypeCode;
510 else if (name.equals("__data") || name.equals(".data"))
511 sect_type = lldb::eSectionTypeCode;
512 else if (name.startswith("__debug_") || name.startswith(".debug_"))
513 {
514 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
515 llvm::StringRef dwarf_name(name.substr(name_idx));
516 switch (dwarf_name[0])
517 {
518 case 'a':
519 if (dwarf_name.equals("abbrev"))
520 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
521 else if (dwarf_name.equals("aranges"))
522 sect_type = lldb::eSectionTypeDWARFDebugAranges;
523 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000524
Greg Clayton23f8c952014-03-24 23:10:19 +0000525 case 'f':
526 if (dwarf_name.equals("frame"))
527 sect_type = lldb::eSectionTypeDWARFDebugFrame;
528 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000529
Greg Clayton23f8c952014-03-24 23:10:19 +0000530 case 'i':
531 if (dwarf_name.equals("info"))
532 sect_type = lldb::eSectionTypeDWARFDebugInfo;
533 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000534
Greg Clayton23f8c952014-03-24 23:10:19 +0000535 case 'l':
536 if (dwarf_name.equals("line"))
537 sect_type = lldb::eSectionTypeDWARFDebugLine;
538 else if (dwarf_name.equals("loc"))
539 sect_type = lldb::eSectionTypeDWARFDebugLoc;
540 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000541
Greg Clayton23f8c952014-03-24 23:10:19 +0000542 case 'm':
543 if (dwarf_name.equals("macinfo"))
544 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
545 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000546
Greg Clayton23f8c952014-03-24 23:10:19 +0000547 case 'p':
548 if (dwarf_name.equals("pubnames"))
549 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
550 else if (dwarf_name.equals("pubtypes"))
551 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
552 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000553
Greg Clayton23f8c952014-03-24 23:10:19 +0000554 case 's':
555 if (dwarf_name.equals("str"))
556 sect_type = lldb::eSectionTypeDWARFDebugStr;
557 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000558
Greg Clayton23f8c952014-03-24 23:10:19 +0000559 case 'r':
560 if (dwarf_name.equals("ranges"))
561 sect_type = lldb::eSectionTypeDWARFDebugRanges;
562 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000563
Greg Clayton23f8c952014-03-24 23:10:19 +0000564 default:
565 break;
566 }
567 }
568 else if (name.startswith("__apple_") || name.startswith(".apple_"))
569 {
570#if 0
571 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
572 llvm::StringRef apple_name(name.substr(name_idx));
573 switch (apple_name[0])
574 {
575 case 'n':
576 if (apple_name.equals("names"))
577 sect_type = lldb::eSectionTypeDWARFAppleNames;
578 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
579 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
580 break;
581 case 't':
582 if (apple_name.equals("types"))
583 sect_type = lldb::eSectionTypeDWARFAppleTypes;
584 break;
585 case 'o':
586 if (apple_name.equals("objc"))
587 sect_type = lldb::eSectionTypeDWARFAppleObjC;
588 break;
589 default:
590 break;
591 }
592#else
593 sect_type = lldb::eSectionTypeInvalid;
594#endif
595 }
596 else if (name.equals("__objc_imageinfo"))
597 sect_type = lldb::eSectionTypeOther;
598 }
599 return sect_type;
600}
601
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000602uint8_t *
603IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
604{
Greg Clayton5160ce52013-03-27 23:08:40 +0000605 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000606
607 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000608
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000609 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
610 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000611 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Bytes),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000612 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000613 Alignment,
614 eSectionIDInvalid,
615 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000616
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000617 if (log)
618 {
619 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
620 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000621 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000623 return return_value;
624}
625
626uint8_t *
627IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
628 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000629 unsigned SectionID,
630 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000631{
Greg Clayton5160ce52013-03-27 23:08:40 +0000632 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000633
Filip Pizlobfcff682013-10-02 01:43:46 +0000634 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000635
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000636 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000637 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000638 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000639 Size,
640 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000641 SectionID,
642 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000644 if (log)
645 {
646 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
647 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000648 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000649
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000650 return return_value;
651}
652
653uint8_t *
654IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
655 unsigned Alignment,
656 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000657 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000658 bool IsReadOnly)
659{
Greg Clayton5160ce52013-03-27 23:08:40 +0000660 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000661
Filip Pizlobfcff682013-10-02 01:43:46 +0000662 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000663
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000664 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000665 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
666 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000667 Size,
668 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000669 SectionID,
670 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000671 if (log)
672 {
673 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
674 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000675 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000676
677 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000678}
679
680uint8_t *
681IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
682 unsigned Alignment)
683{
Greg Clayton5160ce52013-03-27 23:08:40 +0000684 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000685
686 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000687
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000688 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
689 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000690 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Global),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000691 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000692 Alignment,
693 eSectionIDInvalid,
694 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000695
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000696 if (log)
697 {
698 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
699 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000701
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000702 return return_value;
703}
704
705void
706IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
707{
708 m_default_mm_ap->deallocateFunctionBody(Body);
709}
710
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000711lldb::addr_t
712IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
713{
Sean Callananffae9442013-06-27 01:42:47 +0000714 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
715
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000716 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000717 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000718 if (local_address >= record.m_host_address &&
719 local_address < record.m_host_address + record.m_size)
720 {
721 if (record.m_process_address == LLDB_INVALID_ADDRESS)
722 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000723
Sean Callananffae9442013-06-27 01:42:47 +0000724 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000725
Sean Callananffae9442013-06-27 01:42:47 +0000726 if (log)
727 {
728 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
729 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000730 (uint64_t)record.m_host_address,
731 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000732 ret,
733 record.m_process_address,
734 record.m_process_address + record.m_size);
735 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000736
Sean Callananffae9442013-06-27 01:42:47 +0000737 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000738 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000739 }
740
741 return LLDB_INVALID_ADDRESS;
742}
743
744IRExecutionUnit::AddrRange
745IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
746{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000747 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000748 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000749 if (local_address >= record.m_host_address &&
750 local_address < record.m_host_address + record.m_size)
751 {
752 if (record.m_process_address == LLDB_INVALID_ADDRESS)
753 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000754
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000755 return AddrRange(record.m_process_address, record.m_size);
756 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000757 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000758
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000759 return AddrRange (0, 0);
760}
761
762bool
763IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
764{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000765 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000767 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000768
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000769 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000770 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000771 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000772 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000773
Greg Clayton23f8c952014-03-24 23:10:19 +0000774 switch (record.m_sect_type)
775 {
776 case lldb::eSectionTypeInvalid:
777 case lldb::eSectionTypeDWARFDebugAbbrev:
778 case lldb::eSectionTypeDWARFDebugAranges:
779 case lldb::eSectionTypeDWARFDebugFrame:
780 case lldb::eSectionTypeDWARFDebugInfo:
781 case lldb::eSectionTypeDWARFDebugLine:
782 case lldb::eSectionTypeDWARFDebugLoc:
783 case lldb::eSectionTypeDWARFDebugMacInfo:
784 case lldb::eSectionTypeDWARFDebugPubNames:
785 case lldb::eSectionTypeDWARFDebugPubTypes:
786 case lldb::eSectionTypeDWARFDebugRanges:
787 case lldb::eSectionTypeDWARFDebugStr:
788 case lldb::eSectionTypeDWARFAppleNames:
789 case lldb::eSectionTypeDWARFAppleTypes:
790 case lldb::eSectionTypeDWARFAppleNamespaces:
791 case lldb::eSectionTypeDWARFAppleObjC:
792 err.Clear();
793 break;
794 default:
795 record.m_process_address = Malloc (record.m_size,
796 record.m_alignment,
797 record.m_permissions,
798 eAllocationPolicyProcessOnly,
799 err);
800 break;
801 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000802
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000803 if (!err.Success())
804 {
805 ret = false;
806 break;
807 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000808 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000809
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000810 if (!ret)
811 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000812 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000813 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000814 if (record.m_process_address != LLDB_INVALID_ADDRESS)
815 {
816 Free(record.m_process_address, err);
817 record.m_process_address = LLDB_INVALID_ADDRESS;
818 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000819 }
820 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000822 return ret;
823}
824
825void
826IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
827{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000828 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000829 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000830 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000831 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000832
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000833 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000834 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000835
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000836 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000837 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000838
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000839 // Trigger re-application of relocations.
840 engine.finalizeObject();
841}
842
843bool
844IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
845{
Greg Clayton23f8c952014-03-24 23:10:19 +0000846 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000847 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000848 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000849 if (record.m_process_address != LLDB_INVALID_ADDRESS)
850 {
851 lldb_private::Error err;
852 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
853 if (err.Success())
854 wrote_something = true;
855 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000856 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000857 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000858}
859
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000861IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000862{
863 if (!log)
864 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000865
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000866 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000867 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000868 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000869 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000870 (unsigned)m_alignment,
871 (unsigned)m_section_id);
872}
Greg Clayton23f8c952014-03-24 23:10:19 +0000873
874
875lldb::ByteOrder
876IRExecutionUnit::GetByteOrder () const
877{
878 ExecutionContext exe_ctx (GetBestExecutionContextScope());
879 return exe_ctx.GetByteOrder();
880}
881
882uint32_t
883IRExecutionUnit::GetAddressByteSize () const
884{
885 ExecutionContext exe_ctx (GetBestExecutionContextScope());
886 return exe_ctx.GetAddressByteSize();
887}
888
889void
890IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
891 lldb_private::Symtab &symtab)
892{
893 // No symbols yet...
894}
895
896
897void
898IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
899 lldb_private::SectionList &section_list)
900{
901 for (AllocationRecord &record : m_records)
902 {
903 if (record.m_size > 0)
904 {
905 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
906 obj_file,
907 record.m_section_id,
908 ConstString(record.m_name),
909 record.m_sect_type,
910 record.m_process_address,
911 record.m_size,
912 record.m_host_address, // file_offset (which is the host address for the data)
913 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000914 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000915 record.m_permissions)); // flags
916 section_list.AddSection (section_sp);
917 }
918 }
919}
920
921bool
922IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
923{
924 ExecutionContext exe_ctx (GetBestExecutionContextScope());
925 Target *target = exe_ctx.GetTargetPtr();
926 if (target)
927 arch = target->GetArchitecture();
928 else
929 arch.Clear();
930 return arch.IsValid();
931}
932
933lldb::ModuleSP
934IRExecutionUnit::GetJITModule ()
935{
936 ExecutionContext exe_ctx (GetBestExecutionContextScope());
937 Target *target = exe_ctx.GetTargetPtr();
938 if (target)
939 {
940 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
941 if (jit_module_sp)
942 {
943 bool changed = false;
944 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
945 }
946 return jit_module_sp;
947 }
948 return lldb::ModuleSP();
949}