1 //===- ExecutionUtils.h - Utilities for executing code in Orc ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains utilities for executing code in Orc.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/ExecutionEngine/JITSymbol.h"
20 #include "llvm/ExecutionEngine/Orc/Core.h"
21 #include "llvm/ExecutionEngine/Orc/OrcError.h"
22 #include "llvm/ExecutionEngine/RuntimeDyld.h"
23 #include "llvm/Support/DynamicLibrary.h"
41 /// This iterator provides a convenient way to iterate over the elements
42 /// of an llvm.global_ctors/llvm.global_dtors instance.
44 /// The easiest way to get hold of instances of this class is to use the
45 /// getConstructors/getDestructors functions.
46 class CtorDtorIterator {
48 /// Accessor for an element of the global_ctors/global_dtors array.
50 /// This class provides a read-only view of the element with any casts on
51 /// the function stripped away.
53 Element(unsigned Priority, Function *Func, Value *Data)
54 : Priority(Priority), Func(Func), Data(Data) {}
61 /// Construct an iterator instance. If End is true then this iterator
62 /// acts as the end of the range, otherwise it is the beginning.
63 CtorDtorIterator(const GlobalVariable *GV, bool End);
65 /// Test iterators for equality.
66 bool operator==(const CtorDtorIterator &Other) const;
68 /// Test iterators for inequality.
69 bool operator!=(const CtorDtorIterator &Other) const;
71 /// Pre-increment iterator.
72 CtorDtorIterator& operator++();
74 /// Post-increment iterator.
75 CtorDtorIterator operator++(int);
77 /// Dereference iterator. The resulting value provides a read-only view
78 /// of this element of the global_ctors/global_dtors list.
79 Element operator*() const;
82 const ConstantArray *InitList;
86 /// Create an iterator range over the entries of the llvm.global_ctors
88 iterator_range<CtorDtorIterator> getConstructors(const Module &M);
90 /// Create an iterator range over the entries of the llvm.global_ctors
92 iterator_range<CtorDtorIterator> getDestructors(const Module &M);
94 /// Convenience class for recording constructor/destructor names for
96 template <typename JITLayerT>
97 class LegacyCtorDtorRunner {
99 /// Construct a CtorDtorRunner for the given range using the given
100 /// name mangling function.
101 LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames, VModuleKey K)
102 : CtorDtorNames(std::move(CtorDtorNames)), K(K) {}
104 /// Run the recorded constructors/destructors through the given JIT
106 Error runViaLayer(JITLayerT &JITLayer) const {
107 using CtorDtorTy = void (*)();
109 for (const auto &CtorDtorName : CtorDtorNames) {
110 if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) {
111 if (auto AddrOrErr = CtorDtorSym.getAddress()) {
112 CtorDtorTy CtorDtor =
113 reinterpret_cast<CtorDtorTy>(static_cast<uintptr_t>(*AddrOrErr));
116 return AddrOrErr.takeError();
118 if (auto Err = CtorDtorSym.takeError())
121 return make_error<JITSymbolNotFound>(CtorDtorName);
124 return Error::success();
128 std::vector<std::string> CtorDtorNames;
132 class CtorDtorRunner {
134 CtorDtorRunner(JITDylib &JD) : JD(JD) {}
135 void add(iterator_range<CtorDtorIterator> CtorDtors);
139 using CtorDtorList = std::vector<SymbolStringPtr>;
140 using CtorDtorPriorityMap = std::map<unsigned, CtorDtorList>;
143 CtorDtorPriorityMap CtorDtorsByPriority;
146 /// Support class for static dtor execution. For hosted (in-process) JITs
149 /// If a __cxa_atexit function isn't found C++ programs that use static
150 /// destructors will fail to link. However, we don't want to use the host
151 /// process's __cxa_atexit, because it will schedule JIT'd destructors to run
152 /// after the JIT has been torn down, which is no good. This class makes it easy
153 /// to override __cxa_atexit (and the related __dso_handle).
155 /// To use, clients should manually call searchOverrides from their symbol
156 /// resolver. This should generally be done after attempting symbol resolution
157 /// inside the JIT, but before searching the host process's symbol table. When
158 /// the client determines that destructors should be run (generally at JIT
159 /// teardown or after a return from main), the runDestructors method should be
161 class LocalCXXRuntimeOverridesBase {
163 /// Run any destructors recorded by the overriden __cxa_atexit function
164 /// (CXAAtExitOverride).
165 void runDestructors();
168 template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy *P) {
169 return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P));
172 using DestructorPtr = void (*)(void *);
173 using CXXDestructorDataPair = std::pair<DestructorPtr, void *>;
174 using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>;
175 CXXDestructorDataPairList DSOHandleOverride;
176 static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg,
180 class LegacyLocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase {
182 /// Create a runtime-overrides class.
183 template <typename MangleFtorT>
184 LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle) {
185 addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride));
186 addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride));
189 /// Search overrided symbols.
190 JITEvaluatedSymbol searchOverrides(const std::string &Name) {
191 auto I = CXXRuntimeOverrides.find(Name);
192 if (I != CXXRuntimeOverrides.end())
193 return JITEvaluatedSymbol(I->second, JITSymbolFlags::Exported);
198 void addOverride(const std::string &Name, JITTargetAddress Addr) {
199 CXXRuntimeOverrides.insert(std::make_pair(Name, Addr));
202 StringMap<JITTargetAddress> CXXRuntimeOverrides;
205 class LocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase {
207 Error enable(JITDylib &JD, MangleAndInterner &Mangler);
210 /// A utility class to expose symbols found via dlsym to the JIT.
212 /// If an instance of this class is attached to a JITDylib as a fallback
213 /// definition generator, then any symbol found in the given DynamicLibrary that
214 /// passes the 'Allow' predicate will be added to the JITDylib.
215 class DynamicLibrarySearchGenerator {
217 using SymbolPredicate = std::function<bool(SymbolStringPtr)>;
219 /// Create a DynamicLibrarySearchGenerator that searches for symbols in the
220 /// given sys::DynamicLibrary.
221 /// If the Allow predicate is given then only symbols matching the predicate
222 /// will be searched for in the DynamicLibrary. If the predicate is not given
223 /// then all symbols will be searched for.
224 DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, const DataLayout &DL,
225 SymbolPredicate Allow = SymbolPredicate());
227 /// Permanently loads the library at the given path and, on success, returns
228 /// a DynamicLibrarySearchGenerator that will search it for symbol definitions
229 /// in the library. On failure returns the reason the library failed to load.
230 static Expected<DynamicLibrarySearchGenerator>
231 Load(const char *FileName, const DataLayout &DL,
232 SymbolPredicate Allow = SymbolPredicate());
234 /// Creates a DynamicLibrarySearchGenerator that searches for symbols in
235 /// the current process.
236 static Expected<DynamicLibrarySearchGenerator>
237 GetForCurrentProcess(const DataLayout &DL,
238 SymbolPredicate Allow = SymbolPredicate()) {
239 return Load(nullptr, DL, std::move(Allow));
242 SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names);
245 sys::DynamicLibrary Dylib;
246 SymbolPredicate Allow;
250 } // end namespace orc
251 } // end namespace llvm
253 #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H