Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===// |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +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 | // Interface for the implementations of runtime dynamic linker facilities. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_RUNTIME_DYLD_IMPL_H |
| 15 | #define LLVM_RUNTIME_DYLD_IMPL_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Memory.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/system_error.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | |
| 30 | using namespace llvm; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 31 | |
| 32 | namespace llvm { |
| 33 | class RuntimeDyldImpl { |
| 34 | protected: |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 35 | unsigned CPUType; |
| 36 | unsigned CPUSubtype; |
| 37 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 38 | // The MemoryManager to load objects into. |
| 39 | RTDyldMemoryManager *MemMgr; |
| 40 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 41 | // For each section, we have a MemoryBlock of it's data. |
| 42 | // Indexed by SectionID. |
| 43 | SmallVector<sys::MemoryBlock, 32> Sections; |
| 44 | // For each section, the address it will be considered to live at for |
| 45 | // relocations. The same as the pointer to the above memory block for hosted |
| 46 | // JITs. Indexed by SectionID. |
| 47 | SmallVector<uint64_t, 32> SectionLoadAddress; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 48 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 49 | // Keep a map of starting local address to the SectionID which references it. |
| 50 | // Lookup function for when we assign virtual addresses. |
| 51 | DenseMap<void *, unsigned> SectionLocalMemToID; |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 52 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 53 | // Master symbol table. As modules are loaded and external symbols are |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 54 | // resolved, their addresses are stored here as a SectionID/Offset pair. |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 55 | typedef std::pair<unsigned, uint64_t> SymbolLoc; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 56 | StringMap<SymbolLoc> SymbolTable; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 57 | |
| 58 | bool HasError; |
| 59 | std::string ErrorStr; |
| 60 | |
| 61 | // Set the error state and record an error string. |
| 62 | bool Error(const Twine &Msg) { |
| 63 | ErrorStr = Msg.str(); |
| 64 | HasError = true; |
| 65 | return true; |
| 66 | } |
| 67 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 68 | uint8_t *getSectionAddress(unsigned SectionID) { |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 69 | return (uint8_t*)Sections[SectionID].base(); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 70 | } |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 71 | void extractFunction(StringRef Name, uint8_t *StartAddress, |
| 72 | uint8_t *EndAddress); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 73 | |
| 74 | public: |
| 75 | RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} |
| 76 | |
| 77 | virtual ~RuntimeDyldImpl(); |
| 78 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 79 | virtual bool loadObject(MemoryBuffer *InputBuffer) = 0; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 80 | |
| 81 | void *getSymbolAddress(StringRef Name) { |
| 82 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 83 | // Work in progress. |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 84 | if (SymbolTable.find(Name) == SymbolTable.end()) |
| 85 | return 0; |
| 86 | SymbolLoc Loc = SymbolTable.lookup(Name); |
| 87 | return getSectionAddress(Loc.first) + Loc.second; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 90 | virtual void resolveRelocations(); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 91 | |
Bill Wendling | 288967d | 2012-03-29 23:23:59 +0000 | [diff] [blame] | 92 | virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 93 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 94 | void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress); |
| 95 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 96 | // Is the linker in an error state? |
| 97 | bool hasError() { return HasError; } |
| 98 | |
| 99 | // Mark the error condition as handled and continue. |
| 100 | void clearError() { HasError = false; } |
| 101 | |
| 102 | // Get the error message. |
| 103 | StringRef getErrorString() { return ErrorStr; } |
| 104 | |
| 105 | virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0; |
| 106 | }; |
| 107 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 108 | } // end namespace llvm |
| 109 | |
| 110 | |
| 111 | #endif |