10b57cec5SDimitry Andric //===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 100b57cec5SDimitry Andric 118bcb0991SDimitry Andric #include "llvm/ExecutionEngine/Orc/Layer.h" 120b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 130b57cec5SDimitry Andric #include "llvm/IR/Function.h" 140b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 150b57cec5SDimitry Andric #include "llvm/IR/Module.h" 16*5ffd83dbSDimitry Andric #include "llvm/Object/MachOUniversal.h" 17*5ffd83dbSDimitry Andric #include "llvm/Support/FormatVariadic.h" 180b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 190b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric namespace llvm { 220b57cec5SDimitry Andric namespace orc { 230b57cec5SDimitry Andric 24480093f4SDimitry Andric int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args, 25480093f4SDimitry Andric Optional<StringRef> ProgramName) { 26480093f4SDimitry Andric std::vector<std::unique_ptr<char[]>> ArgVStorage; 27480093f4SDimitry Andric std::vector<char *> ArgV; 28480093f4SDimitry Andric 29480093f4SDimitry Andric ArgVStorage.reserve(Args.size() + (ProgramName ? 1 : 0)); 30480093f4SDimitry Andric ArgV.reserve(Args.size() + 1 + (ProgramName ? 1 : 0)); 31480093f4SDimitry Andric 32480093f4SDimitry Andric if (ProgramName) { 33480093f4SDimitry Andric ArgVStorage.push_back(std::make_unique<char[]>(ProgramName->size() + 1)); 34480093f4SDimitry Andric llvm::copy(*ProgramName, &ArgVStorage.back()[0]); 35480093f4SDimitry Andric ArgVStorage.back()[ProgramName->size()] = '\0'; 36480093f4SDimitry Andric ArgV.push_back(ArgVStorage.back().get()); 37480093f4SDimitry Andric } 38480093f4SDimitry Andric 39480093f4SDimitry Andric for (auto &Arg : Args) { 40480093f4SDimitry Andric ArgVStorage.push_back(std::make_unique<char[]>(Arg.size() + 1)); 41480093f4SDimitry Andric llvm::copy(Arg, &ArgVStorage.back()[0]); 42480093f4SDimitry Andric ArgVStorage.back()[Arg.size()] = '\0'; 43480093f4SDimitry Andric ArgV.push_back(ArgVStorage.back().get()); 44480093f4SDimitry Andric } 45480093f4SDimitry Andric ArgV.push_back(nullptr); 46480093f4SDimitry Andric 47480093f4SDimitry Andric return Main(Args.size() + !!ProgramName, ArgV.data()); 48480093f4SDimitry Andric } 49480093f4SDimitry Andric 500b57cec5SDimitry Andric CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End) 510b57cec5SDimitry Andric : InitList( 520b57cec5SDimitry Andric GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr), 530b57cec5SDimitry Andric I((InitList && End) ? InitList->getNumOperands() : 0) { 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const { 570b57cec5SDimitry Andric assert(InitList == Other.InitList && "Incomparable iterators."); 580b57cec5SDimitry Andric return I == Other.I; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const { 620b57cec5SDimitry Andric return !(*this == Other); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric CtorDtorIterator& CtorDtorIterator::operator++() { 660b57cec5SDimitry Andric ++I; 670b57cec5SDimitry Andric return *this; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric CtorDtorIterator CtorDtorIterator::operator++(int) { 710b57cec5SDimitry Andric CtorDtorIterator Temp = *this; 720b57cec5SDimitry Andric ++I; 730b57cec5SDimitry Andric return Temp; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric CtorDtorIterator::Element CtorDtorIterator::operator*() const { 770b57cec5SDimitry Andric ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I)); 780b57cec5SDimitry Andric assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors"); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric Constant *FuncC = CS->getOperand(1); 810b57cec5SDimitry Andric Function *Func = nullptr; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Extract function pointer, pulling off any casts. 840b57cec5SDimitry Andric while (FuncC) { 850b57cec5SDimitry Andric if (Function *F = dyn_cast_or_null<Function>(FuncC)) { 860b57cec5SDimitry Andric Func = F; 870b57cec5SDimitry Andric break; 880b57cec5SDimitry Andric } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) { 890b57cec5SDimitry Andric if (CE->isCast()) 900b57cec5SDimitry Andric FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0)); 910b57cec5SDimitry Andric else 920b57cec5SDimitry Andric break; 930b57cec5SDimitry Andric } else { 940b57cec5SDimitry Andric // This isn't anything we recognize. Bail out with Func left set to null. 950b57cec5SDimitry Andric break; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric 998bcb0991SDimitry Andric auto *Priority = cast<ConstantInt>(CS->getOperand(0)); 1000b57cec5SDimitry Andric Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr; 1010b57cec5SDimitry Andric if (Data && !isa<GlobalValue>(Data)) 1020b57cec5SDimitry Andric Data = nullptr; 1030b57cec5SDimitry Andric return Element(Priority->getZExtValue(), Func, Data); 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric iterator_range<CtorDtorIterator> getConstructors(const Module &M) { 1070b57cec5SDimitry Andric const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors"); 1080b57cec5SDimitry Andric return make_range(CtorDtorIterator(CtorsList, false), 1090b57cec5SDimitry Andric CtorDtorIterator(CtorsList, true)); 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric iterator_range<CtorDtorIterator> getDestructors(const Module &M) { 1130b57cec5SDimitry Andric const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors"); 1140b57cec5SDimitry Andric return make_range(CtorDtorIterator(DtorsList, false), 1150b57cec5SDimitry Andric CtorDtorIterator(DtorsList, true)); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 118*5ffd83dbSDimitry Andric bool StaticInitGVIterator::isStaticInitGlobal(GlobalValue &GV) { 119*5ffd83dbSDimitry Andric if (GV.isDeclaration()) 120*5ffd83dbSDimitry Andric return false; 121*5ffd83dbSDimitry Andric 122*5ffd83dbSDimitry Andric if (GV.hasName() && (GV.getName() == "llvm.global_ctors" || 123*5ffd83dbSDimitry Andric GV.getName() == "llvm.global_dtors")) 124*5ffd83dbSDimitry Andric return true; 125*5ffd83dbSDimitry Andric 126*5ffd83dbSDimitry Andric if (ObjFmt == Triple::MachO) { 127*5ffd83dbSDimitry Andric // FIXME: These section checks are too strict: We should match first and 128*5ffd83dbSDimitry Andric // second word split by comma. 129*5ffd83dbSDimitry Andric if (GV.hasSection() && 130*5ffd83dbSDimitry Andric (GV.getSection().startswith("__DATA,__objc_classlist") || 131*5ffd83dbSDimitry Andric GV.getSection().startswith("__DATA,__objc_selrefs"))) 132*5ffd83dbSDimitry Andric return true; 133*5ffd83dbSDimitry Andric } 134*5ffd83dbSDimitry Andric 135*5ffd83dbSDimitry Andric return false; 136*5ffd83dbSDimitry Andric } 137*5ffd83dbSDimitry Andric 1380b57cec5SDimitry Andric void CtorDtorRunner::add(iterator_range<CtorDtorIterator> CtorDtors) { 1398bcb0991SDimitry Andric if (CtorDtors.empty()) 1400b57cec5SDimitry Andric return; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric MangleAndInterner Mangle( 1430b57cec5SDimitry Andric JD.getExecutionSession(), 1440b57cec5SDimitry Andric (*CtorDtors.begin()).Func->getParent()->getDataLayout()); 1450b57cec5SDimitry Andric 146480093f4SDimitry Andric for (auto CtorDtor : CtorDtors) { 1470b57cec5SDimitry Andric assert(CtorDtor.Func && CtorDtor.Func->hasName() && 1480b57cec5SDimitry Andric "Ctor/Dtor function must be named to be runnable under the JIT"); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // FIXME: Maybe use a symbol promoter here instead. 1510b57cec5SDimitry Andric if (CtorDtor.Func->hasLocalLinkage()) { 1520b57cec5SDimitry Andric CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage); 1530b57cec5SDimitry Andric CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) { 1570b57cec5SDimitry Andric dbgs() << " Skipping because why now?\n"; 1580b57cec5SDimitry Andric continue; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric CtorDtorsByPriority[CtorDtor.Priority].push_back( 1620b57cec5SDimitry Andric Mangle(CtorDtor.Func->getName())); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric Error CtorDtorRunner::run() { 1670b57cec5SDimitry Andric using CtorDtorTy = void (*)(); 1680b57cec5SDimitry Andric 169480093f4SDimitry Andric SymbolLookupSet LookupSet; 170480093f4SDimitry Andric for (auto &KV : CtorDtorsByPriority) 171480093f4SDimitry Andric for (auto &Name : KV.second) 172480093f4SDimitry Andric LookupSet.add(Name); 173480093f4SDimitry Andric assert(!LookupSet.containsDuplicates() && 174480093f4SDimitry Andric "Ctor/Dtor list contains duplicates"); 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric auto &ES = JD.getExecutionSession(); 177480093f4SDimitry Andric if (auto CtorDtorMap = ES.lookup( 178480093f4SDimitry Andric makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols), 179480093f4SDimitry Andric std::move(LookupSet))) { 1800b57cec5SDimitry Andric for (auto &KV : CtorDtorsByPriority) { 1810b57cec5SDimitry Andric for (auto &Name : KV.second) { 1820b57cec5SDimitry Andric assert(CtorDtorMap->count(Name) && "No entry for Name"); 1830b57cec5SDimitry Andric auto CtorDtor = reinterpret_cast<CtorDtorTy>( 1840b57cec5SDimitry Andric static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress())); 1850b57cec5SDimitry Andric CtorDtor(); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric CtorDtorsByPriority.clear(); 1890b57cec5SDimitry Andric return Error::success(); 1900b57cec5SDimitry Andric } else 1910b57cec5SDimitry Andric return CtorDtorMap.takeError(); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric void LocalCXXRuntimeOverridesBase::runDestructors() { 1950b57cec5SDimitry Andric auto& CXXDestructorDataPairs = DSOHandleOverride; 1960b57cec5SDimitry Andric for (auto &P : CXXDestructorDataPairs) 1970b57cec5SDimitry Andric P.first(P.second); 1980b57cec5SDimitry Andric CXXDestructorDataPairs.clear(); 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor, 2020b57cec5SDimitry Andric void *Arg, 2030b57cec5SDimitry Andric void *DSOHandle) { 2040b57cec5SDimitry Andric auto& CXXDestructorDataPairs = 2050b57cec5SDimitry Andric *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle); 2060b57cec5SDimitry Andric CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg)); 2070b57cec5SDimitry Andric return 0; 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric Error LocalCXXRuntimeOverrides::enable(JITDylib &JD, 2110b57cec5SDimitry Andric MangleAndInterner &Mangle) { 2120b57cec5SDimitry Andric SymbolMap RuntimeInterposes; 2130b57cec5SDimitry Andric RuntimeInterposes[Mangle("__dso_handle")] = 2140b57cec5SDimitry Andric JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride), 2150b57cec5SDimitry Andric JITSymbolFlags::Exported); 2160b57cec5SDimitry Andric RuntimeInterposes[Mangle("__cxa_atexit")] = 2170b57cec5SDimitry Andric JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride), 2180b57cec5SDimitry Andric JITSymbolFlags::Exported); 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric return JD.define(absoluteSymbols(std::move(RuntimeInterposes))); 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 223*5ffd83dbSDimitry Andric void ItaniumCXAAtExitSupport::registerAtExit(void (*F)(void *), void *Ctx, 224*5ffd83dbSDimitry Andric void *DSOHandle) { 225*5ffd83dbSDimitry Andric std::lock_guard<std::mutex> Lock(AtExitsMutex); 226*5ffd83dbSDimitry Andric AtExitRecords[DSOHandle].push_back({F, Ctx}); 227*5ffd83dbSDimitry Andric } 228*5ffd83dbSDimitry Andric 229*5ffd83dbSDimitry Andric void ItaniumCXAAtExitSupport::runAtExits(void *DSOHandle) { 230*5ffd83dbSDimitry Andric std::vector<AtExitRecord> AtExitsToRun; 231*5ffd83dbSDimitry Andric 232*5ffd83dbSDimitry Andric { 233*5ffd83dbSDimitry Andric std::lock_guard<std::mutex> Lock(AtExitsMutex); 234*5ffd83dbSDimitry Andric auto I = AtExitRecords.find(DSOHandle); 235*5ffd83dbSDimitry Andric if (I != AtExitRecords.end()) { 236*5ffd83dbSDimitry Andric AtExitsToRun = std::move(I->second); 237*5ffd83dbSDimitry Andric AtExitRecords.erase(I); 238*5ffd83dbSDimitry Andric } 239*5ffd83dbSDimitry Andric } 240*5ffd83dbSDimitry Andric 241*5ffd83dbSDimitry Andric while (!AtExitsToRun.empty()) { 242*5ffd83dbSDimitry Andric AtExitsToRun.back().F(AtExitsToRun.back().Ctx); 243*5ffd83dbSDimitry Andric AtExitsToRun.pop_back(); 244*5ffd83dbSDimitry Andric } 245*5ffd83dbSDimitry Andric } 246*5ffd83dbSDimitry Andric 2470b57cec5SDimitry Andric DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator( 2480b57cec5SDimitry Andric sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow) 2490b57cec5SDimitry Andric : Dylib(std::move(Dylib)), Allow(std::move(Allow)), 2500b57cec5SDimitry Andric GlobalPrefix(GlobalPrefix) {} 2510b57cec5SDimitry Andric 2528bcb0991SDimitry Andric Expected<std::unique_ptr<DynamicLibrarySearchGenerator>> 2530b57cec5SDimitry Andric DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix, 2540b57cec5SDimitry Andric SymbolPredicate Allow) { 2550b57cec5SDimitry Andric std::string ErrMsg; 2560b57cec5SDimitry Andric auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg); 2570b57cec5SDimitry Andric if (!Lib.isValid()) 2580b57cec5SDimitry Andric return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()); 2598bcb0991SDimitry Andric return std::make_unique<DynamicLibrarySearchGenerator>( 2608bcb0991SDimitry Andric std::move(Lib), GlobalPrefix, std::move(Allow)); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 263480093f4SDimitry Andric Error DynamicLibrarySearchGenerator::tryToGenerate( 264480093f4SDimitry Andric LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, 265480093f4SDimitry Andric const SymbolLookupSet &Symbols) { 2660b57cec5SDimitry Andric orc::SymbolMap NewSymbols; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric bool HasGlobalPrefix = (GlobalPrefix != '\0'); 2690b57cec5SDimitry Andric 270480093f4SDimitry Andric for (auto &KV : Symbols) { 271480093f4SDimitry Andric auto &Name = KV.first; 272480093f4SDimitry Andric 2730b57cec5SDimitry Andric if ((*Name).empty()) 2740b57cec5SDimitry Andric continue; 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric if (Allow && !Allow(Name)) 2770b57cec5SDimitry Andric continue; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric if (HasGlobalPrefix && (*Name).front() != GlobalPrefix) 2800b57cec5SDimitry Andric continue; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric std::string Tmp((*Name).data() + HasGlobalPrefix, 2830b57cec5SDimitry Andric (*Name).size() - HasGlobalPrefix); 2840b57cec5SDimitry Andric if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) { 2850b57cec5SDimitry Andric NewSymbols[Name] = JITEvaluatedSymbol( 2860b57cec5SDimitry Andric static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)), 2870b57cec5SDimitry Andric JITSymbolFlags::Exported); 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric 291480093f4SDimitry Andric if (NewSymbols.empty()) 292480093f4SDimitry Andric return Error::success(); 2930b57cec5SDimitry Andric 294480093f4SDimitry Andric return JD.define(absoluteSymbols(std::move(NewSymbols))); 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric 2978bcb0991SDimitry Andric Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> 2988bcb0991SDimitry Andric StaticLibraryDefinitionGenerator::Load(ObjectLayer &L, const char *FileName) { 2998bcb0991SDimitry Andric auto ArchiveBuffer = errorOrToExpected(MemoryBuffer::getFile(FileName)); 3008bcb0991SDimitry Andric 3018bcb0991SDimitry Andric if (!ArchiveBuffer) 3028bcb0991SDimitry Andric return ArchiveBuffer.takeError(); 3038bcb0991SDimitry Andric 3048bcb0991SDimitry Andric return Create(L, std::move(*ArchiveBuffer)); 3058bcb0991SDimitry Andric } 3068bcb0991SDimitry Andric 3078bcb0991SDimitry Andric Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> 308*5ffd83dbSDimitry Andric StaticLibraryDefinitionGenerator::Load(ObjectLayer &L, const char *FileName, 309*5ffd83dbSDimitry Andric const Triple &TT) { 310*5ffd83dbSDimitry Andric auto B = object::createBinary(FileName); 311*5ffd83dbSDimitry Andric if (!B) 312*5ffd83dbSDimitry Andric return B.takeError(); 313*5ffd83dbSDimitry Andric 314*5ffd83dbSDimitry Andric // If this is a regular archive then create an instance from it. 315*5ffd83dbSDimitry Andric if (isa<object::Archive>(B->getBinary())) 316*5ffd83dbSDimitry Andric return Create(L, std::move(B->takeBinary().second)); 317*5ffd83dbSDimitry Andric 318*5ffd83dbSDimitry Andric // If this is a universal binary then search for a slice matching the given 319*5ffd83dbSDimitry Andric // Triple. 320*5ffd83dbSDimitry Andric if (auto *UB = cast<object::MachOUniversalBinary>(B->getBinary())) { 321*5ffd83dbSDimitry Andric for (const auto &Obj : UB->objects()) { 322*5ffd83dbSDimitry Andric auto ObjTT = Obj.getTriple(); 323*5ffd83dbSDimitry Andric if (ObjTT.getArch() == TT.getArch() && 324*5ffd83dbSDimitry Andric ObjTT.getSubArch() == TT.getSubArch() && 325*5ffd83dbSDimitry Andric ObjTT.getVendor() == TT.getVendor()) { 326*5ffd83dbSDimitry Andric // We found a match. Create an instance from a buffer covering this 327*5ffd83dbSDimitry Andric // slice. 328*5ffd83dbSDimitry Andric auto SliceBuffer = MemoryBuffer::getFileSlice(FileName, Obj.getSize(), 329*5ffd83dbSDimitry Andric Obj.getOffset()); 330*5ffd83dbSDimitry Andric if (!SliceBuffer) 331*5ffd83dbSDimitry Andric return make_error<StringError>( 332*5ffd83dbSDimitry Andric Twine("Could not create buffer for ") + TT.str() + " slice of " + 333*5ffd83dbSDimitry Andric FileName + ": [ " + formatv("{0:x}", Obj.getOffset()) + 334*5ffd83dbSDimitry Andric " .. " + formatv("{0:x}", Obj.getOffset() + Obj.getSize()) + 335*5ffd83dbSDimitry Andric ": " + SliceBuffer.getError().message(), 336*5ffd83dbSDimitry Andric SliceBuffer.getError()); 337*5ffd83dbSDimitry Andric return Create(L, std::move(*SliceBuffer)); 338*5ffd83dbSDimitry Andric } 339*5ffd83dbSDimitry Andric } 340*5ffd83dbSDimitry Andric 341*5ffd83dbSDimitry Andric return make_error<StringError>(Twine("Universal binary ") + FileName + 342*5ffd83dbSDimitry Andric " does not contain a slice for " + 343*5ffd83dbSDimitry Andric TT.str(), 344*5ffd83dbSDimitry Andric inconvertibleErrorCode()); 345*5ffd83dbSDimitry Andric } 346*5ffd83dbSDimitry Andric 347*5ffd83dbSDimitry Andric return make_error<StringError>(Twine("Unrecognized file type for ") + 348*5ffd83dbSDimitry Andric FileName, 349*5ffd83dbSDimitry Andric inconvertibleErrorCode()); 350*5ffd83dbSDimitry Andric } 351*5ffd83dbSDimitry Andric 352*5ffd83dbSDimitry Andric Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> 3538bcb0991SDimitry Andric StaticLibraryDefinitionGenerator::Create( 3548bcb0991SDimitry Andric ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer) { 3558bcb0991SDimitry Andric Error Err = Error::success(); 3568bcb0991SDimitry Andric 3578bcb0991SDimitry Andric std::unique_ptr<StaticLibraryDefinitionGenerator> ADG( 3588bcb0991SDimitry Andric new StaticLibraryDefinitionGenerator(L, std::move(ArchiveBuffer), Err)); 3598bcb0991SDimitry Andric 3608bcb0991SDimitry Andric if (Err) 3618bcb0991SDimitry Andric return std::move(Err); 3628bcb0991SDimitry Andric 3638bcb0991SDimitry Andric return std::move(ADG); 3648bcb0991SDimitry Andric } 3658bcb0991SDimitry Andric 366480093f4SDimitry Andric Error StaticLibraryDefinitionGenerator::tryToGenerate( 367480093f4SDimitry Andric LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, 368480093f4SDimitry Andric const SymbolLookupSet &Symbols) { 369480093f4SDimitry Andric 370480093f4SDimitry Andric // Don't materialize symbols from static archives unless this is a static 371480093f4SDimitry Andric // lookup. 372480093f4SDimitry Andric if (K != LookupKind::Static) 373480093f4SDimitry Andric return Error::success(); 374480093f4SDimitry Andric 375480093f4SDimitry Andric // Bail out early if we've already freed the archive. 376480093f4SDimitry Andric if (!Archive) 377480093f4SDimitry Andric return Error::success(); 3788bcb0991SDimitry Andric 3798bcb0991SDimitry Andric DenseSet<std::pair<StringRef, StringRef>> ChildBufferInfos; 3808bcb0991SDimitry Andric 381480093f4SDimitry Andric for (const auto &KV : Symbols) { 382480093f4SDimitry Andric const auto &Name = KV.first; 383480093f4SDimitry Andric auto Child = Archive->findSym(*Name); 3848bcb0991SDimitry Andric if (!Child) 3858bcb0991SDimitry Andric return Child.takeError(); 3868bcb0991SDimitry Andric if (*Child == None) 3878bcb0991SDimitry Andric continue; 3888bcb0991SDimitry Andric auto ChildBuffer = (*Child)->getMemoryBufferRef(); 3898bcb0991SDimitry Andric if (!ChildBuffer) 3908bcb0991SDimitry Andric return ChildBuffer.takeError(); 3918bcb0991SDimitry Andric ChildBufferInfos.insert( 3928bcb0991SDimitry Andric {ChildBuffer->getBuffer(), ChildBuffer->getBufferIdentifier()}); 3938bcb0991SDimitry Andric } 3948bcb0991SDimitry Andric 3958bcb0991SDimitry Andric for (auto ChildBufferInfo : ChildBufferInfos) { 3968bcb0991SDimitry Andric MemoryBufferRef ChildBufferRef(ChildBufferInfo.first, 3978bcb0991SDimitry Andric ChildBufferInfo.second); 3988bcb0991SDimitry Andric 399*5ffd83dbSDimitry Andric if (auto Err = L.add(JD, MemoryBuffer::getMemBuffer(ChildBufferRef, false), 400*5ffd83dbSDimitry Andric VModuleKey())) 401480093f4SDimitry Andric return Err; 4028bcb0991SDimitry Andric } 4038bcb0991SDimitry Andric 404480093f4SDimitry Andric return Error::success(); 4058bcb0991SDimitry Andric } 4068bcb0991SDimitry Andric 4078bcb0991SDimitry Andric StaticLibraryDefinitionGenerator::StaticLibraryDefinitionGenerator( 4088bcb0991SDimitry Andric ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer, Error &Err) 4098bcb0991SDimitry Andric : L(L), ArchiveBuffer(std::move(ArchiveBuffer)), 410480093f4SDimitry Andric Archive(std::make_unique<object::Archive>(*this->ArchiveBuffer, Err)) {} 4118bcb0991SDimitry Andric 4120b57cec5SDimitry Andric } // End namespace orc. 4130b57cec5SDimitry Andric } // End namespace llvm. 414