RuntimeDyld.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //===-- RuntimeDyld.h - Run-time dynamic linker 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 for the runtime dynamic linker facilities of the MC-JIT.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
  14. #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
  15. #include "JITSymbolFlags.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Support/Memory.h"
  19. #include "llvm/DebugInfo/DIContext.h"
  20. #include <memory>
  21. namespace llvm {
  22. namespace object {
  23. class ObjectFile;
  24. template <typename T> class OwningBinary;
  25. }
  26. class RuntimeDyldImpl;
  27. class RuntimeDyldCheckerImpl;
  28. class RuntimeDyld {
  29. friend class RuntimeDyldCheckerImpl;
  30. RuntimeDyld(const RuntimeDyld &) = delete;
  31. void operator=(const RuntimeDyld &) = delete;
  32. protected:
  33. // Change the address associated with a section when resolving relocations.
  34. // Any relocations already associated with the symbol will be re-resolved.
  35. void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
  36. public:
  37. /// \brief Information about a named symbol.
  38. class SymbolInfo : public JITSymbolBase {
  39. public:
  40. SymbolInfo(std::nullptr_t) : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
  41. SymbolInfo(uint64_t Address, JITSymbolFlags Flags)
  42. : JITSymbolBase(Flags), Address(Address) {}
  43. explicit operator bool() const { return Address != 0; }
  44. uint64_t getAddress() const { return Address; }
  45. private:
  46. uint64_t Address;
  47. };
  48. /// \brief Information about the loaded object.
  49. class LoadedObjectInfo : public llvm::LoadedObjectInfo {
  50. friend class RuntimeDyldImpl;
  51. public:
  52. LoadedObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
  53. unsigned EndIdx)
  54. : RTDyld(RTDyld), BeginIdx(BeginIdx), EndIdx(EndIdx) { }
  55. virtual object::OwningBinary<object::ObjectFile>
  56. getObjectForDebug(const object::ObjectFile &Obj) const = 0;
  57. uint64_t getSectionLoadAddress(StringRef Name) const;
  58. protected:
  59. virtual void anchor();
  60. RuntimeDyldImpl &RTDyld;
  61. unsigned BeginIdx, EndIdx;
  62. };
  63. template <typename Derived> struct LoadedObjectInfoHelper : LoadedObjectInfo {
  64. LoadedObjectInfoHelper(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
  65. unsigned EndIdx)
  66. : LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
  67. std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
  68. return llvm::make_unique<Derived>(static_cast<const Derived &>(*this));
  69. }
  70. };
  71. /// \brief Memory Management.
  72. class MemoryManager {
  73. public:
  74. virtual ~MemoryManager() {};
  75. /// Allocate a memory block of (at least) the given size suitable for
  76. /// executable code. The SectionID is a unique identifier assigned by the
  77. /// RuntimeDyld instance, and optionally recorded by the memory manager to
  78. /// access a loaded section.
  79. virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
  80. unsigned SectionID,
  81. StringRef SectionName) = 0;
  82. /// Allocate a memory block of (at least) the given size suitable for data.
  83. /// The SectionID is a unique identifier assigned by the JIT engine, and
  84. /// optionally recorded by the memory manager to access a loaded section.
  85. virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
  86. unsigned SectionID,
  87. StringRef SectionName,
  88. bool IsReadOnly) = 0;
  89. /// Inform the memory manager about the total amount of memory required to
  90. /// allocate all sections to be loaded:
  91. /// \p CodeSize - the total size of all code sections
  92. /// \p DataSizeRO - the total size of all read-only data sections
  93. /// \p DataSizeRW - the total size of all read-write data sections
  94. ///
  95. /// Note that by default the callback is disabled. To enable it
  96. /// redefine the method needsToReserveAllocationSpace to return true.
  97. virtual void reserveAllocationSpace(uintptr_t CodeSize,
  98. uintptr_t DataSizeRO,
  99. uintptr_t DataSizeRW) {}
  100. /// Override to return true to enable the reserveAllocationSpace callback.
  101. virtual bool needsToReserveAllocationSpace() { return false; }
  102. /// Register the EH frames with the runtime so that c++ exceptions work.
  103. ///
  104. /// \p Addr parameter provides the local address of the EH frame section
  105. /// data, while \p LoadAddr provides the address of the data in the target
  106. /// address space. If the section has not been remapped (which will usually
  107. /// be the case for local execution) these two values will be the same.
  108. virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
  109. size_t Size) = 0;
  110. virtual void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
  111. size_t Size) = 0;
  112. /// This method is called when object loading is complete and section page
  113. /// permissions can be applied. It is up to the memory manager implementation
  114. /// to decide whether or not to act on this method. The memory manager will
  115. /// typically allocate all sections as read-write and then apply specific
  116. /// permissions when this method is called. Code sections cannot be executed
  117. /// until this function has been called. In addition, any cache coherency
  118. /// operations needed to reliably use the memory are also performed.
  119. ///
  120. /// Returns true if an error occurred, false otherwise.
  121. virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
  122. private:
  123. virtual void anchor();
  124. };
  125. /// \brief Symbol resolution.
  126. class SymbolResolver {
  127. public:
  128. virtual ~SymbolResolver() {};
  129. /// This method returns the address of the specified function or variable.
  130. /// It is used to resolve symbols during module linking.
  131. ///
  132. /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
  133. /// skip all relocations for that symbol, and the client will be responsible
  134. /// for handling them manually.
  135. virtual SymbolInfo findSymbol(const std::string &Name) = 0;
  136. /// This method returns the address of the specified symbol if it exists
  137. /// within the logical dynamic library represented by this
  138. /// RTDyldMemoryManager. Unlike getSymbolAddress, queries through this
  139. /// interface should return addresses for hidden symbols.
  140. ///
  141. /// This is of particular importance for the Orc JIT APIs, which support lazy
  142. /// compilation by breaking up modules: Each of those broken out modules
  143. /// must be able to resolve hidden symbols provided by the others. Clients
  144. /// writing memory managers for MCJIT can usually ignore this method.
  145. ///
  146. /// This method will be queried by RuntimeDyld when checking for previous
  147. /// definitions of common symbols. It will *not* be queried by default when
  148. /// resolving external symbols (this minimises the link-time overhead for
  149. /// MCJIT clients who don't care about Orc features). If you are writing a
  150. /// RTDyldMemoryManager for Orc and want "external" symbol resolution to
  151. /// search the logical dylib, you should override your getSymbolAddress
  152. /// method call this method directly.
  153. virtual SymbolInfo findSymbolInLogicalDylib(const std::string &Name) = 0;
  154. private:
  155. virtual void anchor();
  156. };
  157. /// \brief Construct a RuntimeDyld instance.
  158. RuntimeDyld(MemoryManager &MemMgr, SymbolResolver &Resolver);
  159. ~RuntimeDyld();
  160. /// Add the referenced object file to the list of objects to be loaded and
  161. /// relocated.
  162. std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
  163. /// Get the address of our local copy of the symbol. This may or may not
  164. /// be the address used for relocation (clients can copy the data around
  165. /// and resolve relocatons based on where they put it).
  166. void *getSymbolLocalAddress(StringRef Name) const;
  167. /// Get the target address and flags for the named symbol.
  168. /// This address is the one used for relocation.
  169. SymbolInfo getSymbol(StringRef Name) const;
  170. /// Resolve the relocations for all symbols we currently know about.
  171. void resolveRelocations();
  172. /// Map a section to its target address space value.
  173. /// Map the address of a JIT section as returned from the memory manager
  174. /// to the address in the target process as the running code will see it.
  175. /// This is the address which will be used for relocation resolution.
  176. void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
  177. /// Register any EH frame sections that have been loaded but not previously
  178. /// registered with the memory manager. Note, RuntimeDyld is responsible
  179. /// for identifying the EH frame and calling the memory manager with the
  180. /// EH frame section data. However, the memory manager itself will handle
  181. /// the actual target-specific EH frame registration.
  182. void registerEHFrames();
  183. void deregisterEHFrames();
  184. bool hasError();
  185. StringRef getErrorString();
  186. /// By default, only sections that are "required for execution" are passed to
  187. /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
  188. /// to this method will cause RuntimeDyld to pass all sections to its
  189. /// memory manager regardless of whether they are "required to execute" in the
  190. /// usual sense. This is useful for inspecting metadata sections that may not
  191. /// contain relocations, E.g. Debug info, stackmaps.
  192. ///
  193. /// Must be called before the first object file is loaded.
  194. void setProcessAllSections(bool ProcessAllSections) {
  195. assert(!Dyld && "setProcessAllSections must be called before loadObject.");
  196. this->ProcessAllSections = ProcessAllSections;
  197. }
  198. private:
  199. // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
  200. // interface.
  201. std::unique_ptr<RuntimeDyldImpl> Dyld;
  202. MemoryManager &MemMgr;
  203. SymbolResolver &Resolver;
  204. bool ProcessAllSections;
  205. RuntimeDyldCheckerImpl *Checker;
  206. };
  207. } // end namespace llvm
  208. #endif