1 //===- SyntheticSection.h ---------------------------------------*- 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 // Synthetic sections represent chunks of linker-created data. If you 10 // need to create a chunk of data that to be included in some section 11 // in the result, you probably want to create that as a synthetic section. 12 // 13 // Synthetic sections are designed as input sections as opposed to 14 // output sections because we want to allow them to be manipulated 15 // using linker scripts just like other input sections from regular 16 // files. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLD_ELF_SYNTHETIC_SECTIONS_H 21 #define LLD_ELF_SYNTHETIC_SECTIONS_H 22 23 #include "DWARF.h" 24 #include "EhFrame.h" 25 #include "InputSection.h" 26 #include "llvm/ADT/DenseSet.h" 27 #include "llvm/ADT/MapVector.h" 28 #include "llvm/MC/StringTableBuilder.h" 29 #include "llvm/Support/Endian.h" 30 #include <functional> 31 32 namespace lld { 33 namespace elf { 34 class Defined; 35 struct PhdrEntry; 36 class SymbolTableBaseSection; 37 class VersionNeedBaseSection; 38 39 class SyntheticSection : public InputSection { 40 public: 41 SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment, 42 StringRef name) 43 : InputSection(nullptr, flags, type, alignment, {}, name, 44 InputSectionBase::Synthetic) { 45 markLive(); 46 } 47 48 virtual ~SyntheticSection() = default; 49 virtual void writeTo(uint8_t *buf) = 0; 50 virtual size_t getSize() const = 0; 51 virtual void finalizeContents() {} 52 // If the section has the SHF_ALLOC flag and the size may be changed if 53 // thunks are added, update the section size. 54 virtual bool updateAllocSize() { return false; } 55 virtual bool isNeeded() const { return true; } 56 57 static bool classof(const SectionBase *d) { 58 return d->kind() == InputSectionBase::Synthetic; 59 } 60 }; 61 62 struct CieRecord { 63 EhSectionPiece *cie = nullptr; 64 std::vector<EhSectionPiece *> fdes; 65 }; 66 67 // Section for .eh_frame. 68 class EhFrameSection final : public SyntheticSection { 69 public: 70 EhFrameSection(); 71 void writeTo(uint8_t *buf) override; 72 void finalizeContents() override; 73 bool isNeeded() const override { return !sections.empty(); } 74 size_t getSize() const override { return size; } 75 76 static bool classof(const SectionBase *d) { 77 return SyntheticSection::classof(d) && d->name == ".eh_frame"; 78 } 79 80 void addSection(EhInputSection *sec); 81 82 std::vector<EhInputSection *> sections; 83 size_t numFdes = 0; 84 85 struct FdeData { 86 uint32_t pcRel; 87 uint32_t fdeVARel; 88 }; 89 90 std::vector<FdeData> getFdeData() const; 91 ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; } 92 template <class ELFT> 93 void iterateFDEWithLSDA(llvm::function_ref<void(InputSection &)> fn); 94 95 private: 96 // This is used only when parsing EhInputSection. We keep it here to avoid 97 // allocating one for each EhInputSection. 98 llvm::DenseMap<size_t, CieRecord *> offsetToCie; 99 100 uint64_t size = 0; 101 102 template <class ELFT, class RelTy> 103 void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels); 104 template <class ELFT> void addSectionAux(EhInputSection *s); 105 template <class ELFT, class RelTy> 106 void iterateFDEWithLSDAAux(EhInputSection &sec, ArrayRef<RelTy> rels, 107 llvm::DenseSet<size_t> &ciesWithLSDA, 108 llvm::function_ref<void(InputSection &)> fn); 109 110 template <class ELFT, class RelTy> 111 CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels); 112 113 template <class ELFT, class RelTy> 114 Defined *isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels); 115 116 uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const; 117 118 std::vector<CieRecord *> cieRecords; 119 120 // CIE records are uniquified by their contents and personality functions. 121 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap; 122 }; 123 124 class GotSection : public SyntheticSection { 125 public: 126 GotSection(); 127 size_t getSize() const override { return size; } 128 void finalizeContents() override; 129 bool isNeeded() const override; 130 void writeTo(uint8_t *buf) override; 131 132 void addEntry(Symbol &sym); 133 bool addDynTlsEntry(Symbol &sym); 134 bool addTlsIndex(); 135 uint64_t getGlobalDynAddr(const Symbol &b) const; 136 uint64_t getGlobalDynOffset(const Symbol &b) const; 137 138 uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; } 139 uint32_t getTlsIndexOff() const { return tlsIndexOff; } 140 141 // Flag to force GOT to be in output if we have relocations 142 // that relies on its address. 143 bool hasGotOffRel = false; 144 145 protected: 146 size_t numEntries = 0; 147 uint32_t tlsIndexOff = -1; 148 uint64_t size = 0; 149 }; 150 151 // .note.GNU-stack section. 152 class GnuStackSection : public SyntheticSection { 153 public: 154 GnuStackSection() 155 : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {} 156 void writeTo(uint8_t *buf) override {} 157 size_t getSize() const override { return 0; } 158 }; 159 160 class GnuPropertySection : public SyntheticSection { 161 public: 162 GnuPropertySection(); 163 void writeTo(uint8_t *buf) override; 164 size_t getSize() const override; 165 }; 166 167 // .note.gnu.build-id section. 168 class BuildIdSection : public SyntheticSection { 169 // First 16 bytes are a header. 170 static const unsigned headerSize = 16; 171 172 public: 173 const size_t hashSize; 174 BuildIdSection(); 175 void writeTo(uint8_t *buf) override; 176 size_t getSize() const override { return headerSize + hashSize; } 177 void writeBuildId(llvm::ArrayRef<uint8_t> buf); 178 179 private: 180 uint8_t *hashBuf; 181 }; 182 183 // BssSection is used to reserve space for copy relocations and common symbols. 184 // We create three instances of this class for .bss, .bss.rel.ro and "COMMON", 185 // that are used for writable symbols, read-only symbols and common symbols, 186 // respectively. 187 class BssSection final : public SyntheticSection { 188 public: 189 BssSection(StringRef name, uint64_t size, uint32_t alignment); 190 void writeTo(uint8_t *) override { 191 llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section"); 192 } 193 bool isNeeded() const override { return size != 0; } 194 size_t getSize() const override { return size; } 195 196 static bool classof(const SectionBase *s) { return s->bss; } 197 uint64_t size; 198 }; 199 200 class MipsGotSection final : public SyntheticSection { 201 public: 202 MipsGotSection(); 203 void writeTo(uint8_t *buf) override; 204 size_t getSize() const override { return size; } 205 bool updateAllocSize() override; 206 void finalizeContents() override; 207 bool isNeeded() const override; 208 209 // Join separate GOTs built for each input file to generate 210 // primary and optional multiple secondary GOTs. 211 void build(); 212 213 void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr); 214 void addDynTlsEntry(InputFile &file, Symbol &sym); 215 void addTlsIndex(InputFile &file); 216 217 uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s, 218 int64_t addend) const; 219 uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s, 220 int64_t addend) const; 221 uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const; 222 uint64_t getTlsIndexOffset(const InputFile *f) const; 223 224 // Returns the symbol which corresponds to the first entry of the global part 225 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic 226 // table properties. 227 // Returns nullptr if the global part is empty. 228 const Symbol *getFirstGlobalEntry() const; 229 230 // Returns the number of entries in the local part of GOT including 231 // the number of reserved entries. 232 unsigned getLocalEntriesNum() const; 233 234 // Return _gp value for primary GOT (nullptr) or particular input file. 235 uint64_t getGp(const InputFile *f = nullptr) const; 236 237 private: 238 // MIPS GOT consists of three parts: local, global and tls. Each part 239 // contains different types of entries. Here is a layout of GOT: 240 // - Header entries | 241 // - Page entries | Local part 242 // - Local entries (16-bit access) | 243 // - Local entries (32-bit access) | 244 // - Normal global entries || Global part 245 // - Reloc-only global entries || 246 // - TLS entries ||| TLS part 247 // 248 // Header: 249 // Two entries hold predefined value 0x0 and 0x80000000. 250 // Page entries: 251 // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16 252 // relocation against local symbols. They are initialized by higher 16-bit 253 // of the corresponding symbol's value. So each 64kb of address space 254 // requires a single GOT entry. 255 // Local entries (16-bit access): 256 // These entries created by GOT relocations against global non-preemptible 257 // symbols so dynamic linker is not necessary to resolve the symbol's 258 // values. "16-bit access" means that corresponding relocations address 259 // GOT using 16-bit index. Each unique Symbol-Addend pair has its own 260 // GOT entry. 261 // Local entries (32-bit access): 262 // These entries are the same as above but created by relocations which 263 // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc). 264 // Normal global entries: 265 // These entries created by GOT relocations against preemptible global 266 // symbols. They need to be initialized by dynamic linker and they ordered 267 // exactly as the corresponding entries in the dynamic symbols table. 268 // Reloc-only global entries: 269 // These entries created for symbols that are referenced by dynamic 270 // relocations R_MIPS_REL32. These entries are not accessed with gp-relative 271 // addressing, but MIPS ABI requires that these entries be present in GOT. 272 // TLS entries: 273 // Entries created by TLS relocations. 274 // 275 // If the sum of local, global and tls entries is less than 64K only single 276 // got is enough. Otherwise, multi-got is created. Series of primary and 277 // multiple secondary GOTs have the following layout: 278 // - Primary GOT 279 // Header 280 // Local entries 281 // Global entries 282 // Relocation only entries 283 // TLS entries 284 // 285 // - Secondary GOT 286 // Local entries 287 // Global entries 288 // TLS entries 289 // ... 290 // 291 // All GOT entries required by relocations from a single input file entirely 292 // belong to either primary or one of secondary GOTs. To reference GOT entries 293 // each GOT has its own _gp value points to the "middle" of the GOT. 294 // In the code this value loaded to the register which is used for GOT access. 295 // 296 // MIPS 32 function's prologue: 297 // lui v0,0x0 298 // 0: R_MIPS_HI16 _gp_disp 299 // addiu v0,v0,0 300 // 4: R_MIPS_LO16 _gp_disp 301 // 302 // MIPS 64: 303 // lui at,0x0 304 // 14: R_MIPS_GPREL16 main 305 // 306 // Dynamic linker does not know anything about secondary GOTs and cannot 307 // use a regular MIPS mechanism for GOT entries initialization. So we have 308 // to use an approach accepted by other architectures and create dynamic 309 // relocations R_MIPS_REL32 to initialize global entries (and local in case 310 // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker 311 // requires GOT entries and correspondingly ordered dynamic symbol table 312 // entries to deal with dynamic relocations. To handle this problem 313 // relocation-only section in the primary GOT contains entries for all 314 // symbols referenced in global parts of secondary GOTs. Although the sum 315 // of local and normal global entries of the primary got should be less 316 // than 64K, the size of the primary got (including relocation-only entries 317 // can be greater than 64K, because parts of the primary got that overflow 318 // the 64K limit are used only by the dynamic linker at dynamic link-time 319 // and not by 16-bit gp-relative addressing at run-time. 320 // 321 // For complete multi-GOT description see the following link 322 // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT 323 324 // Number of "Header" entries. 325 static const unsigned headerEntriesNum = 2; 326 327 uint64_t size = 0; 328 329 // Symbol and addend. 330 using GotEntry = std::pair<Symbol *, int64_t>; 331 332 struct FileGot { 333 InputFile *file = nullptr; 334 size_t startIndex = 0; 335 336 struct PageBlock { 337 size_t firstIndex; 338 size_t count; 339 PageBlock() : firstIndex(0), count(0) {} 340 }; 341 342 // Map output sections referenced by MIPS GOT relocations 343 // to the description (index/count) "page" entries allocated 344 // for this section. 345 llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap; 346 // Maps from Symbol+Addend pair or just Symbol to the GOT entry index. 347 llvm::MapVector<GotEntry, size_t> local16; 348 llvm::MapVector<GotEntry, size_t> local32; 349 llvm::MapVector<Symbol *, size_t> global; 350 llvm::MapVector<Symbol *, size_t> relocs; 351 llvm::MapVector<Symbol *, size_t> tls; 352 // Set of symbols referenced by dynamic TLS relocations. 353 llvm::MapVector<Symbol *, size_t> dynTlsSymbols; 354 355 // Total number of all entries. 356 size_t getEntriesNum() const; 357 // Number of "page" entries. 358 size_t getPageEntriesNum() const; 359 // Number of entries require 16-bit index to access. 360 size_t getIndexedEntriesNum() const; 361 }; 362 363 // Container of GOT created for each input file. 364 // After building a final series of GOTs this container 365 // holds primary and secondary GOT's. 366 std::vector<FileGot> gots; 367 368 // Return (and create if necessary) `FileGot`. 369 FileGot &getGot(InputFile &f); 370 371 // Try to merge two GOTs. In case of success the `Dst` contains 372 // result of merging and the function returns true. In case of 373 // overflow the `Dst` is unchanged and the function returns false. 374 bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary); 375 }; 376 377 class GotPltSection final : public SyntheticSection { 378 public: 379 GotPltSection(); 380 void addEntry(Symbol &sym); 381 size_t getSize() const override; 382 void writeTo(uint8_t *buf) override; 383 bool isNeeded() const override; 384 385 // Flag to force GotPlt to be in output if we have relocations 386 // that relies on its address. 387 bool hasGotPltOffRel = false; 388 389 private: 390 std::vector<const Symbol *> entries; 391 }; 392 393 // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc 394 // Symbols that will be relocated by Target->IRelativeRel. 395 // On most Targets the IgotPltSection will immediately follow the GotPltSection 396 // on ARM the IgotPltSection will immediately follow the GotSection. 397 class IgotPltSection final : public SyntheticSection { 398 public: 399 IgotPltSection(); 400 void addEntry(Symbol &sym); 401 size_t getSize() const override; 402 void writeTo(uint8_t *buf) override; 403 bool isNeeded() const override { return !entries.empty(); } 404 405 private: 406 std::vector<const Symbol *> entries; 407 }; 408 409 class StringTableSection final : public SyntheticSection { 410 public: 411 StringTableSection(StringRef name, bool dynamic); 412 unsigned addString(StringRef s, bool hashIt = true); 413 void writeTo(uint8_t *buf) override; 414 size_t getSize() const override { return size; } 415 bool isDynamic() const { return dynamic; } 416 417 private: 418 const bool dynamic; 419 420 uint64_t size = 0; 421 422 llvm::DenseMap<StringRef, unsigned> stringMap; 423 std::vector<StringRef> strings; 424 }; 425 426 class DynamicReloc { 427 public: 428 enum Kind { 429 /// The resulting dynamic relocation does not reference a symbol (#sym must 430 /// be nullptr) and uses #addend as the result of computeAddend(). 431 AddendOnly, 432 /// The resulting dynamic relocation will not reference a symbol: #sym is 433 /// only used to compute the addend with InputSection::getRelocTargetVA(). 434 /// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64). 435 AddendOnlyWithTargetVA, 436 /// The resulting dynamic relocation references symbol #sym from the dynamic 437 /// symbol table and uses #addend as the value of computeAddend(). 438 AgainstSymbol, 439 /// The resulting dynamic relocation references symbol #sym from the dynamic 440 /// symbol table and uses InputSection::getRelocTargetVA() + #addend for the 441 /// final addend. It can be used for relocations that write the symbol VA as 442 // the addend (e.g. R_MIPS_TLS_TPREL64) but still reference the symbol. 443 AgainstSymbolWithTargetVA, 444 /// This is used by the MIPS multi-GOT implementation. It relocates 445 /// addresses of 64kb pages that lie inside the output section. 446 MipsMultiGotPage, 447 }; 448 /// This constructor records a relocation against a symbol. 449 DynamicReloc(RelType type, const InputSectionBase *inputSec, 450 uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend, 451 RelExpr expr) 452 : type(type), sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), 453 kind(kind), expr(expr), addend(addend) {} 454 /// This constructor records a relative relocation with no symbol. 455 DynamicReloc(RelType type, const InputSectionBase *inputSec, 456 uint64_t offsetInSec, int64_t addend = 0) 457 : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec), 458 kind(AddendOnly), expr(R_ADDEND), addend(addend) {} 459 /// This constructor records dynamic relocation settings used by the MIPS 460 /// multi-GOT implementation. 461 DynamicReloc(RelType type, const InputSectionBase *inputSec, 462 uint64_t offsetInSec, const OutputSection *outputSec, 463 int64_t addend) 464 : type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec), 465 kind(MipsMultiGotPage), expr(R_ADDEND), addend(addend), 466 outputSec(outputSec) {} 467 468 uint64_t getOffset() const; 469 uint32_t getSymIndex(SymbolTableBaseSection *symTab) const; 470 bool needsDynSymIndex() const { 471 return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA; 472 } 473 474 /// Computes the addend of the dynamic relocation. Note that this is not the 475 /// same as the #addend member variable as it may also include the symbol 476 /// address/the address of the corresponding GOT entry/etc. 477 int64_t computeAddend() const; 478 479 RelType type; 480 Symbol *sym; 481 const InputSectionBase *inputSec; 482 uint64_t offsetInSec; 483 484 private: 485 Kind kind; 486 // The kind of expression used to calculate the added (required e.g. for 487 // relative GOT relocations). 488 RelExpr expr; 489 int64_t addend; 490 const OutputSection *outputSec = nullptr; 491 }; 492 493 template <class ELFT> class DynamicSection final : public SyntheticSection { 494 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 495 496 // finalizeContents() fills this vector with the section contents. 497 std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries; 498 499 public: 500 DynamicSection(); 501 void finalizeContents() override; 502 void writeTo(uint8_t *buf) override; 503 size_t getSize() const override { return size; } 504 505 private: 506 void add(int32_t tag, std::function<uint64_t()> fn); 507 void addInt(int32_t tag, uint64_t val); 508 void addInSec(int32_t tag, InputSection *sec); 509 void addInSecRelative(int32_t tag, InputSection *sec); 510 void addOutSec(int32_t tag, OutputSection *sec); 511 void addSize(int32_t tag, OutputSection *sec); 512 void addSym(int32_t tag, Symbol *sym); 513 514 uint64_t size = 0; 515 }; 516 517 class RelocationBaseSection : public SyntheticSection { 518 public: 519 RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag, 520 int32_t sizeDynamicTag); 521 /// Add a dynamic relocation without writing an addend to the output section. 522 /// This overload can be used if the addends are written directly instead of 523 /// using relocations on the input section (e.g. MipsGotSection::writeTo()). 524 void addReloc(const DynamicReloc &reloc); 525 /// Add a dynamic relocation against \p sym with an optional addend. 526 void addSymbolReloc(RelType dynType, InputSectionBase *isec, 527 uint64_t offsetInSec, Symbol &sym, int64_t addend = 0, 528 llvm::Optional<RelType> addendRelType = llvm::None); 529 /// Add a relative dynamic relocation that uses the target address of \p sym 530 /// (i.e. InputSection::getRelocTargetVA()) + \p addend as the addend. 531 void addRelativeReloc(RelType dynType, InputSectionBase *isec, 532 uint64_t offsetInSec, Symbol &sym, int64_t addend, 533 RelType addendRelType, RelExpr expr); 534 /// Add a dynamic relocation using the target address of \p sym as the addend 535 /// if \p sym is non-preemptible. Otherwise add a relocation against \p sym. 536 void addAddendOnlyRelocIfNonPreemptible(RelType dynType, 537 InputSectionBase *isec, 538 uint64_t offsetInSec, Symbol &sym, 539 RelType addendRelType); 540 void addReloc(DynamicReloc::Kind kind, RelType dynType, 541 InputSectionBase *inputSec, uint64_t offsetInSec, Symbol &sym, 542 int64_t addend, RelExpr expr, RelType addendRelType); 543 bool isNeeded() const override { return !relocs.empty(); } 544 size_t getSize() const override { return relocs.size() * this->entsize; } 545 size_t getRelativeRelocCount() const { return numRelativeRelocs; } 546 void finalizeContents() override; 547 static bool classof(const SectionBase *d) { 548 return SyntheticSection::classof(d) && 549 (d->type == llvm::ELF::SHT_RELA || d->type == llvm::ELF::SHT_REL || 550 d->type == llvm::ELF::SHT_RELR); 551 } 552 int32_t dynamicTag, sizeDynamicTag; 553 std::vector<DynamicReloc> relocs; 554 555 protected: 556 size_t numRelativeRelocs = 0; 557 }; 558 559 template <class ELFT> 560 class RelocationSection final : public RelocationBaseSection { 561 using Elf_Rel = typename ELFT::Rel; 562 using Elf_Rela = typename ELFT::Rela; 563 564 public: 565 RelocationSection(StringRef name, bool sort); 566 void writeTo(uint8_t *buf) override; 567 568 private: 569 bool sort; 570 }; 571 572 template <class ELFT> 573 class AndroidPackedRelocationSection final : public RelocationBaseSection { 574 using Elf_Rel = typename ELFT::Rel; 575 using Elf_Rela = typename ELFT::Rela; 576 577 public: 578 AndroidPackedRelocationSection(StringRef name); 579 580 bool updateAllocSize() override; 581 size_t getSize() const override { return relocData.size(); } 582 void writeTo(uint8_t *buf) override { 583 memcpy(buf, relocData.data(), relocData.size()); 584 } 585 586 private: 587 SmallVector<char, 0> relocData; 588 }; 589 590 struct RelativeReloc { 591 uint64_t getOffset() const { return inputSec->getVA(offsetInSec); } 592 593 const InputSectionBase *inputSec; 594 uint64_t offsetInSec; 595 }; 596 597 class RelrBaseSection : public SyntheticSection { 598 public: 599 RelrBaseSection(); 600 bool isNeeded() const override { return !relocs.empty(); } 601 std::vector<RelativeReloc> relocs; 602 }; 603 604 // RelrSection is used to encode offsets for relative relocations. 605 // Proposal for adding SHT_RELR sections to generic-abi is here: 606 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 607 // For more details, see the comment in RelrSection::updateAllocSize(). 608 template <class ELFT> class RelrSection final : public RelrBaseSection { 609 using Elf_Relr = typename ELFT::Relr; 610 611 public: 612 RelrSection(); 613 614 bool updateAllocSize() override; 615 size_t getSize() const override { return relrRelocs.size() * this->entsize; } 616 void writeTo(uint8_t *buf) override { 617 memcpy(buf, relrRelocs.data(), getSize()); 618 } 619 620 private: 621 std::vector<Elf_Relr> relrRelocs; 622 }; 623 624 struct SymbolTableEntry { 625 Symbol *sym; 626 size_t strTabOffset; 627 }; 628 629 class SymbolTableBaseSection : public SyntheticSection { 630 public: 631 SymbolTableBaseSection(StringTableSection &strTabSec); 632 void finalizeContents() override; 633 size_t getSize() const override { return getNumSymbols() * entsize; } 634 void addSymbol(Symbol *sym); 635 unsigned getNumSymbols() const { return symbols.size() + 1; } 636 size_t getSymbolIndex(Symbol *sym); 637 ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; } 638 639 protected: 640 void sortSymTabSymbols(); 641 642 // A vector of symbols and their string table offsets. 643 std::vector<SymbolTableEntry> symbols; 644 645 StringTableSection &strTabSec; 646 647 llvm::once_flag onceFlag; 648 llvm::DenseMap<Symbol *, size_t> symbolIndexMap; 649 llvm::DenseMap<OutputSection *, size_t> sectionIndexMap; 650 }; 651 652 template <class ELFT> 653 class SymbolTableSection final : public SymbolTableBaseSection { 654 using Elf_Sym = typename ELFT::Sym; 655 656 public: 657 SymbolTableSection(StringTableSection &strTabSec); 658 void writeTo(uint8_t *buf) override; 659 }; 660 661 class SymtabShndxSection final : public SyntheticSection { 662 public: 663 SymtabShndxSection(); 664 665 void writeTo(uint8_t *buf) override; 666 size_t getSize() const override; 667 bool isNeeded() const override; 668 void finalizeContents() override; 669 }; 670 671 // Outputs GNU Hash section. For detailed explanation see: 672 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections 673 class GnuHashTableSection final : public SyntheticSection { 674 public: 675 GnuHashTableSection(); 676 void finalizeContents() override; 677 void writeTo(uint8_t *buf) override; 678 size_t getSize() const override { return size; } 679 680 // Adds symbols to the hash table. 681 // Sorts the input to satisfy GNU hash section requirements. 682 void addSymbols(std::vector<SymbolTableEntry> &symbols); 683 684 private: 685 // See the comment in writeBloomFilter. 686 enum { Shift2 = 26 }; 687 688 void writeBloomFilter(uint8_t *buf); 689 void writeHashTable(uint8_t *buf); 690 691 struct Entry { 692 Symbol *sym; 693 size_t strTabOffset; 694 uint32_t hash; 695 uint32_t bucketIdx; 696 }; 697 698 std::vector<Entry> symbols; 699 size_t maskWords; 700 size_t nBuckets = 0; 701 size_t size = 0; 702 }; 703 704 class HashTableSection final : public SyntheticSection { 705 public: 706 HashTableSection(); 707 void finalizeContents() override; 708 void writeTo(uint8_t *buf) override; 709 size_t getSize() const override { return size; } 710 711 private: 712 size_t size = 0; 713 }; 714 715 // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT 716 // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily 717 // at runtime. 718 // 719 // On PowerPC, this section contains lazy symbol resolvers. A branch instruction 720 // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a 721 // lazy symbol resolver. 722 // 723 // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs. 724 // A call instruction jumps to a .plt.sec entry, which will then jump to the 725 // target (BIND_NOW) or a .plt entry. 726 class PltSection : public SyntheticSection { 727 public: 728 PltSection(); 729 void writeTo(uint8_t *buf) override; 730 size_t getSize() const override; 731 bool isNeeded() const override; 732 void addSymbols(); 733 void addEntry(Symbol &sym); 734 size_t getNumEntries() const { return entries.size(); } 735 736 size_t headerSize; 737 738 std::vector<const Symbol *> entries; 739 }; 740 741 // Used for non-preemptible ifuncs. It does not have a header. Each entry is 742 // associated with an IRELATIVE relocation, which will be resolved eagerly at 743 // runtime. PltSection can only contain entries associated with JUMP_SLOT 744 // relocations, so IPLT entries are in a separate section. 745 class IpltSection final : public SyntheticSection { 746 std::vector<const Symbol *> entries; 747 748 public: 749 IpltSection(); 750 void writeTo(uint8_t *buf) override; 751 size_t getSize() const override; 752 bool isNeeded() const override { return !entries.empty(); } 753 void addSymbols(); 754 void addEntry(Symbol &sym); 755 }; 756 757 class PPC32GlinkSection : public PltSection { 758 public: 759 PPC32GlinkSection(); 760 void writeTo(uint8_t *buf) override; 761 size_t getSize() const override; 762 763 std::vector<const Symbol *> canonical_plts; 764 static constexpr size_t footerSize = 64; 765 }; 766 767 // This is x86-only. 768 class IBTPltSection : public SyntheticSection { 769 public: 770 IBTPltSection(); 771 void writeTo(uint8_t *Buf) override; 772 size_t getSize() const override; 773 }; 774 775 class GdbIndexSection final : public SyntheticSection { 776 public: 777 struct AddressEntry { 778 InputSection *section; 779 uint64_t lowAddress; 780 uint64_t highAddress; 781 uint32_t cuIndex; 782 }; 783 784 struct CuEntry { 785 uint64_t cuOffset; 786 uint64_t cuLength; 787 }; 788 789 struct NameAttrEntry { 790 llvm::CachedHashStringRef name; 791 uint32_t cuIndexAndAttrs; 792 }; 793 794 struct GdbChunk { 795 InputSection *sec; 796 std::vector<AddressEntry> addressAreas; 797 std::vector<CuEntry> compilationUnits; 798 }; 799 800 struct GdbSymbol { 801 llvm::CachedHashStringRef name; 802 std::vector<uint32_t> cuVector; 803 uint32_t nameOff; 804 uint32_t cuVectorOff; 805 }; 806 807 GdbIndexSection(); 808 template <typename ELFT> static GdbIndexSection *create(); 809 void writeTo(uint8_t *buf) override; 810 size_t getSize() const override { return size; } 811 bool isNeeded() const override; 812 813 private: 814 struct GdbIndexHeader { 815 llvm::support::ulittle32_t version; 816 llvm::support::ulittle32_t cuListOff; 817 llvm::support::ulittle32_t cuTypesOff; 818 llvm::support::ulittle32_t addressAreaOff; 819 llvm::support::ulittle32_t symtabOff; 820 llvm::support::ulittle32_t constantPoolOff; 821 }; 822 823 void initOutputSize(); 824 size_t computeSymtabSize() const; 825 826 // Each chunk contains information gathered from debug sections of a 827 // single object file. 828 std::vector<GdbChunk> chunks; 829 830 // A symbol table for this .gdb_index section. 831 std::vector<GdbSymbol> symbols; 832 833 size_t size; 834 }; 835 836 // --eh-frame-hdr option tells linker to construct a header for all the 837 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr 838 // and also to a PT_GNU_EH_FRAME segment. 839 // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by 840 // calling dl_iterate_phdr. 841 // This section contains a lookup table for quick binary search of FDEs. 842 // Detailed info about internals can be found in Ian Lance Taylor's blog: 843 // http://www.airs.com/blog/archives/460 (".eh_frame") 844 // http://www.airs.com/blog/archives/462 (".eh_frame_hdr") 845 class EhFrameHeader final : public SyntheticSection { 846 public: 847 EhFrameHeader(); 848 void write(); 849 void writeTo(uint8_t *buf) override; 850 size_t getSize() const override; 851 bool isNeeded() const override; 852 }; 853 854 // For more information about .gnu.version and .gnu.version_r see: 855 // https://www.akkadia.org/drepper/symbol-versioning 856 857 // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall 858 // contain symbol version definitions. The number of entries in this section 859 // shall be contained in the DT_VERDEFNUM entry of the .dynamic section. 860 // The section shall contain an array of Elf_Verdef structures, optionally 861 // followed by an array of Elf_Verdaux structures. 862 class VersionDefinitionSection final : public SyntheticSection { 863 public: 864 VersionDefinitionSection(); 865 void finalizeContents() override; 866 size_t getSize() const override; 867 void writeTo(uint8_t *buf) override; 868 869 private: 870 enum { EntrySize = 28 }; 871 void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff); 872 StringRef getFileDefName(); 873 874 unsigned fileDefNameOff; 875 std::vector<unsigned> verDefNameOffs; 876 }; 877 878 // The .gnu.version section specifies the required version of each symbol in the 879 // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol 880 // table entry. An Elf_Versym is just a 16-bit integer that refers to a version 881 // identifier defined in the either .gnu.version_r or .gnu.version_d section. 882 // The values 0 and 1 are reserved. All other values are used for versions in 883 // the own object or in any of the dependencies. 884 class VersionTableSection final : public SyntheticSection { 885 public: 886 VersionTableSection(); 887 void finalizeContents() override; 888 size_t getSize() const override; 889 void writeTo(uint8_t *buf) override; 890 bool isNeeded() const override; 891 }; 892 893 // The .gnu.version_r section defines the version identifiers used by 894 // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each 895 // Elf_Verneed specifies the version requirements for a single DSO, and contains 896 // a reference to a linked list of Elf_Vernaux data structures which define the 897 // mapping from version identifiers to version names. 898 template <class ELFT> 899 class VersionNeedSection final : public SyntheticSection { 900 using Elf_Verneed = typename ELFT::Verneed; 901 using Elf_Vernaux = typename ELFT::Vernaux; 902 903 struct Vernaux { 904 uint64_t hash; 905 uint32_t verneedIndex; 906 uint64_t nameStrTab; 907 }; 908 909 struct Verneed { 910 uint64_t nameStrTab; 911 std::vector<Vernaux> vernauxs; 912 }; 913 914 std::vector<Verneed> verneeds; 915 916 public: 917 VersionNeedSection(); 918 void finalizeContents() override; 919 void writeTo(uint8_t *buf) override; 920 size_t getSize() const override; 921 bool isNeeded() const override; 922 }; 923 924 // MergeSyntheticSection is a class that allows us to put mergeable sections 925 // with different attributes in a single output sections. To do that 926 // we put them into MergeSyntheticSection synthetic input sections which are 927 // attached to regular output sections. 928 class MergeSyntheticSection : public SyntheticSection { 929 public: 930 void addSection(MergeInputSection *ms); 931 std::vector<MergeInputSection *> sections; 932 933 protected: 934 MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags, 935 uint32_t alignment) 936 : SyntheticSection(flags, type, alignment, name) {} 937 }; 938 939 class MergeTailSection final : public MergeSyntheticSection { 940 public: 941 MergeTailSection(StringRef name, uint32_t type, uint64_t flags, 942 uint32_t alignment); 943 944 size_t getSize() const override; 945 void writeTo(uint8_t *buf) override; 946 void finalizeContents() override; 947 948 private: 949 llvm::StringTableBuilder builder; 950 }; 951 952 class MergeNoTailSection final : public MergeSyntheticSection { 953 public: 954 MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags, 955 uint32_t alignment) 956 : MergeSyntheticSection(name, type, flags, alignment) {} 957 958 size_t getSize() const override { return size; } 959 void writeTo(uint8_t *buf) override; 960 void finalizeContents() override; 961 962 private: 963 // We use the most significant bits of a hash as a shard ID. 964 // The reason why we don't want to use the least significant bits is 965 // because DenseMap also uses lower bits to determine a bucket ID. 966 // If we use lower bits, it significantly increases the probability of 967 // hash collisons. 968 size_t getShardId(uint32_t hash) { 969 assert((hash >> 31) == 0); 970 return hash >> (31 - llvm::countTrailingZeros(numShards)); 971 } 972 973 // Section size 974 size_t size; 975 976 // String table contents 977 constexpr static size_t numShards = 32; 978 std::vector<llvm::StringTableBuilder> shards; 979 size_t shardOffsets[numShards]; 980 }; 981 982 // .MIPS.abiflags section. 983 template <class ELFT> 984 class MipsAbiFlagsSection final : public SyntheticSection { 985 using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>; 986 987 public: 988 static MipsAbiFlagsSection *create(); 989 990 MipsAbiFlagsSection(Elf_Mips_ABIFlags flags); 991 size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); } 992 void writeTo(uint8_t *buf) override; 993 994 private: 995 Elf_Mips_ABIFlags flags; 996 }; 997 998 // .MIPS.options section. 999 template <class ELFT> class MipsOptionsSection final : public SyntheticSection { 1000 using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>; 1001 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; 1002 1003 public: 1004 static MipsOptionsSection *create(); 1005 1006 MipsOptionsSection(Elf_Mips_RegInfo reginfo); 1007 void writeTo(uint8_t *buf) override; 1008 1009 size_t getSize() const override { 1010 return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 1011 } 1012 1013 private: 1014 Elf_Mips_RegInfo reginfo; 1015 }; 1016 1017 // MIPS .reginfo section. 1018 template <class ELFT> class MipsReginfoSection final : public SyntheticSection { 1019 using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; 1020 1021 public: 1022 static MipsReginfoSection *create(); 1023 1024 MipsReginfoSection(Elf_Mips_RegInfo reginfo); 1025 size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); } 1026 void writeTo(uint8_t *buf) override; 1027 1028 private: 1029 Elf_Mips_RegInfo reginfo; 1030 }; 1031 1032 // This is a MIPS specific section to hold a space within the data segment 1033 // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry. 1034 // See "Dynamic section" in Chapter 5 in the following document: 1035 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1036 class MipsRldMapSection : public SyntheticSection { 1037 public: 1038 MipsRldMapSection(); 1039 size_t getSize() const override { return config->wordsize; } 1040 void writeTo(uint8_t *buf) override {} 1041 }; 1042 1043 // Representation of the combined .ARM.Exidx input sections. We process these 1044 // as a SyntheticSection like .eh_frame as we need to merge duplicate entries 1045 // and add terminating sentinel entries. 1046 // 1047 // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form 1048 // a table that the unwinder can derive (Addresses are encoded as offsets from 1049 // table): 1050 // | Address of function | Unwind instructions for function | 1051 // where the unwind instructions are either a small number of unwind or the 1052 // special EXIDX_CANTUNWIND entry representing no unwinding information. 1053 // When an exception is thrown from an address A, the unwinder searches the 1054 // table for the closest table entry with Address of function <= A. This means 1055 // that for two consecutive table entries: 1056 // | A1 | U1 | 1057 // | A2 | U2 | 1058 // The range of addresses described by U1 is [A1, A2) 1059 // 1060 // There are two cases where we need a linker generated table entry to fixup 1061 // the address ranges in the table 1062 // Case 1: 1063 // - A sentinel entry added with an address higher than all 1064 // executable sections. This was needed to work around libunwind bug pr31091. 1065 // - After address assignment we need to find the highest addressed executable 1066 // section and use the limit of that section so that the unwinder never 1067 // matches it. 1068 // Case 2: 1069 // - InputSections without a .ARM.exidx section (usually from Assembly) 1070 // need a table entry so that they terminate the range of the previously 1071 // function. This is pr40277. 1072 // 1073 // Instead of storing pointers to the .ARM.exidx InputSections from 1074 // InputObjects, we store pointers to the executable sections that need 1075 // .ARM.exidx sections. We can then use the dependentSections of these to 1076 // either find the .ARM.exidx section or know that we need to generate one. 1077 class ARMExidxSyntheticSection : public SyntheticSection { 1078 public: 1079 ARMExidxSyntheticSection(); 1080 1081 // Add an input section to the ARMExidxSyntheticSection. Returns whether the 1082 // section needs to be removed from the main input section list. 1083 bool addSection(InputSection *isec); 1084 1085 size_t getSize() const override { return size; } 1086 void writeTo(uint8_t *buf) override; 1087 bool isNeeded() const override; 1088 // Sort and remove duplicate entries. 1089 void finalizeContents() override; 1090 InputSection *getLinkOrderDep() const; 1091 1092 static bool classof(const SectionBase *d); 1093 1094 // Links to the ARMExidxSections so we can transfer the relocations once the 1095 // layout is known. 1096 std::vector<InputSection *> exidxSections; 1097 1098 private: 1099 size_t size = 0; 1100 1101 // Instead of storing pointers to the .ARM.exidx InputSections from 1102 // InputObjects, we store pointers to the executable sections that need 1103 // .ARM.exidx sections. We can then use the dependentSections of these to 1104 // either find the .ARM.exidx section or know that we need to generate one. 1105 std::vector<InputSection *> executableSections; 1106 1107 // The executable InputSection with the highest address to use for the 1108 // sentinel. We store separately from ExecutableSections as merging of 1109 // duplicate entries may mean this InputSection is removed from 1110 // ExecutableSections. 1111 InputSection *sentinel = nullptr; 1112 }; 1113 1114 // A container for one or more linker generated thunks. Instances of these 1115 // thunks including ARM interworking and Mips LA25 PI to non-PI thunks. 1116 class ThunkSection : public SyntheticSection { 1117 public: 1118 // ThunkSection in OS, with desired outSecOff of Off 1119 ThunkSection(OutputSection *os, uint64_t off); 1120 1121 // Add a newly created Thunk to this container: 1122 // Thunk is given offset from start of this InputSection 1123 // Thunk defines a symbol in this InputSection that can be used as target 1124 // of a relocation 1125 void addThunk(Thunk *t); 1126 size_t getSize() const override; 1127 void writeTo(uint8_t *buf) override; 1128 InputSection *getTargetInputSection() const; 1129 bool assignOffsets(); 1130 1131 // When true, round up reported size of section to 4 KiB. See comment 1132 // in addThunkSection() for more details. 1133 bool roundUpSizeForErrata = false; 1134 1135 private: 1136 std::vector<Thunk *> thunks; 1137 size_t size = 0; 1138 }; 1139 1140 // Used to compute outSecOff of .got2 in each object file. This is needed to 1141 // synthesize PLT entries for PPC32 Secure PLT ABI. 1142 class PPC32Got2Section final : public SyntheticSection { 1143 public: 1144 PPC32Got2Section(); 1145 size_t getSize() const override { return 0; } 1146 bool isNeeded() const override; 1147 void finalizeContents() override; 1148 void writeTo(uint8_t *buf) override {} 1149 }; 1150 1151 // This section is used to store the addresses of functions that are called 1152 // in range-extending thunks on PowerPC64. When producing position dependent 1153 // code the addresses are link-time constants and the table is written out to 1154 // the binary. When producing position-dependent code the table is allocated and 1155 // filled in by the dynamic linker. 1156 class PPC64LongBranchTargetSection final : public SyntheticSection { 1157 public: 1158 PPC64LongBranchTargetSection(); 1159 uint64_t getEntryVA(const Symbol *sym, int64_t addend); 1160 llvm::Optional<uint32_t> addEntry(const Symbol *sym, int64_t addend); 1161 size_t getSize() const override; 1162 void writeTo(uint8_t *buf) override; 1163 bool isNeeded() const override; 1164 void finalizeContents() override { finalized = true; } 1165 1166 private: 1167 std::vector<std::pair<const Symbol *, int64_t>> entries; 1168 llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index; 1169 bool finalized = false; 1170 }; 1171 1172 template <typename ELFT> 1173 class PartitionElfHeaderSection : public SyntheticSection { 1174 public: 1175 PartitionElfHeaderSection(); 1176 size_t getSize() const override; 1177 void writeTo(uint8_t *buf) override; 1178 }; 1179 1180 template <typename ELFT> 1181 class PartitionProgramHeadersSection : public SyntheticSection { 1182 public: 1183 PartitionProgramHeadersSection(); 1184 size_t getSize() const override; 1185 void writeTo(uint8_t *buf) override; 1186 }; 1187 1188 class PartitionIndexSection : public SyntheticSection { 1189 public: 1190 PartitionIndexSection(); 1191 size_t getSize() const override; 1192 void finalizeContents() override; 1193 void writeTo(uint8_t *buf) override; 1194 }; 1195 1196 InputSection *createInterpSection(); 1197 MergeInputSection *createCommentSection(); 1198 MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type, 1199 uint64_t flags, uint32_t alignment); 1200 template <class ELFT> void splitSections(); 1201 1202 template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part); 1203 template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part); 1204 1205 Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value, 1206 uint64_t size, InputSectionBase §ion); 1207 1208 void addVerneed(Symbol *ss); 1209 1210 // Linker generated per-partition sections. 1211 struct Partition { 1212 StringRef name; 1213 uint64_t nameStrTab; 1214 1215 SyntheticSection *elfHeader; 1216 SyntheticSection *programHeaders; 1217 std::vector<PhdrEntry *> phdrs; 1218 1219 ARMExidxSyntheticSection *armExidx; 1220 BuildIdSection *buildId; 1221 SyntheticSection *dynamic; 1222 StringTableSection *dynStrTab; 1223 SymbolTableBaseSection *dynSymTab; 1224 EhFrameHeader *ehFrameHdr; 1225 EhFrameSection *ehFrame; 1226 GnuHashTableSection *gnuHashTab; 1227 HashTableSection *hashTab; 1228 RelocationBaseSection *relaDyn; 1229 RelrBaseSection *relrDyn; 1230 VersionDefinitionSection *verDef; 1231 SyntheticSection *verNeed; 1232 VersionTableSection *verSym; 1233 1234 unsigned getNumber() const { return this - &partitions[0] + 1; } 1235 }; 1236 1237 extern Partition *mainPart; 1238 1239 inline Partition &SectionBase::getPartition() const { 1240 assert(isLive()); 1241 return partitions[partition - 1]; 1242 } 1243 1244 // Linker generated sections which can be used as inputs and are not specific to 1245 // a partition. 1246 struct InStruct { 1247 InputSection *attributes; 1248 BssSection *bss; 1249 BssSection *bssRelRo; 1250 GotSection *got; 1251 GotPltSection *gotPlt; 1252 IgotPltSection *igotPlt; 1253 PPC64LongBranchTargetSection *ppc64LongBranchTarget; 1254 MipsGotSection *mipsGot; 1255 MipsRldMapSection *mipsRldMap; 1256 SyntheticSection *partEnd; 1257 SyntheticSection *partIndex; 1258 PltSection *plt; 1259 IpltSection *iplt; 1260 PPC32Got2Section *ppc32Got2; 1261 IBTPltSection *ibtPlt; 1262 RelocationBaseSection *relaPlt; 1263 RelocationBaseSection *relaIplt; 1264 StringTableSection *shStrTab; 1265 StringTableSection *strTab; 1266 SymbolTableBaseSection *symTab; 1267 SymtabShndxSection *symTabShndx; 1268 }; 1269 1270 extern InStruct in; 1271 1272 } // namespace elf 1273 } // namespace lld 1274 1275 #endif 1276