LLVM 20.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(jitlink::Scope::Default);
51 NewSymbols[std::move(PtrName)] = JITSymbolFlags();
52 }
53
54 // Try to claim responsibility for the new stub symbols.
55 if (auto Err = R->defineMaterializing(std::move(NewSymbols))) {
56 ES.reportError(std::move(Err));
57 return R->failMaterialization();
58 }
59
60 ObjLinkingLayer.emit(std::move(R), std::move(G));
61}
62
64 const SymbolMap &NewDests) {
65 auto &ES = ObjLinkingLayer.getExecutionSession();
68 for (auto &[StubName, Sym] : NewDests) {
69 auto PtrName = ES.intern((*StubName + StubSuffix).str());
70 PtrToStub[NonOwningSymbolStringPtr(PtrName)] = StubName;
71 LS.add(std::move(PtrName));
72 }
73 auto PtrSyms =
74 ES.lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}}, std::move(LS));
75 if (!PtrSyms)
76 return PtrSyms.takeError();
77
78 std::vector<tpctypes::PointerWrite> PtrWrites;
79 for (auto &[PtrName, PtrSym] : *PtrSyms) {
80 auto DestSymI = NewDests.find(PtrToStub[NonOwningSymbolStringPtr(PtrName)]);
81 assert(DestSymI != NewDests.end() && "Bad ptr -> stub mapping");
82 auto &DestSym = DestSymI->second;
83 PtrWrites.push_back({PtrSym.getAddress(), DestSym.getAddress()});
84 }
85
86 return ObjLinkingLayer.getExecutionSession()
89 .writePointers(PtrWrites);
90}
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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:81
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition: Core.h:1380
Error writePointers(ArrayRef< tpctypes::PointerWrite > Ws)
MemoryAccess & getMemoryAccess() const
Return a MemoryAccess object for the target process.
Represents a JIT'd dynamic library.
Definition: Core.h:897
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.
ExecutionSession & getExecutionSession()
Non-owning SymbolStringPool entry pointer.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:194
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18