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