LLVM  16.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 
15 #include "llvm/Support/Format.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 
22 #define DEBUG_TYPE "jitlink"
23 
24 namespace {
25 
26 enum JITLinkErrorCode { GenericJITLinkError = 1 };
27 
28 // FIXME: This class is only here to support the transition to llvm::Error. It
29 // will be removed once this transition is complete. Clients should prefer to
30 // deal with the Error value directly, rather than converting to error_code.
31 class JITLinkerErrorCategory : public std::error_category {
32 public:
33  const char *name() const noexcept override { return "runtimedyld"; }
34 
35  std::string message(int Condition) const override {
36  switch (static_cast<JITLinkErrorCode>(Condition)) {
37  case GenericJITLinkError:
38  return "Generic JITLink error";
39  }
40  llvm_unreachable("Unrecognized JITLinkErrorCode");
41  }
42 };
43 
44 } // namespace
45 
46 namespace llvm {
47 namespace jitlink {
48 
49 char JITLinkError::ID = 0;
50 
51 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
52 
53 std::error_code JITLinkError::convertToErrorCode() const {
54  static JITLinkerErrorCategory TheJITLinkerErrorCategory;
55  return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
56 }
57 
59  switch (K) {
60  case Edge::Invalid:
61  return "INVALID RELOCATION";
62  case Edge::KeepAlive:
63  return "Keep-Alive";
64  default:
65  return "<Unrecognized edge kind>";
66  }
67 }
68 
69 const char *getLinkageName(Linkage L) {
70  switch (L) {
71  case Linkage::Strong:
72  return "strong";
73  case Linkage::Weak:
74  return "weak";
75  }
76  llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
77 }
78 
79 const char *getScopeName(Scope S) {
80  switch (S) {
81  case Scope::Default:
82  return "default";
83  case Scope::Hidden:
84  return "hidden";
85  case Scope::Local:
86  return "local";
87  }
88  llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
89 }
90 
92  return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
93  << ": "
94  << "size = " << formatv("{0:x8}", B.getSize()) << ", "
95  << (B.isZeroFill() ? "zero-fill" : "content")
96  << ", align = " << B.getAlignment()
97  << ", align-ofs = " << B.getAlignmentOffset()
98  << ", section = " << B.getSection().getName();
99 }
100 
102  OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
103  << " + " << formatv("{0:x8}", Sym.getOffset())
104  << "): size: " << formatv("{0:x8}", Sym.getSize())
105  << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
106  << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
107  << (Sym.isLive() ? "live" : "dead") << " - "
108  << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
109  return OS;
110 }
111 
112 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
113  StringRef EdgeKindName) {
114  OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
115  << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
116  << " -> ";
117 
118  auto &TargetSym = E.getTarget();
119  if (TargetSym.hasName())
120  OS << TargetSym.getName();
121  else {
122  auto &TargetBlock = TargetSym.getBlock();
123  auto &TargetSec = TargetBlock.getSection();
124  orc::ExecutorAddr SecAddress(~uint64_t(0));
125  for (auto *B : TargetSec.blocks())
126  if (B->getAddress() < SecAddress)
127  SecAddress = B->getAddress();
128 
129  orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
130  OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
131  if (SecDelta)
132  OS << " + " << formatv("{0:x}", SecDelta);
133  OS << " / block " << TargetBlock.getAddress();
134  if (TargetSym.getOffset())
135  OS << " + " << formatv("{0:x}", TargetSym.getOffset());
136  OS << ")";
137  }
138 
139  if (E.getAddend() != 0)
140  OS << " + " << E.getAddend();
141 }
142 
143 Section::~Section() {
144  for (auto *Sym : Symbols)
145  Sym->~Symbol();
146  for (auto *B : Blocks)
147  B->~Block();
148 }
149 
150 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
151  SplitBlockCache *Cache) {
152 
153  assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
154 
155  // If the split point covers all of B then just return B.
156  if (SplitIndex == B.getSize())
157  return B;
158 
159  assert(SplitIndex < B.getSize() && "SplitIndex out of range");
160 
161  // Create the new block covering [ 0, SplitIndex ).
162  auto &NewBlock =
163  B.isZeroFill()
164  ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
165  B.getAlignment(), B.getAlignmentOffset())
166  : createContentBlock(
167  B.getSection(), B.getContent().slice(0, SplitIndex),
168  B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
169 
170  // Modify B to cover [ SplitIndex, B.size() ).
171  B.setAddress(B.getAddress() + SplitIndex);
172  B.setContent(B.getContent().slice(SplitIndex));
173  B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
174  B.getAlignment());
175 
176  // Handle edge transfer/update.
177  {
178  // Copy edges to NewBlock (recording their iterators so that we can remove
179  // them from B), and update of Edges remaining on B.
180  std::vector<Block::edge_iterator> EdgesToRemove;
181  for (auto I = B.edges().begin(); I != B.edges().end();) {
182  if (I->getOffset() < SplitIndex) {
183  NewBlock.addEdge(*I);
184  I = B.removeEdge(I);
185  } else {
186  I->setOffset(I->getOffset() - SplitIndex);
187  ++I;
188  }
189  }
190  }
191 
192  // Handle symbol transfer/update.
193  {
194  // Initialize the symbols cache if necessary.
195  SplitBlockCache LocalBlockSymbolsCache;
196  if (!Cache)
197  Cache = &LocalBlockSymbolsCache;
198  if (*Cache == None) {
199  *Cache = SplitBlockCache::value_type();
200  for (auto *Sym : B.getSection().symbols())
201  if (&Sym->getBlock() == &B)
202  (*Cache)->push_back(Sym);
203 
204  llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
205  return LHS->getOffset() > RHS->getOffset();
206  });
207  }
208  auto &BlockSymbols = **Cache;
209 
210  // Transfer all symbols with offset less than SplitIndex to NewBlock.
211  while (!BlockSymbols.empty() &&
212  BlockSymbols.back()->getOffset() < SplitIndex) {
213  auto *Sym = BlockSymbols.back();
214  // If the symbol extends beyond the split, update the size to be within
215  // the new block.
216  if (Sym->getOffset() + Sym->getSize() > SplitIndex)
217  Sym->setSize(SplitIndex - Sym->getOffset());
218  Sym->setBlock(NewBlock);
219  BlockSymbols.pop_back();
220  }
221 
222  // Update offsets for all remaining symbols in B.
223  for (auto *Sym : BlockSymbols)
224  Sym->setOffset(Sym->getOffset() - SplitIndex);
225  }
226 
227  return NewBlock;
228 }
229 
232 
233  // Map from blocks to the symbols pointing at them.
234  for (auto *Sym : defined_symbols())
235  BlockSymbols[&Sym->getBlock()].push_back(Sym);
236 
237  // For each block, sort its symbols by something approximating
238  // relevance.
239  for (auto &KV : BlockSymbols)
240  llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
241  if (LHS->getOffset() != RHS->getOffset())
242  return LHS->getOffset() < RHS->getOffset();
243  if (LHS->getLinkage() != RHS->getLinkage())
244  return LHS->getLinkage() < RHS->getLinkage();
245  if (LHS->getScope() != RHS->getScope())
246  return LHS->getScope() < RHS->getScope();
247  if (LHS->hasName()) {
248  if (!RHS->hasName())
249  return true;
250  return LHS->getName() < RHS->getName();
251  }
252  return false;
253  });
254 
255  for (auto &Sec : sections()) {
256  OS << "section " << Sec.getName() << ":\n\n";
257 
258  std::vector<Block *> SortedBlocks;
259  llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
260  llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
261  return LHS->getAddress() < RHS->getAddress();
262  });
263 
264  for (auto *B : SortedBlocks) {
265  OS << " block " << B->getAddress()
266  << " size = " << formatv("{0:x8}", B->getSize())
267  << ", align = " << B->getAlignment()
268  << ", alignment-offset = " << B->getAlignmentOffset();
269  if (B->isZeroFill())
270  OS << ", zero-fill";
271  OS << "\n";
272 
273  auto BlockSymsI = BlockSymbols.find(B);
274  if (BlockSymsI != BlockSymbols.end()) {
275  OS << " symbols:\n";
276  auto &Syms = BlockSymsI->second;
277  for (auto *Sym : Syms)
278  OS << " " << *Sym << "\n";
279  } else
280  OS << " no symbols\n";
281 
282  if (!B->edges_empty()) {
283  OS << " edges:\n";
284  std::vector<Edge> SortedEdges;
285  llvm::copy(B->edges(), std::back_inserter(SortedEdges));
286  llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
287  return LHS.getOffset() < RHS.getOffset();
288  });
289  for (auto &E : SortedEdges) {
290  OS << " " << B->getFixupAddress(E) << " (block + "
291  << formatv("{0:x8}", E.getOffset()) << "), addend = ";
292  if (E.getAddend() >= 0)
293  OS << formatv("+{0:x8}", E.getAddend());
294  else
295  OS << formatv("-{0:x8}", -E.getAddend());
296  OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
297  if (E.getTarget().hasName())
298  OS << E.getTarget().getName();
299  else
300  OS << "addressable@"
301  << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
302  << formatv("{0:x8}", E.getTarget().getOffset());
303  OS << "\n";
304  }
305  } else
306  OS << " no edges\n";
307  OS << "\n";
308  }
309  }
310 
311  OS << "Absolute symbols:\n";
312  if (!absolute_symbols().empty()) {
313  for (auto *Sym : absolute_symbols())
314  OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
315  } else
316  OS << " none\n";
317 
318  OS << "\nExternal symbols:\n";
319  if (!external_symbols().empty()) {
320  for (auto *Sym : external_symbols())
321  OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
322  } else
323  OS << " none\n";
324 }
325 
327  switch (LF) {
328  case SymbolLookupFlags::RequiredSymbol:
329  return OS << "RequiredSymbol";
330  case SymbolLookupFlags::WeaklyReferencedSymbol:
331  return OS << "WeaklyReferencedSymbol";
332  }
333  llvm_unreachable("Unrecognized lookup flags");
334 }
335 
336 void JITLinkAsyncLookupContinuation::anchor() {}
337 
338 JITLinkContext::~JITLinkContext() = default;
339 
340 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
341  return true;
342 }
343 
344 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
345  return LinkGraphPassFunction();
346 }
347 
348 Error JITLinkContext::modifyPassConfig(LinkGraph &G,
349  PassConfiguration &Config) {
350  return Error::success();
351 }
352 
354  for (auto *Sym : G.defined_symbols())
355  Sym->setLive(true);
356  return Error::success();
357 }
358 
360  const Edge &E) {
361  std::string ErrMsg;
362  {
363  raw_string_ostream ErrStream(ErrMsg);
364  Section &Sec = B.getSection();
365  ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
366  << ": relocation target ";
367  if (E.getTarget().hasName()) {
368  ErrStream << "\"" << E.getTarget().getName() << "\"";
369  } else
370  ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
371  << formatv("{0:x}", E.getOffset());
372  ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
373  << " is out of range of " << G.getEdgeKindName(E.getKind())
374  << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
375 
376  Symbol *BestSymbolForBlock = nullptr;
377  for (auto *Sym : Sec.symbols())
378  if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
379  (!BestSymbolForBlock ||
380  Sym->getScope() < BestSymbolForBlock->getScope() ||
381  Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
382  BestSymbolForBlock = Sym;
383 
384  if (BestSymbolForBlock)
385  ErrStream << BestSymbolForBlock->getName() << ", ";
386  else
387  ErrStream << "<anonymous block> @ ";
388 
389  ErrStream << formatv("{0:x}", B.getAddress()) << " + "
390  << formatv("{0:x}", E.getOffset()) << ")";
391  }
392  return make_error<JITLinkError>(std::move(ErrMsg));
393 }
394 
396  const Edge &E) {
397  return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
398  " improper alignment for relocation " +
399  formatv("{0:d}", E.getKind()) + ": 0x" +
400  llvm::utohexstr(Value) +
401  " is not aligned to " + Twine(N) + " bytes");
402 }
403 
406  auto Magic = identify_magic(ObjectBuffer.getBuffer());
407  switch (Magic) {
409  return createLinkGraphFromMachOObject(ObjectBuffer);
411  return createLinkGraphFromELFObject(ObjectBuffer);
413  return createLinkGraphFromCOFFObject(ObjectBuffer);
414  default:
415  return make_error<JITLinkError>("Unsupported file format");
416  };
417 }
418 
419 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
420  switch (G->getTargetTriple().getObjectFormat()) {
421  case Triple::MachO:
422  return link_MachO(std::move(G), std::move(Ctx));
423  case Triple::ELF:
424  return link_ELF(std::move(G), std::move(Ctx));
425  case Triple::COFF:
426  return link_COFF(std::move(G), std::move(Ctx));
427  default:
428  Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
429  };
430 }
431 
432 } // end namespace jitlink
433 } // end namespace llvm
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:31
MemoryBuffer.h
llvm::Optional::value_type
T value_type
Definition: Optional.h:273
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:628
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::orc::ExecutorAddr::getValue
uint64_t getValue() const
Definition: ExecutorAddress.h:105
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1641
llvm::Optional
Definition: APInt.h:33
splitBlock
static void splitBlock(MachineBasicBlock &MBB, MachineInstr &MI, MachineDominatorTree *MDT)
Definition: SILateBranchLowering.cpp:104
llvm::dump
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Definition: SparseBitVector.h:877
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
Format.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:251
llvm::object
Definition: DWARFDebugLoc.h:25
Magic.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::None
const NoneType None
Definition: None.h:24
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:26
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1538
llvm::object::operator<<
raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:34
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
getLinkageName
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
Definition: AsmWriter.cpp:3117
llvm::Triple::ELF
@ ELF
Definition: Triple.h:275
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition: Magic.h:46
I
#define I(x, y, z)
Definition: MD5.cpp:58
message
message(STATUS "Targeting ${t}") add_subdirectory($
Definition: CMakeLists.txt:34
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::msf::Magic
static const char Magic[]
Definition: MSFCommon.h:23
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:74
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::empty
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:256
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::Triple::MachO
@ MachO
Definition: Triple.h:277
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::Triple::COFF
@ COFF
Definition: Triple.h:273
Invalid
@ Invalid
Definition: AArch64ISelLowering.cpp:9324
N
#define N
raw_ostream.h
llvm::identify_magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition: Magic.h:31
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38