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