Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===// |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 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 | // Implementation of the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "dyld" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 15 | #include "RuntimeDyldImpl.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame^] | 16 | #include "llvm/Support/Path.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | using namespace llvm::object; |
| 19 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 20 | // Empty out-of-line virtual destructor as the key function. |
| 21 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 22 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 23 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 24 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 25 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 26 | void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, |
Jim Grosbach | 01ccab4 | 2011-04-06 22:13:52 +0000 | [diff] [blame] | 27 | uint8_t *EndAddress) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 28 | // Allocate memory for the function via the memory manager. |
| 29 | uintptr_t Size = EndAddress - StartAddress + 1; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 30 | uintptr_t AllocSize = Size; |
| 31 | uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 32 | assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && |
| 33 | "Memory manager failed to allocate enough memory!"); |
| 34 | // Copy the function payload into the memory block. |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 35 | memcpy(Mem, StartAddress, Size); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 36 | MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); |
| 37 | // Remember where we put it. |
| 38 | Functions[Name] = sys::MemoryBlock(Mem, Size); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 39 | // Default the assigned address for this symbol to wherever this |
| 40 | // allocated it. |
| 41 | SymbolTable[Name] = Mem; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 42 | DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 45 | // Resolve the relocations for all symbols we currently know about. |
| 46 | void RuntimeDyldImpl::resolveRelocations() { |
| 47 | // Just iterate over the symbols in our symbol table and assign their |
| 48 | // addresses. |
| 49 | StringMap<uint8_t*>::iterator i = SymbolTable.begin(); |
| 50 | StringMap<uint8_t*>::iterator e = SymbolTable.end(); |
| 51 | for (;i != e; ++i) |
| 52 | reassignSymbolAddress(i->getKey(), i->getValue()); |
| 53 | } |
| 54 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | // RuntimeDyld class implementation |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 57 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
| 58 | Dyld = 0; |
| 59 | MM = mm; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | RuntimeDyld::~RuntimeDyld() { |
| 63 | delete Dyld; |
| 64 | } |
| 65 | |
| 66 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 67 | if (!Dyld) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame^] | 68 | sys::LLVMFileType type = sys::IdentifyFileType( |
| 69 | InputBuffer->getBufferStart(), |
| 70 | static_cast<unsigned>(InputBuffer->getBufferSize())); |
| 71 | switch (type) { |
| 72 | case sys::ELF_Relocatable_FileType: |
| 73 | case sys::ELF_Executable_FileType: |
| 74 | case sys::ELF_SharedObject_FileType: |
| 75 | case sys::ELF_Core_FileType: |
| 76 | Dyld = new RuntimeDyldELF(MM); |
| 77 | break; |
| 78 | case sys::Mach_O_Object_FileType: |
| 79 | case sys::Mach_O_Executable_FileType: |
| 80 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 81 | case sys::Mach_O_Core_FileType: |
| 82 | case sys::Mach_O_PreloadExecutable_FileType: |
| 83 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 84 | case sys::Mach_O_DynamicLinker_FileType: |
| 85 | case sys::Mach_O_Bundle_FileType: |
| 86 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 87 | case sys::Mach_O_DSYMCompanion_FileType: |
| 88 | Dyld = new RuntimeDyldMachO(MM); |
| 89 | break; |
| 90 | case sys::Unknown_FileType: |
| 91 | case sys::Bitcode_FileType: |
| 92 | case sys::Archive_FileType: |
| 93 | case sys::COFF_FileType: |
| 94 | report_fatal_error("Incompatible object format!"); |
| 95 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 96 | } else { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame^] | 97 | if (!Dyld->isCompatibleFormat(InputBuffer)) |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 98 | report_fatal_error("Incompatible object format!"); |
| 99 | } |
| 100 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 101 | return Dyld->loadObject(InputBuffer); |
| 102 | } |
| 103 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 104 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 105 | return Dyld->getSymbolAddress(Name); |
| 106 | } |
| 107 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 108 | void RuntimeDyld::resolveRelocations() { |
| 109 | Dyld->resolveRelocations(); |
| 110 | } |
| 111 | |
| 112 | void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { |
| 113 | Dyld->reassignSymbolAddress(Name, Addr); |
| 114 | } |
| 115 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 116 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 117 | return Dyld->getErrorString(); |
| 118 | } |
| 119 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 120 | } // end namespace llvm |