1 //===------------- JITLink.cpp - Core Run-time JIT linker APIs ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
11 
12 #include "llvm/BinaryFormat/Magic.h"
13 #include "llvm/ExecutionEngine/JITLink/ELF.h"
14 #include "llvm/ExecutionEngine/JITLink/MachO.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace llvm;
21 using namespace llvm::object;
22 
23 #define DEBUG_TYPE "jitlink"
24 
25 namespace {
26 
27 enum JITLinkErrorCode { GenericJITLinkError = 1 };
28 
29 // FIXME: This class is only here to support the transition to llvm::Error. It
30 // will be removed once this transition is complete. Clients should prefer to
31 // deal with the Error value directly, rather than converting to error_code.
32 class JITLinkerErrorCategory : public std::error_category {
33 public:
34   const char *name() const noexcept override { return "runtimedyld"; }
35 
36   std::string message(int Condition) const override {
37     switch (static_cast<JITLinkErrorCode>(Condition)) {
38     case GenericJITLinkError:
39       return "Generic JITLink error";
40     }
41     llvm_unreachable("Unrecognized JITLinkErrorCode");
42   }
43 };
44 
45 static ManagedStatic<JITLinkerErrorCategory> JITLinkerErrorCategory;
46 
47 } // namespace
48 
49 namespace llvm {
50 namespace jitlink {
51 
52 char JITLinkError::ID = 0;
53 
54 void JITLinkError::log(raw_ostream &OS) const { OS << ErrMsg; }
55 
56 std::error_code JITLinkError::convertToErrorCode() const {
57   return std::error_code(GenericJITLinkError, *JITLinkerErrorCategory);
58 }
59 
60 const char *getGenericEdgeKindName(Edge::Kind K) {
61   switch (K) {
62   case Edge::Invalid:
63     return "INVALID RELOCATION";
64   case Edge::KeepAlive:
65     return "Keep-Alive";
66   default:
67     return "<Unrecognized edge kind>";
68   }
69 }
70 
71 const char *getLinkageName(Linkage L) {
72   switch (L) {
73   case Linkage::Strong:
74     return "strong";
75   case Linkage::Weak:
76     return "weak";
77   }
78   llvm_unreachable("Unrecognized llvm.jitlink.Linkage enum");
79 }
80 
81 const char *getScopeName(Scope S) {
82   switch (S) {
83   case Scope::Default:
84     return "default";
85   case Scope::Hidden:
86     return "hidden";
87   case Scope::Local:
88     return "local";
89   }
90   llvm_unreachable("Unrecognized llvm.jitlink.Scope enum");
91 }
92 
93 raw_ostream &operator<<(raw_ostream &OS, const Block &B) {
94   return OS << formatv("{0:x16}", B.getAddress()) << " -- "
95             << formatv("{0:x16}", B.getAddress() + B.getSize()) << ": "
96             << "size = " << formatv("{0:x}", B.getSize()) << ", "
97             << (B.isZeroFill() ? "zero-fill" : "content")
98             << ", align = " << B.getAlignment()
99             << ", align-ofs = " << B.getAlignmentOffset()
100             << ", section = " << B.getSection().getName();
101 }
102 
103 raw_ostream &operator<<(raw_ostream &OS, const Symbol &Sym) {
104   OS << "<";
105   if (Sym.getName().empty())
106     OS << "*anon*";
107   else
108     OS << Sym.getName();
109   OS << ": flags = ";
110   switch (Sym.getLinkage()) {
111   case Linkage::Strong:
112     OS << 'S';
113     break;
114   case Linkage::Weak:
115     OS << 'W';
116     break;
117   }
118   switch (Sym.getScope()) {
119   case Scope::Default:
120     OS << 'D';
121     break;
122   case Scope::Hidden:
123     OS << 'H';
124     break;
125   case Scope::Local:
126     OS << 'L';
127     break;
128   }
129   OS << (Sym.isLive() ? '+' : '-')
130      << ", size = " << formatv("{0:x}", Sym.getSize())
131      << ", addr = " << formatv("{0:x16}", Sym.getAddress()) << " ("
132      << formatv("{0:x16}", Sym.getAddressable().getAddress()) << " + "
133      << formatv("{0:x}", Sym.getOffset());
134   if (Sym.isDefined())
135     OS << " " << Sym.getBlock().getSection().getName();
136   OS << ")>";
137   return OS;
138 }
139 
140 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
141                StringRef EdgeKindName) {
142   OS << "edge@" << formatv("{0:x16}", B.getAddress() + E.getOffset()) << ": "
143      << formatv("{0:x16}", B.getAddress()) << " + "
144      << formatv("{0:x}", E.getOffset()) << " -- " << EdgeKindName << " -> ";
145 
146   auto &TargetSym = E.getTarget();
147   if (TargetSym.hasName())
148     OS << TargetSym.getName();
149   else {
150     auto &TargetBlock = TargetSym.getBlock();
151     auto &TargetSec = TargetBlock.getSection();
152     JITTargetAddress SecAddress = ~JITTargetAddress(0);
153     for (auto *B : TargetSec.blocks())
154       if (B->getAddress() < SecAddress)
155         SecAddress = B->getAddress();
156 
157     JITTargetAddress SecDelta = TargetSym.getAddress() - SecAddress;
158     OS << formatv("{0:x16}", TargetSym.getAddress()) << " (section "
159        << TargetSec.getName();
160     if (SecDelta)
161       OS << " + " << formatv("{0:x}", SecDelta);
162     OS << " / block " << formatv("{0:x16}", TargetBlock.getAddress());
163     if (TargetSym.getOffset())
164       OS << " + " << formatv("{0:x}", TargetSym.getOffset());
165     OS << ")";
166   }
167 
168   if (E.getAddend() != 0)
169     OS << " + " << E.getAddend();
170 }
171 
172 Section::~Section() {
173   for (auto *Sym : Symbols)
174     Sym->~Symbol();
175   for (auto *B : Blocks)
176     B->~Block();
177 }
178 
179 Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
180                              SplitBlockCache *Cache) {
181 
182   assert(SplitIndex > 0 && "splitBlock can not be called with SplitIndex == 0");
183 
184   // If the split point covers all of B then just return B.
185   if (SplitIndex == B.getSize())
186     return B;
187 
188   assert(SplitIndex < B.getSize() && "SplitIndex out of range");
189 
190   // Create the new block covering [ 0, SplitIndex ).
191   auto &NewBlock =
192       B.isZeroFill()
193           ? createZeroFillBlock(B.getSection(), SplitIndex, B.getAddress(),
194                                 B.getAlignment(), B.getAlignmentOffset())
195           : createContentBlock(
196                 B.getSection(), B.getContent().slice(0, SplitIndex),
197                 B.getAddress(), B.getAlignment(), B.getAlignmentOffset());
198 
199   // Modify B to cover [ SplitIndex, B.size() ).
200   B.setAddress(B.getAddress() + SplitIndex);
201   B.setContent(B.getContent().slice(SplitIndex));
202   B.setAlignmentOffset((B.getAlignmentOffset() + SplitIndex) %
203                        B.getAlignment());
204 
205   // Handle edge transfer/update.
206   {
207     // Copy edges to NewBlock (recording their iterators so that we can remove
208     // them from B), and update of Edges remaining on B.
209     std::vector<Block::edge_iterator> EdgesToRemove;
210     for (auto I = B.edges().begin(); I != B.edges().end();) {
211       if (I->getOffset() < SplitIndex) {
212         NewBlock.addEdge(*I);
213         I = B.removeEdge(I);
214       } else {
215         I->setOffset(I->getOffset() - SplitIndex);
216         ++I;
217       }
218     }
219   }
220 
221   // Handle symbol transfer/update.
222   {
223     // Initialize the symbols cache if necessary.
224     SplitBlockCache LocalBlockSymbolsCache;
225     if (!Cache)
226       Cache = &LocalBlockSymbolsCache;
227     if (*Cache == None) {
228       *Cache = SplitBlockCache::value_type();
229       for (auto *Sym : B.getSection().symbols())
230         if (&Sym->getBlock() == &B)
231           (*Cache)->push_back(Sym);
232 
233       llvm::sort(**Cache, [](const Symbol *LHS, const Symbol *RHS) {
234         return LHS->getOffset() > RHS->getOffset();
235       });
236     }
237     auto &BlockSymbols = **Cache;
238 
239     // Transfer all symbols with offset less than SplitIndex to NewBlock.
240     while (!BlockSymbols.empty() &&
241            BlockSymbols.back()->getOffset() < SplitIndex) {
242       BlockSymbols.back()->setBlock(NewBlock);
243       BlockSymbols.pop_back();
244     }
245 
246     // Update offsets for all remaining symbols in B.
247     for (auto *Sym : BlockSymbols)
248       Sym->setOffset(Sym->getOffset() - SplitIndex);
249   }
250 
251   return NewBlock;
252 }
253 
254 void LinkGraph::dump(raw_ostream &OS) {
255   OS << "Symbols:\n";
256   for (auto *Sym : defined_symbols()) {
257     OS << "  " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
258        << "\n";
259     if (Sym->isDefined()) {
260       for (auto &E : Sym->getBlock().edges()) {
261         OS << "    ";
262         printEdge(OS, Sym->getBlock(), E, getEdgeKindName(E.getKind()));
263         OS << "\n";
264       }
265     }
266   }
267 
268   OS << "Absolute symbols:\n";
269   for (auto *Sym : absolute_symbols())
270     OS << "  " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
271        << "\n";
272 
273   OS << "External symbols:\n";
274   for (auto *Sym : external_symbols())
275     OS << "  " << format("0x%016" PRIx64, Sym->getAddress()) << ": " << *Sym
276        << "\n";
277 }
278 
279 raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF) {
280   switch (LF) {
281   case SymbolLookupFlags::RequiredSymbol:
282     return OS << "RequiredSymbol";
283   case SymbolLookupFlags::WeaklyReferencedSymbol:
284     return OS << "WeaklyReferencedSymbol";
285   }
286   llvm_unreachable("Unrecognized lookup flags");
287 }
288 
289 void JITLinkAsyncLookupContinuation::anchor() {}
290 
291 JITLinkContext::~JITLinkContext() {}
292 
293 bool JITLinkContext::shouldAddDefaultTargetPasses(const Triple &TT) const {
294   return true;
295 }
296 
297 LinkGraphPassFunction JITLinkContext::getMarkLivePass(const Triple &TT) const {
298   return LinkGraphPassFunction();
299 }
300 
301 Error JITLinkContext::modifyPassConfig(LinkGraph &G,
302                                        PassConfiguration &Config) {
303   return Error::success();
304 }
305 
306 Error markAllSymbolsLive(LinkGraph &G) {
307   for (auto *Sym : G.defined_symbols())
308     Sym->setLive(true);
309   return Error::success();
310 }
311 
312 Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
313                                 const Edge &E) {
314   std::string ErrMsg;
315   {
316     raw_string_ostream ErrStream(ErrMsg);
317     Section &Sec = B.getSection();
318     ErrStream << "In graph " << G.getName() << ", section " << Sec.getName()
319               << ": relocation target ";
320     if (E.getTarget().hasName())
321       ErrStream << "\"" << E.getTarget().getName() << "\" ";
322     ErrStream << "at address " << formatv("{0:x}", E.getTarget().getAddress());
323     ErrStream << " is out of range of " << G.getEdgeKindName(E.getKind())
324               << " fixup at " << formatv("{0:x}", B.getFixupAddress(E)) << " (";
325 
326     Symbol *BestSymbolForBlock = nullptr;
327     for (auto *Sym : Sec.symbols())
328       if (&Sym->getBlock() == &B && Sym->hasName() && Sym->getOffset() == 0 &&
329           (!BestSymbolForBlock ||
330            Sym->getScope() < BestSymbolForBlock->getScope() ||
331            Sym->getLinkage() < BestSymbolForBlock->getLinkage()))
332         BestSymbolForBlock = Sym;
333 
334     if (BestSymbolForBlock)
335       ErrStream << BestSymbolForBlock->getName() << ", ";
336     else
337       ErrStream << "<anonymous block> @ ";
338 
339     ErrStream << formatv("{0:x}", B.getAddress()) << " + "
340               << formatv("{0:x}", E.getOffset()) << ")";
341   }
342   return make_error<JITLinkError>(std::move(ErrMsg));
343 }
344 
345 Expected<std::unique_ptr<LinkGraph>>
346 createLinkGraphFromObject(MemoryBufferRef ObjectBuffer) {
347   auto Magic = identify_magic(ObjectBuffer.getBuffer());
348   switch (Magic) {
349   case file_magic::macho_object:
350     return createLinkGraphFromMachOObject(ObjectBuffer);
351   case file_magic::elf_relocatable:
352     return createLinkGraphFromELFObject(ObjectBuffer);
353   default:
354     return make_error<JITLinkError>("Unsupported file format");
355   };
356 }
357 
358 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
359   switch (G->getTargetTriple().getObjectFormat()) {
360   case Triple::MachO:
361     return link_MachO(std::move(G), std::move(Ctx));
362   case Triple::ELF:
363     return link_ELF(std::move(G), std::move(Ctx));
364   default:
365     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
366   };
367 }
368 
369 } // end namespace jitlink
370 } // end namespace llvm
371