1 //===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ExecutionEngine/ExecutionEngine.h"
11 #include "llvm/ExecutionEngine/ObjectCache.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Disassembler.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Expression/IRExecutionUnit.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/SymbolVendor.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/ObjCLanguageRuntime.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Utility/DataBufferHeap.h"
31 #include "lldb/Utility/DataExtractor.h"
32 #include "lldb/Utility/LLDBAssert.h"
33 #include "lldb/Utility/Log.h"
34 
35 #include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
36 #include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h"
37 
38 using namespace lldb_private;
39 
40 IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_ap,
41                                  std::unique_ptr<llvm::Module> &module_ap,
42                                  ConstString &name,
43                                  const lldb::TargetSP &target_sp,
44                                  const SymbolContext &sym_ctx,
45                                  std::vector<std::string> &cpu_features)
46     : IRMemoryMap(target_sp), m_context_ap(context_ap.release()),
47       m_module_ap(module_ap.release()), m_module(m_module_ap.get()),
48       m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
49       m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
50       m_function_end_load_addr(LLDB_INVALID_ADDRESS),
51       m_reported_allocations(false) {}
52 
53 lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
54                                        Status &error) {
55   const bool zero_memory = false;
56   lldb::addr_t allocation_process_addr =
57       Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable,
58              eAllocationPolicyMirror, zero_memory, error);
59 
60   if (!error.Success())
61     return LLDB_INVALID_ADDRESS;
62 
63   WriteMemory(allocation_process_addr, bytes, size, error);
64 
65   if (!error.Success()) {
66     Status err;
67     Free(allocation_process_addr, err);
68 
69     return LLDB_INVALID_ADDRESS;
70   }
71 
72   if (Log *log =
73           lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_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(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_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   if (log)
127     log->Printf("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   if (log)
143     log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
144                 func_range.first, func_range.second);
145 
146   Target *target = exe_ctx.GetTargetPtr();
147   if (!target) {
148     ret.SetErrorToGenericError();
149     ret.SetErrorString("Couldn't find the target");
150     return ret;
151   }
152 
153   lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
154 
155   Process *process = exe_ctx.GetProcessPtr();
156   Status err;
157   process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
158                       buffer_sp->GetByteSize(), err);
159 
160   if (!err.Success()) {
161     ret.SetErrorToGenericError();
162     ret.SetErrorStringWithFormat("Couldn't read from process: %s",
163                                  err.AsCString("unknown error"));
164     return ret;
165   }
166 
167   ArchSpec arch(target->GetArchitecture());
168 
169   const char *plugin_name = NULL;
170   const char *flavor_string = NULL;
171   lldb::DisassemblerSP disassembler_sp =
172       Disassembler::FindPlugin(arch, flavor_string, plugin_name);
173 
174   if (!disassembler_sp) {
175     ret.SetErrorToGenericError();
176     ret.SetErrorStringWithFormat(
177         "Unable to find disassembler plug-in for %s architecture.",
178         arch.GetArchitectureName());
179     return ret;
180   }
181 
182   if (!process) {
183     ret.SetErrorToGenericError();
184     ret.SetErrorString("Couldn't find the process");
185     return ret;
186   }
187 
188   DataExtractor extractor(buffer_sp, process->GetByteOrder(),
189                           target->GetArchitecture().GetAddressByteSize());
190 
191   if (log) {
192     log->Printf("Function data has contents:");
193     extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
194                        DataExtractor::TypeUInt8);
195   }
196 
197   disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
198                                       UINT32_MAX, false, false);
199 
200   InstructionList &instruction_list = disassembler_sp->GetInstructionList();
201   instruction_list.Dump(&stream, true, true, &exe_ctx);
202   return ret;
203 }
204 
205 static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic,
206                                  void *Context, unsigned LocCookie) {
207   Status *err = static_cast<Status *>(Context);
208 
209   if (err && err->Success()) {
210     err->SetErrorToGenericError();
211     err->SetErrorStringWithFormat("Inline assembly error: %s",
212                                   diagnostic.getMessage().str().c_str());
213   }
214 }
215 
216 void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) {
217   m_failed_lookups.push_back(name);
218 }
219 
220 void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
221                                       lldb::addr_t &func_end) {
222   lldb::ProcessSP process_sp(GetProcessWP().lock());
223 
224   static std::recursive_mutex s_runnable_info_mutex;
225 
226   func_addr = LLDB_INVALID_ADDRESS;
227   func_end = LLDB_INVALID_ADDRESS;
228 
229   if (!process_sp) {
230     error.SetErrorToGenericError();
231     error.SetErrorString("Couldn't write the JIT compiled code into the "
232                          "process because the process is invalid");
233     return;
234   }
235 
236   if (m_did_jit) {
237     func_addr = m_function_load_addr;
238     func_end = m_function_end_load_addr;
239 
240     return;
241   };
242 
243   std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
244 
245   m_did_jit = true;
246 
247   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
248 
249   std::string error_string;
250 
251   if (log) {
252     std::string s;
253     llvm::raw_string_ostream oss(s);
254 
255     m_module->print(oss, NULL);
256 
257     oss.flush();
258 
259     log->Printf("Module being sent to JIT: \n%s", s.c_str());
260   }
261 
262   llvm::Triple triple(m_module->getTargetTriple());
263   llvm::Reloc::Model relocModel;
264 
265   if (triple.isOSBinFormatELF()) {
266     relocModel = llvm::Reloc::Static;
267   } else {
268     relocModel = llvm::Reloc::PIC_;
269   }
270 
271   m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError,
272                                                           &error);
273 
274   llvm::EngineBuilder builder(std::move(m_module_ap));
275 
276   builder.setEngineKind(llvm::EngineKind::JIT)
277       .setErrorStr(&error_string)
278       .setRelocationModel(relocModel)
279       .setMCJITMemoryManager(
280           std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
281       .setOptLevel(llvm::CodeGenOpt::Less);
282 
283   llvm::StringRef mArch;
284   llvm::StringRef mCPU;
285   llvm::SmallVector<std::string, 0> mAttrs;
286 
287   for (std::string &feature : m_cpu_features)
288     mAttrs.push_back(feature);
289 
290   llvm::TargetMachine *target_machine =
291       builder.selectTarget(triple, mArch, mCPU, mAttrs);
292 
293   m_execution_engine_ap.reset(builder.create(target_machine));
294 
295   m_strip_underscore =
296       (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_');
297 
298   if (!m_execution_engine_ap.get()) {
299     error.SetErrorToGenericError();
300     error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
301                                    error_string.c_str());
302     return;
303   }
304 
305   class ObjectDumper : public llvm::ObjectCache {
306   public:
307     void notifyObjectCompiled(const llvm::Module *module,
308                               llvm::MemoryBufferRef object) override {
309       int fd = 0;
310       llvm::SmallVector<char, 256> result_path;
311       std::string object_name_model =
312           "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
313       (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
314       llvm::raw_fd_ostream fds(fd, true);
315       fds.write(object.getBufferStart(), object.getBufferSize());
316     }
317 
318     std::unique_ptr<llvm::MemoryBuffer>
319     getObject(const llvm::Module *module) override {
320       // Return nothing - we're just abusing the object-cache mechanism to dump
321       // objects.
322       return nullptr;
323     }
324   };
325 
326   if (process_sp->GetTarget().GetEnableSaveObjects()) {
327     m_object_cache_ap = llvm::make_unique<ObjectDumper>();
328     m_execution_engine_ap->setObjectCache(m_object_cache_ap.get());
329   }
330 
331   // Make sure we see all sections, including ones that don't have
332   // relocations...
333   m_execution_engine_ap->setProcessAllSections(true);
334 
335   m_execution_engine_ap->DisableLazyCompilation();
336 
337   for (llvm::Function &function : *m_module) {
338     if (function.isDeclaration() || function.hasPrivateLinkage())
339       continue;
340 
341     const bool external =
342         function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
343 
344     void *fun_ptr = m_execution_engine_ap->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, (lldb::addr_t)fun_ptr));
360   }
361 
362   CommitAllocations(process_sp);
363   ReportAllocations(*m_execution_engine_ap);
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_ap->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 (const 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)
416                         .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus)
417                         .AsCString());
418     }
419 
420     m_failed_lookups.clear();
421 
422     error.SetErrorString(ss.GetString());
423 
424     return;
425   }
426 
427   m_function_load_addr = LLDB_INVALID_ADDRESS;
428   m_function_end_load_addr = LLDB_INVALID_ADDRESS;
429 
430   for (JittedFunction &jitted_function : m_jitted_functions) {
431     jitted_function.m_remote_addr =
432         GetRemoteAddressForLocal(jitted_function.m_local_addr);
433 
434     if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
435       AddrRange func_range =
436           GetRemoteRangeForLocal(jitted_function.m_local_addr);
437       m_function_end_load_addr = func_range.first + func_range.second;
438       m_function_load_addr = jitted_function.m_remote_addr;
439     }
440   }
441 
442   if (log) {
443     log->Printf("Code can be run in the target.");
444 
445     StreamString disassembly_stream;
446 
447     Status err = DisassembleFunction(disassembly_stream, process_sp);
448 
449     if (!err.Success()) {
450       log->Printf("Couldn't disassemble function : %s",
451                   err.AsCString("unknown error"));
452     } else {
453       log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
454     }
455 
456     log->Printf("Sections: ");
457     for (AllocationRecord &record : m_records) {
458       if (record.m_process_address != LLDB_INVALID_ADDRESS) {
459         record.dump(log);
460 
461         DataBufferHeap my_buffer(record.m_size, 0);
462         Status err;
463         ReadMemory(my_buffer.GetBytes(), record.m_process_address,
464                    record.m_size, err);
465 
466         if (err.Success()) {
467           DataExtractor my_extractor(my_buffer.GetBytes(),
468                                      my_buffer.GetByteSize(),
469                                      lldb::eByteOrderBig, 8);
470           my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
471                                 record.m_process_address, 16,
472                                 DataExtractor::TypeUInt8);
473         }
474       } else {
475         record.dump(log);
476 
477         DataExtractor my_extractor((const void *)record.m_host_address,
478                                    record.m_size, lldb::eByteOrderBig, 8);
479         my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
480                               DataExtractor::TypeUInt8);
481       }
482     }
483   }
484 
485   func_addr = m_function_load_addr;
486   func_end = m_function_end_load_addr;
487 
488   return;
489 }
490 
491 IRExecutionUnit::~IRExecutionUnit() {
492   m_module_ap.reset();
493   m_execution_engine_ap.reset();
494   m_context_ap.reset();
495 }
496 
497 IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
498     : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {}
499 
500 IRExecutionUnit::MemoryManager::~MemoryManager() {}
501 
502 lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
503     const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
504   lldb::SectionType sect_type = lldb::eSectionTypeCode;
505   switch (alloc_kind) {
506   case AllocationKind::Stub:
507     sect_type = lldb::eSectionTypeCode;
508     break;
509   case AllocationKind::Code:
510     sect_type = lldb::eSectionTypeCode;
511     break;
512   case AllocationKind::Data:
513     sect_type = lldb::eSectionTypeData;
514     break;
515   case AllocationKind::Global:
516     sect_type = lldb::eSectionTypeData;
517     break;
518   case AllocationKind::Bytes:
519     sect_type = lldb::eSectionTypeOther;
520     break;
521   }
522 
523   if (!name.empty()) {
524     if (name.equals("__text") || name.equals(".text"))
525       sect_type = lldb::eSectionTypeCode;
526     else if (name.equals("__data") || name.equals(".data"))
527       sect_type = lldb::eSectionTypeCode;
528     else if (name.startswith("__debug_") || name.startswith(".debug_")) {
529       const uint32_t name_idx = name[0] == '_' ? 8 : 7;
530       llvm::StringRef dwarf_name(name.substr(name_idx));
531       switch (dwarf_name[0]) {
532       case 'a':
533         if (dwarf_name.equals("abbrev"))
534           sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
535         else if (dwarf_name.equals("aranges"))
536           sect_type = lldb::eSectionTypeDWARFDebugAranges;
537         else if (dwarf_name.equals("addr"))
538           sect_type = lldb::eSectionTypeDWARFDebugAddr;
539         break;
540 
541       case 'f':
542         if (dwarf_name.equals("frame"))
543           sect_type = lldb::eSectionTypeDWARFDebugFrame;
544         break;
545 
546       case 'i':
547         if (dwarf_name.equals("info"))
548           sect_type = lldb::eSectionTypeDWARFDebugInfo;
549         break;
550 
551       case 'l':
552         if (dwarf_name.equals("line"))
553           sect_type = lldb::eSectionTypeDWARFDebugLine;
554         else if (dwarf_name.equals("loc"))
555           sect_type = lldb::eSectionTypeDWARFDebugLoc;
556         break;
557 
558       case 'm':
559         if (dwarf_name.equals("macinfo"))
560           sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
561         break;
562 
563       case 'p':
564         if (dwarf_name.equals("pubnames"))
565           sect_type = lldb::eSectionTypeDWARFDebugPubNames;
566         else if (dwarf_name.equals("pubtypes"))
567           sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
568         break;
569 
570       case 's':
571         if (dwarf_name.equals("str"))
572           sect_type = lldb::eSectionTypeDWARFDebugStr;
573         else if (dwarf_name.equals("str_offsets"))
574           sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
575         break;
576 
577       case 'r':
578         if (dwarf_name.equals("ranges"))
579           sect_type = lldb::eSectionTypeDWARFDebugRanges;
580         break;
581 
582       default:
583         break;
584       }
585     } else if (name.startswith("__apple_") || name.startswith(".apple_"))
586       sect_type = lldb::eSectionTypeInvalid;
587     else if (name.equals("__objc_imageinfo"))
588       sect_type = lldb::eSectionTypeOther;
589   }
590   return sect_type;
591 }
592 
593 uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
594     uintptr_t Size, unsigned Alignment, unsigned SectionID,
595     llvm::StringRef SectionName) {
596   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
597 
598   uint8_t *return_value = m_default_mm_ap->allocateCodeSection(
599       Size, Alignment, SectionID, SectionName);
600 
601   m_parent.m_records.push_back(AllocationRecord(
602       (uintptr_t)return_value,
603       lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
604       GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
605       Alignment, SectionID, SectionName.str().c_str()));
606 
607   if (log) {
608     log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
609                 ", Alignment=%u, SectionID=%u) = %p",
610                 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
611   }
612 
613   if (m_parent.m_reported_allocations) {
614     Status err;
615     lldb::ProcessSP process_sp =
616         m_parent.GetBestExecutionContextScope()->CalculateProcess();
617 
618     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
619   }
620 
621   return return_value;
622 }
623 
624 uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
625     uintptr_t Size, unsigned Alignment, unsigned SectionID,
626     llvm::StringRef SectionName, bool IsReadOnly) {
627   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
628 
629   uint8_t *return_value = m_default_mm_ap->allocateDataSection(
630       Size, Alignment, SectionID, SectionName, IsReadOnly);
631 
632   uint32_t permissions = lldb::ePermissionsReadable;
633   if (!IsReadOnly)
634     permissions |= lldb::ePermissionsWritable;
635   m_parent.m_records.push_back(AllocationRecord(
636       (uintptr_t)return_value, permissions,
637       GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
638       Alignment, SectionID, SectionName.str().c_str()));
639   if (log) {
640     log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
641                 ", Alignment=%u, SectionID=%u) = %p",
642                 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
643   }
644 
645   if (m_parent.m_reported_allocations) {
646     Status err;
647     lldb::ProcessSP process_sp =
648         m_parent.GetBestExecutionContextScope()->CalculateProcess();
649 
650     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
651   }
652 
653   return return_value;
654 }
655 
656 static ConstString
657 FindBestAlternateMangledName(const ConstString &demangled,
658                              const lldb::LanguageType &lang_type,
659                              const SymbolContext &sym_ctx) {
660   CPlusPlusLanguage::MethodName cpp_name(demangled);
661   std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
662 
663   if (!scope_qualified_name.size())
664     return ConstString();
665 
666   if (!sym_ctx.module_sp)
667     return ConstString();
668 
669   SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
670   if (!sym_vendor)
671     return ConstString();
672 
673   lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
674   if (!sym_file)
675     return ConstString();
676 
677   std::vector<ConstString> alternates;
678   sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
679 
680   std::vector<ConstString> param_and_qual_matches;
681   std::vector<ConstString> param_matches;
682   for (size_t i = 0; i < alternates.size(); i++) {
683     ConstString alternate_mangled_name = alternates[i];
684     Mangled mangled(alternate_mangled_name, true);
685     ConstString demangled = mangled.GetDemangledName(lang_type);
686 
687     CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
688     if (!cpp_name.IsValid())
689       continue;
690 
691     if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
692       if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
693         param_and_qual_matches.push_back(alternate_mangled_name);
694       else
695         param_matches.push_back(alternate_mangled_name);
696     }
697   }
698 
699   if (param_and_qual_matches.size())
700     return param_and_qual_matches[0]; // It is assumed that there will be only
701                                       // one!
702   else if (param_matches.size())
703     return param_matches[0]; // Return one of them as a best match
704   else
705     return ConstString();
706 }
707 
708 struct IRExecutionUnit::SearchSpec {
709   ConstString name;
710   uint32_t mask;
711 
712   SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeFull)
713       : name(n), mask(m) {}
714 };
715 
716 void IRExecutionUnit::CollectCandidateCNames(
717     std::vector<IRExecutionUnit::SearchSpec> &C_specs,
718     const ConstString &name) {
719   if (m_strip_underscore && name.AsCString()[0] == '_')
720     C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
721   C_specs.push_back(SearchSpec(name));
722 }
723 
724 void IRExecutionUnit::CollectCandidateCPlusPlusNames(
725     std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
726     const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
727   for (const SearchSpec &C_spec : C_specs) {
728     const ConstString &name = C_spec.name;
729 
730     if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
731       Mangled mangled(name, true);
732       ConstString demangled =
733           mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
734 
735       if (demangled) {
736         ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
737             demangled, lldb::eLanguageTypeC_plus_plus, sc);
738 
739         if (best_alternate_mangled_name) {
740           CPP_specs.push_back(best_alternate_mangled_name);
741         }
742 
743         CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
744       }
745     }
746 
747     std::set<ConstString> alternates;
748     CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
749     CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
750   }
751 }
752 
753 void IRExecutionUnit::CollectFallbackNames(
754     std::vector<SearchSpec> &fallback_specs,
755     const std::vector<SearchSpec> &C_specs) {
756   // As a last-ditch fallback, try the base name for C++ names.  It's terrible,
757   // but the DWARF doesn't always encode "extern C" correctly.
758 
759   for (const SearchSpec &C_spec : C_specs) {
760     const ConstString &name = C_spec.name;
761 
762     if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
763       Mangled mangled_name(name);
764       ConstString demangled_name =
765           mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
766       if (!demangled_name.IsEmpty()) {
767         const char *demangled_cstr = demangled_name.AsCString();
768         const char *lparen_loc = strchr(demangled_cstr, '(');
769         if (lparen_loc) {
770           llvm::StringRef base_name(demangled_cstr,
771                                     lparen_loc - demangled_cstr);
772           fallback_specs.push_back(ConstString(base_name));
773         }
774       }
775     }
776   }
777 }
778 
779 lldb::addr_t IRExecutionUnit::FindInSymbols(
780     const std::vector<IRExecutionUnit::SearchSpec> &specs,
781     const lldb_private::SymbolContext &sc) {
782   Target *target = sc.target_sp.get();
783 
784   if (!target) {
785     // we shouldn't be doing any symbol lookup at all without a target
786     return LLDB_INVALID_ADDRESS;
787   }
788 
789   for (const SearchSpec &spec : specs) {
790     SymbolContextList sc_list;
791 
792     lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
793 
794     std::function<bool(lldb::addr_t &, SymbolContextList &,
795                        const lldb_private::SymbolContext &)>
796         get_external_load_address = [&best_internal_load_address, target](
797             lldb::addr_t &load_address, SymbolContextList &sc_list,
798             const lldb_private::SymbolContext &sc) -> lldb::addr_t {
799       load_address = LLDB_INVALID_ADDRESS;
800 
801       for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
802         SymbolContext candidate_sc;
803 
804         sc_list.GetContextAtIndex(si, candidate_sc);
805 
806         const bool is_external =
807             (candidate_sc.function) ||
808             (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
809         if (candidate_sc.symbol) {
810           load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
811 
812           if (load_address == LLDB_INVALID_ADDRESS) {
813             if (target->GetProcessSP())
814               load_address =
815                   candidate_sc.symbol->GetAddress().GetLoadAddress(target);
816             else
817               load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
818           }
819         }
820 
821         if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
822           if (target->GetProcessSP())
823             load_address = candidate_sc.function->GetAddressRange()
824                                .GetBaseAddress()
825                                .GetLoadAddress(target);
826           else
827             load_address = candidate_sc.function->GetAddressRange()
828                                .GetBaseAddress()
829                                .GetFileAddress();
830         }
831 
832         if (load_address != LLDB_INVALID_ADDRESS) {
833           if (is_external) {
834             return true;
835           } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
836             best_internal_load_address = load_address;
837             load_address = LLDB_INVALID_ADDRESS;
838           }
839         }
840       }
841 
842       return false;
843     };
844 
845     if (sc.module_sp) {
846       sc.module_sp->FindFunctions(spec.name, NULL, spec.mask,
847                                   true,  // include_symbols
848                                   false, // include_inlines
849                                   true,  // append
850                                   sc_list);
851     }
852 
853     lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
854 
855     if (get_external_load_address(load_address, sc_list, sc)) {
856       return load_address;
857     } else {
858       sc_list.Clear();
859     }
860 
861     if (sc_list.GetSize() == 0 && sc.target_sp) {
862       sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
863                                               true,  // include_symbols
864                                               false, // include_inlines
865                                               true,  // append
866                                               sc_list);
867     }
868 
869     if (get_external_load_address(load_address, sc_list, sc)) {
870       return load_address;
871     } else {
872       sc_list.Clear();
873     }
874 
875     if (sc_list.GetSize() == 0 && sc.target_sp) {
876       sc.target_sp->GetImages().FindSymbolsWithNameAndType(
877           spec.name, lldb::eSymbolTypeAny, sc_list);
878     }
879 
880     if (get_external_load_address(load_address, sc_list, sc)) {
881       return load_address;
882     }
883     // if there are any searches we try after this, add an sc_list.Clear() in
884     // an "else" clause here
885 
886     if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
887       return best_internal_load_address;
888     }
889   }
890 
891   return LLDB_INVALID_ADDRESS;
892 }
893 
894 lldb::addr_t
895 IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
896                                 const lldb_private::SymbolContext &sc) {
897   lldb::TargetSP target_sp = sc.target_sp;
898 
899   if (!target_sp) {
900     return LLDB_INVALID_ADDRESS;
901   }
902 
903   lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
904 
905   if (!process_sp) {
906     return LLDB_INVALID_ADDRESS;
907   }
908 
909   ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
910 
911   if (runtime) {
912     for (const SearchSpec &spec : specs) {
913       lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
914 
915       if (symbol_load_addr != LLDB_INVALID_ADDRESS)
916         return symbol_load_addr;
917     }
918   }
919 
920   return LLDB_INVALID_ADDRESS;
921 }
922 
923 lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
924     const std::vector<SearchSpec> &specs,
925     const lldb_private::SymbolContext &sc) {
926   lldb::TargetSP target_sp = sc.target_sp;
927 
928   for (const SearchSpec &spec : specs) {
929     lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
930 
931     if (symbol_load_addr != LLDB_INVALID_ADDRESS)
932       return symbol_load_addr;
933   }
934 
935   return LLDB_INVALID_ADDRESS;
936 }
937 
938 lldb::addr_t
939 IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) {
940   std::vector<SearchSpec> candidate_C_names;
941   std::vector<SearchSpec> candidate_CPlusPlus_names;
942 
943   CollectCandidateCNames(candidate_C_names, name);
944 
945   lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
946   if (ret == LLDB_INVALID_ADDRESS)
947     ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
948 
949   if (ret == LLDB_INVALID_ADDRESS)
950     ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
951 
952   if (ret == LLDB_INVALID_ADDRESS) {
953     CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
954                                    m_sym_ctx);
955     ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
956   }
957 
958   if (ret == LLDB_INVALID_ADDRESS) {
959     std::vector<SearchSpec> candidate_fallback_names;
960 
961     CollectFallbackNames(candidate_fallback_names, candidate_C_names);
962     ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
963   }
964 
965   return ret;
966 }
967 
968 void IRExecutionUnit::GetStaticInitializers(
969     std::vector<lldb::addr_t> &static_initializers) {
970   if (llvm::GlobalVariable *global_ctors =
971           m_module->getNamedGlobal("llvm.global_ctors")) {
972     if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
973             global_ctors->getInitializer())) {
974       for (llvm::Use &ctor_use : ctor_array->operands()) {
975         if (llvm::ConstantStruct *ctor_struct =
976                 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
977           lldbassert(ctor_struct->getNumOperands() ==
978                      3); // this is standardized
979           if (llvm::Function *ctor_function =
980                   llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
981             ConstString ctor_function_name_cs(ctor_function->getName().str());
982 
983             for (JittedFunction &jitted_function : m_jitted_functions) {
984               if (ctor_function_name_cs == jitted_function.m_name) {
985                 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
986                   static_initializers.push_back(jitted_function.m_remote_addr);
987                 }
988                 break;
989               }
990             }
991           }
992         }
993       }
994     }
995   }
996 }
997 
998 uint64_t
999 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
1000   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1001 
1002   ConstString name_cs(Name.c_str());
1003 
1004   lldb::addr_t ret = m_parent.FindSymbol(name_cs);
1005 
1006   if (ret == LLDB_INVALID_ADDRESS) {
1007     if (log)
1008       log->Printf(
1009           "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1010           Name.c_str());
1011 
1012     m_parent.ReportSymbolLookupError(name_cs);
1013     return 0xbad0bad0;
1014   } else {
1015     if (log)
1016       log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1017                   Name.c_str(), ret);
1018     return ret;
1019   }
1020 }
1021 
1022 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1023     const std::string &Name, bool AbortOnFailure) {
1024   assert(sizeof(void *) == 8);
1025 
1026   return (void *)getSymbolAddress(Name);
1027 }
1028 
1029 lldb::addr_t
1030 IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1031   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1032 
1033   for (AllocationRecord &record : m_records) {
1034     if (local_address >= record.m_host_address &&
1035         local_address < record.m_host_address + record.m_size) {
1036       if (record.m_process_address == LLDB_INVALID_ADDRESS)
1037         return LLDB_INVALID_ADDRESS;
1038 
1039       lldb::addr_t ret =
1040           record.m_process_address + (local_address - record.m_host_address);
1041 
1042       if (log) {
1043         log->Printf(
1044             "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1045             " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1046             " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1047             local_address, (uint64_t)record.m_host_address,
1048             (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1049             record.m_process_address, record.m_process_address + record.m_size);
1050       }
1051 
1052       return ret;
1053     }
1054   }
1055 
1056   return LLDB_INVALID_ADDRESS;
1057 }
1058 
1059 IRExecutionUnit::AddrRange
1060 IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1061   for (AllocationRecord &record : m_records) {
1062     if (local_address >= record.m_host_address &&
1063         local_address < record.m_host_address + record.m_size) {
1064       if (record.m_process_address == LLDB_INVALID_ADDRESS)
1065         return AddrRange(0, 0);
1066 
1067       return AddrRange(record.m_process_address, record.m_size);
1068     }
1069   }
1070 
1071   return AddrRange(0, 0);
1072 }
1073 
1074 bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1075                                           Status &error,
1076                                           AllocationRecord &record) {
1077   if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1078     return true;
1079   }
1080 
1081   switch (record.m_sect_type) {
1082   case lldb::eSectionTypeInvalid:
1083   case lldb::eSectionTypeDWARFDebugAbbrev:
1084   case lldb::eSectionTypeDWARFDebugAddr:
1085   case lldb::eSectionTypeDWARFDebugAranges:
1086   case lldb::eSectionTypeDWARFDebugCuIndex:
1087   case lldb::eSectionTypeDWARFDebugFrame:
1088   case lldb::eSectionTypeDWARFDebugInfo:
1089   case lldb::eSectionTypeDWARFDebugLine:
1090   case lldb::eSectionTypeDWARFDebugLoc:
1091   case lldb::eSectionTypeDWARFDebugMacInfo:
1092   case lldb::eSectionTypeDWARFDebugPubNames:
1093   case lldb::eSectionTypeDWARFDebugPubTypes:
1094   case lldb::eSectionTypeDWARFDebugRanges:
1095   case lldb::eSectionTypeDWARFDebugStr:
1096   case lldb::eSectionTypeDWARFDebugStrOffsets:
1097   case lldb::eSectionTypeDWARFAppleNames:
1098   case lldb::eSectionTypeDWARFAppleTypes:
1099   case lldb::eSectionTypeDWARFAppleNamespaces:
1100   case lldb::eSectionTypeDWARFAppleObjC:
1101   case lldb::eSectionTypeDWARFGNUDebugAltLink:
1102     error.Clear();
1103     break;
1104   default:
1105     const bool zero_memory = false;
1106     record.m_process_address =
1107         Malloc(record.m_size, record.m_alignment, record.m_permissions,
1108                eAllocationPolicyProcessOnly, zero_memory, error);
1109     break;
1110   }
1111 
1112   return error.Success();
1113 }
1114 
1115 bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1116   bool ret = true;
1117 
1118   lldb_private::Status err;
1119 
1120   for (AllocationRecord &record : m_records) {
1121     ret = CommitOneAllocation(process_sp, err, record);
1122 
1123     if (!ret) {
1124       break;
1125     }
1126   }
1127 
1128   if (!ret) {
1129     for (AllocationRecord &record : m_records) {
1130       if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1131         Free(record.m_process_address, err);
1132         record.m_process_address = LLDB_INVALID_ADDRESS;
1133       }
1134     }
1135   }
1136 
1137   return ret;
1138 }
1139 
1140 void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1141   m_reported_allocations = true;
1142 
1143   for (AllocationRecord &record : m_records) {
1144     if (record.m_process_address == LLDB_INVALID_ADDRESS)
1145       continue;
1146 
1147     if (record.m_section_id == eSectionIDInvalid)
1148       continue;
1149 
1150     engine.mapSectionAddress((void *)record.m_host_address,
1151                              record.m_process_address);
1152   }
1153 
1154   // Trigger re-application of relocations.
1155   engine.finalizeObject();
1156 }
1157 
1158 bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1159   bool wrote_something = false;
1160   for (AllocationRecord &record : m_records) {
1161     if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1162       lldb_private::Status err;
1163       WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1164                   record.m_size, err);
1165       if (err.Success())
1166         wrote_something = true;
1167     }
1168   }
1169   return wrote_something;
1170 }
1171 
1172 void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1173   if (!log)
1174     return;
1175 
1176   log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1177               (unsigned long long)m_host_address, (unsigned long long)m_size,
1178               (unsigned long long)m_process_address, (unsigned)m_alignment,
1179               (unsigned)m_section_id, m_name.c_str());
1180 }
1181 
1182 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1183   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1184   return exe_ctx.GetByteOrder();
1185 }
1186 
1187 uint32_t IRExecutionUnit::GetAddressByteSize() const {
1188   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1189   return exe_ctx.GetAddressByteSize();
1190 }
1191 
1192 void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1193                                      lldb_private::Symtab &symtab) {
1194   // No symbols yet...
1195 }
1196 
1197 void IRExecutionUnit::PopulateSectionList(
1198     lldb_private::ObjectFile *obj_file,
1199     lldb_private::SectionList &section_list) {
1200   for (AllocationRecord &record : m_records) {
1201     if (record.m_size > 0) {
1202       lldb::SectionSP section_sp(new lldb_private::Section(
1203           obj_file->GetModule(), obj_file, record.m_section_id,
1204           ConstString(record.m_name), record.m_sect_type,
1205           record.m_process_address, record.m_size,
1206           record.m_host_address, // file_offset (which is the host address for
1207                                  // the data)
1208           record.m_size,         // file_size
1209           0,
1210           record.m_permissions)); // flags
1211       section_list.AddSection(section_sp);
1212     }
1213   }
1214 }
1215 
1216 bool IRExecutionUnit::GetArchitecture(lldb_private::ArchSpec &arch) {
1217   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1218   Target *target = exe_ctx.GetTargetPtr();
1219   if (target)
1220     arch = target->GetArchitecture();
1221   else
1222     arch.Clear();
1223   return arch.IsValid();
1224 }
1225 
1226 lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1227   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1228   Target *target = exe_ctx.GetTargetPtr();
1229   if (!target)
1230     return nullptr;
1231 
1232   auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1233       shared_from_this());
1234 
1235   lldb::ModuleSP jit_module_sp =
1236       lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1237   if (!jit_module_sp)
1238     return nullptr;
1239 
1240   bool changed = false;
1241   jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1242   return jit_module_sp;
1243 }
1244