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