1*0b57cec5SDimitry Andric //===- SyntheticSections.cpp ----------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains linker-synthesized sections. Currently, 10*0b57cec5SDimitry Andric // synthetic sections are created either output sections or input sections, 11*0b57cec5SDimitry Andric // but we are rewriting code so that all synthetic sections are created as 12*0b57cec5SDimitry Andric // input sections. 13*0b57cec5SDimitry Andric // 14*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include "SyntheticSections.h" 17*0b57cec5SDimitry Andric #include "Config.h" 18*0b57cec5SDimitry Andric #include "InputFiles.h" 19*0b57cec5SDimitry Andric #include "LinkerScript.h" 20*0b57cec5SDimitry Andric #include "OutputSections.h" 21*0b57cec5SDimitry Andric #include "SymbolTable.h" 22*0b57cec5SDimitry Andric #include "Symbols.h" 23*0b57cec5SDimitry Andric #include "Target.h" 24*0b57cec5SDimitry Andric #include "Writer.h" 25*0b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 26*0b57cec5SDimitry Andric #include "lld/Common/Memory.h" 27*0b57cec5SDimitry Andric #include "lld/Common/Strings.h" 28*0b57cec5SDimitry Andric #include "lld/Common/Threads.h" 29*0b57cec5SDimitry Andric #include "lld/Common/Version.h" 30*0b57cec5SDimitry Andric #include "llvm/ADT/SetOperations.h" 31*0b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 32*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 33*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 34*0b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 35*0b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 36*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 37*0b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 38*0b57cec5SDimitry Andric #include "llvm/Support/MD5.h" 39*0b57cec5SDimitry Andric #include <cstdlib> 40*0b57cec5SDimitry Andric #include <thread> 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric using namespace llvm; 43*0b57cec5SDimitry Andric using namespace llvm::dwarf; 44*0b57cec5SDimitry Andric using namespace llvm::ELF; 45*0b57cec5SDimitry Andric using namespace llvm::object; 46*0b57cec5SDimitry Andric using namespace llvm::support; 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric using namespace lld; 49*0b57cec5SDimitry Andric using namespace lld::elf; 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric using llvm::support::endian::read32le; 52*0b57cec5SDimitry Andric using llvm::support::endian::write32le; 53*0b57cec5SDimitry Andric using llvm::support::endian::write64le; 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric constexpr size_t MergeNoTailSection::numShards; 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric static uint64_t readUint(uint8_t *buf) { 58*0b57cec5SDimitry Andric return config->is64 ? read64(buf) : read32(buf); 59*0b57cec5SDimitry Andric } 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric static void writeUint(uint8_t *buf, uint64_t val) { 62*0b57cec5SDimitry Andric if (config->is64) 63*0b57cec5SDimitry Andric write64(buf, val); 64*0b57cec5SDimitry Andric else 65*0b57cec5SDimitry Andric write32(buf, val); 66*0b57cec5SDimitry Andric } 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric // Returns an LLD version string. 69*0b57cec5SDimitry Andric static ArrayRef<uint8_t> getVersion() { 70*0b57cec5SDimitry Andric // Check LLD_VERSION first for ease of testing. 71*0b57cec5SDimitry Andric // You can get consistent output by using the environment variable. 72*0b57cec5SDimitry Andric // This is only for testing. 73*0b57cec5SDimitry Andric StringRef s = getenv("LLD_VERSION"); 74*0b57cec5SDimitry Andric if (s.empty()) 75*0b57cec5SDimitry Andric s = saver.save(Twine("Linker: ") + getLLDVersion()); 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric // +1 to include the terminating '\0'. 78*0b57cec5SDimitry Andric return {(const uint8_t *)s.data(), s.size() + 1}; 79*0b57cec5SDimitry Andric } 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric // Creates a .comment section containing LLD version info. 82*0b57cec5SDimitry Andric // With this feature, you can identify LLD-generated binaries easily 83*0b57cec5SDimitry Andric // by "readelf --string-dump .comment <file>". 84*0b57cec5SDimitry Andric // The returned object is a mergeable string section. 85*0b57cec5SDimitry Andric MergeInputSection *elf::createCommentSection() { 86*0b57cec5SDimitry Andric return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1, 87*0b57cec5SDimitry Andric getVersion(), ".comment"); 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric // .MIPS.abiflags section. 91*0b57cec5SDimitry Andric template <class ELFT> 92*0b57cec5SDimitry Andric MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags) 93*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), 94*0b57cec5SDimitry Andric flags(flags) { 95*0b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_ABIFlags); 96*0b57cec5SDimitry Andric } 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) { 99*0b57cec5SDimitry Andric memcpy(buf, &flags, sizeof(flags)); 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric template <class ELFT> 103*0b57cec5SDimitry Andric MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() { 104*0b57cec5SDimitry Andric Elf_Mips_ABIFlags flags = {}; 105*0b57cec5SDimitry Andric bool create = false; 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) { 108*0b57cec5SDimitry Andric if (sec->type != SHT_MIPS_ABIFLAGS) 109*0b57cec5SDimitry Andric continue; 110*0b57cec5SDimitry Andric sec->markDead(); 111*0b57cec5SDimitry Andric create = true; 112*0b57cec5SDimitry Andric 113*0b57cec5SDimitry Andric std::string filename = toString(sec->file); 114*0b57cec5SDimitry Andric const size_t size = sec->data().size(); 115*0b57cec5SDimitry Andric // Older version of BFD (such as the default FreeBSD linker) concatenate 116*0b57cec5SDimitry Andric // .MIPS.abiflags instead of merging. To allow for this case (or potential 117*0b57cec5SDimitry Andric // zero padding) we ignore everything after the first Elf_Mips_ABIFlags 118*0b57cec5SDimitry Andric if (size < sizeof(Elf_Mips_ABIFlags)) { 119*0b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.abiflags section: got " + 120*0b57cec5SDimitry Andric Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); 121*0b57cec5SDimitry Andric return nullptr; 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data()); 124*0b57cec5SDimitry Andric if (s->version != 0) { 125*0b57cec5SDimitry Andric error(filename + ": unexpected .MIPS.abiflags version " + 126*0b57cec5SDimitry Andric Twine(s->version)); 127*0b57cec5SDimitry Andric return nullptr; 128*0b57cec5SDimitry Andric } 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric // LLD checks ISA compatibility in calcMipsEFlags(). Here we just 131*0b57cec5SDimitry Andric // select the highest number of ISA/Rev/Ext. 132*0b57cec5SDimitry Andric flags.isa_level = std::max(flags.isa_level, s->isa_level); 133*0b57cec5SDimitry Andric flags.isa_rev = std::max(flags.isa_rev, s->isa_rev); 134*0b57cec5SDimitry Andric flags.isa_ext = std::max(flags.isa_ext, s->isa_ext); 135*0b57cec5SDimitry Andric flags.gpr_size = std::max(flags.gpr_size, s->gpr_size); 136*0b57cec5SDimitry Andric flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size); 137*0b57cec5SDimitry Andric flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size); 138*0b57cec5SDimitry Andric flags.ases |= s->ases; 139*0b57cec5SDimitry Andric flags.flags1 |= s->flags1; 140*0b57cec5SDimitry Andric flags.flags2 |= s->flags2; 141*0b57cec5SDimitry Andric flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename); 142*0b57cec5SDimitry Andric }; 143*0b57cec5SDimitry Andric 144*0b57cec5SDimitry Andric if (create) 145*0b57cec5SDimitry Andric return make<MipsAbiFlagsSection<ELFT>>(flags); 146*0b57cec5SDimitry Andric return nullptr; 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric // .MIPS.options section. 150*0b57cec5SDimitry Andric template <class ELFT> 151*0b57cec5SDimitry Andric MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo) 152*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), 153*0b57cec5SDimitry Andric reginfo(reginfo) { 154*0b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 155*0b57cec5SDimitry Andric } 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) { 158*0b57cec5SDimitry Andric auto *options = reinterpret_cast<Elf_Mips_Options *>(buf); 159*0b57cec5SDimitry Andric options->kind = ODK_REGINFO; 160*0b57cec5SDimitry Andric options->size = getSize(); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric if (!config->relocatable) 163*0b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 164*0b57cec5SDimitry Andric memcpy(buf + sizeof(Elf_Mips_Options), ®info, sizeof(reginfo)); 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric template <class ELFT> 168*0b57cec5SDimitry Andric MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { 169*0b57cec5SDimitry Andric // N64 ABI only. 170*0b57cec5SDimitry Andric if (!ELFT::Is64Bits) 171*0b57cec5SDimitry Andric return nullptr; 172*0b57cec5SDimitry Andric 173*0b57cec5SDimitry Andric std::vector<InputSectionBase *> sections; 174*0b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) 175*0b57cec5SDimitry Andric if (sec->type == SHT_MIPS_OPTIONS) 176*0b57cec5SDimitry Andric sections.push_back(sec); 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric if (sections.empty()) 179*0b57cec5SDimitry Andric return nullptr; 180*0b57cec5SDimitry Andric 181*0b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 182*0b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 183*0b57cec5SDimitry Andric sec->markDead(); 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric std::string filename = toString(sec->file); 186*0b57cec5SDimitry Andric ArrayRef<uint8_t> d = sec->data(); 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric while (!d.empty()) { 189*0b57cec5SDimitry Andric if (d.size() < sizeof(Elf_Mips_Options)) { 190*0b57cec5SDimitry Andric error(filename + ": invalid size of .MIPS.options section"); 191*0b57cec5SDimitry Andric break; 192*0b57cec5SDimitry Andric } 193*0b57cec5SDimitry Andric 194*0b57cec5SDimitry Andric auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data()); 195*0b57cec5SDimitry Andric if (opt->kind == ODK_REGINFO) { 196*0b57cec5SDimitry Andric reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask; 197*0b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value; 198*0b57cec5SDimitry Andric break; 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric if (!opt->size) 202*0b57cec5SDimitry Andric fatal(filename + ": zero option descriptor size"); 203*0b57cec5SDimitry Andric d = d.slice(opt->size); 204*0b57cec5SDimitry Andric } 205*0b57cec5SDimitry Andric }; 206*0b57cec5SDimitry Andric 207*0b57cec5SDimitry Andric return make<MipsOptionsSection<ELFT>>(reginfo); 208*0b57cec5SDimitry Andric } 209*0b57cec5SDimitry Andric 210*0b57cec5SDimitry Andric // MIPS .reginfo section. 211*0b57cec5SDimitry Andric template <class ELFT> 212*0b57cec5SDimitry Andric MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo) 213*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), 214*0b57cec5SDimitry Andric reginfo(reginfo) { 215*0b57cec5SDimitry Andric this->entsize = sizeof(Elf_Mips_RegInfo); 216*0b57cec5SDimitry Andric } 217*0b57cec5SDimitry Andric 218*0b57cec5SDimitry Andric template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) { 219*0b57cec5SDimitry Andric if (!config->relocatable) 220*0b57cec5SDimitry Andric reginfo.ri_gp_value = in.mipsGot->getGp(); 221*0b57cec5SDimitry Andric memcpy(buf, ®info, sizeof(reginfo)); 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric template <class ELFT> 225*0b57cec5SDimitry Andric MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { 226*0b57cec5SDimitry Andric // Section should be alive for O32 and N32 ABIs only. 227*0b57cec5SDimitry Andric if (ELFT::Is64Bits) 228*0b57cec5SDimitry Andric return nullptr; 229*0b57cec5SDimitry Andric 230*0b57cec5SDimitry Andric std::vector<InputSectionBase *> sections; 231*0b57cec5SDimitry Andric for (InputSectionBase *sec : inputSections) 232*0b57cec5SDimitry Andric if (sec->type == SHT_MIPS_REGINFO) 233*0b57cec5SDimitry Andric sections.push_back(sec); 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric if (sections.empty()) 236*0b57cec5SDimitry Andric return nullptr; 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric Elf_Mips_RegInfo reginfo = {}; 239*0b57cec5SDimitry Andric for (InputSectionBase *sec : sections) { 240*0b57cec5SDimitry Andric sec->markDead(); 241*0b57cec5SDimitry Andric 242*0b57cec5SDimitry Andric if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) { 243*0b57cec5SDimitry Andric error(toString(sec->file) + ": invalid size of .reginfo section"); 244*0b57cec5SDimitry Andric return nullptr; 245*0b57cec5SDimitry Andric } 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data()); 248*0b57cec5SDimitry Andric reginfo.ri_gprmask |= r->ri_gprmask; 249*0b57cec5SDimitry Andric sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value; 250*0b57cec5SDimitry Andric }; 251*0b57cec5SDimitry Andric 252*0b57cec5SDimitry Andric return make<MipsReginfoSection<ELFT>>(reginfo); 253*0b57cec5SDimitry Andric } 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric InputSection *elf::createInterpSection() { 256*0b57cec5SDimitry Andric // StringSaver guarantees that the returned string ends with '\0'. 257*0b57cec5SDimitry Andric StringRef s = saver.save(config->dynamicLinker); 258*0b57cec5SDimitry Andric ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1}; 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric auto *sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents, 261*0b57cec5SDimitry Andric ".interp"); 262*0b57cec5SDimitry Andric sec->markLive(); 263*0b57cec5SDimitry Andric return sec; 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value, 267*0b57cec5SDimitry Andric uint64_t size, InputSectionBase §ion) { 268*0b57cec5SDimitry Andric auto *s = make<Defined>(section.file, name, STB_LOCAL, STV_DEFAULT, type, 269*0b57cec5SDimitry Andric value, size, §ion); 270*0b57cec5SDimitry Andric if (in.symTab) 271*0b57cec5SDimitry Andric in.symTab->addSymbol(s); 272*0b57cec5SDimitry Andric return s; 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric static size_t getHashSize() { 276*0b57cec5SDimitry Andric switch (config->buildId) { 277*0b57cec5SDimitry Andric case BuildIdKind::Fast: 278*0b57cec5SDimitry Andric return 8; 279*0b57cec5SDimitry Andric case BuildIdKind::Md5: 280*0b57cec5SDimitry Andric case BuildIdKind::Uuid: 281*0b57cec5SDimitry Andric return 16; 282*0b57cec5SDimitry Andric case BuildIdKind::Sha1: 283*0b57cec5SDimitry Andric return 20; 284*0b57cec5SDimitry Andric case BuildIdKind::Hexstring: 285*0b57cec5SDimitry Andric return config->buildIdVector.size(); 286*0b57cec5SDimitry Andric default: 287*0b57cec5SDimitry Andric llvm_unreachable("unknown BuildIdKind"); 288*0b57cec5SDimitry Andric } 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric // This class represents a linker-synthesized .note.gnu.property section. 292*0b57cec5SDimitry Andric // 293*0b57cec5SDimitry Andric // In x86 and AArch64, object files may contain feature flags indicating the 294*0b57cec5SDimitry Andric // features that they have used. The flags are stored in a .note.gnu.property 295*0b57cec5SDimitry Andric // section. 296*0b57cec5SDimitry Andric // 297*0b57cec5SDimitry Andric // lld reads the sections from input files and merges them by computing AND of 298*0b57cec5SDimitry Andric // the flags. The result is written as a new .note.gnu.property section. 299*0b57cec5SDimitry Andric // 300*0b57cec5SDimitry Andric // If the flag is zero (which indicates that the intersection of the feature 301*0b57cec5SDimitry Andric // sets is empty, or some input files didn't have .note.gnu.property sections), 302*0b57cec5SDimitry Andric // we don't create this section. 303*0b57cec5SDimitry Andric GnuPropertySection::GnuPropertySection() 304*0b57cec5SDimitry Andric : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, 4, 305*0b57cec5SDimitry Andric ".note.gnu.property") {} 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric void GnuPropertySection::writeTo(uint8_t *buf) { 308*0b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 309*0b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 310*0b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric write32(buf, 4); // Name size 313*0b57cec5SDimitry Andric write32(buf + 4, config->is64 ? 16 : 12); // Content size 314*0b57cec5SDimitry Andric write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type 315*0b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 316*0b57cec5SDimitry Andric write32(buf + 16, featureAndType); // Feature type 317*0b57cec5SDimitry Andric write32(buf + 20, 4); // Feature size 318*0b57cec5SDimitry Andric write32(buf + 24, config->andFeatures); // Feature flags 319*0b57cec5SDimitry Andric if (config->is64) 320*0b57cec5SDimitry Andric write32(buf + 28, 0); // Padding 321*0b57cec5SDimitry Andric } 322*0b57cec5SDimitry Andric 323*0b57cec5SDimitry Andric size_t GnuPropertySection::getSize() const { return config->is64 ? 32 : 28; } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric BuildIdSection::BuildIdSection() 326*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"), 327*0b57cec5SDimitry Andric hashSize(getHashSize()) {} 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric void BuildIdSection::writeTo(uint8_t *buf) { 330*0b57cec5SDimitry Andric write32(buf, 4); // Name size 331*0b57cec5SDimitry Andric write32(buf + 4, hashSize); // Content size 332*0b57cec5SDimitry Andric write32(buf + 8, NT_GNU_BUILD_ID); // Type 333*0b57cec5SDimitry Andric memcpy(buf + 12, "GNU", 4); // Name string 334*0b57cec5SDimitry Andric hashBuf = buf + 16; 335*0b57cec5SDimitry Andric } 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) { 338*0b57cec5SDimitry Andric assert(buf.size() == hashSize); 339*0b57cec5SDimitry Andric memcpy(hashBuf, buf.data(), hashSize); 340*0b57cec5SDimitry Andric } 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment) 343*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) { 344*0b57cec5SDimitry Andric this->bss = true; 345*0b57cec5SDimitry Andric this->size = size; 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric EhFrameSection::EhFrameSection() 349*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric // Search for an existing CIE record or create a new one. 352*0b57cec5SDimitry Andric // CIE records from input object files are uniquified by their contents 353*0b57cec5SDimitry Andric // and where their relocations point to. 354*0b57cec5SDimitry Andric template <class ELFT, class RelTy> 355*0b57cec5SDimitry Andric CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) { 356*0b57cec5SDimitry Andric Symbol *personality = nullptr; 357*0b57cec5SDimitry Andric unsigned firstRelI = cie.firstRelocation; 358*0b57cec5SDimitry Andric if (firstRelI != (unsigned)-1) 359*0b57cec5SDimitry Andric personality = 360*0b57cec5SDimitry Andric &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]); 361*0b57cec5SDimitry Andric 362*0b57cec5SDimitry Andric // Search for an existing CIE by CIE contents/relocation target pair. 363*0b57cec5SDimitry Andric CieRecord *&rec = cieMap[{cie.data(), personality}]; 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric // If not found, create a new one. 366*0b57cec5SDimitry Andric if (!rec) { 367*0b57cec5SDimitry Andric rec = make<CieRecord>(); 368*0b57cec5SDimitry Andric rec->cie = &cie; 369*0b57cec5SDimitry Andric cieRecords.push_back(rec); 370*0b57cec5SDimitry Andric } 371*0b57cec5SDimitry Andric return rec; 372*0b57cec5SDimitry Andric } 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric // There is one FDE per function. Returns true if a given FDE 375*0b57cec5SDimitry Andric // points to a live function. 376*0b57cec5SDimitry Andric template <class ELFT, class RelTy> 377*0b57cec5SDimitry Andric bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) { 378*0b57cec5SDimitry Andric auto *sec = cast<EhInputSection>(fde.sec); 379*0b57cec5SDimitry Andric unsigned firstRelI = fde.firstRelocation; 380*0b57cec5SDimitry Andric 381*0b57cec5SDimitry Andric // An FDE should point to some function because FDEs are to describe 382*0b57cec5SDimitry Andric // functions. That's however not always the case due to an issue of 383*0b57cec5SDimitry Andric // ld.gold with -r. ld.gold may discard only functions and leave their 384*0b57cec5SDimitry Andric // corresponding FDEs, which results in creating bad .eh_frame sections. 385*0b57cec5SDimitry Andric // To deal with that, we ignore such FDEs. 386*0b57cec5SDimitry Andric if (firstRelI == (unsigned)-1) 387*0b57cec5SDimitry Andric return false; 388*0b57cec5SDimitry Andric 389*0b57cec5SDimitry Andric const RelTy &rel = rels[firstRelI]; 390*0b57cec5SDimitry Andric Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel); 391*0b57cec5SDimitry Andric 392*0b57cec5SDimitry Andric // FDEs for garbage-collected or merged-by-ICF sections, or sections in 393*0b57cec5SDimitry Andric // another partition, are dead. 394*0b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&b)) 395*0b57cec5SDimitry Andric if (SectionBase *sec = d->section) 396*0b57cec5SDimitry Andric return sec->partition == partition; 397*0b57cec5SDimitry Andric return false; 398*0b57cec5SDimitry Andric } 399*0b57cec5SDimitry Andric 400*0b57cec5SDimitry Andric // .eh_frame is a sequence of CIE or FDE records. In general, there 401*0b57cec5SDimitry Andric // is one CIE record per input object file which is followed by 402*0b57cec5SDimitry Andric // a list of FDEs. This function searches an existing CIE or create a new 403*0b57cec5SDimitry Andric // one and associates FDEs to the CIE. 404*0b57cec5SDimitry Andric template <class ELFT, class RelTy> 405*0b57cec5SDimitry Andric void EhFrameSection::addSectionAux(EhInputSection *sec, ArrayRef<RelTy> rels) { 406*0b57cec5SDimitry Andric offsetToCie.clear(); 407*0b57cec5SDimitry Andric for (EhSectionPiece &piece : sec->pieces) { 408*0b57cec5SDimitry Andric // The empty record is the end marker. 409*0b57cec5SDimitry Andric if (piece.size == 4) 410*0b57cec5SDimitry Andric return; 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric size_t offset = piece.inputOff; 413*0b57cec5SDimitry Andric uint32_t id = read32(piece.data().data() + 4); 414*0b57cec5SDimitry Andric if (id == 0) { 415*0b57cec5SDimitry Andric offsetToCie[offset] = addCie<ELFT>(piece, rels); 416*0b57cec5SDimitry Andric continue; 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric uint32_t cieOffset = offset + 4 - id; 420*0b57cec5SDimitry Andric CieRecord *rec = offsetToCie[cieOffset]; 421*0b57cec5SDimitry Andric if (!rec) 422*0b57cec5SDimitry Andric fatal(toString(sec) + ": invalid CIE reference"); 423*0b57cec5SDimitry Andric 424*0b57cec5SDimitry Andric if (!isFdeLive<ELFT>(piece, rels)) 425*0b57cec5SDimitry Andric continue; 426*0b57cec5SDimitry Andric rec->fdes.push_back(&piece); 427*0b57cec5SDimitry Andric numFdes++; 428*0b57cec5SDimitry Andric } 429*0b57cec5SDimitry Andric } 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric template <class ELFT> void EhFrameSection::addSection(InputSectionBase *c) { 432*0b57cec5SDimitry Andric auto *sec = cast<EhInputSection>(c); 433*0b57cec5SDimitry Andric sec->parent = this; 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric alignment = std::max(alignment, sec->alignment); 436*0b57cec5SDimitry Andric sections.push_back(sec); 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric for (auto *ds : sec->dependentSections) 439*0b57cec5SDimitry Andric dependentSections.push_back(ds); 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric if (sec->pieces.empty()) 442*0b57cec5SDimitry Andric return; 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric if (sec->areRelocsRela) 445*0b57cec5SDimitry Andric addSectionAux<ELFT>(sec, sec->template relas<ELFT>()); 446*0b57cec5SDimitry Andric else 447*0b57cec5SDimitry Andric addSectionAux<ELFT>(sec, sec->template rels<ELFT>()); 448*0b57cec5SDimitry Andric } 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) { 451*0b57cec5SDimitry Andric memcpy(buf, d.data(), d.size()); 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric size_t aligned = alignTo(d.size(), config->wordsize); 454*0b57cec5SDimitry Andric 455*0b57cec5SDimitry Andric // Zero-clear trailing padding if it exists. 456*0b57cec5SDimitry Andric memset(buf + d.size(), 0, aligned - d.size()); 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric // Fix the size field. -4 since size does not include the size field itself. 459*0b57cec5SDimitry Andric write32(buf, aligned - 4); 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric void EhFrameSection::finalizeContents() { 463*0b57cec5SDimitry Andric assert(!this->size); // Not finalized. 464*0b57cec5SDimitry Andric size_t off = 0; 465*0b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 466*0b57cec5SDimitry Andric rec->cie->outputOff = off; 467*0b57cec5SDimitry Andric off += alignTo(rec->cie->size, config->wordsize); 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 470*0b57cec5SDimitry Andric fde->outputOff = off; 471*0b57cec5SDimitry Andric off += alignTo(fde->size, config->wordsize); 472*0b57cec5SDimitry Andric } 473*0b57cec5SDimitry Andric } 474*0b57cec5SDimitry Andric 475*0b57cec5SDimitry Andric // The LSB standard does not allow a .eh_frame section with zero 476*0b57cec5SDimitry Andric // Call Frame Information records. glibc unwind-dw2-fde.c 477*0b57cec5SDimitry Andric // classify_object_over_fdes expects there is a CIE record length 0 as a 478*0b57cec5SDimitry Andric // terminator. Thus we add one unconditionally. 479*0b57cec5SDimitry Andric off += 4; 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric this->size = off; 482*0b57cec5SDimitry Andric } 483*0b57cec5SDimitry Andric 484*0b57cec5SDimitry Andric // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table 485*0b57cec5SDimitry Andric // to get an FDE from an address to which FDE is applied. This function 486*0b57cec5SDimitry Andric // returns a list of such pairs. 487*0b57cec5SDimitry Andric std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const { 488*0b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 489*0b57cec5SDimitry Andric std::vector<FdeData> ret; 490*0b57cec5SDimitry Andric 491*0b57cec5SDimitry Andric uint64_t va = getPartition().ehFrameHdr->getVA(); 492*0b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 493*0b57cec5SDimitry Andric uint8_t enc = getFdeEncoding(rec->cie); 494*0b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 495*0b57cec5SDimitry Andric uint64_t pc = getFdePc(buf, fde->outputOff, enc); 496*0b57cec5SDimitry Andric uint64_t fdeVA = getParent()->addr + fde->outputOff; 497*0b57cec5SDimitry Andric if (!isInt<32>(pc - va)) 498*0b57cec5SDimitry Andric fatal(toString(fde->sec) + ": PC offset is too large: 0x" + 499*0b57cec5SDimitry Andric Twine::utohexstr(pc - va)); 500*0b57cec5SDimitry Andric ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)}); 501*0b57cec5SDimitry Andric } 502*0b57cec5SDimitry Andric } 503*0b57cec5SDimitry Andric 504*0b57cec5SDimitry Andric // Sort the FDE list by their PC and uniqueify. Usually there is only 505*0b57cec5SDimitry Andric // one FDE for a PC (i.e. function), but if ICF merges two functions 506*0b57cec5SDimitry Andric // into one, there can be more than one FDEs pointing to the address. 507*0b57cec5SDimitry Andric auto less = [](const FdeData &a, const FdeData &b) { 508*0b57cec5SDimitry Andric return a.pcRel < b.pcRel; 509*0b57cec5SDimitry Andric }; 510*0b57cec5SDimitry Andric llvm::stable_sort(ret, less); 511*0b57cec5SDimitry Andric auto eq = [](const FdeData &a, const FdeData &b) { 512*0b57cec5SDimitry Andric return a.pcRel == b.pcRel; 513*0b57cec5SDimitry Andric }; 514*0b57cec5SDimitry Andric ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end()); 515*0b57cec5SDimitry Andric 516*0b57cec5SDimitry Andric return ret; 517*0b57cec5SDimitry Andric } 518*0b57cec5SDimitry Andric 519*0b57cec5SDimitry Andric static uint64_t readFdeAddr(uint8_t *buf, int size) { 520*0b57cec5SDimitry Andric switch (size) { 521*0b57cec5SDimitry Andric case DW_EH_PE_udata2: 522*0b57cec5SDimitry Andric return read16(buf); 523*0b57cec5SDimitry Andric case DW_EH_PE_sdata2: 524*0b57cec5SDimitry Andric return (int16_t)read16(buf); 525*0b57cec5SDimitry Andric case DW_EH_PE_udata4: 526*0b57cec5SDimitry Andric return read32(buf); 527*0b57cec5SDimitry Andric case DW_EH_PE_sdata4: 528*0b57cec5SDimitry Andric return (int32_t)read32(buf); 529*0b57cec5SDimitry Andric case DW_EH_PE_udata8: 530*0b57cec5SDimitry Andric case DW_EH_PE_sdata8: 531*0b57cec5SDimitry Andric return read64(buf); 532*0b57cec5SDimitry Andric case DW_EH_PE_absptr: 533*0b57cec5SDimitry Andric return readUint(buf); 534*0b57cec5SDimitry Andric } 535*0b57cec5SDimitry Andric fatal("unknown FDE size encoding"); 536*0b57cec5SDimitry Andric } 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. 539*0b57cec5SDimitry Andric // We need it to create .eh_frame_hdr section. 540*0b57cec5SDimitry Andric uint64_t EhFrameSection::getFdePc(uint8_t *buf, size_t fdeOff, 541*0b57cec5SDimitry Andric uint8_t enc) const { 542*0b57cec5SDimitry Andric // The starting address to which this FDE applies is 543*0b57cec5SDimitry Andric // stored at FDE + 8 byte. 544*0b57cec5SDimitry Andric size_t off = fdeOff + 8; 545*0b57cec5SDimitry Andric uint64_t addr = readFdeAddr(buf + off, enc & 0xf); 546*0b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_absptr) 547*0b57cec5SDimitry Andric return addr; 548*0b57cec5SDimitry Andric if ((enc & 0x70) == DW_EH_PE_pcrel) 549*0b57cec5SDimitry Andric return addr + getParent()->addr + off; 550*0b57cec5SDimitry Andric fatal("unknown FDE size relative encoding"); 551*0b57cec5SDimitry Andric } 552*0b57cec5SDimitry Andric 553*0b57cec5SDimitry Andric void EhFrameSection::writeTo(uint8_t *buf) { 554*0b57cec5SDimitry Andric // Write CIE and FDE records. 555*0b57cec5SDimitry Andric for (CieRecord *rec : cieRecords) { 556*0b57cec5SDimitry Andric size_t cieOffset = rec->cie->outputOff; 557*0b57cec5SDimitry Andric writeCieFde(buf + cieOffset, rec->cie->data()); 558*0b57cec5SDimitry Andric 559*0b57cec5SDimitry Andric for (EhSectionPiece *fde : rec->fdes) { 560*0b57cec5SDimitry Andric size_t off = fde->outputOff; 561*0b57cec5SDimitry Andric writeCieFde(buf + off, fde->data()); 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric // FDE's second word should have the offset to an associated CIE. 564*0b57cec5SDimitry Andric // Write it. 565*0b57cec5SDimitry Andric write32(buf + off + 4, off + 4 - cieOffset); 566*0b57cec5SDimitry Andric } 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric // Apply relocations. .eh_frame section contents are not contiguous 570*0b57cec5SDimitry Andric // in the output buffer, but relocateAlloc() still works because 571*0b57cec5SDimitry Andric // getOffset() takes care of discontiguous section pieces. 572*0b57cec5SDimitry Andric for (EhInputSection *s : sections) 573*0b57cec5SDimitry Andric s->relocateAlloc(buf, nullptr); 574*0b57cec5SDimitry Andric 575*0b57cec5SDimitry Andric if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent()) 576*0b57cec5SDimitry Andric getPartition().ehFrameHdr->write(); 577*0b57cec5SDimitry Andric } 578*0b57cec5SDimitry Andric 579*0b57cec5SDimitry Andric GotSection::GotSection() 580*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 581*0b57cec5SDimitry Andric ".got") { 582*0b57cec5SDimitry Andric // If ElfSym::globalOffsetTable is relative to .got and is referenced, 583*0b57cec5SDimitry Andric // increase numEntries by the number of entries used to emit 584*0b57cec5SDimitry Andric // ElfSym::globalOffsetTable. 585*0b57cec5SDimitry Andric if (ElfSym::globalOffsetTable && !target->gotBaseSymInGotPlt) 586*0b57cec5SDimitry Andric numEntries += target->gotHeaderEntriesNum; 587*0b57cec5SDimitry Andric } 588*0b57cec5SDimitry Andric 589*0b57cec5SDimitry Andric void GotSection::addEntry(Symbol &sym) { 590*0b57cec5SDimitry Andric sym.gotIndex = numEntries; 591*0b57cec5SDimitry Andric ++numEntries; 592*0b57cec5SDimitry Andric } 593*0b57cec5SDimitry Andric 594*0b57cec5SDimitry Andric bool GotSection::addDynTlsEntry(Symbol &sym) { 595*0b57cec5SDimitry Andric if (sym.globalDynIndex != -1U) 596*0b57cec5SDimitry Andric return false; 597*0b57cec5SDimitry Andric sym.globalDynIndex = numEntries; 598*0b57cec5SDimitry Andric // Global Dynamic TLS entries take two GOT slots. 599*0b57cec5SDimitry Andric numEntries += 2; 600*0b57cec5SDimitry Andric return true; 601*0b57cec5SDimitry Andric } 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric // Reserves TLS entries for a TLS module ID and a TLS block offset. 604*0b57cec5SDimitry Andric // In total it takes two GOT slots. 605*0b57cec5SDimitry Andric bool GotSection::addTlsIndex() { 606*0b57cec5SDimitry Andric if (tlsIndexOff != uint32_t(-1)) 607*0b57cec5SDimitry Andric return false; 608*0b57cec5SDimitry Andric tlsIndexOff = numEntries * config->wordsize; 609*0b57cec5SDimitry Andric numEntries += 2; 610*0b57cec5SDimitry Andric return true; 611*0b57cec5SDimitry Andric } 612*0b57cec5SDimitry Andric 613*0b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const { 614*0b57cec5SDimitry Andric return this->getVA() + b.globalDynIndex * config->wordsize; 615*0b57cec5SDimitry Andric } 616*0b57cec5SDimitry Andric 617*0b57cec5SDimitry Andric uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const { 618*0b57cec5SDimitry Andric return b.globalDynIndex * config->wordsize; 619*0b57cec5SDimitry Andric } 620*0b57cec5SDimitry Andric 621*0b57cec5SDimitry Andric void GotSection::finalizeContents() { 622*0b57cec5SDimitry Andric size = numEntries * config->wordsize; 623*0b57cec5SDimitry Andric } 624*0b57cec5SDimitry Andric 625*0b57cec5SDimitry Andric bool GotSection::isNeeded() const { 626*0b57cec5SDimitry Andric // We need to emit a GOT even if it's empty if there's a relocation that is 627*0b57cec5SDimitry Andric // relative to GOT(such as GOTOFFREL). 628*0b57cec5SDimitry Andric return numEntries || hasGotOffRel; 629*0b57cec5SDimitry Andric } 630*0b57cec5SDimitry Andric 631*0b57cec5SDimitry Andric void GotSection::writeTo(uint8_t *buf) { 632*0b57cec5SDimitry Andric // Buf points to the start of this section's buffer, 633*0b57cec5SDimitry Andric // whereas InputSectionBase::relocateAlloc() expects its argument 634*0b57cec5SDimitry Andric // to point to the start of the output section. 635*0b57cec5SDimitry Andric target->writeGotHeader(buf); 636*0b57cec5SDimitry Andric relocateAlloc(buf - outSecOff, buf - outSecOff + size); 637*0b57cec5SDimitry Andric } 638*0b57cec5SDimitry Andric 639*0b57cec5SDimitry Andric static uint64_t getMipsPageAddr(uint64_t addr) { 640*0b57cec5SDimitry Andric return (addr + 0x8000) & ~0xffff; 641*0b57cec5SDimitry Andric } 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric static uint64_t getMipsPageCount(uint64_t size) { 644*0b57cec5SDimitry Andric return (size + 0xfffe) / 0xffff + 1; 645*0b57cec5SDimitry Andric } 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric MipsGotSection::MipsGotSection() 648*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, 649*0b57cec5SDimitry Andric ".got") {} 650*0b57cec5SDimitry Andric 651*0b57cec5SDimitry Andric void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend, 652*0b57cec5SDimitry Andric RelExpr expr) { 653*0b57cec5SDimitry Andric FileGot &g = getGot(file); 654*0b57cec5SDimitry Andric if (expr == R_MIPS_GOT_LOCAL_PAGE) { 655*0b57cec5SDimitry Andric if (const OutputSection *os = sym.getOutputSection()) 656*0b57cec5SDimitry Andric g.pagesMap.insert({os, {}}); 657*0b57cec5SDimitry Andric else 658*0b57cec5SDimitry Andric g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0}); 659*0b57cec5SDimitry Andric } else if (sym.isTls()) 660*0b57cec5SDimitry Andric g.tls.insert({&sym, 0}); 661*0b57cec5SDimitry Andric else if (sym.isPreemptible && expr == R_ABS) 662*0b57cec5SDimitry Andric g.relocs.insert({&sym, 0}); 663*0b57cec5SDimitry Andric else if (sym.isPreemptible) 664*0b57cec5SDimitry Andric g.global.insert({&sym, 0}); 665*0b57cec5SDimitry Andric else if (expr == R_MIPS_GOT_OFF32) 666*0b57cec5SDimitry Andric g.local32.insert({{&sym, addend}, 0}); 667*0b57cec5SDimitry Andric else 668*0b57cec5SDimitry Andric g.local16.insert({{&sym, addend}, 0}); 669*0b57cec5SDimitry Andric } 670*0b57cec5SDimitry Andric 671*0b57cec5SDimitry Andric void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) { 672*0b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({&sym, 0}); 673*0b57cec5SDimitry Andric } 674*0b57cec5SDimitry Andric 675*0b57cec5SDimitry Andric void MipsGotSection::addTlsIndex(InputFile &file) { 676*0b57cec5SDimitry Andric getGot(file).dynTlsSymbols.insert({nullptr, 0}); 677*0b57cec5SDimitry Andric } 678*0b57cec5SDimitry Andric 679*0b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getEntriesNum() const { 680*0b57cec5SDimitry Andric return getPageEntriesNum() + local16.size() + global.size() + relocs.size() + 681*0b57cec5SDimitry Andric tls.size() + dynTlsSymbols.size() * 2; 682*0b57cec5SDimitry Andric } 683*0b57cec5SDimitry Andric 684*0b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getPageEntriesNum() const { 685*0b57cec5SDimitry Andric size_t num = 0; 686*0b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap) 687*0b57cec5SDimitry Andric num += p.second.count; 688*0b57cec5SDimitry Andric return num; 689*0b57cec5SDimitry Andric } 690*0b57cec5SDimitry Andric 691*0b57cec5SDimitry Andric size_t MipsGotSection::FileGot::getIndexedEntriesNum() const { 692*0b57cec5SDimitry Andric size_t count = getPageEntriesNum() + local16.size() + global.size(); 693*0b57cec5SDimitry Andric // If there are relocation-only entries in the GOT, TLS entries 694*0b57cec5SDimitry Andric // are allocated after them. TLS entries should be addressable 695*0b57cec5SDimitry Andric // by 16-bit index so count both reloc-only and TLS entries. 696*0b57cec5SDimitry Andric if (!tls.empty() || !dynTlsSymbols.empty()) 697*0b57cec5SDimitry Andric count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2; 698*0b57cec5SDimitry Andric return count; 699*0b57cec5SDimitry Andric } 700*0b57cec5SDimitry Andric 701*0b57cec5SDimitry Andric MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) { 702*0b57cec5SDimitry Andric if (!f.mipsGotIndex.hasValue()) { 703*0b57cec5SDimitry Andric gots.emplace_back(); 704*0b57cec5SDimitry Andric gots.back().file = &f; 705*0b57cec5SDimitry Andric f.mipsGotIndex = gots.size() - 1; 706*0b57cec5SDimitry Andric } 707*0b57cec5SDimitry Andric return gots[*f.mipsGotIndex]; 708*0b57cec5SDimitry Andric } 709*0b57cec5SDimitry Andric 710*0b57cec5SDimitry Andric uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f, 711*0b57cec5SDimitry Andric const Symbol &sym, 712*0b57cec5SDimitry Andric int64_t addend) const { 713*0b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 714*0b57cec5SDimitry Andric uint64_t index = 0; 715*0b57cec5SDimitry Andric if (const OutputSection *outSec = sym.getOutputSection()) { 716*0b57cec5SDimitry Andric uint64_t secAddr = getMipsPageAddr(outSec->addr); 717*0b57cec5SDimitry Andric uint64_t symAddr = getMipsPageAddr(sym.getVA(addend)); 718*0b57cec5SDimitry Andric index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff; 719*0b57cec5SDimitry Andric } else { 720*0b57cec5SDimitry Andric index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))}); 721*0b57cec5SDimitry Andric } 722*0b57cec5SDimitry Andric return index * config->wordsize; 723*0b57cec5SDimitry Andric } 724*0b57cec5SDimitry Andric 725*0b57cec5SDimitry Andric uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s, 726*0b57cec5SDimitry Andric int64_t addend) const { 727*0b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 728*0b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 729*0b57cec5SDimitry Andric if (sym->isTls()) 730*0b57cec5SDimitry Andric return g.tls.lookup(sym) * config->wordsize; 731*0b57cec5SDimitry Andric if (sym->isPreemptible) 732*0b57cec5SDimitry Andric return g.global.lookup(sym) * config->wordsize; 733*0b57cec5SDimitry Andric return g.local16.lookup({sym, addend}) * config->wordsize; 734*0b57cec5SDimitry Andric } 735*0b57cec5SDimitry Andric 736*0b57cec5SDimitry Andric uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const { 737*0b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 738*0b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(nullptr) * config->wordsize; 739*0b57cec5SDimitry Andric } 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f, 742*0b57cec5SDimitry Andric const Symbol &s) const { 743*0b57cec5SDimitry Andric const FileGot &g = gots[*f->mipsGotIndex]; 744*0b57cec5SDimitry Andric Symbol *sym = const_cast<Symbol *>(&s); 745*0b57cec5SDimitry Andric return g.dynTlsSymbols.lookup(sym) * config->wordsize; 746*0b57cec5SDimitry Andric } 747*0b57cec5SDimitry Andric 748*0b57cec5SDimitry Andric const Symbol *MipsGotSection::getFirstGlobalEntry() const { 749*0b57cec5SDimitry Andric if (gots.empty()) 750*0b57cec5SDimitry Andric return nullptr; 751*0b57cec5SDimitry Andric const FileGot &primGot = gots.front(); 752*0b57cec5SDimitry Andric if (!primGot.global.empty()) 753*0b57cec5SDimitry Andric return primGot.global.front().first; 754*0b57cec5SDimitry Andric if (!primGot.relocs.empty()) 755*0b57cec5SDimitry Andric return primGot.relocs.front().first; 756*0b57cec5SDimitry Andric return nullptr; 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric 759*0b57cec5SDimitry Andric unsigned MipsGotSection::getLocalEntriesNum() const { 760*0b57cec5SDimitry Andric if (gots.empty()) 761*0b57cec5SDimitry Andric return headerEntriesNum; 762*0b57cec5SDimitry Andric return headerEntriesNum + gots.front().getPageEntriesNum() + 763*0b57cec5SDimitry Andric gots.front().local16.size(); 764*0b57cec5SDimitry Andric } 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) { 767*0b57cec5SDimitry Andric FileGot tmp = dst; 768*0b57cec5SDimitry Andric set_union(tmp.pagesMap, src.pagesMap); 769*0b57cec5SDimitry Andric set_union(tmp.local16, src.local16); 770*0b57cec5SDimitry Andric set_union(tmp.global, src.global); 771*0b57cec5SDimitry Andric set_union(tmp.relocs, src.relocs); 772*0b57cec5SDimitry Andric set_union(tmp.tls, src.tls); 773*0b57cec5SDimitry Andric set_union(tmp.dynTlsSymbols, src.dynTlsSymbols); 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric size_t count = isPrimary ? headerEntriesNum : 0; 776*0b57cec5SDimitry Andric count += tmp.getIndexedEntriesNum(); 777*0b57cec5SDimitry Andric 778*0b57cec5SDimitry Andric if (count * config->wordsize > config->mipsGotSize) 779*0b57cec5SDimitry Andric return false; 780*0b57cec5SDimitry Andric 781*0b57cec5SDimitry Andric std::swap(tmp, dst); 782*0b57cec5SDimitry Andric return true; 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric 785*0b57cec5SDimitry Andric void MipsGotSection::finalizeContents() { updateAllocSize(); } 786*0b57cec5SDimitry Andric 787*0b57cec5SDimitry Andric bool MipsGotSection::updateAllocSize() { 788*0b57cec5SDimitry Andric size = headerEntriesNum * config->wordsize; 789*0b57cec5SDimitry Andric for (const FileGot &g : gots) 790*0b57cec5SDimitry Andric size += g.getEntriesNum() * config->wordsize; 791*0b57cec5SDimitry Andric return false; 792*0b57cec5SDimitry Andric } 793*0b57cec5SDimitry Andric 794*0b57cec5SDimitry Andric void MipsGotSection::build() { 795*0b57cec5SDimitry Andric if (gots.empty()) 796*0b57cec5SDimitry Andric return; 797*0b57cec5SDimitry Andric 798*0b57cec5SDimitry Andric std::vector<FileGot> mergedGots(1); 799*0b57cec5SDimitry Andric 800*0b57cec5SDimitry Andric // For each GOT move non-preemptible symbols from the `Global` 801*0b57cec5SDimitry Andric // to `Local16` list. Preemptible symbol might become non-preemptible 802*0b57cec5SDimitry Andric // one if, for example, it gets a related copy relocation. 803*0b57cec5SDimitry Andric for (FileGot &got : gots) { 804*0b57cec5SDimitry Andric for (auto &p: got.global) 805*0b57cec5SDimitry Andric if (!p.first->isPreemptible) 806*0b57cec5SDimitry Andric got.local16.insert({{p.first, 0}, 0}); 807*0b57cec5SDimitry Andric got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) { 808*0b57cec5SDimitry Andric return !p.first->isPreemptible; 809*0b57cec5SDimitry Andric }); 810*0b57cec5SDimitry Andric } 811*0b57cec5SDimitry Andric 812*0b57cec5SDimitry Andric // For each GOT remove "reloc-only" entry if there is "global" 813*0b57cec5SDimitry Andric // entry for the same symbol. And add local entries which indexed 814*0b57cec5SDimitry Andric // using 32-bit value at the end of 16-bit entries. 815*0b57cec5SDimitry Andric for (FileGot &got : gots) { 816*0b57cec5SDimitry Andric got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 817*0b57cec5SDimitry Andric return got.global.count(p.first); 818*0b57cec5SDimitry Andric }); 819*0b57cec5SDimitry Andric set_union(got.local16, got.local32); 820*0b57cec5SDimitry Andric got.local32.clear(); 821*0b57cec5SDimitry Andric } 822*0b57cec5SDimitry Andric 823*0b57cec5SDimitry Andric // Evaluate number of "reloc-only" entries in the resulting GOT. 824*0b57cec5SDimitry Andric // To do that put all unique "reloc-only" and "global" entries 825*0b57cec5SDimitry Andric // from all GOTs to the future primary GOT. 826*0b57cec5SDimitry Andric FileGot *primGot = &mergedGots.front(); 827*0b57cec5SDimitry Andric for (FileGot &got : gots) { 828*0b57cec5SDimitry Andric set_union(primGot->relocs, got.global); 829*0b57cec5SDimitry Andric set_union(primGot->relocs, got.relocs); 830*0b57cec5SDimitry Andric got.relocs.clear(); 831*0b57cec5SDimitry Andric } 832*0b57cec5SDimitry Andric 833*0b57cec5SDimitry Andric // Evaluate number of "page" entries in each GOT. 834*0b57cec5SDimitry Andric for (FileGot &got : gots) { 835*0b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 836*0b57cec5SDimitry Andric got.pagesMap) { 837*0b57cec5SDimitry Andric const OutputSection *os = p.first; 838*0b57cec5SDimitry Andric uint64_t secSize = 0; 839*0b57cec5SDimitry Andric for (BaseCommand *cmd : os->sectionCommands) { 840*0b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) 841*0b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 842*0b57cec5SDimitry Andric uint64_t off = alignTo(secSize, isec->alignment); 843*0b57cec5SDimitry Andric secSize = off + isec->getSize(); 844*0b57cec5SDimitry Andric } 845*0b57cec5SDimitry Andric } 846*0b57cec5SDimitry Andric p.second.count = getMipsPageCount(secSize); 847*0b57cec5SDimitry Andric } 848*0b57cec5SDimitry Andric } 849*0b57cec5SDimitry Andric 850*0b57cec5SDimitry Andric // Merge GOTs. Try to join as much as possible GOTs but do not exceed 851*0b57cec5SDimitry Andric // maximum GOT size. At first, try to fill the primary GOT because 852*0b57cec5SDimitry Andric // the primary GOT can be accessed in the most effective way. If it 853*0b57cec5SDimitry Andric // is not possible, try to fill the last GOT in the list, and finally 854*0b57cec5SDimitry Andric // create a new GOT if both attempts failed. 855*0b57cec5SDimitry Andric for (FileGot &srcGot : gots) { 856*0b57cec5SDimitry Andric InputFile *file = srcGot.file; 857*0b57cec5SDimitry Andric if (tryMergeGots(mergedGots.front(), srcGot, true)) { 858*0b57cec5SDimitry Andric file->mipsGotIndex = 0; 859*0b57cec5SDimitry Andric } else { 860*0b57cec5SDimitry Andric // If this is the first time we failed to merge with the primary GOT, 861*0b57cec5SDimitry Andric // MergedGots.back() will also be the primary GOT. We must make sure not 862*0b57cec5SDimitry Andric // to try to merge again with isPrimary=false, as otherwise, if the 863*0b57cec5SDimitry Andric // inputs are just right, we could allow the primary GOT to become 1 or 2 864*0b57cec5SDimitry Andric // words bigger due to ignoring the header size. 865*0b57cec5SDimitry Andric if (mergedGots.size() == 1 || 866*0b57cec5SDimitry Andric !tryMergeGots(mergedGots.back(), srcGot, false)) { 867*0b57cec5SDimitry Andric mergedGots.emplace_back(); 868*0b57cec5SDimitry Andric std::swap(mergedGots.back(), srcGot); 869*0b57cec5SDimitry Andric } 870*0b57cec5SDimitry Andric file->mipsGotIndex = mergedGots.size() - 1; 871*0b57cec5SDimitry Andric } 872*0b57cec5SDimitry Andric } 873*0b57cec5SDimitry Andric std::swap(gots, mergedGots); 874*0b57cec5SDimitry Andric 875*0b57cec5SDimitry Andric // Reduce number of "reloc-only" entries in the primary GOT 876*0b57cec5SDimitry Andric // by substracting "global" entries exist in the primary GOT. 877*0b57cec5SDimitry Andric primGot = &gots.front(); 878*0b57cec5SDimitry Andric primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) { 879*0b57cec5SDimitry Andric return primGot->global.count(p.first); 880*0b57cec5SDimitry Andric }); 881*0b57cec5SDimitry Andric 882*0b57cec5SDimitry Andric // Calculate indexes for each GOT entry. 883*0b57cec5SDimitry Andric size_t index = headerEntriesNum; 884*0b57cec5SDimitry Andric for (FileGot &got : gots) { 885*0b57cec5SDimitry Andric got.startIndex = &got == primGot ? 0 : index; 886*0b57cec5SDimitry Andric for (std::pair<const OutputSection *, FileGot::PageBlock> &p : 887*0b57cec5SDimitry Andric got.pagesMap) { 888*0b57cec5SDimitry Andric // For each output section referenced by GOT page relocations calculate 889*0b57cec5SDimitry Andric // and save into pagesMap an upper bound of MIPS GOT entries required 890*0b57cec5SDimitry Andric // to store page addresses of local symbols. We assume the worst case - 891*0b57cec5SDimitry Andric // each 64kb page of the output section has at least one GOT relocation 892*0b57cec5SDimitry Andric // against it. And take in account the case when the section intersects 893*0b57cec5SDimitry Andric // page boundaries. 894*0b57cec5SDimitry Andric p.second.firstIndex = index; 895*0b57cec5SDimitry Andric index += p.second.count; 896*0b57cec5SDimitry Andric } 897*0b57cec5SDimitry Andric for (auto &p: got.local16) 898*0b57cec5SDimitry Andric p.second = index++; 899*0b57cec5SDimitry Andric for (auto &p: got.global) 900*0b57cec5SDimitry Andric p.second = index++; 901*0b57cec5SDimitry Andric for (auto &p: got.relocs) 902*0b57cec5SDimitry Andric p.second = index++; 903*0b57cec5SDimitry Andric for (auto &p: got.tls) 904*0b57cec5SDimitry Andric p.second = index++; 905*0b57cec5SDimitry Andric for (auto &p: got.dynTlsSymbols) { 906*0b57cec5SDimitry Andric p.second = index; 907*0b57cec5SDimitry Andric index += 2; 908*0b57cec5SDimitry Andric } 909*0b57cec5SDimitry Andric } 910*0b57cec5SDimitry Andric 911*0b57cec5SDimitry Andric // Update Symbol::gotIndex field to use this 912*0b57cec5SDimitry Andric // value later in the `sortMipsSymbols` function. 913*0b57cec5SDimitry Andric for (auto &p : primGot->global) 914*0b57cec5SDimitry Andric p.first->gotIndex = p.second; 915*0b57cec5SDimitry Andric for (auto &p : primGot->relocs) 916*0b57cec5SDimitry Andric p.first->gotIndex = p.second; 917*0b57cec5SDimitry Andric 918*0b57cec5SDimitry Andric // Create dynamic relocations. 919*0b57cec5SDimitry Andric for (FileGot &got : gots) { 920*0b57cec5SDimitry Andric // Create dynamic relocations for TLS entries. 921*0b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.tls) { 922*0b57cec5SDimitry Andric Symbol *s = p.first; 923*0b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 924*0b57cec5SDimitry Andric if (s->isPreemptible) 925*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsGotRel, this, offset, s); 926*0b57cec5SDimitry Andric } 927*0b57cec5SDimitry Andric for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) { 928*0b57cec5SDimitry Andric Symbol *s = p.first; 929*0b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 930*0b57cec5SDimitry Andric if (s == nullptr) { 931*0b57cec5SDimitry Andric if (!config->isPic) 932*0b57cec5SDimitry Andric continue; 933*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s); 934*0b57cec5SDimitry Andric } else { 935*0b57cec5SDimitry Andric // When building a shared library we still need a dynamic relocation 936*0b57cec5SDimitry Andric // for the module index. Therefore only checking for 937*0b57cec5SDimitry Andric // S->isPreemptible is not sufficient (this happens e.g. for 938*0b57cec5SDimitry Andric // thread-locals that have been marked as local through a linker script) 939*0b57cec5SDimitry Andric if (!s->isPreemptible && !config->isPic) 940*0b57cec5SDimitry Andric continue; 941*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s); 942*0b57cec5SDimitry Andric // However, we can skip writing the TLS offset reloc for non-preemptible 943*0b57cec5SDimitry Andric // symbols since it is known even in shared libraries 944*0b57cec5SDimitry Andric if (!s->isPreemptible) 945*0b57cec5SDimitry Andric continue; 946*0b57cec5SDimitry Andric offset += config->wordsize; 947*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->tlsOffsetRel, this, offset, s); 948*0b57cec5SDimitry Andric } 949*0b57cec5SDimitry Andric } 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric // Do not create dynamic relocations for non-TLS 952*0b57cec5SDimitry Andric // entries in the primary GOT. 953*0b57cec5SDimitry Andric if (&got == primGot) 954*0b57cec5SDimitry Andric continue; 955*0b57cec5SDimitry Andric 956*0b57cec5SDimitry Andric // Dynamic relocations for "global" entries. 957*0b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : got.global) { 958*0b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 959*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc(target->relativeRel, this, offset, p.first); 960*0b57cec5SDimitry Andric } 961*0b57cec5SDimitry Andric if (!config->isPic) 962*0b57cec5SDimitry Andric continue; 963*0b57cec5SDimitry Andric // Dynamic relocations for "local" entries in case of PIC. 964*0b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 965*0b57cec5SDimitry Andric got.pagesMap) { 966*0b57cec5SDimitry Andric size_t pageCount = l.second.count; 967*0b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) { 968*0b57cec5SDimitry Andric uint64_t offset = (l.second.firstIndex + pi) * config->wordsize; 969*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first, 970*0b57cec5SDimitry Andric int64_t(pi * 0x10000)}); 971*0b57cec5SDimitry Andric } 972*0b57cec5SDimitry Andric } 973*0b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : got.local16) { 974*0b57cec5SDimitry Andric uint64_t offset = p.second * config->wordsize; 975*0b57cec5SDimitry Andric mainPart->relaDyn->addReloc({target->relativeRel, this, offset, true, 976*0b57cec5SDimitry Andric p.first.first, p.first.second}); 977*0b57cec5SDimitry Andric } 978*0b57cec5SDimitry Andric } 979*0b57cec5SDimitry Andric } 980*0b57cec5SDimitry Andric 981*0b57cec5SDimitry Andric bool MipsGotSection::isNeeded() const { 982*0b57cec5SDimitry Andric // We add the .got section to the result for dynamic MIPS target because 983*0b57cec5SDimitry Andric // its address and properties are mentioned in the .dynamic section. 984*0b57cec5SDimitry Andric return !config->relocatable; 985*0b57cec5SDimitry Andric } 986*0b57cec5SDimitry Andric 987*0b57cec5SDimitry Andric uint64_t MipsGotSection::getGp(const InputFile *f) const { 988*0b57cec5SDimitry Andric // For files without related GOT or files refer a primary GOT 989*0b57cec5SDimitry Andric // returns "common" _gp value. For secondary GOTs calculate 990*0b57cec5SDimitry Andric // individual _gp values. 991*0b57cec5SDimitry Andric if (!f || !f->mipsGotIndex.hasValue() || *f->mipsGotIndex == 0) 992*0b57cec5SDimitry Andric return ElfSym::mipsGp->getVA(0); 993*0b57cec5SDimitry Andric return getVA() + gots[*f->mipsGotIndex].startIndex * config->wordsize + 994*0b57cec5SDimitry Andric 0x7ff0; 995*0b57cec5SDimitry Andric } 996*0b57cec5SDimitry Andric 997*0b57cec5SDimitry Andric void MipsGotSection::writeTo(uint8_t *buf) { 998*0b57cec5SDimitry Andric // Set the MSB of the second GOT slot. This is not required by any 999*0b57cec5SDimitry Andric // MIPS ABI documentation, though. 1000*0b57cec5SDimitry Andric // 1001*0b57cec5SDimitry Andric // There is a comment in glibc saying that "The MSB of got[1] of a 1002*0b57cec5SDimitry Andric // gnu object is set to identify gnu objects," and in GNU gold it 1003*0b57cec5SDimitry Andric // says "the second entry will be used by some runtime loaders". 1004*0b57cec5SDimitry Andric // But how this field is being used is unclear. 1005*0b57cec5SDimitry Andric // 1006*0b57cec5SDimitry Andric // We are not really willing to mimic other linkers behaviors 1007*0b57cec5SDimitry Andric // without understanding why they do that, but because all files 1008*0b57cec5SDimitry Andric // generated by GNU tools have this special GOT value, and because 1009*0b57cec5SDimitry Andric // we've been doing this for years, it is probably a safe bet to 1010*0b57cec5SDimitry Andric // keep doing this for now. We really need to revisit this to see 1011*0b57cec5SDimitry Andric // if we had to do this. 1012*0b57cec5SDimitry Andric writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1)); 1013*0b57cec5SDimitry Andric for (const FileGot &g : gots) { 1014*0b57cec5SDimitry Andric auto write = [&](size_t i, const Symbol *s, int64_t a) { 1015*0b57cec5SDimitry Andric uint64_t va = a; 1016*0b57cec5SDimitry Andric if (s) 1017*0b57cec5SDimitry Andric va = s->getVA(a); 1018*0b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, va); 1019*0b57cec5SDimitry Andric }; 1020*0b57cec5SDimitry Andric // Write 'page address' entries to the local part of the GOT. 1021*0b57cec5SDimitry Andric for (const std::pair<const OutputSection *, FileGot::PageBlock> &l : 1022*0b57cec5SDimitry Andric g.pagesMap) { 1023*0b57cec5SDimitry Andric size_t pageCount = l.second.count; 1024*0b57cec5SDimitry Andric uint64_t firstPageAddr = getMipsPageAddr(l.first->addr); 1025*0b57cec5SDimitry Andric for (size_t pi = 0; pi < pageCount; ++pi) 1026*0b57cec5SDimitry Andric write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000); 1027*0b57cec5SDimitry Andric } 1028*0b57cec5SDimitry Andric // Local, global, TLS, reloc-only entries. 1029*0b57cec5SDimitry Andric // If TLS entry has a corresponding dynamic relocations, leave it 1030*0b57cec5SDimitry Andric // initialized by zero. Write down adjusted TLS symbol's values otherwise. 1031*0b57cec5SDimitry Andric // To calculate the adjustments use offsets for thread-local storage. 1032*0b57cec5SDimitry Andric // https://www.linux-mips.org/wiki/NPTL 1033*0b57cec5SDimitry Andric for (const std::pair<GotEntry, size_t> &p : g.local16) 1034*0b57cec5SDimitry Andric write(p.second, p.first.first, p.first.second); 1035*0b57cec5SDimitry Andric // Write VA to the primary GOT only. For secondary GOTs that 1036*0b57cec5SDimitry Andric // will be done by REL32 dynamic relocations. 1037*0b57cec5SDimitry Andric if (&g == &gots.front()) 1038*0b57cec5SDimitry Andric for (const std::pair<const Symbol *, size_t> &p : g.global) 1039*0b57cec5SDimitry Andric write(p.second, p.first, 0); 1040*0b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.relocs) 1041*0b57cec5SDimitry Andric write(p.second, p.first, 0); 1042*0b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.tls) 1043*0b57cec5SDimitry Andric write(p.second, p.first, p.first->isPreemptible ? 0 : -0x7000); 1044*0b57cec5SDimitry Andric for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) { 1045*0b57cec5SDimitry Andric if (p.first == nullptr && !config->isPic) 1046*0b57cec5SDimitry Andric write(p.second, nullptr, 1); 1047*0b57cec5SDimitry Andric else if (p.first && !p.first->isPreemptible) { 1048*0b57cec5SDimitry Andric // If we are emitting PIC code with relocations we mustn't write 1049*0b57cec5SDimitry Andric // anything to the GOT here. When using Elf_Rel relocations the value 1050*0b57cec5SDimitry Andric // one will be treated as an addend and will cause crashes at runtime 1051*0b57cec5SDimitry Andric if (!config->isPic) 1052*0b57cec5SDimitry Andric write(p.second, nullptr, 1); 1053*0b57cec5SDimitry Andric write(p.second + 1, p.first, -0x8000); 1054*0b57cec5SDimitry Andric } 1055*0b57cec5SDimitry Andric } 1056*0b57cec5SDimitry Andric } 1057*0b57cec5SDimitry Andric } 1058*0b57cec5SDimitry Andric 1059*0b57cec5SDimitry Andric // On PowerPC the .plt section is used to hold the table of function addresses 1060*0b57cec5SDimitry Andric // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss 1061*0b57cec5SDimitry Andric // section. I don't know why we have a BSS style type for the section but it is 1062*0b57cec5SDimitry Andric // consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI. 1063*0b57cec5SDimitry Andric GotPltSection::GotPltSection() 1064*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 1065*0b57cec5SDimitry Andric ".got.plt") { 1066*0b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 1067*0b57cec5SDimitry Andric name = ".plt"; 1068*0b57cec5SDimitry Andric } else if (config->emachine == EM_PPC64) { 1069*0b57cec5SDimitry Andric type = SHT_NOBITS; 1070*0b57cec5SDimitry Andric name = ".plt"; 1071*0b57cec5SDimitry Andric } 1072*0b57cec5SDimitry Andric } 1073*0b57cec5SDimitry Andric 1074*0b57cec5SDimitry Andric void GotPltSection::addEntry(Symbol &sym) { 1075*0b57cec5SDimitry Andric assert(sym.pltIndex == entries.size()); 1076*0b57cec5SDimitry Andric entries.push_back(&sym); 1077*0b57cec5SDimitry Andric } 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric size_t GotPltSection::getSize() const { 1080*0b57cec5SDimitry Andric return (target->gotPltHeaderEntriesNum + entries.size()) * config->wordsize; 1081*0b57cec5SDimitry Andric } 1082*0b57cec5SDimitry Andric 1083*0b57cec5SDimitry Andric void GotPltSection::writeTo(uint8_t *buf) { 1084*0b57cec5SDimitry Andric target->writeGotPltHeader(buf); 1085*0b57cec5SDimitry Andric buf += target->gotPltHeaderEntriesNum * config->wordsize; 1086*0b57cec5SDimitry Andric for (const Symbol *b : entries) { 1087*0b57cec5SDimitry Andric target->writeGotPlt(buf, *b); 1088*0b57cec5SDimitry Andric buf += config->wordsize; 1089*0b57cec5SDimitry Andric } 1090*0b57cec5SDimitry Andric } 1091*0b57cec5SDimitry Andric 1092*0b57cec5SDimitry Andric bool GotPltSection::isNeeded() const { 1093*0b57cec5SDimitry Andric // We need to emit GOTPLT even if it's empty if there's a relocation relative 1094*0b57cec5SDimitry Andric // to it. 1095*0b57cec5SDimitry Andric return !entries.empty() || hasGotPltOffRel; 1096*0b57cec5SDimitry Andric } 1097*0b57cec5SDimitry Andric 1098*0b57cec5SDimitry Andric static StringRef getIgotPltName() { 1099*0b57cec5SDimitry Andric // On ARM the IgotPltSection is part of the GotSection. 1100*0b57cec5SDimitry Andric if (config->emachine == EM_ARM) 1101*0b57cec5SDimitry Andric return ".got"; 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection 1104*0b57cec5SDimitry Andric // needs to be named the same. 1105*0b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 1106*0b57cec5SDimitry Andric return ".plt"; 1107*0b57cec5SDimitry Andric 1108*0b57cec5SDimitry Andric return ".got.plt"; 1109*0b57cec5SDimitry Andric } 1110*0b57cec5SDimitry Andric 1111*0b57cec5SDimitry Andric // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit 1112*0b57cec5SDimitry Andric // with the IgotPltSection. 1113*0b57cec5SDimitry Andric IgotPltSection::IgotPltSection() 1114*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 1115*0b57cec5SDimitry Andric config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS, 1116*0b57cec5SDimitry Andric config->wordsize, getIgotPltName()) {} 1117*0b57cec5SDimitry Andric 1118*0b57cec5SDimitry Andric void IgotPltSection::addEntry(Symbol &sym) { 1119*0b57cec5SDimitry Andric assert(sym.pltIndex == entries.size()); 1120*0b57cec5SDimitry Andric entries.push_back(&sym); 1121*0b57cec5SDimitry Andric } 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric size_t IgotPltSection::getSize() const { 1124*0b57cec5SDimitry Andric return entries.size() * config->wordsize; 1125*0b57cec5SDimitry Andric } 1126*0b57cec5SDimitry Andric 1127*0b57cec5SDimitry Andric void IgotPltSection::writeTo(uint8_t *buf) { 1128*0b57cec5SDimitry Andric for (const Symbol *b : entries) { 1129*0b57cec5SDimitry Andric target->writeIgotPlt(buf, *b); 1130*0b57cec5SDimitry Andric buf += config->wordsize; 1131*0b57cec5SDimitry Andric } 1132*0b57cec5SDimitry Andric } 1133*0b57cec5SDimitry Andric 1134*0b57cec5SDimitry Andric StringTableSection::StringTableSection(StringRef name, bool dynamic) 1135*0b57cec5SDimitry Andric : SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name), 1136*0b57cec5SDimitry Andric dynamic(dynamic) { 1137*0b57cec5SDimitry Andric // ELF string tables start with a NUL byte. 1138*0b57cec5SDimitry Andric addString(""); 1139*0b57cec5SDimitry Andric } 1140*0b57cec5SDimitry Andric 1141*0b57cec5SDimitry Andric // Adds a string to the string table. If `hashIt` is true we hash and check for 1142*0b57cec5SDimitry Andric // duplicates. It is optional because the name of global symbols are already 1143*0b57cec5SDimitry Andric // uniqued and hashing them again has a big cost for a small value: uniquing 1144*0b57cec5SDimitry Andric // them with some other string that happens to be the same. 1145*0b57cec5SDimitry Andric unsigned StringTableSection::addString(StringRef s, bool hashIt) { 1146*0b57cec5SDimitry Andric if (hashIt) { 1147*0b57cec5SDimitry Andric auto r = stringMap.insert(std::make_pair(s, this->size)); 1148*0b57cec5SDimitry Andric if (!r.second) 1149*0b57cec5SDimitry Andric return r.first->second; 1150*0b57cec5SDimitry Andric } 1151*0b57cec5SDimitry Andric unsigned ret = this->size; 1152*0b57cec5SDimitry Andric this->size = this->size + s.size() + 1; 1153*0b57cec5SDimitry Andric strings.push_back(s); 1154*0b57cec5SDimitry Andric return ret; 1155*0b57cec5SDimitry Andric } 1156*0b57cec5SDimitry Andric 1157*0b57cec5SDimitry Andric void StringTableSection::writeTo(uint8_t *buf) { 1158*0b57cec5SDimitry Andric for (StringRef s : strings) { 1159*0b57cec5SDimitry Andric memcpy(buf, s.data(), s.size()); 1160*0b57cec5SDimitry Andric buf[s.size()] = '\0'; 1161*0b57cec5SDimitry Andric buf += s.size() + 1; 1162*0b57cec5SDimitry Andric } 1163*0b57cec5SDimitry Andric } 1164*0b57cec5SDimitry Andric 1165*0b57cec5SDimitry Andric // Returns the number of version definition entries. Because the first entry 1166*0b57cec5SDimitry Andric // is for the version definition itself, it is the number of versioned symbols 1167*0b57cec5SDimitry Andric // plus one. Note that we don't support multiple versions yet. 1168*0b57cec5SDimitry Andric static unsigned getVerDefNum() { return config->versionDefinitions.size() + 1; } 1169*0b57cec5SDimitry Andric 1170*0b57cec5SDimitry Andric template <class ELFT> 1171*0b57cec5SDimitry Andric DynamicSection<ELFT>::DynamicSection() 1172*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize, 1173*0b57cec5SDimitry Andric ".dynamic") { 1174*0b57cec5SDimitry Andric this->entsize = ELFT::Is64Bits ? 16 : 8; 1175*0b57cec5SDimitry Andric 1176*0b57cec5SDimitry Andric // .dynamic section is not writable on MIPS and on Fuchsia OS 1177*0b57cec5SDimitry Andric // which passes -z rodynamic. 1178*0b57cec5SDimitry Andric // See "Special Section" in Chapter 4 in the following document: 1179*0b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1180*0b57cec5SDimitry Andric if (config->emachine == EM_MIPS || config->zRodynamic) 1181*0b57cec5SDimitry Andric this->flags = SHF_ALLOC; 1182*0b57cec5SDimitry Andric } 1183*0b57cec5SDimitry Andric 1184*0b57cec5SDimitry Andric template <class ELFT> 1185*0b57cec5SDimitry Andric void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) { 1186*0b57cec5SDimitry Andric entries.push_back({tag, fn}); 1187*0b57cec5SDimitry Andric } 1188*0b57cec5SDimitry Andric 1189*0b57cec5SDimitry Andric template <class ELFT> 1190*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) { 1191*0b57cec5SDimitry Andric entries.push_back({tag, [=] { return val; }}); 1192*0b57cec5SDimitry Andric } 1193*0b57cec5SDimitry Andric 1194*0b57cec5SDimitry Andric template <class ELFT> 1195*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) { 1196*0b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->getVA(0); }}); 1197*0b57cec5SDimitry Andric } 1198*0b57cec5SDimitry Andric 1199*0b57cec5SDimitry Andric template <class ELFT> 1200*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) { 1201*0b57cec5SDimitry Andric size_t tagOffset = entries.size() * entsize; 1202*0b57cec5SDimitry Andric entries.push_back( 1203*0b57cec5SDimitry Andric {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }}); 1204*0b57cec5SDimitry Andric } 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric template <class ELFT> 1207*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addOutSec(int32_t tag, OutputSection *sec) { 1208*0b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->addr; }}); 1209*0b57cec5SDimitry Andric } 1210*0b57cec5SDimitry Andric 1211*0b57cec5SDimitry Andric template <class ELFT> 1212*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) { 1213*0b57cec5SDimitry Andric entries.push_back({tag, [=] { return sec->size; }}); 1214*0b57cec5SDimitry Andric } 1215*0b57cec5SDimitry Andric 1216*0b57cec5SDimitry Andric template <class ELFT> 1217*0b57cec5SDimitry Andric void DynamicSection<ELFT>::addSym(int32_t tag, Symbol *sym) { 1218*0b57cec5SDimitry Andric entries.push_back({tag, [=] { return sym->getVA(); }}); 1219*0b57cec5SDimitry Andric } 1220*0b57cec5SDimitry Andric 1221*0b57cec5SDimitry Andric // A Linker script may assign the RELA relocation sections to the same 1222*0b57cec5SDimitry Andric // output section. When this occurs we cannot just use the OutputSection 1223*0b57cec5SDimitry Andric // Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to 1224*0b57cec5SDimitry Andric // overlap with the [DT_RELA, DT_RELA + DT_RELASZ). 1225*0b57cec5SDimitry Andric static uint64_t addPltRelSz() { 1226*0b57cec5SDimitry Andric size_t size = in.relaPlt->getSize(); 1227*0b57cec5SDimitry Andric if (in.relaIplt->getParent() == in.relaPlt->getParent() && 1228*0b57cec5SDimitry Andric in.relaIplt->name == in.relaPlt->name) 1229*0b57cec5SDimitry Andric size += in.relaIplt->getSize(); 1230*0b57cec5SDimitry Andric return size; 1231*0b57cec5SDimitry Andric } 1232*0b57cec5SDimitry Andric 1233*0b57cec5SDimitry Andric // Add remaining entries to complete .dynamic contents. 1234*0b57cec5SDimitry Andric template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { 1235*0b57cec5SDimitry Andric elf::Partition &part = getPartition(); 1236*0b57cec5SDimitry Andric bool isMain = part.name.empty(); 1237*0b57cec5SDimitry Andric 1238*0b57cec5SDimitry Andric for (StringRef s : config->filterList) 1239*0b57cec5SDimitry Andric addInt(DT_FILTER, part.dynStrTab->addString(s)); 1240*0b57cec5SDimitry Andric for (StringRef s : config->auxiliaryList) 1241*0b57cec5SDimitry Andric addInt(DT_AUXILIARY, part.dynStrTab->addString(s)); 1242*0b57cec5SDimitry Andric 1243*0b57cec5SDimitry Andric if (!config->rpath.empty()) 1244*0b57cec5SDimitry Andric addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH, 1245*0b57cec5SDimitry Andric part.dynStrTab->addString(config->rpath)); 1246*0b57cec5SDimitry Andric 1247*0b57cec5SDimitry Andric for (SharedFile *file : sharedFiles) 1248*0b57cec5SDimitry Andric if (file->isNeeded) 1249*0b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(file->soName)); 1250*0b57cec5SDimitry Andric 1251*0b57cec5SDimitry Andric if (isMain) { 1252*0b57cec5SDimitry Andric if (!config->soName.empty()) 1253*0b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(config->soName)); 1254*0b57cec5SDimitry Andric } else { 1255*0b57cec5SDimitry Andric if (!config->soName.empty()) 1256*0b57cec5SDimitry Andric addInt(DT_NEEDED, part.dynStrTab->addString(config->soName)); 1257*0b57cec5SDimitry Andric addInt(DT_SONAME, part.dynStrTab->addString(part.name)); 1258*0b57cec5SDimitry Andric } 1259*0b57cec5SDimitry Andric 1260*0b57cec5SDimitry Andric // Set DT_FLAGS and DT_FLAGS_1. 1261*0b57cec5SDimitry Andric uint32_t dtFlags = 0; 1262*0b57cec5SDimitry Andric uint32_t dtFlags1 = 0; 1263*0b57cec5SDimitry Andric if (config->bsymbolic) 1264*0b57cec5SDimitry Andric dtFlags |= DF_SYMBOLIC; 1265*0b57cec5SDimitry Andric if (config->zGlobal) 1266*0b57cec5SDimitry Andric dtFlags1 |= DF_1_GLOBAL; 1267*0b57cec5SDimitry Andric if (config->zInitfirst) 1268*0b57cec5SDimitry Andric dtFlags1 |= DF_1_INITFIRST; 1269*0b57cec5SDimitry Andric if (config->zInterpose) 1270*0b57cec5SDimitry Andric dtFlags1 |= DF_1_INTERPOSE; 1271*0b57cec5SDimitry Andric if (config->zNodefaultlib) 1272*0b57cec5SDimitry Andric dtFlags1 |= DF_1_NODEFLIB; 1273*0b57cec5SDimitry Andric if (config->zNodelete) 1274*0b57cec5SDimitry Andric dtFlags1 |= DF_1_NODELETE; 1275*0b57cec5SDimitry Andric if (config->zNodlopen) 1276*0b57cec5SDimitry Andric dtFlags1 |= DF_1_NOOPEN; 1277*0b57cec5SDimitry Andric if (config->zNow) { 1278*0b57cec5SDimitry Andric dtFlags |= DF_BIND_NOW; 1279*0b57cec5SDimitry Andric dtFlags1 |= DF_1_NOW; 1280*0b57cec5SDimitry Andric } 1281*0b57cec5SDimitry Andric if (config->zOrigin) { 1282*0b57cec5SDimitry Andric dtFlags |= DF_ORIGIN; 1283*0b57cec5SDimitry Andric dtFlags1 |= DF_1_ORIGIN; 1284*0b57cec5SDimitry Andric } 1285*0b57cec5SDimitry Andric if (!config->zText) 1286*0b57cec5SDimitry Andric dtFlags |= DF_TEXTREL; 1287*0b57cec5SDimitry Andric if (config->hasStaticTlsModel) 1288*0b57cec5SDimitry Andric dtFlags |= DF_STATIC_TLS; 1289*0b57cec5SDimitry Andric 1290*0b57cec5SDimitry Andric if (dtFlags) 1291*0b57cec5SDimitry Andric addInt(DT_FLAGS, dtFlags); 1292*0b57cec5SDimitry Andric if (dtFlags1) 1293*0b57cec5SDimitry Andric addInt(DT_FLAGS_1, dtFlags1); 1294*0b57cec5SDimitry Andric 1295*0b57cec5SDimitry Andric // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We 1296*0b57cec5SDimitry Andric // need it for each process, so we don't write it for DSOs. The loader writes 1297*0b57cec5SDimitry Andric // the pointer into this entry. 1298*0b57cec5SDimitry Andric // 1299*0b57cec5SDimitry Andric // DT_DEBUG is the only .dynamic entry that needs to be written to. Some 1300*0b57cec5SDimitry Andric // systems (currently only Fuchsia OS) provide other means to give the 1301*0b57cec5SDimitry Andric // debugger this information. Such systems may choose make .dynamic read-only. 1302*0b57cec5SDimitry Andric // If the target is such a system (used -z rodynamic) don't write DT_DEBUG. 1303*0b57cec5SDimitry Andric if (!config->shared && !config->relocatable && !config->zRodynamic) 1304*0b57cec5SDimitry Andric addInt(DT_DEBUG, 0); 1305*0b57cec5SDimitry Andric 1306*0b57cec5SDimitry Andric if (OutputSection *sec = part.dynStrTab->getParent()) 1307*0b57cec5SDimitry Andric this->link = sec->sectionIndex; 1308*0b57cec5SDimitry Andric 1309*0b57cec5SDimitry Andric if (part.relaDyn->isNeeded()) { 1310*0b57cec5SDimitry Andric addInSec(part.relaDyn->dynamicTag, part.relaDyn); 1311*0b57cec5SDimitry Andric addSize(part.relaDyn->sizeDynamicTag, part.relaDyn->getParent()); 1312*0b57cec5SDimitry Andric 1313*0b57cec5SDimitry Andric bool isRela = config->isRela; 1314*0b57cec5SDimitry Andric addInt(isRela ? DT_RELAENT : DT_RELENT, 1315*0b57cec5SDimitry Andric isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); 1316*0b57cec5SDimitry Andric 1317*0b57cec5SDimitry Andric // MIPS dynamic loader does not support RELCOUNT tag. 1318*0b57cec5SDimitry Andric // The problem is in the tight relation between dynamic 1319*0b57cec5SDimitry Andric // relocations and GOT. So do not emit this tag on MIPS. 1320*0b57cec5SDimitry Andric if (config->emachine != EM_MIPS) { 1321*0b57cec5SDimitry Andric size_t numRelativeRels = part.relaDyn->getRelativeRelocCount(); 1322*0b57cec5SDimitry Andric if (config->zCombreloc && numRelativeRels) 1323*0b57cec5SDimitry Andric addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels); 1324*0b57cec5SDimitry Andric } 1325*0b57cec5SDimitry Andric } 1326*0b57cec5SDimitry Andric if (part.relrDyn && !part.relrDyn->relocs.empty()) { 1327*0b57cec5SDimitry Andric addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR, 1328*0b57cec5SDimitry Andric part.relrDyn); 1329*0b57cec5SDimitry Andric addSize(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ, 1330*0b57cec5SDimitry Andric part.relrDyn->getParent()); 1331*0b57cec5SDimitry Andric addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT, 1332*0b57cec5SDimitry Andric sizeof(Elf_Relr)); 1333*0b57cec5SDimitry Andric } 1334*0b57cec5SDimitry Andric // .rel[a].plt section usually consists of two parts, containing plt and 1335*0b57cec5SDimitry Andric // iplt relocations. It is possible to have only iplt relocations in the 1336*0b57cec5SDimitry Andric // output. In that case relaPlt is empty and have zero offset, the same offset 1337*0b57cec5SDimitry Andric // as relaIplt has. And we still want to emit proper dynamic tags for that 1338*0b57cec5SDimitry Andric // case, so here we always use relaPlt as marker for the begining of 1339*0b57cec5SDimitry Andric // .rel[a].plt section. 1340*0b57cec5SDimitry Andric if (isMain && (in.relaPlt->isNeeded() || in.relaIplt->isNeeded())) { 1341*0b57cec5SDimitry Andric addInSec(DT_JMPREL, in.relaPlt); 1342*0b57cec5SDimitry Andric entries.push_back({DT_PLTRELSZ, addPltRelSz}); 1343*0b57cec5SDimitry Andric switch (config->emachine) { 1344*0b57cec5SDimitry Andric case EM_MIPS: 1345*0b57cec5SDimitry Andric addInSec(DT_MIPS_PLTGOT, in.gotPlt); 1346*0b57cec5SDimitry Andric break; 1347*0b57cec5SDimitry Andric case EM_SPARCV9: 1348*0b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.plt); 1349*0b57cec5SDimitry Andric break; 1350*0b57cec5SDimitry Andric default: 1351*0b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.gotPlt); 1352*0b57cec5SDimitry Andric break; 1353*0b57cec5SDimitry Andric } 1354*0b57cec5SDimitry Andric addInt(DT_PLTREL, config->isRela ? DT_RELA : DT_REL); 1355*0b57cec5SDimitry Andric } 1356*0b57cec5SDimitry Andric 1357*0b57cec5SDimitry Andric if (config->emachine == EM_AARCH64) { 1358*0b57cec5SDimitry Andric if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) 1359*0b57cec5SDimitry Andric addInt(DT_AARCH64_BTI_PLT, 0); 1360*0b57cec5SDimitry Andric if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC) 1361*0b57cec5SDimitry Andric addInt(DT_AARCH64_PAC_PLT, 0); 1362*0b57cec5SDimitry Andric } 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric addInSec(DT_SYMTAB, part.dynSymTab); 1365*0b57cec5SDimitry Andric addInt(DT_SYMENT, sizeof(Elf_Sym)); 1366*0b57cec5SDimitry Andric addInSec(DT_STRTAB, part.dynStrTab); 1367*0b57cec5SDimitry Andric addInt(DT_STRSZ, part.dynStrTab->getSize()); 1368*0b57cec5SDimitry Andric if (!config->zText) 1369*0b57cec5SDimitry Andric addInt(DT_TEXTREL, 0); 1370*0b57cec5SDimitry Andric if (part.gnuHashTab) 1371*0b57cec5SDimitry Andric addInSec(DT_GNU_HASH, part.gnuHashTab); 1372*0b57cec5SDimitry Andric if (part.hashTab) 1373*0b57cec5SDimitry Andric addInSec(DT_HASH, part.hashTab); 1374*0b57cec5SDimitry Andric 1375*0b57cec5SDimitry Andric if (isMain) { 1376*0b57cec5SDimitry Andric if (Out::preinitArray) { 1377*0b57cec5SDimitry Andric addOutSec(DT_PREINIT_ARRAY, Out::preinitArray); 1378*0b57cec5SDimitry Andric addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray); 1379*0b57cec5SDimitry Andric } 1380*0b57cec5SDimitry Andric if (Out::initArray) { 1381*0b57cec5SDimitry Andric addOutSec(DT_INIT_ARRAY, Out::initArray); 1382*0b57cec5SDimitry Andric addSize(DT_INIT_ARRAYSZ, Out::initArray); 1383*0b57cec5SDimitry Andric } 1384*0b57cec5SDimitry Andric if (Out::finiArray) { 1385*0b57cec5SDimitry Andric addOutSec(DT_FINI_ARRAY, Out::finiArray); 1386*0b57cec5SDimitry Andric addSize(DT_FINI_ARRAYSZ, Out::finiArray); 1387*0b57cec5SDimitry Andric } 1388*0b57cec5SDimitry Andric 1389*0b57cec5SDimitry Andric if (Symbol *b = symtab->find(config->init)) 1390*0b57cec5SDimitry Andric if (b->isDefined()) 1391*0b57cec5SDimitry Andric addSym(DT_INIT, b); 1392*0b57cec5SDimitry Andric if (Symbol *b = symtab->find(config->fini)) 1393*0b57cec5SDimitry Andric if (b->isDefined()) 1394*0b57cec5SDimitry Andric addSym(DT_FINI, b); 1395*0b57cec5SDimitry Andric } 1396*0b57cec5SDimitry Andric 1397*0b57cec5SDimitry Andric bool hasVerNeed = SharedFile::vernauxNum != 0; 1398*0b57cec5SDimitry Andric if (hasVerNeed || part.verDef) 1399*0b57cec5SDimitry Andric addInSec(DT_VERSYM, part.verSym); 1400*0b57cec5SDimitry Andric if (part.verDef) { 1401*0b57cec5SDimitry Andric addInSec(DT_VERDEF, part.verDef); 1402*0b57cec5SDimitry Andric addInt(DT_VERDEFNUM, getVerDefNum()); 1403*0b57cec5SDimitry Andric } 1404*0b57cec5SDimitry Andric if (hasVerNeed) { 1405*0b57cec5SDimitry Andric addInSec(DT_VERNEED, part.verNeed); 1406*0b57cec5SDimitry Andric unsigned needNum = 0; 1407*0b57cec5SDimitry Andric for (SharedFile *f : sharedFiles) 1408*0b57cec5SDimitry Andric if (!f->vernauxs.empty()) 1409*0b57cec5SDimitry Andric ++needNum; 1410*0b57cec5SDimitry Andric addInt(DT_VERNEEDNUM, needNum); 1411*0b57cec5SDimitry Andric } 1412*0b57cec5SDimitry Andric 1413*0b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 1414*0b57cec5SDimitry Andric addInt(DT_MIPS_RLD_VERSION, 1); 1415*0b57cec5SDimitry Andric addInt(DT_MIPS_FLAGS, RHF_NOTPOT); 1416*0b57cec5SDimitry Andric addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase()); 1417*0b57cec5SDimitry Andric addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols()); 1418*0b57cec5SDimitry Andric 1419*0b57cec5SDimitry Andric add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); }); 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric if (const Symbol *b = in.mipsGot->getFirstGlobalEntry()) 1422*0b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, b->dynsymIndex); 1423*0b57cec5SDimitry Andric else 1424*0b57cec5SDimitry Andric addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols()); 1425*0b57cec5SDimitry Andric addInSec(DT_PLTGOT, in.mipsGot); 1426*0b57cec5SDimitry Andric if (in.mipsRldMap) { 1427*0b57cec5SDimitry Andric if (!config->pie) 1428*0b57cec5SDimitry Andric addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap); 1429*0b57cec5SDimitry Andric // Store the offset to the .rld_map section 1430*0b57cec5SDimitry Andric // relative to the address of the tag. 1431*0b57cec5SDimitry Andric addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap); 1432*0b57cec5SDimitry Andric } 1433*0b57cec5SDimitry Andric } 1434*0b57cec5SDimitry Andric 1435*0b57cec5SDimitry Andric // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent, 1436*0b57cec5SDimitry Andric // glibc assumes the old-style BSS PLT layout which we don't support. 1437*0b57cec5SDimitry Andric if (config->emachine == EM_PPC) 1438*0b57cec5SDimitry Andric add(DT_PPC_GOT, [] { return in.got->getVA(); }); 1439*0b57cec5SDimitry Andric 1440*0b57cec5SDimitry Andric // Glink dynamic tag is required by the V2 abi if the plt section isn't empty. 1441*0b57cec5SDimitry Andric if (config->emachine == EM_PPC64 && in.plt->isNeeded()) { 1442*0b57cec5SDimitry Andric // The Glink tag points to 32 bytes before the first lazy symbol resolution 1443*0b57cec5SDimitry Andric // stub, which starts directly after the header. 1444*0b57cec5SDimitry Andric entries.push_back({DT_PPC64_GLINK, [=] { 1445*0b57cec5SDimitry Andric unsigned offset = target->pltHeaderSize - 32; 1446*0b57cec5SDimitry Andric return in.plt->getVA(0) + offset; 1447*0b57cec5SDimitry Andric }}); 1448*0b57cec5SDimitry Andric } 1449*0b57cec5SDimitry Andric 1450*0b57cec5SDimitry Andric addInt(DT_NULL, 0); 1451*0b57cec5SDimitry Andric 1452*0b57cec5SDimitry Andric getParent()->link = this->link; 1453*0b57cec5SDimitry Andric this->size = entries.size() * this->entsize; 1454*0b57cec5SDimitry Andric } 1455*0b57cec5SDimitry Andric 1456*0b57cec5SDimitry Andric template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) { 1457*0b57cec5SDimitry Andric auto *p = reinterpret_cast<Elf_Dyn *>(buf); 1458*0b57cec5SDimitry Andric 1459*0b57cec5SDimitry Andric for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) { 1460*0b57cec5SDimitry Andric p->d_tag = kv.first; 1461*0b57cec5SDimitry Andric p->d_un.d_val = kv.second(); 1462*0b57cec5SDimitry Andric ++p; 1463*0b57cec5SDimitry Andric } 1464*0b57cec5SDimitry Andric } 1465*0b57cec5SDimitry Andric 1466*0b57cec5SDimitry Andric uint64_t DynamicReloc::getOffset() const { 1467*0b57cec5SDimitry Andric return inputSec->getVA(offsetInSec); 1468*0b57cec5SDimitry Andric } 1469*0b57cec5SDimitry Andric 1470*0b57cec5SDimitry Andric int64_t DynamicReloc::computeAddend() const { 1471*0b57cec5SDimitry Andric if (useSymVA) 1472*0b57cec5SDimitry Andric return sym->getVA(addend); 1473*0b57cec5SDimitry Andric if (!outputSec) 1474*0b57cec5SDimitry Andric return addend; 1475*0b57cec5SDimitry Andric // See the comment in the DynamicReloc ctor. 1476*0b57cec5SDimitry Andric return getMipsPageAddr(outputSec->addr) + addend; 1477*0b57cec5SDimitry Andric } 1478*0b57cec5SDimitry Andric 1479*0b57cec5SDimitry Andric uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const { 1480*0b57cec5SDimitry Andric if (sym && !useSymVA) 1481*0b57cec5SDimitry Andric return symTab->getSymbolIndex(sym); 1482*0b57cec5SDimitry Andric return 0; 1483*0b57cec5SDimitry Andric } 1484*0b57cec5SDimitry Andric 1485*0b57cec5SDimitry Andric RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type, 1486*0b57cec5SDimitry Andric int32_t dynamicTag, 1487*0b57cec5SDimitry Andric int32_t sizeDynamicTag) 1488*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, type, config->wordsize, name), 1489*0b57cec5SDimitry Andric dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag) {} 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric void RelocationBaseSection::addReloc(RelType dynType, InputSectionBase *isec, 1492*0b57cec5SDimitry Andric uint64_t offsetInSec, Symbol *sym) { 1493*0b57cec5SDimitry Andric addReloc({dynType, isec, offsetInSec, false, sym, 0}); 1494*0b57cec5SDimitry Andric } 1495*0b57cec5SDimitry Andric 1496*0b57cec5SDimitry Andric void RelocationBaseSection::addReloc(RelType dynType, 1497*0b57cec5SDimitry Andric InputSectionBase *inputSec, 1498*0b57cec5SDimitry Andric uint64_t offsetInSec, Symbol *sym, 1499*0b57cec5SDimitry Andric int64_t addend, RelExpr expr, 1500*0b57cec5SDimitry Andric RelType type) { 1501*0b57cec5SDimitry Andric // Write the addends to the relocated address if required. We skip 1502*0b57cec5SDimitry Andric // it if the written value would be zero. 1503*0b57cec5SDimitry Andric if (config->writeAddends && (expr != R_ADDEND || addend != 0)) 1504*0b57cec5SDimitry Andric inputSec->relocations.push_back({expr, type, offsetInSec, addend, sym}); 1505*0b57cec5SDimitry Andric addReloc({dynType, inputSec, offsetInSec, expr != R_ADDEND, sym, addend}); 1506*0b57cec5SDimitry Andric } 1507*0b57cec5SDimitry Andric 1508*0b57cec5SDimitry Andric void RelocationBaseSection::addReloc(const DynamicReloc &reloc) { 1509*0b57cec5SDimitry Andric if (reloc.type == target->relativeRel) 1510*0b57cec5SDimitry Andric ++numRelativeRelocs; 1511*0b57cec5SDimitry Andric relocs.push_back(reloc); 1512*0b57cec5SDimitry Andric } 1513*0b57cec5SDimitry Andric 1514*0b57cec5SDimitry Andric void RelocationBaseSection::finalizeContents() { 1515*0b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 1516*0b57cec5SDimitry Andric 1517*0b57cec5SDimitry Andric // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE 1518*0b57cec5SDimitry Andric // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that 1519*0b57cec5SDimitry Andric // case. 1520*0b57cec5SDimitry Andric if (symTab && symTab->getParent()) 1521*0b57cec5SDimitry Andric getParent()->link = symTab->getParent()->sectionIndex; 1522*0b57cec5SDimitry Andric else 1523*0b57cec5SDimitry Andric getParent()->link = 0; 1524*0b57cec5SDimitry Andric 1525*0b57cec5SDimitry Andric if (in.relaPlt == this) 1526*0b57cec5SDimitry Andric getParent()->info = in.gotPlt->getParent()->sectionIndex; 1527*0b57cec5SDimitry Andric if (in.relaIplt == this) 1528*0b57cec5SDimitry Andric getParent()->info = in.igotPlt->getParent()->sectionIndex; 1529*0b57cec5SDimitry Andric } 1530*0b57cec5SDimitry Andric 1531*0b57cec5SDimitry Andric RelrBaseSection::RelrBaseSection() 1532*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, 1533*0b57cec5SDimitry Andric config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR, 1534*0b57cec5SDimitry Andric config->wordsize, ".relr.dyn") {} 1535*0b57cec5SDimitry Andric 1536*0b57cec5SDimitry Andric template <class ELFT> 1537*0b57cec5SDimitry Andric static void encodeDynamicReloc(SymbolTableBaseSection *symTab, 1538*0b57cec5SDimitry Andric typename ELFT::Rela *p, 1539*0b57cec5SDimitry Andric const DynamicReloc &rel) { 1540*0b57cec5SDimitry Andric if (config->isRela) 1541*0b57cec5SDimitry Andric p->r_addend = rel.computeAddend(); 1542*0b57cec5SDimitry Andric p->r_offset = rel.getOffset(); 1543*0b57cec5SDimitry Andric p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL); 1544*0b57cec5SDimitry Andric } 1545*0b57cec5SDimitry Andric 1546*0b57cec5SDimitry Andric template <class ELFT> 1547*0b57cec5SDimitry Andric RelocationSection<ELFT>::RelocationSection(StringRef name, bool sort) 1548*0b57cec5SDimitry Andric : RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL, 1549*0b57cec5SDimitry Andric config->isRela ? DT_RELA : DT_REL, 1550*0b57cec5SDimitry Andric config->isRela ? DT_RELASZ : DT_RELSZ), 1551*0b57cec5SDimitry Andric sort(sort) { 1552*0b57cec5SDimitry Andric this->entsize = config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 1553*0b57cec5SDimitry Andric } 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) { 1556*0b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 1557*0b57cec5SDimitry Andric 1558*0b57cec5SDimitry Andric // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to 1559*0b57cec5SDimitry Andric // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset 1560*0b57cec5SDimitry Andric // is to make results easier to read. 1561*0b57cec5SDimitry Andric if (sort) 1562*0b57cec5SDimitry Andric llvm::stable_sort( 1563*0b57cec5SDimitry Andric relocs, [&](const DynamicReloc &a, const DynamicReloc &b) { 1564*0b57cec5SDimitry Andric return std::make_tuple(a.type != target->relativeRel, 1565*0b57cec5SDimitry Andric a.getSymIndex(symTab), a.getOffset()) < 1566*0b57cec5SDimitry Andric std::make_tuple(b.type != target->relativeRel, 1567*0b57cec5SDimitry Andric b.getSymIndex(symTab), b.getOffset()); 1568*0b57cec5SDimitry Andric }); 1569*0b57cec5SDimitry Andric 1570*0b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 1571*0b57cec5SDimitry Andric encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel); 1572*0b57cec5SDimitry Andric buf += config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 1573*0b57cec5SDimitry Andric } 1574*0b57cec5SDimitry Andric } 1575*0b57cec5SDimitry Andric 1576*0b57cec5SDimitry Andric template <class ELFT> 1577*0b57cec5SDimitry Andric AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection( 1578*0b57cec5SDimitry Andric StringRef name) 1579*0b57cec5SDimitry Andric : RelocationBaseSection( 1580*0b57cec5SDimitry Andric name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL, 1581*0b57cec5SDimitry Andric config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL, 1582*0b57cec5SDimitry Andric config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) { 1583*0b57cec5SDimitry Andric this->entsize = 1; 1584*0b57cec5SDimitry Andric } 1585*0b57cec5SDimitry Andric 1586*0b57cec5SDimitry Andric template <class ELFT> 1587*0b57cec5SDimitry Andric bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() { 1588*0b57cec5SDimitry Andric // This function computes the contents of an Android-format packed relocation 1589*0b57cec5SDimitry Andric // section. 1590*0b57cec5SDimitry Andric // 1591*0b57cec5SDimitry Andric // This format compresses relocations by using relocation groups to factor out 1592*0b57cec5SDimitry Andric // fields that are common between relocations and storing deltas from previous 1593*0b57cec5SDimitry Andric // relocations in SLEB128 format (which has a short representation for small 1594*0b57cec5SDimitry Andric // numbers). A good example of a relocation type with common fields is 1595*0b57cec5SDimitry Andric // R_*_RELATIVE, which is normally used to represent function pointers in 1596*0b57cec5SDimitry Andric // vtables. In the REL format, each relative relocation has the same r_info 1597*0b57cec5SDimitry Andric // field, and is only different from other relative relocations in terms of 1598*0b57cec5SDimitry Andric // the r_offset field. By sorting relocations by offset, grouping them by 1599*0b57cec5SDimitry Andric // r_info and representing each relocation with only the delta from the 1600*0b57cec5SDimitry Andric // previous offset, each 8-byte relocation can be compressed to as little as 1 1601*0b57cec5SDimitry Andric // byte (or less with run-length encoding). This relocation packer was able to 1602*0b57cec5SDimitry Andric // reduce the size of the relocation section in an Android Chromium DSO from 1603*0b57cec5SDimitry Andric // 2,911,184 bytes to 174,693 bytes, or 6% of the original size. 1604*0b57cec5SDimitry Andric // 1605*0b57cec5SDimitry Andric // A relocation section consists of a header containing the literal bytes 1606*0b57cec5SDimitry Andric // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two 1607*0b57cec5SDimitry Andric // elements are the total number of relocations in the section and an initial 1608*0b57cec5SDimitry Andric // r_offset value. The remaining elements define a sequence of relocation 1609*0b57cec5SDimitry Andric // groups. Each relocation group starts with a header consisting of the 1610*0b57cec5SDimitry Andric // following elements: 1611*0b57cec5SDimitry Andric // 1612*0b57cec5SDimitry Andric // - the number of relocations in the relocation group 1613*0b57cec5SDimitry Andric // - flags for the relocation group 1614*0b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta 1615*0b57cec5SDimitry Andric // for each relocation in the group. 1616*0b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info 1617*0b57cec5SDimitry Andric // field for each relocation in the group. 1618*0b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and 1619*0b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for 1620*0b57cec5SDimitry Andric // each relocation in the group. 1621*0b57cec5SDimitry Andric // 1622*0b57cec5SDimitry Andric // Following the relocation group header are descriptions of each of the 1623*0b57cec5SDimitry Andric // relocations in the group. They consist of the following elements: 1624*0b57cec5SDimitry Andric // 1625*0b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset 1626*0b57cec5SDimitry Andric // delta for this relocation. 1627*0b57cec5SDimitry Andric // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info 1628*0b57cec5SDimitry Andric // field for this relocation. 1629*0b57cec5SDimitry Andric // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and 1630*0b57cec5SDimitry Andric // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for 1631*0b57cec5SDimitry Andric // this relocation. 1632*0b57cec5SDimitry Andric 1633*0b57cec5SDimitry Andric size_t oldSize = relocData.size(); 1634*0b57cec5SDimitry Andric 1635*0b57cec5SDimitry Andric relocData = {'A', 'P', 'S', '2'}; 1636*0b57cec5SDimitry Andric raw_svector_ostream os(relocData); 1637*0b57cec5SDimitry Andric auto add = [&](int64_t v) { encodeSLEB128(v, os); }; 1638*0b57cec5SDimitry Andric 1639*0b57cec5SDimitry Andric // The format header includes the number of relocations and the initial 1640*0b57cec5SDimitry Andric // offset (we set this to zero because the first relocation group will 1641*0b57cec5SDimitry Andric // perform the initial adjustment). 1642*0b57cec5SDimitry Andric add(relocs.size()); 1643*0b57cec5SDimitry Andric add(0); 1644*0b57cec5SDimitry Andric 1645*0b57cec5SDimitry Andric std::vector<Elf_Rela> relatives, nonRelatives; 1646*0b57cec5SDimitry Andric 1647*0b57cec5SDimitry Andric for (const DynamicReloc &rel : relocs) { 1648*0b57cec5SDimitry Andric Elf_Rela r; 1649*0b57cec5SDimitry Andric encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel); 1650*0b57cec5SDimitry Andric 1651*0b57cec5SDimitry Andric if (r.getType(config->isMips64EL) == target->relativeRel) 1652*0b57cec5SDimitry Andric relatives.push_back(r); 1653*0b57cec5SDimitry Andric else 1654*0b57cec5SDimitry Andric nonRelatives.push_back(r); 1655*0b57cec5SDimitry Andric } 1656*0b57cec5SDimitry Andric 1657*0b57cec5SDimitry Andric llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) { 1658*0b57cec5SDimitry Andric return a.r_offset < b.r_offset; 1659*0b57cec5SDimitry Andric }); 1660*0b57cec5SDimitry Andric 1661*0b57cec5SDimitry Andric // Try to find groups of relative relocations which are spaced one word 1662*0b57cec5SDimitry Andric // apart from one another. These generally correspond to vtable entries. The 1663*0b57cec5SDimitry Andric // format allows these groups to be encoded using a sort of run-length 1664*0b57cec5SDimitry Andric // encoding, but each group will cost 7 bytes in addition to the offset from 1665*0b57cec5SDimitry Andric // the previous group, so it is only profitable to do this for groups of 1666*0b57cec5SDimitry Andric // size 8 or larger. 1667*0b57cec5SDimitry Andric std::vector<Elf_Rela> ungroupedRelatives; 1668*0b57cec5SDimitry Andric std::vector<std::vector<Elf_Rela>> relativeGroups; 1669*0b57cec5SDimitry Andric for (auto i = relatives.begin(), e = relatives.end(); i != e;) { 1670*0b57cec5SDimitry Andric std::vector<Elf_Rela> group; 1671*0b57cec5SDimitry Andric do { 1672*0b57cec5SDimitry Andric group.push_back(*i++); 1673*0b57cec5SDimitry Andric } while (i != e && (i - 1)->r_offset + config->wordsize == i->r_offset); 1674*0b57cec5SDimitry Andric 1675*0b57cec5SDimitry Andric if (group.size() < 8) 1676*0b57cec5SDimitry Andric ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(), 1677*0b57cec5SDimitry Andric group.end()); 1678*0b57cec5SDimitry Andric else 1679*0b57cec5SDimitry Andric relativeGroups.emplace_back(std::move(group)); 1680*0b57cec5SDimitry Andric } 1681*0b57cec5SDimitry Andric 1682*0b57cec5SDimitry Andric unsigned hasAddendIfRela = 1683*0b57cec5SDimitry Andric config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0; 1684*0b57cec5SDimitry Andric 1685*0b57cec5SDimitry Andric uint64_t offset = 0; 1686*0b57cec5SDimitry Andric uint64_t addend = 0; 1687*0b57cec5SDimitry Andric 1688*0b57cec5SDimitry Andric // Emit the run-length encoding for the groups of adjacent relative 1689*0b57cec5SDimitry Andric // relocations. Each group is represented using two groups in the packed 1690*0b57cec5SDimitry Andric // format. The first is used to set the current offset to the start of the 1691*0b57cec5SDimitry Andric // group (and also encodes the first relocation), and the second encodes the 1692*0b57cec5SDimitry Andric // remaining relocations. 1693*0b57cec5SDimitry Andric for (std::vector<Elf_Rela> &g : relativeGroups) { 1694*0b57cec5SDimitry Andric // The first relocation in the group. 1695*0b57cec5SDimitry Andric add(1); 1696*0b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 1697*0b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 1698*0b57cec5SDimitry Andric add(g[0].r_offset - offset); 1699*0b57cec5SDimitry Andric add(target->relativeRel); 1700*0b57cec5SDimitry Andric if (config->isRela) { 1701*0b57cec5SDimitry Andric add(g[0].r_addend - addend); 1702*0b57cec5SDimitry Andric addend = g[0].r_addend; 1703*0b57cec5SDimitry Andric } 1704*0b57cec5SDimitry Andric 1705*0b57cec5SDimitry Andric // The remaining relocations. 1706*0b57cec5SDimitry Andric add(g.size() - 1); 1707*0b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | 1708*0b57cec5SDimitry Andric RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 1709*0b57cec5SDimitry Andric add(config->wordsize); 1710*0b57cec5SDimitry Andric add(target->relativeRel); 1711*0b57cec5SDimitry Andric if (config->isRela) { 1712*0b57cec5SDimitry Andric for (auto i = g.begin() + 1, e = g.end(); i != e; ++i) { 1713*0b57cec5SDimitry Andric add(i->r_addend - addend); 1714*0b57cec5SDimitry Andric addend = i->r_addend; 1715*0b57cec5SDimitry Andric } 1716*0b57cec5SDimitry Andric } 1717*0b57cec5SDimitry Andric 1718*0b57cec5SDimitry Andric offset = g.back().r_offset; 1719*0b57cec5SDimitry Andric } 1720*0b57cec5SDimitry Andric 1721*0b57cec5SDimitry Andric // Now the ungrouped relatives. 1722*0b57cec5SDimitry Andric if (!ungroupedRelatives.empty()) { 1723*0b57cec5SDimitry Andric add(ungroupedRelatives.size()); 1724*0b57cec5SDimitry Andric add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela); 1725*0b57cec5SDimitry Andric add(target->relativeRel); 1726*0b57cec5SDimitry Andric for (Elf_Rela &r : ungroupedRelatives) { 1727*0b57cec5SDimitry Andric add(r.r_offset - offset); 1728*0b57cec5SDimitry Andric offset = r.r_offset; 1729*0b57cec5SDimitry Andric if (config->isRela) { 1730*0b57cec5SDimitry Andric add(r.r_addend - addend); 1731*0b57cec5SDimitry Andric addend = r.r_addend; 1732*0b57cec5SDimitry Andric } 1733*0b57cec5SDimitry Andric } 1734*0b57cec5SDimitry Andric } 1735*0b57cec5SDimitry Andric 1736*0b57cec5SDimitry Andric // Finally the non-relative relocations. 1737*0b57cec5SDimitry Andric llvm::sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) { 1738*0b57cec5SDimitry Andric return a.r_offset < b.r_offset; 1739*0b57cec5SDimitry Andric }); 1740*0b57cec5SDimitry Andric if (!nonRelatives.empty()) { 1741*0b57cec5SDimitry Andric add(nonRelatives.size()); 1742*0b57cec5SDimitry Andric add(hasAddendIfRela); 1743*0b57cec5SDimitry Andric for (Elf_Rela &r : nonRelatives) { 1744*0b57cec5SDimitry Andric add(r.r_offset - offset); 1745*0b57cec5SDimitry Andric offset = r.r_offset; 1746*0b57cec5SDimitry Andric add(r.r_info); 1747*0b57cec5SDimitry Andric if (config->isRela) { 1748*0b57cec5SDimitry Andric add(r.r_addend - addend); 1749*0b57cec5SDimitry Andric addend = r.r_addend; 1750*0b57cec5SDimitry Andric } 1751*0b57cec5SDimitry Andric } 1752*0b57cec5SDimitry Andric } 1753*0b57cec5SDimitry Andric 1754*0b57cec5SDimitry Andric // Don't allow the section to shrink; otherwise the size of the section can 1755*0b57cec5SDimitry Andric // oscillate infinitely. 1756*0b57cec5SDimitry Andric if (relocData.size() < oldSize) 1757*0b57cec5SDimitry Andric relocData.append(oldSize - relocData.size(), 0); 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric // Returns whether the section size changed. We need to keep recomputing both 1760*0b57cec5SDimitry Andric // section layout and the contents of this section until the size converges 1761*0b57cec5SDimitry Andric // because changing this section's size can affect section layout, which in 1762*0b57cec5SDimitry Andric // turn can affect the sizes of the LEB-encoded integers stored in this 1763*0b57cec5SDimitry Andric // section. 1764*0b57cec5SDimitry Andric return relocData.size() != oldSize; 1765*0b57cec5SDimitry Andric } 1766*0b57cec5SDimitry Andric 1767*0b57cec5SDimitry Andric template <class ELFT> RelrSection<ELFT>::RelrSection() { 1768*0b57cec5SDimitry Andric this->entsize = config->wordsize; 1769*0b57cec5SDimitry Andric } 1770*0b57cec5SDimitry Andric 1771*0b57cec5SDimitry Andric template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() { 1772*0b57cec5SDimitry Andric // This function computes the contents of an SHT_RELR packed relocation 1773*0b57cec5SDimitry Andric // section. 1774*0b57cec5SDimitry Andric // 1775*0b57cec5SDimitry Andric // Proposal for adding SHT_RELR sections to generic-abi is here: 1776*0b57cec5SDimitry Andric // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg 1777*0b57cec5SDimitry Andric // 1778*0b57cec5SDimitry Andric // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks 1779*0b57cec5SDimitry Andric // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] 1780*0b57cec5SDimitry Andric // 1781*0b57cec5SDimitry Andric // i.e. start with an address, followed by any number of bitmaps. The address 1782*0b57cec5SDimitry Andric // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63 1783*0b57cec5SDimitry Andric // relocations each, at subsequent offsets following the last address entry. 1784*0b57cec5SDimitry Andric // 1785*0b57cec5SDimitry Andric // The bitmap entries must have 1 in the least significant bit. The assumption 1786*0b57cec5SDimitry Andric // here is that an address cannot have 1 in lsb. Odd addresses are not 1787*0b57cec5SDimitry Andric // supported. 1788*0b57cec5SDimitry Andric // 1789*0b57cec5SDimitry Andric // Excluding the least significant bit in the bitmap, each non-zero bit in 1790*0b57cec5SDimitry Andric // the bitmap represents a relocation to be applied to a corresponding machine 1791*0b57cec5SDimitry Andric // word that follows the base address word. The second least significant bit 1792*0b57cec5SDimitry Andric // represents the machine word immediately following the initial address, and 1793*0b57cec5SDimitry Andric // each bit that follows represents the next word, in linear order. As such, 1794*0b57cec5SDimitry Andric // a single bitmap can encode up to 31 relocations in a 32-bit object, and 1795*0b57cec5SDimitry Andric // 63 relocations in a 64-bit object. 1796*0b57cec5SDimitry Andric // 1797*0b57cec5SDimitry Andric // This encoding has a couple of interesting properties: 1798*0b57cec5SDimitry Andric // 1. Looking at any entry, it is clear whether it's an address or a bitmap: 1799*0b57cec5SDimitry Andric // even means address, odd means bitmap. 1800*0b57cec5SDimitry Andric // 2. Just a simple list of addresses is a valid encoding. 1801*0b57cec5SDimitry Andric 1802*0b57cec5SDimitry Andric size_t oldSize = relrRelocs.size(); 1803*0b57cec5SDimitry Andric relrRelocs.clear(); 1804*0b57cec5SDimitry Andric 1805*0b57cec5SDimitry Andric // Same as Config->Wordsize but faster because this is a compile-time 1806*0b57cec5SDimitry Andric // constant. 1807*0b57cec5SDimitry Andric const size_t wordsize = sizeof(typename ELFT::uint); 1808*0b57cec5SDimitry Andric 1809*0b57cec5SDimitry Andric // Number of bits to use for the relocation offsets bitmap. 1810*0b57cec5SDimitry Andric // Must be either 63 or 31. 1811*0b57cec5SDimitry Andric const size_t nBits = wordsize * 8 - 1; 1812*0b57cec5SDimitry Andric 1813*0b57cec5SDimitry Andric // Get offsets for all relative relocations and sort them. 1814*0b57cec5SDimitry Andric std::vector<uint64_t> offsets; 1815*0b57cec5SDimitry Andric for (const RelativeReloc &rel : relocs) 1816*0b57cec5SDimitry Andric offsets.push_back(rel.getOffset()); 1817*0b57cec5SDimitry Andric llvm::sort(offsets); 1818*0b57cec5SDimitry Andric 1819*0b57cec5SDimitry Andric // For each leading relocation, find following ones that can be folded 1820*0b57cec5SDimitry Andric // as a bitmap and fold them. 1821*0b57cec5SDimitry Andric for (size_t i = 0, e = offsets.size(); i < e;) { 1822*0b57cec5SDimitry Andric // Add a leading relocation. 1823*0b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr(offsets[i])); 1824*0b57cec5SDimitry Andric uint64_t base = offsets[i] + wordsize; 1825*0b57cec5SDimitry Andric ++i; 1826*0b57cec5SDimitry Andric 1827*0b57cec5SDimitry Andric // Find foldable relocations to construct bitmaps. 1828*0b57cec5SDimitry Andric while (i < e) { 1829*0b57cec5SDimitry Andric uint64_t bitmap = 0; 1830*0b57cec5SDimitry Andric 1831*0b57cec5SDimitry Andric while (i < e) { 1832*0b57cec5SDimitry Andric uint64_t delta = offsets[i] - base; 1833*0b57cec5SDimitry Andric 1834*0b57cec5SDimitry Andric // If it is too far, it cannot be folded. 1835*0b57cec5SDimitry Andric if (delta >= nBits * wordsize) 1836*0b57cec5SDimitry Andric break; 1837*0b57cec5SDimitry Andric 1838*0b57cec5SDimitry Andric // If it is not a multiple of wordsize away, it cannot be folded. 1839*0b57cec5SDimitry Andric if (delta % wordsize) 1840*0b57cec5SDimitry Andric break; 1841*0b57cec5SDimitry Andric 1842*0b57cec5SDimitry Andric // Fold it. 1843*0b57cec5SDimitry Andric bitmap |= 1ULL << (delta / wordsize); 1844*0b57cec5SDimitry Andric ++i; 1845*0b57cec5SDimitry Andric } 1846*0b57cec5SDimitry Andric 1847*0b57cec5SDimitry Andric if (!bitmap) 1848*0b57cec5SDimitry Andric break; 1849*0b57cec5SDimitry Andric 1850*0b57cec5SDimitry Andric relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1)); 1851*0b57cec5SDimitry Andric base += nBits * wordsize; 1852*0b57cec5SDimitry Andric } 1853*0b57cec5SDimitry Andric } 1854*0b57cec5SDimitry Andric 1855*0b57cec5SDimitry Andric return relrRelocs.size() != oldSize; 1856*0b57cec5SDimitry Andric } 1857*0b57cec5SDimitry Andric 1858*0b57cec5SDimitry Andric SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec) 1859*0b57cec5SDimitry Andric : SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0, 1860*0b57cec5SDimitry Andric strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, 1861*0b57cec5SDimitry Andric config->wordsize, 1862*0b57cec5SDimitry Andric strTabSec.isDynamic() ? ".dynsym" : ".symtab"), 1863*0b57cec5SDimitry Andric strTabSec(strTabSec) {} 1864*0b57cec5SDimitry Andric 1865*0b57cec5SDimitry Andric // Orders symbols according to their positions in the GOT, 1866*0b57cec5SDimitry Andric // in compliance with MIPS ABI rules. 1867*0b57cec5SDimitry Andric // See "Global Offset Table" in Chapter 5 in the following document 1868*0b57cec5SDimitry Andric // for detailed description: 1869*0b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1870*0b57cec5SDimitry Andric static bool sortMipsSymbols(const SymbolTableEntry &l, 1871*0b57cec5SDimitry Andric const SymbolTableEntry &r) { 1872*0b57cec5SDimitry Andric // Sort entries related to non-local preemptible symbols by GOT indexes. 1873*0b57cec5SDimitry Andric // All other entries go to the beginning of a dynsym in arbitrary order. 1874*0b57cec5SDimitry Andric if (l.sym->isInGot() && r.sym->isInGot()) 1875*0b57cec5SDimitry Andric return l.sym->gotIndex < r.sym->gotIndex; 1876*0b57cec5SDimitry Andric if (!l.sym->isInGot() && !r.sym->isInGot()) 1877*0b57cec5SDimitry Andric return false; 1878*0b57cec5SDimitry Andric return !l.sym->isInGot(); 1879*0b57cec5SDimitry Andric } 1880*0b57cec5SDimitry Andric 1881*0b57cec5SDimitry Andric void SymbolTableBaseSection::finalizeContents() { 1882*0b57cec5SDimitry Andric if (OutputSection *sec = strTabSec.getParent()) 1883*0b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 1884*0b57cec5SDimitry Andric 1885*0b57cec5SDimitry Andric if (this->type != SHT_DYNSYM) { 1886*0b57cec5SDimitry Andric sortSymTabSymbols(); 1887*0b57cec5SDimitry Andric return; 1888*0b57cec5SDimitry Andric } 1889*0b57cec5SDimitry Andric 1890*0b57cec5SDimitry Andric // If it is a .dynsym, there should be no local symbols, but we need 1891*0b57cec5SDimitry Andric // to do a few things for the dynamic linker. 1892*0b57cec5SDimitry Andric 1893*0b57cec5SDimitry Andric // Section's Info field has the index of the first non-local symbol. 1894*0b57cec5SDimitry Andric // Because the first symbol entry is a null entry, 1 is the first. 1895*0b57cec5SDimitry Andric getParent()->info = 1; 1896*0b57cec5SDimitry Andric 1897*0b57cec5SDimitry Andric if (getPartition().gnuHashTab) { 1898*0b57cec5SDimitry Andric // NB: It also sorts Symbols to meet the GNU hash table requirements. 1899*0b57cec5SDimitry Andric getPartition().gnuHashTab->addSymbols(symbols); 1900*0b57cec5SDimitry Andric } else if (config->emachine == EM_MIPS) { 1901*0b57cec5SDimitry Andric llvm::stable_sort(symbols, sortMipsSymbols); 1902*0b57cec5SDimitry Andric } 1903*0b57cec5SDimitry Andric 1904*0b57cec5SDimitry Andric // Only the main partition's dynsym indexes are stored in the symbols 1905*0b57cec5SDimitry Andric // themselves. All other partitions use a lookup table. 1906*0b57cec5SDimitry Andric if (this == mainPart->dynSymTab) { 1907*0b57cec5SDimitry Andric size_t i = 0; 1908*0b57cec5SDimitry Andric for (const SymbolTableEntry &s : symbols) 1909*0b57cec5SDimitry Andric s.sym->dynsymIndex = ++i; 1910*0b57cec5SDimitry Andric } 1911*0b57cec5SDimitry Andric } 1912*0b57cec5SDimitry Andric 1913*0b57cec5SDimitry Andric // The ELF spec requires that all local symbols precede global symbols, so we 1914*0b57cec5SDimitry Andric // sort symbol entries in this function. (For .dynsym, we don't do that because 1915*0b57cec5SDimitry Andric // symbols for dynamic linking are inherently all globals.) 1916*0b57cec5SDimitry Andric // 1917*0b57cec5SDimitry Andric // Aside from above, we put local symbols in groups starting with the STT_FILE 1918*0b57cec5SDimitry Andric // symbol. That is convenient for purpose of identifying where are local symbols 1919*0b57cec5SDimitry Andric // coming from. 1920*0b57cec5SDimitry Andric void SymbolTableBaseSection::sortSymTabSymbols() { 1921*0b57cec5SDimitry Andric // Move all local symbols before global symbols. 1922*0b57cec5SDimitry Andric auto e = std::stable_partition( 1923*0b57cec5SDimitry Andric symbols.begin(), symbols.end(), [](const SymbolTableEntry &s) { 1924*0b57cec5SDimitry Andric return s.sym->isLocal() || s.sym->computeBinding() == STB_LOCAL; 1925*0b57cec5SDimitry Andric }); 1926*0b57cec5SDimitry Andric size_t numLocals = e - symbols.begin(); 1927*0b57cec5SDimitry Andric getParent()->info = numLocals + 1; 1928*0b57cec5SDimitry Andric 1929*0b57cec5SDimitry Andric // We want to group the local symbols by file. For that we rebuild the local 1930*0b57cec5SDimitry Andric // part of the symbols vector. We do not need to care about the STT_FILE 1931*0b57cec5SDimitry Andric // symbols, they are already naturally placed first in each group. That 1932*0b57cec5SDimitry Andric // happens because STT_FILE is always the first symbol in the object and hence 1933*0b57cec5SDimitry Andric // precede all other local symbols we add for a file. 1934*0b57cec5SDimitry Andric MapVector<InputFile *, std::vector<SymbolTableEntry>> arr; 1935*0b57cec5SDimitry Andric for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e)) 1936*0b57cec5SDimitry Andric arr[s.sym->file].push_back(s); 1937*0b57cec5SDimitry Andric 1938*0b57cec5SDimitry Andric auto i = symbols.begin(); 1939*0b57cec5SDimitry Andric for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &p : arr) 1940*0b57cec5SDimitry Andric for (SymbolTableEntry &entry : p.second) 1941*0b57cec5SDimitry Andric *i++ = entry; 1942*0b57cec5SDimitry Andric } 1943*0b57cec5SDimitry Andric 1944*0b57cec5SDimitry Andric void SymbolTableBaseSection::addSymbol(Symbol *b) { 1945*0b57cec5SDimitry Andric // Adding a local symbol to a .dynsym is a bug. 1946*0b57cec5SDimitry Andric assert(this->type != SHT_DYNSYM || !b->isLocal()); 1947*0b57cec5SDimitry Andric 1948*0b57cec5SDimitry Andric bool hashIt = b->isLocal(); 1949*0b57cec5SDimitry Andric symbols.push_back({b, strTabSec.addString(b->getName(), hashIt)}); 1950*0b57cec5SDimitry Andric } 1951*0b57cec5SDimitry Andric 1952*0b57cec5SDimitry Andric size_t SymbolTableBaseSection::getSymbolIndex(Symbol *sym) { 1953*0b57cec5SDimitry Andric if (this == mainPart->dynSymTab) 1954*0b57cec5SDimitry Andric return sym->dynsymIndex; 1955*0b57cec5SDimitry Andric 1956*0b57cec5SDimitry Andric // Initializes symbol lookup tables lazily. This is used only for -r, 1957*0b57cec5SDimitry Andric // -emit-relocs and dynsyms in partitions other than the main one. 1958*0b57cec5SDimitry Andric llvm::call_once(onceFlag, [&] { 1959*0b57cec5SDimitry Andric symbolIndexMap.reserve(symbols.size()); 1960*0b57cec5SDimitry Andric size_t i = 0; 1961*0b57cec5SDimitry Andric for (const SymbolTableEntry &e : symbols) { 1962*0b57cec5SDimitry Andric if (e.sym->type == STT_SECTION) 1963*0b57cec5SDimitry Andric sectionIndexMap[e.sym->getOutputSection()] = ++i; 1964*0b57cec5SDimitry Andric else 1965*0b57cec5SDimitry Andric symbolIndexMap[e.sym] = ++i; 1966*0b57cec5SDimitry Andric } 1967*0b57cec5SDimitry Andric }); 1968*0b57cec5SDimitry Andric 1969*0b57cec5SDimitry Andric // Section symbols are mapped based on their output sections 1970*0b57cec5SDimitry Andric // to maintain their semantics. 1971*0b57cec5SDimitry Andric if (sym->type == STT_SECTION) 1972*0b57cec5SDimitry Andric return sectionIndexMap.lookup(sym->getOutputSection()); 1973*0b57cec5SDimitry Andric return symbolIndexMap.lookup(sym); 1974*0b57cec5SDimitry Andric } 1975*0b57cec5SDimitry Andric 1976*0b57cec5SDimitry Andric template <class ELFT> 1977*0b57cec5SDimitry Andric SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec) 1978*0b57cec5SDimitry Andric : SymbolTableBaseSection(strTabSec) { 1979*0b57cec5SDimitry Andric this->entsize = sizeof(Elf_Sym); 1980*0b57cec5SDimitry Andric } 1981*0b57cec5SDimitry Andric 1982*0b57cec5SDimitry Andric static BssSection *getCommonSec(Symbol *sym) { 1983*0b57cec5SDimitry Andric if (!config->defineCommon) 1984*0b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 1985*0b57cec5SDimitry Andric return dyn_cast_or_null<BssSection>(d->section); 1986*0b57cec5SDimitry Andric return nullptr; 1987*0b57cec5SDimitry Andric } 1988*0b57cec5SDimitry Andric 1989*0b57cec5SDimitry Andric static uint32_t getSymSectionIndex(Symbol *sym) { 1990*0b57cec5SDimitry Andric if (getCommonSec(sym)) 1991*0b57cec5SDimitry Andric return SHN_COMMON; 1992*0b57cec5SDimitry Andric if (!isa<Defined>(sym) || sym->needsPltAddr) 1993*0b57cec5SDimitry Andric return SHN_UNDEF; 1994*0b57cec5SDimitry Andric if (const OutputSection *os = sym->getOutputSection()) 1995*0b57cec5SDimitry Andric return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX 1996*0b57cec5SDimitry Andric : os->sectionIndex; 1997*0b57cec5SDimitry Andric return SHN_ABS; 1998*0b57cec5SDimitry Andric } 1999*0b57cec5SDimitry Andric 2000*0b57cec5SDimitry Andric // Write the internal symbol table contents to the output symbol table. 2001*0b57cec5SDimitry Andric template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) { 2002*0b57cec5SDimitry Andric // The first entry is a null entry as per the ELF spec. 2003*0b57cec5SDimitry Andric memset(buf, 0, sizeof(Elf_Sym)); 2004*0b57cec5SDimitry Andric buf += sizeof(Elf_Sym); 2005*0b57cec5SDimitry Andric 2006*0b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 2007*0b57cec5SDimitry Andric 2008*0b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 2009*0b57cec5SDimitry Andric Symbol *sym = ent.sym; 2010*0b57cec5SDimitry Andric bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition; 2011*0b57cec5SDimitry Andric 2012*0b57cec5SDimitry Andric // Set st_info and st_other. 2013*0b57cec5SDimitry Andric eSym->st_other = 0; 2014*0b57cec5SDimitry Andric if (sym->isLocal()) { 2015*0b57cec5SDimitry Andric eSym->setBindingAndType(STB_LOCAL, sym->type); 2016*0b57cec5SDimitry Andric } else { 2017*0b57cec5SDimitry Andric eSym->setBindingAndType(sym->computeBinding(), sym->type); 2018*0b57cec5SDimitry Andric eSym->setVisibility(sym->visibility); 2019*0b57cec5SDimitry Andric } 2020*0b57cec5SDimitry Andric 2021*0b57cec5SDimitry Andric // The 3 most significant bits of st_other are used by OpenPOWER ABI. 2022*0b57cec5SDimitry Andric // See getPPC64GlobalEntryToLocalEntryOffset() for more details. 2023*0b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 2024*0b57cec5SDimitry Andric eSym->st_other |= sym->stOther & 0xe0; 2025*0b57cec5SDimitry Andric 2026*0b57cec5SDimitry Andric eSym->st_name = ent.strTabOffset; 2027*0b57cec5SDimitry Andric if (isDefinedHere) 2028*0b57cec5SDimitry Andric eSym->st_shndx = getSymSectionIndex(ent.sym); 2029*0b57cec5SDimitry Andric else 2030*0b57cec5SDimitry Andric eSym->st_shndx = 0; 2031*0b57cec5SDimitry Andric 2032*0b57cec5SDimitry Andric // Copy symbol size if it is a defined symbol. st_size is not significant 2033*0b57cec5SDimitry Andric // for undefined symbols, so whether copying it or not is up to us if that's 2034*0b57cec5SDimitry Andric // the case. We'll leave it as zero because by not setting a value, we can 2035*0b57cec5SDimitry Andric // get the exact same outputs for two sets of input files that differ only 2036*0b57cec5SDimitry Andric // in undefined symbol size in DSOs. 2037*0b57cec5SDimitry Andric if (eSym->st_shndx == SHN_UNDEF || !isDefinedHere) 2038*0b57cec5SDimitry Andric eSym->st_size = 0; 2039*0b57cec5SDimitry Andric else 2040*0b57cec5SDimitry Andric eSym->st_size = sym->getSize(); 2041*0b57cec5SDimitry Andric 2042*0b57cec5SDimitry Andric // st_value is usually an address of a symbol, but that has a 2043*0b57cec5SDimitry Andric // special meaining for uninstantiated common symbols (this can 2044*0b57cec5SDimitry Andric // occur if -r is given). 2045*0b57cec5SDimitry Andric if (BssSection *commonSec = getCommonSec(ent.sym)) 2046*0b57cec5SDimitry Andric eSym->st_value = commonSec->alignment; 2047*0b57cec5SDimitry Andric else if (isDefinedHere) 2048*0b57cec5SDimitry Andric eSym->st_value = sym->getVA(); 2049*0b57cec5SDimitry Andric else 2050*0b57cec5SDimitry Andric eSym->st_value = 0; 2051*0b57cec5SDimitry Andric 2052*0b57cec5SDimitry Andric ++eSym; 2053*0b57cec5SDimitry Andric } 2054*0b57cec5SDimitry Andric 2055*0b57cec5SDimitry Andric // On MIPS we need to mark symbol which has a PLT entry and requires 2056*0b57cec5SDimitry Andric // pointer equality by STO_MIPS_PLT flag. That is necessary to help 2057*0b57cec5SDimitry Andric // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. 2058*0b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2008-07/txt00000.txt 2059*0b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 2060*0b57cec5SDimitry Andric auto *eSym = reinterpret_cast<Elf_Sym *>(buf); 2061*0b57cec5SDimitry Andric 2062*0b57cec5SDimitry Andric for (SymbolTableEntry &ent : symbols) { 2063*0b57cec5SDimitry Andric Symbol *sym = ent.sym; 2064*0b57cec5SDimitry Andric if (sym->isInPlt() && sym->needsPltAddr) 2065*0b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PLT; 2066*0b57cec5SDimitry Andric if (isMicroMips()) { 2067*0b57cec5SDimitry Andric // We already set the less-significant bit for symbols 2068*0b57cec5SDimitry Andric // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT 2069*0b57cec5SDimitry Andric // records. That allows us to distinguish such symbols in 2070*0b57cec5SDimitry Andric // the `MIPS<ELFT>::relocateOne()` routine. Now we should 2071*0b57cec5SDimitry Andric // clear that bit for non-dynamic symbol table, so tools 2072*0b57cec5SDimitry Andric // like `objdump` will be able to deal with a correct 2073*0b57cec5SDimitry Andric // symbol position. 2074*0b57cec5SDimitry Andric if (sym->isDefined() && 2075*0b57cec5SDimitry Andric ((sym->stOther & STO_MIPS_MICROMIPS) || sym->needsPltAddr)) { 2076*0b57cec5SDimitry Andric if (!strTabSec.isDynamic()) 2077*0b57cec5SDimitry Andric eSym->st_value &= ~1; 2078*0b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_MICROMIPS; 2079*0b57cec5SDimitry Andric } 2080*0b57cec5SDimitry Andric } 2081*0b57cec5SDimitry Andric if (config->relocatable) 2082*0b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 2083*0b57cec5SDimitry Andric if (isMipsPIC<ELFT>(d)) 2084*0b57cec5SDimitry Andric eSym->st_other |= STO_MIPS_PIC; 2085*0b57cec5SDimitry Andric ++eSym; 2086*0b57cec5SDimitry Andric } 2087*0b57cec5SDimitry Andric } 2088*0b57cec5SDimitry Andric } 2089*0b57cec5SDimitry Andric 2090*0b57cec5SDimitry Andric SymtabShndxSection::SymtabShndxSection() 2091*0b57cec5SDimitry Andric : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndx") { 2092*0b57cec5SDimitry Andric this->entsize = 4; 2093*0b57cec5SDimitry Andric } 2094*0b57cec5SDimitry Andric 2095*0b57cec5SDimitry Andric void SymtabShndxSection::writeTo(uint8_t *buf) { 2096*0b57cec5SDimitry Andric // We write an array of 32 bit values, where each value has 1:1 association 2097*0b57cec5SDimitry Andric // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX, 2098*0b57cec5SDimitry Andric // we need to write actual index, otherwise, we must write SHN_UNDEF(0). 2099*0b57cec5SDimitry Andric buf += 4; // Ignore .symtab[0] entry. 2100*0b57cec5SDimitry Andric for (const SymbolTableEntry &entry : in.symTab->getSymbols()) { 2101*0b57cec5SDimitry Andric if (getSymSectionIndex(entry.sym) == SHN_XINDEX) 2102*0b57cec5SDimitry Andric write32(buf, entry.sym->getOutputSection()->sectionIndex); 2103*0b57cec5SDimitry Andric buf += 4; 2104*0b57cec5SDimitry Andric } 2105*0b57cec5SDimitry Andric } 2106*0b57cec5SDimitry Andric 2107*0b57cec5SDimitry Andric bool SymtabShndxSection::isNeeded() const { 2108*0b57cec5SDimitry Andric // SHT_SYMTAB can hold symbols with section indices values up to 2109*0b57cec5SDimitry Andric // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX 2110*0b57cec5SDimitry Andric // section. Problem is that we reveal the final section indices a bit too 2111*0b57cec5SDimitry Andric // late, and we do not know them here. For simplicity, we just always create 2112*0b57cec5SDimitry Andric // a .symtab_shndx section when the amount of output sections is huge. 2113*0b57cec5SDimitry Andric size_t size = 0; 2114*0b57cec5SDimitry Andric for (BaseCommand *base : script->sectionCommands) 2115*0b57cec5SDimitry Andric if (isa<OutputSection>(base)) 2116*0b57cec5SDimitry Andric ++size; 2117*0b57cec5SDimitry Andric return size >= SHN_LORESERVE; 2118*0b57cec5SDimitry Andric } 2119*0b57cec5SDimitry Andric 2120*0b57cec5SDimitry Andric void SymtabShndxSection::finalizeContents() { 2121*0b57cec5SDimitry Andric getParent()->link = in.symTab->getParent()->sectionIndex; 2122*0b57cec5SDimitry Andric } 2123*0b57cec5SDimitry Andric 2124*0b57cec5SDimitry Andric size_t SymtabShndxSection::getSize() const { 2125*0b57cec5SDimitry Andric return in.symTab->getNumSymbols() * 4; 2126*0b57cec5SDimitry Andric } 2127*0b57cec5SDimitry Andric 2128*0b57cec5SDimitry Andric // .hash and .gnu.hash sections contain on-disk hash tables that map 2129*0b57cec5SDimitry Andric // symbol names to their dynamic symbol table indices. Their purpose 2130*0b57cec5SDimitry Andric // is to help the dynamic linker resolve symbols quickly. If ELF files 2131*0b57cec5SDimitry Andric // don't have them, the dynamic linker has to do linear search on all 2132*0b57cec5SDimitry Andric // dynamic symbols, which makes programs slower. Therefore, a .hash 2133*0b57cec5SDimitry Andric // section is added to a DSO by default. A .gnu.hash is added if you 2134*0b57cec5SDimitry Andric // give the -hash-style=gnu or -hash-style=both option. 2135*0b57cec5SDimitry Andric // 2136*0b57cec5SDimitry Andric // The Unix semantics of resolving dynamic symbols is somewhat expensive. 2137*0b57cec5SDimitry Andric // Each ELF file has a list of DSOs that the ELF file depends on and a 2138*0b57cec5SDimitry Andric // list of dynamic symbols that need to be resolved from any of the 2139*0b57cec5SDimitry Andric // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) 2140*0b57cec5SDimitry Andric // where m is the number of DSOs and n is the number of dynamic 2141*0b57cec5SDimitry Andric // symbols. For modern large programs, both m and n are large. So 2142*0b57cec5SDimitry Andric // making each step faster by using hash tables substiantially 2143*0b57cec5SDimitry Andric // improves time to load programs. 2144*0b57cec5SDimitry Andric // 2145*0b57cec5SDimitry Andric // (Note that this is not the only way to design the shared library. 2146*0b57cec5SDimitry Andric // For instance, the Windows DLL takes a different approach. On 2147*0b57cec5SDimitry Andric // Windows, each dynamic symbol has a name of DLL from which the symbol 2148*0b57cec5SDimitry Andric // has to be resolved. That makes the cost of symbol resolution O(n). 2149*0b57cec5SDimitry Andric // This disables some hacky techniques you can use on Unix such as 2150*0b57cec5SDimitry Andric // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) 2151*0b57cec5SDimitry Andric // 2152*0b57cec5SDimitry Andric // Due to historical reasons, we have two different hash tables, .hash 2153*0b57cec5SDimitry Andric // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new 2154*0b57cec5SDimitry Andric // and better version of .hash. .hash is just an on-disk hash table, but 2155*0b57cec5SDimitry Andric // .gnu.hash has a bloom filter in addition to a hash table to skip 2156*0b57cec5SDimitry Andric // DSOs very quickly. If you are sure that your dynamic linker knows 2157*0b57cec5SDimitry Andric // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a 2158*0b57cec5SDimitry Andric // safe bet is to specify -hash-style=both for backward compatibilty. 2159*0b57cec5SDimitry Andric GnuHashTableSection::GnuHashTableSection() 2160*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, config->wordsize, ".gnu.hash") { 2161*0b57cec5SDimitry Andric } 2162*0b57cec5SDimitry Andric 2163*0b57cec5SDimitry Andric void GnuHashTableSection::finalizeContents() { 2164*0b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynSymTab->getParent()) 2165*0b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 2166*0b57cec5SDimitry Andric 2167*0b57cec5SDimitry Andric // Computes bloom filter size in word size. We want to allocate 12 2168*0b57cec5SDimitry Andric // bits for each symbol. It must be a power of two. 2169*0b57cec5SDimitry Andric if (symbols.empty()) { 2170*0b57cec5SDimitry Andric maskWords = 1; 2171*0b57cec5SDimitry Andric } else { 2172*0b57cec5SDimitry Andric uint64_t numBits = symbols.size() * 12; 2173*0b57cec5SDimitry Andric maskWords = NextPowerOf2(numBits / (config->wordsize * 8)); 2174*0b57cec5SDimitry Andric } 2175*0b57cec5SDimitry Andric 2176*0b57cec5SDimitry Andric size = 16; // Header 2177*0b57cec5SDimitry Andric size += config->wordsize * maskWords; // Bloom filter 2178*0b57cec5SDimitry Andric size += nBuckets * 4; // Hash buckets 2179*0b57cec5SDimitry Andric size += symbols.size() * 4; // Hash values 2180*0b57cec5SDimitry Andric } 2181*0b57cec5SDimitry Andric 2182*0b57cec5SDimitry Andric void GnuHashTableSection::writeTo(uint8_t *buf) { 2183*0b57cec5SDimitry Andric // The output buffer is not guaranteed to be zero-cleared because we pre- 2184*0b57cec5SDimitry Andric // fill executable sections with trap instructions. This is a precaution 2185*0b57cec5SDimitry Andric // for that case, which happens only when -no-rosegment is given. 2186*0b57cec5SDimitry Andric memset(buf, 0, size); 2187*0b57cec5SDimitry Andric 2188*0b57cec5SDimitry Andric // Write a header. 2189*0b57cec5SDimitry Andric write32(buf, nBuckets); 2190*0b57cec5SDimitry Andric write32(buf + 4, getPartition().dynSymTab->getNumSymbols() - symbols.size()); 2191*0b57cec5SDimitry Andric write32(buf + 8, maskWords); 2192*0b57cec5SDimitry Andric write32(buf + 12, Shift2); 2193*0b57cec5SDimitry Andric buf += 16; 2194*0b57cec5SDimitry Andric 2195*0b57cec5SDimitry Andric // Write a bloom filter and a hash table. 2196*0b57cec5SDimitry Andric writeBloomFilter(buf); 2197*0b57cec5SDimitry Andric buf += config->wordsize * maskWords; 2198*0b57cec5SDimitry Andric writeHashTable(buf); 2199*0b57cec5SDimitry Andric } 2200*0b57cec5SDimitry Andric 2201*0b57cec5SDimitry Andric // This function writes a 2-bit bloom filter. This bloom filter alone 2202*0b57cec5SDimitry Andric // usually filters out 80% or more of all symbol lookups [1]. 2203*0b57cec5SDimitry Andric // The dynamic linker uses the hash table only when a symbol is not 2204*0b57cec5SDimitry Andric // filtered out by a bloom filter. 2205*0b57cec5SDimitry Andric // 2206*0b57cec5SDimitry Andric // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2), 2207*0b57cec5SDimitry Andric // p.9, https://www.akkadia.org/drepper/dsohowto.pdf 2208*0b57cec5SDimitry Andric void GnuHashTableSection::writeBloomFilter(uint8_t *buf) { 2209*0b57cec5SDimitry Andric unsigned c = config->is64 ? 64 : 32; 2210*0b57cec5SDimitry Andric for (const Entry &sym : symbols) { 2211*0b57cec5SDimitry Andric // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in 2212*0b57cec5SDimitry Andric // the word using bits [0:5] and [26:31]. 2213*0b57cec5SDimitry Andric size_t i = (sym.hash / c) & (maskWords - 1); 2214*0b57cec5SDimitry Andric uint64_t val = readUint(buf + i * config->wordsize); 2215*0b57cec5SDimitry Andric val |= uint64_t(1) << (sym.hash % c); 2216*0b57cec5SDimitry Andric val |= uint64_t(1) << ((sym.hash >> Shift2) % c); 2217*0b57cec5SDimitry Andric writeUint(buf + i * config->wordsize, val); 2218*0b57cec5SDimitry Andric } 2219*0b57cec5SDimitry Andric } 2220*0b57cec5SDimitry Andric 2221*0b57cec5SDimitry Andric void GnuHashTableSection::writeHashTable(uint8_t *buf) { 2222*0b57cec5SDimitry Andric uint32_t *buckets = reinterpret_cast<uint32_t *>(buf); 2223*0b57cec5SDimitry Andric uint32_t oldBucket = -1; 2224*0b57cec5SDimitry Andric uint32_t *values = buckets + nBuckets; 2225*0b57cec5SDimitry Andric for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) { 2226*0b57cec5SDimitry Andric // Write a hash value. It represents a sequence of chains that share the 2227*0b57cec5SDimitry Andric // same hash modulo value. The last element of each chain is terminated by 2228*0b57cec5SDimitry Andric // LSB 1. 2229*0b57cec5SDimitry Andric uint32_t hash = i->hash; 2230*0b57cec5SDimitry Andric bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx; 2231*0b57cec5SDimitry Andric hash = isLastInChain ? hash | 1 : hash & ~1; 2232*0b57cec5SDimitry Andric write32(values++, hash); 2233*0b57cec5SDimitry Andric 2234*0b57cec5SDimitry Andric if (i->bucketIdx == oldBucket) 2235*0b57cec5SDimitry Andric continue; 2236*0b57cec5SDimitry Andric // Write a hash bucket. Hash buckets contain indices in the following hash 2237*0b57cec5SDimitry Andric // value table. 2238*0b57cec5SDimitry Andric write32(buckets + i->bucketIdx, 2239*0b57cec5SDimitry Andric getPartition().dynSymTab->getSymbolIndex(i->sym)); 2240*0b57cec5SDimitry Andric oldBucket = i->bucketIdx; 2241*0b57cec5SDimitry Andric } 2242*0b57cec5SDimitry Andric } 2243*0b57cec5SDimitry Andric 2244*0b57cec5SDimitry Andric static uint32_t hashGnu(StringRef name) { 2245*0b57cec5SDimitry Andric uint32_t h = 5381; 2246*0b57cec5SDimitry Andric for (uint8_t c : name) 2247*0b57cec5SDimitry Andric h = (h << 5) + h + c; 2248*0b57cec5SDimitry Andric return h; 2249*0b57cec5SDimitry Andric } 2250*0b57cec5SDimitry Andric 2251*0b57cec5SDimitry Andric // Add symbols to this symbol hash table. Note that this function 2252*0b57cec5SDimitry Andric // destructively sort a given vector -- which is needed because 2253*0b57cec5SDimitry Andric // GNU-style hash table places some sorting requirements. 2254*0b57cec5SDimitry Andric void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &v) { 2255*0b57cec5SDimitry Andric // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce 2256*0b57cec5SDimitry Andric // its type correctly. 2257*0b57cec5SDimitry Andric std::vector<SymbolTableEntry>::iterator mid = 2258*0b57cec5SDimitry Andric std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) { 2259*0b57cec5SDimitry Andric return !s.sym->isDefined() || s.sym->partition != partition; 2260*0b57cec5SDimitry Andric }); 2261*0b57cec5SDimitry Andric 2262*0b57cec5SDimitry Andric // We chose load factor 4 for the on-disk hash table. For each hash 2263*0b57cec5SDimitry Andric // collision, the dynamic linker will compare a uint32_t hash value. 2264*0b57cec5SDimitry Andric // Since the integer comparison is quite fast, we believe we can 2265*0b57cec5SDimitry Andric // make the load factor even larger. 4 is just a conservative choice. 2266*0b57cec5SDimitry Andric // 2267*0b57cec5SDimitry Andric // Note that we don't want to create a zero-sized hash table because 2268*0b57cec5SDimitry Andric // Android loader as of 2018 doesn't like a .gnu.hash containing such 2269*0b57cec5SDimitry Andric // table. If that's the case, we create a hash table with one unused 2270*0b57cec5SDimitry Andric // dummy slot. 2271*0b57cec5SDimitry Andric nBuckets = std::max<size_t>((v.end() - mid) / 4, 1); 2272*0b57cec5SDimitry Andric 2273*0b57cec5SDimitry Andric if (mid == v.end()) 2274*0b57cec5SDimitry Andric return; 2275*0b57cec5SDimitry Andric 2276*0b57cec5SDimitry Andric for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) { 2277*0b57cec5SDimitry Andric Symbol *b = ent.sym; 2278*0b57cec5SDimitry Andric uint32_t hash = hashGnu(b->getName()); 2279*0b57cec5SDimitry Andric uint32_t bucketIdx = hash % nBuckets; 2280*0b57cec5SDimitry Andric symbols.push_back({b, ent.strTabOffset, hash, bucketIdx}); 2281*0b57cec5SDimitry Andric } 2282*0b57cec5SDimitry Andric 2283*0b57cec5SDimitry Andric llvm::stable_sort(symbols, [](const Entry &l, const Entry &r) { 2284*0b57cec5SDimitry Andric return l.bucketIdx < r.bucketIdx; 2285*0b57cec5SDimitry Andric }); 2286*0b57cec5SDimitry Andric 2287*0b57cec5SDimitry Andric v.erase(mid, v.end()); 2288*0b57cec5SDimitry Andric for (const Entry &ent : symbols) 2289*0b57cec5SDimitry Andric v.push_back({ent.sym, ent.strTabOffset}); 2290*0b57cec5SDimitry Andric } 2291*0b57cec5SDimitry Andric 2292*0b57cec5SDimitry Andric HashTableSection::HashTableSection() 2293*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { 2294*0b57cec5SDimitry Andric this->entsize = 4; 2295*0b57cec5SDimitry Andric } 2296*0b57cec5SDimitry Andric 2297*0b57cec5SDimitry Andric void HashTableSection::finalizeContents() { 2298*0b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 2299*0b57cec5SDimitry Andric 2300*0b57cec5SDimitry Andric if (OutputSection *sec = symTab->getParent()) 2301*0b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 2302*0b57cec5SDimitry Andric 2303*0b57cec5SDimitry Andric unsigned numEntries = 2; // nbucket and nchain. 2304*0b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); // The chain entries. 2305*0b57cec5SDimitry Andric 2306*0b57cec5SDimitry Andric // Create as many buckets as there are symbols. 2307*0b57cec5SDimitry Andric numEntries += symTab->getNumSymbols(); 2308*0b57cec5SDimitry Andric this->size = numEntries * 4; 2309*0b57cec5SDimitry Andric } 2310*0b57cec5SDimitry Andric 2311*0b57cec5SDimitry Andric void HashTableSection::writeTo(uint8_t *buf) { 2312*0b57cec5SDimitry Andric SymbolTableBaseSection *symTab = getPartition().dynSymTab; 2313*0b57cec5SDimitry Andric 2314*0b57cec5SDimitry Andric // See comment in GnuHashTableSection::writeTo. 2315*0b57cec5SDimitry Andric memset(buf, 0, size); 2316*0b57cec5SDimitry Andric 2317*0b57cec5SDimitry Andric unsigned numSymbols = symTab->getNumSymbols(); 2318*0b57cec5SDimitry Andric 2319*0b57cec5SDimitry Andric uint32_t *p = reinterpret_cast<uint32_t *>(buf); 2320*0b57cec5SDimitry Andric write32(p++, numSymbols); // nbucket 2321*0b57cec5SDimitry Andric write32(p++, numSymbols); // nchain 2322*0b57cec5SDimitry Andric 2323*0b57cec5SDimitry Andric uint32_t *buckets = p; 2324*0b57cec5SDimitry Andric uint32_t *chains = p + numSymbols; 2325*0b57cec5SDimitry Andric 2326*0b57cec5SDimitry Andric for (const SymbolTableEntry &s : symTab->getSymbols()) { 2327*0b57cec5SDimitry Andric Symbol *sym = s.sym; 2328*0b57cec5SDimitry Andric StringRef name = sym->getName(); 2329*0b57cec5SDimitry Andric unsigned i = sym->dynsymIndex; 2330*0b57cec5SDimitry Andric uint32_t hash = hashSysV(name) % numSymbols; 2331*0b57cec5SDimitry Andric chains[i] = buckets[hash]; 2332*0b57cec5SDimitry Andric write32(buckets + hash, i); 2333*0b57cec5SDimitry Andric } 2334*0b57cec5SDimitry Andric } 2335*0b57cec5SDimitry Andric 2336*0b57cec5SDimitry Andric // On PowerPC64 the lazy symbol resolvers go into the `global linkage table` 2337*0b57cec5SDimitry Andric // in the .glink section, rather then the typical .plt section. 2338*0b57cec5SDimitry Andric PltSection::PltSection(bool isIplt) 2339*0b57cec5SDimitry Andric : SyntheticSection( 2340*0b57cec5SDimitry Andric SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, 2341*0b57cec5SDimitry Andric (config->emachine == EM_PPC || config->emachine == EM_PPC64) 2342*0b57cec5SDimitry Andric ? ".glink" 2343*0b57cec5SDimitry Andric : ".plt"), 2344*0b57cec5SDimitry Andric headerSize(!isIplt || config->zRetpolineplt ? target->pltHeaderSize : 0), 2345*0b57cec5SDimitry Andric isIplt(isIplt) { 2346*0b57cec5SDimitry Andric // The PLT needs to be writable on SPARC as the dynamic linker will 2347*0b57cec5SDimitry Andric // modify the instructions in the PLT entries. 2348*0b57cec5SDimitry Andric if (config->emachine == EM_SPARCV9) 2349*0b57cec5SDimitry Andric this->flags |= SHF_WRITE; 2350*0b57cec5SDimitry Andric } 2351*0b57cec5SDimitry Andric 2352*0b57cec5SDimitry Andric void PltSection::writeTo(uint8_t *buf) { 2353*0b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 2354*0b57cec5SDimitry Andric writePPC32GlinkSection(buf, entries.size()); 2355*0b57cec5SDimitry Andric return; 2356*0b57cec5SDimitry Andric } 2357*0b57cec5SDimitry Andric 2358*0b57cec5SDimitry Andric // At beginning of PLT or retpoline IPLT, we have code to call the dynamic 2359*0b57cec5SDimitry Andric // linker to resolve dynsyms at runtime. Write such code. 2360*0b57cec5SDimitry Andric if (headerSize) 2361*0b57cec5SDimitry Andric target->writePltHeader(buf); 2362*0b57cec5SDimitry Andric size_t off = headerSize; 2363*0b57cec5SDimitry Andric 2364*0b57cec5SDimitry Andric RelocationBaseSection *relSec = isIplt ? in.relaIplt : in.relaPlt; 2365*0b57cec5SDimitry Andric 2366*0b57cec5SDimitry Andric // The IPlt is immediately after the Plt, account for this in relOff 2367*0b57cec5SDimitry Andric size_t pltOff = isIplt ? in.plt->getSize() : 0; 2368*0b57cec5SDimitry Andric 2369*0b57cec5SDimitry Andric for (size_t i = 0, e = entries.size(); i != e; ++i) { 2370*0b57cec5SDimitry Andric const Symbol *b = entries[i]; 2371*0b57cec5SDimitry Andric unsigned relOff = relSec->entsize * i + pltOff; 2372*0b57cec5SDimitry Andric uint64_t got = b->getGotPltVA(); 2373*0b57cec5SDimitry Andric uint64_t plt = this->getVA() + off; 2374*0b57cec5SDimitry Andric target->writePlt(buf + off, got, plt, b->pltIndex, relOff); 2375*0b57cec5SDimitry Andric off += target->pltEntrySize; 2376*0b57cec5SDimitry Andric } 2377*0b57cec5SDimitry Andric } 2378*0b57cec5SDimitry Andric 2379*0b57cec5SDimitry Andric template <class ELFT> void PltSection::addEntry(Symbol &sym) { 2380*0b57cec5SDimitry Andric sym.pltIndex = entries.size(); 2381*0b57cec5SDimitry Andric entries.push_back(&sym); 2382*0b57cec5SDimitry Andric } 2383*0b57cec5SDimitry Andric 2384*0b57cec5SDimitry Andric size_t PltSection::getSize() const { 2385*0b57cec5SDimitry Andric return headerSize + entries.size() * target->pltEntrySize; 2386*0b57cec5SDimitry Andric } 2387*0b57cec5SDimitry Andric 2388*0b57cec5SDimitry Andric // Some architectures such as additional symbols in the PLT section. For 2389*0b57cec5SDimitry Andric // example ARM uses mapping symbols to aid disassembly 2390*0b57cec5SDimitry Andric void PltSection::addSymbols() { 2391*0b57cec5SDimitry Andric // The PLT may have symbols defined for the Header, the IPLT has no header 2392*0b57cec5SDimitry Andric if (!isIplt) 2393*0b57cec5SDimitry Andric target->addPltHeaderSymbols(*this); 2394*0b57cec5SDimitry Andric 2395*0b57cec5SDimitry Andric size_t off = headerSize; 2396*0b57cec5SDimitry Andric for (size_t i = 0; i < entries.size(); ++i) { 2397*0b57cec5SDimitry Andric target->addPltSymbols(*this, off); 2398*0b57cec5SDimitry Andric off += target->pltEntrySize; 2399*0b57cec5SDimitry Andric } 2400*0b57cec5SDimitry Andric } 2401*0b57cec5SDimitry Andric 2402*0b57cec5SDimitry Andric // The string hash function for .gdb_index. 2403*0b57cec5SDimitry Andric static uint32_t computeGdbHash(StringRef s) { 2404*0b57cec5SDimitry Andric uint32_t h = 0; 2405*0b57cec5SDimitry Andric for (uint8_t c : s) 2406*0b57cec5SDimitry Andric h = h * 67 + toLower(c) - 113; 2407*0b57cec5SDimitry Andric return h; 2408*0b57cec5SDimitry Andric } 2409*0b57cec5SDimitry Andric 2410*0b57cec5SDimitry Andric GdbIndexSection::GdbIndexSection() 2411*0b57cec5SDimitry Andric : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {} 2412*0b57cec5SDimitry Andric 2413*0b57cec5SDimitry Andric // Returns the desired size of an on-disk hash table for a .gdb_index section. 2414*0b57cec5SDimitry Andric // There's a tradeoff between size and collision rate. We aim 75% utilization. 2415*0b57cec5SDimitry Andric size_t GdbIndexSection::computeSymtabSize() const { 2416*0b57cec5SDimitry Andric return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024); 2417*0b57cec5SDimitry Andric } 2418*0b57cec5SDimitry Andric 2419*0b57cec5SDimitry Andric // Compute the output section size. 2420*0b57cec5SDimitry Andric void GdbIndexSection::initOutputSize() { 2421*0b57cec5SDimitry Andric size = sizeof(GdbIndexHeader) + computeSymtabSize() * 8; 2422*0b57cec5SDimitry Andric 2423*0b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) 2424*0b57cec5SDimitry Andric size += chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20; 2425*0b57cec5SDimitry Andric 2426*0b57cec5SDimitry Andric // Add the constant pool size if exists. 2427*0b57cec5SDimitry Andric if (!symbols.empty()) { 2428*0b57cec5SDimitry Andric GdbSymbol &sym = symbols.back(); 2429*0b57cec5SDimitry Andric size += sym.nameOff + sym.name.size() + 1; 2430*0b57cec5SDimitry Andric } 2431*0b57cec5SDimitry Andric } 2432*0b57cec5SDimitry Andric 2433*0b57cec5SDimitry Andric static std::vector<InputSection *> getDebugInfoSections() { 2434*0b57cec5SDimitry Andric std::vector<InputSection *> ret; 2435*0b57cec5SDimitry Andric for (InputSectionBase *s : inputSections) 2436*0b57cec5SDimitry Andric if (InputSection *isec = dyn_cast<InputSection>(s)) 2437*0b57cec5SDimitry Andric if (isec->name == ".debug_info") 2438*0b57cec5SDimitry Andric ret.push_back(isec); 2439*0b57cec5SDimitry Andric return ret; 2440*0b57cec5SDimitry Andric } 2441*0b57cec5SDimitry Andric 2442*0b57cec5SDimitry Andric static std::vector<GdbIndexSection::CuEntry> readCuList(DWARFContext &dwarf) { 2443*0b57cec5SDimitry Andric std::vector<GdbIndexSection::CuEntry> ret; 2444*0b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) 2445*0b57cec5SDimitry Andric ret.push_back({cu->getOffset(), cu->getLength() + 4}); 2446*0b57cec5SDimitry Andric return ret; 2447*0b57cec5SDimitry Andric } 2448*0b57cec5SDimitry Andric 2449*0b57cec5SDimitry Andric static std::vector<GdbIndexSection::AddressEntry> 2450*0b57cec5SDimitry Andric readAddressAreas(DWARFContext &dwarf, InputSection *sec) { 2451*0b57cec5SDimitry Andric std::vector<GdbIndexSection::AddressEntry> ret; 2452*0b57cec5SDimitry Andric 2453*0b57cec5SDimitry Andric uint32_t cuIdx = 0; 2454*0b57cec5SDimitry Andric for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) { 2455*0b57cec5SDimitry Andric Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges(); 2456*0b57cec5SDimitry Andric if (!ranges) { 2457*0b57cec5SDimitry Andric error(toString(sec) + ": " + toString(ranges.takeError())); 2458*0b57cec5SDimitry Andric return {}; 2459*0b57cec5SDimitry Andric } 2460*0b57cec5SDimitry Andric 2461*0b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = sec->file->getSections(); 2462*0b57cec5SDimitry Andric for (DWARFAddressRange &r : *ranges) { 2463*0b57cec5SDimitry Andric if (r.SectionIndex == -1ULL) 2464*0b57cec5SDimitry Andric continue; 2465*0b57cec5SDimitry Andric InputSectionBase *s = sections[r.SectionIndex]; 2466*0b57cec5SDimitry Andric if (!s || s == &InputSection::discarded || !s->isLive()) 2467*0b57cec5SDimitry Andric continue; 2468*0b57cec5SDimitry Andric // Range list with zero size has no effect. 2469*0b57cec5SDimitry Andric if (r.LowPC == r.HighPC) 2470*0b57cec5SDimitry Andric continue; 2471*0b57cec5SDimitry Andric auto *isec = cast<InputSection>(s); 2472*0b57cec5SDimitry Andric uint64_t offset = isec->getOffsetInFile(); 2473*0b57cec5SDimitry Andric ret.push_back({isec, r.LowPC - offset, r.HighPC - offset, cuIdx}); 2474*0b57cec5SDimitry Andric } 2475*0b57cec5SDimitry Andric ++cuIdx; 2476*0b57cec5SDimitry Andric } 2477*0b57cec5SDimitry Andric 2478*0b57cec5SDimitry Andric return ret; 2479*0b57cec5SDimitry Andric } 2480*0b57cec5SDimitry Andric 2481*0b57cec5SDimitry Andric template <class ELFT> 2482*0b57cec5SDimitry Andric static std::vector<GdbIndexSection::NameAttrEntry> 2483*0b57cec5SDimitry Andric readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj, 2484*0b57cec5SDimitry Andric const std::vector<GdbIndexSection::CuEntry> &cUs) { 2485*0b57cec5SDimitry Andric const DWARFSection &pubNames = obj.getGnuPubNamesSection(); 2486*0b57cec5SDimitry Andric const DWARFSection &pubTypes = obj.getGnuPubTypesSection(); 2487*0b57cec5SDimitry Andric 2488*0b57cec5SDimitry Andric std::vector<GdbIndexSection::NameAttrEntry> ret; 2489*0b57cec5SDimitry Andric for (const DWARFSection *pub : {&pubNames, &pubTypes}) { 2490*0b57cec5SDimitry Andric DWARFDebugPubTable table(obj, *pub, config->isLE, true); 2491*0b57cec5SDimitry Andric for (const DWARFDebugPubTable::Set &set : table.getData()) { 2492*0b57cec5SDimitry Andric // The value written into the constant pool is kind << 24 | cuIndex. As we 2493*0b57cec5SDimitry Andric // don't know how many compilation units precede this object to compute 2494*0b57cec5SDimitry Andric // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add 2495*0b57cec5SDimitry Andric // the number of preceding compilation units later. 2496*0b57cec5SDimitry Andric uint32_t i = 2497*0b57cec5SDimitry Andric lower_bound(cUs, set.Offset, 2498*0b57cec5SDimitry Andric [](GdbIndexSection::CuEntry cu, uint32_t offset) { 2499*0b57cec5SDimitry Andric return cu.cuOffset < offset; 2500*0b57cec5SDimitry Andric }) - 2501*0b57cec5SDimitry Andric cUs.begin(); 2502*0b57cec5SDimitry Andric for (const DWARFDebugPubTable::Entry &ent : set.Entries) 2503*0b57cec5SDimitry Andric ret.push_back({{ent.Name, computeGdbHash(ent.Name)}, 2504*0b57cec5SDimitry Andric (ent.Descriptor.toBits() << 24) | i}); 2505*0b57cec5SDimitry Andric } 2506*0b57cec5SDimitry Andric } 2507*0b57cec5SDimitry Andric return ret; 2508*0b57cec5SDimitry Andric } 2509*0b57cec5SDimitry Andric 2510*0b57cec5SDimitry Andric // Create a list of symbols from a given list of symbol names and types 2511*0b57cec5SDimitry Andric // by uniquifying them by name. 2512*0b57cec5SDimitry Andric static std::vector<GdbIndexSection::GdbSymbol> 2513*0b57cec5SDimitry Andric createSymbols(ArrayRef<std::vector<GdbIndexSection::NameAttrEntry>> nameAttrs, 2514*0b57cec5SDimitry Andric const std::vector<GdbIndexSection::GdbChunk> &chunks) { 2515*0b57cec5SDimitry Andric using GdbSymbol = GdbIndexSection::GdbSymbol; 2516*0b57cec5SDimitry Andric using NameAttrEntry = GdbIndexSection::NameAttrEntry; 2517*0b57cec5SDimitry Andric 2518*0b57cec5SDimitry Andric // For each chunk, compute the number of compilation units preceding it. 2519*0b57cec5SDimitry Andric uint32_t cuIdx = 0; 2520*0b57cec5SDimitry Andric std::vector<uint32_t> cuIdxs(chunks.size()); 2521*0b57cec5SDimitry Andric for (uint32_t i = 0, e = chunks.size(); i != e; ++i) { 2522*0b57cec5SDimitry Andric cuIdxs[i] = cuIdx; 2523*0b57cec5SDimitry Andric cuIdx += chunks[i].compilationUnits.size(); 2524*0b57cec5SDimitry Andric } 2525*0b57cec5SDimitry Andric 2526*0b57cec5SDimitry Andric // The number of symbols we will handle in this function is of the order 2527*0b57cec5SDimitry Andric // of millions for very large executables, so we use multi-threading to 2528*0b57cec5SDimitry Andric // speed it up. 2529*0b57cec5SDimitry Andric size_t numShards = 32; 2530*0b57cec5SDimitry Andric size_t concurrency = 1; 2531*0b57cec5SDimitry Andric if (threadsEnabled) 2532*0b57cec5SDimitry Andric concurrency = 2533*0b57cec5SDimitry Andric std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards); 2534*0b57cec5SDimitry Andric 2535*0b57cec5SDimitry Andric // A sharded map to uniquify symbols by name. 2536*0b57cec5SDimitry Andric std::vector<DenseMap<CachedHashStringRef, size_t>> map(numShards); 2537*0b57cec5SDimitry Andric size_t shift = 32 - countTrailingZeros(numShards); 2538*0b57cec5SDimitry Andric 2539*0b57cec5SDimitry Andric // Instantiate GdbSymbols while uniqufying them by name. 2540*0b57cec5SDimitry Andric std::vector<std::vector<GdbSymbol>> symbols(numShards); 2541*0b57cec5SDimitry Andric parallelForEachN(0, concurrency, [&](size_t threadId) { 2542*0b57cec5SDimitry Andric uint32_t i = 0; 2543*0b57cec5SDimitry Andric for (ArrayRef<NameAttrEntry> entries : nameAttrs) { 2544*0b57cec5SDimitry Andric for (const NameAttrEntry &ent : entries) { 2545*0b57cec5SDimitry Andric size_t shardId = ent.name.hash() >> shift; 2546*0b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) != threadId) 2547*0b57cec5SDimitry Andric continue; 2548*0b57cec5SDimitry Andric 2549*0b57cec5SDimitry Andric uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i]; 2550*0b57cec5SDimitry Andric size_t &idx = map[shardId][ent.name]; 2551*0b57cec5SDimitry Andric if (idx) { 2552*0b57cec5SDimitry Andric symbols[shardId][idx - 1].cuVector.push_back(v); 2553*0b57cec5SDimitry Andric continue; 2554*0b57cec5SDimitry Andric } 2555*0b57cec5SDimitry Andric 2556*0b57cec5SDimitry Andric idx = symbols[shardId].size() + 1; 2557*0b57cec5SDimitry Andric symbols[shardId].push_back({ent.name, {v}, 0, 0}); 2558*0b57cec5SDimitry Andric } 2559*0b57cec5SDimitry Andric ++i; 2560*0b57cec5SDimitry Andric } 2561*0b57cec5SDimitry Andric }); 2562*0b57cec5SDimitry Andric 2563*0b57cec5SDimitry Andric size_t numSymbols = 0; 2564*0b57cec5SDimitry Andric for (ArrayRef<GdbSymbol> v : symbols) 2565*0b57cec5SDimitry Andric numSymbols += v.size(); 2566*0b57cec5SDimitry Andric 2567*0b57cec5SDimitry Andric // The return type is a flattened vector, so we'll copy each vector 2568*0b57cec5SDimitry Andric // contents to Ret. 2569*0b57cec5SDimitry Andric std::vector<GdbSymbol> ret; 2570*0b57cec5SDimitry Andric ret.reserve(numSymbols); 2571*0b57cec5SDimitry Andric for (std::vector<GdbSymbol> &vec : symbols) 2572*0b57cec5SDimitry Andric for (GdbSymbol &sym : vec) 2573*0b57cec5SDimitry Andric ret.push_back(std::move(sym)); 2574*0b57cec5SDimitry Andric 2575*0b57cec5SDimitry Andric // CU vectors and symbol names are adjacent in the output file. 2576*0b57cec5SDimitry Andric // We can compute their offsets in the output file now. 2577*0b57cec5SDimitry Andric size_t off = 0; 2578*0b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 2579*0b57cec5SDimitry Andric sym.cuVectorOff = off; 2580*0b57cec5SDimitry Andric off += (sym.cuVector.size() + 1) * 4; 2581*0b57cec5SDimitry Andric } 2582*0b57cec5SDimitry Andric for (GdbSymbol &sym : ret) { 2583*0b57cec5SDimitry Andric sym.nameOff = off; 2584*0b57cec5SDimitry Andric off += sym.name.size() + 1; 2585*0b57cec5SDimitry Andric } 2586*0b57cec5SDimitry Andric 2587*0b57cec5SDimitry Andric return ret; 2588*0b57cec5SDimitry Andric } 2589*0b57cec5SDimitry Andric 2590*0b57cec5SDimitry Andric // Returns a newly-created .gdb_index section. 2591*0b57cec5SDimitry Andric template <class ELFT> GdbIndexSection *GdbIndexSection::create() { 2592*0b57cec5SDimitry Andric std::vector<InputSection *> sections = getDebugInfoSections(); 2593*0b57cec5SDimitry Andric 2594*0b57cec5SDimitry Andric // .debug_gnu_pub{names,types} are useless in executables. 2595*0b57cec5SDimitry Andric // They are present in input object files solely for creating 2596*0b57cec5SDimitry Andric // a .gdb_index. So we can remove them from the output. 2597*0b57cec5SDimitry Andric for (InputSectionBase *s : inputSections) 2598*0b57cec5SDimitry Andric if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes") 2599*0b57cec5SDimitry Andric s->markDead(); 2600*0b57cec5SDimitry Andric 2601*0b57cec5SDimitry Andric std::vector<GdbChunk> chunks(sections.size()); 2602*0b57cec5SDimitry Andric std::vector<std::vector<NameAttrEntry>> nameAttrs(sections.size()); 2603*0b57cec5SDimitry Andric 2604*0b57cec5SDimitry Andric parallelForEachN(0, sections.size(), [&](size_t i) { 2605*0b57cec5SDimitry Andric ObjFile<ELFT> *file = sections[i]->getFile<ELFT>(); 2606*0b57cec5SDimitry Andric DWARFContext dwarf(make_unique<LLDDwarfObj<ELFT>>(file)); 2607*0b57cec5SDimitry Andric 2608*0b57cec5SDimitry Andric chunks[i].sec = sections[i]; 2609*0b57cec5SDimitry Andric chunks[i].compilationUnits = readCuList(dwarf); 2610*0b57cec5SDimitry Andric chunks[i].addressAreas = readAddressAreas(dwarf, sections[i]); 2611*0b57cec5SDimitry Andric nameAttrs[i] = readPubNamesAndTypes<ELFT>( 2612*0b57cec5SDimitry Andric static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj()), 2613*0b57cec5SDimitry Andric chunks[i].compilationUnits); 2614*0b57cec5SDimitry Andric }); 2615*0b57cec5SDimitry Andric 2616*0b57cec5SDimitry Andric auto *ret = make<GdbIndexSection>(); 2617*0b57cec5SDimitry Andric ret->chunks = std::move(chunks); 2618*0b57cec5SDimitry Andric ret->symbols = createSymbols(nameAttrs, ret->chunks); 2619*0b57cec5SDimitry Andric ret->initOutputSize(); 2620*0b57cec5SDimitry Andric return ret; 2621*0b57cec5SDimitry Andric } 2622*0b57cec5SDimitry Andric 2623*0b57cec5SDimitry Andric void GdbIndexSection::writeTo(uint8_t *buf) { 2624*0b57cec5SDimitry Andric // Write the header. 2625*0b57cec5SDimitry Andric auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf); 2626*0b57cec5SDimitry Andric uint8_t *start = buf; 2627*0b57cec5SDimitry Andric hdr->version = 7; 2628*0b57cec5SDimitry Andric buf += sizeof(*hdr); 2629*0b57cec5SDimitry Andric 2630*0b57cec5SDimitry Andric // Write the CU list. 2631*0b57cec5SDimitry Andric hdr->cuListOff = buf - start; 2632*0b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 2633*0b57cec5SDimitry Andric for (CuEntry &cu : chunk.compilationUnits) { 2634*0b57cec5SDimitry Andric write64le(buf, chunk.sec->outSecOff + cu.cuOffset); 2635*0b57cec5SDimitry Andric write64le(buf + 8, cu.cuLength); 2636*0b57cec5SDimitry Andric buf += 16; 2637*0b57cec5SDimitry Andric } 2638*0b57cec5SDimitry Andric } 2639*0b57cec5SDimitry Andric 2640*0b57cec5SDimitry Andric // Write the address area. 2641*0b57cec5SDimitry Andric hdr->cuTypesOff = buf - start; 2642*0b57cec5SDimitry Andric hdr->addressAreaOff = buf - start; 2643*0b57cec5SDimitry Andric uint32_t cuOff = 0; 2644*0b57cec5SDimitry Andric for (GdbChunk &chunk : chunks) { 2645*0b57cec5SDimitry Andric for (AddressEntry &e : chunk.addressAreas) { 2646*0b57cec5SDimitry Andric uint64_t baseAddr = e.section->getVA(0); 2647*0b57cec5SDimitry Andric write64le(buf, baseAddr + e.lowAddress); 2648*0b57cec5SDimitry Andric write64le(buf + 8, baseAddr + e.highAddress); 2649*0b57cec5SDimitry Andric write32le(buf + 16, e.cuIndex + cuOff); 2650*0b57cec5SDimitry Andric buf += 20; 2651*0b57cec5SDimitry Andric } 2652*0b57cec5SDimitry Andric cuOff += chunk.compilationUnits.size(); 2653*0b57cec5SDimitry Andric } 2654*0b57cec5SDimitry Andric 2655*0b57cec5SDimitry Andric // Write the on-disk open-addressing hash table containing symbols. 2656*0b57cec5SDimitry Andric hdr->symtabOff = buf - start; 2657*0b57cec5SDimitry Andric size_t symtabSize = computeSymtabSize(); 2658*0b57cec5SDimitry Andric uint32_t mask = symtabSize - 1; 2659*0b57cec5SDimitry Andric 2660*0b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 2661*0b57cec5SDimitry Andric uint32_t h = sym.name.hash(); 2662*0b57cec5SDimitry Andric uint32_t i = h & mask; 2663*0b57cec5SDimitry Andric uint32_t step = ((h * 17) & mask) | 1; 2664*0b57cec5SDimitry Andric 2665*0b57cec5SDimitry Andric while (read32le(buf + i * 8)) 2666*0b57cec5SDimitry Andric i = (i + step) & mask; 2667*0b57cec5SDimitry Andric 2668*0b57cec5SDimitry Andric write32le(buf + i * 8, sym.nameOff); 2669*0b57cec5SDimitry Andric write32le(buf + i * 8 + 4, sym.cuVectorOff); 2670*0b57cec5SDimitry Andric } 2671*0b57cec5SDimitry Andric 2672*0b57cec5SDimitry Andric buf += symtabSize * 8; 2673*0b57cec5SDimitry Andric 2674*0b57cec5SDimitry Andric // Write the string pool. 2675*0b57cec5SDimitry Andric hdr->constantPoolOff = buf - start; 2676*0b57cec5SDimitry Andric parallelForEach(symbols, [&](GdbSymbol &sym) { 2677*0b57cec5SDimitry Andric memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size()); 2678*0b57cec5SDimitry Andric }); 2679*0b57cec5SDimitry Andric 2680*0b57cec5SDimitry Andric // Write the CU vectors. 2681*0b57cec5SDimitry Andric for (GdbSymbol &sym : symbols) { 2682*0b57cec5SDimitry Andric write32le(buf, sym.cuVector.size()); 2683*0b57cec5SDimitry Andric buf += 4; 2684*0b57cec5SDimitry Andric for (uint32_t val : sym.cuVector) { 2685*0b57cec5SDimitry Andric write32le(buf, val); 2686*0b57cec5SDimitry Andric buf += 4; 2687*0b57cec5SDimitry Andric } 2688*0b57cec5SDimitry Andric } 2689*0b57cec5SDimitry Andric } 2690*0b57cec5SDimitry Andric 2691*0b57cec5SDimitry Andric bool GdbIndexSection::isNeeded() const { return !chunks.empty(); } 2692*0b57cec5SDimitry Andric 2693*0b57cec5SDimitry Andric EhFrameHeader::EhFrameHeader() 2694*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {} 2695*0b57cec5SDimitry Andric 2696*0b57cec5SDimitry Andric void EhFrameHeader::writeTo(uint8_t *buf) { 2697*0b57cec5SDimitry Andric // Unlike most sections, the EhFrameHeader section is written while writing 2698*0b57cec5SDimitry Andric // another section, namely EhFrameSection, which calls the write() function 2699*0b57cec5SDimitry Andric // below from its writeTo() function. This is necessary because the contents 2700*0b57cec5SDimitry Andric // of EhFrameHeader depend on the relocated contents of EhFrameSection and we 2701*0b57cec5SDimitry Andric // don't know which order the sections will be written in. 2702*0b57cec5SDimitry Andric } 2703*0b57cec5SDimitry Andric 2704*0b57cec5SDimitry Andric // .eh_frame_hdr contains a binary search table of pointers to FDEs. 2705*0b57cec5SDimitry Andric // Each entry of the search table consists of two values, 2706*0b57cec5SDimitry Andric // the starting PC from where FDEs covers, and the FDE's address. 2707*0b57cec5SDimitry Andric // It is sorted by PC. 2708*0b57cec5SDimitry Andric void EhFrameHeader::write() { 2709*0b57cec5SDimitry Andric uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff; 2710*0b57cec5SDimitry Andric using FdeData = EhFrameSection::FdeData; 2711*0b57cec5SDimitry Andric 2712*0b57cec5SDimitry Andric std::vector<FdeData> fdes = getPartition().ehFrame->getFdeData(); 2713*0b57cec5SDimitry Andric 2714*0b57cec5SDimitry Andric buf[0] = 1; 2715*0b57cec5SDimitry Andric buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; 2716*0b57cec5SDimitry Andric buf[2] = DW_EH_PE_udata4; 2717*0b57cec5SDimitry Andric buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; 2718*0b57cec5SDimitry Andric write32(buf + 4, 2719*0b57cec5SDimitry Andric getPartition().ehFrame->getParent()->addr - this->getVA() - 4); 2720*0b57cec5SDimitry Andric write32(buf + 8, fdes.size()); 2721*0b57cec5SDimitry Andric buf += 12; 2722*0b57cec5SDimitry Andric 2723*0b57cec5SDimitry Andric for (FdeData &fde : fdes) { 2724*0b57cec5SDimitry Andric write32(buf, fde.pcRel); 2725*0b57cec5SDimitry Andric write32(buf + 4, fde.fdeVARel); 2726*0b57cec5SDimitry Andric buf += 8; 2727*0b57cec5SDimitry Andric } 2728*0b57cec5SDimitry Andric } 2729*0b57cec5SDimitry Andric 2730*0b57cec5SDimitry Andric size_t EhFrameHeader::getSize() const { 2731*0b57cec5SDimitry Andric // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. 2732*0b57cec5SDimitry Andric return 12 + getPartition().ehFrame->numFdes * 8; 2733*0b57cec5SDimitry Andric } 2734*0b57cec5SDimitry Andric 2735*0b57cec5SDimitry Andric bool EhFrameHeader::isNeeded() const { 2736*0b57cec5SDimitry Andric return isLive() && getPartition().ehFrame->isNeeded(); 2737*0b57cec5SDimitry Andric } 2738*0b57cec5SDimitry Andric 2739*0b57cec5SDimitry Andric VersionDefinitionSection::VersionDefinitionSection() 2740*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), 2741*0b57cec5SDimitry Andric ".gnu.version_d") {} 2742*0b57cec5SDimitry Andric 2743*0b57cec5SDimitry Andric StringRef VersionDefinitionSection::getFileDefName() { 2744*0b57cec5SDimitry Andric if (!getPartition().name.empty()) 2745*0b57cec5SDimitry Andric return getPartition().name; 2746*0b57cec5SDimitry Andric if (!config->soName.empty()) 2747*0b57cec5SDimitry Andric return config->soName; 2748*0b57cec5SDimitry Andric return config->outputFile; 2749*0b57cec5SDimitry Andric } 2750*0b57cec5SDimitry Andric 2751*0b57cec5SDimitry Andric void VersionDefinitionSection::finalizeContents() { 2752*0b57cec5SDimitry Andric fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName()); 2753*0b57cec5SDimitry Andric for (VersionDefinition &v : config->versionDefinitions) 2754*0b57cec5SDimitry Andric verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name)); 2755*0b57cec5SDimitry Andric 2756*0b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 2757*0b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 2758*0b57cec5SDimitry Andric 2759*0b57cec5SDimitry Andric // sh_info should be set to the number of definitions. This fact is missed in 2760*0b57cec5SDimitry Andric // documentation, but confirmed by binutils community: 2761*0b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2014-11/msg00355.html 2762*0b57cec5SDimitry Andric getParent()->info = getVerDefNum(); 2763*0b57cec5SDimitry Andric } 2764*0b57cec5SDimitry Andric 2765*0b57cec5SDimitry Andric void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index, 2766*0b57cec5SDimitry Andric StringRef name, size_t nameOff) { 2767*0b57cec5SDimitry Andric uint16_t flags = index == 1 ? VER_FLG_BASE : 0; 2768*0b57cec5SDimitry Andric 2769*0b57cec5SDimitry Andric // Write a verdef. 2770*0b57cec5SDimitry Andric write16(buf, 1); // vd_version 2771*0b57cec5SDimitry Andric write16(buf + 2, flags); // vd_flags 2772*0b57cec5SDimitry Andric write16(buf + 4, index); // vd_ndx 2773*0b57cec5SDimitry Andric write16(buf + 6, 1); // vd_cnt 2774*0b57cec5SDimitry Andric write32(buf + 8, hashSysV(name)); // vd_hash 2775*0b57cec5SDimitry Andric write32(buf + 12, 20); // vd_aux 2776*0b57cec5SDimitry Andric write32(buf + 16, 28); // vd_next 2777*0b57cec5SDimitry Andric 2778*0b57cec5SDimitry Andric // Write a veraux. 2779*0b57cec5SDimitry Andric write32(buf + 20, nameOff); // vda_name 2780*0b57cec5SDimitry Andric write32(buf + 24, 0); // vda_next 2781*0b57cec5SDimitry Andric } 2782*0b57cec5SDimitry Andric 2783*0b57cec5SDimitry Andric void VersionDefinitionSection::writeTo(uint8_t *buf) { 2784*0b57cec5SDimitry Andric writeOne(buf, 1, getFileDefName(), fileDefNameOff); 2785*0b57cec5SDimitry Andric 2786*0b57cec5SDimitry Andric auto nameOffIt = verDefNameOffs.begin(); 2787*0b57cec5SDimitry Andric for (VersionDefinition &v : config->versionDefinitions) { 2788*0b57cec5SDimitry Andric buf += EntrySize; 2789*0b57cec5SDimitry Andric writeOne(buf, v.id, v.name, *nameOffIt++); 2790*0b57cec5SDimitry Andric } 2791*0b57cec5SDimitry Andric 2792*0b57cec5SDimitry Andric // Need to terminate the last version definition. 2793*0b57cec5SDimitry Andric write32(buf + 16, 0); // vd_next 2794*0b57cec5SDimitry Andric } 2795*0b57cec5SDimitry Andric 2796*0b57cec5SDimitry Andric size_t VersionDefinitionSection::getSize() const { 2797*0b57cec5SDimitry Andric return EntrySize * getVerDefNum(); 2798*0b57cec5SDimitry Andric } 2799*0b57cec5SDimitry Andric 2800*0b57cec5SDimitry Andric // .gnu.version is a table where each entry is 2 byte long. 2801*0b57cec5SDimitry Andric VersionTableSection::VersionTableSection() 2802*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), 2803*0b57cec5SDimitry Andric ".gnu.version") { 2804*0b57cec5SDimitry Andric this->entsize = 2; 2805*0b57cec5SDimitry Andric } 2806*0b57cec5SDimitry Andric 2807*0b57cec5SDimitry Andric void VersionTableSection::finalizeContents() { 2808*0b57cec5SDimitry Andric // At the moment of june 2016 GNU docs does not mention that sh_link field 2809*0b57cec5SDimitry Andric // should be set, but Sun docs do. Also readelf relies on this field. 2810*0b57cec5SDimitry Andric getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex; 2811*0b57cec5SDimitry Andric } 2812*0b57cec5SDimitry Andric 2813*0b57cec5SDimitry Andric size_t VersionTableSection::getSize() const { 2814*0b57cec5SDimitry Andric return (getPartition().dynSymTab->getSymbols().size() + 1) * 2; 2815*0b57cec5SDimitry Andric } 2816*0b57cec5SDimitry Andric 2817*0b57cec5SDimitry Andric void VersionTableSection::writeTo(uint8_t *buf) { 2818*0b57cec5SDimitry Andric buf += 2; 2819*0b57cec5SDimitry Andric for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) { 2820*0b57cec5SDimitry Andric write16(buf, s.sym->versionId); 2821*0b57cec5SDimitry Andric buf += 2; 2822*0b57cec5SDimitry Andric } 2823*0b57cec5SDimitry Andric } 2824*0b57cec5SDimitry Andric 2825*0b57cec5SDimitry Andric bool VersionTableSection::isNeeded() const { 2826*0b57cec5SDimitry Andric return getPartition().verDef || getPartition().verNeed->isNeeded(); 2827*0b57cec5SDimitry Andric } 2828*0b57cec5SDimitry Andric 2829*0b57cec5SDimitry Andric void elf::addVerneed(Symbol *ss) { 2830*0b57cec5SDimitry Andric auto &file = cast<SharedFile>(*ss->file); 2831*0b57cec5SDimitry Andric if (ss->verdefIndex == VER_NDX_GLOBAL) { 2832*0b57cec5SDimitry Andric ss->versionId = VER_NDX_GLOBAL; 2833*0b57cec5SDimitry Andric return; 2834*0b57cec5SDimitry Andric } 2835*0b57cec5SDimitry Andric 2836*0b57cec5SDimitry Andric if (file.vernauxs.empty()) 2837*0b57cec5SDimitry Andric file.vernauxs.resize(file.verdefs.size()); 2838*0b57cec5SDimitry Andric 2839*0b57cec5SDimitry Andric // Select a version identifier for the vernaux data structure, if we haven't 2840*0b57cec5SDimitry Andric // already allocated one. The verdef identifiers cover the range 2841*0b57cec5SDimitry Andric // [1..getVerDefNum()]; this causes the vernaux identifiers to start from 2842*0b57cec5SDimitry Andric // getVerDefNum()+1. 2843*0b57cec5SDimitry Andric if (file.vernauxs[ss->verdefIndex] == 0) 2844*0b57cec5SDimitry Andric file.vernauxs[ss->verdefIndex] = ++SharedFile::vernauxNum + getVerDefNum(); 2845*0b57cec5SDimitry Andric 2846*0b57cec5SDimitry Andric ss->versionId = file.vernauxs[ss->verdefIndex]; 2847*0b57cec5SDimitry Andric } 2848*0b57cec5SDimitry Andric 2849*0b57cec5SDimitry Andric template <class ELFT> 2850*0b57cec5SDimitry Andric VersionNeedSection<ELFT>::VersionNeedSection() 2851*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), 2852*0b57cec5SDimitry Andric ".gnu.version_r") {} 2853*0b57cec5SDimitry Andric 2854*0b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { 2855*0b57cec5SDimitry Andric for (SharedFile *f : sharedFiles) { 2856*0b57cec5SDimitry Andric if (f->vernauxs.empty()) 2857*0b57cec5SDimitry Andric continue; 2858*0b57cec5SDimitry Andric verneeds.emplace_back(); 2859*0b57cec5SDimitry Andric Verneed &vn = verneeds.back(); 2860*0b57cec5SDimitry Andric vn.nameStrTab = getPartition().dynStrTab->addString(f->soName); 2861*0b57cec5SDimitry Andric for (unsigned i = 0; i != f->vernauxs.size(); ++i) { 2862*0b57cec5SDimitry Andric if (f->vernauxs[i] == 0) 2863*0b57cec5SDimitry Andric continue; 2864*0b57cec5SDimitry Andric auto *verdef = 2865*0b57cec5SDimitry Andric reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]); 2866*0b57cec5SDimitry Andric vn.vernauxs.push_back( 2867*0b57cec5SDimitry Andric {verdef->vd_hash, f->vernauxs[i], 2868*0b57cec5SDimitry Andric getPartition().dynStrTab->addString(f->getStringTable().data() + 2869*0b57cec5SDimitry Andric verdef->getAux()->vda_name)}); 2870*0b57cec5SDimitry Andric } 2871*0b57cec5SDimitry Andric } 2872*0b57cec5SDimitry Andric 2873*0b57cec5SDimitry Andric if (OutputSection *sec = getPartition().dynStrTab->getParent()) 2874*0b57cec5SDimitry Andric getParent()->link = sec->sectionIndex; 2875*0b57cec5SDimitry Andric getParent()->info = verneeds.size(); 2876*0b57cec5SDimitry Andric } 2877*0b57cec5SDimitry Andric 2878*0b57cec5SDimitry Andric template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) { 2879*0b57cec5SDimitry Andric // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. 2880*0b57cec5SDimitry Andric auto *verneed = reinterpret_cast<Elf_Verneed *>(buf); 2881*0b57cec5SDimitry Andric auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size()); 2882*0b57cec5SDimitry Andric 2883*0b57cec5SDimitry Andric for (auto &vn : verneeds) { 2884*0b57cec5SDimitry Andric // Create an Elf_Verneed for this DSO. 2885*0b57cec5SDimitry Andric verneed->vn_version = 1; 2886*0b57cec5SDimitry Andric verneed->vn_cnt = vn.vernauxs.size(); 2887*0b57cec5SDimitry Andric verneed->vn_file = vn.nameStrTab; 2888*0b57cec5SDimitry Andric verneed->vn_aux = 2889*0b57cec5SDimitry Andric reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed); 2890*0b57cec5SDimitry Andric verneed->vn_next = sizeof(Elf_Verneed); 2891*0b57cec5SDimitry Andric ++verneed; 2892*0b57cec5SDimitry Andric 2893*0b57cec5SDimitry Andric // Create the Elf_Vernauxs for this Elf_Verneed. 2894*0b57cec5SDimitry Andric for (auto &vna : vn.vernauxs) { 2895*0b57cec5SDimitry Andric vernaux->vna_hash = vna.hash; 2896*0b57cec5SDimitry Andric vernaux->vna_flags = 0; 2897*0b57cec5SDimitry Andric vernaux->vna_other = vna.verneedIndex; 2898*0b57cec5SDimitry Andric vernaux->vna_name = vna.nameStrTab; 2899*0b57cec5SDimitry Andric vernaux->vna_next = sizeof(Elf_Vernaux); 2900*0b57cec5SDimitry Andric ++vernaux; 2901*0b57cec5SDimitry Andric } 2902*0b57cec5SDimitry Andric 2903*0b57cec5SDimitry Andric vernaux[-1].vna_next = 0; 2904*0b57cec5SDimitry Andric } 2905*0b57cec5SDimitry Andric verneed[-1].vn_next = 0; 2906*0b57cec5SDimitry Andric } 2907*0b57cec5SDimitry Andric 2908*0b57cec5SDimitry Andric template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { 2909*0b57cec5SDimitry Andric return verneeds.size() * sizeof(Elf_Verneed) + 2910*0b57cec5SDimitry Andric SharedFile::vernauxNum * sizeof(Elf_Vernaux); 2911*0b57cec5SDimitry Andric } 2912*0b57cec5SDimitry Andric 2913*0b57cec5SDimitry Andric template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const { 2914*0b57cec5SDimitry Andric return SharedFile::vernauxNum != 0; 2915*0b57cec5SDimitry Andric } 2916*0b57cec5SDimitry Andric 2917*0b57cec5SDimitry Andric void MergeSyntheticSection::addSection(MergeInputSection *ms) { 2918*0b57cec5SDimitry Andric ms->parent = this; 2919*0b57cec5SDimitry Andric sections.push_back(ms); 2920*0b57cec5SDimitry Andric assert(alignment == ms->alignment || !(ms->flags & SHF_STRINGS)); 2921*0b57cec5SDimitry Andric alignment = std::max(alignment, ms->alignment); 2922*0b57cec5SDimitry Andric } 2923*0b57cec5SDimitry Andric 2924*0b57cec5SDimitry Andric MergeTailSection::MergeTailSection(StringRef name, uint32_t type, 2925*0b57cec5SDimitry Andric uint64_t flags, uint32_t alignment) 2926*0b57cec5SDimitry Andric : MergeSyntheticSection(name, type, flags, alignment), 2927*0b57cec5SDimitry Andric builder(StringTableBuilder::RAW, alignment) {} 2928*0b57cec5SDimitry Andric 2929*0b57cec5SDimitry Andric size_t MergeTailSection::getSize() const { return builder.getSize(); } 2930*0b57cec5SDimitry Andric 2931*0b57cec5SDimitry Andric void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); } 2932*0b57cec5SDimitry Andric 2933*0b57cec5SDimitry Andric void MergeTailSection::finalizeContents() { 2934*0b57cec5SDimitry Andric // Add all string pieces to the string table builder to create section 2935*0b57cec5SDimitry Andric // contents. 2936*0b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 2937*0b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 2938*0b57cec5SDimitry Andric if (sec->pieces[i].live) 2939*0b57cec5SDimitry Andric builder.add(sec->getData(i)); 2940*0b57cec5SDimitry Andric 2941*0b57cec5SDimitry Andric // Fix the string table content. After this, the contents will never change. 2942*0b57cec5SDimitry Andric builder.finalize(); 2943*0b57cec5SDimitry Andric 2944*0b57cec5SDimitry Andric // finalize() fixed tail-optimized strings, so we can now get 2945*0b57cec5SDimitry Andric // offsets of strings. Get an offset for each string and save it 2946*0b57cec5SDimitry Andric // to a corresponding SectionPiece for easy access. 2947*0b57cec5SDimitry Andric for (MergeInputSection *sec : sections) 2948*0b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 2949*0b57cec5SDimitry Andric if (sec->pieces[i].live) 2950*0b57cec5SDimitry Andric sec->pieces[i].outputOff = builder.getOffset(sec->getData(i)); 2951*0b57cec5SDimitry Andric } 2952*0b57cec5SDimitry Andric 2953*0b57cec5SDimitry Andric void MergeNoTailSection::writeTo(uint8_t *buf) { 2954*0b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) 2955*0b57cec5SDimitry Andric shards[i].write(buf + shardOffsets[i]); 2956*0b57cec5SDimitry Andric } 2957*0b57cec5SDimitry Andric 2958*0b57cec5SDimitry Andric // This function is very hot (i.e. it can take several seconds to finish) 2959*0b57cec5SDimitry Andric // because sometimes the number of inputs is in an order of magnitude of 2960*0b57cec5SDimitry Andric // millions. So, we use multi-threading. 2961*0b57cec5SDimitry Andric // 2962*0b57cec5SDimitry Andric // For any strings S and T, we know S is not mergeable with T if S's hash 2963*0b57cec5SDimitry Andric // value is different from T's. If that's the case, we can safely put S and 2964*0b57cec5SDimitry Andric // T into different string builders without worrying about merge misses. 2965*0b57cec5SDimitry Andric // We do it in parallel. 2966*0b57cec5SDimitry Andric void MergeNoTailSection::finalizeContents() { 2967*0b57cec5SDimitry Andric // Initializes string table builders. 2968*0b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) 2969*0b57cec5SDimitry Andric shards.emplace_back(StringTableBuilder::RAW, alignment); 2970*0b57cec5SDimitry Andric 2971*0b57cec5SDimitry Andric // Concurrency level. Must be a power of 2 to avoid expensive modulo 2972*0b57cec5SDimitry Andric // operations in the following tight loop. 2973*0b57cec5SDimitry Andric size_t concurrency = 1; 2974*0b57cec5SDimitry Andric if (threadsEnabled) 2975*0b57cec5SDimitry Andric concurrency = 2976*0b57cec5SDimitry Andric std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards); 2977*0b57cec5SDimitry Andric 2978*0b57cec5SDimitry Andric // Add section pieces to the builders. 2979*0b57cec5SDimitry Andric parallelForEachN(0, concurrency, [&](size_t threadId) { 2980*0b57cec5SDimitry Andric for (MergeInputSection *sec : sections) { 2981*0b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) { 2982*0b57cec5SDimitry Andric if (!sec->pieces[i].live) 2983*0b57cec5SDimitry Andric continue; 2984*0b57cec5SDimitry Andric size_t shardId = getShardId(sec->pieces[i].hash); 2985*0b57cec5SDimitry Andric if ((shardId & (concurrency - 1)) == threadId) 2986*0b57cec5SDimitry Andric sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i)); 2987*0b57cec5SDimitry Andric } 2988*0b57cec5SDimitry Andric } 2989*0b57cec5SDimitry Andric }); 2990*0b57cec5SDimitry Andric 2991*0b57cec5SDimitry Andric // Compute an in-section offset for each shard. 2992*0b57cec5SDimitry Andric size_t off = 0; 2993*0b57cec5SDimitry Andric for (size_t i = 0; i < numShards; ++i) { 2994*0b57cec5SDimitry Andric shards[i].finalizeInOrder(); 2995*0b57cec5SDimitry Andric if (shards[i].getSize() > 0) 2996*0b57cec5SDimitry Andric off = alignTo(off, alignment); 2997*0b57cec5SDimitry Andric shardOffsets[i] = off; 2998*0b57cec5SDimitry Andric off += shards[i].getSize(); 2999*0b57cec5SDimitry Andric } 3000*0b57cec5SDimitry Andric size = off; 3001*0b57cec5SDimitry Andric 3002*0b57cec5SDimitry Andric // So far, section pieces have offsets from beginning of shards, but 3003*0b57cec5SDimitry Andric // we want offsets from beginning of the whole section. Fix them. 3004*0b57cec5SDimitry Andric parallelForEach(sections, [&](MergeInputSection *sec) { 3005*0b57cec5SDimitry Andric for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) 3006*0b57cec5SDimitry Andric if (sec->pieces[i].live) 3007*0b57cec5SDimitry Andric sec->pieces[i].outputOff += 3008*0b57cec5SDimitry Andric shardOffsets[getShardId(sec->pieces[i].hash)]; 3009*0b57cec5SDimitry Andric }); 3010*0b57cec5SDimitry Andric } 3011*0b57cec5SDimitry Andric 3012*0b57cec5SDimitry Andric static MergeSyntheticSection *createMergeSynthetic(StringRef name, 3013*0b57cec5SDimitry Andric uint32_t type, 3014*0b57cec5SDimitry Andric uint64_t flags, 3015*0b57cec5SDimitry Andric uint32_t alignment) { 3016*0b57cec5SDimitry Andric bool shouldTailMerge = (flags & SHF_STRINGS) && config->optimize >= 2; 3017*0b57cec5SDimitry Andric if (shouldTailMerge) 3018*0b57cec5SDimitry Andric return make<MergeTailSection>(name, type, flags, alignment); 3019*0b57cec5SDimitry Andric return make<MergeNoTailSection>(name, type, flags, alignment); 3020*0b57cec5SDimitry Andric } 3021*0b57cec5SDimitry Andric 3022*0b57cec5SDimitry Andric template <class ELFT> void elf::splitSections() { 3023*0b57cec5SDimitry Andric // splitIntoPieces needs to be called on each MergeInputSection 3024*0b57cec5SDimitry Andric // before calling finalizeContents(). 3025*0b57cec5SDimitry Andric parallelForEach(inputSections, [](InputSectionBase *sec) { 3026*0b57cec5SDimitry Andric if (auto *s = dyn_cast<MergeInputSection>(sec)) 3027*0b57cec5SDimitry Andric s->splitIntoPieces(); 3028*0b57cec5SDimitry Andric else if (auto *eh = dyn_cast<EhInputSection>(sec)) 3029*0b57cec5SDimitry Andric eh->split<ELFT>(); 3030*0b57cec5SDimitry Andric }); 3031*0b57cec5SDimitry Andric } 3032*0b57cec5SDimitry Andric 3033*0b57cec5SDimitry Andric // This function scans over the inputsections to create mergeable 3034*0b57cec5SDimitry Andric // synthetic sections. 3035*0b57cec5SDimitry Andric // 3036*0b57cec5SDimitry Andric // It removes MergeInputSections from the input section array and adds 3037*0b57cec5SDimitry Andric // new synthetic sections at the location of the first input section 3038*0b57cec5SDimitry Andric // that it replaces. It then finalizes each synthetic section in order 3039*0b57cec5SDimitry Andric // to compute an output offset for each piece of each input section. 3040*0b57cec5SDimitry Andric void elf::mergeSections() { 3041*0b57cec5SDimitry Andric std::vector<MergeSyntheticSection *> mergeSections; 3042*0b57cec5SDimitry Andric for (InputSectionBase *&s : inputSections) { 3043*0b57cec5SDimitry Andric MergeInputSection *ms = dyn_cast<MergeInputSection>(s); 3044*0b57cec5SDimitry Andric if (!ms) 3045*0b57cec5SDimitry Andric continue; 3046*0b57cec5SDimitry Andric 3047*0b57cec5SDimitry Andric // We do not want to handle sections that are not alive, so just remove 3048*0b57cec5SDimitry Andric // them instead of trying to merge. 3049*0b57cec5SDimitry Andric if (!ms->isLive()) { 3050*0b57cec5SDimitry Andric s = nullptr; 3051*0b57cec5SDimitry Andric continue; 3052*0b57cec5SDimitry Andric } 3053*0b57cec5SDimitry Andric 3054*0b57cec5SDimitry Andric StringRef outsecName = getOutputSectionName(ms); 3055*0b57cec5SDimitry Andric 3056*0b57cec5SDimitry Andric auto i = llvm::find_if(mergeSections, [=](MergeSyntheticSection *sec) { 3057*0b57cec5SDimitry Andric // While we could create a single synthetic section for two different 3058*0b57cec5SDimitry Andric // values of Entsize, it is better to take Entsize into consideration. 3059*0b57cec5SDimitry Andric // 3060*0b57cec5SDimitry Andric // With a single synthetic section no two pieces with different Entsize 3061*0b57cec5SDimitry Andric // could be equal, so we may as well have two sections. 3062*0b57cec5SDimitry Andric // 3063*0b57cec5SDimitry Andric // Using Entsize in here also allows us to propagate it to the synthetic 3064*0b57cec5SDimitry Andric // section. 3065*0b57cec5SDimitry Andric // 3066*0b57cec5SDimitry Andric // SHF_STRINGS section with different alignments should not be merged. 3067*0b57cec5SDimitry Andric return sec->name == outsecName && sec->flags == ms->flags && 3068*0b57cec5SDimitry Andric sec->entsize == ms->entsize && 3069*0b57cec5SDimitry Andric (sec->alignment == ms->alignment || !(sec->flags & SHF_STRINGS)); 3070*0b57cec5SDimitry Andric }); 3071*0b57cec5SDimitry Andric if (i == mergeSections.end()) { 3072*0b57cec5SDimitry Andric MergeSyntheticSection *syn = 3073*0b57cec5SDimitry Andric createMergeSynthetic(outsecName, ms->type, ms->flags, ms->alignment); 3074*0b57cec5SDimitry Andric mergeSections.push_back(syn); 3075*0b57cec5SDimitry Andric i = std::prev(mergeSections.end()); 3076*0b57cec5SDimitry Andric s = syn; 3077*0b57cec5SDimitry Andric syn->entsize = ms->entsize; 3078*0b57cec5SDimitry Andric } else { 3079*0b57cec5SDimitry Andric s = nullptr; 3080*0b57cec5SDimitry Andric } 3081*0b57cec5SDimitry Andric (*i)->addSection(ms); 3082*0b57cec5SDimitry Andric } 3083*0b57cec5SDimitry Andric for (auto *ms : mergeSections) 3084*0b57cec5SDimitry Andric ms->finalizeContents(); 3085*0b57cec5SDimitry Andric 3086*0b57cec5SDimitry Andric std::vector<InputSectionBase *> &v = inputSections; 3087*0b57cec5SDimitry Andric v.erase(std::remove(v.begin(), v.end(), nullptr), v.end()); 3088*0b57cec5SDimitry Andric } 3089*0b57cec5SDimitry Andric 3090*0b57cec5SDimitry Andric MipsRldMapSection::MipsRldMapSection() 3091*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, 3092*0b57cec5SDimitry Andric ".rld_map") {} 3093*0b57cec5SDimitry Andric 3094*0b57cec5SDimitry Andric ARMExidxSyntheticSection::ARMExidxSyntheticSection() 3095*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, 3096*0b57cec5SDimitry Andric config->wordsize, ".ARM.exidx") {} 3097*0b57cec5SDimitry Andric 3098*0b57cec5SDimitry Andric static InputSection *findExidxSection(InputSection *isec) { 3099*0b57cec5SDimitry Andric for (InputSection *d : isec->dependentSections) 3100*0b57cec5SDimitry Andric if (d->type == SHT_ARM_EXIDX) 3101*0b57cec5SDimitry Andric return d; 3102*0b57cec5SDimitry Andric return nullptr; 3103*0b57cec5SDimitry Andric } 3104*0b57cec5SDimitry Andric 3105*0b57cec5SDimitry Andric bool ARMExidxSyntheticSection::addSection(InputSection *isec) { 3106*0b57cec5SDimitry Andric if (isec->type == SHT_ARM_EXIDX) { 3107*0b57cec5SDimitry Andric exidxSections.push_back(isec); 3108*0b57cec5SDimitry Andric return true; 3109*0b57cec5SDimitry Andric } 3110*0b57cec5SDimitry Andric 3111*0b57cec5SDimitry Andric if ((isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) && 3112*0b57cec5SDimitry Andric isec->getSize() > 0) { 3113*0b57cec5SDimitry Andric executableSections.push_back(isec); 3114*0b57cec5SDimitry Andric if (empty && findExidxSection(isec)) 3115*0b57cec5SDimitry Andric empty = false; 3116*0b57cec5SDimitry Andric return false; 3117*0b57cec5SDimitry Andric } 3118*0b57cec5SDimitry Andric 3119*0b57cec5SDimitry Andric // FIXME: we do not output a relocation section when --emit-relocs is used 3120*0b57cec5SDimitry Andric // as we do not have relocation sections for linker generated table entries 3121*0b57cec5SDimitry Andric // and we would have to erase at a late stage relocations from merged entries. 3122*0b57cec5SDimitry Andric // Given that exception tables are already position independent and a binary 3123*0b57cec5SDimitry Andric // analyzer could derive the relocations we choose to erase the relocations. 3124*0b57cec5SDimitry Andric if (config->emitRelocs && isec->type == SHT_REL) 3125*0b57cec5SDimitry Andric if (InputSectionBase *ex = isec->getRelocatedSection()) 3126*0b57cec5SDimitry Andric if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX) 3127*0b57cec5SDimitry Andric return true; 3128*0b57cec5SDimitry Andric 3129*0b57cec5SDimitry Andric return false; 3130*0b57cec5SDimitry Andric } 3131*0b57cec5SDimitry Andric 3132*0b57cec5SDimitry Andric // References to .ARM.Extab Sections have bit 31 clear and are not the 3133*0b57cec5SDimitry Andric // special EXIDX_CANTUNWIND bit-pattern. 3134*0b57cec5SDimitry Andric static bool isExtabRef(uint32_t unwind) { 3135*0b57cec5SDimitry Andric return (unwind & 0x80000000) == 0 && unwind != 0x1; 3136*0b57cec5SDimitry Andric } 3137*0b57cec5SDimitry Andric 3138*0b57cec5SDimitry Andric // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx 3139*0b57cec5SDimitry Andric // section Prev, where Cur follows Prev in the table. This can be done if the 3140*0b57cec5SDimitry Andric // unwinding instructions in Cur are identical to Prev. Linker generated 3141*0b57cec5SDimitry Andric // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an 3142*0b57cec5SDimitry Andric // InputSection. 3143*0b57cec5SDimitry Andric static bool isDuplicateArmExidxSec(InputSection *prev, InputSection *cur) { 3144*0b57cec5SDimitry Andric 3145*0b57cec5SDimitry Andric struct ExidxEntry { 3146*0b57cec5SDimitry Andric ulittle32_t fn; 3147*0b57cec5SDimitry Andric ulittle32_t unwind; 3148*0b57cec5SDimitry Andric }; 3149*0b57cec5SDimitry Andric // Get the last table Entry from the previous .ARM.exidx section. If Prev is 3150*0b57cec5SDimitry Andric // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry. 3151*0b57cec5SDimitry Andric ExidxEntry prevEntry = {ulittle32_t(0), ulittle32_t(1)}; 3152*0b57cec5SDimitry Andric if (prev) 3153*0b57cec5SDimitry Andric prevEntry = prev->getDataAs<ExidxEntry>().back(); 3154*0b57cec5SDimitry Andric if (isExtabRef(prevEntry.unwind)) 3155*0b57cec5SDimitry Andric return false; 3156*0b57cec5SDimitry Andric 3157*0b57cec5SDimitry Andric // We consider the unwind instructions of an .ARM.exidx table entry 3158*0b57cec5SDimitry Andric // a duplicate if the previous unwind instructions if: 3159*0b57cec5SDimitry Andric // - Both are the special EXIDX_CANTUNWIND. 3160*0b57cec5SDimitry Andric // - Both are the same inline unwind instructions. 3161*0b57cec5SDimitry Andric // We do not attempt to follow and check links into .ARM.extab tables as 3162*0b57cec5SDimitry Andric // consecutive identical entries are rare and the effort to check that they 3163*0b57cec5SDimitry Andric // are identical is high. 3164*0b57cec5SDimitry Andric 3165*0b57cec5SDimitry Andric // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry. 3166*0b57cec5SDimitry Andric if (cur == nullptr) 3167*0b57cec5SDimitry Andric return prevEntry.unwind == 1; 3168*0b57cec5SDimitry Andric 3169*0b57cec5SDimitry Andric for (const ExidxEntry entry : cur->getDataAs<ExidxEntry>()) 3170*0b57cec5SDimitry Andric if (isExtabRef(entry.unwind) || entry.unwind != prevEntry.unwind) 3171*0b57cec5SDimitry Andric return false; 3172*0b57cec5SDimitry Andric 3173*0b57cec5SDimitry Andric // All table entries in this .ARM.exidx Section can be merged into the 3174*0b57cec5SDimitry Andric // previous Section. 3175*0b57cec5SDimitry Andric return true; 3176*0b57cec5SDimitry Andric } 3177*0b57cec5SDimitry Andric 3178*0b57cec5SDimitry Andric // The .ARM.exidx table must be sorted in ascending order of the address of the 3179*0b57cec5SDimitry Andric // functions the table describes. Optionally duplicate adjacent table entries 3180*0b57cec5SDimitry Andric // can be removed. At the end of the function the executableSections must be 3181*0b57cec5SDimitry Andric // sorted in ascending order of address, Sentinel is set to the InputSection 3182*0b57cec5SDimitry Andric // with the highest address and any InputSections that have mergeable 3183*0b57cec5SDimitry Andric // .ARM.exidx table entries are removed from it. 3184*0b57cec5SDimitry Andric void ARMExidxSyntheticSection::finalizeContents() { 3185*0b57cec5SDimitry Andric if (script->hasSectionsCommand) { 3186*0b57cec5SDimitry Andric // The executableSections and exidxSections that we use to derive the 3187*0b57cec5SDimitry Andric // final contents of this SyntheticSection are populated before the 3188*0b57cec5SDimitry Andric // linker script assigns InputSections to OutputSections. The linker script 3189*0b57cec5SDimitry Andric // SECTIONS command may have a /DISCARD/ entry that removes executable 3190*0b57cec5SDimitry Andric // InputSections and their dependent .ARM.exidx section that we recorded 3191*0b57cec5SDimitry Andric // earlier. 3192*0b57cec5SDimitry Andric auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); }; 3193*0b57cec5SDimitry Andric llvm::erase_if(executableSections, isDiscarded); 3194*0b57cec5SDimitry Andric llvm::erase_if(exidxSections, isDiscarded); 3195*0b57cec5SDimitry Andric } 3196*0b57cec5SDimitry Andric 3197*0b57cec5SDimitry Andric // Sort the executable sections that may or may not have associated 3198*0b57cec5SDimitry Andric // .ARM.exidx sections by order of ascending address. This requires the 3199*0b57cec5SDimitry Andric // relative positions of InputSections to be known. 3200*0b57cec5SDimitry Andric auto compareByFilePosition = [](const InputSection *a, 3201*0b57cec5SDimitry Andric const InputSection *b) { 3202*0b57cec5SDimitry Andric OutputSection *aOut = a->getParent(); 3203*0b57cec5SDimitry Andric OutputSection *bOut = b->getParent(); 3204*0b57cec5SDimitry Andric 3205*0b57cec5SDimitry Andric if (aOut != bOut) 3206*0b57cec5SDimitry Andric return aOut->sectionIndex < bOut->sectionIndex; 3207*0b57cec5SDimitry Andric return a->outSecOff < b->outSecOff; 3208*0b57cec5SDimitry Andric }; 3209*0b57cec5SDimitry Andric llvm::stable_sort(executableSections, compareByFilePosition); 3210*0b57cec5SDimitry Andric sentinel = executableSections.back(); 3211*0b57cec5SDimitry Andric // Optionally merge adjacent duplicate entries. 3212*0b57cec5SDimitry Andric if (config->mergeArmExidx) { 3213*0b57cec5SDimitry Andric std::vector<InputSection *> selectedSections; 3214*0b57cec5SDimitry Andric selectedSections.reserve(executableSections.size()); 3215*0b57cec5SDimitry Andric selectedSections.push_back(executableSections[0]); 3216*0b57cec5SDimitry Andric size_t prev = 0; 3217*0b57cec5SDimitry Andric for (size_t i = 1; i < executableSections.size(); ++i) { 3218*0b57cec5SDimitry Andric InputSection *ex1 = findExidxSection(executableSections[prev]); 3219*0b57cec5SDimitry Andric InputSection *ex2 = findExidxSection(executableSections[i]); 3220*0b57cec5SDimitry Andric if (!isDuplicateArmExidxSec(ex1, ex2)) { 3221*0b57cec5SDimitry Andric selectedSections.push_back(executableSections[i]); 3222*0b57cec5SDimitry Andric prev = i; 3223*0b57cec5SDimitry Andric } 3224*0b57cec5SDimitry Andric } 3225*0b57cec5SDimitry Andric executableSections = std::move(selectedSections); 3226*0b57cec5SDimitry Andric } 3227*0b57cec5SDimitry Andric 3228*0b57cec5SDimitry Andric size_t offset = 0; 3229*0b57cec5SDimitry Andric size = 0; 3230*0b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 3231*0b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 3232*0b57cec5SDimitry Andric d->outSecOff = offset; 3233*0b57cec5SDimitry Andric d->parent = getParent(); 3234*0b57cec5SDimitry Andric offset += d->getSize(); 3235*0b57cec5SDimitry Andric } else { 3236*0b57cec5SDimitry Andric offset += 8; 3237*0b57cec5SDimitry Andric } 3238*0b57cec5SDimitry Andric } 3239*0b57cec5SDimitry Andric // Size includes Sentinel. 3240*0b57cec5SDimitry Andric size = offset + 8; 3241*0b57cec5SDimitry Andric } 3242*0b57cec5SDimitry Andric 3243*0b57cec5SDimitry Andric InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const { 3244*0b57cec5SDimitry Andric return executableSections.front(); 3245*0b57cec5SDimitry Andric } 3246*0b57cec5SDimitry Andric 3247*0b57cec5SDimitry Andric // To write the .ARM.exidx table from the ExecutableSections we have three cases 3248*0b57cec5SDimitry Andric // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections. 3249*0b57cec5SDimitry Andric // We write the .ARM.exidx section contents and apply its relocations. 3250*0b57cec5SDimitry Andric // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We 3251*0b57cec5SDimitry Andric // must write the contents of an EXIDX_CANTUNWIND directly. We use the 3252*0b57cec5SDimitry Andric // start of the InputSection as the purpose of the linker generated 3253*0b57cec5SDimitry Andric // section is to terminate the address range of the previous entry. 3254*0b57cec5SDimitry Andric // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of 3255*0b57cec5SDimitry Andric // the table to terminate the address range of the final entry. 3256*0b57cec5SDimitry Andric void ARMExidxSyntheticSection::writeTo(uint8_t *buf) { 3257*0b57cec5SDimitry Andric 3258*0b57cec5SDimitry Andric const uint8_t cantUnwindData[8] = {0, 0, 0, 0, // PREL31 to target 3259*0b57cec5SDimitry Andric 1, 0, 0, 0}; // EXIDX_CANTUNWIND 3260*0b57cec5SDimitry Andric 3261*0b57cec5SDimitry Andric uint64_t offset = 0; 3262*0b57cec5SDimitry Andric for (InputSection *isec : executableSections) { 3263*0b57cec5SDimitry Andric assert(isec->getParent() != nullptr); 3264*0b57cec5SDimitry Andric if (InputSection *d = findExidxSection(isec)) { 3265*0b57cec5SDimitry Andric memcpy(buf + offset, d->data().data(), d->data().size()); 3266*0b57cec5SDimitry Andric d->relocateAlloc(buf, buf + d->getSize()); 3267*0b57cec5SDimitry Andric offset += d->getSize(); 3268*0b57cec5SDimitry Andric } else { 3269*0b57cec5SDimitry Andric // A Linker generated CANTUNWIND section. 3270*0b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 3271*0b57cec5SDimitry Andric uint64_t s = isec->getVA(); 3272*0b57cec5SDimitry Andric uint64_t p = getVA() + offset; 3273*0b57cec5SDimitry Andric target->relocateOne(buf + offset, R_ARM_PREL31, s - p); 3274*0b57cec5SDimitry Andric offset += 8; 3275*0b57cec5SDimitry Andric } 3276*0b57cec5SDimitry Andric } 3277*0b57cec5SDimitry Andric // Write Sentinel. 3278*0b57cec5SDimitry Andric memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData)); 3279*0b57cec5SDimitry Andric uint64_t s = sentinel->getVA(sentinel->getSize()); 3280*0b57cec5SDimitry Andric uint64_t p = getVA() + offset; 3281*0b57cec5SDimitry Andric target->relocateOne(buf + offset, R_ARM_PREL31, s - p); 3282*0b57cec5SDimitry Andric assert(size == offset + 8); 3283*0b57cec5SDimitry Andric } 3284*0b57cec5SDimitry Andric 3285*0b57cec5SDimitry Andric bool ARMExidxSyntheticSection::classof(const SectionBase *d) { 3286*0b57cec5SDimitry Andric return d->kind() == InputSectionBase::Synthetic && d->type == SHT_ARM_EXIDX; 3287*0b57cec5SDimitry Andric } 3288*0b57cec5SDimitry Andric 3289*0b57cec5SDimitry Andric ThunkSection::ThunkSection(OutputSection *os, uint64_t off) 3290*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 3291*0b57cec5SDimitry Andric config->wordsize, ".text.thunk") { 3292*0b57cec5SDimitry Andric this->parent = os; 3293*0b57cec5SDimitry Andric this->outSecOff = off; 3294*0b57cec5SDimitry Andric } 3295*0b57cec5SDimitry Andric 3296*0b57cec5SDimitry Andric void ThunkSection::addThunk(Thunk *t) { 3297*0b57cec5SDimitry Andric thunks.push_back(t); 3298*0b57cec5SDimitry Andric t->addSymbols(*this); 3299*0b57cec5SDimitry Andric } 3300*0b57cec5SDimitry Andric 3301*0b57cec5SDimitry Andric void ThunkSection::writeTo(uint8_t *buf) { 3302*0b57cec5SDimitry Andric for (Thunk *t : thunks) 3303*0b57cec5SDimitry Andric t->writeTo(buf + t->offset); 3304*0b57cec5SDimitry Andric } 3305*0b57cec5SDimitry Andric 3306*0b57cec5SDimitry Andric InputSection *ThunkSection::getTargetInputSection() const { 3307*0b57cec5SDimitry Andric if (thunks.empty()) 3308*0b57cec5SDimitry Andric return nullptr; 3309*0b57cec5SDimitry Andric const Thunk *t = thunks.front(); 3310*0b57cec5SDimitry Andric return t->getTargetInputSection(); 3311*0b57cec5SDimitry Andric } 3312*0b57cec5SDimitry Andric 3313*0b57cec5SDimitry Andric bool ThunkSection::assignOffsets() { 3314*0b57cec5SDimitry Andric uint64_t off = 0; 3315*0b57cec5SDimitry Andric for (Thunk *t : thunks) { 3316*0b57cec5SDimitry Andric off = alignTo(off, t->alignment); 3317*0b57cec5SDimitry Andric t->setOffset(off); 3318*0b57cec5SDimitry Andric uint32_t size = t->size(); 3319*0b57cec5SDimitry Andric t->getThunkTargetSym()->size = size; 3320*0b57cec5SDimitry Andric off += size; 3321*0b57cec5SDimitry Andric } 3322*0b57cec5SDimitry Andric bool changed = off != size; 3323*0b57cec5SDimitry Andric size = off; 3324*0b57cec5SDimitry Andric return changed; 3325*0b57cec5SDimitry Andric } 3326*0b57cec5SDimitry Andric 3327*0b57cec5SDimitry Andric PPC32Got2Section::PPC32Got2Section() 3328*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {} 3329*0b57cec5SDimitry Andric 3330*0b57cec5SDimitry Andric bool PPC32Got2Section::isNeeded() const { 3331*0b57cec5SDimitry Andric // See the comment below. This is not needed if there is no other 3332*0b57cec5SDimitry Andric // InputSection. 3333*0b57cec5SDimitry Andric for (BaseCommand *base : getParent()->sectionCommands) 3334*0b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(base)) 3335*0b57cec5SDimitry Andric for (InputSection *isec : isd->sections) 3336*0b57cec5SDimitry Andric if (isec != this) 3337*0b57cec5SDimitry Andric return true; 3338*0b57cec5SDimitry Andric return false; 3339*0b57cec5SDimitry Andric } 3340*0b57cec5SDimitry Andric 3341*0b57cec5SDimitry Andric void PPC32Got2Section::finalizeContents() { 3342*0b57cec5SDimitry Andric // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in 3343*0b57cec5SDimitry Andric // .got2 . This function computes outSecOff of each .got2 to be used in 3344*0b57cec5SDimitry Andric // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is 3345*0b57cec5SDimitry Andric // to collect input sections named ".got2". 3346*0b57cec5SDimitry Andric uint32_t offset = 0; 3347*0b57cec5SDimitry Andric for (BaseCommand *base : getParent()->sectionCommands) 3348*0b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(base)) { 3349*0b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 3350*0b57cec5SDimitry Andric if (isec == this) 3351*0b57cec5SDimitry Andric continue; 3352*0b57cec5SDimitry Andric isec->file->ppc32Got2OutSecOff = offset; 3353*0b57cec5SDimitry Andric offset += (uint32_t)isec->getSize(); 3354*0b57cec5SDimitry Andric } 3355*0b57cec5SDimitry Andric } 3356*0b57cec5SDimitry Andric } 3357*0b57cec5SDimitry Andric 3358*0b57cec5SDimitry Andric // If linking position-dependent code then the table will store the addresses 3359*0b57cec5SDimitry Andric // directly in the binary so the section has type SHT_PROGBITS. If linking 3360*0b57cec5SDimitry Andric // position-independent code the section has type SHT_NOBITS since it will be 3361*0b57cec5SDimitry Andric // allocated and filled in by the dynamic linker. 3362*0b57cec5SDimitry Andric PPC64LongBranchTargetSection::PPC64LongBranchTargetSection() 3363*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, 3364*0b57cec5SDimitry Andric config->isPic ? SHT_NOBITS : SHT_PROGBITS, 8, 3365*0b57cec5SDimitry Andric ".branch_lt") {} 3366*0b57cec5SDimitry Andric 3367*0b57cec5SDimitry Andric void PPC64LongBranchTargetSection::addEntry(Symbol &sym) { 3368*0b57cec5SDimitry Andric assert(sym.ppc64BranchltIndex == 0xffff); 3369*0b57cec5SDimitry Andric sym.ppc64BranchltIndex = entries.size(); 3370*0b57cec5SDimitry Andric entries.push_back(&sym); 3371*0b57cec5SDimitry Andric } 3372*0b57cec5SDimitry Andric 3373*0b57cec5SDimitry Andric size_t PPC64LongBranchTargetSection::getSize() const { 3374*0b57cec5SDimitry Andric return entries.size() * 8; 3375*0b57cec5SDimitry Andric } 3376*0b57cec5SDimitry Andric 3377*0b57cec5SDimitry Andric void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) { 3378*0b57cec5SDimitry Andric // If linking non-pic we have the final addresses of the targets and they get 3379*0b57cec5SDimitry Andric // written to the table directly. For pic the dynamic linker will allocate 3380*0b57cec5SDimitry Andric // the section and fill it it. 3381*0b57cec5SDimitry Andric if (config->isPic) 3382*0b57cec5SDimitry Andric return; 3383*0b57cec5SDimitry Andric 3384*0b57cec5SDimitry Andric for (const Symbol *sym : entries) { 3385*0b57cec5SDimitry Andric assert(sym->getVA()); 3386*0b57cec5SDimitry Andric // Need calls to branch to the local entry-point since a long-branch 3387*0b57cec5SDimitry Andric // must be a local-call. 3388*0b57cec5SDimitry Andric write64(buf, 3389*0b57cec5SDimitry Andric sym->getVA() + getPPC64GlobalEntryToLocalEntryOffset(sym->stOther)); 3390*0b57cec5SDimitry Andric buf += 8; 3391*0b57cec5SDimitry Andric } 3392*0b57cec5SDimitry Andric } 3393*0b57cec5SDimitry Andric 3394*0b57cec5SDimitry Andric bool PPC64LongBranchTargetSection::isNeeded() const { 3395*0b57cec5SDimitry Andric // `removeUnusedSyntheticSections()` is called before thunk allocation which 3396*0b57cec5SDimitry Andric // is too early to determine if this section will be empty or not. We need 3397*0b57cec5SDimitry Andric // Finalized to keep the section alive until after thunk creation. Finalized 3398*0b57cec5SDimitry Andric // only gets set to true once `finalizeSections()` is called after thunk 3399*0b57cec5SDimitry Andric // creation. Becuase of this, if we don't create any long-branch thunks we end 3400*0b57cec5SDimitry Andric // up with an empty .branch_lt section in the binary. 3401*0b57cec5SDimitry Andric return !finalized || !entries.empty(); 3402*0b57cec5SDimitry Andric } 3403*0b57cec5SDimitry Andric 3404*0b57cec5SDimitry Andric RISCVSdataSection::RISCVSdataSection() 3405*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 1, ".sdata") {} 3406*0b57cec5SDimitry Andric 3407*0b57cec5SDimitry Andric bool RISCVSdataSection::isNeeded() const { 3408*0b57cec5SDimitry Andric if (!ElfSym::riscvGlobalPointer) 3409*0b57cec5SDimitry Andric return false; 3410*0b57cec5SDimitry Andric 3411*0b57cec5SDimitry Andric // __global_pointer$ is defined relative to .sdata . If the section does not 3412*0b57cec5SDimitry Andric // exist, create a dummy one. 3413*0b57cec5SDimitry Andric for (BaseCommand *base : getParent()->sectionCommands) 3414*0b57cec5SDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(base)) 3415*0b57cec5SDimitry Andric for (InputSection *isec : isd->sections) 3416*0b57cec5SDimitry Andric if (isec != this) 3417*0b57cec5SDimitry Andric return false; 3418*0b57cec5SDimitry Andric return true; 3419*0b57cec5SDimitry Andric } 3420*0b57cec5SDimitry Andric 3421*0b57cec5SDimitry Andric static uint8_t getAbiVersion() { 3422*0b57cec5SDimitry Andric // MIPS non-PIC executable gets ABI version 1. 3423*0b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 3424*0b57cec5SDimitry Andric if (!config->isPic && !config->relocatable && 3425*0b57cec5SDimitry Andric (config->eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC) 3426*0b57cec5SDimitry Andric return 1; 3427*0b57cec5SDimitry Andric return 0; 3428*0b57cec5SDimitry Andric } 3429*0b57cec5SDimitry Andric 3430*0b57cec5SDimitry Andric if (config->emachine == EM_AMDGPU) { 3431*0b57cec5SDimitry Andric uint8_t ver = objectFiles[0]->abiVersion; 3432*0b57cec5SDimitry Andric for (InputFile *file : makeArrayRef(objectFiles).slice(1)) 3433*0b57cec5SDimitry Andric if (file->abiVersion != ver) 3434*0b57cec5SDimitry Andric error("incompatible ABI version: " + toString(file)); 3435*0b57cec5SDimitry Andric return ver; 3436*0b57cec5SDimitry Andric } 3437*0b57cec5SDimitry Andric 3438*0b57cec5SDimitry Andric return 0; 3439*0b57cec5SDimitry Andric } 3440*0b57cec5SDimitry Andric 3441*0b57cec5SDimitry Andric template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) { 3442*0b57cec5SDimitry Andric // For executable segments, the trap instructions are written before writing 3443*0b57cec5SDimitry Andric // the header. Setting Elf header bytes to zero ensures that any unused bytes 3444*0b57cec5SDimitry Andric // in header are zero-cleared, instead of having trap instructions. 3445*0b57cec5SDimitry Andric memset(buf, 0, sizeof(typename ELFT::Ehdr)); 3446*0b57cec5SDimitry Andric memcpy(buf, "\177ELF", 4); 3447*0b57cec5SDimitry Andric 3448*0b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 3449*0b57cec5SDimitry Andric eHdr->e_ident[EI_CLASS] = config->is64 ? ELFCLASS64 : ELFCLASS32; 3450*0b57cec5SDimitry Andric eHdr->e_ident[EI_DATA] = config->isLE ? ELFDATA2LSB : ELFDATA2MSB; 3451*0b57cec5SDimitry Andric eHdr->e_ident[EI_VERSION] = EV_CURRENT; 3452*0b57cec5SDimitry Andric eHdr->e_ident[EI_OSABI] = config->osabi; 3453*0b57cec5SDimitry Andric eHdr->e_ident[EI_ABIVERSION] = getAbiVersion(); 3454*0b57cec5SDimitry Andric eHdr->e_machine = config->emachine; 3455*0b57cec5SDimitry Andric eHdr->e_version = EV_CURRENT; 3456*0b57cec5SDimitry Andric eHdr->e_flags = config->eflags; 3457*0b57cec5SDimitry Andric eHdr->e_ehsize = sizeof(typename ELFT::Ehdr); 3458*0b57cec5SDimitry Andric eHdr->e_phnum = part.phdrs.size(); 3459*0b57cec5SDimitry Andric eHdr->e_shentsize = sizeof(typename ELFT::Shdr); 3460*0b57cec5SDimitry Andric 3461*0b57cec5SDimitry Andric if (!config->relocatable) { 3462*0b57cec5SDimitry Andric eHdr->e_phoff = sizeof(typename ELFT::Ehdr); 3463*0b57cec5SDimitry Andric eHdr->e_phentsize = sizeof(typename ELFT::Phdr); 3464*0b57cec5SDimitry Andric } 3465*0b57cec5SDimitry Andric } 3466*0b57cec5SDimitry Andric 3467*0b57cec5SDimitry Andric template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) { 3468*0b57cec5SDimitry Andric // Write the program header table. 3469*0b57cec5SDimitry Andric auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf); 3470*0b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) { 3471*0b57cec5SDimitry Andric hBuf->p_type = p->p_type; 3472*0b57cec5SDimitry Andric hBuf->p_flags = p->p_flags; 3473*0b57cec5SDimitry Andric hBuf->p_offset = p->p_offset; 3474*0b57cec5SDimitry Andric hBuf->p_vaddr = p->p_vaddr; 3475*0b57cec5SDimitry Andric hBuf->p_paddr = p->p_paddr; 3476*0b57cec5SDimitry Andric hBuf->p_filesz = p->p_filesz; 3477*0b57cec5SDimitry Andric hBuf->p_memsz = p->p_memsz; 3478*0b57cec5SDimitry Andric hBuf->p_align = p->p_align; 3479*0b57cec5SDimitry Andric ++hBuf; 3480*0b57cec5SDimitry Andric } 3481*0b57cec5SDimitry Andric } 3482*0b57cec5SDimitry Andric 3483*0b57cec5SDimitry Andric template <typename ELFT> 3484*0b57cec5SDimitry Andric PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection() 3485*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {} 3486*0b57cec5SDimitry Andric 3487*0b57cec5SDimitry Andric template <typename ELFT> 3488*0b57cec5SDimitry Andric size_t PartitionElfHeaderSection<ELFT>::getSize() const { 3489*0b57cec5SDimitry Andric return sizeof(typename ELFT::Ehdr); 3490*0b57cec5SDimitry Andric } 3491*0b57cec5SDimitry Andric 3492*0b57cec5SDimitry Andric template <typename ELFT> 3493*0b57cec5SDimitry Andric void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) { 3494*0b57cec5SDimitry Andric writeEhdr<ELFT>(buf, getPartition()); 3495*0b57cec5SDimitry Andric 3496*0b57cec5SDimitry Andric // Loadable partitions are always ET_DYN. 3497*0b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf); 3498*0b57cec5SDimitry Andric eHdr->e_type = ET_DYN; 3499*0b57cec5SDimitry Andric } 3500*0b57cec5SDimitry Andric 3501*0b57cec5SDimitry Andric template <typename ELFT> 3502*0b57cec5SDimitry Andric PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection() 3503*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {} 3504*0b57cec5SDimitry Andric 3505*0b57cec5SDimitry Andric template <typename ELFT> 3506*0b57cec5SDimitry Andric size_t PartitionProgramHeadersSection<ELFT>::getSize() const { 3507*0b57cec5SDimitry Andric return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size(); 3508*0b57cec5SDimitry Andric } 3509*0b57cec5SDimitry Andric 3510*0b57cec5SDimitry Andric template <typename ELFT> 3511*0b57cec5SDimitry Andric void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) { 3512*0b57cec5SDimitry Andric writePhdrs<ELFT>(buf, getPartition()); 3513*0b57cec5SDimitry Andric } 3514*0b57cec5SDimitry Andric 3515*0b57cec5SDimitry Andric PartitionIndexSection::PartitionIndexSection() 3516*0b57cec5SDimitry Andric : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {} 3517*0b57cec5SDimitry Andric 3518*0b57cec5SDimitry Andric size_t PartitionIndexSection::getSize() const { 3519*0b57cec5SDimitry Andric return 12 * (partitions.size() - 1); 3520*0b57cec5SDimitry Andric } 3521*0b57cec5SDimitry Andric 3522*0b57cec5SDimitry Andric void PartitionIndexSection::finalizeContents() { 3523*0b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) 3524*0b57cec5SDimitry Andric partitions[i].nameStrTab = mainPart->dynStrTab->addString(partitions[i].name); 3525*0b57cec5SDimitry Andric } 3526*0b57cec5SDimitry Andric 3527*0b57cec5SDimitry Andric void PartitionIndexSection::writeTo(uint8_t *buf) { 3528*0b57cec5SDimitry Andric uint64_t va = getVA(); 3529*0b57cec5SDimitry Andric for (size_t i = 1; i != partitions.size(); ++i) { 3530*0b57cec5SDimitry Andric write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va); 3531*0b57cec5SDimitry Andric write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4)); 3532*0b57cec5SDimitry Andric 3533*0b57cec5SDimitry Andric SyntheticSection *next = 3534*0b57cec5SDimitry Andric i == partitions.size() - 1 ? in.partEnd : partitions[i + 1].elfHeader; 3535*0b57cec5SDimitry Andric write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA()); 3536*0b57cec5SDimitry Andric 3537*0b57cec5SDimitry Andric va += 12; 3538*0b57cec5SDimitry Andric buf += 12; 3539*0b57cec5SDimitry Andric } 3540*0b57cec5SDimitry Andric } 3541*0b57cec5SDimitry Andric 3542*0b57cec5SDimitry Andric InStruct elf::in; 3543*0b57cec5SDimitry Andric 3544*0b57cec5SDimitry Andric std::vector<Partition> elf::partitions; 3545*0b57cec5SDimitry Andric Partition *elf::mainPart; 3546*0b57cec5SDimitry Andric 3547*0b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32LE>(); 3548*0b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF32BE>(); 3549*0b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64LE>(); 3550*0b57cec5SDimitry Andric template GdbIndexSection *GdbIndexSection::create<ELF64BE>(); 3551*0b57cec5SDimitry Andric 3552*0b57cec5SDimitry Andric template void elf::splitSections<ELF32LE>(); 3553*0b57cec5SDimitry Andric template void elf::splitSections<ELF32BE>(); 3554*0b57cec5SDimitry Andric template void elf::splitSections<ELF64LE>(); 3555*0b57cec5SDimitry Andric template void elf::splitSections<ELF64BE>(); 3556*0b57cec5SDimitry Andric 3557*0b57cec5SDimitry Andric template void EhFrameSection::addSection<ELF32LE>(InputSectionBase *); 3558*0b57cec5SDimitry Andric template void EhFrameSection::addSection<ELF32BE>(InputSectionBase *); 3559*0b57cec5SDimitry Andric template void EhFrameSection::addSection<ELF64LE>(InputSectionBase *); 3560*0b57cec5SDimitry Andric template void EhFrameSection::addSection<ELF64BE>(InputSectionBase *); 3561*0b57cec5SDimitry Andric 3562*0b57cec5SDimitry Andric template void PltSection::addEntry<ELF32LE>(Symbol &Sym); 3563*0b57cec5SDimitry Andric template void PltSection::addEntry<ELF32BE>(Symbol &Sym); 3564*0b57cec5SDimitry Andric template void PltSection::addEntry<ELF64LE>(Symbol &Sym); 3565*0b57cec5SDimitry Andric template void PltSection::addEntry<ELF64BE>(Symbol &Sym); 3566*0b57cec5SDimitry Andric 3567*0b57cec5SDimitry Andric template class elf::MipsAbiFlagsSection<ELF32LE>; 3568*0b57cec5SDimitry Andric template class elf::MipsAbiFlagsSection<ELF32BE>; 3569*0b57cec5SDimitry Andric template class elf::MipsAbiFlagsSection<ELF64LE>; 3570*0b57cec5SDimitry Andric template class elf::MipsAbiFlagsSection<ELF64BE>; 3571*0b57cec5SDimitry Andric 3572*0b57cec5SDimitry Andric template class elf::MipsOptionsSection<ELF32LE>; 3573*0b57cec5SDimitry Andric template class elf::MipsOptionsSection<ELF32BE>; 3574*0b57cec5SDimitry Andric template class elf::MipsOptionsSection<ELF64LE>; 3575*0b57cec5SDimitry Andric template class elf::MipsOptionsSection<ELF64BE>; 3576*0b57cec5SDimitry Andric 3577*0b57cec5SDimitry Andric template class elf::MipsReginfoSection<ELF32LE>; 3578*0b57cec5SDimitry Andric template class elf::MipsReginfoSection<ELF32BE>; 3579*0b57cec5SDimitry Andric template class elf::MipsReginfoSection<ELF64LE>; 3580*0b57cec5SDimitry Andric template class elf::MipsReginfoSection<ELF64BE>; 3581*0b57cec5SDimitry Andric 3582*0b57cec5SDimitry Andric template class elf::DynamicSection<ELF32LE>; 3583*0b57cec5SDimitry Andric template class elf::DynamicSection<ELF32BE>; 3584*0b57cec5SDimitry Andric template class elf::DynamicSection<ELF64LE>; 3585*0b57cec5SDimitry Andric template class elf::DynamicSection<ELF64BE>; 3586*0b57cec5SDimitry Andric 3587*0b57cec5SDimitry Andric template class elf::RelocationSection<ELF32LE>; 3588*0b57cec5SDimitry Andric template class elf::RelocationSection<ELF32BE>; 3589*0b57cec5SDimitry Andric template class elf::RelocationSection<ELF64LE>; 3590*0b57cec5SDimitry Andric template class elf::RelocationSection<ELF64BE>; 3591*0b57cec5SDimitry Andric 3592*0b57cec5SDimitry Andric template class elf::AndroidPackedRelocationSection<ELF32LE>; 3593*0b57cec5SDimitry Andric template class elf::AndroidPackedRelocationSection<ELF32BE>; 3594*0b57cec5SDimitry Andric template class elf::AndroidPackedRelocationSection<ELF64LE>; 3595*0b57cec5SDimitry Andric template class elf::AndroidPackedRelocationSection<ELF64BE>; 3596*0b57cec5SDimitry Andric 3597*0b57cec5SDimitry Andric template class elf::RelrSection<ELF32LE>; 3598*0b57cec5SDimitry Andric template class elf::RelrSection<ELF32BE>; 3599*0b57cec5SDimitry Andric template class elf::RelrSection<ELF64LE>; 3600*0b57cec5SDimitry Andric template class elf::RelrSection<ELF64BE>; 3601*0b57cec5SDimitry Andric 3602*0b57cec5SDimitry Andric template class elf::SymbolTableSection<ELF32LE>; 3603*0b57cec5SDimitry Andric template class elf::SymbolTableSection<ELF32BE>; 3604*0b57cec5SDimitry Andric template class elf::SymbolTableSection<ELF64LE>; 3605*0b57cec5SDimitry Andric template class elf::SymbolTableSection<ELF64BE>; 3606*0b57cec5SDimitry Andric 3607*0b57cec5SDimitry Andric template class elf::VersionNeedSection<ELF32LE>; 3608*0b57cec5SDimitry Andric template class elf::VersionNeedSection<ELF32BE>; 3609*0b57cec5SDimitry Andric template class elf::VersionNeedSection<ELF64LE>; 3610*0b57cec5SDimitry Andric template class elf::VersionNeedSection<ELF64BE>; 3611*0b57cec5SDimitry Andric 3612*0b57cec5SDimitry Andric template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part); 3613*0b57cec5SDimitry Andric template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part); 3614*0b57cec5SDimitry Andric template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part); 3615*0b57cec5SDimitry Andric template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part); 3616*0b57cec5SDimitry Andric 3617*0b57cec5SDimitry Andric template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part); 3618*0b57cec5SDimitry Andric template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part); 3619*0b57cec5SDimitry Andric template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part); 3620*0b57cec5SDimitry Andric template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part); 3621*0b57cec5SDimitry Andric 3622*0b57cec5SDimitry Andric template class elf::PartitionElfHeaderSection<ELF32LE>; 3623*0b57cec5SDimitry Andric template class elf::PartitionElfHeaderSection<ELF32BE>; 3624*0b57cec5SDimitry Andric template class elf::PartitionElfHeaderSection<ELF64LE>; 3625*0b57cec5SDimitry Andric template class elf::PartitionElfHeaderSection<ELF64BE>; 3626*0b57cec5SDimitry Andric 3627*0b57cec5SDimitry Andric template class elf::PartitionProgramHeadersSection<ELF32LE>; 3628*0b57cec5SDimitry Andric template class elf::PartitionProgramHeadersSection<ELF32BE>; 3629*0b57cec5SDimitry Andric template class elf::PartitionProgramHeadersSection<ELF64LE>; 3630*0b57cec5SDimitry Andric template class elf::PartitionProgramHeadersSection<ELF64BE>; 3631