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::WritableDataBufferSP buffer_sp(
155       new DataBufferHeap(func_range.second, 0));
156 
157   Process *process = exe_ctx.GetProcessPtr();
158   Status err;
159   process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
160                       buffer_sp->GetByteSize(), err);
161 
162   if (!err.Success()) {
163     ret.SetErrorToGenericError();
164     ret.SetErrorStringWithFormat("Couldn't read from process: %s",
165                                  err.AsCString("unknown error"));
166     return ret;
167   }
168 
169   ArchSpec arch(target->GetArchitecture());
170 
171   const char *plugin_name = nullptr;
172   const char *flavor_string = nullptr;
173   lldb::DisassemblerSP disassembler_sp =
174       Disassembler::FindPlugin(arch, flavor_string, plugin_name);
175 
176   if (!disassembler_sp) {
177     ret.SetErrorToGenericError();
178     ret.SetErrorStringWithFormat(
179         "Unable to find disassembler plug-in for %s architecture.",
180         arch.GetArchitectureName());
181     return ret;
182   }
183 
184   if (!process) {
185     ret.SetErrorToGenericError();
186     ret.SetErrorString("Couldn't find the process");
187     return ret;
188   }
189 
190   DataExtractor extractor(buffer_sp, process->GetByteOrder(),
191                           target->GetArchitecture().GetAddressByteSize());
192 
193   if (log) {
194     LLDB_LOGF(log, "Function data has contents:");
195     extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
196                        DataExtractor::TypeUInt8);
197   }
198 
199   disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
200                                       UINT32_MAX, false, false);
201 
202   InstructionList &instruction_list = disassembler_sp->GetInstructionList();
203   instruction_list.Dump(&stream, true, true, &exe_ctx);
204   return ret;
205 }
206 
207 namespace {
208 struct IRExecDiagnosticHandler : public llvm::DiagnosticHandler {
209   Status *err;
210   IRExecDiagnosticHandler(Status *err) : err(err) {}
211   bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override {
212     if (DI.getKind() == llvm::DK_SrcMgr) {
213       const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(DI);
214       if (err && err->Success()) {
215         err->SetErrorToGenericError();
216         err->SetErrorStringWithFormat(
217             "Inline assembly error: %s",
218             DISM.getSMDiag().getMessage().str().c_str());
219       }
220       return true;
221     }
222 
223     return false;
224   }
225 };
226 } // namespace
227 
228 void IRExecutionUnit::ReportSymbolLookupError(ConstString name) {
229   m_failed_lookups.push_back(name);
230 }
231 
232 void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
233                                       lldb::addr_t &func_end) {
234   lldb::ProcessSP process_sp(GetProcessWP().lock());
235 
236   static std::recursive_mutex s_runnable_info_mutex;
237 
238   func_addr = LLDB_INVALID_ADDRESS;
239   func_end = LLDB_INVALID_ADDRESS;
240 
241   if (!process_sp) {
242     error.SetErrorToGenericError();
243     error.SetErrorString("Couldn't write the JIT compiled code into the "
244                          "process because the process is invalid");
245     return;
246   }
247 
248   if (m_did_jit) {
249     func_addr = m_function_load_addr;
250     func_end = m_function_end_load_addr;
251 
252     return;
253   };
254 
255   std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
256 
257   m_did_jit = true;
258 
259   Log *log = GetLog(LLDBLog::Expressions);
260 
261   std::string error_string;
262 
263   if (log) {
264     std::string s;
265     llvm::raw_string_ostream oss(s);
266 
267     m_module->print(oss, nullptr);
268 
269     oss.flush();
270 
271     LLDB_LOGF(log, "Module being sent to JIT: \n%s", s.c_str());
272   }
273 
274   m_module_up->getContext().setDiagnosticHandler(
275       std::make_unique<IRExecDiagnosticHandler>(&error));
276 
277   llvm::EngineBuilder builder(std::move(m_module_up));
278   llvm::Triple triple(m_module->getTargetTriple());
279 
280   builder.setEngineKind(llvm::EngineKind::JIT)
281       .setErrorStr(&error_string)
282       .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_
283                                                       : llvm::Reloc::Static)
284       .setMCJITMemoryManager(std::make_unique<MemoryManager>(*this))
285       .setOptLevel(llvm::CodeGenOpt::Less);
286 
287   llvm::StringRef mArch;
288   llvm::StringRef mCPU;
289   llvm::SmallVector<std::string, 0> mAttrs;
290 
291   for (std::string &feature : m_cpu_features)
292     mAttrs.push_back(feature);
293 
294   llvm::TargetMachine *target_machine =
295       builder.selectTarget(triple, mArch, mCPU, mAttrs);
296 
297   m_execution_engine_up.reset(builder.create(target_machine));
298 
299   if (!m_execution_engine_up) {
300     error.SetErrorToGenericError();
301     error.SetErrorStringWithFormat("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.SetErrorToGenericError();
366       error.SetErrorStringWithFormat(
367           "'%s' was in the JITted module but wasn't lowered",
368           function.getName().str().c_str());
369       return;
370     }
371     m_jitted_functions.push_back(JittedFunction(
372         function.getName().str().c_str(), external, reinterpret_cast<uintptr_t>(fun_ptr)));
373   }
374 
375   CommitAllocations(process_sp);
376   ReportAllocations(*m_execution_engine_up);
377 
378   // We have to do this after calling ReportAllocations because for the MCJIT,
379   // getGlobalValueAddress will cause the JIT to perform all relocations.  That
380   // can only be done once, and has to happen after we do the remapping from
381   // local -> remote. That means we don't know the local address of the
382   // Variables, but we don't need that for anything, so that's okay.
383 
384   std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
385       llvm::GlobalValue &val) {
386     if (val.hasExternalLinkage() && !val.isDeclaration()) {
387       uint64_t var_ptr_addr =
388           m_execution_engine_up->getGlobalValueAddress(val.getName().str());
389 
390       lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
391 
392       // This is a really unfortunae API that sometimes returns local addresses
393       // and sometimes returns remote addresses, based on whether the variable
394       // was relocated during ReportAllocations or not.
395 
396       if (remote_addr == LLDB_INVALID_ADDRESS) {
397         remote_addr = var_ptr_addr;
398       }
399 
400       if (var_ptr_addr != 0)
401         m_jitted_global_variables.push_back(JittedGlobalVariable(
402             val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
403     }
404   };
405 
406   for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) {
407     RegisterOneValue(global_var);
408   }
409 
410   for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
411     RegisterOneValue(global_alias);
412   }
413 
414   WriteData(process_sp);
415 
416   if (m_failed_lookups.size()) {
417     StreamString ss;
418 
419     ss.PutCString("Couldn't lookup symbols:\n");
420 
421     bool emitNewLine = false;
422 
423     for (ConstString failed_lookup : m_failed_lookups) {
424       if (emitNewLine)
425         ss.PutCString("\n");
426       emitNewLine = true;
427       ss.PutCString("  ");
428       ss.PutCString(Mangled(failed_lookup).GetDemangledName().GetStringRef());
429     }
430 
431     m_failed_lookups.clear();
432 
433     error.SetErrorString(ss.GetString());
434 
435     return;
436   }
437 
438   m_function_load_addr = LLDB_INVALID_ADDRESS;
439   m_function_end_load_addr = LLDB_INVALID_ADDRESS;
440 
441   for (JittedFunction &jitted_function : m_jitted_functions) {
442     jitted_function.m_remote_addr =
443         GetRemoteAddressForLocal(jitted_function.m_local_addr);
444 
445     if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
446       AddrRange func_range =
447           GetRemoteRangeForLocal(jitted_function.m_local_addr);
448       m_function_end_load_addr = func_range.first + func_range.second;
449       m_function_load_addr = jitted_function.m_remote_addr;
450     }
451   }
452 
453   if (log) {
454     LLDB_LOGF(log, "Code can be run in the target.");
455 
456     StreamString disassembly_stream;
457 
458     Status err = DisassembleFunction(disassembly_stream, process_sp);
459 
460     if (!err.Success()) {
461       LLDB_LOGF(log, "Couldn't disassemble function : %s",
462                 err.AsCString("unknown error"));
463     } else {
464       LLDB_LOGF(log, "Function disassembly:\n%s", disassembly_stream.GetData());
465     }
466 
467     LLDB_LOGF(log, "Sections: ");
468     for (AllocationRecord &record : m_records) {
469       if (record.m_process_address != LLDB_INVALID_ADDRESS) {
470         record.dump(log);
471 
472         DataBufferHeap my_buffer(record.m_size, 0);
473         Status err;
474         ReadMemory(my_buffer.GetBytes(), record.m_process_address,
475                    record.m_size, err);
476 
477         if (err.Success()) {
478           DataExtractor my_extractor(my_buffer.GetBytes(),
479                                      my_buffer.GetByteSize(),
480                                      lldb::eByteOrderBig, 8);
481           my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
482                                 record.m_process_address, 16,
483                                 DataExtractor::TypeUInt8);
484         }
485       } else {
486         record.dump(log);
487 
488         DataExtractor my_extractor((const void *)record.m_host_address,
489                                    record.m_size, lldb::eByteOrderBig, 8);
490         my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
491                               DataExtractor::TypeUInt8);
492       }
493     }
494   }
495 
496   func_addr = m_function_load_addr;
497   func_end = m_function_end_load_addr;
498 }
499 
500 IRExecutionUnit::~IRExecutionUnit() {
501   m_module_up.reset();
502   m_execution_engine_up.reset();
503   m_context_up.reset();
504 }
505 
506 IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
507     : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {}
508 
509 IRExecutionUnit::MemoryManager::~MemoryManager() = default;
510 
511 lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
512     const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
513   lldb::SectionType sect_type = lldb::eSectionTypeCode;
514   switch (alloc_kind) {
515   case AllocationKind::Stub:
516     sect_type = lldb::eSectionTypeCode;
517     break;
518   case AllocationKind::Code:
519     sect_type = lldb::eSectionTypeCode;
520     break;
521   case AllocationKind::Data:
522     sect_type = lldb::eSectionTypeData;
523     break;
524   case AllocationKind::Global:
525     sect_type = lldb::eSectionTypeData;
526     break;
527   case AllocationKind::Bytes:
528     sect_type = lldb::eSectionTypeOther;
529     break;
530   }
531 
532   if (!name.empty()) {
533     if (name.equals("__text") || name.equals(".text"))
534       sect_type = lldb::eSectionTypeCode;
535     else if (name.equals("__data") || name.equals(".data"))
536       sect_type = lldb::eSectionTypeCode;
537     else if (name.startswith("__debug_") || name.startswith(".debug_")) {
538       const uint32_t name_idx = name[0] == '_' ? 8 : 7;
539       llvm::StringRef dwarf_name(name.substr(name_idx));
540       switch (dwarf_name[0]) {
541       case 'a':
542         if (dwarf_name.equals("abbrev"))
543           sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
544         else if (dwarf_name.equals("aranges"))
545           sect_type = lldb::eSectionTypeDWARFDebugAranges;
546         else if (dwarf_name.equals("addr"))
547           sect_type = lldb::eSectionTypeDWARFDebugAddr;
548         break;
549 
550       case 'f':
551         if (dwarf_name.equals("frame"))
552           sect_type = lldb::eSectionTypeDWARFDebugFrame;
553         break;
554 
555       case 'i':
556         if (dwarf_name.equals("info"))
557           sect_type = lldb::eSectionTypeDWARFDebugInfo;
558         break;
559 
560       case 'l':
561         if (dwarf_name.equals("line"))
562           sect_type = lldb::eSectionTypeDWARFDebugLine;
563         else if (dwarf_name.equals("loc"))
564           sect_type = lldb::eSectionTypeDWARFDebugLoc;
565         else if (dwarf_name.equals("loclists"))
566           sect_type = lldb::eSectionTypeDWARFDebugLocLists;
567         break;
568 
569       case 'm':
570         if (dwarf_name.equals("macinfo"))
571           sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
572         break;
573 
574       case 'p':
575         if (dwarf_name.equals("pubnames"))
576           sect_type = lldb::eSectionTypeDWARFDebugPubNames;
577         else if (dwarf_name.equals("pubtypes"))
578           sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
579         break;
580 
581       case 's':
582         if (dwarf_name.equals("str"))
583           sect_type = lldb::eSectionTypeDWARFDebugStr;
584         else if (dwarf_name.equals("str_offsets"))
585           sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
586         break;
587 
588       case 'r':
589         if (dwarf_name.equals("ranges"))
590           sect_type = lldb::eSectionTypeDWARFDebugRanges;
591         break;
592 
593       default:
594         break;
595       }
596     } else if (name.startswith("__apple_") || name.startswith(".apple_"))
597       sect_type = lldb::eSectionTypeInvalid;
598     else if (name.equals("__objc_imageinfo"))
599       sect_type = lldb::eSectionTypeOther;
600   }
601   return sect_type;
602 }
603 
604 uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
605     uintptr_t Size, unsigned Alignment, unsigned SectionID,
606     llvm::StringRef SectionName) {
607   Log *log = GetLog(LLDBLog::Expressions);
608 
609   uint8_t *return_value = m_default_mm_up->allocateCodeSection(
610       Size, Alignment, SectionID, SectionName);
611 
612   m_parent.m_records.push_back(AllocationRecord(
613       (uintptr_t)return_value,
614       lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
615       GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
616       Alignment, SectionID, SectionName.str().c_str()));
617 
618   LLDB_LOGF(log,
619             "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
620             ", Alignment=%u, SectionID=%u) = %p",
621             (uint64_t)Size, Alignment, SectionID, (void *)return_value);
622 
623   if (m_parent.m_reported_allocations) {
624     Status err;
625     lldb::ProcessSP process_sp =
626         m_parent.GetBestExecutionContextScope()->CalculateProcess();
627 
628     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
629   }
630 
631   return return_value;
632 }
633 
634 uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
635     uintptr_t Size, unsigned Alignment, unsigned SectionID,
636     llvm::StringRef SectionName, bool IsReadOnly) {
637   Log *log = GetLog(LLDBLog::Expressions);
638 
639   uint8_t *return_value = m_default_mm_up->allocateDataSection(
640       Size, Alignment, SectionID, SectionName, IsReadOnly);
641 
642   uint32_t permissions = lldb::ePermissionsReadable;
643   if (!IsReadOnly)
644     permissions |= lldb::ePermissionsWritable;
645   m_parent.m_records.push_back(AllocationRecord(
646       (uintptr_t)return_value, permissions,
647       GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
648       Alignment, SectionID, SectionName.str().c_str()));
649   LLDB_LOGF(log,
650             "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
651             ", Alignment=%u, SectionID=%u) = %p",
652             (uint64_t)Size, Alignment, SectionID, (void *)return_value);
653 
654   if (m_parent.m_reported_allocations) {
655     Status err;
656     lldb::ProcessSP process_sp =
657         m_parent.GetBestExecutionContextScope()->CalculateProcess();
658 
659     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
660   }
661 
662   return return_value;
663 }
664 
665 void IRExecutionUnit::CollectCandidateCNames(std::vector<ConstString> &C_names,
666                                              ConstString name) {
667   if (m_strip_underscore && name.AsCString()[0] == '_')
668     C_names.insert(C_names.begin(), ConstString(&name.AsCString()[1]));
669   C_names.push_back(name);
670 }
671 
672 void IRExecutionUnit::CollectCandidateCPlusPlusNames(
673     std::vector<ConstString> &CPP_names,
674     const std::vector<ConstString> &C_names, const SymbolContext &sc) {
675   if (auto *cpp_lang = Language::FindPlugin(lldb::eLanguageTypeC_plus_plus)) {
676     for (const ConstString &name : C_names) {
677       Mangled mangled(name);
678       if (cpp_lang->SymbolNameFitsToLanguage(mangled)) {
679         if (ConstString best_alternate =
680                 cpp_lang->FindBestAlternateFunctionMangledName(mangled, sc)) {
681           CPP_names.push_back(best_alternate);
682         }
683       }
684 
685       std::vector<ConstString> alternates =
686           cpp_lang->GenerateAlternateFunctionManglings(name);
687       CPP_names.insert(CPP_names.end(), alternates.begin(), alternates.end());
688 
689       // As a last-ditch fallback, try the base name for C++ names.  It's
690       // terrible, but the DWARF doesn't always encode "extern C" correctly.
691       ConstString basename =
692           cpp_lang->GetDemangledFunctionNameWithoutArguments(mangled);
693       CPP_names.push_back(basename);
694     }
695   }
696 }
697 
698 class LoadAddressResolver {
699 public:
700   LoadAddressResolver(Target *target, bool &symbol_was_missing_weak)
701       : m_target(target), m_symbol_was_missing_weak(symbol_was_missing_weak) {}
702 
703   llvm::Optional<lldb::addr_t> Resolve(SymbolContextList &sc_list) {
704     if (sc_list.IsEmpty())
705       return llvm::None;
706 
707     lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
708 
709     // Missing_weak_symbol will be true only if we found only weak undefined
710     // references to this symbol.
711     m_symbol_was_missing_weak = true;
712 
713     for (auto candidate_sc : sc_list.SymbolContexts()) {
714       // Only symbols can be weak undefined.
715       if (!candidate_sc.symbol ||
716           candidate_sc.symbol->GetType() != lldb::eSymbolTypeUndefined ||
717           !candidate_sc.symbol->IsWeak())
718         m_symbol_was_missing_weak = false;
719 
720       // First try the symbol.
721       if (candidate_sc.symbol) {
722         load_address = candidate_sc.symbol->ResolveCallableAddress(*m_target);
723         if (load_address == LLDB_INVALID_ADDRESS) {
724           Address addr = candidate_sc.symbol->GetAddress();
725           load_address = m_target->GetProcessSP()
726                              ? addr.GetLoadAddress(m_target)
727                              : addr.GetFileAddress();
728         }
729       }
730 
731       // If that didn't work, try the function.
732       if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
733         Address addr =
734             candidate_sc.function->GetAddressRange().GetBaseAddress();
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 llvm::None;
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   LoadAddressResolver resolver(target, symbol_was_missing_weak);
785 
786   ModuleFunctionSearchOptions function_options;
787   function_options.include_symbols = true;
788   function_options.include_inlines = false;
789 
790   for (const ConstString &name : names) {
791     if (sc.module_sp) {
792       SymbolContextList sc_list;
793       sc.module_sp->FindFunctions(name, CompilerDeclContext(),
794                                   lldb::eFunctionNameTypeFull, function_options,
795                                   sc_list);
796       if (auto load_addr = resolver.Resolve(sc_list))
797         return *load_addr;
798     }
799 
800     if (sc.target_sp) {
801       SymbolContextList sc_list;
802       sc.target_sp->GetImages().FindFunctions(name, lldb::eFunctionNameTypeFull,
803                                               function_options, sc_list);
804       if (auto load_addr = resolver.Resolve(sc_list))
805         return *load_addr;
806     }
807 
808     if (sc.target_sp) {
809       SymbolContextList sc_list;
810       sc.target_sp->GetImages().FindSymbolsWithNameAndType(
811           name, lldb::eSymbolTypeAny, sc_list);
812       if (auto load_addr = resolver.Resolve(sc_list))
813         return *load_addr;
814     }
815 
816     lldb::addr_t best_internal_load_address =
817         resolver.GetBestInternalLoadAddress();
818     if (best_internal_load_address != LLDB_INVALID_ADDRESS)
819       return best_internal_load_address;
820   }
821 
822   return LLDB_INVALID_ADDRESS;
823 }
824 
825 lldb::addr_t
826 IRExecutionUnit::FindInRuntimes(const std::vector<ConstString> &names,
827                                 const lldb_private::SymbolContext &sc) {
828   lldb::TargetSP target_sp = sc.target_sp;
829 
830   if (!target_sp) {
831     return LLDB_INVALID_ADDRESS;
832   }
833 
834   lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
835 
836   if (!process_sp) {
837     return LLDB_INVALID_ADDRESS;
838   }
839 
840   for (const ConstString &name : names) {
841     for (LanguageRuntime *runtime : process_sp->GetLanguageRuntimes()) {
842       lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(name);
843 
844       if (symbol_load_addr != LLDB_INVALID_ADDRESS)
845         return symbol_load_addr;
846     }
847   }
848 
849   return LLDB_INVALID_ADDRESS;
850 }
851 
852 lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
853     const std::vector<ConstString> &names,
854     const lldb_private::SymbolContext &sc) {
855   lldb::TargetSP target_sp = sc.target_sp;
856 
857   for (const ConstString &name : names) {
858     lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(name);
859 
860     if (symbol_load_addr != LLDB_INVALID_ADDRESS)
861       return symbol_load_addr;
862   }
863 
864   return LLDB_INVALID_ADDRESS;
865 }
866 
867 lldb::addr_t IRExecutionUnit::FindSymbol(lldb_private::ConstString name,
868                                          bool &missing_weak) {
869   std::vector<ConstString> candidate_C_names;
870   std::vector<ConstString> candidate_CPlusPlus_names;
871 
872   CollectCandidateCNames(candidate_C_names, name);
873 
874   lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx, missing_weak);
875   if (ret != LLDB_INVALID_ADDRESS)
876     return ret;
877 
878   // If we find the symbol in runtimes or user defined symbols it can't be
879   // a missing weak symbol.
880   missing_weak = false;
881   ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
882   if (ret != LLDB_INVALID_ADDRESS)
883     return ret;
884 
885   ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
886   if (ret != LLDB_INVALID_ADDRESS)
887     return ret;
888 
889   CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
890                                  m_sym_ctx);
891   ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx, missing_weak);
892   return ret;
893 }
894 
895 void IRExecutionUnit::GetStaticInitializers(
896     std::vector<lldb::addr_t> &static_initializers) {
897   Log *log = GetLog(LLDBLog::Expressions);
898 
899   llvm::GlobalVariable *global_ctors =
900       m_module->getNamedGlobal("llvm.global_ctors");
901   if (!global_ctors) {
902     LLDB_LOG(log, "Couldn't find llvm.global_ctors.");
903     return;
904   }
905   auto *ctor_array =
906       llvm::dyn_cast<llvm::ConstantArray>(global_ctors->getInitializer());
907   if (!ctor_array) {
908     LLDB_LOG(log, "llvm.global_ctors not a ConstantArray.");
909     return;
910   }
911 
912   for (llvm::Use &ctor_use : ctor_array->operands()) {
913     auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(ctor_use);
914     if (!ctor_struct)
915       continue;
916     // this is standardized
917     lldbassert(ctor_struct->getNumOperands() == 3);
918     auto *ctor_function =
919         llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1));
920     if (!ctor_function) {
921       LLDB_LOG(log, "global_ctor doesn't contain an llvm::Function");
922       continue;
923     }
924 
925     ConstString ctor_function_name(ctor_function->getName().str());
926     LLDB_LOG(log, "Looking for callable jitted function with name {0}.",
927              ctor_function_name);
928 
929     for (JittedFunction &jitted_function : m_jitted_functions) {
930       if (ctor_function_name != jitted_function.m_name)
931         continue;
932       if (jitted_function.m_remote_addr == LLDB_INVALID_ADDRESS) {
933         LLDB_LOG(log, "Found jitted function with invalid address.");
934         continue;
935       }
936       static_initializers.push_back(jitted_function.m_remote_addr);
937       LLDB_LOG(log, "Calling function at address {0:x}.",
938                jitted_function.m_remote_addr);
939       break;
940     }
941   }
942 }
943 
944 llvm::JITSymbol
945 IRExecutionUnit::MemoryManager::findSymbol(const std::string &Name) {
946     bool missing_weak = false;
947     uint64_t addr = GetSymbolAddressAndPresence(Name, missing_weak);
948     // This is a weak symbol:
949     if (missing_weak)
950       return llvm::JITSymbol(addr,
951           llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak);
952     else
953       return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported);
954 }
955 
956 uint64_t
957 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
958   bool missing_weak = false;
959   return GetSymbolAddressAndPresence(Name, missing_weak);
960 }
961 
962 uint64_t
963 IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence(
964     const std::string &Name, bool &missing_weak) {
965   Log *log = GetLog(LLDBLog::Expressions);
966 
967   ConstString name_cs(Name.c_str());
968 
969   lldb::addr_t ret = m_parent.FindSymbol(name_cs, missing_weak);
970 
971   if (ret == LLDB_INVALID_ADDRESS) {
972     LLDB_LOGF(log,
973               "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
974               Name.c_str());
975 
976     m_parent.ReportSymbolLookupError(name_cs);
977     return 0;
978   } else {
979     LLDB_LOGF(log, "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
980               Name.c_str(), ret);
981     return ret;
982   }
983 }
984 
985 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
986     const std::string &Name, bool AbortOnFailure) {
987   return (void *)getSymbolAddress(Name);
988 }
989 
990 lldb::addr_t
991 IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
992   Log *log = GetLog(LLDBLog::Expressions);
993 
994   for (AllocationRecord &record : m_records) {
995     if (local_address >= record.m_host_address &&
996         local_address < record.m_host_address + record.m_size) {
997       if (record.m_process_address == LLDB_INVALID_ADDRESS)
998         return LLDB_INVALID_ADDRESS;
999 
1000       lldb::addr_t ret =
1001           record.m_process_address + (local_address - record.m_host_address);
1002 
1003       LLDB_LOGF(log,
1004                 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1005                 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1006                 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1007                 local_address, (uint64_t)record.m_host_address,
1008                 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1009                 record.m_process_address,
1010                 record.m_process_address + record.m_size);
1011 
1012       return ret;
1013     }
1014   }
1015 
1016   return LLDB_INVALID_ADDRESS;
1017 }
1018 
1019 IRExecutionUnit::AddrRange
1020 IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1021   for (AllocationRecord &record : m_records) {
1022     if (local_address >= record.m_host_address &&
1023         local_address < record.m_host_address + record.m_size) {
1024       if (record.m_process_address == LLDB_INVALID_ADDRESS)
1025         return AddrRange(0, 0);
1026 
1027       return AddrRange(record.m_process_address, record.m_size);
1028     }
1029   }
1030 
1031   return AddrRange(0, 0);
1032 }
1033 
1034 bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1035                                           Status &error,
1036                                           AllocationRecord &record) {
1037   if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1038     return true;
1039   }
1040 
1041   switch (record.m_sect_type) {
1042   case lldb::eSectionTypeInvalid:
1043   case lldb::eSectionTypeDWARFDebugAbbrev:
1044   case lldb::eSectionTypeDWARFDebugAddr:
1045   case lldb::eSectionTypeDWARFDebugAranges:
1046   case lldb::eSectionTypeDWARFDebugCuIndex:
1047   case lldb::eSectionTypeDWARFDebugFrame:
1048   case lldb::eSectionTypeDWARFDebugInfo:
1049   case lldb::eSectionTypeDWARFDebugLine:
1050   case lldb::eSectionTypeDWARFDebugLoc:
1051   case lldb::eSectionTypeDWARFDebugLocLists:
1052   case lldb::eSectionTypeDWARFDebugMacInfo:
1053   case lldb::eSectionTypeDWARFDebugPubNames:
1054   case lldb::eSectionTypeDWARFDebugPubTypes:
1055   case lldb::eSectionTypeDWARFDebugRanges:
1056   case lldb::eSectionTypeDWARFDebugStr:
1057   case lldb::eSectionTypeDWARFDebugStrOffsets:
1058   case lldb::eSectionTypeDWARFAppleNames:
1059   case lldb::eSectionTypeDWARFAppleTypes:
1060   case lldb::eSectionTypeDWARFAppleNamespaces:
1061   case lldb::eSectionTypeDWARFAppleObjC:
1062   case lldb::eSectionTypeDWARFGNUDebugAltLink:
1063     error.Clear();
1064     break;
1065   default:
1066     const bool zero_memory = false;
1067     record.m_process_address =
1068         Malloc(record.m_size, record.m_alignment, record.m_permissions,
1069                eAllocationPolicyProcessOnly, zero_memory, error);
1070     break;
1071   }
1072 
1073   return error.Success();
1074 }
1075 
1076 bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1077   bool ret = true;
1078 
1079   lldb_private::Status err;
1080 
1081   for (AllocationRecord &record : m_records) {
1082     ret = CommitOneAllocation(process_sp, err, record);
1083 
1084     if (!ret) {
1085       break;
1086     }
1087   }
1088 
1089   if (!ret) {
1090     for (AllocationRecord &record : m_records) {
1091       if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1092         Free(record.m_process_address, err);
1093         record.m_process_address = LLDB_INVALID_ADDRESS;
1094       }
1095     }
1096   }
1097 
1098   return ret;
1099 }
1100 
1101 void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1102   m_reported_allocations = true;
1103 
1104   for (AllocationRecord &record : m_records) {
1105     if (record.m_process_address == LLDB_INVALID_ADDRESS)
1106       continue;
1107 
1108     if (record.m_section_id == eSectionIDInvalid)
1109       continue;
1110 
1111     engine.mapSectionAddress((void *)record.m_host_address,
1112                              record.m_process_address);
1113   }
1114 
1115   // Trigger re-application of relocations.
1116   engine.finalizeObject();
1117 }
1118 
1119 bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1120   bool wrote_something = false;
1121   for (AllocationRecord &record : m_records) {
1122     if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1123       lldb_private::Status err;
1124       WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1125                   record.m_size, err);
1126       if (err.Success())
1127         wrote_something = true;
1128     }
1129   }
1130   return wrote_something;
1131 }
1132 
1133 void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1134   if (!log)
1135     return;
1136 
1137   LLDB_LOGF(log,
1138             "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1139             (unsigned long long)m_host_address, (unsigned long long)m_size,
1140             (unsigned long long)m_process_address, (unsigned)m_alignment,
1141             (unsigned)m_section_id, m_name.c_str());
1142 }
1143 
1144 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1145   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1146   return exe_ctx.GetByteOrder();
1147 }
1148 
1149 uint32_t IRExecutionUnit::GetAddressByteSize() const {
1150   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1151   return exe_ctx.GetAddressByteSize();
1152 }
1153 
1154 void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1155                                      lldb_private::Symtab &symtab) {
1156   // No symbols yet...
1157 }
1158 
1159 void IRExecutionUnit::PopulateSectionList(
1160     lldb_private::ObjectFile *obj_file,
1161     lldb_private::SectionList &section_list) {
1162   for (AllocationRecord &record : m_records) {
1163     if (record.m_size > 0) {
1164       lldb::SectionSP section_sp(new lldb_private::Section(
1165           obj_file->GetModule(), obj_file, record.m_section_id,
1166           ConstString(record.m_name), record.m_sect_type,
1167           record.m_process_address, record.m_size,
1168           record.m_host_address, // file_offset (which is the host address for
1169                                  // the data)
1170           record.m_size,         // file_size
1171           0,
1172           record.m_permissions)); // flags
1173       section_list.AddSection(section_sp);
1174     }
1175   }
1176 }
1177 
1178 ArchSpec IRExecutionUnit::GetArchitecture() {
1179   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1180   if(Target *target = exe_ctx.GetTargetPtr())
1181     return target->GetArchitecture();
1182   return ArchSpec();
1183 }
1184 
1185 lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1186   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1187   Target *target = exe_ctx.GetTargetPtr();
1188   if (!target)
1189     return nullptr;
1190 
1191   auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1192       shared_from_this());
1193 
1194   lldb::ModuleSP jit_module_sp =
1195       lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1196   if (!jit_module_sp)
1197     return nullptr;
1198 
1199   bool changed = false;
1200   jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1201   return jit_module_sp;
1202 }
1203