LLVM 22.0.0git
JITLink.cpp
Go to the documentation of this file.
1//===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
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
10
22
23using namespace llvm;
24using namespace llvm::object;
25
26#define DEBUG_TYPE "jitlink"
27
28namespace {
29
30enum JITLinkErrorCode { GenericJITLinkError = 1 };
31
32// FIXME: This class is only here to support the transition to llvm::Error. It
33// will be removed once this transition is complete. Clients should prefer to
34// deal with the Error value directly, rather than converting to error_code.
35class JITLinkerErrorCategory : public std::error_category {
36public:
37 const char *name() const noexcept override { return "runtimedyld"; }
38
39 std::string message(int Condition) const override {
40 switch (static_cast<JITLinkErrorCode>(Condition)) {
41 case GenericJITLinkError:
42 return "Generic JITLink error";
43 }
44 llvm_unreachable("Unrecognized JITLinkErrorCode");
45 }
46};
47
48} // namespace
49
50namespace llvm {
51namespace jitlink {
52
53char JITLinkError::ID = 0;
54
55void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
56
57std::error_code JITLinkError::convertToErrorCode() const {
58 static JITLinkerErrorCategory TheJITLinkerErrorCategory;
59 return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
60}
61
62const char *getGenericEdgeKindName(Edge::Kind K) {
63 switch (K) {
64 case Edge::Invalid:
65 return "INVALID RELOCATION";
66 case Edge::KeepAlive:
67 return "Keep-Alive";
68 default:
69 return "<Unrecognized edge kind>";
70 }
71}
72
73const char *getLinkageName(Linkage L) {
74 switch (L) {
75 case Linkage::Strong:
76 return "strong";
77 case Linkage::Weak:
78 return "weak";
79 }
80 llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
81}
82
83const char *getScopeName(Scope S) {
84 switch (S) {
85 case Scope::Default:
86 return "default";
87 case Scope::Hidden:
88 return "hidden";
90 return "side-effects-only";
91 case Scope::Local:
92 return "local";
93 }
94 llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
95}
96
98 if (B.getSize() == 0) // Empty blocks are not valid C-strings.
99 return false;
100
101 // Zero-fill blocks of size one are valid empty strings.
102 if (B.isZeroFill())
103 return B.getSize() == 1;
104
105 for (size_t I = 0; I != B.getSize() - 1; ++I)
106 if (B.getContent()[I] == '\0')
107 return false;
108
109 return B.getContent()[B.getSize() - 1] == '\0';
110}
111
113 return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
114 << ": "
115 << "size = " << formatv("{0:x8}", B.getSize()) << ", "
116 << (B.isZeroFill() ? "zero-fill" : "content")
117 << ", align = " << B.getAlignment()
118 << ", align-ofs = " << B.getAlignmentOffset()
119 << ", section = " << B.getSection().getName();
120}
121
123 OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
124 << " + " << formatv("{0:x8}", Sym.getOffset())
125 << "): size: " << formatv("{0:x8}", Sym.getSize())
126 << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
127 << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
128 << (Sym.isLive() ? "live" : "dead") << " - "
129 << (Sym.hasName() ? *Sym.getName() : "<anonymous symbol>");
130 return OS;
131}
132
133void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
134 StringRef EdgeKindName) {
135 OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
136 << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
137 << " -> ";
138
139 auto &TargetSym = E.getTarget();
140 if (TargetSym.hasName())
141 OS << TargetSym.getName();
142 else {
143 auto &TargetBlock = TargetSym.getBlock();
144 auto &TargetSec = TargetBlock.getSection();
145 orc::ExecutorAddr SecAddress(~uint64_t(0));
146 for (auto *B : TargetSec.blocks())
147 if (B->getAddress() < SecAddress)
148 SecAddress = B->getAddress();
149
150 orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
151 OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
152 if (SecDelta)
153 OS << " + " << formatv("{0:x}", SecDelta);
154 OS << " / block " << TargetBlock.getAddress();
155 if (TargetSym.getOffset())
156 OS << " + " << formatv("{0:x}", TargetSym.getOffset());
157 OS << ")";
158 }
159
160 if (E.getAddend() != 0)
161 OS << " + " << E.getAddend();
162}
163
165 for (auto *Sym : Symbols)
166 Sym->~Symbol();
167 for (auto *B : Blocks)
168 B->~Block();
169}
170
172 for (auto *Sym : AbsoluteSymbols) {
173 Sym->~Symbol();
174 }
175 for (auto *Sym : external_symbols()) {
176 Sym->~Symbol();
177 }
178 ExternalSymbols.clear();
179}
180
181std::vector<Block *> LinkGraph::splitBlockImpl(std::vector<Block *> Blocks,
182 SplitBlockCache *Cache) {
183 assert(!Blocks.empty() && "Blocks must at least contain the original block");
184
185 // Fix up content of all blocks.
186 ArrayRef<char> Content = Blocks.front()->getContent();
187 for (size_t I = 0; I != Blocks.size() - 1; ++I) {
188 Blocks[I]->setContent(
189 Content.slice(Blocks[I]->getAddress() - Blocks[0]->getAddress(),
190 Blocks[I + 1]->getAddress() - Blocks[I]->getAddress()));
191 }
192 Blocks.back()->setContent(
193 Content.slice(Blocks.back()->getAddress() - Blocks[0]->getAddress()));
194 bool IsMutable = Blocks[0]->ContentMutable;
195 for (auto *B : Blocks)
196 B->ContentMutable = IsMutable;
197
198 // Transfer symbols.
199 {
200 SplitBlockCache LocalBlockSymbolsCache;
201 if (!Cache)
202 Cache = &LocalBlockSymbolsCache;
203
204 // Build cache if required.
205 if (*Cache == std::nullopt) {
206 *Cache = SplitBlockCache::value_type();
207
208 for (auto *Sym : Blocks[0]->getSection().symbols())
209 if (&Sym->getBlock() == Blocks[0])
210 (*Cache)->push_back(Sym);
211 llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
212 return LHS->getAddress() > RHS->getAddress();
213 });
214 }
215
216 auto TransferSymbol = [](Symbol &Sym, Block &B) {
217 Sym.setOffset(Sym.getAddress() - B.getAddress());
218 Sym.setBlock(B);
219 if (Sym.getSize() > B.getSize())
220 Sym.setSize(B.getSize() - Sym.getOffset());
221 };
222
223 // Transfer symbols to all blocks except the last one.
224 for (size_t I = 0; I != Blocks.size() - 1; ++I) {
225 if ((*Cache)->empty())
226 break;
227 while (!(*Cache)->empty() &&
228 (*Cache)->back()->getAddress() < Blocks[I + 1]->getAddress()) {
229 TransferSymbol(*(*Cache)->back(), *Blocks[I]);
230 (*Cache)->pop_back();
231 }
232 }
233 // Transfer symbols to the last block, checking that all are in-range.
234 while (!(*Cache)->empty()) {
235 auto &Sym = *(*Cache)->back();
236 (*Cache)->pop_back();
237 assert(Sym.getAddress() >= Blocks.back()->getAddress() &&
238 "Symbol address preceeds block");
239 assert(Sym.getAddress() <= Blocks.back()->getRange().End &&
240 "Symbol address starts past end of block");
241 TransferSymbol(Sym, *Blocks.back());
242 }
243 }
244
245 // Transfer edges.
246 auto &Edges = Blocks[0]->Edges;
247 llvm::sort(Edges, [](const Edge &LHS, const Edge &RHS) {
248 return LHS.getOffset() < RHS.getOffset();
249 });
250
251 for (size_t I = Blocks.size() - 1; I != 0; --I) {
252
253 // If all edges have been transferred then bail out.
254 if (Edges.empty())
255 break;
256
257 Edge::OffsetT Delta = Blocks[I]->getAddress() - Blocks[0]->getAddress();
258
259 // If no edges to move for this block then move to the next one.
260 if (Edges.back().getOffset() < Delta)
261 continue;
262
263 size_t EI = Edges.size() - 1;
264 while (EI != 0 && Edges[EI - 1].getOffset() >= Delta)
265 --EI;
266
267 for (size_t J = EI; J != Edges.size(); ++J) {
268 Blocks[I]->Edges.push_back(std::move(Edges[J]));
269 Blocks[I]->Edges.back().setOffset(Blocks[I]->Edges.back().getOffset() -
270 Delta);
271 }
272
273 while (Edges.size() > EI)
274 Edges.pop_back();
275 }
276
277 return Blocks;
278}
279
282
283 OS << "LinkGraph \"" << getName()
284 << "\" (triple = " << getTargetTriple().str() << ")\n";
285
286 // Map from blocks to the symbols pointing at them.
287 for (auto *Sym : defined_symbols())
288 BlockSymbols[&Sym->getBlock()].push_back(Sym);
289
290 // For each block, sort its symbols by something approximating
291 // relevance.
292 for (auto &KV : BlockSymbols)
293 llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
294 if (LHS->getOffset() != RHS->getOffset())
295 return LHS->getOffset() < RHS->getOffset();
296 if (LHS->getLinkage() != RHS->getLinkage())
297 return LHS->getLinkage() < RHS->getLinkage();
298 if (LHS->getScope() != RHS->getScope())
299 return LHS->getScope() < RHS->getScope();
300 if (LHS->hasName()) {
301 if (!RHS->hasName())
302 return true;
303 return LHS->getName() < RHS->getName();
304 }
305 return false;
306 });
307
308 std::vector<Section *> SortedSections;
309 for (auto &Sec : sections())
310 SortedSections.push_back(&Sec);
311 llvm::sort(SortedSections, [](const Section *LHS, const Section *RHS) {
312 return LHS->getName() < RHS->getName();
313 });
314
315 for (auto *Sec : SortedSections) {
316 OS << "section " << Sec->getName() << ":\n\n";
317
318 std::vector<Block *> SortedBlocks;
319 llvm::append_range(SortedBlocks, Sec->blocks());
320 llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
321 return LHS->getAddress() < RHS->getAddress();
322 });
323
324 for (auto *B : SortedBlocks) {
325 OS << " block " << B->getAddress()
326 << " size = " << formatv("{0:x8}", B->getSize())
327 << ", align = " << B->getAlignment()
328 << ", alignment-offset = " << B->getAlignmentOffset();
329 if (B->isZeroFill())
330 OS << ", zero-fill";
331 OS << "\n";
332
333 auto BlockSymsI = BlockSymbols.find(B);
334 if (BlockSymsI != BlockSymbols.end()) {
335 OS << " symbols:\n";
336 auto &Syms = BlockSymsI->second;
337 for (auto *Sym : Syms)
338 OS << " " << *Sym << "\n";
339 } else
340 OS << " no symbols\n";
341
342 if (!B->edges_empty()) {
343 OS << " edges:\n";
344 std::vector<Edge> SortedEdges;
345 llvm::append_range(SortedEdges, B->edges());
346 llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
347 return LHS.getOffset() < RHS.getOffset();
348 });
349 for (auto &E : SortedEdges) {
350 OS << " " << B->getFixupAddress(E) << " (block + "
351 << formatv("{0:x8}", E.getOffset()) << "), addend = ";
352 if (E.getAddend() >= 0)
353 OS << formatv("+{0:x8}", E.getAddend());
354 else
355 OS << formatv("-{0:x8}", -E.getAddend());
356 OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
357 if (E.getTarget().hasName())
358 OS << E.getTarget().getName();
359 else
360 OS << "addressable@"
361 << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
362 << formatv("{0:x8}", E.getTarget().getOffset());
363 OS << "\n";
364 }
365 } else
366 OS << " no edges\n";
367 OS << "\n";
368 }
369 }
370
371 OS << "Absolute symbols:\n";
372 if (!absolute_symbols().empty()) {
373 for (auto *Sym : absolute_symbols())
374 OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
375 } else
376 OS << " none\n";
377
378 OS << "\nExternal symbols:\n";
379 if (!external_symbols().empty()) {
380 for (auto *Sym : external_symbols())
381 OS << " " << Sym->getAddress() << ": " << *Sym
382 << (Sym->isWeaklyReferenced() ? " (weakly referenced)" : "") << "\n";
383 } else
384 OS << " none\n";
385}
386
388 switch (LF) {
390 return OS << "RequiredSymbol";
392 return OS << "WeaklyReferencedSymbol";
393 }
394 llvm_unreachable("Unrecognized lookup flags");
395}
396
397void JITLinkAsyncLookupContinuation::anchor() {}
398
399JITLinkContext::~JITLinkContext() = default;
400
402 return true;
403}
404
408
413
415 for (auto *Sym : G.defined_symbols())
416 Sym->setLive(true);
417 return Error::success();
418}
419
421 const Edge &E) {
422 std::string ErrMsg;
423 {
424 raw_string_ostream ErrStream(ErrMsg);
425 Section &Sec = B.getSection();
426 ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
427 << ": relocation target "
428 << formatv("{0:x}", E.getTarget().getAddress() + E.getAddend())
429 << " (";
430 if (E.getTarget().hasName())
431 ErrStream << E.getTarget().getName();
432 else
433 ErrStream << "<anonymous symbol>";
434 if (E.getAddend()) {
435 // Target address includes non-zero added, so break down the arithmetic.
436 ErrStream << formatv(":{0:x}", E.getTarget().getAddress()) << " + "
437 << formatv("{0:x}", E.getAddend());
438 }
439 ErrStream << ") is out of range of " << G.getEdgeKindName(E.getKind())
440 << " fixup at address "
441 << formatv("{0:x}", E.getTarget().getAddress()) << " (";
442
443 Symbol *BestSymbolForBlock = nullptr;
444 for (auto *Sym : Sec.symbols())
445 if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
446 (!BestSymbolForBlock ||
447 Sym->getScope() < BestSymbolForBlock->getScope() ||
448 Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
449 BestSymbolForBlock = Sym;
450
451 if (BestSymbolForBlock)
452 ErrStream << BestSymbolForBlock->getName() << ", ";
453 else
454 ErrStream << "<anonymous block> @ ";
455
456 ErrStream << formatv("{0:x}", B.getAddress()) << " + "
457 << formatv("{0:x}", E.getOffset()) << ")";
458 }
459 return make_error<JITLinkError>(std::move(ErrMsg));
460}
461
463 const Edge &E) {
464 return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
465 " improper alignment for relocation " +
466 formatv("{0:d}", E.getKind()) + ": 0x" +
468 " is not aligned to " + Twine(N) + " bytes");
469}
470
472 switch (TT.getArch()) {
473 case Triple::aarch64:
475 case Triple::x86_64:
477 case Triple::x86:
482 default:
483 return nullptr;
484 }
485}
486
488 switch (TT.getArch()) {
489 case Triple::aarch64:
491 case Triple::x86_64:
493 case Triple::x86:
498 default:
499 return nullptr;
500 }
501}
502
505 std::shared_ptr<orc::SymbolStringPool> SSP) {
506 auto Magic = identify_magic(ObjectBuffer.getBuffer());
507 switch (Magic) {
509 return createLinkGraphFromMachOObject(ObjectBuffer, std::move(SSP));
511 return createLinkGraphFromELFObject(ObjectBuffer, std::move(SSP));
513 return createLinkGraphFromCOFFObject(ObjectBuffer, std::move(SSP));
515 return createLinkGraphFromXCOFFObject(ObjectBuffer, std::move(SSP));
516 default:
517 return make_error<JITLinkError>("Unsupported file format");
518 };
519}
520
521std::unique_ptr<LinkGraph>
522absoluteSymbolsLinkGraph(Triple TT, std::shared_ptr<orc::SymbolStringPool> SSP,
523 orc::SymbolMap Symbols) {
524 static std::atomic<uint64_t> Counter = {0};
525 auto Index = Counter.fetch_add(1, std::memory_order_relaxed);
526 auto G = std::make_unique<LinkGraph>(
527 "<Absolute Symbols " + std::to_string(Index) + ">", std::move(SSP),
528 std::move(TT), SubtargetFeatures(), getGenericEdgeKindName);
529 for (auto &[Name, Def] : Symbols) {
530 auto &Sym =
531 G->addAbsoluteSymbol(*Name, Def.getAddress(), /*Size=*/0,
532 Linkage::Strong, Scope::Default, /*IsLive=*/true);
533 Sym.setCallable(Def.getFlags().isCallable());
534 }
535
536 return G;
537}
538
539void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
540 switch (G->getTargetTriple().getObjectFormat()) {
541 case Triple::MachO:
542 return link_MachO(std::move(G), std::move(Ctx));
543 case Triple::ELF:
544 return link_ELF(std::move(G), std::move(Ctx));
545 case Triple::COFF:
546 return link_COFF(std::move(G), std::move(Ctx));
547 case Triple::XCOFF:
548 return link_XCOFF(std::move(G), std::move(Ctx));
549 default:
550 Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
551 };
552}
553
554} // end namespace jitlink
555} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
bbsections Prepares for basic block sections
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
static const char * name
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some functions that are useful when dealing with strings.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
@ loongarch32
Definition Triple.h:64
@ loongarch64
Definition Triple.h:65
const std::string & str() const
Definition Triple.h:478
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
Represents an address in the executor process.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition ELF.h:541
uint64_t ExecutorAddrDiff
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
bool empty() const
Definition BasicBlock.h:101
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition Magic.cpp:33
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2138
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
#define N
@ elf_relocatable
ELF Relocatable object file.
Definition Magic.h:28
@ xcoff_object_64
64-bit XCOFF object file
Definition Magic.h:53
@ macho_object
Mach-O Object file.
Definition Magic.h:33
@ coff_object
COFF object file.
Definition Magic.h:48