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