blob: e34a4c7fac52fcc8eacbf2895f751dae6b988f41 [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 Callanan5deb06e2016-09-26 20:18:51 +000011#include "llvm/ExecutionEngine/ObjectCache.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000012#include "llvm/IR/Constants.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000013#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000014#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000015#include "llvm/Support/SourceMgr.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000016#include "llvm/Support/raw_ostream.h"
17
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"
Greg Clayton23f8c952014-03-24 23:10:19 +000020#include "lldb/Core/Module.h"
21#include "lldb/Core/Section.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#include "lldb/Expression/IRExecutionUnit.h"
Sean Callananb2814802016-02-12 21:11:25 +000023#include "lldb/Symbol/CompileUnit.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000024#include "lldb/Symbol/SymbolContext.h"
Sean Callananb2814802016-02-12 21:11:25 +000025#include "lldb/Symbol/SymbolFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000027#include "lldb/Target/ExecutionContext.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000028#include "lldb/Target/ObjCLanguageRuntime.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000029#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000030#include "lldb/Utility/DataBufferHeap.h"
31#include "lldb/Utility/DataExtractor.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000032#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000033#include "lldb/Utility/Log.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034
Sean Callananb2814802016-02-12 21:11:25 +000035#include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner8e8d4b92018-05-23 23:56:09 +000036#include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h"
Sean Callananb2814802016-02-12 21:11:25 +000037
Sean Callanan8dfb68e2013-03-19 00:10:07 +000038using namespace lldb_private;
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_ap,
41 std::unique_ptr<llvm::Module> &module_ap,
42 ConstString &name,
43 const lldb::TargetSP &target_sp,
44 const SymbolContext &sym_ctx,
45 std::vector<std::string> &cpu_features)
46 : IRMemoryMap(target_sp), m_context_ap(context_ap.release()),
47 m_module_ap(module_ap.release()), m_module(m_module_ap.get()),
48 m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
49 m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
50 m_function_end_load_addr(LLDB_INVALID_ADDRESS),
51 m_reported_allocations(false) {}
Sean Callanan8dfb68e2013-03-19 00:10:07 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000054 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 const bool zero_memory = false;
56 lldb::addr_t allocation_process_addr =
57 Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable,
58 eAllocationPolicyMirror, zero_memory, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if (!error.Success())
61 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 if (!error.Success()) {
Zachary Turner97206d52017-05-12 04:51:55 +000066 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 Free(allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 return LLDB_INVALID_ADDRESS;
70 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 if (Log *log =
73 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
74 DataBufferHeap my_buffer(size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +000075 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 if (err.Success()) {
79 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(),
80 lldb::eByteOrderBig, 8);
81 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
82 allocation_process_addr, 16,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000083 DataExtractor::TypeUInt8);
84 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 return allocation_process_addr;
88}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void IRExecutionUnit::FreeNow(lldb::addr_t allocation) {
91 if (allocation == LLDB_INVALID_ADDRESS)
92 return;
93
Zachary Turner97206d52017-05-12 04:51:55 +000094 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000095
96 Free(allocation, err);
97}
98
Zachary Turner97206d52017-05-12 04:51:55 +000099Status IRExecutionUnit::DisassembleFunction(Stream &stream,
100 lldb::ProcessSP &process_wp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
102
103 ExecutionContext exe_ctx(process_wp);
104
Zachary Turner97206d52017-05-12 04:51:55 +0000105 Status ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106
107 ret.Clear();
108
109 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
110 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
111
112 for (JittedFunction &function : m_jitted_functions) {
113 if (function.m_name == m_name) {
114 func_local_addr = function.m_local_addr;
115 func_remote_addr = function.m_remote_addr;
116 }
117 }
118
119 if (func_local_addr == LLDB_INVALID_ADDRESS) {
120 ret.SetErrorToGenericError();
121 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly",
122 m_name.AsCString());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000123 return ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 }
125
126 if (log)
127 log->Printf("Found function, has local address 0x%" PRIx64
128 " and remote address 0x%" PRIx64,
129 (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
130
131 std::pair<lldb::addr_t, lldb::addr_t> func_range;
132
133 func_range = GetRemoteRangeForLocal(func_local_addr);
134
135 if (func_range.first == 0 && func_range.second == 0) {
136 ret.SetErrorToGenericError();
137 ret.SetErrorStringWithFormat("Couldn't find code range for function %s",
138 m_name.AsCString());
139 return ret;
140 }
141
142 if (log)
143 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
144 func_range.first, func_range.second);
145
146 Target *target = exe_ctx.GetTargetPtr();
147 if (!target) {
148 ret.SetErrorToGenericError();
149 ret.SetErrorString("Couldn't find the target");
150 return ret;
151 }
152
153 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
154
155 Process *process = exe_ctx.GetProcessPtr();
Zachary Turner97206d52017-05-12 04:51:55 +0000156 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
158 buffer_sp->GetByteSize(), err);
159
160 if (!err.Success()) {
161 ret.SetErrorToGenericError();
162 ret.SetErrorStringWithFormat("Couldn't read from process: %s",
163 err.AsCString("unknown error"));
164 return ret;
165 }
166
167 ArchSpec arch(target->GetArchitecture());
168
169 const char *plugin_name = NULL;
170 const char *flavor_string = NULL;
171 lldb::DisassemblerSP disassembler_sp =
172 Disassembler::FindPlugin(arch, flavor_string, plugin_name);
173
174 if (!disassembler_sp) {
175 ret.SetErrorToGenericError();
176 ret.SetErrorStringWithFormat(
177 "Unable to find disassembler plug-in for %s architecture.",
178 arch.GetArchitectureName());
179 return ret;
180 }
181
182 if (!process) {
183 ret.SetErrorToGenericError();
184 ret.SetErrorString("Couldn't find the process");
185 return ret;
186 }
187
188 DataExtractor extractor(buffer_sp, process->GetByteOrder(),
189 target->GetArchitecture().GetAddressByteSize());
190
191 if (log) {
192 log->Printf("Function data has contents:");
193 extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
194 DataExtractor::TypeUInt8);
195 }
196
197 disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
198 UINT32_MAX, false, false);
199
200 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
201 instruction_list.Dump(&stream, true, true, &exe_ctx);
202 return ret;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic,
206 void *Context, unsigned LocCookie) {
Zachary Turner97206d52017-05-12 04:51:55 +0000207 Status *err = static_cast<Status *>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 if (err && err->Success()) {
210 err->SetErrorToGenericError();
211 err->SetErrorStringWithFormat("Inline assembly error: %s",
212 diagnostic.getMessage().str().c_str());
213 }
Sean Callanan44bc6572013-03-27 03:09:55 +0000214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) {
217 m_failed_lookups.push_back(name);
Jim Ingham27e5fe82015-01-27 18:03:05 +0000218}
219
Zachary Turner97206d52017-05-12 04:51:55 +0000220void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 lldb::addr_t &func_end) {
222 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 static std::recursive_mutex s_runnable_info_mutex;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 func_addr = LLDB_INVALID_ADDRESS;
227 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 if (!process_sp) {
230 error.SetErrorToGenericError();
231 error.SetErrorString("Couldn't write the JIT compiled code into the "
232 "process because the process is invalid");
233 return;
234 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (m_did_jit) {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000237 func_addr = m_function_load_addr;
238 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000239
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000240 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 };
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 m_did_jit = true;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 std::string error_string;
250
251 if (log) {
252 std::string s;
253 llvm::raw_string_ostream oss(s);
254
255 m_module->print(oss, NULL);
256
257 oss.flush();
258
259 log->Printf("Module being sent to JIT: \n%s", s.c_str());
260 }
261
262 llvm::Triple triple(m_module->getTargetTriple());
263 llvm::Reloc::Model relocModel;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264
265 if (triple.isOSBinFormatELF()) {
266 relocModel = llvm::Reloc::Static;
267 } else {
268 relocModel = llvm::Reloc::PIC_;
269 }
270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError,
272 &error);
273
274 llvm::EngineBuilder builder(std::move(m_module_ap));
275
276 builder.setEngineKind(llvm::EngineKind::JIT)
277 .setErrorStr(&error_string)
278 .setRelocationModel(relocModel)
279 .setMCJITMemoryManager(
280 std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
Pavel Labathd7396362017-11-13 14:03:17 +0000281 .setOptLevel(llvm::CodeGenOpt::Less);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282
283 llvm::StringRef mArch;
284 llvm::StringRef mCPU;
285 llvm::SmallVector<std::string, 0> mAttrs;
286
287 for (std::string &feature : m_cpu_features)
288 mAttrs.push_back(feature);
289
290 llvm::TargetMachine *target_machine =
291 builder.selectTarget(triple, mArch, mCPU, mAttrs);
292
293 m_execution_engine_ap.reset(builder.create(target_machine));
294
295 m_strip_underscore =
296 (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_');
297
298 if (!m_execution_engine_ap.get()) {
299 error.SetErrorToGenericError();
300 error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
301 error_string.c_str());
302 return;
303 }
304
Sean Callanan5deb06e2016-09-26 20:18:51 +0000305 class ObjectDumper : public llvm::ObjectCache {
306 public:
307 void notifyObjectCompiled(const llvm::Module *module,
308 llvm::MemoryBufferRef object) override {
309 int fd = 0;
310 llvm::SmallVector<char, 256> result_path;
311 std::string object_name_model =
312 "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
313 (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
314 llvm::raw_fd_ostream fds(fd, true);
315 fds.write(object.getBufferStart(), object.getBufferSize());
316 }
317
318 std::unique_ptr<llvm::MemoryBuffer>
319 getObject(const llvm::Module *module) override {
320 // Return nothing - we're just abusing the object-cache mechanism to dump
321 // objects.
322 return nullptr;
323 }
324 };
325
326 if (process_sp->GetTarget().GetEnableSaveObjects()) {
327 m_object_cache_ap = llvm::make_unique<ObjectDumper>();
328 m_execution_engine_ap->setObjectCache(m_object_cache_ap.get());
329 }
330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 // Make sure we see all sections, including ones that don't have
332 // relocations...
333 m_execution_engine_ap->setProcessAllSections(true);
334
335 m_execution_engine_ap->DisableLazyCompilation();
336
337 for (llvm::Function &function : *m_module) {
338 if (function.isDeclaration() || function.hasPrivateLinkage())
339 continue;
340
341 const bool external =
342 function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
343
344 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(&function);
345
346 if (!error.Success()) {
347 // We got an error through our callback!
348 return;
Greg Clayton23f8c952014-03-24 23:10:19 +0000349 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (!fun_ptr) {
352 error.SetErrorToGenericError();
353 error.SetErrorStringWithFormat(
354 "'%s' was in the JITted module but wasn't lowered",
355 function.getName().str().c_str());
356 return;
357 }
358 m_jitted_functions.push_back(JittedFunction(
359 function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr));
360 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 CommitAllocations(process_sp);
363 ReportAllocations(*m_execution_engine_ap);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 // We have to do this after calling ReportAllocations because for the MCJIT,
Adrian Prantl05097242018-04-30 16:49:04 +0000366 // getGlobalValueAddress will cause the JIT to perform all relocations. That
367 // can only be done once, and has to happen after we do the remapping from
368 // local -> remote. That means we don't know the local address of the
369 // Variables, but we don't need that for anything, so that's okay.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000370
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
372 llvm::GlobalValue &val) {
373 if (val.hasExternalLinkage() && !val.isDeclaration()) {
374 uint64_t var_ptr_addr =
375 m_execution_engine_ap->getGlobalValueAddress(val.getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 // This is a really unfortunae API that sometimes returns local addresses
Adrian Prantl05097242018-04-30 16:49:04 +0000380 // and sometimes returns remote addresses, based on whether the variable
381 // was relocated during ReportAllocations or not.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 if (remote_addr == LLDB_INVALID_ADDRESS) {
384 remote_addr = var_ptr_addr;
385 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 if (var_ptr_addr != 0)
388 m_jitted_global_variables.push_back(JittedGlobalVariable(
389 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
390 }
391 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) {
394 RegisterOneValue(global_var);
395 }
396
397 for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
398 RegisterOneValue(global_alias);
399 }
400
401 WriteData(process_sp);
402
403 if (m_failed_lookups.size()) {
404 StreamString ss;
405
406 ss.PutCString("Couldn't lookup symbols:\n");
407
408 bool emitNewLine = false;
409
410 for (const ConstString &failed_lookup : m_failed_lookups) {
411 if (emitNewLine)
412 ss.PutCString("\n");
413 emitNewLine = true;
414 ss.PutCString(" ");
415 ss.PutCString(Mangled(failed_lookup)
416 .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus)
417 .AsCString());
418 }
419
420 m_failed_lookups.clear();
421
Zachary Turnerc1564272016-11-16 21:15:24 +0000422 error.SetErrorString(ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423
424 return;
425 }
426
427 m_function_load_addr = LLDB_INVALID_ADDRESS;
428 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
429
430 for (JittedFunction &jitted_function : m_jitted_functions) {
431 jitted_function.m_remote_addr =
432 GetRemoteAddressForLocal(jitted_function.m_local_addr);
433
434 if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
435 AddrRange func_range =
436 GetRemoteRangeForLocal(jitted_function.m_local_addr);
437 m_function_end_load_addr = func_range.first + func_range.second;
438 m_function_load_addr = jitted_function.m_remote_addr;
439 }
440 }
441
442 if (log) {
443 log->Printf("Code can be run in the target.");
444
445 StreamString disassembly_stream;
446
Zachary Turner97206d52017-05-12 04:51:55 +0000447 Status err = DisassembleFunction(disassembly_stream, process_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448
449 if (!err.Success()) {
450 log->Printf("Couldn't disassemble function : %s",
451 err.AsCString("unknown error"));
452 } else {
453 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
454 }
455
456 log->Printf("Sections: ");
457 for (AllocationRecord &record : m_records) {
458 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
459 record.dump(log);
460
461 DataBufferHeap my_buffer(record.m_size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +0000462 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 ReadMemory(my_buffer.GetBytes(), record.m_process_address,
464 record.m_size, err);
465
466 if (err.Success()) {
467 DataExtractor my_extractor(my_buffer.GetBytes(),
468 my_buffer.GetByteSize(),
469 lldb::eByteOrderBig, 8);
470 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
471 record.m_process_address, 16,
472 DataExtractor::TypeUInt8);
Greg Clayton23f8c952014-03-24 23:10:19 +0000473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 } else {
475 record.dump(log);
476
477 DataExtractor my_extractor((const void *)record.m_host_address,
478 record.m_size, lldb::eByteOrderBig, 8);
479 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
480 DataExtractor::TypeUInt8);
481 }
482 }
483 }
484
485 func_addr = m_function_load_addr;
486 func_end = m_function_end_load_addr;
487
488 return;
489}
490
491IRExecutionUnit::~IRExecutionUnit() {
492 m_module_ap.reset();
493 m_execution_engine_ap.reset();
494 m_context_ap.reset();
495}
496
497IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
498 : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {}
499
500IRExecutionUnit::MemoryManager::~MemoryManager() {}
501
502lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
503 const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
504 lldb::SectionType sect_type = lldb::eSectionTypeCode;
505 switch (alloc_kind) {
506 case AllocationKind::Stub:
507 sect_type = lldb::eSectionTypeCode;
508 break;
509 case AllocationKind::Code:
510 sect_type = lldb::eSectionTypeCode;
511 break;
512 case AllocationKind::Data:
513 sect_type = lldb::eSectionTypeData;
514 break;
515 case AllocationKind::Global:
516 sect_type = lldb::eSectionTypeData;
517 break;
518 case AllocationKind::Bytes:
519 sect_type = lldb::eSectionTypeOther;
520 break;
521 }
522
523 if (!name.empty()) {
524 if (name.equals("__text") || name.equals(".text"))
525 sect_type = lldb::eSectionTypeCode;
526 else if (name.equals("__data") || name.equals(".data"))
527 sect_type = lldb::eSectionTypeCode;
528 else if (name.startswith("__debug_") || name.startswith(".debug_")) {
529 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
530 llvm::StringRef dwarf_name(name.substr(name_idx));
531 switch (dwarf_name[0]) {
532 case 'a':
533 if (dwarf_name.equals("abbrev"))
534 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
535 else if (dwarf_name.equals("aranges"))
536 sect_type = lldb::eSectionTypeDWARFDebugAranges;
537 else if (dwarf_name.equals("addr"))
538 sect_type = lldb::eSectionTypeDWARFDebugAddr;
539 break;
540
541 case 'f':
542 if (dwarf_name.equals("frame"))
543 sect_type = lldb::eSectionTypeDWARFDebugFrame;
544 break;
545
546 case 'i':
547 if (dwarf_name.equals("info"))
548 sect_type = lldb::eSectionTypeDWARFDebugInfo;
549 break;
550
551 case 'l':
552 if (dwarf_name.equals("line"))
553 sect_type = lldb::eSectionTypeDWARFDebugLine;
554 else if (dwarf_name.equals("loc"))
555 sect_type = lldb::eSectionTypeDWARFDebugLoc;
556 break;
557
558 case 'm':
559 if (dwarf_name.equals("macinfo"))
560 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
561 break;
562
563 case 'p':
564 if (dwarf_name.equals("pubnames"))
565 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
566 else if (dwarf_name.equals("pubtypes"))
567 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
568 break;
569
570 case 's':
571 if (dwarf_name.equals("str"))
572 sect_type = lldb::eSectionTypeDWARFDebugStr;
573 else if (dwarf_name.equals("str_offsets"))
574 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
575 break;
576
577 case 'r':
578 if (dwarf_name.equals("ranges"))
579 sect_type = lldb::eSectionTypeDWARFDebugRanges;
580 break;
581
582 default:
583 break;
584 }
Davide Italiano45925d72018-01-04 23:37:18 +0000585 } else if (name.startswith("__apple_") || name.startswith(".apple_"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 sect_type = lldb::eSectionTypeInvalid;
Davide Italiano45925d72018-01-04 23:37:18 +0000587 else if (name.equals("__objc_imageinfo"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 sect_type = lldb::eSectionTypeOther;
589 }
590 return sect_type;
Greg Clayton23f8c952014-03-24 23:10:19 +0000591}
592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
594 uintptr_t Size, unsigned Alignment, unsigned SectionID,
595 llvm::StringRef SectionName) {
596 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000597
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(
599 Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 m_parent.m_records.push_back(AllocationRecord(
602 (uintptr_t)return_value,
603 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
604 GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
605 Alignment, SectionID, SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 if (log) {
608 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
609 ", Alignment=%u, SectionID=%u) = %p",
610 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
611 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000614 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 lldb::ProcessSP process_sp =
616 m_parent.GetBestExecutionContextScope()->CalculateProcess();
617
618 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
619 }
620
621 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000622}
623
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
625 uintptr_t Size, unsigned Alignment, unsigned SectionID,
626 llvm::StringRef SectionName, bool IsReadOnly) {
627 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000628
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 uint8_t *return_value = m_default_mm_ap->allocateDataSection(
630 Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000631
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 uint32_t permissions = lldb::ePermissionsReadable;
633 if (!IsReadOnly)
634 permissions |= lldb::ePermissionsWritable;
635 m_parent.m_records.push_back(AllocationRecord(
636 (uintptr_t)return_value, permissions,
637 GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
638 Alignment, SectionID, SectionName.str().c_str()));
639 if (log) {
640 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
641 ", Alignment=%u, SectionID=%u) = %p",
642 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
643 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000644
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000646 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 lldb::ProcessSP process_sp =
648 m_parent.GetBestExecutionContextScope()->CalculateProcess();
649
650 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
651 }
652
653 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000654}
655
Sean Callananb2814802016-02-12 21:11:25 +0000656static ConstString
657FindBestAlternateMangledName(const ConstString &demangled,
658 const lldb::LanguageType &lang_type,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 const SymbolContext &sym_ctx) {
660 CPlusPlusLanguage::MethodName cpp_name(demangled);
661 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
Sean Callananb2814802016-02-12 21:11:25 +0000662
Kate Stoneb9c1b512016-09-06 20:57:50 +0000663 if (!scope_qualified_name.size())
664 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000665
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666 if (!sym_ctx.module_sp)
667 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
670 if (!sym_vendor)
671 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
674 if (!sym_file)
675 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000676
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 std::vector<ConstString> alternates;
678 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
Sean Callananb2814802016-02-12 21:11:25 +0000679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 std::vector<ConstString> param_and_qual_matches;
681 std::vector<ConstString> param_matches;
682 for (size_t i = 0; i < alternates.size(); i++) {
683 ConstString alternate_mangled_name = alternates[i];
684 Mangled mangled(alternate_mangled_name, true);
685 ConstString demangled = mangled.GetDemangledName(lang_type);
Sean Callananb2814802016-02-12 21:11:25 +0000686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
688 if (!cpp_name.IsValid())
689 continue;
Sean Callananb2814802016-02-12 21:11:25 +0000690
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
692 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
693 param_and_qual_matches.push_back(alternate_mangled_name);
694 else
695 param_matches.push_back(alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 }
Sean Callananb2814802016-02-12 21:11:25 +0000698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 if (param_and_qual_matches.size())
700 return param_and_qual_matches[0]; // It is assumed that there will be only
701 // one!
702 else if (param_matches.size())
703 return param_matches[0]; // Return one of them as a best match
704 else
705 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000706}
707
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708struct IRExecutionUnit::SearchSpec {
709 ConstString name;
710 uint32_t mask;
Sean Callananb2814802016-02-12 21:11:25 +0000711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeFull)
713 : name(n), mask(m) {}
Sean Callananb2814802016-02-12 21:11:25 +0000714};
715
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716void IRExecutionUnit::CollectCandidateCNames(
717 std::vector<IRExecutionUnit::SearchSpec> &C_specs,
718 const ConstString &name) {
719 if (m_strip_underscore && name.AsCString()[0] == '_')
720 C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
721 C_specs.push_back(SearchSpec(name));
Sean Callananb2814802016-02-12 21:11:25 +0000722}
723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724void IRExecutionUnit::CollectCandidateCPlusPlusNames(
725 std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
726 const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
727 for (const SearchSpec &C_spec : C_specs) {
728 const ConstString &name = C_spec.name;
Sean Callananb2814802016-02-12 21:11:25 +0000729
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
731 Mangled mangled(name, true);
732 ConstString demangled =
733 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 if (demangled) {
736 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
737 demangled, lldb::eLanguageTypeC_plus_plus, sc);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 if (best_alternate_mangled_name) {
740 CPP_specs.push_back(best_alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000741 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
744 }
Sean Callananb2814802016-02-12 21:11:25 +0000745 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746
Luke Drummondf5bb1d62016-12-19 17:22:44 +0000747 std::set<ConstString> alternates;
748 CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
749 CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 }
Sean Callananb2814802016-02-12 21:11:25 +0000751}
752
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753void IRExecutionUnit::CollectFallbackNames(
754 std::vector<SearchSpec> &fallback_specs,
755 const std::vector<SearchSpec> &C_specs) {
756 // As a last-ditch fallback, try the base name for C++ names. It's terrible,
757 // but the DWARF doesn't always encode "extern C" correctly.
758
759 for (const SearchSpec &C_spec : C_specs) {
760 const ConstString &name = C_spec.name;
761
762 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
763 Mangled mangled_name(name);
764 ConstString demangled_name =
765 mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
766 if (!demangled_name.IsEmpty()) {
767 const char *demangled_cstr = demangled_name.AsCString();
768 const char *lparen_loc = strchr(demangled_cstr, '(');
769 if (lparen_loc) {
770 llvm::StringRef base_name(demangled_cstr,
771 lparen_loc - demangled_cstr);
772 fallback_specs.push_back(ConstString(base_name));
Sean Callanan34ab28a2016-06-02 17:59:47 +0000773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000777}
778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779lldb::addr_t IRExecutionUnit::FindInSymbols(
780 const std::vector<IRExecutionUnit::SearchSpec> &specs,
781 const lldb_private::SymbolContext &sc) {
782 Target *target = sc.target_sp.get();
Sean Callanan34ab28a2016-06-02 17:59:47 +0000783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 if (!target) {
785 // we shouldn't be doing any symbol lookup at all without a target
786 return LLDB_INVALID_ADDRESS;
787 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 for (const SearchSpec &spec : specs) {
790 SymbolContextList sc_list;
Sean Callananfed0e7582016-02-23 23:09:06 +0000791
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 std::function<bool(lldb::addr_t &, SymbolContextList &,
795 const lldb_private::SymbolContext &)>
796 get_external_load_address = [&best_internal_load_address, target](
797 lldb::addr_t &load_address, SymbolContextList &sc_list,
798 const lldb_private::SymbolContext &sc) -> lldb::addr_t {
799 load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000800
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
802 SymbolContext candidate_sc;
803
804 sc_list.GetContextAtIndex(si, candidate_sc);
805
806 const bool is_external =
807 (candidate_sc.function) ||
808 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
809 if (candidate_sc.symbol) {
810 load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
811
812 if (load_address == LLDB_INVALID_ADDRESS) {
813 if (target->GetProcessSP())
814 load_address =
815 candidate_sc.symbol->GetAddress().GetLoadAddress(target);
816 else
817 load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
818 }
819 }
820
821 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
822 if (target->GetProcessSP())
823 load_address = candidate_sc.function->GetAddressRange()
824 .GetBaseAddress()
825 .GetLoadAddress(target);
826 else
827 load_address = candidate_sc.function->GetAddressRange()
828 .GetBaseAddress()
829 .GetFileAddress();
830 }
831
832 if (load_address != LLDB_INVALID_ADDRESS) {
833 if (is_external) {
834 return true;
835 } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
836 best_internal_load_address = load_address;
Sean Callananfed0e7582016-02-23 23:09:06 +0000837 load_address = LLDB_INVALID_ADDRESS;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839 }
840 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 return false;
843 };
Sean Callananfed0e7582016-02-23 23:09:06 +0000844
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 if (sc.module_sp) {
846 sc.module_sp->FindFunctions(spec.name, NULL, spec.mask,
847 true, // include_symbols
848 false, // include_inlines
849 true, // append
850 sc_list);
851 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000852
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
Ted Woodwardeee554d2016-03-09 22:05:17 +0000854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 if (get_external_load_address(load_address, sc_list, sc)) {
856 return load_address;
857 } else {
858 sc_list.Clear();
859 }
860
861 if (sc_list.GetSize() == 0 && sc.target_sp) {
862 sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
863 true, // include_symbols
864 false, // include_inlines
865 true, // append
866 sc_list);
867 }
868
869 if (get_external_load_address(load_address, sc_list, sc)) {
870 return load_address;
871 } else {
872 sc_list.Clear();
873 }
874
875 if (sc_list.GetSize() == 0 && sc.target_sp) {
876 sc.target_sp->GetImages().FindSymbolsWithNameAndType(
877 spec.name, lldb::eSymbolTypeAny, sc_list);
878 }
879
880 if (get_external_load_address(load_address, sc_list, sc)) {
881 return load_address;
882 }
Adrian Prantl05097242018-04-30 16:49:04 +0000883 // if there are any searches we try after this, add an sc_list.Clear() in
884 // an "else" clause here
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885
886 if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
887 return best_internal_load_address;
888 }
889 }
890
891 return LLDB_INVALID_ADDRESS;
892}
893
894lldb::addr_t
895IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
896 const lldb_private::SymbolContext &sc) {
897 lldb::TargetSP target_sp = sc.target_sp;
898
899 if (!target_sp) {
900 return LLDB_INVALID_ADDRESS;
901 }
902
903 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
904
905 if (!process_sp) {
906 return LLDB_INVALID_ADDRESS;
907 }
908
909 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
910
911 if (runtime) {
912 for (const SearchSpec &spec : specs) {
913 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
914
915 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
916 return symbol_load_addr;
917 }
918 }
919
920 return LLDB_INVALID_ADDRESS;
921}
922
923lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
924 const std::vector<SearchSpec> &specs,
925 const lldb_private::SymbolContext &sc) {
926 lldb::TargetSP target_sp = sc.target_sp;
927
928 for (const SearchSpec &spec : specs) {
929 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
930
931 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
932 return symbol_load_addr;
933 }
934
935 return LLDB_INVALID_ADDRESS;
936}
937
938lldb::addr_t
939IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) {
940 std::vector<SearchSpec> candidate_C_names;
941 std::vector<SearchSpec> candidate_CPlusPlus_names;
942
943 CollectCandidateCNames(candidate_C_names, name);
944
945 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
946 if (ret == LLDB_INVALID_ADDRESS)
947 ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
948
949 if (ret == LLDB_INVALID_ADDRESS)
950 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
951
952 if (ret == LLDB_INVALID_ADDRESS) {
953 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
954 m_sym_ctx);
955 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
956 }
957
958 if (ret == LLDB_INVALID_ADDRESS) {
959 std::vector<SearchSpec> candidate_fallback_names;
960
961 CollectFallbackNames(candidate_fallback_names, candidate_C_names);
962 ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
963 }
964
965 return ret;
966}
967
968void IRExecutionUnit::GetStaticInitializers(
969 std::vector<lldb::addr_t> &static_initializers) {
970 if (llvm::GlobalVariable *global_ctors =
971 m_module->getNamedGlobal("llvm.global_ctors")) {
972 if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
973 global_ctors->getInitializer())) {
974 for (llvm::Use &ctor_use : ctor_array->operands()) {
975 if (llvm::ConstantStruct *ctor_struct =
976 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
977 lldbassert(ctor_struct->getNumOperands() ==
978 3); // this is standardized
979 if (llvm::Function *ctor_function =
980 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 ConstString ctor_function_name_cs(ctor_function->getName().str());
982
983 for (JittedFunction &jitted_function : m_jitted_functions) {
984 if (ctor_function_name_cs == jitted_function.m_name) {
985 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
986 static_initializers.push_back(jitted_function.m_remote_addr);
Ted Woodwardeee554d2016-03-09 22:05:17 +0000987 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 break;
989 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000990 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 }
Sean Callananb2814802016-02-12 21:11:25 +0000992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 }
Sean Callananb2814802016-02-12 21:11:25 +0000994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 }
Sean Callananbd4dc692016-03-21 22:23:38 +0000996}
997
Jim Ingham27e5fe82015-01-27 18:03:05 +0000998uint64_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
1000 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Greg Claytond6171a72015-07-02 16:43:49 +00001001
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 ConstString name_cs(Name.c_str());
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 lldb::addr_t ret = m_parent.FindSymbol(name_cs);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 if (ret == LLDB_INVALID_ADDRESS) {
1007 if (log)
1008 log->Printf(
1009 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1010 Name.c_str());
Sean Callananb2814802016-02-12 21:11:25 +00001011
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 m_parent.ReportSymbolLookupError(name_cs);
1013 return 0xbad0bad0;
1014 } else {
1015 if (log)
1016 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1017 Name.c_str(), ret);
1018 return ret;
1019 }
Jim Ingham27e5fe82015-01-27 18:03:05 +00001020}
1021
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1023 const std::string &Name, bool AbortOnFailure) {
1024 assert(sizeof(void *) == 8);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 return (void *)getSymbolAddress(Name);
Jim Ingham27e5fe82015-01-27 18:03:05 +00001027}
1028
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001029lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1031 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananffae9442013-06-27 01:42:47 +00001032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 for (AllocationRecord &record : m_records) {
1034 if (local_address >= record.m_host_address &&
1035 local_address < record.m_host_address + record.m_size) {
1036 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1037 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001038
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 lldb::addr_t ret =
1040 record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001041
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 if (log) {
1043 log->Printf(
1044 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1045 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1046 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1047 local_address, (uint64_t)record.m_host_address,
1048 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1049 record.m_process_address, record.m_process_address + record.m_size);
1050 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 return ret;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 return LLDB_INVALID_ADDRESS;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001057}
1058
1059IRExecutionUnit::AddrRange
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1061 for (AllocationRecord &record : m_records) {
1062 if (local_address >= record.m_host_address &&
1063 local_address < record.m_host_address + record.m_size) {
1064 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1065 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 return AddrRange(record.m_process_address, record.m_size);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 return AddrRange(0, 0);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001072}
1073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
Zachary Turner97206d52017-05-12 04:51:55 +00001075 Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 AllocationRecord &record) {
1077 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1078 return true;
1079 }
1080
1081 switch (record.m_sect_type) {
1082 case lldb::eSectionTypeInvalid:
1083 case lldb::eSectionTypeDWARFDebugAbbrev:
1084 case lldb::eSectionTypeDWARFDebugAddr:
1085 case lldb::eSectionTypeDWARFDebugAranges:
Tamas Berghammer963ce482017-08-25 13:56:14 +00001086 case lldb::eSectionTypeDWARFDebugCuIndex:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 case lldb::eSectionTypeDWARFDebugFrame:
1088 case lldb::eSectionTypeDWARFDebugInfo:
1089 case lldb::eSectionTypeDWARFDebugLine:
1090 case lldb::eSectionTypeDWARFDebugLoc:
1091 case lldb::eSectionTypeDWARFDebugMacInfo:
1092 case lldb::eSectionTypeDWARFDebugPubNames:
1093 case lldb::eSectionTypeDWARFDebugPubTypes:
1094 case lldb::eSectionTypeDWARFDebugRanges:
1095 case lldb::eSectionTypeDWARFDebugStr:
1096 case lldb::eSectionTypeDWARFDebugStrOffsets:
1097 case lldb::eSectionTypeDWARFAppleNames:
1098 case lldb::eSectionTypeDWARFAppleTypes:
1099 case lldb::eSectionTypeDWARFAppleNamespaces:
1100 case lldb::eSectionTypeDWARFAppleObjC:
Jan Kratochvile4777a92018-04-29 19:47:48 +00001101 case lldb::eSectionTypeDWARFGNUDebugAltLink:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 error.Clear();
1103 break;
1104 default:
1105 const bool zero_memory = false;
1106 record.m_process_address =
1107 Malloc(record.m_size, record.m_alignment, record.m_permissions,
1108 eAllocationPolicyProcessOnly, zero_memory, error);
1109 break;
1110 }
1111
1112 return error.Success();
1113}
1114
1115bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1116 bool ret = true;
1117
Zachary Turner97206d52017-05-12 04:51:55 +00001118 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119
1120 for (AllocationRecord &record : m_records) {
1121 ret = CommitOneAllocation(process_sp, err, record);
1122
1123 if (!ret) {
1124 break;
Sean Callananbd4dc692016-03-21 22:23:38 +00001125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 }
1127
1128 if (!ret) {
1129 for (AllocationRecord &record : m_records) {
1130 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1131 Free(record.m_process_address, err);
1132 record.m_process_address = LLDB_INVALID_ADDRESS;
1133 }
Sean Callananbd4dc692016-03-21 22:23:38 +00001134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 }
1136
1137 return ret;
Sean Callananbd4dc692016-03-21 22:23:38 +00001138}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001139
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1141 m_reported_allocations = true;
Sean Callananbd4dc692016-03-21 22:23:38 +00001142
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 for (AllocationRecord &record : m_records) {
1144 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1145 continue;
Sean Callananbd4dc692016-03-21 22:23:38 +00001146
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 if (record.m_section_id == eSectionIDInvalid)
1148 continue;
1149
1150 engine.mapSectionAddress((void *)record.m_host_address,
1151 record.m_process_address);
1152 }
1153
1154 // Trigger re-application of relocations.
1155 engine.finalizeObject();
1156}
1157
1158bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1159 bool wrote_something = false;
1160 for (AllocationRecord &record : m_records) {
1161 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +00001162 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1164 record.m_size, err);
1165 if (err.Success())
1166 wrote_something = true;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 }
1169 return wrote_something;
1170}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1173 if (!log)
1174 return;
1175
1176 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1177 (unsigned long long)m_host_address, (unsigned long long)m_size,
1178 (unsigned long long)m_process_address, (unsigned)m_alignment,
1179 (unsigned)m_section_id, m_name.c_str());
1180}
1181
1182lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1183 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1184 return exe_ctx.GetByteOrder();
1185}
1186
1187uint32_t IRExecutionUnit::GetAddressByteSize() const {
1188 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1189 return exe_ctx.GetAddressByteSize();
1190}
1191
1192void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1193 lldb_private::Symtab &symtab) {
1194 // No symbols yet...
1195}
1196
1197void IRExecutionUnit::PopulateSectionList(
1198 lldb_private::ObjectFile *obj_file,
1199 lldb_private::SectionList &section_list) {
1200 for (AllocationRecord &record : m_records) {
1201 if (record.m_size > 0) {
1202 lldb::SectionSP section_sp(new lldb_private::Section(
1203 obj_file->GetModule(), obj_file, record.m_section_id,
1204 ConstString(record.m_name), record.m_sect_type,
1205 record.m_process_address, record.m_size,
1206 record.m_host_address, // file_offset (which is the host address for
1207 // the data)
1208 record.m_size, // file_size
1209 0,
1210 record.m_permissions)); // flags
1211 section_list.AddSection(section_sp);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001214}
1215
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216bool IRExecutionUnit::GetArchitecture(lldb_private::ArchSpec &arch) {
1217 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1218 Target *target = exe_ctx.GetTargetPtr();
1219 if (target)
1220 arch = target->GetArchitecture();
1221 else
1222 arch.Clear();
1223 return arch.IsValid();
1224}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001225
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1227 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1228 Target *target = exe_ctx.GetTargetPtr();
Zachary Turner8e8d4b92018-05-23 23:56:09 +00001229 if (!target)
1230 return nullptr;
1231
1232 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1233 shared_from_this());
1234
1235 lldb::ModuleSP jit_module_sp =
1236 lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1237 if (!jit_module_sp)
1238 return nullptr;
1239
1240 bool changed = false;
1241 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1242 return jit_module_sp;
Greg Clayton23f8c952014-03-24 23:10:19 +00001243}