1//===-- IRExecutionUnit.cpp -----------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ExecutionEngine/ExecutionEngine.h"
10#include "llvm/ExecutionEngine/ObjectCache.h"
11#include "llvm/IR/Constants.h"
12#include "llvm/IR/DiagnosticHandler.h"
13#include "llvm/IR/DiagnosticInfo.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/SourceMgr.h"
17#include "llvm/Support/raw_ostream.h"
18
19#include "lldb/Core/Debugger.h"
20#include "lldb/Core/Disassembler.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Core/Section.h"
23#include "lldb/Expression/IRExecutionUnit.h"
24#include "lldb/Expression/ObjectFileJIT.h"
25#include "lldb/Host/HostInfo.h"
26#include "lldb/Symbol/CompileUnit.h"
27#include "lldb/Symbol/SymbolContext.h"
28#include "lldb/Symbol/SymbolFile.h"
29#include "lldb/Symbol/SymbolVendor.h"
30#include "lldb/Target/ExecutionContext.h"
31#include "lldb/Target/Language.h"
32#include "lldb/Target/LanguageRuntime.h"
33#include "lldb/Target/Target.h"
34#include "lldb/Utility/DataBufferHeap.h"
35#include "lldb/Utility/DataExtractor.h"
36#include "lldb/Utility/LLDBAssert.h"
37#include "lldb/Utility/LLDBLog.h"
38#include "lldb/Utility/Log.h"
39
40#include <optional>
41
42using namespace lldb_private;
43
44IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up,
45 std::unique_ptr<llvm::Module> &module_up,
46 ConstString &name,
47 const lldb::TargetSP &target_sp,
48 const SymbolContext &sym_ctx,
49 std::vector<std::string> &cpu_features)
50 : IRMemoryMap(target_sp), m_context_up(context_up.release()),
51 m_module_up(module_up.release()), m_module(m_module_up.get()),
52 m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
53 m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
54 m_function_end_load_addr(LLDB_INVALID_ADDRESS),
55 m_reported_allocations(false) {}
56
57lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
58 Status &error) {
59 const bool zero_memory = false;
60 lldb::addr_t allocation_process_addr =
61 Malloc(size, alignment: 8, permissions: lldb::ePermissionsWritable | lldb::ePermissionsReadable,
62 policy: eAllocationPolicyMirror, zero_memory, error);
63
64 if (!error.Success())
65 return LLDB_INVALID_ADDRESS;
66
67 WriteMemory(process_address: allocation_process_addr, bytes, size, error);
68
69 if (!error.Success()) {
70 Status err;
71 Free(process_address: allocation_process_addr, error&: err);
72
73 return LLDB_INVALID_ADDRESS;
74 }
75
76 if (Log *log = GetLog(mask: LLDBLog::Expressions)) {
77 DataBufferHeap my_buffer(size, 0);
78 Status err;
79 ReadMemory(bytes: my_buffer.GetBytes(), process_address: allocation_process_addr, size, error&: err);
80
81 if (err.Success()) {
82 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(),
83 lldb::eByteOrderBig, 8);
84 my_extractor.PutToLog(log, offset: 0, length: my_buffer.GetByteSize(),
85 base_addr: allocation_process_addr, num_per_line: 16,
86 type: DataExtractor::TypeUInt8);
87 }
88 }
89
90 return allocation_process_addr;
91}
92
93void IRExecutionUnit::FreeNow(lldb::addr_t allocation) {
94 if (allocation == LLDB_INVALID_ADDRESS)
95 return;
96
97 Status err;
98
99 Free(process_address: allocation, error&: err);
100}
101
102Status IRExecutionUnit::DisassembleFunction(Stream &stream,
103 lldb::ProcessSP &process_wp) {
104 Log *log = GetLog(mask: LLDBLog::Expressions);
105
106 ExecutionContext exe_ctx(process_wp);
107
108 Status ret;
109
110 ret.Clear();
111
112 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
113 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
114
115 for (JittedFunction &function : m_jitted_functions) {
116 if (function.m_name == m_name) {
117 func_local_addr = function.m_local_addr;
118 func_remote_addr = function.m_remote_addr;
119 }
120 }
121
122 if (func_local_addr == LLDB_INVALID_ADDRESS) {
123 ret.SetErrorToGenericError();
124 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly",
125 m_name.AsCString());
126 return ret;
127 }
128
129 LLDB_LOGF(log,
130 "Found function, has local address 0x%" PRIx64
131 " and remote address 0x%" PRIx64,
132 (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
133
134 std::pair<lldb::addr_t, lldb::addr_t> func_range;
135
136 func_range = GetRemoteRangeForLocal(local_address: func_local_addr);
137
138 if (func_range.first == 0 && func_range.second == 0) {
139 ret.SetErrorToGenericError();
140 ret.SetErrorStringWithFormat("Couldn't find code range for function %s",
141 m_name.AsCString());
142 return ret;
143 }
144
145 LLDB_LOGF(log, "Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
146 func_range.first, func_range.second);
147
148 Target *target = exe_ctx.GetTargetPtr();
149 if (!target) {
150 ret.SetErrorToGenericError();
151 ret.SetErrorString("Couldn't find the target");
152 return ret;
153 }
154
155 lldb::WritableDataBufferSP buffer_sp(
156 new DataBufferHeap(func_range.second, 0));
157
158 Process *process = exe_ctx.GetProcessPtr();
159 Status err;
160 process->ReadMemory(vm_addr: func_remote_addr, buf: buffer_sp->GetBytes(),
161 size: buffer_sp->GetByteSize(), error&: err);
162
163 if (!err.Success()) {
164 ret.SetErrorToGenericError();
165 ret.SetErrorStringWithFormat("Couldn't read from process: %s",
166 err.AsCString(default_error_str: "unknown error"));
167 return ret;
168 }
169
170 ArchSpec arch(target->GetArchitecture());
171
172 const char *plugin_name = nullptr;
173 const char *flavor_string = nullptr;
174 lldb::DisassemblerSP disassembler_sp =
175 Disassembler::FindPlugin(arch, flavor: flavor_string, plugin_name);
176
177 if (!disassembler_sp) {
178 ret.SetErrorToGenericError();
179 ret.SetErrorStringWithFormat(
180 "Unable to find disassembler plug-in for %s architecture.",
181 arch.GetArchitectureName());
182 return ret;
183 }
184
185 if (!process) {
186 ret.SetErrorToGenericError();
187 ret.SetErrorString("Couldn't find the process");
188 return ret;
189 }
190
191 DataExtractor extractor(buffer_sp, process->GetByteOrder(),
192 target->GetArchitecture().GetAddressByteSize());
193
194 if (log) {
195 LLDB_LOGF(log, "Function data has contents:");
196 extractor.PutToLog(log, offset: 0, length: extractor.GetByteSize(), base_addr: func_remote_addr, num_per_line: 16,
197 type: DataExtractor::TypeUInt8);
198 }
199
200 disassembler_sp->DecodeInstructions(base_addr: Address(func_remote_addr), data: extractor, data_offset: 0,
201 UINT32_MAX, append: false, data_from_file: false);
202
203 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
204 instruction_list.Dump(s: &stream, show_address: true, show_bytes: true, /*show_control_flow_kind=*/false,
205 exe_ctx: &exe_ctx);
206
207 return ret;
208}
209
210namespace {
211struct IRExecDiagnosticHandler : public llvm::DiagnosticHandler {
212 Status *err;
213 IRExecDiagnosticHandler(Status *err) : err(err) {}
214 bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override {
215 if (DI.getSeverity() == llvm::DS_Error) {
216 const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(Val: DI);
217 if (err && err->Success()) {
218 err->SetErrorToGenericError();
219 err->SetErrorStringWithFormat(
220 "IRExecution error: %s",
221 DISM.getSMDiag().getMessage().str().c_str());
222 }
223 }
224
225 return true;
226 }
227};
228} // namespace
229
230void IRExecutionUnit::ReportSymbolLookupError(ConstString name) {
231 m_failed_lookups.push_back(x: name);
232}
233
234void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
235 lldb::addr_t &func_end) {
236 lldb::ProcessSP process_sp(GetProcessWP().lock());
237
238 static std::recursive_mutex s_runnable_info_mutex;
239
240 func_addr = LLDB_INVALID_ADDRESS;
241 func_end = LLDB_INVALID_ADDRESS;
242
243 if (!process_sp) {
244 error.SetErrorToGenericError();
245 error.SetErrorString("Couldn't write the JIT compiled code into the "
246 "process because the process is invalid");
247 return;
248 }
249
250 if (m_did_jit) {
251 func_addr = m_function_load_addr;
252 func_end = m_function_end_load_addr;
253
254 return;
255 };
256
257 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
258
259 m_did_jit = true;
260
261 Log *log = GetLog(mask: LLDBLog::Expressions);
262
263 std::string error_string;
264
265 if (log) {
266 std::string s;
267 llvm::raw_string_ostream oss(s);
268
269 m_module->print(OS&: oss, AAW: nullptr);
270
271 oss.flush();
272
273 LLDB_LOGF(log, "Module being sent to JIT: \n%s", s.c_str());
274 }
275
276 m_module_up->getContext().setDiagnosticHandler(
277 DH: std::make_unique<IRExecDiagnosticHandler>(args: &error));
278
279 llvm::EngineBuilder builder(std::move(m_module_up));
280 llvm::Triple triple(m_module->getTargetTriple());
281
282 builder.setEngineKind(llvm::EngineKind::JIT)
283 .setErrorStr(&error_string)
284 .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_
285 : llvm::Reloc::Static)
286 .setMCJITMemoryManager(std::make_unique<MemoryManager>(args&: *this))
287 .setOptLevel(llvm::CodeGenOptLevel::Less);
288
289 llvm::StringRef mArch;
290 llvm::StringRef mCPU;
291 llvm::SmallVector<std::string, 0> mAttrs;
292
293 for (std::string &feature : m_cpu_features)
294 mAttrs.push_back(Elt: feature);
295
296 llvm::TargetMachine *target_machine =
297 builder.selectTarget(TargetTriple: triple, MArch: mArch, MCPU: mCPU, MAttrs: mAttrs);
298
299 m_execution_engine_up.reset(p: builder.create(TM: target_machine));
300
301 if (!m_execution_engine_up) {
302 error.SetErrorToGenericError();
303 error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
304 error_string.c_str());
305 return;
306 }
307
308 m_strip_underscore =
309 (m_execution_engine_up->getDataLayout().getGlobalPrefix() == '_');
310
311 class ObjectDumper : public llvm::ObjectCache {
312 public:
313 ObjectDumper(FileSpec output_dir) : m_out_dir(output_dir) {}
314 void notifyObjectCompiled(const llvm::Module *module,
315 llvm::MemoryBufferRef object) override {
316 int fd = 0;
317 llvm::SmallVector<char, 256> result_path;
318 std::string object_name_model =
319 "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
320 FileSpec model_spec
321 = m_out_dir.CopyByAppendingPathComponent(component: object_name_model);
322 std::string model_path = model_spec.GetPath();
323
324 std::error_code result
325 = llvm::sys::fs::createUniqueFile(Model: model_path, ResultFD&: fd, ResultPath&: result_path);
326 if (!result) {
327 llvm::raw_fd_ostream fds(fd, true);
328 fds.write(Ptr: object.getBufferStart(), Size: object.getBufferSize());
329 }
330 }
331 std::unique_ptr<llvm::MemoryBuffer>
332 getObject(const llvm::Module *module) override {
333 // Return nothing - we're just abusing the object-cache mechanism to dump
334 // objects.
335 return nullptr;
336 }
337 private:
338 FileSpec m_out_dir;
339 };
340
341 FileSpec save_objects_dir = process_sp->GetTarget().GetSaveJITObjectsDir();
342 if (save_objects_dir) {
343 m_object_cache_up = std::make_unique<ObjectDumper>(args&: save_objects_dir);
344 m_execution_engine_up->setObjectCache(m_object_cache_up.get());
345 }
346
347 // Make sure we see all sections, including ones that don't have
348 // relocations...
349 m_execution_engine_up->setProcessAllSections(true);
350
351 m_execution_engine_up->DisableLazyCompilation();
352
353 for (llvm::Function &function : *m_module) {
354 if (function.isDeclaration() || function.hasPrivateLinkage())
355 continue;
356
357 const bool external = !function.hasLocalLinkage();
358
359 void *fun_ptr = m_execution_engine_up->getPointerToFunction(F: &function);
360
361 if (!error.Success()) {
362 // We got an error through our callback!
363 return;
364 }
365
366 if (!fun_ptr) {
367 error.SetErrorToGenericError();
368 error.SetErrorStringWithFormat(
369 "'%s' was in the JITted module but wasn't lowered",
370 function.getName().str().c_str());
371 return;
372 }
373 m_jitted_functions.push_back(x: JittedFunction(
374 function.getName().str().c_str(), external, reinterpret_cast<uintptr_t>(fun_ptr)));
375 }
376
377 CommitAllocations(process_sp);
378 ReportAllocations(engine&: *m_execution_engine_up);
379
380 // We have to do this after calling ReportAllocations because for the MCJIT,
381 // getGlobalValueAddress will cause the JIT to perform all relocations. That
382 // can only be done once, and has to happen after we do the remapping from
383 // local -> remote. That means we don't know the local address of the
384 // Variables, but we don't need that for anything, so that's okay.
385
386 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
387 llvm::GlobalValue &val) {
388 if (val.hasExternalLinkage() && !val.isDeclaration()) {
389 uint64_t var_ptr_addr =
390 m_execution_engine_up->getGlobalValueAddress(Name: val.getName().str());
391
392 lldb::addr_t remote_addr = GetRemoteAddressForLocal(local_address: var_ptr_addr);
393
394 // This is a really unfortunae API that sometimes returns local addresses
395 // and sometimes returns remote addresses, based on whether the variable
396 // was relocated during ReportAllocations or not.
397
398 if (remote_addr == LLDB_INVALID_ADDRESS) {
399 remote_addr = var_ptr_addr;
400 }
401
402 if (var_ptr_addr != 0)
403 m_jitted_global_variables.push_back(x: JittedGlobalVariable(
404 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
405 }
406 };
407
408 for (llvm::GlobalVariable &global_var : m_module->globals()) {
409 RegisterOneValue(global_var);
410 }
411
412 for (llvm::GlobalAlias &global_alias : m_module->aliases()) {
413 RegisterOneValue(global_alias);
414 }
415
416 WriteData(process_sp);
417
418 if (m_failed_lookups.size()) {
419 StreamString ss;
420
421 ss.PutCString(cstr: "Couldn't look up symbols:\n");
422
423 bool emitNewLine = false;
424
425 for (ConstString failed_lookup : m_failed_lookups) {
426 if (emitNewLine)
427 ss.PutCString(cstr: "\n");
428 emitNewLine = true;
429 ss.PutCString(cstr: " ");
430 ss.PutCString(cstr: Mangled(failed_lookup).GetDemangledName().GetStringRef());
431 }
432
433 m_failed_lookups.clear();
434 ss.PutCString(
435 cstr: "\nHint: The expression tried to call a function that is not present "
436 "in the target, perhaps because it was optimized out by the compiler.");
437 error.SetErrorString(ss.GetString());
438
439 return;
440 }
441
442 m_function_load_addr = LLDB_INVALID_ADDRESS;
443 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
444
445 for (JittedFunction &jitted_function : m_jitted_functions) {
446 jitted_function.m_remote_addr =
447 GetRemoteAddressForLocal(local_address: jitted_function.m_local_addr);
448
449 if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
450 AddrRange func_range =
451 GetRemoteRangeForLocal(local_address: jitted_function.m_local_addr);
452 m_function_end_load_addr = func_range.first + func_range.second;
453 m_function_load_addr = jitted_function.m_remote_addr;
454 }
455 }
456
457 if (log) {
458 LLDB_LOGF(log, "Code can be run in the target.");
459
460 StreamString disassembly_stream;
461
462 Status err = DisassembleFunction(stream&: disassembly_stream, process_wp&: process_sp);
463
464 if (!err.Success()) {
465 LLDB_LOGF(log, "Couldn't disassemble function : %s",
466 err.AsCString("unknown error"));
467 } else {
468 LLDB_LOGF(log, "Function disassembly:\n%s", disassembly_stream.GetData());
469 }
470
471 LLDB_LOGF(log, "Sections: ");
472 for (AllocationRecord &record : m_records) {
473 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
474 record.dump(log);
475
476 DataBufferHeap my_buffer(record.m_size, 0);
477 Status err;
478 ReadMemory(bytes: my_buffer.GetBytes(), process_address: record.m_process_address,
479 size: record.m_size, error&: err);
480
481 if (err.Success()) {
482 DataExtractor my_extractor(my_buffer.GetBytes(),
483 my_buffer.GetByteSize(),
484 lldb::eByteOrderBig, 8);
485 my_extractor.PutToLog(log, offset: 0, length: my_buffer.GetByteSize(),
486 base_addr: record.m_process_address, num_per_line: 16,
487 type: DataExtractor::TypeUInt8);
488 }
489 } else {
490 record.dump(log);
491
492 DataExtractor my_extractor((const void *)record.m_host_address,
493 record.m_size, lldb::eByteOrderBig, 8);
494 my_extractor.PutToLog(log, offset: 0, length: record.m_size, base_addr: record.m_host_address, num_per_line: 16,
495 type: DataExtractor::TypeUInt8);
496 }
497 }
498 }
499
500 func_addr = m_function_load_addr;
501 func_end = m_function_end_load_addr;
502}
503
504IRExecutionUnit::~IRExecutionUnit() {
505 m_module_up.reset();
506 m_execution_engine_up.reset();
507 m_context_up.reset();
508}
509
510IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
511 : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {}
512
513IRExecutionUnit::MemoryManager::~MemoryManager() = default;
514
515lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
516 const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
517 lldb::SectionType sect_type = lldb::eSectionTypeCode;
518 switch (alloc_kind) {
519 case AllocationKind::Stub:
520 sect_type = lldb::eSectionTypeCode;
521 break;
522 case AllocationKind::Code:
523 sect_type = lldb::eSectionTypeCode;
524 break;
525 case AllocationKind::Data:
526 sect_type = lldb::eSectionTypeData;
527 break;
528 case AllocationKind::Global:
529 sect_type = lldb::eSectionTypeData;
530 break;
531 case AllocationKind::Bytes:
532 sect_type = lldb::eSectionTypeOther;
533 break;
534 }
535
536 if (!name.empty()) {
537 if (name.equals(RHS: "__text") || name.equals(RHS: ".text"))
538 sect_type = lldb::eSectionTypeCode;
539 else if (name.equals(RHS: "__data") || name.equals(RHS: ".data"))
540 sect_type = lldb::eSectionTypeCode;
541 else if (name.starts_with(Prefix: "__debug_") || name.starts_with(Prefix: ".debug_")) {
542 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
543 llvm::StringRef dwarf_name(name.substr(Start: name_idx));
544 switch (dwarf_name[0]) {
545 case 'a':
546 if (dwarf_name.equals(RHS: "abbrev"))
547 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
548 else if (dwarf_name.equals(RHS: "aranges"))
549 sect_type = lldb::eSectionTypeDWARFDebugAranges;
550 else if (dwarf_name.equals(RHS: "addr"))
551 sect_type = lldb::eSectionTypeDWARFDebugAddr;
552 break;
553
554 case 'f':
555 if (dwarf_name.equals(RHS: "frame"))
556 sect_type = lldb::eSectionTypeDWARFDebugFrame;
557 break;
558
559 case 'i':
560 if (dwarf_name.equals(RHS: "info"))
561 sect_type = lldb::eSectionTypeDWARFDebugInfo;
562 break;
563
564 case 'l':
565 if (dwarf_name.equals(RHS: "line"))
566 sect_type = lldb::eSectionTypeDWARFDebugLine;
567 else if (dwarf_name.equals(RHS: "loc"))
568 sect_type = lldb::eSectionTypeDWARFDebugLoc;
569 else if (dwarf_name.equals(RHS: "loclists"))
570 sect_type = lldb::eSectionTypeDWARFDebugLocLists;
571 break;
572
573 case 'm':
574 if (dwarf_name.equals(RHS: "macinfo"))
575 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
576 break;
577
578 case 'p':
579 if (dwarf_name.equals(RHS: "pubnames"))
580 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
581 else if (dwarf_name.equals(RHS: "pubtypes"))
582 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
583 break;
584
585 case 's':
586 if (dwarf_name.equals(RHS: "str"))
587 sect_type = lldb::eSectionTypeDWARFDebugStr;
588 else if (dwarf_name.equals(RHS: "str_offsets"))
589 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
590 break;
591
592 case 'r':
593 if (dwarf_name.equals(RHS: "ranges"))
594 sect_type = lldb::eSectionTypeDWARFDebugRanges;
595 break;
596
597 default:
598 break;
599 }
600 } else if (name.starts_with(Prefix: "__apple_") || name.starts_with(Prefix: ".apple_"))
601 sect_type = lldb::eSectionTypeInvalid;
602 else if (name.equals(RHS: "__objc_imageinfo"))
603 sect_type = lldb::eSectionTypeOther;
604 }
605 return sect_type;
606}
607
608uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
609 uintptr_t Size, unsigned Alignment, unsigned SectionID,
610 llvm::StringRef SectionName) {
611 Log *log = GetLog(mask: LLDBLog::Expressions);
612
613 uint8_t *return_value = m_default_mm_up->allocateCodeSection(
614 Size, Alignment, SectionID, SectionName);
615
616 m_parent.m_records.push_back(x: AllocationRecord(
617 (uintptr_t)return_value,
618 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
619 GetSectionTypeFromSectionName(name: SectionName, alloc_kind: AllocationKind::Code), Size,
620 Alignment, SectionID, SectionName.str().c_str()));
621
622 LLDB_LOGF(log,
623 "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
624 ", Alignment=%u, SectionID=%u) = %p",
625 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
626
627 if (m_parent.m_reported_allocations) {
628 Status err;
629 lldb::ProcessSP process_sp =
630 m_parent.GetBestExecutionContextScope()->CalculateProcess();
631
632 m_parent.CommitOneAllocation(process_sp, error&: err, record&: m_parent.m_records.back());
633 }
634
635 return return_value;
636}
637
638uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
639 uintptr_t Size, unsigned Alignment, unsigned SectionID,
640 llvm::StringRef SectionName, bool IsReadOnly) {
641 Log *log = GetLog(mask: LLDBLog::Expressions);
642
643 uint8_t *return_value = m_default_mm_up->allocateDataSection(
644 Size, Alignment, SectionID, SectionName, isReadOnly: IsReadOnly);
645
646 uint32_t permissions = lldb::ePermissionsReadable;
647 if (!IsReadOnly)
648 permissions |= lldb::ePermissionsWritable;
649 m_parent.m_records.push_back(x: AllocationRecord(
650 (uintptr_t)return_value, permissions,
651 GetSectionTypeFromSectionName(name: SectionName, alloc_kind: AllocationKind::Data), Size,
652 Alignment, SectionID, SectionName.str().c_str()));
653 LLDB_LOGF(log,
654 "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
655 ", Alignment=%u, SectionID=%u) = %p",
656 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
657
658 if (m_parent.m_reported_allocations) {
659 Status err;
660 lldb::ProcessSP process_sp =
661 m_parent.GetBestExecutionContextScope()->CalculateProcess();
662
663 m_parent.CommitOneAllocation(process_sp, error&: err, record&: m_parent.m_records.back());
664 }
665
666 return return_value;
667}
668
669void IRExecutionUnit::CollectCandidateCNames(std::vector<ConstString> &C_names,
670 ConstString name) {
671 if (m_strip_underscore && name.AsCString()[0] == '_')
672 C_names.insert(position: C_names.begin(), x: ConstString(&name.AsCString()[1]));
673 C_names.push_back(x: name);
674}
675
676void IRExecutionUnit::CollectCandidateCPlusPlusNames(
677 std::vector<ConstString> &CPP_names,
678 const std::vector<ConstString> &C_names, const SymbolContext &sc) {
679 if (auto *cpp_lang = Language::FindPlugin(language: lldb::eLanguageTypeC_plus_plus)) {
680 for (const ConstString &name : C_names) {
681 Mangled mangled(name);
682 if (cpp_lang->SymbolNameFitsToLanguage(name: mangled)) {
683 if (ConstString best_alternate =
684 cpp_lang->FindBestAlternateFunctionMangledName(mangled, sym_ctx: sc)) {
685 CPP_names.push_back(x: best_alternate);
686 }
687 }
688
689 std::vector<ConstString> alternates =
690 cpp_lang->GenerateAlternateFunctionManglings(mangled: name);
691 CPP_names.insert(position: CPP_names.end(), first: alternates.begin(), last: alternates.end());
692
693 // As a last-ditch fallback, try the base name for C++ names. It's
694 // terrible, but the DWARF doesn't always encode "extern C" correctly.
695 ConstString basename =
696 cpp_lang->GetDemangledFunctionNameWithoutArguments(mangled);
697 CPP_names.push_back(x: basename);
698 }
699 }
700}
701
702class LoadAddressResolver {
703public:
704 LoadAddressResolver(Target *target, bool &symbol_was_missing_weak)
705 : m_target(target), m_symbol_was_missing_weak(symbol_was_missing_weak) {}
706
707 std::optional<lldb::addr_t> Resolve(SymbolContextList &sc_list) {
708 if (sc_list.IsEmpty())
709 return std::nullopt;
710
711 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
712
713 // Missing_weak_symbol will be true only if we found only weak undefined
714 // references to this symbol.
715 m_symbol_was_missing_weak = true;
716
717 for (auto candidate_sc : sc_list.SymbolContexts()) {
718 // Only symbols can be weak undefined.
719 if (!candidate_sc.symbol ||
720 candidate_sc.symbol->GetType() != lldb::eSymbolTypeUndefined ||
721 !candidate_sc.symbol->IsWeak())
722 m_symbol_was_missing_weak = false;
723
724 // First try the symbol.
725 if (candidate_sc.symbol) {
726 load_address = candidate_sc.symbol->ResolveCallableAddress(target&: *m_target);
727 if (load_address == LLDB_INVALID_ADDRESS) {
728 Address addr = candidate_sc.symbol->GetAddress();
729 load_address = m_target->GetProcessSP()
730 ? addr.GetLoadAddress(target: m_target)
731 : addr.GetFileAddress();
732 }
733 }
734
735 // If that didn't work, try the function.
736 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
737 Address addr =
738 candidate_sc.function->GetAddressRange().GetBaseAddress();
739 load_address = m_target->GetProcessSP() ? addr.GetLoadAddress(target: m_target)
740 : addr.GetFileAddress();
741 }
742
743 // We found a load address.
744 if (load_address != LLDB_INVALID_ADDRESS) {
745 // If the load address is external, we're done.
746 const bool is_external =
747 (candidate_sc.function) ||
748 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
749 if (is_external)
750 return load_address;
751
752 // Otherwise, remember the best internal load address.
753 if (m_best_internal_load_address == LLDB_INVALID_ADDRESS)
754 m_best_internal_load_address = load_address;
755 }
756 }
757
758 // You test the address of a weak symbol against NULL to see if it is
759 // present. So we should return 0 for a missing weak symbol.
760 if (m_symbol_was_missing_weak)
761 return 0;
762
763 return std::nullopt;
764 }
765
766 lldb::addr_t GetBestInternalLoadAddress() const {
767 return m_best_internal_load_address;
768 }
769
770private:
771 Target *m_target;
772 bool &m_symbol_was_missing_weak;
773 lldb::addr_t m_best_internal_load_address = LLDB_INVALID_ADDRESS;
774};
775
776lldb::addr_t
777IRExecutionUnit::FindInSymbols(const std::vector<ConstString> &names,
778 const lldb_private::SymbolContext &sc,
779 bool &symbol_was_missing_weak) {
780 symbol_was_missing_weak = false;
781
782 Target *target = sc.target_sp.get();
783 if (!target) {
784 // We shouldn't be doing any symbol lookup at all without a target.
785 return LLDB_INVALID_ADDRESS;
786 }
787
788 LoadAddressResolver resolver(target, symbol_was_missing_weak);
789
790 ModuleFunctionSearchOptions function_options;
791 function_options.include_symbols = true;
792 function_options.include_inlines = false;
793
794 for (const ConstString &name : names) {
795 if (sc.module_sp) {
796 SymbolContextList sc_list;
797 sc.module_sp->FindFunctions(name, parent_decl_ctx: CompilerDeclContext(),
798 name_type_mask: lldb::eFunctionNameTypeFull, options: function_options,
799 sc_list);
800 if (auto load_addr = resolver.Resolve(sc_list))
801 return *load_addr;
802 }
803
804 if (sc.target_sp) {
805 SymbolContextList sc_list;
806 sc.target_sp->GetImages().FindFunctions(name, name_type_mask: lldb::eFunctionNameTypeFull,
807 options: function_options, sc_list);
808 if (auto load_addr = resolver.Resolve(sc_list))
809 return *load_addr;
810 }
811
812 if (sc.target_sp) {
813 SymbolContextList sc_list;
814 sc.target_sp->GetImages().FindSymbolsWithNameAndType(
815 name, symbol_type: lldb::eSymbolTypeAny, sc_list);
816 if (auto load_addr = resolver.Resolve(sc_list))
817 return *load_addr;
818 }
819
820 lldb::addr_t best_internal_load_address =
821 resolver.GetBestInternalLoadAddress();
822 if (best_internal_load_address != LLDB_INVALID_ADDRESS)
823 return best_internal_load_address;
824 }
825
826 return LLDB_INVALID_ADDRESS;
827}
828
829lldb::addr_t
830IRExecutionUnit::FindInRuntimes(const std::vector<ConstString> &names,
831 const lldb_private::SymbolContext &sc) {
832 lldb::TargetSP target_sp = sc.target_sp;
833
834 if (!target_sp) {
835 return LLDB_INVALID_ADDRESS;
836 }
837
838 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
839
840 if (!process_sp) {
841 return LLDB_INVALID_ADDRESS;
842 }
843
844 for (const ConstString &name : names) {
845 for (LanguageRuntime *runtime : process_sp->GetLanguageRuntimes()) {
846 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(name);
847
848 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
849 return symbol_load_addr;
850 }
851 }
852
853 return LLDB_INVALID_ADDRESS;
854}
855
856lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
857 const std::vector<ConstString> &names,
858 const lldb_private::SymbolContext &sc) {
859 lldb::TargetSP target_sp = sc.target_sp;
860
861 for (const ConstString &name : names) {
862 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(name);
863
864 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
865 return symbol_load_addr;
866 }
867
868 return LLDB_INVALID_ADDRESS;
869}
870
871lldb::addr_t IRExecutionUnit::FindSymbol(lldb_private::ConstString name,
872 bool &missing_weak) {
873 std::vector<ConstString> candidate_C_names;
874 std::vector<ConstString> candidate_CPlusPlus_names;
875
876 CollectCandidateCNames(C_names&: candidate_C_names, name);
877
878 lldb::addr_t ret = FindInSymbols(names: candidate_C_names, sc: m_sym_ctx, symbol_was_missing_weak&: missing_weak);
879 if (ret != LLDB_INVALID_ADDRESS)
880 return ret;
881
882 // If we find the symbol in runtimes or user defined symbols it can't be
883 // a missing weak symbol.
884 missing_weak = false;
885 ret = FindInRuntimes(names: candidate_C_names, sc: m_sym_ctx);
886 if (ret != LLDB_INVALID_ADDRESS)
887 return ret;
888
889 ret = FindInUserDefinedSymbols(names: candidate_C_names, sc: m_sym_ctx);
890 if (ret != LLDB_INVALID_ADDRESS)
891 return ret;
892
893 CollectCandidateCPlusPlusNames(CPP_names&: candidate_CPlusPlus_names, C_names: candidate_C_names,
894 sc: m_sym_ctx);
895 ret = FindInSymbols(names: candidate_CPlusPlus_names, sc: m_sym_ctx, symbol_was_missing_weak&: missing_weak);
896 return ret;
897}
898
899void IRExecutionUnit::GetStaticInitializers(
900 std::vector<lldb::addr_t> &static_initializers) {
901 Log *log = GetLog(mask: LLDBLog::Expressions);
902
903 llvm::GlobalVariable *global_ctors =
904 m_module->getNamedGlobal(Name: "llvm.global_ctors");
905 if (!global_ctors) {
906 LLDB_LOG(log, "Couldn't find llvm.global_ctors.");
907 return;
908 }
909 auto *ctor_array =
910 llvm::dyn_cast<llvm::ConstantArray>(Val: global_ctors->getInitializer());
911 if (!ctor_array) {
912 LLDB_LOG(log, "llvm.global_ctors not a ConstantArray.");
913 return;
914 }
915
916 for (llvm::Use &ctor_use : ctor_array->operands()) {
917 auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(Val&: ctor_use);
918 if (!ctor_struct)
919 continue;
920 // this is standardized
921 lldbassert(ctor_struct->getNumOperands() == 3);
922 auto *ctor_function =
923 llvm::dyn_cast<llvm::Function>(Val: ctor_struct->getOperand(i_nocapture: 1));
924 if (!ctor_function) {
925 LLDB_LOG(log, "global_ctor doesn't contain an llvm::Function");
926 continue;
927 }
928
929 ConstString ctor_function_name(ctor_function->getName().str());
930 LLDB_LOG(log, "Looking for callable jitted function with name {0}.",
931 ctor_function_name);
932
933 for (JittedFunction &jitted_function : m_jitted_functions) {
934 if (ctor_function_name != jitted_function.m_name)
935 continue;
936 if (jitted_function.m_remote_addr == LLDB_INVALID_ADDRESS) {
937 LLDB_LOG(log, "Found jitted function with invalid address.");
938 continue;
939 }
940 static_initializers.push_back(x: jitted_function.m_remote_addr);
941 LLDB_LOG(log, "Calling function at address {0:x}.",
942 jitted_function.m_remote_addr);
943 break;
944 }
945 }
946}
947
948llvm::JITSymbol
949IRExecutionUnit::MemoryManager::findSymbol(const std::string &Name) {
950 bool missing_weak = false;
951 uint64_t addr = GetSymbolAddressAndPresence(Name, missing_weak);
952 // This is a weak symbol:
953 if (missing_weak)
954 return llvm::JITSymbol(addr,
955 llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak);
956 else
957 return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported);
958}
959
960uint64_t
961IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
962 bool missing_weak = false;
963 return GetSymbolAddressAndPresence(Name, missing_weak);
964}
965
966uint64_t
967IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence(
968 const std::string &Name, bool &missing_weak) {
969 Log *log = GetLog(mask: LLDBLog::Expressions);
970
971 ConstString name_cs(Name.c_str());
972
973 lldb::addr_t ret = m_parent.FindSymbol(name: name_cs, missing_weak);
974
975 if (ret == LLDB_INVALID_ADDRESS) {
976 LLDB_LOGF(log,
977 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
978 Name.c_str());
979
980 m_parent.ReportSymbolLookupError(name: name_cs);
981 return 0;
982 } else {
983 LLDB_LOGF(log, "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
984 Name.c_str(), ret);
985 return ret;
986 }
987}
988
989void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
990 const std::string &Name, bool AbortOnFailure) {
991 return (void *)getSymbolAddress(Name);
992}
993
994lldb::addr_t
995IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
996 Log *log = GetLog(mask: LLDBLog::Expressions);
997
998 for (AllocationRecord &record : m_records) {
999 if (local_address >= record.m_host_address &&
1000 local_address < record.m_host_address + record.m_size) {
1001 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1002 return LLDB_INVALID_ADDRESS;
1003
1004 lldb::addr_t ret =
1005 record.m_process_address + (local_address - record.m_host_address);
1006
1007 LLDB_LOGF(log,
1008 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1009 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1010 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1011 local_address, (uint64_t)record.m_host_address,
1012 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1013 record.m_process_address,
1014 record.m_process_address + record.m_size);
1015
1016 return ret;
1017 }
1018 }
1019
1020 return LLDB_INVALID_ADDRESS;
1021}
1022
1023IRExecutionUnit::AddrRange
1024IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1025 for (AllocationRecord &record : m_records) {
1026 if (local_address >= record.m_host_address &&
1027 local_address < record.m_host_address + record.m_size) {
1028 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1029 return AddrRange(0, 0);
1030
1031 return AddrRange(record.m_process_address, record.m_size);
1032 }
1033 }
1034
1035 return AddrRange(0, 0);
1036}
1037
1038bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1039 Status &error,
1040 AllocationRecord &record) {
1041 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1042 return true;
1043 }
1044
1045 switch (record.m_sect_type) {
1046 case lldb::eSectionTypeInvalid:
1047 case lldb::eSectionTypeDWARFDebugAbbrev:
1048 case lldb::eSectionTypeDWARFDebugAddr:
1049 case lldb::eSectionTypeDWARFDebugAranges:
1050 case lldb::eSectionTypeDWARFDebugCuIndex:
1051 case lldb::eSectionTypeDWARFDebugFrame:
1052 case lldb::eSectionTypeDWARFDebugInfo:
1053 case lldb::eSectionTypeDWARFDebugLine:
1054 case lldb::eSectionTypeDWARFDebugLoc:
1055 case lldb::eSectionTypeDWARFDebugLocLists:
1056 case lldb::eSectionTypeDWARFDebugMacInfo:
1057 case lldb::eSectionTypeDWARFDebugPubNames:
1058 case lldb::eSectionTypeDWARFDebugPubTypes:
1059 case lldb::eSectionTypeDWARFDebugRanges:
1060 case lldb::eSectionTypeDWARFDebugStr:
1061 case lldb::eSectionTypeDWARFDebugStrOffsets:
1062 case lldb::eSectionTypeDWARFAppleNames:
1063 case lldb::eSectionTypeDWARFAppleTypes:
1064 case lldb::eSectionTypeDWARFAppleNamespaces:
1065 case lldb::eSectionTypeDWARFAppleObjC:
1066 case lldb::eSectionTypeDWARFGNUDebugAltLink:
1067 error.Clear();
1068 break;
1069 default:
1070 const bool zero_memory = false;
1071 record.m_process_address =
1072 Malloc(size: record.m_size, alignment: record.m_alignment, permissions: record.m_permissions,
1073 policy: eAllocationPolicyProcessOnly, zero_memory, error);
1074 break;
1075 }
1076
1077 return error.Success();
1078}
1079
1080bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1081 bool ret = true;
1082
1083 lldb_private::Status err;
1084
1085 for (AllocationRecord &record : m_records) {
1086 ret = CommitOneAllocation(process_sp, error&: err, record);
1087
1088 if (!ret) {
1089 break;
1090 }
1091 }
1092
1093 if (!ret) {
1094 for (AllocationRecord &record : m_records) {
1095 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1096 Free(process_address: record.m_process_address, error&: err);
1097 record.m_process_address = LLDB_INVALID_ADDRESS;
1098 }
1099 }
1100 }
1101
1102 return ret;
1103}
1104
1105void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1106 m_reported_allocations = true;
1107
1108 for (AllocationRecord &record : m_records) {
1109 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1110 continue;
1111
1112 if (record.m_section_id == eSectionIDInvalid)
1113 continue;
1114
1115 engine.mapSectionAddress(LocalAddress: (void *)record.m_host_address,
1116 TargetAddress: record.m_process_address);
1117 }
1118
1119 // Trigger re-application of relocations.
1120 engine.finalizeObject();
1121}
1122
1123bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1124 bool wrote_something = false;
1125 for (AllocationRecord &record : m_records) {
1126 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1127 lldb_private::Status err;
1128 WriteMemory(process_address: record.m_process_address, bytes: (uint8_t *)record.m_host_address,
1129 size: record.m_size, error&: err);
1130 if (err.Success())
1131 wrote_something = true;
1132 }
1133 }
1134 return wrote_something;
1135}
1136
1137void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1138 if (!log)
1139 return;
1140
1141 LLDB_LOGF(log,
1142 "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1143 (unsigned long long)m_host_address, (unsigned long long)m_size,
1144 (unsigned long long)m_process_address, (unsigned)m_alignment,
1145 (unsigned)m_section_id, m_name.c_str());
1146}
1147
1148lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1149 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1150 return exe_ctx.GetByteOrder();
1151}
1152
1153uint32_t IRExecutionUnit::GetAddressByteSize() const {
1154 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1155 return exe_ctx.GetAddressByteSize();
1156}
1157
1158void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1159 lldb_private::Symtab &symtab) {
1160 // No symbols yet...
1161}
1162
1163void IRExecutionUnit::PopulateSectionList(
1164 lldb_private::ObjectFile *obj_file,
1165 lldb_private::SectionList &section_list) {
1166 for (AllocationRecord &record : m_records) {
1167 if (record.m_size > 0) {
1168 lldb::SectionSP section_sp(new lldb_private::Section(
1169 obj_file->GetModule(), obj_file, record.m_section_id,
1170 ConstString(record.m_name), record.m_sect_type,
1171 record.m_process_address, record.m_size,
1172 record.m_host_address, // file_offset (which is the host address for
1173 // the data)
1174 record.m_size, // file_size
1175 0,
1176 record.m_permissions)); // flags
1177 section_list.AddSection(section_sp);
1178 }
1179 }
1180}
1181
1182ArchSpec IRExecutionUnit::GetArchitecture() {
1183 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1184 if(Target *target = exe_ctx.GetTargetPtr())
1185 return target->GetArchitecture();
1186 return ArchSpec();
1187}
1188
1189lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1190 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1191 Target *target = exe_ctx.GetTargetPtr();
1192 if (!target)
1193 return nullptr;
1194
1195 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1196 r: shared_from_this());
1197
1198 lldb::ModuleSP jit_module_sp =
1199 lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(args&: Delegate);
1200 if (!jit_module_sp)
1201 return nullptr;
1202
1203 bool changed = false;
1204 jit_module_sp->SetLoadAddress(target&: *target, value: 0, value_is_offset: true, changed);
1205 return jit_module_sp;
1206}
1207

source code of lldb/source/Expression/IRExecutionUnit.cpp