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