blob: 34a3488578cde87e2774179b857f9c05da7a4fce [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sean Callanan8dfb68e2013-03-19 00:10:07 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callanan8dfb68e2013-03-19 00:10:07 +00009#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan5deb06e2016-09-26 20:18:51 +000010#include "llvm/ExecutionEngine/ObjectCache.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000011#include "llvm/IR/Constants.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000012#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000013#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000014#include "llvm/Support/SourceMgr.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000015#include "llvm/Support/raw_ostream.h"
16
Jason Molendaaff1b352014-10-10 23:07:36 +000017#include "lldb/Core/Debugger.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000018#include "lldb/Core/Disassembler.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000019#include "lldb/Core/Module.h"
20#include "lldb/Core/Section.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "lldb/Expression/IRExecutionUnit.h"
Sean Callananb2814802016-02-12 21:11:25 +000022#include "lldb/Symbol/CompileUnit.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000023#include "lldb/Symbol/SymbolContext.h"
Sean Callananb2814802016-02-12 21:11:25 +000024#include "lldb/Symbol/SymbolFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000026#include "lldb/Target/ExecutionContext.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000027#include "lldb/Target/ObjCLanguageRuntime.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000028#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000029#include "lldb/Utility/DataBufferHeap.h"
30#include "lldb/Utility/DataExtractor.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000031#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000032#include "lldb/Utility/Log.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000033
Sean Callananb2814802016-02-12 21:11:25 +000034#include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner8e8d4b92018-05-23 23:56:09 +000035#include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h"
Sean Callananb2814802016-02-12 21:11:25 +000036
Sean Callanan8dfb68e2013-03-19 00:10:07 +000037using namespace lldb_private;
38
Jonas Devlieghered5b44032019-02-13 06:25:41 +000039IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up,
40 std::unique_ptr<llvm::Module> &module_up,
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 ConstString &name,
42 const lldb::TargetSP &target_sp,
43 const SymbolContext &sym_ctx,
44 std::vector<std::string> &cpu_features)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000045 : IRMemoryMap(target_sp), m_context_up(context_up.release()),
46 m_module_up(module_up.release()), m_module(m_module_up.get()),
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 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
Konrad Kleine248a1302019-05-23 11:14:47 +0000168 const char *plugin_name = nullptr;
169 const char *flavor_string = nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 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
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000215void IRExecutionUnit::ReportSymbolLookupError(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 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
Konrad Kleine248a1302019-05-23 11:14:47 +0000254 m_module->print(oss, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255
256 oss.flush();
257
258 log->Printf("Module being sent to JIT: \n%s", s.c_str());
259 }
260
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000261 m_module_up->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 &error);
263
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000264 llvm::EngineBuilder builder(std::move(m_module_up));
Saleem Abdulrasool29561272019-05-22 23:23:39 +0000265 llvm::Triple triple(m_module->getTargetTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266
267 builder.setEngineKind(llvm::EngineKind::JIT)
268 .setErrorStr(&error_string)
Saleem Abdulrasool29561272019-05-22 23:23:39 +0000269 .setRelocationModel(triple.isOSBinFormatMachO()
270 ? llvm::Reloc::PIC_
271 : llvm::Reloc::Static)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 .setMCJITMemoryManager(
273 std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
Pavel Labathd7396362017-11-13 14:03:17 +0000274 .setOptLevel(llvm::CodeGenOpt::Less);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275
276 llvm::StringRef mArch;
277 llvm::StringRef mCPU;
278 llvm::SmallVector<std::string, 0> mAttrs;
279
280 for (std::string &feature : m_cpu_features)
281 mAttrs.push_back(feature);
282
283 llvm::TargetMachine *target_machine =
284 builder.selectTarget(triple, mArch, mCPU, mAttrs);
285
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000286 m_execution_engine_up.reset(builder.create(target_machine));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287
288 m_strip_underscore =
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000289 (m_execution_engine_up->getDataLayout().getGlobalPrefix() == '_');
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000291 if (!m_execution_engine_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 error.SetErrorToGenericError();
293 error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
294 error_string.c_str());
295 return;
296 }
297
Sean Callanan5deb06e2016-09-26 20:18:51 +0000298 class ObjectDumper : public llvm::ObjectCache {
299 public:
300 void notifyObjectCompiled(const llvm::Module *module,
301 llvm::MemoryBufferRef object) override {
302 int fd = 0;
303 llvm::SmallVector<char, 256> result_path;
304 std::string object_name_model =
305 "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
306 (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
307 llvm::raw_fd_ostream fds(fd, true);
308 fds.write(object.getBufferStart(), object.getBufferSize());
309 }
310
311 std::unique_ptr<llvm::MemoryBuffer>
312 getObject(const llvm::Module *module) override {
313 // Return nothing - we're just abusing the object-cache mechanism to dump
314 // objects.
315 return nullptr;
316 }
317 };
318
319 if (process_sp->GetTarget().GetEnableSaveObjects()) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000320 m_object_cache_up = llvm::make_unique<ObjectDumper>();
321 m_execution_engine_up->setObjectCache(m_object_cache_up.get());
Sean Callanan5deb06e2016-09-26 20:18:51 +0000322 }
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 // Make sure we see all sections, including ones that don't have
325 // relocations...
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000326 m_execution_engine_up->setProcessAllSections(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000328 m_execution_engine_up->DisableLazyCompilation();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329
330 for (llvm::Function &function : *m_module) {
331 if (function.isDeclaration() || function.hasPrivateLinkage())
332 continue;
333
334 const bool external =
335 function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
336
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000337 void *fun_ptr = m_execution_engine_up->getPointerToFunction(&function);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338
339 if (!error.Success()) {
340 // We got an error through our callback!
341 return;
Greg Clayton23f8c952014-03-24 23:10:19 +0000342 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 if (!fun_ptr) {
345 error.SetErrorToGenericError();
346 error.SetErrorStringWithFormat(
347 "'%s' was in the JITted module but wasn't lowered",
348 function.getName().str().c_str());
349 return;
350 }
351 m_jitted_functions.push_back(JittedFunction(
352 function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr));
353 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355 CommitAllocations(process_sp);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000356 ReportAllocations(*m_execution_engine_up);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 // We have to do this after calling ReportAllocations because for the MCJIT,
Adrian Prantl05097242018-04-30 16:49:04 +0000359 // getGlobalValueAddress will cause the JIT to perform all relocations. That
360 // can only be done once, and has to happen after we do the remapping from
361 // local -> remote. That means we don't know the local address of the
362 // Variables, but we don't need that for anything, so that's okay.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
365 llvm::GlobalValue &val) {
366 if (val.hasExternalLinkage() && !val.isDeclaration()) {
367 uint64_t var_ptr_addr =
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000368 m_execution_engine_up->getGlobalValueAddress(val.getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 // This is a really unfortunae API that sometimes returns local addresses
Adrian Prantl05097242018-04-30 16:49:04 +0000373 // and sometimes returns remote addresses, based on whether the variable
374 // was relocated during ReportAllocations or not.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 if (remote_addr == LLDB_INVALID_ADDRESS) {
377 remote_addr = var_ptr_addr;
378 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 if (var_ptr_addr != 0)
381 m_jitted_global_variables.push_back(JittedGlobalVariable(
382 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
383 }
384 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) {
387 RegisterOneValue(global_var);
388 }
389
390 for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
391 RegisterOneValue(global_alias);
392 }
393
394 WriteData(process_sp);
395
396 if (m_failed_lookups.size()) {
397 StreamString ss;
398
399 ss.PutCString("Couldn't lookup symbols:\n");
400
401 bool emitNewLine = false;
402
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000403 for (ConstString failed_lookup : m_failed_lookups) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 if (emitNewLine)
405 ss.PutCString("\n");
406 emitNewLine = true;
407 ss.PutCString(" ");
408 ss.PutCString(Mangled(failed_lookup)
409 .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus)
410 .AsCString());
411 }
412
413 m_failed_lookups.clear();
414
Zachary Turnerc1564272016-11-16 21:15:24 +0000415 error.SetErrorString(ss.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416
417 return;
418 }
419
420 m_function_load_addr = LLDB_INVALID_ADDRESS;
421 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
422
423 for (JittedFunction &jitted_function : m_jitted_functions) {
424 jitted_function.m_remote_addr =
425 GetRemoteAddressForLocal(jitted_function.m_local_addr);
426
427 if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
428 AddrRange func_range =
429 GetRemoteRangeForLocal(jitted_function.m_local_addr);
430 m_function_end_load_addr = func_range.first + func_range.second;
431 m_function_load_addr = jitted_function.m_remote_addr;
432 }
433 }
434
435 if (log) {
436 log->Printf("Code can be run in the target.");
437
438 StreamString disassembly_stream;
439
Zachary Turner97206d52017-05-12 04:51:55 +0000440 Status err = DisassembleFunction(disassembly_stream, process_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441
442 if (!err.Success()) {
443 log->Printf("Couldn't disassemble function : %s",
444 err.AsCString("unknown error"));
445 } else {
446 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
447 }
448
449 log->Printf("Sections: ");
450 for (AllocationRecord &record : m_records) {
451 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
452 record.dump(log);
453
454 DataBufferHeap my_buffer(record.m_size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +0000455 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 ReadMemory(my_buffer.GetBytes(), record.m_process_address,
457 record.m_size, err);
458
459 if (err.Success()) {
460 DataExtractor my_extractor(my_buffer.GetBytes(),
461 my_buffer.GetByteSize(),
462 lldb::eByteOrderBig, 8);
463 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
464 record.m_process_address, 16,
465 DataExtractor::TypeUInt8);
Greg Clayton23f8c952014-03-24 23:10:19 +0000466 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 } else {
468 record.dump(log);
469
470 DataExtractor my_extractor((const void *)record.m_host_address,
471 record.m_size, lldb::eByteOrderBig, 8);
472 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
473 DataExtractor::TypeUInt8);
474 }
475 }
476 }
477
478 func_addr = m_function_load_addr;
479 func_end = m_function_end_load_addr;
480
481 return;
482}
483
484IRExecutionUnit::~IRExecutionUnit() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000485 m_module_up.reset();
486 m_execution_engine_up.reset();
487 m_context_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488}
489
490IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000491 : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492
493IRExecutionUnit::MemoryManager::~MemoryManager() {}
494
495lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
496 const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
497 lldb::SectionType sect_type = lldb::eSectionTypeCode;
498 switch (alloc_kind) {
499 case AllocationKind::Stub:
500 sect_type = lldb::eSectionTypeCode;
501 break;
502 case AllocationKind::Code:
503 sect_type = lldb::eSectionTypeCode;
504 break;
505 case AllocationKind::Data:
506 sect_type = lldb::eSectionTypeData;
507 break;
508 case AllocationKind::Global:
509 sect_type = lldb::eSectionTypeData;
510 break;
511 case AllocationKind::Bytes:
512 sect_type = lldb::eSectionTypeOther;
513 break;
514 }
515
516 if (!name.empty()) {
517 if (name.equals("__text") || name.equals(".text"))
518 sect_type = lldb::eSectionTypeCode;
519 else if (name.equals("__data") || name.equals(".data"))
520 sect_type = lldb::eSectionTypeCode;
521 else if (name.startswith("__debug_") || name.startswith(".debug_")) {
522 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
523 llvm::StringRef dwarf_name(name.substr(name_idx));
524 switch (dwarf_name[0]) {
525 case 'a':
526 if (dwarf_name.equals("abbrev"))
527 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
528 else if (dwarf_name.equals("aranges"))
529 sect_type = lldb::eSectionTypeDWARFDebugAranges;
530 else if (dwarf_name.equals("addr"))
531 sect_type = lldb::eSectionTypeDWARFDebugAddr;
532 break;
533
534 case 'f':
535 if (dwarf_name.equals("frame"))
536 sect_type = lldb::eSectionTypeDWARFDebugFrame;
537 break;
538
539 case 'i':
540 if (dwarf_name.equals("info"))
541 sect_type = lldb::eSectionTypeDWARFDebugInfo;
542 break;
543
544 case 'l':
545 if (dwarf_name.equals("line"))
546 sect_type = lldb::eSectionTypeDWARFDebugLine;
547 else if (dwarf_name.equals("loc"))
548 sect_type = lldb::eSectionTypeDWARFDebugLoc;
George Rimare4dee262018-10-23 09:46:15 +0000549 else if (dwarf_name.equals("loclists"))
550 sect_type = lldb::eSectionTypeDWARFDebugLocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 break;
552
553 case 'm':
554 if (dwarf_name.equals("macinfo"))
555 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
556 break;
557
558 case 'p':
559 if (dwarf_name.equals("pubnames"))
560 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
561 else if (dwarf_name.equals("pubtypes"))
562 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
563 break;
564
565 case 's':
566 if (dwarf_name.equals("str"))
567 sect_type = lldb::eSectionTypeDWARFDebugStr;
568 else if (dwarf_name.equals("str_offsets"))
569 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
570 break;
571
572 case 'r':
573 if (dwarf_name.equals("ranges"))
574 sect_type = lldb::eSectionTypeDWARFDebugRanges;
575 break;
576
577 default:
578 break;
579 }
Davide Italiano45925d72018-01-04 23:37:18 +0000580 } else if (name.startswith("__apple_") || name.startswith(".apple_"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 sect_type = lldb::eSectionTypeInvalid;
Davide Italiano45925d72018-01-04 23:37:18 +0000582 else if (name.equals("__objc_imageinfo"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 sect_type = lldb::eSectionTypeOther;
584 }
585 return sect_type;
Greg Clayton23f8c952014-03-24 23:10:19 +0000586}
587
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
589 uintptr_t Size, unsigned Alignment, unsigned SectionID,
590 llvm::StringRef SectionName) {
591 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000593 uint8_t *return_value = m_default_mm_up->allocateCodeSection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000595
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 m_parent.m_records.push_back(AllocationRecord(
597 (uintptr_t)return_value,
598 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
599 GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
600 Alignment, SectionID, SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 if (log) {
603 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
604 ", Alignment=%u, SectionID=%u) = %p",
605 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
606 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000609 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 lldb::ProcessSP process_sp =
611 m_parent.GetBestExecutionContextScope()->CalculateProcess();
612
613 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
614 }
615
616 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000617}
618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
620 uintptr_t Size, unsigned Alignment, unsigned SectionID,
621 llvm::StringRef SectionName, bool IsReadOnly) {
622 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000623
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000624 uint8_t *return_value = m_default_mm_up->allocateDataSection(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 uint32_t permissions = lldb::ePermissionsReadable;
628 if (!IsReadOnly)
629 permissions |= lldb::ePermissionsWritable;
630 m_parent.m_records.push_back(AllocationRecord(
631 (uintptr_t)return_value, permissions,
632 GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
633 Alignment, SectionID, SectionName.str().c_str()));
634 if (log) {
635 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
636 ", Alignment=%u, SectionID=%u) = %p",
637 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
638 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000639
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 if (m_parent.m_reported_allocations) {
Zachary Turner97206d52017-05-12 04:51:55 +0000641 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 lldb::ProcessSP process_sp =
643 m_parent.GetBestExecutionContextScope()->CalculateProcess();
644
645 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
646 }
647
648 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000649}
650
Sean Callananb2814802016-02-12 21:11:25 +0000651static ConstString
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000652FindBestAlternateMangledName(ConstString demangled,
Sean Callananb2814802016-02-12 21:11:25 +0000653 const lldb::LanguageType &lang_type,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 const SymbolContext &sym_ctx) {
655 CPlusPlusLanguage::MethodName cpp_name(demangled);
656 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
Sean Callananb2814802016-02-12 21:11:25 +0000657
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 if (!scope_qualified_name.size())
659 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 if (!sym_ctx.module_sp)
662 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000663
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
665 if (!sym_vendor)
666 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000667
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
669 if (!sym_file)
670 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672 std::vector<ConstString> alternates;
673 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
Sean Callananb2814802016-02-12 21:11:25 +0000674
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 std::vector<ConstString> param_and_qual_matches;
676 std::vector<ConstString> param_matches;
677 for (size_t i = 0; i < alternates.size(); i++) {
678 ConstString alternate_mangled_name = alternates[i];
679 Mangled mangled(alternate_mangled_name, true);
680 ConstString demangled = mangled.GetDemangledName(lang_type);
Sean Callananb2814802016-02-12 21:11:25 +0000681
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
683 if (!cpp_name.IsValid())
684 continue;
Sean Callananb2814802016-02-12 21:11:25 +0000685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
687 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
688 param_and_qual_matches.push_back(alternate_mangled_name);
689 else
690 param_matches.push_back(alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000691 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 }
Sean Callananb2814802016-02-12 21:11:25 +0000693
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 if (param_and_qual_matches.size())
695 return param_and_qual_matches[0]; // It is assumed that there will be only
696 // one!
697 else if (param_matches.size())
698 return param_matches[0]; // Return one of them as a best match
699 else
700 return ConstString();
Sean Callananb2814802016-02-12 21:11:25 +0000701}
702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703struct IRExecutionUnit::SearchSpec {
704 ConstString name;
Zachary Turner117b1fa2018-10-25 20:45:40 +0000705 lldb::FunctionNameType mask;
Sean Callananb2814802016-02-12 21:11:25 +0000706
Zachary Turner117b1fa2018-10-25 20:45:40 +0000707 SearchSpec(ConstString n,
708 lldb::FunctionNameType m = lldb::eFunctionNameTypeFull)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 : name(n), mask(m) {}
Sean Callananb2814802016-02-12 21:11:25 +0000710};
711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712void IRExecutionUnit::CollectCandidateCNames(
713 std::vector<IRExecutionUnit::SearchSpec> &C_specs,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000714 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 if (m_strip_underscore && name.AsCString()[0] == '_')
716 C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
717 C_specs.push_back(SearchSpec(name));
Sean Callananb2814802016-02-12 21:11:25 +0000718}
719
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720void IRExecutionUnit::CollectCandidateCPlusPlusNames(
721 std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
722 const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
723 for (const SearchSpec &C_spec : C_specs) {
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000724 ConstString name = C_spec.name;
Sean Callananb2814802016-02-12 21:11:25 +0000725
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
727 Mangled mangled(name, true);
728 ConstString demangled =
729 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 if (demangled) {
732 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
733 demangled, lldb::eLanguageTypeC_plus_plus, sc);
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 if (best_alternate_mangled_name) {
736 CPP_specs.push_back(best_alternate_mangled_name);
Sean Callananb2814802016-02-12 21:11:25 +0000737 }
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000738
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
740 }
Sean Callananb2814802016-02-12 21:11:25 +0000741 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742
Luke Drummondf5bb1d62016-12-19 17:22:44 +0000743 std::set<ConstString> alternates;
744 CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
745 CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 }
Sean Callananb2814802016-02-12 21:11:25 +0000747}
748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749void IRExecutionUnit::CollectFallbackNames(
750 std::vector<SearchSpec> &fallback_specs,
751 const std::vector<SearchSpec> &C_specs) {
752 // As a last-ditch fallback, try the base name for C++ names. It's terrible,
753 // but the DWARF doesn't always encode "extern C" correctly.
754
755 for (const SearchSpec &C_spec : C_specs) {
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000756 ConstString name = C_spec.name;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757
758 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
759 Mangled mangled_name(name);
760 ConstString demangled_name =
761 mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
762 if (!demangled_name.IsEmpty()) {
763 const char *demangled_cstr = demangled_name.AsCString();
764 const char *lparen_loc = strchr(demangled_cstr, '(');
765 if (lparen_loc) {
766 llvm::StringRef base_name(demangled_cstr,
767 lparen_loc - demangled_cstr);
768 fallback_specs.push_back(ConstString(base_name));
Sean Callanan34ab28a2016-06-02 17:59:47 +0000769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 }
Sean Callanan34ab28a2016-06-02 17:59:47 +0000773}
774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775lldb::addr_t IRExecutionUnit::FindInSymbols(
776 const std::vector<IRExecutionUnit::SearchSpec> &specs,
777 const lldb_private::SymbolContext &sc) {
778 Target *target = sc.target_sp.get();
Sean Callanan34ab28a2016-06-02 17:59:47 +0000779
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 if (!target) {
781 // we shouldn't be doing any symbol lookup at all without a target
782 return LLDB_INVALID_ADDRESS;
783 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000784
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 for (const SearchSpec &spec : specs) {
786 SymbolContextList sc_list;
Sean Callananfed0e7582016-02-23 23:09:06 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000789
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 std::function<bool(lldb::addr_t &, SymbolContextList &,
791 const lldb_private::SymbolContext &)>
792 get_external_load_address = [&best_internal_load_address, target](
793 lldb::addr_t &load_address, SymbolContextList &sc_list,
794 const lldb_private::SymbolContext &sc) -> lldb::addr_t {
795 load_address = LLDB_INVALID_ADDRESS;
Sean Callananfed0e7582016-02-23 23:09:06 +0000796
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
798 SymbolContext candidate_sc;
799
800 sc_list.GetContextAtIndex(si, candidate_sc);
801
802 const bool is_external =
803 (candidate_sc.function) ||
804 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
805 if (candidate_sc.symbol) {
806 load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
807
808 if (load_address == LLDB_INVALID_ADDRESS) {
809 if (target->GetProcessSP())
810 load_address =
811 candidate_sc.symbol->GetAddress().GetLoadAddress(target);
812 else
813 load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
814 }
815 }
816
817 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
818 if (target->GetProcessSP())
819 load_address = candidate_sc.function->GetAddressRange()
820 .GetBaseAddress()
821 .GetLoadAddress(target);
822 else
823 load_address = candidate_sc.function->GetAddressRange()
824 .GetBaseAddress()
825 .GetFileAddress();
826 }
827
828 if (load_address != LLDB_INVALID_ADDRESS) {
829 if (is_external) {
830 return true;
831 } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
832 best_internal_load_address = load_address;
Sean Callananfed0e7582016-02-23 23:09:06 +0000833 load_address = LLDB_INVALID_ADDRESS;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 }
835 }
836 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 return false;
839 };
Sean Callananfed0e7582016-02-23 23:09:06 +0000840
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 if (sc.module_sp) {
Konrad Kleine248a1302019-05-23 11:14:47 +0000842 sc.module_sp->FindFunctions(spec.name, nullptr, spec.mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 true, // include_symbols
844 false, // include_inlines
845 true, // append
846 sc_list);
847 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000848
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
Ted Woodwardeee554d2016-03-09 22:05:17 +0000850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 if (get_external_load_address(load_address, sc_list, sc)) {
852 return load_address;
853 } else {
854 sc_list.Clear();
855 }
856
857 if (sc_list.GetSize() == 0 && sc.target_sp) {
858 sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
859 true, // include_symbols
860 false, // include_inlines
861 true, // append
862 sc_list);
863 }
864
865 if (get_external_load_address(load_address, sc_list, sc)) {
866 return load_address;
867 } else {
868 sc_list.Clear();
869 }
870
871 if (sc_list.GetSize() == 0 && sc.target_sp) {
872 sc.target_sp->GetImages().FindSymbolsWithNameAndType(
873 spec.name, lldb::eSymbolTypeAny, sc_list);
874 }
875
876 if (get_external_load_address(load_address, sc_list, sc)) {
877 return load_address;
878 }
Adrian Prantl05097242018-04-30 16:49:04 +0000879 // if there are any searches we try after this, add an sc_list.Clear() in
880 // an "else" clause here
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881
882 if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
883 return best_internal_load_address;
884 }
885 }
886
887 return LLDB_INVALID_ADDRESS;
888}
889
890lldb::addr_t
891IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
892 const lldb_private::SymbolContext &sc) {
893 lldb::TargetSP target_sp = sc.target_sp;
894
895 if (!target_sp) {
896 return LLDB_INVALID_ADDRESS;
897 }
898
899 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
900
901 if (!process_sp) {
902 return LLDB_INVALID_ADDRESS;
903 }
904
905 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
906
907 if (runtime) {
908 for (const SearchSpec &spec : specs) {
909 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
910
911 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
912 return symbol_load_addr;
913 }
914 }
915
916 return LLDB_INVALID_ADDRESS;
917}
918
919lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
920 const std::vector<SearchSpec> &specs,
921 const lldb_private::SymbolContext &sc) {
922 lldb::TargetSP target_sp = sc.target_sp;
923
924 for (const SearchSpec &spec : specs) {
925 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
926
927 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
928 return symbol_load_addr;
929 }
930
931 return LLDB_INVALID_ADDRESS;
932}
933
934lldb::addr_t
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000935IRExecutionUnit::FindSymbol(lldb_private::ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 std::vector<SearchSpec> candidate_C_names;
937 std::vector<SearchSpec> candidate_CPlusPlus_names;
938
939 CollectCandidateCNames(candidate_C_names, name);
940
941 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
942 if (ret == LLDB_INVALID_ADDRESS)
943 ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
944
945 if (ret == LLDB_INVALID_ADDRESS)
946 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
947
948 if (ret == LLDB_INVALID_ADDRESS) {
949 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
950 m_sym_ctx);
951 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
952 }
953
954 if (ret == LLDB_INVALID_ADDRESS) {
955 std::vector<SearchSpec> candidate_fallback_names;
956
957 CollectFallbackNames(candidate_fallback_names, candidate_C_names);
958 ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
959 }
960
961 return ret;
962}
963
964void IRExecutionUnit::GetStaticInitializers(
965 std::vector<lldb::addr_t> &static_initializers) {
966 if (llvm::GlobalVariable *global_ctors =
967 m_module->getNamedGlobal("llvm.global_ctors")) {
968 if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
969 global_ctors->getInitializer())) {
970 for (llvm::Use &ctor_use : ctor_array->operands()) {
971 if (llvm::ConstantStruct *ctor_struct =
972 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
973 lldbassert(ctor_struct->getNumOperands() ==
974 3); // this is standardized
975 if (llvm::Function *ctor_function =
976 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 ConstString ctor_function_name_cs(ctor_function->getName().str());
978
979 for (JittedFunction &jitted_function : m_jitted_functions) {
980 if (ctor_function_name_cs == jitted_function.m_name) {
981 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
982 static_initializers.push_back(jitted_function.m_remote_addr);
Ted Woodwardeee554d2016-03-09 22:05:17 +0000983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 break;
985 }
Sean Callananfed0e7582016-02-23 23:09:06 +0000986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 }
Sean Callananb2814802016-02-12 21:11:25 +0000988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989 }
Sean Callananb2814802016-02-12 21:11:25 +0000990 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 }
Sean Callananbd4dc692016-03-21 22:23:38 +0000992}
993
Jim Ingham27e5fe82015-01-27 18:03:05 +0000994uint64_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
996 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Greg Claytond6171a72015-07-02 16:43:49 +0000997
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 ConstString name_cs(Name.c_str());
Chaoren Lin74a1fc62016-02-24 03:15:21 +0000999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 lldb::addr_t ret = m_parent.FindSymbol(name_cs);
Chaoren Lin74a1fc62016-02-24 03:15:21 +00001001
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 if (ret == LLDB_INVALID_ADDRESS) {
1003 if (log)
1004 log->Printf(
1005 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1006 Name.c_str());
Sean Callananb2814802016-02-12 21:11:25 +00001007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 m_parent.ReportSymbolLookupError(name_cs);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001009 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 } else {
1011 if (log)
1012 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1013 Name.c_str(), ret);
1014 return ret;
1015 }
Jim Ingham27e5fe82015-01-27 18:03:05 +00001016}
1017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1019 const std::string &Name, bool AbortOnFailure) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020 return (void *)getSymbolAddress(Name);
Jim Ingham27e5fe82015-01-27 18:03:05 +00001021}
1022
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001023lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1025 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Sean Callananffae9442013-06-27 01:42:47 +00001026
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 for (AllocationRecord &record : m_records) {
1028 if (local_address >= record.m_host_address &&
1029 local_address < record.m_host_address + record.m_size) {
1030 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1031 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 lldb::addr_t ret =
1034 record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 if (log) {
1037 log->Printf(
1038 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1039 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1040 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1041 local_address, (uint64_t)record.m_host_address,
1042 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1043 record.m_process_address, record.m_process_address + record.m_size);
1044 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001045
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 return ret;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 return LLDB_INVALID_ADDRESS;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001051}
1052
1053IRExecutionUnit::AddrRange
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1055 for (AllocationRecord &record : m_records) {
1056 if (local_address >= record.m_host_address &&
1057 local_address < record.m_host_address + record.m_size) {
1058 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1059 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 return AddrRange(record.m_process_address, record.m_size);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 return AddrRange(0, 0);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001066}
1067
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
Zachary Turner97206d52017-05-12 04:51:55 +00001069 Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 AllocationRecord &record) {
1071 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1072 return true;
1073 }
1074
1075 switch (record.m_sect_type) {
1076 case lldb::eSectionTypeInvalid:
1077 case lldb::eSectionTypeDWARFDebugAbbrev:
1078 case lldb::eSectionTypeDWARFDebugAddr:
1079 case lldb::eSectionTypeDWARFDebugAranges:
Tamas Berghammer963ce482017-08-25 13:56:14 +00001080 case lldb::eSectionTypeDWARFDebugCuIndex:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 case lldb::eSectionTypeDWARFDebugFrame:
1082 case lldb::eSectionTypeDWARFDebugInfo:
1083 case lldb::eSectionTypeDWARFDebugLine:
1084 case lldb::eSectionTypeDWARFDebugLoc:
George Rimare4dee262018-10-23 09:46:15 +00001085 case lldb::eSectionTypeDWARFDebugLocLists:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 case lldb::eSectionTypeDWARFDebugMacInfo:
1087 case lldb::eSectionTypeDWARFDebugPubNames:
1088 case lldb::eSectionTypeDWARFDebugPubTypes:
1089 case lldb::eSectionTypeDWARFDebugRanges:
1090 case lldb::eSectionTypeDWARFDebugStr:
1091 case lldb::eSectionTypeDWARFDebugStrOffsets:
1092 case lldb::eSectionTypeDWARFAppleNames:
1093 case lldb::eSectionTypeDWARFAppleTypes:
1094 case lldb::eSectionTypeDWARFAppleNamespaces:
1095 case lldb::eSectionTypeDWARFAppleObjC:
Jan Kratochvile4777a92018-04-29 19:47:48 +00001096 case lldb::eSectionTypeDWARFGNUDebugAltLink:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 error.Clear();
1098 break;
1099 default:
1100 const bool zero_memory = false;
1101 record.m_process_address =
1102 Malloc(record.m_size, record.m_alignment, record.m_permissions,
1103 eAllocationPolicyProcessOnly, zero_memory, error);
1104 break;
1105 }
1106
1107 return error.Success();
1108}
1109
1110bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1111 bool ret = true;
1112
Zachary Turner97206d52017-05-12 04:51:55 +00001113 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114
1115 for (AllocationRecord &record : m_records) {
1116 ret = CommitOneAllocation(process_sp, err, record);
1117
1118 if (!ret) {
1119 break;
Sean Callananbd4dc692016-03-21 22:23:38 +00001120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 }
1122
1123 if (!ret) {
1124 for (AllocationRecord &record : m_records) {
1125 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1126 Free(record.m_process_address, err);
1127 record.m_process_address = LLDB_INVALID_ADDRESS;
1128 }
Sean Callananbd4dc692016-03-21 22:23:38 +00001129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 }
1131
1132 return ret;
Sean Callananbd4dc692016-03-21 22:23:38 +00001133}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001134
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1136 m_reported_allocations = true;
Sean Callananbd4dc692016-03-21 22:23:38 +00001137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 for (AllocationRecord &record : m_records) {
1139 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1140 continue;
Sean Callananbd4dc692016-03-21 22:23:38 +00001141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 if (record.m_section_id == eSectionIDInvalid)
1143 continue;
1144
1145 engine.mapSectionAddress((void *)record.m_host_address,
1146 record.m_process_address);
1147 }
1148
1149 // Trigger re-application of relocations.
1150 engine.finalizeObject();
1151}
1152
1153bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1154 bool wrote_something = false;
1155 for (AllocationRecord &record : m_records) {
1156 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
Zachary Turner97206d52017-05-12 04:51:55 +00001157 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1159 record.m_size, err);
1160 if (err.Success())
1161 wrote_something = true;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 }
1164 return wrote_something;
1165}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1168 if (!log)
1169 return;
1170
1171 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1172 (unsigned long long)m_host_address, (unsigned long long)m_size,
1173 (unsigned long long)m_process_address, (unsigned)m_alignment,
1174 (unsigned)m_section_id, m_name.c_str());
1175}
1176
1177lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1178 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1179 return exe_ctx.GetByteOrder();
1180}
1181
1182uint32_t IRExecutionUnit::GetAddressByteSize() const {
1183 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1184 return exe_ctx.GetAddressByteSize();
1185}
1186
1187void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1188 lldb_private::Symtab &symtab) {
1189 // No symbols yet...
1190}
1191
1192void IRExecutionUnit::PopulateSectionList(
1193 lldb_private::ObjectFile *obj_file,
1194 lldb_private::SectionList &section_list) {
1195 for (AllocationRecord &record : m_records) {
1196 if (record.m_size > 0) {
1197 lldb::SectionSP section_sp(new lldb_private::Section(
1198 obj_file->GetModule(), obj_file, record.m_section_id,
1199 ConstString(record.m_name), record.m_sect_type,
1200 record.m_process_address, record.m_size,
1201 record.m_host_address, // file_offset (which is the host address for
1202 // the data)
1203 record.m_size, // file_size
1204 0,
1205 record.m_permissions)); // flags
1206 section_list.AddSection(section_sp);
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001207 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001209}
1210
Pavel Labathf760f5a2019-01-03 10:37:19 +00001211ArchSpec IRExecutionUnit::GetArchitecture() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 ExecutionContext exe_ctx(GetBestExecutionContextScope());
Pavel Labathf760f5a2019-01-03 10:37:19 +00001213 if(Target *target = exe_ctx.GetTargetPtr())
1214 return target->GetArchitecture();
1215 return ArchSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216}
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001217
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1219 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1220 Target *target = exe_ctx.GetTargetPtr();
Zachary Turner8e8d4b92018-05-23 23:56:09 +00001221 if (!target)
1222 return nullptr;
1223
1224 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1225 shared_from_this());
1226
1227 lldb::ModuleSP jit_module_sp =
1228 lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1229 if (!jit_module_sp)
1230 return nullptr;
1231
1232 bool changed = false;
1233 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1234 return jit_module_sp;
Greg Clayton23f8c952014-03-24 23:10:19 +00001235}