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