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 Triple TT = ES.getTargetTriple();
28
29 auto G = std::make_unique<jitlink::LinkGraph>(
30 ("<indirect stubs graph #" + Twine(++StubGraphIdx) + ">").str(),
31 ES.getSymbolStringPool(), TT, TT.isArch64Bit() ? 8 : 4,
32 TT.isLittleEndian() ? endianness::little : endianness::big,
34 auto &PointerSection =
35 G->createSection(StubPtrSectionName, MemProt::Write | MemProt::Read);
36 auto &StubsSection =
37 G->createSection(JumpStubSectionName, MemProt::Exec | MemProt::Read);
38
39 SymbolFlagsMap NewSymbols;
40 for (auto &[Name, Def] : InitialDests) {
41 jitlink::Symbol *TargetSym = nullptr;
42 if (Def.getAddress())
43 TargetSym = &G->addAbsoluteSymbol(
44 G->allocateName(*Name + "$__init_tgt"), Def.getAddress(), 0,
46
47 auto PtrName = ES.intern((*Name + StubSuffix).str());
48 auto &Ptr = AnonymousPtrCreator(*G, PointerSection, TargetSym, 0);
49 Ptr.setName(PtrName);
51 auto &Stub = PtrJumpStubCreator(*G, StubsSection, Ptr);
52 Stub.setName(Name);
53 Stub.setScope(jitlink::Scope::Default);
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()
92 .writePointers(PtrWrites);
93}
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
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:1379
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