blob: 090f88fc1bfe7f890ddaf9e21a2f0f13f461530e [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
Rafael Espindola7c03d952014-08-19 04:27:03 +0000298 llvm::EngineBuilder builder(std::move(m_module_ap));
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 Turnerd8675202014-08-07 23:26:01 +0000305 .setAllocateGVsWithCode(true)
306 .setCodeModel(codeModel)
307 .setUseMCJIT(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000308
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000309 llvm::StringRef mArch;
310 llvm::StringRef mCPU;
311 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000313 for (std::string &feature : m_cpu_features)
314 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000315
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000316 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
317 mArch,
318 mCPU,
319 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000321 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000322
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000323 if (!m_execution_engine_ap.get())
324 {
325 error.SetErrorToGenericError();
326 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000327 return;
328 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Greg Clayton23f8c952014-03-24 23:10:19 +0000330 // Make sure we see all sections, including ones that don't have relocations...
331 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000332
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000333 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000334
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000335 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000337 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000338
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000339 if (!error.Success())
340 {
341 // We got an error through our callback!
342 return;
343 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000345 if (!function)
346 {
347 error.SetErrorToGenericError();
348 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
349 return;
350 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000351
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000352 if (!fun_ptr)
353 {
354 error.SetErrorToGenericError();
355 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
356 return;
357 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000358
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000359 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000360
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000361 CommitAllocations(process_sp);
362 ReportAllocations(*m_execution_engine_ap);
363 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000364
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000365 for (JittedFunction &jitted_function : m_jitted_functions)
366 {
367 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000369 if (!jitted_function.m_name.compare(m_name.AsCString()))
370 {
371 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
372 m_function_end_load_addr = func_range.first + func_range.second;
373 m_function_load_addr = jitted_function.m_remote_addr;
374 }
375 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000376
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000377 if (log)
378 {
379 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000381 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000383 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000384
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000385 if (!err.Success())
386 {
387 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
388 }
389 else
390 {
391 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
392 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000393
Sean Callanan4f2c1982014-01-21 00:54:48 +0000394 log->Printf("Sections: ");
395 for (AllocationRecord &record : m_records)
396 {
397 if (record.m_process_address != LLDB_INVALID_ADDRESS)
398 {
399 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000400
Sean Callanan4f2c1982014-01-21 00:54:48 +0000401 DataBufferHeap my_buffer(record.m_size, 0);
402 Error err;
403 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000404
Sean Callanan4f2c1982014-01-21 00:54:48 +0000405 if (err.Success())
406 {
407 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
408 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
409 }
410 }
411 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000412 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000413
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000414 func_addr = m_function_load_addr;
415 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000416
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000417 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000418}
419
420IRExecutionUnit::~IRExecutionUnit ()
421{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000422 m_module_ap.reset();
423 m_execution_engine_ap.reset();
424 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000425}
426
427IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
428 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
429 m_parent (parent)
430{
431}
432
Greg Clayton23f8c952014-03-24 23:10:19 +0000433IRExecutionUnit::MemoryManager::~MemoryManager ()
434{
435}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000436void
437IRExecutionUnit::MemoryManager::setMemoryWritable ()
438{
439 m_default_mm_ap->setMemoryWritable();
440}
441
442void
443IRExecutionUnit::MemoryManager::setMemoryExecutable ()
444{
445 m_default_mm_ap->setMemoryExecutable();
446}
447
448
449uint8_t *
450IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
451 uintptr_t &ActualSize)
452{
453 return m_default_mm_ap->startFunctionBody(F, ActualSize);
454}
455
456uint8_t *
457IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
458 unsigned StubSize,
459 unsigned Alignment)
460{
Greg Clayton5160ce52013-03-27 23:08:40 +0000461 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000462
463 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000464
465 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
466 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000467 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Stub),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000468 StubSize,
Greg Clayton23f8c952014-03-24 23:10:19 +0000469 Alignment,
470 eSectionIDInvalid,
471 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000472
473 if (log)
474 {
475 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000476 static_cast<const void*>(F), StubSize, Alignment,
477 static_cast<void*>(return_value));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000478 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000479
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000480 return return_value;
481}
482
483void
484IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
485 uint8_t *FunctionStart,
486 uint8_t *FunctionEnd)
487{
488 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
489}
490
Greg Clayton23f8c952014-03-24 23:10:19 +0000491lldb::SectionType
492IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
493{
494 lldb::SectionType sect_type = lldb::eSectionTypeCode;
495 switch (alloc_kind)
496 {
497 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
498 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
499 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
500 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
501 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
502 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000503
Greg Clayton23f8c952014-03-24 23:10:19 +0000504 if (!name.empty())
505 {
506 if (name.equals("__text") || name.equals(".text"))
507 sect_type = lldb::eSectionTypeCode;
508 else if (name.equals("__data") || name.equals(".data"))
509 sect_type = lldb::eSectionTypeCode;
510 else if (name.startswith("__debug_") || name.startswith(".debug_"))
511 {
512 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
513 llvm::StringRef dwarf_name(name.substr(name_idx));
514 switch (dwarf_name[0])
515 {
516 case 'a':
517 if (dwarf_name.equals("abbrev"))
518 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
519 else if (dwarf_name.equals("aranges"))
520 sect_type = lldb::eSectionTypeDWARFDebugAranges;
521 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000522
Greg Clayton23f8c952014-03-24 23:10:19 +0000523 case 'f':
524 if (dwarf_name.equals("frame"))
525 sect_type = lldb::eSectionTypeDWARFDebugFrame;
526 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000527
Greg Clayton23f8c952014-03-24 23:10:19 +0000528 case 'i':
529 if (dwarf_name.equals("info"))
530 sect_type = lldb::eSectionTypeDWARFDebugInfo;
531 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000532
Greg Clayton23f8c952014-03-24 23:10:19 +0000533 case 'l':
534 if (dwarf_name.equals("line"))
535 sect_type = lldb::eSectionTypeDWARFDebugLine;
536 else if (dwarf_name.equals("loc"))
537 sect_type = lldb::eSectionTypeDWARFDebugLoc;
538 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000539
Greg Clayton23f8c952014-03-24 23:10:19 +0000540 case 'm':
541 if (dwarf_name.equals("macinfo"))
542 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
543 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Greg Clayton23f8c952014-03-24 23:10:19 +0000545 case 'p':
546 if (dwarf_name.equals("pubnames"))
547 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
548 else if (dwarf_name.equals("pubtypes"))
549 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
550 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000551
Greg Clayton23f8c952014-03-24 23:10:19 +0000552 case 's':
553 if (dwarf_name.equals("str"))
554 sect_type = lldb::eSectionTypeDWARFDebugStr;
555 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000556
Greg Clayton23f8c952014-03-24 23:10:19 +0000557 case 'r':
558 if (dwarf_name.equals("ranges"))
559 sect_type = lldb::eSectionTypeDWARFDebugRanges;
560 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000561
Greg Clayton23f8c952014-03-24 23:10:19 +0000562 default:
563 break;
564 }
565 }
566 else if (name.startswith("__apple_") || name.startswith(".apple_"))
567 {
568#if 0
569 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
570 llvm::StringRef apple_name(name.substr(name_idx));
571 switch (apple_name[0])
572 {
573 case 'n':
574 if (apple_name.equals("names"))
575 sect_type = lldb::eSectionTypeDWARFAppleNames;
576 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
577 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
578 break;
579 case 't':
580 if (apple_name.equals("types"))
581 sect_type = lldb::eSectionTypeDWARFAppleTypes;
582 break;
583 case 'o':
584 if (apple_name.equals("objc"))
585 sect_type = lldb::eSectionTypeDWARFAppleObjC;
586 break;
587 default:
588 break;
589 }
590#else
591 sect_type = lldb::eSectionTypeInvalid;
592#endif
593 }
594 else if (name.equals("__objc_imageinfo"))
595 sect_type = lldb::eSectionTypeOther;
596 }
597 return sect_type;
598}
599
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000600uint8_t *
601IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
602{
Greg Clayton5160ce52013-03-27 23:08:40 +0000603 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000604
605 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000607 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
608 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000609 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Bytes),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000610 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000611 Alignment,
612 eSectionIDInvalid,
613 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000615 if (log)
616 {
617 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
618 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000619 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000621 return return_value;
622}
623
624uint8_t *
625IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
626 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000627 unsigned SectionID,
628 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000629{
Greg Clayton5160ce52013-03-27 23:08:40 +0000630 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000631
Filip Pizlobfcff682013-10-02 01:43:46 +0000632 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000633
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000634 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000635 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000636 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000637 Size,
638 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000639 SectionID,
640 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000641
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000642 if (log)
643 {
644 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
645 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000646 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000648 return return_value;
649}
650
651uint8_t *
652IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
653 unsigned Alignment,
654 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000655 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000656 bool IsReadOnly)
657{
Greg Clayton5160ce52013-03-27 23:08:40 +0000658 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000659
Filip Pizlobfcff682013-10-02 01:43:46 +0000660 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000661
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000662 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000663 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
664 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000665 Size,
666 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000667 SectionID,
668 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000669 if (log)
670 {
671 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
672 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000673 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674
675 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000676}
677
678uint8_t *
679IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
680 unsigned Alignment)
681{
Greg Clayton5160ce52013-03-27 23:08:40 +0000682 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000683
684 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000685
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000686 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
687 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000688 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Global),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000689 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000690 Alignment,
691 eSectionIDInvalid,
692 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000693
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000694 if (log)
695 {
696 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
697 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000698 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000699
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700 return return_value;
701}
702
703void
704IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
705{
706 m_default_mm_ap->deallocateFunctionBody(Body);
707}
708
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000709lldb::addr_t
710IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
711{
Sean Callananffae9442013-06-27 01:42:47 +0000712 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
713
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000714 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000715 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000716 if (local_address >= record.m_host_address &&
717 local_address < record.m_host_address + record.m_size)
718 {
719 if (record.m_process_address == LLDB_INVALID_ADDRESS)
720 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000721
Sean Callananffae9442013-06-27 01:42:47 +0000722 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000723
Sean Callananffae9442013-06-27 01:42:47 +0000724 if (log)
725 {
726 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
727 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000728 (uint64_t)record.m_host_address,
729 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000730 ret,
731 record.m_process_address,
732 record.m_process_address + record.m_size);
733 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callananffae9442013-06-27 01:42:47 +0000735 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000736 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000737 }
738
739 return LLDB_INVALID_ADDRESS;
740}
741
742IRExecutionUnit::AddrRange
743IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
744{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000745 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000746 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000747 if (local_address >= record.m_host_address &&
748 local_address < record.m_host_address + record.m_size)
749 {
750 if (record.m_process_address == LLDB_INVALID_ADDRESS)
751 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000752
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000753 return AddrRange(record.m_process_address, record.m_size);
754 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000755 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000757 return AddrRange (0, 0);
758}
759
760bool
761IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
762{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000763 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000764
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000765 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000767 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000768 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000769 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000770 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000771
Greg Clayton23f8c952014-03-24 23:10:19 +0000772 switch (record.m_sect_type)
773 {
774 case lldb::eSectionTypeInvalid:
775 case lldb::eSectionTypeDWARFDebugAbbrev:
776 case lldb::eSectionTypeDWARFDebugAranges:
777 case lldb::eSectionTypeDWARFDebugFrame:
778 case lldb::eSectionTypeDWARFDebugInfo:
779 case lldb::eSectionTypeDWARFDebugLine:
780 case lldb::eSectionTypeDWARFDebugLoc:
781 case lldb::eSectionTypeDWARFDebugMacInfo:
782 case lldb::eSectionTypeDWARFDebugPubNames:
783 case lldb::eSectionTypeDWARFDebugPubTypes:
784 case lldb::eSectionTypeDWARFDebugRanges:
785 case lldb::eSectionTypeDWARFDebugStr:
786 case lldb::eSectionTypeDWARFAppleNames:
787 case lldb::eSectionTypeDWARFAppleTypes:
788 case lldb::eSectionTypeDWARFAppleNamespaces:
789 case lldb::eSectionTypeDWARFAppleObjC:
790 err.Clear();
791 break;
792 default:
793 record.m_process_address = Malloc (record.m_size,
794 record.m_alignment,
795 record.m_permissions,
796 eAllocationPolicyProcessOnly,
797 err);
798 break;
799 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000801 if (!err.Success())
802 {
803 ret = false;
804 break;
805 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000806 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000807
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000808 if (!ret)
809 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000810 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000811 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000812 if (record.m_process_address != LLDB_INVALID_ADDRESS)
813 {
814 Free(record.m_process_address, err);
815 record.m_process_address = LLDB_INVALID_ADDRESS;
816 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000817 }
818 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000819
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000820 return ret;
821}
822
823void
824IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
825{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000826 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000827 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000828 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000829 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000830
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000831 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000832 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000834 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000835 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000836
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000837 // Trigger re-application of relocations.
838 engine.finalizeObject();
839}
840
841bool
842IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
843{
Greg Clayton23f8c952014-03-24 23:10:19 +0000844 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000845 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000846 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000847 if (record.m_process_address != LLDB_INVALID_ADDRESS)
848 {
849 lldb_private::Error err;
850 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
851 if (err.Success())
852 wrote_something = true;
853 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000854 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000855 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000856}
857
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000858void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000859IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000860{
861 if (!log)
862 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000863
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000864 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000865 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000866 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000867 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000868 (unsigned)m_alignment,
869 (unsigned)m_section_id);
870}
Greg Clayton23f8c952014-03-24 23:10:19 +0000871
872
873lldb::ByteOrder
874IRExecutionUnit::GetByteOrder () const
875{
876 ExecutionContext exe_ctx (GetBestExecutionContextScope());
877 return exe_ctx.GetByteOrder();
878}
879
880uint32_t
881IRExecutionUnit::GetAddressByteSize () const
882{
883 ExecutionContext exe_ctx (GetBestExecutionContextScope());
884 return exe_ctx.GetAddressByteSize();
885}
886
887void
888IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
889 lldb_private::Symtab &symtab)
890{
891 // No symbols yet...
892}
893
894
895void
896IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
897 lldb_private::SectionList &section_list)
898{
899 for (AllocationRecord &record : m_records)
900 {
901 if (record.m_size > 0)
902 {
903 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
904 obj_file,
905 record.m_section_id,
906 ConstString(record.m_name),
907 record.m_sect_type,
908 record.m_process_address,
909 record.m_size,
910 record.m_host_address, // file_offset (which is the host address for the data)
911 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000912 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000913 record.m_permissions)); // flags
914 section_list.AddSection (section_sp);
915 }
916 }
917}
918
919bool
920IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
921{
922 ExecutionContext exe_ctx (GetBestExecutionContextScope());
923 Target *target = exe_ctx.GetTargetPtr();
924 if (target)
925 arch = target->GetArchitecture();
926 else
927 arch.Clear();
928 return arch.IsValid();
929}
930
931lldb::ModuleSP
932IRExecutionUnit::GetJITModule ()
933{
934 ExecutionContext exe_ctx (GetBestExecutionContextScope());
935 Target *target = exe_ctx.GetTargetPtr();
936 if (target)
937 {
938 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
939 if (jit_module_sp)
940 {
941 bool changed = false;
942 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
943 }
944 return jit_module_sp;
945 }
946 return lldb::ModuleSP();
947}