blob: 52250662a9545a91ed73ec77226f6ffebb5aa53e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
14#ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
15#define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
16
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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010023#include "llvm/Support/DynamicLibrary.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024#include <algorithm>
25#include <cstdint>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace llvm {
31
32class ConstantArray;
33class GlobalVariable;
34class Function;
35class Module;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010036class TargetMachine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010037class Value;
38
39namespace orc {
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041/// This iterator provides a convenient way to iterate over the elements
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042/// 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.
46class CtorDtorIterator {
47public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048 /// Accessor for an element of the global_ctors/global_dtors array.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049 ///
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
56 unsigned Priority;
57 Function *Func;
58 Value *Data;
59 };
60
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061 /// Construct an iterator instance. If End is true then this iterator
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062 /// acts as the end of the range, otherwise it is the beginning.
63 CtorDtorIterator(const GlobalVariable *GV, bool End);
64
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065 /// Test iterators for equality.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 bool operator==(const CtorDtorIterator &Other) const;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Test iterators for inequality.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 bool operator!=(const CtorDtorIterator &Other) const;
70
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Pre-increment iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 CtorDtorIterator& operator++();
73
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// Post-increment iterator.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 CtorDtorIterator operator++(int);
76
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 /// Dereference iterator. The resulting value provides a read-only view
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 /// of this element of the global_ctors/global_dtors list.
79 Element operator*() const;
80
81private:
82 const ConstantArray *InitList;
83 unsigned I;
84};
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086/// Create an iterator range over the entries of the llvm.global_ctors
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087/// array.
88iterator_range<CtorDtorIterator> getConstructors(const Module &M);
89
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090/// Create an iterator range over the entries of the llvm.global_ctors
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091/// array.
92iterator_range<CtorDtorIterator> getDestructors(const Module &M);
93
Andrew Scullcdfcccc2018-10-05 20:58:37 +010094/// Convenience class for recording constructor/destructor names for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010095/// later execution.
96template <typename JITLayerT>
97class CtorDtorRunner {
98public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 /// Construct a CtorDtorRunner for the given range using the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100100 /// name mangling function.
101 CtorDtorRunner(std::vector<std::string> CtorDtorNames, VModuleKey K)
102 : CtorDtorNames(std::move(CtorDtorNames)), K(K) {}
103
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100104 /// Run the recorded constructors/destructors through the given JIT
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100105 /// layer.
106 Error runViaLayer(JITLayerT &JITLayer) const {
107 using CtorDtorTy = void (*)();
108
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));
114 CtorDtor();
115 } else
116 return AddrOrErr.takeError();
117 } else {
118 if (auto Err = CtorDtorSym.takeError())
119 return Err;
120 else
121 return make_error<JITSymbolNotFound>(CtorDtorName);
122 }
123 }
124 return Error::success();
125 }
126
127private:
128 std::vector<std::string> CtorDtorNames;
129 orc::VModuleKey K;
130};
131
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100132class CtorDtorRunner2 {
133public:
Andrew Scull0372a572018-11-16 15:47:06 +0000134 CtorDtorRunner2(JITDylib &JD) : JD(JD) {}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100135 void add(iterator_range<CtorDtorIterator> CtorDtors);
136 Error run();
137
138private:
139 using CtorDtorList = std::vector<SymbolStringPtr>;
140 using CtorDtorPriorityMap = std::map<unsigned, CtorDtorList>;
141
Andrew Scull0372a572018-11-16 15:47:06 +0000142 JITDylib &JD;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100143 CtorDtorPriorityMap CtorDtorsByPriority;
144};
145
146/// Support class for static dtor execution. For hosted (in-process) JITs
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147/// only!
148///
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).
154///
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
160/// called.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100161class LocalCXXRuntimeOverridesBase {
162public:
163 /// Run any destructors recorded by the overriden __cxa_atexit function
164 /// (CXAAtExitOverride).
165 void runDestructors();
166
167protected:
168 template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy *P) {
169 return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P));
170 }
171
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,
177 void *DSOHandle);
178};
179
180class LocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100181public:
182 /// Create a runtime-overrides class.
183 template <typename MangleFtorT>
184 LocalCXXRuntimeOverrides(const MangleFtorT &Mangle) {
185 addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride));
186 addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride));
187 }
188
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);
194 return nullptr;
195 }
196
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100197private:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100198 void addOverride(const std::string &Name, JITTargetAddress Addr) {
199 CXXRuntimeOverrides.insert(std::make_pair(Name, Addr));
200 }
201
202 StringMap<JITTargetAddress> CXXRuntimeOverrides;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100203};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100204
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100205class LocalCXXRuntimeOverrides2 : public LocalCXXRuntimeOverridesBase {
206public:
Andrew Scull0372a572018-11-16 15:47:06 +0000207 Error enable(JITDylib &JD, MangleAndInterner &Mangler);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100208};
209
210/// A utility class to expose symbols found via dlsym to the JIT.
211///
Andrew Scull0372a572018-11-16 15:47:06 +0000212/// 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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100215class DynamicLibraryFallbackGenerator {
216public:
217 using SymbolPredicate = std::function<bool(SymbolStringPtr)>;
Andrew Scull0372a572018-11-16 15:47:06 +0000218
219 static bool AllowAll(SymbolStringPtr Name) { return true; }
220
221 /// Create a DynamicLibraryFallbackGenerator that searches for symbols in the
222 /// given sys::DynamicLibrary.
223 /// Only symbols that match the 'Allow' predicate will be searched for.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100224 DynamicLibraryFallbackGenerator(sys::DynamicLibrary Dylib,
Andrew Scull0372a572018-11-16 15:47:06 +0000225 const DataLayout &DL,
226 SymbolPredicate Allow = AllowAll);
227
228 /// Permanently loads the library at the given path and, on success, returns
229 /// a DynamicLibraryFallbackGenerator that will search it for symbol
230 /// definitions matching the Allow predicate.
231 /// On failure returns the reason the library failed to load.
232 static Expected<DynamicLibraryFallbackGenerator>
233 Load(const char *FileName, const DataLayout &DL,
234 SymbolPredicate Allow = AllowAll);
235
236 /// Creates a DynamicLibraryFallbackGenerator that searches for symbols in
237 /// the current process.
238 static Expected<DynamicLibraryFallbackGenerator>
239 CreateForCurrentProcess(const DataLayout &DL,
240 SymbolPredicate Allow = AllowAll) {
241 return Load(nullptr, DL, std::move(Allow));
242 }
243
244 SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100245
246private:
247 sys::DynamicLibrary Dylib;
248 SymbolPredicate Allow;
249 char GlobalPrefix;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100250};
251
252} // end namespace orc
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253} // end namespace llvm
254
255#endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H