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