LLVM  15.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 
14 #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 static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
45 
46 } // namespace
47 
48 namespace llvm {
49 namespace jitlink {
50 
51 char JITLinkError::ID = 0;
52 
53 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
54 
55 std::error_code JITLinkError::convertToErrorCode() const {
56  return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
57 }
58 
60  switch (K) {
61  case Edge::Invalid:
62  return "INVALID RELOCATION";
63  case Edge::KeepAlive:
64  return "Keep-Alive";
65  default:
66  return "<Unrecognized edge kind>";
67  }
68 }
69 
70 const char *getLinkageName(Linkage L) {
71  switch (L) {
72  case Linkage::Strong:
73  return "strong";
74  case Linkage::Weak:
75  return "weak";
76  }
77  llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
78 }
79 
80 const char *getScopeName(Scope S) {
81  switch (S) {
82  case Scope::Default:
83  return "default";
84  case Scope::Hidden:
85  return "hidden";
86  case Scope::Local:
87  return "local";
88  }
89  llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
90 }
91 
93  return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
94  << ": "
95  << "size = " << formatv("{0:x8}", B.getSize()) << ", "
96  << (B.isZeroFill() ? "zero-fill" : "content")
97  << ", align = " << B.getAlignment()
98  << ", align-ofs = " << B.getAlignmentOffset()
99  << ", section = " << B.getSection().getName();
100 }
101 
103  OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
104  << " + " << formatv("{0:x8}", Sym.getOffset())
105  << "): size: " << formatv("{0:x8}", Sym.getSize())
106  << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
107  << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
108  << (Sym.isLive() ? "live" : "dead") << " - "
109  << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
110  return OS;
111 }
112 
113 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
114  StringRef EdgeKindName) {
115  OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
116  << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
117  << " -> ";
118 
119  auto &TargetSym = E.getTarget();
120  if (TargetSym.hasName())
121  OS << TargetSym.getName();
122  else {
123  auto &TargetBlock = TargetSym.getBlock();
124  auto &TargetSec = TargetBlock.getSection();
125  orc::ExecutorAddr SecAddress(~uint64_t(0));
126  for (auto *B : TargetSec.blocks())
127  if (B->getAddress() < SecAddress)
128  SecAddress = B->getAddress();
129 
130  orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
131  OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
132  if (SecDelta)
133  OS << " + " << formatv("{0:x}", SecDelta);
134  OS << " / block " << TargetBlock.getAddress();
135  if (TargetSym.getOffset())
136  OS << " + " << formatv("{0:x}", TargetSym.getOffset());
137  OS << ")";
138  }
139 
140  if (E.getAddend() != 0)
141  OS << " + " << E.getAddend();
142 }
143 
144 Section::~Section() {
145  for (auto *Sym : Symbols)
146  Sym->~Symbol();
147  for (auto *B : Blocks)
148  B->~Block();
149 }
150 
151 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
152  SplitBlockCache *Cache) {
153 
154  assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
155 
156  // If the split point covers all of B then just return B.
157  if (SplitIndex == B.getSize())
158  return B;
159 
160  assert(SplitIndex < B.getSize() && "SplitIndex out of range");
161 
162  // Create the new block covering [ 0, SplitIndex ).
163  auto &NewBlock =
164  B.isZeroFill()
165  ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
166  B.getAlignment(), B.getAlignmentOffset())
167  : createContentBlock(
168  B.getSection(), B.getContent().slice(0, SplitIndex),
169  B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
170 
171  // Modify B to cover [ SplitIndex, B.size() ).
172  B.setAddress(B.getAddress() + SplitIndex);
173  B.setContent(B.getContent().slice(SplitIndex));
174  B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
175  B.getAlignment());
176 
177  // Handle edge transfer/update.
178  {
179  // Copy edges to NewBlock (recording their iterators so that we can remove
180  // them from B), and update of Edges remaining on B.
181  std::vector<Block::edge_iterator> EdgesToRemove;
182  for (auto I = B.edges().begin(); I != B.edges().end();) {
183  if (I->getOffset() < SplitIndex) {
184  NewBlock.addEdge(*I);
185  I = B.removeEdge(I);
186  } else {
187  I->setOffset(I->getOffset() - SplitIndex);
188  ++I;
189  }
190  }
191  }
192 
193  // Handle symbol transfer/update.
194  {
195  // Initialize the symbols cache if necessary.
196  SplitBlockCache LocalBlockSymbolsCache;
197  if (!Cache)
198  Cache = &LocalBlockSymbolsCache;
199  if (*Cache == None) {
200  *Cache = SplitBlockCache::value_type();
201  for (auto *Sym : B.getSection().symbols())
202  if (&Sym->getBlock() == &B)
203  (*Cache)->push_back(Sym);
204 
205  llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
206  return LHS->getOffset() > RHS->getOffset();
207  });
208  }
209  auto &BlockSymbols = **Cache;
210 
211  // Transfer all symbols with offset less than SplitIndex to NewBlock.
212  while (!BlockSymbols.empty() &&
213  BlockSymbols.back()->getOffset() < SplitIndex) {
214  auto *Sym = BlockSymbols.back();
215  // If the symbol extends beyond the split, update the size to be within
216  // the new block.
217  if (Sym->getOffset() + Sym->getSize() > SplitIndex)
218  Sym->setSize(SplitIndex - Sym->getOffset());
219  Sym->setBlock(NewBlock);
220  BlockSymbols.pop_back();
221  }
222 
223  // Update offsets for all remaining symbols in B.
224  for (auto *Sym : BlockSymbols)
225  Sym->setOffset(Sym->getOffset() - SplitIndex);
226  }
227 
228  return NewBlock;
229 }
230 
233 
234  // Map from blocks to the symbols pointing at them.
235  for (auto *Sym : defined_symbols())
236  BlockSymbols[&Sym->getBlock()].push_back(Sym);
237 
238  // For each block, sort its symbols by something approximating
239  // relevance.
240  for (auto &KV : BlockSymbols)
241  llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
242  if (LHS->getOffset() != RHS->getOffset())
243  return LHS->getOffset() < RHS->getOffset();
244  if (LHS->getLinkage() != RHS->getLinkage())
245  return LHS->getLinkage() < RHS->getLinkage();
246  if (LHS->getScope() != RHS->getScope())
247  return LHS->getScope() < RHS->getScope();
248  if (LHS->hasName()) {
249  if (!RHS->hasName())
250  return true;
251  return LHS->getName() < RHS->getName();
252  }
253  return false;
254  });
255 
256  for (auto &Sec : sections()) {
257  OS << "section " << Sec.getName() << ":\n\n";
258 
259  std::vector<Block *> SortedBlocks;
260  llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
261  llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
262  return LHS->getAddress() < RHS->getAddress();
263  });
264 
265  for (auto *B : SortedBlocks) {
266  OS << " block " << B->getAddress()
267  << " size = " << formatv("{0:x8}", B->getSize())
268  << ", align = " << B->getAlignment()
269  << ", alignment-offset = " << B->getAlignmentOffset();
270  if (B->isZeroFill())
271  OS << ", zero-fill";
272  OS << "\n";
273 
274  auto BlockSymsI = BlockSymbols.find(B);
275  if (BlockSymsI != BlockSymbols.end()) {
276  OS << " symbols:\n";
277  auto &Syms = BlockSymsI->second;
278  for (auto *Sym : Syms)
279  OS << " " << *Sym << "\n";
280  } else
281  OS << " no symbols\n";
282 
283  if (!B->edges_empty()) {
284  OS << " edges:\n";
285  std::vector<Edge> SortedEdges;
286  llvm::copy(B->edges(), std::back_inserter(SortedEdges));
287  llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
288  return LHS.getOffset() < RHS.getOffset();
289  });
290  for (auto &E : SortedEdges) {
291  OS << " " << B->getFixupAddress(E) << " (block + "
292  << formatv("{0:x8}", E.getOffset()) << "), addend = ";
293  if (E.getAddend() >= 0)
294  OS << formatv("+{0:x8}", E.getAddend());
295  else
296  OS << formatv("-{0:x8}", -E.getAddend());
297  OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
298  if (E.getTarget().hasName())
299  OS << E.getTarget().getName();
300  else
301  OS << "addressable@"
302  << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
303  << formatv("{0:x8}", E.getTarget().getOffset());
304  OS << "\n";
305  }
306  } else
307  OS << " no edges\n";
308  OS << "\n";
309  }
310  }
311 
312  OS << "Absolute symbols:\n";
313  if (!llvm::empty(absolute_symbols())) {
314  for (auto *Sym : absolute_symbols())
315  OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
316  } else
317  OS << " none\n";
318 
319  OS << "\nExternal symbols:\n";
320  if (!llvm::empty(external_symbols())) {
321  for (auto *Sym : external_symbols())
322  OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
323  } else
324  OS << " none\n";
325 }
326 
328  switch (LF) {
329  case SymbolLookupFlags::RequiredSymbol:
330  return OS << "RequiredSymbol";
331  case SymbolLookupFlags::WeaklyReferencedSymbol:
332  return OS << "WeaklyReferencedSymbol";
333  }
334  llvm_unreachable("Unrecognized lookup flags");
335 }
336 
337 void JITLinkAsyncLookupContinuation::anchor() {}
338 
339 JITLinkContext::~JITLinkContext() = default;
340 
341 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
342  return true;
343 }
344 
345 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
346  return LinkGraphPassFunction();
347 }
348 
349 Error JITLinkContext::modifyPassConfig(LinkGraph &G,
350  PassConfiguration &Config) {
351  return Error::success();
352 }
353 
355  for (auto *Sym : G.defined_symbols())
356  Sym->setLive(true);
357  return Error::success();
358 }
359 
361  const Edge &E) {
362  std::string ErrMsg;
363  {
364  raw_string_ostream ErrStream(ErrMsg);
365  Section &Sec = B.getSection();
366  ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
367  << ": relocation target ";
368  if (E.getTarget().hasName()) {
369  ErrStream << "\"" << E.getTarget().getName() << "\"";
370  } else
371  ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
372  << formatv("{0:x}", E.getOffset());
373  ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
374  << " is out of range of " << G.getEdgeKindName(E.getKind())
375  << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
376 
377  Symbol *BestSymbolForBlock = nullptr;
378  for (auto *Sym : Sec.symbols())
379  if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
380  (!BestSymbolForBlock ||
381  Sym->getScope() < BestSymbolForBlock->getScope() ||
382  Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
383  BestSymbolForBlock = Sym;
384 
385  if (BestSymbolForBlock)
386  ErrStream << BestSymbolForBlock->getName() << ", ";
387  else
388  ErrStream << "<anonymous block> @ ";
389 
390  ErrStream << formatv("{0:x}", B.getAddress()) << " + "
391  << formatv("{0:x}", E.getOffset()) << ")";
392  }
393  return make_error<JITLinkError>(std::move(ErrMsg));
394 }
395 
397  const Edge &E) {
398  return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
399  " improper alignment for relocation " +
400  formatv("{0:d}", E.getKind()) + ": 0x" +
401  llvm::utohexstr(Value) +
402  " is not aligned to " + Twine(N) + " bytes");
403 }
404 
407  auto Magic = identify_magic(ObjectBuffer.getBuffer());
408  switch (Magic) {
410  return createLinkGraphFromMachOObject(ObjectBuffer);
412  return createLinkGraphFromELFObject(ObjectBuffer);
413  default:
414  return make_error<JITLinkError>("Unsupported file format");
415  };
416 }
417 
418 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
419  switch (G->getTargetTriple().getObjectFormat()) {
420  case Triple::MachO:
421  return link_MachO(std::move(G), std::move(Ctx));
422  case Triple::ELF:
423  return link_ELF(std::move(G), std::move(Ctx));
424  default:
425  Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
426  };
427 }
428 
429 } // end namespace jitlink
430 } // end namespace llvm
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:30
MemoryBuffer.h
llvm::Optional::value_type
T value_type
Definition: Optional.h:239
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
ManagedStatic.h
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:62
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1656
llvm::Optional
Definition: APInt.h:33
splitBlock
static void splitBlock(MachineBasicBlock &MBB, MachineInstr &MI, MachineDominatorTree *MDT)
Definition: SILateBranchLowering.cpp:98
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")
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
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:54
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::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:3115
llvm::Triple::ELF
@ ELF
Definition: Triple.h:266
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:716
I
#define I(x, y, z)
Definition: MD5.cpp:58
message
message(STATUS "Targeting ${t}") add_subdirectory($
Definition: CMakeLists.txt:33
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:58
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:83
llvm::empty
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:268
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::Triple::MachO
@ MachO
Definition: Triple.h:268
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1550
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
Invalid
@ Invalid
Definition: AArch64ISelLowering.cpp:8685
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
error_category
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:76
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:37