xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/FunctionExtras.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
21 #include "llvm/ExecutionEngine/JITSymbol.h"
22 #include "llvm/ExecutionEngine/Orc/Core.h"
23 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
24 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
25 #include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
26 #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/BinaryStreamReader.h"
29 #include "llvm/Support/BinaryStreamWriter.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/Error.h"
33 #include "llvm/Support/FormatVariadic.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/TargetParser/SubtargetFeature.h"
37 #include "llvm/TargetParser/Triple.h"
38 #include <optional>
39 
40 #include <map>
41 #include <string>
42 #include <system_error>
43 
44 namespace llvm {
45 namespace jitlink {
46 
47 class LinkGraph;
48 class Symbol;
49 class Section;
50 
51 /// Base class for errors originating in JIT linker, e.g. missing relocation
52 /// support.
53 class LLVM_ABI JITLinkError : public ErrorInfo<JITLinkError> {
54 public:
55   static char ID;
56 
JITLinkError(Twine ErrMsg)57   JITLinkError(Twine ErrMsg) : ErrMsg(ErrMsg.str()) {}
58 
59   void log(raw_ostream &OS) const override;
getErrorMessage()60   const std::string &getErrorMessage() const { return ErrMsg; }
61   std::error_code convertToErrorCode() const override;
62 
63 private:
64   std::string ErrMsg;
65 };
66 
67 /// Represents fixups and constraints in the LinkGraph.
68 class Edge {
69 public:
70   using Kind = uint8_t;
71 
72   enum GenericEdgeKind : Kind {
73     Invalid,                    // Invalid edge value.
74     FirstKeepAlive,             // Keeps target alive. Offset/addend zero.
75     KeepAlive = FirstKeepAlive, // Tag first edge kind that preserves liveness.
76     FirstRelocation             // First architecture specific relocation.
77   };
78 
79   using OffsetT = uint32_t;
80   using AddendT = int64_t;
81 
Edge(Kind K,OffsetT Offset,Symbol & Target,AddendT Addend)82   Edge(Kind K, OffsetT Offset, Symbol &Target, AddendT Addend)
83       : Target(&Target), Offset(Offset), Addend(Addend), K(K) {}
84 
getOffset()85   OffsetT getOffset() const { return Offset; }
setOffset(OffsetT Offset)86   void setOffset(OffsetT Offset) { this->Offset = Offset; }
getKind()87   Kind getKind() const { return K; }
setKind(Kind K)88   void setKind(Kind K) { this->K = K; }
isRelocation()89   bool isRelocation() const { return K >= FirstRelocation; }
getRelocation()90   Kind getRelocation() const {
91     assert(isRelocation() && "Not a relocation edge");
92     return K - FirstRelocation;
93   }
isKeepAlive()94   bool isKeepAlive() const { return K >= FirstKeepAlive; }
getTarget()95   Symbol &getTarget() const { return *Target; }
setTarget(Symbol & Target)96   void setTarget(Symbol &Target) { this->Target = &Target; }
getAddend()97   AddendT getAddend() const { return Addend; }
setAddend(AddendT Addend)98   void setAddend(AddendT Addend) { this->Addend = Addend; }
99 
100 private:
101   Symbol *Target = nullptr;
102   OffsetT Offset = 0;
103   AddendT Addend = 0;
104   Kind K = 0;
105 };
106 
107 /// Returns the string name of the given generic edge kind, or "unknown"
108 /// otherwise. Useful for debugging.
109 LLVM_ABI const char *getGenericEdgeKindName(Edge::Kind K);
110 
111 /// Base class for Addressable entities (externals, absolutes, blocks).
112 class Addressable {
113   friend class LinkGraph;
114 
115 protected:
Addressable(orc::ExecutorAddr Address,bool IsDefined)116   Addressable(orc::ExecutorAddr Address, bool IsDefined)
117       : Address(Address), IsDefined(IsDefined), IsAbsolute(false) {}
118 
Addressable(orc::ExecutorAddr Address)119   Addressable(orc::ExecutorAddr Address)
120       : Address(Address), IsDefined(false), IsAbsolute(true) {
121     assert(!(IsDefined && IsAbsolute) &&
122            "Block cannot be both defined and absolute");
123   }
124 
125 public:
126   Addressable(const Addressable &) = delete;
127   Addressable &operator=(const Addressable &) = default;
128   Addressable(Addressable &&) = delete;
129   Addressable &operator=(Addressable &&) = default;
130 
getAddress()131   orc::ExecutorAddr getAddress() const { return Address; }
setAddress(orc::ExecutorAddr Address)132   void setAddress(orc::ExecutorAddr Address) { this->Address = Address; }
133 
134   /// Returns true if this is a defined addressable, in which case you
135   /// can downcast this to a Block.
isDefined()136   bool isDefined() const { return static_cast<bool>(IsDefined); }
isAbsolute()137   bool isAbsolute() const { return static_cast<bool>(IsAbsolute); }
138 
139 private:
setAbsolute(bool IsAbsolute)140   void setAbsolute(bool IsAbsolute) {
141     assert(!IsDefined && "Cannot change the Absolute flag on a defined block");
142     this->IsAbsolute = IsAbsolute;
143   }
144 
145   orc::ExecutorAddr Address;
146   uint64_t IsDefined : 1;
147   uint64_t IsAbsolute : 1;
148 
149 protected:
150   // bitfields for Block, allocated here to improve packing.
151   uint64_t ContentMutable : 1;
152   uint64_t P2Align : 5;
153   uint64_t AlignmentOffset : 56;
154 };
155 
156 using SectionOrdinal = unsigned;
157 
158 /// An Addressable with content and edges.
159 class Block : public Addressable {
160   friend class LinkGraph;
161 
162 private:
163   /// Create a zero-fill defined addressable.
Block(Section & Parent,orc::ExecutorAddrDiff Size,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)164   Block(Section &Parent, orc::ExecutorAddrDiff Size, orc::ExecutorAddr Address,
165         uint64_t Alignment, uint64_t AlignmentOffset)
166       : Addressable(Address, true), Parent(&Parent), Size(Size) {
167     assert(isPowerOf2_64(Alignment) && "Alignment must be power of 2");
168     assert(AlignmentOffset < Alignment &&
169            "Alignment offset cannot exceed alignment");
170     assert(AlignmentOffset <= MaxAlignmentOffset &&
171            "Alignment offset exceeds maximum");
172     ContentMutable = false;
173     P2Align = Alignment ? llvm::countr_zero(Alignment) : 0;
174     this->AlignmentOffset = AlignmentOffset;
175   }
176 
177   /// Create a defined addressable for the given content.
178   /// The Content is assumed to be non-writable, and will be copied when
179   /// mutations are required.
Block(Section & Parent,ArrayRef<char> Content,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)180   Block(Section &Parent, ArrayRef<char> Content, orc::ExecutorAddr Address,
181         uint64_t Alignment, uint64_t AlignmentOffset)
182       : Addressable(Address, true), Parent(&Parent), Data(Content.data()),
183         Size(Content.size()) {
184     assert(isPowerOf2_64(Alignment) && "Alignment must be power of 2");
185     assert(AlignmentOffset < Alignment &&
186            "Alignment offset cannot exceed alignment");
187     assert(AlignmentOffset <= MaxAlignmentOffset &&
188            "Alignment offset exceeds maximum");
189     ContentMutable = false;
190     P2Align = Alignment ? llvm::countr_zero(Alignment) : 0;
191     this->AlignmentOffset = AlignmentOffset;
192   }
193 
194   /// Create a defined addressable for the given content.
195   /// The content is assumed to be writable, and the caller is responsible
196   /// for ensuring that it lives for the duration of the Block's lifetime.
197   /// The standard way to achieve this is to allocate it on the Graph's
198   /// allocator.
Block(Section & Parent,MutableArrayRef<char> Content,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)199   Block(Section &Parent, MutableArrayRef<char> Content,
200         orc::ExecutorAddr Address, uint64_t Alignment, uint64_t AlignmentOffset)
201       : Addressable(Address, true), Parent(&Parent), Data(Content.data()),
202         Size(Content.size()) {
203     assert(isPowerOf2_64(Alignment) && "Alignment must be power of 2");
204     assert(AlignmentOffset < Alignment &&
205            "Alignment offset cannot exceed alignment");
206     assert(AlignmentOffset <= MaxAlignmentOffset &&
207            "Alignment offset exceeds maximum");
208     ContentMutable = true;
209     P2Align = Alignment ? llvm::countr_zero(Alignment) : 0;
210     this->AlignmentOffset = AlignmentOffset;
211   }
212 
213 public:
214   using EdgeVector = std::vector<Edge>;
215   using edge_iterator = EdgeVector::iterator;
216   using const_edge_iterator = EdgeVector::const_iterator;
217 
218   Block(const Block &) = delete;
219   Block &operator=(const Block &) = delete;
220   Block(Block &&) = delete;
221   Block &operator=(Block &&) = delete;
222 
223   /// Return the parent section for this block.
getSection()224   Section &getSection() const { return *Parent; }
225 
226   /// Returns true if this is a zero-fill block.
227   ///
228   /// If true, getSize is callable but getContent is not (the content is
229   /// defined to be a sequence of zero bytes of length Size).
isZeroFill()230   bool isZeroFill() const { return !Data; }
231 
232   /// Returns the size of this defined addressable.
getSize()233   size_t getSize() const { return Size; }
234 
235   /// Turns this block into a zero-fill block of the given size.
setZeroFillSize(size_t Size)236   void setZeroFillSize(size_t Size) {
237     Data = nullptr;
238     this->Size = Size;
239   }
240 
241   /// Returns the address range of this defined addressable.
getRange()242   orc::ExecutorAddrRange getRange() const {
243     return orc::ExecutorAddrRange(getAddress(), getSize());
244   }
245 
246   /// Get the content for this block. Block must not be a zero-fill block.
getContent()247   ArrayRef<char> getContent() const {
248     assert(Data && "Block does not contain content");
249     return ArrayRef<char>(Data, Size);
250   }
251 
252   /// Set the content for this block.
253   /// Caller is responsible for ensuring the underlying bytes are not
254   /// deallocated while pointed to by this block.
setContent(ArrayRef<char> Content)255   void setContent(ArrayRef<char> Content) {
256     assert(Content.data() && "Setting null content");
257     Data = Content.data();
258     Size = Content.size();
259     ContentMutable = false;
260   }
261 
262   /// Get mutable content for this block.
263   ///
264   /// If this Block's content is not already mutable this will trigger a copy
265   /// of the existing immutable content to a new, mutable buffer allocated using
266   /// LinkGraph::allocateContent.
267   MutableArrayRef<char> getMutableContent(LinkGraph &G);
268 
269   /// Get mutable content for this block.
270   ///
271   /// This block's content must already be mutable. It is a programmatic error
272   /// to call this on a block with immutable content -- consider using
273   /// getMutableContent instead.
getAlreadyMutableContent()274   MutableArrayRef<char> getAlreadyMutableContent() {
275     assert(Data && "Block does not contain content");
276     assert(ContentMutable && "Content is not mutable");
277     return MutableArrayRef<char>(const_cast<char *>(Data), Size);
278   }
279 
280   /// Set mutable content for this block.
281   ///
282   /// The caller is responsible for ensuring that the memory pointed to by
283   /// MutableContent is not deallocated while pointed to by this block.
setMutableContent(MutableArrayRef<char> MutableContent)284   void setMutableContent(MutableArrayRef<char> MutableContent) {
285     assert(MutableContent.data() && "Setting null content");
286     Data = MutableContent.data();
287     Size = MutableContent.size();
288     ContentMutable = true;
289   }
290 
291   /// Returns true if this block's content is mutable.
292   ///
293   /// This is primarily useful for asserting that a block is already in a
294   /// mutable state prior to modifying the content. E.g. when applying
295   /// fixups we expect the block to already be mutable as it should have been
296   /// copied to working memory.
isContentMutable()297   bool isContentMutable() const { return ContentMutable; }
298 
299   /// Get the alignment for this content.
getAlignment()300   uint64_t getAlignment() const { return 1ull << P2Align; }
301 
302   /// Set the alignment for this content.
setAlignment(uint64_t Alignment)303   void setAlignment(uint64_t Alignment) {
304     assert(isPowerOf2_64(Alignment) && "Alignment must be a power of two");
305     P2Align = Alignment ? llvm::countr_zero(Alignment) : 0;
306   }
307 
308   /// Get the alignment offset for this content.
getAlignmentOffset()309   uint64_t getAlignmentOffset() const { return AlignmentOffset; }
310 
311   /// Set the alignment offset for this content.
setAlignmentOffset(uint64_t AlignmentOffset)312   void setAlignmentOffset(uint64_t AlignmentOffset) {
313     assert(AlignmentOffset < (1ull << P2Align) &&
314            "Alignment offset can't exceed alignment");
315     this->AlignmentOffset = AlignmentOffset;
316   }
317 
318   /// Add an edge to this block.
addEdge(Edge::Kind K,Edge::OffsetT Offset,Symbol & Target,Edge::AddendT Addend)319   void addEdge(Edge::Kind K, Edge::OffsetT Offset, Symbol &Target,
320                Edge::AddendT Addend) {
321     assert((K == Edge::KeepAlive || !isZeroFill()) &&
322            "Adding edge to zero-fill block?");
323     Edges.push_back(Edge(K, Offset, Target, Addend));
324   }
325 
326   /// Add an edge by copying an existing one. This is typically used when
327   /// moving edges between blocks.
addEdge(const Edge & E)328   void addEdge(const Edge &E) { Edges.push_back(E); }
329 
330   /// Return the list of edges attached to this content.
edges()331   iterator_range<edge_iterator> edges() {
332     return make_range(Edges.begin(), Edges.end());
333   }
334 
335   /// Returns the list of edges attached to this content.
edges()336   iterator_range<const_edge_iterator> edges() const {
337     return make_range(Edges.begin(), Edges.end());
338   }
339 
340   /// Returns an iterator over all edges at the given offset within the block.
edges_at(Edge::OffsetT O)341   auto edges_at(Edge::OffsetT O) {
342     return make_filter_range(edges(),
343                              [O](const Edge &E) { return E.getOffset() == O; });
344   }
345 
346   /// Returns an iterator over all edges at the given offset within the block.
edges_at(Edge::OffsetT O)347   auto edges_at(Edge::OffsetT O) const {
348     return make_filter_range(edges(),
349                              [O](const Edge &E) { return E.getOffset() == O; });
350   }
351 
352   /// Return the size of the edges list.
edges_size()353   size_t edges_size() const { return Edges.size(); }
354 
355   /// Returns true if the list of edges is empty.
edges_empty()356   bool edges_empty() const { return Edges.empty(); }
357 
358   /// Remove the edge pointed to by the given iterator.
359   /// Returns an iterator to the new next element.
removeEdge(edge_iterator I)360   edge_iterator removeEdge(edge_iterator I) { return Edges.erase(I); }
361 
362   /// Returns the address of the fixup for the given edge, which is equal to
363   /// this block's address plus the edge's offset.
getFixupAddress(const Edge & E)364   orc::ExecutorAddr getFixupAddress(const Edge &E) const {
365     return getAddress() + E.getOffset();
366   }
367 
368 private:
369   static constexpr uint64_t MaxAlignmentOffset = (1ULL << 56) - 1;
370 
setSection(Section & Parent)371   void setSection(Section &Parent) { this->Parent = &Parent; }
372 
373   Section *Parent;
374   const char *Data = nullptr;
375   size_t Size = 0;
376   std::vector<Edge> Edges;
377 };
378 
379 // Align an address to conform with block alignment requirements.
alignToBlock(uint64_t Addr,const Block & B)380 inline uint64_t alignToBlock(uint64_t Addr, const Block &B) {
381   uint64_t Delta = (B.getAlignmentOffset() - Addr) % B.getAlignment();
382   return Addr + Delta;
383 }
384 
385 // Align a orc::ExecutorAddr to conform with block alignment requirements.
alignToBlock(orc::ExecutorAddr Addr,const Block & B)386 inline orc::ExecutorAddr alignToBlock(orc::ExecutorAddr Addr, const Block &B) {
387   return orc::ExecutorAddr(alignToBlock(Addr.getValue(), B));
388 }
389 
390 // Returns true if the given blocks contains exactly one valid c-string.
391 // Zero-fill blocks of size 1 count as valid empty strings. Content blocks
392 // must end with a zero, and contain no zeros before the end.
393 LLVM_ABI bool isCStringBlock(Block &B);
394 
395 /// Describes symbol linkage. This can be used to resolve definition clashes.
396 enum class Linkage : uint8_t {
397   Strong,
398   Weak,
399 };
400 
401 /// Holds target-specific properties for a symbol.
402 using TargetFlagsType = uint8_t;
403 
404 /// For errors and debugging output.
405 LLVM_ABI const char *getLinkageName(Linkage L);
406 
407 /// Defines the scope in which this symbol should be visible:
408 ///   Default -- Visible in the public interface of the linkage unit.
409 ///   Hidden -- Visible within the linkage unit, but not exported from it.
410 ///   SideEffectsOnly -- Like hidden, but symbol can only be looked up once
411 ///                      to trigger materialization of the containing graph.
412 ///   Local -- Visible only within the LinkGraph.
413 enum class Scope : uint8_t { Default, Hidden, SideEffectsOnly, Local };
414 
415 /// For debugging output.
416 LLVM_ABI const char *getScopeName(Scope S);
417 
418 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const Block &B);
419 
420 /// Symbol representation.
421 ///
422 /// Symbols represent locations within Addressable objects.
423 /// They can be either Named or Anonymous.
424 /// Anonymous symbols have neither linkage nor visibility, and must point at
425 /// ContentBlocks.
426 /// Named symbols may be in one of four states:
427 ///   - Null: Default initialized. Assignable, but otherwise unusable.
428 ///   - Defined: Has both linkage and visibility and points to a ContentBlock
429 ///   - Common: Has both linkage and visibility, points to a null Addressable.
430 ///   - External: Has neither linkage nor visibility, points to an external
431 ///     Addressable.
432 ///
433 class Symbol {
434   friend class LinkGraph;
435 
436 private:
Symbol(Addressable & Base,orc::ExecutorAddrDiff Offset,orc::SymbolStringPtr && Name,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive,bool IsCallable)437   Symbol(Addressable &Base, orc::ExecutorAddrDiff Offset,
438          orc::SymbolStringPtr &&Name, orc::ExecutorAddrDiff Size, Linkage L,
439          Scope S, bool IsLive, bool IsCallable)
440       : Name(std::move(Name)), Base(&Base), Offset(Offset), WeakRef(0),
441         Size(Size) {
442     assert(Offset <= MaxOffset && "Offset out of range");
443     setLinkage(L);
444     setScope(S);
445     setLive(IsLive);
446     setCallable(IsCallable);
447     setTargetFlags(TargetFlagsType{});
448   }
449 
constructExternal(BumpPtrAllocator & Allocator,Addressable & Base,orc::SymbolStringPtr && Name,orc::ExecutorAddrDiff Size,Linkage L,bool WeaklyReferenced)450   static Symbol &constructExternal(BumpPtrAllocator &Allocator,
451                                    Addressable &Base,
452                                    orc::SymbolStringPtr &&Name,
453                                    orc::ExecutorAddrDiff Size, Linkage L,
454                                    bool WeaklyReferenced) {
455     assert(!Base.isDefined() &&
456            "Cannot create external symbol from defined block");
457     assert(Name && "External symbol name cannot be empty");
458     auto *Sym = Allocator.Allocate<Symbol>();
459     new (Sym)
460         Symbol(Base, 0, std::move(Name), Size, L, Scope::Default, false, false);
461     Sym->setWeaklyReferenced(WeaklyReferenced);
462     return *Sym;
463   }
464 
constructAbsolute(BumpPtrAllocator & Allocator,Addressable & Base,orc::SymbolStringPtr && Name,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive)465   static Symbol &constructAbsolute(BumpPtrAllocator &Allocator,
466                                    Addressable &Base,
467                                    orc::SymbolStringPtr &&Name,
468                                    orc::ExecutorAddrDiff Size, Linkage L,
469                                    Scope S, bool IsLive) {
470     assert(!Base.isDefined() &&
471            "Cannot create absolute symbol from a defined block");
472     auto *Sym = Allocator.Allocate<Symbol>();
473     new (Sym) Symbol(Base, 0, std::move(Name), Size, L, S, IsLive, false);
474     return *Sym;
475   }
476 
constructAnonDef(BumpPtrAllocator & Allocator,Block & Base,orc::ExecutorAddrDiff Offset,orc::ExecutorAddrDiff Size,bool IsCallable,bool IsLive)477   static Symbol &constructAnonDef(BumpPtrAllocator &Allocator, Block &Base,
478                                   orc::ExecutorAddrDiff Offset,
479                                   orc::ExecutorAddrDiff Size, bool IsCallable,
480                                   bool IsLive) {
481     assert((Offset + Size) <= Base.getSize() &&
482            "Symbol extends past end of block");
483     auto *Sym = Allocator.Allocate<Symbol>();
484     new (Sym) Symbol(Base, Offset, nullptr, Size, Linkage::Strong, Scope::Local,
485                      IsLive, IsCallable);
486     return *Sym;
487   }
488 
constructNamedDef(BumpPtrAllocator & Allocator,Block & Base,orc::ExecutorAddrDiff Offset,orc::SymbolStringPtr Name,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive,bool IsCallable)489   static Symbol &constructNamedDef(BumpPtrAllocator &Allocator, Block &Base,
490                                    orc::ExecutorAddrDiff Offset,
491                                    orc::SymbolStringPtr Name,
492                                    orc::ExecutorAddrDiff Size, Linkage L,
493                                    Scope S, bool IsLive, bool IsCallable) {
494     assert((Offset + Size) <= Base.getSize() &&
495            "Symbol extends past end of block");
496     assert(Name && "Name cannot be empty");
497     auto *Sym = Allocator.Allocate<Symbol>();
498     new (Sym)
499         Symbol(Base, Offset, std::move(Name), Size, L, S, IsLive, IsCallable);
500     return *Sym;
501   }
502 
503 public:
504   /// Create a null Symbol. This allows Symbols to be default initialized for
505   /// use in containers (e.g. as map values). Null symbols are only useful for
506   /// assigning to.
507   Symbol() = default;
508 
509   // Symbols are not movable or copyable.
510   Symbol(const Symbol &) = delete;
511   Symbol &operator=(const Symbol &) = delete;
512   Symbol(Symbol &&) = delete;
513   Symbol &operator=(Symbol &&) = delete;
514 
515   /// Returns true if this symbol has a name.
hasName()516   bool hasName() const { return Name != nullptr; }
517 
518   /// Returns the name of this symbol (empty if the symbol is anonymous).
getName()519   const orc::SymbolStringPtr &getName() const {
520     assert((hasName() || getScope() == Scope::Local) &&
521            "Anonymous symbol has non-local scope");
522 
523     return Name;
524   }
525 
526   /// Rename this symbol. The client is responsible for updating scope and
527   /// linkage if this name-change requires it.
setName(const orc::SymbolStringPtr Name)528   void setName(const orc::SymbolStringPtr Name) { this->Name = Name; }
529 
530   /// Returns true if this Symbol has content (potentially) defined within this
531   /// object file (i.e. is anything but an external or absolute symbol).
isDefined()532   bool isDefined() const {
533     assert(Base && "Attempt to access null symbol");
534     return Base->isDefined();
535   }
536 
537   /// Returns true if this symbol is live (i.e. should be treated as a root for
538   /// dead stripping).
isLive()539   bool isLive() const {
540     assert(Base && "Attempting to access null symbol");
541     return IsLive;
542   }
543 
544   /// Set this symbol's live bit.
setLive(bool IsLive)545   void setLive(bool IsLive) { this->IsLive = IsLive; }
546 
547   /// Returns true is this symbol is callable.
isCallable()548   bool isCallable() const { return IsCallable; }
549 
550   /// Set this symbol's callable bit.
setCallable(bool IsCallable)551   void setCallable(bool IsCallable) { this->IsCallable = IsCallable; }
552 
553   /// Returns true if the underlying addressable is an unresolved external.
isExternal()554   bool isExternal() const {
555     assert(Base && "Attempt to access null symbol");
556     return !Base->isDefined() && !Base->isAbsolute();
557   }
558 
559   /// Returns true if the underlying addressable is an absolute symbol.
isAbsolute()560   bool isAbsolute() const {
561     assert(Base && "Attempt to access null symbol");
562     return Base->isAbsolute();
563   }
564 
565   /// Return the addressable that this symbol points to.
getAddressable()566   Addressable &getAddressable() {
567     assert(Base && "Cannot get underlying addressable for null symbol");
568     return *Base;
569   }
570 
571   /// Return the addressable that this symbol points to.
getAddressable()572   const Addressable &getAddressable() const {
573     assert(Base && "Cannot get underlying addressable for null symbol");
574     return *Base;
575   }
576 
577   /// Return the Block for this Symbol (Symbol must be defined).
getBlock()578   Block &getBlock() {
579     assert(Base && "Cannot get block for null symbol");
580     assert(Base->isDefined() && "Not a defined symbol");
581     return static_cast<Block &>(*Base);
582   }
583 
584   /// Return the Block for this Symbol (Symbol must be defined).
getBlock()585   const Block &getBlock() const {
586     assert(Base && "Cannot get block for null symbol");
587     assert(Base->isDefined() && "Not a defined symbol");
588     return static_cast<const Block &>(*Base);
589   }
590 
591   /// Return the Section for this Symbol (Symbol must be defined).
getSection()592   Section &getSection() const { return getBlock().getSection(); }
593 
594   /// Returns the offset for this symbol within the underlying addressable.
getOffset()595   orc::ExecutorAddrDiff getOffset() const { return Offset; }
596 
setOffset(orc::ExecutorAddrDiff NewOffset)597   void setOffset(orc::ExecutorAddrDiff NewOffset) {
598     assert(NewOffset <= getBlock().getSize() && "Offset out of range");
599     Offset = NewOffset;
600   }
601 
602   /// Returns the address of this symbol.
getAddress()603   orc::ExecutorAddr getAddress() const { return Base->getAddress() + Offset; }
604 
605   /// Returns the size of this symbol.
getSize()606   orc::ExecutorAddrDiff getSize() const { return Size; }
607 
608   /// Set the size of this symbol.
setSize(orc::ExecutorAddrDiff Size)609   void setSize(orc::ExecutorAddrDiff Size) {
610     assert(Base && "Cannot set size for null Symbol");
611     assert((Size == 0 || Base->isDefined()) &&
612            "Non-zero size can only be set for defined symbols");
613     assert((Offset + Size <= static_cast<const Block &>(*Base).getSize()) &&
614            "Symbol size cannot extend past the end of its containing block");
615     this->Size = Size;
616   }
617 
618   /// Returns the address range of this symbol.
getRange()619   orc::ExecutorAddrRange getRange() const {
620     return orc::ExecutorAddrRange(getAddress(), getSize());
621   }
622 
623   /// Returns true if this symbol is backed by a zero-fill block.
624   /// This method may only be called on defined symbols.
isSymbolZeroFill()625   bool isSymbolZeroFill() const { return getBlock().isZeroFill(); }
626 
627   /// Returns the content in the underlying block covered by this symbol.
628   /// This method may only be called on defined non-zero-fill symbols.
getSymbolContent()629   ArrayRef<char> getSymbolContent() const {
630     return getBlock().getContent().slice(Offset, Size);
631   }
632 
633   /// Get the linkage for this Symbol.
getLinkage()634   Linkage getLinkage() const { return static_cast<Linkage>(L); }
635 
636   /// Set the linkage for this Symbol.
setLinkage(Linkage L)637   void setLinkage(Linkage L) {
638     assert((L == Linkage::Strong || (!Base->isAbsolute() && Name)) &&
639            "Linkage can only be applied to defined named symbols");
640     this->L = static_cast<uint8_t>(L);
641   }
642 
643   /// Get the visibility for this Symbol.
getScope()644   Scope getScope() const { return static_cast<Scope>(S); }
645 
646   /// Set the visibility for this Symbol.
setScope(Scope S)647   void setScope(Scope S) {
648     assert((hasName() || S == Scope::Local) &&
649            "Can not set anonymous symbol to non-local scope");
650     assert((S != Scope::Local || Base->isDefined() || Base->isAbsolute()) &&
651            "Invalid visibility for symbol type");
652     this->S = static_cast<uint8_t>(S);
653   }
654 
655   /// Get the target flags of this Symbol.
getTargetFlags()656   TargetFlagsType getTargetFlags() const { return TargetFlags; }
657 
658   /// Set the target flags for this Symbol.
setTargetFlags(TargetFlagsType Flags)659   void setTargetFlags(TargetFlagsType Flags) {
660     assert(Flags <= 1 && "Add more bits to store more than single flag");
661     TargetFlags = Flags;
662   }
663 
664   /// Returns true if this is a weakly referenced external symbol.
665   /// This method may only be called on external symbols.
isWeaklyReferenced()666   bool isWeaklyReferenced() const {
667     assert(isExternal() && "isWeaklyReferenced called on non-external");
668     return WeakRef;
669   }
670 
671   /// Set the WeaklyReferenced value for this symbol.
672   /// This method may only be called on external symbols.
setWeaklyReferenced(bool WeakRef)673   void setWeaklyReferenced(bool WeakRef) {
674     assert(isExternal() && "setWeaklyReferenced called on non-external");
675     this->WeakRef = WeakRef;
676   }
677 
678 private:
makeExternal(Addressable & A)679   void makeExternal(Addressable &A) {
680     assert(!A.isDefined() && !A.isAbsolute() &&
681            "Attempting to make external with defined or absolute block");
682     Base = &A;
683     Offset = 0;
684     setScope(Scope::Default);
685     IsLive = 0;
686     // note: Size, Linkage and IsCallable fields left unchanged.
687   }
688 
makeAbsolute(Addressable & A)689   void makeAbsolute(Addressable &A) {
690     assert(!A.isDefined() && A.isAbsolute() &&
691            "Attempting to make absolute with defined or external block");
692     Base = &A;
693     Offset = 0;
694   }
695 
setBlock(Block & B)696   void setBlock(Block &B) { Base = &B; }
697 
698   static constexpr uint64_t MaxOffset = (1ULL << 59) - 1;
699 
700   orc::SymbolStringPtr Name = nullptr;
701   Addressable *Base = nullptr;
702   uint64_t Offset : 57;
703   uint64_t L : 1;
704   uint64_t S : 2;
705   uint64_t IsLive : 1;
706   uint64_t IsCallable : 1;
707   uint64_t WeakRef : 1;
708   uint64_t TargetFlags : 1;
709   size_t Size = 0;
710 };
711 
712 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const Symbol &A);
713 
714 LLVM_ABI void printEdge(raw_ostream &OS, const Block &B, const Edge &E,
715                         StringRef EdgeKindName);
716 
717 /// Represents an object file section.
718 class Section {
719   friend class LinkGraph;
720 
721 private:
Section(StringRef Name,orc::MemProt Prot,SectionOrdinal SecOrdinal)722   Section(StringRef Name, orc::MemProt Prot, SectionOrdinal SecOrdinal)
723       : Name(Name), Prot(Prot), SecOrdinal(SecOrdinal) {}
724 
725   using SymbolSet = DenseSet<Symbol *>;
726   using BlockSet = DenseSet<Block *>;
727 
728 public:
729   using symbol_iterator = SymbolSet::iterator;
730   using const_symbol_iterator = SymbolSet::const_iterator;
731 
732   using block_iterator = BlockSet::iterator;
733   using const_block_iterator = BlockSet::const_iterator;
734 
735   LLVM_ABI ~Section();
736 
737   // Sections are not movable or copyable.
738   Section(const Section &) = delete;
739   Section &operator=(const Section &) = delete;
740   Section(Section &&) = delete;
741   Section &operator=(Section &&) = delete;
742 
743   /// Returns the name of this section.
getName()744   StringRef getName() const { return Name; }
745 
746   /// Returns the protection flags for this section.
getMemProt()747   orc::MemProt getMemProt() const { return Prot; }
748 
749   /// Set the protection flags for this section.
setMemProt(orc::MemProt Prot)750   void setMemProt(orc::MemProt Prot) { this->Prot = Prot; }
751 
752   /// Get the memory lifetime policy for this section.
getMemLifetime()753   orc::MemLifetime getMemLifetime() const { return ML; }
754 
755   /// Set the memory lifetime policy for this section.
setMemLifetime(orc::MemLifetime ML)756   void setMemLifetime(orc::MemLifetime ML) { this->ML = ML; }
757 
758   /// Returns the ordinal for this section.
getOrdinal()759   SectionOrdinal getOrdinal() const { return SecOrdinal; }
760 
761   /// Set the ordinal for this section. Ordinals are used to order the layout
762   /// of sections with the same permissions.
setOrdinal(SectionOrdinal SecOrdinal)763   void setOrdinal(SectionOrdinal SecOrdinal) { this->SecOrdinal = SecOrdinal; }
764 
765   /// Returns true if this section is empty (contains no blocks or symbols).
empty()766   bool empty() const { return Blocks.empty(); }
767 
768   /// Returns an iterator over the blocks defined in this section.
blocks()769   iterator_range<block_iterator> blocks() {
770     return make_range(Blocks.begin(), Blocks.end());
771   }
772 
773   /// Returns an iterator over the blocks defined in this section.
blocks()774   iterator_range<const_block_iterator> blocks() const {
775     return make_range(Blocks.begin(), Blocks.end());
776   }
777 
778   /// Returns the number of blocks in this section.
blocks_size()779   BlockSet::size_type blocks_size() const { return Blocks.size(); }
780 
781   /// Returns an iterator over the symbols defined in this section.
symbols()782   iterator_range<symbol_iterator> symbols() {
783     return make_range(Symbols.begin(), Symbols.end());
784   }
785 
786   /// Returns an iterator over the symbols defined in this section.
symbols()787   iterator_range<const_symbol_iterator> symbols() const {
788     return make_range(Symbols.begin(), Symbols.end());
789   }
790 
791   /// Return the number of symbols in this section.
symbols_size()792   SymbolSet::size_type symbols_size() const { return Symbols.size(); }
793 
794 private:
addSymbol(Symbol & Sym)795   void addSymbol(Symbol &Sym) {
796     assert(!Symbols.count(&Sym) && "Symbol is already in this section");
797     Symbols.insert(&Sym);
798   }
799 
removeSymbol(Symbol & Sym)800   void removeSymbol(Symbol &Sym) {
801     assert(Symbols.count(&Sym) && "symbol is not in this section");
802     Symbols.erase(&Sym);
803   }
804 
addBlock(Block & B)805   void addBlock(Block &B) {
806     assert(!Blocks.count(&B) && "Block is already in this section");
807     Blocks.insert(&B);
808   }
809 
removeBlock(Block & B)810   void removeBlock(Block &B) {
811     assert(Blocks.count(&B) && "Block is not in this section");
812     Blocks.erase(&B);
813   }
814 
transferContentTo(Section & DstSection)815   void transferContentTo(Section &DstSection) {
816     if (&DstSection == this)
817       return;
818     for (auto *S : Symbols)
819       DstSection.addSymbol(*S);
820     for (auto *B : Blocks)
821       DstSection.addBlock(*B);
822     Symbols.clear();
823     Blocks.clear();
824   }
825 
826   StringRef Name;
827   orc::MemProt Prot;
828   orc::MemLifetime ML = orc::MemLifetime::Standard;
829   SectionOrdinal SecOrdinal = 0;
830   BlockSet Blocks;
831   SymbolSet Symbols;
832 };
833 
834 /// Represents a section address range via a pair of Block pointers
835 /// to the first and last Blocks in the section.
836 class SectionRange {
837 public:
838   SectionRange() = default;
SectionRange(const Section & Sec)839   SectionRange(const Section &Sec) {
840     if (Sec.blocks().empty())
841       return;
842     First = Last = *Sec.blocks().begin();
843     for (auto *B : Sec.blocks()) {
844       if (B->getAddress() < First->getAddress())
845         First = B;
846       if (B->getAddress() > Last->getAddress())
847         Last = B;
848     }
849   }
getFirstBlock()850   Block *getFirstBlock() const {
851     assert((!Last || First) && "First can not be null if end is non-null");
852     return First;
853   }
getLastBlock()854   Block *getLastBlock() const {
855     assert((First || !Last) && "Last can not be null if start is non-null");
856     return Last;
857   }
empty()858   bool empty() const {
859     assert((First || !Last) && "Last can not be null if start is non-null");
860     return !First;
861   }
getStart()862   orc::ExecutorAddr getStart() const {
863     return First ? First->getAddress() : orc::ExecutorAddr();
864   }
getEnd()865   orc::ExecutorAddr getEnd() const {
866     return Last ? Last->getAddress() + Last->getSize() : orc::ExecutorAddr();
867   }
getSize()868   orc::ExecutorAddrDiff getSize() const { return getEnd() - getStart(); }
869 
getRange()870   orc::ExecutorAddrRange getRange() const {
871     return orc::ExecutorAddrRange(getStart(), getEnd());
872   }
873 
874 private:
875   Block *First = nullptr;
876   Block *Last = nullptr;
877 };
878 
879 class LinkGraph {
880 private:
881   using SectionMap = DenseMap<StringRef, std::unique_ptr<Section>>;
882   using ExternalSymbolMap = StringMap<Symbol *>;
883   using AbsoluteSymbolSet = DenseSet<Symbol *>;
884   using BlockSet = DenseSet<Block *>;
885 
886   template <typename... ArgTs>
createAddressable(ArgTs &&...Args)887   Addressable &createAddressable(ArgTs &&... Args) {
888     Addressable *A =
889         reinterpret_cast<Addressable *>(Allocator.Allocate<Addressable>());
890     new (A) Addressable(std::forward<ArgTs>(Args)...);
891     return *A;
892   }
893 
destroyAddressable(Addressable & A)894   void destroyAddressable(Addressable &A) {
895     A.~Addressable();
896     Allocator.Deallocate(&A);
897   }
898 
createBlock(ArgTs &&...Args)899   template <typename... ArgTs> Block &createBlock(ArgTs &&... Args) {
900     Block *B = reinterpret_cast<Block *>(Allocator.Allocate<Block>());
901     new (B) Block(std::forward<ArgTs>(Args)...);
902     B->getSection().addBlock(*B);
903     return *B;
904   }
905 
destroyBlock(Block & B)906   void destroyBlock(Block &B) {
907     B.~Block();
908     Allocator.Deallocate(&B);
909   }
910 
destroySymbol(Symbol & S)911   void destroySymbol(Symbol &S) {
912     S.~Symbol();
913     Allocator.Deallocate(&S);
914   }
915 
getSectionBlocks(Section & S)916   static iterator_range<Section::block_iterator> getSectionBlocks(Section &S) {
917     return S.blocks();
918   }
919 
920   static iterator_range<Section::const_block_iterator>
getSectionConstBlocks(const Section & S)921   getSectionConstBlocks(const Section &S) {
922     return S.blocks();
923   }
924 
925   static iterator_range<Section::symbol_iterator>
getSectionSymbols(Section & S)926   getSectionSymbols(Section &S) {
927     return S.symbols();
928   }
929 
930   static iterator_range<Section::const_symbol_iterator>
getSectionConstSymbols(const Section & S)931   getSectionConstSymbols(const Section &S) {
932     return S.symbols();
933   }
934 
935   struct GetExternalSymbolMapEntryValue {
operatorGetExternalSymbolMapEntryValue936     Symbol *operator()(ExternalSymbolMap::value_type &KV) const {
937       return KV.second;
938     }
939   };
940 
941   struct GetSectionMapEntryValue {
operatorGetSectionMapEntryValue942     Section &operator()(SectionMap::value_type &KV) const { return *KV.second; }
943   };
944 
945   struct GetSectionMapEntryConstValue {
operatorGetSectionMapEntryConstValue946     const Section &operator()(const SectionMap::value_type &KV) const {
947       return *KV.second;
948     }
949   };
950 
951 public:
952   using external_symbol_iterator =
953       mapped_iterator<ExternalSymbolMap::iterator,
954                       GetExternalSymbolMapEntryValue>;
955   using absolute_symbol_iterator = AbsoluteSymbolSet::iterator;
956 
957   using section_iterator =
958       mapped_iterator<SectionMap::iterator, GetSectionMapEntryValue>;
959   using const_section_iterator =
960       mapped_iterator<SectionMap::const_iterator, GetSectionMapEntryConstValue>;
961 
962   template <typename OuterItrT, typename InnerItrT, typename T,
963             iterator_range<InnerItrT> getInnerRange(
964                 typename OuterItrT::reference)>
965   class nested_collection_iterator
966       : public iterator_facade_base<
967             nested_collection_iterator<OuterItrT, InnerItrT, T, getInnerRange>,
968             std::forward_iterator_tag, T> {
969   public:
970     nested_collection_iterator() = default;
971 
nested_collection_iterator(OuterItrT OuterI,OuterItrT OuterE)972     nested_collection_iterator(OuterItrT OuterI, OuterItrT OuterE)
973         : OuterI(OuterI), OuterE(OuterE),
974           InnerI(getInnerBegin(OuterI, OuterE)) {
975       moveToNonEmptyInnerOrEnd();
976     }
977 
978     bool operator==(const nested_collection_iterator &RHS) const {
979       return (OuterI == RHS.OuterI) && (InnerI == RHS.InnerI);
980     }
981 
982     T operator*() const {
983       assert(InnerI != getInnerRange(*OuterI).end() && "Dereferencing end?");
984       return *InnerI;
985     }
986 
987     nested_collection_iterator operator++() {
988       ++InnerI;
989       moveToNonEmptyInnerOrEnd();
990       return *this;
991     }
992 
993   private:
getInnerBegin(OuterItrT OuterI,OuterItrT OuterE)994     static InnerItrT getInnerBegin(OuterItrT OuterI, OuterItrT OuterE) {
995       return OuterI != OuterE ? getInnerRange(*OuterI).begin() : InnerItrT();
996     }
997 
moveToNonEmptyInnerOrEnd()998     void moveToNonEmptyInnerOrEnd() {
999       while (OuterI != OuterE && InnerI == getInnerRange(*OuterI).end()) {
1000         ++OuterI;
1001         InnerI = getInnerBegin(OuterI, OuterE);
1002       }
1003     }
1004 
1005     OuterItrT OuterI, OuterE;
1006     InnerItrT InnerI;
1007   };
1008 
1009   using defined_symbol_iterator =
1010       nested_collection_iterator<section_iterator, Section::symbol_iterator,
1011                                  Symbol *, getSectionSymbols>;
1012 
1013   using const_defined_symbol_iterator =
1014       nested_collection_iterator<const_section_iterator,
1015                                  Section::const_symbol_iterator, const Symbol *,
1016                                  getSectionConstSymbols>;
1017 
1018   using block_iterator =
1019       nested_collection_iterator<section_iterator, Section::block_iterator,
1020                                  Block *, getSectionBlocks>;
1021 
1022   using const_block_iterator =
1023       nested_collection_iterator<const_section_iterator,
1024                                  Section::const_block_iterator, const Block *,
1025                                  getSectionConstBlocks>;
1026 
1027   using GetEdgeKindNameFunction = const char *(*)(Edge::Kind);
1028 
LinkGraph(std::string Name,std::shared_ptr<orc::SymbolStringPool> SSP,Triple TT,SubtargetFeatures Features,GetEdgeKindNameFunction GetEdgeKindName)1029   LinkGraph(std::string Name, std::shared_ptr<orc::SymbolStringPool> SSP,
1030             Triple TT, SubtargetFeatures Features,
1031             GetEdgeKindNameFunction GetEdgeKindName)
1032       : Name(std::move(Name)), SSP(std::move(SSP)), TT(std::move(TT)),
1033         Features(std::move(Features)),
1034         GetEdgeKindName(std::move(GetEdgeKindName)) {
1035     assert(!(Triple::getArchPointerBitWidth(this->TT.getArch()) % 8) &&
1036            "Arch bitwidth is not a multiple of 8");
1037   }
1038 
1039   LinkGraph(const LinkGraph &) = delete;
1040   LinkGraph &operator=(const LinkGraph &) = delete;
1041   LinkGraph(LinkGraph &&) = delete;
1042   LinkGraph &operator=(LinkGraph &&) = delete;
1043   LLVM_ABI ~LinkGraph();
1044 
1045   /// Returns the name of this graph (usually the name of the original
1046   /// underlying MemoryBuffer).
getName()1047   const std::string &getName() const { return Name; }
1048 
1049   /// Returns the target triple for this Graph.
getTargetTriple()1050   const Triple &getTargetTriple() const { return TT; }
1051 
1052   /// Return the subtarget features for this Graph.
getFeatures()1053   const SubtargetFeatures &getFeatures() const { return Features; }
1054 
1055   /// Returns the pointer size for use in this graph.
getPointerSize()1056   unsigned getPointerSize() const { return TT.getArchPointerBitWidth() / 8; }
1057 
1058   /// Returns the endianness of content in this graph.
getEndianness()1059   llvm::endianness getEndianness() const {
1060     return TT.isLittleEndian() ? endianness::little : endianness::big;
1061   }
1062 
getEdgeKindName(Edge::Kind K)1063   const char *getEdgeKindName(Edge::Kind K) const { return GetEdgeKindName(K); }
1064 
getSymbolStringPool()1065   std::shared_ptr<orc::SymbolStringPool> getSymbolStringPool() { return SSP; }
1066 
1067   /// Allocate a mutable buffer of the given size using the LinkGraph's
1068   /// allocator.
allocateBuffer(size_t Size)1069   MutableArrayRef<char> allocateBuffer(size_t Size) {
1070     return {Allocator.Allocate<char>(Size), Size};
1071   }
1072 
1073   /// Allocate a copy of the given string using the LinkGraph's allocator.
1074   /// This can be useful when renaming symbols or adding new content to the
1075   /// graph.
allocateContent(ArrayRef<char> Source)1076   MutableArrayRef<char> allocateContent(ArrayRef<char> Source) {
1077     auto *AllocatedBuffer = Allocator.Allocate<char>(Source.size());
1078     llvm::copy(Source, AllocatedBuffer);
1079     return MutableArrayRef<char>(AllocatedBuffer, Source.size());
1080   }
1081 
1082   /// Allocate a copy of the given string using the LinkGraph's allocator.
1083   /// This can be useful when renaming symbols or adding new content to the
1084   /// graph.
1085   ///
1086   /// Note: This Twine-based overload requires an extra string copy and an
1087   /// extra heap allocation for large strings. The ArrayRef<char> overload
1088   /// should be preferred where possible.
allocateContent(Twine Source)1089   MutableArrayRef<char> allocateContent(Twine Source) {
1090     SmallString<256> TmpBuffer;
1091     auto SourceStr = Source.toStringRef(TmpBuffer);
1092     auto *AllocatedBuffer = Allocator.Allocate<char>(SourceStr.size());
1093     llvm::copy(SourceStr, AllocatedBuffer);
1094     return MutableArrayRef<char>(AllocatedBuffer, SourceStr.size());
1095   }
1096 
1097   /// Allocate a copy of the given string using the LinkGraph's allocator
1098   /// and return it as a StringRef.
1099   ///
1100   /// This is a convenience wrapper around allocateContent(Twine) that is
1101   /// handy when creating new symbol names within the graph.
allocateName(Twine Source)1102   StringRef allocateName(Twine Source) {
1103     auto Buf = allocateContent(Source);
1104     return {Buf.data(), Buf.size()};
1105   }
1106 
1107   /// Allocate a copy of the given string using the LinkGraph's allocator.
1108   ///
1109   /// The allocated string will be terminated with a null character, and the
1110   /// returned MutableArrayRef will include this null character in the last
1111   /// position.
allocateCString(StringRef Source)1112   MutableArrayRef<char> allocateCString(StringRef Source) {
1113     char *AllocatedBuffer = Allocator.Allocate<char>(Source.size() + 1);
1114     llvm::copy(Source, AllocatedBuffer);
1115     AllocatedBuffer[Source.size()] = '\0';
1116     return MutableArrayRef<char>(AllocatedBuffer, Source.size() + 1);
1117   }
1118 
1119   /// Allocate a copy of the given string using the LinkGraph's allocator.
1120   ///
1121   /// The allocated string will be terminated with a null character, and the
1122   /// returned MutableArrayRef will include this null character in the last
1123   /// position.
1124   ///
1125   /// Note: This Twine-based overload requires an extra string copy and an
1126   /// extra heap allocation for large strings. The ArrayRef<char> overload
1127   /// should be preferred where possible.
allocateCString(Twine Source)1128   MutableArrayRef<char> allocateCString(Twine Source) {
1129     SmallString<256> TmpBuffer;
1130     auto SourceStr = Source.toStringRef(TmpBuffer);
1131     auto *AllocatedBuffer = Allocator.Allocate<char>(SourceStr.size() + 1);
1132     llvm::copy(SourceStr, AllocatedBuffer);
1133     AllocatedBuffer[SourceStr.size()] = '\0';
1134     return MutableArrayRef<char>(AllocatedBuffer, SourceStr.size() + 1);
1135   }
1136 
1137   /// Create a section with the given name, protection flags.
createSection(StringRef Name,orc::MemProt Prot)1138   Section &createSection(StringRef Name, orc::MemProt Prot) {
1139     assert(!Sections.count(Name) && "Duplicate section name");
1140     std::unique_ptr<Section> Sec(new Section(Name, Prot, Sections.size()));
1141     return *Sections.insert(std::make_pair(Name, std::move(Sec))).first->second;
1142   }
1143 
1144   /// Create a content block.
createContentBlock(Section & Parent,ArrayRef<char> Content,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)1145   Block &createContentBlock(Section &Parent, ArrayRef<char> Content,
1146                             orc::ExecutorAddr Address, uint64_t Alignment,
1147                             uint64_t AlignmentOffset) {
1148     return createBlock(Parent, Content, Address, Alignment, AlignmentOffset);
1149   }
1150 
1151   /// Create a content block with initially mutable data.
createMutableContentBlock(Section & Parent,MutableArrayRef<char> MutableContent,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)1152   Block &createMutableContentBlock(Section &Parent,
1153                                    MutableArrayRef<char> MutableContent,
1154                                    orc::ExecutorAddr Address,
1155                                    uint64_t Alignment,
1156                                    uint64_t AlignmentOffset) {
1157     return createBlock(Parent, MutableContent, Address, Alignment,
1158                        AlignmentOffset);
1159   }
1160 
1161   /// Create a content block with initially mutable data of the given size.
1162   /// Content will be allocated via the LinkGraph's allocateBuffer method.
1163   /// By default the memory will be zero-initialized. Passing false for
1164   /// ZeroInitialize will prevent this.
1165   Block &createMutableContentBlock(Section &Parent, size_t ContentSize,
1166                                    orc::ExecutorAddr Address,
1167                                    uint64_t Alignment, uint64_t AlignmentOffset,
1168                                    bool ZeroInitialize = true) {
1169     auto Content = allocateBuffer(ContentSize);
1170     if (ZeroInitialize)
1171       memset(Content.data(), 0, Content.size());
1172     return createBlock(Parent, Content, Address, Alignment, AlignmentOffset);
1173   }
1174 
1175   /// Create a zero-fill block.
createZeroFillBlock(Section & Parent,orc::ExecutorAddrDiff Size,orc::ExecutorAddr Address,uint64_t Alignment,uint64_t AlignmentOffset)1176   Block &createZeroFillBlock(Section &Parent, orc::ExecutorAddrDiff Size,
1177                              orc::ExecutorAddr Address, uint64_t Alignment,
1178                              uint64_t AlignmentOffset) {
1179     return createBlock(Parent, Size, Address, Alignment, AlignmentOffset);
1180   }
1181 
1182   /// Returns a BinaryStreamReader for the given block.
getBlockContentReader(Block & B)1183   BinaryStreamReader getBlockContentReader(Block &B) {
1184     ArrayRef<uint8_t> C(
1185         reinterpret_cast<const uint8_t *>(B.getContent().data()), B.getSize());
1186     return BinaryStreamReader(C, getEndianness());
1187   }
1188 
1189   /// Returns a BinaryStreamWriter for the given block.
1190   /// This will call getMutableContent to obtain mutable content for the block.
getBlockContentWriter(Block & B)1191   BinaryStreamWriter getBlockContentWriter(Block &B) {
1192     MutableArrayRef<uint8_t> C(
1193         reinterpret_cast<uint8_t *>(B.getMutableContent(*this).data()),
1194         B.getSize());
1195     return BinaryStreamWriter(C, getEndianness());
1196   }
1197 
1198   /// Cache type for the splitBlock function.
1199   using SplitBlockCache = std::optional<SmallVector<Symbol *, 8>>;
1200 
1201   /// Splits block B into a sequence of smaller blocks.
1202   ///
1203   /// SplitOffsets should be a sequence of ascending offsets in B. The starting
1204   /// offset should be greater than zero, and the final offset less than
1205   /// B.getSize() - 1.
1206   ///
1207   /// The resulting seqeunce of blocks will start with the original block B
1208   /// (truncated to end at the first split offset) followed by newly introduced
1209   /// blocks starting at the subsequent split points.
1210   ///
1211   /// The optional Cache parameter can be used to speed up repeated calls to
1212   /// splitBlock for blocks within a single Section. If the value is None then
1213   /// the cache will be treated as uninitialized and splitBlock will populate
1214   /// it. Otherwise it is assumed to contain the list of Symbols pointing at B,
1215   /// sorted in descending order of offset.
1216   ///
1217   ///
1218   /// Notes:
1219   ///
1220   /// 1. splitBlock must be used with care. Splitting a block may cause
1221   ///    incoming edges to become invalid if the edge target subexpression
1222   ///    points outside the bounds of the newly split target block (E.g. an
1223   ///    edge 'S + 10 : Pointer64' where S points to a newly split block
1224   ///    whose size is less than 10). No attempt is made to detect invalidation
1225   ///    of incoming edges, as in general this requires context that the
1226   ///    LinkGraph does not have. Clients are responsible for ensuring that
1227   ///    splitBlock is not used in a way that invalidates edges.
1228   ///
1229   /// 2. The newly introduced blocks will have new ordinals that will be higher
1230   ///    than any other ordinals in the section. Clients are responsible for
1231   ///    re-assigning block ordinals to restore a compatible order if needed.
1232   ///
1233   /// 3. The cache is not automatically updated if new symbols are introduced
1234   ///    between calls to splitBlock. Any newly introduced symbols may be
1235   ///    added to the cache manually (descending offset order must be
1236   ///    preserved), or the cache can be set to None and rebuilt by
1237   ///    splitBlock on the next call.
1238   template <typename SplitOffsetRange>
1239   std::vector<Block *> splitBlock(Block &B, SplitOffsetRange &&SplitOffsets,
1240                                   LinkGraph::SplitBlockCache *Cache = nullptr) {
1241     std::vector<Block *> Blocks;
1242     Blocks.push_back(&B);
1243 
1244     if (std::empty(SplitOffsets))
1245       return Blocks;
1246 
1247     // Special case zero-fill:
1248     if (B.isZeroFill()) {
1249       size_t OrigSize = B.getSize();
1250       for (Edge::OffsetT Offset : SplitOffsets) {
1251         assert(Offset > 0 && Offset < B.getSize() &&
1252                "Split offset must be inside block content");
1253         Blocks.back()->setZeroFillSize(
1254             Offset - (Blocks.back()->getAddress() - B.getAddress()));
1255         Blocks.push_back(&createZeroFillBlock(
1256             B.getSection(), B.getSize(), B.getAddress() + Offset,
1257             B.getAlignment(),
1258             (B.getAlignmentOffset() + Offset) % B.getAlignment()));
1259       }
1260       Blocks.back()->setZeroFillSize(
1261           OrigSize - (Blocks.back()->getAddress() - B.getAddress()));
1262       return Blocks;
1263     }
1264 
1265     // Handle content blocks. We'll just create the blocks with their starting
1266     // address and no content here. The bulk of the work is deferred to
1267     // splitBlockImpl.
1268     for (Edge::OffsetT Offset : SplitOffsets) {
1269       assert(Offset > 0 && Offset < B.getSize() &&
1270              "Split offset must be inside block content");
1271       Blocks.push_back(&createContentBlock(
1272           B.getSection(), ArrayRef<char>(), B.getAddress() + Offset,
1273           B.getAlignment(),
1274           (B.getAlignmentOffset() + Offset) % B.getAlignment()));
1275     }
1276 
1277     return splitBlockImpl(std::move(Blocks), Cache);
1278   }
1279 
1280   /// Intern the given string in the LinkGraph's SymbolStringPool.
intern(StringRef SymbolName)1281   orc::SymbolStringPtr intern(StringRef SymbolName) {
1282     return SSP->intern(SymbolName);
1283   }
1284 
1285   /// Add an external symbol.
1286   /// Some formats (e.g. ELF) allow Symbols to have sizes. For Symbols whose
1287   /// size is not known, you should substitute '0'.
1288   /// The IsWeaklyReferenced argument determines whether the symbol must be
1289   /// present during lookup: Externals that are strongly referenced must be
1290   /// found or an error will be emitted. Externals that are weakly referenced
1291   /// are permitted to be undefined, in which case they are assigned an address
1292   /// of 0.
addExternalSymbol(orc::SymbolStringPtr Name,orc::ExecutorAddrDiff Size,bool IsWeaklyReferenced)1293   Symbol &addExternalSymbol(orc::SymbolStringPtr Name,
1294                             orc::ExecutorAddrDiff Size,
1295                             bool IsWeaklyReferenced) {
1296     assert(!ExternalSymbols.contains(*Name) && "Duplicate external symbol");
1297     auto &Sym = Symbol::constructExternal(
1298         Allocator, createAddressable(orc::ExecutorAddr(), false),
1299         std::move(Name), Size, Linkage::Strong, IsWeaklyReferenced);
1300     ExternalSymbols.insert({*Sym.getName(), &Sym});
1301     return Sym;
1302   }
1303 
addExternalSymbol(StringRef Name,orc::ExecutorAddrDiff Size,bool IsWeaklyReferenced)1304   Symbol &addExternalSymbol(StringRef Name, orc::ExecutorAddrDiff Size,
1305                             bool IsWeaklyReferenced) {
1306     return addExternalSymbol(SSP->intern(Name), Size, IsWeaklyReferenced);
1307   }
1308 
1309   /// Add an absolute symbol.
addAbsoluteSymbol(orc::SymbolStringPtr Name,orc::ExecutorAddr Address,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive)1310   Symbol &addAbsoluteSymbol(orc::SymbolStringPtr Name,
1311                             orc::ExecutorAddr Address,
1312                             orc::ExecutorAddrDiff Size, Linkage L, Scope S,
1313                             bool IsLive) {
1314     assert((S == Scope::Local || llvm::none_of(AbsoluteSymbols,
1315                                                [&](const Symbol *Sym) {
1316                                                  return Sym->getName() == Name;
1317                                                })) &&
1318            "Duplicate absolute symbol");
1319     auto &Sym = Symbol::constructAbsolute(Allocator, createAddressable(Address),
1320                                           std::move(Name), Size, L, S, IsLive);
1321     AbsoluteSymbols.insert(&Sym);
1322     return Sym;
1323   }
1324 
addAbsoluteSymbol(StringRef Name,orc::ExecutorAddr Address,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive)1325   Symbol &addAbsoluteSymbol(StringRef Name, orc::ExecutorAddr Address,
1326                             orc::ExecutorAddrDiff Size, Linkage L, Scope S,
1327                             bool IsLive) {
1328 
1329     return addAbsoluteSymbol(SSP->intern(Name), Address, Size, L, S, IsLive);
1330   }
1331 
1332   /// Add an anonymous symbol.
addAnonymousSymbol(Block & Content,orc::ExecutorAddrDiff Offset,orc::ExecutorAddrDiff Size,bool IsCallable,bool IsLive)1333   Symbol &addAnonymousSymbol(Block &Content, orc::ExecutorAddrDiff Offset,
1334                              orc::ExecutorAddrDiff Size, bool IsCallable,
1335                              bool IsLive) {
1336     auto &Sym = Symbol::constructAnonDef(Allocator, Content, Offset, Size,
1337                                          IsCallable, IsLive);
1338     Content.getSection().addSymbol(Sym);
1339     return Sym;
1340   }
1341 
1342   /// Add a named symbol.
addDefinedSymbol(Block & Content,orc::ExecutorAddrDiff Offset,StringRef Name,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsCallable,bool IsLive)1343   Symbol &addDefinedSymbol(Block &Content, orc::ExecutorAddrDiff Offset,
1344                            StringRef Name, orc::ExecutorAddrDiff Size,
1345                            Linkage L, Scope S, bool IsCallable, bool IsLive) {
1346     return addDefinedSymbol(Content, Offset, SSP->intern(Name), Size, L, S,
1347                             IsCallable, IsLive);
1348   }
1349 
addDefinedSymbol(Block & Content,orc::ExecutorAddrDiff Offset,orc::SymbolStringPtr Name,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsCallable,bool IsLive)1350   Symbol &addDefinedSymbol(Block &Content, orc::ExecutorAddrDiff Offset,
1351                            orc::SymbolStringPtr Name,
1352                            orc::ExecutorAddrDiff Size, Linkage L, Scope S,
1353                            bool IsCallable, bool IsLive) {
1354     assert((S == Scope::Local || llvm::none_of(defined_symbols(),
1355                                                [&](const Symbol *Sym) {
1356                                                  return Sym->getName() == Name;
1357                                                })) &&
1358            "Duplicate defined symbol");
1359     auto &Sym =
1360         Symbol::constructNamedDef(Allocator, Content, Offset, std::move(Name),
1361                                   Size, L, S, IsLive, IsCallable);
1362     Content.getSection().addSymbol(Sym);
1363     return Sym;
1364   }
1365 
sections()1366   iterator_range<section_iterator> sections() {
1367     return make_range(
1368         section_iterator(Sections.begin(), GetSectionMapEntryValue()),
1369         section_iterator(Sections.end(), GetSectionMapEntryValue()));
1370   }
1371 
sections()1372   iterator_range<const_section_iterator> sections() const {
1373     return make_range(
1374         const_section_iterator(Sections.begin(),
1375                                GetSectionMapEntryConstValue()),
1376         const_section_iterator(Sections.end(), GetSectionMapEntryConstValue()));
1377   }
1378 
sections_size()1379   size_t sections_size() const { return Sections.size(); }
1380 
1381   /// Returns the section with the given name if it exists, otherwise returns
1382   /// null.
findSectionByName(StringRef Name)1383   Section *findSectionByName(StringRef Name) {
1384     auto I = Sections.find(Name);
1385     if (I == Sections.end())
1386       return nullptr;
1387     return I->second.get();
1388   }
1389 
blocks()1390   iterator_range<block_iterator> blocks() {
1391     auto Secs = sections();
1392     return make_range(block_iterator(Secs.begin(), Secs.end()),
1393                       block_iterator(Secs.end(), Secs.end()));
1394   }
1395 
blocks()1396   iterator_range<const_block_iterator> blocks() const {
1397     auto Secs = sections();
1398     return make_range(const_block_iterator(Secs.begin(), Secs.end()),
1399                       const_block_iterator(Secs.end(), Secs.end()));
1400   }
1401 
external_symbols()1402   iterator_range<external_symbol_iterator> external_symbols() {
1403     return make_range(
1404         external_symbol_iterator(ExternalSymbols.begin(),
1405                                  GetExternalSymbolMapEntryValue()),
1406         external_symbol_iterator(ExternalSymbols.end(),
1407                                  GetExternalSymbolMapEntryValue()));
1408   }
1409 
1410   /// Returns the external symbol with the given name if one exists, otherwise
1411   /// returns nullptr.
findExternalSymbolByName(const orc::SymbolStringPtrBase & Name)1412   Symbol *findExternalSymbolByName(const orc::SymbolStringPtrBase &Name) {
1413     for (auto *Sym : external_symbols())
1414       if (Sym->getName() == Name)
1415         return Sym;
1416     return nullptr;
1417   }
1418 
absolute_symbols()1419   iterator_range<absolute_symbol_iterator> absolute_symbols() {
1420     return make_range(AbsoluteSymbols.begin(), AbsoluteSymbols.end());
1421   }
1422 
findAbsoluteSymbolByName(const orc::SymbolStringPtrBase & Name)1423   Symbol *findAbsoluteSymbolByName(const orc::SymbolStringPtrBase &Name) {
1424     for (auto *Sym : absolute_symbols())
1425       if (Sym->getName() == Name)
1426         return Sym;
1427     return nullptr;
1428   }
1429 
defined_symbols()1430   iterator_range<defined_symbol_iterator> defined_symbols() {
1431     auto Secs = sections();
1432     return make_range(defined_symbol_iterator(Secs.begin(), Secs.end()),
1433                       defined_symbol_iterator(Secs.end(), Secs.end()));
1434   }
1435 
defined_symbols()1436   iterator_range<const_defined_symbol_iterator> defined_symbols() const {
1437     auto Secs = sections();
1438     return make_range(const_defined_symbol_iterator(Secs.begin(), Secs.end()),
1439                       const_defined_symbol_iterator(Secs.end(), Secs.end()));
1440   }
1441 
1442   /// Returns the defined symbol with the given name if one exists, otherwise
1443   /// returns nullptr.
findDefinedSymbolByName(const orc::SymbolStringPtrBase & Name)1444   Symbol *findDefinedSymbolByName(const orc::SymbolStringPtrBase &Name) {
1445     for (auto *Sym : defined_symbols())
1446       if (Sym->hasName() && Sym->getName() == Name)
1447         return Sym;
1448     return nullptr;
1449   }
1450 
1451   /// Make the given symbol external (must not already be external).
1452   ///
1453   /// Symbol size, linkage and callability will be left unchanged. Symbol scope
1454   /// will be set to Default, and offset will be reset to 0.
makeExternal(Symbol & Sym)1455   void makeExternal(Symbol &Sym) {
1456     assert(!Sym.isExternal() && "Symbol is already external");
1457     if (Sym.isAbsolute()) {
1458       assert(AbsoluteSymbols.count(&Sym) &&
1459              "Sym is not in the absolute symbols set");
1460       assert(Sym.getOffset() == 0 && "Absolute not at offset 0");
1461       AbsoluteSymbols.erase(&Sym);
1462       auto &A = Sym.getAddressable();
1463       A.setAbsolute(false);
1464       A.setAddress(orc::ExecutorAddr());
1465     } else {
1466       assert(Sym.isDefined() && "Sym is not a defined symbol");
1467       Section &Sec = Sym.getSection();
1468       Sec.removeSymbol(Sym);
1469       Sym.makeExternal(createAddressable(orc::ExecutorAddr(), false));
1470     }
1471     ExternalSymbols.insert({*Sym.getName(), &Sym});
1472   }
1473 
1474   /// Make the given symbol an absolute with the given address (must not already
1475   /// be absolute).
1476   ///
1477   /// The symbol's size, linkage, and callability, and liveness will be left
1478   /// unchanged, and its offset will be reset to 0.
1479   ///
1480   /// If the symbol was external then its scope will be set to local, otherwise
1481   /// it will be left unchanged.
makeAbsolute(Symbol & Sym,orc::ExecutorAddr Address)1482   void makeAbsolute(Symbol &Sym, orc::ExecutorAddr Address) {
1483     assert(!Sym.isAbsolute() && "Symbol is already absolute");
1484     if (Sym.isExternal()) {
1485       assert(ExternalSymbols.contains(*Sym.getName()) &&
1486              "Sym is not in the absolute symbols set");
1487       assert(Sym.getOffset() == 0 && "External is not at offset 0");
1488       ExternalSymbols.erase(*Sym.getName());
1489       auto &A = Sym.getAddressable();
1490       A.setAbsolute(true);
1491       A.setAddress(Address);
1492       Sym.setScope(Scope::Local);
1493     } else {
1494       assert(Sym.isDefined() && "Sym is not a defined symbol");
1495       Section &Sec = Sym.getSection();
1496       Sec.removeSymbol(Sym);
1497       Sym.makeAbsolute(createAddressable(Address));
1498     }
1499     AbsoluteSymbols.insert(&Sym);
1500   }
1501 
1502   /// Turn an absolute or external symbol into a defined one by attaching it to
1503   /// a block. Symbol must not already be defined.
makeDefined(Symbol & Sym,Block & Content,orc::ExecutorAddrDiff Offset,orc::ExecutorAddrDiff Size,Linkage L,Scope S,bool IsLive)1504   void makeDefined(Symbol &Sym, Block &Content, orc::ExecutorAddrDiff Offset,
1505                    orc::ExecutorAddrDiff Size, Linkage L, Scope S,
1506                    bool IsLive) {
1507     assert(!Sym.isDefined() && "Sym is already a defined symbol");
1508     if (Sym.isAbsolute()) {
1509       assert(AbsoluteSymbols.count(&Sym) &&
1510              "Symbol is not in the absolutes set");
1511       AbsoluteSymbols.erase(&Sym);
1512     } else {
1513       assert(ExternalSymbols.contains(*Sym.getName()) &&
1514              "Symbol is not in the externals set");
1515       ExternalSymbols.erase(*Sym.getName());
1516     }
1517     Addressable &OldBase = *Sym.Base;
1518     Sym.setBlock(Content);
1519     Sym.setOffset(Offset);
1520     Sym.setSize(Size);
1521     Sym.setLinkage(L);
1522     Sym.setScope(S);
1523     Sym.setLive(IsLive);
1524     Content.getSection().addSymbol(Sym);
1525     destroyAddressable(OldBase);
1526   }
1527 
1528   /// Transfer a defined symbol from one block to another.
1529   ///
1530   /// The symbol's offset within DestBlock is set to NewOffset.
1531   ///
1532   /// If ExplicitNewSize is given as None then the size of the symbol will be
1533   /// checked and auto-truncated to at most the size of the remainder (from the
1534   /// given offset) of the size of the new block.
1535   ///
1536   /// All other symbol attributes are unchanged.
1537   void
transferDefinedSymbol(Symbol & Sym,Block & DestBlock,orc::ExecutorAddrDiff NewOffset,std::optional<orc::ExecutorAddrDiff> ExplicitNewSize)1538   transferDefinedSymbol(Symbol &Sym, Block &DestBlock,
1539                         orc::ExecutorAddrDiff NewOffset,
1540                         std::optional<orc::ExecutorAddrDiff> ExplicitNewSize) {
1541     auto &OldSection = Sym.getSection();
1542     Sym.setBlock(DestBlock);
1543     Sym.setOffset(NewOffset);
1544     if (ExplicitNewSize)
1545       Sym.setSize(*ExplicitNewSize);
1546     else {
1547       auto RemainingBlockSize = DestBlock.getSize() - NewOffset;
1548       if (Sym.getSize() > RemainingBlockSize)
1549         Sym.setSize(RemainingBlockSize);
1550     }
1551     if (&DestBlock.getSection() != &OldSection) {
1552       OldSection.removeSymbol(Sym);
1553       DestBlock.getSection().addSymbol(Sym);
1554     }
1555   }
1556 
1557   /// Transfers the given Block and all Symbols pointing to it to the given
1558   /// Section.
1559   ///
1560   /// No attempt is made to check compatibility of the source and destination
1561   /// sections. Blocks may be moved between sections with incompatible
1562   /// permissions (e.g. from data to text). The client is responsible for
1563   /// ensuring that this is safe.
transferBlock(Block & B,Section & NewSection)1564   void transferBlock(Block &B, Section &NewSection) {
1565     auto &OldSection = B.getSection();
1566     if (&OldSection == &NewSection)
1567       return;
1568     SmallVector<Symbol *> AttachedSymbols;
1569     for (auto *S : OldSection.symbols())
1570       if (&S->getBlock() == &B)
1571         AttachedSymbols.push_back(S);
1572     for (auto *S : AttachedSymbols) {
1573       OldSection.removeSymbol(*S);
1574       NewSection.addSymbol(*S);
1575     }
1576     OldSection.removeBlock(B);
1577     NewSection.addBlock(B);
1578   }
1579 
1580   /// Move all blocks and symbols from the source section to the destination
1581   /// section.
1582   ///
1583   /// If PreserveSrcSection is true (or SrcSection and DstSection are the same)
1584   /// then SrcSection is preserved, otherwise it is removed (the default).
1585   void mergeSections(Section &DstSection, Section &SrcSection,
1586                      bool PreserveSrcSection = false) {
1587     if (&DstSection == &SrcSection)
1588       return;
1589     for (auto *B : SrcSection.blocks())
1590       B->setSection(DstSection);
1591     SrcSection.transferContentTo(DstSection);
1592     if (!PreserveSrcSection)
1593       removeSection(SrcSection);
1594   }
1595 
1596   /// Removes an external symbol. Also removes the underlying Addressable.
removeExternalSymbol(Symbol & Sym)1597   void removeExternalSymbol(Symbol &Sym) {
1598     assert(!Sym.isDefined() && !Sym.isAbsolute() &&
1599            "Sym is not an external symbol");
1600     assert(ExternalSymbols.contains(*Sym.getName()) &&
1601            "Symbol is not in the externals set");
1602     ExternalSymbols.erase(*Sym.getName());
1603     Addressable &Base = *Sym.Base;
1604     assert(llvm::none_of(external_symbols(),
1605                          [&](Symbol *AS) { return AS->Base == &Base; }) &&
1606            "Base addressable still in use");
1607     destroySymbol(Sym);
1608     destroyAddressable(Base);
1609   }
1610 
1611   /// Remove an absolute symbol. Also removes the underlying Addressable.
removeAbsoluteSymbol(Symbol & Sym)1612   void removeAbsoluteSymbol(Symbol &Sym) {
1613     assert(!Sym.isDefined() && Sym.isAbsolute() &&
1614            "Sym is not an absolute symbol");
1615     assert(AbsoluteSymbols.count(&Sym) &&
1616            "Symbol is not in the absolute symbols set");
1617     AbsoluteSymbols.erase(&Sym);
1618     Addressable &Base = *Sym.Base;
1619     assert(llvm::none_of(external_symbols(),
1620                          [&](Symbol *AS) { return AS->Base == &Base; }) &&
1621            "Base addressable still in use");
1622     destroySymbol(Sym);
1623     destroyAddressable(Base);
1624   }
1625 
1626   /// Removes defined symbols. Does not remove the underlying block.
removeDefinedSymbol(Symbol & Sym)1627   void removeDefinedSymbol(Symbol &Sym) {
1628     assert(Sym.isDefined() && "Sym is not a defined symbol");
1629     Sym.getSection().removeSymbol(Sym);
1630     destroySymbol(Sym);
1631   }
1632 
1633   /// Remove a block. The block reference is defunct after calling this
1634   /// function and should no longer be used.
removeBlock(Block & B)1635   void removeBlock(Block &B) {
1636     assert(llvm::none_of(B.getSection().symbols(),
1637                          [&](const Symbol *Sym) {
1638                            return &Sym->getBlock() == &B;
1639                          }) &&
1640            "Block still has symbols attached");
1641     B.getSection().removeBlock(B);
1642     destroyBlock(B);
1643   }
1644 
1645   /// Remove a section. The section reference is defunct after calling this
1646   /// function and should no longer be used.
removeSection(Section & Sec)1647   void removeSection(Section &Sec) {
1648     assert(Sections.count(Sec.getName()) && "Section not found");
1649     assert(Sections.find(Sec.getName())->second.get() == &Sec &&
1650            "Section map entry invalid");
1651     Sections.erase(Sec.getName());
1652   }
1653 
1654   /// Accessor for the AllocActions object for this graph. This can be used to
1655   /// register allocation action calls prior to finalization.
1656   ///
1657   /// Accessing this object after finalization will result in undefined
1658   /// behavior.
allocActions()1659   orc::shared::AllocActions &allocActions() { return AAs; }
1660 
1661   /// Dump the graph.
1662   LLVM_ABI void dump(raw_ostream &OS);
1663 
1664 private:
1665   LLVM_ABI std::vector<Block *> splitBlockImpl(std::vector<Block *> Blocks,
1666                                                SplitBlockCache *Cache);
1667 
1668   // Put the BumpPtrAllocator first so that we don't free any of the underlying
1669   // memory until the Symbol/Addressable destructors have been run.
1670   BumpPtrAllocator Allocator;
1671 
1672   std::string Name;
1673   std::shared_ptr<orc::SymbolStringPool> SSP;
1674   Triple TT;
1675   SubtargetFeatures Features;
1676   GetEdgeKindNameFunction GetEdgeKindName = nullptr;
1677   DenseMap<StringRef, std::unique_ptr<Section>> Sections;
1678   // FIXME(jared): these should become dense maps
1679   ExternalSymbolMap ExternalSymbols;
1680   AbsoluteSymbolSet AbsoluteSymbols;
1681   orc::shared::AllocActions AAs;
1682 };
1683 
getMutableContent(LinkGraph & G)1684 inline MutableArrayRef<char> Block::getMutableContent(LinkGraph &G) {
1685   if (!ContentMutable)
1686     setMutableContent(G.allocateContent({Data, Size}));
1687   return MutableArrayRef<char>(const_cast<char *>(Data), Size);
1688 }
1689 
1690 /// Enables easy lookup of blocks by addresses.
1691 class BlockAddressMap {
1692 public:
1693   using AddrToBlockMap = std::map<orc::ExecutorAddr, Block *>;
1694   using const_iterator = AddrToBlockMap::const_iterator;
1695 
1696   /// A block predicate that always adds all blocks.
includeAllBlocks(const Block & B)1697   static bool includeAllBlocks(const Block &B) { return true; }
1698 
1699   /// A block predicate that always includes blocks with non-null addresses.
includeNonNull(const Block & B)1700   static bool includeNonNull(const Block &B) { return !!B.getAddress(); }
1701 
1702   BlockAddressMap() = default;
1703 
1704   /// Add a block to the map. Returns an error if the block overlaps with any
1705   /// existing block.
1706   template <typename PredFn = decltype(includeAllBlocks)>
1707   Error addBlock(Block &B, PredFn Pred = includeAllBlocks) {
1708     if (!Pred(B))
1709       return Error::success();
1710 
1711     auto I = AddrToBlock.upper_bound(B.getAddress());
1712 
1713     // If we're not at the end of the map, check for overlap with the next
1714     // element.
1715     if (I != AddrToBlock.end()) {
1716       if (B.getAddress() + B.getSize() > I->second->getAddress())
1717         return overlapError(B, *I->second);
1718     }
1719 
1720     // If we're not at the start of the map, check for overlap with the previous
1721     // element.
1722     if (I != AddrToBlock.begin()) {
1723       auto &PrevBlock = *std::prev(I)->second;
1724       if (PrevBlock.getAddress() + PrevBlock.getSize() > B.getAddress())
1725         return overlapError(B, PrevBlock);
1726     }
1727 
1728     AddrToBlock.insert(I, std::make_pair(B.getAddress(), &B));
1729     return Error::success();
1730   }
1731 
1732   /// Add a block to the map without checking for overlap with existing blocks.
1733   /// The client is responsible for ensuring that the block added does not
1734   /// overlap with any existing block.
addBlockWithoutChecking(Block & B)1735   void addBlockWithoutChecking(Block &B) { AddrToBlock[B.getAddress()] = &B; }
1736 
1737   /// Add a range of blocks to the map. Returns an error if any block in the
1738   /// range overlaps with any other block in the range, or with any existing
1739   /// block in the map.
1740   template <typename BlockPtrRange,
1741             typename PredFn = decltype(includeAllBlocks)>
1742   Error addBlocks(BlockPtrRange &&Blocks, PredFn Pred = includeAllBlocks) {
1743     for (auto *B : Blocks)
1744       if (auto Err = addBlock(*B, Pred))
1745         return Err;
1746     return Error::success();
1747   }
1748 
1749   /// Add a range of blocks to the map without checking for overlap with
1750   /// existing blocks. The client is responsible for ensuring that the block
1751   /// added does not overlap with any existing block.
1752   template <typename BlockPtrRange>
addBlocksWithoutChecking(BlockPtrRange && Blocks)1753   void addBlocksWithoutChecking(BlockPtrRange &&Blocks) {
1754     for (auto *B : Blocks)
1755       addBlockWithoutChecking(*B);
1756   }
1757 
1758   /// Iterates over (Address, Block*) pairs in ascending order of address.
begin()1759   const_iterator begin() const { return AddrToBlock.begin(); }
end()1760   const_iterator end() const { return AddrToBlock.end(); }
1761 
1762   /// Returns the block starting at the given address, or nullptr if no such
1763   /// block exists.
getBlockAt(orc::ExecutorAddr Addr)1764   Block *getBlockAt(orc::ExecutorAddr Addr) const {
1765     auto I = AddrToBlock.find(Addr);
1766     if (I == AddrToBlock.end())
1767       return nullptr;
1768     return I->second;
1769   }
1770 
1771   /// Returns the block covering the given address, or nullptr if no such block
1772   /// exists.
getBlockCovering(orc::ExecutorAddr Addr)1773   Block *getBlockCovering(orc::ExecutorAddr Addr) const {
1774     auto I = AddrToBlock.upper_bound(Addr);
1775     if (I == AddrToBlock.begin())
1776       return nullptr;
1777     auto *B = std::prev(I)->second;
1778     if (Addr < B->getAddress() + B->getSize())
1779       return B;
1780     return nullptr;
1781   }
1782 
1783 private:
overlapError(Block & NewBlock,Block & ExistingBlock)1784   Error overlapError(Block &NewBlock, Block &ExistingBlock) {
1785     auto NewBlockEnd = NewBlock.getAddress() + NewBlock.getSize();
1786     auto ExistingBlockEnd =
1787         ExistingBlock.getAddress() + ExistingBlock.getSize();
1788     return make_error<JITLinkError>(
1789         "Block at " +
1790         formatv("{0:x16} -- {1:x16}", NewBlock.getAddress().getValue(),
1791                 NewBlockEnd.getValue()) +
1792         " overlaps " +
1793         formatv("{0:x16} -- {1:x16}", ExistingBlock.getAddress().getValue(),
1794                 ExistingBlockEnd.getValue()));
1795   }
1796 
1797   AddrToBlockMap AddrToBlock;
1798 };
1799 
1800 /// A map of addresses to Symbols.
1801 class SymbolAddressMap {
1802 public:
1803   using SymbolVector = SmallVector<Symbol *, 1>;
1804 
1805   /// Add a symbol to the SymbolAddressMap.
addSymbol(Symbol & Sym)1806   void addSymbol(Symbol &Sym) {
1807     AddrToSymbols[Sym.getAddress()].push_back(&Sym);
1808   }
1809 
1810   /// Add all symbols in a given range to the SymbolAddressMap.
1811   template <typename SymbolPtrCollection>
addSymbols(SymbolPtrCollection && Symbols)1812   void addSymbols(SymbolPtrCollection &&Symbols) {
1813     for (auto *Sym : Symbols)
1814       addSymbol(*Sym);
1815   }
1816 
1817   /// Returns the list of symbols that start at the given address, or nullptr if
1818   /// no such symbols exist.
getSymbolsAt(orc::ExecutorAddr Addr)1819   const SymbolVector *getSymbolsAt(orc::ExecutorAddr Addr) const {
1820     auto I = AddrToSymbols.find(Addr);
1821     if (I == AddrToSymbols.end())
1822       return nullptr;
1823     return &I->second;
1824   }
1825 
1826 private:
1827   std::map<orc::ExecutorAddr, SymbolVector> AddrToSymbols;
1828 };
1829 
1830 /// A function for mutating LinkGraphs.
1831 using LinkGraphPassFunction = unique_function<Error(LinkGraph &)>;
1832 
1833 /// A list of LinkGraph passes.
1834 using LinkGraphPassList = std::vector<LinkGraphPassFunction>;
1835 
1836 /// An LinkGraph pass configuration, consisting of a list of pre-prune,
1837 /// post-prune, and post-fixup passes.
1838 struct PassConfiguration {
1839 
1840   /// Pre-prune passes.
1841   ///
1842   /// These passes are called on the graph after it is built, and before any
1843   /// symbols have been pruned. Graph nodes still have their original vmaddrs.
1844   ///
1845   /// Notable use cases: Marking symbols live or should-discard.
1846   LinkGraphPassList PrePrunePasses;
1847 
1848   /// Post-prune passes.
1849   ///
1850   /// These passes are called on the graph after dead stripping, but before
1851   /// memory is allocated or nodes assigned their final addresses.
1852   ///
1853   /// Notable use cases: Building GOT, stub, and TLV symbols.
1854   LinkGraphPassList PostPrunePasses;
1855 
1856   /// Post-allocation passes.
1857   ///
1858   /// These passes are called on the graph after memory has been allocated and
1859   /// defined nodes have been assigned their final addresses, but before the
1860   /// context has been notified of these addresses. At this point externals
1861   /// have not been resolved, and symbol content has not yet been copied into
1862   /// working memory.
1863   ///
1864   /// Notable use cases: Setting up data structures associated with addresses
1865   /// of defined symbols (e.g. a mapping of __dso_handle to JITDylib* for the
1866   /// JIT runtime) -- using a PostAllocationPass for this ensures that the
1867   /// data structures are in-place before any query for resolved symbols
1868   /// can complete.
1869   LinkGraphPassList PostAllocationPasses;
1870 
1871   /// Pre-fixup passes.
1872   ///
1873   /// These passes are called on the graph after memory has been allocated,
1874   /// content copied into working memory, and all nodes (including externals)
1875   /// have been assigned their final addresses, but before any fixups have been
1876   /// applied.
1877   ///
1878   /// Notable use cases: Late link-time optimizations like GOT and stub
1879   /// elimination.
1880   LinkGraphPassList PreFixupPasses;
1881 
1882   /// Post-fixup passes.
1883   ///
1884   /// These passes are called on the graph after block contents has been copied
1885   /// to working memory, and fixups applied. Blocks have been updated to point
1886   /// to their fixed up content.
1887   ///
1888   /// Notable use cases: Testing and validation.
1889   LinkGraphPassList PostFixupPasses;
1890 };
1891 
1892 /// Flags for symbol lookup.
1893 ///
1894 /// FIXME: These basically duplicate orc::SymbolLookupFlags -- We should merge
1895 ///        the two types once we have an OrcSupport library.
1896 enum class SymbolLookupFlags { RequiredSymbol, WeaklyReferencedSymbol };
1897 
1898 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LF);
1899 
1900 /// A map of symbol names to resolved addresses.
1901 using AsyncLookupResult =
1902     DenseMap<orc::SymbolStringPtr, orc::ExecutorSymbolDef>;
1903 
1904 /// A function object to call with a resolved symbol map (See AsyncLookupResult)
1905 /// or an error if resolution failed.
1906 class LLVM_ABI JITLinkAsyncLookupContinuation {
1907 public:
1908   virtual ~JITLinkAsyncLookupContinuation() = default;
1909   virtual void run(Expected<AsyncLookupResult> LR) = 0;
1910 
1911 private:
1912   virtual void anchor();
1913 };
1914 
1915 /// Create a lookup continuation from a function object.
1916 template <typename Continuation>
1917 std::unique_ptr<JITLinkAsyncLookupContinuation>
createLookupContinuation(Continuation Cont)1918 createLookupContinuation(Continuation Cont) {
1919 
1920   class Impl final : public JITLinkAsyncLookupContinuation {
1921   public:
1922     Impl(Continuation C) : C(std::move(C)) {}
1923     void run(Expected<AsyncLookupResult> LR) override { C(std::move(LR)); }
1924 
1925   private:
1926     Continuation C;
1927   };
1928 
1929   return std::make_unique<Impl>(std::move(Cont));
1930 }
1931 
1932 /// Holds context for a single jitLink invocation.
1933 class LLVM_ABI JITLinkContext {
1934 public:
1935   using LookupMap = DenseMap<orc::SymbolStringPtr, SymbolLookupFlags>;
1936 
1937   /// Create a JITLinkContext.
JITLinkContext(const JITLinkDylib * JD)1938   JITLinkContext(const JITLinkDylib *JD) : JD(JD) {}
1939 
1940   /// Destroy a JITLinkContext.
1941   virtual ~JITLinkContext();
1942 
1943   /// Return the JITLinkDylib that this link is targeting, if any.
getJITLinkDylib()1944   const JITLinkDylib *getJITLinkDylib() const { return JD; }
1945 
1946   /// Return the MemoryManager to be used for this link.
1947   virtual JITLinkMemoryManager &getMemoryManager() = 0;
1948 
1949   /// Notify this context that linking failed.
1950   /// Called by JITLink if linking cannot be completed.
1951   virtual void notifyFailed(Error Err) = 0;
1952 
1953   /// Called by JITLink to resolve external symbols. This method is passed a
1954   /// lookup continutation which it must call with a result to continue the
1955   /// linking process.
1956   virtual void lookup(const LookupMap &Symbols,
1957                       std::unique_ptr<JITLinkAsyncLookupContinuation> LC) = 0;
1958 
1959   /// Called by JITLink once all defined symbols in the graph have been assigned
1960   /// their final memory locations in the target process. At this point the
1961   /// LinkGraph can be inspected to build a symbol table, however the block
1962   /// content will not generally have been copied to the target location yet.
1963   ///
1964   /// If the client detects an error in the LinkGraph state (e.g. unexpected or
1965   /// missing symbols) they may return an error here. The error will be
1966   /// propagated to notifyFailed and the linker will bail out.
1967   virtual Error notifyResolved(LinkGraph &G) = 0;
1968 
1969   /// Called by JITLink to notify the context that the object has been
1970   /// finalized (i.e. emitted to memory and memory permissions set). If all of
1971   /// this objects dependencies have also been finalized then the code is ready
1972   /// to run.
1973   virtual void notifyFinalized(JITLinkMemoryManager::FinalizedAlloc Alloc) = 0;
1974 
1975   /// Called by JITLink prior to linking to determine whether default passes for
1976   /// the target should be added. The default implementation returns true.
1977   /// If subclasses override this method to return false for any target then
1978   /// they are required to fully configure the pass pipeline for that target.
1979   virtual bool shouldAddDefaultTargetPasses(const Triple &TT) const;
1980 
1981   /// Returns the mark-live pass to be used for this link. If no pass is
1982   /// returned (the default) then the target-specific linker implementation will
1983   /// choose a conservative default (usually marking all symbols live).
1984   /// This function is only called if shouldAddDefaultTargetPasses returns true,
1985   /// otherwise the JITContext is responsible for adding a mark-live pass in
1986   /// modifyPassConfig.
1987   virtual LinkGraphPassFunction getMarkLivePass(const Triple &TT) const;
1988 
1989   /// Called by JITLink to modify the pass pipeline prior to linking.
1990   /// The default version performs no modification.
1991   virtual Error modifyPassConfig(LinkGraph &G, PassConfiguration &Config);
1992 
1993 private:
1994   const JITLinkDylib *JD = nullptr;
1995 };
1996 
1997 /// Marks all symbols in a graph live. This can be used as a default,
1998 /// conservative mark-live implementation.
1999 LLVM_ABI Error markAllSymbolsLive(LinkGraph &G);
2000 
2001 /// Create an out of range error for the given edge in the given block.
2002 LLVM_ABI Error makeTargetOutOfRangeError(const LinkGraph &G, const Block &B,
2003                                          const Edge &E);
2004 
2005 LLVM_ABI Error makeAlignmentError(llvm::orc::ExecutorAddr Loc, uint64_t Value,
2006                                   int N, const Edge &E);
2007 
2008 /// Creates a new pointer block in the given section and returns an
2009 /// Anonymous symbol pointing to it.
2010 ///
2011 /// The pointer block will have the following default values:
2012 ///   alignment: PointerSize
2013 ///   alignment-offset: 0
2014 ///   address: highest allowable
2015 using AnonymousPointerCreator =
2016     unique_function<Symbol &(LinkGraph &G, Section &PointerSection,
2017                              Symbol *InitialTarget, uint64_t InitialAddend)>;
2018 
2019 /// Get target-specific AnonymousPointerCreator
2020 LLVM_ABI AnonymousPointerCreator getAnonymousPointerCreator(const Triple &TT);
2021 
2022 /// Create a jump stub that jumps via the pointer at the given symbol and
2023 /// an anonymous symbol pointing to it. Return the anonymous symbol.
2024 ///
2025 /// The stub block will be created by createPointerJumpStubBlock.
2026 using PointerJumpStubCreator = unique_function<Symbol &(
2027     LinkGraph &G, Section &StubSection, Symbol &PointerSymbol)>;
2028 
2029 /// Get target-specific PointerJumpStubCreator
2030 LLVM_ABI PointerJumpStubCreator getPointerJumpStubCreator(const Triple &TT);
2031 
2032 /// Base case for edge-visitors where the visitor-list is empty.
visitEdge(LinkGraph & G,Block * B,Edge & E)2033 inline void visitEdge(LinkGraph &G, Block *B, Edge &E) {}
2034 
2035 /// Applies the first visitor in the list to the given edge. If the visitor's
2036 /// visitEdge method returns true then we return immediately, otherwise we
2037 /// apply the next visitor.
2038 template <typename VisitorT, typename... VisitorTs>
visitEdge(LinkGraph & G,Block * B,Edge & E,VisitorT && V,VisitorTs &&...Vs)2039 void visitEdge(LinkGraph &G, Block *B, Edge &E, VisitorT &&V,
2040                VisitorTs &&...Vs) {
2041   if (!V.visitEdge(G, B, E))
2042     visitEdge(G, B, E, std::forward<VisitorTs>(Vs)...);
2043 }
2044 
2045 /// For each edge in the given graph, apply a list of visitors to the edge,
2046 /// stopping when the first visitor's visitEdge method returns true.
2047 ///
2048 /// Only visits edges that were in the graph at call time: if any visitor
2049 /// adds new edges those will not be visited. Visitors are not allowed to
2050 /// remove edges (though they can change their kind, target, and addend).
2051 template <typename... VisitorTs>
visitExistingEdges(LinkGraph & G,VisitorTs &&...Vs)2052 void visitExistingEdges(LinkGraph &G, VisitorTs &&...Vs) {
2053   // We may add new blocks during this process, but we don't want to iterate
2054   // over them, so build a worklist.
2055   std::vector<Block *> Worklist(G.blocks().begin(), G.blocks().end());
2056 
2057   for (auto *B : Worklist)
2058     for (auto &E : B->edges())
2059       visitEdge(G, B, E, std::forward<VisitorTs>(Vs)...);
2060 }
2061 
2062 /// Create a LinkGraph from the given object buffer.
2063 ///
2064 /// Note: The graph does not take ownership of the underlying buffer, nor copy
2065 /// its contents. The caller is responsible for ensuring that the object buffer
2066 /// outlives the graph.
2067 LLVM_ABI Expected<std::unique_ptr<LinkGraph>>
2068 createLinkGraphFromObject(MemoryBufferRef ObjectBuffer,
2069                           std::shared_ptr<orc::SymbolStringPool> SSP);
2070 
2071 /// Create a \c LinkGraph defining the given absolute symbols.
2072 LLVM_ABI std::unique_ptr<LinkGraph>
2073 absoluteSymbolsLinkGraph(Triple TT, std::shared_ptr<orc::SymbolStringPool> SSP,
2074                          orc::SymbolMap Symbols);
2075 
2076 /// Link the given graph.
2077 LLVM_ABI void link(std::unique_ptr<LinkGraph> G,
2078                    std::unique_ptr<JITLinkContext> Ctx);
2079 
2080 } // end namespace jitlink
2081 } // end namespace llvm
2082 
2083 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
2084