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