RTDyldObjectLinkingLayer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Contains the definition for an RTDyld-based, in-process object linking layer.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
  18. #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ExecutionEngine/JITEventListener.h"
  23. #include "llvm/ExecutionEngine/JITSymbol.h"
  24. #include "llvm/ExecutionEngine/Orc/Core.h"
  25. #include "llvm/ExecutionEngine/Orc/Layer.h"
  26. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  27. #include "llvm/Object/ObjectFile.h"
  28. #include "llvm/Support/Error.h"
  29. #include <algorithm>
  30. #include <cassert>
  31. #include <functional>
  32. #include <list>
  33. #include <memory>
  34. #include <string>
  35. #include <utility>
  36. #include <vector>
  37. namespace llvm {
  38. namespace orc {
  39. class RTDyldObjectLinkingLayer : public ObjectLayer, private ResourceManager {
  40. public:
  41. /// Functor for receiving object-loaded notifications.
  42. using NotifyLoadedFunction = std::function<void(
  43. MaterializationResponsibility &R, const object::ObjectFile &Obj,
  44. const RuntimeDyld::LoadedObjectInfo &)>;
  45. /// Functor for receiving finalization notifications.
  46. using NotifyEmittedFunction = std::function<void(
  47. MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>;
  48. using GetMemoryManagerFunction =
  49. std::function<std::unique_ptr<RuntimeDyld::MemoryManager>()>;
  50. /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
  51. /// and NotifyEmitted functors.
  52. RTDyldObjectLinkingLayer(ExecutionSession &ES,
  53. GetMemoryManagerFunction GetMemoryManager);
  54. ~RTDyldObjectLinkingLayer();
  55. /// Emit the object.
  56. void emit(std::unique_ptr<MaterializationResponsibility> R,
  57. std::unique_ptr<MemoryBuffer> O) override;
  58. /// Set the NotifyLoaded callback.
  59. RTDyldObjectLinkingLayer &setNotifyLoaded(NotifyLoadedFunction NotifyLoaded) {
  60. this->NotifyLoaded = std::move(NotifyLoaded);
  61. return *this;
  62. }
  63. /// Set the NotifyEmitted callback.
  64. RTDyldObjectLinkingLayer &
  65. setNotifyEmitted(NotifyEmittedFunction NotifyEmitted) {
  66. this->NotifyEmitted = std::move(NotifyEmitted);
  67. return *this;
  68. }
  69. /// Set the 'ProcessAllSections' flag.
  70. ///
  71. /// If set to true, all sections in each object file will be allocated using
  72. /// the memory manager, rather than just the sections required for execution.
  73. ///
  74. /// This is kludgy, and may be removed in the future.
  75. RTDyldObjectLinkingLayer &setProcessAllSections(bool ProcessAllSections) {
  76. this->ProcessAllSections = ProcessAllSections;
  77. return *this;
  78. }
  79. /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags
  80. /// returned by RuntimeDyld for any given object file with the flags supplied
  81. /// by the MaterializationResponsibility instance. This is a workaround to
  82. /// support symbol visibility in COFF, which does not use the libObject's
  83. /// SF_Exported flag. Use only when generating / adding COFF object files.
  84. ///
  85. /// FIXME: We should be able to remove this if/when COFF properly tracks
  86. /// exported symbols.
  87. RTDyldObjectLinkingLayer &
  88. setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) {
  89. this->OverrideObjectFlags = OverrideObjectFlags;
  90. return *this;
  91. }
  92. /// If set, this RTDyldObjectLinkingLayer instance will claim responsibility
  93. /// for any symbols provided by a given object file that were not already in
  94. /// the MaterializationResponsibility instance. Setting this flag allows
  95. /// higher-level program representations (e.g. LLVM IR) to be added based on
  96. /// only a subset of the symbols they provide, without having to write
  97. /// intervening layers to scan and add the additional symbols. This trades
  98. /// diagnostic quality for convenience however: If all symbols are enumerated
  99. /// up-front then clashes can be detected and reported early (and usually
  100. /// deterministically). If this option is set, clashes for the additional
  101. /// symbols may not be detected until late, and detection may depend on
  102. /// the flow of control through JIT'd code. Use with care.
  103. RTDyldObjectLinkingLayer &
  104. setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
  105. this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
  106. return *this;
  107. }
  108. /// Register a JITEventListener.
  109. void registerJITEventListener(JITEventListener &L);
  110. /// Unregister a JITEventListener.
  111. void unregisterJITEventListener(JITEventListener &L);
  112. private:
  113. using MemoryManagerUP = std::unique_ptr<RuntimeDyld::MemoryManager>;
  114. Error onObjLoad(MaterializationResponsibility &R,
  115. const object::ObjectFile &Obj,
  116. RuntimeDyld::MemoryManager &MemMgr,
  117. RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
  118. std::map<StringRef, JITEvaluatedSymbol> Resolved,
  119. std::set<StringRef> &InternalSymbols);
  120. void onObjEmit(MaterializationResponsibility &R,
  121. object::OwningBinary<object::ObjectFile> O,
  122. std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
  123. std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
  124. Error Err);
  125. Error handleRemoveResources(ResourceKey K) override;
  126. void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override;
  127. mutable std::mutex RTDyldLayerMutex;
  128. GetMemoryManagerFunction GetMemoryManager;
  129. NotifyLoadedFunction NotifyLoaded;
  130. NotifyEmittedFunction NotifyEmitted;
  131. bool ProcessAllSections = false;
  132. bool OverrideObjectFlags = false;
  133. bool AutoClaimObjectSymbols = false;
  134. DenseMap<ResourceKey, std::vector<MemoryManagerUP>> MemMgrs;
  135. std::vector<JITEventListener *> EventListeners;
  136. };
  137. } // end namespace orc
  138. } // end namespace llvm
  139. #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
  140. #ifdef __GNUC__
  141. #pragma GCC diagnostic pop
  142. #endif