blob: 298ad10e9dddfe74843d7fb396e83b01562fb5f9 [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"
36
Sean Callanan8dfb68e2013-03-19 00:10:07 +000037using namespace lldb_private;
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_ap,
40 std::unique_ptr<llvm::Module> &module_ap,
41 ConstString &name,
42 const lldb::TargetSP &target_sp,
43 const SymbolContext &sym_ctx,
44 std::vector<std::string> &cpu_features)
45 : IRMemoryMap(target_sp), m_context_ap(context_ap.release()),
46 m_module_ap(module_ap.release()), m_module(m_module_ap.get()),
47 m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
48 m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
49 m_function_end_load_addr(LLDB_INVALID_ADDRESS),
50 m_reported_allocations(false) {}
Sean Callanan8dfb68e2013-03-19 00:10:07 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
Zachary Turner97206d52017-05-12 04:51:55 +000053 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 const bool zero_memory = false;
55 lldb::addr_t allocation_process_addr =
56 Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable,
57 eAllocationPolicyMirror, zero_memory, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if (!error.Success())
60 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 if (!error.Success()) {
Zachary Turner97206d52017-05-12 04:51:55 +000065 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 Free(allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 return LLDB_INVALID_ADDRESS;
69 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 if (Log *log =
72 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
73 DataBufferHeap my_buffer(size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +000074 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 if (err.Success()) {
78 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(),
79 lldb::eByteOrderBig, 8);
80 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
81 allocation_process_addr, 16,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000082 DataExtractor::TypeUInt8);
83 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 return allocation_process_addr;
87}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089void IRExecutionUnit::FreeNow(lldb::addr_t allocation) {
90 if (allocation == LLDB_INVALID_ADDRESS)
91 return;
92
Zachary Turner97206d52017-05-12 04:51:55 +000093 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +000094
95 Free(allocation, err);
96}
97
Zachary Turner97206d52017-05-12 04:51:55 +000098Status IRExecutionUnit::DisassembleFunction(Stream &stream,
99 lldb::ProcessSP &process_wp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
101
102 ExecutionContext exe_ctx(process_wp);
103
Zachary Turner97206d52017-05-12 04:51:55 +0000104 Status ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105
106 ret.Clear();
107
108 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
109 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
110
111 for (JittedFunction &function : m_jitted_functions) {
112 if (function.m_name == m_name) {
113 func_local_addr = function.m_local_addr;
114 func_remote_addr = function.m_remote_addr;
115 }
116 }
117
118 if (func_local_addr == LLDB_INVALID_ADDRESS) {
119 ret.SetErrorToGenericError();
120 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly",
121 m_name.AsCString());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000122 return ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 }
124
125 if (log)
126 log->Printf("Found function, has local address 0x%" PRIx64
127 " and remote address 0x%" PRIx64,
128 (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
129
130 std::pair<lldb::addr_t, lldb::addr_t> func_range;
131
132 func_range = GetRemoteRangeForLocal(func_local_addr);
133
134 if (func_range.first == 0 && func_range.second == 0) {
135 ret.SetErrorToGenericError();
136 ret.SetErrorStringWithFormat("Couldn't find code range for function %s",
137 m_name.AsCString());
138 return ret;
139 }
140
141 if (log)
142 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
143 func_range.first, func_range.second);
144
145 Target *target = exe_ctx.GetTargetPtr();
146 if (!target) {
147 ret.SetErrorToGenericError();
148 ret.SetErrorString("Couldn't find the target");
149 return ret;
150 }
151
152 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
153
154 Process *process = exe_ctx.GetProcessPtr();
Zachary Turner97206d52017-05-12 04:51:55 +0000155 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
157 buffer_sp->GetByteSize(), err);
158
159 if (!err.Success()) {
160 ret.SetErrorToGenericError();
161 ret.SetErrorStringWithFormat("Couldn't read from process: %s",
162 err.AsCString("unknown error"));
163 return ret;
164 }
165
166 ArchSpec arch(target->GetArchitecture());
167
168 const char *plugin_name = NULL;
169 const char *flavor_string = NULL;
170 lldb::DisassemblerSP disassembler_sp =
171 Disassembler::FindPlugin(arch, flavor_string, plugin_name);
172
173 if (!disassembler_sp) {
174 ret.SetErrorToGenericError();
175 ret.SetErrorStringWithFormat(
176 "Unable to find disassembler plug-in for %s architecture.",
177 arch.GetArchitectureName());
178 return ret;
179 }
180
181 if (!process) {
182 ret.SetErrorToGenericError();
183 ret.SetErrorString("Couldn't find the process");
184 return ret;
185 }
186
187 DataExtractor extractor(buffer_sp, process->GetByteOrder(),
188 target->GetArchitecture().GetAddressByteSize());
189
190 if (log) {
191 log->Printf("Function data has contents:");
192 extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
193 DataExtractor::TypeUInt8);
194 }
195
196 disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
197 UINT32_MAX, false, false);
198
199 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
200 instruction_list.Dump(&stream, true, true, &exe_ctx);
201 return ret;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000202}
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic,
205 void *Context, unsigned LocCookie) {
Zachary Turner97206d52017-05-12 04:51:55 +0000206 Status *err = static_cast<Status *>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 if (err && err->Success()) {
209 err->SetErrorToGenericError();
210 err->SetErrorStringWithFormat("Inline assembly error: %s",
211 diagnostic.getMessage().str().c_str());
212 }
Sean Callanan44bc6572013-03-27 03:09:55 +0000213}
214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) {
216 m_failed_lookups.push_back(name);
Jim Ingham27e5fe82015-01-27 18:03:05 +0000217}
218
Zachary Turner97206d52017-05-12 04:51:55 +0000219void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 lldb::addr_t &func_end) {
221 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 static std::recursive_mutex s_runnable_info_mutex;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 func_addr = LLDB_INVALID_ADDRESS;
226 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 if (!process_sp) {
229 error.SetErrorToGenericError();
230 error.SetErrorString("Couldn't write the JIT compiled code into the "
231 "process because the process is invalid");
232 return;
233 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 if (m_did_jit) {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000236 func_addr = m_function_load_addr;
237 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000239 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 };
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 m_did_jit = true;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 std::string error_string;
249
250 if (log) {
251 std::string s;
252 llvm::raw_string_ostream oss(s);
253
254 m_module->print(oss, NULL);
255
256 oss.flush();
257
258 log->Printf("Module being sent to JIT: \n%s", s.c_str());
259 }
260
261 llvm::Triple triple(m_module->getTargetTriple());
262 llvm::Reloc::Model relocModel;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263
264 if (triple.isOSBinFormatELF()) {
265 relocModel = llvm::Reloc::Static;
266 } else {
267 relocModel = llvm::Reloc::PIC_;
268 }
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError,
271 &error);
272
273 llvm::EngineBuilder builder(std::move(m_module_ap));
274
275 builder.setEngineKind(llvm::EngineKind::JIT)
276 .setErrorStr(&error_string)
277 .setRelocationModel(relocModel)
278 .setMCJITMemoryManager(
279 std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
Pavel Labathd7396362017-11-13 14:03:17 +0000280 .setOptLevel(llvm::CodeGenOpt::Less);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281
282 llvm::StringRef mArch;
283 llvm::StringRef mCPU;
284 llvm::SmallVector<std::string, 0> mAttrs;
285
286 for (std::string &feature : m_cpu_features)
287 mAttrs.push_back(feature);
288
289 llvm::TargetMachine *target_machine =
290 builder.selectTarget(triple, mArch, mCPU, mAttrs);
291
292 m_execution_engine_ap.reset(builder.create(target_machine));
293
294 m_strip_underscore =
295 (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_');
296
297 if (!m_execution_engine_ap.get()) {
298 error.SetErrorToGenericError();
299 error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
300 error_string.c_str());
301 return;
302 }
303
Sean Callanan5deb06e2016-09-26 20:18:51 +0000304 class ObjectDumper : public llvm::ObjectCache {
305 public:
306 void notifyObjectCompiled(const llvm::Module *module,
307 llvm::MemoryBufferRef object) override {
308 int fd = 0;
309 llvm::SmallVector<char, 256> result_path;
310 std::string object_name_model =
311 "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
312 (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
313 llvm::raw_fd_ostream fds(fd, true);
314 fds.write(object.getBufferStart(), object.getBufferSize());
315 }
316
317 std::unique_ptr<llvm::MemoryBuffer>
318 getObject(const llvm::Module *module) override {
319 // Return nothing - we're just abusing the object-cache mechanism to dump
320 // objects.
321 return nullptr;
322 }
323 };
324
325 if (process_sp->GetTarget().GetEnableSaveObjects()) {
326 m_object_cache_ap = llvm::make_unique<ObjectDumper>();
327 m_execution_engine_ap->setObjectCache(m_object_cache_ap.get());
328 }
329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 // Make sure we see all sections, including ones that don't have
331 // relocations...
332 m_execution_engine_ap->setProcessAllSections(true);
333
334 m_execution_engine_ap->DisableLazyCompilation();
335
336 for (llvm::Function &function : *m_module) {
337 if (function.isDeclaration() || function.hasPrivateLinkage())
338 continue;
339
340 const bool external =
341 function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
342
343 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(&function);
344
345 if (!error.Success()) {
346 // We got an error through our callback!
347 return;
Greg Clayton23f8c952014-03-24 23:10:19 +0000348 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 if (!fun_ptr) {
351 error.SetErrorToGenericError();
352 error.SetErrorStringWithFormat(
353 "'%s' was in the JITted module but wasn't lowered",
354 function.getName().str().c_str());
355 return;
356 }
357 m_jitted_functions.push_back(JittedFunction(
358 function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr));
359 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 CommitAllocations(process_sp);
362 ReportAllocations(*m_execution_engine_ap);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 // We have to do this after calling ReportAllocations because for the MCJIT,
Adrian Prantl05097242018-04-30 16:49:04 +0000365 // getGlobalValueAddress will cause the JIT to perform all relocations. That
366 // can only be done once, and has to happen after we do the remapping from
367 // local -> remote. That means we don't know the local address of the
368 // Variables, but we don't need that for anything, so that's okay.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
371 llvm::GlobalValue &val) {
372 if (val.hasExternalLinkage() && !val.isDeclaration()) {
373 uint64_t var_ptr_addr =
374 m_execution_engine_ap->getGlobalValueAddress(val.getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 // This is a really unfortunae API that sometimes returns local addresses
Adrian Prantl05097242018-04-30 16:49:04 +0000379 // and sometimes returns remote addresses, based on whether the variable
380 // was relocated during ReportAllocations or not.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 if (remote_addr == LLDB_INVALID_ADDRESS) {
383 remote_addr = var_ptr_addr;
384 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 if (var_ptr_addr != 0)
387 m_jitted_global_variables.push_back(JittedGlobalVariable(
388 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
389 }
390 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) {
393 RegisterOneValue(global_var);
394 }
395
396 for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
397 RegisterOneValue(global_alias);
398 }
399
400 WriteData(process_sp);
401
402 if (m_failed_lookups.size()) {
403 StreamString ss;
404
405 ss.PutCString("Couldn't lookup symbols:\n");
406
407 bool emitNewLine = false;
408
409 for (const ConstString &failed_lookup : m_failed_lookups) {
410 if (emitNewLine)
411 ss.PutCString("\n");
412 emitNewLine = true;
413 ss.PutCString(" ");
414 ss.PutCString(Mangled(failed_lookup)
415 .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus)
416 .AsCString());
417 }
418
419 m_failed_lookups.clear();
420
Zachary Turnerc1564272016-11-16 21:15:24 +0000421 error.SetErrorString(ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422
423 return;
424 }
425
426 m_function_load_addr = LLDB_INVALID_ADDRESS;
427 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
428
429 for (JittedFunction &jitted_function : m_jitted_functions) {
430 jitted_function.m_remote_addr =
431 GetRemoteAddressForLocal(jitted_function.m_local_addr);
432
433 if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
434 AddrRange func_range =
435 GetRemoteRangeForLocal(jitted_function.m_local_addr);
436 m_function_end_load_addr = func_range.first + func_range.second;
437 m_function_load_addr = jitted_function.m_remote_addr;
438 }
439 }
440
441 if (log) {
442 log->Printf("Code can be run in the target.");
443
444 StreamString disassembly_stream;
445
Zachary Turner97206d52017-05-12 04:51:55 +0000446 Status err = DisassembleFunction(disassembly_stream, process_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447
448 if (!err.Success()) {
449 log->Printf("Couldn't disassemble function : %s",
450 err.AsCString("unknown error"));
451 } else {
452 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
453 }
454
455 log->Printf("Sections: ");
456 for (AllocationRecord &record : m_records) {
457 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
458 record.dump(log);
459
460 DataBufferHeap my_buffer(record.m_size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +0000461 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 ReadMemory(my_buffer.GetBytes(), record.m_process_address,
463 record.m_size, err);
464
465 if (err.Success()) {
466 DataExtractor my_extractor(my_buffer.GetBytes(),
467 my_buffer.GetByteSize(),
468 lldb::eByteOrderBig, 8);
469 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
470 record.m_process_address, 16,
471 DataExtractor::TypeUInt8);
Greg Clayton23f8c952014-03-24 23:10:19 +0000472 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 } else {
474 record.dump(log);
475
476 DataExtractor my_extractor((const void *)record.m_host_address,
477 record.m_size, lldb::eByteOrderBig, 8);
478 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
479 DataExtractor::TypeUInt8);
480 }
481 }
482 }
483
484 func_addr = m_function_load_addr;
485 func_end = m_function_end_load_addr;
486
487 return;
488}
489
490IRExecutionUnit::~IRExecutionUnit() {
491 m_module_ap.reset();
492 m_execution_engine_ap.reset();
493 m_context_ap.reset();
494}
495
496IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
497 : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {}
498
499IRExecutionUnit::MemoryManager::~MemoryManager() {}
500
501lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
502 const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
503 lldb::SectionType sect_type = lldb::eSectionTypeCode;
504 switch (alloc_kind) {
505 case AllocationKind::Stub:
506 sect_type = lldb::eSectionTypeCode;
507 break;
508 case AllocationKind::Code:
509 sect_type = lldb::eSectionTypeCode;
510 break;
511 case AllocationKind::Data:
512 sect_type = lldb::eSectionTypeData;
513 break;
514 case AllocationKind::Global:
515 sect_type = lldb::eSectionTypeData;
516 break;
517 case AllocationKind::Bytes:
518 sect_type = lldb::eSectionTypeOther;
519 break;
520 }
521
522 if (!name.empty()) {
523 if (name.equals("__text") || name.equals(".text"))
524 sect_type = lldb::eSectionTypeCode;
525 else if (name.equals("__data") || name.equals(".data"))
526 sect_type = lldb::eSectionTypeCode;
527 else if (name.startswith("__debug_") || name.startswith(".debug_")) {
528 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
529 llvm::StringRef dwarf_name(name.substr(name_idx));
530 switch (dwarf_name[0]) {
531 case 'a':
532 if (dwarf_name.equals("abbrev"))
533 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
534 else if (dwarf_name.equals("aranges"))
535 sect_type = lldb::eSectionTypeDWARFDebugAranges;
536 else if (dwarf_name.equals("addr"))
537 sect_type = lldb::eSectionTypeDWARFDebugAddr;
538 break;
539
540 case 'f':
541 if (dwarf_name.equals("frame"))
542 sect_type = lldb::eSectionTypeDWARFDebugFrame;
543 break;
544
545 case 'i':
546 if (dwarf_name.equals("info"))
547 sect_type = lldb::eSectionTypeDWARFDebugInfo;
548 break;
549
550 case 'l':
551 if (dwarf_name.equals("line"))
552 sect_type = lldb::eSectionTypeDWARFDebugLine;
553 else if (dwarf_name.equals("loc"))
554 sect_type = lldb::eSectionTypeDWARFDebugLoc;
555 break;
556
557 case 'm':
558 if (dwarf_name.equals("macinfo"))
559 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
560 break;
561
562 case 'p':
563 if (dwarf_name.equals("pubnames"))
564 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
565 else if (dwarf_name.equals("pubtypes"))
566 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
567 break;
568
569 case 's':
570 if (dwarf_name.equals("str"))
571 sect_type = lldb::eSectionTypeDWARFDebugStr;
572 else if (dwarf_name.equals("str_offsets"))
573 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
574 break;
575
576 case 'r':
577 if (dwarf_name.equals("ranges"))
578 sect_type = lldb::eSectionTypeDWARFDebugRanges;
579 break;
580
581 default:
582 break;
583 }
Davide Italiano45925d72018-01-04 23:37:18 +0000584 } else if (name.startswith("__apple_") || name.startswith(".apple_"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 sect_type = lldb::eSectionTypeInvalid;
Davide Italiano45925d72018-01-04 23:37:18 +0000586 else if (name.equals("__objc_imageinfo"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 sect_type = lldb::eSectionTypeOther;
588 }
589 return sect_type;
Greg Clayton23f8c952014-03-24 23:10:19 +0000590}
591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
593 uintptr_t Size, unsigned Alignment, unsigned SectionID,
594 llvm::StringRef SectionName) {
595 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(
598 Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000599
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 m_parent.m_records.push_back(AllocationRecord(
601 (uintptr_t)return_value,
602 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
603 GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
604 Alignment, SectionID, SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000605
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 if (log) {
607 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
608 ", Alignment=%u, SectionID=%u) = %p",
609 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
610 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000613 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 lldb::ProcessSP process_sp =
615 m_parent.GetBestExecutionContextScope()->CalculateProcess();
616
617 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
618 }
619
620 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000621}
622
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
624 uintptr_t Size, unsigned Alignment, unsigned SectionID,
625 llvm::StringRef SectionName, bool IsReadOnly) {
626 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000627
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 uint8_t *return_value = m_default_mm_ap->allocateDataSection(
629 Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 uint32_t permissions = lldb::ePermissionsReadable;
632 if (!IsReadOnly)
633 permissions |= lldb::ePermissionsWritable;
634 m_parent.m_records.push_back(AllocationRecord(
635 (uintptr_t)return_value, permissions,
636 GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
637 Alignment, SectionID, SectionName.str().c_str()));
638 if (log) {
639 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
640 ", Alignment=%u, SectionID=%u) = %p",
641 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
642 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000643
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000645 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 lldb::ProcessSP process_sp =
647 m_parent.GetBestExecutionContextScope()->CalculateProcess();
648
649 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
650 }
651
652 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000653}
654
Sean Callananb2814802016-02-12 21:11:25 +0000655static ConstString
656FindBestAlternateMangledName(const ConstString &demangled,
657 const lldb::LanguageType &lang_type,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 const SymbolContext &sym_ctx) {
659 CPlusPlusLanguage::MethodName cpp_name(demangled);
660 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
Sean Callananb2814802016-02-12 21:11:25 +0000661
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 if (!scope_qualified_name.size())
663 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000664
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 if (!sym_ctx.module_sp)
666 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000667
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
669 if (!sym_vendor)
670 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
673 if (!sym_file)
674 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676 std::vector<ConstString> alternates;
677 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
Sean Callananb2814802016-02-12 21:11:25 +0000678
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 std::vector<ConstString> param_and_qual_matches;
680 std::vector<ConstString> param_matches;
681 for (size_t i = 0; i < alternates.size(); i++) {
682 ConstString alternate_mangled_name = alternates[i];
683 Mangled mangled(alternate_mangled_name, true);
684 ConstString demangled = mangled.GetDemangledName(lang_type);
Sean Callananb2814802016-02-12 21:11:25 +0000685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
687 if (!cpp_name.IsValid())
688 continue;
Sean Callananb2814802016-02-12 21:11:25 +0000689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
691 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
692 param_and_qual_matches.push_back(alternate_mangled_name);
693 else
694 param_matches.push_back(alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000695 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 }
Sean Callananb2814802016-02-12 21:11:25 +0000697
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 if (param_and_qual_matches.size())
699 return param_and_qual_matches[0]; // It is assumed that there will be only
700 // one!
701 else if (param_matches.size())
702 return param_matches[0]; // Return one of them as a best match
703 else
704 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000705}
706
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707struct IRExecutionUnit::SearchSpec {
708 ConstString name;
709 uint32_t mask;
Sean Callananb2814802016-02-12 21:11:25 +0000710
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeFull)
712 : name(n), mask(m) {}
Sean Callananb2814802016-02-12 21:11:25 +0000713};
714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715void IRExecutionUnit::CollectCandidateCNames(
716 std::vector<IRExecutionUnit::SearchSpec> &C_specs,
717 const ConstString &name) {
718 if (m_strip_underscore && name.AsCString()[0] == '_')
719 C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
720 C_specs.push_back(SearchSpec(name));
Sean Callananb2814802016-02-12 21:11:25 +0000721}
722
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723void IRExecutionUnit::CollectCandidateCPlusPlusNames(
724 std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
725 const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
726 for (const SearchSpec &C_spec : C_specs) {
727 const ConstString &name = C_spec.name;
Sean Callananb2814802016-02-12 21:11:25 +0000728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
730 Mangled mangled(name, true);
731 ConstString demangled =
732 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 if (demangled) {
735 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
736 demangled, lldb::eLanguageTypeC_plus_plus, sc);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 if (best_alternate_mangled_name) {
739 CPP_specs.push_back(best_alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000740 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000741
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
743 }
Sean Callananb2814802016-02-12 21:11:25 +0000744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745
Luke Drummondf5bb1d62016-12-19 17:22:44 +0000746 std::set<ConstString> alternates;
747 CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
748 CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 }
Sean Callananb2814802016-02-12 21:11:25 +0000750}
751
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752void IRExecutionUnit::CollectFallbackNames(
753 std::vector<SearchSpec> &fallback_specs,
754 const std::vector<SearchSpec> &C_specs) {
755 // As a last-ditch fallback, try the base name for C++ names. It's terrible,
756 // but the DWARF doesn't always encode "extern C" correctly.
757
758 for (const SearchSpec &C_spec : C_specs) {
759 const ConstString &name = C_spec.name;
760
761 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
762 Mangled mangled_name(name);
763 ConstString demangled_name =
764 mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
765 if (!demangled_name.IsEmpty()) {
766 const char *demangled_cstr = demangled_name.AsCString();
767 const char *lparen_loc = strchr(demangled_cstr, '(');
768 if (lparen_loc) {
769 llvm::StringRef base_name(demangled_cstr,
770 lparen_loc - demangled_cstr);
771 fallback_specs.push_back(ConstString(base_name));
Sean Callanan34ab28a2016-06-02 17:59:47 +0000772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000774 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000776}
777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778lldb::addr_t IRExecutionUnit::FindInSymbols(
779 const std::vector<IRExecutionUnit::SearchSpec> &specs,
780 const lldb_private::SymbolContext &sc) {
781 Target *target = sc.target_sp.get();
Sean Callanan34ab28a2016-06-02 17:59:47 +0000782
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 if (!target) {
784 // we shouldn't be doing any symbol lookup at all without a target
785 return LLDB_INVALID_ADDRESS;
786 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 for (const SearchSpec &spec : specs) {
789 SymbolContextList sc_list;
Sean Callananfed0e7582016-02-23 23:09:06 +0000790
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 std::function<bool(lldb::addr_t &, SymbolContextList &,
794 const lldb_private::SymbolContext &)>
795 get_external_load_address = [&best_internal_load_address, target](
796 lldb::addr_t &load_address, SymbolContextList &sc_list,
797 const lldb_private::SymbolContext &sc) -> lldb::addr_t {
798 load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000799
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
801 SymbolContext candidate_sc;
802
803 sc_list.GetContextAtIndex(si, candidate_sc);
804
805 const bool is_external =
806 (candidate_sc.function) ||
807 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
808 if (candidate_sc.symbol) {
809 load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
810
811 if (load_address == LLDB_INVALID_ADDRESS) {
812 if (target->GetProcessSP())
813 load_address =
814 candidate_sc.symbol->GetAddress().GetLoadAddress(target);
815 else
816 load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
817 }
818 }
819
820 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
821 if (target->GetProcessSP())
822 load_address = candidate_sc.function->GetAddressRange()
823 .GetBaseAddress()
824 .GetLoadAddress(target);
825 else
826 load_address = candidate_sc.function->GetAddressRange()
827 .GetBaseAddress()
828 .GetFileAddress();
829 }
830
831 if (load_address != LLDB_INVALID_ADDRESS) {
832 if (is_external) {
833 return true;
834 } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
835 best_internal_load_address = load_address;
Sean Callananfed0e7582016-02-23 23:09:06 +0000836 load_address = LLDB_INVALID_ADDRESS;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 }
838 }
839 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 return false;
842 };
Sean Callananfed0e7582016-02-23 23:09:06 +0000843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 if (sc.module_sp) {
845 sc.module_sp->FindFunctions(spec.name, NULL, spec.mask,
846 true, // include_symbols
847 false, // include_inlines
848 true, // append
849 sc_list);
850 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
Ted Woodwardeee554d2016-03-09 22:05:17 +0000853
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 if (get_external_load_address(load_address, sc_list, sc)) {
855 return load_address;
856 } else {
857 sc_list.Clear();
858 }
859
860 if (sc_list.GetSize() == 0 && sc.target_sp) {
861 sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
862 true, // include_symbols
863 false, // include_inlines
864 true, // append
865 sc_list);
866 }
867
868 if (get_external_load_address(load_address, sc_list, sc)) {
869 return load_address;
870 } else {
871 sc_list.Clear();
872 }
873
874 if (sc_list.GetSize() == 0 && sc.target_sp) {
875 sc.target_sp->GetImages().FindSymbolsWithNameAndType(
876 spec.name, lldb::eSymbolTypeAny, sc_list);
877 }
878
879 if (get_external_load_address(load_address, sc_list, sc)) {
880 return load_address;
881 }
Adrian Prantl05097242018-04-30 16:49:04 +0000882 // if there are any searches we try after this, add an sc_list.Clear() in
883 // an "else" clause here
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884
885 if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
886 return best_internal_load_address;
887 }
888 }
889
890 return LLDB_INVALID_ADDRESS;
891}
892
893lldb::addr_t
894IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
895 const lldb_private::SymbolContext &sc) {
896 lldb::TargetSP target_sp = sc.target_sp;
897
898 if (!target_sp) {
899 return LLDB_INVALID_ADDRESS;
900 }
901
902 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
903
904 if (!process_sp) {
905 return LLDB_INVALID_ADDRESS;
906 }
907
908 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
909
910 if (runtime) {
911 for (const SearchSpec &spec : specs) {
912 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
913
914 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
915 return symbol_load_addr;
916 }
917 }
918
919 return LLDB_INVALID_ADDRESS;
920}
921
922lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
923 const std::vector<SearchSpec> &specs,
924 const lldb_private::SymbolContext &sc) {
925 lldb::TargetSP target_sp = sc.target_sp;
926
927 for (const SearchSpec &spec : specs) {
928 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
929
930 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
931 return symbol_load_addr;
932 }
933
934 return LLDB_INVALID_ADDRESS;
935}
936
937lldb::addr_t
938IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) {
939 std::vector<SearchSpec> candidate_C_names;
940 std::vector<SearchSpec> candidate_CPlusPlus_names;
941
942 CollectCandidateCNames(candidate_C_names, name);
943
944 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
945 if (ret == LLDB_INVALID_ADDRESS)
946 ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
947
948 if (ret == LLDB_INVALID_ADDRESS)
949 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
950
951 if (ret == LLDB_INVALID_ADDRESS) {
952 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
953 m_sym_ctx);
954 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
955 }
956
957 if (ret == LLDB_INVALID_ADDRESS) {
958 std::vector<SearchSpec> candidate_fallback_names;
959
960 CollectFallbackNames(candidate_fallback_names, candidate_C_names);
961 ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
962 }
963
964 return ret;
965}
966
967void IRExecutionUnit::GetStaticInitializers(
968 std::vector<lldb::addr_t> &static_initializers) {
969 if (llvm::GlobalVariable *global_ctors =
970 m_module->getNamedGlobal("llvm.global_ctors")) {
971 if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
972 global_ctors->getInitializer())) {
973 for (llvm::Use &ctor_use : ctor_array->operands()) {
974 if (llvm::ConstantStruct *ctor_struct =
975 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
976 lldbassert(ctor_struct->getNumOperands() ==
977 3); // this is standardized
978 if (llvm::Function *ctor_function =
979 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 ConstString ctor_function_name_cs(ctor_function->getName().str());
981
982 for (JittedFunction &jitted_function : m_jitted_functions) {
983 if (ctor_function_name_cs == jitted_function.m_name) {
984 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
985 static_initializers.push_back(jitted_function.m_remote_addr);
Ted Woodwardeee554d2016-03-09 22:05:17 +0000986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 break;
988 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 }
Sean Callananb2814802016-02-12 21:11:25 +0000991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 }
Sean Callananb2814802016-02-12 21:11:25 +0000993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 }
Sean Callananbd4dc692016-03-21 22:23:38 +0000995}
996
Jim Ingham27e5fe82015-01-27 18:03:05 +0000997uint64_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
999 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Greg Claytond6171a72015-07-02 16:43:49 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 ConstString name_cs(Name.c_str());
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001002
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 lldb::addr_t ret = m_parent.FindSymbol(name_cs);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 if (ret == LLDB_INVALID_ADDRESS) {
1006 if (log)
1007 log->Printf(
1008 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1009 Name.c_str());
Sean Callananb2814802016-02-12 21:11:25 +00001010
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 m_parent.ReportSymbolLookupError(name_cs);
1012 return 0xbad0bad0;
1013 } else {
1014 if (log)
1015 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1016 Name.c_str(), ret);
1017 return ret;
1018 }
Jim Ingham27e5fe82015-01-27 18:03:05 +00001019}
1020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1022 const std::string &Name, bool AbortOnFailure) {
1023 assert(sizeof(void *) == 8);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 return (void *)getSymbolAddress(Name);
Jim Ingham27e5fe82015-01-27 18:03:05 +00001026}
1027
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001028lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1030 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananffae9442013-06-27 01:42:47 +00001031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 for (AllocationRecord &record : m_records) {
1033 if (local_address >= record.m_host_address &&
1034 local_address < record.m_host_address + record.m_size) {
1035 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1036 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 lldb::addr_t ret =
1039 record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 if (log) {
1042 log->Printf(
1043 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1044 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1045 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1046 local_address, (uint64_t)record.m_host_address,
1047 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1048 record.m_process_address, record.m_process_address + record.m_size);
1049 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 return ret;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 return LLDB_INVALID_ADDRESS;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001056}
1057
1058IRExecutionUnit::AddrRange
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1060 for (AllocationRecord &record : m_records) {
1061 if (local_address >= record.m_host_address &&
1062 local_address < record.m_host_address + record.m_size) {
1063 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1064 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001065
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 return AddrRange(record.m_process_address, record.m_size);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 return AddrRange(0, 0);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001071}
1072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
Zachary Turner97206d52017-05-12 04:51:55 +00001074 Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 AllocationRecord &record) {
1076 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1077 return true;
1078 }
1079
1080 switch (record.m_sect_type) {
1081 case lldb::eSectionTypeInvalid:
1082 case lldb::eSectionTypeDWARFDebugAbbrev:
1083 case lldb::eSectionTypeDWARFDebugAddr:
1084 case lldb::eSectionTypeDWARFDebugAranges:
Tamas Berghammer963ce482017-08-25 13:56:14 +00001085 case lldb::eSectionTypeDWARFDebugCuIndex:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 case lldb::eSectionTypeDWARFDebugFrame:
1087 case lldb::eSectionTypeDWARFDebugInfo:
1088 case lldb::eSectionTypeDWARFDebugLine:
1089 case lldb::eSectionTypeDWARFDebugLoc:
1090 case lldb::eSectionTypeDWARFDebugMacInfo:
1091 case lldb::eSectionTypeDWARFDebugPubNames:
1092 case lldb::eSectionTypeDWARFDebugPubTypes:
1093 case lldb::eSectionTypeDWARFDebugRanges:
1094 case lldb::eSectionTypeDWARFDebugStr:
1095 case lldb::eSectionTypeDWARFDebugStrOffsets:
1096 case lldb::eSectionTypeDWARFAppleNames:
1097 case lldb::eSectionTypeDWARFAppleTypes:
1098 case lldb::eSectionTypeDWARFAppleNamespaces:
1099 case lldb::eSectionTypeDWARFAppleObjC:
Jan Kratochvile4777a92018-04-29 19:47:48 +00001100 case lldb::eSectionTypeDWARFGNUDebugAltLink:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 error.Clear();
1102 break;
1103 default:
1104 const bool zero_memory = false;
1105 record.m_process_address =
1106 Malloc(record.m_size, record.m_alignment, record.m_permissions,
1107 eAllocationPolicyProcessOnly, zero_memory, error);
1108 break;
1109 }
1110
1111 return error.Success();
1112}
1113
1114bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1115 bool ret = true;
1116
Zachary Turner97206d52017-05-12 04:51:55 +00001117 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118
1119 for (AllocationRecord &record : m_records) {
1120 ret = CommitOneAllocation(process_sp, err, record);
1121
1122 if (!ret) {
1123 break;
Sean Callananbd4dc692016-03-21 22:23:38 +00001124 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 }
1126
1127 if (!ret) {
1128 for (AllocationRecord &record : m_records) {
1129 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1130 Free(record.m_process_address, err);
1131 record.m_process_address = LLDB_INVALID_ADDRESS;
1132 }
Sean Callananbd4dc692016-03-21 22:23:38 +00001133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135
1136 return ret;
Sean Callananbd4dc692016-03-21 22:23:38 +00001137}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001138
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1140 m_reported_allocations = true;
Sean Callananbd4dc692016-03-21 22:23:38 +00001141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 for (AllocationRecord &record : m_records) {
1143 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1144 continue;
Sean Callananbd4dc692016-03-21 22:23:38 +00001145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 if (record.m_section_id == eSectionIDInvalid)
1147 continue;
1148
1149 engine.mapSectionAddress((void *)record.m_host_address,
1150 record.m_process_address);
1151 }
1152
1153 // Trigger re-application of relocations.
1154 engine.finalizeObject();
1155}
1156
1157bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1158 bool wrote_something = false;
1159 for (AllocationRecord &record : m_records) {
1160 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +00001161 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1163 record.m_size, err);
1164 if (err.Success())
1165 wrote_something = true;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001166 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 }
1168 return wrote_something;
1169}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1172 if (!log)
1173 return;
1174
1175 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1176 (unsigned long long)m_host_address, (unsigned long long)m_size,
1177 (unsigned long long)m_process_address, (unsigned)m_alignment,
1178 (unsigned)m_section_id, m_name.c_str());
1179}
1180
1181lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1182 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1183 return exe_ctx.GetByteOrder();
1184}
1185
1186uint32_t IRExecutionUnit::GetAddressByteSize() const {
1187 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1188 return exe_ctx.GetAddressByteSize();
1189}
1190
1191void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1192 lldb_private::Symtab &symtab) {
1193 // No symbols yet...
1194}
1195
1196void IRExecutionUnit::PopulateSectionList(
1197 lldb_private::ObjectFile *obj_file,
1198 lldb_private::SectionList &section_list) {
1199 for (AllocationRecord &record : m_records) {
1200 if (record.m_size > 0) {
1201 lldb::SectionSP section_sp(new lldb_private::Section(
1202 obj_file->GetModule(), obj_file, record.m_section_id,
1203 ConstString(record.m_name), record.m_sect_type,
1204 record.m_process_address, record.m_size,
1205 record.m_host_address, // file_offset (which is the host address for
1206 // the data)
1207 record.m_size, // file_size
1208 0,
1209 record.m_permissions)); // flags
1210 section_list.AddSection(section_sp);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001213}
1214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215bool IRExecutionUnit::GetArchitecture(lldb_private::ArchSpec &arch) {
1216 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1217 Target *target = exe_ctx.GetTargetPtr();
1218 if (target)
1219 arch = target->GetArchitecture();
1220 else
1221 arch.Clear();
1222 return arch.IsValid();
1223}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1226 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1227 Target *target = exe_ctx.GetTargetPtr();
1228 if (target) {
1229 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule(
1230 std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1231 shared_from_this()));
1232 if (jit_module_sp) {
1233 bool changed = false;
1234 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001235 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 return jit_module_sp;
1237 }
1238 return lldb::ModuleSP();
Greg Clayton23f8c952014-03-24 23:10:19 +00001239}