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                     NoDependenciesToRegister, true)) {
134     for (auto &KV : CtorDtorsByPriority) {
135       for (auto &Name : KV.second) {
136         assert(CtorDtorMap->count(Name) && "No entry for Name");
137         auto CtorDtor = reinterpret_cast<CtorDtorTy>(
138             static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
139         CtorDtor();
140       }
141     }
142     CtorDtorsByPriority.clear();
143     return Error::success();
144   } else
145     return CtorDtorMap.takeError();
146 }
147 
148 void LocalCXXRuntimeOverridesBase::runDestructors() {
149   auto& CXXDestructorDataPairs = DSOHandleOverride;
150   for (auto &P : CXXDestructorDataPairs)
151     P.first(P.second);
152   CXXDestructorDataPairs.clear();
153 }
154 
155 int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
156                                                     void *Arg,
157                                                     void *DSOHandle) {
158   auto& CXXDestructorDataPairs =
159     *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
160   CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
161   return 0;
162 }
163 
164 Error LocalCXXRuntimeOverrides::enable(JITDylib &JD,
165                                         MangleAndInterner &Mangle) {
166   SymbolMap RuntimeInterposes;
167   RuntimeInterposes[Mangle("__dso_handle")] =
168     JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
169                        JITSymbolFlags::Exported);
170   RuntimeInterposes[Mangle("__cxa_atexit")] =
171     JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
172                        JITSymbolFlags::Exported);
173 
174   return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
175 }
176 
177 DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
178     sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow)
179     : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
180       GlobalPrefix(GlobalPrefix) {}
181 
182 Expected<DynamicLibrarySearchGenerator>
183 DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix,
184                                     SymbolPredicate Allow) {
185   std::string ErrMsg;
186   auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
187   if (!Lib.isValid())
188     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
189   return DynamicLibrarySearchGenerator(std::move(Lib), GlobalPrefix,
190                                        std::move(Allow));
191 }
192 
193 Expected<SymbolNameSet>
194 DynamicLibrarySearchGenerator::operator()(JITDylib &JD,
195                                           const SymbolNameSet &Names) {
196   orc::SymbolNameSet Added;
197   orc::SymbolMap NewSymbols;
198 
199   bool HasGlobalPrefix = (GlobalPrefix != '\0');
200 
201   for (auto &Name : Names) {
202     if ((*Name).empty())
203       continue;
204 
205     if (Allow && !Allow(Name))
206       continue;
207 
208     if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
209       continue;
210 
211     std::string Tmp((*Name).data() + HasGlobalPrefix,
212                     (*Name).size() - HasGlobalPrefix);
213     if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
214       Added.insert(Name);
215       NewSymbols[Name] = JITEvaluatedSymbol(
216           static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
217           JITSymbolFlags::Exported);
218     }
219   }
220 
221   // Add any new symbols to JD. Since the generator is only called for symbols
222   // that are not already defined, this will never trigger a duplicate
223   // definition error, so we can wrap this call in a 'cantFail'.
224   if (!NewSymbols.empty())
225     cantFail(JD.define(absoluteSymbols(std::move(NewSymbols))));
226 
227   return Added;
228 }
229 
230 } // End namespace orc.
231 } // End namespace llvm.
232