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