blob: db6f19a490fd36a51034660ed653b5962d1b1b2f [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 Callananbd4dc692016-03-21 22:23:38 +000011#include "llvm/IR/Constants.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000012#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000013#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000014#include "llvm/Support/SourceMgr.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000015#include "llvm/Support/raw_ostream.h"
16
Sean Callanan8dfb68e2013-03-19 00:10:07 +000017#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/DataExtractor.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000019#include "lldb/Core/Debugger.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000020#include "lldb/Core/Disassembler.h"
21#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000022#include "lldb/Core/Module.h"
23#include "lldb/Core/Section.h"
Sean Callananb2814802016-02-12 21:11:25 +000024#include "lldb/Symbol/CompileUnit.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000025#include "lldb/Symbol/SymbolContext.h"
Sean Callananb2814802016-02-12 21:11:25 +000026#include "lldb/Symbol/SymbolVendor.h"
27#include "lldb/Symbol/SymbolFile.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028#include "lldb/Expression/IRExecutionUnit.h"
29#include "lldb/Target/ExecutionContext.h"
30#include "lldb/Target/Target.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000031#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000032#include "lldb/Utility/LLDBAssert.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000033
Sean Callananb2814802016-02-12 21:11:25 +000034#include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
35
Sean Callanan8dfb68e2013-03-19 00:10:07 +000036using namespace lldb_private;
37
Greg Clayton7b0992d2013-04-18 22:45:39 +000038IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
39 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000040 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000041 const lldb::TargetSP &target_sp,
Sean Callananb2814802016-02-12 21:11:25 +000042 const SymbolContext &sym_ctx,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000043 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000044 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000045 m_context_ap(context_ap.release()),
46 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000047 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000048 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000049 m_name(name),
Sean Callananb2814802016-02-12 21:11:25 +000050 m_sym_ctx(sym_ctx),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000051 m_did_jit(false),
52 m_function_load_addr(LLDB_INVALID_ADDRESS),
Sean Callananbd4dc692016-03-21 22:23:38 +000053 m_function_end_load_addr(LLDB_INVALID_ADDRESS),
54 m_reported_allocations(false)
Sean Callanan8dfb68e2013-03-19 00:10:07 +000055{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000056}
57
58lldb::addr_t
59IRExecutionUnit::WriteNow (const uint8_t *bytes,
60 size_t size,
61 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000062{
Jim Ingham2c381412015-11-04 20:32:27 +000063 const bool zero_memory = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +000064 lldb::addr_t allocation_process_addr = Malloc (size,
65 8,
66 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
67 eAllocationPolicyMirror,
Jim Ingham2c381412015-11-04 20:32:27 +000068 zero_memory,
Sean Callanan5a1af4e2013-04-05 02:22:57 +000069 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000070
Sean Callanan5a1af4e2013-04-05 02:22:57 +000071 if (!error.Success())
72 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000073
Sean Callanan5a1af4e2013-04-05 02:22:57 +000074 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000075
Sean Callanan5a1af4e2013-04-05 02:22:57 +000076 if (!error.Success())
77 {
78 Error err;
79 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanan5a1af4e2013-04-05 02:22:57 +000081 return LLDB_INVALID_ADDRESS;
82 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000083
Sean Callanan5a1af4e2013-04-05 02:22:57 +000084 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
85 {
86 DataBufferHeap my_buffer(size, 0);
87 Error err;
88 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089
Sean Callanan5a1af4e2013-04-05 02:22:57 +000090 if (err.Success())
91 {
92 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000093 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000094 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000095 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000096
Sean Callanan5a1af4e2013-04-05 02:22:57 +000097 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000098}
99
100void
101IRExecutionUnit::FreeNow (lldb::addr_t allocation)
102{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000103 if (allocation == LLDB_INVALID_ADDRESS)
104 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000105
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000106 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000107
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000108 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109}
110
111Error
112IRExecutionUnit::DisassembleFunction (Stream &stream,
113 lldb::ProcessSP &process_wp)
114{
Greg Clayton5160ce52013-03-27 23:08:40 +0000115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000116
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000117 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000118
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000119 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000120
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000121 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000122
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000123 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
124 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000125
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000126 for (JittedFunction &function : m_jitted_functions)
127 {
Sean Callananbd4dc692016-03-21 22:23:38 +0000128 if (function.m_name.AsCString() != m_name.AsCString())
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000129 {
130 func_local_addr = function.m_local_addr;
131 func_remote_addr = function.m_remote_addr;
132 }
133 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000134
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000135 if (func_local_addr == LLDB_INVALID_ADDRESS)
136 {
137 ret.SetErrorToGenericError();
138 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
139 return ret;
140 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000141
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000142 if (log)
143 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 +0000144
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000145 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000146
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000147 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000148
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000149 if (func_range.first == 0 && func_range.second == 0)
150 {
151 ret.SetErrorToGenericError();
152 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
153 return ret;
154 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000155
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000156 if (log)
157 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000158
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000159 Target *target = exe_ctx.GetTargetPtr();
160 if (!target)
161 {
162 ret.SetErrorToGenericError();
163 ret.SetErrorString("Couldn't find the target");
164 return ret;
165 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000166
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000167 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000168
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000169 Process *process = exe_ctx.GetProcessPtr();
170 Error err;
171 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000172
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000173 if (!err.Success())
174 {
175 ret.SetErrorToGenericError();
176 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
177 return ret;
178 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000179
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000180 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000181
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000182 const char *plugin_name = NULL;
183 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000184 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000185
Jim Ingham56d40422013-07-31 02:19:15 +0000186 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000187 {
188 ret.SetErrorToGenericError();
189 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
190 return ret;
191 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000192
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000193 if (!process)
194 {
195 ret.SetErrorToGenericError();
196 ret.SetErrorString("Couldn't find the process");
197 return ret;
198 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000199
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000200 DataExtractor extractor(buffer_sp,
201 process->GetByteOrder(),
202 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000203
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000204 if (log)
205 {
206 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000207 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000208 0,
209 extractor.GetByteSize(),
210 func_remote_addr,
211 16,
212 DataExtractor::TypeUInt8);
213 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000214
Jim Ingham56d40422013-07-31 02:19:15 +0000215 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000216
Jim Ingham56d40422013-07-31 02:19:15 +0000217 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Greg Clayton554f68d2015-02-04 22:00:53 +0000218 instruction_list.Dump(&stream, true, true, &exe_ctx);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000219
Jim Ingham56d40422013-07-31 02:19:15 +0000220 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
221 // I'll fix that but for now, just clear the list and it will go away nicely.
222 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000223 return ret;
224}
225
Sean Callanan44bc6572013-03-27 03:09:55 +0000226static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
227{
228 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229
Sean Callanan44bc6572013-03-27 03:09:55 +0000230 if (err && err->Success())
231 {
232 err->SetErrorToGenericError();
233 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
234 }
235}
236
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000237void
Jim Ingham27e5fe82015-01-27 18:03:05 +0000238IRExecutionUnit::ReportSymbolLookupError(const ConstString &name)
239{
240 m_failed_lookups.push_back(name);
241}
242
243void
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000244IRExecutionUnit::GetRunnableInfo(Error &error,
245 lldb::addr_t &func_addr,
246 lldb::addr_t &func_end)
247{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000248 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan7c435a52014-02-06 22:25:20 +0000250 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000251
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000252 func_addr = LLDB_INVALID_ADDRESS;
253 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000254
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000255 if (!process_sp)
256 {
257 error.SetErrorToGenericError();
258 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
259 return;
260 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000262 if (m_did_jit)
263 {
264 func_addr = m_function_load_addr;
265 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000267 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000268 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000269
Sean Callanan7c435a52014-02-06 22:25:20 +0000270 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000272 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000273
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000274 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000277
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000279 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000280 std::string s;
281 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000282
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000283 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000285 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000286
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000287 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
288 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000289
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000290 llvm::Triple triple(m_module->getTargetTriple());
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000291 llvm::Reloc::Model relocModel;
292 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000293
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000294 if (triple.isOSBinFormatELF())
295 {
296 relocModel = llvm::Reloc::Static;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000297 }
298 else
299 {
300 relocModel = llvm::Reloc::PIC_;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000301 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000302
Sean Callananb2814802016-02-12 21:11:25 +0000303 // This will be small for 32-bit and large for 64-bit.
304 codeModel = llvm::CodeModel::JITDefault;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000305
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000306 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Rafael Espindola7c03d952014-08-19 04:27:03 +0000308 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000309
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000310 builder.setEngineKind(llvm::EngineKind::JIT)
311 .setErrorStr(&error_string)
312 .setRelocationModel(relocModel)
David Majnemerb313e462014-12-04 21:26:25 +0000313 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
David Majnemer8faf9372014-09-16 06:34:29 +0000314 .setCodeModel(codeModel)
Reid Kleckner54209532014-09-03 00:40:36 +0000315 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000316
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000317 llvm::StringRef mArch;
318 llvm::StringRef mCPU;
319 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000321 for (std::string &feature : m_cpu_features)
322 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000323
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000324 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
325 mArch,
326 mCPU,
327 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000328
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000329 m_execution_engine_ap.reset(builder.create(target_machine));
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000330
Sean Callananf4527032016-02-12 23:55:13 +0000331 m_strip_underscore = (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_');
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000332
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000333 if (!m_execution_engine_ap.get())
334 {
335 error.SetErrorToGenericError();
336 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000337 return;
338 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000339
Greg Clayton23f8c952014-03-24 23:10:19 +0000340 // Make sure we see all sections, including ones that don't have relocations...
341 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000342
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000343 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callananbd4dc692016-03-21 22:23:38 +0000345 for (llvm::Function &function : *m_module)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000346 {
Sean Callananbd4dc692016-03-21 22:23:38 +0000347 if (function.isDeclaration())
348 continue;
349
350 const bool external = function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
351
352 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(&function);
353
354 if (!error.Success())
355 {
356 // We got an error through our callback!
357 return;
358 }
359
360 if (!fun_ptr)
361 {
362 error.SetErrorToGenericError();
363 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", function.getName().str().c_str());
364 return;
365 }
366 m_jitted_functions.push_back (JittedFunction(function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr));
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000367 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000369 CommitAllocations(process_sp);
370 ReportAllocations(*m_execution_engine_ap);
Sean Callananbd4dc692016-03-21 22:23:38 +0000371
372 // We have to do this after calling ReportAllocations because for the MCJIT, getGlobalValueAddress
373 // will cause the JIT to perform all relocations. That can only be done once, and has to happen
374 // after we do the remapping from local -> remote.
375 // That means we don't know the local address of the Variables, but we don't need that for anything,
376 // so that's okay.
377
378 std::function<void (llvm::GlobalValue &)> RegisterOneValue = [this] (llvm::GlobalValue &val) {
379 if (val.hasExternalLinkage() && !val.isDeclaration())
380 {
381 uint64_t var_ptr_addr = m_execution_engine_ap->getGlobalValueAddress(val.getName().str());
382
383 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
384
385 // This is a really unfortunae API that sometimes returns local addresses and sometimes returns remote addresses, based on whether
386 // the variable was relocated during ReportAllocations or not.
387
388 if (remote_addr == LLDB_INVALID_ADDRESS)
389 {
390 remote_addr = var_ptr_addr;
391 }
392
393 if (var_ptr_addr != 0)
394 m_jitted_global_variables.push_back (JittedGlobalVariable (val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
395 }
396 };
397
398 for (llvm::GlobalVariable &global_var : m_module->getGlobalList())
399 {
400 RegisterOneValue(global_var);
401 }
402
403 for (llvm::GlobalAlias &global_alias : m_module->getAliasList())
404 {
405 RegisterOneValue(global_alias);
406 }
407
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000408 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000409
Jim Ingham27e5fe82015-01-27 18:03:05 +0000410 if (m_failed_lookups.size())
411 {
412 StreamString ss;
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000413
Jim Ingham27e5fe82015-01-27 18:03:05 +0000414 ss.PutCString("Couldn't lookup symbols:\n");
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000415
Jim Ingham27e5fe82015-01-27 18:03:05 +0000416 bool emitNewLine = false;
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000417
Jim Ingham27e5fe82015-01-27 18:03:05 +0000418 for (const ConstString &failed_lookup : m_failed_lookups)
419 {
420 if (emitNewLine)
421 ss.PutCString("\n");
422 emitNewLine = true;
423 ss.PutCString(" ");
Greg Claytonddaf6a72015-07-08 22:32:23 +0000424 ss.PutCString(Mangled(failed_lookup).GetDemangledName(lldb::eLanguageTypeObjC_plus_plus).AsCString());
Jim Ingham27e5fe82015-01-27 18:03:05 +0000425 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000426
Jim Ingham27e5fe82015-01-27 18:03:05 +0000427 m_failed_lookups.clear();
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000428
Jim Ingham27e5fe82015-01-27 18:03:05 +0000429 error.SetErrorString(ss.GetData());
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000430
Jim Ingham27e5fe82015-01-27 18:03:05 +0000431 return;
432 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000433
Jim Ingham27e5fe82015-01-27 18:03:05 +0000434 m_function_load_addr = LLDB_INVALID_ADDRESS;
435 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
436
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000437 for (JittedFunction &jitted_function : m_jitted_functions)
438 {
439 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callananbd4dc692016-03-21 22:23:38 +0000441 if (!m_name.IsEmpty() && jitted_function.m_name == m_name)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000442 {
443 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
444 m_function_end_load_addr = func_range.first + func_range.second;
445 m_function_load_addr = jitted_function.m_remote_addr;
446 }
447 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000448
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000449 if (log)
450 {
451 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000452
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000453 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000454
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000455 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000456
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000457 if (!err.Success())
458 {
459 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
460 }
461 else
462 {
463 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
464 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000465
Sean Callanan4f2c1982014-01-21 00:54:48 +0000466 log->Printf("Sections: ");
467 for (AllocationRecord &record : m_records)
468 {
469 if (record.m_process_address != LLDB_INVALID_ADDRESS)
470 {
471 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000472
Sean Callanan4f2c1982014-01-21 00:54:48 +0000473 DataBufferHeap my_buffer(record.m_size, 0);
474 Error err;
475 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000476
Sean Callanan4f2c1982014-01-21 00:54:48 +0000477 if (err.Success())
478 {
479 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
480 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
481 }
482 }
Sean Callanan44dbf112015-10-23 00:39:09 +0000483 else
484 {
485 record.dump(log);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000486
Sean Callanan44dbf112015-10-23 00:39:09 +0000487 DataExtractor my_extractor ((const void*)record.m_host_address, record.m_size, lldb::eByteOrderBig, 8);
488 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16, DataExtractor::TypeUInt8);
489 }
Sean Callanan4f2c1982014-01-21 00:54:48 +0000490 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000491 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000492
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000493 func_addr = m_function_load_addr;
494 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000495
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000496 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000497}
498
499IRExecutionUnit::~IRExecutionUnit ()
500{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000501 m_module_ap.reset();
502 m_execution_engine_ap.reset();
503 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000504}
505
506IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000507 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000508 m_parent (parent)
509{
510}
511
Greg Clayton23f8c952014-03-24 23:10:19 +0000512IRExecutionUnit::MemoryManager::~MemoryManager ()
513{
514}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000515
Greg Clayton23f8c952014-03-24 23:10:19 +0000516lldb::SectionType
517IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
518{
519 lldb::SectionType sect_type = lldb::eSectionTypeCode;
520 switch (alloc_kind)
521 {
522 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
523 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
524 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
525 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
526 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
527 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000528
Greg Clayton23f8c952014-03-24 23:10:19 +0000529 if (!name.empty())
530 {
531 if (name.equals("__text") || name.equals(".text"))
532 sect_type = lldb::eSectionTypeCode;
533 else if (name.equals("__data") || name.equals(".data"))
534 sect_type = lldb::eSectionTypeCode;
535 else if (name.startswith("__debug_") || name.startswith(".debug_"))
536 {
537 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
538 llvm::StringRef dwarf_name(name.substr(name_idx));
539 switch (dwarf_name[0])
540 {
541 case 'a':
542 if (dwarf_name.equals("abbrev"))
543 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
544 else if (dwarf_name.equals("aranges"))
545 sect_type = lldb::eSectionTypeDWARFDebugAranges;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000546 else if (dwarf_name.equals("addr"))
547 sect_type = lldb::eSectionTypeDWARFDebugAddr;
Greg Clayton23f8c952014-03-24 23:10:19 +0000548 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000549
Greg Clayton23f8c952014-03-24 23:10:19 +0000550 case 'f':
551 if (dwarf_name.equals("frame"))
552 sect_type = lldb::eSectionTypeDWARFDebugFrame;
553 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000554
Greg Clayton23f8c952014-03-24 23:10:19 +0000555 case 'i':
556 if (dwarf_name.equals("info"))
557 sect_type = lldb::eSectionTypeDWARFDebugInfo;
558 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000559
Greg Clayton23f8c952014-03-24 23:10:19 +0000560 case 'l':
561 if (dwarf_name.equals("line"))
562 sect_type = lldb::eSectionTypeDWARFDebugLine;
563 else if (dwarf_name.equals("loc"))
564 sect_type = lldb::eSectionTypeDWARFDebugLoc;
565 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000566
Greg Clayton23f8c952014-03-24 23:10:19 +0000567 case 'm':
568 if (dwarf_name.equals("macinfo"))
569 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
570 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000571
Greg Clayton23f8c952014-03-24 23:10:19 +0000572 case 'p':
573 if (dwarf_name.equals("pubnames"))
574 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
575 else if (dwarf_name.equals("pubtypes"))
576 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
577 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000578
Greg Clayton23f8c952014-03-24 23:10:19 +0000579 case 's':
580 if (dwarf_name.equals("str"))
581 sect_type = lldb::eSectionTypeDWARFDebugStr;
Tamas Berghammerc178d4c2015-08-25 11:45:58 +0000582 else if (dwarf_name.equals("str_offsets"))
583 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
Greg Clayton23f8c952014-03-24 23:10:19 +0000584 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000585
Greg Clayton23f8c952014-03-24 23:10:19 +0000586 case 'r':
587 if (dwarf_name.equals("ranges"))
588 sect_type = lldb::eSectionTypeDWARFDebugRanges;
589 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000590
Greg Clayton23f8c952014-03-24 23:10:19 +0000591 default:
592 break;
593 }
594 }
595 else if (name.startswith("__apple_") || name.startswith(".apple_"))
596 {
597#if 0
598 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
599 llvm::StringRef apple_name(name.substr(name_idx));
600 switch (apple_name[0])
601 {
602 case 'n':
603 if (apple_name.equals("names"))
604 sect_type = lldb::eSectionTypeDWARFAppleNames;
605 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
606 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
607 break;
608 case 't':
609 if (apple_name.equals("types"))
610 sect_type = lldb::eSectionTypeDWARFAppleTypes;
611 break;
612 case 'o':
613 if (apple_name.equals("objc"))
614 sect_type = lldb::eSectionTypeDWARFAppleObjC;
615 break;
616 default:
617 break;
618 }
619#else
620 sect_type = lldb::eSectionTypeInvalid;
621#endif
622 }
623 else if (name.equals("__objc_imageinfo"))
624 sect_type = lldb::eSectionTypeOther;
625 }
626 return sect_type;
627}
628
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000629uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000630IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
631 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000632 unsigned SectionID,
633 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000634{
Greg Clayton5160ce52013-03-27 23:08:40 +0000635 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
Filip Pizlobfcff682013-10-02 01:43:46 +0000637 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000639 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000640 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000641 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000642 Size,
643 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000644 SectionID,
645 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000646
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000647 if (log)
648 {
649 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000650 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000651 }
Sean Callananbd4dc692016-03-21 22:23:38 +0000652
653 if (m_parent.m_reported_allocations)
654 {
655 Error err;
656 lldb::ProcessSP process_sp = m_parent.GetBestExecutionContextScope()->CalculateProcess();
657
658 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
659 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000660
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000661 return return_value;
662}
663
664uint8_t *
665IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
666 unsigned Alignment,
667 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000668 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000669 bool IsReadOnly)
670{
Greg Clayton5160ce52013-03-27 23:08:40 +0000671 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000672
Filip Pizlobfcff682013-10-02 01:43:46 +0000673 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000675 uint32_t permissions = lldb::ePermissionsReadable;
676 if (!IsReadOnly)
677 permissions |= lldb::ePermissionsWritable;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000678 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000679 permissions,
Greg Clayton23f8c952014-03-24 23:10:19 +0000680 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000681 Size,
682 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000683 SectionID,
684 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000685 if (log)
686 {
687 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
David Blaikie129b8392015-04-08 20:23:52 +0000688 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000689 }
Sean Callananbd4dc692016-03-21 22:23:38 +0000690
691 if (m_parent.m_reported_allocations)
692 {
693 Error err;
694 lldb::ProcessSP process_sp = m_parent.GetBestExecutionContextScope()->CalculateProcess();
695
696 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
697 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000698
699 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700}
701
Sean Callananb2814802016-02-12 21:11:25 +0000702static ConstString
703FindBestAlternateMangledName(const ConstString &demangled,
704 const lldb::LanguageType &lang_type,
705 const SymbolContext &sym_ctx)
706{
707 CPlusPlusLanguage::MethodName cpp_name(demangled);
708 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
709
710 if (!scope_qualified_name.size())
711 return ConstString();
712
713 if (!sym_ctx.module_sp)
714 return ConstString();
715
716 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
717 if (!sym_vendor)
718 return ConstString();
719
720 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
721 if (!sym_file)
722 return ConstString();
723
724 std::vector<ConstString> alternates;
725 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
726
727 std::vector<ConstString> param_and_qual_matches;
728 std::vector<ConstString> param_matches;
729 for (size_t i = 0; i < alternates.size(); i++)
730 {
731 ConstString alternate_mangled_name = alternates[i];
732 Mangled mangled(alternate_mangled_name, true);
733 ConstString demangled = mangled.GetDemangledName(lang_type);
734
735 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
736 if (!cpp_name.IsValid())
737 continue;
738
739 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments())
740 {
741 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
742 param_and_qual_matches.push_back(alternate_mangled_name);
743 else
744 param_matches.push_back(alternate_mangled_name);
745 }
746 }
747
748 if (param_and_qual_matches.size())
749 return param_and_qual_matches[0]; // It is assumed that there will be only one!
750 else if (param_matches.size())
751 return param_matches[0]; // Return one of them as a best match
752 else
753 return ConstString();
754}
755
Sean Callananf4527032016-02-12 23:55:13 +0000756struct IRExecutionUnit::SearchSpec
Sean Callananb2814802016-02-12 21:11:25 +0000757{
758 ConstString name;
759 uint32_t mask;
760
761 SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeAuto) :
762 name(n),
763 mask(m)
764 {
765 }
766};
767
Sean Callananf4527032016-02-12 23:55:13 +0000768void
769IRExecutionUnit::CollectCandidateCNames(std::vector<IRExecutionUnit::SearchSpec> &C_specs, const ConstString &name)
Sean Callananb2814802016-02-12 21:11:25 +0000770{
Sean Callananf4527032016-02-12 23:55:13 +0000771 if (m_strip_underscore && name.AsCString()[0] == '_')
772 C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
Sean Callananb2814802016-02-12 21:11:25 +0000773 C_specs.push_back(SearchSpec(name));
Sean Callananb2814802016-02-12 21:11:25 +0000774}
775
Sean Callananf4527032016-02-12 23:55:13 +0000776void
777IRExecutionUnit::CollectCandidateCPlusPlusNames(std::vector<IRExecutionUnit::SearchSpec> &CPP_specs, const std::vector<SearchSpec> &C_specs, const SymbolContext &sc)
Sean Callananb2814802016-02-12 21:11:25 +0000778{
779 for (const SearchSpec &C_spec : C_specs)
780 {
781 const ConstString &name = C_spec.name;
782
783 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString()))
784 {
785 Mangled mangled(name, true);
786 ConstString demangled = mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000787
Sean Callananb2814802016-02-12 21:11:25 +0000788 if (demangled)
789 {
790 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(demangled, lldb::eLanguageTypeC_plus_plus, sc);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000791
Sean Callananb2814802016-02-12 21:11:25 +0000792 if (best_alternate_mangled_name)
793 {
794 CPP_specs.push_back(best_alternate_mangled_name);
795 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000796
Sean Callananb2814802016-02-12 21:11:25 +0000797 CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
798 }
799 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000800
Sean Callananb2814802016-02-12 21:11:25 +0000801 // Maybe we're looking for a const symbol but the debug info told us it was const...
802 if (!strncmp(name.GetCString(), "_ZN", 3) &&
803 strncmp(name.GetCString(), "_ZNK", 4))
804 {
805 std::string fixed_scratch("_ZNK");
806 fixed_scratch.append(name.GetCString() + 3);
807 CPP_specs.push_back(ConstString(fixed_scratch.c_str()));
808 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000809
Sean Callananb2814802016-02-12 21:11:25 +0000810 // Maybe we're looking for a static symbol but we thought it was global...
811 if (!strncmp(name.GetCString(), "_Z", 2) &&
812 strncmp(name.GetCString(), "_ZL", 3))
813 {
814 std::string fixed_scratch("_ZL");
815 fixed_scratch.append(name.GetCString() + 2);
816 CPP_specs.push_back(ConstString(fixed_scratch.c_str()));
817 }
818
819 }
820}
821
Sean Callananf4527032016-02-12 23:55:13 +0000822lldb::addr_t
823IRExecutionUnit::FindInSymbols(const std::vector<IRExecutionUnit::SearchSpec> &specs, const lldb_private::SymbolContext &sc)
Sean Callananb2814802016-02-12 21:11:25 +0000824{
Sean Callananfed0e7582016-02-23 23:09:06 +0000825 Target *target = sc.target_sp.get();
826
827 if (!target)
828 {
829 // we shouldn't be doing any symbol lookup at all without a target
830 return LLDB_INVALID_ADDRESS;
831 }
832
Sean Callananb2814802016-02-12 21:11:25 +0000833 for (const SearchSpec &spec : specs)
834 {
835 SymbolContextList sc_list;
Sean Callananfed0e7582016-02-23 23:09:06 +0000836
837 lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
838
839 std::function<bool (lldb::addr_t &, SymbolContextList &, const lldb_private::SymbolContext &)> get_external_load_address =
840 [&best_internal_load_address, target](lldb::addr_t &load_address,
841 SymbolContextList &sc_list,
842 const lldb_private::SymbolContext &sc) -> lldb::addr_t
843 {
844 load_address = LLDB_INVALID_ADDRESS;
845
846 for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si)
847 {
848 SymbolContext candidate_sc;
849
850 sc_list.GetContextAtIndex(si, candidate_sc);
851
852 const bool is_external = (candidate_sc.function) ||
853 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
Ted Woodwardeee554d2016-03-09 22:05:17 +0000854 if (candidate_sc.symbol)
Pavel Labath16d3fd82016-03-04 11:26:56 +0000855 {
Ted Woodwardeee554d2016-03-09 22:05:17 +0000856 load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
857
858 if (load_address == LLDB_INVALID_ADDRESS)
859 {
860 if (target->GetProcessSP())
861 load_address = candidate_sc.symbol->GetAddress().GetLoadAddress(target);
862 else
863 load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
864 }
865 }
866
867 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function)
868 {
869 if (target->GetProcessSP())
870 load_address = candidate_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(target);
871 else
872 load_address = candidate_sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
Pavel Labath16d3fd82016-03-04 11:26:56 +0000873 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000874
875 if (load_address != LLDB_INVALID_ADDRESS)
Sean Callananfed0e7582016-02-23 23:09:06 +0000876 {
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000877 if (is_external)
Sean Callananfed0e7582016-02-23 23:09:06 +0000878 {
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000879 return true;
880 }
881 else if (best_internal_load_address == LLDB_INVALID_ADDRESS)
882 {
883 best_internal_load_address = load_address;
884 load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000885 }
886 }
887 }
888
889 return false;
890 };
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000891
Sean Callananb2814802016-02-12 21:11:25 +0000892 if (sc.module_sp)
893 {
894 sc.module_sp->FindFunctions(spec.name,
895 NULL,
896 spec.mask,
897 true, // include_symbols
898 false, // include_inlines
899 true, // append
900 sc_list);
901 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000902
903 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
904
905 if (get_external_load_address(load_address, sc_list, sc))
906 {
907 return load_address;
908 }
909 else
910 {
911 sc_list.Clear();
912 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000913
Sean Callananb2814802016-02-12 21:11:25 +0000914 if (sc_list.GetSize() == 0 && sc.target_sp)
915 {
916 sc.target_sp->GetImages().FindFunctions(spec.name,
917 spec.mask,
918 true, // include_symbols
919 false, // include_inlines
920 true, // append
921 sc_list);
922 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000923
Sean Callananfed0e7582016-02-23 23:09:06 +0000924 if (get_external_load_address(load_address, sc_list, sc))
925 {
926 return load_address;
927 }
928 else
929 {
930 sc_list.Clear();
931 }
932
Sean Callananb2814802016-02-12 21:11:25 +0000933 if (sc_list.GetSize() == 0 && sc.target_sp)
934 {
935 sc.target_sp->GetImages().FindSymbolsWithNameAndType(spec.name, lldb::eSymbolTypeAny, sc_list);
936 }
937
Sean Callananfed0e7582016-02-23 23:09:06 +0000938 if (get_external_load_address(load_address, sc_list, sc))
Sean Callananb2814802016-02-12 21:11:25 +0000939 {
Sean Callananfed0e7582016-02-23 23:09:06 +0000940 return load_address;
Sean Callananb2814802016-02-12 21:11:25 +0000941 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000942 // if there are any searches we try after this, add an sc_list.Clear() in an "else" clause here
943
Sean Callananb2814802016-02-12 21:11:25 +0000944 if (best_internal_load_address != LLDB_INVALID_ADDRESS)
945 {
946 return best_internal_load_address;
947 }
948 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000949
Sean Callananb2814802016-02-12 21:11:25 +0000950 return LLDB_INVALID_ADDRESS;
951}
952
Sean Callananf4527032016-02-12 23:55:13 +0000953lldb::addr_t
954IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs, const lldb_private::SymbolContext &sc)
Sean Callananb2814802016-02-12 21:11:25 +0000955{
956 lldb::TargetSP target_sp = sc.target_sp;
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000957
Sean Callananb2814802016-02-12 21:11:25 +0000958 if (!target_sp)
959 {
960 return LLDB_INVALID_ADDRESS;
961 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000962
Sean Callananb2814802016-02-12 21:11:25 +0000963 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000964
Sean Callananb2814802016-02-12 21:11:25 +0000965 if (!process_sp)
966 {
967 return LLDB_INVALID_ADDRESS;
968 }
969
970 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
971
972 if (runtime)
973 {
974 for (const SearchSpec &spec : specs)
975 {
976 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000977
Sean Callananb2814802016-02-12 21:11:25 +0000978 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
979 return symbol_load_addr;
980 }
981 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000982
Sean Callananb2814802016-02-12 21:11:25 +0000983 return LLDB_INVALID_ADDRESS;
984}
985
986lldb::addr_t
Sean Callananbd4dc692016-03-21 22:23:38 +0000987IRExecutionUnit::FindInUserDefinedSymbols(const std::vector<SearchSpec> &specs, const lldb_private::SymbolContext &sc)
988{
989 lldb::TargetSP target_sp = sc.target_sp;
990
991 for (const SearchSpec &spec : specs)
992 {
993 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
994
995 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
996 return symbol_load_addr;
997 }
998
999 return LLDB_INVALID_ADDRESS;
1000}
1001
1002lldb::addr_t
Sean Callananf4527032016-02-12 23:55:13 +00001003IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name)
Sean Callananb2814802016-02-12 21:11:25 +00001004{
1005 std::vector<SearchSpec> candidate_C_names;
1006 std::vector<SearchSpec> candidate_CPlusPlus_names;
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001007
Sean Callananb2814802016-02-12 21:11:25 +00001008 CollectCandidateCNames(candidate_C_names, name);
1009
Sean Callananf4527032016-02-12 23:55:13 +00001010 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
Sean Callananb2814802016-02-12 21:11:25 +00001011 if (ret == LLDB_INVALID_ADDRESS)
Sean Callananf4527032016-02-12 23:55:13 +00001012 ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001013
Sean Callananb2814802016-02-12 21:11:25 +00001014 if (ret == LLDB_INVALID_ADDRESS)
Sean Callananbd4dc692016-03-21 22:23:38 +00001015 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
1016
1017 if (ret == LLDB_INVALID_ADDRESS)
Sean Callananb2814802016-02-12 21:11:25 +00001018 {
Sean Callananf4527032016-02-12 23:55:13 +00001019 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names, m_sym_ctx);
1020 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
Sean Callananb2814802016-02-12 21:11:25 +00001021 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001022
Sean Callananb2814802016-02-12 21:11:25 +00001023 return ret;
1024}
1025
Sean Callananbd4dc692016-03-21 22:23:38 +00001026void
1027IRExecutionUnit::GetStaticInitializers(std::vector <lldb::addr_t> &static_initializers)
1028{
1029 if (llvm::GlobalVariable *global_ctors = m_module->getNamedGlobal("llvm.global_ctors"))
1030 {
1031 if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(global_ctors->getInitializer()))
1032 {
1033 for (llvm::Use &ctor_use : ctor_array->operands())
1034 {
1035 if (llvm::ConstantStruct *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(ctor_use))
1036 {
1037 lldbassert(ctor_struct->getNumOperands() == 3); // this is standardized
1038 if (llvm::Function *ctor_function = llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1)))
1039 {
1040 ctor_function->dump();
1041
1042 ConstString ctor_function_name_cs(ctor_function->getName().str());
1043
1044 for (JittedFunction &jitted_function : m_jitted_functions)
1045 {
1046 if (ctor_function_name_cs == jitted_function.m_name)
1047 {
1048 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS)
1049 {
1050 static_initializers.push_back(jitted_function.m_remote_addr);
1051 }
1052 break;
1053 }
1054 }
1055 }
1056 }
1057 }
1058 }
1059 }
1060}
1061
Jim Ingham27e5fe82015-01-27 18:03:05 +00001062uint64_t
1063IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name)
1064{
1065 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Claytond6171a72015-07-02 16:43:49 +00001066
Sean Callananb2814802016-02-12 21:11:25 +00001067 ConstString name_cs(Name.c_str());
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001068
Sean Callananb2814802016-02-12 21:11:25 +00001069 lldb::addr_t ret = m_parent.FindSymbol(name_cs);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001070
Sean Callananb2814802016-02-12 21:11:25 +00001071 if (ret == LLDB_INVALID_ADDRESS)
Jim Ingham27e5fe82015-01-27 18:03:05 +00001072 {
1073 if (log)
1074 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
Sean Callananb2814802016-02-12 21:11:25 +00001075 Name.c_str());
1076
1077 m_parent.ReportSymbolLookupError(name_cs);
Jim Ingham27e5fe82015-01-27 18:03:05 +00001078 return 0xbad0bad0;
1079 }
Sean Callananb2814802016-02-12 21:11:25 +00001080 else
1081 {
1082 if (log)
1083 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1084 Name.c_str(),
1085 ret);
1086 return ret;
1087 }
Jim Ingham27e5fe82015-01-27 18:03:05 +00001088}
1089
1090void *
1091IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name,
1092 bool AbortOnFailure) {
1093 assert (sizeof(void *) == 8);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001094
Jim Ingham27e5fe82015-01-27 18:03:05 +00001095 return (void*)getSymbolAddress(Name);
1096}
1097
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001098lldb::addr_t
1099IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
1100{
Sean Callananffae9442013-06-27 01:42:47 +00001101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1102
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001103 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001104 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001105 if (local_address >= record.m_host_address &&
1106 local_address < record.m_host_address + record.m_size)
1107 {
1108 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1109 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001110
Sean Callananffae9442013-06-27 01:42:47 +00001111 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001112
Sean Callananffae9442013-06-27 01:42:47 +00001113 if (log)
1114 {
1115 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1116 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +00001117 (uint64_t)record.m_host_address,
1118 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +00001119 ret,
1120 record.m_process_address,
1121 record.m_process_address + record.m_size);
1122 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001123
Sean Callananffae9442013-06-27 01:42:47 +00001124 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001125 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001126 }
1127
1128 return LLDB_INVALID_ADDRESS;
1129}
1130
1131IRExecutionUnit::AddrRange
1132IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
1133{
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001134 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001135 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001136 if (local_address >= record.m_host_address &&
1137 local_address < record.m_host_address + record.m_size)
1138 {
1139 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1140 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001141
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001142 return AddrRange(record.m_process_address, record.m_size);
1143 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001144 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001145
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001146 return AddrRange (0, 0);
1147}
1148
1149bool
Sean Callananbd4dc692016-03-21 22:23:38 +00001150IRExecutionUnit::CommitOneAllocation (lldb::ProcessSP &process_sp,
1151 Error &error,
1152 AllocationRecord &record)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001153{
Sean Callananbd4dc692016-03-21 22:23:38 +00001154 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001155 {
Sean Callananbd4dc692016-03-21 22:23:38 +00001156 return true;
1157 }
1158
1159 switch (record.m_sect_type)
1160 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001161 case lldb::eSectionTypeInvalid:
1162 case lldb::eSectionTypeDWARFDebugAbbrev:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001163 case lldb::eSectionTypeDWARFDebugAddr:
Greg Clayton23f8c952014-03-24 23:10:19 +00001164 case lldb::eSectionTypeDWARFDebugAranges:
1165 case lldb::eSectionTypeDWARFDebugFrame:
1166 case lldb::eSectionTypeDWARFDebugInfo:
1167 case lldb::eSectionTypeDWARFDebugLine:
1168 case lldb::eSectionTypeDWARFDebugLoc:
1169 case lldb::eSectionTypeDWARFDebugMacInfo:
1170 case lldb::eSectionTypeDWARFDebugPubNames:
1171 case lldb::eSectionTypeDWARFDebugPubTypes:
1172 case lldb::eSectionTypeDWARFDebugRanges:
1173 case lldb::eSectionTypeDWARFDebugStr:
Tamas Berghammerc178d4c2015-08-25 11:45:58 +00001174 case lldb::eSectionTypeDWARFDebugStrOffsets:
Greg Clayton23f8c952014-03-24 23:10:19 +00001175 case lldb::eSectionTypeDWARFAppleNames:
1176 case lldb::eSectionTypeDWARFAppleTypes:
1177 case lldb::eSectionTypeDWARFAppleNamespaces:
1178 case lldb::eSectionTypeDWARFAppleObjC:
Sean Callananbd4dc692016-03-21 22:23:38 +00001179 error.Clear();
Greg Clayton23f8c952014-03-24 23:10:19 +00001180 break;
1181 default:
Jim Ingham2c381412015-11-04 20:32:27 +00001182 const bool zero_memory = false;
Greg Clayton23f8c952014-03-24 23:10:19 +00001183 record.m_process_address = Malloc (record.m_size,
1184 record.m_alignment,
1185 record.m_permissions,
1186 eAllocationPolicyProcessOnly,
Jim Ingham2c381412015-11-04 20:32:27 +00001187 zero_memory,
Sean Callananbd4dc692016-03-21 22:23:38 +00001188 error);
Greg Clayton23f8c952014-03-24 23:10:19 +00001189 break;
Sean Callananbd4dc692016-03-21 22:23:38 +00001190 }
1191
1192 return error.Success();
1193}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001194
Sean Callananbd4dc692016-03-21 22:23:38 +00001195bool
1196IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
1197{
1198 bool ret = true;
1199
1200 lldb_private::Error err;
1201
1202 for (AllocationRecord &record : m_records)
1203 {
1204 ret = CommitOneAllocation(process_sp, err, record);
1205
1206 if (!ret)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001207 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001208 break;
1209 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001210 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001211
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001212 if (!ret)
1213 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001214 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001215 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001216 if (record.m_process_address != LLDB_INVALID_ADDRESS)
1217 {
1218 Free(record.m_process_address, err);
1219 record.m_process_address = LLDB_INVALID_ADDRESS;
1220 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001221 }
1222 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001223
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001224 return ret;
1225}
1226
1227void
1228IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
1229{
Sean Callananbd4dc692016-03-21 22:23:38 +00001230 m_reported_allocations = true;
1231
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001232 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001233 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001234 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001235 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001236
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001237 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001238 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001239
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001240 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001241 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001242
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001243 // Trigger re-application of relocations.
1244 engine.finalizeObject();
1245}
1246
1247bool
1248IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
1249{
Greg Clayton23f8c952014-03-24 23:10:19 +00001250 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001251 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001252 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001253 if (record.m_process_address != LLDB_INVALID_ADDRESS)
1254 {
1255 lldb_private::Error err;
1256 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
1257 if (err.Success())
1258 wrote_something = true;
1259 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001260 }
Greg Clayton23f8c952014-03-24 23:10:19 +00001261 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001262}
1263
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001264void
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001265IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001266{
1267 if (!log)
1268 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001269
Sean Callanan44dbf112015-10-23 00:39:09 +00001270 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001271 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001272 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001273 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001274 (unsigned)m_alignment,
Sean Callanan44dbf112015-10-23 00:39:09 +00001275 (unsigned)m_section_id,
1276 m_name.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001277}
Greg Clayton23f8c952014-03-24 23:10:19 +00001278
1279
1280lldb::ByteOrder
1281IRExecutionUnit::GetByteOrder () const
1282{
1283 ExecutionContext exe_ctx (GetBestExecutionContextScope());
1284 return exe_ctx.GetByteOrder();
1285}
1286
1287uint32_t
1288IRExecutionUnit::GetAddressByteSize () const
1289{
1290 ExecutionContext exe_ctx (GetBestExecutionContextScope());
1291 return exe_ctx.GetAddressByteSize();
1292}
1293
1294void
1295IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
1296 lldb_private::Symtab &symtab)
1297{
1298 // No symbols yet...
1299}
1300
1301
1302void
1303IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
1304 lldb_private::SectionList &section_list)
1305{
1306 for (AllocationRecord &record : m_records)
1307 {
1308 if (record.m_size > 0)
1309 {
1310 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
1311 obj_file,
1312 record.m_section_id,
1313 ConstString(record.m_name),
1314 record.m_sect_type,
1315 record.m_process_address,
1316 record.m_size,
1317 record.m_host_address, // file_offset (which is the host address for the data)
1318 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +00001319 0,
Greg Clayton23f8c952014-03-24 23:10:19 +00001320 record.m_permissions)); // flags
1321 section_list.AddSection (section_sp);
1322 }
1323 }
1324}
1325
1326bool
1327IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
1328{
1329 ExecutionContext exe_ctx (GetBestExecutionContextScope());
1330 Target *target = exe_ctx.GetTargetPtr();
1331 if (target)
1332 arch = target->GetArchitecture();
1333 else
1334 arch.Clear();
1335 return arch.IsValid();
1336}
1337
1338lldb::ModuleSP
1339IRExecutionUnit::GetJITModule ()
1340{
1341 ExecutionContext exe_ctx (GetBestExecutionContextScope());
1342 Target *target = exe_ctx.GetTargetPtr();
1343 if (target)
1344 {
1345 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
1346 if (jit_module_sp)
1347 {
1348 bool changed = false;
1349 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1350 }
1351 return jit_module_sp;
1352 }
1353 return lldb::ModuleSP();
1354}