xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp (revision 162ae9c834f6d9f9cb443bd62cceb23e0b5fef48)
1 //===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
10 
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/GlobalVariable.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Target/TargetMachine.h"
17 
18 namespace llvm {
19 namespace orc {
20 
21 CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
22   : InitList(
23       GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
24     I((InitList && End) ? InitList->getNumOperands() : 0) {
25 }
26 
27 bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const {
28   assert(InitList == Other.InitList && "Incomparable iterators.");
29   return I == Other.I;
30 }
31 
32 bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const {
33   return !(*this == Other);
34 }
35 
36 CtorDtorIterator& CtorDtorIterator::operator++() {
37   ++I;
38   return *this;
39 }
40 
41 CtorDtorIterator CtorDtorIterator::operator++(int) {
42   CtorDtorIterator Temp = *this;
43   ++I;
44   return Temp;
45 }
46 
47 CtorDtorIterator::Element CtorDtorIterator::operator*() const {
48   ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
49   assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors");
50 
51   Constant *FuncC = CS->getOperand(1);
52   Function *Func = nullptr;
53 
54   // Extract function pointer, pulling off any casts.
55   while (FuncC) {
56     if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
57       Func = F;
58       break;
59     } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
60       if (CE->isCast())
61         FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
62       else
63         break;
64     } else {
65       // This isn't anything we recognize. Bail out with Func left set to null.
66       break;
67     }
68   }
69 
70   ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
71   Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
72   if (Data && !isa<GlobalValue>(Data))
73     Data = nullptr;
74   return Element(Priority->getZExtValue(), Func, Data);
75 }
76 
77 iterator_range<CtorDtorIterator> getConstructors(const Module &M) {
78   const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
79   return make_range(CtorDtorIterator(CtorsList, false),
80                     CtorDtorIterator(CtorsList, true));
81 }
82 
83 iterator_range<CtorDtorIterator> getDestructors(const Module &M) {
84   const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
85   return make_range(CtorDtorIterator(DtorsList, false),
86                     CtorDtorIterator(DtorsList, true));
87 }
88 
89 void CtorDtorRunner::add(iterator_range<CtorDtorIterator> CtorDtors) {
90   if (empty(CtorDtors))
91     return;
92 
93   MangleAndInterner Mangle(
94       JD.getExecutionSession(),
95       (*CtorDtors.begin()).Func->getParent()->getDataLayout());
96 
97   for (const auto &CtorDtor : CtorDtors) {
98     assert(CtorDtor.Func && CtorDtor.Func->hasName() &&
99            "Ctor/Dtor function must be named to be runnable under the JIT");
100 
101     // FIXME: Maybe use a symbol promoter here instead.
102     if (CtorDtor.Func->hasLocalLinkage()) {
103       CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage);
104       CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility);
105     }
106 
107     if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
108       dbgs() << "  Skipping because why now?\n";
109       continue;
110     }
111 
112     CtorDtorsByPriority[CtorDtor.Priority].push_back(
113         Mangle(CtorDtor.Func->getName()));
114   }
115 }
116 
117 Error CtorDtorRunner::run() {
118   using CtorDtorTy = void (*)();
119 
120   SymbolNameSet Names;
121 
122   for (auto &KV : CtorDtorsByPriority) {
123     for (auto &Name : KV.second) {
124       auto Added = Names.insert(Name).second;
125       (void)Added;
126       assert(Added && "Ctor/Dtor names clashed");
127     }
128   }
129 
130   auto &ES = JD.getExecutionSession();
131   if (auto CtorDtorMap =
132           ES.lookup(JITDylibSearchList({{&JD, true}}), std::move(Names))) {
133     for (auto &KV : CtorDtorsByPriority) {
134       for (auto &Name : KV.second) {
135         assert(CtorDtorMap->count(Name) && "No entry for Name");
136         auto CtorDtor = reinterpret_cast<CtorDtorTy>(
137             static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
138         CtorDtor();
139       }
140     }
141     CtorDtorsByPriority.clear();
142     return Error::success();
143   } else
144     return CtorDtorMap.takeError();
145 }
146 
147 void LocalCXXRuntimeOverridesBase::runDestructors() {
148   auto& CXXDestructorDataPairs = DSOHandleOverride;
149   for (auto &P : CXXDestructorDataPairs)
150     P.first(P.second);
151   CXXDestructorDataPairs.clear();
152 }
153 
154 int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
155                                                     void *Arg,
156                                                     void *DSOHandle) {
157   auto& CXXDestructorDataPairs =
158     *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
159   CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
160   return 0;
161 }
162 
163 Error LocalCXXRuntimeOverrides::enable(JITDylib &JD,
164                                         MangleAndInterner &Mangle) {
165   SymbolMap RuntimeInterposes;
166   RuntimeInterposes[Mangle("__dso_handle")] =
167     JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
168                        JITSymbolFlags::Exported);
169   RuntimeInterposes[Mangle("__cxa_atexit")] =
170     JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
171                        JITSymbolFlags::Exported);
172 
173   return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
174 }
175 
176 DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
177     sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow)
178     : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
179       GlobalPrefix(GlobalPrefix) {}
180 
181 Expected<DynamicLibrarySearchGenerator>
182 DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix,
183                                     SymbolPredicate Allow) {
184   std::string ErrMsg;
185   auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
186   if (!Lib.isValid())
187     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
188   return DynamicLibrarySearchGenerator(std::move(Lib), GlobalPrefix,
189                                        std::move(Allow));
190 }
191 
192 Expected<SymbolNameSet>
193 DynamicLibrarySearchGenerator::operator()(JITDylib &JD,
194                                           const SymbolNameSet &Names) {
195   orc::SymbolNameSet Added;
196   orc::SymbolMap NewSymbols;
197 
198   bool HasGlobalPrefix = (GlobalPrefix != '\0');
199 
200   for (auto &Name : Names) {
201     if ((*Name).empty())
202       continue;
203 
204     if (Allow && !Allow(Name))
205       continue;
206 
207     if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
208       continue;
209 
210     std::string Tmp((*Name).data() + HasGlobalPrefix,
211                     (*Name).size() - HasGlobalPrefix);
212     if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
213       Added.insert(Name);
214       NewSymbols[Name] = JITEvaluatedSymbol(
215           static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
216           JITSymbolFlags::Exported);
217     }
218   }
219 
220   // Add any new symbols to JD. Since the generator is only called for symbols
221   // that are not already defined, this will never trigger a duplicate
222   // definition error, so we can wrap this call in a 'cantFail'.
223   if (!NewSymbols.empty())
224     cantFail(JD.define(absoluteSymbols(std::move(NewSymbols))));
225 
226   return Added;
227 }
228 
229 } // End namespace orc.
230 } // End namespace llvm.
231