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