1 //===- SyntheticSections.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "SyntheticSections.h"
20 #include "InputFiles.h"
21 #include "LinkerScript.h"
22 #include "OutputSections.h"
23 #include "SymbolTable.h"
28 #include "lld/Common/CommonLinkerContext.h"
29 #include "lld/Common/DWARF.h"
30 #include "lld/Common/Strings.h"
31 #include "lld/Common/Version.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/Sequence.h"
34 #include "llvm/ADT/SetOperations.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/BinaryFormat/Dwarf.h"
37 #include "llvm/BinaryFormat/ELF.h"
38 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
39 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
40 #include "llvm/Support/DJB.h"
41 #include "llvm/Support/Endian.h"
42 #include "llvm/Support/LEB128.h"
43 #include "llvm/Support/Parallel.h"
44 #include "llvm/Support/TimeProfiler.h"
49 using namespace llvm::dwarf
;
50 using namespace llvm::ELF
;
51 using namespace llvm::object
;
52 using namespace llvm::support
;
54 using namespace lld::elf
;
56 using llvm::support::endian::read32le
;
57 using llvm::support::endian::write32le
;
58 using llvm::support::endian::write64le
;
60 constexpr size_t MergeNoTailSection::numShards
;
62 static uint64_t readUint(Ctx
&ctx
, uint8_t *buf
) {
63 return ctx
.arg
.is64
? read64(ctx
, buf
) : read32(ctx
, buf
);
66 static void writeUint(Ctx
&ctx
, uint8_t *buf
, uint64_t val
) {
68 write64(ctx
, buf
, val
);
70 write32(ctx
, buf
, val
);
73 // Returns an LLD version string.
74 static ArrayRef
<uint8_t> getVersion(Ctx
&ctx
) {
75 // Check LLD_VERSION first for ease of testing.
76 // You can get consistent output by using the environment variable.
77 // This is only for testing.
78 StringRef s
= getenv("LLD_VERSION");
80 s
= ctx
.saver
.save(Twine("Linker: ") + getLLDVersion());
82 // +1 to include the terminating '\0'.
83 return {(const uint8_t *)s
.data(), s
.size() + 1};
86 // Creates a .comment section containing LLD version info.
87 // With this feature, you can identify LLD-generated binaries easily
88 // by "readelf --string-dump .comment <file>".
89 // The returned object is a mergeable string section.
90 MergeInputSection
*elf::createCommentSection(Ctx
&ctx
) {
92 make
<MergeInputSection
>(ctx
, SHF_MERGE
| SHF_STRINGS
, SHT_PROGBITS
, 1,
93 getVersion(ctx
), ".comment");
94 sec
->splitIntoPieces();
98 // .MIPS.abiflags section.
100 MipsAbiFlagsSection
<ELFT
>::MipsAbiFlagsSection(Ctx
&ctx
,
101 Elf_Mips_ABIFlags flags
)
102 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_MIPS_ABIFLAGS
, 8, ".MIPS.abiflags"),
104 this->entsize
= sizeof(Elf_Mips_ABIFlags
);
107 template <class ELFT
> void MipsAbiFlagsSection
<ELFT
>::writeTo(uint8_t *buf
) {
108 memcpy(buf
, &flags
, sizeof(flags
));
111 template <class ELFT
>
112 std::unique_ptr
<MipsAbiFlagsSection
<ELFT
>>
113 MipsAbiFlagsSection
<ELFT
>::create(Ctx
&ctx
) {
114 Elf_Mips_ABIFlags flags
= {};
117 for (InputSectionBase
*sec
: ctx
.inputSections
) {
118 if (sec
->type
!= SHT_MIPS_ABIFLAGS
)
123 std::string filename
= toStr(ctx
, sec
->file
);
124 const size_t size
= sec
->content().size();
125 // Older version of BFD (such as the default FreeBSD linker) concatenate
126 // .MIPS.abiflags instead of merging. To allow for this case (or potential
127 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
128 if (size
< sizeof(Elf_Mips_ABIFlags
)) {
129 ErrAlways(ctx
) << filename
130 << ": invalid size of .MIPS.abiflags section: got "
131 << Twine(size
) << " instead of "
132 << Twine(sizeof(Elf_Mips_ABIFlags
));
136 reinterpret_cast<const Elf_Mips_ABIFlags
*>(sec
->content().data());
137 if (s
->version
!= 0) {
138 ErrAlways(ctx
) << filename
<< ": unexpected .MIPS.abiflags version "
139 << Twine(s
->version
);
143 // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
144 // select the highest number of ISA/Rev/Ext.
145 flags
.isa_level
= std::max(flags
.isa_level
, s
->isa_level
);
146 flags
.isa_rev
= std::max(flags
.isa_rev
, s
->isa_rev
);
147 flags
.isa_ext
= std::max(flags
.isa_ext
, s
->isa_ext
);
148 flags
.gpr_size
= std::max(flags
.gpr_size
, s
->gpr_size
);
149 flags
.cpr1_size
= std::max(flags
.cpr1_size
, s
->cpr1_size
);
150 flags
.cpr2_size
= std::max(flags
.cpr2_size
, s
->cpr2_size
);
151 flags
.ases
|= s
->ases
;
152 flags
.flags1
|= s
->flags1
;
153 flags
.flags2
|= s
->flags2
;
155 elf::getMipsFpAbiFlag(ctx
, flags
.fp_abi
, s
->fp_abi
, filename
);
159 return std::make_unique
<MipsAbiFlagsSection
<ELFT
>>(ctx
, flags
);
163 // .MIPS.options section.
164 template <class ELFT
>
165 MipsOptionsSection
<ELFT
>::MipsOptionsSection(Ctx
&ctx
, Elf_Mips_RegInfo reginfo
)
166 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_MIPS_OPTIONS
, 8, ".MIPS.options"),
168 this->entsize
= sizeof(Elf_Mips_Options
) + sizeof(Elf_Mips_RegInfo
);
171 template <class ELFT
> void MipsOptionsSection
<ELFT
>::writeTo(uint8_t *buf
) {
172 auto *options
= reinterpret_cast<Elf_Mips_Options
*>(buf
);
173 options
->kind
= ODK_REGINFO
;
174 options
->size
= getSize();
176 if (!ctx
.arg
.relocatable
)
177 reginfo
.ri_gp_value
= ctx
.in
.mipsGot
->getGp();
178 memcpy(buf
+ sizeof(Elf_Mips_Options
), ®info
, sizeof(reginfo
));
181 template <class ELFT
>
182 std::unique_ptr
<MipsOptionsSection
<ELFT
>>
183 MipsOptionsSection
<ELFT
>::create(Ctx
&ctx
) {
188 SmallVector
<InputSectionBase
*, 0> sections
;
189 for (InputSectionBase
*sec
: ctx
.inputSections
)
190 if (sec
->type
== SHT_MIPS_OPTIONS
)
191 sections
.push_back(sec
);
193 if (sections
.empty())
196 Elf_Mips_RegInfo reginfo
= {};
197 for (InputSectionBase
*sec
: sections
) {
200 std::string filename
= toStr(ctx
, sec
->file
);
201 ArrayRef
<uint8_t> d
= sec
->content();
204 if (d
.size() < sizeof(Elf_Mips_Options
)) {
205 ErrAlways(ctx
) << filename
<< ": invalid size of .MIPS.options section";
209 auto *opt
= reinterpret_cast<const Elf_Mips_Options
*>(d
.data());
210 if (opt
->kind
== ODK_REGINFO
) {
211 reginfo
.ri_gprmask
|= opt
->getRegInfo().ri_gprmask
;
212 sec
->getFile
<ELFT
>()->mipsGp0
= opt
->getRegInfo().ri_gp_value
;
217 Fatal(ctx
) << filename
<< ": zero option descriptor size";
218 d
= d
.slice(opt
->size
);
222 return std::make_unique
<MipsOptionsSection
<ELFT
>>(ctx
, reginfo
);
225 // MIPS .reginfo section.
226 template <class ELFT
>
227 MipsReginfoSection
<ELFT
>::MipsReginfoSection(Ctx
&ctx
, Elf_Mips_RegInfo reginfo
)
228 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_MIPS_REGINFO
, 4, ".reginfo"),
230 this->entsize
= sizeof(Elf_Mips_RegInfo
);
233 template <class ELFT
> void MipsReginfoSection
<ELFT
>::writeTo(uint8_t *buf
) {
234 if (!ctx
.arg
.relocatable
)
235 reginfo
.ri_gp_value
= ctx
.in
.mipsGot
->getGp();
236 memcpy(buf
, ®info
, sizeof(reginfo
));
239 template <class ELFT
>
240 std::unique_ptr
<MipsReginfoSection
<ELFT
>>
241 MipsReginfoSection
<ELFT
>::create(Ctx
&ctx
) {
242 // Section should be alive for O32 and N32 ABIs only.
246 SmallVector
<InputSectionBase
*, 0> sections
;
247 for (InputSectionBase
*sec
: ctx
.inputSections
)
248 if (sec
->type
== SHT_MIPS_REGINFO
)
249 sections
.push_back(sec
);
251 if (sections
.empty())
254 Elf_Mips_RegInfo reginfo
= {};
255 for (InputSectionBase
*sec
: sections
) {
258 if (sec
->content().size() != sizeof(Elf_Mips_RegInfo
)) {
259 ErrAlways(ctx
) << sec
->file
<< ": invalid size of .reginfo section";
263 auto *r
= reinterpret_cast<const Elf_Mips_RegInfo
*>(sec
->content().data());
264 reginfo
.ri_gprmask
|= r
->ri_gprmask
;
265 sec
->getFile
<ELFT
>()->mipsGp0
= r
->ri_gp_value
;
268 return std::make_unique
<MipsReginfoSection
<ELFT
>>(ctx
, reginfo
);
271 InputSection
*elf::createInterpSection(Ctx
&ctx
) {
272 // StringSaver guarantees that the returned string ends with '\0'.
273 StringRef s
= ctx
.saver
.save(ctx
.arg
.dynamicLinker
);
274 ArrayRef
<uint8_t> contents
= {(const uint8_t *)s
.data(), s
.size() + 1};
276 return make
<InputSection
>(ctx
.internalFile
, SHF_ALLOC
, SHT_PROGBITS
, 1,
277 contents
, ".interp");
280 Defined
*elf::addSyntheticLocal(Ctx
&ctx
, StringRef name
, uint8_t type
,
281 uint64_t value
, uint64_t size
,
282 InputSectionBase
§ion
) {
283 Defined
*s
= makeDefined(ctx
, section
.file
, name
, STB_LOCAL
, STV_DEFAULT
,
284 type
, value
, size
, §ion
);
286 ctx
.in
.symTab
->addSymbol(s
);
288 if (ctx
.arg
.emachine
== EM_ARM
&& !ctx
.arg
.isLE
&& ctx
.arg
.armBe8
&&
289 (section
.flags
& SHF_EXECINSTR
))
290 // Adding Linker generated mapping symbols to the arm specific mapping
292 addArmSyntheticSectionMappingSymbol(s
);
297 static size_t getHashSize(Ctx
&ctx
) {
298 switch (ctx
.arg
.buildId
) {
299 case BuildIdKind::Fast
:
301 case BuildIdKind::Md5
:
302 case BuildIdKind::Uuid
:
304 case BuildIdKind::Sha1
:
306 case BuildIdKind::Hexstring
:
307 return ctx
.arg
.buildIdVector
.size();
309 llvm_unreachable("unknown BuildIdKind");
313 // This class represents a linker-synthesized .note.gnu.property section.
315 // In x86 and AArch64, object files may contain feature flags indicating the
316 // features that they have used. The flags are stored in a .note.gnu.property
319 // lld reads the sections from input files and merges them by computing AND of
320 // the flags. The result is written as a new .note.gnu.property section.
322 // If the flag is zero (which indicates that the intersection of the feature
323 // sets is empty, or some input files didn't have .note.gnu.property sections),
324 // we don't create this section.
325 GnuPropertySection::GnuPropertySection(Ctx
&ctx
)
326 : SyntheticSection(ctx
, llvm::ELF::SHF_ALLOC
, llvm::ELF::SHT_NOTE
,
327 ctx
.arg
.wordsize
, ".note.gnu.property") {}
329 void GnuPropertySection::writeTo(uint8_t *buf
) {
330 write32(ctx
, buf
, 4); // Name size
331 write32(ctx
, buf
+ 4, getSize() - 16); // Content size
332 write32(ctx
, buf
+ 8, NT_GNU_PROPERTY_TYPE_0
); // Type
333 memcpy(buf
+ 12, "GNU", 4); // Name string
335 uint32_t featureAndType
= ctx
.arg
.emachine
== EM_AARCH64
336 ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
337 : GNU_PROPERTY_X86_FEATURE_1_AND
;
339 unsigned offset
= 16;
340 if (ctx
.arg
.andFeatures
!= 0) {
341 write32(ctx
, buf
+ offset
+ 0, featureAndType
); // Feature type
342 write32(ctx
, buf
+ offset
+ 4, 4); // Feature size
343 write32(ctx
, buf
+ offset
+ 8, ctx
.arg
.andFeatures
); // Feature flags
345 write32(ctx
, buf
+ offset
+ 12, 0); // Padding
349 if (!ctx
.aarch64PauthAbiCoreInfo
.empty()) {
350 write32(ctx
, buf
+ offset
+ 0, GNU_PROPERTY_AARCH64_FEATURE_PAUTH
);
351 write32(ctx
, buf
+ offset
+ 4, ctx
.aarch64PauthAbiCoreInfo
.size());
352 memcpy(buf
+ offset
+ 8, ctx
.aarch64PauthAbiCoreInfo
.data(),
353 ctx
.aarch64PauthAbiCoreInfo
.size());
357 size_t GnuPropertySection::getSize() const {
358 uint32_t contentSize
= 0;
359 if (ctx
.arg
.andFeatures
!= 0)
360 contentSize
+= ctx
.arg
.is64
? 16 : 12;
361 if (!ctx
.aarch64PauthAbiCoreInfo
.empty())
362 contentSize
+= 4 + 4 + ctx
.aarch64PauthAbiCoreInfo
.size();
363 assert(contentSize
!= 0);
364 return contentSize
+ 16;
367 BuildIdSection::BuildIdSection(Ctx
&ctx
)
368 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_NOTE
, 4, ".note.gnu.build-id"),
369 hashSize(getHashSize(ctx
)) {}
371 void BuildIdSection::writeTo(uint8_t *buf
) {
372 write32(ctx
, buf
, 4); // Name size
373 write32(ctx
, buf
+ 4, hashSize
); // Content size
374 write32(ctx
, buf
+ 8, NT_GNU_BUILD_ID
); // Type
375 memcpy(buf
+ 12, "GNU", 4); // Name string
379 void BuildIdSection::writeBuildId(ArrayRef
<uint8_t> buf
) {
380 assert(buf
.size() == hashSize
);
381 memcpy(hashBuf
, buf
.data(), hashSize
);
384 BssSection::BssSection(Ctx
&ctx
, StringRef name
, uint64_t size
,
386 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_NOBITS
, alignment
,
392 EhFrameSection::EhFrameSection(Ctx
&ctx
)
393 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_PROGBITS
, 1, ".eh_frame") {}
395 // Search for an existing CIE record or create a new one.
396 // CIE records from input object files are uniquified by their contents
397 // and where their relocations point to.
398 template <class ELFT
, class RelTy
>
399 CieRecord
*EhFrameSection::addCie(EhSectionPiece
&cie
, ArrayRef
<RelTy
> rels
) {
400 Symbol
*personality
= nullptr;
401 unsigned firstRelI
= cie
.firstRelocation
;
402 if (firstRelI
!= (unsigned)-1)
403 personality
= &cie
.sec
->file
->getRelocTargetSym(rels
[firstRelI
]);
405 // Search for an existing CIE by CIE contents/relocation target pair.
406 CieRecord
*&rec
= cieMap
[{cie
.data(), personality
}];
408 // If not found, create a new one.
410 rec
= make
<CieRecord
>();
412 cieRecords
.push_back(rec
);
417 // There is one FDE per function. Returns a non-null pointer to the function
418 // symbol if the given FDE points to a live function.
419 template <class ELFT
, class RelTy
>
420 Defined
*EhFrameSection::isFdeLive(EhSectionPiece
&fde
, ArrayRef
<RelTy
> rels
) {
421 auto *sec
= cast
<EhInputSection
>(fde
.sec
);
422 unsigned firstRelI
= fde
.firstRelocation
;
424 // An FDE should point to some function because FDEs are to describe
425 // functions. That's however not always the case due to an issue of
426 // ld.gold with -r. ld.gold may discard only functions and leave their
427 // corresponding FDEs, which results in creating bad .eh_frame sections.
428 // To deal with that, we ignore such FDEs.
429 if (firstRelI
== (unsigned)-1)
432 const RelTy
&rel
= rels
[firstRelI
];
433 Symbol
&b
= sec
->file
->getRelocTargetSym(rel
);
435 // FDEs for garbage-collected or merged-by-ICF sections, or sections in
436 // another partition, are dead.
437 if (auto *d
= dyn_cast
<Defined
>(&b
))
438 if (!d
->folded
&& d
->section
&& d
->section
->partition
== partition
)
443 // .eh_frame is a sequence of CIE or FDE records. In general, there
444 // is one CIE record per input object file which is followed by
445 // a list of FDEs. This function searches an existing CIE or create a new
446 // one and associates FDEs to the CIE.
447 template <class ELFT
, class RelTy
>
448 void EhFrameSection::addRecords(EhInputSection
*sec
, ArrayRef
<RelTy
> rels
) {
450 for (EhSectionPiece
&cie
: sec
->cies
)
451 offsetToCie
[cie
.inputOff
] = addCie
<ELFT
>(cie
, rels
);
452 for (EhSectionPiece
&fde
: sec
->fdes
) {
453 uint32_t id
= endian::read32
<ELFT::Endianness
>(fde
.data().data() + 4);
454 CieRecord
*rec
= offsetToCie
[fde
.inputOff
+ 4 - id
];
456 Fatal(ctx
) << sec
<< ": invalid CIE reference";
458 if (!isFdeLive
<ELFT
>(fde
, rels
))
460 rec
->fdes
.push_back(&fde
);
465 template <class ELFT
>
466 void EhFrameSection::addSectionAux(EhInputSection
*sec
) {
469 const RelsOrRelas
<ELFT
> rels
=
470 sec
->template relsOrRelas
<ELFT
>(/*supportsCrel=*/false);
471 if (rels
.areRelocsRel())
472 addRecords
<ELFT
>(sec
, rels
.rels
);
474 addRecords
<ELFT
>(sec
, rels
.relas
);
477 // Used by ICF<ELFT>::handleLSDA(). This function is very similar to
478 // EhFrameSection::addRecords().
479 template <class ELFT
, class RelTy
>
480 void EhFrameSection::iterateFDEWithLSDAAux(
481 EhInputSection
&sec
, ArrayRef
<RelTy
> rels
, DenseSet
<size_t> &ciesWithLSDA
,
482 llvm::function_ref
<void(InputSection
&)> fn
) {
483 for (EhSectionPiece
&cie
: sec
.cies
)
485 ciesWithLSDA
.insert(cie
.inputOff
);
486 for (EhSectionPiece
&fde
: sec
.fdes
) {
487 uint32_t id
= endian::read32
<ELFT::Endianness
>(fde
.data().data() + 4);
488 if (!ciesWithLSDA
.contains(fde
.inputOff
+ 4 - id
))
491 // The CIE has a LSDA argument. Call fn with d's section.
492 if (Defined
*d
= isFdeLive
<ELFT
>(fde
, rels
))
493 if (auto *s
= dyn_cast_or_null
<InputSection
>(d
->section
))
498 template <class ELFT
>
499 void EhFrameSection::iterateFDEWithLSDA(
500 llvm::function_ref
<void(InputSection
&)> fn
) {
501 DenseSet
<size_t> ciesWithLSDA
;
502 for (EhInputSection
*sec
: sections
) {
503 ciesWithLSDA
.clear();
504 const RelsOrRelas
<ELFT
> rels
=
505 sec
->template relsOrRelas
<ELFT
>(/*supportsCrel=*/false);
506 if (rels
.areRelocsRel())
507 iterateFDEWithLSDAAux
<ELFT
>(*sec
, rels
.rels
, ciesWithLSDA
, fn
);
509 iterateFDEWithLSDAAux
<ELFT
>(*sec
, rels
.relas
, ciesWithLSDA
, fn
);
513 static void writeCieFde(Ctx
&ctx
, uint8_t *buf
, ArrayRef
<uint8_t> d
) {
514 memcpy(buf
, d
.data(), d
.size());
515 // Fix the size field. -4 since size does not include the size field itself.
516 write32(ctx
, buf
, d
.size() - 4);
519 void EhFrameSection::finalizeContents() {
520 assert(!this->size
); // Not finalized.
522 switch (ctx
.arg
.ekind
) {
524 llvm_unreachable("invalid ekind");
526 for (EhInputSection
*sec
: sections
)
527 addSectionAux
<ELF32LE
>(sec
);
530 for (EhInputSection
*sec
: sections
)
531 addSectionAux
<ELF32BE
>(sec
);
534 for (EhInputSection
*sec
: sections
)
535 addSectionAux
<ELF64LE
>(sec
);
538 for (EhInputSection
*sec
: sections
)
539 addSectionAux
<ELF64BE
>(sec
);
544 for (CieRecord
*rec
: cieRecords
) {
545 rec
->cie
->outputOff
= off
;
546 off
+= rec
->cie
->size
;
548 for (EhSectionPiece
*fde
: rec
->fdes
) {
549 fde
->outputOff
= off
;
554 // The LSB standard does not allow a .eh_frame section with zero
555 // Call Frame Information records. glibc unwind-dw2-fde.c
556 // classify_object_over_fdes expects there is a CIE record length 0 as a
557 // terminator. Thus we add one unconditionally.
563 // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
564 // to get an FDE from an address to which FDE is applied. This function
565 // returns a list of such pairs.
566 SmallVector
<EhFrameSection::FdeData
, 0> EhFrameSection::getFdeData() const {
567 uint8_t *buf
= ctx
.bufferStart
+ getParent()->offset
+ outSecOff
;
568 SmallVector
<FdeData
, 0> ret
;
570 uint64_t va
= getPartition(ctx
).ehFrameHdr
->getVA();
571 for (CieRecord
*rec
: cieRecords
) {
572 uint8_t enc
= getFdeEncoding(rec
->cie
);
573 for (EhSectionPiece
*fde
: rec
->fdes
) {
574 uint64_t pc
= getFdePc(buf
, fde
->outputOff
, enc
);
575 uint64_t fdeVA
= getParent()->addr
+ fde
->outputOff
;
576 if (!isInt
<32>(pc
- va
)) {
577 Err(ctx
) << fde
->sec
<< ": PC offset is too large: 0x"
578 << Twine::utohexstr(pc
- va
);
581 ret
.push_back({uint32_t(pc
- va
), uint32_t(fdeVA
- va
)});
585 // Sort the FDE list by their PC and uniqueify. Usually there is only
586 // one FDE for a PC (i.e. function), but if ICF merges two functions
587 // into one, there can be more than one FDEs pointing to the address.
588 auto less
= [](const FdeData
&a
, const FdeData
&b
) {
589 return a
.pcRel
< b
.pcRel
;
591 llvm::stable_sort(ret
, less
);
592 auto eq
= [](const FdeData
&a
, const FdeData
&b
) {
593 return a
.pcRel
== b
.pcRel
;
595 ret
.erase(std::unique(ret
.begin(), ret
.end(), eq
), ret
.end());
600 static uint64_t readFdeAddr(Ctx
&ctx
, uint8_t *buf
, int size
) {
602 case DW_EH_PE_udata2
:
603 return read16(ctx
, buf
);
604 case DW_EH_PE_sdata2
:
605 return (int16_t)read16(ctx
, buf
);
606 case DW_EH_PE_udata4
:
607 return read32(ctx
, buf
);
608 case DW_EH_PE_sdata4
:
609 return (int32_t)read32(ctx
, buf
);
610 case DW_EH_PE_udata8
:
611 case DW_EH_PE_sdata8
:
612 return read64(ctx
, buf
);
613 case DW_EH_PE_absptr
:
614 return readUint(ctx
, buf
);
616 Err(ctx
) << "unknown FDE size encoding";
620 // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
621 // We need it to create .eh_frame_hdr section.
622 uint64_t EhFrameSection::getFdePc(uint8_t *buf
, size_t fdeOff
,
624 // The starting address to which this FDE applies is
625 // stored at FDE + 8 byte. And this offset is within
626 // the .eh_frame section.
627 size_t off
= fdeOff
+ 8;
628 uint64_t addr
= readFdeAddr(ctx
, buf
+ off
, enc
& 0xf);
629 if ((enc
& 0x70) == DW_EH_PE_absptr
)
630 return ctx
.arg
.is64
? addr
: uint32_t(addr
);
631 if ((enc
& 0x70) == DW_EH_PE_pcrel
)
632 return addr
+ getParent()->addr
+ off
+ outSecOff
;
633 Err(ctx
) << "unknown FDE size relative encoding";
637 void EhFrameSection::writeTo(uint8_t *buf
) {
638 // Write CIE and FDE records.
639 for (CieRecord
*rec
: cieRecords
) {
640 size_t cieOffset
= rec
->cie
->outputOff
;
641 writeCieFde(ctx
, buf
+ cieOffset
, rec
->cie
->data());
643 for (EhSectionPiece
*fde
: rec
->fdes
) {
644 size_t off
= fde
->outputOff
;
645 writeCieFde(ctx
, buf
+ off
, fde
->data());
647 // FDE's second word should have the offset to an associated CIE.
649 write32(ctx
, buf
+ off
+ 4, off
+ 4 - cieOffset
);
653 // Apply relocations. .eh_frame section contents are not contiguous
654 // in the output buffer, but relocateAlloc() still works because
655 // getOffset() takes care of discontiguous section pieces.
656 for (EhInputSection
*s
: sections
)
657 ctx
.target
->relocateAlloc(*s
, buf
);
659 if (getPartition(ctx
).ehFrameHdr
&& getPartition(ctx
).ehFrameHdr
->getParent())
660 getPartition(ctx
).ehFrameHdr
->write();
663 GotSection::GotSection(Ctx
&ctx
)
664 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_PROGBITS
,
665 ctx
.target
->gotEntrySize
, ".got") {
666 numEntries
= ctx
.target
->gotHeaderEntriesNum
;
669 void GotSection::addConstant(const Relocation
&r
) { relocations
.push_back(r
); }
670 void GotSection::addEntry(const Symbol
&sym
) {
671 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1);
672 ctx
.symAux
.back().gotIdx
= numEntries
++;
675 bool GotSection::addTlsDescEntry(const Symbol
&sym
) {
676 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1);
677 ctx
.symAux
.back().tlsDescIdx
= numEntries
;
682 bool GotSection::addDynTlsEntry(const Symbol
&sym
) {
683 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1);
684 ctx
.symAux
.back().tlsGdIdx
= numEntries
;
685 // Global Dynamic TLS entries take two GOT slots.
690 // Reserves TLS entries for a TLS module ID and a TLS block offset.
691 // In total it takes two GOT slots.
692 bool GotSection::addTlsIndex() {
693 if (tlsIndexOff
!= uint32_t(-1))
695 tlsIndexOff
= numEntries
* ctx
.arg
.wordsize
;
700 uint32_t GotSection::getTlsDescOffset(const Symbol
&sym
) const {
701 return sym
.getTlsDescIdx(ctx
) * ctx
.arg
.wordsize
;
704 uint64_t GotSection::getTlsDescAddr(const Symbol
&sym
) const {
705 return getVA() + getTlsDescOffset(sym
);
708 uint64_t GotSection::getGlobalDynAddr(const Symbol
&b
) const {
709 return this->getVA() + b
.getTlsGdIdx(ctx
) * ctx
.arg
.wordsize
;
712 uint64_t GotSection::getGlobalDynOffset(const Symbol
&b
) const {
713 return b
.getTlsGdIdx(ctx
) * ctx
.arg
.wordsize
;
716 void GotSection::finalizeContents() {
717 if (ctx
.arg
.emachine
== EM_PPC64
&&
718 numEntries
<= ctx
.target
->gotHeaderEntriesNum
&&
719 !ctx
.sym
.globalOffsetTable
)
722 size
= numEntries
* ctx
.arg
.wordsize
;
725 bool GotSection::isNeeded() const {
726 // Needed if the GOT symbol is used or the number of entries is more than just
727 // the header. A GOT with just the header may not be needed.
728 return hasGotOffRel
|| numEntries
> ctx
.target
->gotHeaderEntriesNum
;
731 void GotSection::writeTo(uint8_t *buf
) {
732 // On PPC64 .got may be needed but empty. Skip the write.
735 ctx
.target
->writeGotHeader(buf
);
736 ctx
.target
->relocateAlloc(*this, buf
);
739 static uint64_t getMipsPageAddr(uint64_t addr
) {
740 return (addr
+ 0x8000) & ~0xffff;
743 static uint64_t getMipsPageCount(uint64_t size
) {
744 return (size
+ 0xfffe) / 0xffff + 1;
747 MipsGotSection::MipsGotSection(Ctx
&ctx
)
748 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
| SHF_MIPS_GPREL
,
749 SHT_PROGBITS
, 16, ".got") {}
751 void MipsGotSection::addEntry(InputFile
&file
, Symbol
&sym
, int64_t addend
,
753 FileGot
&g
= getGot(file
);
754 if (expr
== R_MIPS_GOT_LOCAL_PAGE
) {
755 if (const OutputSection
*os
= sym
.getOutputSection())
756 g
.pagesMap
.insert({os
, {}});
758 g
.local16
.insert({{nullptr, getMipsPageAddr(sym
.getVA(ctx
, addend
))}, 0});
759 } else if (sym
.isTls())
760 g
.tls
.insert({&sym
, 0});
761 else if (sym
.isPreemptible
&& expr
== R_ABS
)
762 g
.relocs
.insert({&sym
, 0});
763 else if (sym
.isPreemptible
)
764 g
.global
.insert({&sym
, 0});
765 else if (expr
== R_MIPS_GOT_OFF32
)
766 g
.local32
.insert({{&sym
, addend
}, 0});
768 g
.local16
.insert({{&sym
, addend
}, 0});
771 void MipsGotSection::addDynTlsEntry(InputFile
&file
, Symbol
&sym
) {
772 getGot(file
).dynTlsSymbols
.insert({&sym
, 0});
775 void MipsGotSection::addTlsIndex(InputFile
&file
) {
776 getGot(file
).dynTlsSymbols
.insert({nullptr, 0});
779 size_t MipsGotSection::FileGot::getEntriesNum() const {
780 return getPageEntriesNum() + local16
.size() + global
.size() + relocs
.size() +
781 tls
.size() + dynTlsSymbols
.size() * 2;
784 size_t MipsGotSection::FileGot::getPageEntriesNum() const {
786 for (const std::pair
<const OutputSection
*, FileGot::PageBlock
> &p
: pagesMap
)
787 num
+= p
.second
.count
;
791 size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
792 size_t count
= getPageEntriesNum() + local16
.size() + global
.size();
793 // If there are relocation-only entries in the GOT, TLS entries
794 // are allocated after them. TLS entries should be addressable
795 // by 16-bit index so count both reloc-only and TLS entries.
796 if (!tls
.empty() || !dynTlsSymbols
.empty())
797 count
+= relocs
.size() + tls
.size() + dynTlsSymbols
.size() * 2;
801 MipsGotSection::FileGot
&MipsGotSection::getGot(InputFile
&f
) {
802 if (f
.mipsGotIndex
== uint32_t(-1)) {
804 gots
.back().file
= &f
;
805 f
.mipsGotIndex
= gots
.size() - 1;
807 return gots
[f
.mipsGotIndex
];
810 uint64_t MipsGotSection::getPageEntryOffset(const InputFile
*f
,
812 int64_t addend
) const {
813 const FileGot
&g
= gots
[f
->mipsGotIndex
];
815 if (const OutputSection
*outSec
= sym
.getOutputSection()) {
816 uint64_t secAddr
= getMipsPageAddr(outSec
->addr
);
817 uint64_t symAddr
= getMipsPageAddr(sym
.getVA(ctx
, addend
));
818 index
= g
.pagesMap
.lookup(outSec
).firstIndex
+ (symAddr
- secAddr
) / 0xffff;
821 g
.local16
.lookup({nullptr, getMipsPageAddr(sym
.getVA(ctx
, addend
))});
823 return index
* ctx
.arg
.wordsize
;
826 uint64_t MipsGotSection::getSymEntryOffset(const InputFile
*f
, const Symbol
&s
,
827 int64_t addend
) const {
828 const FileGot
&g
= gots
[f
->mipsGotIndex
];
829 Symbol
*sym
= const_cast<Symbol
*>(&s
);
831 return g
.tls
.lookup(sym
) * ctx
.arg
.wordsize
;
832 if (sym
->isPreemptible
)
833 return g
.global
.lookup(sym
) * ctx
.arg
.wordsize
;
834 return g
.local16
.lookup({sym
, addend
}) * ctx
.arg
.wordsize
;
837 uint64_t MipsGotSection::getTlsIndexOffset(const InputFile
*f
) const {
838 const FileGot
&g
= gots
[f
->mipsGotIndex
];
839 return g
.dynTlsSymbols
.lookup(nullptr) * ctx
.arg
.wordsize
;
842 uint64_t MipsGotSection::getGlobalDynOffset(const InputFile
*f
,
843 const Symbol
&s
) const {
844 const FileGot
&g
= gots
[f
->mipsGotIndex
];
845 Symbol
*sym
= const_cast<Symbol
*>(&s
);
846 return g
.dynTlsSymbols
.lookup(sym
) * ctx
.arg
.wordsize
;
849 const Symbol
*MipsGotSection::getFirstGlobalEntry() const {
852 const FileGot
&primGot
= gots
.front();
853 if (!primGot
.global
.empty())
854 return primGot
.global
.front().first
;
855 if (!primGot
.relocs
.empty())
856 return primGot
.relocs
.front().first
;
860 unsigned MipsGotSection::getLocalEntriesNum() const {
862 return headerEntriesNum
;
863 return headerEntriesNum
+ gots
.front().getPageEntriesNum() +
864 gots
.front().local16
.size();
867 bool MipsGotSection::tryMergeGots(FileGot
&dst
, FileGot
&src
, bool isPrimary
) {
869 set_union(tmp
.pagesMap
, src
.pagesMap
);
870 set_union(tmp
.local16
, src
.local16
);
871 set_union(tmp
.global
, src
.global
);
872 set_union(tmp
.relocs
, src
.relocs
);
873 set_union(tmp
.tls
, src
.tls
);
874 set_union(tmp
.dynTlsSymbols
, src
.dynTlsSymbols
);
876 size_t count
= isPrimary
? headerEntriesNum
: 0;
877 count
+= tmp
.getIndexedEntriesNum();
879 if (count
* ctx
.arg
.wordsize
> ctx
.arg
.mipsGotSize
)
886 void MipsGotSection::finalizeContents() { updateAllocSize(ctx
); }
888 bool MipsGotSection::updateAllocSize(Ctx
&ctx
) {
889 size
= headerEntriesNum
* ctx
.arg
.wordsize
;
890 for (const FileGot
&g
: gots
)
891 size
+= g
.getEntriesNum() * ctx
.arg
.wordsize
;
895 void MipsGotSection::build() {
899 std::vector
<FileGot
> mergedGots(1);
901 // For each GOT move non-preemptible symbols from the `Global`
902 // to `Local16` list. Preemptible symbol might become non-preemptible
903 // one if, for example, it gets a related copy relocation.
904 for (FileGot
&got
: gots
) {
905 for (auto &p
: got
.global
)
906 if (!p
.first
->isPreemptible
)
907 got
.local16
.insert({{p
.first
, 0}, 0});
908 got
.global
.remove_if([&](const std::pair
<Symbol
*, size_t> &p
) {
909 return !p
.first
->isPreemptible
;
913 // For each GOT remove "reloc-only" entry if there is "global"
914 // entry for the same symbol. And add local entries which indexed
915 // using 32-bit value at the end of 16-bit entries.
916 for (FileGot
&got
: gots
) {
917 got
.relocs
.remove_if([&](const std::pair
<Symbol
*, size_t> &p
) {
918 return got
.global
.count(p
.first
);
920 set_union(got
.local16
, got
.local32
);
924 // Evaluate number of "reloc-only" entries in the resulting GOT.
925 // To do that put all unique "reloc-only" and "global" entries
926 // from all GOTs to the future primary GOT.
927 FileGot
*primGot
= &mergedGots
.front();
928 for (FileGot
&got
: gots
) {
929 set_union(primGot
->relocs
, got
.global
);
930 set_union(primGot
->relocs
, got
.relocs
);
934 // Evaluate number of "page" entries in each GOT.
935 for (FileGot
&got
: gots
) {
936 for (std::pair
<const OutputSection
*, FileGot::PageBlock
> &p
:
938 const OutputSection
*os
= p
.first
;
939 uint64_t secSize
= 0;
940 for (SectionCommand
*cmd
: os
->commands
) {
941 if (auto *isd
= dyn_cast
<InputSectionDescription
>(cmd
))
942 for (InputSection
*isec
: isd
->sections
) {
943 uint64_t off
= alignToPowerOf2(secSize
, isec
->addralign
);
944 secSize
= off
+ isec
->getSize();
947 p
.second
.count
= getMipsPageCount(secSize
);
951 // Merge GOTs. Try to join as much as possible GOTs but do not exceed
952 // maximum GOT size. At first, try to fill the primary GOT because
953 // the primary GOT can be accessed in the most effective way. If it
954 // is not possible, try to fill the last GOT in the list, and finally
955 // create a new GOT if both attempts failed.
956 for (FileGot
&srcGot
: gots
) {
957 InputFile
*file
= srcGot
.file
;
958 if (tryMergeGots(mergedGots
.front(), srcGot
, true)) {
959 file
->mipsGotIndex
= 0;
961 // If this is the first time we failed to merge with the primary GOT,
962 // MergedGots.back() will also be the primary GOT. We must make sure not
963 // to try to merge again with isPrimary=false, as otherwise, if the
964 // inputs are just right, we could allow the primary GOT to become 1 or 2
965 // words bigger due to ignoring the header size.
966 if (mergedGots
.size() == 1 ||
967 !tryMergeGots(mergedGots
.back(), srcGot
, false)) {
968 mergedGots
.emplace_back();
969 std::swap(mergedGots
.back(), srcGot
);
971 file
->mipsGotIndex
= mergedGots
.size() - 1;
974 std::swap(gots
, mergedGots
);
976 // Reduce number of "reloc-only" entries in the primary GOT
977 // by subtracting "global" entries in the primary GOT.
978 primGot
= &gots
.front();
979 primGot
->relocs
.remove_if([&](const std::pair
<Symbol
*, size_t> &p
) {
980 return primGot
->global
.count(p
.first
);
983 // Calculate indexes for each GOT entry.
984 size_t index
= headerEntriesNum
;
985 for (FileGot
&got
: gots
) {
986 got
.startIndex
= &got
== primGot
? 0 : index
;
987 for (std::pair
<const OutputSection
*, FileGot::PageBlock
> &p
:
989 // For each output section referenced by GOT page relocations calculate
990 // and save into pagesMap an upper bound of MIPS GOT entries required
991 // to store page addresses of local symbols. We assume the worst case -
992 // each 64kb page of the output section has at least one GOT relocation
993 // against it. And take in account the case when the section intersects
995 p
.second
.firstIndex
= index
;
996 index
+= p
.second
.count
;
998 for (auto &p
: got
.local16
)
1000 for (auto &p
: got
.global
)
1002 for (auto &p
: got
.relocs
)
1004 for (auto &p
: got
.tls
)
1006 for (auto &p
: got
.dynTlsSymbols
) {
1012 // Update SymbolAux::gotIdx field to use this
1013 // value later in the `sortMipsSymbols` function.
1014 for (auto &p
: primGot
->global
) {
1015 if (p
.first
->auxIdx
== 0)
1016 p
.first
->allocateAux(ctx
);
1017 ctx
.symAux
.back().gotIdx
= p
.second
;
1019 for (auto &p
: primGot
->relocs
) {
1020 if (p
.first
->auxIdx
== 0)
1021 p
.first
->allocateAux(ctx
);
1022 ctx
.symAux
.back().gotIdx
= p
.second
;
1025 // Create dynamic relocations.
1026 for (FileGot
&got
: gots
) {
1027 // Create dynamic relocations for TLS entries.
1028 for (std::pair
<Symbol
*, size_t> &p
: got
.tls
) {
1029 Symbol
*s
= p
.first
;
1030 uint64_t offset
= p
.second
* ctx
.arg
.wordsize
;
1031 // When building a shared library we still need a dynamic relocation
1032 // for the TP-relative offset as we don't know how much other data will
1033 // be allocated before us in the static TLS block.
1034 if (s
->isPreemptible
|| ctx
.arg
.shared
)
1035 ctx
.mainPart
->relaDyn
->addReloc(
1036 {ctx
.target
->tlsGotRel
, this, offset
,
1037 DynamicReloc::AgainstSymbolWithTargetVA
, *s
, 0, R_ABS
});
1039 for (std::pair
<Symbol
*, size_t> &p
: got
.dynTlsSymbols
) {
1040 Symbol
*s
= p
.first
;
1041 uint64_t offset
= p
.second
* ctx
.arg
.wordsize
;
1043 if (!ctx
.arg
.shared
)
1045 ctx
.mainPart
->relaDyn
->addReloc(
1046 {ctx
.target
->tlsModuleIndexRel
, this, offset
});
1048 // When building a shared library we still need a dynamic relocation
1049 // for the module index. Therefore only checking for
1050 // S->isPreemptible is not sufficient (this happens e.g. for
1051 // thread-locals that have been marked as local through a linker script)
1052 if (!s
->isPreemptible
&& !ctx
.arg
.shared
)
1054 ctx
.mainPart
->relaDyn
->addSymbolReloc(ctx
.target
->tlsModuleIndexRel
,
1056 // However, we can skip writing the TLS offset reloc for non-preemptible
1057 // symbols since it is known even in shared libraries
1058 if (!s
->isPreemptible
)
1060 offset
+= ctx
.arg
.wordsize
;
1061 ctx
.mainPart
->relaDyn
->addSymbolReloc(ctx
.target
->tlsOffsetRel
, *this,
1066 // Do not create dynamic relocations for non-TLS
1067 // entries in the primary GOT.
1068 if (&got
== primGot
)
1071 // Dynamic relocations for "global" entries.
1072 for (const std::pair
<Symbol
*, size_t> &p
: got
.global
) {
1073 uint64_t offset
= p
.second
* ctx
.arg
.wordsize
;
1074 ctx
.mainPart
->relaDyn
->addSymbolReloc(ctx
.target
->relativeRel
, *this,
1079 // Dynamic relocations for "local" entries in case of PIC.
1080 for (const std::pair
<const OutputSection
*, FileGot::PageBlock
> &l
:
1082 size_t pageCount
= l
.second
.count
;
1083 for (size_t pi
= 0; pi
< pageCount
; ++pi
) {
1084 uint64_t offset
= (l
.second
.firstIndex
+ pi
) * ctx
.arg
.wordsize
;
1085 ctx
.mainPart
->relaDyn
->addReloc({ctx
.target
->relativeRel
, this, offset
,
1086 l
.first
, int64_t(pi
* 0x10000)});
1089 for (const std::pair
<GotEntry
, size_t> &p
: got
.local16
) {
1090 uint64_t offset
= p
.second
* ctx
.arg
.wordsize
;
1091 ctx
.mainPart
->relaDyn
->addReloc({ctx
.target
->relativeRel
, this, offset
,
1092 DynamicReloc::AddendOnlyWithTargetVA
,
1093 *p
.first
.first
, p
.first
.second
, R_ABS
});
1098 bool MipsGotSection::isNeeded() const {
1099 // We add the .got section to the result for dynamic MIPS target because
1100 // its address and properties are mentioned in the .dynamic section.
1101 return !ctx
.arg
.relocatable
;
1104 uint64_t MipsGotSection::getGp(const InputFile
*f
) const {
1105 // For files without related GOT or files refer a primary GOT
1106 // returns "common" _gp value. For secondary GOTs calculate
1107 // individual _gp values.
1108 if (!f
|| f
->mipsGotIndex
== uint32_t(-1) || f
->mipsGotIndex
== 0)
1109 return ctx
.sym
.mipsGp
->getVA(ctx
, 0);
1110 return getVA() + gots
[f
->mipsGotIndex
].startIndex
* ctx
.arg
.wordsize
+ 0x7ff0;
1113 void MipsGotSection::writeTo(uint8_t *buf
) {
1114 // Set the MSB of the second GOT slot. This is not required by any
1115 // MIPS ABI documentation, though.
1117 // There is a comment in glibc saying that "The MSB of got[1] of a
1118 // gnu object is set to identify gnu objects," and in GNU gold it
1119 // says "the second entry will be used by some runtime loaders".
1120 // But how this field is being used is unclear.
1122 // We are not really willing to mimic other linkers behaviors
1123 // without understanding why they do that, but because all files
1124 // generated by GNU tools have this special GOT value, and because
1125 // we've been doing this for years, it is probably a safe bet to
1126 // keep doing this for now. We really need to revisit this to see
1127 // if we had to do this.
1128 writeUint(ctx
, buf
+ ctx
.arg
.wordsize
,
1129 (uint64_t)1 << (ctx
.arg
.wordsize
* 8 - 1));
1130 for (const FileGot
&g
: gots
) {
1131 auto write
= [&](size_t i
, const Symbol
*s
, int64_t a
) {
1134 va
= s
->getVA(ctx
, a
);
1135 writeUint(ctx
, buf
+ i
* ctx
.arg
.wordsize
, va
);
1137 // Write 'page address' entries to the local part of the GOT.
1138 for (const std::pair
<const OutputSection
*, FileGot::PageBlock
> &l
:
1140 size_t pageCount
= l
.second
.count
;
1141 uint64_t firstPageAddr
= getMipsPageAddr(l
.first
->addr
);
1142 for (size_t pi
= 0; pi
< pageCount
; ++pi
)
1143 write(l
.second
.firstIndex
+ pi
, nullptr, firstPageAddr
+ pi
* 0x10000);
1145 // Local, global, TLS, reloc-only entries.
1146 // If TLS entry has a corresponding dynamic relocations, leave it
1147 // initialized by zero. Write down adjusted TLS symbol's values otherwise.
1148 // To calculate the adjustments use offsets for thread-local storage.
1149 // http://web.archive.org/web/20190324223224/https://www.linux-mips.org/wiki/NPTL
1150 for (const std::pair
<GotEntry
, size_t> &p
: g
.local16
)
1151 write(p
.second
, p
.first
.first
, p
.first
.second
);
1152 // Write VA to the primary GOT only. For secondary GOTs that
1153 // will be done by REL32 dynamic relocations.
1154 if (&g
== &gots
.front())
1155 for (const std::pair
<Symbol
*, size_t> &p
: g
.global
)
1156 write(p
.second
, p
.first
, 0);
1157 for (const std::pair
<Symbol
*, size_t> &p
: g
.relocs
)
1158 write(p
.second
, p
.first
, 0);
1159 for (const std::pair
<Symbol
*, size_t> &p
: g
.tls
)
1160 write(p
.second
, p
.first
,
1161 p
.first
->isPreemptible
|| ctx
.arg
.shared
? 0 : -0x7000);
1162 for (const std::pair
<Symbol
*, size_t> &p
: g
.dynTlsSymbols
) {
1163 if (p
.first
== nullptr && !ctx
.arg
.shared
)
1164 write(p
.second
, nullptr, 1);
1165 else if (p
.first
&& !p
.first
->isPreemptible
) {
1166 // If we are emitting a shared library with relocations we mustn't write
1167 // anything to the GOT here. When using Elf_Rel relocations the value
1168 // one will be treated as an addend and will cause crashes at runtime
1169 if (!ctx
.arg
.shared
)
1170 write(p
.second
, nullptr, 1);
1171 write(p
.second
+ 1, p
.first
, -0x8000);
1177 // On PowerPC the .plt section is used to hold the table of function addresses
1178 // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1179 // section. I don't know why we have a BSS style type for the section but it is
1180 // consistent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1181 GotPltSection::GotPltSection(Ctx
&ctx
)
1182 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_PROGBITS
,
1183 ctx
.arg
.wordsize
, ".got.plt") {
1184 if (ctx
.arg
.emachine
== EM_PPC
) {
1186 } else if (ctx
.arg
.emachine
== EM_PPC64
) {
1192 void GotPltSection::addEntry(Symbol
&sym
) {
1193 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1 &&
1194 ctx
.symAux
.back().pltIdx
== entries
.size());
1195 entries
.push_back(&sym
);
1198 size_t GotPltSection::getSize() const {
1199 return (ctx
.target
->gotPltHeaderEntriesNum
+ entries
.size()) *
1200 ctx
.target
->gotEntrySize
;
1203 void GotPltSection::writeTo(uint8_t *buf
) {
1204 ctx
.target
->writeGotPltHeader(buf
);
1205 buf
+= ctx
.target
->gotPltHeaderEntriesNum
* ctx
.target
->gotEntrySize
;
1206 for (const Symbol
*b
: entries
) {
1207 ctx
.target
->writeGotPlt(buf
, *b
);
1208 buf
+= ctx
.target
->gotEntrySize
;
1212 bool GotPltSection::isNeeded() const {
1213 // We need to emit GOTPLT even if it's empty if there's a relocation relative
1215 return !entries
.empty() || hasGotPltOffRel
;
1218 static StringRef
getIgotPltName(Ctx
&ctx
) {
1219 // On ARM the IgotPltSection is part of the GotSection.
1220 if (ctx
.arg
.emachine
== EM_ARM
)
1223 // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1224 // needs to be named the same.
1225 if (ctx
.arg
.emachine
== EM_PPC64
)
1231 // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1232 // with the IgotPltSection.
1233 IgotPltSection::IgotPltSection(Ctx
&ctx
)
1234 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
,
1235 ctx
.arg
.emachine
== EM_PPC64
? SHT_NOBITS
: SHT_PROGBITS
,
1236 ctx
.target
->gotEntrySize
, getIgotPltName(ctx
)) {}
1238 void IgotPltSection::addEntry(Symbol
&sym
) {
1239 assert(ctx
.symAux
.back().pltIdx
== entries
.size());
1240 entries
.push_back(&sym
);
1243 size_t IgotPltSection::getSize() const {
1244 return entries
.size() * ctx
.target
->gotEntrySize
;
1247 void IgotPltSection::writeTo(uint8_t *buf
) {
1248 for (const Symbol
*b
: entries
) {
1249 ctx
.target
->writeIgotPlt(buf
, *b
);
1250 buf
+= ctx
.target
->gotEntrySize
;
1254 StringTableSection::StringTableSection(Ctx
&ctx
, StringRef name
, bool dynamic
)
1255 : SyntheticSection(ctx
, dynamic
? (uint64_t)SHF_ALLOC
: 0, SHT_STRTAB
, 1,
1258 // ELF string tables start with a NUL byte.
1259 strings
.push_back("");
1260 stringMap
.try_emplace(CachedHashStringRef(""), 0);
1264 // Adds a string to the string table. If `hashIt` is true we hash and check for
1265 // duplicates. It is optional because the name of global symbols are already
1266 // uniqued and hashing them again has a big cost for a small value: uniquing
1267 // them with some other string that happens to be the same.
1268 unsigned StringTableSection::addString(StringRef s
, bool hashIt
) {
1270 auto r
= stringMap
.try_emplace(CachedHashStringRef(s
), size
);
1272 return r
.first
->second
;
1276 unsigned ret
= this->size
;
1277 this->size
= this->size
+ s
.size() + 1;
1278 strings
.push_back(s
);
1282 void StringTableSection::writeTo(uint8_t *buf
) {
1283 for (StringRef s
: strings
) {
1284 memcpy(buf
, s
.data(), s
.size());
1285 buf
[s
.size()] = '\0';
1286 buf
+= s
.size() + 1;
1290 // Returns the number of entries in .gnu.version_d: the number of
1291 // non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1.
1292 // Note that we don't support vd_cnt > 1 yet.
1293 static unsigned getVerDefNum(Ctx
&ctx
) {
1294 return namedVersionDefs(ctx
).size() + 1;
1297 template <class ELFT
>
1298 DynamicSection
<ELFT
>::DynamicSection(Ctx
&ctx
)
1299 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_DYNAMIC
,
1300 ctx
.arg
.wordsize
, ".dynamic") {
1301 this->entsize
= ELFT::Is64Bits
? 16 : 8;
1303 // .dynamic section is not writable on MIPS and on Fuchsia OS
1304 // which passes -z rodynamic.
1305 // See "Special Section" in Chapter 4 in the following document:
1306 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1307 if (ctx
.arg
.emachine
== EM_MIPS
|| ctx
.arg
.zRodynamic
)
1308 this->flags
= SHF_ALLOC
;
1311 // The output section .rela.dyn may include these synthetic sections:
1314 // - ctx.in.relaPlt: this is included if a linker script places .rela.plt inside
1317 // DT_RELASZ is the total size of the included sections.
1318 static uint64_t addRelaSz(Ctx
&ctx
, const RelocationBaseSection
&relaDyn
) {
1319 size_t size
= relaDyn
.getSize();
1320 if (ctx
.in
.relaPlt
->getParent() == relaDyn
.getParent())
1321 size
+= ctx
.in
.relaPlt
->getSize();
1325 // A Linker script may assign the RELA relocation sections to the same
1326 // output section. When this occurs we cannot just use the OutputSection
1327 // Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
1328 // overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1329 static uint64_t addPltRelSz(Ctx
&ctx
) { return ctx
.in
.relaPlt
->getSize(); }
1331 // Add remaining entries to complete .dynamic contents.
1332 template <class ELFT
>
1333 std::vector
<std::pair
<int32_t, uint64_t>>
1334 DynamicSection
<ELFT
>::computeContents() {
1335 elf::Partition
&part
= getPartition(ctx
);
1336 bool isMain
= part
.name
.empty();
1337 std::vector
<std::pair
<int32_t, uint64_t>> entries
;
1339 auto addInt
= [&](int32_t tag
, uint64_t val
) {
1340 entries
.emplace_back(tag
, val
);
1342 auto addInSec
= [&](int32_t tag
, const InputSection
&sec
) {
1343 entries
.emplace_back(tag
, sec
.getVA());
1346 for (StringRef s
: ctx
.arg
.filterList
)
1347 addInt(DT_FILTER
, part
.dynStrTab
->addString(s
));
1348 for (StringRef s
: ctx
.arg
.auxiliaryList
)
1349 addInt(DT_AUXILIARY
, part
.dynStrTab
->addString(s
));
1351 if (!ctx
.arg
.rpath
.empty())
1352 addInt(ctx
.arg
.enableNewDtags
? DT_RUNPATH
: DT_RPATH
,
1353 part
.dynStrTab
->addString(ctx
.arg
.rpath
));
1355 for (SharedFile
*file
: ctx
.sharedFiles
)
1357 addInt(DT_NEEDED
, part
.dynStrTab
->addString(file
->soName
));
1360 if (!ctx
.arg
.soName
.empty())
1361 addInt(DT_SONAME
, part
.dynStrTab
->addString(ctx
.arg
.soName
));
1363 if (!ctx
.arg
.soName
.empty())
1364 addInt(DT_NEEDED
, part
.dynStrTab
->addString(ctx
.arg
.soName
));
1365 addInt(DT_SONAME
, part
.dynStrTab
->addString(part
.name
));
1368 // Set DT_FLAGS and DT_FLAGS_1.
1369 uint32_t dtFlags
= 0;
1370 uint32_t dtFlags1
= 0;
1371 if (ctx
.arg
.bsymbolic
== BsymbolicKind::All
)
1372 dtFlags
|= DF_SYMBOLIC
;
1373 if (ctx
.arg
.zGlobal
)
1374 dtFlags1
|= DF_1_GLOBAL
;
1375 if (ctx
.arg
.zInitfirst
)
1376 dtFlags1
|= DF_1_INITFIRST
;
1377 if (ctx
.arg
.zInterpose
)
1378 dtFlags1
|= DF_1_INTERPOSE
;
1379 if (ctx
.arg
.zNodefaultlib
)
1380 dtFlags1
|= DF_1_NODEFLIB
;
1381 if (ctx
.arg
.zNodelete
)
1382 dtFlags1
|= DF_1_NODELETE
;
1383 if (ctx
.arg
.zNodlopen
)
1384 dtFlags1
|= DF_1_NOOPEN
;
1386 dtFlags1
|= DF_1_PIE
;
1388 dtFlags
|= DF_BIND_NOW
;
1389 dtFlags1
|= DF_1_NOW
;
1391 if (ctx
.arg
.zOrigin
) {
1392 dtFlags
|= DF_ORIGIN
;
1393 dtFlags1
|= DF_1_ORIGIN
;
1396 dtFlags
|= DF_TEXTREL
;
1397 if (ctx
.hasTlsIe
&& ctx
.arg
.shared
)
1398 dtFlags
|= DF_STATIC_TLS
;
1401 addInt(DT_FLAGS
, dtFlags
);
1403 addInt(DT_FLAGS_1
, dtFlags1
);
1405 // DT_DEBUG is a pointer to debug information used by debuggers at runtime. We
1406 // need it for each process, so we don't write it for DSOs. The loader writes
1407 // the pointer into this entry.
1409 // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1410 // systems (currently only Fuchsia OS) provide other means to give the
1411 // debugger this information. Such systems may choose make .dynamic read-only.
1412 // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1413 if (!ctx
.arg
.shared
&& !ctx
.arg
.relocatable
&& !ctx
.arg
.zRodynamic
)
1414 addInt(DT_DEBUG
, 0);
1416 if (part
.relaDyn
->isNeeded()) {
1417 addInSec(part
.relaDyn
->dynamicTag
, *part
.relaDyn
);
1418 entries
.emplace_back(part
.relaDyn
->sizeDynamicTag
,
1419 addRelaSz(ctx
, *part
.relaDyn
));
1421 bool isRela
= ctx
.arg
.isRela
;
1422 addInt(isRela
? DT_RELAENT
: DT_RELENT
,
1423 isRela
? sizeof(Elf_Rela
) : sizeof(Elf_Rel
));
1425 // MIPS dynamic loader does not support RELCOUNT tag.
1426 // The problem is in the tight relation between dynamic
1427 // relocations and GOT. So do not emit this tag on MIPS.
1428 if (ctx
.arg
.emachine
!= EM_MIPS
) {
1429 size_t numRelativeRels
= part
.relaDyn
->getRelativeRelocCount();
1430 if (ctx
.arg
.zCombreloc
&& numRelativeRels
)
1431 addInt(isRela
? DT_RELACOUNT
: DT_RELCOUNT
, numRelativeRels
);
1434 if (part
.relrDyn
&& part
.relrDyn
->getParent() &&
1435 !part
.relrDyn
->relocs
.empty()) {
1436 addInSec(ctx
.arg
.useAndroidRelrTags
? DT_ANDROID_RELR
: DT_RELR
,
1438 addInt(ctx
.arg
.useAndroidRelrTags
? DT_ANDROID_RELRSZ
: DT_RELRSZ
,
1439 part
.relrDyn
->getParent()->size
);
1440 addInt(ctx
.arg
.useAndroidRelrTags
? DT_ANDROID_RELRENT
: DT_RELRENT
,
1443 if (part
.relrAuthDyn
&& part
.relrAuthDyn
->getParent() &&
1444 !part
.relrAuthDyn
->relocs
.empty()) {
1445 addInSec(DT_AARCH64_AUTH_RELR
, *part
.relrAuthDyn
);
1446 addInt(DT_AARCH64_AUTH_RELRSZ
, part
.relrAuthDyn
->getParent()->size
);
1447 addInt(DT_AARCH64_AUTH_RELRENT
, sizeof(Elf_Relr
));
1449 if (isMain
&& ctx
.in
.relaPlt
->isNeeded()) {
1450 addInSec(DT_JMPREL
, *ctx
.in
.relaPlt
);
1451 entries
.emplace_back(DT_PLTRELSZ
, addPltRelSz(ctx
));
1452 switch (ctx
.arg
.emachine
) {
1454 addInSec(DT_MIPS_PLTGOT
, *ctx
.in
.gotPlt
);
1457 addInSec(DT_PLTGOT
, *ctx
.in
.got
);
1460 addInSec(DT_PLTGOT
, *ctx
.in
.plt
);
1463 if (llvm::find_if(ctx
.in
.relaPlt
->relocs
, [&ctx
= ctx
](
1464 const DynamicReloc
&r
) {
1465 return r
.type
== ctx
.target
->pltRel
&&
1466 r
.sym
->stOther
& STO_AARCH64_VARIANT_PCS
;
1467 }) != ctx
.in
.relaPlt
->relocs
.end())
1468 addInt(DT_AARCH64_VARIANT_PCS
, 0);
1469 addInSec(DT_PLTGOT
, *ctx
.in
.gotPlt
);
1472 if (llvm::any_of(ctx
.in
.relaPlt
->relocs
, [&ctx
= ctx
](
1473 const DynamicReloc
&r
) {
1474 return r
.type
== ctx
.target
->pltRel
&&
1475 (r
.sym
->stOther
& STO_RISCV_VARIANT_CC
);
1477 addInt(DT_RISCV_VARIANT_CC
, 0);
1480 addInSec(DT_PLTGOT
, *ctx
.in
.gotPlt
);
1483 addInt(DT_PLTREL
, ctx
.arg
.isRela
? DT_RELA
: DT_REL
);
1486 if (ctx
.arg
.emachine
== EM_AARCH64
) {
1487 if (ctx
.arg
.andFeatures
& GNU_PROPERTY_AARCH64_FEATURE_1_BTI
)
1488 addInt(DT_AARCH64_BTI_PLT
, 0);
1489 if (ctx
.arg
.zPacPlt
)
1490 addInt(DT_AARCH64_PAC_PLT
, 0);
1492 if (hasMemtag(ctx
)) {
1493 addInt(DT_AARCH64_MEMTAG_MODE
, ctx
.arg
.androidMemtagMode
== NT_MEMTAG_LEVEL_ASYNC
);
1494 addInt(DT_AARCH64_MEMTAG_HEAP
, ctx
.arg
.androidMemtagHeap
);
1495 addInt(DT_AARCH64_MEMTAG_STACK
, ctx
.arg
.androidMemtagStack
);
1496 if (ctx
.mainPart
->memtagGlobalDescriptors
->isNeeded()) {
1497 addInSec(DT_AARCH64_MEMTAG_GLOBALS
,
1498 *ctx
.mainPart
->memtagGlobalDescriptors
);
1499 addInt(DT_AARCH64_MEMTAG_GLOBALSSZ
,
1500 ctx
.mainPart
->memtagGlobalDescriptors
->getSize());
1505 addInSec(DT_SYMTAB
, *part
.dynSymTab
);
1506 addInt(DT_SYMENT
, sizeof(Elf_Sym
));
1507 addInSec(DT_STRTAB
, *part
.dynStrTab
);
1508 addInt(DT_STRSZ
, part
.dynStrTab
->getSize());
1510 addInt(DT_TEXTREL
, 0);
1511 if (part
.gnuHashTab
&& part
.gnuHashTab
->getParent())
1512 addInSec(DT_GNU_HASH
, *part
.gnuHashTab
);
1513 if (part
.hashTab
&& part
.hashTab
->getParent())
1514 addInSec(DT_HASH
, *part
.hashTab
);
1517 if (ctx
.out
.preinitArray
) {
1518 addInt(DT_PREINIT_ARRAY
, ctx
.out
.preinitArray
->addr
);
1519 addInt(DT_PREINIT_ARRAYSZ
, ctx
.out
.preinitArray
->size
);
1521 if (ctx
.out
.initArray
) {
1522 addInt(DT_INIT_ARRAY
, ctx
.out
.initArray
->addr
);
1523 addInt(DT_INIT_ARRAYSZ
, ctx
.out
.initArray
->size
);
1525 if (ctx
.out
.finiArray
) {
1526 addInt(DT_FINI_ARRAY
, ctx
.out
.finiArray
->addr
);
1527 addInt(DT_FINI_ARRAYSZ
, ctx
.out
.finiArray
->size
);
1530 if (Symbol
*b
= ctx
.symtab
->find(ctx
.arg
.init
))
1532 addInt(DT_INIT
, b
->getVA(ctx
));
1533 if (Symbol
*b
= ctx
.symtab
->find(ctx
.arg
.fini
))
1535 addInt(DT_FINI
, b
->getVA(ctx
));
1538 if (part
.verSym
&& part
.verSym
->isNeeded())
1539 addInSec(DT_VERSYM
, *part
.verSym
);
1540 if (part
.verDef
&& part
.verDef
->isLive()) {
1541 addInSec(DT_VERDEF
, *part
.verDef
);
1542 addInt(DT_VERDEFNUM
, getVerDefNum(ctx
));
1544 if (part
.verNeed
&& part
.verNeed
->isNeeded()) {
1545 addInSec(DT_VERNEED
, *part
.verNeed
);
1546 unsigned needNum
= 0;
1547 for (SharedFile
*f
: ctx
.sharedFiles
)
1548 if (!f
->vernauxs
.empty())
1550 addInt(DT_VERNEEDNUM
, needNum
);
1553 if (ctx
.arg
.emachine
== EM_MIPS
) {
1554 addInt(DT_MIPS_RLD_VERSION
, 1);
1555 addInt(DT_MIPS_FLAGS
, RHF_NOTPOT
);
1556 addInt(DT_MIPS_BASE_ADDRESS
, ctx
.target
->getImageBase());
1557 addInt(DT_MIPS_SYMTABNO
, part
.dynSymTab
->getNumSymbols());
1558 addInt(DT_MIPS_LOCAL_GOTNO
, ctx
.in
.mipsGot
->getLocalEntriesNum());
1560 if (const Symbol
*b
= ctx
.in
.mipsGot
->getFirstGlobalEntry())
1561 addInt(DT_MIPS_GOTSYM
, b
->dynsymIndex
);
1563 addInt(DT_MIPS_GOTSYM
, part
.dynSymTab
->getNumSymbols());
1564 addInSec(DT_PLTGOT
, *ctx
.in
.mipsGot
);
1565 if (ctx
.in
.mipsRldMap
) {
1567 addInSec(DT_MIPS_RLD_MAP
, *ctx
.in
.mipsRldMap
);
1568 // Store the offset to the .rld_map section
1569 // relative to the address of the tag.
1570 addInt(DT_MIPS_RLD_MAP_REL
,
1571 ctx
.in
.mipsRldMap
->getVA() - (getVA() + entries
.size() * entsize
));
1575 // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1576 // glibc assumes the old-style BSS PLT layout which we don't support.
1577 if (ctx
.arg
.emachine
== EM_PPC
)
1578 addInSec(DT_PPC_GOT
, *ctx
.in
.got
);
1580 // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1581 if (ctx
.arg
.emachine
== EM_PPC64
&& ctx
.in
.plt
->isNeeded()) {
1582 // The Glink tag points to 32 bytes before the first lazy symbol resolution
1583 // stub, which starts directly after the header.
1584 addInt(DT_PPC64_GLINK
,
1585 ctx
.in
.plt
->getVA() + ctx
.target
->pltHeaderSize
- 32);
1588 if (ctx
.arg
.emachine
== EM_PPC64
)
1589 addInt(DT_PPC64_OPT
, ctx
.target
->ppc64DynamicSectionOpt
);
1595 template <class ELFT
> void DynamicSection
<ELFT
>::finalizeContents() {
1596 if (OutputSection
*sec
= getPartition(ctx
).dynStrTab
->getParent())
1597 getParent()->link
= sec
->sectionIndex
;
1598 this->size
= computeContents().size() * this->entsize
;
1601 template <class ELFT
> void DynamicSection
<ELFT
>::writeTo(uint8_t *buf
) {
1602 auto *p
= reinterpret_cast<Elf_Dyn
*>(buf
);
1604 for (std::pair
<int32_t, uint64_t> kv
: computeContents()) {
1605 p
->d_tag
= kv
.first
;
1606 p
->d_un
.d_val
= kv
.second
;
1611 uint64_t DynamicReloc::getOffset() const {
1612 return inputSec
->getVA(offsetInSec
);
1615 int64_t DynamicReloc::computeAddend(Ctx
&ctx
) const {
1618 assert(sym
== nullptr);
1621 assert(sym
!= nullptr);
1623 case AddendOnlyWithTargetVA
:
1624 case AgainstSymbolWithTargetVA
: {
1625 uint64_t ca
= inputSec
->getRelocTargetVA(
1626 ctx
, Relocation
{expr
, type
, 0, addend
, sym
}, getOffset());
1627 return ctx
.arg
.is64
? ca
: SignExtend64
<32>(ca
);
1629 case MipsMultiGotPage
:
1630 assert(sym
== nullptr);
1631 return getMipsPageAddr(outputSec
->addr
) + addend
;
1633 llvm_unreachable("Unknown DynamicReloc::Kind enum");
1636 uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection
*symTab
) const {
1637 if (!needsDynSymIndex())
1640 size_t index
= symTab
->getSymbolIndex(*sym
);
1641 assert((index
!= 0 ||
1642 (type
!= symTab
->ctx
.target
->gotRel
&&
1643 type
!= symTab
->ctx
.target
->pltRel
) ||
1644 !symTab
->ctx
.mainPart
->dynSymTab
->getParent()) &&
1645 "GOT or PLT relocation must refer to symbol in dynamic symbol table");
1649 RelocationBaseSection::RelocationBaseSection(Ctx
&ctx
, StringRef name
,
1650 uint32_t type
, int32_t dynamicTag
,
1651 int32_t sizeDynamicTag
,
1653 unsigned concurrency
)
1654 : SyntheticSection(ctx
, SHF_ALLOC
, type
, ctx
.arg
.wordsize
, name
),
1655 dynamicTag(dynamicTag
), sizeDynamicTag(sizeDynamicTag
),
1656 relocsVec(concurrency
), combreloc(combreloc
) {}
1658 void RelocationBaseSection::addSymbolReloc(
1659 RelType dynType
, InputSectionBase
&isec
, uint64_t offsetInSec
, Symbol
&sym
,
1660 int64_t addend
, std::optional
<RelType
> addendRelType
) {
1661 addReloc(DynamicReloc::AgainstSymbol
, dynType
, isec
, offsetInSec
, sym
, addend
,
1662 R_ADDEND
, addendRelType
? *addendRelType
: ctx
.target
->noneRel
);
1665 void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(
1666 RelType dynType
, InputSectionBase
&isec
, uint64_t offsetInSec
, Symbol
&sym
,
1667 RelType addendRelType
) {
1668 // No need to write an addend to the section for preemptible symbols.
1669 if (sym
.isPreemptible
)
1670 addReloc({dynType
, &isec
, offsetInSec
, DynamicReloc::AgainstSymbol
, sym
, 0,
1673 addReloc(DynamicReloc::AddendOnlyWithTargetVA
, dynType
, isec
, offsetInSec
,
1674 sym
, 0, R_ABS
, addendRelType
);
1677 void RelocationBaseSection::mergeRels() {
1678 size_t newSize
= relocs
.size();
1679 for (const auto &v
: relocsVec
)
1680 newSize
+= v
.size();
1681 relocs
.reserve(newSize
);
1682 for (const auto &v
: relocsVec
)
1683 llvm::append_range(relocs
, v
);
1687 void RelocationBaseSection::partitionRels() {
1690 const RelType relativeRel
= ctx
.target
->relativeRel
;
1692 std::stable_partition(relocs
.begin(), relocs
.end(),
1693 [=](auto &r
) { return r
.type
== relativeRel
; }) -
1697 void RelocationBaseSection::finalizeContents() {
1698 SymbolTableBaseSection
*symTab
= getPartition(ctx
).dynSymTab
.get();
1700 // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1701 // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1703 if (symTab
&& symTab
->getParent())
1704 getParent()->link
= symTab
->getParent()->sectionIndex
;
1706 getParent()->link
= 0;
1708 if (ctx
.in
.relaPlt
.get() == this && ctx
.in
.gotPlt
->getParent()) {
1709 getParent()->flags
|= ELF::SHF_INFO_LINK
;
1710 getParent()->info
= ctx
.in
.gotPlt
->getParent()->sectionIndex
;
1714 void DynamicReloc::computeRaw(Ctx
&ctx
, SymbolTableBaseSection
*symt
) {
1715 r_offset
= getOffset();
1716 r_sym
= getSymIndex(symt
);
1717 addend
= computeAddend(ctx
);
1718 kind
= AddendOnly
; // Catch errors
1721 void RelocationBaseSection::computeRels() {
1722 SymbolTableBaseSection
*symTab
= getPartition(ctx
).dynSymTab
.get();
1723 parallelForEach(relocs
, [&ctx
= ctx
, symTab
](DynamicReloc
&rel
) {
1724 rel
.computeRaw(ctx
, symTab
);
1727 auto irelative
= std::stable_partition(
1728 relocs
.begin() + numRelativeRelocs
, relocs
.end(),
1729 [t
= ctx
.target
->iRelativeRel
](auto &r
) { return r
.type
!= t
; });
1731 // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1732 // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1733 // is to make results easier to read.
1735 auto nonRelative
= relocs
.begin() + numRelativeRelocs
;
1736 parallelSort(relocs
.begin(), nonRelative
,
1737 [&](auto &a
, auto &b
) { return a
.r_offset
< b
.r_offset
; });
1738 // Non-relative relocations are few, so don't bother with parallelSort.
1739 llvm::sort(nonRelative
, irelative
, [&](auto &a
, auto &b
) {
1740 return std::tie(a
.r_sym
, a
.r_offset
) < std::tie(b
.r_sym
, b
.r_offset
);
1745 template <class ELFT
>
1746 RelocationSection
<ELFT
>::RelocationSection(Ctx
&ctx
, StringRef name
,
1747 bool combreloc
, unsigned concurrency
)
1748 : RelocationBaseSection(ctx
, name
, ctx
.arg
.isRela
? SHT_RELA
: SHT_REL
,
1749 ctx
.arg
.isRela
? DT_RELA
: DT_REL
,
1750 ctx
.arg
.isRela
? DT_RELASZ
: DT_RELSZ
, combreloc
,
1752 this->entsize
= ctx
.arg
.isRela
? sizeof(Elf_Rela
) : sizeof(Elf_Rel
);
1755 template <class ELFT
> void RelocationSection
<ELFT
>::writeTo(uint8_t *buf
) {
1757 for (const DynamicReloc
&rel
: relocs
) {
1758 auto *p
= reinterpret_cast<Elf_Rela
*>(buf
);
1759 p
->r_offset
= rel
.r_offset
;
1760 p
->setSymbolAndType(rel
.r_sym
, rel
.type
, ctx
.arg
.isMips64EL
);
1762 p
->r_addend
= rel
.addend
;
1763 buf
+= ctx
.arg
.isRela
? sizeof(Elf_Rela
) : sizeof(Elf_Rel
);
1767 RelrBaseSection::RelrBaseSection(Ctx
&ctx
, unsigned concurrency
,
1772 ? SHT_AARCH64_AUTH_RELR
1773 : (ctx
.arg
.useAndroidRelrTags
? SHT_ANDROID_RELR
: SHT_RELR
),
1774 ctx
.arg
.wordsize
, isAArch64Auth
? ".relr.auth.dyn" : ".relr.dyn"),
1775 relocsVec(concurrency
) {}
1777 void RelrBaseSection::mergeRels() {
1778 size_t newSize
= relocs
.size();
1779 for (const auto &v
: relocsVec
)
1780 newSize
+= v
.size();
1781 relocs
.reserve(newSize
);
1782 for (const auto &v
: relocsVec
)
1783 llvm::append_range(relocs
, v
);
1787 template <class ELFT
>
1788 AndroidPackedRelocationSection
<ELFT
>::AndroidPackedRelocationSection(
1789 Ctx
&ctx
, StringRef name
, unsigned concurrency
)
1790 : RelocationBaseSection(
1791 ctx
, name
, ctx
.arg
.isRela
? SHT_ANDROID_RELA
: SHT_ANDROID_REL
,
1792 ctx
.arg
.isRela
? DT_ANDROID_RELA
: DT_ANDROID_REL
,
1793 ctx
.arg
.isRela
? DT_ANDROID_RELASZ
: DT_ANDROID_RELSZ
,
1794 /*combreloc=*/false, concurrency
) {
1798 template <class ELFT
>
1799 bool AndroidPackedRelocationSection
<ELFT
>::updateAllocSize(Ctx
&ctx
) {
1800 // This function computes the contents of an Android-format packed relocation
1803 // This format compresses relocations by using relocation groups to factor out
1804 // fields that are common between relocations and storing deltas from previous
1805 // relocations in SLEB128 format (which has a short representation for small
1806 // numbers). A good example of a relocation type with common fields is
1807 // R_*_RELATIVE, which is normally used to represent function pointers in
1808 // vtables. In the REL format, each relative relocation has the same r_info
1809 // field, and is only different from other relative relocations in terms of
1810 // the r_offset field. By sorting relocations by offset, grouping them by
1811 // r_info and representing each relocation with only the delta from the
1812 // previous offset, each 8-byte relocation can be compressed to as little as 1
1813 // byte (or less with run-length encoding). This relocation packer was able to
1814 // reduce the size of the relocation section in an Android Chromium DSO from
1815 // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1817 // A relocation section consists of a header containing the literal bytes
1818 // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1819 // elements are the total number of relocations in the section and an initial
1820 // r_offset value. The remaining elements define a sequence of relocation
1821 // groups. Each relocation group starts with a header consisting of the
1822 // following elements:
1824 // - the number of relocations in the relocation group
1825 // - flags for the relocation group
1826 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1827 // for each relocation in the group.
1828 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1829 // field for each relocation in the group.
1830 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1831 // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1832 // each relocation in the group.
1834 // Following the relocation group header are descriptions of each of the
1835 // relocations in the group. They consist of the following elements:
1837 // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1838 // delta for this relocation.
1839 // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1840 // field for this relocation.
1841 // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1842 // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1845 size_t oldSize
= relocData
.size();
1847 relocData
= {'A', 'P', 'S', '2'};
1848 raw_svector_ostream
os(relocData
);
1849 auto add
= [&](int64_t v
) { encodeSLEB128(v
, os
); };
1851 // The format header includes the number of relocations and the initial
1852 // offset (we set this to zero because the first relocation group will
1853 // perform the initial adjustment).
1857 std::vector
<Elf_Rela
> relatives
, nonRelatives
;
1859 for (const DynamicReloc
&rel
: relocs
) {
1861 r
.r_offset
= rel
.getOffset();
1862 r
.setSymbolAndType(rel
.getSymIndex(getPartition(ctx
).dynSymTab
.get()),
1864 r
.r_addend
= ctx
.arg
.isRela
? rel
.computeAddend(ctx
) : 0;
1866 if (r
.getType(ctx
.arg
.isMips64EL
) == ctx
.target
->relativeRel
)
1867 relatives
.push_back(r
);
1869 nonRelatives
.push_back(r
);
1872 llvm::sort(relatives
, [](const Elf_Rel
&a
, const Elf_Rel
&b
) {
1873 return a
.r_offset
< b
.r_offset
;
1876 // Try to find groups of relative relocations which are spaced one word
1877 // apart from one another. These generally correspond to vtable entries. The
1878 // format allows these groups to be encoded using a sort of run-length
1879 // encoding, but each group will cost 7 bytes in addition to the offset from
1880 // the previous group, so it is only profitable to do this for groups of
1881 // size 8 or larger.
1882 std::vector
<Elf_Rela
> ungroupedRelatives
;
1883 std::vector
<std::vector
<Elf_Rela
>> relativeGroups
;
1884 for (auto i
= relatives
.begin(), e
= relatives
.end(); i
!= e
;) {
1885 std::vector
<Elf_Rela
> group
;
1887 group
.push_back(*i
++);
1888 } while (i
!= e
&& (i
- 1)->r_offset
+ ctx
.arg
.wordsize
== i
->r_offset
);
1890 if (group
.size() < 8)
1891 ungroupedRelatives
.insert(ungroupedRelatives
.end(), group
.begin(),
1894 relativeGroups
.emplace_back(std::move(group
));
1897 // For non-relative relocations, we would like to:
1898 // 1. Have relocations with the same symbol offset to be consecutive, so
1899 // that the runtime linker can speed-up symbol lookup by implementing an
1901 // 2. Group relocations by r_info to reduce the size of the relocation
1903 // Since the symbol offset is the high bits in r_info, sorting by r_info
1904 // allows us to do both.
1906 // For Rela, we also want to sort by r_addend when r_info is the same. This
1907 // enables us to group by r_addend as well.
1908 llvm::sort(nonRelatives
, [](const Elf_Rela
&a
, const Elf_Rela
&b
) {
1909 if (a
.r_info
!= b
.r_info
)
1910 return a
.r_info
< b
.r_info
;
1911 if (a
.r_addend
!= b
.r_addend
)
1912 return a
.r_addend
< b
.r_addend
;
1913 return a
.r_offset
< b
.r_offset
;
1916 // Group relocations with the same r_info. Note that each group emits a group
1917 // header and that may make the relocation section larger. It is hard to
1918 // estimate the size of a group header as the encoded size of that varies
1919 // based on r_info. However, we can approximate this trade-off by the number
1920 // of values encoded. Each group header contains 3 values, and each relocation
1921 // in a group encodes one less value, as compared to when it is not grouped.
1922 // Therefore, we only group relocations if there are 3 or more of them with
1925 // For Rela, the addend for most non-relative relocations is zero, and thus we
1926 // can usually get a smaller relocation section if we group relocations with 0
1928 std::vector
<Elf_Rela
> ungroupedNonRelatives
;
1929 std::vector
<std::vector
<Elf_Rela
>> nonRelativeGroups
;
1930 for (auto i
= nonRelatives
.begin(), e
= nonRelatives
.end(); i
!= e
;) {
1932 while (j
!= e
&& i
->r_info
== j
->r_info
&&
1933 (!ctx
.arg
.isRela
|| i
->r_addend
== j
->r_addend
))
1935 if (j
- i
< 3 || (ctx
.arg
.isRela
&& i
->r_addend
!= 0))
1936 ungroupedNonRelatives
.insert(ungroupedNonRelatives
.end(), i
, j
);
1938 nonRelativeGroups
.emplace_back(i
, j
);
1942 // Sort ungrouped relocations by offset to minimize the encoded length.
1943 llvm::sort(ungroupedNonRelatives
, [](const Elf_Rela
&a
, const Elf_Rela
&b
) {
1944 return a
.r_offset
< b
.r_offset
;
1947 unsigned hasAddendIfRela
=
1948 ctx
.arg
.isRela
? RELOCATION_GROUP_HAS_ADDEND_FLAG
: 0;
1950 uint64_t offset
= 0;
1951 uint64_t addend
= 0;
1953 // Emit the run-length encoding for the groups of adjacent relative
1954 // relocations. Each group is represented using two groups in the packed
1955 // format. The first is used to set the current offset to the start of the
1956 // group (and also encodes the first relocation), and the second encodes the
1957 // remaining relocations.
1958 for (std::vector
<Elf_Rela
> &g
: relativeGroups
) {
1959 // The first relocation in the group.
1961 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
|
1962 RELOCATION_GROUPED_BY_INFO_FLAG
| hasAddendIfRela
);
1963 add(g
[0].r_offset
- offset
);
1964 add(ctx
.target
->relativeRel
);
1965 if (ctx
.arg
.isRela
) {
1966 add(g
[0].r_addend
- addend
);
1967 addend
= g
[0].r_addend
;
1970 // The remaining relocations.
1972 add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
|
1973 RELOCATION_GROUPED_BY_INFO_FLAG
| hasAddendIfRela
);
1974 add(ctx
.arg
.wordsize
);
1975 add(ctx
.target
->relativeRel
);
1976 if (ctx
.arg
.isRela
) {
1977 for (const auto &i
: llvm::drop_begin(g
)) {
1978 add(i
.r_addend
- addend
);
1979 addend
= i
.r_addend
;
1983 offset
= g
.back().r_offset
;
1986 // Now the ungrouped relatives.
1987 if (!ungroupedRelatives
.empty()) {
1988 add(ungroupedRelatives
.size());
1989 add(RELOCATION_GROUPED_BY_INFO_FLAG
| hasAddendIfRela
);
1990 add(ctx
.target
->relativeRel
);
1991 for (Elf_Rela
&r
: ungroupedRelatives
) {
1992 add(r
.r_offset
- offset
);
1993 offset
= r
.r_offset
;
1994 if (ctx
.arg
.isRela
) {
1995 add(r
.r_addend
- addend
);
1996 addend
= r
.r_addend
;
2001 // Grouped non-relatives.
2002 for (ArrayRef
<Elf_Rela
> g
: nonRelativeGroups
) {
2004 add(RELOCATION_GROUPED_BY_INFO_FLAG
);
2006 for (const Elf_Rela
&r
: g
) {
2007 add(r
.r_offset
- offset
);
2008 offset
= r
.r_offset
;
2013 // Finally the ungrouped non-relative relocations.
2014 if (!ungroupedNonRelatives
.empty()) {
2015 add(ungroupedNonRelatives
.size());
2016 add(hasAddendIfRela
);
2017 for (Elf_Rela
&r
: ungroupedNonRelatives
) {
2018 add(r
.r_offset
- offset
);
2019 offset
= r
.r_offset
;
2021 if (ctx
.arg
.isRela
) {
2022 add(r
.r_addend
- addend
);
2023 addend
= r
.r_addend
;
2028 // Don't allow the section to shrink; otherwise the size of the section can
2029 // oscillate infinitely.
2030 if (relocData
.size() < oldSize
)
2031 relocData
.append(oldSize
- relocData
.size(), 0);
2033 // Returns whether the section size changed. We need to keep recomputing both
2034 // section layout and the contents of this section until the size converges
2035 // because changing this section's size can affect section layout, which in
2036 // turn can affect the sizes of the LEB-encoded integers stored in this
2038 return relocData
.size() != oldSize
;
2041 template <class ELFT
>
2042 RelrSection
<ELFT
>::RelrSection(Ctx
&ctx
, unsigned concurrency
,
2044 : RelrBaseSection(ctx
, concurrency
, isAArch64Auth
) {
2045 this->entsize
= ctx
.arg
.wordsize
;
2048 template <class ELFT
> bool RelrSection
<ELFT
>::updateAllocSize(Ctx
&ctx
) {
2049 // This function computes the contents of an SHT_RELR packed relocation
2052 // Proposal for adding SHT_RELR sections to generic-abi is here:
2053 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
2055 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
2056 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
2058 // i.e. start with an address, followed by any number of bitmaps. The address
2059 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
2060 // relocations each, at subsequent offsets following the last address entry.
2062 // The bitmap entries must have 1 in the least significant bit. The assumption
2063 // here is that an address cannot have 1 in lsb. Odd addresses are not
2066 // Excluding the least significant bit in the bitmap, each non-zero bit in
2067 // the bitmap represents a relocation to be applied to a corresponding machine
2068 // word that follows the base address word. The second least significant bit
2069 // represents the machine word immediately following the initial address, and
2070 // each bit that follows represents the next word, in linear order. As such,
2071 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
2072 // 63 relocations in a 64-bit object.
2074 // This encoding has a couple of interesting properties:
2075 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
2076 // even means address, odd means bitmap.
2077 // 2. Just a simple list of addresses is a valid encoding.
2079 size_t oldSize
= relrRelocs
.size();
2082 const size_t wordsize
= sizeof(typename
ELFT::uint
);
2084 // Number of bits to use for the relocation offsets bitmap.
2085 // Must be either 63 or 31.
2086 const size_t nBits
= wordsize
* 8 - 1;
2088 // Get offsets for all relative relocations and sort them.
2089 std::unique_ptr
<uint64_t[]> offsets(new uint64_t[relocs
.size()]);
2090 for (auto [i
, r
] : llvm::enumerate(relocs
))
2091 offsets
[i
] = r
.getOffset();
2092 llvm::sort(offsets
.get(), offsets
.get() + relocs
.size());
2094 // For each leading relocation, find following ones that can be folded
2095 // as a bitmap and fold them.
2096 for (size_t i
= 0, e
= relocs
.size(); i
!= e
;) {
2097 // Add a leading relocation.
2098 relrRelocs
.push_back(Elf_Relr(offsets
[i
]));
2099 uint64_t base
= offsets
[i
] + wordsize
;
2102 // Find foldable relocations to construct bitmaps.
2104 uint64_t bitmap
= 0;
2105 for (; i
!= e
; ++i
) {
2106 uint64_t d
= offsets
[i
] - base
;
2107 if (d
>= nBits
* wordsize
|| d
% wordsize
)
2109 bitmap
|= uint64_t(1) << (d
/ wordsize
);
2113 relrRelocs
.push_back(Elf_Relr((bitmap
<< 1) | 1));
2114 base
+= nBits
* wordsize
;
2118 // Don't allow the section to shrink; otherwise the size of the section can
2119 // oscillate infinitely. Trailing 1s do not decode to more relocations.
2120 if (relrRelocs
.size() < oldSize
) {
2121 Log(ctx
) << ".relr.dyn needs " << Twine(oldSize
- relrRelocs
.size()) <<
2123 relrRelocs
.resize(oldSize
, Elf_Relr(1));
2126 return relrRelocs
.size() != oldSize
;
2129 SymbolTableBaseSection::SymbolTableBaseSection(Ctx
&ctx
,
2130 StringTableSection
&strTabSec
)
2131 : SyntheticSection(ctx
, strTabSec
.isDynamic() ? (uint64_t)SHF_ALLOC
: 0,
2132 strTabSec
.isDynamic() ? SHT_DYNSYM
: SHT_SYMTAB
,
2134 strTabSec
.isDynamic() ? ".dynsym" : ".symtab"),
2135 strTabSec(strTabSec
) {}
2137 // Orders symbols according to their positions in the GOT,
2138 // in compliance with MIPS ABI rules.
2139 // See "Global Offset Table" in Chapter 5 in the following document
2140 // for detailed description:
2141 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2142 static void sortMipsSymbols(Ctx
&ctx
, SmallVector
<SymbolTableEntry
, 0> &syms
) {
2143 llvm::stable_sort(syms
,
2144 [&](const SymbolTableEntry
&l
, const SymbolTableEntry
&r
) {
2145 // Sort entries related to non-local preemptible symbols
2146 // by GOT indexes. All other entries go to the beginning
2147 // of a dynsym in arbitrary order.
2148 if (l
.sym
->isInGot(ctx
) && r
.sym
->isInGot(ctx
))
2149 return l
.sym
->getGotIdx(ctx
) < r
.sym
->getGotIdx(ctx
);
2150 if (!l
.sym
->isInGot(ctx
) && !r
.sym
->isInGot(ctx
))
2152 return !l
.sym
->isInGot(ctx
);
2156 void SymbolTableBaseSection::finalizeContents() {
2157 if (OutputSection
*sec
= strTabSec
.getParent())
2158 getParent()->link
= sec
->sectionIndex
;
2160 if (this->type
!= SHT_DYNSYM
) {
2161 sortSymTabSymbols();
2165 // If it is a .dynsym, there should be no local symbols, but we need
2166 // to do a few things for the dynamic linker.
2168 // Section's Info field has the index of the first non-local symbol.
2169 // Because the first symbol entry is a null entry, 1 is the first.
2170 getParent()->info
= 1;
2172 if (getPartition(ctx
).gnuHashTab
) {
2173 // NB: It also sorts Symbols to meet the GNU hash table requirements.
2174 getPartition(ctx
).gnuHashTab
->addSymbols(symbols
);
2175 } else if (ctx
.arg
.emachine
== EM_MIPS
) {
2176 sortMipsSymbols(ctx
, symbols
);
2179 // Only the main partition's dynsym indexes are stored in the symbols
2180 // themselves. All other partitions use a lookup table.
2181 if (this == ctx
.mainPart
->dynSymTab
.get()) {
2183 for (const SymbolTableEntry
&s
: symbols
)
2184 s
.sym
->dynsymIndex
= ++i
;
2188 // The ELF spec requires that all local symbols precede global symbols, so we
2189 // sort symbol entries in this function. (For .dynsym, we don't do that because
2190 // symbols for dynamic linking are inherently all globals.)
2192 // Aside from above, we put local symbols in groups starting with the STT_FILE
2193 // symbol. That is convenient for purpose of identifying where are local symbols
2195 void SymbolTableBaseSection::sortSymTabSymbols() {
2196 // Move all local symbols before global symbols.
2197 auto e
= std::stable_partition(
2198 symbols
.begin(), symbols
.end(),
2199 [](const SymbolTableEntry
&s
) { return s
.sym
->isLocal(); });
2200 size_t numLocals
= e
- symbols
.begin();
2201 getParent()->info
= numLocals
+ 1;
2203 // We want to group the local symbols by file. For that we rebuild the local
2204 // part of the symbols vector. We do not need to care about the STT_FILE
2205 // symbols, they are already naturally placed first in each group. That
2206 // happens because STT_FILE is always the first symbol in the object and hence
2207 // precede all other local symbols we add for a file.
2208 MapVector
<InputFile
*, SmallVector
<SymbolTableEntry
, 0>> arr
;
2209 for (const SymbolTableEntry
&s
: llvm::make_range(symbols
.begin(), e
))
2210 arr
[s
.sym
->file
].push_back(s
);
2212 auto i
= symbols
.begin();
2214 for (SymbolTableEntry
&entry
: p
.second
)
2218 void SymbolTableBaseSection::addSymbol(Symbol
*b
) {
2219 // Adding a local symbol to a .dynsym is a bug.
2220 assert(this->type
!= SHT_DYNSYM
|| !b
->isLocal());
2221 symbols
.push_back({b
, strTabSec
.addString(b
->getName(), false)});
2224 size_t SymbolTableBaseSection::getSymbolIndex(const Symbol
&sym
) {
2225 if (this == ctx
.mainPart
->dynSymTab
.get())
2226 return sym
.dynsymIndex
;
2228 // Initializes symbol lookup tables lazily. This is used only for -r,
2229 // --emit-relocs and dynsyms in partitions other than the main one.
2230 llvm::call_once(onceFlag
, [&] {
2231 symbolIndexMap
.reserve(symbols
.size());
2233 for (const SymbolTableEntry
&e
: symbols
) {
2234 if (e
.sym
->type
== STT_SECTION
)
2235 sectionIndexMap
[e
.sym
->getOutputSection()] = ++i
;
2237 symbolIndexMap
[e
.sym
] = ++i
;
2241 // Section symbols are mapped based on their output sections
2242 // to maintain their semantics.
2243 if (sym
.type
== STT_SECTION
)
2244 return sectionIndexMap
.lookup(sym
.getOutputSection());
2245 return symbolIndexMap
.lookup(&sym
);
2248 template <class ELFT
>
2249 SymbolTableSection
<ELFT
>::SymbolTableSection(Ctx
&ctx
,
2250 StringTableSection
&strTabSec
)
2251 : SymbolTableBaseSection(ctx
, strTabSec
) {
2252 this->entsize
= sizeof(Elf_Sym
);
2255 static BssSection
*getCommonSec(bool relocatable
, Symbol
*sym
) {
2257 if (auto *d
= dyn_cast
<Defined
>(sym
))
2258 return dyn_cast_or_null
<BssSection
>(d
->section
);
2262 static uint32_t getSymSectionIndex(Symbol
*sym
) {
2263 assert(!(sym
->hasFlag(NEEDS_COPY
) && sym
->isObject()));
2264 if (!isa
<Defined
>(sym
) || sym
->hasFlag(NEEDS_COPY
))
2266 if (const OutputSection
*os
= sym
->getOutputSection())
2267 return os
->sectionIndex
>= SHN_LORESERVE
? (uint32_t)SHN_XINDEX
2272 // Write the internal symbol table contents to the output symbol table.
2273 template <class ELFT
> void SymbolTableSection
<ELFT
>::writeTo(uint8_t *buf
) {
2274 // The first entry is a null entry as per the ELF spec.
2275 buf
+= sizeof(Elf_Sym
);
2277 auto *eSym
= reinterpret_cast<Elf_Sym
*>(buf
);
2278 bool relocatable
= ctx
.arg
.relocatable
;
2279 for (SymbolTableEntry
&ent
: symbols
) {
2280 Symbol
*sym
= ent
.sym
;
2281 bool isDefinedHere
= type
== SHT_SYMTAB
|| sym
->partition
== partition
;
2283 // Set st_name, st_info and st_other.
2284 eSym
->st_name
= ent
.strTabOffset
;
2285 eSym
->setBindingAndType(sym
->binding
, sym
->type
);
2286 eSym
->st_other
= sym
->stOther
;
2288 if (BssSection
*commonSec
= getCommonSec(relocatable
, sym
)) {
2289 // When -r is specified, a COMMON symbol is not allocated. Its st_shndx
2290 // holds SHN_COMMON and st_value holds the alignment.
2291 eSym
->st_shndx
= SHN_COMMON
;
2292 eSym
->st_value
= commonSec
->addralign
;
2293 eSym
->st_size
= cast
<Defined
>(sym
)->size
;
2295 const uint32_t shndx
= getSymSectionIndex(sym
);
2296 if (isDefinedHere
) {
2297 eSym
->st_shndx
= shndx
;
2298 eSym
->st_value
= sym
->getVA(ctx
);
2299 // Copy symbol size if it is a defined symbol. st_size is not
2300 // significant for undefined symbols, so whether copying it or not is up
2301 // to us if that's the case. We'll leave it as zero because by not
2302 // setting a value, we can get the exact same outputs for two sets of
2303 // input files that differ only in undefined symbol size in DSOs.
2304 eSym
->st_size
= shndx
!= SHN_UNDEF
? cast
<Defined
>(sym
)->size
: 0;
2315 // On MIPS we need to mark symbol which has a PLT entry and requires
2316 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2317 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2318 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2319 if (ctx
.arg
.emachine
== EM_MIPS
) {
2320 auto *eSym
= reinterpret_cast<Elf_Sym
*>(buf
);
2322 for (SymbolTableEntry
&ent
: symbols
) {
2323 Symbol
*sym
= ent
.sym
;
2324 if (sym
->isInPlt(ctx
) && sym
->hasFlag(NEEDS_COPY
))
2325 eSym
->st_other
|= STO_MIPS_PLT
;
2326 if (isMicroMips(ctx
)) {
2327 // We already set the less-significant bit for symbols
2328 // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2329 // records. That allows us to distinguish such symbols in
2330 // the `MIPS<ELFT>::relocate()` routine. Now we should
2331 // clear that bit for non-dynamic symbol table, so tools
2332 // like `objdump` will be able to deal with a correct
2334 if (sym
->isDefined() &&
2335 ((sym
->stOther
& STO_MIPS_MICROMIPS
) || sym
->hasFlag(NEEDS_COPY
))) {
2336 if (!strTabSec
.isDynamic())
2337 eSym
->st_value
&= ~1;
2338 eSym
->st_other
|= STO_MIPS_MICROMIPS
;
2341 if (ctx
.arg
.relocatable
)
2342 if (auto *d
= dyn_cast
<Defined
>(sym
))
2343 if (isMipsPIC
<ELFT
>(d
))
2344 eSym
->st_other
|= STO_MIPS_PIC
;
2350 SymtabShndxSection::SymtabShndxSection(Ctx
&ctx
)
2351 : SyntheticSection(ctx
, 0, SHT_SYMTAB_SHNDX
, 4, ".symtab_shndx") {
2355 void SymtabShndxSection::writeTo(uint8_t *buf
) {
2356 // We write an array of 32 bit values, where each value has 1:1 association
2357 // with an entry in ctx.in.symTab if the corresponding entry contains
2358 // SHN_XINDEX, we need to write actual index, otherwise, we must write
2360 buf
+= 4; // Ignore .symtab[0] entry.
2361 bool relocatable
= ctx
.arg
.relocatable
;
2362 for (const SymbolTableEntry
&entry
: ctx
.in
.symTab
->getSymbols()) {
2363 if (!getCommonSec(relocatable
, entry
.sym
) &&
2364 getSymSectionIndex(entry
.sym
) == SHN_XINDEX
)
2365 write32(ctx
, buf
, entry
.sym
->getOutputSection()->sectionIndex
);
2370 bool SymtabShndxSection::isNeeded() const {
2371 // SHT_SYMTAB can hold symbols with section indices values up to
2372 // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
2373 // section. Problem is that we reveal the final section indices a bit too
2374 // late, and we do not know them here. For simplicity, we just always create
2375 // a .symtab_shndx section when the amount of output sections is huge.
2377 for (SectionCommand
*cmd
: ctx
.script
->sectionCommands
)
2378 if (isa
<OutputDesc
>(cmd
))
2380 return size
>= SHN_LORESERVE
;
2383 void SymtabShndxSection::finalizeContents() {
2384 getParent()->link
= ctx
.in
.symTab
->getParent()->sectionIndex
;
2387 size_t SymtabShndxSection::getSize() const {
2388 return ctx
.in
.symTab
->getNumSymbols() * 4;
2391 // .hash and .gnu.hash sections contain on-disk hash tables that map
2392 // symbol names to their dynamic symbol table indices. Their purpose
2393 // is to help the dynamic linker resolve symbols quickly. If ELF files
2394 // don't have them, the dynamic linker has to do linear search on all
2395 // dynamic symbols, which makes programs slower. Therefore, a .hash
2396 // section is added to a DSO by default.
2398 // The Unix semantics of resolving dynamic symbols is somewhat expensive.
2399 // Each ELF file has a list of DSOs that the ELF file depends on and a
2400 // list of dynamic symbols that need to be resolved from any of the
2401 // DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
2402 // where m is the number of DSOs and n is the number of dynamic
2403 // symbols. For modern large programs, both m and n are large. So
2404 // making each step faster by using hash tables substantially
2405 // improves time to load programs.
2407 // (Note that this is not the only way to design the shared library.
2408 // For instance, the Windows DLL takes a different approach. On
2409 // Windows, each dynamic symbol has a name of DLL from which the symbol
2410 // has to be resolved. That makes the cost of symbol resolution O(n).
2411 // This disables some hacky techniques you can use on Unix such as
2412 // LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
2414 // Due to historical reasons, we have two different hash tables, .hash
2415 // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
2416 // and better version of .hash. .hash is just an on-disk hash table, but
2417 // .gnu.hash has a bloom filter in addition to a hash table to skip
2418 // DSOs very quickly. If you are sure that your dynamic linker knows
2419 // about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a
2420 // safe bet is to specify --hash-style=both for backward compatibility.
2421 GnuHashTableSection::GnuHashTableSection(Ctx
&ctx
)
2422 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_GNU_HASH
, ctx
.arg
.wordsize
,
2425 void GnuHashTableSection::finalizeContents() {
2426 if (OutputSection
*sec
= getPartition(ctx
).dynSymTab
->getParent())
2427 getParent()->link
= sec
->sectionIndex
;
2429 // Computes bloom filter size in word size. We want to allocate 12
2430 // bits for each symbol. It must be a power of two.
2431 if (symbols
.empty()) {
2434 uint64_t numBits
= symbols
.size() * 12;
2435 maskWords
= NextPowerOf2(numBits
/ (ctx
.arg
.wordsize
* 8));
2438 size
= 16; // Header
2439 size
+= ctx
.arg
.wordsize
* maskWords
; // Bloom filter
2440 size
+= nBuckets
* 4; // Hash buckets
2441 size
+= symbols
.size() * 4; // Hash values
2444 void GnuHashTableSection::writeTo(uint8_t *buf
) {
2446 write32(ctx
, buf
, nBuckets
);
2447 write32(ctx
, buf
+ 4,
2448 getPartition(ctx
).dynSymTab
->getNumSymbols() - symbols
.size());
2449 write32(ctx
, buf
+ 8, maskWords
);
2450 write32(ctx
, buf
+ 12, Shift2
);
2453 // Write the 2-bit bloom filter.
2454 const unsigned c
= ctx
.arg
.is64
? 64 : 32;
2455 for (const Entry
&sym
: symbols
) {
2456 // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in
2457 // the word using bits [0:5] and [26:31].
2458 size_t i
= (sym
.hash
/ c
) & (maskWords
- 1);
2459 uint64_t val
= readUint(ctx
, buf
+ i
* ctx
.arg
.wordsize
);
2460 val
|= uint64_t(1) << (sym
.hash
% c
);
2461 val
|= uint64_t(1) << ((sym
.hash
>> Shift2
) % c
);
2462 writeUint(ctx
, buf
+ i
* ctx
.arg
.wordsize
, val
);
2464 buf
+= ctx
.arg
.wordsize
* maskWords
;
2466 // Write the hash table.
2467 uint32_t *buckets
= reinterpret_cast<uint32_t *>(buf
);
2468 uint32_t oldBucket
= -1;
2469 uint32_t *values
= buckets
+ nBuckets
;
2470 for (auto i
= symbols
.begin(), e
= symbols
.end(); i
!= e
; ++i
) {
2471 // Write a hash value. It represents a sequence of chains that share the
2472 // same hash modulo value. The last element of each chain is terminated by
2474 uint32_t hash
= i
->hash
;
2475 bool isLastInChain
= (i
+ 1) == e
|| i
->bucketIdx
!= (i
+ 1)->bucketIdx
;
2476 hash
= isLastInChain
? hash
| 1 : hash
& ~1;
2477 write32(ctx
, values
++, hash
);
2479 if (i
->bucketIdx
== oldBucket
)
2481 // Write a hash bucket. Hash buckets contain indices in the following hash
2483 write32(ctx
, buckets
+ i
->bucketIdx
,
2484 getPartition(ctx
).dynSymTab
->getSymbolIndex(*i
->sym
));
2485 oldBucket
= i
->bucketIdx
;
2489 // Add symbols to this symbol hash table. Note that this function
2490 // destructively sort a given vector -- which is needed because
2491 // GNU-style hash table places some sorting requirements.
2492 void GnuHashTableSection::addSymbols(SmallVectorImpl
<SymbolTableEntry
> &v
) {
2493 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
2494 // its type correctly.
2496 std::stable_partition(v
.begin(), v
.end(), [&](const SymbolTableEntry
&s
) {
2497 return !s
.sym
->isDefined() || s
.sym
->partition
!= partition
;
2500 // We chose load factor 4 for the on-disk hash table. For each hash
2501 // collision, the dynamic linker will compare a uint32_t hash value.
2502 // Since the integer comparison is quite fast, we believe we can
2503 // make the load factor even larger. 4 is just a conservative choice.
2505 // Note that we don't want to create a zero-sized hash table because
2506 // Android loader as of 2018 doesn't like a .gnu.hash containing such
2507 // table. If that's the case, we create a hash table with one unused
2509 nBuckets
= std::max
<size_t>((v
.end() - mid
) / 4, 1);
2514 for (SymbolTableEntry
&ent
: llvm::make_range(mid
, v
.end())) {
2515 Symbol
*b
= ent
.sym
;
2516 uint32_t hash
= hashGnu(b
->getName());
2517 uint32_t bucketIdx
= hash
% nBuckets
;
2518 symbols
.push_back({b
, ent
.strTabOffset
, hash
, bucketIdx
});
2521 llvm::sort(symbols
, [](const Entry
&l
, const Entry
&r
) {
2522 return std::tie(l
.bucketIdx
, l
.strTabOffset
) <
2523 std::tie(r
.bucketIdx
, r
.strTabOffset
);
2526 v
.erase(mid
, v
.end());
2527 for (const Entry
&ent
: symbols
)
2528 v
.push_back({ent
.sym
, ent
.strTabOffset
});
2531 HashTableSection::HashTableSection(Ctx
&ctx
)
2532 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_HASH
, 4, ".hash") {
2536 void HashTableSection::finalizeContents() {
2537 SymbolTableBaseSection
*symTab
= getPartition(ctx
).dynSymTab
.get();
2539 if (OutputSection
*sec
= symTab
->getParent())
2540 getParent()->link
= sec
->sectionIndex
;
2542 unsigned numEntries
= 2; // nbucket and nchain.
2543 numEntries
+= symTab
->getNumSymbols(); // The chain entries.
2545 // Create as many buckets as there are symbols.
2546 numEntries
+= symTab
->getNumSymbols();
2547 this->size
= numEntries
* 4;
2550 void HashTableSection::writeTo(uint8_t *buf
) {
2551 SymbolTableBaseSection
*symTab
= getPartition(ctx
).dynSymTab
.get();
2552 unsigned numSymbols
= symTab
->getNumSymbols();
2554 uint32_t *p
= reinterpret_cast<uint32_t *>(buf
);
2555 write32(ctx
, p
++, numSymbols
); // nbucket
2556 write32(ctx
, p
++, numSymbols
); // nchain
2558 uint32_t *buckets
= p
;
2559 uint32_t *chains
= p
+ numSymbols
;
2561 for (const SymbolTableEntry
&s
: symTab
->getSymbols()) {
2562 Symbol
*sym
= s
.sym
;
2563 StringRef name
= sym
->getName();
2564 unsigned i
= sym
->dynsymIndex
;
2565 uint32_t hash
= hashSysV(name
) % numSymbols
;
2566 chains
[i
] = buckets
[hash
];
2567 write32(ctx
, buckets
+ hash
, i
);
2571 PltSection::PltSection(Ctx
&ctx
)
2572 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_EXECINSTR
, SHT_PROGBITS
, 16,
2574 headerSize(ctx
.target
->pltHeaderSize
) {
2575 // On PowerPC, this section contains lazy symbol resolvers.
2576 if (ctx
.arg
.emachine
== EM_PPC64
) {
2581 // On x86 when IBT is enabled, this section contains the second PLT (lazy
2582 // symbol resolvers).
2583 if ((ctx
.arg
.emachine
== EM_386
|| ctx
.arg
.emachine
== EM_X86_64
) &&
2584 (ctx
.arg
.andFeatures
& GNU_PROPERTY_X86_FEATURE_1_IBT
))
2587 // The PLT needs to be writable on SPARC as the dynamic linker will
2588 // modify the instructions in the PLT entries.
2589 if (ctx
.arg
.emachine
== EM_SPARCV9
)
2590 this->flags
|= SHF_WRITE
;
2593 void PltSection::writeTo(uint8_t *buf
) {
2594 // At beginning of PLT, we have code to call the dynamic
2595 // linker to resolve dynsyms at runtime. Write such code.
2596 ctx
.target
->writePltHeader(buf
);
2597 size_t off
= headerSize
;
2599 for (const Symbol
*sym
: entries
) {
2600 ctx
.target
->writePlt(buf
+ off
, *sym
, getVA() + off
);
2601 off
+= ctx
.target
->pltEntrySize
;
2605 void PltSection::addEntry(Symbol
&sym
) {
2606 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1);
2607 ctx
.symAux
.back().pltIdx
= entries
.size();
2608 entries
.push_back(&sym
);
2611 size_t PltSection::getSize() const {
2612 return headerSize
+ entries
.size() * ctx
.target
->pltEntrySize
;
2615 bool PltSection::isNeeded() const {
2616 // For -z retpolineplt, .iplt needs the .plt header.
2617 return !entries
.empty() || (ctx
.arg
.zRetpolineplt
&& ctx
.in
.iplt
->isNeeded());
2620 // Used by ARM to add mapping symbols in the PLT section, which aid
2622 void PltSection::addSymbols() {
2623 ctx
.target
->addPltHeaderSymbols(*this);
2625 size_t off
= headerSize
;
2626 for (size_t i
= 0; i
< entries
.size(); ++i
) {
2627 ctx
.target
->addPltSymbols(*this, off
);
2628 off
+= ctx
.target
->pltEntrySize
;
2632 IpltSection::IpltSection(Ctx
&ctx
)
2633 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_EXECINSTR
, SHT_PROGBITS
, 16,
2635 if (ctx
.arg
.emachine
== EM_PPC
|| ctx
.arg
.emachine
== EM_PPC64
) {
2641 void IpltSection::writeTo(uint8_t *buf
) {
2643 for (const Symbol
*sym
: entries
) {
2644 ctx
.target
->writeIplt(buf
+ off
, *sym
, getVA() + off
);
2645 off
+= ctx
.target
->ipltEntrySize
;
2649 size_t IpltSection::getSize() const {
2650 return entries
.size() * ctx
.target
->ipltEntrySize
;
2653 void IpltSection::addEntry(Symbol
&sym
) {
2654 assert(sym
.auxIdx
== ctx
.symAux
.size() - 1);
2655 ctx
.symAux
.back().pltIdx
= entries
.size();
2656 entries
.push_back(&sym
);
2659 // ARM uses mapping symbols to aid disassembly.
2660 void IpltSection::addSymbols() {
2662 for (size_t i
= 0, e
= entries
.size(); i
!= e
; ++i
) {
2663 ctx
.target
->addPltSymbols(*this, off
);
2664 off
+= ctx
.target
->pltEntrySize
;
2668 PPC32GlinkSection::PPC32GlinkSection(Ctx
&ctx
) : PltSection(ctx
) {
2673 void PPC32GlinkSection::writeTo(uint8_t *buf
) {
2674 writePPC32GlinkSection(ctx
, buf
, entries
.size());
2677 size_t PPC32GlinkSection::getSize() const {
2678 return headerSize
+ entries
.size() * ctx
.target
->pltEntrySize
+ footerSize
;
2681 // This is an x86-only extra PLT section and used only when a security
2682 // enhancement feature called CET is enabled. In this comment, I'll explain what
2683 // the feature is and why we have two PLT sections if CET is enabled.
2685 // So, what does CET do? CET introduces a new restriction to indirect jump
2686 // instructions. CET works this way. Assume that CET is enabled. Then, if you
2687 // execute an indirect jump instruction, the processor verifies that a special
2688 // "landing pad" instruction (which is actually a repurposed NOP instruction and
2689 // now called "endbr32" or "endbr64") is at the jump target. If the jump target
2690 // does not start with that instruction, the processor raises an exception
2691 // instead of continuing executing code.
2693 // If CET is enabled, the compiler emits endbr to all locations where indirect
2694 // jumps may jump to.
2696 // This mechanism makes it extremely hard to transfer the control to a middle of
2697 // a function that is not supporsed to be a indirect jump target, preventing
2698 // certain types of attacks such as ROP or JOP.
2700 // Note that the processors in the market as of 2019 don't actually support the
2701 // feature. Only the spec is available at the moment.
2703 // Now, I'll explain why we have this extra PLT section for CET.
2705 // Since you can indirectly jump to a PLT entry, we have to make PLT entries
2706 // start with endbr. The problem is there's no extra space for endbr (which is 4
2707 // bytes long), as the PLT entry is only 16 bytes long and all bytes are already
2710 // In order to deal with the issue, we split a PLT entry into two PLT entries.
2711 // Remember that each PLT entry contains code to jump to an address read from
2712 // .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme,
2713 // the former code is written to .plt.sec, and the latter code is written to
2716 // Lazy symbol resolution in the 2-PLT scheme works in the usual way, except
2717 // that the regular .plt is now called .plt.sec and .plt is repurposed to
2718 // contain only code for lazy symbol resolution.
2720 // In other words, this is how the 2-PLT scheme works. Application code is
2721 // supposed to jump to .plt.sec to call an external function. Each .plt.sec
2722 // entry contains code to read an address from a corresponding .got.plt entry
2723 // and jump to that address. Addresses in .got.plt initially point to .plt, so
2724 // when an application calls an external function for the first time, the
2725 // control is transferred to a function that resolves a symbol name from
2726 // external shared object files. That function then rewrites a .got.plt entry
2727 // with a resolved address, so that the subsequent function calls directly jump
2728 // to a desired location from .plt.sec.
2730 // There is an open question as to whether the 2-PLT scheme was desirable or
2731 // not. We could have simply extended the PLT entry size to 32-bytes to
2732 // accommodate endbr, and that scheme would have been much simpler than the
2733 // 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot
2734 // code (.plt.sec) from cold code (.plt). But as far as I know no one proved
2735 // that the optimization actually makes a difference.
2737 // That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools
2738 // depend on it, so we implement the ABI.
2739 IBTPltSection::IBTPltSection(Ctx
&ctx
)
2740 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_EXECINSTR
, SHT_PROGBITS
, 16,
2743 void IBTPltSection::writeTo(uint8_t *buf
) {
2744 ctx
.target
->writeIBTPlt(buf
, ctx
.in
.plt
->getNumEntries());
2747 size_t IBTPltSection::getSize() const {
2748 // 16 is the header size of .plt.
2749 return 16 + ctx
.in
.plt
->getNumEntries() * ctx
.target
->pltEntrySize
;
2752 bool IBTPltSection::isNeeded() const { return ctx
.in
.plt
->getNumEntries() > 0; }
2754 RelroPaddingSection::RelroPaddingSection(Ctx
&ctx
)
2755 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_NOBITS
, 1,
2756 ".relro_padding") {}
2758 // The string hash function for .gdb_index.
2759 static uint32_t computeGdbHash(StringRef s
) {
2762 h
= h
* 67 + toLower(c
) - 113;
2766 // 4-byte alignment ensures that values in the hash lookup table and the name
2767 // table are aligned.
2768 DebugNamesBaseSection::DebugNamesBaseSection(Ctx
&ctx
)
2769 : SyntheticSection(ctx
, 0, SHT_PROGBITS
, 4, ".debug_names") {}
2771 // Get the size of the .debug_names section header in bytes for DWARF32:
2772 static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize
) {
2773 return /* unit length */ 4 +
2778 /* Foreign TU count */ 4 +
2779 /* Bucket Count */ 4 +
2780 /* Name Count */ 4 +
2781 /* Abbrev table size */ 4 +
2782 /* Augmentation string size */ 4 +
2783 /* Augmentation string */ augmentationStringSize
;
2786 static Expected
<DebugNamesBaseSection::IndexEntry
*>
2787 readEntry(uint64_t &offset
, const DWARFDebugNames::NameIndex
&ni
,
2788 uint64_t entriesBase
, DWARFDataExtractor
&namesExtractor
,
2789 const LLDDWARFSection
&namesSec
) {
2790 auto ie
= makeThreadLocal
<DebugNamesBaseSection::IndexEntry
>();
2791 ie
->poolOffset
= offset
;
2792 Error err
= Error::success();
2793 uint64_t ulebVal
= namesExtractor
.getULEB128(&offset
, &err
);
2795 return createStringError(inconvertibleErrorCode(),
2796 "invalid abbrev code: %s",
2797 llvm::toString(std::move(err
)).c_str());
2798 if (!isUInt
<32>(ulebVal
))
2799 return createStringError(inconvertibleErrorCode(),
2800 "abbrev code too large for DWARF32: %" PRIu64
,
2802 ie
->abbrevCode
= static_cast<uint32_t>(ulebVal
);
2803 auto it
= ni
.getAbbrevs().find_as(ie
->abbrevCode
);
2804 if (it
== ni
.getAbbrevs().end())
2805 return createStringError(inconvertibleErrorCode(),
2806 "abbrev code not found in abbrev table: %" PRIu32
,
2809 DebugNamesBaseSection::AttrValue attr
, cuAttr
= {0, 0};
2810 for (DWARFDebugNames::AttributeEncoding a
: it
->Attributes
) {
2811 if (a
.Index
== dwarf::DW_IDX_parent
) {
2812 if (a
.Form
== dwarf::DW_FORM_ref4
) {
2813 attr
.attrValue
= namesExtractor
.getU32(&offset
, &err
);
2815 ie
->parentOffset
= entriesBase
+ attr
.attrValue
;
2816 } else if (a
.Form
!= DW_FORM_flag_present
)
2817 return createStringError(inconvertibleErrorCode(),
2818 "invalid form for DW_IDX_parent");
2822 case DW_FORM_ref1
: {
2823 attr
.attrValue
= namesExtractor
.getU8(&offset
, &err
);
2828 case DW_FORM_ref2
: {
2829 attr
.attrValue
= namesExtractor
.getU16(&offset
, &err
);
2834 case DW_FORM_ref4
: {
2835 attr
.attrValue
= namesExtractor
.getU32(&offset
, &err
);
2840 return createStringError(
2841 inconvertibleErrorCode(),
2842 "unrecognized form encoding %d in abbrev table", a
.Form
);
2846 return createStringError(inconvertibleErrorCode(),
2847 "error while reading attributes: %s",
2848 llvm::toString(std::move(err
)).c_str());
2849 if (a
.Index
== DW_IDX_compile_unit
)
2851 else if (a
.Form
!= DW_FORM_flag_present
)
2852 ie
->attrValues
.push_back(attr
);
2854 // Canonicalize abbrev by placing the CU/TU index at the end.
2855 ie
->attrValues
.push_back(cuAttr
);
2859 void DebugNamesBaseSection::parseDebugNames(
2860 Ctx
&ctx
, InputChunk
&inputChunk
, OutputChunk
&chunk
,
2861 DWARFDataExtractor
&namesExtractor
, DataExtractor
&strExtractor
,
2862 function_ref
<SmallVector
<uint32_t, 0>(
2863 uint32_t numCus
, const DWARFDebugNames::Header
&,
2864 const DWARFDebugNames::DWARFDebugNamesOffsets
&)>
2866 const LLDDWARFSection
&namesSec
= inputChunk
.section
;
2867 DenseMap
<uint32_t, IndexEntry
*> offsetMap
;
2868 // Number of CUs seen in previous NameIndex sections within current chunk.
2869 uint32_t numCus
= 0;
2870 for (const DWARFDebugNames::NameIndex
&ni
: *inputChunk
.llvmDebugNames
) {
2871 NameData
&nd
= inputChunk
.nameData
.emplace_back();
2872 nd
.hdr
= ni
.getHeader();
2873 if (nd
.hdr
.Format
!= DwarfFormat::DWARF32
) {
2874 Err(ctx
) << namesSec
.sec
2875 << Twine(": found DWARF64, which is currently unsupported");
2878 if (nd
.hdr
.Version
!= 5) {
2879 Err(ctx
) << namesSec
.sec
<< ": unsupported version: " << nd
.hdr
.Version
;
2882 uint32_t dwarfSize
= dwarf::getDwarfOffsetByteSize(DwarfFormat::DWARF32
);
2883 DWARFDebugNames::DWARFDebugNamesOffsets locs
= ni
.getOffsets();
2884 if (locs
.EntriesBase
> namesExtractor
.getData().size()) {
2885 Err(ctx
) << namesSec
.sec
2886 << Twine(": entry pool start is beyond end of section");
2890 SmallVector
<uint32_t, 0> entryOffsets
= readOffsets(numCus
, nd
.hdr
, locs
);
2892 // Read the entry pool.
2894 nd
.nameEntries
.resize(nd
.hdr
.NameCount
);
2895 for (auto i
: seq(nd
.hdr
.NameCount
)) {
2896 NameEntry
&ne
= nd
.nameEntries
[i
];
2897 uint64_t strOffset
= locs
.StringOffsetsBase
+ i
* dwarfSize
;
2898 ne
.stringOffset
= strOffset
;
2899 uint64_t strp
= namesExtractor
.getRelocatedValue(dwarfSize
, &strOffset
);
2900 StringRef name
= strExtractor
.getCStrRef(&strp
);
2901 ne
.name
= name
.data();
2902 ne
.hashValue
= caseFoldingDjbHash(name
);
2904 // Read a series of index entries that end with abbreviation code 0.
2905 uint64_t offset
= locs
.EntriesBase
+ entryOffsets
[i
];
2906 while (offset
< namesSec
.Data
.size() && namesSec
.Data
[offset
] != 0) {
2907 // Read & store all entries (for the same string).
2908 Expected
<IndexEntry
*> ieOrErr
=
2909 readEntry(offset
, ni
, locs
.EntriesBase
, namesExtractor
, namesSec
);
2911 Err(ctx
) << namesSec
.sec
<< ": " << ieOrErr
.takeError();
2914 ne
.indexEntries
.push_back(std::move(*ieOrErr
));
2916 if (offset
>= namesSec
.Data
.size())
2917 Err(ctx
) << namesSec
.sec
<< ": index entry is out of bounds";
2919 for (IndexEntry
&ie
: ne
.entries())
2920 offsetMap
[ie
.poolOffset
] = &ie
;
2923 // Assign parent pointers, which will be used to update DW_IDX_parent index
2924 // attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will
2925 // get parentEntry == null as well.
2926 for (NameEntry
&ne
: nd
.nameEntries
)
2927 for (IndexEntry
&ie
: ne
.entries())
2928 ie
.parentEntry
= offsetMap
.lookup(ie
.parentOffset
);
2929 numCus
+= nd
.hdr
.CompUnitCount
;
2933 // Compute the form for output DW_IDX_compile_unit attributes, similar to
2934 // DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all
2935 // the merged CU indices.
2936 std::pair
<uint8_t, dwarf::Form
> static getMergedCuCountForm(
2937 uint32_t compUnitCount
) {
2938 if (compUnitCount
> UINT16_MAX
)
2939 return {4, DW_FORM_data4
};
2940 if (compUnitCount
> UINT8_MAX
)
2941 return {2, DW_FORM_data2
};
2942 return {1, DW_FORM_data1
};
2945 void DebugNamesBaseSection::computeHdrAndAbbrevTable(
2946 MutableArrayRef
<InputChunk
> inputChunks
) {
2947 TimeTraceScope
timeScope("Merge .debug_names", "hdr and abbrev table");
2949 hdr
.Format
= DwarfFormat::DWARF32
;
2951 hdr
.CompUnitCount
= 0;
2952 hdr
.LocalTypeUnitCount
= 0;
2953 hdr
.ForeignTypeUnitCount
= 0;
2954 hdr
.AugmentationStringSize
= 0;
2956 // Compute CU and TU counts.
2957 for (auto i
: seq(numChunks
)) {
2958 InputChunk
&inputChunk
= inputChunks
[i
];
2959 inputChunk
.baseCuIdx
= numCu
;
2960 numCu
+= chunks
[i
].compUnits
.size();
2961 for (const NameData
&nd
: inputChunk
.nameData
) {
2962 hdr
.CompUnitCount
+= nd
.hdr
.CompUnitCount
;
2963 // TODO: We don't handle type units yet, so LocalTypeUnitCount &
2964 // ForeignTypeUnitCount are left as 0.
2965 if (nd
.hdr
.LocalTypeUnitCount
|| nd
.hdr
.ForeignTypeUnitCount
)
2966 Warn(ctx
) << inputChunk
.section
.sec
2967 << Twine(": type units are not implemented");
2968 // If augmentation strings are not identical, use an empty string.
2970 hdr
.AugmentationStringSize
= nd
.hdr
.AugmentationStringSize
;
2971 hdr
.AugmentationString
= nd
.hdr
.AugmentationString
;
2972 } else if (hdr
.AugmentationString
!= nd
.hdr
.AugmentationString
) {
2973 // There are conflicting augmentation strings, so it's best for the
2974 // merged index to not use an augmentation string.
2975 hdr
.AugmentationStringSize
= 0;
2976 hdr
.AugmentationString
.clear();
2981 // Create the merged abbrev table, uniquifyinng the input abbrev tables and
2982 // computing mapping from old (per-cu) abbrev codes to new (merged) abbrev
2984 FoldingSet
<Abbrev
> abbrevSet
;
2985 // Determine the form for the DW_IDX_compile_unit attributes in the merged
2986 // index. The input form may not be big enough for all CU indices.
2987 dwarf::Form cuAttrForm
= getMergedCuCountForm(hdr
.CompUnitCount
).second
;
2988 for (InputChunk
&inputChunk
: inputChunks
) {
2989 for (auto [i
, ni
] : enumerate(*inputChunk
.llvmDebugNames
)) {
2990 for (const DWARFDebugNames::Abbrev
&oldAbbrev
: ni
.getAbbrevs()) {
2991 // Canonicalize abbrev by placing the CU/TU index at the end,
2992 // similar to 'parseDebugNames'.
2994 DWARFDebugNames::AttributeEncoding
cuAttr(DW_IDX_compile_unit
,
2996 abbrev
.code
= oldAbbrev
.Code
;
2997 abbrev
.tag
= oldAbbrev
.Tag
;
2998 for (DWARFDebugNames::AttributeEncoding a
: oldAbbrev
.Attributes
) {
2999 if (a
.Index
== DW_IDX_compile_unit
)
3000 cuAttr
.Index
= a
.Index
;
3002 abbrev
.attributes
.push_back({a
.Index
, a
.Form
});
3004 // Put the CU/TU index at the end of the attributes list.
3005 abbrev
.attributes
.push_back(cuAttr
);
3007 // Profile the abbrev, get or assign a new code, then record the abbrev
3009 FoldingSetNodeID id
;
3013 if (Abbrev
*existing
= abbrevSet
.FindNodeOrInsertPos(id
, insertPos
)) {
3014 // Found it; we've already seen an identical abbreviation.
3015 newCode
= existing
->code
;
3018 new (abbrevAlloc
.Allocate()) Abbrev(std::move(abbrev
));
3019 abbrevSet
.InsertNode(abbrev2
, insertPos
);
3020 abbrevTable
.push_back(abbrev2
);
3021 newCode
= abbrevTable
.size();
3022 abbrev2
->code
= newCode
;
3024 inputChunk
.nameData
[i
].abbrevCodeMap
[oldAbbrev
.Code
] = newCode
;
3029 // Compute the merged abbrev table.
3030 raw_svector_ostream
os(abbrevTableBuf
);
3031 for (Abbrev
*abbrev
: abbrevTable
) {
3032 encodeULEB128(abbrev
->code
, os
);
3033 encodeULEB128(abbrev
->tag
, os
);
3034 for (DWARFDebugNames::AttributeEncoding a
: abbrev
->attributes
) {
3035 encodeULEB128(a
.Index
, os
);
3036 encodeULEB128(a
.Form
, os
);
3038 os
.write("\0", 2); // attribute specification end
3040 os
.write(0); // abbrev table end
3041 hdr
.AbbrevTableSize
= abbrevTableBuf
.size();
3044 void DebugNamesBaseSection::Abbrev::Profile(FoldingSetNodeID
&id
) const {
3046 for (const DWARFDebugNames::AttributeEncoding
&attr
: attributes
) {
3047 id
.AddInteger(attr
.Index
);
3048 id
.AddInteger(attr
.Form
);
3052 std::pair
<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(
3053 MutableArrayRef
<InputChunk
> inputChunks
) {
3054 TimeTraceScope
timeScope("Merge .debug_names", "entry pool");
3055 // Collect and de-duplicate all the names (preserving all the entries).
3056 // Speed it up using multithreading, as the number of symbols can be in the
3057 // order of millions.
3058 const size_t concurrency
=
3059 bit_floor(std::min
<size_t>(ctx
.arg
.threadCount
, numShards
));
3060 const size_t shift
= 32 - countr_zero(numShards
);
3061 const uint8_t cuAttrSize
= getMergedCuCountForm(hdr
.CompUnitCount
).first
;
3062 DenseMap
<CachedHashStringRef
, size_t> maps
[numShards
];
3064 parallelFor(0, concurrency
, [&](size_t threadId
) {
3065 for (auto i
: seq(numChunks
)) {
3066 InputChunk
&inputChunk
= inputChunks
[i
];
3067 for (auto j
: seq(inputChunk
.nameData
.size())) {
3068 NameData
&nd
= inputChunk
.nameData
[j
];
3069 // Deduplicate the NameEntry records (based on the string/name),
3070 // appending all IndexEntries from duplicate NameEntry records to
3071 // the single preserved copy.
3072 for (NameEntry
&ne
: nd
.nameEntries
) {
3073 auto shardId
= ne
.hashValue
>> shift
;
3074 if ((shardId
& (concurrency
- 1)) != threadId
)
3078 for (IndexEntry
&ie
: ne
.entries()) {
3079 // Update the IndexEntry's abbrev code to match the merged
3081 ie
.abbrevCode
= nd
.abbrevCodeMap
[ie
.abbrevCode
];
3082 // Update the DW_IDX_compile_unit attribute (the last one after
3083 // canonicalization) to have correct merged offset value and size.
3084 auto &back
= ie
.attrValues
.back();
3085 back
.attrValue
+= inputChunk
.baseCuIdx
+ j
;
3086 back
.attrSize
= cuAttrSize
;
3089 auto &nameVec
= nameVecs
[shardId
];
3090 auto [it
, inserted
] = maps
[shardId
].try_emplace(
3091 CachedHashStringRef(ne
.name
, ne
.hashValue
), nameVec
.size());
3093 nameVec
.push_back(std::move(ne
));
3095 nameVec
[it
->second
].indexEntries
.append(std::move(ne
.indexEntries
));
3101 // Compute entry offsets in parallel. First, compute offsets relative to the
3103 uint32_t offsets
[numShards
];
3104 parallelFor(0, numShards
, [&](size_t shard
) {
3105 uint32_t offset
= 0;
3106 for (NameEntry
&ne
: nameVecs
[shard
]) {
3107 ne
.entryOffset
= offset
;
3108 for (IndexEntry
&ie
: ne
.entries()) {
3109 ie
.poolOffset
= offset
;
3110 offset
+= getULEB128Size(ie
.abbrevCode
);
3111 for (AttrValue value
: ie
.attrValues
)
3112 offset
+= value
.attrSize
;
3114 ++offset
; // index entry sentinel
3116 offsets
[shard
] = offset
;
3118 // Then add shard offsets.
3119 std::partial_sum(offsets
, std::end(offsets
), offsets
);
3120 parallelFor(1, numShards
, [&](size_t shard
) {
3121 uint32_t offset
= offsets
[shard
- 1];
3122 for (NameEntry
&ne
: nameVecs
[shard
]) {
3123 ne
.entryOffset
+= offset
;
3124 for (IndexEntry
&ie
: ne
.entries())
3125 ie
.poolOffset
+= offset
;
3129 // Update the DW_IDX_parent entries that refer to real parents (have
3131 parallelFor(0, numShards
, [&](size_t shard
) {
3132 for (NameEntry
&ne
: nameVecs
[shard
]) {
3133 for (IndexEntry
&ie
: ne
.entries()) {
3134 if (!ie
.parentEntry
)
3136 // Abbrevs are indexed starting at 1; vector starts at 0. (abbrevCode
3137 // corresponds to position in the merged table vector).
3138 const Abbrev
*abbrev
= abbrevTable
[ie
.abbrevCode
- 1];
3139 for (const auto &[a
, v
] : zip_equal(abbrev
->attributes
, ie
.attrValues
))
3140 if (a
.Index
== DW_IDX_parent
&& a
.Form
== DW_FORM_ref4
)
3141 v
.attrValue
= ie
.parentEntry
->poolOffset
;
3146 // Return (entry pool size, number of entries).
3148 for (auto &map
: maps
)
3150 return {offsets
[numShards
- 1], num
};
3153 void DebugNamesBaseSection::init(
3154 function_ref
<void(InputFile
*, InputChunk
&, OutputChunk
&)> parseFile
) {
3155 TimeTraceScope
timeScope("Merge .debug_names");
3156 // Collect and remove input .debug_names sections. Save InputSection pointers
3157 // to relocate string offsets in `writeTo`.
3158 SetVector
<InputFile
*> files
;
3159 for (InputSectionBase
*s
: ctx
.inputSections
) {
3160 InputSection
*isec
= dyn_cast
<InputSection
>(s
);
3163 if (!(s
->flags
& SHF_ALLOC
) && s
->name
== ".debug_names") {
3165 inputSections
.push_back(isec
);
3166 files
.insert(isec
->file
);
3170 // Parse input .debug_names sections and extract InputChunk and OutputChunk
3171 // data. OutputChunk contains CU information, which will be needed by
3173 auto inputChunksPtr
= std::make_unique
<InputChunk
[]>(files
.size());
3174 MutableArrayRef
<InputChunk
> inputChunks(inputChunksPtr
.get(), files
.size());
3175 numChunks
= files
.size();
3176 chunks
= std::make_unique
<OutputChunk
[]>(files
.size());
3178 TimeTraceScope
timeScope("Merge .debug_names", "parse");
3179 parallelFor(0, files
.size(), [&](size_t i
) {
3180 parseFile(files
[i
], inputChunks
[i
], chunks
[i
]);
3184 // Compute section header (except unit_length), abbrev table, and entry pool.
3185 computeHdrAndAbbrevTable(inputChunks
);
3186 uint32_t entryPoolSize
;
3187 std::tie(entryPoolSize
, hdr
.NameCount
) = computeEntryPool(inputChunks
);
3188 hdr
.BucketCount
= dwarf::getDebugNamesBucketCount(hdr
.NameCount
);
3190 // Compute the section size. Subtract 4 to get the unit_length for DWARF32.
3191 uint32_t hdrSize
= getDebugNamesHeaderSize(hdr
.AugmentationStringSize
);
3192 size
= findDebugNamesOffsets(hdrSize
, hdr
).EntriesBase
+ entryPoolSize
;
3193 hdr
.UnitLength
= size
- 4;
3196 template <class ELFT
>
3197 DebugNamesSection
<ELFT
>::DebugNamesSection(Ctx
&ctx
)
3198 : DebugNamesBaseSection(ctx
) {
3199 init([&](InputFile
*f
, InputChunk
&inputChunk
, OutputChunk
&chunk
) {
3200 auto *file
= cast
<ObjFile
<ELFT
>>(f
);
3201 DWARFContext
dwarf(std::make_unique
<LLDDwarfObj
<ELFT
>>(file
));
3202 auto &dobj
= static_cast<const LLDDwarfObj
<ELFT
> &>(dwarf
.getDWARFObj());
3203 chunk
.infoSec
= dobj
.getInfoSection();
3204 DWARFDataExtractor
namesExtractor(dobj
, dobj
.getNamesSection(),
3205 ELFT::Endianness
== endianness::little
,
3206 ELFT::Is64Bits
? 8 : 4);
3207 // .debug_str is needed to get symbol names from string offsets.
3208 DataExtractor
strExtractor(dobj
.getStrSection(),
3209 ELFT::Endianness
== endianness::little
,
3210 ELFT::Is64Bits
? 8 : 4);
3211 inputChunk
.section
= dobj
.getNamesSection();
3213 inputChunk
.llvmDebugNames
.emplace(namesExtractor
, strExtractor
);
3214 if (Error e
= inputChunk
.llvmDebugNames
->extract()) {
3215 Err(ctx
) << dobj
.getNamesSection().sec
<< ": " << std::move(e
);
3218 ctx
, inputChunk
, chunk
, namesExtractor
, strExtractor
,
3219 [&chunk
, namesData
= dobj
.getNamesSection().Data
.data()](
3220 uint32_t numCus
, const DWARFDebugNames::Header
&hdr
,
3221 const DWARFDebugNames::DWARFDebugNamesOffsets
&locs
) {
3222 // Read CU offsets, which are relocated by .debug_info + X
3223 // relocations. Record the section offset to be relocated by
3224 // `finalizeContents`.
3225 chunk
.compUnits
.resize_for_overwrite(numCus
+ hdr
.CompUnitCount
);
3226 for (auto i
: seq(hdr
.CompUnitCount
))
3227 chunk
.compUnits
[numCus
+ i
] = locs
.CUsBase
+ i
* 4;
3229 // Read entry offsets.
3230 const char *p
= namesData
+ locs
.EntryOffsetsBase
;
3231 SmallVector
<uint32_t, 0> entryOffsets
;
3232 entryOffsets
.resize_for_overwrite(hdr
.NameCount
);
3233 for (uint32_t &offset
: entryOffsets
)
3234 offset
= endian::readNext
<uint32_t, ELFT::Endianness
, unaligned
>(p
);
3235 return entryOffsets
;
3240 template <class ELFT
>
3241 template <class RelTy
>
3242 void DebugNamesSection
<ELFT
>::getNameRelocs(
3243 const InputFile
&file
, DenseMap
<uint32_t, uint32_t> &relocs
,
3244 Relocs
<RelTy
> rels
) {
3245 for (const RelTy
&rel
: rels
) {
3246 Symbol
&sym
= file
.getRelocTargetSym(rel
);
3247 relocs
[rel
.r_offset
] = sym
.getVA(ctx
, getAddend
<ELFT
>(rel
));
3251 template <class ELFT
> void DebugNamesSection
<ELFT
>::finalizeContents() {
3252 // Get relocations of .debug_names sections.
3253 auto relocs
= std::make_unique
<DenseMap
<uint32_t, uint32_t>[]>(numChunks
);
3254 parallelFor(0, numChunks
, [&](size_t i
) {
3255 InputSection
*sec
= inputSections
[i
];
3256 invokeOnRelocs(*sec
, getNameRelocs
, *sec
->file
, relocs
.get()[i
]);
3258 // Relocate CU offsets with .debug_info + X relocations.
3259 OutputChunk
&chunk
= chunks
.get()[i
];
3260 for (auto [j
, cuOffset
] : enumerate(chunk
.compUnits
))
3261 cuOffset
= relocs
.get()[i
].lookup(cuOffset
);
3264 // Relocate string offsets in the name table with .debug_str + X relocations.
3265 parallelForEach(nameVecs
, [&](auto &nameVec
) {
3266 for (NameEntry
&ne
: nameVec
)
3267 ne
.stringOffset
= relocs
.get()[ne
.chunkIdx
].lookup(ne
.stringOffset
);
3271 template <class ELFT
> void DebugNamesSection
<ELFT
>::writeTo(uint8_t *buf
) {
3272 [[maybe_unused
]] const uint8_t *const beginBuf
= buf
;
3273 // Write the header.
3274 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.UnitLength
);
3275 endian::writeNext
<uint16_t, ELFT::Endianness
>(buf
, hdr
.Version
);
3276 buf
+= 2; // padding
3277 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.CompUnitCount
);
3278 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.LocalTypeUnitCount
);
3279 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.ForeignTypeUnitCount
);
3280 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.BucketCount
);
3281 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.NameCount
);
3282 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, hdr
.AbbrevTableSize
);
3283 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
,
3284 hdr
.AugmentationStringSize
);
3285 memcpy(buf
, hdr
.AugmentationString
.c_str(), hdr
.AugmentationString
.size());
3286 buf
+= hdr
.AugmentationStringSize
;
3288 // Write the CU list.
3289 for (auto &chunk
: getChunks())
3290 for (uint32_t cuOffset
: chunk
.compUnits
)
3291 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, cuOffset
);
3293 // TODO: Write the local TU list, then the foreign TU list..
3295 // Write the hash lookup table.
3296 SmallVector
<SmallVector
<NameEntry
*, 0>, 0> buckets(hdr
.BucketCount
);
3297 // Symbols enter into a bucket whose index is the hash modulo bucket_count.
3298 for (auto &nameVec
: nameVecs
)
3299 for (NameEntry
&ne
: nameVec
)
3300 buckets
[ne
.hashValue
% hdr
.BucketCount
].push_back(&ne
);
3302 // Write buckets (accumulated bucket counts).
3303 uint32_t bucketIdx
= 1;
3304 for (const SmallVector
<NameEntry
*, 0> &bucket
: buckets
) {
3305 if (!bucket
.empty())
3306 endian::write32
<ELFT::Endianness
>(buf
, bucketIdx
);
3308 bucketIdx
+= bucket
.size();
3310 // Write the hashes.
3311 for (const SmallVector
<NameEntry
*, 0> &bucket
: buckets
)
3312 for (const NameEntry
*e
: bucket
)
3313 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, e
->hashValue
);
3315 // Write the name table. The name entries are ordered by bucket_idx and
3316 // correspond one-to-one with the hash lookup table.
3318 // First, write the relocated string offsets.
3319 for (const SmallVector
<NameEntry
*, 0> &bucket
: buckets
)
3320 for (const NameEntry
*ne
: bucket
)
3321 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, ne
->stringOffset
);
3323 // Then write the entry offsets.
3324 for (const SmallVector
<NameEntry
*, 0> &bucket
: buckets
)
3325 for (const NameEntry
*ne
: bucket
)
3326 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, ne
->entryOffset
);
3328 // Write the abbrev table.
3329 buf
= llvm::copy(abbrevTableBuf
, buf
);
3331 // Write the entry pool. Unlike the name table, the name entries follow the
3332 // nameVecs order computed by `computeEntryPool`.
3333 for (auto &nameVec
: nameVecs
) {
3334 for (NameEntry
&ne
: nameVec
) {
3335 // Write all the entries for the string.
3336 for (const IndexEntry
&ie
: ne
.entries()) {
3337 buf
+= encodeULEB128(ie
.abbrevCode
, buf
);
3338 for (AttrValue value
: ie
.attrValues
) {
3339 switch (value
.attrSize
) {
3341 *buf
++ = value
.attrValue
;
3344 endian::writeNext
<uint16_t, ELFT::Endianness
>(buf
, value
.attrValue
);
3347 endian::writeNext
<uint32_t, ELFT::Endianness
>(buf
, value
.attrValue
);
3350 llvm_unreachable("invalid attrSize");
3354 ++buf
; // index entry sentinel
3357 assert(uint64_t(buf
- beginBuf
) == size
);
3360 GdbIndexSection::GdbIndexSection(Ctx
&ctx
)
3361 : SyntheticSection(ctx
, 0, SHT_PROGBITS
, 1, ".gdb_index") {}
3363 // Returns the desired size of an on-disk hash table for a .gdb_index section.
3364 // There's a tradeoff between size and collision rate. We aim 75% utilization.
3365 size_t GdbIndexSection::computeSymtabSize() const {
3366 return std::max
<size_t>(NextPowerOf2(symbols
.size() * 4 / 3), 1024);
3369 static SmallVector
<GdbIndexSection::CuEntry
, 0>
3370 readCuList(DWARFContext
&dwarf
) {
3371 SmallVector
<GdbIndexSection::CuEntry
, 0> ret
;
3372 for (std::unique_ptr
<DWARFUnit
> &cu
: dwarf
.compile_units())
3373 ret
.push_back({cu
->getOffset(), cu
->getLength() + 4});
3377 static SmallVector
<GdbIndexSection::AddressEntry
, 0>
3378 readAddressAreas(Ctx
&ctx
, DWARFContext
&dwarf
, InputSection
*sec
) {
3379 SmallVector
<GdbIndexSection::AddressEntry
, 0> ret
;
3382 for (std::unique_ptr
<DWARFUnit
> &cu
: dwarf
.compile_units()) {
3383 if (Error e
= cu
->tryExtractDIEsIfNeeded(false)) {
3384 Warn(ctx
) << sec
<< ": " << std::move(e
);
3387 Expected
<DWARFAddressRangesVector
> ranges
= cu
->collectAddressRanges();
3389 Warn(ctx
) << sec
<< ": " << ranges
.takeError();
3393 ArrayRef
<InputSectionBase
*> sections
= sec
->file
->getSections();
3394 for (DWARFAddressRange
&r
: *ranges
) {
3395 if (r
.SectionIndex
== -1ULL)
3397 // Range list with zero size has no effect.
3398 InputSectionBase
*s
= sections
[r
.SectionIndex
];
3399 if (s
&& s
!= &InputSection::discarded
&& s
->isLive())
3400 if (r
.LowPC
!= r
.HighPC
)
3401 ret
.push_back({cast
<InputSection
>(s
), r
.LowPC
, r
.HighPC
, cuIdx
});
3409 template <class ELFT
>
3410 static SmallVector
<GdbIndexSection::NameAttrEntry
, 0>
3411 readPubNamesAndTypes(Ctx
&ctx
, const LLDDwarfObj
<ELFT
> &obj
,
3412 const SmallVectorImpl
<GdbIndexSection::CuEntry
> &cus
) {
3413 const LLDDWARFSection
&pubNames
= obj
.getGnuPubnamesSection();
3414 const LLDDWARFSection
&pubTypes
= obj
.getGnuPubtypesSection();
3416 SmallVector
<GdbIndexSection::NameAttrEntry
, 0> ret
;
3417 for (const LLDDWARFSection
*pub
: {&pubNames
, &pubTypes
}) {
3418 DWARFDataExtractor
data(obj
, *pub
, ELFT::Endianness
== endianness::little
,
3419 ELFT::Is64Bits
? 8 : 4);
3420 DWARFDebugPubTable table
;
3421 table
.extract(data
, /*GnuStyle=*/true, [&](Error e
) {
3422 Warn(ctx
) << pub
->sec
<< ": " << std::move(e
);
3424 for (const DWARFDebugPubTable::Set
&set
: table
.getData()) {
3425 // The value written into the constant pool is kind << 24 | cuIndex. As we
3426 // don't know how many compilation units precede this object to compute
3427 // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add
3428 // the number of preceding compilation units later.
3429 uint32_t i
= llvm::partition_point(cus
,
3430 [&](GdbIndexSection::CuEntry cu
) {
3431 return cu
.cuOffset
< set
.Offset
;
3434 for (const DWARFDebugPubTable::Entry
&ent
: set
.Entries
)
3435 ret
.push_back({{ent
.Name
, computeGdbHash(ent
.Name
)},
3436 (ent
.Descriptor
.toBits() << 24) | i
});
3442 // Create a list of symbols from a given list of symbol names and types
3443 // by uniquifying them by name.
3444 static std::pair
<SmallVector
<GdbIndexSection::GdbSymbol
, 0>, size_t>
3447 ArrayRef
<SmallVector
<GdbIndexSection::NameAttrEntry
, 0>> nameAttrs
,
3448 const SmallVector
<GdbIndexSection::GdbChunk
, 0> &chunks
) {
3449 using GdbSymbol
= GdbIndexSection::GdbSymbol
;
3450 using NameAttrEntry
= GdbIndexSection::NameAttrEntry
;
3452 // For each chunk, compute the number of compilation units preceding it.
3454 std::unique_ptr
<uint32_t[]> cuIdxs(new uint32_t[chunks
.size()]);
3455 for (uint32_t i
= 0, e
= chunks
.size(); i
!= e
; ++i
) {
3457 cuIdx
+= chunks
[i
].compilationUnits
.size();
3460 // Collect the compilation unitss for each unique name. Speed it up using
3461 // multi-threading as the number of symbols can be in the order of millions.
3462 // Shard GdbSymbols by hash's high bits.
3463 constexpr size_t numShards
= 32;
3464 const size_t concurrency
=
3465 llvm::bit_floor(std::min
<size_t>(ctx
.arg
.threadCount
, numShards
));
3466 const size_t shift
= 32 - llvm::countr_zero(numShards
);
3468 std::make_unique
<DenseMap
<CachedHashStringRef
, size_t>[]>(numShards
);
3469 auto symbols
= std::make_unique
<SmallVector
<GdbSymbol
, 0>[]>(numShards
);
3470 parallelFor(0, concurrency
, [&](size_t threadId
) {
3472 for (ArrayRef
<NameAttrEntry
> entries
: nameAttrs
) {
3473 for (const NameAttrEntry
&ent
: entries
) {
3474 size_t shardId
= ent
.name
.hash() >> shift
;
3475 if ((shardId
& (concurrency
- 1)) != threadId
)
3478 uint32_t v
= ent
.cuIndexAndAttrs
+ cuIdxs
[i
];
3479 auto [it
, inserted
] =
3480 map
[shardId
].try_emplace(ent
.name
, symbols
[shardId
].size());
3482 symbols
[shardId
].push_back({ent
.name
, {v
}, 0, 0});
3484 symbols
[shardId
][it
->second
].cuVector
.push_back(v
);
3490 size_t numSymbols
= 0;
3491 for (ArrayRef
<GdbSymbol
> v
: ArrayRef(symbols
.get(), numShards
))
3492 numSymbols
+= v
.size();
3494 // The return type is a flattened vector, so we'll copy each vector
3496 SmallVector
<GdbSymbol
, 0> ret
;
3497 ret
.reserve(numSymbols
);
3498 for (SmallVector
<GdbSymbol
, 0> &vec
:
3499 MutableArrayRef(symbols
.get(), numShards
))
3500 for (GdbSymbol
&sym
: vec
)
3501 ret
.push_back(std::move(sym
));
3503 // CU vectors and symbol names are adjacent in the output file.
3504 // We can compute their offsets in the output file now.
3506 for (GdbSymbol
&sym
: ret
) {
3507 sym
.cuVectorOff
= off
;
3508 off
+= (sym
.cuVector
.size() + 1) * 4;
3510 for (GdbSymbol
&sym
: ret
) {
3512 off
+= sym
.name
.size() + 1;
3514 // If off overflows, the last symbol's nameOff likely overflows.
3515 if (!isUInt
<32>(off
))
3516 Err(ctx
) << "--gdb-index: constant pool size (" << off
3517 << ") exceeds UINT32_MAX";
3522 // Returns a newly-created .gdb_index section.
3523 template <class ELFT
>
3524 std::unique_ptr
<GdbIndexSection
> GdbIndexSection::create(Ctx
&ctx
) {
3525 llvm::TimeTraceScope
timeScope("Create gdb index");
3527 // Collect InputFiles with .debug_info. See the comment in
3528 // LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future,
3529 // note that isec->data() may uncompress the full content, which should be
3531 SetVector
<InputFile
*> files
;
3532 for (InputSectionBase
*s
: ctx
.inputSections
) {
3533 InputSection
*isec
= dyn_cast
<InputSection
>(s
);
3536 // .debug_gnu_pub{names,types} are useless in executables.
3537 // They are present in input object files solely for creating
3538 // a .gdb_index. So we can remove them from the output.
3539 if (s
->name
== ".debug_gnu_pubnames" || s
->name
== ".debug_gnu_pubtypes")
3541 else if (isec
->name
== ".debug_info")
3542 files
.insert(isec
->file
);
3544 // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs.
3545 llvm::erase_if(ctx
.inputSections
, [](InputSectionBase
*s
) {
3546 if (auto *isec
= dyn_cast
<InputSection
>(s
))
3547 if (InputSectionBase
*rel
= isec
->getRelocatedSection())
3548 return !rel
->isLive();
3549 return !s
->isLive();
3552 SmallVector
<GdbChunk
, 0> chunks(files
.size());
3553 SmallVector
<SmallVector
<NameAttrEntry
, 0>, 0> nameAttrs(files
.size());
3555 parallelFor(0, files
.size(), [&](size_t i
) {
3556 // To keep memory usage low, we don't want to keep cached DWARFContext, so
3557 // avoid getDwarf() here.
3558 ObjFile
<ELFT
> *file
= cast
<ObjFile
<ELFT
>>(files
[i
]);
3559 DWARFContext
dwarf(std::make_unique
<LLDDwarfObj
<ELFT
>>(file
));
3560 auto &dobj
= static_cast<const LLDDwarfObj
<ELFT
> &>(dwarf
.getDWARFObj());
3562 // If the are multiple compile units .debug_info (very rare ld -r --unique),
3563 // this only picks the last one. Other address ranges are lost.
3564 chunks
[i
].sec
= dobj
.getInfoSection();
3565 chunks
[i
].compilationUnits
= readCuList(dwarf
);
3566 chunks
[i
].addressAreas
= readAddressAreas(ctx
, dwarf
, chunks
[i
].sec
);
3568 readPubNamesAndTypes
<ELFT
>(ctx
, dobj
, chunks
[i
].compilationUnits
);
3571 auto ret
= std::make_unique
<GdbIndexSection
>(ctx
);
3572 ret
->chunks
= std::move(chunks
);
3573 std::tie(ret
->symbols
, ret
->size
) =
3574 createSymbols(ctx
, nameAttrs
, ret
->chunks
);
3576 // Count the areas other than the constant pool.
3577 ret
->size
+= sizeof(GdbIndexHeader
) + ret
->computeSymtabSize() * 8;
3578 for (GdbChunk
&chunk
: ret
->chunks
)
3580 chunk
.compilationUnits
.size() * 16 + chunk
.addressAreas
.size() * 20;
3585 void GdbIndexSection::writeTo(uint8_t *buf
) {
3586 // Write the header.
3587 auto *hdr
= reinterpret_cast<GdbIndexHeader
*>(buf
);
3588 uint8_t *start
= buf
;
3590 buf
+= sizeof(*hdr
);
3592 // Write the CU list.
3593 hdr
->cuListOff
= buf
- start
;
3594 for (GdbChunk
&chunk
: chunks
) {
3595 for (CuEntry
&cu
: chunk
.compilationUnits
) {
3596 write64le(buf
, chunk
.sec
->outSecOff
+ cu
.cuOffset
);
3597 write64le(buf
+ 8, cu
.cuLength
);
3602 // Write the address area.
3603 hdr
->cuTypesOff
= buf
- start
;
3604 hdr
->addressAreaOff
= buf
- start
;
3606 for (GdbChunk
&chunk
: chunks
) {
3607 for (AddressEntry
&e
: chunk
.addressAreas
) {
3608 // In the case of ICF there may be duplicate address range entries.
3609 const uint64_t baseAddr
= e
.section
->repl
->getVA(0);
3610 write64le(buf
, baseAddr
+ e
.lowAddress
);
3611 write64le(buf
+ 8, baseAddr
+ e
.highAddress
);
3612 write32le(buf
+ 16, e
.cuIndex
+ cuOff
);
3615 cuOff
+= chunk
.compilationUnits
.size();
3618 // Write the on-disk open-addressing hash table containing symbols.
3619 hdr
->symtabOff
= buf
- start
;
3620 size_t symtabSize
= computeSymtabSize();
3621 uint32_t mask
= symtabSize
- 1;
3623 for (GdbSymbol
&sym
: symbols
) {
3624 uint32_t h
= sym
.name
.hash();
3625 uint32_t i
= h
& mask
;
3626 uint32_t step
= ((h
* 17) & mask
) | 1;
3628 while (read32le(buf
+ i
* 8))
3629 i
= (i
+ step
) & mask
;
3631 write32le(buf
+ i
* 8, sym
.nameOff
);
3632 write32le(buf
+ i
* 8 + 4, sym
.cuVectorOff
);
3635 buf
+= symtabSize
* 8;
3637 // Write the string pool.
3638 hdr
->constantPoolOff
= buf
- start
;
3639 parallelForEach(symbols
, [&](GdbSymbol
&sym
) {
3640 memcpy(buf
+ sym
.nameOff
, sym
.name
.data(), sym
.name
.size());
3643 // Write the CU vectors.
3644 for (GdbSymbol
&sym
: symbols
) {
3645 write32le(buf
, sym
.cuVector
.size());
3647 for (uint32_t val
: sym
.cuVector
) {
3648 write32le(buf
, val
);
3654 bool GdbIndexSection::isNeeded() const { return !chunks
.empty(); }
3656 EhFrameHeader::EhFrameHeader(Ctx
&ctx
)
3657 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_PROGBITS
, 4, ".eh_frame_hdr") {}
3659 void EhFrameHeader::writeTo(uint8_t *buf
) {
3660 // Unlike most sections, the EhFrameHeader section is written while writing
3661 // another section, namely EhFrameSection, which calls the write() function
3662 // below from its writeTo() function. This is necessary because the contents
3663 // of EhFrameHeader depend on the relocated contents of EhFrameSection and we
3664 // don't know which order the sections will be written in.
3667 // .eh_frame_hdr contains a binary search table of pointers to FDEs.
3668 // Each entry of the search table consists of two values,
3669 // the starting PC from where FDEs covers, and the FDE's address.
3670 // It is sorted by PC.
3671 void EhFrameHeader::write() {
3672 uint8_t *buf
= ctx
.bufferStart
+ getParent()->offset
+ outSecOff
;
3673 using FdeData
= EhFrameSection::FdeData
;
3674 SmallVector
<FdeData
, 0> fdes
= getPartition(ctx
).ehFrame
->getFdeData();
3677 buf
[1] = DW_EH_PE_pcrel
| DW_EH_PE_sdata4
;
3678 buf
[2] = DW_EH_PE_udata4
;
3679 buf
[3] = DW_EH_PE_datarel
| DW_EH_PE_sdata4
;
3680 write32(ctx
, buf
+ 4,
3681 getPartition(ctx
).ehFrame
->getParent()->addr
- this->getVA() - 4);
3682 write32(ctx
, buf
+ 8, fdes
.size());
3685 for (FdeData
&fde
: fdes
) {
3686 write32(ctx
, buf
, fde
.pcRel
);
3687 write32(ctx
, buf
+ 4, fde
.fdeVARel
);
3692 size_t EhFrameHeader::getSize() const {
3693 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
3694 return 12 + getPartition(ctx
).ehFrame
->numFdes
* 8;
3697 bool EhFrameHeader::isNeeded() const {
3698 return isLive() && getPartition(ctx
).ehFrame
->isNeeded();
3701 VersionDefinitionSection::VersionDefinitionSection(Ctx
&ctx
)
3702 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_GNU_verdef
, sizeof(uint32_t),
3703 ".gnu.version_d") {}
3705 StringRef
VersionDefinitionSection::getFileDefName() {
3706 if (!getPartition(ctx
).name
.empty())
3707 return getPartition(ctx
).name
;
3708 if (!ctx
.arg
.soName
.empty())
3709 return ctx
.arg
.soName
;
3710 return ctx
.arg
.outputFile
;
3713 void VersionDefinitionSection::finalizeContents() {
3714 fileDefNameOff
= getPartition(ctx
).dynStrTab
->addString(getFileDefName());
3715 for (const VersionDefinition
&v
: namedVersionDefs(ctx
))
3716 verDefNameOffs
.push_back(getPartition(ctx
).dynStrTab
->addString(v
.name
));
3718 if (OutputSection
*sec
= getPartition(ctx
).dynStrTab
->getParent())
3719 getParent()->link
= sec
->sectionIndex
;
3721 // sh_info should be set to the number of definitions. This fact is missed in
3722 // documentation, but confirmed by binutils community:
3723 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
3724 getParent()->info
= getVerDefNum(ctx
);
3727 void VersionDefinitionSection::writeOne(uint8_t *buf
, uint32_t index
,
3728 StringRef name
, size_t nameOff
) {
3729 uint16_t flags
= index
== 1 ? VER_FLG_BASE
: 0;
3732 write16(ctx
, buf
, 1); // vd_version
3733 write16(ctx
, buf
+ 2, flags
); // vd_flags
3734 write16(ctx
, buf
+ 4, index
); // vd_ndx
3735 write16(ctx
, buf
+ 6, 1); // vd_cnt
3736 write32(ctx
, buf
+ 8, hashSysV(name
)); // vd_hash
3737 write32(ctx
, buf
+ 12, 20); // vd_aux
3738 write32(ctx
, buf
+ 16, 28); // vd_next
3741 write32(ctx
, buf
+ 20, nameOff
); // vda_name
3742 write32(ctx
, buf
+ 24, 0); // vda_next
3745 void VersionDefinitionSection::writeTo(uint8_t *buf
) {
3746 writeOne(buf
, 1, getFileDefName(), fileDefNameOff
);
3748 auto nameOffIt
= verDefNameOffs
.begin();
3749 for (const VersionDefinition
&v
: namedVersionDefs(ctx
)) {
3751 writeOne(buf
, v
.id
, v
.name
, *nameOffIt
++);
3754 // Need to terminate the last version definition.
3755 write32(ctx
, buf
+ 16, 0); // vd_next
3758 size_t VersionDefinitionSection::getSize() const {
3759 return EntrySize
* getVerDefNum(ctx
);
3762 // .gnu.version is a table where each entry is 2 byte long.
3763 VersionTableSection::VersionTableSection(Ctx
&ctx
)
3764 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_GNU_versym
, sizeof(uint16_t),
3769 void VersionTableSection::finalizeContents() {
3770 // At the moment of june 2016 GNU docs does not mention that sh_link field
3771 // should be set, but Sun docs do. Also readelf relies on this field.
3772 getParent()->link
= getPartition(ctx
).dynSymTab
->getParent()->sectionIndex
;
3775 size_t VersionTableSection::getSize() const {
3776 return (getPartition(ctx
).dynSymTab
->getSymbols().size() + 1) * 2;
3779 void VersionTableSection::writeTo(uint8_t *buf
) {
3781 for (const SymbolTableEntry
&s
: getPartition(ctx
).dynSymTab
->getSymbols()) {
3782 // For an unextracted lazy symbol (undefined weak), it must have been
3783 // converted to Undefined and have VER_NDX_GLOBAL version here.
3784 assert(!s
.sym
->isLazy());
3785 write16(ctx
, buf
, s
.sym
->versionId
);
3790 bool VersionTableSection::isNeeded() const {
3792 (getPartition(ctx
).verDef
|| getPartition(ctx
).verNeed
->isNeeded());
3795 void elf::addVerneed(Ctx
&ctx
, Symbol
&ss
) {
3796 auto &file
= cast
<SharedFile
>(*ss
.file
);
3797 if (ss
.versionId
== VER_NDX_GLOBAL
)
3800 if (file
.vernauxs
.empty())
3801 file
.vernauxs
.resize(file
.verdefs
.size());
3803 // Select a version identifier for the vernaux data structure, if we haven't
3804 // already allocated one. The verdef identifiers cover the range
3805 // [1..getVerDefNum(ctx)]; this causes the vernaux identifiers to start from
3806 // getVerDefNum(ctx)+1.
3807 if (file
.vernauxs
[ss
.versionId
] == 0)
3808 file
.vernauxs
[ss
.versionId
] = ++ctx
.vernauxNum
+ getVerDefNum(ctx
);
3810 ss
.versionId
= file
.vernauxs
[ss
.versionId
];
3813 template <class ELFT
>
3814 VersionNeedSection
<ELFT
>::VersionNeedSection(Ctx
&ctx
)
3815 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_GNU_verneed
, sizeof(uint32_t),
3816 ".gnu.version_r") {}
3818 template <class ELFT
> void VersionNeedSection
<ELFT
>::finalizeContents() {
3819 for (SharedFile
*f
: ctx
.sharedFiles
) {
3820 if (f
->vernauxs
.empty())
3822 verneeds
.emplace_back();
3823 Verneed
&vn
= verneeds
.back();
3824 vn
.nameStrTab
= getPartition(ctx
).dynStrTab
->addString(f
->soName
);
3825 bool isLibc
= ctx
.arg
.relrGlibc
&& f
->soName
.starts_with("libc.so.");
3826 bool isGlibc2
= false;
3827 for (unsigned i
= 0; i
!= f
->vernauxs
.size(); ++i
) {
3828 if (f
->vernauxs
[i
] == 0)
3831 reinterpret_cast<const typename
ELFT::Verdef
*>(f
->verdefs
[i
]);
3832 StringRef
ver(f
->getStringTable().data() + verdef
->getAux()->vda_name
);
3833 if (isLibc
&& ver
.starts_with("GLIBC_2."))
3835 vn
.vernauxs
.push_back({verdef
->vd_hash
, f
->vernauxs
[i
],
3836 getPartition(ctx
).dynStrTab
->addString(ver
)});
3839 const char *ver
= "GLIBC_ABI_DT_RELR";
3840 vn
.vernauxs
.push_back({hashSysV(ver
),
3841 ++ctx
.vernauxNum
+ getVerDefNum(ctx
),
3842 getPartition(ctx
).dynStrTab
->addString(ver
)});
3846 if (OutputSection
*sec
= getPartition(ctx
).dynStrTab
->getParent())
3847 getParent()->link
= sec
->sectionIndex
;
3848 getParent()->info
= verneeds
.size();
3851 template <class ELFT
> void VersionNeedSection
<ELFT
>::writeTo(uint8_t *buf
) {
3852 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
3853 auto *verneed
= reinterpret_cast<Elf_Verneed
*>(buf
);
3854 auto *vernaux
= reinterpret_cast<Elf_Vernaux
*>(verneed
+ verneeds
.size());
3856 for (auto &vn
: verneeds
) {
3857 // Create an Elf_Verneed for this DSO.
3858 verneed
->vn_version
= 1;
3859 verneed
->vn_cnt
= vn
.vernauxs
.size();
3860 verneed
->vn_file
= vn
.nameStrTab
;
3862 reinterpret_cast<char *>(vernaux
) - reinterpret_cast<char *>(verneed
);
3863 verneed
->vn_next
= sizeof(Elf_Verneed
);
3866 // Create the Elf_Vernauxs for this Elf_Verneed.
3867 for (auto &vna
: vn
.vernauxs
) {
3868 vernaux
->vna_hash
= vna
.hash
;
3869 vernaux
->vna_flags
= 0;
3870 vernaux
->vna_other
= vna
.verneedIndex
;
3871 vernaux
->vna_name
= vna
.nameStrTab
;
3872 vernaux
->vna_next
= sizeof(Elf_Vernaux
);
3876 vernaux
[-1].vna_next
= 0;
3878 verneed
[-1].vn_next
= 0;
3881 template <class ELFT
> size_t VersionNeedSection
<ELFT
>::getSize() const {
3882 return verneeds
.size() * sizeof(Elf_Verneed
) +
3883 ctx
.vernauxNum
* sizeof(Elf_Vernaux
);
3886 template <class ELFT
> bool VersionNeedSection
<ELFT
>::isNeeded() const {
3887 return isLive() && ctx
.vernauxNum
!= 0;
3890 void MergeSyntheticSection::addSection(MergeInputSection
*ms
) {
3892 sections
.push_back(ms
);
3893 assert(addralign
== ms
->addralign
|| !(ms
->flags
& SHF_STRINGS
));
3894 addralign
= std::max(addralign
, ms
->addralign
);
3897 MergeTailSection::MergeTailSection(Ctx
&ctx
, StringRef name
, uint32_t type
,
3898 uint64_t flags
, uint32_t alignment
)
3899 : MergeSyntheticSection(ctx
, name
, type
, flags
, alignment
),
3900 builder(StringTableBuilder::RAW
, llvm::Align(alignment
)) {}
3902 size_t MergeTailSection::getSize() const { return builder
.getSize(); }
3904 void MergeTailSection::writeTo(uint8_t *buf
) { builder
.write(buf
); }
3906 void MergeTailSection::finalizeContents() {
3907 // Add all string pieces to the string table builder to create section
3909 for (MergeInputSection
*sec
: sections
)
3910 for (size_t i
= 0, e
= sec
->pieces
.size(); i
!= e
; ++i
)
3911 if (sec
->pieces
[i
].live
)
3912 builder
.add(sec
->getData(i
));
3914 // Fix the string table content. After this, the contents will never change.
3917 // finalize() fixed tail-optimized strings, so we can now get
3918 // offsets of strings. Get an offset for each string and save it
3919 // to a corresponding SectionPiece for easy access.
3920 for (MergeInputSection
*sec
: sections
)
3921 for (size_t i
= 0, e
= sec
->pieces
.size(); i
!= e
; ++i
)
3922 if (sec
->pieces
[i
].live
)
3923 sec
->pieces
[i
].outputOff
= builder
.getOffset(sec
->getData(i
));
3926 void MergeNoTailSection::writeTo(uint8_t *buf
) {
3927 parallelFor(0, numShards
,
3928 [&](size_t i
) { shards
[i
].write(buf
+ shardOffsets
[i
]); });
3931 // This function is very hot (i.e. it can take several seconds to finish)
3932 // because sometimes the number of inputs is in an order of magnitude of
3933 // millions. So, we use multi-threading.
3935 // For any strings S and T, we know S is not mergeable with T if S's hash
3936 // value is different from T's. If that's the case, we can safely put S and
3937 // T into different string builders without worrying about merge misses.
3938 // We do it in parallel.
3939 void MergeNoTailSection::finalizeContents() {
3940 // Initializes string table builders.
3941 for (size_t i
= 0; i
< numShards
; ++i
)
3942 shards
.emplace_back(StringTableBuilder::RAW
, llvm::Align(addralign
));
3944 // Concurrency level. Must be a power of 2 to avoid expensive modulo
3945 // operations in the following tight loop.
3946 const size_t concurrency
=
3947 llvm::bit_floor(std::min
<size_t>(ctx
.arg
.threadCount
, numShards
));
3949 // Add section pieces to the builders.
3950 parallelFor(0, concurrency
, [&](size_t threadId
) {
3951 for (MergeInputSection
*sec
: sections
) {
3952 for (size_t i
= 0, e
= sec
->pieces
.size(); i
!= e
; ++i
) {
3953 if (!sec
->pieces
[i
].live
)
3955 size_t shardId
= getShardId(sec
->pieces
[i
].hash
);
3956 if ((shardId
& (concurrency
- 1)) == threadId
)
3957 sec
->pieces
[i
].outputOff
= shards
[shardId
].add(sec
->getData(i
));
3962 // Compute an in-section offset for each shard.
3964 for (size_t i
= 0; i
< numShards
; ++i
) {
3965 shards
[i
].finalizeInOrder();
3966 if (shards
[i
].getSize() > 0)
3967 off
= alignToPowerOf2(off
, addralign
);
3968 shardOffsets
[i
] = off
;
3969 off
+= shards
[i
].getSize();
3973 // So far, section pieces have offsets from beginning of shards, but
3974 // we want offsets from beginning of the whole section. Fix them.
3975 parallelForEach(sections
, [&](MergeInputSection
*sec
) {
3976 for (size_t i
= 0, e
= sec
->pieces
.size(); i
!= e
; ++i
)
3977 if (sec
->pieces
[i
].live
)
3978 sec
->pieces
[i
].outputOff
+=
3979 shardOffsets
[getShardId(sec
->pieces
[i
].hash
)];
3983 template <class ELFT
> void elf::splitSections(Ctx
&ctx
) {
3984 llvm::TimeTraceScope
timeScope("Split sections");
3985 // splitIntoPieces needs to be called on each MergeInputSection
3986 // before calling finalizeContents().
3987 parallelForEach(ctx
.objectFiles
, [](ELFFileBase
*file
) {
3988 for (InputSectionBase
*sec
: file
->getSections()) {
3991 if (auto *s
= dyn_cast
<MergeInputSection
>(sec
))
3992 s
->splitIntoPieces();
3993 else if (auto *eh
= dyn_cast
<EhInputSection
>(sec
))
3999 void elf::combineEhSections(Ctx
&ctx
) {
4000 llvm::TimeTraceScope
timeScope("Combine EH sections");
4001 for (EhInputSection
*sec
: ctx
.ehInputSections
) {
4002 EhFrameSection
&eh
= *sec
->getPartition(ctx
).ehFrame
;
4004 eh
.addralign
= std::max(eh
.addralign
, sec
->addralign
);
4005 eh
.sections
.push_back(sec
);
4006 llvm::append_range(eh
.dependentSections
, sec
->dependentSections
);
4009 if (!ctx
.mainPart
->armExidx
)
4011 llvm::erase_if(ctx
.inputSections
, [&](InputSectionBase
*s
) {
4012 // Ignore dead sections and the partition end marker (.part.end),
4013 // whose partition number is out of bounds.
4014 if (!s
->isLive() || s
->partition
== 255)
4016 Partition
&part
= s
->getPartition(ctx
);
4017 return s
->kind() == SectionBase::Regular
&& part
.armExidx
&&
4018 part
.armExidx
->addSection(cast
<InputSection
>(s
));
4022 MipsRldMapSection::MipsRldMapSection(Ctx
&ctx
)
4023 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_PROGBITS
,
4024 ctx
.arg
.wordsize
, ".rld_map") {}
4026 ARMExidxSyntheticSection::ARMExidxSyntheticSection(Ctx
&ctx
)
4027 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_LINK_ORDER
, SHT_ARM_EXIDX
,
4028 ctx
.arg
.wordsize
, ".ARM.exidx") {}
4030 static InputSection
*findExidxSection(InputSection
*isec
) {
4031 for (InputSection
*d
: isec
->dependentSections
)
4032 if (d
->type
== SHT_ARM_EXIDX
&& d
->isLive())
4037 static bool isValidExidxSectionDep(InputSection
*isec
) {
4038 return (isec
->flags
& SHF_ALLOC
) && (isec
->flags
& SHF_EXECINSTR
) &&
4039 isec
->getSize() > 0;
4042 bool ARMExidxSyntheticSection::addSection(InputSection
*isec
) {
4043 if (isec
->type
== SHT_ARM_EXIDX
) {
4044 if (InputSection
*dep
= isec
->getLinkOrderDep())
4045 if (isValidExidxSectionDep(dep
)) {
4046 exidxSections
.push_back(isec
);
4047 // Every exidxSection is 8 bytes, we need an estimate of
4048 // size before assignAddresses can be called. Final size
4049 // will only be known after finalize is called.
4055 if (isValidExidxSectionDep(isec
)) {
4056 executableSections
.push_back(isec
);
4060 // FIXME: we do not output a relocation section when --emit-relocs is used
4061 // as we do not have relocation sections for linker generated table entries
4062 // and we would have to erase at a late stage relocations from merged entries.
4063 // Given that exception tables are already position independent and a binary
4064 // analyzer could derive the relocations we choose to erase the relocations.
4065 if (ctx
.arg
.emitRelocs
&& isec
->type
== SHT_REL
)
4066 if (InputSectionBase
*ex
= isec
->getRelocatedSection())
4067 if (isa
<InputSection
>(ex
) && ex
->type
== SHT_ARM_EXIDX
)
4073 // References to .ARM.Extab Sections have bit 31 clear and are not the
4074 // special EXIDX_CANTUNWIND bit-pattern.
4075 static bool isExtabRef(uint32_t unwind
) {
4076 return (unwind
& 0x80000000) == 0 && unwind
!= 0x1;
4079 // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx
4080 // section Prev, where Cur follows Prev in the table. This can be done if the
4081 // unwinding instructions in Cur are identical to Prev. Linker generated
4082 // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an
4084 static bool isDuplicateArmExidxSec(Ctx
&ctx
, InputSection
*prev
,
4085 InputSection
*cur
) {
4086 // Get the last table Entry from the previous .ARM.exidx section. If Prev is
4087 // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.
4088 uint32_t prevUnwind
= 1;
4091 read32(ctx
, prev
->content().data() + prev
->content().size() - 4);
4092 if (isExtabRef(prevUnwind
))
4095 // We consider the unwind instructions of an .ARM.exidx table entry
4096 // a duplicate if the previous unwind instructions if:
4097 // - Both are the special EXIDX_CANTUNWIND.
4098 // - Both are the same inline unwind instructions.
4099 // We do not attempt to follow and check links into .ARM.extab tables as
4100 // consecutive identical entries are rare and the effort to check that they
4101 // are identical is high.
4103 // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.
4105 return prevUnwind
== 1;
4107 for (uint32_t offset
= 4; offset
< (uint32_t)cur
->content().size(); offset
+=8) {
4108 uint32_t curUnwind
= read32(ctx
, cur
->content().data() + offset
);
4109 if (isExtabRef(curUnwind
) || curUnwind
!= prevUnwind
)
4112 // All table entries in this .ARM.exidx Section can be merged into the
4113 // previous Section.
4117 // The .ARM.exidx table must be sorted in ascending order of the address of the
4118 // functions the table describes. std::optionally duplicate adjacent table
4119 // entries can be removed. At the end of the function the executableSections
4120 // must be sorted in ascending order of address, Sentinel is set to the
4121 // InputSection with the highest address and any InputSections that have
4122 // mergeable .ARM.exidx table entries are removed from it.
4123 void ARMExidxSyntheticSection::finalizeContents() {
4124 // Ensure that any fixed-point iterations after the first see the original set
4126 if (!originalExecutableSections
.empty())
4127 executableSections
= originalExecutableSections
;
4128 else if (ctx
.arg
.enableNonContiguousRegions
)
4129 originalExecutableSections
= executableSections
;
4131 // The executableSections and exidxSections that we use to derive the final
4132 // contents of this SyntheticSection are populated before
4133 // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or
4134 // ICF may remove executable InputSections and their dependent .ARM.exidx
4135 // section that we recorded earlier.
4136 auto isDiscarded
= [](const InputSection
*isec
) { return !isec
->isLive(); };
4137 llvm::erase_if(exidxSections
, isDiscarded
);
4138 // We need to remove discarded InputSections and InputSections without
4139 // .ARM.exidx sections that if we generated the .ARM.exidx it would be out
4141 auto isDiscardedOrOutOfRange
= [this](InputSection
*isec
) {
4142 if (!isec
->isLive())
4144 if (findExidxSection(isec
))
4146 int64_t off
= static_cast<int64_t>(isec
->getVA() - getVA());
4147 return off
!= llvm::SignExtend64(off
, 31);
4149 llvm::erase_if(executableSections
, isDiscardedOrOutOfRange
);
4151 // Sort the executable sections that may or may not have associated
4152 // .ARM.exidx sections by order of ascending address. This requires the
4153 // relative positions of InputSections and OutputSections to be known.
4154 auto compareByFilePosition
= [](const InputSection
*a
,
4155 const InputSection
*b
) {
4156 OutputSection
*aOut
= a
->getParent();
4157 OutputSection
*bOut
= b
->getParent();
4160 return aOut
->addr
< bOut
->addr
;
4161 return a
->outSecOff
< b
->outSecOff
;
4163 llvm::stable_sort(executableSections
, compareByFilePosition
);
4164 sentinel
= executableSections
.back();
4165 // std::optionally merge adjacent duplicate entries.
4166 if (ctx
.arg
.mergeArmExidx
) {
4167 SmallVector
<InputSection
*, 0> selectedSections
;
4168 selectedSections
.reserve(executableSections
.size());
4169 selectedSections
.push_back(executableSections
[0]);
4171 for (size_t i
= 1; i
< executableSections
.size(); ++i
) {
4172 InputSection
*ex1
= findExidxSection(executableSections
[prev
]);
4173 InputSection
*ex2
= findExidxSection(executableSections
[i
]);
4174 if (!isDuplicateArmExidxSec(ctx
, ex1
, ex2
)) {
4175 selectedSections
.push_back(executableSections
[i
]);
4179 executableSections
= std::move(selectedSections
);
4181 // offset is within the SyntheticSection.
4184 for (InputSection
*isec
: executableSections
) {
4185 if (InputSection
*d
= findExidxSection(isec
)) {
4186 d
->outSecOff
= offset
;
4187 d
->parent
= getParent();
4188 offset
+= d
->getSize();
4193 // Size includes Sentinel.
4197 InputSection
*ARMExidxSyntheticSection::getLinkOrderDep() const {
4198 return executableSections
.front();
4201 // To write the .ARM.exidx table from the ExecutableSections we have three cases
4202 // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.
4203 // We write the .ARM.exidx section contents and apply its relocations.
4204 // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We
4205 // must write the contents of an EXIDX_CANTUNWIND directly. We use the
4206 // start of the InputSection as the purpose of the linker generated
4207 // section is to terminate the address range of the previous entry.
4208 // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of
4209 // the table to terminate the address range of the final entry.
4210 void ARMExidxSyntheticSection::writeTo(uint8_t *buf
) {
4212 // A linker generated CANTUNWIND entry is made up of two words:
4213 // 0x0 with R_ARM_PREL31 relocation to target.
4214 // 0x1 with EXIDX_CANTUNWIND.
4215 uint64_t offset
= 0;
4216 for (InputSection
*isec
: executableSections
) {
4217 assert(isec
->getParent() != nullptr);
4218 if (InputSection
*d
= findExidxSection(isec
)) {
4219 for (int dataOffset
= 0; dataOffset
!= (int)d
->content().size();
4221 write32(ctx
, buf
+ offset
+ dataOffset
,
4222 read32(ctx
, d
->content().data() + dataOffset
));
4223 // Recalculate outSecOff as finalizeAddressDependentContent()
4224 // may have altered syntheticSection outSecOff.
4225 d
->outSecOff
= offset
+ outSecOff
;
4226 ctx
.target
->relocateAlloc(*d
, buf
+ offset
);
4227 offset
+= d
->getSize();
4229 // A Linker generated CANTUNWIND section.
4230 write32(ctx
, buf
+ offset
+ 0, 0x0);
4231 write32(ctx
, buf
+ offset
+ 4, 0x1);
4232 uint64_t s
= isec
->getVA();
4233 uint64_t p
= getVA() + offset
;
4234 ctx
.target
->relocateNoSym(buf
+ offset
, R_ARM_PREL31
, s
- p
);
4238 // Write Sentinel CANTUNWIND entry.
4239 write32(ctx
, buf
+ offset
+ 0, 0x0);
4240 write32(ctx
, buf
+ offset
+ 4, 0x1);
4241 uint64_t s
= sentinel
->getVA(sentinel
->getSize());
4242 uint64_t p
= getVA() + offset
;
4243 ctx
.target
->relocateNoSym(buf
+ offset
, R_ARM_PREL31
, s
- p
);
4244 assert(size
== offset
+ 8);
4247 bool ARMExidxSyntheticSection::isNeeded() const {
4248 return llvm::any_of(exidxSections
,
4249 [](InputSection
*isec
) { return isec
->isLive(); });
4252 ThunkSection::ThunkSection(Ctx
&ctx
, OutputSection
*os
, uint64_t off
)
4253 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_EXECINSTR
, SHT_PROGBITS
,
4254 ctx
.arg
.emachine
== EM_PPC64
? 16 : 4, ".text.thunk") {
4256 this->outSecOff
= off
;
4259 size_t ThunkSection::getSize() const {
4260 if (roundUpSizeForErrata
)
4261 return alignTo(size
, 4096);
4265 void ThunkSection::addThunk(Thunk
*t
) {
4266 thunks
.push_back(t
);
4267 t
->addSymbols(*this);
4270 void ThunkSection::writeTo(uint8_t *buf
) {
4271 for (Thunk
*t
: thunks
)
4272 t
->writeTo(buf
+ t
->offset
);
4275 InputSection
*ThunkSection::getTargetInputSection() const {
4278 const Thunk
*t
= thunks
.front();
4279 return t
->getTargetInputSection();
4282 bool ThunkSection::assignOffsets() {
4284 for (Thunk
*t
: thunks
) {
4285 off
= alignToPowerOf2(off
, t
->alignment
);
4287 uint32_t size
= t
->size();
4288 t
->getThunkTargetSym()->size
= size
;
4291 bool changed
= off
!= size
;
4296 PPC32Got2Section::PPC32Got2Section(Ctx
&ctx
)
4297 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
, SHT_PROGBITS
, 4, ".got2") {}
4299 bool PPC32Got2Section::isNeeded() const {
4300 // See the comment below. This is not needed if there is no other
4302 for (SectionCommand
*cmd
: getParent()->commands
)
4303 if (auto *isd
= dyn_cast
<InputSectionDescription
>(cmd
))
4304 for (InputSection
*isec
: isd
->sections
)
4310 void PPC32Got2Section::finalizeContents() {
4311 // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in
4312 // .got2 . This function computes outSecOff of each .got2 to be used in
4313 // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is
4314 // to collect input sections named ".got2".
4315 for (SectionCommand
*cmd
: getParent()->commands
)
4316 if (auto *isd
= dyn_cast
<InputSectionDescription
>(cmd
)) {
4317 for (InputSection
*isec
: isd
->sections
) {
4318 // isec->file may be nullptr for MergeSyntheticSection.
4319 if (isec
!= this && isec
->file
)
4320 isec
->file
->ppc32Got2
= isec
;
4325 // If linking position-dependent code then the table will store the addresses
4326 // directly in the binary so the section has type SHT_PROGBITS. If linking
4327 // position-independent code the section has type SHT_NOBITS since it will be
4328 // allocated and filled in by the dynamic linker.
4329 PPC64LongBranchTargetSection::PPC64LongBranchTargetSection(Ctx
&ctx
)
4330 : SyntheticSection(ctx
, SHF_ALLOC
| SHF_WRITE
,
4331 ctx
.arg
.isPic
? SHT_NOBITS
: SHT_PROGBITS
, 8,
4334 uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol
*sym
,
4336 return getVA() + entry_index
.find({sym
, addend
})->second
* 8;
4339 std::optional
<uint32_t>
4340 PPC64LongBranchTargetSection::addEntry(const Symbol
*sym
, int64_t addend
) {
4342 entry_index
.try_emplace(std::make_pair(sym
, addend
), entries
.size());
4344 return std::nullopt
;
4345 entries
.emplace_back(sym
, addend
);
4346 return res
.first
->second
;
4349 size_t PPC64LongBranchTargetSection::getSize() const {
4350 return entries
.size() * 8;
4353 void PPC64LongBranchTargetSection::writeTo(uint8_t *buf
) {
4354 // If linking non-pic we have the final addresses of the targets and they get
4355 // written to the table directly. For pic the dynamic linker will allocate
4356 // the section and fill it.
4360 for (auto entry
: entries
) {
4361 const Symbol
*sym
= entry
.first
;
4362 int64_t addend
= entry
.second
;
4363 assert(sym
->getVA(ctx
));
4364 // Need calls to branch to the local entry-point since a long-branch
4365 // must be a local-call.
4367 sym
->getVA(ctx
, addend
) +
4368 getPPC64GlobalEntryToLocalEntryOffset(ctx
, sym
->stOther
));
4373 bool PPC64LongBranchTargetSection::isNeeded() const {
4374 // `removeUnusedSyntheticSections()` is called before thunk allocation which
4375 // is too early to determine if this section will be empty or not. We need
4376 // Finalized to keep the section alive until after thunk creation. Finalized
4377 // only gets set to true once `finalizeSections()` is called after thunk
4378 // creation. Because of this, if we don't create any long-branch thunks we end
4379 // up with an empty .branch_lt section in the binary.
4380 return !finalized
|| !entries
.empty();
4383 static uint8_t getAbiVersion(Ctx
&ctx
) {
4384 // MIPS non-PIC executable gets ABI version 1.
4385 if (ctx
.arg
.emachine
== EM_MIPS
) {
4386 if (!ctx
.arg
.isPic
&& !ctx
.arg
.relocatable
&&
4387 (ctx
.arg
.eflags
& (EF_MIPS_PIC
| EF_MIPS_CPIC
)) == EF_MIPS_CPIC
)
4392 if (ctx
.arg
.emachine
== EM_AMDGPU
&& !ctx
.objectFiles
.empty()) {
4393 uint8_t ver
= ctx
.objectFiles
[0]->abiVersion
;
4394 for (InputFile
*file
: ArrayRef(ctx
.objectFiles
).slice(1))
4395 if (file
->abiVersion
!= ver
)
4396 ErrAlways(ctx
) << "incompatible ABI version: " << file
;
4403 template <typename ELFT
>
4404 void elf::writeEhdr(Ctx
&ctx
, uint8_t *buf
, Partition
&part
) {
4405 memcpy(buf
, "\177ELF", 4);
4407 auto *eHdr
= reinterpret_cast<typename
ELFT::Ehdr
*>(buf
);
4408 eHdr
->e_ident
[EI_CLASS
] = ELFT::Is64Bits
? ELFCLASS64
: ELFCLASS32
;
4409 eHdr
->e_ident
[EI_DATA
] =
4410 ELFT::Endianness
== endianness::little
? ELFDATA2LSB
: ELFDATA2MSB
;
4411 eHdr
->e_ident
[EI_VERSION
] = EV_CURRENT
;
4412 eHdr
->e_ident
[EI_OSABI
] = ctx
.arg
.osabi
;
4413 eHdr
->e_ident
[EI_ABIVERSION
] = getAbiVersion(ctx
);
4414 eHdr
->e_machine
= ctx
.arg
.emachine
;
4415 eHdr
->e_version
= EV_CURRENT
;
4416 eHdr
->e_flags
= ctx
.arg
.eflags
;
4417 eHdr
->e_ehsize
= sizeof(typename
ELFT::Ehdr
);
4418 eHdr
->e_phnum
= part
.phdrs
.size();
4419 eHdr
->e_shentsize
= sizeof(typename
ELFT::Shdr
);
4421 if (!ctx
.arg
.relocatable
) {
4422 eHdr
->e_phoff
= sizeof(typename
ELFT::Ehdr
);
4423 eHdr
->e_phentsize
= sizeof(typename
ELFT::Phdr
);
4427 template <typename ELFT
> void elf::writePhdrs(uint8_t *buf
, Partition
&part
) {
4428 // Write the program header table.
4429 auto *hBuf
= reinterpret_cast<typename
ELFT::Phdr
*>(buf
);
4430 for (std::unique_ptr
<PhdrEntry
> &p
: part
.phdrs
) {
4431 hBuf
->p_type
= p
->p_type
;
4432 hBuf
->p_flags
= p
->p_flags
;
4433 hBuf
->p_offset
= p
->p_offset
;
4434 hBuf
->p_vaddr
= p
->p_vaddr
;
4435 hBuf
->p_paddr
= p
->p_paddr
;
4436 hBuf
->p_filesz
= p
->p_filesz
;
4437 hBuf
->p_memsz
= p
->p_memsz
;
4438 hBuf
->p_align
= p
->p_align
;
4443 template <typename ELFT
>
4444 PartitionElfHeaderSection
<ELFT
>::PartitionElfHeaderSection(Ctx
&ctx
)
4445 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_LLVM_PART_EHDR
, 1, "") {}
4447 template <typename ELFT
>
4448 size_t PartitionElfHeaderSection
<ELFT
>::getSize() const {
4449 return sizeof(typename
ELFT::Ehdr
);
4452 template <typename ELFT
>
4453 void PartitionElfHeaderSection
<ELFT
>::writeTo(uint8_t *buf
) {
4454 writeEhdr
<ELFT
>(ctx
, buf
, getPartition(ctx
));
4456 // Loadable partitions are always ET_DYN.
4457 auto *eHdr
= reinterpret_cast<typename
ELFT::Ehdr
*>(buf
);
4458 eHdr
->e_type
= ET_DYN
;
4461 template <typename ELFT
>
4462 PartitionProgramHeadersSection
<ELFT
>::PartitionProgramHeadersSection(Ctx
&ctx
)
4463 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_LLVM_PART_PHDR
, 1, ".phdrs") {}
4465 template <typename ELFT
>
4466 size_t PartitionProgramHeadersSection
<ELFT
>::getSize() const {
4467 return sizeof(typename
ELFT::Phdr
) * getPartition(ctx
).phdrs
.size();
4470 template <typename ELFT
>
4471 void PartitionProgramHeadersSection
<ELFT
>::writeTo(uint8_t *buf
) {
4472 writePhdrs
<ELFT
>(buf
, getPartition(ctx
));
4475 PartitionIndexSection::PartitionIndexSection(Ctx
&ctx
)
4476 : SyntheticSection(ctx
, SHF_ALLOC
, SHT_PROGBITS
, 4, ".rodata") {}
4478 size_t PartitionIndexSection::getSize() const {
4479 return 12 * (ctx
.partitions
.size() - 1);
4482 void PartitionIndexSection::finalizeContents() {
4483 for (size_t i
= 1; i
!= ctx
.partitions
.size(); ++i
)
4484 ctx
.partitions
[i
].nameStrTab
=
4485 ctx
.mainPart
->dynStrTab
->addString(ctx
.partitions
[i
].name
);
4488 void PartitionIndexSection::writeTo(uint8_t *buf
) {
4489 uint64_t va
= getVA();
4490 for (size_t i
= 1; i
!= ctx
.partitions
.size(); ++i
) {
4492 ctx
.mainPart
->dynStrTab
->getVA() + ctx
.partitions
[i
].nameStrTab
-
4494 write32(ctx
, buf
+ 4, ctx
.partitions
[i
].elfHeader
->getVA() - (va
+ 4));
4496 SyntheticSection
*next
= i
== ctx
.partitions
.size() - 1
4497 ? ctx
.in
.partEnd
.get()
4498 : ctx
.partitions
[i
+ 1].elfHeader
.get();
4499 write32(ctx
, buf
+ 8, next
->getVA() - ctx
.partitions
[i
].elfHeader
->getVA());
4506 static bool needsInterpSection(Ctx
&ctx
) {
4507 return !ctx
.arg
.relocatable
&& !ctx
.arg
.shared
&&
4508 !ctx
.arg
.dynamicLinker
.empty() && ctx
.script
->needsInterpSection();
4511 bool elf::hasMemtag(Ctx
&ctx
) {
4512 return ctx
.arg
.emachine
== EM_AARCH64
&&
4513 ctx
.arg
.androidMemtagMode
!= ELF::NT_MEMTAG_LEVEL_NONE
;
4516 // Fully static executables don't support MTE globals at this point in time, as
4517 // we currently rely on:
4518 // - A dynamic loader to process relocations, and
4519 // - Dynamic entries.
4520 // This restriction could be removed in future by re-using some of the ideas
4521 // that ifuncs use in fully static executables.
4522 bool elf::canHaveMemtagGlobals(Ctx
&ctx
) {
4523 return hasMemtag(ctx
) &&
4524 (ctx
.arg
.relocatable
|| ctx
.arg
.shared
|| needsInterpSection(ctx
));
4527 constexpr char kMemtagAndroidNoteName
[] = "Android";
4528 void MemtagAndroidNote::writeTo(uint8_t *buf
) {
4530 sizeof(kMemtagAndroidNoteName
) == 8,
4531 "Android 11 & 12 have an ABI that the note name is 8 bytes long. Keep it "
4532 "that way for backwards compatibility.");
4534 write32(ctx
, buf
, sizeof(kMemtagAndroidNoteName
));
4535 write32(ctx
, buf
+ 4, sizeof(uint32_t));
4536 write32(ctx
, buf
+ 8, ELF::NT_ANDROID_TYPE_MEMTAG
);
4537 memcpy(buf
+ 12, kMemtagAndroidNoteName
, sizeof(kMemtagAndroidNoteName
));
4538 buf
+= 12 + alignTo(sizeof(kMemtagAndroidNoteName
), 4);
4541 value
|= ctx
.arg
.androidMemtagMode
;
4542 if (ctx
.arg
.androidMemtagHeap
)
4543 value
|= ELF::NT_MEMTAG_HEAP
;
4544 // Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled
4545 // binary on Android 11 or 12 will result in a checkfail in the loader.
4546 if (ctx
.arg
.androidMemtagStack
)
4547 value
|= ELF::NT_MEMTAG_STACK
;
4548 write32(ctx
, buf
, value
); // note value
4551 size_t MemtagAndroidNote::getSize() const {
4552 return sizeof(llvm::ELF::Elf64_Nhdr
) +
4553 /*namesz=*/alignTo(sizeof(kMemtagAndroidNoteName
), 4) +
4554 /*descsz=*/sizeof(uint32_t);
4557 void PackageMetadataNote::writeTo(uint8_t *buf
) {
4558 write32(ctx
, buf
, 4);
4559 write32(ctx
, buf
+ 4, ctx
.arg
.packageMetadata
.size() + 1);
4560 write32(ctx
, buf
+ 8, FDO_PACKAGING_METADATA
);
4561 memcpy(buf
+ 12, "FDO", 4);
4562 memcpy(buf
+ 16, ctx
.arg
.packageMetadata
.data(),
4563 ctx
.arg
.packageMetadata
.size());
4566 size_t PackageMetadataNote::getSize() const {
4567 return sizeof(llvm::ELF::Elf64_Nhdr
) + 4 +
4568 alignTo(ctx
.arg
.packageMetadata
.size() + 1, 4);
4571 // Helper function, return the size of the ULEB128 for 'v', optionally writing
4572 // it to `*(buf + offset)` if `buf` is non-null.
4573 static size_t computeOrWriteULEB128(uint64_t v
, uint8_t *buf
, size_t offset
) {
4575 return encodeULEB128(v
, buf
+ offset
);
4576 return getULEB128Size(v
);
4579 // https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic
4580 constexpr uint64_t kMemtagStepSizeBits
= 3;
4581 constexpr uint64_t kMemtagGranuleSize
= 16;
4583 createMemtagGlobalDescriptors(Ctx
&ctx
,
4584 const SmallVector
<const Symbol
*, 0> &symbols
,
4585 uint8_t *buf
= nullptr) {
4586 size_t sectionSize
= 0;
4587 uint64_t lastGlobalEnd
= 0;
4589 for (const Symbol
*sym
: symbols
) {
4590 if (!includeInSymtab(ctx
, *sym
))
4592 const uint64_t addr
= sym
->getVA(ctx
);
4593 const uint64_t size
= sym
->getSize();
4595 if (addr
<= kMemtagGranuleSize
&& buf
!= nullptr)
4596 Err(ctx
) << "address of the tagged symbol \"" << sym
->getName()
4597 << "\" falls in the ELF header. This is indicative of a "
4598 "compiler/linker bug";
4599 if (addr
% kMemtagGranuleSize
!= 0)
4600 Err(ctx
) << "address of the tagged symbol \"" << sym
->getName()
4601 << "\" at 0x" << Twine::utohexstr(addr
)
4602 << "\" is not granule (16-byte) aligned";
4604 Err(ctx
) << "size of the tagged symbol \"" << sym
->getName()
4605 << "\" is not allowed to be zero";
4606 if (size
% kMemtagGranuleSize
!= 0)
4607 Err(ctx
) << "size of the tagged symbol \"" << sym
->getName()
4608 << "\" (size 0x" << Twine::utohexstr(size
)
4609 << ") is not granule (16-byte) aligned";
4611 const uint64_t sizeToEncode
= size
/ kMemtagGranuleSize
;
4612 const uint64_t stepToEncode
= ((addr
- lastGlobalEnd
) / kMemtagGranuleSize
)
4613 << kMemtagStepSizeBits
;
4614 if (sizeToEncode
< (1 << kMemtagStepSizeBits
)) {
4615 sectionSize
+= computeOrWriteULEB128(stepToEncode
| sizeToEncode
, buf
, sectionSize
);
4617 sectionSize
+= computeOrWriteULEB128(stepToEncode
, buf
, sectionSize
);
4618 sectionSize
+= computeOrWriteULEB128(sizeToEncode
- 1, buf
, sectionSize
);
4620 lastGlobalEnd
= addr
+ size
;
4626 bool MemtagGlobalDescriptors::updateAllocSize(Ctx
&ctx
) {
4627 size_t oldSize
= getSize();
4628 std::stable_sort(symbols
.begin(), symbols
.end(),
4629 [&ctx
= ctx
](const Symbol
*s1
, const Symbol
*s2
) {
4630 return s1
->getVA(ctx
) < s2
->getVA(ctx
);
4632 return oldSize
!= getSize();
4635 void MemtagGlobalDescriptors::writeTo(uint8_t *buf
) {
4636 createMemtagGlobalDescriptors(ctx
, symbols
, buf
);
4639 size_t MemtagGlobalDescriptors::getSize() const {
4640 return createMemtagGlobalDescriptors(ctx
, symbols
);
4643 static OutputSection
*findSection(Ctx
&ctx
, StringRef name
) {
4644 for (SectionCommand
*cmd
: ctx
.script
->sectionCommands
)
4645 if (auto *osd
= dyn_cast
<OutputDesc
>(cmd
))
4646 if (osd
->osec
.name
== name
)
4651 static Defined
*addOptionalRegular(Ctx
&ctx
, StringRef name
, SectionBase
*sec
,
4652 uint64_t val
, uint8_t stOther
= STV_HIDDEN
) {
4653 Symbol
*s
= ctx
.symtab
->find(name
);
4654 if (!s
|| s
->isDefined() || s
->isCommon())
4657 s
->resolve(ctx
, Defined
{ctx
, ctx
.internalFile
, StringRef(), STB_GLOBAL
,
4658 stOther
, STT_NOTYPE
, val
,
4660 s
->isUsedInRegularObj
= true;
4661 return cast
<Defined
>(s
);
4664 template <class ELFT
> void elf::createSyntheticSections(Ctx
&ctx
) {
4665 // Add the .interp section first because it is not a SyntheticSection.
4666 // The removeUnusedSyntheticSections() function relies on the
4667 // SyntheticSections coming last.
4668 if (needsInterpSection(ctx
)) {
4669 for (size_t i
= 1; i
<= ctx
.partitions
.size(); ++i
) {
4670 InputSection
*sec
= createInterpSection(ctx
);
4672 ctx
.inputSections
.push_back(sec
);
4676 auto add
= [&](SyntheticSection
&sec
) { ctx
.inputSections
.push_back(&sec
); };
4678 if (ctx
.arg
.zSectionHeader
)
4680 std::make_unique
<StringTableSection
>(ctx
, ".shstrtab", false);
4682 ctx
.out
.programHeaders
=
4683 std::make_unique
<OutputSection
>(ctx
, "", 0, SHF_ALLOC
);
4684 ctx
.out
.programHeaders
->addralign
= ctx
.arg
.wordsize
;
4686 if (ctx
.arg
.strip
!= StripPolicy::All
) {
4687 ctx
.in
.strTab
= std::make_unique
<StringTableSection
>(ctx
, ".strtab", false);
4689 std::make_unique
<SymbolTableSection
<ELFT
>>(ctx
, *ctx
.in
.strTab
);
4690 ctx
.in
.symTabShndx
= std::make_unique
<SymtabShndxSection
>(ctx
);
4693 ctx
.in
.bss
= std::make_unique
<BssSection
>(ctx
, ".bss", 0, 1);
4696 // If there is a SECTIONS command and a .data.rel.ro section name use name
4697 // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
4698 // This makes sure our relro is contiguous.
4700 ctx
.script
->hasSectionsCommand
&& findSection(ctx
, ".data.rel.ro");
4701 ctx
.in
.bssRelRo
= std::make_unique
<BssSection
>(
4702 ctx
, hasDataRelRo
? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
4703 add(*ctx
.in
.bssRelRo
);
4705 // Add MIPS-specific sections.
4706 if (ctx
.arg
.emachine
== EM_MIPS
) {
4707 if (!ctx
.arg
.shared
&& ctx
.arg
.hasDynSymTab
) {
4708 ctx
.in
.mipsRldMap
= std::make_unique
<MipsRldMapSection
>(ctx
);
4709 add(*ctx
.in
.mipsRldMap
);
4711 if ((ctx
.in
.mipsAbiFlags
= MipsAbiFlagsSection
<ELFT
>::create(ctx
)))
4712 add(*ctx
.in
.mipsAbiFlags
);
4713 if ((ctx
.in
.mipsOptions
= MipsOptionsSection
<ELFT
>::create(ctx
)))
4714 add(*ctx
.in
.mipsOptions
);
4715 if ((ctx
.in
.mipsReginfo
= MipsReginfoSection
<ELFT
>::create(ctx
)))
4716 add(*ctx
.in
.mipsReginfo
);
4719 StringRef relaDynName
= ctx
.arg
.isRela
? ".rela.dyn" : ".rel.dyn";
4721 const unsigned threadCount
= ctx
.arg
.threadCount
;
4722 for (Partition
&part
: ctx
.partitions
) {
4723 auto add
= [&](SyntheticSection
&sec
) {
4724 sec
.partition
= part
.getNumber(ctx
);
4725 ctx
.inputSections
.push_back(&sec
);
4728 if (!part
.name
.empty()) {
4729 part
.elfHeader
= std::make_unique
<PartitionElfHeaderSection
<ELFT
>>(ctx
);
4730 part
.elfHeader
->name
= part
.name
;
4731 add(*part
.elfHeader
);
4733 part
.programHeaders
=
4734 std::make_unique
<PartitionProgramHeadersSection
<ELFT
>>(ctx
);
4735 add(*part
.programHeaders
);
4738 if (ctx
.arg
.buildId
!= BuildIdKind::None
) {
4739 part
.buildId
= std::make_unique
<BuildIdSection
>(ctx
);
4743 // dynSymTab is always present to simplify sym->includeInDynsym(ctx) in
4744 // finalizeSections.
4745 part
.dynStrTab
= std::make_unique
<StringTableSection
>(ctx
, ".dynstr", true);
4747 std::make_unique
<SymbolTableSection
<ELFT
>>(ctx
, *part
.dynStrTab
);
4749 if (ctx
.arg
.relocatable
)
4751 part
.dynamic
= std::make_unique
<DynamicSection
<ELFT
>>(ctx
);
4753 if (hasMemtag(ctx
)) {
4754 part
.memtagAndroidNote
= std::make_unique
<MemtagAndroidNote
>(ctx
);
4755 add(*part
.memtagAndroidNote
);
4756 if (canHaveMemtagGlobals(ctx
)) {
4757 part
.memtagGlobalDescriptors
=
4758 std::make_unique
<MemtagGlobalDescriptors
>(ctx
);
4759 add(*part
.memtagGlobalDescriptors
);
4763 if (ctx
.arg
.androidPackDynRelocs
)
4764 part
.relaDyn
= std::make_unique
<AndroidPackedRelocationSection
<ELFT
>>(
4765 ctx
, relaDynName
, threadCount
);
4767 part
.relaDyn
= std::make_unique
<RelocationSection
<ELFT
>>(
4768 ctx
, relaDynName
, ctx
.arg
.zCombreloc
, threadCount
);
4770 if (ctx
.arg
.hasDynSymTab
) {
4771 add(*part
.dynSymTab
);
4773 part
.verSym
= std::make_unique
<VersionTableSection
>(ctx
);
4776 if (!namedVersionDefs(ctx
).empty()) {
4777 part
.verDef
= std::make_unique
<VersionDefinitionSection
>(ctx
);
4781 part
.verNeed
= std::make_unique
<VersionNeedSection
<ELFT
>>(ctx
);
4784 if (ctx
.arg
.gnuHash
) {
4785 part
.gnuHashTab
= std::make_unique
<GnuHashTableSection
>(ctx
);
4786 add(*part
.gnuHashTab
);
4789 if (ctx
.arg
.sysvHash
) {
4790 part
.hashTab
= std::make_unique
<HashTableSection
>(ctx
);
4795 add(*part
.dynStrTab
);
4799 if (ctx
.arg
.relrPackDynRelocs
) {
4800 part
.relrDyn
= std::make_unique
<RelrSection
<ELFT
>>(ctx
, threadCount
);
4802 part
.relrAuthDyn
= std::make_unique
<RelrSection
<ELFT
>>(
4803 ctx
, threadCount
, /*isAArch64Auth=*/true);
4804 add(*part
.relrAuthDyn
);
4807 if (ctx
.arg
.ehFrameHdr
) {
4808 part
.ehFrameHdr
= std::make_unique
<EhFrameHeader
>(ctx
);
4809 add(*part
.ehFrameHdr
);
4811 part
.ehFrame
= std::make_unique
<EhFrameSection
>(ctx
);
4814 if (ctx
.arg
.emachine
== EM_ARM
) {
4815 // This section replaces all the individual .ARM.exidx InputSections.
4816 part
.armExidx
= std::make_unique
<ARMExidxSyntheticSection
>(ctx
);
4817 add(*part
.armExidx
);
4820 if (!ctx
.arg
.packageMetadata
.empty()) {
4821 part
.packageMetadataNote
= std::make_unique
<PackageMetadataNote
>(ctx
);
4822 add(*part
.packageMetadataNote
);
4826 if (ctx
.partitions
.size() != 1) {
4827 // Create the partition end marker. This needs to be in partition number 255
4828 // so that it is sorted after all other partitions. It also has other
4829 // special handling (see createPhdrs() and combineEhSections()).
4831 std::make_unique
<BssSection
>(ctx
, ".part.end", ctx
.arg
.maxPageSize
, 1);
4832 ctx
.in
.partEnd
->partition
= 255;
4833 add(*ctx
.in
.partEnd
);
4835 ctx
.in
.partIndex
= std::make_unique
<PartitionIndexSection
>(ctx
);
4836 addOptionalRegular(ctx
, "__part_index_begin", ctx
.in
.partIndex
.get(), 0);
4837 addOptionalRegular(ctx
, "__part_index_end", ctx
.in
.partIndex
.get(),
4838 ctx
.in
.partIndex
->getSize());
4839 add(*ctx
.in
.partIndex
);
4842 // Add .got. MIPS' .got is so different from the other archs,
4843 // it has its own class.
4844 if (ctx
.arg
.emachine
== EM_MIPS
) {
4845 ctx
.in
.mipsGot
= std::make_unique
<MipsGotSection
>(ctx
);
4846 add(*ctx
.in
.mipsGot
);
4848 ctx
.in
.got
= std::make_unique
<GotSection
>(ctx
);
4852 if (ctx
.arg
.emachine
== EM_PPC
) {
4853 ctx
.in
.ppc32Got2
= std::make_unique
<PPC32Got2Section
>(ctx
);
4854 add(*ctx
.in
.ppc32Got2
);
4857 if (ctx
.arg
.emachine
== EM_PPC64
) {
4858 ctx
.in
.ppc64LongBranchTarget
=
4859 std::make_unique
<PPC64LongBranchTargetSection
>(ctx
);
4860 add(*ctx
.in
.ppc64LongBranchTarget
);
4863 ctx
.in
.gotPlt
= std::make_unique
<GotPltSection
>(ctx
);
4864 add(*ctx
.in
.gotPlt
);
4865 ctx
.in
.igotPlt
= std::make_unique
<IgotPltSection
>(ctx
);
4866 add(*ctx
.in
.igotPlt
);
4867 // Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the
4868 // section in the absence of PHDRS/SECTIONS commands.
4869 if (ctx
.arg
.zRelro
&&
4870 ((ctx
.script
->phdrsCommands
.empty() && !ctx
.script
->hasSectionsCommand
) ||
4871 ctx
.script
->seenRelroEnd
)) {
4872 ctx
.in
.relroPadding
= std::make_unique
<RelroPaddingSection
>(ctx
);
4873 add(*ctx
.in
.relroPadding
);
4876 if (ctx
.arg
.emachine
== EM_ARM
) {
4877 ctx
.in
.armCmseSGSection
= std::make_unique
<ArmCmseSGSection
>(ctx
);
4878 add(*ctx
.in
.armCmseSGSection
);
4881 // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
4882 // it as a relocation and ensure the referenced section is created.
4883 if (ctx
.sym
.globalOffsetTable
&& ctx
.arg
.emachine
!= EM_MIPS
) {
4884 if (ctx
.target
->gotBaseSymInGotPlt
)
4885 ctx
.in
.gotPlt
->hasGotPltOffRel
= true;
4887 ctx
.in
.got
->hasGotOffRel
= true;
4890 // We always need to add rel[a].plt to output if it has entries.
4891 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
4892 ctx
.in
.relaPlt
= std::make_unique
<RelocationSection
<ELFT
>>(
4893 ctx
, ctx
.arg
.isRela
? ".rela.plt" : ".rel.plt", /*sort=*/false,
4895 add(*ctx
.in
.relaPlt
);
4897 if ((ctx
.arg
.emachine
== EM_386
|| ctx
.arg
.emachine
== EM_X86_64
) &&
4898 (ctx
.arg
.andFeatures
& GNU_PROPERTY_X86_FEATURE_1_IBT
)) {
4899 ctx
.in
.ibtPlt
= std::make_unique
<IBTPltSection
>(ctx
);
4900 add(*ctx
.in
.ibtPlt
);
4903 if (ctx
.arg
.emachine
== EM_PPC
)
4904 ctx
.in
.plt
= std::make_unique
<PPC32GlinkSection
>(ctx
);
4906 ctx
.in
.plt
= std::make_unique
<PltSection
>(ctx
);
4908 ctx
.in
.iplt
= std::make_unique
<IpltSection
>(ctx
);
4911 if (ctx
.arg
.andFeatures
|| !ctx
.aarch64PauthAbiCoreInfo
.empty()) {
4912 ctx
.in
.gnuProperty
= std::make_unique
<GnuPropertySection
>(ctx
);
4913 add(*ctx
.in
.gnuProperty
);
4916 if (ctx
.arg
.debugNames
) {
4917 ctx
.in
.debugNames
= std::make_unique
<DebugNamesSection
<ELFT
>>(ctx
);
4918 add(*ctx
.in
.debugNames
);
4921 if (ctx
.arg
.gdbIndex
) {
4922 ctx
.in
.gdbIndex
= GdbIndexSection::create
<ELFT
>(ctx
);
4923 add(*ctx
.in
.gdbIndex
);
4926 // .note.GNU-stack is always added when we are creating a re-linkable
4927 // object file. Other linkers are using the presence of this marker
4928 // section to control the executable-ness of the stack area, but that
4929 // is irrelevant these days. Stack area should always be non-executable
4930 // by default. So we emit this section unconditionally.
4931 if (ctx
.arg
.relocatable
) {
4932 ctx
.in
.gnuStack
= std::make_unique
<GnuStackSection
>(ctx
);
4933 add(*ctx
.in
.gnuStack
);
4937 add(*ctx
.in
.symTab
);
4938 if (ctx
.in
.symTabShndx
)
4939 add(*ctx
.in
.symTabShndx
);
4940 if (ctx
.in
.shStrTab
)
4941 add(*ctx
.in
.shStrTab
);
4943 add(*ctx
.in
.strTab
);
4946 template void elf::splitSections
<ELF32LE
>(Ctx
&);
4947 template void elf::splitSections
<ELF32BE
>(Ctx
&);
4948 template void elf::splitSections
<ELF64LE
>(Ctx
&);
4949 template void elf::splitSections
<ELF64BE
>(Ctx
&);
4951 template void EhFrameSection::iterateFDEWithLSDA
<ELF32LE
>(
4952 function_ref
<void(InputSection
&)>);
4953 template void EhFrameSection::iterateFDEWithLSDA
<ELF32BE
>(
4954 function_ref
<void(InputSection
&)>);
4955 template void EhFrameSection::iterateFDEWithLSDA
<ELF64LE
>(
4956 function_ref
<void(InputSection
&)>);
4957 template void EhFrameSection::iterateFDEWithLSDA
<ELF64BE
>(
4958 function_ref
<void(InputSection
&)>);
4960 template class elf::SymbolTableSection
<ELF32LE
>;
4961 template class elf::SymbolTableSection
<ELF32BE
>;
4962 template class elf::SymbolTableSection
<ELF64LE
>;
4963 template class elf::SymbolTableSection
<ELF64BE
>;
4965 template void elf::writeEhdr
<ELF32LE
>(Ctx
&, uint8_t *Buf
, Partition
&Part
);
4966 template void elf::writeEhdr
<ELF32BE
>(Ctx
&, uint8_t *Buf
, Partition
&Part
);
4967 template void elf::writeEhdr
<ELF64LE
>(Ctx
&, uint8_t *Buf
, Partition
&Part
);
4968 template void elf::writeEhdr
<ELF64BE
>(Ctx
&, uint8_t *Buf
, Partition
&Part
);
4970 template void elf::writePhdrs
<ELF32LE
>(uint8_t *Buf
, Partition
&Part
);
4971 template void elf::writePhdrs
<ELF32BE
>(uint8_t *Buf
, Partition
&Part
);
4972 template void elf::writePhdrs
<ELF64LE
>(uint8_t *Buf
, Partition
&Part
);
4973 template void elf::writePhdrs
<ELF64BE
>(uint8_t *Buf
, Partition
&Part
);
4975 template void elf::createSyntheticSections
<ELF32LE
>(Ctx
&);
4976 template void elf::createSyntheticSections
<ELF32BE
>(Ctx
&);
4977 template void elf::createSyntheticSections
<ELF64LE
>(Ctx
&);
4978 template void elf::createSyntheticSections
<ELF64BE
>(Ctx
&);