blob: 41cc89979f3cc6af572b5cb840304a4b2e78925d [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))
Reid Kleckner54209532014-09-03 00:40:36 +0000304 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000305
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000306 llvm::StringRef mArch;
307 llvm::StringRef mCPU;
308 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000309
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000310 for (std::string &feature : m_cpu_features)
311 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000313 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
314 mArch,
315 mCPU,
316 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000317
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000318 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000320 if (!m_execution_engine_ap.get())
321 {
322 error.SetErrorToGenericError();
323 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000324 return;
325 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000326
Greg Clayton23f8c952014-03-24 23:10:19 +0000327 // Make sure we see all sections, including ones that don't have relocations...
328 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000330 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000331
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000332 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000333
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000334 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000335
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000336 if (!error.Success())
337 {
338 // We got an error through our callback!
339 return;
340 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000342 if (!function)
343 {
344 error.SetErrorToGenericError();
345 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
346 return;
347 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000348
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000349 if (!fun_ptr)
350 {
351 error.SetErrorToGenericError();
352 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
353 return;
354 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000355
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000356 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000358 CommitAllocations(process_sp);
359 ReportAllocations(*m_execution_engine_ap);
360 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000362 for (JittedFunction &jitted_function : m_jitted_functions)
363 {
364 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000365
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000366 if (!jitted_function.m_name.compare(m_name.AsCString()))
367 {
368 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
369 m_function_end_load_addr = func_range.first + func_range.second;
370 m_function_load_addr = jitted_function.m_remote_addr;
371 }
372 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000374 if (log)
375 {
376 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000377
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000378 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000379
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000380 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000381
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000382 if (!err.Success())
383 {
384 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
385 }
386 else
387 {
388 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
389 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000390
Sean Callanan4f2c1982014-01-21 00:54:48 +0000391 log->Printf("Sections: ");
392 for (AllocationRecord &record : m_records)
393 {
394 if (record.m_process_address != LLDB_INVALID_ADDRESS)
395 {
396 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000397
Sean Callanan4f2c1982014-01-21 00:54:48 +0000398 DataBufferHeap my_buffer(record.m_size, 0);
399 Error err;
400 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callanan4f2c1982014-01-21 00:54:48 +0000402 if (err.Success())
403 {
404 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
405 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
406 }
407 }
408 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000409 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000411 func_addr = m_function_load_addr;
412 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000413
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000414 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000415}
416
417IRExecutionUnit::~IRExecutionUnit ()
418{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000419 m_module_ap.reset();
420 m_execution_engine_ap.reset();
421 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000422}
423
424IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
425 m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()),
426 m_parent (parent)
427{
428}
429
Greg Clayton23f8c952014-03-24 23:10:19 +0000430IRExecutionUnit::MemoryManager::~MemoryManager ()
431{
432}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000433void
434IRExecutionUnit::MemoryManager::setMemoryWritable ()
435{
436 m_default_mm_ap->setMemoryWritable();
437}
438
439void
440IRExecutionUnit::MemoryManager::setMemoryExecutable ()
441{
442 m_default_mm_ap->setMemoryExecutable();
443}
444
445
446uint8_t *
447IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F,
448 uintptr_t &ActualSize)
449{
450 return m_default_mm_ap->startFunctionBody(F, ActualSize);
451}
452
453uint8_t *
454IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F,
455 unsigned StubSize,
456 unsigned Alignment)
457{
Greg Clayton5160ce52013-03-27 23:08:40 +0000458 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000459
460 uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000461
462 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
463 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000464 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Stub),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000465 StubSize,
Greg Clayton23f8c952014-03-24 23:10:19 +0000466 Alignment,
467 eSectionIDInvalid,
468 NULL));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000469
470 if (log)
471 {
472 log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000473 static_cast<const void*>(F), StubSize, Alignment,
474 static_cast<void*>(return_value));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000475 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000476
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000477 return return_value;
478}
479
480void
481IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F,
482 uint8_t *FunctionStart,
483 uint8_t *FunctionEnd)
484{
485 m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd);
486}
487
Greg Clayton23f8c952014-03-24 23:10:19 +0000488lldb::SectionType
489IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
490{
491 lldb::SectionType sect_type = lldb::eSectionTypeCode;
492 switch (alloc_kind)
493 {
494 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
495 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
496 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
497 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
498 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
499 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000500
Greg Clayton23f8c952014-03-24 23:10:19 +0000501 if (!name.empty())
502 {
503 if (name.equals("__text") || name.equals(".text"))
504 sect_type = lldb::eSectionTypeCode;
505 else if (name.equals("__data") || name.equals(".data"))
506 sect_type = lldb::eSectionTypeCode;
507 else if (name.startswith("__debug_") || name.startswith(".debug_"))
508 {
509 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
510 llvm::StringRef dwarf_name(name.substr(name_idx));
511 switch (dwarf_name[0])
512 {
513 case 'a':
514 if (dwarf_name.equals("abbrev"))
515 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
516 else if (dwarf_name.equals("aranges"))
517 sect_type = lldb::eSectionTypeDWARFDebugAranges;
518 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000519
Greg Clayton23f8c952014-03-24 23:10:19 +0000520 case 'f':
521 if (dwarf_name.equals("frame"))
522 sect_type = lldb::eSectionTypeDWARFDebugFrame;
523 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000524
Greg Clayton23f8c952014-03-24 23:10:19 +0000525 case 'i':
526 if (dwarf_name.equals("info"))
527 sect_type = lldb::eSectionTypeDWARFDebugInfo;
528 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000529
Greg Clayton23f8c952014-03-24 23:10:19 +0000530 case 'l':
531 if (dwarf_name.equals("line"))
532 sect_type = lldb::eSectionTypeDWARFDebugLine;
533 else if (dwarf_name.equals("loc"))
534 sect_type = lldb::eSectionTypeDWARFDebugLoc;
535 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000536
Greg Clayton23f8c952014-03-24 23:10:19 +0000537 case 'm':
538 if (dwarf_name.equals("macinfo"))
539 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
540 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000541
Greg Clayton23f8c952014-03-24 23:10:19 +0000542 case 'p':
543 if (dwarf_name.equals("pubnames"))
544 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
545 else if (dwarf_name.equals("pubtypes"))
546 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
547 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000548
Greg Clayton23f8c952014-03-24 23:10:19 +0000549 case 's':
550 if (dwarf_name.equals("str"))
551 sect_type = lldb::eSectionTypeDWARFDebugStr;
552 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000553
Greg Clayton23f8c952014-03-24 23:10:19 +0000554 case 'r':
555 if (dwarf_name.equals("ranges"))
556 sect_type = lldb::eSectionTypeDWARFDebugRanges;
557 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000558
Greg Clayton23f8c952014-03-24 23:10:19 +0000559 default:
560 break;
561 }
562 }
563 else if (name.startswith("__apple_") || name.startswith(".apple_"))
564 {
565#if 0
566 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
567 llvm::StringRef apple_name(name.substr(name_idx));
568 switch (apple_name[0])
569 {
570 case 'n':
571 if (apple_name.equals("names"))
572 sect_type = lldb::eSectionTypeDWARFAppleNames;
573 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
574 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
575 break;
576 case 't':
577 if (apple_name.equals("types"))
578 sect_type = lldb::eSectionTypeDWARFAppleTypes;
579 break;
580 case 'o':
581 if (apple_name.equals("objc"))
582 sect_type = lldb::eSectionTypeDWARFAppleObjC;
583 break;
584 default:
585 break;
586 }
587#else
588 sect_type = lldb::eSectionTypeInvalid;
589#endif
590 }
591 else if (name.equals("__objc_imageinfo"))
592 sect_type = lldb::eSectionTypeOther;
593 }
594 return sect_type;
595}
596
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000597uint8_t *
598IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
599{
Greg Clayton5160ce52013-03-27 23:08:40 +0000600 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000601
602 uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000603
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000604 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
605 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000606 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Bytes),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000607 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000608 Alignment,
609 eSectionIDInvalid,
610 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000612 if (log)
613 {
614 log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p",
615 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000616 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000618 return return_value;
619}
620
621uint8_t *
622IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
623 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000624 unsigned SectionID,
625 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000626{
Greg Clayton5160ce52013-03-27 23:08:40 +0000627 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000628
Filip Pizlobfcff682013-10-02 01:43:46 +0000629 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000631 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000632 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000633 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000634 Size,
635 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000636 SectionID,
637 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000639 if (log)
640 {
641 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
642 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000643 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000644
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000645 return return_value;
646}
647
648uint8_t *
649IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
650 unsigned Alignment,
651 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000652 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000653 bool IsReadOnly)
654{
Greg Clayton5160ce52013-03-27 23:08:40 +0000655 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000656
Filip Pizlobfcff682013-10-02 01:43:46 +0000657 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000658
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000659 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000660 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
661 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000662 Size,
663 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000664 SectionID,
665 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000666 if (log)
667 {
668 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
669 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000670 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000671
672 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000673}
674
675uint8_t *
676IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size,
677 unsigned Alignment)
678{
Greg Clayton5160ce52013-03-27 23:08:40 +0000679 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000680
681 uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000683 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
684 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000685 GetSectionTypeFromSectionName (llvm::StringRef(), AllocationKind::Global),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000686 Size,
Greg Clayton23f8c952014-03-24 23:10:19 +0000687 Alignment,
688 eSectionIDInvalid,
689 NULL));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000690
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000691 if (log)
692 {
693 log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p",
694 (uint64_t)Size, Alignment, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000695 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000696
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000697 return return_value;
698}
699
700void
701IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body)
702{
703 m_default_mm_ap->deallocateFunctionBody(Body);
704}
705
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000706lldb::addr_t
707IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
708{
Sean Callananffae9442013-06-27 01:42:47 +0000709 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
710
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000711 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000712 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000713 if (local_address >= record.m_host_address &&
714 local_address < record.m_host_address + record.m_size)
715 {
716 if (record.m_process_address == LLDB_INVALID_ADDRESS)
717 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000718
Sean Callananffae9442013-06-27 01:42:47 +0000719 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000720
Sean Callananffae9442013-06-27 01:42:47 +0000721 if (log)
722 {
723 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
724 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000725 (uint64_t)record.m_host_address,
726 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000727 ret,
728 record.m_process_address,
729 record.m_process_address + record.m_size);
730 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000731
Sean Callananffae9442013-06-27 01:42:47 +0000732 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000733 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000734 }
735
736 return LLDB_INVALID_ADDRESS;
737}
738
739IRExecutionUnit::AddrRange
740IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
741{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000742 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000743 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000744 if (local_address >= record.m_host_address &&
745 local_address < record.m_host_address + record.m_size)
746 {
747 if (record.m_process_address == LLDB_INVALID_ADDRESS)
748 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000749
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000750 return AddrRange(record.m_process_address, record.m_size);
751 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000752 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000753
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000754 return AddrRange (0, 0);
755}
756
757bool
758IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
759{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000760 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000761
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000762 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000763
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000764 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000765 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000766 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000767 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000768
Greg Clayton23f8c952014-03-24 23:10:19 +0000769 switch (record.m_sect_type)
770 {
771 case lldb::eSectionTypeInvalid:
772 case lldb::eSectionTypeDWARFDebugAbbrev:
773 case lldb::eSectionTypeDWARFDebugAranges:
774 case lldb::eSectionTypeDWARFDebugFrame:
775 case lldb::eSectionTypeDWARFDebugInfo:
776 case lldb::eSectionTypeDWARFDebugLine:
777 case lldb::eSectionTypeDWARFDebugLoc:
778 case lldb::eSectionTypeDWARFDebugMacInfo:
779 case lldb::eSectionTypeDWARFDebugPubNames:
780 case lldb::eSectionTypeDWARFDebugPubTypes:
781 case lldb::eSectionTypeDWARFDebugRanges:
782 case lldb::eSectionTypeDWARFDebugStr:
783 case lldb::eSectionTypeDWARFAppleNames:
784 case lldb::eSectionTypeDWARFAppleTypes:
785 case lldb::eSectionTypeDWARFAppleNamespaces:
786 case lldb::eSectionTypeDWARFAppleObjC:
787 err.Clear();
788 break;
789 default:
790 record.m_process_address = Malloc (record.m_size,
791 record.m_alignment,
792 record.m_permissions,
793 eAllocationPolicyProcessOnly,
794 err);
795 break;
796 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000797
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000798 if (!err.Success())
799 {
800 ret = false;
801 break;
802 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000803 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000804
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000805 if (!ret)
806 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000807 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000808 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000809 if (record.m_process_address != LLDB_INVALID_ADDRESS)
810 {
811 Free(record.m_process_address, err);
812 record.m_process_address = LLDB_INVALID_ADDRESS;
813 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000814 }
815 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000816
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000817 return ret;
818}
819
820void
821IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
822{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000823 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000824 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000825 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000826 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000827
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000828 if (record.m_section_id == eSectionIDInvalid)
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 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000832 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000834 // Trigger re-application of relocations.
835 engine.finalizeObject();
836}
837
838bool
839IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
840{
Greg Clayton23f8c952014-03-24 23:10:19 +0000841 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000842 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000843 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000844 if (record.m_process_address != LLDB_INVALID_ADDRESS)
845 {
846 lldb_private::Error err;
847 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
848 if (err.Success())
849 wrote_something = true;
850 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000851 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000852 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000853}
854
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000855void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000856IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000857{
858 if (!log)
859 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000861 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000862 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000863 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000864 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000865 (unsigned)m_alignment,
866 (unsigned)m_section_id);
867}
Greg Clayton23f8c952014-03-24 23:10:19 +0000868
869
870lldb::ByteOrder
871IRExecutionUnit::GetByteOrder () const
872{
873 ExecutionContext exe_ctx (GetBestExecutionContextScope());
874 return exe_ctx.GetByteOrder();
875}
876
877uint32_t
878IRExecutionUnit::GetAddressByteSize () const
879{
880 ExecutionContext exe_ctx (GetBestExecutionContextScope());
881 return exe_ctx.GetAddressByteSize();
882}
883
884void
885IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
886 lldb_private::Symtab &symtab)
887{
888 // No symbols yet...
889}
890
891
892void
893IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
894 lldb_private::SectionList &section_list)
895{
896 for (AllocationRecord &record : m_records)
897 {
898 if (record.m_size > 0)
899 {
900 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
901 obj_file,
902 record.m_section_id,
903 ConstString(record.m_name),
904 record.m_sect_type,
905 record.m_process_address,
906 record.m_size,
907 record.m_host_address, // file_offset (which is the host address for the data)
908 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000909 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000910 record.m_permissions)); // flags
911 section_list.AddSection (section_sp);
912 }
913 }
914}
915
916bool
917IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
918{
919 ExecutionContext exe_ctx (GetBestExecutionContextScope());
920 Target *target = exe_ctx.GetTargetPtr();
921 if (target)
922 arch = target->GetArchitecture();
923 else
924 arch.Clear();
925 return arch.IsValid();
926}
927
928lldb::ModuleSP
929IRExecutionUnit::GetJITModule ()
930{
931 ExecutionContext exe_ctx (GetBestExecutionContextScope());
932 Target *target = exe_ctx.GetTargetPtr();
933 if (target)
934 {
935 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
936 if (jit_module_sp)
937 {
938 bool changed = false;
939 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
940 }
941 return jit_module_sp;
942 }
943 return lldb::ModuleSP();
944}