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; |
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(); |
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 | |
123 | uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; } |
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: |
139 | GnuStackSection() |
140 | : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack" ) {} |
141 | void writeTo(uint8_t *buf) override {} |
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 = 16; |
156 | |
157 | public: |
158 | const size_t hashSize; |
159 | BuildIdSection(); |
160 | void writeTo(uint8_t *buf) override; |
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); |
175 | void writeTo(uint8_t *) override {} |
176 | bool isNeeded() const override { return size != 0; } |
177 | size_t getSize() const override { return size; } |
178 | |
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; |
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 = 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; |
322 | 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; |
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; |
397 | size_t getSize() const override { return size; } |
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. |
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) |
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. |
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; |
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; |
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()). |
504 | template <bool shard = false> void addReloc(const DynamicReloc &reloc) { |
505 | relocs.push_back(Elt: 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> |
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> |
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(r: {.expr: expr, .type: addendRelType, .offset: offsetInSec, .addend: addend, .sym: &sym}); |
537 | addReloc<shard>({dynType, &sec, offsetInSec, kind, sym, addend, expr}); |
538 | } |
539 | bool isNeeded() const override { |
540 | return !relocs.empty() || |
541 | llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); }); |
542 | } |
543 | size_t getSize() const override { return relocs.size() * this->entsize; } |
544 | size_t getRelativeRelocCount() const { return numRelativeRelocs; } |
545 | void mergeRels(); |
546 | void partitionRels(); |
547 | void finalizeContents() override; |
548 | static bool classof(const SectionBase *d) { |
549 | return SyntheticSection::classof(sec: d) && |
550 | (d->type == llvm::ELF::SHT_RELA || d->type == llvm::ELF::SHT_REL || |
551 | d->type == llvm::ELF::SHT_RELR); |
552 | } |
553 | int32_t dynamicTag, sizeDynamicTag; |
554 | SmallVector<DynamicReloc, 0> relocs; |
555 | |
556 | protected: |
557 | void computeRels(); |
558 | // Used when parallel relocation scanning adds relocations. The elements |
559 | // will be moved into relocs by mergeRel(). |
560 | SmallVector<SmallVector<DynamicReloc, 0>, 0> relocsVec; |
561 | size_t numRelativeRelocs = 0; // used by -z combreloc |
562 | bool combreloc; |
563 | }; |
564 | |
565 | template <> |
566 | inline void RelocationBaseSection::addReloc<true>(const DynamicReloc &reloc) { |
567 | relocsVec[llvm::parallel::getThreadIndex()].push_back(Elt: reloc); |
568 | } |
569 | |
570 | template <class ELFT> |
571 | class RelocationSection final : public RelocationBaseSection { |
572 | using Elf_Rel = typename ELFT::Rel; |
573 | using Elf_Rela = typename ELFT::Rela; |
574 | |
575 | public: |
576 | RelocationSection(StringRef name, bool combreloc, unsigned concurrency); |
577 | void writeTo(uint8_t *buf) override; |
578 | }; |
579 | |
580 | template <class ELFT> |
581 | class AndroidPackedRelocationSection final : public RelocationBaseSection { |
582 | using Elf_Rel = typename ELFT::Rel; |
583 | using Elf_Rela = typename ELFT::Rela; |
584 | |
585 | public: |
586 | AndroidPackedRelocationSection(StringRef name, unsigned concurrency); |
587 | |
588 | bool updateAllocSize() override; |
589 | size_t getSize() const override { return relocData.size(); } |
590 | void writeTo(uint8_t *buf) override { |
591 | memcpy(dest: buf, src: relocData.data(), n: relocData.size()); |
592 | } |
593 | |
594 | private: |
595 | SmallVector<char, 0> relocData; |
596 | }; |
597 | |
598 | struct RelativeReloc { |
599 | uint64_t getOffset() const { return inputSec->getVA(offset: offsetInSec); } |
600 | |
601 | const InputSectionBase *inputSec; |
602 | uint64_t offsetInSec; |
603 | }; |
604 | |
605 | class RelrBaseSection : public SyntheticSection { |
606 | public: |
607 | RelrBaseSection(unsigned concurrency); |
608 | void mergeRels(); |
609 | bool isNeeded() const override { |
610 | return !relocs.empty() || |
611 | llvm::any_of(Range: relocsVec, P: [](auto &v) { return !v.empty(); }); |
612 | } |
613 | SmallVector<RelativeReloc, 0> relocs; |
614 | SmallVector<SmallVector<RelativeReloc, 0>, 0> relocsVec; |
615 | }; |
616 | |
617 | // RelrSection is used to encode offsets for relative relocations. |
618 | // Proposal for adding SHT_RELR sections to generic-abi is here: |
619 | // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg |
620 | // For more details, see the comment in RelrSection::updateAllocSize(). |
621 | template <class ELFT> class RelrSection final : public RelrBaseSection { |
622 | using Elf_Relr = typename ELFT::Relr; |
623 | |
624 | public: |
625 | RelrSection(unsigned concurrency); |
626 | |
627 | bool updateAllocSize() override; |
628 | size_t getSize() const override { return relrRelocs.size() * this->entsize; } |
629 | void writeTo(uint8_t *buf) override { |
630 | memcpy(buf, relrRelocs.data(), getSize()); |
631 | } |
632 | |
633 | private: |
634 | SmallVector<Elf_Relr, 0> relrRelocs; |
635 | }; |
636 | |
637 | struct SymbolTableEntry { |
638 | Symbol *sym; |
639 | size_t strTabOffset; |
640 | }; |
641 | |
642 | class SymbolTableBaseSection : public SyntheticSection { |
643 | public: |
644 | SymbolTableBaseSection(StringTableSection &strTabSec); |
645 | void finalizeContents() override; |
646 | size_t getSize() const override { return getNumSymbols() * entsize; } |
647 | void addSymbol(Symbol *sym); |
648 | unsigned getNumSymbols() const { return symbols.size() + 1; } |
649 | size_t getSymbolIndex(const Symbol &sym); |
650 | ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; } |
651 | |
652 | protected: |
653 | void sortSymTabSymbols(); |
654 | |
655 | // A vector of symbols and their string table offsets. |
656 | SmallVector<SymbolTableEntry, 0> symbols; |
657 | |
658 | StringTableSection &strTabSec; |
659 | |
660 | llvm::once_flag onceFlag; |
661 | llvm::DenseMap<Symbol *, size_t> symbolIndexMap; |
662 | llvm::DenseMap<OutputSection *, size_t> sectionIndexMap; |
663 | }; |
664 | |
665 | template <class ELFT> |
666 | class SymbolTableSection final : public SymbolTableBaseSection { |
667 | using Elf_Sym = typename ELFT::Sym; |
668 | |
669 | public: |
670 | SymbolTableSection(StringTableSection &strTabSec); |
671 | void writeTo(uint8_t *buf) override; |
672 | }; |
673 | |
674 | class SymtabShndxSection final : public SyntheticSection { |
675 | public: |
676 | SymtabShndxSection(); |
677 | |
678 | void writeTo(uint8_t *buf) override; |
679 | size_t getSize() const override; |
680 | bool isNeeded() const override; |
681 | void finalizeContents() override; |
682 | }; |
683 | |
684 | // Outputs GNU Hash section. For detailed explanation see: |
685 | // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections |
686 | class GnuHashTableSection final : public SyntheticSection { |
687 | public: |
688 | GnuHashTableSection(); |
689 | void finalizeContents() override; |
690 | void writeTo(uint8_t *buf) override; |
691 | size_t getSize() const override { return size; } |
692 | |
693 | // Adds symbols to the hash table. |
694 | // Sorts the input to satisfy GNU hash section requirements. |
695 | void addSymbols(llvm::SmallVectorImpl<SymbolTableEntry> &symbols); |
696 | |
697 | private: |
698 | // See the comment in writeBloomFilter. |
699 | enum { Shift2 = 26 }; |
700 | |
701 | struct Entry { |
702 | Symbol *sym; |
703 | size_t strTabOffset; |
704 | uint32_t hash; |
705 | uint32_t bucketIdx; |
706 | }; |
707 | |
708 | SmallVector<Entry, 0> symbols; |
709 | size_t maskWords; |
710 | size_t nBuckets = 0; |
711 | size_t size = 0; |
712 | }; |
713 | |
714 | class HashTableSection final : public SyntheticSection { |
715 | public: |
716 | HashTableSection(); |
717 | void finalizeContents() override; |
718 | void writeTo(uint8_t *buf) override; |
719 | size_t getSize() const override { return size; } |
720 | |
721 | private: |
722 | size_t size = 0; |
723 | }; |
724 | |
725 | // Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT |
726 | // entry is associated with a JUMP_SLOT relocation, which may be resolved lazily |
727 | // at runtime. |
728 | // |
729 | // On PowerPC, this section contains lazy symbol resolvers. A branch instruction |
730 | // jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a |
731 | // lazy symbol resolver. |
732 | // |
733 | // On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs. |
734 | // A call instruction jumps to a .plt.sec entry, which will then jump to the |
735 | // target (BIND_NOW) or a .plt entry. |
736 | class PltSection : public SyntheticSection { |
737 | public: |
738 | PltSection(); |
739 | void writeTo(uint8_t *buf) override; |
740 | size_t getSize() const override; |
741 | bool isNeeded() const override; |
742 | void addSymbols(); |
743 | void addEntry(Symbol &sym); |
744 | size_t getNumEntries() const { return entries.size(); } |
745 | |
746 | size_t ; |
747 | |
748 | SmallVector<const Symbol *, 0> entries; |
749 | }; |
750 | |
751 | // Used for non-preemptible ifuncs. It does not have a header. Each entry is |
752 | // associated with an IRELATIVE relocation, which will be resolved eagerly at |
753 | // runtime. PltSection can only contain entries associated with JUMP_SLOT |
754 | // relocations, so IPLT entries are in a separate section. |
755 | class IpltSection final : public SyntheticSection { |
756 | SmallVector<const Symbol *, 0> entries; |
757 | |
758 | public: |
759 | IpltSection(); |
760 | void writeTo(uint8_t *buf) override; |
761 | size_t getSize() const override; |
762 | bool isNeeded() const override { return !entries.empty(); } |
763 | void addSymbols(); |
764 | void addEntry(Symbol &sym); |
765 | }; |
766 | |
767 | class PPC32GlinkSection : public PltSection { |
768 | public: |
769 | PPC32GlinkSection(); |
770 | void writeTo(uint8_t *buf) override; |
771 | size_t getSize() const override; |
772 | |
773 | SmallVector<const Symbol *, 0> canonical_plts; |
774 | static constexpr size_t = 64; |
775 | }; |
776 | |
777 | // This is x86-only. |
778 | class IBTPltSection : public SyntheticSection { |
779 | public: |
780 | IBTPltSection(); |
781 | void writeTo(uint8_t *Buf) override; |
782 | bool isNeeded() const override; |
783 | size_t getSize() const override; |
784 | }; |
785 | |
786 | // Used to align the end of the PT_GNU_RELRO segment and the associated PT_LOAD |
787 | // segment to a common-page-size boundary. This padding section ensures that all |
788 | // pages in the PT_LOAD segment is covered by at least one section. |
789 | class RelroPaddingSection final : public SyntheticSection { |
790 | public: |
791 | RelroPaddingSection(); |
792 | size_t getSize() const override { return 0; } |
793 | void writeTo(uint8_t *buf) override {} |
794 | }; |
795 | |
796 | // Used by the merged DWARF32 .debug_names (a per-module index). If we |
797 | // move to DWARF64, most of this data will need to be re-sized. |
798 | class DebugNamesBaseSection : public SyntheticSection { |
799 | public: |
800 | struct Abbrev : llvm::FoldingSetNode { |
801 | uint32_t code; |
802 | uint32_t tag; |
803 | SmallVector<llvm::DWARFDebugNames::AttributeEncoding, 2> attributes; |
804 | |
805 | void Profile(llvm::FoldingSetNodeID &id) const; |
806 | }; |
807 | |
808 | struct AttrValue { |
809 | uint32_t attrValue; |
810 | uint8_t attrSize; |
811 | }; |
812 | |
813 | struct IndexEntry { |
814 | uint32_t abbrevCode; |
815 | uint32_t poolOffset; |
816 | union { |
817 | uint64_t parentOffset = 0; |
818 | IndexEntry *parentEntry; |
819 | }; |
820 | SmallVector<AttrValue, 3> attrValues; |
821 | }; |
822 | |
823 | struct NameEntry { |
824 | const char *name; |
825 | uint32_t hashValue; |
826 | uint32_t stringOffset; |
827 | uint32_t entryOffset; |
828 | // Used to relocate `stringOffset` in the merged section. |
829 | uint32_t chunkIdx; |
830 | SmallVector<IndexEntry *, 0> indexEntries; |
831 | |
832 | llvm::iterator_range< |
833 | llvm::pointee_iterator<typename SmallVector<IndexEntry *, 0>::iterator>> |
834 | entries() { |
835 | return llvm::make_pointee_range(Range&: indexEntries); |
836 | } |
837 | }; |
838 | |
839 | // The contents of one input .debug_names section. An InputChunk |
840 | // typically contains one NameData, but might contain more, especially |
841 | // in LTO builds. |
842 | struct NameData { |
843 | llvm::DWARFDebugNames::Header hdr; |
844 | llvm::DenseMap<uint32_t, uint32_t> abbrevCodeMap; |
845 | SmallVector<NameEntry, 0> nameEntries; |
846 | }; |
847 | |
848 | // InputChunk and OutputChunk hold per-file contributions to the merged index. |
849 | // InputChunk instances will be discarded after `init` completes. |
850 | struct InputChunk { |
851 | uint32_t baseCuIdx; |
852 | LLDDWARFSection section; |
853 | SmallVector<NameData, 0> nameData; |
854 | std::optional<llvm::DWARFDebugNames> llvmDebugNames; |
855 | }; |
856 | |
857 | struct OutputChunk { |
858 | // Pointer to the .debug_info section that contains compile units, used to |
859 | // compute the relocated CU offsets. |
860 | InputSection *infoSec; |
861 | // This initially holds section offsets. After relocation, the section |
862 | // offsets are changed to CU offsets relative the the output section. |
863 | SmallVector<uint32_t, 0> compUnits; |
864 | }; |
865 | |
866 | DebugNamesBaseSection(); |
867 | size_t getSize() const override { return size; } |
868 | bool isNeeded() const override { return numChunks > 0; } |
869 | |
870 | protected: |
871 | void init(llvm::function_ref<void(InputFile *, InputChunk &, OutputChunk &)>); |
872 | static void |
873 | (InputChunk &inputChunk, OutputChunk &chunk, |
874 | llvm::DWARFDataExtractor &, |
875 | llvm::DataExtractor &, |
876 | llvm::function_ref<SmallVector<uint32_t, 0>( |
877 | uint32_t numCUs, const llvm::DWARFDebugNames::Header &hdr, |
878 | const llvm::DWARFDebugNames::DWARFDebugNamesOffsets &)> |
879 | readOffsets); |
880 | void computeHdrAndAbbrevTable(MutableArrayRef<InputChunk> inputChunks); |
881 | std::pair<uint32_t, uint32_t> |
882 | computeEntryPool(MutableArrayRef<InputChunk> inputChunks); |
883 | |
884 | // Input .debug_names sections for relocating string offsets in the name table |
885 | // in `finalizeContents`. |
886 | SmallVector<InputSection *, 0> inputSections; |
887 | |
888 | llvm::DWARFDebugNames::Header hdr; |
889 | size_t numChunks; |
890 | std::unique_ptr<OutputChunk[]> chunks; |
891 | llvm::SpecificBumpPtrAllocator<Abbrev> abbrevAlloc; |
892 | SmallVector<Abbrev *, 0> abbrevTable; |
893 | SmallVector<char, 0> abbrevTableBuf; |
894 | |
895 | ArrayRef<OutputChunk> getChunks() const { |
896 | return ArrayRef(chunks.get(), numChunks); |
897 | } |
898 | |
899 | // Sharded name entries that will be used to compute bucket_count and the |
900 | // count name table. |
901 | static constexpr size_t numShards = 32; |
902 | SmallVector<NameEntry, 0> nameVecs[numShards]; |
903 | }; |
904 | |
905 | // Complement DebugNamesBaseSection for ELFT-aware code: reading offsets, |
906 | // relocating string offsets, and writeTo. |
907 | template <class ELFT> |
908 | class DebugNamesSection final : public DebugNamesBaseSection { |
909 | public: |
910 | DebugNamesSection(); |
911 | void finalizeContents() override; |
912 | void writeTo(uint8_t *buf) override; |
913 | |
914 | template <class RelTy> |
915 | void getNameRelocs(InputSection *sec, ArrayRef<RelTy> rels, |
916 | llvm::DenseMap<uint32_t, uint32_t> &relocs); |
917 | |
918 | private: |
919 | static void (InputChunk &inputChunk, OutputChunk &chunk, |
920 | llvm::DWARFDataExtractor &, |
921 | llvm::DataExtractor &); |
922 | }; |
923 | |
924 | class GdbIndexSection final : public SyntheticSection { |
925 | public: |
926 | struct AddressEntry { |
927 | InputSection *section; |
928 | uint64_t lowAddress; |
929 | uint64_t highAddress; |
930 | uint32_t cuIndex; |
931 | }; |
932 | |
933 | struct CuEntry { |
934 | uint64_t cuOffset; |
935 | uint64_t cuLength; |
936 | }; |
937 | |
938 | struct NameAttrEntry { |
939 | llvm::CachedHashStringRef name; |
940 | uint32_t cuIndexAndAttrs; |
941 | }; |
942 | |
943 | struct GdbChunk { |
944 | InputSection *sec; |
945 | SmallVector<AddressEntry, 0> addressAreas; |
946 | SmallVector<CuEntry, 0> compilationUnits; |
947 | }; |
948 | |
949 | struct GdbSymbol { |
950 | llvm::CachedHashStringRef name; |
951 | SmallVector<uint32_t, 0> cuVector; |
952 | uint32_t nameOff; |
953 | uint32_t cuVectorOff; |
954 | }; |
955 | |
956 | GdbIndexSection(); |
957 | template <typename ELFT> static std::unique_ptr<GdbIndexSection> create(); |
958 | void writeTo(uint8_t *buf) override; |
959 | size_t getSize() const override { return size; } |
960 | bool isNeeded() const override; |
961 | |
962 | private: |
963 | struct { |
964 | llvm::support::ulittle32_t ; |
965 | llvm::support::ulittle32_t ; |
966 | llvm::support::ulittle32_t ; |
967 | llvm::support::ulittle32_t ; |
968 | llvm::support::ulittle32_t ; |
969 | llvm::support::ulittle32_t ; |
970 | }; |
971 | |
972 | size_t computeSymtabSize() const; |
973 | |
974 | // Each chunk contains information gathered from debug sections of a |
975 | // single object file. |
976 | SmallVector<GdbChunk, 0> chunks; |
977 | |
978 | // A symbol table for this .gdb_index section. |
979 | SmallVector<GdbSymbol, 0> symbols; |
980 | |
981 | size_t size; |
982 | }; |
983 | |
984 | // --eh-frame-hdr option tells linker to construct a header for all the |
985 | // .eh_frame sections. This header is placed to a section named .eh_frame_hdr |
986 | // and also to a PT_GNU_EH_FRAME segment. |
987 | // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by |
988 | // calling dl_iterate_phdr. |
989 | // This section contains a lookup table for quick binary search of FDEs. |
990 | // Detailed info about internals can be found in Ian Lance Taylor's blog: |
991 | // http://www.airs.com/blog/archives/460 (".eh_frame") |
992 | // http://www.airs.com/blog/archives/462 (".eh_frame_hdr") |
993 | class final : public SyntheticSection { |
994 | public: |
995 | (); |
996 | void (); |
997 | void (uint8_t *buf) override; |
998 | size_t () const override; |
999 | bool () const override; |
1000 | }; |
1001 | |
1002 | // For more information about .gnu.version and .gnu.version_r see: |
1003 | // https://www.akkadia.org/drepper/symbol-versioning |
1004 | |
1005 | // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall |
1006 | // contain symbol version definitions. The number of entries in this section |
1007 | // shall be contained in the DT_VERDEFNUM entry of the .dynamic section. |
1008 | // The section shall contain an array of Elf_Verdef structures, optionally |
1009 | // followed by an array of Elf_Verdaux structures. |
1010 | class VersionDefinitionSection final : public SyntheticSection { |
1011 | public: |
1012 | VersionDefinitionSection(); |
1013 | void finalizeContents() override; |
1014 | size_t getSize() const override; |
1015 | void writeTo(uint8_t *buf) override; |
1016 | |
1017 | private: |
1018 | enum { EntrySize = 28 }; |
1019 | void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff); |
1020 | StringRef getFileDefName(); |
1021 | |
1022 | unsigned fileDefNameOff; |
1023 | SmallVector<unsigned, 0> verDefNameOffs; |
1024 | }; |
1025 | |
1026 | // The .gnu.version section specifies the required version of each symbol in the |
1027 | // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol |
1028 | // table entry. An Elf_Versym is just a 16-bit integer that refers to a version |
1029 | // identifier defined in the either .gnu.version_r or .gnu.version_d section. |
1030 | // The values 0 and 1 are reserved. All other values are used for versions in |
1031 | // the own object or in any of the dependencies. |
1032 | class VersionTableSection final : public SyntheticSection { |
1033 | public: |
1034 | VersionTableSection(); |
1035 | void finalizeContents() override; |
1036 | size_t getSize() const override; |
1037 | void writeTo(uint8_t *buf) override; |
1038 | bool isNeeded() const override; |
1039 | }; |
1040 | |
1041 | // The .gnu.version_r section defines the version identifiers used by |
1042 | // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each |
1043 | // Elf_Verneed specifies the version requirements for a single DSO, and contains |
1044 | // a reference to a linked list of Elf_Vernaux data structures which define the |
1045 | // mapping from version identifiers to version names. |
1046 | template <class ELFT> |
1047 | class VersionNeedSection final : public SyntheticSection { |
1048 | using Elf_Verneed = typename ELFT::Verneed; |
1049 | using Elf_Vernaux = typename ELFT::Vernaux; |
1050 | |
1051 | struct Vernaux { |
1052 | uint64_t hash; |
1053 | uint32_t verneedIndex; |
1054 | uint64_t nameStrTab; |
1055 | }; |
1056 | |
1057 | struct Verneed { |
1058 | uint64_t nameStrTab; |
1059 | std::vector<Vernaux> vernauxs; |
1060 | }; |
1061 | |
1062 | SmallVector<Verneed, 0> verneeds; |
1063 | |
1064 | public: |
1065 | VersionNeedSection(); |
1066 | void finalizeContents() override; |
1067 | void writeTo(uint8_t *buf) override; |
1068 | size_t getSize() const override; |
1069 | bool isNeeded() const override; |
1070 | }; |
1071 | |
1072 | // MergeSyntheticSection is a class that allows us to put mergeable sections |
1073 | // with different attributes in a single output sections. To do that |
1074 | // we put them into MergeSyntheticSection synthetic input sections which are |
1075 | // attached to regular output sections. |
1076 | class MergeSyntheticSection : public SyntheticSection { |
1077 | public: |
1078 | void addSection(MergeInputSection *ms); |
1079 | SmallVector<MergeInputSection *, 0> sections; |
1080 | |
1081 | protected: |
1082 | MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags, |
1083 | uint32_t addralign) |
1084 | : SyntheticSection(flags, type, addralign, name) {} |
1085 | }; |
1086 | |
1087 | class MergeTailSection final : public MergeSyntheticSection { |
1088 | public: |
1089 | MergeTailSection(StringRef name, uint32_t type, uint64_t flags, |
1090 | uint32_t addralign); |
1091 | |
1092 | size_t getSize() const override; |
1093 | void writeTo(uint8_t *buf) override; |
1094 | void finalizeContents() override; |
1095 | |
1096 | private: |
1097 | llvm::StringTableBuilder builder; |
1098 | }; |
1099 | |
1100 | class MergeNoTailSection final : public MergeSyntheticSection { |
1101 | public: |
1102 | MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags, |
1103 | uint32_t addralign) |
1104 | : MergeSyntheticSection(name, type, flags, addralign) {} |
1105 | |
1106 | size_t getSize() const override { return size; } |
1107 | void writeTo(uint8_t *buf) override; |
1108 | void finalizeContents() override; |
1109 | |
1110 | private: |
1111 | // We use the most significant bits of a hash as a shard ID. |
1112 | // The reason why we don't want to use the least significant bits is |
1113 | // because DenseMap also uses lower bits to determine a bucket ID. |
1114 | // If we use lower bits, it significantly increases the probability of |
1115 | // hash collisions. |
1116 | size_t getShardId(uint32_t hash) { |
1117 | assert((hash >> 31) == 0); |
1118 | return hash >> (31 - llvm::countr_zero(Val: numShards)); |
1119 | } |
1120 | |
1121 | // Section size |
1122 | size_t size; |
1123 | |
1124 | // String table contents |
1125 | constexpr static size_t numShards = 32; |
1126 | SmallVector<llvm::StringTableBuilder, 0> shards; |
1127 | size_t shardOffsets[numShards]; |
1128 | }; |
1129 | |
1130 | // .MIPS.abiflags section. |
1131 | template <class ELFT> |
1132 | class MipsAbiFlagsSection final : public SyntheticSection { |
1133 | using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>; |
1134 | |
1135 | public: |
1136 | static std::unique_ptr<MipsAbiFlagsSection> create(); |
1137 | |
1138 | MipsAbiFlagsSection(Elf_Mips_ABIFlags flags); |
1139 | size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); } |
1140 | void writeTo(uint8_t *buf) override; |
1141 | |
1142 | private: |
1143 | Elf_Mips_ABIFlags flags; |
1144 | }; |
1145 | |
1146 | // .MIPS.options section. |
1147 | template <class ELFT> class MipsOptionsSection final : public SyntheticSection { |
1148 | using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>; |
1149 | using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; |
1150 | |
1151 | public: |
1152 | static std::unique_ptr<MipsOptionsSection<ELFT>> create(); |
1153 | |
1154 | MipsOptionsSection(Elf_Mips_RegInfo reginfo); |
1155 | void writeTo(uint8_t *buf) override; |
1156 | |
1157 | size_t getSize() const override { |
1158 | return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); |
1159 | } |
1160 | |
1161 | private: |
1162 | Elf_Mips_RegInfo reginfo; |
1163 | }; |
1164 | |
1165 | // MIPS .reginfo section. |
1166 | template <class ELFT> class MipsReginfoSection final : public SyntheticSection { |
1167 | using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>; |
1168 | |
1169 | public: |
1170 | static std::unique_ptr<MipsReginfoSection> create(); |
1171 | |
1172 | MipsReginfoSection(Elf_Mips_RegInfo reginfo); |
1173 | size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); } |
1174 | void writeTo(uint8_t *buf) override; |
1175 | |
1176 | private: |
1177 | Elf_Mips_RegInfo reginfo; |
1178 | }; |
1179 | |
1180 | // This is a MIPS specific section to hold a space within the data segment |
1181 | // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry. |
1182 | // See "Dynamic section" in Chapter 5 in the following document: |
1183 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
1184 | class MipsRldMapSection final : public SyntheticSection { |
1185 | public: |
1186 | MipsRldMapSection(); |
1187 | size_t getSize() const override { return config->wordsize; } |
1188 | void writeTo(uint8_t *buf) override {} |
1189 | }; |
1190 | |
1191 | // Representation of the combined .ARM.Exidx input sections. We process these |
1192 | // as a SyntheticSection like .eh_frame as we need to merge duplicate entries |
1193 | // and add terminating sentinel entries. |
1194 | // |
1195 | // The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form |
1196 | // a table that the unwinder can derive (Addresses are encoded as offsets from |
1197 | // table): |
1198 | // | Address of function | Unwind instructions for function | |
1199 | // where the unwind instructions are either a small number of unwind or the |
1200 | // special EXIDX_CANTUNWIND entry representing no unwinding information. |
1201 | // When an exception is thrown from an address A, the unwinder searches the |
1202 | // table for the closest table entry with Address of function <= A. This means |
1203 | // that for two consecutive table entries: |
1204 | // | A1 | U1 | |
1205 | // | A2 | U2 | |
1206 | // The range of addresses described by U1 is [A1, A2) |
1207 | // |
1208 | // There are two cases where we need a linker generated table entry to fixup |
1209 | // the address ranges in the table |
1210 | // Case 1: |
1211 | // - A sentinel entry added with an address higher than all |
1212 | // executable sections. This was needed to work around libunwind bug pr31091. |
1213 | // - After address assignment we need to find the highest addressed executable |
1214 | // section and use the limit of that section so that the unwinder never |
1215 | // matches it. |
1216 | // Case 2: |
1217 | // - InputSections without a .ARM.exidx section (usually from Assembly) |
1218 | // need a table entry so that they terminate the range of the previously |
1219 | // function. This is pr40277. |
1220 | // |
1221 | // Instead of storing pointers to the .ARM.exidx InputSections from |
1222 | // InputObjects, we store pointers to the executable sections that need |
1223 | // .ARM.exidx sections. We can then use the dependentSections of these to |
1224 | // either find the .ARM.exidx section or know that we need to generate one. |
1225 | class ARMExidxSyntheticSection : public SyntheticSection { |
1226 | public: |
1227 | ARMExidxSyntheticSection(); |
1228 | |
1229 | // Add an input section to the ARMExidxSyntheticSection. Returns whether the |
1230 | // section needs to be removed from the main input section list. |
1231 | bool addSection(InputSection *isec); |
1232 | |
1233 | size_t getSize() const override { return size; } |
1234 | void writeTo(uint8_t *buf) override; |
1235 | bool isNeeded() const override; |
1236 | // Sort and remove duplicate entries. |
1237 | void finalizeContents() override; |
1238 | InputSection *getLinkOrderDep() const; |
1239 | |
1240 | static bool classof(const SectionBase *sec) { |
1241 | return sec->kind() == InputSectionBase::Synthetic && |
1242 | sec->type == llvm::ELF::SHT_ARM_EXIDX; |
1243 | } |
1244 | |
1245 | // Links to the ARMExidxSections so we can transfer the relocations once the |
1246 | // layout is known. |
1247 | SmallVector<InputSection *, 0> exidxSections; |
1248 | |
1249 | private: |
1250 | size_t size = 0; |
1251 | |
1252 | // Instead of storing pointers to the .ARM.exidx InputSections from |
1253 | // InputObjects, we store pointers to the executable sections that need |
1254 | // .ARM.exidx sections. We can then use the dependentSections of these to |
1255 | // either find the .ARM.exidx section or know that we need to generate one. |
1256 | SmallVector<InputSection *, 0> executableSections; |
1257 | |
1258 | // The executable InputSection with the highest address to use for the |
1259 | // sentinel. We store separately from ExecutableSections as merging of |
1260 | // duplicate entries may mean this InputSection is removed from |
1261 | // ExecutableSections. |
1262 | InputSection *sentinel = nullptr; |
1263 | }; |
1264 | |
1265 | // A container for one or more linker generated thunks. Instances of these |
1266 | // thunks including ARM interworking and Mips LA25 PI to non-PI thunks. |
1267 | class ThunkSection final : public SyntheticSection { |
1268 | public: |
1269 | // ThunkSection in OS, with desired outSecOff of Off |
1270 | ThunkSection(OutputSection *os, uint64_t off); |
1271 | |
1272 | // Add a newly created Thunk to this container: |
1273 | // Thunk is given offset from start of this InputSection |
1274 | // Thunk defines a symbol in this InputSection that can be used as target |
1275 | // of a relocation |
1276 | void addThunk(Thunk *t); |
1277 | size_t getSize() const override; |
1278 | void writeTo(uint8_t *buf) override; |
1279 | InputSection *getTargetInputSection() const; |
1280 | bool assignOffsets(); |
1281 | |
1282 | // When true, round up reported size of section to 4 KiB. See comment |
1283 | // in addThunkSection() for more details. |
1284 | bool roundUpSizeForErrata = false; |
1285 | |
1286 | private: |
1287 | SmallVector<Thunk *, 0> thunks; |
1288 | size_t size = 0; |
1289 | }; |
1290 | |
1291 | // Cortex-M Security Extensions. Prefix for functions that should be exported |
1292 | // for the non-secure world. |
1293 | const char ACLESESYM_PREFIX[] = "__acle_se_" ; |
1294 | const int ACLESESYM_SIZE = 8; |
1295 | |
1296 | class ArmCmseSGVeneer; |
1297 | |
1298 | class ArmCmseSGSection final : public SyntheticSection { |
1299 | public: |
1300 | ArmCmseSGSection(); |
1301 | bool isNeeded() const override { return !entries.empty(); } |
1302 | size_t getSize() const override; |
1303 | void writeTo(uint8_t *buf) override; |
1304 | void addSGVeneer(Symbol *sym, Symbol *ext_sym); |
1305 | void addMappingSymbol(); |
1306 | void finalizeContents() override; |
1307 | void exportEntries(SymbolTableBaseSection *symTab); |
1308 | uint64_t impLibMaxAddr = 0; |
1309 | |
1310 | private: |
1311 | SmallVector<std::pair<Symbol *, Symbol *>, 0> entries; |
1312 | SmallVector<ArmCmseSGVeneer *, 0> sgVeneers; |
1313 | uint64_t newEntries = 0; |
1314 | }; |
1315 | |
1316 | // Used to compute outSecOff of .got2 in each object file. This is needed to |
1317 | // synthesize PLT entries for PPC32 Secure PLT ABI. |
1318 | class PPC32Got2Section final : public SyntheticSection { |
1319 | public: |
1320 | PPC32Got2Section(); |
1321 | size_t getSize() const override { return 0; } |
1322 | bool isNeeded() const override; |
1323 | void finalizeContents() override; |
1324 | void writeTo(uint8_t *buf) override {} |
1325 | }; |
1326 | |
1327 | // This section is used to store the addresses of functions that are called |
1328 | // in range-extending thunks on PowerPC64. When producing position dependent |
1329 | // code the addresses are link-time constants and the table is written out to |
1330 | // the binary. When producing position-dependent code the table is allocated and |
1331 | // filled in by the dynamic linker. |
1332 | class PPC64LongBranchTargetSection final : public SyntheticSection { |
1333 | public: |
1334 | PPC64LongBranchTargetSection(); |
1335 | uint64_t getEntryVA(const Symbol *sym, int64_t addend); |
1336 | std::optional<uint32_t> addEntry(const Symbol *sym, int64_t addend); |
1337 | size_t getSize() const override; |
1338 | void writeTo(uint8_t *buf) override; |
1339 | bool isNeeded() const override; |
1340 | void finalizeContents() override { finalized = true; } |
1341 | |
1342 | private: |
1343 | SmallVector<std::pair<const Symbol *, int64_t>, 0> entries; |
1344 | llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index; |
1345 | bool finalized = false; |
1346 | }; |
1347 | |
1348 | template <typename ELFT> |
1349 | class final : public SyntheticSection { |
1350 | public: |
1351 | (); |
1352 | size_t () const override; |
1353 | void (uint8_t *buf) override; |
1354 | }; |
1355 | |
1356 | template <typename ELFT> |
1357 | class final : public SyntheticSection { |
1358 | public: |
1359 | (); |
1360 | size_t () const override; |
1361 | void (uint8_t *buf) override; |
1362 | }; |
1363 | |
1364 | class PartitionIndexSection final : public SyntheticSection { |
1365 | public: |
1366 | PartitionIndexSection(); |
1367 | size_t getSize() const override; |
1368 | void finalizeContents() override; |
1369 | void writeTo(uint8_t *buf) override; |
1370 | }; |
1371 | |
1372 | // See the following link for the Android-specific loader code that operates on |
1373 | // this section: |
1374 | // https://cs.android.com/android/platform/superproject/+/master:bionic/libc/bionic/libc_init_static.cpp;drc=9425b16978f9c5aa8f2c50c873db470819480d1d;l=192 |
1375 | class MemtagAndroidNote final : public SyntheticSection { |
1376 | public: |
1377 | MemtagAndroidNote() |
1378 | : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, |
1379 | /*alignment=*/4, ".note.android.memtag" ) {} |
1380 | void writeTo(uint8_t *buf) override; |
1381 | size_t getSize() const override; |
1382 | }; |
1383 | |
1384 | class PackageMetadataNote final : public SyntheticSection { |
1385 | public: |
1386 | PackageMetadataNote() |
1387 | : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, |
1388 | /*alignment=*/4, ".note.package" ) {} |
1389 | void writeTo(uint8_t *buf) override; |
1390 | size_t getSize() const override; |
1391 | }; |
1392 | |
1393 | class MemtagGlobalDescriptors final : public SyntheticSection { |
1394 | public: |
1395 | MemtagGlobalDescriptors() |
1396 | : SyntheticSection(llvm::ELF::SHF_ALLOC, |
1397 | llvm::ELF::SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC, |
1398 | /*alignment=*/4, ".memtag.globals.dynamic" ) {} |
1399 | void writeTo(uint8_t *buf) override; |
1400 | // The size of the section is non-computable until all addresses are |
1401 | // synthetized, because the section's contents contain a sorted |
1402 | // varint-compressed list of pointers to global variables. We only know the |
1403 | // final size after `finalizeAddressDependentContent()`. |
1404 | size_t getSize() const override; |
1405 | bool updateAllocSize() override; |
1406 | |
1407 | void addSymbol(const Symbol &sym) { |
1408 | symbols.push_back(Elt: &sym); |
1409 | } |
1410 | |
1411 | bool isNeeded() const override { |
1412 | return !symbols.empty(); |
1413 | } |
1414 | |
1415 | private: |
1416 | SmallVector<const Symbol *, 0> symbols; |
1417 | }; |
1418 | |
1419 | template <class ELFT> void createSyntheticSections(); |
1420 | InputSection *createInterpSection(); |
1421 | MergeInputSection *(); |
1422 | template <class ELFT> void splitSections(); |
1423 | void combineEhSections(); |
1424 | |
1425 | bool hasMemtag(); |
1426 | bool canHaveMemtagGlobals(); |
1427 | |
1428 | template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part); |
1429 | template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part); |
1430 | |
1431 | Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value, |
1432 | uint64_t size, InputSectionBase §ion); |
1433 | |
1434 | void addVerneed(Symbol *ss); |
1435 | |
1436 | // Linker generated per-partition sections. |
1437 | struct Partition { |
1438 | StringRef name; |
1439 | uint64_t nameStrTab; |
1440 | |
1441 | std::unique_ptr<SyntheticSection> ; |
1442 | std::unique_ptr<SyntheticSection> ; |
1443 | SmallVector<PhdrEntry *, 0> phdrs; |
1444 | |
1445 | std::unique_ptr<ARMExidxSyntheticSection> armExidx; |
1446 | std::unique_ptr<BuildIdSection> buildId; |
1447 | std::unique_ptr<SyntheticSection> dynamic; |
1448 | std::unique_ptr<StringTableSection> dynStrTab; |
1449 | std::unique_ptr<SymbolTableBaseSection> dynSymTab; |
1450 | std::unique_ptr<EhFrameHeader> ehFrameHdr; |
1451 | std::unique_ptr<EhFrameSection> ehFrame; |
1452 | std::unique_ptr<GnuHashTableSection> gnuHashTab; |
1453 | std::unique_ptr<HashTableSection> hashTab; |
1454 | std::unique_ptr<MemtagAndroidNote> memtagAndroidNote; |
1455 | std::unique_ptr<MemtagGlobalDescriptors> memtagGlobalDescriptors; |
1456 | std::unique_ptr<PackageMetadataNote> packageMetadataNote; |
1457 | std::unique_ptr<RelocationBaseSection> relaDyn; |
1458 | std::unique_ptr<RelrBaseSection> relrDyn; |
1459 | std::unique_ptr<VersionDefinitionSection> verDef; |
1460 | std::unique_ptr<SyntheticSection> verNeed; |
1461 | std::unique_ptr<VersionTableSection> verSym; |
1462 | |
1463 | unsigned getNumber() const { return this - &partitions[0] + 1; } |
1464 | }; |
1465 | |
1466 | LLVM_LIBRARY_VISIBILITY extern Partition *mainPart; |
1467 | |
1468 | inline Partition &SectionBase::getPartition() const { |
1469 | assert(isLive()); |
1470 | return partitions[partition - 1]; |
1471 | } |
1472 | |
1473 | // Linker generated sections which can be used as inputs and are not specific to |
1474 | // a partition. |
1475 | struct InStruct { |
1476 | std::unique_ptr<InputSection> attributes; |
1477 | std::unique_ptr<SyntheticSection> riscvAttributes; |
1478 | std::unique_ptr<BssSection> bss; |
1479 | std::unique_ptr<BssSection> bssRelRo; |
1480 | std::unique_ptr<GotSection> got; |
1481 | std::unique_ptr<GotPltSection> gotPlt; |
1482 | std::unique_ptr<IgotPltSection> igotPlt; |
1483 | std::unique_ptr<RelroPaddingSection> relroPadding; |
1484 | std::unique_ptr<SyntheticSection> armCmseSGSection; |
1485 | std::unique_ptr<PPC64LongBranchTargetSection> ppc64LongBranchTarget; |
1486 | std::unique_ptr<SyntheticSection> mipsAbiFlags; |
1487 | std::unique_ptr<MipsGotSection> mipsGot; |
1488 | std::unique_ptr<SyntheticSection> mipsOptions; |
1489 | std::unique_ptr<SyntheticSection> mipsReginfo; |
1490 | std::unique_ptr<MipsRldMapSection> mipsRldMap; |
1491 | std::unique_ptr<SyntheticSection> partEnd; |
1492 | std::unique_ptr<SyntheticSection> partIndex; |
1493 | std::unique_ptr<PltSection> plt; |
1494 | std::unique_ptr<IpltSection> iplt; |
1495 | std::unique_ptr<PPC32Got2Section> ppc32Got2; |
1496 | std::unique_ptr<IBTPltSection> ibtPlt; |
1497 | std::unique_ptr<RelocationBaseSection> relaPlt; |
1498 | // Non-SHF_ALLOC sections |
1499 | std::unique_ptr<SyntheticSection> debugNames; |
1500 | std::unique_ptr<GdbIndexSection> gdbIndex; |
1501 | std::unique_ptr<StringTableSection> shStrTab; |
1502 | std::unique_ptr<StringTableSection> strTab; |
1503 | std::unique_ptr<SymbolTableBaseSection> symTab; |
1504 | std::unique_ptr<SymtabShndxSection> symTabShndx; |
1505 | |
1506 | void reset(); |
1507 | }; |
1508 | |
1509 | LLVM_LIBRARY_VISIBILITY extern InStruct in; |
1510 | |
1511 | } // namespace lld::elf |
1512 | |
1513 | #endif |
1514 | |