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" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace llvm::object; |
| 18 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 19 | // Empty out-of-line virtual destructor as the key function. |
| 20 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 21 | RuntimeDyldImpl::~RuntimeDyldImpl() {} |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 22 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 23 | namespace llvm { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 24 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 25 | void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, |
Jim Grosbach | 01ccab4 | 2011-04-06 22:13:52 +0000 | [diff] [blame] | 26 | uint8_t *EndAddress) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 27 | // Allocate memory for the function via the memory manager. |
| 28 | uintptr_t Size = EndAddress - StartAddress + 1; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 29 | uintptr_t AllocSize = Size; |
| 30 | uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 31 | assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && |
| 32 | "Memory manager failed to allocate enough memory!"); |
| 33 | // Copy the function payload into the memory block. |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 34 | memcpy(Mem, StartAddress, Size); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 35 | MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); |
| 36 | // Remember where we put it. |
| 37 | Functions[Name] = sys::MemoryBlock(Mem, Size); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 38 | // Default the assigned address for this symbol to wherever this |
| 39 | // allocated it. |
| 40 | SymbolTable[Name] = Mem; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 41 | DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 44 | // Resolve the relocations for all symbols we currently know about. |
| 45 | void RuntimeDyldImpl::resolveRelocations() { |
| 46 | // Just iterate over the symbols in our symbol table and assign their |
| 47 | // addresses. |
| 48 | StringMap<uint8_t*>::iterator i = SymbolTable.begin(); |
| 49 | StringMap<uint8_t*>::iterator e = SymbolTable.end(); |
| 50 | for (;i != e; ++i) |
| 51 | reassignSymbolAddress(i->getKey(), i->getValue()); |
| 52 | } |
| 53 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | // RuntimeDyld class implementation |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 56 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { |
| 57 | Dyld = 0; |
| 58 | MM = mm; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | RuntimeDyld::~RuntimeDyld() { |
| 62 | delete Dyld; |
| 63 | } |
| 64 | |
| 65 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 66 | if (!Dyld) { |
| 67 | if (RuntimeDyldMachO::isKnownFormat(InputBuffer)) |
| 68 | Dyld = new RuntimeDyldMachO(MM); |
| 69 | else |
| 70 | report_fatal_error("Unknown object format!"); |
| 71 | } else { |
| 72 | if(!Dyld->isCompatibleFormat(InputBuffer)) |
| 73 | report_fatal_error("Incompatible object format!"); |
| 74 | } |
| 75 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 76 | return Dyld->loadObject(InputBuffer); |
| 77 | } |
| 78 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 79 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 80 | return Dyld->getSymbolAddress(Name); |
| 81 | } |
| 82 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 83 | void RuntimeDyld::resolveRelocations() { |
| 84 | Dyld->resolveRelocations(); |
| 85 | } |
| 86 | |
| 87 | void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { |
| 88 | Dyld->reassignSymbolAddress(Name, Addr); |
| 89 | } |
| 90 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 91 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 92 | return Dyld->getErrorString(); |
| 93 | } |
| 94 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 95 | } // end namespace llvm |