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::GetRunnableInfo(Error &error, 243 lldb::addr_t &func_addr, 244 lldb::addr_t &func_end) 245 { 246 lldb::ProcessSP process_sp(GetProcessWP().lock()); 247 248 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive); 249 250 func_addr = LLDB_INVALID_ADDRESS; 251 func_end = LLDB_INVALID_ADDRESS; 252 253 if (!process_sp) 254 { 255 error.SetErrorToGenericError(); 256 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid"); 257 return; 258 } 259 260 if (m_did_jit) 261 { 262 func_addr = m_function_load_addr; 263 func_end = m_function_end_load_addr; 264 265 return; 266 }; 267 268 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex); 269 270 m_did_jit = true; 271 272 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 273 274 std::string error_string; 275 276 if (log) 277 { 278 std::string s; 279 llvm::raw_string_ostream oss(s); 280 281 m_module->print(oss, NULL); 282 283 oss.flush(); 284 285 log->Printf ("Module being sent to JIT: \n%s", s.c_str()); 286 } 287 288 llvm::Triple triple(m_module->getTargetTriple()); 289 llvm::Function *function = m_module->getFunction (m_name.AsCString()); 290 llvm::Reloc::Model relocModel; 291 llvm::CodeModel::Model codeModel; 292 293 if (triple.isOSBinFormatELF()) 294 { 295 relocModel = llvm::Reloc::Static; 296 // This will be small for 32-bit and large for 64-bit. 297 codeModel = llvm::CodeModel::JITDefault; 298 } 299 else 300 { 301 relocModel = llvm::Reloc::PIC_; 302 codeModel = llvm::CodeModel::Small; 303 } 304 305 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error); 306 307 llvm::EngineBuilder builder(std::move(m_module_ap)); 308 309 builder.setEngineKind(llvm::EngineKind::JIT) 310 .setErrorStr(&error_string) 311 .setRelocationModel(relocModel) 312 .setMCJITMemoryManager(new MemoryManager(*this)) 313 .setCodeModel(codeModel) 314 .setOptLevel(llvm::CodeGenOpt::Less); 315 316 llvm::StringRef mArch; 317 llvm::StringRef mCPU; 318 llvm::SmallVector<std::string, 0> mAttrs; 319 320 for (std::string &feature : m_cpu_features) 321 mAttrs.push_back(feature); 322 323 llvm::TargetMachine *target_machine = builder.selectTarget(triple, 324 mArch, 325 mCPU, 326 mAttrs); 327 328 m_execution_engine_ap.reset(builder.create(target_machine)); 329 330 if (!m_execution_engine_ap.get()) 331 { 332 error.SetErrorToGenericError(); 333 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); 334 return; 335 } 336 337 // Make sure we see all sections, including ones that don't have relocations... 338 m_execution_engine_ap->setProcessAllSections(true); 339 340 m_execution_engine_ap->DisableLazyCompilation(); 341 342 // We don't actually need the function pointer here, this just forces it to get resolved. 343 344 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function); 345 346 if (!error.Success()) 347 { 348 // We got an error through our callback! 349 return; 350 } 351 352 if (!function) 353 { 354 error.SetErrorToGenericError(); 355 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString()); 356 return; 357 } 358 359 if (!fun_ptr) 360 { 361 error.SetErrorToGenericError(); 362 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString()); 363 return; 364 } 365 366 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr)); 367 368 CommitAllocations(process_sp); 369 ReportAllocations(*m_execution_engine_ap); 370 WriteData(process_sp); 371 372 for (JittedFunction &jitted_function : m_jitted_functions) 373 { 374 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr); 375 376 if (!jitted_function.m_name.compare(m_name.AsCString())) 377 { 378 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr); 379 m_function_end_load_addr = func_range.first + func_range.second; 380 m_function_load_addr = jitted_function.m_remote_addr; 381 } 382 } 383 384 if (log) 385 { 386 log->Printf("Code can be run in the target."); 387 388 StreamString disassembly_stream; 389 390 Error err = DisassembleFunction(disassembly_stream, process_sp); 391 392 if (!err.Success()) 393 { 394 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); 395 } 396 else 397 { 398 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); 399 } 400 401 log->Printf("Sections: "); 402 for (AllocationRecord &record : m_records) 403 { 404 if (record.m_process_address != LLDB_INVALID_ADDRESS) 405 { 406 record.dump(log); 407 408 DataBufferHeap my_buffer(record.m_size, 0); 409 Error err; 410 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err); 411 412 if (err.Success()) 413 { 414 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); 415 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8); 416 } 417 } 418 } 419 } 420 421 func_addr = m_function_load_addr; 422 func_end = m_function_end_load_addr; 423 424 return; 425 } 426 427 IRExecutionUnit::~IRExecutionUnit () 428 { 429 m_module_ap.reset(); 430 m_execution_engine_ap.reset(); 431 m_context_ap.reset(); 432 } 433 434 IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) : 435 m_default_mm_ap (new llvm::SectionMemoryManager()), 436 m_parent (parent) 437 { 438 } 439 440 IRExecutionUnit::MemoryManager::~MemoryManager () 441 { 442 } 443 444 lldb::SectionType 445 IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) 446 { 447 lldb::SectionType sect_type = lldb::eSectionTypeCode; 448 switch (alloc_kind) 449 { 450 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break; 451 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break; 452 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break; 453 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break; 454 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break; 455 } 456 457 if (!name.empty()) 458 { 459 if (name.equals("__text") || name.equals(".text")) 460 sect_type = lldb::eSectionTypeCode; 461 else if (name.equals("__data") || name.equals(".data")) 462 sect_type = lldb::eSectionTypeCode; 463 else if (name.startswith("__debug_") || name.startswith(".debug_")) 464 { 465 const uint32_t name_idx = name[0] == '_' ? 8 : 7; 466 llvm::StringRef dwarf_name(name.substr(name_idx)); 467 switch (dwarf_name[0]) 468 { 469 case 'a': 470 if (dwarf_name.equals("abbrev")) 471 sect_type = lldb::eSectionTypeDWARFDebugAbbrev; 472 else if (dwarf_name.equals("aranges")) 473 sect_type = lldb::eSectionTypeDWARFDebugAranges; 474 break; 475 476 case 'f': 477 if (dwarf_name.equals("frame")) 478 sect_type = lldb::eSectionTypeDWARFDebugFrame; 479 break; 480 481 case 'i': 482 if (dwarf_name.equals("info")) 483 sect_type = lldb::eSectionTypeDWARFDebugInfo; 484 break; 485 486 case 'l': 487 if (dwarf_name.equals("line")) 488 sect_type = lldb::eSectionTypeDWARFDebugLine; 489 else if (dwarf_name.equals("loc")) 490 sect_type = lldb::eSectionTypeDWARFDebugLoc; 491 break; 492 493 case 'm': 494 if (dwarf_name.equals("macinfo")) 495 sect_type = lldb::eSectionTypeDWARFDebugMacInfo; 496 break; 497 498 case 'p': 499 if (dwarf_name.equals("pubnames")) 500 sect_type = lldb::eSectionTypeDWARFDebugPubNames; 501 else if (dwarf_name.equals("pubtypes")) 502 sect_type = lldb::eSectionTypeDWARFDebugPubTypes; 503 break; 504 505 case 's': 506 if (dwarf_name.equals("str")) 507 sect_type = lldb::eSectionTypeDWARFDebugStr; 508 break; 509 510 case 'r': 511 if (dwarf_name.equals("ranges")) 512 sect_type = lldb::eSectionTypeDWARFDebugRanges; 513 break; 514 515 default: 516 break; 517 } 518 } 519 else if (name.startswith("__apple_") || name.startswith(".apple_")) 520 { 521 #if 0 522 const uint32_t name_idx = name[0] == '_' ? 8 : 7; 523 llvm::StringRef apple_name(name.substr(name_idx)); 524 switch (apple_name[0]) 525 { 526 case 'n': 527 if (apple_name.equals("names")) 528 sect_type = lldb::eSectionTypeDWARFAppleNames; 529 else if (apple_name.equals("namespac") || apple_name.equals("namespaces")) 530 sect_type = lldb::eSectionTypeDWARFAppleNamespaces; 531 break; 532 case 't': 533 if (apple_name.equals("types")) 534 sect_type = lldb::eSectionTypeDWARFAppleTypes; 535 break; 536 case 'o': 537 if (apple_name.equals("objc")) 538 sect_type = lldb::eSectionTypeDWARFAppleObjC; 539 break; 540 default: 541 break; 542 } 543 #else 544 sect_type = lldb::eSectionTypeInvalid; 545 #endif 546 } 547 else if (name.equals("__objc_imageinfo")) 548 sect_type = lldb::eSectionTypeOther; 549 } 550 return sect_type; 551 } 552 553 uint8_t * 554 IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size, 555 unsigned Alignment, 556 unsigned SectionID, 557 llvm::StringRef SectionName) 558 { 559 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 560 561 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName); 562 563 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, 564 lldb::ePermissionsReadable | lldb::ePermissionsExecutable, 565 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code), 566 Size, 567 Alignment, 568 SectionID, 569 SectionName.str().c_str())); 570 571 if (log) 572 { 573 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", 574 (uint64_t)Size, Alignment, SectionID, return_value); 575 } 576 577 return return_value; 578 } 579 580 uint8_t * 581 IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size, 582 unsigned Alignment, 583 unsigned SectionID, 584 llvm::StringRef SectionName, 585 bool IsReadOnly) 586 { 587 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 588 589 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly); 590 591 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, 592 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable), 593 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data), 594 Size, 595 Alignment, 596 SectionID, 597 SectionName.str().c_str())); 598 if (log) 599 { 600 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", 601 (uint64_t)Size, Alignment, SectionID, return_value); 602 } 603 604 return return_value; 605 } 606 607 lldb::addr_t 608 IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address) 609 { 610 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 611 612 for (AllocationRecord &record : m_records) 613 { 614 if (local_address >= record.m_host_address && 615 local_address < record.m_host_address + record.m_size) 616 { 617 if (record.m_process_address == LLDB_INVALID_ADDRESS) 618 return LLDB_INVALID_ADDRESS; 619 620 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address); 621 622 if (log) 623 { 624 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].", 625 local_address, 626 (uint64_t)record.m_host_address, 627 (uint64_t)record.m_host_address + (uint64_t)record.m_size, 628 ret, 629 record.m_process_address, 630 record.m_process_address + record.m_size); 631 } 632 633 return ret; 634 } 635 } 636 637 return LLDB_INVALID_ADDRESS; 638 } 639 640 IRExecutionUnit::AddrRange 641 IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address) 642 { 643 for (AllocationRecord &record : m_records) 644 { 645 if (local_address >= record.m_host_address && 646 local_address < record.m_host_address + record.m_size) 647 { 648 if (record.m_process_address == LLDB_INVALID_ADDRESS) 649 return AddrRange(0, 0); 650 651 return AddrRange(record.m_process_address, record.m_size); 652 } 653 } 654 655 return AddrRange (0, 0); 656 } 657 658 bool 659 IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp) 660 { 661 bool ret = true; 662 663 lldb_private::Error err; 664 665 for (AllocationRecord &record : m_records) 666 { 667 if (record.m_process_address != LLDB_INVALID_ADDRESS) 668 continue; 669 670 switch (record.m_sect_type) 671 { 672 case lldb::eSectionTypeInvalid: 673 case lldb::eSectionTypeDWARFDebugAbbrev: 674 case lldb::eSectionTypeDWARFDebugAranges: 675 case lldb::eSectionTypeDWARFDebugFrame: 676 case lldb::eSectionTypeDWARFDebugInfo: 677 case lldb::eSectionTypeDWARFDebugLine: 678 case lldb::eSectionTypeDWARFDebugLoc: 679 case lldb::eSectionTypeDWARFDebugMacInfo: 680 case lldb::eSectionTypeDWARFDebugPubNames: 681 case lldb::eSectionTypeDWARFDebugPubTypes: 682 case lldb::eSectionTypeDWARFDebugRanges: 683 case lldb::eSectionTypeDWARFDebugStr: 684 case lldb::eSectionTypeDWARFAppleNames: 685 case lldb::eSectionTypeDWARFAppleTypes: 686 case lldb::eSectionTypeDWARFAppleNamespaces: 687 case lldb::eSectionTypeDWARFAppleObjC: 688 err.Clear(); 689 break; 690 default: 691 record.m_process_address = Malloc (record.m_size, 692 record.m_alignment, 693 record.m_permissions, 694 eAllocationPolicyProcessOnly, 695 err); 696 break; 697 } 698 699 if (!err.Success()) 700 { 701 ret = false; 702 break; 703 } 704 } 705 706 if (!ret) 707 { 708 for (AllocationRecord &record : m_records) 709 { 710 if (record.m_process_address != LLDB_INVALID_ADDRESS) 711 { 712 Free(record.m_process_address, err); 713 record.m_process_address = LLDB_INVALID_ADDRESS; 714 } 715 } 716 } 717 718 return ret; 719 } 720 721 void 722 IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine) 723 { 724 for (AllocationRecord &record : m_records) 725 { 726 if (record.m_process_address == LLDB_INVALID_ADDRESS) 727 continue; 728 729 if (record.m_section_id == eSectionIDInvalid) 730 continue; 731 732 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address); 733 } 734 735 // Trigger re-application of relocations. 736 engine.finalizeObject(); 737 } 738 739 bool 740 IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp) 741 { 742 bool wrote_something = false; 743 for (AllocationRecord &record : m_records) 744 { 745 if (record.m_process_address != LLDB_INVALID_ADDRESS) 746 { 747 lldb_private::Error err; 748 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err); 749 if (err.Success()) 750 wrote_something = true; 751 } 752 } 753 return wrote_something; 754 } 755 756 void 757 IRExecutionUnit::AllocationRecord::dump (Log *log) 758 { 759 if (!log) 760 return; 761 762 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)", 763 (unsigned long long)m_host_address, 764 (unsigned long long)m_size, 765 (unsigned long long)m_process_address, 766 (unsigned)m_alignment, 767 (unsigned)m_section_id); 768 } 769 770 771 lldb::ByteOrder 772 IRExecutionUnit::GetByteOrder () const 773 { 774 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 775 return exe_ctx.GetByteOrder(); 776 } 777 778 uint32_t 779 IRExecutionUnit::GetAddressByteSize () const 780 { 781 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 782 return exe_ctx.GetAddressByteSize(); 783 } 784 785 void 786 IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file, 787 lldb_private::Symtab &symtab) 788 { 789 // No symbols yet... 790 } 791 792 793 void 794 IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file, 795 lldb_private::SectionList §ion_list) 796 { 797 for (AllocationRecord &record : m_records) 798 { 799 if (record.m_size > 0) 800 { 801 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(), 802 obj_file, 803 record.m_section_id, 804 ConstString(record.m_name), 805 record.m_sect_type, 806 record.m_process_address, 807 record.m_size, 808 record.m_host_address, // file_offset (which is the host address for the data) 809 record.m_size, // file_size 810 0, 811 record.m_permissions)); // flags 812 section_list.AddSection (section_sp); 813 } 814 } 815 } 816 817 bool 818 IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch) 819 { 820 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 821 Target *target = exe_ctx.GetTargetPtr(); 822 if (target) 823 arch = target->GetArchitecture(); 824 else 825 arch.Clear(); 826 return arch.IsValid(); 827 } 828 829 lldb::ModuleSP 830 IRExecutionUnit::GetJITModule () 831 { 832 ExecutionContext exe_ctx (GetBestExecutionContextScope()); 833 Target *target = exe_ctx.GetTargetPtr(); 834 if (target) 835 { 836 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this())); 837 if (jit_module_sp) 838 { 839 bool changed = false; 840 jit_module_sp->SetLoadAddress(*target, 0, true, changed); 841 } 842 return jit_module_sp; 843 } 844 return lldb::ModuleSP(); 845 } 846