xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
20b57cec5SDimitry Andric //
3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/JITLink.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "llvm/BinaryFormat/Magic.h"
125ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/ELF.h"
130b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/MachO.h"
140b57cec5SDimitry Andric #include "llvm/Support/Format.h"
150b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
160b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric using namespace llvm::object;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define DEBUG_TYPE "jitlink"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace {
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric enum JITLinkErrorCode { GenericJITLinkError = 1 };
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
290b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
300b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
310b57cec5SDimitry Andric class JITLinkerErrorCategory : public std::error_category {
320b57cec5SDimitry Andric public:
330b57cec5SDimitry Andric   const char *name() const noexcept override { return "runtimedyld"; }
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   std::string message(int Condition) const override {
360b57cec5SDimitry Andric     switch (static_cast<JITLinkErrorCode>(Condition)) {
370b57cec5SDimitry Andric     case GenericJITLinkError:
380b57cec5SDimitry Andric       return "Generic JITLink error";
390b57cec5SDimitry Andric     }
400b57cec5SDimitry Andric     llvm_unreachable("Unrecognized JITLinkErrorCode");
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric };
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric } // namespace
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric namespace llvm {
490b57cec5SDimitry Andric namespace jitlink {
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric char JITLinkError::ID = 0;
520b57cec5SDimitry Andric 
53fe6060f1SDimitry Andric void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric std::error_code JITLinkError::convertToErrorCode() const {
560b57cec5SDimitry Andric   return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric 
598bcb0991SDimitry Andric const char *getGenericEdgeKindName(Edge::Kind K) {
600b57cec5SDimitry Andric   switch (K) {
610b57cec5SDimitry Andric   case Edge::Invalid:
620b57cec5SDimitry Andric     return "INVALID RELOCATION";
630b57cec5SDimitry Andric   case Edge::KeepAlive:
640b57cec5SDimitry Andric     return "Keep-Alive";
650b57cec5SDimitry Andric   default:
66e8d8bef9SDimitry Andric     return "<Unrecognized edge kind>";
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
708bcb0991SDimitry Andric const char *getLinkageName(Linkage L) {
718bcb0991SDimitry Andric   switch (L) {
728bcb0991SDimitry Andric   case Linkage::Strong:
738bcb0991SDimitry Andric     return "strong";
748bcb0991SDimitry Andric   case Linkage::Weak:
758bcb0991SDimitry Andric     return "weak";
768bcb0991SDimitry Andric   }
778bcb0991SDimitry Andric   llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
788bcb0991SDimitry Andric }
798bcb0991SDimitry Andric 
808bcb0991SDimitry Andric const char *getScopeName(Scope S) {
818bcb0991SDimitry Andric   switch (S) {
828bcb0991SDimitry Andric   case Scope::Default:
838bcb0991SDimitry Andric     return "default";
848bcb0991SDimitry Andric   case Scope::Hidden:
858bcb0991SDimitry Andric     return "hidden";
868bcb0991SDimitry Andric   case Scope::Local:
878bcb0991SDimitry Andric     return "local";
888bcb0991SDimitry Andric   }
898bcb0991SDimitry Andric   llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
908bcb0991SDimitry Andric }
918bcb0991SDimitry Andric 
928bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
938bcb0991SDimitry Andric   return OS << formatv("{0:x16}", B.getAddress()) << " -- "
94fe6060f1SDimitry Andric             << formatv("{0:x8}", B.getAddress() + B.getSize()) << ": "
95fe6060f1SDimitry Andric             << "size = " << formatv("{0:x8}", B.getSize()) << ", "
968bcb0991SDimitry Andric             << (B.isZeroFill() ? "zero-fill" : "content")
978bcb0991SDimitry Andric             << ", align = " << B.getAlignment()
988bcb0991SDimitry Andric             << ", align-ofs = " << B.getAlignmentOffset()
998bcb0991SDimitry Andric             << ", section = " << B.getSection().getName();
1008bcb0991SDimitry Andric }
1018bcb0991SDimitry Andric 
1028bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
103fe6060f1SDimitry Andric   OS << formatv("{0:x16}", Sym.getAddress()) << " ("
104fe6060f1SDimitry Andric      << (Sym.isDefined() ? "block" : "addressable") << " + "
105fe6060f1SDimitry Andric      << formatv("{0:x8}", Sym.getOffset())
106fe6060f1SDimitry Andric      << "): size: " << formatv("{0:x8}", Sym.getSize())
107fe6060f1SDimitry Andric      << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
108fe6060f1SDimitry Andric      << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
109fe6060f1SDimitry Andric      << (Sym.isLive() ? "live" : "dead") << "  -   "
110fe6060f1SDimitry Andric      << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
1110b57cec5SDimitry Andric   return OS;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1148bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
1150b57cec5SDimitry Andric                StringRef EdgeKindName) {
1168bcb0991SDimitry Andric   OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": "
117e8d8bef9SDimitry Andric      << formatv("{0:x16}", B.getAddress()) << " + "
118e8d8bef9SDimitry Andric      << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName << " -> ";
119e8d8bef9SDimitry Andric 
120e8d8bef9SDimitry Andric   auto &TargetSym = E.getTarget();
121e8d8bef9SDimitry Andric   if (TargetSym.hasName())
122e8d8bef9SDimitry Andric     OS << TargetSym.getName();
123e8d8bef9SDimitry Andric   else {
124e8d8bef9SDimitry Andric     auto &TargetBlock = TargetSym.getBlock();
125e8d8bef9SDimitry Andric     auto &TargetSec = TargetBlock.getSection();
126e8d8bef9SDimitry Andric     JITTargetAddress SecAddress = ~JITTargetAddress(0);
127e8d8bef9SDimitry Andric     for (auto *B : TargetSec.blocks())
128e8d8bef9SDimitry Andric       if (B->getAddress() < SecAddress)
129e8d8bef9SDimitry Andric         SecAddress = B->getAddress();
130e8d8bef9SDimitry Andric 
131e8d8bef9SDimitry Andric     JITTargetAddress SecDelta = TargetSym.getAddress() - SecAddress;
132e8d8bef9SDimitry Andric     OS << formatv("{0:x16}", TargetSym.getAddress()) << " (section "
133e8d8bef9SDimitry Andric        << TargetSec.getName();
134e8d8bef9SDimitry Andric     if (SecDelta)
135e8d8bef9SDimitry Andric       OS << " + " << formatv("{0:x}", SecDelta);
136e8d8bef9SDimitry Andric     OS << " / block " << formatv("{0:x16}", TargetBlock.getAddress());
137e8d8bef9SDimitry Andric     if (TargetSym.getOffset())
138e8d8bef9SDimitry Andric       OS << " + " << formatv("{0:x}", TargetSym.getOffset());
139e8d8bef9SDimitry Andric     OS << ")";
140e8d8bef9SDimitry Andric   }
141e8d8bef9SDimitry Andric 
142e8d8bef9SDimitry Andric   if (E.getAddend() != 0)
143e8d8bef9SDimitry Andric     OS << " + " << E.getAddend();
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric Section::~Section() {
1478bcb0991SDimitry Andric   for (auto *Sym : Symbols)
1488bcb0991SDimitry Andric     Sym->~Symbol();
1498bcb0991SDimitry Andric   for (auto *B : Blocks)
1508bcb0991SDimitry Andric     B->~Block();
1518bcb0991SDimitry Andric }
1528bcb0991SDimitry Andric 
153480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
154480093f4SDimitry Andric                              SplitBlockCache *Cache) {
155480093f4SDimitry Andric 
156480093f4SDimitry Andric   assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
157480093f4SDimitry Andric 
158480093f4SDimitry Andric   // If the split point covers all of B then just return B.
159480093f4SDimitry Andric   if (SplitIndex == B.getSize())
160480093f4SDimitry Andric     return B;
161480093f4SDimitry Andric 
162480093f4SDimitry Andric   assert(SplitIndex < B.getSize() && "SplitIndex out of range");
163480093f4SDimitry Andric 
164480093f4SDimitry Andric   // Create the new block covering [ 0, SplitIndex ).
165480093f4SDimitry Andric   auto &NewBlock =
166480093f4SDimitry Andric       B.isZeroFill()
167480093f4SDimitry Andric           ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
168480093f4SDimitry Andric                                 B.getAlignment(), B.getAlignmentOffset())
169480093f4SDimitry Andric           : createContentBlock(
170fe6060f1SDimitry Andric                 B.getSection(), B.getContent().slice(0, SplitIndex),
171480093f4SDimitry Andric                 B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
172480093f4SDimitry Andric 
173480093f4SDimitry Andric   // Modify B to cover [ SplitIndex, B.size() ).
174480093f4SDimitry Andric   B.setAddress(B.getAddress() + SplitIndex);
175fe6060f1SDimitry Andric   B.setContent(B.getContent().slice(SplitIndex));
176480093f4SDimitry Andric   B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
177480093f4SDimitry Andric                        B.getAlignment());
178480093f4SDimitry Andric 
179480093f4SDimitry Andric   // Handle edge transfer/update.
180480093f4SDimitry Andric   {
181480093f4SDimitry Andric     // Copy edges to NewBlock (recording their iterators so that we can remove
182480093f4SDimitry Andric     // them from B), and update of Edges remaining on B.
183480093f4SDimitry Andric     std::vector<Block::edge_iterator> EdgesToRemove;
1845ffd83dbSDimitry Andric     for (auto I = B.edges().begin(); I != B.edges().end();) {
185480093f4SDimitry Andric       if (I->getOffset() < SplitIndex) {
186480093f4SDimitry Andric         NewBlock.addEdge(*I);
1875ffd83dbSDimitry Andric         I = B.removeEdge(I);
1885ffd83dbSDimitry Andric       } else {
189480093f4SDimitry Andric         I->setOffset(I->getOffset() - SplitIndex);
1905ffd83dbSDimitry Andric         ++I;
191480093f4SDimitry Andric       }
192480093f4SDimitry Andric     }
193480093f4SDimitry Andric   }
194480093f4SDimitry Andric 
195480093f4SDimitry Andric   // Handle symbol transfer/update.
196480093f4SDimitry Andric   {
197480093f4SDimitry Andric     // Initialize the symbols cache if necessary.
198480093f4SDimitry Andric     SplitBlockCache LocalBlockSymbolsCache;
199480093f4SDimitry Andric     if (!Cache)
200480093f4SDimitry Andric       Cache = &LocalBlockSymbolsCache;
201480093f4SDimitry Andric     if (*Cache == None) {
202480093f4SDimitry Andric       *Cache = SplitBlockCache::value_type();
203480093f4SDimitry Andric       for (auto *Sym : B.getSection().symbols())
204480093f4SDimitry Andric         if (&Sym->getBlock() == &B)
205480093f4SDimitry Andric           (*Cache)->push_back(Sym);
206480093f4SDimitry Andric 
207480093f4SDimitry Andric       llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
208480093f4SDimitry Andric         return LHS->getOffset() > RHS->getOffset();
209480093f4SDimitry Andric       });
210480093f4SDimitry Andric     }
211480093f4SDimitry Andric     auto &BlockSymbols = **Cache;
212480093f4SDimitry Andric 
213480093f4SDimitry Andric     // Transfer all symbols with offset less than SplitIndex to NewBlock.
214480093f4SDimitry Andric     while (!BlockSymbols.empty() &&
215480093f4SDimitry Andric            BlockSymbols.back()->getOffset() < SplitIndex) {
216*349cc55cSDimitry Andric       auto *Sym = BlockSymbols.back();
217*349cc55cSDimitry Andric       // If the symbol extends beyond the split, update the size to be within
218*349cc55cSDimitry Andric       // the new block.
219*349cc55cSDimitry Andric       if (Sym->getOffset() + Sym->getSize() > SplitIndex)
220*349cc55cSDimitry Andric         Sym->setSize(SplitIndex - Sym->getOffset());
221*349cc55cSDimitry Andric       Sym->setBlock(NewBlock);
222480093f4SDimitry Andric       BlockSymbols.pop_back();
223480093f4SDimitry Andric     }
224480093f4SDimitry Andric 
225480093f4SDimitry Andric     // Update offsets for all remaining symbols in B.
226480093f4SDimitry Andric     for (auto *Sym : BlockSymbols)
227480093f4SDimitry Andric       Sym->setOffset(Sym->getOffset() - SplitIndex);
228480093f4SDimitry Andric   }
229480093f4SDimitry Andric 
230480093f4SDimitry Andric   return NewBlock;
231480093f4SDimitry Andric }
232480093f4SDimitry Andric 
233fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) {
234fe6060f1SDimitry Andric   DenseMap<Block *, std::vector<Symbol *>> BlockSymbols;
2350b57cec5SDimitry Andric 
236fe6060f1SDimitry Andric   // Map from blocks to the symbols pointing at them.
237fe6060f1SDimitry Andric   for (auto *Sym : defined_symbols())
238fe6060f1SDimitry Andric     BlockSymbols[&Sym->getBlock()].push_back(Sym);
2390b57cec5SDimitry Andric 
240fe6060f1SDimitry Andric   // For each block, sort its symbols by something approximating
241fe6060f1SDimitry Andric   // relevance.
242fe6060f1SDimitry Andric   for (auto &KV : BlockSymbols)
243fe6060f1SDimitry Andric     llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
244fe6060f1SDimitry Andric       if (LHS->getOffset() != RHS->getOffset())
245fe6060f1SDimitry Andric         return LHS->getOffset() < RHS->getOffset();
246fe6060f1SDimitry Andric       if (LHS->getLinkage() != RHS->getLinkage())
247fe6060f1SDimitry Andric         return LHS->getLinkage() < RHS->getLinkage();
248fe6060f1SDimitry Andric       if (LHS->getScope() != RHS->getScope())
249fe6060f1SDimitry Andric         return LHS->getScope() < RHS->getScope();
250fe6060f1SDimitry Andric       if (LHS->hasName()) {
251fe6060f1SDimitry Andric         if (!RHS->hasName())
252fe6060f1SDimitry Andric           return true;
253fe6060f1SDimitry Andric         return LHS->getName() < RHS->getName();
2540b57cec5SDimitry Andric       }
255fe6060f1SDimitry Andric       return false;
256fe6060f1SDimitry Andric     });
257fe6060f1SDimitry Andric 
258fe6060f1SDimitry Andric   for (auto &Sec : sections()) {
259fe6060f1SDimitry Andric     OS << "section " << Sec.getName() << ":\n\n";
260fe6060f1SDimitry Andric 
261fe6060f1SDimitry Andric     std::vector<Block *> SortedBlocks;
262fe6060f1SDimitry Andric     llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
263fe6060f1SDimitry Andric     llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
264fe6060f1SDimitry Andric       return LHS->getAddress() < RHS->getAddress();
265fe6060f1SDimitry Andric     });
266fe6060f1SDimitry Andric 
267fe6060f1SDimitry Andric     for (auto *B : SortedBlocks) {
268fe6060f1SDimitry Andric       OS << "  block " << formatv("{0:x16}", B->getAddress())
269fe6060f1SDimitry Andric          << " size = " << formatv("{0:x8}", B->getSize())
270fe6060f1SDimitry Andric          << ", align = " << B->getAlignment()
271fe6060f1SDimitry Andric          << ", alignment-offset = " << B->getAlignmentOffset();
272fe6060f1SDimitry Andric       if (B->isZeroFill())
273fe6060f1SDimitry Andric         OS << ", zero-fill";
274fe6060f1SDimitry Andric       OS << "\n";
275fe6060f1SDimitry Andric 
276fe6060f1SDimitry Andric       auto BlockSymsI = BlockSymbols.find(B);
277fe6060f1SDimitry Andric       if (BlockSymsI != BlockSymbols.end()) {
278fe6060f1SDimitry Andric         OS << "    symbols:\n";
279fe6060f1SDimitry Andric         auto &Syms = BlockSymsI->second;
280fe6060f1SDimitry Andric         for (auto *Sym : Syms)
281fe6060f1SDimitry Andric           OS << "      " << *Sym << "\n";
282fe6060f1SDimitry Andric       } else
283fe6060f1SDimitry Andric         OS << "    no symbols\n";
284fe6060f1SDimitry Andric 
285fe6060f1SDimitry Andric       if (!B->edges_empty()) {
286fe6060f1SDimitry Andric         OS << "    edges:\n";
287fe6060f1SDimitry Andric         std::vector<Edge> SortedEdges;
288fe6060f1SDimitry Andric         llvm::copy(B->edges(), std::back_inserter(SortedEdges));
289fe6060f1SDimitry Andric         llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
290fe6060f1SDimitry Andric           return LHS.getOffset() < RHS.getOffset();
291fe6060f1SDimitry Andric         });
292fe6060f1SDimitry Andric         for (auto &E : SortedEdges) {
293fe6060f1SDimitry Andric           OS << "      " << formatv("{0:x16}", B->getFixupAddress(E))
294fe6060f1SDimitry Andric              << " (block + " << formatv("{0:x8}", E.getOffset())
295fe6060f1SDimitry Andric              << "), addend = ";
296fe6060f1SDimitry Andric           if (E.getAddend() >= 0)
297fe6060f1SDimitry Andric             OS << formatv("+{0:x8}", E.getAddend());
298fe6060f1SDimitry Andric           else
299fe6060f1SDimitry Andric             OS << formatv("-{0:x8}", -E.getAddend());
300fe6060f1SDimitry Andric           OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
301fe6060f1SDimitry Andric           if (E.getTarget().hasName())
302fe6060f1SDimitry Andric             OS << E.getTarget().getName();
303fe6060f1SDimitry Andric           else
304fe6060f1SDimitry Andric             OS << "addressable@"
305fe6060f1SDimitry Andric                << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
306fe6060f1SDimitry Andric                << formatv("{0:x8}", E.getTarget().getOffset());
3070b57cec5SDimitry Andric           OS << "\n";
3080b57cec5SDimitry Andric         }
309fe6060f1SDimitry Andric       } else
310fe6060f1SDimitry Andric         OS << "    no edges\n";
311fe6060f1SDimitry Andric       OS << "\n";
3120b57cec5SDimitry Andric     }
3138bcb0991SDimitry Andric   }
3140b57cec5SDimitry Andric 
3158bcb0991SDimitry Andric   OS << "Absolute symbols:\n";
316fe6060f1SDimitry Andric   if (!llvm::empty(absolute_symbols())) {
3178bcb0991SDimitry Andric     for (auto *Sym : absolute_symbols())
3188bcb0991SDimitry Andric       OS << "  " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
3190b57cec5SDimitry Andric          << "\n";
320fe6060f1SDimitry Andric   } else
321fe6060f1SDimitry Andric     OS << "  none\n";
3220b57cec5SDimitry Andric 
323fe6060f1SDimitry Andric   OS << "\nExternal symbols:\n";
324fe6060f1SDimitry Andric   if (!llvm::empty(external_symbols())) {
3258bcb0991SDimitry Andric     for (auto *Sym : external_symbols())
3268bcb0991SDimitry Andric       OS << "  " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
3270b57cec5SDimitry Andric          << "\n";
328fe6060f1SDimitry Andric   } else
329fe6060f1SDimitry Andric     OS << "  none\n";
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
332480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
333480093f4SDimitry Andric   switch (LF) {
334480093f4SDimitry Andric   case SymbolLookupFlags::RequiredSymbol:
335480093f4SDimitry Andric     return OS << "RequiredSymbol";
336480093f4SDimitry Andric   case SymbolLookupFlags::WeaklyReferencedSymbol:
337480093f4SDimitry Andric     return OS << "WeaklyReferencedSymbol";
338480093f4SDimitry Andric   }
339480093f4SDimitry Andric   llvm_unreachable("Unrecognized lookup flags");
340480093f4SDimitry Andric }
341480093f4SDimitry Andric 
3428bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {}
3438bcb0991SDimitry Andric 
3440b57cec5SDimitry Andric JITLinkContext::~JITLinkContext() {}
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
3470b57cec5SDimitry Andric   return true;
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
3508bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
3518bcb0991SDimitry Andric   return LinkGraphPassFunction();
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric 
354fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G,
3550b57cec5SDimitry Andric                                        PassConfiguration &Config) {
3560b57cec5SDimitry Andric   return Error::success();
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
3598bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) {
3608bcb0991SDimitry Andric   for (auto *Sym : G.defined_symbols())
3618bcb0991SDimitry Andric     Sym->setLive(true);
3620b57cec5SDimitry Andric   return Error::success();
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric 
365fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
366fe6060f1SDimitry Andric                                 const Edge &E) {
367fe6060f1SDimitry Andric   std::string ErrMsg;
368fe6060f1SDimitry Andric   {
369fe6060f1SDimitry Andric     raw_string_ostream ErrStream(ErrMsg);
370fe6060f1SDimitry Andric     Section &Sec = B.getSection();
371fe6060f1SDimitry Andric     ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
372fe6060f1SDimitry Andric               << ": relocation target ";
373fe6060f1SDimitry Andric     if (E.getTarget().hasName())
374fe6060f1SDimitry Andric       ErrStream << "\"" << E.getTarget().getName() << "\" ";
375fe6060f1SDimitry Andric     ErrStream << "at address " << formatv("{0:x}", E.getTarget().getAddress());
376fe6060f1SDimitry Andric     ErrStream << " is out of range of " << G.getEdgeKindName(E.getKind())
377fe6060f1SDimitry Andric               << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
378fe6060f1SDimitry Andric 
379fe6060f1SDimitry Andric     Symbol *BestSymbolForBlock = nullptr;
380fe6060f1SDimitry Andric     for (auto *Sym : Sec.symbols())
381fe6060f1SDimitry Andric       if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
382fe6060f1SDimitry Andric           (!BestSymbolForBlock ||
383fe6060f1SDimitry Andric            Sym->getScope() < BestSymbolForBlock->getScope() ||
384fe6060f1SDimitry Andric            Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
385fe6060f1SDimitry Andric         BestSymbolForBlock = Sym;
386fe6060f1SDimitry Andric 
387fe6060f1SDimitry Andric     if (BestSymbolForBlock)
388fe6060f1SDimitry Andric       ErrStream << BestSymbolForBlock->getName() << ", ";
389fe6060f1SDimitry Andric     else
390fe6060f1SDimitry Andric       ErrStream << "<anonymous block> @ ";
391fe6060f1SDimitry Andric 
392fe6060f1SDimitry Andric     ErrStream << formatv("{0:x}", B.getAddress()) << " + "
393fe6060f1SDimitry Andric               << formatv("{0:x}", E.getOffset()) << ")";
394fe6060f1SDimitry Andric   }
395fe6060f1SDimitry Andric   return make_error<JITLinkError>(std::move(ErrMsg));
396fe6060f1SDimitry Andric }
397fe6060f1SDimitry Andric 
398e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>>
399e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
400e8d8bef9SDimitry Andric   auto Magic = identify_magic(ObjectBuffer.getBuffer());
4010b57cec5SDimitry Andric   switch (Magic) {
4020b57cec5SDimitry Andric   case file_magic::macho_object:
403fe6060f1SDimitry Andric     return createLinkGraphFromMachOObject(ObjectBuffer);
4045ffd83dbSDimitry Andric   case file_magic::elf_relocatable:
405fe6060f1SDimitry Andric     return createLinkGraphFromELFObject(ObjectBuffer);
4060b57cec5SDimitry Andric   default:
407e8d8bef9SDimitry Andric     return make_error<JITLinkError>("Unsupported file format");
408e8d8bef9SDimitry Andric   };
409e8d8bef9SDimitry Andric }
410e8d8bef9SDimitry Andric 
411e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
412e8d8bef9SDimitry Andric   switch (G->getTargetTriple().getObjectFormat()) {
413e8d8bef9SDimitry Andric   case Triple::MachO:
414e8d8bef9SDimitry Andric     return link_MachO(std::move(G), std::move(Ctx));
415e8d8bef9SDimitry Andric   case Triple::ELF:
416e8d8bef9SDimitry Andric     return link_ELF(std::move(G), std::move(Ctx));
417e8d8bef9SDimitry Andric   default:
418e8d8bef9SDimitry Andric     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
4190b57cec5SDimitry Andric   };
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric } // end namespace jitlink
4230b57cec5SDimitry Andric } // end namespace llvm
424