[Orc] New JIT APIs.
[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
26   namespace object {
27     class ObjectFile;
28   }
29
30 // RuntimeDyld clients often want to handle the memory management of
31 // what gets placed where. For JIT clients, this is the subset of
32 // JITMemoryManager required for dynamic loading of binaries.
33 //
34 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
35 //        for the varying types of objects to be allocated.
36 class RTDyldMemoryManager {
37   RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
38   void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
39 public:
40   RTDyldMemoryManager() {}
41   virtual ~RTDyldMemoryManager();
42
43   /// Allocate a memory block of (at least) the given size suitable for
44   /// executable code. The SectionID is a unique identifier assigned by the JIT
45   /// engine, and optionally recorded by the memory manager to access a loaded
46   /// section.
47   virtual uint8_t *allocateCodeSection(
48     uintptr_t Size, unsigned Alignment, unsigned SectionID,
49     StringRef SectionName) = 0;
50
51   /// Allocate a memory block of (at least) the given size suitable for data.
52   /// The SectionID is a unique identifier assigned by the JIT engine, and
53   /// optionally recorded by the memory manager to access a loaded section.
54   virtual uint8_t *allocateDataSection(
55     uintptr_t Size, unsigned Alignment, unsigned SectionID,
56     StringRef SectionName, bool IsReadOnly) = 0;
57
58   /// Inform the memory manager about the total amount of memory required to
59   /// allocate all sections to be loaded:
60   /// \p CodeSize - the total size of all code sections
61   /// \p DataSizeRO - the total size of all read-only data sections
62   /// \p DataSizeRW - the total size of all read-write data sections
63   /// 
64   /// Note that by default the callback is disabled. To enable it
65   /// redefine the method needsToReserveAllocationSpace to return true.
66   virtual void reserveAllocationSpace(
67     uintptr_t CodeSize, uintptr_t DataSizeRO, uintptr_t DataSizeRW) { }
68   
69   /// Override to return true to enable the reserveAllocationSpace callback.
70   virtual bool needsToReserveAllocationSpace() { return false; }
71
72   /// Register the EH frames with the runtime so that c++ exceptions work.
73   ///
74   /// \p Addr parameter provides the local address of the EH frame section
75   /// data, while \p LoadAddr provides the address of the data in the target
76   /// address space.  If the section has not been remapped (which will usually
77   /// be the case for local execution) these two values will be the same.
78   virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
79
80   virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
81
82   /// This method returns the address of the specified function or variable in
83   /// the current process.
84   static uint64_t getSymbolAddressInProcess(const std::string &Name);
85
86   /// This method returns the address of the specified function or variable.
87   /// It is used to resolve symbols during module linking.
88   virtual uint64_t getSymbolAddress(const std::string &Name) {
89     return getSymbolAddressInProcess(Name);
90   }
91
92   /// This method returns the address of the specified symbol if it exists
93   /// within the logical dynamic library represented by this
94   /// RTDyldMemoryManager. Unlike getSymbolAddress, queries through this
95   /// interface should return addresses for hidden symbols.
96   ///
97   /// This is of particular importance for the Orc JIT APIs, which support lazy
98   /// compilation by breaking up modules: Each of those broken out modules
99   /// must be able to resolve hidden symbols provided by the others. Clients
100   /// writing memory managers for MCJIT can usually ignore this method.
101   ///
102   /// This method will be queried by RuntimeDyld when checking for previous
103   /// definitions of common symbols. It will *not* be queried by default when
104   /// resolving external symbols (this minimises the link-time overhead for
105   /// MCJIT clients who don't care about Orc features). If you are writing a
106   /// RTDyldMemoryManager for Orc and want "external" symbol resolution to
107   /// search the logical dylib, you should override your getSymbolAddress
108   /// method call this method directly.
109   virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
110     return 0;
111   }
112
113   /// This method returns the address of the specified function. As such it is
114   /// only useful for resolving library symbols, not code generated symbols.
115   ///
116   /// If \p AbortOnFailure is false and no function with the given name is
117   /// found, this function returns a null pointer. Otherwise, it prints a
118   /// message to stderr and aborts.
119   ///
120   /// This function is deprecated for memory managers to be used with
121   /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
122   virtual void *getPointerToNamedFunction(const std::string &Name,
123                                           bool AbortOnFailure = true);
124
125   /// This method is called after an object has been loaded into memory but
126   /// before relocations are applied to the loaded sections.  The object load
127   /// may have been initiated by MCJIT to resolve an external symbol for another
128   /// object that is being finalized.  In that case, the object about which
129   /// the memory manager is being notified will be finalized immediately after
130   /// the memory manager returns from this call.
131   ///
132   /// Memory managers which are preparing code for execution in an external
133   /// address space can use this call to remap the section addresses for the
134   /// newly loaded object.
135   virtual void notifyObjectLoaded(ExecutionEngine *EE,
136                                   const object::ObjectFile &) {}
137
138   /// This method is called when object loading is complete and section page
139   /// permissions can be applied.  It is up to the memory manager implementation
140   /// to decide whether or not to act on this method.  The memory manager will
141   /// typically allocate all sections as read-write and then apply specific
142   /// permissions when this method is called.  Code sections cannot be executed
143   /// until this function has been called.  In addition, any cache coherency
144   /// operations needed to reliably use the memory are also performed.
145   ///
146   /// Returns true if an error occurred, false otherwise.
147   virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
148 };
149
150 // Create wrappers for C Binding types (see CBindingWrapping.h).
151 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
152     RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
153
154 } // namespace llvm
155
156 #endif