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" 180b57cec5SDimitry Andric #include "InputFiles.h" 190b57cec5SDimitry Andric #include "LinkerScript.h" 200b57cec5SDimitry Andric #include "OutputSections.h" 210b57cec5SDimitry Andric #include "SymbolTable.h" 220b57cec5SDimitry Andric #include "Symbols.h" 230b57cec5SDimitry Andric #include "Target.h" 240b57cec5SDimitry Andric #include "Writer.h" 250b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 260b57cec5SDimitry Andric #include "lld/Common/Memory.h" 270b57cec5SDimitry Andric #include "lld/Common/Strings.h" 280b57cec5SDimitry Andric #include "lld/Common/Threads.h" 290b57cec5SDimitry Andric #include "lld/Common/Version.h" 300b57cec5SDimitry Andric #include "llvm/ADT/SetOperations.h" 310b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 320b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 330b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 340b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 350b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 360b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 370b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 380b57cec5SDimitry Andric #include "llvm/Support/MD5.h" 390b57cec5SDimitry Andric #include <cstdlib> 400b57cec5SDimitry Andric #include <thread> 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric using namespace llvm; 430b57cec5SDimitry Andric using namespace llvm::dwarf; 440b57cec5SDimitry Andric using namespace llvm::ELF; 450b57cec5SDimitry Andric using namespace llvm::object; 460b57cec5SDimitry Andric using namespace llvm::support; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric using llvm::support::endian::read32le; 490b57cec5SDimitry Andric using llvm::support::endian::write32le; 500b57cec5SDimitry Andric using llvm::support::endian::write64le; 510b57cec5SDimitry Andric 52*85868e8aSDimitry Andric namespace lld { 53*85868e8aSDimitry Andric namespace elf { 540b57cec5SDimitry Andric constexpr size_t MergeNoTailSection::numShards; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric static uint64_t readUint(uint8_t *buf) { 570b57cec5SDimitry Andric return config->is64 ? read64(buf) : read32(buf); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric static void writeUint(uint8_t *buf, uint64_t val) { 610b57cec5SDimitry Andric if (config->is64) 620b57cec5SDimitry Andric write64(buf, val); 630b57cec5SDimitry Andric else 640b57cec5SDimitry Andric write32(buf, val); 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric // Returns an LLD version string. 680b57cec5SDimitry Andric static ArrayRef<uint8_t> getVersion() { 690b57cec5SDimitry Andric // Check LLD_VERSION first for ease of testing. 700b57cec5SDimitry Andric // You can get consistent output by using the environment variable. 710b57cec5SDimitry Andric // This is only for testing. 720b57cec5SDimitry Andric StringRef s = getenv("LLD_VERSION"); 730b57cec5SDimitry Andric if (s.empty()) 740b57cec5SDimitry Andric s = saver.save(Twine("Linker: ") + getLLDVersion()); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // +1 to include the terminating '\0'. 770b57cec5SDimitry Andric return {(const uint8_t *)s.data(), s.size() + 1}; 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Creates a .comment section containing LLD version info. 810b57cec5SDimitry Andric // With this feature, you can identify LLD-generated binaries easily 820b57cec5SDimitry Andric // by "readelf --string-dump .comment <file>". 830b57cec5SDimitry Andric // The returned object is a mergeable string section. 84*85868e8aSDimitry Andric MergeInputSection *createCommentSection() { 850b57cec5SDimitry Andric return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1, 860b57cec5SDimitry Andric getVersion(), ".comment"); 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // .MIPS.abiflags section. 900b57cec5SDimitry Andric template <class ELFT> 910b57cec5SDimitry Andric MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags) 920b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), 930b57cec5SDimitry Andric flags(flags) { 940b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_ABIFlags); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) { 980b57cec5SDimitry Andric memcpy(buf, &flags, sizeof(flags)); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric template <class ELFT> 1020b57cec5SDimitry Andric MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() { 1030b57cec5SDimitry Andric Elf_Mips_ABIFlags flags = {}; 1040b57cec5SDimitry Andric bool create = false; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) { 1070b57cec5SDimitry Andric if (sec->type != SHT_MIPS_ABIFLAGS) 1080b57cec5SDimitry Andric continue; 1090b57cec5SDimitry Andric sec->markDead(); 1100b57cec5SDimitry Andric create = true; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric std::string filename = toString(sec->file); 1130b57cec5SDimitry Andric const size_t size = sec->data().size(); 1140b57cec5SDimitry Andric // Older version of BFD (such as the default FreeBSD linker) concatenate 1150b57cec5SDimitry Andric // .MIPS.abiflags instead of merging. To allow for this case (or potential 1160b57cec5SDimitry Andric // zero padding) we ignore everything after the first Elf_Mips_ABIFlags 1170b57cec5SDimitry Andric if (size < sizeof(Elf_Mips_ABIFlags)) { 1180b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.abiflags section: got " + 1190b57cec5SDimitry Andric Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); 1200b57cec5SDimitry Andric return nullptr; 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data()); 1230b57cec5SDimitry Andric if (s->version != 0) { 1240b57cec5SDimitry Andric error(filename + ": unexpected .MIPS.abiflags version " + 1250b57cec5SDimitry Andric Twine(s->version)); 1260b57cec5SDimitry Andric return nullptr; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // LLD checks ISA compatibility in calcMipsEFlags(). Here we just 1300b57cec5SDimitry Andric // select the highest number of ISA/Rev/Ext. 1310b57cec5SDimitry Andric flags.isa_level = std::max(flags.isa_level, s->isa_level); 1320b57cec5SDimitry Andric flags.isa_rev = std::max(flags.isa_rev, s->isa_rev); 1330b57cec5SDimitry Andric flags.isa_ext = std::max(flags.isa_ext, s->isa_ext); 1340b57cec5SDimitry Andric flags.gpr_size = std::max(flags.gpr_size, s->gpr_size); 1350b57cec5SDimitry Andric flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size); 1360b57cec5SDimitry Andric flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size); 1370b57cec5SDimitry Andric flags.ases |= s->ases; 1380b57cec5SDimitry Andric flags.flags1 |= s->flags1; 1390b57cec5SDimitry Andric flags.flags2 |= s->flags2; 140*85868e8aSDimitry Andric flags.fp_abi = getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename); 1410b57cec5SDimitry Andric }; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric if (create) 1440b57cec5SDimitry Andric return make<MipsAbiFlagsSection<ELFT>>(flags); 1450b57cec5SDimitry Andric return nullptr; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric // .MIPS.options section. 1490b57cec5SDimitry Andric template <class ELFT> 1500b57cec5SDimitry Andric MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo) 1510b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), 1520b57cec5SDimitry Andric reginfo(reginfo) { 1530b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) { 1570b57cec5SDimitry Andric auto *options = reinterpret_cast<Elf_Mips_Options *>(buf); 1580b57cec5SDimitry Andric options->kind = ODK_REGINFO; 1590b57cec5SDimitry Andric options->size = getSize(); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric if (!config->relocatable) 1620b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 1630b57cec5SDimitry Andric memcpy(buf + sizeof(Elf_Mips_Options), ®info, sizeof(reginfo)); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric template <class ELFT> 1670b57cec5SDimitry Andric MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { 1680b57cec5SDimitry Andric // N64 ABI only. 1690b57cec5SDimitry Andric if (!ELFT::Is64Bits) 1700b57cec5SDimitry Andric return nullptr; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric std::vector<InputSectionBase *> sections; 1730b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) 1740b57cec5SDimitry Andric if (sec->type == SHT_MIPS_OPTIONS) 1750b57cec5SDimitry Andric sections.push_back(sec); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric if (sections.empty()) 1780b57cec5SDimitry Andric return nullptr; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 1810b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 1820b57cec5SDimitry Andric sec->markDead(); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric std::string filename = toString(sec->file); 1850b57cec5SDimitry Andric ArrayRef<uint8_t> d = sec->data(); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric while (!d.empty()) { 1880b57cec5SDimitry Andric if (d.size() < sizeof(Elf_Mips_Options)) { 1890b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.options section"); 1900b57cec5SDimitry Andric break; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data()); 1940b57cec5SDimitry Andric if (opt->kind == ODK_REGINFO) { 1950b57cec5SDimitry Andric reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask; 1960b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value; 1970b57cec5SDimitry Andric break; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric if (!opt->size) 2010b57cec5SDimitry Andric fatal(filename + ": zero option descriptor size"); 2020b57cec5SDimitry Andric d = d.slice(opt->size); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric }; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric return make<MipsOptionsSection<ELFT>>(reginfo); 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // MIPS .reginfo section. 2100b57cec5SDimitry Andric template <class ELFT> 2110b57cec5SDimitry Andric MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo) 2120b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), 2130b57cec5SDimitry Andric reginfo(reginfo) { 2140b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_RegInfo); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) { 2180b57cec5SDimitry Andric if (!config->relocatable) 2190b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 2200b57cec5SDimitry Andric memcpy(buf, ®info, sizeof(reginfo)); 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric template <class ELFT> 2240b57cec5SDimitry Andric MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { 2250b57cec5SDimitry Andric // Section should be alive for O32 and N32 ABIs only. 2260b57cec5SDimitry Andric if (ELFT::Is64Bits) 2270b57cec5SDimitry Andric return nullptr; 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric std::vector<InputSectionBase *> sections; 2300b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) 2310b57cec5SDimitry Andric if (sec->type == SHT_MIPS_REGINFO) 2320b57cec5SDimitry Andric sections.push_back(sec); 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric if (sections.empty()) 2350b57cec5SDimitry Andric return nullptr; 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 2380b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 2390b57cec5SDimitry Andric sec->markDead(); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) { 2420b57cec5SDimitry Andric error(toString(sec->file) + ": invalid size of .reginfo section"); 2430b57cec5SDimitry Andric return nullptr; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data()); 2470b57cec5SDimitry Andric reginfo.ri_gprmask |= r->ri_gprmask; 2480b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value; 2490b57cec5SDimitry Andric }; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric return make<MipsReginfoSection<ELFT>>(reginfo); 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric 254*85868e8aSDimitry Andric InputSection *createInterpSection() { 2550b57cec5SDimitry Andric // StringSaver guarantees that the returned string ends with '\0'. 2560b57cec5SDimitry Andric StringRef s = saver.save(config->dynamicLinker); 2570b57cec5SDimitry Andric ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1}; 2580b57cec5SDimitry Andric 259*85868e8aSDimitry Andric return make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents, 2600b57cec5SDimitry Andric ".interp"); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 263*85868e8aSDimitry Andric Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value, 2640b57cec5SDimitry Andric uint64_t size, InputSectionBase §ion) { 2650b57cec5SDimitry Andric auto *s = make<Defined>(section.file, name, STB_LOCAL, STV_DEFAULT, type, 2660b57cec5SDimitry Andric value, size, §ion); 2670b57cec5SDimitry Andric if (in.symTab) 2680b57cec5SDimitry Andric in.symTab->addSymbol(s); 2690b57cec5SDimitry Andric return s; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric static size_t getHashSize() { 2730b57cec5SDimitry Andric switch (config->buildId) { 2740b57cec5SDimitry Andric case BuildIdKind::Fast: 2750b57cec5SDimitry Andric return 8; 2760b57cec5SDimitry Andric case BuildIdKind::Md5: 2770b57cec5SDimitry Andric case BuildIdKind::Uuid: 2780b57cec5SDimitry Andric return 16; 2790b57cec5SDimitry Andric case BuildIdKind::Sha1: 2800b57cec5SDimitry Andric return 20; 2810b57cec5SDimitry Andric case BuildIdKind::Hexstring: 2820b57cec5SDimitry Andric return config->buildIdVector.size(); 2830b57cec5SDimitry Andric default: 2840b57cec5SDimitry Andric llvm_unreachable("unknown BuildIdKind"); 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // This class represents a linker-synthesized .note.gnu.property section. 2890b57cec5SDimitry Andric // 2900b57cec5SDimitry Andric // In x86 and AArch64, object files may contain feature flags indicating the 2910b57cec5SDimitry Andric // features that they have used. The flags are stored in a .note.gnu.property 2920b57cec5SDimitry Andric // section. 2930b57cec5SDimitry Andric // 2940b57cec5SDimitry Andric // lld reads the sections from input files and merges them by computing AND of 2950b57cec5SDimitry Andric // the flags. The result is written as a new .note.gnu.property section. 2960b57cec5SDimitry Andric // 2970b57cec5SDimitry Andric // If the flag is zero (which indicates that the intersection of the feature 2980b57cec5SDimitry Andric // sets is empty, or some input files didn't have .note.gnu.property sections), 2990b57cec5SDimitry Andric // we don't create this section. 3000b57cec5SDimitry Andric GnuPropertySection::GnuPropertySection() 3010b57cec5SDimitry Andric : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, 4, 3020b57cec5SDimitry Andric ".note.gnu.property") {} 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric void GnuPropertySection::writeTo(uint8_t *buf) { 3050b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 3060b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 3070b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric write32(buf, 4); // Name size 3100b57cec5SDimitry Andric write32(buf + 4, config->is64 ? 16 : 12); // Content size 3110b57cec5SDimitry Andric write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type 3120b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 3130b57cec5SDimitry Andric write32(buf + 16, featureAndType); // Feature type 3140b57cec5SDimitry Andric write32(buf + 20, 4); // Feature size 3150b57cec5SDimitry Andric write32(buf + 24, config->andFeatures); // Feature flags 3160b57cec5SDimitry Andric if (config->is64) 3170b57cec5SDimitry Andric write32(buf + 28, 0); // Padding 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric size_t GnuPropertySection::getSize() const { return config->is64 ? 32 : 28; } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric BuildIdSection::BuildIdSection() 3230b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"), 3240b57cec5SDimitry Andric hashSize(getHashSize()) {} 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric void BuildIdSection::writeTo(uint8_t *buf) { 3270b57cec5SDimitry Andric write32(buf, 4); // Name size 3280b57cec5SDimitry Andric write32(buf + 4, hashSize); // Content size 3290b57cec5SDimitry Andric write32(buf + 8, NT_GNU_BUILD_ID); // Type 3300b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 3310b57cec5SDimitry Andric hashBuf = buf + 16; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) { 3350b57cec5SDimitry Andric assert(buf.size() == hashSize); 3360b57cec5SDimitry Andric memcpy(hashBuf, buf.data(), hashSize); 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment) 3400b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) { 3410b57cec5SDimitry Andric this->bss = true; 3420b57cec5SDimitry Andric this->size = size; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric EhFrameSection::EhFrameSection() 3460b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Search for an existing CIE record or create a new one. 3490b57cec5SDimitry Andric // CIE records from input object files are uniquified by their contents 3500b57cec5SDimitry Andric // and where their relocations point to. 3510b57cec5SDimitry Andric template <class ELFT, class RelTy> 3520b57cec5SDimitry Andric CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) { 3530b57cec5SDimitry Andric Symbol *personality = nullptr; 3540b57cec5SDimitry Andric unsigned firstRelI = cie.firstRelocation; 3550b57cec5SDimitry Andric if (firstRelI != (unsigned)-1) 3560b57cec5SDimitry Andric personality = 3570b57cec5SDimitry Andric &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric // Search for an existing CIE by CIE contents/relocation target pair. 3600b57cec5SDimitry Andric CieRecord *&rec = cieMap[{cie.data(), personality}]; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // If not found, create a new one. 3630b57cec5SDimitry Andric if (!rec) { 3640b57cec5SDimitry Andric rec = make<CieRecord>(); 3650b57cec5SDimitry Andric rec->cie = &cie; 3660b57cec5SDimitry Andric cieRecords.push_back(rec); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric return rec; 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric // There is one FDE per function. Returns true if a given FDE 3720b57cec5SDimitry Andric // points to a live function. 3730b57cec5SDimitry Andric template <class ELFT, class RelTy> 3740b57cec5SDimitry Andric bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) { 3750b57cec5SDimitry Andric auto *sec = cast<EhInputSection>(fde.sec); 3760b57cec5SDimitry Andric unsigned firstRelI = fde.firstRelocation; 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric // An FDE should point to some function because FDEs are to describe 3790b57cec5SDimitry Andric // functions. That's however not always the case due to an issue of 3800b57cec5SDimitry Andric // ld.gold with -r. ld.gold may discard only functions and leave their 3810b57cec5SDimitry Andric // corresponding FDEs, which results in creating bad .eh_frame sections. 3820b57cec5SDimitry Andric // To deal with that, we ignore such FDEs. 3830b57cec5SDimitry Andric if (firstRelI == (unsigned)-1) 3840b57cec5SDimitry Andric return false; 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric const RelTy &rel = rels[firstRelI]; 3870b57cec5SDimitry Andric Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel); 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // FDEs for garbage-collected or merged-by-ICF sections, or sections in 3900b57cec5SDimitry Andric // another partition, are dead. 3910b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&b)) 3920b57cec5SDimitry Andric if (SectionBase *sec = d->section) 3930b57cec5SDimitry Andric return sec->partition == partition; 3940b57cec5SDimitry Andric return false; 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric // .eh_frame is a sequence of CIE or FDE records. In general, there 3980b57cec5SDimitry Andric // is one CIE record per input object file which is followed by 3990b57cec5SDimitry Andric // a list of FDEs. This function searches an existing CIE or create a new 4000b57cec5SDimitry Andric // one and associates FDEs to the CIE. 4010b57cec5SDimitry Andric template <class ELFT, class RelTy> 402*85868e8aSDimitry Andric void EhFrameSection::addRecords(EhInputSection *sec, ArrayRef<RelTy> rels) { 4030b57cec5SDimitry Andric offsetToCie.clear(); 4040b57cec5SDimitry Andric for (EhSectionPiece &piece : sec->pieces) { 4050b57cec5SDimitry Andric // The empty record is the end marker. 4060b57cec5SDimitry Andric if (piece.size == 4) 4070b57cec5SDimitry Andric return; 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric size_t offset = piece.inputOff; 4100b57cec5SDimitry Andric uint32_t id = read32(piece.data().data() + 4); 4110b57cec5SDimitry Andric if (id == 0) { 4120b57cec5SDimitry Andric offsetToCie[offset] = addCie<ELFT>(piece, rels); 4130b57cec5SDimitry Andric continue; 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric uint32_t cieOffset = offset + 4 - id; 4170b57cec5SDimitry Andric CieRecord *rec = offsetToCie[cieOffset]; 4180b57cec5SDimitry Andric if (!rec) 4190b57cec5SDimitry Andric fatal(toString(sec) + ": invalid CIE reference"); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric if (!isFdeLive<ELFT>(piece, rels)) 4220b57cec5SDimitry Andric continue; 4230b57cec5SDimitry Andric rec->fdes.push_back(&piece); 4240b57cec5SDimitry Andric numFdes++; 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 428*85868e8aSDimitry Andric template <class ELFT> 429*85868e8aSDimitry Andric void EhFrameSection::addSectionAux(EhInputSection *sec) { 430*85868e8aSDimitry Andric if (!sec->isLive()) 431*85868e8aSDimitry Andric return; 432*85868e8aSDimitry Andric if (sec->areRelocsRela) 433*85868e8aSDimitry Andric addRecords<ELFT>(sec, sec->template relas<ELFT>()); 434*85868e8aSDimitry Andric else 435*85868e8aSDimitry Andric addRecords<ELFT>(sec, sec->template rels<ELFT>()); 436*85868e8aSDimitry Andric } 437*85868e8aSDimitry Andric 438*85868e8aSDimitry Andric void EhFrameSection::addSection(EhInputSection *sec) { 4390b57cec5SDimitry Andric sec->parent = this; 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric alignment = std::max(alignment, sec->alignment); 4420b57cec5SDimitry Andric sections.push_back(sec); 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric for (auto *ds : sec->dependentSections) 4450b57cec5SDimitry Andric dependentSections.push_back(ds); 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) { 4490b57cec5SDimitry Andric memcpy(buf, d.data(), d.size()); 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric size_t aligned = alignTo(d.size(), config->wordsize); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // Zero-clear trailing padding if it exists. 4540b57cec5SDimitry Andric memset(buf + d.size(), 0, aligned - d.size()); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // Fix the size field. -4 since size does not include the size field itself. 4570b57cec5SDimitry Andric write32(buf, aligned - 4); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric void EhFrameSection::finalizeContents() { 4610b57cec5SDimitry Andric assert(!this->size); // Not finalized. 462*85868e8aSDimitry Andric 463*85868e8aSDimitry Andric switch (config->ekind) { 464*85868e8aSDimitry Andric case ELFNoneKind: 465*85868e8aSDimitry Andric llvm_unreachable("invalid ekind"); 466*85868e8aSDimitry Andric case ELF32LEKind: 467*85868e8aSDimitry Andric for (EhInputSection *sec : sections) 468*85868e8aSDimitry Andric addSectionAux<ELF32LE>(sec); 469*85868e8aSDimitry Andric break; 470*85868e8aSDimitry Andric case ELF32BEKind: 471*85868e8aSDimitry Andric for (EhInputSection *sec : sections) 472*85868e8aSDimitry Andric addSectionAux<ELF32BE>(sec); 473*85868e8aSDimitry Andric break; 474*85868e8aSDimitry Andric case ELF64LEKind: 475*85868e8aSDimitry Andric for (EhInputSection *sec : sections) 476*85868e8aSDimitry Andric addSectionAux<ELF64LE>(sec); 477*85868e8aSDimitry Andric break; 478*85868e8aSDimitry Andric case ELF64BEKind: 479*85868e8aSDimitry Andric for (EhInputSection *sec : sections) 480*85868e8aSDimitry Andric addSectionAux<ELF64BE>(sec); 481*85868e8aSDimitry Andric break; 482*85868e8aSDimitry Andric } 483*85868e8aSDimitry Andric 4840b57cec5SDimitry Andric size_t off = 0; 4850b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 4860b57cec5SDimitry Andric rec->cie->outputOff = off; 4870b57cec5SDimitry Andric off += alignTo(rec->cie->size, config->wordsize); 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 4900b57cec5SDimitry Andric fde->outputOff = off; 4910b57cec5SDimitry Andric off += alignTo(fde->size, config->wordsize); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric // The LSB standard does not allow a .eh_frame section with zero 4960b57cec5SDimitry Andric // Call Frame Information records. glibc unwind-dw2-fde.c 4970b57cec5SDimitry Andric // classify_object_over_fdes expects there is a CIE record length 0 as a 4980b57cec5SDimitry Andric // terminator. Thus we add one unconditionally. 4990b57cec5SDimitry Andric off += 4; 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric this->size = off; 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table 5050b57cec5SDimitry Andric // to get an FDE from an address to which FDE is applied. This function 5060b57cec5SDimitry Andric // returns a list of such pairs. 5070b57cec5SDimitry Andric std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const { 5080b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 5090b57cec5SDimitry Andric std::vector<FdeData> ret; 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric uint64_t va = getPartition().ehFrameHdr->getVA(); 5120b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 5130b57cec5SDimitry Andric uint8_t enc = getFdeEncoding(rec->cie); 5140b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 5150b57cec5SDimitry Andric uint64_t pc = getFdePc(buf, fde->outputOff, enc); 5160b57cec5SDimitry Andric uint64_t fdeVA = getParent()->addr + fde->outputOff; 5170b57cec5SDimitry Andric if (!isInt<32>(pc - va)) 5180b57cec5SDimitry Andric fatal(toString(fde->sec) + ": PC offset is too large: 0x" + 5190b57cec5SDimitry Andric Twine::utohexstr(pc - va)); 5200b57cec5SDimitry Andric ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)}); 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric // Sort the FDE list by their PC and uniqueify. Usually there is only 5250b57cec5SDimitry Andric // one FDE for a PC (i.e. function), but if ICF merges two functions 5260b57cec5SDimitry Andric // into one, there can be more than one FDEs pointing to the address. 5270b57cec5SDimitry Andric auto less = [](const FdeData &a, const FdeData &b) { 5280b57cec5SDimitry Andric return a.pcRel < b.pcRel; 5290b57cec5SDimitry Andric }; 5300b57cec5SDimitry Andric llvm::stable_sort(ret, less); 5310b57cec5SDimitry Andric auto eq = [](const FdeData &a, const FdeData &b) { 5320b57cec5SDimitry Andric return a.pcRel == b.pcRel; 5330b57cec5SDimitry Andric }; 5340b57cec5SDimitry Andric ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end()); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric return ret; 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric static uint64_t readFdeAddr(uint8_t *buf, int size) { 5400b57cec5SDimitry Andric switch (size) { 5410b57cec5SDimitry Andric case DW_EH_PE_udata2: 5420b57cec5SDimitry Andric return read16(buf); 5430b57cec5SDimitry Andric case DW_EH_PE_sdata2: 5440b57cec5SDimitry Andric return (int16_t)read16(buf); 5450b57cec5SDimitry Andric case DW_EH_PE_udata4: 5460b57cec5SDimitry Andric return read32(buf); 5470b57cec5SDimitry Andric case DW_EH_PE_sdata4: 5480b57cec5SDimitry Andric return (int32_t)read32(buf); 5490b57cec5SDimitry Andric case DW_EH_PE_udata8: 5500b57cec5SDimitry Andric case DW_EH_PE_sdata8: 5510b57cec5SDimitry Andric return read64(buf); 5520b57cec5SDimitry Andric case DW_EH_PE_absptr: 5530b57cec5SDimitry Andric return readUint(buf); 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric fatal("unknown FDE size encoding"); 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. 5590b57cec5SDimitry Andric // We need it to create .eh_frame_hdr section. 5600b57cec5SDimitry Andric uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff, 5610b57cec5SDimitry Andric uint8_t enc) const { 5620b57cec5SDimitry Andric // The starting address to which this FDE applies is 5630b57cec5SDimitry Andric // stored at FDE + 8 byte. 5640b57cec5SDimitry Andric size_t off = fdeOff + 8; 5650b57cec5SDimitry Andric uint64_t addr = readFdeAddr(buf + off, enc & 0xf); 5660b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_absptr) 5670b57cec5SDimitry Andric return addr; 5680b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_pcrel) 5690b57cec5SDimitry Andric return addr + getParent()->addr + off; 5700b57cec5SDimitry Andric fatal("unknown FDE size relative encoding"); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric void EhFrameSection::writeTo(uint8_t *buf) { 5740b57cec5SDimitry Andric // Write CIE and FDE records. 5750b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 5760b57cec5SDimitry Andric size_t cieOffset = rec->cie->outputOff; 5770b57cec5SDimitry Andric writeCieFde(buf + cieOffset, rec->cie->data()); 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 5800b57cec5SDimitry Andric size_t off = fde->outputOff; 5810b57cec5SDimitry Andric writeCieFde(buf + off, fde->data()); 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric // FDE's second word should have the offset to an associated CIE. 5840b57cec5SDimitry Andric // Write it. 5850b57cec5SDimitry Andric write32(buf + off + 4, off + 4 - cieOffset); 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric // Apply relocations. .eh_frame section contents are not contiguous 5900b57cec5SDimitry Andric // in the output buffer, but relocateAlloc() still works because 5910b57cec5SDimitry Andric // getOffset() takes care of discontiguous section pieces. 5920b57cec5SDimitry Andric for (EhInputSection *s : sections) 5930b57cec5SDimitry Andric s->relocateAlloc(buf, nullptr); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent()) 5960b57cec5SDimitry Andric getPartition().ehFrameHdr->write(); 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric GotSection::GotSection() 6000b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 6010b57cec5SDimitry Andric ".got") { 6020b57cec5SDimitry Andric // If ElfSym::globalOffsetTable is relative to .got and is referenced, 6030b57cec5SDimitry Andric // increase numEntries by the number of entries used to emit 6040b57cec5SDimitry Andric // ElfSym::globalOffsetTable. 6050b57cec5SDimitry Andric if (ElfSym::globalOffsetTable && !target->gotBaseSymInGotPlt) 6060b57cec5SDimitry Andric numEntries += target->gotHeaderEntriesNum; 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric void GotSection::addEntry(Symbol &sym) { 6100b57cec5SDimitry Andric sym.gotIndex = numEntries; 6110b57cec5SDimitry Andric ++numEntries; 6120b57cec5SDimitry Andric } 6130b57cec5SDimitry Andric 6140b57cec5SDimitry Andric bool GotSection::addDynTlsEntry(Symbol &sym) { 6150b57cec5SDimitry Andric if (sym.globalDynIndex != -1U) 6160b57cec5SDimitry Andric return false; 6170b57cec5SDimitry Andric sym.globalDynIndex = numEntries; 6180b57cec5SDimitry Andric // Global Dynamic TLS entries take two GOT slots. 6190b57cec5SDimitry Andric numEntries += 2; 6200b57cec5SDimitry Andric return true; 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric // Reserves TLS entries for a TLS module ID and a TLS block offset. 6240b57cec5SDimitry Andric // In total it takes two GOT slots. 6250b57cec5SDimitry Andric bool GotSection::addTlsIndex() { 6260b57cec5SDimitry Andric if (tlsIndexOff != uint32_t(-1)) 6270b57cec5SDimitry Andric return false; 6280b57cec5SDimitry Andric tlsIndexOff = numEntries * config->wordsize; 6290b57cec5SDimitry Andric numEntries += 2; 6300b57cec5SDimitry Andric return true; 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const { 6340b57cec5SDimitry Andric return this->getVA() + b.globalDynIndex * config->wordsize; 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const { 6380b57cec5SDimitry Andric return b.globalDynIndex * config->wordsize; 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric void GotSection::finalizeContents() { 6420b57cec5SDimitry Andric size = numEntries * config->wordsize; 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric bool GotSection::isNeeded() const { 6460b57cec5SDimitry Andric // We need to emit a GOT even if it's empty if there's a relocation that is 6470b57cec5SDimitry Andric // relative to GOT(such as GOTOFFREL). 6480b57cec5SDimitry Andric return numEntries || hasGotOffRel; 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric void GotSection::writeTo(uint8_t *buf) { 6520b57cec5SDimitry Andric // Buf points to the start of this section's buffer, 6530b57cec5SDimitry Andric // whereas InputSectionBase::relocateAlloc() expects its argument 6540b57cec5SDimitry Andric // to point to the start of the output section. 6550b57cec5SDimitry Andric target->writeGotHeader(buf); 6560b57cec5SDimitry Andric relocateAlloc(buf - outSecOff, buf - outSecOff + size); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric static uint64_t getMipsPageAddr(uint64_t addr) { 6600b57cec5SDimitry Andric return (addr + 0x8000) & ~0xffff; 6610b57cec5SDimitry Andric } 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric static uint64_t getMipsPageCount(uint64_t size) { 6640b57cec5SDimitry Andric return (size + 0xfffe) / 0xffff + 1; 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric MipsGotSection::MipsGotSection() 6680b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, 6690b57cec5SDimitry Andric ".got") {} 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend, 6720b57cec5SDimitry Andric RelExpr expr) { 6730b57cec5SDimitry Andric FileGot &g = getGot(file); 6740b57cec5SDimitry Andric if (expr == R_MIPS_GOT_LOCAL_PAGE) { 6750b57cec5SDimitry Andric if (const OutputSection *os = sym.getOutputSection()) 6760b57cec5SDimitry Andric g.pagesMap.insert({os, {}}); 6770b57cec5SDimitry Andric else 6780b57cec5SDimitry Andric g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0}); 6790b57cec5SDimitry Andric } else if (sym.isTls()) 6800b57cec5SDimitry Andric g.tls.insert({&sym, 0}); 6810b57cec5SDimitry Andric else if (sym.isPreemptible && expr == R_ABS) 6820b57cec5SDimitry Andric g.relocs.insert({&sym, 0}); 6830b57cec5SDimitry Andric else if (sym.isPreemptible) 6840b57cec5SDimitry Andric g.global.insert({&sym, 0}); 6850b57cec5SDimitry Andric else if (expr == R_MIPS_GOT_OFF32) 6860b57cec5SDimitry Andric g.local32.insert({{&sym, addend}, 0}); 6870b57cec5SDimitry Andric else 6880b57cec5SDimitry Andric g.local16.insert({{&sym, addend}, 0}); 6890b57cec5SDimitry Andric } 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) { 6920b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({&sym, 0}); 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric void MipsGotSection::addTlsIndex(InputFile &file) { 6960b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({nullptr, 0}); 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getEntriesNum() const { 7000b57cec5SDimitry Andric return getPageEntriesNum() + local16.size() + global.size() + relocs.size() + 7010b57cec5SDimitry Andric tls.size() + dynTlsSymbols.size() * 2; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getPageEntriesNum() const { 7050b57cec5SDimitry Andric size_t num = 0; 7060b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap) 7070b57cec5SDimitry Andric num += p.second.count; 7080b57cec5SDimitry Andric return num; 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getIndexedEntriesNum() const { 7120b57cec5SDimitry Andric size_t count = getPageEntriesNum() + local16.size() + global.size(); 7130b57cec5SDimitry Andric // If there are relocation-only entries in the GOT, TLS entries 7140b57cec5SDimitry Andric // are allocated after them. TLS entries should be addressable 7150b57cec5SDimitry Andric // by 16-bit index so count both reloc-only and TLS entries. 7160b57cec5SDimitry Andric if (!tls.empty() || !dynTlsSymbols.empty()) 7170b57cec5SDimitry Andric count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2; 7180b57cec5SDimitry Andric return count; 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) { 7220b57cec5SDimitry Andric if (!f.mipsGotIndex.hasValue()) { 7230b57cec5SDimitry Andric gots.emplace_back(); 7240b57cec5SDimitry Andric gots.back().file = &f; 7250b57cec5SDimitry Andric f.mipsGotIndex = gots.size() - 1; 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric return gots[*f.mipsGotIndex]; 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f, 7310b57cec5SDimitry Andric const Symbol &sym, 7320b57cec5SDimitry Andric int64_t addend) const { 7330b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 7340b57cec5SDimitry Andric uint64_t index = 0; 7350b57cec5SDimitry Andric if (const OutputSection *outSec = sym.getOutputSection()) { 7360b57cec5SDimitry Andric uint64_t secAddr = getMipsPageAddr(outSec->addr); 7370b57cec5SDimitry Andric uint64_t symAddr = getMipsPageAddr(sym.getVA(addend)); 7380b57cec5SDimitry Andric index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff; 7390b57cec5SDimitry Andric } else { 7400b57cec5SDimitry Andric index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))}); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric return index * config->wordsize; 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s, 7460b57cec5SDimitry Andric int64_t addend) const { 7470b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 7480b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 7490b57cec5SDimitry Andric if (sym->isTls()) 7500b57cec5SDimitry Andric return g.tls.lookup(sym) * config->wordsize; 7510b57cec5SDimitry Andric if (sym->isPreemptible) 7520b57cec5SDimitry Andric return g.global.lookup(sym) * config->wordsize; 7530b57cec5SDimitry Andric return g.local16.lookup({sym, addend}) * config->wordsize; 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const { 7570b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 7580b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(nullptr) * config->wordsize; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f, 7620b57cec5SDimitry Andric const Symbol &s) const { 7630b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 7640b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 7650b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(sym) * config->wordsize; 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric const Symbol *MipsGotSection::getFirstGlobalEntry() const { 7690b57cec5SDimitry Andric if (gots.empty()) 7700b57cec5SDimitry Andric return nullptr; 7710b57cec5SDimitry Andric const FileGot &primGot = gots.front(); 7720b57cec5SDimitry Andric if (!primGot.global.empty()) 7730b57cec5SDimitry Andric return primGot.global.front().first; 7740b57cec5SDimitry Andric if (!primGot.relocs.empty()) 7750b57cec5SDimitry Andric return primGot.relocs.front().first; 7760b57cec5SDimitry Andric return nullptr; 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric unsigned MipsGotSection::getLocalEntriesNum() const { 7800b57cec5SDimitry Andric if (gots.empty()) 7810b57cec5SDimitry Andric return headerEntriesNum; 7820b57cec5SDimitry Andric return headerEntriesNum + gots.front().getPageEntriesNum() + 7830b57cec5SDimitry Andric gots.front().local16.size(); 7840b57cec5SDimitry Andric } 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) { 7870b57cec5SDimitry Andric FileGot tmp = dst; 7880b57cec5SDimitry Andric set_union(tmp.pagesMap, src.pagesMap); 7890b57cec5SDimitry Andric set_union(tmp.local16, src.local16); 7900b57cec5SDimitry Andric set_union(tmp.global, src.global); 7910b57cec5SDimitry Andric set_union(tmp.relocs, src.relocs); 7920b57cec5SDimitry Andric set_union(tmp.tls, src.tls); 7930b57cec5SDimitry Andric set_union(tmp.dynTlsSymbols, src.dynTlsSymbols); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric size_t count = isPrimary ? headerEntriesNum : 0; 7960b57cec5SDimitry Andric count += tmp.getIndexedEntriesNum(); 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric if (count * config->wordsize > config->mipsGotSize) 7990b57cec5SDimitry Andric return false; 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric std::swap(tmp, dst); 8020b57cec5SDimitry Andric return true; 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric void MipsGotSection::finalizeContents() { updateAllocSize(); } 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric bool MipsGotSection::updateAllocSize() { 8080b57cec5SDimitry Andric size = headerEntriesNum * config->wordsize; 8090b57cec5SDimitry Andric for (const FileGot &g : gots) 8100b57cec5SDimitry Andric size += g.getEntriesNum() * config->wordsize; 8110b57cec5SDimitry Andric return false; 8120b57cec5SDimitry Andric } 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric void MipsGotSection::build() { 8150b57cec5SDimitry Andric if (gots.empty()) 8160b57cec5SDimitry Andric return; 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric std::vector<FileGot> mergedGots(1); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric // For each GOT move non-preemptible symbols from the `Global` 8210b57cec5SDimitry Andric // to `Local16` list. Preemptible symbol might become non-preemptible 8220b57cec5SDimitry Andric // one if, for example, it gets a related copy relocation. 8230b57cec5SDimitry Andric for (FileGot &got : gots) { 8240b57cec5SDimitry Andric for (auto &p: got.global) 8250b57cec5SDimitry Andric if (!p.first->isPreemptible) 8260b57cec5SDimitry Andric got.local16.insert({{p.first, 0}, 0}); 8270b57cec5SDimitry Andric got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) { 8280b57cec5SDimitry Andric return !p.first->isPreemptible; 8290b57cec5SDimitry Andric }); 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric // For each GOT remove "reloc-only" entry if there is "global" 8330b57cec5SDimitry Andric // entry for the same symbol. And add local entries which indexed 8340b57cec5SDimitry Andric // using 32-bit value at the end of 16-bit entries. 8350b57cec5SDimitry Andric for (FileGot &got : gots) { 8360b57cec5SDimitry Andric got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 8370b57cec5SDimitry Andric return got.global.count(p.first); 8380b57cec5SDimitry Andric }); 8390b57cec5SDimitry Andric set_union(got.local16, got.local32); 8400b57cec5SDimitry Andric got.local32.clear(); 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric // Evaluate number of "reloc-only" entries in the resulting GOT. 8440b57cec5SDimitry Andric // To do that put all unique "reloc-only" and "global" entries 8450b57cec5SDimitry Andric // from all GOTs to the future primary GOT. 8460b57cec5SDimitry Andric FileGot *primGot = &mergedGots.front(); 8470b57cec5SDimitry Andric for (FileGot &got : gots) { 8480b57cec5SDimitry Andric set_union(primGot->relocs, got.global); 8490b57cec5SDimitry Andric set_union(primGot->relocs, got.relocs); 8500b57cec5SDimitry Andric got.relocs.clear(); 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric // Evaluate number of "page" entries in each GOT. 8540b57cec5SDimitry Andric for (FileGot &got : gots) { 8550b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 8560b57cec5SDimitry Andric got.pagesMap) { 8570b57cec5SDimitry Andric const OutputSection *os = p.first; 8580b57cec5SDimitry Andric uint64_t secSize = 0; 8590b57cec5SDimitry Andric for (BaseCommand *cmd : os->sectionCommands) { 8600b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) 8610b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 8620b57cec5SDimitry Andric uint64_t off = alignTo(secSize, isec->alignment); 8630b57cec5SDimitry Andric secSize = off + isec->getSize(); 8640b57cec5SDimitry Andric } 8650b57cec5SDimitry Andric } 8660b57cec5SDimitry Andric p.second.count = getMipsPageCount(secSize); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric // Merge GOTs. Try to join as much as possible GOTs but do not exceed 8710b57cec5SDimitry Andric // maximum GOT size. At first, try to fill the primary GOT because 8720b57cec5SDimitry Andric // the primary GOT can be accessed in the most effective way. If it 8730b57cec5SDimitry Andric // is not possible, try to fill the last GOT in the list, and finally 8740b57cec5SDimitry Andric // create a new GOT if both attempts failed. 8750b57cec5SDimitry Andric for (FileGot &srcGot : gots) { 8760b57cec5SDimitry Andric InputFile *file = srcGot.file; 8770b57cec5SDimitry Andric if (tryMergeGots(mergedGots.front(), srcGot, true)) { 8780b57cec5SDimitry Andric file->mipsGotIndex = 0; 8790b57cec5SDimitry Andric } else { 8800b57cec5SDimitry Andric // If this is the first time we failed to merge with the primary GOT, 8810b57cec5SDimitry Andric // MergedGots.back() will also be the primary GOT. We must make sure not 8820b57cec5SDimitry Andric // to try to merge again with isPrimary=false, as otherwise, if the 8830b57cec5SDimitry Andric // inputs are just right, we could allow the primary GOT to become 1 or 2 8840b57cec5SDimitry Andric // words bigger due to ignoring the header size. 8850b57cec5SDimitry Andric if (mergedGots.size() == 1 || 8860b57cec5SDimitry Andric !tryMergeGots(mergedGots.back(), srcGot, false)) { 8870b57cec5SDimitry Andric mergedGots.emplace_back(); 8880b57cec5SDimitry Andric std::swap(mergedGots.back(), srcGot); 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric file->mipsGotIndex = mergedGots.size() - 1; 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric std::swap(gots, mergedGots); 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric // Reduce number of "reloc-only" entries in the primary GOT 8960b57cec5SDimitry Andric // by substracting "global" entries exist in the primary GOT. 8970b57cec5SDimitry Andric primGot = &gots.front(); 8980b57cec5SDimitry Andric primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 8990b57cec5SDimitry Andric return primGot->global.count(p.first); 9000b57cec5SDimitry Andric }); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric // Calculate indexes for each GOT entry. 9030b57cec5SDimitry Andric size_t index = headerEntriesNum; 9040b57cec5SDimitry Andric for (FileGot &got : gots) { 9050b57cec5SDimitry Andric got.startIndex = &got == primGot ? 0 : index; 9060b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 9070b57cec5SDimitry Andric got.pagesMap) { 9080b57cec5SDimitry Andric // For each output section referenced by GOT page relocations calculate 9090b57cec5SDimitry Andric // and save into pagesMap an upper bound of MIPS GOT entries required 9100b57cec5SDimitry Andric // to store page addresses of local symbols. We assume the worst case - 9110b57cec5SDimitry Andric // each 64kb page of the output section has at least one GOT relocation 9120b57cec5SDimitry Andric // against it. And take in account the case when the section intersects 9130b57cec5SDimitry Andric // page boundaries. 9140b57cec5SDimitry Andric p.second.firstIndex = index; 9150b57cec5SDimitry Andric index += p.second.count; 9160b57cec5SDimitry Andric } 9170b57cec5SDimitry Andric for (auto &p: got.local16) 9180b57cec5SDimitry Andric p.second = index++; 9190b57cec5SDimitry Andric for (auto &p: got.global) 9200b57cec5SDimitry Andric p.second = index++; 9210b57cec5SDimitry Andric for (auto &p: got.relocs) 9220b57cec5SDimitry Andric p.second = index++; 9230b57cec5SDimitry Andric for (auto &p: got.tls) 9240b57cec5SDimitry Andric p.second = index++; 9250b57cec5SDimitry Andric for (auto &p: got.dynTlsSymbols) { 9260b57cec5SDimitry Andric p.second = index; 9270b57cec5SDimitry Andric index += 2; 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric // Update Symbol::gotIndex field to use this 9320b57cec5SDimitry Andric // value later in the `sortMipsSymbols` function. 9330b57cec5SDimitry Andric for (auto &p : primGot->global) 9340b57cec5SDimitry Andric p.first->gotIndex = p.second; 9350b57cec5SDimitry Andric for (auto &p : primGot->relocs) 9360b57cec5SDimitry Andric p.first->gotIndex = p.second; 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric // Create dynamic relocations. 9390b57cec5SDimitry Andric for (FileGot &got : gots) { 9400b57cec5SDimitry Andric // Create dynamic relocations for TLS entries. 9410b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.tls) { 9420b57cec5SDimitry Andric Symbol *s = p.first; 9430b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 9440b57cec5SDimitry Andric if (s->isPreemptible) 9450b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsGotRel, this, offset, s); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) { 9480b57cec5SDimitry Andric Symbol *s = p.first; 9490b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 9500b57cec5SDimitry Andric if (s == nullptr) { 9510b57cec5SDimitry Andric if (!config->isPic) 9520b57cec5SDimitry Andric continue; 9530b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s); 9540b57cec5SDimitry Andric } else { 9550b57cec5SDimitry Andric // When building a shared library we still need a dynamic relocation 9560b57cec5SDimitry Andric // for the module index. Therefore only checking for 9570b57cec5SDimitry Andric // S->isPreemptible is not sufficient (this happens e.g. for 9580b57cec5SDimitry Andric // thread-locals that have been marked as local through a linker script) 9590b57cec5SDimitry Andric if (!s->isPreemptible && !config->isPic) 9600b57cec5SDimitry Andric continue; 9610b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s); 9620b57cec5SDimitry Andric // However, we can skip writing the TLS offset reloc for non-preemptible 9630b57cec5SDimitry Andric // symbols since it is known even in shared libraries 9640b57cec5SDimitry Andric if (!s->isPreemptible) 9650b57cec5SDimitry Andric continue; 9660b57cec5SDimitry Andric offset += config->wordsize; 9670b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsOffsetRel, this, offset, s); 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric // Do not create dynamic relocations for non-TLS 9720b57cec5SDimitry Andric // entries in the primary GOT. 9730b57cec5SDimitry Andric if (&got == primGot) 9740b57cec5SDimitry Andric continue; 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric // Dynamic relocations for "global" entries. 9770b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : got.global) { 9780b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 9790b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->relativeRel, this, offset, p.first); 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric if (!config->isPic) 9820b57cec5SDimitry Andric continue; 9830b57cec5SDimitry Andric // Dynamic relocations for "local" entries in case of PIC. 9840b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 9850b57cec5SDimitry Andric got.pagesMap) { 9860b57cec5SDimitry Andric size_t pageCount = l.second.count; 9870b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) { 9880b57cec5SDimitry Andric uint64_t offset = (l.second.firstIndex + pi) * config->wordsize; 9890b57cec5SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first, 9900b57cec5SDimitry Andric int64_t(pi * 0x10000)}); 9910b57cec5SDimitry Andric } 9920b57cec5SDimitry Andric } 9930b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : got.local16) { 9940b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 9950b57cec5SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, true, 9960b57cec5SDimitry Andric p.first.first, p.first.second}); 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric bool MipsGotSection::isNeeded() const { 10020b57cec5SDimitry Andric // We add the .got section to the result for dynamic MIPS target because 10030b57cec5SDimitry Andric // its address and properties are mentioned in the .dynamic section. 10040b57cec5SDimitry Andric return !config->relocatable; 10050b57cec5SDimitry Andric } 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric uint64_t MipsGotSection::getGp(const InputFile *f) const { 10080b57cec5SDimitry Andric // For files without related GOT or files refer a primary GOT 10090b57cec5SDimitry Andric // returns "common" _gp value. For secondary GOTs calculate 10100b57cec5SDimitry Andric // individual _gp values. 10110b57cec5SDimitry Andric if (!f || !f->mipsGotIndex.hasValue() || *f->mipsGotIndex == 0) 10120b57cec5SDimitry Andric return ElfSym::mipsGp->getVA(0); 10130b57cec5SDimitry Andric return getVA() + gots[*f->mipsGotIndex].startIndex * config->wordsize + 10140b57cec5SDimitry Andric 0x7ff0; 10150b57cec5SDimitry Andric } 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric void MipsGotSection::writeTo(uint8_t *buf) { 10180b57cec5SDimitry Andric // Set the MSB of the second GOT slot. This is not required by any 10190b57cec5SDimitry Andric // MIPS ABI documentation, though. 10200b57cec5SDimitry Andric // 10210b57cec5SDimitry Andric // There is a comment in glibc saying that "The MSB of got[1] of a 10220b57cec5SDimitry Andric // gnu object is set to identify gnu objects," and in GNU gold it 10230b57cec5SDimitry Andric // says "the second entry will be used by some runtime loaders". 10240b57cec5SDimitry Andric // But how this field is being used is unclear. 10250b57cec5SDimitry Andric // 10260b57cec5SDimitry Andric // We are not really willing to mimic other linkers behaviors 10270b57cec5SDimitry Andric // without understanding why they do that, but because all files 10280b57cec5SDimitry Andric // generated by GNU tools have this special GOT value, and because 10290b57cec5SDimitry Andric // we've been doing this for years, it is probably a safe bet to 10300b57cec5SDimitry Andric // keep doing this for now. We really need to revisit this to see 10310b57cec5SDimitry Andric // if we had to do this. 10320b57cec5SDimitry Andric writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1)); 10330b57cec5SDimitry Andric for (const FileGot &g : gots) { 10340b57cec5SDimitry Andric auto write = [&](size_t i, const Symbol *s, int64_t a) { 10350b57cec5SDimitry Andric uint64_t va = a; 10360b57cec5SDimitry Andric if (s) 10370b57cec5SDimitry Andric va = s->getVA(a); 10380b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, va); 10390b57cec5SDimitry Andric }; 10400b57cec5SDimitry Andric // Write 'page address' entries to the local part of the GOT. 10410b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 10420b57cec5SDimitry Andric g.pagesMap) { 10430b57cec5SDimitry Andric size_t pageCount = l.second.count; 10440b57cec5SDimitry Andric uint64_t firstPageAddr = getMipsPageAddr(l.first->addr); 10450b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) 10460b57cec5SDimitry Andric write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000); 10470b57cec5SDimitry Andric } 10480b57cec5SDimitry Andric // Local, global, TLS, reloc-only entries. 10490b57cec5SDimitry Andric // If TLS entry has a corresponding dynamic relocations, leave it 10500b57cec5SDimitry Andric // initialized by zero. Write down adjusted TLS symbol's values otherwise. 10510b57cec5SDimitry Andric // To calculate the adjustments use offsets for thread-local storage. 10520b57cec5SDimitry Andric // https://www.linux-mips.org/wiki/NPTL 10530b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : g.local16) 10540b57cec5SDimitry Andric write(p.second, p.first.first, p.first.second); 10550b57cec5SDimitry Andric // Write VA to the primary GOT only. For secondary GOTs that 10560b57cec5SDimitry Andric // will be done by REL32 dynamic relocations. 10570b57cec5SDimitry Andric if (&g == &gots.front()) 10580b57cec5SDimitry Andric for (const std::pair<const Symbol *, size_t> &p : g.global) 10590b57cec5SDimitry Andric write(p.second, p.first, 0); 10600b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.relocs) 10610b57cec5SDimitry Andric write(p.second, p.first, 0); 10620b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.tls) 10630b57cec5SDimitry Andric write(p.second, p.first, p.first->isPreemptible ? 0 : -0x7000); 10640b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) { 10650b57cec5SDimitry Andric if (p.first == nullptr && !config->isPic) 10660b57cec5SDimitry Andric write(p.second, nullptr, 1); 10670b57cec5SDimitry Andric else if (p.first && !p.first->isPreemptible) { 10680b57cec5SDimitry Andric // If we are emitting PIC code with relocations we mustn't write 10690b57cec5SDimitry Andric // anything to the GOT here. When using Elf_Rel relocations the value 10700b57cec5SDimitry Andric // one will be treated as an addend and will cause crashes at runtime 10710b57cec5SDimitry Andric if (!config->isPic) 10720b57cec5SDimitry Andric write(p.second, nullptr, 1); 10730b57cec5SDimitry Andric write(p.second + 1, p.first, -0x8000); 10740b57cec5SDimitry Andric } 10750b57cec5SDimitry Andric } 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric // On PowerPC the .plt section is used to hold the table of function addresses 10800b57cec5SDimitry Andric // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss 10810b57cec5SDimitry Andric // section. I don't know why we have a BSS style type for the section but it is 10820b57cec5SDimitry Andric // consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI. 10830b57cec5SDimitry Andric GotPltSection::GotPltSection() 10840b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 10850b57cec5SDimitry Andric ".got.plt") { 10860b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 10870b57cec5SDimitry Andric name = ".plt"; 10880b57cec5SDimitry Andric } else if (config->emachine == EM_PPC64) { 10890b57cec5SDimitry Andric type = SHT_NOBITS; 10900b57cec5SDimitry Andric name = ".plt"; 10910b57cec5SDimitry Andric } 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric void GotPltSection::addEntry(Symbol &sym) { 10950b57cec5SDimitry Andric assert(sym.pltIndex == entries.size()); 10960b57cec5SDimitry Andric entries.push_back(&sym); 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric size_t GotPltSection::getSize() const { 11000b57cec5SDimitry Andric return (target->gotPltHeaderEntriesNum + entries.size()) * config->wordsize; 11010b57cec5SDimitry Andric } 11020b57cec5SDimitry Andric 11030b57cec5SDimitry Andric void GotPltSection::writeTo(uint8_t *buf) { 11040b57cec5SDimitry Andric target->writeGotPltHeader(buf); 11050b57cec5SDimitry Andric buf += target->gotPltHeaderEntriesNum * config->wordsize; 11060b57cec5SDimitry Andric for (const Symbol *b : entries) { 11070b57cec5SDimitry Andric target->writeGotPlt(buf, *b); 11080b57cec5SDimitry Andric buf += config->wordsize; 11090b57cec5SDimitry Andric } 11100b57cec5SDimitry Andric } 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric bool GotPltSection::isNeeded() const { 11130b57cec5SDimitry Andric // We need to emit GOTPLT even if it's empty if there's a relocation relative 11140b57cec5SDimitry Andric // to it. 11150b57cec5SDimitry Andric return !entries.empty() || hasGotPltOffRel; 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric static StringRef getIgotPltName() { 11190b57cec5SDimitry Andric // On ARM the IgotPltSection is part of the GotSection. 11200b57cec5SDimitry Andric if (config->emachine == EM_ARM) 11210b57cec5SDimitry Andric return ".got"; 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection 11240b57cec5SDimitry Andric // needs to be named the same. 11250b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 11260b57cec5SDimitry Andric return ".plt"; 11270b57cec5SDimitry Andric 11280b57cec5SDimitry Andric return ".got.plt"; 11290b57cec5SDimitry Andric } 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit 11320b57cec5SDimitry Andric // with the IgotPltSection. 11330b57cec5SDimitry Andric IgotPltSection::IgotPltSection() 11340b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 11350b57cec5SDimitry Andric config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS, 11360b57cec5SDimitry Andric config->wordsize, getIgotPltName()) {} 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric void IgotPltSection::addEntry(Symbol &sym) { 11390b57cec5SDimitry Andric assert(sym.pltIndex == entries.size()); 11400b57cec5SDimitry Andric entries.push_back(&sym); 11410b57cec5SDimitry Andric } 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric size_t IgotPltSection::getSize() const { 11440b57cec5SDimitry Andric return entries.size() * config->wordsize; 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric void IgotPltSection::writeTo(uint8_t *buf) { 11480b57cec5SDimitry Andric for (const Symbol *b : entries) { 11490b57cec5SDimitry Andric target->writeIgotPlt(buf, *b); 11500b57cec5SDimitry Andric buf += config->wordsize; 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric } 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric StringTableSection::StringTableSection(StringRef name, bool dynamic) 11550b57cec5SDimitry Andric : SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name), 11560b57cec5SDimitry Andric dynamic(dynamic) { 11570b57cec5SDimitry Andric // ELF string tables start with a NUL byte. 11580b57cec5SDimitry Andric addString(""); 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric // Adds a string to the string table. If `hashIt` is true we hash and check for 11620b57cec5SDimitry Andric // duplicates. It is optional because the name of global symbols are already 11630b57cec5SDimitry Andric // uniqued and hashing them again has a big cost for a small value: uniquing 11640b57cec5SDimitry Andric // them with some other string that happens to be the same. 11650b57cec5SDimitry Andric unsigned StringTableSection::addString(StringRef s, bool hashIt) { 11660b57cec5SDimitry Andric if (hashIt) { 11670b57cec5SDimitry Andric auto r = stringMap.insert(std::make_pair(s, this->size)); 11680b57cec5SDimitry Andric if (!r.second) 11690b57cec5SDimitry Andric return r.first->second; 11700b57cec5SDimitry Andric } 11710b57cec5SDimitry Andric unsigned ret = this->size; 11720b57cec5SDimitry Andric this->size = this->size + s.size() + 1; 11730b57cec5SDimitry Andric strings.push_back(s); 11740b57cec5SDimitry Andric return ret; 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric void StringTableSection::writeTo(uint8_t *buf) { 11780b57cec5SDimitry Andric for (StringRef s : strings) { 11790b57cec5SDimitry Andric memcpy(buf, s.data(), s.size()); 11800b57cec5SDimitry Andric buf[s.size()] = '\0'; 11810b57cec5SDimitry Andric buf += s.size() + 1; 11820b57cec5SDimitry Andric } 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric 1185*85868e8aSDimitry Andric // Returns the number of entries in .gnu.version_d: the number of 1186*85868e8aSDimitry Andric // non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1. 1187*85868e8aSDimitry Andric // Note that we don't support vd_cnt > 1 yet. 1188*85868e8aSDimitry Andric static unsigned getVerDefNum() { 1189*85868e8aSDimitry Andric return namedVersionDefs().size() + 1; 1190*85868e8aSDimitry Andric } 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric template <class ELFT> 11930b57cec5SDimitry Andric DynamicSection<ELFT>::DynamicSection() 11940b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize, 11950b57cec5SDimitry Andric ".dynamic") { 11960b57cec5SDimitry Andric this->entsize = ELFT::Is64Bits ? 16 : 8; 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric // .dynamic section is not writable on MIPS and on Fuchsia OS 11990b57cec5SDimitry Andric // which passes -z rodynamic. 12000b57cec5SDimitry Andric // See "Special Section" in Chapter 4 in the following document: 12010b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 12020b57cec5SDimitry Andric if (config->emachine == EM_MIPS || config->zRodynamic) 12030b57cec5SDimitry Andric this->flags = SHF_ALLOC; 12040b57cec5SDimitry Andric } 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric template <class ELFT> 12070b57cec5SDimitry Andric void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) { 12080b57cec5SDimitry Andric entries.push_back({tag, fn}); 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric template <class ELFT> 12120b57cec5SDimitry Andric void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) { 12130b57cec5SDimitry Andric entries.push_back({tag, [=] { return val; }}); 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric template <class ELFT> 12170b57cec5SDimitry Andric void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) { 12180b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->getVA(0); }}); 12190b57cec5SDimitry Andric } 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric template <class ELFT> 12220b57cec5SDimitry Andric void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) { 12230b57cec5SDimitry Andric size_t tagOffset = entries.size() * entsize; 12240b57cec5SDimitry Andric entries.push_back( 12250b57cec5SDimitry Andric {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }}); 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric 12280b57cec5SDimitry Andric template <class ELFT> 12290b57cec5SDimitry Andric void DynamicSection<ELFT>::addOutSec(int32_t tag, OutputSection *sec) { 12300b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->addr; }}); 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric template <class ELFT> 12340b57cec5SDimitry Andric void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) { 12350b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->size; }}); 12360b57cec5SDimitry Andric } 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric template <class ELFT> 12390b57cec5SDimitry Andric void DynamicSection<ELFT>::addSym(int32_t tag, Symbol *sym) { 12400b57cec5SDimitry Andric entries.push_back({tag, [=] { return sym->getVA(); }}); 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric 1243*85868e8aSDimitry Andric // The output section .rela.dyn may include these synthetic sections: 1244*85868e8aSDimitry Andric // 1245*85868e8aSDimitry Andric // - part.relaDyn 1246*85868e8aSDimitry Andric // - in.relaIplt: this is included if in.relaIplt is named .rela.dyn 1247*85868e8aSDimitry Andric // - in.relaPlt: this is included if a linker script places .rela.plt inside 1248*85868e8aSDimitry Andric // .rela.dyn 1249*85868e8aSDimitry Andric // 1250*85868e8aSDimitry Andric // DT_RELASZ is the total size of the included sections. 1251*85868e8aSDimitry Andric static std::function<uint64_t()> addRelaSz(RelocationBaseSection *relaDyn) { 1252*85868e8aSDimitry Andric return [=]() { 1253*85868e8aSDimitry Andric size_t size = relaDyn->getSize(); 1254*85868e8aSDimitry Andric if (in.relaIplt->getParent() == relaDyn->getParent()) 1255*85868e8aSDimitry Andric size += in.relaIplt->getSize(); 1256*85868e8aSDimitry Andric if (in.relaPlt->getParent() == relaDyn->getParent()) 1257*85868e8aSDimitry Andric size += in.relaPlt->getSize(); 1258*85868e8aSDimitry Andric return size; 1259*85868e8aSDimitry Andric }; 1260*85868e8aSDimitry Andric } 1261*85868e8aSDimitry Andric 12620b57cec5SDimitry Andric // A Linker script may assign the RELA relocation sections to the same 12630b57cec5SDimitry Andric // output section. When this occurs we cannot just use the OutputSection 12640b57cec5SDimitry Andric // Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to 12650b57cec5SDimitry Andric // overlap with the [DT_RELA, DT_RELA + DT_RELASZ). 12660b57cec5SDimitry Andric static uint64_t addPltRelSz() { 12670b57cec5SDimitry Andric size_t size = in.relaPlt->getSize(); 12680b57cec5SDimitry Andric if (in.relaIplt->getParent() == in.relaPlt->getParent() && 12690b57cec5SDimitry Andric in.relaIplt->name == in.relaPlt->name) 12700b57cec5SDimitry Andric size += in.relaIplt->getSize(); 12710b57cec5SDimitry Andric return size; 12720b57cec5SDimitry Andric } 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric // Add remaining entries to complete .dynamic contents. 12750b57cec5SDimitry Andric template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { 1276*85868e8aSDimitry Andric Partition &part = getPartition(); 12770b57cec5SDimitry Andric bool isMain = part.name.empty(); 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric for (StringRef s : config->filterList) 12800b57cec5SDimitry Andric addInt(DT_FILTER, part.dynStrTab->addString(s)); 12810b57cec5SDimitry Andric for (StringRef s : config->auxiliaryList) 12820b57cec5SDimitry Andric addInt(DT_AUXILIARY, part.dynStrTab->addString(s)); 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric if (!config->rpath.empty()) 12850b57cec5SDimitry Andric addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH, 12860b57cec5SDimitry Andric part.dynStrTab->addString(config->rpath)); 12870b57cec5SDimitry Andric 12880b57cec5SDimitry Andric for (SharedFile *file : sharedFiles) 12890b57cec5SDimitry Andric if (file->isNeeded) 12900b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(file->soName)); 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric if (isMain) { 12930b57cec5SDimitry Andric if (!config->soName.empty()) 12940b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(config->soName)); 12950b57cec5SDimitry Andric } else { 12960b57cec5SDimitry Andric if (!config->soName.empty()) 12970b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(config->soName)); 12980b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(part.name)); 12990b57cec5SDimitry Andric } 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric // Set DT_FLAGS and DT_FLAGS_1. 13020b57cec5SDimitry Andric uint32_t dtFlags = 0; 13030b57cec5SDimitry Andric uint32_t dtFlags1 = 0; 13040b57cec5SDimitry Andric if (config->bsymbolic) 13050b57cec5SDimitry Andric dtFlags |= DF_SYMBOLIC; 13060b57cec5SDimitry Andric if (config->zGlobal) 13070b57cec5SDimitry Andric dtFlags1 |= DF_1_GLOBAL; 13080b57cec5SDimitry Andric if (config->zInitfirst) 13090b57cec5SDimitry Andric dtFlags1 |= DF_1_INITFIRST; 13100b57cec5SDimitry Andric if (config->zInterpose) 13110b57cec5SDimitry Andric dtFlags1 |= DF_1_INTERPOSE; 13120b57cec5SDimitry Andric if (config->zNodefaultlib) 13130b57cec5SDimitry Andric dtFlags1 |= DF_1_NODEFLIB; 13140b57cec5SDimitry Andric if (config->zNodelete) 13150b57cec5SDimitry Andric dtFlags1 |= DF_1_NODELETE; 13160b57cec5SDimitry Andric if (config->zNodlopen) 13170b57cec5SDimitry Andric dtFlags1 |= DF_1_NOOPEN; 13180b57cec5SDimitry Andric if (config->zNow) { 13190b57cec5SDimitry Andric dtFlags |= DF_BIND_NOW; 13200b57cec5SDimitry Andric dtFlags1 |= DF_1_NOW; 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric if (config->zOrigin) { 13230b57cec5SDimitry Andric dtFlags |= DF_ORIGIN; 13240b57cec5SDimitry Andric dtFlags1 |= DF_1_ORIGIN; 13250b57cec5SDimitry Andric } 13260b57cec5SDimitry Andric if (!config->zText) 13270b57cec5SDimitry Andric dtFlags |= DF_TEXTREL; 13280b57cec5SDimitry Andric if (config->hasStaticTlsModel) 13290b57cec5SDimitry Andric dtFlags |= DF_STATIC_TLS; 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric if (dtFlags) 13320b57cec5SDimitry Andric addInt(DT_FLAGS, dtFlags); 13330b57cec5SDimitry Andric if (dtFlags1) 13340b57cec5SDimitry Andric addInt(DT_FLAGS_1, dtFlags1); 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We 13370b57cec5SDimitry Andric // need it for each process, so we don't write it for DSOs. The loader writes 13380b57cec5SDimitry Andric // the pointer into this entry. 13390b57cec5SDimitry Andric // 13400b57cec5SDimitry Andric // DT_DEBUG is the only .dynamic entry that needs to be written to. Some 13410b57cec5SDimitry Andric // systems (currently only Fuchsia OS) provide other means to give the 13420b57cec5SDimitry Andric // debugger this information. Such systems may choose make .dynamic read-only. 13430b57cec5SDimitry Andric // If the target is such a system (used -z rodynamic) don't write DT_DEBUG. 13440b57cec5SDimitry Andric if (!config->shared && !config->relocatable && !config->zRodynamic) 13450b57cec5SDimitry Andric addInt(DT_DEBUG, 0); 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric if (OutputSection *sec = part.dynStrTab->getParent()) 13480b57cec5SDimitry Andric this->link = sec->sectionIndex; 13490b57cec5SDimitry Andric 1350*85868e8aSDimitry Andric if (part.relaDyn->isNeeded() || 1351*85868e8aSDimitry Andric (in.relaIplt->isNeeded() && 1352*85868e8aSDimitry Andric part.relaDyn->getParent() == in.relaIplt->getParent())) { 13530b57cec5SDimitry Andric addInSec(part.relaDyn->dynamicTag, part.relaDyn); 1354*85868e8aSDimitry Andric entries.push_back({part.relaDyn->sizeDynamicTag, addRelaSz(part.relaDyn)}); 13550b57cec5SDimitry Andric 13560b57cec5SDimitry Andric bool isRela = config->isRela; 13570b57cec5SDimitry Andric addInt(isRela ? DT_RELAENT : DT_RELENT, 13580b57cec5SDimitry Andric isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andric // MIPS dynamic loader does not support RELCOUNT tag. 13610b57cec5SDimitry Andric // The problem is in the tight relation between dynamic 13620b57cec5SDimitry Andric // relocations and GOT. So do not emit this tag on MIPS. 13630b57cec5SDimitry Andric if (config->emachine != EM_MIPS) { 13640b57cec5SDimitry Andric size_t numRelativeRels = part.relaDyn->getRelativeRelocCount(); 13650b57cec5SDimitry Andric if (config->zCombreloc && numRelativeRels) 13660b57cec5SDimitry Andric addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels); 13670b57cec5SDimitry Andric } 13680b57cec5SDimitry Andric } 13690b57cec5SDimitry Andric if (part.relrDyn && !part.relrDyn->relocs.empty()) { 13700b57cec5SDimitry Andric addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR, 13710b57cec5SDimitry Andric part.relrDyn); 13720b57cec5SDimitry Andric addSize(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ, 13730b57cec5SDimitry Andric part.relrDyn->getParent()); 13740b57cec5SDimitry Andric addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT, 13750b57cec5SDimitry Andric sizeof(Elf_Relr)); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric // .rel[a].plt section usually consists of two parts, containing plt and 13780b57cec5SDimitry Andric // iplt relocations. It is possible to have only iplt relocations in the 13790b57cec5SDimitry Andric // output. In that case relaPlt is empty and have zero offset, the same offset 13800b57cec5SDimitry Andric // as relaIplt has. And we still want to emit proper dynamic tags for that 13810b57cec5SDimitry Andric // case, so here we always use relaPlt as marker for the begining of 13820b57cec5SDimitry Andric // .rel[a].plt section. 13830b57cec5SDimitry Andric if (isMain && (in.relaPlt->isNeeded() || in.relaIplt->isNeeded())) { 13840b57cec5SDimitry Andric addInSec(DT_JMPREL, in.relaPlt); 13850b57cec5SDimitry Andric entries.push_back({DT_PLTRELSZ, addPltRelSz}); 13860b57cec5SDimitry Andric switch (config->emachine) { 13870b57cec5SDimitry Andric case EM_MIPS: 13880b57cec5SDimitry Andric addInSec(DT_MIPS_PLTGOT, in.gotPlt); 13890b57cec5SDimitry Andric break; 13900b57cec5SDimitry Andric case EM_SPARCV9: 13910b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.plt); 13920b57cec5SDimitry Andric break; 13930b57cec5SDimitry Andric default: 13940b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.gotPlt); 13950b57cec5SDimitry Andric break; 13960b57cec5SDimitry Andric } 13970b57cec5SDimitry Andric addInt(DT_PLTREL, config->isRela ? DT_RELA : DT_REL); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric if (config->emachine == EM_AARCH64) { 14010b57cec5SDimitry Andric if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 14020b57cec5SDimitry Andric addInt(DT_AARCH64_BTI_PLT, 0); 14030b57cec5SDimitry Andric if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC) 14040b57cec5SDimitry Andric addInt(DT_AARCH64_PAC_PLT, 0); 14050b57cec5SDimitry Andric } 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric addInSec(DT_SYMTAB, part.dynSymTab); 14080b57cec5SDimitry Andric addInt(DT_SYMENT, sizeof(Elf_Sym)); 14090b57cec5SDimitry Andric addInSec(DT_STRTAB, part.dynStrTab); 14100b57cec5SDimitry Andric addInt(DT_STRSZ, part.dynStrTab->getSize()); 14110b57cec5SDimitry Andric if (!config->zText) 14120b57cec5SDimitry Andric addInt(DT_TEXTREL, 0); 14130b57cec5SDimitry Andric if (part.gnuHashTab) 14140b57cec5SDimitry Andric addInSec(DT_GNU_HASH, part.gnuHashTab); 14150b57cec5SDimitry Andric if (part.hashTab) 14160b57cec5SDimitry Andric addInSec(DT_HASH, part.hashTab); 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric if (isMain) { 14190b57cec5SDimitry Andric if (Out::preinitArray) { 14200b57cec5SDimitry Andric addOutSec(DT_PREINIT_ARRAY, Out::preinitArray); 14210b57cec5SDimitry Andric addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray); 14220b57cec5SDimitry Andric } 14230b57cec5SDimitry Andric if (Out::initArray) { 14240b57cec5SDimitry Andric addOutSec(DT_INIT_ARRAY, Out::initArray); 14250b57cec5SDimitry Andric addSize(DT_INIT_ARRAYSZ, Out::initArray); 14260b57cec5SDimitry Andric } 14270b57cec5SDimitry Andric if (Out::finiArray) { 14280b57cec5SDimitry Andric addOutSec(DT_FINI_ARRAY, Out::finiArray); 14290b57cec5SDimitry Andric addSize(DT_FINI_ARRAYSZ, Out::finiArray); 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric if (Symbol *b = symtab->find(config->init)) 14330b57cec5SDimitry Andric if (b->isDefined()) 14340b57cec5SDimitry Andric addSym(DT_INIT, b); 14350b57cec5SDimitry Andric if (Symbol *b = symtab->find(config->fini)) 14360b57cec5SDimitry Andric if (b->isDefined()) 14370b57cec5SDimitry Andric addSym(DT_FINI, b); 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric bool hasVerNeed = SharedFile::vernauxNum != 0; 14410b57cec5SDimitry Andric if (hasVerNeed || part.verDef) 14420b57cec5SDimitry Andric addInSec(DT_VERSYM, part.verSym); 14430b57cec5SDimitry Andric if (part.verDef) { 14440b57cec5SDimitry Andric addInSec(DT_VERDEF, part.verDef); 14450b57cec5SDimitry Andric addInt(DT_VERDEFNUM, getVerDefNum()); 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric if (hasVerNeed) { 14480b57cec5SDimitry Andric addInSec(DT_VERNEED, part.verNeed); 14490b57cec5SDimitry Andric unsigned needNum = 0; 14500b57cec5SDimitry Andric for (SharedFile *f : sharedFiles) 14510b57cec5SDimitry Andric if (!f->vernauxs.empty()) 14520b57cec5SDimitry Andric ++needNum; 14530b57cec5SDimitry Andric addInt(DT_VERNEEDNUM, needNum); 14540b57cec5SDimitry Andric } 14550b57cec5SDimitry Andric 14560b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 14570b57cec5SDimitry Andric addInt(DT_MIPS_RLD_VERSION, 1); 14580b57cec5SDimitry Andric addInt(DT_MIPS_FLAGS, RHF_NOTPOT); 14590b57cec5SDimitry Andric addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase()); 14600b57cec5SDimitry Andric addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols()); 14610b57cec5SDimitry Andric 14620b57cec5SDimitry Andric add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); }); 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric if (const Symbol *b = in.mipsGot->getFirstGlobalEntry()) 14650b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, b->dynsymIndex); 14660b57cec5SDimitry Andric else 14670b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols()); 14680b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.mipsGot); 14690b57cec5SDimitry Andric if (in.mipsRldMap) { 14700b57cec5SDimitry Andric if (!config->pie) 14710b57cec5SDimitry Andric addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap); 14720b57cec5SDimitry Andric // Store the offset to the .rld_map section 14730b57cec5SDimitry Andric // relative to the address of the tag. 14740b57cec5SDimitry Andric addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap); 14750b57cec5SDimitry Andric } 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent, 14790b57cec5SDimitry Andric // glibc assumes the old-style BSS PLT layout which we don't support. 14800b57cec5SDimitry Andric if (config->emachine == EM_PPC) 14810b57cec5SDimitry Andric add(DT_PPC_GOT, [] { return in.got->getVA(); }); 14820b57cec5SDimitry Andric 14830b57cec5SDimitry Andric // Glink dynamic tag is required by the V2 abi if the plt section isn't empty. 14840b57cec5SDimitry Andric if (config->emachine == EM_PPC64 && in.plt->isNeeded()) { 14850b57cec5SDimitry Andric // The Glink tag points to 32 bytes before the first lazy symbol resolution 14860b57cec5SDimitry Andric // stub, which starts directly after the header. 14870b57cec5SDimitry Andric entries.push_back({DT_PPC64_GLINK, [=] { 14880b57cec5SDimitry Andric unsigned offset = target->pltHeaderSize - 32; 14890b57cec5SDimitry Andric return in.plt->getVA(0) + offset; 14900b57cec5SDimitry Andric }}); 14910b57cec5SDimitry Andric } 14920b57cec5SDimitry Andric 14930b57cec5SDimitry Andric addInt(DT_NULL, 0); 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric getParent()->link = this->link; 14960b57cec5SDimitry Andric this->size = entries.size() * this->entsize; 14970b57cec5SDimitry Andric } 14980b57cec5SDimitry Andric 14990b57cec5SDimitry Andric template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) { 15000b57cec5SDimitry Andric auto *p = reinterpret_cast<Elf_Dyn *>(buf); 15010b57cec5SDimitry Andric 15020b57cec5SDimitry Andric for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) { 15030b57cec5SDimitry Andric p->d_tag = kv.first; 15040b57cec5SDimitry Andric p->d_un.d_val = kv.second(); 15050b57cec5SDimitry Andric ++p; 15060b57cec5SDimitry Andric } 15070b57cec5SDimitry Andric } 15080b57cec5SDimitry Andric 15090b57cec5SDimitry Andric uint64_t DynamicReloc::getOffset() const { 15100b57cec5SDimitry Andric return inputSec->getVA(offsetInSec); 15110b57cec5SDimitry Andric } 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric int64_t DynamicReloc::computeAddend() const { 15140b57cec5SDimitry Andric if (useSymVA) 15150b57cec5SDimitry Andric return sym->getVA(addend); 15160b57cec5SDimitry Andric if (!outputSec) 15170b57cec5SDimitry Andric return addend; 15180b57cec5SDimitry Andric // See the comment in the DynamicReloc ctor. 15190b57cec5SDimitry Andric return getMipsPageAddr(outputSec->addr) + addend; 15200b57cec5SDimitry Andric } 15210b57cec5SDimitry Andric 15220b57cec5SDimitry Andric uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const { 15230b57cec5SDimitry Andric if (sym && !useSymVA) 15240b57cec5SDimitry Andric return symTab->getSymbolIndex(sym); 15250b57cec5SDimitry Andric return 0; 15260b57cec5SDimitry Andric } 15270b57cec5SDimitry Andric 15280b57cec5SDimitry Andric RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type, 15290b57cec5SDimitry Andric int32_t dynamicTag, 15300b57cec5SDimitry Andric int32_t sizeDynamicTag) 15310b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, type, config->wordsize, name), 15320b57cec5SDimitry Andric dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag) {} 15330b57cec5SDimitry Andric 15340b57cec5SDimitry Andric void RelocationBaseSection::addReloc(RelType dynType, InputSectionBase *isec, 15350b57cec5SDimitry Andric uint64_t offsetInSec, Symbol *sym) { 15360b57cec5SDimitry Andric addReloc({dynType, isec, offsetInSec, false, sym, 0}); 15370b57cec5SDimitry Andric } 15380b57cec5SDimitry Andric 15390b57cec5SDimitry Andric void RelocationBaseSection::addReloc(RelType dynType, 15400b57cec5SDimitry Andric InputSectionBase *inputSec, 15410b57cec5SDimitry Andric uint64_t offsetInSec, Symbol *sym, 15420b57cec5SDimitry Andric int64_t addend, RelExpr expr, 15430b57cec5SDimitry Andric RelType type) { 15440b57cec5SDimitry Andric // Write the addends to the relocated address if required. We skip 15450b57cec5SDimitry Andric // it if the written value would be zero. 15460b57cec5SDimitry Andric if (config->writeAddends && (expr != R_ADDEND || addend != 0)) 15470b57cec5SDimitry Andric inputSec->relocations.push_back({expr, type, offsetInSec, addend, sym}); 15480b57cec5SDimitry Andric addReloc({dynType, inputSec, offsetInSec, expr != R_ADDEND, sym, addend}); 15490b57cec5SDimitry Andric } 15500b57cec5SDimitry Andric 15510b57cec5SDimitry Andric void RelocationBaseSection::addReloc(const DynamicReloc &reloc) { 15520b57cec5SDimitry Andric if (reloc.type == target->relativeRel) 15530b57cec5SDimitry Andric ++numRelativeRelocs; 15540b57cec5SDimitry Andric relocs.push_back(reloc); 15550b57cec5SDimitry Andric } 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric void RelocationBaseSection::finalizeContents() { 15580b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE 15610b57cec5SDimitry Andric // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that 15620b57cec5SDimitry Andric // case. 15630b57cec5SDimitry Andric if (symTab && symTab->getParent()) 15640b57cec5SDimitry Andric getParent()->link = symTab->getParent()->sectionIndex; 15650b57cec5SDimitry Andric else 15660b57cec5SDimitry Andric getParent()->link = 0; 15670b57cec5SDimitry Andric 15680b57cec5SDimitry Andric if (in.relaPlt == this) 15690b57cec5SDimitry Andric getParent()->info = in.gotPlt->getParent()->sectionIndex; 15700b57cec5SDimitry Andric if (in.relaIplt == this) 15710b57cec5SDimitry Andric getParent()->info = in.igotPlt->getParent()->sectionIndex; 15720b57cec5SDimitry Andric } 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric RelrBaseSection::RelrBaseSection() 15750b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, 15760b57cec5SDimitry Andric config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR, 15770b57cec5SDimitry Andric config->wordsize, ".relr.dyn") {} 15780b57cec5SDimitry Andric 15790b57cec5SDimitry Andric template <class ELFT> 15800b57cec5SDimitry Andric static void encodeDynamicReloc(SymbolTableBaseSection *symTab, 15810b57cec5SDimitry Andric typename ELFT::Rela *p, 15820b57cec5SDimitry Andric const DynamicReloc &rel) { 15830b57cec5SDimitry Andric if (config->isRela) 15840b57cec5SDimitry Andric p->r_addend = rel.computeAddend(); 15850b57cec5SDimitry Andric p->r_offset = rel.getOffset(); 15860b57cec5SDimitry Andric p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL); 15870b57cec5SDimitry Andric } 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric template <class ELFT> 15900b57cec5SDimitry Andric RelocationSection<ELFT>::RelocationSection(StringRef name, bool sort) 15910b57cec5SDimitry Andric : RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL, 15920b57cec5SDimitry Andric config->isRela ? DT_RELA : DT_REL, 15930b57cec5SDimitry Andric config->isRela ? DT_RELASZ : DT_RELSZ), 15940b57cec5SDimitry Andric sort(sort) { 15950b57cec5SDimitry Andric this->entsize = config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric 15980b57cec5SDimitry Andric template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) { 15990b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to 16020b57cec5SDimitry Andric // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset 16030b57cec5SDimitry Andric // is to make results easier to read. 16040b57cec5SDimitry Andric if (sort) 16050b57cec5SDimitry Andric llvm::stable_sort( 16060b57cec5SDimitry Andric relocs, [&](const DynamicReloc &a, const DynamicReloc &b) { 16070b57cec5SDimitry Andric return std::make_tuple(a.type != target->relativeRel, 16080b57cec5SDimitry Andric a.getSymIndex(symTab), a.getOffset()) < 16090b57cec5SDimitry Andric std::make_tuple(b.type != target->relativeRel, 16100b57cec5SDimitry Andric b.getSymIndex(symTab), b.getOffset()); 16110b57cec5SDimitry Andric }); 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 16140b57cec5SDimitry Andric encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel); 16150b57cec5SDimitry Andric buf += config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 16160b57cec5SDimitry Andric } 16170b57cec5SDimitry Andric } 16180b57cec5SDimitry Andric 16190b57cec5SDimitry Andric template <class ELFT> 16200b57cec5SDimitry Andric AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection( 16210b57cec5SDimitry Andric StringRef name) 16220b57cec5SDimitry Andric : RelocationBaseSection( 16230b57cec5SDimitry Andric name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL, 16240b57cec5SDimitry Andric config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL, 16250b57cec5SDimitry Andric config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) { 16260b57cec5SDimitry Andric this->entsize = 1; 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric 16290b57cec5SDimitry Andric template <class ELFT> 16300b57cec5SDimitry Andric bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() { 16310b57cec5SDimitry Andric // This function computes the contents of an Android-format packed relocation 16320b57cec5SDimitry Andric // section. 16330b57cec5SDimitry Andric // 16340b57cec5SDimitry Andric // This format compresses relocations by using relocation groups to factor out 16350b57cec5SDimitry Andric // fields that are common between relocations and storing deltas from previous 16360b57cec5SDimitry Andric // relocations in SLEB128 format (which has a short representation for small 16370b57cec5SDimitry Andric // numbers). A good example of a relocation type with common fields is 16380b57cec5SDimitry Andric // R_*_RELATIVE, which is normally used to represent function pointers in 16390b57cec5SDimitry Andric // vtables. In the REL format, each relative relocation has the same r_info 16400b57cec5SDimitry Andric // field, and is only different from other relative relocations in terms of 16410b57cec5SDimitry Andric // the r_offset field. By sorting relocations by offset, grouping them by 16420b57cec5SDimitry Andric // r_info and representing each relocation with only the delta from the 16430b57cec5SDimitry Andric // previous offset, each 8-byte relocation can be compressed to as little as 1 16440b57cec5SDimitry Andric // byte (or less with run-length encoding). This relocation packer was able to 16450b57cec5SDimitry Andric // reduce the size of the relocation section in an Android Chromium DSO from 16460b57cec5SDimitry Andric // 2,911,184 bytes to 174,693 bytes, or 6% of the original size. 16470b57cec5SDimitry Andric // 16480b57cec5SDimitry Andric // A relocation section consists of a header containing the literal bytes 16490b57cec5SDimitry Andric // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two 16500b57cec5SDimitry Andric // elements are the total number of relocations in the section and an initial 16510b57cec5SDimitry Andric // r_offset value. The remaining elements define a sequence of relocation 16520b57cec5SDimitry Andric // groups. Each relocation group starts with a header consisting of the 16530b57cec5SDimitry Andric // following elements: 16540b57cec5SDimitry Andric // 16550b57cec5SDimitry Andric // - the number of relocations in the relocation group 16560b57cec5SDimitry Andric // - flags for the relocation group 16570b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta 16580b57cec5SDimitry Andric // for each relocation in the group. 16590b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info 16600b57cec5SDimitry Andric // field for each relocation in the group. 16610b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and 16620b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for 16630b57cec5SDimitry Andric // each relocation in the group. 16640b57cec5SDimitry Andric // 16650b57cec5SDimitry Andric // Following the relocation group header are descriptions of each of the 16660b57cec5SDimitry Andric // relocations in the group. They consist of the following elements: 16670b57cec5SDimitry Andric // 16680b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset 16690b57cec5SDimitry Andric // delta for this relocation. 16700b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info 16710b57cec5SDimitry Andric // field for this relocation. 16720b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and 16730b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for 16740b57cec5SDimitry Andric // this relocation. 16750b57cec5SDimitry Andric 16760b57cec5SDimitry Andric size_t oldSize = relocData.size(); 16770b57cec5SDimitry Andric 16780b57cec5SDimitry Andric relocData = {'A', 'P', 'S', '2'}; 16790b57cec5SDimitry Andric raw_svector_ostream os(relocData); 16800b57cec5SDimitry Andric auto add = [&](int64_t v) { encodeSLEB128(v, os); }; 16810b57cec5SDimitry Andric 16820b57cec5SDimitry Andric // The format header includes the number of relocations and the initial 16830b57cec5SDimitry Andric // offset (we set this to zero because the first relocation group will 16840b57cec5SDimitry Andric // perform the initial adjustment). 16850b57cec5SDimitry Andric add(relocs.size()); 16860b57cec5SDimitry Andric add(0); 16870b57cec5SDimitry Andric 16880b57cec5SDimitry Andric std::vector<Elf_Rela> relatives, nonRelatives; 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 16910b57cec5SDimitry Andric Elf_Rela r; 16920b57cec5SDimitry Andric encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel); 16930b57cec5SDimitry Andric 16940b57cec5SDimitry Andric if (r.getType(config->isMips64EL) == target->relativeRel) 16950b57cec5SDimitry Andric relatives.push_back(r); 16960b57cec5SDimitry Andric else 16970b57cec5SDimitry Andric nonRelatives.push_back(r); 16980b57cec5SDimitry Andric } 16990b57cec5SDimitry Andric 17000b57cec5SDimitry Andric llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) { 17010b57cec5SDimitry Andric return a.r_offset < b.r_offset; 17020b57cec5SDimitry Andric }); 17030b57cec5SDimitry Andric 17040b57cec5SDimitry Andric // Try to find groups of relative relocations which are spaced one word 17050b57cec5SDimitry Andric // apart from one another. These generally correspond to vtable entries. The 17060b57cec5SDimitry Andric // format allows these groups to be encoded using a sort of run-length 17070b57cec5SDimitry Andric // encoding, but each group will cost 7 bytes in addition to the offset from 17080b57cec5SDimitry Andric // the previous group, so it is only profitable to do this for groups of 17090b57cec5SDimitry Andric // size 8 or larger. 17100b57cec5SDimitry Andric std::vector<Elf_Rela> ungroupedRelatives; 17110b57cec5SDimitry Andric std::vector<std::vector<Elf_Rela>> relativeGroups; 17120b57cec5SDimitry Andric for (auto i = relatives.begin(), e = relatives.end(); i != e;) { 17130b57cec5SDimitry Andric std::vector<Elf_Rela> group; 17140b57cec5SDimitry Andric do { 17150b57cec5SDimitry Andric group.push_back(*i++); 17160b57cec5SDimitry Andric } while (i != e && (i - 1)->r_offset + config->wordsize == i->r_offset); 17170b57cec5SDimitry Andric 17180b57cec5SDimitry Andric if (group.size() < 8) 17190b57cec5SDimitry Andric ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(), 17200b57cec5SDimitry Andric group.end()); 17210b57cec5SDimitry Andric else 17220b57cec5SDimitry Andric relativeGroups.emplace_back(std::move(group)); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 1725*85868e8aSDimitry Andric // For non-relative relocations, we would like to: 1726*85868e8aSDimitry Andric // 1. Have relocations with the same symbol offset to be consecutive, so 1727*85868e8aSDimitry Andric // that the runtime linker can speed-up symbol lookup by implementing an 1728*85868e8aSDimitry Andric // 1-entry cache. 1729*85868e8aSDimitry Andric // 2. Group relocations by r_info to reduce the size of the relocation 1730*85868e8aSDimitry Andric // section. 1731*85868e8aSDimitry Andric // Since the symbol offset is the high bits in r_info, sorting by r_info 1732*85868e8aSDimitry Andric // allows us to do both. 1733*85868e8aSDimitry Andric // 1734*85868e8aSDimitry Andric // For Rela, we also want to sort by r_addend when r_info is the same. This 1735*85868e8aSDimitry Andric // enables us to group by r_addend as well. 1736*85868e8aSDimitry Andric llvm::stable_sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) { 1737*85868e8aSDimitry Andric if (a.r_info != b.r_info) 1738*85868e8aSDimitry Andric return a.r_info < b.r_info; 1739*85868e8aSDimitry Andric if (config->isRela) 1740*85868e8aSDimitry Andric return a.r_addend < b.r_addend; 1741*85868e8aSDimitry Andric return false; 1742*85868e8aSDimitry Andric }); 1743*85868e8aSDimitry Andric 1744*85868e8aSDimitry Andric // Group relocations with the same r_info. Note that each group emits a group 1745*85868e8aSDimitry Andric // header and that may make the relocation section larger. It is hard to 1746*85868e8aSDimitry Andric // estimate the size of a group header as the encoded size of that varies 1747*85868e8aSDimitry Andric // based on r_info. However, we can approximate this trade-off by the number 1748*85868e8aSDimitry Andric // of values encoded. Each group header contains 3 values, and each relocation 1749*85868e8aSDimitry Andric // in a group encodes one less value, as compared to when it is not grouped. 1750*85868e8aSDimitry Andric // Therefore, we only group relocations if there are 3 or more of them with 1751*85868e8aSDimitry Andric // the same r_info. 1752*85868e8aSDimitry Andric // 1753*85868e8aSDimitry Andric // For Rela, the addend for most non-relative relocations is zero, and thus we 1754*85868e8aSDimitry Andric // can usually get a smaller relocation section if we group relocations with 0 1755*85868e8aSDimitry Andric // addend as well. 1756*85868e8aSDimitry Andric std::vector<Elf_Rela> ungroupedNonRelatives; 1757*85868e8aSDimitry Andric std::vector<std::vector<Elf_Rela>> nonRelativeGroups; 1758*85868e8aSDimitry Andric for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) { 1759*85868e8aSDimitry Andric auto j = i + 1; 1760*85868e8aSDimitry Andric while (j != e && i->r_info == j->r_info && 1761*85868e8aSDimitry Andric (!config->isRela || i->r_addend == j->r_addend)) 1762*85868e8aSDimitry Andric ++j; 1763*85868e8aSDimitry Andric if (j - i < 3 || (config->isRela && i->r_addend != 0)) 1764*85868e8aSDimitry Andric ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j); 1765*85868e8aSDimitry Andric else 1766*85868e8aSDimitry Andric nonRelativeGroups.emplace_back(i, j); 1767*85868e8aSDimitry Andric i = j; 1768*85868e8aSDimitry Andric } 1769*85868e8aSDimitry Andric 1770*85868e8aSDimitry Andric // Sort ungrouped relocations by offset to minimize the encoded length. 1771*85868e8aSDimitry Andric llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) { 1772*85868e8aSDimitry Andric return a.r_offset < b.r_offset; 1773*85868e8aSDimitry Andric }); 1774*85868e8aSDimitry Andric 17750b57cec5SDimitry Andric unsigned hasAddendIfRela = 17760b57cec5SDimitry Andric config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0; 17770b57cec5SDimitry Andric 17780b57cec5SDimitry Andric uint64_t offset = 0; 17790b57cec5SDimitry Andric uint64_t addend = 0; 17800b57cec5SDimitry Andric 17810b57cec5SDimitry Andric // Emit the run-length encoding for the groups of adjacent relative 17820b57cec5SDimitry Andric // relocations. Each group is represented using two groups in the packed 17830b57cec5SDimitry Andric // format. The first is used to set the current offset to the start of the 17840b57cec5SDimitry Andric // group (and also encodes the first relocation), and the second encodes the 17850b57cec5SDimitry Andric // remaining relocations. 17860b57cec5SDimitry Andric for (std::vector<Elf_Rela> &g : relativeGroups) { 17870b57cec5SDimitry Andric // The first relocation in the group. 17880b57cec5SDimitry Andric add(1); 17890b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 17900b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 17910b57cec5SDimitry Andric add(g[0].r_offset - offset); 17920b57cec5SDimitry Andric add(target->relativeRel); 17930b57cec5SDimitry Andric if (config->isRela) { 17940b57cec5SDimitry Andric add(g[0].r_addend - addend); 17950b57cec5SDimitry Andric addend = g[0].r_addend; 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric // The remaining relocations. 17990b57cec5SDimitry Andric add(g.size() - 1); 18000b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 18010b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 18020b57cec5SDimitry Andric add(config->wordsize); 18030b57cec5SDimitry Andric add(target->relativeRel); 18040b57cec5SDimitry Andric if (config->isRela) { 18050b57cec5SDimitry Andric for (auto i = g.begin() + 1, e = g.end(); i != e; ++i) { 18060b57cec5SDimitry Andric add(i->r_addend - addend); 18070b57cec5SDimitry Andric addend = i->r_addend; 18080b57cec5SDimitry Andric } 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric offset = g.back().r_offset; 18120b57cec5SDimitry Andric } 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric // Now the ungrouped relatives. 18150b57cec5SDimitry Andric if (!ungroupedRelatives.empty()) { 18160b57cec5SDimitry Andric add(ungroupedRelatives.size()); 18170b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 18180b57cec5SDimitry Andric add(target->relativeRel); 18190b57cec5SDimitry Andric for (Elf_Rela &r : ungroupedRelatives) { 18200b57cec5SDimitry Andric add(r.r_offset - offset); 18210b57cec5SDimitry Andric offset = r.r_offset; 18220b57cec5SDimitry Andric if (config->isRela) { 18230b57cec5SDimitry Andric add(r.r_addend - addend); 18240b57cec5SDimitry Andric addend = r.r_addend; 18250b57cec5SDimitry Andric } 18260b57cec5SDimitry Andric } 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric 1829*85868e8aSDimitry Andric // Grouped non-relatives. 1830*85868e8aSDimitry Andric for (ArrayRef<Elf_Rela> g : nonRelativeGroups) { 1831*85868e8aSDimitry Andric add(g.size()); 1832*85868e8aSDimitry Andric add(RELOCATION_GROUPED_BY_INFO_FLAG); 1833*85868e8aSDimitry Andric add(g[0].r_info); 1834*85868e8aSDimitry Andric for (const Elf_Rela &r : g) { 1835*85868e8aSDimitry Andric add(r.r_offset - offset); 1836*85868e8aSDimitry Andric offset = r.r_offset; 1837*85868e8aSDimitry Andric } 1838*85868e8aSDimitry Andric addend = 0; 1839*85868e8aSDimitry Andric } 1840*85868e8aSDimitry Andric 1841*85868e8aSDimitry Andric // Finally the ungrouped non-relative relocations. 1842*85868e8aSDimitry Andric if (!ungroupedNonRelatives.empty()) { 1843*85868e8aSDimitry Andric add(ungroupedNonRelatives.size()); 18440b57cec5SDimitry Andric add(hasAddendIfRela); 1845*85868e8aSDimitry Andric for (Elf_Rela &r : ungroupedNonRelatives) { 18460b57cec5SDimitry Andric add(r.r_offset - offset); 18470b57cec5SDimitry Andric offset = r.r_offset; 18480b57cec5SDimitry Andric add(r.r_info); 18490b57cec5SDimitry Andric if (config->isRela) { 18500b57cec5SDimitry Andric add(r.r_addend - addend); 18510b57cec5SDimitry Andric addend = r.r_addend; 18520b57cec5SDimitry Andric } 18530b57cec5SDimitry Andric } 18540b57cec5SDimitry Andric } 18550b57cec5SDimitry Andric 18560b57cec5SDimitry Andric // Don't allow the section to shrink; otherwise the size of the section can 18570b57cec5SDimitry Andric // oscillate infinitely. 18580b57cec5SDimitry Andric if (relocData.size() < oldSize) 18590b57cec5SDimitry Andric relocData.append(oldSize - relocData.size(), 0); 18600b57cec5SDimitry Andric 18610b57cec5SDimitry Andric // Returns whether the section size changed. We need to keep recomputing both 18620b57cec5SDimitry Andric // section layout and the contents of this section until the size converges 18630b57cec5SDimitry Andric // because changing this section's size can affect section layout, which in 18640b57cec5SDimitry Andric // turn can affect the sizes of the LEB-encoded integers stored in this 18650b57cec5SDimitry Andric // section. 18660b57cec5SDimitry Andric return relocData.size() != oldSize; 18670b57cec5SDimitry Andric } 18680b57cec5SDimitry Andric 18690b57cec5SDimitry Andric template <class ELFT> RelrSection<ELFT>::RelrSection() { 18700b57cec5SDimitry Andric this->entsize = config->wordsize; 18710b57cec5SDimitry Andric } 18720b57cec5SDimitry Andric 18730b57cec5SDimitry Andric template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() { 18740b57cec5SDimitry Andric // This function computes the contents of an SHT_RELR packed relocation 18750b57cec5SDimitry Andric // section. 18760b57cec5SDimitry Andric // 18770b57cec5SDimitry Andric // Proposal for adding SHT_RELR sections to generic-abi is here: 18780b57cec5SDimitry Andric // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 18790b57cec5SDimitry Andric // 18800b57cec5SDimitry Andric // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 18810b57cec5SDimitry Andric // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 18820b57cec5SDimitry Andric // 18830b57cec5SDimitry Andric // i.e. start with an address, followed by any number of bitmaps. The address 18840b57cec5SDimitry Andric // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 18850b57cec5SDimitry Andric // relocations each, at subsequent offsets following the last address entry. 18860b57cec5SDimitry Andric // 18870b57cec5SDimitry Andric // The bitmap entries must have 1 in the least significant bit. The assumption 18880b57cec5SDimitry Andric // here is that an address cannot have 1 in lsb. Odd addresses are not 18890b57cec5SDimitry Andric // supported. 18900b57cec5SDimitry Andric // 18910b57cec5SDimitry Andric // Excluding the least significant bit in the bitmap, each non-zero bit in 18920b57cec5SDimitry Andric // the bitmap represents a relocation to be applied to a corresponding machine 18930b57cec5SDimitry Andric // word that follows the base address word. The second least significant bit 18940b57cec5SDimitry Andric // represents the machine word immediately following the initial address, and 18950b57cec5SDimitry Andric // each bit that follows represents the next word, in linear order. As such, 18960b57cec5SDimitry Andric // a single bitmap can encode up to 31 relocations in a 32-bit object, and 18970b57cec5SDimitry Andric // 63 relocations in a 64-bit object. 18980b57cec5SDimitry Andric // 18990b57cec5SDimitry Andric // This encoding has a couple of interesting properties: 19000b57cec5SDimitry Andric // 1. Looking at any entry, it is clear whether it's an address or a bitmap: 19010b57cec5SDimitry Andric // even means address, odd means bitmap. 19020b57cec5SDimitry Andric // 2. Just a simple list of addresses is a valid encoding. 19030b57cec5SDimitry Andric 19040b57cec5SDimitry Andric size_t oldSize = relrRelocs.size(); 19050b57cec5SDimitry Andric relrRelocs.clear(); 19060b57cec5SDimitry Andric 19070b57cec5SDimitry Andric // Same as Config->Wordsize but faster because this is a compile-time 19080b57cec5SDimitry Andric // constant. 19090b57cec5SDimitry Andric const size_t wordsize = sizeof(typename ELFT::uint); 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric // Number of bits to use for the relocation offsets bitmap. 19120b57cec5SDimitry Andric // Must be either 63 or 31. 19130b57cec5SDimitry Andric const size_t nBits = wordsize * 8 - 1; 19140b57cec5SDimitry Andric 19150b57cec5SDimitry Andric // Get offsets for all relative relocations and sort them. 19160b57cec5SDimitry Andric std::vector<uint64_t> offsets; 19170b57cec5SDimitry Andric for (const RelativeReloc &rel : relocs) 19180b57cec5SDimitry Andric offsets.push_back(rel.getOffset()); 19190b57cec5SDimitry Andric llvm::sort(offsets); 19200b57cec5SDimitry Andric 19210b57cec5SDimitry Andric // For each leading relocation, find following ones that can be folded 19220b57cec5SDimitry Andric // as a bitmap and fold them. 19230b57cec5SDimitry Andric for (size_t i = 0, e = offsets.size(); i < e;) { 19240b57cec5SDimitry Andric // Add a leading relocation. 19250b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr(offsets[i])); 19260b57cec5SDimitry Andric uint64_t base = offsets[i] + wordsize; 19270b57cec5SDimitry Andric ++i; 19280b57cec5SDimitry Andric 19290b57cec5SDimitry Andric // Find foldable relocations to construct bitmaps. 19300b57cec5SDimitry Andric while (i < e) { 19310b57cec5SDimitry Andric uint64_t bitmap = 0; 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric while (i < e) { 19340b57cec5SDimitry Andric uint64_t delta = offsets[i] - base; 19350b57cec5SDimitry Andric 19360b57cec5SDimitry Andric // If it is too far, it cannot be folded. 19370b57cec5SDimitry Andric if (delta >= nBits * wordsize) 19380b57cec5SDimitry Andric break; 19390b57cec5SDimitry Andric 19400b57cec5SDimitry Andric // If it is not a multiple of wordsize away, it cannot be folded. 19410b57cec5SDimitry Andric if (delta % wordsize) 19420b57cec5SDimitry Andric break; 19430b57cec5SDimitry Andric 19440b57cec5SDimitry Andric // Fold it. 19450b57cec5SDimitry Andric bitmap |= 1ULL << (delta / wordsize); 19460b57cec5SDimitry Andric ++i; 19470b57cec5SDimitry Andric } 19480b57cec5SDimitry Andric 19490b57cec5SDimitry Andric if (!bitmap) 19500b57cec5SDimitry Andric break; 19510b57cec5SDimitry Andric 19520b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1)); 19530b57cec5SDimitry Andric base += nBits * wordsize; 19540b57cec5SDimitry Andric } 19550b57cec5SDimitry Andric } 19560b57cec5SDimitry Andric 1957*85868e8aSDimitry Andric // Don't allow the section to shrink; otherwise the size of the section can 1958*85868e8aSDimitry Andric // oscillate infinitely. Trailing 1s do not decode to more relocations. 1959*85868e8aSDimitry Andric if (relrRelocs.size() < oldSize) { 1960*85868e8aSDimitry Andric log(".relr.dyn needs " + Twine(oldSize - relrRelocs.size()) + 1961*85868e8aSDimitry Andric " padding word(s)"); 1962*85868e8aSDimitry Andric relrRelocs.resize(oldSize, Elf_Relr(1)); 1963*85868e8aSDimitry Andric } 1964*85868e8aSDimitry Andric 19650b57cec5SDimitry Andric return relrRelocs.size() != oldSize; 19660b57cec5SDimitry Andric } 19670b57cec5SDimitry Andric 19680b57cec5SDimitry Andric SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec) 19690b57cec5SDimitry Andric : SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0, 19700b57cec5SDimitry Andric strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, 19710b57cec5SDimitry Andric config->wordsize, 19720b57cec5SDimitry Andric strTabSec.isDynamic() ? ".dynsym" : ".symtab"), 19730b57cec5SDimitry Andric strTabSec(strTabSec) {} 19740b57cec5SDimitry Andric 19750b57cec5SDimitry Andric // Orders symbols according to their positions in the GOT, 19760b57cec5SDimitry Andric // in compliance with MIPS ABI rules. 19770b57cec5SDimitry Andric // See "Global Offset Table" in Chapter 5 in the following document 19780b57cec5SDimitry Andric // for detailed description: 19790b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 19800b57cec5SDimitry Andric static bool sortMipsSymbols(const SymbolTableEntry &l, 19810b57cec5SDimitry Andric const SymbolTableEntry &r) { 19820b57cec5SDimitry Andric // Sort entries related to non-local preemptible symbols by GOT indexes. 19830b57cec5SDimitry Andric // All other entries go to the beginning of a dynsym in arbitrary order. 19840b57cec5SDimitry Andric if (l.sym->isInGot() && r.sym->isInGot()) 19850b57cec5SDimitry Andric return l.sym->gotIndex < r.sym->gotIndex; 19860b57cec5SDimitry Andric if (!l.sym->isInGot() && !r.sym->isInGot()) 19870b57cec5SDimitry Andric return false; 19880b57cec5SDimitry Andric return !l.sym->isInGot(); 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric 19910b57cec5SDimitry Andric void SymbolTableBaseSection::finalizeContents() { 19920b57cec5SDimitry Andric if (OutputSection *sec = strTabSec.getParent()) 19930b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 19940b57cec5SDimitry Andric 19950b57cec5SDimitry Andric if (this->type != SHT_DYNSYM) { 19960b57cec5SDimitry Andric sortSymTabSymbols(); 19970b57cec5SDimitry Andric return; 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric 20000b57cec5SDimitry Andric // If it is a .dynsym, there should be no local symbols, but we need 20010b57cec5SDimitry Andric // to do a few things for the dynamic linker. 20020b57cec5SDimitry Andric 20030b57cec5SDimitry Andric // Section's Info field has the index of the first non-local symbol. 20040b57cec5SDimitry Andric // Because the first symbol entry is a null entry, 1 is the first. 20050b57cec5SDimitry Andric getParent()->info = 1; 20060b57cec5SDimitry Andric 20070b57cec5SDimitry Andric if (getPartition().gnuHashTab) { 20080b57cec5SDimitry Andric // NB: It also sorts Symbols to meet the GNU hash table requirements. 20090b57cec5SDimitry Andric getPartition().gnuHashTab->addSymbols(symbols); 20100b57cec5SDimitry Andric } else if (config->emachine == EM_MIPS) { 20110b57cec5SDimitry Andric llvm::stable_sort(symbols, sortMipsSymbols); 20120b57cec5SDimitry Andric } 20130b57cec5SDimitry Andric 20140b57cec5SDimitry Andric // Only the main partition's dynsym indexes are stored in the symbols 20150b57cec5SDimitry Andric // themselves. All other partitions use a lookup table. 20160b57cec5SDimitry Andric if (this == mainPart->dynSymTab) { 20170b57cec5SDimitry Andric size_t i = 0; 20180b57cec5SDimitry Andric for (const SymbolTableEntry &s : symbols) 20190b57cec5SDimitry Andric s.sym->dynsymIndex = ++i; 20200b57cec5SDimitry Andric } 20210b57cec5SDimitry Andric } 20220b57cec5SDimitry Andric 20230b57cec5SDimitry Andric // The ELF spec requires that all local symbols precede global symbols, so we 20240b57cec5SDimitry Andric // sort symbol entries in this function. (For .dynsym, we don't do that because 20250b57cec5SDimitry Andric // symbols for dynamic linking are inherently all globals.) 20260b57cec5SDimitry Andric // 20270b57cec5SDimitry Andric // Aside from above, we put local symbols in groups starting with the STT_FILE 20280b57cec5SDimitry Andric // symbol. That is convenient for purpose of identifying where are local symbols 20290b57cec5SDimitry Andric // coming from. 20300b57cec5SDimitry Andric void SymbolTableBaseSection::sortSymTabSymbols() { 20310b57cec5SDimitry Andric // Move all local symbols before global symbols. 20320b57cec5SDimitry Andric auto e = std::stable_partition( 20330b57cec5SDimitry Andric symbols.begin(), symbols.end(), [](const SymbolTableEntry &s) { 20340b57cec5SDimitry Andric return s.sym->isLocal() || s.sym->computeBinding() == STB_LOCAL; 20350b57cec5SDimitry Andric }); 20360b57cec5SDimitry Andric size_t numLocals = e - symbols.begin(); 20370b57cec5SDimitry Andric getParent()->info = numLocals + 1; 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric // We want to group the local symbols by file. For that we rebuild the local 20400b57cec5SDimitry Andric // part of the symbols vector. We do not need to care about the STT_FILE 20410b57cec5SDimitry Andric // symbols, they are already naturally placed first in each group. That 20420b57cec5SDimitry Andric // happens because STT_FILE is always the first symbol in the object and hence 20430b57cec5SDimitry Andric // precede all other local symbols we add for a file. 20440b57cec5SDimitry Andric MapVector<InputFile *, std::vector<SymbolTableEntry>> arr; 20450b57cec5SDimitry Andric for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e)) 20460b57cec5SDimitry Andric arr[s.sym->file].push_back(s); 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric auto i = symbols.begin(); 20490b57cec5SDimitry Andric for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &p : arr) 20500b57cec5SDimitry Andric for (SymbolTableEntry &entry : p.second) 20510b57cec5SDimitry Andric *i++ = entry; 20520b57cec5SDimitry Andric } 20530b57cec5SDimitry Andric 20540b57cec5SDimitry Andric void SymbolTableBaseSection::addSymbol(Symbol *b) { 20550b57cec5SDimitry Andric // Adding a local symbol to a .dynsym is a bug. 20560b57cec5SDimitry Andric assert(this->type != SHT_DYNSYM || !b->isLocal()); 20570b57cec5SDimitry Andric 20580b57cec5SDimitry Andric bool hashIt = b->isLocal(); 20590b57cec5SDimitry Andric symbols.push_back({b, strTabSec.addString(b->getName(), hashIt)}); 20600b57cec5SDimitry Andric } 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric size_t SymbolTableBaseSection::getSymbolIndex(Symbol *sym) { 20630b57cec5SDimitry Andric if (this == mainPart->dynSymTab) 20640b57cec5SDimitry Andric return sym->dynsymIndex; 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric // Initializes symbol lookup tables lazily. This is used only for -r, 20670b57cec5SDimitry Andric // -emit-relocs and dynsyms in partitions other than the main one. 20680b57cec5SDimitry Andric llvm::call_once(onceFlag, [&] { 20690b57cec5SDimitry Andric symbolIndexMap.reserve(symbols.size()); 20700b57cec5SDimitry Andric size_t i = 0; 20710b57cec5SDimitry Andric for (const SymbolTableEntry &e : symbols) { 20720b57cec5SDimitry Andric if (e.sym->type == STT_SECTION) 20730b57cec5SDimitry Andric sectionIndexMap[e.sym->getOutputSection()] = ++i; 20740b57cec5SDimitry Andric else 20750b57cec5SDimitry Andric symbolIndexMap[e.sym] = ++i; 20760b57cec5SDimitry Andric } 20770b57cec5SDimitry Andric }); 20780b57cec5SDimitry Andric 20790b57cec5SDimitry Andric // Section symbols are mapped based on their output sections 20800b57cec5SDimitry Andric // to maintain their semantics. 20810b57cec5SDimitry Andric if (sym->type == STT_SECTION) 20820b57cec5SDimitry Andric return sectionIndexMap.lookup(sym->getOutputSection()); 20830b57cec5SDimitry Andric return symbolIndexMap.lookup(sym); 20840b57cec5SDimitry Andric } 20850b57cec5SDimitry Andric 20860b57cec5SDimitry Andric template <class ELFT> 20870b57cec5SDimitry Andric SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec) 20880b57cec5SDimitry Andric : SymbolTableBaseSection(strTabSec) { 20890b57cec5SDimitry Andric this->entsize = sizeof(Elf_Sym); 20900b57cec5SDimitry Andric } 20910b57cec5SDimitry Andric 20920b57cec5SDimitry Andric static BssSection *getCommonSec(Symbol *sym) { 20930b57cec5SDimitry Andric if (!config->defineCommon) 20940b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 20950b57cec5SDimitry Andric return dyn_cast_or_null<BssSection>(d->section); 20960b57cec5SDimitry Andric return nullptr; 20970b57cec5SDimitry Andric } 20980b57cec5SDimitry Andric 20990b57cec5SDimitry Andric static uint32_t getSymSectionIndex(Symbol *sym) { 21000b57cec5SDimitry Andric if (getCommonSec(sym)) 21010b57cec5SDimitry Andric return SHN_COMMON; 21020b57cec5SDimitry Andric if (!isa<Defined>(sym) || sym->needsPltAddr) 21030b57cec5SDimitry Andric return SHN_UNDEF; 21040b57cec5SDimitry Andric if (const OutputSection *os = sym->getOutputSection()) 21050b57cec5SDimitry Andric return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX 21060b57cec5SDimitry Andric : os->sectionIndex; 21070b57cec5SDimitry Andric return SHN_ABS; 21080b57cec5SDimitry Andric } 21090b57cec5SDimitry Andric 21100b57cec5SDimitry Andric // Write the internal symbol table contents to the output symbol table. 21110b57cec5SDimitry Andric template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) { 21120b57cec5SDimitry Andric // The first entry is a null entry as per the ELF spec. 21130b57cec5SDimitry Andric memset(buf, 0, sizeof(Elf_Sym)); 21140b57cec5SDimitry Andric buf += sizeof(Elf_Sym); 21150b57cec5SDimitry Andric 21160b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 21170b57cec5SDimitry Andric 21180b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 21190b57cec5SDimitry Andric Symbol *sym = ent.sym; 21200b57cec5SDimitry Andric bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition; 21210b57cec5SDimitry Andric 21220b57cec5SDimitry Andric // Set st_info and st_other. 21230b57cec5SDimitry Andric eSym->st_other = 0; 21240b57cec5SDimitry Andric if (sym->isLocal()) { 21250b57cec5SDimitry Andric eSym->setBindingAndType(STB_LOCAL, sym->type); 21260b57cec5SDimitry Andric } else { 21270b57cec5SDimitry Andric eSym->setBindingAndType(sym->computeBinding(), sym->type); 21280b57cec5SDimitry Andric eSym->setVisibility(sym->visibility); 21290b57cec5SDimitry Andric } 21300b57cec5SDimitry Andric 21310b57cec5SDimitry Andric // The 3 most significant bits of st_other are used by OpenPOWER ABI. 21320b57cec5SDimitry Andric // See getPPC64GlobalEntryToLocalEntryOffset() for more details. 21330b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 21340b57cec5SDimitry Andric eSym->st_other |= sym->stOther & 0xe0; 21350b57cec5SDimitry Andric 21360b57cec5SDimitry Andric eSym->st_name = ent.strTabOffset; 21370b57cec5SDimitry Andric if (isDefinedHere) 21380b57cec5SDimitry Andric eSym->st_shndx = getSymSectionIndex(ent.sym); 21390b57cec5SDimitry Andric else 21400b57cec5SDimitry Andric eSym->st_shndx = 0; 21410b57cec5SDimitry Andric 21420b57cec5SDimitry Andric // Copy symbol size if it is a defined symbol. st_size is not significant 21430b57cec5SDimitry Andric // for undefined symbols, so whether copying it or not is up to us if that's 21440b57cec5SDimitry Andric // the case. We'll leave it as zero because by not setting a value, we can 21450b57cec5SDimitry Andric // get the exact same outputs for two sets of input files that differ only 21460b57cec5SDimitry Andric // in undefined symbol size in DSOs. 21470b57cec5SDimitry Andric if (eSym->st_shndx == SHN_UNDEF || !isDefinedHere) 21480b57cec5SDimitry Andric eSym->st_size = 0; 21490b57cec5SDimitry Andric else 21500b57cec5SDimitry Andric eSym->st_size = sym->getSize(); 21510b57cec5SDimitry Andric 21520b57cec5SDimitry Andric // st_value is usually an address of a symbol, but that has a 21530b57cec5SDimitry Andric // special meaining for uninstantiated common symbols (this can 21540b57cec5SDimitry Andric // occur if -r is given). 21550b57cec5SDimitry Andric if (BssSection *commonSec = getCommonSec(ent.sym)) 21560b57cec5SDimitry Andric eSym->st_value = commonSec->alignment; 21570b57cec5SDimitry Andric else if (isDefinedHere) 21580b57cec5SDimitry Andric eSym->st_value = sym->getVA(); 21590b57cec5SDimitry Andric else 21600b57cec5SDimitry Andric eSym->st_value = 0; 21610b57cec5SDimitry Andric 21620b57cec5SDimitry Andric ++eSym; 21630b57cec5SDimitry Andric } 21640b57cec5SDimitry Andric 21650b57cec5SDimitry Andric // On MIPS we need to mark symbol which has a PLT entry and requires 21660b57cec5SDimitry Andric // pointer equality by STO_MIPS_PLT flag. That is necessary to help 21670b57cec5SDimitry Andric // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. 21680b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2008-07/txt00000.txt 21690b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 21700b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 21730b57cec5SDimitry Andric Symbol *sym = ent.sym; 21740b57cec5SDimitry Andric if (sym->isInPlt() && sym->needsPltAddr) 21750b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PLT; 21760b57cec5SDimitry Andric if (isMicroMips()) { 21770b57cec5SDimitry Andric // We already set the less-significant bit for symbols 21780b57cec5SDimitry Andric // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT 21790b57cec5SDimitry Andric // records. That allows us to distinguish such symbols in 21800b57cec5SDimitry Andric // the `MIPS<ELFT>::relocateOne()` routine. Now we should 21810b57cec5SDimitry Andric // clear that bit for non-dynamic symbol table, so tools 21820b57cec5SDimitry Andric // like `objdump` will be able to deal with a correct 21830b57cec5SDimitry Andric // symbol position. 21840b57cec5SDimitry Andric if (sym->isDefined() && 21850b57cec5SDimitry Andric ((sym->stOther & STO_MIPS_MICROMIPS) || sym->needsPltAddr)) { 21860b57cec5SDimitry Andric if (!strTabSec.isDynamic()) 21870b57cec5SDimitry Andric eSym->st_value &= ~1; 21880b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_MICROMIPS; 21890b57cec5SDimitry Andric } 21900b57cec5SDimitry Andric } 21910b57cec5SDimitry Andric if (config->relocatable) 21920b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 21930b57cec5SDimitry Andric if (isMipsPIC<ELFT>(d)) 21940b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PIC; 21950b57cec5SDimitry Andric ++eSym; 21960b57cec5SDimitry Andric } 21970b57cec5SDimitry Andric } 21980b57cec5SDimitry Andric } 21990b57cec5SDimitry Andric 22000b57cec5SDimitry Andric SymtabShndxSection::SymtabShndxSection() 22010b57cec5SDimitry Andric : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndx") { 22020b57cec5SDimitry Andric this->entsize = 4; 22030b57cec5SDimitry Andric } 22040b57cec5SDimitry Andric 22050b57cec5SDimitry Andric void SymtabShndxSection::writeTo(uint8_t *buf) { 22060b57cec5SDimitry Andric // We write an array of 32 bit values, where each value has 1:1 association 22070b57cec5SDimitry Andric // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX, 22080b57cec5SDimitry Andric // we need to write actual index, otherwise, we must write SHN_UNDEF(0). 22090b57cec5SDimitry Andric buf += 4; // Ignore .symtab[0] entry. 22100b57cec5SDimitry Andric for (const SymbolTableEntry &entry : in.symTab->getSymbols()) { 22110b57cec5SDimitry Andric if (getSymSectionIndex(entry.sym) == SHN_XINDEX) 22120b57cec5SDimitry Andric write32(buf, entry.sym->getOutputSection()->sectionIndex); 22130b57cec5SDimitry Andric buf += 4; 22140b57cec5SDimitry Andric } 22150b57cec5SDimitry Andric } 22160b57cec5SDimitry Andric 22170b57cec5SDimitry Andric bool SymtabShndxSection::isNeeded() const { 22180b57cec5SDimitry Andric // SHT_SYMTAB can hold symbols with section indices values up to 22190b57cec5SDimitry Andric // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX 22200b57cec5SDimitry Andric // section. Problem is that we reveal the final section indices a bit too 22210b57cec5SDimitry Andric // late, and we do not know them here. For simplicity, we just always create 22220b57cec5SDimitry Andric // a .symtab_shndx section when the amount of output sections is huge. 22230b57cec5SDimitry Andric size_t size = 0; 22240b57cec5SDimitry Andric for (BaseCommand *base : script->sectionCommands) 22250b57cec5SDimitry Andric if (isa<OutputSection>(base)) 22260b57cec5SDimitry Andric ++size; 22270b57cec5SDimitry Andric return size >= SHN_LORESERVE; 22280b57cec5SDimitry Andric } 22290b57cec5SDimitry Andric 22300b57cec5SDimitry Andric void SymtabShndxSection::finalizeContents() { 22310b57cec5SDimitry Andric getParent()->link = in.symTab->getParent()->sectionIndex; 22320b57cec5SDimitry Andric } 22330b57cec5SDimitry Andric 22340b57cec5SDimitry Andric size_t SymtabShndxSection::getSize() const { 22350b57cec5SDimitry Andric return in.symTab->getNumSymbols() * 4; 22360b57cec5SDimitry Andric } 22370b57cec5SDimitry Andric 22380b57cec5SDimitry Andric // .hash and .gnu.hash sections contain on-disk hash tables that map 22390b57cec5SDimitry Andric // symbol names to their dynamic symbol table indices. Their purpose 22400b57cec5SDimitry Andric // is to help the dynamic linker resolve symbols quickly. If ELF files 22410b57cec5SDimitry Andric // don't have them, the dynamic linker has to do linear search on all 22420b57cec5SDimitry Andric // dynamic symbols, which makes programs slower. Therefore, a .hash 22430b57cec5SDimitry Andric // section is added to a DSO by default. A .gnu.hash is added if you 22440b57cec5SDimitry Andric // give the -hash-style=gnu or -hash-style=both option. 22450b57cec5SDimitry Andric // 22460b57cec5SDimitry Andric // The Unix semantics of resolving dynamic symbols is somewhat expensive. 22470b57cec5SDimitry Andric // Each ELF file has a list of DSOs that the ELF file depends on and a 22480b57cec5SDimitry Andric // list of dynamic symbols that need to be resolved from any of the 22490b57cec5SDimitry Andric // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) 22500b57cec5SDimitry Andric // where m is the number of DSOs and n is the number of dynamic 22510b57cec5SDimitry Andric // symbols. For modern large programs, both m and n are large. So 22520b57cec5SDimitry Andric // making each step faster by using hash tables substiantially 22530b57cec5SDimitry Andric // improves time to load programs. 22540b57cec5SDimitry Andric // 22550b57cec5SDimitry Andric // (Note that this is not the only way to design the shared library. 22560b57cec5SDimitry Andric // For instance, the Windows DLL takes a different approach. On 22570b57cec5SDimitry Andric // Windows, each dynamic symbol has a name of DLL from which the symbol 22580b57cec5SDimitry Andric // has to be resolved. That makes the cost of symbol resolution O(n). 22590b57cec5SDimitry Andric // This disables some hacky techniques you can use on Unix such as 22600b57cec5SDimitry Andric // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) 22610b57cec5SDimitry Andric // 22620b57cec5SDimitry Andric // Due to historical reasons, we have two different hash tables, .hash 22630b57cec5SDimitry Andric // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new 22640b57cec5SDimitry Andric // and better version of .hash. .hash is just an on-disk hash table, but 22650b57cec5SDimitry Andric // .gnu.hash has a bloom filter in addition to a hash table to skip 22660b57cec5SDimitry Andric // DSOs very quickly. If you are sure that your dynamic linker knows 22670b57cec5SDimitry Andric // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a 22680b57cec5SDimitry Andric // safe bet is to specify -hash-style=both for backward compatibilty. 22690b57cec5SDimitry Andric GnuHashTableSection::GnuHashTableSection() 22700b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, config->wordsize, ".gnu.hash") { 22710b57cec5SDimitry Andric } 22720b57cec5SDimitry Andric 22730b57cec5SDimitry Andric void GnuHashTableSection::finalizeContents() { 22740b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynSymTab->getParent()) 22750b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 22760b57cec5SDimitry Andric 22770b57cec5SDimitry Andric // Computes bloom filter size in word size. We want to allocate 12 22780b57cec5SDimitry Andric // bits for each symbol. It must be a power of two. 22790b57cec5SDimitry Andric if (symbols.empty()) { 22800b57cec5SDimitry Andric maskWords = 1; 22810b57cec5SDimitry Andric } else { 22820b57cec5SDimitry Andric uint64_t numBits = symbols.size() * 12; 22830b57cec5SDimitry Andric maskWords = NextPowerOf2(numBits / (config->wordsize * 8)); 22840b57cec5SDimitry Andric } 22850b57cec5SDimitry Andric 22860b57cec5SDimitry Andric size = 16; // Header 22870b57cec5SDimitry Andric size += config->wordsize * maskWords; // Bloom filter 22880b57cec5SDimitry Andric size += nBuckets * 4; // Hash buckets 22890b57cec5SDimitry Andric size += symbols.size() * 4; // Hash values 22900b57cec5SDimitry Andric } 22910b57cec5SDimitry Andric 22920b57cec5SDimitry Andric void GnuHashTableSection::writeTo(uint8_t *buf) { 22930b57cec5SDimitry Andric // The output buffer is not guaranteed to be zero-cleared because we pre- 22940b57cec5SDimitry Andric // fill executable sections with trap instructions. This is a precaution 22950b57cec5SDimitry Andric // for that case, which happens only when -no-rosegment is given. 22960b57cec5SDimitry Andric memset(buf, 0, size); 22970b57cec5SDimitry Andric 22980b57cec5SDimitry Andric // Write a header. 22990b57cec5SDimitry Andric write32(buf, nBuckets); 23000b57cec5SDimitry Andric write32(buf + 4, getPartition().dynSymTab->getNumSymbols() - symbols.size()); 23010b57cec5SDimitry Andric write32(buf + 8, maskWords); 23020b57cec5SDimitry Andric write32(buf + 12, Shift2); 23030b57cec5SDimitry Andric buf += 16; 23040b57cec5SDimitry Andric 23050b57cec5SDimitry Andric // Write a bloom filter and a hash table. 23060b57cec5SDimitry Andric writeBloomFilter(buf); 23070b57cec5SDimitry Andric buf += config->wordsize * maskWords; 23080b57cec5SDimitry Andric writeHashTable(buf); 23090b57cec5SDimitry Andric } 23100b57cec5SDimitry Andric 23110b57cec5SDimitry Andric // This function writes a 2-bit bloom filter. This bloom filter alone 23120b57cec5SDimitry Andric // usually filters out 80% or more of all symbol lookups [1]. 23130b57cec5SDimitry Andric // The dynamic linker uses the hash table only when a symbol is not 23140b57cec5SDimitry Andric // filtered out by a bloom filter. 23150b57cec5SDimitry Andric // 23160b57cec5SDimitry Andric // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2), 23170b57cec5SDimitry Andric // p.9, https://www.akkadia.org/drepper/dsohowto.pdf 23180b57cec5SDimitry Andric void GnuHashTableSection::writeBloomFilter(uint8_t *buf) { 23190b57cec5SDimitry Andric unsigned c = config->is64 ? 64 : 32; 23200b57cec5SDimitry Andric for (const Entry &sym : symbols) { 23210b57cec5SDimitry Andric // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in 23220b57cec5SDimitry Andric // the word using bits [0:5] and [26:31]. 23230b57cec5SDimitry Andric size_t i = (sym.hash / c) & (maskWords - 1); 23240b57cec5SDimitry Andric uint64_t val = readUint(buf + i * config->wordsize); 23250b57cec5SDimitry Andric val |= uint64_t(1) << (sym.hash % c); 23260b57cec5SDimitry Andric val |= uint64_t(1) << ((sym.hash >> Shift2) % c); 23270b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, val); 23280b57cec5SDimitry Andric } 23290b57cec5SDimitry Andric } 23300b57cec5SDimitry Andric 23310b57cec5SDimitry Andric void GnuHashTableSection::writeHashTable(uint8_t *buf) { 23320b57cec5SDimitry Andric uint32_t *buckets = reinterpret_cast<uint32_t *>(buf); 23330b57cec5SDimitry Andric uint32_t oldBucket = -1; 23340b57cec5SDimitry Andric uint32_t *values = buckets + nBuckets; 23350b57cec5SDimitry Andric for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) { 23360b57cec5SDimitry Andric // Write a hash value. It represents a sequence of chains that share the 23370b57cec5SDimitry Andric // same hash modulo value. The last element of each chain is terminated by 23380b57cec5SDimitry Andric // LSB 1. 23390b57cec5SDimitry Andric uint32_t hash = i->hash; 23400b57cec5SDimitry Andric bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx; 23410b57cec5SDimitry Andric hash = isLastInChain ? hash | 1 : hash & ~1; 23420b57cec5SDimitry Andric write32(values++, hash); 23430b57cec5SDimitry Andric 23440b57cec5SDimitry Andric if (i->bucketIdx == oldBucket) 23450b57cec5SDimitry Andric continue; 23460b57cec5SDimitry Andric // Write a hash bucket. Hash buckets contain indices in the following hash 23470b57cec5SDimitry Andric // value table. 23480b57cec5SDimitry Andric write32(buckets + i->bucketIdx, 23490b57cec5SDimitry Andric getPartition().dynSymTab->getSymbolIndex(i->sym)); 23500b57cec5SDimitry Andric oldBucket = i->bucketIdx; 23510b57cec5SDimitry Andric } 23520b57cec5SDimitry Andric } 23530b57cec5SDimitry Andric 23540b57cec5SDimitry Andric static uint32_t hashGnu(StringRef name) { 23550b57cec5SDimitry Andric uint32_t h = 5381; 23560b57cec5SDimitry Andric for (uint8_t c : name) 23570b57cec5SDimitry Andric h = (h << 5) + h + c; 23580b57cec5SDimitry Andric return h; 23590b57cec5SDimitry Andric } 23600b57cec5SDimitry Andric 23610b57cec5SDimitry Andric // Add symbols to this symbol hash table. Note that this function 23620b57cec5SDimitry Andric // destructively sort a given vector -- which is needed because 23630b57cec5SDimitry Andric // GNU-style hash table places some sorting requirements. 23640b57cec5SDimitry Andric void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &v) { 23650b57cec5SDimitry Andric // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce 23660b57cec5SDimitry Andric // its type correctly. 23670b57cec5SDimitry Andric std::vector<SymbolTableEntry>::iterator mid = 23680b57cec5SDimitry Andric std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) { 23690b57cec5SDimitry Andric return !s.sym->isDefined() || s.sym->partition != partition; 23700b57cec5SDimitry Andric }); 23710b57cec5SDimitry Andric 23720b57cec5SDimitry Andric // We chose load factor 4 for the on-disk hash table. For each hash 23730b57cec5SDimitry Andric // collision, the dynamic linker will compare a uint32_t hash value. 23740b57cec5SDimitry Andric // Since the integer comparison is quite fast, we believe we can 23750b57cec5SDimitry Andric // make the load factor even larger. 4 is just a conservative choice. 23760b57cec5SDimitry Andric // 23770b57cec5SDimitry Andric // Note that we don't want to create a zero-sized hash table because 23780b57cec5SDimitry Andric // Android loader as of 2018 doesn't like a .gnu.hash containing such 23790b57cec5SDimitry Andric // table. If that's the case, we create a hash table with one unused 23800b57cec5SDimitry Andric // dummy slot. 23810b57cec5SDimitry Andric nBuckets = std::max<size_t>((v.end() - mid) / 4, 1); 23820b57cec5SDimitry Andric 23830b57cec5SDimitry Andric if (mid == v.end()) 23840b57cec5SDimitry Andric return; 23850b57cec5SDimitry Andric 23860b57cec5SDimitry Andric for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) { 23870b57cec5SDimitry Andric Symbol *b = ent.sym; 23880b57cec5SDimitry Andric uint32_t hash = hashGnu(b->getName()); 23890b57cec5SDimitry Andric uint32_t bucketIdx = hash % nBuckets; 23900b57cec5SDimitry Andric symbols.push_back({b, ent.strTabOffset, hash, bucketIdx}); 23910b57cec5SDimitry Andric } 23920b57cec5SDimitry Andric 23930b57cec5SDimitry Andric llvm::stable_sort(symbols, [](const Entry &l, const Entry &r) { 23940b57cec5SDimitry Andric return l.bucketIdx < r.bucketIdx; 23950b57cec5SDimitry Andric }); 23960b57cec5SDimitry Andric 23970b57cec5SDimitry Andric v.erase(mid, v.end()); 23980b57cec5SDimitry Andric for (const Entry &ent : symbols) 23990b57cec5SDimitry Andric v.push_back({ent.sym, ent.strTabOffset}); 24000b57cec5SDimitry Andric } 24010b57cec5SDimitry Andric 24020b57cec5SDimitry Andric HashTableSection::HashTableSection() 24030b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { 24040b57cec5SDimitry Andric this->entsize = 4; 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric void HashTableSection::finalizeContents() { 24080b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 24090b57cec5SDimitry Andric 24100b57cec5SDimitry Andric if (OutputSection *sec = symTab->getParent()) 24110b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 24120b57cec5SDimitry Andric 24130b57cec5SDimitry Andric unsigned numEntries = 2; // nbucket and nchain. 24140b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); // The chain entries. 24150b57cec5SDimitry Andric 24160b57cec5SDimitry Andric // Create as many buckets as there are symbols. 24170b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); 24180b57cec5SDimitry Andric this->size = numEntries * 4; 24190b57cec5SDimitry Andric } 24200b57cec5SDimitry Andric 24210b57cec5SDimitry Andric void HashTableSection::writeTo(uint8_t *buf) { 24220b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 24230b57cec5SDimitry Andric 24240b57cec5SDimitry Andric // See comment in GnuHashTableSection::writeTo. 24250b57cec5SDimitry Andric memset(buf, 0, size); 24260b57cec5SDimitry Andric 24270b57cec5SDimitry Andric unsigned numSymbols = symTab->getNumSymbols(); 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric uint32_t *p = reinterpret_cast<uint32_t *>(buf); 24300b57cec5SDimitry Andric write32(p++, numSymbols); // nbucket 24310b57cec5SDimitry Andric write32(p++, numSymbols); // nchain 24320b57cec5SDimitry Andric 24330b57cec5SDimitry Andric uint32_t *buckets = p; 24340b57cec5SDimitry Andric uint32_t *chains = p + numSymbols; 24350b57cec5SDimitry Andric 24360b57cec5SDimitry Andric for (const SymbolTableEntry &s : symTab->getSymbols()) { 24370b57cec5SDimitry Andric Symbol *sym = s.sym; 24380b57cec5SDimitry Andric StringRef name = sym->getName(); 24390b57cec5SDimitry Andric unsigned i = sym->dynsymIndex; 24400b57cec5SDimitry Andric uint32_t hash = hashSysV(name) % numSymbols; 24410b57cec5SDimitry Andric chains[i] = buckets[hash]; 24420b57cec5SDimitry Andric write32(buckets + hash, i); 24430b57cec5SDimitry Andric } 24440b57cec5SDimitry Andric } 24450b57cec5SDimitry Andric 24460b57cec5SDimitry Andric // On PowerPC64 the lazy symbol resolvers go into the `global linkage table` 24470b57cec5SDimitry Andric // in the .glink section, rather then the typical .plt section. 24480b57cec5SDimitry Andric PltSection::PltSection(bool isIplt) 24490b57cec5SDimitry Andric : SyntheticSection( 24500b57cec5SDimitry Andric SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, 24510b57cec5SDimitry Andric (config->emachine == EM_PPC || config->emachine == EM_PPC64) 24520b57cec5SDimitry Andric ? ".glink" 24530b57cec5SDimitry Andric : ".plt"), 24540b57cec5SDimitry Andric headerSize(!isIplt || config->zRetpolineplt ? target->pltHeaderSize : 0), 24550b57cec5SDimitry Andric isIplt(isIplt) { 24560b57cec5SDimitry Andric // The PLT needs to be writable on SPARC as the dynamic linker will 24570b57cec5SDimitry Andric // modify the instructions in the PLT entries. 24580b57cec5SDimitry Andric if (config->emachine == EM_SPARCV9) 24590b57cec5SDimitry Andric this->flags |= SHF_WRITE; 24600b57cec5SDimitry Andric } 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric void PltSection::writeTo(uint8_t *buf) { 24630b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 24640b57cec5SDimitry Andric writePPC32GlinkSection(buf, entries.size()); 24650b57cec5SDimitry Andric return; 24660b57cec5SDimitry Andric } 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric // At beginning of PLT or retpoline IPLT, we have code to call the dynamic 24690b57cec5SDimitry Andric // linker to resolve dynsyms at runtime. Write such code. 24700b57cec5SDimitry Andric if (headerSize) 24710b57cec5SDimitry Andric target->writePltHeader(buf); 24720b57cec5SDimitry Andric size_t off = headerSize; 24730b57cec5SDimitry Andric 24740b57cec5SDimitry Andric RelocationBaseSection *relSec = isIplt ? in.relaIplt : in.relaPlt; 24750b57cec5SDimitry Andric 24760b57cec5SDimitry Andric // The IPlt is immediately after the Plt, account for this in relOff 24770b57cec5SDimitry Andric size_t pltOff = isIplt ? in.plt->getSize() : 0; 24780b57cec5SDimitry Andric 24790b57cec5SDimitry Andric for (size_t i = 0, e = entries.size(); i != e; ++i) { 24800b57cec5SDimitry Andric const Symbol *b = entries[i]; 24810b57cec5SDimitry Andric unsigned relOff = relSec->entsize * i + pltOff; 24820b57cec5SDimitry Andric uint64_t got = b->getGotPltVA(); 24830b57cec5SDimitry Andric uint64_t plt = this->getVA() + off; 24840b57cec5SDimitry Andric target->writePlt(buf + off, got, plt, b->pltIndex, relOff); 24850b57cec5SDimitry Andric off += target->pltEntrySize; 24860b57cec5SDimitry Andric } 24870b57cec5SDimitry Andric } 24880b57cec5SDimitry Andric 24890b57cec5SDimitry Andric template <class ELFT> void PltSection::addEntry(Symbol &sym) { 24900b57cec5SDimitry Andric sym.pltIndex = entries.size(); 24910b57cec5SDimitry Andric entries.push_back(&sym); 24920b57cec5SDimitry Andric } 24930b57cec5SDimitry Andric 24940b57cec5SDimitry Andric size_t PltSection::getSize() const { 24950b57cec5SDimitry Andric return headerSize + entries.size() * target->pltEntrySize; 24960b57cec5SDimitry Andric } 24970b57cec5SDimitry Andric 24980b57cec5SDimitry Andric // Some architectures such as additional symbols in the PLT section. For 24990b57cec5SDimitry Andric // example ARM uses mapping symbols to aid disassembly 25000b57cec5SDimitry Andric void PltSection::addSymbols() { 25010b57cec5SDimitry Andric // The PLT may have symbols defined for the Header, the IPLT has no header 25020b57cec5SDimitry Andric if (!isIplt) 25030b57cec5SDimitry Andric target->addPltHeaderSymbols(*this); 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric size_t off = headerSize; 25060b57cec5SDimitry Andric for (size_t i = 0; i < entries.size(); ++i) { 25070b57cec5SDimitry Andric target->addPltSymbols(*this, off); 25080b57cec5SDimitry Andric off += target->pltEntrySize; 25090b57cec5SDimitry Andric } 25100b57cec5SDimitry Andric } 25110b57cec5SDimitry Andric 25120b57cec5SDimitry Andric // The string hash function for .gdb_index. 25130b57cec5SDimitry Andric static uint32_t computeGdbHash(StringRef s) { 25140b57cec5SDimitry Andric uint32_t h = 0; 25150b57cec5SDimitry Andric for (uint8_t c : s) 25160b57cec5SDimitry Andric h = h * 67 + toLower(c) - 113; 25170b57cec5SDimitry Andric return h; 25180b57cec5SDimitry Andric } 25190b57cec5SDimitry Andric 25200b57cec5SDimitry Andric GdbIndexSection::GdbIndexSection() 25210b57cec5SDimitry Andric : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {} 25220b57cec5SDimitry Andric 25230b57cec5SDimitry Andric // Returns the desired size of an on-disk hash table for a .gdb_index section. 25240b57cec5SDimitry Andric // There's a tradeoff between size and collision rate. We aim 75% utilization. 25250b57cec5SDimitry Andric size_t GdbIndexSection::computeSymtabSize() const { 25260b57cec5SDimitry Andric return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024); 25270b57cec5SDimitry Andric } 25280b57cec5SDimitry Andric 25290b57cec5SDimitry Andric // Compute the output section size. 25300b57cec5SDimitry Andric void GdbIndexSection::initOutputSize() { 25310b57cec5SDimitry Andric size = sizeof(GdbIndexHeader) + computeSymtabSize() * 8; 25320b57cec5SDimitry Andric 25330b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) 25340b57cec5SDimitry Andric size += chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20; 25350b57cec5SDimitry Andric 25360b57cec5SDimitry Andric // Add the constant pool size if exists. 25370b57cec5SDimitry Andric if (!symbols.empty()) { 25380b57cec5SDimitry Andric GdbSymbol &sym = symbols.back(); 25390b57cec5SDimitry Andric size += sym.nameOff + sym.name.size() + 1; 25400b57cec5SDimitry Andric } 25410b57cec5SDimitry Andric } 25420b57cec5SDimitry Andric 25430b57cec5SDimitry Andric static std::vector<InputSection *> getDebugInfoSections() { 25440b57cec5SDimitry Andric std::vector<InputSection *> ret; 25450b57cec5SDimitry Andric for (InputSectionBase *s : inputSections) 25460b57cec5SDimitry Andric if (InputSection *isec = dyn_cast<InputSection>(s)) 25470b57cec5SDimitry Andric if (isec->name == ".debug_info") 25480b57cec5SDimitry Andric ret.push_back(isec); 25490b57cec5SDimitry Andric return ret; 25500b57cec5SDimitry Andric } 25510b57cec5SDimitry Andric 25520b57cec5SDimitry Andric static std::vector<GdbIndexSection::CuEntry> readCuList(DWARFContext &dwarf) { 25530b57cec5SDimitry Andric std::vector<GdbIndexSection::CuEntry> ret; 25540b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) 25550b57cec5SDimitry Andric ret.push_back({cu->getOffset(), cu->getLength() + 4}); 25560b57cec5SDimitry Andric return ret; 25570b57cec5SDimitry Andric } 25580b57cec5SDimitry Andric 25590b57cec5SDimitry Andric static std::vector<GdbIndexSection::AddressEntry> 25600b57cec5SDimitry Andric readAddressAreas(DWARFContext &dwarf, InputSection *sec) { 25610b57cec5SDimitry Andric std::vector<GdbIndexSection::AddressEntry> ret; 25620b57cec5SDimitry Andric 25630b57cec5SDimitry Andric uint32_t cuIdx = 0; 25640b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) { 2565*85868e8aSDimitry Andric if (Error e = cu->tryExtractDIEsIfNeeded(false)) { 2566*85868e8aSDimitry Andric error(toString(sec) + ": " + toString(std::move(e))); 2567*85868e8aSDimitry Andric return {}; 2568*85868e8aSDimitry Andric } 25690b57cec5SDimitry Andric Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges(); 25700b57cec5SDimitry Andric if (!ranges) { 25710b57cec5SDimitry Andric error(toString(sec) + ": " + toString(ranges.takeError())); 25720b57cec5SDimitry Andric return {}; 25730b57cec5SDimitry Andric } 25740b57cec5SDimitry Andric 25750b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = sec->file->getSections(); 25760b57cec5SDimitry Andric for (DWARFAddressRange &r : *ranges) { 25770b57cec5SDimitry Andric if (r.SectionIndex == -1ULL) 25780b57cec5SDimitry Andric continue; 25790b57cec5SDimitry Andric InputSectionBase *s = sections[r.SectionIndex]; 25800b57cec5SDimitry Andric if (!s || s == &InputSection::discarded || !s->isLive()) 25810b57cec5SDimitry Andric continue; 25820b57cec5SDimitry Andric // Range list with zero size has no effect. 25830b57cec5SDimitry Andric if (r.LowPC == r.HighPC) 25840b57cec5SDimitry Andric continue; 25850b57cec5SDimitry Andric auto *isec = cast<InputSection>(s); 25860b57cec5SDimitry Andric uint64_t offset = isec->getOffsetInFile(); 25870b57cec5SDimitry Andric ret.push_back({isec, r.LowPC - offset, r.HighPC - offset, cuIdx}); 25880b57cec5SDimitry Andric } 25890b57cec5SDimitry Andric ++cuIdx; 25900b57cec5SDimitry Andric } 25910b57cec5SDimitry Andric 25920b57cec5SDimitry Andric return ret; 25930b57cec5SDimitry Andric } 25940b57cec5SDimitry Andric 25950b57cec5SDimitry Andric template <class ELFT> 25960b57cec5SDimitry Andric static std::vector<GdbIndexSection::NameAttrEntry> 25970b57cec5SDimitry Andric readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj, 2598*85868e8aSDimitry Andric const std::vector<GdbIndexSection::CuEntry> &cus) { 2599*85868e8aSDimitry Andric const DWARFSection &pubNames = obj.getGnuPubnamesSection(); 2600*85868e8aSDimitry Andric const DWARFSection &pubTypes = obj.getGnuPubtypesSection(); 26010b57cec5SDimitry Andric 26020b57cec5SDimitry Andric std::vector<GdbIndexSection::NameAttrEntry> ret; 26030b57cec5SDimitry Andric for (const DWARFSection *pub : {&pubNames, &pubTypes}) { 26040b57cec5SDimitry Andric DWARFDebugPubTable table(obj, *pub, config->isLE, true); 26050b57cec5SDimitry Andric for (const DWARFDebugPubTable::Set &set : table.getData()) { 26060b57cec5SDimitry Andric // The value written into the constant pool is kind << 24 | cuIndex. As we 26070b57cec5SDimitry Andric // don't know how many compilation units precede this object to compute 26080b57cec5SDimitry Andric // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add 26090b57cec5SDimitry Andric // the number of preceding compilation units later. 2610*85868e8aSDimitry Andric uint32_t i = llvm::partition_point(cus, 2611*85868e8aSDimitry Andric [&](GdbIndexSection::CuEntry cu) { 2612*85868e8aSDimitry Andric return cu.cuOffset < set.Offset; 26130b57cec5SDimitry Andric }) - 2614*85868e8aSDimitry Andric cus.begin(); 26150b57cec5SDimitry Andric for (const DWARFDebugPubTable::Entry &ent : set.Entries) 26160b57cec5SDimitry Andric ret.push_back({{ent.Name, computeGdbHash(ent.Name)}, 26170b57cec5SDimitry Andric (ent.Descriptor.toBits() << 24) | i}); 26180b57cec5SDimitry Andric } 26190b57cec5SDimitry Andric } 26200b57cec5SDimitry Andric return ret; 26210b57cec5SDimitry Andric } 26220b57cec5SDimitry Andric 26230b57cec5SDimitry Andric // Create a list of symbols from a given list of symbol names and types 26240b57cec5SDimitry Andric // by uniquifying them by name. 26250b57cec5SDimitry Andric static std::vector<GdbIndexSection::GdbSymbol> 26260b57cec5SDimitry Andric createSymbols(ArrayRef<std::vector<GdbIndexSection::NameAttrEntry>> nameAttrs, 26270b57cec5SDimitry Andric const std::vector<GdbIndexSection::GdbChunk> &chunks) { 26280b57cec5SDimitry Andric using GdbSymbol = GdbIndexSection::GdbSymbol; 26290b57cec5SDimitry Andric using NameAttrEntry = GdbIndexSection::NameAttrEntry; 26300b57cec5SDimitry Andric 26310b57cec5SDimitry Andric // For each chunk, compute the number of compilation units preceding it. 26320b57cec5SDimitry Andric uint32_t cuIdx = 0; 26330b57cec5SDimitry Andric std::vector<uint32_t> cuIdxs(chunks.size()); 26340b57cec5SDimitry Andric for (uint32_t i = 0, e = chunks.size(); i != e; ++i) { 26350b57cec5SDimitry Andric cuIdxs[i] = cuIdx; 26360b57cec5SDimitry Andric cuIdx += chunks[i].compilationUnits.size(); 26370b57cec5SDimitry Andric } 26380b57cec5SDimitry Andric 26390b57cec5SDimitry Andric // The number of symbols we will handle in this function is of the order 26400b57cec5SDimitry Andric // of millions for very large executables, so we use multi-threading to 26410b57cec5SDimitry Andric // speed it up. 26420b57cec5SDimitry Andric size_t numShards = 32; 26430b57cec5SDimitry Andric size_t concurrency = 1; 26440b57cec5SDimitry Andric if (threadsEnabled) 26450b57cec5SDimitry Andric concurrency = 26460b57cec5SDimitry Andric std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards); 26470b57cec5SDimitry Andric 26480b57cec5SDimitry Andric // A sharded map to uniquify symbols by name. 26490b57cec5SDimitry Andric std::vector<DenseMap<CachedHashStringRef, size_t>> map(numShards); 26500b57cec5SDimitry Andric size_t shift = 32 - countTrailingZeros(numShards); 26510b57cec5SDimitry Andric 26520b57cec5SDimitry Andric // Instantiate GdbSymbols while uniqufying them by name. 26530b57cec5SDimitry Andric std::vector<std::vector<GdbSymbol>> symbols(numShards); 26540b57cec5SDimitry Andric parallelForEachN(0, concurrency, [&](size_t threadId) { 26550b57cec5SDimitry Andric uint32_t i = 0; 26560b57cec5SDimitry Andric for (ArrayRef<NameAttrEntry> entries : nameAttrs) { 26570b57cec5SDimitry Andric for (const NameAttrEntry &ent : entries) { 26580b57cec5SDimitry Andric size_t shardId = ent.name.hash() >> shift; 26590b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) != threadId) 26600b57cec5SDimitry Andric continue; 26610b57cec5SDimitry Andric 26620b57cec5SDimitry Andric uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i]; 26630b57cec5SDimitry Andric size_t &idx = map[shardId][ent.name]; 26640b57cec5SDimitry Andric if (idx) { 26650b57cec5SDimitry Andric symbols[shardId][idx - 1].cuVector.push_back(v); 26660b57cec5SDimitry Andric continue; 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric 26690b57cec5SDimitry Andric idx = symbols[shardId].size() + 1; 26700b57cec5SDimitry Andric symbols[shardId].push_back({ent.name, {v}, 0, 0}); 26710b57cec5SDimitry Andric } 26720b57cec5SDimitry Andric ++i; 26730b57cec5SDimitry Andric } 26740b57cec5SDimitry Andric }); 26750b57cec5SDimitry Andric 26760b57cec5SDimitry Andric size_t numSymbols = 0; 26770b57cec5SDimitry Andric for (ArrayRef<GdbSymbol> v : symbols) 26780b57cec5SDimitry Andric numSymbols += v.size(); 26790b57cec5SDimitry Andric 26800b57cec5SDimitry Andric // The return type is a flattened vector, so we'll copy each vector 26810b57cec5SDimitry Andric // contents to Ret. 26820b57cec5SDimitry Andric std::vector<GdbSymbol> ret; 26830b57cec5SDimitry Andric ret.reserve(numSymbols); 26840b57cec5SDimitry Andric for (std::vector<GdbSymbol> &vec : symbols) 26850b57cec5SDimitry Andric for (GdbSymbol &sym : vec) 26860b57cec5SDimitry Andric ret.push_back(std::move(sym)); 26870b57cec5SDimitry Andric 26880b57cec5SDimitry Andric // CU vectors and symbol names are adjacent in the output file. 26890b57cec5SDimitry Andric // We can compute their offsets in the output file now. 26900b57cec5SDimitry Andric size_t off = 0; 26910b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 26920b57cec5SDimitry Andric sym.cuVectorOff = off; 26930b57cec5SDimitry Andric off += (sym.cuVector.size() + 1) * 4; 26940b57cec5SDimitry Andric } 26950b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 26960b57cec5SDimitry Andric sym.nameOff = off; 26970b57cec5SDimitry Andric off += sym.name.size() + 1; 26980b57cec5SDimitry Andric } 26990b57cec5SDimitry Andric 27000b57cec5SDimitry Andric return ret; 27010b57cec5SDimitry Andric } 27020b57cec5SDimitry Andric 27030b57cec5SDimitry Andric // Returns a newly-created .gdb_index section. 27040b57cec5SDimitry Andric template <class ELFT> GdbIndexSection *GdbIndexSection::create() { 27050b57cec5SDimitry Andric std::vector<InputSection *> sections = getDebugInfoSections(); 27060b57cec5SDimitry Andric 27070b57cec5SDimitry Andric // .debug_gnu_pub{names,types} are useless in executables. 27080b57cec5SDimitry Andric // They are present in input object files solely for creating 27090b57cec5SDimitry Andric // a .gdb_index. So we can remove them from the output. 27100b57cec5SDimitry Andric for (InputSectionBase *s : inputSections) 27110b57cec5SDimitry Andric if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes") 27120b57cec5SDimitry Andric s->markDead(); 27130b57cec5SDimitry Andric 27140b57cec5SDimitry Andric std::vector<GdbChunk> chunks(sections.size()); 27150b57cec5SDimitry Andric std::vector<std::vector<NameAttrEntry>> nameAttrs(sections.size()); 27160b57cec5SDimitry Andric 27170b57cec5SDimitry Andric parallelForEachN(0, sections.size(), [&](size_t i) { 27180b57cec5SDimitry Andric ObjFile<ELFT> *file = sections[i]->getFile<ELFT>(); 2719*85868e8aSDimitry Andric DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file)); 27200b57cec5SDimitry Andric 27210b57cec5SDimitry Andric chunks[i].sec = sections[i]; 27220b57cec5SDimitry Andric chunks[i].compilationUnits = readCuList(dwarf); 27230b57cec5SDimitry Andric chunks[i].addressAreas = readAddressAreas(dwarf, sections[i]); 27240b57cec5SDimitry Andric nameAttrs[i] = readPubNamesAndTypes<ELFT>( 27250b57cec5SDimitry Andric static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj()), 27260b57cec5SDimitry Andric chunks[i].compilationUnits); 27270b57cec5SDimitry Andric }); 27280b57cec5SDimitry Andric 27290b57cec5SDimitry Andric auto *ret = make<GdbIndexSection>(); 27300b57cec5SDimitry Andric ret->chunks = std::move(chunks); 27310b57cec5SDimitry Andric ret->symbols = createSymbols(nameAttrs, ret->chunks); 27320b57cec5SDimitry Andric ret->initOutputSize(); 27330b57cec5SDimitry Andric return ret; 27340b57cec5SDimitry Andric } 27350b57cec5SDimitry Andric 27360b57cec5SDimitry Andric void GdbIndexSection::writeTo(uint8_t *buf) { 27370b57cec5SDimitry Andric // Write the header. 27380b57cec5SDimitry Andric auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf); 27390b57cec5SDimitry Andric uint8_t *start = buf; 27400b57cec5SDimitry Andric hdr->version = 7; 27410b57cec5SDimitry Andric buf += sizeof(*hdr); 27420b57cec5SDimitry Andric 27430b57cec5SDimitry Andric // Write the CU list. 27440b57cec5SDimitry Andric hdr->cuListOff = buf - start; 27450b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 27460b57cec5SDimitry Andric for (CuEntry &cu : chunk.compilationUnits) { 27470b57cec5SDimitry Andric write64le(buf, chunk.sec->outSecOff + cu.cuOffset); 27480b57cec5SDimitry Andric write64le(buf + 8, cu.cuLength); 27490b57cec5SDimitry Andric buf += 16; 27500b57cec5SDimitry Andric } 27510b57cec5SDimitry Andric } 27520b57cec5SDimitry Andric 27530b57cec5SDimitry Andric // Write the address area. 27540b57cec5SDimitry Andric hdr->cuTypesOff = buf - start; 27550b57cec5SDimitry Andric hdr->addressAreaOff = buf - start; 27560b57cec5SDimitry Andric uint32_t cuOff = 0; 27570b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 27580b57cec5SDimitry Andric for (AddressEntry &e : chunk.addressAreas) { 27590b57cec5SDimitry Andric uint64_t baseAddr = e.section->getVA(0); 27600b57cec5SDimitry Andric write64le(buf, baseAddr + e.lowAddress); 27610b57cec5SDimitry Andric write64le(buf + 8, baseAddr + e.highAddress); 27620b57cec5SDimitry Andric write32le(buf + 16, e.cuIndex + cuOff); 27630b57cec5SDimitry Andric buf += 20; 27640b57cec5SDimitry Andric } 27650b57cec5SDimitry Andric cuOff += chunk.compilationUnits.size(); 27660b57cec5SDimitry Andric } 27670b57cec5SDimitry Andric 27680b57cec5SDimitry Andric // Write the on-disk open-addressing hash table containing symbols. 27690b57cec5SDimitry Andric hdr->symtabOff = buf - start; 27700b57cec5SDimitry Andric size_t symtabSize = computeSymtabSize(); 27710b57cec5SDimitry Andric uint32_t mask = symtabSize - 1; 27720b57cec5SDimitry Andric 27730b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 27740b57cec5SDimitry Andric uint32_t h = sym.name.hash(); 27750b57cec5SDimitry Andric uint32_t i = h & mask; 27760b57cec5SDimitry Andric uint32_t step = ((h * 17) & mask) | 1; 27770b57cec5SDimitry Andric 27780b57cec5SDimitry Andric while (read32le(buf + i * 8)) 27790b57cec5SDimitry Andric i = (i + step) & mask; 27800b57cec5SDimitry Andric 27810b57cec5SDimitry Andric write32le(buf + i * 8, sym.nameOff); 27820b57cec5SDimitry Andric write32le(buf + i * 8 + 4, sym.cuVectorOff); 27830b57cec5SDimitry Andric } 27840b57cec5SDimitry Andric 27850b57cec5SDimitry Andric buf += symtabSize * 8; 27860b57cec5SDimitry Andric 27870b57cec5SDimitry Andric // Write the string pool. 27880b57cec5SDimitry Andric hdr->constantPoolOff = buf - start; 27890b57cec5SDimitry Andric parallelForEach(symbols, [&](GdbSymbol &sym) { 27900b57cec5SDimitry Andric memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size()); 27910b57cec5SDimitry Andric }); 27920b57cec5SDimitry Andric 27930b57cec5SDimitry Andric // Write the CU vectors. 27940b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 27950b57cec5SDimitry Andric write32le(buf, sym.cuVector.size()); 27960b57cec5SDimitry Andric buf += 4; 27970b57cec5SDimitry Andric for (uint32_t val : sym.cuVector) { 27980b57cec5SDimitry Andric write32le(buf, val); 27990b57cec5SDimitry Andric buf += 4; 28000b57cec5SDimitry Andric } 28010b57cec5SDimitry Andric } 28020b57cec5SDimitry Andric } 28030b57cec5SDimitry Andric 28040b57cec5SDimitry Andric bool GdbIndexSection::isNeeded() const { return !chunks.empty(); } 28050b57cec5SDimitry Andric 28060b57cec5SDimitry Andric EhFrameHeader::EhFrameHeader() 28070b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {} 28080b57cec5SDimitry Andric 28090b57cec5SDimitry Andric void EhFrameHeader::writeTo(uint8_t *buf) { 28100b57cec5SDimitry Andric // Unlike most sections, the EhFrameHeader section is written while writing 28110b57cec5SDimitry Andric // another section, namely EhFrameSection, which calls the write() function 28120b57cec5SDimitry Andric // below from its writeTo() function. This is necessary because the contents 28130b57cec5SDimitry Andric // of EhFrameHeader depend on the relocated contents of EhFrameSection and we 28140b57cec5SDimitry Andric // don't know which order the sections will be written in. 28150b57cec5SDimitry Andric } 28160b57cec5SDimitry Andric 28170b57cec5SDimitry Andric // .eh_frame_hdr contains a binary search table of pointers to FDEs. 28180b57cec5SDimitry Andric // Each entry of the search table consists of two values, 28190b57cec5SDimitry Andric // the starting PC from where FDEs covers, and the FDE's address. 28200b57cec5SDimitry Andric // It is sorted by PC. 28210b57cec5SDimitry Andric void EhFrameHeader::write() { 28220b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 28230b57cec5SDimitry Andric using FdeData = EhFrameSection::FdeData; 28240b57cec5SDimitry Andric 28250b57cec5SDimitry Andric std::vector<FdeData> fdes = getPartition().ehFrame->getFdeData(); 28260b57cec5SDimitry Andric 28270b57cec5SDimitry Andric buf[0] = 1; 28280b57cec5SDimitry Andric buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; 28290b57cec5SDimitry Andric buf[2] = DW_EH_PE_udata4; 28300b57cec5SDimitry Andric buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; 28310b57cec5SDimitry Andric write32(buf + 4, 28320b57cec5SDimitry Andric getPartition().ehFrame->getParent()->addr - this->getVA() - 4); 28330b57cec5SDimitry Andric write32(buf + 8, fdes.size()); 28340b57cec5SDimitry Andric buf += 12; 28350b57cec5SDimitry Andric 28360b57cec5SDimitry Andric for (FdeData &fde : fdes) { 28370b57cec5SDimitry Andric write32(buf, fde.pcRel); 28380b57cec5SDimitry Andric write32(buf + 4, fde.fdeVARel); 28390b57cec5SDimitry Andric buf += 8; 28400b57cec5SDimitry Andric } 28410b57cec5SDimitry Andric } 28420b57cec5SDimitry Andric 28430b57cec5SDimitry Andric size_t EhFrameHeader::getSize() const { 28440b57cec5SDimitry Andric // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. 28450b57cec5SDimitry Andric return 12 + getPartition().ehFrame->numFdes * 8; 28460b57cec5SDimitry Andric } 28470b57cec5SDimitry Andric 28480b57cec5SDimitry Andric bool EhFrameHeader::isNeeded() const { 28490b57cec5SDimitry Andric return isLive() && getPartition().ehFrame->isNeeded(); 28500b57cec5SDimitry Andric } 28510b57cec5SDimitry Andric 28520b57cec5SDimitry Andric VersionDefinitionSection::VersionDefinitionSection() 28530b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), 28540b57cec5SDimitry Andric ".gnu.version_d") {} 28550b57cec5SDimitry Andric 28560b57cec5SDimitry Andric StringRef VersionDefinitionSection::getFileDefName() { 28570b57cec5SDimitry Andric if (!getPartition().name.empty()) 28580b57cec5SDimitry Andric return getPartition().name; 28590b57cec5SDimitry Andric if (!config->soName.empty()) 28600b57cec5SDimitry Andric return config->soName; 28610b57cec5SDimitry Andric return config->outputFile; 28620b57cec5SDimitry Andric } 28630b57cec5SDimitry Andric 28640b57cec5SDimitry Andric void VersionDefinitionSection::finalizeContents() { 28650b57cec5SDimitry Andric fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName()); 2866*85868e8aSDimitry Andric for (const VersionDefinition &v : namedVersionDefs()) 28670b57cec5SDimitry Andric verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name)); 28680b57cec5SDimitry Andric 28690b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 28700b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 28710b57cec5SDimitry Andric 28720b57cec5SDimitry Andric // sh_info should be set to the number of definitions. This fact is missed in 28730b57cec5SDimitry Andric // documentation, but confirmed by binutils community: 28740b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2014-11/msg00355.html 28750b57cec5SDimitry Andric getParent()->info = getVerDefNum(); 28760b57cec5SDimitry Andric } 28770b57cec5SDimitry Andric 28780b57cec5SDimitry Andric void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index, 28790b57cec5SDimitry Andric StringRef name, size_t nameOff) { 28800b57cec5SDimitry Andric uint16_t flags = index == 1 ? VER_FLG_BASE : 0; 28810b57cec5SDimitry Andric 28820b57cec5SDimitry Andric // Write a verdef. 28830b57cec5SDimitry Andric write16(buf, 1); // vd_version 28840b57cec5SDimitry Andric write16(buf + 2, flags); // vd_flags 28850b57cec5SDimitry Andric write16(buf + 4, index); // vd_ndx 28860b57cec5SDimitry Andric write16(buf + 6, 1); // vd_cnt 28870b57cec5SDimitry Andric write32(buf + 8, hashSysV(name)); // vd_hash 28880b57cec5SDimitry Andric write32(buf + 12, 20); // vd_aux 28890b57cec5SDimitry Andric write32(buf + 16, 28); // vd_next 28900b57cec5SDimitry Andric 28910b57cec5SDimitry Andric // Write a veraux. 28920b57cec5SDimitry Andric write32(buf + 20, nameOff); // vda_name 28930b57cec5SDimitry Andric write32(buf + 24, 0); // vda_next 28940b57cec5SDimitry Andric } 28950b57cec5SDimitry Andric 28960b57cec5SDimitry Andric void VersionDefinitionSection::writeTo(uint8_t *buf) { 28970b57cec5SDimitry Andric writeOne(buf, 1, getFileDefName(), fileDefNameOff); 28980b57cec5SDimitry Andric 28990b57cec5SDimitry Andric auto nameOffIt = verDefNameOffs.begin(); 2900*85868e8aSDimitry Andric for (const VersionDefinition &v : namedVersionDefs()) { 29010b57cec5SDimitry Andric buf += EntrySize; 29020b57cec5SDimitry Andric writeOne(buf, v.id, v.name, *nameOffIt++); 29030b57cec5SDimitry Andric } 29040b57cec5SDimitry Andric 29050b57cec5SDimitry Andric // Need to terminate the last version definition. 29060b57cec5SDimitry Andric write32(buf + 16, 0); // vd_next 29070b57cec5SDimitry Andric } 29080b57cec5SDimitry Andric 29090b57cec5SDimitry Andric size_t VersionDefinitionSection::getSize() const { 29100b57cec5SDimitry Andric return EntrySize * getVerDefNum(); 29110b57cec5SDimitry Andric } 29120b57cec5SDimitry Andric 29130b57cec5SDimitry Andric // .gnu.version is a table where each entry is 2 byte long. 29140b57cec5SDimitry Andric VersionTableSection::VersionTableSection() 29150b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), 29160b57cec5SDimitry Andric ".gnu.version") { 29170b57cec5SDimitry Andric this->entsize = 2; 29180b57cec5SDimitry Andric } 29190b57cec5SDimitry Andric 29200b57cec5SDimitry Andric void VersionTableSection::finalizeContents() { 29210b57cec5SDimitry Andric // At the moment of june 2016 GNU docs does not mention that sh_link field 29220b57cec5SDimitry Andric // should be set, but Sun docs do. Also readelf relies on this field. 29230b57cec5SDimitry Andric getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex; 29240b57cec5SDimitry Andric } 29250b57cec5SDimitry Andric 29260b57cec5SDimitry Andric size_t VersionTableSection::getSize() const { 29270b57cec5SDimitry Andric return (getPartition().dynSymTab->getSymbols().size() + 1) * 2; 29280b57cec5SDimitry Andric } 29290b57cec5SDimitry Andric 29300b57cec5SDimitry Andric void VersionTableSection::writeTo(uint8_t *buf) { 29310b57cec5SDimitry Andric buf += 2; 29320b57cec5SDimitry Andric for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) { 29330b57cec5SDimitry Andric write16(buf, s.sym->versionId); 29340b57cec5SDimitry Andric buf += 2; 29350b57cec5SDimitry Andric } 29360b57cec5SDimitry Andric } 29370b57cec5SDimitry Andric 29380b57cec5SDimitry Andric bool VersionTableSection::isNeeded() const { 29390b57cec5SDimitry Andric return getPartition().verDef || getPartition().verNeed->isNeeded(); 29400b57cec5SDimitry Andric } 29410b57cec5SDimitry Andric 2942*85868e8aSDimitry Andric void addVerneed(Symbol *ss) { 29430b57cec5SDimitry Andric auto &file = cast<SharedFile>(*ss->file); 29440b57cec5SDimitry Andric if (ss->verdefIndex == VER_NDX_GLOBAL) { 29450b57cec5SDimitry Andric ss->versionId = VER_NDX_GLOBAL; 29460b57cec5SDimitry Andric return; 29470b57cec5SDimitry Andric } 29480b57cec5SDimitry Andric 29490b57cec5SDimitry Andric if (file.vernauxs.empty()) 29500b57cec5SDimitry Andric file.vernauxs.resize(file.verdefs.size()); 29510b57cec5SDimitry Andric 29520b57cec5SDimitry Andric // Select a version identifier for the vernaux data structure, if we haven't 29530b57cec5SDimitry Andric // already allocated one. The verdef identifiers cover the range 29540b57cec5SDimitry Andric // [1..getVerDefNum()]; this causes the vernaux identifiers to start from 29550b57cec5SDimitry Andric // getVerDefNum()+1. 29560b57cec5SDimitry Andric if (file.vernauxs[ss->verdefIndex] == 0) 29570b57cec5SDimitry Andric file.vernauxs[ss->verdefIndex] = ++SharedFile::vernauxNum + getVerDefNum(); 29580b57cec5SDimitry Andric 29590b57cec5SDimitry Andric ss->versionId = file.vernauxs[ss->verdefIndex]; 29600b57cec5SDimitry Andric } 29610b57cec5SDimitry Andric 29620b57cec5SDimitry Andric template <class ELFT> 29630b57cec5SDimitry Andric VersionNeedSection<ELFT>::VersionNeedSection() 29640b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), 29650b57cec5SDimitry Andric ".gnu.version_r") {} 29660b57cec5SDimitry Andric 29670b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { 29680b57cec5SDimitry Andric for (SharedFile *f : sharedFiles) { 29690b57cec5SDimitry Andric if (f->vernauxs.empty()) 29700b57cec5SDimitry Andric continue; 29710b57cec5SDimitry Andric verneeds.emplace_back(); 29720b57cec5SDimitry Andric Verneed &vn = verneeds.back(); 29730b57cec5SDimitry Andric vn.nameStrTab = getPartition().dynStrTab->addString(f->soName); 29740b57cec5SDimitry Andric for (unsigned i = 0; i != f->vernauxs.size(); ++i) { 29750b57cec5SDimitry Andric if (f->vernauxs[i] == 0) 29760b57cec5SDimitry Andric continue; 29770b57cec5SDimitry Andric auto *verdef = 29780b57cec5SDimitry Andric reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]); 29790b57cec5SDimitry Andric vn.vernauxs.push_back( 29800b57cec5SDimitry Andric {verdef->vd_hash, f->vernauxs[i], 29810b57cec5SDimitry Andric getPartition().dynStrTab->addString(f->getStringTable().data() + 29820b57cec5SDimitry Andric verdef->getAux()->vda_name)}); 29830b57cec5SDimitry Andric } 29840b57cec5SDimitry Andric } 29850b57cec5SDimitry Andric 29860b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 29870b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 29880b57cec5SDimitry Andric getParent()->info = verneeds.size(); 29890b57cec5SDimitry Andric } 29900b57cec5SDimitry Andric 29910b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) { 29920b57cec5SDimitry Andric // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. 29930b57cec5SDimitry Andric auto *verneed = reinterpret_cast<Elf_Verneed *>(buf); 29940b57cec5SDimitry Andric auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size()); 29950b57cec5SDimitry Andric 29960b57cec5SDimitry Andric for (auto &vn : verneeds) { 29970b57cec5SDimitry Andric // Create an Elf_Verneed for this DSO. 29980b57cec5SDimitry Andric verneed->vn_version = 1; 29990b57cec5SDimitry Andric verneed->vn_cnt = vn.vernauxs.size(); 30000b57cec5SDimitry Andric verneed->vn_file = vn.nameStrTab; 30010b57cec5SDimitry Andric verneed->vn_aux = 30020b57cec5SDimitry Andric reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed); 30030b57cec5SDimitry Andric verneed->vn_next = sizeof(Elf_Verneed); 30040b57cec5SDimitry Andric ++verneed; 30050b57cec5SDimitry Andric 30060b57cec5SDimitry Andric // Create the Elf_Vernauxs for this Elf_Verneed. 30070b57cec5SDimitry Andric for (auto &vna : vn.vernauxs) { 30080b57cec5SDimitry Andric vernaux->vna_hash = vna.hash; 30090b57cec5SDimitry Andric vernaux->vna_flags = 0; 30100b57cec5SDimitry Andric vernaux->vna_other = vna.verneedIndex; 30110b57cec5SDimitry Andric vernaux->vna_name = vna.nameStrTab; 30120b57cec5SDimitry Andric vernaux->vna_next = sizeof(Elf_Vernaux); 30130b57cec5SDimitry Andric ++vernaux; 30140b57cec5SDimitry Andric } 30150b57cec5SDimitry Andric 30160b57cec5SDimitry Andric vernaux[-1].vna_next = 0; 30170b57cec5SDimitry Andric } 30180b57cec5SDimitry Andric verneed[-1].vn_next = 0; 30190b57cec5SDimitry Andric } 30200b57cec5SDimitry Andric 30210b57cec5SDimitry Andric template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { 30220b57cec5SDimitry Andric return verneeds.size() * sizeof(Elf_Verneed) + 30230b57cec5SDimitry Andric SharedFile::vernauxNum * sizeof(Elf_Vernaux); 30240b57cec5SDimitry Andric } 30250b57cec5SDimitry Andric 30260b57cec5SDimitry Andric template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const { 30270b57cec5SDimitry Andric return SharedFile::vernauxNum != 0; 30280b57cec5SDimitry Andric } 30290b57cec5SDimitry Andric 30300b57cec5SDimitry Andric void MergeSyntheticSection::addSection(MergeInputSection *ms) { 30310b57cec5SDimitry Andric ms->parent = this; 30320b57cec5SDimitry Andric sections.push_back(ms); 30330b57cec5SDimitry Andric assert(alignment == ms->alignment || !(ms->flags & SHF_STRINGS)); 30340b57cec5SDimitry Andric alignment = std::max(alignment, ms->alignment); 30350b57cec5SDimitry Andric } 30360b57cec5SDimitry Andric 30370b57cec5SDimitry Andric MergeTailSection::MergeTailSection(StringRef name, uint32_t type, 30380b57cec5SDimitry Andric uint64_t flags, uint32_t alignment) 30390b57cec5SDimitry Andric : MergeSyntheticSection(name, type, flags, alignment), 30400b57cec5SDimitry Andric builder(StringTableBuilder::RAW, alignment) {} 30410b57cec5SDimitry Andric 30420b57cec5SDimitry Andric size_t MergeTailSection::getSize() const { return builder.getSize(); } 30430b57cec5SDimitry Andric 30440b57cec5SDimitry Andric void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); } 30450b57cec5SDimitry Andric 30460b57cec5SDimitry Andric void MergeTailSection::finalizeContents() { 30470b57cec5SDimitry Andric // Add all string pieces to the string table builder to create section 30480b57cec5SDimitry Andric // contents. 30490b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 30500b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 30510b57cec5SDimitry Andric if (sec->pieces[i].live) 30520b57cec5SDimitry Andric builder.add(sec->getData(i)); 30530b57cec5SDimitry Andric 30540b57cec5SDimitry Andric // Fix the string table content. After this, the contents will never change. 30550b57cec5SDimitry Andric builder.finalize(); 30560b57cec5SDimitry Andric 30570b57cec5SDimitry Andric // finalize() fixed tail-optimized strings, so we can now get 30580b57cec5SDimitry Andric // offsets of strings. Get an offset for each string and save it 30590b57cec5SDimitry Andric // to a corresponding SectionPiece for easy access. 30600b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 30610b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 30620b57cec5SDimitry Andric if (sec->pieces[i].live) 30630b57cec5SDimitry Andric sec->pieces[i].outputOff = builder.getOffset(sec->getData(i)); 30640b57cec5SDimitry Andric } 30650b57cec5SDimitry Andric 30660b57cec5SDimitry Andric void MergeNoTailSection::writeTo(uint8_t *buf) { 30670b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) 30680b57cec5SDimitry Andric shards[i].write(buf + shardOffsets[i]); 30690b57cec5SDimitry Andric } 30700b57cec5SDimitry Andric 30710b57cec5SDimitry Andric // This function is very hot (i.e. it can take several seconds to finish) 30720b57cec5SDimitry Andric // because sometimes the number of inputs is in an order of magnitude of 30730b57cec5SDimitry Andric // millions. So, we use multi-threading. 30740b57cec5SDimitry Andric // 30750b57cec5SDimitry Andric // For any strings S and T, we know S is not mergeable with T if S's hash 30760b57cec5SDimitry Andric // value is different from T's. If that's the case, we can safely put S and 30770b57cec5SDimitry Andric // T into different string builders without worrying about merge misses. 30780b57cec5SDimitry Andric // We do it in parallel. 30790b57cec5SDimitry Andric void MergeNoTailSection::finalizeContents() { 30800b57cec5SDimitry Andric // Initializes string table builders. 30810b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) 30820b57cec5SDimitry Andric shards.emplace_back(StringTableBuilder::RAW, alignment); 30830b57cec5SDimitry Andric 30840b57cec5SDimitry Andric // Concurrency level. Must be a power of 2 to avoid expensive modulo 30850b57cec5SDimitry Andric // operations in the following tight loop. 30860b57cec5SDimitry Andric size_t concurrency = 1; 30870b57cec5SDimitry Andric if (threadsEnabled) 30880b57cec5SDimitry Andric concurrency = 30890b57cec5SDimitry Andric std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards); 30900b57cec5SDimitry Andric 30910b57cec5SDimitry Andric // Add section pieces to the builders. 30920b57cec5SDimitry Andric parallelForEachN(0, concurrency, [&](size_t threadId) { 30930b57cec5SDimitry Andric for (MergeInputSection *sec : sections) { 30940b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) { 30950b57cec5SDimitry Andric if (!sec->pieces[i].live) 30960b57cec5SDimitry Andric continue; 30970b57cec5SDimitry Andric size_t shardId = getShardId(sec->pieces[i].hash); 30980b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) == threadId) 30990b57cec5SDimitry Andric sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i)); 31000b57cec5SDimitry Andric } 31010b57cec5SDimitry Andric } 31020b57cec5SDimitry Andric }); 31030b57cec5SDimitry Andric 31040b57cec5SDimitry Andric // Compute an in-section offset for each shard. 31050b57cec5SDimitry Andric size_t off = 0; 31060b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) { 31070b57cec5SDimitry Andric shards[i].finalizeInOrder(); 31080b57cec5SDimitry Andric if (shards[i].getSize() > 0) 31090b57cec5SDimitry Andric off = alignTo(off, alignment); 31100b57cec5SDimitry Andric shardOffsets[i] = off; 31110b57cec5SDimitry Andric off += shards[i].getSize(); 31120b57cec5SDimitry Andric } 31130b57cec5SDimitry Andric size = off; 31140b57cec5SDimitry Andric 31150b57cec5SDimitry Andric // So far, section pieces have offsets from beginning of shards, but 31160b57cec5SDimitry Andric // we want offsets from beginning of the whole section. Fix them. 31170b57cec5SDimitry Andric parallelForEach(sections, [&](MergeInputSection *sec) { 31180b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 31190b57cec5SDimitry Andric if (sec->pieces[i].live) 31200b57cec5SDimitry Andric sec->pieces[i].outputOff += 31210b57cec5SDimitry Andric shardOffsets[getShardId(sec->pieces[i].hash)]; 31220b57cec5SDimitry Andric }); 31230b57cec5SDimitry Andric } 31240b57cec5SDimitry Andric 3125*85868e8aSDimitry Andric MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type, 31260b57cec5SDimitry Andric uint64_t flags, 31270b57cec5SDimitry Andric uint32_t alignment) { 31280b57cec5SDimitry Andric bool shouldTailMerge = (flags & SHF_STRINGS) && config->optimize >= 2; 31290b57cec5SDimitry Andric if (shouldTailMerge) 31300b57cec5SDimitry Andric return make<MergeTailSection>(name, type, flags, alignment); 31310b57cec5SDimitry Andric return make<MergeNoTailSection>(name, type, flags, alignment); 31320b57cec5SDimitry Andric } 31330b57cec5SDimitry Andric 3134*85868e8aSDimitry Andric template <class ELFT> void splitSections() { 31350b57cec5SDimitry Andric // splitIntoPieces needs to be called on each MergeInputSection 31360b57cec5SDimitry Andric // before calling finalizeContents(). 31370b57cec5SDimitry Andric parallelForEach(inputSections, [](InputSectionBase *sec) { 31380b57cec5SDimitry Andric if (auto *s = dyn_cast<MergeInputSection>(sec)) 31390b57cec5SDimitry Andric s->splitIntoPieces(); 31400b57cec5SDimitry Andric else if (auto *eh = dyn_cast<EhInputSection>(sec)) 31410b57cec5SDimitry Andric eh->split<ELFT>(); 31420b57cec5SDimitry Andric }); 31430b57cec5SDimitry Andric } 31440b57cec5SDimitry Andric 31450b57cec5SDimitry Andric MipsRldMapSection::MipsRldMapSection() 31460b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 31470b57cec5SDimitry Andric ".rld_map") {} 31480b57cec5SDimitry Andric 31490b57cec5SDimitry Andric ARMExidxSyntheticSection::ARMExidxSyntheticSection() 31500b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, 31510b57cec5SDimitry Andric config->wordsize, ".ARM.exidx") {} 31520b57cec5SDimitry Andric 31530b57cec5SDimitry Andric static InputSection *findExidxSection(InputSection *isec) { 31540b57cec5SDimitry Andric for (InputSection *d : isec->dependentSections) 31550b57cec5SDimitry Andric if (d->type == SHT_ARM_EXIDX) 31560b57cec5SDimitry Andric return d; 31570b57cec5SDimitry Andric return nullptr; 31580b57cec5SDimitry Andric } 31590b57cec5SDimitry Andric 3160*85868e8aSDimitry Andric static bool isValidExidxSectionDep(InputSection *isec) { 3161*85868e8aSDimitry Andric return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) && 3162*85868e8aSDimitry Andric isec->getSize() > 0; 3163*85868e8aSDimitry Andric } 3164*85868e8aSDimitry Andric 31650b57cec5SDimitry Andric bool ARMExidxSyntheticSection::addSection(InputSection *isec) { 31660b57cec5SDimitry Andric if (isec->type == SHT_ARM_EXIDX) { 3167*85868e8aSDimitry Andric if (InputSection* dep = isec->getLinkOrderDep()) 3168*85868e8aSDimitry Andric if (isValidExidxSectionDep(dep)) { 31690b57cec5SDimitry Andric exidxSections.push_back(isec); 31700b57cec5SDimitry Andric return true; 31710b57cec5SDimitry Andric } 3172*85868e8aSDimitry Andric return false; 3173*85868e8aSDimitry Andric } 31740b57cec5SDimitry Andric 3175*85868e8aSDimitry Andric if (isValidExidxSectionDep(isec)) { 31760b57cec5SDimitry Andric executableSections.push_back(isec); 31770b57cec5SDimitry Andric return false; 31780b57cec5SDimitry Andric } 31790b57cec5SDimitry Andric 31800b57cec5SDimitry Andric // FIXME: we do not output a relocation section when --emit-relocs is used 31810b57cec5SDimitry Andric // as we do not have relocation sections for linker generated table entries 31820b57cec5SDimitry Andric // and we would have to erase at a late stage relocations from merged entries. 31830b57cec5SDimitry Andric // Given that exception tables are already position independent and a binary 31840b57cec5SDimitry Andric // analyzer could derive the relocations we choose to erase the relocations. 31850b57cec5SDimitry Andric if (config->emitRelocs && isec->type == SHT_REL) 31860b57cec5SDimitry Andric if (InputSectionBase *ex = isec->getRelocatedSection()) 31870b57cec5SDimitry Andric if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX) 31880b57cec5SDimitry Andric return true; 31890b57cec5SDimitry Andric 31900b57cec5SDimitry Andric return false; 31910b57cec5SDimitry Andric } 31920b57cec5SDimitry Andric 31930b57cec5SDimitry Andric // References to .ARM.Extab Sections have bit 31 clear and are not the 31940b57cec5SDimitry Andric // special EXIDX_CANTUNWIND bit-pattern. 31950b57cec5SDimitry Andric static bool isExtabRef(uint32_t unwind) { 31960b57cec5SDimitry Andric return (unwind & 0x80000000) == 0 && unwind != 0x1; 31970b57cec5SDimitry Andric } 31980b57cec5SDimitry Andric 31990b57cec5SDimitry Andric // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx 32000b57cec5SDimitry Andric // section Prev, where Cur follows Prev in the table. This can be done if the 32010b57cec5SDimitry Andric // unwinding instructions in Cur are identical to Prev. Linker generated 32020b57cec5SDimitry Andric // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an 32030b57cec5SDimitry Andric // InputSection. 32040b57cec5SDimitry Andric static bool isDuplicateArmExidxSec(InputSection *prev, InputSection *cur) { 32050b57cec5SDimitry Andric 32060b57cec5SDimitry Andric struct ExidxEntry { 32070b57cec5SDimitry Andric ulittle32_t fn; 32080b57cec5SDimitry Andric ulittle32_t unwind; 32090b57cec5SDimitry Andric }; 32100b57cec5SDimitry Andric // Get the last table Entry from the previous .ARM.exidx section. If Prev is 32110b57cec5SDimitry Andric // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry. 32120b57cec5SDimitry Andric ExidxEntry prevEntry = {ulittle32_t(0), ulittle32_t(1)}; 32130b57cec5SDimitry Andric if (prev) 32140b57cec5SDimitry Andric prevEntry = prev->getDataAs<ExidxEntry>().back(); 32150b57cec5SDimitry Andric if (isExtabRef(prevEntry.unwind)) 32160b57cec5SDimitry Andric return false; 32170b57cec5SDimitry Andric 32180b57cec5SDimitry Andric // We consider the unwind instructions of an .ARM.exidx table entry 32190b57cec5SDimitry Andric // a duplicate if the previous unwind instructions if: 32200b57cec5SDimitry Andric // - Both are the special EXIDX_CANTUNWIND. 32210b57cec5SDimitry Andric // - Both are the same inline unwind instructions. 32220b57cec5SDimitry Andric // We do not attempt to follow and check links into .ARM.extab tables as 32230b57cec5SDimitry Andric // consecutive identical entries are rare and the effort to check that they 32240b57cec5SDimitry Andric // are identical is high. 32250b57cec5SDimitry Andric 32260b57cec5SDimitry Andric // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry. 32270b57cec5SDimitry Andric if (cur == nullptr) 32280b57cec5SDimitry Andric return prevEntry.unwind == 1; 32290b57cec5SDimitry Andric 32300b57cec5SDimitry Andric for (const ExidxEntry entry : cur->getDataAs<ExidxEntry>()) 32310b57cec5SDimitry Andric if (isExtabRef(entry.unwind) || entry.unwind != prevEntry.unwind) 32320b57cec5SDimitry Andric return false; 32330b57cec5SDimitry Andric 32340b57cec5SDimitry Andric // All table entries in this .ARM.exidx Section can be merged into the 32350b57cec5SDimitry Andric // previous Section. 32360b57cec5SDimitry Andric return true; 32370b57cec5SDimitry Andric } 32380b57cec5SDimitry Andric 32390b57cec5SDimitry Andric // The .ARM.exidx table must be sorted in ascending order of the address of the 32400b57cec5SDimitry Andric // functions the table describes. Optionally duplicate adjacent table entries 32410b57cec5SDimitry Andric // can be removed. At the end of the function the executableSections must be 32420b57cec5SDimitry Andric // sorted in ascending order of address, Sentinel is set to the InputSection 32430b57cec5SDimitry Andric // with the highest address and any InputSections that have mergeable 32440b57cec5SDimitry Andric // .ARM.exidx table entries are removed from it. 32450b57cec5SDimitry Andric void ARMExidxSyntheticSection::finalizeContents() { 3246*85868e8aSDimitry Andric // The executableSections and exidxSections that we use to derive the final 3247*85868e8aSDimitry Andric // contents of this SyntheticSection are populated before 3248*85868e8aSDimitry Andric // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or 3249*85868e8aSDimitry Andric // ICF may remove executable InputSections and their dependent .ARM.exidx 3250*85868e8aSDimitry Andric // section that we recorded earlier. 32510b57cec5SDimitry Andric auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); }; 32520b57cec5SDimitry Andric llvm::erase_if(executableSections, isDiscarded); 32530b57cec5SDimitry Andric llvm::erase_if(exidxSections, isDiscarded); 32540b57cec5SDimitry Andric 32550b57cec5SDimitry Andric // Sort the executable sections that may or may not have associated 32560b57cec5SDimitry Andric // .ARM.exidx sections by order of ascending address. This requires the 32570b57cec5SDimitry Andric // relative positions of InputSections to be known. 32580b57cec5SDimitry Andric auto compareByFilePosition = [](const InputSection *a, 32590b57cec5SDimitry Andric const InputSection *b) { 32600b57cec5SDimitry Andric OutputSection *aOut = a->getParent(); 32610b57cec5SDimitry Andric OutputSection *bOut = b->getParent(); 32620b57cec5SDimitry Andric 32630b57cec5SDimitry Andric if (aOut != bOut) 32640b57cec5SDimitry Andric return aOut->sectionIndex < bOut->sectionIndex; 32650b57cec5SDimitry Andric return a->outSecOff < b->outSecOff; 32660b57cec5SDimitry Andric }; 32670b57cec5SDimitry Andric llvm::stable_sort(executableSections, compareByFilePosition); 32680b57cec5SDimitry Andric sentinel = executableSections.back(); 32690b57cec5SDimitry Andric // Optionally merge adjacent duplicate entries. 32700b57cec5SDimitry Andric if (config->mergeArmExidx) { 32710b57cec5SDimitry Andric std::vector<InputSection *> selectedSections; 32720b57cec5SDimitry Andric selectedSections.reserve(executableSections.size()); 32730b57cec5SDimitry Andric selectedSections.push_back(executableSections[0]); 32740b57cec5SDimitry Andric size_t prev = 0; 32750b57cec5SDimitry Andric for (size_t i = 1; i < executableSections.size(); ++i) { 32760b57cec5SDimitry Andric InputSection *ex1 = findExidxSection(executableSections[prev]); 32770b57cec5SDimitry Andric InputSection *ex2 = findExidxSection(executableSections[i]); 32780b57cec5SDimitry Andric if (!isDuplicateArmExidxSec(ex1, ex2)) { 32790b57cec5SDimitry Andric selectedSections.push_back(executableSections[i]); 32800b57cec5SDimitry Andric prev = i; 32810b57cec5SDimitry Andric } 32820b57cec5SDimitry Andric } 32830b57cec5SDimitry Andric executableSections = std::move(selectedSections); 32840b57cec5SDimitry Andric } 32850b57cec5SDimitry Andric 32860b57cec5SDimitry Andric size_t offset = 0; 32870b57cec5SDimitry Andric size = 0; 32880b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 32890b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 32900b57cec5SDimitry Andric d->outSecOff = offset; 32910b57cec5SDimitry Andric d->parent = getParent(); 32920b57cec5SDimitry Andric offset += d->getSize(); 32930b57cec5SDimitry Andric } else { 32940b57cec5SDimitry Andric offset += 8; 32950b57cec5SDimitry Andric } 32960b57cec5SDimitry Andric } 32970b57cec5SDimitry Andric // Size includes Sentinel. 32980b57cec5SDimitry Andric size = offset + 8; 32990b57cec5SDimitry Andric } 33000b57cec5SDimitry Andric 33010b57cec5SDimitry Andric InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const { 33020b57cec5SDimitry Andric return executableSections.front(); 33030b57cec5SDimitry Andric } 33040b57cec5SDimitry Andric 33050b57cec5SDimitry Andric // To write the .ARM.exidx table from the ExecutableSections we have three cases 33060b57cec5SDimitry Andric // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections. 33070b57cec5SDimitry Andric // We write the .ARM.exidx section contents and apply its relocations. 33080b57cec5SDimitry Andric // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We 33090b57cec5SDimitry Andric // must write the contents of an EXIDX_CANTUNWIND directly. We use the 33100b57cec5SDimitry Andric // start of the InputSection as the purpose of the linker generated 33110b57cec5SDimitry Andric // section is to terminate the address range of the previous entry. 33120b57cec5SDimitry Andric // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of 33130b57cec5SDimitry Andric // the table to terminate the address range of the final entry. 33140b57cec5SDimitry Andric void ARMExidxSyntheticSection::writeTo(uint8_t *buf) { 33150b57cec5SDimitry Andric 33160b57cec5SDimitry Andric const uint8_t cantUnwindData[8] = {0, 0, 0, 0, // PREL31 to target 33170b57cec5SDimitry Andric 1, 0, 0, 0}; // EXIDX_CANTUNWIND 33180b57cec5SDimitry Andric 33190b57cec5SDimitry Andric uint64_t offset = 0; 33200b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 33210b57cec5SDimitry Andric assert(isec->getParent() != nullptr); 33220b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 33230b57cec5SDimitry Andric memcpy(buf + offset, d->data().data(), d->data().size()); 33240b57cec5SDimitry Andric d->relocateAlloc(buf, buf + d->getSize()); 33250b57cec5SDimitry Andric offset += d->getSize(); 33260b57cec5SDimitry Andric } else { 33270b57cec5SDimitry Andric // A Linker generated CANTUNWIND section. 33280b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 33290b57cec5SDimitry Andric uint64_t s = isec->getVA(); 33300b57cec5SDimitry Andric uint64_t p = getVA() + offset; 33310b57cec5SDimitry Andric target->relocateOne(buf + offset, R_ARM_PREL31, s - p); 33320b57cec5SDimitry Andric offset += 8; 33330b57cec5SDimitry Andric } 33340b57cec5SDimitry Andric } 33350b57cec5SDimitry Andric // Write Sentinel. 33360b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 33370b57cec5SDimitry Andric uint64_t s = sentinel->getVA(sentinel->getSize()); 33380b57cec5SDimitry Andric uint64_t p = getVA() + offset; 33390b57cec5SDimitry Andric target->relocateOne(buf + offset, R_ARM_PREL31, s - p); 33400b57cec5SDimitry Andric assert(size == offset + 8); 33410b57cec5SDimitry Andric } 33420b57cec5SDimitry Andric 3343*85868e8aSDimitry Andric bool ARMExidxSyntheticSection::isNeeded() const { 3344*85868e8aSDimitry Andric return llvm::find_if(exidxSections, [](InputSection *isec) { 3345*85868e8aSDimitry Andric return isec->isLive(); 3346*85868e8aSDimitry Andric }) != exidxSections.end(); 3347*85868e8aSDimitry Andric } 3348*85868e8aSDimitry Andric 33490b57cec5SDimitry Andric bool ARMExidxSyntheticSection::classof(const SectionBase *d) { 33500b57cec5SDimitry Andric return d->kind() == InputSectionBase::Synthetic && d->type == SHT_ARM_EXIDX; 33510b57cec5SDimitry Andric } 33520b57cec5SDimitry Andric 33530b57cec5SDimitry Andric ThunkSection::ThunkSection(OutputSection *os, uint64_t off) 33540b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 33550b57cec5SDimitry Andric config->wordsize, ".text.thunk") { 33560b57cec5SDimitry Andric this->parent = os; 33570b57cec5SDimitry Andric this->outSecOff = off; 33580b57cec5SDimitry Andric } 33590b57cec5SDimitry Andric 33600b57cec5SDimitry Andric void ThunkSection::addThunk(Thunk *t) { 33610b57cec5SDimitry Andric thunks.push_back(t); 33620b57cec5SDimitry Andric t->addSymbols(*this); 33630b57cec5SDimitry Andric } 33640b57cec5SDimitry Andric 33650b57cec5SDimitry Andric void ThunkSection::writeTo(uint8_t *buf) { 33660b57cec5SDimitry Andric for (Thunk *t : thunks) 33670b57cec5SDimitry Andric t->writeTo(buf + t->offset); 33680b57cec5SDimitry Andric } 33690b57cec5SDimitry Andric 33700b57cec5SDimitry Andric InputSection *ThunkSection::getTargetInputSection() const { 33710b57cec5SDimitry Andric if (thunks.empty()) 33720b57cec5SDimitry Andric return nullptr; 33730b57cec5SDimitry Andric const Thunk *t = thunks.front(); 33740b57cec5SDimitry Andric return t->getTargetInputSection(); 33750b57cec5SDimitry Andric } 33760b57cec5SDimitry Andric 33770b57cec5SDimitry Andric bool ThunkSection::assignOffsets() { 33780b57cec5SDimitry Andric uint64_t off = 0; 33790b57cec5SDimitry Andric for (Thunk *t : thunks) { 33800b57cec5SDimitry Andric off = alignTo(off, t->alignment); 33810b57cec5SDimitry Andric t->setOffset(off); 33820b57cec5SDimitry Andric uint32_t size = t->size(); 33830b57cec5SDimitry Andric t->getThunkTargetSym()->size = size; 33840b57cec5SDimitry Andric off += size; 33850b57cec5SDimitry Andric } 33860b57cec5SDimitry Andric bool changed = off != size; 33870b57cec5SDimitry Andric size = off; 33880b57cec5SDimitry Andric return changed; 33890b57cec5SDimitry Andric } 33900b57cec5SDimitry Andric 33910b57cec5SDimitry Andric PPC32Got2Section::PPC32Got2Section() 33920b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {} 33930b57cec5SDimitry Andric 33940b57cec5SDimitry Andric bool PPC32Got2Section::isNeeded() const { 33950b57cec5SDimitry Andric // See the comment below. This is not needed if there is no other 33960b57cec5SDimitry Andric // InputSection. 33970b57cec5SDimitry Andric for (BaseCommand *base : getParent()->sectionCommands) 33980b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(base)) 33990b57cec5SDimitry Andric for (InputSection *isec : isd->sections) 34000b57cec5SDimitry Andric if (isec != this) 34010b57cec5SDimitry Andric return true; 34020b57cec5SDimitry Andric return false; 34030b57cec5SDimitry Andric } 34040b57cec5SDimitry Andric 34050b57cec5SDimitry Andric void PPC32Got2Section::finalizeContents() { 34060b57cec5SDimitry Andric // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in 34070b57cec5SDimitry Andric // .got2 . This function computes outSecOff of each .got2 to be used in 34080b57cec5SDimitry Andric // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is 34090b57cec5SDimitry Andric // to collect input sections named ".got2". 34100b57cec5SDimitry Andric uint32_t offset = 0; 34110b57cec5SDimitry Andric for (BaseCommand *base : getParent()->sectionCommands) 34120b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(base)) { 34130b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 34140b57cec5SDimitry Andric if (isec == this) 34150b57cec5SDimitry Andric continue; 34160b57cec5SDimitry Andric isec->file->ppc32Got2OutSecOff = offset; 34170b57cec5SDimitry Andric offset += (uint32_t)isec->getSize(); 34180b57cec5SDimitry Andric } 34190b57cec5SDimitry Andric } 34200b57cec5SDimitry Andric } 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric // If linking position-dependent code then the table will store the addresses 34230b57cec5SDimitry Andric // directly in the binary so the section has type SHT_PROGBITS. If linking 34240b57cec5SDimitry Andric // position-independent code the section has type SHT_NOBITS since it will be 34250b57cec5SDimitry Andric // allocated and filled in by the dynamic linker. 34260b57cec5SDimitry Andric PPC64LongBranchTargetSection::PPC64LongBranchTargetSection() 34270b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 34280b57cec5SDimitry Andric config->isPic ? SHT_NOBITS : SHT_PROGBITS, 8, 34290b57cec5SDimitry Andric ".branch_lt") {} 34300b57cec5SDimitry Andric 34310b57cec5SDimitry Andric void PPC64LongBranchTargetSection::addEntry(Symbol &sym) { 34320b57cec5SDimitry Andric assert(sym.ppc64BranchltIndex == 0xffff); 34330b57cec5SDimitry Andric sym.ppc64BranchltIndex = entries.size(); 34340b57cec5SDimitry Andric entries.push_back(&sym); 34350b57cec5SDimitry Andric } 34360b57cec5SDimitry Andric 34370b57cec5SDimitry Andric size_t PPC64LongBranchTargetSection::getSize() const { 34380b57cec5SDimitry Andric return entries.size() * 8; 34390b57cec5SDimitry Andric } 34400b57cec5SDimitry Andric 34410b57cec5SDimitry Andric void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) { 34420b57cec5SDimitry Andric // If linking non-pic we have the final addresses of the targets and they get 34430b57cec5SDimitry Andric // written to the table directly. For pic the dynamic linker will allocate 34440b57cec5SDimitry Andric // the section and fill it it. 34450b57cec5SDimitry Andric if (config->isPic) 34460b57cec5SDimitry Andric return; 34470b57cec5SDimitry Andric 34480b57cec5SDimitry Andric for (const Symbol *sym : entries) { 34490b57cec5SDimitry Andric assert(sym->getVA()); 34500b57cec5SDimitry Andric // Need calls to branch to the local entry-point since a long-branch 34510b57cec5SDimitry Andric // must be a local-call. 34520b57cec5SDimitry Andric write64(buf, 34530b57cec5SDimitry Andric sym->getVA() + getPPC64GlobalEntryToLocalEntryOffset(sym->stOther)); 34540b57cec5SDimitry Andric buf += 8; 34550b57cec5SDimitry Andric } 34560b57cec5SDimitry Andric } 34570b57cec5SDimitry Andric 34580b57cec5SDimitry Andric bool PPC64LongBranchTargetSection::isNeeded() const { 34590b57cec5SDimitry Andric // `removeUnusedSyntheticSections()` is called before thunk allocation which 34600b57cec5SDimitry Andric // is too early to determine if this section will be empty or not. We need 34610b57cec5SDimitry Andric // Finalized to keep the section alive until after thunk creation. Finalized 34620b57cec5SDimitry Andric // only gets set to true once `finalizeSections()` is called after thunk 34630b57cec5SDimitry Andric // creation. Becuase of this, if we don't create any long-branch thunks we end 34640b57cec5SDimitry Andric // up with an empty .branch_lt section in the binary. 34650b57cec5SDimitry Andric return !finalized || !entries.empty(); 34660b57cec5SDimitry Andric } 34670b57cec5SDimitry Andric 34680b57cec5SDimitry Andric static uint8_t getAbiVersion() { 34690b57cec5SDimitry Andric // MIPS non-PIC executable gets ABI version 1. 34700b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 34710b57cec5SDimitry Andric if (!config->isPic && !config->relocatable && 34720b57cec5SDimitry Andric (config->eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC) 34730b57cec5SDimitry Andric return 1; 34740b57cec5SDimitry Andric return 0; 34750b57cec5SDimitry Andric } 34760b57cec5SDimitry Andric 34770b57cec5SDimitry Andric if (config->emachine == EM_AMDGPU) { 34780b57cec5SDimitry Andric uint8_t ver = objectFiles[0]->abiVersion; 34790b57cec5SDimitry Andric for (InputFile *file : makeArrayRef(objectFiles).slice(1)) 34800b57cec5SDimitry Andric if (file->abiVersion != ver) 34810b57cec5SDimitry Andric error("incompatible ABI version: " + toString(file)); 34820b57cec5SDimitry Andric return ver; 34830b57cec5SDimitry Andric } 34840b57cec5SDimitry Andric 34850b57cec5SDimitry Andric return 0; 34860b57cec5SDimitry Andric } 34870b57cec5SDimitry Andric 3488*85868e8aSDimitry Andric template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part) { 34890b57cec5SDimitry Andric // For executable segments, the trap instructions are written before writing 34900b57cec5SDimitry Andric // the header. Setting Elf header bytes to zero ensures that any unused bytes 34910b57cec5SDimitry Andric // in header are zero-cleared, instead of having trap instructions. 34920b57cec5SDimitry Andric memset(buf, 0, sizeof(typename ELFT::Ehdr)); 34930b57cec5SDimitry Andric memcpy(buf, "\177ELF", 4); 34940b57cec5SDimitry Andric 34950b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 34960b57cec5SDimitry Andric eHdr->e_ident[EI_CLASS] = config->is64 ? ELFCLASS64 : ELFCLASS32; 34970b57cec5SDimitry Andric eHdr->e_ident[EI_DATA] = config->isLE ? ELFDATA2LSB : ELFDATA2MSB; 34980b57cec5SDimitry Andric eHdr->e_ident[EI_VERSION] = EV_CURRENT; 34990b57cec5SDimitry Andric eHdr->e_ident[EI_OSABI] = config->osabi; 35000b57cec5SDimitry Andric eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(); 35010b57cec5SDimitry Andric eHdr->e_machine = config->emachine; 35020b57cec5SDimitry Andric eHdr->e_version = EV_CURRENT; 35030b57cec5SDimitry Andric eHdr->e_flags = config->eflags; 35040b57cec5SDimitry Andric eHdr->e_ehsize = sizeof(typename ELFT::Ehdr); 35050b57cec5SDimitry Andric eHdr->e_phnum = part.phdrs.size(); 35060b57cec5SDimitry Andric eHdr->e_shentsize = sizeof(typename ELFT::Shdr); 35070b57cec5SDimitry Andric 35080b57cec5SDimitry Andric if (!config->relocatable) { 35090b57cec5SDimitry Andric eHdr->e_phoff = sizeof(typename ELFT::Ehdr); 35100b57cec5SDimitry Andric eHdr->e_phentsize = sizeof(typename ELFT::Phdr); 35110b57cec5SDimitry Andric } 35120b57cec5SDimitry Andric } 35130b57cec5SDimitry Andric 3514*85868e8aSDimitry Andric template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part) { 35150b57cec5SDimitry Andric // Write the program header table. 35160b57cec5SDimitry Andric auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf); 35170b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) { 35180b57cec5SDimitry Andric hBuf->p_type = p->p_type; 35190b57cec5SDimitry Andric hBuf->p_flags = p->p_flags; 35200b57cec5SDimitry Andric hBuf->p_offset = p->p_offset; 35210b57cec5SDimitry Andric hBuf->p_vaddr = p->p_vaddr; 35220b57cec5SDimitry Andric hBuf->p_paddr = p->p_paddr; 35230b57cec5SDimitry Andric hBuf->p_filesz = p->p_filesz; 35240b57cec5SDimitry Andric hBuf->p_memsz = p->p_memsz; 35250b57cec5SDimitry Andric hBuf->p_align = p->p_align; 35260b57cec5SDimitry Andric ++hBuf; 35270b57cec5SDimitry Andric } 35280b57cec5SDimitry Andric } 35290b57cec5SDimitry Andric 35300b57cec5SDimitry Andric template <typename ELFT> 35310b57cec5SDimitry Andric PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection() 35320b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {} 35330b57cec5SDimitry Andric 35340b57cec5SDimitry Andric template <typename ELFT> 35350b57cec5SDimitry Andric size_t PartitionElfHeaderSection<ELFT>::getSize() const { 35360b57cec5SDimitry Andric return sizeof(typename ELFT::Ehdr); 35370b57cec5SDimitry Andric } 35380b57cec5SDimitry Andric 35390b57cec5SDimitry Andric template <typename ELFT> 35400b57cec5SDimitry Andric void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) { 35410b57cec5SDimitry Andric writeEhdr<ELFT>(buf, getPartition()); 35420b57cec5SDimitry Andric 35430b57cec5SDimitry Andric // Loadable partitions are always ET_DYN. 35440b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 35450b57cec5SDimitry Andric eHdr->e_type = ET_DYN; 35460b57cec5SDimitry Andric } 35470b57cec5SDimitry Andric 35480b57cec5SDimitry Andric template <typename ELFT> 35490b57cec5SDimitry Andric PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection() 35500b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {} 35510b57cec5SDimitry Andric 35520b57cec5SDimitry Andric template <typename ELFT> 35530b57cec5SDimitry Andric size_t PartitionProgramHeadersSection<ELFT>::getSize() const { 35540b57cec5SDimitry Andric return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size(); 35550b57cec5SDimitry Andric } 35560b57cec5SDimitry Andric 35570b57cec5SDimitry Andric template <typename ELFT> 35580b57cec5SDimitry Andric void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) { 35590b57cec5SDimitry Andric writePhdrs<ELFT>(buf, getPartition()); 35600b57cec5SDimitry Andric } 35610b57cec5SDimitry Andric 35620b57cec5SDimitry Andric PartitionIndexSection::PartitionIndexSection() 35630b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {} 35640b57cec5SDimitry Andric 35650b57cec5SDimitry Andric size_t PartitionIndexSection::getSize() const { 35660b57cec5SDimitry Andric return 12 * (partitions.size() - 1); 35670b57cec5SDimitry Andric } 35680b57cec5SDimitry Andric 35690b57cec5SDimitry Andric void PartitionIndexSection::finalizeContents() { 35700b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) 35710b57cec5SDimitry Andric partitions[i].nameStrTab = mainPart->dynStrTab->addString(partitions[i].name); 35720b57cec5SDimitry Andric } 35730b57cec5SDimitry Andric 35740b57cec5SDimitry Andric void PartitionIndexSection::writeTo(uint8_t *buf) { 35750b57cec5SDimitry Andric uint64_t va = getVA(); 35760b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) { 35770b57cec5SDimitry Andric write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va); 35780b57cec5SDimitry Andric write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4)); 35790b57cec5SDimitry Andric 35800b57cec5SDimitry Andric SyntheticSection *next = 35810b57cec5SDimitry Andric i == partitions.size() - 1 ? in.partEnd : partitions[i + 1].elfHeader; 35820b57cec5SDimitry Andric write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA()); 35830b57cec5SDimitry Andric 35840b57cec5SDimitry Andric va += 12; 35850b57cec5SDimitry Andric buf += 12; 35860b57cec5SDimitry Andric } 35870b57cec5SDimitry Andric } 35880b57cec5SDimitry Andric 3589*85868e8aSDimitry Andric InStruct in; 35900b57cec5SDimitry Andric 3591*85868e8aSDimitry Andric std::vector<Partition> partitions; 3592*85868e8aSDimitry Andric Partition *mainPart; 35930b57cec5SDimitry Andric 35940b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32LE>(); 35950b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32BE>(); 35960b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64LE>(); 35970b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64BE>(); 35980b57cec5SDimitry Andric 3599*85868e8aSDimitry Andric template void splitSections<ELF32LE>(); 3600*85868e8aSDimitry Andric template void splitSections<ELF32BE>(); 3601*85868e8aSDimitry Andric template void splitSections<ELF64LE>(); 3602*85868e8aSDimitry Andric template void splitSections<ELF64BE>(); 36030b57cec5SDimitry Andric 36040b57cec5SDimitry Andric template void PltSection::addEntry<ELF32LE>(Symbol &Sym); 36050b57cec5SDimitry Andric template void PltSection::addEntry<ELF32BE>(Symbol &Sym); 36060b57cec5SDimitry Andric template void PltSection::addEntry<ELF64LE>(Symbol &Sym); 36070b57cec5SDimitry Andric template void PltSection::addEntry<ELF64BE>(Symbol &Sym); 36080b57cec5SDimitry Andric 3609*85868e8aSDimitry Andric template class MipsAbiFlagsSection<ELF32LE>; 3610*85868e8aSDimitry Andric template class MipsAbiFlagsSection<ELF32BE>; 3611*85868e8aSDimitry Andric template class MipsAbiFlagsSection<ELF64LE>; 3612*85868e8aSDimitry Andric template class MipsAbiFlagsSection<ELF64BE>; 36130b57cec5SDimitry Andric 3614*85868e8aSDimitry Andric template class MipsOptionsSection<ELF32LE>; 3615*85868e8aSDimitry Andric template class MipsOptionsSection<ELF32BE>; 3616*85868e8aSDimitry Andric template class MipsOptionsSection<ELF64LE>; 3617*85868e8aSDimitry Andric template class MipsOptionsSection<ELF64BE>; 36180b57cec5SDimitry Andric 3619*85868e8aSDimitry Andric template class MipsReginfoSection<ELF32LE>; 3620*85868e8aSDimitry Andric template class MipsReginfoSection<ELF32BE>; 3621*85868e8aSDimitry Andric template class MipsReginfoSection<ELF64LE>; 3622*85868e8aSDimitry Andric template class MipsReginfoSection<ELF64BE>; 36230b57cec5SDimitry Andric 3624*85868e8aSDimitry Andric template class DynamicSection<ELF32LE>; 3625*85868e8aSDimitry Andric template class DynamicSection<ELF32BE>; 3626*85868e8aSDimitry Andric template class DynamicSection<ELF64LE>; 3627*85868e8aSDimitry Andric template class DynamicSection<ELF64BE>; 36280b57cec5SDimitry Andric 3629*85868e8aSDimitry Andric template class RelocationSection<ELF32LE>; 3630*85868e8aSDimitry Andric template class RelocationSection<ELF32BE>; 3631*85868e8aSDimitry Andric template class RelocationSection<ELF64LE>; 3632*85868e8aSDimitry Andric template class RelocationSection<ELF64BE>; 36330b57cec5SDimitry Andric 3634*85868e8aSDimitry Andric template class AndroidPackedRelocationSection<ELF32LE>; 3635*85868e8aSDimitry Andric template class AndroidPackedRelocationSection<ELF32BE>; 3636*85868e8aSDimitry Andric template class AndroidPackedRelocationSection<ELF64LE>; 3637*85868e8aSDimitry Andric template class AndroidPackedRelocationSection<ELF64BE>; 36380b57cec5SDimitry Andric 3639*85868e8aSDimitry Andric template class RelrSection<ELF32LE>; 3640*85868e8aSDimitry Andric template class RelrSection<ELF32BE>; 3641*85868e8aSDimitry Andric template class RelrSection<ELF64LE>; 3642*85868e8aSDimitry Andric template class RelrSection<ELF64BE>; 36430b57cec5SDimitry Andric 3644*85868e8aSDimitry Andric template class SymbolTableSection<ELF32LE>; 3645*85868e8aSDimitry Andric template class SymbolTableSection<ELF32BE>; 3646*85868e8aSDimitry Andric template class SymbolTableSection<ELF64LE>; 3647*85868e8aSDimitry Andric template class SymbolTableSection<ELF64BE>; 36480b57cec5SDimitry Andric 3649*85868e8aSDimitry Andric template class VersionNeedSection<ELF32LE>; 3650*85868e8aSDimitry Andric template class VersionNeedSection<ELF32BE>; 3651*85868e8aSDimitry Andric template class VersionNeedSection<ELF64LE>; 3652*85868e8aSDimitry Andric template class VersionNeedSection<ELF64BE>; 36530b57cec5SDimitry Andric 3654*85868e8aSDimitry Andric template void writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part); 3655*85868e8aSDimitry Andric template void writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part); 3656*85868e8aSDimitry Andric template void writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part); 3657*85868e8aSDimitry Andric template void writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part); 36580b57cec5SDimitry Andric 3659*85868e8aSDimitry Andric template void writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part); 3660*85868e8aSDimitry Andric template void writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part); 3661*85868e8aSDimitry Andric template void writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part); 3662*85868e8aSDimitry Andric template void writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part); 36630b57cec5SDimitry Andric 3664*85868e8aSDimitry Andric template class PartitionElfHeaderSection<ELF32LE>; 3665*85868e8aSDimitry Andric template class PartitionElfHeaderSection<ELF32BE>; 3666*85868e8aSDimitry Andric template class PartitionElfHeaderSection<ELF64LE>; 3667*85868e8aSDimitry Andric template class PartitionElfHeaderSection<ELF64BE>; 36680b57cec5SDimitry Andric 3669*85868e8aSDimitry Andric template class PartitionProgramHeadersSection<ELF32LE>; 3670*85868e8aSDimitry Andric template class PartitionProgramHeadersSection<ELF32BE>; 3671*85868e8aSDimitry Andric template class PartitionProgramHeadersSection<ELF64LE>; 3672*85868e8aSDimitry Andric template class PartitionProgramHeadersSection<ELF64BE>; 3673*85868e8aSDimitry Andric 3674*85868e8aSDimitry Andric } // namespace elf 3675*85868e8aSDimitry Andric } // namespace lld 3676