# HG changeset patch # User Shinji KONO # Date 1626634116 -32400 # Node ID f7655407a6ba1a581af7715983fa5d086ebcd8c7 # Parent 292f64a68d8aa322c27b8df81671baee234c0483 remove unnecessary files diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.cpp --- a/llvm/lib/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -//===---------- TargetProcessControlTypes.cpp - Shared TPC types ----------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// TargetProcessControl types. -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h" - -namespace llvm { -namespace orc { -namespace tpctypes { - -WrapperFunctionResult WrapperFunctionResult::from(StringRef S) { - CWrapperFunctionResult R; - zeroInit(R); - R.Size = S.size(); - if (R.Size > sizeof(uint64_t)) { - R.Data.ValuePtr = new uint8_t[R.Size]; - memcpy(R.Data.ValuePtr, S.data(), R.Size); - R.Destroy = destroyWithDeleteArray; - } else - memcpy(R.Data.Value, S.data(), R.Size); - return R; -} - -void WrapperFunctionResult::destroyWithFree(CWrapperFunctionResultData Data, - uint64_t Size) { - free(Data.ValuePtr); -} - -void WrapperFunctionResult::destroyWithDeleteArray( - CWrapperFunctionResultData Data, uint64_t Size) { - delete[] Data.ValuePtr; -} - -} // end namespace tpctypes -} // end namespace orc -} // end namespace llvm diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/TPCDebugObjectRegistrar.cpp --- a/llvm/lib/ExecutionEngine/Orc/TPCDebugObjectRegistrar.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -//===----- TPCDebugObjectRegistrar.cpp - TPC-based debug registration -----===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/TPCDebugObjectRegistrar.h" - -#include "llvm/ExecutionEngine/Orc/Core.h" -#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h" -#include "llvm/Support/BinaryStreamWriter.h" - -namespace llvm { -namespace orc { - -// Counterpart for readDebugObjectInfo() in TargetProcess/JITLoaderGDB.cpp -static std::vector -writeDebugObjectInfo(sys::MemoryBlock TargetMemBlock) { - auto DebugObjAddr = pointerToJITTargetAddress(TargetMemBlock.base()); - uint64_t DebugObjSize = TargetMemBlock.allocatedSize(); - - std::vector ArgBuffer; - ArgBuffer.resize(sizeof(decltype(DebugObjAddr)) + - sizeof(decltype(DebugObjSize))); - - BinaryStreamWriter ArgWriter(ArgBuffer, support::endianness::big); - cantFail(ArgWriter.writeInteger(DebugObjAddr)); - cantFail(ArgWriter.writeInteger(DebugObjSize)); - - return ArgBuffer; -} - -Expected> -createJITLoaderGDBRegistrar(TargetProcessControl &TPC) { - auto ProcessHandle = TPC.loadDylib(nullptr); - if (!ProcessHandle) - return ProcessHandle.takeError(); - - SymbolStringPtr RegisterFn = - TPC.getTargetTriple().isOSBinFormatMachO() - ? TPC.intern("_llvm_orc_registerJITLoaderGDBWrapper") - : TPC.intern("llvm_orc_registerJITLoaderGDBWrapper"); - - SymbolLookupSet RegistrationSymbols; - RegistrationSymbols.add(RegisterFn); - - auto Result = TPC.lookupSymbols({{*ProcessHandle, RegistrationSymbols}}); - if (!Result) - return Result.takeError(); - - assert(Result->size() == 1 && "Unexpected number of dylibs in result"); - assert((*Result)[0].size() == 1 && - "Unexpected number of addresses in result"); - - return std::make_unique(TPC, (*Result)[0][0], - &writeDebugObjectInfo); -} - -} // namespace orc -} // namespace llvm diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.cpp --- a/llvm/lib/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -//===---------------- TPCDynamicLibrarySearchGenerator.cpp ----------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/TPCDynamicLibrarySearchGenerator.h" - -namespace llvm { -namespace orc { - -Expected> -TPCDynamicLibrarySearchGenerator::Load(TargetProcessControl &TPC, - const char *LibraryPath, - SymbolPredicate Allow) { - auto Handle = TPC.loadDylib(LibraryPath); - if (!Handle) - return Handle.takeError(); - - return std::make_unique(TPC, *Handle, - std::move(Allow)); -} - -Error TPCDynamicLibrarySearchGenerator::tryToGenerate( - LookupState &LS, LookupKind K, JITDylib &JD, - JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) { - - if (Symbols.empty()) - return Error::success(); - - SymbolLookupSet LookupSymbols; - - for (auto &KV : Symbols) { - // Skip symbols that don't match the filter. - if (Allow && !Allow(KV.first)) - continue; - LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol); - } - - SymbolMap NewSymbols; - - TargetProcessControl::LookupRequest Request(H, LookupSymbols); - auto Result = TPC.lookupSymbols(Request); - if (!Result) - return Result.takeError(); - - assert(Result->size() == 1 && "Results for more than one library returned"); - assert(Result->front().size() == LookupSymbols.size() && - "Result has incorrect number of elements"); - - auto ResultI = Result->front().begin(); - for (auto &KV : LookupSymbols) { - if (*ResultI) - NewSymbols[KV.first] = - JITEvaluatedSymbol(*ResultI, JITSymbolFlags::Exported); - ++ResultI; - } - - // If there were no resolved symbols bail out. - if (NewSymbols.empty()) - return Error::success(); - - // Define resolved symbols. - return JD.define(absoluteSymbols(std::move(NewSymbols))); -} - -} // end namespace orc -} // end namespace llvm diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/TPCEHFrameRegistrar.cpp --- a/llvm/lib/ExecutionEngine/Orc/TPCEHFrameRegistrar.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -//===------ TPCEHFrameRegistrar.cpp - TPC-based eh-frame registration -----===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/TPCEHFrameRegistrar.h" -#include "llvm/Support/BinaryStreamWriter.h" - -namespace llvm { -namespace orc { - -Expected> -TPCEHFrameRegistrar::Create(TargetProcessControl &TPC) { - // FIXME: Proper mangling here -- we really need to decouple linker mangling - // from DataLayout. - - // Find the addresses of the registration/deregistration functions in the - // target process. - auto ProcessHandle = TPC.loadDylib(nullptr); - if (!ProcessHandle) - return ProcessHandle.takeError(); - - std::string RegisterWrapperName, DeregisterWrapperName; - if (TPC.getTargetTriple().isOSBinFormatMachO()) { - RegisterWrapperName += '_'; - DeregisterWrapperName += '_'; - } - RegisterWrapperName += "llvm_orc_registerEHFrameSectionWrapper"; - DeregisterWrapperName += "llvm_orc_deregisterEHFrameSectionWrapper"; - - SymbolLookupSet RegistrationSymbols; - RegistrationSymbols.add(TPC.intern(RegisterWrapperName)); - RegistrationSymbols.add(TPC.intern(DeregisterWrapperName)); - - auto Result = TPC.lookupSymbols({{*ProcessHandle, RegistrationSymbols}}); - if (!Result) - return Result.takeError(); - - assert(Result->size() == 1 && "Unexpected number of dylibs in result"); - assert((*Result)[0].size() == 2 && - "Unexpected number of addresses in result"); - - auto RegisterEHFrameWrapperFnAddr = (*Result)[0][0]; - auto DeregisterEHFrameWrapperFnAddr = (*Result)[0][1]; - - return std::make_unique( - TPC, RegisterEHFrameWrapperFnAddr, DeregisterEHFrameWrapperFnAddr); -} - -Error TPCEHFrameRegistrar::registerEHFrames(JITTargetAddress EHFrameSectionAddr, - size_t EHFrameSectionSize) { - constexpr size_t ArgBufferSize = sizeof(uint64_t) + sizeof(uint64_t); - uint8_t ArgBuffer[ArgBufferSize]; - BinaryStreamWriter ArgWriter( - MutableArrayRef(ArgBuffer, ArgBufferSize), - support::endianness::big); - cantFail(ArgWriter.writeInteger(static_cast(EHFrameSectionAddr))); - cantFail(ArgWriter.writeInteger(static_cast(EHFrameSectionSize))); - - return TPC.runWrapper(RegisterEHFrameWrapperFnAddr, ArgBuffer).takeError(); -} - -Error TPCEHFrameRegistrar::deregisterEHFrames( - JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) { - constexpr size_t ArgBufferSize = sizeof(uint64_t) + sizeof(uint64_t); - uint8_t ArgBuffer[ArgBufferSize]; - BinaryStreamWriter ArgWriter( - MutableArrayRef(ArgBuffer, ArgBufferSize), - support::endianness::big); - cantFail(ArgWriter.writeInteger(static_cast(EHFrameSectionAddr))); - cantFail(ArgWriter.writeInteger(static_cast(EHFrameSectionSize))); - - return TPC.runWrapper(DeregisterEHFrameWrapperFnAddr, ArgBuffer).takeError(); -} - -} // end namespace orc -} // end namespace llvm diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp --- a/llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,423 +0,0 @@ -//===------ TargetProcessControl.cpp -- Target process control APIs -------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/TPCIndirectionUtils.h" - -#include "llvm/ExecutionEngine/Orc/TargetProcessControl.h" -#include "llvm/Support/MathExtras.h" - -#include - -using namespace llvm; -using namespace llvm::orc; - -namespace llvm { -namespace orc { - -class TPCIndirectionUtilsAccess { -public: - using IndirectStubInfo = TPCIndirectionUtils::IndirectStubInfo; - using IndirectStubInfoVector = TPCIndirectionUtils::IndirectStubInfoVector; - - static Expected - getIndirectStubs(TPCIndirectionUtils &TPCIU, unsigned NumStubs) { - return TPCIU.getIndirectStubs(NumStubs); - }; -}; - -} // end namespace orc -} // end namespace llvm - -namespace { - -class TPCTrampolinePool : public TrampolinePool { -public: - TPCTrampolinePool(TPCIndirectionUtils &TPCIU); - Error deallocatePool(); - -protected: - Error grow() override; - - using Allocation = jitlink::JITLinkMemoryManager::Allocation; - - TPCIndirectionUtils &TPCIU; - unsigned TrampolineSize = 0; - unsigned TrampolinesPerPage = 0; - std::vector> TrampolineBlocks; -}; - -class TPCIndirectStubsManager : public IndirectStubsManager, - private TPCIndirectionUtilsAccess { -public: - TPCIndirectStubsManager(TPCIndirectionUtils &TPCIU) : TPCIU(TPCIU) {} - - Error deallocateStubs(); - - Error createStub(StringRef StubName, JITTargetAddress StubAddr, - JITSymbolFlags StubFlags) override; - - Error createStubs(const StubInitsMap &StubInits) override; - - JITEvaluatedSymbol findStub(StringRef Name, bool ExportedStubsOnly) override; - - JITEvaluatedSymbol findPointer(StringRef Name) override; - - Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override; - -private: - using StubInfo = std::pair; - - std::mutex ISMMutex; - TPCIndirectionUtils &TPCIU; - StringMap StubInfos; -}; - -TPCTrampolinePool::TPCTrampolinePool(TPCIndirectionUtils &TPCIU) - : TPCIU(TPCIU) { - auto &TPC = TPCIU.getTargetProcessControl(); - auto &ABI = TPCIU.getABISupport(); - - TrampolineSize = ABI.getTrampolineSize(); - TrampolinesPerPage = - (TPC.getPageSize() - ABI.getPointerSize()) / TrampolineSize; -} - -Error TPCTrampolinePool::deallocatePool() { - Error Err = Error::success(); - for (auto &Alloc : TrampolineBlocks) - Err = joinErrors(std::move(Err), Alloc->deallocate()); - return Err; -} - -Error TPCTrampolinePool::grow() { - assert(AvailableTrampolines.empty() && - "Grow called with trampolines still available"); - - auto ResolverAddress = TPCIU.getResolverBlockAddress(); - assert(ResolverAddress && "Resolver address can not be null"); - - auto &TPC = TPCIU.getTargetProcessControl(); - constexpr auto TrampolinePagePermissions = - static_cast(sys::Memory::MF_READ | - sys::Memory::MF_EXEC); - auto PageSize = TPC.getPageSize(); - jitlink::JITLinkMemoryManager::SegmentsRequestMap Request; - Request[TrampolinePagePermissions] = {PageSize, static_cast(PageSize), - 0}; - auto Alloc = TPC.getMemMgr().allocate(nullptr, Request); - - if (!Alloc) - return Alloc.takeError(); - - unsigned NumTrampolines = TrampolinesPerPage; - - auto WorkingMemory = (*Alloc)->getWorkingMemory(TrampolinePagePermissions); - auto TargetAddress = (*Alloc)->getTargetMemory(TrampolinePagePermissions); - - TPCIU.getABISupport().writeTrampolines(WorkingMemory.data(), TargetAddress, - ResolverAddress, NumTrampolines); - - auto TargetAddr = (*Alloc)->getTargetMemory(TrampolinePagePermissions); - for (unsigned I = 0; I < NumTrampolines; ++I) - AvailableTrampolines.push_back(TargetAddr + (I * TrampolineSize)); - - if (auto Err = (*Alloc)->finalize()) - return Err; - - TrampolineBlocks.push_back(std::move(*Alloc)); - - return Error::success(); -} - -Error TPCIndirectStubsManager::createStub(StringRef StubName, - JITTargetAddress StubAddr, - JITSymbolFlags StubFlags) { - StubInitsMap SIM; - SIM[StubName] = std::make_pair(StubAddr, StubFlags); - return createStubs(SIM); -} - -Error TPCIndirectStubsManager::createStubs(const StubInitsMap &StubInits) { - auto AvailableStubInfos = getIndirectStubs(TPCIU, StubInits.size()); - if (!AvailableStubInfos) - return AvailableStubInfos.takeError(); - - { - std::lock_guard Lock(ISMMutex); - unsigned ASIdx = 0; - for (auto &SI : StubInits) { - auto &A = (*AvailableStubInfos)[ASIdx++]; - StubInfos[SI.first()] = std::make_pair(A, SI.second.second); - } - } - - auto &MemAccess = TPCIU.getTargetProcessControl().getMemoryAccess(); - switch (TPCIU.getABISupport().getPointerSize()) { - case 4: { - unsigned ASIdx = 0; - std::vector PtrUpdates; - for (auto &SI : StubInits) - PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress, - static_cast(SI.second.first)}); - return MemAccess.writeUInt32s(PtrUpdates); - } - case 8: { - unsigned ASIdx = 0; - std::vector PtrUpdates; - for (auto &SI : StubInits) - PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress, - static_cast(SI.second.first)}); - return MemAccess.writeUInt64s(PtrUpdates); - } - default: - return make_error("Unsupported pointer size", - inconvertibleErrorCode()); - } -} - -JITEvaluatedSymbol TPCIndirectStubsManager::findStub(StringRef Name, - bool ExportedStubsOnly) { - std::lock_guard Lock(ISMMutex); - auto I = StubInfos.find(Name); - if (I == StubInfos.end()) - return nullptr; - return {I->second.first.StubAddress, I->second.second}; -} - -JITEvaluatedSymbol TPCIndirectStubsManager::findPointer(StringRef Name) { - std::lock_guard Lock(ISMMutex); - auto I = StubInfos.find(Name); - if (I == StubInfos.end()) - return nullptr; - return {I->second.first.PointerAddress, I->second.second}; -} - -Error TPCIndirectStubsManager::updatePointer(StringRef Name, - JITTargetAddress NewAddr) { - - JITTargetAddress PtrAddr = 0; - { - std::lock_guard Lock(ISMMutex); - auto I = StubInfos.find(Name); - if (I == StubInfos.end()) - return make_error("Unknown stub name", - inconvertibleErrorCode()); - PtrAddr = I->second.first.PointerAddress; - } - - auto &MemAccess = TPCIU.getTargetProcessControl().getMemoryAccess(); - switch (TPCIU.getABISupport().getPointerSize()) { - case 4: { - tpctypes::UInt32Write PUpdate(PtrAddr, NewAddr); - return MemAccess.writeUInt32s(PUpdate); - } - case 8: { - tpctypes::UInt64Write PUpdate(PtrAddr, NewAddr); - return MemAccess.writeUInt64s(PUpdate); - } - default: - return make_error("Unsupported pointer size", - inconvertibleErrorCode()); - } -} - -} // end anonymous namespace. - -namespace llvm { -namespace orc { - -TPCIndirectionUtils::ABISupport::~ABISupport() {} - -Expected> -TPCIndirectionUtils::Create(TargetProcessControl &TPC) { - const auto &TT = TPC.getTargetTriple(); - switch (TT.getArch()) { - default: - return make_error( - std::string("No TPCIndirectionUtils available for ") + TT.str(), - inconvertibleErrorCode()); - case Triple::aarch64: - case Triple::aarch64_32: - return CreateWithABI(TPC); - - case Triple::x86: - return CreateWithABI(TPC); - - case Triple::mips: - return CreateWithABI(TPC); - - case Triple::mipsel: - return CreateWithABI(TPC); - - case Triple::mips64: - case Triple::mips64el: - return CreateWithABI(TPC); - - case Triple::x86_64: - if (TT.getOS() == Triple::OSType::Win32) - return CreateWithABI(TPC); - else - return CreateWithABI(TPC); - } -} - -Error TPCIndirectionUtils::cleanup() { - Error Err = Error::success(); - - for (auto &A : IndirectStubAllocs) - Err = joinErrors(std::move(Err), A->deallocate()); - - if (TP) - Err = joinErrors(std::move(Err), - static_cast(*TP).deallocatePool()); - - if (ResolverBlock) - Err = joinErrors(std::move(Err), ResolverBlock->deallocate()); - - return Err; -} - -Expected -TPCIndirectionUtils::writeResolverBlock(JITTargetAddress ReentryFnAddr, - JITTargetAddress ReentryCtxAddr) { - assert(ABI && "ABI can not be null"); - constexpr auto ResolverBlockPermissions = - static_cast(sys::Memory::MF_READ | - sys::Memory::MF_EXEC); - auto ResolverSize = ABI->getResolverCodeSize(); - - jitlink::JITLinkMemoryManager::SegmentsRequestMap Request; - Request[ResolverBlockPermissions] = {TPC.getPageSize(), - static_cast(ResolverSize), 0}; - auto Alloc = TPC.getMemMgr().allocate(nullptr, Request); - if (!Alloc) - return Alloc.takeError(); - - auto WorkingMemory = (*Alloc)->getWorkingMemory(ResolverBlockPermissions); - ResolverBlockAddr = (*Alloc)->getTargetMemory(ResolverBlockPermissions); - ABI->writeResolverCode(WorkingMemory.data(), ResolverBlockAddr, ReentryFnAddr, - ReentryCtxAddr); - - if (auto Err = (*Alloc)->finalize()) - return std::move(Err); - - ResolverBlock = std::move(*Alloc); - return ResolverBlockAddr; -} - -std::unique_ptr -TPCIndirectionUtils::createIndirectStubsManager() { - return std::make_unique(*this); -} - -TrampolinePool &TPCIndirectionUtils::getTrampolinePool() { - if (!TP) - TP = std::make_unique(*this); - return *TP; -} - -LazyCallThroughManager &TPCIndirectionUtils::createLazyCallThroughManager( - ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr) { - assert(!LCTM && - "createLazyCallThroughManager can not have been called before"); - LCTM = std::make_unique(ES, ErrorHandlerAddr, - &getTrampolinePool()); - return *LCTM; -} - -TPCIndirectionUtils::TPCIndirectionUtils(TargetProcessControl &TPC, - std::unique_ptr ABI) - : TPC(TPC), ABI(std::move(ABI)) { - assert(this->ABI && "ABI can not be null"); - - assert(TPC.getPageSize() > getABISupport().getStubSize() && - "Stubs larger than one page are not supported"); -} - -Expected -TPCIndirectionUtils::getIndirectStubs(unsigned NumStubs) { - - std::lock_guard Lock(TPCUIMutex); - - // If there aren't enough stubs available then allocate some more. - if (NumStubs > AvailableIndirectStubs.size()) { - auto NumStubsToAllocate = NumStubs; - auto PageSize = TPC.getPageSize(); - auto StubBytes = alignTo(NumStubsToAllocate * ABI->getStubSize(), PageSize); - NumStubsToAllocate = StubBytes / ABI->getStubSize(); - auto PointerBytes = - alignTo(NumStubsToAllocate * ABI->getPointerSize(), PageSize); - - constexpr auto StubPagePermissions = - static_cast(sys::Memory::MF_READ | - sys::Memory::MF_EXEC); - constexpr auto PointerPagePermissions = - static_cast(sys::Memory::MF_READ | - sys::Memory::MF_WRITE); - - jitlink::JITLinkMemoryManager::SegmentsRequestMap Request; - Request[StubPagePermissions] = {PageSize, static_cast(StubBytes), - 0}; - Request[PointerPagePermissions] = {PageSize, 0, PointerBytes}; - auto Alloc = TPC.getMemMgr().allocate(nullptr, Request); - if (!Alloc) - return Alloc.takeError(); - - auto StubTargetAddr = (*Alloc)->getTargetMemory(StubPagePermissions); - auto PointerTargetAddr = (*Alloc)->getTargetMemory(PointerPagePermissions); - - ABI->writeIndirectStubsBlock( - (*Alloc)->getWorkingMemory(StubPagePermissions).data(), StubTargetAddr, - PointerTargetAddr, NumStubsToAllocate); - - if (auto Err = (*Alloc)->finalize()) - return std::move(Err); - - for (unsigned I = 0; I != NumStubsToAllocate; ++I) { - AvailableIndirectStubs.push_back( - IndirectStubInfo(StubTargetAddr, PointerTargetAddr)); - StubTargetAddr += ABI->getStubSize(); - PointerTargetAddr += ABI->getPointerSize(); - } - - IndirectStubAllocs.push_back(std::move(*Alloc)); - } - - assert(NumStubs <= AvailableIndirectStubs.size() && - "Sufficient stubs should have been allocated above"); - - IndirectStubInfoVector Result; - while (NumStubs--) { - Result.push_back(AvailableIndirectStubs.back()); - AvailableIndirectStubs.pop_back(); - } - - return std::move(Result); -} - -static JITTargetAddress reentry(JITTargetAddress LCTMAddr, - JITTargetAddress TrampolineAddr) { - auto &LCTM = *jitTargetAddressToPointer(LCTMAddr); - std::promise LandingAddrP; - auto LandingAddrF = LandingAddrP.get_future(); - LCTM.resolveTrampolineLandingAddress( - TrampolineAddr, - [&](JITTargetAddress Addr) { LandingAddrP.set_value(Addr); }); - return LandingAddrF.get(); -} - -Error setUpInProcessLCTMReentryViaTPCIU(TPCIndirectionUtils &TPCIU) { - auto &LCTM = TPCIU.getLazyCallThroughManager(); - return TPCIU - .writeResolverBlock(pointerToJITTargetAddress(&reentry), - pointerToJITTargetAddress(&LCTM)) - .takeError(); -} - -} // end namespace orc -} // end namespace llvm diff -r 292f64a68d8a -r f7655407a6ba llvm/lib/ExecutionEngine/Orc/TargetProcessControl.cpp --- a/llvm/lib/ExecutionEngine/Orc/TargetProcessControl.cpp Mon Jul 19 03:29:25 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,153 +0,0 @@ -//===------ TargetProcessControl.cpp -- Target process control APIs -------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ExecutionEngine/Orc/TargetProcessControl.h" - -#include "llvm/ExecutionEngine/Orc/Core.h" -#include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" -#include "llvm/Support/Host.h" -#include "llvm/Support/Process.h" - -#include - -namespace llvm { -namespace orc { - -TargetProcessControl::MemoryAccess::~MemoryAccess() {} - -TargetProcessControl::~TargetProcessControl() {} - -SelfTargetProcessControl::SelfTargetProcessControl( - std::shared_ptr SSP, Triple TargetTriple, - unsigned PageSize, std::unique_ptr MemMgr) - : TargetProcessControl(std::move(SSP)) { - - OwnedMemMgr = std::move(MemMgr); - if (!OwnedMemMgr) - OwnedMemMgr = std::make_unique(); - - this->TargetTriple = std::move(TargetTriple); - this->PageSize = PageSize; - this->MemMgr = OwnedMemMgr.get(); - this->MemAccess = this; - if (this->TargetTriple.isOSBinFormatMachO()) - GlobalManglingPrefix = '_'; -} - -Expected> -SelfTargetProcessControl::Create( - std::shared_ptr SSP, - std::unique_ptr MemMgr) { - auto PageSize = sys::Process::getPageSize(); - if (!PageSize) - return PageSize.takeError(); - - Triple TT(sys::getProcessTriple()); - - return std::make_unique( - std::move(SSP), std::move(TT), *PageSize, std::move(MemMgr)); -} - -Expected -SelfTargetProcessControl::loadDylib(const char *DylibPath) { - std::string ErrMsg; - auto Dylib = std::make_unique( - sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg)); - if (!Dylib->isValid()) - return make_error(std::move(ErrMsg), inconvertibleErrorCode()); - DynamicLibraries.push_back(std::move(Dylib)); - return pointerToJITTargetAddress(DynamicLibraries.back().get()); -} - -Expected> -SelfTargetProcessControl::lookupSymbols(ArrayRef Request) { - std::vector R; - - for (auto &Elem : Request) { - auto *Dylib = jitTargetAddressToPointer(Elem.Handle); - assert(llvm::any_of(DynamicLibraries, - [=](const std::unique_ptr &DL) { - return DL.get() == Dylib; - }) && - "Invalid handle"); - - R.push_back(std::vector()); - for (auto &KV : Elem.Symbols) { - auto &Sym = KV.first; - std::string Tmp((*Sym).data() + !!GlobalManglingPrefix, - (*Sym).size() - !!GlobalManglingPrefix); - void *Addr = Dylib->getAddressOfSymbol(Tmp.c_str()); - if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) { - // FIXME: Collect all failing symbols before erroring out. - SymbolNameVector MissingSymbols; - MissingSymbols.push_back(Sym); - return make_error(std::move(MissingSymbols)); - } - R.back().push_back(pointerToJITTargetAddress(Addr)); - } - } - - return R; -} - -Expected -SelfTargetProcessControl::runAsMain(JITTargetAddress MainFnAddr, - ArrayRef Args) { - using MainTy = int (*)(int, char *[]); - return orc::runAsMain(jitTargetAddressToFunction(MainFnAddr), Args); -} - -Expected -SelfTargetProcessControl::runWrapper(JITTargetAddress WrapperFnAddr, - ArrayRef ArgBuffer) { - using WrapperFnTy = - tpctypes::CWrapperFunctionResult (*)(const uint8_t *Data, uint64_t Size); - auto *WrapperFn = jitTargetAddressToFunction(WrapperFnAddr); - return WrapperFn(ArgBuffer.data(), ArgBuffer.size()); -} - -Error SelfTargetProcessControl::disconnect() { return Error::success(); } - -void SelfTargetProcessControl::writeUInt8s(ArrayRef Ws, - WriteResultFn OnWriteComplete) { - for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; - OnWriteComplete(Error::success()); -} - -void SelfTargetProcessControl::writeUInt16s(ArrayRef Ws, - WriteResultFn OnWriteComplete) { - for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; - OnWriteComplete(Error::success()); -} - -void SelfTargetProcessControl::writeUInt32s(ArrayRef Ws, - WriteResultFn OnWriteComplete) { - for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; - OnWriteComplete(Error::success()); -} - -void SelfTargetProcessControl::writeUInt64s(ArrayRef Ws, - WriteResultFn OnWriteComplete) { - for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; - OnWriteComplete(Error::success()); -} - -void SelfTargetProcessControl::writeBuffers(ArrayRef Ws, - WriteResultFn OnWriteComplete) { - for (auto &W : Ws) - memcpy(jitTargetAddressToPointer(W.Address), W.Buffer.data(), - W.Buffer.size()); - OnWriteComplete(Error::success()); -} - -} // end namespace orc -} // end namespace llvm