Path: blob/main/contrib/llvm-project/lld/ELF/SyntheticSections.cpp
34878 views
//===- SyntheticSections.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains linker-synthesized sections. Currently,9// synthetic sections are created either output sections or input sections,10// but we are rewriting code so that all synthetic sections are created as11// input sections.12//13//===----------------------------------------------------------------------===//1415#include "SyntheticSections.h"16#include "Config.h"17#include "DWARF.h"18#include "EhFrame.h"19#include "InputFiles.h"20#include "LinkerScript.h"21#include "OutputSections.h"22#include "SymbolTable.h"23#include "Symbols.h"24#include "Target.h"25#include "Thunks.h"26#include "Writer.h"27#include "lld/Common/CommonLinkerContext.h"28#include "lld/Common/DWARF.h"29#include "lld/Common/Strings.h"30#include "lld/Common/Version.h"31#include "llvm/ADT/STLExtras.h"32#include "llvm/ADT/Sequence.h"33#include "llvm/ADT/SetOperations.h"34#include "llvm/ADT/StringExtras.h"35#include "llvm/BinaryFormat/Dwarf.h"36#include "llvm/BinaryFormat/ELF.h"37#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"38#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"39#include "llvm/Support/DJB.h"40#include "llvm/Support/Endian.h"41#include "llvm/Support/LEB128.h"42#include "llvm/Support/Parallel.h"43#include "llvm/Support/TimeProfiler.h"44#include <cinttypes>45#include <cstdlib>4647using namespace llvm;48using namespace llvm::dwarf;49using namespace llvm::ELF;50using namespace llvm::object;51using namespace llvm::support;52using namespace lld;53using namespace lld::elf;5455using llvm::support::endian::read32le;56using llvm::support::endian::write32le;57using llvm::support::endian::write64le;5859constexpr size_t MergeNoTailSection::numShards;6061static uint64_t readUint(uint8_t *buf) {62return config->is64 ? read64(buf) : read32(buf);63}6465static void writeUint(uint8_t *buf, uint64_t val) {66if (config->is64)67write64(buf, val);68else69write32(buf, val);70}7172// Returns an LLD version string.73static ArrayRef<uint8_t> getVersion() {74// Check LLD_VERSION first for ease of testing.75// You can get consistent output by using the environment variable.76// This is only for testing.77StringRef s = getenv("LLD_VERSION");78if (s.empty())79s = saver().save(Twine("Linker: ") + getLLDVersion());8081// +1 to include the terminating '\0'.82return {(const uint8_t *)s.data(), s.size() + 1};83}8485// Creates a .comment section containing LLD version info.86// With this feature, you can identify LLD-generated binaries easily87// by "readelf --string-dump .comment <file>".88// The returned object is a mergeable string section.89MergeInputSection *elf::createCommentSection() {90auto *sec = make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,91getVersion(), ".comment");92sec->splitIntoPieces();93return sec;94}9596// .MIPS.abiflags section.97template <class ELFT>98MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags)99: SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),100flags(flags) {101this->entsize = sizeof(Elf_Mips_ABIFlags);102}103104template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {105memcpy(buf, &flags, sizeof(flags));106}107108template <class ELFT>109std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() {110Elf_Mips_ABIFlags flags = {};111bool create = false;112113for (InputSectionBase *sec : ctx.inputSections) {114if (sec->type != SHT_MIPS_ABIFLAGS)115continue;116sec->markDead();117create = true;118119std::string filename = toString(sec->file);120const size_t size = sec->content().size();121// Older version of BFD (such as the default FreeBSD linker) concatenate122// .MIPS.abiflags instead of merging. To allow for this case (or potential123// zero padding) we ignore everything after the first Elf_Mips_ABIFlags124if (size < sizeof(Elf_Mips_ABIFlags)) {125error(filename + ": invalid size of .MIPS.abiflags section: got " +126Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));127return nullptr;128}129auto *s =130reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->content().data());131if (s->version != 0) {132error(filename + ": unexpected .MIPS.abiflags version " +133Twine(s->version));134return nullptr;135}136137// LLD checks ISA compatibility in calcMipsEFlags(). Here we just138// select the highest number of ISA/Rev/Ext.139flags.isa_level = std::max(flags.isa_level, s->isa_level);140flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);141flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);142flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);143flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);144flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);145flags.ases |= s->ases;146flags.flags1 |= s->flags1;147flags.flags2 |= s->flags2;148flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);149};150151if (create)152return std::make_unique<MipsAbiFlagsSection<ELFT>>(flags);153return nullptr;154}155156// .MIPS.options section.157template <class ELFT>158MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo)159: SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),160reginfo(reginfo) {161this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);162}163164template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {165auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);166options->kind = ODK_REGINFO;167options->size = getSize();168169if (!config->relocatable)170reginfo.ri_gp_value = in.mipsGot->getGp();171memcpy(buf + sizeof(Elf_Mips_Options), ®info, sizeof(reginfo));172}173174template <class ELFT>175std::unique_ptr<MipsOptionsSection<ELFT>> MipsOptionsSection<ELFT>::create() {176// N64 ABI only.177if (!ELFT::Is64Bits)178return nullptr;179180SmallVector<InputSectionBase *, 0> sections;181for (InputSectionBase *sec : ctx.inputSections)182if (sec->type == SHT_MIPS_OPTIONS)183sections.push_back(sec);184185if (sections.empty())186return nullptr;187188Elf_Mips_RegInfo reginfo = {};189for (InputSectionBase *sec : sections) {190sec->markDead();191192std::string filename = toString(sec->file);193ArrayRef<uint8_t> d = sec->content();194195while (!d.empty()) {196if (d.size() < sizeof(Elf_Mips_Options)) {197error(filename + ": invalid size of .MIPS.options section");198break;199}200201auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());202if (opt->kind == ODK_REGINFO) {203reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;204sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;205break;206}207208if (!opt->size)209fatal(filename + ": zero option descriptor size");210d = d.slice(opt->size);211}212};213214return std::make_unique<MipsOptionsSection<ELFT>>(reginfo);215}216217// MIPS .reginfo section.218template <class ELFT>219MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo)220: SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),221reginfo(reginfo) {222this->entsize = sizeof(Elf_Mips_RegInfo);223}224225template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {226if (!config->relocatable)227reginfo.ri_gp_value = in.mipsGot->getGp();228memcpy(buf, ®info, sizeof(reginfo));229}230231template <class ELFT>232std::unique_ptr<MipsReginfoSection<ELFT>> MipsReginfoSection<ELFT>::create() {233// Section should be alive for O32 and N32 ABIs only.234if (ELFT::Is64Bits)235return nullptr;236237SmallVector<InputSectionBase *, 0> sections;238for (InputSectionBase *sec : ctx.inputSections)239if (sec->type == SHT_MIPS_REGINFO)240sections.push_back(sec);241242if (sections.empty())243return nullptr;244245Elf_Mips_RegInfo reginfo = {};246for (InputSectionBase *sec : sections) {247sec->markDead();248249if (sec->content().size() != sizeof(Elf_Mips_RegInfo)) {250error(toString(sec->file) + ": invalid size of .reginfo section");251return nullptr;252}253254auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->content().data());255reginfo.ri_gprmask |= r->ri_gprmask;256sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;257};258259return std::make_unique<MipsReginfoSection<ELFT>>(reginfo);260}261262InputSection *elf::createInterpSection() {263// StringSaver guarantees that the returned string ends with '\0'.264StringRef s = saver().save(config->dynamicLinker);265ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};266267return make<InputSection>(ctx.internalFile, SHF_ALLOC, SHT_PROGBITS, 1,268contents, ".interp");269}270271Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,272uint64_t size, InputSectionBase §ion) {273Defined *s = makeDefined(section.file, name, STB_LOCAL, STV_DEFAULT, type,274value, size, §ion);275if (in.symTab)276in.symTab->addSymbol(s);277278if (config->emachine == EM_ARM && !config->isLE && config->armBe8 &&279(section.flags & SHF_EXECINSTR))280// Adding Linker generated mapping symbols to the arm specific mapping281// symbols list.282addArmSyntheticSectionMappingSymbol(s);283284return s;285}286287static size_t getHashSize() {288switch (config->buildId) {289case BuildIdKind::Fast:290return 8;291case BuildIdKind::Md5:292case BuildIdKind::Uuid:293return 16;294case BuildIdKind::Sha1:295return 20;296case BuildIdKind::Hexstring:297return config->buildIdVector.size();298default:299llvm_unreachable("unknown BuildIdKind");300}301}302303// This class represents a linker-synthesized .note.gnu.property section.304//305// In x86 and AArch64, object files may contain feature flags indicating the306// features that they have used. The flags are stored in a .note.gnu.property307// section.308//309// lld reads the sections from input files and merges them by computing AND of310// the flags. The result is written as a new .note.gnu.property section.311//312// If the flag is zero (which indicates that the intersection of the feature313// sets is empty, or some input files didn't have .note.gnu.property sections),314// we don't create this section.315GnuPropertySection::GnuPropertySection()316: SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE,317config->wordsize, ".note.gnu.property") {}318319void GnuPropertySection::writeTo(uint8_t *buf) {320write32(buf, 4); // Name size321write32(buf + 4, getSize() - 16); // Content size322write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type323memcpy(buf + 12, "GNU", 4); // Name string324325uint32_t featureAndType = config->emachine == EM_AARCH64326? GNU_PROPERTY_AARCH64_FEATURE_1_AND327: GNU_PROPERTY_X86_FEATURE_1_AND;328329unsigned offset = 16;330if (config->andFeatures != 0) {331write32(buf + offset + 0, featureAndType); // Feature type332write32(buf + offset + 4, 4); // Feature size333write32(buf + offset + 8, config->andFeatures); // Feature flags334if (config->is64)335write32(buf + offset + 12, 0); // Padding336offset += 16;337}338339if (!ctx.aarch64PauthAbiCoreInfo.empty()) {340write32(buf + offset + 0, GNU_PROPERTY_AARCH64_FEATURE_PAUTH);341write32(buf + offset + 4, ctx.aarch64PauthAbiCoreInfo.size());342memcpy(buf + offset + 8, ctx.aarch64PauthAbiCoreInfo.data(),343ctx.aarch64PauthAbiCoreInfo.size());344}345}346347size_t GnuPropertySection::getSize() const {348uint32_t contentSize = 0;349if (config->andFeatures != 0)350contentSize += config->is64 ? 16 : 12;351if (!ctx.aarch64PauthAbiCoreInfo.empty())352contentSize += 4 + 4 + ctx.aarch64PauthAbiCoreInfo.size();353assert(contentSize != 0);354return contentSize + 16;355}356357BuildIdSection::BuildIdSection()358: SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),359hashSize(getHashSize()) {}360361void BuildIdSection::writeTo(uint8_t *buf) {362write32(buf, 4); // Name size363write32(buf + 4, hashSize); // Content size364write32(buf + 8, NT_GNU_BUILD_ID); // Type365memcpy(buf + 12, "GNU", 4); // Name string366hashBuf = buf + 16;367}368369void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {370assert(buf.size() == hashSize);371memcpy(hashBuf, buf.data(), hashSize);372}373374BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment)375: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) {376this->bss = true;377this->size = size;378}379380EhFrameSection::EhFrameSection()381: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}382383// Search for an existing CIE record or create a new one.384// CIE records from input object files are uniquified by their contents385// and where their relocations point to.386template <class ELFT, class RelTy>387CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {388Symbol *personality = nullptr;389unsigned firstRelI = cie.firstRelocation;390if (firstRelI != (unsigned)-1)391personality = &cie.sec->file->getRelocTargetSym(rels[firstRelI]);392393// Search for an existing CIE by CIE contents/relocation target pair.394CieRecord *&rec = cieMap[{cie.data(), personality}];395396// If not found, create a new one.397if (!rec) {398rec = make<CieRecord>();399rec->cie = &cie;400cieRecords.push_back(rec);401}402return rec;403}404405// There is one FDE per function. Returns a non-null pointer to the function406// symbol if the given FDE points to a live function.407template <class ELFT, class RelTy>408Defined *EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {409auto *sec = cast<EhInputSection>(fde.sec);410unsigned firstRelI = fde.firstRelocation;411412// An FDE should point to some function because FDEs are to describe413// functions. That's however not always the case due to an issue of414// ld.gold with -r. ld.gold may discard only functions and leave their415// corresponding FDEs, which results in creating bad .eh_frame sections.416// To deal with that, we ignore such FDEs.417if (firstRelI == (unsigned)-1)418return nullptr;419420const RelTy &rel = rels[firstRelI];421Symbol &b = sec->file->getRelocTargetSym(rel);422423// FDEs for garbage-collected or merged-by-ICF sections, or sections in424// another partition, are dead.425if (auto *d = dyn_cast<Defined>(&b))426if (!d->folded && d->section && d->section->partition == partition)427return d;428return nullptr;429}430431// .eh_frame is a sequence of CIE or FDE records. In general, there432// is one CIE record per input object file which is followed by433// a list of FDEs. This function searches an existing CIE or create a new434// one and associates FDEs to the CIE.435template <class ELFT, class RelTy>436void EhFrameSection::addRecords(EhInputSection *sec, ArrayRef<RelTy> rels) {437offsetToCie.clear();438for (EhSectionPiece &cie : sec->cies)439offsetToCie[cie.inputOff] = addCie<ELFT>(cie, rels);440for (EhSectionPiece &fde : sec->fdes) {441uint32_t id = endian::read32<ELFT::Endianness>(fde.data().data() + 4);442CieRecord *rec = offsetToCie[fde.inputOff + 4 - id];443if (!rec)444fatal(toString(sec) + ": invalid CIE reference");445446if (!isFdeLive<ELFT>(fde, rels))447continue;448rec->fdes.push_back(&fde);449numFdes++;450}451}452453template <class ELFT>454void EhFrameSection::addSectionAux(EhInputSection *sec) {455if (!sec->isLive())456return;457const RelsOrRelas<ELFT> rels =458sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);459if (rels.areRelocsRel())460addRecords<ELFT>(sec, rels.rels);461else462addRecords<ELFT>(sec, rels.relas);463}464465// Used by ICF<ELFT>::handleLSDA(). This function is very similar to466// EhFrameSection::addRecords().467template <class ELFT, class RelTy>468void EhFrameSection::iterateFDEWithLSDAAux(469EhInputSection &sec, ArrayRef<RelTy> rels, DenseSet<size_t> &ciesWithLSDA,470llvm::function_ref<void(InputSection &)> fn) {471for (EhSectionPiece &cie : sec.cies)472if (hasLSDA(cie))473ciesWithLSDA.insert(cie.inputOff);474for (EhSectionPiece &fde : sec.fdes) {475uint32_t id = endian::read32<ELFT::Endianness>(fde.data().data() + 4);476if (!ciesWithLSDA.contains(fde.inputOff + 4 - id))477continue;478479// The CIE has a LSDA argument. Call fn with d's section.480if (Defined *d = isFdeLive<ELFT>(fde, rels))481if (auto *s = dyn_cast_or_null<InputSection>(d->section))482fn(*s);483}484}485486template <class ELFT>487void EhFrameSection::iterateFDEWithLSDA(488llvm::function_ref<void(InputSection &)> fn) {489DenseSet<size_t> ciesWithLSDA;490for (EhInputSection *sec : sections) {491ciesWithLSDA.clear();492const RelsOrRelas<ELFT> rels =493sec->template relsOrRelas<ELFT>(/*supportsCrel=*/false);494if (rels.areRelocsRel())495iterateFDEWithLSDAAux<ELFT>(*sec, rels.rels, ciesWithLSDA, fn);496else497iterateFDEWithLSDAAux<ELFT>(*sec, rels.relas, ciesWithLSDA, fn);498}499}500501static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) {502memcpy(buf, d.data(), d.size());503// Fix the size field. -4 since size does not include the size field itself.504write32(buf, d.size() - 4);505}506507void EhFrameSection::finalizeContents() {508assert(!this->size); // Not finalized.509510switch (config->ekind) {511case ELFNoneKind:512llvm_unreachable("invalid ekind");513case ELF32LEKind:514for (EhInputSection *sec : sections)515addSectionAux<ELF32LE>(sec);516break;517case ELF32BEKind:518for (EhInputSection *sec : sections)519addSectionAux<ELF32BE>(sec);520break;521case ELF64LEKind:522for (EhInputSection *sec : sections)523addSectionAux<ELF64LE>(sec);524break;525case ELF64BEKind:526for (EhInputSection *sec : sections)527addSectionAux<ELF64BE>(sec);528break;529}530531size_t off = 0;532for (CieRecord *rec : cieRecords) {533rec->cie->outputOff = off;534off += rec->cie->size;535536for (EhSectionPiece *fde : rec->fdes) {537fde->outputOff = off;538off += fde->size;539}540}541542// The LSB standard does not allow a .eh_frame section with zero543// Call Frame Information records. glibc unwind-dw2-fde.c544// classify_object_over_fdes expects there is a CIE record length 0 as a545// terminator. Thus we add one unconditionally.546off += 4;547548this->size = off;549}550551// Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table552// to get an FDE from an address to which FDE is applied. This function553// returns a list of such pairs.554SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const {555uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;556SmallVector<FdeData, 0> ret;557558uint64_t va = getPartition().ehFrameHdr->getVA();559for (CieRecord *rec : cieRecords) {560uint8_t enc = getFdeEncoding(rec->cie);561for (EhSectionPiece *fde : rec->fdes) {562uint64_t pc = getFdePc(buf, fde->outputOff, enc);563uint64_t fdeVA = getParent()->addr + fde->outputOff;564if (!isInt<32>(pc - va)) {565errorOrWarn(toString(fde->sec) + ": PC offset is too large: 0x" +566Twine::utohexstr(pc - va));567continue;568}569ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});570}571}572573// Sort the FDE list by their PC and uniqueify. Usually there is only574// one FDE for a PC (i.e. function), but if ICF merges two functions575// into one, there can be more than one FDEs pointing to the address.576auto less = [](const FdeData &a, const FdeData &b) {577return a.pcRel < b.pcRel;578};579llvm::stable_sort(ret, less);580auto eq = [](const FdeData &a, const FdeData &b) {581return a.pcRel == b.pcRel;582};583ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end());584585return ret;586}587588static uint64_t readFdeAddr(uint8_t *buf, int size) {589switch (size) {590case DW_EH_PE_udata2:591return read16(buf);592case DW_EH_PE_sdata2:593return (int16_t)read16(buf);594case DW_EH_PE_udata4:595return read32(buf);596case DW_EH_PE_sdata4:597return (int32_t)read32(buf);598case DW_EH_PE_udata8:599case DW_EH_PE_sdata8:600return read64(buf);601case DW_EH_PE_absptr:602return readUint(buf);603}604fatal("unknown FDE size encoding");605}606607// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.608// We need it to create .eh_frame_hdr section.609uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff,610uint8_t enc) const {611// The starting address to which this FDE applies is612// stored at FDE + 8 byte. And this offset is within613// the .eh_frame section.614size_t off = fdeOff + 8;615uint64_t addr = readFdeAddr(buf + off, enc & 0xf);616if ((enc & 0x70) == DW_EH_PE_absptr)617return config->is64 ? addr : uint32_t(addr);618if ((enc & 0x70) == DW_EH_PE_pcrel)619return addr + getParent()->addr + off + outSecOff;620fatal("unknown FDE size relative encoding");621}622623void EhFrameSection::writeTo(uint8_t *buf) {624// Write CIE and FDE records.625for (CieRecord *rec : cieRecords) {626size_t cieOffset = rec->cie->outputOff;627writeCieFde(buf + cieOffset, rec->cie->data());628629for (EhSectionPiece *fde : rec->fdes) {630size_t off = fde->outputOff;631writeCieFde(buf + off, fde->data());632633// FDE's second word should have the offset to an associated CIE.634// Write it.635write32(buf + off + 4, off + 4 - cieOffset);636}637}638639// Apply relocations. .eh_frame section contents are not contiguous640// in the output buffer, but relocateAlloc() still works because641// getOffset() takes care of discontiguous section pieces.642for (EhInputSection *s : sections)643target->relocateAlloc(*s, buf);644645if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent())646getPartition().ehFrameHdr->write();647}648649GotSection::GotSection()650: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,651target->gotEntrySize, ".got") {652numEntries = target->gotHeaderEntriesNum;653}654655void GotSection::addConstant(const Relocation &r) { relocations.push_back(r); }656void GotSection::addEntry(const Symbol &sym) {657assert(sym.auxIdx == symAux.size() - 1);658symAux.back().gotIdx = numEntries++;659}660661bool GotSection::addTlsDescEntry(const Symbol &sym) {662assert(sym.auxIdx == symAux.size() - 1);663symAux.back().tlsDescIdx = numEntries;664numEntries += 2;665return true;666}667668bool GotSection::addDynTlsEntry(const Symbol &sym) {669assert(sym.auxIdx == symAux.size() - 1);670symAux.back().tlsGdIdx = numEntries;671// Global Dynamic TLS entries take two GOT slots.672numEntries += 2;673return true;674}675676// Reserves TLS entries for a TLS module ID and a TLS block offset.677// In total it takes two GOT slots.678bool GotSection::addTlsIndex() {679if (tlsIndexOff != uint32_t(-1))680return false;681tlsIndexOff = numEntries * config->wordsize;682numEntries += 2;683return true;684}685686uint32_t GotSection::getTlsDescOffset(const Symbol &sym) const {687return sym.getTlsDescIdx() * config->wordsize;688}689690uint64_t GotSection::getTlsDescAddr(const Symbol &sym) const {691return getVA() + getTlsDescOffset(sym);692}693694uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {695return this->getVA() + b.getTlsGdIdx() * config->wordsize;696}697698uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {699return b.getTlsGdIdx() * config->wordsize;700}701702void GotSection::finalizeContents() {703if (config->emachine == EM_PPC64 &&704numEntries <= target->gotHeaderEntriesNum && !ElfSym::globalOffsetTable)705size = 0;706else707size = numEntries * config->wordsize;708}709710bool GotSection::isNeeded() const {711// Needed if the GOT symbol is used or the number of entries is more than just712// the header. A GOT with just the header may not be needed.713return hasGotOffRel || numEntries > target->gotHeaderEntriesNum;714}715716void GotSection::writeTo(uint8_t *buf) {717// On PPC64 .got may be needed but empty. Skip the write.718if (size == 0)719return;720target->writeGotHeader(buf);721target->relocateAlloc(*this, buf);722}723724static uint64_t getMipsPageAddr(uint64_t addr) {725return (addr + 0x8000) & ~0xffff;726}727728static uint64_t getMipsPageCount(uint64_t size) {729return (size + 0xfffe) / 0xffff + 1;730}731732MipsGotSection::MipsGotSection()733: SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,734".got") {}735736void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,737RelExpr expr) {738FileGot &g = getGot(file);739if (expr == R_MIPS_GOT_LOCAL_PAGE) {740if (const OutputSection *os = sym.getOutputSection())741g.pagesMap.insert({os, {}});742else743g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0});744} else if (sym.isTls())745g.tls.insert({&sym, 0});746else if (sym.isPreemptible && expr == R_ABS)747g.relocs.insert({&sym, 0});748else if (sym.isPreemptible)749g.global.insert({&sym, 0});750else if (expr == R_MIPS_GOT_OFF32)751g.local32.insert({{&sym, addend}, 0});752else753g.local16.insert({{&sym, addend}, 0});754}755756void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) {757getGot(file).dynTlsSymbols.insert({&sym, 0});758}759760void MipsGotSection::addTlsIndex(InputFile &file) {761getGot(file).dynTlsSymbols.insert({nullptr, 0});762}763764size_t MipsGotSection::FileGot::getEntriesNum() const {765return getPageEntriesNum() + local16.size() + global.size() + relocs.size() +766tls.size() + dynTlsSymbols.size() * 2;767}768769size_t MipsGotSection::FileGot::getPageEntriesNum() const {770size_t num = 0;771for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap)772num += p.second.count;773return num;774}775776size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {777size_t count = getPageEntriesNum() + local16.size() + global.size();778// If there are relocation-only entries in the GOT, TLS entries779// are allocated after them. TLS entries should be addressable780// by 16-bit index so count both reloc-only and TLS entries.781if (!tls.empty() || !dynTlsSymbols.empty())782count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2;783return count;784}785786MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) {787if (f.mipsGotIndex == uint32_t(-1)) {788gots.emplace_back();789gots.back().file = &f;790f.mipsGotIndex = gots.size() - 1;791}792return gots[f.mipsGotIndex];793}794795uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f,796const Symbol &sym,797int64_t addend) const {798const FileGot &g = gots[f->mipsGotIndex];799uint64_t index = 0;800if (const OutputSection *outSec = sym.getOutputSection()) {801uint64_t secAddr = getMipsPageAddr(outSec->addr);802uint64_t symAddr = getMipsPageAddr(sym.getVA(addend));803index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff;804} else {805index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))});806}807return index * config->wordsize;808}809810uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s,811int64_t addend) const {812const FileGot &g = gots[f->mipsGotIndex];813Symbol *sym = const_cast<Symbol *>(&s);814if (sym->isTls())815return g.tls.lookup(sym) * config->wordsize;816if (sym->isPreemptible)817return g.global.lookup(sym) * config->wordsize;818return g.local16.lookup({sym, addend}) * config->wordsize;819}820821uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const {822const FileGot &g = gots[f->mipsGotIndex];823return g.dynTlsSymbols.lookup(nullptr) * config->wordsize;824}825826uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f,827const Symbol &s) const {828const FileGot &g = gots[f->mipsGotIndex];829Symbol *sym = const_cast<Symbol *>(&s);830return g.dynTlsSymbols.lookup(sym) * config->wordsize;831}832833const Symbol *MipsGotSection::getFirstGlobalEntry() const {834if (gots.empty())835return nullptr;836const FileGot &primGot = gots.front();837if (!primGot.global.empty())838return primGot.global.front().first;839if (!primGot.relocs.empty())840return primGot.relocs.front().first;841return nullptr;842}843844unsigned MipsGotSection::getLocalEntriesNum() const {845if (gots.empty())846return headerEntriesNum;847return headerEntriesNum + gots.front().getPageEntriesNum() +848gots.front().local16.size();849}850851bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {852FileGot tmp = dst;853set_union(tmp.pagesMap, src.pagesMap);854set_union(tmp.local16, src.local16);855set_union(tmp.global, src.global);856set_union(tmp.relocs, src.relocs);857set_union(tmp.tls, src.tls);858set_union(tmp.dynTlsSymbols, src.dynTlsSymbols);859860size_t count = isPrimary ? headerEntriesNum : 0;861count += tmp.getIndexedEntriesNum();862863if (count * config->wordsize > config->mipsGotSize)864return false;865866std::swap(tmp, dst);867return true;868}869870void MipsGotSection::finalizeContents() { updateAllocSize(); }871872bool MipsGotSection::updateAllocSize() {873size = headerEntriesNum * config->wordsize;874for (const FileGot &g : gots)875size += g.getEntriesNum() * config->wordsize;876return false;877}878879void MipsGotSection::build() {880if (gots.empty())881return;882883std::vector<FileGot> mergedGots(1);884885// For each GOT move non-preemptible symbols from the `Global`886// to `Local16` list. Preemptible symbol might become non-preemptible887// one if, for example, it gets a related copy relocation.888for (FileGot &got : gots) {889for (auto &p: got.global)890if (!p.first->isPreemptible)891got.local16.insert({{p.first, 0}, 0});892got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) {893return !p.first->isPreemptible;894});895}896897// For each GOT remove "reloc-only" entry if there is "global"898// entry for the same symbol. And add local entries which indexed899// using 32-bit value at the end of 16-bit entries.900for (FileGot &got : gots) {901got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {902return got.global.count(p.first);903});904set_union(got.local16, got.local32);905got.local32.clear();906}907908// Evaluate number of "reloc-only" entries in the resulting GOT.909// To do that put all unique "reloc-only" and "global" entries910// from all GOTs to the future primary GOT.911FileGot *primGot = &mergedGots.front();912for (FileGot &got : gots) {913set_union(primGot->relocs, got.global);914set_union(primGot->relocs, got.relocs);915got.relocs.clear();916}917918// Evaluate number of "page" entries in each GOT.919for (FileGot &got : gots) {920for (std::pair<const OutputSection *, FileGot::PageBlock> &p :921got.pagesMap) {922const OutputSection *os = p.first;923uint64_t secSize = 0;924for (SectionCommand *cmd : os->commands) {925if (auto *isd = dyn_cast<InputSectionDescription>(cmd))926for (InputSection *isec : isd->sections) {927uint64_t off = alignToPowerOf2(secSize, isec->addralign);928secSize = off + isec->getSize();929}930}931p.second.count = getMipsPageCount(secSize);932}933}934935// Merge GOTs. Try to join as much as possible GOTs but do not exceed936// maximum GOT size. At first, try to fill the primary GOT because937// the primary GOT can be accessed in the most effective way. If it938// is not possible, try to fill the last GOT in the list, and finally939// create a new GOT if both attempts failed.940for (FileGot &srcGot : gots) {941InputFile *file = srcGot.file;942if (tryMergeGots(mergedGots.front(), srcGot, true)) {943file->mipsGotIndex = 0;944} else {945// If this is the first time we failed to merge with the primary GOT,946// MergedGots.back() will also be the primary GOT. We must make sure not947// to try to merge again with isPrimary=false, as otherwise, if the948// inputs are just right, we could allow the primary GOT to become 1 or 2949// words bigger due to ignoring the header size.950if (mergedGots.size() == 1 ||951!tryMergeGots(mergedGots.back(), srcGot, false)) {952mergedGots.emplace_back();953std::swap(mergedGots.back(), srcGot);954}955file->mipsGotIndex = mergedGots.size() - 1;956}957}958std::swap(gots, mergedGots);959960// Reduce number of "reloc-only" entries in the primary GOT961// by subtracting "global" entries in the primary GOT.962primGot = &gots.front();963primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {964return primGot->global.count(p.first);965});966967// Calculate indexes for each GOT entry.968size_t index = headerEntriesNum;969for (FileGot &got : gots) {970got.startIndex = &got == primGot ? 0 : index;971for (std::pair<const OutputSection *, FileGot::PageBlock> &p :972got.pagesMap) {973// For each output section referenced by GOT page relocations calculate974// and save into pagesMap an upper bound of MIPS GOT entries required975// to store page addresses of local symbols. We assume the worst case -976// each 64kb page of the output section has at least one GOT relocation977// against it. And take in account the case when the section intersects978// page boundaries.979p.second.firstIndex = index;980index += p.second.count;981}982for (auto &p: got.local16)983p.second = index++;984for (auto &p: got.global)985p.second = index++;986for (auto &p: got.relocs)987p.second = index++;988for (auto &p: got.tls)989p.second = index++;990for (auto &p: got.dynTlsSymbols) {991p.second = index;992index += 2;993}994}995996// Update SymbolAux::gotIdx field to use this997// value later in the `sortMipsSymbols` function.998for (auto &p : primGot->global) {999if (p.first->auxIdx == 0)1000p.first->allocateAux();1001symAux.back().gotIdx = p.second;1002}1003for (auto &p : primGot->relocs) {1004if (p.first->auxIdx == 0)1005p.first->allocateAux();1006symAux.back().gotIdx = p.second;1007}10081009// Create dynamic relocations.1010for (FileGot &got : gots) {1011// Create dynamic relocations for TLS entries.1012for (std::pair<Symbol *, size_t> &p : got.tls) {1013Symbol *s = p.first;1014uint64_t offset = p.second * config->wordsize;1015// When building a shared library we still need a dynamic relocation1016// for the TP-relative offset as we don't know how much other data will1017// be allocated before us in the static TLS block.1018if (s->isPreemptible || config->shared)1019mainPart->relaDyn->addReloc({target->tlsGotRel, this, offset,1020DynamicReloc::AgainstSymbolWithTargetVA,1021*s, 0, R_ABS});1022}1023for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {1024Symbol *s = p.first;1025uint64_t offset = p.second * config->wordsize;1026if (s == nullptr) {1027if (!config->shared)1028continue;1029mainPart->relaDyn->addReloc({target->tlsModuleIndexRel, this, offset});1030} else {1031// When building a shared library we still need a dynamic relocation1032// for the module index. Therefore only checking for1033// S->isPreemptible is not sufficient (this happens e.g. for1034// thread-locals that have been marked as local through a linker script)1035if (!s->isPreemptible && !config->shared)1036continue;1037mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *this,1038offset, *s);1039// However, we can skip writing the TLS offset reloc for non-preemptible1040// symbols since it is known even in shared libraries1041if (!s->isPreemptible)1042continue;1043offset += config->wordsize;1044mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *this, offset,1045*s);1046}1047}10481049// Do not create dynamic relocations for non-TLS1050// entries in the primary GOT.1051if (&got == primGot)1052continue;10531054// Dynamic relocations for "global" entries.1055for (const std::pair<Symbol *, size_t> &p : got.global) {1056uint64_t offset = p.second * config->wordsize;1057mainPart->relaDyn->addSymbolReloc(target->relativeRel, *this, offset,1058*p.first);1059}1060if (!config->isPic)1061continue;1062// Dynamic relocations for "local" entries in case of PIC.1063for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :1064got.pagesMap) {1065size_t pageCount = l.second.count;1066for (size_t pi = 0; pi < pageCount; ++pi) {1067uint64_t offset = (l.second.firstIndex + pi) * config->wordsize;1068mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first,1069int64_t(pi * 0x10000)});1070}1071}1072for (const std::pair<GotEntry, size_t> &p : got.local16) {1073uint64_t offset = p.second * config->wordsize;1074mainPart->relaDyn->addReloc({target->relativeRel, this, offset,1075DynamicReloc::AddendOnlyWithTargetVA,1076*p.first.first, p.first.second, R_ABS});1077}1078}1079}10801081bool MipsGotSection::isNeeded() const {1082// We add the .got section to the result for dynamic MIPS target because1083// its address and properties are mentioned in the .dynamic section.1084return !config->relocatable;1085}10861087uint64_t MipsGotSection::getGp(const InputFile *f) const {1088// For files without related GOT or files refer a primary GOT1089// returns "common" _gp value. For secondary GOTs calculate1090// individual _gp values.1091if (!f || f->mipsGotIndex == uint32_t(-1) || f->mipsGotIndex == 0)1092return ElfSym::mipsGp->getVA(0);1093return getVA() + gots[f->mipsGotIndex].startIndex * config->wordsize + 0x7ff0;1094}10951096void MipsGotSection::writeTo(uint8_t *buf) {1097// Set the MSB of the second GOT slot. This is not required by any1098// MIPS ABI documentation, though.1099//1100// There is a comment in glibc saying that "The MSB of got[1] of a1101// gnu object is set to identify gnu objects," and in GNU gold it1102// says "the second entry will be used by some runtime loaders".1103// But how this field is being used is unclear.1104//1105// We are not really willing to mimic other linkers behaviors1106// without understanding why they do that, but because all files1107// generated by GNU tools have this special GOT value, and because1108// we've been doing this for years, it is probably a safe bet to1109// keep doing this for now. We really need to revisit this to see1110// if we had to do this.1111writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1));1112for (const FileGot &g : gots) {1113auto write = [&](size_t i, const Symbol *s, int64_t a) {1114uint64_t va = a;1115if (s)1116va = s->getVA(a);1117writeUint(buf + i * config->wordsize, va);1118};1119// Write 'page address' entries to the local part of the GOT.1120for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :1121g.pagesMap) {1122size_t pageCount = l.second.count;1123uint64_t firstPageAddr = getMipsPageAddr(l.first->addr);1124for (size_t pi = 0; pi < pageCount; ++pi)1125write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000);1126}1127// Local, global, TLS, reloc-only entries.1128// If TLS entry has a corresponding dynamic relocations, leave it1129// initialized by zero. Write down adjusted TLS symbol's values otherwise.1130// To calculate the adjustments use offsets for thread-local storage.1131// http://web.archive.org/web/20190324223224/https://www.linux-mips.org/wiki/NPTL1132for (const std::pair<GotEntry, size_t> &p : g.local16)1133write(p.second, p.first.first, p.first.second);1134// Write VA to the primary GOT only. For secondary GOTs that1135// will be done by REL32 dynamic relocations.1136if (&g == &gots.front())1137for (const std::pair<Symbol *, size_t> &p : g.global)1138write(p.second, p.first, 0);1139for (const std::pair<Symbol *, size_t> &p : g.relocs)1140write(p.second, p.first, 0);1141for (const std::pair<Symbol *, size_t> &p : g.tls)1142write(p.second, p.first,1143p.first->isPreemptible || config->shared ? 0 : -0x7000);1144for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) {1145if (p.first == nullptr && !config->shared)1146write(p.second, nullptr, 1);1147else if (p.first && !p.first->isPreemptible) {1148// If we are emitting a shared library with relocations we mustn't write1149// anything to the GOT here. When using Elf_Rel relocations the value1150// one will be treated as an addend and will cause crashes at runtime1151if (!config->shared)1152write(p.second, nullptr, 1);1153write(p.second + 1, p.first, -0x8000);1154}1155}1156}1157}11581159// On PowerPC the .plt section is used to hold the table of function addresses1160// instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss1161// section. I don't know why we have a BSS style type for the section but it is1162// consistent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.1163GotPltSection::GotPltSection()1164: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,1165".got.plt") {1166if (config->emachine == EM_PPC) {1167name = ".plt";1168} else if (config->emachine == EM_PPC64) {1169type = SHT_NOBITS;1170name = ".plt";1171}1172}11731174void GotPltSection::addEntry(Symbol &sym) {1175assert(sym.auxIdx == symAux.size() - 1 &&1176symAux.back().pltIdx == entries.size());1177entries.push_back(&sym);1178}11791180size_t GotPltSection::getSize() const {1181return (target->gotPltHeaderEntriesNum + entries.size()) *1182target->gotEntrySize;1183}11841185void GotPltSection::writeTo(uint8_t *buf) {1186target->writeGotPltHeader(buf);1187buf += target->gotPltHeaderEntriesNum * target->gotEntrySize;1188for (const Symbol *b : entries) {1189target->writeGotPlt(buf, *b);1190buf += target->gotEntrySize;1191}1192}11931194bool GotPltSection::isNeeded() const {1195// We need to emit GOTPLT even if it's empty if there's a relocation relative1196// to it.1197return !entries.empty() || hasGotPltOffRel;1198}11991200static StringRef getIgotPltName() {1201// On ARM the IgotPltSection is part of the GotSection.1202if (config->emachine == EM_ARM)1203return ".got";12041205// On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection1206// needs to be named the same.1207if (config->emachine == EM_PPC64)1208return ".plt";12091210return ".got.plt";1211}12121213// On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit1214// with the IgotPltSection.1215IgotPltSection::IgotPltSection()1216: SyntheticSection(SHF_ALLOC | SHF_WRITE,1217config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,1218target->gotEntrySize, getIgotPltName()) {}12191220void IgotPltSection::addEntry(Symbol &sym) {1221assert(symAux.back().pltIdx == entries.size());1222entries.push_back(&sym);1223}12241225size_t IgotPltSection::getSize() const {1226return entries.size() * target->gotEntrySize;1227}12281229void IgotPltSection::writeTo(uint8_t *buf) {1230for (const Symbol *b : entries) {1231target->writeIgotPlt(buf, *b);1232buf += target->gotEntrySize;1233}1234}12351236StringTableSection::StringTableSection(StringRef name, bool dynamic)1237: SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name),1238dynamic(dynamic) {1239// ELF string tables start with a NUL byte.1240strings.push_back("");1241stringMap.try_emplace(CachedHashStringRef(""), 0);1242size = 1;1243}12441245// Adds a string to the string table. If `hashIt` is true we hash and check for1246// duplicates. It is optional because the name of global symbols are already1247// uniqued and hashing them again has a big cost for a small value: uniquing1248// them with some other string that happens to be the same.1249unsigned StringTableSection::addString(StringRef s, bool hashIt) {1250if (hashIt) {1251auto r = stringMap.try_emplace(CachedHashStringRef(s), size);1252if (!r.second)1253return r.first->second;1254}1255if (s.empty())1256return 0;1257unsigned ret = this->size;1258this->size = this->size + s.size() + 1;1259strings.push_back(s);1260return ret;1261}12621263void StringTableSection::writeTo(uint8_t *buf) {1264for (StringRef s : strings) {1265memcpy(buf, s.data(), s.size());1266buf[s.size()] = '\0';1267buf += s.size() + 1;1268}1269}12701271// Returns the number of entries in .gnu.version_d: the number of1272// non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1.1273// Note that we don't support vd_cnt > 1 yet.1274static unsigned getVerDefNum() {1275return namedVersionDefs().size() + 1;1276}12771278template <class ELFT>1279DynamicSection<ELFT>::DynamicSection()1280: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize,1281".dynamic") {1282this->entsize = ELFT::Is64Bits ? 16 : 8;12831284// .dynamic section is not writable on MIPS and on Fuchsia OS1285// which passes -z rodynamic.1286// See "Special Section" in Chapter 4 in the following document:1287// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf1288if (config->emachine == EM_MIPS || config->zRodynamic)1289this->flags = SHF_ALLOC;1290}12911292// The output section .rela.dyn may include these synthetic sections:1293//1294// - part.relaDyn1295// - in.relaPlt: this is included if a linker script places .rela.plt inside1296// .rela.dyn1297//1298// DT_RELASZ is the total size of the included sections.1299static uint64_t addRelaSz(const RelocationBaseSection &relaDyn) {1300size_t size = relaDyn.getSize();1301if (in.relaPlt->getParent() == relaDyn.getParent())1302size += in.relaPlt->getSize();1303return size;1304}13051306// A Linker script may assign the RELA relocation sections to the same1307// output section. When this occurs we cannot just use the OutputSection1308// Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to1309// overlap with the [DT_RELA, DT_RELA + DT_RELASZ).1310static uint64_t addPltRelSz() { return in.relaPlt->getSize(); }13111312// Add remaining entries to complete .dynamic contents.1313template <class ELFT>1314std::vector<std::pair<int32_t, uint64_t>>1315DynamicSection<ELFT>::computeContents() {1316elf::Partition &part = getPartition();1317bool isMain = part.name.empty();1318std::vector<std::pair<int32_t, uint64_t>> entries;13191320auto addInt = [&](int32_t tag, uint64_t val) {1321entries.emplace_back(tag, val);1322};1323auto addInSec = [&](int32_t tag, const InputSection &sec) {1324entries.emplace_back(tag, sec.getVA());1325};13261327for (StringRef s : config->filterList)1328addInt(DT_FILTER, part.dynStrTab->addString(s));1329for (StringRef s : config->auxiliaryList)1330addInt(DT_AUXILIARY, part.dynStrTab->addString(s));13311332if (!config->rpath.empty())1333addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH,1334part.dynStrTab->addString(config->rpath));13351336for (SharedFile *file : ctx.sharedFiles)1337if (file->isNeeded)1338addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));13391340if (isMain) {1341if (!config->soName.empty())1342addInt(DT_SONAME, part.dynStrTab->addString(config->soName));1343} else {1344if (!config->soName.empty())1345addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));1346addInt(DT_SONAME, part.dynStrTab->addString(part.name));1347}13481349// Set DT_FLAGS and DT_FLAGS_1.1350uint32_t dtFlags = 0;1351uint32_t dtFlags1 = 0;1352if (config->bsymbolic == BsymbolicKind::All)1353dtFlags |= DF_SYMBOLIC;1354if (config->zGlobal)1355dtFlags1 |= DF_1_GLOBAL;1356if (config->zInitfirst)1357dtFlags1 |= DF_1_INITFIRST;1358if (config->zInterpose)1359dtFlags1 |= DF_1_INTERPOSE;1360if (config->zNodefaultlib)1361dtFlags1 |= DF_1_NODEFLIB;1362if (config->zNodelete)1363dtFlags1 |= DF_1_NODELETE;1364if (config->zNodlopen)1365dtFlags1 |= DF_1_NOOPEN;1366if (config->pie)1367dtFlags1 |= DF_1_PIE;1368if (config->zNow) {1369dtFlags |= DF_BIND_NOW;1370dtFlags1 |= DF_1_NOW;1371}1372if (config->zOrigin) {1373dtFlags |= DF_ORIGIN;1374dtFlags1 |= DF_1_ORIGIN;1375}1376if (!config->zText)1377dtFlags |= DF_TEXTREL;1378if (ctx.hasTlsIe && config->shared)1379dtFlags |= DF_STATIC_TLS;13801381if (dtFlags)1382addInt(DT_FLAGS, dtFlags);1383if (dtFlags1)1384addInt(DT_FLAGS_1, dtFlags1);13851386// DT_DEBUG is a pointer to debug information used by debuggers at runtime. We1387// need it for each process, so we don't write it for DSOs. The loader writes1388// the pointer into this entry.1389//1390// DT_DEBUG is the only .dynamic entry that needs to be written to. Some1391// systems (currently only Fuchsia OS) provide other means to give the1392// debugger this information. Such systems may choose make .dynamic read-only.1393// If the target is such a system (used -z rodynamic) don't write DT_DEBUG.1394if (!config->shared && !config->relocatable && !config->zRodynamic)1395addInt(DT_DEBUG, 0);13961397if (part.relaDyn->isNeeded()) {1398addInSec(part.relaDyn->dynamicTag, *part.relaDyn);1399entries.emplace_back(part.relaDyn->sizeDynamicTag,1400addRelaSz(*part.relaDyn));14011402bool isRela = config->isRela;1403addInt(isRela ? DT_RELAENT : DT_RELENT,1404isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));14051406// MIPS dynamic loader does not support RELCOUNT tag.1407// The problem is in the tight relation between dynamic1408// relocations and GOT. So do not emit this tag on MIPS.1409if (config->emachine != EM_MIPS) {1410size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();1411if (config->zCombreloc && numRelativeRels)1412addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels);1413}1414}1415if (part.relrDyn && part.relrDyn->getParent() &&1416!part.relrDyn->relocs.empty()) {1417addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,1418*part.relrDyn);1419addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,1420part.relrDyn->getParent()->size);1421addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,1422sizeof(Elf_Relr));1423}1424if (part.relrAuthDyn && part.relrAuthDyn->getParent() &&1425!part.relrAuthDyn->relocs.empty()) {1426addInSec(DT_AARCH64_AUTH_RELR, *part.relrAuthDyn);1427addInt(DT_AARCH64_AUTH_RELRSZ, part.relrAuthDyn->getParent()->size);1428addInt(DT_AARCH64_AUTH_RELRENT, sizeof(Elf_Relr));1429}1430if (isMain && in.relaPlt->isNeeded()) {1431addInSec(DT_JMPREL, *in.relaPlt);1432entries.emplace_back(DT_PLTRELSZ, addPltRelSz());1433switch (config->emachine) {1434case EM_MIPS:1435addInSec(DT_MIPS_PLTGOT, *in.gotPlt);1436break;1437case EM_S390:1438addInSec(DT_PLTGOT, *in.got);1439break;1440case EM_SPARCV9:1441addInSec(DT_PLTGOT, *in.plt);1442break;1443case EM_AARCH64:1444if (llvm::find_if(in.relaPlt->relocs, [](const DynamicReloc &r) {1445return r.type == target->pltRel &&1446r.sym->stOther & STO_AARCH64_VARIANT_PCS;1447}) != in.relaPlt->relocs.end())1448addInt(DT_AARCH64_VARIANT_PCS, 0);1449addInSec(DT_PLTGOT, *in.gotPlt);1450break;1451case EM_RISCV:1452if (llvm::any_of(in.relaPlt->relocs, [](const DynamicReloc &r) {1453return r.type == target->pltRel &&1454(r.sym->stOther & STO_RISCV_VARIANT_CC);1455}))1456addInt(DT_RISCV_VARIANT_CC, 0);1457[[fallthrough]];1458default:1459addInSec(DT_PLTGOT, *in.gotPlt);1460break;1461}1462addInt(DT_PLTREL, config->isRela ? DT_RELA : DT_REL);1463}14641465if (config->emachine == EM_AARCH64) {1466if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)1467addInt(DT_AARCH64_BTI_PLT, 0);1468if (config->zPacPlt)1469addInt(DT_AARCH64_PAC_PLT, 0);14701471if (hasMemtag()) {1472addInt(DT_AARCH64_MEMTAG_MODE, config->androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);1473addInt(DT_AARCH64_MEMTAG_HEAP, config->androidMemtagHeap);1474addInt(DT_AARCH64_MEMTAG_STACK, config->androidMemtagStack);1475if (mainPart->memtagGlobalDescriptors->isNeeded()) {1476addInSec(DT_AARCH64_MEMTAG_GLOBALS, *mainPart->memtagGlobalDescriptors);1477addInt(DT_AARCH64_MEMTAG_GLOBALSSZ,1478mainPart->memtagGlobalDescriptors->getSize());1479}1480}1481}14821483addInSec(DT_SYMTAB, *part.dynSymTab);1484addInt(DT_SYMENT, sizeof(Elf_Sym));1485addInSec(DT_STRTAB, *part.dynStrTab);1486addInt(DT_STRSZ, part.dynStrTab->getSize());1487if (!config->zText)1488addInt(DT_TEXTREL, 0);1489if (part.gnuHashTab && part.gnuHashTab->getParent())1490addInSec(DT_GNU_HASH, *part.gnuHashTab);1491if (part.hashTab && part.hashTab->getParent())1492addInSec(DT_HASH, *part.hashTab);14931494if (isMain) {1495if (Out::preinitArray) {1496addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr);1497addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size);1498}1499if (Out::initArray) {1500addInt(DT_INIT_ARRAY, Out::initArray->addr);1501addInt(DT_INIT_ARRAYSZ, Out::initArray->size);1502}1503if (Out::finiArray) {1504addInt(DT_FINI_ARRAY, Out::finiArray->addr);1505addInt(DT_FINI_ARRAYSZ, Out::finiArray->size);1506}15071508if (Symbol *b = symtab.find(config->init))1509if (b->isDefined())1510addInt(DT_INIT, b->getVA());1511if (Symbol *b = symtab.find(config->fini))1512if (b->isDefined())1513addInt(DT_FINI, b->getVA());1514}15151516if (part.verSym && part.verSym->isNeeded())1517addInSec(DT_VERSYM, *part.verSym);1518if (part.verDef && part.verDef->isLive()) {1519addInSec(DT_VERDEF, *part.verDef);1520addInt(DT_VERDEFNUM, getVerDefNum());1521}1522if (part.verNeed && part.verNeed->isNeeded()) {1523addInSec(DT_VERNEED, *part.verNeed);1524unsigned needNum = 0;1525for (SharedFile *f : ctx.sharedFiles)1526if (!f->vernauxs.empty())1527++needNum;1528addInt(DT_VERNEEDNUM, needNum);1529}15301531if (config->emachine == EM_MIPS) {1532addInt(DT_MIPS_RLD_VERSION, 1);1533addInt(DT_MIPS_FLAGS, RHF_NOTPOT);1534addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());1535addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());1536addInt(DT_MIPS_LOCAL_GOTNO, in.mipsGot->getLocalEntriesNum());15371538if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())1539addInt(DT_MIPS_GOTSYM, b->dynsymIndex);1540else1541addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());1542addInSec(DT_PLTGOT, *in.mipsGot);1543if (in.mipsRldMap) {1544if (!config->pie)1545addInSec(DT_MIPS_RLD_MAP, *in.mipsRldMap);1546// Store the offset to the .rld_map section1547// relative to the address of the tag.1548addInt(DT_MIPS_RLD_MAP_REL,1549in.mipsRldMap->getVA() - (getVA() + entries.size() * entsize));1550}1551}15521553// DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,1554// glibc assumes the old-style BSS PLT layout which we don't support.1555if (config->emachine == EM_PPC)1556addInSec(DT_PPC_GOT, *in.got);15571558// Glink dynamic tag is required by the V2 abi if the plt section isn't empty.1559if (config->emachine == EM_PPC64 && in.plt->isNeeded()) {1560// The Glink tag points to 32 bytes before the first lazy symbol resolution1561// stub, which starts directly after the header.1562addInt(DT_PPC64_GLINK, in.plt->getVA() + target->pltHeaderSize - 32);1563}15641565if (config->emachine == EM_PPC64)1566addInt(DT_PPC64_OPT, getPPC64TargetInfo()->ppc64DynamicSectionOpt);15671568addInt(DT_NULL, 0);1569return entries;1570}15711572template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {1573if (OutputSection *sec = getPartition().dynStrTab->getParent())1574getParent()->link = sec->sectionIndex;1575this->size = computeContents().size() * this->entsize;1576}15771578template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {1579auto *p = reinterpret_cast<Elf_Dyn *>(buf);15801581for (std::pair<int32_t, uint64_t> kv : computeContents()) {1582p->d_tag = kv.first;1583p->d_un.d_val = kv.second;1584++p;1585}1586}15871588uint64_t DynamicReloc::getOffset() const {1589return inputSec->getVA(offsetInSec);1590}15911592int64_t DynamicReloc::computeAddend() const {1593switch (kind) {1594case AddendOnly:1595assert(sym == nullptr);1596return addend;1597case AgainstSymbol:1598assert(sym != nullptr);1599return addend;1600case AddendOnlyWithTargetVA:1601case AgainstSymbolWithTargetVA: {1602uint64_t ca = InputSection::getRelocTargetVA(inputSec->file, type, addend,1603getOffset(), *sym, expr);1604return config->is64 ? ca : SignExtend64<32>(ca);1605}1606case MipsMultiGotPage:1607assert(sym == nullptr);1608return getMipsPageAddr(outputSec->addr) + addend;1609}1610llvm_unreachable("Unknown DynamicReloc::Kind enum");1611}16121613uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {1614if (!needsDynSymIndex())1615return 0;16161617size_t index = symTab->getSymbolIndex(*sym);1618assert((index != 0 || (type != target->gotRel && type != target->pltRel) ||1619!mainPart->dynSymTab->getParent()) &&1620"GOT or PLT relocation must refer to symbol in dynamic symbol table");1621return index;1622}16231624RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type,1625int32_t dynamicTag,1626int32_t sizeDynamicTag,1627bool combreloc,1628unsigned concurrency)1629: SyntheticSection(SHF_ALLOC, type, config->wordsize, name),1630dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag),1631relocsVec(concurrency), combreloc(combreloc) {}16321633void RelocationBaseSection::addSymbolReloc(1634RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym,1635int64_t addend, std::optional<RelType> addendRelType) {1636addReloc(DynamicReloc::AgainstSymbol, dynType, isec, offsetInSec, sym, addend,1637R_ADDEND, addendRelType ? *addendRelType : target->noneRel);1638}16391640void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible(1641RelType dynType, GotSection &sec, uint64_t offsetInSec, Symbol &sym,1642RelType addendRelType) {1643// No need to write an addend to the section for preemptible symbols.1644if (sym.isPreemptible)1645addReloc({dynType, &sec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0,1646R_ABS});1647else1648addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, sec, offsetInSec,1649sym, 0, R_ABS, addendRelType);1650}16511652void RelocationBaseSection::mergeRels() {1653size_t newSize = relocs.size();1654for (const auto &v : relocsVec)1655newSize += v.size();1656relocs.reserve(newSize);1657for (const auto &v : relocsVec)1658llvm::append_range(relocs, v);1659relocsVec.clear();1660}16611662void RelocationBaseSection::partitionRels() {1663if (!combreloc)1664return;1665const RelType relativeRel = target->relativeRel;1666numRelativeRelocs =1667std::stable_partition(relocs.begin(), relocs.end(),1668[=](auto &r) { return r.type == relativeRel; }) -1669relocs.begin();1670}16711672void RelocationBaseSection::finalizeContents() {1673SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();16741675// When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE1676// relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that1677// case.1678if (symTab && symTab->getParent())1679getParent()->link = symTab->getParent()->sectionIndex;1680else1681getParent()->link = 0;16821683if (in.relaPlt.get() == this && in.gotPlt->getParent()) {1684getParent()->flags |= ELF::SHF_INFO_LINK;1685getParent()->info = in.gotPlt->getParent()->sectionIndex;1686}1687}16881689void DynamicReloc::computeRaw(SymbolTableBaseSection *symtab) {1690r_offset = getOffset();1691r_sym = getSymIndex(symtab);1692addend = computeAddend();1693kind = AddendOnly; // Catch errors1694}16951696void RelocationBaseSection::computeRels() {1697SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();1698parallelForEach(relocs,1699[symTab](DynamicReloc &rel) { rel.computeRaw(symTab); });17001701auto irelative = std::stable_partition(1702relocs.begin() + numRelativeRelocs, relocs.end(),1703[t = target->iRelativeRel](auto &r) { return r.type != t; });17041705// Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to1706// place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset1707// is to make results easier to read.1708if (combreloc) {1709auto nonRelative = relocs.begin() + numRelativeRelocs;1710parallelSort(relocs.begin(), nonRelative,1711[&](auto &a, auto &b) { return a.r_offset < b.r_offset; });1712// Non-relative relocations are few, so don't bother with parallelSort.1713llvm::sort(nonRelative, irelative, [&](auto &a, auto &b) {1714return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset);1715});1716}1717}17181719template <class ELFT>1720RelocationSection<ELFT>::RelocationSection(StringRef name, bool combreloc,1721unsigned concurrency)1722: RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL,1723config->isRela ? DT_RELA : DT_REL,1724config->isRela ? DT_RELASZ : DT_RELSZ, combreloc,1725concurrency) {1726this->entsize = config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);1727}17281729template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {1730computeRels();1731for (const DynamicReloc &rel : relocs) {1732auto *p = reinterpret_cast<Elf_Rela *>(buf);1733p->r_offset = rel.r_offset;1734p->setSymbolAndType(rel.r_sym, rel.type, config->isMips64EL);1735if (config->isRela)1736p->r_addend = rel.addend;1737buf += config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);1738}1739}17401741RelrBaseSection::RelrBaseSection(unsigned concurrency, bool isAArch64Auth)1742: SyntheticSection(1743SHF_ALLOC,1744isAArch64Auth1745? SHT_AARCH64_AUTH_RELR1746: (config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR),1747config->wordsize, isAArch64Auth ? ".relr.auth.dyn" : ".relr.dyn"),1748relocsVec(concurrency) {}17491750void RelrBaseSection::mergeRels() {1751size_t newSize = relocs.size();1752for (const auto &v : relocsVec)1753newSize += v.size();1754relocs.reserve(newSize);1755for (const auto &v : relocsVec)1756llvm::append_range(relocs, v);1757relocsVec.clear();1758}17591760template <class ELFT>1761AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(1762StringRef name, unsigned concurrency)1763: RelocationBaseSection(1764name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,1765config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,1766config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ,1767/*combreloc=*/false, concurrency) {1768this->entsize = 1;1769}17701771template <class ELFT>1772bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {1773// This function computes the contents of an Android-format packed relocation1774// section.1775//1776// This format compresses relocations by using relocation groups to factor out1777// fields that are common between relocations and storing deltas from previous1778// relocations in SLEB128 format (which has a short representation for small1779// numbers). A good example of a relocation type with common fields is1780// R_*_RELATIVE, which is normally used to represent function pointers in1781// vtables. In the REL format, each relative relocation has the same r_info1782// field, and is only different from other relative relocations in terms of1783// the r_offset field. By sorting relocations by offset, grouping them by1784// r_info and representing each relocation with only the delta from the1785// previous offset, each 8-byte relocation can be compressed to as little as 11786// byte (or less with run-length encoding). This relocation packer was able to1787// reduce the size of the relocation section in an Android Chromium DSO from1788// 2,911,184 bytes to 174,693 bytes, or 6% of the original size.1789//1790// A relocation section consists of a header containing the literal bytes1791// 'APS2' followed by a sequence of SLEB128-encoded integers. The first two1792// elements are the total number of relocations in the section and an initial1793// r_offset value. The remaining elements define a sequence of relocation1794// groups. Each relocation group starts with a header consisting of the1795// following elements:1796//1797// - the number of relocations in the relocation group1798// - flags for the relocation group1799// - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta1800// for each relocation in the group.1801// - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info1802// field for each relocation in the group.1803// - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and1804// RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for1805// each relocation in the group.1806//1807// Following the relocation group header are descriptions of each of the1808// relocations in the group. They consist of the following elements:1809//1810// - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset1811// delta for this relocation.1812// - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info1813// field for this relocation.1814// - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and1815// RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for1816// this relocation.18171818size_t oldSize = relocData.size();18191820relocData = {'A', 'P', 'S', '2'};1821raw_svector_ostream os(relocData);1822auto add = [&](int64_t v) { encodeSLEB128(v, os); };18231824// The format header includes the number of relocations and the initial1825// offset (we set this to zero because the first relocation group will1826// perform the initial adjustment).1827add(relocs.size());1828add(0);18291830std::vector<Elf_Rela> relatives, nonRelatives;18311832for (const DynamicReloc &rel : relocs) {1833Elf_Rela r;1834r.r_offset = rel.getOffset();1835r.setSymbolAndType(rel.getSymIndex(getPartition().dynSymTab.get()),1836rel.type, false);1837r.r_addend = config->isRela ? rel.computeAddend() : 0;18381839if (r.getType(config->isMips64EL) == target->relativeRel)1840relatives.push_back(r);1841else1842nonRelatives.push_back(r);1843}18441845llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {1846return a.r_offset < b.r_offset;1847});18481849// Try to find groups of relative relocations which are spaced one word1850// apart from one another. These generally correspond to vtable entries. The1851// format allows these groups to be encoded using a sort of run-length1852// encoding, but each group will cost 7 bytes in addition to the offset from1853// the previous group, so it is only profitable to do this for groups of1854// size 8 or larger.1855std::vector<Elf_Rela> ungroupedRelatives;1856std::vector<std::vector<Elf_Rela>> relativeGroups;1857for (auto i = relatives.begin(), e = relatives.end(); i != e;) {1858std::vector<Elf_Rela> group;1859do {1860group.push_back(*i++);1861} while (i != e && (i - 1)->r_offset + config->wordsize == i->r_offset);18621863if (group.size() < 8)1864ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),1865group.end());1866else1867relativeGroups.emplace_back(std::move(group));1868}18691870// For non-relative relocations, we would like to:1871// 1. Have relocations with the same symbol offset to be consecutive, so1872// that the runtime linker can speed-up symbol lookup by implementing an1873// 1-entry cache.1874// 2. Group relocations by r_info to reduce the size of the relocation1875// section.1876// Since the symbol offset is the high bits in r_info, sorting by r_info1877// allows us to do both.1878//1879// For Rela, we also want to sort by r_addend when r_info is the same. This1880// enables us to group by r_addend as well.1881llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {1882if (a.r_info != b.r_info)1883return a.r_info < b.r_info;1884if (a.r_addend != b.r_addend)1885return a.r_addend < b.r_addend;1886return a.r_offset < b.r_offset;1887});18881889// Group relocations with the same r_info. Note that each group emits a group1890// header and that may make the relocation section larger. It is hard to1891// estimate the size of a group header as the encoded size of that varies1892// based on r_info. However, we can approximate this trade-off by the number1893// of values encoded. Each group header contains 3 values, and each relocation1894// in a group encodes one less value, as compared to when it is not grouped.1895// Therefore, we only group relocations if there are 3 or more of them with1896// the same r_info.1897//1898// For Rela, the addend for most non-relative relocations is zero, and thus we1899// can usually get a smaller relocation section if we group relocations with 01900// addend as well.1901std::vector<Elf_Rela> ungroupedNonRelatives;1902std::vector<std::vector<Elf_Rela>> nonRelativeGroups;1903for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) {1904auto j = i + 1;1905while (j != e && i->r_info == j->r_info &&1906(!config->isRela || i->r_addend == j->r_addend))1907++j;1908if (j - i < 3 || (config->isRela && i->r_addend != 0))1909ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j);1910else1911nonRelativeGroups.emplace_back(i, j);1912i = j;1913}19141915// Sort ungrouped relocations by offset to minimize the encoded length.1916llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {1917return a.r_offset < b.r_offset;1918});19191920unsigned hasAddendIfRela =1921config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0;19221923uint64_t offset = 0;1924uint64_t addend = 0;19251926// Emit the run-length encoding for the groups of adjacent relative1927// relocations. Each group is represented using two groups in the packed1928// format. The first is used to set the current offset to the start of the1929// group (and also encodes the first relocation), and the second encodes the1930// remaining relocations.1931for (std::vector<Elf_Rela> &g : relativeGroups) {1932// The first relocation in the group.1933add(1);1934add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |1935RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);1936add(g[0].r_offset - offset);1937add(target->relativeRel);1938if (config->isRela) {1939add(g[0].r_addend - addend);1940addend = g[0].r_addend;1941}19421943// The remaining relocations.1944add(g.size() - 1);1945add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |1946RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);1947add(config->wordsize);1948add(target->relativeRel);1949if (config->isRela) {1950for (const auto &i : llvm::drop_begin(g)) {1951add(i.r_addend - addend);1952addend = i.r_addend;1953}1954}19551956offset = g.back().r_offset;1957}19581959// Now the ungrouped relatives.1960if (!ungroupedRelatives.empty()) {1961add(ungroupedRelatives.size());1962add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);1963add(target->relativeRel);1964for (Elf_Rela &r : ungroupedRelatives) {1965add(r.r_offset - offset);1966offset = r.r_offset;1967if (config->isRela) {1968add(r.r_addend - addend);1969addend = r.r_addend;1970}1971}1972}19731974// Grouped non-relatives.1975for (ArrayRef<Elf_Rela> g : nonRelativeGroups) {1976add(g.size());1977add(RELOCATION_GROUPED_BY_INFO_FLAG);1978add(g[0].r_info);1979for (const Elf_Rela &r : g) {1980add(r.r_offset - offset);1981offset = r.r_offset;1982}1983addend = 0;1984}19851986// Finally the ungrouped non-relative relocations.1987if (!ungroupedNonRelatives.empty()) {1988add(ungroupedNonRelatives.size());1989add(hasAddendIfRela);1990for (Elf_Rela &r : ungroupedNonRelatives) {1991add(r.r_offset - offset);1992offset = r.r_offset;1993add(r.r_info);1994if (config->isRela) {1995add(r.r_addend - addend);1996addend = r.r_addend;1997}1998}1999}20002001// Don't allow the section to shrink; otherwise the size of the section can2002// oscillate infinitely.2003if (relocData.size() < oldSize)2004relocData.append(oldSize - relocData.size(), 0);20052006// Returns whether the section size changed. We need to keep recomputing both2007// section layout and the contents of this section until the size converges2008// because changing this section's size can affect section layout, which in2009// turn can affect the sizes of the LEB-encoded integers stored in this2010// section.2011return relocData.size() != oldSize;2012}20132014template <class ELFT>2015RelrSection<ELFT>::RelrSection(unsigned concurrency, bool isAArch64Auth)2016: RelrBaseSection(concurrency, isAArch64Auth) {2017this->entsize = config->wordsize;2018}20192020template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {2021// This function computes the contents of an SHT_RELR packed relocation2022// section.2023//2024// Proposal for adding SHT_RELR sections to generic-abi is here:2025// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg2026//2027// The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks2028// like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]2029//2030// i.e. start with an address, followed by any number of bitmaps. The address2031// entry encodes 1 relocation. The subsequent bitmap entries encode up to 632032// relocations each, at subsequent offsets following the last address entry.2033//2034// The bitmap entries must have 1 in the least significant bit. The assumption2035// here is that an address cannot have 1 in lsb. Odd addresses are not2036// supported.2037//2038// Excluding the least significant bit in the bitmap, each non-zero bit in2039// the bitmap represents a relocation to be applied to a corresponding machine2040// word that follows the base address word. The second least significant bit2041// represents the machine word immediately following the initial address, and2042// each bit that follows represents the next word, in linear order. As such,2043// a single bitmap can encode up to 31 relocations in a 32-bit object, and2044// 63 relocations in a 64-bit object.2045//2046// This encoding has a couple of interesting properties:2047// 1. Looking at any entry, it is clear whether it's an address or a bitmap:2048// even means address, odd means bitmap.2049// 2. Just a simple list of addresses is a valid encoding.20502051size_t oldSize = relrRelocs.size();2052relrRelocs.clear();20532054// Same as Config->Wordsize but faster because this is a compile-time2055// constant.2056const size_t wordsize = sizeof(typename ELFT::uint);20572058// Number of bits to use for the relocation offsets bitmap.2059// Must be either 63 or 31.2060const size_t nBits = wordsize * 8 - 1;20612062// Get offsets for all relative relocations and sort them.2063std::unique_ptr<uint64_t[]> offsets(new uint64_t[relocs.size()]);2064for (auto [i, r] : llvm::enumerate(relocs))2065offsets[i] = r.getOffset();2066llvm::sort(offsets.get(), offsets.get() + relocs.size());20672068// For each leading relocation, find following ones that can be folded2069// as a bitmap and fold them.2070for (size_t i = 0, e = relocs.size(); i != e;) {2071// Add a leading relocation.2072relrRelocs.push_back(Elf_Relr(offsets[i]));2073uint64_t base = offsets[i] + wordsize;2074++i;20752076// Find foldable relocations to construct bitmaps.2077for (;;) {2078uint64_t bitmap = 0;2079for (; i != e; ++i) {2080uint64_t d = offsets[i] - base;2081if (d >= nBits * wordsize || d % wordsize)2082break;2083bitmap |= uint64_t(1) << (d / wordsize);2084}2085if (!bitmap)2086break;2087relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));2088base += nBits * wordsize;2089}2090}20912092// Don't allow the section to shrink; otherwise the size of the section can2093// oscillate infinitely. Trailing 1s do not decode to more relocations.2094if (relrRelocs.size() < oldSize) {2095log(".relr.dyn needs " + Twine(oldSize - relrRelocs.size()) +2096" padding word(s)");2097relrRelocs.resize(oldSize, Elf_Relr(1));2098}20992100return relrRelocs.size() != oldSize;2101}21022103SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec)2104: SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,2105strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,2106config->wordsize,2107strTabSec.isDynamic() ? ".dynsym" : ".symtab"),2108strTabSec(strTabSec) {}21092110// Orders symbols according to their positions in the GOT,2111// in compliance with MIPS ABI rules.2112// See "Global Offset Table" in Chapter 5 in the following document2113// for detailed description:2114// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf2115static bool sortMipsSymbols(const SymbolTableEntry &l,2116const SymbolTableEntry &r) {2117// Sort entries related to non-local preemptible symbols by GOT indexes.2118// All other entries go to the beginning of a dynsym in arbitrary order.2119if (l.sym->isInGot() && r.sym->isInGot())2120return l.sym->getGotIdx() < r.sym->getGotIdx();2121if (!l.sym->isInGot() && !r.sym->isInGot())2122return false;2123return !l.sym->isInGot();2124}21252126void SymbolTableBaseSection::finalizeContents() {2127if (OutputSection *sec = strTabSec.getParent())2128getParent()->link = sec->sectionIndex;21292130if (this->type != SHT_DYNSYM) {2131sortSymTabSymbols();2132return;2133}21342135// If it is a .dynsym, there should be no local symbols, but we need2136// to do a few things for the dynamic linker.21372138// Section's Info field has the index of the first non-local symbol.2139// Because the first symbol entry is a null entry, 1 is the first.2140getParent()->info = 1;21412142if (getPartition().gnuHashTab) {2143// NB: It also sorts Symbols to meet the GNU hash table requirements.2144getPartition().gnuHashTab->addSymbols(symbols);2145} else if (config->emachine == EM_MIPS) {2146llvm::stable_sort(symbols, sortMipsSymbols);2147}21482149// Only the main partition's dynsym indexes are stored in the symbols2150// themselves. All other partitions use a lookup table.2151if (this == mainPart->dynSymTab.get()) {2152size_t i = 0;2153for (const SymbolTableEntry &s : symbols)2154s.sym->dynsymIndex = ++i;2155}2156}21572158// The ELF spec requires that all local symbols precede global symbols, so we2159// sort symbol entries in this function. (For .dynsym, we don't do that because2160// symbols for dynamic linking are inherently all globals.)2161//2162// Aside from above, we put local symbols in groups starting with the STT_FILE2163// symbol. That is convenient for purpose of identifying where are local symbols2164// coming from.2165void SymbolTableBaseSection::sortSymTabSymbols() {2166// Move all local symbols before global symbols.2167auto e = std::stable_partition(2168symbols.begin(), symbols.end(),2169[](const SymbolTableEntry &s) { return s.sym->isLocal(); });2170size_t numLocals = e - symbols.begin();2171getParent()->info = numLocals + 1;21722173// We want to group the local symbols by file. For that we rebuild the local2174// part of the symbols vector. We do not need to care about the STT_FILE2175// symbols, they are already naturally placed first in each group. That2176// happens because STT_FILE is always the first symbol in the object and hence2177// precede all other local symbols we add for a file.2178MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr;2179for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e))2180arr[s.sym->file].push_back(s);21812182auto i = symbols.begin();2183for (auto &p : arr)2184for (SymbolTableEntry &entry : p.second)2185*i++ = entry;2186}21872188void SymbolTableBaseSection::addSymbol(Symbol *b) {2189// Adding a local symbol to a .dynsym is a bug.2190assert(this->type != SHT_DYNSYM || !b->isLocal());2191symbols.push_back({b, strTabSec.addString(b->getName(), false)});2192}21932194size_t SymbolTableBaseSection::getSymbolIndex(const Symbol &sym) {2195if (this == mainPart->dynSymTab.get())2196return sym.dynsymIndex;21972198// Initializes symbol lookup tables lazily. This is used only for -r,2199// --emit-relocs and dynsyms in partitions other than the main one.2200llvm::call_once(onceFlag, [&] {2201symbolIndexMap.reserve(symbols.size());2202size_t i = 0;2203for (const SymbolTableEntry &e : symbols) {2204if (e.sym->type == STT_SECTION)2205sectionIndexMap[e.sym->getOutputSection()] = ++i;2206else2207symbolIndexMap[e.sym] = ++i;2208}2209});22102211// Section symbols are mapped based on their output sections2212// to maintain their semantics.2213if (sym.type == STT_SECTION)2214return sectionIndexMap.lookup(sym.getOutputSection());2215return symbolIndexMap.lookup(&sym);2216}22172218template <class ELFT>2219SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec)2220: SymbolTableBaseSection(strTabSec) {2221this->entsize = sizeof(Elf_Sym);2222}22232224static BssSection *getCommonSec(Symbol *sym) {2225if (config->relocatable)2226if (auto *d = dyn_cast<Defined>(sym))2227return dyn_cast_or_null<BssSection>(d->section);2228return nullptr;2229}22302231static uint32_t getSymSectionIndex(Symbol *sym) {2232assert(!(sym->hasFlag(NEEDS_COPY) && sym->isObject()));2233if (!isa<Defined>(sym) || sym->hasFlag(NEEDS_COPY))2234return SHN_UNDEF;2235if (const OutputSection *os = sym->getOutputSection())2236return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX2237: os->sectionIndex;2238return SHN_ABS;2239}22402241// Write the internal symbol table contents to the output symbol table.2242template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {2243// The first entry is a null entry as per the ELF spec.2244buf += sizeof(Elf_Sym);22452246auto *eSym = reinterpret_cast<Elf_Sym *>(buf);22472248for (SymbolTableEntry &ent : symbols) {2249Symbol *sym = ent.sym;2250bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;22512252// Set st_name, st_info and st_other.2253eSym->st_name = ent.strTabOffset;2254eSym->setBindingAndType(sym->binding, sym->type);2255eSym->st_other = sym->stOther;22562257if (BssSection *commonSec = getCommonSec(sym)) {2258// When -r is specified, a COMMON symbol is not allocated. Its st_shndx2259// holds SHN_COMMON and st_value holds the alignment.2260eSym->st_shndx = SHN_COMMON;2261eSym->st_value = commonSec->addralign;2262eSym->st_size = cast<Defined>(sym)->size;2263} else {2264const uint32_t shndx = getSymSectionIndex(sym);2265if (isDefinedHere) {2266eSym->st_shndx = shndx;2267eSym->st_value = sym->getVA();2268// Copy symbol size if it is a defined symbol. st_size is not2269// significant for undefined symbols, so whether copying it or not is up2270// to us if that's the case. We'll leave it as zero because by not2271// setting a value, we can get the exact same outputs for two sets of2272// input files that differ only in undefined symbol size in DSOs.2273eSym->st_size = shndx != SHN_UNDEF ? cast<Defined>(sym)->size : 0;2274} else {2275eSym->st_shndx = 0;2276eSym->st_value = 0;2277eSym->st_size = 0;2278}2279}22802281++eSym;2282}22832284// On MIPS we need to mark symbol which has a PLT entry and requires2285// pointer equality by STO_MIPS_PLT flag. That is necessary to help2286// dynamic linker distinguish such symbols and MIPS lazy-binding stubs.2287// https://sourceware.org/ml/binutils/2008-07/txt00000.txt2288if (config->emachine == EM_MIPS) {2289auto *eSym = reinterpret_cast<Elf_Sym *>(buf);22902291for (SymbolTableEntry &ent : symbols) {2292Symbol *sym = ent.sym;2293if (sym->isInPlt() && sym->hasFlag(NEEDS_COPY))2294eSym->st_other |= STO_MIPS_PLT;2295if (isMicroMips()) {2296// We already set the less-significant bit for symbols2297// marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT2298// records. That allows us to distinguish such symbols in2299// the `MIPS<ELFT>::relocate()` routine. Now we should2300// clear that bit for non-dynamic symbol table, so tools2301// like `objdump` will be able to deal with a correct2302// symbol position.2303if (sym->isDefined() &&2304((sym->stOther & STO_MIPS_MICROMIPS) || sym->hasFlag(NEEDS_COPY))) {2305if (!strTabSec.isDynamic())2306eSym->st_value &= ~1;2307eSym->st_other |= STO_MIPS_MICROMIPS;2308}2309}2310if (config->relocatable)2311if (auto *d = dyn_cast<Defined>(sym))2312if (isMipsPIC<ELFT>(d))2313eSym->st_other |= STO_MIPS_PIC;2314++eSym;2315}2316}2317}23182319SymtabShndxSection::SymtabShndxSection()2320: SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndx") {2321this->entsize = 4;2322}23232324void SymtabShndxSection::writeTo(uint8_t *buf) {2325// We write an array of 32 bit values, where each value has 1:1 association2326// with an entry in .symtab. If the corresponding entry contains SHN_XINDEX,2327// we need to write actual index, otherwise, we must write SHN_UNDEF(0).2328buf += 4; // Ignore .symtab[0] entry.2329for (const SymbolTableEntry &entry : in.symTab->getSymbols()) {2330if (!getCommonSec(entry.sym) && getSymSectionIndex(entry.sym) == SHN_XINDEX)2331write32(buf, entry.sym->getOutputSection()->sectionIndex);2332buf += 4;2333}2334}23352336bool SymtabShndxSection::isNeeded() const {2337// SHT_SYMTAB can hold symbols with section indices values up to2338// SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX2339// section. Problem is that we reveal the final section indices a bit too2340// late, and we do not know them here. For simplicity, we just always create2341// a .symtab_shndx section when the amount of output sections is huge.2342size_t size = 0;2343for (SectionCommand *cmd : script->sectionCommands)2344if (isa<OutputDesc>(cmd))2345++size;2346return size >= SHN_LORESERVE;2347}23482349void SymtabShndxSection::finalizeContents() {2350getParent()->link = in.symTab->getParent()->sectionIndex;2351}23522353size_t SymtabShndxSection::getSize() const {2354return in.symTab->getNumSymbols() * 4;2355}23562357// .hash and .gnu.hash sections contain on-disk hash tables that map2358// symbol names to their dynamic symbol table indices. Their purpose2359// is to help the dynamic linker resolve symbols quickly. If ELF files2360// don't have them, the dynamic linker has to do linear search on all2361// dynamic symbols, which makes programs slower. Therefore, a .hash2362// section is added to a DSO by default.2363//2364// The Unix semantics of resolving dynamic symbols is somewhat expensive.2365// Each ELF file has a list of DSOs that the ELF file depends on and a2366// list of dynamic symbols that need to be resolved from any of the2367// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)2368// where m is the number of DSOs and n is the number of dynamic2369// symbols. For modern large programs, both m and n are large. So2370// making each step faster by using hash tables substantially2371// improves time to load programs.2372//2373// (Note that this is not the only way to design the shared library.2374// For instance, the Windows DLL takes a different approach. On2375// Windows, each dynamic symbol has a name of DLL from which the symbol2376// has to be resolved. That makes the cost of symbol resolution O(n).2377// This disables some hacky techniques you can use on Unix such as2378// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)2379//2380// Due to historical reasons, we have two different hash tables, .hash2381// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new2382// and better version of .hash. .hash is just an on-disk hash table, but2383// .gnu.hash has a bloom filter in addition to a hash table to skip2384// DSOs very quickly. If you are sure that your dynamic linker knows2385// about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a2386// safe bet is to specify --hash-style=both for backward compatibility.2387GnuHashTableSection::GnuHashTableSection()2388: SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, config->wordsize, ".gnu.hash") {2389}23902391void GnuHashTableSection::finalizeContents() {2392if (OutputSection *sec = getPartition().dynSymTab->getParent())2393getParent()->link = sec->sectionIndex;23942395// Computes bloom filter size in word size. We want to allocate 122396// bits for each symbol. It must be a power of two.2397if (symbols.empty()) {2398maskWords = 1;2399} else {2400uint64_t numBits = symbols.size() * 12;2401maskWords = NextPowerOf2(numBits / (config->wordsize * 8));2402}24032404size = 16; // Header2405size += config->wordsize * maskWords; // Bloom filter2406size += nBuckets * 4; // Hash buckets2407size += symbols.size() * 4; // Hash values2408}24092410void GnuHashTableSection::writeTo(uint8_t *buf) {2411// Write a header.2412write32(buf, nBuckets);2413write32(buf + 4, getPartition().dynSymTab->getNumSymbols() - symbols.size());2414write32(buf + 8, maskWords);2415write32(buf + 12, Shift2);2416buf += 16;24172418// Write the 2-bit bloom filter.2419const unsigned c = config->is64 ? 64 : 32;2420for (const Entry &sym : symbols) {2421// When C = 64, we choose a word with bits [6:...] and set 1 to two bits in2422// the word using bits [0:5] and [26:31].2423size_t i = (sym.hash / c) & (maskWords - 1);2424uint64_t val = readUint(buf + i * config->wordsize);2425val |= uint64_t(1) << (sym.hash % c);2426val |= uint64_t(1) << ((sym.hash >> Shift2) % c);2427writeUint(buf + i * config->wordsize, val);2428}2429buf += config->wordsize * maskWords;24302431// Write the hash table.2432uint32_t *buckets = reinterpret_cast<uint32_t *>(buf);2433uint32_t oldBucket = -1;2434uint32_t *values = buckets + nBuckets;2435for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) {2436// Write a hash value. It represents a sequence of chains that share the2437// same hash modulo value. The last element of each chain is terminated by2438// LSB 1.2439uint32_t hash = i->hash;2440bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx;2441hash = isLastInChain ? hash | 1 : hash & ~1;2442write32(values++, hash);24432444if (i->bucketIdx == oldBucket)2445continue;2446// Write a hash bucket. Hash buckets contain indices in the following hash2447// value table.2448write32(buckets + i->bucketIdx,2449getPartition().dynSymTab->getSymbolIndex(*i->sym));2450oldBucket = i->bucketIdx;2451}2452}24532454// Add symbols to this symbol hash table. Note that this function2455// destructively sort a given vector -- which is needed because2456// GNU-style hash table places some sorting requirements.2457void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) {2458// We cannot use 'auto' for Mid because GCC 6.1 cannot deduce2459// its type correctly.2460auto mid =2461std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) {2462return !s.sym->isDefined() || s.sym->partition != partition;2463});24642465// We chose load factor 4 for the on-disk hash table. For each hash2466// collision, the dynamic linker will compare a uint32_t hash value.2467// Since the integer comparison is quite fast, we believe we can2468// make the load factor even larger. 4 is just a conservative choice.2469//2470// Note that we don't want to create a zero-sized hash table because2471// Android loader as of 2018 doesn't like a .gnu.hash containing such2472// table. If that's the case, we create a hash table with one unused2473// dummy slot.2474nBuckets = std::max<size_t>((v.end() - mid) / 4, 1);24752476if (mid == v.end())2477return;24782479for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) {2480Symbol *b = ent.sym;2481uint32_t hash = hashGnu(b->getName());2482uint32_t bucketIdx = hash % nBuckets;2483symbols.push_back({b, ent.strTabOffset, hash, bucketIdx});2484}24852486llvm::sort(symbols, [](const Entry &l, const Entry &r) {2487return std::tie(l.bucketIdx, l.strTabOffset) <2488std::tie(r.bucketIdx, r.strTabOffset);2489});24902491v.erase(mid, v.end());2492for (const Entry &ent : symbols)2493v.push_back({ent.sym, ent.strTabOffset});2494}24952496HashTableSection::HashTableSection()2497: SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {2498this->entsize = 4;2499}25002501void HashTableSection::finalizeContents() {2502SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();25032504if (OutputSection *sec = symTab->getParent())2505getParent()->link = sec->sectionIndex;25062507unsigned numEntries = 2; // nbucket and nchain.2508numEntries += symTab->getNumSymbols(); // The chain entries.25092510// Create as many buckets as there are symbols.2511numEntries += symTab->getNumSymbols();2512this->size = numEntries * 4;2513}25142515void HashTableSection::writeTo(uint8_t *buf) {2516SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();2517unsigned numSymbols = symTab->getNumSymbols();25182519uint32_t *p = reinterpret_cast<uint32_t *>(buf);2520write32(p++, numSymbols); // nbucket2521write32(p++, numSymbols); // nchain25222523uint32_t *buckets = p;2524uint32_t *chains = p + numSymbols;25252526for (const SymbolTableEntry &s : symTab->getSymbols()) {2527Symbol *sym = s.sym;2528StringRef name = sym->getName();2529unsigned i = sym->dynsymIndex;2530uint32_t hash = hashSysV(name) % numSymbols;2531chains[i] = buckets[hash];2532write32(buckets + hash, i);2533}2534}25352536PltSection::PltSection()2537: SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"),2538headerSize(target->pltHeaderSize) {2539// On PowerPC, this section contains lazy symbol resolvers.2540if (config->emachine == EM_PPC64) {2541name = ".glink";2542addralign = 4;2543}25442545// On x86 when IBT is enabled, this section contains the second PLT (lazy2546// symbol resolvers).2547if ((config->emachine == EM_386 || config->emachine == EM_X86_64) &&2548(config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT))2549name = ".plt.sec";25502551// The PLT needs to be writable on SPARC as the dynamic linker will2552// modify the instructions in the PLT entries.2553if (config->emachine == EM_SPARCV9)2554this->flags |= SHF_WRITE;2555}25562557void PltSection::writeTo(uint8_t *buf) {2558// At beginning of PLT, we have code to call the dynamic2559// linker to resolve dynsyms at runtime. Write such code.2560target->writePltHeader(buf);2561size_t off = headerSize;25622563for (const Symbol *sym : entries) {2564target->writePlt(buf + off, *sym, getVA() + off);2565off += target->pltEntrySize;2566}2567}25682569void PltSection::addEntry(Symbol &sym) {2570assert(sym.auxIdx == symAux.size() - 1);2571symAux.back().pltIdx = entries.size();2572entries.push_back(&sym);2573}25742575size_t PltSection::getSize() const {2576return headerSize + entries.size() * target->pltEntrySize;2577}25782579bool PltSection::isNeeded() const {2580// For -z retpolineplt, .iplt needs the .plt header.2581return !entries.empty() || (config->zRetpolineplt && in.iplt->isNeeded());2582}25832584// Used by ARM to add mapping symbols in the PLT section, which aid2585// disassembly.2586void PltSection::addSymbols() {2587target->addPltHeaderSymbols(*this);25882589size_t off = headerSize;2590for (size_t i = 0; i < entries.size(); ++i) {2591target->addPltSymbols(*this, off);2592off += target->pltEntrySize;2593}2594}25952596IpltSection::IpltSection()2597: SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".iplt") {2598if (config->emachine == EM_PPC || config->emachine == EM_PPC64) {2599name = ".glink";2600addralign = 4;2601}2602}26032604void IpltSection::writeTo(uint8_t *buf) {2605uint32_t off = 0;2606for (const Symbol *sym : entries) {2607target->writeIplt(buf + off, *sym, getVA() + off);2608off += target->ipltEntrySize;2609}2610}26112612size_t IpltSection::getSize() const {2613return entries.size() * target->ipltEntrySize;2614}26152616void IpltSection::addEntry(Symbol &sym) {2617assert(sym.auxIdx == symAux.size() - 1);2618symAux.back().pltIdx = entries.size();2619entries.push_back(&sym);2620}26212622// ARM uses mapping symbols to aid disassembly.2623void IpltSection::addSymbols() {2624size_t off = 0;2625for (size_t i = 0, e = entries.size(); i != e; ++i) {2626target->addPltSymbols(*this, off);2627off += target->pltEntrySize;2628}2629}26302631PPC32GlinkSection::PPC32GlinkSection() {2632name = ".glink";2633addralign = 4;2634}26352636void PPC32GlinkSection::writeTo(uint8_t *buf) {2637writePPC32GlinkSection(buf, entries.size());2638}26392640size_t PPC32GlinkSection::getSize() const {2641return headerSize + entries.size() * target->pltEntrySize + footerSize;2642}26432644// This is an x86-only extra PLT section and used only when a security2645// enhancement feature called CET is enabled. In this comment, I'll explain what2646// the feature is and why we have two PLT sections if CET is enabled.2647//2648// So, what does CET do? CET introduces a new restriction to indirect jump2649// instructions. CET works this way. Assume that CET is enabled. Then, if you2650// execute an indirect jump instruction, the processor verifies that a special2651// "landing pad" instruction (which is actually a repurposed NOP instruction and2652// now called "endbr32" or "endbr64") is at the jump target. If the jump target2653// does not start with that instruction, the processor raises an exception2654// instead of continuing executing code.2655//2656// If CET is enabled, the compiler emits endbr to all locations where indirect2657// jumps may jump to.2658//2659// This mechanism makes it extremely hard to transfer the control to a middle of2660// a function that is not supporsed to be a indirect jump target, preventing2661// certain types of attacks such as ROP or JOP.2662//2663// Note that the processors in the market as of 2019 don't actually support the2664// feature. Only the spec is available at the moment.2665//2666// Now, I'll explain why we have this extra PLT section for CET.2667//2668// Since you can indirectly jump to a PLT entry, we have to make PLT entries2669// start with endbr. The problem is there's no extra space for endbr (which is 42670// bytes long), as the PLT entry is only 16 bytes long and all bytes are already2671// used.2672//2673// In order to deal with the issue, we split a PLT entry into two PLT entries.2674// Remember that each PLT entry contains code to jump to an address read from2675// .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme,2676// the former code is written to .plt.sec, and the latter code is written to2677// .plt.2678//2679// Lazy symbol resolution in the 2-PLT scheme works in the usual way, except2680// that the regular .plt is now called .plt.sec and .plt is repurposed to2681// contain only code for lazy symbol resolution.2682//2683// In other words, this is how the 2-PLT scheme works. Application code is2684// supposed to jump to .plt.sec to call an external function. Each .plt.sec2685// entry contains code to read an address from a corresponding .got.plt entry2686// and jump to that address. Addresses in .got.plt initially point to .plt, so2687// when an application calls an external function for the first time, the2688// control is transferred to a function that resolves a symbol name from2689// external shared object files. That function then rewrites a .got.plt entry2690// with a resolved address, so that the subsequent function calls directly jump2691// to a desired location from .plt.sec.2692//2693// There is an open question as to whether the 2-PLT scheme was desirable or2694// not. We could have simply extended the PLT entry size to 32-bytes to2695// accommodate endbr, and that scheme would have been much simpler than the2696// 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot2697// code (.plt.sec) from cold code (.plt). But as far as I know no one proved2698// that the optimization actually makes a difference.2699//2700// That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools2701// depend on it, so we implement the ABI.2702IBTPltSection::IBTPltSection()2703: SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt") {}27042705void IBTPltSection::writeTo(uint8_t *buf) {2706target->writeIBTPlt(buf, in.plt->getNumEntries());2707}27082709size_t IBTPltSection::getSize() const {2710// 16 is the header size of .plt.2711return 16 + in.plt->getNumEntries() * target->pltEntrySize;2712}27132714bool IBTPltSection::isNeeded() const { return in.plt->getNumEntries() > 0; }27152716RelroPaddingSection::RelroPaddingSection()2717: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, ".relro_padding") {2718}27192720// The string hash function for .gdb_index.2721static uint32_t computeGdbHash(StringRef s) {2722uint32_t h = 0;2723for (uint8_t c : s)2724h = h * 67 + toLower(c) - 113;2725return h;2726}27272728// 4-byte alignment ensures that values in the hash lookup table and the name2729// table are aligned.2730DebugNamesBaseSection::DebugNamesBaseSection()2731: SyntheticSection(0, SHT_PROGBITS, 4, ".debug_names") {}27322733// Get the size of the .debug_names section header in bytes for DWARF32:2734static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {2735return /* unit length */ 4 +2736/* version */ 2 +2737/* padding */ 2 +2738/* CU count */ 4 +2739/* TU count */ 4 +2740/* Foreign TU count */ 4 +2741/* Bucket Count */ 4 +2742/* Name Count */ 4 +2743/* Abbrev table size */ 4 +2744/* Augmentation string size */ 4 +2745/* Augmentation string */ augmentationStringSize;2746}27472748static Expected<DebugNamesBaseSection::IndexEntry *>2749readEntry(uint64_t &offset, const DWARFDebugNames::NameIndex &ni,2750uint64_t entriesBase, DWARFDataExtractor &namesExtractor,2751const LLDDWARFSection &namesSec) {2752auto ie = makeThreadLocal<DebugNamesBaseSection::IndexEntry>();2753ie->poolOffset = offset;2754Error err = Error::success();2755uint64_t ulebVal = namesExtractor.getULEB128(&offset, &err);2756if (err)2757return createStringError(inconvertibleErrorCode(),2758"invalid abbrev code: %s",2759toString(std::move(err)).c_str());2760if (!isUInt<32>(ulebVal))2761return createStringError(inconvertibleErrorCode(),2762"abbrev code too large for DWARF32: %" PRIu64,2763ulebVal);2764ie->abbrevCode = static_cast<uint32_t>(ulebVal);2765auto it = ni.getAbbrevs().find_as(ie->abbrevCode);2766if (it == ni.getAbbrevs().end())2767return createStringError(inconvertibleErrorCode(),2768"abbrev code not found in abbrev table: %" PRIu32,2769ie->abbrevCode);27702771DebugNamesBaseSection::AttrValue attr, cuAttr = {0, 0};2772for (DWARFDebugNames::AttributeEncoding a : it->Attributes) {2773if (a.Index == dwarf::DW_IDX_parent) {2774if (a.Form == dwarf::DW_FORM_ref4) {2775attr.attrValue = namesExtractor.getU32(&offset, &err);2776attr.attrSize = 4;2777ie->parentOffset = entriesBase + attr.attrValue;2778} else if (a.Form != DW_FORM_flag_present)2779return createStringError(inconvertibleErrorCode(),2780"invalid form for DW_IDX_parent");2781} else {2782switch (a.Form) {2783case DW_FORM_data1:2784case DW_FORM_ref1: {2785attr.attrValue = namesExtractor.getU8(&offset, &err);2786attr.attrSize = 1;2787break;2788}2789case DW_FORM_data2:2790case DW_FORM_ref2: {2791attr.attrValue = namesExtractor.getU16(&offset, &err);2792attr.attrSize = 2;2793break;2794}2795case DW_FORM_data4:2796case DW_FORM_ref4: {2797attr.attrValue = namesExtractor.getU32(&offset, &err);2798attr.attrSize = 4;2799break;2800}2801default:2802return createStringError(2803inconvertibleErrorCode(),2804"unrecognized form encoding %d in abbrev table", a.Form);2805}2806}2807if (err)2808return createStringError(inconvertibleErrorCode(),2809"error while reading attributes: %s",2810toString(std::move(err)).c_str());2811if (a.Index == DW_IDX_compile_unit)2812cuAttr = attr;2813else if (a.Form != DW_FORM_flag_present)2814ie->attrValues.push_back(attr);2815}2816// Canonicalize abbrev by placing the CU/TU index at the end.2817ie->attrValues.push_back(cuAttr);2818return ie;2819}28202821void DebugNamesBaseSection::parseDebugNames(2822InputChunk &inputChunk, OutputChunk &chunk,2823DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,2824function_ref<SmallVector<uint32_t, 0>(2825uint32_t numCus, const DWARFDebugNames::Header &,2826const DWARFDebugNames::DWARFDebugNamesOffsets &)>2827readOffsets) {2828const LLDDWARFSection &namesSec = inputChunk.section;2829DenseMap<uint32_t, IndexEntry *> offsetMap;2830// Number of CUs seen in previous NameIndex sections within current chunk.2831uint32_t numCus = 0;2832for (const DWARFDebugNames::NameIndex &ni : *inputChunk.llvmDebugNames) {2833NameData &nd = inputChunk.nameData.emplace_back();2834nd.hdr = ni.getHeader();2835if (nd.hdr.Format != DwarfFormat::DWARF32) {2836errorOrWarn(toString(namesSec.sec) +2837Twine(": found DWARF64, which is currently unsupported"));2838return;2839}2840if (nd.hdr.Version != 5) {2841errorOrWarn(toString(namesSec.sec) + Twine(": unsupported version: ") +2842Twine(nd.hdr.Version));2843return;2844}2845uint32_t dwarfSize = dwarf::getDwarfOffsetByteSize(DwarfFormat::DWARF32);2846DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();2847if (locs.EntriesBase > namesExtractor.getData().size()) {2848errorOrWarn(toString(namesSec.sec) +2849Twine(": entry pool start is beyond end of section"));2850return;2851}28522853SmallVector<uint32_t, 0> entryOffsets = readOffsets(numCus, nd.hdr, locs);28542855// Read the entry pool.2856offsetMap.clear();2857nd.nameEntries.resize(nd.hdr.NameCount);2858for (auto i : seq(nd.hdr.NameCount)) {2859NameEntry &ne = nd.nameEntries[i];2860uint64_t strOffset = locs.StringOffsetsBase + i * dwarfSize;2861ne.stringOffset = strOffset;2862uint64_t strp = namesExtractor.getRelocatedValue(dwarfSize, &strOffset);2863StringRef name = strExtractor.getCStrRef(&strp);2864ne.name = name.data();2865ne.hashValue = caseFoldingDjbHash(name);28662867// Read a series of index entries that end with abbreviation code 0.2868uint64_t offset = locs.EntriesBase + entryOffsets[i];2869while (offset < namesSec.Data.size() && namesSec.Data[offset] != 0) {2870// Read & store all entries (for the same string).2871Expected<IndexEntry *> ieOrErr =2872readEntry(offset, ni, locs.EntriesBase, namesExtractor, namesSec);2873if (!ieOrErr) {2874errorOrWarn(toString(namesSec.sec) + ": " +2875toString(ieOrErr.takeError()));2876return;2877}2878ne.indexEntries.push_back(std::move(*ieOrErr));2879}2880if (offset >= namesSec.Data.size())2881errorOrWarn(toString(namesSec.sec) +2882Twine(": index entry is out of bounds"));28832884for (IndexEntry &ie : ne.entries())2885offsetMap[ie.poolOffset] = &ie;2886}28872888// Assign parent pointers, which will be used to update DW_IDX_parent index2889// attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will2890// get parentEntry == null as well.2891for (NameEntry &ne : nd.nameEntries)2892for (IndexEntry &ie : ne.entries())2893ie.parentEntry = offsetMap.lookup(ie.parentOffset);2894numCus += nd.hdr.CompUnitCount;2895}2896}28972898// Compute the form for output DW_IDX_compile_unit attributes, similar to2899// DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all2900// the merged CU indices.2901std::pair<uint8_t, dwarf::Form> static getMergedCuCountForm(2902uint32_t compUnitCount) {2903if (compUnitCount > UINT16_MAX)2904return {4, DW_FORM_data4};2905if (compUnitCount > UINT8_MAX)2906return {2, DW_FORM_data2};2907return {1, DW_FORM_data1};2908}29092910void DebugNamesBaseSection::computeHdrAndAbbrevTable(2911MutableArrayRef<InputChunk> inputChunks) {2912TimeTraceScope timeScope("Merge .debug_names", "hdr and abbrev table");2913size_t numCu = 0;2914hdr.Format = DwarfFormat::DWARF32;2915hdr.Version = 5;2916hdr.CompUnitCount = 0;2917hdr.LocalTypeUnitCount = 0;2918hdr.ForeignTypeUnitCount = 0;2919hdr.AugmentationStringSize = 0;29202921// Compute CU and TU counts.2922for (auto i : seq(numChunks)) {2923InputChunk &inputChunk = inputChunks[i];2924inputChunk.baseCuIdx = numCu;2925numCu += chunks[i].compUnits.size();2926for (const NameData &nd : inputChunk.nameData) {2927hdr.CompUnitCount += nd.hdr.CompUnitCount;2928// TODO: We don't handle type units yet, so LocalTypeUnitCount &2929// ForeignTypeUnitCount are left as 0.2930if (nd.hdr.LocalTypeUnitCount || nd.hdr.ForeignTypeUnitCount)2931warn(toString(inputChunk.section.sec) +2932Twine(": type units are not implemented"));2933// If augmentation strings are not identical, use an empty string.2934if (i == 0) {2935hdr.AugmentationStringSize = nd.hdr.AugmentationStringSize;2936hdr.AugmentationString = nd.hdr.AugmentationString;2937} else if (hdr.AugmentationString != nd.hdr.AugmentationString) {2938// There are conflicting augmentation strings, so it's best for the2939// merged index to not use an augmentation string.2940hdr.AugmentationStringSize = 0;2941hdr.AugmentationString.clear();2942}2943}2944}29452946// Create the merged abbrev table, uniquifyinng the input abbrev tables and2947// computing mapping from old (per-cu) abbrev codes to new (merged) abbrev2948// codes.2949FoldingSet<Abbrev> abbrevSet;2950// Determine the form for the DW_IDX_compile_unit attributes in the merged2951// index. The input form may not be big enough for all CU indices.2952dwarf::Form cuAttrForm = getMergedCuCountForm(hdr.CompUnitCount).second;2953for (InputChunk &inputChunk : inputChunks) {2954for (auto [i, ni] : enumerate(*inputChunk.llvmDebugNames)) {2955for (const DWARFDebugNames::Abbrev &oldAbbrev : ni.getAbbrevs()) {2956// Canonicalize abbrev by placing the CU/TU index at the end,2957// similar to 'parseDebugNames'.2958Abbrev abbrev;2959DWARFDebugNames::AttributeEncoding cuAttr(DW_IDX_compile_unit,2960cuAttrForm);2961abbrev.code = oldAbbrev.Code;2962abbrev.tag = oldAbbrev.Tag;2963for (DWARFDebugNames::AttributeEncoding a : oldAbbrev.Attributes) {2964if (a.Index == DW_IDX_compile_unit)2965cuAttr.Index = a.Index;2966else2967abbrev.attributes.push_back({a.Index, a.Form});2968}2969// Put the CU/TU index at the end of the attributes list.2970abbrev.attributes.push_back(cuAttr);29712972// Profile the abbrev, get or assign a new code, then record the abbrev2973// code mapping.2974FoldingSetNodeID id;2975abbrev.Profile(id);2976uint32_t newCode;2977void *insertPos;2978if (Abbrev *existing = abbrevSet.FindNodeOrInsertPos(id, insertPos)) {2979// Found it; we've already seen an identical abbreviation.2980newCode = existing->code;2981} else {2982Abbrev *abbrev2 =2983new (abbrevAlloc.Allocate()) Abbrev(std::move(abbrev));2984abbrevSet.InsertNode(abbrev2, insertPos);2985abbrevTable.push_back(abbrev2);2986newCode = abbrevTable.size();2987abbrev2->code = newCode;2988}2989inputChunk.nameData[i].abbrevCodeMap[oldAbbrev.Code] = newCode;2990}2991}2992}29932994// Compute the merged abbrev table.2995raw_svector_ostream os(abbrevTableBuf);2996for (Abbrev *abbrev : abbrevTable) {2997encodeULEB128(abbrev->code, os);2998encodeULEB128(abbrev->tag, os);2999for (DWARFDebugNames::AttributeEncoding a : abbrev->attributes) {3000encodeULEB128(a.Index, os);3001encodeULEB128(a.Form, os);3002}3003os.write("\0", 2); // attribute specification end3004}3005os.write(0); // abbrev table end3006hdr.AbbrevTableSize = abbrevTableBuf.size();3007}30083009void DebugNamesBaseSection::Abbrev::Profile(FoldingSetNodeID &id) const {3010id.AddInteger(tag);3011for (const DWARFDebugNames::AttributeEncoding &attr : attributes) {3012id.AddInteger(attr.Index);3013id.AddInteger(attr.Form);3014}3015}30163017std::pair<uint32_t, uint32_t> DebugNamesBaseSection::computeEntryPool(3018MutableArrayRef<InputChunk> inputChunks) {3019TimeTraceScope timeScope("Merge .debug_names", "entry pool");3020// Collect and de-duplicate all the names (preserving all the entries).3021// Speed it up using multithreading, as the number of symbols can be in the3022// order of millions.3023const size_t concurrency =3024bit_floor(std::min<size_t>(config->threadCount, numShards));3025const size_t shift = 32 - countr_zero(numShards);3026const uint8_t cuAttrSize = getMergedCuCountForm(hdr.CompUnitCount).first;3027DenseMap<CachedHashStringRef, size_t> maps[numShards];30283029parallelFor(0, concurrency, [&](size_t threadId) {3030for (auto i : seq(numChunks)) {3031InputChunk &inputChunk = inputChunks[i];3032for (auto j : seq(inputChunk.nameData.size())) {3033NameData &nd = inputChunk.nameData[j];3034// Deduplicate the NameEntry records (based on the string/name),3035// appending all IndexEntries from duplicate NameEntry records to3036// the single preserved copy.3037for (NameEntry &ne : nd.nameEntries) {3038auto shardId = ne.hashValue >> shift;3039if ((shardId & (concurrency - 1)) != threadId)3040continue;30413042ne.chunkIdx = i;3043for (IndexEntry &ie : ne.entries()) {3044// Update the IndexEntry's abbrev code to match the merged3045// abbreviations.3046ie.abbrevCode = nd.abbrevCodeMap[ie.abbrevCode];3047// Update the DW_IDX_compile_unit attribute (the last one after3048// canonicalization) to have correct merged offset value and size.3049auto &back = ie.attrValues.back();3050back.attrValue += inputChunk.baseCuIdx + j;3051back.attrSize = cuAttrSize;3052}30533054auto &nameVec = nameVecs[shardId];3055auto [it, inserted] = maps[shardId].try_emplace(3056CachedHashStringRef(ne.name, ne.hashValue), nameVec.size());3057if (inserted)3058nameVec.push_back(std::move(ne));3059else3060nameVec[it->second].indexEntries.append(std::move(ne.indexEntries));3061}3062}3063}3064});30653066// Compute entry offsets in parallel. First, compute offsets relative to the3067// current shard.3068uint32_t offsets[numShards];3069parallelFor(0, numShards, [&](size_t shard) {3070uint32_t offset = 0;3071for (NameEntry &ne : nameVecs[shard]) {3072ne.entryOffset = offset;3073for (IndexEntry &ie : ne.entries()) {3074ie.poolOffset = offset;3075offset += getULEB128Size(ie.abbrevCode);3076for (AttrValue value : ie.attrValues)3077offset += value.attrSize;3078}3079++offset; // index entry sentinel3080}3081offsets[shard] = offset;3082});3083// Then add shard offsets.3084std::partial_sum(offsets, std::end(offsets), offsets);3085parallelFor(1, numShards, [&](size_t shard) {3086uint32_t offset = offsets[shard - 1];3087for (NameEntry &ne : nameVecs[shard]) {3088ne.entryOffset += offset;3089for (IndexEntry &ie : ne.entries())3090ie.poolOffset += offset;3091}3092});30933094// Update the DW_IDX_parent entries that refer to real parents (have3095// DW_FORM_ref4).3096parallelFor(0, numShards, [&](size_t shard) {3097for (NameEntry &ne : nameVecs[shard]) {3098for (IndexEntry &ie : ne.entries()) {3099if (!ie.parentEntry)3100continue;3101// Abbrevs are indexed starting at 1; vector starts at 0. (abbrevCode3102// corresponds to position in the merged table vector).3103const Abbrev *abbrev = abbrevTable[ie.abbrevCode - 1];3104for (const auto &[a, v] : zip_equal(abbrev->attributes, ie.attrValues))3105if (a.Index == DW_IDX_parent && a.Form == DW_FORM_ref4)3106v.attrValue = ie.parentEntry->poolOffset;3107}3108}3109});31103111// Return (entry pool size, number of entries).3112uint32_t num = 0;3113for (auto &map : maps)3114num += map.size();3115return {offsets[numShards - 1], num};3116}31173118void DebugNamesBaseSection::init(3119function_ref<void(InputFile *, InputChunk &, OutputChunk &)> parseFile) {3120TimeTraceScope timeScope("Merge .debug_names");3121// Collect and remove input .debug_names sections. Save InputSection pointers3122// to relocate string offsets in `writeTo`.3123SetVector<InputFile *> files;3124for (InputSectionBase *s : ctx.inputSections) {3125InputSection *isec = dyn_cast<InputSection>(s);3126if (!isec)3127continue;3128if (!(s->flags & SHF_ALLOC) && s->name == ".debug_names") {3129s->markDead();3130inputSections.push_back(isec);3131files.insert(isec->file);3132}3133}31343135// Parse input .debug_names sections and extract InputChunk and OutputChunk3136// data. OutputChunk contains CU information, which will be needed by3137// `writeTo`.3138auto inputChunksPtr = std::make_unique<InputChunk[]>(files.size());3139MutableArrayRef<InputChunk> inputChunks(inputChunksPtr.get(), files.size());3140numChunks = files.size();3141chunks = std::make_unique<OutputChunk[]>(files.size());3142{3143TimeTraceScope timeScope("Merge .debug_names", "parse");3144parallelFor(0, files.size(), [&](size_t i) {3145parseFile(files[i], inputChunks[i], chunks[i]);3146});3147}31483149// Compute section header (except unit_length), abbrev table, and entry pool.3150computeHdrAndAbbrevTable(inputChunks);3151uint32_t entryPoolSize;3152std::tie(entryPoolSize, hdr.NameCount) = computeEntryPool(inputChunks);3153hdr.BucketCount = dwarf::getDebugNamesBucketCount(hdr.NameCount);31543155// Compute the section size. Subtract 4 to get the unit_length for DWARF32.3156uint32_t hdrSize = getDebugNamesHeaderSize(hdr.AugmentationStringSize);3157size = findDebugNamesOffsets(hdrSize, hdr).EntriesBase + entryPoolSize;3158hdr.UnitLength = size - 4;3159}31603161template <class ELFT> DebugNamesSection<ELFT>::DebugNamesSection() {3162init([](InputFile *f, InputChunk &inputChunk, OutputChunk &chunk) {3163auto *file = cast<ObjFile<ELFT>>(f);3164DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));3165auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());3166chunk.infoSec = dobj.getInfoSection();3167DWARFDataExtractor namesExtractor(dobj, dobj.getNamesSection(),3168ELFT::Endianness == endianness::little,3169ELFT::Is64Bits ? 8 : 4);3170// .debug_str is needed to get symbol names from string offsets.3171DataExtractor strExtractor(dobj.getStrSection(),3172ELFT::Endianness == endianness::little,3173ELFT::Is64Bits ? 8 : 4);3174inputChunk.section = dobj.getNamesSection();31753176inputChunk.llvmDebugNames.emplace(namesExtractor, strExtractor);3177if (Error e = inputChunk.llvmDebugNames->extract()) {3178errorOrWarn(toString(dobj.getNamesSection().sec) + Twine(": ") +3179toString(std::move(e)));3180}3181parseDebugNames(3182inputChunk, chunk, namesExtractor, strExtractor,3183[&chunk, namesData = dobj.getNamesSection().Data.data()](3184uint32_t numCus, const DWARFDebugNames::Header &hdr,3185const DWARFDebugNames::DWARFDebugNamesOffsets &locs) {3186// Read CU offsets, which are relocated by .debug_info + X3187// relocations. Record the section offset to be relocated by3188// `finalizeContents`.3189chunk.compUnits.resize_for_overwrite(numCus + hdr.CompUnitCount);3190for (auto i : seq(hdr.CompUnitCount))3191chunk.compUnits[numCus + i] = locs.CUsBase + i * 4;31923193// Read entry offsets.3194const char *p = namesData + locs.EntryOffsetsBase;3195SmallVector<uint32_t, 0> entryOffsets;3196entryOffsets.resize_for_overwrite(hdr.NameCount);3197for (uint32_t &offset : entryOffsets)3198offset = endian::readNext<uint32_t, ELFT::Endianness, unaligned>(p);3199return entryOffsets;3200});3201});3202}32033204template <class ELFT>3205template <class RelTy>3206void DebugNamesSection<ELFT>::getNameRelocs(3207const InputFile &file, DenseMap<uint32_t, uint32_t> &relocs,3208Relocs<RelTy> rels) {3209for (const RelTy &rel : rels) {3210Symbol &sym = file.getRelocTargetSym(rel);3211relocs[rel.r_offset] = sym.getVA(getAddend<ELFT>(rel));3212}3213}32143215template <class ELFT> void DebugNamesSection<ELFT>::finalizeContents() {3216// Get relocations of .debug_names sections.3217auto relocs = std::make_unique<DenseMap<uint32_t, uint32_t>[]>(numChunks);3218parallelFor(0, numChunks, [&](size_t i) {3219InputSection *sec = inputSections[i];3220invokeOnRelocs(*sec, getNameRelocs, *sec->file, relocs.get()[i]);32213222// Relocate CU offsets with .debug_info + X relocations.3223OutputChunk &chunk = chunks.get()[i];3224for (auto [j, cuOffset] : enumerate(chunk.compUnits))3225cuOffset = relocs.get()[i].lookup(cuOffset);3226});32273228// Relocate string offsets in the name table with .debug_str + X relocations.3229parallelForEach(nameVecs, [&](auto &nameVec) {3230for (NameEntry &ne : nameVec)3231ne.stringOffset = relocs.get()[ne.chunkIdx].lookup(ne.stringOffset);3232});3233}32343235template <class ELFT> void DebugNamesSection<ELFT>::writeTo(uint8_t *buf) {3236[[maybe_unused]] const uint8_t *const beginBuf = buf;3237// Write the header.3238endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.UnitLength);3239endian::writeNext<uint16_t, ELFT::Endianness>(buf, hdr.Version);3240buf += 2; // padding3241endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.CompUnitCount);3242endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.LocalTypeUnitCount);3243endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.ForeignTypeUnitCount);3244endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.BucketCount);3245endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.NameCount);3246endian::writeNext<uint32_t, ELFT::Endianness>(buf, hdr.AbbrevTableSize);3247endian::writeNext<uint32_t, ELFT::Endianness>(buf,3248hdr.AugmentationStringSize);3249memcpy(buf, hdr.AugmentationString.c_str(), hdr.AugmentationString.size());3250buf += hdr.AugmentationStringSize;32513252// Write the CU list.3253for (auto &chunk : getChunks())3254for (uint32_t cuOffset : chunk.compUnits)3255endian::writeNext<uint32_t, ELFT::Endianness>(buf, cuOffset);32563257// TODO: Write the local TU list, then the foreign TU list..32583259// Write the hash lookup table.3260SmallVector<SmallVector<NameEntry *, 0>, 0> buckets(hdr.BucketCount);3261// Symbols enter into a bucket whose index is the hash modulo bucket_count.3262for (auto &nameVec : nameVecs)3263for (NameEntry &ne : nameVec)3264buckets[ne.hashValue % hdr.BucketCount].push_back(&ne);32653266// Write buckets (accumulated bucket counts).3267uint32_t bucketIdx = 1;3268for (const SmallVector<NameEntry *, 0> &bucket : buckets) {3269if (!bucket.empty())3270endian::write32<ELFT::Endianness>(buf, bucketIdx);3271buf += 4;3272bucketIdx += bucket.size();3273}3274// Write the hashes.3275for (const SmallVector<NameEntry *, 0> &bucket : buckets)3276for (const NameEntry *e : bucket)3277endian::writeNext<uint32_t, ELFT::Endianness>(buf, e->hashValue);32783279// Write the name table. The name entries are ordered by bucket_idx and3280// correspond one-to-one with the hash lookup table.3281//3282// First, write the relocated string offsets.3283for (const SmallVector<NameEntry *, 0> &bucket : buckets)3284for (const NameEntry *ne : bucket)3285endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->stringOffset);32863287// Then write the entry offsets.3288for (const SmallVector<NameEntry *, 0> &bucket : buckets)3289for (const NameEntry *ne : bucket)3290endian::writeNext<uint32_t, ELFT::Endianness>(buf, ne->entryOffset);32913292// Write the abbrev table.3293buf = llvm::copy(abbrevTableBuf, buf);32943295// Write the entry pool. Unlike the name table, the name entries follow the3296// nameVecs order computed by `computeEntryPool`.3297for (auto &nameVec : nameVecs) {3298for (NameEntry &ne : nameVec) {3299// Write all the entries for the string.3300for (const IndexEntry &ie : ne.entries()) {3301buf += encodeULEB128(ie.abbrevCode, buf);3302for (AttrValue value : ie.attrValues) {3303switch (value.attrSize) {3304case 1:3305*buf++ = value.attrValue;3306break;3307case 2:3308endian::writeNext<uint16_t, ELFT::Endianness>(buf, value.attrValue);3309break;3310case 4:3311endian::writeNext<uint32_t, ELFT::Endianness>(buf, value.attrValue);3312break;3313default:3314llvm_unreachable("invalid attrSize");3315}3316}3317}3318++buf; // index entry sentinel3319}3320}3321assert(uint64_t(buf - beginBuf) == size);3322}33233324GdbIndexSection::GdbIndexSection()3325: SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {}33263327// Returns the desired size of an on-disk hash table for a .gdb_index section.3328// There's a tradeoff between size and collision rate. We aim 75% utilization.3329size_t GdbIndexSection::computeSymtabSize() const {3330return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024);3331}33323333static SmallVector<GdbIndexSection::CuEntry, 0>3334readCuList(DWARFContext &dwarf) {3335SmallVector<GdbIndexSection::CuEntry, 0> ret;3336for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())3337ret.push_back({cu->getOffset(), cu->getLength() + 4});3338return ret;3339}33403341static SmallVector<GdbIndexSection::AddressEntry, 0>3342readAddressAreas(DWARFContext &dwarf, InputSection *sec) {3343SmallVector<GdbIndexSection::AddressEntry, 0> ret;33443345uint32_t cuIdx = 0;3346for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {3347if (Error e = cu->tryExtractDIEsIfNeeded(false)) {3348warn(toString(sec) + ": " + toString(std::move(e)));3349return {};3350}3351Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges();3352if (!ranges) {3353warn(toString(sec) + ": " + toString(ranges.takeError()));3354return {};3355}33563357ArrayRef<InputSectionBase *> sections = sec->file->getSections();3358for (DWARFAddressRange &r : *ranges) {3359if (r.SectionIndex == -1ULL)3360continue;3361// Range list with zero size has no effect.3362InputSectionBase *s = sections[r.SectionIndex];3363if (s && s != &InputSection::discarded && s->isLive())3364if (r.LowPC != r.HighPC)3365ret.push_back({cast<InputSection>(s), r.LowPC, r.HighPC, cuIdx});3366}3367++cuIdx;3368}33693370return ret;3371}33723373template <class ELFT>3374static SmallVector<GdbIndexSection::NameAttrEntry, 0>3375readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj,3376const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) {3377const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection();3378const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection();33793380SmallVector<GdbIndexSection::NameAttrEntry, 0> ret;3381for (const LLDDWARFSection *pub : {&pubNames, &pubTypes}) {3382DWARFDataExtractor data(obj, *pub, ELFT::Endianness == endianness::little,3383ELFT::Is64Bits ? 8 : 4);3384DWARFDebugPubTable table;3385table.extract(data, /*GnuStyle=*/true, [&](Error e) {3386warn(toString(pub->sec) + ": " + toString(std::move(e)));3387});3388for (const DWARFDebugPubTable::Set &set : table.getData()) {3389// The value written into the constant pool is kind << 24 | cuIndex. As we3390// don't know how many compilation units precede this object to compute3391// cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add3392// the number of preceding compilation units later.3393uint32_t i = llvm::partition_point(cus,3394[&](GdbIndexSection::CuEntry cu) {3395return cu.cuOffset < set.Offset;3396}) -3397cus.begin();3398for (const DWARFDebugPubTable::Entry &ent : set.Entries)3399ret.push_back({{ent.Name, computeGdbHash(ent.Name)},3400(ent.Descriptor.toBits() << 24) | i});3401}3402}3403return ret;3404}34053406// Create a list of symbols from a given list of symbol names and types3407// by uniquifying them by name.3408static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t>3409createSymbols(3410ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs,3411const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) {3412using GdbSymbol = GdbIndexSection::GdbSymbol;3413using NameAttrEntry = GdbIndexSection::NameAttrEntry;34143415// For each chunk, compute the number of compilation units preceding it.3416uint32_t cuIdx = 0;3417std::unique_ptr<uint32_t[]> cuIdxs(new uint32_t[chunks.size()]);3418for (uint32_t i = 0, e = chunks.size(); i != e; ++i) {3419cuIdxs[i] = cuIdx;3420cuIdx += chunks[i].compilationUnits.size();3421}34223423// Collect the compilation unitss for each unique name. Speed it up using3424// multi-threading as the number of symbols can be in the order of millions.3425// Shard GdbSymbols by hash's high bits.3426constexpr size_t numShards = 32;3427const size_t concurrency =3428llvm::bit_floor(std::min<size_t>(config->threadCount, numShards));3429const size_t shift = 32 - llvm::countr_zero(numShards);3430auto map =3431std::make_unique<DenseMap<CachedHashStringRef, size_t>[]>(numShards);3432auto symbols = std::make_unique<SmallVector<GdbSymbol, 0>[]>(numShards);3433parallelFor(0, concurrency, [&](size_t threadId) {3434uint32_t i = 0;3435for (ArrayRef<NameAttrEntry> entries : nameAttrs) {3436for (const NameAttrEntry &ent : entries) {3437size_t shardId = ent.name.hash() >> shift;3438if ((shardId & (concurrency - 1)) != threadId)3439continue;34403441uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i];3442auto [it, inserted] =3443map[shardId].try_emplace(ent.name, symbols[shardId].size());3444if (inserted)3445symbols[shardId].push_back({ent.name, {v}, 0, 0});3446else3447symbols[shardId][it->second].cuVector.push_back(v);3448}3449++i;3450}3451});34523453size_t numSymbols = 0;3454for (ArrayRef<GdbSymbol> v : ArrayRef(symbols.get(), numShards))3455numSymbols += v.size();34563457// The return type is a flattened vector, so we'll copy each vector3458// contents to Ret.3459SmallVector<GdbSymbol, 0> ret;3460ret.reserve(numSymbols);3461for (SmallVector<GdbSymbol, 0> &vec :3462MutableArrayRef(symbols.get(), numShards))3463for (GdbSymbol &sym : vec)3464ret.push_back(std::move(sym));34653466// CU vectors and symbol names are adjacent in the output file.3467// We can compute their offsets in the output file now.3468size_t off = 0;3469for (GdbSymbol &sym : ret) {3470sym.cuVectorOff = off;3471off += (sym.cuVector.size() + 1) * 4;3472}3473for (GdbSymbol &sym : ret) {3474sym.nameOff = off;3475off += sym.name.size() + 1;3476}3477// If off overflows, the last symbol's nameOff likely overflows.3478if (!isUInt<32>(off))3479errorOrWarn("--gdb-index: constant pool size (" + Twine(off) +3480") exceeds UINT32_MAX");34813482return {ret, off};3483}34843485// Returns a newly-created .gdb_index section.3486template <class ELFT>3487std::unique_ptr<GdbIndexSection> GdbIndexSection::create() {3488llvm::TimeTraceScope timeScope("Create gdb index");34893490// Collect InputFiles with .debug_info. See the comment in3491// LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future,3492// note that isec->data() may uncompress the full content, which should be3493// parallelized.3494SetVector<InputFile *> files;3495for (InputSectionBase *s : ctx.inputSections) {3496InputSection *isec = dyn_cast<InputSection>(s);3497if (!isec)3498continue;3499// .debug_gnu_pub{names,types} are useless in executables.3500// They are present in input object files solely for creating3501// a .gdb_index. So we can remove them from the output.3502if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")3503s->markDead();3504else if (isec->name == ".debug_info")3505files.insert(isec->file);3506}3507// Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs.3508llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {3509if (auto *isec = dyn_cast<InputSection>(s))3510if (InputSectionBase *rel = isec->getRelocatedSection())3511return !rel->isLive();3512return !s->isLive();3513});35143515SmallVector<GdbChunk, 0> chunks(files.size());3516SmallVector<SmallVector<NameAttrEntry, 0>, 0> nameAttrs(files.size());35173518parallelFor(0, files.size(), [&](size_t i) {3519// To keep memory usage low, we don't want to keep cached DWARFContext, so3520// avoid getDwarf() here.3521ObjFile<ELFT> *file = cast<ObjFile<ELFT>>(files[i]);3522DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));3523auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj());35243525// If the are multiple compile units .debug_info (very rare ld -r --unique),3526// this only picks the last one. Other address ranges are lost.3527chunks[i].sec = dobj.getInfoSection();3528chunks[i].compilationUnits = readCuList(dwarf);3529chunks[i].addressAreas = readAddressAreas(dwarf, chunks[i].sec);3530nameAttrs[i] = readPubNamesAndTypes<ELFT>(dobj, chunks[i].compilationUnits);3531});35323533auto ret = std::make_unique<GdbIndexSection>();3534ret->chunks = std::move(chunks);3535std::tie(ret->symbols, ret->size) = createSymbols(nameAttrs, ret->chunks);35363537// Count the areas other than the constant pool.3538ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8;3539for (GdbChunk &chunk : ret->chunks)3540ret->size +=3541chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20;35423543return ret;3544}35453546void GdbIndexSection::writeTo(uint8_t *buf) {3547// Write the header.3548auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf);3549uint8_t *start = buf;3550hdr->version = 7;3551buf += sizeof(*hdr);35523553// Write the CU list.3554hdr->cuListOff = buf - start;3555for (GdbChunk &chunk : chunks) {3556for (CuEntry &cu : chunk.compilationUnits) {3557write64le(buf, chunk.sec->outSecOff + cu.cuOffset);3558write64le(buf + 8, cu.cuLength);3559buf += 16;3560}3561}35623563// Write the address area.3564hdr->cuTypesOff = buf - start;3565hdr->addressAreaOff = buf - start;3566uint32_t cuOff = 0;3567for (GdbChunk &chunk : chunks) {3568for (AddressEntry &e : chunk.addressAreas) {3569// In the case of ICF there may be duplicate address range entries.3570const uint64_t baseAddr = e.section->repl->getVA(0);3571write64le(buf, baseAddr + e.lowAddress);3572write64le(buf + 8, baseAddr + e.highAddress);3573write32le(buf + 16, e.cuIndex + cuOff);3574buf += 20;3575}3576cuOff += chunk.compilationUnits.size();3577}35783579// Write the on-disk open-addressing hash table containing symbols.3580hdr->symtabOff = buf - start;3581size_t symtabSize = computeSymtabSize();3582uint32_t mask = symtabSize - 1;35833584for (GdbSymbol &sym : symbols) {3585uint32_t h = sym.name.hash();3586uint32_t i = h & mask;3587uint32_t step = ((h * 17) & mask) | 1;35883589while (read32le(buf + i * 8))3590i = (i + step) & mask;35913592write32le(buf + i * 8, sym.nameOff);3593write32le(buf + i * 8 + 4, sym.cuVectorOff);3594}35953596buf += symtabSize * 8;35973598// Write the string pool.3599hdr->constantPoolOff = buf - start;3600parallelForEach(symbols, [&](GdbSymbol &sym) {3601memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size());3602});36033604// Write the CU vectors.3605for (GdbSymbol &sym : symbols) {3606write32le(buf, sym.cuVector.size());3607buf += 4;3608for (uint32_t val : sym.cuVector) {3609write32le(buf, val);3610buf += 4;3611}3612}3613}36143615bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }36163617EhFrameHeader::EhFrameHeader()3618: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {}36193620void EhFrameHeader::writeTo(uint8_t *buf) {3621// Unlike most sections, the EhFrameHeader section is written while writing3622// another section, namely EhFrameSection, which calls the write() function3623// below from its writeTo() function. This is necessary because the contents3624// of EhFrameHeader depend on the relocated contents of EhFrameSection and we3625// don't know which order the sections will be written in.3626}36273628// .eh_frame_hdr contains a binary search table of pointers to FDEs.3629// Each entry of the search table consists of two values,3630// the starting PC from where FDEs covers, and the FDE's address.3631// It is sorted by PC.3632void EhFrameHeader::write() {3633uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;3634using FdeData = EhFrameSection::FdeData;3635SmallVector<FdeData, 0> fdes = getPartition().ehFrame->getFdeData();36363637buf[0] = 1;3638buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;3639buf[2] = DW_EH_PE_udata4;3640buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;3641write32(buf + 4,3642getPartition().ehFrame->getParent()->addr - this->getVA() - 4);3643write32(buf + 8, fdes.size());3644buf += 12;36453646for (FdeData &fde : fdes) {3647write32(buf, fde.pcRel);3648write32(buf + 4, fde.fdeVARel);3649buf += 8;3650}3651}36523653size_t EhFrameHeader::getSize() const {3654// .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.3655return 12 + getPartition().ehFrame->numFdes * 8;3656}36573658bool EhFrameHeader::isNeeded() const {3659return isLive() && getPartition().ehFrame->isNeeded();3660}36613662VersionDefinitionSection::VersionDefinitionSection()3663: SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),3664".gnu.version_d") {}36653666StringRef VersionDefinitionSection::getFileDefName() {3667if (!getPartition().name.empty())3668return getPartition().name;3669if (!config->soName.empty())3670return config->soName;3671return config->outputFile;3672}36733674void VersionDefinitionSection::finalizeContents() {3675fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName());3676for (const VersionDefinition &v : namedVersionDefs())3677verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name));36783679if (OutputSection *sec = getPartition().dynStrTab->getParent())3680getParent()->link = sec->sectionIndex;36813682// sh_info should be set to the number of definitions. This fact is missed in3683// documentation, but confirmed by binutils community:3684// https://sourceware.org/ml/binutils/2014-11/msg00355.html3685getParent()->info = getVerDefNum();3686}36873688void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index,3689StringRef name, size_t nameOff) {3690uint16_t flags = index == 1 ? VER_FLG_BASE : 0;36913692// Write a verdef.3693write16(buf, 1); // vd_version3694write16(buf + 2, flags); // vd_flags3695write16(buf + 4, index); // vd_ndx3696write16(buf + 6, 1); // vd_cnt3697write32(buf + 8, hashSysV(name)); // vd_hash3698write32(buf + 12, 20); // vd_aux3699write32(buf + 16, 28); // vd_next37003701// Write a veraux.3702write32(buf + 20, nameOff); // vda_name3703write32(buf + 24, 0); // vda_next3704}37053706void VersionDefinitionSection::writeTo(uint8_t *buf) {3707writeOne(buf, 1, getFileDefName(), fileDefNameOff);37083709auto nameOffIt = verDefNameOffs.begin();3710for (const VersionDefinition &v : namedVersionDefs()) {3711buf += EntrySize;3712writeOne(buf, v.id, v.name, *nameOffIt++);3713}37143715// Need to terminate the last version definition.3716write32(buf + 16, 0); // vd_next3717}37183719size_t VersionDefinitionSection::getSize() const {3720return EntrySize * getVerDefNum();3721}37223723// .gnu.version is a table where each entry is 2 byte long.3724VersionTableSection::VersionTableSection()3725: SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),3726".gnu.version") {3727this->entsize = 2;3728}37293730void VersionTableSection::finalizeContents() {3731// At the moment of june 2016 GNU docs does not mention that sh_link field3732// should be set, but Sun docs do. Also readelf relies on this field.3733getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex;3734}37353736size_t VersionTableSection::getSize() const {3737return (getPartition().dynSymTab->getSymbols().size() + 1) * 2;3738}37393740void VersionTableSection::writeTo(uint8_t *buf) {3741buf += 2;3742for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) {3743// For an unextracted lazy symbol (undefined weak), it must have been3744// converted to Undefined and have VER_NDX_GLOBAL version here.3745assert(!s.sym->isLazy());3746write16(buf, s.sym->versionId);3747buf += 2;3748}3749}37503751bool VersionTableSection::isNeeded() const {3752return isLive() &&3753(getPartition().verDef || getPartition().verNeed->isNeeded());3754}37553756void elf::addVerneed(Symbol *ss) {3757auto &file = cast<SharedFile>(*ss->file);3758if (ss->versionId == VER_NDX_GLOBAL)3759return;37603761if (file.vernauxs.empty())3762file.vernauxs.resize(file.verdefs.size());37633764// Select a version identifier for the vernaux data structure, if we haven't3765// already allocated one. The verdef identifiers cover the range3766// [1..getVerDefNum()]; this causes the vernaux identifiers to start from3767// getVerDefNum()+1.3768if (file.vernauxs[ss->versionId] == 0)3769file.vernauxs[ss->versionId] = ++SharedFile::vernauxNum + getVerDefNum();37703771ss->versionId = file.vernauxs[ss->versionId];3772}37733774template <class ELFT>3775VersionNeedSection<ELFT>::VersionNeedSection()3776: SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),3777".gnu.version_r") {}37783779template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {3780for (SharedFile *f : ctx.sharedFiles) {3781if (f->vernauxs.empty())3782continue;3783verneeds.emplace_back();3784Verneed &vn = verneeds.back();3785vn.nameStrTab = getPartition().dynStrTab->addString(f->soName);3786bool isLibc = config->relrGlibc && f->soName.starts_with("libc.so.");3787bool isGlibc2 = false;3788for (unsigned i = 0; i != f->vernauxs.size(); ++i) {3789if (f->vernauxs[i] == 0)3790continue;3791auto *verdef =3792reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]);3793StringRef ver(f->getStringTable().data() + verdef->getAux()->vda_name);3794if (isLibc && ver.starts_with("GLIBC_2."))3795isGlibc2 = true;3796vn.vernauxs.push_back({verdef->vd_hash, f->vernauxs[i],3797getPartition().dynStrTab->addString(ver)});3798}3799if (isGlibc2) {3800const char *ver = "GLIBC_ABI_DT_RELR";3801vn.vernauxs.push_back({hashSysV(ver),3802++SharedFile::vernauxNum + getVerDefNum(),3803getPartition().dynStrTab->addString(ver)});3804}3805}38063807if (OutputSection *sec = getPartition().dynStrTab->getParent())3808getParent()->link = sec->sectionIndex;3809getParent()->info = verneeds.size();3810}38113812template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) {3813// The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.3814auto *verneed = reinterpret_cast<Elf_Verneed *>(buf);3815auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size());38163817for (auto &vn : verneeds) {3818// Create an Elf_Verneed for this DSO.3819verneed->vn_version = 1;3820verneed->vn_cnt = vn.vernauxs.size();3821verneed->vn_file = vn.nameStrTab;3822verneed->vn_aux =3823reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed);3824verneed->vn_next = sizeof(Elf_Verneed);3825++verneed;38263827// Create the Elf_Vernauxs for this Elf_Verneed.3828for (auto &vna : vn.vernauxs) {3829vernaux->vna_hash = vna.hash;3830vernaux->vna_flags = 0;3831vernaux->vna_other = vna.verneedIndex;3832vernaux->vna_name = vna.nameStrTab;3833vernaux->vna_next = sizeof(Elf_Vernaux);3834++vernaux;3835}38363837vernaux[-1].vna_next = 0;3838}3839verneed[-1].vn_next = 0;3840}38413842template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {3843return verneeds.size() * sizeof(Elf_Verneed) +3844SharedFile::vernauxNum * sizeof(Elf_Vernaux);3845}38463847template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {3848return isLive() && SharedFile::vernauxNum != 0;3849}38503851void MergeSyntheticSection::addSection(MergeInputSection *ms) {3852ms->parent = this;3853sections.push_back(ms);3854assert(addralign == ms->addralign || !(ms->flags & SHF_STRINGS));3855addralign = std::max(addralign, ms->addralign);3856}38573858MergeTailSection::MergeTailSection(StringRef name, uint32_t type,3859uint64_t flags, uint32_t alignment)3860: MergeSyntheticSection(name, type, flags, alignment),3861builder(StringTableBuilder::RAW, llvm::Align(alignment)) {}38623863size_t MergeTailSection::getSize() const { return builder.getSize(); }38643865void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); }38663867void MergeTailSection::finalizeContents() {3868// Add all string pieces to the string table builder to create section3869// contents.3870for (MergeInputSection *sec : sections)3871for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)3872if (sec->pieces[i].live)3873builder.add(sec->getData(i));38743875// Fix the string table content. After this, the contents will never change.3876builder.finalize();38773878// finalize() fixed tail-optimized strings, so we can now get3879// offsets of strings. Get an offset for each string and save it3880// to a corresponding SectionPiece for easy access.3881for (MergeInputSection *sec : sections)3882for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)3883if (sec->pieces[i].live)3884sec->pieces[i].outputOff = builder.getOffset(sec->getData(i));3885}38863887void MergeNoTailSection::writeTo(uint8_t *buf) {3888parallelFor(0, numShards,3889[&](size_t i) { shards[i].write(buf + shardOffsets[i]); });3890}38913892// This function is very hot (i.e. it can take several seconds to finish)3893// because sometimes the number of inputs is in an order of magnitude of3894// millions. So, we use multi-threading.3895//3896// For any strings S and T, we know S is not mergeable with T if S's hash3897// value is different from T's. If that's the case, we can safely put S and3898// T into different string builders without worrying about merge misses.3899// We do it in parallel.3900void MergeNoTailSection::finalizeContents() {3901// Initializes string table builders.3902for (size_t i = 0; i < numShards; ++i)3903shards.emplace_back(StringTableBuilder::RAW, llvm::Align(addralign));39043905// Concurrency level. Must be a power of 2 to avoid expensive modulo3906// operations in the following tight loop.3907const size_t concurrency =3908llvm::bit_floor(std::min<size_t>(config->threadCount, numShards));39093910// Add section pieces to the builders.3911parallelFor(0, concurrency, [&](size_t threadId) {3912for (MergeInputSection *sec : sections) {3913for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) {3914if (!sec->pieces[i].live)3915continue;3916size_t shardId = getShardId(sec->pieces[i].hash);3917if ((shardId & (concurrency - 1)) == threadId)3918sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i));3919}3920}3921});39223923// Compute an in-section offset for each shard.3924size_t off = 0;3925for (size_t i = 0; i < numShards; ++i) {3926shards[i].finalizeInOrder();3927if (shards[i].getSize() > 0)3928off = alignToPowerOf2(off, addralign);3929shardOffsets[i] = off;3930off += shards[i].getSize();3931}3932size = off;39333934// So far, section pieces have offsets from beginning of shards, but3935// we want offsets from beginning of the whole section. Fix them.3936parallelForEach(sections, [&](MergeInputSection *sec) {3937for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)3938if (sec->pieces[i].live)3939sec->pieces[i].outputOff +=3940shardOffsets[getShardId(sec->pieces[i].hash)];3941});3942}39433944template <class ELFT> void elf::splitSections() {3945llvm::TimeTraceScope timeScope("Split sections");3946// splitIntoPieces needs to be called on each MergeInputSection3947// before calling finalizeContents().3948parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {3949for (InputSectionBase *sec : file->getSections()) {3950if (!sec)3951continue;3952if (auto *s = dyn_cast<MergeInputSection>(sec))3953s->splitIntoPieces();3954else if (auto *eh = dyn_cast<EhInputSection>(sec))3955eh->split<ELFT>();3956}3957});3958}39593960void elf::combineEhSections() {3961llvm::TimeTraceScope timeScope("Combine EH sections");3962for (EhInputSection *sec : ctx.ehInputSections) {3963EhFrameSection &eh = *sec->getPartition().ehFrame;3964sec->parent = &eh;3965eh.addralign = std::max(eh.addralign, sec->addralign);3966eh.sections.push_back(sec);3967llvm::append_range(eh.dependentSections, sec->dependentSections);3968}39693970if (!mainPart->armExidx)3971return;3972llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {3973// Ignore dead sections and the partition end marker (.part.end),3974// whose partition number is out of bounds.3975if (!s->isLive() || s->partition == 255)3976return false;3977Partition &part = s->getPartition();3978return s->kind() == SectionBase::Regular && part.armExidx &&3979part.armExidx->addSection(cast<InputSection>(s));3980});3981}39823983MipsRldMapSection::MipsRldMapSection()3984: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,3985".rld_map") {}39863987ARMExidxSyntheticSection::ARMExidxSyntheticSection()3988: SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,3989config->wordsize, ".ARM.exidx") {}39903991static InputSection *findExidxSection(InputSection *isec) {3992for (InputSection *d : isec->dependentSections)3993if (d->type == SHT_ARM_EXIDX && d->isLive())3994return d;3995return nullptr;3996}39973998static bool isValidExidxSectionDep(InputSection *isec) {3999return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) &&4000isec->getSize() > 0;4001}40024003bool ARMExidxSyntheticSection::addSection(InputSection *isec) {4004if (isec->type == SHT_ARM_EXIDX) {4005if (InputSection *dep = isec->getLinkOrderDep())4006if (isValidExidxSectionDep(dep)) {4007exidxSections.push_back(isec);4008// Every exidxSection is 8 bytes, we need an estimate of4009// size before assignAddresses can be called. Final size4010// will only be known after finalize is called.4011size += 8;4012}4013return true;4014}40154016if (isValidExidxSectionDep(isec)) {4017executableSections.push_back(isec);4018return false;4019}40204021// FIXME: we do not output a relocation section when --emit-relocs is used4022// as we do not have relocation sections for linker generated table entries4023// and we would have to erase at a late stage relocations from merged entries.4024// Given that exception tables are already position independent and a binary4025// analyzer could derive the relocations we choose to erase the relocations.4026if (config->emitRelocs && isec->type == SHT_REL)4027if (InputSectionBase *ex = isec->getRelocatedSection())4028if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX)4029return true;40304031return false;4032}40334034// References to .ARM.Extab Sections have bit 31 clear and are not the4035// special EXIDX_CANTUNWIND bit-pattern.4036static bool isExtabRef(uint32_t unwind) {4037return (unwind & 0x80000000) == 0 && unwind != 0x1;4038}40394040// Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx4041// section Prev, where Cur follows Prev in the table. This can be done if the4042// unwinding instructions in Cur are identical to Prev. Linker generated4043// EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an4044// InputSection.4045static bool isDuplicateArmExidxSec(InputSection *prev, InputSection *cur) {4046// Get the last table Entry from the previous .ARM.exidx section. If Prev is4047// nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.4048uint32_t prevUnwind = 1;4049if (prev)4050prevUnwind = read32(prev->content().data() + prev->content().size() - 4);4051if (isExtabRef(prevUnwind))4052return false;40534054// We consider the unwind instructions of an .ARM.exidx table entry4055// a duplicate if the previous unwind instructions if:4056// - Both are the special EXIDX_CANTUNWIND.4057// - Both are the same inline unwind instructions.4058// We do not attempt to follow and check links into .ARM.extab tables as4059// consecutive identical entries are rare and the effort to check that they4060// are identical is high.40614062// If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.4063if (cur == nullptr)4064return prevUnwind == 1;40654066for (uint32_t offset = 4; offset < (uint32_t)cur->content().size(); offset +=8) {4067uint32_t curUnwind = read32(cur->content().data() + offset);4068if (isExtabRef(curUnwind) || curUnwind != prevUnwind)4069return false;4070}4071// All table entries in this .ARM.exidx Section can be merged into the4072// previous Section.4073return true;4074}40754076// The .ARM.exidx table must be sorted in ascending order of the address of the4077// functions the table describes. std::optionally duplicate adjacent table4078// entries can be removed. At the end of the function the executableSections4079// must be sorted in ascending order of address, Sentinel is set to the4080// InputSection with the highest address and any InputSections that have4081// mergeable .ARM.exidx table entries are removed from it.4082void ARMExidxSyntheticSection::finalizeContents() {4083// Ensure that any fixed-point iterations after the first see the original set4084// of sections.4085if (!originalExecutableSections.empty())4086executableSections = originalExecutableSections;4087else if (config->enableNonContiguousRegions)4088originalExecutableSections = executableSections;40894090// The executableSections and exidxSections that we use to derive the final4091// contents of this SyntheticSection are populated before4092// processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or4093// ICF may remove executable InputSections and their dependent .ARM.exidx4094// section that we recorded earlier.4095auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); };4096llvm::erase_if(exidxSections, isDiscarded);4097// We need to remove discarded InputSections and InputSections without4098// .ARM.exidx sections that if we generated the .ARM.exidx it would be out4099// of range.4100auto isDiscardedOrOutOfRange = [this](InputSection *isec) {4101if (!isec->isLive())4102return true;4103if (findExidxSection(isec))4104return false;4105int64_t off = static_cast<int64_t>(isec->getVA() - getVA());4106return off != llvm::SignExtend64(off, 31);4107};4108llvm::erase_if(executableSections, isDiscardedOrOutOfRange);41094110// Sort the executable sections that may or may not have associated4111// .ARM.exidx sections by order of ascending address. This requires the4112// relative positions of InputSections and OutputSections to be known.4113auto compareByFilePosition = [](const InputSection *a,4114const InputSection *b) {4115OutputSection *aOut = a->getParent();4116OutputSection *bOut = b->getParent();41174118if (aOut != bOut)4119return aOut->addr < bOut->addr;4120return a->outSecOff < b->outSecOff;4121};4122llvm::stable_sort(executableSections, compareByFilePosition);4123sentinel = executableSections.back();4124// std::optionally merge adjacent duplicate entries.4125if (config->mergeArmExidx) {4126SmallVector<InputSection *, 0> selectedSections;4127selectedSections.reserve(executableSections.size());4128selectedSections.push_back(executableSections[0]);4129size_t prev = 0;4130for (size_t i = 1; i < executableSections.size(); ++i) {4131InputSection *ex1 = findExidxSection(executableSections[prev]);4132InputSection *ex2 = findExidxSection(executableSections[i]);4133if (!isDuplicateArmExidxSec(ex1, ex2)) {4134selectedSections.push_back(executableSections[i]);4135prev = i;4136}4137}4138executableSections = std::move(selectedSections);4139}4140// offset is within the SyntheticSection.4141size_t offset = 0;4142size = 0;4143for (InputSection *isec : executableSections) {4144if (InputSection *d = findExidxSection(isec)) {4145d->outSecOff = offset;4146d->parent = getParent();4147offset += d->getSize();4148} else {4149offset += 8;4150}4151}4152// Size includes Sentinel.4153size = offset + 8;4154}41554156InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const {4157return executableSections.front();4158}41594160// To write the .ARM.exidx table from the ExecutableSections we have three cases4161// 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.4162// We write the .ARM.exidx section contents and apply its relocations.4163// 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We4164// must write the contents of an EXIDX_CANTUNWIND directly. We use the4165// start of the InputSection as the purpose of the linker generated4166// section is to terminate the address range of the previous entry.4167// 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of4168// the table to terminate the address range of the final entry.4169void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {41704171// A linker generated CANTUNWIND entry is made up of two words:4172// 0x0 with R_ARM_PREL31 relocation to target.4173// 0x1 with EXIDX_CANTUNWIND.4174uint64_t offset = 0;4175for (InputSection *isec : executableSections) {4176assert(isec->getParent() != nullptr);4177if (InputSection *d = findExidxSection(isec)) {4178for (int dataOffset = 0; dataOffset != (int)d->content().size();4179dataOffset += 4)4180write32(buf + offset + dataOffset,4181read32(d->content().data() + dataOffset));4182// Recalculate outSecOff as finalizeAddressDependentContent()4183// may have altered syntheticSection outSecOff.4184d->outSecOff = offset + outSecOff;4185target->relocateAlloc(*d, buf + offset);4186offset += d->getSize();4187} else {4188// A Linker generated CANTUNWIND section.4189write32(buf + offset + 0, 0x0);4190write32(buf + offset + 4, 0x1);4191uint64_t s = isec->getVA();4192uint64_t p = getVA() + offset;4193target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p);4194offset += 8;4195}4196}4197// Write Sentinel CANTUNWIND entry.4198write32(buf + offset + 0, 0x0);4199write32(buf + offset + 4, 0x1);4200uint64_t s = sentinel->getVA(sentinel->getSize());4201uint64_t p = getVA() + offset;4202target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p);4203assert(size == offset + 8);4204}42054206bool ARMExidxSyntheticSection::isNeeded() const {4207return llvm::any_of(exidxSections,4208[](InputSection *isec) { return isec->isLive(); });4209}42104211ThunkSection::ThunkSection(OutputSection *os, uint64_t off)4212: SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS,4213config->emachine == EM_PPC64 ? 16 : 4, ".text.thunk") {4214this->parent = os;4215this->outSecOff = off;4216}42174218size_t ThunkSection::getSize() const {4219if (roundUpSizeForErrata)4220return alignTo(size, 4096);4221return size;4222}42234224void ThunkSection::addThunk(Thunk *t) {4225thunks.push_back(t);4226t->addSymbols(*this);4227}42284229void ThunkSection::writeTo(uint8_t *buf) {4230for (Thunk *t : thunks)4231t->writeTo(buf + t->offset);4232}42334234InputSection *ThunkSection::getTargetInputSection() const {4235if (thunks.empty())4236return nullptr;4237const Thunk *t = thunks.front();4238return t->getTargetInputSection();4239}42404241bool ThunkSection::assignOffsets() {4242uint64_t off = 0;4243for (Thunk *t : thunks) {4244off = alignToPowerOf2(off, t->alignment);4245t->setOffset(off);4246uint32_t size = t->size();4247t->getThunkTargetSym()->size = size;4248off += size;4249}4250bool changed = off != size;4251size = off;4252return changed;4253}42544255PPC32Got2Section::PPC32Got2Section()4256: SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {}42574258bool PPC32Got2Section::isNeeded() const {4259// See the comment below. This is not needed if there is no other4260// InputSection.4261for (SectionCommand *cmd : getParent()->commands)4262if (auto *isd = dyn_cast<InputSectionDescription>(cmd))4263for (InputSection *isec : isd->sections)4264if (isec != this)4265return true;4266return false;4267}42684269void PPC32Got2Section::finalizeContents() {4270// PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in4271// .got2 . This function computes outSecOff of each .got2 to be used in4272// PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is4273// to collect input sections named ".got2".4274for (SectionCommand *cmd : getParent()->commands)4275if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {4276for (InputSection *isec : isd->sections) {4277// isec->file may be nullptr for MergeSyntheticSection.4278if (isec != this && isec->file)4279isec->file->ppc32Got2 = isec;4280}4281}4282}42834284// If linking position-dependent code then the table will store the addresses4285// directly in the binary so the section has type SHT_PROGBITS. If linking4286// position-independent code the section has type SHT_NOBITS since it will be4287// allocated and filled in by the dynamic linker.4288PPC64LongBranchTargetSection::PPC64LongBranchTargetSection()4289: SyntheticSection(SHF_ALLOC | SHF_WRITE,4290config->isPic ? SHT_NOBITS : SHT_PROGBITS, 8,4291".branch_lt") {}42924293uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol *sym,4294int64_t addend) {4295return getVA() + entry_index.find({sym, addend})->second * 8;4296}42974298std::optional<uint32_t>4299PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) {4300auto res =4301entry_index.try_emplace(std::make_pair(sym, addend), entries.size());4302if (!res.second)4303return std::nullopt;4304entries.emplace_back(sym, addend);4305return res.first->second;4306}43074308size_t PPC64LongBranchTargetSection::getSize() const {4309return entries.size() * 8;4310}43114312void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) {4313// If linking non-pic we have the final addresses of the targets and they get4314// written to the table directly. For pic the dynamic linker will allocate4315// the section and fill it.4316if (config->isPic)4317return;43184319for (auto entry : entries) {4320const Symbol *sym = entry.first;4321int64_t addend = entry.second;4322assert(sym->getVA());4323// Need calls to branch to the local entry-point since a long-branch4324// must be a local-call.4325write64(buf, sym->getVA(addend) +4326getPPC64GlobalEntryToLocalEntryOffset(sym->stOther));4327buf += 8;4328}4329}43304331bool PPC64LongBranchTargetSection::isNeeded() const {4332// `removeUnusedSyntheticSections()` is called before thunk allocation which4333// is too early to determine if this section will be empty or not. We need4334// Finalized to keep the section alive until after thunk creation. Finalized4335// only gets set to true once `finalizeSections()` is called after thunk4336// creation. Because of this, if we don't create any long-branch thunks we end4337// up with an empty .branch_lt section in the binary.4338return !finalized || !entries.empty();4339}43404341static uint8_t getAbiVersion() {4342// MIPS non-PIC executable gets ABI version 1.4343if (config->emachine == EM_MIPS) {4344if (!config->isPic && !config->relocatable &&4345(config->eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)4346return 1;4347return 0;4348}43494350if (config->emachine == EM_AMDGPU && !ctx.objectFiles.empty()) {4351uint8_t ver = ctx.objectFiles[0]->abiVersion;4352for (InputFile *file : ArrayRef(ctx.objectFiles).slice(1))4353if (file->abiVersion != ver)4354error("incompatible ABI version: " + toString(file));4355return ver;4356}43574358return 0;4359}43604361template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) {4362memcpy(buf, "\177ELF", 4);43634364auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);4365eHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;4366eHdr->e_ident[EI_DATA] =4367ELFT::Endianness == endianness::little ? ELFDATA2LSB : ELFDATA2MSB;4368eHdr->e_ident[EI_VERSION] = EV_CURRENT;4369eHdr->e_ident[EI_OSABI] = config->osabi;4370eHdr->e_ident[EI_ABIVERSION] = getAbiVersion();4371eHdr->e_machine = config->emachine;4372eHdr->e_version = EV_CURRENT;4373eHdr->e_flags = config->eflags;4374eHdr->e_ehsize = sizeof(typename ELFT::Ehdr);4375eHdr->e_phnum = part.phdrs.size();4376eHdr->e_shentsize = sizeof(typename ELFT::Shdr);43774378if (!config->relocatable) {4379eHdr->e_phoff = sizeof(typename ELFT::Ehdr);4380eHdr->e_phentsize = sizeof(typename ELFT::Phdr);4381}4382}43834384template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) {4385// Write the program header table.4386auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf);4387for (PhdrEntry *p : part.phdrs) {4388hBuf->p_type = p->p_type;4389hBuf->p_flags = p->p_flags;4390hBuf->p_offset = p->p_offset;4391hBuf->p_vaddr = p->p_vaddr;4392hBuf->p_paddr = p->p_paddr;4393hBuf->p_filesz = p->p_filesz;4394hBuf->p_memsz = p->p_memsz;4395hBuf->p_align = p->p_align;4396++hBuf;4397}4398}43994400template <typename ELFT>4401PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection()4402: SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {}44034404template <typename ELFT>4405size_t PartitionElfHeaderSection<ELFT>::getSize() const {4406return sizeof(typename ELFT::Ehdr);4407}44084409template <typename ELFT>4410void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {4411writeEhdr<ELFT>(buf, getPartition());44124413// Loadable partitions are always ET_DYN.4414auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);4415eHdr->e_type = ET_DYN;4416}44174418template <typename ELFT>4419PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection()4420: SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {}44214422template <typename ELFT>4423size_t PartitionProgramHeadersSection<ELFT>::getSize() const {4424return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size();4425}44264427template <typename ELFT>4428void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {4429writePhdrs<ELFT>(buf, getPartition());4430}44314432PartitionIndexSection::PartitionIndexSection()4433: SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}44344435size_t PartitionIndexSection::getSize() const {4436return 12 * (partitions.size() - 1);4437}44384439void PartitionIndexSection::finalizeContents() {4440for (size_t i = 1; i != partitions.size(); ++i)4441partitions[i].nameStrTab = mainPart->dynStrTab->addString(partitions[i].name);4442}44434444void PartitionIndexSection::writeTo(uint8_t *buf) {4445uint64_t va = getVA();4446for (size_t i = 1; i != partitions.size(); ++i) {4447write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va);4448write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4));44494450SyntheticSection *next = i == partitions.size() - 14451? in.partEnd.get()4452: partitions[i + 1].elfHeader.get();4453write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA());44544455va += 12;4456buf += 12;4457}4458}44594460void InStruct::reset() {4461attributes.reset();4462riscvAttributes.reset();4463bss.reset();4464bssRelRo.reset();4465got.reset();4466gotPlt.reset();4467igotPlt.reset();4468relroPadding.reset();4469armCmseSGSection.reset();4470ppc64LongBranchTarget.reset();4471mipsAbiFlags.reset();4472mipsGot.reset();4473mipsOptions.reset();4474mipsReginfo.reset();4475mipsRldMap.reset();4476partEnd.reset();4477partIndex.reset();4478plt.reset();4479iplt.reset();4480ppc32Got2.reset();4481ibtPlt.reset();4482relaPlt.reset();4483debugNames.reset();4484gdbIndex.reset();4485shStrTab.reset();4486strTab.reset();4487symTab.reset();4488symTabShndx.reset();4489}44904491static bool needsInterpSection() {4492return !config->relocatable && !config->shared &&4493!config->dynamicLinker.empty() && script->needsInterpSection();4494}44954496bool elf::hasMemtag() {4497return config->emachine == EM_AARCH64 &&4498config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;4499}45004501// Fully static executables don't support MTE globals at this point in time, as4502// we currently rely on:4503// - A dynamic loader to process relocations, and4504// - Dynamic entries.4505// This restriction could be removed in future by re-using some of the ideas4506// that ifuncs use in fully static executables.4507bool elf::canHaveMemtagGlobals() {4508return hasMemtag() &&4509(config->relocatable || config->shared || needsInterpSection());4510}45114512constexpr char kMemtagAndroidNoteName[] = "Android";4513void MemtagAndroidNote::writeTo(uint8_t *buf) {4514static_assert(4515sizeof(kMemtagAndroidNoteName) == 8,4516"Android 11 & 12 have an ABI that the note name is 8 bytes long. Keep it "4517"that way for backwards compatibility.");45184519write32(buf, sizeof(kMemtagAndroidNoteName));4520write32(buf + 4, sizeof(uint32_t));4521write32(buf + 8, ELF::NT_ANDROID_TYPE_MEMTAG);4522memcpy(buf + 12, kMemtagAndroidNoteName, sizeof(kMemtagAndroidNoteName));4523buf += 12 + alignTo(sizeof(kMemtagAndroidNoteName), 4);45244525uint32_t value = 0;4526value |= config->androidMemtagMode;4527if (config->androidMemtagHeap)4528value |= ELF::NT_MEMTAG_HEAP;4529// Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled4530// binary on Android 11 or 12 will result in a checkfail in the loader.4531if (config->androidMemtagStack)4532value |= ELF::NT_MEMTAG_STACK;4533write32(buf, value); // note value4534}45354536size_t MemtagAndroidNote::getSize() const {4537return sizeof(llvm::ELF::Elf64_Nhdr) +4538/*namesz=*/alignTo(sizeof(kMemtagAndroidNoteName), 4) +4539/*descsz=*/sizeof(uint32_t);4540}45414542void PackageMetadataNote::writeTo(uint8_t *buf) {4543write32(buf, 4);4544write32(buf + 4, config->packageMetadata.size() + 1);4545write32(buf + 8, FDO_PACKAGING_METADATA);4546memcpy(buf + 12, "FDO", 4);4547memcpy(buf + 16, config->packageMetadata.data(),4548config->packageMetadata.size());4549}45504551size_t PackageMetadataNote::getSize() const {4552return sizeof(llvm::ELF::Elf64_Nhdr) + 4 +4553alignTo(config->packageMetadata.size() + 1, 4);4554}45554556// Helper function, return the size of the ULEB128 for 'v', optionally writing4557// it to `*(buf + offset)` if `buf` is non-null.4558static size_t computeOrWriteULEB128(uint64_t v, uint8_t *buf, size_t offset) {4559if (buf)4560return encodeULEB128(v, buf + offset);4561return getULEB128Size(v);4562}45634564// https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#83encoding-of-sht_aarch64_memtag_globals_dynamic4565constexpr uint64_t kMemtagStepSizeBits = 3;4566constexpr uint64_t kMemtagGranuleSize = 16;4567static size_t4568createMemtagGlobalDescriptors(const SmallVector<const Symbol *, 0> &symbols,4569uint8_t *buf = nullptr) {4570size_t sectionSize = 0;4571uint64_t lastGlobalEnd = 0;45724573for (const Symbol *sym : symbols) {4574if (!includeInSymtab(*sym))4575continue;4576const uint64_t addr = sym->getVA();4577const uint64_t size = sym->getSize();45784579if (addr <= kMemtagGranuleSize && buf != nullptr)4580errorOrWarn("address of the tagged symbol \"" + sym->getName() +4581"\" falls in the ELF header. This is indicative of a "4582"compiler/linker bug");4583if (addr % kMemtagGranuleSize != 0)4584errorOrWarn("address of the tagged symbol \"" + sym->getName() +4585"\" at 0x" + Twine::utohexstr(addr) +4586"\" is not granule (16-byte) aligned");4587if (size == 0)4588errorOrWarn("size of the tagged symbol \"" + sym->getName() +4589"\" is not allowed to be zero");4590if (size % kMemtagGranuleSize != 0)4591errorOrWarn("size of the tagged symbol \"" + sym->getName() +4592"\" (size 0x" + Twine::utohexstr(size) +4593") is not granule (16-byte) aligned");45944595const uint64_t sizeToEncode = size / kMemtagGranuleSize;4596const uint64_t stepToEncode = ((addr - lastGlobalEnd) / kMemtagGranuleSize)4597<< kMemtagStepSizeBits;4598if (sizeToEncode < (1 << kMemtagStepSizeBits)) {4599sectionSize += computeOrWriteULEB128(stepToEncode | sizeToEncode, buf, sectionSize);4600} else {4601sectionSize += computeOrWriteULEB128(stepToEncode, buf, sectionSize);4602sectionSize += computeOrWriteULEB128(sizeToEncode - 1, buf, sectionSize);4603}4604lastGlobalEnd = addr + size;4605}46064607return sectionSize;4608}46094610bool MemtagGlobalDescriptors::updateAllocSize() {4611size_t oldSize = getSize();4612std::stable_sort(symbols.begin(), symbols.end(),4613[](const Symbol *s1, const Symbol *s2) {4614return s1->getVA() < s2->getVA();4615});4616return oldSize != getSize();4617}46184619void MemtagGlobalDescriptors::writeTo(uint8_t *buf) {4620createMemtagGlobalDescriptors(symbols, buf);4621}46224623size_t MemtagGlobalDescriptors::getSize() const {4624return createMemtagGlobalDescriptors(symbols);4625}46264627static OutputSection *findSection(StringRef name) {4628for (SectionCommand *cmd : script->sectionCommands)4629if (auto *osd = dyn_cast<OutputDesc>(cmd))4630if (osd->osec.name == name)4631return &osd->osec;4632return nullptr;4633}46344635static Defined *addOptionalRegular(StringRef name, SectionBase *sec,4636uint64_t val, uint8_t stOther = STV_HIDDEN) {4637Symbol *s = symtab.find(name);4638if (!s || s->isDefined() || s->isCommon())4639return nullptr;46404641s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, stOther,4642STT_NOTYPE, val,4643/*size=*/0, sec});4644s->isUsedInRegularObj = true;4645return cast<Defined>(s);4646}46474648template <class ELFT> void elf::createSyntheticSections() {4649// Initialize all pointers with NULL. This is needed because4650// you can call lld::elf::main more than once as a library.4651Out::tlsPhdr = nullptr;4652Out::preinitArray = nullptr;4653Out::initArray = nullptr;4654Out::finiArray = nullptr;46554656// Add the .interp section first because it is not a SyntheticSection.4657// The removeUnusedSyntheticSections() function relies on the4658// SyntheticSections coming last.4659if (needsInterpSection()) {4660for (size_t i = 1; i <= partitions.size(); ++i) {4661InputSection *sec = createInterpSection();4662sec->partition = i;4663ctx.inputSections.push_back(sec);4664}4665}46664667auto add = [](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };46684669in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false);46704671Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);4672Out::programHeaders->addralign = config->wordsize;46734674if (config->strip != StripPolicy::All) {4675in.strTab = std::make_unique<StringTableSection>(".strtab", false);4676in.symTab = std::make_unique<SymbolTableSection<ELFT>>(*in.strTab);4677in.symTabShndx = std::make_unique<SymtabShndxSection>();4678}46794680in.bss = std::make_unique<BssSection>(".bss", 0, 1);4681add(*in.bss);46824683// If there is a SECTIONS command and a .data.rel.ro section name use name4684// .data.rel.ro.bss so that we match in the .data.rel.ro output section.4685// This makes sure our relro is contiguous.4686bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro");4687in.bssRelRo = std::make_unique<BssSection>(4688hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);4689add(*in.bssRelRo);46904691// Add MIPS-specific sections.4692if (config->emachine == EM_MIPS) {4693if (!config->shared && config->hasDynSymTab) {4694in.mipsRldMap = std::make_unique<MipsRldMapSection>();4695add(*in.mipsRldMap);4696}4697if ((in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create()))4698add(*in.mipsAbiFlags);4699if ((in.mipsOptions = MipsOptionsSection<ELFT>::create()))4700add(*in.mipsOptions);4701if ((in.mipsReginfo = MipsReginfoSection<ELFT>::create()))4702add(*in.mipsReginfo);4703}47044705StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";47064707const unsigned threadCount = config->threadCount;4708for (Partition &part : partitions) {4709auto add = [&](SyntheticSection &sec) {4710sec.partition = part.getNumber();4711ctx.inputSections.push_back(&sec);4712};47134714if (!part.name.empty()) {4715part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>();4716part.elfHeader->name = part.name;4717add(*part.elfHeader);47184719part.programHeaders =4720std::make_unique<PartitionProgramHeadersSection<ELFT>>();4721add(*part.programHeaders);4722}47234724if (config->buildId != BuildIdKind::None) {4725part.buildId = std::make_unique<BuildIdSection>();4726add(*part.buildId);4727}47284729// dynSymTab is always present to simplify sym->includeInDynsym() in4730// finalizeSections.4731part.dynStrTab = std::make_unique<StringTableSection>(".dynstr", true);4732part.dynSymTab =4733std::make_unique<SymbolTableSection<ELFT>>(*part.dynStrTab);47344735if (config->relocatable)4736continue;4737part.dynamic = std::make_unique<DynamicSection<ELFT>>();47384739if (hasMemtag()) {4740part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>();4741add(*part.memtagAndroidNote);4742if (canHaveMemtagGlobals()) {4743part.memtagGlobalDescriptors =4744std::make_unique<MemtagGlobalDescriptors>();4745add(*part.memtagGlobalDescriptors);4746}4747}47484749if (config->androidPackDynRelocs)4750part.relaDyn = std::make_unique<AndroidPackedRelocationSection<ELFT>>(4751relaDynName, threadCount);4752else4753part.relaDyn = std::make_unique<RelocationSection<ELFT>>(4754relaDynName, config->zCombreloc, threadCount);47554756if (config->hasDynSymTab) {4757add(*part.dynSymTab);47584759part.verSym = std::make_unique<VersionTableSection>();4760add(*part.verSym);47614762if (!namedVersionDefs().empty()) {4763part.verDef = std::make_unique<VersionDefinitionSection>();4764add(*part.verDef);4765}47664767part.verNeed = std::make_unique<VersionNeedSection<ELFT>>();4768add(*part.verNeed);47694770if (config->gnuHash) {4771part.gnuHashTab = std::make_unique<GnuHashTableSection>();4772add(*part.gnuHashTab);4773}47744775if (config->sysvHash) {4776part.hashTab = std::make_unique<HashTableSection>();4777add(*part.hashTab);4778}47794780add(*part.dynamic);4781add(*part.dynStrTab);4782}4783add(*part.relaDyn);47844785if (config->relrPackDynRelocs) {4786part.relrDyn = std::make_unique<RelrSection<ELFT>>(threadCount);4787add(*part.relrDyn);4788part.relrAuthDyn = std::make_unique<RelrSection<ELFT>>(4789threadCount, /*isAArch64Auth=*/true);4790add(*part.relrAuthDyn);4791}47924793if (config->ehFrameHdr) {4794part.ehFrameHdr = std::make_unique<EhFrameHeader>();4795add(*part.ehFrameHdr);4796}4797part.ehFrame = std::make_unique<EhFrameSection>();4798add(*part.ehFrame);47994800if (config->emachine == EM_ARM) {4801// This section replaces all the individual .ARM.exidx InputSections.4802part.armExidx = std::make_unique<ARMExidxSyntheticSection>();4803add(*part.armExidx);4804}48054806if (!config->packageMetadata.empty()) {4807part.packageMetadataNote = std::make_unique<PackageMetadataNote>();4808add(*part.packageMetadataNote);4809}4810}48114812if (partitions.size() != 1) {4813// Create the partition end marker. This needs to be in partition number 2554814// so that it is sorted after all other partitions. It also has other4815// special handling (see createPhdrs() and combineEhSections()).4816in.partEnd =4817std::make_unique<BssSection>(".part.end", config->maxPageSize, 1);4818in.partEnd->partition = 255;4819add(*in.partEnd);48204821in.partIndex = std::make_unique<PartitionIndexSection>();4822addOptionalRegular("__part_index_begin", in.partIndex.get(), 0);4823addOptionalRegular("__part_index_end", in.partIndex.get(),4824in.partIndex->getSize());4825add(*in.partIndex);4826}48274828// Add .got. MIPS' .got is so different from the other archs,4829// it has its own class.4830if (config->emachine == EM_MIPS) {4831in.mipsGot = std::make_unique<MipsGotSection>();4832add(*in.mipsGot);4833} else {4834in.got = std::make_unique<GotSection>();4835add(*in.got);4836}48374838if (config->emachine == EM_PPC) {4839in.ppc32Got2 = std::make_unique<PPC32Got2Section>();4840add(*in.ppc32Got2);4841}48424843if (config->emachine == EM_PPC64) {4844in.ppc64LongBranchTarget = std::make_unique<PPC64LongBranchTargetSection>();4845add(*in.ppc64LongBranchTarget);4846}48474848in.gotPlt = std::make_unique<GotPltSection>();4849add(*in.gotPlt);4850in.igotPlt = std::make_unique<IgotPltSection>();4851add(*in.igotPlt);4852// Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the4853// section in the absence of PHDRS/SECTIONS commands.4854if (config->zRelro &&4855((script->phdrsCommands.empty() && !script->hasSectionsCommand) ||4856script->seenRelroEnd)) {4857in.relroPadding = std::make_unique<RelroPaddingSection>();4858add(*in.relroPadding);4859}48604861if (config->emachine == EM_ARM) {4862in.armCmseSGSection = std::make_unique<ArmCmseSGSection>();4863add(*in.armCmseSGSection);4864}48654866// _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat4867// it as a relocation and ensure the referenced section is created.4868if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) {4869if (target->gotBaseSymInGotPlt)4870in.gotPlt->hasGotPltOffRel = true;4871else4872in.got->hasGotOffRel = true;4873}48744875// We always need to add rel[a].plt to output if it has entries.4876// Even for static linking it can contain R_[*]_IRELATIVE relocations.4877in.relaPlt = std::make_unique<RelocationSection<ELFT>>(4878config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false,4879/*threadCount=*/1);4880add(*in.relaPlt);48814882if ((config->emachine == EM_386 || config->emachine == EM_X86_64) &&4883(config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {4884in.ibtPlt = std::make_unique<IBTPltSection>();4885add(*in.ibtPlt);4886}48874888if (config->emachine == EM_PPC)4889in.plt = std::make_unique<PPC32GlinkSection>();4890else4891in.plt = std::make_unique<PltSection>();4892add(*in.plt);4893in.iplt = std::make_unique<IpltSection>();4894add(*in.iplt);48954896if (config->andFeatures || !ctx.aarch64PauthAbiCoreInfo.empty())4897add(*make<GnuPropertySection>());48984899if (config->debugNames) {4900in.debugNames = std::make_unique<DebugNamesSection<ELFT>>();4901add(*in.debugNames);4902}49034904if (config->gdbIndex) {4905in.gdbIndex = GdbIndexSection::create<ELFT>();4906add(*in.gdbIndex);4907}49084909// .note.GNU-stack is always added when we are creating a re-linkable4910// object file. Other linkers are using the presence of this marker4911// section to control the executable-ness of the stack area, but that4912// is irrelevant these days. Stack area should always be non-executable4913// by default. So we emit this section unconditionally.4914if (config->relocatable)4915add(*make<GnuStackSection>());49164917if (in.symTab)4918add(*in.symTab);4919if (in.symTabShndx)4920add(*in.symTabShndx);4921add(*in.shStrTab);4922if (in.strTab)4923add(*in.strTab);4924}49254926InStruct elf::in;49274928std::vector<Partition> elf::partitions;4929Partition *elf::mainPart;49304931template void elf::splitSections<ELF32LE>();4932template void elf::splitSections<ELF32BE>();4933template void elf::splitSections<ELF64LE>();4934template void elf::splitSections<ELF64BE>();49354936template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>(4937function_ref<void(InputSection &)>);4938template void EhFrameSection::iterateFDEWithLSDA<ELF32BE>(4939function_ref<void(InputSection &)>);4940template void EhFrameSection::iterateFDEWithLSDA<ELF64LE>(4941function_ref<void(InputSection &)>);4942template void EhFrameSection::iterateFDEWithLSDA<ELF64BE>(4943function_ref<void(InputSection &)>);49444945template class elf::SymbolTableSection<ELF32LE>;4946template class elf::SymbolTableSection<ELF32BE>;4947template class elf::SymbolTableSection<ELF64LE>;4948template class elf::SymbolTableSection<ELF64BE>;49494950template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part);4951template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part);4952template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part);4953template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part);49544955template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);4956template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);4957template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);4958template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);49594960template void elf::createSyntheticSections<ELF32LE>();4961template void elf::createSyntheticSections<ELF32BE>();4962template void elf::createSyntheticSections<ELF64LE>();4963template void elf::createSyntheticSections<ELF64BE>();496449654966