[mlir][py] Enable loading only specified dialects during creation. (#121421)
[llvm-project.git] / lld / ELF / SyntheticSections.cpp
blobbaa7a083404fe7fbb4b5ce28a7d1f64794c77aec
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.
14 //===----------------------------------------------------------------------===//
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/CommonLinkerContext.h"
29 #include "lld/Common/DWARF.h"
30 #include "lld/Common/Strings.h"
31 #include "lld/Common/Version.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/Sequence.h"
34 #include "llvm/ADT/SetOperations.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/BinaryFormat/Dwarf.h"
37 #include "llvm/BinaryFormat/ELF.h"
38 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
39 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
40 #include "llvm/Support/DJB.h"
41 #include "llvm/Support/Endian.h"
42 #include "llvm/Support/LEB128.h"
43 #include "llvm/Support/Parallel.h"
44 #include "llvm/Support/TimeProfiler.h"
45 #include <cinttypes>
46 #include <cstdlib>
48 using namespace llvm;
49 using namespace llvm::dwarf;
50 using namespace llvm::ELF;
51 using namespace llvm::object;
52 using namespace llvm::support;
53 using namespace lld;
54 using namespace lld::elf;
56 using llvm::support::endian::read32le;
57 using llvm::support::endian::write32le;
58 using llvm::support::endian::write64le;
60 constexpr size_t MergeNoTailSection::numShards;
62 static uint64_t readUint(Ctx &ctx, uint8_t *buf) {
63 return ctx.arg.is64 ? read64(ctx, buf) : read32(ctx, buf);
66 static void writeUint(Ctx &ctx, uint8_t *buf, uint64_t val) {
67 if (ctx.arg.is64)
68 write64(ctx, buf, val);
69 else
70 write32(ctx, buf, val);
73 // Returns an LLD version string.
74 static ArrayRef<uint8_t> getVersion(Ctx &ctx) {
75 // Check LLD_VERSION first for ease of testing.
76 // You can get consistent output by using the environment variable.
77 // This is only for testing.
78 StringRef s = getenv("LLD_VERSION");
79 if (s.empty())
80 s = ctx.saver.save(Twine("Linker: ") + getLLDVersion());
82 // +1 to include the terminating '\0'.
83 return {(const uint8_t *)s.data(), s.size() + 1};
86 // Creates a .comment section containing LLD version info.
87 // With this feature, you can identify LLD-generated binaries easily
88 // by "readelf --string-dump .comment <file>".
89 // The returned object is a mergeable string section.
90 MergeInputSection *elf::createCommentSection(Ctx &ctx) {
91 auto *sec =
92 make<MergeInputSection>(ctx, ".comment", SHT_PROGBITS,
93 SHF_MERGE | SHF_STRINGS, 1, getVersion(ctx));
94 sec->splitIntoPieces();
95 return sec;
98 // .MIPS.abiflags section.
99 template <class ELFT>
100 MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Ctx &ctx,
101 Elf_Mips_ABIFlags flags)
102 : SyntheticSection(ctx, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, 8),
103 flags(flags) {
104 this->entsize = sizeof(Elf_Mips_ABIFlags);
107 template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
108 memcpy(buf, &flags, sizeof(flags));
111 template <class ELFT>
112 std::unique_ptr<MipsAbiFlagsSection<ELFT>>
113 MipsAbiFlagsSection<ELFT>::create(Ctx &ctx) {
114 Elf_Mips_ABIFlags flags = {};
115 bool create = false;
117 for (InputSectionBase *sec : ctx.inputSections) {
118 if (sec->type != SHT_MIPS_ABIFLAGS)
119 continue;
120 sec->markDead();
121 create = true;
123 const size_t size = sec->content().size();
124 // Older version of BFD (such as the default FreeBSD linker) concatenate
125 // .MIPS.abiflags instead of merging. To allow for this case (or potential
126 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
127 if (size < sizeof(Elf_Mips_ABIFlags)) {
128 Err(ctx) << sec->file << ": invalid size of .MIPS.abiflags section: got "
129 << size << " instead of " << sizeof(Elf_Mips_ABIFlags);
130 return nullptr;
132 auto *s =
133 reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->content().data());
134 if (s->version != 0) {
135 Err(ctx) << sec->file << ": unexpected .MIPS.abiflags version "
136 << s->version;
137 return nullptr;
140 // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
141 // select the highest number of ISA/Rev/Ext.
142 flags.isa_level = std::max(flags.isa_level, s->isa_level);
143 flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
144 flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
145 flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
146 flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
147 flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
148 flags.ases |= s->ases;
149 flags.flags1 |= s->flags1;
150 flags.flags2 |= s->flags2;
151 flags.fp_abi =
152 elf::getMipsFpAbiFlag(ctx, sec->file, flags.fp_abi, s->fp_abi);
155 if (create)
156 return std::make_unique<MipsAbiFlagsSection<ELFT>>(ctx, flags);
157 return nullptr;
160 // .MIPS.options section.
161 template <class ELFT>
162 MipsOptionsSection<ELFT>::MipsOptionsSection(Ctx &ctx, Elf_Mips_RegInfo reginfo)
163 : SyntheticSection(ctx, ".MIPS.options", SHT_MIPS_OPTIONS, SHF_ALLOC, 8),
164 reginfo(reginfo) {
165 this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
168 template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
169 auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
170 options->kind = ODK_REGINFO;
171 options->size = getSize();
173 if (!ctx.arg.relocatable)
174 reginfo.ri_gp_value = ctx.in.mipsGot->getGp();
175 memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
178 template <class ELFT>
179 std::unique_ptr<MipsOptionsSection<ELFT>>
180 MipsOptionsSection<ELFT>::create(Ctx &ctx) {
181 // N64 ABI only.
182 if (!ELFT::Is64Bits)
183 return nullptr;
185 SmallVector<InputSectionBase *, 0> sections;
186 for (InputSectionBase *sec : ctx.inputSections)
187 if (sec->type == SHT_MIPS_OPTIONS)
188 sections.push_back(sec);
190 if (sections.empty())
191 return nullptr;
193 Elf_Mips_RegInfo reginfo = {};
194 for (InputSectionBase *sec : sections) {
195 sec->markDead();
197 ArrayRef<uint8_t> d = sec->content();
198 while (!d.empty()) {
199 if (d.size() < sizeof(Elf_Mips_Options)) {
200 Err(ctx) << sec->file << ": invalid size of .MIPS.options section";
201 break;
204 auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
205 if (opt->kind == ODK_REGINFO) {
206 reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
207 sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
208 break;
211 if (!opt->size) {
212 Err(ctx) << sec->file << ": zero option descriptor size";
213 break;
215 d = d.slice(opt->size);
219 return std::make_unique<MipsOptionsSection<ELFT>>(ctx, reginfo);
222 // MIPS .reginfo section.
223 template <class ELFT>
224 MipsReginfoSection<ELFT>::MipsReginfoSection(Ctx &ctx, Elf_Mips_RegInfo reginfo)
225 : SyntheticSection(ctx, ".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC, 4),
226 reginfo(reginfo) {
227 this->entsize = sizeof(Elf_Mips_RegInfo);
230 template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
231 if (!ctx.arg.relocatable)
232 reginfo.ri_gp_value = ctx.in.mipsGot->getGp();
233 memcpy(buf, &reginfo, sizeof(reginfo));
236 template <class ELFT>
237 std::unique_ptr<MipsReginfoSection<ELFT>>
238 MipsReginfoSection<ELFT>::create(Ctx &ctx) {
239 // Section should be alive for O32 and N32 ABIs only.
240 if (ELFT::Is64Bits)
241 return nullptr;
243 SmallVector<InputSectionBase *, 0> sections;
244 for (InputSectionBase *sec : ctx.inputSections)
245 if (sec->type == SHT_MIPS_REGINFO)
246 sections.push_back(sec);
248 if (sections.empty())
249 return nullptr;
251 Elf_Mips_RegInfo reginfo = {};
252 for (InputSectionBase *sec : sections) {
253 sec->markDead();
255 if (sec->content().size() != sizeof(Elf_Mips_RegInfo)) {
256 Err(ctx) << sec->file << ": invalid size of .reginfo section";
257 return nullptr;
260 auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->content().data());
261 reginfo.ri_gprmask |= r->ri_gprmask;
262 sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
265 return std::make_unique<MipsReginfoSection<ELFT>>(ctx, reginfo);
268 InputSection *elf::createInterpSection(Ctx &ctx) {
269 // StringSaver guarantees that the returned string ends with '\0'.
270 StringRef s = ctx.saver.save(ctx.arg.dynamicLinker);
271 ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
273 return make<InputSection>(ctx.internalFile, ".interp", SHT_PROGBITS,
274 SHF_ALLOC,
275 /*addralign=*/1, /*entsize=*/0, contents);
278 Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
279 uint64_t value, uint64_t size,
280 InputSectionBase &section) {
281 Defined *s = makeDefined(ctx, section.file, name, STB_LOCAL, STV_DEFAULT,
282 type, value, size, &section);
283 if (ctx.in.symTab)
284 ctx.in.symTab->addSymbol(s);
286 if (ctx.arg.emachine == EM_ARM && !ctx.arg.isLE && ctx.arg.armBe8 &&
287 (section.flags & SHF_EXECINSTR))
288 // Adding Linker generated mapping symbols to the arm specific mapping
289 // symbols list.
290 addArmSyntheticSectionMappingSymbol(s);
292 return s;
295 static size_t getHashSize(Ctx &ctx) {
296 switch (ctx.arg.buildId) {
297 case BuildIdKind::Fast:
298 return 8;
299 case BuildIdKind::Md5:
300 case BuildIdKind::Uuid:
301 return 16;
302 case BuildIdKind::Sha1:
303 return 20;
304 case BuildIdKind::Hexstring:
305 return ctx.arg.buildIdVector.size();
306 default:
307 llvm_unreachable("unknown BuildIdKind");
311 // This class represents a linker-synthesized .note.gnu.property section.
313 // In x86 and AArch64, object files may contain feature flags indicating the
314 // features that they have used. The flags are stored in a .note.gnu.property
315 // section.
317 // lld reads the sections from input files and merges them by computing AND of
318 // the flags. The result is written as a new .note.gnu.property section.
320 // If the flag is zero (which indicates that the intersection of the feature
321 // sets is empty, or some input files didn't have .note.gnu.property sections),
322 // we don't create this section.
323 GnuPropertySection::GnuPropertySection(Ctx &ctx)
324 : SyntheticSection(ctx, ".note.gnu.property", SHT_NOTE, SHF_ALLOC,
325 ctx.arg.wordsize) {}
327 void GnuPropertySection::writeTo(uint8_t *buf) {
328 write32(ctx, buf, 4); // Name size
329 write32(ctx, buf + 4, getSize() - 16); // Content size
330 write32(ctx, buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type
331 memcpy(buf + 12, "GNU", 4); // Name string
333 uint32_t featureAndType = ctx.arg.emachine == EM_AARCH64
334 ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
335 : GNU_PROPERTY_X86_FEATURE_1_AND;
337 unsigned offset = 16;
338 if (ctx.arg.andFeatures != 0) {
339 write32(ctx, buf + offset + 0, featureAndType); // Feature type
340 write32(ctx, buf + offset + 4, 4); // Feature size
341 write32(ctx, buf + offset + 8, ctx.arg.andFeatures); // Feature flags
342 if (ctx.arg.is64)
343 write32(ctx, buf + offset + 12, 0); // Padding
344 offset += 16;
347 if (!ctx.aarch64PauthAbiCoreInfo.empty()) {
348 write32(ctx, buf + offset + 0, GNU_PROPERTY_AARCH64_FEATURE_PAUTH);
349 write32(ctx, buf + offset + 4, ctx.aarch64PauthAbiCoreInfo.size());
350 memcpy(buf + offset + 8, ctx.aarch64PauthAbiCoreInfo.data(),
351 ctx.aarch64PauthAbiCoreInfo.size());
355 size_t GnuPropertySection::getSize() const {
356 uint32_t contentSize = 0;
357 if (ctx.arg.andFeatures != 0)
358 contentSize += ctx.arg.is64 ? 16 : 12;
359 if (!ctx.aarch64PauthAbiCoreInfo.empty())
360 contentSize += 4 + 4 + ctx.aarch64PauthAbiCoreInfo.size();
361 assert(contentSize != 0);
362 return contentSize + 16;
365 BuildIdSection::BuildIdSection(Ctx &ctx)
366 : SyntheticSection(ctx, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, 4),
367 hashSize(getHashSize(ctx)) {}
369 void BuildIdSection::writeTo(uint8_t *buf) {
370 write32(ctx, buf, 4); // Name size
371 write32(ctx, buf + 4, hashSize); // Content size
372 write32(ctx, buf + 8, NT_GNU_BUILD_ID); // Type
373 memcpy(buf + 12, "GNU", 4); // Name string
374 hashBuf = buf + 16;
377 void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {
378 assert(buf.size() == hashSize);
379 memcpy(hashBuf, buf.data(), hashSize);
382 BssSection::BssSection(Ctx &ctx, StringRef name, uint64_t size,
383 uint32_t alignment)
384 : SyntheticSection(ctx, name, SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
385 alignment) {
386 this->bss = true;
387 this->size = size;
390 EhFrameSection::EhFrameSection(Ctx &ctx)
391 : SyntheticSection(ctx, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, 1) {}
393 // Search for an existing CIE record or create a new one.
394 // CIE records from input object files are uniquified by their contents
395 // and where their relocations point to.
396 template <class ELFT, class RelTy>
397 CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
398 Symbol *personality = nullptr;
399 unsigned firstRelI = cie.firstRelocation;
400 if (firstRelI != (unsigned)-1)
401 personality = &cie.sec->file->getRelocTargetSym(rels[firstRelI]);
403 // Search for an existing CIE by CIE contents/relocation target pair.
404 CieRecord *&rec = cieMap[{cie.data(), personality}];
406 // If not found, create a new one.
407 if (!rec) {
408 rec = make<CieRecord>();
409 rec->cie = &cie;
410 cieRecords.push_back(rec);
412 return rec;
415 // There is one FDE per function. Returns a non-null pointer to the function
416 // symbol if the given FDE points to a live function.
417 template <class ELFT, class RelTy>
418 Defined *EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
419 auto *sec = cast<EhInputSection>(fde.sec);
420 unsigned firstRelI = fde.firstRelocation;
422 // An FDE should point to some function because FDEs are to describe
423 // functions. That's however not always the case due to an issue of
424 // ld.gold with -r. ld.gold may discard only functions and leave their
425 // corresponding FDEs, which results in creating bad .eh_frame sections.
426 // To deal with that, we ignore such FDEs.
427 if (firstRelI == (unsigned)-1)
428 return nullptr;
430 const RelTy &rel = rels[firstRelI];
431 Symbol &b = sec->file->getRelocTargetSym(rel);
433 // FDEs for garbage-collected or merged-by-ICF sections, or sections in
434 // another partition, are dead.
435 if (auto *d = dyn_cast<Defined>(&b))
436 if (!d->folded && d->section && d->section->partition == partition)
437 return d;
438 return nullptr;
441 // .eh_frame is a sequence of CIE or FDE records. In general, there
442 // is one CIE record per input object file which is followed by
443 // a list of FDEs. This function searches an existing CIE or create a new
444 // one and associates FDEs to the CIE.
445 template <class ELFT, class RelTy>
446 void EhFrameSection::addRecords(EhInputSection *sec, ArrayRef<RelTy> rels) {
447 offsetToCie.clear();
448 for (EhSectionPiece &cie : sec->cies)
449 offsetToCie[cie.inputOff] = addCie<ELFT>(cie, rels);
450 for (EhSectionPiece &fde : sec->fdes) {
451 uint32_t id = endian::read32<ELFT::Endianness>(fde.data().data() + 4);
452 CieRecord *rec = offsetToCie[fde.inputOff + 4 - id];
453 if (!rec)
454 Fatal(ctx) << sec << ": invalid CIE reference";
456 if (!isFdeLive<ELFT>(fde, rels))
457 continue;
458 rec->fdes.push_back(&fde);
459 numFdes++;
463 template <class ELFT>
464 void EhFrameSection::addSectionAux(EhInputSection *sec) {
465 if (!sec->isLive())
466 return;
467 const RelsOrRelas<ELFT> rels =
468 sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);
469 if (rels.areRelocsRel())
470 addRecords<ELFT>(sec, rels.rels);
471 else
472 addRecords<ELFT>(sec, rels.relas);
475 // Used by ICF<ELFT>::handleLSDA(). This function is very similar to
476 // EhFrameSection::addRecords().
477 template <class ELFT, class RelTy>
478 void EhFrameSection::iterateFDEWithLSDAAux(
479 EhInputSection &sec, ArrayRef<RelTy> rels, DenseSet<size_t> &ciesWithLSDA,
480 llvm::function_ref<void(InputSection &)> fn) {
481 for (EhSectionPiece &cie : sec.cies)
482 if (hasLSDA(cie))
483 ciesWithLSDA.insert(cie.inputOff);
484 for (EhSectionPiece &fde : sec.fdes) {
485 uint32_t id = endian::read32<ELFT::Endianness>(fde.data().data() + 4);
486 if (!ciesWithLSDA.contains(fde.inputOff + 4 - id))
487 continue;
489 // The CIE has a LSDA argument. Call fn with d's section.
490 if (Defined *d = isFdeLive<ELFT>(fde, rels))
491 if (auto *s = dyn_cast_or_null<InputSection>(d->section))
492 fn(*s);
496 template <class ELFT>
497 void EhFrameSection::iterateFDEWithLSDA(
498 llvm::function_ref<void(InputSection &)> fn) {
499 DenseSet<size_t> ciesWithLSDA;
500 for (EhInputSection *sec : sections) {
501 ciesWithLSDA.clear();
502 const RelsOrRelas<ELFT> rels =
503 sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);
504 if (rels.areRelocsRel())
505 iterateFDEWithLSDAAux<ELFT>(*sec, rels.rels, ciesWithLSDA, fn);
506 else
507 iterateFDEWithLSDAAux<ELFT>(*sec, rels.relas, ciesWithLSDA, fn);
511 static void writeCieFde(Ctx &ctx, uint8_t *buf, ArrayRef<uint8_t> d) {
512 memcpy(buf, d.data(), d.size());
513 // Fix the size field. -4 since size does not include the size field itself.
514 write32(ctx, buf, d.size() - 4);
517 void EhFrameSection::finalizeContents() {
518 assert(!this->size); // Not finalized.
520 switch (ctx.arg.ekind) {
521 case ELFNoneKind:
522 llvm_unreachable("invalid ekind");
523 case ELF32LEKind:
524 for (EhInputSection *sec : sections)
525 addSectionAux<ELF32LE>(sec);
526 break;
527 case ELF32BEKind:
528 for (EhInputSection *sec : sections)
529 addSectionAux<ELF32BE>(sec);
530 break;
531 case ELF64LEKind:
532 for (EhInputSection *sec : sections)
533 addSectionAux<ELF64LE>(sec);
534 break;
535 case ELF64BEKind:
536 for (EhInputSection *sec : sections)
537 addSectionAux<ELF64BE>(sec);
538 break;
541 size_t off = 0;
542 for (CieRecord *rec : cieRecords) {
543 rec->cie->outputOff = off;
544 off += rec->cie->size;
546 for (EhSectionPiece *fde : rec->fdes) {
547 fde->outputOff = off;
548 off += fde->size;
552 // The LSB standard does not allow a .eh_frame section with zero
553 // Call Frame Information records. glibc unwind-dw2-fde.c
554 // classify_object_over_fdes expects there is a CIE record length 0 as a
555 // terminator. Thus we add one unconditionally.
556 off += 4;
558 this->size = off;
561 // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
562 // to get an FDE from an address to which FDE is applied. This function
563 // returns a list of such pairs.
564 SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {
565 uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
566 SmallVector<FdeData, 0> ret;
568 uint64_t va = getPartition(ctx).ehFrameHdr->getVA();
569 for (CieRecord *rec : cieRecords) {
570 uint8_t enc = getFdeEncoding(rec->cie);
571 for (EhSectionPiece *fde : rec->fdes) {
572 uint64_t pc = getFdePc(buf, fde->outputOff, enc);
573 uint64_t fdeVA = getParent()->addr + fde->outputOff;
574 if (!isInt<32>(pc - va)) {
575 Err(ctx) << fde->sec << ": PC offset is too large: 0x"
576 << Twine::utohexstr(pc - va);
577 continue;
579 ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});
583 // Sort the FDE list by their PC and uniqueify. Usually there is only
584 // one FDE for a PC (i.e. function), but if ICF merges two functions
585 // into one, there can be more than one FDEs pointing to the address.
586 auto less = [](const FdeData &a, const FdeData &b) {
587 return a.pcRel < b.pcRel;
589 llvm::stable_sort(ret, less);
590 auto eq = [](const FdeData &a, const FdeData &b) {
591 return a.pcRel == b.pcRel;
593 ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end());
595 return ret;
598 static uint64_t readFdeAddr(Ctx &ctx, uint8_t *buf, int size) {
599 switch (size) {
600 case DW_EH_PE_udata2:
601 return read16(ctx, buf);
602 case DW_EH_PE_sdata2:
603 return (int16_t)read16(ctx, buf);
604 case DW_EH_PE_udata4:
605 return read32(ctx, buf);
606 case DW_EH_PE_sdata4:
607 return (int32_t)read32(ctx, buf);
608 case DW_EH_PE_udata8:
609 case DW_EH_PE_sdata8:
610 return read64(ctx, buf);
611 case DW_EH_PE_absptr:
612 return readUint(ctx, buf);
614 Err(ctx) << "unknown FDE size encoding";
615 return 0;
618 // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
619 // We need it to create .eh_frame_hdr section.
620 uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,
621 uint8_t enc) const {
622 // The starting address to which this FDE applies is
623 // stored at FDE + 8 byte. And this offset is within
624 // the .eh_frame section.
625 size_t off = fdeOff + 8;
626 uint64_t addr = readFdeAddr(ctx, buf + off, enc & 0xf);
627 if ((enc & 0x70) == DW_EH_PE_absptr)
628 return ctx.arg.is64 ? addr : uint32_t(addr);
629 if ((enc & 0x70) == DW_EH_PE_pcrel)
630 return addr + getParent()->addr + off + outSecOff;
631 Err(ctx) << "unknown FDE size relative encoding";
632 return 0;
635 void EhFrameSection::writeTo(uint8_t *buf) {
636 // Write CIE and FDE records.
637 for (CieRecord *rec : cieRecords) {
638 size_t cieOffset = rec->cie->outputOff;
639 writeCieFde(ctx, buf + cieOffset, rec->cie->data());
641 for (EhSectionPiece *fde : rec->fdes) {
642 size_t off = fde->outputOff;
643 writeCieFde(ctx, buf + off, fde->data());
645 // FDE's second word should have the offset to an associated CIE.
646 // Write it.
647 write32(ctx, buf + off + 4, off + 4 - cieOffset);
651 // Apply relocations. .eh_frame section contents are not contiguous
652 // in the output buffer, but relocateAlloc() still works because
653 // getOffset() takes care of discontiguous section pieces.
654 for (EhInputSection *s : sections)
655 ctx.target->relocateAlloc(*s, buf);
657 if (getPartition(ctx).ehFrameHdr && getPartition(ctx).ehFrameHdr->getParent())
658 getPartition(ctx).ehFrameHdr->write();
661 GotSection::GotSection(Ctx &ctx)
662 : SyntheticSection(ctx, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
663 ctx.target->gotEntrySize) {
664 numEntries = ctx.target->gotHeaderEntriesNum;
667 void GotSection::addConstant(const Relocation &r) { relocations.push_back(r); }
668 void GotSection::addEntry(const Symbol &sym) {
669 assert(sym.auxIdx == ctx.symAux.size() - 1);
670 ctx.symAux.back().gotIdx = numEntries++;
673 void GotSection::addAuthEntry(const Symbol &sym) {
674 authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, sym.isFunc()});
677 bool GotSection::addTlsDescEntry(const Symbol &sym) {
678 assert(sym.auxIdx == ctx.symAux.size() - 1);
679 ctx.symAux.back().tlsDescIdx = numEntries;
680 numEntries += 2;
681 return true;
684 bool GotSection::addDynTlsEntry(const Symbol &sym) {
685 assert(sym.auxIdx == ctx.symAux.size() - 1);
686 ctx.symAux.back().tlsGdIdx = numEntries;
687 // Global Dynamic TLS entries take two GOT slots.
688 numEntries += 2;
689 return true;
692 // Reserves TLS entries for a TLS module ID and a TLS block offset.
693 // In total it takes two GOT slots.
694 bool GotSection::addTlsIndex() {
695 if (tlsIndexOff != uint32_t(-1))
696 return false;
697 tlsIndexOff = numEntries * ctx.arg.wordsize;
698 numEntries += 2;
699 return true;
702 uint32_t GotSection::getTlsDescOffset(const Symbol &sym) const {
703 return sym.getTlsDescIdx(ctx) * ctx.arg.wordsize;
706 uint64_t GotSection::getTlsDescAddr(const Symbol &sym) const {
707 return getVA() + getTlsDescOffset(sym);
710 uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {
711 return this->getVA() + b.getTlsGdIdx(ctx) * ctx.arg.wordsize;
714 uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {
715 return b.getTlsGdIdx(ctx) * ctx.arg.wordsize;
718 void GotSection::finalizeContents() {
719 if (ctx.arg.emachine == EM_PPC64 &&
720 numEntries <= ctx.target->gotHeaderEntriesNum &&
721 !ctx.sym.globalOffsetTable)
722 size = 0;
723 else
724 size = numEntries * ctx.arg.wordsize;
727 bool GotSection::isNeeded() const {
728 // Needed if the GOT symbol is used or the number of entries is more than just
729 // the header. A GOT with just the header may not be needed.
730 return hasGotOffRel || numEntries > ctx.target->gotHeaderEntriesNum;
733 void GotSection::writeTo(uint8_t *buf) {
734 // On PPC64 .got may be needed but empty. Skip the write.
735 if (size == 0)
736 return;
737 ctx.target->writeGotHeader(buf);
738 ctx.target->relocateAlloc(*this, buf);
739 for (const AuthEntryInfo &authEntry : authEntries) {
740 // https://github.com/ARM-software/abi-aa/blob/2024Q3/pauthabielf64/pauthabielf64.rst#default-signing-schema
741 // Signed GOT entries use the IA key for symbols of type STT_FUNC and the
742 // DA key for all other symbol types, with the address of the GOT entry as
743 // the modifier. The static linker must encode the signing schema into the
744 // GOT slot.
746 // https://github.com/ARM-software/abi-aa/blob/2024Q3/pauthabielf64/pauthabielf64.rst#encoding-the-signing-schema
747 // If address diversity is set and the discriminator
748 // is 0 then modifier = Place
749 uint8_t *dest = buf + authEntry.offset;
750 uint64_t key = authEntry.isSymbolFunc ? /*IA=*/0b00 : /*DA=*/0b10;
751 uint64_t addrDiversity = 1;
752 write64(ctx, dest, (addrDiversity << 63) | (key << 60));
756 static uint64_t getMipsPageAddr(uint64_t addr) {
757 return (addr + 0x8000) & ~0xffff;
760 static uint64_t getMipsPageCount(uint64_t size) {
761 return (size + 0xfffe) / 0xffff + 1;
764 MipsGotSection::MipsGotSection(Ctx &ctx)
765 : SyntheticSection(ctx, ".got", SHT_PROGBITS,
766 SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, 16) {}
768 void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,
769 RelExpr expr) {
770 FileGot &g = getGot(file);
771 if (expr == RE_MIPS_GOT_LOCAL_PAGE) {
772 if (const OutputSection *os = sym.getOutputSection())
773 g.pagesMap.insert({os, {}});
774 else
775 g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(ctx, addend))}, 0});
776 } else if (sym.isTls())
777 g.tls.insert({&sym, 0});
778 else if (sym.isPreemptible && expr == R_ABS)
779 g.relocs.insert({&sym, 0});
780 else if (sym.isPreemptible)
781 g.global.insert({&sym, 0});
782 else if (expr == RE_MIPS_GOT_OFF32)
783 g.local32.insert({{&sym, addend}, 0});
784 else
785 g.local16.insert({{&sym, addend}, 0});
788 void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) {
789 getGot(file).dynTlsSymbols.insert({&sym, 0});
792 void MipsGotSection::addTlsIndex(InputFile &file) {
793 getGot(file).dynTlsSymbols.insert({nullptr, 0});
796 size_t MipsGotSection::FileGot::getEntriesNum() const {
797 return getPageEntriesNum() + local16.size() + global.size() + relocs.size() +
798 tls.size() + dynTlsSymbols.size() * 2;
801 size_t MipsGotSection::FileGot::getPageEntriesNum() const {
802 size_t num = 0;
803 for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap)
804 num += p.second.count;
805 return num;
808 size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
809 size_t count = getPageEntriesNum() + local16.size() + global.size();
810 // If there are relocation-only entries in the GOT, TLS entries
811 // are allocated after them. TLS entries should be addressable
812 // by 16-bit index so count both reloc-only and TLS entries.
813 if (!tls.empty() || !dynTlsSymbols.empty())
814 count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2;
815 return count;
818 MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) {
819 if (f.mipsGotIndex == uint32_t(-1)) {
820 gots.emplace_back();
821 gots.back().file = &f;
822 f.mipsGotIndex = gots.size() - 1;
824 return gots[f.mipsGotIndex];
827 uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f,
828 const Symbol &sym,
829 int64_t addend) const {
830 const FileGot &g = gots[f->mipsGotIndex];
831 uint64_t index = 0;
832 if (const OutputSection *outSec = sym.getOutputSection()) {
833 uint64_t secAddr = getMipsPageAddr(outSec->addr);
834 uint64_t symAddr = getMipsPageAddr(sym.getVA(ctx, addend));
835 index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff;
836 } else {
837 index =
838 g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(ctx, addend))});
840 return index * ctx.arg.wordsize;
843 uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s,
844 int64_t addend) const {
845 const FileGot &g = gots[f->mipsGotIndex];
846 Symbol *sym = const_cast<Symbol *>(&s);
847 if (sym->isTls())
848 return g.tls.lookup(sym) * ctx.arg.wordsize;
849 if (sym->isPreemptible)
850 return g.global.lookup(sym) * ctx.arg.wordsize;
851 return g.local16.lookup({sym, addend}) * ctx.arg.wordsize;
854 uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const {
855 const FileGot &g = gots[f->mipsGotIndex];
856 return g.dynTlsSymbols.lookup(nullptr) * ctx.arg.wordsize;
859 uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f,
860 const Symbol &s) const {
861 const FileGot &g = gots[f->mipsGotIndex];
862 Symbol *sym = const_cast<Symbol *>(&s);
863 return g.dynTlsSymbols.lookup(sym) * ctx.arg.wordsize;
866 const Symbol *MipsGotSection::getFirstGlobalEntry() const {
867 if (gots.empty())
868 return nullptr;
869 const FileGot &primGot = gots.front();
870 if (!primGot.global.empty())
871 return primGot.global.front().first;
872 if (!primGot.relocs.empty())
873 return primGot.relocs.front().first;
874 return nullptr;
877 unsigned MipsGotSection::getLocalEntriesNum() const {
878 if (gots.empty())
879 return headerEntriesNum;
880 return headerEntriesNum + gots.front().getPageEntriesNum() +
881 gots.front().local16.size();
884 bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {
885 FileGot tmp = dst;
886 set_union(tmp.pagesMap, src.pagesMap);
887 set_union(tmp.local16, src.local16);
888 set_union(tmp.global, src.global);
889 set_union(tmp.relocs, src.relocs);
890 set_union(tmp.tls, src.tls);
891 set_union(tmp.dynTlsSymbols, src.dynTlsSymbols);
893 size_t count = isPrimary ? headerEntriesNum : 0;
894 count += tmp.getIndexedEntriesNum();
896 if (count * ctx.arg.wordsize > ctx.arg.mipsGotSize)
897 return false;
899 std::swap(tmp, dst);
900 return true;
903 void MipsGotSection::finalizeContents() { updateAllocSize(ctx); }
905 bool MipsGotSection::updateAllocSize(Ctx &ctx) {
906 size = headerEntriesNum * ctx.arg.wordsize;
907 for (const FileGot &g : gots)
908 size += g.getEntriesNum() * ctx.arg.wordsize;
909 return false;
912 void MipsGotSection::build() {
913 if (gots.empty())
914 return;
916 std::vector<FileGot> mergedGots(1);
918 // For each GOT move non-preemptible symbols from the `Global`
919 // to `Local16` list. Preemptible symbol might become non-preemptible
920 // one if, for example, it gets a related copy relocation.
921 for (FileGot &got : gots) {
922 for (auto &p: got.global)
923 if (!p.first->isPreemptible)
924 got.local16.insert({{p.first, 0}, 0});
925 got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) {
926 return !p.first->isPreemptible;
930 // For each GOT remove "reloc-only" entry if there is "global"
931 // entry for the same symbol. And add local entries which indexed
932 // using 32-bit value at the end of 16-bit entries.
933 for (FileGot &got : gots) {
934 got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
935 return got.global.count(p.first);
937 set_union(got.local16, got.local32);
938 got.local32.clear();
941 // Evaluate number of "reloc-only" entries in the resulting GOT.
942 // To do that put all unique "reloc-only" and "global" entries
943 // from all GOTs to the future primary GOT.
944 FileGot *primGot = &mergedGots.front();
945 for (FileGot &got : gots) {
946 set_union(primGot->relocs, got.global);
947 set_union(primGot->relocs, got.relocs);
948 got.relocs.clear();
951 // Evaluate number of "page" entries in each GOT.
952 for (FileGot &got : gots) {
953 for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
954 got.pagesMap) {
955 const OutputSection *os = p.first;
956 uint64_t secSize = 0;
957 for (SectionCommand *cmd : os->commands) {
958 if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
959 for (InputSection *isec : isd->sections) {
960 uint64_t off = alignToPowerOf2(secSize, isec->addralign);
961 secSize = off + isec->getSize();
964 p.second.count = getMipsPageCount(secSize);
968 // Merge GOTs. Try to join as much as possible GOTs but do not exceed
969 // maximum GOT size. At first, try to fill the primary GOT because
970 // the primary GOT can be accessed in the most effective way. If it
971 // is not possible, try to fill the last GOT in the list, and finally
972 // create a new GOT if both attempts failed.
973 for (FileGot &srcGot : gots) {
974 InputFile *file = srcGot.file;
975 if (tryMergeGots(mergedGots.front(), srcGot, true)) {
976 file->mipsGotIndex = 0;
977 } else {
978 // If this is the first time we failed to merge with the primary GOT,
979 // MergedGots.back() will also be the primary GOT. We must make sure not
980 // to try to merge again with isPrimary=false, as otherwise, if the
981 // inputs are just right, we could allow the primary GOT to become 1 or 2
982 // words bigger due to ignoring the header size.
983 if (mergedGots.size() == 1 ||
984 !tryMergeGots(mergedGots.back(), srcGot, false)) {
985 mergedGots.emplace_back();
986 std::swap(mergedGots.back(), srcGot);
988 file->mipsGotIndex = mergedGots.size() - 1;
991 std::swap(gots, mergedGots);
993 // Reduce number of "reloc-only" entries in the primary GOT
994 // by subtracting "global" entries in the primary GOT.
995 primGot = &gots.front();
996 primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
997 return primGot->global.count(p.first);
1000 // Calculate indexes for each GOT entry.
1001 size_t index = headerEntriesNum;
1002 for (FileGot &got : gots) {
1003 got.startIndex = &got == primGot ? 0 : index;
1004 for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
1005 got.pagesMap) {
1006 // For each output section referenced by GOT page relocations calculate
1007 // and save into pagesMap an upper bound of MIPS GOT entries required
1008 // to store page addresses of local symbols. We assume the worst case -
1009 // each 64kb page of the output section has at least one GOT relocation
1010 // against it. And take in account the case when the section intersects
1011 // page boundaries.
1012 p.second.firstIndex = index;
1013 index += p.second.count;
1015 for (auto &p: got.local16)
1016 p.second = index++;
1017 for (auto &p: got.global)
1018 p.second = index++;
1019 for (auto &p: got.relocs)
1020 p.second = index++;
1021 for (auto &p: got.tls)
1022 p.second = index++;
1023 for (auto &p: got.dynTlsSymbols) {
1024 p.second = index;
1025 index += 2;
1029 // Update SymbolAux::gotIdx field to use this
1030 // value later in the `sortMipsSymbols` function.
1031 for (auto &p : primGot->global) {
1032 if (p.first->auxIdx == 0)
1033 p.first->allocateAux(ctx);
1034 ctx.symAux.back().gotIdx = p.second;
1036 for (auto &p : primGot->relocs) {
1037 if (p.first->auxIdx == 0)
1038 p.first->allocateAux(ctx);
1039 ctx.symAux.back().gotIdx = p.second;
1042 // Create dynamic relocations.
1043 for (FileGot &got : gots) {
1044 // Create dynamic relocations for TLS entries.
1045 for (std::pair<Symbol *, size_t> &p : got.tls) {
1046 Symbol *s = p.first;
1047 uint64_t offset = p.second * ctx.arg.wordsize;
1048 // When building a shared library we still need a dynamic relocation
1049 // for the TP-relative offset as we don't know how much other data will
1050 // be allocated before us in the static TLS block.
1051 if (s->isPreemptible || ctx.arg.shared)
1052 ctx.mainPart->relaDyn->addReloc(
1053 {ctx.target->tlsGotRel, this, offset,
1054 DynamicReloc::AgainstSymbolWithTargetVA, *s, 0, R_ABS});
1056 for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
1057 Symbol *s = p.first;
1058 uint64_t offset = p.second * ctx.arg.wordsize;
1059 if (s == nullptr) {
1060 if (!ctx.arg.shared)
1061 continue;
1062 ctx.mainPart->relaDyn->addReloc(
1063 {ctx.target->tlsModuleIndexRel, this, offset});
1064 } else {
1065 // When building a shared library we still need a dynamic relocation
1066 // for the module index. Therefore only checking for
1067 // S->isPreemptible is not sufficient (this happens e.g. for
1068 // thread-locals that have been marked as local through a linker script)
1069 if (!s->isPreemptible && !ctx.arg.shared)
1070 continue;
1071 ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->tlsModuleIndexRel,
1072 *this, offset, *s);
1073 // However, we can skip writing the TLS offset reloc for non-preemptible
1074 // symbols since it is known even in shared libraries
1075 if (!s->isPreemptible)
1076 continue;
1077 offset += ctx.arg.wordsize;
1078 ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->tlsOffsetRel, *this,
1079 offset, *s);
1083 // Do not create dynamic relocations for non-TLS
1084 // entries in the primary GOT.
1085 if (&got == primGot)
1086 continue;
1088 // Dynamic relocations for "global" entries.
1089 for (const std::pair<Symbol *, size_t> &p : got.global) {
1090 uint64_t offset = p.second * ctx.arg.wordsize;
1091 ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->relativeRel, *this,
1092 offset, *p.first);
1094 if (!ctx.arg.isPic)
1095 continue;
1096 // Dynamic relocations for "local" entries in case of PIC.
1097 for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
1098 got.pagesMap) {
1099 size_t pageCount = l.second.count;
1100 for (size_t pi = 0; pi < pageCount; ++pi) {
1101 uint64_t offset = (l.second.firstIndex + pi) * ctx.arg.wordsize;
1102 ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset,
1103 l.first, int64_t(pi * 0x10000)});
1106 for (const std::pair<GotEntry, size_t> &p : got.local16) {
1107 uint64_t offset = p.second * ctx.arg.wordsize;
1108 ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset,
1109 DynamicReloc::AddendOnlyWithTargetVA,
1110 *p.first.first, p.first.second, R_ABS});
1115 bool MipsGotSection::isNeeded() const {
1116 // We add the .got section to the result for dynamic MIPS target because
1117 // its address and properties are mentioned in the .dynamic section.
1118 return !ctx.arg.relocatable;
1121 uint64_t MipsGotSection::getGp(const InputFile *f) const {
1122 // For files without related GOT or files refer a primary GOT
1123 // returns "common" _gp value. For secondary GOTs calculate
1124 // individual _gp values.
1125 if (!f || f->mipsGotIndex == uint32_t(-1) || f->mipsGotIndex == 0)
1126 return ctx.sym.mipsGp->getVA(ctx, 0);
1127 return getVA() + gots[f->mipsGotIndex].startIndex * ctx.arg.wordsize + 0x7ff0;
1130 void MipsGotSection::writeTo(uint8_t *buf) {
1131 // Set the MSB of the second GOT slot. This is not required by any
1132 // MIPS ABI documentation, though.
1134 // There is a comment in glibc saying that "The MSB of got[1] of a
1135 // gnu object is set to identify gnu objects," and in GNU gold it
1136 // says "the second entry will be used by some runtime loaders".
1137 // But how this field is being used is unclear.
1139 // We are not really willing to mimic other linkers behaviors
1140 // without understanding why they do that, but because all files
1141 // generated by GNU tools have this special GOT value, and because
1142 // we've been doing this for years, it is probably a safe bet to
1143 // keep doing this for now. We really need to revisit this to see
1144 // if we had to do this.
1145 writeUint(ctx, buf + ctx.arg.wordsize,
1146 (uint64_t)1 << (ctx.arg.wordsize * 8 - 1));
1147 for (const FileGot &g : gots) {
1148 auto write = [&](size_t i, const Symbol *s, int64_t a) {
1149 uint64_t va = a;
1150 if (s)
1151 va = s->getVA(ctx, a);
1152 writeUint(ctx, buf + i * ctx.arg.wordsize, va);
1154 // Write 'page address' entries to the local part of the GOT.
1155 for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
1156 g.pagesMap) {
1157 size_t pageCount = l.second.count;
1158 uint64_t firstPageAddr = getMipsPageAddr(l.first->addr);
1159 for (size_t pi = 0; pi < pageCount; ++pi)
1160 write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000);
1162 // Local, global, TLS, reloc-only entries.
1163 // If TLS entry has a corresponding dynamic relocations, leave it
1164 // initialized by zero. Write down adjusted TLS symbol's values otherwise.
1165 // To calculate the adjustments use offsets for thread-local storage.
1166 // http://web.archive.org/web/20190324223224/https://www.linux-mips.org/wiki/NPTL
1167 for (const std::pair<GotEntry, size_t> &p : g.local16)
1168 write(p.second, p.first.first, p.first.second);
1169 // Write VA to the primary GOT only. For secondary GOTs that
1170 // will be done by REL32 dynamic relocations.
1171 if (&g == &gots.front())
1172 for (const std::pair<Symbol *, size_t> &p : g.global)
1173 write(p.second, p.first, 0);
1174 for (const std::pair<Symbol *, size_t> &p : g.relocs)
1175 write(p.second, p.first, 0);
1176 for (const std::pair<Symbol *, size_t> &p : g.tls)
1177 write(p.second, p.first,
1178 p.first->isPreemptible || ctx.arg.shared ? 0 : -0x7000);
1179 for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) {
1180 if (p.first == nullptr && !ctx.arg.shared)
1181 write(p.second, nullptr, 1);
1182 else if (p.first && !p.first->isPreemptible) {
1183 // If we are emitting a shared library with relocations we mustn't write
1184 // anything to the GOT here. When using Elf_Rel relocations the value
1185 // one will be treated as an addend and will cause crashes at runtime
1186 if (!ctx.arg.shared)
1187 write(p.second, nullptr, 1);
1188 write(p.second + 1, p.first, -0x8000);
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.
1198 GotPltSection::GotPltSection(Ctx &ctx)
1199 : SyntheticSection(ctx, ".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
1200 ctx.arg.wordsize) {
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";
1209 void GotPltSection::addEntry(Symbol &sym) {
1210 assert(sym.auxIdx == ctx.symAux.size() - 1 &&
1211 ctx.symAux.back().pltIdx == entries.size());
1212 entries.push_back(&sym);
1215 size_t GotPltSection::getSize() const {
1216 return (ctx.target->gotPltHeaderEntriesNum + entries.size()) *
1217 ctx.target->gotEntrySize;
1220 void 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, *b);
1225 buf += ctx.target->gotEntrySize;
1229 bool 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;
1235 static StringRef getIgotPltName(Ctx &ctx) {
1236 // On ARM the IgotPltSection is part of the GotSection.
1237 if (ctx.arg.emachine == EM_ARM)
1238 return ".got";
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";
1245 return ".got.plt";
1248 // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1249 // with the IgotPltSection.
1250 IgotPltSection::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) {}
1255 void IgotPltSection::addEntry(Symbol &sym) {
1256 assert(ctx.symAux.back().pltIdx == entries.size());
1257 entries.push_back(&sym);
1260 size_t IgotPltSection::getSize() const {
1261 return entries.size() * ctx.target->gotEntrySize;
1264 void IgotPltSection::writeTo(uint8_t *buf) {
1265 for (const Symbol *b : entries) {
1266 ctx.target->writeIgotPlt(buf, *b);
1267 buf += ctx.target->gotEntrySize;
1271 StringTableSection::StringTableSection(Ctx &ctx, StringRef name, bool dynamic)
1272 : SyntheticSection(ctx, name, SHT_STRTAB, dynamic ? (uint64_t)SHF_ALLOC : 0,
1274 dynamic(dynamic) {
1275 // ELF string tables start with a NUL byte.
1276 strings.push_back("");
1277 stringMap.try_emplace(CachedHashStringRef(""), 0);
1278 size = 1;
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.
1285 unsigned StringTableSection::addString(StringRef s, bool hashIt) {
1286 if (hashIt) {
1287 auto r = stringMap.try_emplace(CachedHashStringRef(s), size);
1288 if (!r.second)
1289 return r.first->second;
1291 if (s.empty())
1292 return 0;
1293 unsigned ret = this->size;
1294 this->size = this->size + s.size() + 1;
1295 strings.push_back(s);
1296 return ret;
1299 void StringTableSection::writeTo(uint8_t *buf) {
1300 for (StringRef s : strings) {
1301 memcpy(buf, s.data(), s.size());
1302 buf[s.size()] = '\0';
1303 buf += s.size() + 1;
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.
1310 static unsigned getVerDefNum(Ctx &ctx) {
1311 return namedVersionDefs(ctx).size() + 1;
1314 template <class ELFT>
1315 DynamicSection<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;
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;
1328 // The output section .rela.dyn may include these synthetic sections:
1330 // - part.relaDyn
1331 // - ctx.in.relaPlt: this is included if a linker script places .rela.plt inside
1332 // .rela.dyn
1334 // DT_RELASZ is the total size of the included sections.
1335 static 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;
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).
1346 static uint64_t addPltRelSz(Ctx &ctx) { return ctx.in.relaPlt->getSize(); }
1348 // Add remaining entries to complete .dynamic contents.
1349 template <class ELFT>
1350 std::vector<std::pair<int32_t, uint64_t>>
1351 DynamicSection<ELFT>::computeContents() {
1352 elf::Partition &part = getPartition(ctx);
1353 bool isMain = part.name.empty();
1354 std::vector<std::pair<int32_t, uint64_t>> entries;
1356 auto addInt = [&](int32_t tag, uint64_t val) {
1357 entries.emplace_back(tag, val);
1359 auto addInSec = [&](int32_t tag, const InputSection &sec) {
1360 entries.emplace_back(tag, sec.getVA());
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));
1368 if (!ctx.arg.rpath.empty())
1369 addInt(ctx.arg.enableNewDtags ? DT_RUNPATH : DT_RPATH,
1370 part.dynStrTab->addString(ctx.arg.rpath));
1372 for (SharedFile *file : ctx.sharedFiles)
1373 if (file->isNeeded)
1374 addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1376 if (isMain) {
1377 if (!ctx.arg.soName.empty())
1378 addInt(DT_SONAME, part.dynStrTab->addString(ctx.arg.soName));
1379 } else {
1380 if (!ctx.arg.soName.empty())
1381 addInt(DT_NEEDED, part.dynStrTab->addString(ctx.arg.soName));
1382 addInt(DT_SONAME, part.dynStrTab->addString(part.name));
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;
1408 if (ctx.arg.zOrigin) {
1409 dtFlags |= DF_ORIGIN;
1410 dtFlags1 |= DF_1_ORIGIN;
1412 if (!ctx.arg.zText)
1413 dtFlags |= DF_TEXTREL;
1414 if (ctx.hasTlsIe && ctx.arg.shared)
1415 dtFlags |= DF_STATIC_TLS;
1417 if (dtFlags)
1418 addInt(DT_FLAGS, dtFlags);
1419 if (dtFlags1)
1420 addInt(DT_FLAGS_1, dtFlags1);
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.
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);
1433 if (part.relaDyn->isNeeded()) {
1434 addInSec(part.relaDyn->dynamicTag, *part.relaDyn);
1435 entries.emplace_back(part.relaDyn->sizeDynamicTag,
1436 addRelaSz(ctx, *part.relaDyn));
1438 bool isRela = ctx.arg.isRela;
1439 addInt(isRela ? DT_RELAENT : DT_RELENT,
1440 isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
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);
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));
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));
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);
1494 addInt(DT_RISCV_VARIANT_CC, 0);
1495 [[fallthrough]];
1496 default:
1497 addInSec(DT_PLTGOT, *ctx.in.gotPlt);
1498 break;
1500 addInt(DT_PLTREL, ctx.arg.isRela ? DT_RELA : DT_REL);
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);
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());
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);
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);
1538 if (ctx.out.initArray) {
1539 addInt(DT_INIT_ARRAY, ctx.out.initArray->addr);
1540 addInt(DT_INIT_ARRAYSZ, ctx.out.initArray->size);
1542 if (ctx.out.finiArray) {
1543 addInt(DT_FINI_ARRAY, ctx.out.finiArray->addr);
1544 addInt(DT_FINI_ARRAYSZ, ctx.out.finiArray->size);
1547 if (Symbol *b = ctx.symtab->find(ctx.arg.init))
1548 if (b->isDefined())
1549 addInt(DT_INIT, b->getVA(ctx));
1550 if (Symbol *b = ctx.symtab->find(ctx.arg.fini))
1551 if (b->isDefined())
1552 addInt(DT_FINI, b->getVA(ctx));
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));
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->vernauxs.empty())
1566 ++needNum;
1567 addInt(DT_VERNEEDNUM, needNum);
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());
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));
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);
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);
1605 if (ctx.arg.emachine == EM_PPC64)
1606 addInt(DT_PPC64_OPT, ctx.target->ppc64DynamicSectionOpt);
1608 addInt(DT_NULL, 0);
1609 return entries;
1612 template <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;
1618 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1619 auto *p = reinterpret_cast<Elf_Dyn *>(buf);
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;
1628 uint64_t DynamicReloc::getOffset() const {
1629 return inputSec->getVA(offsetInSec);
1632 int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
1633 switch (kind) {
1634 case AddendOnly:
1635 assert(sym == nullptr);
1636 return addend;
1637 case AgainstSymbol:
1638 assert(sym != nullptr);
1639 return addend;
1640 case AddendOnlyWithTargetVA:
1641 case AgainstSymbolWithTargetVA: {
1642 uint64_t ca = inputSec->getRelocTargetVA(
1643 ctx, Relocation{expr, type, 0, addend, sym}, getOffset());
1644 return ctx.arg.is64 ? ca : SignExtend64<32>(ca);
1646 case MipsMultiGotPage:
1647 assert(sym == nullptr);
1648 return getMipsPageAddr(outputSec->addr) + addend;
1650 llvm_unreachable("Unknown DynamicReloc::Kind enum");
1653 uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
1654 if (!needsDynSymIndex())
1655 return 0;
1657 size_t index = symTab->getSymbolIndex(*sym);
1658 assert((index != 0 ||
1659 (type != symTab->ctx.target->gotRel &&
1660 type != symTab->ctx.target->pltRel) ||
1661 !symTab->ctx.mainPart->dynSymTab->getParent()) &&
1662 "GOT or PLT relocation must refer to symbol in dynamic symbol table");
1663 return index;
1666 RelocationBaseSection::RelocationBaseSection(Ctx &ctx, StringRef name,
1667 uint32_t type, int32_t dynamicTag,
1668 int32_t sizeDynamicTag,
1669 bool combreloc,
1670 unsigned concurrency)
1671 : SyntheticSection(ctx, name, type, SHF_ALLOC, ctx.arg.wordsize),
1672 dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag),
1673 relocsVec(concurrency), combreloc(combreloc) {}
1675 void RelocationBaseSection::addSymbolReloc(
1676 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1677 int64_t addend, std::optional<RelType> addendRelType) {
1678 addReloc(DynamicReloc::AgainstSymbol, dynType, isec, offsetInSec, sym, addend,
1679 R_ADDEND, addendRelType ? *addendRelType : ctx.target->noneRel);
1682 void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
1683 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1684 RelType addendRelType) {
1685 // No need to write an addend to the section for preemptible symbols.
1686 if (sym.isPreemptible)
1687 addReloc({dynType, &isec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0,
1688 R_ABS});
1689 else
1690 addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec, offsetInSec,
1691 sym, 0, R_ABS, addendRelType);
1694 void RelocationBaseSection::mergeRels() {
1695 size_t newSize = relocs.size();
1696 for (const auto &v : relocsVec)
1697 newSize += v.size();
1698 relocs.reserve(newSize);
1699 for (const auto &v : relocsVec)
1700 llvm::append_range(relocs, v);
1701 relocsVec.clear();
1704 void RelocationBaseSection::partitionRels() {
1705 if (!combreloc)
1706 return;
1707 const RelType relativeRel = ctx.target->relativeRel;
1708 numRelativeRelocs =
1709 std::stable_partition(relocs.begin(), relocs.end(),
1710 [=](auto &r) { return r.type == relativeRel; }) -
1711 relocs.begin();
1714 void RelocationBaseSection::finalizeContents() {
1715 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1717 // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1718 // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1719 // case.
1720 if (symTab && symTab->getParent())
1721 getParent()->link = symTab->getParent()->sectionIndex;
1722 else
1723 getParent()->link = 0;
1725 if (ctx.in.relaPlt.get() == this && ctx.in.gotPlt->getParent()) {
1726 getParent()->flags |= ELF::SHF_INFO_LINK;
1727 getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
1731 void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
1732 r_offset = getOffset();
1733 r_sym = getSymIndex(symt);
1734 addend = computeAddend(ctx);
1735 kind = AddendOnly; // Catch errors
1738 void RelocationBaseSection::computeRels() {
1739 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1740 parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
1741 rel.computeRaw(ctx, symTab);
1744 auto irelative = std::stable_partition(
1745 relocs.begin() + numRelativeRelocs, relocs.end(),
1746 [t = ctx.target->iRelativeRel](auto &r) { return r.type != t; });
1748 // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1749 // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1750 // is to make results easier to read.
1751 if (combreloc) {
1752 auto nonRelative = relocs.begin() + numRelativeRelocs;
1753 parallelSort(relocs.begin(), nonRelative,
1754 [&](auto &a, auto &b) { return a.r_offset < b.r_offset; });
1755 // Non-relative relocations are few, so don't bother with parallelSort.
1756 llvm::sort(nonRelative, irelative, [&](auto &a, auto &b) {
1757 return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
1762 template <class ELFT>
1763 RelocationSection<ELFT>::RelocationSection(Ctx &ctx, StringRef name,
1764 bool combreloc, unsigned concurrency)
1765 : RelocationBaseSection(ctx, name, ctx.arg.isRela ? SHT_RELA : SHT_REL,
1766 ctx.arg.isRela ? DT_RELA : DT_REL,
1767 ctx.arg.isRela ? DT_RELASZ : DT_RELSZ, combreloc,
1768 concurrency) {
1769 this->entsize = ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1772 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1773 computeRels();
1774 for (const DynamicReloc &rel : relocs) {
1775 auto *p = reinterpret_cast<Elf_Rela *>(buf);
1776 p->r_offset = rel.r_offset;
1777 p->setSymbolAndType(rel.r_sym, rel.type, ctx.arg.isMips64EL);
1778 if (ctx.arg.isRela)
1779 p->r_addend = rel.addend;
1780 buf += ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1784 RelrBaseSection::RelrBaseSection(Ctx &ctx, unsigned concurrency,
1785 bool isAArch64Auth)
1786 : SyntheticSection(
1787 ctx, isAArch64Auth ? ".relr.auth.dyn" : ".relr.dyn",
1788 isAArch64Auth
1789 ? SHT_AARCH64_AUTH_RELR
1790 : (ctx.arg.useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR),
1791 SHF_ALLOC, ctx.arg.wordsize),
1792 relocsVec(concurrency) {}
1794 void RelrBaseSection::mergeRels() {
1795 size_t newSize = relocs.size();
1796 for (const auto &v : relocsVec)
1797 newSize += v.size();
1798 relocs.reserve(newSize);
1799 for (const auto &v : relocsVec)
1800 llvm::append_range(relocs, v);
1801 relocsVec.clear();
1804 template <class ELFT>
1805 AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1806 Ctx &ctx, StringRef name, unsigned concurrency)
1807 : RelocationBaseSection(
1808 ctx, name, ctx.arg.isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1809 ctx.arg.isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1810 ctx.arg.isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ,
1811 /*combreloc=*/false, concurrency) {
1812 this->entsize = 1;
1815 template <class ELFT>
1816 bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
1817 // This function computes the contents of an Android-format packed relocation
1818 // section.
1820 // This format compresses relocations by using relocation groups to factor out
1821 // fields that are common between relocations and storing deltas from previous
1822 // relocations in SLEB128 format (which has a short representation for small
1823 // numbers). A good example of a relocation type with common fields is
1824 // R_*_RELATIVE, which is normally used to represent function pointers in
1825 // vtables. In the REL format, each relative relocation has the same r_info
1826 // field, and is only different from other relative relocations in terms of
1827 // the r_offset field. By sorting relocations by offset, grouping them by
1828 // r_info and representing each relocation with only the delta from the
1829 // previous offset, each 8-byte relocation can be compressed to as little as 1
1830 // byte (or less with run-length encoding). This relocation packer was able to
1831 // reduce the size of the relocation section in an Android Chromium DSO from
1832 // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1834 // A relocation section consists of a header containing the literal bytes
1835 // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1836 // elements are the total number of relocations in the section and an initial
1837 // r_offset value. The remaining elements define a sequence of relocation
1838 // groups. Each relocation group starts with a header consisting of the
1839 // following elements:
1841 // - the number of relocations in the relocation group
1842 // - flags for the relocation group
1843 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1844 // for each relocation in the group.
1845 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1846 // field for each relocation in the group.
1847 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1848 // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1849 // each relocation in the group.
1851 // Following the relocation group header are descriptions of each of the
1852 // relocations in the group. They consist of the following elements:
1854 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1855 // delta for this relocation.
1856 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1857 // field for this relocation.
1858 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1859 // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1860 // this relocation.
1862 size_t oldSize = relocData.size();
1864 relocData = {'A', 'P', 'S', '2'};
1865 raw_svector_ostream os(relocData);
1866 auto add = [&](int64_t v) { encodeSLEB128(v, os); };
1868 // The format header includes the number of relocations and the initial
1869 // offset (we set this to zero because the first relocation group will
1870 // perform the initial adjustment).
1871 add(relocs.size());
1872 add(0);
1874 std::vector<Elf_Rela> relatives, nonRelatives;
1876 for (const DynamicReloc &rel : relocs) {
1877 Elf_Rela r;
1878 r.r_offset = rel.getOffset();
1879 r.setSymbolAndType(rel.getSymIndex(getPartition(ctx).dynSymTab.get()),
1880 rel.type, false);
1881 r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
1883 if (r.getType(ctx.arg.isMips64EL) == ctx.target->relativeRel)
1884 relatives.push_back(r);
1885 else
1886 nonRelatives.push_back(r);
1889 llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1890 return a.r_offset < b.r_offset;
1893 // Try to find groups of relative relocations which are spaced one word
1894 // apart from one another. These generally correspond to vtable entries. The
1895 // format allows these groups to be encoded using a sort of run-length
1896 // encoding, but each group will cost 7 bytes in addition to the offset from
1897 // the previous group, so it is only profitable to do this for groups of
1898 // size 8 or larger.
1899 std::vector<Elf_Rela> ungroupedRelatives;
1900 std::vector<std::vector<Elf_Rela>> relativeGroups;
1901 for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1902 std::vector<Elf_Rela> group;
1903 do {
1904 group.push_back(*i++);
1905 } while (i != e && (i - 1)->r_offset + ctx.arg.wordsize == i->r_offset);
1907 if (group.size() < 8)
1908 ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1909 group.end());
1910 else
1911 relativeGroups.emplace_back(std::move(group));
1914 // For non-relative relocations, we would like to:
1915 // 1. Have relocations with the same symbol offset to be consecutive, so
1916 // that the runtime linker can speed-up symbol lookup by implementing an
1917 // 1-entry cache.
1918 // 2. Group relocations by r_info to reduce the size of the relocation
1919 // section.
1920 // Since the symbol offset is the high bits in r_info, sorting by r_info
1921 // allows us to do both.
1923 // For Rela, we also want to sort by r_addend when r_info is the same. This
1924 // enables us to group by r_addend as well.
1925 llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1926 if (a.r_info != b.r_info)
1927 return a.r_info < b.r_info;
1928 if (a.r_addend != b.r_addend)
1929 return a.r_addend < b.r_addend;
1930 return a.r_offset < b.r_offset;
1933 // Group relocations with the same r_info. Note that each group emits a group
1934 // header and that may make the relocation section larger. It is hard to
1935 // estimate the size of a group header as the encoded size of that varies
1936 // based on r_info. However, we can approximate this trade-off by the number
1937 // of values encoded. Each group header contains 3 values, and each relocation
1938 // in a group encodes one less value, as compared to when it is not grouped.
1939 // Therefore, we only group relocations if there are 3 or more of them with
1940 // the same r_info.
1942 // For Rela, the addend for most non-relative relocations is zero, and thus we
1943 // can usually get a smaller relocation section if we group relocations with 0
1944 // addend as well.
1945 std::vector<Elf_Rela> ungroupedNonRelatives;
1946 std::vector<std::vector<Elf_Rela>> nonRelativeGroups;
1947 for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) {
1948 auto j = i + 1;
1949 while (j != e && i->r_info == j->r_info &&
1950 (!ctx.arg.isRela || i->r_addend == j->r_addend))
1951 ++j;
1952 if (j - i < 3 || (ctx.arg.isRela && i->r_addend != 0))
1953 ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j);
1954 else
1955 nonRelativeGroups.emplace_back(i, j);
1956 i = j;
1959 // Sort ungrouped relocations by offset to minimize the encoded length.
1960 llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1961 return a.r_offset < b.r_offset;
1964 unsigned hasAddendIfRela =
1965 ctx.arg.isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0;
1967 uint64_t offset = 0;
1968 uint64_t addend = 0;
1970 // Emit the run-length encoding for the groups of adjacent relative
1971 // relocations. Each group is represented using two groups in the packed
1972 // format. The first is used to set the current offset to the start of the
1973 // group (and also encodes the first relocation), and the second encodes the
1974 // remaining relocations.
1975 for (std::vector<Elf_Rela> &g : relativeGroups) {
1976 // The first relocation in the group.
1977 add(1);
1978 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1979 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1980 add(g[0].r_offset - offset);
1981 add(ctx.target->relativeRel);
1982 if (ctx.arg.isRela) {
1983 add(g[0].r_addend - addend);
1984 addend = g[0].r_addend;
1987 // The remaining relocations.
1988 add(g.size() - 1);
1989 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1990 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1991 add(ctx.arg.wordsize);
1992 add(ctx.target->relativeRel);
1993 if (ctx.arg.isRela) {
1994 for (const auto &i : llvm::drop_begin(g)) {
1995 add(i.r_addend - addend);
1996 addend = i.r_addend;
2000 offset = g.back().r_offset;
2003 // Now the ungrouped relatives.
2004 if (!ungroupedRelatives.empty()) {
2005 add(ungroupedRelatives.size());
2006 add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
2007 add(ctx.target->relativeRel);
2008 for (Elf_Rela &r : ungroupedRelatives) {
2009 add(r.r_offset - offset);
2010 offset = r.r_offset;
2011 if (ctx.arg.isRela) {
2012 add(r.r_addend - addend);
2013 addend = r.r_addend;
2018 // Grouped non-relatives.
2019 for (ArrayRef<Elf_Rela> g : nonRelativeGroups) {
2020 add(g.size());
2021 add(RELOCATION_GROUPED_BY_INFO_FLAG);
2022 add(g[0].r_info);
2023 for (const Elf_Rela &r : g) {
2024 add(r.r_offset - offset);
2025 offset = r.r_offset;
2027 addend = 0;
2030 // Finally the ungrouped non-relative relocations.
2031 if (!ungroupedNonRelatives.empty()) {
2032 add(ungroupedNonRelatives.size());
2033 add(hasAddendIfRela);
2034 for (Elf_Rela &r : ungroupedNonRelatives) {
2035 add(r.r_offset - offset);
2036 offset = r.r_offset;
2037 add(r.r_info);
2038 if (ctx.arg.isRela) {
2039 add(r.r_addend - addend);
2040 addend = r.r_addend;
2045 // Don't allow the section to shrink; otherwise the size of the section can
2046 // oscillate infinitely.
2047 if (relocData.size() < oldSize)
2048 relocData.append(oldSize - relocData.size(), 0);
2050 // Returns whether the section size changed. We need to keep recomputing both
2051 // section layout and the contents of this section until the size converges
2052 // because changing this section's size can affect section layout, which in
2053 // turn can affect the sizes of the LEB-encoded integers stored in this
2054 // section.
2055 return relocData.size() != oldSize;
2058 template <class ELFT>
2059 RelrSection<ELFT>::RelrSection(Ctx &ctx, unsigned concurrency,
2060 bool isAArch64Auth)
2061 : RelrBaseSection(ctx, concurrency, isAArch64Auth) {
2062 this->entsize = ctx.arg.wordsize;
2065 template <class ELFT> bool RelrSection<ELFT>::updateAllocSize(Ctx &ctx) {
2066 // This function computes the contents of an SHT_RELR packed relocation
2067 // section.
2069 // Proposal for adding SHT_RELR sections to generic-abi is here:
2070 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
2072 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
2073 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
2075 // i.e. start with an address, followed by any number of bitmaps. The address
2076 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
2077 // relocations each, at subsequent offsets following the last address entry.
2079 // The bitmap entries must have 1 in the least significant bit. The assumption
2080 // here is that an address cannot have 1 in lsb. Odd addresses are not
2081 // supported.
2083 // Excluding the least significant bit in the bitmap, each non-zero bit in
2084 // the bitmap represents a relocation to be applied to a corresponding machine
2085 // word that follows the base address word. The second least significant bit
2086 // represents the machine word immediately following the initial address, and
2087 // each bit that follows represents the next word, in linear order. As such,
2088 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
2089 // 63 relocations in a 64-bit object.
2091 // This encoding has a couple of interesting properties:
2092 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
2093 // even means address, odd means bitmap.
2094 // 2. Just a simple list of addresses is a valid encoding.
2096 size_t oldSize = relrRelocs.size();
2097 relrRelocs.clear();
2099 const size_t wordsize = sizeof(typename ELFT::uint);
2101 // Number of bits to use for the relocation offsets bitmap.
2102 // Must be either 63 or 31.
2103 const size_t nBits = wordsize * 8 - 1;
2105 // Get offsets for all relative relocations and sort them.
2106 std::unique_ptr<uint64_t[]> offsets(new uint64_t[relocs.size()]);
2107 for (auto [i, r] : llvm::enumerate(relocs))
2108 offsets[i] = r.getOffset();
2109 llvm::sort(offsets.get(), offsets.get() + relocs.size());
2111 // For each leading relocation, find following ones that can be folded
2112 // as a bitmap and fold them.
2113 for (size_t i = 0, e = relocs.size(); i != e;) {
2114 // Add a leading relocation.
2115 relrRelocs.push_back(Elf_Relr(offsets[i]));
2116 uint64_t base = offsets[i] + wordsize;
2117 ++i;
2119 // Find foldable relocations to construct bitmaps.
2120 for (;;) {
2121 uint64_t bitmap = 0;
2122 for (; i != e; ++i) {
2123 uint64_t d = offsets[i] - base;
2124 if (d >= nBits * wordsize || d % wordsize)
2125 break;
2126 bitmap |= uint64_t(1) << (d / wordsize);
2128 if (!bitmap)
2129 break;
2130 relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
2131 base += nBits * wordsize;
2135 // Don't allow the section to shrink; otherwise the size of the section can
2136 // oscillate infinitely. Trailing 1s do not decode to more relocations.
2137 if (relrRelocs.size() < oldSize) {
2138 Log(ctx) << ".relr.dyn needs " << (oldSize - relrRelocs.size())
2139 << " padding word(s)";
2140 relrRelocs.resize(oldSize, Elf_Relr(1));
2143 return relrRelocs.size() != oldSize;
2146 SymbolTableBaseSection::SymbolTableBaseSection(Ctx &ctx,
2147 StringTableSection &strTabSec)
2148 : SyntheticSection(ctx, strTabSec.isDynamic() ? ".dynsym" : ".symtab",
2149 strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
2150 strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
2151 ctx.arg.wordsize),
2152 strTabSec(strTabSec) {}
2154 // Orders symbols according to their positions in the GOT,
2155 // in compliance with MIPS ABI rules.
2156 // See "Global Offset Table" in Chapter 5 in the following document
2157 // for detailed description:
2158 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2159 static void sortMipsSymbols(Ctx &ctx, SmallVector<SymbolTableEntry, 0> &syms) {
2160 llvm::stable_sort(syms,
2161 [&](const SymbolTableEntry &l, const SymbolTableEntry &r) {
2162 // Sort entries related to non-local preemptible symbols
2163 // by GOT indexes. All other entries go to the beginning
2164 // of a dynsym in arbitrary order.
2165 if (l.sym->isInGot(ctx) && r.sym->isInGot(ctx))
2166 return l.sym->getGotIdx(ctx) < r.sym->getGotIdx(ctx);
2167 if (!l.sym->isInGot(ctx) && !r.sym->isInGot(ctx))
2168 return false;
2169 return !l.sym->isInGot(ctx);
2173 void SymbolTableBaseSection::finalizeContents() {
2174 if (OutputSection *sec = strTabSec.getParent())
2175 getParent()->link = sec->sectionIndex;
2177 if (this->type != SHT_DYNSYM) {
2178 sortSymTabSymbols();
2179 return;
2182 // If it is a .dynsym, there should be no local symbols, but we need
2183 // to do a few things for the dynamic linker.
2185 // Section's Info field has the index of the first non-local symbol.
2186 // Because the first symbol entry is a null entry, 1 is the first.
2187 getParent()->info = 1;
2189 if (getPartition(ctx).gnuHashTab) {
2190 // NB: It also sorts Symbols to meet the GNU hash table requirements.
2191 getPartition(ctx).gnuHashTab->addSymbols(symbols);
2192 } else if (ctx.arg.emachine == EM_MIPS) {
2193 sortMipsSymbols(ctx, symbols);
2196 // Only the main partition's dynsym indexes are stored in the symbols
2197 // themselves. All other partitions use a lookup table.
2198 if (this == ctx.mainPart->dynSymTab.get()) {
2199 size_t i = 0;
2200 for (const SymbolTableEntry &s : symbols)
2201 s.sym->dynsymIndex = ++i;
2205 // The ELF spec requires that all local symbols precede global symbols, so we
2206 // sort symbol entries in this function. (For .dynsym, we don't do that because
2207 // symbols for dynamic linking are inherently all globals.)
2209 // Aside from above, we put local symbols in groups starting with the STT_FILE
2210 // symbol. That is convenient for purpose of identifying where are local symbols
2211 // coming from.
2212 void SymbolTableBaseSection::sortSymTabSymbols() {
2213 // Move all local symbols before global symbols.
2214 auto e = std::stable_partition(
2215 symbols.begin(), symbols.end(),
2216 [](const SymbolTableEntry &s) { return s.sym->isLocal(); });
2217 size_t numLocals = e - symbols.begin();
2218 getParent()->info = numLocals + 1;
2220 // We want to group the local symbols by file. For that we rebuild the local
2221 // part of the symbols vector. We do not need to care about the STT_FILE
2222 // symbols, they are already naturally placed first in each group. That
2223 // happens because STT_FILE is always the first symbol in the object and hence
2224 // precede all other local symbols we add for a file.
2225 MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr;
2226 for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e))
2227 arr[s.sym->file].push_back(s);
2229 auto i = symbols.begin();
2230 for (auto &p : arr)
2231 for (SymbolTableEntry &entry : p.second)
2232 *i++ = entry;
2235 void SymbolTableBaseSection::addSymbol(Symbol *b) {
2236 // Adding a local symbol to a .dynsym is a bug.
2237 assert(this->type != SHT_DYNSYM || !b->isLocal());
2238 symbols.push_back({b, strTabSec.addString(b->getName(), false)});
2241 size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {
2242 if (this == ctx.mainPart->dynSymTab.get())
2243 return sym.dynsymIndex;
2245 // Initializes symbol lookup tables lazily. This is used only for -r,
2246 // --emit-relocs and dynsyms in partitions other than the main one.
2247 llvm::call_once(onceFlag, [&] {
2248 symbolIndexMap.reserve(symbols.size());
2249 size_t i = 0;
2250 for (const SymbolTableEntry &e : symbols) {
2251 if (e.sym->type == STT_SECTION)
2252 sectionIndexMap[e.sym->getOutputSection()] = ++i;
2253 else
2254 symbolIndexMap[e.sym] = ++i;
2258 // Section symbols are mapped based on their output sections
2259 // to maintain their semantics.
2260 if (sym.type == STT_SECTION)
2261 return sectionIndexMap.lookup(sym.getOutputSection());
2262 return symbolIndexMap.lookup(&sym);
2265 template <class ELFT>
2266 SymbolTableSection<ELFT>::SymbolTableSection(Ctx &ctx,
2267 StringTableSection &strTabSec)
2268 : SymbolTableBaseSection(ctx, strTabSec) {
2269 this->entsize = sizeof(Elf_Sym);
2272 static BssSection *getCommonSec(bool relocatable, Symbol *sym) {
2273 if (relocatable)
2274 if (auto *d = dyn_cast<Defined>(sym))
2275 return dyn_cast_or_null<BssSection>(d->section);
2276 return nullptr;
2279 static uint32_t getSymSectionIndex(Symbol *sym) {
2280 assert(!(sym->hasFlag(NEEDS_COPY) && sym->isObject()));
2281 if (!isa<Defined>(sym) || sym->hasFlag(NEEDS_COPY))
2282 return SHN_UNDEF;
2283 if (const OutputSection *os = sym->getOutputSection())
2284 return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX
2285 : os->sectionIndex;
2286 return SHN_ABS;
2289 // Write the internal symbol table contents to the output symbol table.
2290 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2291 // The first entry is a null entry as per the ELF spec.
2292 buf += sizeof(Elf_Sym);
2294 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2295 bool relocatable = ctx.arg.relocatable;
2296 for (SymbolTableEntry &ent : symbols) {
2297 Symbol *sym = ent.sym;
2298 bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
2300 // Set st_name, st_info and st_other.
2301 eSym->st_name = ent.strTabOffset;
2302 eSym->setBindingAndType(sym->binding, sym->type);
2303 eSym->st_other = sym->stOther;
2305 if (BssSection *commonSec = getCommonSec(relocatable, sym)) {
2306 // When -r is specified, a COMMON symbol is not allocated. Its st_shndx
2307 // holds SHN_COMMON and st_value holds the alignment.
2308 eSym->st_shndx = SHN_COMMON;
2309 eSym->st_value = commonSec->addralign;
2310 eSym->st_size = cast<Defined>(sym)->size;
2311 } else {
2312 const uint32_t shndx = getSymSectionIndex(sym);
2313 if (isDefinedHere) {
2314 eSym->st_shndx = shndx;
2315 eSym->st_value = sym->getVA(ctx);
2316 // Copy symbol size if it is a defined symbol. st_size is not
2317 // significant for undefined symbols, so whether copying it or not is up
2318 // to us if that's the case. We'll leave it as zero because by not
2319 // setting a value, we can get the exact same outputs for two sets of
2320 // input files that differ only in undefined symbol size in DSOs.
2321 eSym->st_size = shndx != SHN_UNDEF ? cast<Defined>(sym)->size : 0;
2322 } else {
2323 eSym->st_shndx = 0;
2324 eSym->st_value = 0;
2325 eSym->st_size = 0;
2329 ++eSym;
2332 // On MIPS we need to mark symbol which has a PLT entry and requires
2333 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2334 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2335 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2336 if (ctx.arg.emachine == EM_MIPS) {
2337 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2339 for (SymbolTableEntry &ent : symbols) {
2340 Symbol *sym = ent.sym;
2341 if (sym->isInPlt(ctx) && sym->hasFlag(NEEDS_COPY))
2342 eSym->st_other |= STO_MIPS_PLT;
2343 if (isMicroMips(ctx)) {
2344 // We already set the less-significant bit for symbols
2345 // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2346 // records. That allows us to distinguish such symbols in
2347 // the `MIPS<ELFT>::relocate()` routine. Now we should
2348 // clear that bit for non-dynamic symbol table, so tools
2349 // like `objdump` will be able to deal with a correct
2350 // symbol position.
2351 if (sym->isDefined() &&
2352 ((sym->stOther & STO_MIPS_MICROMIPS) || sym->hasFlag(NEEDS_COPY))) {
2353 if (!strTabSec.isDynamic())
2354 eSym->st_value &= ~1;
2355 eSym->st_other |= STO_MIPS_MICROMIPS;
2358 if (ctx.arg.relocatable)
2359 if (auto *d = dyn_cast<Defined>(sym))
2360 if (isMipsPIC<ELFT>(d))
2361 eSym->st_other |= STO_MIPS_PIC;
2362 ++eSym;
2367 SymtabShndxSection::SymtabShndxSection(Ctx &ctx)
2368 : SyntheticSection(ctx, ".symtab_shndx", SHT_SYMTAB_SHNDX, 0, 4) {
2369 this->entsize = 4;
2372 void SymtabShndxSection::writeTo(uint8_t *buf) {
2373 // We write an array of 32 bit values, where each value has 1:1 association
2374 // with an entry in ctx.in.symTab if the corresponding entry contains
2375 // SHN_XINDEX, we need to write actual index, otherwise, we must write
2376 // SHN_UNDEF(0).
2377 buf += 4; // Ignore .symtab[0] entry.
2378 bool relocatable = ctx.arg.relocatable;
2379 for (const SymbolTableEntry &entry : ctx.in.symTab->getSymbols()) {
2380 if (!getCommonSec(relocatable, entry.sym) &&
2381 getSymSectionIndex(entry.sym) == SHN_XINDEX)
2382 write32(ctx, buf, entry.sym->getOutputSection()->sectionIndex);
2383 buf += 4;
2387 bool SymtabShndxSection::isNeeded() const {
2388 // SHT_SYMTAB can hold symbols with section indices values up to
2389 // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
2390 // section. Problem is that we reveal the final section indices a bit too
2391 // late, and we do not know them here. For simplicity, we just always create
2392 // a .symtab_shndx section when the amount of output sections is huge.
2393 size_t size = 0;
2394 for (SectionCommand *cmd : ctx.script->sectionCommands)
2395 if (isa<OutputDesc>(cmd))
2396 ++size;
2397 return size >= SHN_LORESERVE;
2400 void SymtabShndxSection::finalizeContents() {
2401 getParent()->link = ctx.in.symTab->getParent()->sectionIndex;
2404 size_t SymtabShndxSection::getSize() const {
2405 return ctx.in.symTab->getNumSymbols() * 4;
2408 // .hash and .gnu.hash sections contain on-disk hash tables that map
2409 // symbol names to their dynamic symbol table indices. Their purpose
2410 // is to help the dynamic linker resolve symbols quickly. If ELF files
2411 // don't have them, the dynamic linker has to do linear search on all
2412 // dynamic symbols, which makes programs slower. Therefore, a .hash
2413 // section is added to a DSO by default.
2415 // The Unix semantics of resolving dynamic symbols is somewhat expensive.
2416 // Each ELF file has a list of DSOs that the ELF file depends on and a
2417 // list of dynamic symbols that need to be resolved from any of the
2418 // DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
2419 // where m is the number of DSOs and n is the number of dynamic
2420 // symbols. For modern large programs, both m and n are large. So
2421 // making each step faster by using hash tables substantially
2422 // improves time to load programs.
2424 // (Note that this is not the only way to design the shared library.
2425 // For instance, the Windows DLL takes a different approach. On
2426 // Windows, each dynamic symbol has a name of DLL from which the symbol
2427 // has to be resolved. That makes the cost of symbol resolution O(n).
2428 // This disables some hacky techniques you can use on Unix such as
2429 // LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
2431 // Due to historical reasons, we have two different hash tables, .hash
2432 // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
2433 // and better version of .hash. .hash is just an on-disk hash table, but
2434 // .gnu.hash has a bloom filter in addition to a hash table to skip
2435 // DSOs very quickly. If you are sure that your dynamic linker knows
2436 // about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a
2437 // safe bet is to specify --hash-style=both for backward compatibility.
2438 GnuHashTableSection::GnuHashTableSection(Ctx &ctx)
2439 : SyntheticSection(ctx, ".gnu.hash", SHT_GNU_HASH, SHF_ALLOC,
2440 ctx.arg.wordsize) {}
2442 void GnuHashTableSection::finalizeContents() {
2443 if (OutputSection *sec = getPartition(ctx).dynSymTab->getParent())
2444 getParent()->link = sec->sectionIndex;
2446 // Computes bloom filter size in word size. We want to allocate 12
2447 // bits for each symbol. It must be a power of two.
2448 if (symbols.empty()) {
2449 maskWords = 1;
2450 } else {
2451 uint64_t numBits = symbols.size() * 12;
2452 maskWords = NextPowerOf2(numBits / (ctx.arg.wordsize * 8));
2455 size = 16; // Header
2456 size += ctx.arg.wordsize * maskWords; // Bloom filter
2457 size += nBuckets * 4; // Hash buckets
2458 size += symbols.size() * 4; // Hash values
2461 void GnuHashTableSection::writeTo(uint8_t *buf) {
2462 // Write a header.
2463 write32(ctx, buf, nBuckets);
2464 write32(ctx, buf + 4,
2465 getPartition(ctx).dynSymTab->getNumSymbols() - symbols.size());
2466 write32(ctx, buf + 8, maskWords);
2467 write32(ctx, buf + 12, Shift2);
2468 buf += 16;
2470 // Write the 2-bit bloom filter.
2471 const unsigned c = ctx.arg.is64 ? 64 : 32;
2472 for (const Entry &sym : symbols) {
2473 // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in
2474 // the word using bits [0:5] and [26:31].
2475 size_t i = (sym.hash / c) & (maskWords - 1);
2476 uint64_t val = readUint(ctx, buf + i * ctx.arg.wordsize);
2477 val |= uint64_t(1) << (sym.hash % c);
2478 val |= uint64_t(1) << ((sym.hash >> Shift2) % c);
2479 writeUint(ctx, buf + i * ctx.arg.wordsize, val);
2481 buf += ctx.arg.wordsize * maskWords;
2483 // Write the hash table.
2484 uint32_t *buckets = reinterpret_cast<uint32_t *>(buf);
2485 uint32_t oldBucket = -1;
2486 uint32_t *values = buckets + nBuckets;
2487 for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) {
2488 // Write a hash value. It represents a sequence of chains that share the
2489 // same hash modulo value. The last element of each chain is terminated by
2490 // LSB 1.
2491 uint32_t hash = i->hash;
2492 bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx;
2493 hash = isLastInChain ? hash | 1 : hash & ~1;
2494 write32(ctx, values++, hash);
2496 if (i->bucketIdx == oldBucket)
2497 continue;
2498 // Write a hash bucket. Hash buckets contain indices in the following hash
2499 // value table.
2500 write32(ctx, buckets + i->bucketIdx,
2501 getPartition(ctx).dynSymTab->getSymbolIndex(*i->sym));
2502 oldBucket = i->bucketIdx;
2506 // Add symbols to this symbol hash table. Note that this function
2507 // destructively sort a given vector -- which is needed because
2508 // GNU-style hash table places some sorting requirements.
2509 void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) {
2510 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
2511 // its type correctly.
2512 auto mid =
2513 std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) {
2514 return !s.sym->isDefined() || s.sym->partition != partition;
2517 // We chose load factor 4 for the on-disk hash table. For each hash
2518 // collision, the dynamic linker will compare a uint32_t hash value.
2519 // Since the integer comparison is quite fast, we believe we can
2520 // make the load factor even larger. 4 is just a conservative choice.
2522 // Note that we don't want to create a zero-sized hash table because
2523 // Android loader as of 2018 doesn't like a .gnu.hash containing such
2524 // table. If that's the case, we create a hash table with one unused
2525 // dummy slot.
2526 nBuckets = std::max<size_t>((v.end() - mid) / 4, 1);
2528 if (mid == v.end())
2529 return;
2531 for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) {
2532 Symbol *b = ent.sym;
2533 uint32_t hash = hashGnu(b->getName());
2534 uint32_t bucketIdx = hash % nBuckets;
2535 symbols.push_back({b, ent.strTabOffset, hash, bucketIdx});
2538 llvm::sort(symbols, [](const Entry &l, const Entry &r) {
2539 return std::tie(l.bucketIdx, l.strTabOffset) <
2540 std::tie(r.bucketIdx, r.strTabOffset);
2543 v.erase(mid, v.end());
2544 for (const Entry &ent : symbols)
2545 v.push_back({ent.sym, ent.strTabOffset});
2548 HashTableSection::HashTableSection(Ctx &ctx)
2549 : SyntheticSection(ctx, ".hash", SHT_HASH, SHF_ALLOC, 4) {
2550 this->entsize = 4;
2553 void HashTableSection::finalizeContents() {
2554 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2556 if (OutputSection *sec = symTab->getParent())
2557 getParent()->link = sec->sectionIndex;
2559 unsigned numEntries = 2; // nbucket and nchain.
2560 numEntries += symTab->getNumSymbols(); // The chain entries.
2562 // Create as many buckets as there are symbols.
2563 numEntries += symTab->getNumSymbols();
2564 this->size = numEntries * 4;
2567 void HashTableSection::writeTo(uint8_t *buf) {
2568 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2569 unsigned numSymbols = symTab->getNumSymbols();
2571 uint32_t *p = reinterpret_cast<uint32_t *>(buf);
2572 write32(ctx, p++, numSymbols); // nbucket
2573 write32(ctx, p++, numSymbols); // nchain
2575 uint32_t *buckets = p;
2576 uint32_t *chains = p + numSymbols;
2578 for (const SymbolTableEntry &s : symTab->getSymbols()) {
2579 Symbol *sym = s.sym;
2580 StringRef name = sym->getName();
2581 unsigned i = sym->dynsymIndex;
2582 uint32_t hash = hashSysV(name) % numSymbols;
2583 chains[i] = buckets[hash];
2584 write32(ctx, buckets + hash, i);
2588 PltSection::PltSection(Ctx &ctx)
2589 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2590 16),
2591 headerSize(ctx.target->pltHeaderSize) {
2592 // On PowerPC, this section contains lazy symbol resolvers.
2593 if (ctx.arg.emachine == EM_PPC64) {
2594 name = ".glink";
2595 addralign = 4;
2598 // On x86 when IBT is enabled, this section contains the second PLT (lazy
2599 // symbol resolvers).
2600 if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
2601 (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT))
2602 name = ".plt.sec";
2604 // The PLT needs to be writable on SPARC as the dynamic linker will
2605 // modify the instructions in the PLT entries.
2606 if (ctx.arg.emachine == EM_SPARCV9)
2607 this->flags |= SHF_WRITE;
2610 void PltSection::writeTo(uint8_t *buf) {
2611 // At beginning of PLT, we have code to call the dynamic
2612 // linker to resolve dynsyms at runtime. Write such code.
2613 ctx.target->writePltHeader(buf);
2614 size_t off = headerSize;
2616 for (const Symbol *sym : entries) {
2617 ctx.target->writePlt(buf + off, *sym, getVA() + off);
2618 off += ctx.target->pltEntrySize;
2622 void PltSection::addEntry(Symbol &sym) {
2623 assert(sym.auxIdx == ctx.symAux.size() - 1);
2624 ctx.symAux.back().pltIdx = entries.size();
2625 entries.push_back(&sym);
2628 size_t PltSection::getSize() const {
2629 return headerSize + entries.size() * ctx.target->pltEntrySize;
2632 bool PltSection::isNeeded() const {
2633 // For -z retpolineplt, .iplt needs the .plt header.
2634 return !entries.empty() || (ctx.arg.zRetpolineplt && ctx.in.iplt->isNeeded());
2637 // Used by ARM to add mapping symbols in the PLT section, which aid
2638 // disassembly.
2639 void PltSection::addSymbols() {
2640 ctx.target->addPltHeaderSymbols(*this);
2642 size_t off = headerSize;
2643 for (size_t i = 0; i < entries.size(); ++i) {
2644 ctx.target->addPltSymbols(*this, off);
2645 off += ctx.target->pltEntrySize;
2649 IpltSection::IpltSection(Ctx &ctx)
2650 : SyntheticSection(ctx, ".iplt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2651 16) {
2652 if (ctx.arg.emachine == EM_PPC || ctx.arg.emachine == EM_PPC64) {
2653 name = ".glink";
2654 addralign = 4;
2658 void IpltSection::writeTo(uint8_t *buf) {
2659 uint32_t off = 0;
2660 for (const Symbol *sym : entries) {
2661 ctx.target->writeIplt(buf + off, *sym, getVA() + off);
2662 off += ctx.target->ipltEntrySize;
2666 size_t IpltSection::getSize() const {
2667 return entries.size() * ctx.target->ipltEntrySize;
2670 void IpltSection::addEntry(Symbol &sym) {
2671 assert(sym.auxIdx == ctx.symAux.size() - 1);
2672 ctx.symAux.back().pltIdx = entries.size();
2673 entries.push_back(&sym);
2676 // ARM uses mapping symbols to aid disassembly.
2677 void IpltSection::addSymbols() {
2678 size_t off = 0;
2679 for (size_t i = 0, e = entries.size(); i != e; ++i) {
2680 ctx.target->addPltSymbols(*this, off);
2681 off += ctx.target->pltEntrySize;
2685 PPC32GlinkSection::PPC32GlinkSection(Ctx &ctx) : PltSection(ctx) {
2686 name = ".glink";
2687 addralign = 4;
2690 void PPC32GlinkSection::writeTo(uint8_t *buf) {
2691 writePPC32GlinkSection(ctx, buf, entries.size());
2694 size_t PPC32GlinkSection::getSize() const {
2695 return headerSize + entries.size() * ctx.target->pltEntrySize + footerSize;
2698 // This is an x86-only extra PLT section and used only when a security
2699 // enhancement feature called CET is enabled. In this comment, I'll explain what
2700 // the feature is and why we have two PLT sections if CET is enabled.
2702 // So, what does CET do? CET introduces a new restriction to indirect jump
2703 // instructions. CET works this way. Assume that CET is enabled. Then, if you
2704 // execute an indirect jump instruction, the processor verifies that a special
2705 // "landing pad" instruction (which is actually a repurposed NOP instruction and
2706 // now called "endbr32" or "endbr64") is at the jump target. If the jump target
2707 // does not start with that instruction, the processor raises an exception
2708 // instead of continuing executing code.
2710 // If CET is enabled, the compiler emits endbr to all locations where indirect
2711 // jumps may jump to.
2713 // This mechanism makes it extremely hard to transfer the control to a middle of
2714 // a function that is not supporsed to be a indirect jump target, preventing
2715 // certain types of attacks such as ROP or JOP.
2717 // Note that the processors in the market as of 2019 don't actually support the
2718 // feature. Only the spec is available at the moment.
2720 // Now, I'll explain why we have this extra PLT section for CET.
2722 // Since you can indirectly jump to a PLT entry, we have to make PLT entries
2723 // start with endbr. The problem is there's no extra space for endbr (which is 4
2724 // bytes long), as the PLT entry is only 16 bytes long and all bytes are already
2725 // used.
2727 // In order to deal with the issue, we split a PLT entry into two PLT entries.
2728 // Remember that each PLT entry contains code to jump to an address read from
2729 // .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme,
2730 // the former code is written to .plt.sec, and the latter code is written to
2731 // .plt.
2733 // Lazy symbol resolution in the 2-PLT scheme works in the usual way, except
2734 // that the regular .plt is now called .plt.sec and .plt is repurposed to
2735 // contain only code for lazy symbol resolution.
2737 // In other words, this is how the 2-PLT scheme works. Application code is
2738 // supposed to jump to .plt.sec to call an external function. Each .plt.sec
2739 // entry contains code to read an address from a corresponding .got.plt entry
2740 // and jump to that address. Addresses in .got.plt initially point to .plt, so
2741 // when an application calls an external function for the first time, the
2742 // control is transferred to a function that resolves a symbol name from
2743 // external shared object files. That function then rewrites a .got.plt entry
2744 // with a resolved address, so that the subsequent function calls directly jump
2745 // to a desired location from .plt.sec.
2747 // There is an open question as to whether the 2-PLT scheme was desirable or
2748 // not. We could have simply extended the PLT entry size to 32-bytes to
2749 // accommodate endbr, and that scheme would have been much simpler than the
2750 // 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot
2751 // code (.plt.sec) from cold code (.plt). But as far as I know no one proved
2752 // that the optimization actually makes a difference.
2754 // That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools
2755 // depend on it, so we implement the ABI.
2756 IBTPltSection::IBTPltSection(Ctx &ctx)
2757 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2758 16) {}
2760 void IBTPltSection::writeTo(uint8_t *buf) {
2761 ctx.target->writeIBTPlt(buf, ctx.in.plt->getNumEntries());
2764 size_t IBTPltSection::getSize() const {
2765 // 16 is the header size of .plt.
2766 return 16 + ctx.in.plt->getNumEntries() * ctx.target->pltEntrySize;
2769 bool IBTPltSection::isNeeded() const { return ctx.in.plt->getNumEntries() > 0; }
2771 RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
2772 : SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
2773 1) {}
2775 RandomizePaddingSection::RandomizePaddingSection(Ctx &ctx, uint64_t size,
2776 OutputSection *parent)
2777 : SyntheticSection(ctx, ".randomize_padding", SHT_PROGBITS, SHF_ALLOC, 1),
2778 size(size) {
2779 this->parent = parent;
2782 void RandomizePaddingSection::writeTo(uint8_t *buf) {
2783 std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
2784 uint8_t *end = buf + size;
2785 for (; buf + 4 <= end; buf += 4)
2786 memcpy(buf, &filler[0], 4);
2787 memcpy(buf, &filler[0], end - buf);
2790 // The string hash function for .gdb_index.
2791 static uint32_t computeGdbHash(StringRef s) {
2792 uint32_t h = 0;
2793 for (uint8_t c : s)
2794 h = h * 67 + toLower(c) - 113;
2795 return h;
2798 // 4-byte alignment ensures that values in the hash lookup table and the name
2799 // table are aligned.
2800 DebugNamesBaseSection::DebugNamesBaseSection(Ctx &ctx)
2801 : SyntheticSection(ctx, ".debug_names", SHT_PROGBITS, 0, 4) {}
2803 // Get the size of the .debug_names section header in bytes for DWARF32:
2804 static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {
2805 return /* unit length */ 4 +
2806 /* version */ 2 +
2807 /* padding */ 2 +
2808 /* CU count */ 4 +
2809 /* TU count */ 4 +
2810 /* Foreign TU count */ 4 +
2811 /* Bucket Count */ 4 +
2812 /* Name Count */ 4 +
2813 /* Abbrev table size */ 4 +
2814 /* Augmentation string size */ 4 +
2815 /* Augmentation string */ augmentationStringSize;
2818 static Expected<DebugNamesBaseSection::IndexEntry *>
2819 readEntry(uint64_t &offset, const DWARFDebugNames::NameIndex &ni,
2820 uint64_t entriesBase, DWARFDataExtractor &namesExtractor,
2821 const LLDDWARFSection &namesSec) {
2822 auto ie = makeThreadLocal<DebugNamesBaseSection::IndexEntry>();
2823 ie->poolOffset = offset;
2824 Error err = Error::success();
2825 uint64_t ulebVal = namesExtractor.getULEB128(&offset, &err);
2826 if (err)
2827 return createStringError(inconvertibleErrorCode(),
2828 "invalid abbrev code: %s",
2829 llvm::toString(std::move(err)).c_str());
2830 if (!isUInt<32>(ulebVal))
2831 return createStringError(inconvertibleErrorCode(),
2832 "abbrev code too large for DWARF32: %" PRIu64,
2833 ulebVal);
2834 ie->abbrevCode = static_cast<uint32_t>(ulebVal);
2835 auto it = ni.getAbbrevs().find_as(ie->abbrevCode);
2836 if (it == ni.getAbbrevs().end())
2837 return createStringError(inconvertibleErrorCode(),
2838 "abbrev code not found in abbrev table: %" PRIu32,
2839 ie->abbrevCode);
2841 DebugNamesBaseSection::AttrValue attr, cuAttr = {0, 0};
2842 for (DWARFDebugNames::AttributeEncoding a : it->Attributes) {
2843 if (a.Index == dwarf::DW_IDX_parent) {
2844 if (a.Form == dwarf::DW_FORM_ref4) {
2845 attr.attrValue = namesExtractor.getU32(&offset, &err);
2846 attr.attrSize = 4;
2847 ie->parentOffset = entriesBase + attr.attrValue;
2848 } else if (a.Form != DW_FORM_flag_present)
2849 return createStringError(inconvertibleErrorCode(),
2850 "invalid form for DW_IDX_parent");
2851 } else {
2852 switch (a.Form) {
2853 case DW_FORM_data1:
2854 case DW_FORM_ref1: {
2855 attr.attrValue = namesExtractor.getU8(&offset, &err);
2856 attr.attrSize = 1;
2857 break;
2859 case DW_FORM_data2:
2860 case DW_FORM_ref2: {
2861 attr.attrValue = namesExtractor.getU16(&offset, &err);
2862 attr.attrSize = 2;
2863 break;
2865 case DW_FORM_data4:
2866 case DW_FORM_ref4: {
2867 attr.attrValue = namesExtractor.getU32(&offset, &err);
2868 attr.attrSize = 4;
2869 break;
2871 default:
2872 return createStringError(
2873 inconvertibleErrorCode(),
2874 "unrecognized form encoding %d in abbrev table", a.Form);
2877 if (err)
2878 return createStringError(inconvertibleErrorCode(),
2879 "error while reading attributes: %s",
2880 llvm::toString(std::move(err)).c_str());
2881 if (a.Index == DW_IDX_compile_unit)
2882 cuAttr = attr;
2883 else if (a.Form != DW_FORM_flag_present)
2884 ie->attrValues.push_back(attr);
2886 // Canonicalize abbrev by placing the CU/TU index at the end.
2887 ie->attrValues.push_back(cuAttr);
2888 return ie;
2891 void DebugNamesBaseSection::parseDebugNames(
2892 Ctx &ctx, InputChunk &inputChunk, OutputChunk &chunk,
2893 DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,
2894 function_ref<SmallVector<uint32_t, 0>(
2895 uint32_t numCus, const DWARFDebugNames::Header &,
2896 const DWARFDebugNames::DWARFDebugNamesOffsets &)>
2897 readOffsets) {
2898 const LLDDWARFSection &namesSec = inputChunk.section;
2899 DenseMap<uint32_t, IndexEntry *> offsetMap;
2900 // Number of CUs seen in previous NameIndex sections within current chunk.
2901 uint32_t numCus = 0;
2902 for (const DWARFDebugNames::NameIndex &ni : *inputChunk.llvmDebugNames) {
2903 NameData &nd = inputChunk.nameData.emplace_back();
2904 nd.hdr = ni.getHeader();
2905 if (nd.hdr.Format != DwarfFormat::DWARF32) {
2906 Err(ctx) << namesSec.sec
2907 << ": found DWARF64, which is currently unsupported";
2908 return;
2910 if (nd.hdr.Version != 5) {
2911 Err(ctx) << namesSec.sec << ": unsupported version: " << nd.hdr.Version;
2912 return;
2914 uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize(DwarfFormat::DWARF32);
2915 DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();
2916 if (locs.EntriesBase > namesExtractor.getData().size()) {
2917 Err(ctx) << namesSec.sec << ": entry pool start is beyond end of section";
2918 return;
2921 SmallVector<uint32_t, 0> entryOffsets = readOffsets(numCus, nd.hdr, locs);
2923 // Read the entry pool.
2924 offsetMap.clear();
2925 nd.nameEntries.resize(nd.hdr.NameCount);
2926 for (auto i : seq(nd.hdr.NameCount)) {
2927 NameEntry &ne = nd.nameEntries[i];
2928 uint64_t strOffset = locs.StringOffsetsBase + i * dwarfSize;
2929 ne.stringOffset = strOffset;
2930 uint64_t strp = namesExtractor.getRelocatedValue(dwarfSize, &strOffset);
2931 StringRef name = strExtractor.getCStrRef(&strp);
2932 ne.name = name.data();
2933 ne.hashValue = caseFoldingDjbHash(name);
2935 // Read a series of index entries that end with abbreviation code 0.
2936 uint64_t offset = locs.EntriesBase + entryOffsets[i];
2937 while (offset < namesSec.Data.size() && namesSec.Data[offset] != 0) {
2938 // Read & store all entries (for the same string).
2939 Expected<IndexEntry *> ieOrErr =
2940 readEntry(offset, ni, locs.EntriesBase, namesExtractor, namesSec);
2941 if (!ieOrErr) {
2942 Err(ctx) << namesSec.sec << ": " << ieOrErr.takeError();
2943 return;
2945 ne.indexEntries.push_back(std::move(*ieOrErr));
2947 if (offset >= namesSec.Data.size())
2948 Err(ctx) << namesSec.sec << ": index entry is out of bounds";
2950 for (IndexEntry &ie : ne.entries())
2951 offsetMap[ie.poolOffset] = &ie;
2954 // Assign parent pointers, which will be used to update DW_IDX_parent index
2955 // attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will
2956 // get parentEntry == null as well.
2957 for (NameEntry &ne : nd.nameEntries)
2958 for (IndexEntry &ie : ne.entries())
2959 ie.parentEntry = offsetMap.lookup(ie.parentOffset);
2960 numCus += nd.hdr.CompUnitCount;
2964 // Compute the form for output DW_IDX_compile_unit attributes, similar to
2965 // DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all
2966 // the merged CU indices.
2967 std::pair<uint8_t, dwarf::Form> static getMergedCuCountForm(
2968 uint32_t compUnitCount) {
2969 if (compUnitCount > UINT16_MAX)
2970 return {4, DW_FORM_data4};
2971 if (compUnitCount > UINT8_MAX)
2972 return {2, DW_FORM_data2};
2973 return {1, DW_FORM_data1};
2976 void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2977 MutableArrayRef<InputChunk> inputChunks) {
2978 TimeTraceScope timeScope("Merge .debug_names", "hdr and abbrev table");
2979 size_t numCu = 0;
2980 hdr.Format = DwarfFormat::DWARF32;
2981 hdr.Version = 5;
2982 hdr.CompUnitCount = 0;
2983 hdr.LocalTypeUnitCount = 0;
2984 hdr.ForeignTypeUnitCount = 0;
2985 hdr.AugmentationStringSize = 0;
2987 // Compute CU and TU counts.
2988 for (auto i : seq(numChunks)) {
2989 InputChunk &inputChunk = inputChunks[i];
2990 inputChunk.baseCuIdx = numCu;
2991 numCu += chunks[i].compUnits.size();
2992 for (const NameData &nd : inputChunk.nameData) {
2993 hdr.CompUnitCount += nd.hdr.CompUnitCount;
2994 // TODO: We don't handle type units yet, so LocalTypeUnitCount &
2995 // ForeignTypeUnitCount are left as 0.
2996 if (nd.hdr.LocalTypeUnitCount || nd.hdr.ForeignTypeUnitCount)
2997 Warn(ctx) << inputChunk.section.sec
2998 << ": type units are not implemented";
2999 // If augmentation strings are not identical, use an empty string.
3000 if (i == 0) {
3001 hdr.AugmentationStringSize = nd.hdr.AugmentationStringSize;
3002 hdr.AugmentationString = nd.hdr.AugmentationString;
3003 } else if (hdr.AugmentationString != nd.hdr.AugmentationString) {
3004 // There are conflicting augmentation strings, so it's best for the
3005 // merged index to not use an augmentation string.
3006 hdr.AugmentationStringSize = 0;
3007 hdr.AugmentationString.clear();
3012 // Create the merged abbrev table, uniquifyinng the input abbrev tables and
3013 // computing mapping from old (per-cu) abbrev codes to new (merged) abbrev
3014 // codes.
3015 FoldingSet<Abbrev> abbrevSet;
3016 // Determine the form for the DW_IDX_compile_unit attributes in the merged
3017 // index. The input form may not be big enough for all CU indices.
3018 dwarf::Form cuAttrForm = getMergedCuCountForm(hdr.CompUnitCount).second;
3019 for (InputChunk &inputChunk : inputChunks) {
3020 for (auto [i, ni] : enumerate(*inputChunk.llvmDebugNames)) {
3021 for (const DWARFDebugNames::Abbrev &oldAbbrev : ni.getAbbrevs()) {
3022 // Canonicalize abbrev by placing the CU/TU index at the end,
3023 // similar to 'parseDebugNames'.
3024 Abbrev abbrev;
3025 DWARFDebugNames::AttributeEncoding cuAttr(DW_IDX_compile_unit,
3026 cuAttrForm);
3027 abbrev.code = oldAbbrev.Code;
3028 abbrev.tag = oldAbbrev.Tag;
3029 for (DWARFDebugNames::AttributeEncoding a : oldAbbrev.Attributes) {
3030 if (a.Index == DW_IDX_compile_unit)
3031 cuAttr.Index = a.Index;
3032 else
3033 abbrev.attributes.push_back({a.Index, a.Form});
3035 // Put the CU/TU index at the end of the attributes list.
3036 abbrev.attributes.push_back(cuAttr);
3038 // Profile the abbrev, get or assign a new code, then record the abbrev
3039 // code mapping.
3040 FoldingSetNodeID id;
3041 abbrev.Profile(id);
3042 uint32_t newCode;
3043 void *insertPos;
3044 if (Abbrev *existing = abbrevSet.FindNodeOrInsertPos(id, insertPos)) {
3045 // Found it; we've already seen an identical abbreviation.
3046 newCode = existing->code;
3047 } else {
3048 Abbrev *abbrev2 =
3049 new (abbrevAlloc.Allocate()) Abbrev(std::move(abbrev));
3050 abbrevSet.InsertNode(abbrev2, insertPos);
3051 abbrevTable.push_back(abbrev2);
3052 newCode = abbrevTable.size();
3053 abbrev2->code = newCode;
3055 inputChunk.nameData[i].abbrevCodeMap[oldAbbrev.Code] = newCode;
3060 // Compute the merged abbrev table.
3061 raw_svector_ostream os(abbrevTableBuf);
3062 for (Abbrev *abbrev : abbrevTable) {
3063 encodeULEB128(abbrev->code, os);
3064 encodeULEB128(abbrev->tag, os);
3065 for (DWARFDebugNames::AttributeEncoding a : abbrev->attributes) {
3066 encodeULEB128(a.Index, os);
3067 encodeULEB128(a.Form, os);
3069 os.write("\0", 2); // attribute specification end
3071 os.write(0); // abbrev table end
3072 hdr.AbbrevTableSize = abbrevTableBuf.size();
3075 void DebugNamesBaseSection::Abbrev::Profile(FoldingSetNodeID &id) const {
3076 id.AddInteger(tag);
3077 for (const DWARFDebugNames::AttributeEncoding &attr : attributes) {
3078 id.AddInteger(attr.Index);
3079 id.AddInteger(attr.Form);
3083 std::pair<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(
3084 MutableArrayRef<InputChunk> inputChunks) {
3085 TimeTraceScope timeScope("Merge .debug_names", "entry pool");
3086 // Collect and de-duplicate all the names (preserving all the entries).
3087 // Speed it up using multithreading, as the number of symbols can be in the
3088 // order of millions.
3089 const size_t concurrency =
3090 bit_floor(std::min<size_t>(ctx.arg.threadCount, numShards));
3091 const size_t shift = 32 - countr_zero(numShards);
3092 const uint8_t cuAttrSize = getMergedCuCountForm(hdr.CompUnitCount).first;
3093 DenseMap<CachedHashStringRef, size_t> maps[numShards];
3095 parallelFor(0, concurrency, [&](size_t threadId) {
3096 for (auto i : seq(numChunks)) {
3097 InputChunk &inputChunk = inputChunks[i];
3098 for (auto j : seq(inputChunk.nameData.size())) {
3099 NameData &nd = inputChunk.nameData[j];
3100 // Deduplicate the NameEntry records (based on the string/name),
3101 // appending all IndexEntries from duplicate NameEntry records to
3102 // the single preserved copy.
3103 for (NameEntry &ne : nd.nameEntries) {
3104 auto shardId = ne.hashValue >> shift;
3105 if ((shardId & (concurrency - 1)) != threadId)
3106 continue;
3108 ne.chunkIdx = i;
3109 for (IndexEntry &ie : ne.entries()) {
3110 // Update the IndexEntry's abbrev code to match the merged
3111 // abbreviations.
3112 ie.abbrevCode = nd.abbrevCodeMap[ie.abbrevCode];
3113 // Update the DW_IDX_compile_unit attribute (the last one after
3114 // canonicalization) to have correct merged offset value and size.
3115 auto &back = ie.attrValues.back();
3116 back.attrValue += inputChunk.baseCuIdx + j;
3117 back.attrSize = cuAttrSize;
3120 auto &nameVec = nameVecs[shardId];
3121 auto [it, inserted] = maps[shardId].try_emplace(
3122 CachedHashStringRef(ne.name, ne.hashValue), nameVec.size());
3123 if (inserted)
3124 nameVec.push_back(std::move(ne));
3125 else
3126 nameVec[it->second].indexEntries.append(std::move(ne.indexEntries));
3132 // Compute entry offsets in parallel. First, compute offsets relative to the
3133 // current shard.
3134 uint32_t offsets[numShards];
3135 parallelFor(0, numShards, [&](size_t shard) {
3136 uint32_t offset = 0;
3137 for (NameEntry &ne : nameVecs[shard]) {
3138 ne.entryOffset = offset;
3139 for (IndexEntry &ie : ne.entries()) {
3140 ie.poolOffset = offset;
3141 offset += getULEB128Size(ie.abbrevCode);
3142 for (AttrValue value : ie.attrValues)
3143 offset += value.attrSize;
3145 ++offset; // index entry sentinel
3147 offsets[shard] = offset;
3149 // Then add shard offsets.
3150 std::partial_sum(offsets, std::end(offsets), offsets);
3151 parallelFor(1, numShards, [&](size_t shard) {
3152 uint32_t offset = offsets[shard - 1];
3153 for (NameEntry &ne : nameVecs[shard]) {
3154 ne.entryOffset += offset;
3155 for (IndexEntry &ie : ne.entries())
3156 ie.poolOffset += offset;
3160 // Update the DW_IDX_parent entries that refer to real parents (have
3161 // DW_FORM_ref4).
3162 parallelFor(0, numShards, [&](size_t shard) {
3163 for (NameEntry &ne : nameVecs[shard]) {
3164 for (IndexEntry &ie : ne.entries()) {
3165 if (!ie.parentEntry)
3166 continue;
3167 // Abbrevs are indexed starting at 1; vector starts at 0. (abbrevCode
3168 // corresponds to position in the merged table vector).
3169 const Abbrev *abbrev = abbrevTable[ie.abbrevCode - 1];
3170 for (const auto &[a, v] : zip_equal(abbrev->attributes, ie.attrValues))
3171 if (a.Index == DW_IDX_parent && a.Form == DW_FORM_ref4)
3172 v.attrValue = ie.parentEntry->poolOffset;
3177 // Return (entry pool size, number of entries).
3178 uint32_t num = 0;
3179 for (auto &map : maps)
3180 num += map.size();
3181 return {offsets[numShards - 1], num};
3184 void DebugNamesBaseSection::init(
3185 function_ref<void(InputFile *, InputChunk &, OutputChunk &)> parseFile) {
3186 TimeTraceScope timeScope("Merge .debug_names");
3187 // Collect and remove input .debug_names sections. Save InputSection pointers
3188 // to relocate string offsets in `writeTo`.
3189 SetVector<InputFile *> files;
3190 for (InputSectionBase *s : ctx.inputSections) {
3191 InputSection *isec = dyn_cast<InputSection>(s);
3192 if (!isec)
3193 continue;
3194 if (!(s->flags & SHF_ALLOC) && s->name == ".debug_names") {
3195 s->markDead();
3196 inputSections.push_back(isec);
3197 files.insert(isec->file);
3201 // Parse input .debug_names sections and extract InputChunk and OutputChunk
3202 // data. OutputChunk contains CU information, which will be needed by
3203 // `writeTo`.
3204 auto inputChunksPtr = std::make_unique<InputChunk[]>(files.size());
3205 MutableArrayRef<InputChunk> inputChunks(inputChunksPtr.get(), files.size());
3206 numChunks = files.size();
3207 chunks = std::make_unique<OutputChunk[]>(files.size());
3209 TimeTraceScope timeScope("Merge .debug_names", "parse");
3210 parallelFor(0, files.size(), [&](size_t i) {
3211 parseFile(files[i], inputChunks[i], chunks[i]);
3215 // Compute section header (except unit_length), abbrev table, and entry pool.
3216 computeHdrAndAbbrevTable(inputChunks);
3217 uint32_t entryPoolSize;
3218 std::tie(entryPoolSize, hdr.NameCount) = computeEntryPool(inputChunks);
3219 hdr.BucketCount = dwarf::getDebugNamesBucketCount(hdr.NameCount);
3221 // Compute the section size. Subtract 4 to get the unit_length for DWARF32.
3222 uint32_t hdrSize = getDebugNamesHeaderSize(hdr.AugmentationStringSize);
3223 size = findDebugNamesOffsets(hdrSize, hdr).EntriesBase + entryPoolSize;
3224 hdr.UnitLength = size - 4;
3227 template <class ELFT>
3228 DebugNamesSection<ELFT>::DebugNamesSection(Ctx &ctx)
3229 : DebugNamesBaseSection(ctx) {
3230 init([&](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {
3231 auto *file = cast<ObjFile<ELFT>>(f);
3232 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3233 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3234 chunk.infoSec = dobj.getInfoSection();
3235 DWARFDataExtractor namesExtractor(dobj, dobj.getNamesSection(),
3236 ELFT::Endianness == endianness::little,
3237 ELFT::Is64Bits ? 8 : 4);
3238 // .debug_str is needed to get symbol names from string offsets.
3239 DataExtractor strExtractor(dobj.getStrSection(),
3240 ELFT::Endianness == endianness::little,
3241 ELFT::Is64Bits ? 8 : 4);
3242 inputChunk.section = dobj.getNamesSection();
3244 inputChunk.llvmDebugNames.emplace(namesExtractor, strExtractor);
3245 if (Error e = inputChunk.llvmDebugNames->extract()) {
3246 Err(ctx) << dobj.getNamesSection().sec << ": " << std::move(e);
3248 parseDebugNames(
3249 ctx, inputChunk, chunk, namesExtractor, strExtractor,
3250 [&chunk, namesData = dobj.getNamesSection().Data.data()](
3251 uint32_t numCus, const DWARFDebugNames::Header &hdr,
3252 const DWARFDebugNames::DWARFDebugNamesOffsets &locs) {
3253 // Read CU offsets, which are relocated by .debug_info + X
3254 // relocations. Record the section offset to be relocated by
3255 // `finalizeContents`.
3256 chunk.compUnits.resize_for_overwrite(numCus + hdr.CompUnitCount);
3257 for (auto i : seq(hdr.CompUnitCount))
3258 chunk.compUnits[numCus + i] = locs.CUsBase + i * 4;
3260 // Read entry offsets.
3261 const char *p = namesData + locs.EntryOffsetsBase;
3262 SmallVector<uint32_t, 0> entryOffsets;
3263 entryOffsets.resize_for_overwrite(hdr.NameCount);
3264 for (uint32_t &offset : entryOffsets)
3265 offset = endian::readNext<uint32_t, ELFT::Endianness, unaligned>(p);
3266 return entryOffsets;
3271 template <class ELFT>
3272 template <class RelTy>
3273 void DebugNamesSection<ELFT>::getNameRelocs(
3274 const InputFile &file, DenseMap<uint32_t, uint32_t> &relocs,
3275 Relocs<RelTy> rels) {
3276 for (const RelTy &rel : rels) {
3277 Symbol &sym = file.getRelocTargetSym(rel);
3278 relocs[rel.r_offset] = sym.getVA(ctx, getAddend<ELFT>(rel));
3282 template <class ELFT> void DebugNamesSection<ELFT>::finalizeContents() {
3283 // Get relocations of .debug_names sections.
3284 auto relocs = std::make_unique<DenseMap<uint32_t, uint32_t>[]>(numChunks);
3285 parallelFor(0, numChunks, [&](size_t i) {
3286 InputSection *sec = inputSections[i];
3287 invokeOnRelocs(*sec, getNameRelocs, *sec->file, relocs.get()[i]);
3289 // Relocate CU offsets with .debug_info + X relocations.
3290 OutputChunk &chunk = chunks.get()[i];
3291 for (auto [j, cuOffset] : enumerate(chunk.compUnits))
3292 cuOffset = relocs.get()[i].lookup(cuOffset);
3295 // Relocate string offsets in the name table with .debug_str + X relocations.
3296 parallelForEach(nameVecs, [&](auto &nameVec) {
3297 for (NameEntry &ne : nameVec)
3298 ne.stringOffset = relocs.get()[ne.chunkIdx].lookup(ne.stringOffset);
3302 template <class ELFT> void DebugNamesSection<ELFT>::writeTo(uint8_t *buf) {
3303 [[maybe_unused]] const uint8_t *const beginBuf = buf;
3304 // Write the header.
3305 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.UnitLength);
3306 endian::writeNext<uint16_t, ELFT::Endianness>(buf, hdr.Version);
3307 buf += 2; // padding
3308 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.CompUnitCount);
3309 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.LocalTypeUnitCount);
3310 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.ForeignTypeUnitCount);
3311 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.BucketCount);
3312 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.NameCount);
3313 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.AbbrevTableSize);
3314 endian::writeNext<uint32_t, ELFT::Endianness>(buf,
3315 hdr.AugmentationStringSize);
3316 memcpy(buf, hdr.AugmentationString.c_str(), hdr.AugmentationString.size());
3317 buf += hdr.AugmentationStringSize;
3319 // Write the CU list.
3320 for (auto &chunk : getChunks())
3321 for (uint32_t cuOffset : chunk.compUnits)
3322 endian::writeNext<uint32_t, ELFT::Endianness>(buf, cuOffset);
3324 // TODO: Write the local TU list, then the foreign TU list..
3326 // Write the hash lookup table.
3327 SmallVector<SmallVector<NameEntry *, 0>, 0> buckets(hdr.BucketCount);
3328 // Symbols enter into a bucket whose index is the hash modulo bucket_count.
3329 for (auto &nameVec : nameVecs)
3330 for (NameEntry &ne : nameVec)
3331 buckets[ne.hashValue % hdr.BucketCount].push_back(&ne);
3333 // Write buckets (accumulated bucket counts).
3334 uint32_t bucketIdx = 1;
3335 for (const SmallVector<NameEntry *, 0> &bucket : buckets) {
3336 if (!bucket.empty())
3337 endian::write32<ELFT::Endianness>(buf, bucketIdx);
3338 buf += 4;
3339 bucketIdx += bucket.size();
3341 // Write the hashes.
3342 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3343 for (const NameEntry *e : bucket)
3344 endian::writeNext<uint32_t, ELFT::Endianness>(buf, e->hashValue);
3346 // Write the name table. The name entries are ordered by bucket_idx and
3347 // correspond one-to-one with the hash lookup table.
3349 // First, write the relocated string offsets.
3350 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3351 for (const NameEntry *ne : bucket)
3352 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->stringOffset);
3354 // Then write the entry offsets.
3355 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3356 for (const NameEntry *ne : bucket)
3357 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->entryOffset);
3359 // Write the abbrev table.
3360 buf = llvm::copy(abbrevTableBuf, buf);
3362 // Write the entry pool. Unlike the name table, the name entries follow the
3363 // nameVecs order computed by `computeEntryPool`.
3364 for (auto &nameVec : nameVecs) {
3365 for (NameEntry &ne : nameVec) {
3366 // Write all the entries for the string.
3367 for (const IndexEntry &ie : ne.entries()) {
3368 buf += encodeULEB128(ie.abbrevCode, buf);
3369 for (AttrValue value : ie.attrValues) {
3370 switch (value.attrSize) {
3371 case 1:
3372 *buf++ = value.attrValue;
3373 break;
3374 case 2:
3375 endian::writeNext<uint16_t, ELFT::Endianness>(buf, value.attrValue);
3376 break;
3377 case 4:
3378 endian::writeNext<uint32_t, ELFT::Endianness>(buf, value.attrValue);
3379 break;
3380 default:
3381 llvm_unreachable("invalid attrSize");
3385 ++buf; // index entry sentinel
3388 assert(uint64_t(buf - beginBuf) == size);
3391 GdbIndexSection::GdbIndexSection(Ctx &ctx)
3392 : SyntheticSection(ctx, ".gdb_index", SHT_PROGBITS, 0, 1) {}
3394 // Returns the desired size of an on-disk hash table for a .gdb_index section.
3395 // There's a tradeoff between size and collision rate. We aim 75% utilization.
3396 size_t GdbIndexSection::computeSymtabSize() const {
3397 return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024);
3400 static SmallVector<GdbIndexSection::CuEntry, 0>
3401 readCuList(DWARFContext &dwarf) {
3402 SmallVector<GdbIndexSection::CuEntry, 0> ret;
3403 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())
3404 ret.push_back({cu->getOffset(), cu->getLength() + 4});
3405 return ret;
3408 static SmallVector<GdbIndexSection::AddressEntry, 0>
3409 readAddressAreas(Ctx &ctx, DWARFContext &dwarf, InputSection *sec) {
3410 SmallVector<GdbIndexSection::AddressEntry, 0> ret;
3412 uint32_t cuIdx = 0;
3413 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {
3414 if (Error e = cu->tryExtractDIEsIfNeeded(false)) {
3415 Warn(ctx) << sec << ": " << std::move(e);
3416 return {};
3418 Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges();
3419 if (!ranges) {
3420 Warn(ctx) << sec << ": " << ranges.takeError();
3421 return {};
3424 ArrayRef<InputSectionBase *> sections = sec->file->getSections();
3425 for (DWARFAddressRange &r : *ranges) {
3426 if (r.SectionIndex == -1ULL)
3427 continue;
3428 // Range list with zero size has no effect.
3429 InputSectionBase *s = sections[r.SectionIndex];
3430 if (s && s != &InputSection::discarded && s->isLive())
3431 if (r.LowPC != r.HighPC)
3432 ret.push_back({cast<InputSection>(s), r.LowPC, r.HighPC, cuIdx});
3434 ++cuIdx;
3437 return ret;
3440 template <class ELFT>
3441 static SmallVector<GdbIndexSection::NameAttrEntry, 0>
3442 readPubNamesAndTypes(Ctx &ctx, const LLDDwarfObj<ELFT> &obj,
3443 const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) {
3444 const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection();
3445 const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection();
3447 SmallVector<GdbIndexSection::NameAttrEntry, 0> ret;
3448 for (const LLDDWARFSection *pub : {&pubNames, &pubTypes}) {
3449 DWARFDataExtractor data(obj, *pub, ELFT::Endianness == endianness::little,
3450 ELFT::Is64Bits ? 8 : 4);
3451 DWARFDebugPubTable table;
3452 table.extract(data, /*GnuStyle=*/true, [&](Error e) {
3453 Warn(ctx) << pub->sec << ": " << std::move(e);
3455 for (const DWARFDebugPubTable::Set &set : table.getData()) {
3456 // The value written into the constant pool is kind << 24 | cuIndex. As we
3457 // don't know how many compilation units precede this object to compute
3458 // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add
3459 // the number of preceding compilation units later.
3460 uint32_t i = llvm::partition_point(cus,
3461 [&](GdbIndexSection::CuEntry cu) {
3462 return cu.cuOffset < set.Offset;
3463 }) -
3464 cus.begin();
3465 for (const DWARFDebugPubTable::Entry &ent : set.Entries)
3466 ret.push_back({{ent.Name, computeGdbHash(ent.Name)},
3467 (ent.Descriptor.toBits() << 24) | i});
3470 return ret;
3473 // Create a list of symbols from a given list of symbol names and types
3474 // by uniquifying them by name.
3475 static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t>
3476 createSymbols(
3477 Ctx &ctx,
3478 ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs,
3479 const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) {
3480 using GdbSymbol = GdbIndexSection::GdbSymbol;
3481 using NameAttrEntry = GdbIndexSection::NameAttrEntry;
3483 // For each chunk, compute the number of compilation units preceding it.
3484 uint32_t cuIdx = 0;
3485 std::unique_ptr<uint32_t[]> cuIdxs(new uint32_t[chunks.size()]);
3486 for (uint32_t i = 0, e = chunks.size(); i != e; ++i) {
3487 cuIdxs[i] = cuIdx;
3488 cuIdx += chunks[i].compilationUnits.size();
3491 // Collect the compilation unitss for each unique name. Speed it up using
3492 // multi-threading as the number of symbols can be in the order of millions.
3493 // Shard GdbSymbols by hash's high bits.
3494 constexpr size_t numShards = 32;
3495 const size_t concurrency =
3496 llvm::bit_floor(std::min<size_t>(ctx.arg.threadCount, numShards));
3497 const size_t shift = 32 - llvm::countr_zero(numShards);
3498 auto map =
3499 std::make_unique<DenseMap<CachedHashStringRef, size_t>[]>(numShards);
3500 auto symbols = std::make_unique<SmallVector<GdbSymbol, 0>[]>(numShards);
3501 parallelFor(0, concurrency, [&](size_t threadId) {
3502 uint32_t i = 0;
3503 for (ArrayRef<NameAttrEntry> entries : nameAttrs) {
3504 for (const NameAttrEntry &ent : entries) {
3505 size_t shardId = ent.name.hash() >> shift;
3506 if ((shardId & (concurrency - 1)) != threadId)
3507 continue;
3509 uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i];
3510 auto [it, inserted] =
3511 map[shardId].try_emplace(ent.name, symbols[shardId].size());
3512 if (inserted)
3513 symbols[shardId].push_back({ent.name, {v}, 0, 0});
3514 else
3515 symbols[shardId][it->second].cuVector.push_back(v);
3517 ++i;
3521 size_t numSymbols = 0;
3522 for (ArrayRef<GdbSymbol> v : ArrayRef(symbols.get(), numShards))
3523 numSymbols += v.size();
3525 // The return type is a flattened vector, so we'll copy each vector
3526 // contents to Ret.
3527 SmallVector<GdbSymbol, 0> ret;
3528 ret.reserve(numSymbols);
3529 for (SmallVector<GdbSymbol, 0> &vec :
3530 MutableArrayRef(symbols.get(), numShards))
3531 for (GdbSymbol &sym : vec)
3532 ret.push_back(std::move(sym));
3534 // CU vectors and symbol names are adjacent in the output file.
3535 // We can compute their offsets in the output file now.
3536 size_t off = 0;
3537 for (GdbSymbol &sym : ret) {
3538 sym.cuVectorOff = off;
3539 off += (sym.cuVector.size() + 1) * 4;
3541 for (GdbSymbol &sym : ret) {
3542 sym.nameOff = off;
3543 off += sym.name.size() + 1;
3545 // If off overflows, the last symbol's nameOff likely overflows.
3546 if (!isUInt<32>(off))
3547 Err(ctx) << "--gdb-index: constant pool size (" << off
3548 << ") exceeds UINT32_MAX";
3550 return {ret, off};
3553 // Returns a newly-created .gdb_index section.
3554 template <class ELFT>
3555 std::unique_ptr<GdbIndexSection> GdbIndexSection::create(Ctx &ctx) {
3556 llvm::TimeTraceScope timeScope("Create gdb index");
3558 // Collect InputFiles with .debug_info. See the comment in
3559 // LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future,
3560 // note that isec->data() may uncompress the full content, which should be
3561 // parallelized.
3562 SetVector<InputFile *> files;
3563 for (InputSectionBase *s : ctx.inputSections) {
3564 InputSection *isec = dyn_cast<InputSection>(s);
3565 if (!isec)
3566 continue;
3567 // .debug_gnu_pub{names,types} are useless in executables.
3568 // They are present in input object files solely for creating
3569 // a .gdb_index. So we can remove them from the output.
3570 if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")
3571 s->markDead();
3572 else if (isec->name == ".debug_info")
3573 files.insert(isec->file);
3575 // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs.
3576 llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
3577 if (auto *isec = dyn_cast<InputSection>(s))
3578 if (InputSectionBase *rel = isec->getRelocatedSection())
3579 return !rel->isLive();
3580 return !s->isLive();
3583 SmallVector<GdbChunk, 0> chunks(files.size());
3584 SmallVector<SmallVector<NameAttrEntry, 0>, 0> nameAttrs(files.size());
3586 parallelFor(0, files.size(), [&](size_t i) {
3587 // To keep memory usage low, we don't want to keep cached DWARFContext, so
3588 // avoid getDwarf() here.
3589 ObjFile<ELFT> *file = cast<ObjFile<ELFT>>(files[i]);
3590 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3591 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3593 // If the are multiple compile units .debug_info (very rare ld -r --unique),
3594 // this only picks the last one. Other address ranges are lost.
3595 chunks[i].sec = dobj.getInfoSection();
3596 chunks[i].compilationUnits = readCuList(dwarf);
3597 chunks[i].addressAreas = readAddressAreas(ctx, dwarf, chunks[i].sec);
3598 nameAttrs[i] =
3599 readPubNamesAndTypes<ELFT>(ctx, dobj, chunks[i].compilationUnits);
3602 auto ret = std::make_unique<GdbIndexSection>(ctx);
3603 ret->chunks = std::move(chunks);
3604 std::tie(ret->symbols, ret->size) =
3605 createSymbols(ctx, nameAttrs, ret->chunks);
3607 // Count the areas other than the constant pool.
3608 ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8;
3609 for (GdbChunk &chunk : ret->chunks)
3610 ret->size +=
3611 chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20;
3613 return ret;
3616 void GdbIndexSection::writeTo(uint8_t *buf) {
3617 // Write the header.
3618 auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf);
3619 uint8_t *start = buf;
3620 hdr->version = 7;
3621 buf += sizeof(*hdr);
3623 // Write the CU list.
3624 hdr->cuListOff = buf - start;
3625 for (GdbChunk &chunk : chunks) {
3626 for (CuEntry &cu : chunk.compilationUnits) {
3627 write64le(buf, chunk.sec->outSecOff + cu.cuOffset);
3628 write64le(buf + 8, cu.cuLength);
3629 buf += 16;
3633 // Write the address area.
3634 hdr->cuTypesOff = buf - start;
3635 hdr->addressAreaOff = buf - start;
3636 uint32_t cuOff = 0;
3637 for (GdbChunk &chunk : chunks) {
3638 for (AddressEntry &e : chunk.addressAreas) {
3639 // In the case of ICF there may be duplicate address range entries.
3640 const uint64_t baseAddr = e.section->repl->getVA(0);
3641 write64le(buf, baseAddr + e.lowAddress);
3642 write64le(buf + 8, baseAddr + e.highAddress);
3643 write32le(buf + 16, e.cuIndex + cuOff);
3644 buf += 20;
3646 cuOff += chunk.compilationUnits.size();
3649 // Write the on-disk open-addressing hash table containing symbols.
3650 hdr->symtabOff = buf - start;
3651 size_t symtabSize = computeSymtabSize();
3652 uint32_t mask = symtabSize - 1;
3654 for (GdbSymbol &sym : symbols) {
3655 uint32_t h = sym.name.hash();
3656 uint32_t i = h & mask;
3657 uint32_t step = ((h * 17) & mask) | 1;
3659 while (read32le(buf + i * 8))
3660 i = (i + step) & mask;
3662 write32le(buf + i * 8, sym.nameOff);
3663 write32le(buf + i * 8 + 4, sym.cuVectorOff);
3666 buf += symtabSize * 8;
3668 // Write the string pool.
3669 hdr->constantPoolOff = buf - start;
3670 parallelForEach(symbols, [&](GdbSymbol &sym) {
3671 memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size());
3674 // Write the CU vectors.
3675 for (GdbSymbol &sym : symbols) {
3676 write32le(buf, sym.cuVector.size());
3677 buf += 4;
3678 for (uint32_t val : sym.cuVector) {
3679 write32le(buf, val);
3680 buf += 4;
3685 bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }
3687 EhFrameHeader::EhFrameHeader(Ctx &ctx)
3688 : SyntheticSection(ctx, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, 4) {}
3690 void EhFrameHeader::writeTo(uint8_t *buf) {
3691 // Unlike most sections, the EhFrameHeader section is written while writing
3692 // another section, namely EhFrameSection, which calls the write() function
3693 // below from its writeTo() function. This is necessary because the contents
3694 // of EhFrameHeader depend on the relocated contents of EhFrameSection and we
3695 // don't know which order the sections will be written in.
3698 // .eh_frame_hdr contains a binary search table of pointers to FDEs.
3699 // Each entry of the search table consists of two values,
3700 // the starting PC from where FDEs covers, and the FDE's address.
3701 // It is sorted by PC.
3702 void EhFrameHeader::write() {
3703 uint8_t *buf = ctx.bufferStart + getParent()->offset + outSecOff;
3704 using FdeData = EhFrameSection::FdeData;
3705 SmallVector<FdeData, 0> fdes = getPartition(ctx).ehFrame->getFdeData();
3707 buf[0] = 1;
3708 buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
3709 buf[2] = DW_EH_PE_udata4;
3710 buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
3711 write32(ctx, buf + 4,
3712 getPartition(ctx).ehFrame->getParent()->addr - this->getVA() - 4);
3713 write32(ctx, buf + 8, fdes.size());
3714 buf += 12;
3716 for (FdeData &fde : fdes) {
3717 write32(ctx, buf, fde.pcRel);
3718 write32(ctx, buf + 4, fde.fdeVARel);
3719 buf += 8;
3723 size_t EhFrameHeader::getSize() const {
3724 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
3725 return 12 + getPartition(ctx).ehFrame->numFdes * 8;
3728 bool EhFrameHeader::isNeeded() const {
3729 return isLive() && getPartition(ctx).ehFrame->isNeeded();
3732 VersionDefinitionSection::VersionDefinitionSection(Ctx &ctx)
3733 : SyntheticSection(ctx, ".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC,
3734 sizeof(uint32_t)) {}
3736 StringRef VersionDefinitionSection::getFileDefName() {
3737 if (!getPartition(ctx).name.empty())
3738 return getPartition(ctx).name;
3739 if (!ctx.arg.soName.empty())
3740 return ctx.arg.soName;
3741 return ctx.arg.outputFile;
3744 void VersionDefinitionSection::finalizeContents() {
3745 fileDefNameOff = getPartition(ctx).dynStrTab->addString(getFileDefName());
3746 for (const VersionDefinition &v : namedVersionDefs(ctx))
3747 verDefNameOffs.push_back(getPartition(ctx).dynStrTab->addString(v.name));
3749 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3750 getParent()->link = sec->sectionIndex;
3752 // sh_info should be set to the number of definitions. This fact is missed in
3753 // documentation, but confirmed by binutils community:
3754 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
3755 getParent()->info = getVerDefNum(ctx);
3758 void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index,
3759 StringRef name, size_t nameOff) {
3760 uint16_t flags = index == 1 ? VER_FLG_BASE : 0;
3762 // Write a verdef.
3763 write16(ctx, buf, 1); // vd_version
3764 write16(ctx, buf + 2, flags); // vd_flags
3765 write16(ctx, buf + 4, index); // vd_ndx
3766 write16(ctx, buf + 6, 1); // vd_cnt
3767 write32(ctx, buf + 8, hashSysV(name)); // vd_hash
3768 write32(ctx, buf + 12, 20); // vd_aux
3769 write32(ctx, buf + 16, 28); // vd_next
3771 // Write a veraux.
3772 write32(ctx, buf + 20, nameOff); // vda_name
3773 write32(ctx, buf + 24, 0); // vda_next
3776 void VersionDefinitionSection::writeTo(uint8_t *buf) {
3777 writeOne(buf, 1, getFileDefName(), fileDefNameOff);
3779 auto nameOffIt = verDefNameOffs.begin();
3780 for (const VersionDefinition &v : namedVersionDefs(ctx)) {
3781 buf += EntrySize;
3782 writeOne(buf, v.id, v.name, *nameOffIt++);
3785 // Need to terminate the last version definition.
3786 write32(ctx, buf + 16, 0); // vd_next
3789 size_t VersionDefinitionSection::getSize() const {
3790 return EntrySize * getVerDefNum(ctx);
3793 // .gnu.version is a table where each entry is 2 byte long.
3794 VersionTableSection::VersionTableSection(Ctx &ctx)
3795 : SyntheticSection(ctx, ".gnu.version", SHT_GNU_versym, SHF_ALLOC,
3796 sizeof(uint16_t)) {
3797 this->entsize = 2;
3800 void VersionTableSection::finalizeContents() {
3801 // At the moment of june 2016 GNU docs does not mention that sh_link field
3802 // should be set, but Sun docs do. Also readelf relies on this field.
3803 getParent()->link = getPartition(ctx).dynSymTab->getParent()->sectionIndex;
3806 size_t VersionTableSection::getSize() const {
3807 return (getPartition(ctx).dynSymTab->getSymbols().size() + 1) * 2;
3810 void VersionTableSection::writeTo(uint8_t *buf) {
3811 buf += 2;
3812 for (const SymbolTableEntry &s : getPartition(ctx).dynSymTab->getSymbols()) {
3813 // For an unextracted lazy symbol (undefined weak), it must have been
3814 // converted to Undefined and have VER_NDX_GLOBAL version here.
3815 assert(!s.sym->isLazy());
3816 write16(ctx, buf, s.sym->versionId);
3817 buf += 2;
3821 bool VersionTableSection::isNeeded() const {
3822 return isLive() &&
3823 (getPartition(ctx).verDef || getPartition(ctx).verNeed->isNeeded());
3826 void elf::addVerneed(Ctx &ctx, Symbol &ss) {
3827 auto &file = cast<SharedFile>(*ss.file);
3828 if (ss.versionId == VER_NDX_GLOBAL)
3829 return;
3831 if (file.vernauxs.empty())
3832 file.vernauxs.resize(file.verdefs.size());
3834 // Select a version identifier for the vernaux data structure, if we haven't
3835 // already allocated one. The verdef identifiers cover the range
3836 // [1..getVerDefNum(ctx)]; this causes the vernaux identifiers to start from
3837 // getVerDefNum(ctx)+1.
3838 if (file.vernauxs[ss.versionId] == 0)
3839 file.vernauxs[ss.versionId] = ++ctx.vernauxNum + getVerDefNum(ctx);
3841 ss.versionId = file.vernauxs[ss.versionId];
3844 template <class ELFT>
3845 VersionNeedSection<ELFT>::VersionNeedSection(Ctx &ctx)
3846 : SyntheticSection(ctx, ".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC,
3847 sizeof(uint32_t)) {}
3849 template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
3850 for (SharedFile *f : ctx.sharedFiles) {
3851 if (f->vernauxs.empty())
3852 continue;
3853 verneeds.emplace_back();
3854 Verneed &vn = verneeds.back();
3855 vn.nameStrTab = getPartition(ctx).dynStrTab->addString(f->soName);
3856 bool isLibc = ctx.arg.relrGlibc && f->soName.starts_with("libc.so.");
3857 bool isGlibc2 = false;
3858 for (unsigned i = 0; i != f->vernauxs.size(); ++i) {
3859 if (f->vernauxs[i] == 0)
3860 continue;
3861 auto *verdef =
3862 reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]);
3863 StringRef ver(f->getStringTable().data() + verdef->getAux()->vda_name);
3864 if (isLibc && ver.starts_with("GLIBC_2."))
3865 isGlibc2 = true;
3866 vn.vernauxs.push_back({verdef->vd_hash, f->vernauxs[i],
3867 getPartition(ctx).dynStrTab->addString(ver)});
3869 if (isGlibc2) {
3870 const char *ver = "GLIBC_ABI_DT_RELR";
3871 vn.vernauxs.push_back({hashSysV(ver),
3872 ++ctx.vernauxNum + getVerDefNum(ctx),
3873 getPartition(ctx).dynStrTab->addString(ver)});
3877 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3878 getParent()->link = sec->sectionIndex;
3879 getParent()->info = verneeds.size();
3882 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) {
3883 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
3884 auto *verneed = reinterpret_cast<Elf_Verneed *>(buf);
3885 auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size());
3887 for (auto &vn : verneeds) {
3888 // Create an Elf_Verneed for this DSO.
3889 verneed->vn_version = 1;
3890 verneed->vn_cnt = vn.vernauxs.size();
3891 verneed->vn_file = vn.nameStrTab;
3892 verneed->vn_aux =
3893 reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed);
3894 verneed->vn_next = sizeof(Elf_Verneed);
3895 ++verneed;
3897 // Create the Elf_Vernauxs for this Elf_Verneed.
3898 for (auto &vna : vn.vernauxs) {
3899 vernaux->vna_hash = vna.hash;
3900 vernaux->vna_flags = 0;
3901 vernaux->vna_other = vna.verneedIndex;
3902 vernaux->vna_name = vna.nameStrTab;
3903 vernaux->vna_next = sizeof(Elf_Vernaux);
3904 ++vernaux;
3907 vernaux[-1].vna_next = 0;
3909 verneed[-1].vn_next = 0;
3912 template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
3913 return verneeds.size() * sizeof(Elf_Verneed) +
3914 ctx.vernauxNum * sizeof(Elf_Vernaux);
3917 template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
3918 return isLive() && ctx.vernauxNum != 0;
3921 void MergeSyntheticSection::addSection(MergeInputSection *ms) {
3922 ms->parent = this;
3923 sections.push_back(ms);
3924 assert(addralign == ms->addralign || !(ms->flags & SHF_STRINGS));
3925 addralign = std::max(addralign, ms->addralign);
3928 MergeTailSection::MergeTailSection(Ctx &ctx, StringRef name, uint32_t type,
3929 uint64_t flags, uint32_t alignment)
3930 : MergeSyntheticSection(ctx, name, type, flags, alignment),
3931 builder(StringTableBuilder::RAW, llvm::Align(alignment)) {}
3933 size_t MergeTailSection::getSize() const { return builder.getSize(); }
3935 void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); }
3937 void MergeTailSection::finalizeContents() {
3938 // Add all string pieces to the string table builder to create section
3939 // contents.
3940 for (MergeInputSection *sec : sections)
3941 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3942 if (sec->pieces[i].live)
3943 builder.add(sec->getData(i));
3945 // Fix the string table content. After this, the contents will never change.
3946 builder.finalize();
3948 // finalize() fixed tail-optimized strings, so we can now get
3949 // offsets of strings. Get an offset for each string and save it
3950 // to a corresponding SectionPiece for easy access.
3951 for (MergeInputSection *sec : sections)
3952 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3953 if (sec->pieces[i].live)
3954 sec->pieces[i].outputOff = builder.getOffset(sec->getData(i));
3957 void MergeNoTailSection::writeTo(uint8_t *buf) {
3958 parallelFor(0, numShards,
3959 [&](size_t i) { shards[i].write(buf + shardOffsets[i]); });
3962 // This function is very hot (i.e. it can take several seconds to finish)
3963 // because sometimes the number of inputs is in an order of magnitude of
3964 // millions. So, we use multi-threading.
3966 // For any strings S and T, we know S is not mergeable with T if S's hash
3967 // value is different from T's. If that's the case, we can safely put S and
3968 // T into different string builders without worrying about merge misses.
3969 // We do it in parallel.
3970 void MergeNoTailSection::finalizeContents() {
3971 // Initializes string table builders.
3972 for (size_t i = 0; i < numShards; ++i)
3973 shards.emplace_back(StringTableBuilder::RAW, llvm::Align(addralign));
3975 // Concurrency level. Must be a power of 2 to avoid expensive modulo
3976 // operations in the following tight loop.
3977 const size_t concurrency =
3978 llvm::bit_floor(std::min<size_t>(ctx.arg.threadCount, numShards));
3980 // Add section pieces to the builders.
3981 parallelFor(0, concurrency, [&](size_t threadId) {
3982 for (MergeInputSection *sec : sections) {
3983 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) {
3984 if (!sec->pieces[i].live)
3985 continue;
3986 size_t shardId = getShardId(sec->pieces[i].hash);
3987 if ((shardId & (concurrency - 1)) == threadId)
3988 sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i));
3993 // Compute an in-section offset for each shard.
3994 size_t off = 0;
3995 for (size_t i = 0; i < numShards; ++i) {
3996 shards[i].finalizeInOrder();
3997 if (shards[i].getSize() > 0)
3998 off = alignToPowerOf2(off, addralign);
3999 shardOffsets[i] = off;
4000 off += shards[i].getSize();
4002 size = off;
4004 // So far, section pieces have offsets from beginning of shards, but
4005 // we want offsets from beginning of the whole section. Fix them.
4006 parallelForEach(sections, [&](MergeInputSection *sec) {
4007 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
4008 if (sec->pieces[i].live)
4009 sec->pieces[i].outputOff +=
4010 shardOffsets[getShardId(sec->pieces[i].hash)];
4014 template <class ELFT> void elf::splitSections(Ctx &ctx) {
4015 llvm::TimeTraceScope timeScope("Split sections");
4016 // splitIntoPieces needs to be called on each MergeInputSection
4017 // before calling finalizeContents().
4018 parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
4019 for (InputSectionBase *sec : file->getSections()) {
4020 if (!sec)
4021 continue;
4022 if (auto *s = dyn_cast<MergeInputSection>(sec))
4023 s->splitIntoPieces();
4024 else if (auto *eh = dyn_cast<EhInputSection>(sec))
4025 eh->split<ELFT>();
4030 void elf::combineEhSections(Ctx &ctx) {
4031 llvm::TimeTraceScope timeScope("Combine EH sections");
4032 for (EhInputSection *sec : ctx.ehInputSections) {
4033 EhFrameSection &eh = *sec->getPartition(ctx).ehFrame;
4034 sec->parent = &eh;
4035 eh.addralign = std::max(eh.addralign, sec->addralign);
4036 eh.sections.push_back(sec);
4037 llvm::append_range(eh.dependentSections, sec->dependentSections);
4040 if (!ctx.mainPart->armExidx)
4041 return;
4042 llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) {
4043 // Ignore dead sections and the partition end marker (.part.end),
4044 // whose partition number is out of bounds.
4045 if (!s->isLive() || s->partition == 255)
4046 return false;
4047 Partition &part = s->getPartition(ctx);
4048 return s->kind() == SectionBase::Regular && part.armExidx &&
4049 part.armExidx->addSection(cast<InputSection>(s));
4053 MipsRldMapSection::MipsRldMapSection(Ctx &ctx)
4054 : SyntheticSection(ctx, ".rld_map", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
4055 ctx.arg.wordsize) {}
4057 ARMExidxSyntheticSection::ARMExidxSyntheticSection(Ctx &ctx)
4058 : SyntheticSection(ctx, ".ARM.exidx", SHT_ARM_EXIDX,
4059 SHF_ALLOC | SHF_LINK_ORDER, ctx.arg.wordsize) {}
4061 static InputSection *findExidxSection(InputSection *isec) {
4062 for (InputSection *d : isec->dependentSections)
4063 if (d->type == SHT_ARM_EXIDX && d->isLive())
4064 return d;
4065 return nullptr;
4068 static bool isValidExidxSectionDep(InputSection *isec) {
4069 return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) &&
4070 isec->getSize() > 0;
4073 bool ARMExidxSyntheticSection::addSection(InputSection *isec) {
4074 if (isec->type == SHT_ARM_EXIDX) {
4075 if (InputSection *dep = isec->getLinkOrderDep())
4076 if (isValidExidxSectionDep(dep)) {
4077 exidxSections.push_back(isec);
4078 // Every exidxSection is 8 bytes, we need an estimate of
4079 // size before assignAddresses can be called. Final size
4080 // will only be known after finalize is called.
4081 size += 8;
4083 return true;
4086 if (isValidExidxSectionDep(isec)) {
4087 executableSections.push_back(isec);
4088 return false;
4091 // FIXME: we do not output a relocation section when --emit-relocs is used
4092 // as we do not have relocation sections for linker generated table entries
4093 // and we would have to erase at a late stage relocations from merged entries.
4094 // Given that exception tables are already position independent and a binary
4095 // analyzer could derive the relocations we choose to erase the relocations.
4096 if (ctx.arg.emitRelocs && isec->type == SHT_REL)
4097 if (InputSectionBase *ex = isec->getRelocatedSection())
4098 if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX)
4099 return true;
4101 return false;
4104 // References to .ARM.Extab Sections have bit 31 clear and are not the
4105 // special EXIDX_CANTUNWIND bit-pattern.
4106 static bool isExtabRef(uint32_t unwind) {
4107 return (unwind & 0x80000000) == 0 && unwind != 0x1;
4110 // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx
4111 // section Prev, where Cur follows Prev in the table. This can be done if the
4112 // unwinding instructions in Cur are identical to Prev. Linker generated
4113 // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an
4114 // InputSection.
4115 static bool isDuplicateArmExidxSec(Ctx &ctx, InputSection *prev,
4116 InputSection *cur) {
4117 // Get the last table Entry from the previous .ARM.exidx section. If Prev is
4118 // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.
4119 uint32_t prevUnwind = 1;
4120 if (prev)
4121 prevUnwind =
4122 read32(ctx, prev->content().data() + prev->content().size() - 4);
4123 if (isExtabRef(prevUnwind))
4124 return false;
4126 // We consider the unwind instructions of an .ARM.exidx table entry
4127 // a duplicate if the previous unwind instructions if:
4128 // - Both are the special EXIDX_CANTUNWIND.
4129 // - Both are the same inline unwind instructions.
4130 // We do not attempt to follow and check links into .ARM.extab tables as
4131 // consecutive identical entries are rare and the effort to check that they
4132 // are identical is high.
4134 // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.
4135 if (cur == nullptr)
4136 return prevUnwind == 1;
4138 for (uint32_t offset = 4; offset < (uint32_t)cur->content().size(); offset +=8) {
4139 uint32_t curUnwind = read32(ctx, cur->content().data() + offset);
4140 if (isExtabRef(curUnwind) || curUnwind != prevUnwind)
4141 return false;
4143 // All table entries in this .ARM.exidx Section can be merged into the
4144 // previous Section.
4145 return true;
4148 // The .ARM.exidx table must be sorted in ascending order of the address of the
4149 // functions the table describes. std::optionally duplicate adjacent table
4150 // entries can be removed. At the end of the function the executableSections
4151 // must be sorted in ascending order of address, Sentinel is set to the
4152 // InputSection with the highest address and any InputSections that have
4153 // mergeable .ARM.exidx table entries are removed from it.
4154 void ARMExidxSyntheticSection::finalizeContents() {
4155 // Ensure that any fixed-point iterations after the first see the original set
4156 // of sections.
4157 if (!originalExecutableSections.empty())
4158 executableSections = originalExecutableSections;
4159 else if (ctx.arg.enableNonContiguousRegions)
4160 originalExecutableSections = executableSections;
4162 // The executableSections and exidxSections that we use to derive the final
4163 // contents of this SyntheticSection are populated before
4164 // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or
4165 // ICF may remove executable InputSections and their dependent .ARM.exidx
4166 // section that we recorded earlier.
4167 auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); };
4168 llvm::erase_if(exidxSections, isDiscarded);
4169 // We need to remove discarded InputSections and InputSections without
4170 // .ARM.exidx sections that if we generated the .ARM.exidx it would be out
4171 // of range.
4172 auto isDiscardedOrOutOfRange = [this](InputSection *isec) {
4173 if (!isec->isLive())
4174 return true;
4175 if (findExidxSection(isec))
4176 return false;
4177 int64_t off = static_cast<int64_t>(isec->getVA() - getVA());
4178 return off != llvm::SignExtend64(off, 31);
4180 llvm::erase_if(executableSections, isDiscardedOrOutOfRange);
4182 // Sort the executable sections that may or may not have associated
4183 // .ARM.exidx sections by order of ascending address. This requires the
4184 // relative positions of InputSections and OutputSections to be known.
4185 auto compareByFilePosition = [](const InputSection *a,
4186 const InputSection *b) {
4187 OutputSection *aOut = a->getParent();
4188 OutputSection *bOut = b->getParent();
4190 if (aOut != bOut)
4191 return aOut->addr < bOut->addr;
4192 return a->outSecOff < b->outSecOff;
4194 llvm::stable_sort(executableSections, compareByFilePosition);
4195 sentinel = executableSections.back();
4196 // std::optionally merge adjacent duplicate entries.
4197 if (ctx.arg.mergeArmExidx) {
4198 SmallVector<InputSection *, 0> selectedSections;
4199 selectedSections.reserve(executableSections.size());
4200 selectedSections.push_back(executableSections[0]);
4201 size_t prev = 0;
4202 for (size_t i = 1; i < executableSections.size(); ++i) {
4203 InputSection *ex1 = findExidxSection(executableSections[prev]);
4204 InputSection *ex2 = findExidxSection(executableSections[i]);
4205 if (!isDuplicateArmExidxSec(ctx, ex1, ex2)) {
4206 selectedSections.push_back(executableSections[i]);
4207 prev = i;
4210 executableSections = std::move(selectedSections);
4212 // offset is within the SyntheticSection.
4213 size_t offset = 0;
4214 size = 0;
4215 for (InputSection *isec : executableSections) {
4216 if (InputSection *d = findExidxSection(isec)) {
4217 d->outSecOff = offset;
4218 d->parent = getParent();
4219 offset += d->getSize();
4220 } else {
4221 offset += 8;
4224 // Size includes Sentinel.
4225 size = offset + 8;
4228 InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const {
4229 return executableSections.front();
4232 // To write the .ARM.exidx table from the ExecutableSections we have three cases
4233 // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.
4234 // We write the .ARM.exidx section contents and apply its relocations.
4235 // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We
4236 // must write the contents of an EXIDX_CANTUNWIND directly. We use the
4237 // start of the InputSection as the purpose of the linker generated
4238 // section is to terminate the address range of the previous entry.
4239 // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of
4240 // the table to terminate the address range of the final entry.
4241 void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
4243 // A linker generated CANTUNWIND entry is made up of two words:
4244 // 0x0 with R_ARM_PREL31 relocation to target.
4245 // 0x1 with EXIDX_CANTUNWIND.
4246 uint64_t offset = 0;
4247 for (InputSection *isec : executableSections) {
4248 assert(isec->getParent() != nullptr);
4249 if (InputSection *d = findExidxSection(isec)) {
4250 for (int dataOffset = 0; dataOffset != (int)d->content().size();
4251 dataOffset += 4)
4252 write32(ctx, buf + offset + dataOffset,
4253 read32(ctx, d->content().data() + dataOffset));
4254 // Recalculate outSecOff as finalizeAddressDependentContent()
4255 // may have altered syntheticSection outSecOff.
4256 d->outSecOff = offset + outSecOff;
4257 ctx.target->relocateAlloc(*d, buf + offset);
4258 offset += d->getSize();
4259 } else {
4260 // A Linker generated CANTUNWIND section.
4261 write32(ctx, buf + offset + 0, 0x0);
4262 write32(ctx, buf + offset + 4, 0x1);
4263 uint64_t s = isec->getVA();
4264 uint64_t p = getVA() + offset;
4265 ctx.target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p);
4266 offset += 8;
4269 // Write Sentinel CANTUNWIND entry.
4270 write32(ctx, buf + offset + 0, 0x0);
4271 write32(ctx, buf + offset + 4, 0x1);
4272 uint64_t s = sentinel->getVA(sentinel->getSize());
4273 uint64_t p = getVA() + offset;
4274 ctx.target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p);
4275 assert(size == offset + 8);
4278 bool ARMExidxSyntheticSection::isNeeded() const {
4279 return llvm::any_of(exidxSections,
4280 [](InputSection *isec) { return isec->isLive(); });
4283 ThunkSection::ThunkSection(Ctx &ctx, OutputSection *os, uint64_t off)
4284 : SyntheticSection(ctx, ".text.thunk", SHT_PROGBITS,
4285 SHF_ALLOC | SHF_EXECINSTR,
4286 ctx.arg.emachine == EM_PPC64 ? 16 : 4) {
4287 this->parent = os;
4288 this->outSecOff = off;
4291 size_t ThunkSection::getSize() const {
4292 if (roundUpSizeForErrata)
4293 return alignTo(size, 4096);
4294 return size;
4297 void ThunkSection::addThunk(Thunk *t) {
4298 thunks.push_back(t);
4299 t->addSymbols(*this);
4302 void ThunkSection::writeTo(uint8_t *buf) {
4303 for (Thunk *t : thunks)
4304 t->writeTo(buf + t->offset);
4307 InputSection *ThunkSection::getTargetInputSection() const {
4308 if (thunks.empty())
4309 return nullptr;
4310 const Thunk *t = thunks.front();
4311 return t->getTargetInputSection();
4314 bool ThunkSection::assignOffsets() {
4315 uint64_t off = 0;
4316 for (Thunk *t : thunks) {
4317 off = alignToPowerOf2(off, t->alignment);
4318 t->setOffset(off);
4319 uint32_t size = t->size();
4320 t->getThunkTargetSym()->size = size;
4321 off += size;
4323 bool changed = off != size;
4324 size = off;
4325 return changed;
4328 PPC32Got2Section::PPC32Got2Section(Ctx &ctx)
4329 : SyntheticSection(ctx, ".got2", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, 4) {}
4331 bool PPC32Got2Section::isNeeded() const {
4332 // See the comment below. This is not needed if there is no other
4333 // InputSection.
4334 for (SectionCommand *cmd : getParent()->commands)
4335 if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
4336 for (InputSection *isec : isd->sections)
4337 if (isec != this)
4338 return true;
4339 return false;
4342 void PPC32Got2Section::finalizeContents() {
4343 // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in
4344 // .got2 . This function computes outSecOff of each .got2 to be used in
4345 // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is
4346 // to collect input sections named ".got2".
4347 for (SectionCommand *cmd : getParent()->commands)
4348 if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
4349 for (InputSection *isec : isd->sections) {
4350 // isec->file may be nullptr for MergeSyntheticSection.
4351 if (isec != this && isec->file)
4352 isec->file->ppc32Got2 = isec;
4357 // If linking position-dependent code then the table will store the addresses
4358 // directly in the binary so the section has type SHT_PROGBITS. If linking
4359 // position-independent code the section has type SHT_NOBITS since it will be
4360 // allocated and filled in by the dynamic linker.
4361 PPC64LongBranchTargetSection::PPC64LongBranchTargetSection(Ctx &ctx)
4362 : SyntheticSection(ctx, ".branch_lt",
4363 ctx.arg.isPic ? SHT_NOBITS : SHT_PROGBITS,
4364 SHF_ALLOC | SHF_WRITE, 8) {}
4366 uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol *sym,
4367 int64_t addend) {
4368 return getVA() + entry_index.find({sym, addend})->second * 8;
4371 std::optional<uint32_t>
4372 PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) {
4373 auto res =
4374 entry_index.try_emplace(std::make_pair(sym, addend), entries.size());
4375 if (!res.second)
4376 return std::nullopt;
4377 entries.emplace_back(sym, addend);
4378 return res.first->second;
4381 size_t PPC64LongBranchTargetSection::getSize() const {
4382 return entries.size() * 8;
4385 void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) {
4386 // If linking non-pic we have the final addresses of the targets and they get
4387 // written to the table directly. For pic the dynamic linker will allocate
4388 // the section and fill it.
4389 if (ctx.arg.isPic)
4390 return;
4392 for (auto entry : entries) {
4393 const Symbol *sym = entry.first;
4394 int64_t addend = entry.second;
4395 assert(sym->getVA(ctx));
4396 // Need calls to branch to the local entry-point since a long-branch
4397 // must be a local-call.
4398 write64(ctx, buf,
4399 sym->getVA(ctx, addend) +
4400 getPPC64GlobalEntryToLocalEntryOffset(ctx, sym->stOther));
4401 buf += 8;
4405 bool PPC64LongBranchTargetSection::isNeeded() const {
4406 // `removeUnusedSyntheticSections()` is called before thunk allocation which
4407 // is too early to determine if this section will be empty or not. We need
4408 // Finalized to keep the section alive until after thunk creation. Finalized
4409 // only gets set to true once `finalizeSections()` is called after thunk
4410 // creation. Because of this, if we don't create any long-branch thunks we end
4411 // up with an empty .branch_lt section in the binary.
4412 return !finalized || !entries.empty();
4415 static uint8_t getAbiVersion(Ctx &ctx) {
4416 // MIPS non-PIC executable gets ABI version 1.
4417 if (ctx.arg.emachine == EM_MIPS) {
4418 if (!ctx.arg.isPic && !ctx.arg.relocatable &&
4419 (ctx.arg.eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
4420 return 1;
4421 return 0;
4424 if (ctx.arg.emachine == EM_AMDGPU && !ctx.objectFiles.empty()) {
4425 uint8_t ver = ctx.objectFiles[0]->abiVersion;
4426 for (InputFile *file : ArrayRef(ctx.objectFiles).slice(1))
4427 if (file->abiVersion != ver)
4428 Err(ctx) << "incompatible ABI version: " << file;
4429 return ver;
4432 return 0;
4435 template <typename ELFT>
4436 void elf::writeEhdr(Ctx &ctx, uint8_t *buf, Partition &part) {
4437 memcpy(buf, "\177ELF", 4);
4439 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4440 eHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
4441 eHdr->e_ident[EI_DATA] =
4442 ELFT::Endianness == endianness::little ? ELFDATA2LSB : ELFDATA2MSB;
4443 eHdr->e_ident[EI_VERSION] = EV_CURRENT;
4444 eHdr->e_ident[EI_OSABI] = ctx.arg.osabi;
4445 eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(ctx);
4446 eHdr->e_machine = ctx.arg.emachine;
4447 eHdr->e_version = EV_CURRENT;
4448 eHdr->e_flags = ctx.arg.eflags;
4449 eHdr->e_ehsize = sizeof(typename ELFT::Ehdr);
4450 eHdr->e_phnum = part.phdrs.size();
4451 eHdr->e_shentsize = sizeof(typename ELFT::Shdr);
4453 if (!ctx.arg.relocatable) {
4454 eHdr->e_phoff = sizeof(typename ELFT::Ehdr);
4455 eHdr->e_phentsize = sizeof(typename ELFT::Phdr);
4459 template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) {
4460 // Write the program header table.
4461 auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf);
4462 for (std::unique_ptr<PhdrEntry> &p : part.phdrs) {
4463 hBuf->p_type = p->p_type;
4464 hBuf->p_flags = p->p_flags;
4465 hBuf->p_offset = p->p_offset;
4466 hBuf->p_vaddr = p->p_vaddr;
4467 hBuf->p_paddr = p->p_paddr;
4468 hBuf->p_filesz = p->p_filesz;
4469 hBuf->p_memsz = p->p_memsz;
4470 hBuf->p_align = p->p_align;
4471 ++hBuf;
4475 template <typename ELFT>
4476 PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection(Ctx &ctx)
4477 : SyntheticSection(ctx, "", SHT_LLVM_PART_EHDR, SHF_ALLOC, 1) {}
4479 template <typename ELFT>
4480 size_t PartitionElfHeaderSection<ELFT>::getSize() const {
4481 return sizeof(typename ELFT::Ehdr);
4484 template <typename ELFT>
4485 void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4486 writeEhdr<ELFT>(ctx, buf, getPartition(ctx));
4488 // Loadable partitions are always ET_DYN.
4489 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4490 eHdr->e_type = ET_DYN;
4493 template <typename ELFT>
4494 PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection(Ctx &ctx)
4495 : SyntheticSection(ctx, ".phdrs", SHT_LLVM_PART_PHDR, SHF_ALLOC, 1) {}
4497 template <typename ELFT>
4498 size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
4499 return sizeof(typename ELFT::Phdr) * getPartition(ctx).phdrs.size();
4502 template <typename ELFT>
4503 void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {
4504 writePhdrs<ELFT>(buf, getPartition(ctx));
4507 PartitionIndexSection::PartitionIndexSection(Ctx &ctx)
4508 : SyntheticSection(ctx, ".rodata", SHT_PROGBITS, SHF_ALLOC, 4) {}
4510 size_t PartitionIndexSection::getSize() const {
4511 return 12 * (ctx.partitions.size() - 1);
4514 void PartitionIndexSection::finalizeContents() {
4515 for (size_t i = 1; i != ctx.partitions.size(); ++i)
4516 ctx.partitions[i].nameStrTab =
4517 ctx.mainPart->dynStrTab->addString(ctx.partitions[i].name);
4520 void PartitionIndexSection::writeTo(uint8_t *buf) {
4521 uint64_t va = getVA();
4522 for (size_t i = 1; i != ctx.partitions.size(); ++i) {
4523 write32(ctx, buf,
4524 ctx.mainPart->dynStrTab->getVA() + ctx.partitions[i].nameStrTab -
4525 va);
4526 write32(ctx, buf + 4, ctx.partitions[i].elfHeader->getVA() - (va + 4));
4528 SyntheticSection *next = i == ctx.partitions.size() - 1
4529 ? ctx.in.partEnd.get()
4530 : ctx.partitions[i + 1].elfHeader.get();
4531 write32(ctx, buf + 8, next->getVA() - ctx.partitions[i].elfHeader->getVA());
4533 va += 12;
4534 buf += 12;
4538 static bool needsInterpSection(Ctx &ctx) {
4539 return !ctx.arg.relocatable && !ctx.arg.shared &&
4540 !ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
4543 bool elf::hasMemtag(Ctx &ctx) {
4544 return ctx.arg.emachine == EM_AARCH64 &&
4545 ctx.arg.androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
4548 // Fully static executables don't support MTE globals at this point in time, as
4549 // we currently rely on:
4550 // - A dynamic loader to process relocations, and
4551 // - Dynamic entries.
4552 // This restriction could be removed in future by re-using some of the ideas
4553 // that ifuncs use in fully static executables.
4554 bool elf::canHaveMemtagGlobals(Ctx &ctx) {
4555 return hasMemtag(ctx) &&
4556 (ctx.arg.relocatable || ctx.arg.shared || needsInterpSection(ctx));
4559 constexpr char kMemtagAndroidNoteName[] = "Android";
4560 void MemtagAndroidNote::writeTo(uint8_t *buf) {
4561 static_assert(
4562 sizeof(kMemtagAndroidNoteName) == 8,
4563 "Android 11 & 12 have an ABI that the note name is 8 bytes long. Keep it "
4564 "that way for backwards compatibility.");
4566 write32(ctx, buf, sizeof(kMemtagAndroidNoteName));
4567 write32(ctx, buf + 4, sizeof(uint32_t));
4568 write32(ctx, buf + 8, ELF::NT_ANDROID_TYPE_MEMTAG);
4569 memcpy(buf + 12, kMemtagAndroidNoteName, sizeof(kMemtagAndroidNoteName));
4570 buf += 12 + alignTo(sizeof(kMemtagAndroidNoteName), 4);
4572 uint32_t value = 0;
4573 value |= ctx.arg.androidMemtagMode;
4574 if (ctx.arg.androidMemtagHeap)
4575 value |= ELF::NT_MEMTAG_HEAP;
4576 // Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled
4577 // binary on Android 11 or 12 will result in a checkfail in the loader.
4578 if (ctx.arg.androidMemtagStack)
4579 value |= ELF::NT_MEMTAG_STACK;
4580 write32(ctx, buf, value); // note value
4583 size_t MemtagAndroidNote::getSize() const {
4584 return sizeof(llvm::ELF::Elf64_Nhdr) +
4585 /*namesz=*/alignTo(sizeof(kMemtagAndroidNoteName), 4) +
4586 /*descsz=*/sizeof(uint32_t);
4589 void PackageMetadataNote::writeTo(uint8_t *buf) {
4590 write32(ctx, buf, 4);
4591 write32(ctx, buf + 4, ctx.arg.packageMetadata.size() + 1);
4592 write32(ctx, buf + 8, FDO_PACKAGING_METADATA);
4593 memcpy(buf + 12, "FDO", 4);
4594 memcpy(buf + 16, ctx.arg.packageMetadata.data(),
4595 ctx.arg.packageMetadata.size());
4598 size_t PackageMetadataNote::getSize() const {
4599 return sizeof(llvm::ELF::Elf64_Nhdr) + 4 +
4600 alignTo(ctx.arg.packageMetadata.size() + 1, 4);
4603 // Helper function, return the size of the ULEB128 for 'v', optionally writing
4604 // it to `*(buf + offset)` if `buf` is non-null.
4605 static size_t computeOrWriteULEB128(uint64_t v, uint8_t *buf, size_t offset) {
4606 if (buf)
4607 return encodeULEB128(v, buf + offset);
4608 return getULEB128Size(v);
4611 // https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic
4612 constexpr uint64_t kMemtagStepSizeBits = 3;
4613 constexpr uint64_t kMemtagGranuleSize = 16;
4614 static size_t
4615 createMemtagGlobalDescriptors(Ctx &ctx,
4616 const SmallVector<const Symbol *, 0> &symbols,
4617 uint8_t *buf = nullptr) {
4618 size_t sectionSize = 0;
4619 uint64_t lastGlobalEnd = 0;
4621 for (const Symbol *sym : symbols) {
4622 if (!includeInSymtab(ctx, *sym))
4623 continue;
4624 const uint64_t addr = sym->getVA(ctx);
4625 const uint64_t size = sym->getSize();
4627 if (addr <= kMemtagGranuleSize && buf != nullptr)
4628 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4629 << "\" falls in the ELF header. This is indicative of a "
4630 "compiler/linker bug";
4631 if (addr % kMemtagGranuleSize != 0)
4632 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4633 << "\" at 0x" << Twine::utohexstr(addr)
4634 << "\" is not granule (16-byte) aligned";
4635 if (size == 0)
4636 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4637 << "\" is not allowed to be zero";
4638 if (size % kMemtagGranuleSize != 0)
4639 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4640 << "\" (size 0x" << Twine::utohexstr(size)
4641 << ") is not granule (16-byte) aligned";
4643 const uint64_t sizeToEncode = size / kMemtagGranuleSize;
4644 const uint64_t stepToEncode = ((addr - lastGlobalEnd) / kMemtagGranuleSize)
4645 << kMemtagStepSizeBits;
4646 if (sizeToEncode < (1 << kMemtagStepSizeBits)) {
4647 sectionSize += computeOrWriteULEB128(stepToEncode | sizeToEncode, buf, sectionSize);
4648 } else {
4649 sectionSize += computeOrWriteULEB128(stepToEncode, buf, sectionSize);
4650 sectionSize += computeOrWriteULEB128(sizeToEncode - 1, buf, sectionSize);
4652 lastGlobalEnd = addr + size;
4655 return sectionSize;
4658 bool MemtagGlobalDescriptors::updateAllocSize(Ctx &ctx) {
4659 size_t oldSize = getSize();
4660 std::stable_sort(symbols.begin(), symbols.end(),
4661 [&ctx = ctx](const Symbol *s1, const Symbol *s2) {
4662 return s1->getVA(ctx) < s2->getVA(ctx);
4664 return oldSize != getSize();
4667 void MemtagGlobalDescriptors::writeTo(uint8_t *buf) {
4668 createMemtagGlobalDescriptors(ctx, symbols, buf);
4671 size_t MemtagGlobalDescriptors::getSize() const {
4672 return createMemtagGlobalDescriptors(ctx, symbols);
4675 static OutputSection *findSection(Ctx &ctx, StringRef name) {
4676 for (SectionCommand *cmd : ctx.script->sectionCommands)
4677 if (auto *osd = dyn_cast<OutputDesc>(cmd))
4678 if (osd->osec.name == name)
4679 return &osd->osec;
4680 return nullptr;
4683 static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
4684 uint64_t val, uint8_t stOther = STV_HIDDEN) {
4685 Symbol *s = ctx.symtab->find(name);
4686 if (!s || s->isDefined() || s->isCommon())
4687 return nullptr;
4689 s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
4690 stOther, STT_NOTYPE, val,
4691 /*size=*/0, sec});
4692 s->isUsedInRegularObj = true;
4693 return cast<Defined>(s);
4696 template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
4697 // Add the .interp section first because it is not a SyntheticSection.
4698 // The removeUnusedSyntheticSections() function relies on the
4699 // SyntheticSections coming last.
4700 if (needsInterpSection(ctx)) {
4701 for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
4702 InputSection *sec = createInterpSection(ctx);
4703 sec->partition = i;
4704 ctx.inputSections.push_back(sec);
4708 auto add = [&](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };
4710 if (ctx.arg.zSectionHeader)
4711 ctx.in.shStrTab =
4712 std::make_unique<StringTableSection>(ctx, ".shstrtab", false);
4714 ctx.out.programHeaders =
4715 std::make_unique<OutputSection>(ctx, "", 0, SHF_ALLOC);
4716 ctx.out.programHeaders->addralign = ctx.arg.wordsize;
4718 if (ctx.arg.strip != StripPolicy::All) {
4719 ctx.in.strTab = std::make_unique<StringTableSection>(ctx, ".strtab", false);
4720 ctx.in.symTab =
4721 std::make_unique<SymbolTableSection<ELFT>>(ctx, *ctx.in.strTab);
4722 ctx.in.symTabShndx = std::make_unique<SymtabShndxSection>(ctx);
4725 ctx.in.bss = std::make_unique<BssSection>(ctx, ".bss", 0, 1);
4726 add(*ctx.in.bss);
4728 // If there is a SECTIONS command and a .data.rel.ro section name use name
4729 // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
4730 // This makes sure our relro is contiguous.
4731 bool hasDataRelRo =
4732 ctx.script->hasSectionsCommand && findSection(ctx, ".data.rel.ro");
4733 ctx.in.bssRelRo = std::make_unique<BssSection>(
4734 ctx, hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
4735 add(*ctx.in.bssRelRo);
4737 // Add MIPS-specific sections.
4738 if (ctx.arg.emachine == EM_MIPS) {
4739 if (!ctx.arg.shared && ctx.arg.hasDynSymTab) {
4740 ctx.in.mipsRldMap = std::make_unique<MipsRldMapSection>(ctx);
4741 add(*ctx.in.mipsRldMap);
4743 if ((ctx.in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create(ctx)))
4744 add(*ctx.in.mipsAbiFlags);
4745 if ((ctx.in.mipsOptions = MipsOptionsSection<ELFT>::create(ctx)))
4746 add(*ctx.in.mipsOptions);
4747 if ((ctx.in.mipsReginfo = MipsReginfoSection<ELFT>::create(ctx)))
4748 add(*ctx.in.mipsReginfo);
4751 StringRef relaDynName = ctx.arg.isRela ? ".rela.dyn" : ".rel.dyn";
4753 const unsigned threadCount = ctx.arg.threadCount;
4754 for (Partition &part : ctx.partitions) {
4755 auto add = [&](SyntheticSection &sec) {
4756 sec.partition = part.getNumber(ctx);
4757 ctx.inputSections.push_back(&sec);
4760 if (!part.name.empty()) {
4761 part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>(ctx);
4762 part.elfHeader->name = part.name;
4763 add(*part.elfHeader);
4765 part.programHeaders =
4766 std::make_unique<PartitionProgramHeadersSection<ELFT>>(ctx);
4767 add(*part.programHeaders);
4770 if (ctx.arg.buildId != BuildIdKind::None) {
4771 part.buildId = std::make_unique<BuildIdSection>(ctx);
4772 add(*part.buildId);
4775 // dynSymTab is always present to simplify sym->includeInDynsym(ctx) in
4776 // finalizeSections.
4777 part.dynStrTab = std::make_unique<StringTableSection>(ctx, ".dynstr", true);
4778 part.dynSymTab =
4779 std::make_unique<SymbolTableSection<ELFT>>(ctx, *part.dynStrTab);
4781 if (ctx.arg.relocatable)
4782 continue;
4783 part.dynamic = std::make_unique<DynamicSection<ELFT>>(ctx);
4785 if (hasMemtag(ctx)) {
4786 part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>(ctx);
4787 add(*part.memtagAndroidNote);
4788 if (canHaveMemtagGlobals(ctx)) {
4789 part.memtagGlobalDescriptors =
4790 std::make_unique<MemtagGlobalDescriptors>(ctx);
4791 add(*part.memtagGlobalDescriptors);
4795 if (ctx.arg.androidPackDynRelocs)
4796 part.relaDyn = std::make_unique<AndroidPackedRelocationSection<ELFT>>(
4797 ctx, relaDynName, threadCount);
4798 else
4799 part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
4800 ctx, relaDynName, ctx.arg.zCombreloc, threadCount);
4802 if (ctx.arg.hasDynSymTab) {
4803 add(*part.dynSymTab);
4805 part.verSym = std::make_unique<VersionTableSection>(ctx);
4806 add(*part.verSym);
4808 if (!namedVersionDefs(ctx).empty()) {
4809 part.verDef = std::make_unique<VersionDefinitionSection>(ctx);
4810 add(*part.verDef);
4813 part.verNeed = std::make_unique<VersionNeedSection<ELFT>>(ctx);
4814 add(*part.verNeed);
4816 if (ctx.arg.gnuHash) {
4817 part.gnuHashTab = std::make_unique<GnuHashTableSection>(ctx);
4818 add(*part.gnuHashTab);
4821 if (ctx.arg.sysvHash) {
4822 part.hashTab = std::make_unique<HashTableSection>(ctx);
4823 add(*part.hashTab);
4826 add(*part.dynamic);
4827 add(*part.dynStrTab);
4829 add(*part.relaDyn);
4831 if (ctx.arg.relrPackDynRelocs) {
4832 part.relrDyn = std::make_unique<RelrSection<ELFT>>(ctx, threadCount);
4833 add(*part.relrDyn);
4834 part.relrAuthDyn = std::make_unique<RelrSection<ELFT>>(
4835 ctx, threadCount, /*isAArch64Auth=*/true);
4836 add(*part.relrAuthDyn);
4839 if (ctx.arg.ehFrameHdr) {
4840 part.ehFrameHdr = std::make_unique<EhFrameHeader>(ctx);
4841 add(*part.ehFrameHdr);
4843 part.ehFrame = std::make_unique<EhFrameSection>(ctx);
4844 add(*part.ehFrame);
4846 if (ctx.arg.emachine == EM_ARM) {
4847 // This section replaces all the individual .ARM.exidx InputSections.
4848 part.armExidx = std::make_unique<ARMExidxSyntheticSection>(ctx);
4849 add(*part.armExidx);
4852 if (!ctx.arg.packageMetadata.empty()) {
4853 part.packageMetadataNote = std::make_unique<PackageMetadataNote>(ctx);
4854 add(*part.packageMetadataNote);
4858 if (ctx.partitions.size() != 1) {
4859 // Create the partition end marker. This needs to be in partition number 255
4860 // so that it is sorted after all other partitions. It also has other
4861 // special handling (see createPhdrs() and combineEhSections()).
4862 ctx.in.partEnd =
4863 std::make_unique<BssSection>(ctx, ".part.end", ctx.arg.maxPageSize, 1);
4864 ctx.in.partEnd->partition = 255;
4865 add(*ctx.in.partEnd);
4867 ctx.in.partIndex = std::make_unique<PartitionIndexSection>(ctx);
4868 addOptionalRegular(ctx, "__part_index_begin", ctx.in.partIndex.get(), 0);
4869 addOptionalRegular(ctx, "__part_index_end", ctx.in.partIndex.get(),
4870 ctx.in.partIndex->getSize());
4871 add(*ctx.in.partIndex);
4874 // Add .got. MIPS' .got is so different from the other archs,
4875 // it has its own class.
4876 if (ctx.arg.emachine == EM_MIPS) {
4877 ctx.in.mipsGot = std::make_unique<MipsGotSection>(ctx);
4878 add(*ctx.in.mipsGot);
4879 } else {
4880 ctx.in.got = std::make_unique<GotSection>(ctx);
4881 add(*ctx.in.got);
4884 if (ctx.arg.emachine == EM_PPC) {
4885 ctx.in.ppc32Got2 = std::make_unique<PPC32Got2Section>(ctx);
4886 add(*ctx.in.ppc32Got2);
4889 if (ctx.arg.emachine == EM_PPC64) {
4890 ctx.in.ppc64LongBranchTarget =
4891 std::make_unique<PPC64LongBranchTargetSection>(ctx);
4892 add(*ctx.in.ppc64LongBranchTarget);
4895 ctx.in.gotPlt = std::make_unique<GotPltSection>(ctx);
4896 add(*ctx.in.gotPlt);
4897 ctx.in.igotPlt = std::make_unique<IgotPltSection>(ctx);
4898 add(*ctx.in.igotPlt);
4899 // Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the
4900 // section in the absence of PHDRS/SECTIONS commands.
4901 if (ctx.arg.zRelro &&
4902 ((ctx.script->phdrsCommands.empty() && !ctx.script->hasSectionsCommand) ||
4903 ctx.script->seenRelroEnd)) {
4904 ctx.in.relroPadding = std::make_unique<RelroPaddingSection>(ctx);
4905 add(*ctx.in.relroPadding);
4908 if (ctx.arg.emachine == EM_ARM) {
4909 ctx.in.armCmseSGSection = std::make_unique<ArmCmseSGSection>(ctx);
4910 add(*ctx.in.armCmseSGSection);
4913 // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
4914 // it as a relocation and ensure the referenced section is created.
4915 if (ctx.sym.globalOffsetTable && ctx.arg.emachine != EM_MIPS) {
4916 if (ctx.target->gotBaseSymInGotPlt)
4917 ctx.in.gotPlt->hasGotPltOffRel = true;
4918 else
4919 ctx.in.got->hasGotOffRel = true;
4922 // We always need to add rel[a].plt to output if it has entries.
4923 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
4924 ctx.in.relaPlt = std::make_unique<RelocationSection<ELFT>>(
4925 ctx, ctx.arg.isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false,
4926 /*threadCount=*/1);
4927 add(*ctx.in.relaPlt);
4929 if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
4930 (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
4931 ctx.in.ibtPlt = std::make_unique<IBTPltSection>(ctx);
4932 add(*ctx.in.ibtPlt);
4935 if (ctx.arg.emachine == EM_PPC)
4936 ctx.in.plt = std::make_unique<PPC32GlinkSection>(ctx);
4937 else
4938 ctx.in.plt = std::make_unique<PltSection>(ctx);
4939 add(*ctx.in.plt);
4940 ctx.in.iplt = std::make_unique<IpltSection>(ctx);
4941 add(*ctx.in.iplt);
4943 if (ctx.arg.andFeatures || !ctx.aarch64PauthAbiCoreInfo.empty()) {
4944 ctx.in.gnuProperty = std::make_unique<GnuPropertySection>(ctx);
4945 add(*ctx.in.gnuProperty);
4948 if (ctx.arg.debugNames) {
4949 ctx.in.debugNames = std::make_unique<DebugNamesSection<ELFT>>(ctx);
4950 add(*ctx.in.debugNames);
4953 if (ctx.arg.gdbIndex) {
4954 ctx.in.gdbIndex = GdbIndexSection::create<ELFT>(ctx);
4955 add(*ctx.in.gdbIndex);
4958 // .note.GNU-stack is always added when we are creating a re-linkable
4959 // object file. Other linkers are using the presence of this marker
4960 // section to control the executable-ness of the stack area, but that
4961 // is irrelevant these days. Stack area should always be non-executable
4962 // by default. So we emit this section unconditionally.
4963 if (ctx.arg.relocatable) {
4964 ctx.in.gnuStack = std::make_unique<GnuStackSection>(ctx);
4965 add(*ctx.in.gnuStack);
4968 if (ctx.in.symTab)
4969 add(*ctx.in.symTab);
4970 if (ctx.in.symTabShndx)
4971 add(*ctx.in.symTabShndx);
4972 if (ctx.in.shStrTab)
4973 add(*ctx.in.shStrTab);
4974 if (ctx.in.strTab)
4975 add(*ctx.in.strTab);
4978 template void elf::splitSections<ELF32LE>(Ctx &);
4979 template void elf::splitSections<ELF32BE>(Ctx &);
4980 template void elf::splitSections<ELF64LE>(Ctx &);
4981 template void elf::splitSections<ELF64BE>(Ctx &);
4983 template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>(
4984 function_ref<void(InputSection &)>);
4985 template void EhFrameSection::iterateFDEWithLSDA<ELF32BE>(
4986 function_ref<void(InputSection &)>);
4987 template void EhFrameSection::iterateFDEWithLSDA<ELF64LE>(
4988 function_ref<void(InputSection &)>);
4989 template void EhFrameSection::iterateFDEWithLSDA<ELF64BE>(
4990 function_ref<void(InputSection &)>);
4992 template class elf::SymbolTableSection<ELF32LE>;
4993 template class elf::SymbolTableSection<ELF32BE>;
4994 template class elf::SymbolTableSection<ELF64LE>;
4995 template class elf::SymbolTableSection<ELF64BE>;
4997 template void elf::writeEhdr<ELF32LE>(Ctx &, uint8_t *Buf, Partition &Part);
4998 template void elf::writeEhdr<ELF32BE>(Ctx &, uint8_t *Buf, Partition &Part);
4999 template void elf::writeEhdr<ELF64LE>(Ctx &, uint8_t *Buf, Partition &Part);
5000 template void elf::writeEhdr<ELF64BE>(Ctx &, uint8_t *Buf, Partition &Part);
5002 template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
5003 template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
5004 template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);
5005 template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);
5007 template void elf::createSyntheticSections<ELF32LE>(Ctx &);
5008 template void elf::createSyntheticSections<ELF32BE>(Ctx &);
5009 template void elf::createSyntheticSections<ELF64LE>(Ctx &);
5010 template void elf::createSyntheticSections<ELF64BE>(Ctx &);