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" 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) { 93*04eeddc0SDimitry Andric return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize()) 94*04eeddc0SDimitry Andric << ": " 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) { 103*04eeddc0SDimitry Andric OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable") 104*04eeddc0SDimitry Andric << " + " << formatv("{0:x8}", Sym.getOffset()) 105fe6060f1SDimitry Andric << "): size: " << formatv("{0:x8}", Sym.getSize()) 106fe6060f1SDimitry Andric << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage())) 107fe6060f1SDimitry Andric << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", " 108fe6060f1SDimitry Andric << (Sym.isLive() ? "live" : "dead") << " - " 109fe6060f1SDimitry Andric << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>"); 1100b57cec5SDimitry Andric return OS; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1138bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E, 1140b57cec5SDimitry Andric StringRef EdgeKindName) { 115*04eeddc0SDimitry Andric OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress() 116*04eeddc0SDimitry Andric << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName 117*04eeddc0SDimitry Andric << " -> "; 118e8d8bef9SDimitry Andric 119e8d8bef9SDimitry Andric auto &TargetSym = E.getTarget(); 120e8d8bef9SDimitry Andric if (TargetSym.hasName()) 121e8d8bef9SDimitry Andric OS << TargetSym.getName(); 122e8d8bef9SDimitry Andric else { 123e8d8bef9SDimitry Andric auto &TargetBlock = TargetSym.getBlock(); 124e8d8bef9SDimitry Andric auto &TargetSec = TargetBlock.getSection(); 125*04eeddc0SDimitry Andric orc::ExecutorAddr SecAddress(~uint64_t(0)); 126e8d8bef9SDimitry Andric for (auto *B : TargetSec.blocks()) 127e8d8bef9SDimitry Andric if (B->getAddress() < SecAddress) 128e8d8bef9SDimitry Andric SecAddress = B->getAddress(); 129e8d8bef9SDimitry Andric 130*04eeddc0SDimitry Andric orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress; 131*04eeddc0SDimitry Andric OS << TargetSym.getAddress() << " (section " << TargetSec.getName(); 132e8d8bef9SDimitry Andric if (SecDelta) 133e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", SecDelta); 134*04eeddc0SDimitry Andric OS << " / block " << TargetBlock.getAddress(); 135e8d8bef9SDimitry Andric if (TargetSym.getOffset()) 136e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", TargetSym.getOffset()); 137e8d8bef9SDimitry Andric OS << ")"; 138e8d8bef9SDimitry Andric } 139e8d8bef9SDimitry Andric 140e8d8bef9SDimitry Andric if (E.getAddend() != 0) 141e8d8bef9SDimitry Andric OS << " + " << E.getAddend(); 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric Section::~Section() { 1458bcb0991SDimitry Andric for (auto *Sym : Symbols) 1468bcb0991SDimitry Andric Sym->~Symbol(); 1478bcb0991SDimitry Andric for (auto *B : Blocks) 1488bcb0991SDimitry Andric B->~Block(); 1498bcb0991SDimitry Andric } 1508bcb0991SDimitry Andric 151480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex, 152480093f4SDimitry Andric SplitBlockCache *Cache) { 153480093f4SDimitry Andric 154480093f4SDimitry Andric assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0"); 155480093f4SDimitry Andric 156480093f4SDimitry Andric // If the split point covers all of B then just return B. 157480093f4SDimitry Andric if (SplitIndex == B.getSize()) 158480093f4SDimitry Andric return B; 159480093f4SDimitry Andric 160480093f4SDimitry Andric assert(SplitIndex < B.getSize() && "SplitIndex out of range"); 161480093f4SDimitry Andric 162480093f4SDimitry Andric // Create the new block covering [ 0, SplitIndex ). 163480093f4SDimitry Andric auto &NewBlock = 164480093f4SDimitry Andric B.isZeroFill() 165480093f4SDimitry Andric ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(), 166480093f4SDimitry Andric B.getAlignment(), B.getAlignmentOffset()) 167480093f4SDimitry Andric : createContentBlock( 168fe6060f1SDimitry Andric B.getSection(), B.getContent().slice(0, SplitIndex), 169480093f4SDimitry Andric B.getAddress(), B.getAlignment(), B.getAlignmentOffset()); 170480093f4SDimitry Andric 171480093f4SDimitry Andric // Modify B to cover [ SplitIndex, B.size() ). 172480093f4SDimitry Andric B.setAddress(B.getAddress() + SplitIndex); 173fe6060f1SDimitry Andric B.setContent(B.getContent().slice(SplitIndex)); 174480093f4SDimitry Andric B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) % 175480093f4SDimitry Andric B.getAlignment()); 176480093f4SDimitry Andric 177480093f4SDimitry Andric // Handle edge transfer/update. 178480093f4SDimitry Andric { 179480093f4SDimitry Andric // Copy edges to NewBlock (recording their iterators so that we can remove 180480093f4SDimitry Andric // them from B), and update of Edges remaining on B. 181480093f4SDimitry Andric std::vector<Block::edge_iterator> EdgesToRemove; 1825ffd83dbSDimitry Andric for (auto I = B.edges().begin(); I != B.edges().end();) { 183480093f4SDimitry Andric if (I->getOffset() < SplitIndex) { 184480093f4SDimitry Andric NewBlock.addEdge(*I); 1855ffd83dbSDimitry Andric I = B.removeEdge(I); 1865ffd83dbSDimitry Andric } else { 187480093f4SDimitry Andric I->setOffset(I->getOffset() - SplitIndex); 1885ffd83dbSDimitry Andric ++I; 189480093f4SDimitry Andric } 190480093f4SDimitry Andric } 191480093f4SDimitry Andric } 192480093f4SDimitry Andric 193480093f4SDimitry Andric // Handle symbol transfer/update. 194480093f4SDimitry Andric { 195480093f4SDimitry Andric // Initialize the symbols cache if necessary. 196480093f4SDimitry Andric SplitBlockCache LocalBlockSymbolsCache; 197480093f4SDimitry Andric if (!Cache) 198480093f4SDimitry Andric Cache = &LocalBlockSymbolsCache; 199480093f4SDimitry Andric if (*Cache == None) { 200480093f4SDimitry Andric *Cache = SplitBlockCache::value_type(); 201480093f4SDimitry Andric for (auto *Sym : B.getSection().symbols()) 202480093f4SDimitry Andric if (&Sym->getBlock() == &B) 203480093f4SDimitry Andric (*Cache)->push_back(Sym); 204480093f4SDimitry Andric 205480093f4SDimitry Andric llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) { 206480093f4SDimitry Andric return LHS->getOffset() > RHS->getOffset(); 207480093f4SDimitry Andric }); 208480093f4SDimitry Andric } 209480093f4SDimitry Andric auto &BlockSymbols = **Cache; 210480093f4SDimitry Andric 211480093f4SDimitry Andric // Transfer all symbols with offset less than SplitIndex to NewBlock. 212480093f4SDimitry Andric while (!BlockSymbols.empty() && 213480093f4SDimitry Andric BlockSymbols.back()->getOffset() < SplitIndex) { 214349cc55cSDimitry Andric auto *Sym = BlockSymbols.back(); 215349cc55cSDimitry Andric // If the symbol extends beyond the split, update the size to be within 216349cc55cSDimitry Andric // the new block. 217349cc55cSDimitry Andric if (Sym->getOffset() + Sym->getSize() > SplitIndex) 218349cc55cSDimitry Andric Sym->setSize(SplitIndex - Sym->getOffset()); 219349cc55cSDimitry Andric Sym->setBlock(NewBlock); 220480093f4SDimitry Andric BlockSymbols.pop_back(); 221480093f4SDimitry Andric } 222480093f4SDimitry Andric 223480093f4SDimitry Andric // Update offsets for all remaining symbols in B. 224480093f4SDimitry Andric for (auto *Sym : BlockSymbols) 225480093f4SDimitry Andric Sym->setOffset(Sym->getOffset() - SplitIndex); 226480093f4SDimitry Andric } 227480093f4SDimitry Andric 228480093f4SDimitry Andric return NewBlock; 229480093f4SDimitry Andric } 230480093f4SDimitry Andric 231fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) { 232fe6060f1SDimitry Andric DenseMap<Block *, std::vector<Symbol *>> BlockSymbols; 2330b57cec5SDimitry Andric 234fe6060f1SDimitry Andric // Map from blocks to the symbols pointing at them. 235fe6060f1SDimitry Andric for (auto *Sym : defined_symbols()) 236fe6060f1SDimitry Andric BlockSymbols[&Sym->getBlock()].push_back(Sym); 2370b57cec5SDimitry Andric 238fe6060f1SDimitry Andric // For each block, sort its symbols by something approximating 239fe6060f1SDimitry Andric // relevance. 240fe6060f1SDimitry Andric for (auto &KV : BlockSymbols) 241fe6060f1SDimitry Andric llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) { 242fe6060f1SDimitry Andric if (LHS->getOffset() != RHS->getOffset()) 243fe6060f1SDimitry Andric return LHS->getOffset() < RHS->getOffset(); 244fe6060f1SDimitry Andric if (LHS->getLinkage() != RHS->getLinkage()) 245fe6060f1SDimitry Andric return LHS->getLinkage() < RHS->getLinkage(); 246fe6060f1SDimitry Andric if (LHS->getScope() != RHS->getScope()) 247fe6060f1SDimitry Andric return LHS->getScope() < RHS->getScope(); 248fe6060f1SDimitry Andric if (LHS->hasName()) { 249fe6060f1SDimitry Andric if (!RHS->hasName()) 250fe6060f1SDimitry Andric return true; 251fe6060f1SDimitry Andric return LHS->getName() < RHS->getName(); 2520b57cec5SDimitry Andric } 253fe6060f1SDimitry Andric return false; 254fe6060f1SDimitry Andric }); 255fe6060f1SDimitry Andric 256fe6060f1SDimitry Andric for (auto &Sec : sections()) { 257fe6060f1SDimitry Andric OS << "section " << Sec.getName() << ":\n\n"; 258fe6060f1SDimitry Andric 259fe6060f1SDimitry Andric std::vector<Block *> SortedBlocks; 260fe6060f1SDimitry Andric llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks)); 261fe6060f1SDimitry Andric llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) { 262fe6060f1SDimitry Andric return LHS->getAddress() < RHS->getAddress(); 263fe6060f1SDimitry Andric }); 264fe6060f1SDimitry Andric 265fe6060f1SDimitry Andric for (auto *B : SortedBlocks) { 266*04eeddc0SDimitry Andric OS << " block " << B->getAddress() 267fe6060f1SDimitry Andric << " size = " << formatv("{0:x8}", B->getSize()) 268fe6060f1SDimitry Andric << ", align = " << B->getAlignment() 269fe6060f1SDimitry Andric << ", alignment-offset = " << B->getAlignmentOffset(); 270fe6060f1SDimitry Andric if (B->isZeroFill()) 271fe6060f1SDimitry Andric OS << ", zero-fill"; 272fe6060f1SDimitry Andric OS << "\n"; 273fe6060f1SDimitry Andric 274fe6060f1SDimitry Andric auto BlockSymsI = BlockSymbols.find(B); 275fe6060f1SDimitry Andric if (BlockSymsI != BlockSymbols.end()) { 276fe6060f1SDimitry Andric OS << " symbols:\n"; 277fe6060f1SDimitry Andric auto &Syms = BlockSymsI->second; 278fe6060f1SDimitry Andric for (auto *Sym : Syms) 279fe6060f1SDimitry Andric OS << " " << *Sym << "\n"; 280fe6060f1SDimitry Andric } else 281fe6060f1SDimitry Andric OS << " no symbols\n"; 282fe6060f1SDimitry Andric 283fe6060f1SDimitry Andric if (!B->edges_empty()) { 284fe6060f1SDimitry Andric OS << " edges:\n"; 285fe6060f1SDimitry Andric std::vector<Edge> SortedEdges; 286fe6060f1SDimitry Andric llvm::copy(B->edges(), std::back_inserter(SortedEdges)); 287fe6060f1SDimitry Andric llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) { 288fe6060f1SDimitry Andric return LHS.getOffset() < RHS.getOffset(); 289fe6060f1SDimitry Andric }); 290fe6060f1SDimitry Andric for (auto &E : SortedEdges) { 291*04eeddc0SDimitry Andric OS << " " << B->getFixupAddress(E) << " (block + " 292*04eeddc0SDimitry Andric << formatv("{0:x8}", E.getOffset()) << "), addend = "; 293fe6060f1SDimitry Andric if (E.getAddend() >= 0) 294fe6060f1SDimitry Andric OS << formatv("+{0:x8}", E.getAddend()); 295fe6060f1SDimitry Andric else 296fe6060f1SDimitry Andric OS << formatv("-{0:x8}", -E.getAddend()); 297fe6060f1SDimitry Andric OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = "; 298fe6060f1SDimitry Andric if (E.getTarget().hasName()) 299fe6060f1SDimitry Andric OS << E.getTarget().getName(); 300fe6060f1SDimitry Andric else 301fe6060f1SDimitry Andric OS << "addressable@" 302fe6060f1SDimitry Andric << formatv("{0:x16}", E.getTarget().getAddress()) << "+" 303fe6060f1SDimitry Andric << formatv("{0:x8}", E.getTarget().getOffset()); 3040b57cec5SDimitry Andric OS << "\n"; 3050b57cec5SDimitry Andric } 306fe6060f1SDimitry Andric } else 307fe6060f1SDimitry Andric OS << " no edges\n"; 308fe6060f1SDimitry Andric OS << "\n"; 3090b57cec5SDimitry Andric } 3108bcb0991SDimitry Andric } 3110b57cec5SDimitry Andric 3128bcb0991SDimitry Andric OS << "Absolute symbols:\n"; 313fe6060f1SDimitry Andric if (!llvm::empty(absolute_symbols())) { 3148bcb0991SDimitry Andric for (auto *Sym : absolute_symbols()) 315*04eeddc0SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym << "\n"; 316fe6060f1SDimitry Andric } else 317fe6060f1SDimitry Andric OS << " none\n"; 3180b57cec5SDimitry Andric 319fe6060f1SDimitry Andric OS << "\nExternal symbols:\n"; 320fe6060f1SDimitry Andric if (!llvm::empty(external_symbols())) { 3218bcb0991SDimitry Andric for (auto *Sym : external_symbols()) 322*04eeddc0SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym << "\n"; 323fe6060f1SDimitry Andric } else 324fe6060f1SDimitry Andric OS << " none\n"; 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 327480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) { 328480093f4SDimitry Andric switch (LF) { 329480093f4SDimitry Andric case SymbolLookupFlags::RequiredSymbol: 330480093f4SDimitry Andric return OS << "RequiredSymbol"; 331480093f4SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol: 332480093f4SDimitry Andric return OS << "WeaklyReferencedSymbol"; 333480093f4SDimitry Andric } 334480093f4SDimitry Andric llvm_unreachable("Unrecognized lookup flags"); 335480093f4SDimitry Andric } 336480093f4SDimitry Andric 3378bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {} 3388bcb0991SDimitry Andric 3390b57cec5SDimitry Andric JITLinkContext::~JITLinkContext() {} 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const { 3420b57cec5SDimitry Andric return true; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric 3458bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const { 3468bcb0991SDimitry Andric return LinkGraphPassFunction(); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 349fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G, 3500b57cec5SDimitry Andric PassConfiguration &Config) { 3510b57cec5SDimitry Andric return Error::success(); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric 3548bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) { 3558bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) 3568bcb0991SDimitry Andric Sym->setLive(true); 3570b57cec5SDimitry Andric return Error::success(); 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 360fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B, 361fe6060f1SDimitry Andric const Edge &E) { 362fe6060f1SDimitry Andric std::string ErrMsg; 363fe6060f1SDimitry Andric { 364fe6060f1SDimitry Andric raw_string_ostream ErrStream(ErrMsg); 365fe6060f1SDimitry Andric Section &Sec = B.getSection(); 366fe6060f1SDimitry Andric ErrStream << "In graph " << G.getName() << ", section " << Sec.getName() 367fe6060f1SDimitry Andric << ": relocation target "; 368*04eeddc0SDimitry Andric if (E.getTarget().hasName()) { 369fe6060f1SDimitry Andric ErrStream << "\"" << E.getTarget().getName() << "\""; 370*04eeddc0SDimitry Andric } else 371*04eeddc0SDimitry Andric ErrStream << E.getTarget().getBlock().getSection().getName() << " + " 372*04eeddc0SDimitry Andric << formatv("{0:x}", E.getOffset()); 373*04eeddc0SDimitry Andric ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress()) 374*04eeddc0SDimitry Andric << " is out of range of " << G.getEdgeKindName(E.getKind()) 375fe6060f1SDimitry Andric << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " ("; 376fe6060f1SDimitry Andric 377fe6060f1SDimitry Andric Symbol *BestSymbolForBlock = nullptr; 378fe6060f1SDimitry Andric for (auto *Sym : Sec.symbols()) 379fe6060f1SDimitry Andric if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 && 380fe6060f1SDimitry Andric (!BestSymbolForBlock || 381fe6060f1SDimitry Andric Sym->getScope() < BestSymbolForBlock->getScope() || 382fe6060f1SDimitry Andric Sym->getLinkage() < BestSymbolForBlock->getLinkage())) 383fe6060f1SDimitry Andric BestSymbolForBlock = Sym; 384fe6060f1SDimitry Andric 385fe6060f1SDimitry Andric if (BestSymbolForBlock) 386fe6060f1SDimitry Andric ErrStream << BestSymbolForBlock->getName() << ", "; 387fe6060f1SDimitry Andric else 388fe6060f1SDimitry Andric ErrStream << "<anonymous block> @ "; 389fe6060f1SDimitry Andric 390fe6060f1SDimitry Andric ErrStream << formatv("{0:x}", B.getAddress()) << " + " 391fe6060f1SDimitry Andric << formatv("{0:x}", E.getOffset()) << ")"; 392fe6060f1SDimitry Andric } 393fe6060f1SDimitry Andric return make_error<JITLinkError>(std::move(ErrMsg)); 394fe6060f1SDimitry Andric } 395fe6060f1SDimitry Andric 396e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>> 397e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) { 398e8d8bef9SDimitry Andric auto Magic = identify_magic(ObjectBuffer.getBuffer()); 3990b57cec5SDimitry Andric switch (Magic) { 4000b57cec5SDimitry Andric case file_magic::macho_object: 401fe6060f1SDimitry Andric return createLinkGraphFromMachOObject(ObjectBuffer); 4025ffd83dbSDimitry Andric case file_magic::elf_relocatable: 403fe6060f1SDimitry Andric return createLinkGraphFromELFObject(ObjectBuffer); 4040b57cec5SDimitry Andric default: 405e8d8bef9SDimitry Andric return make_error<JITLinkError>("Unsupported file format"); 406e8d8bef9SDimitry Andric }; 407e8d8bef9SDimitry Andric } 408e8d8bef9SDimitry Andric 409e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) { 410e8d8bef9SDimitry Andric switch (G->getTargetTriple().getObjectFormat()) { 411e8d8bef9SDimitry Andric case Triple::MachO: 412e8d8bef9SDimitry Andric return link_MachO(std::move(G), std::move(Ctx)); 413e8d8bef9SDimitry Andric case Triple::ELF: 414e8d8bef9SDimitry Andric return link_ELF(std::move(G), std::move(Ctx)); 415e8d8bef9SDimitry Andric default: 416e8d8bef9SDimitry Andric Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format")); 4170b57cec5SDimitry Andric }; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric } // end namespace jitlink 4210b57cec5SDimitry Andric } // end namespace llvm 422