10b57cec5SDimitry Andric //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // The LLVM Compiler Infrastructure 40b57cec5SDimitry Andric // 50b57cec5SDimitry Andric // This file is distributed under the University of Illinois Open Source 60b57cec5SDimitry Andric // License. See LICENSE.TXT for details. 70b57cec5SDimitry Andric // 80b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/JITLink.h" 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "llvm/BinaryFormat/Magic.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/ManagedStatic.h" 170b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 180b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace llvm; 210b57cec5SDimitry Andric using namespace llvm::object; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric #define DEBUG_TYPE "jitlink" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric namespace { 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric enum JITLinkErrorCode { GenericJITLinkError = 1 }; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 300b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 310b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 320b57cec5SDimitry Andric class JITLinkerErrorCategory : public std::error_category { 330b57cec5SDimitry Andric public: 340b57cec5SDimitry Andric const char *name() const noexcept override { return "runtimedyld"; } 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric std::string message(int Condition) const override { 370b57cec5SDimitry Andric switch (static_cast<JITLinkErrorCode>(Condition)) { 380b57cec5SDimitry Andric case GenericJITLinkError: 390b57cec5SDimitry Andric return "Generic JITLink error"; 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric llvm_unreachable("Unrecognized JITLinkErrorCode"); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric }; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric } // namespace 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric namespace llvm { 500b57cec5SDimitry Andric namespace jitlink { 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric char JITLinkError::ID = 0; 530b57cec5SDimitry Andric 54*fe6060f1SDimitry Andric void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric std::error_code JITLinkError::convertToErrorCode() const { 570b57cec5SDimitry Andric return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 608bcb0991SDimitry Andric const char *getGenericEdgeKindName(Edge::Kind K) { 610b57cec5SDimitry Andric switch (K) { 620b57cec5SDimitry Andric case Edge::Invalid: 630b57cec5SDimitry Andric return "INVALID RELOCATION"; 640b57cec5SDimitry Andric case Edge::KeepAlive: 650b57cec5SDimitry Andric return "Keep-Alive"; 660b57cec5SDimitry Andric default: 67e8d8bef9SDimitry Andric return "<Unrecognized edge kind>"; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric 718bcb0991SDimitry Andric const char *getLinkageName(Linkage L) { 728bcb0991SDimitry Andric switch (L) { 738bcb0991SDimitry Andric case Linkage::Strong: 748bcb0991SDimitry Andric return "strong"; 758bcb0991SDimitry Andric case Linkage::Weak: 768bcb0991SDimitry Andric return "weak"; 778bcb0991SDimitry Andric } 788bcb0991SDimitry Andric llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum"); 798bcb0991SDimitry Andric } 808bcb0991SDimitry Andric 818bcb0991SDimitry Andric const char *getScopeName(Scope S) { 828bcb0991SDimitry Andric switch (S) { 838bcb0991SDimitry Andric case Scope::Default: 848bcb0991SDimitry Andric return "default"; 858bcb0991SDimitry Andric case Scope::Hidden: 868bcb0991SDimitry Andric return "hidden"; 878bcb0991SDimitry Andric case Scope::Local: 888bcb0991SDimitry Andric return "local"; 898bcb0991SDimitry Andric } 908bcb0991SDimitry Andric llvm_unreachable("Unrecognized llvm.jitlink.Scope enum"); 918bcb0991SDimitry Andric } 928bcb0991SDimitry Andric 938bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Block &B) { 948bcb0991SDimitry Andric return OS << formatv("{0:x16}", B.getAddress()) << " -- " 95*fe6060f1SDimitry Andric << formatv("{0:x8}", B.getAddress() + B.getSize()) << ": " 96*fe6060f1SDimitry Andric << "size = " << formatv("{0:x8}", B.getSize()) << ", " 978bcb0991SDimitry Andric << (B.isZeroFill() ? "zero-fill" : "content") 988bcb0991SDimitry Andric << ", align = " << B.getAlignment() 998bcb0991SDimitry Andric << ", align-ofs = " << B.getAlignmentOffset() 1008bcb0991SDimitry Andric << ", section = " << B.getSection().getName(); 1018bcb0991SDimitry Andric } 1028bcb0991SDimitry Andric 1038bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) { 104*fe6060f1SDimitry Andric OS << formatv("{0:x16}", Sym.getAddress()) << " (" 105*fe6060f1SDimitry Andric << (Sym.isDefined() ? "block" : "addressable") << " + " 106*fe6060f1SDimitry Andric << formatv("{0:x8}", Sym.getOffset()) 107*fe6060f1SDimitry Andric << "): size: " << formatv("{0:x8}", Sym.getSize()) 108*fe6060f1SDimitry Andric << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage())) 109*fe6060f1SDimitry Andric << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", " 110*fe6060f1SDimitry Andric << (Sym.isLive() ? "live" : "dead") << " - " 111*fe6060f1SDimitry Andric << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>"); 1120b57cec5SDimitry Andric return OS; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 1158bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E, 1160b57cec5SDimitry Andric StringRef EdgeKindName) { 1178bcb0991SDimitry Andric OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": " 118e8d8bef9SDimitry Andric << formatv("{0:x16}", B.getAddress()) << " + " 119e8d8bef9SDimitry Andric << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName << " -> "; 120e8d8bef9SDimitry Andric 121e8d8bef9SDimitry Andric auto &TargetSym = E.getTarget(); 122e8d8bef9SDimitry Andric if (TargetSym.hasName()) 123e8d8bef9SDimitry Andric OS << TargetSym.getName(); 124e8d8bef9SDimitry Andric else { 125e8d8bef9SDimitry Andric auto &TargetBlock = TargetSym.getBlock(); 126e8d8bef9SDimitry Andric auto &TargetSec = TargetBlock.getSection(); 127e8d8bef9SDimitry Andric JITTargetAddress SecAddress = ~JITTargetAddress(0); 128e8d8bef9SDimitry Andric for (auto *B : TargetSec.blocks()) 129e8d8bef9SDimitry Andric if (B->getAddress() < SecAddress) 130e8d8bef9SDimitry Andric SecAddress = B->getAddress(); 131e8d8bef9SDimitry Andric 132e8d8bef9SDimitry Andric JITTargetAddress SecDelta = TargetSym.getAddress() - SecAddress; 133e8d8bef9SDimitry Andric OS << formatv("{0:x16}", TargetSym.getAddress()) << " (section " 134e8d8bef9SDimitry Andric << TargetSec.getName(); 135e8d8bef9SDimitry Andric if (SecDelta) 136e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", SecDelta); 137e8d8bef9SDimitry Andric OS << " / block " << formatv("{0:x16}", TargetBlock.getAddress()); 138e8d8bef9SDimitry Andric if (TargetSym.getOffset()) 139e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", TargetSym.getOffset()); 140e8d8bef9SDimitry Andric OS << ")"; 141e8d8bef9SDimitry Andric } 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric if (E.getAddend() != 0) 144e8d8bef9SDimitry Andric OS << " + " << E.getAddend(); 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric Section::~Section() { 1488bcb0991SDimitry Andric for (auto *Sym : Symbols) 1498bcb0991SDimitry Andric Sym->~Symbol(); 1508bcb0991SDimitry Andric for (auto *B : Blocks) 1518bcb0991SDimitry Andric B->~Block(); 1528bcb0991SDimitry Andric } 1538bcb0991SDimitry Andric 154480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex, 155480093f4SDimitry Andric SplitBlockCache *Cache) { 156480093f4SDimitry Andric 157480093f4SDimitry Andric assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0"); 158480093f4SDimitry Andric 159480093f4SDimitry Andric // If the split point covers all of B then just return B. 160480093f4SDimitry Andric if (SplitIndex == B.getSize()) 161480093f4SDimitry Andric return B; 162480093f4SDimitry Andric 163480093f4SDimitry Andric assert(SplitIndex < B.getSize() && "SplitIndex out of range"); 164480093f4SDimitry Andric 165480093f4SDimitry Andric // Create the new block covering [ 0, SplitIndex ). 166480093f4SDimitry Andric auto &NewBlock = 167480093f4SDimitry Andric B.isZeroFill() 168480093f4SDimitry Andric ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(), 169480093f4SDimitry Andric B.getAlignment(), B.getAlignmentOffset()) 170480093f4SDimitry Andric : createContentBlock( 171*fe6060f1SDimitry Andric B.getSection(), B.getContent().slice(0, SplitIndex), 172480093f4SDimitry Andric B.getAddress(), B.getAlignment(), B.getAlignmentOffset()); 173480093f4SDimitry Andric 174480093f4SDimitry Andric // Modify B to cover [ SplitIndex, B.size() ). 175480093f4SDimitry Andric B.setAddress(B.getAddress() + SplitIndex); 176*fe6060f1SDimitry Andric B.setContent(B.getContent().slice(SplitIndex)); 177480093f4SDimitry Andric B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) % 178480093f4SDimitry Andric B.getAlignment()); 179480093f4SDimitry Andric 180480093f4SDimitry Andric // Handle edge transfer/update. 181480093f4SDimitry Andric { 182480093f4SDimitry Andric // Copy edges to NewBlock (recording their iterators so that we can remove 183480093f4SDimitry Andric // them from B), and update of Edges remaining on B. 184480093f4SDimitry Andric std::vector<Block::edge_iterator> EdgesToRemove; 1855ffd83dbSDimitry Andric for (auto I = B.edges().begin(); I != B.edges().end();) { 186480093f4SDimitry Andric if (I->getOffset() < SplitIndex) { 187480093f4SDimitry Andric NewBlock.addEdge(*I); 1885ffd83dbSDimitry Andric I = B.removeEdge(I); 1895ffd83dbSDimitry Andric } else { 190480093f4SDimitry Andric I->setOffset(I->getOffset() - SplitIndex); 1915ffd83dbSDimitry Andric ++I; 192480093f4SDimitry Andric } 193480093f4SDimitry Andric } 194480093f4SDimitry Andric } 195480093f4SDimitry Andric 196480093f4SDimitry Andric // Handle symbol transfer/update. 197480093f4SDimitry Andric { 198480093f4SDimitry Andric // Initialize the symbols cache if necessary. 199480093f4SDimitry Andric SplitBlockCache LocalBlockSymbolsCache; 200480093f4SDimitry Andric if (!Cache) 201480093f4SDimitry Andric Cache = &LocalBlockSymbolsCache; 202480093f4SDimitry Andric if (*Cache == None) { 203480093f4SDimitry Andric *Cache = SplitBlockCache::value_type(); 204480093f4SDimitry Andric for (auto *Sym : B.getSection().symbols()) 205480093f4SDimitry Andric if (&Sym->getBlock() == &B) 206480093f4SDimitry Andric (*Cache)->push_back(Sym); 207480093f4SDimitry Andric 208480093f4SDimitry Andric llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) { 209480093f4SDimitry Andric return LHS->getOffset() > RHS->getOffset(); 210480093f4SDimitry Andric }); 211480093f4SDimitry Andric } 212480093f4SDimitry Andric auto &BlockSymbols = **Cache; 213480093f4SDimitry Andric 214480093f4SDimitry Andric // Transfer all symbols with offset less than SplitIndex to NewBlock. 215480093f4SDimitry Andric while (!BlockSymbols.empty() && 216480093f4SDimitry Andric BlockSymbols.back()->getOffset() < SplitIndex) { 217480093f4SDimitry Andric BlockSymbols.back()->setBlock(NewBlock); 218480093f4SDimitry Andric BlockSymbols.pop_back(); 219480093f4SDimitry Andric } 220480093f4SDimitry Andric 221480093f4SDimitry Andric // Update offsets for all remaining symbols in B. 222480093f4SDimitry Andric for (auto *Sym : BlockSymbols) 223480093f4SDimitry Andric Sym->setOffset(Sym->getOffset() - SplitIndex); 224480093f4SDimitry Andric } 225480093f4SDimitry Andric 226480093f4SDimitry Andric return NewBlock; 227480093f4SDimitry Andric } 228480093f4SDimitry Andric 229*fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) { 230*fe6060f1SDimitry Andric DenseMap<Block *, std::vector<Symbol *>> BlockSymbols; 2310b57cec5SDimitry Andric 232*fe6060f1SDimitry Andric // Map from blocks to the symbols pointing at them. 233*fe6060f1SDimitry Andric for (auto *Sym : defined_symbols()) 234*fe6060f1SDimitry Andric BlockSymbols[&Sym->getBlock()].push_back(Sym); 2350b57cec5SDimitry Andric 236*fe6060f1SDimitry Andric // For each block, sort its symbols by something approximating 237*fe6060f1SDimitry Andric // relevance. 238*fe6060f1SDimitry Andric for (auto &KV : BlockSymbols) 239*fe6060f1SDimitry Andric llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) { 240*fe6060f1SDimitry Andric if (LHS->getOffset() != RHS->getOffset()) 241*fe6060f1SDimitry Andric return LHS->getOffset() < RHS->getOffset(); 242*fe6060f1SDimitry Andric if (LHS->getLinkage() != RHS->getLinkage()) 243*fe6060f1SDimitry Andric return LHS->getLinkage() < RHS->getLinkage(); 244*fe6060f1SDimitry Andric if (LHS->getScope() != RHS->getScope()) 245*fe6060f1SDimitry Andric return LHS->getScope() < RHS->getScope(); 246*fe6060f1SDimitry Andric if (LHS->hasName()) { 247*fe6060f1SDimitry Andric if (!RHS->hasName()) 248*fe6060f1SDimitry Andric return true; 249*fe6060f1SDimitry Andric return LHS->getName() < RHS->getName(); 2500b57cec5SDimitry Andric } 251*fe6060f1SDimitry Andric return false; 252*fe6060f1SDimitry Andric }); 253*fe6060f1SDimitry Andric 254*fe6060f1SDimitry Andric for (auto &Sec : sections()) { 255*fe6060f1SDimitry Andric OS << "section " << Sec.getName() << ":\n\n"; 256*fe6060f1SDimitry Andric 257*fe6060f1SDimitry Andric std::vector<Block *> SortedBlocks; 258*fe6060f1SDimitry Andric llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks)); 259*fe6060f1SDimitry Andric llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) { 260*fe6060f1SDimitry Andric return LHS->getAddress() < RHS->getAddress(); 261*fe6060f1SDimitry Andric }); 262*fe6060f1SDimitry Andric 263*fe6060f1SDimitry Andric for (auto *B : SortedBlocks) { 264*fe6060f1SDimitry Andric OS << " block " << formatv("{0:x16}", B->getAddress()) 265*fe6060f1SDimitry Andric << " size = " << formatv("{0:x8}", B->getSize()) 266*fe6060f1SDimitry Andric << ", align = " << B->getAlignment() 267*fe6060f1SDimitry Andric << ", alignment-offset = " << B->getAlignmentOffset(); 268*fe6060f1SDimitry Andric if (B->isZeroFill()) 269*fe6060f1SDimitry Andric OS << ", zero-fill"; 270*fe6060f1SDimitry Andric OS << "\n"; 271*fe6060f1SDimitry Andric 272*fe6060f1SDimitry Andric auto BlockSymsI = BlockSymbols.find(B); 273*fe6060f1SDimitry Andric if (BlockSymsI != BlockSymbols.end()) { 274*fe6060f1SDimitry Andric OS << " symbols:\n"; 275*fe6060f1SDimitry Andric auto &Syms = BlockSymsI->second; 276*fe6060f1SDimitry Andric for (auto *Sym : Syms) 277*fe6060f1SDimitry Andric OS << " " << *Sym << "\n"; 278*fe6060f1SDimitry Andric } else 279*fe6060f1SDimitry Andric OS << " no symbols\n"; 280*fe6060f1SDimitry Andric 281*fe6060f1SDimitry Andric if (!B->edges_empty()) { 282*fe6060f1SDimitry Andric OS << " edges:\n"; 283*fe6060f1SDimitry Andric std::vector<Edge> SortedEdges; 284*fe6060f1SDimitry Andric llvm::copy(B->edges(), std::back_inserter(SortedEdges)); 285*fe6060f1SDimitry Andric llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) { 286*fe6060f1SDimitry Andric return LHS.getOffset() < RHS.getOffset(); 287*fe6060f1SDimitry Andric }); 288*fe6060f1SDimitry Andric for (auto &E : SortedEdges) { 289*fe6060f1SDimitry Andric OS << " " << formatv("{0:x16}", B->getFixupAddress(E)) 290*fe6060f1SDimitry Andric << " (block + " << formatv("{0:x8}", E.getOffset()) 291*fe6060f1SDimitry Andric << "), addend = "; 292*fe6060f1SDimitry Andric if (E.getAddend() >= 0) 293*fe6060f1SDimitry Andric OS << formatv("+{0:x8}", E.getAddend()); 294*fe6060f1SDimitry Andric else 295*fe6060f1SDimitry Andric OS << formatv("-{0:x8}", -E.getAddend()); 296*fe6060f1SDimitry Andric OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = "; 297*fe6060f1SDimitry Andric if (E.getTarget().hasName()) 298*fe6060f1SDimitry Andric OS << E.getTarget().getName(); 299*fe6060f1SDimitry Andric else 300*fe6060f1SDimitry Andric OS << "addressable@" 301*fe6060f1SDimitry Andric << formatv("{0:x16}", E.getTarget().getAddress()) << "+" 302*fe6060f1SDimitry Andric << formatv("{0:x8}", E.getTarget().getOffset()); 3030b57cec5SDimitry Andric OS << "\n"; 3040b57cec5SDimitry Andric } 305*fe6060f1SDimitry Andric } else 306*fe6060f1SDimitry Andric OS << " no edges\n"; 307*fe6060f1SDimitry Andric OS << "\n"; 3080b57cec5SDimitry Andric } 3098bcb0991SDimitry Andric } 3100b57cec5SDimitry Andric 3118bcb0991SDimitry Andric OS << "Absolute symbols:\n"; 312*fe6060f1SDimitry Andric if (!llvm::empty(absolute_symbols())) { 3138bcb0991SDimitry Andric for (auto *Sym : absolute_symbols()) 3148bcb0991SDimitry Andric OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym 3150b57cec5SDimitry Andric << "\n"; 316*fe6060f1SDimitry Andric } else 317*fe6060f1SDimitry Andric OS << " none\n"; 3180b57cec5SDimitry Andric 319*fe6060f1SDimitry Andric OS << "\nExternal symbols:\n"; 320*fe6060f1SDimitry Andric if (!llvm::empty(external_symbols())) { 3218bcb0991SDimitry Andric for (auto *Sym : external_symbols()) 3228bcb0991SDimitry Andric OS << " " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym 3230b57cec5SDimitry Andric << "\n"; 324*fe6060f1SDimitry Andric } else 325*fe6060f1SDimitry Andric OS << " none\n"; 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 328480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) { 329480093f4SDimitry Andric switch (LF) { 330480093f4SDimitry Andric case SymbolLookupFlags::RequiredSymbol: 331480093f4SDimitry Andric return OS << "RequiredSymbol"; 332480093f4SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol: 333480093f4SDimitry Andric return OS << "WeaklyReferencedSymbol"; 334480093f4SDimitry Andric } 335480093f4SDimitry Andric llvm_unreachable("Unrecognized lookup flags"); 336480093f4SDimitry Andric } 337480093f4SDimitry Andric 3388bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {} 3398bcb0991SDimitry Andric 3400b57cec5SDimitry Andric JITLinkContext::~JITLinkContext() {} 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const { 3430b57cec5SDimitry Andric return true; 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3468bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const { 3478bcb0991SDimitry Andric return LinkGraphPassFunction(); 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric 350*fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G, 3510b57cec5SDimitry Andric PassConfiguration &Config) { 3520b57cec5SDimitry Andric return Error::success(); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3558bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) { 3568bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) 3578bcb0991SDimitry Andric Sym->setLive(true); 3580b57cec5SDimitry Andric return Error::success(); 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric 361*fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B, 362*fe6060f1SDimitry Andric const Edge &E) { 363*fe6060f1SDimitry Andric std::string ErrMsg; 364*fe6060f1SDimitry Andric { 365*fe6060f1SDimitry Andric raw_string_ostream ErrStream(ErrMsg); 366*fe6060f1SDimitry Andric Section &Sec = B.getSection(); 367*fe6060f1SDimitry Andric ErrStream << "In graph " << G.getName() << ", section " << Sec.getName() 368*fe6060f1SDimitry Andric << ": relocation target "; 369*fe6060f1SDimitry Andric if (E.getTarget().hasName()) 370*fe6060f1SDimitry Andric ErrStream << "\"" << E.getTarget().getName() << "\" "; 371*fe6060f1SDimitry Andric ErrStream << "at address " << formatv("{0:x}", E.getTarget().getAddress()); 372*fe6060f1SDimitry Andric ErrStream << " is out of range of " << G.getEdgeKindName(E.getKind()) 373*fe6060f1SDimitry Andric << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " ("; 374*fe6060f1SDimitry Andric 375*fe6060f1SDimitry Andric Symbol *BestSymbolForBlock = nullptr; 376*fe6060f1SDimitry Andric for (auto *Sym : Sec.symbols()) 377*fe6060f1SDimitry Andric if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 && 378*fe6060f1SDimitry Andric (!BestSymbolForBlock || 379*fe6060f1SDimitry Andric Sym->getScope() < BestSymbolForBlock->getScope() || 380*fe6060f1SDimitry Andric Sym->getLinkage() < BestSymbolForBlock->getLinkage())) 381*fe6060f1SDimitry Andric BestSymbolForBlock = Sym; 382*fe6060f1SDimitry Andric 383*fe6060f1SDimitry Andric if (BestSymbolForBlock) 384*fe6060f1SDimitry Andric ErrStream << BestSymbolForBlock->getName() << ", "; 385*fe6060f1SDimitry Andric else 386*fe6060f1SDimitry Andric ErrStream << "<anonymous block> @ "; 387*fe6060f1SDimitry Andric 388*fe6060f1SDimitry Andric ErrStream << formatv("{0:x}", B.getAddress()) << " + " 389*fe6060f1SDimitry Andric << formatv("{0:x}", E.getOffset()) << ")"; 390*fe6060f1SDimitry Andric } 391*fe6060f1SDimitry Andric return make_error<JITLinkError>(std::move(ErrMsg)); 392*fe6060f1SDimitry Andric } 393*fe6060f1SDimitry Andric 394e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>> 395e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) { 396e8d8bef9SDimitry Andric auto Magic = identify_magic(ObjectBuffer.getBuffer()); 3970b57cec5SDimitry Andric switch (Magic) { 3980b57cec5SDimitry Andric case file_magic::macho_object: 399*fe6060f1SDimitry Andric return createLinkGraphFromMachOObject(ObjectBuffer); 4005ffd83dbSDimitry Andric case file_magic::elf_relocatable: 401*fe6060f1SDimitry Andric return createLinkGraphFromELFObject(ObjectBuffer); 4020b57cec5SDimitry Andric default: 403e8d8bef9SDimitry Andric return make_error<JITLinkError>("Unsupported file format"); 404e8d8bef9SDimitry Andric }; 405e8d8bef9SDimitry Andric } 406e8d8bef9SDimitry Andric 407e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) { 408e8d8bef9SDimitry Andric switch (G->getTargetTriple().getObjectFormat()) { 409e8d8bef9SDimitry Andric case Triple::MachO: 410e8d8bef9SDimitry Andric return link_MachO(std::move(G), std::move(Ctx)); 411e8d8bef9SDimitry Andric case Triple::ELF: 412e8d8bef9SDimitry Andric return link_ELF(std::move(G), std::move(Ctx)); 413e8d8bef9SDimitry Andric default: 414e8d8bef9SDimitry Andric Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format")); 4150b57cec5SDimitry Andric }; 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric } // end namespace jitlink 4190b57cec5SDimitry Andric } // end namespace llvm 420