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