1 //===------------ JITLink.h - JIT linker functionality ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Contains generic JIT-linker types.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H
16 #include "JITLinkMemoryManager.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/ExecutionEngine/JITSymbol.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/FormatVariadic.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/Memory.h"
28 #include "llvm/Support/MemoryBuffer.h"
32 #include <system_error>
40 /// Base class for errors originating in JIT linker, e.g. missing relocation
42 class JITLinkError
: public ErrorInfo
<JITLinkError
> {
46 JITLinkError(Twine ErrMsg
) : ErrMsg(ErrMsg
.str()) {}
48 void log(raw_ostream
&OS
) const override
;
49 const std::string
&getErrorMessage() const { return ErrMsg
; }
50 std::error_code
convertToErrorCode() const override
;
56 /// Represents fixups and constraints in the LinkGraph.
61 enum GenericEdgeKind
: Kind
{
62 Invalid
, // Invalid edge value.
63 FirstKeepAlive
, // Keeps target alive. Offset/addend zero.
64 KeepAlive
= FirstKeepAlive
, // Tag first edge kind that preserves liveness.
65 FirstRelocation
// First architecture specific relocation.
68 using OffsetT
= uint32_t;
69 using AddendT
= int64_t;
71 Edge(Kind K
, OffsetT Offset
, Symbol
&Target
, AddendT Addend
)
72 : Target(&Target
), Offset(Offset
), Addend(Addend
), K(K
) {}
74 OffsetT
getOffset() const { return Offset
; }
75 Kind
getKind() const { return K
; }
76 void setKind(Kind K
) { this->K
= K
; }
77 bool isRelocation() const { return K
>= FirstRelocation
; }
78 Kind
getRelocation() const {
79 assert(isRelocation() && "Not a relocation edge");
80 return K
- FirstRelocation
;
82 bool isKeepAlive() const { return K
>= FirstKeepAlive
; }
83 Symbol
&getTarget() const { return *Target
; }
84 void setTarget(Symbol
&Target
) { this->Target
= &Target
; }
85 AddendT
getAddend() const { return Addend
; }
86 void setAddend(AddendT Addend
) { this->Addend
= Addend
; }
89 Symbol
*Target
= nullptr;
95 /// Returns the string name of the given generic edge kind, or "unknown"
96 /// otherwise. Useful for debugging.
97 const char *getGenericEdgeKindName(Edge::Kind K
);
99 /// Base class for Addressable entities (externals, absolutes, blocks).
101 friend class LinkGraph
;
104 Addressable(JITTargetAddress Address
, bool IsDefined
)
105 : Address(Address
), IsDefined(IsDefined
), IsAbsolute(false) {}
107 Addressable(JITTargetAddress Address
)
108 : Address(Address
), IsDefined(false), IsAbsolute(true) {
109 assert(!(IsDefined
&& IsAbsolute
) &&
110 "Block cannot be both defined and absolute");
114 Addressable(const Addressable
&) = delete;
115 Addressable
&operator=(const Addressable
&) = default;
116 Addressable(Addressable
&&) = delete;
117 Addressable
&operator=(Addressable
&&) = default;
119 JITTargetAddress
getAddress() const { return Address
; }
120 void setAddress(JITTargetAddress Address
) { this->Address
= Address
; }
122 /// Returns true if this is a defined addressable, in which case you
123 /// can downcast this to a .
124 bool isDefined() const { return static_cast<bool>(IsDefined
); }
125 bool isAbsolute() const { return static_cast<bool>(IsAbsolute
); }
128 JITTargetAddress Address
= 0;
129 uint64_t IsDefined
: 1;
130 uint64_t IsAbsolute
: 1;
133 using BlockOrdinal
= unsigned;
134 using SectionOrdinal
= unsigned;
136 /// An Addressable with content and edges.
137 class Block
: public Addressable
{
138 friend class LinkGraph
;
141 /// Create a zero-fill defined addressable.
142 Block(Section
&Parent
, BlockOrdinal Ordinal
, JITTargetAddress Size
,
143 JITTargetAddress Address
, uint64_t Alignment
, uint64_t AlignmentOffset
)
144 : Addressable(Address
, true), Parent(Parent
), Size(Size
),
146 assert(isPowerOf2_64(Alignment
) && "Alignment must be power of 2");
147 assert(AlignmentOffset
< Alignment
&&
148 "Alignment offset cannot exceed alignment");
149 assert(AlignmentOffset
<= MaxAlignmentOffset
&&
150 "Alignment offset exceeds maximum");
151 P2Align
= Alignment
? countTrailingZeros(Alignment
) : 0;
152 this->AlignmentOffset
= AlignmentOffset
;
155 /// Create a defined addressable for the given content.
156 Block(Section
&Parent
, BlockOrdinal Ordinal
, StringRef Content
,
157 JITTargetAddress Address
, uint64_t Alignment
, uint64_t AlignmentOffset
)
158 : Addressable(Address
, true), Parent(Parent
), Data(Content
.data()),
159 Size(Content
.size()), Ordinal(Ordinal
) {
160 assert(isPowerOf2_64(Alignment
) && "Alignment must be power of 2");
161 assert(AlignmentOffset
< Alignment
&&
162 "Alignment offset cannot exceed alignment");
163 assert(AlignmentOffset
<= MaxAlignmentOffset
&&
164 "Alignment offset exceeds maximum");
165 P2Align
= Alignment
? countTrailingZeros(Alignment
) : 0;
166 this->AlignmentOffset
= AlignmentOffset
;
170 using EdgeVector
= std::vector
<Edge
>;
171 using edge_iterator
= EdgeVector::iterator
;
172 using const_edge_iterator
= EdgeVector::const_iterator
;
174 Block(const Block
&) = delete;
175 Block
&operator=(const Block
&) = delete;
176 Block(Block
&&) = delete;
177 Block
&operator=(Block
&&) = delete;
179 /// Return the parent section for this block.
180 Section
&getSection() const { return Parent
; }
182 /// Return the ordinal for this block.
183 BlockOrdinal
getOrdinal() const { return Ordinal
; }
185 /// Returns true if this is a zero-fill block.
187 /// If true, getSize is callable but getContent is not (the content is
188 /// defined to be a sequence of zero bytes of length Size).
189 bool isZeroFill() const { return !Data
; }
191 /// Returns the size of this defined addressable.
192 size_t getSize() const { return Size
; }
194 /// Get the content for this block. Block must not be a zero-fill block.
195 StringRef
getContent() const {
196 assert(Data
&& "Section does not contain content");
197 return StringRef(Data
, Size
);
200 /// Set the content for this block.
201 /// Caller is responsible for ensuring the underlying bytes are not
202 /// deallocated while pointed to by this block.
203 void setContent(StringRef Content
) {
204 Data
= Content
.data();
205 Size
= Content
.size();
208 /// Get the alignment for this content.
209 uint64_t getAlignment() const { return 1ull << P2Align
; }
211 /// Get the alignment offset for this content.
212 uint64_t getAlignmentOffset() const { return AlignmentOffset
; }
214 /// Add an edge to this block.
215 void addEdge(Edge::Kind K
, Edge::OffsetT Offset
, Symbol
&Target
,
216 Edge::AddendT Addend
) {
217 Edges
.push_back(Edge(K
, Offset
, Target
, Addend
));
220 /// Return the list of edges attached to this content.
221 iterator_range
<edge_iterator
> edges() {
222 return make_range(Edges
.begin(), Edges
.end());
225 /// Returns the list of edges attached to this content.
226 iterator_range
<const_edge_iterator
> edges() const {
227 return make_range(Edges
.begin(), Edges
.end());
230 /// Return the size of the edges list.
231 size_t edges_size() const { return Edges
.size(); }
233 /// Returns true if the list of edges is empty.
234 bool edges_empty() const { return Edges
.empty(); }
237 static constexpr uint64_t MaxAlignmentOffset
= (1ULL << 57) - 1;
239 uint64_t P2Align
: 5;
240 uint64_t AlignmentOffset
: 57;
242 const char *Data
= nullptr;
244 BlockOrdinal Ordinal
= 0;
245 std::vector
<Edge
> Edges
;
248 /// Describes symbol linkage. This can be used to make resolve definition
250 enum class Linkage
: uint8_t {
255 /// For errors and debugging output.
256 const char *getLinkageName(Linkage L
);
258 /// Defines the scope in which this symbol should be visible:
259 /// Default -- Visible in the public interface of the linkage unit.
260 /// Hidden -- Visible within the linkage unit, but not exported from it.
261 /// Local -- Visible only within the LinkGraph.
262 enum class Scope
: uint8_t { Default
, Hidden
, Local
};
264 /// For debugging output.
265 const char *getScopeName(Scope S
);
267 raw_ostream
&operator<<(raw_ostream
&OS
, const Block
&B
);
269 /// Symbol representation.
271 /// Symbols represent locations within Addressable objects.
272 /// They can be either Named or Anonymous.
273 /// Anonymous symbols have neither linkage nor visibility, and must point at
275 /// Named symbols may be in one of four states:
276 /// - Null: Default initialized. Assignable, but otherwise unusable.
277 /// - Defined: Has both linkage and visibility and points to a ContentBlock
278 /// - Common: Has both linkage and visibility, points to a null Addressable.
279 /// - External: Has neither linkage nor visibility, points to an external
283 friend class LinkGraph
;
286 Symbol(Addressable
&Base
, JITTargetAddress Offset
, StringRef Name
,
287 JITTargetAddress Size
, Linkage L
, Scope S
, bool IsLive
,
289 : Name(Name
), Base(&Base
), Offset(Offset
), Size(Size
) {
293 setCallable(IsCallable
);
296 static Symbol
&constructCommon(void *SymStorage
, Block
&Base
, StringRef Name
,
297 JITTargetAddress Size
, Scope S
, bool IsLive
) {
298 assert(SymStorage
&& "Storage cannot be null");
299 assert(!Name
.empty() && "Common symbol name cannot be empty");
300 assert(Base
.isDefined() &&
301 "Cannot create common symbol from undefined block");
302 assert(static_cast<Block
&>(Base
).getSize() == Size
&&
303 "Common symbol size should match underlying block size");
304 auto *Sym
= reinterpret_cast<Symbol
*>(SymStorage
);
305 new (Sym
) Symbol(Base
, 0, Name
, Size
, Linkage::Weak
, S
, IsLive
, false);
309 static Symbol
&constructExternal(void *SymStorage
, Addressable
&Base
,
310 StringRef Name
, JITTargetAddress Size
) {
311 assert(SymStorage
&& "Storage cannot be null");
312 assert(!Base
.isDefined() &&
313 "Cannot create external symbol from defined block");
314 assert(!Name
.empty() && "External symbol name cannot be empty");
315 auto *Sym
= reinterpret_cast<Symbol
*>(SymStorage
);
316 new (Sym
) Symbol(Base
, 0, Name
, Size
, Linkage::Strong
, Scope::Default
,
321 static Symbol
&constructAbsolute(void *SymStorage
, Addressable
&Base
,
322 StringRef Name
, JITTargetAddress Size
,
323 Linkage L
, Scope S
, bool IsLive
) {
324 assert(SymStorage
&& "Storage cannot be null");
325 assert(!Base
.isDefined() &&
326 "Cannot create absolute symbol from a defined block");
327 auto *Sym
= reinterpret_cast<Symbol
*>(SymStorage
);
328 new (Sym
) Symbol(Base
, 0, Name
, Size
, L
, S
, IsLive
, false);
332 static Symbol
&constructAnonDef(void *SymStorage
, Block
&Base
,
333 JITTargetAddress Offset
,
334 JITTargetAddress Size
, bool IsCallable
,
336 assert(SymStorage
&& "Storage cannot be null");
337 auto *Sym
= reinterpret_cast<Symbol
*>(SymStorage
);
338 new (Sym
) Symbol(Base
, Offset
, StringRef(), Size
, Linkage::Strong
,
339 Scope::Local
, IsLive
, IsCallable
);
343 static Symbol
&constructNamedDef(void *SymStorage
, Block
&Base
,
344 JITTargetAddress Offset
, StringRef Name
,
345 JITTargetAddress Size
, Linkage L
, Scope S
,
346 bool IsLive
, bool IsCallable
) {
347 assert(SymStorage
&& "Storage cannot be null");
348 assert(!Name
.empty() && "Name cannot be empty");
349 auto *Sym
= reinterpret_cast<Symbol
*>(SymStorage
);
350 new (Sym
) Symbol(Base
, Offset
, Name
, Size
, L
, S
, IsLive
, IsCallable
);
355 /// Create a null Symbol. This allows Symbols to be default initialized for
356 /// use in containers (e.g. as map values). Null symbols are only useful for
360 // Symbols are not movable or copyable.
361 Symbol(const Symbol
&) = delete;
362 Symbol
&operator=(const Symbol
&) = delete;
363 Symbol(Symbol
&&) = delete;
364 Symbol
&operator=(Symbol
&&) = delete;
366 /// Returns true if this symbol has a name.
367 bool hasName() const { return !Name
.empty(); }
369 /// Returns the name of this symbol (empty if the symbol is anonymous).
370 StringRef
getName() const {
371 assert((!Name
.empty() || getScope() == Scope::Local
) &&
372 "Anonymous symbol has non-local scope");
376 /// Returns true if this Symbol has content (potentially) defined within this
377 /// object file (i.e. is anything but an external or absolute symbol).
378 bool isDefined() const {
379 assert(Base
&& "Attempt to access null symbol");
380 return Base
->isDefined();
383 /// Returns true if this symbol is live (i.e. should be treated as a root for
385 bool isLive() const {
386 assert(Base
&& "Attempting to access null symbol");
390 /// Set this symbol's live bit.
391 void setLive(bool IsLive
) { this->IsLive
= IsLive
; }
393 /// Returns true is this symbol is callable.
394 bool isCallable() const { return IsCallable
; }
396 /// Set this symbol's callable bit.
397 void setCallable(bool IsCallable
) { this->IsCallable
= IsCallable
; }
399 /// Returns true if the underlying addressable is an unresolved external.
400 bool isExternal() const {
401 assert(Base
&& "Attempt to access null symbol");
402 return !Base
->isDefined() && !Base
->isAbsolute();
405 /// Returns true if the underlying addressable is an absolute symbol.
406 bool isAbsolute() const {
407 assert(Base
&& "Attempt to access null symbol");
408 return !Base
->isDefined() && Base
->isAbsolute();
411 /// Return the addressable that this symbol points to.
412 Addressable
&getAddressable() {
413 assert(Base
&& "Cannot get underlying addressable for null symbol");
417 /// Return the addressable that thsi symbol points to.
418 const Addressable
&getAddressable() const {
419 assert(Base
&& "Cannot get underlying addressable for null symbol");
423 /// Return the Block for this Symbol (Symbol must be defined).
425 assert(Base
&& "Cannot get block for null symbol");
426 assert(Base
->isDefined() && "Not a defined symbol");
427 return static_cast<Block
&>(*Base
);
430 /// Return the Block for this Symbol (Symbol must be defined).
431 const Block
&getBlock() const {
432 assert(Base
&& "Cannot get block for null symbol");
433 assert(Base
->isDefined() && "Not a defined symbol");
434 return static_cast<const Block
&>(*Base
);
437 /// Returns the offset for this symbol within the underlying addressable.
438 JITTargetAddress
getOffset() const { return Offset
; }
440 /// Returns the address of this symbol.
441 JITTargetAddress
getAddress() const { return Base
->getAddress() + Offset
; }
443 /// Returns the size of this symbol.
444 JITTargetAddress
getSize() const { return Size
; }
446 /// Returns true if this symbol is backed by a zero-fill block.
447 /// This method may only be called on defined symbols.
448 bool isSymbolZeroFill() const { return getBlock().isZeroFill(); }
450 /// Returns the content in the underlying block covered by this symbol.
451 /// This method may only be called on defined non-zero-fill symbols.
452 StringRef
getSymbolContent() const {
453 return getBlock().getContent().substr(Offset
, Size
);
456 /// Get the linkage for this Symbol.
457 Linkage
getLinkage() const { return static_cast<Linkage
>(L
); }
459 /// Set the linkage for this Symbol.
460 void setLinkage(Linkage L
) {
461 assert((L
== Linkage::Strong
|| (Base
->isDefined() && !Name
.empty())) &&
462 "Linkage can only be applied to defined named symbols");
463 this->L
= static_cast<uint8_t>(L
);
466 /// Get the visibility for this Symbol.
467 Scope
getScope() const { return static_cast<Scope
>(S
); }
469 /// Set the visibility for this Symbol.
470 void setScope(Scope S
) {
471 assert((S
== Scope::Default
|| Base
->isDefined() || Base
->isAbsolute()) &&
472 "Invalid visibility for symbol type");
473 this->S
= static_cast<uint8_t>(S
);
477 void makeExternal(Addressable
&A
) {
478 assert(!A
.isDefined() && "Attempting to make external with defined block");
481 setLinkage(Linkage::Strong
);
482 setScope(Scope::Default
);
484 // note: Size and IsCallable fields left unchanged.
487 static constexpr uint64_t MaxOffset
= (1ULL << 59) - 1;
489 // FIXME: A char* or SymbolStringPtr may pack better.
491 Addressable
*Base
= nullptr;
492 uint64_t Offset
: 59;
496 uint64_t IsCallable
: 1;
497 JITTargetAddress Size
= 0;
500 raw_ostream
&operator<<(raw_ostream
&OS
, const Symbol
&A
);
502 void printEdge(raw_ostream
&OS
, const Block
&B
, const Edge
&E
,
503 StringRef EdgeKindName
);
505 /// Represents an object file section.
507 friend class LinkGraph
;
510 Section(StringRef Name
, sys::Memory::ProtectionFlags Prot
,
511 SectionOrdinal SecOrdinal
)
512 : Name(Name
), Prot(Prot
), SecOrdinal(SecOrdinal
) {}
514 using SymbolSet
= DenseSet
<Symbol
*>;
515 using BlockSet
= DenseSet
<Block
*>;
518 using symbol_iterator
= SymbolSet::iterator
;
519 using const_symbol_iterator
= SymbolSet::const_iterator
;
521 using block_iterator
= BlockSet::iterator
;
522 using const_block_iterator
= BlockSet::const_iterator
;
526 /// Returns the name of this section.
527 StringRef
getName() const { return Name
; }
529 /// Returns the protection flags for this section.
530 sys::Memory::ProtectionFlags
getProtectionFlags() const { return Prot
; }
532 /// Returns the ordinal for this section.
533 SectionOrdinal
getOrdinal() const { return SecOrdinal
; }
535 /// Returns an iterator over the symbols defined in this section.
536 iterator_range
<symbol_iterator
> symbols() {
537 return make_range(Symbols
.begin(), Symbols
.end());
540 /// Returns an iterator over the symbols defined in this section.
541 iterator_range
<const_symbol_iterator
> symbols() const {
542 return make_range(Symbols
.begin(), Symbols
.end());
545 /// Return the number of symbols in this section.
546 SymbolSet::size_type
symbols_size() { return Symbols
.size(); }
548 /// Return true if this section contains no symbols.
549 bool symbols_empty() const { return Symbols
.empty(); }
551 /// Returns the ordinal for the next block.
552 BlockOrdinal
getNextBlockOrdinal() { return NextBlockOrdinal
++; }
555 void addSymbol(Symbol
&Sym
) {
556 assert(!Symbols
.count(&Sym
) && "Symbol is already in this section");
557 Symbols
.insert(&Sym
);
560 void removeSymbol(Symbol
&Sym
) {
561 assert(Symbols
.count(&Sym
) && "symbol is not in this section");
566 sys::Memory::ProtectionFlags Prot
;
567 SectionOrdinal SecOrdinal
= 0;
568 BlockOrdinal NextBlockOrdinal
= 0;
572 /// Represents a section address range via a pair of Block pointers
573 /// to the first and last Blocks in the section.
576 SectionRange() = default;
577 SectionRange(const Section
&Sec
) {
578 if (Sec
.symbols_empty())
580 First
= Last
= *Sec
.symbols().begin();
581 for (auto *Sym
: Sec
.symbols()) {
582 if (Sym
->getAddress() < First
->getAddress())
584 if (Sym
->getAddress() > Last
->getAddress())
588 Symbol
*getFirstSymbol() const {
589 assert((!Last
|| First
) && "First can not be null if end is non-null");
592 Symbol
*getLastSymbol() const {
593 assert((First
|| !Last
) && "Last can not be null if start is non-null");
596 bool isEmpty() const {
597 assert((First
|| !Last
) && "Last can not be null if start is non-null");
600 JITTargetAddress
getStart() const {
601 return First
? First
->getBlock().getAddress() : 0;
603 JITTargetAddress
getEnd() const {
604 return Last
? Last
->getBlock().getAddress() + Last
->getBlock().getSize()
607 uint64_t getSize() const { return getEnd() - getStart(); }
610 Symbol
*First
= nullptr;
611 Symbol
*Last
= nullptr;
616 using SectionList
= std::vector
<std::unique_ptr
<Section
>>;
617 using ExternalSymbolSet
= DenseSet
<Symbol
*>;
618 using BlockSet
= DenseSet
<Block
*>;
620 template <typename
... ArgTs
>
621 Addressable
&createAddressable(ArgTs
&&... Args
) {
623 reinterpret_cast<Addressable
*>(Allocator
.Allocate
<Addressable
>());
624 new (A
) Addressable(std::forward
<ArgTs
>(Args
)...);
628 void destroyAddressable(Addressable
&A
) {
630 Allocator
.Deallocate(&A
);
633 template <typename
... ArgTs
> Block
&createBlock(ArgTs
&&... Args
) {
634 Block
*B
= reinterpret_cast<Block
*>(Allocator
.Allocate
<Block
>());
635 new (B
) Block(std::forward
<ArgTs
>(Args
)...);
640 void destroyBlock(Block
&B
) {
643 Allocator
.Deallocate(&B
);
646 void destroySymbol(Symbol
&S
) {
648 Allocator
.Deallocate(&S
);
652 using external_symbol_iterator
= ExternalSymbolSet::iterator
;
654 using block_iterator
= BlockSet::iterator
;
656 using section_iterator
= pointee_iterator
<SectionList::iterator
>;
657 using const_section_iterator
= pointee_iterator
<SectionList::const_iterator
>;
659 template <typename SectionItrT
, typename SymbolItrT
, typename T
>
660 class defined_symbol_iterator_impl
661 : public iterator_facade_base
<
662 defined_symbol_iterator_impl
<SectionItrT
, SymbolItrT
, T
>,
663 std::forward_iterator_tag
, T
> {
665 defined_symbol_iterator_impl() = default;
667 defined_symbol_iterator_impl(SectionItrT SecI
, SectionItrT SecE
)
668 : SecI(SecI
), SecE(SecE
),
669 SymI(SecI
!= SecE
? SecI
->symbols().begin() : SymbolItrT()) {
670 moveToNextSymbolOrEnd();
673 bool operator==(const defined_symbol_iterator_impl
&RHS
) const {
674 return (SecI
== RHS
.SecI
) && (SymI
== RHS
.SymI
);
677 T
operator*() const {
678 assert(SymI
!= SecI
->symbols().end() && "Dereferencing end?");
682 defined_symbol_iterator_impl
operator++() {
684 moveToNextSymbolOrEnd();
689 void moveToNextSymbolOrEnd() {
690 while (SecI
!= SecE
&& SymI
== SecI
->symbols().end()) {
692 SymI
= SecI
== SecE
? SymbolItrT() : SecI
->symbols().begin();
696 SectionItrT SecI
, SecE
;
700 using defined_symbol_iterator
=
701 defined_symbol_iterator_impl
<const_section_iterator
,
702 Section::symbol_iterator
, Symbol
*>;
704 using const_defined_symbol_iterator
= defined_symbol_iterator_impl
<
705 const_section_iterator
, Section::const_symbol_iterator
, const Symbol
*>;
707 LinkGraph(std::string Name
, unsigned PointerSize
,
708 support::endianness Endianness
)
709 : Name(std::move(Name
)), PointerSize(PointerSize
),
710 Endianness(Endianness
) {}
714 /// Returns the name of this graph (usually the name of the original
715 /// underlying MemoryBuffer).
716 const std::string
&getName() { return Name
; }
718 /// Returns the pointer size for use in this graph.
719 unsigned getPointerSize() const { return PointerSize
; }
721 /// Returns the endianness of content in this graph.
722 support::endianness
getEndianness() const { return Endianness
; }
724 /// Create a section with the given name, protection flags, and alignment.
725 Section
&createSection(StringRef Name
, sys::Memory::ProtectionFlags Prot
) {
726 std::unique_ptr
<Section
> Sec(new Section(Name
, Prot
, Sections
.size()));
727 Sections
.push_back(std::move(Sec
));
728 return *Sections
.back();
731 /// Create a content block.
732 Block
&createContentBlock(Section
&Parent
, StringRef Content
,
733 uint64_t Address
, uint64_t Alignment
,
734 uint64_t AlignmentOffset
) {
735 return createBlock(Parent
, Parent
.getNextBlockOrdinal(), Content
, Address
,
736 Alignment
, AlignmentOffset
);
739 /// Create a zero-fill block.
740 Block
&createZeroFillBlock(Section
&Parent
, uint64_t Size
, uint64_t Address
,
741 uint64_t Alignment
, uint64_t AlignmentOffset
) {
742 return createBlock(Parent
, Parent
.getNextBlockOrdinal(), Size
, Address
,
743 Alignment
, AlignmentOffset
);
746 /// Add an external symbol.
747 /// Some formats (e.g. ELF) allow Symbols to have sizes. For Symbols whose
748 /// size is not known, you should substitute '0'.
749 Symbol
&addExternalSymbol(StringRef Name
, uint64_t Size
) {
750 auto &Sym
= Symbol::constructExternal(
751 Allocator
.Allocate
<Symbol
>(), createAddressable(0, false), Name
, Size
);
752 ExternalSymbols
.insert(&Sym
);
756 /// Add an absolute symbol.
757 Symbol
&addAbsoluteSymbol(StringRef Name
, JITTargetAddress Address
,
758 uint64_t Size
, Linkage L
, Scope S
, bool IsLive
) {
759 auto &Sym
= Symbol::constructAbsolute(Allocator
.Allocate
<Symbol
>(),
760 createAddressable(Address
), Name
,
762 AbsoluteSymbols
.insert(&Sym
);
766 /// Convenience method for adding a weak zero-fill symbol.
767 Symbol
&addCommonSymbol(StringRef Name
, Scope S
, Section
&Section
,
768 JITTargetAddress Address
, uint64_t Size
,
769 uint64_t Alignment
, bool IsLive
) {
770 auto &Sym
= Symbol::constructCommon(
771 Allocator
.Allocate
<Symbol
>(),
772 createBlock(Section
, Section
.getNextBlockOrdinal(), Address
, Size
,
774 Name
, Size
, S
, IsLive
);
775 Section
.addSymbol(Sym
);
779 /// Add an anonymous symbol.
780 Symbol
&addAnonymousSymbol(Block
&Content
, JITTargetAddress Offset
,
781 JITTargetAddress Size
, bool IsCallable
,
783 auto &Sym
= Symbol::constructAnonDef(Allocator
.Allocate
<Symbol
>(), Content
,
784 Offset
, Size
, IsCallable
, IsLive
);
785 Content
.getSection().addSymbol(Sym
);
789 /// Add a named symbol.
790 Symbol
&addDefinedSymbol(Block
&Content
, JITTargetAddress Offset
,
791 StringRef Name
, JITTargetAddress Size
, Linkage L
,
792 Scope S
, bool IsCallable
, bool IsLive
) {
794 Symbol::constructNamedDef(Allocator
.Allocate
<Symbol
>(), Content
, Offset
,
795 Name
, Size
, L
, S
, IsLive
, IsCallable
);
796 Content
.getSection().addSymbol(Sym
);
800 iterator_range
<section_iterator
> sections() {
801 return make_range(section_iterator(Sections
.begin()),
802 section_iterator(Sections
.end()));
805 /// Returns the section with the given name if it exists, otherwise returns
807 Section
*findSectionByName(StringRef Name
) {
808 for (auto &S
: sections())
809 if (S
.getName() == Name
)
814 iterator_range
<external_symbol_iterator
> external_symbols() {
815 return make_range(ExternalSymbols
.begin(), ExternalSymbols
.end());
818 iterator_range
<external_symbol_iterator
> absolute_symbols() {
819 return make_range(AbsoluteSymbols
.begin(), AbsoluteSymbols
.end());
822 iterator_range
<defined_symbol_iterator
> defined_symbols() {
823 return make_range(defined_symbol_iterator(Sections
.begin(), Sections
.end()),
824 defined_symbol_iterator(Sections
.end(), Sections
.end()));
827 iterator_range
<const_defined_symbol_iterator
> defined_symbols() const {
829 const_defined_symbol_iterator(Sections
.begin(), Sections
.end()),
830 const_defined_symbol_iterator(Sections
.end(), Sections
.end()));
833 iterator_range
<block_iterator
> blocks() {
834 return make_range(Blocks
.begin(), Blocks
.end());
837 /// Turn a defined symbol into an external one.
838 void makeExternal(Symbol
&Sym
) {
839 if (Sym
.getAddressable().isAbsolute()) {
840 assert(AbsoluteSymbols
.count(&Sym
) &&
841 "Sym is not in the absolute symbols set");
842 AbsoluteSymbols
.erase(&Sym
);
844 assert(Sym
.isDefined() && "Sym is not a defined symbol");
845 Section
&Sec
= Sym
.getBlock().getSection();
846 Sec
.removeSymbol(Sym
);
848 Sym
.makeExternal(createAddressable(false));
849 ExternalSymbols
.insert(&Sym
);
852 /// Removes an external symbol. Also removes the underlying Addressable.
853 void removeExternalSymbol(Symbol
&Sym
) {
854 assert(!Sym
.isDefined() && !Sym
.isAbsolute() &&
855 "Sym is not an external symbol");
856 assert(ExternalSymbols
.count(&Sym
) && "Symbol is not in the externals set");
857 ExternalSymbols
.erase(&Sym
);
858 Addressable
&Base
= *Sym
.Base
;
860 destroyAddressable(Base
);
863 /// Remove an absolute symbol. Also removes the underlying Addressable.
864 void removeAbsoluteSymbol(Symbol
&Sym
) {
865 assert(!Sym
.isDefined() && Sym
.isAbsolute() &&
866 "Sym is not an absolute symbol");
867 assert(AbsoluteSymbols
.count(&Sym
) &&
868 "Symbol is not in the absolute symbols set");
869 AbsoluteSymbols
.erase(&Sym
);
870 Addressable
&Base
= *Sym
.Base
;
872 destroyAddressable(Base
);
875 /// Removes defined symbols. Does not remove the underlying block.
876 void removeDefinedSymbol(Symbol
&Sym
) {
877 assert(Sym
.isDefined() && "Sym is not a defined symbol");
878 Sym
.getBlock().getSection().removeSymbol(Sym
);
883 void removeBlock(Block
&B
) {
890 /// If supplied, the EdgeKindToName function will be used to name edge
891 /// kinds in the debug output. Otherwise raw edge kind numbers will be
893 void dump(raw_ostream
&OS
,
894 std::function
<StringRef(Edge::Kind
)> EdegKindToName
=
895 std::function
<StringRef(Edge::Kind
)>());
898 // Put the BumpPtrAllocator first so that we don't free any of the underlying
899 // memory until the Symbol/Addressable destructors have been run.
900 BumpPtrAllocator Allocator
;
903 unsigned PointerSize
;
904 support::endianness Endianness
;
906 SectionList Sections
;
907 ExternalSymbolSet ExternalSymbols
;
908 ExternalSymbolSet AbsoluteSymbols
;
911 /// A function for mutating LinkGraphs.
912 using LinkGraphPassFunction
= std::function
<Error(LinkGraph
&)>;
914 /// A list of LinkGraph passes.
915 using LinkGraphPassList
= std::vector
<LinkGraphPassFunction
>;
917 /// An LinkGraph pass configuration, consisting of a list of pre-prune,
918 /// post-prune, and post-fixup passes.
919 struct PassConfiguration
{
921 /// Pre-prune passes.
923 /// These passes are called on the graph after it is built, and before any
924 /// symbols have been pruned.
926 /// Notable use cases: Marking symbols live or should-discard.
927 LinkGraphPassList PrePrunePasses
;
929 /// Post-prune passes.
931 /// These passes are called on the graph after dead stripping, but before
932 /// fixups are applied.
934 /// Notable use cases: Building GOT, stub, and TLV symbols.
935 LinkGraphPassList PostPrunePasses
;
937 /// Post-fixup passes.
939 /// These passes are called on the graph after block contents has been copied
940 /// to working memory, and fixups applied.
942 /// Notable use cases: Testing and validation.
943 LinkGraphPassList PostFixupPasses
;
946 /// A map of symbol names to resolved addresses.
947 using AsyncLookupResult
= DenseMap
<StringRef
, JITEvaluatedSymbol
>;
949 /// A function object to call with a resolved symbol map (See AsyncLookupResult)
950 /// or an error if resolution failed.
951 class JITLinkAsyncLookupContinuation
{
953 virtual ~JITLinkAsyncLookupContinuation() {}
954 virtual void run(Expected
<AsyncLookupResult
> LR
) = 0;
957 virtual void anchor();
960 /// Create a lookup continuation from a function object.
961 template <typename Continuation
>
962 std::unique_ptr
<JITLinkAsyncLookupContinuation
>
963 createLookupContinuation(Continuation Cont
) {
965 class Impl final
: public JITLinkAsyncLookupContinuation
{
967 Impl(Continuation C
) : C(std::move(C
)) {}
968 void run(Expected
<AsyncLookupResult
> LR
) override
{ C(std::move(LR
)); }
974 return std::make_unique
<Impl
>(std::move(Cont
));
977 /// Holds context for a single jitLink invocation.
978 class JITLinkContext
{
980 /// Destroy a JITLinkContext.
981 virtual ~JITLinkContext();
983 /// Return the MemoryManager to be used for this link.
984 virtual JITLinkMemoryManager
&getMemoryManager() = 0;
986 /// Returns a StringRef for the object buffer.
987 /// This method can not be called once takeObjectBuffer has been called.
988 virtual MemoryBufferRef
getObjectBuffer() const = 0;
990 /// Notify this context that linking failed.
991 /// Called by JITLink if linking cannot be completed.
992 virtual void notifyFailed(Error Err
) = 0;
994 /// Called by JITLink to resolve external symbols. This method is passed a
995 /// lookup continutation which it must call with a result to continue the
997 virtual void lookup(const DenseSet
<StringRef
> &Symbols
,
998 std::unique_ptr
<JITLinkAsyncLookupContinuation
> LC
) = 0;
1000 /// Called by JITLink once all defined symbols in the graph have been assigned
1001 /// their final memory locations in the target process. At this point the
1002 /// LinkGraph can be inspected to build a symbol table, however the block
1003 /// content will not generally have been copied to the target location yet.
1004 virtual void notifyResolved(LinkGraph
&G
) = 0;
1006 /// Called by JITLink to notify the context that the object has been
1007 /// finalized (i.e. emitted to memory and memory permissions set). If all of
1008 /// this objects dependencies have also been finalized then the code is ready
1011 notifyFinalized(std::unique_ptr
<JITLinkMemoryManager::Allocation
> A
) = 0;
1013 /// Called by JITLink prior to linking to determine whether default passes for
1014 /// the target should be added. The default implementation returns true.
1015 /// If subclasses override this method to return false for any target then
1016 /// they are required to fully configure the pass pipeline for that target.
1017 virtual bool shouldAddDefaultTargetPasses(const Triple
&TT
) const;
1019 /// Returns the mark-live pass to be used for this link. If no pass is
1020 /// returned (the default) then the target-specific linker implementation will
1021 /// choose a conservative default (usually marking all symbols live).
1022 /// This function is only called if shouldAddDefaultTargetPasses returns true,
1023 /// otherwise the JITContext is responsible for adding a mark-live pass in
1024 /// modifyPassConfig.
1025 virtual LinkGraphPassFunction
getMarkLivePass(const Triple
&TT
) const;
1027 /// Called by JITLink to modify the pass pipeline prior to linking.
1028 /// The default version performs no modification.
1029 virtual Error
modifyPassConfig(const Triple
&TT
, PassConfiguration
&Config
);
1032 /// Marks all symbols in a graph live. This can be used as a default,
1033 /// conservative mark-live implementation.
1034 Error
markAllSymbolsLive(LinkGraph
&G
);
1036 /// Basic JITLink implementation.
1038 /// This function will use sensible defaults for GOT and Stub handling.
1039 void jitLink(std::unique_ptr
<JITLinkContext
> Ctx
);
1041 } // end namespace jitlink
1042 } // end namespace llvm
1044 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H