1//===- SyntheticSections.cpp ----------------------------------------------===//
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// This file contains linker-synthesized sections. Currently,
10// synthetic sections are created either output sections or input sections,
11// but we are rewriting code so that all synthetic sections are created as
12// input sections.
13//
14//===----------------------------------------------------------------------===//
15
16#include "SyntheticSections.h"
17#include "Config.h"
18#include "DWARF.h"
19#include "EhFrame.h"
20#include "InputFiles.h"
21#include "LinkerScript.h"
22#include "OutputSections.h"
23#include "SymbolTable.h"
24#include "Symbols.h"
25#include "Target.h"
26#include "Thunks.h"
27#include "Writer.h"
28#include "lld/Common/Version.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/Sequence.h"
31#include "llvm/ADT/SetOperations.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/BinaryFormat/Dwarf.h"
34#include "llvm/BinaryFormat/ELF.h"
35#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
36#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
37#include "llvm/Support/DJB.h"
38#include "llvm/Support/Endian.h"
39#include "llvm/Support/LEB128.h"
40#include "llvm/Support/Parallel.h"
41#include "llvm/Support/TimeProfiler.h"
42#include <cinttypes>
43#include <cstdlib>
44
45using namespace llvm;
46using namespace llvm::dwarf;
47using namespace llvm::ELF;
48using namespace llvm::object;
49using namespace llvm::support;
50using namespace lld;
51using namespace lld::elf;
52
53using llvm::support::endian::read32le;
54using llvm::support::endian::write32le;
55using llvm::support::endian::write64le;
56
57static uint64_t readUint(Ctx &ctx, uint8_t *buf) {
58 return ctx.arg.is64 ? read64(ctx, p: buf) : read32(ctx, p: buf);
59}
60
61static void writeUint(Ctx &ctx, uint8_t *buf, uint64_t val) {
62 if (ctx.arg.is64)
63 write64(ctx, p: buf, v: val);
64 else
65 write32(ctx, p: buf, v: val);
66}
67
68// Returns an LLD version string.
69static ArrayRef<uint8_t> getVersion(Ctx &ctx) {
70 // Check LLD_VERSION first for ease of testing.
71 // You can get consistent output by using the environment variable.
72 // This is only for testing.
73 StringRef s = getenv(name: "LLD_VERSION");
74 if (s.empty())
75 s = ctx.saver.save(S: Twine("Linker: ") + getLLDVersion());
76
77 // +1 to include the terminating '\0'.
78 return {(const uint8_t *)s.data(), s.size() + 1};
79}
80
81// Creates a .comment section containing LLD version info.
82// With this feature, you can identify LLD-generated binaries easily
83// by "readelf --string-dump .comment <file>".
84// The returned object is a mergeable string section.
85MergeInputSection *elf::createCommentSection(Ctx &ctx) {
86 auto *sec =
87 make<MergeInputSection>(args&: ctx, args: ".comment", args: SHT_PROGBITS,
88 args: SHF_MERGE | SHF_STRINGS, args: 1, args: getVersion(ctx));
89 sec->splitIntoPieces();
90 return sec;
91}
92
93// .MIPS.abiflags section.
94template <class ELFT>
95MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Ctx &ctx,
96 Elf_Mips_ABIFlags flags)
97 : SyntheticSection(ctx, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, 8),
98 flags(flags) {
99 this->entsize = sizeof(Elf_Mips_ABIFlags);
100}
101
102template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
103 memcpy(buf, &flags, sizeof(flags));
104}
105
106template <class ELFT>
107std::unique_ptr<MipsAbiFlagsSection<ELFT>>
108MipsAbiFlagsSection<ELFT>::create(Ctx &ctx) {
109 Elf_Mips_ABIFlags flags = {};
110 bool create = false;
111
112 for (InputSectionBase *sec : ctx.inputSections) {
113 if (sec->type != SHT_MIPS_ABIFLAGS)
114 continue;
115 sec->markDead();
116 create = true;
117
118 const size_t size = sec->content().size();
119 // Older version of BFD (such as the default FreeBSD linker) concatenate
120 // .MIPS.abiflags instead of merging. To allow for this case (or potential
121 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
122 if (size < sizeof(Elf_Mips_ABIFlags)) {
123 Err(ctx) << sec->file << ": invalid size of .MIPS.abiflags section: got "
124 << size << " instead of " << sizeof(Elf_Mips_ABIFlags);
125 return nullptr;
126 }
127 auto *s =
128 reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->content().data());
129 if (s->version != 0) {
130 Err(ctx) << sec->file << ": unexpected .MIPS.abiflags version "
131 << s->version;
132 return nullptr;
133 }
134
135 // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
136 // select the highest number of ISA/Rev/Ext.
137 flags.isa_level = std::max(flags.isa_level, s->isa_level);
138 flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
139 flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
140 flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
141 flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
142 flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
143 flags.ases |= s->ases;
144 flags.flags1 |= s->flags1;
145 flags.flags2 |= s->flags2;
146 flags.fp_abi =
147 elf::getMipsFpAbiFlag(ctx, file: sec->file, oldFlag: flags.fp_abi, newFlag: s->fp_abi);
148 };
149
150 if (create)
151 return std::make_unique<MipsAbiFlagsSection<ELFT>>(ctx, flags);
152 return nullptr;
153}
154
155// .MIPS.options section.
156template <class ELFT>
157MipsOptionsSection<ELFT>::MipsOptionsSection(Ctx &ctx, Elf_Mips_RegInfo reginfo)
158 : SyntheticSection(ctx, ".MIPS.options", SHT_MIPS_OPTIONS, SHF_ALLOC, 8),
159 reginfo(reginfo) {
160 this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
161}
162
163template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
164 auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
165 options->kind = ODK_REGINFO;
166 options->size = getSize();
167
168 if (!ctx.arg.relocatable)
169 reginfo.ri_gp_value = ctx.in.mipsGot->getGp();
170 memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
171}
172
173template <class ELFT>
174std::unique_ptr<MipsOptionsSection<ELFT>>
175MipsOptionsSection<ELFT>::create(Ctx &ctx) {
176 // N64 ABI only.
177 if (!ELFT::Is64Bits)
178 return nullptr;
179
180 SmallVector<InputSectionBase *, 0> sections;
181 for (InputSectionBase *sec : ctx.inputSections)
182 if (sec->type == SHT_MIPS_OPTIONS)
183 sections.push_back(Elt: sec);
184
185 if (sections.empty())
186 return nullptr;
187
188 Elf_Mips_RegInfo reginfo = {};
189 for (InputSectionBase *sec : sections) {
190 sec->markDead();
191
192 ArrayRef<uint8_t> d = sec->content();
193 while (!d.empty()) {
194 if (d.size() < sizeof(Elf_Mips_Options)) {
195 Err(ctx) << sec->file << ": invalid size of .MIPS.options section";
196 break;
197 }
198
199 auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
200 if (opt->kind == ODK_REGINFO) {
201 reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
202 sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
203 break;
204 }
205
206 if (!opt->size) {
207 Err(ctx) << sec->file << ": zero option descriptor size";
208 break;
209 }
210 d = d.slice(opt->size);
211 }
212 };
213
214 return std::make_unique<MipsOptionsSection<ELFT>>(ctx, reginfo);
215}
216
217// MIPS .reginfo section.
218template <class ELFT>
219MipsReginfoSection<ELFT>::MipsReginfoSection(Ctx &ctx, Elf_Mips_RegInfo reginfo)
220 : SyntheticSection(ctx, ".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC, 4),
221 reginfo(reginfo) {
222 this->entsize = sizeof(Elf_Mips_RegInfo);
223}
224
225template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
226 if (!ctx.arg.relocatable)
227 reginfo.ri_gp_value = ctx.in.mipsGot->getGp();
228 memcpy(buf, &reginfo, sizeof(reginfo));
229}
230
231template <class ELFT>
232std::unique_ptr<MipsReginfoSection<ELFT>>
233MipsReginfoSection<ELFT>::create(Ctx &ctx) {
234 // Section should be alive for O32 and N32 ABIs only.
235 if (ELFT::Is64Bits)
236 return nullptr;
237
238 SmallVector<InputSectionBase *, 0> sections;
239 for (InputSectionBase *sec : ctx.inputSections)
240 if (sec->type == SHT_MIPS_REGINFO)
241 sections.push_back(Elt: sec);
242
243 if (sections.empty())
244 return nullptr;
245
246 Elf_Mips_RegInfo reginfo = {};
247 for (InputSectionBase *sec : sections) {
248 sec->markDead();
249
250 if (sec->content().size() != sizeof(Elf_Mips_RegInfo)) {
251 Err(ctx) << sec->file << ": invalid size of .reginfo section";
252 return nullptr;
253 }
254
255 auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->content().data());
256 reginfo.ri_gprmask |= r->ri_gprmask;
257 sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
258 };
259
260 return std::make_unique<MipsReginfoSection<ELFT>>(ctx, reginfo);
261}
262
263InputSection *elf::createInterpSection(Ctx &ctx) {
264 // StringSaver guarantees that the returned string ends with '\0'.
265 StringRef s = ctx.saver.save(S: ctx.arg.dynamicLinker);
266 ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
267
268 return make<InputSection>(args&: ctx.internalFile, args: ".interp", args: SHT_PROGBITS,
269 args: SHF_ALLOC,
270 /*addralign=*/args: 1, /*entsize=*/args: 0, args&: contents);
271}
272
273Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
274 uint64_t value, uint64_t size,
275 InputSectionBase &section) {
276 Defined *s = makeDefined(args&: ctx, args&: section.file, args&: name, args: STB_LOCAL, args: STV_DEFAULT,
277 args&: type, args&: value, args&: size, args: &section);
278 if (ctx.in.symTab)
279 ctx.in.symTab->addSymbol(sym: s);
280
281 if (ctx.arg.emachine == EM_ARM && !ctx.arg.isLE && ctx.arg.armBe8 &&
282 (section.flags & SHF_EXECINSTR))
283 // Adding Linker generated mapping symbols to the arm specific mapping
284 // symbols list.
285 addArmSyntheticSectionMappingSymbol(s);
286
287 return s;
288}
289
290static size_t getHashSize(Ctx &ctx) {
291 switch (ctx.arg.buildId) {
292 case BuildIdKind::Fast:
293 return 8;
294 case BuildIdKind::Md5:
295 case BuildIdKind::Uuid:
296 return 16;
297 case BuildIdKind::Sha1:
298 return 20;
299 case BuildIdKind::Hexstring:
300 return ctx.arg.buildIdVector.size();
301 default:
302 llvm_unreachable("unknown BuildIdKind");
303 }
304}
305
306// This class represents a linker-synthesized .note.gnu.property section.
307//
308// In x86 and AArch64, object files may contain feature flags indicating the
309// features that they have used. The flags are stored in a .note.gnu.property
310// section.
311//
312// lld reads the sections from input files and merges them by computing AND of
313// the flags. The result is written as a new .note.gnu.property section.
314//
315// If the flag is zero (which indicates that the intersection of the feature
316// sets is empty, or some input files didn't have .note.gnu.property sections),
317// we don't create this section.
318GnuPropertySection::GnuPropertySection(Ctx &ctx)
319 : SyntheticSection(ctx, ".note.gnu.property", SHT_NOTE, SHF_ALLOC,
320 ctx.arg.wordsize) {}
321
322void GnuPropertySection::writeTo(uint8_t *buf) {
323 uint32_t featureAndType;
324 switch (ctx.arg.emachine) {
325 case EM_386:
326 case EM_X86_64:
327 featureAndType = GNU_PROPERTY_X86_FEATURE_1_AND;
328 break;
329 case EM_AARCH64:
330 featureAndType = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
331 break;
332 case EM_RISCV:
333 featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;
334 break;
335 default:
336 llvm_unreachable(
337 "target machine does not support .note.gnu.property section");
338 }
339
340 write32(ctx, p: buf, v: 4); // Name size
341 write32(ctx, p: buf + 4, v: getSize() - 16); // Content size
342 write32(ctx, p: buf + 8, v: NT_GNU_PROPERTY_TYPE_0); // Type
343 memcpy(dest: buf + 12, src: "GNU", n: 4); // Name string
344
345 unsigned offset = 16;
346 if (ctx.arg.andFeatures != 0) {
347 write32(ctx, p: buf + offset + 0, v: featureAndType); // Feature type
348 write32(ctx, p: buf + offset + 4, v: 4); // Feature size
349 write32(ctx, p: buf + offset + 8, v: ctx.arg.andFeatures); // Feature flags
350 if (ctx.arg.is64)
351 write32(ctx, p: buf + offset + 12, v: 0); // Padding
352 offset += 16;
353 }
354
355 if (ctx.aarch64PauthAbiCoreInfo) {
356 write32(ctx, p: buf + offset + 0, v: GNU_PROPERTY_AARCH64_FEATURE_PAUTH);
357 write32(ctx, p: buf + offset + 4, v: AArch64PauthAbiCoreInfo::size());
358 write64(ctx, p: buf + offset + 8, v: ctx.aarch64PauthAbiCoreInfo->platform);
359 write64(ctx, p: buf + offset + 16, v: ctx.aarch64PauthAbiCoreInfo->version);
360 }
361}
362
363size_t GnuPropertySection::getSize() const {
364 uint32_t contentSize = 0;
365 if (ctx.arg.andFeatures != 0)
366 contentSize += ctx.arg.is64 ? 16 : 12;
367 if (ctx.aarch64PauthAbiCoreInfo)
368 contentSize += 4 + 4 + AArch64PauthAbiCoreInfo::size();
369 assert(contentSize != 0);
370 return contentSize + 16;
371}
372
373BuildIdSection::BuildIdSection(Ctx &ctx)
374 : SyntheticSection(ctx, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, 4),
375 hashSize(getHashSize(ctx)) {}
376
377void BuildIdSection::writeTo(uint8_t *buf) {
378 write32(ctx, p: buf, v: 4); // Name size
379 write32(ctx, p: buf + 4, v: hashSize); // Content size
380 write32(ctx, p: buf + 8, v: NT_GNU_BUILD_ID); // Type
381 memcpy(dest: buf + 12, src: "GNU", n: 4); // Name string
382 hashBuf = buf + 16;
383}
384
385void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {
386 assert(buf.size() == hashSize);
387 memcpy(dest: hashBuf, src: buf.data(), n: hashSize);
388}
389
390BssSection::BssSection(Ctx &ctx, StringRef name, uint64_t size,
391 uint32_t alignment)
392 : SyntheticSection(ctx, name, SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
393 alignment) {
394 this->bss = true;
395 this->size = size;
396}
397
398EhFrameSection::EhFrameSection(Ctx &ctx)
399 : SyntheticSection(ctx, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, 1) {}
400
401// Search for an existing CIE record or create a new one.
402// CIE records from input object files are uniquified by their contents
403// and where their relocations point to.
404CieRecord *EhFrameSection::addCie(EhSectionPiece &cie,
405 ArrayRef<Relocation> rels) {
406 Symbol *personality = nullptr;
407 unsigned firstRelI = cie.firstRelocation;
408 if (firstRelI != (unsigned)-1)
409 personality = rels[firstRelI].sym;
410
411 // Search for an existing CIE by CIE contents/relocation target pair.
412 CieRecord *&rec = cieMap[{cie.data(), personality}];
413
414 // If not found, create a new one.
415 if (!rec) {
416 rec = make<CieRecord>();
417 rec->cie = &cie;
418 cieRecords.push_back(Elt: rec);
419 }
420 return rec;
421}
422
423// There is one FDE per function. Returns a non-null pointer to the function
424// symbol if the given FDE points to a live function.
425Defined *EhFrameSection::isFdeLive(EhSectionPiece &fde,
426 ArrayRef<Relocation> rels) {
427 // An FDE should point to some function because FDEs are to describe
428 // functions. That's however not always the case due to an issue of
429 // ld.gold with -r. ld.gold may discard only functions and leave their
430 // corresponding FDEs, which results in creating bad .eh_frame sections.
431 // To deal with that, we ignore such FDEs.
432 unsigned firstRelI = fde.firstRelocation;
433 if (firstRelI == (unsigned)-1)
434 return nullptr;
435
436 // FDEs for garbage-collected or merged-by-ICF sections, or sections in
437 // another partition, are dead.
438 if (auto *d = dyn_cast<Defined>(Val: rels[firstRelI].sym))
439 if (!d->folded && d->section && d->section->partition == partition)
440 return d;
441 return nullptr;
442}
443
444// .eh_frame is a sequence of CIE or FDE records. In general, there
445// is one CIE record per input object file which is followed by
446// a list of FDEs. This function searches an existing CIE or create a new
447// one and associates FDEs to the CIE.
448template <endianness e> void EhFrameSection::addRecords(EhInputSection *sec) {
449 auto rels = sec->rels;
450 offsetToCie.clear();
451 for (EhSectionPiece &cie : sec->cies)
452 offsetToCie[cie.inputOff] = addCie(cie, rels);
453 for (EhSectionPiece &fde : sec->fdes) {
454 uint32_t id = endian::read32<e>(fde.data().data() + 4);
455 CieRecord *rec = offsetToCie[fde.inputOff + 4 - id];
456 if (!rec)
457 Fatal(ctx) << sec << ": invalid CIE reference";
458
459 if (!isFdeLive(fde, rels))
460 continue;
461 rec->fdes.push_back(Elt: &fde);
462 numFdes++;
463 }
464}
465
466// Used by ICF<ELFT>::handleLSDA(). This function is very similar to
467// EhFrameSection::addRecords().
468template <class ELFT>
469void EhFrameSection::iterateFDEWithLSDAAux(
470 EhInputSection &sec, DenseSet<size_t> &ciesWithLSDA,
471 llvm::function_ref<void(InputSection &)> fn) {
472 for (EhSectionPiece &cie : sec.cies)
473 if (hasLSDA(p: cie))
474 ciesWithLSDA.insert(V: cie.inputOff);
475 for (EhSectionPiece &fde : sec.fdes) {
476 uint32_t id = endian::read32<ELFT::Endianness>(fde.data().data() + 4);
477 if (!ciesWithLSDA.contains(V: fde.inputOff + 4 - id))
478 continue;
479
480 // The CIE has a LSDA argument. Call fn with d's section.
481 if (Defined *d = isFdeLive(fde, rels: sec.rels))
482 if (auto *s = dyn_cast_or_null<InputSection>(Val: d->section))
483 fn(*s);
484 }
485}
486
487template <class ELFT>
488void EhFrameSection::iterateFDEWithLSDA(
489 llvm::function_ref<void(InputSection &)> fn) {
490 DenseSet<size_t> ciesWithLSDA;
491 for (EhInputSection *sec : sections) {
492 ciesWithLSDA.clear();
493 iterateFDEWithLSDAAux<ELFT>(*sec, ciesWithLSDA, fn);
494 }
495}
496
497static void writeCieFde(Ctx &ctx, uint8_t *buf, ArrayRef<uint8_t> d) {
498 memcpy(dest: buf, src: d.data(), n: d.size());
499 // Fix the size field. -4 since size does not include the size field itself.
500 write32(ctx, p: buf, v: d.size() - 4);
501}
502
503void EhFrameSection::finalizeContents() {
504 assert(!this->size); // Not finalized.
505
506 switch (ctx.arg.ekind) {
507 case ELFNoneKind:
508 llvm_unreachable("invalid ekind");
509 case ELF32LEKind:
510 case ELF64LEKind:
511 for (EhInputSection *sec : sections)
512 if (sec->isLive())
513 addRecords<endianness::little>(sec);
514 break;
515 case ELF32BEKind:
516 case ELF64BEKind:
517 for (EhInputSection *sec : sections)
518 if (sec->isLive())
519 addRecords<endianness::big>(sec);
520 break;
521 }
522
523 size_t off = 0;
524 for (CieRecord *rec : cieRecords) {
525 rec->cie->outputOff = off;
526 off += rec->cie->size;
527
528 for (EhSectionPiece *fde : rec->fdes) {
529 fde->outputOff = off;
530 off += fde->size;
531 }
532 }
533
534 // The LSB standard does not allow a .eh_frame section with zero
535 // Call Frame Information records. glibc unwind-dw2-fde.c
536 // classify_object_over_fdes expects there is a CIE record length 0 as a
537 // terminator. Thus we add one unconditionally.
538 off += 4;
539
540 this->size = off;
541}
542
543static uint64_t readFdeAddr(Ctx &ctx, uint8_t *buf, int size) {
544 switch (size) {
545 case DW_EH_PE_udata2:
546 return read16(ctx, p: buf);
547 case DW_EH_PE_sdata2:
548 return (int16_t)read16(ctx, p: buf);
549 case DW_EH_PE_udata4:
550 return read32(ctx, p: buf);
551 case DW_EH_PE_sdata4:
552 return (int32_t)read32(ctx, p: buf);
553 case DW_EH_PE_udata8:
554 case DW_EH_PE_sdata8:
555 return read64(ctx, p: buf);
556 case DW_EH_PE_absptr:
557 return readUint(ctx, buf);
558 }
559 Err(ctx) << "unknown FDE size encoding";
560 return 0;
561}
562
563// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
564// We need it to create .eh_frame_hdr section.
565uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,
566 uint8_t enc) const {
567 // The starting address to which this FDE applies is
568 // stored at FDE + 8 byte. And this offset is within
569 // the .eh_frame section.
570 size_t off = fdeOff + 8;
571 uint64_t addr = readFdeAddr(ctx, buf: buf + off, size: enc & 0xf);
572 if ((enc & 0x70) == DW_EH_PE_absptr)
573 return ctx.arg.is64 ? addr : uint32_t(addr);
574 if ((enc & 0x70) == DW_EH_PE_pcrel)
575 return addr + getParent()->addr + off + outSecOff;
576 Err(ctx) << "unknown FDE size relative encoding";
577 return 0;
578}
579
580void EhFrameSection::writeTo(uint8_t *buf) {
581 // Write CIE and FDE records.
582 for (CieRecord *rec : cieRecords) {
583 size_t cieOffset = rec->cie->outputOff;
584 writeCieFde(ctx, buf: buf + cieOffset, d: rec->cie->data());
585
586 for (EhSectionPiece *fde : rec->fdes) {
587 size_t off = fde->outputOff;
588 writeCieFde(ctx, buf: buf + off, d: fde->data());
589
590 // FDE's second word should have the offset to an associated CIE.
591 // Write it.
592 write32(ctx, p: buf + off + 4, v: off + 4 - cieOffset);
593 }
594 }
595
596 // Apply relocations to .eh_frame entries. This includes CIE personality
597 // pointers, FDE initial_location fields, and LSDA pointers.
598 for (EhInputSection *s : sections)
599 ctx.target->relocateEh(sec&: *s, buf);
600
601 EhFrameHeader *hdr = getPartition(ctx).ehFrameHdr.get();
602 if (!hdr || !hdr->getParent())
603 return;
604
605 // Write the .eh_frame_hdr section, which contains a binary search table of
606 // pointers to FDEs. This must be written after .eh_frame relocation since
607 // the content depends on relocated initial_location fields in FDEs.
608 using FdeData = EhFrameSection::FdeData;
609 SmallVector<FdeData, 0> fdes;
610 uint64_t va = hdr->getVA();
611 for (CieRecord *rec : cieRecords) {
612 uint8_t enc = getFdeEncoding(p: rec->cie);
613 for (EhSectionPiece *fde : rec->fdes) {
614 uint64_t pc = getFdePc(buf, fdeOff: fde->outputOff, enc);
615 uint64_t fdeVA = getParent()->addr + fde->outputOff;
616 if (!isInt<32>(x: pc - va)) {
617 Err(ctx) << fde->sec << ": PC offset is too large: 0x"
618 << Twine::utohexstr(Val: pc - va);
619 continue;
620 }
621 fdes.push_back(Elt: {.pcRel: uint32_t(pc - va), .fdeVARel: uint32_t(fdeVA - va)});
622 }
623 }
624
625 // Sort the FDE list by their PC and uniqueify. Usually there is only
626 // one FDE for a PC (i.e. function), but if ICF merges two functions
627 // into one, there can be more than one FDEs pointing to the address.
628 llvm::stable_sort(Range&: fdes, C: [](const FdeData &a, const FdeData &b) {
629 return a.pcRel < b.pcRel;
630 });
631 fdes.erase(
632 CS: llvm::unique(R&: fdes, P: [](auto &a, auto &b) { return a.pcRel == b.pcRel; }),
633 CE: fdes.end());
634
635 // Write header.
636 uint8_t *hdrBuf = ctx.bufferStart + hdr->getParent()->offset + hdr->outSecOff;
637 hdrBuf[0] = 1; // version
638 hdrBuf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; // eh_frame_ptr_enc
639 hdrBuf[2] = DW_EH_PE_udata4; // fde_count_enc
640 hdrBuf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; // table_enc
641 write32(ctx, p: hdrBuf + 4,
642 v: getParent()->addr - hdr->getVA() - 4); // eh_frame_ptr
643 write32(ctx, p: hdrBuf + 8, v: fdes.size()); // fde_count
644 hdrBuf += 12;
645
646 // Write binary search table. Each entry describes the starting PC and the FDE
647 // address.
648 for (FdeData &fde : fdes) {
649 write32(ctx, p: hdrBuf, v: fde.pcRel);
650 write32(ctx, p: hdrBuf + 4, v: fde.fdeVARel);
651 hdrBuf += 8;
652 }
653}
654
655EhFrameHeader::EhFrameHeader(Ctx &ctx)
656 : SyntheticSection(ctx, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, 4) {}
657
658void EhFrameHeader::writeTo(uint8_t *buf) {
659 // The section content is written during EhFrameSection::writeTo.
660}
661
662size_t EhFrameHeader::getSize() const {
663 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
664 return 12 + getPartition(ctx).ehFrame->numFdes * 8;
665}
666
667bool EhFrameHeader::isNeeded() const {
668 return isLive() && getPartition(ctx).ehFrame->isNeeded();
669}
670
671GotSection::GotSection(Ctx &ctx)
672 : SyntheticSection(ctx, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
673 ctx.target->gotEntrySize) {
674 numEntries = ctx.target->gotHeaderEntriesNum;
675}
676
677void GotSection::addEntry(const Symbol &sym) {
678 assert(sym.auxIdx == ctx.symAux.size() - 1);
679 ctx.symAux.back().gotIdx = numEntries++;
680}
681
682void GotSection::addAuthEntry(const Symbol &sym) {
683 authEntries.push_back(
684 Elt: {.offset: (numEntries - 1) * ctx.target->gotEntrySize, .isSymbolFunc: sym.isFunc()});
685}
686
687bool GotSection::addTlsDescEntry(const Symbol &sym) {
688 assert(sym.auxIdx == ctx.symAux.size() - 1);
689 ctx.symAux.back().tlsDescIdx = numEntries;
690 numEntries += 2;
691 return true;
692}
693
694void GotSection::addTlsDescAuthEntry() {
695 authEntries.push_back(Elt: {.offset: (numEntries - 2) * ctx.target->gotEntrySize, .isSymbolFunc: true});
696 authEntries.push_back(Elt: {.offset: (numEntries - 1) * ctx.target->gotEntrySize, .isSymbolFunc: false});
697}
698
699bool GotSection::addDynTlsEntry(const Symbol &sym) {
700 assert(sym.auxIdx == ctx.symAux.size() - 1);
701 ctx.symAux.back().tlsGdIdx = numEntries;
702 // Global Dynamic TLS entries take two GOT slots.
703 numEntries += 2;
704 return true;
705}
706
707// Reserves TLS entries for a TLS module ID and a TLS block offset.
708// In total it takes two GOT slots.
709bool GotSection::addTlsIndex() {
710 if (tlsIndexOff != uint32_t(-1))
711 return false;
712 tlsIndexOff = numEntries * ctx.target->gotEntrySize;
713 numEntries += 2;
714 return true;
715}
716
717uint32_t GotSection::getTlsDescOffset(const Symbol &sym) const {
718 return sym.getTlsDescIdx(ctx) * ctx.target->gotEntrySize;
719}
720
721uint64_t GotSection::getTlsDescAddr(const Symbol &sym) const {
722 return getVA() + getTlsDescOffset(sym);
723}
724
725uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {
726 return this->getVA() + b.getTlsGdIdx(ctx) * ctx.target->gotEntrySize;
727}
728
729uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {
730 return b.getTlsGdIdx(ctx) * ctx.target->gotEntrySize;
731}
732
733void GotSection::finalizeContents() {
734 if (ctx.arg.emachine == EM_PPC64 &&
735 numEntries <= ctx.target->gotHeaderEntriesNum &&
736 !ctx.sym.globalOffsetTable)
737 size = 0;
738 else
739 size = numEntries * ctx.target->gotEntrySize;
740}
741
742bool GotSection::isNeeded() const {
743 // Needed if the GOT symbol is used or the number of entries is more than just
744 // the header. A GOT with just the header may not be needed.
745 return hasGotOffRel || numEntries > ctx.target->gotHeaderEntriesNum;
746}
747
748void GotSection::writeTo(uint8_t *buf) {
749 // On PPC64 .got may be needed but empty. Skip the write.
750 if (size == 0)
751 return;
752 ctx.target->writeGotHeader(buf);
753 ctx.target->relocateAlloc(sec&: *this, buf);
754 for (const AuthEntryInfo &authEntry : authEntries) {
755 // https://github.com/ARM-software/abi-aa/blob/2024Q3/pauthabielf64/pauthabielf64.rst#default-signing-schema
756 // Signed GOT entries use the IA key for symbols of type STT_FUNC and the
757 // DA key for all other symbol types, with the address of the GOT entry as
758 // the modifier. The static linker must encode the signing schema into the
759 // GOT slot.
760 //
761 // https://github.com/ARM-software/abi-aa/blob/2024Q3/pauthabielf64/pauthabielf64.rst#encoding-the-signing-schema
762 // If address diversity is set and the discriminator
763 // is 0 then modifier = Place
764 uint8_t *dest = buf + authEntry.offset;
765 uint64_t key = authEntry.isSymbolFunc ? /*IA=*/0b00 : /*DA=*/0b10;
766 uint64_t addrDiversity = 1;
767 write64(ctx, p: dest, v: (addrDiversity << 63) | (key << 60));
768 }
769}
770
771static uint64_t getMipsPageCount(uint64_t size) {
772 return (size + 0xfffe) / 0xffff + 1;
773}
774
775MipsGotSection::MipsGotSection(Ctx &ctx)
776 : SyntheticSection(ctx, ".got", SHT_PROGBITS,
777 SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, 16) {}
778
779void MipsGotSection::addConstant(const Relocation &r) {
780 relocations.push_back(Elt: r);
781}
782
783void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,
784 RelExpr expr) {
785 FileGot &g = getGot(f&: file);
786 if (expr == RE_MIPS_GOT_LOCAL_PAGE) {
787 if (const OutputSection *os = sym.getOutputSection())
788 g.pagesMap.insert(KV: {os, {&sym}});
789 else
790 g.local16.insert(KV: {{nullptr, getMipsPageAddr(addr: sym.getVA(ctx, addend))}, 0});
791 } else if (sym.isTls())
792 g.tls.insert(KV: {&sym, 0});
793 else if (sym.isPreemptible && expr == R_ABS)
794 g.relocs.insert(KV: {&sym, 0});
795 else if (sym.isPreemptible)
796 g.global.insert(KV: {&sym, 0});
797 else if (expr == RE_MIPS_GOT_OFF32)
798 g.local32.insert(KV: {{&sym, addend}, 0});
799 else
800 g.local16.insert(KV: {{&sym, addend}, 0});
801}
802
803void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) {
804 getGot(f&: file).dynTlsSymbols.insert(KV: {&sym, 0});
805}
806
807void MipsGotSection::addTlsIndex(InputFile &file) {
808 getGot(f&: file).dynTlsSymbols.insert(KV: {nullptr, 0});
809}
810
811size_t MipsGotSection::FileGot::getEntriesNum() const {
812 return getPageEntriesNum() + local16.size() + global.size() + relocs.size() +
813 tls.size() + dynTlsSymbols.size() * 2;
814}
815
816size_t MipsGotSection::FileGot::getPageEntriesNum() const {
817 size_t num = 0;
818 for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap)
819 num += p.second.count;
820 return num;
821}
822
823size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
824 size_t count = getPageEntriesNum() + local16.size() + global.size();
825 // If there are relocation-only entries in the GOT, TLS entries
826 // are allocated after them. TLS entries should be addressable
827 // by 16-bit index so count both reloc-only and TLS entries.
828 if (!tls.empty() || !dynTlsSymbols.empty())
829 count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2;
830 return count;
831}
832
833MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) {
834 if (f.mipsGotIndex == uint32_t(-1)) {
835 gots.emplace_back();
836 gots.back().file = &f;
837 f.mipsGotIndex = gots.size() - 1;
838 }
839 return gots[f.mipsGotIndex];
840}
841
842uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f,
843 const Symbol &sym,
844 int64_t addend) const {
845 const FileGot &g = gots[f->mipsGotIndex];
846 uint64_t index = 0;
847 if (const OutputSection *outSec = sym.getOutputSection()) {
848 uint64_t secAddr = getMipsPageAddr(addr: outSec->addr);
849 uint64_t symAddr = getMipsPageAddr(addr: sym.getVA(ctx, addend));
850 index = g.pagesMap.lookup(Key: outSec).firstIndex + (symAddr - secAddr) / 0xffff;
851 } else {
852 index =
853 g.local16.lookup(Key: {nullptr, getMipsPageAddr(addr: sym.getVA(ctx, addend))});
854 }
855 return index * ctx.arg.wordsize;
856}
857
858uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s,
859 int64_t addend) const {
860 const FileGot &g = gots[f->mipsGotIndex];
861 Symbol *sym = const_cast<Symbol *>(&s);
862 if (sym->isTls())
863 return g.tls.lookup(Key: sym) * ctx.arg.wordsize;
864 if (sym->isPreemptible)
865 return g.global.lookup(Key: sym) * ctx.arg.wordsize;
866 return g.local16.lookup(Key: {sym, addend}) * ctx.arg.wordsize;
867}
868
869uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const {
870 const FileGot &g = gots[f->mipsGotIndex];
871 return g.dynTlsSymbols.lookup(Key: nullptr) * ctx.arg.wordsize;
872}
873
874uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f,
875 const Symbol &s) const {
876 const FileGot &g = gots[f->mipsGotIndex];
877 Symbol *sym = const_cast<Symbol *>(&s);
878 return g.dynTlsSymbols.lookup(Key: sym) * ctx.arg.wordsize;
879}
880
881const Symbol *MipsGotSection::getFirstGlobalEntry() const {
882 if (gots.empty())
883 return nullptr;
884 const FileGot &primGot = gots.front();
885 if (!primGot.global.empty())
886 return primGot.global.front().first;
887 if (!primGot.relocs.empty())
888 return primGot.relocs.front().first;
889 return nullptr;
890}
891
892unsigned MipsGotSection::getLocalEntriesNum() const {
893 if (gots.empty())
894 return headerEntriesNum;
895 return headerEntriesNum + gots.front().getPageEntriesNum() +
896 gots.front().local16.size();
897}
898
899bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {
900 FileGot tmp = dst;
901 set_union(S1&: tmp.pagesMap, S2: src.pagesMap);
902 set_union(S1&: tmp.local16, S2: src.local16);
903 set_union(S1&: tmp.global, S2: src.global);
904 set_union(S1&: tmp.relocs, S2: src.relocs);
905 set_union(S1&: tmp.tls, S2: src.tls);
906 set_union(S1&: tmp.dynTlsSymbols, S2: src.dynTlsSymbols);
907
908 size_t count = isPrimary ? headerEntriesNum : 0;
909 count += tmp.getIndexedEntriesNum();
910
911 if (count * ctx.arg.wordsize > ctx.arg.mipsGotSize)
912 return false;
913
914 std::swap(a&: tmp, b&: dst);
915 return true;
916}
917
918void MipsGotSection::finalizeContents() { updateAllocSize(ctx); }
919
920bool MipsGotSection::updateAllocSize(Ctx &ctx) {
921 size = headerEntriesNum * ctx.arg.wordsize;
922 for (const FileGot &g : gots)
923 size += g.getEntriesNum() * ctx.arg.wordsize;
924 return false;
925}
926
927void MipsGotSection::build() {
928 if (gots.empty())
929 return;
930
931 std::vector<FileGot> mergedGots(1);
932
933 // For each GOT move non-preemptible symbols from the `Global`
934 // to `Local16` list. Preemptible symbol might become non-preemptible
935 // one if, for example, it gets a related copy relocation.
936 for (FileGot &got : gots) {
937 for (auto &p: got.global)
938 if (!p.first->isPreemptible)
939 got.local16.insert(KV: {{p.first, 0}, 0});
940 got.global.remove_if(Pred: [&](const std::pair<Symbol *, size_t> &p) {
941 return !p.first->isPreemptible;
942 });
943 }
944
945 // For each GOT remove "reloc-only" entry if there is "global"
946 // entry for the same symbol. And add local entries which indexed
947 // using 32-bit value at the end of 16-bit entries.
948 for (FileGot &got : gots) {
949 got.relocs.remove_if(Pred: [&](const std::pair<Symbol *, size_t> &p) {
950 return got.global.contains(Key: p.first);
951 });
952 set_union(S1&: got.local16, S2: got.local32);
953 got.local32.clear();
954 }
955
956 // Evaluate number of "reloc-only" entries in the resulting GOT.
957 // To do that put all unique "reloc-only" and "global" entries
958 // from all GOTs to the future primary GOT.
959 FileGot *primGot = &mergedGots.front();
960 for (FileGot &got : gots) {
961 set_union(S1&: primGot->relocs, S2: got.global);
962 set_union(S1&: primGot->relocs, S2: got.relocs);
963 got.relocs.clear();
964 }
965
966 // Evaluate number of "page" entries in each GOT.
967 for (FileGot &got : gots) {
968 for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
969 got.pagesMap) {
970 const OutputSection *os = p.first;
971 uint64_t secSize = 0;
972 for (SectionCommand *cmd : os->commands) {
973 if (auto *isd = dyn_cast<InputSectionDescription>(Val: cmd))
974 for (InputSection *isec : isd->sections) {
975 uint64_t off = alignToPowerOf2(Value: secSize, Align: isec->addralign);
976 secSize = off + isec->getSize();
977 }
978 }
979 p.second.count = getMipsPageCount(size: secSize);
980 }
981 }
982
983 // Merge GOTs. Try to join as much as possible GOTs but do not exceed
984 // maximum GOT size. At first, try to fill the primary GOT because
985 // the primary GOT can be accessed in the most effective way. If it
986 // is not possible, try to fill the last GOT in the list, and finally
987 // create a new GOT if both attempts failed.
988 for (FileGot &srcGot : gots) {
989 InputFile *file = srcGot.file;
990 if (tryMergeGots(dst&: mergedGots.front(), src&: srcGot, isPrimary: true)) {
991 file->mipsGotIndex = 0;
992 } else {
993 // If this is the first time we failed to merge with the primary GOT,
994 // MergedGots.back() will also be the primary GOT. We must make sure not
995 // to try to merge again with isPrimary=false, as otherwise, if the
996 // inputs are just right, we could allow the primary GOT to become 1 or 2
997 // words bigger due to ignoring the header size.
998 if (mergedGots.size() == 1 ||
999 !tryMergeGots(dst&: mergedGots.back(), src&: srcGot, isPrimary: false)) {
1000 mergedGots.emplace_back();
1001 std::swap(a&: mergedGots.back(), b&: srcGot);
1002 }
1003 file->mipsGotIndex = mergedGots.size() - 1;
1004 }
1005 }
1006 std::swap(x&: gots, y&: mergedGots);
1007
1008 // Reduce number of "reloc-only" entries in the primary GOT
1009 // by subtracting "global" entries in the primary GOT.
1010 primGot = &gots.front();
1011 primGot->relocs.remove_if(Pred: [&](const std::pair<Symbol *, size_t> &p) {
1012 return primGot->global.contains(Key: p.first);
1013 });
1014
1015 // Calculate indexes for each GOT entry.
1016 size_t index = headerEntriesNum;
1017 for (FileGot &got : gots) {
1018 got.startIndex = &got == primGot ? 0 : index;
1019 for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
1020 got.pagesMap) {
1021 // For each output section referenced by GOT page relocations calculate
1022 // and save into pagesMap an upper bound of MIPS GOT entries required
1023 // to store page addresses of local symbols. We assume the worst case -
1024 // each 64kb page of the output section has at least one GOT relocation
1025 // against it. And take in account the case when the section intersects
1026 // page boundaries.
1027 p.second.firstIndex = index;
1028 index += p.second.count;
1029 }
1030 for (auto &p: got.local16)
1031 p.second = index++;
1032 for (auto &p: got.global)
1033 p.second = index++;
1034 for (auto &p: got.relocs)
1035 p.second = index++;
1036 for (auto &p: got.tls)
1037 p.second = index++;
1038 for (auto &p: got.dynTlsSymbols) {
1039 p.second = index;
1040 index += 2;
1041 }
1042 }
1043
1044 // Update SymbolAux::gotIdx field to use this
1045 // value later in the `sortMipsSymbols` function.
1046 for (auto &p : primGot->global) {
1047 if (p.first->auxIdx == 0)
1048 p.first->allocateAux(ctx);
1049 ctx.symAux.back().gotIdx = p.second;
1050 }
1051 for (auto &p : primGot->relocs) {
1052 if (p.first->auxIdx == 0)
1053 p.first->allocateAux(ctx);
1054 ctx.symAux.back().gotIdx = p.second;
1055 }
1056
1057 // Create relocations.
1058 //
1059 // Note the primary GOT's local and global relocations are implicit, and the
1060 // MIPS ABI requires the VA be written even for the global entries, so we
1061 // treat both as constants here.
1062 for (FileGot &got : gots) {
1063 // Create relocations for TLS entries.
1064 for (std::pair<Symbol *, size_t> &p : got.tls) {
1065 Symbol *s = p.first;
1066 uint64_t offset = p.second * ctx.arg.wordsize;
1067 // When building a shared library we still need a dynamic relocation
1068 // for the TP-relative offset as we don't know how much other data will
1069 // be allocated before us in the static TLS block.
1070 if (!s->isPreemptible && !ctx.arg.shared)
1071 addConstant(r: {.expr: R_TPREL, .type: ctx.target->symbolicRel, .offset: offset, .addend: 0, .sym: s});
1072 else
1073 ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
1074 dynType: ctx.target->tlsGotRel, isec&: *this, offsetInSec: offset, sym&: *s, addendRelType: ctx.target->symbolicRel);
1075 }
1076 for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
1077 Symbol *s = p.first;
1078 uint64_t offset = p.second * ctx.arg.wordsize;
1079 if (s == nullptr) {
1080 if (ctx.arg.shared)
1081 ctx.mainPart->relaDyn->addReloc(
1082 reloc: {ctx.target->tlsModuleIndexRel, this, offset});
1083 else
1084 addConstant(
1085 r: {.expr: R_ADDEND, .type: ctx.target->symbolicRel, .offset: offset, .addend: 1, .sym: ctx.dummySym});
1086 } else {
1087 // When building a shared library we still need a dynamic relocation
1088 // for the module index. Therefore only checking for
1089 // S->isPreemptible is not sufficient (this happens e.g. for
1090 // thread-locals that have been marked as local through a linker script)
1091 if (!s->isPreemptible && !ctx.arg.shared)
1092 // Write one to the GOT slot.
1093 addConstant(r: {.expr: R_ADDEND, .type: ctx.target->symbolicRel, .offset: offset, .addend: 1, .sym: s});
1094 else
1095 ctx.mainPart->relaDyn->addSymbolReloc(dynType: ctx.target->tlsModuleIndexRel,
1096 isec&: *this, offsetInSec: offset, sym&: *s);
1097 offset += ctx.arg.wordsize;
1098 // However, we can skip writing the TLS offset reloc for non-preemptible
1099 // symbols since it is known even in shared libraries
1100 if (s->isPreemptible)
1101 ctx.mainPart->relaDyn->addSymbolReloc(dynType: ctx.target->tlsOffsetRel, isec&: *this,
1102 offsetInSec: offset, sym&: *s);
1103 else
1104 addConstant(r: {.expr: R_ABS, .type: ctx.target->tlsOffsetRel, .offset: offset, .addend: 0, .sym: s});
1105 }
1106 }
1107
1108 // Relocations for "global" entries.
1109 for (const std::pair<Symbol *, size_t> &p : got.global) {
1110 uint64_t offset = p.second * ctx.arg.wordsize;
1111 if (&got == primGot)
1112 addConstant(r: {.expr: R_ABS, .type: ctx.target->relativeRel, .offset: offset, .addend: 0, .sym: p.first});
1113 else
1114 ctx.mainPart->relaDyn->addSymbolReloc(dynType: ctx.target->relativeRel, isec&: *this,
1115 offsetInSec: offset, sym&: *p.first);
1116 }
1117 // Relocation-only entries exist as dummy entries for dynamic symbols that
1118 // aren't otherwise in the primary GOT, as the ABI requires an entry for
1119 // each dynamic symbol. Secondary GOTs have no need for them.
1120 assert((got.relocs.empty() || &got == primGot) &&
1121 "Relocation-only entries should only be in the primary GOT");
1122 for (const std::pair<Symbol *, size_t> &p : got.relocs) {
1123 uint64_t offset = p.second * ctx.arg.wordsize;
1124 addConstant(r: {.expr: R_ABS, .type: ctx.target->relativeRel, .offset: offset, .addend: 0, .sym: p.first});
1125 }
1126
1127 // Relocations for "local" entries
1128 for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
1129 got.pagesMap) {
1130 size_t pageCount = l.second.count;
1131 for (size_t pi = 0; pi < pageCount; ++pi) {
1132 uint64_t offset = (l.second.firstIndex + pi) * ctx.arg.wordsize;
1133 int64_t addend = int64_t(pi * 0x10000);
1134 if (!ctx.arg.isPic || &got == primGot)
1135 addConstant(r: {.expr: RE_MIPS_OSEC_LOCAL_PAGE, .type: ctx.target->relativeRel, .offset: offset,
1136 .addend: addend, .sym: l.second.repSym});
1137 else
1138 ctx.mainPart->relaDyn->addRelativeReloc(
1139 dynType: ctx.target->relativeRel, isec&: *this, offsetInSec: offset, sym&: *l.second.repSym, addend,
1140 addendRelType: ctx.target->relativeRel, expr: RE_MIPS_OSEC_LOCAL_PAGE);
1141 }
1142 }
1143 for (const std::pair<GotEntry, size_t> &p : got.local16) {
1144 uint64_t offset = p.second * ctx.arg.wordsize;
1145 if (p.first.first == nullptr)
1146 addConstant(r: {.expr: R_ADDEND, .type: ctx.target->relativeRel, .offset: offset, .addend: p.first.second,
1147 .sym: ctx.dummySym});
1148 else if (!ctx.arg.isPic || &got == primGot)
1149 addConstant(r: {.expr: R_ABS, .type: ctx.target->relativeRel, .offset: offset, .addend: p.first.second,
1150 .sym: p.first.first});
1151 else
1152 ctx.mainPart->relaDyn->addRelativeReloc(
1153 dynType: ctx.target->relativeRel, isec&: *this, offsetInSec: offset, sym&: *p.first.first,
1154 addend: p.first.second, addendRelType: ctx.target->relativeRel, expr: R_ABS);
1155 }
1156 }
1157}
1158
1159bool MipsGotSection::isNeeded() const {
1160 // We add the .got section to the result for dynamic MIPS target because
1161 // its address and properties are mentioned in the .dynamic section.
1162 return !ctx.arg.relocatable;
1163}
1164
1165uint64_t MipsGotSection::getGp(const InputFile *f) const {
1166 // For files without related GOT or files refer a primary GOT
1167 // returns "common" _gp value. For secondary GOTs calculate
1168 // individual _gp values.
1169 if (!f || f->mipsGotIndex == uint32_t(-1) || f->mipsGotIndex == 0)
1170 return ctx.sym.mipsGp->getVA(ctx, addend: 0);
1171 return getVA() + gots[f->mipsGotIndex].startIndex * ctx.arg.wordsize + 0x7ff0;
1172}
1173
1174void MipsGotSection::writeTo(uint8_t *buf) {
1175 // Set the MSB of the second GOT slot. This is not required by any
1176 // MIPS ABI documentation, though.
1177 //
1178 // There is a comment in glibc saying that "The MSB of got[1] of a
1179 // gnu object is set to identify gnu objects," and in GNU gold it
1180 // says "the second entry will be used by some runtime loaders".
1181 // But how this field is being used is unclear.
1182 //
1183 // We are not really willing to mimic other linkers behaviors
1184 // without understanding why they do that, but because all files
1185 // generated by GNU tools have this special GOT value, and because
1186 // we've been doing this for years, it is probably a safe bet to
1187 // keep doing this for now. We really need to revisit this to see
1188 // if we had to do this.
1189 writeUint(ctx, buf: buf + ctx.arg.wordsize,
1190 val: (uint64_t)1 << (ctx.arg.wordsize * 8 - 1));
1191 ctx.target->relocateAlloc(sec&: *this, buf);
1192}
1193
1194// On PowerPC the .plt section is used to hold the table of function addresses
1195// instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1196// section. I don't know why we have a BSS style type for the section but it is
1197// consistent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1198GotPltSection::GotPltSection(Ctx &ctx)
1199 : SyntheticSection(ctx, ".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
1200 ctx.target->gotEntrySize) {
1201 if (ctx.arg.emachine == EM_PPC) {
1202 name = ".plt";
1203 } else if (ctx.arg.emachine == EM_PPC64) {
1204 type = SHT_NOBITS;
1205 name = ".plt";
1206 }
1207}
1208
1209void GotPltSection::addEntry(Symbol &sym) {
1210 assert(sym.auxIdx == ctx.symAux.size() - 1 &&
1211 ctx.symAux.back().pltIdx == entries.size());
1212 entries.push_back(Elt: &sym);
1213}
1214
1215size_t GotPltSection::getSize() const {
1216 return (ctx.target->gotPltHeaderEntriesNum + entries.size()) *
1217 ctx.target->gotEntrySize;
1218}
1219
1220void GotPltSection::writeTo(uint8_t *buf) {
1221 ctx.target->writeGotPltHeader(buf);
1222 buf += ctx.target->gotPltHeaderEntriesNum * ctx.target->gotEntrySize;
1223 for (const Symbol *b : entries) {
1224 ctx.target->writeGotPlt(buf, s: *b);
1225 buf += ctx.target->gotEntrySize;
1226 }
1227}
1228
1229bool GotPltSection::isNeeded() const {
1230 // We need to emit GOTPLT even if it's empty if there's a relocation relative
1231 // to it.
1232 return !entries.empty() || hasGotPltOffRel;
1233}
1234
1235static StringRef getIgotPltName(Ctx &ctx) {
1236 // On ARM the IgotPltSection is part of the GotSection.
1237 if (ctx.arg.emachine == EM_ARM)
1238 return ".got";
1239
1240 // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1241 // needs to be named the same.
1242 if (ctx.arg.emachine == EM_PPC64)
1243 return ".plt";
1244
1245 return ".got.plt";
1246}
1247
1248// On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1249// with the IgotPltSection.
1250IgotPltSection::IgotPltSection(Ctx &ctx)
1251 : SyntheticSection(ctx, getIgotPltName(ctx),
1252 ctx.arg.emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1253 SHF_ALLOC | SHF_WRITE, ctx.target->gotEntrySize) {}
1254
1255void IgotPltSection::addEntry(Symbol &sym) {
1256 assert(ctx.symAux.back().pltIdx == entries.size());
1257 entries.push_back(Elt: &sym);
1258}
1259
1260size_t IgotPltSection::getSize() const {
1261 return entries.size() * ctx.target->gotEntrySize;
1262}
1263
1264void IgotPltSection::writeTo(uint8_t *buf) {
1265 for (const Symbol *b : entries) {
1266 ctx.target->writeIgotPlt(buf, s: *b);
1267 buf += ctx.target->gotEntrySize;
1268 }
1269}
1270
1271StringTableSection::StringTableSection(Ctx &ctx, StringRef name, bool dynamic)
1272 : SyntheticSection(ctx, name, SHT_STRTAB, dynamic ? (uint64_t)SHF_ALLOC : 0,
1273 1),
1274 dynamic(dynamic) {
1275 // ELF string tables start with a NUL byte.
1276 strings.push_back(Elt: "");
1277 stringMap.try_emplace(Key: CachedHashStringRef(""), Args: 0);
1278 size = 1;
1279}
1280
1281// Adds a string to the string table. If `hashIt` is true we hash and check for
1282// duplicates. It is optional because the name of global symbols are already
1283// uniqued and hashing them again has a big cost for a small value: uniquing
1284// them with some other string that happens to be the same.
1285unsigned StringTableSection::addString(StringRef s, bool hashIt) {
1286 if (hashIt) {
1287 auto r = stringMap.try_emplace(Key: CachedHashStringRef(s), Args&: size);
1288 if (!r.second)
1289 return r.first->second;
1290 }
1291 if (s.empty())
1292 return 0;
1293 unsigned ret = this->size;
1294 this->size = this->size + s.size() + 1;
1295 strings.push_back(Elt: s);
1296 return ret;
1297}
1298
1299void StringTableSection::writeTo(uint8_t *buf) {
1300 for (StringRef s : strings) {
1301 memcpy(dest: buf, src: s.data(), n: s.size());
1302 buf[s.size()] = '\0';
1303 buf += s.size() + 1;
1304 }
1305}
1306
1307// Returns the number of entries in .gnu.version_d: the number of
1308// non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1.
1309// Note that we don't support vd_cnt > 1 yet.
1310static unsigned getVerDefNum(Ctx &ctx) {
1311 return namedVersionDefs(ctx).size() + 1;
1312}
1313
1314template <class ELFT>
1315DynamicSection<ELFT>::DynamicSection(Ctx &ctx)
1316 : SyntheticSection(ctx, ".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE,
1317 ctx.arg.wordsize) {
1318 this->entsize = ELFT::Is64Bits ? 16 : 8;
1319
1320 // .dynamic section is not writable on MIPS and on Fuchsia OS
1321 // which passes -z rodynamic.
1322 // See "Special Section" in Chapter 4 in the following document:
1323 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1324 if (ctx.arg.emachine == EM_MIPS || ctx.arg.zRodynamic)
1325 this->flags = SHF_ALLOC;
1326}
1327
1328// The output section .rela.dyn may include these synthetic sections:
1329//
1330// - part.relaDyn
1331// - ctx.in.relaPlt: this is included if a linker script places .rela.plt inside
1332// .rela.dyn
1333//
1334// DT_RELASZ is the total size of the included sections.
1335static uint64_t addRelaSz(Ctx &ctx, const RelocationBaseSection &relaDyn) {
1336 size_t size = relaDyn.getSize();
1337 if (ctx.in.relaPlt->getParent() == relaDyn.getParent())
1338 size += ctx.in.relaPlt->getSize();
1339 return size;
1340}
1341
1342// A Linker script may assign the RELA relocation sections to the same
1343// output section. When this occurs we cannot just use the OutputSection
1344// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
1345// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1346static uint64_t addPltRelSz(Ctx &ctx) { return ctx.in.relaPlt->getSize(); }
1347
1348// Add remaining entries to complete .dynamic contents.
1349template <class ELFT>
1350std::vector<std::pair<int32_t, uint64_t>>
1351DynamicSection<ELFT>::computeContents() {
1352 elf::Partition &part = getPartition(ctx);
1353 bool isMain = part.name.empty();
1354 std::vector<std::pair<int32_t, uint64_t>> entries;
1355
1356 auto addInt = [&](int32_t tag, uint64_t val) {
1357 entries.emplace_back(args&: tag, args&: val);
1358 };
1359 auto addInSec = [&](int32_t tag, const InputSection &sec) {
1360 entries.emplace_back(args&: tag, args: sec.getVA());
1361 };
1362
1363 for (StringRef s : ctx.arg.filterList)
1364 addInt(DT_FILTER, part.dynStrTab->addString(s));
1365 for (StringRef s : ctx.arg.auxiliaryList)
1366 addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1367
1368 if (!ctx.arg.rpath.empty())
1369 addInt(ctx.arg.enableNewDtags ? DT_RUNPATH : DT_RPATH,
1370 part.dynStrTab->addString(s: ctx.arg.rpath));
1371
1372 for (SharedFile *file : ctx.sharedFiles)
1373 if (file->isNeeded)
1374 addInt(DT_NEEDED, part.dynStrTab->addString(s: file->soName));
1375
1376 if (isMain) {
1377 if (!ctx.arg.soName.empty())
1378 addInt(DT_SONAME, part.dynStrTab->addString(s: ctx.arg.soName));
1379 } else {
1380 if (!ctx.arg.soName.empty())
1381 addInt(DT_NEEDED, part.dynStrTab->addString(s: ctx.arg.soName));
1382 addInt(DT_SONAME, part.dynStrTab->addString(s: part.name));
1383 }
1384
1385 // Set DT_FLAGS and DT_FLAGS_1.
1386 uint32_t dtFlags = 0;
1387 uint32_t dtFlags1 = 0;
1388 if (ctx.arg.bsymbolic == BsymbolicKind::All)
1389 dtFlags |= DF_SYMBOLIC;
1390 if (ctx.arg.zGlobal)
1391 dtFlags1 |= DF_1_GLOBAL;
1392 if (ctx.arg.zInitfirst)
1393 dtFlags1 |= DF_1_INITFIRST;
1394 if (ctx.arg.zInterpose)
1395 dtFlags1 |= DF_1_INTERPOSE;
1396 if (ctx.arg.zNodefaultlib)
1397 dtFlags1 |= DF_1_NODEFLIB;
1398 if (ctx.arg.zNodelete)
1399 dtFlags1 |= DF_1_NODELETE;
1400 if (ctx.arg.zNodlopen)
1401 dtFlags1 |= DF_1_NOOPEN;
1402 if (ctx.arg.pie)
1403 dtFlags1 |= DF_1_PIE;
1404 if (ctx.arg.zNow) {
1405 dtFlags |= DF_BIND_NOW;
1406 dtFlags1 |= DF_1_NOW;
1407 }
1408 if (ctx.arg.zOrigin) {
1409 dtFlags |= DF_ORIGIN;
1410 dtFlags1 |= DF_1_ORIGIN;
1411 }
1412 if (!ctx.arg.zText)
1413 dtFlags |= DF_TEXTREL;
1414 if (ctx.hasTlsIe && ctx.arg.shared)
1415 dtFlags |= DF_STATIC_TLS;
1416
1417 if (dtFlags)
1418 addInt(DT_FLAGS, dtFlags);
1419 if (dtFlags1)
1420 addInt(DT_FLAGS_1, dtFlags1);
1421
1422 // DT_DEBUG is a pointer to debug information used by debuggers at runtime. We
1423 // need it for each process, so we don't write it for DSOs. The loader writes
1424 // the pointer into this entry.
1425 //
1426 // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1427 // systems (currently only Fuchsia OS) provide other means to give the
1428 // debugger this information. Such systems may choose make .dynamic read-only.
1429 // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1430 if (!ctx.arg.shared && !ctx.arg.relocatable && !ctx.arg.zRodynamic)
1431 addInt(DT_DEBUG, 0);
1432
1433 if (part.relaDyn->isNeeded()) {
1434 addInSec(part.relaDyn->dynamicTag, *part.relaDyn);
1435 entries.emplace_back(part.relaDyn->sizeDynamicTag,
1436 addRelaSz(ctx, *part.relaDyn));
1437
1438 bool isRela = ctx.arg.isRela;
1439 addInt(isRela ? DT_RELAENT : DT_RELENT,
1440 isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
1441
1442 // MIPS dynamic loader does not support RELCOUNT tag.
1443 // The problem is in the tight relation between dynamic
1444 // relocations and GOT. So do not emit this tag on MIPS.
1445 if (ctx.arg.emachine != EM_MIPS) {
1446 size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1447 if (ctx.arg.zCombreloc && numRelativeRels)
1448 addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels);
1449 }
1450 }
1451 if (part.relrDyn && part.relrDyn->getParent() &&
1452 !part.relrDyn->relocs.empty()) {
1453 addInSec(ctx.arg.useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1454 *part.relrDyn);
1455 addInt(ctx.arg.useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1456 part.relrDyn->getParent()->size);
1457 addInt(ctx.arg.useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1458 sizeof(Elf_Relr));
1459 }
1460 if (part.relrAuthDyn && part.relrAuthDyn->getParent() &&
1461 !part.relrAuthDyn->relocs.empty()) {
1462 addInSec(DT_AARCH64_AUTH_RELR, *part.relrAuthDyn);
1463 addInt(DT_AARCH64_AUTH_RELRSZ, part.relrAuthDyn->getParent()->size);
1464 addInt(DT_AARCH64_AUTH_RELRENT, sizeof(Elf_Relr));
1465 }
1466 if (isMain && ctx.in.relaPlt->isNeeded()) {
1467 addInSec(DT_JMPREL, *ctx.in.relaPlt);
1468 entries.emplace_back(DT_PLTRELSZ, addPltRelSz(ctx));
1469 switch (ctx.arg.emachine) {
1470 case EM_MIPS:
1471 addInSec(DT_MIPS_PLTGOT, *ctx.in.gotPlt);
1472 break;
1473 case EM_S390:
1474 addInSec(DT_PLTGOT, *ctx.in.got);
1475 break;
1476 case EM_SPARCV9:
1477 addInSec(DT_PLTGOT, *ctx.in.plt);
1478 break;
1479 case EM_AARCH64:
1480 if (llvm::find_if(ctx.in.relaPlt->relocs, [&ctx = ctx](
1481 const DynamicReloc &r) {
1482 return r.type == ctx.target->pltRel &&
1483 r.sym->stOther & STO_AARCH64_VARIANT_PCS;
1484 }) != ctx.in.relaPlt->relocs.end())
1485 addInt(DT_AARCH64_VARIANT_PCS, 0);
1486 addInSec(DT_PLTGOT, *ctx.in.gotPlt);
1487 break;
1488 case EM_RISCV:
1489 if (llvm::any_of(ctx.in.relaPlt->relocs, [&ctx = ctx](
1490 const DynamicReloc &r) {
1491 return r.type == ctx.target->pltRel &&
1492 (r.sym->stOther & STO_RISCV_VARIANT_CC);
1493 }))
1494 addInt(DT_RISCV_VARIANT_CC, 0);
1495 [[fallthrough]];
1496 default:
1497 addInSec(DT_PLTGOT, *ctx.in.gotPlt);
1498 break;
1499 }
1500 addInt(DT_PLTREL, ctx.arg.isRela ? DT_RELA : DT_REL);
1501 }
1502
1503 if (ctx.arg.emachine == EM_AARCH64) {
1504 if (ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1505 addInt(DT_AARCH64_BTI_PLT, 0);
1506 if (ctx.arg.zPacPlt)
1507 addInt(DT_AARCH64_PAC_PLT, 0);
1508
1509 if (hasMemtag(ctx)) {
1510 addInt(DT_AARCH64_MEMTAG_MODE, ctx.arg.androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
1511 addInt(DT_AARCH64_MEMTAG_HEAP, ctx.arg.androidMemtagHeap);
1512 addInt(DT_AARCH64_MEMTAG_STACK, ctx.arg.androidMemtagStack);
1513 if (ctx.mainPart->memtagGlobalDescriptors->isNeeded()) {
1514 addInSec(DT_AARCH64_MEMTAG_GLOBALS,
1515 *ctx.mainPart->memtagGlobalDescriptors);
1516 addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,
1517 ctx.mainPart->memtagGlobalDescriptors->getSize());
1518 }
1519 }
1520 }
1521
1522 addInSec(DT_SYMTAB, *part.dynSymTab);
1523 addInt(DT_SYMENT, sizeof(Elf_Sym));
1524 addInSec(DT_STRTAB, *part.dynStrTab);
1525 addInt(DT_STRSZ, part.dynStrTab->getSize());
1526 if (!ctx.arg.zText)
1527 addInt(DT_TEXTREL, 0);
1528 if (part.gnuHashTab && part.gnuHashTab->getParent())
1529 addInSec(DT_GNU_HASH, *part.gnuHashTab);
1530 if (part.hashTab && part.hashTab->getParent())
1531 addInSec(DT_HASH, *part.hashTab);
1532
1533 if (isMain) {
1534 if (ctx.out.preinitArray) {
1535 addInt(DT_PREINIT_ARRAY, ctx.out.preinitArray->addr);
1536 addInt(DT_PREINIT_ARRAYSZ, ctx.out.preinitArray->size);
1537 }
1538 if (ctx.out.initArray) {
1539 addInt(DT_INIT_ARRAY, ctx.out.initArray->addr);
1540 addInt(DT_INIT_ARRAYSZ, ctx.out.initArray->size);
1541 }
1542 if (ctx.out.finiArray) {
1543 addInt(DT_FINI_ARRAY, ctx.out.finiArray->addr);
1544 addInt(DT_FINI_ARRAYSZ, ctx.out.finiArray->size);
1545 }
1546
1547 if (Symbol *b = ctx.symtab->find(name: ctx.arg.init))
1548 if (b->isDefined())
1549 addInt(DT_INIT, b->getVA(ctx));
1550 if (Symbol *b = ctx.symtab->find(name: ctx.arg.fini))
1551 if (b->isDefined())
1552 addInt(DT_FINI, b->getVA(ctx));
1553 }
1554
1555 if (part.verSym && part.verSym->isNeeded())
1556 addInSec(DT_VERSYM, *part.verSym);
1557 if (part.verDef && part.verDef->isLive()) {
1558 addInSec(DT_VERDEF, *part.verDef);
1559 addInt(DT_VERDEFNUM, getVerDefNum(ctx));
1560 }
1561 if (part.verNeed && part.verNeed->isNeeded()) {
1562 addInSec(DT_VERNEED, *part.verNeed);
1563 unsigned needNum = 0;
1564 for (SharedFile *f : ctx.sharedFiles)
1565 if (!f->verneedInfo.empty())
1566 ++needNum;
1567 addInt(DT_VERNEEDNUM, needNum);
1568 }
1569
1570 if (ctx.arg.emachine == EM_MIPS) {
1571 addInt(DT_MIPS_RLD_VERSION, 1);
1572 addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1573 addInt(DT_MIPS_BASE_ADDRESS, ctx.target->getImageBase());
1574 addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1575 addInt(DT_MIPS_LOCAL_GOTNO, ctx.in.mipsGot->getLocalEntriesNum());
1576
1577 if (const Symbol *b = ctx.in.mipsGot->getFirstGlobalEntry())
1578 addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1579 else
1580 addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1581 addInSec(DT_PLTGOT, *ctx.in.mipsGot);
1582 if (ctx.in.mipsRldMap) {
1583 if (!ctx.arg.pie)
1584 addInSec(DT_MIPS_RLD_MAP, *ctx.in.mipsRldMap);
1585 // Store the offset to the .rld_map section
1586 // relative to the address of the tag.
1587 addInt(DT_MIPS_RLD_MAP_REL,
1588 ctx.in.mipsRldMap->getVA() - (getVA() + entries.size() * entsize));
1589 }
1590 }
1591
1592 // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1593 // glibc assumes the old-style BSS PLT layout which we don't support.
1594 if (ctx.arg.emachine == EM_PPC)
1595 addInSec(DT_PPC_GOT, *ctx.in.got);
1596
1597 // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1598 if (ctx.arg.emachine == EM_PPC64 && ctx.in.plt->isNeeded()) {
1599 // The Glink tag points to 32 bytes before the first lazy symbol resolution
1600 // stub, which starts directly after the header.
1601 addInt(DT_PPC64_GLINK,
1602 ctx.in.plt->getVA() + ctx.target->pltHeaderSize - 32);
1603 }
1604
1605 if (ctx.arg.emachine == EM_PPC64)
1606 addInt(DT_PPC64_OPT, ctx.target->ppc64DynamicSectionOpt);
1607
1608 addInt(DT_NULL, 0);
1609 return entries;
1610}
1611
1612template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1613 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
1614 getParent()->link = sec->sectionIndex;
1615 this->size = computeContents().size() * this->entsize;
1616}
1617
1618template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1619 auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1620
1621 for (std::pair<int32_t, uint64_t> kv : computeContents()) {
1622 p->d_tag = kv.first;
1623 p->d_un.d_val = kv.second;
1624 ++p;
1625 }
1626}
1627
1628uint64_t DynamicReloc::getOffset() const {
1629 return inputSec->getVA(offset: offsetInSec);
1630}
1631
1632int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
1633 assert(!isFinal && "addend already computed");
1634 uint64_t ca = inputSec->getRelocTargetVA(
1635 ctx, r: Relocation{.expr: expr, .type: type, .offset: 0, .addend: addend, .sym: sym}, p: getOffset());
1636 return ctx.arg.is64 ? ca : SignExtend64<32>(x: ca);
1637}
1638
1639uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
1640 if (!needsDynSymIndex())
1641 return 0;
1642
1643 size_t index = symTab->getSymbolIndex(sym: *sym);
1644 assert((index != 0 ||
1645 (type != symTab->ctx.target->gotRel &&
1646 type != symTab->ctx.target->pltRel) ||
1647 !symTab->ctx.mainPart->dynSymTab->getParent()) &&
1648 "GOT or PLT relocation must refer to symbol in dynamic symbol table");
1649 return index;
1650}
1651
1652RelocationBaseSection::RelocationBaseSection(Ctx &ctx, StringRef name,
1653 uint32_t type, int32_t dynamicTag,
1654 int32_t sizeDynamicTag,
1655 bool combreloc,
1656 unsigned concurrency)
1657 : SyntheticSection(ctx, name, type, SHF_ALLOC, ctx.arg.wordsize),
1658 dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag),
1659 relocsVec(concurrency), combreloc(combreloc) {}
1660
1661void RelocationBaseSection::addSymbolReloc(
1662 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1663 int64_t addend, std::optional<RelType> addendRelType) {
1664 addReloc(isAgainstSymbol: true, dynType, sec&: isec, offsetInSec, sym, addend, expr: R_ADDEND,
1665 addendRelType: addendRelType ? *addendRelType : ctx.target->noneRel);
1666}
1667
1668void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
1669 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1670 RelType addendRelType) {
1671 // No need to write an addend to the section for preemptible symbols.
1672 if (sym.isPreemptible)
1673 addReloc(reloc: {dynType, &isec, offsetInSec, true, sym, 0, R_ADDEND});
1674 else
1675 addReloc(isAgainstSymbol: false, dynType, sec&: isec, offsetInSec, sym, addend: 0, expr: R_ABS, addendRelType);
1676}
1677
1678void RelocationBaseSection::mergeRels() {
1679 size_t newSize = relocs.size();
1680 for (const auto &v : relocsVec)
1681 newSize += v.size();
1682 relocs.reserve(N: newSize);
1683 for (const auto &v : relocsVec)
1684 llvm::append_range(C&: relocs, R: v);
1685 relocsVec.clear();
1686}
1687
1688void RelocationBaseSection::partitionRels() {
1689 if (!combreloc)
1690 return;
1691 const RelType relativeRel = ctx.target->relativeRel;
1692 numRelativeRelocs =
1693 std::stable_partition(first: relocs.begin(), last: relocs.end(),
1694 pred: [=](auto &r) { return r.type == relativeRel; }) -
1695 relocs.begin();
1696}
1697
1698void RelocationBaseSection::finalizeContents() {
1699 mergeRels();
1700 // Compute DT_RELACOUNT to be used by part.dynamic.
1701 partitionRels();
1702 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1703
1704 // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1705 // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1706 // case.
1707 if (symTab && symTab->getParent())
1708 getParent()->link = symTab->getParent()->sectionIndex;
1709 else
1710 getParent()->link = 0;
1711
1712 if (ctx.in.relaPlt.get() == this && ctx.in.gotPlt->getParent()) {
1713 getParent()->flags |= ELF::SHF_INFO_LINK;
1714 getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
1715 }
1716}
1717
1718void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
1719 r_offset = getOffset();
1720 r_sym = getSymIndex(symTab: symt);
1721 addend = computeAddend(ctx);
1722 isFinal = true; // Catch errors
1723}
1724
1725void RelocationBaseSection::computeRels() {
1726 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1727 parallelForEach(R&: relocs, Fn: [&ctx = ctx, symTab](DynamicReloc &rel) {
1728 rel.finalize(ctx, symt: symTab);
1729 });
1730
1731 auto irelative = std::stable_partition(
1732 first: relocs.begin() + numRelativeRelocs, last: relocs.end(),
1733 pred: [t = ctx.target->iRelativeRel](auto &r) { return r.type != t; });
1734
1735 // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1736 // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1737 // is to make results easier to read.
1738 if (combreloc) {
1739 auto nonRelative = relocs.begin() + numRelativeRelocs;
1740 parallelSort(Start: relocs.begin(), End: nonRelative,
1741 Comp: [&](auto &a, auto &b) { return a.r_offset < b.r_offset; });
1742 // Non-relative relocations are few, so don't bother with parallelSort.
1743 llvm::sort(Start: nonRelative, End: irelative, Comp: [&](auto &a, auto &b) {
1744 return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
1745 });
1746 }
1747}
1748
1749template <class ELFT>
1750RelocationSection<ELFT>::RelocationSection(Ctx &ctx, StringRef name,
1751 bool combreloc, unsigned concurrency)
1752 : RelocationBaseSection(ctx, name, ctx.arg.isRela ? SHT_RELA : SHT_REL,
1753 ctx.arg.isRela ? DT_RELA : DT_REL,
1754 ctx.arg.isRela ? DT_RELASZ : DT_RELSZ, combreloc,
1755 concurrency) {
1756 this->entsize = ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1757}
1758
1759template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1760 computeRels();
1761 for (const DynamicReloc &rel : relocs) {
1762 auto *p = reinterpret_cast<Elf_Rela *>(buf);
1763 p->r_offset = rel.r_offset;
1764 p->setSymbolAndType(rel.r_sym, rel.type, ctx.arg.isMips64EL);
1765 if (ctx.arg.isRela)
1766 p->r_addend = rel.addend;
1767 buf += ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1768 }
1769}
1770
1771RelrBaseSection::RelrBaseSection(Ctx &ctx, unsigned concurrency,
1772 bool isAArch64Auth)
1773 : SyntheticSection(
1774 ctx, isAArch64Auth ? ".relr.auth.dyn" : ".relr.dyn",
1775 isAArch64Auth
1776 ? SHT_AARCH64_AUTH_RELR
1777 : (ctx.arg.useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR),
1778 SHF_ALLOC, ctx.arg.wordsize),
1779 relocsVec(concurrency) {}
1780
1781void RelrBaseSection::mergeRels() {
1782 size_t newSize = relocs.size();
1783 for (const auto &v : relocsVec)
1784 newSize += v.size();
1785 relocs.reserve(N: newSize);
1786 for (const auto &v : relocsVec)
1787 llvm::append_range(C&: relocs, R: v);
1788 relocsVec.clear();
1789}
1790
1791void RelrBaseSection::finalizeContents() { mergeRels(); }
1792
1793template <class ELFT>
1794AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1795 Ctx &ctx, StringRef name, unsigned concurrency)
1796 : RelocationBaseSection(
1797 ctx, name, ctx.arg.isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1798 ctx.arg.isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1799 ctx.arg.isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ,
1800 /*combreloc=*/false, concurrency) {
1801 this->entsize = 1;
1802}
1803
1804template <class ELFT>
1805bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
1806 // This function computes the contents of an Android-format packed relocation
1807 // section.
1808 //
1809 // This format compresses relocations by using relocation groups to factor out
1810 // fields that are common between relocations and storing deltas from previous
1811 // relocations in SLEB128 format (which has a short representation for small
1812 // numbers). A good example of a relocation type with common fields is
1813 // R_*_RELATIVE, which is normally used to represent function pointers in
1814 // vtables. In the REL format, each relative relocation has the same r_info
1815 // field, and is only different from other relative relocations in terms of
1816 // the r_offset field. By sorting relocations by offset, grouping them by
1817 // r_info and representing each relocation with only the delta from the
1818 // previous offset, each 8-byte relocation can be compressed to as little as 1
1819 // byte (or less with run-length encoding). This relocation packer was able to
1820 // reduce the size of the relocation section in an Android Chromium DSO from
1821 // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1822 //
1823 // A relocation section consists of a header containing the literal bytes
1824 // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1825 // elements are the total number of relocations in the section and an initial
1826 // r_offset value. The remaining elements define a sequence of relocation
1827 // groups. Each relocation group starts with a header consisting of the
1828 // following elements:
1829 //
1830 // - the number of relocations in the relocation group
1831 // - flags for the relocation group
1832 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1833 // for each relocation in the group.
1834 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1835 // field for each relocation in the group.
1836 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1837 // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1838 // each relocation in the group.
1839 //
1840 // Following the relocation group header are descriptions of each of the
1841 // relocations in the group. They consist of the following elements:
1842 //
1843 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1844 // delta for this relocation.
1845 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1846 // field for this relocation.
1847 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1848 // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1849 // this relocation.
1850
1851 size_t oldSize = relocData.size();
1852
1853 relocData = {'A', 'P', 'S', '2'};
1854 raw_svector_ostream os(relocData);
1855 auto add = [&](int64_t v) { encodeSLEB128(Value: v, OS&: os); };
1856
1857 // The format header includes the number of relocations and the initial
1858 // offset (we set this to zero because the first relocation group will
1859 // perform the initial adjustment).
1860 add(relocs.size());
1861 add(0);
1862
1863 std::vector<Elf_Rela> relatives, nonRelatives;
1864
1865 for (const DynamicReloc &rel : relocs) {
1866 Elf_Rela r;
1867 r.r_offset = rel.getOffset();
1868 r.setSymbolAndType(rel.getSymIndex(symTab: getPartition(ctx).dynSymTab.get()),
1869 rel.type, false);
1870 r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
1871
1872 if (r.getType(ctx.arg.isMips64EL) == ctx.target->relativeRel)
1873 relatives.push_back(r);
1874 else
1875 nonRelatives.push_back(r);
1876 }
1877
1878 llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1879 return a.r_offset < b.r_offset;
1880 });
1881
1882 // Try to find groups of relative relocations which are spaced one word
1883 // apart from one another. These generally correspond to vtable entries. The
1884 // format allows these groups to be encoded using a sort of run-length
1885 // encoding, but each group will cost 7 bytes in addition to the offset from
1886 // the previous group, so it is only profitable to do this for groups of
1887 // size 8 or larger.
1888 std::vector<Elf_Rela> ungroupedRelatives;
1889 std::vector<std::vector<Elf_Rela>> relativeGroups;
1890 for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1891 std::vector<Elf_Rela> group;
1892 do {
1893 group.push_back(*i++);
1894 } while (i != e && (i - 1)->r_offset + ctx.arg.wordsize == i->r_offset);
1895
1896 if (group.size() < 8)
1897 ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1898 group.end());
1899 else
1900 relativeGroups.emplace_back(std::move(group));
1901 }
1902
1903 // For non-relative relocations, we would like to:
1904 // 1. Have relocations with the same symbol offset to be consecutive, so
1905 // that the runtime linker can speed-up symbol lookup by implementing an
1906 // 1-entry cache.
1907 // 2. Group relocations by r_info to reduce the size of the relocation
1908 // section.
1909 // Since the symbol offset is the high bits in r_info, sorting by r_info
1910 // allows us to do both.
1911 //
1912 // For Rela, we also want to sort by r_addend when r_info is the same. This
1913 // enables us to group by r_addend as well.
1914 llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1915 return std::tie(a.r_info, a.r_addend, a.r_offset) <
1916 std::tie(b.r_info, b.r_addend, b.r_offset);
1917 });
1918
1919 // Group relocations with the same r_info. Note that each group emits a group
1920 // header and that may make the relocation section larger. It is hard to
1921 // estimate the size of a group header as the encoded size of that varies
1922 // based on r_info. However, we can approximate this trade-off by the number
1923 // of values encoded. Each group header contains 3 values, and each relocation
1924 // in a group encodes one less value, as compared to when it is not grouped.
1925 // Therefore, we only group relocations if there are 3 or more of them with
1926 // the same r_info.
1927 //
1928 // For Rela, the addend for most non-relative relocations is zero, and thus we
1929 // can usually get a smaller relocation section if we group relocations with 0
1930 // addend as well.
1931 std::vector<Elf_Rela> ungroupedNonRelatives;
1932 std::vector<std::vector<Elf_Rela>> nonRelativeGroups;
1933 for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) {
1934 auto j = i + 1;
1935 while (j != e && i->r_info == j->r_info &&
1936 (!ctx.arg.isRela || i->r_addend == j->r_addend))
1937 ++j;
1938 if (j - i < 3 || (ctx.arg.isRela && i->r_addend != 0))
1939 ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j);
1940 else
1941 nonRelativeGroups.emplace_back(i, j);
1942 i = j;
1943 }
1944
1945 // Sort ungrouped relocations by offset to minimize the encoded length.
1946 llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1947 return a.r_offset < b.r_offset;
1948 });
1949
1950 unsigned hasAddendIfRela =
1951 ctx.arg.isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0;
1952
1953 uint64_t offset = 0;
1954 uint64_t addend = 0;
1955
1956 // Emit the run-length encoding for the groups of adjacent relative
1957 // relocations. Each group is represented using two groups in the packed
1958 // format. The first is used to set the current offset to the start of the
1959 // group (and also encodes the first relocation), and the second encodes the
1960 // remaining relocations.
1961 for (std::vector<Elf_Rela> &g : relativeGroups) {
1962 // The first relocation in the group.
1963 add(1);
1964 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1965 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1966 add(g[0].r_offset - offset);
1967 add(ctx.target->relativeRel);
1968 if (ctx.arg.isRela) {
1969 add(g[0].r_addend - addend);
1970 addend = g[0].r_addend;
1971 }
1972
1973 // The remaining relocations.
1974 add(g.size() - 1);
1975 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1976 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1977 add(ctx.arg.wordsize);
1978 add(ctx.target->relativeRel);
1979 if (ctx.arg.isRela) {
1980 for (const auto &i : llvm::drop_begin(g)) {
1981 add(i.r_addend - addend);
1982 addend = i.r_addend;
1983 }
1984 }
1985
1986 offset = g.back().r_offset;
1987 }
1988
1989 // Now the ungrouped relatives.
1990 if (!ungroupedRelatives.empty()) {
1991 add(ungroupedRelatives.size());
1992 add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1993 add(ctx.target->relativeRel);
1994 for (Elf_Rela &r : ungroupedRelatives) {
1995 add(r.r_offset - offset);
1996 offset = r.r_offset;
1997 if (ctx.arg.isRela) {
1998 add(r.r_addend - addend);
1999 addend = r.r_addend;
2000 }
2001 }
2002 }
2003
2004 // Grouped non-relatives.
2005 for (ArrayRef<Elf_Rela> g : nonRelativeGroups) {
2006 add(g.size());
2007 add(RELOCATION_GROUPED_BY_INFO_FLAG);
2008 add(g[0].r_info);
2009 for (const Elf_Rela &r : g) {
2010 add(r.r_offset - offset);
2011 offset = r.r_offset;
2012 }
2013 addend = 0;
2014 }
2015
2016 // Finally the ungrouped non-relative relocations.
2017 if (!ungroupedNonRelatives.empty()) {
2018 add(ungroupedNonRelatives.size());
2019 add(hasAddendIfRela);
2020 for (Elf_Rela &r : ungroupedNonRelatives) {
2021 add(r.r_offset - offset);
2022 offset = r.r_offset;
2023 add(r.r_info);
2024 if (ctx.arg.isRela) {
2025 add(r.r_addend - addend);
2026 addend = r.r_addend;
2027 }
2028 }
2029 }
2030
2031 // Don't allow the section to shrink; otherwise the size of the section can
2032 // oscillate infinitely.
2033 if (relocData.size() < oldSize)
2034 relocData.append(NumInputs: oldSize - relocData.size(), Elt: 0);
2035
2036 // Returns whether the section size changed. We need to keep recomputing both
2037 // section layout and the contents of this section until the size converges
2038 // because changing this section's size can affect section layout, which in
2039 // turn can affect the sizes of the LEB-encoded integers stored in this
2040 // section.
2041 return relocData.size() != oldSize;
2042}
2043
2044template <class ELFT>
2045RelrSection<ELFT>::RelrSection(Ctx &ctx, unsigned concurrency,
2046 bool isAArch64Auth)
2047 : RelrBaseSection(ctx, concurrency, isAArch64Auth) {
2048 this->entsize = ctx.arg.wordsize;
2049}
2050
2051template <class ELFT> bool RelrSection<ELFT>::updateAllocSize(Ctx &ctx) {
2052 // This function computes the contents of an SHT_RELR packed relocation
2053 // section.
2054 //
2055 // Proposal for adding SHT_RELR sections to generic-abi is here:
2056 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
2057 //
2058 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
2059 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
2060 //
2061 // i.e. start with an address, followed by any number of bitmaps. The address
2062 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
2063 // relocations each, at subsequent offsets following the last address entry.
2064 //
2065 // The bitmap entries must have 1 in the least significant bit. The assumption
2066 // here is that an address cannot have 1 in lsb. Odd addresses are not
2067 // supported.
2068 //
2069 // Excluding the least significant bit in the bitmap, each non-zero bit in
2070 // the bitmap represents a relocation to be applied to a corresponding machine
2071 // word that follows the base address word. The second least significant bit
2072 // represents the machine word immediately following the initial address, and
2073 // each bit that follows represents the next word, in linear order. As such,
2074 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
2075 // 63 relocations in a 64-bit object.
2076 //
2077 // This encoding has a couple of interesting properties:
2078 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
2079 // even means address, odd means bitmap.
2080 // 2. Just a simple list of addresses is a valid encoding.
2081
2082 size_t oldSize = relrRelocs.size();
2083 relrRelocs.clear();
2084
2085 const size_t wordsize = sizeof(typename ELFT::uint);
2086
2087 // Number of bits to use for the relocation offsets bitmap.
2088 // Must be either 63 or 31.
2089 const size_t nBits = wordsize * 8 - 1;
2090
2091 // Get offsets for all relative relocations and sort them.
2092 std::unique_ptr<uint64_t[]> offsets(new uint64_t[relocs.size()]);
2093 for (auto [i, r] : llvm::enumerate(relocs))
2094 offsets[i] = r.getOffset();
2095 llvm::sort(offsets.get(), offsets.get() + relocs.size());
2096
2097 // For each leading relocation, find following ones that can be folded
2098 // as a bitmap and fold them.
2099 for (size_t i = 0, e = relocs.size(); i != e;) {
2100 // Add a leading relocation.
2101 relrRelocs.push_back(Elf_Relr(offsets[i]));
2102 uint64_t base = offsets[i] + wordsize;
2103 ++i;
2104
2105 // Find foldable relocations to construct bitmaps.
2106 for (;;) {
2107 uint64_t bitmap = 0;
2108 for (; i != e; ++i) {
2109 uint64_t d = offsets[i] - base;
2110 if (d >= nBits * wordsize || d % wordsize)
2111 break;
2112 bitmap |= uint64_t(1) << (d / wordsize);
2113 }
2114 if (!bitmap)
2115 break;
2116 relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
2117 base += nBits * wordsize;
2118 }
2119 }
2120
2121 // Don't allow the section to shrink; otherwise the size of the section can
2122 // oscillate infinitely. Trailing 1s do not decode to more relocations.
2123 if (relrRelocs.size() < oldSize) {
2124 Log(ctx) << ".relr.dyn needs " << (oldSize - relrRelocs.size())
2125 << " padding word(s)";
2126 relrRelocs.resize(oldSize, Elf_Relr(1));
2127 }
2128
2129 return relrRelocs.size() != oldSize;
2130}
2131
2132SymbolTableBaseSection::SymbolTableBaseSection(Ctx &ctx,
2133 StringTableSection &strTabSec)
2134 : SyntheticSection(ctx, strTabSec.isDynamic() ? ".dynsym" : ".symtab",
2135 strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
2136 strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
2137 ctx.arg.wordsize),
2138 strTabSec(strTabSec) {}
2139
2140// Orders symbols according to their positions in the GOT,
2141// in compliance with MIPS ABI rules.
2142// See "Global Offset Table" in Chapter 5 in the following document
2143// for detailed description:
2144// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2145static void sortMipsSymbols(Ctx &ctx, SmallVector<SymbolTableEntry, 0> &syms) {
2146 llvm::stable_sort(Range&: syms,
2147 C: [&](const SymbolTableEntry &l, const SymbolTableEntry &r) {
2148 // Sort entries related to non-local preemptible symbols
2149 // by GOT indexes. All other entries go to the beginning
2150 // of a dynsym in arbitrary order.
2151 if (l.sym->isInGot(ctx) && r.sym->isInGot(ctx))
2152 return l.sym->getGotIdx(ctx) < r.sym->getGotIdx(ctx);
2153 if (!l.sym->isInGot(ctx) && !r.sym->isInGot(ctx))
2154 return false;
2155 return !l.sym->isInGot(ctx);
2156 });
2157}
2158
2159void SymbolTableBaseSection::finalizeContents() {
2160 if (OutputSection *sec = strTabSec.getParent())
2161 getParent()->link = sec->sectionIndex;
2162
2163 if (this->type != SHT_DYNSYM) {
2164 sortSymTabSymbols();
2165 return;
2166 }
2167
2168 // If it is a .dynsym, there should be no local symbols, but we need
2169 // to do a few things for the dynamic linker.
2170
2171 // Section's Info field has the index of the first non-local symbol.
2172 // Because the first symbol entry is a null entry, 1 is the first.
2173 getParent()->info = 1;
2174
2175 if (getPartition(ctx).gnuHashTab) {
2176 // NB: It also sorts Symbols to meet the GNU hash table requirements.
2177 getPartition(ctx).gnuHashTab->addSymbols(symbols);
2178 } else if (ctx.arg.emachine == EM_MIPS) {
2179 sortMipsSymbols(ctx, syms&: symbols);
2180 }
2181
2182 // Only the main partition's dynsym indexes are stored in the symbols
2183 // themselves. All other partitions use a lookup table.
2184 if (this == ctx.mainPart->dynSymTab.get()) {
2185 size_t i = 0;
2186 for (const SymbolTableEntry &s : symbols)
2187 s.sym->dynsymIndex = ++i;
2188 }
2189}
2190
2191// The ELF spec requires that all local symbols precede global symbols, so we
2192// sort symbol entries in this function. (For .dynsym, we don't do that because
2193// symbols for dynamic linking are inherently all globals.)
2194//
2195// Aside from above, we put local symbols in groups starting with the STT_FILE
2196// symbol. That is convenient for purpose of identifying where are local symbols
2197// coming from.
2198void SymbolTableBaseSection::sortSymTabSymbols() {
2199 // Move all local symbols before global symbols.
2200 auto e = std::stable_partition(
2201 first: symbols.begin(), last: symbols.end(),
2202 pred: [](const SymbolTableEntry &s) { return s.sym->isLocal(); });
2203 size_t numLocals = e - symbols.begin();
2204 getParent()->info = numLocals + 1;
2205
2206 // We want to group the local symbols by file. For that we rebuild the local
2207 // part of the symbols vector. We do not need to care about the STT_FILE
2208 // symbols, they are already naturally placed first in each group. That
2209 // happens because STT_FILE is always the first symbol in the object and hence
2210 // precede all other local symbols we add for a file.
2211 MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr;
2212 for (const SymbolTableEntry &s : llvm::make_range(x: symbols.begin(), y: e))
2213 arr[s.sym->file].push_back(Elt: s);
2214
2215 auto i = symbols.begin();
2216 for (auto &p : arr)
2217 for (SymbolTableEntry &entry : p.second)
2218 *i++ = entry;
2219}
2220
2221void SymbolTableBaseSection::addSymbol(Symbol *b) {
2222 // Adding a local symbol to a .dynsym is a bug.
2223 assert(this->type != SHT_DYNSYM || !b->isLocal());
2224 symbols.push_back(Elt: {.sym: b, .strTabOffset: strTabSec.addString(s: b->getName(), hashIt: false)});
2225}
2226
2227size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {
2228 if (this == ctx.mainPart->dynSymTab.get())
2229 return sym.dynsymIndex;
2230
2231 // Initializes symbol lookup tables lazily. This is used only for -r,
2232 // --emit-relocs and dynsyms in partitions other than the main one.
2233 llvm::call_once(flag&: onceFlag, F: [&] {
2234 symbolIndexMap.reserve(NumEntries: symbols.size());
2235 size_t i = 0;
2236 for (const SymbolTableEntry &e : symbols) {
2237 if (e.sym->type == STT_SECTION)
2238 sectionIndexMap[e.sym->getOutputSection()] = ++i;
2239 else
2240 symbolIndexMap[e.sym] = ++i;
2241 }
2242 });
2243
2244 // Section symbols are mapped based on their output sections
2245 // to maintain their semantics.
2246 if (sym.type == STT_SECTION)
2247 return sectionIndexMap.lookup(Val: sym.getOutputSection());
2248 return symbolIndexMap.lookup(Val: &sym);
2249}
2250
2251template <class ELFT>
2252SymbolTableSection<ELFT>::SymbolTableSection(Ctx &ctx,
2253 StringTableSection &strTabSec)
2254 : SymbolTableBaseSection(ctx, strTabSec) {
2255 this->entsize = sizeof(Elf_Sym);
2256}
2257
2258static BssSection *getCommonSec(bool relocatable, Symbol *sym) {
2259 if (relocatable)
2260 if (auto *d = dyn_cast<Defined>(Val: sym))
2261 return dyn_cast_or_null<BssSection>(Val: d->section);
2262 return nullptr;
2263}
2264
2265static uint32_t getSymSectionIndex(Symbol *sym) {
2266 assert(!(sym->hasFlag(NEEDS_COPY) && sym->isObject()));
2267 if (!isa<Defined>(Val: sym) || sym->hasFlag(bit: NEEDS_COPY))
2268 return SHN_UNDEF;
2269 if (const OutputSection *os = sym->getOutputSection())
2270 return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX
2271 : os->sectionIndex;
2272 return SHN_ABS;
2273}
2274
2275// Write the internal symbol table contents to the output symbol table.
2276template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2277 // The first entry is a null entry as per the ELF spec.
2278 buf += sizeof(Elf_Sym);
2279
2280 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2281 bool relocatable = ctx.arg.relocatable;
2282 for (SymbolTableEntry &ent : symbols) {
2283 Symbol *sym = ent.sym;
2284 bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
2285
2286 // Set st_name, st_info and st_other.
2287 eSym->st_name = ent.strTabOffset;
2288 eSym->setBindingAndType(sym->binding, sym->type);
2289 eSym->st_other = sym->stOther;
2290
2291 if (BssSection *commonSec = getCommonSec(relocatable, sym)) {
2292 // When -r is specified, a COMMON symbol is not allocated. Its st_shndx
2293 // holds SHN_COMMON and st_value holds the alignment.
2294 eSym->st_shndx = SHN_COMMON;
2295 eSym->st_value = commonSec->addralign;
2296 eSym->st_size = cast<Defined>(Val: sym)->size;
2297 } else {
2298 const uint32_t shndx = getSymSectionIndex(sym);
2299 if (isDefinedHere) {
2300 eSym->st_shndx = shndx;
2301 eSym->st_value = sym->getVA(ctx);
2302 // Copy symbol size if it is a defined symbol. st_size is not
2303 // significant for undefined symbols, so whether copying it or not is up
2304 // to us if that's the case. We'll leave it as zero because by not
2305 // setting a value, we can get the exact same outputs for two sets of
2306 // input files that differ only in undefined symbol size in DSOs.
2307 eSym->st_size = shndx != SHN_UNDEF ? cast<Defined>(Val: sym)->size : 0;
2308 } else {
2309 eSym->st_shndx = 0;
2310 eSym->st_value = 0;
2311 eSym->st_size = 0;
2312 }
2313 }
2314
2315 ++eSym;
2316 }
2317
2318 // On MIPS we need to mark symbol which has a PLT entry and requires
2319 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2320 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2321 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2322 if (ctx.arg.emachine == EM_MIPS) {
2323 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2324
2325 for (SymbolTableEntry &ent : symbols) {
2326 Symbol *sym = ent.sym;
2327 if (sym->isInPlt(ctx) && sym->hasFlag(bit: NEEDS_COPY))
2328 eSym->st_other |= STO_MIPS_PLT;
2329 if (isMicroMips(ctx)) {
2330 // We already set the less-significant bit for symbols
2331 // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2332 // records. That allows us to distinguish such symbols in
2333 // the `MIPS<ELFT>::relocate()` routine. Now we should
2334 // clear that bit for non-dynamic symbol table, so tools
2335 // like `objdump` will be able to deal with a correct
2336 // symbol position.
2337 if (sym->isDefined() &&
2338 ((sym->stOther & STO_MIPS_MICROMIPS) || sym->hasFlag(bit: NEEDS_COPY))) {
2339 if (!strTabSec.isDynamic())
2340 eSym->st_value &= ~1;
2341 eSym->st_other |= STO_MIPS_MICROMIPS;
2342 }
2343 }
2344 if (ctx.arg.relocatable)
2345 if (auto *d = dyn_cast<Defined>(Val: sym))
2346 if (isMipsPIC<ELFT>(d))
2347 eSym->st_other |= STO_MIPS_PIC;
2348 ++eSym;
2349 }
2350 }
2351}
2352
2353SymtabShndxSection::SymtabShndxSection(Ctx &ctx)
2354 : SyntheticSection(ctx, ".symtab_shndx", SHT_SYMTAB_SHNDX, 0, 4) {
2355 this->entsize = 4;
2356}
2357
2358void SymtabShndxSection::writeTo(uint8_t *buf) {
2359 // We write an array of 32 bit values, where each value has 1:1 association
2360 // with an entry in ctx.in.symTab if the corresponding entry contains
2361 // SHN_XINDEX, we need to write actual index, otherwise, we must write
2362 // SHN_UNDEF(0).
2363 buf += 4; // Ignore .symtab[0] entry.
2364 bool relocatable = ctx.arg.relocatable;
2365 for (const SymbolTableEntry &entry : ctx.in.symTab->getSymbols()) {
2366 if (!getCommonSec(relocatable, sym: entry.sym) &&
2367 getSymSectionIndex(sym: entry.sym) == SHN_XINDEX)
2368 write32(ctx, p: buf, v: entry.sym->getOutputSection()->sectionIndex);
2369 buf += 4;
2370 }
2371}
2372
2373bool SymtabShndxSection::isNeeded() const {
2374 // SHT_SYMTAB can hold symbols with section indices values up to
2375 // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
2376 // section. Problem is that we reveal the final section indices a bit too
2377 // late, and we do not know them here. For simplicity, we just always create
2378 // a .symtab_shndx section when the amount of output sections is huge.
2379 size_t size = 0;
2380 for (SectionCommand *cmd : ctx.script->sectionCommands)
2381 if (isa<OutputDesc>(Val: cmd))
2382 ++size;
2383 return size >= SHN_LORESERVE;
2384}
2385
2386void SymtabShndxSection::finalizeContents() {
2387 getParent()->link = ctx.in.symTab->getParent()->sectionIndex;
2388}
2389
2390size_t SymtabShndxSection::getSize() const {
2391 return ctx.in.symTab->getNumSymbols() * 4;
2392}
2393
2394// .hash and .gnu.hash sections contain on-disk hash tables that map
2395// symbol names to their dynamic symbol table indices. Their purpose
2396// is to help the dynamic linker resolve symbols quickly. If ELF files
2397// don't have them, the dynamic linker has to do linear search on all
2398// dynamic symbols, which makes programs slower. Therefore, a .hash
2399// section is added to a DSO by default.
2400//
2401// The Unix semantics of resolving dynamic symbols is somewhat expensive.
2402// Each ELF file has a list of DSOs that the ELF file depends on and a
2403// list of dynamic symbols that need to be resolved from any of the
2404// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
2405// where m is the number of DSOs and n is the number of dynamic
2406// symbols. For modern large programs, both m and n are large. So
2407// making each step faster by using hash tables substantially
2408// improves time to load programs.
2409//
2410// (Note that this is not the only way to design the shared library.
2411// For instance, the Windows DLL takes a different approach. On
2412// Windows, each dynamic symbol has a name of DLL from which the symbol
2413// has to be resolved. That makes the cost of symbol resolution O(n).
2414// This disables some hacky techniques you can use on Unix such as
2415// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
2416//
2417// Due to historical reasons, we have two different hash tables, .hash
2418// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
2419// and better version of .hash. .hash is just an on-disk hash table, but
2420// .gnu.hash has a bloom filter in addition to a hash table to skip
2421// DSOs very quickly. If you are sure that your dynamic linker knows
2422// about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a
2423// safe bet is to specify --hash-style=both for backward compatibility.
2424GnuHashTableSection::GnuHashTableSection(Ctx &ctx)
2425 : SyntheticSection(ctx, ".gnu.hash", SHT_GNU_HASH, SHF_ALLOC,
2426 ctx.arg.wordsize) {}
2427
2428void GnuHashTableSection::finalizeContents() {
2429 if (OutputSection *sec = getPartition(ctx).dynSymTab->getParent())
2430 getParent()->link = sec->sectionIndex;
2431
2432 // Computes bloom filter size in word size. We want to allocate 12
2433 // bits for each symbol. It must be a power of two.
2434 if (symbols.empty()) {
2435 maskWords = 1;
2436 } else {
2437 uint64_t numBits = symbols.size() * 12;
2438 maskWords = NextPowerOf2(A: numBits / (ctx.arg.wordsize * 8));
2439 }
2440
2441 size = 16; // Header
2442 size += ctx.arg.wordsize * maskWords; // Bloom filter
2443 size += nBuckets * 4; // Hash buckets
2444 size += symbols.size() * 4; // Hash values
2445}
2446
2447void GnuHashTableSection::writeTo(uint8_t *buf) {
2448 // Write a header.
2449 write32(ctx, p: buf, v: nBuckets);
2450 write32(ctx, p: buf + 4,
2451 v: getPartition(ctx).dynSymTab->getNumSymbols() - symbols.size());
2452 write32(ctx, p: buf + 8, v: maskWords);
2453 write32(ctx, p: buf + 12, v: Shift2);
2454 buf += 16;
2455
2456 // Write the 2-bit bloom filter.
2457 const unsigned c = ctx.arg.is64 ? 64 : 32;
2458 for (const Entry &sym : symbols) {
2459 // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in
2460 // the word using bits [0:5] and [26:31].
2461 size_t i = (sym.hash / c) & (maskWords - 1);
2462 uint64_t val = readUint(ctx, buf: buf + i * ctx.arg.wordsize);
2463 val |= uint64_t(1) << (sym.hash % c);
2464 val |= uint64_t(1) << ((sym.hash >> Shift2) % c);
2465 writeUint(ctx, buf: buf + i * ctx.arg.wordsize, val);
2466 }
2467 buf += ctx.arg.wordsize * maskWords;
2468
2469 // Write the hash table.
2470 uint32_t *buckets = reinterpret_cast<uint32_t *>(buf);
2471 uint32_t oldBucket = -1;
2472 uint32_t *values = buckets + nBuckets;
2473 for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) {
2474 // Write a hash value. It represents a sequence of chains that share the
2475 // same hash modulo value. The last element of each chain is terminated by
2476 // LSB 1.
2477 uint32_t hash = i->hash;
2478 bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx;
2479 hash = isLastInChain ? hash | 1 : hash & ~1;
2480 write32(ctx, p: values++, v: hash);
2481
2482 if (i->bucketIdx == oldBucket)
2483 continue;
2484 // Write a hash bucket. Hash buckets contain indices in the following hash
2485 // value table.
2486 write32(ctx, p: buckets + i->bucketIdx,
2487 v: getPartition(ctx).dynSymTab->getSymbolIndex(sym: *i->sym));
2488 oldBucket = i->bucketIdx;
2489 }
2490}
2491
2492// Add symbols to this symbol hash table. Note that this function
2493// destructively sort a given vector -- which is needed because
2494// GNU-style hash table places some sorting requirements.
2495void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) {
2496 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
2497 // its type correctly.
2498 auto mid =
2499 std::stable_partition(first: v.begin(), last: v.end(), pred: [&](const SymbolTableEntry &s) {
2500 return !s.sym->isDefined() || s.sym->partition != partition;
2501 });
2502
2503 // We chose load factor 4 for the on-disk hash table. For each hash
2504 // collision, the dynamic linker will compare a uint32_t hash value.
2505 // Since the integer comparison is quite fast, we believe we can
2506 // make the load factor even larger. 4 is just a conservative choice.
2507 //
2508 // Note that we don't want to create a zero-sized hash table because
2509 // Android loader as of 2018 doesn't like a .gnu.hash containing such
2510 // table. If that's the case, we create a hash table with one unused
2511 // dummy slot.
2512 nBuckets = std::max<size_t>(a: (v.end() - mid) / 4, b: 1);
2513
2514 if (mid == v.end())
2515 return;
2516
2517 for (SymbolTableEntry &ent : llvm::make_range(x: mid, y: v.end())) {
2518 Symbol *b = ent.sym;
2519 uint32_t hash = hashGnu(Name: b->getName());
2520 uint32_t bucketIdx = hash % nBuckets;
2521 symbols.push_back(Elt: {.sym: b, .strTabOffset: ent.strTabOffset, .hash: hash, .bucketIdx: bucketIdx});
2522 }
2523
2524 llvm::sort(C&: symbols, Comp: [](const Entry &l, const Entry &r) {
2525 return std::tie(args: l.bucketIdx, args: l.strTabOffset) <
2526 std::tie(args: r.bucketIdx, args: r.strTabOffset);
2527 });
2528
2529 v.erase(CS: mid, CE: v.end());
2530 for (const Entry &ent : symbols)
2531 v.push_back(Elt: {.sym: ent.sym, .strTabOffset: ent.strTabOffset});
2532}
2533
2534HashTableSection::HashTableSection(Ctx &ctx)
2535 : SyntheticSection(ctx, ".hash", SHT_HASH, SHF_ALLOC, 4) {
2536 this->entsize = 4;
2537}
2538
2539void HashTableSection::finalizeContents() {
2540 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2541
2542 if (OutputSection *sec = symTab->getParent())
2543 getParent()->link = sec->sectionIndex;
2544
2545 unsigned numEntries = 2; // nbucket and nchain.
2546 numEntries += symTab->getNumSymbols(); // The chain entries.
2547
2548 // Create as many buckets as there are symbols.
2549 numEntries += symTab->getNumSymbols();
2550 this->size = numEntries * 4;
2551}
2552
2553void HashTableSection::writeTo(uint8_t *buf) {
2554 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2555 unsigned numSymbols = symTab->getNumSymbols();
2556
2557 uint32_t *p = reinterpret_cast<uint32_t *>(buf);
2558 write32(ctx, p: p++, v: numSymbols); // nbucket
2559 write32(ctx, p: p++, v: numSymbols); // nchain
2560
2561 uint32_t *buckets = p;
2562 uint32_t *chains = p + numSymbols;
2563
2564 for (const SymbolTableEntry &s : symTab->getSymbols()) {
2565 Symbol *sym = s.sym;
2566 StringRef name = sym->getName();
2567 unsigned i = sym->dynsymIndex;
2568 uint32_t hash = hashSysV(SymbolName: name) % numSymbols;
2569 chains[i] = buckets[hash];
2570 write32(ctx, p: buckets + hash, v: i);
2571 }
2572}
2573
2574PltSection::PltSection(Ctx &ctx)
2575 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2576 16),
2577 headerSize(ctx.target->pltHeaderSize) {
2578 // On AArch64, PLT entries only do loads from the .got.plt section, so the
2579 // .plt section can be marked with the SHF_AARCH64_PURECODE section flag.
2580 if (ctx.arg.emachine == EM_AARCH64)
2581 this->flags |= SHF_AARCH64_PURECODE;
2582
2583 // On PowerPC, this section contains lazy symbol resolvers.
2584 if (ctx.arg.emachine == EM_PPC64) {
2585 name = ".glink";
2586 addralign = 4;
2587 }
2588
2589 // On x86 when IBT is enabled, this section contains the second PLT (lazy
2590 // symbol resolvers).
2591 if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
2592 (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT))
2593 name = ".plt.sec";
2594
2595 // The PLT needs to be writable on SPARC as the dynamic linker will
2596 // modify the instructions in the PLT entries.
2597 if (ctx.arg.emachine == EM_SPARCV9)
2598 this->flags |= SHF_WRITE;
2599}
2600
2601void PltSection::writeTo(uint8_t *buf) {
2602 // At beginning of PLT, we have code to call the dynamic
2603 // linker to resolve dynsyms at runtime. Write such code.
2604 ctx.target->writePltHeader(buf);
2605 size_t off = headerSize;
2606
2607 for (const Symbol *sym : entries) {
2608 ctx.target->writePlt(buf: buf + off, sym: *sym, pltEntryAddr: getVA() + off);
2609 off += ctx.target->pltEntrySize;
2610 }
2611}
2612
2613void PltSection::addEntry(Symbol &sym) {
2614 assert(sym.auxIdx == ctx.symAux.size() - 1);
2615 ctx.symAux.back().pltIdx = entries.size();
2616 entries.push_back(Elt: &sym);
2617}
2618
2619size_t PltSection::getSize() const {
2620 return headerSize + entries.size() * ctx.target->pltEntrySize;
2621}
2622
2623bool PltSection::isNeeded() const {
2624 // For -z retpolineplt, .iplt needs the .plt header.
2625 return !entries.empty() || (ctx.arg.zRetpolineplt && ctx.in.iplt->isNeeded());
2626}
2627
2628// Used by ARM to add mapping symbols in the PLT section, which aid
2629// disassembly.
2630void PltSection::addSymbols() {
2631 ctx.target->addPltHeaderSymbols(isec&: *this);
2632
2633 size_t off = headerSize;
2634 for (size_t i = 0; i < entries.size(); ++i) {
2635 ctx.target->addPltSymbols(isec&: *this, off);
2636 off += ctx.target->pltEntrySize;
2637 }
2638}
2639
2640IpltSection::IpltSection(Ctx &ctx)
2641 : SyntheticSection(ctx, ".iplt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2642 16) {
2643 // On AArch64, PLT entries only do loads from the .got.plt section, so the
2644 // .iplt section can be marked with the SHF_AARCH64_PURECODE section flag.
2645 if (ctx.arg.emachine == EM_AARCH64)
2646 this->flags |= SHF_AARCH64_PURECODE;
2647
2648 if (ctx.arg.emachine == EM_PPC || ctx.arg.emachine == EM_PPC64) {
2649 name = ".glink";
2650 addralign = 4;
2651 }
2652}
2653
2654void IpltSection::writeTo(uint8_t *buf) {
2655 uint32_t off = 0;
2656 for (const Symbol *sym : entries) {
2657 ctx.target->writeIplt(buf: buf + off, sym: *sym, pltEntryAddr: getVA() + off);
2658 off += ctx.target->ipltEntrySize;
2659 }
2660}
2661
2662size_t IpltSection::getSize() const {
2663 return entries.size() * ctx.target->ipltEntrySize;
2664}
2665
2666void IpltSection::addEntry(Symbol &sym) {
2667 assert(sym.auxIdx == ctx.symAux.size() - 1);
2668 ctx.symAux.back().pltIdx = entries.size();
2669 entries.push_back(Elt: &sym);
2670}
2671
2672// ARM uses mapping symbols to aid disassembly.
2673void IpltSection::addSymbols() {
2674 size_t off = 0;
2675 for (size_t i = 0, e = entries.size(); i != e; ++i) {
2676 ctx.target->addPltSymbols(isec&: *this, off);
2677 off += ctx.target->pltEntrySize;
2678 }
2679}
2680
2681PPC32GlinkSection::PPC32GlinkSection(Ctx &ctx) : PltSection(ctx) {
2682 name = ".glink";
2683 addralign = 4;
2684}
2685
2686void PPC32GlinkSection::writeTo(uint8_t *buf) {
2687 writePPC32GlinkSection(ctx, buf, numEntries: entries.size());
2688}
2689
2690size_t PPC32GlinkSection::getSize() const {
2691 return headerSize + entries.size() * ctx.target->pltEntrySize + footerSize;
2692}
2693
2694// This is an x86-only extra PLT section and used only when a security
2695// enhancement feature called CET is enabled. In this comment, I'll explain what
2696// the feature is and why we have two PLT sections if CET is enabled.
2697//
2698// So, what does CET do? CET introduces a new restriction to indirect jump
2699// instructions. CET works this way. Assume that CET is enabled. Then, if you
2700// execute an indirect jump instruction, the processor verifies that a special
2701// "landing pad" instruction (which is actually a repurposed NOP instruction and
2702// now called "endbr32" or "endbr64") is at the jump target. If the jump target
2703// does not start with that instruction, the processor raises an exception
2704// instead of continuing executing code.
2705//
2706// If CET is enabled, the compiler emits endbr to all locations where indirect
2707// jumps may jump to.
2708//
2709// This mechanism makes it extremely hard to transfer the control to a middle of
2710// a function that is not supporsed to be a indirect jump target, preventing
2711// certain types of attacks such as ROP or JOP.
2712//
2713// Note that the processors in the market as of 2019 don't actually support the
2714// feature. Only the spec is available at the moment.
2715//
2716// Now, I'll explain why we have this extra PLT section for CET.
2717//
2718// Since you can indirectly jump to a PLT entry, we have to make PLT entries
2719// start with endbr. The problem is there's no extra space for endbr (which is 4
2720// bytes long), as the PLT entry is only 16 bytes long and all bytes are already
2721// used.
2722//
2723// In order to deal with the issue, we split a PLT entry into two PLT entries.
2724// Remember that each PLT entry contains code to jump to an address read from
2725// .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme,
2726// the former code is written to .plt.sec, and the latter code is written to
2727// .plt.
2728//
2729// Lazy symbol resolution in the 2-PLT scheme works in the usual way, except
2730// that the regular .plt is now called .plt.sec and .plt is repurposed to
2731// contain only code for lazy symbol resolution.
2732//
2733// In other words, this is how the 2-PLT scheme works. Application code is
2734// supposed to jump to .plt.sec to call an external function. Each .plt.sec
2735// entry contains code to read an address from a corresponding .got.plt entry
2736// and jump to that address. Addresses in .got.plt initially point to .plt, so
2737// when an application calls an external function for the first time, the
2738// control is transferred to a function that resolves a symbol name from
2739// external shared object files. That function then rewrites a .got.plt entry
2740// with a resolved address, so that the subsequent function calls directly jump
2741// to a desired location from .plt.sec.
2742//
2743// There is an open question as to whether the 2-PLT scheme was desirable or
2744// not. We could have simply extended the PLT entry size to 32-bytes to
2745// accommodate endbr, and that scheme would have been much simpler than the
2746// 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot
2747// code (.plt.sec) from cold code (.plt). But as far as I know no one proved
2748// that the optimization actually makes a difference.
2749//
2750// That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools
2751// depend on it, so we implement the ABI.
2752IBTPltSection::IBTPltSection(Ctx &ctx)
2753 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2754 16) {}
2755
2756void IBTPltSection::writeTo(uint8_t *buf) {
2757 ctx.target->writeIBTPlt(buf, numEntries: ctx.in.plt->getNumEntries());
2758}
2759
2760size_t IBTPltSection::getSize() const {
2761 // 16 is the header size of .plt.
2762 return 16 + ctx.in.plt->getNumEntries() * ctx.target->pltEntrySize;
2763}
2764
2765bool IBTPltSection::isNeeded() const { return ctx.in.plt->getNumEntries() > 0; }
2766
2767RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
2768 : SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
2769 1) {}
2770
2771PaddingSection::PaddingSection(Ctx &ctx, uint64_t amount, OutputSection *parent)
2772 : SyntheticSection(ctx, ".padding", SHT_PROGBITS, SHF_ALLOC, 1) {
2773 size = amount;
2774 this->parent = parent;
2775}
2776
2777void PaddingSection::writeTo(uint8_t *buf) {
2778 std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
2779 uint8_t *end = buf + size;
2780 for (; buf + 4 <= end; buf += 4)
2781 memcpy(dest: buf, src: &filler[0], n: 4);
2782 memcpy(dest: buf, src: &filler[0], n: end - buf);
2783}
2784
2785// The string hash function for .gdb_index.
2786static uint32_t computeGdbHash(StringRef s) {
2787 uint32_t h = 0;
2788 for (uint8_t c : s)
2789 h = h * 67 + toLower(x: c) - 113;
2790 return h;
2791}
2792
2793// 4-byte alignment ensures that values in the hash lookup table and the name
2794// table are aligned.
2795DebugNamesBaseSection::DebugNamesBaseSection(Ctx &ctx)
2796 : SyntheticSection(ctx, ".debug_names", SHT_PROGBITS, 0, 4) {}
2797
2798// Get the size of the .debug_names section header in bytes for DWARF32:
2799static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {
2800 return /* unit length */ 4 +
2801 /* version */ 2 +
2802 /* padding */ 2 +
2803 /* CU count */ 4 +
2804 /* TU count */ 4 +
2805 /* Foreign TU count */ 4 +
2806 /* Bucket Count */ 4 +
2807 /* Name Count */ 4 +
2808 /* Abbrev table size */ 4 +
2809 /* Augmentation string size */ 4 +
2810 /* Augmentation string */ augmentationStringSize;
2811}
2812
2813static Expected<DebugNamesBaseSection::IndexEntry *>
2814readEntry(uint64_t &offset, const DWARFDebugNames::NameIndex &ni,
2815 uint64_t entriesBase, DWARFDataExtractor &namesExtractor,
2816 const LLDDWARFSection &namesSec) {
2817 auto ie = makeThreadLocal<DebugNamesBaseSection::IndexEntry>();
2818 ie->poolOffset = offset;
2819 Error err = Error::success();
2820 uint64_t ulebVal = namesExtractor.getULEB128(offset_ptr: &offset, Err: &err);
2821 if (err)
2822 return createStringError(EC: inconvertibleErrorCode(),
2823 Fmt: "invalid abbrev code: %s",
2824 Vals: llvm::toString(E: std::move(err)).c_str());
2825 if (!isUInt<32>(x: ulebVal))
2826 return createStringError(EC: inconvertibleErrorCode(),
2827 Fmt: "abbrev code too large for DWARF32: %" PRIu64,
2828 Vals: ulebVal);
2829 ie->abbrevCode = static_cast<uint32_t>(ulebVal);
2830 auto it = ni.getAbbrevs().find_as(Val: ie->abbrevCode);
2831 if (it == ni.getAbbrevs().end())
2832 return createStringError(EC: inconvertibleErrorCode(),
2833 Fmt: "abbrev code not found in abbrev table: %" PRIu32,
2834 Vals: ie->abbrevCode);
2835
2836 DebugNamesBaseSection::AttrValue attr, cuAttr = {.attrValue: 0, .attrSize: 0};
2837 for (DWARFDebugNames::AttributeEncoding a : it->Attributes) {
2838 if (a.Index == dwarf::DW_IDX_parent) {
2839 if (a.Form == dwarf::DW_FORM_ref4) {
2840 attr.attrValue = namesExtractor.getU32(offset_ptr: &offset, Err: &err);
2841 attr.attrSize = 4;
2842 ie->parentOffset = entriesBase + attr.attrValue;
2843 } else if (a.Form != DW_FORM_flag_present)
2844 return createStringError(EC: inconvertibleErrorCode(),
2845 S: "invalid form for DW_IDX_parent");
2846 } else {
2847 switch (a.Form) {
2848 case DW_FORM_data1:
2849 case DW_FORM_ref1: {
2850 attr.attrValue = namesExtractor.getU8(offset_ptr: &offset, Err: &err);
2851 attr.attrSize = 1;
2852 break;
2853 }
2854 case DW_FORM_data2:
2855 case DW_FORM_ref2: {
2856 attr.attrValue = namesExtractor.getU16(offset_ptr: &offset, Err: &err);
2857 attr.attrSize = 2;
2858 break;
2859 }
2860 case DW_FORM_data4:
2861 case DW_FORM_ref4: {
2862 attr.attrValue = namesExtractor.getU32(offset_ptr: &offset, Err: &err);
2863 attr.attrSize = 4;
2864 break;
2865 }
2866 default:
2867 return createStringError(
2868 EC: inconvertibleErrorCode(),
2869 Fmt: "unrecognized form encoding %d in abbrev table", Vals: a.Form);
2870 }
2871 }
2872 if (err)
2873 return createStringError(EC: inconvertibleErrorCode(),
2874 Fmt: "error while reading attributes: %s",
2875 Vals: llvm::toString(E: std::move(err)).c_str());
2876 if (a.Index == DW_IDX_compile_unit)
2877 cuAttr = attr;
2878 else if (a.Form != DW_FORM_flag_present)
2879 ie->attrValues.push_back(Elt: attr);
2880 }
2881 // Canonicalize abbrev by placing the CU/TU index at the end.
2882 ie->attrValues.push_back(Elt: cuAttr);
2883 return ie;
2884}
2885
2886void DebugNamesBaseSection::parseDebugNames(
2887 Ctx &ctx, InputChunk &inputChunk, OutputChunk &chunk,
2888 DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,
2889 function_ref<SmallVector<uint32_t, 0>(
2890 uint32_t numCus, const DWARFDebugNames::Header &,
2891 const DWARFDebugNames::DWARFDebugNamesOffsets &)>
2892 readOffsets) {
2893 const LLDDWARFSection &namesSec = inputChunk.section;
2894 DenseMap<uint32_t, IndexEntry *> offsetMap;
2895 // Number of CUs seen in previous NameIndex sections within current chunk.
2896 uint32_t numCus = 0;
2897 for (const DWARFDebugNames::NameIndex &ni : *inputChunk.llvmDebugNames) {
2898 NameData &nd = inputChunk.nameData.emplace_back();
2899 nd.hdr = ni.getHeader();
2900 if (nd.hdr.Format != DwarfFormat::DWARF32) {
2901 Err(ctx) << namesSec.sec
2902 << ": found DWARF64, which is currently unsupported";
2903 return;
2904 }
2905 if (nd.hdr.Version != 5) {
2906 Err(ctx) << namesSec.sec << ": unsupported version: " << nd.hdr.Version;
2907 return;
2908 }
2909 uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize(Format: DwarfFormat::DWARF32);
2910 DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();
2911 if (locs.EntriesBase > namesExtractor.getData().size()) {
2912 Err(ctx) << namesSec.sec << ": entry pool start is beyond end of section";
2913 return;
2914 }
2915
2916 SmallVector<uint32_t, 0> entryOffsets = readOffsets(numCus, nd.hdr, locs);
2917
2918 // Read the entry pool.
2919 offsetMap.clear();
2920 nd.nameEntries.resize(N: nd.hdr.NameCount);
2921 for (auto i : seq(Size: nd.hdr.NameCount)) {
2922 NameEntry &ne = nd.nameEntries[i];
2923 uint64_t strOffset = locs.StringOffsetsBase + i * dwarfSize;
2924 ne.stringOffset = strOffset;
2925 uint64_t strp = namesExtractor.getRelocatedValue(Size: dwarfSize, Off: &strOffset);
2926 StringRef name = strExtractor.getCStrRef(OffsetPtr: &strp);
2927 ne.name = name.data();
2928 ne.hashValue = caseFoldingDjbHash(Buffer: name);
2929
2930 // Read a series of index entries that end with abbreviation code 0.
2931 uint64_t offset = locs.EntriesBase + entryOffsets[i];
2932 while (offset < namesSec.Data.size() && namesSec.Data[offset] != 0) {
2933 // Read & store all entries (for the same string).
2934 Expected<IndexEntry *> ieOrErr =
2935 readEntry(offset, ni, entriesBase: locs.EntriesBase, namesExtractor, namesSec);
2936 if (!ieOrErr) {
2937 Err(ctx) << namesSec.sec << ": " << ieOrErr.takeError();
2938 return;
2939 }
2940 ne.indexEntries.push_back(Elt: std::move(*ieOrErr));
2941 }
2942 if (offset >= namesSec.Data.size())
2943 Err(ctx) << namesSec.sec << ": index entry is out of bounds";
2944
2945 for (IndexEntry &ie : ne.entries())
2946 offsetMap[ie.poolOffset] = &ie;
2947 }
2948
2949 // Assign parent pointers, which will be used to update DW_IDX_parent index
2950 // attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will
2951 // get parentEntry == null as well.
2952 for (NameEntry &ne : nd.nameEntries)
2953 for (IndexEntry &ie : ne.entries())
2954 ie.parentEntry = offsetMap.lookup(Val: ie.parentOffset);
2955 numCus += nd.hdr.CompUnitCount;
2956 }
2957}
2958
2959// Compute the form for output DW_IDX_compile_unit attributes, similar to
2960// DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all
2961// the merged CU indices.
2962std::pair<uint8_t, dwarf::Form> static getMergedCuCountForm(
2963 uint32_t compUnitCount) {
2964 if (compUnitCount > UINT16_MAX)
2965 return {4, DW_FORM_data4};
2966 if (compUnitCount > UINT8_MAX)
2967 return {2, DW_FORM_data2};
2968 return {1, DW_FORM_data1};
2969}
2970
2971void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2972 MutableArrayRef<InputChunk> inputChunks) {
2973 TimeTraceScope timeScope("Merge .debug_names", "hdr and abbrev table");
2974 size_t numCu = 0;
2975 hdr.Format = DwarfFormat::DWARF32;
2976 hdr.Version = 5;
2977 hdr.CompUnitCount = 0;
2978 hdr.LocalTypeUnitCount = 0;
2979 hdr.ForeignTypeUnitCount = 0;
2980 hdr.AugmentationStringSize = 0;
2981
2982 // Compute CU and TU counts.
2983 for (auto i : seq(Size: numChunks)) {
2984 InputChunk &inputChunk = inputChunks[i];
2985 inputChunk.baseCuIdx = numCu;
2986 numCu += chunks[i].compUnits.size();
2987 for (const NameData &nd : inputChunk.nameData) {
2988 hdr.CompUnitCount += nd.hdr.CompUnitCount;
2989 // TODO: We don't handle type units yet, so LocalTypeUnitCount &
2990 // ForeignTypeUnitCount are left as 0.
2991 if (nd.hdr.LocalTypeUnitCount || nd.hdr.ForeignTypeUnitCount)
2992 Warn(ctx) << inputChunk.section.sec
2993 << ": type units are not implemented";
2994 // If augmentation strings are not identical, use an empty string.
2995 if (i == 0) {
2996 hdr.AugmentationStringSize = nd.hdr.AugmentationStringSize;
2997 hdr.AugmentationString = nd.hdr.AugmentationString;
2998 } else if (hdr.AugmentationString != nd.hdr.AugmentationString) {
2999 // There are conflicting augmentation strings, so it's best for the
3000 // merged index to not use an augmentation string.
3001 hdr.AugmentationStringSize = 0;
3002 hdr.AugmentationString.clear();
3003 }
3004 }
3005 }
3006
3007 // Create the merged abbrev table, uniquifyinng the input abbrev tables and
3008 // computing mapping from old (per-cu) abbrev codes to new (merged) abbrev
3009 // codes.
3010 FoldingSet<Abbrev> abbrevSet;
3011 // Determine the form for the DW_IDX_compile_unit attributes in the merged
3012 // index. The input form may not be big enough for all CU indices.
3013 dwarf::Form cuAttrForm = getMergedCuCountForm(compUnitCount: hdr.CompUnitCount).second;
3014 for (InputChunk &inputChunk : inputChunks) {
3015 for (auto [i, ni] : enumerate(First&: *inputChunk.llvmDebugNames)) {
3016 for (const DWARFDebugNames::Abbrev &oldAbbrev : ni.getAbbrevs()) {
3017 // Canonicalize abbrev by placing the CU/TU index at the end,
3018 // similar to 'parseDebugNames'.
3019 Abbrev abbrev;
3020 DWARFDebugNames::AttributeEncoding cuAttr(DW_IDX_compile_unit,
3021 cuAttrForm);
3022 abbrev.code = oldAbbrev.Code;
3023 abbrev.tag = oldAbbrev.Tag;
3024 for (DWARFDebugNames::AttributeEncoding a : oldAbbrev.Attributes) {
3025 if (a.Index == DW_IDX_compile_unit)
3026 cuAttr.Index = a.Index;
3027 else
3028 abbrev.attributes.push_back(Elt: {a.Index, a.Form});
3029 }
3030 // Put the CU/TU index at the end of the attributes list.
3031 abbrev.attributes.push_back(Elt: cuAttr);
3032
3033 // Profile the abbrev, get or assign a new code, then record the abbrev
3034 // code mapping.
3035 FoldingSetNodeID id;
3036 abbrev.Profile(id);
3037 uint32_t newCode;
3038 void *insertPos;
3039 if (Abbrev *existing = abbrevSet.FindNodeOrInsertPos(ID: id, InsertPos&: insertPos)) {
3040 // Found it; we've already seen an identical abbreviation.
3041 newCode = existing->code;
3042 } else {
3043 Abbrev *abbrev2 =
3044 new (abbrevAlloc.Allocate()) Abbrev(std::move(abbrev));
3045 abbrevSet.InsertNode(N: abbrev2, InsertPos: insertPos);
3046 abbrevTable.push_back(Elt: abbrev2);
3047 newCode = abbrevTable.size();
3048 abbrev2->code = newCode;
3049 }
3050 inputChunk.nameData[i].abbrevCodeMap[oldAbbrev.Code] = newCode;
3051 }
3052 }
3053 }
3054
3055 // Compute the merged abbrev table.
3056 raw_svector_ostream os(abbrevTableBuf);
3057 for (Abbrev *abbrev : abbrevTable) {
3058 encodeULEB128(Value: abbrev->code, OS&: os);
3059 encodeULEB128(Value: abbrev->tag, OS&: os);
3060 for (DWARFDebugNames::AttributeEncoding a : abbrev->attributes) {
3061 encodeULEB128(Value: a.Index, OS&: os);
3062 encodeULEB128(Value: a.Form, OS&: os);
3063 }
3064 os.write(Ptr: "\0", Size: 2); // attribute specification end
3065 }
3066 os.write(C: 0); // abbrev table end
3067 hdr.AbbrevTableSize = abbrevTableBuf.size();
3068}
3069
3070void DebugNamesBaseSection::Abbrev::Profile(FoldingSetNodeID &id) const {
3071 id.AddInteger(I: tag);
3072 for (const DWARFDebugNames::AttributeEncoding &attr : attributes) {
3073 id.AddInteger(I: attr.Index);
3074 id.AddInteger(I: attr.Form);
3075 }
3076}
3077
3078std::pair<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(
3079 MutableArrayRef<InputChunk> inputChunks) {
3080 TimeTraceScope timeScope("Merge .debug_names", "entry pool");
3081 // Collect and de-duplicate all the names (preserving all the entries).
3082 // Speed it up using multithreading, as the number of symbols can be in the
3083 // order of millions.
3084 const size_t concurrency =
3085 bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
3086 const size_t shift = 32 - countr_zero(Val: numShards);
3087 const uint8_t cuAttrSize = getMergedCuCountForm(compUnitCount: hdr.CompUnitCount).first;
3088 DenseMap<CachedHashStringRef, size_t> maps[numShards];
3089
3090 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
3091 for (auto i : seq(Size: numChunks)) {
3092 InputChunk &inputChunk = inputChunks[i];
3093 for (auto j : seq(Size: inputChunk.nameData.size())) {
3094 NameData &nd = inputChunk.nameData[j];
3095 // Deduplicate the NameEntry records (based on the string/name),
3096 // appending all IndexEntries from duplicate NameEntry records to
3097 // the single preserved copy.
3098 for (NameEntry &ne : nd.nameEntries) {
3099 auto shardId = ne.hashValue >> shift;
3100 if ((shardId & (concurrency - 1)) != threadId)
3101 continue;
3102
3103 ne.chunkIdx = i;
3104 for (IndexEntry &ie : ne.entries()) {
3105 // Update the IndexEntry's abbrev code to match the merged
3106 // abbreviations.
3107 ie.abbrevCode = nd.abbrevCodeMap[ie.abbrevCode];
3108 // Update the DW_IDX_compile_unit attribute (the last one after
3109 // canonicalization) to have correct merged offset value and size.
3110 auto &back = ie.attrValues.back();
3111 back.attrValue += inputChunk.baseCuIdx + j;
3112 back.attrSize = cuAttrSize;
3113 }
3114
3115 auto &nameVec = nameVecs[shardId];
3116 auto [it, inserted] = maps[shardId].try_emplace(
3117 Key: CachedHashStringRef(ne.name, ne.hashValue), Args: nameVec.size());
3118 if (inserted)
3119 nameVec.push_back(Elt: std::move(ne));
3120 else
3121 nameVec[it->second].indexEntries.append(RHS: std::move(ne.indexEntries));
3122 }
3123 }
3124 }
3125 });
3126
3127 // Compute entry offsets in parallel. First, compute offsets relative to the
3128 // current shard.
3129 uint32_t offsets[numShards];
3130 parallelFor(Begin: 0, End: numShards, Fn: [&](size_t shard) {
3131 uint32_t offset = 0;
3132 for (NameEntry &ne : nameVecs[shard]) {
3133 ne.entryOffset = offset;
3134 for (IndexEntry &ie : ne.entries()) {
3135 ie.poolOffset = offset;
3136 offset += getULEB128Size(Value: ie.abbrevCode);
3137 for (AttrValue value : ie.attrValues)
3138 offset += value.attrSize;
3139 }
3140 ++offset; // index entry sentinel
3141 }
3142 offsets[shard] = offset;
3143 });
3144 // Then add shard offsets.
3145 std::partial_sum(first: offsets, last: std::end(arr&: offsets), result: offsets);
3146 parallelFor(Begin: 1, End: numShards, Fn: [&](size_t shard) {
3147 uint32_t offset = offsets[shard - 1];
3148 for (NameEntry &ne : nameVecs[shard]) {
3149 ne.entryOffset += offset;
3150 for (IndexEntry &ie : ne.entries())
3151 ie.poolOffset += offset;
3152 }
3153 });
3154
3155 // Update the DW_IDX_parent entries that refer to real parents (have
3156 // DW_FORM_ref4).
3157 parallelFor(Begin: 0, End: numShards, Fn: [&](size_t shard) {
3158 for (NameEntry &ne : nameVecs[shard]) {
3159 for (IndexEntry &ie : ne.entries()) {
3160 if (!ie.parentEntry)
3161 continue;
3162 // Abbrevs are indexed starting at 1; vector starts at 0. (abbrevCode
3163 // corresponds to position in the merged table vector).
3164 const Abbrev *abbrev = abbrevTable[ie.abbrevCode - 1];
3165 for (const auto &[a, v] : zip_equal(t: abbrev->attributes, u&: ie.attrValues))
3166 if (a.Index == DW_IDX_parent && a.Form == DW_FORM_ref4)
3167 v.attrValue = ie.parentEntry->poolOffset;
3168 }
3169 }
3170 });
3171
3172 // Return (entry pool size, number of entries).
3173 uint32_t num = 0;
3174 for (auto &map : maps)
3175 num += map.size();
3176 return {offsets[numShards - 1], num};
3177}
3178
3179void DebugNamesBaseSection::init(
3180 function_ref<void(InputFile *, InputChunk &, OutputChunk &)> parseFile) {
3181 TimeTraceScope timeScope("Merge .debug_names");
3182 // Collect and remove input .debug_names sections. Save InputSection pointers
3183 // to relocate string offsets in `writeTo`.
3184 SetVector<InputFile *> files;
3185 for (InputSectionBase *s : ctx.inputSections) {
3186 InputSection *isec = dyn_cast<InputSection>(Val: s);
3187 if (!isec)
3188 continue;
3189 if (!(s->flags & SHF_ALLOC) && s->name == ".debug_names") {
3190 s->markDead();
3191 inputSections.push_back(Elt: isec);
3192 files.insert(X: isec->file);
3193 }
3194 }
3195
3196 // Parse input .debug_names sections and extract InputChunk and OutputChunk
3197 // data. OutputChunk contains CU information, which will be needed by
3198 // `writeTo`.
3199 auto inputChunksPtr = std::make_unique<InputChunk[]>(num: files.size());
3200 MutableArrayRef<InputChunk> inputChunks(inputChunksPtr.get(), files.size());
3201 numChunks = files.size();
3202 chunks = std::make_unique<OutputChunk[]>(num: files.size());
3203 {
3204 TimeTraceScope timeScope("Merge .debug_names", "parse");
3205 parallelFor(Begin: 0, End: files.size(), Fn: [&](size_t i) {
3206 parseFile(files[i], inputChunks[i], chunks[i]);
3207 });
3208 }
3209
3210 // Compute section header (except unit_length), abbrev table, and entry pool.
3211 computeHdrAndAbbrevTable(inputChunks);
3212 uint32_t entryPoolSize;
3213 std::tie(args&: entryPoolSize, args&: hdr.NameCount) = computeEntryPool(inputChunks);
3214 hdr.BucketCount = dwarf::getDebugNamesBucketCount(UniqueHashCount: hdr.NameCount);
3215
3216 // Compute the section size. Subtract 4 to get the unit_length for DWARF32.
3217 uint32_t hdrSize = getDebugNamesHeaderSize(augmentationStringSize: hdr.AugmentationStringSize);
3218 size = findDebugNamesOffsets(EndOfHeaderOffset: hdrSize, Hdr: hdr).EntriesBase + entryPoolSize;
3219 hdr.UnitLength = size - 4;
3220}
3221
3222template <class ELFT>
3223DebugNamesSection<ELFT>::DebugNamesSection(Ctx &ctx)
3224 : DebugNamesBaseSection(ctx) {
3225 init(parseFile: [&](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {
3226 auto *file = cast<ObjFile<ELFT>>(f);
3227 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3228 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3229 chunk.infoSec = dobj.getInfoSection();
3230 DWARFDataExtractor namesExtractor(dobj, dobj.getNamesSection(),
3231 ELFT::Endianness == endianness::little,
3232 ELFT::Is64Bits ? 8 : 4);
3233 // .debug_str is needed to get symbol names from string offsets.
3234 DataExtractor strExtractor(dobj.getStrSection(),
3235 ELFT::Endianness == endianness::little,
3236 ELFT::Is64Bits ? 8 : 4);
3237 inputChunk.section = dobj.getNamesSection();
3238
3239 inputChunk.llvmDebugNames.emplace(args&: namesExtractor, args&: strExtractor);
3240 if (Error e = inputChunk.llvmDebugNames->extract()) {
3241 Err(ctx) << dobj.getNamesSection().sec << ": " << std::move(e);
3242 }
3243 parseDebugNames(
3244 ctx, inputChunk, chunk, namesExtractor, strExtractor,
3245 readOffsets: [&chunk, namesData = dobj.getNamesSection().Data.data()](
3246 uint32_t numCus, const DWARFDebugNames::Header &hdr,
3247 const DWARFDebugNames::DWARFDebugNamesOffsets &locs) {
3248 // Read CU offsets, which are relocated by .debug_info + X
3249 // relocations. Record the section offset to be relocated by
3250 // `finalizeContents`.
3251 chunk.compUnits.resize_for_overwrite(N: numCus + hdr.CompUnitCount);
3252 for (auto i : seq(Size: hdr.CompUnitCount))
3253 chunk.compUnits[numCus + i] = locs.CUsBase + i * 4;
3254
3255 // Read entry offsets.
3256 const char *p = namesData + locs.EntryOffsetsBase;
3257 SmallVector<uint32_t, 0> entryOffsets;
3258 entryOffsets.resize_for_overwrite(N: hdr.NameCount);
3259 for (uint32_t &offset : entryOffsets)
3260 offset = endian::readNext<uint32_t, ELFT::Endianness, unaligned>(p);
3261 return entryOffsets;
3262 });
3263 });
3264}
3265
3266template <class ELFT>
3267template <class RelTy>
3268void DebugNamesSection<ELFT>::getNameRelocs(
3269 const InputFile &file, DenseMap<uint32_t, uint32_t> &relocs,
3270 Relocs<RelTy> rels) {
3271 for (const RelTy &rel : rels) {
3272 Symbol &sym = file.getRelocTargetSym(rel);
3273 relocs[rel.r_offset] = sym.getVA(ctx, addend: getAddend<ELFT>(rel));
3274 }
3275}
3276
3277template <class ELFT> void DebugNamesSection<ELFT>::finalizeContents() {
3278 // Get relocations of .debug_names sections.
3279 auto relocs = std::make_unique<DenseMap<uint32_t, uint32_t>[]>(numChunks);
3280 parallelFor(0, numChunks, [&](size_t i) {
3281 InputSection *sec = inputSections[i];
3282 invokeOnRelocs(*sec, getNameRelocs, *sec->file, relocs.get()[i]);
3283
3284 // Relocate CU offsets with .debug_info + X relocations.
3285 OutputChunk &chunk = chunks.get()[i];
3286 for (auto [j, cuOffset] : enumerate(First&: chunk.compUnits))
3287 cuOffset = relocs.get()[i].lookup(cuOffset);
3288 });
3289
3290 // Relocate string offsets in the name table with .debug_str + X relocations.
3291 parallelForEach(nameVecs, [&](auto &nameVec) {
3292 for (NameEntry &ne : nameVec)
3293 ne.stringOffset = relocs.get()[ne.chunkIdx].lookup(ne.stringOffset);
3294 });
3295}
3296
3297template <class ELFT> void DebugNamesSection<ELFT>::writeTo(uint8_t *buf) {
3298 [[maybe_unused]] const uint8_t *const beginBuf = buf;
3299 // Write the header.
3300 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.UnitLength);
3301 endian::writeNext<uint16_t, ELFT::Endianness>(buf, hdr.Version);
3302 buf += 2; // padding
3303 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.CompUnitCount);
3304 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.LocalTypeUnitCount);
3305 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.ForeignTypeUnitCount);
3306 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.BucketCount);
3307 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.NameCount);
3308 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.AbbrevTableSize);
3309 endian::writeNext<uint32_t, ELFT::Endianness>(buf,
3310 hdr.AugmentationStringSize);
3311 memcpy(buf, hdr.AugmentationString.c_str(), hdr.AugmentationString.size());
3312 buf += hdr.AugmentationStringSize;
3313
3314 // Write the CU list.
3315 for (auto &chunk : getChunks())
3316 for (uint32_t cuOffset : chunk.compUnits)
3317 endian::writeNext<uint32_t, ELFT::Endianness>(buf, cuOffset);
3318
3319 // TODO: Write the local TU list, then the foreign TU list..
3320
3321 // Write the hash lookup table.
3322 SmallVector<SmallVector<NameEntry *, 0>, 0> buckets(hdr.BucketCount);
3323 // Symbols enter into a bucket whose index is the hash modulo bucket_count.
3324 for (auto &nameVec : nameVecs)
3325 for (NameEntry &ne : nameVec)
3326 buckets[ne.hashValue % hdr.BucketCount].push_back(&ne);
3327
3328 // Write buckets (accumulated bucket counts).
3329 uint32_t bucketIdx = 1;
3330 for (const SmallVector<NameEntry *, 0> &bucket : buckets) {
3331 if (!bucket.empty())
3332 endian::write32<ELFT::Endianness>(buf, bucketIdx);
3333 buf += 4;
3334 bucketIdx += bucket.size();
3335 }
3336 // Write the hashes.
3337 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3338 for (const NameEntry *e : bucket)
3339 endian::writeNext<uint32_t, ELFT::Endianness>(buf, e->hashValue);
3340
3341 // Write the name table. The name entries are ordered by bucket_idx and
3342 // correspond one-to-one with the hash lookup table.
3343 //
3344 // First, write the relocated string offsets.
3345 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3346 for (const NameEntry *ne : bucket)
3347 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->stringOffset);
3348
3349 // Then write the entry offsets.
3350 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3351 for (const NameEntry *ne : bucket)
3352 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->entryOffset);
3353
3354 // Write the abbrev table.
3355 buf = llvm::copy(abbrevTableBuf, buf);
3356
3357 // Write the entry pool. Unlike the name table, the name entries follow the
3358 // nameVecs order computed by `computeEntryPool`.
3359 for (auto &nameVec : nameVecs) {
3360 for (NameEntry &ne : nameVec) {
3361 // Write all the entries for the string.
3362 for (const IndexEntry &ie : ne.entries()) {
3363 buf += encodeULEB128(Value: ie.abbrevCode, p: buf);
3364 for (AttrValue value : ie.attrValues) {
3365 switch (value.attrSize) {
3366 case 1:
3367 *buf++ = value.attrValue;
3368 break;
3369 case 2:
3370 endian::writeNext<uint16_t, ELFT::Endianness>(buf, value.attrValue);
3371 break;
3372 case 4:
3373 endian::writeNext<uint32_t, ELFT::Endianness>(buf, value.attrValue);
3374 break;
3375 default:
3376 llvm_unreachable("invalid attrSize");
3377 }
3378 }
3379 }
3380 ++buf; // index entry sentinel
3381 }
3382 }
3383 assert(uint64_t(buf - beginBuf) == size);
3384}
3385
3386GdbIndexSection::GdbIndexSection(Ctx &ctx)
3387 : SyntheticSection(ctx, ".gdb_index", SHT_PROGBITS, 0, 1) {}
3388
3389// Returns the desired size of an on-disk hash table for a .gdb_index section.
3390// There's a tradeoff between size and collision rate. We aim 75% utilization.
3391size_t GdbIndexSection::computeSymtabSize() const {
3392 return std::max<size_t>(a: NextPowerOf2(A: symbols.size() * 4 / 3), b: 1024);
3393}
3394
3395static SmallVector<GdbIndexSection::CuEntry, 0>
3396readCuList(DWARFContext &dwarf) {
3397 SmallVector<GdbIndexSection::CuEntry, 0> ret;
3398 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())
3399 ret.push_back(Elt: {.cuOffset: cu->getOffset(), .cuLength: cu->getLength() + 4});
3400 return ret;
3401}
3402
3403static SmallVector<GdbIndexSection::AddressEntry, 0>
3404readAddressAreas(Ctx &ctx, DWARFContext &dwarf, InputSection *sec) {
3405 SmallVector<GdbIndexSection::AddressEntry, 0> ret;
3406
3407 uint32_t cuIdx = 0;
3408 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {
3409 if (Error e = cu->tryExtractDIEsIfNeeded(CUDieOnly: false)) {
3410 Warn(ctx) << sec << ": " << std::move(e);
3411 return {};
3412 }
3413 Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges();
3414 if (!ranges) {
3415 Warn(ctx) << sec << ": " << ranges.takeError();
3416 return {};
3417 }
3418
3419 ArrayRef<InputSectionBase *> sections = sec->file->getSections();
3420 for (DWARFAddressRange &r : *ranges) {
3421 if (r.SectionIndex == -1ULL)
3422 continue;
3423 // Range list with zero size has no effect.
3424 InputSectionBase *s = sections[r.SectionIndex];
3425 if (s && s != &InputSection::discarded && s->isLive())
3426 if (r.LowPC != r.HighPC)
3427 ret.push_back(Elt: {.section: cast<InputSection>(Val: s), .lowAddress: r.LowPC, .highAddress: r.HighPC, .cuIndex: cuIdx});
3428 }
3429 ++cuIdx;
3430 }
3431
3432 return ret;
3433}
3434
3435template <class ELFT>
3436static SmallVector<GdbIndexSection::NameAttrEntry, 0>
3437readPubNamesAndTypes(Ctx &ctx, const LLDDwarfObj<ELFT> &obj,
3438 const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) {
3439 const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection();
3440 const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection();
3441
3442 SmallVector<GdbIndexSection::NameAttrEntry, 0> ret;
3443 for (const LLDDWARFSection *pub : {&pubNames, &pubTypes}) {
3444 DWARFDataExtractor data(obj, *pub, ELFT::Endianness == endianness::little,
3445 ELFT::Is64Bits ? 8 : 4);
3446 DWARFDebugPubTable table;
3447 table.extract(Data: data, /*GnuStyle=*/true, RecoverableErrorHandler: [&](Error e) {
3448 Warn(ctx) << pub->sec << ": " << std::move(e);
3449 });
3450 for (const DWARFDebugPubTable::Set &set : table.getData()) {
3451 // The value written into the constant pool is kind << 24 | cuIndex. As we
3452 // don't know how many compilation units precede this object to compute
3453 // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add
3454 // the number of preceding compilation units later.
3455 uint32_t i = llvm::partition_point(cus,
3456 [&](GdbIndexSection::CuEntry cu) {
3457 return cu.cuOffset < set.Offset;
3458 }) -
3459 cus.begin();
3460 for (const DWARFDebugPubTable::Entry &ent : set.Entries)
3461 ret.push_back(Elt: {.name: {ent.Name, computeGdbHash(s: ent.Name)},
3462 .cuIndexAndAttrs: (ent.Descriptor.toBits() << 24) | i});
3463 }
3464 }
3465 return ret;
3466}
3467
3468// Create a list of symbols from a given list of symbol names and types
3469// by uniquifying them by name.
3470static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t>
3471createSymbols(
3472 Ctx &ctx,
3473 ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs,
3474 const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) {
3475 using GdbSymbol = GdbIndexSection::GdbSymbol;
3476 using NameAttrEntry = GdbIndexSection::NameAttrEntry;
3477
3478 // For each chunk, compute the number of compilation units preceding it.
3479 uint32_t cuIdx = 0;
3480 std::unique_ptr<uint32_t[]> cuIdxs(new uint32_t[chunks.size()]);
3481 for (uint32_t i = 0, e = chunks.size(); i != e; ++i) {
3482 cuIdxs[i] = cuIdx;
3483 cuIdx += chunks[i].compilationUnits.size();
3484 }
3485
3486 // Collect the compilation unitss for each unique name. Speed it up using
3487 // multi-threading as the number of symbols can be in the order of millions.
3488 // Shard GdbSymbols by hash's high bits.
3489 constexpr size_t numShards = 32;
3490 const size_t concurrency =
3491 llvm::bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
3492 const size_t shift = 32 - llvm::countr_zero(Val: numShards);
3493 auto map =
3494 std::make_unique<DenseMap<CachedHashStringRef, size_t>[]>(num: numShards);
3495 auto symbols = std::make_unique<SmallVector<GdbSymbol, 0>[]>(num: numShards);
3496 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
3497 uint32_t i = 0;
3498 for (ArrayRef<NameAttrEntry> entries : nameAttrs) {
3499 for (const NameAttrEntry &ent : entries) {
3500 size_t shardId = ent.name.hash() >> shift;
3501 if ((shardId & (concurrency - 1)) != threadId)
3502 continue;
3503
3504 uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i];
3505 auto [it, inserted] =
3506 map[shardId].try_emplace(Key: ent.name, Args: symbols[shardId].size());
3507 if (inserted)
3508 symbols[shardId].push_back(Elt: {.name: ent.name, .cuVector: {v}, .nameOff: 0, .cuVectorOff: 0});
3509 else
3510 symbols[shardId][it->second].cuVector.push_back(Elt: v);
3511 }
3512 ++i;
3513 }
3514 });
3515
3516 size_t numSymbols = 0;
3517 for (ArrayRef<GdbSymbol> v : ArrayRef(symbols.get(), numShards))
3518 numSymbols += v.size();
3519
3520 // The return type is a flattened vector, so we'll copy each vector
3521 // contents to Ret.
3522 SmallVector<GdbSymbol, 0> ret;
3523 ret.reserve(N: numSymbols);
3524 for (SmallVector<GdbSymbol, 0> &vec :
3525 MutableArrayRef(symbols.get(), numShards))
3526 for (GdbSymbol &sym : vec)
3527 ret.push_back(Elt: std::move(sym));
3528
3529 // CU vectors and symbol names are adjacent in the output file.
3530 // We can compute their offsets in the output file now.
3531 size_t off = 0;
3532 for (GdbSymbol &sym : ret) {
3533 sym.cuVectorOff = off;
3534 off += (sym.cuVector.size() + 1) * 4;
3535 }
3536 for (GdbSymbol &sym : ret) {
3537 sym.nameOff = off;
3538 off += sym.name.size() + 1;
3539 }
3540 // If off overflows, the last symbol's nameOff likely overflows.
3541 if (!isUInt<32>(x: off))
3542 Err(ctx) << "--gdb-index: constant pool size (" << off
3543 << ") exceeds UINT32_MAX";
3544
3545 return {ret, off};
3546}
3547
3548// Returns a newly-created .gdb_index section.
3549template <class ELFT>
3550std::unique_ptr<GdbIndexSection> GdbIndexSection::create(Ctx &ctx) {
3551 llvm::TimeTraceScope timeScope("Create gdb index");
3552
3553 // Collect InputFiles with .debug_info. See the comment in
3554 // LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future,
3555 // note that isec->data() may uncompress the full content, which should be
3556 // parallelized.
3557 SetVector<InputFile *> files;
3558 for (InputSectionBase *s : ctx.inputSections) {
3559 InputSection *isec = dyn_cast<InputSection>(Val: s);
3560 if (!isec)
3561 continue;
3562 // .debug_gnu_pub{names,types} are useless in executables.
3563 // They are present in input object files solely for creating
3564 // a .gdb_index. So we can remove them from the output.
3565 if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")
3566 s->markDead();
3567 else if (isec->name == ".debug_info")
3568 files.insert(X: isec->file);
3569 }
3570 // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs.
3571 llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
3572 if (auto *isec = dyn_cast<InputSection>(Val: s))
3573 if (InputSectionBase *rel = isec->getRelocatedSection())
3574 return !rel->isLive();
3575 return !s->isLive();
3576 });
3577
3578 SmallVector<GdbChunk, 0> chunks(files.size());
3579 SmallVector<SmallVector<NameAttrEntry, 0>, 0> nameAttrs(files.size());
3580
3581 parallelFor(0, files.size(), [&](size_t i) {
3582 // To keep memory usage low, we don't want to keep cached DWARFContext, so
3583 // avoid getDwarf() here.
3584 ObjFile<ELFT> *file = cast<ObjFile<ELFT>>(files[i]);
3585 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3586 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3587
3588 // If the are multiple compile units .debug_info (very rare ld -r --unique),
3589 // this only picks the last one. Other address ranges are lost.
3590 chunks[i].sec = dobj.getInfoSection();
3591 chunks[i].compilationUnits = readCuList(dwarf);
3592 chunks[i].addressAreas = readAddressAreas(ctx, dwarf, sec: chunks[i].sec);
3593 nameAttrs[i] =
3594 readPubNamesAndTypes<ELFT>(ctx, dobj, chunks[i].compilationUnits);
3595 });
3596
3597 auto ret = std::make_unique<GdbIndexSection>(args&: ctx);
3598 ret->chunks = std::move(chunks);
3599 std::tie(args&: ret->symbols, args&: ret->size) =
3600 createSymbols(ctx, nameAttrs, chunks: ret->chunks);
3601
3602 // Count the areas other than the constant pool.
3603 ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8;
3604 for (GdbChunk &chunk : ret->chunks)
3605 ret->size +=
3606 chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20;
3607
3608 return ret;
3609}
3610
3611void GdbIndexSection::writeTo(uint8_t *buf) {
3612 // Write the header.
3613 auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf);
3614 uint8_t *start = buf;
3615 hdr->version = 7;
3616 buf += sizeof(*hdr);
3617
3618 // Write the CU list.
3619 hdr->cuListOff = buf - start;
3620 for (GdbChunk &chunk : chunks) {
3621 for (CuEntry &cu : chunk.compilationUnits) {
3622 write64le(P: buf, V: chunk.sec->outSecOff + cu.cuOffset);
3623 write64le(P: buf + 8, V: cu.cuLength);
3624 buf += 16;
3625 }
3626 }
3627
3628 // Write the address area.
3629 hdr->cuTypesOff = buf - start;
3630 hdr->addressAreaOff = buf - start;
3631 uint32_t cuOff = 0;
3632 for (GdbChunk &chunk : chunks) {
3633 for (AddressEntry &e : chunk.addressAreas) {
3634 // In the case of ICF there may be duplicate address range entries.
3635 const uint64_t baseAddr = e.section->repl->getVA(offset: 0);
3636 write64le(P: buf, V: baseAddr + e.lowAddress);
3637 write64le(P: buf + 8, V: baseAddr + e.highAddress);
3638 write32le(P: buf + 16, V: e.cuIndex + cuOff);
3639 buf += 20;
3640 }
3641 cuOff += chunk.compilationUnits.size();
3642 }
3643
3644 // Write the on-disk open-addressing hash table containing symbols.
3645 hdr->symtabOff = buf - start;
3646 size_t symtabSize = computeSymtabSize();
3647 uint32_t mask = symtabSize - 1;
3648
3649 for (GdbSymbol &sym : symbols) {
3650 uint32_t h = sym.name.hash();
3651 uint32_t i = h & mask;
3652 uint32_t step = ((h * 17) & mask) | 1;
3653
3654 while (read32le(P: buf + i * 8))
3655 i = (i + step) & mask;
3656
3657 write32le(P: buf + i * 8, V: sym.nameOff);
3658 write32le(P: buf + i * 8 + 4, V: sym.cuVectorOff);
3659 }
3660
3661 buf += symtabSize * 8;
3662
3663 // Write the string pool.
3664 hdr->constantPoolOff = buf - start;
3665 parallelForEach(R&: symbols, Fn: [&](GdbSymbol &sym) {
3666 memcpy(dest: buf + sym.nameOff, src: sym.name.data(), n: sym.name.size());
3667 });
3668
3669 // Write the CU vectors.
3670 for (GdbSymbol &sym : symbols) {
3671 write32le(P: buf, V: sym.cuVector.size());
3672 buf += 4;
3673 for (uint32_t val : sym.cuVector) {
3674 write32le(P: buf, V: val);
3675 buf += 4;
3676 }
3677 }
3678}
3679
3680bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }
3681
3682VersionDefinitionSection::VersionDefinitionSection(Ctx &ctx)
3683 : SyntheticSection(ctx, ".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC,
3684 sizeof(uint32_t)) {}
3685
3686StringRef VersionDefinitionSection::getFileDefName() {
3687 if (!getPartition(ctx).name.empty())
3688 return getPartition(ctx).name;
3689 if (!ctx.arg.soName.empty())
3690 return ctx.arg.soName;
3691 return ctx.arg.outputFile;
3692}
3693
3694void VersionDefinitionSection::finalizeContents() {
3695 fileDefNameOff = getPartition(ctx).dynStrTab->addString(s: getFileDefName());
3696 for (const VersionDefinition &v : namedVersionDefs(ctx))
3697 verDefNameOffs.push_back(Elt: getPartition(ctx).dynStrTab->addString(s: v.name));
3698
3699 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3700 getParent()->link = sec->sectionIndex;
3701
3702 // sh_info should be set to the number of definitions. This fact is missed in
3703 // documentation, but confirmed by binutils community:
3704 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
3705 getParent()->info = getVerDefNum(ctx);
3706}
3707
3708void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index,
3709 StringRef name, size_t nameOff) {
3710 uint16_t flags = index == 1 ? VER_FLG_BASE : 0;
3711
3712 // Write a verdef.
3713 write16(ctx, p: buf, v: 1); // vd_version
3714 write16(ctx, p: buf + 2, v: flags); // vd_flags
3715 write16(ctx, p: buf + 4, v: index); // vd_ndx
3716 write16(ctx, p: buf + 6, v: 1); // vd_cnt
3717 write32(ctx, p: buf + 8, v: hashSysV(SymbolName: name)); // vd_hash
3718 write32(ctx, p: buf + 12, v: 20); // vd_aux
3719 write32(ctx, p: buf + 16, v: 28); // vd_next
3720
3721 // Write a veraux.
3722 write32(ctx, p: buf + 20, v: nameOff); // vda_name
3723 write32(ctx, p: buf + 24, v: 0); // vda_next
3724}
3725
3726void VersionDefinitionSection::writeTo(uint8_t *buf) {
3727 writeOne(buf, index: 1, name: getFileDefName(), nameOff: fileDefNameOff);
3728
3729 auto nameOffIt = verDefNameOffs.begin();
3730 for (const VersionDefinition &v : namedVersionDefs(ctx)) {
3731 buf += EntrySize;
3732 writeOne(buf, index: v.id, name: v.name, nameOff: *nameOffIt++);
3733 }
3734
3735 // Need to terminate the last version definition.
3736 write32(ctx, p: buf + 16, v: 0); // vd_next
3737}
3738
3739size_t VersionDefinitionSection::getSize() const {
3740 return EntrySize * getVerDefNum(ctx);
3741}
3742
3743// .gnu.version is a table where each entry is 2 byte long.
3744VersionTableSection::VersionTableSection(Ctx &ctx)
3745 : SyntheticSection(ctx, ".gnu.version", SHT_GNU_versym, SHF_ALLOC,
3746 sizeof(uint16_t)) {
3747 this->entsize = 2;
3748}
3749
3750void VersionTableSection::finalizeContents() {
3751 if (OutputSection *osec = getPartition(ctx).dynSymTab->getParent())
3752 getParent()->link = osec->sectionIndex;
3753}
3754
3755size_t VersionTableSection::getSize() const {
3756 return (getPartition(ctx).dynSymTab->getSymbols().size() + 1) * 2;
3757}
3758
3759void VersionTableSection::writeTo(uint8_t *buf) {
3760 buf += 2;
3761 for (const SymbolTableEntry &s : getPartition(ctx).dynSymTab->getSymbols()) {
3762 // For an unextracted lazy symbol (undefined weak), it must have been
3763 // converted to Undefined.
3764 assert(!s.sym->isLazy());
3765 // Undefined symbols should use index 0 when unversioned.
3766 write16(ctx, p: buf, v: s.sym->isUndefined() ? 0 : s.sym->versionId);
3767 buf += 2;
3768 }
3769}
3770
3771bool VersionTableSection::isNeeded() const {
3772 return isLive() &&
3773 (getPartition(ctx).verDef || getPartition(ctx).verNeed->isNeeded());
3774}
3775
3776void elf::addVerneed(Ctx &ctx, Symbol &ss) {
3777 auto &file = cast<SharedFile>(Val&: *ss.file);
3778 if (ss.versionId == VER_NDX_GLOBAL)
3779 return;
3780
3781 if (file.verneedInfo.empty())
3782 file.verneedInfo.resize(N: file.verdefs.size());
3783
3784 // Select a version identifier for the vernaux data structure, if we haven't
3785 // already allocated one. The verdef identifiers cover the range
3786 // [1..getVerDefNum(ctx)]; this causes the vernaux identifiers to start from
3787 // getVerDefNum(ctx)+1.
3788 if (file.verneedInfo[ss.versionId].id == 0)
3789 file.verneedInfo[ss.versionId].id = ++ctx.vernauxNum + getVerDefNum(ctx);
3790 file.verneedInfo[ss.versionId].weak &= ss.isWeak();
3791
3792 ss.versionId = file.verneedInfo[ss.versionId].id;
3793}
3794
3795template <class ELFT>
3796VersionNeedSection<ELFT>::VersionNeedSection(Ctx &ctx)
3797 : SyntheticSection(ctx, ".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC,
3798 sizeof(uint32_t)) {}
3799
3800template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
3801 for (SharedFile *f : ctx.sharedFiles) {
3802 if (f->verneedInfo.empty())
3803 continue;
3804 verneeds.emplace_back();
3805 Verneed &vn = verneeds.back();
3806 vn.nameStrTab = getPartition(ctx).dynStrTab->addString(f->soName);
3807 bool isLibc = ctx.arg.relrGlibc && f->soName.starts_with(Prefix: "libc.so.");
3808 bool isGlibc2 = false;
3809 for (unsigned i = 0; i != f->verneedInfo.size(); ++i) {
3810 if (f->verneedInfo[i].id == 0)
3811 continue;
3812 // Each Verdef has one or more Verdaux entries. The first Verdaux gives
3813 // the version name; subsequent entries (if any) are parent versions
3814 // (e.g., v2 {} v1;). We only use the first one, as parent versions have
3815 // no rtld behavior difference in practice.
3816 auto *verdef =
3817 reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]);
3818 StringRef ver(f->getStringTable().data() + verdef->getAux()->vda_name);
3819 if (isLibc && ver.starts_with(Prefix: "GLIBC_2."))
3820 isGlibc2 = true;
3821 vn.vernauxs.push_back({verdef->vd_hash, f->verneedInfo[i],
3822 getPartition(ctx).dynStrTab->addString(ver)});
3823 }
3824 if (isGlibc2) {
3825 const char *ver = "GLIBC_ABI_DT_RELR";
3826 vn.vernauxs.push_back(
3827 {hashSysV(SymbolName: ver),
3828 {uint16_t(++ctx.vernauxNum + getVerDefNum(ctx)), false},
3829 getPartition(ctx).dynStrTab->addString(ver)});
3830 }
3831 }
3832
3833 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3834 getParent()->link = sec->sectionIndex;
3835 getParent()->info = verneeds.size();
3836}
3837
3838template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) {
3839 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
3840 auto *verneed = reinterpret_cast<Elf_Verneed *>(buf);
3841 auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size());
3842
3843 for (auto &vn : verneeds) {
3844 // Create an Elf_Verneed for this DSO.
3845 verneed->vn_version = 1;
3846 verneed->vn_cnt = vn.vernauxs.size();
3847 verneed->vn_file = vn.nameStrTab;
3848 verneed->vn_aux =
3849 reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed);
3850 verneed->vn_next = sizeof(Elf_Verneed);
3851 ++verneed;
3852
3853 // Create the Elf_Vernauxs for this Elf_Verneed.
3854 for (auto &vna : vn.vernauxs) {
3855 vernaux->vna_hash = vna.hash;
3856 vernaux->vna_flags = vna.verneedInfo.weak ? VER_FLG_WEAK : 0;
3857 vernaux->vna_other = vna.verneedInfo.id;
3858 vernaux->vna_name = vna.nameStrTab;
3859 vernaux->vna_next = sizeof(Elf_Vernaux);
3860 ++vernaux;
3861 }
3862
3863 vernaux[-1].vna_next = 0;
3864 }
3865 verneed[-1].vn_next = 0;
3866}
3867
3868template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
3869 return verneeds.size() * sizeof(Elf_Verneed) +
3870 ctx.vernauxNum * sizeof(Elf_Vernaux);
3871}
3872
3873template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
3874 return isLive() && ctx.vernauxNum != 0;
3875}
3876
3877void MergeSyntheticSection::addSection(MergeInputSection *ms) {
3878 ms->parent = this;
3879 sections.push_back(Elt: ms);
3880 assert(addralign == ms->addralign || !(ms->flags & SHF_STRINGS));
3881 addralign = std::max(a: addralign, b: ms->addralign);
3882}
3883
3884MergeTailSection::MergeTailSection(Ctx &ctx, StringRef name, uint32_t type,
3885 uint64_t flags, uint32_t alignment)
3886 : MergeSyntheticSection(ctx, name, type, flags, alignment),
3887 builder(StringTableBuilder::RAW, llvm::Align(alignment)) {}
3888
3889size_t MergeTailSection::getSize() const { return builder.getSize(); }
3890
3891void MergeTailSection::writeTo(uint8_t *buf) { builder.write(Buf: buf); }
3892
3893void MergeTailSection::finalizeContents() {
3894 // Add all string pieces to the string table builder to create section
3895 // contents.
3896 for (MergeInputSection *sec : sections)
3897 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3898 if (sec->pieces[i].live)
3899 builder.add(S: sec->getData(i));
3900
3901 // Fix the string table content. After this, the contents will never change.
3902 builder.finalize();
3903
3904 // finalize() fixed tail-optimized strings, so we can now get
3905 // offsets of strings. Get an offset for each string and save it
3906 // to a corresponding SectionPiece for easy access.
3907 for (MergeInputSection *sec : sections)
3908 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3909 if (sec->pieces[i].live)
3910 sec->pieces[i].outputOff = builder.getOffset(S: sec->getData(i));
3911}
3912
3913void MergeNoTailSection::writeTo(uint8_t *buf) {
3914 parallelFor(Begin: 0, End: numShards,
3915 Fn: [&](size_t i) { shards[i].write(Buf: buf + shardOffsets[i]); });
3916}
3917
3918// This function is very hot (i.e. it can take several seconds to finish)
3919// because sometimes the number of inputs is in an order of magnitude of
3920// millions. So, we use multi-threading.
3921//
3922// For any strings S and T, we know S is not mergeable with T if S's hash
3923// value is different from T's. If that's the case, we can safely put S and
3924// T into different string builders without worrying about merge misses.
3925// We do it in parallel.
3926void MergeNoTailSection::finalizeContents() {
3927 // Initializes string table builders.
3928 for (size_t i = 0; i < numShards; ++i)
3929 shards.emplace_back(Args: StringTableBuilder::RAW, Args: llvm::Align(addralign));
3930
3931 // Concurrency level. Must be a power of 2 to avoid expensive modulo
3932 // operations in the following tight loop.
3933 const size_t concurrency =
3934 llvm::bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
3935
3936 // Add section pieces to the builders.
3937 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
3938 for (MergeInputSection *sec : sections) {
3939 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) {
3940 if (!sec->pieces[i].live)
3941 continue;
3942 size_t shardId = getShardId(hash: sec->pieces[i].hash);
3943 if ((shardId & (concurrency - 1)) == threadId)
3944 sec->pieces[i].outputOff = shards[shardId].add(S: sec->getData(i));
3945 }
3946 }
3947 });
3948
3949 // Compute an in-section offset for each shard.
3950 size_t off = 0;
3951 for (size_t i = 0; i < numShards; ++i) {
3952 shards[i].finalizeInOrder();
3953 if (shards[i].getSize() > 0)
3954 off = alignToPowerOf2(Value: off, Align: addralign);
3955 shardOffsets[i] = off;
3956 off += shards[i].getSize();
3957 }
3958 size = off;
3959
3960 // So far, section pieces have offsets from beginning of shards, but
3961 // we want offsets from beginning of the whole section. Fix them.
3962 parallelForEach(R&: sections, Fn: [&](MergeInputSection *sec) {
3963 for (SectionPiece &piece : sec->pieces)
3964 if (piece.live)
3965 piece.outputOff += shardOffsets[getShardId(hash: piece.hash)];
3966 });
3967}
3968
3969template <class ELFT> void elf::splitSections(Ctx &ctx) {
3970 llvm::TimeTraceScope timeScope("Split sections");
3971 // splitIntoPieces needs to be called on each MergeInputSection
3972 // before calling finalizeContents().
3973 parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
3974 for (InputSectionBase *sec : file->getSections()) {
3975 if (!sec)
3976 continue;
3977 if (auto *s = dyn_cast<MergeInputSection>(Val: sec))
3978 s->splitIntoPieces();
3979 else if (auto *eh = dyn_cast<EhInputSection>(Val: sec))
3980 eh->split<ELFT>();
3981 }
3982 });
3983}
3984
3985void elf::combineEhSections(Ctx &ctx) {
3986 llvm::TimeTraceScope timeScope("Combine EH sections");
3987 for (EhInputSection *sec : ctx.ehInputSections) {
3988 EhFrameSection &eh = *sec->getPartition(ctx).ehFrame;
3989 sec->parent = &eh;
3990 eh.addralign = std::max(a: eh.addralign, b: sec->addralign);
3991 eh.sections.push_back(Elt: sec);
3992 llvm::append_range(C&: eh.dependentSections, R&: sec->dependentSections);
3993 }
3994
3995 if (!ctx.mainPart->armExidx)
3996 return;
3997 llvm::erase_if(C&: ctx.inputSections, P: [&](InputSectionBase *s) {
3998 // Ignore dead sections and the partition end marker (.part.end),
3999 // whose partition number is out of bounds.
4000 if (!s->isLive() || s->partition == 255)
4001 return false;
4002 Partition &part = s->getPartition(ctx);
4003 return s->kind() == SectionBase::Regular && part.armExidx &&
4004 part.armExidx->addSection(isec: cast<InputSection>(Val: s));
4005 });
4006}
4007
4008MipsRldMapSection::MipsRldMapSection(Ctx &ctx)
4009 : SyntheticSection(ctx, ".rld_map", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
4010 ctx.arg.wordsize) {}
4011
4012ARMExidxSyntheticSection::ARMExidxSyntheticSection(Ctx &ctx)
4013 : SyntheticSection(ctx, ".ARM.exidx", SHT_ARM_EXIDX,
4014 SHF_ALLOC | SHF_LINK_ORDER, ctx.arg.wordsize) {}
4015
4016static InputSection *findExidxSection(InputSection *isec) {
4017 for (InputSection *d : isec->dependentSections)
4018 if (d->type == SHT_ARM_EXIDX && d->isLive())
4019 return d;
4020 return nullptr;
4021}
4022
4023static bool isValidExidxSectionDep(InputSection *isec) {
4024 return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) &&
4025 isec->getSize() > 0;
4026}
4027
4028bool ARMExidxSyntheticSection::addSection(InputSection *isec) {
4029 if (isec->type == SHT_ARM_EXIDX) {
4030 if (InputSection *dep = isec->getLinkOrderDep())
4031 if (isValidExidxSectionDep(isec: dep)) {
4032 exidxSections.push_back(Elt: isec);
4033 // Every exidxSection is 8 bytes, we need an estimate of
4034 // size before assignAddresses can be called. Final size
4035 // will only be known after finalize is called.
4036 size += 8;
4037 }
4038 return true;
4039 }
4040
4041 if (isValidExidxSectionDep(isec)) {
4042 executableSections.push_back(Elt: isec);
4043 return false;
4044 }
4045
4046 // FIXME: we do not output a relocation section when --emit-relocs is used
4047 // as we do not have relocation sections for linker generated table entries
4048 // and we would have to erase at a late stage relocations from merged entries.
4049 // Given that exception tables are already position independent and a binary
4050 // analyzer could derive the relocations we choose to erase the relocations.
4051 if (ctx.arg.emitRelocs && isec->type == SHT_REL)
4052 if (InputSectionBase *ex = isec->getRelocatedSection())
4053 if (isa<InputSection>(Val: ex) && ex->type == SHT_ARM_EXIDX)
4054 return true;
4055
4056 return false;
4057}
4058
4059// References to .ARM.Extab Sections have bit 31 clear and are not the
4060// special EXIDX_CANTUNWIND bit-pattern.
4061static bool isExtabRef(uint32_t unwind) {
4062 return (unwind & 0x80000000) == 0 && unwind != 0x1;
4063}
4064
4065// Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx
4066// section Prev, where Cur follows Prev in the table. This can be done if the
4067// unwinding instructions in Cur are identical to Prev. Linker generated
4068// EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an
4069// InputSection.
4070static bool isDuplicateArmExidxSec(Ctx &ctx, InputSection *prev,
4071 InputSection *cur) {
4072 // Get the last table Entry from the previous .ARM.exidx section. If Prev is
4073 // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.
4074 uint32_t prevUnwind = 1;
4075 if (prev)
4076 prevUnwind =
4077 read32(ctx, p: prev->content().data() + prev->content().size() - 4);
4078 if (isExtabRef(unwind: prevUnwind))
4079 return false;
4080
4081 // We consider the unwind instructions of an .ARM.exidx table entry
4082 // a duplicate if the previous unwind instructions if:
4083 // - Both are the special EXIDX_CANTUNWIND.
4084 // - Both are the same inline unwind instructions.
4085 // We do not attempt to follow and check links into .ARM.extab tables as
4086 // consecutive identical entries are rare and the effort to check that they
4087 // are identical is high.
4088
4089 // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.
4090 if (cur == nullptr)
4091 return prevUnwind == 1;
4092
4093 for (uint32_t offset = 4; offset < (uint32_t)cur->content().size(); offset +=8) {
4094 uint32_t curUnwind = read32(ctx, p: cur->content().data() + offset);
4095 if (isExtabRef(unwind: curUnwind) || curUnwind != prevUnwind)
4096 return false;
4097 }
4098 // All table entries in this .ARM.exidx Section can be merged into the
4099 // previous Section.
4100 return true;
4101}
4102
4103// The .ARM.exidx table must be sorted in ascending order of the address of the
4104// functions the table describes. std::optionally duplicate adjacent table
4105// entries can be removed. At the end of the function the executableSections
4106// must be sorted in ascending order of address, Sentinel is set to the
4107// InputSection with the highest address and any InputSections that have
4108// mergeable .ARM.exidx table entries are removed from it.
4109void ARMExidxSyntheticSection::finalizeContents() {
4110 // Ensure that any fixed-point iterations after the first see the original set
4111 // of sections.
4112 if (!originalExecutableSections.empty())
4113 executableSections = originalExecutableSections;
4114 else if (ctx.arg.enableNonContiguousRegions)
4115 originalExecutableSections = executableSections;
4116
4117 // The executableSections and exidxSections that we use to derive the final
4118 // contents of this SyntheticSection are populated before
4119 // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or
4120 // ICF may remove executable InputSections and their dependent .ARM.exidx
4121 // section that we recorded earlier.
4122 auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); };
4123 llvm::erase_if(C&: exidxSections, P: isDiscarded);
4124 // We need to remove discarded InputSections and InputSections without
4125 // .ARM.exidx sections that if we generated the .ARM.exidx it would be out
4126 // of range.
4127 auto isDiscardedOrOutOfRange = [this](InputSection *isec) {
4128 if (!isec->isLive())
4129 return true;
4130 if (findExidxSection(isec))
4131 return false;
4132 int64_t off = static_cast<int64_t>(isec->getVA() - getVA());
4133 return off != llvm::SignExtend64(X: off, B: 31);
4134 };
4135 llvm::erase_if(C&: executableSections, P: isDiscardedOrOutOfRange);
4136
4137 // Sort the executable sections that may or may not have associated
4138 // .ARM.exidx sections by order of ascending address. This requires the
4139 // relative positions of InputSections and OutputSections to be known.
4140 auto compareByFilePosition = [](const InputSection *a,
4141 const InputSection *b) {
4142 OutputSection *aOut = a->getParent();
4143 OutputSection *bOut = b->getParent();
4144
4145 if (aOut != bOut)
4146 return aOut->addr < bOut->addr;
4147 return a->outSecOff < b->outSecOff;
4148 };
4149 llvm::stable_sort(Range&: executableSections, C: compareByFilePosition);
4150 sentinel = executableSections.back();
4151 // std::optionally merge adjacent duplicate entries.
4152 if (ctx.arg.mergeArmExidx) {
4153 SmallVector<InputSection *, 0> selectedSections;
4154 selectedSections.reserve(N: executableSections.size());
4155 selectedSections.push_back(Elt: executableSections[0]);
4156 size_t prev = 0;
4157 for (size_t i = 1; i < executableSections.size(); ++i) {
4158 InputSection *ex1 = findExidxSection(isec: executableSections[prev]);
4159 InputSection *ex2 = findExidxSection(isec: executableSections[i]);
4160 if (!isDuplicateArmExidxSec(ctx, prev: ex1, cur: ex2)) {
4161 selectedSections.push_back(Elt: executableSections[i]);
4162 prev = i;
4163 }
4164 }
4165 executableSections = std::move(selectedSections);
4166 }
4167 // offset is within the SyntheticSection.
4168 size_t offset = 0;
4169 size = 0;
4170 for (InputSection *isec : executableSections) {
4171 if (InputSection *d = findExidxSection(isec)) {
4172 d->outSecOff = offset;
4173 d->parent = getParent();
4174 offset += d->getSize();
4175 } else {
4176 offset += 8;
4177 }
4178 }
4179 // Size includes Sentinel.
4180 size = offset + 8;
4181}
4182
4183InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const {
4184 return executableSections.front();
4185}
4186
4187// To write the .ARM.exidx table from the ExecutableSections we have three cases
4188// 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.
4189// We write the .ARM.exidx section contents and apply its relocations.
4190// 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We
4191// must write the contents of an EXIDX_CANTUNWIND directly. We use the
4192// start of the InputSection as the purpose of the linker generated
4193// section is to terminate the address range of the previous entry.
4194// 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of
4195// the table to terminate the address range of the final entry.
4196void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
4197
4198 // A linker generated CANTUNWIND entry is made up of two words:
4199 // 0x0 with R_ARM_PREL31 relocation to target.
4200 // 0x1 with EXIDX_CANTUNWIND.
4201 uint64_t offset = 0;
4202 for (InputSection *isec : executableSections) {
4203 assert(isec->getParent() != nullptr);
4204 if (InputSection *d = findExidxSection(isec)) {
4205 for (int dataOffset = 0; dataOffset != (int)d->content().size();
4206 dataOffset += 4)
4207 write32(ctx, p: buf + offset + dataOffset,
4208 v: read32(ctx, p: d->content().data() + dataOffset));
4209 // Recalculate outSecOff as finalizeAddressDependentContent()
4210 // may have altered syntheticSection outSecOff.
4211 d->outSecOff = offset + outSecOff;
4212 ctx.target->relocateAlloc(sec&: *d, buf: buf + offset);
4213 offset += d->getSize();
4214 } else {
4215 // A Linker generated CANTUNWIND section.
4216 write32(ctx, p: buf + offset + 0, v: 0x0);
4217 write32(ctx, p: buf + offset + 4, v: 0x1);
4218 uint64_t s = isec->getVA();
4219 uint64_t p = getVA() + offset;
4220 ctx.target->relocateNoSym(loc: buf + offset, type: R_ARM_PREL31, val: s - p);
4221 offset += 8;
4222 }
4223 }
4224 // Write Sentinel CANTUNWIND entry.
4225 write32(ctx, p: buf + offset + 0, v: 0x0);
4226 write32(ctx, p: buf + offset + 4, v: 0x1);
4227 uint64_t s = sentinel->getVA(offset: sentinel->getSize());
4228 uint64_t p = getVA() + offset;
4229 ctx.target->relocateNoSym(loc: buf + offset, type: R_ARM_PREL31, val: s - p);
4230 assert(size == offset + 8);
4231}
4232
4233bool ARMExidxSyntheticSection::isNeeded() const {
4234 return llvm::any_of(Range: exidxSections,
4235 P: [](InputSection *isec) { return isec->isLive(); });
4236}
4237
4238ThunkSection::ThunkSection(Ctx &ctx, OutputSection *os, uint64_t off)
4239 : SyntheticSection(ctx, ".text.thunk", SHT_PROGBITS,
4240 SHF_ALLOC | SHF_EXECINSTR,
4241 ctx.arg.emachine == EM_PPC64 ? 16 : 4) {
4242 this->parent = os;
4243 this->outSecOff = off;
4244}
4245
4246size_t ThunkSection::getSize() const {
4247 if (roundUpSizeForErrata)
4248 return alignTo(Value: size, Align: 4096);
4249 return size;
4250}
4251
4252void ThunkSection::addThunk(Thunk *t) {
4253 thunks.push_back(Elt: t);
4254 t->addSymbols(isec&: *this);
4255}
4256
4257void ThunkSection::writeTo(uint8_t *buf) {
4258 for (Thunk *t : thunks)
4259 t->writeTo(buf: buf + t->offset);
4260}
4261
4262InputSection *ThunkSection::getTargetInputSection() const {
4263 if (thunks.empty())
4264 return nullptr;
4265 const Thunk *t = thunks.front();
4266 return t->getTargetInputSection();
4267}
4268
4269bool ThunkSection::assignOffsets() {
4270 uint64_t off = 0;
4271 bool changed = false;
4272 for (Thunk *t : thunks) {
4273 if (t->alignment > addralign) {
4274 addralign = t->alignment;
4275 changed = true;
4276 }
4277 off = alignToPowerOf2(Value: off, Align: t->alignment);
4278 t->setOffset(off);
4279 uint32_t size = t->size();
4280 t->getThunkTargetSym()->size = size;
4281 off += size;
4282 }
4283 if (off != size)
4284 changed = true;
4285 size = off;
4286 return changed;
4287}
4288
4289PPC32Got2Section::PPC32Got2Section(Ctx &ctx)
4290 : SyntheticSection(ctx, ".got2", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 4) {}
4291
4292bool PPC32Got2Section::isNeeded() const {
4293 // See the comment below. This is not needed if there is no other
4294 // InputSection.
4295 for (SectionCommand *cmd : getParent()->commands)
4296 if (auto *isd = dyn_cast<InputSectionDescription>(Val: cmd))
4297 for (InputSection *isec : isd->sections)
4298 if (isec != this)
4299 return true;
4300 return false;
4301}
4302
4303void PPC32Got2Section::finalizeContents() {
4304 // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in
4305 // .got2 . This function computes outSecOff of each .got2 to be used in
4306 // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is
4307 // to collect input sections named ".got2".
4308 for (SectionCommand *cmd : getParent()->commands)
4309 if (auto *isd = dyn_cast<InputSectionDescription>(Val: cmd)) {
4310 for (InputSection *isec : isd->sections) {
4311 // isec->file may be nullptr for MergeSyntheticSection.
4312 if (isec != this && isec->file)
4313 isec->file->ppc32Got2 = isec;
4314 }
4315 }
4316}
4317
4318// If linking position-dependent code then the table will store the addresses
4319// directly in the binary so the section has type SHT_PROGBITS. If linking
4320// position-independent code the section has type SHT_NOBITS since it will be
4321// allocated and filled in by the dynamic linker.
4322PPC64LongBranchTargetSection::PPC64LongBranchTargetSection(Ctx &ctx)
4323 : SyntheticSection(ctx, ".branch_lt",
4324 ctx.arg.isPic ? SHT_NOBITS : SHT_PROGBITS,
4325 SHF_ALLOC | SHF_WRITE, 8) {}
4326
4327uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol *sym,
4328 int64_t addend) {
4329 return getVA() + entry_index.find(Val: {sym, addend})->second * 8;
4330}
4331
4332std::optional<uint32_t>
4333PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) {
4334 auto res =
4335 entry_index.try_emplace(Key: std::make_pair(x&: sym, y&: addend), Args: entries.size());
4336 if (!res.second)
4337 return std::nullopt;
4338 entries.emplace_back(Args&: sym, Args&: addend);
4339 return res.first->second;
4340}
4341
4342size_t PPC64LongBranchTargetSection::getSize() const {
4343 return entries.size() * 8;
4344}
4345
4346void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) {
4347 // If linking non-pic we have the final addresses of the targets and they get
4348 // written to the table directly. For pic the dynamic linker will allocate
4349 // the section and fill it.
4350 if (ctx.arg.isPic)
4351 return;
4352
4353 for (auto entry : entries) {
4354 const Symbol *sym = entry.first;
4355 int64_t addend = entry.second;
4356 assert(sym->getVA(ctx));
4357 // Need calls to branch to the local entry-point since a long-branch
4358 // must be a local-call.
4359 write64(ctx, p: buf,
4360 v: sym->getVA(ctx, addend) +
4361 getPPC64GlobalEntryToLocalEntryOffset(ctx, stOther: sym->stOther));
4362 buf += 8;
4363 }
4364}
4365
4366bool PPC64LongBranchTargetSection::isNeeded() const {
4367 // `removeUnusedSyntheticSections()` is called before thunk allocation which
4368 // is too early to determine if this section will be empty or not. We need
4369 // Finalized to keep the section alive until after thunk creation. Finalized
4370 // only gets set to true once `finalizeSections()` is called after thunk
4371 // creation. Because of this, if we don't create any long-branch thunks we end
4372 // up with an empty .branch_lt section in the binary.
4373 return !finalized || !entries.empty();
4374}
4375
4376static uint8_t getAbiVersion(Ctx &ctx) {
4377 // MIPS non-PIC executable gets ABI version 1.
4378 if (ctx.arg.emachine == EM_MIPS) {
4379 if (!ctx.arg.isPic && !ctx.arg.relocatable &&
4380 (ctx.arg.eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
4381 return 1;
4382 return 0;
4383 }
4384
4385 if (ctx.arg.emachine == EM_AMDGPU && !ctx.objectFiles.empty()) {
4386 uint8_t ver = ctx.objectFiles[0]->abiVersion;
4387 for (InputFile *file : ArrayRef(ctx.objectFiles).slice(N: 1))
4388 if (file->abiVersion != ver)
4389 Err(ctx) << "incompatible ABI version: " << file;
4390 return ver;
4391 }
4392
4393 return 0;
4394}
4395
4396template <typename ELFT>
4397void elf::writeEhdr(Ctx &ctx, uint8_t *buf, Partition &part) {
4398 memcpy(dest: buf, src: "\177ELF", n: 4);
4399
4400 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4401 eHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
4402 eHdr->e_ident[EI_DATA] =
4403 ELFT::Endianness == endianness::little ? ELFDATA2LSB : ELFDATA2MSB;
4404 eHdr->e_ident[EI_VERSION] = EV_CURRENT;
4405 eHdr->e_ident[EI_OSABI] = ctx.arg.osabi;
4406 eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(ctx);
4407 eHdr->e_machine = ctx.arg.emachine;
4408 eHdr->e_version = EV_CURRENT;
4409 eHdr->e_flags = ctx.arg.eflags;
4410 eHdr->e_ehsize = sizeof(typename ELFT::Ehdr);
4411 eHdr->e_phnum = part.phdrs.size();
4412 eHdr->e_shentsize = sizeof(typename ELFT::Shdr);
4413
4414 if (!ctx.arg.relocatable) {
4415 eHdr->e_phoff = sizeof(typename ELFT::Ehdr);
4416 eHdr->e_phentsize = sizeof(typename ELFT::Phdr);
4417 }
4418}
4419
4420template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) {
4421 // Write the program header table.
4422 auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf);
4423 for (std::unique_ptr<PhdrEntry> &p : part.phdrs) {
4424 hBuf->p_type = p->p_type;
4425 hBuf->p_flags = p->p_flags;
4426 hBuf->p_offset = p->p_offset;
4427 hBuf->p_vaddr = p->p_vaddr;
4428 hBuf->p_paddr = p->p_paddr;
4429 hBuf->p_filesz = p->p_filesz;
4430 hBuf->p_memsz = p->p_memsz;
4431 hBuf->p_align = p->p_align;
4432 ++hBuf;
4433 }
4434}
4435
4436template <typename ELFT>
4437PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection(Ctx &ctx)
4438 : SyntheticSection(ctx, "", SHT_LLVM_PART_EHDR, SHF_ALLOC, 1) {}
4439
4440template <typename ELFT>
4441size_t PartitionElfHeaderSection<ELFT>::getSize() const {
4442 return sizeof(typename ELFT::Ehdr);
4443}
4444
4445template <typename ELFT>
4446void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4447 writeEhdr<ELFT>(ctx, buf, getPartition(ctx));
4448
4449 // Loadable partitions are always ET_DYN.
4450 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4451 eHdr->e_type = ET_DYN;
4452}
4453
4454template <typename ELFT>
4455PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection(Ctx &ctx)
4456 : SyntheticSection(ctx, ".phdrs", SHT_LLVM_PART_PHDR, SHF_ALLOC, 1) {}
4457
4458template <typename ELFT>
4459size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
4460 return sizeof(typename ELFT::Phdr) * getPartition(ctx).phdrs.size();
4461}
4462
4463template <typename ELFT>
4464void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {
4465 writePhdrs<ELFT>(buf, getPartition(ctx));
4466}
4467
4468PartitionIndexSection::PartitionIndexSection(Ctx &ctx)
4469 : SyntheticSection(ctx, ".rodata", SHT_PROGBITS, SHF_ALLOC, 4) {}
4470
4471size_t PartitionIndexSection::getSize() const {
4472 return 12 * (ctx.partitions.size() - 1);
4473}
4474
4475void PartitionIndexSection::finalizeContents() {
4476 for (size_t i = 1; i != ctx.partitions.size(); ++i)
4477 ctx.partitions[i].nameStrTab =
4478 ctx.mainPart->dynStrTab->addString(s: ctx.partitions[i].name);
4479}
4480
4481void PartitionIndexSection::writeTo(uint8_t *buf) {
4482 uint64_t va = getVA();
4483 for (size_t i = 1; i != ctx.partitions.size(); ++i) {
4484 write32(ctx, p: buf,
4485 v: ctx.mainPart->dynStrTab->getVA() + ctx.partitions[i].nameStrTab -
4486 va);
4487 write32(ctx, p: buf + 4, v: ctx.partitions[i].elfHeader->getVA() - (va + 4));
4488
4489 SyntheticSection *next = i == ctx.partitions.size() - 1
4490 ? ctx.in.partEnd.get()
4491 : ctx.partitions[i + 1].elfHeader.get();
4492 write32(ctx, p: buf + 8, v: next->getVA() - ctx.partitions[i].elfHeader->getVA());
4493
4494 va += 12;
4495 buf += 12;
4496 }
4497}
4498
4499static bool needsInterpSection(Ctx &ctx) {
4500 return !ctx.arg.relocatable && !ctx.arg.shared &&
4501 !ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
4502}
4503
4504bool elf::hasMemtag(Ctx &ctx) {
4505 return ctx.arg.emachine == EM_AARCH64 &&
4506 ctx.arg.androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
4507}
4508
4509// Fully static executables don't support MTE globals at this point in time, as
4510// we currently rely on:
4511// - A dynamic loader to process relocations, and
4512// - Dynamic entries.
4513// This restriction could be removed in future by re-using some of the ideas
4514// that ifuncs use in fully static executables.
4515bool elf::canHaveMemtagGlobals(Ctx &ctx) {
4516 return hasMemtag(ctx) &&
4517 (ctx.arg.relocatable || ctx.arg.shared || needsInterpSection(ctx));
4518}
4519
4520constexpr char kMemtagAndroidNoteName[] = "Android";
4521void MemtagAndroidNote::writeTo(uint8_t *buf) {
4522 static_assert(
4523 sizeof(kMemtagAndroidNoteName) == 8,
4524 "Android 11 & 12 have an ABI that the note name is 8 bytes long. Keep it "
4525 "that way for backwards compatibility.");
4526
4527 write32(ctx, p: buf, v: sizeof(kMemtagAndroidNoteName));
4528 write32(ctx, p: buf + 4, v: sizeof(uint32_t));
4529 write32(ctx, p: buf + 8, v: ELF::NT_ANDROID_TYPE_MEMTAG);
4530 memcpy(dest: buf + 12, src: kMemtagAndroidNoteName, n: sizeof(kMemtagAndroidNoteName));
4531 buf += 12 + alignTo(Value: sizeof(kMemtagAndroidNoteName), Align: 4);
4532
4533 uint32_t value = 0;
4534 value |= ctx.arg.androidMemtagMode;
4535 if (ctx.arg.androidMemtagHeap)
4536 value |= ELF::NT_MEMTAG_HEAP;
4537 // Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled
4538 // binary on Android 11 or 12 will result in a checkfail in the loader.
4539 if (ctx.arg.androidMemtagStack)
4540 value |= ELF::NT_MEMTAG_STACK;
4541 write32(ctx, p: buf, v: value); // note value
4542}
4543
4544size_t MemtagAndroidNote::getSize() const {
4545 return sizeof(llvm::ELF::Elf64_Nhdr) +
4546 /*namesz=*/alignTo(Value: sizeof(kMemtagAndroidNoteName), Align: 4) +
4547 /*descsz=*/sizeof(uint32_t);
4548}
4549
4550void PackageMetadataNote::writeTo(uint8_t *buf) {
4551 write32(ctx, p: buf, v: 4);
4552 write32(ctx, p: buf + 4, v: ctx.arg.packageMetadata.size() + 1);
4553 write32(ctx, p: buf + 8, v: FDO_PACKAGING_METADATA);
4554 memcpy(dest: buf + 12, src: "FDO", n: 4);
4555 memcpy(dest: buf + 16, src: ctx.arg.packageMetadata.data(),
4556 n: ctx.arg.packageMetadata.size());
4557}
4558
4559size_t PackageMetadataNote::getSize() const {
4560 return sizeof(llvm::ELF::Elf64_Nhdr) + 4 +
4561 alignTo(Value: ctx.arg.packageMetadata.size() + 1, Align: 4);
4562}
4563
4564// Helper function, return the size of the ULEB128 for 'v', optionally writing
4565// it to `*(buf + offset)` if `buf` is non-null.
4566static size_t computeOrWriteULEB128(uint64_t v, uint8_t *buf, size_t offset) {
4567 if (buf)
4568 return encodeULEB128(Value: v, p: buf + offset);
4569 return getULEB128Size(Value: v);
4570}
4571
4572// https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic
4573constexpr uint64_t kMemtagStepSizeBits = 3;
4574constexpr uint64_t kMemtagGranuleSize = 16;
4575static size_t
4576createMemtagGlobalDescriptors(Ctx &ctx,
4577 const SmallVector<const Symbol *, 0> &symbols,
4578 uint8_t *buf = nullptr) {
4579 size_t sectionSize = 0;
4580 uint64_t lastGlobalEnd = 0;
4581
4582 for (const Symbol *sym : symbols) {
4583 if (!includeInSymtab(ctx, *sym))
4584 continue;
4585 const uint64_t addr = sym->getVA(ctx);
4586 const uint64_t size = sym->getSize();
4587
4588 if (addr <= kMemtagGranuleSize && buf != nullptr)
4589 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4590 << "\" falls in the ELF header. This is indicative of a "
4591 "compiler/linker bug";
4592 if (addr % kMemtagGranuleSize != 0)
4593 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4594 << "\" at 0x" << Twine::utohexstr(Val: addr)
4595 << "\" is not granule (16-byte) aligned";
4596 if (size == 0)
4597 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4598 << "\" is not allowed to be zero";
4599 if (size % kMemtagGranuleSize != 0)
4600 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4601 << "\" (size 0x" << Twine::utohexstr(Val: size)
4602 << ") is not granule (16-byte) aligned";
4603
4604 const uint64_t sizeToEncode = size / kMemtagGranuleSize;
4605 const uint64_t stepToEncode = ((addr - lastGlobalEnd) / kMemtagGranuleSize)
4606 << kMemtagStepSizeBits;
4607 if (sizeToEncode < (1 << kMemtagStepSizeBits)) {
4608 sectionSize += computeOrWriteULEB128(v: stepToEncode | sizeToEncode, buf, offset: sectionSize);
4609 } else {
4610 sectionSize += computeOrWriteULEB128(v: stepToEncode, buf, offset: sectionSize);
4611 sectionSize += computeOrWriteULEB128(v: sizeToEncode - 1, buf, offset: sectionSize);
4612 }
4613 lastGlobalEnd = addr + size;
4614 }
4615
4616 return sectionSize;
4617}
4618
4619bool MemtagGlobalDescriptors::updateAllocSize(Ctx &ctx) {
4620 size_t oldSize = getSize();
4621 llvm::stable_sort(Range&: symbols, C: [&ctx = ctx](const Symbol *s1, const Symbol *s2) {
4622 return s1->getVA(ctx) < s2->getVA(ctx);
4623 });
4624 return oldSize != getSize();
4625}
4626
4627void MemtagGlobalDescriptors::writeTo(uint8_t *buf) {
4628 createMemtagGlobalDescriptors(ctx, symbols, buf);
4629}
4630
4631size_t MemtagGlobalDescriptors::getSize() const {
4632 return createMemtagGlobalDescriptors(ctx, symbols);
4633}
4634
4635static OutputSection *findSection(Ctx &ctx, StringRef name) {
4636 for (SectionCommand *cmd : ctx.script->sectionCommands)
4637 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
4638 if (osd->osec.name == name)
4639 return &osd->osec;
4640 return nullptr;
4641}
4642
4643static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
4644 uint64_t val, uint8_t stOther = STV_HIDDEN) {
4645 Symbol *s = ctx.symtab->find(name);
4646 if (!s || s->isDefined() || s->isCommon())
4647 return nullptr;
4648
4649 s->resolve(ctx, other: Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
4650 stOther, STT_NOTYPE, val,
4651 /*size=*/0, sec});
4652 s->isUsedInRegularObj = true;
4653 return cast<Defined>(Val: s);
4654}
4655
4656template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
4657 // Add the .interp section first because it is not a SyntheticSection.
4658 // The removeUnusedSyntheticSections() function relies on the
4659 // SyntheticSections coming last.
4660 if (needsInterpSection(ctx)) {
4661 for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
4662 InputSection *sec = createInterpSection(ctx);
4663 sec->partition = i;
4664 ctx.inputSections.push_back(Elt: sec);
4665 }
4666 }
4667
4668 auto add = [&](SyntheticSection &sec) { ctx.inputSections.push_back(Elt: &sec); };
4669
4670 if (ctx.arg.zSectionHeader)
4671 ctx.in.shStrTab =
4672 std::make_unique<StringTableSection>(args&: ctx, args: ".shstrtab", args: false);
4673
4674 ctx.out.programHeaders =
4675 std::make_unique<OutputSection>(args&: ctx, args: "", args: 0, args: SHF_ALLOC);
4676 ctx.out.programHeaders->addralign = ctx.arg.wordsize;
4677
4678 if (ctx.arg.strip != StripPolicy::All) {
4679 ctx.in.strTab = std::make_unique<StringTableSection>(args&: ctx, args: ".strtab", args: false);
4680 ctx.in.symTab =
4681 std::make_unique<SymbolTableSection<ELFT>>(ctx, *ctx.in.strTab);
4682 ctx.in.symTabShndx = std::make_unique<SymtabShndxSection>(args&: ctx);
4683 }
4684
4685 ctx.in.bss = std::make_unique<BssSection>(args&: ctx, args: ".bss", args: 0, args: 1);
4686 add(*ctx.in.bss);
4687
4688 // If there is a SECTIONS command and a .data.rel.ro section name use name
4689 // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
4690 // This makes sure our relro is contiguous.
4691 bool hasDataRelRo =
4692 ctx.script->hasSectionsCommand && findSection(ctx, name: ".data.rel.ro");
4693 ctx.in.bssRelRo = std::make_unique<BssSection>(
4694 args&: ctx, args: hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", args: 0, args: 1);
4695 add(*ctx.in.bssRelRo);
4696
4697 // Add MIPS-specific sections.
4698 if (ctx.arg.emachine == EM_MIPS) {
4699 if (!ctx.arg.shared && ctx.hasDynsym) {
4700 ctx.in.mipsRldMap = std::make_unique<MipsRldMapSection>(args&: ctx);
4701 add(*ctx.in.mipsRldMap);
4702 }
4703 if ((ctx.in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create(ctx)))
4704 add(*ctx.in.mipsAbiFlags);
4705 if ((ctx.in.mipsOptions = MipsOptionsSection<ELFT>::create(ctx)))
4706 add(*ctx.in.mipsOptions);
4707 if ((ctx.in.mipsReginfo = MipsReginfoSection<ELFT>::create(ctx)))
4708 add(*ctx.in.mipsReginfo);
4709 }
4710
4711 StringRef relaDynName = ctx.arg.isRela ? ".rela.dyn" : ".rel.dyn";
4712
4713 const unsigned threadCount = ctx.arg.threadCount;
4714 for (Partition &part : ctx.partitions) {
4715 auto add = [&](SyntheticSection &sec) {
4716 sec.partition = part.getNumber(ctx);
4717 ctx.inputSections.push_back(Elt: &sec);
4718 };
4719
4720 if (!part.name.empty()) {
4721 part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>(ctx);
4722 part.elfHeader->name = part.name;
4723 add(*part.elfHeader);
4724
4725 part.programHeaders =
4726 std::make_unique<PartitionProgramHeadersSection<ELFT>>(ctx);
4727 add(*part.programHeaders);
4728 }
4729
4730 if (ctx.arg.buildId != BuildIdKind::None) {
4731 part.buildId = std::make_unique<BuildIdSection>(args&: ctx);
4732 add(*part.buildId);
4733 }
4734
4735 // dynSymTab is always present to simplify several finalizeSections
4736 // functions.
4737 part.dynStrTab = std::make_unique<StringTableSection>(args&: ctx, args: ".dynstr", args: true);
4738 part.dynSymTab =
4739 std::make_unique<SymbolTableSection<ELFT>>(ctx, *part.dynStrTab);
4740
4741 if (ctx.arg.relocatable)
4742 continue;
4743 part.dynamic = std::make_unique<DynamicSection<ELFT>>(ctx);
4744
4745 if (hasMemtag(ctx)) {
4746 part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>(args&: ctx);
4747 add(*part.memtagAndroidNote);
4748 if (canHaveMemtagGlobals(ctx)) {
4749 part.memtagGlobalDescriptors =
4750 std::make_unique<MemtagGlobalDescriptors>(args&: ctx);
4751 add(*part.memtagGlobalDescriptors);
4752 }
4753 }
4754
4755 if (ctx.arg.androidPackDynRelocs)
4756 part.relaDyn = std::make_unique<AndroidPackedRelocationSection<ELFT>>(
4757 ctx, relaDynName, threadCount);
4758 else
4759 part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
4760 ctx, relaDynName, ctx.arg.zCombreloc, threadCount);
4761
4762 if (ctx.hasDynsym) {
4763 add(*part.dynSymTab);
4764
4765 part.verSym = std::make_unique<VersionTableSection>(args&: ctx);
4766 add(*part.verSym);
4767
4768 if (!namedVersionDefs(ctx).empty()) {
4769 part.verDef = std::make_unique<VersionDefinitionSection>(args&: ctx);
4770 add(*part.verDef);
4771 }
4772
4773 part.verNeed = std::make_unique<VersionNeedSection<ELFT>>(ctx);
4774 add(*part.verNeed);
4775
4776 if (ctx.arg.gnuHash) {
4777 part.gnuHashTab = std::make_unique<GnuHashTableSection>(args&: ctx);
4778 add(*part.gnuHashTab);
4779 }
4780
4781 if (ctx.arg.sysvHash) {
4782 part.hashTab = std::make_unique<HashTableSection>(args&: ctx);
4783 add(*part.hashTab);
4784 }
4785
4786 add(*part.dynamic);
4787 add(*part.dynStrTab);
4788 }
4789 add(*part.relaDyn);
4790
4791 if (ctx.arg.relrPackDynRelocs) {
4792 part.relrDyn = std::make_unique<RelrSection<ELFT>>(ctx, threadCount);
4793 add(*part.relrDyn);
4794 part.relrAuthDyn = std::make_unique<RelrSection<ELFT>>(
4795 ctx, threadCount, /*isAArch64Auth=*/true);
4796 add(*part.relrAuthDyn);
4797 }
4798
4799 if (ctx.arg.ehFrameHdr) {
4800 part.ehFrameHdr = std::make_unique<EhFrameHeader>(args&: ctx);
4801 add(*part.ehFrameHdr);
4802 }
4803 part.ehFrame = std::make_unique<EhFrameSection>(args&: ctx);
4804 add(*part.ehFrame);
4805
4806 if (ctx.arg.emachine == EM_ARM) {
4807 // This section replaces all the individual .ARM.exidx InputSections.
4808 part.armExidx = std::make_unique<ARMExidxSyntheticSection>(args&: ctx);
4809 add(*part.armExidx);
4810 }
4811
4812 if (!ctx.arg.packageMetadata.empty()) {
4813 part.packageMetadataNote = std::make_unique<PackageMetadataNote>(args&: ctx);
4814 add(*part.packageMetadataNote);
4815 }
4816 }
4817
4818 if (ctx.partitions.size() != 1) {
4819 // Create the partition end marker. This needs to be in partition number 255
4820 // so that it is sorted after all other partitions. It also has other
4821 // special handling (see createPhdrs() and combineEhSections()).
4822 ctx.in.partEnd =
4823 std::make_unique<BssSection>(args&: ctx, args: ".part.end", args&: ctx.arg.maxPageSize, args: 1);
4824 ctx.in.partEnd->partition = 255;
4825 add(*ctx.in.partEnd);
4826
4827 ctx.in.partIndex = std::make_unique<PartitionIndexSection>(args&: ctx);
4828 addOptionalRegular(ctx, name: "__part_index_begin", sec: ctx.in.partIndex.get(), val: 0);
4829 addOptionalRegular(ctx, name: "__part_index_end", sec: ctx.in.partIndex.get(),
4830 val: ctx.in.partIndex->getSize());
4831 add(*ctx.in.partIndex);
4832 }
4833
4834 // Add .got. MIPS' .got is so different from the other archs,
4835 // it has its own class.
4836 if (ctx.arg.emachine == EM_MIPS) {
4837 ctx.in.mipsGot = std::make_unique<MipsGotSection>(args&: ctx);
4838 add(*ctx.in.mipsGot);
4839 } else {
4840 ctx.in.got = std::make_unique<GotSection>(args&: ctx);
4841 add(*ctx.in.got);
4842 }
4843
4844 if (ctx.arg.emachine == EM_PPC) {
4845 ctx.in.ppc32Got2 = std::make_unique<PPC32Got2Section>(args&: ctx);
4846 add(*ctx.in.ppc32Got2);
4847 }
4848
4849 if (ctx.arg.emachine == EM_PPC64) {
4850 ctx.in.ppc64LongBranchTarget =
4851 std::make_unique<PPC64LongBranchTargetSection>(args&: ctx);
4852 add(*ctx.in.ppc64LongBranchTarget);
4853 }
4854
4855 ctx.in.gotPlt = std::make_unique<GotPltSection>(args&: ctx);
4856 add(*ctx.in.gotPlt);
4857 ctx.in.igotPlt = std::make_unique<IgotPltSection>(args&: ctx);
4858 add(*ctx.in.igotPlt);
4859 // Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the
4860 // section in the absence of PHDRS/SECTIONS commands.
4861 if (ctx.arg.zRelro &&
4862 ((ctx.script->phdrsCommands.empty() && !ctx.script->hasSectionsCommand) ||
4863 ctx.script->seenRelroEnd)) {
4864 ctx.in.relroPadding = std::make_unique<RelroPaddingSection>(args&: ctx);
4865 add(*ctx.in.relroPadding);
4866 }
4867
4868 if (ctx.arg.emachine == EM_ARM) {
4869 ctx.in.armCmseSGSection = std::make_unique<ArmCmseSGSection>(args&: ctx);
4870 add(*ctx.in.armCmseSGSection);
4871 }
4872
4873 // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
4874 // it as a relocation and ensure the referenced section is created.
4875 if (ctx.sym.globalOffsetTable && ctx.arg.emachine != EM_MIPS) {
4876 if (ctx.target->gotBaseSymInGotPlt)
4877 ctx.in.gotPlt->hasGotPltOffRel = true;
4878 else
4879 ctx.in.got->hasGotOffRel = true;
4880 }
4881
4882 // We always need to add rel[a].plt to output if it has entries.
4883 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
4884 ctx.in.relaPlt = std::make_unique<RelocationSection<ELFT>>(
4885 ctx, ctx.arg.isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false,
4886 /*threadCount=*/1);
4887 add(*ctx.in.relaPlt);
4888
4889 if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
4890 (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
4891 ctx.in.ibtPlt = std::make_unique<IBTPltSection>(args&: ctx);
4892 add(*ctx.in.ibtPlt);
4893 }
4894
4895 if (ctx.arg.emachine == EM_PPC)
4896 ctx.in.plt = std::make_unique<PPC32GlinkSection>(args&: ctx);
4897 else
4898 ctx.in.plt = std::make_unique<PltSection>(args&: ctx);
4899 add(*ctx.in.plt);
4900 ctx.in.iplt = std::make_unique<IpltSection>(args&: ctx);
4901 add(*ctx.in.iplt);
4902
4903 if (ctx.arg.andFeatures || ctx.aarch64PauthAbiCoreInfo) {
4904 ctx.in.gnuProperty = std::make_unique<GnuPropertySection>(args&: ctx);
4905 add(*ctx.in.gnuProperty);
4906 }
4907
4908 if (ctx.arg.debugNames) {
4909 ctx.in.debugNames = std::make_unique<DebugNamesSection<ELFT>>(ctx);
4910 add(*ctx.in.debugNames);
4911 }
4912
4913 if (ctx.arg.gdbIndex) {
4914 ctx.in.gdbIndex = GdbIndexSection::create<ELFT>(ctx);
4915 add(*ctx.in.gdbIndex);
4916 }
4917
4918 // .note.GNU-stack is always added when we are creating a re-linkable
4919 // object file. Other linkers are using the presence of this marker
4920 // section to control the executable-ness of the stack area, but that
4921 // is irrelevant these days. Stack area should always be non-executable
4922 // by default. So we emit this section unconditionally.
4923 if (ctx.arg.relocatable) {
4924 ctx.in.gnuStack = std::make_unique<GnuStackSection>(args&: ctx);
4925 add(*ctx.in.gnuStack);
4926 }
4927
4928 if (ctx.in.symTab)
4929 add(*ctx.in.symTab);
4930 if (ctx.in.symTabShndx)
4931 add(*ctx.in.symTabShndx);
4932 if (ctx.in.shStrTab)
4933 add(*ctx.in.shStrTab);
4934 if (ctx.in.strTab)
4935 add(*ctx.in.strTab);
4936}
4937
4938template void elf::splitSections<ELF32LE>(Ctx &);
4939template void elf::splitSections<ELF32BE>(Ctx &);
4940template void elf::splitSections<ELF64LE>(Ctx &);
4941template void elf::splitSections<ELF64BE>(Ctx &);
4942
4943template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>(
4944 function_ref<void(InputSection &)>);
4945template void EhFrameSection::iterateFDEWithLSDA<ELF32BE>(
4946 function_ref<void(InputSection &)>);
4947template void EhFrameSection::iterateFDEWithLSDA<ELF64LE>(
4948 function_ref<void(InputSection &)>);
4949template void EhFrameSection::iterateFDEWithLSDA<ELF64BE>(
4950 function_ref<void(InputSection &)>);
4951
4952template class elf::SymbolTableSection<ELF32LE>;
4953template class elf::SymbolTableSection<ELF32BE>;
4954template class elf::SymbolTableSection<ELF64LE>;
4955template class elf::SymbolTableSection<ELF64BE>;
4956
4957template void elf::writeEhdr<ELF32LE>(Ctx &, uint8_t *Buf, Partition &Part);
4958template void elf::writeEhdr<ELF32BE>(Ctx &, uint8_t *Buf, Partition &Part);
4959template void elf::writeEhdr<ELF64LE>(Ctx &, uint8_t *Buf, Partition &Part);
4960template void elf::writeEhdr<ELF64BE>(Ctx &, uint8_t *Buf, Partition &Part);
4961
4962template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
4963template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
4964template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);
4965template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);
4966
4967template void elf::createSyntheticSections<ELF32LE>(Ctx &);
4968template void elf::createSyntheticSections<ELF32BE>(Ctx &);
4969template void elf::createSyntheticSections<ELF64LE>(Ctx &);
4970template void elf::createSyntheticSections<ELF64BE>(Ctx &);
4971