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