Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 1 | //===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===// |
| 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 | // This memory manager allocates local storage and keeps a record of each |
| 11 | // allocation. Iterators are provided for all data and code allocations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef REMOTEMEMORYMANAGER_H |
| 16 | #define REMOTEMEMORYMANAGER_H |
| 17 | |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/Memory.h" |
| 23 | #include <utility> |
| 24 | |
| 25 | #include "RemoteTarget.h" |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | class RemoteMemoryManager : public JITMemoryManager { |
| 30 | public: |
| 31 | // Notice that this structure takes ownership of the memory allocated. |
| 32 | struct Allocation { |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 33 | Allocation() {} |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 34 | Allocation(sys::MemoryBlock mb, unsigned a, bool code) |
| 35 | : MB(mb), Alignment(a), IsCode(code) {} |
| 36 | |
| 37 | sys::MemoryBlock MB; |
| 38 | unsigned Alignment; |
| 39 | bool IsCode; |
| 40 | }; |
| 41 | |
| 42 | private: |
| 43 | // This vector contains Allocation objects for all sections which we have |
| 44 | // allocated. This vector effectively owns the memory associated with the |
| 45 | // allocations. |
| 46 | SmallVector<Allocation, 2> AllocatedSections; |
| 47 | |
| 48 | // This vector contains pointers to Allocation objects for any sections we |
| 49 | // have allocated locally but have not yet remapped for the remote target. |
| 50 | // When we receive notification of a completed module load, we will map |
| 51 | // these sections into the remote target. |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 52 | SmallVector<Allocation, 2> UnmappedSections; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 53 | |
| 54 | // This map tracks the sections we have remapped for the remote target |
| 55 | // but have not yet copied to the target. |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 56 | DenseMap<uint64_t, Allocation> MappedSections; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 57 | |
| 58 | // FIXME: This is part of a work around to keep sections near one another |
| 59 | // when MCJIT performs relocations after code emission but before |
| 60 | // the generated code is moved to the remote target. |
| 61 | sys::MemoryBlock Near; |
| 62 | sys::MemoryBlock allocateSection(uintptr_t Size); |
| 63 | |
| 64 | RemoteTarget *Target; |
| 65 | |
| 66 | public: |
| 67 | RemoteMemoryManager() : Target(NULL) {} |
| 68 | virtual ~RemoteMemoryManager(); |
| 69 | |
| 70 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 71 | unsigned SectionID, StringRef SectionName); |
| 72 | |
| 73 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 74 | unsigned SectionID, StringRef SectionName, |
| 75 | bool IsReadOnly); |
| 76 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame^] | 77 | // For now, remote symbol resolution is not support in lli. The MCJIT |
| 78 | // interface does support this, but clients must provide their own |
| 79 | // mechanism for finding remote symbol addresses. MCJIT will resolve |
| 80 | // symbols from Modules it contains. |
| 81 | uint64_t getSymbolAddress(const std::string &Name) {} |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 82 | |
| 83 | void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj); |
| 84 | |
| 85 | bool finalizeMemory(std::string *ErrMsg); |
| 86 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame^] | 87 | // For now, remote EH frame registration isn't supported. Remote symbol |
| 88 | // resolution is a prerequisite to supporting remote EH frame registration. |
| 89 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {} |
| 90 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 91 | // This is a non-interface function used by lli |
| 92 | void setRemoteTarget(RemoteTarget *T) { Target = T; } |
| 93 | |
| 94 | // The following obsolete JITMemoryManager calls are stubbed out for |
| 95 | // this model. |
| 96 | void setMemoryWritable(); |
| 97 | void setMemoryExecutable(); |
| 98 | void setPoisonMemory(bool poison); |
| 99 | void AllocateGOT(); |
| 100 | uint8_t *getGOTBase() const; |
| 101 | uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize); |
| 102 | uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, |
| 103 | unsigned Alignment); |
| 104 | void endFunctionBody(const Function *F, uint8_t *FunctionStart, |
| 105 | uint8_t *FunctionEnd); |
| 106 | uint8_t *allocateSpace(intptr_t Size, unsigned Alignment); |
| 107 | uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment); |
| 108 | void deallocateFunctionBody(void *Body); |
| 109 | }; |
| 110 | |
| 111 | } // end namespace llvm |
| 112 | |
| 113 | #endif |