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