LLVM 24.0.0git
JITLinkRedirectableSymbolManager.cpp
Go to the documentation of this file.
1//===-- JITLinkRedirectableSymbolManager.cpp - JITLink redirection 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
11
12#define DEBUG_TYPE "orc"
13
14using namespace llvm;
15using namespace llvm::orc;
16
17namespace {
18constexpr StringRef JumpStubSectionName = "__orc_stubs";
19constexpr StringRef StubPtrSectionName = "__orc_stub_ptrs";
20constexpr StringRef StubSuffix = "$__stub_ptr";
21} // namespace
22
24 std::unique_ptr<MaterializationResponsibility> R, SymbolMap InitialDests) {
25
26 auto &ES = ObjLinkingLayer.getExecutionSession();
27 auto G = std::make_unique<jitlink::LinkGraph>(
28 ("<indirect stubs graph #" + Twine(++StubGraphIdx) + ">").str(),
29 ES.getSymbolStringPool(), ES.getTargetTriple(),
30 ES.getTargetTriple().getArchPointerBitWidth() / 8, SubtargetFeatures(),
32 auto &PointerSection =
33 G->createSection(StubPtrSectionName, MemProt::Write | MemProt::Read);
34 auto &StubsSection =
35 G->createSection(JumpStubSectionName, MemProt::Exec | MemProt::Read);
36
37 SymbolFlagsMap NewSymbols;
38 for (auto &[Name, Def] : InitialDests) {
39 jitlink::Symbol *TargetSym = nullptr;
40 if (Def.getAddress())
41 TargetSym = &G->addAbsoluteSymbol(
42 G->allocateName(*Name + "$__init_tgt"), Def.getAddress(), 0,
44
45 auto PtrName = ES.intern((*Name + StubSuffix).str());
46 auto &Ptr = AnonymousPtrCreator(*G, PointerSection, TargetSym, 0);
47 Ptr.setName(PtrName);
48 Ptr.setScope(jitlink::Scope::Hidden);
49 auto &Stub = PtrJumpStubCreator(*G, StubsSection, Ptr);
50 Stub.setName(Name);
51 Stub.setScope(Def.getFlags().isExported() ? jitlink::Scope::Default
53 Stub.setLinkage(!Def.getFlags().isWeak() ? jitlink::Linkage::Strong
55 NewSymbols[std::move(PtrName)] = JITSymbolFlags();
56 }
57
58 // Try to claim responsibility for the new stub symbols.
59 if (auto Err = R->defineMaterializing(std::move(NewSymbols))) {
60 ES.reportError(std::move(Err));
61 return R->failMaterialization();
62 }
63
64 ObjLinkingLayer.emit(std::move(R), std::move(G));
65}
66
68 const SymbolMap &NewDests) {
69 auto &ES = ObjLinkingLayer.getExecutionSession();
72 for (auto &[StubName, Sym] : NewDests) {
73 auto PtrName = ES.intern((*StubName + StubSuffix).str());
74 PtrToStub[NonOwningSymbolStringPtr(PtrName)] = StubName;
75 LS.add(std::move(PtrName));
76 }
77 auto PtrSyms =
78 ES.lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}}, std::move(LS));
79 if (!PtrSyms)
80 return PtrSyms.takeError();
81
82 std::vector<tpctypes::PointerWrite> PtrWrites;
83 for (auto &[PtrName, PtrSym] : *PtrSyms) {
84 auto DestSymI = NewDests.find(PtrToStub[NonOwningSymbolStringPtr(PtrName)]);
85 assert(DestSymI != NewDests.end() && "Bad ptr -> stub mapping");
86 auto &DestSym = DestSymI->second;
87 PtrWrites.push_back({PtrSym.getAddress(), DestSym.getAddress()});
88 }
89
90 return MemAccess.writePointers(PtrWrites);
91}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define G(x, y, z)
Definition MD5.cpp:55
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:782
iterator end()
Definition DenseMap.h:702
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:809
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Flags for symbols in the JIT.
Definition JITSymbol.h:75
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Manages the enabling and disabling of subtarget specific features.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Represents a JIT'd dynamic library.
Definition Core.h:676
void emitRedirectableSymbols(std::unique_ptr< MaterializationResponsibility > R, SymbolMap InitialDests) override
Emit redirectable symbol.
Error redirect(JITDylib &JD, const SymbolMap &NewDests) override
Change the redirection destination of given symbols to new destination symbols.
Non-owning SymbolStringPool entry pointer.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
This is an optimization pass for GlobalISel generic memory operations.