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