Home | History | Annotate | Download | only in ExecutionEngine
      1 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
      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 // Interface of the runtime dynamic memory manager base class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
     15 #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
     16 
     17 #include "llvm-c/ExecutionEngine.h"
     18 #include "llvm/ExecutionEngine/JITSymbol.h"
     19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
     20 #include "llvm/Support/CBindingWrapping.h"
     21 #include <cstddef>
     22 #include <cstdint>
     23 #include <string>
     24 
     25 namespace llvm {
     26 
     27 class ExecutionEngine;
     28 
     29 namespace object {
     30   class ObjectFile;
     31 } // end namespace object
     32 
     33 class MCJITMemoryManager : public RuntimeDyld::MemoryManager {
     34 public:
     35   // Don't hide the notifyObjectLoaded method from RuntimeDyld::MemoryManager.
     36   using RuntimeDyld::MemoryManager::notifyObjectLoaded;
     37 
     38   /// This method is called after an object has been loaded into memory but
     39   /// before relocations are applied to the loaded sections.  The object load
     40   /// may have been initiated by MCJIT to resolve an external symbol for another
     41   /// object that is being finalized.  In that case, the object about which
     42   /// the memory manager is being notified will be finalized immediately after
     43   /// the memory manager returns from this call.
     44   ///
     45   /// Memory managers which are preparing code for execution in an external
     46   /// address space can use this call to remap the section addresses for the
     47   /// newly loaded object.
     48   virtual void notifyObjectLoaded(ExecutionEngine *EE,
     49                                   const object::ObjectFile &) {}
     50 };
     51 
     52 // RuntimeDyld clients often want to handle the memory management of
     53 // what gets placed where. For JIT clients, this is the subset of
     54 // JITMemoryManager required for dynamic loading of binaries.
     55 //
     56 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
     57 //        for the varying types of objects to be allocated.
     58 class RTDyldMemoryManager : public MCJITMemoryManager,
     59                             public JITSymbolResolver {
     60 public:
     61   RTDyldMemoryManager() = default;
     62   RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
     63   void operator=(const RTDyldMemoryManager&) = delete;
     64   ~RTDyldMemoryManager() override;
     65 
     66   /// Register EH frames in the current process.
     67   static void registerEHFramesInProcess(uint8_t *Addr, size_t Size);
     68 
     69   /// Deregister EH frames in the current proces.
     70   static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
     71 
     72   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
     73   void deregisterEHFrames() override;
     74 
     75   /// This method returns the address of the specified function or variable in
     76   /// the current process.
     77   static uint64_t getSymbolAddressInProcess(const std::string &Name);
     78 
     79   /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
     80   ///
     81   /// This method returns the address of the specified function or variable.
     82   /// It is used to resolve symbols during module linking.
     83   virtual uint64_t getSymbolAddress(const std::string &Name) {
     84     return getSymbolAddressInProcess(Name);
     85   }
     86 
     87   /// This method returns a RuntimeDyld::SymbolInfo for the specified function
     88   /// or variable. It is used to resolve symbols during module linking.
     89   ///
     90   /// By default this falls back on the legacy lookup method:
     91   /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
     92   /// a strong, exported symbol, consistent with historical treatment by
     93   /// RuntimeDyld.
     94   ///
     95   /// Clients writing custom RTDyldMemoryManagers are encouraged to override
     96   /// this method and return a SymbolInfo with the flags set correctly. This is
     97   /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
     98   JITSymbol findSymbol(const std::string &Name) override {
     99     return JITSymbol(getSymbolAddress(Name), JITSymbolFlags::Exported);
    100   }
    101 
    102   /// Legacy symbol lookup -- DEPRECATED! Please override
    103   /// findSymbolInLogicalDylib instead.
    104   ///
    105   /// Default to treating all modules as separate.
    106   virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
    107     return 0;
    108   }
    109 
    110   /// Default to treating all modules as separate.
    111   ///
    112   /// By default this falls back on the legacy lookup method:
    113   /// 'getSymbolAddressInLogicalDylib'. The address returned by
    114   /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
    115   /// consistent with historical treatment by RuntimeDyld.
    116   ///
    117   /// Clients writing custom RTDyldMemoryManagers are encouraged to override
    118   /// this method and return a SymbolInfo with the flags set correctly. This is
    119   /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
    120   JITSymbol
    121   findSymbolInLogicalDylib(const std::string &Name) override {
    122     return JITSymbol(getSymbolAddressInLogicalDylib(Name),
    123                           JITSymbolFlags::Exported);
    124   }
    125 
    126   /// This method returns the address of the specified function. As such it is
    127   /// only useful for resolving library symbols, not code generated symbols.
    128   ///
    129   /// If \p AbortOnFailure is false and no function with the given name is
    130   /// found, this function returns a null pointer. Otherwise, it prints a
    131   /// message to stderr and aborts.
    132   ///
    133   /// This function is deprecated for memory managers to be used with
    134   /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
    135   virtual void *getPointerToNamedFunction(const std::string &Name,
    136                                           bool AbortOnFailure = true);
    137 
    138 protected:
    139   struct EHFrame {
    140     uint8_t *Addr;
    141     size_t Size;
    142   };
    143   typedef std::vector<EHFrame> EHFrameInfos;
    144   EHFrameInfos EHFrames;
    145 };
    146 
    147 // Create wrappers for C Binding types (see CBindingWrapping.h).
    148 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
    149     RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
    150 
    151 } // end namespace llvm
    152 
    153 #endif // LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
    154