LLVM  14.0.0git
RTDyldMemoryManager.h
Go to the documentation of this file.
1 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Interface of the runtime dynamic memory manager base class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
14 #define LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
15 
16 #include "llvm-c/ExecutionEngine.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 
24 namespace llvm {
25 
26 class ExecutionEngine;
27 
28 namespace object {
29  class ObjectFile;
30 } // end namespace object
31 
33 public:
34  // Don't hide the notifyObjectLoaded method from RuntimeDyld::MemoryManager.
36 
37  /// This method is called after an object has been loaded into memory but
38  /// before relocations are applied to the loaded sections. The object load
39  /// may have been initiated by MCJIT to resolve an external symbol for another
40  /// object that is being finalized. In that case, the object about which
41  /// the memory manager is being notified will be finalized immediately after
42  /// the memory manager returns from this call.
43  ///
44  /// Memory managers which are preparing code for execution in an external
45  /// address space can use this call to remap the section addresses for the
46  /// newly loaded object.
48  const object::ObjectFile &) {}
49 
50 private:
51  void anchor() override;
52 };
53 
54 // RuntimeDyld clients often want to handle the memory management of
55 // what gets placed where. For JIT clients, this is the subset of
56 // JITMemoryManager required for dynamic loading of binaries.
57 //
58 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
59 // for the varying types of objects to be allocated.
62 public:
63  RTDyldMemoryManager() = default;
65  void operator=(const RTDyldMemoryManager&) = delete;
66  ~RTDyldMemoryManager() override;
67 
68  /// Register EH frames in the current process.
69  static void registerEHFramesInProcess(uint8_t *Addr, size_t Size);
70 
71  /// Deregister EH frames in the current proces.
72  static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
73 
74  void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
75  void deregisterEHFrames() override;
76 
77  /// This method returns the address of the specified function or variable in
78  /// the current process.
79  static uint64_t getSymbolAddressInProcess(const std::string &Name);
80 
81  /// Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
82  ///
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) {
87  }
88 
89  /// This method returns a RuntimeDyld::SymbolInfo for the specified function
90  /// or variable. It is used to resolve symbols during module linking.
91  ///
92  /// By default this falls back on the legacy lookup method:
93  /// 'getSymbolAddress'. The address returned by getSymbolAddress is treated as
94  /// a strong, exported symbol, consistent with historical treatment by
95  /// RuntimeDyld.
96  ///
97  /// Clients writing custom RTDyldMemoryManagers are encouraged to override
98  /// this method and return a SymbolInfo with the flags set correctly. This is
99  /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
100  JITSymbol findSymbol(const std::string &Name) override {
102  }
103 
104  /// Legacy symbol lookup -- DEPRECATED! Please override
105  /// findSymbolInLogicalDylib instead.
106  ///
107  /// Default to treating all modules as separate.
108  virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) {
109  return 0;
110  }
111 
112  /// Default to treating all modules as separate.
113  ///
114  /// By default this falls back on the legacy lookup method:
115  /// 'getSymbolAddressInLogicalDylib'. The address returned by
116  /// getSymbolAddressInLogicalDylib is treated as a strong, exported symbol,
117  /// consistent with historical treatment by RuntimeDyld.
118  ///
119  /// Clients writing custom RTDyldMemoryManagers are encouraged to override
120  /// this method and return a SymbolInfo with the flags set correctly. This is
121  /// necessary for RuntimeDyld to correctly handle weak and non-exported symbols.
122  JITSymbol
123  findSymbolInLogicalDylib(const std::string &Name) override {
126  }
127 
128  /// This method returns the address of the specified function. As such it is
129  /// only useful for resolving library symbols, not code generated symbols.
130  ///
131  /// If \p AbortOnFailure is false and no function with the given name is
132  /// found, this function returns a null pointer. Otherwise, it prints a
133  /// message to stderr and aborts.
134  ///
135  /// This function is deprecated for memory managers to be used with
136  /// MCJIT or RuntimeDyld. Use getSymbolAddress instead.
137  virtual void *getPointerToNamedFunction(const std::string &Name,
138  bool AbortOnFailure = true);
139 
140 protected:
141  struct EHFrame {
142  uint8_t *Addr;
143  size_t Size;
144  };
145  typedef std::vector<EHFrame> EHFrameInfos;
147 
148 private:
149  void anchor() override;
150 };
151 
152 // Create wrappers for C Binding types (see CBindingWrapping.h).
155 
156 } // end namespace llvm
157 
158 #endif // LLVM_EXECUTIONENGINE_RTDYLDMEMORYMANAGER_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::RTDyldMemoryManager::registerEHFrames
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override
Register the EH frames with the runtime so that c++ exceptions work.
Definition: RTDyldMemoryManager.cpp:131
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
JITSymbol.h
llvm::RTDyldMemoryManager::RTDyldMemoryManager
RTDyldMemoryManager()=default
LLVMMCJITMemoryManagerRef
struct LLVMOpaqueMCJITMemoryManager * LLVMMCJITMemoryManagerRef
Definition: ExecutionEngine.h:41
CBindingWrapping.h
llvm::RTDyldMemoryManager::getPointerToNamedFunction
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
This method returns the address of the specified function.
Definition: RTDyldMemoryManager.cpp:284
RuntimeDyld.h
llvm::RTDyldMemoryManager::EHFrame::Addr
uint8_t * Addr
Definition: RTDyldMemoryManager.h:142
DEFINE_SIMPLE_CONVERSION_FUNCTIONS
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:19
llvm::RTDyldMemoryManager
Definition: RTDyldMemoryManager.h:60
llvm::RTDyldMemoryManager::findSymbol
JITSymbol findSymbol(const std::string &Name) override
This method returns a RuntimeDyld::SymbolInfo for the specified function or variable.
Definition: RTDyldMemoryManager.h:100
llvm::RTDyldMemoryManager::operator=
void operator=(const RTDyldMemoryManager &)=delete
llvm::RTDyldMemoryManager::findSymbolInLogicalDylib
JITSymbol findSymbolInLogicalDylib(const std::string &Name) override
Default to treating all modules as separate.
Definition: RTDyldMemoryManager.h:123
llvm::LegacyJITSymbolResolver
Legacy symbol resolution interface.
Definition: JITSymbol.h:401
llvm::RTDyldMemoryManager::EHFrameInfos
std::vector< EHFrame > EHFrameInfos
Definition: RTDyldMemoryManager.h:145
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::RTDyldMemoryManager::EHFrame
Definition: RTDyldMemoryManager.h:141
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
llvm::RTDyldMemoryManager::deregisterEHFramesInProcess
static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size)
Deregister EH frames in the current proces.
Definition: RTDyldMemoryManager.cpp:124
llvm::RuntimeDyld::MemoryManager::notifyObjectLoaded
virtual void notifyObjectLoaded(RuntimeDyld &RTDyld, const object::ObjectFile &Obj)
This method is called after an object has been loaded into memory but before relocations are applied ...
Definition: RuntimeDyld.h:183
llvm::MCJITMemoryManager::notifyObjectLoaded
virtual void notifyObjectLoaded(ExecutionEngine *EE, const object::ObjectFile &)
This method is called after an object has been loaded into memory but before relocations are applied ...
Definition: RTDyldMemoryManager.h:47
llvm::codeview::CodeViewContainer::ObjectFile
@ ObjectFile
llvm::RTDyldMemoryManager::~RTDyldMemoryManager
~RTDyldMemoryManager() override
Definition: RTDyldMemoryManager.cpp:32
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::RTDyldMemoryManager::deregisterEHFrames
void deregisterEHFrames() override
Definition: RTDyldMemoryManager.cpp:137
llvm::RTDyldMemoryManager::getSymbolAddressInProcess
static uint64_t getSymbolAddressInProcess(const std::string &Name)
This method returns the address of the specified function or variable in the current process.
Definition: RTDyldMemoryManager.cpp:223
llvm::ExecutionEngine
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Definition: ExecutionEngine.h:99
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
ExecutionEngine.h
llvm::MCJITMemoryManager
Definition: RTDyldMemoryManager.h:32
llvm::RTDyldMemoryManager::getSymbolAddress
virtual uint64_t getSymbolAddress(const std::string &Name)
Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
Definition: RTDyldMemoryManager.h:85
llvm::RTDyldMemoryManager::getSymbolAddressInLogicalDylib
virtual uint64_t getSymbolAddressInLogicalDylib(const std::string &Name)
Legacy symbol lookup – DEPRECATED! Please override findSymbolInLogicalDylib instead.
Definition: RTDyldMemoryManager.h:108
llvm::JITSymbol
Represents a symbol in the JIT.
Definition: JITSymbol.h:265
llvm::RTDyldMemoryManager::EHFrame::Size
size_t Size
Definition: RTDyldMemoryManager.h:143
llvm::JITSymbolFlags::Exported
@ Exported
Definition: JITSymbol.h:85
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92
llvm::RTDyldMemoryManager::registerEHFramesInProcess
static void registerEHFramesInProcess(uint8_t *Addr, size_t Size)
Register EH frames in the current process.
Definition: RTDyldMemoryManager.cpp:114
llvm::RTDyldMemoryManager::EHFrames
EHFrameInfos EHFrames
Definition: RTDyldMemoryManager.h:146