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, ctx.arg.androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
1338 addInt(DT_AARCH64_MEMTAG_HEAP, ctx.arg.androidMemtagHeap);
1339 addInt(DT_AARCH64_MEMTAG_STACK, ctx.arg.androidMemtagStack);
1340 if (ctx.mainPart->memtagGlobalDescriptors->isNeeded()) {
1341 addInSec(DT_AARCH64_MEMTAG_GLOBALS,
1342 *ctx.mainPart->memtagGlobalDescriptors);
1343 addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,
1344 ctx.mainPart->memtagGlobalDescriptors->getSize());
1345 }
1346 }
1347 }
1348
1349 addInSec(DT_SYMTAB, *part.dynSymTab);
1350 addInt(DT_SYMENT, sizeof(Elf_Sym));
1351 addInSec(DT_STRTAB, *part.dynStrTab);
1352 addInt(DT_STRSZ, part.dynStrTab->getSize());
1353 if (!ctx.arg.zText)
1354 addInt(DT_TEXTREL, 0);
1355 if (part.gnuHashTab && part.gnuHashTab->getParent())
1356 addInSec(DT_GNU_HASH, *part.gnuHashTab);
1357 if (part.hashTab && part.hashTab->getParent())
1358 addInSec(DT_HASH, *part.hashTab);
1359
1360 if (isMain) {
1361 if (ctx.out.preinitArray) {
1362 addInt(DT_PREINIT_ARRAY, ctx.out.preinitArray->addr);
1363 addInt(DT_PREINIT_ARRAYSZ, ctx.out.preinitArray->size);
1364 }
1365 if (ctx.out.initArray) {
1366 addInt(DT_INIT_ARRAY, ctx.out.initArray->addr);
1367 addInt(DT_INIT_ARRAYSZ, ctx.out.initArray->size);
1368 }
1369 if (ctx.out.finiArray) {
1370 addInt(DT_FINI_ARRAY, ctx.out.finiArray->addr);
1371 addInt(DT_FINI_ARRAYSZ, ctx.out.finiArray->size);
1372 }
1373
1374 if (Symbol *b = ctx.symtab->find(name: ctx.arg.init))
1375 if (b->isDefined())
1376 addInt(DT_INIT, b->getVA(ctx));
1377 if (Symbol *b = ctx.symtab->find(name: ctx.arg.fini))
1378 if (b->isDefined())
1379 addInt(DT_FINI, b->getVA(ctx));
1380 }
1381
1382 if (part.verSym && part.verSym->isNeeded())
1383 addInSec(DT_VERSYM, *part.verSym);
1384 if (part.verDef && part.verDef->isLive()) {
1385 addInSec(DT_VERDEF, *part.verDef);
1386 addInt(DT_VERDEFNUM, getVerDefNum(ctx));
1387 }
1388 if (part.verNeed && part.verNeed->isNeeded()) {
1389 addInSec(DT_VERNEED, *part.verNeed);
1390 unsigned needNum = 0;
1391 for (SharedFile *f : ctx.sharedFiles)
1392 if (!f->verneedInfo.empty())
1393 ++needNum;
1394 addInt(DT_VERNEEDNUM, needNum);
1395 }
1396
1397 if (ctx.arg.emachine == EM_MIPS) {
1398 addInt(DT_MIPS_RLD_VERSION, 1);
1399 addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1400 addInt(DT_MIPS_BASE_ADDRESS, ctx.target->getImageBase());
1401 addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1402 addInt(DT_MIPS_LOCAL_GOTNO, ctx.in.mipsGot->getLocalEntriesNum());
1403
1404 if (const Symbol *b = ctx.in.mipsGot->getFirstGlobalEntry())
1405 addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1406 else
1407 addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1408 addInSec(DT_PLTGOT, *ctx.in.mipsGot);
1409 if (ctx.in.mipsRldMap) {
1410 if (!ctx.arg.pie)
1411 addInSec(DT_MIPS_RLD_MAP, *ctx.in.mipsRldMap);
1412 // Store the offset to the .rld_map section
1413 // relative to the address of the tag.
1414 addInt(DT_MIPS_RLD_MAP_REL,
1415 ctx.in.mipsRldMap->getVA() - (getVA() + entries.size() * entsize));
1416 }
1417 }
1418
1419 // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1420 // glibc assumes the old-style BSS PLT layout which we don't support.
1421 if (ctx.arg.emachine == EM_PPC)
1422 addInSec(DT_PPC_GOT, *ctx.in.got);
1423
1424 // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1425 if (ctx.arg.emachine == EM_PPC64 && ctx.in.plt->isNeeded()) {
1426 // The Glink tag points to 32 bytes before the first lazy symbol resolution
1427 // stub, which starts directly after the header.
1428 addInt(DT_PPC64_GLINK,
1429 ctx.in.plt->getVA() + ctx.target->pltHeaderSize - 32);
1430 }
1431
1432 if (ctx.arg.emachine == EM_PPC64)
1433 addInt(DT_PPC64_OPT, ctx.target->ppc64DynamicSectionOpt);
1434
1435 addInt(DT_NULL, 0);
1436 return entries;
1437}
1438
1439template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1440 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
1441 getParent()->link = sec->sectionIndex;
1442 this->size = computeContents().size() * this->entsize;
1443}
1444
1445template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1446 auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1447
1448 for (std::pair<int32_t, uint64_t> kv : computeContents()) {
1449 p->d_tag = kv.first;
1450 p->d_un.d_val = kv.second;
1451 ++p;
1452 }
1453}
1454
1455uint64_t DynamicReloc::getOffset() const {
1456 return inputSec->getVA(offset: offsetInSec);
1457}
1458
1459int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
1460 assert(!isFinal && "addend already computed");
1461 uint64_t ca = inputSec->getRelocTargetVA(
1462 ctx, r: Relocation{.expr: expr, .type: type, .offset: 0, .addend: addend, .sym: sym}, p: getOffset());
1463 return ctx.arg.is64 ? ca : SignExtend64<32>(x: ca);
1464}
1465
1466uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
1467 if (!needsDynSymIndex())
1468 return 0;
1469
1470 size_t index = symTab->getSymbolIndex(sym: *sym);
1471 assert((index != 0 ||
1472 (type != symTab->ctx.target->gotRel &&
1473 type != symTab->ctx.target->pltRel) ||
1474 !symTab->ctx.mainPart->dynSymTab->getParent()) &&
1475 "GOT or PLT relocation must refer to symbol in dynamic symbol table");
1476 return index;
1477}
1478
1479RelocationBaseSection::RelocationBaseSection(Ctx &ctx, StringRef name,
1480 uint32_t type, int32_t dynamicTag,
1481 int32_t sizeDynamicTag,
1482 bool combreloc,
1483 unsigned concurrency)
1484 : SyntheticSection(ctx, name, type, SHF_ALLOC, ctx.arg.wordsize),
1485 dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag),
1486 relocsVec(concurrency), combreloc(combreloc) {}
1487
1488void RelocationBaseSection::addSymbolReloc(
1489 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1490 int64_t addend, std::optional<RelType> addendRelType) {
1491 addReloc(isAgainstSymbol: true, dynType, sec&: isec, offsetInSec, sym, addend, expr: R_ADDEND,
1492 addendRelType: addendRelType ? *addendRelType : ctx.target->noneRel);
1493}
1494
1495void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
1496 RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,
1497 RelType addendRelType) {
1498 // No need to write an addend to the section for preemptible symbols.
1499 if (sym.isPreemptible)
1500 addReloc(reloc: {dynType, &isec, offsetInSec, true, sym, 0, R_ADDEND});
1501 else
1502 addReloc(isAgainstSymbol: false, dynType, sec&: isec, offsetInSec, sym, addend: 0, expr: R_ABS, addendRelType);
1503}
1504
1505void RelocationBaseSection::mergeRels() {
1506 size_t newSize = relocs.size();
1507 for (const auto &v : relocsVec)
1508 newSize += v.size();
1509 relocs.reserve(N: newSize);
1510 for (const auto &v : relocsVec)
1511 llvm::append_range(C&: relocs, R: v);
1512 relocsVec.clear();
1513}
1514
1515void RelocationBaseSection::partitionRels() {
1516 if (!combreloc)
1517 return;
1518 const RelType relativeRel = ctx.target->relativeRel;
1519 numRelativeRelocs =
1520 std::stable_partition(first: relocs.begin(), last: relocs.end(),
1521 pred: [=](auto &r) { return r.type == relativeRel; }) -
1522 relocs.begin();
1523}
1524
1525void RelocationBaseSection::finalizeContents() {
1526 mergeRels();
1527 // Compute DT_RELACOUNT to be used by part.dynamic.
1528 partitionRels();
1529 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1530
1531 // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1532 // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1533 // case.
1534 if (symTab && symTab->getParent())
1535 getParent()->link = symTab->getParent()->sectionIndex;
1536 else
1537 getParent()->link = 0;
1538
1539 if (ctx.in.relaPlt.get() == this && ctx.in.gotPlt->getParent()) {
1540 getParent()->flags |= ELF::SHF_INFO_LINK;
1541 getParent()->info = ctx.in.gotPlt->getParent()->sectionIndex;
1542 }
1543}
1544
1545void DynamicReloc::finalize(Ctx &ctx, SymbolTableBaseSection *symt) {
1546 r_offset = getOffset();
1547 r_sym = getSymIndex(symTab: symt);
1548 addend = computeAddend(ctx);
1549 isFinal = true; // Catch errors
1550}
1551
1552void RelocationBaseSection::computeRels() {
1553 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1554 parallelForEach(R&: relocs, Fn: [&ctx = ctx, symTab](DynamicReloc &rel) {
1555 rel.finalize(ctx, symt: symTab);
1556 });
1557
1558 auto irelative = std::stable_partition(
1559 first: relocs.begin() + numRelativeRelocs, last: relocs.end(),
1560 pred: [t = ctx.target->iRelativeRel](auto &r) { return r.type != t; });
1561
1562 // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1563 // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1564 // is to make results easier to read.
1565 if (combreloc) {
1566 auto nonRelative = relocs.begin() + numRelativeRelocs;
1567 parallelSort(Start: relocs.begin(), End: nonRelative,
1568 Comp: [&](auto &a, auto &b) { return a.r_offset < b.r_offset; });
1569 // Non-relative relocations are few, so don't bother with parallelSort.
1570 llvm::sort(Start: nonRelative, End: irelative, Comp: [&](auto &a, auto &b) {
1571 return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);
1572 });
1573 }
1574}
1575
1576template <class ELFT>
1577RelocationSection<ELFT>::RelocationSection(Ctx &ctx, StringRef name,
1578 bool combreloc, unsigned concurrency)
1579 : RelocationBaseSection(ctx, name, ctx.arg.isRela ? SHT_RELA : SHT_REL,
1580 ctx.arg.isRela ? DT_RELA : DT_REL,
1581 ctx.arg.isRela ? DT_RELASZ : DT_RELSZ, combreloc,
1582 concurrency) {
1583 this->entsize = ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1584}
1585
1586template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1587 computeRels();
1588 for (const DynamicReloc &rel : relocs) {
1589 auto *p = reinterpret_cast<Elf_Rela *>(buf);
1590 p->r_offset = rel.r_offset;
1591 p->setSymbolAndType(rel.r_sym, rel.type, ctx.arg.isMips64EL);
1592 if (ctx.arg.isRela)
1593 p->r_addend = rel.addend;
1594 buf += ctx.arg.isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1595 }
1596}
1597
1598RelrBaseSection::RelrBaseSection(Ctx &ctx, unsigned concurrency,
1599 bool isAArch64Auth)
1600 : SyntheticSection(
1601 ctx, isAArch64Auth ? ".relr.auth.dyn" : ".relr.dyn",
1602 isAArch64Auth
1603 ? SHT_AARCH64_AUTH_RELR
1604 : (ctx.arg.useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR),
1605 SHF_ALLOC, ctx.arg.wordsize),
1606 relocsVec(concurrency) {}
1607
1608void RelrBaseSection::mergeRels() {
1609 size_t newSize = relocs.size();
1610 for (const auto &v : relocsVec)
1611 newSize += v.size();
1612 relocs.reserve(N: newSize);
1613 for (const auto &v : relocsVec)
1614 llvm::append_range(C&: relocs, R: v);
1615 relocsVec.clear();
1616}
1617
1618void RelrBaseSection::finalizeContents() { mergeRels(); }
1619
1620template <class ELFT>
1621AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1622 Ctx &ctx, StringRef name, unsigned concurrency)
1623 : RelocationBaseSection(
1624 ctx, name, ctx.arg.isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1625 ctx.arg.isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1626 ctx.arg.isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ,
1627 /*combreloc=*/false, concurrency) {
1628 this->entsize = 1;
1629}
1630
1631template <class ELFT>
1632bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
1633 // This function computes the contents of an Android-format packed relocation
1634 // section.
1635 //
1636 // This format compresses relocations by using relocation groups to factor out
1637 // fields that are common between relocations and storing deltas from previous
1638 // relocations in SLEB128 format (which has a short representation for small
1639 // numbers). A good example of a relocation type with common fields is
1640 // R_*_RELATIVE, which is normally used to represent function pointers in
1641 // vtables. In the REL format, each relative relocation has the same r_info
1642 // field, and is only different from other relative relocations in terms of
1643 // the r_offset field. By sorting relocations by offset, grouping them by
1644 // r_info and representing each relocation with only the delta from the
1645 // previous offset, each 8-byte relocation can be compressed to as little as 1
1646 // byte (or less with run-length encoding). This relocation packer was able to
1647 // reduce the size of the relocation section in an Android Chromium DSO from
1648 // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1649 //
1650 // A relocation section consists of a header containing the literal bytes
1651 // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1652 // elements are the total number of relocations in the section and an initial
1653 // r_offset value. The remaining elements define a sequence of relocation
1654 // groups. Each relocation group starts with a header consisting of the
1655 // following elements:
1656 //
1657 // - the number of relocations in the relocation group
1658 // - flags for the relocation group
1659 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1660 // for each relocation in the group.
1661 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1662 // field for each relocation in the group.
1663 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1664 // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1665 // each relocation in the group.
1666 //
1667 // Following the relocation group header are descriptions of each of the
1668 // relocations in the group. They consist of the following elements:
1669 //
1670 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1671 // delta for this relocation.
1672 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1673 // field for this relocation.
1674 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1675 // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1676 // this relocation.
1677
1678 size_t oldSize = relocData.size();
1679
1680 relocData = {'A', 'P', 'S', '2'};
1681 raw_svector_ostream os(relocData);
1682 auto add = [&](int64_t v) { encodeSLEB128(Value: v, OS&: os); };
1683
1684 // The format header includes the number of relocations and the initial
1685 // offset (we set this to zero because the first relocation group will
1686 // perform the initial adjustment).
1687 add(relocs.size());
1688 add(0);
1689
1690 std::vector<Elf_Rela> relatives, nonRelatives;
1691
1692 for (const DynamicReloc &rel : relocs) {
1693 Elf_Rela r;
1694 r.r_offset = rel.getOffset();
1695 r.setSymbolAndType(rel.getSymIndex(symTab: getPartition(ctx).dynSymTab.get()),
1696 rel.type, false);
1697 r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
1698
1699 if (r.getType(ctx.arg.isMips64EL) == ctx.target->relativeRel)
1700 relatives.push_back(r);
1701 else
1702 nonRelatives.push_back(r);
1703 }
1704
1705 llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1706 return a.r_offset < b.r_offset;
1707 });
1708
1709 // Try to find groups of relative relocations which are spaced one word
1710 // apart from one another. These generally correspond to vtable entries. The
1711 // format allows these groups to be encoded using a sort of run-length
1712 // encoding, but each group will cost 7 bytes in addition to the offset from
1713 // the previous group, so it is only profitable to do this for groups of
1714 // size 8 or larger.
1715 std::vector<Elf_Rela> ungroupedRelatives;
1716 std::vector<std::vector<Elf_Rela>> relativeGroups;
1717 for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1718 std::vector<Elf_Rela> group;
1719 do {
1720 group.push_back(*i++);
1721 } while (i != e && (i - 1)->r_offset + ctx.arg.wordsize == i->r_offset);
1722
1723 if (group.size() < 8)
1724 ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1725 group.end());
1726 else
1727 relativeGroups.emplace_back(std::move(group));
1728 }
1729
1730 // For non-relative relocations, we would like to:
1731 // 1. Have relocations with the same symbol offset to be consecutive, so
1732 // that the runtime linker can speed-up symbol lookup by implementing an
1733 // 1-entry cache.
1734 // 2. Group relocations by r_info to reduce the size of the relocation
1735 // section.
1736 // Since the symbol offset is the high bits in r_info, sorting by r_info
1737 // allows us to do both.
1738 //
1739 // For Rela, we also want to sort by r_addend when r_info is the same. This
1740 // enables us to group by r_addend as well.
1741 llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1742 return std::tie(a.r_info, a.r_addend, a.r_offset) <
1743 std::tie(b.r_info, b.r_addend, b.r_offset);
1744 });
1745
1746 // Group relocations with the same r_info. Note that each group emits a group
1747 // header and that may make the relocation section larger. It is hard to
1748 // estimate the size of a group header as the encoded size of that varies
1749 // based on r_info. However, we can approximate this trade-off by the number
1750 // of values encoded. Each group header contains 3 values, and each relocation
1751 // in a group encodes one less value, as compared to when it is not grouped.
1752 // Therefore, we only group relocations if there are 3 or more of them with
1753 // the same r_info.
1754 //
1755 // For Rela, the addend for most non-relative relocations is zero, and thus we
1756 // can usually get a smaller relocation section if we group relocations with 0
1757 // addend as well.
1758 std::vector<Elf_Rela> ungroupedNonRelatives;
1759 std::vector<std::vector<Elf_Rela>> nonRelativeGroups;
1760 for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) {
1761 auto j = i + 1;
1762 while (j != e && i->r_info == j->r_info &&
1763 (!ctx.arg.isRela || i->r_addend == j->r_addend))
1764 ++j;
1765 if (j - i < 3 || (ctx.arg.isRela && i->r_addend != 0))
1766 ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j);
1767 else
1768 nonRelativeGroups.emplace_back(i, j);
1769 i = j;
1770 }
1771
1772 // Sort ungrouped relocations by offset to minimize the encoded length.
1773 llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1774 return a.r_offset < b.r_offset;
1775 });
1776
1777 unsigned hasAddendIfRela =
1778 ctx.arg.isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0;
1779
1780 uint64_t offset = 0;
1781 uint64_t addend = 0;
1782
1783 // Emit the run-length encoding for the groups of adjacent relative
1784 // relocations. Each group is represented using two groups in the packed
1785 // format. The first is used to set the current offset to the start of the
1786 // group (and also encodes the first relocation), and the second encodes the
1787 // remaining relocations.
1788 for (std::vector<Elf_Rela> &g : relativeGroups) {
1789 // The first relocation in the group.
1790 add(1);
1791 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1792 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1793 add(g[0].r_offset - offset);
1794 add(ctx.target->relativeRel);
1795 if (ctx.arg.isRela) {
1796 add(g[0].r_addend - addend);
1797 addend = g[0].r_addend;
1798 }
1799
1800 // The remaining relocations.
1801 add(g.size() - 1);
1802 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1803 RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1804 add(ctx.arg.wordsize);
1805 add(ctx.target->relativeRel);
1806 if (ctx.arg.isRela) {
1807 for (const auto &i : llvm::drop_begin(g)) {
1808 add(i.r_addend - addend);
1809 addend = i.r_addend;
1810 }
1811 }
1812
1813 offset = g.back().r_offset;
1814 }
1815
1816 // Now the ungrouped relatives.
1817 if (!ungroupedRelatives.empty()) {
1818 add(ungroupedRelatives.size());
1819 add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1820 add(ctx.target->relativeRel);
1821 for (Elf_Rela &r : ungroupedRelatives) {
1822 add(r.r_offset - offset);
1823 offset = r.r_offset;
1824 if (ctx.arg.isRela) {
1825 add(r.r_addend - addend);
1826 addend = r.r_addend;
1827 }
1828 }
1829 }
1830
1831 // Grouped non-relatives.
1832 for (ArrayRef<Elf_Rela> g : nonRelativeGroups) {
1833 add(g.size());
1834 add(RELOCATION_GROUPED_BY_INFO_FLAG);
1835 add(g[0].r_info);
1836 for (const Elf_Rela &r : g) {
1837 add(r.r_offset - offset);
1838 offset = r.r_offset;
1839 }
1840 addend = 0;
1841 }
1842
1843 // Finally the ungrouped non-relative relocations.
1844 if (!ungroupedNonRelatives.empty()) {
1845 add(ungroupedNonRelatives.size());
1846 add(hasAddendIfRela);
1847 for (Elf_Rela &r : ungroupedNonRelatives) {
1848 add(r.r_offset - offset);
1849 offset = r.r_offset;
1850 add(r.r_info);
1851 if (ctx.arg.isRela) {
1852 add(r.r_addend - addend);
1853 addend = r.r_addend;
1854 }
1855 }
1856 }
1857
1858 // Don't allow the section to shrink; otherwise the size of the section can
1859 // oscillate infinitely.
1860 if (relocData.size() < oldSize)
1861 relocData.append(NumInputs: oldSize - relocData.size(), Elt: 0);
1862
1863 // Returns whether the section size changed. We need to keep recomputing both
1864 // section layout and the contents of this section until the size converges
1865 // because changing this section's size can affect section layout, which in
1866 // turn can affect the sizes of the LEB-encoded integers stored in this
1867 // section.
1868 return relocData.size() != oldSize;
1869}
1870
1871template <class ELFT>
1872RelrSection<ELFT>::RelrSection(Ctx &ctx, unsigned concurrency,
1873 bool isAArch64Auth)
1874 : RelrBaseSection(ctx, concurrency, isAArch64Auth) {
1875 this->entsize = ctx.arg.wordsize;
1876}
1877
1878template <class ELFT> bool RelrSection<ELFT>::updateAllocSize(Ctx &ctx) {
1879 // This function computes the contents of an SHT_RELR packed relocation
1880 // section.
1881 //
1882 // Proposal for adding SHT_RELR sections to generic-abi is here:
1883 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1884 //
1885 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1886 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1887 //
1888 // i.e. start with an address, followed by any number of bitmaps. The address
1889 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1890 // relocations each, at subsequent offsets following the last address entry.
1891 //
1892 // The bitmap entries must have 1 in the least significant bit. The assumption
1893 // here is that an address cannot have 1 in lsb. Odd addresses are not
1894 // supported.
1895 //
1896 // Excluding the least significant bit in the bitmap, each non-zero bit in
1897 // the bitmap represents a relocation to be applied to a corresponding machine
1898 // word that follows the base address word. The second least significant bit
1899 // represents the machine word immediately following the initial address, and
1900 // each bit that follows represents the next word, in linear order. As such,
1901 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1902 // 63 relocations in a 64-bit object.
1903 //
1904 // This encoding has a couple of interesting properties:
1905 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1906 // even means address, odd means bitmap.
1907 // 2. Just a simple list of addresses is a valid encoding.
1908
1909 size_t oldSize = relrRelocs.size();
1910 relrRelocs.clear();
1911
1912 const size_t wordsize = sizeof(typename ELFT::uint);
1913
1914 // Number of bits to use for the relocation offsets bitmap.
1915 // Must be either 63 or 31.
1916 const size_t nBits = wordsize * 8 - 1;
1917
1918 // Get offsets for all relative relocations and sort them.
1919 std::unique_ptr<uint64_t[]> offsets(new uint64_t[relocs.size()]);
1920 for (auto [i, r] : llvm::enumerate(relocs))
1921 offsets[i] = r.getOffset();
1922 llvm::sort(offsets.get(), offsets.get() + relocs.size());
1923
1924 // For each leading relocation, find following ones that can be folded
1925 // as a bitmap and fold them.
1926 for (size_t i = 0, e = relocs.size(); i != e;) {
1927 // Add a leading relocation.
1928 relrRelocs.push_back(Elf_Relr(offsets[i]));
1929 uint64_t base = offsets[i] + wordsize;
1930 ++i;
1931
1932 // Find foldable relocations to construct bitmaps.
1933 for (;;) {
1934 uint64_t bitmap = 0;
1935 for (; i != e; ++i) {
1936 uint64_t d = offsets[i] - base;
1937 if (d >= nBits * wordsize || d % wordsize)
1938 break;
1939 bitmap |= uint64_t(1) << (d / wordsize);
1940 }
1941 if (!bitmap)
1942 break;
1943 relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
1944 base += nBits * wordsize;
1945 }
1946 }
1947
1948 // Don't allow the section to shrink; otherwise the size of the section can
1949 // oscillate infinitely. Trailing 1s do not decode to more relocations.
1950 if (relrRelocs.size() < oldSize) {
1951 Log(ctx) << ".relr.dyn needs " << (oldSize - relrRelocs.size())
1952 << " padding word(s)";
1953 relrRelocs.resize(oldSize, Elf_Relr(1));
1954 }
1955
1956 return relrRelocs.size() != oldSize;
1957}
1958
1959SymbolTableBaseSection::SymbolTableBaseSection(Ctx &ctx,
1960 StringTableSection &strTabSec)
1961 : SyntheticSection(ctx, strTabSec.isDynamic() ? ".dynsym" : ".symtab",
1962 strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1963 strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1964 ctx.arg.wordsize),
1965 strTabSec(strTabSec) {}
1966
1967// Orders symbols according to their positions in the GOT,
1968// in compliance with MIPS ABI rules.
1969// See "Global Offset Table" in Chapter 5 in the following document
1970// for detailed description:
1971// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1972static void sortMipsSymbols(Ctx &ctx, SmallVector<SymbolTableEntry, 0> &syms) {
1973 llvm::stable_sort(Range&: syms,
1974 C: [&](const SymbolTableEntry &l, const SymbolTableEntry &r) {
1975 // Sort entries related to non-local preemptible symbols
1976 // by GOT indexes. All other entries go to the beginning
1977 // of a dynsym in arbitrary order.
1978 if (l.sym->isInGot(ctx) && r.sym->isInGot(ctx))
1979 return l.sym->getGotIdx(ctx) < r.sym->getGotIdx(ctx);
1980 if (!l.sym->isInGot(ctx) && !r.sym->isInGot(ctx))
1981 return false;
1982 return !l.sym->isInGot(ctx);
1983 });
1984}
1985
1986void SymbolTableBaseSection::finalizeContents() {
1987 if (OutputSection *sec = strTabSec.getParent())
1988 getParent()->link = sec->sectionIndex;
1989
1990 if (this->type != SHT_DYNSYM) {
1991 sortSymTabSymbols();
1992 return;
1993 }
1994
1995 // If it is a .dynsym, there should be no local symbols, but we need
1996 // to do a few things for the dynamic linker.
1997
1998 // Section's Info field has the index of the first non-local symbol.
1999 // Because the first symbol entry is a null entry, 1 is the first.
2000 getParent()->info = 1;
2001
2002 if (getPartition(ctx).gnuHashTab) {
2003 // NB: It also sorts Symbols to meet the GNU hash table requirements.
2004 getPartition(ctx).gnuHashTab->addSymbols(symbols);
2005 } else if (ctx.arg.emachine == EM_MIPS) {
2006 sortMipsSymbols(ctx, syms&: symbols);
2007 }
2008
2009 // Only the main partition's dynsym indexes are stored in the symbols
2010 // themselves. All other partitions use a lookup table.
2011 if (this == ctx.mainPart->dynSymTab.get()) {
2012 size_t i = 0;
2013 for (const SymbolTableEntry &s : symbols)
2014 s.sym->dynsymIndex = ++i;
2015 }
2016}
2017
2018// The ELF spec requires that all local symbols precede global symbols, so we
2019// sort symbol entries in this function. (For .dynsym, we don't do that because
2020// symbols for dynamic linking are inherently all globals.)
2021//
2022// Aside from above, we put local symbols in groups starting with the STT_FILE
2023// symbol. That is convenient for purpose of identifying where are local symbols
2024// coming from.
2025void SymbolTableBaseSection::sortSymTabSymbols() {
2026 // Move all local symbols before global symbols.
2027 auto e = std::stable_partition(
2028 first: symbols.begin(), last: symbols.end(),
2029 pred: [](const SymbolTableEntry &s) { return s.sym->isLocal(); });
2030 size_t numLocals = e - symbols.begin();
2031 getParent()->info = numLocals + 1;
2032
2033 // We want to group the local symbols by file. For that we rebuild the local
2034 // part of the symbols vector. We do not need to care about the STT_FILE
2035 // symbols, they are already naturally placed first in each group. That
2036 // happens because STT_FILE is always the first symbol in the object and hence
2037 // precede all other local symbols we add for a file.
2038 MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr;
2039 for (const SymbolTableEntry &s : llvm::make_range(x: symbols.begin(), y: e))
2040 arr[s.sym->file].push_back(Elt: s);
2041
2042 auto i = symbols.begin();
2043 for (auto &p : arr)
2044 for (SymbolTableEntry &entry : p.second)
2045 *i++ = entry;
2046}
2047
2048void SymbolTableBaseSection::addSymbol(Symbol *b) {
2049 // Adding a local symbol to a .dynsym is a bug.
2050 assert(this->type != SHT_DYNSYM || !b->isLocal());
2051 symbols.push_back(Elt: {.sym: b, .strTabOffset: strTabSec.addString(s: b->getName(), hashIt: false)});
2052}
2053
2054size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {
2055 if (this == ctx.mainPart->dynSymTab.get())
2056 return sym.dynsymIndex;
2057
2058 // Initializes symbol lookup tables lazily. This is used only for -r,
2059 // --emit-relocs and dynsyms in partitions other than the main one.
2060 llvm::call_once(flag&: onceFlag, F: [&] {
2061 symbolIndexMap.reserve(NumEntries: symbols.size());
2062 size_t i = 0;
2063 for (const SymbolTableEntry &e : symbols) {
2064 if (e.sym->type == STT_SECTION)
2065 sectionIndexMap[e.sym->getOutputSection()] = ++i;
2066 else
2067 symbolIndexMap[e.sym] = ++i;
2068 }
2069 });
2070
2071 // Section symbols are mapped based on their output sections
2072 // to maintain their semantics.
2073 if (sym.type == STT_SECTION)
2074 return sectionIndexMap.lookup(Val: sym.getOutputSection());
2075 return symbolIndexMap.lookup(Val: &sym);
2076}
2077
2078template <class ELFT>
2079SymbolTableSection<ELFT>::SymbolTableSection(Ctx &ctx,
2080 StringTableSection &strTabSec)
2081 : SymbolTableBaseSection(ctx, strTabSec) {
2082 this->entsize = sizeof(Elf_Sym);
2083}
2084
2085static BssSection *getCommonSec(bool relocatable, Symbol *sym) {
2086 if (relocatable)
2087 if (auto *d = dyn_cast<Defined>(Val: sym))
2088 return dyn_cast_or_null<BssSection>(Val: d->section);
2089 return nullptr;
2090}
2091
2092static uint32_t getSymSectionIndex(Symbol *sym) {
2093 assert(!(sym->hasFlag(NEEDS_COPY) && sym->isObject()));
2094 if (!isa<Defined>(Val: sym) || sym->hasFlag(bit: NEEDS_COPY))
2095 return SHN_UNDEF;
2096 if (const OutputSection *os = sym->getOutputSection())
2097 return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX
2098 : os->sectionIndex;
2099 return SHN_ABS;
2100}
2101
2102// Write the internal symbol table contents to the output symbol table.
2103template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2104 // The first entry is a null entry as per the ELF spec.
2105 buf += sizeof(Elf_Sym);
2106
2107 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2108 bool relocatable = ctx.arg.relocatable;
2109 for (SymbolTableEntry &ent : symbols) {
2110 Symbol *sym = ent.sym;
2111 bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
2112
2113 // Set st_name, st_info and st_other.
2114 eSym->st_name = ent.strTabOffset;
2115 eSym->setBindingAndType(sym->binding, sym->type);
2116 eSym->st_other = sym->stOther;
2117
2118 if (BssSection *commonSec = getCommonSec(relocatable, sym)) {
2119 // When -r is specified, a COMMON symbol is not allocated. Its st_shndx
2120 // holds SHN_COMMON and st_value holds the alignment.
2121 eSym->st_shndx = SHN_COMMON;
2122 eSym->st_value = commonSec->addralign;
2123 eSym->st_size = cast<Defined>(Val: sym)->size;
2124 } else {
2125 const uint32_t shndx = getSymSectionIndex(sym);
2126 if (isDefinedHere) {
2127 eSym->st_shndx = shndx;
2128 eSym->st_value = sym->getVA(ctx);
2129 // Copy symbol size if it is a defined symbol. st_size is not
2130 // significant for undefined symbols, so whether copying it or not is up
2131 // to us if that's the case. We'll leave it as zero because by not
2132 // setting a value, we can get the exact same outputs for two sets of
2133 // input files that differ only in undefined symbol size in DSOs.
2134 eSym->st_size = shndx != SHN_UNDEF ? cast<Defined>(Val: sym)->size : 0;
2135 } else {
2136 eSym->st_shndx = 0;
2137 eSym->st_value = 0;
2138 eSym->st_size = 0;
2139 }
2140 }
2141
2142 ++eSym;
2143 }
2144
2145 // On MIPS we need to mark symbol which has a PLT entry and requires
2146 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2147 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2148 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2149 if (ctx.arg.emachine == EM_MIPS) {
2150 auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2151
2152 for (SymbolTableEntry &ent : symbols) {
2153 Symbol *sym = ent.sym;
2154 if (sym->isInPlt(ctx) && sym->hasFlag(bit: NEEDS_COPY))
2155 eSym->st_other |= STO_MIPS_PLT;
2156 if (isMicroMips(ctx)) {
2157 // We already set the less-significant bit for symbols
2158 // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2159 // records. That allows us to distinguish such symbols in
2160 // the `MIPS<ELFT>::relocate()` routine. Now we should
2161 // clear that bit for non-dynamic symbol table, so tools
2162 // like `objdump` will be able to deal with a correct
2163 // symbol position.
2164 if (sym->isDefined() &&
2165 ((sym->stOther & STO_MIPS_MICROMIPS) || sym->hasFlag(bit: NEEDS_COPY))) {
2166 if (!strTabSec.isDynamic())
2167 eSym->st_value &= ~1;
2168 eSym->st_other |= STO_MIPS_MICROMIPS;
2169 }
2170 }
2171 if (ctx.arg.relocatable)
2172 if (auto *d = dyn_cast<Defined>(Val: sym))
2173 if (isMipsPIC<ELFT>(d))
2174 eSym->st_other |= STO_MIPS_PIC;
2175 ++eSym;
2176 }
2177 }
2178}
2179
2180SymtabShndxSection::SymtabShndxSection(Ctx &ctx)
2181 : SyntheticSection(ctx, ".symtab_shndx", SHT_SYMTAB_SHNDX, 0, 4) {
2182 this->entsize = 4;
2183}
2184
2185void SymtabShndxSection::writeTo(uint8_t *buf) {
2186 // We write an array of 32 bit values, where each value has 1:1 association
2187 // with an entry in ctx.in.symTab if the corresponding entry contains
2188 // SHN_XINDEX, we need to write actual index, otherwise, we must write
2189 // SHN_UNDEF(0).
2190 buf += 4; // Ignore .symtab[0] entry.
2191 bool relocatable = ctx.arg.relocatable;
2192 for (const SymbolTableEntry &entry : ctx.in.symTab->getSymbols()) {
2193 if (!getCommonSec(relocatable, sym: entry.sym) &&
2194 getSymSectionIndex(sym: entry.sym) == SHN_XINDEX)
2195 write32(ctx, p: buf, v: entry.sym->getOutputSection()->sectionIndex);
2196 buf += 4;
2197 }
2198}
2199
2200bool SymtabShndxSection::isNeeded() const {
2201 // SHT_SYMTAB can hold symbols with section indices values up to
2202 // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
2203 // section. Problem is that we reveal the final section indices a bit too
2204 // late, and we do not know them here. For simplicity, we just always create
2205 // a .symtab_shndx section when the amount of output sections is huge.
2206 size_t size = 0;
2207 for (SectionCommand *cmd : ctx.script->sectionCommands)
2208 if (isa<OutputDesc>(Val: cmd))
2209 ++size;
2210 return size >= SHN_LORESERVE;
2211}
2212
2213void SymtabShndxSection::finalizeContents() {
2214 getParent()->link = ctx.in.symTab->getParent()->sectionIndex;
2215}
2216
2217size_t SymtabShndxSection::getSize() const {
2218 return ctx.in.symTab->getNumSymbols() * 4;
2219}
2220
2221// .hash and .gnu.hash sections contain on-disk hash tables that map
2222// symbol names to their dynamic symbol table indices. Their purpose
2223// is to help the dynamic linker resolve symbols quickly. If ELF files
2224// don't have them, the dynamic linker has to do linear search on all
2225// dynamic symbols, which makes programs slower. Therefore, a .hash
2226// section is added to a DSO by default.
2227//
2228// The Unix semantics of resolving dynamic symbols is somewhat expensive.
2229// Each ELF file has a list of DSOs that the ELF file depends on and a
2230// list of dynamic symbols that need to be resolved from any of the
2231// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
2232// where m is the number of DSOs and n is the number of dynamic
2233// symbols. For modern large programs, both m and n are large. So
2234// making each step faster by using hash tables substantially
2235// improves time to load programs.
2236//
2237// (Note that this is not the only way to design the shared library.
2238// For instance, the Windows DLL takes a different approach. On
2239// Windows, each dynamic symbol has a name of DLL from which the symbol
2240// has to be resolved. That makes the cost of symbol resolution O(n).
2241// This disables some hacky techniques you can use on Unix such as
2242// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
2243//
2244// Due to historical reasons, we have two different hash tables, .hash
2245// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
2246// and better version of .hash. .hash is just an on-disk hash table, but
2247// .gnu.hash has a bloom filter in addition to a hash table to skip
2248// DSOs very quickly. If you are sure that your dynamic linker knows
2249// about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a
2250// safe bet is to specify --hash-style=both for backward compatibility.
2251GnuHashTableSection::GnuHashTableSection(Ctx &ctx)
2252 : SyntheticSection(ctx, ".gnu.hash", SHT_GNU_HASH, SHF_ALLOC,
2253 ctx.arg.wordsize) {}
2254
2255void GnuHashTableSection::finalizeContents() {
2256 if (OutputSection *sec = getPartition(ctx).dynSymTab->getParent())
2257 getParent()->link = sec->sectionIndex;
2258
2259 // Computes bloom filter size in word size. We want to allocate 12
2260 // bits for each symbol. It must be a power of two.
2261 if (symbols.empty()) {
2262 maskWords = 1;
2263 } else {
2264 uint64_t numBits = symbols.size() * 12;
2265 maskWords = NextPowerOf2(A: numBits / (ctx.arg.wordsize * 8));
2266 }
2267
2268 size = 16; // Header
2269 size += ctx.arg.wordsize * maskWords; // Bloom filter
2270 size += nBuckets * 4; // Hash buckets
2271 size += symbols.size() * 4; // Hash values
2272}
2273
2274void GnuHashTableSection::writeTo(uint8_t *buf) {
2275 // Write a header.
2276 write32(ctx, p: buf, v: nBuckets);
2277 write32(ctx, p: buf + 4,
2278 v: getPartition(ctx).dynSymTab->getNumSymbols() - symbols.size());
2279 write32(ctx, p: buf + 8, v: maskWords);
2280 write32(ctx, p: buf + 12, v: Shift2);
2281 buf += 16;
2282
2283 // Write the 2-bit bloom filter.
2284 const unsigned c = ctx.arg.is64 ? 64 : 32;
2285 for (const Entry &sym : symbols) {
2286 // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in
2287 // the word using bits [0:5] and [26:31].
2288 size_t i = (sym.hash / c) & (maskWords - 1);
2289 uint64_t val = readUint(ctx, buf: buf + i * ctx.arg.wordsize);
2290 val |= uint64_t(1) << (sym.hash % c);
2291 val |= uint64_t(1) << ((sym.hash >> Shift2) % c);
2292 writeUint(ctx, buf: buf + i * ctx.arg.wordsize, val);
2293 }
2294 buf += ctx.arg.wordsize * maskWords;
2295
2296 // Write the hash table.
2297 uint32_t *buckets = reinterpret_cast<uint32_t *>(buf);
2298 uint32_t oldBucket = -1;
2299 uint32_t *values = buckets + nBuckets;
2300 for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) {
2301 // Write a hash value. It represents a sequence of chains that share the
2302 // same hash modulo value. The last element of each chain is terminated by
2303 // LSB 1.
2304 uint32_t hash = i->hash;
2305 bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx;
2306 hash = isLastInChain ? hash | 1 : hash & ~1;
2307 write32(ctx, p: values++, v: hash);
2308
2309 if (i->bucketIdx == oldBucket)
2310 continue;
2311 // Write a hash bucket. Hash buckets contain indices in the following hash
2312 // value table.
2313 write32(ctx, p: buckets + i->bucketIdx,
2314 v: getPartition(ctx).dynSymTab->getSymbolIndex(sym: *i->sym));
2315 oldBucket = i->bucketIdx;
2316 }
2317}
2318
2319// Add symbols to this symbol hash table. Note that this function
2320// destructively sort a given vector -- which is needed because
2321// GNU-style hash table places some sorting requirements.
2322void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) {
2323 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
2324 // its type correctly.
2325 auto mid =
2326 std::stable_partition(first: v.begin(), last: v.end(), pred: [&](const SymbolTableEntry &s) {
2327 return !s.sym->isDefined() || s.sym->partition != partition;
2328 });
2329
2330 // We chose load factor 4 for the on-disk hash table. For each hash
2331 // collision, the dynamic linker will compare a uint32_t hash value.
2332 // Since the integer comparison is quite fast, we believe we can
2333 // make the load factor even larger. 4 is just a conservative choice.
2334 //
2335 // Note that we don't want to create a zero-sized hash table because
2336 // Android loader as of 2018 doesn't like a .gnu.hash containing such
2337 // table. If that's the case, we create a hash table with one unused
2338 // dummy slot.
2339 nBuckets = std::max<size_t>(a: (v.end() - mid) / 4, b: 1);
2340
2341 if (mid == v.end())
2342 return;
2343
2344 for (SymbolTableEntry &ent : llvm::make_range(x: mid, y: v.end())) {
2345 Symbol *b = ent.sym;
2346 uint32_t hash = hashGnu(Name: b->getName());
2347 uint32_t bucketIdx = hash % nBuckets;
2348 symbols.push_back(Elt: {.sym: b, .strTabOffset: ent.strTabOffset, .hash: hash, .bucketIdx: bucketIdx});
2349 }
2350
2351 llvm::sort(C&: symbols, Comp: [](const Entry &l, const Entry &r) {
2352 return std::tie(args: l.bucketIdx, args: l.strTabOffset) <
2353 std::tie(args: r.bucketIdx, args: r.strTabOffset);
2354 });
2355
2356 v.erase(CS: mid, CE: v.end());
2357 for (const Entry &ent : symbols)
2358 v.push_back(Elt: {.sym: ent.sym, .strTabOffset: ent.strTabOffset});
2359}
2360
2361HashTableSection::HashTableSection(Ctx &ctx)
2362 : SyntheticSection(ctx, ".hash", SHT_HASH, SHF_ALLOC, 4) {
2363 this->entsize = 4;
2364}
2365
2366void HashTableSection::finalizeContents() {
2367 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2368
2369 if (OutputSection *sec = symTab->getParent())
2370 getParent()->link = sec->sectionIndex;
2371
2372 unsigned numEntries = 2; // nbucket and nchain.
2373 numEntries += symTab->getNumSymbols(); // The chain entries.
2374
2375 // Create as many buckets as there are symbols.
2376 numEntries += symTab->getNumSymbols();
2377 this->size = numEntries * 4;
2378}
2379
2380void HashTableSection::writeTo(uint8_t *buf) {
2381 SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
2382 unsigned numSymbols = symTab->getNumSymbols();
2383
2384 uint32_t *p = reinterpret_cast<uint32_t *>(buf);
2385 write32(ctx, p: p++, v: numSymbols); // nbucket
2386 write32(ctx, p: p++, v: numSymbols); // nchain
2387
2388 uint32_t *buckets = p;
2389 uint32_t *chains = p + numSymbols;
2390
2391 for (const SymbolTableEntry &s : symTab->getSymbols()) {
2392 Symbol *sym = s.sym;
2393 StringRef name = sym->getName();
2394 unsigned i = sym->dynsymIndex;
2395 uint32_t hash = hashSysV(SymbolName: name) % numSymbols;
2396 chains[i] = buckets[hash];
2397 write32(ctx, p: buckets + hash, v: i);
2398 }
2399}
2400
2401PltSection::PltSection(Ctx &ctx)
2402 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2403 16),
2404 headerSize(ctx.target->pltHeaderSize) {
2405 // On AArch64, PLT entries only do loads from the .got.plt section, so the
2406 // .plt section can be marked with the SHF_AARCH64_PURECODE section flag.
2407 if (ctx.arg.emachine == EM_AARCH64)
2408 this->flags |= SHF_AARCH64_PURECODE;
2409
2410 // On PowerPC, this section contains lazy symbol resolvers.
2411 if (ctx.arg.emachine == EM_PPC64) {
2412 name = ".glink";
2413 addralign = 4;
2414 }
2415
2416 // On x86 when IBT is enabled, this section contains the second PLT (lazy
2417 // symbol resolvers).
2418 if ((ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) &&
2419 (ctx.arg.andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT))
2420 name = ".plt.sec";
2421
2422 // The PLT needs to be writable on SPARC as the dynamic linker will
2423 // modify the instructions in the PLT entries.
2424 if (ctx.arg.emachine == EM_SPARCV9)
2425 this->flags |= SHF_WRITE;
2426}
2427
2428void PltSection::writeTo(uint8_t *buf) {
2429 // At beginning of PLT, we have code to call the dynamic
2430 // linker to resolve dynsyms at runtime. Write such code.
2431 ctx.target->writePltHeader(buf);
2432 size_t off = headerSize;
2433
2434 for (const Symbol *sym : entries) {
2435 ctx.target->writePlt(buf: buf + off, sym: *sym, pltEntryAddr: getVA() + off);
2436 off += ctx.target->pltEntrySize;
2437 }
2438}
2439
2440void PltSection::addEntry(Symbol &sym) {
2441 assert(sym.auxIdx == ctx.symAux.size() - 1);
2442 ctx.symAux.back().pltIdx = entries.size();
2443 entries.push_back(Elt: &sym);
2444}
2445
2446size_t PltSection::getSize() const {
2447 return headerSize + entries.size() * ctx.target->pltEntrySize;
2448}
2449
2450bool PltSection::isNeeded() const {
2451 // For -z retpolineplt, .iplt needs the .plt header.
2452 return !entries.empty() || (ctx.arg.zRetpolineplt && ctx.in.iplt->isNeeded());
2453}
2454
2455// Used by ARM to add mapping symbols in the PLT section, which aid
2456// disassembly.
2457void PltSection::addSymbols() {
2458 ctx.target->addPltHeaderSymbols(isec&: *this);
2459
2460 size_t off = headerSize;
2461 for (size_t i = 0; i < entries.size(); ++i) {
2462 ctx.target->addPltSymbols(isec&: *this, off);
2463 off += ctx.target->pltEntrySize;
2464 }
2465}
2466
2467IpltSection::IpltSection(Ctx &ctx)
2468 : SyntheticSection(ctx, ".iplt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2469 16) {
2470 // On AArch64, PLT entries only do loads from the .got.plt section, so the
2471 // .iplt section can be marked with the SHF_AARCH64_PURECODE section flag.
2472 if (ctx.arg.emachine == EM_AARCH64)
2473 this->flags |= SHF_AARCH64_PURECODE;
2474
2475 if (ctx.arg.emachine == EM_PPC || ctx.arg.emachine == EM_PPC64) {
2476 name = ".glink";
2477 addralign = 4;
2478 }
2479}
2480
2481void IpltSection::writeTo(uint8_t *buf) {
2482 uint32_t off = 0;
2483 for (const Symbol *sym : entries) {
2484 ctx.target->writeIplt(buf: buf + off, sym: *sym, pltEntryAddr: getVA() + off);
2485 off += ctx.target->ipltEntrySize;
2486 }
2487}
2488
2489size_t IpltSection::getSize() const {
2490 return entries.size() * ctx.target->ipltEntrySize;
2491}
2492
2493void IpltSection::addEntry(Symbol &sym) {
2494 assert(sym.auxIdx == ctx.symAux.size() - 1);
2495 ctx.symAux.back().pltIdx = entries.size();
2496 entries.push_back(Elt: &sym);
2497}
2498
2499// ARM uses mapping symbols to aid disassembly.
2500void IpltSection::addSymbols() {
2501 size_t off = 0;
2502 for (size_t i = 0, e = entries.size(); i != e; ++i) {
2503 ctx.target->addPltSymbols(isec&: *this, off);
2504 off += ctx.target->pltEntrySize;
2505 }
2506}
2507
2508PPC32GlinkSection::PPC32GlinkSection(Ctx &ctx) : PltSection(ctx) {
2509 name = ".glink";
2510 addralign = 4;
2511}
2512
2513void PPC32GlinkSection::writeTo(uint8_t *buf) {
2514 writePPC32GlinkSection(ctx, buf, numEntries: entries.size());
2515}
2516
2517size_t PPC32GlinkSection::getSize() const {
2518 return headerSize + entries.size() * ctx.target->pltEntrySize + footerSize;
2519}
2520
2521// This is an x86-only extra PLT section and used only when a security
2522// enhancement feature called CET is enabled. In this comment, I'll explain what
2523// the feature is and why we have two PLT sections if CET is enabled.
2524//
2525// So, what does CET do? CET introduces a new restriction to indirect jump
2526// instructions. CET works this way. Assume that CET is enabled. Then, if you
2527// execute an indirect jump instruction, the processor verifies that a special
2528// "landing pad" instruction (which is actually a repurposed NOP instruction and
2529// now called "endbr32" or "endbr64") is at the jump target. If the jump target
2530// does not start with that instruction, the processor raises an exception
2531// instead of continuing executing code.
2532//
2533// If CET is enabled, the compiler emits endbr to all locations where indirect
2534// jumps may jump to.
2535//
2536// This mechanism makes it extremely hard to transfer the control to a middle of
2537// a function that is not supporsed to be a indirect jump target, preventing
2538// certain types of attacks such as ROP or JOP.
2539//
2540// Note that the processors in the market as of 2019 don't actually support the
2541// feature. Only the spec is available at the moment.
2542//
2543// Now, I'll explain why we have this extra PLT section for CET.
2544//
2545// Since you can indirectly jump to a PLT entry, we have to make PLT entries
2546// start with endbr. The problem is there's no extra space for endbr (which is 4
2547// bytes long), as the PLT entry is only 16 bytes long and all bytes are already
2548// used.
2549//
2550// In order to deal with the issue, we split a PLT entry into two PLT entries.
2551// Remember that each PLT entry contains code to jump to an address read from
2552// .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme,
2553// the former code is written to .plt.sec, and the latter code is written to
2554// .plt.
2555//
2556// Lazy symbol resolution in the 2-PLT scheme works in the usual way, except
2557// that the regular .plt is now called .plt.sec and .plt is repurposed to
2558// contain only code for lazy symbol resolution.
2559//
2560// In other words, this is how the 2-PLT scheme works. Application code is
2561// supposed to jump to .plt.sec to call an external function. Each .plt.sec
2562// entry contains code to read an address from a corresponding .got.plt entry
2563// and jump to that address. Addresses in .got.plt initially point to .plt, so
2564// when an application calls an external function for the first time, the
2565// control is transferred to a function that resolves a symbol name from
2566// external shared object files. That function then rewrites a .got.plt entry
2567// with a resolved address, so that the subsequent function calls directly jump
2568// to a desired location from .plt.sec.
2569//
2570// There is an open question as to whether the 2-PLT scheme was desirable or
2571// not. We could have simply extended the PLT entry size to 32-bytes to
2572// accommodate endbr, and that scheme would have been much simpler than the
2573// 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot
2574// code (.plt.sec) from cold code (.plt). But as far as I know no one proved
2575// that the optimization actually makes a difference.
2576//
2577// That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools
2578// depend on it, so we implement the ABI.
2579IBTPltSection::IBTPltSection(Ctx &ctx)
2580 : SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
2581 16) {}
2582
2583void IBTPltSection::writeTo(uint8_t *buf) {
2584 ctx.target->writeIBTPlt(buf, numEntries: ctx.in.plt->getNumEntries());
2585}
2586
2587size_t IBTPltSection::getSize() const {
2588 // 16 is the header size of .plt.
2589 return 16 + ctx.in.plt->getNumEntries() * ctx.target->pltEntrySize;
2590}
2591
2592bool IBTPltSection::isNeeded() const { return ctx.in.plt->getNumEntries() > 0; }
2593
2594RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
2595 : SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
2596 1) {}
2597
2598PaddingSection::PaddingSection(Ctx &ctx, uint64_t amount, OutputSection *parent)
2599 : SyntheticSection(ctx, ".padding", SHT_PROGBITS, SHF_ALLOC, 1) {
2600 size = amount;
2601 this->parent = parent;
2602}
2603
2604void PaddingSection::writeTo(uint8_t *buf) {
2605 std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
2606 uint8_t *end = buf + size;
2607 for (; buf + 4 <= end; buf += 4)
2608 memcpy(dest: buf, src: &filler[0], n: 4);
2609 memcpy(dest: buf, src: &filler[0], n: end - buf);
2610}
2611
2612// The string hash function for .gdb_index.
2613static uint32_t computeGdbHash(StringRef s) {
2614 uint32_t h = 0;
2615 for (uint8_t c : s)
2616 h = h * 67 + toLower(x: c) - 113;
2617 return h;
2618}
2619
2620// 4-byte alignment ensures that values in the hash lookup table and the name
2621// table are aligned.
2622DebugNamesBaseSection::DebugNamesBaseSection(Ctx &ctx)
2623 : SyntheticSection(ctx, ".debug_names", SHT_PROGBITS, 0, 4) {}
2624
2625// Get the size of the .debug_names section header in bytes for DWARF32:
2626static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {
2627 return /* unit length */ 4 +
2628 /* version */ 2 +
2629 /* padding */ 2 +
2630 /* CU count */ 4 +
2631 /* TU count */ 4 +
2632 /* Foreign TU count */ 4 +
2633 /* Bucket Count */ 4 +
2634 /* Name Count */ 4 +
2635 /* Abbrev table size */ 4 +
2636 /* Augmentation string size */ 4 +
2637 /* Augmentation string */ augmentationStringSize;
2638}
2639
2640static Expected<DebugNamesBaseSection::IndexEntry *>
2641readEntry(uint64_t &offset, const DWARFDebugNames::NameIndex &ni,
2642 uint64_t entriesBase, DWARFDataExtractor &namesExtractor,
2643 const LLDDWARFSection &namesSec) {
2644 auto ie = makeThreadLocal<DebugNamesBaseSection::IndexEntry>();
2645 ie->poolOffset = offset;
2646 Error err = Error::success();
2647 uint64_t ulebVal = namesExtractor.getULEB128(offset_ptr: &offset, Err: &err);
2648 if (err)
2649 return createStringError(EC: inconvertibleErrorCode(),
2650 Fmt: "invalid abbrev code: %s",
2651 Vals: llvm::toString(E: std::move(err)).c_str());
2652 if (!isUInt<32>(x: ulebVal))
2653 return createStringError(EC: inconvertibleErrorCode(),
2654 Fmt: "abbrev code too large for DWARF32: %" PRIu64,
2655 Vals: ulebVal);
2656 ie->abbrevCode = static_cast<uint32_t>(ulebVal);
2657 auto it = ni.getAbbrevs().find_as(Val: ie->abbrevCode);
2658 if (it == ni.getAbbrevs().end())
2659 return createStringError(EC: inconvertibleErrorCode(),
2660 Fmt: "abbrev code not found in abbrev table: %" PRIu32,
2661 Vals: ie->abbrevCode);
2662
2663 DebugNamesBaseSection::AttrValue attr, cuAttr = {.attrValue: 0, .attrSize: 0};
2664 for (DWARFDebugNames::AttributeEncoding a : it->Attributes) {
2665 if (a.Index == dwarf::DW_IDX_parent) {
2666 if (a.Form == dwarf::DW_FORM_ref4) {
2667 attr.attrValue = namesExtractor.getU32(offset_ptr: &offset, Err: &err);
2668 attr.attrSize = 4;
2669 ie->parentOffset = entriesBase + attr.attrValue;
2670 } else if (a.Form != DW_FORM_flag_present)
2671 return createStringError(EC: inconvertibleErrorCode(),
2672 S: "invalid form for DW_IDX_parent");
2673 } else {
2674 switch (a.Form) {
2675 case DW_FORM_data1:
2676 case DW_FORM_ref1: {
2677 attr.attrValue = namesExtractor.getU8(offset_ptr: &offset, Err: &err);
2678 attr.attrSize = 1;
2679 break;
2680 }
2681 case DW_FORM_data2:
2682 case DW_FORM_ref2: {
2683 attr.attrValue = namesExtractor.getU16(offset_ptr: &offset, Err: &err);
2684 attr.attrSize = 2;
2685 break;
2686 }
2687 case DW_FORM_data4:
2688 case DW_FORM_ref4: {
2689 attr.attrValue = namesExtractor.getU32(offset_ptr: &offset, Err: &err);
2690 attr.attrSize = 4;
2691 break;
2692 }
2693 default:
2694 return createStringError(
2695 EC: inconvertibleErrorCode(),
2696 Fmt: "unrecognized form encoding %d in abbrev table", Vals: a.Form);
2697 }
2698 }
2699 if (err)
2700 return createStringError(EC: inconvertibleErrorCode(),
2701 Fmt: "error while reading attributes: %s",
2702 Vals: llvm::toString(E: std::move(err)).c_str());
2703 if (a.Index == DW_IDX_compile_unit)
2704 cuAttr = attr;
2705 else if (a.Form != DW_FORM_flag_present)
2706 ie->attrValues.push_back(Elt: attr);
2707 }
2708 // Canonicalize abbrev by placing the CU/TU index at the end.
2709 ie->attrValues.push_back(Elt: cuAttr);
2710 return ie;
2711}
2712
2713void DebugNamesBaseSection::parseDebugNames(
2714 Ctx &ctx, InputChunk &inputChunk, OutputChunk &chunk,
2715 DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,
2716 function_ref<SmallVector<uint32_t, 0>(
2717 uint32_t numCus, const DWARFDebugNames::Header &,
2718 const DWARFDebugNames::DWARFDebugNamesOffsets &)>
2719 readOffsets) {
2720 const LLDDWARFSection &namesSec = inputChunk.section;
2721 DenseMap<uint32_t, IndexEntry *> offsetMap;
2722 // Number of CUs seen in previous NameIndex sections within current chunk.
2723 uint32_t numCus = 0;
2724 for (const DWARFDebugNames::NameIndex &ni : *inputChunk.llvmDebugNames) {
2725 NameData &nd = inputChunk.nameData.emplace_back();
2726 nd.hdr = ni.getHeader();
2727 if (nd.hdr.Format != DwarfFormat::DWARF32) {
2728 Err(ctx) << namesSec.sec
2729 << ": found DWARF64, which is currently unsupported";
2730 return;
2731 }
2732 if (nd.hdr.Version != 5) {
2733 Err(ctx) << namesSec.sec << ": unsupported version: " << nd.hdr.Version;
2734 return;
2735 }
2736 uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize(Format: DwarfFormat::DWARF32);
2737 DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();
2738 if (locs.EntriesBase > namesExtractor.getData().size()) {
2739 Err(ctx) << namesSec.sec << ": entry pool start is beyond end of section";
2740 return;
2741 }
2742
2743 SmallVector<uint32_t, 0> entryOffsets = readOffsets(numCus, nd.hdr, locs);
2744
2745 // Read the entry pool.
2746 offsetMap.clear();
2747 nd.nameEntries.resize(N: nd.hdr.NameCount);
2748 for (auto i : seq(Size: nd.hdr.NameCount)) {
2749 NameEntry &ne = nd.nameEntries[i];
2750 uint64_t strOffset = locs.StringOffsetsBase + i * dwarfSize;
2751 ne.stringOffset = strOffset;
2752 uint64_t strp = namesExtractor.getRelocatedValue(Size: dwarfSize, Off: &strOffset);
2753 StringRef name = strExtractor.getCStrRef(OffsetPtr: &strp);
2754 ne.name = name.data();
2755 ne.hashValue = caseFoldingDjbHash(Buffer: name);
2756
2757 // Read a series of index entries that end with abbreviation code 0.
2758 uint64_t offset = locs.EntriesBase + entryOffsets[i];
2759 while (offset < namesSec.Data.size() && namesSec.Data[offset] != 0) {
2760 // Read & store all entries (for the same string).
2761 Expected<IndexEntry *> ieOrErr =
2762 readEntry(offset, ni, entriesBase: locs.EntriesBase, namesExtractor, namesSec);
2763 if (!ieOrErr) {
2764 Err(ctx) << namesSec.sec << ": " << ieOrErr.takeError();
2765 return;
2766 }
2767 ne.indexEntries.push_back(Elt: std::move(*ieOrErr));
2768 }
2769 if (offset >= namesSec.Data.size())
2770 Err(ctx) << namesSec.sec << ": index entry is out of bounds";
2771
2772 for (IndexEntry &ie : ne.entries())
2773 offsetMap[ie.poolOffset] = &ie;
2774 }
2775
2776 // Assign parent pointers, which will be used to update DW_IDX_parent index
2777 // attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will
2778 // get parentEntry == null as well.
2779 for (NameEntry &ne : nd.nameEntries)
2780 for (IndexEntry &ie : ne.entries())
2781 ie.parentEntry = offsetMap.lookup(Val: ie.parentOffset);
2782 numCus += nd.hdr.CompUnitCount;
2783 }
2784}
2785
2786// Compute the form for output DW_IDX_compile_unit attributes, similar to
2787// DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all
2788// the merged CU indices.
2789std::pair<uint8_t, dwarf::Form> static getMergedCuCountForm(
2790 uint32_t compUnitCount) {
2791 if (compUnitCount > UINT16_MAX)
2792 return {4, DW_FORM_data4};
2793 if (compUnitCount > UINT8_MAX)
2794 return {2, DW_FORM_data2};
2795 return {1, DW_FORM_data1};
2796}
2797
2798void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2799 MutableArrayRef<InputChunk> inputChunks) {
2800 TimeTraceScope timeScope("Merge .debug_names", "hdr and abbrev table");
2801 size_t numCu = 0;
2802 hdr.Format = DwarfFormat::DWARF32;
2803 hdr.Version = 5;
2804 hdr.CompUnitCount = 0;
2805 hdr.LocalTypeUnitCount = 0;
2806 hdr.ForeignTypeUnitCount = 0;
2807 hdr.AugmentationStringSize = 0;
2808
2809 // Compute CU and TU counts.
2810 for (auto i : seq(Size: numChunks)) {
2811 InputChunk &inputChunk = inputChunks[i];
2812 inputChunk.baseCuIdx = numCu;
2813 numCu += chunks[i].compUnits.size();
2814 for (const NameData &nd : inputChunk.nameData) {
2815 hdr.CompUnitCount += nd.hdr.CompUnitCount;
2816 // TODO: We don't handle type units yet, so LocalTypeUnitCount &
2817 // ForeignTypeUnitCount are left as 0.
2818 if (nd.hdr.LocalTypeUnitCount || nd.hdr.ForeignTypeUnitCount)
2819 Warn(ctx) << inputChunk.section.sec
2820 << ": type units are not implemented";
2821 // If augmentation strings are not identical, use an empty string.
2822 if (i == 0) {
2823 hdr.AugmentationStringSize = nd.hdr.AugmentationStringSize;
2824 hdr.AugmentationString = nd.hdr.AugmentationString;
2825 } else if (hdr.AugmentationString != nd.hdr.AugmentationString) {
2826 // There are conflicting augmentation strings, so it's best for the
2827 // merged index to not use an augmentation string.
2828 hdr.AugmentationStringSize = 0;
2829 hdr.AugmentationString.clear();
2830 }
2831 }
2832 }
2833
2834 // Create the merged abbrev table, uniquifyinng the input abbrev tables and
2835 // computing mapping from old (per-cu) abbrev codes to new (merged) abbrev
2836 // codes.
2837 FoldingSet<Abbrev> abbrevSet;
2838 // Determine the form for the DW_IDX_compile_unit attributes in the merged
2839 // index. The input form may not be big enough for all CU indices.
2840 dwarf::Form cuAttrForm = getMergedCuCountForm(compUnitCount: hdr.CompUnitCount).second;
2841 for (InputChunk &inputChunk : inputChunks) {
2842 for (auto [i, ni] : enumerate(First&: *inputChunk.llvmDebugNames)) {
2843 for (const DWARFDebugNames::Abbrev &oldAbbrev : ni.getAbbrevs()) {
2844 // Canonicalize abbrev by placing the CU/TU index at the end,
2845 // similar to 'parseDebugNames'.
2846 Abbrev abbrev;
2847 DWARFDebugNames::AttributeEncoding cuAttr(DW_IDX_compile_unit,
2848 cuAttrForm);
2849 abbrev.code = oldAbbrev.Code;
2850 abbrev.tag = oldAbbrev.Tag;
2851 for (DWARFDebugNames::AttributeEncoding a : oldAbbrev.Attributes) {
2852 if (a.Index == DW_IDX_compile_unit)
2853 cuAttr.Index = a.Index;
2854 else
2855 abbrev.attributes.push_back(Elt: {a.Index, a.Form});
2856 }
2857 // Put the CU/TU index at the end of the attributes list.
2858 abbrev.attributes.push_back(Elt: cuAttr);
2859
2860 // Profile the abbrev, get or assign a new code, then record the abbrev
2861 // code mapping.
2862 FoldingSetNodeID id;
2863 abbrev.Profile(id);
2864 uint32_t newCode;
2865 void *insertPos;
2866 if (Abbrev *existing = abbrevSet.FindNodeOrInsertPos(ID: id, InsertPos&: insertPos)) {
2867 // Found it; we've already seen an identical abbreviation.
2868 newCode = existing->code;
2869 } else {
2870 Abbrev *abbrev2 =
2871 new (abbrevAlloc.Allocate()) Abbrev(std::move(abbrev));
2872 abbrevSet.InsertNode(N: abbrev2, InsertPos: insertPos);
2873 abbrevTable.push_back(Elt: abbrev2);
2874 newCode = abbrevTable.size();
2875 abbrev2->code = newCode;
2876 }
2877 inputChunk.nameData[i].abbrevCodeMap[oldAbbrev.Code] = newCode;
2878 }
2879 }
2880 }
2881
2882 // Compute the merged abbrev table.
2883 raw_svector_ostream os(abbrevTableBuf);
2884 for (Abbrev *abbrev : abbrevTable) {
2885 encodeULEB128(Value: abbrev->code, OS&: os);
2886 encodeULEB128(Value: abbrev->tag, OS&: os);
2887 for (DWARFDebugNames::AttributeEncoding a : abbrev->attributes) {
2888 encodeULEB128(Value: a.Index, OS&: os);
2889 encodeULEB128(Value: a.Form, OS&: os);
2890 }
2891 os.write(Ptr: "\0", Size: 2); // attribute specification end
2892 }
2893 os.write(C: 0); // abbrev table end
2894 hdr.AbbrevTableSize = abbrevTableBuf.size();
2895}
2896
2897void DebugNamesBaseSection::Abbrev::Profile(FoldingSetNodeID &id) const {
2898 id.AddInteger(I: tag);
2899 for (const DWARFDebugNames::AttributeEncoding &attr : attributes) {
2900 id.AddInteger(I: attr.Index);
2901 id.AddInteger(I: attr.Form);
2902 }
2903}
2904
2905std::pair<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(
2906 MutableArrayRef<InputChunk> inputChunks) {
2907 TimeTraceScope timeScope("Merge .debug_names", "entry pool");
2908 // Collect and de-duplicate all the names (preserving all the entries).
2909 // Speed it up using multithreading, as the number of symbols can be in the
2910 // order of millions.
2911 const size_t concurrency =
2912 bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
2913 const size_t shift = 32 - countr_zero(Val: numShards);
2914 const uint8_t cuAttrSize = getMergedCuCountForm(compUnitCount: hdr.CompUnitCount).first;
2915 DenseMap<CachedHashStringRef, size_t> maps[numShards];
2916
2917 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
2918 for (auto i : seq(Size: numChunks)) {
2919 InputChunk &inputChunk = inputChunks[i];
2920 for (auto j : seq(Size: inputChunk.nameData.size())) {
2921 NameData &nd = inputChunk.nameData[j];
2922 // Deduplicate the NameEntry records (based on the string/name),
2923 // appending all IndexEntries from duplicate NameEntry records to
2924 // the single preserved copy.
2925 for (NameEntry &ne : nd.nameEntries) {
2926 auto shardId = ne.hashValue >> shift;
2927 if ((shardId & (concurrency - 1)) != threadId)
2928 continue;
2929
2930 ne.chunkIdx = i;
2931 for (IndexEntry &ie : ne.entries()) {
2932 // Update the IndexEntry's abbrev code to match the merged
2933 // abbreviations.
2934 ie.abbrevCode = nd.abbrevCodeMap[ie.abbrevCode];
2935 // Update the DW_IDX_compile_unit attribute (the last one after
2936 // canonicalization) to have correct merged offset value and size.
2937 auto &back = ie.attrValues.back();
2938 back.attrValue += inputChunk.baseCuIdx + j;
2939 back.attrSize = cuAttrSize;
2940 }
2941
2942 auto &nameVec = nameVecs[shardId];
2943 auto [it, inserted] = maps[shardId].try_emplace(
2944 Key: CachedHashStringRef(ne.name, ne.hashValue), Args: nameVec.size());
2945 if (inserted)
2946 nameVec.push_back(Elt: std::move(ne));
2947 else
2948 nameVec[it->second].indexEntries.append(RHS: std::move(ne.indexEntries));
2949 }
2950 }
2951 }
2952 });
2953
2954 // Compute entry offsets in parallel. First, compute offsets relative to the
2955 // current shard.
2956 uint32_t offsets[numShards];
2957 parallelFor(Begin: 0, End: numShards, Fn: [&](size_t shard) {
2958 uint32_t offset = 0;
2959 for (NameEntry &ne : nameVecs[shard]) {
2960 ne.entryOffset = offset;
2961 for (IndexEntry &ie : ne.entries()) {
2962 ie.poolOffset = offset;
2963 offset += getULEB128Size(Value: ie.abbrevCode);
2964 for (AttrValue value : ie.attrValues)
2965 offset += value.attrSize;
2966 }
2967 ++offset; // index entry sentinel
2968 }
2969 offsets[shard] = offset;
2970 });
2971 // Then add shard offsets.
2972 std::partial_sum(first: offsets, last: std::end(arr&: offsets), result: offsets);
2973 parallelFor(Begin: 1, End: numShards, Fn: [&](size_t shard) {
2974 uint32_t offset = offsets[shard - 1];
2975 for (NameEntry &ne : nameVecs[shard]) {
2976 ne.entryOffset += offset;
2977 for (IndexEntry &ie : ne.entries())
2978 ie.poolOffset += offset;
2979 }
2980 });
2981
2982 // Update the DW_IDX_parent entries that refer to real parents (have
2983 // DW_FORM_ref4).
2984 parallelFor(Begin: 0, End: numShards, Fn: [&](size_t shard) {
2985 for (NameEntry &ne : nameVecs[shard]) {
2986 for (IndexEntry &ie : ne.entries()) {
2987 if (!ie.parentEntry)
2988 continue;
2989 // Abbrevs are indexed starting at 1; vector starts at 0. (abbrevCode
2990 // corresponds to position in the merged table vector).
2991 const Abbrev *abbrev = abbrevTable[ie.abbrevCode - 1];
2992 for (const auto &[a, v] : zip_equal(t: abbrev->attributes, u&: ie.attrValues))
2993 if (a.Index == DW_IDX_parent && a.Form == DW_FORM_ref4)
2994 v.attrValue = ie.parentEntry->poolOffset;
2995 }
2996 }
2997 });
2998
2999 // Return (entry pool size, number of entries).
3000 uint32_t num = 0;
3001 for (auto &map : maps)
3002 num += map.size();
3003 return {offsets[numShards - 1], num};
3004}
3005
3006void DebugNamesBaseSection::init(
3007 function_ref<void(InputFile *, InputChunk &, OutputChunk &)> parseFile) {
3008 TimeTraceScope timeScope("Merge .debug_names");
3009 // Collect and remove input .debug_names sections. Save InputSection pointers
3010 // to relocate string offsets in `writeTo`.
3011 SetVector<InputFile *> files;
3012 for (InputSectionBase *s : ctx.inputSections) {
3013 InputSection *isec = dyn_cast<InputSection>(Val: s);
3014 if (!isec)
3015 continue;
3016 if (!(s->flags & SHF_ALLOC) && s->name == ".debug_names") {
3017 s->markDead();
3018 inputSections.push_back(Elt: isec);
3019 files.insert(X: isec->file);
3020 }
3021 }
3022
3023 // Parse input .debug_names sections and extract InputChunk and OutputChunk
3024 // data. OutputChunk contains CU information, which will be needed by
3025 // `writeTo`.
3026 auto inputChunksPtr = std::make_unique<InputChunk[]>(num: files.size());
3027 MutableArrayRef<InputChunk> inputChunks(inputChunksPtr.get(), files.size());
3028 numChunks = files.size();
3029 chunks = std::make_unique<OutputChunk[]>(num: files.size());
3030 {
3031 TimeTraceScope timeScope("Merge .debug_names", "parse");
3032 parallelFor(Begin: 0, End: files.size(), Fn: [&](size_t i) {
3033 parseFile(files[i], inputChunks[i], chunks[i]);
3034 });
3035 }
3036
3037 // Compute section header (except unit_length), abbrev table, and entry pool.
3038 computeHdrAndAbbrevTable(inputChunks);
3039 uint32_t entryPoolSize;
3040 std::tie(args&: entryPoolSize, args&: hdr.NameCount) = computeEntryPool(inputChunks);
3041 hdr.BucketCount = dwarf::getDebugNamesBucketCount(UniqueHashCount: hdr.NameCount);
3042
3043 // Compute the section size. Subtract 4 to get the unit_length for DWARF32.
3044 uint32_t hdrSize = getDebugNamesHeaderSize(augmentationStringSize: hdr.AugmentationStringSize);
3045 size = findDebugNamesOffsets(EndOfHeaderOffset: hdrSize, Hdr: hdr).EntriesBase + entryPoolSize;
3046 hdr.UnitLength = size - 4;
3047}
3048
3049template <class ELFT>
3050DebugNamesSection<ELFT>::DebugNamesSection(Ctx &ctx)
3051 : DebugNamesBaseSection(ctx) {
3052 init(parseFile: [&](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {
3053 auto *file = cast<ObjFile<ELFT>>(f);
3054 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3055 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3056 chunk.infoSec = dobj.getInfoSection();
3057 DWARFDataExtractor namesExtractor(dobj, dobj.getNamesSection(),
3058 ELFT::Endianness == endianness::little,
3059 ELFT::Is64Bits ? 8 : 4);
3060 // .debug_str is needed to get symbol names from string offsets.
3061 DataExtractor strExtractor(dobj.getStrSection(),
3062 ELFT::Endianness == endianness::little,
3063 ELFT::Is64Bits ? 8 : 4);
3064 inputChunk.section = dobj.getNamesSection();
3065
3066 inputChunk.llvmDebugNames.emplace(args&: namesExtractor, args&: strExtractor);
3067 if (Error e = inputChunk.llvmDebugNames->extract()) {
3068 Err(ctx) << dobj.getNamesSection().sec << ": " << std::move(e);
3069 }
3070 parseDebugNames(
3071 ctx, inputChunk, chunk, namesExtractor, strExtractor,
3072 readOffsets: [&chunk, namesData = dobj.getNamesSection().Data.data()](
3073 uint32_t numCus, const DWARFDebugNames::Header &hdr,
3074 const DWARFDebugNames::DWARFDebugNamesOffsets &locs) {
3075 // Read CU offsets, which are relocated by .debug_info + X
3076 // relocations. Record the section offset to be relocated by
3077 // `finalizeContents`.
3078 chunk.compUnits.resize_for_overwrite(N: numCus + hdr.CompUnitCount);
3079 for (auto i : seq(Size: hdr.CompUnitCount))
3080 chunk.compUnits[numCus + i] = locs.CUsBase + i * 4;
3081
3082 // Read entry offsets.
3083 const char *p = namesData + locs.EntryOffsetsBase;
3084 SmallVector<uint32_t, 0> entryOffsets;
3085 entryOffsets.resize_for_overwrite(N: hdr.NameCount);
3086 for (uint32_t &offset : entryOffsets)
3087 offset = endian::readNext<uint32_t, ELFT::Endianness, unaligned>(p);
3088 return entryOffsets;
3089 });
3090 });
3091}
3092
3093template <class ELFT>
3094template <class RelTy>
3095void DebugNamesSection<ELFT>::getNameRelocs(
3096 const InputFile &file, DenseMap<uint32_t, uint32_t> &relocs,
3097 Relocs<RelTy> rels) {
3098 for (const RelTy &rel : rels) {
3099 Symbol &sym = file.getRelocTargetSym(rel);
3100 relocs[rel.r_offset] = sym.getVA(ctx, addend: getAddend<ELFT>(rel));
3101 }
3102}
3103
3104template <class ELFT> void DebugNamesSection<ELFT>::finalizeContents() {
3105 // Get relocations of .debug_names sections.
3106 auto relocs = std::make_unique<DenseMap<uint32_t, uint32_t>[]>(numChunks);
3107 parallelFor(0, numChunks, [&](size_t i) {
3108 InputSection *sec = inputSections[i];
3109 invokeOnRelocs(*sec, getNameRelocs, *sec->file, relocs.get()[i]);
3110
3111 // Relocate CU offsets with .debug_info + X relocations.
3112 OutputChunk &chunk = chunks.get()[i];
3113 for (auto [j, cuOffset] : enumerate(First&: chunk.compUnits))
3114 cuOffset = relocs.get()[i].lookup(cuOffset);
3115 });
3116
3117 // Relocate string offsets in the name table with .debug_str + X relocations.
3118 parallelForEach(nameVecs, [&](auto &nameVec) {
3119 for (NameEntry &ne : nameVec)
3120 ne.stringOffset = relocs.get()[ne.chunkIdx].lookup(ne.stringOffset);
3121 });
3122}
3123
3124template <class ELFT> void DebugNamesSection<ELFT>::writeTo(uint8_t *buf) {
3125 [[maybe_unused]] const uint8_t *const beginBuf = buf;
3126 // Write the header.
3127 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.UnitLength);
3128 endian::writeNext<uint16_t, ELFT::Endianness>(buf, hdr.Version);
3129 buf += 2; // padding
3130 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.CompUnitCount);
3131 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.LocalTypeUnitCount);
3132 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.ForeignTypeUnitCount);
3133 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.BucketCount);
3134 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.NameCount);
3135 endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.AbbrevTableSize);
3136 endian::writeNext<uint32_t, ELFT::Endianness>(buf,
3137 hdr.AugmentationStringSize);
3138 memcpy(buf, hdr.AugmentationString.c_str(), hdr.AugmentationString.size());
3139 buf += hdr.AugmentationStringSize;
3140
3141 // Write the CU list.
3142 for (auto &chunk : getChunks())
3143 for (uint32_t cuOffset : chunk.compUnits)
3144 endian::writeNext<uint32_t, ELFT::Endianness>(buf, cuOffset);
3145
3146 // TODO: Write the local TU list, then the foreign TU list..
3147
3148 // Write the hash lookup table.
3149 SmallVector<SmallVector<NameEntry *, 0>, 0> buckets(hdr.BucketCount);
3150 // Symbols enter into a bucket whose index is the hash modulo bucket_count.
3151 for (auto &nameVec : nameVecs)
3152 for (NameEntry &ne : nameVec)
3153 buckets[ne.hashValue % hdr.BucketCount].push_back(&ne);
3154
3155 // Write buckets (accumulated bucket counts).
3156 uint32_t bucketIdx = 1;
3157 for (const SmallVector<NameEntry *, 0> &bucket : buckets) {
3158 if (!bucket.empty())
3159 endian::write32<ELFT::Endianness>(buf, bucketIdx);
3160 buf += 4;
3161 bucketIdx += bucket.size();
3162 }
3163 // Write the hashes.
3164 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3165 for (const NameEntry *e : bucket)
3166 endian::writeNext<uint32_t, ELFT::Endianness>(buf, e->hashValue);
3167
3168 // Write the name table. The name entries are ordered by bucket_idx and
3169 // correspond one-to-one with the hash lookup table.
3170 //
3171 // First, write the relocated string offsets.
3172 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3173 for (const NameEntry *ne : bucket)
3174 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->stringOffset);
3175
3176 // Then write the entry offsets.
3177 for (const SmallVector<NameEntry *, 0> &bucket : buckets)
3178 for (const NameEntry *ne : bucket)
3179 endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->entryOffset);
3180
3181 // Write the abbrev table.
3182 buf = llvm::copy(abbrevTableBuf, buf);
3183
3184 // Write the entry pool. Unlike the name table, the name entries follow the
3185 // nameVecs order computed by `computeEntryPool`.
3186 for (auto &nameVec : nameVecs) {
3187 for (NameEntry &ne : nameVec) {
3188 // Write all the entries for the string.
3189 for (const IndexEntry &ie : ne.entries()) {
3190 buf += encodeULEB128(Value: ie.abbrevCode, p: buf);
3191 for (AttrValue value : ie.attrValues) {
3192 switch (value.attrSize) {
3193 case 1:
3194 *buf++ = value.attrValue;
3195 break;
3196 case 2:
3197 endian::writeNext<uint16_t, ELFT::Endianness>(buf, value.attrValue);
3198 break;
3199 case 4:
3200 endian::writeNext<uint32_t, ELFT::Endianness>(buf, value.attrValue);
3201 break;
3202 default:
3203 llvm_unreachable("invalid attrSize");
3204 }
3205 }
3206 }
3207 ++buf; // index entry sentinel
3208 }
3209 }
3210 assert(uint64_t(buf - beginBuf) == size);
3211}
3212
3213GdbIndexSection::GdbIndexSection(Ctx &ctx)
3214 : SyntheticSection(ctx, ".gdb_index", SHT_PROGBITS, 0, 1) {}
3215
3216// Returns the desired size of an on-disk hash table for a .gdb_index section.
3217// There's a tradeoff between size and collision rate. We aim 75% utilization.
3218size_t GdbIndexSection::computeSymtabSize() const {
3219 return std::max<size_t>(a: NextPowerOf2(A: symbols.size() * 4 / 3), b: 1024);
3220}
3221
3222static SmallVector<GdbIndexSection::CuEntry, 0>
3223readCuList(DWARFContext &dwarf) {
3224 SmallVector<GdbIndexSection::CuEntry, 0> ret;
3225 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())
3226 ret.push_back(Elt: {.cuOffset: cu->getOffset(), .cuLength: cu->getLength() + 4});
3227 return ret;
3228}
3229
3230static SmallVector<GdbIndexSection::AddressEntry, 0>
3231readAddressAreas(Ctx &ctx, DWARFContext &dwarf, InputSection *sec) {
3232 SmallVector<GdbIndexSection::AddressEntry, 0> ret;
3233
3234 uint32_t cuIdx = 0;
3235 for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {
3236 if (Error e = cu->tryExtractDIEsIfNeeded(CUDieOnly: false)) {
3237 Warn(ctx) << sec << ": " << std::move(e);
3238 return {};
3239 }
3240 Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges();
3241 if (!ranges) {
3242 Warn(ctx) << sec << ": " << ranges.takeError();
3243 return {};
3244 }
3245
3246 ArrayRef<InputSectionBase *> sections = sec->file->getSections();
3247 for (DWARFAddressRange &r : *ranges) {
3248 if (r.SectionIndex == -1ULL)
3249 continue;
3250 // Range list with zero size has no effect.
3251 InputSectionBase *s = sections[r.SectionIndex];
3252 if (s && s != &InputSection::discarded && s->isLive())
3253 if (r.LowPC != r.HighPC)
3254 ret.push_back(Elt: {.section: cast<InputSection>(Val: s), .lowAddress: r.LowPC, .highAddress: r.HighPC, .cuIndex: cuIdx});
3255 }
3256 ++cuIdx;
3257 }
3258
3259 return ret;
3260}
3261
3262template <class ELFT>
3263static SmallVector<GdbIndexSection::NameAttrEntry, 0>
3264readPubNamesAndTypes(Ctx &ctx, const LLDDwarfObj<ELFT> &obj,
3265 const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) {
3266 const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection();
3267 const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection();
3268
3269 SmallVector<GdbIndexSection::NameAttrEntry, 0> ret;
3270 for (const LLDDWARFSection *pub : {&pubNames, &pubTypes}) {
3271 DWARFDataExtractor data(obj, *pub, ELFT::Endianness == endianness::little,
3272 ELFT::Is64Bits ? 8 : 4);
3273 DWARFDebugPubTable table;
3274 table.extract(Data: data, /*GnuStyle=*/true, RecoverableErrorHandler: [&](Error e) {
3275 Warn(ctx) << pub->sec << ": " << std::move(e);
3276 });
3277 for (const DWARFDebugPubTable::Set &set : table.getData()) {
3278 // The value written into the constant pool is kind << 24 | cuIndex. As we
3279 // don't know how many compilation units precede this object to compute
3280 // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add
3281 // the number of preceding compilation units later.
3282 uint32_t i = llvm::partition_point(cus,
3283 [&](GdbIndexSection::CuEntry cu) {
3284 return cu.cuOffset < set.Offset;
3285 }) -
3286 cus.begin();
3287 for (const DWARFDebugPubTable::Entry &ent : set.Entries)
3288 ret.push_back(Elt: {.name: {ent.Name, computeGdbHash(s: ent.Name)},
3289 .cuIndexAndAttrs: (ent.Descriptor.toBits() << 24) | i});
3290 }
3291 }
3292 return ret;
3293}
3294
3295// Create a list of symbols from a given list of symbol names and types
3296// by uniquifying them by name.
3297static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t>
3298createSymbols(
3299 Ctx &ctx,
3300 ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs,
3301 const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) {
3302 using GdbSymbol = GdbIndexSection::GdbSymbol;
3303 using NameAttrEntry = GdbIndexSection::NameAttrEntry;
3304
3305 // For each chunk, compute the number of compilation units preceding it.
3306 uint32_t cuIdx = 0;
3307 std::unique_ptr<uint32_t[]> cuIdxs(new uint32_t[chunks.size()]);
3308 for (uint32_t i = 0, e = chunks.size(); i != e; ++i) {
3309 cuIdxs[i] = cuIdx;
3310 cuIdx += chunks[i].compilationUnits.size();
3311 }
3312
3313 // Collect the compilation unitss for each unique name. Speed it up using
3314 // multi-threading as the number of symbols can be in the order of millions.
3315 // Shard GdbSymbols by hash's high bits.
3316 constexpr size_t numShards = 32;
3317 const size_t concurrency =
3318 llvm::bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
3319 const size_t shift = 32 - llvm::countr_zero(Val: numShards);
3320 auto map =
3321 std::make_unique<DenseMap<CachedHashStringRef, size_t>[]>(num: numShards);
3322 auto symbols = std::make_unique<SmallVector<GdbSymbol, 0>[]>(num: numShards);
3323 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
3324 uint32_t i = 0;
3325 for (ArrayRef<NameAttrEntry> entries : nameAttrs) {
3326 for (const NameAttrEntry &ent : entries) {
3327 size_t shardId = ent.name.hash() >> shift;
3328 if ((shardId & (concurrency - 1)) != threadId)
3329 continue;
3330
3331 uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i];
3332 auto [it, inserted] =
3333 map[shardId].try_emplace(Key: ent.name, Args: symbols[shardId].size());
3334 if (inserted)
3335 symbols[shardId].push_back(Elt: {.name: ent.name, .cuVector: {v}, .nameOff: 0, .cuVectorOff: 0});
3336 else
3337 symbols[shardId][it->second].cuVector.push_back(Elt: v);
3338 }
3339 ++i;
3340 }
3341 });
3342
3343 size_t numSymbols = 0;
3344 for (ArrayRef<GdbSymbol> v : ArrayRef(symbols.get(), numShards))
3345 numSymbols += v.size();
3346
3347 // The return type is a flattened vector, so we'll copy each vector
3348 // contents to Ret.
3349 SmallVector<GdbSymbol, 0> ret;
3350 ret.reserve(N: numSymbols);
3351 for (SmallVector<GdbSymbol, 0> &vec :
3352 MutableArrayRef(symbols.get(), numShards))
3353 for (GdbSymbol &sym : vec)
3354 ret.push_back(Elt: std::move(sym));
3355
3356 // CU vectors and symbol names are adjacent in the output file.
3357 // We can compute their offsets in the output file now.
3358 size_t off = 0;
3359 for (GdbSymbol &sym : ret) {
3360 sym.cuVectorOff = off;
3361 off += (sym.cuVector.size() + 1) * 4;
3362 }
3363 for (GdbSymbol &sym : ret) {
3364 sym.nameOff = off;
3365 off += sym.name.size() + 1;
3366 }
3367 // If off overflows, the last symbol's nameOff likely overflows.
3368 if (!isUInt<32>(x: off))
3369 Err(ctx) << "--gdb-index: constant pool size (" << off
3370 << ") exceeds UINT32_MAX";
3371
3372 return {ret, off};
3373}
3374
3375// Returns a newly-created .gdb_index section.
3376template <class ELFT>
3377std::unique_ptr<GdbIndexSection> GdbIndexSection::create(Ctx &ctx) {
3378 llvm::TimeTraceScope timeScope("Create gdb index");
3379
3380 // Collect InputFiles with .debug_info. See the comment in
3381 // LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future,
3382 // note that isec->data() may uncompress the full content, which should be
3383 // parallelized.
3384 SetVector<InputFile *> files;
3385 for (InputSectionBase *s : ctx.inputSections) {
3386 InputSection *isec = dyn_cast<InputSection>(Val: s);
3387 if (!isec)
3388 continue;
3389 // .debug_gnu_pub{names,types} are useless in executables.
3390 // They are present in input object files solely for creating
3391 // a .gdb_index. So we can remove them from the output.
3392 if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")
3393 s->markDead();
3394 else if (isec->name == ".debug_info")
3395 files.insert(X: isec->file);
3396 }
3397 // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs.
3398 llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
3399 if (auto *isec = dyn_cast<InputSection>(Val: s))
3400 if (InputSectionBase *rel = isec->getRelocatedSection())
3401 return !rel->isLive();
3402 return !s->isLive();
3403 });
3404
3405 SmallVector<GdbChunk, 0> chunks(files.size());
3406 SmallVector<SmallVector<NameAttrEntry, 0>, 0> nameAttrs(files.size());
3407
3408 parallelFor(0, files.size(), [&](size_t i) {
3409 // To keep memory usage low, we don't want to keep cached DWARFContext, so
3410 // avoid getDwarf() here.
3411 ObjFile<ELFT> *file = cast<ObjFile<ELFT>>(files[i]);
3412 DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
3413 auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());
3414
3415 // If the are multiple compile units .debug_info (very rare ld -r --unique),
3416 // this only picks the last one. Other address ranges are lost.
3417 chunks[i].sec = dobj.getInfoSection();
3418 chunks[i].compilationUnits = readCuList(dwarf);
3419 chunks[i].addressAreas = readAddressAreas(ctx, dwarf, sec: chunks[i].sec);
3420 nameAttrs[i] =
3421 readPubNamesAndTypes<ELFT>(ctx, dobj, chunks[i].compilationUnits);
3422 });
3423
3424 auto ret = std::make_unique<GdbIndexSection>(args&: ctx);
3425 ret->chunks = std::move(chunks);
3426 std::tie(args&: ret->symbols, args&: ret->size) =
3427 createSymbols(ctx, nameAttrs, chunks: ret->chunks);
3428
3429 // Count the areas other than the constant pool.
3430 ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8;
3431 for (GdbChunk &chunk : ret->chunks)
3432 ret->size +=
3433 chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20;
3434
3435 return ret;
3436}
3437
3438void GdbIndexSection::writeTo(uint8_t *buf) {
3439 // Write the header.
3440 auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf);
3441 uint8_t *start = buf;
3442 hdr->version = 7;
3443 buf += sizeof(*hdr);
3444
3445 // Write the CU list.
3446 hdr->cuListOff = buf - start;
3447 for (GdbChunk &chunk : chunks) {
3448 for (CuEntry &cu : chunk.compilationUnits) {
3449 write64le(P: buf, V: chunk.sec->outSecOff + cu.cuOffset);
3450 write64le(P: buf + 8, V: cu.cuLength);
3451 buf += 16;
3452 }
3453 }
3454
3455 // Write the address area.
3456 hdr->cuTypesOff = buf - start;
3457 hdr->addressAreaOff = buf - start;
3458 uint32_t cuOff = 0;
3459 for (GdbChunk &chunk : chunks) {
3460 for (AddressEntry &e : chunk.addressAreas) {
3461 // In the case of ICF there may be duplicate address range entries.
3462 const uint64_t baseAddr = e.section->repl->getVA(offset: 0);
3463 write64le(P: buf, V: baseAddr + e.lowAddress);
3464 write64le(P: buf + 8, V: baseAddr + e.highAddress);
3465 write32le(P: buf + 16, V: e.cuIndex + cuOff);
3466 buf += 20;
3467 }
3468 cuOff += chunk.compilationUnits.size();
3469 }
3470
3471 // Write the on-disk open-addressing hash table containing symbols.
3472 hdr->symtabOff = buf - start;
3473 size_t symtabSize = computeSymtabSize();
3474 uint32_t mask = symtabSize - 1;
3475
3476 for (GdbSymbol &sym : symbols) {
3477 uint32_t h = sym.name.hash();
3478 uint32_t i = h & mask;
3479 uint32_t step = ((h * 17) & mask) | 1;
3480
3481 while (read32le(P: buf + i * 8))
3482 i = (i + step) & mask;
3483
3484 write32le(P: buf + i * 8, V: sym.nameOff);
3485 write32le(P: buf + i * 8 + 4, V: sym.cuVectorOff);
3486 }
3487
3488 buf += symtabSize * 8;
3489
3490 // Write the string pool.
3491 hdr->constantPoolOff = buf - start;
3492 parallelForEach(R&: symbols, Fn: [&](GdbSymbol &sym) {
3493 memcpy(dest: buf + sym.nameOff, src: sym.name.data(), n: sym.name.size());
3494 });
3495
3496 // Write the CU vectors.
3497 for (GdbSymbol &sym : symbols) {
3498 write32le(P: buf, V: sym.cuVector.size());
3499 buf += 4;
3500 for (uint32_t val : sym.cuVector) {
3501 write32le(P: buf, V: val);
3502 buf += 4;
3503 }
3504 }
3505}
3506
3507bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }
3508
3509VersionDefinitionSection::VersionDefinitionSection(Ctx &ctx)
3510 : SyntheticSection(ctx, ".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC,
3511 sizeof(uint32_t)) {}
3512
3513StringRef VersionDefinitionSection::getFileDefName() {
3514 if (!getPartition(ctx).name.empty())
3515 return getPartition(ctx).name;
3516 if (!ctx.arg.soName.empty())
3517 return ctx.arg.soName;
3518 return ctx.arg.outputFile;
3519}
3520
3521void VersionDefinitionSection::finalizeContents() {
3522 fileDefNameOff = getPartition(ctx).dynStrTab->addString(s: getFileDefName());
3523 for (const VersionDefinition &v : namedVersionDefs(ctx))
3524 verDefNameOffs.push_back(Elt: getPartition(ctx).dynStrTab->addString(s: v.name));
3525
3526 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3527 getParent()->link = sec->sectionIndex;
3528
3529 // sh_info should be set to the number of definitions. This fact is missed in
3530 // documentation, but confirmed by binutils community:
3531 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
3532 getParent()->info = getVerDefNum(ctx);
3533}
3534
3535void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index,
3536 StringRef name, size_t nameOff) {
3537 uint16_t flags = index == 1 ? VER_FLG_BASE : 0;
3538
3539 // Write a verdef.
3540 write16(ctx, p: buf, v: 1); // vd_version
3541 write16(ctx, p: buf + 2, v: flags); // vd_flags
3542 write16(ctx, p: buf + 4, v: index); // vd_ndx
3543 write16(ctx, p: buf + 6, v: 1); // vd_cnt
3544 write32(ctx, p: buf + 8, v: hashSysV(SymbolName: name)); // vd_hash
3545 write32(ctx, p: buf + 12, v: 20); // vd_aux
3546 write32(ctx, p: buf + 16, v: 28); // vd_next
3547
3548 // Write a veraux.
3549 write32(ctx, p: buf + 20, v: nameOff); // vda_name
3550 write32(ctx, p: buf + 24, v: 0); // vda_next
3551}
3552
3553void VersionDefinitionSection::writeTo(uint8_t *buf) {
3554 writeOne(buf, index: 1, name: getFileDefName(), nameOff: fileDefNameOff);
3555
3556 auto nameOffIt = verDefNameOffs.begin();
3557 for (const VersionDefinition &v : namedVersionDefs(ctx)) {
3558 buf += EntrySize;
3559 writeOne(buf, index: v.id, name: v.name, nameOff: *nameOffIt++);
3560 }
3561
3562 // Need to terminate the last version definition.
3563 write32(ctx, p: buf + 16, v: 0); // vd_next
3564}
3565
3566size_t VersionDefinitionSection::getSize() const {
3567 return EntrySize * getVerDefNum(ctx);
3568}
3569
3570// .gnu.version is a table where each entry is 2 byte long.
3571VersionTableSection::VersionTableSection(Ctx &ctx)
3572 : SyntheticSection(ctx, ".gnu.version", SHT_GNU_versym, SHF_ALLOC,
3573 sizeof(uint16_t)) {
3574 this->entsize = 2;
3575}
3576
3577void VersionTableSection::finalizeContents() {
3578 if (OutputSection *osec = getPartition(ctx).dynSymTab->getParent())
3579 getParent()->link = osec->sectionIndex;
3580}
3581
3582size_t VersionTableSection::getSize() const {
3583 return (getPartition(ctx).dynSymTab->getSymbols().size() + 1) * 2;
3584}
3585
3586void VersionTableSection::writeTo(uint8_t *buf) {
3587 buf += 2;
3588 for (const SymbolTableEntry &s : getPartition(ctx).dynSymTab->getSymbols()) {
3589 // For an unextracted lazy symbol (undefined weak), it must have been
3590 // converted to Undefined.
3591 assert(!s.sym->isLazy());
3592 // Undefined symbols should use index 0 when unversioned.
3593 write16(ctx, p: buf, v: s.sym->isUndefined() ? 0 : s.sym->versionId);
3594 buf += 2;
3595 }
3596}
3597
3598bool VersionTableSection::isNeeded() const {
3599 return isLive() &&
3600 (getPartition(ctx).verDef || getPartition(ctx).verNeed->isNeeded());
3601}
3602
3603void elf::addVerneed(Ctx &ctx, Symbol &ss) {
3604 auto &file = cast<SharedFile>(Val&: *ss.file);
3605 if (ss.versionId == VER_NDX_GLOBAL)
3606 return;
3607
3608 if (file.verneedInfo.empty())
3609 file.verneedInfo.resize(N: file.verdefs.size());
3610
3611 // Select a version identifier for the vernaux data structure, if we haven't
3612 // already allocated one. The verdef identifiers cover the range
3613 // [1..getVerDefNum(ctx)]; this causes the vernaux identifiers to start from
3614 // getVerDefNum(ctx)+1.
3615 if (file.verneedInfo[ss.versionId].id == 0)
3616 file.verneedInfo[ss.versionId].id = ++ctx.vernauxNum + getVerDefNum(ctx);
3617 file.verneedInfo[ss.versionId].weak &= ss.isWeak();
3618
3619 ss.versionId = file.verneedInfo[ss.versionId].id;
3620}
3621
3622template <class ELFT>
3623VersionNeedSection<ELFT>::VersionNeedSection(Ctx &ctx)
3624 : SyntheticSection(ctx, ".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC,
3625 sizeof(uint32_t)) {}
3626
3627template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
3628 for (SharedFile *f : ctx.sharedFiles) {
3629 if (f->verneedInfo.empty())
3630 continue;
3631 verneeds.emplace_back();
3632 Verneed &vn = verneeds.back();
3633 vn.nameStrTab = getPartition(ctx).dynStrTab->addString(f->soName);
3634 bool isLibc = ctx.arg.relrGlibc && f->soName.starts_with(Prefix: "libc.so.");
3635 bool isGlibc2 = false;
3636 for (unsigned i = 0; i != f->verneedInfo.size(); ++i) {
3637 if (f->verneedInfo[i].id == 0)
3638 continue;
3639 // Each Verdef has one or more Verdaux entries. The first Verdaux gives
3640 // the version name; subsequent entries (if any) are parent versions
3641 // (e.g., v2 {} v1;). We only use the first one, as parent versions have
3642 // no rtld behavior difference in practice.
3643 auto *verdef =
3644 reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]);
3645 StringRef ver(f->getStringTable().data() + verdef->getAux()->vda_name);
3646 if (isLibc && ver.starts_with(Prefix: "GLIBC_2."))
3647 isGlibc2 = true;
3648 vn.vernauxs.push_back({verdef->vd_hash, f->verneedInfo[i],
3649 getPartition(ctx).dynStrTab->addString(ver)});
3650 }
3651 if (isGlibc2) {
3652 const char *ver = "GLIBC_ABI_DT_RELR";
3653 vn.vernauxs.push_back(
3654 {hashSysV(SymbolName: ver),
3655 {uint16_t(++ctx.vernauxNum + getVerDefNum(ctx)), false},
3656 getPartition(ctx).dynStrTab->addString(ver)});
3657 }
3658 }
3659
3660 if (OutputSection *sec = getPartition(ctx).dynStrTab->getParent())
3661 getParent()->link = sec->sectionIndex;
3662 getParent()->info = verneeds.size();
3663}
3664
3665template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) {
3666 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
3667 auto *verneed = reinterpret_cast<Elf_Verneed *>(buf);
3668 auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size());
3669
3670 for (auto &vn : verneeds) {
3671 // Create an Elf_Verneed for this DSO.
3672 verneed->vn_version = 1;
3673 verneed->vn_cnt = vn.vernauxs.size();
3674 verneed->vn_file = vn.nameStrTab;
3675 verneed->vn_aux =
3676 reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed);
3677 verneed->vn_next = sizeof(Elf_Verneed);
3678 ++verneed;
3679
3680 // Create the Elf_Vernauxs for this Elf_Verneed.
3681 for (auto &vna : vn.vernauxs) {
3682 vernaux->vna_hash = vna.hash;
3683 vernaux->vna_flags = vna.verneedInfo.weak ? VER_FLG_WEAK : 0;
3684 vernaux->vna_other = vna.verneedInfo.id;
3685 vernaux->vna_name = vna.nameStrTab;
3686 vernaux->vna_next = sizeof(Elf_Vernaux);
3687 ++vernaux;
3688 }
3689
3690 vernaux[-1].vna_next = 0;
3691 }
3692 verneed[-1].vn_next = 0;
3693}
3694
3695template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
3696 return verneeds.size() * sizeof(Elf_Verneed) +
3697 ctx.vernauxNum * sizeof(Elf_Vernaux);
3698}
3699
3700template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
3701 return isLive() && ctx.vernauxNum != 0;
3702}
3703
3704void MergeSyntheticSection::addSection(MergeInputSection *ms) {
3705 ms->parent = this;
3706 sections.push_back(Elt: ms);
3707 assert(addralign == ms->addralign || !(ms->flags & SHF_STRINGS));
3708 addralign = std::max(a: addralign, b: ms->addralign);
3709}
3710
3711MergeTailSection::MergeTailSection(Ctx &ctx, StringRef name, uint32_t type,
3712 uint64_t flags, uint32_t alignment)
3713 : MergeSyntheticSection(ctx, name, type, flags, alignment),
3714 builder(StringTableBuilder::RAW, llvm::Align(alignment)) {}
3715
3716size_t MergeTailSection::getSize() const { return builder.getSize(); }
3717
3718void MergeTailSection::writeTo(uint8_t *buf) { builder.write(Buf: buf); }
3719
3720void MergeTailSection::finalizeContents() {
3721 // Add all string pieces to the string table builder to create section
3722 // contents.
3723 for (MergeInputSection *sec : sections)
3724 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3725 if (sec->pieces[i].live)
3726 builder.add(S: sec->getData(i));
3727
3728 // Fix the string table content. After this, the contents will never change.
3729 builder.finalize();
3730
3731 // finalize() fixed tail-optimized strings, so we can now get
3732 // offsets of strings. Get an offset for each string and save it
3733 // to a corresponding SectionPiece for easy access.
3734 for (MergeInputSection *sec : sections)
3735 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3736 if (sec->pieces[i].live)
3737 sec->pieces[i].outputOff = builder.getOffset(S: sec->getData(i));
3738}
3739
3740void MergeNoTailSection::writeTo(uint8_t *buf) {
3741 parallelFor(Begin: 0, End: numShards,
3742 Fn: [&](size_t i) { shards[i].write(Buf: buf + shardOffsets[i]); });
3743}
3744
3745// This function is very hot (i.e. it can take several seconds to finish)
3746// because sometimes the number of inputs is in an order of magnitude of
3747// millions. So, we use multi-threading.
3748//
3749// For any strings S and T, we know S is not mergeable with T if S's hash
3750// value is different from T's. If that's the case, we can safely put S and
3751// T into different string builders without worrying about merge misses.
3752// We do it in parallel.
3753void MergeNoTailSection::finalizeContents() {
3754 // Initializes string table builders.
3755 for (size_t i = 0; i < numShards; ++i)
3756 shards.emplace_back(Args: StringTableBuilder::RAW, Args: llvm::Align(addralign));
3757
3758 // Concurrency level. Must be a power of 2 to avoid expensive modulo
3759 // operations in the following tight loop.
3760 const size_t concurrency =
3761 llvm::bit_floor(Value: std::min<size_t>(a: ctx.arg.threadCount, b: numShards));
3762
3763 // Add section pieces to the builders.
3764 parallelFor(Begin: 0, End: concurrency, Fn: [&](size_t threadId) {
3765 for (MergeInputSection *sec : sections) {
3766 for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) {
3767 if (!sec->pieces[i].live)
3768 continue;
3769 size_t shardId = getShardId(hash: sec->pieces[i].hash);
3770 if ((shardId & (concurrency - 1)) == threadId)
3771 sec->pieces[i].outputOff = shards[shardId].add(S: sec->getData(i));
3772 }
3773 }
3774 });
3775
3776 // Compute an in-section offset for each shard.
3777 size_t off = 0;
3778 for (size_t i = 0; i < numShards; ++i) {
3779 shards[i].finalizeInOrder();
3780 if (shards[i].getSize() > 0)
3781 off = alignToPowerOf2(Value: off, Align: addralign);
3782 shardOffsets[i] = off;
3783 off += shards[i].getSize();
3784 }
3785 size = off;
3786
3787 // So far, section pieces have offsets from beginning of shards, but
3788 // we want offsets from beginning of the whole section. Fix them.
3789 parallelForEach(R&: sections, Fn: [&](MergeInputSection *sec) {
3790 for (SectionPiece &piece : sec->pieces)
3791 if (piece.live)
3792 piece.outputOff += shardOffsets[getShardId(hash: piece.hash)];
3793 });
3794}
3795
3796template <class ELFT> void elf::splitSections(Ctx &ctx) {
3797 llvm::TimeTraceScope timeScope("Split sections");
3798 // splitIntoPieces needs to be called on each MergeInputSection
3799 // before calling finalizeContents().
3800 parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
3801 for (InputSectionBase *sec : file->getSections()) {
3802 if (!sec)
3803 continue;
3804 if (auto *s = dyn_cast<MergeInputSection>(Val: sec))
3805 s->splitIntoPieces();
3806 else if (auto *eh = dyn_cast<EhInputSection>(Val: sec))
3807 eh->split<ELFT>();
3808 }
3809 });
3810}
3811
3812void elf::combineEhSections(Ctx &ctx) {
3813 llvm::TimeTraceScope timeScope("Combine EH sections");
3814 for (EhInputSection *sec : ctx.ehInputSections) {
3815 EhFrameSection &eh = *sec->getPartition(ctx).ehFrame;
3816 sec->parent = &eh;
3817 eh.addralign = std::max(a: eh.addralign, b: sec->addralign);
3818 eh.sections.push_back(Elt: sec);
3819 llvm::append_range(C&: eh.dependentSections, R&: sec->dependentSections);
3820 }
3821
3822 if (!ctx.mainPart->armExidx)
3823 return;
3824 llvm::erase_if(C&: ctx.inputSections, P: [&](InputSectionBase *s) {
3825 // Ignore dead sections and the partition end marker (.part.end),
3826 // whose partition number is out of bounds.
3827 if (!s->isLive() || s->partition == 255)
3828 return false;
3829 Partition &part = s->getPartition(ctx);
3830 return s->kind() == SectionBase::Regular && part.armExidx &&
3831 part.armExidx->addSection(isec: cast<InputSection>(Val: s));
3832 });
3833}
3834
3835ARMExidxSyntheticSection::ARMExidxSyntheticSection(Ctx &ctx)
3836 : SyntheticSection(ctx, ".ARM.exidx", SHT_ARM_EXIDX,
3837 SHF_ALLOC | SHF_LINK_ORDER, ctx.arg.wordsize) {}
3838
3839static InputSection *findExidxSection(InputSection *isec) {
3840 for (InputSection *d : isec->dependentSections)
3841 if (d->type == SHT_ARM_EXIDX && d->isLive())
3842 return d;
3843 return nullptr;
3844}
3845
3846static bool isValidExidxSectionDep(InputSection *isec) {
3847 return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) &&
3848 isec->getSize() > 0;
3849}
3850
3851bool ARMExidxSyntheticSection::addSection(InputSection *isec) {
3852 if (isec->type == SHT_ARM_EXIDX) {
3853 if (InputSection *dep = isec->getLinkOrderDep())
3854 if (isValidExidxSectionDep(isec: dep)) {
3855 exidxSections.push_back(Elt: isec);
3856 // Every exidxSection is 8 bytes, we need an estimate of
3857 // size before assignAddresses can be called. Final size
3858 // will only be known after finalize is called.
3859 size += 8;
3860 }
3861 return true;
3862 }
3863
3864 if (isValidExidxSectionDep(isec)) {
3865 executableSections.push_back(Elt: isec);
3866 return false;
3867 }
3868
3869 // FIXME: we do not output a relocation section when --emit-relocs is used
3870 // as we do not have relocation sections for linker generated table entries
3871 // and we would have to erase at a late stage relocations from merged entries.
3872 // Given that exception tables are already position independent and a binary
3873 // analyzer could derive the relocations we choose to erase the relocations.
3874 if (ctx.arg.emitRelocs && isec->type == SHT_REL)
3875 if (InputSectionBase *ex = isec->getRelocatedSection())
3876 if (isa<InputSection>(Val: ex) && ex->type == SHT_ARM_EXIDX)
3877 return true;
3878
3879 return false;
3880}
3881
3882// References to .ARM.Extab Sections have bit 31 clear and are not the
3883// special EXIDX_CANTUNWIND bit-pattern.
3884static bool isExtabRef(uint32_t unwind) {
3885 return (unwind & 0x80000000) == 0 && unwind != 0x1;
3886}
3887
3888// Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx
3889// section Prev, where Cur follows Prev in the table. This can be done if the
3890// unwinding instructions in Cur are identical to Prev. Linker generated
3891// EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an
3892// InputSection.
3893static bool isDuplicateArmExidxSec(Ctx &ctx, InputSection *prev,
3894 InputSection *cur) {
3895 // Get the last table Entry from the previous .ARM.exidx section. If Prev is
3896 // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.
3897 uint32_t prevUnwind = 1;
3898 if (prev)
3899 prevUnwind =
3900 read32(ctx, p: prev->content().data() + prev->content().size() - 4);
3901 if (isExtabRef(unwind: prevUnwind))
3902 return false;
3903
3904 // We consider the unwind instructions of an .ARM.exidx table entry
3905 // a duplicate if the previous unwind instructions if:
3906 // - Both are the special EXIDX_CANTUNWIND.
3907 // - Both are the same inline unwind instructions.
3908 // We do not attempt to follow and check links into .ARM.extab tables as
3909 // consecutive identical entries are rare and the effort to check that they
3910 // are identical is high.
3911
3912 // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.
3913 if (cur == nullptr)
3914 return prevUnwind == 1;
3915
3916 for (uint32_t offset = 4; offset < (uint32_t)cur->content().size(); offset +=8) {
3917 uint32_t curUnwind = read32(ctx, p: cur->content().data() + offset);
3918 if (isExtabRef(unwind: curUnwind) || curUnwind != prevUnwind)
3919 return false;
3920 }
3921 // All table entries in this .ARM.exidx Section can be merged into the
3922 // previous Section.
3923 return true;
3924}
3925
3926// The .ARM.exidx table must be sorted in ascending order of the address of the
3927// functions the table describes. std::optionally duplicate adjacent table
3928// entries can be removed. At the end of the function the executableSections
3929// must be sorted in ascending order of address, Sentinel is set to the
3930// InputSection with the highest address and any InputSections that have
3931// mergeable .ARM.exidx table entries are removed from it.
3932void ARMExidxSyntheticSection::finalizeContents() {
3933 // Ensure that any fixed-point iterations after the first see the original set
3934 // of sections.
3935 if (!originalExecutableSections.empty())
3936 executableSections = originalExecutableSections;
3937 else if (ctx.arg.enableNonContiguousRegions)
3938 originalExecutableSections = executableSections;
3939
3940 // The executableSections and exidxSections that we use to derive the final
3941 // contents of this SyntheticSection are populated before
3942 // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or
3943 // ICF may remove executable InputSections and their dependent .ARM.exidx
3944 // section that we recorded earlier.
3945 auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); };
3946 llvm::erase_if(C&: exidxSections, P: isDiscarded);
3947 // We need to remove discarded InputSections and InputSections without
3948 // .ARM.exidx sections that if we generated the .ARM.exidx it would be out
3949 // of range.
3950 auto isDiscardedOrOutOfRange = [this](InputSection *isec) {
3951 if (!isec->isLive())
3952 return true;
3953 if (findExidxSection(isec))
3954 return false;
3955 int64_t off = static_cast<int64_t>(isec->getVA() - getVA());
3956 return off != llvm::SignExtend64(X: off, B: 31);
3957 };
3958 llvm::erase_if(C&: executableSections, P: isDiscardedOrOutOfRange);
3959
3960 // Sort the executable sections that may or may not have associated
3961 // .ARM.exidx sections by order of ascending address. This requires the
3962 // relative positions of InputSections and OutputSections to be known.
3963 auto compareByFilePosition = [](const InputSection *a,
3964 const InputSection *b) {
3965 OutputSection *aOut = a->getParent();
3966 OutputSection *bOut = b->getParent();
3967
3968 if (aOut != bOut)
3969 return aOut->addr < bOut->addr;
3970 return a->outSecOff < b->outSecOff;
3971 };
3972 llvm::stable_sort(Range&: executableSections, C: compareByFilePosition);
3973 sentinel = executableSections.back();
3974 // std::optionally merge adjacent duplicate entries.
3975 if (ctx.arg.mergeArmExidx) {
3976 SmallVector<InputSection *, 0> selectedSections;
3977 selectedSections.reserve(N: executableSections.size());
3978 selectedSections.push_back(Elt: executableSections[0]);
3979 size_t prev = 0;
3980 for (size_t i = 1; i < executableSections.size(); ++i) {
3981 InputSection *ex1 = findExidxSection(isec: executableSections[prev]);
3982 InputSection *ex2 = findExidxSection(isec: executableSections[i]);
3983 if (!isDuplicateArmExidxSec(ctx, prev: ex1, cur: ex2)) {
3984 selectedSections.push_back(Elt: executableSections[i]);
3985 prev = i;
3986 }
3987 }
3988 executableSections = std::move(selectedSections);
3989 }
3990 // offset is within the SyntheticSection.
3991 size_t offset = 0;
3992 size = 0;
3993 for (InputSection *isec : executableSections) {
3994 if (InputSection *d = findExidxSection(isec)) {
3995 d->outSecOff = offset;
3996 d->parent = getParent();
3997 offset += d->getSize();
3998 } else {
3999 offset += 8;
4000 }
4001 }
4002 // Size includes Sentinel.
4003 size = offset + 8;
4004}
4005
4006InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const {
4007 return executableSections.front();
4008}
4009
4010// To write the .ARM.exidx table from the ExecutableSections we have three cases
4011// 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.
4012// We write the .ARM.exidx section contents and apply its relocations.
4013// 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We
4014// must write the contents of an EXIDX_CANTUNWIND directly. We use the
4015// start of the InputSection as the purpose of the linker generated
4016// section is to terminate the address range of the previous entry.
4017// 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of
4018// the table to terminate the address range of the final entry.
4019void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
4020
4021 // A linker generated CANTUNWIND entry is made up of two words:
4022 // 0x0 with R_ARM_PREL31 relocation to target.
4023 // 0x1 with EXIDX_CANTUNWIND.
4024 uint64_t offset = 0;
4025 for (InputSection *isec : executableSections) {
4026 assert(isec->getParent() != nullptr);
4027 if (InputSection *d = findExidxSection(isec)) {
4028 for (int dataOffset = 0; dataOffset != (int)d->content().size();
4029 dataOffset += 4)
4030 write32(ctx, p: buf + offset + dataOffset,
4031 v: read32(ctx, p: d->content().data() + dataOffset));
4032 // Recalculate outSecOff as finalizeAddressDependentContent()
4033 // may have altered syntheticSection outSecOff.
4034 d->outSecOff = offset + outSecOff;
4035 ctx.target->relocateAlloc(sec&: *d, buf: buf + offset);
4036 offset += d->getSize();
4037 } else {
4038 // A Linker generated CANTUNWIND section.
4039 write32(ctx, p: buf + offset + 0, v: 0x0);
4040 write32(ctx, p: buf + offset + 4, v: 0x1);
4041 uint64_t s = isec->getVA();
4042 uint64_t p = getVA() + offset;
4043 ctx.target->relocateNoSym(loc: buf + offset, type: R_ARM_PREL31, val: s - p);
4044 offset += 8;
4045 }
4046 }
4047 // Write Sentinel CANTUNWIND entry.
4048 write32(ctx, p: buf + offset + 0, v: 0x0);
4049 write32(ctx, p: buf + offset + 4, v: 0x1);
4050 uint64_t s = sentinel->getVA(offset: sentinel->getSize());
4051 uint64_t p = getVA() + offset;
4052 ctx.target->relocateNoSym(loc: buf + offset, type: R_ARM_PREL31, val: s - p);
4053 assert(size == offset + 8);
4054}
4055
4056bool ARMExidxSyntheticSection::isNeeded() const {
4057 return llvm::any_of(Range: exidxSections,
4058 P: [](InputSection *isec) { return isec->isLive(); });
4059}
4060
4061ThunkSection::ThunkSection(Ctx &ctx, OutputSection *os, uint64_t off)
4062 : SyntheticSection(ctx, ".text.thunk", SHT_PROGBITS,
4063 SHF_ALLOC | SHF_EXECINSTR,
4064 ctx.arg.emachine == EM_PPC64 ? 16 : 4) {
4065 this->parent = os;
4066 this->outSecOff = off;
4067}
4068
4069size_t ThunkSection::getSize() const {
4070 if (roundUpSizeForErrata)
4071 return alignTo(Value: size, Align: 4096);
4072 return size;
4073}
4074
4075void ThunkSection::addThunk(Thunk *t) {
4076 thunks.push_back(Elt: t);
4077 t->addSymbols(isec&: *this);
4078}
4079
4080void ThunkSection::writeTo(uint8_t *buf) {
4081 for (Thunk *t : thunks)
4082 t->writeTo(buf: buf + t->offset);
4083}
4084
4085InputSection *ThunkSection::getTargetInputSection() const {
4086 if (thunks.empty())
4087 return nullptr;
4088 const Thunk *t = thunks.front();
4089 return t->getTargetInputSection();
4090}
4091
4092bool ThunkSection::assignOffsets() {
4093 uint64_t off = 0;
4094 bool changed = false;
4095 for (Thunk *t : thunks) {
4096 if (t->alignment > addralign) {
4097 addralign = t->alignment;
4098 changed = true;
4099 }
4100 off = alignToPowerOf2(Value: off, Align: t->alignment);
4101 t->setOffset(off);
4102 uint32_t size = t->size();
4103 t->getThunkTargetSym()->size = size;
4104 off += size;
4105 }
4106 if (off != size)
4107 changed = true;
4108 size = off;
4109 return changed;
4110}
4111
4112// If linking position-dependent code then the table will store the addresses
4113// directly in the binary so the section has type SHT_PROGBITS. If linking
4114// position-independent code the section has type SHT_NOBITS since it will be
4115// allocated and filled in by the dynamic linker.
4116PPC64LongBranchTargetSection::PPC64LongBranchTargetSection(Ctx &ctx)
4117 : SyntheticSection(ctx, ".branch_lt",
4118 ctx.arg.isPic ? SHT_NOBITS : SHT_PROGBITS,
4119 SHF_ALLOC | SHF_WRITE, 8) {}
4120
4121uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol *sym,
4122 int64_t addend) {
4123 return getVA() + entry_index.find(Val: {sym, addend})->second * 8;
4124}
4125
4126std::optional<uint32_t>
4127PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) {
4128 auto res =
4129 entry_index.try_emplace(Key: std::make_pair(x&: sym, y&: addend), Args: entries.size());
4130 if (!res.second)
4131 return std::nullopt;
4132 entries.emplace_back(Args&: sym, Args&: addend);
4133 return res.first->second;
4134}
4135
4136size_t PPC64LongBranchTargetSection::getSize() const {
4137 return entries.size() * 8;
4138}
4139
4140void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) {
4141 // If linking non-pic we have the final addresses of the targets and they get
4142 // written to the table directly. For pic the dynamic linker will allocate
4143 // the section and fill it.
4144 if (ctx.arg.isPic)
4145 return;
4146
4147 for (auto entry : entries) {
4148 const Symbol *sym = entry.first;
4149 int64_t addend = entry.second;
4150 assert(sym->getVA(ctx));
4151 // Need calls to branch to the local entry-point since a long-branch
4152 // must be a local-call.
4153 write64(ctx, p: buf,
4154 v: sym->getVA(ctx, addend) +
4155 getPPC64GlobalEntryToLocalEntryOffset(ctx, stOther: sym->stOther));
4156 buf += 8;
4157 }
4158}
4159
4160bool PPC64LongBranchTargetSection::isNeeded() const {
4161 // `removeUnusedSyntheticSections()` is called before thunk allocation which
4162 // is too early to determine if this section will be empty or not. We need
4163 // Finalized to keep the section alive until after thunk creation. Finalized
4164 // only gets set to true once `finalizeSections()` is called after thunk
4165 // creation. Because of this, if we don't create any long-branch thunks we end
4166 // up with an empty .branch_lt section in the binary.
4167 return !finalized || !entries.empty();
4168}
4169
4170static uint8_t getAbiVersion(Ctx &ctx) {
4171 // MIPS non-PIC executable gets ABI version 1.
4172 if (ctx.arg.emachine == EM_MIPS) {
4173 if (!ctx.arg.isPic && !ctx.arg.relocatable &&
4174 (ctx.arg.eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
4175 return 1;
4176 return 0;
4177 }
4178
4179 if (ctx.arg.emachine == EM_AMDGPU && !ctx.objectFiles.empty()) {
4180 uint8_t ver = ctx.objectFiles[0]->abiVersion;
4181 for (InputFile *file : ArrayRef(ctx.objectFiles).slice(N: 1))
4182 if (file->abiVersion != ver)
4183 Err(ctx) << "incompatible ABI version: " << file;
4184 return ver;
4185 }
4186
4187 return 0;
4188}
4189
4190template <typename ELFT>
4191void elf::writeEhdr(Ctx &ctx, uint8_t *buf, Partition &part) {
4192 memcpy(dest: buf, src: "\177ELF", n: 4);
4193
4194 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4195 eHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
4196 eHdr->e_ident[EI_DATA] =
4197 ELFT::Endianness == endianness::little ? ELFDATA2LSB : ELFDATA2MSB;
4198 eHdr->e_ident[EI_VERSION] = EV_CURRENT;
4199 eHdr->e_ident[EI_OSABI] = ctx.arg.osabi;
4200 eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(ctx);
4201 eHdr->e_machine = ctx.arg.emachine;
4202 eHdr->e_version = EV_CURRENT;
4203 eHdr->e_flags = ctx.arg.eflags;
4204 eHdr->e_ehsize = sizeof(typename ELFT::Ehdr);
4205 eHdr->e_phnum = part.phdrs.size();
4206 eHdr->e_shentsize = sizeof(typename ELFT::Shdr);
4207
4208 if (!ctx.arg.relocatable) {
4209 eHdr->e_phoff = sizeof(typename ELFT::Ehdr);
4210 eHdr->e_phentsize = sizeof(typename ELFT::Phdr);
4211 }
4212}
4213
4214template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) {
4215 // Write the program header table.
4216 auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf);
4217 for (std::unique_ptr<PhdrEntry> &p : part.phdrs) {
4218 hBuf->p_type = p->p_type;
4219 hBuf->p_flags = p->p_flags;
4220 hBuf->p_offset = p->p_offset;
4221 hBuf->p_vaddr = p->p_vaddr;
4222 hBuf->p_paddr = p->p_paddr;
4223 hBuf->p_filesz = p->p_filesz;
4224 hBuf->p_memsz = p->p_memsz;
4225 hBuf->p_align = p->p_align;
4226 ++hBuf;
4227 }
4228}
4229
4230template <typename ELFT>
4231PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection(Ctx &ctx)
4232 : SyntheticSection(ctx, "", SHT_LLVM_PART_EHDR, SHF_ALLOC, 1) {}
4233
4234template <typename ELFT>
4235size_t PartitionElfHeaderSection<ELFT>::getSize() const {
4236 return sizeof(typename ELFT::Ehdr);
4237}
4238
4239template <typename ELFT>
4240void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4241 writeEhdr<ELFT>(ctx, buf, getPartition(ctx));
4242
4243 // Loadable partitions are always ET_DYN.
4244 auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
4245 eHdr->e_type = ET_DYN;
4246}
4247
4248template <typename ELFT>
4249PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection(Ctx &ctx)
4250 : SyntheticSection(ctx, ".phdrs", SHT_LLVM_PART_PHDR, SHF_ALLOC, 1) {}
4251
4252template <typename ELFT>
4253size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
4254 return sizeof(typename ELFT::Phdr) * getPartition(ctx).phdrs.size();
4255}
4256
4257template <typename ELFT>
4258void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {
4259 writePhdrs<ELFT>(buf, getPartition(ctx));
4260}
4261
4262PartitionIndexSection::PartitionIndexSection(Ctx &ctx)
4263 : SyntheticSection(ctx, ".rodata", SHT_PROGBITS, SHF_ALLOC, 4) {}
4264
4265size_t PartitionIndexSection::getSize() const {
4266 return 12 * (ctx.partitions.size() - 1);
4267}
4268
4269void PartitionIndexSection::finalizeContents() {
4270 for (size_t i = 1; i != ctx.partitions.size(); ++i)
4271 ctx.partitions[i].nameStrTab =
4272 ctx.mainPart->dynStrTab->addString(s: ctx.partitions[i].name);
4273}
4274
4275void PartitionIndexSection::writeTo(uint8_t *buf) {
4276 uint64_t va = getVA();
4277 for (size_t i = 1; i != ctx.partitions.size(); ++i) {
4278 write32(ctx, p: buf,
4279 v: ctx.mainPart->dynStrTab->getVA() + ctx.partitions[i].nameStrTab -
4280 va);
4281 write32(ctx, p: buf + 4, v: ctx.partitions[i].elfHeader->getVA() - (va + 4));
4282
4283 SyntheticSection *next = i == ctx.partitions.size() - 1
4284 ? ctx.in.partEnd.get()
4285 : ctx.partitions[i + 1].elfHeader.get();
4286 write32(ctx, p: buf + 8, v: next->getVA() - ctx.partitions[i].elfHeader->getVA());
4287
4288 va += 12;
4289 buf += 12;
4290 }
4291}
4292
4293static bool needsInterpSection(Ctx &ctx) {
4294 return !ctx.arg.relocatable && !ctx.arg.shared &&
4295 !ctx.arg.dynamicLinker.empty() && ctx.script->needsInterpSection();
4296}
4297
4298bool elf::hasMemtag(Ctx &ctx) {
4299 return ctx.arg.emachine == EM_AARCH64 &&
4300 ctx.arg.androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
4301}
4302
4303// Fully static executables don't support MTE globals at this point in time, as
4304// we currently rely on:
4305// - A dynamic loader to process relocations, and
4306// - Dynamic entries.
4307// This restriction could be removed in future by re-using some of the ideas
4308// that ifuncs use in fully static executables.
4309bool elf::canHaveMemtagGlobals(Ctx &ctx) {
4310 return hasMemtag(ctx) &&
4311 (ctx.arg.relocatable || ctx.arg.shared || needsInterpSection(ctx));
4312}
4313
4314constexpr char kMemtagAndroidNoteName[] = "Android";
4315void MemtagAndroidNote::writeTo(uint8_t *buf) {
4316 static_assert(
4317 sizeof(kMemtagAndroidNoteName) == 8,
4318 "Android 11 & 12 have an ABI that the note name is 8 bytes long. Keep it "
4319 "that way for backwards compatibility.");
4320
4321 write32(ctx, p: buf, v: sizeof(kMemtagAndroidNoteName));
4322 write32(ctx, p: buf + 4, v: sizeof(uint32_t));
4323 write32(ctx, p: buf + 8, v: ELF::NT_ANDROID_TYPE_MEMTAG);
4324 memcpy(dest: buf + 12, src: kMemtagAndroidNoteName, n: sizeof(kMemtagAndroidNoteName));
4325 buf += 12 + alignTo(Value: sizeof(kMemtagAndroidNoteName), Align: 4);
4326
4327 uint32_t value = 0;
4328 value |= ctx.arg.androidMemtagMode;
4329 if (ctx.arg.androidMemtagHeap)
4330 value |= ELF::NT_MEMTAG_HEAP;
4331 // Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled
4332 // binary on Android 11 or 12 will result in a checkfail in the loader.
4333 if (ctx.arg.androidMemtagStack)
4334 value |= ELF::NT_MEMTAG_STACK;
4335 write32(ctx, p: buf, v: value); // note value
4336}
4337
4338size_t MemtagAndroidNote::getSize() const {
4339 return sizeof(llvm::ELF::Elf64_Nhdr) +
4340 /*namesz=*/alignTo(Value: sizeof(kMemtagAndroidNoteName), Align: 4) +
4341 /*descsz=*/sizeof(uint32_t);
4342}
4343
4344void PackageMetadataNote::writeTo(uint8_t *buf) {
4345 write32(ctx, p: buf, v: 4);
4346 write32(ctx, p: buf + 4, v: ctx.arg.packageMetadata.size() + 1);
4347 write32(ctx, p: buf + 8, v: FDO_PACKAGING_METADATA);
4348 memcpy(dest: buf + 12, src: "FDO", n: 4);
4349 memcpy(dest: buf + 16, src: ctx.arg.packageMetadata.data(),
4350 n: ctx.arg.packageMetadata.size());
4351}
4352
4353size_t PackageMetadataNote::getSize() const {
4354 return sizeof(llvm::ELF::Elf64_Nhdr) + 4 +
4355 alignTo(Value: ctx.arg.packageMetadata.size() + 1, Align: 4);
4356}
4357
4358// Helper function, return the size of the ULEB128 for 'v', optionally writing
4359// it to `*(buf + offset)` if `buf` is non-null.
4360static size_t computeOrWriteULEB128(uint64_t v, uint8_t *buf, size_t offset) {
4361 if (buf)
4362 return encodeULEB128(Value: v, p: buf + offset);
4363 return getULEB128Size(Value: v);
4364}
4365
4366// https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic
4367constexpr uint64_t kMemtagStepSizeBits = 3;
4368constexpr uint64_t kMemtagGranuleSize = 16;
4369static size_t
4370createMemtagGlobalDescriptors(Ctx &ctx,
4371 const SmallVector<const Symbol *, 0> &symbols,
4372 uint8_t *buf = nullptr) {
4373 size_t sectionSize = 0;
4374 uint64_t lastGlobalEnd = 0;
4375
4376 for (const Symbol *sym : symbols) {
4377 if (!includeInSymtab(ctx, *sym))
4378 continue;
4379 const uint64_t addr = sym->getVA(ctx);
4380 const uint64_t size = sym->getSize();
4381
4382 if (addr <= kMemtagGranuleSize && buf != nullptr)
4383 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4384 << "\" falls in the ELF header. This is indicative of a "
4385 "compiler/linker bug";
4386 if (addr % kMemtagGranuleSize != 0)
4387 Err(ctx) << "address of the tagged symbol \"" << sym->getName()
4388 << "\" at 0x" << Twine::utohexstr(Val: addr)
4389 << "\" is not granule (16-byte) aligned";
4390 if (size == 0)
4391 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4392 << "\" is not allowed to be zero";
4393 if (size % kMemtagGranuleSize != 0)
4394 Err(ctx) << "size of the tagged symbol \"" << sym->getName()
4395 << "\" (size 0x" << Twine::utohexstr(Val: size)
4396 << ") is not granule (16-byte) aligned";
4397
4398 const uint64_t sizeToEncode = size / kMemtagGranuleSize;
4399 const uint64_t stepToEncode = ((addr - lastGlobalEnd) / kMemtagGranuleSize)
4400 << kMemtagStepSizeBits;
4401 if (sizeToEncode < (1 << kMemtagStepSizeBits)) {
4402 sectionSize += computeOrWriteULEB128(v: stepToEncode | sizeToEncode, buf, offset: sectionSize);
4403 } else {
4404 sectionSize += computeOrWriteULEB128(v: stepToEncode, buf, offset: sectionSize);
4405 sectionSize += computeOrWriteULEB128(v: sizeToEncode - 1, buf, offset: sectionSize);
4406 }
4407 lastGlobalEnd = addr + size;
4408 }
4409
4410 return sectionSize;
4411}
4412
4413bool MemtagGlobalDescriptors::updateAllocSize(Ctx &ctx) {
4414 size_t oldSize = getSize();
4415 llvm::stable_sort(Range&: symbols, C: [&ctx = ctx](const Symbol *s1, const Symbol *s2) {
4416 return s1->getVA(ctx) < s2->getVA(ctx);
4417 });
4418 return oldSize != getSize();
4419}
4420
4421void MemtagGlobalDescriptors::writeTo(uint8_t *buf) {
4422 createMemtagGlobalDescriptors(ctx, symbols, buf);
4423}
4424
4425size_t MemtagGlobalDescriptors::getSize() const {
4426 return createMemtagGlobalDescriptors(ctx, symbols);
4427}
4428
4429static OutputSection *findSection(Ctx &ctx, StringRef name) {
4430 for (SectionCommand *cmd : ctx.script->sectionCommands)
4431 if (auto *osd = dyn_cast<OutputDesc>(Val: cmd))
4432 if (osd->osec.name == name)
4433 return &osd->osec;
4434 return nullptr;
4435}
4436
4437static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
4438 uint64_t val, uint8_t stOther = STV_HIDDEN) {
4439 Symbol *s = ctx.symtab->find(name);
4440 if (!s || s->isDefined() || s->isCommon())
4441 return nullptr;
4442
4443 s->resolve(ctx, other: Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
4444 stOther, STT_NOTYPE, val,
4445 /*size=*/0, sec});
4446 s->isUsedInRegularObj = true;
4447 return cast<Defined>(Val: s);
4448}
4449
4450template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
4451 // Add the .interp section first because it is not a SyntheticSection.
4452 // The removeUnusedSyntheticSections() function relies on the
4453 // SyntheticSections coming last.
4454 if (needsInterpSection(ctx)) {
4455 for (size_t i = 1; i <= ctx.partitions.size(); ++i) {
4456 InputSection *sec = createInterpSection(ctx);
4457 sec->partition = i;
4458 ctx.inputSections.push_back(Elt: sec);
4459 }
4460 }
4461
4462 auto add = [&](SyntheticSection &sec) { ctx.inputSections.push_back(Elt: &sec); };
4463
4464 if (ctx.arg.zSectionHeader)
4465 ctx.in.shStrTab =
4466 std::make_unique<StringTableSection>(args&: ctx, args: ".shstrtab", args: false);
4467
4468 ctx.out.programHeaders =
4469 std::make_unique<OutputSection>(args&: ctx, args: "", args: 0, args: SHF_ALLOC);
4470 ctx.out.programHeaders->addralign = ctx.arg.wordsize;
4471
4472 if (ctx.arg.strip != StripPolicy::All) {
4473 ctx.in.strTab = std::make_unique<StringTableSection>(args&: ctx, args: ".strtab", args: false);
4474 ctx.in.symTab =
4475 std::make_unique<SymbolTableSection<ELFT>>(ctx, *ctx.in.strTab);
4476 ctx.in.symTabShndx = std::make_unique<SymtabShndxSection>(args&: ctx);
4477 }
4478
4479 ctx.in.bss = std::make_unique<BssSection>(args&: ctx, args: ".bss", args: 0, args: 1);
4480 add(*ctx.in.bss);
4481
4482 // If there is a SECTIONS command and a .data.rel.ro section name use name
4483 // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
4484 // This makes sure our relro is contiguous.
4485 bool hasDataRelRo =
4486 ctx.script->hasSectionsCommand && findSection(ctx, name: ".data.rel.ro");
4487 ctx.in.bssRelRo = std::make_unique<BssSection>(
4488 args&: ctx, args: hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", args: 0, args: 1);
4489 add(*ctx.in.bssRelRo);
4490
4491 ctx.target->initTargetSpecificSections();
4492
4493 StringRef relaDynName = ctx.arg.isRela ? ".rela.dyn" : ".rel.dyn";
4494
4495 const unsigned threadCount = ctx.arg.threadCount;
4496 for (Partition &part : ctx.partitions) {
4497 auto add = [&](SyntheticSection &sec) {
4498 sec.partition = part.getNumber(ctx);
4499 ctx.inputSections.push_back(Elt: &sec);
4500 };
4501
4502 if (!part.name.empty()) {
4503 part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>(ctx);
4504 part.elfHeader->name = part.name;
4505 add(*part.elfHeader);
4506
4507 part.programHeaders =
4508 std::make_unique<PartitionProgramHeadersSection<ELFT>>(ctx);
4509 add(*part.programHeaders);
4510 }
4511
4512 if (ctx.arg.buildId != BuildIdKind::None) {
4513 part.buildId = std::make_unique<BuildIdSection>(args&: ctx);
4514 add(*part.buildId);
4515 }
4516
4517 // dynSymTab is always present to simplify several finalizeSections
4518 // functions.
4519 part.dynStrTab = std::make_unique<StringTableSection>(args&: ctx, args: ".dynstr", args: true);
4520 part.dynSymTab =
4521 std::make_unique<SymbolTableSection<ELFT>>(ctx, *part.dynStrTab);
4522
4523 if (ctx.arg.relocatable)
4524 continue;
4525 part.dynamic = std::make_unique<DynamicSection<ELFT>>(ctx);
4526
4527 if (hasMemtag(ctx)) {
4528 part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>(args&: ctx);
4529 add(*part.memtagAndroidNote);
4530 if (canHaveMemtagGlobals(ctx)) {
4531 part.memtagGlobalDescriptors =
4532 std::make_unique<MemtagGlobalDescriptors>(args&: ctx);
4533 add(*part.memtagGlobalDescriptors);
4534 }
4535 }
4536
4537 if (ctx.arg.androidPackDynRelocs)
4538 part.relaDyn = std::make_unique<AndroidPackedRelocationSection<ELFT>>(
4539 ctx, relaDynName, threadCount);
4540 else
4541 part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
4542 ctx, relaDynName, ctx.arg.zCombreloc, threadCount);
4543
4544 if (ctx.hasDynsym) {
4545 add(*part.dynSymTab);
4546
4547 part.verSym = std::make_unique<VersionTableSection>(args&: ctx);
4548 add(*part.verSym);
4549
4550 if (!namedVersionDefs(ctx).empty()) {
4551 part.verDef = std::make_unique<VersionDefinitionSection>(args&: ctx);
4552 add(*part.verDef);
4553 }
4554
4555 part.verNeed = std::make_unique<VersionNeedSection<ELFT>>(ctx);
4556 add(*part.verNeed);
4557
4558 if (ctx.arg.gnuHash) {
4559 part.gnuHashTab = std::make_unique<GnuHashTableSection>(args&: ctx);
4560 add(*part.gnuHashTab);
4561 }
4562
4563 if (ctx.arg.sysvHash) {
4564 part.hashTab = std::make_unique<HashTableSection>(args&: ctx);
4565 add(*part.hashTab);
4566 }
4567
4568 add(*part.dynamic);
4569 add(*part.dynStrTab);
4570 }
4571 add(*part.relaDyn);
4572
4573 if (ctx.arg.relrPackDynRelocs) {
4574 part.relrDyn = std::make_unique<RelrSection<ELFT>>(ctx, threadCount);
4575 add(*part.relrDyn);
4576 part.relrAuthDyn = std::make_unique<RelrSection<ELFT>>(
4577 ctx, threadCount, /*isAArch64Auth=*/true);
4578 add(*part.relrAuthDyn);
4579 }
4580
4581 if (ctx.arg.ehFrameHdr) {
4582 part.ehFrameHdr = std::make_unique<EhFrameHeader>(args&: ctx);
4583 add(*part.ehFrameHdr);
4584 }
4585 part.ehFrame = std::make_unique<EhFrameSection>(args&: ctx);
4586 add(*part.ehFrame);
4587
4588 if (ctx.arg.emachine == EM_ARM) {
4589 // This section replaces all the individual .ARM.exidx InputSections.
4590 part.armExidx = std::make_unique<ARMExidxSyntheticSection>(args&: ctx);
4591 add(*part.armExidx);
4592 }
4593
4594 if (!ctx.arg.packageMetadata.empty()) {
4595 part.packageMetadataNote = std::make_unique<PackageMetadataNote>(args&: ctx);
4596 add(*part.packageMetadataNote);
4597 }
4598 }
4599
4600 if (ctx.partitions.size() != 1) {
4601 // Create the partition end marker. This needs to be in partition number 255
4602 // so that it is sorted after all other partitions. It also has other
4603 // special handling (see createPhdrs() and combineEhSections()).
4604 ctx.in.partEnd =
4605 std::make_unique<BssSection>(args&: ctx, args: ".part.end", args&: ctx.arg.maxPageSize, args: 1);
4606 ctx.in.partEnd->partition = 255;
4607 add(*ctx.in.partEnd);
4608
4609 ctx.in.partIndex = std::make_unique<PartitionIndexSection>(args&: ctx);
4610 addOptionalRegular(ctx, name: "__part_index_begin", sec: ctx.in.partIndex.get(), val: 0);
4611 addOptionalRegular(ctx, name: "__part_index_end", sec: ctx.in.partIndex.get(),
4612 val: ctx.in.partIndex->getSize());
4613 add(*ctx.in.partIndex);
4614 }
4615
4616 // Add .got. MIPS' .got is so different from the other archs,
4617 // it has its own class.
4618 if (ctx.arg.emachine == EM_MIPS) {
4619 ctx.in.mipsGot = std::make_unique<MipsGotSection>(args&: ctx);
4620 add(*ctx.in.mipsGot);
4621 } else {
4622 ctx.in.got = std::make_unique<GotSection>(args&: ctx);
4623 add(*ctx.in.got);
4624 }
4625
4626 ctx.in.gotPlt = std::make_unique<GotPltSection>(args&: ctx);
4627 add(*ctx.in.gotPlt);
4628 ctx.in.igotPlt = std::make_unique<IgotPltSection>(args&: ctx);
4629 add(*ctx.in.igotPlt);
4630 // Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the
4631 // section in the absence of PHDRS/SECTIONS commands.
4632 if (ctx.arg.zRelro &&
4633 ((ctx.script->phdrsCommands.empty() && !ctx.script->hasSectionsCommand) ||
4634 ctx.script->seenRelroEnd)) {
4635 ctx.in.relroPadding = std::make_unique<RelroPaddingSection>(args&: ctx);
4636 add(*ctx.in.relroPadding);
4637 }
4638
4639 // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
4640 // it as a relocation and ensure the referenced section is created.
4641 if (ctx.sym.globalOffsetTable && ctx.arg.emachine != EM_MIPS) {
4642 if (ctx.target->gotBaseSymInGotPlt)
4643 ctx.in.gotPlt->hasGotPltOffRel = true;
4644 else
4645 ctx.in.got->hasGotOffRel = true;
4646 }
4647
4648 // We always need to add rel[a].plt to output if it has entries.
4649 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
4650 ctx.in.relaPlt = std::make_unique<RelocationSection<ELFT>>(
4651 ctx, ctx.arg.isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false,
4652 /*threadCount=*/1);
4653 add(*ctx.in.relaPlt);
4654
4655 if (ctx.arg.emachine == EM_PPC)
4656 ctx.in.plt = std::make_unique<PPC32GlinkSection>(args&: ctx);
4657 else
4658 ctx.in.plt = std::make_unique<PltSection>(args&: ctx);
4659 add(*ctx.in.plt);
4660 ctx.in.iplt = std::make_unique<IpltSection>(args&: ctx);
4661 add(*ctx.in.iplt);
4662
4663 if (ctx.arg.andFeatures || ctx.aarch64PauthAbiCoreInfo) {
4664 ctx.in.gnuProperty = std::make_unique<GnuPropertySection>(args&: ctx);
4665 add(*ctx.in.gnuProperty);
4666 }
4667
4668 if (ctx.arg.debugNames) {
4669 ctx.in.debugNames = std::make_unique<DebugNamesSection<ELFT>>(ctx);
4670 add(*ctx.in.debugNames);
4671 }
4672
4673 if (ctx.arg.gdbIndex) {
4674 ctx.in.gdbIndex = GdbIndexSection::create<ELFT>(ctx);
4675 add(*ctx.in.gdbIndex);
4676 }
4677
4678 // .note.GNU-stack is always added when we are creating a re-linkable
4679 // object file. Other linkers are using the presence of this marker
4680 // section to control the executable-ness of the stack area, but that
4681 // is irrelevant these days. Stack area should always be non-executable
4682 // by default. So we emit this section unconditionally.
4683 if (ctx.arg.relocatable) {
4684 ctx.in.gnuStack = std::make_unique<GnuStackSection>(args&: ctx);
4685 add(*ctx.in.gnuStack);
4686 }
4687
4688 if (ctx.in.symTab)
4689 add(*ctx.in.symTab);
4690 if (ctx.in.symTabShndx)
4691 add(*ctx.in.symTabShndx);
4692 if (ctx.in.shStrTab)
4693 add(*ctx.in.shStrTab);
4694 if (ctx.in.strTab)
4695 add(*ctx.in.strTab);
4696}
4697
4698template void elf::splitSections<ELF32LE>(Ctx &);
4699template void elf::splitSections<ELF32BE>(Ctx &);
4700template void elf::splitSections<ELF64LE>(Ctx &);
4701template void elf::splitSections<ELF64BE>(Ctx &);
4702
4703template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>(
4704 function_ref<void(InputSection &)>);
4705template void EhFrameSection::iterateFDEWithLSDA<ELF32BE>(
4706 function_ref<void(InputSection &)>);
4707template void EhFrameSection::iterateFDEWithLSDA<ELF64LE>(
4708 function_ref<void(InputSection &)>);
4709template void EhFrameSection::iterateFDEWithLSDA<ELF64BE>(
4710 function_ref<void(InputSection &)>);
4711
4712template class elf::SymbolTableSection<ELF32LE>;
4713template class elf::SymbolTableSection<ELF32BE>;
4714template class elf::SymbolTableSection<ELF64LE>;
4715template class elf::SymbolTableSection<ELF64BE>;
4716
4717template void elf::writeEhdr<ELF32LE>(Ctx &, uint8_t *Buf, Partition &Part);
4718template void elf::writeEhdr<ELF32BE>(Ctx &, uint8_t *Buf, Partition &Part);
4719template void elf::writeEhdr<ELF64LE>(Ctx &, uint8_t *Buf, Partition &Part);
4720template void elf::writeEhdr<ELF64BE>(Ctx &, uint8_t *Buf, Partition &Part);
4721
4722template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
4723template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
4724template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);
4725template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);
4726
4727template void elf::createSyntheticSections<ELF32LE>(Ctx &);
4728template void elf::createSyntheticSections<ELF32BE>(Ctx &);
4729template void elf::createSyntheticSections<ELF64LE>(Ctx &);
4730template void elf::createSyntheticSections<ELF64BE>(Ctx &);
4731