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