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