ExecutionUtils.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //===-- ExecutionUtils.h - Utilities for executing code in Orc --*- 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. // Contains utilities for executing code in Orc.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
  14. #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
  15. #include "JITSymbol.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  19. #include <vector>
  20. namespace llvm {
  21. class ConstantArray;
  22. class GlobalVariable;
  23. class Function;
  24. class Module;
  25. class Value;
  26. namespace orc {
  27. /// @brief This iterator provides a convenient way to iterate over the elements
  28. /// of an llvm.global_ctors/llvm.global_dtors instance.
  29. ///
  30. /// The easiest way to get hold of instances of this class is to use the
  31. /// getConstructors/getDestructors functions.
  32. class CtorDtorIterator {
  33. public:
  34. /// @brief Accessor for an element of the global_ctors/global_dtors array.
  35. ///
  36. /// This class provides a read-only view of the element with any casts on
  37. /// the function stripped away.
  38. struct Element {
  39. Element(unsigned Priority, const Function *Func, const Value *Data)
  40. : Priority(Priority), Func(Func), Data(Data) {}
  41. unsigned Priority;
  42. const Function *Func;
  43. const Value *Data;
  44. };
  45. /// @brief Construct an iterator instance. If End is true then this iterator
  46. /// acts as the end of the range, otherwise it is the beginning.
  47. CtorDtorIterator(const GlobalVariable *GV, bool End);
  48. /// @brief Test iterators for equality.
  49. bool operator==(const CtorDtorIterator &Other) const;
  50. /// @brief Test iterators for inequality.
  51. bool operator!=(const CtorDtorIterator &Other) const;
  52. /// @brief Pre-increment iterator.
  53. CtorDtorIterator& operator++();
  54. /// @brief Post-increment iterator.
  55. CtorDtorIterator operator++(int);
  56. /// @brief Dereference iterator. The resulting value provides a read-only view
  57. /// of this element of the global_ctors/global_dtors list.
  58. Element operator*() const;
  59. private:
  60. const ConstantArray *InitList;
  61. unsigned I;
  62. };
  63. /// @brief Create an iterator range over the entries of the llvm.global_ctors
  64. /// array.
  65. iterator_range<CtorDtorIterator> getConstructors(const Module &M);
  66. /// @brief Create an iterator range over the entries of the llvm.global_ctors
  67. /// array.
  68. iterator_range<CtorDtorIterator> getDestructors(const Module &M);
  69. /// @brief Convenience class for recording constructor/destructor names for
  70. /// later execution.
  71. template <typename JITLayerT>
  72. class CtorDtorRunner {
  73. public:
  74. /// @brief Construct a CtorDtorRunner for the given range using the given
  75. /// name mangling function.
  76. CtorDtorRunner(std::vector<std::string> CtorDtorNames,
  77. typename JITLayerT::ModuleSetHandleT H)
  78. : CtorDtorNames(std::move(CtorDtorNames)), H(H) {}
  79. /// @brief Run the recorded constructors/destructors through the given JIT
  80. /// layer.
  81. bool runViaLayer(JITLayerT &JITLayer) const {
  82. typedef void (*CtorDtorTy)();
  83. bool Error = false;
  84. for (const auto &CtorDtorName : CtorDtorNames)
  85. if (auto CtorDtorSym = JITLayer.findSymbolIn(H, CtorDtorName, false)) {
  86. CtorDtorTy CtorDtor =
  87. reinterpret_cast<CtorDtorTy>(
  88. static_cast<uintptr_t>(CtorDtorSym.getAddress()));
  89. CtorDtor();
  90. } else
  91. Error = true;
  92. return !Error;
  93. }
  94. private:
  95. std::vector<std::string> CtorDtorNames;
  96. typename JITLayerT::ModuleSetHandleT H;
  97. };
  98. /// @brief Support class for static dtor execution. For hosted (in-process) JITs
  99. /// only!
  100. ///
  101. /// If a __cxa_atexit function isn't found C++ programs that use static
  102. /// destructors will fail to link. However, we don't want to use the host
  103. /// process's __cxa_atexit, because it will schedule JIT'd destructors to run
  104. /// after the JIT has been torn down, which is no good. This class makes it easy
  105. /// to override __cxa_atexit (and the related __dso_handle).
  106. ///
  107. /// To use, clients should manually call searchOverrides from their symbol
  108. /// resolver. This should generally be done after attempting symbol resolution
  109. /// inside the JIT, but before searching the host process's symbol table. When
  110. /// the client determines that destructors should be run (generally at JIT
  111. /// teardown or after a return from main), the runDestructors method should be
  112. /// called.
  113. class LocalCXXRuntimeOverrides {
  114. public:
  115. /// Create a runtime-overrides class.
  116. template <typename MangleFtorT>
  117. LocalCXXRuntimeOverrides(const MangleFtorT &Mangle) {
  118. addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride));
  119. addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride));
  120. }
  121. /// Search overrided symbols.
  122. RuntimeDyld::SymbolInfo searchOverrides(const std::string &Name) {
  123. auto I = CXXRuntimeOverrides.find(Name);
  124. if (I != CXXRuntimeOverrides.end())
  125. return RuntimeDyld::SymbolInfo(I->second, JITSymbolFlags::Exported);
  126. return nullptr;
  127. }
  128. /// Run any destructors recorded by the overriden __cxa_atexit function
  129. /// (CXAAtExitOverride).
  130. void runDestructors();
  131. private:
  132. template <typename PtrTy>
  133. TargetAddress toTargetAddress(PtrTy* P) {
  134. return static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(P));
  135. }
  136. void addOverride(const std::string &Name, TargetAddress Addr) {
  137. CXXRuntimeOverrides.insert(std::make_pair(Name, Addr));
  138. }
  139. StringMap<TargetAddress> CXXRuntimeOverrides;
  140. typedef void (*DestructorPtr)(void*);
  141. typedef std::pair<DestructorPtr, void*> CXXDestructorDataPair;
  142. typedef std::vector<CXXDestructorDataPair> CXXDestructorDataPairList;
  143. CXXDestructorDataPairList DSOHandleOverride;
  144. static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg,
  145. void *DSOHandle);
  146. };
  147. } // End namespace orc.
  148. } // End namespace llvm.
  149. #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H