xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
10b57cec5SDimitry Andric //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
20b57cec5SDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry 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"
12*753f127fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/COFF.h"
135ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/ELF.h"
140b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/MachO.h"
150b57cec5SDimitry Andric #include "llvm/Support/Format.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 } // namespace
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric namespace llvm {
470b57cec5SDimitry Andric namespace jitlink {
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric char JITLinkError::ID = 0;
500b57cec5SDimitry Andric 
51fe6060f1SDimitry Andric void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric std::error_code JITLinkError::convertToErrorCode() const {
54*753f127fSDimitry Andric   static JITLinkerErrorCategory TheJITLinkerErrorCategory;
55*753f127fSDimitry Andric   return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
588bcb0991SDimitry Andric const char *getGenericEdgeKindName(Edge::Kind K) {
590b57cec5SDimitry Andric   switch (K) {
600b57cec5SDimitry Andric   case Edge::Invalid:
610b57cec5SDimitry Andric     return "INVALID RELOCATION";
620b57cec5SDimitry Andric   case Edge::KeepAlive:
630b57cec5SDimitry Andric     return "Keep-Alive";
640b57cec5SDimitry Andric   default:
65e8d8bef9SDimitry Andric     return "<Unrecognized edge kind>";
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
698bcb0991SDimitry Andric const char *getLinkageName(Linkage L) {
708bcb0991SDimitry Andric   switch (L) {
718bcb0991SDimitry Andric   case Linkage::Strong:
728bcb0991SDimitry Andric     return "strong";
738bcb0991SDimitry Andric   case Linkage::Weak:
748bcb0991SDimitry Andric     return "weak";
758bcb0991SDimitry Andric   }
768bcb0991SDimitry Andric   llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
778bcb0991SDimitry Andric }
788bcb0991SDimitry Andric 
798bcb0991SDimitry Andric const char *getScopeName(Scope S) {
808bcb0991SDimitry Andric   switch (S) {
818bcb0991SDimitry Andric   case Scope::Default:
828bcb0991SDimitry Andric     return "default";
838bcb0991SDimitry Andric   case Scope::Hidden:
848bcb0991SDimitry Andric     return "hidden";
858bcb0991SDimitry Andric   case Scope::Local:
868bcb0991SDimitry Andric     return "local";
878bcb0991SDimitry Andric   }
888bcb0991SDimitry Andric   llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
898bcb0991SDimitry Andric }
908bcb0991SDimitry Andric 
918bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
9204eeddc0SDimitry Andric   return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
9304eeddc0SDimitry Andric             << ": "
94fe6060f1SDimitry Andric             << "size = " << formatv("{0:x8}", B.getSize()) << ", "
958bcb0991SDimitry Andric             << (B.isZeroFill() ? "zero-fill" : "content")
968bcb0991SDimitry Andric             << ", align = " << B.getAlignment()
978bcb0991SDimitry Andric             << ", align-ofs = " << B.getAlignmentOffset()
988bcb0991SDimitry Andric             << ", section = " << B.getSection().getName();
998bcb0991SDimitry Andric }
1008bcb0991SDimitry Andric 
1018bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
10204eeddc0SDimitry Andric   OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
10304eeddc0SDimitry Andric      << " + " << formatv("{0:x8}", Sym.getOffset())
104fe6060f1SDimitry Andric      << "): size: " << formatv("{0:x8}", Sym.getSize())
105fe6060f1SDimitry Andric      << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
106fe6060f1SDimitry Andric      << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
107fe6060f1SDimitry Andric      << (Sym.isLive() ? "live" : "dead") << "  -   "
108fe6060f1SDimitry Andric      << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
1090b57cec5SDimitry Andric   return OS;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
1128bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
1130b57cec5SDimitry Andric                StringRef EdgeKindName) {
11404eeddc0SDimitry Andric   OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
11504eeddc0SDimitry Andric      << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
11604eeddc0SDimitry Andric      << " -> ";
117e8d8bef9SDimitry Andric 
118e8d8bef9SDimitry Andric   auto &TargetSym = E.getTarget();
119e8d8bef9SDimitry Andric   if (TargetSym.hasName())
120e8d8bef9SDimitry Andric     OS << TargetSym.getName();
121e8d8bef9SDimitry Andric   else {
122e8d8bef9SDimitry Andric     auto &TargetBlock = TargetSym.getBlock();
123e8d8bef9SDimitry Andric     auto &TargetSec = TargetBlock.getSection();
12404eeddc0SDimitry Andric     orc::ExecutorAddr SecAddress(~uint64_t(0));
125e8d8bef9SDimitry Andric     for (auto *B : TargetSec.blocks())
126e8d8bef9SDimitry Andric       if (B->getAddress() < SecAddress)
127e8d8bef9SDimitry Andric         SecAddress = B->getAddress();
128e8d8bef9SDimitry Andric 
12904eeddc0SDimitry Andric     orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
13004eeddc0SDimitry Andric     OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
131e8d8bef9SDimitry Andric     if (SecDelta)
132e8d8bef9SDimitry Andric       OS << " + " << formatv("{0:x}", SecDelta);
13304eeddc0SDimitry Andric     OS << " / block " << TargetBlock.getAddress();
134e8d8bef9SDimitry Andric     if (TargetSym.getOffset())
135e8d8bef9SDimitry Andric       OS << " + " << formatv("{0:x}", TargetSym.getOffset());
136e8d8bef9SDimitry Andric     OS << ")";
137e8d8bef9SDimitry Andric   }
138e8d8bef9SDimitry Andric 
139e8d8bef9SDimitry Andric   if (E.getAddend() != 0)
140e8d8bef9SDimitry Andric     OS << " + " << E.getAddend();
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric Section::~Section() {
1448bcb0991SDimitry Andric   for (auto *Sym : Symbols)
1458bcb0991SDimitry Andric     Sym->~Symbol();
1468bcb0991SDimitry Andric   for (auto *B : Blocks)
1478bcb0991SDimitry Andric     B->~Block();
1488bcb0991SDimitry Andric }
1498bcb0991SDimitry Andric 
150480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
151480093f4SDimitry Andric                              SplitBlockCache *Cache) {
152480093f4SDimitry Andric 
153480093f4SDimitry Andric   assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
154480093f4SDimitry Andric 
155480093f4SDimitry Andric   // If the split point covers all of B then just return B.
156480093f4SDimitry Andric   if (SplitIndex == B.getSize())
157480093f4SDimitry Andric     return B;
158480093f4SDimitry Andric 
159480093f4SDimitry Andric   assert(SplitIndex < B.getSize() && "SplitIndex out of range");
160480093f4SDimitry Andric 
161480093f4SDimitry Andric   // Create the new block covering [ 0, SplitIndex ).
162480093f4SDimitry Andric   auto &NewBlock =
163480093f4SDimitry Andric       B.isZeroFill()
164480093f4SDimitry Andric           ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
165480093f4SDimitry Andric                                 B.getAlignment(), B.getAlignmentOffset())
166480093f4SDimitry Andric           : createContentBlock(
167fe6060f1SDimitry Andric                 B.getSection(), B.getContent().slice(0, SplitIndex),
168480093f4SDimitry Andric                 B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
169480093f4SDimitry Andric 
170480093f4SDimitry Andric   // Modify B to cover [ SplitIndex, B.size() ).
171480093f4SDimitry Andric   B.setAddress(B.getAddress() + SplitIndex);
172fe6060f1SDimitry Andric   B.setContent(B.getContent().slice(SplitIndex));
173480093f4SDimitry Andric   B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
174480093f4SDimitry Andric                        B.getAlignment());
175480093f4SDimitry Andric 
176480093f4SDimitry Andric   // Handle edge transfer/update.
177480093f4SDimitry Andric   {
178480093f4SDimitry Andric     // Copy edges to NewBlock (recording their iterators so that we can remove
179480093f4SDimitry Andric     // them from B), and update of Edges remaining on B.
180480093f4SDimitry Andric     std::vector<Block::edge_iterator> EdgesToRemove;
1815ffd83dbSDimitry Andric     for (auto I = B.edges().begin(); I != B.edges().end();) {
182480093f4SDimitry Andric       if (I->getOffset() < SplitIndex) {
183480093f4SDimitry Andric         NewBlock.addEdge(*I);
1845ffd83dbSDimitry Andric         I = B.removeEdge(I);
1855ffd83dbSDimitry Andric       } else {
186480093f4SDimitry Andric         I->setOffset(I->getOffset() - SplitIndex);
1875ffd83dbSDimitry Andric         ++I;
188480093f4SDimitry Andric       }
189480093f4SDimitry Andric     }
190480093f4SDimitry Andric   }
191480093f4SDimitry Andric 
192480093f4SDimitry Andric   // Handle symbol transfer/update.
193480093f4SDimitry Andric   {
194480093f4SDimitry Andric     // Initialize the symbols cache if necessary.
195480093f4SDimitry Andric     SplitBlockCache LocalBlockSymbolsCache;
196480093f4SDimitry Andric     if (!Cache)
197480093f4SDimitry Andric       Cache = &LocalBlockSymbolsCache;
198480093f4SDimitry Andric     if (*Cache == None) {
199480093f4SDimitry Andric       *Cache = SplitBlockCache::value_type();
200480093f4SDimitry Andric       for (auto *Sym : B.getSection().symbols())
201480093f4SDimitry Andric         if (&Sym->getBlock() == &B)
202480093f4SDimitry Andric           (*Cache)->push_back(Sym);
203480093f4SDimitry Andric 
204480093f4SDimitry Andric       llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
205480093f4SDimitry Andric         return LHS->getOffset() > RHS->getOffset();
206480093f4SDimitry Andric       });
207480093f4SDimitry Andric     }
208480093f4SDimitry Andric     auto &BlockSymbols = **Cache;
209480093f4SDimitry Andric 
210480093f4SDimitry Andric     // Transfer all symbols with offset less than SplitIndex to NewBlock.
211480093f4SDimitry Andric     while (!BlockSymbols.empty() &&
212480093f4SDimitry Andric            BlockSymbols.back()->getOffset() < SplitIndex) {
213349cc55cSDimitry Andric       auto *Sym = BlockSymbols.back();
214349cc55cSDimitry Andric       // If the symbol extends beyond the split, update the size to be within
215349cc55cSDimitry Andric       // the new block.
216349cc55cSDimitry Andric       if (Sym->getOffset() + Sym->getSize() > SplitIndex)
217349cc55cSDimitry Andric         Sym->setSize(SplitIndex - Sym->getOffset());
218349cc55cSDimitry Andric       Sym->setBlock(NewBlock);
219480093f4SDimitry Andric       BlockSymbols.pop_back();
220480093f4SDimitry Andric     }
221480093f4SDimitry Andric 
222480093f4SDimitry Andric     // Update offsets for all remaining symbols in B.
223480093f4SDimitry Andric     for (auto *Sym : BlockSymbols)
224480093f4SDimitry Andric       Sym->setOffset(Sym->getOffset() - SplitIndex);
225480093f4SDimitry Andric   }
226480093f4SDimitry Andric 
227480093f4SDimitry Andric   return NewBlock;
228480093f4SDimitry Andric }
229480093f4SDimitry Andric 
230fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) {
231fe6060f1SDimitry Andric   DenseMap<Block *, std::vector<Symbol *>> BlockSymbols;
2320b57cec5SDimitry Andric 
233fe6060f1SDimitry Andric   // Map from blocks to the symbols pointing at them.
234fe6060f1SDimitry Andric   for (auto *Sym : defined_symbols())
235fe6060f1SDimitry Andric     BlockSymbols[&Sym->getBlock()].push_back(Sym);
2360b57cec5SDimitry Andric 
237fe6060f1SDimitry Andric   // For each block, sort its symbols by something approximating
238fe6060f1SDimitry Andric   // relevance.
239fe6060f1SDimitry Andric   for (auto &KV : BlockSymbols)
240fe6060f1SDimitry Andric     llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
241fe6060f1SDimitry Andric       if (LHS->getOffset() != RHS->getOffset())
242fe6060f1SDimitry Andric         return LHS->getOffset() < RHS->getOffset();
243fe6060f1SDimitry Andric       if (LHS->getLinkage() != RHS->getLinkage())
244fe6060f1SDimitry Andric         return LHS->getLinkage() < RHS->getLinkage();
245fe6060f1SDimitry Andric       if (LHS->getScope() != RHS->getScope())
246fe6060f1SDimitry Andric         return LHS->getScope() < RHS->getScope();
247fe6060f1SDimitry Andric       if (LHS->hasName()) {
248fe6060f1SDimitry Andric         if (!RHS->hasName())
249fe6060f1SDimitry Andric           return true;
250fe6060f1SDimitry Andric         return LHS->getName() < RHS->getName();
2510b57cec5SDimitry Andric       }
252fe6060f1SDimitry Andric       return false;
253fe6060f1SDimitry Andric     });
254fe6060f1SDimitry Andric 
255fe6060f1SDimitry Andric   for (auto &Sec : sections()) {
256fe6060f1SDimitry Andric     OS << "section " << Sec.getName() << ":\n\n";
257fe6060f1SDimitry Andric 
258fe6060f1SDimitry Andric     std::vector<Block *> SortedBlocks;
259fe6060f1SDimitry Andric     llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
260fe6060f1SDimitry Andric     llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
261fe6060f1SDimitry Andric       return LHS->getAddress() < RHS->getAddress();
262fe6060f1SDimitry Andric     });
263fe6060f1SDimitry Andric 
264fe6060f1SDimitry Andric     for (auto *B : SortedBlocks) {
26504eeddc0SDimitry Andric       OS << "  block " << B->getAddress()
266fe6060f1SDimitry Andric          << " size = " << formatv("{0:x8}", B->getSize())
267fe6060f1SDimitry Andric          << ", align = " << B->getAlignment()
268fe6060f1SDimitry Andric          << ", alignment-offset = " << B->getAlignmentOffset();
269fe6060f1SDimitry Andric       if (B->isZeroFill())
270fe6060f1SDimitry Andric         OS << ", zero-fill";
271fe6060f1SDimitry Andric       OS << "\n";
272fe6060f1SDimitry Andric 
273fe6060f1SDimitry Andric       auto BlockSymsI = BlockSymbols.find(B);
274fe6060f1SDimitry Andric       if (BlockSymsI != BlockSymbols.end()) {
275fe6060f1SDimitry Andric         OS << "    symbols:\n";
276fe6060f1SDimitry Andric         auto &Syms = BlockSymsI->second;
277fe6060f1SDimitry Andric         for (auto *Sym : Syms)
278fe6060f1SDimitry Andric           OS << "      " << *Sym << "\n";
279fe6060f1SDimitry Andric       } else
280fe6060f1SDimitry Andric         OS << "    no symbols\n";
281fe6060f1SDimitry Andric 
282fe6060f1SDimitry Andric       if (!B->edges_empty()) {
283fe6060f1SDimitry Andric         OS << "    edges:\n";
284fe6060f1SDimitry Andric         std::vector<Edge> SortedEdges;
285fe6060f1SDimitry Andric         llvm::copy(B->edges(), std::back_inserter(SortedEdges));
286fe6060f1SDimitry Andric         llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
287fe6060f1SDimitry Andric           return LHS.getOffset() < RHS.getOffset();
288fe6060f1SDimitry Andric         });
289fe6060f1SDimitry Andric         for (auto &E : SortedEdges) {
29004eeddc0SDimitry Andric           OS << "      " << B->getFixupAddress(E) << " (block + "
29104eeddc0SDimitry Andric              << formatv("{0:x8}", E.getOffset()) << "), addend = ";
292fe6060f1SDimitry Andric           if (E.getAddend() >= 0)
293fe6060f1SDimitry Andric             OS << formatv("+{0:x8}", E.getAddend());
294fe6060f1SDimitry Andric           else
295fe6060f1SDimitry Andric             OS << formatv("-{0:x8}", -E.getAddend());
296fe6060f1SDimitry Andric           OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
297fe6060f1SDimitry Andric           if (E.getTarget().hasName())
298fe6060f1SDimitry Andric             OS << E.getTarget().getName();
299fe6060f1SDimitry Andric           else
300fe6060f1SDimitry Andric             OS << "addressable@"
301fe6060f1SDimitry Andric                << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
302fe6060f1SDimitry Andric                << formatv("{0:x8}", E.getTarget().getOffset());
3030b57cec5SDimitry Andric           OS << "\n";
3040b57cec5SDimitry Andric         }
305fe6060f1SDimitry Andric       } else
306fe6060f1SDimitry Andric         OS << "    no edges\n";
307fe6060f1SDimitry Andric       OS << "\n";
3080b57cec5SDimitry Andric     }
3098bcb0991SDimitry Andric   }
3100b57cec5SDimitry Andric 
3118bcb0991SDimitry Andric   OS << "Absolute symbols:\n";
312fe6060f1SDimitry Andric   if (!llvm::empty(absolute_symbols())) {
3138bcb0991SDimitry Andric     for (auto *Sym : absolute_symbols())
31404eeddc0SDimitry Andric       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
315fe6060f1SDimitry Andric   } else
316fe6060f1SDimitry Andric     OS << "  none\n";
3170b57cec5SDimitry Andric 
318fe6060f1SDimitry Andric   OS << "\nExternal symbols:\n";
319fe6060f1SDimitry Andric   if (!llvm::empty(external_symbols())) {
3208bcb0991SDimitry Andric     for (auto *Sym : external_symbols())
32104eeddc0SDimitry Andric       OS << "  " << Sym->getAddress() << ": " << *Sym << "\n";
322fe6060f1SDimitry Andric   } else
323fe6060f1SDimitry Andric     OS << "  none\n";
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
326480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
327480093f4SDimitry Andric   switch (LF) {
328480093f4SDimitry Andric   case SymbolLookupFlags::RequiredSymbol:
329480093f4SDimitry Andric     return OS << "RequiredSymbol";
330480093f4SDimitry Andric   case SymbolLookupFlags::WeaklyReferencedSymbol:
331480093f4SDimitry Andric     return OS << "WeaklyReferencedSymbol";
332480093f4SDimitry Andric   }
333480093f4SDimitry Andric   llvm_unreachable("Unrecognized lookup flags");
334480093f4SDimitry Andric }
335480093f4SDimitry Andric 
3368bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {}
3378bcb0991SDimitry Andric 
33881ad6265SDimitry Andric JITLinkContext::~JITLinkContext() = default;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
3410b57cec5SDimitry Andric   return true;
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric 
3448bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
3458bcb0991SDimitry Andric   return LinkGraphPassFunction();
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
348fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G,
3490b57cec5SDimitry Andric                                        PassConfiguration &Config) {
3500b57cec5SDimitry Andric   return Error::success();
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
3538bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) {
3548bcb0991SDimitry Andric   for (auto *Sym : G.defined_symbols())
3558bcb0991SDimitry Andric     Sym->setLive(true);
3560b57cec5SDimitry Andric   return Error::success();
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
359fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
360fe6060f1SDimitry Andric                                 const Edge &E) {
361fe6060f1SDimitry Andric   std::string ErrMsg;
362fe6060f1SDimitry Andric   {
363fe6060f1SDimitry Andric     raw_string_ostream ErrStream(ErrMsg);
364fe6060f1SDimitry Andric     Section &Sec = B.getSection();
365fe6060f1SDimitry Andric     ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
366fe6060f1SDimitry Andric               << ": relocation target ";
36704eeddc0SDimitry Andric     if (E.getTarget().hasName()) {
368fe6060f1SDimitry Andric       ErrStream << "\"" << E.getTarget().getName() << "\"";
36904eeddc0SDimitry Andric     } else
37004eeddc0SDimitry Andric       ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
37104eeddc0SDimitry Andric                 << formatv("{0:x}", E.getOffset());
37204eeddc0SDimitry Andric     ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
37304eeddc0SDimitry Andric               << " is out of range of " << G.getEdgeKindName(E.getKind())
374fe6060f1SDimitry Andric               << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
375fe6060f1SDimitry Andric 
376fe6060f1SDimitry Andric     Symbol *BestSymbolForBlock = nullptr;
377fe6060f1SDimitry Andric     for (auto *Sym : Sec.symbols())
378fe6060f1SDimitry Andric       if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
379fe6060f1SDimitry Andric           (!BestSymbolForBlock ||
380fe6060f1SDimitry Andric            Sym->getScope() < BestSymbolForBlock->getScope() ||
381fe6060f1SDimitry Andric            Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
382fe6060f1SDimitry Andric         BestSymbolForBlock = Sym;
383fe6060f1SDimitry Andric 
384fe6060f1SDimitry Andric     if (BestSymbolForBlock)
385fe6060f1SDimitry Andric       ErrStream << BestSymbolForBlock->getName() << ", ";
386fe6060f1SDimitry Andric     else
387fe6060f1SDimitry Andric       ErrStream << "<anonymous block> @ ";
388fe6060f1SDimitry Andric 
389fe6060f1SDimitry Andric     ErrStream << formatv("{0:x}", B.getAddress()) << " + "
390fe6060f1SDimitry Andric               << formatv("{0:x}", E.getOffset()) << ")";
391fe6060f1SDimitry Andric   }
392fe6060f1SDimitry Andric   return make_error<JITLinkError>(std::move(ErrMsg));
393fe6060f1SDimitry Andric }
394fe6060f1SDimitry Andric 
39581ad6265SDimitry Andric Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value, int N,
39681ad6265SDimitry Andric                          const Edge &E) {
39781ad6265SDimitry Andric   return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
39881ad6265SDimitry Andric                                   " improper alignment for relocation " +
39981ad6265SDimitry Andric                                   formatv("{0:d}", E.getKind()) + ": 0x" +
40081ad6265SDimitry Andric                                   llvm::utohexstr(Value) +
40181ad6265SDimitry Andric                                   " is not aligned to " + Twine(N) + " bytes");
40281ad6265SDimitry Andric }
40381ad6265SDimitry Andric 
404e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>>
405e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
406e8d8bef9SDimitry Andric   auto Magic = identify_magic(ObjectBuffer.getBuffer());
4070b57cec5SDimitry Andric   switch (Magic) {
4080b57cec5SDimitry Andric   case file_magic::macho_object:
409fe6060f1SDimitry Andric     return createLinkGraphFromMachOObject(ObjectBuffer);
4105ffd83dbSDimitry Andric   case file_magic::elf_relocatable:
411fe6060f1SDimitry Andric     return createLinkGraphFromELFObject(ObjectBuffer);
412*753f127fSDimitry Andric   case file_magic::coff_object:
413*753f127fSDimitry Andric     return createLinkGraphFromCOFFObject(ObjectBuffer);
4140b57cec5SDimitry Andric   default:
415e8d8bef9SDimitry Andric     return make_error<JITLinkError>("Unsupported file format");
416e8d8bef9SDimitry Andric   };
417e8d8bef9SDimitry Andric }
418e8d8bef9SDimitry Andric 
419e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
420e8d8bef9SDimitry Andric   switch (G->getTargetTriple().getObjectFormat()) {
421e8d8bef9SDimitry Andric   case Triple::MachO:
422e8d8bef9SDimitry Andric     return link_MachO(std::move(G), std::move(Ctx));
423e8d8bef9SDimitry Andric   case Triple::ELF:
424e8d8bef9SDimitry Andric     return link_ELF(std::move(G), std::move(Ctx));
425*753f127fSDimitry Andric   case Triple::COFF:
426*753f127fSDimitry Andric     return link_COFF(std::move(G), std::move(Ctx));
427e8d8bef9SDimitry Andric   default:
428e8d8bef9SDimitry Andric     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
4290b57cec5SDimitry Andric   };
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric } // end namespace jitlink
4330b57cec5SDimitry Andric } // end namespace llvm
434