blob: 156a65cf185c4aa6b5c70a10e13cc4e656151a17 [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{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000053 lldb::addr_t allocation_process_addr = Malloc (size,
54 8,
55 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
56 eAllocationPolicyMirror,
57 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000058
Sean Callanan5a1af4e2013-04-05 02:22:57 +000059 if (!error.Success())
60 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000061
Sean Callanan5a1af4e2013-04-05 02:22:57 +000062 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Sean Callanan5a1af4e2013-04-05 02:22:57 +000064 if (!error.Success())
65 {
66 Error err;
67 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000068
Sean Callanan5a1af4e2013-04-05 02:22:57 +000069 return LLDB_INVALID_ADDRESS;
70 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000071
Sean Callanan5a1af4e2013-04-05 02:22:57 +000072 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
73 {
74 DataBufferHeap my_buffer(size, 0);
75 Error err;
76 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000077
Sean Callanan5a1af4e2013-04-05 02:22:57 +000078 if (err.Success())
79 {
80 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000081 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000082 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000083 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000084
Sean Callanan5a1af4e2013-04-05 02:22:57 +000085 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000086}
87
88void
89IRExecutionUnit::FreeNow (lldb::addr_t allocation)
90{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000091 if (allocation == LLDB_INVALID_ADDRESS)
92 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000093
Sean Callanan5a1af4e2013-04-05 02:22:57 +000094 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000095
Sean Callanan5a1af4e2013-04-05 02:22:57 +000096 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000097}
98
99Error
100IRExecutionUnit::DisassembleFunction (Stream &stream,
101 lldb::ProcessSP &process_wp)
102{
Greg Clayton5160ce52013-03-27 23:08:40 +0000103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000104
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000105 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000106
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000107 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000110
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000111 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
112 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000113
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000114 for (JittedFunction &function : m_jitted_functions)
115 {
116 if (strstr(function.m_name.c_str(), m_name.AsCString()))
117 {
118 func_local_addr = function.m_local_addr;
119 func_remote_addr = function.m_remote_addr;
120 }
121 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000122
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000123 if (func_local_addr == LLDB_INVALID_ADDRESS)
124 {
125 ret.SetErrorToGenericError();
126 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
127 return ret;
128 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000130 if (log)
131 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 +0000132
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000133 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000134
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000135 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000136
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000137 if (func_range.first == 0 && func_range.second == 0)
138 {
139 ret.SetErrorToGenericError();
140 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
141 return ret;
142 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000143
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000144 if (log)
145 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000146
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000147 Target *target = exe_ctx.GetTargetPtr();
148 if (!target)
149 {
150 ret.SetErrorToGenericError();
151 ret.SetErrorString("Couldn't find the target");
152 return ret;
153 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000154
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000155 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000157 Process *process = exe_ctx.GetProcessPtr();
158 Error err;
159 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000160
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000161 if (!err.Success())
162 {
163 ret.SetErrorToGenericError();
164 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
165 return ret;
166 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000168 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000170 const char *plugin_name = NULL;
171 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000172 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000173
Jim Ingham56d40422013-07-31 02:19:15 +0000174 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000175 {
176 ret.SetErrorToGenericError();
177 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
178 return ret;
179 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000180
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000181 if (!process)
182 {
183 ret.SetErrorToGenericError();
184 ret.SetErrorString("Couldn't find the process");
185 return ret;
186 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000188 DataExtractor extractor(buffer_sp,
189 process->GetByteOrder(),
190 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000191
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000192 if (log)
193 {
194 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000195 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000196 0,
197 extractor.GetByteSize(),
198 func_remote_addr,
199 16,
200 DataExtractor::TypeUInt8);
201 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Jim Ingham56d40422013-07-31 02:19:15 +0000203 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000204
Jim Ingham56d40422013-07-31 02:19:15 +0000205 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Greg Clayton554f68d2015-02-04 22:00:53 +0000206 instruction_list.Dump(&stream, true, true, &exe_ctx);
207
Jim Ingham56d40422013-07-31 02:19:15 +0000208 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
209 // I'll fix that but for now, just clear the list and it will go away nicely.
210 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000211 return ret;
212}
213
Sean Callanan44bc6572013-03-27 03:09:55 +0000214static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
215{
216 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000217
Sean Callanan44bc6572013-03-27 03:09:55 +0000218 if (err && err->Success())
219 {
220 err->SetErrorToGenericError();
221 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
222 }
223}
224
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000225void
Jim Ingham27e5fe82015-01-27 18:03:05 +0000226IRExecutionUnit::ReportSymbolLookupError(const ConstString &name)
227{
228 m_failed_lookups.push_back(name);
229}
230
231void
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000232IRExecutionUnit::GetRunnableInfo(Error &error,
233 lldb::addr_t &func_addr,
234 lldb::addr_t &func_end)
235{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000236 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000237
Sean Callanan7c435a52014-02-06 22:25:20 +0000238 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000239
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000240 func_addr = LLDB_INVALID_ADDRESS;
241 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000242
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000243 if (!process_sp)
244 {
245 error.SetErrorToGenericError();
246 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
247 return;
248 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000250 if (m_did_jit)
251 {
252 func_addr = m_function_load_addr;
253 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000254
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000255 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000256 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000257
Sean Callanan7c435a52014-02-06 22:25:20 +0000258 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000259
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000260 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000264 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000265
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000266 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000267 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000268 std::string s;
269 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000270
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000271 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000272
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000273 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000274
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000275 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
276 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000277
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 llvm::Triple triple(m_module->getTargetTriple());
279 llvm::Function *function = m_module->getFunction (m_name.AsCString());
280 llvm::Reloc::Model relocModel;
281 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000282
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000283 if (triple.isOSBinFormatELF())
284 {
285 relocModel = llvm::Reloc::Static;
286 // This will be small for 32-bit and large for 64-bit.
287 codeModel = llvm::CodeModel::JITDefault;
288 }
289 else
290 {
291 relocModel = llvm::Reloc::PIC_;
292 codeModel = llvm::CodeModel::Small;
293 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000295 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000296
Rafael Espindola7c03d952014-08-19 04:27:03 +0000297 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000298
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000299 builder.setEngineKind(llvm::EngineKind::JIT)
300 .setErrorStr(&error_string)
301 .setRelocationModel(relocModel)
David Majnemerb313e462014-12-04 21:26:25 +0000302 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
David Majnemer8faf9372014-09-16 06:34:29 +0000303 .setCodeModel(codeModel)
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
Jim Ingham27e5fe82015-01-27 18:03:05 +0000362 if (m_failed_lookups.size())
363 {
364 StreamString ss;
365
366 ss.PutCString("Couldn't lookup symbols:\n");
367
368 bool emitNewLine = false;
369
370 for (const ConstString &failed_lookup : m_failed_lookups)
371 {
372 if (emitNewLine)
373 ss.PutCString("\n");
374 emitNewLine = true;
375 ss.PutCString(" ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000376 ss.PutCString(Mangled(failed_lookup).GetDemangledName(lldb::eLanguageTypeObjC_plus_plus).AsCString());
Jim Ingham27e5fe82015-01-27 18:03:05 +0000377 }
378
379 m_failed_lookups.clear();
380
381 error.SetErrorString(ss.GetData());
382
383 return;
384 }
385
386 m_function_load_addr = LLDB_INVALID_ADDRESS;
387 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
388
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000389 for (JittedFunction &jitted_function : m_jitted_functions)
390 {
391 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000392
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000393 if (!jitted_function.m_name.compare(m_name.AsCString()))
394 {
395 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
396 m_function_end_load_addr = func_range.first + func_range.second;
397 m_function_load_addr = jitted_function.m_remote_addr;
398 }
399 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000400
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000401 if (log)
402 {
403 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000404
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000405 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000407 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000409 if (!err.Success())
410 {
411 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
412 }
413 else
414 {
415 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
416 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000417
Sean Callanan4f2c1982014-01-21 00:54:48 +0000418 log->Printf("Sections: ");
419 for (AllocationRecord &record : m_records)
420 {
421 if (record.m_process_address != LLDB_INVALID_ADDRESS)
422 {
423 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000424
Sean Callanan4f2c1982014-01-21 00:54:48 +0000425 DataBufferHeap my_buffer(record.m_size, 0);
426 Error err;
427 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Sean Callanan4f2c1982014-01-21 00:54:48 +0000429 if (err.Success())
430 {
431 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
432 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
433 }
434 }
435 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000436 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000437
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000438 func_addr = m_function_load_addr;
439 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000441 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000442}
443
444IRExecutionUnit::~IRExecutionUnit ()
445{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000446 m_module_ap.reset();
447 m_execution_engine_ap.reset();
448 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000449}
450
451IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000452 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000453 m_parent (parent)
454{
455}
456
Greg Clayton23f8c952014-03-24 23:10:19 +0000457IRExecutionUnit::MemoryManager::~MemoryManager ()
458{
459}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000460
Greg Clayton23f8c952014-03-24 23:10:19 +0000461lldb::SectionType
462IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
463{
464 lldb::SectionType sect_type = lldb::eSectionTypeCode;
465 switch (alloc_kind)
466 {
467 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
468 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
469 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
470 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
471 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
472 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000473
Greg Clayton23f8c952014-03-24 23:10:19 +0000474 if (!name.empty())
475 {
476 if (name.equals("__text") || name.equals(".text"))
477 sect_type = lldb::eSectionTypeCode;
478 else if (name.equals("__data") || name.equals(".data"))
479 sect_type = lldb::eSectionTypeCode;
480 else if (name.startswith("__debug_") || name.startswith(".debug_"))
481 {
482 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
483 llvm::StringRef dwarf_name(name.substr(name_idx));
484 switch (dwarf_name[0])
485 {
486 case 'a':
487 if (dwarf_name.equals("abbrev"))
488 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
489 else if (dwarf_name.equals("aranges"))
490 sect_type = lldb::eSectionTypeDWARFDebugAranges;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000491 else if (dwarf_name.equals("addr"))
492 sect_type = lldb::eSectionTypeDWARFDebugAddr;
Greg Clayton23f8c952014-03-24 23:10:19 +0000493 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000494
Greg Clayton23f8c952014-03-24 23:10:19 +0000495 case 'f':
496 if (dwarf_name.equals("frame"))
497 sect_type = lldb::eSectionTypeDWARFDebugFrame;
498 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000499
Greg Clayton23f8c952014-03-24 23:10:19 +0000500 case 'i':
501 if (dwarf_name.equals("info"))
502 sect_type = lldb::eSectionTypeDWARFDebugInfo;
503 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000504
Greg Clayton23f8c952014-03-24 23:10:19 +0000505 case 'l':
506 if (dwarf_name.equals("line"))
507 sect_type = lldb::eSectionTypeDWARFDebugLine;
508 else if (dwarf_name.equals("loc"))
509 sect_type = lldb::eSectionTypeDWARFDebugLoc;
510 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000511
Greg Clayton23f8c952014-03-24 23:10:19 +0000512 case 'm':
513 if (dwarf_name.equals("macinfo"))
514 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
515 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000516
Greg Clayton23f8c952014-03-24 23:10:19 +0000517 case 'p':
518 if (dwarf_name.equals("pubnames"))
519 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
520 else if (dwarf_name.equals("pubtypes"))
521 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
522 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000523
Greg Clayton23f8c952014-03-24 23:10:19 +0000524 case 's':
525 if (dwarf_name.equals("str"))
526 sect_type = lldb::eSectionTypeDWARFDebugStr;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000527 else if (dwarf_name.equals("str_offsets"))
528 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
Greg Clayton23f8c952014-03-24 23:10:19 +0000529 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000530
Greg Clayton23f8c952014-03-24 23:10:19 +0000531 case 'r':
532 if (dwarf_name.equals("ranges"))
533 sect_type = lldb::eSectionTypeDWARFDebugRanges;
534 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Greg Clayton23f8c952014-03-24 23:10:19 +0000536 default:
537 break;
538 }
539 }
540 else if (name.startswith("__apple_") || name.startswith(".apple_"))
541 {
542#if 0
543 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
544 llvm::StringRef apple_name(name.substr(name_idx));
545 switch (apple_name[0])
546 {
547 case 'n':
548 if (apple_name.equals("names"))
549 sect_type = lldb::eSectionTypeDWARFAppleNames;
550 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
551 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
552 break;
553 case 't':
554 if (apple_name.equals("types"))
555 sect_type = lldb::eSectionTypeDWARFAppleTypes;
556 break;
557 case 'o':
558 if (apple_name.equals("objc"))
559 sect_type = lldb::eSectionTypeDWARFAppleObjC;
560 break;
561 default:
562 break;
563 }
564#else
565 sect_type = lldb::eSectionTypeInvalid;
566#endif
567 }
568 else if (name.equals("__objc_imageinfo"))
569 sect_type = lldb::eSectionTypeOther;
570 }
571 return sect_type;
572}
573
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000574uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000575IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
576 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000577 unsigned SectionID,
578 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000579{
Greg Clayton5160ce52013-03-27 23:08:40 +0000580 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000581
Filip Pizlobfcff682013-10-02 01:43:46 +0000582 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000583
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000584 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000585 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000586 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000587 Size,
588 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000589 SectionID,
590 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000591
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000592 if (log)
593 {
594 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000595 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000596 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000597
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000598 return return_value;
599}
600
601uint8_t *
602IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
603 unsigned Alignment,
604 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000605 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000606 bool IsReadOnly)
607{
Greg Clayton5160ce52013-03-27 23:08:40 +0000608 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000609
Filip Pizlobfcff682013-10-02 01:43:46 +0000610 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000612 uint32_t permissions = lldb::ePermissionsReadable;
613 if (!IsReadOnly)
614 permissions |= lldb::ePermissionsWritable;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000615 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000616 permissions,
Greg Clayton23f8c952014-03-24 23:10:19 +0000617 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000618 Size,
619 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000620 SectionID,
621 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000622 if (log)
623 {
624 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000625 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000626 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000627
628 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000629}
630
Jim Ingham27e5fe82015-01-27 18:03:05 +0000631uint64_t
632IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name)
633{
634 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
635
636 SymbolContextList sc_list;
637
638 ExecutionContextScope *exe_scope = m_parent.GetBestExecutionContextScope();
639
640 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
641
642 const char *name = Name.c_str();
643
644 ConstString bare_name_cs(name);
645 ConstString name_cs;
646
647 if (name[0] == '_')
648 name_cs = ConstString(name + 1);
649
650 if (!target_sp)
651 {
652 if (log)
653 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <no target>",
654 Name.c_str());
655
656 m_parent.ReportSymbolLookupError(name_cs);
657
658 return 0xbad0bad0;
659 }
660
661 uint32_t num_matches = 0;
662 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
663
664 if (!name_cs.IsEmpty())
665 {
666 target_sp->GetImages().FindSymbolsWithNameAndType(name_cs, lldb::eSymbolTypeAny, sc_list);
667 num_matches = sc_list.GetSize();
668 }
669
670 if (!num_matches)
671 {
672 target_sp->GetImages().FindSymbolsWithNameAndType(bare_name_cs, lldb::eSymbolTypeAny, sc_list);
673 num_matches = sc_list.GetSize();
674 }
675
676 lldb::addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
677
678 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++)
679 {
680 SymbolContext sym_ctx;
681 sc_list.GetContextAtIndex(i, sym_ctx);
682
Jim Ingham27e5fe82015-01-27 18:03:05 +0000683 symbol_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target_sp);
Greg Claytond6171a72015-07-02 16:43:49 +0000684
Jim Ingham27e5fe82015-01-27 18:03:05 +0000685 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
Greg Claytond6171a72015-07-02 16:43:49 +0000686 symbol_load_addr = sym_ctx.symbol->GetAddress().GetLoadAddress(target_sp.get());
Jim Ingham27e5fe82015-01-27 18:03:05 +0000687 }
688
689 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process_sp && name_cs)
690 {
691 // Try the Objective-C language runtime.
692
693 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
694
695 if (runtime)
696 symbol_load_addr = runtime->LookupRuntimeSymbol(name_cs);
697 }
698
699 if (symbol_load_addr == LLDB_INVALID_ADDRESS)
700 {
701 if (log)
702 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
703 name);
704
705 m_parent.ReportSymbolLookupError(bare_name_cs);
706
707 return 0xbad0bad0;
708 }
709
710 if (log)
711 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
712 name,
713 symbol_load_addr);
714
715 if (symbol_load_addr == 0)
716 return 0xbad00add;
717
718 return symbol_load_addr;
719}
720
721void *
722IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name,
723 bool AbortOnFailure) {
724 assert (sizeof(void *) == 8);
725
726 return (void*)getSymbolAddress(Name);
727}
728
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000729lldb::addr_t
730IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
731{
Sean Callananffae9442013-06-27 01:42:47 +0000732 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
733
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000734 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000735 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000736 if (local_address >= record.m_host_address &&
737 local_address < record.m_host_address + record.m_size)
738 {
739 if (record.m_process_address == LLDB_INVALID_ADDRESS)
740 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000741
Sean Callananffae9442013-06-27 01:42:47 +0000742 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000743
Sean Callananffae9442013-06-27 01:42:47 +0000744 if (log)
745 {
746 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
747 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000748 (uint64_t)record.m_host_address,
749 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000750 ret,
751 record.m_process_address,
752 record.m_process_address + record.m_size);
753 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000754
Sean Callananffae9442013-06-27 01:42:47 +0000755 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000756 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000757 }
758
759 return LLDB_INVALID_ADDRESS;
760}
761
762IRExecutionUnit::AddrRange
763IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
764{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000765 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000766 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000767 if (local_address >= record.m_host_address &&
768 local_address < record.m_host_address + record.m_size)
769 {
770 if (record.m_process_address == LLDB_INVALID_ADDRESS)
771 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000773 return AddrRange(record.m_process_address, record.m_size);
774 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000775 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000776
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000777 return AddrRange (0, 0);
778}
779
780bool
781IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
782{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000783 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000784
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000785 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000786
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000787 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000788 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000789 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000790 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000791
Greg Clayton23f8c952014-03-24 23:10:19 +0000792 switch (record.m_sect_type)
793 {
794 case lldb::eSectionTypeInvalid:
795 case lldb::eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000796 case lldb::eSectionTypeDWARFDebugAddr:
Greg Clayton23f8c952014-03-24 23:10:19 +0000797 case lldb::eSectionTypeDWARFDebugAranges:
798 case lldb::eSectionTypeDWARFDebugFrame:
799 case lldb::eSectionTypeDWARFDebugInfo:
800 case lldb::eSectionTypeDWARFDebugLine:
801 case lldb::eSectionTypeDWARFDebugLoc:
802 case lldb::eSectionTypeDWARFDebugMacInfo:
803 case lldb::eSectionTypeDWARFDebugPubNames:
804 case lldb::eSectionTypeDWARFDebugPubTypes:
805 case lldb::eSectionTypeDWARFDebugRanges:
806 case lldb::eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000807 case lldb::eSectionTypeDWARFDebugStrOffsets:
Greg Clayton23f8c952014-03-24 23:10:19 +0000808 case lldb::eSectionTypeDWARFAppleNames:
809 case lldb::eSectionTypeDWARFAppleTypes:
810 case lldb::eSectionTypeDWARFAppleNamespaces:
811 case lldb::eSectionTypeDWARFAppleObjC:
812 err.Clear();
813 break;
814 default:
815 record.m_process_address = Malloc (record.m_size,
816 record.m_alignment,
817 record.m_permissions,
818 eAllocationPolicyProcessOnly,
819 err);
820 break;
821 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000822
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000823 if (!err.Success())
824 {
825 ret = false;
826 break;
827 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000828 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000829
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000830 if (!ret)
831 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000832 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000833 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000834 if (record.m_process_address != LLDB_INVALID_ADDRESS)
835 {
836 Free(record.m_process_address, err);
837 record.m_process_address = LLDB_INVALID_ADDRESS;
838 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000839 }
840 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000841
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000842 return ret;
843}
844
845void
846IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
847{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000848 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000849 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000850 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000851 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000853 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000854 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000855
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000856 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000857 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000858
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000859 // Trigger re-application of relocations.
860 engine.finalizeObject();
861}
862
863bool
864IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
865{
Greg Clayton23f8c952014-03-24 23:10:19 +0000866 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000867 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000868 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000869 if (record.m_process_address != LLDB_INVALID_ADDRESS)
870 {
871 lldb_private::Error err;
872 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
873 if (err.Success())
874 wrote_something = true;
875 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000876 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000877 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000878}
879
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000880void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000881IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000882{
883 if (!log)
884 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000885
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000886 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000887 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000888 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000889 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000890 (unsigned)m_alignment,
891 (unsigned)m_section_id);
892}
Greg Clayton23f8c952014-03-24 23:10:19 +0000893
894
895lldb::ByteOrder
896IRExecutionUnit::GetByteOrder () const
897{
898 ExecutionContext exe_ctx (GetBestExecutionContextScope());
899 return exe_ctx.GetByteOrder();
900}
901
902uint32_t
903IRExecutionUnit::GetAddressByteSize () const
904{
905 ExecutionContext exe_ctx (GetBestExecutionContextScope());
906 return exe_ctx.GetAddressByteSize();
907}
908
909void
910IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
911 lldb_private::Symtab &symtab)
912{
913 // No symbols yet...
914}
915
916
917void
918IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
919 lldb_private::SectionList &section_list)
920{
921 for (AllocationRecord &record : m_records)
922 {
923 if (record.m_size > 0)
924 {
925 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
926 obj_file,
927 record.m_section_id,
928 ConstString(record.m_name),
929 record.m_sect_type,
930 record.m_process_address,
931 record.m_size,
932 record.m_host_address, // file_offset (which is the host address for the data)
933 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000934 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000935 record.m_permissions)); // flags
936 section_list.AddSection (section_sp);
937 }
938 }
939}
940
941bool
942IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
943{
944 ExecutionContext exe_ctx (GetBestExecutionContextScope());
945 Target *target = exe_ctx.GetTargetPtr();
946 if (target)
947 arch = target->GetArchitecture();
948 else
949 arch.Clear();
950 return arch.IsValid();
951}
952
953lldb::ModuleSP
954IRExecutionUnit::GetJITModule ()
955{
956 ExecutionContext exe_ctx (GetBestExecutionContextScope());
957 Target *target = exe_ctx.GetTargetPtr();
958 if (target)
959 {
960 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
961 if (jit_module_sp)
962 {
963 bool changed = false;
964 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
965 }
966 return jit_module_sp;
967 }
968 return lldb::ModuleSP();
969}