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
10#include "llvm/ExecutionEngine/JITLink/JITLink.h"
11
12#include "llvm/BinaryFormat/Magic.h"
13#include "llvm/ExecutionEngine/JITLink/ELF.h"
14#include "llvm/ExecutionEngine/JITLink/MachO.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/ManagedStatic.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21using namespace llvm::object;
22
23#define DEBUG_TYPE "jitlink"
24
25namespace {
26
27enum 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.
32class JITLinkerErrorCategory : public std::error_category {
33public:
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
45static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
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  return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
58}
59
60const char *getGenericEdgeKindName(Edge::Kind K) {
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
71const 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
81const 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
93raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
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
103raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
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
115void 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
147Section::~Section() {
148  for (auto *Sym : Symbols)
149    Sym->~Symbol();
150  for (auto *B : Blocks)
151    B->~Block();
152}
153
154Block &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
229void LinkGraph::dump(raw_ostream &OS) {
230  DenseMap<Block *, std::vector<Symbol *>> BlockSymbols;
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
325raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
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
335void JITLinkAsyncLookupContinuation::anchor() {}
336
337JITLinkContext::~JITLinkContext() {}
338
339bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
340  return true;
341}
342
343LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
344  return LinkGraphPassFunction();
345}
346
347Error JITLinkContext::modifyPassConfig(LinkGraph &G,
348                                       PassConfiguration &Config) {
349  return Error::success();
350}
351
352Error markAllSymbolsLive(LinkGraph &G) {
353  for (auto *Sym : G.defined_symbols())
354    Sym->setLive(true);
355  return Error::success();
356}
357
358Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
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
391Expected<std::unique_ptr<LinkGraph>>
392createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
393  auto Magic = identify_magic(ObjectBuffer.getBuffer());
394  switch (Magic) {
395  case file_magic::macho_object:
396    return createLinkGraphFromMachOObject(ObjectBuffer);
397  case file_magic::elf_relocatable:
398    return createLinkGraphFromELFObject(ObjectBuffer);
399  default:
400    return make_error<JITLinkError>("Unsupported file format");
401  };
402}
403
404void 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
417