LLVM 22.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(), SubtargetFeatures(),
31 auto &PointerSection =
32 G->createSection(StubPtrSectionName, MemProt::Write | MemProt::Read);
33 auto &StubsSection =
34 G->createSection(JumpStubSectionName, MemProt::Exec | MemProt::Read);
35
36 SymbolFlagsMap NewSymbols;
37 for (auto &[Name, Def] : InitialDests) {
38 jitlink::Symbol *TargetSym = nullptr;
39 if (Def.getAddress())
40 TargetSym = &G->addAbsoluteSymbol(
41 G->allocateName(*Name + "$__init_tgt"), Def.getAddress(), 0,
43
44 auto PtrName = ES.intern((*Name + StubSuffix).str());
45 auto &Ptr = AnonymousPtrCreator(*G, PointerSection, TargetSym, 0);
46 Ptr.setName(PtrName);
48 auto &Stub = PtrJumpStubCreator(*G, StubsSection, Ptr);
49 Stub.setName(Name);
50 Stub.setScope(Def.getFlags().isExported() ? jitlink::Scope::Default
52 Stub.setLinkage(!Def.getFlags().isWeak() ? jitlink::Linkage::Strong
54 NewSymbols[std::move(PtrName)] = JITSymbolFlags();
55 }
56
57 // Try to claim responsibility for the new stub symbols.
58 if (auto Err = R->defineMaterializing(std::move(NewSymbols))) {
59 ES.reportError(std::move(Err));
60 return R->failMaterialization();
61 }
62
63 ObjLinkingLayer.emit(std::move(R), std::move(G));
64}
65
67 const SymbolMap &NewDests) {
68 auto &ES = ObjLinkingLayer.getExecutionSession();
71 for (auto &[StubName, Sym] : NewDests) {
72 auto PtrName = ES.intern((*StubName + StubSuffix).str());
73 PtrToStub[NonOwningSymbolStringPtr(PtrName)] = StubName;
74 LS.add(std::move(PtrName));
75 }
76 auto PtrSyms =
77 ES.lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}}, std::move(LS));
78 if (!PtrSyms)
79 return PtrSyms.takeError();
80
81 std::vector<tpctypes::PointerWrite> PtrWrites;
82 for (auto &[PtrName, PtrSym] : *PtrSyms) {
83 auto DestSymI = NewDests.find(PtrToStub[NonOwningSymbolStringPtr(PtrName)]);
84 assert(DestSymI != NewDests.end() && "Bad ptr -> stub mapping");
85 auto &DestSym = DestSymI->second;
86 PtrWrites.push_back({PtrSym.getAddress(), DestSym.getAddress()});
87 }
88
89 return ObjLinkingLayer.getExecutionSession()
90 .getExecutorProcessControl()
91 .getMemoryAccess()
92 .writePointers(PtrWrites);
93}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define G(x, y, z)
Definition MD5.cpp:56
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:187
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:165
iterator end()
Definition DenseMap.h:81
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Flags for symbols in the JIT.
Definition JITSymbol.h:75
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:902
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.
Definition Core.h:195
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.