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 11*06c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h" 120b57cec5SDimitry Andric #include "llvm/BinaryFormat/Magic.h" 13753f127fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/COFF.h" 145ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/ELF.h" 150b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITLink/MachO.h" 160b57cec5SDimitry Andric #include "llvm/Support/Format.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 } // namespace 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric namespace llvm { 480b57cec5SDimitry Andric namespace jitlink { 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric char JITLinkError::ID = 0; 510b57cec5SDimitry Andric 52fe6060f1SDimitry Andric void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric std::error_code JITLinkError::convertToErrorCode() const { 55753f127fSDimitry Andric static JITLinkerErrorCategory TheJITLinkerErrorCategory; 56753f127fSDimitry Andric return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory); 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 92*06c3fb27SDimitry Andric bool isCStringBlock(Block &B) { 93*06c3fb27SDimitry Andric if (B.getSize() == 0) // Empty blocks are not valid C-strings. 94*06c3fb27SDimitry Andric return false; 95*06c3fb27SDimitry Andric 96*06c3fb27SDimitry Andric // Zero-fill blocks of size one are valid empty strings. 97*06c3fb27SDimitry Andric if (B.isZeroFill()) 98*06c3fb27SDimitry Andric return B.getSize() == 1; 99*06c3fb27SDimitry Andric 100*06c3fb27SDimitry Andric for (size_t I = 0; I != B.getSize() - 1; ++I) 101*06c3fb27SDimitry Andric if (B.getContent()[I] == '\0') 102*06c3fb27SDimitry Andric return false; 103*06c3fb27SDimitry Andric 104*06c3fb27SDimitry Andric return B.getContent()[B.getSize() - 1] == '\0'; 105*06c3fb27SDimitry Andric } 106*06c3fb27SDimitry Andric 1078bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Block &B) { 10804eeddc0SDimitry Andric return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize()) 10904eeddc0SDimitry Andric << ": " 110fe6060f1SDimitry Andric << "size = " << formatv("{0:x8}", B.getSize()) << ", " 1118bcb0991SDimitry Andric << (B.isZeroFill() ? "zero-fill" : "content") 1128bcb0991SDimitry Andric << ", align = " << B.getAlignment() 1138bcb0991SDimitry Andric << ", align-ofs = " << B.getAlignmentOffset() 1148bcb0991SDimitry Andric << ", section = " << B.getSection().getName(); 1158bcb0991SDimitry Andric } 1168bcb0991SDimitry Andric 1178bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) { 11804eeddc0SDimitry Andric OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable") 11904eeddc0SDimitry Andric << " + " << formatv("{0:x8}", Sym.getOffset()) 120fe6060f1SDimitry Andric << "): size: " << formatv("{0:x8}", Sym.getSize()) 121fe6060f1SDimitry Andric << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage())) 122fe6060f1SDimitry Andric << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", " 123fe6060f1SDimitry Andric << (Sym.isLive() ? "live" : "dead") << " - " 124fe6060f1SDimitry Andric << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>"); 1250b57cec5SDimitry Andric return OS; 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1288bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E, 1290b57cec5SDimitry Andric StringRef EdgeKindName) { 13004eeddc0SDimitry Andric OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress() 13104eeddc0SDimitry Andric << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName 13204eeddc0SDimitry Andric << " -> "; 133e8d8bef9SDimitry Andric 134e8d8bef9SDimitry Andric auto &TargetSym = E.getTarget(); 135e8d8bef9SDimitry Andric if (TargetSym.hasName()) 136e8d8bef9SDimitry Andric OS << TargetSym.getName(); 137e8d8bef9SDimitry Andric else { 138e8d8bef9SDimitry Andric auto &TargetBlock = TargetSym.getBlock(); 139e8d8bef9SDimitry Andric auto &TargetSec = TargetBlock.getSection(); 14004eeddc0SDimitry Andric orc::ExecutorAddr SecAddress(~uint64_t(0)); 141e8d8bef9SDimitry Andric for (auto *B : TargetSec.blocks()) 142e8d8bef9SDimitry Andric if (B->getAddress() < SecAddress) 143e8d8bef9SDimitry Andric SecAddress = B->getAddress(); 144e8d8bef9SDimitry Andric 14504eeddc0SDimitry Andric orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress; 14604eeddc0SDimitry Andric OS << TargetSym.getAddress() << " (section " << TargetSec.getName(); 147e8d8bef9SDimitry Andric if (SecDelta) 148e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", SecDelta); 14904eeddc0SDimitry Andric OS << " / block " << TargetBlock.getAddress(); 150e8d8bef9SDimitry Andric if (TargetSym.getOffset()) 151e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", TargetSym.getOffset()); 152e8d8bef9SDimitry Andric OS << ")"; 153e8d8bef9SDimitry Andric } 154e8d8bef9SDimitry Andric 155e8d8bef9SDimitry Andric if (E.getAddend() != 0) 156e8d8bef9SDimitry Andric OS << " + " << E.getAddend(); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric Section::~Section() { 1608bcb0991SDimitry Andric for (auto *Sym : Symbols) 1618bcb0991SDimitry Andric Sym->~Symbol(); 1628bcb0991SDimitry Andric for (auto *B : Blocks) 1638bcb0991SDimitry Andric B->~Block(); 1648bcb0991SDimitry Andric } 1658bcb0991SDimitry Andric 166480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex, 167480093f4SDimitry Andric SplitBlockCache *Cache) { 168480093f4SDimitry Andric 169480093f4SDimitry Andric assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0"); 170480093f4SDimitry Andric 171480093f4SDimitry Andric // If the split point covers all of B then just return B. 172480093f4SDimitry Andric if (SplitIndex == B.getSize()) 173480093f4SDimitry Andric return B; 174480093f4SDimitry Andric 175480093f4SDimitry Andric assert(SplitIndex < B.getSize() && "SplitIndex out of range"); 176480093f4SDimitry Andric 177480093f4SDimitry Andric // Create the new block covering [ 0, SplitIndex ). 178480093f4SDimitry Andric auto &NewBlock = 179480093f4SDimitry Andric B.isZeroFill() 180480093f4SDimitry Andric ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(), 181480093f4SDimitry Andric B.getAlignment(), B.getAlignmentOffset()) 182480093f4SDimitry Andric : createContentBlock( 183fe6060f1SDimitry Andric B.getSection(), B.getContent().slice(0, SplitIndex), 184480093f4SDimitry Andric B.getAddress(), B.getAlignment(), B.getAlignmentOffset()); 185480093f4SDimitry Andric 186480093f4SDimitry Andric // Modify B to cover [ SplitIndex, B.size() ). 187480093f4SDimitry Andric B.setAddress(B.getAddress() + SplitIndex); 188fe6060f1SDimitry Andric B.setContent(B.getContent().slice(SplitIndex)); 189480093f4SDimitry Andric B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) % 190480093f4SDimitry Andric B.getAlignment()); 191480093f4SDimitry Andric 192480093f4SDimitry Andric // Handle edge transfer/update. 193480093f4SDimitry Andric { 194480093f4SDimitry Andric // Copy edges to NewBlock (recording their iterators so that we can remove 195480093f4SDimitry Andric // them from B), and update of Edges remaining on B. 196480093f4SDimitry Andric std::vector<Block::edge_iterator> EdgesToRemove; 1975ffd83dbSDimitry Andric for (auto I = B.edges().begin(); I != B.edges().end();) { 198480093f4SDimitry Andric if (I->getOffset() < SplitIndex) { 199480093f4SDimitry Andric NewBlock.addEdge(*I); 2005ffd83dbSDimitry Andric I = B.removeEdge(I); 2015ffd83dbSDimitry Andric } else { 202480093f4SDimitry Andric I->setOffset(I->getOffset() - SplitIndex); 2035ffd83dbSDimitry Andric ++I; 204480093f4SDimitry Andric } 205480093f4SDimitry Andric } 206480093f4SDimitry Andric } 207480093f4SDimitry Andric 208480093f4SDimitry Andric // Handle symbol transfer/update. 209480093f4SDimitry Andric { 210480093f4SDimitry Andric // Initialize the symbols cache if necessary. 211480093f4SDimitry Andric SplitBlockCache LocalBlockSymbolsCache; 212480093f4SDimitry Andric if (!Cache) 213480093f4SDimitry Andric Cache = &LocalBlockSymbolsCache; 214bdd1243dSDimitry Andric if (*Cache == std::nullopt) { 215480093f4SDimitry Andric *Cache = SplitBlockCache::value_type(); 216480093f4SDimitry Andric for (auto *Sym : B.getSection().symbols()) 217480093f4SDimitry Andric if (&Sym->getBlock() == &B) 218480093f4SDimitry Andric (*Cache)->push_back(Sym); 219480093f4SDimitry Andric 220480093f4SDimitry Andric llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) { 221480093f4SDimitry Andric return LHS->getOffset() > RHS->getOffset(); 222480093f4SDimitry Andric }); 223480093f4SDimitry Andric } 224480093f4SDimitry Andric auto &BlockSymbols = **Cache; 225480093f4SDimitry Andric 226480093f4SDimitry Andric // Transfer all symbols with offset less than SplitIndex to NewBlock. 227480093f4SDimitry Andric while (!BlockSymbols.empty() && 228480093f4SDimitry Andric BlockSymbols.back()->getOffset() < SplitIndex) { 229349cc55cSDimitry Andric auto *Sym = BlockSymbols.back(); 230349cc55cSDimitry Andric // If the symbol extends beyond the split, update the size to be within 231349cc55cSDimitry Andric // the new block. 232349cc55cSDimitry Andric if (Sym->getOffset() + Sym->getSize() > SplitIndex) 233349cc55cSDimitry Andric Sym->setSize(SplitIndex - Sym->getOffset()); 234349cc55cSDimitry Andric Sym->setBlock(NewBlock); 235480093f4SDimitry Andric BlockSymbols.pop_back(); 236480093f4SDimitry Andric } 237480093f4SDimitry Andric 238480093f4SDimitry Andric // Update offsets for all remaining symbols in B. 239480093f4SDimitry Andric for (auto *Sym : BlockSymbols) 240480093f4SDimitry Andric Sym->setOffset(Sym->getOffset() - SplitIndex); 241480093f4SDimitry Andric } 242480093f4SDimitry Andric 243480093f4SDimitry Andric return NewBlock; 244480093f4SDimitry Andric } 245480093f4SDimitry Andric 246fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) { 247fe6060f1SDimitry Andric DenseMap<Block *, std::vector<Symbol *>> BlockSymbols; 2480b57cec5SDimitry Andric 249fe6060f1SDimitry Andric // Map from blocks to the symbols pointing at them. 250fe6060f1SDimitry Andric for (auto *Sym : defined_symbols()) 251fe6060f1SDimitry Andric BlockSymbols[&Sym->getBlock()].push_back(Sym); 2520b57cec5SDimitry Andric 253fe6060f1SDimitry Andric // For each block, sort its symbols by something approximating 254fe6060f1SDimitry Andric // relevance. 255fe6060f1SDimitry Andric for (auto &KV : BlockSymbols) 256fe6060f1SDimitry Andric llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) { 257fe6060f1SDimitry Andric if (LHS->getOffset() != RHS->getOffset()) 258fe6060f1SDimitry Andric return LHS->getOffset() < RHS->getOffset(); 259fe6060f1SDimitry Andric if (LHS->getLinkage() != RHS->getLinkage()) 260fe6060f1SDimitry Andric return LHS->getLinkage() < RHS->getLinkage(); 261fe6060f1SDimitry Andric if (LHS->getScope() != RHS->getScope()) 262fe6060f1SDimitry Andric return LHS->getScope() < RHS->getScope(); 263fe6060f1SDimitry Andric if (LHS->hasName()) { 264fe6060f1SDimitry Andric if (!RHS->hasName()) 265fe6060f1SDimitry Andric return true; 266fe6060f1SDimitry Andric return LHS->getName() < RHS->getName(); 2670b57cec5SDimitry Andric } 268fe6060f1SDimitry Andric return false; 269fe6060f1SDimitry Andric }); 270fe6060f1SDimitry Andric 271fe6060f1SDimitry Andric for (auto &Sec : sections()) { 272fe6060f1SDimitry Andric OS << "section " << Sec.getName() << ":\n\n"; 273fe6060f1SDimitry Andric 274fe6060f1SDimitry Andric std::vector<Block *> SortedBlocks; 275fe6060f1SDimitry Andric llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks)); 276fe6060f1SDimitry Andric llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) { 277fe6060f1SDimitry Andric return LHS->getAddress() < RHS->getAddress(); 278fe6060f1SDimitry Andric }); 279fe6060f1SDimitry Andric 280fe6060f1SDimitry Andric for (auto *B : SortedBlocks) { 28104eeddc0SDimitry Andric OS << " block " << B->getAddress() 282fe6060f1SDimitry Andric << " size = " << formatv("{0:x8}", B->getSize()) 283fe6060f1SDimitry Andric << ", align = " << B->getAlignment() 284fe6060f1SDimitry Andric << ", alignment-offset = " << B->getAlignmentOffset(); 285fe6060f1SDimitry Andric if (B->isZeroFill()) 286fe6060f1SDimitry Andric OS << ", zero-fill"; 287fe6060f1SDimitry Andric OS << "\n"; 288fe6060f1SDimitry Andric 289fe6060f1SDimitry Andric auto BlockSymsI = BlockSymbols.find(B); 290fe6060f1SDimitry Andric if (BlockSymsI != BlockSymbols.end()) { 291fe6060f1SDimitry Andric OS << " symbols:\n"; 292fe6060f1SDimitry Andric auto &Syms = BlockSymsI->second; 293fe6060f1SDimitry Andric for (auto *Sym : Syms) 294fe6060f1SDimitry Andric OS << " " << *Sym << "\n"; 295fe6060f1SDimitry Andric } else 296fe6060f1SDimitry Andric OS << " no symbols\n"; 297fe6060f1SDimitry Andric 298fe6060f1SDimitry Andric if (!B->edges_empty()) { 299fe6060f1SDimitry Andric OS << " edges:\n"; 300fe6060f1SDimitry Andric std::vector<Edge> SortedEdges; 301fe6060f1SDimitry Andric llvm::copy(B->edges(), std::back_inserter(SortedEdges)); 302fe6060f1SDimitry Andric llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) { 303fe6060f1SDimitry Andric return LHS.getOffset() < RHS.getOffset(); 304fe6060f1SDimitry Andric }); 305fe6060f1SDimitry Andric for (auto &E : SortedEdges) { 30604eeddc0SDimitry Andric OS << " " << B->getFixupAddress(E) << " (block + " 30704eeddc0SDimitry Andric << formatv("{0:x8}", E.getOffset()) << "), addend = "; 308fe6060f1SDimitry Andric if (E.getAddend() >= 0) 309fe6060f1SDimitry Andric OS << formatv("+{0:x8}", E.getAddend()); 310fe6060f1SDimitry Andric else 311fe6060f1SDimitry Andric OS << formatv("-{0:x8}", -E.getAddend()); 312fe6060f1SDimitry Andric OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = "; 313fe6060f1SDimitry Andric if (E.getTarget().hasName()) 314fe6060f1SDimitry Andric OS << E.getTarget().getName(); 315fe6060f1SDimitry Andric else 316fe6060f1SDimitry Andric OS << "addressable@" 317fe6060f1SDimitry Andric << formatv("{0:x16}", E.getTarget().getAddress()) << "+" 318fe6060f1SDimitry Andric << formatv("{0:x8}", E.getTarget().getOffset()); 3190b57cec5SDimitry Andric OS << "\n"; 3200b57cec5SDimitry Andric } 321fe6060f1SDimitry Andric } else 322fe6060f1SDimitry Andric OS << " no edges\n"; 323fe6060f1SDimitry Andric OS << "\n"; 3240b57cec5SDimitry Andric } 3258bcb0991SDimitry Andric } 3260b57cec5SDimitry Andric 3278bcb0991SDimitry Andric OS << "Absolute symbols:\n"; 328bdd1243dSDimitry Andric if (!absolute_symbols().empty()) { 3298bcb0991SDimitry Andric for (auto *Sym : absolute_symbols()) 33004eeddc0SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym << "\n"; 331fe6060f1SDimitry Andric } else 332fe6060f1SDimitry Andric OS << " none\n"; 3330b57cec5SDimitry Andric 334fe6060f1SDimitry Andric OS << "\nExternal symbols:\n"; 335bdd1243dSDimitry Andric if (!external_symbols().empty()) { 3368bcb0991SDimitry Andric for (auto *Sym : external_symbols()) 33704eeddc0SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym << "\n"; 338fe6060f1SDimitry Andric } else 339fe6060f1SDimitry Andric OS << " none\n"; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 342480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) { 343480093f4SDimitry Andric switch (LF) { 344480093f4SDimitry Andric case SymbolLookupFlags::RequiredSymbol: 345480093f4SDimitry Andric return OS << "RequiredSymbol"; 346480093f4SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol: 347480093f4SDimitry Andric return OS << "WeaklyReferencedSymbol"; 348480093f4SDimitry Andric } 349480093f4SDimitry Andric llvm_unreachable("Unrecognized lookup flags"); 350480093f4SDimitry Andric } 351480093f4SDimitry Andric 3528bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {} 3538bcb0991SDimitry Andric 35481ad6265SDimitry Andric JITLinkContext::~JITLinkContext() = default; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const { 3570b57cec5SDimitry Andric return true; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3608bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const { 3618bcb0991SDimitry Andric return LinkGraphPassFunction(); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 364fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G, 3650b57cec5SDimitry Andric PassConfiguration &Config) { 3660b57cec5SDimitry Andric return Error::success(); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3698bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) { 3708bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols()) 3718bcb0991SDimitry Andric Sym->setLive(true); 3720b57cec5SDimitry Andric return Error::success(); 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 375fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B, 376fe6060f1SDimitry Andric const Edge &E) { 377fe6060f1SDimitry Andric std::string ErrMsg; 378fe6060f1SDimitry Andric { 379fe6060f1SDimitry Andric raw_string_ostream ErrStream(ErrMsg); 380fe6060f1SDimitry Andric Section &Sec = B.getSection(); 381fe6060f1SDimitry Andric ErrStream << "In graph " << G.getName() << ", section " << Sec.getName() 382fe6060f1SDimitry Andric << ": relocation target "; 38304eeddc0SDimitry Andric if (E.getTarget().hasName()) { 384fe6060f1SDimitry Andric ErrStream << "\"" << E.getTarget().getName() << "\""; 38504eeddc0SDimitry Andric } else 38604eeddc0SDimitry Andric ErrStream << E.getTarget().getBlock().getSection().getName() << " + " 38704eeddc0SDimitry Andric << formatv("{0:x}", E.getOffset()); 38804eeddc0SDimitry Andric ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress()) 38904eeddc0SDimitry Andric << " is out of range of " << G.getEdgeKindName(E.getKind()) 390fe6060f1SDimitry Andric << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " ("; 391fe6060f1SDimitry Andric 392fe6060f1SDimitry Andric Symbol *BestSymbolForBlock = nullptr; 393fe6060f1SDimitry Andric for (auto *Sym : Sec.symbols()) 394fe6060f1SDimitry Andric if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 && 395fe6060f1SDimitry Andric (!BestSymbolForBlock || 396fe6060f1SDimitry Andric Sym->getScope() < BestSymbolForBlock->getScope() || 397fe6060f1SDimitry Andric Sym->getLinkage() < BestSymbolForBlock->getLinkage())) 398fe6060f1SDimitry Andric BestSymbolForBlock = Sym; 399fe6060f1SDimitry Andric 400fe6060f1SDimitry Andric if (BestSymbolForBlock) 401fe6060f1SDimitry Andric ErrStream << BestSymbolForBlock->getName() << ", "; 402fe6060f1SDimitry Andric else 403fe6060f1SDimitry Andric ErrStream << "<anonymous block> @ "; 404fe6060f1SDimitry Andric 405fe6060f1SDimitry Andric ErrStream << formatv("{0:x}", B.getAddress()) << " + " 406fe6060f1SDimitry Andric << formatv("{0:x}", E.getOffset()) << ")"; 407fe6060f1SDimitry Andric } 408fe6060f1SDimitry Andric return make_error<JITLinkError>(std::move(ErrMsg)); 409fe6060f1SDimitry Andric } 410fe6060f1SDimitry Andric 41181ad6265SDimitry Andric Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value, int N, 41281ad6265SDimitry Andric const Edge &E) { 41381ad6265SDimitry Andric return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) + 41481ad6265SDimitry Andric " improper alignment for relocation " + 41581ad6265SDimitry Andric formatv("{0:d}", E.getKind()) + ": 0x" + 41681ad6265SDimitry Andric llvm::utohexstr(Value) + 41781ad6265SDimitry Andric " is not aligned to " + Twine(N) + " bytes"); 41881ad6265SDimitry Andric } 41981ad6265SDimitry Andric 420e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>> 421e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) { 422e8d8bef9SDimitry Andric auto Magic = identify_magic(ObjectBuffer.getBuffer()); 4230b57cec5SDimitry Andric switch (Magic) { 4240b57cec5SDimitry Andric case file_magic::macho_object: 425fe6060f1SDimitry Andric return createLinkGraphFromMachOObject(ObjectBuffer); 4265ffd83dbSDimitry Andric case file_magic::elf_relocatable: 427fe6060f1SDimitry Andric return createLinkGraphFromELFObject(ObjectBuffer); 428753f127fSDimitry Andric case file_magic::coff_object: 429753f127fSDimitry Andric return createLinkGraphFromCOFFObject(ObjectBuffer); 4300b57cec5SDimitry Andric default: 431e8d8bef9SDimitry Andric return make_error<JITLinkError>("Unsupported file format"); 432e8d8bef9SDimitry Andric }; 433e8d8bef9SDimitry Andric } 434e8d8bef9SDimitry Andric 435e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) { 436e8d8bef9SDimitry Andric switch (G->getTargetTriple().getObjectFormat()) { 437e8d8bef9SDimitry Andric case Triple::MachO: 438e8d8bef9SDimitry Andric return link_MachO(std::move(G), std::move(Ctx)); 439e8d8bef9SDimitry Andric case Triple::ELF: 440e8d8bef9SDimitry Andric return link_ELF(std::move(G), std::move(Ctx)); 441753f127fSDimitry Andric case Triple::COFF: 442753f127fSDimitry Andric return link_COFF(std::move(G), std::move(Ctx)); 443e8d8bef9SDimitry Andric default: 444e8d8bef9SDimitry Andric Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format")); 4450b57cec5SDimitry Andric }; 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric } // end namespace jitlink 4490b57cec5SDimitry Andric } // end namespace llvm 450