1259698Sdim//===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
2259698Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6259698Sdim//
7259698Sdim//===----------------------------------------------------------------------===//
8259698Sdim//
9259698Sdim// Interface of the runtime dynamic memory manager base class.
10259698Sdim//
11259698Sdim//===----------------------------------------------------------------------===//
12259698Sdim
13280031Sdim#ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
14280031Sdim#define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
15259698Sdim
16321369Sdim#include "llvm-c/ExecutionEngine.h"
17314564Sdim#include "llvm/ExecutionEngine/JITSymbol.h"
18314564Sdim#include "llvm/ExecutionEngine/RuntimeDyld.h"
19314564Sdim#include "llvm/Support/CBindingWrapping.h"
20314564Sdim#include <cstddef>
21314564Sdim#include <cstdint>
22314564Sdim#include <string>
23259698Sdim
24259698Sdimnamespace llvm {
25259698Sdim
26259698Sdimclass ExecutionEngine;
27259698Sdim
28314564Sdimnamespace object {
29314564Sdim  class ObjectFile;
30314564Sdim} // end namespace object
31280031Sdim
32288943Sdimclass MCJITMemoryManager : public RuntimeDyld::MemoryManager {
33288943Sdimpublic:
34296417Sdim  // Don't hide the notifyObjectLoaded method from RuntimeDyld::MemoryManager.
35296417Sdim  using RuntimeDyld::MemoryManager::notifyObjectLoaded;
36296417Sdim
37288943Sdim  /// This method is called after an object has been loaded into memory but
38288943Sdim  /// before relocations are applied to the loaded sections.  The object load
39288943Sdim  /// may have been initiated by MCJIT to resolve an external symbol for another
40288943Sdim  /// object that is being finalized.  In that case, the object about which
41288943Sdim  /// the memory manager is being notified will be finalized immediately after
42288943Sdim  /// the memory manager returns from this call.
43288943Sdim  ///
44288943Sdim  /// Memory managers which are preparing code for execution in an external
45288943Sdim  /// address space can use this call to remap the section addresses for the
46288943Sdim  /// newly loaded object.
47288943Sdim  virtual void notifyObjectLoaded(ExecutionEngine *EE,
48288943Sdim                                  const object::ObjectFile &) {}
49341825Sdim
50341825Sdimprivate:
51341825Sdim  void anchor() override;
52288943Sdim};
53288943Sdim
54259698Sdim// RuntimeDyld clients often want to handle the memory management of
55259698Sdim// what gets placed where. For JIT clients, this is the subset of
56259698Sdim// JITMemoryManager required for dynamic loading of binaries.
57259698Sdim//
58259698Sdim// FIXME: As the RuntimeDyld fills out, additional routines will be needed
59259698Sdim//        for the varying types of objects to be allocated.
60288943Sdimclass RTDyldMemoryManager : public MCJITMemoryManager,
61341825Sdim                            public LegacyJITSymbolResolver {
62314564Sdimpublic:
63314564Sdim  RTDyldMemoryManager() = default;
64288943Sdim  RTDyldMemoryManager(const RTDyldMemoryManager&) = delete;
65288943Sdim  void operator=(const RTDyldMemoryManager&) = delete;
66288943Sdim  ~RTDyldMemoryManager() override;
67259698Sdim
68309124Sdim  /// Register EH frames in the current process.
69309124Sdim  static void registerEHFramesInProcess(uint8_t *Addr, size_t Size);
70259698Sdim
71309124Sdim  /// Deregister EH frames in the current proces.
72309124Sdim  static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
73309124Sdim
74321369Sdim  void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
75321369Sdim  void deregisterEHFrames() override;
76309124Sdim
77280031Sdim  /// This method returns the address of the specified function or variable in
78280031Sdim  /// the current process.
79280031Sdim  static uint64_t getSymbolAddressInProcess(const std::string &Name);
80280031Sdim
81288943Sdim  /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
82288943Sdim  ///
83259698Sdim  /// This method returns the address of the specified function or variable.
84259698Sdim  /// It is used to resolve symbols during module linking.
85280031Sdim  virtual uint64_t getSymbolAddress(const std::string &Name) {
86280031Sdim    return getSymbolAddressInProcess(Name);
87280031Sdim  }
88259698Sdim
89288943Sdim  /// This method returns a RuntimeDyld::SymbolInfo for the specified function
90288943Sdim  /// or variable. It is used to resolve symbols during module linking.
91288943Sdim  ///
92288943Sdim  /// By default this falls back on the legacy lookup method:
93288943Sdim  /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
94288943Sdim  /// a strong, exported symbol, consistent with historical treatment by
95288943Sdim  /// RuntimeDyld.
96288943Sdim  ///
97288943Sdim  /// Clients writing custom RTDyldMemoryManagers are encouraged to override
98288943Sdim  /// this method and return a SymbolInfo with the flags set correctly. This is
99288943Sdim  /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
100314564Sdim  JITSymbol findSymbol(const std::string &Name) override {
101314564Sdim    return JITSymbol(getSymbolAddress(Name), JITSymbolFlags::Exported);
102288943Sdim  }
103288943Sdim
104288943Sdim  /// Legacy symbol lookup -- DEPRECATED! Please override
105288943Sdim  /// findSymbolInLogicalDylib instead.
106288943Sdim  ///
107288943Sdim  /// Default to treating all modules as separate.
108288943Sdim  virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
109288943Sdim    return 0;
110288943Sdim  }
111288943Sdim
112288943Sdim  /// Default to treating all modules as separate.
113288943Sdim  ///
114288943Sdim  /// By default this falls back on the legacy lookup method:
115288943Sdim  /// 'getSymbolAddressInLogicalDylib'. The address returned by
116288943Sdim  /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
117288943Sdim  /// consistent with historical treatment by RuntimeDyld.
118288943Sdim  ///
119288943Sdim  /// Clients writing custom RTDyldMemoryManagers are encouraged to override
120288943Sdim  /// this method and return a SymbolInfo with the flags set correctly. This is
121288943Sdim  /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
122314564Sdim  JITSymbol
123288943Sdim  findSymbolInLogicalDylib(const std::string &Name) override {
124314564Sdim    return JITSymbol(getSymbolAddressInLogicalDylib(Name),
125314564Sdim                          JITSymbolFlags::Exported);
126288943Sdim  }
127288943Sdim
128259698Sdim  /// This method returns the address of the specified function. As such it is
129259698Sdim  /// only useful for resolving library symbols, not code generated symbols.
130259698Sdim  ///
131259698Sdim  /// If \p AbortOnFailure is false and no function with the given name is
132259698Sdim  /// found, this function returns a null pointer. Otherwise, it prints a
133259698Sdim  /// message to stderr and aborts.
134259698Sdim  ///
135259698Sdim  /// This function is deprecated for memory managers to be used with
136259698Sdim  /// MCJIT or RuntimeDyld.  Use getSymbolAddress instead.
137259698Sdim  virtual void *getPointerToNamedFunction(const std::string &Name,
138259698Sdim                                          bool AbortOnFailure = true);
139321369Sdim
140321369Sdimprotected:
141321369Sdim  struct EHFrame {
142321369Sdim    uint8_t *Addr;
143321369Sdim    size_t Size;
144321369Sdim  };
145321369Sdim  typedef std::vector<EHFrame> EHFrameInfos;
146321369Sdim  EHFrameInfos EHFrames;
147341825Sdim
148341825Sdimprivate:
149341825Sdim  void anchor() override;
150259698Sdim};
151259698Sdim
152259698Sdim// Create wrappers for C Binding types (see CBindingWrapping.h).
153259698SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(
154259698Sdim    RTDyldMemoryManager, LLVMMCJITMemoryManagerRef)
155259698Sdim
156314564Sdim} // end namespace llvm
157259698Sdim
158314564Sdim#endif // LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
159