Jim Grosbach | e0934be | 2012-01-16 23:50: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 | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame^] | 16 | #include "RuntimeDyldELF.h" |
| 17 | #include "RuntimeDyldMachO.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame^] | 19 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | using namespace llvm::object; |
| 22 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 23 | // Empty out-of-line virtual destructor as the key function. |
| 24 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 25 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 26 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 27 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 28 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 29 | void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, |
Jim Grosbach | 01ccab4 | 2011-04-06 22:13:52 +0000 | [diff] [blame] | 30 | uint8_t *EndAddress) { |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 31 | // FIXME: DEPRECATED in favor of by-section allocation. |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 32 | // Allocate memory for the function via the memory manager. |
| 33 | uintptr_t Size = EndAddress - StartAddress + 1; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 34 | uintptr_t AllocSize = Size; |
| 35 | uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 36 | assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && |
| 37 | "Memory manager failed to allocate enough memory!"); |
| 38 | // Copy the function payload into the memory block. |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 39 | memcpy(Mem, StartAddress, Size); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 40 | MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); |
| 41 | // Remember where we put it. |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 42 | unsigned SectionID = Sections.size(); |
| 43 | Sections.push_back(sys::MemoryBlock(Mem, Size)); |
| 44 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 45 | // Default the assigned address for this symbol to wherever this |
| 46 | // allocated it. |
Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 47 | SymbolTable[Name] = SymbolLoc(SectionID, 0); |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 48 | DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 51 | // Resolve the relocations for all symbols we currently know about. |
| 52 | void RuntimeDyldImpl::resolveRelocations() { |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 53 | // Just iterate over the sections we have and resolve all the relocations |
| 54 | // in them. Gross overkill, but it gets the job done. |
| 55 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
| 56 | reassignSectionAddress(i, SectionLoadAddress[i]); |
| 57 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 60 | void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress, |
| 61 | uint64_t TargetAddress) { |
| 62 | assert(SectionLocalMemToID.count(LocalAddress) && |
| 63 | "Attempting to remap address of unknown section!"); |
| 64 | unsigned SectionID = SectionLocalMemToID[LocalAddress]; |
| 65 | reassignSectionAddress(SectionID, TargetAddress); |
| 66 | } |
| 67 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 68 | //===----------------------------------------------------------------------===// |
| 69 | // RuntimeDyld class implementation |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 70 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
| 71 | Dyld = 0; |
| 72 | MM = mm; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | RuntimeDyld::~RuntimeDyld() { |
| 76 | delete Dyld; |
| 77 | } |
| 78 | |
| 79 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 80 | if (!Dyld) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 81 | sys::LLVMFileType type = sys::IdentifyFileType( |
| 82 | InputBuffer->getBufferStart(), |
| 83 | static_cast<unsigned>(InputBuffer->getBufferSize())); |
| 84 | switch (type) { |
| 85 | case sys::ELF_Relocatable_FileType: |
| 86 | case sys::ELF_Executable_FileType: |
| 87 | case sys::ELF_SharedObject_FileType: |
| 88 | case sys::ELF_Core_FileType: |
| 89 | Dyld = new RuntimeDyldELF(MM); |
| 90 | break; |
| 91 | case sys::Mach_O_Object_FileType: |
| 92 | case sys::Mach_O_Executable_FileType: |
| 93 | case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 94 | case sys::Mach_O_Core_FileType: |
| 95 | case sys::Mach_O_PreloadExecutable_FileType: |
| 96 | case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 97 | case sys::Mach_O_DynamicLinker_FileType: |
| 98 | case sys::Mach_O_Bundle_FileType: |
| 99 | case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 100 | case sys::Mach_O_DSYMCompanion_FileType: |
| 101 | Dyld = new RuntimeDyldMachO(MM); |
| 102 | break; |
| 103 | case sys::Unknown_FileType: |
| 104 | case sys::Bitcode_FileType: |
| 105 | case sys::Archive_FileType: |
| 106 | case sys::COFF_FileType: |
| 107 | report_fatal_error("Incompatible object format!"); |
| 108 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 109 | } else { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 110 | if (!Dyld->isCompatibleFormat(InputBuffer)) |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 111 | report_fatal_error("Incompatible object format!"); |
| 112 | } |
| 113 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 114 | return Dyld->loadObject(InputBuffer); |
| 115 | } |
| 116 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 117 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 118 | return Dyld->getSymbolAddress(Name); |
| 119 | } |
| 120 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 121 | void RuntimeDyld::resolveRelocations() { |
| 122 | Dyld->resolveRelocations(); |
| 123 | } |
| 124 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 125 | void RuntimeDyld::reassignSectionAddress(unsigned SectionID, |
| 126 | uint64_t Addr) { |
| 127 | Dyld->reassignSectionAddress(SectionID, Addr); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 130 | void RuntimeDyld::mapSectionAddress(void *LocalAddress, |
| 131 | uint64_t TargetAddress) { |
| 132 | Dyld->mapSectionAddress(LocalAddress, TargetAddress); |
| 133 | } |
| 134 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 135 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 136 | return Dyld->getErrorString(); |
| 137 | } |
| 138 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 139 | } // end namespace llvm |