1 //===------------ JITLink.h - JIT linker functionality ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Contains generic JIT-linker types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
15 
16 #include "JITLinkMemoryManager.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ExecutionEngine/JITSymbol.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FormatVariadic.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/Memory.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 
31 #include <map>
32 #include <string>
33 #include <system_error>
34 
35 namespace llvm {
36 namespace jitlink {
37 
38 class Symbol;
39 class Section;
40 
41 /// Base class for errors originating in JIT linker, e.g. missing relocation
42 /// support.
43 class JITLinkError : public ErrorInfo<JITLinkError> {
44 public:
45   static char ID;
46 
JITLinkError(Twine ErrMsg)47   JITLinkError(Twine ErrMsg) : ErrMsg(ErrMsg.str()) {}
48 
49   void log(raw_ostream &OS) const override;
getErrorMessage()50   const std::string &getErrorMessage() const { return ErrMsg; }
51   std::error_code convertToErrorCode() const override;
52 
53 private:
54   std::string ErrMsg;
55 };
56 
57 /// Represents fixups and constraints in the LinkGraph.
58 class Edge {
59 public:
60   using Kind = uint8_t;
61 
62   enum GenericEdgeKind : Kind {
63     Invalid,                    // Invalid edge value.
64     FirstKeepAlive,             // Keeps target alive. Offset/addend zero.
65     KeepAlive = FirstKeepAlive, // Tag first edge kind that preserves liveness.
66     FirstRelocation             // First architecture specific relocation.
67   };
68 
69   using OffsetT = uint32_t;
70   using AddendT = int64_t;
71 
Edge(Kind K,OffsetT Offset,Symbol & Target,AddendT Addend)72   Edge(Kind K, OffsetT Offset, Symbol &Target, AddendT Addend)
73       : Target(&Target), Offset(Offset), Addend(Addend), K(K) {}
74 
getOffset()75   OffsetT getOffset() const { return Offset; }
setOffset(OffsetT Offset)76   void setOffset(OffsetT Offset) { this->Offset = Offset; }
getKind()77   Kind getKind() const { return K; }
setKind(Kind K)78   void setKind(Kind K) { this->K = K; }
isRelocation()79   bool isRelocation() const { return K >= FirstRelocation; }
getRelocation()80   Kind getRelocation() const {
81     assert(isRelocation() && "Not a relocation edge");
82     return K - FirstRelocation;
83   }
isKeepAlive()84   bool isKeepAlive() const { return K >= FirstKeepAlive; }
getTarget()85   Symbol &getTarget() const { return *Target; }
setTarget(Symbol & Target)86   void setTarget(Symbol &Target) { this->Target = &Target; }
getAddend()87   AddendT getAddend() const { return Addend; }
setAddend(AddendT Addend)88   void setAddend(AddendT Addend) { this->Addend = Addend; }
89 
90 private:
91   Symbol *Target = nullptr;
92   OffsetT Offset = 0;
93   AddendT Addend = 0;
94   Kind K = 0;
95 };
96 
97 /// Returns the string name of the given generic edge kind, or "unknown"
98 /// otherwise. Useful for debugging.
99 const char *getGenericEdgeKindName(Edge::Kind K);
100 
101 /// Base class for Addressable entities (externals, absolutes, blocks).
102 class Addressable {
103   friend class LinkGraph;
104 
105 protected:
Addressable(JITTargetAddress Address,bool IsDefined)106   Addressable(JITTargetAddress Address, bool IsDefined)
107       : Address(Address), IsDefined(IsDefined), IsAbsolute(false) {}
108 
Addressable(JITTargetAddress Address)109   Addressable(JITTargetAddress Address)
110       : Address(Address), IsDefined(false), IsAbsolute(true) {
111     assert(!(IsDefined && IsAbsolute) &&
112            "Block cannot be both defined and absolute");
113   }
114 
115 public:
116   Addressable(const Addressable &) = delete;
117   Addressable &operator=(const Addressable &) = default;
118   Addressable(Addressable &&) = delete;
119   Addressable &operator=(Addressable &&) = default;
120 
getAddress()121   JITTargetAddress getAddress() const { return Address; }
setAddress(JITTargetAddress Address)122   void setAddress(JITTargetAddress Address) { this->Address = Address; }
123 
124   /// Returns true if this is a defined addressable, in which case you
125   /// can downcast this to a Block.
isDefined()126   bool isDefined() const { return static_cast<bool>(IsDefined); }
isAbsolute()127   bool isAbsolute() const { return static_cast<bool>(IsAbsolute); }
128 
129 private:
setAbsolute(bool IsAbsolute)130   void setAbsolute(bool IsAbsolute) {
131     assert(!IsDefined && "Cannot change the Absolute flag on a defined block");
132     this->IsAbsolute = IsAbsolute;
133   }
134 
135   JITTargetAddress Address = 0;
136   uint64_t IsDefined : 1;
137   uint64_t IsAbsolute : 1;
138 
139 protected:
140   // bitfields for Block, allocated here to improve packing.
141   uint64_t P2Align : 5;
142   uint64_t AlignmentOffset : 57;
143 };
144 
145 using SectionOrdinal = unsigned;
146 
147 /// An Addressable with content and edges.
148 class Block : public Addressable {
149   friend class LinkGraph;
150 
151 private:
152   /// Create a zero-fill defined addressable.
Block(Section & Parent,JITTargetAddress Size,JITTargetAddress Address,uint64_t Alignment,uint64_t AlignmentOffset)153   Block(Section &Parent, JITTargetAddress Size, JITTargetAddress Address,
154         uint64_t Alignment, uint64_t AlignmentOffset)
155       : Addressable(Address, true), Parent(Parent), Size(Size) {
156     assert(isPowerOf2_64(Alignment) && "Alignment must be power of 2");
157     assert(AlignmentOffset < Alignment &&
158            "Alignment offset cannot exceed alignment");
159     assert(AlignmentOffset <= MaxAlignmentOffset &&
160            "Alignment offset exceeds maximum");
161     P2Align = Alignment ? countTrailingZeros(Alignment) : 0;
162     this->AlignmentOffset = AlignmentOffset;
163   }
164 
165   /// Create a defined addressable for the given content.
Block(Section & Parent,ArrayRef<char> Content,JITTargetAddress Address,uint64_t Alignment,uint64_t AlignmentOffset)166   Block(Section &Parent, ArrayRef<char> Content, JITTargetAddress Address,
167         uint64_t Alignment, uint64_t AlignmentOffset)
168       : Addressable(Address, true), Parent(Parent), Data(Content.data()),
169         Size(Content.size()) {
170     assert(isPowerOf2_64(Alignment) && "Alignment must be power of 2");
171     assert(AlignmentOffset < Alignment &&
172            "Alignment offset cannot exceed alignment");
173     assert(AlignmentOffset <= MaxAlignmentOffset &&
174            "Alignment offset exceeds maximum");
175     P2Align = Alignment ? countTrailingZeros(Alignment) : 0;
176     this->AlignmentOffset = AlignmentOffset;
177   }
178 
179 public:
180   using EdgeVector = std::vector<Edge>;
181   using edge_iterator = EdgeVector::iterator;
182   using const_edge_iterator = EdgeVector::const_iterator;
183 
184   Block(const Block &) = delete;
185   Block &operator=(const Block &) = delete;
186   Block(Block &&) = delete;
187   Block &operator=(Block &&) = delete;
188 
189   /// Return the parent section for this block.
getSection()190   Section &getSection() const { return Parent; }
191 
192   /// Returns true if this is a zero-fill block.
193   ///
194   /// If true, getSize is callable but getContent is not (the content is
195   /// defined to be a sequence of zero bytes of length Size).
isZeroFill()196   bool isZeroFill() const { return !Data; }
197 
198   /// Returns the size of this defined addressable.
getSize()199   size_t getSize() const { return Size; }
200 
201   /// Get the content for this block. Block must not be a zero-fill block.
getContent()202   ArrayRef<char> getContent() const {
203     assert(Data && "Section does not contain content");
204     return ArrayRef<char>(Data, Size);
205   }
206 
207   /// Set the content for this block.
208   /// Caller is responsible for ensuring the underlying bytes are not
209   /// deallocated while pointed to by this block.
setContent(ArrayRef<char> Content)210   void setContent(ArrayRef<char> Content) {
211     Data = Content.data();
212     Size = Content.size();
213   }
214 
215   /// Get the alignment for this content.
getAlignment()216   uint64_t getAlignment() const { return 1ull << P2Align; }
217 
218   /// Set the alignment for this content.
setAlignment(uint64_t Alignment)219   void setAlignment(uint64_t Alignment) {
220     assert(isPowerOf2_64(Alignment) && "Alignment must be a power of two");
221     P2Align = Alignment ? countTrailingZeros(Alignment) : 0;
222   }
223 
224   /// Get the alignment offset for this content.
getAlignmentOffset()225   uint64_t getAlignmentOffset() const { return AlignmentOffset; }
226 
227   /// Set the alignment offset for this content.
setAlignmentOffset(uint64_t AlignmentOffset)228   void setAlignmentOffset(uint64_t AlignmentOffset) {
229     assert(AlignmentOffset < (1ull << P2Align) &&
230            "Alignment offset can't exceed alignment");
231     this->AlignmentOffset = AlignmentOffset;
232   }
233 
234   /// Add an edge to this block.
addEdge(Edge::Kind K,Edge::OffsetT Offset,Symbol & Target,Edge::AddendT Addend)235   void addEdge(Edge::Kind K, Edge::OffsetT Offset, Symbol &Target,
236                Edge::AddendT Addend) {
237     Edges.push_back(Edge(K, Offset, Target, Addend));
238   }
239 
240   /// Add an edge by copying an existing one. This is typically used when
241   /// moving edges between blocks.
addEdge(const Edge & E)242   void addEdge(const Edge &E) { Edges.push_back(E); }
243 
244   /// Return the list of edges attached to this content.
edges()245   iterator_range<edge_iterator> edges() {
246     return make_range(Edges.begin(), Edges.end());
247   }
248 
249   /// Returns the list of edges attached to this content.
edges()250   iterator_range<const_edge_iterator> edges() const {
251     return make_range(Edges.begin(), Edges.end());
252   }
253 
254   /// Return the size of the edges list.
edges_size()255   size_t edges_size() const { return Edges.size(); }
256 
257   /// Returns true if the list of edges is empty.
edges_empty()258   bool edges_empty() const { return Edges.empty(); }
259 
260   /// Remove the edge pointed to by the given iterator.
261   /// Returns an iterator to the new next element.
removeEdge(edge_iterator I)262   edge_iterator removeEdge(edge_iterator I) { return Edges.erase(I); }
263 
264   /// Returns the address of the fixup for the given edge, which is equal to
265   /// this block's address plus the edge's offset.
getFixupAddress(const Edge & E)266   JITTargetAddress getFixupAddress(const Edge &E) const {
267     return getAddress() + E.getOffset();
268   }
269 
270 private:
271   static constexpr uint64_t MaxAlignmentOffset = (1ULL << 57) - 1;
272 
273   Section &Parent;
274   const char *Data = nullptr;
275   size_t Size = 0;
276   std::vector<Edge> Edges;
277 };
278 
279 /// Describes symbol linkage. This can be used to make resolve definition
280 /// clashes.
281 enum class Linkage : uint8_t {
282   Strong,
283   Weak,
284 };
285 
286 /// For errors and debugging output.
287 const char *getLinkageName(Linkage L);
288 
289 /// Defines the scope in which this symbol should be visible:
290 ///   Default -- Visible in the public interface of the linkage unit.
291 ///   Hidden -- Visible within the linkage unit, but not exported from it.
292 ///   Local -- Visible only within the LinkGraph.
293 enum class Scope : uint8_t {
294   Default,
295   Hidden,
296   Local
297 };
298 
299 /// For debugging output.
300 const char *getScopeName(Scope S);
301 
302 raw_ostream &operator<<(raw_ostream &OS, const Block &B);
303 
304 /// Symbol representation.
305 ///
306 /// Symbols represent locations within Addressable objects.
307 /// They can be either Named or Anonymous.
308 /// Anonymous symbols have neither linkage nor visibility, and must point at
309 /// ContentBlocks.
310 /// Named symbols may be in one of four states:
311 ///   - Null: Default initialized. Assignable, but otherwise unusable.
312 ///   - Defined: Has both linkage and visibility and points to a ContentBlock
313 ///   - Common: Has both linkage and visibility, points to a null Addressable.
314 ///   - External: Has neither linkage nor visibility, points to an external
315 ///     Addressable.
316 ///
317 class Symbol {
318   friend class LinkGraph;
319 
320 private:
Symbol(Addressable & Base,JITTargetAddress Offset,StringRef Name,JITTargetAddress Size,Linkage L,Scope S,bool IsLive,bool IsCallable)321   Symbol(Addressable &Base, JITTargetAddress Offset, StringRef Name,
322          JITTargetAddress Size, Linkage L, Scope S, bool IsLive,
323          bool IsCallable)
324       : Name(Name), Base(&Base), Offset(Offset), Size(Size) {
325     assert(Offset <= MaxOffset && "Offset out of range");
326     setLinkage(L);
327     setScope(S);
328     setLive(IsLive);
329     setCallable(IsCallable);
330   }
331 
constructCommon(void * SymStorage,Block & Base,StringRef Name,JITTargetAddress Size,Scope S,bool IsLive)332   static Symbol &constructCommon(void *SymStorage, Block &Base, StringRef Name,
333                                  JITTargetAddress Size, Scope S, bool IsLive) {
334     assert(SymStorage && "Storage cannot be null");
335     assert(!Name.empty() && "Common symbol name cannot be empty");
336     assert(Base.isDefined() &&
337            "Cannot create common symbol from undefined block");
338     assert(static_cast<Block &>(Base).getSize() == Size &&
339            "Common symbol size should match underlying block size");
340     auto *Sym = reinterpret_cast<Symbol *>(SymStorage);
341     new (Sym) Symbol(Base, 0, Name, Size, Linkage::Weak, S, IsLive, false);
342     return *Sym;
343   }
344 
constructExternal(void * SymStorage,Addressable & Base,StringRef Name,JITTargetAddress Size,Linkage L)345   static Symbol &constructExternal(void *SymStorage, Addressable &Base,
346                                    StringRef Name, JITTargetAddress Size,
347                                    Linkage L) {
348     assert(SymStorage && "Storage cannot be null");
349     assert(!Base.isDefined() &&
350            "Cannot create external symbol from defined block");
351     assert(!Name.empty() && "External symbol name cannot be empty");
352     auto *Sym = reinterpret_cast<Symbol *>(SymStorage);
353     new (Sym) Symbol(Base, 0, Name, Size, L, Scope::Default, false, false);
354     return *Sym;
355   }
356 
constructAbsolute(void * SymStorage,Addressable & Base,StringRef Name,JITTargetAddress Size,Linkage L,Scope S,bool IsLive)357   static Symbol &constructAbsolute(void *SymStorage, Addressable &Base,
358                                    StringRef Name, JITTargetAddress Size,
359                                    Linkage L, Scope S, bool IsLive) {
360     assert(SymStorage && "Storage cannot be null");
361     assert(!Base.isDefined() &&
362            "Cannot create absolute symbol from a defined block");
363     auto *Sym = reinterpret_cast<Symbol *>(SymStorage);
364     new (Sym) Symbol(Base, 0, Name, Size, L, S, IsLive, false);
365     return *Sym;
366   }
367 
constructAnonDef(void * SymStorage,Block & Base,JITTargetAddress Offset,JITTargetAddress Size,bool IsCallable,bool IsLive)368   static Symbol &constructAnonDef(void *SymStorage, Block &Base,
369                                   JITTargetAddress Offset,
370                                   JITTargetAddress Size, bool IsCallable,
371                                   bool IsLive) {
372     assert(SymStorage && "Storage cannot be null");
373     assert((Offset + Size) <= Base.getSize() &&
374            "Symbol extends past end of block");
375     auto *Sym = reinterpret_cast<Symbol *>(SymStorage);
376     new (Sym) Symbol(Base, Offset, StringRef(), Size, Linkage::Strong,
377                      Scope::Local, IsLive, IsCallable);
378     return *Sym;
379   }
380 
constructNamedDef(void * SymStorage,Block & Base,JITTargetAddress Offset,StringRef Name,JITTargetAddress Size,Linkage L,Scope S,bool IsLive,bool IsCallable)381   static Symbol &constructNamedDef(void *SymStorage, Block &Base,
382                                    JITTargetAddress Offset, StringRef Name,
383                                    JITTargetAddress Size, Linkage L, Scope S,
384                                    bool IsLive, bool IsCallable) {
385     assert(SymStorage && "Storage cannot be null");
386     assert((Offset + Size) <= Base.getSize() &&
387            "Symbol extends past end of block");
388     assert(!Name.empty() && "Name cannot be empty");
389     auto *Sym = reinterpret_cast<Symbol *>(SymStorage);
390     new (Sym) Symbol(Base, Offset, Name, Size, L, S, IsLive, IsCallable);
391     return *Sym;
392   }
393 
394 public:
395   /// Create a null Symbol. This allows Symbols to be default initialized for
396   /// use in containers (e.g. as map values). Null symbols are only useful for
397   /// assigning to.
398   Symbol() = default;
399 
400   // Symbols are not movable or copyable.
401   Symbol(const Symbol &) = delete;
402   Symbol &operator=(const Symbol &) = delete;
403   Symbol(Symbol &&) = delete;
404   Symbol &operator=(Symbol &&) = delete;
405 
406   /// Returns true if this symbol has a name.
hasName()407   bool hasName() const { return !Name.empty(); }
408 
409   /// Returns the name of this symbol (empty if the symbol is anonymous).
getName()410   StringRef getName() const {
411     assert((!Name.empty() || getScope() == Scope::Local) &&
412            "Anonymous symbol has non-local scope");
413     return Name;
414   }
415 
416   /// Rename this symbol. The client is responsible for updating scope and
417   /// linkage if this name-change requires it.
setName(StringRef Name)418   void setName(StringRef Name) { this->Name = Name; }
419 
420   /// Returns true if this Symbol has content (potentially) defined within this
421   /// object file (i.e. is anything but an external or absolute symbol).
isDefined()422   bool isDefined() const {
423     assert(Base && "Attempt to access null symbol");
424     return Base->isDefined();
425   }
426 
427   /// Returns true if this symbol is live (i.e. should be treated as a root for
428   /// dead stripping).
isLive()429   bool isLive() const {
430     assert(Base && "Attempting to access null symbol");
431     return IsLive;
432   }
433 
434   /// Set this symbol's live bit.
setLive(bool IsLive)435   void setLive(bool IsLive) { this->IsLive = IsLive; }
436 
437   /// Returns true is this symbol is callable.
isCallable()438   bool isCallable() const { return IsCallable; }
439 
440   /// Set this symbol's callable bit.
setCallable(bool IsCallable)441   void setCallable(bool IsCallable) { this->IsCallable = IsCallable; }
442 
443   /// Returns true if the underlying addressable is an unresolved external.
isExternal()444   bool isExternal() const {
445     assert(Base && "Attempt to access null symbol");
446     return !Base->isDefined() && !Base->isAbsolute();
447   }
448 
449   /// Returns true if the underlying addressable is an absolute symbol.
isAbsolute()450   bool isAbsolute() const {
451     assert(Base && "Attempt to access null symbol");
452     return Base->isAbsolute();
453   }
454 
455   /// Return the addressable that this symbol points to.
getAddressable()456   Addressable &getAddressable() {
457     assert(Base && "Cannot get underlying addressable for null symbol");
458     return *Base;
459   }
460 
461   /// Return the addressable that thsi symbol points to.
getAddressable()462   const Addressable &getAddressable() const {
463     assert(Base && "Cannot get underlying addressable for null symbol");
464     return *Base;
465   }
466 
467   /// Return the Block for this Symbol (Symbol must be defined).
getBlock()468   Block &getBlock() {
469     assert(Base && "Cannot get block for null symbol");
470     assert(Base->isDefined() && "Not a defined symbol");
471     return static_cast<Block &>(*Base);
472   }
473 
474   /// Return the Block for this Symbol (Symbol must be defined).
getBlock()475   const Block &getBlock() const {
476     assert(Base && "Cannot get block for null symbol");
477     assert(Base->isDefined() && "Not a defined symbol");
478     return static_cast<const Block &>(*Base);
479   }
480 
481   /// Returns the offset for this symbol within the underlying addressable.
getOffset()482   JITTargetAddress getOffset() const { return Offset; }
483 
484   /// Returns the address of this symbol.
getAddress()485   JITTargetAddress getAddress() const { return Base->getAddress() + Offset; }
486 
487   /// Returns the size of this symbol.
getSize()488   JITTargetAddress getSize() const { return Size; }
489 
490   /// Set the size of this symbol.
setSize(JITTargetAddress Size)491   void setSize(JITTargetAddress Size) {
492     assert(Base && "Cannot set size for null Symbol");
493     assert((Size == 0 || Base->isDefined()) &&
494            "Non-zero size can only be set for defined symbols");
495     assert((Offset + Size <= static_cast<const Block &>(*Base).getSize()) &&
496            "Symbol size cannot extend past the end of its containing block");
497     this->Size = Size;
498   }
499 
500   /// Returns true if this symbol is backed by a zero-fill block.
501   /// This method may only be called on defined symbols.
isSymbolZeroFill()502   bool isSymbolZeroFill() const { return getBlock().isZeroFill(); }
503 
504   /// Returns the content in the underlying block covered by this symbol.
505   /// This method may only be called on defined non-zero-fill symbols.
getSymbolContent()506   ArrayRef<char> getSymbolContent() const {
507     return getBlock().getContent().slice(Offset, Size);
508   }
509 
510   /// Get the linkage for this Symbol.
getLinkage()511   Linkage getLinkage() const { return static_cast<Linkage>(L); }
512 
513   /// Set the linkage for this Symbol.
setLinkage(Linkage L)514   void setLinkage(Linkage L) {
515     assert((L == Linkage::Strong || (!Base->isAbsolute() && !Name.empty())) &&
516            "Linkage can only be applied to defined named symbols");
517     this->L = static_cast<uint8_t>(L);
518   }
519 
520   /// Get the visibility for this Symbol.
getScope()521   Scope getScope() const { return static_cast<Scope>(S); }
522 
523   /// Set the visibility for this Symbol.
setScope(Scope S)524   void setScope(Scope S) {
525     assert((!Name.empty() || S == Scope::Local) &&
526            "Can not set anonymous symbol to non-local scope");
527     assert((S == Scope::Default || Base->isDefined() || Base->isAbsolute()) &&
528            "Invalid visibility for symbol type");
529     this->S = static_cast<uint8_t>(S);
530   }
531 
532 private:
makeExternal(Addressable & A)533   void makeExternal(Addressable &A) {
534     assert(!A.isDefined() && !A.isAbsolute() &&
535            "Attempting to make external with defined or absolute block");
536     Base = &A;
537     Offset = 0;
538     setScope(Scope::Default);
539     IsLive = 0;
540     // note: Size, Linkage and IsCallable fields left unchanged.
541   }
542 
makeAbsolute(Addressable & A)543   void makeAbsolute(Addressable &A) {
544     assert(!A.isDefined() && A.isAbsolute() &&
545            "Attempting to make absolute with defined or external block");
546     Base = &A;
547     Offset = 0;
548   }
549 
setBlock(Block & B)550   void setBlock(Block &B) { Base = &B; }
551 
setOffset(uint64_t NewOffset)552   void setOffset(uint64_t NewOffset) {
553     assert(NewOffset <= MaxOffset && "Offset out of range");
554     Offset = NewOffset;
555   }
556 
557   static constexpr uint64_t MaxOffset = (1ULL << 59) - 1;
558 
559   // FIXME: A char* or SymbolStringPtr may pack better.
560   StringRef Name;
561   Addressable *Base = nullptr;
562   uint64_t Offset : 59;
563   uint64_t L : 1;
564   uint64_t S : 2;
565   uint64_t IsLive : 1;
566   uint64_t IsCallable : 1;
567   JITTargetAddress Size = 0;
568 };
569 
570 raw_ostream &operator<<(raw_ostream &OS, const Symbol &A);
571 
572 void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
573                StringRef EdgeKindName);
574 
575 /// Represents an object file section.
576 class Section {
577   friend class LinkGraph;
578 
579 private:
Section(StringRef Name,sys::Memory::ProtectionFlags Prot,SectionOrdinal SecOrdinal)580   Section(StringRef Name, sys::Memory::ProtectionFlags Prot,
581           SectionOrdinal SecOrdinal)
582       : Name(Name), Prot(Prot), SecOrdinal(SecOrdinal) {}
583 
584   using SymbolSet = DenseSet<Symbol *>;
585   using BlockSet = DenseSet<Block *>;
586 
587 public:
588   using symbol_iterator = SymbolSet::iterator;
589   using const_symbol_iterator = SymbolSet::const_iterator;
590 
591   using block_iterator = BlockSet::iterator;
592   using const_block_iterator = BlockSet::const_iterator;
593 
594   ~Section();
595 
596   // Sections are not movable or copyable.
597   Section(const Section &) = delete;
598   Section &operator=(const Section &) = delete;
599   Section(Section &&) = delete;
600   Section &operator=(Section &&) = delete;
601 
602   /// Returns the name of this section.
getName()603   StringRef getName() const { return Name; }
604 
605   /// Returns the protection flags for this section.
getProtectionFlags()606   sys::Memory::ProtectionFlags getProtectionFlags() const { return Prot; }
607 
608   /// Set the protection flags for this section.
setProtectionFlags(sys::Memory::ProtectionFlags Prot)609   void setProtectionFlags(sys::Memory::ProtectionFlags Prot) {
610     this->Prot = Prot;
611   }
612 
613   /// Returns the ordinal for this section.
getOrdinal()614   SectionOrdinal getOrdinal() const { return SecOrdinal; }
615 
616   /// Returns an iterator over the blocks defined in this section.
blocks()617   iterator_range<block_iterator> blocks() {
618     return make_range(Blocks.begin(), Blocks.end());
619   }
620 
621   /// Returns an iterator over the blocks defined in this section.
blocks()622   iterator_range<const_block_iterator> blocks() const {
623     return make_range(Blocks.begin(), Blocks.end());
624   }
625 
626   /// Returns an iterator over the symbols defined in this section.
symbols()627   iterator_range<symbol_iterator> symbols() {
628     return make_range(Symbols.begin(), Symbols.end());
629   }
630 
631   /// Returns an iterator over the symbols defined in this section.
symbols()632   iterator_range<const_symbol_iterator> symbols() const {
633     return make_range(Symbols.begin(), Symbols.end());
634   }
635 
636   /// Return the number of symbols in this section.
symbols_size()637   SymbolSet::size_type symbols_size() { return Symbols.size(); }
638 
639 private:
addSymbol(Symbol & Sym)640   void addSymbol(Symbol &Sym) {
641     assert(!Symbols.count(&Sym) && "Symbol is already in this section");
642     Symbols.insert(&Sym);
643   }
644 
removeSymbol(Symbol & Sym)645   void removeSymbol(Symbol &Sym) {
646     assert(Symbols.count(&Sym) && "symbol is not in this section");
647     Symbols.erase(&Sym);
648   }
649 
addBlock(Block & B)650   void addBlock(Block &B) {
651     assert(!Blocks.count(&B) && "Block is already in this section");
652     Blocks.insert(&B);
653   }
654 
removeBlock(Block & B)655   void removeBlock(Block &B) {
656     assert(Blocks.count(&B) && "Block is not in this section");
657     Blocks.erase(&B);
658   }
659 
660   StringRef Name;
661   sys::Memory::ProtectionFlags Prot;
662   SectionOrdinal SecOrdinal = 0;
663   BlockSet Blocks;
664   SymbolSet Symbols;
665 };
666 
667 /// Represents a section address range via a pair of Block pointers
668 /// to the first and last Blocks in the section.
669 class SectionRange {
670 public:
671   SectionRange() = default;
SectionRange(const Section & Sec)672   SectionRange(const Section &Sec) {
673     if (llvm::empty(Sec.blocks()))
674       return;
675     First = Last = *Sec.blocks().begin();
676     for (auto *B : Sec.blocks()) {
677       if (B->getAddress() < First->getAddress())
678         First = B;
679       if (B->getAddress() > Last->getAddress())
680         Last = B;
681     }
682   }
getFirstBlock()683   Block *getFirstBlock() const {
684     assert((!Last || First) && "First can not be null if end is non-null");
685     return First;
686   }
getLastBlock()687   Block *getLastBlock() const {
688     assert((First || !Last) && "Last can not be null if start is non-null");
689     return Last;
690   }
empty()691   bool empty() const {
692     assert((First || !Last) && "Last can not be null if start is non-null");
693     return !First;
694   }
getStart()695   JITTargetAddress getStart() const {
696     return First ? First->getAddress() : 0;
697   }
getEnd()698   JITTargetAddress getEnd() const {
699     return Last ? Last->getAddress() + Last->getSize() : 0;
700   }
getSize()701   uint64_t getSize() const { return getEnd() - getStart(); }
702 
703 private:
704   Block *First = nullptr;
705   Block *Last = nullptr;
706 };
707 
708 class LinkGraph {
709 private:
710   using SectionList = std::vector<std::unique_ptr<Section>>;
711   using ExternalSymbolSet = DenseSet<Symbol *>;
712   using BlockSet = DenseSet<Block *>;
713 
714   template <typename... ArgTs>
createAddressable(ArgTs &&...Args)715   Addressable &createAddressable(ArgTs &&... Args) {
716     Addressable *A =
717         reinterpret_cast<Addressable *>(Allocator.Allocate<Addressable>());
718     new (A) Addressable(std::forward<ArgTs>(Args)...);
719     return *A;
720   }
721 
destroyAddressable(Addressable & A)722   void destroyAddressable(Addressable &A) {
723     A.~Addressable();
724     Allocator.Deallocate(&A);
725   }
726 
createBlock(ArgTs &&...Args)727   template <typename... ArgTs> Block &createBlock(ArgTs &&... Args) {
728     Block *B = reinterpret_cast<Block *>(Allocator.Allocate<Block>());
729     new (B) Block(std::forward<ArgTs>(Args)...);
730     B->getSection().addBlock(*B);
731     return *B;
732   }
733 
destroyBlock(Block & B)734   void destroyBlock(Block &B) {
735     B.~Block();
736     Allocator.Deallocate(&B);
737   }
738 
destroySymbol(Symbol & S)739   void destroySymbol(Symbol &S) {
740     S.~Symbol();
741     Allocator.Deallocate(&S);
742   }
743 
getSectionBlocks(Section & S)744   static iterator_range<Section::block_iterator> getSectionBlocks(Section &S) {
745     return S.blocks();
746   }
747 
748   static iterator_range<Section::const_block_iterator>
getSectionConstBlocks(Section & S)749   getSectionConstBlocks(Section &S) {
750     return S.blocks();
751   }
752 
753   static iterator_range<Section::symbol_iterator>
getSectionSymbols(Section & S)754   getSectionSymbols(Section &S) {
755     return S.symbols();
756   }
757 
758   static iterator_range<Section::const_symbol_iterator>
getSectionConstSymbols(Section & S)759   getSectionConstSymbols(Section &S) {
760     return S.symbols();
761   }
762 
763 public:
764   using external_symbol_iterator = ExternalSymbolSet::iterator;
765 
766   using section_iterator = pointee_iterator<SectionList::iterator>;
767   using const_section_iterator = pointee_iterator<SectionList::const_iterator>;
768 
769   template <typename OuterItrT, typename InnerItrT, typename T,
770             iterator_range<InnerItrT> getInnerRange(
771                 typename OuterItrT::reference)>
772   class nested_collection_iterator
773       : public iterator_facade_base<
774             nested_collection_iterator<OuterItrT, InnerItrT, T, getInnerRange>,
775             std::forward_iterator_tag, T> {
776   public:
777     nested_collection_iterator() = default;
778 
nested_collection_iterator(OuterItrT OuterI,OuterItrT OuterE)779     nested_collection_iterator(OuterItrT OuterI, OuterItrT OuterE)
780         : OuterI(OuterI), OuterE(OuterE),
781           InnerI(getInnerBegin(OuterI, OuterE)) {
782       moveToNonEmptyInnerOrEnd();
783     }
784 
785     bool operator==(const nested_collection_iterator &RHS) const {
786       return (OuterI == RHS.OuterI) && (InnerI == RHS.InnerI);
787     }
788 
789     T operator*() const {
790       assert(InnerI != getInnerRange(*OuterI).end() && "Dereferencing end?");
791       return *InnerI;
792     }
793 
794     nested_collection_iterator operator++() {
795       ++InnerI;
796       moveToNonEmptyInnerOrEnd();
797       return *this;
798     }
799 
800   private:
getInnerBegin(OuterItrT OuterI,OuterItrT OuterE)801     static InnerItrT getInnerBegin(OuterItrT OuterI, OuterItrT OuterE) {
802       return OuterI != OuterE ? getInnerRange(*OuterI).begin() : InnerItrT();
803     }
804 
moveToNonEmptyInnerOrEnd()805     void moveToNonEmptyInnerOrEnd() {
806       while (OuterI != OuterE && InnerI == getInnerRange(*OuterI).end()) {
807         ++OuterI;
808         InnerI = getInnerBegin(OuterI, OuterE);
809       }
810     }
811 
812     OuterItrT OuterI, OuterE;
813     InnerItrT InnerI;
814   };
815 
816   using defined_symbol_iterator =
817       nested_collection_iterator<const_section_iterator,
818                                  Section::symbol_iterator, Symbol *,
819                                  getSectionSymbols>;
820 
821   using const_defined_symbol_iterator =
822       nested_collection_iterator<const_section_iterator,
823                                  Section::const_symbol_iterator, const Symbol *,
824                                  getSectionConstSymbols>;
825 
826   using block_iterator = nested_collection_iterator<const_section_iterator,
827                                                     Section::block_iterator,
828                                                     Block *, getSectionBlocks>;
829 
830   using const_block_iterator =
831       nested_collection_iterator<const_section_iterator,
832                                  Section::const_block_iterator, const Block *,
833                                  getSectionConstBlocks>;
834 
835   using GetEdgeKindNameFunction = const char *(*)(Edge::Kind);
836 
LinkGraph(std::string Name,const Triple & TT,unsigned PointerSize,support::endianness Endianness,GetEdgeKindNameFunction GetEdgeKindName)837   LinkGraph(std::string Name, const Triple &TT, unsigned PointerSize,
838             support::endianness Endianness,
839             GetEdgeKindNameFunction GetEdgeKindName)
840       : Name(std::move(Name)), TT(TT), PointerSize(PointerSize),
841         Endianness(Endianness), GetEdgeKindName(std::move(GetEdgeKindName)) {}
842 
843   /// Returns the name of this graph (usually the name of the original
844   /// underlying MemoryBuffer).
getName()845   const std::string &getName() const { return Name; }
846 
847   /// Returns the target triple for this Graph.
getTargetTriple()848   const Triple &getTargetTriple() const { return TT; }
849 
850   /// Returns the pointer size for use in this graph.
getPointerSize()851   unsigned getPointerSize() const { return PointerSize; }
852 
853   /// Returns the endianness of content in this graph.
getEndianness()854   support::endianness getEndianness() const { return Endianness; }
855 
getEdgeKindName(Edge::Kind K)856   const char *getEdgeKindName(Edge::Kind K) const { return GetEdgeKindName(K); }
857 
858   /// Allocate a copy of the given string using the LinkGraph's allocator.
859   /// This can be useful when renaming symbols or adding new content to the
860   /// graph.
allocateString(ArrayRef<char> Source)861   ArrayRef<char> allocateString(ArrayRef<char> Source) {
862     auto *AllocatedBuffer = Allocator.Allocate<char>(Source.size());
863     llvm::copy(Source, AllocatedBuffer);
864     return ArrayRef<char>(AllocatedBuffer, Source.size());
865   }
866 
867   /// Allocate a copy of the given string using the LinkGraph's allocator.
868   /// This can be useful when renaming symbols or adding new content to the
869   /// graph.
870   ///
871   /// Note: This Twine-based overload requires an extra string copy and an
872   /// extra heap allocation for large strings. The ArrayRef<char> overload
873   /// should be preferred where possible.
allocateString(Twine Source)874   ArrayRef<char> allocateString(Twine Source) {
875     SmallString<256> TmpBuffer;
876     auto SourceStr = Source.toStringRef(TmpBuffer);
877     auto *AllocatedBuffer = Allocator.Allocate<char>(SourceStr.size());
878     llvm::copy(SourceStr, AllocatedBuffer);
879     return ArrayRef<char>(AllocatedBuffer, SourceStr.size());
880   }
881 
882   /// Create a section with the given name, protection flags, and alignment.
createSection(StringRef Name,sys::Memory::ProtectionFlags Prot)883   Section &createSection(StringRef Name, sys::Memory::ProtectionFlags Prot) {
884     assert(llvm::find_if(Sections,
885                          [&](std::unique_ptr<Section> &Sec) {
886                            return Sec->getName() == Name;
887                          }) == Sections.end() &&
888            "Duplicate section name");
889     std::unique_ptr<Section> Sec(new Section(Name, Prot, Sections.size()));
890     Sections.push_back(std::move(Sec));
891     return *Sections.back();
892   }
893 
894   /// Create a content block.
createContentBlock(Section & Parent,ArrayRef<char> Content,uint64_t Address,uint64_t Alignment,uint64_t AlignmentOffset)895   Block &createContentBlock(Section &Parent, ArrayRef<char> Content,
896                             uint64_t Address, uint64_t Alignment,
897                             uint64_t AlignmentOffset) {
898     return createBlock(Parent, Content, Address, Alignment, AlignmentOffset);
899   }
900 
901   /// Create a zero-fill block.
createZeroFillBlock(Section & Parent,uint64_t Size,uint64_t Address,uint64_t Alignment,uint64_t AlignmentOffset)902   Block &createZeroFillBlock(Section &Parent, uint64_t Size, uint64_t Address,
903                              uint64_t Alignment, uint64_t AlignmentOffset) {
904     return createBlock(Parent, Size, Address, Alignment, AlignmentOffset);
905   }
906 
907   /// Cache type for the splitBlock function.
908   using SplitBlockCache = Optional<SmallVector<Symbol *, 8>>;
909 
910   /// Splits block B at the given index which must be greater than zero.
911   /// If SplitIndex == B.getSize() then this function is a no-op and returns B.
912   /// If SplitIndex < B.getSize() then this function returns a new block
913   /// covering the range [ 0, SplitIndex ), and B is modified to cover the range
914   /// [ SplitIndex, B.size() ).
915   ///
916   /// The optional Cache parameter can be used to speed up repeated calls to
917   /// splitBlock for a single block. If the value is None the cache will be
918   /// treated as uninitialized and splitBlock will populate it. Otherwise it
919   /// is assumed to contain the list of Symbols pointing at B, sorted in
920   /// descending order of offset.
921   ///
922   /// Notes:
923   ///
924   /// 1. The newly introduced block will have a new ordinal which will be
925   ///    higher than any other ordinals in the section. Clients are responsible
926   ///    for re-assigning block ordinals to restore a compatible order if
927   ///    needed.
928   ///
929   /// 2. The cache is not automatically updated if new symbols are introduced
930   ///    between calls to splitBlock. Any newly introduced symbols may be
931   ///    added to the cache manually (descending offset order must be
932   ///    preserved), or the cache can be set to None and rebuilt by
933   ///    splitBlock on the next call.
934   Block &splitBlock(Block &B, size_t SplitIndex,
935                     SplitBlockCache *Cache = nullptr);
936 
937   /// Add an external symbol.
938   /// Some formats (e.g. ELF) allow Symbols to have sizes. For Symbols whose
939   /// size is not known, you should substitute '0'.
940   /// For external symbols Linkage determines whether the symbol must be
941   /// present during lookup: Externals with strong linkage must be found or
942   /// an error will be emitted. Externals with weak linkage are permitted to
943   /// be undefined, in which case they are assigned a value of 0.
addExternalSymbol(StringRef Name,uint64_t Size,Linkage L)944   Symbol &addExternalSymbol(StringRef Name, uint64_t Size, Linkage L) {
945     assert(llvm::count_if(ExternalSymbols,
946                           [&](const Symbol *Sym) {
947                             return Sym->getName() == Name;
948                           }) == 0 &&
949            "Duplicate external symbol");
950     auto &Sym =
951         Symbol::constructExternal(Allocator.Allocate<Symbol>(),
952                                   createAddressable(0, false), Name, Size, L);
953     ExternalSymbols.insert(&Sym);
954     return Sym;
955   }
956 
957   /// Add an absolute symbol.
addAbsoluteSymbol(StringRef Name,JITTargetAddress Address,uint64_t Size,Linkage L,Scope S,bool IsLive)958   Symbol &addAbsoluteSymbol(StringRef Name, JITTargetAddress Address,
959                             uint64_t Size, Linkage L, Scope S, bool IsLive) {
960     assert(llvm::count_if(AbsoluteSymbols,
961                           [&](const Symbol *Sym) {
962                             return Sym->getName() == Name;
963                           }) == 0 &&
964            "Duplicate absolute symbol");
965     auto &Sym = Symbol::constructAbsolute(Allocator.Allocate<Symbol>(),
966                                           createAddressable(Address), Name,
967                                           Size, L, S, IsLive);
968     AbsoluteSymbols.insert(&Sym);
969     return Sym;
970   }
971 
972   /// Convenience method for adding a weak zero-fill symbol.
addCommonSymbol(StringRef Name,Scope S,Section & Section,JITTargetAddress Address,uint64_t Size,uint64_t Alignment,bool IsLive)973   Symbol &addCommonSymbol(StringRef Name, Scope S, Section &Section,
974                           JITTargetAddress Address, uint64_t Size,
975                           uint64_t Alignment, bool IsLive) {
976     assert(llvm::count_if(defined_symbols(),
977                           [&](const Symbol *Sym) {
978                             return Sym->getName() == Name;
979                           }) == 0 &&
980            "Duplicate defined symbol");
981     auto &Sym = Symbol::constructCommon(
982         Allocator.Allocate<Symbol>(),
983         createBlock(Section, Size, Address, Alignment, 0), Name, Size, S,
984         IsLive);
985     Section.addSymbol(Sym);
986     return Sym;
987   }
988 
989   /// Add an anonymous symbol.
addAnonymousSymbol(Block & Content,JITTargetAddress Offset,JITTargetAddress Size,bool IsCallable,bool IsLive)990   Symbol &addAnonymousSymbol(Block &Content, JITTargetAddress Offset,
991                              JITTargetAddress Size, bool IsCallable,
992                              bool IsLive) {
993     auto &Sym = Symbol::constructAnonDef(Allocator.Allocate<Symbol>(), Content,
994                                          Offset, Size, IsCallable, IsLive);
995     Content.getSection().addSymbol(Sym);
996     return Sym;
997   }
998 
999   /// Add a named symbol.
addDefinedSymbol(Block & Content,JITTargetAddress Offset,StringRef Name,JITTargetAddress Size,Linkage L,Scope S,bool IsCallable,bool IsLive)1000   Symbol &addDefinedSymbol(Block &Content, JITTargetAddress Offset,
1001                            StringRef Name, JITTargetAddress Size, Linkage L,
1002                            Scope S, bool IsCallable, bool IsLive) {
1003     assert(llvm::count_if(defined_symbols(),
1004                           [&](const Symbol *Sym) {
1005                             return Sym->getName() == Name;
1006                           }) == 0 &&
1007            "Duplicate defined symbol");
1008     auto &Sym =
1009         Symbol::constructNamedDef(Allocator.Allocate<Symbol>(), Content, Offset,
1010                                   Name, Size, L, S, IsLive, IsCallable);
1011     Content.getSection().addSymbol(Sym);
1012     return Sym;
1013   }
1014 
sections()1015   iterator_range<section_iterator> sections() {
1016     return make_range(section_iterator(Sections.begin()),
1017                       section_iterator(Sections.end()));
1018   }
1019 
1020   /// Returns the section with the given name if it exists, otherwise returns
1021   /// null.
findSectionByName(StringRef Name)1022   Section *findSectionByName(StringRef Name) {
1023     for (auto &S : sections())
1024       if (S.getName() == Name)
1025         return &S;
1026     return nullptr;
1027   }
1028 
blocks()1029   iterator_range<block_iterator> blocks() {
1030     return make_range(block_iterator(Sections.begin(), Sections.end()),
1031                       block_iterator(Sections.end(), Sections.end()));
1032   }
1033 
blocks()1034   iterator_range<const_block_iterator> blocks() const {
1035     return make_range(const_block_iterator(Sections.begin(), Sections.end()),
1036                       const_block_iterator(Sections.end(), Sections.end()));
1037   }
1038 
external_symbols()1039   iterator_range<external_symbol_iterator> external_symbols() {
1040     return make_range(ExternalSymbols.begin(), ExternalSymbols.end());
1041   }
1042 
absolute_symbols()1043   iterator_range<external_symbol_iterator> absolute_symbols() {
1044     return make_range(AbsoluteSymbols.begin(), AbsoluteSymbols.end());
1045   }
1046 
defined_symbols()1047   iterator_range<defined_symbol_iterator> defined_symbols() {
1048     return make_range(defined_symbol_iterator(Sections.begin(), Sections.end()),
1049                       defined_symbol_iterator(Sections.end(), Sections.end()));
1050   }
1051 
defined_symbols()1052   iterator_range<const_defined_symbol_iterator> defined_symbols() const {
1053     return make_range(
1054         const_defined_symbol_iterator(Sections.begin(), Sections.end()),
1055         const_defined_symbol_iterator(Sections.end(), Sections.end()));
1056   }
1057 
1058   /// Make the given symbol external (must not already be external).
1059   ///
1060   /// Symbol size, linkage and callability will be left unchanged. Symbol scope
1061   /// will be set to Default, and offset will be reset to 0.
makeExternal(Symbol & Sym)1062   void makeExternal(Symbol &Sym) {
1063     assert(!Sym.isExternal() && "Symbol is already external");
1064     if (Sym.isAbsolute()) {
1065       assert(AbsoluteSymbols.count(&Sym) &&
1066              "Sym is not in the absolute symbols set");
1067       assert(Sym.getOffset() == 0 && "Absolute not at offset 0");
1068       AbsoluteSymbols.erase(&Sym);
1069       Sym.getAddressable().setAbsolute(false);
1070     } else {
1071       assert(Sym.isDefined() && "Sym is not a defined symbol");
1072       Section &Sec = Sym.getBlock().getSection();
1073       Sec.removeSymbol(Sym);
1074       Sym.makeExternal(createAddressable(0, false));
1075     }
1076     ExternalSymbols.insert(&Sym);
1077   }
1078 
1079   /// Make the given symbol an absolute with the given address (must not already
1080   /// be absolute).
1081   ///
1082   /// Symbol size, linkage, scope, and callability, and liveness will be left
1083   /// unchanged. Symbol offset will be reset to 0.
makeAbsolute(Symbol & Sym,JITTargetAddress Address)1084   void makeAbsolute(Symbol &Sym, JITTargetAddress Address) {
1085     assert(!Sym.isAbsolute() && "Symbol is already absolute");
1086     if (Sym.isExternal()) {
1087       assert(ExternalSymbols.count(&Sym) &&
1088              "Sym is not in the absolute symbols set");
1089       assert(Sym.getOffset() == 0 && "External is not at offset 0");
1090       ExternalSymbols.erase(&Sym);
1091       Sym.getAddressable().setAbsolute(true);
1092     } else {
1093       assert(Sym.isDefined() && "Sym is not a defined symbol");
1094       Section &Sec = Sym.getBlock().getSection();
1095       Sec.removeSymbol(Sym);
1096       Sym.makeAbsolute(createAddressable(Address));
1097     }
1098     AbsoluteSymbols.insert(&Sym);
1099   }
1100 
1101   /// Turn an absolute or external symbol into a defined one by attaching it to
1102   /// a block. Symbol must not already be defined.
makeDefined(Symbol & Sym,Block & Content,JITTargetAddress Offset,JITTargetAddress Size,Linkage L,Scope S,bool IsLive)1103   void makeDefined(Symbol &Sym, Block &Content, JITTargetAddress Offset,
1104                    JITTargetAddress Size, Linkage L, Scope S, bool IsLive) {
1105     assert(!Sym.isDefined() && "Sym is already a defined symbol");
1106     if (Sym.isAbsolute()) {
1107       assert(AbsoluteSymbols.count(&Sym) &&
1108              "Symbol is not in the absolutes set");
1109       AbsoluteSymbols.erase(&Sym);
1110     } else {
1111       assert(ExternalSymbols.count(&Sym) &&
1112              "Symbol is not in the externals set");
1113       ExternalSymbols.erase(&Sym);
1114     }
1115     Addressable &OldBase = *Sym.Base;
1116     Sym.setBlock(Content);
1117     Sym.setOffset(Offset);
1118     Sym.setSize(Size);
1119     Sym.setLinkage(L);
1120     Sym.setScope(S);
1121     Sym.setLive(IsLive);
1122     Content.getSection().addSymbol(Sym);
1123     destroyAddressable(OldBase);
1124   }
1125 
1126   /// Transfer a defined symbol from one block to another.
1127   ///
1128   /// The symbol's offset within DestBlock is set to NewOffset.
1129   ///
1130   /// If ExplicitNewSize is given as None then the size of the symbol will be
1131   /// checked and auto-truncated to at most the size of the remainder (from the
1132   /// given offset) of the size of the new block.
1133   ///
1134   /// All other symbol attributes are unchanged.
transferDefinedSymbol(Symbol & Sym,Block & DestBlock,JITTargetAddress NewOffset,Optional<JITTargetAddress> ExplicitNewSize)1135   void transferDefinedSymbol(Symbol &Sym, Block &DestBlock,
1136                              JITTargetAddress NewOffset,
1137                              Optional<JITTargetAddress> ExplicitNewSize) {
1138     Sym.setBlock(DestBlock);
1139     Sym.setOffset(NewOffset);
1140     if (ExplicitNewSize)
1141       Sym.setSize(*ExplicitNewSize);
1142     else {
1143       JITTargetAddress RemainingBlockSize = DestBlock.getSize() - NewOffset;
1144       if (Sym.getSize() > RemainingBlockSize)
1145         Sym.setSize(RemainingBlockSize);
1146     }
1147   }
1148 
1149   /// Removes an external symbol. Also removes the underlying Addressable.
removeExternalSymbol(Symbol & Sym)1150   void removeExternalSymbol(Symbol &Sym) {
1151     assert(!Sym.isDefined() && !Sym.isAbsolute() &&
1152            "Sym is not an external symbol");
1153     assert(ExternalSymbols.count(&Sym) && "Symbol is not in the externals set");
1154     ExternalSymbols.erase(&Sym);
1155     Addressable &Base = *Sym.Base;
1156     assert(llvm::find_if(ExternalSymbols,
1157                          [&](Symbol *AS) { return AS->Base == &Base; }) ==
1158                ExternalSymbols.end() &&
1159            "Base addressable still in use");
1160     destroySymbol(Sym);
1161     destroyAddressable(Base);
1162   }
1163 
1164   /// Remove an absolute symbol. Also removes the underlying Addressable.
removeAbsoluteSymbol(Symbol & Sym)1165   void removeAbsoluteSymbol(Symbol &Sym) {
1166     assert(!Sym.isDefined() && Sym.isAbsolute() &&
1167            "Sym is not an absolute symbol");
1168     assert(AbsoluteSymbols.count(&Sym) &&
1169            "Symbol is not in the absolute symbols set");
1170     AbsoluteSymbols.erase(&Sym);
1171     Addressable &Base = *Sym.Base;
1172     assert(llvm::find_if(ExternalSymbols,
1173                          [&](Symbol *AS) { return AS->Base == &Base; }) ==
1174                ExternalSymbols.end() &&
1175            "Base addressable still in use");
1176     destroySymbol(Sym);
1177     destroyAddressable(Base);
1178   }
1179 
1180   /// Removes defined symbols. Does not remove the underlying block.
removeDefinedSymbol(Symbol & Sym)1181   void removeDefinedSymbol(Symbol &Sym) {
1182     assert(Sym.isDefined() && "Sym is not a defined symbol");
1183     Sym.getBlock().getSection().removeSymbol(Sym);
1184     destroySymbol(Sym);
1185   }
1186 
1187   /// Remove a block.
removeBlock(Block & B)1188   void removeBlock(Block &B) {
1189     assert(llvm::none_of(B.getSection().symbols(),
1190                          [&](const Symbol *Sym) {
1191                            return &Sym->getBlock() == &B;
1192                          }) &&
1193            "Block still has symbols attached");
1194     B.getSection().removeBlock(B);
1195     destroyBlock(B);
1196   }
1197 
1198   /// Dump the graph.
1199   void dump(raw_ostream &OS);
1200 
1201 private:
1202   // Put the BumpPtrAllocator first so that we don't free any of the underlying
1203   // memory until the Symbol/Addressable destructors have been run.
1204   BumpPtrAllocator Allocator;
1205 
1206   std::string Name;
1207   Triple TT;
1208   unsigned PointerSize;
1209   support::endianness Endianness;
1210   GetEdgeKindNameFunction GetEdgeKindName = nullptr;
1211   SectionList Sections;
1212   ExternalSymbolSet ExternalSymbols;
1213   ExternalSymbolSet AbsoluteSymbols;
1214 };
1215 
1216 /// Enables easy lookup of blocks by addresses.
1217 class BlockAddressMap {
1218 public:
1219   using AddrToBlockMap = std::map<JITTargetAddress, Block *>;
1220   using const_iterator = AddrToBlockMap::const_iterator;
1221 
1222   /// A block predicate that always adds all blocks.
includeAllBlocks(const Block & B)1223   static bool includeAllBlocks(const Block &B) { return true; }
1224 
1225   /// A block predicate that always includes blocks with non-null addresses.
includeNonNull(const Block & B)1226   static bool includeNonNull(const Block &B) { return B.getAddress(); }
1227 
1228   BlockAddressMap() = default;
1229 
1230   /// Add a block to the map. Returns an error if the block overlaps with any
1231   /// existing block.
1232   template <typename PredFn = decltype(includeAllBlocks)>
1233   Error addBlock(Block &B, PredFn Pred = includeAllBlocks) {
1234     if (!Pred(B))
1235       return Error::success();
1236 
1237     auto I = AddrToBlock.upper_bound(B.getAddress());
1238 
1239     // If we're not at the end of the map, check for overlap with the next
1240     // element.
1241     if (I != AddrToBlock.end()) {
1242       if (B.getAddress() + B.getSize() > I->second->getAddress())
1243         return overlapError(B, *I->second);
1244     }
1245 
1246     // If we're not at the start of the map, check for overlap with the previous
1247     // element.
1248     if (I != AddrToBlock.begin()) {
1249       auto &PrevBlock = *std::prev(I)->second;
1250       if (PrevBlock.getAddress() + PrevBlock.getSize() > B.getAddress())
1251         return overlapError(B, PrevBlock);
1252     }
1253 
1254     AddrToBlock.insert(I, std::make_pair(B.getAddress(), &B));
1255     return Error::success();
1256   }
1257 
1258   /// Add a block to the map without checking for overlap with existing blocks.
1259   /// The client is responsible for ensuring that the block added does not
1260   /// overlap with any existing block.
addBlockWithoutChecking(Block & B)1261   void addBlockWithoutChecking(Block &B) { AddrToBlock[B.getAddress()] = &B; }
1262 
1263   /// Add a range of blocks to the map. Returns an error if any block in the
1264   /// range overlaps with any other block in the range, or with any existing
1265   /// block in the map.
1266   template <typename BlockPtrRange,
1267             typename PredFn = decltype(includeAllBlocks)>
1268   Error addBlocks(BlockPtrRange &&Blocks, PredFn Pred = includeAllBlocks) {
1269     for (auto *B : Blocks)
1270       if (auto Err = addBlock(*B, Pred))
1271         return Err;
1272     return Error::success();
1273   }
1274 
1275   /// Add a range of blocks to the map without checking for overlap with
1276   /// existing blocks. The client is responsible for ensuring that the block
1277   /// added does not overlap with any existing block.
1278   template <typename BlockPtrRange>
addBlocksWithoutChecking(BlockPtrRange && Blocks)1279   void addBlocksWithoutChecking(BlockPtrRange &&Blocks) {
1280     for (auto *B : Blocks)
1281       addBlockWithoutChecking(*B);
1282   }
1283 
1284   /// Iterates over (Address, Block*) pairs in ascending order of address.
begin()1285   const_iterator begin() const { return AddrToBlock.begin(); }
end()1286   const_iterator end() const { return AddrToBlock.end(); }
1287 
1288   /// Returns the block starting at the given address, or nullptr if no such
1289   /// block exists.
getBlockAt(JITTargetAddress Addr)1290   Block *getBlockAt(JITTargetAddress Addr) const {
1291     auto I = AddrToBlock.find(Addr);
1292     if (I == AddrToBlock.end())
1293       return nullptr;
1294     return I->second;
1295   }
1296 
1297   /// Returns the block covering the given address, or nullptr if no such block
1298   /// exists.
getBlockCovering(JITTargetAddress Addr)1299   Block *getBlockCovering(JITTargetAddress Addr) const {
1300     auto I = AddrToBlock.upper_bound(Addr);
1301     if (I == AddrToBlock.begin())
1302       return nullptr;
1303     auto *B = std::prev(I)->second;
1304     if (Addr < B->getAddress() + B->getSize())
1305       return B;
1306     return nullptr;
1307   }
1308 
1309 private:
overlapError(Block & NewBlock,Block & ExistingBlock)1310   Error overlapError(Block &NewBlock, Block &ExistingBlock) {
1311     auto NewBlockEnd = NewBlock.getAddress() + NewBlock.getSize();
1312     auto ExistingBlockEnd =
1313         ExistingBlock.getAddress() + ExistingBlock.getSize();
1314     return make_error<JITLinkError>(
1315         "Block at " +
1316         formatv("{0:x16} -- {1:x16}", NewBlock.getAddress(), NewBlockEnd) +
1317         " overlaps " +
1318         formatv("{0:x16} -- {1:x16}", ExistingBlock.getAddress(),
1319                 ExistingBlockEnd));
1320   }
1321 
1322   AddrToBlockMap AddrToBlock;
1323 };
1324 
1325 /// A map of addresses to Symbols.
1326 class SymbolAddressMap {
1327 public:
1328   using SymbolVector = SmallVector<Symbol *, 1>;
1329 
1330   /// Add a symbol to the SymbolAddressMap.
addSymbol(Symbol & Sym)1331   void addSymbol(Symbol &Sym) {
1332     AddrToSymbols[Sym.getAddress()].push_back(&Sym);
1333   }
1334 
1335   /// Add all symbols in a given range to the SymbolAddressMap.
1336   template <typename SymbolPtrCollection>
addSymbols(SymbolPtrCollection && Symbols)1337   void addSymbols(SymbolPtrCollection &&Symbols) {
1338     for (auto *Sym : Symbols)
1339       addSymbol(*Sym);
1340   }
1341 
1342   /// Returns the list of symbols that start at the given address, or nullptr if
1343   /// no such symbols exist.
getSymbolsAt(JITTargetAddress Addr)1344   const SymbolVector *getSymbolsAt(JITTargetAddress Addr) const {
1345     auto I = AddrToSymbols.find(Addr);
1346     if (I == AddrToSymbols.end())
1347       return nullptr;
1348     return &I->second;
1349   }
1350 
1351 private:
1352   std::map<JITTargetAddress, SymbolVector> AddrToSymbols;
1353 };
1354 
1355 /// A function for mutating LinkGraphs.
1356 using LinkGraphPassFunction = std::function<Error(LinkGraph &)>;
1357 
1358 /// A list of LinkGraph passes.
1359 using LinkGraphPassList = std::vector<LinkGraphPassFunction>;
1360 
1361 /// An LinkGraph pass configuration, consisting of a list of pre-prune,
1362 /// post-prune, and post-fixup passes.
1363 struct PassConfiguration {
1364 
1365   /// Pre-prune passes.
1366   ///
1367   /// These passes are called on the graph after it is built, and before any
1368   /// symbols have been pruned. Graph nodes still have their original vmaddrs.
1369   ///
1370   /// Notable use cases: Marking symbols live or should-discard.
1371   LinkGraphPassList PrePrunePasses;
1372 
1373   /// Post-prune passes.
1374   ///
1375   /// These passes are called on the graph after dead stripping, but before
1376   /// memory is allocated or nodes assigned their final addresses.
1377   ///
1378   /// Notable use cases: Building GOT, stub, and TLV symbols.
1379   LinkGraphPassList PostPrunePasses;
1380 
1381   /// Post-allocation passes.
1382   ///
1383   /// These passes are called on the graph after memory has been allocated and
1384   /// defined nodes have been assigned their final addresses, but before the
1385   /// context has been notified of these addresses. At this point externals
1386   /// have not been resolved, and symbol content has not yet been copied into
1387   /// working memory.
1388   ///
1389   /// Notable use cases: Setting up data structures associated with addresses
1390   /// of defined symbols (e.g. a mapping of __dso_handle to JITDylib* for the
1391   /// JIT runtime) -- using a PostAllocationPass for this ensures that the
1392   /// data structures are in-place before any query for resolved symbols
1393   /// can complete.
1394   LinkGraphPassList PostAllocationPasses;
1395 
1396   /// Pre-fixup passes.
1397   ///
1398   /// These passes are called on the graph after memory has been allocated,
1399   /// content copied into working memory, and all nodes (including externals)
1400   /// have been assigned their final addresses, but before any fixups have been
1401   /// applied.
1402   ///
1403   /// Notable use cases: Late link-time optimizations like GOT and stub
1404   /// elimination.
1405   LinkGraphPassList PreFixupPasses;
1406 
1407   /// Post-fixup passes.
1408   ///
1409   /// These passes are called on the graph after block contents has been copied
1410   /// to working memory, and fixups applied. Blocks have been updated to point
1411   /// to their fixed up content.
1412   ///
1413   /// Notable use cases: Testing and validation.
1414   LinkGraphPassList PostFixupPasses;
1415 };
1416 
1417 /// Flags for symbol lookup.
1418 ///
1419 /// FIXME: These basically duplicate orc::SymbolLookupFlags -- We should merge
1420 ///        the two types once we have an OrcSupport library.
1421 enum class SymbolLookupFlags { RequiredSymbol, WeaklyReferencedSymbol };
1422 
1423 raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF);
1424 
1425 /// A map of symbol names to resolved addresses.
1426 using AsyncLookupResult = DenseMap<StringRef, JITEvaluatedSymbol>;
1427 
1428 /// A function object to call with a resolved symbol map (See AsyncLookupResult)
1429 /// or an error if resolution failed.
1430 class JITLinkAsyncLookupContinuation {
1431 public:
~JITLinkAsyncLookupContinuation()1432   virtual ~JITLinkAsyncLookupContinuation() {}
1433   virtual void run(Expected<AsyncLookupResult> LR) = 0;
1434 
1435 private:
1436   virtual void anchor();
1437 };
1438 
1439 /// Create a lookup continuation from a function object.
1440 template <typename Continuation>
1441 std::unique_ptr<JITLinkAsyncLookupContinuation>
createLookupContinuation(Continuation Cont)1442 createLookupContinuation(Continuation Cont) {
1443 
1444   class Impl final : public JITLinkAsyncLookupContinuation {
1445   public:
1446     Impl(Continuation C) : C(std::move(C)) {}
1447     void run(Expected<AsyncLookupResult> LR) override { C(std::move(LR)); }
1448 
1449   private:
1450     Continuation C;
1451   };
1452 
1453   return std::make_unique<Impl>(std::move(Cont));
1454 }
1455 
1456 /// Holds context for a single jitLink invocation.
1457 class JITLinkContext {
1458 public:
1459   using LookupMap = DenseMap<StringRef, SymbolLookupFlags>;
1460 
1461   /// Create a JITLinkContext.
JITLinkContext(const JITLinkDylib * JD)1462   JITLinkContext(const JITLinkDylib *JD) : JD(JD) {}
1463 
1464   /// Destroy a JITLinkContext.
1465   virtual ~JITLinkContext();
1466 
1467   /// Return the JITLinkDylib that this link is targeting, if any.
getJITLinkDylib()1468   const JITLinkDylib *getJITLinkDylib() const { return JD; }
1469 
1470   /// Return the MemoryManager to be used for this link.
1471   virtual JITLinkMemoryManager &getMemoryManager() = 0;
1472 
1473   /// Notify this context that linking failed.
1474   /// Called by JITLink if linking cannot be completed.
1475   virtual void notifyFailed(Error Err) = 0;
1476 
1477   /// Called by JITLink to resolve external symbols. This method is passed a
1478   /// lookup continutation which it must call with a result to continue the
1479   /// linking process.
1480   virtual void lookup(const LookupMap &Symbols,
1481                       std::unique_ptr<JITLinkAsyncLookupContinuation> LC) = 0;
1482 
1483   /// Called by JITLink once all defined symbols in the graph have been assigned
1484   /// their final memory locations in the target process. At this point the
1485   /// LinkGraph can be inspected to build a symbol table, however the block
1486   /// content will not generally have been copied to the target location yet.
1487   ///
1488   /// If the client detects an error in the LinkGraph state (e.g. unexpected or
1489   /// missing symbols) they may return an error here. The error will be
1490   /// propagated to notifyFailed and the linker will bail out.
1491   virtual Error notifyResolved(LinkGraph &G) = 0;
1492 
1493   /// Called by JITLink to notify the context that the object has been
1494   /// finalized (i.e. emitted to memory and memory permissions set). If all of
1495   /// this objects dependencies have also been finalized then the code is ready
1496   /// to run.
1497   virtual void
1498   notifyFinalized(std::unique_ptr<JITLinkMemoryManager::Allocation> A) = 0;
1499 
1500   /// Called by JITLink prior to linking to determine whether default passes for
1501   /// the target should be added. The default implementation returns true.
1502   /// If subclasses override this method to return false for any target then
1503   /// they are required to fully configure the pass pipeline for that target.
1504   virtual bool shouldAddDefaultTargetPasses(const Triple &TT) const;
1505 
1506   /// Returns the mark-live pass to be used for this link. If no pass is
1507   /// returned (the default) then the target-specific linker implementation will
1508   /// choose a conservative default (usually marking all symbols live).
1509   /// This function is only called if shouldAddDefaultTargetPasses returns true,
1510   /// otherwise the JITContext is responsible for adding a mark-live pass in
1511   /// modifyPassConfig.
1512   virtual LinkGraphPassFunction getMarkLivePass(const Triple &TT) const;
1513 
1514   /// Called by JITLink to modify the pass pipeline prior to linking.
1515   /// The default version performs no modification.
1516   virtual Error modifyPassConfig(LinkGraph &G, PassConfiguration &Config);
1517 
1518 private:
1519   const JITLinkDylib *JD = nullptr;
1520 };
1521 
1522 /// Marks all symbols in a graph live. This can be used as a default,
1523 /// conservative mark-live implementation.
1524 Error markAllSymbolsLive(LinkGraph &G);
1525 
1526 /// Create an out of range error for the given edge in the given block.
1527 Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
1528                                 const Edge &E);
1529 
1530 /// Create a LinkGraph from the given object buffer.
1531 ///
1532 /// Note: The graph does not take ownership of the underlying buffer, nor copy
1533 /// its contents. The caller is responsible for ensuring that the object buffer
1534 /// outlives the graph.
1535 Expected<std::unique_ptr<LinkGraph>>
1536 createLinkGraphFromObject(MemoryBufferRef ObjectBuffer);
1537 
1538 /// Link the given graph.
1539 void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx);
1540 
1541 } // end namespace jitlink
1542 } // end namespace llvm
1543 
1544 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
1545