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/Host/HostInfo.h" 25 #include "lldb/Symbol/CompileUnit.h" 26 #include "lldb/Symbol/SymbolContext.h" 27 #include "lldb/Symbol/SymbolFile.h" 28 #include "lldb/Symbol/SymbolVendor.h" 29 #include "lldb/Target/ExecutionContext.h" 30 #include "lldb/Target/Language.h" 31 #include "lldb/Target/LanguageRuntime.h" 32 #include "lldb/Target/Target.h" 33 #include "lldb/Utility/DataBufferHeap.h" 34 #include "lldb/Utility/DataExtractor.h" 35 #include "lldb/Utility/LLDBAssert.h" 36 #include "lldb/Utility/LLDBLog.h" 37 #include "lldb/Utility/Log.h" 38 39 #include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h" 40 #include <optional> 41 42 using namespace lldb_private; 43 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) {} 56 57 lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size, 58 Status &error) { 59 const bool zero_memory = false; 60 lldb::addr_t allocation_process_addr = 61 Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable, 62 eAllocationPolicyMirror, zero_memory, error); 63 64 if (!error.Success()) 65 return LLDB_INVALID_ADDRESS; 66 67 WriteMemory(allocation_process_addr, bytes, size, error); 68 69 if (!error.Success()) { 70 Status err; 71 Free(allocation_process_addr, err); 72 73 return LLDB_INVALID_ADDRESS; 74 } 75 76 if (Log *log = GetLog(LLDBLog::Expressions)) { 77 DataBufferHeap my_buffer(size, 0); 78 Status err; 79 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err); 80 81 if (err.Success()) { 82 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), 83 lldb::eByteOrderBig, 8); 84 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), 85 allocation_process_addr, 16, 86 DataExtractor::TypeUInt8); 87 } 88 } 89 90 return allocation_process_addr; 91 } 92 93 void IRExecutionUnit::FreeNow(lldb::addr_t allocation) { 94 if (allocation == LLDB_INVALID_ADDRESS) 95 return; 96 97 Status err; 98 99 Free(allocation, err); 100 } 101 102 Status IRExecutionUnit::DisassembleFunction(Stream &stream, 103 lldb::ProcessSP &process_wp) { 104 Log *log = GetLog(LLDBLog::Expressions); 105 106 ExecutionContext exe_ctx(process_wp); 107 108 Status ret; 109 110 ret.Clear(); 111 112 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; 113 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; 114 115 for (JittedFunction &function : m_jitted_functions) { 116 if (function.m_name == m_name) { 117 func_local_addr = function.m_local_addr; 118 func_remote_addr = function.m_remote_addr; 119 } 120 } 121 122 if (func_local_addr == LLDB_INVALID_ADDRESS) { 123 ret.SetErrorToGenericError(); 124 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", 125 m_name.AsCString()); 126 return ret; 127 } 128 129 LLDB_LOGF(log, 130 "Found function, has local address 0x%" PRIx64 131 " and remote address 0x%" PRIx64, 132 (uint64_t)func_local_addr, (uint64_t)func_remote_addr); 133 134 std::pair<lldb::addr_t, lldb::addr_t> func_range; 135 136 func_range = GetRemoteRangeForLocal(func_local_addr); 137 138 if (func_range.first == 0 && func_range.second == 0) { 139 ret.SetErrorToGenericError(); 140 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", 141 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.SetErrorToGenericError(); 151 ret.SetErrorString("Couldn't find the target"); 152 return ret; 153 } 154 155 lldb::WritableDataBufferSP buffer_sp( 156 new DataBufferHeap(func_range.second, 0)); 157 158 Process *process = exe_ctx.GetProcessPtr(); 159 Status err; 160 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), 161 buffer_sp->GetByteSize(), err); 162 163 if (!err.Success()) { 164 ret.SetErrorToGenericError(); 165 ret.SetErrorStringWithFormat("Couldn't read from process: %s", 166 err.AsCString("unknown error")); 167 return ret; 168 } 169 170 ArchSpec arch(target->GetArchitecture()); 171 172 const char *plugin_name = nullptr; 173 const char *flavor_string = nullptr; 174 lldb::DisassemblerSP disassembler_sp = 175 Disassembler::FindPlugin(arch, flavor_string, plugin_name); 176 177 if (!disassembler_sp) { 178 ret.SetErrorToGenericError(); 179 ret.SetErrorStringWithFormat( 180 "Unable to find disassembler plug-in for %s architecture.", 181 arch.GetArchitectureName()); 182 return ret; 183 } 184 185 if (!process) { 186 ret.SetErrorToGenericError(); 187 ret.SetErrorString("Couldn't find the process"); 188 return ret; 189 } 190 191 DataExtractor extractor(buffer_sp, process->GetByteOrder(), 192 target->GetArchitecture().GetAddressByteSize()); 193 194 if (log) { 195 LLDB_LOGF(log, "Function data has contents:"); 196 extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16, 197 DataExtractor::TypeUInt8); 198 } 199 200 disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0, 201 UINT32_MAX, false, false); 202 203 InstructionList &instruction_list = disassembler_sp->GetInstructionList(); 204 instruction_list.Dump(&stream, true, true, /*show_control_flow_kind=*/true, 205 &exe_ctx); 206 207 return ret; 208 } 209 210 namespace { 211 struct IRExecDiagnosticHandler : public llvm::DiagnosticHandler { 212 Status *err; 213 IRExecDiagnosticHandler(Status *err) : err(err) {} 214 bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override { 215 if (DI.getKind() == llvm::DK_SrcMgr) { 216 const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(DI); 217 if (err && err->Success()) { 218 err->SetErrorToGenericError(); 219 err->SetErrorStringWithFormat( 220 "Inline assembly error: %s", 221 DISM.getSMDiag().getMessage().str().c_str()); 222 } 223 return true; 224 } 225 226 return false; 227 } 228 }; 229 } // namespace 230 231 void IRExecutionUnit::ReportSymbolLookupError(ConstString name) { 232 m_failed_lookups.push_back(name); 233 } 234 235 void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr, 236 lldb::addr_t &func_end) { 237 lldb::ProcessSP process_sp(GetProcessWP().lock()); 238 239 static std::recursive_mutex s_runnable_info_mutex; 240 241 func_addr = LLDB_INVALID_ADDRESS; 242 func_end = LLDB_INVALID_ADDRESS; 243 244 if (!process_sp) { 245 error.SetErrorToGenericError(); 246 error.SetErrorString("Couldn't write the JIT compiled code into the " 247 "process because the process is invalid"); 248 return; 249 } 250 251 if (m_did_jit) { 252 func_addr = m_function_load_addr; 253 func_end = m_function_end_load_addr; 254 255 return; 256 }; 257 258 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex); 259 260 m_did_jit = true; 261 262 Log *log = GetLog(LLDBLog::Expressions); 263 264 std::string error_string; 265 266 if (log) { 267 std::string s; 268 llvm::raw_string_ostream oss(s); 269 270 m_module->print(oss, nullptr); 271 272 oss.flush(); 273 274 LLDB_LOGF(log, "Module being sent to JIT: \n%s", s.c_str()); 275 } 276 277 m_module_up->getContext().setDiagnosticHandler( 278 std::make_unique<IRExecDiagnosticHandler>(&error)); 279 280 llvm::EngineBuilder builder(std::move(m_module_up)); 281 llvm::Triple triple(m_module->getTargetTriple()); 282 283 builder.setEngineKind(llvm::EngineKind::JIT) 284 .setErrorStr(&error_string) 285 .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_ 286 : llvm::Reloc::Static) 287 .setMCJITMemoryManager(std::make_unique<MemoryManager>(*this)) 288 .setOptLevel(llvm::CodeGenOpt::Less); 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.SetErrorToGenericError(); 304 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", 305 error_string.c_str()); 306 return; 307 } 308 309 m_strip_underscore = 310 (m_execution_engine_up->getDataLayout().getGlobalPrefix() == '_'); 311 312 class ObjectDumper : public llvm::ObjectCache { 313 public: 314 ObjectDumper(FileSpec output_dir) : m_out_dir(output_dir) {} 315 void notifyObjectCompiled(const llvm::Module *module, 316 llvm::MemoryBufferRef object) override { 317 int fd = 0; 318 llvm::SmallVector<char, 256> result_path; 319 std::string object_name_model = 320 "jit-object-" + module->getModuleIdentifier() + "-%%%.o"; 321 FileSpec model_spec 322 = m_out_dir.CopyByAppendingPathComponent(object_name_model); 323 std::string model_path = model_spec.GetPath(); 324 325 std::error_code result 326 = llvm::sys::fs::createUniqueFile(model_path, fd, result_path); 327 if (!result) { 328 llvm::raw_fd_ostream fds(fd, true); 329 fds.write(object.getBufferStart(), object.getBufferSize()); 330 } 331 } 332 std::unique_ptr<llvm::MemoryBuffer> 333 getObject(const llvm::Module *module) override { 334 // Return nothing - we're just abusing the object-cache mechanism to dump 335 // objects. 336 return nullptr; 337 } 338 private: 339 FileSpec m_out_dir; 340 }; 341 342 FileSpec save_objects_dir = process_sp->GetTarget().GetSaveJITObjectsDir(); 343 if (save_objects_dir) { 344 m_object_cache_up = std::make_unique<ObjectDumper>(save_objects_dir); 345 m_execution_engine_up->setObjectCache(m_object_cache_up.get()); 346 } 347 348 // Make sure we see all sections, including ones that don't have 349 // relocations... 350 m_execution_engine_up->setProcessAllSections(true); 351 352 m_execution_engine_up->DisableLazyCompilation(); 353 354 for (llvm::Function &function : *m_module) { 355 if (function.isDeclaration() || function.hasPrivateLinkage()) 356 continue; 357 358 const bool external = !function.hasLocalLinkage(); 359 360 void *fun_ptr = m_execution_engine_up->getPointerToFunction(&function); 361 362 if (!error.Success()) { 363 // We got an error through our callback! 364 return; 365 } 366 367 if (!fun_ptr) { 368 error.SetErrorToGenericError(); 369 error.SetErrorStringWithFormat( 370 "'%s' was in the JITted module but wasn't lowered", 371 function.getName().str().c_str()); 372 return; 373 } 374 m_jitted_functions.push_back(JittedFunction( 375 function.getName().str().c_str(), external, reinterpret_cast<uintptr_t>(fun_ptr))); 376 } 377 378 CommitAllocations(process_sp); 379 ReportAllocations(*m_execution_engine_up); 380 381 // We have to do this after calling ReportAllocations because for the MCJIT, 382 // getGlobalValueAddress will cause the JIT to perform all relocations. That 383 // can only be done once, and has to happen after we do the remapping from 384 // local -> remote. That means we don't know the local address of the 385 // Variables, but we don't need that for anything, so that's okay. 386 387 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this]( 388 llvm::GlobalValue &val) { 389 if (val.hasExternalLinkage() && !val.isDeclaration()) { 390 uint64_t var_ptr_addr = 391 m_execution_engine_up->getGlobalValueAddress(val.getName().str()); 392 393 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr); 394 395 // This is a really unfortunae API that sometimes returns local addresses 396 // and sometimes returns remote addresses, based on whether the variable 397 // was relocated during ReportAllocations or not. 398 399 if (remote_addr == LLDB_INVALID_ADDRESS) { 400 remote_addr = var_ptr_addr; 401 } 402 403 if (var_ptr_addr != 0) 404 m_jitted_global_variables.push_back(JittedGlobalVariable( 405 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr)); 406 } 407 }; 408 409 for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) { 410 RegisterOneValue(global_var); 411 } 412 413 for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) { 414 RegisterOneValue(global_alias); 415 } 416 417 WriteData(process_sp); 418 419 if (m_failed_lookups.size()) { 420 StreamString ss; 421 422 ss.PutCString("Couldn't lookup symbols:\n"); 423 424 bool emitNewLine = false; 425 426 for (ConstString failed_lookup : m_failed_lookups) { 427 if (emitNewLine) 428 ss.PutCString("\n"); 429 emitNewLine = true; 430 ss.PutCString(" "); 431 ss.PutCString(Mangled(failed_lookup).GetDemangledName().GetStringRef()); 432 } 433 434 m_failed_lookups.clear(); 435 436 error.SetErrorString(ss.GetString()); 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 503 IRExecutionUnit::~IRExecutionUnit() { 504 m_module_up.reset(); 505 m_execution_engine_up.reset(); 506 m_context_up.reset(); 507 } 508 509 IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent) 510 : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {} 511 512 IRExecutionUnit::MemoryManager::~MemoryManager() = default; 513 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.equals("__text") || name.equals(".text")) 537 sect_type = lldb::eSectionTypeCode; 538 else if (name.equals("__data") || name.equals(".data")) 539 sect_type = lldb::eSectionTypeCode; 540 else if (name.startswith("__debug_") || name.startswith(".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.equals("abbrev")) 546 sect_type = lldb::eSectionTypeDWARFDebugAbbrev; 547 else if (dwarf_name.equals("aranges")) 548 sect_type = lldb::eSectionTypeDWARFDebugAranges; 549 else if (dwarf_name.equals("addr")) 550 sect_type = lldb::eSectionTypeDWARFDebugAddr; 551 break; 552 553 case 'f': 554 if (dwarf_name.equals("frame")) 555 sect_type = lldb::eSectionTypeDWARFDebugFrame; 556 break; 557 558 case 'i': 559 if (dwarf_name.equals("info")) 560 sect_type = lldb::eSectionTypeDWARFDebugInfo; 561 break; 562 563 case 'l': 564 if (dwarf_name.equals("line")) 565 sect_type = lldb::eSectionTypeDWARFDebugLine; 566 else if (dwarf_name.equals("loc")) 567 sect_type = lldb::eSectionTypeDWARFDebugLoc; 568 else if (dwarf_name.equals("loclists")) 569 sect_type = lldb::eSectionTypeDWARFDebugLocLists; 570 break; 571 572 case 'm': 573 if (dwarf_name.equals("macinfo")) 574 sect_type = lldb::eSectionTypeDWARFDebugMacInfo; 575 break; 576 577 case 'p': 578 if (dwarf_name.equals("pubnames")) 579 sect_type = lldb::eSectionTypeDWARFDebugPubNames; 580 else if (dwarf_name.equals("pubtypes")) 581 sect_type = lldb::eSectionTypeDWARFDebugPubTypes; 582 break; 583 584 case 's': 585 if (dwarf_name.equals("str")) 586 sect_type = lldb::eSectionTypeDWARFDebugStr; 587 else if (dwarf_name.equals("str_offsets")) 588 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets; 589 break; 590 591 case 'r': 592 if (dwarf_name.equals("ranges")) 593 sect_type = lldb::eSectionTypeDWARFDebugRanges; 594 break; 595 596 default: 597 break; 598 } 599 } else if (name.startswith("__apple_") || name.startswith(".apple_")) 600 sect_type = lldb::eSectionTypeInvalid; 601 else if (name.equals("__objc_imageinfo")) 602 sect_type = lldb::eSectionTypeOther; 603 } 604 return sect_type; 605 } 606 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 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 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 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: 703 LoadAddressResolver(Target *target, bool &symbol_was_missing_weak) 704 : m_target(target), m_symbol_was_missing_weak(symbol_was_missing_weak) {} 705 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 = 737 candidate_sc.function->GetAddressRange().GetBaseAddress(); 738 load_address = m_target->GetProcessSP() ? addr.GetLoadAddress(m_target) 739 : addr.GetFileAddress(); 740 } 741 742 // We found a load address. 743 if (load_address != LLDB_INVALID_ADDRESS) { 744 // If the load address is external, we're done. 745 const bool is_external = 746 (candidate_sc.function) || 747 (candidate_sc.symbol && candidate_sc.symbol->IsExternal()); 748 if (is_external) 749 return load_address; 750 751 // Otherwise, remember the best internal load address. 752 if (m_best_internal_load_address == LLDB_INVALID_ADDRESS) 753 m_best_internal_load_address = load_address; 754 } 755 } 756 757 // You test the address of a weak symbol against NULL to see if it is 758 // present. So we should return 0 for a missing weak symbol. 759 if (m_symbol_was_missing_weak) 760 return 0; 761 762 return std::nullopt; 763 } 764 765 lldb::addr_t GetBestInternalLoadAddress() const { 766 return m_best_internal_load_address; 767 } 768 769 private: 770 Target *m_target; 771 bool &m_symbol_was_missing_weak; 772 lldb::addr_t m_best_internal_load_address = LLDB_INVALID_ADDRESS; 773 }; 774 775 lldb::addr_t 776 IRExecutionUnit::FindInSymbols(const std::vector<ConstString> &names, 777 const lldb_private::SymbolContext &sc, 778 bool &symbol_was_missing_weak) { 779 symbol_was_missing_weak = false; 780 781 Target *target = sc.target_sp.get(); 782 if (!target) { 783 // We shouldn't be doing any symbol lookup at all without a target. 784 return LLDB_INVALID_ADDRESS; 785 } 786 787 LoadAddressResolver resolver(target, symbol_was_missing_weak); 788 789 ModuleFunctionSearchOptions function_options; 790 function_options.include_symbols = true; 791 function_options.include_inlines = false; 792 793 for (const ConstString &name : names) { 794 if (sc.module_sp) { 795 SymbolContextList sc_list; 796 sc.module_sp->FindFunctions(name, CompilerDeclContext(), 797 lldb::eFunctionNameTypeFull, function_options, 798 sc_list); 799 if (auto load_addr = resolver.Resolve(sc_list)) 800 return *load_addr; 801 } 802 803 if (sc.target_sp) { 804 SymbolContextList sc_list; 805 sc.target_sp->GetImages().FindFunctions(name, lldb::eFunctionNameTypeFull, 806 function_options, sc_list); 807 if (auto load_addr = resolver.Resolve(sc_list)) 808 return *load_addr; 809 } 810 811 if (sc.target_sp) { 812 SymbolContextList sc_list; 813 sc.target_sp->GetImages().FindSymbolsWithNameAndType( 814 name, lldb::eSymbolTypeAny, sc_list); 815 if (auto load_addr = resolver.Resolve(sc_list)) 816 return *load_addr; 817 } 818 819 lldb::addr_t best_internal_load_address = 820 resolver.GetBestInternalLoadAddress(); 821 if (best_internal_load_address != LLDB_INVALID_ADDRESS) 822 return best_internal_load_address; 823 } 824 825 return LLDB_INVALID_ADDRESS; 826 } 827 828 lldb::addr_t 829 IRExecutionUnit::FindInRuntimes(const std::vector<ConstString> &names, 830 const lldb_private::SymbolContext &sc) { 831 lldb::TargetSP target_sp = sc.target_sp; 832 833 if (!target_sp) { 834 return LLDB_INVALID_ADDRESS; 835 } 836 837 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP(); 838 839 if (!process_sp) { 840 return LLDB_INVALID_ADDRESS; 841 } 842 843 for (const ConstString &name : names) { 844 for (LanguageRuntime *runtime : process_sp->GetLanguageRuntimes()) { 845 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(name); 846 847 if (symbol_load_addr != LLDB_INVALID_ADDRESS) 848 return symbol_load_addr; 849 } 850 } 851 852 return LLDB_INVALID_ADDRESS; 853 } 854 855 lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols( 856 const std::vector<ConstString> &names, 857 const lldb_private::SymbolContext &sc) { 858 lldb::TargetSP target_sp = sc.target_sp; 859 860 for (const ConstString &name : names) { 861 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(name); 862 863 if (symbol_load_addr != LLDB_INVALID_ADDRESS) 864 return symbol_load_addr; 865 } 866 867 return LLDB_INVALID_ADDRESS; 868 } 869 870 lldb::addr_t IRExecutionUnit::FindSymbol(lldb_private::ConstString name, 871 bool &missing_weak) { 872 std::vector<ConstString> candidate_C_names; 873 std::vector<ConstString> candidate_CPlusPlus_names; 874 875 CollectCandidateCNames(candidate_C_names, name); 876 877 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx, missing_weak); 878 if (ret != LLDB_INVALID_ADDRESS) 879 return ret; 880 881 // If we find the symbol in runtimes or user defined symbols it can't be 882 // a missing weak symbol. 883 missing_weak = false; 884 ret = FindInRuntimes(candidate_C_names, m_sym_ctx); 885 if (ret != LLDB_INVALID_ADDRESS) 886 return ret; 887 888 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx); 889 if (ret != LLDB_INVALID_ADDRESS) 890 return ret; 891 892 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names, 893 m_sym_ctx); 894 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx, missing_weak); 895 return ret; 896 } 897 898 void IRExecutionUnit::GetStaticInitializers( 899 std::vector<lldb::addr_t> &static_initializers) { 900 Log *log = GetLog(LLDBLog::Expressions); 901 902 llvm::GlobalVariable *global_ctors = 903 m_module->getNamedGlobal("llvm.global_ctors"); 904 if (!global_ctors) { 905 LLDB_LOG(log, "Couldn't find llvm.global_ctors."); 906 return; 907 } 908 auto *ctor_array = 909 llvm::dyn_cast<llvm::ConstantArray>(global_ctors->getInitializer()); 910 if (!ctor_array) { 911 LLDB_LOG(log, "llvm.global_ctors not a ConstantArray."); 912 return; 913 } 914 915 for (llvm::Use &ctor_use : ctor_array->operands()) { 916 auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(ctor_use); 917 if (!ctor_struct) 918 continue; 919 // this is standardized 920 lldbassert(ctor_struct->getNumOperands() == 3); 921 auto *ctor_function = 922 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1)); 923 if (!ctor_function) { 924 LLDB_LOG(log, "global_ctor doesn't contain an llvm::Function"); 925 continue; 926 } 927 928 ConstString ctor_function_name(ctor_function->getName().str()); 929 LLDB_LOG(log, "Looking for callable jitted function with name {0}.", 930 ctor_function_name); 931 932 for (JittedFunction &jitted_function : m_jitted_functions) { 933 if (ctor_function_name != jitted_function.m_name) 934 continue; 935 if (jitted_function.m_remote_addr == LLDB_INVALID_ADDRESS) { 936 LLDB_LOG(log, "Found jitted function with invalid address."); 937 continue; 938 } 939 static_initializers.push_back(jitted_function.m_remote_addr); 940 LLDB_LOG(log, "Calling function at address {0:x}.", 941 jitted_function.m_remote_addr); 942 break; 943 } 944 } 945 } 946 947 llvm::JITSymbol 948 IRExecutionUnit::MemoryManager::findSymbol(const std::string &Name) { 949 bool missing_weak = false; 950 uint64_t addr = GetSymbolAddressAndPresence(Name, missing_weak); 951 // This is a weak symbol: 952 if (missing_weak) 953 return llvm::JITSymbol(addr, 954 llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak); 955 else 956 return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported); 957 } 958 959 uint64_t 960 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) { 961 bool missing_weak = false; 962 return GetSymbolAddressAndPresence(Name, missing_weak); 963 } 964 965 uint64_t 966 IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence( 967 const std::string &Name, bool &missing_weak) { 968 Log *log = GetLog(LLDBLog::Expressions); 969 970 ConstString name_cs(Name.c_str()); 971 972 lldb::addr_t ret = m_parent.FindSymbol(name_cs, missing_weak); 973 974 if (ret == LLDB_INVALID_ADDRESS) { 975 LLDB_LOGF(log, 976 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>", 977 Name.c_str()); 978 979 m_parent.ReportSymbolLookupError(name_cs); 980 return 0; 981 } else { 982 LLDB_LOGF(log, "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64, 983 Name.c_str(), ret); 984 return ret; 985 } 986 } 987 988 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction( 989 const std::string &Name, bool AbortOnFailure) { 990 return (void *)getSymbolAddress(Name); 991 } 992 993 lldb::addr_t 994 IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) { 995 Log *log = GetLog(LLDBLog::Expressions); 996 997 for (AllocationRecord &record : m_records) { 998 if (local_address >= record.m_host_address && 999 local_address < record.m_host_address + record.m_size) { 1000 if (record.m_process_address == LLDB_INVALID_ADDRESS) 1001 return LLDB_INVALID_ADDRESS; 1002 1003 lldb::addr_t ret = 1004 record.m_process_address + (local_address - record.m_host_address); 1005 1006 LLDB_LOGF(log, 1007 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 1008 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 1009 " from [0x%" PRIx64 "..0x%" PRIx64 "].", 1010 local_address, (uint64_t)record.m_host_address, 1011 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret, 1012 record.m_process_address, 1013 record.m_process_address + record.m_size); 1014 1015 return ret; 1016 } 1017 } 1018 1019 return LLDB_INVALID_ADDRESS; 1020 } 1021 1022 IRExecutionUnit::AddrRange 1023 IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) { 1024 for (AllocationRecord &record : m_records) { 1025 if (local_address >= record.m_host_address && 1026 local_address < record.m_host_address + record.m_size) { 1027 if (record.m_process_address == LLDB_INVALID_ADDRESS) 1028 return AddrRange(0, 0); 1029 1030 return AddrRange(record.m_process_address, record.m_size); 1031 } 1032 } 1033 1034 return AddrRange(0, 0); 1035 } 1036 1037 bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp, 1038 Status &error, 1039 AllocationRecord &record) { 1040 if (record.m_process_address != LLDB_INVALID_ADDRESS) { 1041 return true; 1042 } 1043 1044 switch (record.m_sect_type) { 1045 case lldb::eSectionTypeInvalid: 1046 case lldb::eSectionTypeDWARFDebugAbbrev: 1047 case lldb::eSectionTypeDWARFDebugAddr: 1048 case lldb::eSectionTypeDWARFDebugAranges: 1049 case lldb::eSectionTypeDWARFDebugCuIndex: 1050 case lldb::eSectionTypeDWARFDebugFrame: 1051 case lldb::eSectionTypeDWARFDebugInfo: 1052 case lldb::eSectionTypeDWARFDebugLine: 1053 case lldb::eSectionTypeDWARFDebugLoc: 1054 case lldb::eSectionTypeDWARFDebugLocLists: 1055 case lldb::eSectionTypeDWARFDebugMacInfo: 1056 case lldb::eSectionTypeDWARFDebugPubNames: 1057 case lldb::eSectionTypeDWARFDebugPubTypes: 1058 case lldb::eSectionTypeDWARFDebugRanges: 1059 case lldb::eSectionTypeDWARFDebugStr: 1060 case lldb::eSectionTypeDWARFDebugStrOffsets: 1061 case lldb::eSectionTypeDWARFAppleNames: 1062 case lldb::eSectionTypeDWARFAppleTypes: 1063 case lldb::eSectionTypeDWARFAppleNamespaces: 1064 case lldb::eSectionTypeDWARFAppleObjC: 1065 case lldb::eSectionTypeDWARFGNUDebugAltLink: 1066 error.Clear(); 1067 break; 1068 default: 1069 const bool zero_memory = false; 1070 record.m_process_address = 1071 Malloc(record.m_size, record.m_alignment, record.m_permissions, 1072 eAllocationPolicyProcessOnly, zero_memory, error); 1073 break; 1074 } 1075 1076 return error.Success(); 1077 } 1078 1079 bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) { 1080 bool ret = true; 1081 1082 lldb_private::Status err; 1083 1084 for (AllocationRecord &record : m_records) { 1085 ret = CommitOneAllocation(process_sp, err, record); 1086 1087 if (!ret) { 1088 break; 1089 } 1090 } 1091 1092 if (!ret) { 1093 for (AllocationRecord &record : m_records) { 1094 if (record.m_process_address != LLDB_INVALID_ADDRESS) { 1095 Free(record.m_process_address, err); 1096 record.m_process_address = LLDB_INVALID_ADDRESS; 1097 } 1098 } 1099 } 1100 1101 return ret; 1102 } 1103 1104 void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) { 1105 m_reported_allocations = true; 1106 1107 for (AllocationRecord &record : m_records) { 1108 if (record.m_process_address == LLDB_INVALID_ADDRESS) 1109 continue; 1110 1111 if (record.m_section_id == eSectionIDInvalid) 1112 continue; 1113 1114 engine.mapSectionAddress((void *)record.m_host_address, 1115 record.m_process_address); 1116 } 1117 1118 // Trigger re-application of relocations. 1119 engine.finalizeObject(); 1120 } 1121 1122 bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) { 1123 bool wrote_something = false; 1124 for (AllocationRecord &record : m_records) { 1125 if (record.m_process_address != LLDB_INVALID_ADDRESS) { 1126 lldb_private::Status err; 1127 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address, 1128 record.m_size, err); 1129 if (err.Success()) 1130 wrote_something = true; 1131 } 1132 } 1133 return wrote_something; 1134 } 1135 1136 void IRExecutionUnit::AllocationRecord::dump(Log *log) { 1137 if (!log) 1138 return; 1139 1140 LLDB_LOGF(log, 1141 "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)", 1142 (unsigned long long)m_host_address, (unsigned long long)m_size, 1143 (unsigned long long)m_process_address, (unsigned)m_alignment, 1144 (unsigned)m_section_id, m_name.c_str()); 1145 } 1146 1147 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const { 1148 ExecutionContext exe_ctx(GetBestExecutionContextScope()); 1149 return exe_ctx.GetByteOrder(); 1150 } 1151 1152 uint32_t IRExecutionUnit::GetAddressByteSize() const { 1153 ExecutionContext exe_ctx(GetBestExecutionContextScope()); 1154 return exe_ctx.GetAddressByteSize(); 1155 } 1156 1157 void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file, 1158 lldb_private::Symtab &symtab) { 1159 // No symbols yet... 1160 } 1161 1162 void IRExecutionUnit::PopulateSectionList( 1163 lldb_private::ObjectFile *obj_file, 1164 lldb_private::SectionList §ion_list) { 1165 for (AllocationRecord &record : m_records) { 1166 if (record.m_size > 0) { 1167 lldb::SectionSP section_sp(new lldb_private::Section( 1168 obj_file->GetModule(), obj_file, record.m_section_id, 1169 ConstString(record.m_name), record.m_sect_type, 1170 record.m_process_address, record.m_size, 1171 record.m_host_address, // file_offset (which is the host address for 1172 // the data) 1173 record.m_size, // file_size 1174 0, 1175 record.m_permissions)); // flags 1176 section_list.AddSection(section_sp); 1177 } 1178 } 1179 } 1180 1181 ArchSpec IRExecutionUnit::GetArchitecture() { 1182 ExecutionContext exe_ctx(GetBestExecutionContextScope()); 1183 if(Target *target = exe_ctx.GetTargetPtr()) 1184 return target->GetArchitecture(); 1185 return ArchSpec(); 1186 } 1187 1188 lldb::ModuleSP IRExecutionUnit::GetJITModule() { 1189 ExecutionContext exe_ctx(GetBestExecutionContextScope()); 1190 Target *target = exe_ctx.GetTargetPtr(); 1191 if (!target) 1192 return nullptr; 1193 1194 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>( 1195 shared_from_this()); 1196 1197 lldb::ModuleSP jit_module_sp = 1198 lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate); 1199 if (!jit_module_sp) 1200 return nullptr; 1201 1202 bool changed = false; 1203 jit_module_sp->SetLoadAddress(*target, 0, true, changed); 1204 return jit_module_sp; 1205 } 1206