10b57cec5SDimitry Andric //===- SyntheticSections.cpp ----------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains linker-synthesized sections. Currently, 100b57cec5SDimitry Andric // synthetic sections are created either output sections or input sections, 110b57cec5SDimitry Andric // but we are rewriting code so that all synthetic sections are created as 120b57cec5SDimitry Andric // input sections. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "SyntheticSections.h" 170b57cec5SDimitry Andric #include "Config.h" 1881ad6265SDimitry Andric #include "DWARF.h" 1981ad6265SDimitry Andric #include "EhFrame.h" 200b57cec5SDimitry Andric #include "InputFiles.h" 210b57cec5SDimitry Andric #include "LinkerScript.h" 220b57cec5SDimitry Andric #include "OutputSections.h" 230b57cec5SDimitry Andric #include "SymbolTable.h" 240b57cec5SDimitry Andric #include "Symbols.h" 250b57cec5SDimitry Andric #include "Target.h" 2681ad6265SDimitry Andric #include "Thunks.h" 270b57cec5SDimitry Andric #include "Writer.h" 2804eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h" 295ffd83dbSDimitry Andric #include "lld/Common/DWARF.h" 300b57cec5SDimitry Andric #include "lld/Common/Strings.h" 310b57cec5SDimitry Andric #include "lld/Common/Version.h" 32fcaf7f86SDimitry Andric #include "llvm/ADT/STLExtras.h" 330b57cec5SDimitry Andric #include "llvm/ADT/SetOperations.h" 340b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 350b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 3681ad6265SDimitry Andric #include "llvm/BinaryFormat/ELF.h" 370b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 380b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 390b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 405ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h" 415ffd83dbSDimitry Andric #include "llvm/Support/TimeProfiler.h" 420b57cec5SDimitry Andric #include <cstdlib> 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric using namespace llvm; 450b57cec5SDimitry Andric using namespace llvm::dwarf; 460b57cec5SDimitry Andric using namespace llvm::ELF; 470b57cec5SDimitry Andric using namespace llvm::object; 480b57cec5SDimitry Andric using namespace llvm::support; 495ffd83dbSDimitry Andric using namespace lld; 505ffd83dbSDimitry Andric using namespace lld::elf; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric using llvm::support::endian::read32le; 530b57cec5SDimitry Andric using llvm::support::endian::write32le; 540b57cec5SDimitry Andric using llvm::support::endian::write64le; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric constexpr size_t MergeNoTailSection::numShards; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric static uint64_t readUint(uint8_t *buf) { 590b57cec5SDimitry Andric return config->is64 ? read64(buf) : read32(buf); 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static void writeUint(uint8_t *buf, uint64_t val) { 630b57cec5SDimitry Andric if (config->is64) 640b57cec5SDimitry Andric write64(buf, val); 650b57cec5SDimitry Andric else 660b57cec5SDimitry Andric write32(buf, val); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // Returns an LLD version string. 700b57cec5SDimitry Andric static ArrayRef<uint8_t> getVersion() { 710b57cec5SDimitry Andric // Check LLD_VERSION first for ease of testing. 720b57cec5SDimitry Andric // You can get consistent output by using the environment variable. 730b57cec5SDimitry Andric // This is only for testing. 740b57cec5SDimitry Andric StringRef s = getenv("LLD_VERSION"); 750b57cec5SDimitry Andric if (s.empty()) 7604eeddc0SDimitry Andric s = saver().save(Twine("Linker: ") + getLLDVersion()); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // +1 to include the terminating '\0'. 790b57cec5SDimitry Andric return {(const uint8_t *)s.data(), s.size() + 1}; 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Creates a .comment section containing LLD version info. 830b57cec5SDimitry Andric // With this feature, you can identify LLD-generated binaries easily 840b57cec5SDimitry Andric // by "readelf --string-dump .comment <file>". 850b57cec5SDimitry Andric // The returned object is a mergeable string section. 865ffd83dbSDimitry Andric MergeInputSection *elf::createCommentSection() { 871fd87a68SDimitry Andric auto *sec = make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1, 880b57cec5SDimitry Andric getVersion(), ".comment"); 891fd87a68SDimitry Andric sec->splitIntoPieces(); 901fd87a68SDimitry Andric return sec; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric // .MIPS.abiflags section. 940b57cec5SDimitry Andric template <class ELFT> 950b57cec5SDimitry Andric MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags) 960b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), 970b57cec5SDimitry Andric flags(flags) { 980b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_ABIFlags); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) { 1020b57cec5SDimitry Andric memcpy(buf, &flags, sizeof(flags)); 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric template <class ELFT> 1061fd87a68SDimitry Andric std::unique_ptr<MipsAbiFlagsSection<ELFT>> MipsAbiFlagsSection<ELFT>::create() { 1070b57cec5SDimitry Andric Elf_Mips_ABIFlags flags = {}; 1080b57cec5SDimitry Andric bool create = false; 1090b57cec5SDimitry Andric 110*bdd1243dSDimitry Andric for (InputSectionBase *sec : ctx.inputSections) { 1110b57cec5SDimitry Andric if (sec->type != SHT_MIPS_ABIFLAGS) 1120b57cec5SDimitry Andric continue; 1130b57cec5SDimitry Andric sec->markDead(); 1140b57cec5SDimitry Andric create = true; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric std::string filename = toString(sec->file); 117*bdd1243dSDimitry Andric const size_t size = sec->content().size(); 1180b57cec5SDimitry Andric // Older version of BFD (such as the default FreeBSD linker) concatenate 1190b57cec5SDimitry Andric // .MIPS.abiflags instead of merging. To allow for this case (or potential 1200b57cec5SDimitry Andric // zero padding) we ignore everything after the first Elf_Mips_ABIFlags 1210b57cec5SDimitry Andric if (size < sizeof(Elf_Mips_ABIFlags)) { 1220b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.abiflags section: got " + 1230b57cec5SDimitry Andric Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); 1240b57cec5SDimitry Andric return nullptr; 1250b57cec5SDimitry Andric } 126*bdd1243dSDimitry Andric auto *s = 127*bdd1243dSDimitry Andric reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->content().data()); 1280b57cec5SDimitry Andric if (s->version != 0) { 1290b57cec5SDimitry Andric error(filename + ": unexpected .MIPS.abiflags version " + 1300b57cec5SDimitry Andric Twine(s->version)); 1310b57cec5SDimitry Andric return nullptr; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric // LLD checks ISA compatibility in calcMipsEFlags(). Here we just 1350b57cec5SDimitry Andric // select the highest number of ISA/Rev/Ext. 1360b57cec5SDimitry Andric flags.isa_level = std::max(flags.isa_level, s->isa_level); 1370b57cec5SDimitry Andric flags.isa_rev = std::max(flags.isa_rev, s->isa_rev); 1380b57cec5SDimitry Andric flags.isa_ext = std::max(flags.isa_ext, s->isa_ext); 1390b57cec5SDimitry Andric flags.gpr_size = std::max(flags.gpr_size, s->gpr_size); 1400b57cec5SDimitry Andric flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size); 1410b57cec5SDimitry Andric flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size); 1420b57cec5SDimitry Andric flags.ases |= s->ases; 1430b57cec5SDimitry Andric flags.flags1 |= s->flags1; 1440b57cec5SDimitry Andric flags.flags2 |= s->flags2; 1455ffd83dbSDimitry Andric flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename); 1460b57cec5SDimitry Andric }; 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric if (create) 1491fd87a68SDimitry Andric return std::make_unique<MipsAbiFlagsSection<ELFT>>(flags); 1500b57cec5SDimitry Andric return nullptr; 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric // .MIPS.options section. 1540b57cec5SDimitry Andric template <class ELFT> 1550b57cec5SDimitry Andric MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo) 1560b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), 1570b57cec5SDimitry Andric reginfo(reginfo) { 1580b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) { 1620b57cec5SDimitry Andric auto *options = reinterpret_cast<Elf_Mips_Options *>(buf); 1630b57cec5SDimitry Andric options->kind = ODK_REGINFO; 1640b57cec5SDimitry Andric options->size = getSize(); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric if (!config->relocatable) 1670b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 1680b57cec5SDimitry Andric memcpy(buf + sizeof(Elf_Mips_Options), ®info, sizeof(reginfo)); 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric template <class ELFT> 1721fd87a68SDimitry Andric std::unique_ptr<MipsOptionsSection<ELFT>> MipsOptionsSection<ELFT>::create() { 1730b57cec5SDimitry Andric // N64 ABI only. 1740b57cec5SDimitry Andric if (!ELFT::Is64Bits) 1750b57cec5SDimitry Andric return nullptr; 1760b57cec5SDimitry Andric 17704eeddc0SDimitry Andric SmallVector<InputSectionBase *, 0> sections; 178*bdd1243dSDimitry Andric for (InputSectionBase *sec : ctx.inputSections) 1790b57cec5SDimitry Andric if (sec->type == SHT_MIPS_OPTIONS) 1800b57cec5SDimitry Andric sections.push_back(sec); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric if (sections.empty()) 1830b57cec5SDimitry Andric return nullptr; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 1860b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 1870b57cec5SDimitry Andric sec->markDead(); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric std::string filename = toString(sec->file); 190*bdd1243dSDimitry Andric ArrayRef<uint8_t> d = sec->content(); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric while (!d.empty()) { 1930b57cec5SDimitry Andric if (d.size() < sizeof(Elf_Mips_Options)) { 1940b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.options section"); 1950b57cec5SDimitry Andric break; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data()); 1990b57cec5SDimitry Andric if (opt->kind == ODK_REGINFO) { 2000b57cec5SDimitry Andric reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask; 2010b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value; 2020b57cec5SDimitry Andric break; 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric if (!opt->size) 2060b57cec5SDimitry Andric fatal(filename + ": zero option descriptor size"); 2070b57cec5SDimitry Andric d = d.slice(opt->size); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric }; 2100b57cec5SDimitry Andric 2111fd87a68SDimitry Andric return std::make_unique<MipsOptionsSection<ELFT>>(reginfo); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // MIPS .reginfo section. 2150b57cec5SDimitry Andric template <class ELFT> 2160b57cec5SDimitry Andric MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo) 2170b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), 2180b57cec5SDimitry Andric reginfo(reginfo) { 2190b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_RegInfo); 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) { 2230b57cec5SDimitry Andric if (!config->relocatable) 2240b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 2250b57cec5SDimitry Andric memcpy(buf, ®info, sizeof(reginfo)); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric template <class ELFT> 2291fd87a68SDimitry Andric std::unique_ptr<MipsReginfoSection<ELFT>> MipsReginfoSection<ELFT>::create() { 2300b57cec5SDimitry Andric // Section should be alive for O32 and N32 ABIs only. 2310b57cec5SDimitry Andric if (ELFT::Is64Bits) 2320b57cec5SDimitry Andric return nullptr; 2330b57cec5SDimitry Andric 23404eeddc0SDimitry Andric SmallVector<InputSectionBase *, 0> sections; 235*bdd1243dSDimitry Andric for (InputSectionBase *sec : ctx.inputSections) 2360b57cec5SDimitry Andric if (sec->type == SHT_MIPS_REGINFO) 2370b57cec5SDimitry Andric sections.push_back(sec); 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric if (sections.empty()) 2400b57cec5SDimitry Andric return nullptr; 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 2430b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 2440b57cec5SDimitry Andric sec->markDead(); 2450b57cec5SDimitry Andric 246*bdd1243dSDimitry Andric if (sec->content().size() != sizeof(Elf_Mips_RegInfo)) { 2470b57cec5SDimitry Andric error(toString(sec->file) + ": invalid size of .reginfo section"); 2480b57cec5SDimitry Andric return nullptr; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 251*bdd1243dSDimitry Andric auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->content().data()); 2520b57cec5SDimitry Andric reginfo.ri_gprmask |= r->ri_gprmask; 2530b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value; 2540b57cec5SDimitry Andric }; 2550b57cec5SDimitry Andric 2561fd87a68SDimitry Andric return std::make_unique<MipsReginfoSection<ELFT>>(reginfo); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2595ffd83dbSDimitry Andric InputSection *elf::createInterpSection() { 2600b57cec5SDimitry Andric // StringSaver guarantees that the returned string ends with '\0'. 26104eeddc0SDimitry Andric StringRef s = saver().save(config->dynamicLinker); 2620b57cec5SDimitry Andric ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1}; 2630b57cec5SDimitry Andric 26485868e8aSDimitry Andric return make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents, 2650b57cec5SDimitry Andric ".interp"); 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2685ffd83dbSDimitry Andric Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value, 2690b57cec5SDimitry Andric uint64_t size, InputSectionBase §ion) { 2700eae32dcSDimitry Andric Defined *s = makeDefined(section.file, name, STB_LOCAL, STV_DEFAULT, type, 2710b57cec5SDimitry Andric value, size, §ion); 2720b57cec5SDimitry Andric if (in.symTab) 2730b57cec5SDimitry Andric in.symTab->addSymbol(s); 2740b57cec5SDimitry Andric return s; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric static size_t getHashSize() { 2780b57cec5SDimitry Andric switch (config->buildId) { 2790b57cec5SDimitry Andric case BuildIdKind::Fast: 2800b57cec5SDimitry Andric return 8; 2810b57cec5SDimitry Andric case BuildIdKind::Md5: 2820b57cec5SDimitry Andric case BuildIdKind::Uuid: 2830b57cec5SDimitry Andric return 16; 2840b57cec5SDimitry Andric case BuildIdKind::Sha1: 2850b57cec5SDimitry Andric return 20; 2860b57cec5SDimitry Andric case BuildIdKind::Hexstring: 2870b57cec5SDimitry Andric return config->buildIdVector.size(); 2880b57cec5SDimitry Andric default: 2890b57cec5SDimitry Andric llvm_unreachable("unknown BuildIdKind"); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // This class represents a linker-synthesized .note.gnu.property section. 2940b57cec5SDimitry Andric // 2950b57cec5SDimitry Andric // In x86 and AArch64, object files may contain feature flags indicating the 2960b57cec5SDimitry Andric // features that they have used. The flags are stored in a .note.gnu.property 2970b57cec5SDimitry Andric // section. 2980b57cec5SDimitry Andric // 2990b57cec5SDimitry Andric // lld reads the sections from input files and merges them by computing AND of 3000b57cec5SDimitry Andric // the flags. The result is written as a new .note.gnu.property section. 3010b57cec5SDimitry Andric // 3020b57cec5SDimitry Andric // If the flag is zero (which indicates that the intersection of the feature 3030b57cec5SDimitry Andric // sets is empty, or some input files didn't have .note.gnu.property sections), 3040b57cec5SDimitry Andric // we don't create this section. 3050b57cec5SDimitry Andric GnuPropertySection::GnuPropertySection() 306480093f4SDimitry Andric : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, 307480093f4SDimitry Andric config->wordsize, ".note.gnu.property") {} 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric void GnuPropertySection::writeTo(uint8_t *buf) { 3100b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 3110b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 3120b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric write32(buf, 4); // Name size 3150b57cec5SDimitry Andric write32(buf + 4, config->is64 ? 16 : 12); // Content size 3160b57cec5SDimitry Andric write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type 3170b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 3180b57cec5SDimitry Andric write32(buf + 16, featureAndType); // Feature type 3190b57cec5SDimitry Andric write32(buf + 20, 4); // Feature size 3200b57cec5SDimitry Andric write32(buf + 24, config->andFeatures); // Feature flags 3210b57cec5SDimitry Andric if (config->is64) 3220b57cec5SDimitry Andric write32(buf + 28, 0); // Padding 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric size_t GnuPropertySection::getSize() const { return config->is64 ? 32 : 28; } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric BuildIdSection::BuildIdSection() 3280b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"), 3290b57cec5SDimitry Andric hashSize(getHashSize()) {} 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric void BuildIdSection::writeTo(uint8_t *buf) { 3320b57cec5SDimitry Andric write32(buf, 4); // Name size 3330b57cec5SDimitry Andric write32(buf + 4, hashSize); // Content size 3340b57cec5SDimitry Andric write32(buf + 8, NT_GNU_BUILD_ID); // Type 3350b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 3360b57cec5SDimitry Andric hashBuf = buf + 16; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) { 3400b57cec5SDimitry Andric assert(buf.size() == hashSize); 3410b57cec5SDimitry Andric memcpy(hashBuf, buf.data(), hashSize); 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment) 3450b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) { 3460b57cec5SDimitry Andric this->bss = true; 3470b57cec5SDimitry Andric this->size = size; 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric EhFrameSection::EhFrameSection() 3510b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric // Search for an existing CIE record or create a new one. 3540b57cec5SDimitry Andric // CIE records from input object files are uniquified by their contents 3550b57cec5SDimitry Andric // and where their relocations point to. 3560b57cec5SDimitry Andric template <class ELFT, class RelTy> 3570b57cec5SDimitry Andric CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) { 3580b57cec5SDimitry Andric Symbol *personality = nullptr; 3590b57cec5SDimitry Andric unsigned firstRelI = cie.firstRelocation; 3600b57cec5SDimitry Andric if (firstRelI != (unsigned)-1) 3610b57cec5SDimitry Andric personality = 3620b57cec5SDimitry Andric &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]); 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric // Search for an existing CIE by CIE contents/relocation target pair. 3650b57cec5SDimitry Andric CieRecord *&rec = cieMap[{cie.data(), personality}]; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric // If not found, create a new one. 3680b57cec5SDimitry Andric if (!rec) { 3690b57cec5SDimitry Andric rec = make<CieRecord>(); 3700b57cec5SDimitry Andric rec->cie = &cie; 3710b57cec5SDimitry Andric cieRecords.push_back(rec); 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric return rec; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 376e8d8bef9SDimitry Andric // There is one FDE per function. Returns a non-null pointer to the function 377e8d8bef9SDimitry Andric // symbol if the given FDE points to a live function. 3780b57cec5SDimitry Andric template <class ELFT, class RelTy> 379e8d8bef9SDimitry Andric Defined *EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) { 3800b57cec5SDimitry Andric auto *sec = cast<EhInputSection>(fde.sec); 3810b57cec5SDimitry Andric unsigned firstRelI = fde.firstRelocation; 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric // An FDE should point to some function because FDEs are to describe 3840b57cec5SDimitry Andric // functions. That's however not always the case due to an issue of 3850b57cec5SDimitry Andric // ld.gold with -r. ld.gold may discard only functions and leave their 3860b57cec5SDimitry Andric // corresponding FDEs, which results in creating bad .eh_frame sections. 3870b57cec5SDimitry Andric // To deal with that, we ignore such FDEs. 3880b57cec5SDimitry Andric if (firstRelI == (unsigned)-1) 389e8d8bef9SDimitry Andric return nullptr; 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric const RelTy &rel = rels[firstRelI]; 3920b57cec5SDimitry Andric Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel); 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric // FDEs for garbage-collected or merged-by-ICF sections, or sections in 3950b57cec5SDimitry Andric // another partition, are dead. 3960b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&b)) 3970eae32dcSDimitry Andric if (!d->folded && d->section && d->section->partition == partition) 398e8d8bef9SDimitry Andric return d; 399e8d8bef9SDimitry Andric return nullptr; 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric // .eh_frame is a sequence of CIE or FDE records. In general, there 4030b57cec5SDimitry Andric // is one CIE record per input object file which is followed by 4040b57cec5SDimitry Andric // a list of FDEs. This function searches an existing CIE or create a new 4050b57cec5SDimitry Andric // one and associates FDEs to the CIE. 4060b57cec5SDimitry Andric template <class ELFT, class RelTy> 40785868e8aSDimitry Andric void EhFrameSection::addRecords(EhInputSection *sec, ArrayRef<RelTy> rels) { 4080b57cec5SDimitry Andric offsetToCie.clear(); 409*bdd1243dSDimitry Andric for (EhSectionPiece &cie : sec->cies) 410*bdd1243dSDimitry Andric offsetToCie[cie.inputOff] = addCie<ELFT>(cie, rels); 411*bdd1243dSDimitry Andric for (EhSectionPiece &fde : sec->fdes) { 412*bdd1243dSDimitry Andric uint32_t id = endian::read32<ELFT::TargetEndianness>(fde.data().data() + 4); 413*bdd1243dSDimitry Andric CieRecord *rec = offsetToCie[fde.inputOff + 4 - id]; 4140b57cec5SDimitry Andric if (!rec) 4150b57cec5SDimitry Andric fatal(toString(sec) + ": invalid CIE reference"); 4160b57cec5SDimitry Andric 417*bdd1243dSDimitry Andric if (!isFdeLive<ELFT>(fde, rels)) 4180b57cec5SDimitry Andric continue; 419*bdd1243dSDimitry Andric rec->fdes.push_back(&fde); 4200b57cec5SDimitry Andric numFdes++; 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 42485868e8aSDimitry Andric template <class ELFT> 42585868e8aSDimitry Andric void EhFrameSection::addSectionAux(EhInputSection *sec) { 42685868e8aSDimitry Andric if (!sec->isLive()) 42785868e8aSDimitry Andric return; 428349cc55cSDimitry Andric const RelsOrRelas<ELFT> rels = sec->template relsOrRelas<ELFT>(); 429349cc55cSDimitry Andric if (rels.areRelocsRel()) 430349cc55cSDimitry Andric addRecords<ELFT>(sec, rels.rels); 43185868e8aSDimitry Andric else 432349cc55cSDimitry Andric addRecords<ELFT>(sec, rels.relas); 43385868e8aSDimitry Andric } 43485868e8aSDimitry Andric 435e8d8bef9SDimitry Andric // Used by ICF<ELFT>::handleLSDA(). This function is very similar to 436e8d8bef9SDimitry Andric // EhFrameSection::addRecords(). 437e8d8bef9SDimitry Andric template <class ELFT, class RelTy> 438e8d8bef9SDimitry Andric void EhFrameSection::iterateFDEWithLSDAAux( 439e8d8bef9SDimitry Andric EhInputSection &sec, ArrayRef<RelTy> rels, DenseSet<size_t> &ciesWithLSDA, 440e8d8bef9SDimitry Andric llvm::function_ref<void(InputSection &)> fn) { 441*bdd1243dSDimitry Andric for (EhSectionPiece &cie : sec.cies) 442*bdd1243dSDimitry Andric if (hasLSDA(cie)) 443*bdd1243dSDimitry Andric ciesWithLSDA.insert(cie.inputOff); 444*bdd1243dSDimitry Andric for (EhSectionPiece &fde : sec.fdes) { 445*bdd1243dSDimitry Andric uint32_t id = endian::read32<ELFT::TargetEndianness>(fde.data().data() + 4); 446*bdd1243dSDimitry Andric if (!ciesWithLSDA.contains(fde.inputOff + 4 - id)) 447e8d8bef9SDimitry Andric continue; 448e8d8bef9SDimitry Andric 449e8d8bef9SDimitry Andric // The CIE has a LSDA argument. Call fn with d's section. 450*bdd1243dSDimitry Andric if (Defined *d = isFdeLive<ELFT>(fde, rels)) 451e8d8bef9SDimitry Andric if (auto *s = dyn_cast_or_null<InputSection>(d->section)) 452e8d8bef9SDimitry Andric fn(*s); 453e8d8bef9SDimitry Andric } 454e8d8bef9SDimitry Andric } 455e8d8bef9SDimitry Andric 456e8d8bef9SDimitry Andric template <class ELFT> 457e8d8bef9SDimitry Andric void EhFrameSection::iterateFDEWithLSDA( 458e8d8bef9SDimitry Andric llvm::function_ref<void(InputSection &)> fn) { 459e8d8bef9SDimitry Andric DenseSet<size_t> ciesWithLSDA; 460e8d8bef9SDimitry Andric for (EhInputSection *sec : sections) { 461e8d8bef9SDimitry Andric ciesWithLSDA.clear(); 462349cc55cSDimitry Andric const RelsOrRelas<ELFT> rels = sec->template relsOrRelas<ELFT>(); 463349cc55cSDimitry Andric if (rels.areRelocsRel()) 464349cc55cSDimitry Andric iterateFDEWithLSDAAux<ELFT>(*sec, rels.rels, ciesWithLSDA, fn); 465e8d8bef9SDimitry Andric else 466349cc55cSDimitry Andric iterateFDEWithLSDAAux<ELFT>(*sec, rels.relas, ciesWithLSDA, fn); 467e8d8bef9SDimitry Andric } 468e8d8bef9SDimitry Andric } 469e8d8bef9SDimitry Andric 4700b57cec5SDimitry Andric static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) { 4710b57cec5SDimitry Andric memcpy(buf, d.data(), d.size()); 4720b57cec5SDimitry Andric // Fix the size field. -4 since size does not include the size field itself. 473*bdd1243dSDimitry Andric write32(buf, d.size() - 4); 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric void EhFrameSection::finalizeContents() { 4770b57cec5SDimitry Andric assert(!this->size); // Not finalized. 47885868e8aSDimitry Andric 47985868e8aSDimitry Andric switch (config->ekind) { 48085868e8aSDimitry Andric case ELFNoneKind: 48185868e8aSDimitry Andric llvm_unreachable("invalid ekind"); 48285868e8aSDimitry Andric case ELF32LEKind: 48385868e8aSDimitry Andric for (EhInputSection *sec : sections) 48485868e8aSDimitry Andric addSectionAux<ELF32LE>(sec); 48585868e8aSDimitry Andric break; 48685868e8aSDimitry Andric case ELF32BEKind: 48785868e8aSDimitry Andric for (EhInputSection *sec : sections) 48885868e8aSDimitry Andric addSectionAux<ELF32BE>(sec); 48985868e8aSDimitry Andric break; 49085868e8aSDimitry Andric case ELF64LEKind: 49185868e8aSDimitry Andric for (EhInputSection *sec : sections) 49285868e8aSDimitry Andric addSectionAux<ELF64LE>(sec); 49385868e8aSDimitry Andric break; 49485868e8aSDimitry Andric case ELF64BEKind: 49585868e8aSDimitry Andric for (EhInputSection *sec : sections) 49685868e8aSDimitry Andric addSectionAux<ELF64BE>(sec); 49785868e8aSDimitry Andric break; 49885868e8aSDimitry Andric } 49985868e8aSDimitry Andric 5000b57cec5SDimitry Andric size_t off = 0; 5010b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 5020b57cec5SDimitry Andric rec->cie->outputOff = off; 503*bdd1243dSDimitry Andric off += rec->cie->size; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 5060b57cec5SDimitry Andric fde->outputOff = off; 507*bdd1243dSDimitry Andric off += fde->size; 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // The LSB standard does not allow a .eh_frame section with zero 5120b57cec5SDimitry Andric // Call Frame Information records. glibc unwind-dw2-fde.c 5130b57cec5SDimitry Andric // classify_object_over_fdes expects there is a CIE record length 0 as a 5140b57cec5SDimitry Andric // terminator. Thus we add one unconditionally. 5150b57cec5SDimitry Andric off += 4; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric this->size = off; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table 5210b57cec5SDimitry Andric // to get an FDE from an address to which FDE is applied. This function 5220b57cec5SDimitry Andric // returns a list of such pairs. 52304eeddc0SDimitry Andric SmallVector<EhFrameSection::FdeData, 0> EhFrameSection::getFdeData() const { 5240b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 52504eeddc0SDimitry Andric SmallVector<FdeData, 0> ret; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric uint64_t va = getPartition().ehFrameHdr->getVA(); 5280b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 5290b57cec5SDimitry Andric uint8_t enc = getFdeEncoding(rec->cie); 5300b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 5310b57cec5SDimitry Andric uint64_t pc = getFdePc(buf, fde->outputOff, enc); 5320b57cec5SDimitry Andric uint64_t fdeVA = getParent()->addr + fde->outputOff; 5330b57cec5SDimitry Andric if (!isInt<32>(pc - va)) 5340b57cec5SDimitry Andric fatal(toString(fde->sec) + ": PC offset is too large: 0x" + 5350b57cec5SDimitry Andric Twine::utohexstr(pc - va)); 5360b57cec5SDimitry Andric ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)}); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric // Sort the FDE list by their PC and uniqueify. Usually there is only 5410b57cec5SDimitry Andric // one FDE for a PC (i.e. function), but if ICF merges two functions 5420b57cec5SDimitry Andric // into one, there can be more than one FDEs pointing to the address. 5430b57cec5SDimitry Andric auto less = [](const FdeData &a, const FdeData &b) { 5440b57cec5SDimitry Andric return a.pcRel < b.pcRel; 5450b57cec5SDimitry Andric }; 5460b57cec5SDimitry Andric llvm::stable_sort(ret, less); 5470b57cec5SDimitry Andric auto eq = [](const FdeData &a, const FdeData &b) { 5480b57cec5SDimitry Andric return a.pcRel == b.pcRel; 5490b57cec5SDimitry Andric }; 5500b57cec5SDimitry Andric ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end()); 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric return ret; 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric static uint64_t readFdeAddr(uint8_t *buf, int size) { 5560b57cec5SDimitry Andric switch (size) { 5570b57cec5SDimitry Andric case DW_EH_PE_udata2: 5580b57cec5SDimitry Andric return read16(buf); 5590b57cec5SDimitry Andric case DW_EH_PE_sdata2: 5600b57cec5SDimitry Andric return (int16_t)read16(buf); 5610b57cec5SDimitry Andric case DW_EH_PE_udata4: 5620b57cec5SDimitry Andric return read32(buf); 5630b57cec5SDimitry Andric case DW_EH_PE_sdata4: 5640b57cec5SDimitry Andric return (int32_t)read32(buf); 5650b57cec5SDimitry Andric case DW_EH_PE_udata8: 5660b57cec5SDimitry Andric case DW_EH_PE_sdata8: 5670b57cec5SDimitry Andric return read64(buf); 5680b57cec5SDimitry Andric case DW_EH_PE_absptr: 5690b57cec5SDimitry Andric return readUint(buf); 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric fatal("unknown FDE size encoding"); 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. 5750b57cec5SDimitry Andric // We need it to create .eh_frame_hdr section. 5760b57cec5SDimitry Andric uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff, 5770b57cec5SDimitry Andric uint8_t enc) const { 5780b57cec5SDimitry Andric // The starting address to which this FDE applies is 5790b57cec5SDimitry Andric // stored at FDE + 8 byte. 5800b57cec5SDimitry Andric size_t off = fdeOff + 8; 5810b57cec5SDimitry Andric uint64_t addr = readFdeAddr(buf + off, enc & 0xf); 5820b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_absptr) 5830b57cec5SDimitry Andric return addr; 5840b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_pcrel) 5850b57cec5SDimitry Andric return addr + getParent()->addr + off; 5860b57cec5SDimitry Andric fatal("unknown FDE size relative encoding"); 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric void EhFrameSection::writeTo(uint8_t *buf) { 5900b57cec5SDimitry Andric // Write CIE and FDE records. 5910b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 5920b57cec5SDimitry Andric size_t cieOffset = rec->cie->outputOff; 5930b57cec5SDimitry Andric writeCieFde(buf + cieOffset, rec->cie->data()); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 5960b57cec5SDimitry Andric size_t off = fde->outputOff; 5970b57cec5SDimitry Andric writeCieFde(buf + off, fde->data()); 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric // FDE's second word should have the offset to an associated CIE. 6000b57cec5SDimitry Andric // Write it. 6010b57cec5SDimitry Andric write32(buf + off + 4, off + 4 - cieOffset); 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric // Apply relocations. .eh_frame section contents are not contiguous 6060b57cec5SDimitry Andric // in the output buffer, but relocateAlloc() still works because 6070b57cec5SDimitry Andric // getOffset() takes care of discontiguous section pieces. 6080b57cec5SDimitry Andric for (EhInputSection *s : sections) 609*bdd1243dSDimitry Andric target->relocateAlloc(*s, buf); 6100b57cec5SDimitry Andric 6110b57cec5SDimitry Andric if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent()) 6120b57cec5SDimitry Andric getPartition().ehFrameHdr->write(); 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric GotSection::GotSection() 616fe6060f1SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 617fe6060f1SDimitry Andric target->gotEntrySize, ".got") { 618fe6060f1SDimitry Andric numEntries = target->gotHeaderEntriesNum; 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 621*bdd1243dSDimitry Andric void GotSection::addConstant(const Relocation &r) { relocations.push_back(r); } 6220b57cec5SDimitry Andric void GotSection::addEntry(Symbol &sym) { 62304eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1); 62404eeddc0SDimitry Andric symAux.back().gotIdx = numEntries++; 62504eeddc0SDimitry Andric } 62604eeddc0SDimitry Andric 62704eeddc0SDimitry Andric bool GotSection::addTlsDescEntry(Symbol &sym) { 62804eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1); 62904eeddc0SDimitry Andric symAux.back().tlsDescIdx = numEntries; 63004eeddc0SDimitry Andric numEntries += 2; 63104eeddc0SDimitry Andric return true; 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric bool GotSection::addDynTlsEntry(Symbol &sym) { 63504eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1); 63604eeddc0SDimitry Andric symAux.back().tlsGdIdx = numEntries; 6370b57cec5SDimitry Andric // Global Dynamic TLS entries take two GOT slots. 6380b57cec5SDimitry Andric numEntries += 2; 6390b57cec5SDimitry Andric return true; 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric // Reserves TLS entries for a TLS module ID and a TLS block offset. 6430b57cec5SDimitry Andric // In total it takes two GOT slots. 6440b57cec5SDimitry Andric bool GotSection::addTlsIndex() { 6450b57cec5SDimitry Andric if (tlsIndexOff != uint32_t(-1)) 6460b57cec5SDimitry Andric return false; 6470b57cec5SDimitry Andric tlsIndexOff = numEntries * config->wordsize; 6480b57cec5SDimitry Andric numEntries += 2; 6490b57cec5SDimitry Andric return true; 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 65204eeddc0SDimitry Andric uint32_t GotSection::getTlsDescOffset(const Symbol &sym) const { 65304eeddc0SDimitry Andric return sym.getTlsDescIdx() * config->wordsize; 65404eeddc0SDimitry Andric } 65504eeddc0SDimitry Andric 65604eeddc0SDimitry Andric uint64_t GotSection::getTlsDescAddr(const Symbol &sym) const { 65704eeddc0SDimitry Andric return getVA() + getTlsDescOffset(sym); 65804eeddc0SDimitry Andric } 65904eeddc0SDimitry Andric 6600b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const { 66104eeddc0SDimitry Andric return this->getVA() + b.getTlsGdIdx() * config->wordsize; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const { 66504eeddc0SDimitry Andric return b.getTlsGdIdx() * config->wordsize; 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric void GotSection::finalizeContents() { 669fe6060f1SDimitry Andric if (config->emachine == EM_PPC64 && 670fe6060f1SDimitry Andric numEntries <= target->gotHeaderEntriesNum && !ElfSym::globalOffsetTable) 671fe6060f1SDimitry Andric size = 0; 672fe6060f1SDimitry Andric else 6730b57cec5SDimitry Andric size = numEntries * config->wordsize; 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric bool GotSection::isNeeded() const { 677fe6060f1SDimitry Andric // Needed if the GOT symbol is used or the number of entries is more than just 678fe6060f1SDimitry Andric // the header. A GOT with just the header may not be needed. 679fe6060f1SDimitry Andric return hasGotOffRel || numEntries > target->gotHeaderEntriesNum; 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric void GotSection::writeTo(uint8_t *buf) { 68361cfbce3SDimitry Andric // On PPC64 .got may be needed but empty. Skip the write. 68461cfbce3SDimitry Andric if (size == 0) 68561cfbce3SDimitry Andric return; 6860b57cec5SDimitry Andric target->writeGotHeader(buf); 687*bdd1243dSDimitry Andric target->relocateAlloc(*this, buf); 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric static uint64_t getMipsPageAddr(uint64_t addr) { 6910b57cec5SDimitry Andric return (addr + 0x8000) & ~0xffff; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric static uint64_t getMipsPageCount(uint64_t size) { 6950b57cec5SDimitry Andric return (size + 0xfffe) / 0xffff + 1; 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric MipsGotSection::MipsGotSection() 6990b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, 7000b57cec5SDimitry Andric ".got") {} 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend, 7030b57cec5SDimitry Andric RelExpr expr) { 7040b57cec5SDimitry Andric FileGot &g = getGot(file); 7050b57cec5SDimitry Andric if (expr == R_MIPS_GOT_LOCAL_PAGE) { 7060b57cec5SDimitry Andric if (const OutputSection *os = sym.getOutputSection()) 7070b57cec5SDimitry Andric g.pagesMap.insert({os, {}}); 7080b57cec5SDimitry Andric else 7090b57cec5SDimitry Andric g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0}); 7100b57cec5SDimitry Andric } else if (sym.isTls()) 7110b57cec5SDimitry Andric g.tls.insert({&sym, 0}); 7120b57cec5SDimitry Andric else if (sym.isPreemptible && expr == R_ABS) 7130b57cec5SDimitry Andric g.relocs.insert({&sym, 0}); 7140b57cec5SDimitry Andric else if (sym.isPreemptible) 7150b57cec5SDimitry Andric g.global.insert({&sym, 0}); 7160b57cec5SDimitry Andric else if (expr == R_MIPS_GOT_OFF32) 7170b57cec5SDimitry Andric g.local32.insert({{&sym, addend}, 0}); 7180b57cec5SDimitry Andric else 7190b57cec5SDimitry Andric g.local16.insert({{&sym, addend}, 0}); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) { 7230b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({&sym, 0}); 7240b57cec5SDimitry Andric } 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric void MipsGotSection::addTlsIndex(InputFile &file) { 7270b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({nullptr, 0}); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getEntriesNum() const { 7310b57cec5SDimitry Andric return getPageEntriesNum() + local16.size() + global.size() + relocs.size() + 7320b57cec5SDimitry Andric tls.size() + dynTlsSymbols.size() * 2; 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getPageEntriesNum() const { 7360b57cec5SDimitry Andric size_t num = 0; 7370b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap) 7380b57cec5SDimitry Andric num += p.second.count; 7390b57cec5SDimitry Andric return num; 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getIndexedEntriesNum() const { 7430b57cec5SDimitry Andric size_t count = getPageEntriesNum() + local16.size() + global.size(); 7440b57cec5SDimitry Andric // If there are relocation-only entries in the GOT, TLS entries 7450b57cec5SDimitry Andric // are allocated after them. TLS entries should be addressable 7460b57cec5SDimitry Andric // by 16-bit index so count both reloc-only and TLS entries. 7470b57cec5SDimitry Andric if (!tls.empty() || !dynTlsSymbols.empty()) 7480b57cec5SDimitry Andric count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2; 7490b57cec5SDimitry Andric return count; 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) { 7530eae32dcSDimitry Andric if (f.mipsGotIndex == uint32_t(-1)) { 7540b57cec5SDimitry Andric gots.emplace_back(); 7550b57cec5SDimitry Andric gots.back().file = &f; 7560b57cec5SDimitry Andric f.mipsGotIndex = gots.size() - 1; 7570b57cec5SDimitry Andric } 7580eae32dcSDimitry Andric return gots[f.mipsGotIndex]; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f, 7620b57cec5SDimitry Andric const Symbol &sym, 7630b57cec5SDimitry Andric int64_t addend) const { 7640eae32dcSDimitry Andric const FileGot &g = gots[f->mipsGotIndex]; 7650b57cec5SDimitry Andric uint64_t index = 0; 7660b57cec5SDimitry Andric if (const OutputSection *outSec = sym.getOutputSection()) { 7670b57cec5SDimitry Andric uint64_t secAddr = getMipsPageAddr(outSec->addr); 7680b57cec5SDimitry Andric uint64_t symAddr = getMipsPageAddr(sym.getVA(addend)); 7690b57cec5SDimitry Andric index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff; 7700b57cec5SDimitry Andric } else { 7710b57cec5SDimitry Andric index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))}); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric return index * config->wordsize; 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s, 7770b57cec5SDimitry Andric int64_t addend) const { 7780eae32dcSDimitry Andric const FileGot &g = gots[f->mipsGotIndex]; 7790b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 7800b57cec5SDimitry Andric if (sym->isTls()) 7810b57cec5SDimitry Andric return g.tls.lookup(sym) * config->wordsize; 7820b57cec5SDimitry Andric if (sym->isPreemptible) 7830b57cec5SDimitry Andric return g.global.lookup(sym) * config->wordsize; 7840b57cec5SDimitry Andric return g.local16.lookup({sym, addend}) * config->wordsize; 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const { 7880eae32dcSDimitry Andric const FileGot &g = gots[f->mipsGotIndex]; 7890b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(nullptr) * config->wordsize; 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f, 7930b57cec5SDimitry Andric const Symbol &s) const { 7940eae32dcSDimitry Andric const FileGot &g = gots[f->mipsGotIndex]; 7950b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 7960b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(sym) * config->wordsize; 7970b57cec5SDimitry Andric } 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric const Symbol *MipsGotSection::getFirstGlobalEntry() const { 8000b57cec5SDimitry Andric if (gots.empty()) 8010b57cec5SDimitry Andric return nullptr; 8020b57cec5SDimitry Andric const FileGot &primGot = gots.front(); 8030b57cec5SDimitry Andric if (!primGot.global.empty()) 8040b57cec5SDimitry Andric return primGot.global.front().first; 8050b57cec5SDimitry Andric if (!primGot.relocs.empty()) 8060b57cec5SDimitry Andric return primGot.relocs.front().first; 8070b57cec5SDimitry Andric return nullptr; 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric unsigned MipsGotSection::getLocalEntriesNum() const { 8110b57cec5SDimitry Andric if (gots.empty()) 8120b57cec5SDimitry Andric return headerEntriesNum; 8130b57cec5SDimitry Andric return headerEntriesNum + gots.front().getPageEntriesNum() + 8140b57cec5SDimitry Andric gots.front().local16.size(); 8150b57cec5SDimitry Andric } 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) { 8180b57cec5SDimitry Andric FileGot tmp = dst; 8190b57cec5SDimitry Andric set_union(tmp.pagesMap, src.pagesMap); 8200b57cec5SDimitry Andric set_union(tmp.local16, src.local16); 8210b57cec5SDimitry Andric set_union(tmp.global, src.global); 8220b57cec5SDimitry Andric set_union(tmp.relocs, src.relocs); 8230b57cec5SDimitry Andric set_union(tmp.tls, src.tls); 8240b57cec5SDimitry Andric set_union(tmp.dynTlsSymbols, src.dynTlsSymbols); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric size_t count = isPrimary ? headerEntriesNum : 0; 8270b57cec5SDimitry Andric count += tmp.getIndexedEntriesNum(); 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric if (count * config->wordsize > config->mipsGotSize) 8300b57cec5SDimitry Andric return false; 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric std::swap(tmp, dst); 8330b57cec5SDimitry Andric return true; 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric void MipsGotSection::finalizeContents() { updateAllocSize(); } 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric bool MipsGotSection::updateAllocSize() { 8390b57cec5SDimitry Andric size = headerEntriesNum * config->wordsize; 8400b57cec5SDimitry Andric for (const FileGot &g : gots) 8410b57cec5SDimitry Andric size += g.getEntriesNum() * config->wordsize; 8420b57cec5SDimitry Andric return false; 8430b57cec5SDimitry Andric } 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric void MipsGotSection::build() { 8460b57cec5SDimitry Andric if (gots.empty()) 8470b57cec5SDimitry Andric return; 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric std::vector<FileGot> mergedGots(1); 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric // For each GOT move non-preemptible symbols from the `Global` 8520b57cec5SDimitry Andric // to `Local16` list. Preemptible symbol might become non-preemptible 8530b57cec5SDimitry Andric // one if, for example, it gets a related copy relocation. 8540b57cec5SDimitry Andric for (FileGot &got : gots) { 8550b57cec5SDimitry Andric for (auto &p: got.global) 8560b57cec5SDimitry Andric if (!p.first->isPreemptible) 8570b57cec5SDimitry Andric got.local16.insert({{p.first, 0}, 0}); 8580b57cec5SDimitry Andric got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) { 8590b57cec5SDimitry Andric return !p.first->isPreemptible; 8600b57cec5SDimitry Andric }); 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric // For each GOT remove "reloc-only" entry if there is "global" 8640b57cec5SDimitry Andric // entry for the same symbol. And add local entries which indexed 8650b57cec5SDimitry Andric // using 32-bit value at the end of 16-bit entries. 8660b57cec5SDimitry Andric for (FileGot &got : gots) { 8670b57cec5SDimitry Andric got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 8680b57cec5SDimitry Andric return got.global.count(p.first); 8690b57cec5SDimitry Andric }); 8700b57cec5SDimitry Andric set_union(got.local16, got.local32); 8710b57cec5SDimitry Andric got.local32.clear(); 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric // Evaluate number of "reloc-only" entries in the resulting GOT. 8750b57cec5SDimitry Andric // To do that put all unique "reloc-only" and "global" entries 8760b57cec5SDimitry Andric // from all GOTs to the future primary GOT. 8770b57cec5SDimitry Andric FileGot *primGot = &mergedGots.front(); 8780b57cec5SDimitry Andric for (FileGot &got : gots) { 8790b57cec5SDimitry Andric set_union(primGot->relocs, got.global); 8800b57cec5SDimitry Andric set_union(primGot->relocs, got.relocs); 8810b57cec5SDimitry Andric got.relocs.clear(); 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric // Evaluate number of "page" entries in each GOT. 8850b57cec5SDimitry Andric for (FileGot &got : gots) { 8860b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 8870b57cec5SDimitry Andric got.pagesMap) { 8880b57cec5SDimitry Andric const OutputSection *os = p.first; 8890b57cec5SDimitry Andric uint64_t secSize = 0; 8904824e7fdSDimitry Andric for (SectionCommand *cmd : os->commands) { 8910b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) 8920b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 893*bdd1243dSDimitry Andric uint64_t off = alignToPowerOf2(secSize, isec->addralign); 8940b57cec5SDimitry Andric secSize = off + isec->getSize(); 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric p.second.count = getMipsPageCount(secSize); 8980b57cec5SDimitry Andric } 8990b57cec5SDimitry Andric } 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric // Merge GOTs. Try to join as much as possible GOTs but do not exceed 9020b57cec5SDimitry Andric // maximum GOT size. At first, try to fill the primary GOT because 9030b57cec5SDimitry Andric // the primary GOT can be accessed in the most effective way. If it 9040b57cec5SDimitry Andric // is not possible, try to fill the last GOT in the list, and finally 9050b57cec5SDimitry Andric // create a new GOT if both attempts failed. 9060b57cec5SDimitry Andric for (FileGot &srcGot : gots) { 9070b57cec5SDimitry Andric InputFile *file = srcGot.file; 9080b57cec5SDimitry Andric if (tryMergeGots(mergedGots.front(), srcGot, true)) { 9090b57cec5SDimitry Andric file->mipsGotIndex = 0; 9100b57cec5SDimitry Andric } else { 9110b57cec5SDimitry Andric // If this is the first time we failed to merge with the primary GOT, 9120b57cec5SDimitry Andric // MergedGots.back() will also be the primary GOT. We must make sure not 9130b57cec5SDimitry Andric // to try to merge again with isPrimary=false, as otherwise, if the 9140b57cec5SDimitry Andric // inputs are just right, we could allow the primary GOT to become 1 or 2 9150b57cec5SDimitry Andric // words bigger due to ignoring the header size. 9160b57cec5SDimitry Andric if (mergedGots.size() == 1 || 9170b57cec5SDimitry Andric !tryMergeGots(mergedGots.back(), srcGot, false)) { 9180b57cec5SDimitry Andric mergedGots.emplace_back(); 9190b57cec5SDimitry Andric std::swap(mergedGots.back(), srcGot); 9200b57cec5SDimitry Andric } 9210b57cec5SDimitry Andric file->mipsGotIndex = mergedGots.size() - 1; 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric std::swap(gots, mergedGots); 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric // Reduce number of "reloc-only" entries in the primary GOT 927480093f4SDimitry Andric // by subtracting "global" entries in the primary GOT. 9280b57cec5SDimitry Andric primGot = &gots.front(); 9290b57cec5SDimitry Andric primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 9300b57cec5SDimitry Andric return primGot->global.count(p.first); 9310b57cec5SDimitry Andric }); 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric // Calculate indexes for each GOT entry. 9340b57cec5SDimitry Andric size_t index = headerEntriesNum; 9350b57cec5SDimitry Andric for (FileGot &got : gots) { 9360b57cec5SDimitry Andric got.startIndex = &got == primGot ? 0 : index; 9370b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 9380b57cec5SDimitry Andric got.pagesMap) { 9390b57cec5SDimitry Andric // For each output section referenced by GOT page relocations calculate 9400b57cec5SDimitry Andric // and save into pagesMap an upper bound of MIPS GOT entries required 9410b57cec5SDimitry Andric // to store page addresses of local symbols. We assume the worst case - 9420b57cec5SDimitry Andric // each 64kb page of the output section has at least one GOT relocation 9430b57cec5SDimitry Andric // against it. And take in account the case when the section intersects 9440b57cec5SDimitry Andric // page boundaries. 9450b57cec5SDimitry Andric p.second.firstIndex = index; 9460b57cec5SDimitry Andric index += p.second.count; 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric for (auto &p: got.local16) 9490b57cec5SDimitry Andric p.second = index++; 9500b57cec5SDimitry Andric for (auto &p: got.global) 9510b57cec5SDimitry Andric p.second = index++; 9520b57cec5SDimitry Andric for (auto &p: got.relocs) 9530b57cec5SDimitry Andric p.second = index++; 9540b57cec5SDimitry Andric for (auto &p: got.tls) 9550b57cec5SDimitry Andric p.second = index++; 9560b57cec5SDimitry Andric for (auto &p: got.dynTlsSymbols) { 9570b57cec5SDimitry Andric p.second = index; 9580b57cec5SDimitry Andric index += 2; 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 96204eeddc0SDimitry Andric // Update SymbolAux::gotIdx field to use this 9630b57cec5SDimitry Andric // value later in the `sortMipsSymbols` function. 96404eeddc0SDimitry Andric for (auto &p : primGot->global) { 965*bdd1243dSDimitry Andric if (p.first->auxIdx == 0) 96604eeddc0SDimitry Andric p.first->allocateAux(); 96704eeddc0SDimitry Andric symAux.back().gotIdx = p.second; 96804eeddc0SDimitry Andric } 96904eeddc0SDimitry Andric for (auto &p : primGot->relocs) { 970*bdd1243dSDimitry Andric if (p.first->auxIdx == 0) 97104eeddc0SDimitry Andric p.first->allocateAux(); 97204eeddc0SDimitry Andric symAux.back().gotIdx = p.second; 97304eeddc0SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric // Create dynamic relocations. 9760b57cec5SDimitry Andric for (FileGot &got : gots) { 9770b57cec5SDimitry Andric // Create dynamic relocations for TLS entries. 9780b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.tls) { 9790b57cec5SDimitry Andric Symbol *s = p.first; 9800b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 981fe6060f1SDimitry Andric // When building a shared library we still need a dynamic relocation 982fe6060f1SDimitry Andric // for the TP-relative offset as we don't know how much other data will 983fe6060f1SDimitry Andric // be allocated before us in the static TLS block. 984fe6060f1SDimitry Andric if (s->isPreemptible || config->shared) 985fe6060f1SDimitry Andric mainPart->relaDyn->addReloc({target->tlsGotRel, this, offset, 986fe6060f1SDimitry Andric DynamicReloc::AgainstSymbolWithTargetVA, 987fe6060f1SDimitry Andric *s, 0, R_ABS}); 9880b57cec5SDimitry Andric } 9890b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) { 9900b57cec5SDimitry Andric Symbol *s = p.first; 9910b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 9920b57cec5SDimitry Andric if (s == nullptr) { 993fe6060f1SDimitry Andric if (!config->shared) 9940b57cec5SDimitry Andric continue; 995fe6060f1SDimitry Andric mainPart->relaDyn->addReloc({target->tlsModuleIndexRel, this, offset}); 9960b57cec5SDimitry Andric } else { 9970b57cec5SDimitry Andric // When building a shared library we still need a dynamic relocation 9980b57cec5SDimitry Andric // for the module index. Therefore only checking for 9990b57cec5SDimitry Andric // S->isPreemptible is not sufficient (this happens e.g. for 10000b57cec5SDimitry Andric // thread-locals that have been marked as local through a linker script) 1001fe6060f1SDimitry Andric if (!s->isPreemptible && !config->shared) 10020b57cec5SDimitry Andric continue; 10030eae32dcSDimitry Andric mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *this, 1004fe6060f1SDimitry Andric offset, *s); 10050b57cec5SDimitry Andric // However, we can skip writing the TLS offset reloc for non-preemptible 10060b57cec5SDimitry Andric // symbols since it is known even in shared libraries 10070b57cec5SDimitry Andric if (!s->isPreemptible) 10080b57cec5SDimitry Andric continue; 10090b57cec5SDimitry Andric offset += config->wordsize; 10100eae32dcSDimitry Andric mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *this, offset, 1011fe6060f1SDimitry Andric *s); 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric } 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric // Do not create dynamic relocations for non-TLS 10160b57cec5SDimitry Andric // entries in the primary GOT. 10170b57cec5SDimitry Andric if (&got == primGot) 10180b57cec5SDimitry Andric continue; 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric // Dynamic relocations for "global" entries. 10210b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : got.global) { 10220b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 10230eae32dcSDimitry Andric mainPart->relaDyn->addSymbolReloc(target->relativeRel, *this, offset, 1024fe6060f1SDimitry Andric *p.first); 10250b57cec5SDimitry Andric } 10260b57cec5SDimitry Andric if (!config->isPic) 10270b57cec5SDimitry Andric continue; 10280b57cec5SDimitry Andric // Dynamic relocations for "local" entries in case of PIC. 10290b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 10300b57cec5SDimitry Andric got.pagesMap) { 10310b57cec5SDimitry Andric size_t pageCount = l.second.count; 10320b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) { 10330b57cec5SDimitry Andric uint64_t offset = (l.second.firstIndex + pi) * config->wordsize; 10340b57cec5SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first, 10350b57cec5SDimitry Andric int64_t(pi * 0x10000)}); 10360b57cec5SDimitry Andric } 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : got.local16) { 10390b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 1040fe6060f1SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, 1041fe6060f1SDimitry Andric DynamicReloc::AddendOnlyWithTargetVA, 1042fe6060f1SDimitry Andric *p.first.first, p.first.second, R_ABS}); 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric } 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric bool MipsGotSection::isNeeded() const { 10480b57cec5SDimitry Andric // We add the .got section to the result for dynamic MIPS target because 10490b57cec5SDimitry Andric // its address and properties are mentioned in the .dynamic section. 10500b57cec5SDimitry Andric return !config->relocatable; 10510b57cec5SDimitry Andric } 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric uint64_t MipsGotSection::getGp(const InputFile *f) const { 10540b57cec5SDimitry Andric // For files without related GOT or files refer a primary GOT 10550b57cec5SDimitry Andric // returns "common" _gp value. For secondary GOTs calculate 10560b57cec5SDimitry Andric // individual _gp values. 10570eae32dcSDimitry Andric if (!f || f->mipsGotIndex == uint32_t(-1) || f->mipsGotIndex == 0) 10580b57cec5SDimitry Andric return ElfSym::mipsGp->getVA(0); 10590eae32dcSDimitry Andric return getVA() + gots[f->mipsGotIndex].startIndex * config->wordsize + 0x7ff0; 10600b57cec5SDimitry Andric } 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric void MipsGotSection::writeTo(uint8_t *buf) { 10630b57cec5SDimitry Andric // Set the MSB of the second GOT slot. This is not required by any 10640b57cec5SDimitry Andric // MIPS ABI documentation, though. 10650b57cec5SDimitry Andric // 10660b57cec5SDimitry Andric // There is a comment in glibc saying that "The MSB of got[1] of a 10670b57cec5SDimitry Andric // gnu object is set to identify gnu objects," and in GNU gold it 10680b57cec5SDimitry Andric // says "the second entry will be used by some runtime loaders". 10690b57cec5SDimitry Andric // But how this field is being used is unclear. 10700b57cec5SDimitry Andric // 10710b57cec5SDimitry Andric // We are not really willing to mimic other linkers behaviors 10720b57cec5SDimitry Andric // without understanding why they do that, but because all files 10730b57cec5SDimitry Andric // generated by GNU tools have this special GOT value, and because 10740b57cec5SDimitry Andric // we've been doing this for years, it is probably a safe bet to 10750b57cec5SDimitry Andric // keep doing this for now. We really need to revisit this to see 10760b57cec5SDimitry Andric // if we had to do this. 10770b57cec5SDimitry Andric writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1)); 10780b57cec5SDimitry Andric for (const FileGot &g : gots) { 10790b57cec5SDimitry Andric auto write = [&](size_t i, const Symbol *s, int64_t a) { 10800b57cec5SDimitry Andric uint64_t va = a; 10810b57cec5SDimitry Andric if (s) 10820b57cec5SDimitry Andric va = s->getVA(a); 10830b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, va); 10840b57cec5SDimitry Andric }; 10850b57cec5SDimitry Andric // Write 'page address' entries to the local part of the GOT. 10860b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 10870b57cec5SDimitry Andric g.pagesMap) { 10880b57cec5SDimitry Andric size_t pageCount = l.second.count; 10890b57cec5SDimitry Andric uint64_t firstPageAddr = getMipsPageAddr(l.first->addr); 10900b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) 10910b57cec5SDimitry Andric write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000); 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric // Local, global, TLS, reloc-only entries. 10940b57cec5SDimitry Andric // If TLS entry has a corresponding dynamic relocations, leave it 10950b57cec5SDimitry Andric // initialized by zero. Write down adjusted TLS symbol's values otherwise. 10960b57cec5SDimitry Andric // To calculate the adjustments use offsets for thread-local storage. 1097fe6060f1SDimitry Andric // http://web.archive.org/web/20190324223224/https://www.linux-mips.org/wiki/NPTL 10980b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : g.local16) 10990b57cec5SDimitry Andric write(p.second, p.first.first, p.first.second); 11000b57cec5SDimitry Andric // Write VA to the primary GOT only. For secondary GOTs that 11010b57cec5SDimitry Andric // will be done by REL32 dynamic relocations. 11020b57cec5SDimitry Andric if (&g == &gots.front()) 1103480093f4SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.global) 11040b57cec5SDimitry Andric write(p.second, p.first, 0); 11050b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.relocs) 11060b57cec5SDimitry Andric write(p.second, p.first, 0); 11070b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.tls) 1108fe6060f1SDimitry Andric write(p.second, p.first, 1109fe6060f1SDimitry Andric p.first->isPreemptible || config->shared ? 0 : -0x7000); 11100b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) { 1111fe6060f1SDimitry Andric if (p.first == nullptr && !config->shared) 11120b57cec5SDimitry Andric write(p.second, nullptr, 1); 11130b57cec5SDimitry Andric else if (p.first && !p.first->isPreemptible) { 1114349cc55cSDimitry Andric // If we are emitting a shared library with relocations we mustn't write 11150b57cec5SDimitry Andric // anything to the GOT here. When using Elf_Rel relocations the value 11160b57cec5SDimitry Andric // one will be treated as an addend and will cause crashes at runtime 1117fe6060f1SDimitry Andric if (!config->shared) 11180b57cec5SDimitry Andric write(p.second, nullptr, 1); 11190b57cec5SDimitry Andric write(p.second + 1, p.first, -0x8000); 11200b57cec5SDimitry Andric } 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric // On PowerPC the .plt section is used to hold the table of function addresses 11260b57cec5SDimitry Andric // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss 11270b57cec5SDimitry Andric // section. I don't know why we have a BSS style type for the section but it is 1128480093f4SDimitry Andric // consistent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI. 11290b57cec5SDimitry Andric GotPltSection::GotPltSection() 11300b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 11310b57cec5SDimitry Andric ".got.plt") { 11320b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 11330b57cec5SDimitry Andric name = ".plt"; 11340b57cec5SDimitry Andric } else if (config->emachine == EM_PPC64) { 11350b57cec5SDimitry Andric type = SHT_NOBITS; 11360b57cec5SDimitry Andric name = ".plt"; 11370b57cec5SDimitry Andric } 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric void GotPltSection::addEntry(Symbol &sym) { 114104eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1 && 114204eeddc0SDimitry Andric symAux.back().pltIdx == entries.size()); 11430b57cec5SDimitry Andric entries.push_back(&sym); 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric 11460b57cec5SDimitry Andric size_t GotPltSection::getSize() const { 1147fe6060f1SDimitry Andric return (target->gotPltHeaderEntriesNum + entries.size()) * 1148fe6060f1SDimitry Andric target->gotEntrySize; 11490b57cec5SDimitry Andric } 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric void GotPltSection::writeTo(uint8_t *buf) { 11520b57cec5SDimitry Andric target->writeGotPltHeader(buf); 1153fe6060f1SDimitry Andric buf += target->gotPltHeaderEntriesNum * target->gotEntrySize; 11540b57cec5SDimitry Andric for (const Symbol *b : entries) { 11550b57cec5SDimitry Andric target->writeGotPlt(buf, *b); 1156fe6060f1SDimitry Andric buf += target->gotEntrySize; 11570b57cec5SDimitry Andric } 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric bool GotPltSection::isNeeded() const { 11610b57cec5SDimitry Andric // We need to emit GOTPLT even if it's empty if there's a relocation relative 11620b57cec5SDimitry Andric // to it. 11630b57cec5SDimitry Andric return !entries.empty() || hasGotPltOffRel; 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric static StringRef getIgotPltName() { 11670b57cec5SDimitry Andric // On ARM the IgotPltSection is part of the GotSection. 11680b57cec5SDimitry Andric if (config->emachine == EM_ARM) 11690b57cec5SDimitry Andric return ".got"; 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection 11720b57cec5SDimitry Andric // needs to be named the same. 11730b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 11740b57cec5SDimitry Andric return ".plt"; 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric return ".got.plt"; 11770b57cec5SDimitry Andric } 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit 11800b57cec5SDimitry Andric // with the IgotPltSection. 11810b57cec5SDimitry Andric IgotPltSection::IgotPltSection() 11820b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 11830b57cec5SDimitry Andric config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS, 1184fe6060f1SDimitry Andric target->gotEntrySize, getIgotPltName()) {} 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric void IgotPltSection::addEntry(Symbol &sym) { 118704eeddc0SDimitry Andric assert(symAux.back().pltIdx == entries.size()); 11880b57cec5SDimitry Andric entries.push_back(&sym); 11890b57cec5SDimitry Andric } 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric size_t IgotPltSection::getSize() const { 1192fe6060f1SDimitry Andric return entries.size() * target->gotEntrySize; 11930b57cec5SDimitry Andric } 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric void IgotPltSection::writeTo(uint8_t *buf) { 11960b57cec5SDimitry Andric for (const Symbol *b : entries) { 11970b57cec5SDimitry Andric target->writeIgotPlt(buf, *b); 1198fe6060f1SDimitry Andric buf += target->gotEntrySize; 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric 12020b57cec5SDimitry Andric StringTableSection::StringTableSection(StringRef name, bool dynamic) 12030b57cec5SDimitry Andric : SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name), 12040b57cec5SDimitry Andric dynamic(dynamic) { 12050b57cec5SDimitry Andric // ELF string tables start with a NUL byte. 12061fd87a68SDimitry Andric strings.push_back(""); 1207d781ede6SDimitry Andric stringMap.try_emplace(CachedHashStringRef(""), 0); 12081fd87a68SDimitry Andric size = 1; 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric // Adds a string to the string table. If `hashIt` is true we hash and check for 12120b57cec5SDimitry Andric // duplicates. It is optional because the name of global symbols are already 12130b57cec5SDimitry Andric // uniqued and hashing them again has a big cost for a small value: uniquing 12140b57cec5SDimitry Andric // them with some other string that happens to be the same. 12150b57cec5SDimitry Andric unsigned StringTableSection::addString(StringRef s, bool hashIt) { 12160b57cec5SDimitry Andric if (hashIt) { 121704eeddc0SDimitry Andric auto r = stringMap.try_emplace(CachedHashStringRef(s), size); 12180b57cec5SDimitry Andric if (!r.second) 12190b57cec5SDimitry Andric return r.first->second; 12200b57cec5SDimitry Andric } 12211fd87a68SDimitry Andric if (s.empty()) 12221fd87a68SDimitry Andric return 0; 12230b57cec5SDimitry Andric unsigned ret = this->size; 12240b57cec5SDimitry Andric this->size = this->size + s.size() + 1; 12250b57cec5SDimitry Andric strings.push_back(s); 12260b57cec5SDimitry Andric return ret; 12270b57cec5SDimitry Andric } 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric void StringTableSection::writeTo(uint8_t *buf) { 12300b57cec5SDimitry Andric for (StringRef s : strings) { 12310b57cec5SDimitry Andric memcpy(buf, s.data(), s.size()); 12320b57cec5SDimitry Andric buf[s.size()] = '\0'; 12330b57cec5SDimitry Andric buf += s.size() + 1; 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric } 12360b57cec5SDimitry Andric 123785868e8aSDimitry Andric // Returns the number of entries in .gnu.version_d: the number of 123885868e8aSDimitry Andric // non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1. 123985868e8aSDimitry Andric // Note that we don't support vd_cnt > 1 yet. 124085868e8aSDimitry Andric static unsigned getVerDefNum() { 124185868e8aSDimitry Andric return namedVersionDefs().size() + 1; 124285868e8aSDimitry Andric } 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric template <class ELFT> 12450b57cec5SDimitry Andric DynamicSection<ELFT>::DynamicSection() 12460b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize, 12470b57cec5SDimitry Andric ".dynamic") { 12480b57cec5SDimitry Andric this->entsize = ELFT::Is64Bits ? 16 : 8; 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric // .dynamic section is not writable on MIPS and on Fuchsia OS 12510b57cec5SDimitry Andric // which passes -z rodynamic. 12520b57cec5SDimitry Andric // See "Special Section" in Chapter 4 in the following document: 12530b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 12540b57cec5SDimitry Andric if (config->emachine == EM_MIPS || config->zRodynamic) 12550b57cec5SDimitry Andric this->flags = SHF_ALLOC; 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric 125885868e8aSDimitry Andric // The output section .rela.dyn may include these synthetic sections: 125985868e8aSDimitry Andric // 126085868e8aSDimitry Andric // - part.relaDyn 126185868e8aSDimitry Andric // - in.relaIplt: this is included if in.relaIplt is named .rela.dyn 126285868e8aSDimitry Andric // - in.relaPlt: this is included if a linker script places .rela.plt inside 126385868e8aSDimitry Andric // .rela.dyn 126485868e8aSDimitry Andric // 126585868e8aSDimitry Andric // DT_RELASZ is the total size of the included sections. 126604eeddc0SDimitry Andric static uint64_t addRelaSz(const RelocationBaseSection &relaDyn) { 126704eeddc0SDimitry Andric size_t size = relaDyn.getSize(); 126804eeddc0SDimitry Andric if (in.relaIplt->getParent() == relaDyn.getParent()) 126985868e8aSDimitry Andric size += in.relaIplt->getSize(); 127004eeddc0SDimitry Andric if (in.relaPlt->getParent() == relaDyn.getParent()) 127185868e8aSDimitry Andric size += in.relaPlt->getSize(); 127285868e8aSDimitry Andric return size; 127385868e8aSDimitry Andric } 127485868e8aSDimitry Andric 12750b57cec5SDimitry Andric // A Linker script may assign the RELA relocation sections to the same 12760b57cec5SDimitry Andric // output section. When this occurs we cannot just use the OutputSection 12770b57cec5SDimitry Andric // Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to 12780b57cec5SDimitry Andric // overlap with the [DT_RELA, DT_RELA + DT_RELASZ). 12790b57cec5SDimitry Andric static uint64_t addPltRelSz() { 12800b57cec5SDimitry Andric size_t size = in.relaPlt->getSize(); 12810b57cec5SDimitry Andric if (in.relaIplt->getParent() == in.relaPlt->getParent() && 12820b57cec5SDimitry Andric in.relaIplt->name == in.relaPlt->name) 12830b57cec5SDimitry Andric size += in.relaIplt->getSize(); 12840b57cec5SDimitry Andric return size; 12850b57cec5SDimitry Andric } 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric // Add remaining entries to complete .dynamic contents. 12884824e7fdSDimitry Andric template <class ELFT> 12894824e7fdSDimitry Andric std::vector<std::pair<int32_t, uint64_t>> 12904824e7fdSDimitry Andric DynamicSection<ELFT>::computeContents() { 12915ffd83dbSDimitry Andric elf::Partition &part = getPartition(); 12920b57cec5SDimitry Andric bool isMain = part.name.empty(); 12934824e7fdSDimitry Andric std::vector<std::pair<int32_t, uint64_t>> entries; 12944824e7fdSDimitry Andric 12954824e7fdSDimitry Andric auto addInt = [&](int32_t tag, uint64_t val) { 12964824e7fdSDimitry Andric entries.emplace_back(tag, val); 12974824e7fdSDimitry Andric }; 12980eae32dcSDimitry Andric auto addInSec = [&](int32_t tag, const InputSection &sec) { 12990eae32dcSDimitry Andric entries.emplace_back(tag, sec.getVA()); 13004824e7fdSDimitry Andric }; 13010b57cec5SDimitry Andric 13020b57cec5SDimitry Andric for (StringRef s : config->filterList) 13030b57cec5SDimitry Andric addInt(DT_FILTER, part.dynStrTab->addString(s)); 13040b57cec5SDimitry Andric for (StringRef s : config->auxiliaryList) 13050b57cec5SDimitry Andric addInt(DT_AUXILIARY, part.dynStrTab->addString(s)); 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric if (!config->rpath.empty()) 13080b57cec5SDimitry Andric addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH, 13090b57cec5SDimitry Andric part.dynStrTab->addString(config->rpath)); 13100b57cec5SDimitry Andric 1311*bdd1243dSDimitry Andric for (SharedFile *file : ctx.sharedFiles) 13120b57cec5SDimitry Andric if (file->isNeeded) 13130b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(file->soName)); 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric if (isMain) { 13160b57cec5SDimitry Andric if (!config->soName.empty()) 13170b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(config->soName)); 13180b57cec5SDimitry Andric } else { 13190b57cec5SDimitry Andric if (!config->soName.empty()) 13200b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(config->soName)); 13210b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(part.name)); 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric // Set DT_FLAGS and DT_FLAGS_1. 13250b57cec5SDimitry Andric uint32_t dtFlags = 0; 13260b57cec5SDimitry Andric uint32_t dtFlags1 = 0; 13276e75b2fbSDimitry Andric if (config->bsymbolic == BsymbolicKind::All) 13280b57cec5SDimitry Andric dtFlags |= DF_SYMBOLIC; 13290b57cec5SDimitry Andric if (config->zGlobal) 13300b57cec5SDimitry Andric dtFlags1 |= DF_1_GLOBAL; 13310b57cec5SDimitry Andric if (config->zInitfirst) 13320b57cec5SDimitry Andric dtFlags1 |= DF_1_INITFIRST; 13330b57cec5SDimitry Andric if (config->zInterpose) 13340b57cec5SDimitry Andric dtFlags1 |= DF_1_INTERPOSE; 13350b57cec5SDimitry Andric if (config->zNodefaultlib) 13360b57cec5SDimitry Andric dtFlags1 |= DF_1_NODEFLIB; 13370b57cec5SDimitry Andric if (config->zNodelete) 13380b57cec5SDimitry Andric dtFlags1 |= DF_1_NODELETE; 13390b57cec5SDimitry Andric if (config->zNodlopen) 13400b57cec5SDimitry Andric dtFlags1 |= DF_1_NOOPEN; 1341dfd4db93SEd Maste if (config->pie) 1342dfd4db93SEd Maste dtFlags1 |= DF_1_PIE; 13430b57cec5SDimitry Andric if (config->zNow) { 13440b57cec5SDimitry Andric dtFlags |= DF_BIND_NOW; 13450b57cec5SDimitry Andric dtFlags1 |= DF_1_NOW; 13460b57cec5SDimitry Andric } 13470b57cec5SDimitry Andric if (config->zOrigin) { 13480b57cec5SDimitry Andric dtFlags |= DF_ORIGIN; 13490b57cec5SDimitry Andric dtFlags1 |= DF_1_ORIGIN; 13500b57cec5SDimitry Andric } 13510b57cec5SDimitry Andric if (!config->zText) 13520b57cec5SDimitry Andric dtFlags |= DF_TEXTREL; 1353*bdd1243dSDimitry Andric if (ctx.hasTlsIe && config->shared) 13540b57cec5SDimitry Andric dtFlags |= DF_STATIC_TLS; 13550b57cec5SDimitry Andric 13560b57cec5SDimitry Andric if (dtFlags) 13570b57cec5SDimitry Andric addInt(DT_FLAGS, dtFlags); 13580b57cec5SDimitry Andric if (dtFlags1) 13590b57cec5SDimitry Andric addInt(DT_FLAGS_1, dtFlags1); 13600b57cec5SDimitry Andric 1361480093f4SDimitry Andric // DT_DEBUG is a pointer to debug information used by debuggers at runtime. We 13620b57cec5SDimitry Andric // need it for each process, so we don't write it for DSOs. The loader writes 13630b57cec5SDimitry Andric // the pointer into this entry. 13640b57cec5SDimitry Andric // 13650b57cec5SDimitry Andric // DT_DEBUG is the only .dynamic entry that needs to be written to. Some 13660b57cec5SDimitry Andric // systems (currently only Fuchsia OS) provide other means to give the 13670b57cec5SDimitry Andric // debugger this information. Such systems may choose make .dynamic read-only. 13680b57cec5SDimitry Andric // If the target is such a system (used -z rodynamic) don't write DT_DEBUG. 13690b57cec5SDimitry Andric if (!config->shared && !config->relocatable && !config->zRodynamic) 13700b57cec5SDimitry Andric addInt(DT_DEBUG, 0); 13710b57cec5SDimitry Andric 137285868e8aSDimitry Andric if (part.relaDyn->isNeeded() || 137385868e8aSDimitry Andric (in.relaIplt->isNeeded() && 137485868e8aSDimitry Andric part.relaDyn->getParent() == in.relaIplt->getParent())) { 13750eae32dcSDimitry Andric addInSec(part.relaDyn->dynamicTag, *part.relaDyn); 137604eeddc0SDimitry Andric entries.emplace_back(part.relaDyn->sizeDynamicTag, 137704eeddc0SDimitry Andric addRelaSz(*part.relaDyn)); 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric bool isRela = config->isRela; 13800b57cec5SDimitry Andric addInt(isRela ? DT_RELAENT : DT_RELENT, 13810b57cec5SDimitry Andric isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric // MIPS dynamic loader does not support RELCOUNT tag. 13840b57cec5SDimitry Andric // The problem is in the tight relation between dynamic 13850b57cec5SDimitry Andric // relocations and GOT. So do not emit this tag on MIPS. 13860b57cec5SDimitry Andric if (config->emachine != EM_MIPS) { 13870b57cec5SDimitry Andric size_t numRelativeRels = part.relaDyn->getRelativeRelocCount(); 13880b57cec5SDimitry Andric if (config->zCombreloc && numRelativeRels) 13890b57cec5SDimitry Andric addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels); 13900b57cec5SDimitry Andric } 13910b57cec5SDimitry Andric } 139204eeddc0SDimitry Andric if (part.relrDyn && part.relrDyn->getParent() && 139304eeddc0SDimitry Andric !part.relrDyn->relocs.empty()) { 13940b57cec5SDimitry Andric addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR, 13950eae32dcSDimitry Andric *part.relrDyn); 13964824e7fdSDimitry Andric addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ, 13974824e7fdSDimitry Andric part.relrDyn->getParent()->size); 13980b57cec5SDimitry Andric addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT, 13990b57cec5SDimitry Andric sizeof(Elf_Relr)); 14000b57cec5SDimitry Andric } 14010b57cec5SDimitry Andric // .rel[a].plt section usually consists of two parts, containing plt and 14020b57cec5SDimitry Andric // iplt relocations. It is possible to have only iplt relocations in the 14030b57cec5SDimitry Andric // output. In that case relaPlt is empty and have zero offset, the same offset 14040b57cec5SDimitry Andric // as relaIplt has. And we still want to emit proper dynamic tags for that 1405480093f4SDimitry Andric // case, so here we always use relaPlt as marker for the beginning of 14060b57cec5SDimitry Andric // .rel[a].plt section. 14070b57cec5SDimitry Andric if (isMain && (in.relaPlt->isNeeded() || in.relaIplt->isNeeded())) { 14080eae32dcSDimitry Andric addInSec(DT_JMPREL, *in.relaPlt); 14094824e7fdSDimitry Andric entries.emplace_back(DT_PLTRELSZ, addPltRelSz()); 14100b57cec5SDimitry Andric switch (config->emachine) { 14110b57cec5SDimitry Andric case EM_MIPS: 14120eae32dcSDimitry Andric addInSec(DT_MIPS_PLTGOT, *in.gotPlt); 14130b57cec5SDimitry Andric break; 14140b57cec5SDimitry Andric case EM_SPARCV9: 14150eae32dcSDimitry Andric addInSec(DT_PLTGOT, *in.plt); 14160b57cec5SDimitry Andric break; 1417e8d8bef9SDimitry Andric case EM_AARCH64: 1418e8d8bef9SDimitry Andric if (llvm::find_if(in.relaPlt->relocs, [](const DynamicReloc &r) { 1419e8d8bef9SDimitry Andric return r.type == target->pltRel && 1420e8d8bef9SDimitry Andric r.sym->stOther & STO_AARCH64_VARIANT_PCS; 1421e8d8bef9SDimitry Andric }) != in.relaPlt->relocs.end()) 1422e8d8bef9SDimitry Andric addInt(DT_AARCH64_VARIANT_PCS, 0); 1423*bdd1243dSDimitry Andric addInSec(DT_PLTGOT, *in.gotPlt); 1424*bdd1243dSDimitry Andric break; 1425*bdd1243dSDimitry Andric case EM_RISCV: 1426*bdd1243dSDimitry Andric if (llvm::any_of(in.relaPlt->relocs, [](const DynamicReloc &r) { 1427*bdd1243dSDimitry Andric return r.type == target->pltRel && 1428*bdd1243dSDimitry Andric (r.sym->stOther & STO_RISCV_VARIANT_CC); 1429*bdd1243dSDimitry Andric })) 1430*bdd1243dSDimitry Andric addInt(DT_RISCV_VARIANT_CC, 0); 1431*bdd1243dSDimitry Andric [[fallthrough]]; 14320b57cec5SDimitry Andric default: 14330eae32dcSDimitry Andric addInSec(DT_PLTGOT, *in.gotPlt); 14340b57cec5SDimitry Andric break; 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric addInt(DT_PLTREL, config->isRela ? DT_RELA : DT_REL); 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric 14390b57cec5SDimitry Andric if (config->emachine == EM_AARCH64) { 14400b57cec5SDimitry Andric if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 14410b57cec5SDimitry Andric addInt(DT_AARCH64_BTI_PLT, 0); 14425ffd83dbSDimitry Andric if (config->zPacPlt) 14430b57cec5SDimitry Andric addInt(DT_AARCH64_PAC_PLT, 0); 14440b57cec5SDimitry Andric } 14450b57cec5SDimitry Andric 14460eae32dcSDimitry Andric addInSec(DT_SYMTAB, *part.dynSymTab); 14470b57cec5SDimitry Andric addInt(DT_SYMENT, sizeof(Elf_Sym)); 14480eae32dcSDimitry Andric addInSec(DT_STRTAB, *part.dynStrTab); 14490b57cec5SDimitry Andric addInt(DT_STRSZ, part.dynStrTab->getSize()); 14500b57cec5SDimitry Andric if (!config->zText) 14510b57cec5SDimitry Andric addInt(DT_TEXTREL, 0); 145204eeddc0SDimitry Andric if (part.gnuHashTab && part.gnuHashTab->getParent()) 14530eae32dcSDimitry Andric addInSec(DT_GNU_HASH, *part.gnuHashTab); 145404eeddc0SDimitry Andric if (part.hashTab && part.hashTab->getParent()) 14550eae32dcSDimitry Andric addInSec(DT_HASH, *part.hashTab); 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric if (isMain) { 14580b57cec5SDimitry Andric if (Out::preinitArray) { 14594824e7fdSDimitry Andric addInt(DT_PREINIT_ARRAY, Out::preinitArray->addr); 14604824e7fdSDimitry Andric addInt(DT_PREINIT_ARRAYSZ, Out::preinitArray->size); 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric if (Out::initArray) { 14634824e7fdSDimitry Andric addInt(DT_INIT_ARRAY, Out::initArray->addr); 14644824e7fdSDimitry Andric addInt(DT_INIT_ARRAYSZ, Out::initArray->size); 14650b57cec5SDimitry Andric } 14660b57cec5SDimitry Andric if (Out::finiArray) { 14674824e7fdSDimitry Andric addInt(DT_FINI_ARRAY, Out::finiArray->addr); 14684824e7fdSDimitry Andric addInt(DT_FINI_ARRAYSZ, Out::finiArray->size); 14690b57cec5SDimitry Andric } 14700b57cec5SDimitry Andric 1471*bdd1243dSDimitry Andric if (Symbol *b = symtab.find(config->init)) 14720b57cec5SDimitry Andric if (b->isDefined()) 14734824e7fdSDimitry Andric addInt(DT_INIT, b->getVA()); 1474*bdd1243dSDimitry Andric if (Symbol *b = symtab.find(config->fini)) 14750b57cec5SDimitry Andric if (b->isDefined()) 14764824e7fdSDimitry Andric addInt(DT_FINI, b->getVA()); 14770b57cec5SDimitry Andric } 14780b57cec5SDimitry Andric 1479480093f4SDimitry Andric if (part.verSym && part.verSym->isNeeded()) 14800eae32dcSDimitry Andric addInSec(DT_VERSYM, *part.verSym); 1481480093f4SDimitry Andric if (part.verDef && part.verDef->isLive()) { 14820eae32dcSDimitry Andric addInSec(DT_VERDEF, *part.verDef); 14830b57cec5SDimitry Andric addInt(DT_VERDEFNUM, getVerDefNum()); 14840b57cec5SDimitry Andric } 1485480093f4SDimitry Andric if (part.verNeed && part.verNeed->isNeeded()) { 14860eae32dcSDimitry Andric addInSec(DT_VERNEED, *part.verNeed); 14870b57cec5SDimitry Andric unsigned needNum = 0; 1488*bdd1243dSDimitry Andric for (SharedFile *f : ctx.sharedFiles) 14890b57cec5SDimitry Andric if (!f->vernauxs.empty()) 14900b57cec5SDimitry Andric ++needNum; 14910b57cec5SDimitry Andric addInt(DT_VERNEEDNUM, needNum); 14920b57cec5SDimitry Andric } 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 14950b57cec5SDimitry Andric addInt(DT_MIPS_RLD_VERSION, 1); 14960b57cec5SDimitry Andric addInt(DT_MIPS_FLAGS, RHF_NOTPOT); 14970b57cec5SDimitry Andric addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase()); 14980b57cec5SDimitry Andric addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols()); 14994824e7fdSDimitry Andric addInt(DT_MIPS_LOCAL_GOTNO, in.mipsGot->getLocalEntriesNum()); 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric if (const Symbol *b = in.mipsGot->getFirstGlobalEntry()) 15020b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, b->dynsymIndex); 15030b57cec5SDimitry Andric else 15040b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols()); 15050eae32dcSDimitry Andric addInSec(DT_PLTGOT, *in.mipsGot); 15060b57cec5SDimitry Andric if (in.mipsRldMap) { 15070b57cec5SDimitry Andric if (!config->pie) 15080eae32dcSDimitry Andric addInSec(DT_MIPS_RLD_MAP, *in.mipsRldMap); 15090b57cec5SDimitry Andric // Store the offset to the .rld_map section 15100b57cec5SDimitry Andric // relative to the address of the tag. 15114824e7fdSDimitry Andric addInt(DT_MIPS_RLD_MAP_REL, 15124824e7fdSDimitry Andric in.mipsRldMap->getVA() - (getVA() + entries.size() * entsize)); 15130b57cec5SDimitry Andric } 15140b57cec5SDimitry Andric } 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent, 15170b57cec5SDimitry Andric // glibc assumes the old-style BSS PLT layout which we don't support. 15180b57cec5SDimitry Andric if (config->emachine == EM_PPC) 15190eae32dcSDimitry Andric addInSec(DT_PPC_GOT, *in.got); 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric // Glink dynamic tag is required by the V2 abi if the plt section isn't empty. 15220b57cec5SDimitry Andric if (config->emachine == EM_PPC64 && in.plt->isNeeded()) { 15230b57cec5SDimitry Andric // The Glink tag points to 32 bytes before the first lazy symbol resolution 15240b57cec5SDimitry Andric // stub, which starts directly after the header. 15254824e7fdSDimitry Andric addInt(DT_PPC64_GLINK, in.plt->getVA() + target->pltHeaderSize - 32); 15260b57cec5SDimitry Andric } 15270b57cec5SDimitry Andric 15280b57cec5SDimitry Andric addInt(DT_NULL, 0); 15294824e7fdSDimitry Andric return entries; 15304824e7fdSDimitry Andric } 15310b57cec5SDimitry Andric 15324824e7fdSDimitry Andric template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { 15334824e7fdSDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 15344824e7fdSDimitry Andric getParent()->link = sec->sectionIndex; 15354824e7fdSDimitry Andric this->size = computeContents().size() * this->entsize; 15360b57cec5SDimitry Andric } 15370b57cec5SDimitry Andric 15380b57cec5SDimitry Andric template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) { 15390b57cec5SDimitry Andric auto *p = reinterpret_cast<Elf_Dyn *>(buf); 15400b57cec5SDimitry Andric 15414824e7fdSDimitry Andric for (std::pair<int32_t, uint64_t> kv : computeContents()) { 15420b57cec5SDimitry Andric p->d_tag = kv.first; 15434824e7fdSDimitry Andric p->d_un.d_val = kv.second; 15440b57cec5SDimitry Andric ++p; 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric } 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric uint64_t DynamicReloc::getOffset() const { 15490b57cec5SDimitry Andric return inputSec->getVA(offsetInSec); 15500b57cec5SDimitry Andric } 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric int64_t DynamicReloc::computeAddend() const { 1553fe6060f1SDimitry Andric switch (kind) { 1554fe6060f1SDimitry Andric case AddendOnly: 1555fe6060f1SDimitry Andric assert(sym == nullptr); 15560b57cec5SDimitry Andric return addend; 1557fe6060f1SDimitry Andric case AgainstSymbol: 1558fe6060f1SDimitry Andric assert(sym != nullptr); 1559fe6060f1SDimitry Andric return addend; 1560fe6060f1SDimitry Andric case AddendOnlyWithTargetVA: 1561fe6060f1SDimitry Andric case AgainstSymbolWithTargetVA: 1562fe6060f1SDimitry Andric return InputSection::getRelocTargetVA(inputSec->file, type, addend, 1563fe6060f1SDimitry Andric getOffset(), *sym, expr); 1564fe6060f1SDimitry Andric case MipsMultiGotPage: 1565fe6060f1SDimitry Andric assert(sym == nullptr); 15660b57cec5SDimitry Andric return getMipsPageAddr(outputSec->addr) + addend; 15670b57cec5SDimitry Andric } 1568fe6060f1SDimitry Andric llvm_unreachable("Unknown DynamicReloc::Kind enum"); 1569fe6060f1SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const { 157281ad6265SDimitry Andric if (!needsDynSymIndex()) 15730b57cec5SDimitry Andric return 0; 157481ad6265SDimitry Andric 157581ad6265SDimitry Andric size_t index = symTab->getSymbolIndex(sym); 157681ad6265SDimitry Andric assert((index != 0 || (type != target->gotRel && type != target->pltRel) || 157781ad6265SDimitry Andric !mainPart->dynSymTab->getParent()) && 157881ad6265SDimitry Andric "GOT or PLT relocation must refer to symbol in dynamic symbol table"); 157981ad6265SDimitry Andric return index; 15800b57cec5SDimitry Andric } 15810b57cec5SDimitry Andric 15820b57cec5SDimitry Andric RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type, 15830b57cec5SDimitry Andric int32_t dynamicTag, 15841fd87a68SDimitry Andric int32_t sizeDynamicTag, 1585*bdd1243dSDimitry Andric bool combreloc, 1586*bdd1243dSDimitry Andric unsigned concurrency) 15870b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, type, config->wordsize, name), 15881fd87a68SDimitry Andric dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag), 1589*bdd1243dSDimitry Andric relocsVec(concurrency), combreloc(combreloc) {} 15900b57cec5SDimitry Andric 1591*bdd1243dSDimitry Andric void RelocationBaseSection::addSymbolReloc( 1592*bdd1243dSDimitry Andric RelType dynType, InputSectionBase &isec, uint64_t offsetInSec, Symbol &sym, 1593*bdd1243dSDimitry Andric int64_t addend, std::optional<RelType> addendRelType) { 1594fe6060f1SDimitry Andric addReloc(DynamicReloc::AgainstSymbol, dynType, isec, offsetInSec, sym, addend, 1595fe6060f1SDimitry Andric R_ADDEND, addendRelType ? *addendRelType : target->noneRel); 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric 1598fe6060f1SDimitry Andric void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible( 1599*bdd1243dSDimitry Andric RelType dynType, GotSection &sec, uint64_t offsetInSec, Symbol &sym, 1600fe6060f1SDimitry Andric RelType addendRelType) { 1601fe6060f1SDimitry Andric // No need to write an addend to the section for preemptible symbols. 1602fe6060f1SDimitry Andric if (sym.isPreemptible) 1603*bdd1243dSDimitry Andric addReloc({dynType, &sec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0, 1604fe6060f1SDimitry Andric R_ABS}); 1605fe6060f1SDimitry Andric else 1606*bdd1243dSDimitry Andric addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, sec, offsetInSec, 1607fe6060f1SDimitry Andric sym, 0, R_ABS, addendRelType); 1608fe6060f1SDimitry Andric } 1609fe6060f1SDimitry Andric 1610*bdd1243dSDimitry Andric void RelocationBaseSection::mergeRels() { 1611*bdd1243dSDimitry Andric size_t newSize = relocs.size(); 1612*bdd1243dSDimitry Andric for (const auto &v : relocsVec) 1613*bdd1243dSDimitry Andric newSize += v.size(); 1614*bdd1243dSDimitry Andric relocs.reserve(newSize); 1615*bdd1243dSDimitry Andric for (const auto &v : relocsVec) 1616*bdd1243dSDimitry Andric llvm::append_range(relocs, v); 1617*bdd1243dSDimitry Andric relocsVec.clear(); 16180b57cec5SDimitry Andric } 16190b57cec5SDimitry Andric 16201fd87a68SDimitry Andric void RelocationBaseSection::partitionRels() { 16211fd87a68SDimitry Andric if (!combreloc) 16221fd87a68SDimitry Andric return; 16231fd87a68SDimitry Andric const RelType relativeRel = target->relativeRel; 16241fd87a68SDimitry Andric numRelativeRelocs = 16251fd87a68SDimitry Andric llvm::partition(relocs, [=](auto &r) { return r.type == relativeRel; }) - 16261fd87a68SDimitry Andric relocs.begin(); 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric 16290b57cec5SDimitry Andric void RelocationBaseSection::finalizeContents() { 163004eeddc0SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab.get(); 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE 16330b57cec5SDimitry Andric // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that 16340b57cec5SDimitry Andric // case. 16350b57cec5SDimitry Andric if (symTab && symTab->getParent()) 16360b57cec5SDimitry Andric getParent()->link = symTab->getParent()->sectionIndex; 16370b57cec5SDimitry Andric else 16380b57cec5SDimitry Andric getParent()->link = 0; 16390b57cec5SDimitry Andric 164004eeddc0SDimitry Andric if (in.relaPlt.get() == this && in.gotPlt->getParent()) { 1641e8d8bef9SDimitry Andric getParent()->flags |= ELF::SHF_INFO_LINK; 16420b57cec5SDimitry Andric getParent()->info = in.gotPlt->getParent()->sectionIndex; 1643e8d8bef9SDimitry Andric } 164404eeddc0SDimitry Andric if (in.relaIplt.get() == this && in.igotPlt->getParent()) { 1645e8d8bef9SDimitry Andric getParent()->flags |= ELF::SHF_INFO_LINK; 16460b57cec5SDimitry Andric getParent()->info = in.igotPlt->getParent()->sectionIndex; 16470b57cec5SDimitry Andric } 1648e8d8bef9SDimitry Andric } 16490b57cec5SDimitry Andric 16500eae32dcSDimitry Andric void DynamicReloc::computeRaw(SymbolTableBaseSection *symtab) { 16510eae32dcSDimitry Andric r_offset = getOffset(); 16520eae32dcSDimitry Andric r_sym = getSymIndex(symtab); 16530eae32dcSDimitry Andric addend = computeAddend(); 16540eae32dcSDimitry Andric kind = AddendOnly; // Catch errors 16550b57cec5SDimitry Andric } 16560b57cec5SDimitry Andric 16571fd87a68SDimitry Andric void RelocationBaseSection::computeRels() { 165804eeddc0SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab.get(); 16590eae32dcSDimitry Andric parallelForEach(relocs, 16600eae32dcSDimitry Andric [symTab](DynamicReloc &rel) { rel.computeRaw(symTab); }); 16610b57cec5SDimitry Andric // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to 16620b57cec5SDimitry Andric // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset 16630b57cec5SDimitry Andric // is to make results easier to read. 16641fd87a68SDimitry Andric if (combreloc) { 16651fd87a68SDimitry Andric auto nonRelative = relocs.begin() + numRelativeRelocs; 166604eeddc0SDimitry Andric parallelSort(relocs.begin(), nonRelative, 166704eeddc0SDimitry Andric [&](auto &a, auto &b) { return a.r_offset < b.r_offset; }); 166804eeddc0SDimitry Andric // Non-relative relocations are few, so don't bother with parallelSort. 1669fcaf7f86SDimitry Andric llvm::sort(nonRelative, relocs.end(), [&](auto &a, auto &b) { 167004eeddc0SDimitry Andric return std::tie(a.r_sym, a.r_offset) < std::tie(b.r_sym, b.r_offset); 16710b57cec5SDimitry Andric }); 16720eae32dcSDimitry Andric } 16731fd87a68SDimitry Andric } 16740b57cec5SDimitry Andric 16751fd87a68SDimitry Andric template <class ELFT> 1676*bdd1243dSDimitry Andric RelocationSection<ELFT>::RelocationSection(StringRef name, bool combreloc, 1677*bdd1243dSDimitry Andric unsigned concurrency) 16781fd87a68SDimitry Andric : RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL, 16791fd87a68SDimitry Andric config->isRela ? DT_RELA : DT_REL, 1680*bdd1243dSDimitry Andric config->isRela ? DT_RELASZ : DT_RELSZ, combreloc, 1681*bdd1243dSDimitry Andric concurrency) { 16821fd87a68SDimitry Andric this->entsize = config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 16831fd87a68SDimitry Andric } 16841fd87a68SDimitry Andric 16851fd87a68SDimitry Andric template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) { 16861fd87a68SDimitry Andric computeRels(); 16870b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 16881fd87a68SDimitry Andric auto *p = reinterpret_cast<Elf_Rela *>(buf); 16891fd87a68SDimitry Andric p->r_offset = rel.r_offset; 16901fd87a68SDimitry Andric p->setSymbolAndType(rel.r_sym, rel.type, config->isMips64EL); 16911fd87a68SDimitry Andric if (config->isRela) 16921fd87a68SDimitry Andric p->r_addend = rel.addend; 16930b57cec5SDimitry Andric buf += config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 16940b57cec5SDimitry Andric } 16950b57cec5SDimitry Andric } 16960b57cec5SDimitry Andric 1697*bdd1243dSDimitry Andric RelrBaseSection::RelrBaseSection(unsigned concurrency) 16981fd87a68SDimitry Andric : SyntheticSection(SHF_ALLOC, 16991fd87a68SDimitry Andric config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR, 1700*bdd1243dSDimitry Andric config->wordsize, ".relr.dyn"), 1701*bdd1243dSDimitry Andric relocsVec(concurrency) {} 1702*bdd1243dSDimitry Andric 1703*bdd1243dSDimitry Andric void RelrBaseSection::mergeRels() { 1704*bdd1243dSDimitry Andric size_t newSize = relocs.size(); 1705*bdd1243dSDimitry Andric for (const auto &v : relocsVec) 1706*bdd1243dSDimitry Andric newSize += v.size(); 1707*bdd1243dSDimitry Andric relocs.reserve(newSize); 1708*bdd1243dSDimitry Andric for (const auto &v : relocsVec) 1709*bdd1243dSDimitry Andric llvm::append_range(relocs, v); 1710*bdd1243dSDimitry Andric relocsVec.clear(); 1711*bdd1243dSDimitry Andric } 17121fd87a68SDimitry Andric 17130b57cec5SDimitry Andric template <class ELFT> 17140b57cec5SDimitry Andric AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection( 1715*bdd1243dSDimitry Andric StringRef name, unsigned concurrency) 17160b57cec5SDimitry Andric : RelocationBaseSection( 17170b57cec5SDimitry Andric name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL, 17180b57cec5SDimitry Andric config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL, 17191fd87a68SDimitry Andric config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ, 1720*bdd1243dSDimitry Andric /*combreloc=*/false, concurrency) { 17210b57cec5SDimitry Andric this->entsize = 1; 17220b57cec5SDimitry Andric } 17230b57cec5SDimitry Andric 17240b57cec5SDimitry Andric template <class ELFT> 17250b57cec5SDimitry Andric bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() { 17260b57cec5SDimitry Andric // This function computes the contents of an Android-format packed relocation 17270b57cec5SDimitry Andric // section. 17280b57cec5SDimitry Andric // 17290b57cec5SDimitry Andric // This format compresses relocations by using relocation groups to factor out 17300b57cec5SDimitry Andric // fields that are common between relocations and storing deltas from previous 17310b57cec5SDimitry Andric // relocations in SLEB128 format (which has a short representation for small 17320b57cec5SDimitry Andric // numbers). A good example of a relocation type with common fields is 17330b57cec5SDimitry Andric // R_*_RELATIVE, which is normally used to represent function pointers in 17340b57cec5SDimitry Andric // vtables. In the REL format, each relative relocation has the same r_info 17350b57cec5SDimitry Andric // field, and is only different from other relative relocations in terms of 17360b57cec5SDimitry Andric // the r_offset field. By sorting relocations by offset, grouping them by 17370b57cec5SDimitry Andric // r_info and representing each relocation with only the delta from the 17380b57cec5SDimitry Andric // previous offset, each 8-byte relocation can be compressed to as little as 1 17390b57cec5SDimitry Andric // byte (or less with run-length encoding). This relocation packer was able to 17400b57cec5SDimitry Andric // reduce the size of the relocation section in an Android Chromium DSO from 17410b57cec5SDimitry Andric // 2,911,184 bytes to 174,693 bytes, or 6% of the original size. 17420b57cec5SDimitry Andric // 17430b57cec5SDimitry Andric // A relocation section consists of a header containing the literal bytes 17440b57cec5SDimitry Andric // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two 17450b57cec5SDimitry Andric // elements are the total number of relocations in the section and an initial 17460b57cec5SDimitry Andric // r_offset value. The remaining elements define a sequence of relocation 17470b57cec5SDimitry Andric // groups. Each relocation group starts with a header consisting of the 17480b57cec5SDimitry Andric // following elements: 17490b57cec5SDimitry Andric // 17500b57cec5SDimitry Andric // - the number of relocations in the relocation group 17510b57cec5SDimitry Andric // - flags for the relocation group 17520b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta 17530b57cec5SDimitry Andric // for each relocation in the group. 17540b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info 17550b57cec5SDimitry Andric // field for each relocation in the group. 17560b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and 17570b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for 17580b57cec5SDimitry Andric // each relocation in the group. 17590b57cec5SDimitry Andric // 17600b57cec5SDimitry Andric // Following the relocation group header are descriptions of each of the 17610b57cec5SDimitry Andric // relocations in the group. They consist of the following elements: 17620b57cec5SDimitry Andric // 17630b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset 17640b57cec5SDimitry Andric // delta for this relocation. 17650b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info 17660b57cec5SDimitry Andric // field for this relocation. 17670b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and 17680b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for 17690b57cec5SDimitry Andric // this relocation. 17700b57cec5SDimitry Andric 17710b57cec5SDimitry Andric size_t oldSize = relocData.size(); 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric relocData = {'A', 'P', 'S', '2'}; 17740b57cec5SDimitry Andric raw_svector_ostream os(relocData); 17750b57cec5SDimitry Andric auto add = [&](int64_t v) { encodeSLEB128(v, os); }; 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric // The format header includes the number of relocations and the initial 17780b57cec5SDimitry Andric // offset (we set this to zero because the first relocation group will 17790b57cec5SDimitry Andric // perform the initial adjustment). 17800b57cec5SDimitry Andric add(relocs.size()); 17810b57cec5SDimitry Andric add(0); 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric std::vector<Elf_Rela> relatives, nonRelatives; 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 17860b57cec5SDimitry Andric Elf_Rela r; 17870eae32dcSDimitry Andric r.r_offset = rel.getOffset(); 178804eeddc0SDimitry Andric r.setSymbolAndType(rel.getSymIndex(getPartition().dynSymTab.get()), 178904eeddc0SDimitry Andric rel.type, false); 1790*bdd1243dSDimitry Andric r.r_addend = config->isRela ? rel.computeAddend() : 0; 17910b57cec5SDimitry Andric 17920b57cec5SDimitry Andric if (r.getType(config->isMips64EL) == target->relativeRel) 17930b57cec5SDimitry Andric relatives.push_back(r); 17940b57cec5SDimitry Andric else 17950b57cec5SDimitry Andric nonRelatives.push_back(r); 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) { 17990b57cec5SDimitry Andric return a.r_offset < b.r_offset; 18000b57cec5SDimitry Andric }); 18010b57cec5SDimitry Andric 18020b57cec5SDimitry Andric // Try to find groups of relative relocations which are spaced one word 18030b57cec5SDimitry Andric // apart from one another. These generally correspond to vtable entries. The 18040b57cec5SDimitry Andric // format allows these groups to be encoded using a sort of run-length 18050b57cec5SDimitry Andric // encoding, but each group will cost 7 bytes in addition to the offset from 18060b57cec5SDimitry Andric // the previous group, so it is only profitable to do this for groups of 18070b57cec5SDimitry Andric // size 8 or larger. 18080b57cec5SDimitry Andric std::vector<Elf_Rela> ungroupedRelatives; 18090b57cec5SDimitry Andric std::vector<std::vector<Elf_Rela>> relativeGroups; 18100b57cec5SDimitry Andric for (auto i = relatives.begin(), e = relatives.end(); i != e;) { 18110b57cec5SDimitry Andric std::vector<Elf_Rela> group; 18120b57cec5SDimitry Andric do { 18130b57cec5SDimitry Andric group.push_back(*i++); 18140b57cec5SDimitry Andric } while (i != e && (i - 1)->r_offset + config->wordsize == i->r_offset); 18150b57cec5SDimitry Andric 18160b57cec5SDimitry Andric if (group.size() < 8) 18170b57cec5SDimitry Andric ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(), 18180b57cec5SDimitry Andric group.end()); 18190b57cec5SDimitry Andric else 18200b57cec5SDimitry Andric relativeGroups.emplace_back(std::move(group)); 18210b57cec5SDimitry Andric } 18220b57cec5SDimitry Andric 182385868e8aSDimitry Andric // For non-relative relocations, we would like to: 182485868e8aSDimitry Andric // 1. Have relocations with the same symbol offset to be consecutive, so 182585868e8aSDimitry Andric // that the runtime linker can speed-up symbol lookup by implementing an 182685868e8aSDimitry Andric // 1-entry cache. 182785868e8aSDimitry Andric // 2. Group relocations by r_info to reduce the size of the relocation 182885868e8aSDimitry Andric // section. 182985868e8aSDimitry Andric // Since the symbol offset is the high bits in r_info, sorting by r_info 183085868e8aSDimitry Andric // allows us to do both. 183185868e8aSDimitry Andric // 183285868e8aSDimitry Andric // For Rela, we also want to sort by r_addend when r_info is the same. This 183385868e8aSDimitry Andric // enables us to group by r_addend as well. 1834*bdd1243dSDimitry Andric llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) { 183585868e8aSDimitry Andric if (a.r_info != b.r_info) 183685868e8aSDimitry Andric return a.r_info < b.r_info; 1837*bdd1243dSDimitry Andric if (a.r_addend != b.r_addend) 183885868e8aSDimitry Andric return a.r_addend < b.r_addend; 1839*bdd1243dSDimitry Andric return a.r_offset < b.r_offset; 184085868e8aSDimitry Andric }); 184185868e8aSDimitry Andric 184285868e8aSDimitry Andric // Group relocations with the same r_info. Note that each group emits a group 184385868e8aSDimitry Andric // header and that may make the relocation section larger. It is hard to 184485868e8aSDimitry Andric // estimate the size of a group header as the encoded size of that varies 184585868e8aSDimitry Andric // based on r_info. However, we can approximate this trade-off by the number 184685868e8aSDimitry Andric // of values encoded. Each group header contains 3 values, and each relocation 184785868e8aSDimitry Andric // in a group encodes one less value, as compared to when it is not grouped. 184885868e8aSDimitry Andric // Therefore, we only group relocations if there are 3 or more of them with 184985868e8aSDimitry Andric // the same r_info. 185085868e8aSDimitry Andric // 185185868e8aSDimitry Andric // For Rela, the addend for most non-relative relocations is zero, and thus we 185285868e8aSDimitry Andric // can usually get a smaller relocation section if we group relocations with 0 185385868e8aSDimitry Andric // addend as well. 185485868e8aSDimitry Andric std::vector<Elf_Rela> ungroupedNonRelatives; 185585868e8aSDimitry Andric std::vector<std::vector<Elf_Rela>> nonRelativeGroups; 185685868e8aSDimitry Andric for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) { 185785868e8aSDimitry Andric auto j = i + 1; 185885868e8aSDimitry Andric while (j != e && i->r_info == j->r_info && 185985868e8aSDimitry Andric (!config->isRela || i->r_addend == j->r_addend)) 186085868e8aSDimitry Andric ++j; 186185868e8aSDimitry Andric if (j - i < 3 || (config->isRela && i->r_addend != 0)) 186285868e8aSDimitry Andric ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j); 186385868e8aSDimitry Andric else 186485868e8aSDimitry Andric nonRelativeGroups.emplace_back(i, j); 186585868e8aSDimitry Andric i = j; 186685868e8aSDimitry Andric } 186785868e8aSDimitry Andric 186885868e8aSDimitry Andric // Sort ungrouped relocations by offset to minimize the encoded length. 186985868e8aSDimitry Andric llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) { 187085868e8aSDimitry Andric return a.r_offset < b.r_offset; 187185868e8aSDimitry Andric }); 187285868e8aSDimitry Andric 18730b57cec5SDimitry Andric unsigned hasAddendIfRela = 18740b57cec5SDimitry Andric config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0; 18750b57cec5SDimitry Andric 18760b57cec5SDimitry Andric uint64_t offset = 0; 18770b57cec5SDimitry Andric uint64_t addend = 0; 18780b57cec5SDimitry Andric 18790b57cec5SDimitry Andric // Emit the run-length encoding for the groups of adjacent relative 18800b57cec5SDimitry Andric // relocations. Each group is represented using two groups in the packed 18810b57cec5SDimitry Andric // format. The first is used to set the current offset to the start of the 18820b57cec5SDimitry Andric // group (and also encodes the first relocation), and the second encodes the 18830b57cec5SDimitry Andric // remaining relocations. 18840b57cec5SDimitry Andric for (std::vector<Elf_Rela> &g : relativeGroups) { 18850b57cec5SDimitry Andric // The first relocation in the group. 18860b57cec5SDimitry Andric add(1); 18870b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 18880b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 18890b57cec5SDimitry Andric add(g[0].r_offset - offset); 18900b57cec5SDimitry Andric add(target->relativeRel); 18910b57cec5SDimitry Andric if (config->isRela) { 18920b57cec5SDimitry Andric add(g[0].r_addend - addend); 18930b57cec5SDimitry Andric addend = g[0].r_addend; 18940b57cec5SDimitry Andric } 18950b57cec5SDimitry Andric 18960b57cec5SDimitry Andric // The remaining relocations. 18970b57cec5SDimitry Andric add(g.size() - 1); 18980b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 18990b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 19000b57cec5SDimitry Andric add(config->wordsize); 19010b57cec5SDimitry Andric add(target->relativeRel); 19020b57cec5SDimitry Andric if (config->isRela) { 1903*bdd1243dSDimitry Andric for (const auto &i : llvm::drop_begin(g)) { 1904*bdd1243dSDimitry Andric add(i.r_addend - addend); 1905*bdd1243dSDimitry Andric addend = i.r_addend; 19060b57cec5SDimitry Andric } 19070b57cec5SDimitry Andric } 19080b57cec5SDimitry Andric 19090b57cec5SDimitry Andric offset = g.back().r_offset; 19100b57cec5SDimitry Andric } 19110b57cec5SDimitry Andric 19120b57cec5SDimitry Andric // Now the ungrouped relatives. 19130b57cec5SDimitry Andric if (!ungroupedRelatives.empty()) { 19140b57cec5SDimitry Andric add(ungroupedRelatives.size()); 19150b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 19160b57cec5SDimitry Andric add(target->relativeRel); 19170b57cec5SDimitry Andric for (Elf_Rela &r : ungroupedRelatives) { 19180b57cec5SDimitry Andric add(r.r_offset - offset); 19190b57cec5SDimitry Andric offset = r.r_offset; 19200b57cec5SDimitry Andric if (config->isRela) { 19210b57cec5SDimitry Andric add(r.r_addend - addend); 19220b57cec5SDimitry Andric addend = r.r_addend; 19230b57cec5SDimitry Andric } 19240b57cec5SDimitry Andric } 19250b57cec5SDimitry Andric } 19260b57cec5SDimitry Andric 192785868e8aSDimitry Andric // Grouped non-relatives. 192885868e8aSDimitry Andric for (ArrayRef<Elf_Rela> g : nonRelativeGroups) { 192985868e8aSDimitry Andric add(g.size()); 193085868e8aSDimitry Andric add(RELOCATION_GROUPED_BY_INFO_FLAG); 193185868e8aSDimitry Andric add(g[0].r_info); 193285868e8aSDimitry Andric for (const Elf_Rela &r : g) { 193385868e8aSDimitry Andric add(r.r_offset - offset); 193485868e8aSDimitry Andric offset = r.r_offset; 193585868e8aSDimitry Andric } 193685868e8aSDimitry Andric addend = 0; 193785868e8aSDimitry Andric } 193885868e8aSDimitry Andric 193985868e8aSDimitry Andric // Finally the ungrouped non-relative relocations. 194085868e8aSDimitry Andric if (!ungroupedNonRelatives.empty()) { 194185868e8aSDimitry Andric add(ungroupedNonRelatives.size()); 19420b57cec5SDimitry Andric add(hasAddendIfRela); 194385868e8aSDimitry Andric for (Elf_Rela &r : ungroupedNonRelatives) { 19440b57cec5SDimitry Andric add(r.r_offset - offset); 19450b57cec5SDimitry Andric offset = r.r_offset; 19460b57cec5SDimitry Andric add(r.r_info); 19470b57cec5SDimitry Andric if (config->isRela) { 19480b57cec5SDimitry Andric add(r.r_addend - addend); 19490b57cec5SDimitry Andric addend = r.r_addend; 19500b57cec5SDimitry Andric } 19510b57cec5SDimitry Andric } 19520b57cec5SDimitry Andric } 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric // Don't allow the section to shrink; otherwise the size of the section can 19550b57cec5SDimitry Andric // oscillate infinitely. 19560b57cec5SDimitry Andric if (relocData.size() < oldSize) 19570b57cec5SDimitry Andric relocData.append(oldSize - relocData.size(), 0); 19580b57cec5SDimitry Andric 19590b57cec5SDimitry Andric // Returns whether the section size changed. We need to keep recomputing both 19600b57cec5SDimitry Andric // section layout and the contents of this section until the size converges 19610b57cec5SDimitry Andric // because changing this section's size can affect section layout, which in 19620b57cec5SDimitry Andric // turn can affect the sizes of the LEB-encoded integers stored in this 19630b57cec5SDimitry Andric // section. 19640b57cec5SDimitry Andric return relocData.size() != oldSize; 19650b57cec5SDimitry Andric } 19660b57cec5SDimitry Andric 1967*bdd1243dSDimitry Andric template <class ELFT> 1968*bdd1243dSDimitry Andric RelrSection<ELFT>::RelrSection(unsigned concurrency) 1969*bdd1243dSDimitry Andric : RelrBaseSection(concurrency) { 19700b57cec5SDimitry Andric this->entsize = config->wordsize; 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 19730b57cec5SDimitry Andric template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() { 19740b57cec5SDimitry Andric // This function computes the contents of an SHT_RELR packed relocation 19750b57cec5SDimitry Andric // section. 19760b57cec5SDimitry Andric // 19770b57cec5SDimitry Andric // Proposal for adding SHT_RELR sections to generic-abi is here: 19780b57cec5SDimitry Andric // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 19790b57cec5SDimitry Andric // 19800b57cec5SDimitry Andric // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 19810b57cec5SDimitry Andric // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 19820b57cec5SDimitry Andric // 19830b57cec5SDimitry Andric // i.e. start with an address, followed by any number of bitmaps. The address 19840b57cec5SDimitry Andric // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 19850b57cec5SDimitry Andric // relocations each, at subsequent offsets following the last address entry. 19860b57cec5SDimitry Andric // 19870b57cec5SDimitry Andric // The bitmap entries must have 1 in the least significant bit. The assumption 19880b57cec5SDimitry Andric // here is that an address cannot have 1 in lsb. Odd addresses are not 19890b57cec5SDimitry Andric // supported. 19900b57cec5SDimitry Andric // 19910b57cec5SDimitry Andric // Excluding the least significant bit in the bitmap, each non-zero bit in 19920b57cec5SDimitry Andric // the bitmap represents a relocation to be applied to a corresponding machine 19930b57cec5SDimitry Andric // word that follows the base address word. The second least significant bit 19940b57cec5SDimitry Andric // represents the machine word immediately following the initial address, and 19950b57cec5SDimitry Andric // each bit that follows represents the next word, in linear order. As such, 19960b57cec5SDimitry Andric // a single bitmap can encode up to 31 relocations in a 32-bit object, and 19970b57cec5SDimitry Andric // 63 relocations in a 64-bit object. 19980b57cec5SDimitry Andric // 19990b57cec5SDimitry Andric // This encoding has a couple of interesting properties: 20000b57cec5SDimitry Andric // 1. Looking at any entry, it is clear whether it's an address or a bitmap: 20010b57cec5SDimitry Andric // even means address, odd means bitmap. 20020b57cec5SDimitry Andric // 2. Just a simple list of addresses is a valid encoding. 20030b57cec5SDimitry Andric 20040b57cec5SDimitry Andric size_t oldSize = relrRelocs.size(); 20050b57cec5SDimitry Andric relrRelocs.clear(); 20060b57cec5SDimitry Andric 20070b57cec5SDimitry Andric // Same as Config->Wordsize but faster because this is a compile-time 20080b57cec5SDimitry Andric // constant. 20090b57cec5SDimitry Andric const size_t wordsize = sizeof(typename ELFT::uint); 20100b57cec5SDimitry Andric 20110b57cec5SDimitry Andric // Number of bits to use for the relocation offsets bitmap. 20120b57cec5SDimitry Andric // Must be either 63 or 31. 20130b57cec5SDimitry Andric const size_t nBits = wordsize * 8 - 1; 20140b57cec5SDimitry Andric 20150b57cec5SDimitry Andric // Get offsets for all relative relocations and sort them. 201604eeddc0SDimitry Andric std::unique_ptr<uint64_t[]> offsets(new uint64_t[relocs.size()]); 2017*bdd1243dSDimitry Andric for (auto [i, r] : llvm::enumerate(relocs)) 2018*bdd1243dSDimitry Andric offsets[i] = r.getOffset(); 2019fcaf7f86SDimitry Andric llvm::sort(offsets.get(), offsets.get() + relocs.size()); 20200b57cec5SDimitry Andric 20210b57cec5SDimitry Andric // For each leading relocation, find following ones that can be folded 20220b57cec5SDimitry Andric // as a bitmap and fold them. 202304eeddc0SDimitry Andric for (size_t i = 0, e = relocs.size(); i != e;) { 20240b57cec5SDimitry Andric // Add a leading relocation. 20250b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr(offsets[i])); 20260b57cec5SDimitry Andric uint64_t base = offsets[i] + wordsize; 20270b57cec5SDimitry Andric ++i; 20280b57cec5SDimitry Andric 20290b57cec5SDimitry Andric // Find foldable relocations to construct bitmaps. 203004eeddc0SDimitry Andric for (;;) { 20310b57cec5SDimitry Andric uint64_t bitmap = 0; 203204eeddc0SDimitry Andric for (; i != e; ++i) { 203304eeddc0SDimitry Andric uint64_t d = offsets[i] - base; 203404eeddc0SDimitry Andric if (d >= nBits * wordsize || d % wordsize) 20350b57cec5SDimitry Andric break; 203604eeddc0SDimitry Andric bitmap |= uint64_t(1) << (d / wordsize); 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric if (!bitmap) 20390b57cec5SDimitry Andric break; 20400b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1)); 20410b57cec5SDimitry Andric base += nBits * wordsize; 20420b57cec5SDimitry Andric } 20430b57cec5SDimitry Andric } 20440b57cec5SDimitry Andric 204585868e8aSDimitry Andric // Don't allow the section to shrink; otherwise the size of the section can 204685868e8aSDimitry Andric // oscillate infinitely. Trailing 1s do not decode to more relocations. 204785868e8aSDimitry Andric if (relrRelocs.size() < oldSize) { 204885868e8aSDimitry Andric log(".relr.dyn needs " + Twine(oldSize - relrRelocs.size()) + 204985868e8aSDimitry Andric " padding word(s)"); 205085868e8aSDimitry Andric relrRelocs.resize(oldSize, Elf_Relr(1)); 205185868e8aSDimitry Andric } 205285868e8aSDimitry Andric 20530b57cec5SDimitry Andric return relrRelocs.size() != oldSize; 20540b57cec5SDimitry Andric } 20550b57cec5SDimitry Andric 20560b57cec5SDimitry Andric SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec) 20570b57cec5SDimitry Andric : SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0, 20580b57cec5SDimitry Andric strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, 20590b57cec5SDimitry Andric config->wordsize, 20600b57cec5SDimitry Andric strTabSec.isDynamic() ? ".dynsym" : ".symtab"), 20610b57cec5SDimitry Andric strTabSec(strTabSec) {} 20620b57cec5SDimitry Andric 20630b57cec5SDimitry Andric // Orders symbols according to their positions in the GOT, 20640b57cec5SDimitry Andric // in compliance with MIPS ABI rules. 20650b57cec5SDimitry Andric // See "Global Offset Table" in Chapter 5 in the following document 20660b57cec5SDimitry Andric // for detailed description: 20670b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 20680b57cec5SDimitry Andric static bool sortMipsSymbols(const SymbolTableEntry &l, 20690b57cec5SDimitry Andric const SymbolTableEntry &r) { 20700b57cec5SDimitry Andric // Sort entries related to non-local preemptible symbols by GOT indexes. 20710b57cec5SDimitry Andric // All other entries go to the beginning of a dynsym in arbitrary order. 20720b57cec5SDimitry Andric if (l.sym->isInGot() && r.sym->isInGot()) 207304eeddc0SDimitry Andric return l.sym->getGotIdx() < r.sym->getGotIdx(); 20740b57cec5SDimitry Andric if (!l.sym->isInGot() && !r.sym->isInGot()) 20750b57cec5SDimitry Andric return false; 20760b57cec5SDimitry Andric return !l.sym->isInGot(); 20770b57cec5SDimitry Andric } 20780b57cec5SDimitry Andric 20790b57cec5SDimitry Andric void SymbolTableBaseSection::finalizeContents() { 20800b57cec5SDimitry Andric if (OutputSection *sec = strTabSec.getParent()) 20810b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 20820b57cec5SDimitry Andric 20830b57cec5SDimitry Andric if (this->type != SHT_DYNSYM) { 20840b57cec5SDimitry Andric sortSymTabSymbols(); 20850b57cec5SDimitry Andric return; 20860b57cec5SDimitry Andric } 20870b57cec5SDimitry Andric 20880b57cec5SDimitry Andric // If it is a .dynsym, there should be no local symbols, but we need 20890b57cec5SDimitry Andric // to do a few things for the dynamic linker. 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric // Section's Info field has the index of the first non-local symbol. 20920b57cec5SDimitry Andric // Because the first symbol entry is a null entry, 1 is the first. 20930b57cec5SDimitry Andric getParent()->info = 1; 20940b57cec5SDimitry Andric 20950b57cec5SDimitry Andric if (getPartition().gnuHashTab) { 20960b57cec5SDimitry Andric // NB: It also sorts Symbols to meet the GNU hash table requirements. 20970b57cec5SDimitry Andric getPartition().gnuHashTab->addSymbols(symbols); 20980b57cec5SDimitry Andric } else if (config->emachine == EM_MIPS) { 20990b57cec5SDimitry Andric llvm::stable_sort(symbols, sortMipsSymbols); 21000b57cec5SDimitry Andric } 21010b57cec5SDimitry Andric 21020b57cec5SDimitry Andric // Only the main partition's dynsym indexes are stored in the symbols 21030b57cec5SDimitry Andric // themselves. All other partitions use a lookup table. 210404eeddc0SDimitry Andric if (this == mainPart->dynSymTab.get()) { 21050b57cec5SDimitry Andric size_t i = 0; 21060b57cec5SDimitry Andric for (const SymbolTableEntry &s : symbols) 21070b57cec5SDimitry Andric s.sym->dynsymIndex = ++i; 21080b57cec5SDimitry Andric } 21090b57cec5SDimitry Andric } 21100b57cec5SDimitry Andric 21110b57cec5SDimitry Andric // The ELF spec requires that all local symbols precede global symbols, so we 21120b57cec5SDimitry Andric // sort symbol entries in this function. (For .dynsym, we don't do that because 21130b57cec5SDimitry Andric // symbols for dynamic linking are inherently all globals.) 21140b57cec5SDimitry Andric // 21150b57cec5SDimitry Andric // Aside from above, we put local symbols in groups starting with the STT_FILE 21160b57cec5SDimitry Andric // symbol. That is convenient for purpose of identifying where are local symbols 21170b57cec5SDimitry Andric // coming from. 21180b57cec5SDimitry Andric void SymbolTableBaseSection::sortSymTabSymbols() { 21190b57cec5SDimitry Andric // Move all local symbols before global symbols. 21200b57cec5SDimitry Andric auto e = std::stable_partition( 212104eeddc0SDimitry Andric symbols.begin(), symbols.end(), 212204eeddc0SDimitry Andric [](const SymbolTableEntry &s) { return s.sym->isLocal(); }); 21230b57cec5SDimitry Andric size_t numLocals = e - symbols.begin(); 21240b57cec5SDimitry Andric getParent()->info = numLocals + 1; 21250b57cec5SDimitry Andric 21260b57cec5SDimitry Andric // We want to group the local symbols by file. For that we rebuild the local 21270b57cec5SDimitry Andric // part of the symbols vector. We do not need to care about the STT_FILE 21280b57cec5SDimitry Andric // symbols, they are already naturally placed first in each group. That 21290b57cec5SDimitry Andric // happens because STT_FILE is always the first symbol in the object and hence 21300b57cec5SDimitry Andric // precede all other local symbols we add for a file. 213104eeddc0SDimitry Andric MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr; 21320b57cec5SDimitry Andric for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e)) 21330b57cec5SDimitry Andric arr[s.sym->file].push_back(s); 21340b57cec5SDimitry Andric 21350b57cec5SDimitry Andric auto i = symbols.begin(); 213604eeddc0SDimitry Andric for (auto &p : arr) 21370b57cec5SDimitry Andric for (SymbolTableEntry &entry : p.second) 21380b57cec5SDimitry Andric *i++ = entry; 21390b57cec5SDimitry Andric } 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric void SymbolTableBaseSection::addSymbol(Symbol *b) { 21420b57cec5SDimitry Andric // Adding a local symbol to a .dynsym is a bug. 21430b57cec5SDimitry Andric assert(this->type != SHT_DYNSYM || !b->isLocal()); 214481ad6265SDimitry Andric symbols.push_back({b, strTabSec.addString(b->getName(), false)}); 21450b57cec5SDimitry Andric } 21460b57cec5SDimitry Andric 21470b57cec5SDimitry Andric size_t SymbolTableBaseSection::getSymbolIndex(Symbol *sym) { 214804eeddc0SDimitry Andric if (this == mainPart->dynSymTab.get()) 21490b57cec5SDimitry Andric return sym->dynsymIndex; 21500b57cec5SDimitry Andric 21510b57cec5SDimitry Andric // Initializes symbol lookup tables lazily. This is used only for -r, 2152349cc55cSDimitry Andric // --emit-relocs and dynsyms in partitions other than the main one. 21530b57cec5SDimitry Andric llvm::call_once(onceFlag, [&] { 21540b57cec5SDimitry Andric symbolIndexMap.reserve(symbols.size()); 21550b57cec5SDimitry Andric size_t i = 0; 21560b57cec5SDimitry Andric for (const SymbolTableEntry &e : symbols) { 21570b57cec5SDimitry Andric if (e.sym->type == STT_SECTION) 21580b57cec5SDimitry Andric sectionIndexMap[e.sym->getOutputSection()] = ++i; 21590b57cec5SDimitry Andric else 21600b57cec5SDimitry Andric symbolIndexMap[e.sym] = ++i; 21610b57cec5SDimitry Andric } 21620b57cec5SDimitry Andric }); 21630b57cec5SDimitry Andric 21640b57cec5SDimitry Andric // Section symbols are mapped based on their output sections 21650b57cec5SDimitry Andric // to maintain their semantics. 21660b57cec5SDimitry Andric if (sym->type == STT_SECTION) 21670b57cec5SDimitry Andric return sectionIndexMap.lookup(sym->getOutputSection()); 21680b57cec5SDimitry Andric return symbolIndexMap.lookup(sym); 21690b57cec5SDimitry Andric } 21700b57cec5SDimitry Andric 21710b57cec5SDimitry Andric template <class ELFT> 21720b57cec5SDimitry Andric SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec) 21730b57cec5SDimitry Andric : SymbolTableBaseSection(strTabSec) { 21740b57cec5SDimitry Andric this->entsize = sizeof(Elf_Sym); 21750b57cec5SDimitry Andric } 21760b57cec5SDimitry Andric 21770b57cec5SDimitry Andric static BssSection *getCommonSec(Symbol *sym) { 217881ad6265SDimitry Andric if (config->relocatable) 21790b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 21800b57cec5SDimitry Andric return dyn_cast_or_null<BssSection>(d->section); 21810b57cec5SDimitry Andric return nullptr; 21820b57cec5SDimitry Andric } 21830b57cec5SDimitry Andric 21840b57cec5SDimitry Andric static uint32_t getSymSectionIndex(Symbol *sym) { 2185*bdd1243dSDimitry Andric assert(!(sym->hasFlag(NEEDS_COPY) && sym->isObject())); 2186*bdd1243dSDimitry Andric if (!isa<Defined>(sym) || sym->hasFlag(NEEDS_COPY)) 21870b57cec5SDimitry Andric return SHN_UNDEF; 21880b57cec5SDimitry Andric if (const OutputSection *os = sym->getOutputSection()) 21890b57cec5SDimitry Andric return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX 21900b57cec5SDimitry Andric : os->sectionIndex; 21910b57cec5SDimitry Andric return SHN_ABS; 21920b57cec5SDimitry Andric } 21930b57cec5SDimitry Andric 21940b57cec5SDimitry Andric // Write the internal symbol table contents to the output symbol table. 21950b57cec5SDimitry Andric template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) { 21960b57cec5SDimitry Andric // The first entry is a null entry as per the ELF spec. 21970b57cec5SDimitry Andric buf += sizeof(Elf_Sym); 21980b57cec5SDimitry Andric 21990b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 22000b57cec5SDimitry Andric 22010b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 22020b57cec5SDimitry Andric Symbol *sym = ent.sym; 22030b57cec5SDimitry Andric bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition; 22040b57cec5SDimitry Andric 220504eeddc0SDimitry Andric // Set st_name, st_info and st_other. 220604eeddc0SDimitry Andric eSym->st_name = ent.strTabOffset; 220704eeddc0SDimitry Andric eSym->setBindingAndType(sym->binding, sym->type); 2208*bdd1243dSDimitry Andric eSym->st_other = sym->stOther; 22090b57cec5SDimitry Andric 221004eeddc0SDimitry Andric if (BssSection *commonSec = getCommonSec(sym)) { 221181ad6265SDimitry Andric // When -r is specified, a COMMON symbol is not allocated. Its st_shndx 221281ad6265SDimitry Andric // holds SHN_COMMON and st_value holds the alignment. 221304eeddc0SDimitry Andric eSym->st_shndx = SHN_COMMON; 2214*bdd1243dSDimitry Andric eSym->st_value = commonSec->addralign; 221504eeddc0SDimitry Andric eSym->st_size = cast<Defined>(sym)->size; 221604eeddc0SDimitry Andric } else { 221704eeddc0SDimitry Andric const uint32_t shndx = getSymSectionIndex(sym); 221804eeddc0SDimitry Andric if (isDefinedHere) { 221904eeddc0SDimitry Andric eSym->st_shndx = shndx; 22200b57cec5SDimitry Andric eSym->st_value = sym->getVA(); 222104eeddc0SDimitry Andric // Copy symbol size if it is a defined symbol. st_size is not 222204eeddc0SDimitry Andric // significant for undefined symbols, so whether copying it or not is up 222304eeddc0SDimitry Andric // to us if that's the case. We'll leave it as zero because by not 222404eeddc0SDimitry Andric // setting a value, we can get the exact same outputs for two sets of 222504eeddc0SDimitry Andric // input files that differ only in undefined symbol size in DSOs. 222604eeddc0SDimitry Andric eSym->st_size = shndx != SHN_UNDEF ? cast<Defined>(sym)->size : 0; 222704eeddc0SDimitry Andric } else { 222804eeddc0SDimitry Andric eSym->st_shndx = 0; 22290b57cec5SDimitry Andric eSym->st_value = 0; 223004eeddc0SDimitry Andric eSym->st_size = 0; 223104eeddc0SDimitry Andric } 223204eeddc0SDimitry Andric } 22330b57cec5SDimitry Andric 22340b57cec5SDimitry Andric ++eSym; 22350b57cec5SDimitry Andric } 22360b57cec5SDimitry Andric 22370b57cec5SDimitry Andric // On MIPS we need to mark symbol which has a PLT entry and requires 22380b57cec5SDimitry Andric // pointer equality by STO_MIPS_PLT flag. That is necessary to help 22390b57cec5SDimitry Andric // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. 22400b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2008-07/txt00000.txt 22410b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 22420b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 22430b57cec5SDimitry Andric 22440b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 22450b57cec5SDimitry Andric Symbol *sym = ent.sym; 2246*bdd1243dSDimitry Andric if (sym->isInPlt() && sym->hasFlag(NEEDS_COPY)) 22470b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PLT; 22480b57cec5SDimitry Andric if (isMicroMips()) { 22490b57cec5SDimitry Andric // We already set the less-significant bit for symbols 22500b57cec5SDimitry Andric // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT 22510b57cec5SDimitry Andric // records. That allows us to distinguish such symbols in 22525ffd83dbSDimitry Andric // the `MIPS<ELFT>::relocate()` routine. Now we should 22530b57cec5SDimitry Andric // clear that bit for non-dynamic symbol table, so tools 22540b57cec5SDimitry Andric // like `objdump` will be able to deal with a correct 22550b57cec5SDimitry Andric // symbol position. 22560b57cec5SDimitry Andric if (sym->isDefined() && 2257*bdd1243dSDimitry Andric ((sym->stOther & STO_MIPS_MICROMIPS) || sym->hasFlag(NEEDS_COPY))) { 22580b57cec5SDimitry Andric if (!strTabSec.isDynamic()) 22590b57cec5SDimitry Andric eSym->st_value &= ~1; 22600b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_MICROMIPS; 22610b57cec5SDimitry Andric } 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric if (config->relocatable) 22640b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 22650b57cec5SDimitry Andric if (isMipsPIC<ELFT>(d)) 22660b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PIC; 22670b57cec5SDimitry Andric ++eSym; 22680b57cec5SDimitry Andric } 22690b57cec5SDimitry Andric } 22700b57cec5SDimitry Andric } 22710b57cec5SDimitry Andric 22720b57cec5SDimitry Andric SymtabShndxSection::SymtabShndxSection() 22730b57cec5SDimitry Andric : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndx") { 22740b57cec5SDimitry Andric this->entsize = 4; 22750b57cec5SDimitry Andric } 22760b57cec5SDimitry Andric 22770b57cec5SDimitry Andric void SymtabShndxSection::writeTo(uint8_t *buf) { 22780b57cec5SDimitry Andric // We write an array of 32 bit values, where each value has 1:1 association 22790b57cec5SDimitry Andric // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX, 22800b57cec5SDimitry Andric // we need to write actual index, otherwise, we must write SHN_UNDEF(0). 22810b57cec5SDimitry Andric buf += 4; // Ignore .symtab[0] entry. 22820b57cec5SDimitry Andric for (const SymbolTableEntry &entry : in.symTab->getSymbols()) { 228304eeddc0SDimitry Andric if (!getCommonSec(entry.sym) && getSymSectionIndex(entry.sym) == SHN_XINDEX) 22840b57cec5SDimitry Andric write32(buf, entry.sym->getOutputSection()->sectionIndex); 22850b57cec5SDimitry Andric buf += 4; 22860b57cec5SDimitry Andric } 22870b57cec5SDimitry Andric } 22880b57cec5SDimitry Andric 22890b57cec5SDimitry Andric bool SymtabShndxSection::isNeeded() const { 22900b57cec5SDimitry Andric // SHT_SYMTAB can hold symbols with section indices values up to 22910b57cec5SDimitry Andric // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX 22920b57cec5SDimitry Andric // section. Problem is that we reveal the final section indices a bit too 22930b57cec5SDimitry Andric // late, and we do not know them here. For simplicity, we just always create 22940b57cec5SDimitry Andric // a .symtab_shndx section when the amount of output sections is huge. 22950b57cec5SDimitry Andric size_t size = 0; 22964824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 229781ad6265SDimitry Andric if (isa<OutputDesc>(cmd)) 22980b57cec5SDimitry Andric ++size; 22990b57cec5SDimitry Andric return size >= SHN_LORESERVE; 23000b57cec5SDimitry Andric } 23010b57cec5SDimitry Andric 23020b57cec5SDimitry Andric void SymtabShndxSection::finalizeContents() { 23030b57cec5SDimitry Andric getParent()->link = in.symTab->getParent()->sectionIndex; 23040b57cec5SDimitry Andric } 23050b57cec5SDimitry Andric 23060b57cec5SDimitry Andric size_t SymtabShndxSection::getSize() const { 23070b57cec5SDimitry Andric return in.symTab->getNumSymbols() * 4; 23080b57cec5SDimitry Andric } 23090b57cec5SDimitry Andric 23100b57cec5SDimitry Andric // .hash and .gnu.hash sections contain on-disk hash tables that map 23110b57cec5SDimitry Andric // symbol names to their dynamic symbol table indices. Their purpose 23120b57cec5SDimitry Andric // is to help the dynamic linker resolve symbols quickly. If ELF files 23130b57cec5SDimitry Andric // don't have them, the dynamic linker has to do linear search on all 23140b57cec5SDimitry Andric // dynamic symbols, which makes programs slower. Therefore, a .hash 2315349cc55cSDimitry Andric // section is added to a DSO by default. 23160b57cec5SDimitry Andric // 23170b57cec5SDimitry Andric // The Unix semantics of resolving dynamic symbols is somewhat expensive. 23180b57cec5SDimitry Andric // Each ELF file has a list of DSOs that the ELF file depends on and a 23190b57cec5SDimitry Andric // list of dynamic symbols that need to be resolved from any of the 23200b57cec5SDimitry Andric // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) 23210b57cec5SDimitry Andric // where m is the number of DSOs and n is the number of dynamic 23220b57cec5SDimitry Andric // symbols. For modern large programs, both m and n are large. So 23235ffd83dbSDimitry Andric // making each step faster by using hash tables substantially 23240b57cec5SDimitry Andric // improves time to load programs. 23250b57cec5SDimitry Andric // 23260b57cec5SDimitry Andric // (Note that this is not the only way to design the shared library. 23270b57cec5SDimitry Andric // For instance, the Windows DLL takes a different approach. On 23280b57cec5SDimitry Andric // Windows, each dynamic symbol has a name of DLL from which the symbol 23290b57cec5SDimitry Andric // has to be resolved. That makes the cost of symbol resolution O(n). 23300b57cec5SDimitry Andric // This disables some hacky techniques you can use on Unix such as 23310b57cec5SDimitry Andric // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) 23320b57cec5SDimitry Andric // 23330b57cec5SDimitry Andric // Due to historical reasons, we have two different hash tables, .hash 23340b57cec5SDimitry Andric // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new 23350b57cec5SDimitry Andric // and better version of .hash. .hash is just an on-disk hash table, but 23360b57cec5SDimitry Andric // .gnu.hash has a bloom filter in addition to a hash table to skip 23370b57cec5SDimitry Andric // DSOs very quickly. If you are sure that your dynamic linker knows 2338349cc55cSDimitry Andric // about .gnu.hash, you want to specify --hash-style=gnu. Otherwise, a 2339349cc55cSDimitry Andric // safe bet is to specify --hash-style=both for backward compatibility. 23400b57cec5SDimitry Andric GnuHashTableSection::GnuHashTableSection() 23410b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, config->wordsize, ".gnu.hash") { 23420b57cec5SDimitry Andric } 23430b57cec5SDimitry Andric 23440b57cec5SDimitry Andric void GnuHashTableSection::finalizeContents() { 23450b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynSymTab->getParent()) 23460b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 23470b57cec5SDimitry Andric 23480b57cec5SDimitry Andric // Computes bloom filter size in word size. We want to allocate 12 23490b57cec5SDimitry Andric // bits for each symbol. It must be a power of two. 23500b57cec5SDimitry Andric if (symbols.empty()) { 23510b57cec5SDimitry Andric maskWords = 1; 23520b57cec5SDimitry Andric } else { 23530b57cec5SDimitry Andric uint64_t numBits = symbols.size() * 12; 23540b57cec5SDimitry Andric maskWords = NextPowerOf2(numBits / (config->wordsize * 8)); 23550b57cec5SDimitry Andric } 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric size = 16; // Header 23580b57cec5SDimitry Andric size += config->wordsize * maskWords; // Bloom filter 23590b57cec5SDimitry Andric size += nBuckets * 4; // Hash buckets 23600b57cec5SDimitry Andric size += symbols.size() * 4; // Hash values 23610b57cec5SDimitry Andric } 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric void GnuHashTableSection::writeTo(uint8_t *buf) { 23640b57cec5SDimitry Andric // Write a header. 23650b57cec5SDimitry Andric write32(buf, nBuckets); 23660b57cec5SDimitry Andric write32(buf + 4, getPartition().dynSymTab->getNumSymbols() - symbols.size()); 23670b57cec5SDimitry Andric write32(buf + 8, maskWords); 23680b57cec5SDimitry Andric write32(buf + 12, Shift2); 23690b57cec5SDimitry Andric buf += 16; 23700b57cec5SDimitry Andric 23714824e7fdSDimitry Andric // Write the 2-bit bloom filter. 23724824e7fdSDimitry Andric const unsigned c = config->is64 ? 64 : 32; 23730b57cec5SDimitry Andric for (const Entry &sym : symbols) { 23740b57cec5SDimitry Andric // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in 23750b57cec5SDimitry Andric // the word using bits [0:5] and [26:31]. 23760b57cec5SDimitry Andric size_t i = (sym.hash / c) & (maskWords - 1); 23770b57cec5SDimitry Andric uint64_t val = readUint(buf + i * config->wordsize); 23780b57cec5SDimitry Andric val |= uint64_t(1) << (sym.hash % c); 23790b57cec5SDimitry Andric val |= uint64_t(1) << ((sym.hash >> Shift2) % c); 23800b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, val); 23810b57cec5SDimitry Andric } 23824824e7fdSDimitry Andric buf += config->wordsize * maskWords; 23830b57cec5SDimitry Andric 23844824e7fdSDimitry Andric // Write the hash table. 23850b57cec5SDimitry Andric uint32_t *buckets = reinterpret_cast<uint32_t *>(buf); 23860b57cec5SDimitry Andric uint32_t oldBucket = -1; 23870b57cec5SDimitry Andric uint32_t *values = buckets + nBuckets; 23880b57cec5SDimitry Andric for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) { 23890b57cec5SDimitry Andric // Write a hash value. It represents a sequence of chains that share the 23900b57cec5SDimitry Andric // same hash modulo value. The last element of each chain is terminated by 23910b57cec5SDimitry Andric // LSB 1. 23920b57cec5SDimitry Andric uint32_t hash = i->hash; 23930b57cec5SDimitry Andric bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx; 23940b57cec5SDimitry Andric hash = isLastInChain ? hash | 1 : hash & ~1; 23950b57cec5SDimitry Andric write32(values++, hash); 23960b57cec5SDimitry Andric 23970b57cec5SDimitry Andric if (i->bucketIdx == oldBucket) 23980b57cec5SDimitry Andric continue; 23990b57cec5SDimitry Andric // Write a hash bucket. Hash buckets contain indices in the following hash 24000b57cec5SDimitry Andric // value table. 24010b57cec5SDimitry Andric write32(buckets + i->bucketIdx, 24020b57cec5SDimitry Andric getPartition().dynSymTab->getSymbolIndex(i->sym)); 24030b57cec5SDimitry Andric oldBucket = i->bucketIdx; 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric // Add symbols to this symbol hash table. Note that this function 24080b57cec5SDimitry Andric // destructively sort a given vector -- which is needed because 24090b57cec5SDimitry Andric // GNU-style hash table places some sorting requirements. 24100eae32dcSDimitry Andric void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) { 24110b57cec5SDimitry Andric // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce 24120b57cec5SDimitry Andric // its type correctly. 24130eae32dcSDimitry Andric auto mid = 24140b57cec5SDimitry Andric std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) { 24150b57cec5SDimitry Andric return !s.sym->isDefined() || s.sym->partition != partition; 24160b57cec5SDimitry Andric }); 24170b57cec5SDimitry Andric 24180b57cec5SDimitry Andric // We chose load factor 4 for the on-disk hash table. For each hash 24190b57cec5SDimitry Andric // collision, the dynamic linker will compare a uint32_t hash value. 24200b57cec5SDimitry Andric // Since the integer comparison is quite fast, we believe we can 24210b57cec5SDimitry Andric // make the load factor even larger. 4 is just a conservative choice. 24220b57cec5SDimitry Andric // 24230b57cec5SDimitry Andric // Note that we don't want to create a zero-sized hash table because 24240b57cec5SDimitry Andric // Android loader as of 2018 doesn't like a .gnu.hash containing such 24250b57cec5SDimitry Andric // table. If that's the case, we create a hash table with one unused 24260b57cec5SDimitry Andric // dummy slot. 24270b57cec5SDimitry Andric nBuckets = std::max<size_t>((v.end() - mid) / 4, 1); 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric if (mid == v.end()) 24300b57cec5SDimitry Andric return; 24310b57cec5SDimitry Andric 24320b57cec5SDimitry Andric for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) { 24330b57cec5SDimitry Andric Symbol *b = ent.sym; 24340b57cec5SDimitry Andric uint32_t hash = hashGnu(b->getName()); 24350b57cec5SDimitry Andric uint32_t bucketIdx = hash % nBuckets; 24360b57cec5SDimitry Andric symbols.push_back({b, ent.strTabOffset, hash, bucketIdx}); 24370b57cec5SDimitry Andric } 24380b57cec5SDimitry Andric 243904eeddc0SDimitry Andric llvm::sort(symbols, [](const Entry &l, const Entry &r) { 244004eeddc0SDimitry Andric return std::tie(l.bucketIdx, l.strTabOffset) < 244104eeddc0SDimitry Andric std::tie(r.bucketIdx, r.strTabOffset); 24420b57cec5SDimitry Andric }); 24430b57cec5SDimitry Andric 24440b57cec5SDimitry Andric v.erase(mid, v.end()); 24450b57cec5SDimitry Andric for (const Entry &ent : symbols) 24460b57cec5SDimitry Andric v.push_back({ent.sym, ent.strTabOffset}); 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 24490b57cec5SDimitry Andric HashTableSection::HashTableSection() 24500b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { 24510b57cec5SDimitry Andric this->entsize = 4; 24520b57cec5SDimitry Andric } 24530b57cec5SDimitry Andric 24540b57cec5SDimitry Andric void HashTableSection::finalizeContents() { 245504eeddc0SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab.get(); 24560b57cec5SDimitry Andric 24570b57cec5SDimitry Andric if (OutputSection *sec = symTab->getParent()) 24580b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 24590b57cec5SDimitry Andric 24600b57cec5SDimitry Andric unsigned numEntries = 2; // nbucket and nchain. 24610b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); // The chain entries. 24620b57cec5SDimitry Andric 24630b57cec5SDimitry Andric // Create as many buckets as there are symbols. 24640b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); 24650b57cec5SDimitry Andric this->size = numEntries * 4; 24660b57cec5SDimitry Andric } 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric void HashTableSection::writeTo(uint8_t *buf) { 246904eeddc0SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab.get(); 24700b57cec5SDimitry Andric unsigned numSymbols = symTab->getNumSymbols(); 24710b57cec5SDimitry Andric 24720b57cec5SDimitry Andric uint32_t *p = reinterpret_cast<uint32_t *>(buf); 24730b57cec5SDimitry Andric write32(p++, numSymbols); // nbucket 24740b57cec5SDimitry Andric write32(p++, numSymbols); // nchain 24750b57cec5SDimitry Andric 24760b57cec5SDimitry Andric uint32_t *buckets = p; 24770b57cec5SDimitry Andric uint32_t *chains = p + numSymbols; 24780b57cec5SDimitry Andric 24790b57cec5SDimitry Andric for (const SymbolTableEntry &s : symTab->getSymbols()) { 24800b57cec5SDimitry Andric Symbol *sym = s.sym; 24810b57cec5SDimitry Andric StringRef name = sym->getName(); 24820b57cec5SDimitry Andric unsigned i = sym->dynsymIndex; 24830b57cec5SDimitry Andric uint32_t hash = hashSysV(name) % numSymbols; 24840b57cec5SDimitry Andric chains[i] = buckets[hash]; 24850b57cec5SDimitry Andric write32(buckets + hash, i); 24860b57cec5SDimitry Andric } 24870b57cec5SDimitry Andric } 24880b57cec5SDimitry Andric 2489480093f4SDimitry Andric PltSection::PltSection() 2490480093f4SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"), 2491480093f4SDimitry Andric headerSize(target->pltHeaderSize) { 2492480093f4SDimitry Andric // On PowerPC, this section contains lazy symbol resolvers. 249392c0d181SDimitry Andric if (config->emachine == EM_PPC64) { 2494480093f4SDimitry Andric name = ".glink"; 2495*bdd1243dSDimitry Andric addralign = 4; 2496480093f4SDimitry Andric } 2497480093f4SDimitry Andric 2498480093f4SDimitry Andric // On x86 when IBT is enabled, this section contains the second PLT (lazy 2499480093f4SDimitry Andric // symbol resolvers). 2500480093f4SDimitry Andric if ((config->emachine == EM_386 || config->emachine == EM_X86_64) && 2501480093f4SDimitry Andric (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) 2502480093f4SDimitry Andric name = ".plt.sec"; 2503480093f4SDimitry Andric 25040b57cec5SDimitry Andric // The PLT needs to be writable on SPARC as the dynamic linker will 25050b57cec5SDimitry Andric // modify the instructions in the PLT entries. 25060b57cec5SDimitry Andric if (config->emachine == EM_SPARCV9) 25070b57cec5SDimitry Andric this->flags |= SHF_WRITE; 25080b57cec5SDimitry Andric } 25090b57cec5SDimitry Andric 25100b57cec5SDimitry Andric void PltSection::writeTo(uint8_t *buf) { 2511480093f4SDimitry Andric // At beginning of PLT, we have code to call the dynamic 25120b57cec5SDimitry Andric // linker to resolve dynsyms at runtime. Write such code. 25130b57cec5SDimitry Andric target->writePltHeader(buf); 25140b57cec5SDimitry Andric size_t off = headerSize; 25150b57cec5SDimitry Andric 2516480093f4SDimitry Andric for (const Symbol *sym : entries) { 2517480093f4SDimitry Andric target->writePlt(buf + off, *sym, getVA() + off); 25180b57cec5SDimitry Andric off += target->pltEntrySize; 25190b57cec5SDimitry Andric } 25200b57cec5SDimitry Andric } 25210b57cec5SDimitry Andric 2522480093f4SDimitry Andric void PltSection::addEntry(Symbol &sym) { 252304eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1); 252404eeddc0SDimitry Andric symAux.back().pltIdx = entries.size(); 25250b57cec5SDimitry Andric entries.push_back(&sym); 25260b57cec5SDimitry Andric } 25270b57cec5SDimitry Andric 25280b57cec5SDimitry Andric size_t PltSection::getSize() const { 252992c0d181SDimitry Andric return headerSize + entries.size() * target->pltEntrySize; 25300b57cec5SDimitry Andric } 25310b57cec5SDimitry Andric 2532480093f4SDimitry Andric bool PltSection::isNeeded() const { 2533480093f4SDimitry Andric // For -z retpolineplt, .iplt needs the .plt header. 2534480093f4SDimitry Andric return !entries.empty() || (config->zRetpolineplt && in.iplt->isNeeded()); 2535480093f4SDimitry Andric } 2536480093f4SDimitry Andric 2537480093f4SDimitry Andric // Used by ARM to add mapping symbols in the PLT section, which aid 2538480093f4SDimitry Andric // disassembly. 25390b57cec5SDimitry Andric void PltSection::addSymbols() { 25400b57cec5SDimitry Andric target->addPltHeaderSymbols(*this); 25410b57cec5SDimitry Andric 25420b57cec5SDimitry Andric size_t off = headerSize; 25430b57cec5SDimitry Andric for (size_t i = 0; i < entries.size(); ++i) { 25440b57cec5SDimitry Andric target->addPltSymbols(*this, off); 25450b57cec5SDimitry Andric off += target->pltEntrySize; 25460b57cec5SDimitry Andric } 25470b57cec5SDimitry Andric } 25480b57cec5SDimitry Andric 2549480093f4SDimitry Andric IpltSection::IpltSection() 2550480093f4SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".iplt") { 2551480093f4SDimitry Andric if (config->emachine == EM_PPC || config->emachine == EM_PPC64) { 2552480093f4SDimitry Andric name = ".glink"; 2553*bdd1243dSDimitry Andric addralign = 4; 2554480093f4SDimitry Andric } 2555480093f4SDimitry Andric } 2556480093f4SDimitry Andric 2557480093f4SDimitry Andric void IpltSection::writeTo(uint8_t *buf) { 2558480093f4SDimitry Andric uint32_t off = 0; 2559480093f4SDimitry Andric for (const Symbol *sym : entries) { 2560480093f4SDimitry Andric target->writeIplt(buf + off, *sym, getVA() + off); 2561480093f4SDimitry Andric off += target->ipltEntrySize; 2562480093f4SDimitry Andric } 2563480093f4SDimitry Andric } 2564480093f4SDimitry Andric 2565480093f4SDimitry Andric size_t IpltSection::getSize() const { 2566480093f4SDimitry Andric return entries.size() * target->ipltEntrySize; 2567480093f4SDimitry Andric } 2568480093f4SDimitry Andric 2569480093f4SDimitry Andric void IpltSection::addEntry(Symbol &sym) { 257004eeddc0SDimitry Andric assert(sym.auxIdx == symAux.size() - 1); 257104eeddc0SDimitry Andric symAux.back().pltIdx = entries.size(); 2572480093f4SDimitry Andric entries.push_back(&sym); 2573480093f4SDimitry Andric } 2574480093f4SDimitry Andric 2575480093f4SDimitry Andric // ARM uses mapping symbols to aid disassembly. 2576480093f4SDimitry Andric void IpltSection::addSymbols() { 2577480093f4SDimitry Andric size_t off = 0; 2578480093f4SDimitry Andric for (size_t i = 0, e = entries.size(); i != e; ++i) { 2579480093f4SDimitry Andric target->addPltSymbols(*this, off); 2580480093f4SDimitry Andric off += target->pltEntrySize; 2581480093f4SDimitry Andric } 2582480093f4SDimitry Andric } 2583480093f4SDimitry Andric 258492c0d181SDimitry Andric PPC32GlinkSection::PPC32GlinkSection() { 258592c0d181SDimitry Andric name = ".glink"; 2586*bdd1243dSDimitry Andric addralign = 4; 258792c0d181SDimitry Andric } 258892c0d181SDimitry Andric 258992c0d181SDimitry Andric void PPC32GlinkSection::writeTo(uint8_t *buf) { 259092c0d181SDimitry Andric writePPC32GlinkSection(buf, entries.size()); 259192c0d181SDimitry Andric } 259292c0d181SDimitry Andric 259392c0d181SDimitry Andric size_t PPC32GlinkSection::getSize() const { 259492c0d181SDimitry Andric return headerSize + entries.size() * target->pltEntrySize + footerSize; 259592c0d181SDimitry Andric } 259692c0d181SDimitry Andric 2597480093f4SDimitry Andric // This is an x86-only extra PLT section and used only when a security 2598480093f4SDimitry Andric // enhancement feature called CET is enabled. In this comment, I'll explain what 2599480093f4SDimitry Andric // the feature is and why we have two PLT sections if CET is enabled. 2600480093f4SDimitry Andric // 2601480093f4SDimitry Andric // So, what does CET do? CET introduces a new restriction to indirect jump 2602480093f4SDimitry Andric // instructions. CET works this way. Assume that CET is enabled. Then, if you 2603480093f4SDimitry Andric // execute an indirect jump instruction, the processor verifies that a special 2604480093f4SDimitry Andric // "landing pad" instruction (which is actually a repurposed NOP instruction and 2605480093f4SDimitry Andric // now called "endbr32" or "endbr64") is at the jump target. If the jump target 2606480093f4SDimitry Andric // does not start with that instruction, the processor raises an exception 2607480093f4SDimitry Andric // instead of continuing executing code. 2608480093f4SDimitry Andric // 2609480093f4SDimitry Andric // If CET is enabled, the compiler emits endbr to all locations where indirect 2610480093f4SDimitry Andric // jumps may jump to. 2611480093f4SDimitry Andric // 2612480093f4SDimitry Andric // This mechanism makes it extremely hard to transfer the control to a middle of 2613480093f4SDimitry Andric // a function that is not supporsed to be a indirect jump target, preventing 2614480093f4SDimitry Andric // certain types of attacks such as ROP or JOP. 2615480093f4SDimitry Andric // 2616480093f4SDimitry Andric // Note that the processors in the market as of 2019 don't actually support the 2617480093f4SDimitry Andric // feature. Only the spec is available at the moment. 2618480093f4SDimitry Andric // 2619480093f4SDimitry Andric // Now, I'll explain why we have this extra PLT section for CET. 2620480093f4SDimitry Andric // 2621480093f4SDimitry Andric // Since you can indirectly jump to a PLT entry, we have to make PLT entries 2622480093f4SDimitry Andric // start with endbr. The problem is there's no extra space for endbr (which is 4 2623480093f4SDimitry Andric // bytes long), as the PLT entry is only 16 bytes long and all bytes are already 2624480093f4SDimitry Andric // used. 2625480093f4SDimitry Andric // 2626480093f4SDimitry Andric // In order to deal with the issue, we split a PLT entry into two PLT entries. 2627480093f4SDimitry Andric // Remember that each PLT entry contains code to jump to an address read from 2628480093f4SDimitry Andric // .got.plt AND code to resolve a dynamic symbol lazily. With the 2-PLT scheme, 2629480093f4SDimitry Andric // the former code is written to .plt.sec, and the latter code is written to 2630480093f4SDimitry Andric // .plt. 2631480093f4SDimitry Andric // 2632480093f4SDimitry Andric // Lazy symbol resolution in the 2-PLT scheme works in the usual way, except 2633480093f4SDimitry Andric // that the regular .plt is now called .plt.sec and .plt is repurposed to 2634480093f4SDimitry Andric // contain only code for lazy symbol resolution. 2635480093f4SDimitry Andric // 2636480093f4SDimitry Andric // In other words, this is how the 2-PLT scheme works. Application code is 2637480093f4SDimitry Andric // supposed to jump to .plt.sec to call an external function. Each .plt.sec 2638480093f4SDimitry Andric // entry contains code to read an address from a corresponding .got.plt entry 2639480093f4SDimitry Andric // and jump to that address. Addresses in .got.plt initially point to .plt, so 2640480093f4SDimitry Andric // when an application calls an external function for the first time, the 2641480093f4SDimitry Andric // control is transferred to a function that resolves a symbol name from 2642480093f4SDimitry Andric // external shared object files. That function then rewrites a .got.plt entry 2643480093f4SDimitry Andric // with a resolved address, so that the subsequent function calls directly jump 2644480093f4SDimitry Andric // to a desired location from .plt.sec. 2645480093f4SDimitry Andric // 2646480093f4SDimitry Andric // There is an open question as to whether the 2-PLT scheme was desirable or 2647480093f4SDimitry Andric // not. We could have simply extended the PLT entry size to 32-bytes to 2648480093f4SDimitry Andric // accommodate endbr, and that scheme would have been much simpler than the 2649480093f4SDimitry Andric // 2-PLT scheme. One reason to split PLT was, by doing that, we could keep hot 2650480093f4SDimitry Andric // code (.plt.sec) from cold code (.plt). But as far as I know no one proved 2651480093f4SDimitry Andric // that the optimization actually makes a difference. 2652480093f4SDimitry Andric // 2653480093f4SDimitry Andric // That said, the 2-PLT scheme is a part of the ABI, debuggers and other tools 2654480093f4SDimitry Andric // depend on it, so we implement the ABI. 2655480093f4SDimitry Andric IBTPltSection::IBTPltSection() 2656480093f4SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt") {} 2657480093f4SDimitry Andric 2658480093f4SDimitry Andric void IBTPltSection::writeTo(uint8_t *buf) { 2659480093f4SDimitry Andric target->writeIBTPlt(buf, in.plt->getNumEntries()); 2660480093f4SDimitry Andric } 2661480093f4SDimitry Andric 2662480093f4SDimitry Andric size_t IBTPltSection::getSize() const { 2663480093f4SDimitry Andric // 16 is the header size of .plt. 2664480093f4SDimitry Andric return 16 + in.plt->getNumEntries() * target->pltEntrySize; 2665480093f4SDimitry Andric } 2666480093f4SDimitry Andric 2667d781ede6SDimitry Andric bool IBTPltSection::isNeeded() const { return in.plt->getNumEntries() > 0; } 2668d781ede6SDimitry Andric 26690b57cec5SDimitry Andric // The string hash function for .gdb_index. 26700b57cec5SDimitry Andric static uint32_t computeGdbHash(StringRef s) { 26710b57cec5SDimitry Andric uint32_t h = 0; 26720b57cec5SDimitry Andric for (uint8_t c : s) 26730b57cec5SDimitry Andric h = h * 67 + toLower(c) - 113; 26740b57cec5SDimitry Andric return h; 26750b57cec5SDimitry Andric } 26760b57cec5SDimitry Andric 26770b57cec5SDimitry Andric GdbIndexSection::GdbIndexSection() 26780b57cec5SDimitry Andric : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {} 26790b57cec5SDimitry Andric 26800b57cec5SDimitry Andric // Returns the desired size of an on-disk hash table for a .gdb_index section. 26810b57cec5SDimitry Andric // There's a tradeoff between size and collision rate. We aim 75% utilization. 26820b57cec5SDimitry Andric size_t GdbIndexSection::computeSymtabSize() const { 26830b57cec5SDimitry Andric return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024); 26840b57cec5SDimitry Andric } 26850b57cec5SDimitry Andric 26860eae32dcSDimitry Andric static SmallVector<GdbIndexSection::CuEntry, 0> 26870eae32dcSDimitry Andric readCuList(DWARFContext &dwarf) { 26880eae32dcSDimitry Andric SmallVector<GdbIndexSection::CuEntry, 0> ret; 26890b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) 26900b57cec5SDimitry Andric ret.push_back({cu->getOffset(), cu->getLength() + 4}); 26910b57cec5SDimitry Andric return ret; 26920b57cec5SDimitry Andric } 26930b57cec5SDimitry Andric 26940eae32dcSDimitry Andric static SmallVector<GdbIndexSection::AddressEntry, 0> 26950b57cec5SDimitry Andric readAddressAreas(DWARFContext &dwarf, InputSection *sec) { 26960eae32dcSDimitry Andric SmallVector<GdbIndexSection::AddressEntry, 0> ret; 26970b57cec5SDimitry Andric 26980b57cec5SDimitry Andric uint32_t cuIdx = 0; 26990b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) { 270085868e8aSDimitry Andric if (Error e = cu->tryExtractDIEsIfNeeded(false)) { 27015ffd83dbSDimitry Andric warn(toString(sec) + ": " + toString(std::move(e))); 270285868e8aSDimitry Andric return {}; 270385868e8aSDimitry Andric } 27040b57cec5SDimitry Andric Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges(); 27050b57cec5SDimitry Andric if (!ranges) { 27065ffd83dbSDimitry Andric warn(toString(sec) + ": " + toString(ranges.takeError())); 27070b57cec5SDimitry Andric return {}; 27080b57cec5SDimitry Andric } 27090b57cec5SDimitry Andric 27100b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = sec->file->getSections(); 27110b57cec5SDimitry Andric for (DWARFAddressRange &r : *ranges) { 27120b57cec5SDimitry Andric if (r.SectionIndex == -1ULL) 27130b57cec5SDimitry Andric continue; 27140b57cec5SDimitry Andric // Range list with zero size has no effect. 27155ffd83dbSDimitry Andric InputSectionBase *s = sections[r.SectionIndex]; 27165ffd83dbSDimitry Andric if (s && s != &InputSection::discarded && s->isLive()) 27175ffd83dbSDimitry Andric if (r.LowPC != r.HighPC) 27185ffd83dbSDimitry Andric ret.push_back({cast<InputSection>(s), r.LowPC, r.HighPC, cuIdx}); 27190b57cec5SDimitry Andric } 27200b57cec5SDimitry Andric ++cuIdx; 27210b57cec5SDimitry Andric } 27220b57cec5SDimitry Andric 27230b57cec5SDimitry Andric return ret; 27240b57cec5SDimitry Andric } 27250b57cec5SDimitry Andric 27260b57cec5SDimitry Andric template <class ELFT> 27271fd87a68SDimitry Andric static SmallVector<GdbIndexSection::NameAttrEntry, 0> 27280b57cec5SDimitry Andric readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj, 27290eae32dcSDimitry Andric const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) { 27305ffd83dbSDimitry Andric const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection(); 27315ffd83dbSDimitry Andric const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection(); 27320b57cec5SDimitry Andric 27331fd87a68SDimitry Andric SmallVector<GdbIndexSection::NameAttrEntry, 0> ret; 27345ffd83dbSDimitry Andric for (const LLDDWARFSection *pub : {&pubNames, &pubTypes}) { 27355ffd83dbSDimitry Andric DWARFDataExtractor data(obj, *pub, config->isLE, config->wordsize); 27365ffd83dbSDimitry Andric DWARFDebugPubTable table; 27375ffd83dbSDimitry Andric table.extract(data, /*GnuStyle=*/true, [&](Error e) { 27385ffd83dbSDimitry Andric warn(toString(pub->sec) + ": " + toString(std::move(e))); 27395ffd83dbSDimitry Andric }); 27400b57cec5SDimitry Andric for (const DWARFDebugPubTable::Set &set : table.getData()) { 27410b57cec5SDimitry Andric // The value written into the constant pool is kind << 24 | cuIndex. As we 27420b57cec5SDimitry Andric // don't know how many compilation units precede this object to compute 27430b57cec5SDimitry Andric // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add 27440b57cec5SDimitry Andric // the number of preceding compilation units later. 274585868e8aSDimitry Andric uint32_t i = llvm::partition_point(cus, 274685868e8aSDimitry Andric [&](GdbIndexSection::CuEntry cu) { 274785868e8aSDimitry Andric return cu.cuOffset < set.Offset; 27480b57cec5SDimitry Andric }) - 274985868e8aSDimitry Andric cus.begin(); 27500b57cec5SDimitry Andric for (const DWARFDebugPubTable::Entry &ent : set.Entries) 27510b57cec5SDimitry Andric ret.push_back({{ent.Name, computeGdbHash(ent.Name)}, 27520b57cec5SDimitry Andric (ent.Descriptor.toBits() << 24) | i}); 27530b57cec5SDimitry Andric } 27540b57cec5SDimitry Andric } 27550b57cec5SDimitry Andric return ret; 27560b57cec5SDimitry Andric } 27570b57cec5SDimitry Andric 27580b57cec5SDimitry Andric // Create a list of symbols from a given list of symbol names and types 27590b57cec5SDimitry Andric // by uniquifying them by name. 2760*bdd1243dSDimitry Andric static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t> 2761*bdd1243dSDimitry Andric createSymbols( 27621fd87a68SDimitry Andric ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs, 27631fd87a68SDimitry Andric const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) { 27640b57cec5SDimitry Andric using GdbSymbol = GdbIndexSection::GdbSymbol; 27650b57cec5SDimitry Andric using NameAttrEntry = GdbIndexSection::NameAttrEntry; 27660b57cec5SDimitry Andric 27670b57cec5SDimitry Andric // For each chunk, compute the number of compilation units preceding it. 27680b57cec5SDimitry Andric uint32_t cuIdx = 0; 276904eeddc0SDimitry Andric std::unique_ptr<uint32_t[]> cuIdxs(new uint32_t[chunks.size()]); 27700b57cec5SDimitry Andric for (uint32_t i = 0, e = chunks.size(); i != e; ++i) { 27710b57cec5SDimitry Andric cuIdxs[i] = cuIdx; 27720b57cec5SDimitry Andric cuIdx += chunks[i].compilationUnits.size(); 27730b57cec5SDimitry Andric } 27740b57cec5SDimitry Andric 27750b57cec5SDimitry Andric // The number of symbols we will handle in this function is of the order 27760b57cec5SDimitry Andric // of millions for very large executables, so we use multi-threading to 27770b57cec5SDimitry Andric // speed it up. 27785ffd83dbSDimitry Andric constexpr size_t numShards = 32; 2779*bdd1243dSDimitry Andric const size_t concurrency = 2780*bdd1243dSDimitry Andric PowerOf2Floor(std::min<size_t>(config->threadCount, numShards)); 27810b57cec5SDimitry Andric 27820b57cec5SDimitry Andric // A sharded map to uniquify symbols by name. 278304eeddc0SDimitry Andric auto map = 278404eeddc0SDimitry Andric std::make_unique<DenseMap<CachedHashStringRef, size_t>[]>(numShards); 27850b57cec5SDimitry Andric size_t shift = 32 - countTrailingZeros(numShards); 27860b57cec5SDimitry Andric 27870b57cec5SDimitry Andric // Instantiate GdbSymbols while uniqufying them by name. 27881fd87a68SDimitry Andric auto symbols = std::make_unique<SmallVector<GdbSymbol, 0>[]>(numShards); 278904eeddc0SDimitry Andric 279081ad6265SDimitry Andric parallelFor(0, concurrency, [&](size_t threadId) { 27910b57cec5SDimitry Andric uint32_t i = 0; 27920b57cec5SDimitry Andric for (ArrayRef<NameAttrEntry> entries : nameAttrs) { 27930b57cec5SDimitry Andric for (const NameAttrEntry &ent : entries) { 27940b57cec5SDimitry Andric size_t shardId = ent.name.hash() >> shift; 27950b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) != threadId) 27960b57cec5SDimitry Andric continue; 27970b57cec5SDimitry Andric 27980b57cec5SDimitry Andric uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i]; 27990b57cec5SDimitry Andric size_t &idx = map[shardId][ent.name]; 28000b57cec5SDimitry Andric if (idx) { 28010b57cec5SDimitry Andric symbols[shardId][idx - 1].cuVector.push_back(v); 28020b57cec5SDimitry Andric continue; 28030b57cec5SDimitry Andric } 28040b57cec5SDimitry Andric 28050b57cec5SDimitry Andric idx = symbols[shardId].size() + 1; 28060b57cec5SDimitry Andric symbols[shardId].push_back({ent.name, {v}, 0, 0}); 28070b57cec5SDimitry Andric } 28080b57cec5SDimitry Andric ++i; 28090b57cec5SDimitry Andric } 28100b57cec5SDimitry Andric }); 28110b57cec5SDimitry Andric 28120b57cec5SDimitry Andric size_t numSymbols = 0; 2813*bdd1243dSDimitry Andric for (ArrayRef<GdbSymbol> v : ArrayRef(symbols.get(), numShards)) 28140b57cec5SDimitry Andric numSymbols += v.size(); 28150b57cec5SDimitry Andric 28160b57cec5SDimitry Andric // The return type is a flattened vector, so we'll copy each vector 28170b57cec5SDimitry Andric // contents to Ret. 28181fd87a68SDimitry Andric SmallVector<GdbSymbol, 0> ret; 28190b57cec5SDimitry Andric ret.reserve(numSymbols); 28201fd87a68SDimitry Andric for (SmallVector<GdbSymbol, 0> &vec : 2821*bdd1243dSDimitry Andric MutableArrayRef(symbols.get(), numShards)) 28220b57cec5SDimitry Andric for (GdbSymbol &sym : vec) 28230b57cec5SDimitry Andric ret.push_back(std::move(sym)); 28240b57cec5SDimitry Andric 28250b57cec5SDimitry Andric // CU vectors and symbol names are adjacent in the output file. 28260b57cec5SDimitry Andric // We can compute their offsets in the output file now. 28270b57cec5SDimitry Andric size_t off = 0; 28280b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 28290b57cec5SDimitry Andric sym.cuVectorOff = off; 28300b57cec5SDimitry Andric off += (sym.cuVector.size() + 1) * 4; 28310b57cec5SDimitry Andric } 28320b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 28330b57cec5SDimitry Andric sym.nameOff = off; 28340b57cec5SDimitry Andric off += sym.name.size() + 1; 28350b57cec5SDimitry Andric } 2836*bdd1243dSDimitry Andric // If off overflows, the last symbol's nameOff likely overflows. 2837*bdd1243dSDimitry Andric if (!isUInt<32>(off)) 2838*bdd1243dSDimitry Andric errorOrWarn("--gdb-index: constant pool size (" + Twine(off) + 2839*bdd1243dSDimitry Andric ") exceeds UINT32_MAX"); 28400b57cec5SDimitry Andric 2841*bdd1243dSDimitry Andric return {ret, off}; 28420b57cec5SDimitry Andric } 28430b57cec5SDimitry Andric 28440b57cec5SDimitry Andric // Returns a newly-created .gdb_index section. 28450b57cec5SDimitry Andric template <class ELFT> GdbIndexSection *GdbIndexSection::create() { 2846*bdd1243dSDimitry Andric llvm::TimeTraceScope timeScope("Create gdb index"); 2847*bdd1243dSDimitry Andric 284816d6b3b3SDimitry Andric // Collect InputFiles with .debug_info. See the comment in 284916d6b3b3SDimitry Andric // LLDDwarfObj<ELFT>::LLDDwarfObj. If we do lightweight parsing in the future, 285016d6b3b3SDimitry Andric // note that isec->data() may uncompress the full content, which should be 285116d6b3b3SDimitry Andric // parallelized. 285216d6b3b3SDimitry Andric SetVector<InputFile *> files; 2853*bdd1243dSDimitry Andric for (InputSectionBase *s : ctx.inputSections) { 285416d6b3b3SDimitry Andric InputSection *isec = dyn_cast<InputSection>(s); 285516d6b3b3SDimitry Andric if (!isec) 285616d6b3b3SDimitry Andric continue; 28570b57cec5SDimitry Andric // .debug_gnu_pub{names,types} are useless in executables. 28580b57cec5SDimitry Andric // They are present in input object files solely for creating 28590b57cec5SDimitry Andric // a .gdb_index. So we can remove them from the output. 28600b57cec5SDimitry Andric if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes") 28610b57cec5SDimitry Andric s->markDead(); 286216d6b3b3SDimitry Andric else if (isec->name == ".debug_info") 286316d6b3b3SDimitry Andric files.insert(isec->file); 286416d6b3b3SDimitry Andric } 2865e8d8bef9SDimitry Andric // Drop .rel[a].debug_gnu_pub{names,types} for --emit-relocs. 2866*bdd1243dSDimitry Andric llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { 2867e8d8bef9SDimitry Andric if (auto *isec = dyn_cast<InputSection>(s)) 2868e8d8bef9SDimitry Andric if (InputSectionBase *rel = isec->getRelocatedSection()) 2869e8d8bef9SDimitry Andric return !rel->isLive(); 2870e8d8bef9SDimitry Andric return !s->isLive(); 2871e8d8bef9SDimitry Andric }); 28720b57cec5SDimitry Andric 28731fd87a68SDimitry Andric SmallVector<GdbChunk, 0> chunks(files.size()); 28741fd87a68SDimitry Andric SmallVector<SmallVector<NameAttrEntry, 0>, 0> nameAttrs(files.size()); 28750b57cec5SDimitry Andric 287681ad6265SDimitry Andric parallelFor(0, files.size(), [&](size_t i) { 28775ffd83dbSDimitry Andric // To keep memory usage low, we don't want to keep cached DWARFContext, so 28785ffd83dbSDimitry Andric // avoid getDwarf() here. 287916d6b3b3SDimitry Andric ObjFile<ELFT> *file = cast<ObjFile<ELFT>>(files[i]); 288085868e8aSDimitry Andric DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file)); 288116d6b3b3SDimitry Andric auto &dobj = static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj()); 28820b57cec5SDimitry Andric 288316d6b3b3SDimitry Andric // If the are multiple compile units .debug_info (very rare ld -r --unique), 288416d6b3b3SDimitry Andric // this only picks the last one. Other address ranges are lost. 288516d6b3b3SDimitry Andric chunks[i].sec = dobj.getInfoSection(); 28860b57cec5SDimitry Andric chunks[i].compilationUnits = readCuList(dwarf); 288716d6b3b3SDimitry Andric chunks[i].addressAreas = readAddressAreas(dwarf, chunks[i].sec); 288816d6b3b3SDimitry Andric nameAttrs[i] = readPubNamesAndTypes<ELFT>(dobj, chunks[i].compilationUnits); 28890b57cec5SDimitry Andric }); 28900b57cec5SDimitry Andric 28910b57cec5SDimitry Andric auto *ret = make<GdbIndexSection>(); 28920b57cec5SDimitry Andric ret->chunks = std::move(chunks); 2893*bdd1243dSDimitry Andric std::tie(ret->symbols, ret->size) = createSymbols(nameAttrs, ret->chunks); 2894*bdd1243dSDimitry Andric 2895*bdd1243dSDimitry Andric // Count the areas other than the constant pool. 2896*bdd1243dSDimitry Andric ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8; 2897*bdd1243dSDimitry Andric for (GdbChunk &chunk : ret->chunks) 2898*bdd1243dSDimitry Andric ret->size += 2899*bdd1243dSDimitry Andric chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20; 2900*bdd1243dSDimitry Andric 29010b57cec5SDimitry Andric return ret; 29020b57cec5SDimitry Andric } 29030b57cec5SDimitry Andric 29040b57cec5SDimitry Andric void GdbIndexSection::writeTo(uint8_t *buf) { 29050b57cec5SDimitry Andric // Write the header. 29060b57cec5SDimitry Andric auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf); 29070b57cec5SDimitry Andric uint8_t *start = buf; 29080b57cec5SDimitry Andric hdr->version = 7; 29090b57cec5SDimitry Andric buf += sizeof(*hdr); 29100b57cec5SDimitry Andric 29110b57cec5SDimitry Andric // Write the CU list. 29120b57cec5SDimitry Andric hdr->cuListOff = buf - start; 29130b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 29140b57cec5SDimitry Andric for (CuEntry &cu : chunk.compilationUnits) { 29150b57cec5SDimitry Andric write64le(buf, chunk.sec->outSecOff + cu.cuOffset); 29160b57cec5SDimitry Andric write64le(buf + 8, cu.cuLength); 29170b57cec5SDimitry Andric buf += 16; 29180b57cec5SDimitry Andric } 29190b57cec5SDimitry Andric } 29200b57cec5SDimitry Andric 29210b57cec5SDimitry Andric // Write the address area. 29220b57cec5SDimitry Andric hdr->cuTypesOff = buf - start; 29230b57cec5SDimitry Andric hdr->addressAreaOff = buf - start; 29240b57cec5SDimitry Andric uint32_t cuOff = 0; 29250b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 29260b57cec5SDimitry Andric for (AddressEntry &e : chunk.addressAreas) { 2927e8d8bef9SDimitry Andric // In the case of ICF there may be duplicate address range entries. 2928e8d8bef9SDimitry Andric const uint64_t baseAddr = e.section->repl->getVA(0); 29290b57cec5SDimitry Andric write64le(buf, baseAddr + e.lowAddress); 29300b57cec5SDimitry Andric write64le(buf + 8, baseAddr + e.highAddress); 29310b57cec5SDimitry Andric write32le(buf + 16, e.cuIndex + cuOff); 29320b57cec5SDimitry Andric buf += 20; 29330b57cec5SDimitry Andric } 29340b57cec5SDimitry Andric cuOff += chunk.compilationUnits.size(); 29350b57cec5SDimitry Andric } 29360b57cec5SDimitry Andric 29370b57cec5SDimitry Andric // Write the on-disk open-addressing hash table containing symbols. 29380b57cec5SDimitry Andric hdr->symtabOff = buf - start; 29390b57cec5SDimitry Andric size_t symtabSize = computeSymtabSize(); 29400b57cec5SDimitry Andric uint32_t mask = symtabSize - 1; 29410b57cec5SDimitry Andric 29420b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 29430b57cec5SDimitry Andric uint32_t h = sym.name.hash(); 29440b57cec5SDimitry Andric uint32_t i = h & mask; 29450b57cec5SDimitry Andric uint32_t step = ((h * 17) & mask) | 1; 29460b57cec5SDimitry Andric 29470b57cec5SDimitry Andric while (read32le(buf + i * 8)) 29480b57cec5SDimitry Andric i = (i + step) & mask; 29490b57cec5SDimitry Andric 29500b57cec5SDimitry Andric write32le(buf + i * 8, sym.nameOff); 29510b57cec5SDimitry Andric write32le(buf + i * 8 + 4, sym.cuVectorOff); 29520b57cec5SDimitry Andric } 29530b57cec5SDimitry Andric 29540b57cec5SDimitry Andric buf += symtabSize * 8; 29550b57cec5SDimitry Andric 29560b57cec5SDimitry Andric // Write the string pool. 29570b57cec5SDimitry Andric hdr->constantPoolOff = buf - start; 29580b57cec5SDimitry Andric parallelForEach(symbols, [&](GdbSymbol &sym) { 29590b57cec5SDimitry Andric memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size()); 29600b57cec5SDimitry Andric }); 29610b57cec5SDimitry Andric 29620b57cec5SDimitry Andric // Write the CU vectors. 29630b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 29640b57cec5SDimitry Andric write32le(buf, sym.cuVector.size()); 29650b57cec5SDimitry Andric buf += 4; 29660b57cec5SDimitry Andric for (uint32_t val : sym.cuVector) { 29670b57cec5SDimitry Andric write32le(buf, val); 29680b57cec5SDimitry Andric buf += 4; 29690b57cec5SDimitry Andric } 29700b57cec5SDimitry Andric } 29710b57cec5SDimitry Andric } 29720b57cec5SDimitry Andric 29730b57cec5SDimitry Andric bool GdbIndexSection::isNeeded() const { return !chunks.empty(); } 29740b57cec5SDimitry Andric 29750b57cec5SDimitry Andric EhFrameHeader::EhFrameHeader() 29760b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {} 29770b57cec5SDimitry Andric 29780b57cec5SDimitry Andric void EhFrameHeader::writeTo(uint8_t *buf) { 29790b57cec5SDimitry Andric // Unlike most sections, the EhFrameHeader section is written while writing 29800b57cec5SDimitry Andric // another section, namely EhFrameSection, which calls the write() function 29810b57cec5SDimitry Andric // below from its writeTo() function. This is necessary because the contents 29820b57cec5SDimitry Andric // of EhFrameHeader depend on the relocated contents of EhFrameSection and we 29830b57cec5SDimitry Andric // don't know which order the sections will be written in. 29840b57cec5SDimitry Andric } 29850b57cec5SDimitry Andric 29860b57cec5SDimitry Andric // .eh_frame_hdr contains a binary search table of pointers to FDEs. 29870b57cec5SDimitry Andric // Each entry of the search table consists of two values, 29880b57cec5SDimitry Andric // the starting PC from where FDEs covers, and the FDE's address. 29890b57cec5SDimitry Andric // It is sorted by PC. 29900b57cec5SDimitry Andric void EhFrameHeader::write() { 29910b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 29920b57cec5SDimitry Andric using FdeData = EhFrameSection::FdeData; 299304eeddc0SDimitry Andric SmallVector<FdeData, 0> fdes = getPartition().ehFrame->getFdeData(); 29940b57cec5SDimitry Andric 29950b57cec5SDimitry Andric buf[0] = 1; 29960b57cec5SDimitry Andric buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; 29970b57cec5SDimitry Andric buf[2] = DW_EH_PE_udata4; 29980b57cec5SDimitry Andric buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; 29990b57cec5SDimitry Andric write32(buf + 4, 30000b57cec5SDimitry Andric getPartition().ehFrame->getParent()->addr - this->getVA() - 4); 30010b57cec5SDimitry Andric write32(buf + 8, fdes.size()); 30020b57cec5SDimitry Andric buf += 12; 30030b57cec5SDimitry Andric 30040b57cec5SDimitry Andric for (FdeData &fde : fdes) { 30050b57cec5SDimitry Andric write32(buf, fde.pcRel); 30060b57cec5SDimitry Andric write32(buf + 4, fde.fdeVARel); 30070b57cec5SDimitry Andric buf += 8; 30080b57cec5SDimitry Andric } 30090b57cec5SDimitry Andric } 30100b57cec5SDimitry Andric 30110b57cec5SDimitry Andric size_t EhFrameHeader::getSize() const { 30120b57cec5SDimitry Andric // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. 30130b57cec5SDimitry Andric return 12 + getPartition().ehFrame->numFdes * 8; 30140b57cec5SDimitry Andric } 30150b57cec5SDimitry Andric 30160b57cec5SDimitry Andric bool EhFrameHeader::isNeeded() const { 30170b57cec5SDimitry Andric return isLive() && getPartition().ehFrame->isNeeded(); 30180b57cec5SDimitry Andric } 30190b57cec5SDimitry Andric 30200b57cec5SDimitry Andric VersionDefinitionSection::VersionDefinitionSection() 30210b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), 30220b57cec5SDimitry Andric ".gnu.version_d") {} 30230b57cec5SDimitry Andric 30240b57cec5SDimitry Andric StringRef VersionDefinitionSection::getFileDefName() { 30250b57cec5SDimitry Andric if (!getPartition().name.empty()) 30260b57cec5SDimitry Andric return getPartition().name; 30270b57cec5SDimitry Andric if (!config->soName.empty()) 30280b57cec5SDimitry Andric return config->soName; 30290b57cec5SDimitry Andric return config->outputFile; 30300b57cec5SDimitry Andric } 30310b57cec5SDimitry Andric 30320b57cec5SDimitry Andric void VersionDefinitionSection::finalizeContents() { 30330b57cec5SDimitry Andric fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName()); 303485868e8aSDimitry Andric for (const VersionDefinition &v : namedVersionDefs()) 30350b57cec5SDimitry Andric verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name)); 30360b57cec5SDimitry Andric 30370b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 30380b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 30390b57cec5SDimitry Andric 30400b57cec5SDimitry Andric // sh_info should be set to the number of definitions. This fact is missed in 30410b57cec5SDimitry Andric // documentation, but confirmed by binutils community: 30420b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2014-11/msg00355.html 30430b57cec5SDimitry Andric getParent()->info = getVerDefNum(); 30440b57cec5SDimitry Andric } 30450b57cec5SDimitry Andric 30460b57cec5SDimitry Andric void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index, 30470b57cec5SDimitry Andric StringRef name, size_t nameOff) { 30480b57cec5SDimitry Andric uint16_t flags = index == 1 ? VER_FLG_BASE : 0; 30490b57cec5SDimitry Andric 30500b57cec5SDimitry Andric // Write a verdef. 30510b57cec5SDimitry Andric write16(buf, 1); // vd_version 30520b57cec5SDimitry Andric write16(buf + 2, flags); // vd_flags 30530b57cec5SDimitry Andric write16(buf + 4, index); // vd_ndx 30540b57cec5SDimitry Andric write16(buf + 6, 1); // vd_cnt 30550b57cec5SDimitry Andric write32(buf + 8, hashSysV(name)); // vd_hash 30560b57cec5SDimitry Andric write32(buf + 12, 20); // vd_aux 30570b57cec5SDimitry Andric write32(buf + 16, 28); // vd_next 30580b57cec5SDimitry Andric 30590b57cec5SDimitry Andric // Write a veraux. 30600b57cec5SDimitry Andric write32(buf + 20, nameOff); // vda_name 30610b57cec5SDimitry Andric write32(buf + 24, 0); // vda_next 30620b57cec5SDimitry Andric } 30630b57cec5SDimitry Andric 30640b57cec5SDimitry Andric void VersionDefinitionSection::writeTo(uint8_t *buf) { 30650b57cec5SDimitry Andric writeOne(buf, 1, getFileDefName(), fileDefNameOff); 30660b57cec5SDimitry Andric 30670b57cec5SDimitry Andric auto nameOffIt = verDefNameOffs.begin(); 306885868e8aSDimitry Andric for (const VersionDefinition &v : namedVersionDefs()) { 30690b57cec5SDimitry Andric buf += EntrySize; 30700b57cec5SDimitry Andric writeOne(buf, v.id, v.name, *nameOffIt++); 30710b57cec5SDimitry Andric } 30720b57cec5SDimitry Andric 30730b57cec5SDimitry Andric // Need to terminate the last version definition. 30740b57cec5SDimitry Andric write32(buf + 16, 0); // vd_next 30750b57cec5SDimitry Andric } 30760b57cec5SDimitry Andric 30770b57cec5SDimitry Andric size_t VersionDefinitionSection::getSize() const { 30780b57cec5SDimitry Andric return EntrySize * getVerDefNum(); 30790b57cec5SDimitry Andric } 30800b57cec5SDimitry Andric 30810b57cec5SDimitry Andric // .gnu.version is a table where each entry is 2 byte long. 30820b57cec5SDimitry Andric VersionTableSection::VersionTableSection() 30830b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), 30840b57cec5SDimitry Andric ".gnu.version") { 30850b57cec5SDimitry Andric this->entsize = 2; 30860b57cec5SDimitry Andric } 30870b57cec5SDimitry Andric 30880b57cec5SDimitry Andric void VersionTableSection::finalizeContents() { 30890b57cec5SDimitry Andric // At the moment of june 2016 GNU docs does not mention that sh_link field 30900b57cec5SDimitry Andric // should be set, but Sun docs do. Also readelf relies on this field. 30910b57cec5SDimitry Andric getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex; 30920b57cec5SDimitry Andric } 30930b57cec5SDimitry Andric 30940b57cec5SDimitry Andric size_t VersionTableSection::getSize() const { 30950b57cec5SDimitry Andric return (getPartition().dynSymTab->getSymbols().size() + 1) * 2; 30960b57cec5SDimitry Andric } 30970b57cec5SDimitry Andric 30980b57cec5SDimitry Andric void VersionTableSection::writeTo(uint8_t *buf) { 30990b57cec5SDimitry Andric buf += 2; 31000b57cec5SDimitry Andric for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) { 31014824e7fdSDimitry Andric // For an unextracted lazy symbol (undefined weak), it must have been 3102349cc55cSDimitry Andric // converted to Undefined and have VER_NDX_GLOBAL version here. 3103349cc55cSDimitry Andric assert(!s.sym->isLazy()); 3104349cc55cSDimitry Andric write16(buf, s.sym->versionId); 31050b57cec5SDimitry Andric buf += 2; 31060b57cec5SDimitry Andric } 31070b57cec5SDimitry Andric } 31080b57cec5SDimitry Andric 31090b57cec5SDimitry Andric bool VersionTableSection::isNeeded() const { 3110480093f4SDimitry Andric return isLive() && 3111480093f4SDimitry Andric (getPartition().verDef || getPartition().verNeed->isNeeded()); 31120b57cec5SDimitry Andric } 31130b57cec5SDimitry Andric 31145ffd83dbSDimitry Andric void elf::addVerneed(Symbol *ss) { 31150b57cec5SDimitry Andric auto &file = cast<SharedFile>(*ss->file); 31160b57cec5SDimitry Andric if (ss->verdefIndex == VER_NDX_GLOBAL) { 31170b57cec5SDimitry Andric ss->versionId = VER_NDX_GLOBAL; 31180b57cec5SDimitry Andric return; 31190b57cec5SDimitry Andric } 31200b57cec5SDimitry Andric 31210b57cec5SDimitry Andric if (file.vernauxs.empty()) 31220b57cec5SDimitry Andric file.vernauxs.resize(file.verdefs.size()); 31230b57cec5SDimitry Andric 31240b57cec5SDimitry Andric // Select a version identifier for the vernaux data structure, if we haven't 31250b57cec5SDimitry Andric // already allocated one. The verdef identifiers cover the range 31260b57cec5SDimitry Andric // [1..getVerDefNum()]; this causes the vernaux identifiers to start from 31270b57cec5SDimitry Andric // getVerDefNum()+1. 31280b57cec5SDimitry Andric if (file.vernauxs[ss->verdefIndex] == 0) 31290b57cec5SDimitry Andric file.vernauxs[ss->verdefIndex] = ++SharedFile::vernauxNum + getVerDefNum(); 31300b57cec5SDimitry Andric 31310b57cec5SDimitry Andric ss->versionId = file.vernauxs[ss->verdefIndex]; 31320b57cec5SDimitry Andric } 31330b57cec5SDimitry Andric 31340b57cec5SDimitry Andric template <class ELFT> 31350b57cec5SDimitry Andric VersionNeedSection<ELFT>::VersionNeedSection() 31360b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), 31370b57cec5SDimitry Andric ".gnu.version_r") {} 31380b57cec5SDimitry Andric 31390b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { 3140*bdd1243dSDimitry Andric for (SharedFile *f : ctx.sharedFiles) { 31410b57cec5SDimitry Andric if (f->vernauxs.empty()) 31420b57cec5SDimitry Andric continue; 31430b57cec5SDimitry Andric verneeds.emplace_back(); 31440b57cec5SDimitry Andric Verneed &vn = verneeds.back(); 31450b57cec5SDimitry Andric vn.nameStrTab = getPartition().dynStrTab->addString(f->soName); 314681ad6265SDimitry Andric bool isLibc = config->relrGlibc && f->soName.startswith("libc.so."); 314781ad6265SDimitry Andric bool isGlibc2 = false; 31480b57cec5SDimitry Andric for (unsigned i = 0; i != f->vernauxs.size(); ++i) { 31490b57cec5SDimitry Andric if (f->vernauxs[i] == 0) 31500b57cec5SDimitry Andric continue; 31510b57cec5SDimitry Andric auto *verdef = 31520b57cec5SDimitry Andric reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]); 315381ad6265SDimitry Andric StringRef ver(f->getStringTable().data() + verdef->getAux()->vda_name); 315481ad6265SDimitry Andric if (isLibc && ver.startswith("GLIBC_2.")) 315581ad6265SDimitry Andric isGlibc2 = true; 315681ad6265SDimitry Andric vn.vernauxs.push_back({verdef->vd_hash, f->vernauxs[i], 315781ad6265SDimitry Andric getPartition().dynStrTab->addString(ver)}); 315881ad6265SDimitry Andric } 315981ad6265SDimitry Andric if (isGlibc2) { 316081ad6265SDimitry Andric const char *ver = "GLIBC_ABI_DT_RELR"; 316181ad6265SDimitry Andric vn.vernauxs.push_back({hashSysV(ver), 316281ad6265SDimitry Andric ++SharedFile::vernauxNum + getVerDefNum(), 316381ad6265SDimitry Andric getPartition().dynStrTab->addString(ver)}); 31640b57cec5SDimitry Andric } 31650b57cec5SDimitry Andric } 31660b57cec5SDimitry Andric 31670b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 31680b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 31690b57cec5SDimitry Andric getParent()->info = verneeds.size(); 31700b57cec5SDimitry Andric } 31710b57cec5SDimitry Andric 31720b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) { 31730b57cec5SDimitry Andric // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. 31740b57cec5SDimitry Andric auto *verneed = reinterpret_cast<Elf_Verneed *>(buf); 31750b57cec5SDimitry Andric auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size()); 31760b57cec5SDimitry Andric 31770b57cec5SDimitry Andric for (auto &vn : verneeds) { 31780b57cec5SDimitry Andric // Create an Elf_Verneed for this DSO. 31790b57cec5SDimitry Andric verneed->vn_version = 1; 31800b57cec5SDimitry Andric verneed->vn_cnt = vn.vernauxs.size(); 31810b57cec5SDimitry Andric verneed->vn_file = vn.nameStrTab; 31820b57cec5SDimitry Andric verneed->vn_aux = 31830b57cec5SDimitry Andric reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed); 31840b57cec5SDimitry Andric verneed->vn_next = sizeof(Elf_Verneed); 31850b57cec5SDimitry Andric ++verneed; 31860b57cec5SDimitry Andric 31870b57cec5SDimitry Andric // Create the Elf_Vernauxs for this Elf_Verneed. 31880b57cec5SDimitry Andric for (auto &vna : vn.vernauxs) { 31890b57cec5SDimitry Andric vernaux->vna_hash = vna.hash; 31900b57cec5SDimitry Andric vernaux->vna_flags = 0; 31910b57cec5SDimitry Andric vernaux->vna_other = vna.verneedIndex; 31920b57cec5SDimitry Andric vernaux->vna_name = vna.nameStrTab; 31930b57cec5SDimitry Andric vernaux->vna_next = sizeof(Elf_Vernaux); 31940b57cec5SDimitry Andric ++vernaux; 31950b57cec5SDimitry Andric } 31960b57cec5SDimitry Andric 31970b57cec5SDimitry Andric vernaux[-1].vna_next = 0; 31980b57cec5SDimitry Andric } 31990b57cec5SDimitry Andric verneed[-1].vn_next = 0; 32000b57cec5SDimitry Andric } 32010b57cec5SDimitry Andric 32020b57cec5SDimitry Andric template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { 32030b57cec5SDimitry Andric return verneeds.size() * sizeof(Elf_Verneed) + 32040b57cec5SDimitry Andric SharedFile::vernauxNum * sizeof(Elf_Vernaux); 32050b57cec5SDimitry Andric } 32060b57cec5SDimitry Andric 32070b57cec5SDimitry Andric template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const { 3208480093f4SDimitry Andric return isLive() && SharedFile::vernauxNum != 0; 32090b57cec5SDimitry Andric } 32100b57cec5SDimitry Andric 32110b57cec5SDimitry Andric void MergeSyntheticSection::addSection(MergeInputSection *ms) { 32120b57cec5SDimitry Andric ms->parent = this; 32130b57cec5SDimitry Andric sections.push_back(ms); 3214*bdd1243dSDimitry Andric assert(addralign == ms->addralign || !(ms->flags & SHF_STRINGS)); 3215*bdd1243dSDimitry Andric addralign = std::max(addralign, ms->addralign); 32160b57cec5SDimitry Andric } 32170b57cec5SDimitry Andric 32180b57cec5SDimitry Andric MergeTailSection::MergeTailSection(StringRef name, uint32_t type, 32190b57cec5SDimitry Andric uint64_t flags, uint32_t alignment) 32200b57cec5SDimitry Andric : MergeSyntheticSection(name, type, flags, alignment), 3221*bdd1243dSDimitry Andric builder(StringTableBuilder::RAW, llvm::Align(alignment)) {} 32220b57cec5SDimitry Andric 32230b57cec5SDimitry Andric size_t MergeTailSection::getSize() const { return builder.getSize(); } 32240b57cec5SDimitry Andric 32250b57cec5SDimitry Andric void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); } 32260b57cec5SDimitry Andric 32270b57cec5SDimitry Andric void MergeTailSection::finalizeContents() { 32280b57cec5SDimitry Andric // Add all string pieces to the string table builder to create section 32290b57cec5SDimitry Andric // contents. 32300b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 32310b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 32320b57cec5SDimitry Andric if (sec->pieces[i].live) 32330b57cec5SDimitry Andric builder.add(sec->getData(i)); 32340b57cec5SDimitry Andric 32350b57cec5SDimitry Andric // Fix the string table content. After this, the contents will never change. 32360b57cec5SDimitry Andric builder.finalize(); 32370b57cec5SDimitry Andric 32380b57cec5SDimitry Andric // finalize() fixed tail-optimized strings, so we can now get 32390b57cec5SDimitry Andric // offsets of strings. Get an offset for each string and save it 32400b57cec5SDimitry Andric // to a corresponding SectionPiece for easy access. 32410b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 32420b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 32430b57cec5SDimitry Andric if (sec->pieces[i].live) 32440b57cec5SDimitry Andric sec->pieces[i].outputOff = builder.getOffset(sec->getData(i)); 32450b57cec5SDimitry Andric } 32460b57cec5SDimitry Andric 32470b57cec5SDimitry Andric void MergeNoTailSection::writeTo(uint8_t *buf) { 324881ad6265SDimitry Andric parallelFor(0, numShards, 32490eae32dcSDimitry Andric [&](size_t i) { shards[i].write(buf + shardOffsets[i]); }); 32500b57cec5SDimitry Andric } 32510b57cec5SDimitry Andric 32520b57cec5SDimitry Andric // This function is very hot (i.e. it can take several seconds to finish) 32530b57cec5SDimitry Andric // because sometimes the number of inputs is in an order of magnitude of 32540b57cec5SDimitry Andric // millions. So, we use multi-threading. 32550b57cec5SDimitry Andric // 32560b57cec5SDimitry Andric // For any strings S and T, we know S is not mergeable with T if S's hash 32570b57cec5SDimitry Andric // value is different from T's. If that's the case, we can safely put S and 32580b57cec5SDimitry Andric // T into different string builders without worrying about merge misses. 32590b57cec5SDimitry Andric // We do it in parallel. 32600b57cec5SDimitry Andric void MergeNoTailSection::finalizeContents() { 32610b57cec5SDimitry Andric // Initializes string table builders. 32620b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) 3263*bdd1243dSDimitry Andric shards.emplace_back(StringTableBuilder::RAW, llvm::Align(addralign)); 32640b57cec5SDimitry Andric 32650b57cec5SDimitry Andric // Concurrency level. Must be a power of 2 to avoid expensive modulo 32660b57cec5SDimitry Andric // operations in the following tight loop. 3267*bdd1243dSDimitry Andric const size_t concurrency = 3268*bdd1243dSDimitry Andric PowerOf2Floor(std::min<size_t>(config->threadCount, numShards)); 32690b57cec5SDimitry Andric 32700b57cec5SDimitry Andric // Add section pieces to the builders. 327181ad6265SDimitry Andric parallelFor(0, concurrency, [&](size_t threadId) { 32720b57cec5SDimitry Andric for (MergeInputSection *sec : sections) { 32730b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) { 32740b57cec5SDimitry Andric if (!sec->pieces[i].live) 32750b57cec5SDimitry Andric continue; 32760b57cec5SDimitry Andric size_t shardId = getShardId(sec->pieces[i].hash); 32770b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) == threadId) 32780b57cec5SDimitry Andric sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i)); 32790b57cec5SDimitry Andric } 32800b57cec5SDimitry Andric } 32810b57cec5SDimitry Andric }); 32820b57cec5SDimitry Andric 32830b57cec5SDimitry Andric // Compute an in-section offset for each shard. 32840b57cec5SDimitry Andric size_t off = 0; 32850b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) { 32860b57cec5SDimitry Andric shards[i].finalizeInOrder(); 32870b57cec5SDimitry Andric if (shards[i].getSize() > 0) 3288*bdd1243dSDimitry Andric off = alignToPowerOf2(off, addralign); 32890b57cec5SDimitry Andric shardOffsets[i] = off; 32900b57cec5SDimitry Andric off += shards[i].getSize(); 32910b57cec5SDimitry Andric } 32920b57cec5SDimitry Andric size = off; 32930b57cec5SDimitry Andric 32940b57cec5SDimitry Andric // So far, section pieces have offsets from beginning of shards, but 32950b57cec5SDimitry Andric // we want offsets from beginning of the whole section. Fix them. 32960b57cec5SDimitry Andric parallelForEach(sections, [&](MergeInputSection *sec) { 32970b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 32980b57cec5SDimitry Andric if (sec->pieces[i].live) 32990b57cec5SDimitry Andric sec->pieces[i].outputOff += 33000b57cec5SDimitry Andric shardOffsets[getShardId(sec->pieces[i].hash)]; 33010b57cec5SDimitry Andric }); 33020b57cec5SDimitry Andric } 33030b57cec5SDimitry Andric 33045ffd83dbSDimitry Andric template <class ELFT> void elf::splitSections() { 33055ffd83dbSDimitry Andric llvm::TimeTraceScope timeScope("Split sections"); 33060b57cec5SDimitry Andric // splitIntoPieces needs to be called on each MergeInputSection 33070b57cec5SDimitry Andric // before calling finalizeContents(). 3308*bdd1243dSDimitry Andric parallelForEach(ctx.objectFiles, [](ELFFileBase *file) { 33091fd87a68SDimitry Andric for (InputSectionBase *sec : file->getSections()) { 33101fd87a68SDimitry Andric if (!sec) 33111fd87a68SDimitry Andric continue; 33120b57cec5SDimitry Andric if (auto *s = dyn_cast<MergeInputSection>(sec)) 33130b57cec5SDimitry Andric s->splitIntoPieces(); 33140b57cec5SDimitry Andric else if (auto *eh = dyn_cast<EhInputSection>(sec)) 33150b57cec5SDimitry Andric eh->split<ELFT>(); 33161fd87a68SDimitry Andric } 33170b57cec5SDimitry Andric }); 33180b57cec5SDimitry Andric } 33190b57cec5SDimitry Andric 3320*bdd1243dSDimitry Andric void elf::combineEhSections() { 3321*bdd1243dSDimitry Andric llvm::TimeTraceScope timeScope("Combine EH sections"); 3322*bdd1243dSDimitry Andric for (EhInputSection *sec : ctx.ehInputSections) { 3323*bdd1243dSDimitry Andric EhFrameSection &eh = *sec->getPartition().ehFrame; 3324*bdd1243dSDimitry Andric sec->parent = &eh; 3325*bdd1243dSDimitry Andric eh.addralign = std::max(eh.addralign, sec->addralign); 3326*bdd1243dSDimitry Andric eh.sections.push_back(sec); 3327*bdd1243dSDimitry Andric llvm::append_range(eh.dependentSections, sec->dependentSections); 3328*bdd1243dSDimitry Andric } 3329*bdd1243dSDimitry Andric 3330*bdd1243dSDimitry Andric if (!mainPart->armExidx) 3331*bdd1243dSDimitry Andric return; 3332*bdd1243dSDimitry Andric llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { 3333*bdd1243dSDimitry Andric // Ignore dead sections and the partition end marker (.part.end), 3334*bdd1243dSDimitry Andric // whose partition number is out of bounds. 3335*bdd1243dSDimitry Andric if (!s->isLive() || s->partition == 255) 3336*bdd1243dSDimitry Andric return false; 3337*bdd1243dSDimitry Andric Partition &part = s->getPartition(); 3338*bdd1243dSDimitry Andric return s->kind() == SectionBase::Regular && part.armExidx && 3339*bdd1243dSDimitry Andric part.armExidx->addSection(cast<InputSection>(s)); 3340*bdd1243dSDimitry Andric }); 3341*bdd1243dSDimitry Andric } 3342*bdd1243dSDimitry Andric 33430b57cec5SDimitry Andric MipsRldMapSection::MipsRldMapSection() 33440b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 33450b57cec5SDimitry Andric ".rld_map") {} 33460b57cec5SDimitry Andric 33470b57cec5SDimitry Andric ARMExidxSyntheticSection::ARMExidxSyntheticSection() 33480b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, 33490b57cec5SDimitry Andric config->wordsize, ".ARM.exidx") {} 33500b57cec5SDimitry Andric 33510b57cec5SDimitry Andric static InputSection *findExidxSection(InputSection *isec) { 33520b57cec5SDimitry Andric for (InputSection *d : isec->dependentSections) 33535ffd83dbSDimitry Andric if (d->type == SHT_ARM_EXIDX && d->isLive()) 33540b57cec5SDimitry Andric return d; 33550b57cec5SDimitry Andric return nullptr; 33560b57cec5SDimitry Andric } 33570b57cec5SDimitry Andric 335885868e8aSDimitry Andric static bool isValidExidxSectionDep(InputSection *isec) { 335985868e8aSDimitry Andric return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) && 336085868e8aSDimitry Andric isec->getSize() > 0; 336185868e8aSDimitry Andric } 336285868e8aSDimitry Andric 33630b57cec5SDimitry Andric bool ARMExidxSyntheticSection::addSection(InputSection *isec) { 33640b57cec5SDimitry Andric if (isec->type == SHT_ARM_EXIDX) { 336585868e8aSDimitry Andric if (InputSection *dep = isec->getLinkOrderDep()) 33665ffd83dbSDimitry Andric if (isValidExidxSectionDep(dep)) { 33670b57cec5SDimitry Andric exidxSections.push_back(isec); 33685ffd83dbSDimitry Andric // Every exidxSection is 8 bytes, we need an estimate of 33695ffd83dbSDimitry Andric // size before assignAddresses can be called. Final size 33705ffd83dbSDimitry Andric // will only be known after finalize is called. 33715ffd83dbSDimitry Andric size += 8; 33725ffd83dbSDimitry Andric } 33730b57cec5SDimitry Andric return true; 33740b57cec5SDimitry Andric } 33750b57cec5SDimitry Andric 337685868e8aSDimitry Andric if (isValidExidxSectionDep(isec)) { 33770b57cec5SDimitry Andric executableSections.push_back(isec); 33780b57cec5SDimitry Andric return false; 33790b57cec5SDimitry Andric } 33800b57cec5SDimitry Andric 33810b57cec5SDimitry Andric // FIXME: we do not output a relocation section when --emit-relocs is used 33820b57cec5SDimitry Andric // as we do not have relocation sections for linker generated table entries 33830b57cec5SDimitry Andric // and we would have to erase at a late stage relocations from merged entries. 33840b57cec5SDimitry Andric // Given that exception tables are already position independent and a binary 33850b57cec5SDimitry Andric // analyzer could derive the relocations we choose to erase the relocations. 33860b57cec5SDimitry Andric if (config->emitRelocs && isec->type == SHT_REL) 33870b57cec5SDimitry Andric if (InputSectionBase *ex = isec->getRelocatedSection()) 33880b57cec5SDimitry Andric if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX) 33890b57cec5SDimitry Andric return true; 33900b57cec5SDimitry Andric 33910b57cec5SDimitry Andric return false; 33920b57cec5SDimitry Andric } 33930b57cec5SDimitry Andric 33940b57cec5SDimitry Andric // References to .ARM.Extab Sections have bit 31 clear and are not the 33950b57cec5SDimitry Andric // special EXIDX_CANTUNWIND bit-pattern. 33960b57cec5SDimitry Andric static bool isExtabRef(uint32_t unwind) { 33970b57cec5SDimitry Andric return (unwind & 0x80000000) == 0 && unwind != 0x1; 33980b57cec5SDimitry Andric } 33990b57cec5SDimitry Andric 34000b57cec5SDimitry Andric // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx 34010b57cec5SDimitry Andric // section Prev, where Cur follows Prev in the table. This can be done if the 34020b57cec5SDimitry Andric // unwinding instructions in Cur are identical to Prev. Linker generated 34030b57cec5SDimitry Andric // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an 34040b57cec5SDimitry Andric // InputSection. 34050b57cec5SDimitry Andric static bool isDuplicateArmExidxSec(InputSection *prev, InputSection *cur) { 34060b57cec5SDimitry Andric 34070b57cec5SDimitry Andric struct ExidxEntry { 34080b57cec5SDimitry Andric ulittle32_t fn; 34090b57cec5SDimitry Andric ulittle32_t unwind; 34100b57cec5SDimitry Andric }; 34110b57cec5SDimitry Andric // Get the last table Entry from the previous .ARM.exidx section. If Prev is 34120b57cec5SDimitry Andric // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry. 34130b57cec5SDimitry Andric ExidxEntry prevEntry = {ulittle32_t(0), ulittle32_t(1)}; 34140b57cec5SDimitry Andric if (prev) 34150b57cec5SDimitry Andric prevEntry = prev->getDataAs<ExidxEntry>().back(); 34160b57cec5SDimitry Andric if (isExtabRef(prevEntry.unwind)) 34170b57cec5SDimitry Andric return false; 34180b57cec5SDimitry Andric 34190b57cec5SDimitry Andric // We consider the unwind instructions of an .ARM.exidx table entry 34200b57cec5SDimitry Andric // a duplicate if the previous unwind instructions if: 34210b57cec5SDimitry Andric // - Both are the special EXIDX_CANTUNWIND. 34220b57cec5SDimitry Andric // - Both are the same inline unwind instructions. 34230b57cec5SDimitry Andric // We do not attempt to follow and check links into .ARM.extab tables as 34240b57cec5SDimitry Andric // consecutive identical entries are rare and the effort to check that they 34250b57cec5SDimitry Andric // are identical is high. 34260b57cec5SDimitry Andric 34270b57cec5SDimitry Andric // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry. 34280b57cec5SDimitry Andric if (cur == nullptr) 34290b57cec5SDimitry Andric return prevEntry.unwind == 1; 34300b57cec5SDimitry Andric 34310b57cec5SDimitry Andric for (const ExidxEntry entry : cur->getDataAs<ExidxEntry>()) 34320b57cec5SDimitry Andric if (isExtabRef(entry.unwind) || entry.unwind != prevEntry.unwind) 34330b57cec5SDimitry Andric return false; 34340b57cec5SDimitry Andric 34350b57cec5SDimitry Andric // All table entries in this .ARM.exidx Section can be merged into the 34360b57cec5SDimitry Andric // previous Section. 34370b57cec5SDimitry Andric return true; 34380b57cec5SDimitry Andric } 34390b57cec5SDimitry Andric 34400b57cec5SDimitry Andric // The .ARM.exidx table must be sorted in ascending order of the address of the 3441*bdd1243dSDimitry Andric // functions the table describes. std::optionally duplicate adjacent table 3442*bdd1243dSDimitry Andric // entries can be removed. At the end of the function the executableSections 3443*bdd1243dSDimitry Andric // must be sorted in ascending order of address, Sentinel is set to the 3444*bdd1243dSDimitry Andric // InputSection with the highest address and any InputSections that have 3445*bdd1243dSDimitry Andric // mergeable .ARM.exidx table entries are removed from it. 34460b57cec5SDimitry Andric void ARMExidxSyntheticSection::finalizeContents() { 344785868e8aSDimitry Andric // The executableSections and exidxSections that we use to derive the final 344885868e8aSDimitry Andric // contents of this SyntheticSection are populated before 344985868e8aSDimitry Andric // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or 345085868e8aSDimitry Andric // ICF may remove executable InputSections and their dependent .ARM.exidx 345185868e8aSDimitry Andric // section that we recorded earlier. 34520b57cec5SDimitry Andric auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); }; 34530b57cec5SDimitry Andric llvm::erase_if(exidxSections, isDiscarded); 34545ffd83dbSDimitry Andric // We need to remove discarded InputSections and InputSections without 34555ffd83dbSDimitry Andric // .ARM.exidx sections that if we generated the .ARM.exidx it would be out 34565ffd83dbSDimitry Andric // of range. 34575ffd83dbSDimitry Andric auto isDiscardedOrOutOfRange = [this](InputSection *isec) { 34585ffd83dbSDimitry Andric if (!isec->isLive()) 34595ffd83dbSDimitry Andric return true; 34605ffd83dbSDimitry Andric if (findExidxSection(isec)) 34615ffd83dbSDimitry Andric return false; 34625ffd83dbSDimitry Andric int64_t off = static_cast<int64_t>(isec->getVA() - getVA()); 34635ffd83dbSDimitry Andric return off != llvm::SignExtend64(off, 31); 34645ffd83dbSDimitry Andric }; 34655ffd83dbSDimitry Andric llvm::erase_if(executableSections, isDiscardedOrOutOfRange); 34660b57cec5SDimitry Andric 34670b57cec5SDimitry Andric // Sort the executable sections that may or may not have associated 34680b57cec5SDimitry Andric // .ARM.exidx sections by order of ascending address. This requires the 34695ffd83dbSDimitry Andric // relative positions of InputSections and OutputSections to be known. 34700b57cec5SDimitry Andric auto compareByFilePosition = [](const InputSection *a, 34710b57cec5SDimitry Andric const InputSection *b) { 34720b57cec5SDimitry Andric OutputSection *aOut = a->getParent(); 34730b57cec5SDimitry Andric OutputSection *bOut = b->getParent(); 34740b57cec5SDimitry Andric 34750b57cec5SDimitry Andric if (aOut != bOut) 34765ffd83dbSDimitry Andric return aOut->addr < bOut->addr; 34770b57cec5SDimitry Andric return a->outSecOff < b->outSecOff; 34780b57cec5SDimitry Andric }; 34790b57cec5SDimitry Andric llvm::stable_sort(executableSections, compareByFilePosition); 34800b57cec5SDimitry Andric sentinel = executableSections.back(); 3481*bdd1243dSDimitry Andric // std::optionally merge adjacent duplicate entries. 34820b57cec5SDimitry Andric if (config->mergeArmExidx) { 34831fd87a68SDimitry Andric SmallVector<InputSection *, 0> selectedSections; 34840b57cec5SDimitry Andric selectedSections.reserve(executableSections.size()); 34850b57cec5SDimitry Andric selectedSections.push_back(executableSections[0]); 34860b57cec5SDimitry Andric size_t prev = 0; 34870b57cec5SDimitry Andric for (size_t i = 1; i < executableSections.size(); ++i) { 34880b57cec5SDimitry Andric InputSection *ex1 = findExidxSection(executableSections[prev]); 34890b57cec5SDimitry Andric InputSection *ex2 = findExidxSection(executableSections[i]); 34900b57cec5SDimitry Andric if (!isDuplicateArmExidxSec(ex1, ex2)) { 34910b57cec5SDimitry Andric selectedSections.push_back(executableSections[i]); 34920b57cec5SDimitry Andric prev = i; 34930b57cec5SDimitry Andric } 34940b57cec5SDimitry Andric } 34950b57cec5SDimitry Andric executableSections = std::move(selectedSections); 34960b57cec5SDimitry Andric } 34970b57cec5SDimitry Andric 34980b57cec5SDimitry Andric size_t offset = 0; 34990b57cec5SDimitry Andric size = 0; 35000b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 35010b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 35020b57cec5SDimitry Andric d->outSecOff = offset; 35030b57cec5SDimitry Andric d->parent = getParent(); 35040b57cec5SDimitry Andric offset += d->getSize(); 35050b57cec5SDimitry Andric } else { 35060b57cec5SDimitry Andric offset += 8; 35070b57cec5SDimitry Andric } 35080b57cec5SDimitry Andric } 35090b57cec5SDimitry Andric // Size includes Sentinel. 35100b57cec5SDimitry Andric size = offset + 8; 35110b57cec5SDimitry Andric } 35120b57cec5SDimitry Andric 35130b57cec5SDimitry Andric InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const { 35140b57cec5SDimitry Andric return executableSections.front(); 35150b57cec5SDimitry Andric } 35160b57cec5SDimitry Andric 35170b57cec5SDimitry Andric // To write the .ARM.exidx table from the ExecutableSections we have three cases 35180b57cec5SDimitry Andric // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections. 35190b57cec5SDimitry Andric // We write the .ARM.exidx section contents and apply its relocations. 35200b57cec5SDimitry Andric // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We 35210b57cec5SDimitry Andric // must write the contents of an EXIDX_CANTUNWIND directly. We use the 35220b57cec5SDimitry Andric // start of the InputSection as the purpose of the linker generated 35230b57cec5SDimitry Andric // section is to terminate the address range of the previous entry. 35240b57cec5SDimitry Andric // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of 35250b57cec5SDimitry Andric // the table to terminate the address range of the final entry. 35260b57cec5SDimitry Andric void ARMExidxSyntheticSection::writeTo(uint8_t *buf) { 35270b57cec5SDimitry Andric 35280b57cec5SDimitry Andric const uint8_t cantUnwindData[8] = {0, 0, 0, 0, // PREL31 to target 35290b57cec5SDimitry Andric 1, 0, 0, 0}; // EXIDX_CANTUNWIND 35300b57cec5SDimitry Andric 35310b57cec5SDimitry Andric uint64_t offset = 0; 35320b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 35330b57cec5SDimitry Andric assert(isec->getParent() != nullptr); 35340b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 3535*bdd1243dSDimitry Andric memcpy(buf + offset, d->content().data(), d->content().size()); 3536*bdd1243dSDimitry Andric target->relocateAlloc(*d, buf + d->outSecOff); 35370b57cec5SDimitry Andric offset += d->getSize(); 35380b57cec5SDimitry Andric } else { 35390b57cec5SDimitry Andric // A Linker generated CANTUNWIND section. 35400b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 35410b57cec5SDimitry Andric uint64_t s = isec->getVA(); 35420b57cec5SDimitry Andric uint64_t p = getVA() + offset; 35435ffd83dbSDimitry Andric target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p); 35440b57cec5SDimitry Andric offset += 8; 35450b57cec5SDimitry Andric } 35460b57cec5SDimitry Andric } 35470b57cec5SDimitry Andric // Write Sentinel. 35480b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 35490b57cec5SDimitry Andric uint64_t s = sentinel->getVA(sentinel->getSize()); 35500b57cec5SDimitry Andric uint64_t p = getVA() + offset; 35515ffd83dbSDimitry Andric target->relocateNoSym(buf + offset, R_ARM_PREL31, s - p); 35520b57cec5SDimitry Andric assert(size == offset + 8); 35530b57cec5SDimitry Andric } 35540b57cec5SDimitry Andric 355585868e8aSDimitry Andric bool ARMExidxSyntheticSection::isNeeded() const { 3556349cc55cSDimitry Andric return llvm::any_of(exidxSections, 3557349cc55cSDimitry Andric [](InputSection *isec) { return isec->isLive(); }); 355885868e8aSDimitry Andric } 355985868e8aSDimitry Andric 35600b57cec5SDimitry Andric ThunkSection::ThunkSection(OutputSection *os, uint64_t off) 3561e8d8bef9SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 3562e8d8bef9SDimitry Andric config->emachine == EM_PPC64 ? 16 : 4, ".text.thunk") { 35630b57cec5SDimitry Andric this->parent = os; 35640b57cec5SDimitry Andric this->outSecOff = off; 35650b57cec5SDimitry Andric } 35660b57cec5SDimitry Andric 3567480093f4SDimitry Andric size_t ThunkSection::getSize() const { 356813138422SDimitry Andric if (roundUpSizeForErrata) 3569480093f4SDimitry Andric return alignTo(size, 4096); 3570480093f4SDimitry Andric return size; 3571480093f4SDimitry Andric } 3572480093f4SDimitry Andric 35730b57cec5SDimitry Andric void ThunkSection::addThunk(Thunk *t) { 35740b57cec5SDimitry Andric thunks.push_back(t); 35750b57cec5SDimitry Andric t->addSymbols(*this); 35760b57cec5SDimitry Andric } 35770b57cec5SDimitry Andric 35780b57cec5SDimitry Andric void ThunkSection::writeTo(uint8_t *buf) { 35790b57cec5SDimitry Andric for (Thunk *t : thunks) 35800b57cec5SDimitry Andric t->writeTo(buf + t->offset); 35810b57cec5SDimitry Andric } 35820b57cec5SDimitry Andric 35830b57cec5SDimitry Andric InputSection *ThunkSection::getTargetInputSection() const { 35840b57cec5SDimitry Andric if (thunks.empty()) 35850b57cec5SDimitry Andric return nullptr; 35860b57cec5SDimitry Andric const Thunk *t = thunks.front(); 35870b57cec5SDimitry Andric return t->getTargetInputSection(); 35880b57cec5SDimitry Andric } 35890b57cec5SDimitry Andric 35900b57cec5SDimitry Andric bool ThunkSection::assignOffsets() { 35910b57cec5SDimitry Andric uint64_t off = 0; 35920b57cec5SDimitry Andric for (Thunk *t : thunks) { 3593972a253aSDimitry Andric off = alignToPowerOf2(off, t->alignment); 35940b57cec5SDimitry Andric t->setOffset(off); 35950b57cec5SDimitry Andric uint32_t size = t->size(); 35960b57cec5SDimitry Andric t->getThunkTargetSym()->size = size; 35970b57cec5SDimitry Andric off += size; 35980b57cec5SDimitry Andric } 35990b57cec5SDimitry Andric bool changed = off != size; 36000b57cec5SDimitry Andric size = off; 36010b57cec5SDimitry Andric return changed; 36020b57cec5SDimitry Andric } 36030b57cec5SDimitry Andric 36040b57cec5SDimitry Andric PPC32Got2Section::PPC32Got2Section() 36050b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {} 36060b57cec5SDimitry Andric 36070b57cec5SDimitry Andric bool PPC32Got2Section::isNeeded() const { 36080b57cec5SDimitry Andric // See the comment below. This is not needed if there is no other 36090b57cec5SDimitry Andric // InputSection. 36104824e7fdSDimitry Andric for (SectionCommand *cmd : getParent()->commands) 36114824e7fdSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) 36120b57cec5SDimitry Andric for (InputSection *isec : isd->sections) 36130b57cec5SDimitry Andric if (isec != this) 36140b57cec5SDimitry Andric return true; 36150b57cec5SDimitry Andric return false; 36160b57cec5SDimitry Andric } 36170b57cec5SDimitry Andric 36180b57cec5SDimitry Andric void PPC32Got2Section::finalizeContents() { 36190b57cec5SDimitry Andric // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in 36200b57cec5SDimitry Andric // .got2 . This function computes outSecOff of each .got2 to be used in 36210b57cec5SDimitry Andric // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is 36220b57cec5SDimitry Andric // to collect input sections named ".got2". 36234824e7fdSDimitry Andric for (SectionCommand *cmd : getParent()->commands) 36244824e7fdSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) { 36250b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 36260eae32dcSDimitry Andric // isec->file may be nullptr for MergeSyntheticSection. 36270eae32dcSDimitry Andric if (isec != this && isec->file) 36280eae32dcSDimitry Andric isec->file->ppc32Got2 = isec; 36290b57cec5SDimitry Andric } 36300b57cec5SDimitry Andric } 36310b57cec5SDimitry Andric } 36320b57cec5SDimitry Andric 36330b57cec5SDimitry Andric // If linking position-dependent code then the table will store the addresses 36340b57cec5SDimitry Andric // directly in the binary so the section has type SHT_PROGBITS. If linking 36350b57cec5SDimitry Andric // position-independent code the section has type SHT_NOBITS since it will be 36360b57cec5SDimitry Andric // allocated and filled in by the dynamic linker. 36370b57cec5SDimitry Andric PPC64LongBranchTargetSection::PPC64LongBranchTargetSection() 36380b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 36390b57cec5SDimitry Andric config->isPic ? SHT_NOBITS : SHT_PROGBITS, 8, 36400b57cec5SDimitry Andric ".branch_lt") {} 36410b57cec5SDimitry Andric 3642480093f4SDimitry Andric uint64_t PPC64LongBranchTargetSection::getEntryVA(const Symbol *sym, 3643480093f4SDimitry Andric int64_t addend) { 3644480093f4SDimitry Andric return getVA() + entry_index.find({sym, addend})->second * 8; 3645480093f4SDimitry Andric } 3646480093f4SDimitry Andric 3647*bdd1243dSDimitry Andric std::optional<uint32_t> 3648*bdd1243dSDimitry Andric PPC64LongBranchTargetSection::addEntry(const Symbol *sym, int64_t addend) { 3649480093f4SDimitry Andric auto res = 3650480093f4SDimitry Andric entry_index.try_emplace(std::make_pair(sym, addend), entries.size()); 3651480093f4SDimitry Andric if (!res.second) 3652*bdd1243dSDimitry Andric return std::nullopt; 3653480093f4SDimitry Andric entries.emplace_back(sym, addend); 3654480093f4SDimitry Andric return res.first->second; 36550b57cec5SDimitry Andric } 36560b57cec5SDimitry Andric 36570b57cec5SDimitry Andric size_t PPC64LongBranchTargetSection::getSize() const { 36580b57cec5SDimitry Andric return entries.size() * 8; 36590b57cec5SDimitry Andric } 36600b57cec5SDimitry Andric 36610b57cec5SDimitry Andric void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) { 36620b57cec5SDimitry Andric // If linking non-pic we have the final addresses of the targets and they get 36630b57cec5SDimitry Andric // written to the table directly. For pic the dynamic linker will allocate 3664*bdd1243dSDimitry Andric // the section and fill it. 36650b57cec5SDimitry Andric if (config->isPic) 36660b57cec5SDimitry Andric return; 36670b57cec5SDimitry Andric 3668480093f4SDimitry Andric for (auto entry : entries) { 3669480093f4SDimitry Andric const Symbol *sym = entry.first; 3670480093f4SDimitry Andric int64_t addend = entry.second; 36710b57cec5SDimitry Andric assert(sym->getVA()); 36720b57cec5SDimitry Andric // Need calls to branch to the local entry-point since a long-branch 36730b57cec5SDimitry Andric // must be a local-call. 3674480093f4SDimitry Andric write64(buf, sym->getVA(addend) + 3675480093f4SDimitry Andric getPPC64GlobalEntryToLocalEntryOffset(sym->stOther)); 36760b57cec5SDimitry Andric buf += 8; 36770b57cec5SDimitry Andric } 36780b57cec5SDimitry Andric } 36790b57cec5SDimitry Andric 36800b57cec5SDimitry Andric bool PPC64LongBranchTargetSection::isNeeded() const { 36810b57cec5SDimitry Andric // `removeUnusedSyntheticSections()` is called before thunk allocation which 36820b57cec5SDimitry Andric // is too early to determine if this section will be empty or not. We need 36830b57cec5SDimitry Andric // Finalized to keep the section alive until after thunk creation. Finalized 36840b57cec5SDimitry Andric // only gets set to true once `finalizeSections()` is called after thunk 3685480093f4SDimitry Andric // creation. Because of this, if we don't create any long-branch thunks we end 36860b57cec5SDimitry Andric // up with an empty .branch_lt section in the binary. 36870b57cec5SDimitry Andric return !finalized || !entries.empty(); 36880b57cec5SDimitry Andric } 36890b57cec5SDimitry Andric 36900b57cec5SDimitry Andric static uint8_t getAbiVersion() { 36910b57cec5SDimitry Andric // MIPS non-PIC executable gets ABI version 1. 36920b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 36930b57cec5SDimitry Andric if (!config->isPic && !config->relocatable && 36940b57cec5SDimitry Andric (config->eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC) 36950b57cec5SDimitry Andric return 1; 36960b57cec5SDimitry Andric return 0; 36970b57cec5SDimitry Andric } 36980b57cec5SDimitry Andric 3699*bdd1243dSDimitry Andric if (config->emachine == EM_AMDGPU && !ctx.objectFiles.empty()) { 3700*bdd1243dSDimitry Andric uint8_t ver = ctx.objectFiles[0]->abiVersion; 3701*bdd1243dSDimitry Andric for (InputFile *file : ArrayRef(ctx.objectFiles).slice(1)) 37020b57cec5SDimitry Andric if (file->abiVersion != ver) 37030b57cec5SDimitry Andric error("incompatible ABI version: " + toString(file)); 37040b57cec5SDimitry Andric return ver; 37050b57cec5SDimitry Andric } 37060b57cec5SDimitry Andric 37070b57cec5SDimitry Andric return 0; 37080b57cec5SDimitry Andric } 37090b57cec5SDimitry Andric 37105ffd83dbSDimitry Andric template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) { 37110b57cec5SDimitry Andric memcpy(buf, "\177ELF", 4); 37120b57cec5SDimitry Andric 37130b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 37140b57cec5SDimitry Andric eHdr->e_ident[EI_CLASS] = config->is64 ? ELFCLASS64 : ELFCLASS32; 37150b57cec5SDimitry Andric eHdr->e_ident[EI_DATA] = config->isLE ? ELFDATA2LSB : ELFDATA2MSB; 37160b57cec5SDimitry Andric eHdr->e_ident[EI_VERSION] = EV_CURRENT; 37170b57cec5SDimitry Andric eHdr->e_ident[EI_OSABI] = config->osabi; 37180b57cec5SDimitry Andric eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(); 37190b57cec5SDimitry Andric eHdr->e_machine = config->emachine; 37200b57cec5SDimitry Andric eHdr->e_version = EV_CURRENT; 37210b57cec5SDimitry Andric eHdr->e_flags = config->eflags; 37220b57cec5SDimitry Andric eHdr->e_ehsize = sizeof(typename ELFT::Ehdr); 37230b57cec5SDimitry Andric eHdr->e_phnum = part.phdrs.size(); 37240b57cec5SDimitry Andric eHdr->e_shentsize = sizeof(typename ELFT::Shdr); 37250b57cec5SDimitry Andric 37260b57cec5SDimitry Andric if (!config->relocatable) { 37270b57cec5SDimitry Andric eHdr->e_phoff = sizeof(typename ELFT::Ehdr); 37280b57cec5SDimitry Andric eHdr->e_phentsize = sizeof(typename ELFT::Phdr); 37290b57cec5SDimitry Andric } 37300b57cec5SDimitry Andric } 37310b57cec5SDimitry Andric 37325ffd83dbSDimitry Andric template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) { 37330b57cec5SDimitry Andric // Write the program header table. 37340b57cec5SDimitry Andric auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf); 37350b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) { 37360b57cec5SDimitry Andric hBuf->p_type = p->p_type; 37370b57cec5SDimitry Andric hBuf->p_flags = p->p_flags; 37380b57cec5SDimitry Andric hBuf->p_offset = p->p_offset; 37390b57cec5SDimitry Andric hBuf->p_vaddr = p->p_vaddr; 37400b57cec5SDimitry Andric hBuf->p_paddr = p->p_paddr; 37410b57cec5SDimitry Andric hBuf->p_filesz = p->p_filesz; 37420b57cec5SDimitry Andric hBuf->p_memsz = p->p_memsz; 37430b57cec5SDimitry Andric hBuf->p_align = p->p_align; 37440b57cec5SDimitry Andric ++hBuf; 37450b57cec5SDimitry Andric } 37460b57cec5SDimitry Andric } 37470b57cec5SDimitry Andric 37480b57cec5SDimitry Andric template <typename ELFT> 37490b57cec5SDimitry Andric PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection() 37500b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {} 37510b57cec5SDimitry Andric 37520b57cec5SDimitry Andric template <typename ELFT> 37530b57cec5SDimitry Andric size_t PartitionElfHeaderSection<ELFT>::getSize() const { 37540b57cec5SDimitry Andric return sizeof(typename ELFT::Ehdr); 37550b57cec5SDimitry Andric } 37560b57cec5SDimitry Andric 37570b57cec5SDimitry Andric template <typename ELFT> 37580b57cec5SDimitry Andric void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) { 37590b57cec5SDimitry Andric writeEhdr<ELFT>(buf, getPartition()); 37600b57cec5SDimitry Andric 37610b57cec5SDimitry Andric // Loadable partitions are always ET_DYN. 37620b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 37630b57cec5SDimitry Andric eHdr->e_type = ET_DYN; 37640b57cec5SDimitry Andric } 37650b57cec5SDimitry Andric 37660b57cec5SDimitry Andric template <typename ELFT> 37670b57cec5SDimitry Andric PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection() 37680b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {} 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric template <typename ELFT> 37710b57cec5SDimitry Andric size_t PartitionProgramHeadersSection<ELFT>::getSize() const { 37720b57cec5SDimitry Andric return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size(); 37730b57cec5SDimitry Andric } 37740b57cec5SDimitry Andric 37750b57cec5SDimitry Andric template <typename ELFT> 37760b57cec5SDimitry Andric void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) { 37770b57cec5SDimitry Andric writePhdrs<ELFT>(buf, getPartition()); 37780b57cec5SDimitry Andric } 37790b57cec5SDimitry Andric 37800b57cec5SDimitry Andric PartitionIndexSection::PartitionIndexSection() 37810b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {} 37820b57cec5SDimitry Andric 37830b57cec5SDimitry Andric size_t PartitionIndexSection::getSize() const { 37840b57cec5SDimitry Andric return 12 * (partitions.size() - 1); 37850b57cec5SDimitry Andric } 37860b57cec5SDimitry Andric 37870b57cec5SDimitry Andric void PartitionIndexSection::finalizeContents() { 37880b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) 37890b57cec5SDimitry Andric partitions[i].nameStrTab = mainPart->dynStrTab->addString(partitions[i].name); 37900b57cec5SDimitry Andric } 37910b57cec5SDimitry Andric 37920b57cec5SDimitry Andric void PartitionIndexSection::writeTo(uint8_t *buf) { 37930b57cec5SDimitry Andric uint64_t va = getVA(); 37940b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) { 37950b57cec5SDimitry Andric write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va); 37960b57cec5SDimitry Andric write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4)); 37970b57cec5SDimitry Andric 379804eeddc0SDimitry Andric SyntheticSection *next = i == partitions.size() - 1 379904eeddc0SDimitry Andric ? in.partEnd.get() 380004eeddc0SDimitry Andric : partitions[i + 1].elfHeader.get(); 38010b57cec5SDimitry Andric write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA()); 38020b57cec5SDimitry Andric 38030b57cec5SDimitry Andric va += 12; 38040b57cec5SDimitry Andric buf += 12; 38050b57cec5SDimitry Andric } 38060b57cec5SDimitry Andric } 38070b57cec5SDimitry Andric 380804eeddc0SDimitry Andric void InStruct::reset() { 380904eeddc0SDimitry Andric attributes.reset(); 3810*bdd1243dSDimitry Andric riscvAttributes.reset(); 381104eeddc0SDimitry Andric bss.reset(); 381204eeddc0SDimitry Andric bssRelRo.reset(); 381304eeddc0SDimitry Andric got.reset(); 381404eeddc0SDimitry Andric gotPlt.reset(); 381504eeddc0SDimitry Andric igotPlt.reset(); 381604eeddc0SDimitry Andric ppc64LongBranchTarget.reset(); 38171fd87a68SDimitry Andric mipsAbiFlags.reset(); 381804eeddc0SDimitry Andric mipsGot.reset(); 38191fd87a68SDimitry Andric mipsOptions.reset(); 38201fd87a68SDimitry Andric mipsReginfo.reset(); 382104eeddc0SDimitry Andric mipsRldMap.reset(); 382204eeddc0SDimitry Andric partEnd.reset(); 382304eeddc0SDimitry Andric partIndex.reset(); 382404eeddc0SDimitry Andric plt.reset(); 382504eeddc0SDimitry Andric iplt.reset(); 382604eeddc0SDimitry Andric ppc32Got2.reset(); 382704eeddc0SDimitry Andric ibtPlt.reset(); 382804eeddc0SDimitry Andric relaPlt.reset(); 382904eeddc0SDimitry Andric relaIplt.reset(); 383004eeddc0SDimitry Andric shStrTab.reset(); 383104eeddc0SDimitry Andric strTab.reset(); 383204eeddc0SDimitry Andric symTab.reset(); 383304eeddc0SDimitry Andric symTabShndx.reset(); 383404eeddc0SDimitry Andric } 383504eeddc0SDimitry Andric 383681ad6265SDimitry Andric constexpr char kMemtagAndroidNoteName[] = "Android"; 383781ad6265SDimitry Andric void MemtagAndroidNote::writeTo(uint8_t *buf) { 3838fcaf7f86SDimitry Andric static_assert(sizeof(kMemtagAndroidNoteName) == 8, 3839fcaf7f86SDimitry Andric "ABI check for Android 11 & 12."); 384081ad6265SDimitry Andric assert((config->androidMemtagStack || config->androidMemtagHeap) && 384181ad6265SDimitry Andric "Should only be synthesizing a note if heap || stack is enabled."); 384281ad6265SDimitry Andric 384381ad6265SDimitry Andric write32(buf, sizeof(kMemtagAndroidNoteName)); 384481ad6265SDimitry Andric write32(buf + 4, sizeof(uint32_t)); 384581ad6265SDimitry Andric write32(buf + 8, ELF::NT_ANDROID_TYPE_MEMTAG); 384681ad6265SDimitry Andric memcpy(buf + 12, kMemtagAndroidNoteName, sizeof(kMemtagAndroidNoteName)); 384781ad6265SDimitry Andric buf += 12 + sizeof(kMemtagAndroidNoteName); 384881ad6265SDimitry Andric 384981ad6265SDimitry Andric uint32_t value = 0; 385081ad6265SDimitry Andric value |= config->androidMemtagMode; 385181ad6265SDimitry Andric if (config->androidMemtagHeap) 385281ad6265SDimitry Andric value |= ELF::NT_MEMTAG_HEAP; 385381ad6265SDimitry Andric // Note, MTE stack is an ABI break. Attempting to run an MTE stack-enabled 385481ad6265SDimitry Andric // binary on Android 11 or 12 will result in a checkfail in the loader. 385581ad6265SDimitry Andric if (config->androidMemtagStack) 385681ad6265SDimitry Andric value |= ELF::NT_MEMTAG_STACK; 385781ad6265SDimitry Andric write32(buf, value); // note value 385881ad6265SDimitry Andric } 385981ad6265SDimitry Andric 386081ad6265SDimitry Andric size_t MemtagAndroidNote::getSize() const { 386181ad6265SDimitry Andric return sizeof(llvm::ELF::Elf64_Nhdr) + 386281ad6265SDimitry Andric /*namesz=*/sizeof(kMemtagAndroidNoteName) + 386381ad6265SDimitry Andric /*descsz=*/sizeof(uint32_t); 386481ad6265SDimitry Andric } 386581ad6265SDimitry Andric 386661cfbce3SDimitry Andric void PackageMetadataNote::writeTo(uint8_t *buf) { 386761cfbce3SDimitry Andric write32(buf, 4); 386861cfbce3SDimitry Andric write32(buf + 4, config->packageMetadata.size() + 1); 386961cfbce3SDimitry Andric write32(buf + 8, FDO_PACKAGING_METADATA); 387061cfbce3SDimitry Andric memcpy(buf + 12, "FDO", 4); 387161cfbce3SDimitry Andric memcpy(buf + 16, config->packageMetadata.data(), 387261cfbce3SDimitry Andric config->packageMetadata.size()); 387361cfbce3SDimitry Andric } 387461cfbce3SDimitry Andric 387561cfbce3SDimitry Andric size_t PackageMetadataNote::getSize() const { 387661cfbce3SDimitry Andric return sizeof(llvm::ELF::Elf64_Nhdr) + 4 + 387761cfbce3SDimitry Andric alignTo(config->packageMetadata.size() + 1, 4); 387861cfbce3SDimitry Andric } 387961cfbce3SDimitry Andric 38805ffd83dbSDimitry Andric InStruct elf::in; 38810b57cec5SDimitry Andric 38825ffd83dbSDimitry Andric std::vector<Partition> elf::partitions; 38835ffd83dbSDimitry Andric Partition *elf::mainPart; 38840b57cec5SDimitry Andric 38850b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32LE>(); 38860b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32BE>(); 38870b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64LE>(); 38880b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64BE>(); 38890b57cec5SDimitry Andric 38905ffd83dbSDimitry Andric template void elf::splitSections<ELF32LE>(); 38915ffd83dbSDimitry Andric template void elf::splitSections<ELF32BE>(); 38925ffd83dbSDimitry Andric template void elf::splitSections<ELF64LE>(); 38935ffd83dbSDimitry Andric template void elf::splitSections<ELF64BE>(); 38940b57cec5SDimitry Andric 38955ffd83dbSDimitry Andric template class elf::MipsAbiFlagsSection<ELF32LE>; 38965ffd83dbSDimitry Andric template class elf::MipsAbiFlagsSection<ELF32BE>; 38975ffd83dbSDimitry Andric template class elf::MipsAbiFlagsSection<ELF64LE>; 38985ffd83dbSDimitry Andric template class elf::MipsAbiFlagsSection<ELF64BE>; 38990b57cec5SDimitry Andric 39005ffd83dbSDimitry Andric template class elf::MipsOptionsSection<ELF32LE>; 39015ffd83dbSDimitry Andric template class elf::MipsOptionsSection<ELF32BE>; 39025ffd83dbSDimitry Andric template class elf::MipsOptionsSection<ELF64LE>; 39035ffd83dbSDimitry Andric template class elf::MipsOptionsSection<ELF64BE>; 39040b57cec5SDimitry Andric 3905e8d8bef9SDimitry Andric template void EhFrameSection::iterateFDEWithLSDA<ELF32LE>( 3906e8d8bef9SDimitry Andric function_ref<void(InputSection &)>); 3907e8d8bef9SDimitry Andric template void EhFrameSection::iterateFDEWithLSDA<ELF32BE>( 3908e8d8bef9SDimitry Andric function_ref<void(InputSection &)>); 3909e8d8bef9SDimitry Andric template void EhFrameSection::iterateFDEWithLSDA<ELF64LE>( 3910e8d8bef9SDimitry Andric function_ref<void(InputSection &)>); 3911e8d8bef9SDimitry Andric template void EhFrameSection::iterateFDEWithLSDA<ELF64BE>( 3912e8d8bef9SDimitry Andric function_ref<void(InputSection &)>); 3913e8d8bef9SDimitry Andric 39145ffd83dbSDimitry Andric template class elf::MipsReginfoSection<ELF32LE>; 39155ffd83dbSDimitry Andric template class elf::MipsReginfoSection<ELF32BE>; 39165ffd83dbSDimitry Andric template class elf::MipsReginfoSection<ELF64LE>; 39175ffd83dbSDimitry Andric template class elf::MipsReginfoSection<ELF64BE>; 39180b57cec5SDimitry Andric 39195ffd83dbSDimitry Andric template class elf::DynamicSection<ELF32LE>; 39205ffd83dbSDimitry Andric template class elf::DynamicSection<ELF32BE>; 39215ffd83dbSDimitry Andric template class elf::DynamicSection<ELF64LE>; 39225ffd83dbSDimitry Andric template class elf::DynamicSection<ELF64BE>; 39230b57cec5SDimitry Andric 39245ffd83dbSDimitry Andric template class elf::RelocationSection<ELF32LE>; 39255ffd83dbSDimitry Andric template class elf::RelocationSection<ELF32BE>; 39265ffd83dbSDimitry Andric template class elf::RelocationSection<ELF64LE>; 39275ffd83dbSDimitry Andric template class elf::RelocationSection<ELF64BE>; 39280b57cec5SDimitry Andric 39295ffd83dbSDimitry Andric template class elf::AndroidPackedRelocationSection<ELF32LE>; 39305ffd83dbSDimitry Andric template class elf::AndroidPackedRelocationSection<ELF32BE>; 39315ffd83dbSDimitry Andric template class elf::AndroidPackedRelocationSection<ELF64LE>; 39325ffd83dbSDimitry Andric template class elf::AndroidPackedRelocationSection<ELF64BE>; 39330b57cec5SDimitry Andric 39345ffd83dbSDimitry Andric template class elf::RelrSection<ELF32LE>; 39355ffd83dbSDimitry Andric template class elf::RelrSection<ELF32BE>; 39365ffd83dbSDimitry Andric template class elf::RelrSection<ELF64LE>; 39375ffd83dbSDimitry Andric template class elf::RelrSection<ELF64BE>; 39380b57cec5SDimitry Andric 39395ffd83dbSDimitry Andric template class elf::SymbolTableSection<ELF32LE>; 39405ffd83dbSDimitry Andric template class elf::SymbolTableSection<ELF32BE>; 39415ffd83dbSDimitry Andric template class elf::SymbolTableSection<ELF64LE>; 39425ffd83dbSDimitry Andric template class elf::SymbolTableSection<ELF64BE>; 39430b57cec5SDimitry Andric 39445ffd83dbSDimitry Andric template class elf::VersionNeedSection<ELF32LE>; 39455ffd83dbSDimitry Andric template class elf::VersionNeedSection<ELF32BE>; 39465ffd83dbSDimitry Andric template class elf::VersionNeedSection<ELF64LE>; 39475ffd83dbSDimitry Andric template class elf::VersionNeedSection<ELF64BE>; 39480b57cec5SDimitry Andric 39495ffd83dbSDimitry Andric template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part); 39505ffd83dbSDimitry Andric template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part); 39515ffd83dbSDimitry Andric template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part); 39525ffd83dbSDimitry Andric template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part); 39530b57cec5SDimitry Andric 39545ffd83dbSDimitry Andric template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part); 39555ffd83dbSDimitry Andric template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part); 39565ffd83dbSDimitry Andric template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part); 39575ffd83dbSDimitry Andric template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part); 39580b57cec5SDimitry Andric 39595ffd83dbSDimitry Andric template class elf::PartitionElfHeaderSection<ELF32LE>; 39605ffd83dbSDimitry Andric template class elf::PartitionElfHeaderSection<ELF32BE>; 39615ffd83dbSDimitry Andric template class elf::PartitionElfHeaderSection<ELF64LE>; 39625ffd83dbSDimitry Andric template class elf::PartitionElfHeaderSection<ELF64BE>; 39630b57cec5SDimitry Andric 39645ffd83dbSDimitry Andric template class elf::PartitionProgramHeadersSection<ELF32LE>; 39655ffd83dbSDimitry Andric template class elf::PartitionProgramHeadersSection<ELF32BE>; 39665ffd83dbSDimitry Andric template class elf::PartitionProgramHeadersSection<ELF64LE>; 39675ffd83dbSDimitry Andric template class elf::PartitionProgramHeadersSection<ELF64BE>; 3968