ExecutionUtils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ExecutionUtils.h - Utilities for executing code in Orc ---*- 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 utilities for executing code in Orc.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
  18. #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/iterator_range.h"
  21. #include "llvm/ExecutionEngine/JITSymbol.h"
  22. #include "llvm/ExecutionEngine/Orc/Core.h"
  23. #include "llvm/ExecutionEngine/Orc/Mangling.h"
  24. #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
  25. #include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
  26. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  27. #include "llvm/Object/Archive.h"
  28. #include "llvm/Support/DynamicLibrary.h"
  29. #include <algorithm>
  30. #include <cstdint>
  31. #include <utility>
  32. #include <vector>
  33. namespace llvm {
  34. class ConstantArray;
  35. class GlobalVariable;
  36. class Function;
  37. class Module;
  38. class Value;
  39. namespace orc {
  40. class ObjectLayer;
  41. /// This iterator provides a convenient way to iterate over the elements
  42. /// of an llvm.global_ctors/llvm.global_dtors instance.
  43. ///
  44. /// The easiest way to get hold of instances of this class is to use the
  45. /// getConstructors/getDestructors functions.
  46. class CtorDtorIterator {
  47. public:
  48. /// Accessor for an element of the global_ctors/global_dtors array.
  49. ///
  50. /// This class provides a read-only view of the element with any casts on
  51. /// the function stripped away.
  52. struct Element {
  53. Element(unsigned Priority, Function *Func, Value *Data)
  54. : Priority(Priority), Func(Func), Data(Data) {}
  55. unsigned Priority;
  56. Function *Func;
  57. Value *Data;
  58. };
  59. /// Construct an iterator instance. If End is true then this iterator
  60. /// acts as the end of the range, otherwise it is the beginning.
  61. CtorDtorIterator(const GlobalVariable *GV, bool End);
  62. /// Test iterators for equality.
  63. bool operator==(const CtorDtorIterator &Other) const;
  64. /// Test iterators for inequality.
  65. bool operator!=(const CtorDtorIterator &Other) const;
  66. /// Pre-increment iterator.
  67. CtorDtorIterator& operator++();
  68. /// Post-increment iterator.
  69. CtorDtorIterator operator++(int);
  70. /// Dereference iterator. The resulting value provides a read-only view
  71. /// of this element of the global_ctors/global_dtors list.
  72. Element operator*() const;
  73. private:
  74. const ConstantArray *InitList;
  75. unsigned I;
  76. };
  77. /// Create an iterator range over the entries of the llvm.global_ctors
  78. /// array.
  79. iterator_range<CtorDtorIterator> getConstructors(const Module &M);
  80. /// Create an iterator range over the entries of the llvm.global_ctors
  81. /// array.
  82. iterator_range<CtorDtorIterator> getDestructors(const Module &M);
  83. /// This iterator provides a convenient way to iterate over GlobalValues that
  84. /// have initialization effects.
  85. class StaticInitGVIterator {
  86. public:
  87. StaticInitGVIterator() = default;
  88. StaticInitGVIterator(Module &M)
  89. : I(M.global_values().begin()), E(M.global_values().end()),
  90. ObjFmt(Triple(M.getTargetTriple()).getObjectFormat()) {
  91. if (I != E) {
  92. if (!isStaticInitGlobal(*I))
  93. moveToNextStaticInitGlobal();
  94. } else
  95. I = E = Module::global_value_iterator();
  96. }
  97. bool operator==(const StaticInitGVIterator &O) const { return I == O.I; }
  98. bool operator!=(const StaticInitGVIterator &O) const { return I != O.I; }
  99. StaticInitGVIterator &operator++() {
  100. assert(I != E && "Increment past end of range");
  101. moveToNextStaticInitGlobal();
  102. return *this;
  103. }
  104. GlobalValue &operator*() { return *I; }
  105. private:
  106. bool isStaticInitGlobal(GlobalValue &GV);
  107. void moveToNextStaticInitGlobal() {
  108. ++I;
  109. while (I != E && !isStaticInitGlobal(*I))
  110. ++I;
  111. if (I == E)
  112. I = E = Module::global_value_iterator();
  113. }
  114. Module::global_value_iterator I, E;
  115. Triple::ObjectFormatType ObjFmt;
  116. };
  117. /// Create an iterator range over the GlobalValues that contribute to static
  118. /// initialization.
  119. inline iterator_range<StaticInitGVIterator> getStaticInitGVs(Module &M) {
  120. return make_range(StaticInitGVIterator(M), StaticInitGVIterator());
  121. }
  122. class CtorDtorRunner {
  123. public:
  124. CtorDtorRunner(JITDylib &JD) : JD(JD) {}
  125. void add(iterator_range<CtorDtorIterator> CtorDtors);
  126. Error run();
  127. private:
  128. using CtorDtorList = std::vector<SymbolStringPtr>;
  129. using CtorDtorPriorityMap = std::map<unsigned, CtorDtorList>;
  130. JITDylib &JD;
  131. CtorDtorPriorityMap CtorDtorsByPriority;
  132. };
  133. /// Support class for static dtor execution. For hosted (in-process) JITs
  134. /// only!
  135. ///
  136. /// If a __cxa_atexit function isn't found C++ programs that use static
  137. /// destructors will fail to link. However, we don't want to use the host
  138. /// process's __cxa_atexit, because it will schedule JIT'd destructors to run
  139. /// after the JIT has been torn down, which is no good. This class makes it easy
  140. /// to override __cxa_atexit (and the related __dso_handle).
  141. ///
  142. /// To use, clients should manually call searchOverrides from their symbol
  143. /// resolver. This should generally be done after attempting symbol resolution
  144. /// inside the JIT, but before searching the host process's symbol table. When
  145. /// the client determines that destructors should be run (generally at JIT
  146. /// teardown or after a return from main), the runDestructors method should be
  147. /// called.
  148. class LocalCXXRuntimeOverridesBase {
  149. public:
  150. /// Run any destructors recorded by the overriden __cxa_atexit function
  151. /// (CXAAtExitOverride).
  152. void runDestructors();
  153. protected:
  154. template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy *P) {
  155. return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P));
  156. }
  157. using DestructorPtr = void (*)(void *);
  158. using CXXDestructorDataPair = std::pair<DestructorPtr, void *>;
  159. using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>;
  160. CXXDestructorDataPairList DSOHandleOverride;
  161. static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg,
  162. void *DSOHandle);
  163. };
  164. class LocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase {
  165. public:
  166. Error enable(JITDylib &JD, MangleAndInterner &Mangler);
  167. };
  168. /// An interface for Itanium __cxa_atexit interposer implementations.
  169. class ItaniumCXAAtExitSupport {
  170. public:
  171. struct AtExitRecord {
  172. void (*F)(void *);
  173. void *Ctx;
  174. };
  175. void registerAtExit(void (*F)(void *), void *Ctx, void *DSOHandle);
  176. void runAtExits(void *DSOHandle);
  177. private:
  178. std::mutex AtExitsMutex;
  179. DenseMap<void *, std::vector<AtExitRecord>> AtExitRecords;
  180. };
  181. /// A utility class to expose symbols found via dlsym to the JIT.
  182. ///
  183. /// If an instance of this class is attached to a JITDylib as a fallback
  184. /// definition generator, then any symbol found in the given DynamicLibrary that
  185. /// passes the 'Allow' predicate will be added to the JITDylib.
  186. class DynamicLibrarySearchGenerator : public DefinitionGenerator {
  187. public:
  188. using SymbolPredicate = std::function<bool(const SymbolStringPtr &)>;
  189. /// Create a DynamicLibrarySearchGenerator that searches for symbols in the
  190. /// given sys::DynamicLibrary.
  191. ///
  192. /// If the Allow predicate is given then only symbols matching the predicate
  193. /// will be searched for. If the predicate is not given then all symbols will
  194. /// be searched for.
  195. DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, char GlobalPrefix,
  196. SymbolPredicate Allow = SymbolPredicate());
  197. /// Permanently loads the library at the given path and, on success, returns
  198. /// a DynamicLibrarySearchGenerator that will search it for symbol definitions
  199. /// in the library. On failure returns the reason the library failed to load.
  200. static Expected<std::unique_ptr<DynamicLibrarySearchGenerator>>
  201. Load(const char *FileName, char GlobalPrefix,
  202. SymbolPredicate Allow = SymbolPredicate());
  203. /// Creates a DynamicLibrarySearchGenerator that searches for symbols in
  204. /// the current process.
  205. static Expected<std::unique_ptr<DynamicLibrarySearchGenerator>>
  206. GetForCurrentProcess(char GlobalPrefix,
  207. SymbolPredicate Allow = SymbolPredicate()) {
  208. return Load(nullptr, GlobalPrefix, std::move(Allow));
  209. }
  210. Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
  211. JITDylibLookupFlags JDLookupFlags,
  212. const SymbolLookupSet &Symbols) override;
  213. private:
  214. sys::DynamicLibrary Dylib;
  215. SymbolPredicate Allow;
  216. char GlobalPrefix;
  217. };
  218. /// A utility class to expose symbols from a static library.
  219. ///
  220. /// If an instance of this class is attached to a JITDylib as a fallback
  221. /// definition generator, then any symbol found in the archive will result in
  222. /// the containing object being added to the JITDylib.
  223. class StaticLibraryDefinitionGenerator : public DefinitionGenerator {
  224. public:
  225. // Interface builder function for objects loaded from this archive.
  226. using GetObjectFileInterface =
  227. unique_function<Expected<MaterializationUnit::Interface>(
  228. ExecutionSession &ES, MemoryBufferRef ObjBuffer)>;
  229. /// Try to create a StaticLibraryDefinitionGenerator from the given path.
  230. ///
  231. /// This call will succeed if the file at the given path is a static library
  232. /// is a valid archive, otherwise it will return an error.
  233. static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
  234. Load(ObjectLayer &L, const char *FileName,
  235. GetObjectFileInterface GetObjFileInterface = GetObjectFileInterface());
  236. /// Try to create a StaticLibraryDefinitionGenerator from the given path.
  237. ///
  238. /// This call will succeed if the file at the given path is a static library
  239. /// or a MachO universal binary containing a static library that is compatible
  240. /// with the given triple. Otherwise it will return an error.
  241. static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
  242. Load(ObjectLayer &L, const char *FileName, const Triple &TT,
  243. GetObjectFileInterface GetObjFileInterface = GetObjectFileInterface());
  244. /// Try to create a StaticLibrarySearchGenerator from the given memory buffer.
  245. /// This call will succeed if the buffer contains a valid archive, otherwise
  246. /// it will return an error.
  247. static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
  248. Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer,
  249. GetObjectFileInterface GetObjFileInterface = GetObjectFileInterface());
  250. /// Returns a list of filenames of dynamic libraries that this archive has
  251. /// imported. This class does not load these libraries by itself. User is
  252. /// responsible for making sure these libraries are avaliable to the JITDylib.
  253. const std::set<std::string> &getImportedDynamicLibraries() const {
  254. return ImportedDynamicLibraries;
  255. }
  256. Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
  257. JITDylibLookupFlags JDLookupFlags,
  258. const SymbolLookupSet &Symbols) override;
  259. private:
  260. StaticLibraryDefinitionGenerator(ObjectLayer &L,
  261. std::unique_ptr<MemoryBuffer> ArchiveBuffer,
  262. GetObjectFileInterface GetObjFileInterface,
  263. Error &Err);
  264. Error buildObjectFilesMap();
  265. ObjectLayer &L;
  266. GetObjectFileInterface GetObjFileInterface;
  267. std::set<std::string> ImportedDynamicLibraries;
  268. std::unique_ptr<MemoryBuffer> ArchiveBuffer;
  269. std::unique_ptr<object::Archive> Archive;
  270. DenseMap<SymbolStringPtr, MemoryBufferRef> ObjectFilesMap;
  271. };
  272. /// A utility class to create COFF dllimport GOT symbols (__imp_*) and PLT
  273. /// stubs.
  274. ///
  275. /// If an instance of this class is attached to a JITDylib as a fallback
  276. /// definition generator, PLT stubs and dllimport __imp_ symbols will be
  277. /// generated for external symbols found outside the given jitdylib. Currently
  278. /// only supports x86_64 architecture.
  279. class DLLImportDefinitionGenerator : public DefinitionGenerator {
  280. public:
  281. /// Creates a DLLImportDefinitionGenerator instance.
  282. static std::unique_ptr<DLLImportDefinitionGenerator>
  283. Create(ExecutionSession &ES, ObjectLinkingLayer &L);
  284. Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
  285. JITDylibLookupFlags JDLookupFlags,
  286. const SymbolLookupSet &Symbols) override;
  287. private:
  288. DLLImportDefinitionGenerator(ExecutionSession &ES, ObjectLinkingLayer &L)
  289. : ES(ES), L(L) {}
  290. static Expected<unsigned> getTargetPointerSize(const Triple &TT);
  291. static Expected<support::endianness> getTargetEndianness(const Triple &TT);
  292. Expected<std::unique_ptr<jitlink::LinkGraph>>
  293. createStubsGraph(const SymbolMap &Resolved);
  294. static StringRef getImpPrefix() { return "__imp_"; }
  295. static StringRef getSectionName() { return "$__DLLIMPORT_STUBS"; }
  296. ExecutionSession &ES;
  297. ObjectLinkingLayer &L;
  298. };
  299. } // end namespace orc
  300. } // end namespace llvm
  301. #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
  302. #ifdef __GNUC__
  303. #pragma GCC diagnostic pop
  304. #endif