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