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 
9 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
10 
11 #include "llvm/BinaryFormat/Magic.h"
12 #include "llvm/ExecutionEngine/JITLink/ELF.h"
13 #include "llvm/ExecutionEngine/JITLink/MachO.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 using namespace llvm;
19 using namespace llvm::object;
20 
21 #define DEBUG_TYPE "jitlink"
22 
23 namespace {
24 
25 enum JITLinkErrorCode { GenericJITLinkError = 1 };
26 
27 // FIXME: This class is only here to support the transition to llvm::Error. It
28 // will be removed once this transition is complete. Clients should prefer to
29 // deal with the Error value directly, rather than converting to error_code.
30 class JITLinkerErrorCategory : public std::error_category {
31 public:
32   const char *name() const noexcept override { return "runtimedyld"; }
33 
34   std::string message(int Condition) const override {
35     switch (static_cast<JITLinkErrorCode>(Condition)) {
36     case GenericJITLinkError:
37       return "Generic JITLink error";
38     }
39     llvm_unreachable("Unrecognized JITLinkErrorCode");
40   }
41 };
42 
43 } // namespace
44 
45 namespace llvm {
46 namespace jitlink {
47 
48 char JITLinkError::ID = 0;
49 
50 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
51 
52 std::error_code JITLinkError::convertToErrorCode() const {
53   static JITLinkerErrorCategory TheJITLinkerErrorCategory;
54   return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
55 }
56 
57 const char *getGenericEdgeKindName(Edge::Kind K) {
58   switch (K) {
59   case Edge::Invalid:
60     return "INVALID RELOCATION";
61   case Edge::KeepAlive:
62     return "Keep-Alive";
63   default:
64     return "<Unrecognized edge kind>";
65   }
66 }
67 
68 const char *getLinkageName(Linkage L) {
69   switch (L) {
70   case Linkage::Strong:
71     return "strong";
72   case Linkage::Weak:
73     return "weak";
74   }
75   llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
76 }
77 
78 const char *getScopeName(Scope S) {
79   switch (S) {
80   case Scope::Default:
81     return "default";
82   case Scope::Hidden:
83     return "hidden";
84   case Scope::Local:
85     return "local";
86   }
87   llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
88 }
89 
90 raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
91   return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
92             << ": "
93             << "size = " << formatv("{0:x8}", B.getSize()) << ", "
94             << (B.isZeroFill() ? "zero-fill" : "content")
95             << ", align = " << B.getAlignment()
96             << ", align-ofs = " << B.getAlignmentOffset()
97             << ", section = " << B.getSection().getName();
98 }
99 
100 raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
101   OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
102      << " + " << formatv("{0:x8}", Sym.getOffset())
103      << "): size: " << formatv("{0:x8}", Sym.getSize())
104      << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
105      << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
106      << (Sym.isLive() ? "live" : "dead") << "  -   "
107      << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
108   return OS;
109 }
110 
111 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
112                StringRef EdgeKindName) {
113   OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
114      << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
115      << " -> ";
116 
117   auto &TargetSym = E.getTarget();
118   if (TargetSym.hasName())
119     OS << TargetSym.getName();
120   else {
121     auto &TargetBlock = TargetSym.getBlock();
122     auto &TargetSec = TargetBlock.getSection();
123     orc::ExecutorAddr SecAddress(~uint64_t(0));
124     for (auto *B : TargetSec.blocks())
125       if (B->getAddress() < SecAddress)
126         SecAddress = B->getAddress();
127 
128     orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
129     OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
130     if (SecDelta)
131       OS << " + " << formatv("{0:x}", SecDelta);
132     OS << " / block " << TargetBlock.getAddress();
133     if (TargetSym.getOffset())
134       OS << " + " << formatv("{0:x}", TargetSym.getOffset());
135     OS << ")";
136   }
137 
138   if (E.getAddend() != 0)
139     OS << " + " << E.getAddend();
140 }
141 
142 Section::~Section() {
143   for (auto *Sym : Symbols)
144     Sym->~Symbol();
145   for (auto *B : Blocks)
146     B->~Block();
147 }
148 
149 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
150                              SplitBlockCache *Cache) {
151 
152   assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
153 
154   // If the split point covers all of B then just return B.
155   if (SplitIndex == B.getSize())
156     return B;
157 
158   assert(SplitIndex < B.getSize() && "SplitIndex out of range");
159 
160   // Create the new block covering [ 0, SplitIndex ).
161   auto &NewBlock =
162       B.isZeroFill()
163           ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
164                                 B.getAlignment(), B.getAlignmentOffset())
165           : createContentBlock(
166                 B.getSection(), B.getContent().slice(0, SplitIndex),
167                 B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
168 
169   // Modify B to cover [ SplitIndex, B.size() ).
170   B.setAddress(B.getAddress() + SplitIndex);
171   B.setContent(B.getContent().slice(SplitIndex));
172   B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
173                        B.getAlignment());
174 
175   // Handle edge transfer/update.
176   {
177     // Copy edges to NewBlock (recording their iterators so that we can remove
178     // them from B), and update of Edges remaining on B.
179     std::vector<Block::edge_iterator> EdgesToRemove;
180     for (auto I = B.edges().begin(); I != B.edges().end();) {
181       if (I->getOffset() < SplitIndex) {
182         NewBlock.addEdge(*I);
183         I = B.removeEdge(I);
184       } else {
185         I->setOffset(I->getOffset() - SplitIndex);
186         ++I;
187       }
188     }
189   }
190 
191   // Handle symbol transfer/update.
192   {
193     // Initialize the symbols cache if necessary.
194     SplitBlockCache LocalBlockSymbolsCache;
195     if (!Cache)
196       Cache = &LocalBlockSymbolsCache;
197     if (*Cache == None) {
198       *Cache = SplitBlockCache::value_type();
199       for (auto *Sym : B.getSection().symbols())
200         if (&Sym->getBlock() == &B)
201           (*Cache)->push_back(Sym);
202 
203       llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
204         return LHS->getOffset() > RHS->getOffset();
205       });
206     }
207     auto &BlockSymbols = **Cache;
208 
209     // Transfer all symbols with offset less than SplitIndex to NewBlock.
210     while (!BlockSymbols.empty() &&
211            BlockSymbols.back()->getOffset() < SplitIndex) {
212       auto *Sym = BlockSymbols.back();
213       // If the symbol extends beyond the split, update the size to be within
214       // the new block.
215       if (Sym->getOffset() + Sym->getSize() > SplitIndex)
216         Sym->setSize(SplitIndex - Sym->getOffset());
217       Sym->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 
229 void 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 " << B->getAddress()
265          << " size = " << formatv("{0:x8}", B->getSize())
266          << ", align = " << B->getAlignment()
267          << ", alignment-offset = " << B->getAlignmentOffset();
268       if (B->isZeroFill())
269         OS << ", zero-fill";
270       OS << "\n";
271 
272       auto BlockSymsI = BlockSymbols.find(B);
273       if (BlockSymsI != BlockSymbols.end()) {
274         OS << "    symbols:\n";
275         auto &Syms = BlockSymsI->second;
276         for (auto *Sym : Syms)
277           OS << "      " << *Sym << "\n";
278       } else
279         OS << "    no symbols\n";
280 
281       if (!B->edges_empty()) {
282         OS << "    edges:\n";
283         std::vector<Edge> SortedEdges;
284         llvm::copy(B->edges(), std::back_inserter(SortedEdges));
285         llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
286           return LHS.getOffset() < RHS.getOffset();
287         });
288         for (auto &E : SortedEdges) {
289           OS << "      " << B->getFixupAddress(E) << " (block + "
290              << formatv("{0:x8}", E.getOffset()) << "), addend = ";
291           if (E.getAddend() >= 0)
292             OS << formatv("+{0:x8}", E.getAddend());
293           else
294             OS << formatv("-{0:x8}", -E.getAddend());
295           OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
296           if (E.getTarget().hasName())
297             OS << E.getTarget().getName();
298           else
299             OS << "addressable@"
300                << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
301                << formatv("{0:x8}", E.getTarget().getOffset());
302           OS << "\n";
303         }
304       } else
305         OS << "    no edges\n";
306       OS << "\n";
307     }
308   }
309 
310   OS << "Absolute symbols:\n";
311   if (!llvm::empty(absolute_symbols())) {
312     for (auto *Sym : absolute_symbols())
313       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
314   } else
315     OS << "  none\n";
316 
317   OS << "\nExternal symbols:\n";
318   if (!llvm::empty(external_symbols())) {
319     for (auto *Sym : external_symbols())
320       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
321   } else
322     OS << "  none\n";
323 }
324 
325 raw_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 
335 void JITLinkAsyncLookupContinuation::anchor() {}
336 
337 JITLinkContext::~JITLinkContext() = default;
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 
352 Error markAllSymbolsLive(LinkGraph &G) {
353   for (auto *Sym : G.defined_symbols())
354     Sym->setLive(true);
355   return Error::success();
356 }
357 
358 Error 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     } else
369       ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
370                 << formatv("{0:x}", E.getOffset());
371     ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
372               << " is out of range of " << G.getEdgeKindName(E.getKind())
373               << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
374 
375     Symbol *BestSymbolForBlock = nullptr;
376     for (auto *Sym : Sec.symbols())
377       if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
378           (!BestSymbolForBlock ||
379            Sym->getScope() < BestSymbolForBlock->getScope() ||
380            Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
381         BestSymbolForBlock = Sym;
382 
383     if (BestSymbolForBlock)
384       ErrStream << BestSymbolForBlock->getName() << ", ";
385     else
386       ErrStream << "<anonymous block> @ ";
387 
388     ErrStream << formatv("{0:x}", B.getAddress()) << " + "
389               << formatv("{0:x}", E.getOffset()) << ")";
390   }
391   return make_error<JITLinkError>(std::move(ErrMsg));
392 }
393 
394 Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value, int N,
395                          const Edge &E) {
396   return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
397                                   " improper alignment for relocation " +
398                                   formatv("{0:d}", E.getKind()) + ": 0x" +
399                                   llvm::utohexstr(Value) +
400                                   " is not aligned to " + Twine(N) + " bytes");
401 }
402 
403 Expected<std::unique_ptr<LinkGraph>>
404 createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
405   auto Magic = identify_magic(ObjectBuffer.getBuffer());
406   switch (Magic) {
407   case file_magic::macho_object:
408     return createLinkGraphFromMachOObject(ObjectBuffer);
409   case file_magic::elf_relocatable:
410     return createLinkGraphFromELFObject(ObjectBuffer);
411   default:
412     return make_error<JITLinkError>("Unsupported file format");
413   };
414 }
415 
416 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
417   switch (G->getTargetTriple().getObjectFormat()) {
418   case Triple::MachO:
419     return link_MachO(std::move(G), std::move(Ctx));
420   case Triple::ELF:
421     return link_ELF(std::move(G), std::move(Ctx));
422   default:
423     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
424   };
425 }
426 
427 } // end namespace jitlink
428 } // end namespace llvm
429