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/ADT/StringRef.h"
18#include "llvm/Support/CBindingWrapping.h"
19#include "llvm/Support/Memory.h"
20#include "llvm-c/ExecutionEngine.h"
21
22namespace llvm {
23
24// RuntimeDyld clients often want to handle the memory management of
25// what gets placed where. For JIT clients, this is the subset of
26// JITMemoryManager required for dynamic loading of binaries.
27//
28// FIXME: As the RuntimeDyld fills out, additional routines will be needed
29//        for the varying types of objects to be allocated.
30class RTDyldMemoryManager {
31  RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
32  void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
33public:
34  RTDyldMemoryManager() {}
35  virtual ~RTDyldMemoryManager();
36
37  /// Allocate a memory block of (at least) the given size suitable for
38  /// executable code. The SectionID is a unique identifier assigned by the JIT
39  /// engine, and optionally recorded by the memory manager to access a loaded
40  /// section.
41  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
42                                       unsigned SectionID) = 0;
43
44  /// Allocate a memory block of (at least) the given size suitable for data.
45  /// The SectionID is a unique identifier assigned by the JIT engine, and
46  /// optionally recorded by the memory manager to access a loaded section.
47  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
48                                       unsigned SectionID, bool IsReadOnly) = 0;
49
50  /// Register the EH frames with the runtime so that c++ exceptions work.
51  virtual void registerEHFrames(StringRef SectionData);
52
53  /// This method returns the address of the specified function. As such it is
54  /// only useful for resolving library symbols, not code generated symbols.
55  ///
56  /// If \p AbortOnFailure is false and no function with the given name is
57  /// found, this function returns a null pointer. Otherwise, it prints a
58  /// message to stderr and aborts.
59  virtual void *getPointerToNamedFunction(const std::string &Name,
60                                          bool AbortOnFailure = true);
61
62  /// This method is called when object loading is complete and section page
63  /// permissions can be applied.  It is up to the memory manager implementation
64  /// to decide whether or not to act on this method.  The memory manager will
65  /// typically allocate all sections as read-write and then apply specific
66  /// permissions when this method is called.  Code sections cannot be executed
67  /// until this function has been called.  In addition, any cache coherency
68  /// operations needed to reliably use the memory are also performed.
69  ///
70  /// Returns true if an error occurred, false otherwise.
71  virtual bool finalizeMemory(std::string *ErrMsg = 0) = 0;
72};
73
74// Create wrappers for C Binding types (see CBindingWrapping.h).
75DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
76    RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
77
78} // namespace llvm
79
80#endif // LLVM_EXECUTIONENGINE_RT_DYLD_MEMORY_MANAGER_H
81