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