| 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 "Config.h" |
| 24 | #include "DWARF.h" |
| 25 | #include "InputSection.h" |
| 26 | #include "Symbols.h" |
| 27 | #include "llvm/ADT/DenseSet.h" |
| 28 | #include "llvm/ADT/FoldingSet.h" |
| 29 | #include "llvm/ADT/MapVector.h" |
| 30 | #include "llvm/ADT/STLFunctionalExtras.h" |
| 31 | #include "llvm/BinaryFormat/ELF.h" |
| 32 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
| 33 | #include "llvm/MC/StringTableBuilder.h" |
| 34 | #include "llvm/Support/Allocator.h" |
| 35 | #include "llvm/Support/Compiler.h" |
| 36 | #include "llvm/Support/Endian.h" |
| 37 | #include "llvm/Support/Parallel.h" |
| 38 | #include "llvm/Support/Threading.h" |
| 39 | |
| 40 | namespace lld::elf { |
| 41 | class Defined; |
| 42 | struct PhdrEntry; |
| 43 | class SymbolTableBaseSection; |
| 44 | |
| 45 | struct CieRecord { |
| 46 | EhSectionPiece *cie = nullptr; |
| 47 | SmallVector<EhSectionPiece *, 0> fdes; |
| 48 | }; |
| 49 | |
| 50 | // Section for .eh_frame. |
| 51 | class EhFrameSection final : public SyntheticSection { |
| 52 | public: |
| 53 | EhFrameSection(Ctx &); |
| 54 | void writeTo(uint8_t *buf) override; |
| 55 | void finalizeContents() override; |
| 56 | bool isNeeded() const override { return !sections.empty(); } |
| 57 | size_t getSize() const override { return size; } |
| 58 | |
| 59 | static bool classof(const SectionBase *d) { |
| 60 | return SyntheticSection::classof(sec: d) && d->name == ".eh_frame" ; |
| 61 | } |
| 62 | |
| 63 | SmallVector<EhInputSection *, 0> sections; |
| 64 | size_t numFdes = 0; |
| 65 | |
| 66 | struct FdeData { |
| 67 | uint32_t pcRel; |
| 68 | uint32_t fdeVARel; |
| 69 | }; |
| 70 | |
| 71 | SmallVector<FdeData, 0> getFdeData() const; |
| 72 | ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; } |
| 73 | template <class ELFT> |
| 74 | void iterateFDEWithLSDA(llvm::function_ref<void(InputSection &)> fn); |
| 75 | |
| 76 | private: |
| 77 | // This is used only when parsing EhInputSection. We keep it here to avoid |
| 78 | // allocating one for each EhInputSection. |
| 79 | llvm::DenseMap<size_t, CieRecord *> offsetToCie; |
| 80 | |
| 81 | uint64_t size = 0; |
| 82 | |
| 83 | template <class ELFT, class RelTy> |
| 84 | void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels); |
| 85 | template <class ELFT> void addSectionAux(EhInputSection *s); |
| 86 | template <class ELFT, class RelTy> |
| 87 | void iterateFDEWithLSDAAux(EhInputSection &sec, ArrayRef<RelTy> rels, |
| 88 | llvm::DenseSet<size_t> &ciesWithLSDA, |
| 89 | llvm::function_ref<void(InputSection &)> fn); |
| 90 | |
| 91 | template <class ELFT, class RelTy> |
| 92 | CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels); |
| 93 | |
| 94 | template <class ELFT, class RelTy> |
| 95 | Defined *isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels); |
| 96 | |
| 97 | uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const; |
| 98 | |
| 99 | SmallVector<CieRecord *, 0> cieRecords; |
| 100 | |
| 101 | // CIE records are uniquified by their contents and personality functions. |
| 102 | llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap; |
| 103 | }; |
| 104 | |
| 105 | class GotSection final : public SyntheticSection { |
| 106 | public: |
| 107 | GotSection(Ctx &); |
| 108 | size_t getSize() const override { return size; } |
| 109 | void finalizeContents() override; |
| 110 | bool isNeeded() const override; |
| 111 | void writeTo(uint8_t *buf) override; |
| 112 | |
| 113 | void addConstant(const Relocation &r); |
| 114 | void addEntry(const Symbol &sym); |
| 115 | void addAuthEntry(const Symbol &sym); |
| 116 | bool addTlsDescEntry(const Symbol &sym); |
| 117 | void addTlsDescAuthEntry(); |
| 118 | bool addDynTlsEntry(const Symbol &sym); |
| 119 | bool addTlsIndex(); |
| 120 | uint32_t getTlsDescOffset(const Symbol &sym) const; |
| 121 | uint64_t getTlsDescAddr(const Symbol &sym) const; |
| 122 | uint64_t getGlobalDynAddr(const Symbol &b) const; |
| 123 | uint64_t getGlobalDynOffset(const Symbol &b) const; |
| 124 | |
| 125 | uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; } |
| 126 | uint32_t getTlsIndexOff() const { return tlsIndexOff; } |
| 127 | |
| 128 | // Flag to force GOT to be in output if we have relocations |
| 129 | // that relies on its address. |
| 130 | std::atomic<bool> hasGotOffRel = false; |
| 131 | |
| 132 | protected: |
| 133 | size_t numEntries = 0; |
| 134 | uint32_t tlsIndexOff = -1; |
| 135 | uint64_t size = 0; |
| 136 | struct AuthEntryInfo { |
| 137 | size_t offset; |
| 138 | bool isSymbolFunc; |
| 139 | }; |
| 140 | SmallVector<AuthEntryInfo, 0> authEntries; |
| 141 | }; |
| 142 | |
| 143 | // .note.GNU-stack section. |
| 144 | class GnuStackSection : public SyntheticSection { |
| 145 | public: |
| 146 | GnuStackSection(Ctx &ctx) |
| 147 | : SyntheticSection(ctx, ".note.GNU-stack" , llvm::ELF::SHT_PROGBITS, 0, |
| 148 | 1) {} |
| 149 | void writeTo(uint8_t *buf) override {} |
| 150 | size_t getSize() const override { return 0; } |
| 151 | }; |
| 152 | |
| 153 | class GnuPropertySection final : public SyntheticSection { |
| 154 | public: |
| 155 | GnuPropertySection(Ctx &); |
| 156 | void writeTo(uint8_t *buf) override; |
| 157 | size_t getSize() const override; |
| 158 | }; |
| 159 | |
| 160 | // .note.gnu.build-id section. |
| 161 | class BuildIdSection : public SyntheticSection { |
| 162 | // First 16 bytes are a header. |
| 163 | static const unsigned = 16; |
| 164 | |
| 165 | public: |
| 166 | const size_t hashSize; |
| 167 | BuildIdSection(Ctx &); |
| 168 | void writeTo(uint8_t *buf) override; |
| 169 | size_t getSize() const override { return headerSize + hashSize; } |
| 170 | void writeBuildId(llvm::ArrayRef<uint8_t> buf); |
| 171 | |
| 172 | private: |
| 173 | uint8_t *hashBuf; |
| 174 | }; |
| 175 | |
| 176 | // BssSection is used to reserve space for copy relocations and common symbols. |
| 177 | // We create three instances of this class for .bss, .bss.rel.ro and "COMMON", |
| 178 | // that are used for writable symbols, read-only symbols and common symbols, |
| 179 | // respectively. |
| 180 | class BssSection final : public SyntheticSection { |
| 181 | public: |
| 182 | BssSection(Ctx &, StringRef name, uint64_t size, uint32_t addralign); |
| 183 | void writeTo(uint8_t *) override {} |
| 184 | bool isNeeded() const override { return size != 0; } |
| 185 | size_t getSize() const override { return size; } |
| 186 | |
| 187 | static bool classof(const SectionBase *s) { |
| 188 | return isa<SyntheticSection>(Val: s) && cast<SyntheticSection>(Val: s)->bss; |
| 189 | } |
| 190 | uint64_t size; |
| 191 | }; |
| 192 | |
| 193 | class MipsGotSection final : public SyntheticSection { |
| 194 | public: |
| 195 | MipsGotSection(Ctx &); |
| 196 | void writeTo(uint8_t *buf) override; |
| 197 | size_t getSize() const override { return size; } |
| 198 | bool updateAllocSize(Ctx &) override; |
| 199 | void finalizeContents() override; |
| 200 | bool isNeeded() const override; |
| 201 | |
| 202 | // Join separate GOTs built for each input file to generate |
| 203 | // primary and optional multiple secondary GOTs. |
| 204 | void build(); |
| 205 | |
| 206 | void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr); |
| 207 | void addDynTlsEntry(InputFile &file, Symbol &sym); |
| 208 | void addTlsIndex(InputFile &file); |
| 209 | |
| 210 | uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s, |
| 211 | int64_t addend) const; |
| 212 | uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s, |
| 213 | int64_t addend) const; |
| 214 | uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const; |
| 215 | uint64_t getTlsIndexOffset(const InputFile *f) const; |
| 216 | |
| 217 | // Returns the symbol which corresponds to the first entry of the global part |
| 218 | // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic |
| 219 | // table properties. |
| 220 | // Returns nullptr if the global part is empty. |
| 221 | const Symbol *getFirstGlobalEntry() const; |
| 222 | |
| 223 | // Returns the number of entries in the local part of GOT including |
| 224 | // the number of reserved entries. |
| 225 | unsigned getLocalEntriesNum() const; |
| 226 | |
| 227 | // Return _gp value for primary GOT (nullptr) or particular input file. |
| 228 | uint64_t getGp(const InputFile *f = nullptr) const; |
| 229 | |
| 230 | private: |
| 231 | // MIPS GOT consists of three parts: local, global and tls. Each part |
| 232 | // contains different types of entries. Here is a layout of GOT: |
| 233 | // - Header entries | |
| 234 | // - Page entries | Local part |
| 235 | // - Local entries (16-bit access) | |
| 236 | // - Local entries (32-bit access) | |
| 237 | // - Normal global entries || Global part |
| 238 | // - Reloc-only global entries || |
| 239 | // - TLS entries ||| TLS part |
| 240 | // |
| 241 | // Header: |
| 242 | // Two entries hold predefined value 0x0 and 0x80000000. |
| 243 | // Page entries: |
| 244 | // These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16 |
| 245 | // relocation against local symbols. They are initialized by higher 16-bit |
| 246 | // of the corresponding symbol's value. So each 64kb of address space |
| 247 | // requires a single GOT entry. |
| 248 | // Local entries (16-bit access): |
| 249 | // These entries created by GOT relocations against global non-preemptible |
| 250 | // symbols so dynamic linker is not necessary to resolve the symbol's |
| 251 | // values. "16-bit access" means that corresponding relocations address |
| 252 | // GOT using 16-bit index. Each unique Symbol-Addend pair has its own |
| 253 | // GOT entry. |
| 254 | // Local entries (32-bit access): |
| 255 | // These entries are the same as above but created by relocations which |
| 256 | // address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc). |
| 257 | // Normal global entries: |
| 258 | // These entries created by GOT relocations against preemptible global |
| 259 | // symbols. They need to be initialized by dynamic linker and they ordered |
| 260 | // exactly as the corresponding entries in the dynamic symbols table. |
| 261 | // Reloc-only global entries: |
| 262 | // These entries created for symbols that are referenced by dynamic |
| 263 | // relocations R_MIPS_REL32. These entries are not accessed with gp-relative |
| 264 | // addressing, but MIPS ABI requires that these entries be present in GOT. |
| 265 | // TLS entries: |
| 266 | // Entries created by TLS relocations. |
| 267 | // |
| 268 | // If the sum of local, global and tls entries is less than 64K only single |
| 269 | // got is enough. Otherwise, multi-got is created. Series of primary and |
| 270 | // multiple secondary GOTs have the following layout: |
| 271 | // - Primary GOT |
| 272 | // Header |
| 273 | // Local entries |
| 274 | // Global entries |
| 275 | // Relocation only entries |
| 276 | // TLS entries |
| 277 | // |
| 278 | // - Secondary GOT |
| 279 | // Local entries |
| 280 | // Global entries |
| 281 | // TLS entries |
| 282 | // ... |
| 283 | // |
| 284 | // All GOT entries required by relocations from a single input file entirely |
| 285 | // belong to either primary or one of secondary GOTs. To reference GOT entries |
| 286 | // each GOT has its own _gp value points to the "middle" of the GOT. |
| 287 | // In the code this value loaded to the register which is used for GOT access. |
| 288 | // |
| 289 | // MIPS 32 function's prologue: |
| 290 | // lui v0,0x0 |
| 291 | // 0: R_MIPS_HI16 _gp_disp |
| 292 | // addiu v0,v0,0 |
| 293 | // 4: R_MIPS_LO16 _gp_disp |
| 294 | // |
| 295 | // MIPS 64: |
| 296 | // lui at,0x0 |
| 297 | // 14: R_MIPS_GPREL16 main |
| 298 | // |
| 299 | // Dynamic linker does not know anything about secondary GOTs and cannot |
| 300 | // use a regular MIPS mechanism for GOT entries initialization. So we have |
| 301 | // to use an approach accepted by other architectures and create dynamic |
| 302 | // relocations R_MIPS_REL32 to initialize global entries (and local in case |
| 303 | // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker |
| 304 | // requires GOT entries and correspondingly ordered dynamic symbol table |
| 305 | // entries to deal with dynamic relocations. To handle this problem |
| 306 | // relocation-only section in the primary GOT contains entries for all |
| 307 | // symbols referenced in global parts of secondary GOTs. Although the sum |
| 308 | // of local and normal global entries of the primary got should be less |
| 309 | // than 64K, the size of the primary got (including relocation-only entries |
| 310 | // can be greater than 64K, because parts of the primary got that overflow |
| 311 | // the 64K limit are used only by the dynamic linker at dynamic link-time |
| 312 | // and not by 16-bit gp-relative addressing at run-time. |
| 313 | // |
| 314 | // For complete multi-GOT description see the following link |
| 315 | // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT |
| 316 | |
| 317 | // Number of "Header" entries. |
| 318 | static const unsigned = 2; |
| 319 | |
| 320 | uint64_t size = 0; |
| 321 | |
| 322 | // Symbol and addend. |
| 323 | using GotEntry = std::pair<Symbol *, int64_t>; |
| 324 | |
| 325 | struct FileGot { |
| 326 | InputFile *file = nullptr; |
| 327 | size_t startIndex = 0; |
| 328 | |
| 329 | struct PageBlock { |
| 330 | size_t firstIndex; |
| 331 | size_t count; |
| 332 | PageBlock() : firstIndex(0), count(0) {} |
| 333 | }; |
| 334 | |
| 335 | // Map output sections referenced by MIPS GOT relocations |
| 336 | // to the description (index/count) "page" entries allocated |
| 337 | // for this section. |
| 338 | llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap; |
| 339 | // Maps from Symbol+Addend pair or just Symbol to the GOT entry index. |
| 340 | llvm::MapVector<GotEntry, size_t> local16; |
| 341 | llvm::MapVector<GotEntry, size_t> local32; |
| 342 | llvm::MapVector<Symbol *, size_t> global; |
| 343 | llvm::MapVector<Symbol *, size_t> relocs; |
| 344 | llvm::MapVector<Symbol *, size_t> tls; |
| 345 | // Set of symbols referenced by dynamic TLS relocations. |
| 346 | llvm::MapVector<Symbol *, size_t> dynTlsSymbols; |
| 347 | |
| 348 | // Total number of all entries. |
| 349 | size_t getEntriesNum() const; |
| 350 | // Number of "page" entries. |
| 351 | size_t getPageEntriesNum() const; |
| 352 | // Number of entries require 16-bit index to access. |
| 353 | size_t getIndexedEntriesNum() const; |
| 354 | }; |
| 355 | |
| 356 | // Container of GOT created for each input file. |
| 357 | // After building a final series of GOTs this container |
| 358 | // holds primary and secondary GOT's. |
| 359 | std::vector<FileGot> gots; |
| 360 | |
| 361 | // Return (and create if necessary) `FileGot`. |
| 362 | FileGot &getGot(InputFile &f); |
| 363 | |
| 364 | // Try to merge two GOTs. In case of success the `Dst` contains |
| 365 | // result of merging and the function returns true. In case of |
| 366 | // overflow the `Dst` is unchanged and the function returns false. |
| 367 | bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary); |
| 368 | }; |
| 369 | |
| 370 | class GotPltSection final : public SyntheticSection { |
| 371 | public: |
| 372 | GotPltSection(Ctx &); |
| 373 | void addEntry(Symbol &sym); |
| 374 | size_t getSize() const override; |
| 375 | void writeTo(uint8_t *buf) override; |
| 376 | bool isNeeded() const override; |
| 377 | |
| 378 | // Flag to force GotPlt to be in output if we have relocations |
| 379 | // that relies on its address. |
| 380 | std::atomic<bool> hasGotPltOffRel = false; |
| 381 | |
| 382 | private: |
| 383 | SmallVector<const Symbol *, 0> entries; |
| 384 | }; |
| 385 | |
| 386 | // The IgotPltSection is a Got associated with the PltSection for GNU Ifunc |
| 387 | // Symbols that will be relocated by Target->IRelativeRel. |
| 388 | // On most Targets the IgotPltSection will immediately follow the GotPltSection |
| 389 | // on ARM the IgotPltSection will immediately follow the GotSection. |
| 390 | class IgotPltSection final : public SyntheticSection { |
| 391 | public: |
| 392 | IgotPltSection(Ctx &); |
| 393 | void addEntry(Symbol &sym); |
| 394 | size_t getSize() const override; |
| 395 | void writeTo(uint8_t *buf) override; |
| 396 | bool isNeeded() const override { return !entries.empty(); } |
| 397 | |
| 398 | private: |
| 399 | SmallVector<const Symbol *, 0> entries; |
| 400 | }; |
| 401 | |
| 402 | class StringTableSection final : public SyntheticSection { |
| 403 | public: |
| 404 | StringTableSection(Ctx &, StringRef name, bool dynamic); |
| 405 | unsigned addString(StringRef s, bool hashIt = true); |
| 406 | void writeTo(uint8_t *buf) override; |
| 407 | size_t getSize() const override { return size; } |
| 408 | bool isDynamic() const { return dynamic; } |
| 409 | |
| 410 | private: |
| 411 | const bool dynamic; |
| 412 | |
| 413 | uint64_t size = 0; |
| 414 | |
| 415 | llvm::DenseMap<llvm::CachedHashStringRef, unsigned> stringMap; |
| 416 | SmallVector<StringRef, 0> strings; |
| 417 | }; |
| 418 | |
| 419 | class DynamicReloc { |
| 420 | public: |
| 421 | enum Kind { |
| 422 | /// The resulting dynamic relocation does not reference a symbol (#sym must |
| 423 | /// be nullptr) and uses #addend as the result of computeAddend(ctx). |
| 424 | AddendOnly, |
| 425 | /// The resulting dynamic relocation will not reference a symbol: #sym is |
| 426 | /// only used to compute the addend with InputSection::getRelocTargetVA(). |
| 427 | /// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64). |
| 428 | AddendOnlyWithTargetVA, |
| 429 | /// The resulting dynamic relocation references symbol #sym from the dynamic |
| 430 | /// symbol table and uses #addend as the value of computeAddend(ctx). |
| 431 | AgainstSymbol, |
| 432 | /// The resulting dynamic relocation references symbol #sym from the dynamic |
| 433 | /// symbol table and uses InputSection::getRelocTargetVA() + #addend for the |
| 434 | /// final addend. It can be used for relocations that write the symbol VA as |
| 435 | // the addend (e.g. R_MIPS_TLS_TPREL64) but still reference the symbol. |
| 436 | AgainstSymbolWithTargetVA, |
| 437 | /// This is used by the MIPS multi-GOT implementation. It relocates |
| 438 | /// addresses of 64kb pages that lie inside the output section. |
| 439 | MipsMultiGotPage, |
| 440 | }; |
| 441 | /// This constructor records a relocation against a symbol. |
| 442 | DynamicReloc(RelType type, const InputSectionBase *inputSec, |
| 443 | uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend, |
| 444 | RelExpr expr) |
| 445 | : sym(&sym), inputSec(inputSec), offsetInSec(offsetInSec), type(type), |
| 446 | addend(addend), kind(kind), expr(expr) {} |
| 447 | /// This constructor records a relative relocation with no symbol. |
| 448 | DynamicReloc(RelType type, const InputSectionBase *inputSec, |
| 449 | uint64_t offsetInSec, int64_t addend = 0) |
| 450 | : sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec), type(type), |
| 451 | addend(addend), kind(AddendOnly), expr(R_ADDEND) {} |
| 452 | /// This constructor records dynamic relocation settings used by the MIPS |
| 453 | /// multi-GOT implementation. |
| 454 | DynamicReloc(RelType type, const InputSectionBase *inputSec, |
| 455 | uint64_t offsetInSec, const OutputSection *outputSec, |
| 456 | int64_t addend) |
| 457 | : sym(nullptr), outputSec(outputSec), inputSec(inputSec), |
| 458 | offsetInSec(offsetInSec), type(type), addend(addend), |
| 459 | kind(MipsMultiGotPage), expr(R_ADDEND) {} |
| 460 | |
| 461 | uint64_t getOffset() const; |
| 462 | uint32_t getSymIndex(SymbolTableBaseSection *symTab) const; |
| 463 | bool needsDynSymIndex() const { |
| 464 | return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA; |
| 465 | } |
| 466 | |
| 467 | /// Computes the addend of the dynamic relocation. Note that this is not the |
| 468 | /// same as the #addend member variable as it may also include the symbol |
| 469 | /// address/the address of the corresponding GOT entry/etc. |
| 470 | int64_t computeAddend(Ctx &) const; |
| 471 | |
| 472 | void computeRaw(Ctx &, SymbolTableBaseSection *symt); |
| 473 | |
| 474 | Symbol *sym; |
| 475 | const OutputSection *outputSec = nullptr; |
| 476 | const InputSectionBase *inputSec; |
| 477 | uint64_t offsetInSec; |
| 478 | uint64_t r_offset; |
| 479 | RelType type; |
| 480 | uint32_t r_sym; |
| 481 | // Initially input addend, then the output addend after |
| 482 | // RelocationSection<ELFT>::writeTo. |
| 483 | int64_t addend; |
| 484 | |
| 485 | private: |
| 486 | Kind kind; |
| 487 | // The kind of expression used to calculate the added (required e.g. for |
| 488 | // relative GOT relocations). |
| 489 | RelExpr expr; |
| 490 | }; |
| 491 | |
| 492 | template <class ELFT> class DynamicSection final : public SyntheticSection { |
| 493 | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
| 494 | |
| 495 | public: |
| 496 | DynamicSection(Ctx &); |
| 497 | void finalizeContents() override; |
| 498 | void writeTo(uint8_t *buf) override; |
| 499 | size_t getSize() const override { return size; } |
| 500 | |
| 501 | private: |
| 502 | std::vector<std::pair<int32_t, uint64_t>> computeContents(); |
| 503 | uint64_t size = 0; |
| 504 | }; |
| 505 | |
| 506 | class RelocationBaseSection : public SyntheticSection { |
| 507 | public: |
| 508 | RelocationBaseSection(Ctx &, StringRef name, uint32_t type, |
| 509 | int32_t dynamicTag, int32_t sizeDynamicTag, |
| 510 | bool combreloc, unsigned concurrency); |
| 511 | /// Add a dynamic relocation without writing an addend to the output section. |
| 512 | /// This overload can be used if the addends are written directly instead of |
| 513 | /// using relocations on the input section (e.g. MipsGotSection::writeTo()). |
| 514 | template <bool shard = false> void addReloc(const DynamicReloc &reloc) { |
| 515 | relocs.push_back(Elt: reloc); |
| 516 | } |
| 517 | /// Add a dynamic relocation against \p sym with an optional addend. |
| 518 | void addSymbolReloc(RelType dynType, InputSectionBase &isec, |
| 519 | uint64_t offsetInSec, Symbol &sym, int64_t addend = 0, |
| 520 | std::optional<RelType> addendRelType = {}); |
| 521 | /// Add a relative dynamic relocation that uses the target address of \p sym |
| 522 | /// (i.e. InputSection::getRelocTargetVA()) + \p addend as the addend. |
| 523 | /// This function should only be called for non-preemptible symbols or |
| 524 | /// RelExpr values that refer to an address inside the output file (e.g. the |
| 525 | /// address of the GOT entry for a potentially preemptible symbol). |
| 526 | template <bool shard = false> |
| 527 | void addRelativeReloc(RelType dynType, InputSectionBase &isec, |
| 528 | uint64_t offsetInSec, Symbol &sym, int64_t addend, |
| 529 | RelType addendRelType, RelExpr expr) { |
| 530 | assert(expr != R_ADDEND && "expected non-addend relocation expression" ); |
| 531 | addReloc<shard>(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec, |
| 532 | offsetInSec, sym, addend, expr, addendRelType); |
| 533 | } |
| 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 | template <bool shard = false> |
| 541 | void addReloc(DynamicReloc::Kind kind, RelType dynType, InputSectionBase &sec, |
| 542 | uint64_t offsetInSec, Symbol &sym, int64_t addend, RelExpr expr, |
| 543 | RelType addendRelType) { |
| 544 | // Write the addends to the relocated address if required. We skip |
| 545 | // it if the written value would be zero. |
| 546 | if (ctx.arg.writeAddends && (expr != R_ADDEND || addend != 0)) |
| 547 | sec.addReloc(r: {.expr: expr, .type: addendRelType, .offset: offsetInSec, .addend: addend, .sym: &sym}); |
| 548 | addReloc<shard>({dynType, &sec, offsetInSec, kind, sym, addend, expr}); |
| 549 | } |
| 550 | bool isNeeded() const override { |
| 551 | return !relocs.empty() || |
| 552 | llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); }); |
| 553 | } |
| 554 | size_t getSize() const override { return relocs.size() * this->entsize; } |
| 555 | size_t getRelativeRelocCount() const { return numRelativeRelocs; } |
| 556 | void mergeRels(); |
| 557 | void partitionRels(); |
| 558 | void finalizeContents() override; |
| 559 | |
| 560 | int32_t dynamicTag, sizeDynamicTag; |
| 561 | SmallVector<DynamicReloc, 0> relocs; |
| 562 | |
| 563 | protected: |
| 564 | void computeRels(); |
| 565 | // Used when parallel relocation scanning adds relocations. The elements |
| 566 | // will be moved into relocs by mergeRel(). |
| 567 | SmallVector<SmallVector<DynamicReloc, 0>, 0> relocsVec; |
| 568 | size_t numRelativeRelocs = 0; // used by -z combreloc |
| 569 | bool combreloc; |
| 570 | }; |
| 571 | |
| 572 | template <> |
| 573 | inline void RelocationBaseSection::addReloc<true>(const DynamicReloc &reloc) { |
| 574 | relocsVec[llvm::parallel::getThreadIndex()].push_back(Elt: reloc); |
| 575 | } |
| 576 | |
| 577 | template <class ELFT> |
| 578 | class RelocationSection final : public RelocationBaseSection { |
| 579 | using Elf_Rel = typename ELFT::Rel; |
| 580 | using Elf_Rela = typename ELFT::Rela; |
| 581 | |
| 582 | public: |
| 583 | RelocationSection(Ctx &, StringRef name, bool combreloc, |
| 584 | unsigned concurrency); |
| 585 | void writeTo(uint8_t *buf) override; |
| 586 | }; |
| 587 | |
| 588 | template <class ELFT> |
| 589 | class AndroidPackedRelocationSection final : public RelocationBaseSection { |
| 590 | using Elf_Rel = typename ELFT::Rel; |
| 591 | using Elf_Rela = typename ELFT::Rela; |
| 592 | |
| 593 | public: |
| 594 | AndroidPackedRelocationSection(Ctx &, StringRef name, unsigned concurrency); |
| 595 | |
| 596 | bool updateAllocSize(Ctx &) override; |
| 597 | size_t getSize() const override { return relocData.size(); } |
| 598 | void writeTo(uint8_t *buf) override { |
| 599 | memcpy(dest: buf, src: relocData.data(), n: relocData.size()); |
| 600 | } |
| 601 | |
| 602 | private: |
| 603 | SmallVector<char, 0> relocData; |
| 604 | }; |
| 605 | |
| 606 | struct RelativeReloc { |
| 607 | uint64_t getOffset() const { |
| 608 | return inputSec->getVA(offset: inputSec->relocs()[relocIdx].offset); |
| 609 | } |
| 610 | |
| 611 | const InputSectionBase *inputSec; |
| 612 | size_t relocIdx; |
| 613 | }; |
| 614 | |
| 615 | class RelrBaseSection : public SyntheticSection { |
| 616 | public: |
| 617 | RelrBaseSection(Ctx &, unsigned concurrency, bool isAArch64Auth = false); |
| 618 | void mergeRels(); |
| 619 | bool isNeeded() const override { |
| 620 | return !relocs.empty() || |
| 621 | llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); }); |
| 622 | } |
| 623 | SmallVector<RelativeReloc, 0> relocs; |
| 624 | SmallVector<SmallVector<RelativeReloc, 0>, 0> relocsVec; |
| 625 | }; |
| 626 | |
| 627 | // RelrSection is used to encode offsets for relative relocations. |
| 628 | // Proposal for adding SHT_RELR sections to generic-abi is here: |
| 629 | // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg |
| 630 | // For more details, see the comment in RelrSection::updateAllocSize(Ctx &ctx). |
| 631 | template <class ELFT> class RelrSection final : public RelrBaseSection { |
| 632 | using Elf_Relr = typename ELFT::Relr; |
| 633 | |
| 634 | public: |
| 635 | RelrSection(Ctx &, unsigned concurrency, bool isAArch64Auth = false); |
| 636 | |
| 637 | bool updateAllocSize(Ctx &) override; |
| 638 | size_t getSize() const override { return relrRelocs.size() * this->entsize; } |
| 639 | void writeTo(uint8_t *buf) override { |
| 640 | memcpy(buf, relrRelocs.data(), getSize()); |
| 641 | } |
| 642 | |
| 643 | private: |
| 644 | SmallVector<Elf_Relr, 0> relrRelocs; |
| 645 | }; |
| 646 | |
| 647 | struct SymbolTableEntry { |
| 648 | Symbol *sym; |
| 649 | size_t strTabOffset; |
| 650 | }; |
| 651 | |
| 652 | class SymbolTableBaseSection : public SyntheticSection { |
| 653 | public: |
| 654 | SymbolTableBaseSection(Ctx &ctx, StringTableSection &strTabSec); |
| 655 | void finalizeContents() override; |
| 656 | size_t getSize() const override { return getNumSymbols() * entsize; } |
| 657 | void addSymbol(Symbol *sym); |
| 658 | unsigned getNumSymbols() const { return symbols.size() + 1; } |
| 659 | size_t getSymbolIndex(const Symbol &sym); |
| 660 | ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; } |
| 661 | |
| 662 | protected: |
| 663 | void sortSymTabSymbols(); |
| 664 | |
| 665 | // A vector of symbols and their string table offsets. |
| 666 | SmallVector<SymbolTableEntry, 0> symbols; |
| 667 | |
| 668 | StringTableSection &strTabSec; |
| 669 | |
| 670 | llvm::once_flag onceFlag; |
| 671 | llvm::DenseMap<Symbol *, size_t> symbolIndexMap; |
| 672 | llvm::DenseMap<OutputSection *, size_t> sectionIndexMap; |
| 673 | }; |
| 674 | |
| 675 | template <class ELFT> |
| 676 | class SymbolTableSection final : public SymbolTableBaseSection { |
| 677 | using Elf_Sym = typename ELFT::Sym; |
| 678 | |
| 679 | public: |
| 680 | SymbolTableSection(Ctx &, StringTableSection &strTabSec); |
| 681 | void writeTo(uint8_t *buf) override; |
| 682 | }; |
| 683 | |
| 684 | class SymtabShndxSection final : public SyntheticSection { |
| 685 | public: |
| 686 | SymtabShndxSection(Ctx &); |
| 687 | |
| 688 | void writeTo(uint8_t *buf) override; |
| 689 | size_t getSize() const override; |
| 690 | bool isNeeded() const override; |
| 691 | void finalizeContents() override; |
| 692 | }; |
| 693 | |
| 694 | // Outputs GNU Hash section. For detailed explanation see: |
| 695 | // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections |
| 696 | class GnuHashTableSection final : public SyntheticSection { |
| 697 | public: |
| 698 | GnuHashTableSection(Ctx &); |
| 699 | void finalizeContents() override; |
| 700 | void writeTo(uint8_t *buf) override; |
| 701 | size_t getSize() const override { return size; } |
| 702 | |
| 703 | // Adds symbols to the hash table. |
| 704 | // Sorts the input to satisfy GNU hash section requirements. |
| 705 | void addSymbols(llvm::SmallVectorImpl<SymbolTableEntry> &symbols); |
| 706 | |
| 707 | private: |
| 708 | // See the comment in writeBloomFilter. |
| 709 | enum { Shift2 = 26 }; |
| 710 | |
| 711 | struct Entry { |
| 712 | Symbol *sym; |
| 713 | size_t strTabOffset; |
| 714 | uint32_t hash; |
| 715 | uint32_t bucketIdx; |
| 716 | }; |
| 717 | |
| 718 | SmallVector<Entry, 0> symbols; |
| 719 | size_t maskWords; |
| 720 | size_t nBuckets = 0; |
| 721 | size_t size = 0; |
| 722 | }; |
| 723 | |
| 724 | class HashTableSection final : public SyntheticSection { |
| 725 | public: |
| 726 | HashTableSection(Ctx &); |
| 727 | void finalizeContents() override; |
| 728 | void writeTo(uint8_t *buf) override; |
| 729 | size_t getSize() const override { return size; } |
| 730 | |
| 731 | private: |
| 732 | size_t size = 0; |
| 733 | }; |
| 734 | |
| 735 | // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT |
| 736 | // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily |
| 737 | // at runtime. |
| 738 | // |
| 739 | // On PowerPC, this section contains lazy symbol resolvers. A branch instruction |
| 740 | // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a |
| 741 | // lazy symbol resolver. |
| 742 | // |
| 743 | // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs. |
| 744 | // A call instruction jumps to a .plt.sec entry, which will then jump to the |
| 745 | // target (BIND_NOW) or a .plt entry. |
| 746 | class PltSection : public SyntheticSection { |
| 747 | public: |
| 748 | PltSection(Ctx &); |
| 749 | void writeTo(uint8_t *buf) override; |
| 750 | size_t getSize() const override; |
| 751 | bool isNeeded() const override; |
| 752 | void addSymbols(); |
| 753 | void addEntry(Symbol &sym); |
| 754 | size_t getNumEntries() const { return entries.size(); } |
| 755 | |
| 756 | size_t ; |
| 757 | |
| 758 | SmallVector<const Symbol *, 0> entries; |
| 759 | }; |
| 760 | |
| 761 | // Used for non-preemptible ifuncs. It does not have a header. Each entry is |
| 762 | // associated with an IRELATIVE relocation, which will be resolved eagerly at |
| 763 | // runtime. PltSection can only contain entries associated with JUMP_SLOT |
| 764 | // relocations, so IPLT entries are in a separate section. |
| 765 | class IpltSection final : public SyntheticSection { |
| 766 | SmallVector<const Symbol *, 0> entries; |
| 767 | |
| 768 | public: |
| 769 | IpltSection(Ctx &); |
| 770 | void writeTo(uint8_t *buf) override; |
| 771 | size_t getSize() const override; |
| 772 | bool isNeeded() const override { return !entries.empty(); } |
| 773 | void addSymbols(); |
| 774 | void addEntry(Symbol &sym); |
| 775 | }; |
| 776 | |
| 777 | class PPC32GlinkSection : public PltSection { |
| 778 | public: |
| 779 | PPC32GlinkSection(Ctx &); |
| 780 | void writeTo(uint8_t *buf) override; |
| 781 | size_t getSize() const override; |
| 782 | |
| 783 | SmallVector<const Symbol *, 0> canonical_plts; |
| 784 | static constexpr size_t = 64; |
| 785 | }; |
| 786 | |
| 787 | // This is x86-only. |
| 788 | class IBTPltSection : public SyntheticSection { |
| 789 | public: |
| 790 | IBTPltSection(Ctx &); |
| 791 | void writeTo(uint8_t *Buf) override; |
| 792 | bool isNeeded() const override; |
| 793 | size_t getSize() const override; |
| 794 | }; |
| 795 | |
| 796 | // Used to align the end of the PT_GNU_RELRO segment and the associated PT_LOAD |
| 797 | // segment to a common-page-size boundary. This padding section ensures that all |
| 798 | // pages in the PT_LOAD segment is covered by at least one section. |
| 799 | class RelroPaddingSection final : public SyntheticSection { |
| 800 | public: |
| 801 | RelroPaddingSection(Ctx &); |
| 802 | size_t getSize() const override { return 0; } |
| 803 | void writeTo(uint8_t *buf) override {} |
| 804 | }; |
| 805 | |
| 806 | class RandomizePaddingSection final : public SyntheticSection { |
| 807 | uint64_t size; |
| 808 | |
| 809 | public: |
| 810 | RandomizePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent); |
| 811 | size_t getSize() const override { return size; } |
| 812 | void writeTo(uint8_t *buf) override; |
| 813 | }; |
| 814 | |
| 815 | // Used by the merged DWARF32 .debug_names (a per-module index). If we |
| 816 | // move to DWARF64, most of this data will need to be re-sized. |
| 817 | class DebugNamesBaseSection : public SyntheticSection { |
| 818 | public: |
| 819 | struct Abbrev : llvm::FoldingSetNode { |
| 820 | uint32_t code; |
| 821 | uint32_t tag; |
| 822 | SmallVector<llvm::DWARFDebugNames::AttributeEncoding, 2> attributes; |
| 823 | |
| 824 | void Profile(llvm::FoldingSetNodeID &id) const; |
| 825 | }; |
| 826 | |
| 827 | struct AttrValue { |
| 828 | uint32_t attrValue; |
| 829 | uint8_t attrSize; |
| 830 | }; |
| 831 | |
| 832 | struct IndexEntry { |
| 833 | uint32_t abbrevCode; |
| 834 | uint32_t poolOffset; |
| 835 | union { |
| 836 | uint64_t parentOffset = 0; |
| 837 | IndexEntry *parentEntry; |
| 838 | }; |
| 839 | SmallVector<AttrValue, 3> attrValues; |
| 840 | }; |
| 841 | |
| 842 | struct NameEntry { |
| 843 | const char *name; |
| 844 | uint32_t hashValue; |
| 845 | uint32_t stringOffset; |
| 846 | uint32_t entryOffset; |
| 847 | // Used to relocate `stringOffset` in the merged section. |
| 848 | uint32_t chunkIdx; |
| 849 | SmallVector<IndexEntry *, 0> indexEntries; |
| 850 | |
| 851 | llvm::iterator_range< |
| 852 | llvm::pointee_iterator<typename SmallVector<IndexEntry *, 0>::iterator>> |
| 853 | entries() { |
| 854 | return llvm::make_pointee_range(Range&: indexEntries); |
| 855 | } |
| 856 | }; |
| 857 | |
| 858 | // The contents of one input .debug_names section. An InputChunk |
| 859 | // typically contains one NameData, but might contain more, especially |
| 860 | // in LTO builds. |
| 861 | struct NameData { |
| 862 | llvm::DWARFDebugNames::Header hdr; |
| 863 | llvm::DenseMap<uint32_t, uint32_t> abbrevCodeMap; |
| 864 | SmallVector<NameEntry, 0> nameEntries; |
| 865 | }; |
| 866 | |
| 867 | // InputChunk and OutputChunk hold per-file contributions to the merged index. |
| 868 | // InputChunk instances will be discarded after `init` completes. |
| 869 | struct InputChunk { |
| 870 | uint32_t baseCuIdx; |
| 871 | LLDDWARFSection section; |
| 872 | SmallVector<NameData, 0> nameData; |
| 873 | std::optional<llvm::DWARFDebugNames> llvmDebugNames; |
| 874 | }; |
| 875 | |
| 876 | struct OutputChunk { |
| 877 | // Pointer to the .debug_info section that contains compile units, used to |
| 878 | // compute the relocated CU offsets. |
| 879 | InputSection *infoSec; |
| 880 | // This initially holds section offsets. After relocation, the section |
| 881 | // offsets are changed to CU offsets relative the the output section. |
| 882 | SmallVector<uint32_t, 0> compUnits; |
| 883 | }; |
| 884 | |
| 885 | DebugNamesBaseSection(Ctx &); |
| 886 | size_t getSize() const override { return size; } |
| 887 | bool isNeeded() const override { return numChunks > 0; } |
| 888 | |
| 889 | protected: |
| 890 | void init(llvm::function_ref<void(InputFile *, InputChunk &, OutputChunk &)>); |
| 891 | static void |
| 892 | (Ctx &, InputChunk &inputChunk, OutputChunk &chunk, |
| 893 | llvm::DWARFDataExtractor &, |
| 894 | llvm::DataExtractor &, |
| 895 | llvm::function_ref<SmallVector<uint32_t, 0>( |
| 896 | uint32_t numCUs, const llvm::DWARFDebugNames::Header &hdr, |
| 897 | const llvm::DWARFDebugNames::DWARFDebugNamesOffsets &)> |
| 898 | readOffsets); |
| 899 | void computeHdrAndAbbrevTable(MutableArrayRef<InputChunk> inputChunks); |
| 900 | std::pair<uint32_t, uint32_t> |
| 901 | computeEntryPool(MutableArrayRef<InputChunk> inputChunks); |
| 902 | |
| 903 | // Input .debug_names sections for relocating string offsets in the name table |
| 904 | // in `finalizeContents`. |
| 905 | SmallVector<InputSection *, 0> inputSections; |
| 906 | |
| 907 | llvm::DWARFDebugNames::Header hdr; |
| 908 | size_t numChunks; |
| 909 | std::unique_ptr<OutputChunk[]> chunks; |
| 910 | llvm::SpecificBumpPtrAllocator<Abbrev> abbrevAlloc; |
| 911 | SmallVector<Abbrev *, 0> abbrevTable; |
| 912 | SmallVector<char, 0> abbrevTableBuf; |
| 913 | |
| 914 | ArrayRef<OutputChunk> getChunks() const { |
| 915 | return ArrayRef(chunks.get(), numChunks); |
| 916 | } |
| 917 | |
| 918 | // Sharded name entries that will be used to compute bucket_count and the |
| 919 | // count name table. |
| 920 | static constexpr size_t numShards = 32; |
| 921 | SmallVector<NameEntry, 0> nameVecs[numShards]; |
| 922 | }; |
| 923 | |
| 924 | // Complement DebugNamesBaseSection for ELFT-aware code: reading offsets, |
| 925 | // relocating string offsets, and writeTo. |
| 926 | template <class ELFT> |
| 927 | class DebugNamesSection final : public DebugNamesBaseSection { |
| 928 | public: |
| 929 | DebugNamesSection(Ctx &); |
| 930 | void finalizeContents() override; |
| 931 | void writeTo(uint8_t *buf) override; |
| 932 | |
| 933 | template <class RelTy> |
| 934 | void getNameRelocs(const InputFile &file, |
| 935 | llvm::DenseMap<uint32_t, uint32_t> &relocs, |
| 936 | Relocs<RelTy> rels); |
| 937 | |
| 938 | private: |
| 939 | static void (InputChunk &inputChunk, OutputChunk &chunk, |
| 940 | llvm::DWARFDataExtractor &, |
| 941 | llvm::DataExtractor &); |
| 942 | }; |
| 943 | |
| 944 | class GdbIndexSection final : public SyntheticSection { |
| 945 | public: |
| 946 | struct AddressEntry { |
| 947 | InputSection *section; |
| 948 | uint64_t lowAddress; |
| 949 | uint64_t highAddress; |
| 950 | uint32_t cuIndex; |
| 951 | }; |
| 952 | |
| 953 | struct CuEntry { |
| 954 | uint64_t cuOffset; |
| 955 | uint64_t cuLength; |
| 956 | }; |
| 957 | |
| 958 | struct NameAttrEntry { |
| 959 | llvm::CachedHashStringRef name; |
| 960 | uint32_t cuIndexAndAttrs; |
| 961 | }; |
| 962 | |
| 963 | struct GdbChunk { |
| 964 | InputSection *sec; |
| 965 | SmallVector<AddressEntry, 0> addressAreas; |
| 966 | SmallVector<CuEntry, 0> compilationUnits; |
| 967 | }; |
| 968 | |
| 969 | struct GdbSymbol { |
| 970 | llvm::CachedHashStringRef name; |
| 971 | SmallVector<uint32_t, 0> cuVector; |
| 972 | uint32_t nameOff; |
| 973 | uint32_t cuVectorOff; |
| 974 | }; |
| 975 | |
| 976 | GdbIndexSection(Ctx &); |
| 977 | template <typename ELFT> |
| 978 | static std::unique_ptr<GdbIndexSection> create(Ctx &); |
| 979 | void writeTo(uint8_t *buf) override; |
| 980 | size_t getSize() const override { return size; } |
| 981 | bool isNeeded() const override; |
| 982 | |
| 983 | private: |
| 984 | struct { |
| 985 | llvm::support::ulittle32_t ; |
| 986 | llvm::support::ulittle32_t ; |
| 987 | llvm::support::ulittle32_t ; |
| 988 | llvm::support::ulittle32_t ; |
| 989 | llvm::support::ulittle32_t ; |
| 990 | llvm::support::ulittle32_t ; |
| 991 | }; |
| 992 | |
| 993 | size_t computeSymtabSize() const; |
| 994 | |
| 995 | // Each chunk contains information gathered from debug sections of a |
| 996 | // single object file. |
| 997 | SmallVector<GdbChunk, 0> chunks; |
| 998 | |
| 999 | // A symbol table for this .gdb_index section. |
| 1000 | SmallVector<GdbSymbol, 0> symbols; |
| 1001 | |
| 1002 | size_t size; |
| 1003 | }; |
| 1004 | |
| 1005 | // --eh-frame-hdr option tells linker to construct a header for all the |
| 1006 | // .eh_frame sections. This header is placed to a section named .eh_frame_hdr |
| 1007 | // and also to a PT_GNU_EH_FRAME segment. |
| 1008 | // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by |
| 1009 | // calling dl_iterate_phdr. |
| 1010 | // This section contains a lookup table for quick binary search of FDEs. |
| 1011 | // Detailed info about internals can be found in Ian Lance Taylor's blog: |
| 1012 | // http://www.airs.com/blog/archives/460 (".eh_frame") |
| 1013 | // http://www.airs.com/blog/archives/462 (".eh_frame_hdr") |
| 1014 | class final : public SyntheticSection { |
| 1015 | public: |
| 1016 | (Ctx &); |
| 1017 | void (); |
| 1018 | void (uint8_t *buf) override; |
| 1019 | size_t () const override; |
| 1020 | bool () const override; |
| 1021 | }; |
| 1022 | |
| 1023 | // For more information about .gnu.version and .gnu.version_r see: |
| 1024 | // https://www.akkadia.org/drepper/symbol-versioning |
| 1025 | |
| 1026 | // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall |
| 1027 | // contain symbol version definitions. The number of entries in this section |
| 1028 | // shall be contained in the DT_VERDEFNUM entry of the .dynamic section. |
| 1029 | // The section shall contain an array of Elf_Verdef structures, optionally |
| 1030 | // followed by an array of Elf_Verdaux structures. |
| 1031 | class VersionDefinitionSection final : public SyntheticSection { |
| 1032 | public: |
| 1033 | VersionDefinitionSection(Ctx &); |
| 1034 | void finalizeContents() override; |
| 1035 | size_t getSize() const override; |
| 1036 | void writeTo(uint8_t *buf) override; |
| 1037 | |
| 1038 | private: |
| 1039 | enum { EntrySize = 28 }; |
| 1040 | void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff); |
| 1041 | StringRef getFileDefName(); |
| 1042 | |
| 1043 | unsigned fileDefNameOff; |
| 1044 | SmallVector<unsigned, 0> verDefNameOffs; |
| 1045 | }; |
| 1046 | |
| 1047 | // The .gnu.version section specifies the required version of each symbol in the |
| 1048 | // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol |
| 1049 | // table entry. An Elf_Versym is just a 16-bit integer that refers to a version |
| 1050 | // identifier defined in the either .gnu.version_r or .gnu.version_d section. |
| 1051 | // The values 0 and 1 are reserved. All other values are used for versions in |
| 1052 | // the own object or in any of the dependencies. |
| 1053 | class VersionTableSection final : public SyntheticSection { |
| 1054 | public: |
| 1055 | VersionTableSection(Ctx &); |
| 1056 | void finalizeContents() override; |
| 1057 | size_t getSize() const override; |
| 1058 | void writeTo(uint8_t *buf) override; |
| 1059 | bool isNeeded() const override; |
| 1060 | }; |
| 1061 | |
| 1062 | // The .gnu.version_r section defines the version identifiers used by |
| 1063 | // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each |
| 1064 | // Elf_Verneed specifies the version requirements for a single DSO, and contains |
| 1065 | // a reference to a linked list of Elf_Vernaux data structures which define the |
| 1066 | // mapping from version identifiers to version names. |
| 1067 | template <class ELFT> |
| 1068 | class VersionNeedSection final : public SyntheticSection { |
| 1069 | using Elf_Verneed = typename ELFT::Verneed; |
| 1070 | using Elf_Vernaux = typename ELFT::Vernaux; |
| 1071 | |
| 1072 | struct Vernaux { |
| 1073 | uint64_t hash; |
| 1074 | uint32_t verneedIndex; |
| 1075 | uint64_t nameStrTab; |
| 1076 | }; |
| 1077 | |
| 1078 | struct Verneed { |
| 1079 | uint64_t nameStrTab; |
| 1080 | std::vector<Vernaux> vernauxs; |
| 1081 | }; |
| 1082 | |
| 1083 | SmallVector<Verneed, 0> verneeds; |
| 1084 | |
| 1085 | public: |
| 1086 | VersionNeedSection(Ctx &); |
| 1087 | void finalizeContents() override; |
| 1088 | void writeTo(uint8_t *buf) override; |
| 1089 | size_t getSize() const override; |
| 1090 | bool isNeeded() const override; |
| 1091 | }; |
| 1092 | |
| 1093 | // MergeSyntheticSection is a class that allows us to put mergeable sections |
| 1094 | // with different attributes in a single output sections. To do that |
| 1095 | // we put them into MergeSyntheticSection synthetic input sections which are |
| 1096 | // attached to regular output sections. |
| 1097 | class MergeSyntheticSection : public SyntheticSection { |
| 1098 | public: |
| 1099 | void addSection(MergeInputSection *ms); |
| 1100 | SmallVector<MergeInputSection *, 0> sections; |
| 1101 | |
| 1102 | protected: |
| 1103 | MergeSyntheticSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags, |
| 1104 | uint32_t addralign) |
| 1105 | : SyntheticSection(ctx, name, type, flags, addralign) {} |
| 1106 | }; |
| 1107 | |
| 1108 | class MergeTailSection final : public MergeSyntheticSection { |
| 1109 | public: |
| 1110 | MergeTailSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags, |
| 1111 | uint32_t addralign); |
| 1112 | |
| 1113 | size_t getSize() const override; |
| 1114 | void writeTo(uint8_t *buf) override; |
| 1115 | void finalizeContents() override; |
| 1116 | |
| 1117 | private: |
| 1118 | llvm::StringTableBuilder builder; |
| 1119 | }; |
| 1120 | |
| 1121 | class MergeNoTailSection final : public MergeSyntheticSection { |
| 1122 | public: |
| 1123 | MergeNoTailSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags, |
| 1124 | uint32_t addralign) |
| 1125 | : MergeSyntheticSection(ctx, name, type, flags, addralign) {} |
| 1126 | |
| 1127 | size_t getSize() const override { return size; } |
| 1128 | void writeTo(uint8_t *buf) override; |
| 1129 | void finalizeContents() override; |
| 1130 | |
| 1131 | private: |
| 1132 | // We use the most significant bits of a hash as a shard ID. |
| 1133 | // The reason why we don't want to use the least significant bits is |
| 1134 | // because DenseMap also uses lower bits to determine a bucket ID. |
| 1135 | // If we use lower bits, it significantly increases the probability of |
| 1136 | // hash collisions. |
| 1137 | size_t getShardId(uint32_t hash) { |
| 1138 | assert((hash >> 31) == 0); |
| 1139 | return hash >> (31 - llvm::countr_zero(Val: numShards)); |
| 1140 | } |
| 1141 | |
| 1142 | // Section size |
| 1143 | size_t size; |
| 1144 | |
| 1145 | // String table contents |
| 1146 | constexpr static size_t numShards = 32; |
| 1147 | SmallVector<llvm::StringTableBuilder, 0> shards; |
| 1148 | size_t shardOffsets[numShards]; |
| 1149 | }; |
| 1150 | |
| 1151 | // .MIPS.abiflags section. |
| 1152 | template <class ELFT> |
| 1153 | class MipsAbiFlagsSection final : public SyntheticSection { |
| 1154 | using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>; |
| 1155 | |
| 1156 | public: |
| 1157 | static std::unique_ptr<MipsAbiFlagsSection> create(Ctx &); |
| 1158 | |
| 1159 | MipsAbiFlagsSection(Ctx &, Elf_Mips_ABIFlags flags); |
| 1160 | size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); } |
| 1161 | void writeTo(uint8_t *buf) override; |
| 1162 | |
| 1163 | private: |
| 1164 | Elf_Mips_ABIFlags flags; |
| 1165 | }; |
| 1166 | |
| 1167 | // .MIPS.options section. |
| 1168 | template <class ELFT> class MipsOptionsSection final : public SyntheticSection { |
| 1169 | using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>; |
| 1170 | using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; |
| 1171 | |
| 1172 | public: |
| 1173 | static std::unique_ptr<MipsOptionsSection<ELFT>> create(Ctx &); |
| 1174 | |
| 1175 | MipsOptionsSection(Ctx &, Elf_Mips_RegInfo reginfo); |
| 1176 | void writeTo(uint8_t *buf) override; |
| 1177 | |
| 1178 | size_t getSize() const override { |
| 1179 | return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); |
| 1180 | } |
| 1181 | |
| 1182 | private: |
| 1183 | Elf_Mips_RegInfo reginfo; |
| 1184 | }; |
| 1185 | |
| 1186 | // MIPS .reginfo section. |
| 1187 | template <class ELFT> class MipsReginfoSection final : public SyntheticSection { |
| 1188 | using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; |
| 1189 | |
| 1190 | public: |
| 1191 | static std::unique_ptr<MipsReginfoSection> create(Ctx &); |
| 1192 | |
| 1193 | MipsReginfoSection(Ctx &, Elf_Mips_RegInfo reginfo); |
| 1194 | size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); } |
| 1195 | void writeTo(uint8_t *buf) override; |
| 1196 | |
| 1197 | private: |
| 1198 | Elf_Mips_RegInfo reginfo; |
| 1199 | }; |
| 1200 | |
| 1201 | // This is a MIPS specific section to hold a space within the data segment |
| 1202 | // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry. |
| 1203 | // See "Dynamic section" in Chapter 5 in the following document: |
| 1204 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 1205 | class MipsRldMapSection final : public SyntheticSection { |
| 1206 | public: |
| 1207 | MipsRldMapSection(Ctx &); |
| 1208 | size_t getSize() const override { return ctx.arg.wordsize; } |
| 1209 | void writeTo(uint8_t *buf) override {} |
| 1210 | }; |
| 1211 | |
| 1212 | // Representation of the combined .ARM.Exidx input sections. We process these |
| 1213 | // as a SyntheticSection like .eh_frame as we need to merge duplicate entries |
| 1214 | // and add terminating sentinel entries. |
| 1215 | // |
| 1216 | // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form |
| 1217 | // a table that the unwinder can derive (Addresses are encoded as offsets from |
| 1218 | // table): |
| 1219 | // | Address of function | Unwind instructions for function | |
| 1220 | // where the unwind instructions are either a small number of unwind or the |
| 1221 | // special EXIDX_CANTUNWIND entry representing no unwinding information. |
| 1222 | // When an exception is thrown from an address A, the unwinder searches the |
| 1223 | // table for the closest table entry with Address of function <= A. This means |
| 1224 | // that for two consecutive table entries: |
| 1225 | // | A1 | U1 | |
| 1226 | // | A2 | U2 | |
| 1227 | // The range of addresses described by U1 is [A1, A2) |
| 1228 | // |
| 1229 | // There are two cases where we need a linker generated table entry to fixup |
| 1230 | // the address ranges in the table |
| 1231 | // Case 1: |
| 1232 | // - A sentinel entry added with an address higher than all |
| 1233 | // executable sections. This was needed to work around libunwind bug pr31091. |
| 1234 | // - After address assignment we need to find the highest addressed executable |
| 1235 | // section and use the limit of that section so that the unwinder never |
| 1236 | // matches it. |
| 1237 | // Case 2: |
| 1238 | // - InputSections without a .ARM.exidx section (usually from Assembly) |
| 1239 | // need a table entry so that they terminate the range of the previously |
| 1240 | // function. This is pr40277. |
| 1241 | // |
| 1242 | // Instead of storing pointers to the .ARM.exidx InputSections from |
| 1243 | // InputObjects, we store pointers to the executable sections that need |
| 1244 | // .ARM.exidx sections. We can then use the dependentSections of these to |
| 1245 | // either find the .ARM.exidx section or know that we need to generate one. |
| 1246 | class ARMExidxSyntheticSection : public SyntheticSection { |
| 1247 | public: |
| 1248 | ARMExidxSyntheticSection(Ctx &); |
| 1249 | |
| 1250 | // Add an input section to the ARMExidxSyntheticSection. Returns whether the |
| 1251 | // section needs to be removed from the main input section list. |
| 1252 | bool addSection(InputSection *isec); |
| 1253 | |
| 1254 | size_t getSize() const override { return size; } |
| 1255 | void writeTo(uint8_t *buf) override; |
| 1256 | bool isNeeded() const override; |
| 1257 | // Sort and remove duplicate entries. |
| 1258 | void finalizeContents() override; |
| 1259 | InputSection *getLinkOrderDep() const; |
| 1260 | |
| 1261 | static bool classof(const SectionBase *sec) { |
| 1262 | return sec->kind() == InputSectionBase::Synthetic && |
| 1263 | sec->type == llvm::ELF::SHT_ARM_EXIDX; |
| 1264 | } |
| 1265 | |
| 1266 | // Links to the ARMExidxSections so we can transfer the relocations once the |
| 1267 | // layout is known. |
| 1268 | SmallVector<InputSection *, 0> exidxSections; |
| 1269 | |
| 1270 | private: |
| 1271 | size_t size = 0; |
| 1272 | |
| 1273 | // Instead of storing pointers to the .ARM.exidx InputSections from |
| 1274 | // InputObjects, we store pointers to the executable sections that need |
| 1275 | // .ARM.exidx sections. We can then use the dependentSections of these to |
| 1276 | // either find the .ARM.exidx section or know that we need to generate one. |
| 1277 | SmallVector<InputSection *, 0> executableSections; |
| 1278 | |
| 1279 | // Value of executableSecitons before finalizeContents(), so that it can be |
| 1280 | // run repeateadly during fixed point iteration. |
| 1281 | SmallVector<InputSection *, 0> originalExecutableSections; |
| 1282 | |
| 1283 | // The executable InputSection with the highest address to use for the |
| 1284 | // sentinel. We store separately from ExecutableSections as merging of |
| 1285 | // duplicate entries may mean this InputSection is removed from |
| 1286 | // ExecutableSections. |
| 1287 | InputSection *sentinel = nullptr; |
| 1288 | }; |
| 1289 | |
| 1290 | // A container for one or more linker generated thunks. Instances of these |
| 1291 | // thunks including ARM interworking and Mips LA25 PI to non-PI thunks. |
| 1292 | class ThunkSection final : public SyntheticSection { |
| 1293 | public: |
| 1294 | // ThunkSection in OS, with desired outSecOff of Off |
| 1295 | ThunkSection(Ctx &, OutputSection *os, uint64_t off); |
| 1296 | |
| 1297 | // Add a newly created Thunk to this container: |
| 1298 | // Thunk is given offset from start of this InputSection |
| 1299 | // Thunk defines a symbol in this InputSection that can be used as target |
| 1300 | // of a relocation |
| 1301 | void addThunk(Thunk *t); |
| 1302 | size_t getSize() const override; |
| 1303 | void writeTo(uint8_t *buf) override; |
| 1304 | InputSection *getTargetInputSection() const; |
| 1305 | bool assignOffsets(); |
| 1306 | |
| 1307 | // When true, round up reported size of section to 4 KiB. See comment |
| 1308 | // in addThunkSection() for more details. |
| 1309 | bool roundUpSizeForErrata = false; |
| 1310 | |
| 1311 | private: |
| 1312 | SmallVector<Thunk *, 0> thunks; |
| 1313 | size_t size = 0; |
| 1314 | }; |
| 1315 | |
| 1316 | // Cortex-M Security Extensions. Prefix for functions that should be exported |
| 1317 | // for the non-secure world. |
| 1318 | const char ACLESESYM_PREFIX[] = "__acle_se_" ; |
| 1319 | const int ACLESESYM_SIZE = 8; |
| 1320 | |
| 1321 | class ArmCmseSGVeneer { |
| 1322 | public: |
| 1323 | ArmCmseSGVeneer(Symbol *sym, Symbol *acleSeSym, |
| 1324 | std::optional<uint64_t> addr = std::nullopt) |
| 1325 | : sym(sym), acleSeSym(acleSeSym), entAddr{addr} {} |
| 1326 | static const size_t size{ACLESESYM_SIZE}; |
| 1327 | std::optional<uint64_t> getAddr() const { return entAddr; }; |
| 1328 | |
| 1329 | Symbol *sym; |
| 1330 | Symbol *acleSeSym; |
| 1331 | uint64_t offset = 0; |
| 1332 | |
| 1333 | private: |
| 1334 | const std::optional<uint64_t> entAddr; |
| 1335 | }; |
| 1336 | |
| 1337 | class ArmCmseSGSection final : public SyntheticSection { |
| 1338 | public: |
| 1339 | ArmCmseSGSection(Ctx &ctx); |
| 1340 | bool isNeeded() const override { return !entries.empty(); } |
| 1341 | size_t getSize() const override; |
| 1342 | void writeTo(uint8_t *buf) override; |
| 1343 | void addSGVeneer(Symbol *sym, Symbol *ext_sym); |
| 1344 | void addMappingSymbol(); |
| 1345 | void finalizeContents() override; |
| 1346 | void exportEntries(SymbolTableBaseSection *symTab); |
| 1347 | uint64_t impLibMaxAddr = 0; |
| 1348 | |
| 1349 | private: |
| 1350 | SmallVector<std::pair<Symbol *, Symbol *>, 0> entries; |
| 1351 | SmallVector<std::unique_ptr<ArmCmseSGVeneer>, 0> sgVeneers; |
| 1352 | uint64_t newEntries = 0; |
| 1353 | }; |
| 1354 | |
| 1355 | // Used to compute outSecOff of .got2 in each object file. This is needed to |
| 1356 | // synthesize PLT entries for PPC32 Secure PLT ABI. |
| 1357 | class PPC32Got2Section final : public SyntheticSection { |
| 1358 | public: |
| 1359 | PPC32Got2Section(Ctx &); |
| 1360 | size_t getSize() const override { return 0; } |
| 1361 | bool isNeeded() const override; |
| 1362 | void finalizeContents() override; |
| 1363 | void writeTo(uint8_t *buf) override {} |
| 1364 | }; |
| 1365 | |
| 1366 | // This section is used to store the addresses of functions that are called |
| 1367 | // in range-extending thunks on PowerPC64. When producing position dependent |
| 1368 | // code the addresses are link-time constants and the table is written out to |
| 1369 | // the binary. When producing position-dependent code the table is allocated and |
| 1370 | // filled in by the dynamic linker. |
| 1371 | class PPC64LongBranchTargetSection final : public SyntheticSection { |
| 1372 | public: |
| 1373 | PPC64LongBranchTargetSection(Ctx &); |
| 1374 | uint64_t getEntryVA(const Symbol *sym, int64_t addend); |
| 1375 | std::optional<uint32_t> addEntry(const Symbol *sym, int64_t addend); |
| 1376 | size_t getSize() const override; |
| 1377 | void writeTo(uint8_t *buf) override; |
| 1378 | bool isNeeded() const override; |
| 1379 | void finalizeContents() override { finalized = true; } |
| 1380 | |
| 1381 | private: |
| 1382 | SmallVector<std::pair<const Symbol *, int64_t>, 0> entries; |
| 1383 | llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index; |
| 1384 | bool finalized = false; |
| 1385 | }; |
| 1386 | |
| 1387 | template <typename ELFT> |
| 1388 | class final : public SyntheticSection { |
| 1389 | public: |
| 1390 | (Ctx &); |
| 1391 | size_t () const override; |
| 1392 | void (uint8_t *buf) override; |
| 1393 | }; |
| 1394 | |
| 1395 | template <typename ELFT> |
| 1396 | class final : public SyntheticSection { |
| 1397 | public: |
| 1398 | (Ctx &); |
| 1399 | size_t () const override; |
| 1400 | void (uint8_t *buf) override; |
| 1401 | }; |
| 1402 | |
| 1403 | class PartitionIndexSection final : public SyntheticSection { |
| 1404 | public: |
| 1405 | PartitionIndexSection(Ctx &); |
| 1406 | size_t getSize() const override; |
| 1407 | void finalizeContents() override; |
| 1408 | void writeTo(uint8_t *buf) override; |
| 1409 | }; |
| 1410 | |
| 1411 | // See the following link for the Android-specific loader code that operates on |
| 1412 | // this section: |
| 1413 | // https://cs.android.com/android/platform/superproject/+/master:bionic/libc/bionic/libc_init_static.cpp;drc=9425b16978f9c5aa8f2c50c873db470819480d1d;l=192 |
| 1414 | class MemtagAndroidNote final : public SyntheticSection { |
| 1415 | public: |
| 1416 | MemtagAndroidNote(Ctx &ctx) |
| 1417 | : SyntheticSection(ctx, ".note.android.memtag" , llvm::ELF::SHT_NOTE, |
| 1418 | llvm::ELF::SHF_ALLOC, /*addralign=*/4) {} |
| 1419 | void writeTo(uint8_t *buf) override; |
| 1420 | size_t getSize() const override; |
| 1421 | }; |
| 1422 | |
| 1423 | class PackageMetadataNote final : public SyntheticSection { |
| 1424 | public: |
| 1425 | PackageMetadataNote(Ctx &ctx) |
| 1426 | : SyntheticSection(ctx, ".note.package" , llvm::ELF::SHT_NOTE, |
| 1427 | llvm::ELF::SHF_ALLOC, /*addralign=*/4) {} |
| 1428 | void writeTo(uint8_t *buf) override; |
| 1429 | size_t getSize() const override; |
| 1430 | }; |
| 1431 | |
| 1432 | class MemtagGlobalDescriptors final : public SyntheticSection { |
| 1433 | public: |
| 1434 | MemtagGlobalDescriptors(Ctx &ctx) |
| 1435 | : SyntheticSection(ctx, ".memtag.globals.dynamic" , |
| 1436 | llvm::ELF::SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC, |
| 1437 | llvm::ELF::SHF_ALLOC, /*addralign=*/4) {} |
| 1438 | void writeTo(uint8_t *buf) override; |
| 1439 | // The size of the section is non-computable until all addresses are |
| 1440 | // synthetized, because the section's contents contain a sorted |
| 1441 | // varint-compressed list of pointers to global variables. We only know the |
| 1442 | // final size after `finalizeAddressDependentContent()`. |
| 1443 | size_t getSize() const override; |
| 1444 | bool updateAllocSize(Ctx &) override; |
| 1445 | |
| 1446 | void addSymbol(const Symbol &sym) { |
| 1447 | symbols.push_back(Elt: &sym); |
| 1448 | } |
| 1449 | |
| 1450 | bool isNeeded() const override { return !symbols.empty(); } |
| 1451 | |
| 1452 | private: |
| 1453 | SmallVector<const Symbol *, 0> symbols; |
| 1454 | }; |
| 1455 | |
| 1456 | template <class ELFT> void createSyntheticSections(Ctx &); |
| 1457 | InputSection *createInterpSection(Ctx &); |
| 1458 | MergeInputSection *(Ctx &); |
| 1459 | template <class ELFT> void splitSections(Ctx &); |
| 1460 | void combineEhSections(Ctx &); |
| 1461 | |
| 1462 | bool hasMemtag(Ctx &); |
| 1463 | bool canHaveMemtagGlobals(Ctx &); |
| 1464 | |
| 1465 | template <typename ELFT> void writeEhdr(Ctx &, uint8_t *buf, Partition &part); |
| 1466 | template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part); |
| 1467 | |
| 1468 | Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type, |
| 1469 | uint64_t value, uint64_t size, |
| 1470 | InputSectionBase §ion); |
| 1471 | |
| 1472 | void addVerneed(Ctx &, Symbol &ss); |
| 1473 | |
| 1474 | // This describes a program header entry. |
| 1475 | // Each contains type, access flags and range of output sections that will be |
| 1476 | // placed in it. |
| 1477 | struct PhdrEntry { |
| 1478 | PhdrEntry(Ctx &ctx, unsigned type, unsigned flags) |
| 1479 | : p_align(type == llvm::ELF::PT_LOAD ? ctx.arg.maxPageSize : 0), |
| 1480 | p_type(type), p_flags(flags) {} |
| 1481 | void add(OutputSection *sec); |
| 1482 | |
| 1483 | uint64_t p_paddr = 0; |
| 1484 | uint64_t p_vaddr = 0; |
| 1485 | uint64_t p_memsz = 0; |
| 1486 | uint64_t p_filesz = 0; |
| 1487 | uint64_t p_offset = 0; |
| 1488 | uint32_t p_align = 0; |
| 1489 | uint32_t p_type = 0; |
| 1490 | uint32_t p_flags = 0; |
| 1491 | |
| 1492 | OutputSection *firstSec = nullptr; |
| 1493 | OutputSection *lastSec = nullptr; |
| 1494 | bool hasLMA = false; |
| 1495 | |
| 1496 | uint64_t lmaOffset = 0; |
| 1497 | }; |
| 1498 | |
| 1499 | // Linker generated per-partition sections. |
| 1500 | struct Partition { |
| 1501 | Ctx &ctx; |
| 1502 | StringRef name; |
| 1503 | uint64_t nameStrTab; |
| 1504 | |
| 1505 | std::unique_ptr<SyntheticSection> ; |
| 1506 | std::unique_ptr<SyntheticSection> ; |
| 1507 | SmallVector<std::unique_ptr<PhdrEntry>, 0> phdrs; |
| 1508 | |
| 1509 | std::unique_ptr<ARMExidxSyntheticSection> armExidx; |
| 1510 | std::unique_ptr<BuildIdSection> buildId; |
| 1511 | std::unique_ptr<SyntheticSection> dynamic; |
| 1512 | std::unique_ptr<StringTableSection> dynStrTab; |
| 1513 | std::unique_ptr<SymbolTableBaseSection> dynSymTab; |
| 1514 | std::unique_ptr<EhFrameHeader> ehFrameHdr; |
| 1515 | std::unique_ptr<EhFrameSection> ehFrame; |
| 1516 | std::unique_ptr<GnuHashTableSection> gnuHashTab; |
| 1517 | std::unique_ptr<HashTableSection> hashTab; |
| 1518 | std::unique_ptr<MemtagAndroidNote> memtagAndroidNote; |
| 1519 | std::unique_ptr<MemtagGlobalDescriptors> memtagGlobalDescriptors; |
| 1520 | std::unique_ptr<PackageMetadataNote> packageMetadataNote; |
| 1521 | std::unique_ptr<RelocationBaseSection> relaDyn; |
| 1522 | std::unique_ptr<RelrBaseSection> relrDyn; |
| 1523 | std::unique_ptr<RelrBaseSection> relrAuthDyn; |
| 1524 | std::unique_ptr<VersionDefinitionSection> verDef; |
| 1525 | std::unique_ptr<SyntheticSection> verNeed; |
| 1526 | std::unique_ptr<VersionTableSection> verSym; |
| 1527 | |
| 1528 | Partition(Ctx &ctx) : ctx(ctx) {} |
| 1529 | unsigned getNumber(Ctx &ctx) const { return this - &ctx.partitions[0] + 1; } |
| 1530 | }; |
| 1531 | |
| 1532 | inline Partition &SectionBase::getPartition(Ctx &ctx) const { |
| 1533 | assert(isLive()); |
| 1534 | return ctx.partitions[partition - 1]; |
| 1535 | } |
| 1536 | |
| 1537 | } // namespace lld::elf |
| 1538 | |
| 1539 | #endif |
| 1540 | |