Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:53

0001 //===---- EPCGenericRTDyldMemoryManager.h - EPC-based MemMgr ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Defines a RuntimeDyld::MemoryManager that uses EPC and the ORC runtime
0010 // bootstrap functions.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
0015 #define LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H
0016 
0017 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
0018 #include "llvm/ExecutionEngine/RuntimeDyld.h"
0019 
0020 #define DEBUG_TYPE "orc"
0021 
0022 namespace llvm {
0023 namespace orc {
0024 
0025 /// Remote-mapped RuntimeDyld-compatible memory manager.
0026 class EPCGenericRTDyldMemoryManager : public RuntimeDyld::MemoryManager {
0027 public:
0028   /// Symbol addresses for memory access.
0029   struct SymbolAddrs {
0030     ExecutorAddr Instance;
0031     ExecutorAddr Reserve;
0032     ExecutorAddr Finalize;
0033     ExecutorAddr Deallocate;
0034     ExecutorAddr RegisterEHFrame;
0035     ExecutorAddr DeregisterEHFrame;
0036   };
0037 
0038   /// Create an EPCGenericRTDyldMemoryManager using the given EPC, looking up
0039   /// the default symbol names in the bootstrap symbol set.
0040   static Expected<std::unique_ptr<EPCGenericRTDyldMemoryManager>>
0041   CreateWithDefaultBootstrapSymbols(ExecutorProcessControl &EPC);
0042 
0043   /// Create an EPCGenericRTDyldMemoryManager using the given EPC and symbol
0044   /// addrs.
0045   EPCGenericRTDyldMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs);
0046 
0047   EPCGenericRTDyldMemoryManager(const EPCGenericRTDyldMemoryManager &) = delete;
0048   EPCGenericRTDyldMemoryManager &
0049   operator=(const EPCGenericRTDyldMemoryManager &) = delete;
0050   EPCGenericRTDyldMemoryManager(EPCGenericRTDyldMemoryManager &&) = delete;
0051   EPCGenericRTDyldMemoryManager &
0052   operator=(EPCGenericRTDyldMemoryManager &&) = delete;
0053   ~EPCGenericRTDyldMemoryManager();
0054 
0055   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
0056                                unsigned SectionID,
0057                                StringRef SectionName) override;
0058 
0059   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
0060                                unsigned SectionID, StringRef SectionName,
0061                                bool IsReadOnly) override;
0062 
0063   void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
0064                               uintptr_t RODataSize, Align RODataAlign,
0065                               uintptr_t RWDataSize, Align RWDataAlign) override;
0066 
0067   bool needsToReserveAllocationSpace() override;
0068 
0069   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
0070 
0071   void deregisterEHFrames() override;
0072 
0073   void notifyObjectLoaded(RuntimeDyld &Dyld,
0074                           const object::ObjectFile &Obj) override;
0075 
0076   bool finalizeMemory(std::string *ErrMsg = nullptr) override;
0077 
0078 private:
0079   struct SectionAlloc {
0080   public:
0081     SectionAlloc(uint64_t Size, unsigned Align)
0082         : Size(Size), Align(Align),
0083           Contents(std::make_unique<uint8_t[]>(Size + Align - 1)) {}
0084 
0085     uint64_t Size;
0086     unsigned Align;
0087     std::unique_ptr<uint8_t[]> Contents;
0088     ExecutorAddr RemoteAddr;
0089   };
0090 
0091   // Group of section allocations to be allocated together in the executor. The
0092   // RemoteCodeAddr will stand in as the id of the group for deallocation
0093   // purposes.
0094   struct SectionAllocGroup {
0095     SectionAllocGroup() = default;
0096     SectionAllocGroup(const SectionAllocGroup &) = delete;
0097     SectionAllocGroup &operator=(const SectionAllocGroup &) = delete;
0098     SectionAllocGroup(SectionAllocGroup &&) = default;
0099     SectionAllocGroup &operator=(SectionAllocGroup &&) = default;
0100 
0101     ExecutorAddrRange RemoteCode;
0102     ExecutorAddrRange RemoteROData;
0103     ExecutorAddrRange RemoteRWData;
0104     std::vector<ExecutorAddrRange> UnfinalizedEHFrames;
0105     std::vector<SectionAlloc> CodeAllocs, RODataAllocs, RWDataAllocs;
0106   };
0107 
0108   // Maps all allocations in SectionAllocs to aligned blocks
0109   void mapAllocsToRemoteAddrs(RuntimeDyld &Dyld,
0110                               std::vector<SectionAlloc> &SecAllocs,
0111                               ExecutorAddr NextAddr);
0112 
0113   ExecutorProcessControl &EPC;
0114   SymbolAddrs SAs;
0115 
0116   std::mutex M;
0117   std::vector<SectionAllocGroup> Unmapped;
0118   std::vector<SectionAllocGroup> Unfinalized;
0119   std::vector<ExecutorAddr> FinalizedAllocs;
0120   std::string ErrMsg;
0121 };
0122 
0123 } // end namespace orc
0124 } // end namespace llvm
0125 
0126 #undef DEBUG_TYPE
0127 
0128 #endif // LLVM_EXECUTIONENGINE_ORC_EPCGENERICRTDYLDMEMORYMANAGER_H