LLVM  13.0.0git
JITLink.cpp
Go to the documentation of this file.
1 //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
15 #include "llvm/Support/Format.h"
19 
20 using namespace llvm;
21 using namespace llvm::object;
22 
23 #define DEBUG_TYPE "jitlink"
24 
25 namespace {
26 
27 enum JITLinkErrorCode { GenericJITLinkError = 1 };
28 
29 // FIXME: This class is only here to support the transition to llvm::Error. It
30 // will be removed once this transition is complete. Clients should prefer to
31 // deal with the Error value directly, rather than converting to error_code.
32 class JITLinkerErrorCategory : public std::error_category {
33 public:
34  const char *name() const noexcept override { return "runtimedyld"; }
35 
36  std::string message(int Condition) const override {
37  switch (static_cast<JITLinkErrorCode>(Condition)) {
38  case GenericJITLinkError:
39  return "Generic JITLink error";
40  }
41  llvm_unreachable("Unrecognized JITLinkErrorCode");
42  }
43 };
44 
45 static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
46 
47 } // namespace
48 
49 namespace llvm {
50 namespace jitlink {
51 
52 char JITLinkError::ID = 0;
53 
54 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
55 
56 std::error_code JITLinkError::convertToErrorCode() const {
57  return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
58 }
59 
61  switch (K) {
62  case Edge::Invalid:
63  return "INVALID RELOCATION";
64  case Edge::KeepAlive:
65  return "Keep-Alive";
66  default:
67  return "<Unrecognized edge kind>";
68  }
69 }
70 
71 const char *getLinkageName(Linkage L) {
72  switch (L) {
73  case Linkage::Strong:
74  return "strong";
75  case Linkage::Weak:
76  return "weak";
77  }
78  llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
79 }
80 
81 const char *getScopeName(Scope S) {
82  switch (S) {
83  case Scope::Default:
84  return "default";
85  case Scope::Hidden:
86  return "hidden";
87  case Scope::Local:
88  return "local";
89  }
90  llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
91 }
92 
94  return OS << formatv("{0:x16}", B.getAddress()) << " -- "
95  << formatv("{0:x8}", B.getAddress() + B.getSize()) << ": "
96  << "size = " << formatv("{0:x8}", B.getSize()) << ", "
97  << (B.isZeroFill() ? "zero-fill" : "content")
98  << ", align = " << B.getAlignment()
99  << ", align-ofs = " << B.getAlignmentOffset()
100  << ", section = " << B.getSection().getName();
101 }
102 
104  OS << formatv("{0:x16}", Sym.getAddress()) << " ("
105  << (Sym.isDefined() ? "block" : "addressable") << " + "
106  << formatv("{0:x8}", Sym.getOffset())
107  << "): size: " << formatv("{0:x8}", Sym.getSize())
108  << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
109  << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
110  << (Sym.isLive() ? "live" : "dead") << " - "
111  << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
112  return OS;
113 }
114 
115 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
116  StringRef EdgeKindName) {
117  OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": "
118  << formatv("{0:x16}", B.getAddress()) << " + "
119  << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName << " -> ";
120 
121  auto &TargetSym = E.getTarget();
122  if (TargetSym.hasName())
123  OS << TargetSym.getName();
124  else {
125  auto &TargetBlock = TargetSym.getBlock();
126  auto &TargetSec = TargetBlock.getSection();
127  JITTargetAddress SecAddress = ~JITTargetAddress(0);
128  for (auto *B : TargetSec.blocks())
129  if (B->getAddress() < SecAddress)
130  SecAddress = B->getAddress();
131 
132  JITTargetAddress SecDelta = TargetSym.getAddress() - SecAddress;
133  OS << formatv("{0:x16}", TargetSym.getAddress()) << " (section "
134  << TargetSec.getName();
135  if (SecDelta)
136  OS << " + " << formatv("{0:x}", SecDelta);
137  OS << " / block " << formatv("{0:x16}", TargetBlock.getAddress());
138  if (TargetSym.getOffset())
139  OS << " + " << formatv("{0:x}", TargetSym.getOffset());
140  OS << ")";
141  }
142 
143  if (E.getAddend() != 0)
144  OS << " + " << E.getAddend();
145 }
146 
147 Section::~Section() {
148  for (auto *Sym : Symbols)
149  Sym->~Symbol();
150  for (auto *B : Blocks)
151  B->~Block();
152 }
153 
154 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
155  SplitBlockCache *Cache) {
156 
157  assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
158 
159  // If the split point covers all of B then just return B.
160  if (SplitIndex == B.getSize())
161  return B;
162 
163  assert(SplitIndex < B.getSize() && "SplitIndex out of range");
164 
165  // Create the new block covering [ 0, SplitIndex ).
166  auto &NewBlock =
167  B.isZeroFill()
168  ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
169  B.getAlignment(), B.getAlignmentOffset())
170  : createContentBlock(
171  B.getSection(), B.getContent().slice(0, SplitIndex),
172  B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
173 
174  // Modify B to cover [ SplitIndex, B.size() ).
175  B.setAddress(B.getAddress() + SplitIndex);
176  B.setContent(B.getContent().slice(SplitIndex));
177  B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
178  B.getAlignment());
179 
180  // Handle edge transfer/update.
181  {
182  // Copy edges to NewBlock (recording their iterators so that we can remove
183  // them from B), and update of Edges remaining on B.
184  std::vector<Block::edge_iterator> EdgesToRemove;
185  for (auto I = B.edges().begin(); I != B.edges().end();) {
186  if (I->getOffset() < SplitIndex) {
187  NewBlock.addEdge(*I);
188  I = B.removeEdge(I);
189  } else {
190  I->setOffset(I->getOffset() - SplitIndex);
191  ++I;
192  }
193  }
194  }
195 
196  // Handle symbol transfer/update.
197  {
198  // Initialize the symbols cache if necessary.
199  SplitBlockCache LocalBlockSymbolsCache;
200  if (!Cache)
201  Cache = &LocalBlockSymbolsCache;
202  if (*Cache == None) {
203  *Cache = SplitBlockCache::value_type();
204  for (auto *Sym : B.getSection().symbols())
205  if (&Sym->getBlock() == &B)
206  (*Cache)->push_back(Sym);
207 
208  llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
209  return LHS->getOffset() > RHS->getOffset();
210  });
211  }
212  auto &BlockSymbols = **Cache;
213 
214  // Transfer all symbols with offset less than SplitIndex to NewBlock.
215  while (!BlockSymbols.empty() &&
216  BlockSymbols.back()->getOffset() < SplitIndex) {
217  BlockSymbols.back()->setBlock(NewBlock);
218  BlockSymbols.pop_back();
219  }
220 
221  // Update offsets for all remaining symbols in B.
222  for (auto *Sym : BlockSymbols)
223  Sym->setOffset(Sym->getOffset() - SplitIndex);
224  }
225 
226  return NewBlock;
227 }
228 
231 
232  // Map from blocks to the symbols pointing at them.
233  for (auto *Sym : defined_symbols())
234  BlockSymbols[&Sym->getBlock()].push_back(Sym);
235 
236  // For each block, sort its symbols by something approximating
237  // relevance.
238  for (auto &KV : BlockSymbols)
239  llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
240  if (LHS->getOffset() != RHS->getOffset())
241  return LHS->getOffset() < RHS->getOffset();
242  if (LHS->getLinkage() != RHS->getLinkage())
243  return LHS->getLinkage() < RHS->getLinkage();
244  if (LHS->getScope() != RHS->getScope())
245  return LHS->getScope() < RHS->getScope();
246  if (LHS->hasName()) {
247  if (!RHS->hasName())
248  return true;
249  return LHS->getName() < RHS->getName();
250  }
251  return false;
252  });
253 
254  for (auto &Sec : sections()) {
255  OS << "section " << Sec.getName() << ":\n\n";
256 
257  std::vector<Block *> SortedBlocks;
258  llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
259  llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
260  return LHS->getAddress() < RHS->getAddress();
261  });
262 
263  for (auto *B : SortedBlocks) {
264  OS << " block " << formatv("{0:x16}", B->getAddress())
265  << " size = " << formatv("{0:x8}", B->getSize())
266  << ", align = " << B->getAlignment()
267  << ", alignment-offset = " << B->getAlignmentOffset() << "\n";
268 
269  auto BlockSymsI = BlockSymbols.find(B);
270  if (BlockSymsI != BlockSymbols.end()) {
271  OS << " symbols:\n";
272  auto &Syms = BlockSymsI->second;
273  for (auto *Sym : Syms)
274  OS << " " << *Sym << "\n";
275  } else
276  OS << " no symbols\n";
277 
278  if (!B->edges_empty()) {
279  OS << " edges:\n";
280  std::vector<Edge> SortedEdges;
281  llvm::copy(B->edges(), std::back_inserter(SortedEdges));
282  llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
283  return LHS.getOffset() < RHS.getOffset();
284  });
285  for (auto &E : SortedEdges) {
286  OS << " " << formatv("{0:x16}", B->getFixupAddress(E))
287  << " (block + " << formatv("{0:x8}", E.getOffset())
288  << "), addend = ";
289  if (E.getAddend() >= 0)
290  OS << formatv("+{0:x8}", E.getAddend());
291  else
292  OS << formatv("-{0:x8}", -E.getAddend());
293  OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
294  if (E.getTarget().hasName())
295  OS << E.getTarget().getName();
296  else
297  OS << "addressable@"
298  << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
299  << formatv("{0:x8}", E.getTarget().getOffset());
300  OS << "\n";
301  }
302  } else
303  OS << " no edges\n";
304  OS << "\n";
305  }
306  }
307 
308  OS << "Absolute symbols:\n";
309  if (!llvm::empty(absolute_symbols())) {
310  for (auto *Sym : absolute_symbols())
311  OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
312  << "\n";
313  } else
314  OS << " none\n";
315 
316  OS << "\nExternal symbols:\n";
317  if (!llvm::empty(external_symbols())) {
318  for (auto *Sym : external_symbols())
319  OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
320  << "\n";
321  } else
322  OS << " none\n";
323 }
324 
326  switch (LF) {
327  case SymbolLookupFlags::RequiredSymbol:
328  return OS << "RequiredSymbol";
329  case SymbolLookupFlags::WeaklyReferencedSymbol:
330  return OS << "WeaklyReferencedSymbol";
331  }
332  llvm_unreachable("Unrecognized lookup flags");
333 }
334 
335 void JITLinkAsyncLookupContinuation::anchor() {}
336 
337 JITLinkContext::~JITLinkContext() {}
338 
339 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
340  return true;
341 }
342 
343 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
344  return LinkGraphPassFunction();
345 }
346 
347 Error JITLinkContext::modifyPassConfig(LinkGraph &G,
348  PassConfiguration &Config) {
349  return Error::success();
350 }
351 
353  for (auto *Sym : G.defined_symbols())
354  Sym->setLive(true);
355  return Error::success();
356 }
357 
359  const Edge &E) {
360  std::string ErrMsg;
361  {
362  raw_string_ostream ErrStream(ErrMsg);
363  Section &Sec = B.getSection();
364  ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
365  << ": relocation target ";
366  if (E.getTarget().hasName())
367  ErrStream << "\"" << E.getTarget().getName() << "\" ";
368  ErrStream << "at address " << formatv("{0:x}", E.getTarget().getAddress());
369  ErrStream << " is out of range of " << G.getEdgeKindName(E.getKind())
370  << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
371 
372  Symbol *BestSymbolForBlock = nullptr;
373  for (auto *Sym : Sec.symbols())
374  if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
375  (!BestSymbolForBlock ||
376  Sym->getScope() < BestSymbolForBlock->getScope() ||
377  Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
378  BestSymbolForBlock = Sym;
379 
380  if (BestSymbolForBlock)
381  ErrStream << BestSymbolForBlock->getName() << ", ";
382  else
383  ErrStream << "<anonymous block> @ ";
384 
385  ErrStream << formatv("{0:x}", B.getAddress()) << " + "
386  << formatv("{0:x}", E.getOffset()) << ")";
387  }
388  return make_error<JITLinkError>(std::move(ErrMsg));
389 }
390 
393  auto Magic = identify_magic(ObjectBuffer.getBuffer());
394  switch (Magic) {
396  return createLinkGraphFromMachOObject(ObjectBuffer);
398  return createLinkGraphFromELFObject(ObjectBuffer);
399  default:
400  return make_error<JITLinkError>("Unsupported file format");
401  };
402 }
403 
404 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
405  switch (G->getTargetTriple().getObjectFormat()) {
406  case Triple::MachO:
407  return link_MachO(std::move(G), std::move(Ctx));
408  case Triple::ELF:
409  return link_ELF(std::move(G), std::move(Ctx));
410  default:
411  Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
412  };
413 }
414 
415 } // end namespace jitlink
416 } // end namespace llvm
MemoryBuffer.h
llvm::Optional::value_type
T value_type
Definition: Optional.h:242
llvm
Definition: AllocatorList.h:23
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
ManagedStatic.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1533
llvm::Optional
Definition: APInt.h:33
splitBlock
static void splitBlock(MachineBasicBlock &MBB, MachineInstr &MI, MachineDominatorTree *MDT)
Definition: SILateBranchLowering.cpp:87
llvm::dump
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Definition: SparseBitVector.h:876
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Format.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
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:250
llvm::object
Definition: ObjectFileTransformer.h:18
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:50
llvm::None
const NoneType None
Definition: None.h:23
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:35
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
getLinkageName
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
Definition: AsmWriter.cpp:3162
llvm::Triple::ELF
@ ELF
Definition: Triple.h:235
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
I
#define I(x, y, z)
Definition: MD5.cpp:59
Magic
const char Magic[]
Definition: Archive.cpp:41
message
message(STATUS "Targeting ${t}") add_subdirectory($
Definition: CMakeLists.txt:31
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_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
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
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:84
llvm::empty
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:254
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::Triple::MachO
@ MachO
Definition: Triple.h:237
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1423
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::JITTargetAddress
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42
Invalid
@ Invalid
Definition: AArch64ISelLowering.cpp:7896
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:35
error_category
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:76
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition: Magic.h:30
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38