|
| 1 | +//===- JITLinkRedirectableSymbolManager.h - JITLink redirection -*- C++ -*-===// |
| 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 | +// Redirectable Symbol Manager implementation using JITLink |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_EXECUTIONENGINE_ORC_JITLINKREDIRECABLEMANAGER_H |
| 14 | +#define LLVM_EXECUTIONENGINE_ORC_JITLINKREDIRECABLEMANAGER_H |
| 15 | + |
| 16 | +#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" |
| 17 | +#include "llvm/ExecutionEngine/Orc/RedirectionManager.h" |
| 18 | +#include "llvm/Support/StringSaver.h" |
| 19 | + |
| 20 | +namespace llvm { |
| 21 | +namespace orc { |
| 22 | + |
| 23 | +class JITLinkRedirectableSymbolManager : public RedirectableSymbolManager, |
| 24 | + public ResourceManager { |
| 25 | +public: |
| 26 | + /// Create redirection manager that uses JITLink based implementaion. |
| 27 | + static Expected<std::unique_ptr<RedirectableSymbolManager>> |
| 28 | + Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &JD) { |
| 29 | + Error Err = Error::success(); |
| 30 | + auto RM = std::unique_ptr<RedirectableSymbolManager>( |
| 31 | + new JITLinkRedirectableSymbolManager(ObjLinkingLayer, JD, Err)); |
| 32 | + if (Err) |
| 33 | + return Err; |
| 34 | + return std::move(RM); |
| 35 | + } |
| 36 | + |
| 37 | + void emitRedirectableSymbols(std::unique_ptr<MaterializationResponsibility> R, |
| 38 | + const SymbolAddrMap &InitialDests) override; |
| 39 | + |
| 40 | + Error redirect(JITDylib &TargetJD, const SymbolAddrMap &NewDests) override; |
| 41 | + |
| 42 | + Error handleRemoveResources(JITDylib &TargetJD, ResourceKey K) override; |
| 43 | + |
| 44 | + void handleTransferResources(JITDylib &TargetJD, ResourceKey DstK, |
| 45 | + ResourceKey SrcK) override; |
| 46 | + |
| 47 | +private: |
| 48 | + using StubHandle = unsigned; |
| 49 | + constexpr static unsigned StubBlockSize = 256; |
| 50 | + constexpr static StringRef JumpStubPrefix = "$__IND_JUMP_STUBS"; |
| 51 | + constexpr static StringRef StubPtrPrefix = "$IND_JUMP_PTR_"; |
| 52 | + constexpr static StringRef JumpStubTableName = "$IND_JUMP_"; |
| 53 | + constexpr static StringRef StubPtrTableName = "$__IND_JUMP_PTRS"; |
| 54 | + |
| 55 | + JITLinkRedirectableSymbolManager(ObjectLinkingLayer &ObjLinkingLayer, |
| 56 | + JITDylib &JD, Error &Err) |
| 57 | + : ObjLinkingLayer(ObjLinkingLayer), JD(JD), |
| 58 | + AnonymousPtrCreator(jitlink::getAnonymousPointerCreator( |
| 59 | + ObjLinkingLayer.getExecutionSession().getTargetTriple())), |
| 60 | + PtrJumpStubCreator(jitlink::getPointerJumpStubCreator( |
| 61 | + ObjLinkingLayer.getExecutionSession().getTargetTriple())) { |
| 62 | + if (!AnonymousPtrCreator || !PtrJumpStubCreator) |
| 63 | + Err = make_error<StringError>("Architecture not supported", |
| 64 | + inconvertibleErrorCode()); |
| 65 | + if (Err) |
| 66 | + return; |
| 67 | + ObjLinkingLayer.getExecutionSession().registerResourceManager(*this); |
| 68 | + } |
| 69 | + |
| 70 | + ~JITLinkRedirectableSymbolManager() { |
| 71 | + ObjLinkingLayer.getExecutionSession().deregisterResourceManager(*this); |
| 72 | + } |
| 73 | + |
| 74 | + StringRef JumpStubSymbolName(unsigned I) { |
| 75 | + return *ObjLinkingLayer.getExecutionSession().intern( |
| 76 | + (JumpStubPrefix + Twine(I)).str()); |
| 77 | + } |
| 78 | + |
| 79 | + StringRef StubPtrSymbolName(unsigned I) { |
| 80 | + return *ObjLinkingLayer.getExecutionSession().intern( |
| 81 | + (StubPtrPrefix + Twine(I)).str()); |
| 82 | + } |
| 83 | + |
| 84 | + unsigned GetNumAvailableStubs() const { return AvailableStubs.size(); } |
| 85 | + |
| 86 | + Error redirectInner(JITDylib &TargetJD, const SymbolAddrMap &NewDests); |
| 87 | + Error grow(unsigned Need); |
| 88 | + |
| 89 | + ObjectLinkingLayer &ObjLinkingLayer; |
| 90 | + JITDylib &JD; |
| 91 | + jitlink::AnonymousPointerCreator AnonymousPtrCreator; |
| 92 | + jitlink::PointerJumpStubCreator PtrJumpStubCreator; |
| 93 | + |
| 94 | + std::vector<StubHandle> AvailableStubs; |
| 95 | + using SymbolToStubMap = DenseMap<SymbolStringPtr, StubHandle>; |
| 96 | + DenseMap<JITDylib *, SymbolToStubMap> SymbolToStubs; |
| 97 | + std::vector<ExecutorSymbolDef> JumpStubs; |
| 98 | + std::vector<ExecutorSymbolDef> StubPointers; |
| 99 | + DenseMap<ResourceKey, std::vector<SymbolStringPtr>> TrackedResources; |
| 100 | + |
| 101 | + std::mutex Mutex; |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace orc |
| 105 | +} // namespace llvm |
| 106 | + |
| 107 | +#endif |
0 commit comments