LLVM API Documentation

RTDyldMemoryManager.h
Go to the documentation of this file.
00001 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Interface of the runtime dynamic memory manager base class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
00015 #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
00016 
00017 #include "llvm-c/ExecutionEngine.h"
00018 #include "llvm/ADT/StringRef.h"
00019 #include "llvm/Support/CBindingWrapping.h"
00020 #include "llvm/Support/Memory.h"
00021 
00022 namespace llvm {
00023 
00024 class ExecutionEngine;
00025 class ObjectImage;
00026 
00027 // RuntimeDyld clients often want to handle the memory management of
00028 // what gets placed where. For JIT clients, this is the subset of
00029 // JITMemoryManager required for dynamic loading of binaries.
00030 //
00031 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
00032 //        for the varying types of objects to be allocated.
00033 class RTDyldMemoryManager {
00034   RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
00035   void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
00036 public:
00037   RTDyldMemoryManager() {}
00038   virtual ~RTDyldMemoryManager();
00039 
00040   /// Allocate a memory block of (at least) the given size suitable for
00041   /// executable code. The SectionID is a unique identifier assigned by the JIT
00042   /// engine, and optionally recorded by the memory manager to access a loaded
00043   /// section.
00044   virtual uint8_t *allocateCodeSection(
00045     uintptr_t Size, unsigned Alignment, unsigned SectionID,
00046     StringRef SectionName) = 0;
00047 
00048   /// Allocate a memory block of (at least) the given size suitable for data.
00049   /// The SectionID is a unique identifier assigned by the JIT engine, and
00050   /// optionally recorded by the memory manager to access a loaded section.
00051   virtual uint8_t *allocateDataSection(
00052     uintptr_t Size, unsigned Alignment, unsigned SectionID,
00053     StringRef SectionName, bool IsReadOnly) = 0;
00054 
00055   /// Inform the memory manager about the total amount of memory required to
00056   /// allocate all sections to be loaded:
00057   /// \p CodeSize - the total size of all code sections
00058   /// \p DataSizeRO - the total size of all read-only data sections
00059   /// \p DataSizeRW - the total size of all read-write data sections
00060   /// 
00061   /// Note that by default the callback is disabled. To enable it
00062   /// redefine the method needsToReserveAllocationSpace to return true.
00063   virtual void reserveAllocationSpace(
00064     uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { }
00065   
00066   /// Override to return true to enable the reserveAllocationSpace callback.
00067   virtual bool needsToReserveAllocationSpace() { return false; }
00068 
00069   /// Register the EH frames with the runtime so that c++ exceptions work.
00070   ///
00071   /// \p Addr parameter provides the local address of the EH frame section
00072   /// data, while \p LoadAddr provides the address of the data in the target
00073   /// address space.  If the section has not been remapped (which will usually
00074   /// be the case for local execution) these two values will be the same.
00075   virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
00076 
00077   virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
00078 
00079   /// This method returns the address of the specified function or variable.
00080   /// It is used to resolve symbols during module linking.
00081   virtual uint64_t getSymbolAddress(const std::string &Name);
00082 
00083   /// This method returns the address of the specified function. As such it is
00084   /// only useful for resolving library symbols, not code generated symbols.
00085   ///
00086   /// If \p AbortOnFailure is false and no function with the given name is
00087   /// found, this function returns a null pointer. Otherwise, it prints a
00088   /// message to stderr and aborts.
00089   ///
00090   /// This function is deprecated for memory managers to be used with
00091   /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
00092   virtual void *getPointerToNamedFunction(const std::string &Name,
00093                                           bool AbortOnFailure = true);
00094 
00095   /// This method is called after an object has been loaded into memory but
00096   /// before relocations are applied to the loaded sections.  The object load
00097   /// may have been initiated by MCJIT to resolve an external symbol for another
00098   /// object that is being finalized.  In that case, the object about which
00099   /// the memory manager is being notified will be finalized immediately after
00100   /// the memory manager returns from this call.
00101   ///
00102   /// Memory managers which are preparing code for execution in an external
00103   /// address space can use this call to remap the section addresses for the
00104   /// newly loaded object.
00105   virtual void notifyObjectLoaded(ExecutionEngine *EE,
00106                                   const ObjectImage *) {}
00107 
00108   /// This method is called when object loading is complete and section page
00109   /// permissions can be applied.  It is up to the memory manager implementation
00110   /// to decide whether or not to act on this method.  The memory manager will
00111   /// typically allocate all sections as read-write and then apply specific
00112   /// permissions when this method is called.  Code sections cannot be executed
00113   /// until this function has been called.  In addition, any cache coherency
00114   /// operations needed to reliably use the memory are also performed.
00115   ///
00116   /// Returns true if an error occurred, false otherwise.
00117   virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
00118 };
00119 
00120 // Create wrappers for C Binding types (see CBindingWrapping.h).
00121 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
00122     RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
00123 
00124 } // namespace llvm
00125 
00126 #endif