1cfca06d7SDimitry Andric //===-- IRExecutionUnit.cpp -----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "llvm/ExecutionEngine/ExecutionEngine.h"
1014f1b3e8SDimitry Andric #include "llvm/ExecutionEngine/ObjectCache.h"
11f3fbd1c0SDimitry Andric #include "llvm/IR/Constants.h"
12344a3780SDimitry Andric #include "llvm/IR/DiagnosticHandler.h"
13344a3780SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
14f034231aSEd Maste #include "llvm/IR/LLVMContext.h"
15f034231aSEd Maste #include "llvm/IR/Module.h"
16f034231aSEd Maste #include "llvm/Support/SourceMgr.h"
175e95aa85SEd Maste #include "llvm/Support/raw_ostream.h"
185e95aa85SEd Maste
19205afe67SEd Maste #include "lldb/Core/Debugger.h"
20f034231aSEd Maste #include "lldb/Core/Disassembler.h"
210cac4ca3SEd Maste #include "lldb/Core/Module.h"
220cac4ca3SEd Maste #include "lldb/Core/Section.h"
2314f1b3e8SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
247fa27ce4SDimitry Andric #include "lldb/Expression/ObjectFileJIT.h"
25145449b1SDimitry Andric #include "lldb/Host/HostInfo.h"
26f3fbd1c0SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
275e95aa85SEd Maste #include "lldb/Symbol/SymbolContext.h"
28f3fbd1c0SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
2914f1b3e8SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
30f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
31c0981da4SDimitry Andric #include "lldb/Target/Language.h"
325f29bb8aSDimitry Andric #include "lldb/Target/LanguageRuntime.h"
3314f1b3e8SDimitry Andric #include "lldb/Target/Target.h"
3474a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
3574a628f7SDimitry Andric #include "lldb/Utility/DataExtractor.h"
36f3fbd1c0SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
37145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
3874a628f7SDimitry Andric #include "lldb/Utility/Log.h"
39f3fbd1c0SDimitry Andric
40e3b55780SDimitry Andric #include <optional>
41f034231aSEd Maste
42f034231aSEd Maste using namespace lldb_private;
43f034231aSEd Maste
IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> & context_up,std::unique_ptr<llvm::Module> & module_up,ConstString & name,const lldb::TargetSP & target_sp,const SymbolContext & sym_ctx,std::vector<std::string> & cpu_features)445f29bb8aSDimitry Andric IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up,
455f29bb8aSDimitry Andric std::unique_ptr<llvm::Module> &module_up,
46f034231aSEd Maste ConstString &name,
47f034231aSEd Maste const lldb::TargetSP &target_sp,
48f3fbd1c0SDimitry Andric const SymbolContext &sym_ctx,
4914f1b3e8SDimitry Andric std::vector<std::string> &cpu_features)
505f29bb8aSDimitry Andric : IRMemoryMap(target_sp), m_context_up(context_up.release()),
515f29bb8aSDimitry Andric m_module_up(module_up.release()), m_module(m_module_up.get()),
5214f1b3e8SDimitry Andric m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
5314f1b3e8SDimitry Andric m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
54f3fbd1c0SDimitry Andric m_function_end_load_addr(LLDB_INVALID_ADDRESS),
5514f1b3e8SDimitry Andric m_reported_allocations(false) {}
56f034231aSEd Maste
WriteNow(const uint8_t * bytes,size_t size,Status & error)5714f1b3e8SDimitry Andric lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
58b76161e4SDimitry Andric Status &error) {
59e81d9d49SDimitry Andric const bool zero_memory = false;
6014f1b3e8SDimitry Andric lldb::addr_t allocation_process_addr =
6114f1b3e8SDimitry Andric Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable,
6214f1b3e8SDimitry Andric eAllocationPolicyMirror, zero_memory, error);
63f034231aSEd Maste
64f034231aSEd Maste if (!error.Success())
65f034231aSEd Maste return LLDB_INVALID_ADDRESS;
66f034231aSEd Maste
67f034231aSEd Maste WriteMemory(allocation_process_addr, bytes, size, error);
68f034231aSEd Maste
6914f1b3e8SDimitry Andric if (!error.Success()) {
70b76161e4SDimitry Andric Status err;
71f034231aSEd Maste Free(allocation_process_addr, err);
72f034231aSEd Maste
73f034231aSEd Maste return LLDB_INVALID_ADDRESS;
74f034231aSEd Maste }
75f034231aSEd Maste
76145449b1SDimitry Andric if (Log *log = GetLog(LLDBLog::Expressions)) {
77f034231aSEd Maste DataBufferHeap my_buffer(size, 0);
78b76161e4SDimitry Andric Status err;
79f034231aSEd Maste ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
80f034231aSEd Maste
8114f1b3e8SDimitry Andric if (err.Success()) {
8214f1b3e8SDimitry Andric DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(),
8314f1b3e8SDimitry Andric lldb::eByteOrderBig, 8);
8414f1b3e8SDimitry Andric my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
8514f1b3e8SDimitry Andric allocation_process_addr, 16,
8614f1b3e8SDimitry Andric DataExtractor::TypeUInt8);
87f034231aSEd Maste }
88f034231aSEd Maste }
89f034231aSEd Maste
90f034231aSEd Maste return allocation_process_addr;
91f034231aSEd Maste }
92f034231aSEd Maste
FreeNow(lldb::addr_t allocation)9314f1b3e8SDimitry Andric void IRExecutionUnit::FreeNow(lldb::addr_t allocation) {
94f034231aSEd Maste if (allocation == LLDB_INVALID_ADDRESS)
95f034231aSEd Maste return;
96f034231aSEd Maste
97b76161e4SDimitry Andric Status err;
98f034231aSEd Maste
99f034231aSEd Maste Free(allocation, err);
100f034231aSEd Maste }
101f034231aSEd Maste
DisassembleFunction(Stream & stream,lldb::ProcessSP & process_wp)102b76161e4SDimitry Andric Status IRExecutionUnit::DisassembleFunction(Stream &stream,
10314f1b3e8SDimitry Andric lldb::ProcessSP &process_wp) {
104145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
105f034231aSEd Maste
106f034231aSEd Maste ExecutionContext exe_ctx(process_wp);
107f034231aSEd Maste
108b76161e4SDimitry Andric Status ret;
109f034231aSEd Maste
110f034231aSEd Maste ret.Clear();
111f034231aSEd Maste
112f034231aSEd Maste lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
113f034231aSEd Maste lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
114f034231aSEd Maste
11514f1b3e8SDimitry Andric for (JittedFunction &function : m_jitted_functions) {
11614f1b3e8SDimitry Andric if (function.m_name == m_name) {
117f034231aSEd Maste func_local_addr = function.m_local_addr;
118f034231aSEd Maste func_remote_addr = function.m_remote_addr;
119f034231aSEd Maste }
120f034231aSEd Maste }
121f034231aSEd Maste
12214f1b3e8SDimitry Andric if (func_local_addr == LLDB_INVALID_ADDRESS) {
123f034231aSEd Maste ret.SetErrorToGenericError();
12414f1b3e8SDimitry Andric ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly",
12514f1b3e8SDimitry Andric m_name.AsCString());
126f034231aSEd Maste return ret;
127f034231aSEd Maste }
128f034231aSEd Maste
129ead24645SDimitry Andric LLDB_LOGF(log,
130ead24645SDimitry Andric "Found function, has local address 0x%" PRIx64
13114f1b3e8SDimitry Andric " and remote address 0x%" PRIx64,
13214f1b3e8SDimitry Andric (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
133f034231aSEd Maste
134f034231aSEd Maste std::pair<lldb::addr_t, lldb::addr_t> func_range;
135f034231aSEd Maste
136f034231aSEd Maste func_range = GetRemoteRangeForLocal(func_local_addr);
137f034231aSEd Maste
13814f1b3e8SDimitry Andric if (func_range.first == 0 && func_range.second == 0) {
139f034231aSEd Maste ret.SetErrorToGenericError();
14014f1b3e8SDimitry Andric ret.SetErrorStringWithFormat("Couldn't find code range for function %s",
14114f1b3e8SDimitry Andric m_name.AsCString());
142f034231aSEd Maste return ret;
143f034231aSEd Maste }
144f034231aSEd Maste
145ead24645SDimitry Andric LLDB_LOGF(log, "Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
14614f1b3e8SDimitry Andric func_range.first, func_range.second);
147f034231aSEd Maste
148f034231aSEd Maste Target *target = exe_ctx.GetTargetPtr();
14914f1b3e8SDimitry Andric if (!target) {
150f034231aSEd Maste ret.SetErrorToGenericError();
151f034231aSEd Maste ret.SetErrorString("Couldn't find the target");
152f034231aSEd Maste return ret;
153f034231aSEd Maste }
154f034231aSEd Maste
155145449b1SDimitry Andric lldb::WritableDataBufferSP buffer_sp(
156145449b1SDimitry Andric new DataBufferHeap(func_range.second, 0));
157f034231aSEd Maste
158f034231aSEd Maste Process *process = exe_ctx.GetProcessPtr();
159b76161e4SDimitry Andric Status err;
16014f1b3e8SDimitry Andric process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
16114f1b3e8SDimitry Andric buffer_sp->GetByteSize(), err);
162f034231aSEd Maste
16314f1b3e8SDimitry Andric if (!err.Success()) {
164f034231aSEd Maste ret.SetErrorToGenericError();
16514f1b3e8SDimitry Andric ret.SetErrorStringWithFormat("Couldn't read from process: %s",
16614f1b3e8SDimitry Andric err.AsCString("unknown error"));
167f034231aSEd Maste return ret;
168f034231aSEd Maste }
169f034231aSEd Maste
170f034231aSEd Maste ArchSpec arch(target->GetArchitecture());
171f034231aSEd Maste
1725f29bb8aSDimitry Andric const char *plugin_name = nullptr;
1735f29bb8aSDimitry Andric const char *flavor_string = nullptr;
17414f1b3e8SDimitry Andric lldb::DisassemblerSP disassembler_sp =
17514f1b3e8SDimitry Andric Disassembler::FindPlugin(arch, flavor_string, plugin_name);
176f034231aSEd Maste
17714f1b3e8SDimitry Andric if (!disassembler_sp) {
178f034231aSEd Maste ret.SetErrorToGenericError();
17914f1b3e8SDimitry Andric ret.SetErrorStringWithFormat(
18014f1b3e8SDimitry Andric "Unable to find disassembler plug-in for %s architecture.",
18114f1b3e8SDimitry Andric arch.GetArchitectureName());
182f034231aSEd Maste return ret;
183f034231aSEd Maste }
184f034231aSEd Maste
18514f1b3e8SDimitry Andric if (!process) {
186f034231aSEd Maste ret.SetErrorToGenericError();
187f034231aSEd Maste ret.SetErrorString("Couldn't find the process");
188f034231aSEd Maste return ret;
189f034231aSEd Maste }
190f034231aSEd Maste
19114f1b3e8SDimitry Andric DataExtractor extractor(buffer_sp, process->GetByteOrder(),
192f034231aSEd Maste target->GetArchitecture().GetAddressByteSize());
193f034231aSEd Maste
19414f1b3e8SDimitry Andric if (log) {
195ead24645SDimitry Andric LLDB_LOGF(log, "Function data has contents:");
19614f1b3e8SDimitry Andric extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
197f034231aSEd Maste DataExtractor::TypeUInt8);
198f034231aSEd Maste }
199f034231aSEd Maste
20014f1b3e8SDimitry Andric disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
20114f1b3e8SDimitry Andric UINT32_MAX, false, false);
202f034231aSEd Maste
203f034231aSEd Maste InstructionList &instruction_list = disassembler_sp->GetInstructionList();
204ac9a064cSDimitry Andric instruction_list.Dump(&stream, true, true, /*show_control_flow_kind=*/false,
2051f917f69SDimitry Andric &exe_ctx);
2061f917f69SDimitry Andric
207f034231aSEd Maste return ret;
208f034231aSEd Maste }
209f034231aSEd Maste
210344a3780SDimitry Andric namespace {
211344a3780SDimitry Andric struct IRExecDiagnosticHandler : public llvm::DiagnosticHandler {
212344a3780SDimitry Andric Status *err;
IRExecDiagnosticHandler__anon268fa4700111::IRExecDiagnosticHandler213344a3780SDimitry Andric IRExecDiagnosticHandler(Status *err) : err(err) {}
handleDiagnostics__anon268fa4700111::IRExecDiagnosticHandler214344a3780SDimitry Andric bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override {
215ac9a064cSDimitry Andric if (DI.getSeverity() == llvm::DS_Error) {
216344a3780SDimitry Andric const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(DI);
21714f1b3e8SDimitry Andric if (err && err->Success()) {
218f034231aSEd Maste err->SetErrorToGenericError();
219344a3780SDimitry Andric err->SetErrorStringWithFormat(
220ac9a064cSDimitry Andric "IRExecution error: %s",
221344a3780SDimitry Andric DISM.getSMDiag().getMessage().str().c_str());
222f034231aSEd Maste }
223f034231aSEd Maste }
224f034231aSEd Maste
225ac9a064cSDimitry Andric return true;
226344a3780SDimitry Andric }
227344a3780SDimitry Andric };
228344a3780SDimitry Andric } // namespace
229344a3780SDimitry Andric
ReportSymbolLookupError(ConstString name)2305f29bb8aSDimitry Andric void IRExecutionUnit::ReportSymbolLookupError(ConstString name) {
23112bd4897SEd Maste m_failed_lookups.push_back(name);
23212bd4897SEd Maste }
23312bd4897SEd Maste
GetRunnableInfo(Status & error,lldb::addr_t & func_addr,lldb::addr_t & func_end)234b76161e4SDimitry Andric void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
23514f1b3e8SDimitry Andric lldb::addr_t &func_end) {
236f034231aSEd Maste lldb::ProcessSP process_sp(GetProcessWP().lock());
237f034231aSEd Maste
238f3fbd1c0SDimitry Andric static std::recursive_mutex s_runnable_info_mutex;
239866dcdacSEd Maste
240f034231aSEd Maste func_addr = LLDB_INVALID_ADDRESS;
241f034231aSEd Maste func_end = LLDB_INVALID_ADDRESS;
242f034231aSEd Maste
24314f1b3e8SDimitry Andric if (!process_sp) {
244f034231aSEd Maste error.SetErrorToGenericError();
24514f1b3e8SDimitry Andric error.SetErrorString("Couldn't write the JIT compiled code into the "
24614f1b3e8SDimitry Andric "process because the process is invalid");
247f034231aSEd Maste return;
248f034231aSEd Maste }
249f034231aSEd Maste
25014f1b3e8SDimitry Andric if (m_did_jit) {
251f034231aSEd Maste func_addr = m_function_load_addr;
252f034231aSEd Maste func_end = m_function_end_load_addr;
253f034231aSEd Maste
254f034231aSEd Maste return;
255f034231aSEd Maste };
256f034231aSEd Maste
257f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
258866dcdacSEd Maste
259f034231aSEd Maste m_did_jit = true;
260f034231aSEd Maste
261145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
262f034231aSEd Maste
263f034231aSEd Maste std::string error_string;
264f034231aSEd Maste
26514f1b3e8SDimitry Andric if (log) {
266f034231aSEd Maste std::string s;
267f034231aSEd Maste llvm::raw_string_ostream oss(s);
268f034231aSEd Maste
2695f29bb8aSDimitry Andric m_module->print(oss, nullptr);
270f034231aSEd Maste
271f034231aSEd Maste oss.flush();
272f034231aSEd Maste
273ead24645SDimitry Andric LLDB_LOGF(log, "Module being sent to JIT: \n%s", s.c_str());
274f034231aSEd Maste }
275f034231aSEd Maste
276344a3780SDimitry Andric m_module_up->getContext().setDiagnosticHandler(
277344a3780SDimitry Andric std::make_unique<IRExecDiagnosticHandler>(&error));
278f034231aSEd Maste
2795f29bb8aSDimitry Andric llvm::EngineBuilder builder(std::move(m_module_up));
2805f29bb8aSDimitry Andric llvm::Triple triple(m_module->getTargetTriple());
281f034231aSEd Maste
282f034231aSEd Maste builder.setEngineKind(llvm::EngineKind::JIT)
283f034231aSEd Maste .setErrorStr(&error_string)
284cfca06d7SDimitry Andric .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_
2855f29bb8aSDimitry Andric : llvm::Reloc::Static)
286cfca06d7SDimitry Andric .setMCJITMemoryManager(std::make_unique<MemoryManager>(*this))
287b1c73532SDimitry Andric .setOptLevel(llvm::CodeGenOptLevel::Less);
288f034231aSEd Maste
289f034231aSEd Maste llvm::StringRef mArch;
290f034231aSEd Maste llvm::StringRef mCPU;
291f034231aSEd Maste llvm::SmallVector<std::string, 0> mAttrs;
292f034231aSEd Maste
293f034231aSEd Maste for (std::string &feature : m_cpu_features)
294f034231aSEd Maste mAttrs.push_back(feature);
295f034231aSEd Maste
29614f1b3e8SDimitry Andric llvm::TargetMachine *target_machine =
29714f1b3e8SDimitry Andric builder.selectTarget(triple, mArch, mCPU, mAttrs);
298f034231aSEd Maste
2995f29bb8aSDimitry Andric m_execution_engine_up.reset(builder.create(target_machine));
300f034231aSEd Maste
3015f29bb8aSDimitry Andric if (!m_execution_engine_up) {
302f034231aSEd Maste error.SetErrorToGenericError();
30314f1b3e8SDimitry Andric error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
30414f1b3e8SDimitry Andric error_string.c_str());
305f034231aSEd Maste return;
306f034231aSEd Maste }
3070cac4ca3SEd Maste
3085f29bb8aSDimitry Andric m_strip_underscore =
3095f29bb8aSDimitry Andric (m_execution_engine_up->getDataLayout().getGlobalPrefix() == '_');
3105f29bb8aSDimitry Andric
31114f1b3e8SDimitry Andric class ObjectDumper : public llvm::ObjectCache {
31214f1b3e8SDimitry Andric public:
313145449b1SDimitry Andric ObjectDumper(FileSpec output_dir) : m_out_dir(output_dir) {}
31414f1b3e8SDimitry Andric void notifyObjectCompiled(const llvm::Module *module,
31514f1b3e8SDimitry Andric llvm::MemoryBufferRef object) override {
31614f1b3e8SDimitry Andric int fd = 0;
31714f1b3e8SDimitry Andric llvm::SmallVector<char, 256> result_path;
31814f1b3e8SDimitry Andric std::string object_name_model =
31914f1b3e8SDimitry Andric "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
320145449b1SDimitry Andric FileSpec model_spec
321145449b1SDimitry Andric = m_out_dir.CopyByAppendingPathComponent(object_name_model);
322145449b1SDimitry Andric std::string model_path = model_spec.GetPath();
323145449b1SDimitry Andric
324145449b1SDimitry Andric std::error_code result
325145449b1SDimitry Andric = llvm::sys::fs::createUniqueFile(model_path, fd, result_path);
326145449b1SDimitry Andric if (!result) {
32714f1b3e8SDimitry Andric llvm::raw_fd_ostream fds(fd, true);
32814f1b3e8SDimitry Andric fds.write(object.getBufferStart(), object.getBufferSize());
32914f1b3e8SDimitry Andric }
330145449b1SDimitry Andric }
33114f1b3e8SDimitry Andric std::unique_ptr<llvm::MemoryBuffer>
33214f1b3e8SDimitry Andric getObject(const llvm::Module *module) override {
33314f1b3e8SDimitry Andric // Return nothing - we're just abusing the object-cache mechanism to dump
33414f1b3e8SDimitry Andric // objects.
33514f1b3e8SDimitry Andric return nullptr;
33614f1b3e8SDimitry Andric }
337145449b1SDimitry Andric private:
338145449b1SDimitry Andric FileSpec m_out_dir;
33914f1b3e8SDimitry Andric };
34014f1b3e8SDimitry Andric
341145449b1SDimitry Andric FileSpec save_objects_dir = process_sp->GetTarget().GetSaveJITObjectsDir();
342145449b1SDimitry Andric if (save_objects_dir) {
343145449b1SDimitry Andric m_object_cache_up = std::make_unique<ObjectDumper>(save_objects_dir);
3445f29bb8aSDimitry Andric m_execution_engine_up->setObjectCache(m_object_cache_up.get());
34514f1b3e8SDimitry Andric }
34614f1b3e8SDimitry Andric
34714f1b3e8SDimitry Andric // Make sure we see all sections, including ones that don't have
34814f1b3e8SDimitry Andric // relocations...
3495f29bb8aSDimitry Andric m_execution_engine_up->setProcessAllSections(true);
350f034231aSEd Maste
3515f29bb8aSDimitry Andric m_execution_engine_up->DisableLazyCompilation();
352f034231aSEd Maste
35314f1b3e8SDimitry Andric for (llvm::Function &function : *m_module) {
354f3fbd1c0SDimitry Andric if (function.isDeclaration() || function.hasPrivateLinkage())
355f3fbd1c0SDimitry Andric continue;
356f034231aSEd Maste
357b60736ecSDimitry Andric const bool external = !function.hasLocalLinkage();
358f3fbd1c0SDimitry Andric
3595f29bb8aSDimitry Andric void *fun_ptr = m_execution_engine_up->getPointerToFunction(&function);
360f034231aSEd Maste
36114f1b3e8SDimitry Andric if (!error.Success()) {
362f034231aSEd Maste // We got an error through our callback!
363f034231aSEd Maste return;
364f034231aSEd Maste }
365f034231aSEd Maste
36614f1b3e8SDimitry Andric if (!fun_ptr) {
367f034231aSEd Maste error.SetErrorToGenericError();
36814f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
36914f1b3e8SDimitry Andric "'%s' was in the JITted module but wasn't lowered",
37014f1b3e8SDimitry Andric function.getName().str().c_str());
371f034231aSEd Maste return;
372f034231aSEd Maste }
37314f1b3e8SDimitry Andric m_jitted_functions.push_back(JittedFunction(
374706b4fc4SDimitry Andric function.getName().str().c_str(), external, reinterpret_cast<uintptr_t>(fun_ptr)));
375f3fbd1c0SDimitry Andric }
376f034231aSEd Maste
377f034231aSEd Maste CommitAllocations(process_sp);
3785f29bb8aSDimitry Andric ReportAllocations(*m_execution_engine_up);
379f3fbd1c0SDimitry Andric
38014f1b3e8SDimitry Andric // We have to do this after calling ReportAllocations because for the MCJIT,
381f73363f1SDimitry Andric // getGlobalValueAddress will cause the JIT to perform all relocations. That
382f73363f1SDimitry Andric // can only be done once, and has to happen after we do the remapping from
383f73363f1SDimitry Andric // local -> remote. That means we don't know the local address of the
384f73363f1SDimitry Andric // Variables, but we don't need that for anything, so that's okay.
385f3fbd1c0SDimitry Andric
38614f1b3e8SDimitry Andric std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
38714f1b3e8SDimitry Andric llvm::GlobalValue &val) {
38814f1b3e8SDimitry Andric if (val.hasExternalLinkage() && !val.isDeclaration()) {
38914f1b3e8SDimitry Andric uint64_t var_ptr_addr =
3905f29bb8aSDimitry Andric m_execution_engine_up->getGlobalValueAddress(val.getName().str());
391f3fbd1c0SDimitry Andric
392f3fbd1c0SDimitry Andric lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
393f3fbd1c0SDimitry Andric
39414f1b3e8SDimitry Andric // This is a really unfortunae API that sometimes returns local addresses
395f73363f1SDimitry Andric // and sometimes returns remote addresses, based on whether the variable
396f73363f1SDimitry Andric // was relocated during ReportAllocations or not.
397f3fbd1c0SDimitry Andric
39814f1b3e8SDimitry Andric if (remote_addr == LLDB_INVALID_ADDRESS) {
399f3fbd1c0SDimitry Andric remote_addr = var_ptr_addr;
400f3fbd1c0SDimitry Andric }
401f3fbd1c0SDimitry Andric
402f3fbd1c0SDimitry Andric if (var_ptr_addr != 0)
40314f1b3e8SDimitry Andric m_jitted_global_variables.push_back(JittedGlobalVariable(
40414f1b3e8SDimitry Andric val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
405f3fbd1c0SDimitry Andric }
406f3fbd1c0SDimitry Andric };
407f3fbd1c0SDimitry Andric
4087fa27ce4SDimitry Andric for (llvm::GlobalVariable &global_var : m_module->globals()) {
409f3fbd1c0SDimitry Andric RegisterOneValue(global_var);
410f3fbd1c0SDimitry Andric }
411f3fbd1c0SDimitry Andric
4127fa27ce4SDimitry Andric for (llvm::GlobalAlias &global_alias : m_module->aliases()) {
413f3fbd1c0SDimitry Andric RegisterOneValue(global_alias);
414f3fbd1c0SDimitry Andric }
415f3fbd1c0SDimitry Andric
416f034231aSEd Maste WriteData(process_sp);
417f034231aSEd Maste
41814f1b3e8SDimitry Andric if (m_failed_lookups.size()) {
41912bd4897SEd Maste StreamString ss;
42012bd4897SEd Maste
42112bd4897SEd Maste ss.PutCString("Couldn't look up symbols:\n");
42212bd4897SEd Maste
42312bd4897SEd Maste bool emitNewLine = false;
42412bd4897SEd Maste
4255f29bb8aSDimitry Andric for (ConstString failed_lookup : m_failed_lookups) {
42612bd4897SEd Maste if (emitNewLine)
42712bd4897SEd Maste ss.PutCString("\n");
42812bd4897SEd Maste emitNewLine = true;
42912bd4897SEd Maste ss.PutCString(" ");
430cfca06d7SDimitry Andric ss.PutCString(Mangled(failed_lookup).GetDemangledName().GetStringRef());
43112bd4897SEd Maste }
43212bd4897SEd Maste
43312bd4897SEd Maste m_failed_lookups.clear();
434ac9a064cSDimitry Andric ss.PutCString(
435ac9a064cSDimitry Andric "\nHint: The expression tried to call a function that is not present "
436ac9a064cSDimitry Andric "in the target, perhaps because it was optimized out by the compiler.");
43714f1b3e8SDimitry Andric error.SetErrorString(ss.GetString());
43812bd4897SEd Maste
43912bd4897SEd Maste return;
44012bd4897SEd Maste }
44112bd4897SEd Maste
44212bd4897SEd Maste m_function_load_addr = LLDB_INVALID_ADDRESS;
44312bd4897SEd Maste m_function_end_load_addr = LLDB_INVALID_ADDRESS;
44412bd4897SEd Maste
44514f1b3e8SDimitry Andric for (JittedFunction &jitted_function : m_jitted_functions) {
44614f1b3e8SDimitry Andric jitted_function.m_remote_addr =
44714f1b3e8SDimitry Andric GetRemoteAddressForLocal(jitted_function.m_local_addr);
448f034231aSEd Maste
44914f1b3e8SDimitry Andric if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
45014f1b3e8SDimitry Andric AddrRange func_range =
45114f1b3e8SDimitry Andric GetRemoteRangeForLocal(jitted_function.m_local_addr);
452f034231aSEd Maste m_function_end_load_addr = func_range.first + func_range.second;
453f034231aSEd Maste m_function_load_addr = jitted_function.m_remote_addr;
454f034231aSEd Maste }
455f034231aSEd Maste }
456f034231aSEd Maste
45714f1b3e8SDimitry Andric if (log) {
458ead24645SDimitry Andric LLDB_LOGF(log, "Code can be run in the target.");
459f034231aSEd Maste
460f034231aSEd Maste StreamString disassembly_stream;
461f034231aSEd Maste
462b76161e4SDimitry Andric Status err = DisassembleFunction(disassembly_stream, process_sp);
463f034231aSEd Maste
46414f1b3e8SDimitry Andric if (!err.Success()) {
465ead24645SDimitry Andric LLDB_LOGF(log, "Couldn't disassemble function : %s",
46614f1b3e8SDimitry Andric err.AsCString("unknown error"));
46714f1b3e8SDimitry Andric } else {
468ead24645SDimitry Andric LLDB_LOGF(log, "Function disassembly:\n%s", disassembly_stream.GetData());
469f034231aSEd Maste }
470866dcdacSEd Maste
471ead24645SDimitry Andric LLDB_LOGF(log, "Sections: ");
47214f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
47314f1b3e8SDimitry Andric if (record.m_process_address != LLDB_INVALID_ADDRESS) {
474866dcdacSEd Maste record.dump(log);
475866dcdacSEd Maste
476866dcdacSEd Maste DataBufferHeap my_buffer(record.m_size, 0);
477b76161e4SDimitry Andric Status err;
47814f1b3e8SDimitry Andric ReadMemory(my_buffer.GetBytes(), record.m_process_address,
47914f1b3e8SDimitry Andric record.m_size, err);
480866dcdacSEd Maste
48114f1b3e8SDimitry Andric if (err.Success()) {
48214f1b3e8SDimitry Andric DataExtractor my_extractor(my_buffer.GetBytes(),
48314f1b3e8SDimitry Andric my_buffer.GetByteSize(),
48414f1b3e8SDimitry Andric lldb::eByteOrderBig, 8);
48514f1b3e8SDimitry Andric my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
48614f1b3e8SDimitry Andric record.m_process_address, 16,
48714f1b3e8SDimitry Andric DataExtractor::TypeUInt8);
488866dcdacSEd Maste }
48914f1b3e8SDimitry Andric } else {
490e81d9d49SDimitry Andric record.dump(log);
491e81d9d49SDimitry Andric
49214f1b3e8SDimitry Andric DataExtractor my_extractor((const void *)record.m_host_address,
49314f1b3e8SDimitry Andric record.m_size, lldb::eByteOrderBig, 8);
49414f1b3e8SDimitry Andric my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
49514f1b3e8SDimitry Andric DataExtractor::TypeUInt8);
496e81d9d49SDimitry Andric }
497866dcdacSEd Maste }
498f034231aSEd Maste }
499f034231aSEd Maste
500f034231aSEd Maste func_addr = m_function_load_addr;
501f034231aSEd Maste func_end = m_function_end_load_addr;
502f034231aSEd Maste }
503f034231aSEd Maste
~IRExecutionUnit()50414f1b3e8SDimitry Andric IRExecutionUnit::~IRExecutionUnit() {
5055f29bb8aSDimitry Andric m_module_up.reset();
5065f29bb8aSDimitry Andric m_execution_engine_up.reset();
5075f29bb8aSDimitry Andric m_context_up.reset();
508f034231aSEd Maste }
509f034231aSEd Maste
MemoryManager(IRExecutionUnit & parent)51014f1b3e8SDimitry Andric IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
5115f29bb8aSDimitry Andric : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {}
512f034231aSEd Maste
513344a3780SDimitry Andric IRExecutionUnit::MemoryManager::~MemoryManager() = default;
514f034231aSEd Maste
GetSectionTypeFromSectionName(const llvm::StringRef & name,IRExecutionUnit::AllocationKind alloc_kind)51514f1b3e8SDimitry Andric lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
51614f1b3e8SDimitry Andric const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
5170cac4ca3SEd Maste lldb::SectionType sect_type = lldb::eSectionTypeCode;
51814f1b3e8SDimitry Andric switch (alloc_kind) {
51914f1b3e8SDimitry Andric case AllocationKind::Stub:
52014f1b3e8SDimitry Andric sect_type = lldb::eSectionTypeCode;
52114f1b3e8SDimitry Andric break;
52214f1b3e8SDimitry Andric case AllocationKind::Code:
52314f1b3e8SDimitry Andric sect_type = lldb::eSectionTypeCode;
52414f1b3e8SDimitry Andric break;
52514f1b3e8SDimitry Andric case AllocationKind::Data:
52614f1b3e8SDimitry Andric sect_type = lldb::eSectionTypeData;
52714f1b3e8SDimitry Andric break;
52814f1b3e8SDimitry Andric case AllocationKind::Global:
52914f1b3e8SDimitry Andric sect_type = lldb::eSectionTypeData;
53014f1b3e8SDimitry Andric break;
53114f1b3e8SDimitry Andric case AllocationKind::Bytes:
53214f1b3e8SDimitry Andric sect_type = lldb::eSectionTypeOther;
53314f1b3e8SDimitry Andric break;
5340cac4ca3SEd Maste }
5350cac4ca3SEd Maste
53614f1b3e8SDimitry Andric if (!name.empty()) {
537ac9a064cSDimitry Andric if (name == "__text" || name == ".text")
5380cac4ca3SEd Maste sect_type = lldb::eSectionTypeCode;
539ac9a064cSDimitry Andric else if (name == "__data" || name == ".data")
5400cac4ca3SEd Maste sect_type = lldb::eSectionTypeCode;
541312c0ed1SDimitry Andric else if (name.starts_with("__debug_") || name.starts_with(".debug_")) {
5420cac4ca3SEd Maste const uint32_t name_idx = name[0] == '_' ? 8 : 7;
5430cac4ca3SEd Maste llvm::StringRef dwarf_name(name.substr(name_idx));
54414f1b3e8SDimitry Andric switch (dwarf_name[0]) {
5450cac4ca3SEd Maste case 'a':
546ac9a064cSDimitry Andric if (dwarf_name == "abbrev")
5470cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
548ac9a064cSDimitry Andric else if (dwarf_name == "aranges")
5490cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugAranges;
550ac9a064cSDimitry Andric else if (dwarf_name == "addr")
551e81d9d49SDimitry Andric sect_type = lldb::eSectionTypeDWARFDebugAddr;
5520cac4ca3SEd Maste break;
5530cac4ca3SEd Maste
5540cac4ca3SEd Maste case 'f':
555ac9a064cSDimitry Andric if (dwarf_name == "frame")
5560cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugFrame;
5570cac4ca3SEd Maste break;
5580cac4ca3SEd Maste
5590cac4ca3SEd Maste case 'i':
560ac9a064cSDimitry Andric if (dwarf_name == "info")
5610cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugInfo;
5620cac4ca3SEd Maste break;
5630cac4ca3SEd Maste
5640cac4ca3SEd Maste case 'l':
565ac9a064cSDimitry Andric if (dwarf_name == "line")
5660cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugLine;
567ac9a064cSDimitry Andric else if (dwarf_name == "loc")
5680cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugLoc;
569ac9a064cSDimitry Andric else if (dwarf_name == "loclists")
57094994d37SDimitry Andric sect_type = lldb::eSectionTypeDWARFDebugLocLists;
5710cac4ca3SEd Maste break;
5720cac4ca3SEd Maste
5730cac4ca3SEd Maste case 'm':
574ac9a064cSDimitry Andric if (dwarf_name == "macinfo")
5750cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
5760cac4ca3SEd Maste break;
5770cac4ca3SEd Maste
5780cac4ca3SEd Maste case 'p':
579ac9a064cSDimitry Andric if (dwarf_name == "pubnames")
5800cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugPubNames;
581ac9a064cSDimitry Andric else if (dwarf_name == "pubtypes")
5820cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
5830cac4ca3SEd Maste break;
5840cac4ca3SEd Maste
5850cac4ca3SEd Maste case 's':
586ac9a064cSDimitry Andric if (dwarf_name == "str")
5870cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugStr;
588ac9a064cSDimitry Andric else if (dwarf_name == "str_offsets")
589e81d9d49SDimitry Andric sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
5900cac4ca3SEd Maste break;
5910cac4ca3SEd Maste
5920cac4ca3SEd Maste case 'r':
593ac9a064cSDimitry Andric if (dwarf_name == "ranges")
5940cac4ca3SEd Maste sect_type = lldb::eSectionTypeDWARFDebugRanges;
5950cac4ca3SEd Maste break;
5960cac4ca3SEd Maste
5970cac4ca3SEd Maste default:
5980cac4ca3SEd Maste break;
5990cac4ca3SEd Maste }
600312c0ed1SDimitry Andric } else if (name.starts_with("__apple_") || name.starts_with(".apple_"))
6010cac4ca3SEd Maste sect_type = lldb::eSectionTypeInvalid;
602ac9a064cSDimitry Andric else if (name == "__objc_imageinfo")
6030cac4ca3SEd Maste sect_type = lldb::eSectionTypeOther;
6040cac4ca3SEd Maste }
6050cac4ca3SEd Maste return sect_type;
6060cac4ca3SEd Maste }
6070cac4ca3SEd Maste
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,llvm::StringRef SectionName)60814f1b3e8SDimitry Andric uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
60914f1b3e8SDimitry Andric uintptr_t Size, unsigned Alignment, unsigned SectionID,
61014f1b3e8SDimitry Andric llvm::StringRef SectionName) {
611145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
612f034231aSEd Maste
6135f29bb8aSDimitry Andric uint8_t *return_value = m_default_mm_up->allocateCodeSection(
61414f1b3e8SDimitry Andric Size, Alignment, SectionID, SectionName);
615f034231aSEd Maste
61614f1b3e8SDimitry Andric m_parent.m_records.push_back(AllocationRecord(
61714f1b3e8SDimitry Andric (uintptr_t)return_value,
618f034231aSEd Maste lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
61914f1b3e8SDimitry Andric GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
62014f1b3e8SDimitry Andric Alignment, SectionID, SectionName.str().c_str()));
621f034231aSEd Maste
622ead24645SDimitry Andric LLDB_LOGF(log,
623ead24645SDimitry Andric "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
62414f1b3e8SDimitry Andric ", Alignment=%u, SectionID=%u) = %p",
6255e95aa85SEd Maste (uint64_t)Size, Alignment, SectionID, (void *)return_value);
626f034231aSEd Maste
62714f1b3e8SDimitry Andric if (m_parent.m_reported_allocations) {
628b76161e4SDimitry Andric Status err;
62914f1b3e8SDimitry Andric lldb::ProcessSP process_sp =
63014f1b3e8SDimitry Andric m_parent.GetBestExecutionContextScope()->CalculateProcess();
631f3fbd1c0SDimitry Andric
632f3fbd1c0SDimitry Andric m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
633f3fbd1c0SDimitry Andric }
634f3fbd1c0SDimitry Andric
635f034231aSEd Maste return return_value;
636f034231aSEd Maste }
637f034231aSEd Maste
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,llvm::StringRef SectionName,bool IsReadOnly)63814f1b3e8SDimitry Andric uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
63914f1b3e8SDimitry Andric uintptr_t Size, unsigned Alignment, unsigned SectionID,
64014f1b3e8SDimitry Andric llvm::StringRef SectionName, bool IsReadOnly) {
641145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
642f034231aSEd Maste
6435f29bb8aSDimitry Andric uint8_t *return_value = m_default_mm_up->allocateDataSection(
64414f1b3e8SDimitry Andric Size, Alignment, SectionID, SectionName, IsReadOnly);
645f034231aSEd Maste
6465e95aa85SEd Maste uint32_t permissions = lldb::ePermissionsReadable;
6475e95aa85SEd Maste if (!IsReadOnly)
6485e95aa85SEd Maste permissions |= lldb::ePermissionsWritable;
64914f1b3e8SDimitry Andric m_parent.m_records.push_back(AllocationRecord(
65014f1b3e8SDimitry Andric (uintptr_t)return_value, permissions,
65114f1b3e8SDimitry Andric GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
65214f1b3e8SDimitry Andric Alignment, SectionID, SectionName.str().c_str()));
653ead24645SDimitry Andric LLDB_LOGF(log,
654ead24645SDimitry Andric "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
65514f1b3e8SDimitry Andric ", Alignment=%u, SectionID=%u) = %p",
6565e95aa85SEd Maste (uint64_t)Size, Alignment, SectionID, (void *)return_value);
657f034231aSEd Maste
65814f1b3e8SDimitry Andric if (m_parent.m_reported_allocations) {
659b76161e4SDimitry Andric Status err;
66014f1b3e8SDimitry Andric lldb::ProcessSP process_sp =
66114f1b3e8SDimitry Andric m_parent.GetBestExecutionContextScope()->CalculateProcess();
662f3fbd1c0SDimitry Andric
663f3fbd1c0SDimitry Andric m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
664f3fbd1c0SDimitry Andric }
665f3fbd1c0SDimitry Andric
666f034231aSEd Maste return return_value;
667f034231aSEd Maste }
668f034231aSEd Maste
CollectCandidateCNames(std::vector<ConstString> & C_names,ConstString name)669c0981da4SDimitry Andric void IRExecutionUnit::CollectCandidateCNames(std::vector<ConstString> &C_names,
6705f29bb8aSDimitry Andric ConstString name) {
671f3fbd1c0SDimitry Andric if (m_strip_underscore && name.AsCString()[0] == '_')
672c0981da4SDimitry Andric C_names.insert(C_names.begin(), ConstString(&name.AsCString()[1]));
673c0981da4SDimitry Andric C_names.push_back(name);
674f3fbd1c0SDimitry Andric }
675f3fbd1c0SDimitry Andric
CollectCandidateCPlusPlusNames(std::vector<ConstString> & CPP_names,const std::vector<ConstString> & C_names,const SymbolContext & sc)67614f1b3e8SDimitry Andric void IRExecutionUnit::CollectCandidateCPlusPlusNames(
677c0981da4SDimitry Andric std::vector<ConstString> &CPP_names,
678c0981da4SDimitry Andric const std::vector<ConstString> &C_names, const SymbolContext &sc) {
679c0981da4SDimitry Andric if (auto *cpp_lang = Language::FindPlugin(lldb::eLanguageTypeC_plus_plus)) {
680c0981da4SDimitry Andric for (const ConstString &name : C_names) {
681ead24645SDimitry Andric Mangled mangled(name);
682c0981da4SDimitry Andric if (cpp_lang->SymbolNameFitsToLanguage(mangled)) {
683c0981da4SDimitry Andric if (ConstString best_alternate =
684c0981da4SDimitry Andric cpp_lang->FindBestAlternateFunctionMangledName(mangled, sc)) {
685c0981da4SDimitry Andric CPP_names.push_back(best_alternate);
686c0981da4SDimitry Andric }
687c0981da4SDimitry Andric }
688f3fbd1c0SDimitry Andric
689c0981da4SDimitry Andric std::vector<ConstString> alternates =
690c0981da4SDimitry Andric cpp_lang->GenerateAlternateFunctionManglings(name);
691c0981da4SDimitry Andric CPP_names.insert(CPP_names.end(), alternates.begin(), alternates.end());
692f3fbd1c0SDimitry Andric
693c0981da4SDimitry Andric // As a last-ditch fallback, try the base name for C++ names. It's
694c0981da4SDimitry Andric // terrible, but the DWARF doesn't always encode "extern C" correctly.
695c0981da4SDimitry Andric ConstString basename =
696c0981da4SDimitry Andric cpp_lang->GetDemangledFunctionNameWithoutArguments(mangled);
697c0981da4SDimitry Andric CPP_names.push_back(basename);
698f3fbd1c0SDimitry Andric }
699f3fbd1c0SDimitry Andric }
700f3fbd1c0SDimitry Andric }
701f3fbd1c0SDimitry Andric
702c0981da4SDimitry Andric class LoadAddressResolver {
703c0981da4SDimitry Andric public:
LoadAddressResolver(Target * target,bool & symbol_was_missing_weak)704c0981da4SDimitry Andric LoadAddressResolver(Target *target, bool &symbol_was_missing_weak)
705c0981da4SDimitry Andric : m_target(target), m_symbol_was_missing_weak(symbol_was_missing_weak) {}
706f3fbd1c0SDimitry Andric
Resolve(SymbolContextList & sc_list)707e3b55780SDimitry Andric std::optional<lldb::addr_t> Resolve(SymbolContextList &sc_list) {
708c0981da4SDimitry Andric if (sc_list.IsEmpty())
709e3b55780SDimitry Andric return std::nullopt;
710f3fbd1c0SDimitry Andric
711c0981da4SDimitry Andric lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
712f3fbd1c0SDimitry Andric
713c0981da4SDimitry Andric // Missing_weak_symbol will be true only if we found only weak undefined
7145f29bb8aSDimitry Andric // references to this symbol.
715c0981da4SDimitry Andric m_symbol_was_missing_weak = true;
716f3fbd1c0SDimitry Andric
717c0981da4SDimitry Andric for (auto candidate_sc : sc_list.SymbolContexts()) {
718c0981da4SDimitry Andric // Only symbols can be weak undefined.
719c0981da4SDimitry Andric if (!candidate_sc.symbol ||
720c0981da4SDimitry Andric candidate_sc.symbol->GetType() != lldb::eSymbolTypeUndefined ||
721c0981da4SDimitry Andric !candidate_sc.symbol->IsWeak())
722c0981da4SDimitry Andric m_symbol_was_missing_weak = false;
723c0981da4SDimitry Andric
724c0981da4SDimitry Andric // First try the symbol.
725c0981da4SDimitry Andric if (candidate_sc.symbol) {
726c0981da4SDimitry Andric load_address = candidate_sc.symbol->ResolveCallableAddress(*m_target);
727c0981da4SDimitry Andric if (load_address == LLDB_INVALID_ADDRESS) {
728c0981da4SDimitry Andric Address addr = candidate_sc.symbol->GetAddress();
729c0981da4SDimitry Andric load_address = m_target->GetProcessSP()
730c0981da4SDimitry Andric ? addr.GetLoadAddress(m_target)
731c0981da4SDimitry Andric : addr.GetFileAddress();
732c0981da4SDimitry Andric }
733c0981da4SDimitry Andric }
734c0981da4SDimitry Andric
735c0981da4SDimitry Andric // If that didn't work, try the function.
736c0981da4SDimitry Andric if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
737c0981da4SDimitry Andric Address addr =
738c0981da4SDimitry Andric candidate_sc.function->GetAddressRange().GetBaseAddress();
739c0981da4SDimitry Andric load_address = m_target->GetProcessSP() ? addr.GetLoadAddress(m_target)
740c0981da4SDimitry Andric : addr.GetFileAddress();
741c0981da4SDimitry Andric }
742c0981da4SDimitry Andric
743c0981da4SDimitry Andric // We found a load address.
744c0981da4SDimitry Andric if (load_address != LLDB_INVALID_ADDRESS) {
745c0981da4SDimitry Andric // If the load address is external, we're done.
74614f1b3e8SDimitry Andric const bool is_external =
74714f1b3e8SDimitry Andric (candidate_sc.function) ||
748f3fbd1c0SDimitry Andric (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
749c0981da4SDimitry Andric if (is_external)
750c0981da4SDimitry Andric return load_address;
751f3fbd1c0SDimitry Andric
752c0981da4SDimitry Andric // Otherwise, remember the best internal load address.
753c0981da4SDimitry Andric if (m_best_internal_load_address == LLDB_INVALID_ADDRESS)
754c0981da4SDimitry Andric m_best_internal_load_address = load_address;
755f3fbd1c0SDimitry Andric }
756f3fbd1c0SDimitry Andric }
757f3fbd1c0SDimitry Andric
7585f29bb8aSDimitry Andric // You test the address of a weak symbol against NULL to see if it is
7595f29bb8aSDimitry Andric // present. So we should return 0 for a missing weak symbol.
760c0981da4SDimitry Andric if (m_symbol_was_missing_weak)
761c0981da4SDimitry Andric return 0;
762c0981da4SDimitry Andric
763e3b55780SDimitry Andric return std::nullopt;
7645f29bb8aSDimitry Andric }
7655f29bb8aSDimitry Andric
GetBestInternalLoadAddress() const766c0981da4SDimitry Andric lldb::addr_t GetBestInternalLoadAddress() const {
767c0981da4SDimitry Andric return m_best_internal_load_address;
768c0981da4SDimitry Andric }
769c0981da4SDimitry Andric
770c0981da4SDimitry Andric private:
771c0981da4SDimitry Andric Target *m_target;
772c0981da4SDimitry Andric bool &m_symbol_was_missing_weak;
773c0981da4SDimitry Andric lldb::addr_t m_best_internal_load_address = LLDB_INVALID_ADDRESS;
774f3fbd1c0SDimitry Andric };
775f3fbd1c0SDimitry Andric
776c0981da4SDimitry Andric lldb::addr_t
FindInSymbols(const std::vector<ConstString> & names,const lldb_private::SymbolContext & sc,bool & symbol_was_missing_weak)777c0981da4SDimitry Andric IRExecutionUnit::FindInSymbols(const std::vector<ConstString> &names,
778c0981da4SDimitry Andric const lldb_private::SymbolContext &sc,
779c0981da4SDimitry Andric bool &symbol_was_missing_weak) {
780c0981da4SDimitry Andric symbol_was_missing_weak = false;
781c0981da4SDimitry Andric
782c0981da4SDimitry Andric Target *target = sc.target_sp.get();
783c0981da4SDimitry Andric if (!target) {
784c0981da4SDimitry Andric // We shouldn't be doing any symbol lookup at all without a target.
785c0981da4SDimitry Andric return LLDB_INVALID_ADDRESS;
786c0981da4SDimitry Andric }
787c0981da4SDimitry Andric
788c0981da4SDimitry Andric LoadAddressResolver resolver(target, symbol_was_missing_weak);
789c0981da4SDimitry Andric
790c0981da4SDimitry Andric ModuleFunctionSearchOptions function_options;
791c0981da4SDimitry Andric function_options.include_symbols = true;
792c0981da4SDimitry Andric function_options.include_inlines = false;
793c0981da4SDimitry Andric
794c0981da4SDimitry Andric for (const ConstString &name : names) {
79514f1b3e8SDimitry Andric if (sc.module_sp) {
796c0981da4SDimitry Andric SymbolContextList sc_list;
797c0981da4SDimitry Andric sc.module_sp->FindFunctions(name, CompilerDeclContext(),
798c0981da4SDimitry Andric lldb::eFunctionNameTypeFull, function_options,
799f3fbd1c0SDimitry Andric sc_list);
800c0981da4SDimitry Andric if (auto load_addr = resolver.Resolve(sc_list))
801c0981da4SDimitry Andric return *load_addr;
802f3fbd1c0SDimitry Andric }
803f3fbd1c0SDimitry Andric
804c0981da4SDimitry Andric if (sc.target_sp) {
805c0981da4SDimitry Andric SymbolContextList sc_list;
806c0981da4SDimitry Andric sc.target_sp->GetImages().FindFunctions(name, lldb::eFunctionNameTypeFull,
807c0981da4SDimitry Andric function_options, sc_list);
808c0981da4SDimitry Andric if (auto load_addr = resolver.Resolve(sc_list))
809c0981da4SDimitry Andric return *load_addr;
810f3fbd1c0SDimitry Andric }
811f3fbd1c0SDimitry Andric
812c0981da4SDimitry Andric if (sc.target_sp) {
813c0981da4SDimitry Andric SymbolContextList sc_list;
81414f1b3e8SDimitry Andric sc.target_sp->GetImages().FindSymbolsWithNameAndType(
815c0981da4SDimitry Andric name, lldb::eSymbolTypeAny, sc_list);
816c0981da4SDimitry Andric if (auto load_addr = resolver.Resolve(sc_list))
817c0981da4SDimitry Andric return *load_addr;
818f3fbd1c0SDimitry Andric }
819f3fbd1c0SDimitry Andric
820c0981da4SDimitry Andric lldb::addr_t best_internal_load_address =
821c0981da4SDimitry Andric resolver.GetBestInternalLoadAddress();
822c0981da4SDimitry Andric if (best_internal_load_address != LLDB_INVALID_ADDRESS)
823f3fbd1c0SDimitry Andric return best_internal_load_address;
824f3fbd1c0SDimitry Andric }
825f3fbd1c0SDimitry Andric
826f3fbd1c0SDimitry Andric return LLDB_INVALID_ADDRESS;
827f3fbd1c0SDimitry Andric }
828f3fbd1c0SDimitry Andric
829f3fbd1c0SDimitry Andric lldb::addr_t
FindInRuntimes(const std::vector<ConstString> & names,const lldb_private::SymbolContext & sc)830c0981da4SDimitry Andric IRExecutionUnit::FindInRuntimes(const std::vector<ConstString> &names,
83114f1b3e8SDimitry Andric const lldb_private::SymbolContext &sc) {
832f3fbd1c0SDimitry Andric lldb::TargetSP target_sp = sc.target_sp;
833f3fbd1c0SDimitry Andric
83414f1b3e8SDimitry Andric if (!target_sp) {
835f3fbd1c0SDimitry Andric return LLDB_INVALID_ADDRESS;
836f3fbd1c0SDimitry Andric }
837f3fbd1c0SDimitry Andric
838f3fbd1c0SDimitry Andric lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
839f3fbd1c0SDimitry Andric
84014f1b3e8SDimitry Andric if (!process_sp) {
841f3fbd1c0SDimitry Andric return LLDB_INVALID_ADDRESS;
842f3fbd1c0SDimitry Andric }
843f3fbd1c0SDimitry Andric
844c0981da4SDimitry Andric for (const ConstString &name : names) {
8455f29bb8aSDimitry Andric for (LanguageRuntime *runtime : process_sp->GetLanguageRuntimes()) {
846c0981da4SDimitry Andric lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(name);
847f3fbd1c0SDimitry Andric
848f3fbd1c0SDimitry Andric if (symbol_load_addr != LLDB_INVALID_ADDRESS)
849f3fbd1c0SDimitry Andric return symbol_load_addr;
850f3fbd1c0SDimitry Andric }
851f3fbd1c0SDimitry Andric }
852f3fbd1c0SDimitry Andric
853f3fbd1c0SDimitry Andric return LLDB_INVALID_ADDRESS;
854f3fbd1c0SDimitry Andric }
855f3fbd1c0SDimitry Andric
FindInUserDefinedSymbols(const std::vector<ConstString> & names,const lldb_private::SymbolContext & sc)85614f1b3e8SDimitry Andric lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
857c0981da4SDimitry Andric const std::vector<ConstString> &names,
85814f1b3e8SDimitry Andric const lldb_private::SymbolContext &sc) {
859f3fbd1c0SDimitry Andric lldb::TargetSP target_sp = sc.target_sp;
860f3fbd1c0SDimitry Andric
861c0981da4SDimitry Andric for (const ConstString &name : names) {
862c0981da4SDimitry Andric lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(name);
863f3fbd1c0SDimitry Andric
864f3fbd1c0SDimitry Andric if (symbol_load_addr != LLDB_INVALID_ADDRESS)
865f3fbd1c0SDimitry Andric return symbol_load_addr;
866f3fbd1c0SDimitry Andric }
867f3fbd1c0SDimitry Andric
868f3fbd1c0SDimitry Andric return LLDB_INVALID_ADDRESS;
869f3fbd1c0SDimitry Andric }
870f3fbd1c0SDimitry Andric
FindSymbol(lldb_private::ConstString name,bool & missing_weak)871c0981da4SDimitry Andric lldb::addr_t IRExecutionUnit::FindSymbol(lldb_private::ConstString name,
872c0981da4SDimitry Andric bool &missing_weak) {
873c0981da4SDimitry Andric std::vector<ConstString> candidate_C_names;
874c0981da4SDimitry Andric std::vector<ConstString> candidate_CPlusPlus_names;
875f3fbd1c0SDimitry Andric
876f3fbd1c0SDimitry Andric CollectCandidateCNames(candidate_C_names, name);
877f3fbd1c0SDimitry Andric
8785f29bb8aSDimitry Andric lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx, missing_weak);
8795f29bb8aSDimitry Andric if (ret != LLDB_INVALID_ADDRESS)
8805f29bb8aSDimitry Andric return ret;
8815f29bb8aSDimitry Andric
8825f29bb8aSDimitry Andric // If we find the symbol in runtimes or user defined symbols it can't be
8835f29bb8aSDimitry Andric // a missing weak symbol.
8845f29bb8aSDimitry Andric missing_weak = false;
885f3fbd1c0SDimitry Andric ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
8865f29bb8aSDimitry Andric if (ret != LLDB_INVALID_ADDRESS)
8875f29bb8aSDimitry Andric return ret;
888f3fbd1c0SDimitry Andric
889f3fbd1c0SDimitry Andric ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
8905f29bb8aSDimitry Andric if (ret != LLDB_INVALID_ADDRESS)
8915f29bb8aSDimitry Andric return ret;
892f3fbd1c0SDimitry Andric
89314f1b3e8SDimitry Andric CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
89414f1b3e8SDimitry Andric m_sym_ctx);
8955f29bb8aSDimitry Andric ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx, missing_weak);
896f3fbd1c0SDimitry Andric return ret;
897f3fbd1c0SDimitry Andric }
898f3fbd1c0SDimitry Andric
GetStaticInitializers(std::vector<lldb::addr_t> & static_initializers)89914f1b3e8SDimitry Andric void IRExecutionUnit::GetStaticInitializers(
90014f1b3e8SDimitry Andric std::vector<lldb::addr_t> &static_initializers) {
901145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
902706b4fc4SDimitry Andric
903706b4fc4SDimitry Andric llvm::GlobalVariable *global_ctors =
904706b4fc4SDimitry Andric m_module->getNamedGlobal("llvm.global_ctors");
905706b4fc4SDimitry Andric if (!global_ctors) {
906706b4fc4SDimitry Andric LLDB_LOG(log, "Couldn't find llvm.global_ctors.");
907706b4fc4SDimitry Andric return;
908706b4fc4SDimitry Andric }
909706b4fc4SDimitry Andric auto *ctor_array =
910706b4fc4SDimitry Andric llvm::dyn_cast<llvm::ConstantArray>(global_ctors->getInitializer());
911706b4fc4SDimitry Andric if (!ctor_array) {
912706b4fc4SDimitry Andric LLDB_LOG(log, "llvm.global_ctors not a ConstantArray.");
913706b4fc4SDimitry Andric return;
914706b4fc4SDimitry Andric }
915706b4fc4SDimitry Andric
91614f1b3e8SDimitry Andric for (llvm::Use &ctor_use : ctor_array->operands()) {
917706b4fc4SDimitry Andric auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(ctor_use);
918706b4fc4SDimitry Andric if (!ctor_struct)
919706b4fc4SDimitry Andric continue;
920706b4fc4SDimitry Andric // this is standardized
921706b4fc4SDimitry Andric lldbassert(ctor_struct->getNumOperands() == 3);
922706b4fc4SDimitry Andric auto *ctor_function =
923706b4fc4SDimitry Andric llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1));
924706b4fc4SDimitry Andric if (!ctor_function) {
925706b4fc4SDimitry Andric LLDB_LOG(log, "global_ctor doesn't contain an llvm::Function");
926706b4fc4SDimitry Andric continue;
927706b4fc4SDimitry Andric }
928706b4fc4SDimitry Andric
929706b4fc4SDimitry Andric ConstString ctor_function_name(ctor_function->getName().str());
930706b4fc4SDimitry Andric LLDB_LOG(log, "Looking for callable jitted function with name {0}.",
931706b4fc4SDimitry Andric ctor_function_name);
932f3fbd1c0SDimitry Andric
93314f1b3e8SDimitry Andric for (JittedFunction &jitted_function : m_jitted_functions) {
934706b4fc4SDimitry Andric if (ctor_function_name != jitted_function.m_name)
935706b4fc4SDimitry Andric continue;
936706b4fc4SDimitry Andric if (jitted_function.m_remote_addr == LLDB_INVALID_ADDRESS) {
937706b4fc4SDimitry Andric LLDB_LOG(log, "Found jitted function with invalid address.");
938706b4fc4SDimitry Andric continue;
939706b4fc4SDimitry Andric }
940f3fbd1c0SDimitry Andric static_initializers.push_back(jitted_function.m_remote_addr);
941706b4fc4SDimitry Andric LLDB_LOG(log, "Calling function at address {0:x}.",
942706b4fc4SDimitry Andric jitted_function.m_remote_addr);
943f3fbd1c0SDimitry Andric break;
944f3fbd1c0SDimitry Andric }
945f3fbd1c0SDimitry Andric }
946f3fbd1c0SDimitry Andric }
947f3fbd1c0SDimitry Andric
9485f29bb8aSDimitry Andric llvm::JITSymbol
findSymbol(const std::string & Name)9495f29bb8aSDimitry Andric IRExecutionUnit::MemoryManager::findSymbol(const std::string &Name) {
9505f29bb8aSDimitry Andric bool missing_weak = false;
9515f29bb8aSDimitry Andric uint64_t addr = GetSymbolAddressAndPresence(Name, missing_weak);
9525f29bb8aSDimitry Andric // This is a weak symbol:
9535f29bb8aSDimitry Andric if (missing_weak)
9545f29bb8aSDimitry Andric return llvm::JITSymbol(addr,
9555f29bb8aSDimitry Andric llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak);
9565f29bb8aSDimitry Andric else
9575f29bb8aSDimitry Andric return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported);
9585f29bb8aSDimitry Andric }
9595f29bb8aSDimitry Andric
96012bd4897SEd Maste uint64_t
getSymbolAddress(const std::string & Name)96114f1b3e8SDimitry Andric IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
9625f29bb8aSDimitry Andric bool missing_weak = false;
9635f29bb8aSDimitry Andric return GetSymbolAddressAndPresence(Name, missing_weak);
9645f29bb8aSDimitry Andric }
9655f29bb8aSDimitry Andric
9665f29bb8aSDimitry Andric uint64_t
GetSymbolAddressAndPresence(const std::string & Name,bool & missing_weak)9675f29bb8aSDimitry Andric IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence(
9685f29bb8aSDimitry Andric const std::string &Name, bool &missing_weak) {
969145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
97012bd4897SEd Maste
971f3fbd1c0SDimitry Andric ConstString name_cs(Name.c_str());
97212bd4897SEd Maste
9735f29bb8aSDimitry Andric lldb::addr_t ret = m_parent.FindSymbol(name_cs, missing_weak);
97412bd4897SEd Maste
97514f1b3e8SDimitry Andric if (ret == LLDB_INVALID_ADDRESS) {
976ead24645SDimitry Andric LLDB_LOGF(log,
97714f1b3e8SDimitry Andric "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
978f3fbd1c0SDimitry Andric Name.c_str());
97912bd4897SEd Maste
980f3fbd1c0SDimitry Andric m_parent.ReportSymbolLookupError(name_cs);
98194994d37SDimitry Andric return 0;
98214f1b3e8SDimitry Andric } else {
983ead24645SDimitry Andric LLDB_LOGF(log, "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
98414f1b3e8SDimitry Andric Name.c_str(), ret);
985f3fbd1c0SDimitry Andric return ret;
986f3fbd1c0SDimitry Andric }
98712bd4897SEd Maste }
98812bd4897SEd Maste
getPointerToNamedFunction(const std::string & Name,bool AbortOnFailure)98914f1b3e8SDimitry Andric void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
99014f1b3e8SDimitry Andric const std::string &Name, bool AbortOnFailure) {
99112bd4897SEd Maste return (void *)getSymbolAddress(Name);
99212bd4897SEd Maste }
99312bd4897SEd Maste
994f034231aSEd Maste lldb::addr_t
GetRemoteAddressForLocal(lldb::addr_t local_address)99514f1b3e8SDimitry Andric IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
996145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Expressions);
997f034231aSEd Maste
99814f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
999f034231aSEd Maste if (local_address >= record.m_host_address &&
100014f1b3e8SDimitry Andric local_address < record.m_host_address + record.m_size) {
1001f034231aSEd Maste if (record.m_process_address == LLDB_INVALID_ADDRESS)
1002f034231aSEd Maste return LLDB_INVALID_ADDRESS;
1003f034231aSEd Maste
100414f1b3e8SDimitry Andric lldb::addr_t ret =
100514f1b3e8SDimitry Andric record.m_process_address + (local_address - record.m_host_address);
1006f034231aSEd Maste
1007ead24645SDimitry Andric LLDB_LOGF(log,
100814f1b3e8SDimitry Andric "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
100914f1b3e8SDimitry Andric " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
101014f1b3e8SDimitry Andric " from [0x%" PRIx64 "..0x%" PRIx64 "].",
101114f1b3e8SDimitry Andric local_address, (uint64_t)record.m_host_address,
101214f1b3e8SDimitry Andric (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1013ead24645SDimitry Andric record.m_process_address,
1014ead24645SDimitry Andric record.m_process_address + record.m_size);
1015f034231aSEd Maste
1016f034231aSEd Maste return ret;
1017f034231aSEd Maste }
1018f034231aSEd Maste }
1019f034231aSEd Maste
1020f034231aSEd Maste return LLDB_INVALID_ADDRESS;
1021f034231aSEd Maste }
1022f034231aSEd Maste
1023f034231aSEd Maste IRExecutionUnit::AddrRange
GetRemoteRangeForLocal(lldb::addr_t local_address)102414f1b3e8SDimitry Andric IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
102514f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
1026f034231aSEd Maste if (local_address >= record.m_host_address &&
102714f1b3e8SDimitry Andric local_address < record.m_host_address + record.m_size) {
1028f034231aSEd Maste if (record.m_process_address == LLDB_INVALID_ADDRESS)
1029f034231aSEd Maste return AddrRange(0, 0);
1030f034231aSEd Maste
1031f034231aSEd Maste return AddrRange(record.m_process_address, record.m_size);
1032f034231aSEd Maste }
1033f034231aSEd Maste }
1034f034231aSEd Maste
1035f034231aSEd Maste return AddrRange(0, 0);
1036f034231aSEd Maste }
1037f034231aSEd Maste
CommitOneAllocation(lldb::ProcessSP & process_sp,Status & error,AllocationRecord & record)103814f1b3e8SDimitry Andric bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1039b76161e4SDimitry Andric Status &error,
104014f1b3e8SDimitry Andric AllocationRecord &record) {
104114f1b3e8SDimitry Andric if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1042f3fbd1c0SDimitry Andric return true;
1043f3fbd1c0SDimitry Andric }
1044f034231aSEd Maste
104514f1b3e8SDimitry Andric switch (record.m_sect_type) {
10460cac4ca3SEd Maste case lldb::eSectionTypeInvalid:
10470cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugAbbrev:
1048e81d9d49SDimitry Andric case lldb::eSectionTypeDWARFDebugAddr:
10490cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugAranges:
1050ef5d0b5eSDimitry Andric case lldb::eSectionTypeDWARFDebugCuIndex:
10510cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugFrame:
10520cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugInfo:
10530cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugLine:
10540cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugLoc:
105594994d37SDimitry Andric case lldb::eSectionTypeDWARFDebugLocLists:
10560cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugMacInfo:
10570cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugPubNames:
10580cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugPubTypes:
10590cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugRanges:
10600cac4ca3SEd Maste case lldb::eSectionTypeDWARFDebugStr:
1061e81d9d49SDimitry Andric case lldb::eSectionTypeDWARFDebugStrOffsets:
10620cac4ca3SEd Maste case lldb::eSectionTypeDWARFAppleNames:
10630cac4ca3SEd Maste case lldb::eSectionTypeDWARFAppleTypes:
10640cac4ca3SEd Maste case lldb::eSectionTypeDWARFAppleNamespaces:
10650cac4ca3SEd Maste case lldb::eSectionTypeDWARFAppleObjC:
1066f73363f1SDimitry Andric case lldb::eSectionTypeDWARFGNUDebugAltLink:
1067f3fbd1c0SDimitry Andric error.Clear();
10680cac4ca3SEd Maste break;
10690cac4ca3SEd Maste default:
1070e81d9d49SDimitry Andric const bool zero_memory = false;
107114f1b3e8SDimitry Andric record.m_process_address =
107214f1b3e8SDimitry Andric Malloc(record.m_size, record.m_alignment, record.m_permissions,
107314f1b3e8SDimitry Andric eAllocationPolicyProcessOnly, zero_memory, error);
10740cac4ca3SEd Maste break;
10750cac4ca3SEd Maste }
1076f034231aSEd Maste
1077f3fbd1c0SDimitry Andric return error.Success();
1078f3fbd1c0SDimitry Andric }
1079f3fbd1c0SDimitry Andric
CommitAllocations(lldb::ProcessSP & process_sp)108014f1b3e8SDimitry Andric bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1081f3fbd1c0SDimitry Andric bool ret = true;
1082f3fbd1c0SDimitry Andric
1083b76161e4SDimitry Andric lldb_private::Status err;
1084f3fbd1c0SDimitry Andric
108514f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
1086f3fbd1c0SDimitry Andric ret = CommitOneAllocation(process_sp, err, record);
1087f3fbd1c0SDimitry Andric
108814f1b3e8SDimitry Andric if (!ret) {
1089f034231aSEd Maste break;
1090f034231aSEd Maste }
1091f034231aSEd Maste }
1092f034231aSEd Maste
109314f1b3e8SDimitry Andric if (!ret) {
109414f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
109514f1b3e8SDimitry Andric if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1096f034231aSEd Maste Free(record.m_process_address, err);
1097f034231aSEd Maste record.m_process_address = LLDB_INVALID_ADDRESS;
1098f034231aSEd Maste }
1099f034231aSEd Maste }
1100f034231aSEd Maste }
1101f034231aSEd Maste
1102f034231aSEd Maste return ret;
1103f034231aSEd Maste }
1104f034231aSEd Maste
ReportAllocations(llvm::ExecutionEngine & engine)110514f1b3e8SDimitry Andric void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1106f3fbd1c0SDimitry Andric m_reported_allocations = true;
1107f3fbd1c0SDimitry Andric
110814f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
1109f034231aSEd Maste if (record.m_process_address == LLDB_INVALID_ADDRESS)
1110f034231aSEd Maste continue;
1111f034231aSEd Maste
1112f034231aSEd Maste if (record.m_section_id == eSectionIDInvalid)
1113f034231aSEd Maste continue;
1114f034231aSEd Maste
111514f1b3e8SDimitry Andric engine.mapSectionAddress((void *)record.m_host_address,
111614f1b3e8SDimitry Andric record.m_process_address);
1117f034231aSEd Maste }
1118f034231aSEd Maste
1119f034231aSEd Maste // Trigger re-application of relocations.
1120f034231aSEd Maste engine.finalizeObject();
1121f034231aSEd Maste }
1122f034231aSEd Maste
WriteData(lldb::ProcessSP & process_sp)112314f1b3e8SDimitry Andric bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
11240cac4ca3SEd Maste bool wrote_something = false;
112514f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
112614f1b3e8SDimitry Andric if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1127b76161e4SDimitry Andric lldb_private::Status err;
112814f1b3e8SDimitry Andric WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
112914f1b3e8SDimitry Andric record.m_size, err);
11300cac4ca3SEd Maste if (err.Success())
11310cac4ca3SEd Maste wrote_something = true;
1132f034231aSEd Maste }
11330cac4ca3SEd Maste }
11340cac4ca3SEd Maste return wrote_something;
1135f034231aSEd Maste }
1136f034231aSEd Maste
dump(Log * log)113714f1b3e8SDimitry Andric void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1138f034231aSEd Maste if (!log)
1139f034231aSEd Maste return;
1140f034231aSEd Maste
1141ead24645SDimitry Andric LLDB_LOGF(log,
1142ead24645SDimitry Andric "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
114314f1b3e8SDimitry Andric (unsigned long long)m_host_address, (unsigned long long)m_size,
114414f1b3e8SDimitry Andric (unsigned long long)m_process_address, (unsigned)m_alignment,
114514f1b3e8SDimitry Andric (unsigned)m_section_id, m_name.c_str());
1146f034231aSEd Maste }
11470cac4ca3SEd Maste
GetByteOrder() const114814f1b3e8SDimitry Andric lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
11490cac4ca3SEd Maste ExecutionContext exe_ctx(GetBestExecutionContextScope());
11500cac4ca3SEd Maste return exe_ctx.GetByteOrder();
11510cac4ca3SEd Maste }
11520cac4ca3SEd Maste
GetAddressByteSize() const115314f1b3e8SDimitry Andric uint32_t IRExecutionUnit::GetAddressByteSize() const {
11540cac4ca3SEd Maste ExecutionContext exe_ctx(GetBestExecutionContextScope());
11550cac4ca3SEd Maste return exe_ctx.GetAddressByteSize();
11560cac4ca3SEd Maste }
11570cac4ca3SEd Maste
PopulateSymtab(lldb_private::ObjectFile * obj_file,lldb_private::Symtab & symtab)115814f1b3e8SDimitry Andric void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
115914f1b3e8SDimitry Andric lldb_private::Symtab &symtab) {
11600cac4ca3SEd Maste // No symbols yet...
11610cac4ca3SEd Maste }
11620cac4ca3SEd Maste
PopulateSectionList(lldb_private::ObjectFile * obj_file,lldb_private::SectionList & section_list)116314f1b3e8SDimitry Andric void IRExecutionUnit::PopulateSectionList(
116414f1b3e8SDimitry Andric lldb_private::ObjectFile *obj_file,
116514f1b3e8SDimitry Andric lldb_private::SectionList §ion_list) {
116614f1b3e8SDimitry Andric for (AllocationRecord &record : m_records) {
116714f1b3e8SDimitry Andric if (record.m_size > 0) {
116814f1b3e8SDimitry Andric lldb::SectionSP section_sp(new lldb_private::Section(
116914f1b3e8SDimitry Andric obj_file->GetModule(), obj_file, record.m_section_id,
117014f1b3e8SDimitry Andric ConstString(record.m_name), record.m_sect_type,
117114f1b3e8SDimitry Andric record.m_process_address, record.m_size,
117214f1b3e8SDimitry Andric record.m_host_address, // file_offset (which is the host address for
117314f1b3e8SDimitry Andric // the data)
11740cac4ca3SEd Maste record.m_size, // file_size
11750cac4ca3SEd Maste 0,
11760cac4ca3SEd Maste record.m_permissions)); // flags
11770cac4ca3SEd Maste section_list.AddSection(section_sp);
11780cac4ca3SEd Maste }
11790cac4ca3SEd Maste }
11800cac4ca3SEd Maste }
11810cac4ca3SEd Maste
GetArchitecture()118294994d37SDimitry Andric ArchSpec IRExecutionUnit::GetArchitecture() {
11830cac4ca3SEd Maste ExecutionContext exe_ctx(GetBestExecutionContextScope());
118494994d37SDimitry Andric if(Target *target = exe_ctx.GetTargetPtr())
118594994d37SDimitry Andric return target->GetArchitecture();
118694994d37SDimitry Andric return ArchSpec();
11870cac4ca3SEd Maste }
11880cac4ca3SEd Maste
GetJITModule()118914f1b3e8SDimitry Andric lldb::ModuleSP IRExecutionUnit::GetJITModule() {
11900cac4ca3SEd Maste ExecutionContext exe_ctx(GetBestExecutionContextScope());
11910cac4ca3SEd Maste Target *target = exe_ctx.GetTargetPtr();
1192f73363f1SDimitry Andric if (!target)
1193f73363f1SDimitry Andric return nullptr;
1194f73363f1SDimitry Andric
1195f73363f1SDimitry Andric auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1196f73363f1SDimitry Andric shared_from_this());
1197f73363f1SDimitry Andric
1198f73363f1SDimitry Andric lldb::ModuleSP jit_module_sp =
1199f73363f1SDimitry Andric lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1200f73363f1SDimitry Andric if (!jit_module_sp)
1201f73363f1SDimitry Andric return nullptr;
1202f73363f1SDimitry Andric
12030cac4ca3SEd Maste bool changed = false;
12040cac4ca3SEd Maste jit_module_sp->SetLoadAddress(*target, 0, true, changed);
12050cac4ca3SEd Maste return jit_module_sp;
12060cac4ca3SEd Maste }
1207