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