1 //===---- EPCGenericRTDyldMemoryManager.h - EPC-based MemMgr ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Defines a RuntimeDyld::MemoryManager that uses EPC and the ORC runtime
10 // bootstrap functions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
15 #define LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
17 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #define DEBUG_TYPE "orc"
25 /// Remote-mapped RuntimeDyld-compatible memory manager.
26 class EPCGenericRTDyldMemoryManager : public RuntimeDyld::MemoryManager {
28 /// Symbol addresses for memory access.
30 ExecutorAddr Instance;
32 ExecutorAddr Finalize;
33 ExecutorAddr Deallocate;
34 ExecutorAddr RegisterEHFrame;
35 ExecutorAddr DeregisterEHFrame;
38 /// Create an EPCGenericRTDyldMemoryManager using the given EPC, looking up
39 /// the default symbol names in the bootstrap symbol set.
40 static Expected<std::unique_ptr<EPCGenericRTDyldMemoryManager>>
41 CreateWithDefaultBootstrapSymbols(ExecutorProcessControl &EPC);
43 /// Create an EPCGenericRTDyldMemoryManager using the given EPC and symbol
45 EPCGenericRTDyldMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs);
47 EPCGenericRTDyldMemoryManager(const EPCGenericRTDyldMemoryManager &) = delete;
48 EPCGenericRTDyldMemoryManager &
49 operator=(const EPCGenericRTDyldMemoryManager &) = delete;
50 EPCGenericRTDyldMemoryManager(EPCGenericRTDyldMemoryManager &&) = delete;
51 EPCGenericRTDyldMemoryManager &
52 operator=(EPCGenericRTDyldMemoryManager &&) = delete;
53 ~EPCGenericRTDyldMemoryManager();
55 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
57 StringRef SectionName) override;
59 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
60 unsigned SectionID, StringRef SectionName,
61 bool IsReadOnly) override;
63 void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
64 uintptr_t RODataSize, Align RODataAlign,
65 uintptr_t RWDataSize, Align RWDataAlign) override;
67 bool needsToReserveAllocationSpace() override;
69 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
71 void deregisterEHFrames() override;
73 void notifyObjectLoaded(RuntimeDyld &Dyld,
74 const object::ObjectFile &Obj) override;
76 bool finalizeMemory(std::string *ErrMsg = nullptr) override;
81 SectionAlloc(uint64_t Size, unsigned Align)
82 : Size(Size), Align(Align),
83 Contents(std::make_unique<uint8_t[]>(Size + Align - 1)) {}
87 std::unique_ptr<uint8_t[]> Contents;
88 ExecutorAddr RemoteAddr;
91 // Group of section allocations to be allocated together in the executor. The
92 // RemoteCodeAddr will stand in as the id of the group for deallocation
94 struct SectionAllocGroup {
95 SectionAllocGroup() = default;
96 SectionAllocGroup(const SectionAllocGroup &) = delete;
97 SectionAllocGroup &operator=(const SectionAllocGroup &) = delete;
98 SectionAllocGroup(SectionAllocGroup &&) = default;
99 SectionAllocGroup &operator=(SectionAllocGroup &&) = default;
101 ExecutorAddrRange RemoteCode;
102 ExecutorAddrRange RemoteROData;
103 ExecutorAddrRange RemoteRWData;
104 std::vector<ExecutorAddrRange> UnfinalizedEHFrames;
105 std::vector<SectionAlloc> CodeAllocs, RODataAllocs, RWDataAllocs;
108 // Maps all allocations in SectionAllocs to aligned blocks
109 void mapAllocsToRemoteAddrs(RuntimeDyld &Dyld,
110 std::vector<SectionAlloc> &SecAllocs,
111 ExecutorAddr NextAddr);
113 ExecutorProcessControl &EPC;
117 std::vector<SectionAllocGroup> Unmapped;
118 std::vector<SectionAllocGroup> Unfinalized;
119 std::vector<ExecutorAddr> FinalizedAllocs;
123 } // end namespace orc
124 } // end namespace llvm
128 #endif // LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H