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