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
1106c3fb27SDimitry 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"
165f757f3fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/aarch64.h"
175f757f3fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/i386.h"
185f757f3fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/loongarch.h"
195f757f3fSDimitry Andric #include "llvm/ExecutionEngine/JITLink/x86_64.h"
200b57cec5SDimitry Andric #include "llvm/Support/Format.h"
210b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
220b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric using namespace llvm::object;
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric #define DEBUG_TYPE "jitlink"
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric namespace {
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric enum JITLinkErrorCode { GenericJITLinkError = 1 };
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
340b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
350b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
360b57cec5SDimitry Andric class JITLinkerErrorCategory : public std::error_category {
370b57cec5SDimitry Andric public:
name() const380b57cec5SDimitry Andric const char *name() const noexcept override { return "runtimedyld"; }
390b57cec5SDimitry Andric
message(int Condition) const400b57cec5SDimitry Andric std::string message(int Condition) const override {
410b57cec5SDimitry Andric switch (static_cast<JITLinkErrorCode>(Condition)) {
420b57cec5SDimitry Andric case GenericJITLinkError:
430b57cec5SDimitry Andric return "Generic JITLink error";
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric llvm_unreachable("Unrecognized JITLinkErrorCode");
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric };
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric } // namespace
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric namespace llvm {
520b57cec5SDimitry Andric namespace jitlink {
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric char JITLinkError::ID = 0;
550b57cec5SDimitry Andric
log(raw_ostream & OS) const56fe6060f1SDimitry Andric void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
570b57cec5SDimitry Andric
convertToErrorCode() const580b57cec5SDimitry Andric std::error_code JITLinkError::convertToErrorCode() const {
59753f127fSDimitry Andric static JITLinkerErrorCategory TheJITLinkerErrorCategory;
60753f127fSDimitry Andric return std::error_code(GenericJITLinkError, TheJITLinkerErrorCategory);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
getGenericEdgeKindName(Edge::Kind K)638bcb0991SDimitry Andric const char *getGenericEdgeKindName(Edge::Kind K) {
640b57cec5SDimitry Andric switch (K) {
650b57cec5SDimitry Andric case Edge::Invalid:
660b57cec5SDimitry Andric return "INVALID RELOCATION";
670b57cec5SDimitry Andric case Edge::KeepAlive:
680b57cec5SDimitry Andric return "Keep-Alive";
690b57cec5SDimitry Andric default:
70e8d8bef9SDimitry Andric return "<Unrecognized edge kind>";
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
getLinkageName(Linkage L)748bcb0991SDimitry Andric const char *getLinkageName(Linkage L) {
758bcb0991SDimitry Andric switch (L) {
768bcb0991SDimitry Andric case Linkage::Strong:
778bcb0991SDimitry Andric return "strong";
788bcb0991SDimitry Andric case Linkage::Weak:
798bcb0991SDimitry Andric return "weak";
808bcb0991SDimitry Andric }
818bcb0991SDimitry Andric llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
828bcb0991SDimitry Andric }
838bcb0991SDimitry Andric
getScopeName(Scope S)848bcb0991SDimitry Andric const char *getScopeName(Scope S) {
858bcb0991SDimitry Andric switch (S) {
868bcb0991SDimitry Andric case Scope::Default:
878bcb0991SDimitry Andric return "default";
888bcb0991SDimitry Andric case Scope::Hidden:
898bcb0991SDimitry Andric return "hidden";
908bcb0991SDimitry Andric case Scope::Local:
918bcb0991SDimitry Andric return "local";
928bcb0991SDimitry Andric }
938bcb0991SDimitry Andric llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
948bcb0991SDimitry Andric }
958bcb0991SDimitry Andric
isCStringBlock(Block & B)9606c3fb27SDimitry Andric bool isCStringBlock(Block &B) {
9706c3fb27SDimitry Andric if (B.getSize() == 0) // Empty blocks are not valid C-strings.
9806c3fb27SDimitry Andric return false;
9906c3fb27SDimitry Andric
10006c3fb27SDimitry Andric // Zero-fill blocks of size one are valid empty strings.
10106c3fb27SDimitry Andric if (B.isZeroFill())
10206c3fb27SDimitry Andric return B.getSize() == 1;
10306c3fb27SDimitry Andric
10406c3fb27SDimitry Andric for (size_t I = 0; I != B.getSize() - 1; ++I)
10506c3fb27SDimitry Andric if (B.getContent()[I] == '\0')
10606c3fb27SDimitry Andric return false;
10706c3fb27SDimitry Andric
10806c3fb27SDimitry Andric return B.getContent()[B.getSize() - 1] == '\0';
10906c3fb27SDimitry Andric }
11006c3fb27SDimitry Andric
operator <<(raw_ostream & OS,const Block & B)1118bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
11204eeddc0SDimitry Andric return OS << B.getAddress() << " -- " << (B.getAddress() + B.getSize())
11304eeddc0SDimitry Andric << ": "
114fe6060f1SDimitry Andric << "size = " << formatv("{0:x8}", B.getSize()) << ", "
1158bcb0991SDimitry Andric << (B.isZeroFill() ? "zero-fill" : "content")
1168bcb0991SDimitry Andric << ", align = " << B.getAlignment()
1178bcb0991SDimitry Andric << ", align-ofs = " << B.getAlignmentOffset()
1188bcb0991SDimitry Andric << ", section = " << B.getSection().getName();
1198bcb0991SDimitry Andric }
1208bcb0991SDimitry Andric
operator <<(raw_ostream & OS,const Symbol & Sym)1218bcb0991SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
12204eeddc0SDimitry Andric OS << Sym.getAddress() << " (" << (Sym.isDefined() ? "block" : "addressable")
12304eeddc0SDimitry Andric << " + " << formatv("{0:x8}", Sym.getOffset())
124fe6060f1SDimitry Andric << "): size: " << formatv("{0:x8}", Sym.getSize())
125fe6060f1SDimitry Andric << ", linkage: " << formatv("{0:6}", getLinkageName(Sym.getLinkage()))
126fe6060f1SDimitry Andric << ", scope: " << formatv("{0:8}", getScopeName(Sym.getScope())) << ", "
127fe6060f1SDimitry Andric << (Sym.isLive() ? "live" : "dead") << " - "
128fe6060f1SDimitry Andric << (Sym.hasName() ? Sym.getName() : "<anonymous symbol>");
1290b57cec5SDimitry Andric return OS;
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
printEdge(raw_ostream & OS,const Block & B,const Edge & E,StringRef EdgeKindName)1328bcb0991SDimitry Andric void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
1330b57cec5SDimitry Andric StringRef EdgeKindName) {
13404eeddc0SDimitry Andric OS << "edge@" << B.getAddress() + E.getOffset() << ": " << B.getAddress()
13504eeddc0SDimitry Andric << " + " << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName
13604eeddc0SDimitry Andric << " -> ";
137e8d8bef9SDimitry Andric
138e8d8bef9SDimitry Andric auto &TargetSym = E.getTarget();
139e8d8bef9SDimitry Andric if (TargetSym.hasName())
140e8d8bef9SDimitry Andric OS << TargetSym.getName();
141e8d8bef9SDimitry Andric else {
142e8d8bef9SDimitry Andric auto &TargetBlock = TargetSym.getBlock();
143e8d8bef9SDimitry Andric auto &TargetSec = TargetBlock.getSection();
14404eeddc0SDimitry Andric orc::ExecutorAddr SecAddress(~uint64_t(0));
145e8d8bef9SDimitry Andric for (auto *B : TargetSec.blocks())
146e8d8bef9SDimitry Andric if (B->getAddress() < SecAddress)
147e8d8bef9SDimitry Andric SecAddress = B->getAddress();
148e8d8bef9SDimitry Andric
14904eeddc0SDimitry Andric orc::ExecutorAddrDiff SecDelta = TargetSym.getAddress() - SecAddress;
15004eeddc0SDimitry Andric OS << TargetSym.getAddress() << " (section " << TargetSec.getName();
151e8d8bef9SDimitry Andric if (SecDelta)
152e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", SecDelta);
15304eeddc0SDimitry Andric OS << " / block " << TargetBlock.getAddress();
154e8d8bef9SDimitry Andric if (TargetSym.getOffset())
155e8d8bef9SDimitry Andric OS << " + " << formatv("{0:x}", TargetSym.getOffset());
156e8d8bef9SDimitry Andric OS << ")";
157e8d8bef9SDimitry Andric }
158e8d8bef9SDimitry Andric
159e8d8bef9SDimitry Andric if (E.getAddend() != 0)
160e8d8bef9SDimitry Andric OS << " + " << E.getAddend();
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric
~Section()1630b57cec5SDimitry Andric Section::~Section() {
1648bcb0991SDimitry Andric for (auto *Sym : Symbols)
1658bcb0991SDimitry Andric Sym->~Symbol();
1668bcb0991SDimitry Andric for (auto *B : Blocks)
1678bcb0991SDimitry Andric B->~Block();
1688bcb0991SDimitry Andric }
1698bcb0991SDimitry Andric
splitBlock(Block & B,size_t SplitIndex,SplitBlockCache * Cache)170480093f4SDimitry Andric Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
171480093f4SDimitry Andric SplitBlockCache *Cache) {
172480093f4SDimitry Andric
173480093f4SDimitry Andric assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
174480093f4SDimitry Andric
175480093f4SDimitry Andric // If the split point covers all of B then just return B.
176480093f4SDimitry Andric if (SplitIndex == B.getSize())
177480093f4SDimitry Andric return B;
178480093f4SDimitry Andric
179480093f4SDimitry Andric assert(SplitIndex < B.getSize() && "SplitIndex out of range");
180480093f4SDimitry Andric
181480093f4SDimitry Andric // Create the new block covering [ 0, SplitIndex ).
182480093f4SDimitry Andric auto &NewBlock =
183480093f4SDimitry Andric B.isZeroFill()
184480093f4SDimitry Andric ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
185480093f4SDimitry Andric B.getAlignment(), B.getAlignmentOffset())
186480093f4SDimitry Andric : createContentBlock(
187fe6060f1SDimitry Andric B.getSection(), B.getContent().slice(0, SplitIndex),
188480093f4SDimitry Andric B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
189480093f4SDimitry Andric
190480093f4SDimitry Andric // Modify B to cover [ SplitIndex, B.size() ).
191480093f4SDimitry Andric B.setAddress(B.getAddress() + SplitIndex);
192fe6060f1SDimitry Andric B.setContent(B.getContent().slice(SplitIndex));
193480093f4SDimitry Andric B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
194480093f4SDimitry Andric B.getAlignment());
195480093f4SDimitry Andric
196480093f4SDimitry Andric // Handle edge transfer/update.
197480093f4SDimitry Andric {
198480093f4SDimitry Andric // Copy edges to NewBlock (recording their iterators so that we can remove
199480093f4SDimitry Andric // them from B), and update of Edges remaining on B.
200480093f4SDimitry Andric std::vector<Block::edge_iterator> EdgesToRemove;
2015ffd83dbSDimitry Andric for (auto I = B.edges().begin(); I != B.edges().end();) {
202480093f4SDimitry Andric if (I->getOffset() < SplitIndex) {
203480093f4SDimitry Andric NewBlock.addEdge(*I);
2045ffd83dbSDimitry Andric I = B.removeEdge(I);
2055ffd83dbSDimitry Andric } else {
206480093f4SDimitry Andric I->setOffset(I->getOffset() - SplitIndex);
2075ffd83dbSDimitry Andric ++I;
208480093f4SDimitry Andric }
209480093f4SDimitry Andric }
210480093f4SDimitry Andric }
211480093f4SDimitry Andric
212480093f4SDimitry Andric // Handle symbol transfer/update.
213480093f4SDimitry Andric {
214480093f4SDimitry Andric // Initialize the symbols cache if necessary.
215480093f4SDimitry Andric SplitBlockCache LocalBlockSymbolsCache;
216480093f4SDimitry Andric if (!Cache)
217480093f4SDimitry Andric Cache = &LocalBlockSymbolsCache;
218bdd1243dSDimitry Andric if (*Cache == std::nullopt) {
219480093f4SDimitry Andric *Cache = SplitBlockCache::value_type();
220480093f4SDimitry Andric for (auto *Sym : B.getSection().symbols())
221480093f4SDimitry Andric if (&Sym->getBlock() == &B)
222480093f4SDimitry Andric (*Cache)->push_back(Sym);
223480093f4SDimitry Andric
224480093f4SDimitry Andric llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
225480093f4SDimitry Andric return LHS->getOffset() > RHS->getOffset();
226480093f4SDimitry Andric });
227480093f4SDimitry Andric }
228480093f4SDimitry Andric auto &BlockSymbols = **Cache;
229480093f4SDimitry Andric
230480093f4SDimitry Andric // Transfer all symbols with offset less than SplitIndex to NewBlock.
231480093f4SDimitry Andric while (!BlockSymbols.empty() &&
232480093f4SDimitry Andric BlockSymbols.back()->getOffset() < SplitIndex) {
233349cc55cSDimitry Andric auto *Sym = BlockSymbols.back();
234349cc55cSDimitry Andric // If the symbol extends beyond the split, update the size to be within
235349cc55cSDimitry Andric // the new block.
236349cc55cSDimitry Andric if (Sym->getOffset() + Sym->getSize() > SplitIndex)
237349cc55cSDimitry Andric Sym->setSize(SplitIndex - Sym->getOffset());
238349cc55cSDimitry Andric Sym->setBlock(NewBlock);
239480093f4SDimitry Andric BlockSymbols.pop_back();
240480093f4SDimitry Andric }
241480093f4SDimitry Andric
242480093f4SDimitry Andric // Update offsets for all remaining symbols in B.
243480093f4SDimitry Andric for (auto *Sym : BlockSymbols)
244480093f4SDimitry Andric Sym->setOffset(Sym->getOffset() - SplitIndex);
245480093f4SDimitry Andric }
246480093f4SDimitry Andric
247480093f4SDimitry Andric return NewBlock;
248480093f4SDimitry Andric }
249480093f4SDimitry Andric
dump(raw_ostream & OS)250fe6060f1SDimitry Andric void LinkGraph::dump(raw_ostream &OS) {
251fe6060f1SDimitry Andric DenseMap<Block *, std::vector<Symbol *>> BlockSymbols;
2520b57cec5SDimitry Andric
253fe6060f1SDimitry Andric // Map from blocks to the symbols pointing at them.
254fe6060f1SDimitry Andric for (auto *Sym : defined_symbols())
255fe6060f1SDimitry Andric BlockSymbols[&Sym->getBlock()].push_back(Sym);
2560b57cec5SDimitry Andric
257fe6060f1SDimitry Andric // For each block, sort its symbols by something approximating
258fe6060f1SDimitry Andric // relevance.
259fe6060f1SDimitry Andric for (auto &KV : BlockSymbols)
260fe6060f1SDimitry Andric llvm::sort(KV.second, [](const Symbol *LHS, const Symbol *RHS) {
261fe6060f1SDimitry Andric if (LHS->getOffset() != RHS->getOffset())
262fe6060f1SDimitry Andric return LHS->getOffset() < RHS->getOffset();
263fe6060f1SDimitry Andric if (LHS->getLinkage() != RHS->getLinkage())
264fe6060f1SDimitry Andric return LHS->getLinkage() < RHS->getLinkage();
265fe6060f1SDimitry Andric if (LHS->getScope() != RHS->getScope())
266fe6060f1SDimitry Andric return LHS->getScope() < RHS->getScope();
267fe6060f1SDimitry Andric if (LHS->hasName()) {
268fe6060f1SDimitry Andric if (!RHS->hasName())
269fe6060f1SDimitry Andric return true;
270fe6060f1SDimitry Andric return LHS->getName() < RHS->getName();
2710b57cec5SDimitry Andric }
272fe6060f1SDimitry Andric return false;
273fe6060f1SDimitry Andric });
274fe6060f1SDimitry Andric
275fe6060f1SDimitry Andric for (auto &Sec : sections()) {
276fe6060f1SDimitry Andric OS << "section " << Sec.getName() << ":\n\n";
277fe6060f1SDimitry Andric
278fe6060f1SDimitry Andric std::vector<Block *> SortedBlocks;
279fe6060f1SDimitry Andric llvm::copy(Sec.blocks(), std::back_inserter(SortedBlocks));
280fe6060f1SDimitry Andric llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
281fe6060f1SDimitry Andric return LHS->getAddress() < RHS->getAddress();
282fe6060f1SDimitry Andric });
283fe6060f1SDimitry Andric
284fe6060f1SDimitry Andric for (auto *B : SortedBlocks) {
28504eeddc0SDimitry Andric OS << " block " << B->getAddress()
286fe6060f1SDimitry Andric << " size = " << formatv("{0:x8}", B->getSize())
287fe6060f1SDimitry Andric << ", align = " << B->getAlignment()
288fe6060f1SDimitry Andric << ", alignment-offset = " << B->getAlignmentOffset();
289fe6060f1SDimitry Andric if (B->isZeroFill())
290fe6060f1SDimitry Andric OS << ", zero-fill";
291fe6060f1SDimitry Andric OS << "\n";
292fe6060f1SDimitry Andric
293fe6060f1SDimitry Andric auto BlockSymsI = BlockSymbols.find(B);
294fe6060f1SDimitry Andric if (BlockSymsI != BlockSymbols.end()) {
295fe6060f1SDimitry Andric OS << " symbols:\n";
296fe6060f1SDimitry Andric auto &Syms = BlockSymsI->second;
297fe6060f1SDimitry Andric for (auto *Sym : Syms)
298fe6060f1SDimitry Andric OS << " " << *Sym << "\n";
299fe6060f1SDimitry Andric } else
300fe6060f1SDimitry Andric OS << " no symbols\n";
301fe6060f1SDimitry Andric
302fe6060f1SDimitry Andric if (!B->edges_empty()) {
303fe6060f1SDimitry Andric OS << " edges:\n";
304fe6060f1SDimitry Andric std::vector<Edge> SortedEdges;
305fe6060f1SDimitry Andric llvm::copy(B->edges(), std::back_inserter(SortedEdges));
306fe6060f1SDimitry Andric llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
307fe6060f1SDimitry Andric return LHS.getOffset() < RHS.getOffset();
308fe6060f1SDimitry Andric });
309fe6060f1SDimitry Andric for (auto &E : SortedEdges) {
31004eeddc0SDimitry Andric OS << " " << B->getFixupAddress(E) << " (block + "
31104eeddc0SDimitry Andric << formatv("{0:x8}", E.getOffset()) << "), addend = ";
312fe6060f1SDimitry Andric if (E.getAddend() >= 0)
313fe6060f1SDimitry Andric OS << formatv("+{0:x8}", E.getAddend());
314fe6060f1SDimitry Andric else
315fe6060f1SDimitry Andric OS << formatv("-{0:x8}", -E.getAddend());
316fe6060f1SDimitry Andric OS << ", kind = " << getEdgeKindName(E.getKind()) << ", target = ";
317fe6060f1SDimitry Andric if (E.getTarget().hasName())
318fe6060f1SDimitry Andric OS << E.getTarget().getName();
319fe6060f1SDimitry Andric else
320fe6060f1SDimitry Andric OS << "addressable@"
321fe6060f1SDimitry Andric << formatv("{0:x16}", E.getTarget().getAddress()) << "+"
322fe6060f1SDimitry Andric << formatv("{0:x8}", E.getTarget().getOffset());
3230b57cec5SDimitry Andric OS << "\n";
3240b57cec5SDimitry Andric }
325fe6060f1SDimitry Andric } else
326fe6060f1SDimitry Andric OS << " no edges\n";
327fe6060f1SDimitry Andric OS << "\n";
3280b57cec5SDimitry Andric }
3298bcb0991SDimitry Andric }
3300b57cec5SDimitry Andric
3318bcb0991SDimitry Andric OS << "Absolute symbols:\n";
332bdd1243dSDimitry Andric if (!absolute_symbols().empty()) {
3338bcb0991SDimitry Andric for (auto *Sym : absolute_symbols())
33404eeddc0SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym << "\n";
335fe6060f1SDimitry Andric } else
336fe6060f1SDimitry Andric OS << " none\n";
3370b57cec5SDimitry Andric
338fe6060f1SDimitry Andric OS << "\nExternal symbols:\n";
339bdd1243dSDimitry Andric if (!external_symbols().empty()) {
3408bcb0991SDimitry Andric for (auto *Sym : external_symbols())
341*0fca6ea1SDimitry Andric OS << " " << Sym->getAddress() << ": " << *Sym
342*0fca6ea1SDimitry Andric << (Sym->isWeaklyReferenced() ? " (weakly referenced)" : "") << "\n";
343fe6060f1SDimitry Andric } else
344fe6060f1SDimitry Andric OS << " none\n";
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const SymbolLookupFlags & LF)347480093f4SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
348480093f4SDimitry Andric switch (LF) {
349480093f4SDimitry Andric case SymbolLookupFlags::RequiredSymbol:
350480093f4SDimitry Andric return OS << "RequiredSymbol";
351480093f4SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol:
352480093f4SDimitry Andric return OS << "WeaklyReferencedSymbol";
353480093f4SDimitry Andric }
354480093f4SDimitry Andric llvm_unreachable("Unrecognized lookup flags");
355480093f4SDimitry Andric }
356480093f4SDimitry Andric
anchor()3578bcb0991SDimitry Andric void JITLinkAsyncLookupContinuation::anchor() {}
3588bcb0991SDimitry Andric
35981ad6265SDimitry Andric JITLinkContext::~JITLinkContext() = default;
3600b57cec5SDimitry Andric
shouldAddDefaultTargetPasses(const Triple & TT) const3610b57cec5SDimitry Andric bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
3620b57cec5SDimitry Andric return true;
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric
getMarkLivePass(const Triple & TT) const3658bcb0991SDimitry Andric LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
3668bcb0991SDimitry Andric return LinkGraphPassFunction();
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric
modifyPassConfig(LinkGraph & G,PassConfiguration & Config)369fe6060f1SDimitry Andric Error JITLinkContext::modifyPassConfig(LinkGraph &G,
3700b57cec5SDimitry Andric PassConfiguration &Config) {
3710b57cec5SDimitry Andric return Error::success();
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
markAllSymbolsLive(LinkGraph & G)3748bcb0991SDimitry Andric Error markAllSymbolsLive(LinkGraph &G) {
3758bcb0991SDimitry Andric for (auto *Sym : G.defined_symbols())
3768bcb0991SDimitry Andric Sym->setLive(true);
3770b57cec5SDimitry Andric return Error::success();
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric
makeTargetOutOfRangeError(const LinkGraph & G,const Block & B,const Edge & E)380fe6060f1SDimitry Andric Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
381fe6060f1SDimitry Andric const Edge &E) {
382fe6060f1SDimitry Andric std::string ErrMsg;
383fe6060f1SDimitry Andric {
384fe6060f1SDimitry Andric raw_string_ostream ErrStream(ErrMsg);
385fe6060f1SDimitry Andric Section &Sec = B.getSection();
386fe6060f1SDimitry Andric ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
387fe6060f1SDimitry Andric << ": relocation target ";
38804eeddc0SDimitry Andric if (E.getTarget().hasName()) {
389fe6060f1SDimitry Andric ErrStream << "\"" << E.getTarget().getName() << "\"";
39004eeddc0SDimitry Andric } else
39104eeddc0SDimitry Andric ErrStream << E.getTarget().getBlock().getSection().getName() << " + "
39204eeddc0SDimitry Andric << formatv("{0:x}", E.getOffset());
39304eeddc0SDimitry Andric ErrStream << " at address " << formatv("{0:x}", E.getTarget().getAddress())
39404eeddc0SDimitry Andric << " is out of range of " << G.getEdgeKindName(E.getKind())
395fe6060f1SDimitry Andric << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
396fe6060f1SDimitry Andric
397fe6060f1SDimitry Andric Symbol *BestSymbolForBlock = nullptr;
398fe6060f1SDimitry Andric for (auto *Sym : Sec.symbols())
399fe6060f1SDimitry Andric if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
400fe6060f1SDimitry Andric (!BestSymbolForBlock ||
401fe6060f1SDimitry Andric Sym->getScope() < BestSymbolForBlock->getScope() ||
402fe6060f1SDimitry Andric Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
403fe6060f1SDimitry Andric BestSymbolForBlock = Sym;
404fe6060f1SDimitry Andric
405fe6060f1SDimitry Andric if (BestSymbolForBlock)
406fe6060f1SDimitry Andric ErrStream << BestSymbolForBlock->getName() << ", ";
407fe6060f1SDimitry Andric else
408fe6060f1SDimitry Andric ErrStream << "<anonymous block> @ ";
409fe6060f1SDimitry Andric
410fe6060f1SDimitry Andric ErrStream << formatv("{0:x}", B.getAddress()) << " + "
411fe6060f1SDimitry Andric << formatv("{0:x}", E.getOffset()) << ")";
412fe6060f1SDimitry Andric }
413fe6060f1SDimitry Andric return make_error<JITLinkError>(std::move(ErrMsg));
414fe6060f1SDimitry Andric }
415fe6060f1SDimitry Andric
makeAlignmentError(llvm::orc::ExecutorAddr Loc,uint64_t Value,int N,const Edge & E)41681ad6265SDimitry Andric Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value, int N,
41781ad6265SDimitry Andric const Edge &E) {
41881ad6265SDimitry Andric return make_error<JITLinkError>("0x" + llvm::utohexstr(Loc.getValue()) +
41981ad6265SDimitry Andric " improper alignment for relocation " +
42081ad6265SDimitry Andric formatv("{0:d}", E.getKind()) + ": 0x" +
42181ad6265SDimitry Andric llvm::utohexstr(Value) +
42281ad6265SDimitry Andric " is not aligned to " + Twine(N) + " bytes");
42381ad6265SDimitry Andric }
42481ad6265SDimitry Andric
getAnonymousPointerCreator(const Triple & TT)4255f757f3fSDimitry Andric AnonymousPointerCreator getAnonymousPointerCreator(const Triple &TT) {
4265f757f3fSDimitry Andric switch (TT.getArch()) {
4275f757f3fSDimitry Andric case Triple::aarch64:
4285f757f3fSDimitry Andric return aarch64::createAnonymousPointer;
4295f757f3fSDimitry Andric case Triple::x86_64:
4305f757f3fSDimitry Andric return x86_64::createAnonymousPointer;
4315f757f3fSDimitry Andric case Triple::x86:
4325f757f3fSDimitry Andric return i386::createAnonymousPointer;
4335f757f3fSDimitry Andric case Triple::loongarch32:
4345f757f3fSDimitry Andric case Triple::loongarch64:
4355f757f3fSDimitry Andric return loongarch::createAnonymousPointer;
4365f757f3fSDimitry Andric default:
4375f757f3fSDimitry Andric return nullptr;
4385f757f3fSDimitry Andric }
4395f757f3fSDimitry Andric }
4405f757f3fSDimitry Andric
getPointerJumpStubCreator(const Triple & TT)4415f757f3fSDimitry Andric PointerJumpStubCreator getPointerJumpStubCreator(const Triple &TT) {
4425f757f3fSDimitry Andric switch (TT.getArch()) {
4435f757f3fSDimitry Andric case Triple::aarch64:
4445f757f3fSDimitry Andric return aarch64::createAnonymousPointerJumpStub;
4455f757f3fSDimitry Andric case Triple::x86_64:
4465f757f3fSDimitry Andric return x86_64::createAnonymousPointerJumpStub;
4475f757f3fSDimitry Andric case Triple::x86:
4485f757f3fSDimitry Andric return i386::createAnonymousPointerJumpStub;
4495f757f3fSDimitry Andric case Triple::loongarch32:
4505f757f3fSDimitry Andric case Triple::loongarch64:
4515f757f3fSDimitry Andric return loongarch::createAnonymousPointerJumpStub;
4525f757f3fSDimitry Andric default:
4535f757f3fSDimitry Andric return nullptr;
4545f757f3fSDimitry Andric }
4555f757f3fSDimitry Andric }
4565f757f3fSDimitry Andric
457e8d8bef9SDimitry Andric Expected<std::unique_ptr<LinkGraph>>
createLinkGraphFromObject(MemoryBufferRef ObjectBuffer)458e8d8bef9SDimitry Andric createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
459e8d8bef9SDimitry Andric auto Magic = identify_magic(ObjectBuffer.getBuffer());
4600b57cec5SDimitry Andric switch (Magic) {
4610b57cec5SDimitry Andric case file_magic::macho_object:
462fe6060f1SDimitry Andric return createLinkGraphFromMachOObject(ObjectBuffer);
4635ffd83dbSDimitry Andric case file_magic::elf_relocatable:
464fe6060f1SDimitry Andric return createLinkGraphFromELFObject(ObjectBuffer);
465753f127fSDimitry Andric case file_magic::coff_object:
466753f127fSDimitry Andric return createLinkGraphFromCOFFObject(ObjectBuffer);
4670b57cec5SDimitry Andric default:
468e8d8bef9SDimitry Andric return make_error<JITLinkError>("Unsupported file format");
469e8d8bef9SDimitry Andric };
470e8d8bef9SDimitry Andric }
471e8d8bef9SDimitry Andric
absoluteSymbolsLinkGraph(const Triple & TT,orc::SymbolMap Symbols)4721db9f3b2SDimitry Andric std::unique_ptr<LinkGraph> absoluteSymbolsLinkGraph(const Triple &TT,
4731db9f3b2SDimitry Andric orc::SymbolMap Symbols) {
4741db9f3b2SDimitry Andric unsigned PointerSize;
4751db9f3b2SDimitry Andric endianness Endianness =
4761db9f3b2SDimitry Andric TT.isLittleEndian() ? endianness::little : endianness::big;
4771db9f3b2SDimitry Andric switch (TT.getArch()) {
4781db9f3b2SDimitry Andric case Triple::aarch64:
4791db9f3b2SDimitry Andric case llvm::Triple::riscv64:
4801db9f3b2SDimitry Andric case Triple::x86_64:
4811db9f3b2SDimitry Andric PointerSize = 8;
4821db9f3b2SDimitry Andric break;
4831db9f3b2SDimitry Andric case llvm::Triple::arm:
4841db9f3b2SDimitry Andric case llvm::Triple::riscv32:
4851db9f3b2SDimitry Andric case llvm::Triple::x86:
4861db9f3b2SDimitry Andric PointerSize = 4;
4871db9f3b2SDimitry Andric break;
4881db9f3b2SDimitry Andric default:
4891db9f3b2SDimitry Andric llvm::report_fatal_error("unhandled target architecture");
4901db9f3b2SDimitry Andric }
4911db9f3b2SDimitry Andric
4921db9f3b2SDimitry Andric static std::atomic<uint64_t> Counter = {0};
4931db9f3b2SDimitry Andric auto Index = Counter.fetch_add(1, std::memory_order_relaxed);
4941db9f3b2SDimitry Andric auto G = std::make_unique<LinkGraph>(
4951db9f3b2SDimitry Andric "<Absolute Symbols " + std::to_string(Index) + ">", TT, PointerSize,
4961db9f3b2SDimitry Andric Endianness, /*GetEdgeKindName=*/nullptr);
4971db9f3b2SDimitry Andric for (auto &[Name, Def] : Symbols) {
4981db9f3b2SDimitry Andric auto &Sym =
4991db9f3b2SDimitry Andric G->addAbsoluteSymbol(*Name, Def.getAddress(), /*Size=*/0,
5001db9f3b2SDimitry Andric Linkage::Strong, Scope::Default, /*IsLive=*/true);
5011db9f3b2SDimitry Andric Sym.setCallable(Def.getFlags().isCallable());
5021db9f3b2SDimitry Andric }
5031db9f3b2SDimitry Andric
5041db9f3b2SDimitry Andric return G;
5051db9f3b2SDimitry Andric }
5061db9f3b2SDimitry Andric
link(std::unique_ptr<LinkGraph> G,std::unique_ptr<JITLinkContext> Ctx)507e8d8bef9SDimitry Andric void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
508e8d8bef9SDimitry Andric switch (G->getTargetTriple().getObjectFormat()) {
509e8d8bef9SDimitry Andric case Triple::MachO:
510e8d8bef9SDimitry Andric return link_MachO(std::move(G), std::move(Ctx));
511e8d8bef9SDimitry Andric case Triple::ELF:
512e8d8bef9SDimitry Andric return link_ELF(std::move(G), std::move(Ctx));
513753f127fSDimitry Andric case Triple::COFF:
514753f127fSDimitry Andric return link_COFF(std::move(G), std::move(Ctx));
515e8d8bef9SDimitry Andric default:
516e8d8bef9SDimitry Andric Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
5170b57cec5SDimitry Andric };
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric
5200b57cec5SDimitry Andric } // end namespace jitlink
5210b57cec5SDimitry Andric } // end namespace llvm
522