xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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/ADT/StringExtras.h"
12 #include "llvm/BinaryFormat/Magic.h"
13 #include "llvm/ExecutionEngine/JITLink/COFF.h"
14 #include "llvm/ExecutionEngine/JITLink/ELF.h"
15 #include "llvm/ExecutionEngine/JITLink/MachO.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.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 } // namespace
46 
47 namespace llvm {
48 namespace jitlink {
49 
50 char JITLinkError::ID = 0;
51 
52 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
53 
54 std::error_code JITLinkError::convertToErrorCode() const {
55   static JITLinkerErrorCategory TheJITLinkerErrorCategory;
56   return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
57 }
58 
59 const char *getGenericEdgeKindName(Edge::Kind K) {
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 
92 bool isCStringBlock(Block &B) {
93   if (B.getSize() == 0) // Empty blocks are not valid C-strings.
94     return false;
95 
96   // Zero-fill blocks of size one are valid empty strings.
97   if (B.isZeroFill())
98     return B.getSize() == 1;
99 
100   for (size_t I = 0; I != B.getSize() - 1; ++I)
101     if (B.getContent()[I] == '\0')
102       return false;
103 
104   return B.getContent()[B.getSize() - 1] == '\0';
105 }
106 
107 raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
108   return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
109             << ": "
110             << "size = " << formatv("{0:x8}", B.getSize()) << ", "
111             << (B.isZeroFill() ? "zero-fill" : "content")
112             << ", align = " << B.getAlignment()
113             << ", align-ofs = " << B.getAlignmentOffset()
114             << ", section = " << B.getSection().getName();
115 }
116 
117 raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
118   OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
119      << " + " << formatv("{0:x8}", Sym.getOffset())
120      << "): size: " << formatv("{0:x8}", Sym.getSize())
121      << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
122      << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
123      << (Sym.isLive() ? "live" : "dead") << "  -   "
124      << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
125   return OS;
126 }
127 
128 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
129                StringRef EdgeKindName) {
130   OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
131      << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
132      << " -> ";
133 
134   auto &TargetSym = E.getTarget();
135   if (TargetSym.hasName())
136     OS << TargetSym.getName();
137   else {
138     auto &TargetBlock = TargetSym.getBlock();
139     auto &TargetSec = TargetBlock.getSection();
140     orc::ExecutorAddr SecAddress(~uint64_t(0));
141     for (auto *B : TargetSec.blocks())
142       if (B->getAddress() < SecAddress)
143         SecAddress = B->getAddress();
144 
145     orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
146     OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
147     if (SecDelta)
148       OS << " + " << formatv("{0:x}", SecDelta);
149     OS << " / block " << TargetBlock.getAddress();
150     if (TargetSym.getOffset())
151       OS << " + " << formatv("{0:x}", TargetSym.getOffset());
152     OS << ")";
153   }
154 
155   if (E.getAddend() != 0)
156     OS << " + " << E.getAddend();
157 }
158 
159 Section::~Section() {
160   for (auto *Sym : Symbols)
161     Sym->~Symbol();
162   for (auto *B : Blocks)
163     B->~Block();
164 }
165 
166 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
167                              SplitBlockCache *Cache) {
168 
169   assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
170 
171   // If the split point covers all of B then just return B.
172   if (SplitIndex == B.getSize())
173     return B;
174 
175   assert(SplitIndex < B.getSize() && "SplitIndex out of range");
176 
177   // Create the new block covering [ 0, SplitIndex ).
178   auto &NewBlock =
179       B.isZeroFill()
180           ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
181                                 B.getAlignment(), B.getAlignmentOffset())
182           : createContentBlock(
183                 B.getSection(), B.getContent().slice(0, SplitIndex),
184                 B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
185 
186   // Modify B to cover [ SplitIndex, B.size() ).
187   B.setAddress(B.getAddress() + SplitIndex);
188   B.setContent(B.getContent().slice(SplitIndex));
189   B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
190                        B.getAlignment());
191 
192   // Handle edge transfer/update.
193   {
194     // Copy edges to NewBlock (recording their iterators so that we can remove
195     // them from B), and update of Edges remaining on B.
196     std::vector<Block::edge_iterator> EdgesToRemove;
197     for (auto I = B.edges().begin(); I != B.edges().end();) {
198       if (I->getOffset() < SplitIndex) {
199         NewBlock.addEdge(*I);
200         I = B.removeEdge(I);
201       } else {
202         I->setOffset(I->getOffset() - SplitIndex);
203         ++I;
204       }
205     }
206   }
207 
208   // Handle symbol transfer/update.
209   {
210     // Initialize the symbols cache if necessary.
211     SplitBlockCache LocalBlockSymbolsCache;
212     if (!Cache)
213       Cache = &LocalBlockSymbolsCache;
214     if (*Cache == std::nullopt) {
215       *Cache = SplitBlockCache::value_type();
216       for (auto *Sym : B.getSection().symbols())
217         if (&Sym->getBlock() == &B)
218           (*Cache)->push_back(Sym);
219 
220       llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
221         return LHS->getOffset() > RHS->getOffset();
222       });
223     }
224     auto &BlockSymbols = **Cache;
225 
226     // Transfer all symbols with offset less than SplitIndex to NewBlock.
227     while (!BlockSymbols.empty() &&
228            BlockSymbols.back()->getOffset() < SplitIndex) {
229       auto *Sym = BlockSymbols.back();
230       // If the symbol extends beyond the split, update the size to be within
231       // the new block.
232       if (Sym->getOffset() + Sym->getSize() > SplitIndex)
233         Sym->setSize(SplitIndex - Sym->getOffset());
234       Sym->setBlock(NewBlock);
235       BlockSymbols.pop_back();
236     }
237 
238     // Update offsets for all remaining symbols in B.
239     for (auto *Sym : BlockSymbols)
240       Sym->setOffset(Sym->getOffset() - SplitIndex);
241   }
242 
243   return NewBlock;
244 }
245 
246 void LinkGraph::dump(raw_ostream &OS) {
247   DenseMap<Block *, std::vector<Symbol *>> BlockSymbols;
248 
249   // Map from blocks to the symbols pointing at them.
250   for (auto *Sym : defined_symbols())
251     BlockSymbols[&Sym->getBlock()].push_back(Sym);
252 
253   // For each block, sort its symbols by something approximating
254   // relevance.
255   for (auto &KV : BlockSymbols)
256     llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
257       if (LHS->getOffset() != RHS->getOffset())
258         return LHS->getOffset() < RHS->getOffset();
259       if (LHS->getLinkage() != RHS->getLinkage())
260         return LHS->getLinkage() < RHS->getLinkage();
261       if (LHS->getScope() != RHS->getScope())
262         return LHS->getScope() < RHS->getScope();
263       if (LHS->hasName()) {
264         if (!RHS->hasName())
265           return true;
266         return LHS->getName() < RHS->getName();
267       }
268       return false;
269     });
270 
271   for (auto &Sec : sections()) {
272     OS << "section " << Sec.getName() << ":\n\n";
273 
274     std::vector<Block *> SortedBlocks;
275     llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
276     llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
277       return LHS->getAddress() < RHS->getAddress();
278     });
279 
280     for (auto *B : SortedBlocks) {
281       OS << "  block " << B->getAddress()
282          << " size = " << formatv("{0:x8}", B->getSize())
283          << ", align = " << B->getAlignment()
284          << ", alignment-offset = " << B->getAlignmentOffset();
285       if (B->isZeroFill())
286         OS << ", zero-fill";
287       OS << "\n";
288 
289       auto BlockSymsI = BlockSymbols.find(B);
290       if (BlockSymsI != BlockSymbols.end()) {
291         OS << "    symbols:\n";
292         auto &Syms = BlockSymsI->second;
293         for (auto *Sym : Syms)
294           OS << "      " << *Sym << "\n";
295       } else
296         OS << "    no symbols\n";
297 
298       if (!B->edges_empty()) {
299         OS << "    edges:\n";
300         std::vector<Edge> SortedEdges;
301         llvm::copy(B->edges(), std::back_inserter(SortedEdges));
302         llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
303           return LHS.getOffset() < RHS.getOffset();
304         });
305         for (auto &E : SortedEdges) {
306           OS << "      " << B->getFixupAddress(E) << " (block + "
307              << formatv("{0:x8}", E.getOffset()) << "), addend = ";
308           if (E.getAddend() >= 0)
309             OS << formatv("+{0:x8}", E.getAddend());
310           else
311             OS << formatv("-{0:x8}", -E.getAddend());
312           OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
313           if (E.getTarget().hasName())
314             OS << E.getTarget().getName();
315           else
316             OS << "addressable@"
317                << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
318                << formatv("{0:x8}", E.getTarget().getOffset());
319           OS << "\n";
320         }
321       } else
322         OS << "    no edges\n";
323       OS << "\n";
324     }
325   }
326 
327   OS << "Absolute symbols:\n";
328   if (!absolute_symbols().empty()) {
329     for (auto *Sym : absolute_symbols())
330       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
331   } else
332     OS << "  none\n";
333 
334   OS << "\nExternal symbols:\n";
335   if (!external_symbols().empty()) {
336     for (auto *Sym : external_symbols())
337       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
338   } else
339     OS << "  none\n";
340 }
341 
342 raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
343   switch (LF) {
344   case SymbolLookupFlags::RequiredSymbol:
345     return OS << "RequiredSymbol";
346   case SymbolLookupFlags::WeaklyReferencedSymbol:
347     return OS << "WeaklyReferencedSymbol";
348   }
349   llvm_unreachable("Unrecognized lookup flags");
350 }
351 
352 void JITLinkAsyncLookupContinuation::anchor() {}
353 
354 JITLinkContext::~JITLinkContext() = default;
355 
356 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
357   return true;
358 }
359 
360 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
361   return LinkGraphPassFunction();
362 }
363 
364 Error JITLinkContext::modifyPassConfig(LinkGraph &G,
365                                        PassConfiguration &Config) {
366   return Error::success();
367 }
368 
369 Error markAllSymbolsLive(LinkGraph &G) {
370   for (auto *Sym : G.defined_symbols())
371     Sym->setLive(true);
372   return Error::success();
373 }
374 
375 Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
376                                 const Edge &E) {
377   std::string ErrMsg;
378   {
379     raw_string_ostream ErrStream(ErrMsg);
380     Section &Sec = B.getSection();
381     ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
382               << ": relocation target ";
383     if (E.getTarget().hasName()) {
384       ErrStream << "\"" << E.getTarget().getName() << "\"";
385     } else
386       ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
387                 << formatv("{0:x}", E.getOffset());
388     ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
389               << " is out of range of " << G.getEdgeKindName(E.getKind())
390               << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
391 
392     Symbol *BestSymbolForBlock = nullptr;
393     for (auto *Sym : Sec.symbols())
394       if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
395           (!BestSymbolForBlock ||
396            Sym->getScope() < BestSymbolForBlock->getScope() ||
397            Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
398         BestSymbolForBlock = Sym;
399 
400     if (BestSymbolForBlock)
401       ErrStream << BestSymbolForBlock->getName() << ", ";
402     else
403       ErrStream << "<anonymous block> @ ";
404 
405     ErrStream << formatv("{0:x}", B.getAddress()) << " + "
406               << formatv("{0:x}", E.getOffset()) << ")";
407   }
408   return make_error<JITLinkError>(std::move(ErrMsg));
409 }
410 
411 Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value, int N,
412                          const Edge &E) {
413   return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
414                                   " improper alignment for relocation " +
415                                   formatv("{0:d}", E.getKind()) + ": 0x" +
416                                   llvm::utohexstr(Value) +
417                                   " is not aligned to " + Twine(N) + " bytes");
418 }
419 
420 Expected<std::unique_ptr<LinkGraph>>
421 createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
422   auto Magic = identify_magic(ObjectBuffer.getBuffer());
423   switch (Magic) {
424   case file_magic::macho_object:
425     return createLinkGraphFromMachOObject(ObjectBuffer);
426   case file_magic::elf_relocatable:
427     return createLinkGraphFromELFObject(ObjectBuffer);
428   case file_magic::coff_object:
429     return createLinkGraphFromCOFFObject(ObjectBuffer);
430   default:
431     return make_error<JITLinkError>("Unsupported file format");
432   };
433 }
434 
435 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
436   switch (G->getTargetTriple().getObjectFormat()) {
437   case Triple::MachO:
438     return link_MachO(std::move(G), std::move(Ctx));
439   case Triple::ELF:
440     return link_ELF(std::move(G), std::move(Ctx));
441   case Triple::COFF:
442     return link_COFF(std::move(G), std::move(Ctx));
443   default:
444     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
445   };
446 }
447 
448 } // end namespace jitlink
449 } // end namespace llvm
450