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 
IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> & context_ap,std::unique_ptr<llvm::Module> & module_ap,ConstString & name,const lldb::TargetSP & target_sp,const SymbolContext & sym_ctx,std::vector<std::string> & cpu_features)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 
WriteNow(const uint8_t * bytes,size_t size,Status & error)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 
FreeNow(lldb::addr_t allocation)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 
DisassembleFunction(Stream & stream,lldb::ProcessSP & process_wp)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 
ReportInlineAsmError(const llvm::SMDiagnostic & diagnostic,void * Context,unsigned LocCookie)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 
ReportSymbolLookupError(const ConstString & name)216 void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) {
217   m_failed_lookups.push_back(name);
218 }
219 
GetRunnableInfo(Status & error,lldb::addr_t & func_addr,lldb::addr_t & func_end)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 
~IRExecutionUnit()491 IRExecutionUnit::~IRExecutionUnit() {
492   m_module_ap.reset();
493   m_execution_engine_ap.reset();
494   m_context_ap.reset();
495 }
496 
MemoryManager(IRExecutionUnit & parent)497 IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
498     : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {}
499 
~MemoryManager()500 IRExecutionUnit::MemoryManager::~MemoryManager() {}
501 
GetSectionTypeFromSectionName(const llvm::StringRef & name,IRExecutionUnit::AllocationKind alloc_kind)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         else if (dwarf_name.equals("loclists"))
557           sect_type = lldb::eSectionTypeDWARFDebugLocLists;
558         break;
559 
560       case 'm':
561         if (dwarf_name.equals("macinfo"))
562           sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
563         break;
564 
565       case 'p':
566         if (dwarf_name.equals("pubnames"))
567           sect_type = lldb::eSectionTypeDWARFDebugPubNames;
568         else if (dwarf_name.equals("pubtypes"))
569           sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
570         break;
571 
572       case 's':
573         if (dwarf_name.equals("str"))
574           sect_type = lldb::eSectionTypeDWARFDebugStr;
575         else if (dwarf_name.equals("str_offsets"))
576           sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
577         break;
578 
579       case 'r':
580         if (dwarf_name.equals("ranges"))
581           sect_type = lldb::eSectionTypeDWARFDebugRanges;
582         break;
583 
584       default:
585         break;
586       }
587     } else if (name.startswith("__apple_") || name.startswith(".apple_"))
588       sect_type = lldb::eSectionTypeInvalid;
589     else if (name.equals("__objc_imageinfo"))
590       sect_type = lldb::eSectionTypeOther;
591   }
592   return sect_type;
593 }
594 
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,llvm::StringRef SectionName)595 uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
596     uintptr_t Size, unsigned Alignment, unsigned SectionID,
597     llvm::StringRef SectionName) {
598   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
599 
600   uint8_t *return_value = m_default_mm_ap->allocateCodeSection(
601       Size, Alignment, SectionID, SectionName);
602 
603   m_parent.m_records.push_back(AllocationRecord(
604       (uintptr_t)return_value,
605       lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
606       GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
607       Alignment, SectionID, SectionName.str().c_str()));
608 
609   if (log) {
610     log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
611                 ", Alignment=%u, SectionID=%u) = %p",
612                 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
613   }
614 
615   if (m_parent.m_reported_allocations) {
616     Status err;
617     lldb::ProcessSP process_sp =
618         m_parent.GetBestExecutionContextScope()->CalculateProcess();
619 
620     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
621   }
622 
623   return return_value;
624 }
625 
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,llvm::StringRef SectionName,bool IsReadOnly)626 uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
627     uintptr_t Size, unsigned Alignment, unsigned SectionID,
628     llvm::StringRef SectionName, bool IsReadOnly) {
629   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
630 
631   uint8_t *return_value = m_default_mm_ap->allocateDataSection(
632       Size, Alignment, SectionID, SectionName, IsReadOnly);
633 
634   uint32_t permissions = lldb::ePermissionsReadable;
635   if (!IsReadOnly)
636     permissions |= lldb::ePermissionsWritable;
637   m_parent.m_records.push_back(AllocationRecord(
638       (uintptr_t)return_value, permissions,
639       GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
640       Alignment, SectionID, SectionName.str().c_str()));
641   if (log) {
642     log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
643                 ", Alignment=%u, SectionID=%u) = %p",
644                 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
645   }
646 
647   if (m_parent.m_reported_allocations) {
648     Status err;
649     lldb::ProcessSP process_sp =
650         m_parent.GetBestExecutionContextScope()->CalculateProcess();
651 
652     m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
653   }
654 
655   return return_value;
656 }
657 
658 static ConstString
FindBestAlternateMangledName(const ConstString & demangled,const lldb::LanguageType & lang_type,const SymbolContext & sym_ctx)659 FindBestAlternateMangledName(const ConstString &demangled,
660                              const lldb::LanguageType &lang_type,
661                              const SymbolContext &sym_ctx) {
662   CPlusPlusLanguage::MethodName cpp_name(demangled);
663   std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
664 
665   if (!scope_qualified_name.size())
666     return ConstString();
667 
668   if (!sym_ctx.module_sp)
669     return ConstString();
670 
671   SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
672   if (!sym_vendor)
673     return ConstString();
674 
675   lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
676   if (!sym_file)
677     return ConstString();
678 
679   std::vector<ConstString> alternates;
680   sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
681 
682   std::vector<ConstString> param_and_qual_matches;
683   std::vector<ConstString> param_matches;
684   for (size_t i = 0; i < alternates.size(); i++) {
685     ConstString alternate_mangled_name = alternates[i];
686     Mangled mangled(alternate_mangled_name, true);
687     ConstString demangled = mangled.GetDemangledName(lang_type);
688 
689     CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
690     if (!cpp_name.IsValid())
691       continue;
692 
693     if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
694       if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
695         param_and_qual_matches.push_back(alternate_mangled_name);
696       else
697         param_matches.push_back(alternate_mangled_name);
698     }
699   }
700 
701   if (param_and_qual_matches.size())
702     return param_and_qual_matches[0]; // It is assumed that there will be only
703                                       // one!
704   else if (param_matches.size())
705     return param_matches[0]; // Return one of them as a best match
706   else
707     return ConstString();
708 }
709 
710 struct IRExecutionUnit::SearchSpec {
711   ConstString name;
712   lldb::FunctionNameType mask;
713 
SearchSpecIRExecutionUnit::SearchSpec714   SearchSpec(ConstString n,
715              lldb::FunctionNameType m = lldb::eFunctionNameTypeFull)
716       : name(n), mask(m) {}
717 };
718 
CollectCandidateCNames(std::vector<IRExecutionUnit::SearchSpec> & C_specs,const ConstString & name)719 void IRExecutionUnit::CollectCandidateCNames(
720     std::vector<IRExecutionUnit::SearchSpec> &C_specs,
721     const ConstString &name) {
722   if (m_strip_underscore && name.AsCString()[0] == '_')
723     C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
724   C_specs.push_back(SearchSpec(name));
725 }
726 
CollectCandidateCPlusPlusNames(std::vector<IRExecutionUnit::SearchSpec> & CPP_specs,const std::vector<SearchSpec> & C_specs,const SymbolContext & sc)727 void IRExecutionUnit::CollectCandidateCPlusPlusNames(
728     std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
729     const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
730   for (const SearchSpec &C_spec : C_specs) {
731     const ConstString &name = C_spec.name;
732 
733     if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
734       Mangled mangled(name, true);
735       ConstString demangled =
736           mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
737 
738       if (demangled) {
739         ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
740             demangled, lldb::eLanguageTypeC_plus_plus, sc);
741 
742         if (best_alternate_mangled_name) {
743           CPP_specs.push_back(best_alternate_mangled_name);
744         }
745 
746         CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
747       }
748     }
749 
750     std::set<ConstString> alternates;
751     CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
752     CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
753   }
754 }
755 
CollectFallbackNames(std::vector<SearchSpec> & fallback_specs,const std::vector<SearchSpec> & C_specs)756 void IRExecutionUnit::CollectFallbackNames(
757     std::vector<SearchSpec> &fallback_specs,
758     const std::vector<SearchSpec> &C_specs) {
759   // As a last-ditch fallback, try the base name for C++ names.  It's terrible,
760   // but the DWARF doesn't always encode "extern C" correctly.
761 
762   for (const SearchSpec &C_spec : C_specs) {
763     const ConstString &name = C_spec.name;
764 
765     if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
766       Mangled mangled_name(name);
767       ConstString demangled_name =
768           mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
769       if (!demangled_name.IsEmpty()) {
770         const char *demangled_cstr = demangled_name.AsCString();
771         const char *lparen_loc = strchr(demangled_cstr, '(');
772         if (lparen_loc) {
773           llvm::StringRef base_name(demangled_cstr,
774                                     lparen_loc - demangled_cstr);
775           fallback_specs.push_back(ConstString(base_name));
776         }
777       }
778     }
779   }
780 }
781 
FindInSymbols(const std::vector<IRExecutionUnit::SearchSpec> & specs,const lldb_private::SymbolContext & sc)782 lldb::addr_t IRExecutionUnit::FindInSymbols(
783     const std::vector<IRExecutionUnit::SearchSpec> &specs,
784     const lldb_private::SymbolContext &sc) {
785   Target *target = sc.target_sp.get();
786 
787   if (!target) {
788     // we shouldn't be doing any symbol lookup at all without a target
789     return LLDB_INVALID_ADDRESS;
790   }
791 
792   for (const SearchSpec &spec : specs) {
793     SymbolContextList sc_list;
794 
795     lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
796 
797     std::function<bool(lldb::addr_t &, SymbolContextList &,
798                        const lldb_private::SymbolContext &)>
799         get_external_load_address = [&best_internal_load_address, target](
800             lldb::addr_t &load_address, SymbolContextList &sc_list,
801             const lldb_private::SymbolContext &sc) -> lldb::addr_t {
802       load_address = LLDB_INVALID_ADDRESS;
803 
804       for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
805         SymbolContext candidate_sc;
806 
807         sc_list.GetContextAtIndex(si, candidate_sc);
808 
809         const bool is_external =
810             (candidate_sc.function) ||
811             (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
812         if (candidate_sc.symbol) {
813           load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
814 
815           if (load_address == LLDB_INVALID_ADDRESS) {
816             if (target->GetProcessSP())
817               load_address =
818                   candidate_sc.symbol->GetAddress().GetLoadAddress(target);
819             else
820               load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
821           }
822         }
823 
824         if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
825           if (target->GetProcessSP())
826             load_address = candidate_sc.function->GetAddressRange()
827                                .GetBaseAddress()
828                                .GetLoadAddress(target);
829           else
830             load_address = candidate_sc.function->GetAddressRange()
831                                .GetBaseAddress()
832                                .GetFileAddress();
833         }
834 
835         if (load_address != LLDB_INVALID_ADDRESS) {
836           if (is_external) {
837             return true;
838           } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
839             best_internal_load_address = load_address;
840             load_address = LLDB_INVALID_ADDRESS;
841           }
842         }
843       }
844 
845       return false;
846     };
847 
848     if (sc.module_sp) {
849       sc.module_sp->FindFunctions(spec.name, NULL, spec.mask,
850                                   true,  // include_symbols
851                                   false, // include_inlines
852                                   true,  // append
853                                   sc_list);
854     }
855 
856     lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
857 
858     if (get_external_load_address(load_address, sc_list, sc)) {
859       return load_address;
860     } else {
861       sc_list.Clear();
862     }
863 
864     if (sc_list.GetSize() == 0 && sc.target_sp) {
865       sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
866                                               true,  // include_symbols
867                                               false, // include_inlines
868                                               true,  // append
869                                               sc_list);
870     }
871 
872     if (get_external_load_address(load_address, sc_list, sc)) {
873       return load_address;
874     } else {
875       sc_list.Clear();
876     }
877 
878     if (sc_list.GetSize() == 0 && sc.target_sp) {
879       sc.target_sp->GetImages().FindSymbolsWithNameAndType(
880           spec.name, lldb::eSymbolTypeAny, sc_list);
881     }
882 
883     if (get_external_load_address(load_address, sc_list, sc)) {
884       return load_address;
885     }
886     // if there are any searches we try after this, add an sc_list.Clear() in
887     // an "else" clause here
888 
889     if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
890       return best_internal_load_address;
891     }
892   }
893 
894   return LLDB_INVALID_ADDRESS;
895 }
896 
897 lldb::addr_t
FindInRuntimes(const std::vector<SearchSpec> & specs,const lldb_private::SymbolContext & sc)898 IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
899                                 const lldb_private::SymbolContext &sc) {
900   lldb::TargetSP target_sp = sc.target_sp;
901 
902   if (!target_sp) {
903     return LLDB_INVALID_ADDRESS;
904   }
905 
906   lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
907 
908   if (!process_sp) {
909     return LLDB_INVALID_ADDRESS;
910   }
911 
912   ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
913 
914   if (runtime) {
915     for (const SearchSpec &spec : specs) {
916       lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
917 
918       if (symbol_load_addr != LLDB_INVALID_ADDRESS)
919         return symbol_load_addr;
920     }
921   }
922 
923   return LLDB_INVALID_ADDRESS;
924 }
925 
FindInUserDefinedSymbols(const std::vector<SearchSpec> & specs,const lldb_private::SymbolContext & sc)926 lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
927     const std::vector<SearchSpec> &specs,
928     const lldb_private::SymbolContext &sc) {
929   lldb::TargetSP target_sp = sc.target_sp;
930 
931   for (const SearchSpec &spec : specs) {
932     lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
933 
934     if (symbol_load_addr != LLDB_INVALID_ADDRESS)
935       return symbol_load_addr;
936   }
937 
938   return LLDB_INVALID_ADDRESS;
939 }
940 
941 lldb::addr_t
FindSymbol(const lldb_private::ConstString & name)942 IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) {
943   std::vector<SearchSpec> candidate_C_names;
944   std::vector<SearchSpec> candidate_CPlusPlus_names;
945 
946   CollectCandidateCNames(candidate_C_names, name);
947 
948   lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
949   if (ret == LLDB_INVALID_ADDRESS)
950     ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
951 
952   if (ret == LLDB_INVALID_ADDRESS)
953     ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
954 
955   if (ret == LLDB_INVALID_ADDRESS) {
956     CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
957                                    m_sym_ctx);
958     ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
959   }
960 
961   if (ret == LLDB_INVALID_ADDRESS) {
962     std::vector<SearchSpec> candidate_fallback_names;
963 
964     CollectFallbackNames(candidate_fallback_names, candidate_C_names);
965     ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
966   }
967 
968   return ret;
969 }
970 
GetStaticInitializers(std::vector<lldb::addr_t> & static_initializers)971 void IRExecutionUnit::GetStaticInitializers(
972     std::vector<lldb::addr_t> &static_initializers) {
973   if (llvm::GlobalVariable *global_ctors =
974           m_module->getNamedGlobal("llvm.global_ctors")) {
975     if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
976             global_ctors->getInitializer())) {
977       for (llvm::Use &ctor_use : ctor_array->operands()) {
978         if (llvm::ConstantStruct *ctor_struct =
979                 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
980           lldbassert(ctor_struct->getNumOperands() ==
981                      3); // this is standardized
982           if (llvm::Function *ctor_function =
983                   llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
984             ConstString ctor_function_name_cs(ctor_function->getName().str());
985 
986             for (JittedFunction &jitted_function : m_jitted_functions) {
987               if (ctor_function_name_cs == jitted_function.m_name) {
988                 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
989                   static_initializers.push_back(jitted_function.m_remote_addr);
990                 }
991                 break;
992               }
993             }
994           }
995         }
996       }
997     }
998   }
999 }
1000 
1001 uint64_t
getSymbolAddress(const std::string & Name)1002 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
1003   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1004 
1005   ConstString name_cs(Name.c_str());
1006 
1007   lldb::addr_t ret = m_parent.FindSymbol(name_cs);
1008 
1009   if (ret == LLDB_INVALID_ADDRESS) {
1010     if (log)
1011       log->Printf(
1012           "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1013           Name.c_str());
1014 
1015     m_parent.ReportSymbolLookupError(name_cs);
1016     return 0;
1017   } else {
1018     if (log)
1019       log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1020                   Name.c_str(), ret);
1021     return ret;
1022   }
1023 }
1024 
getPointerToNamedFunction(const std::string & Name,bool AbortOnFailure)1025 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1026     const std::string &Name, bool AbortOnFailure) {
1027   assert(sizeof(void *) == 8);
1028 
1029   return (void *)getSymbolAddress(Name);
1030 }
1031 
1032 lldb::addr_t
GetRemoteAddressForLocal(lldb::addr_t local_address)1033 IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1034   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1035 
1036   for (AllocationRecord &record : m_records) {
1037     if (local_address >= record.m_host_address &&
1038         local_address < record.m_host_address + record.m_size) {
1039       if (record.m_process_address == LLDB_INVALID_ADDRESS)
1040         return LLDB_INVALID_ADDRESS;
1041 
1042       lldb::addr_t ret =
1043           record.m_process_address + (local_address - record.m_host_address);
1044 
1045       if (log) {
1046         log->Printf(
1047             "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1048             " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1049             " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1050             local_address, (uint64_t)record.m_host_address,
1051             (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1052             record.m_process_address, record.m_process_address + record.m_size);
1053       }
1054 
1055       return ret;
1056     }
1057   }
1058 
1059   return LLDB_INVALID_ADDRESS;
1060 }
1061 
1062 IRExecutionUnit::AddrRange
GetRemoteRangeForLocal(lldb::addr_t local_address)1063 IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1064   for (AllocationRecord &record : m_records) {
1065     if (local_address >= record.m_host_address &&
1066         local_address < record.m_host_address + record.m_size) {
1067       if (record.m_process_address == LLDB_INVALID_ADDRESS)
1068         return AddrRange(0, 0);
1069 
1070       return AddrRange(record.m_process_address, record.m_size);
1071     }
1072   }
1073 
1074   return AddrRange(0, 0);
1075 }
1076 
CommitOneAllocation(lldb::ProcessSP & process_sp,Status & error,AllocationRecord & record)1077 bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1078                                           Status &error,
1079                                           AllocationRecord &record) {
1080   if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1081     return true;
1082   }
1083 
1084   switch (record.m_sect_type) {
1085   case lldb::eSectionTypeInvalid:
1086   case lldb::eSectionTypeDWARFDebugAbbrev:
1087   case lldb::eSectionTypeDWARFDebugAddr:
1088   case lldb::eSectionTypeDWARFDebugAranges:
1089   case lldb::eSectionTypeDWARFDebugCuIndex:
1090   case lldb::eSectionTypeDWARFDebugFrame:
1091   case lldb::eSectionTypeDWARFDebugInfo:
1092   case lldb::eSectionTypeDWARFDebugLine:
1093   case lldb::eSectionTypeDWARFDebugLoc:
1094   case lldb::eSectionTypeDWARFDebugLocLists:
1095   case lldb::eSectionTypeDWARFDebugMacInfo:
1096   case lldb::eSectionTypeDWARFDebugPubNames:
1097   case lldb::eSectionTypeDWARFDebugPubTypes:
1098   case lldb::eSectionTypeDWARFDebugRanges:
1099   case lldb::eSectionTypeDWARFDebugStr:
1100   case lldb::eSectionTypeDWARFDebugStrOffsets:
1101   case lldb::eSectionTypeDWARFAppleNames:
1102   case lldb::eSectionTypeDWARFAppleTypes:
1103   case lldb::eSectionTypeDWARFAppleNamespaces:
1104   case lldb::eSectionTypeDWARFAppleObjC:
1105   case lldb::eSectionTypeDWARFGNUDebugAltLink:
1106     error.Clear();
1107     break;
1108   default:
1109     const bool zero_memory = false;
1110     record.m_process_address =
1111         Malloc(record.m_size, record.m_alignment, record.m_permissions,
1112                eAllocationPolicyProcessOnly, zero_memory, error);
1113     break;
1114   }
1115 
1116   return error.Success();
1117 }
1118 
CommitAllocations(lldb::ProcessSP & process_sp)1119 bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1120   bool ret = true;
1121 
1122   lldb_private::Status err;
1123 
1124   for (AllocationRecord &record : m_records) {
1125     ret = CommitOneAllocation(process_sp, err, record);
1126 
1127     if (!ret) {
1128       break;
1129     }
1130   }
1131 
1132   if (!ret) {
1133     for (AllocationRecord &record : m_records) {
1134       if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1135         Free(record.m_process_address, err);
1136         record.m_process_address = LLDB_INVALID_ADDRESS;
1137       }
1138     }
1139   }
1140 
1141   return ret;
1142 }
1143 
ReportAllocations(llvm::ExecutionEngine & engine)1144 void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1145   m_reported_allocations = true;
1146 
1147   for (AllocationRecord &record : m_records) {
1148     if (record.m_process_address == LLDB_INVALID_ADDRESS)
1149       continue;
1150 
1151     if (record.m_section_id == eSectionIDInvalid)
1152       continue;
1153 
1154     engine.mapSectionAddress((void *)record.m_host_address,
1155                              record.m_process_address);
1156   }
1157 
1158   // Trigger re-application of relocations.
1159   engine.finalizeObject();
1160 }
1161 
WriteData(lldb::ProcessSP & process_sp)1162 bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1163   bool wrote_something = false;
1164   for (AllocationRecord &record : m_records) {
1165     if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1166       lldb_private::Status err;
1167       WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1168                   record.m_size, err);
1169       if (err.Success())
1170         wrote_something = true;
1171     }
1172   }
1173   return wrote_something;
1174 }
1175 
dump(Log * log)1176 void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1177   if (!log)
1178     return;
1179 
1180   log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1181               (unsigned long long)m_host_address, (unsigned long long)m_size,
1182               (unsigned long long)m_process_address, (unsigned)m_alignment,
1183               (unsigned)m_section_id, m_name.c_str());
1184 }
1185 
GetByteOrder() const1186 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1187   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1188   return exe_ctx.GetByteOrder();
1189 }
1190 
GetAddressByteSize() const1191 uint32_t IRExecutionUnit::GetAddressByteSize() const {
1192   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1193   return exe_ctx.GetAddressByteSize();
1194 }
1195 
PopulateSymtab(lldb_private::ObjectFile * obj_file,lldb_private::Symtab & symtab)1196 void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1197                                      lldb_private::Symtab &symtab) {
1198   // No symbols yet...
1199 }
1200 
PopulateSectionList(lldb_private::ObjectFile * obj_file,lldb_private::SectionList & section_list)1201 void IRExecutionUnit::PopulateSectionList(
1202     lldb_private::ObjectFile *obj_file,
1203     lldb_private::SectionList &section_list) {
1204   for (AllocationRecord &record : m_records) {
1205     if (record.m_size > 0) {
1206       lldb::SectionSP section_sp(new lldb_private::Section(
1207           obj_file->GetModule(), obj_file, record.m_section_id,
1208           ConstString(record.m_name), record.m_sect_type,
1209           record.m_process_address, record.m_size,
1210           record.m_host_address, // file_offset (which is the host address for
1211                                  // the data)
1212           record.m_size,         // file_size
1213           0,
1214           record.m_permissions)); // flags
1215       section_list.AddSection(section_sp);
1216     }
1217   }
1218 }
1219 
GetArchitecture()1220 ArchSpec IRExecutionUnit::GetArchitecture() {
1221   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1222   if(Target *target = exe_ctx.GetTargetPtr())
1223     return target->GetArchitecture();
1224   return ArchSpec();
1225 }
1226 
GetJITModule()1227 lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1228   ExecutionContext exe_ctx(GetBestExecutionContextScope());
1229   Target *target = exe_ctx.GetTargetPtr();
1230   if (!target)
1231     return nullptr;
1232 
1233   auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1234       shared_from_this());
1235 
1236   lldb::ModuleSP jit_module_sp =
1237       lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1238   if (!jit_module_sp)
1239     return nullptr;
1240 
1241   bool changed = false;
1242   jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1243   return jit_module_sp;
1244 }
1245