Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- 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 // Interface of the runtime dynamic memory manager base class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
0014 #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
0015 
0016 #include "llvm-c/ExecutionEngine.h"
0017 #include "llvm/ExecutionEngine/JITSymbol.h"
0018 #include "llvm/ExecutionEngine/RuntimeDyld.h"
0019 #include "llvm/Support/CBindingWrapping.h"
0020 #include <cstddef>
0021 #include <cstdint>
0022 #include <string>
0023 
0024 namespace llvm {
0025 
0026 class ExecutionEngine;
0027 
0028 namespace object {
0029   class ObjectFile;
0030 } // end namespace object
0031 
0032 class MCJITMemoryManager : public RuntimeDyld::MemoryManager {
0033 public:
0034   // Don't hide the notifyObjectLoaded method from RuntimeDyld::MemoryManager.
0035   using RuntimeDyld::MemoryManager::notifyObjectLoaded;
0036 
0037   /// This method is called after an object has been loaded into memory but
0038   /// before relocations are applied to the loaded sections.  The object load
0039   /// may have been initiated by MCJIT to resolve an external symbol for another
0040   /// object that is being finalized.  In that case, the object about which
0041   /// the memory manager is being notified will be finalized immediately after
0042   /// the memory manager returns from this call.
0043   ///
0044   /// Memory managers which are preparing code for execution in an external
0045   /// address space can use this call to remap the section addresses for the
0046   /// newly loaded object.
0047   virtual void notifyObjectLoaded(ExecutionEngine *EE,
0048                                   const object::ObjectFile &) {}
0049 
0050 private:
0051   void anchor() override;
0052 };
0053 
0054 // RuntimeDyld clients often want to handle the memory management of
0055 // what gets placed where. For JIT clients, this is the subset of
0056 // JITMemoryManager required for dynamic loading of binaries.
0057 //
0058 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
0059 //        for the varying types of objects to be allocated.
0060 class RTDyldMemoryManager : public MCJITMemoryManager,
0061                             public LegacyJITSymbolResolver {
0062 public:
0063   RTDyldMemoryManager() = default;
0064   RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
0065   void operator=(const RTDyldMemoryManager&) = delete;
0066   ~RTDyldMemoryManager() override;
0067 
0068   /// Register EH frames in the current process.
0069   static void registerEHFramesInProcess(uint8_t *Addr, size_t Size);
0070 
0071   /// Deregister EH frames in the current process.
0072   static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
0073 
0074   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
0075   void deregisterEHFrames() override;
0076 
0077   /// This method returns the address of the specified function or variable in
0078   /// the current process.
0079   static uint64_t getSymbolAddressInProcess(const std::string &Name);
0080 
0081   /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
0082   ///
0083   /// This method returns the address of the specified function or variable.
0084   /// It is used to resolve symbols during module linking.
0085   virtual uint64_t getSymbolAddress(const std::string &Name) {
0086     return getSymbolAddressInProcess(Name);
0087   }
0088 
0089   /// This method returns a RuntimeDyld::SymbolInfo for the specified function
0090   /// or variable. It is used to resolve symbols during module linking.
0091   ///
0092   /// By default this falls back on the legacy lookup method:
0093   /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
0094   /// a strong, exported symbol, consistent with historical treatment by
0095   /// RuntimeDyld.
0096   ///
0097   /// Clients writing custom RTDyldMemoryManagers are encouraged to override
0098   /// this method and return a SymbolInfo with the flags set correctly. This is
0099   /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
0100   JITSymbol findSymbol(const std::string &Name) override {
0101     return JITSymbol(getSymbolAddress(Name), JITSymbolFlags::Exported);
0102   }
0103 
0104   /// Legacy symbol lookup -- DEPRECATED! Please override
0105   /// findSymbolInLogicalDylib instead.
0106   ///
0107   /// Default to treating all modules as separate.
0108   virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
0109     return 0;
0110   }
0111 
0112   /// Default to treating all modules as separate.
0113   ///
0114   /// By default this falls back on the legacy lookup method:
0115   /// 'getSymbolAddressInLogicalDylib'. The address returned by
0116   /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
0117   /// consistent with historical treatment by RuntimeDyld.
0118   ///
0119   /// Clients writing custom RTDyldMemoryManagers are encouraged to override
0120   /// this method and return a SymbolInfo with the flags set correctly. This is
0121   /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
0122   JITSymbol
0123   findSymbolInLogicalDylib(const std::string &Name) override {
0124     return JITSymbol(getSymbolAddressInLogicalDylib(Name),
0125                           JITSymbolFlags::Exported);
0126   }
0127 
0128   /// This method returns the address of the specified function. As such it is
0129   /// only useful for resolving library symbols, not code generated symbols.
0130   ///
0131   /// If \p AbortOnFailure is false and no function with the given name is
0132   /// found, this function returns a null pointer. Otherwise, it prints a
0133   /// message to stderr and aborts.
0134   ///
0135   /// This function is deprecated for memory managers to be used with
0136   /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
0137   virtual void *getPointerToNamedFunction(const std::string &Name,
0138                                           bool AbortOnFailure = true);
0139 
0140 protected:
0141   struct EHFrame {
0142     uint8_t *Addr;
0143     size_t Size;
0144   };
0145   typedef std::vector<EHFrame> EHFrameInfos;
0146   EHFrameInfos EHFrames;
0147 
0148 private:
0149   void anchor() override;
0150 };
0151 
0152 // Create wrappers for C Binding types (see CBindingWrapping.h).
0153 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
0154     RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
0155 
0156 } // end namespace llvm
0157 
0158 #endif // LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H