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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H |
| 16 | #define LLVM_TOOLS_LLI_REMOTEMEMORYMANAGER_H |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 18 | #include "RemoteTarget.h" |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Lang Hames | 0f15490 | 2014-09-23 16:56:02 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/Memory.h" |
| 24 | #include <utility> |
| 25 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Lang Hames | 0f15490 | 2014-09-23 16:56:02 +0000 | [diff] [blame] | 28 | class RemoteMemoryManager : public RTDyldMemoryManager { |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 29 | public: |
| 30 | // Notice that this structure takes ownership of the memory allocated. |
| 31 | struct Allocation { |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 32 | Allocation() {} |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 33 | Allocation(sys::MemoryBlock mb, unsigned a, bool code) |
| 34 | : MB(mb), Alignment(a), IsCode(code) {} |
| 35 | |
| 36 | sys::MemoryBlock MB; |
| 37 | unsigned Alignment; |
| 38 | bool IsCode; |
| 39 | }; |
| 40 | |
| 41 | private: |
| 42 | // This vector contains Allocation objects for all sections which we have |
| 43 | // allocated. This vector effectively owns the memory associated with the |
| 44 | // allocations. |
| 45 | SmallVector<Allocation, 2> AllocatedSections; |
| 46 | |
| 47 | // This vector contains pointers to Allocation objects for any sections we |
| 48 | // have allocated locally but have not yet remapped for the remote target. |
| 49 | // When we receive notification of a completed module load, we will map |
| 50 | // these sections into the remote target. |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 51 | SmallVector<Allocation, 2> UnmappedSections; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 52 | |
| 53 | // This map tracks the sections we have remapped for the remote target |
| 54 | // but have not yet copied to the target. |
Andrew Kaylor | 9723176 | 2013-10-04 20:09:36 +0000 | [diff] [blame] | 55 | DenseMap<uint64_t, Allocation> MappedSections; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 56 | |
| 57 | // FIXME: This is part of a work around to keep sections near one another |
| 58 | // when MCJIT performs relocations after code emission but before |
| 59 | // the generated code is moved to the remote target. |
| 60 | sys::MemoryBlock Near; |
| 61 | sys::MemoryBlock allocateSection(uintptr_t Size); |
| 62 | |
| 63 | RemoteTarget *Target; |
| 64 | |
| 65 | public: |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 66 | RemoteMemoryManager() : Target(nullptr) {} |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 67 | ~RemoteMemoryManager() override; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 68 | |
| 69 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 70 | unsigned SectionID, |
| 71 | StringRef SectionName) override; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 72 | |
| 73 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 74 | unsigned SectionID, StringRef SectionName, |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 75 | bool IsReadOnly) override; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 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. |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 81 | uint64_t getSymbolAddress(const std::string &Name) override { return 0; } |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 82 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 83 | void notifyObjectLoaded(ExecutionEngine *EE, |
| 84 | const object::ObjectFile &Obj) override; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 85 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 86 | bool finalizeMemory(std::string *ErrMsg) override; |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 87 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 88 | // For now, remote EH frame registration isn't supported. Remote symbol |
| 89 | // resolution is a prerequisite to supporting remote EH frame registration. |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 90 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 91 | size_t Size) override {} |
| 92 | void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 93 | size_t Size) override {} |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 94 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 95 | // This is a non-interface function used by lli |
| 96 | void setRemoteTarget(RemoteTarget *T) { Target = T; } |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // end namespace llvm |
| 100 | |
| 101 | #endif |