LLVM  10.0.0
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 
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 << "\n"; }
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  llvm_unreachable("Unrecognized relocation 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 << formatv("{0:x16}", B.getAddress()) << " -- "
94  << formatv("{0:x16}", B.getAddress() + 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 << "<";
103  if (Sym.getName().empty())
104  OS << "*anon*";
105  else
106  OS << Sym.getName();
107  OS << ": flags = ";
108  switch (Sym.getLinkage()) {
109  case Linkage::Strong:
110  OS << 'S';
111  break;
112  case Linkage::Weak:
113  OS << 'W';
114  break;
115  }
116  switch (Sym.getScope()) {
117  case Scope::Default:
118  OS << 'D';
119  break;
120  case Scope::Hidden:
121  OS << 'H';
122  break;
123  case Scope::Local:
124  OS << 'L';
125  break;
126  }
127  OS << (Sym.isLive() ? '+' : '-')
128  << ", size = " << formatv("{0:x8}", Sym.getSize())
129  << ", addr = " << formatv("{0:x16}", Sym.getAddress()) << " ("
130  << formatv("{0:x16}", Sym.getAddressable().getAddress()) << " + "
131  << formatv("{0:x8}", Sym.getOffset());
132  if (Sym.isDefined())
133  OS << " " << Sym.getBlock().getSection().getName();
134  OS << ")>";
135  return OS;
136 }
137 
138 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
139  StringRef EdgeKindName) {
140  OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": "
141  << formatv("{0:x16}", B.getAddress()) << " + " << E.getOffset() << " -- "
142  << EdgeKindName << " -> " << E.getTarget() << " + " << E.getAddend();
143 }
144 
145 Section::~Section() {
146  for (auto *Sym : Symbols)
147  Sym->~Symbol();
148  for (auto *B : Blocks)
149  B->~Block();
150 }
151 
152 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
153  SplitBlockCache *Cache) {
154 
155  assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
156 
157  // If the split point covers all of B then just return B.
158  if (SplitIndex == B.getSize())
159  return B;
160 
161  assert(SplitIndex < B.getSize() && "SplitIndex out of range");
162 
163  // Create the new block covering [ 0, SplitIndex ).
164  auto &NewBlock =
165  B.isZeroFill()
166  ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
168  : createContentBlock(
169  B.getSection(), B.getContent().substr(0, SplitIndex),
171 
172  // Modify B to cover [ SplitIndex, B.size() ).
173  B.setAddress(B.getAddress() + SplitIndex);
174  B.setContent(B.getContent().substr(SplitIndex));
175  B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
176  B.getAlignment());
177 
178  // Handle edge transfer/update.
179  {
180  // Copy edges to NewBlock (recording their iterators so that we can remove
181  // them from B), and update of Edges remaining on B.
182  std::vector<Block::edge_iterator> EdgesToRemove;
183  for (auto I = B.edges().begin(), E = B.edges().end(); I != E; ++I) {
184  if (I->getOffset() < SplitIndex) {
185  NewBlock.addEdge(*I);
186  EdgesToRemove.push_back(I);
187  } else
188  I->setOffset(I->getOffset() - SplitIndex);
189  }
190 
191  // Remove edges that were transfered to NewBlock from B.
192  while (!EdgesToRemove.empty()) {
193  B.removeEdge(EdgesToRemove.back());
194  EdgesToRemove.pop_back();
195  }
196  }
197 
198  // Handle symbol transfer/update.
199  {
200  // Initialize the symbols cache if necessary.
201  SplitBlockCache LocalBlockSymbolsCache;
202  if (!Cache)
203  Cache = &LocalBlockSymbolsCache;
204  if (*Cache == None) {
205  *Cache = SplitBlockCache::value_type();
206  for (auto *Sym : B.getSection().symbols())
207  if (&Sym->getBlock() == &B)
208  (*Cache)->push_back(Sym);
209 
210  llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
211  return LHS->getOffset() > RHS->getOffset();
212  });
213  }
214  auto &BlockSymbols = **Cache;
215 
216  // Transfer all symbols with offset less than SplitIndex to NewBlock.
217  while (!BlockSymbols.empty() &&
218  BlockSymbols.back()->getOffset() < SplitIndex) {
219  BlockSymbols.back()->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 
232  std::function<StringRef(Edge::Kind)> EdgeKindToName) {
233  if (!EdgeKindToName)
234  EdgeKindToName = [](Edge::Kind K) { return StringRef(); };
235 
236  OS << "Symbols:\n";
237  for (auto *Sym : defined_symbols()) {
238  OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
239  << "\n";
240  if (Sym->isDefined()) {
241  for (auto &E : Sym->getBlock().edges()) {
242  OS << " ";
243  StringRef EdgeName = (E.getKind() < Edge::FirstRelocation
244  ? getGenericEdgeKindName(E.getKind())
245  : EdgeKindToName(E.getKind()));
246 
247  if (!EdgeName.empty())
248  printEdge(OS, Sym->getBlock(), E, EdgeName);
249  else {
250  auto EdgeNumberString = std::to_string(E.getKind());
251  printEdge(OS, Sym->getBlock(), E, EdgeNumberString);
252  }
253  OS << "\n";
254  }
255  }
256  }
257 
258  OS << "Absolute symbols:\n";
259  for (auto *Sym : absolute_symbols())
260  OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
261  << "\n";
262 
263  OS << "External symbols:\n";
264  for (auto *Sym : external_symbols())
265  OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
266  << "\n";
267 }
268 
270  switch (LF) {
271  case SymbolLookupFlags::RequiredSymbol:
272  return OS << "RequiredSymbol";
273  case SymbolLookupFlags::WeaklyReferencedSymbol:
274  return OS << "WeaklyReferencedSymbol";
275  }
276  llvm_unreachable("Unrecognized lookup flags");
277 }
278 
279 void JITLinkAsyncLookupContinuation::anchor() {}
280 
281 JITLinkContext::~JITLinkContext() {}
282 
283 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
284  return true;
285 }
286 
287 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
288  return LinkGraphPassFunction();
289 }
290 
291 Error JITLinkContext::modifyPassConfig(const Triple &TT,
292  PassConfiguration &Config) {
293  return Error::success();
294 }
295 
297  for (auto *Sym : G.defined_symbols())
298  Sym->setLive(true);
299  return Error::success();
300 }
301 
302 void jitLink(std::unique_ptr<JITLinkContext> Ctx) {
303  auto Magic = identify_magic(Ctx->getObjectBuffer().getBuffer());
304  switch (Magic) {
306  return jitLink_MachO(std::move(Ctx));
307  default:
308  Ctx->notifyFailed(make_error<JITLinkError>("Unsupported file format"));
309  };
310 }
311 
312 } // end namespace jitlink
313 } // end namespace llvm
const NoneType None
Definition: None.h:23
raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:35
static const char * name
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
Definition: AsmWriter.cpp:2922
Mach-O Object file.
Definition: Magic.h:30
This class represents lattice values for constants.
Definition: AllocatorList.h:23
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
static void dump(StringRef Title, SpillInfo const &Spills)
Definition: CoroFrame.cpp:322
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:600
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1114
static const char *const Magic
Definition: Archive.cpp:41
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static MachineBasicBlock & splitBlock(MachineBasicBlock &MBB, MachineInstr &SplitI, const X86InstrInfo &TII)
#define I(x, y, z)
Definition: MD5.cpp:58
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83