bbc8684706dd2510eaa80a3e0d3de369d73c92e4
[oota-llvm.git] / include / llvm / ExecutionEngine / RTDyldMemoryManager.h
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/ADT/StringRef.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Memory.h"
21
22 namespace llvm {
23
24 class ExecutionEngine;
25 class ObjectImage;
26
27 uint64_t getSymbolAddress(const std::string &Name);
28
29 // RuntimeDyld clients often want to handle the memory management of
30 // what gets placed where. For JIT clients, this is the subset of
31 // JITMemoryManager required for dynamic loading of binaries.
32 //
33 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
34 //        for the varying types of objects to be allocated.
35 class RTDyldMemoryManager {
36   RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
37   void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
38 public:
39   RTDyldMemoryManager() {}
40   virtual ~RTDyldMemoryManager();
41
42   /// Allocate a memory block of (at least) the given size suitable for
43   /// executable code. The SectionID is a unique identifier assigned by the JIT
44   /// engine, and optionally recorded by the memory manager to access a loaded
45   /// section.
46   virtual uint8_t *allocateCodeSection(
47     uintptr_t Size, unsigned Alignment, unsigned SectionID,
48     StringRef SectionName) = 0;
49
50   /// Allocate a memory block of (at least) the given size suitable for data.
51   /// The SectionID is a unique identifier assigned by the JIT engine, and
52   /// optionally recorded by the memory manager to access a loaded section.
53   virtual uint8_t *allocateDataSection(
54     uintptr_t Size, unsigned Alignment, unsigned SectionID,
55     StringRef SectionName, bool IsReadOnly) = 0;
56
57   /// Inform the memory manager about the total amount of memory required to
58   /// allocate all sections to be loaded:
59   /// \p CodeSize - the total size of all code sections
60   /// \p DataSizeRO - the total size of all read-only data sections
61   /// \p DataSizeRW - the total size of all read-write data sections
62   /// 
63   /// Note that by default the callback is disabled. To enable it
64   /// redefine the method needsToReserveAllocationSpace to return true.
65   virtual void reserveAllocationSpace(
66     uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { }
67   
68   /// Override to return true to enable the reserveAllocationSpace callback.
69   virtual bool needsToReserveAllocationSpace() { return false; }
70
71   /// Register the EH frames with the runtime so that c++ exceptions work.
72   ///
73   /// \p Addr parameter provides the local address of the EH frame section
74   /// data, while \p LoadAddr provides the address of the data in the target
75   /// address space.  If the section has not been remapped (which will usually
76   /// be the case for local execution) these two values will be the same.
77   virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
78
79   virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
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 llvm::getSymbolAddress(Name);
85   }
86
87   /// This method returns the address of the specified function. As such it is
88   /// only useful for resolving library symbols, not code generated symbols.
89   ///
90   /// If \p AbortOnFailure is false and no function with the given name is
91   /// found, this function returns a null pointer. Otherwise, it prints a
92   /// message to stderr and aborts.
93   ///
94   /// This function is deprecated for memory managers to be used with
95   /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
96   virtual void *getPointerToNamedFunction(const std::string &Name,
97                                           bool AbortOnFailure = true);
98
99   /// This method is called after an object has been loaded into memory but
100   /// before relocations are applied to the loaded sections.  The object load
101   /// may have been initiated by MCJIT to resolve an external symbol for another
102   /// object that is being finalized.  In that case, the object about which
103   /// the memory manager is being notified will be finalized immediately after
104   /// the memory manager returns from this call.
105   ///
106   /// Memory managers which are preparing code for execution in an external
107   /// address space can use this call to remap the section addresses for the
108   /// newly loaded object.
109   virtual void notifyObjectLoaded(ExecutionEngine *EE,
110                                   const ObjectImage *) {}
111
112   /// This method is called when object loading is complete and section page
113   /// permissions can be applied.  It is up to the memory manager implementation
114   /// to decide whether or not to act on this method.  The memory manager will
115   /// typically allocate all sections as read-write and then apply specific
116   /// permissions when this method is called.  Code sections cannot be executed
117   /// until this function has been called.  In addition, any cache coherency
118   /// operations needed to reliably use the memory are also performed.
119   ///
120   /// Returns true if an error occurred, false otherwise.
121   virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
122 };
123
124 // Create wrappers for C Binding types (see CBindingWrapping.h).
125 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
126     RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
127
128 } // namespace llvm
129
130 #endif