blob: 3f19c508c9267ca4666f05ac62f3a6a232fd7d3c [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"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000014#include "llvm/Support/raw_ostream.h"
15
Sean Callanan8dfb68e2013-03-19 00:10:07 +000016#include "lldb/Core/DataBufferHeap.h"
17#include "lldb/Core/DataExtractor.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000018#include "lldb/Core/Debugger.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000019#include "lldb/Core/Disassembler.h"
20#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000021#include "lldb/Core/Module.h"
22#include "lldb/Core/Section.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000023#include "lldb/Symbol/SymbolContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000024#include "lldb/Expression/IRExecutionUnit.h"
25#include "lldb/Target/ExecutionContext.h"
26#include "lldb/Target/Target.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000027#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028
29using namespace lldb_private;
30
Greg Clayton7b0992d2013-04-18 22:45:39 +000031IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
32 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000033 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000034 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000035 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000036 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000037 m_context_ap(context_ap.release()),
38 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000039 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000040 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000041 m_name(name),
42 m_did_jit(false),
43 m_function_load_addr(LLDB_INVALID_ADDRESS),
44 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
45{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000046}
47
48lldb::addr_t
49IRExecutionUnit::WriteNow (const uint8_t *bytes,
50 size_t size,
51 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000052{
Jim Ingham2c381412015-11-04 20:32:27 +000053 const bool zero_memory = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +000054 lldb::addr_t allocation_process_addr = Malloc (size,
55 8,
56 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
57 eAllocationPolicyMirror,
Jim Ingham2c381412015-11-04 20:32:27 +000058 zero_memory,
Sean Callanan5a1af4e2013-04-05 02:22:57 +000059 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000060
Sean Callanan5a1af4e2013-04-05 02:22:57 +000061 if (!error.Success())
62 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Sean Callanan5a1af4e2013-04-05 02:22:57 +000064 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000065
Sean Callanan5a1af4e2013-04-05 02:22:57 +000066 if (!error.Success())
67 {
68 Error err;
69 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000070
Sean Callanan5a1af4e2013-04-05 02:22:57 +000071 return LLDB_INVALID_ADDRESS;
72 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000073
Sean Callanan5a1af4e2013-04-05 02:22:57 +000074 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
75 {
76 DataBufferHeap my_buffer(size, 0);
77 Error err;
78 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000079
Sean Callanan5a1af4e2013-04-05 02:22:57 +000080 if (err.Success())
81 {
82 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000083 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000084 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000085 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000086
Sean Callanan5a1af4e2013-04-05 02:22:57 +000087 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000088}
89
90void
91IRExecutionUnit::FreeNow (lldb::addr_t allocation)
92{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000093 if (allocation == LLDB_INVALID_ADDRESS)
94 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000095
Sean Callanan5a1af4e2013-04-05 02:22:57 +000096 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000097
Sean Callanan5a1af4e2013-04-05 02:22:57 +000098 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000099}
100
101Error
102IRExecutionUnit::DisassembleFunction (Stream &stream,
103 lldb::ProcessSP &process_wp)
104{
Greg Clayton5160ce52013-03-27 23:08:40 +0000105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000106
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000107 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000110
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000111 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000112
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000113 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
114 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000115
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000116 for (JittedFunction &function : m_jitted_functions)
117 {
118 if (strstr(function.m_name.c_str(), m_name.AsCString()))
119 {
120 func_local_addr = function.m_local_addr;
121 func_remote_addr = function.m_remote_addr;
122 }
123 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000125 if (func_local_addr == LLDB_INVALID_ADDRESS)
126 {
127 ret.SetErrorToGenericError();
128 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
129 return ret;
130 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000131
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000132 if (log)
133 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 +0000134
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000135 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000136
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000137 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000138
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000139 if (func_range.first == 0 && func_range.second == 0)
140 {
141 ret.SetErrorToGenericError();
142 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
143 return ret;
144 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000145
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000146 if (log)
147 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000148
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000149 Target *target = exe_ctx.GetTargetPtr();
150 if (!target)
151 {
152 ret.SetErrorToGenericError();
153 ret.SetErrorString("Couldn't find the target");
154 return ret;
155 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000157 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000158
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000159 Process *process = exe_ctx.GetProcessPtr();
160 Error err;
161 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000163 if (!err.Success())
164 {
165 ret.SetErrorToGenericError();
166 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
167 return ret;
168 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000170 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000171
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000172 const char *plugin_name = NULL;
173 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000174 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Jim Ingham56d40422013-07-31 02:19:15 +0000176 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000177 {
178 ret.SetErrorToGenericError();
179 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
180 return ret;
181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000182
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000183 if (!process)
184 {
185 ret.SetErrorToGenericError();
186 ret.SetErrorString("Couldn't find the process");
187 return ret;
188 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000189
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000190 DataExtractor extractor(buffer_sp,
191 process->GetByteOrder(),
192 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000193
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000194 if (log)
195 {
196 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000197 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000198 0,
199 extractor.GetByteSize(),
200 func_remote_addr,
201 16,
202 DataExtractor::TypeUInt8);
203 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000204
Jim Ingham56d40422013-07-31 02:19:15 +0000205 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000206
Jim Ingham56d40422013-07-31 02:19:15 +0000207 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Greg Clayton554f68d2015-02-04 22:00:53 +0000208 instruction_list.Dump(&stream, true, true, &exe_ctx);
209
Jim Ingham56d40422013-07-31 02:19:15 +0000210 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
211 // I'll fix that but for now, just clear the list and it will go away nicely.
212 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000213 return ret;
214}
215
Sean Callanan44bc6572013-03-27 03:09:55 +0000216static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
217{
218 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000219
Sean Callanan44bc6572013-03-27 03:09:55 +0000220 if (err && err->Success())
221 {
222 err->SetErrorToGenericError();
223 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
224 }
225}
226
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000227void
Jim Ingham27e5fe82015-01-27 18:03:05 +0000228IRExecutionUnit::ReportSymbolLookupError(const ConstString &name)
229{
230 m_failed_lookups.push_back(name);
231}
232
233void
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000234IRExecutionUnit::GetRunnableInfo(Error &error,
235 lldb::addr_t &func_addr,
236 lldb::addr_t &func_end)
237{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000238 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000239
Sean Callanan7c435a52014-02-06 22:25:20 +0000240 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000241
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000242 func_addr = LLDB_INVALID_ADDRESS;
243 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000245 if (!process_sp)
246 {
247 error.SetErrorToGenericError();
248 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
249 return;
250 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000251
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000252 if (m_did_jit)
253 {
254 func_addr = m_function_load_addr;
255 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000256
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000257 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000258 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000259
Sean Callanan7c435a52014-02-06 22:25:20 +0000260 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000262 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000265
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000266 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000267
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000268 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000269 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000270 std::string s;
271 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000272
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000273 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000274
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000275 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000276
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000277 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
278 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000279
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000280 llvm::Triple triple(m_module->getTargetTriple());
281 llvm::Function *function = m_module->getFunction (m_name.AsCString());
282 llvm::Reloc::Model relocModel;
283 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000285 if (triple.isOSBinFormatELF())
286 {
287 relocModel = llvm::Reloc::Static;
288 // This will be small for 32-bit and large for 64-bit.
289 codeModel = llvm::CodeModel::JITDefault;
290 }
291 else
292 {
293 relocModel = llvm::Reloc::PIC_;
294 codeModel = llvm::CodeModel::Small;
295 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000296
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000297 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000298
Rafael Espindola7c03d952014-08-19 04:27:03 +0000299 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000300
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000301 builder.setEngineKind(llvm::EngineKind::JIT)
302 .setErrorStr(&error_string)
303 .setRelocationModel(relocModel)
David Majnemerb313e462014-12-04 21:26:25 +0000304 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
David Majnemer8faf9372014-09-16 06:34:29 +0000305 .setCodeModel(codeModel)
Reid Kleckner54209532014-09-03 00:40:36 +0000306 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000308 llvm::StringRef mArch;
309 llvm::StringRef mCPU;
310 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000311
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000312 for (std::string &feature : m_cpu_features)
313 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000314
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000315 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
316 mArch,
317 mCPU,
318 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000320 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000321
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000322 if (!m_execution_engine_ap.get())
323 {
324 error.SetErrorToGenericError();
325 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000326 return;
327 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000328
Greg Clayton23f8c952014-03-24 23:10:19 +0000329 // Make sure we see all sections, including ones that don't have relocations...
330 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000331
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000332 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000333
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000334 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000335
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000336 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000337
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000338 if (!error.Success())
339 {
340 // We got an error through our callback!
341 return;
342 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000344 if (!function)
345 {
346 error.SetErrorToGenericError();
347 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
348 return;
349 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000351 if (!fun_ptr)
352 {
353 error.SetErrorToGenericError();
354 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
355 return;
356 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000358 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000359
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000360 CommitAllocations(process_sp);
361 ReportAllocations(*m_execution_engine_ap);
362 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Jim Ingham27e5fe82015-01-27 18:03:05 +0000364 if (m_failed_lookups.size())
365 {
366 StreamString ss;
367
368 ss.PutCString("Couldn't lookup symbols:\n");
369
370 bool emitNewLine = false;
371
372 for (const ConstString &failed_lookup : m_failed_lookups)
373 {
374 if (emitNewLine)
375 ss.PutCString("\n");
376 emitNewLine = true;
377 ss.PutCString(" ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000378 ss.PutCString(Mangled(failed_lookup).GetDemangledName(lldb::eLanguageTypeObjC_plus_plus).AsCString());
Jim Ingham27e5fe82015-01-27 18:03:05 +0000379 }
380
381 m_failed_lookups.clear();
382
383 error.SetErrorString(ss.GetData());
384
385 return;
386 }
387
388 m_function_load_addr = LLDB_INVALID_ADDRESS;
389 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
390
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000391 for (JittedFunction &jitted_function : m_jitted_functions)
392 {
393 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000394
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000395 if (!jitted_function.m_name.compare(m_name.AsCString()))
396 {
397 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
398 m_function_end_load_addr = func_range.first + func_range.second;
399 m_function_load_addr = jitted_function.m_remote_addr;
400 }
401 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000402
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000403 if (log)
404 {
405 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000407 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000409 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000411 if (!err.Success())
412 {
413 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
414 }
415 else
416 {
417 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
418 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000419
Sean Callanan4f2c1982014-01-21 00:54:48 +0000420 log->Printf("Sections: ");
421 for (AllocationRecord &record : m_records)
422 {
423 if (record.m_process_address != LLDB_INVALID_ADDRESS)
424 {
425 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000426
Sean Callanan4f2c1982014-01-21 00:54:48 +0000427 DataBufferHeap my_buffer(record.m_size, 0);
428 Error err;
429 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000430
Sean Callanan4f2c1982014-01-21 00:54:48 +0000431 if (err.Success())
432 {
433 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
434 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
435 }
436 }
Sean Callanan44dbf112015-10-23 00:39:09 +0000437 else
438 {
439 record.dump(log);
440
441 DataExtractor my_extractor ((const void*)record.m_host_address, record.m_size, lldb::eByteOrderBig, 8);
442 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16, DataExtractor::TypeUInt8);
443 }
Sean Callanan4f2c1982014-01-21 00:54:48 +0000444 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000445 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000447 func_addr = m_function_load_addr;
448 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000450 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000451}
452
453IRExecutionUnit::~IRExecutionUnit ()
454{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000455 m_module_ap.reset();
456 m_execution_engine_ap.reset();
457 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000458}
459
460IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000461 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000462 m_parent (parent)
463{
464}
465
Greg Clayton23f8c952014-03-24 23:10:19 +0000466IRExecutionUnit::MemoryManager::~MemoryManager ()
467{
468}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000469
Greg Clayton23f8c952014-03-24 23:10:19 +0000470lldb::SectionType
471IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
472{
473 lldb::SectionType sect_type = lldb::eSectionTypeCode;
474 switch (alloc_kind)
475 {
476 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
477 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
478 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
479 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
480 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
481 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000482
Greg Clayton23f8c952014-03-24 23:10:19 +0000483 if (!name.empty())
484 {
485 if (name.equals("__text") || name.equals(".text"))
486 sect_type = lldb::eSectionTypeCode;
487 else if (name.equals("__data") || name.equals(".data"))
488 sect_type = lldb::eSectionTypeCode;
489 else if (name.startswith("__debug_") || name.startswith(".debug_"))
490 {
491 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
492 llvm::StringRef dwarf_name(name.substr(name_idx));
493 switch (dwarf_name[0])
494 {
495 case 'a':
496 if (dwarf_name.equals("abbrev"))
497 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
498 else if (dwarf_name.equals("aranges"))
499 sect_type = lldb::eSectionTypeDWARFDebugAranges;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000500 else if (dwarf_name.equals("addr"))
501 sect_type = lldb::eSectionTypeDWARFDebugAddr;
Greg Clayton23f8c952014-03-24 23:10:19 +0000502 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000503
Greg Clayton23f8c952014-03-24 23:10:19 +0000504 case 'f':
505 if (dwarf_name.equals("frame"))
506 sect_type = lldb::eSectionTypeDWARFDebugFrame;
507 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Greg Clayton23f8c952014-03-24 23:10:19 +0000509 case 'i':
510 if (dwarf_name.equals("info"))
511 sect_type = lldb::eSectionTypeDWARFDebugInfo;
512 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000513
Greg Clayton23f8c952014-03-24 23:10:19 +0000514 case 'l':
515 if (dwarf_name.equals("line"))
516 sect_type = lldb::eSectionTypeDWARFDebugLine;
517 else if (dwarf_name.equals("loc"))
518 sect_type = lldb::eSectionTypeDWARFDebugLoc;
519 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Greg Clayton23f8c952014-03-24 23:10:19 +0000521 case 'm':
522 if (dwarf_name.equals("macinfo"))
523 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
524 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000525
Greg Clayton23f8c952014-03-24 23:10:19 +0000526 case 'p':
527 if (dwarf_name.equals("pubnames"))
528 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
529 else if (dwarf_name.equals("pubtypes"))
530 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
531 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000532
Greg Clayton23f8c952014-03-24 23:10:19 +0000533 case 's':
534 if (dwarf_name.equals("str"))
535 sect_type = lldb::eSectionTypeDWARFDebugStr;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000536 else if (dwarf_name.equals("str_offsets"))
537 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
Greg Clayton23f8c952014-03-24 23:10:19 +0000538 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000539
Greg Clayton23f8c952014-03-24 23:10:19 +0000540 case 'r':
541 if (dwarf_name.equals("ranges"))
542 sect_type = lldb::eSectionTypeDWARFDebugRanges;
543 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Greg Clayton23f8c952014-03-24 23:10:19 +0000545 default:
546 break;
547 }
548 }
549 else if (name.startswith("__apple_") || name.startswith(".apple_"))
550 {
551#if 0
552 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
553 llvm::StringRef apple_name(name.substr(name_idx));
554 switch (apple_name[0])
555 {
556 case 'n':
557 if (apple_name.equals("names"))
558 sect_type = lldb::eSectionTypeDWARFAppleNames;
559 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
560 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
561 break;
562 case 't':
563 if (apple_name.equals("types"))
564 sect_type = lldb::eSectionTypeDWARFAppleTypes;
565 break;
566 case 'o':
567 if (apple_name.equals("objc"))
568 sect_type = lldb::eSectionTypeDWARFAppleObjC;
569 break;
570 default:
571 break;
572 }
573#else
574 sect_type = lldb::eSectionTypeInvalid;
575#endif
576 }
577 else if (name.equals("__objc_imageinfo"))
578 sect_type = lldb::eSectionTypeOther;
579 }
580 return sect_type;
581}
582
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000583uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000584IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
585 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000586 unsigned SectionID,
587 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000588{
Greg Clayton5160ce52013-03-27 23:08:40 +0000589 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000590
Filip Pizlobfcff682013-10-02 01:43:46 +0000591 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000593 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000594 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000595 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000596 Size,
597 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000598 SectionID,
599 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000600
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000601 if (log)
602 {
603 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000604 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000605 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000607 return return_value;
608}
609
610uint8_t *
611IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
612 unsigned Alignment,
613 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000614 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000615 bool IsReadOnly)
616{
Greg Clayton5160ce52013-03-27 23:08:40 +0000617 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000618
Filip Pizlobfcff682013-10-02 01:43:46 +0000619 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000621 uint32_t permissions = lldb::ePermissionsReadable;
622 if (!IsReadOnly)
623 permissions |= lldb::ePermissionsWritable;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000624 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000625 permissions,
Greg Clayton23f8c952014-03-24 23:10:19 +0000626 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000627 Size,
628 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000629 SectionID,
630 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000631 if (log)
632 {
633 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000634 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000635 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
637 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000638}
639
Jim Ingham27e5fe82015-01-27 18:03:05 +0000640uint64_t
641IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name)
642{
643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
644
645 SymbolContextList sc_list;
646
647 ExecutionContextScope *exe_scope = m_parent.GetBestExecutionContextScope();
648
649 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
650
651 const char *name = Name.c_str();
652
653 ConstString bare_name_cs(name);
654 ConstString name_cs;
655
656 if (name[0] == '_')
657 name_cs = ConstString(name + 1);
658
659 if (!target_sp)
660 {
661 if (log)
662 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <no target>",
663 Name.c_str());
664
665 m_parent.ReportSymbolLookupError(name_cs);
666
667 return 0xbad0bad0;
668 }
669
670 uint32_t num_matches = 0;
671 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
672
673 if (!name_cs.IsEmpty())
674 {
675 target_sp->GetImages().FindSymbolsWithNameAndType(name_cs, lldb::eSymbolTypeAny, sc_list);
676 num_matches = sc_list.GetSize();
677 }
678
679 if (!num_matches)
680 {
681 target_sp->GetImages().FindSymbolsWithNameAndType(bare_name_cs, lldb::eSymbolTypeAny, sc_list);
682 num_matches = sc_list.GetSize();
683 }
684
685 lldb::addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
686
687 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
688 {
689 SymbolContext sym_ctx;
690 sc_list.GetContextAtIndex(i, sym_ctx);
691
Jim Ingham27e5fe82015-01-27 18:03:05 +0000692 symbol_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target_sp);
Greg Claytond6171a72015-07-02 16:43:49 +0000693
Jim Ingham27e5fe82015-01-27 18:03:05 +0000694 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
Greg Claytond6171a72015-07-02 16:43:49 +0000695 symbol_load_addr = sym_ctx.symbol->GetAddress().GetLoadAddress(target_sp.get());
Jim Ingham27e5fe82015-01-27 18:03:05 +0000696 }
697
698 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process_sp && name_cs)
699 {
700 // Try the Objective-C language runtime.
701
702 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
703
704 if (runtime)
705 symbol_load_addr = runtime->LookupRuntimeSymbol(name_cs);
706 }
707
708 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
709 {
710 if (log)
711 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
712 name);
713
714 m_parent.ReportSymbolLookupError(bare_name_cs);
715
716 return 0xbad0bad0;
717 }
718
719 if (log)
720 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
721 name,
722 symbol_load_addr);
723
724 if (symbol_load_addr == 0)
725 return 0xbad00add;
726
727 return symbol_load_addr;
728}
729
730void *
731IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name,
732 bool AbortOnFailure) {
733 assert (sizeof(void *) == 8);
734
735 return (void*)getSymbolAddress(Name);
736}
737
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000738lldb::addr_t
739IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
740{
Sean Callananffae9442013-06-27 01:42:47 +0000741 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
742
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000743 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000744 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000745 if (local_address >= record.m_host_address &&
746 local_address < record.m_host_address + record.m_size)
747 {
748 if (record.m_process_address == LLDB_INVALID_ADDRESS)
749 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000750
Sean Callananffae9442013-06-27 01:42:47 +0000751 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000752
Sean Callananffae9442013-06-27 01:42:47 +0000753 if (log)
754 {
755 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
756 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000757 (uint64_t)record.m_host_address,
758 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000759 ret,
760 record.m_process_address,
761 record.m_process_address + record.m_size);
762 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000763
Sean Callananffae9442013-06-27 01:42:47 +0000764 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000765 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000766 }
767
768 return LLDB_INVALID_ADDRESS;
769}
770
771IRExecutionUnit::AddrRange
772IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
773{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000774 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000775 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000776 if (local_address >= record.m_host_address &&
777 local_address < record.m_host_address + record.m_size)
778 {
779 if (record.m_process_address == LLDB_INVALID_ADDRESS)
780 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000781
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000782 return AddrRange(record.m_process_address, record.m_size);
783 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000784 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000786 return AddrRange (0, 0);
787}
788
789bool
790IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
791{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000792 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000793
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000794 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000795
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000796 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000797 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000798 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000799 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800
Greg Clayton23f8c952014-03-24 23:10:19 +0000801 switch (record.m_sect_type)
802 {
803 case lldb::eSectionTypeInvalid:
804 case lldb::eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000805 case lldb::eSectionTypeDWARFDebugAddr:
Greg Clayton23f8c952014-03-24 23:10:19 +0000806 case lldb::eSectionTypeDWARFDebugAranges:
807 case lldb::eSectionTypeDWARFDebugFrame:
808 case lldb::eSectionTypeDWARFDebugInfo:
809 case lldb::eSectionTypeDWARFDebugLine:
810 case lldb::eSectionTypeDWARFDebugLoc:
811 case lldb::eSectionTypeDWARFDebugMacInfo:
812 case lldb::eSectionTypeDWARFDebugPubNames:
813 case lldb::eSectionTypeDWARFDebugPubTypes:
814 case lldb::eSectionTypeDWARFDebugRanges:
815 case lldb::eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000816 case lldb::eSectionTypeDWARFDebugStrOffsets:
Greg Clayton23f8c952014-03-24 23:10:19 +0000817 case lldb::eSectionTypeDWARFAppleNames:
818 case lldb::eSectionTypeDWARFAppleTypes:
819 case lldb::eSectionTypeDWARFAppleNamespaces:
820 case lldb::eSectionTypeDWARFAppleObjC:
821 err.Clear();
822 break;
823 default:
Jim Ingham2c381412015-11-04 20:32:27 +0000824 const bool zero_memory = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000825 record.m_process_address = Malloc (record.m_size,
826 record.m_alignment,
827 record.m_permissions,
828 eAllocationPolicyProcessOnly,
Jim Ingham2c381412015-11-04 20:32:27 +0000829 zero_memory,
Greg Clayton23f8c952014-03-24 23:10:19 +0000830 err);
831 break;
832 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000834 if (!err.Success())
835 {
836 ret = false;
837 break;
838 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000839 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000840
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000841 if (!ret)
842 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000843 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000844 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000845 if (record.m_process_address != LLDB_INVALID_ADDRESS)
846 {
847 Free(record.m_process_address, err);
848 record.m_process_address = LLDB_INVALID_ADDRESS;
849 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000850 }
851 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000853 return ret;
854}
855
856void
857IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
858{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000859 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000860 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000861 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000862 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000863
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000864 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000865 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000866
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000867 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000868 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000869
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000870 // Trigger re-application of relocations.
871 engine.finalizeObject();
872}
873
874bool
875IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
876{
Greg Clayton23f8c952014-03-24 23:10:19 +0000877 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000878 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000879 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000880 if (record.m_process_address != LLDB_INVALID_ADDRESS)
881 {
882 lldb_private::Error err;
883 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
884 if (err.Success())
885 wrote_something = true;
886 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000887 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000888 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000889}
890
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000891void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000892IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000893{
894 if (!log)
895 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000896
Sean Callanan44dbf112015-10-23 00:39:09 +0000897 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000898 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000899 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000900 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000901 (unsigned)m_alignment,
Sean Callanan44dbf112015-10-23 00:39:09 +0000902 (unsigned)m_section_id,
903 m_name.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000904}
Greg Clayton23f8c952014-03-24 23:10:19 +0000905
906
907lldb::ByteOrder
908IRExecutionUnit::GetByteOrder () const
909{
910 ExecutionContext exe_ctx (GetBestExecutionContextScope());
911 return exe_ctx.GetByteOrder();
912}
913
914uint32_t
915IRExecutionUnit::GetAddressByteSize () const
916{
917 ExecutionContext exe_ctx (GetBestExecutionContextScope());
918 return exe_ctx.GetAddressByteSize();
919}
920
921void
922IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
923 lldb_private::Symtab &symtab)
924{
925 // No symbols yet...
926}
927
928
929void
930IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
931 lldb_private::SectionList &section_list)
932{
933 for (AllocationRecord &record : m_records)
934 {
935 if (record.m_size > 0)
936 {
937 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
938 obj_file,
939 record.m_section_id,
940 ConstString(record.m_name),
941 record.m_sect_type,
942 record.m_process_address,
943 record.m_size,
944 record.m_host_address, // file_offset (which is the host address for the data)
945 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000946 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000947 record.m_permissions)); // flags
948 section_list.AddSection (section_sp);
949 }
950 }
951}
952
953bool
954IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
955{
956 ExecutionContext exe_ctx (GetBestExecutionContextScope());
957 Target *target = exe_ctx.GetTargetPtr();
958 if (target)
959 arch = target->GetArchitecture();
960 else
961 arch.Clear();
962 return arch.IsValid();
963}
964
965lldb::ModuleSP
966IRExecutionUnit::GetJITModule ()
967{
968 ExecutionContext exe_ctx (GetBestExecutionContextScope());
969 Target *target = exe_ctx.GetTargetPtr();
970 if (target)
971 {
972 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
973 if (jit_module_sp)
974 {
975 bool changed = false;
976 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
977 }
978 return jit_module_sp;
979 }
980 return lldb::ModuleSP();
981}