RTDyldMemoryManager.h revision 276479
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_RT_DYLD_MEMORY_MANAGER_H
15#define LLVM_EXECUTIONENGINE_RT_DYLD_MEMORY_MANAGER_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
22namespace llvm {
23
24class ExecutionEngine;
25class ObjectImage;
26
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.
30//
31// FIXME: As the RuntimeDyld fills out, additional routines will be needed
32//        for the varying types of objects to be allocated.
33class RTDyldMemoryManager {
34  RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
35  void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
36public:
37  RTDyldMemoryManager() {}
38  virtual ~RTDyldMemoryManager();
39
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
43  /// section.
44  virtual uint8_t *allocateCodeSection(
45    uintptr_t Size, unsigned Alignment, unsigned SectionID,
46    StringRef SectionName) = 0;
47
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;
54
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
60  ///
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) { }
65
66  /// Override to return true to enable the reserveAllocationSpace callback.
67  virtual bool needsToReserveAllocationSpace() { return false; }
68
69  /// Register the EH frames with the runtime so that c++ exceptions work.
70  ///
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);
76
77  virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
78
79  /// This method returns the address of the specified function or variable.
80  /// It is used to resolve symbols during module linking.
81  virtual uint64_t getSymbolAddress(const std::string &Name);
82
83  /// This method returns the address of the specified function. As such it is
84  /// only useful for resolving library symbols, not code generated symbols.
85  ///
86  /// If \p AbortOnFailure is false and no function with the given name is
87  /// found, this function returns a null pointer. Otherwise, it prints a
88  /// message to stderr and aborts.
89  ///
90  /// This function is deprecated for memory managers to be used with
91  /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
92  virtual void *getPointerToNamedFunction(const std::string &Name,
93                                          bool AbortOnFailure = true);
94
95  /// This method is called after an object has been loaded into memory but
96  /// before relocations are applied to the loaded sections.  The object load
97  /// may have been initiated by MCJIT to resolve an external symbol for another
98  /// object that is being finalized.  In that case, the object about which
99  /// the memory manager is being notified will be finalized immediately after
100  /// the memory manager returns from this call.
101  ///
102  /// Memory managers which are preparing code for execution in an external
103  /// address space can use this call to remap the section addresses for the
104  /// newly loaded object.
105  virtual void notifyObjectLoaded(ExecutionEngine *EE,
106                                  const ObjectImage *) {}
107
108  /// This method is called when object loading is complete and section page
109  /// permissions can be applied.  It is up to the memory manager implementation
110  /// to decide whether or not to act on this method.  The memory manager will
111  /// typically allocate all sections as read-write and then apply specific
112  /// permissions when this method is called.  Code sections cannot be executed
113  /// until this function has been called.  In addition, any cache coherency
114  /// operations needed to reliably use the memory are also performed.
115  ///
116  /// Returns true if an error occurred, false otherwise.
117  virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
118};
119
120// Create wrappers for C Binding types (see CBindingWrapping.h).
121DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
122    RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
123
124} // namespace llvm
125
126#endif // LLVM_EXECUTIONENGINE_RT_DYLD_MEMORY_MANAGER_H
127