1 //===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ExecutionEngine/ExecutionEngine.h" 11 #include "llvm/IR/LLVMContext.h" 12 #include "llvm/IR/Module.h" 13 #include "llvm/Support/SourceMgr.h" 14 #include "llvm/Support/raw_ostream.h" 15 16 #include "lldb/Core/DataBufferHeap.h" 17 #include "lldb/Core/DataExtractor.h" 18 #include "lldb/Core/Debugger.h" 19 #include "lldb/Core/Disassembler.h" 20 #include "lldb/Core/Log.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Symbol/SymbolContext.h" 24 #include "lldb/Expression/IRExecutionUnit.h" 25 #include "lldb/Target/ExecutionContext.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Target/ObjCLanguageRuntime.h" 28 29 using namespace lldb_private; 30 31 IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap, 32 std::unique_ptr<llvm::Module> &module_ap, 33 ConstString &name, 34 const lldb::TargetSP &target_sp, 35 std::vector<std::string> &cpu_features) : 36 IRMemoryMap(target_sp), 37 m_context_ap(context_ap.release()), 38 m_module_ap(module_ap.release()), 39 m_module(m_module_ap.get()), 40 m_cpu_features(cpu_features), 41 m_name(name), 42 m_did_jit(false), 43 m_function_load_addr(LLDB_INVALID_ADDRESS), 44 m_function_end_load_addr(LLDB_INVALID_ADDRESS) 45 { 46 } 47 48 lldb::addr_t 49 IRExecutionUnit::WriteNow (const uint8_t *bytes, 50 size_t size, 51 Error &error) 52 { 53 const bool zero_memory = false; 54 lldb::addr_t allocation_process_addr = Malloc (size, 55 8, 56 lldb::ePermissionsWritable | lldb::ePermissionsReadable, 57 eAllocationPolicyMirror, 58 zero_memory, 59 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 { 68 Error err; 69 Free (allocation_process_addr, err); 70 71 return LLDB_INVALID_ADDRESS; 72 } 73 74 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) 75 { 76 DataBufferHeap my_buffer(size, 0); 77 Error err; 78 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err); 79 80 if (err.Success()) 81 { 82 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); 83 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8); 84 } 85 } 86 87 return allocation_process_addr; 88 } 89 90 void 91 IRExecutionUnit::FreeNow (lldb::addr_t allocation) 92 { 93 if (allocation == LLDB_INVALID_ADDRESS) 94 return; 95 96 Error err; 97 98 Free(allocation, err); 99 } 100 101 Error 102 IRExecutionUnit::DisassembleFunction (Stream &stream, 103 lldb::ProcessSP &process_wp) 104 { 105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 106 107 ExecutionContext exe_ctx(process_wp); 108 109 Error ret; 110 111 ret.Clear(); 112 113 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; 114 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; 115 116 for (JittedFunction &function : m_jitted_functions) 117 { 118 if (strstr(function.m_name.c_str(), m_name.AsCString())) 119 { 120 func_local_addr = function.m_local_addr; 121 func_remote_addr = function.m_remote_addr; 122 } 123 } 124 125 if (func_local_addr == LLDB_INVALID_ADDRESS) 126 { 127 ret.SetErrorToGenericError(); 128 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString()); 129 return ret; 130 } 131 132 if (log) 133 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr); 134 135 std::pair <lldb::addr_t, lldb::addr_t> func_range; 136 137 func_range = GetRemoteRangeForLocal(func_local_addr); 138 139 if (func_range.first == 0 && func_range.second == 0) 140 { 141 ret.SetErrorToGenericError(); 142 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString()); 143 return ret; 144 } 145 146 if (log) 147 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second); 148 149 Target *target = exe_ctx.GetTargetPtr(); 150 if (!target) 151 { 152 ret.SetErrorToGenericError(); 153 ret.SetErrorString("Couldn't find the target"); 154 return ret; 155 } 156 157 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0)); 158 159 Process *process = exe_ctx.GetProcessPtr(); 160 Error err; 161 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); 162 163 if (!err.Success()) 164 { 165 ret.SetErrorToGenericError(); 166 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); 167 return ret; 168 } 169 170 ArchSpec arch(target->GetArchitecture()); 171 172 const char *plugin_name = NULL; 173 const char *flavor_string = NULL; 174 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name); 175 176 if (!disassembler_sp) 177 { 178 ret.SetErrorToGenericError(); 179 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName()); 180 return ret; 181 } 182 183 if (!process) 184 { 185 ret.SetErrorToGenericError(); 186 ret.SetErrorString("Couldn't find the process"); 187 return ret; 188 } 189 190 DataExtractor extractor(buffer_sp, 191 process->GetByteOrder(), 192 target->GetArchitecture().GetAddressByteSize()); 193 194 if (log) 195 { 196 log->Printf("Function data has contents:"); 197 extractor.PutToLog (log, 198 0, 199 extractor.GetByteSize(), 200 func_remote_addr, 201 16, 202 DataExtractor::TypeUInt8); 203 } 204 205 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false); 206 207 InstructionList &instruction_list = disassembler_sp->GetInstructionList(); 208 instruction_list.Dump(&stream, true, true, &exe_ctx); 209 210 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions. 211 // I'll fix that but for now, just clear the list and it will go away nicely. 212 disassembler_sp->GetInstructionList().Clear(); 213 return ret; 214 } 215 216 static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie) 217 { 218 Error *err = static_cast<Error*>(Context); 219 220 if (err && err->Success()) 221 { 222 err->SetErrorToGenericError(); 223 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str()); 224 } 225 } 226 227 void 228 IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) 229 { 230 m_failed_lookups.push_back(name); 231 } 232 233 void 234 IRExecutionUnit::GetRunnableInfo(Error &error, 235 lldb::addr_t &func_addr, 236 lldb::addr_t &func_end) 237 { 238 lldb::ProcessSP process_sp(GetProcessWP().lock()); 239 240 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive); 241 242 func_addr = LLDB_INVALID_ADDRESS; 243 func_end = LLDB_INVALID_ADDRESS; 244 245 if (!process_sp) 246 { 247 error.SetErrorToGenericError(); 248 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid"); 249 return; 250 } 251 252 if (m_did_jit) 253 { 254 func_addr = m_function_load_addr; 255 func_end = m_function_end_load_addr; 256 257 return; 258 }; 259 260 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex); 261 262 m_did_jit = true; 263 264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 265 266 std::string error_string; 267 268 if (log) 269 { 270 std::string s; 271 llvm::raw_string_ostream oss(s); 272 273 m_module->print(oss, NULL); 274 275 oss.flush(); 276 277 log->Printf ("Module being sent to JIT: \n%s", s.c_str()); 278 } 279 280 llvm::Triple triple(m_module->getTargetTriple()); 281 llvm::Function *function = m_module->getFunction (m_name.AsCString()); 282 llvm::Reloc::Model relocModel; 283 llvm::CodeModel::Model codeModel; 284 285 if (triple.isOSBinFormatELF()) 286 { 287 relocModel = llvm::Reloc::Static; 288 // This will be small for 32-bit and large for 64-bit. 289 codeModel = llvm::CodeModel::JITDefault; 290 } 291 else 292 { 293 relocModel = llvm::Reloc::PIC_; 294 codeModel = llvm::CodeModel::Small; 295 } 296 297 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error); 298 299 llvm::EngineBuilder builder(std::move(m_module_ap)); 300 301 builder.setEngineKind(llvm::EngineKind::JIT) 302 .setErrorStr(&error_string) 303 .setRelocationModel(relocModel) 304 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this))) 305 .setCodeModel(codeModel) 306 .setOptLevel(llvm::CodeGenOpt::Less); 307 308 llvm::StringRef mArch; 309 llvm::StringRef mCPU; 310 llvm::SmallVector<std::string, 0> mAttrs; 311 312 for (std::string &feature : m_cpu_features) 313 mAttrs.push_back(feature); 314 315 llvm::TargetMachine *target_machine = builder.selectTarget(triple, 316 mArch, 317 mCPU, 318 mAttrs); 319 320 m_execution_engine_ap.reset(builder.create(target_machine)); 321 322 if (!m_execution_engine_ap.get()) 323 { 324 error.SetErrorToGenericError(); 325 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); 326 return; 327 } 328 329 // Make sure we see all sections, including ones that don't have relocations... 330 m_execution_engine_ap->setProcessAllSections(true); 331 332 m_execution_engine_ap->DisableLazyCompilation(); 333 334 // We don't actually need the function pointer here, this just forces it to get resolved. 335 336 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function); 337 338 if (!error.Success()) 339 { 340 // We got an error through our callback! 341 return; 342 } 343 344 if (!function) 345 { 346 error.SetErrorToGenericError(); 347 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString()); 348 return; 349 } 350 351 if (!fun_ptr) 352 { 353 error.SetErrorToGenericError(); 354 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString()); 355 return; 356 } 357 358 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr)); 359 360 CommitAllocations(process_sp); 361 ReportAllocations(*m_execution_engine_ap); 362 WriteData(process_sp); 363 364 if (m_failed_lookups.size()) 365 { 366 StreamString ss; 367 368 ss.PutCString("Couldn't lookup symbols:\n"); 369 370 bool emitNewLine = false; 371 372 for (const ConstString &failed_lookup : m_failed_lookups) 373 { 374 if (emitNewLine) 375 ss.PutCString("\n"); 376 emitNewLine = true; 377 ss.PutCString(" "); 378 ss.PutCString(Mangled(failed_lookup).GetDemangledName(lldb::eLanguageTypeObjC_plus_plus).AsCString()); 379 } 380 381 m_failed_lookups.clear(); 382 383 error.SetErrorString(ss.GetData()); 384 385 return; 386 } 387 388 m_function_load_addr = LLDB_INVALID_ADDRESS; 389 m_function_end_load_addr = LLDB_INVALID_ADDRESS; 390 391 for (JittedFunction &jitted_function : m_jitted_functions) 392 { 393 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr); 394 395 if (!jitted_function.m_name.compare(m_name.AsCString())) 396 { 397 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr); 398 m_function_end_load_addr = func_range.first + func_range.second; 399 m_function_load_addr = jitted_function.m_remote_addr; 400 } 401 } 402 403 if (log) 404 { 405 log->Printf("Code can be run in the target."); 406 407 StreamString disassembly_stream; 408 409 Error err = DisassembleFunction(disassembly_stream, process_sp); 410 411 if (!err.Success()) 412 { 413 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); 414 } 415 else 416 { 417 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); 418 } 419 420 log->Printf("Sections: "); 421 for (AllocationRecord &record : m_records) 422 { 423 if (record.m_process_address != LLDB_INVALID_ADDRESS) 424 { 425 record.dump(log); 426 427 DataBufferHeap my_buffer(record.m_size, 0); 428 Error err; 429 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err); 430 431 if (err.Success()) 432 { 433 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); 434 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8); 435 } 436 } 437 else 438 { 439 record.dump(log); 440 441 DataExtractor my_extractor ((const void*)record.m_host_address, record.m_size, lldb::eByteOrderBig, 8); 442 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16, DataExtractor::TypeUInt8); 443 } 444 } 445 } 446 447 func_addr = m_function_load_addr; 448 func_end = m_function_end_load_addr; 449 450 return; 451 } 452 453 IRExecutionUnit::~IRExecutionUnit () 454 { 455 m_module_ap.reset(); 456 m_execution_engine_ap.reset(); 457 m_context_ap.reset(); 458 } 459 460 IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) : 461 m_default_mm_ap (new llvm::SectionMemoryManager()), 462 m_parent (parent) 463 { 464 } 465 466 IRExecutionUnit::MemoryManager::~MemoryManager () 467 { 468 } 469 470 lldb::SectionType 471 IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) 472 { 473 lldb::SectionType sect_type = lldb::eSectionTypeCode; 474 switch (alloc_kind) 475 { 476 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break; 477 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break; 478 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break; 479 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break; 480 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break; 481 } 482 483 if (!name.empty()) 484 { 485 if (name.equals("__text") || name.equals(".text")) 486 sect_type = lldb::eSectionTypeCode; 487 else if (name.equals("__data") || name.equals(".data")) 488 sect_type = lldb::eSectionTypeCode; 489 else if (name.startswith("__debug_") || name.startswith(".debug_")) 490 { 491 const uint32_t name_idx = name[0] == '_' ? 8 : 7; 492 llvm::StringRef dwarf_name(name.substr(name_idx)); 493 switch (dwarf_name[0]) 494 { 495 case 'a': 496 if (dwarf_name.equals("abbrev")) 497 sect_type = lldb::eSectionTypeDWARFDebugAbbrev; 498 else if (dwarf_name.equals("aranges")) 499 sect_type = lldb::eSectionTypeDWARFDebugAranges; 500 else if (dwarf_name.equals("addr")) 501 sect_type = lldb::eSectionTypeDWARFDebugAddr; 502 break; 503 504 case 'f': 505 if (dwarf_name.equals("frame")) 506 sect_type = lldb::eSectionTypeDWARFDebugFrame; 507 break; 508 509 case 'i': 510 if (dwarf_name.equals("info")) 511 sect_type = lldb::eSectionTypeDWARFDebugInfo; 512 break; 513 514 case 'l': 515 if (dwarf_name.equals("line")) 516 sect_type = lldb::eSectionTypeDWARFDebugLine; 517 else if (dwarf_name.equals("loc")) 518 sect_type = lldb::eSectionTypeDWARFDebugLoc; 519 break; 520 521 case 'm': 522 if (dwarf_name.equals("macinfo")) 523 sect_type = lldb::eSectionTypeDWARFDebugMacInfo; 524 break; 525 526 case 'p': 527 if (dwarf_name.equals("pubnames")) 528 sect_type = lldb::eSectionTypeDWARFDebugPubNames; 529 else if (dwarf_name.equals("pubtypes")) 530 sect_type = lldb::eSectionTypeDWARFDebugPubTypes; 531 break; 532 533 case 's': 534 if (dwarf_name.equals("str")) 535 sect_type = lldb::eSectionTypeDWARFDebugStr; 536 else if (dwarf_name.equals("str_offsets")) 537 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets; 538 break; 539 540 case 'r': 541 if (dwarf_name.equals("ranges")) 542 sect_type = lldb::eSectionTypeDWARFDebugRanges; 543 break; 544 545 default: 546 break; 547 } 548 } 549 else if (name.startswith("__apple_") || name.startswith(".apple_")) 550 { 551 #if 0 552 const uint32_t name_idx = name[0] == '_' ? 8 : 7; 553 llvm::StringRef apple_name(name.substr(name_idx)); 554 switch (apple_name[0]) 555 { 556 case 'n': 557 if (apple_name.equals("names")) 558 sect_type = lldb::eSectionTypeDWARFAppleNames; 559 else if (apple_name.equals("namespac") || apple_name.equals("namespaces")) 560 sect_type = lldb::eSectionTypeDWARFAppleNamespaces; 561 break; 562 case 't': 563 if (apple_name.equals("types")) 564 sect_type = lldb::eSectionTypeDWARFAppleTypes; 565 break; 566 case 'o': 567 if (apple_name.equals("objc")) 568 sect_type = lldb::eSectionTypeDWARFAppleObjC; 569 break; 570 default: 571 break; 572 } 573 #else 574 sect_type = lldb::eSectionTypeInvalid; 575 #endif 576 } 577 else if (name.equals("__objc_imageinfo")) 578 sect_type = lldb::eSectionTypeOther; 579 } 580 return sect_type; 581 } 582 583 uint8_t * 584 IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size, 585 unsigned Alignment, 586 unsigned SectionID, 587 llvm::StringRef SectionName) 588 { 589 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 590 591 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName); 592 593 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, 594 lldb::ePermissionsReadable | lldb::ePermissionsExecutable, 595 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code), 596 Size, 597 Alignment, 598 SectionID, 599 SectionName.str().c_str())); 600 601 if (log) 602 { 603 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", 604 (uint64_t)Size, Alignment, SectionID, (void *)return_value); 605 } 606 607 return return_value; 608 } 609 610 uint8_t * 611 IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size, 612 unsigned Alignment, 613 unsigned SectionID, 614 llvm::StringRef SectionName, 615 bool IsReadOnly) 616 { 617 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 618 619 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly); 620 621 uint32_t permissions = lldb::ePermissionsReadable; 622 if (!IsReadOnly) 623 permissions |= lldb::ePermissionsWritable; 624 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, 625 permissions, 626 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data), 627 Size, 628 Alignment, 629 SectionID, 630 SectionName.str().c_str())); 631 if (log) 632 { 633 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", 634 (uint64_t)Size, Alignment, SectionID, (void *)return_value); 635 } 636 637 return return_value; 638 } 639 640 uint64_t 641 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) 642 { 643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 644 645 SymbolContextList sc_list; 646 647 ExecutionContextScope *exe_scope = m_parent.GetBestExecutionContextScope(); 648 649 lldb::TargetSP target_sp = exe_scope->CalculateTarget(); 650 651 const char *name = Name.c_str(); 652 653 ConstString bare_name_cs(name); 654 ConstString name_cs; 655 656 if (name[0] == '_') 657 name_cs = ConstString(name + 1); 658 659 if (!target_sp) 660 { 661 if (log) 662 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <no target>", 663 Name.c_str()); 664 665 m_parent.ReportSymbolLookupError(name_cs); 666 667 return 0xbad0bad0; 668 } 669 670 uint32_t num_matches = 0; 671 lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); 672 673 if (!name_cs.IsEmpty()) 674 { 675 target_sp->GetImages().FindSymbolsWithNameAndType(name_cs, lldb::eSymbolTypeAny, sc_list); 676 num_matches = sc_list.GetSize(); 677 } 678 679 if (!num_matches) 680 { 681 target_sp->GetImages().FindSymbolsWithNameAndType(bare_name_cs, lldb::eSymbolTypeAny, sc_list); 682 num_matches = sc_list.GetSize(); 683 } 684 685 lldb::addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; 686 687 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++) 688 { 689 SymbolContext sym_ctx; 690 sc_list.GetContextAtIndex(i, sym_ctx); 691 692 symbol_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target_sp); 693 694 if (symbol_load_addr == LLDB_INVALID_ADDRESS) 695 symbol_load_addr = sym_ctx.symbol->GetAddress().GetLoadAddress(target_sp.get()); 696 } 697 698 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process_sp && name_cs) 699 { 700 // Try the Objective-C language runtime. 701 702 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime(); 703 704 if (runtime) 705 symbol_load_addr = runtime->LookupRuntimeSymbol(name_cs); 706 } 707 708 if (symbol_load_addr == LLDB_INVALID_ADDRESS) 709 { 710 if (log) 711 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>", 712 name); 713 714 m_parent.ReportSymbolLookupError(bare_name_cs); 715 716 return 0xbad0bad0; 717 } 718 719 if (log) 720 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64, 721 name, 722 symbol_load_addr); 723 724 if (symbol_load_addr == 0) 725 return 0xbad00add; 726 727 return symbol_load_addr; 728 } 729 730 void * 731 IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name, 732 bool AbortOnFailure) { 733 assert (sizeof(void *) == 8); 734 735 return (void*)getSymbolAddress(Name); 736 } 737 738 lldb::addr_t 739 IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address) 740 { 741 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 742 743 for (AllocationRecord &record : m_records) 744 { 745 if (local_address >= record.m_host_address && 746 local_address < record.m_host_address + record.m_size) 747 { 748 if (record.m_process_address == LLDB_INVALID_ADDRESS) 749 return LLDB_INVALID_ADDRESS; 750 751 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address); 752 753 if (log) 754 { 755 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].", 756 local_address, 757 (uint64_t)record.m_host_address, 758 (uint64_t)record.m_host_address + (uint64_t)record.m_size, 759 ret, 760 record.m_process_address, 761 record.m_process_address + record.m_size); 762 } 763 764 return ret; 765 } 766 } 767 768 return LLDB_INVALID_ADDRESS; 769 } 770 771 IRExecutionUnit::AddrRange 772 IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address) 773 { 774 for (AllocationRecord &record : m_records) 775 { 776 if (local_address >= record.m_host_address && 777 local_address < record.m_host_address + record.m_size) 778 { 779 if (record.m_process_address == LLDB_INVALID_ADDRESS) 780 return AddrRange(0, 0); 781 782 return AddrRange(record.m_process_address, record.m_size); 783 } 784 } 785 786 return AddrRange (0, 0); 787 } 788 789 bool 790 IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp) 791 { 792 bool ret = true; 793 794 lldb_private::Error err; 795 796 for (AllocationRecord &record : m_records) 797 { 798 if (record.m_process_address != LLDB_INVALID_ADDRESS) 799 continue; 800 801 switch (record.m_sect_type) 802 { 803 case lldb::eSectionTypeInvalid: 804 case lldb::eSectionTypeDWARFDebugAbbrev: 805 case lldb::eSectionTypeDWARFDebugAddr: 806 case lldb::eSectionTypeDWARFDebugAranges: 807 case lldb::eSectionTypeDWARFDebugFrame: 808 case lldb::eSectionTypeDWARFDebugInfo: 809 case lldb::eSectionTypeDWARFDebugLine: 810 case lldb::eSectionTypeDWARFDebugLoc: 811 case lldb::eSectionTypeDWARFDebugMacInfo: 812 case lldb::eSectionTypeDWARFDebugPubNames: 813 case lldb::eSectionTypeDWARFDebugPubTypes: 814 case lldb::eSectionTypeDWARFDebugRanges: 815 case lldb::eSectionTypeDWARFDebugStr: 816 case lldb::eSectionTypeDWARFDebugStrOffsets: 817 case lldb::eSectionTypeDWARFAppleNames: 818 case lldb::eSectionTypeDWARFAppleTypes: 819 case lldb::eSectionTypeDWARFAppleNamespaces: 820 case lldb::eSectionTypeDWARFAppleObjC: 821 err.Clear(); 822 break; 823 default: 824 const bool zero_memory = false; 825 record.m_process_address = Malloc (record.m_size, 826 record.m_alignment, 827 record.m_permissions, 828 eAllocationPolicyProcessOnly, 829 zero_memory, 830 err); 831 break; 832 } 833 834 if (!err.Success()) 835 { 836 ret = false; 837 break; 838 } 839 } 840 841 if (!ret) 842 { 843 for (AllocationRecord &record : m_records) 844 { 845 if (record.m_process_address != LLDB_INVALID_ADDRESS) 846 { 847 Free(record.m_process_address, err); 848 record.m_process_address = LLDB_INVALID_ADDRESS; 849 } 850 } 851 } 852 853 return ret; 854 } 855 856 void 857 IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine) 858 { 859 for (AllocationRecord &record : m_records) 860 { 861 if (record.m_process_address == LLDB_INVALID_ADDRESS) 862 continue; 863 864 if (record.m_section_id == eSectionIDInvalid) 865 continue; 866 867 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address); 868 } 869 870 // Trigger re-application of relocations. 871 engine.finalizeObject(); 872 } 873 874 bool 875 IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp) 876 { 877 bool wrote_something = false; 878 for (AllocationRecord &record : m_records) 879 { 880 if (record.m_process_address != LLDB_INVALID_ADDRESS) 881 { 882 lldb_private::Error err; 883 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err); 884 if (err.Success()) 885 wrote_something = true; 886 } 887 } 888 return wrote_something; 889 } 890 891 void 892 IRExecutionUnit::AllocationRecord::dump (Log *log) 893 { 894 if (!log) 895 return; 896 897 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)", 898 (unsigned long long)m_host_address, 899 (unsigned long long)m_size, 900 (unsigned long long)m_process_address, 901 (unsigned)m_alignment, 902 (unsigned)m_section_id, 903 m_name.c_str()); 904 } 905 906 907 lldb::ByteOrder 908 IRExecutionUnit::GetByteOrder () const 909 { 910 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 911 return exe_ctx.GetByteOrder(); 912 } 913 914 uint32_t 915 IRExecutionUnit::GetAddressByteSize () const 916 { 917 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 918 return exe_ctx.GetAddressByteSize(); 919 } 920 921 void 922 IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file, 923 lldb_private::Symtab &symtab) 924 { 925 // No symbols yet... 926 } 927 928 929 void 930 IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file, 931 lldb_private::SectionList §ion_list) 932 { 933 for (AllocationRecord &record : m_records) 934 { 935 if (record.m_size > 0) 936 { 937 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(), 938 obj_file, 939 record.m_section_id, 940 ConstString(record.m_name), 941 record.m_sect_type, 942 record.m_process_address, 943 record.m_size, 944 record.m_host_address, // file_offset (which is the host address for the data) 945 record.m_size, // file_size 946 0, 947 record.m_permissions)); // flags 948 section_list.AddSection (section_sp); 949 } 950 } 951 } 952 953 bool 954 IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch) 955 { 956 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 957 Target *target = exe_ctx.GetTargetPtr(); 958 if (target) 959 arch = target->GetArchitecture(); 960 else 961 arch.Clear(); 962 return arch.IsValid(); 963 } 964 965 lldb::ModuleSP 966 IRExecutionUnit::GetJITModule () 967 { 968 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 969 Target *target = exe_ctx.GetTargetPtr(); 970 if (target) 971 { 972 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this())); 973 if (jit_module_sp) 974 { 975 bool changed = false; 976 jit_module_sp->SetLoadAddress(*target, 0, true, changed); 977 } 978 return jit_module_sp; 979 } 980 return lldb::ModuleSP(); 981 } 982