1 //===- SyntheticSections.cpp ----------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains linker-synthesized sections. Currently, 11 // synthetic sections are created either output sections or input sections, 12 // but we are rewriting code so that all synthetic sections are created as 13 // input sections. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "SyntheticSections.h" 18 #include "Config.h" 19 #include "Error.h" 20 #include "InputFiles.h" 21 #include "LinkerScript.h" 22 #include "Memory.h" 23 #include "OutputSections.h" 24 #include "Strings.h" 25 #include "SymbolTable.h" 26 #include "Target.h" 27 #include "Threads.h" 28 #include "Writer.h" 29 #include "lld/Config/Version.h" 30 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 31 #include "llvm/Object/ELFObjectFile.h" 32 #include "llvm/Support/Dwarf.h" 33 #include "llvm/Support/Endian.h" 34 #include "llvm/Support/MD5.h" 35 #include "llvm/Support/RandomNumberGenerator.h" 36 #include "llvm/Support/SHA1.h" 37 #include "llvm/Support/xxhash.h" 38 #include <cstdlib> 39 40 using namespace llvm; 41 using namespace llvm::dwarf; 42 using namespace llvm::ELF; 43 using namespace llvm::object; 44 using namespace llvm::support; 45 using namespace llvm::support::endian; 46 47 using namespace lld; 48 using namespace lld::elf; 49 50 uint64_t SyntheticSection::getVA() const { 51 if (this->OutSec) 52 return this->OutSec->Addr + this->OutSecOff; 53 return 0; 54 } 55 56 template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() { 57 std::vector<DefinedCommon *> V; 58 for (Symbol *S : Symtab<ELFT>::X->getSymbols()) 59 if (auto *B = dyn_cast<DefinedCommon>(S->body())) 60 V.push_back(B); 61 return V; 62 } 63 64 // Find all common symbols and allocate space for them. 65 template <class ELFT> InputSection *elf::createCommonSection() { 66 auto *Ret = make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, 67 ArrayRef<uint8_t>(), "COMMON"); 68 Ret->Live = true; 69 70 if (!Config->DefineCommon) 71 return Ret; 72 73 // Sort the common symbols by alignment as an heuristic to pack them better. 74 std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>(); 75 std::stable_sort(Syms.begin(), Syms.end(), 76 [](const DefinedCommon *A, const DefinedCommon *B) { 77 return A->Alignment > B->Alignment; 78 }); 79 80 // Assign offsets to symbols. 81 size_t Size = 0; 82 size_t Alignment = 1; 83 for (DefinedCommon *Sym : Syms) { 84 Alignment = std::max<size_t>(Alignment, Sym->Alignment); 85 Size = alignTo(Size, Sym->Alignment); 86 87 // Compute symbol offset relative to beginning of input section. 88 Sym->Offset = Size; 89 Size += Sym->Size; 90 } 91 Ret->Alignment = Alignment; 92 Ret->Data = makeArrayRef<uint8_t>(nullptr, Size); 93 return Ret; 94 } 95 96 // Returns an LLD version string. 97 static ArrayRef<uint8_t> getVersion() { 98 // Check LLD_VERSION first for ease of testing. 99 // You can get consitent output by using the environment variable. 100 // This is only for testing. 101 StringRef S = getenv("LLD_VERSION"); 102 if (S.empty()) 103 S = Saver.save(Twine("Linker: ") + getLLDVersion()); 104 105 // +1 to include the terminating '\0'. 106 return {(const uint8_t *)S.data(), S.size() + 1}; 107 } 108 109 // Creates a .comment section containing LLD version info. 110 // With this feature, you can identify LLD-generated binaries easily 111 // by "objdump -s -j .comment <file>". 112 // The returned object is a mergeable string section. 113 template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() { 114 typename ELFT::Shdr Hdr = {}; 115 Hdr.sh_flags = SHF_MERGE | SHF_STRINGS; 116 Hdr.sh_type = SHT_PROGBITS; 117 Hdr.sh_entsize = 1; 118 Hdr.sh_addralign = 1; 119 120 auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment"); 121 Ret->Data = getVersion(); 122 Ret->splitIntoPieces(); 123 return Ret; 124 } 125 126 // .MIPS.abiflags section. 127 template <class ELFT> 128 MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags) 129 : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), 130 Flags(Flags) { 131 this->Entsize = sizeof(Elf_Mips_ABIFlags); 132 } 133 134 template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) { 135 memcpy(Buf, &Flags, sizeof(Flags)); 136 } 137 138 template <class ELFT> 139 MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() { 140 Elf_Mips_ABIFlags Flags = {}; 141 bool Create = false; 142 143 for (InputSectionBase *Sec : InputSections) { 144 if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS) 145 continue; 146 Sec->Live = false; 147 Create = true; 148 149 std::string Filename = toString(Sec->getFile<ELFT>()); 150 const size_t Size = Sec->Data.size(); 151 // Older version of BFD (such as the default FreeBSD linker) concatenate 152 // .MIPS.abiflags instead of merging. To allow for this case (or potential 153 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags 154 if (Size < sizeof(Elf_Mips_ABIFlags)) { 155 error(Filename + ": invalid size of .MIPS.abiflags section: got " + 156 Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); 157 return nullptr; 158 } 159 auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data()); 160 if (S->version != 0) { 161 error(Filename + ": unexpected .MIPS.abiflags version " + 162 Twine(S->version)); 163 return nullptr; 164 } 165 166 // LLD checks ISA compatibility in getMipsEFlags(). Here we just 167 // select the highest number of ISA/Rev/Ext. 168 Flags.isa_level = std::max(Flags.isa_level, S->isa_level); 169 Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev); 170 Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext); 171 Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size); 172 Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size); 173 Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size); 174 Flags.ases |= S->ases; 175 Flags.flags1 |= S->flags1; 176 Flags.flags2 |= S->flags2; 177 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename); 178 }; 179 180 if (Create) 181 return make<MipsAbiFlagsSection<ELFT>>(Flags); 182 return nullptr; 183 } 184 185 // .MIPS.options section. 186 template <class ELFT> 187 MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo) 188 : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), 189 Reginfo(Reginfo) { 190 this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 191 } 192 193 template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) { 194 auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf); 195 Options->kind = ODK_REGINFO; 196 Options->size = getSize(); 197 198 if (!Config->Relocatable) 199 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); 200 memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo)); 201 } 202 203 template <class ELFT> 204 MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { 205 // N64 ABI only. 206 if (!ELFT::Is64Bits) 207 return nullptr; 208 209 Elf_Mips_RegInfo Reginfo = {}; 210 bool Create = false; 211 212 for (InputSectionBase *Sec : InputSections) { 213 if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS) 214 continue; 215 Sec->Live = false; 216 Create = true; 217 218 std::string Filename = toString(Sec->getFile<ELFT>()); 219 ArrayRef<uint8_t> D = Sec->Data; 220 221 while (!D.empty()) { 222 if (D.size() < sizeof(Elf_Mips_Options)) { 223 error(Filename + ": invalid size of .MIPS.options section"); 224 break; 225 } 226 227 auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data()); 228 if (Opt->kind == ODK_REGINFO) { 229 if (Config->Relocatable && Opt->getRegInfo().ri_gp_value) 230 error(Filename + ": unsupported non-zero ri_gp_value"); 231 Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask; 232 Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value; 233 break; 234 } 235 236 if (!Opt->size) 237 fatal(Filename + ": zero option descriptor size"); 238 D = D.slice(Opt->size); 239 } 240 }; 241 242 if (Create) 243 return make<MipsOptionsSection<ELFT>>(Reginfo); 244 return nullptr; 245 } 246 247 // MIPS .reginfo section. 248 template <class ELFT> 249 MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo) 250 : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), 251 Reginfo(Reginfo) { 252 this->Entsize = sizeof(Elf_Mips_RegInfo); 253 } 254 255 template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) { 256 if (!Config->Relocatable) 257 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); 258 memcpy(Buf, &Reginfo, sizeof(Reginfo)); 259 } 260 261 template <class ELFT> 262 MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { 263 // Section should be alive for O32 and N32 ABIs only. 264 if (ELFT::Is64Bits) 265 return nullptr; 266 267 Elf_Mips_RegInfo Reginfo = {}; 268 bool Create = false; 269 270 for (InputSectionBase *Sec : InputSections) { 271 if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO) 272 continue; 273 Sec->Live = false; 274 Create = true; 275 276 if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) { 277 error(toString(Sec->getFile<ELFT>()) + 278 ": invalid size of .reginfo section"); 279 return nullptr; 280 } 281 auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data()); 282 if (Config->Relocatable && R->ri_gp_value) 283 error(toString(Sec->getFile<ELFT>()) + 284 ": unsupported non-zero ri_gp_value"); 285 286 Reginfo.ri_gprmask |= R->ri_gprmask; 287 Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value; 288 }; 289 290 if (Create) 291 return make<MipsReginfoSection<ELFT>>(Reginfo); 292 return nullptr; 293 } 294 295 InputSection *elf::createInterpSection() { 296 // StringSaver guarantees that the returned string ends with '\0'. 297 StringRef S = Saver.save(Config->DynamicLinker); 298 ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1}; 299 300 auto *Sec = 301 make<InputSection>(SHF_ALLOC, SHT_PROGBITS, 1, Contents, ".interp"); 302 Sec->Live = true; 303 return Sec; 304 } 305 306 template <class ELFT> 307 SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value, 308 uint64_t Size, InputSectionBase *Section) { 309 auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type, 310 Value, Size, Section, nullptr); 311 if (In<ELFT>::SymTab) 312 In<ELFT>::SymTab->addSymbol(S); 313 return S; 314 } 315 316 static size_t getHashSize() { 317 switch (Config->BuildId) { 318 case BuildIdKind::Fast: 319 return 8; 320 case BuildIdKind::Md5: 321 case BuildIdKind::Uuid: 322 return 16; 323 case BuildIdKind::Sha1: 324 return 20; 325 case BuildIdKind::Hexstring: 326 return Config->BuildIdVector.size(); 327 default: 328 llvm_unreachable("unknown BuildIdKind"); 329 } 330 } 331 332 template <class ELFT> 333 BuildIdSection<ELFT>::BuildIdSection() 334 : SyntheticSection(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"), 335 HashSize(getHashSize()) {} 336 337 template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) { 338 const endianness E = ELFT::TargetEndianness; 339 write32<E>(Buf, 4); // Name size 340 write32<E>(Buf + 4, HashSize); // Content size 341 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type 342 memcpy(Buf + 12, "GNU", 4); // Name string 343 HashBuf = Buf + 16; 344 } 345 346 // Split one uint8 array into small pieces of uint8 arrays. 347 static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr, 348 size_t ChunkSize) { 349 std::vector<ArrayRef<uint8_t>> Ret; 350 while (Arr.size() > ChunkSize) { 351 Ret.push_back(Arr.take_front(ChunkSize)); 352 Arr = Arr.drop_front(ChunkSize); 353 } 354 if (!Arr.empty()) 355 Ret.push_back(Arr); 356 return Ret; 357 } 358 359 // Computes a hash value of Data using a given hash function. 360 // In order to utilize multiple cores, we first split data into 1MB 361 // chunks, compute a hash for each chunk, and then compute a hash value 362 // of the hash values. 363 template <class ELFT> 364 void BuildIdSection<ELFT>::computeHash( 365 llvm::ArrayRef<uint8_t> Data, 366 std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) { 367 std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024); 368 std::vector<uint8_t> Hashes(Chunks.size() * HashSize); 369 370 // Compute hash values. 371 forLoop(0, Chunks.size(), 372 [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); }); 373 374 // Write to the final output buffer. 375 HashFn(HashBuf, Hashes); 376 } 377 378 template <class ELFT> 379 CopyRelSection<ELFT>::CopyRelSection(bool ReadOnly, uintX_t AddrAlign, size_t S) 380 : SyntheticSection(SHF_ALLOC, SHT_NOBITS, AddrAlign, 381 ReadOnly ? ".bss.rel.ro" : ".bss"), 382 Size(S) {} 383 384 template <class ELFT> 385 void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) { 386 switch (Config->BuildId) { 387 case BuildIdKind::Fast: 388 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { 389 write64le(Dest, xxHash64(toStringRef(Arr))); 390 }); 391 break; 392 case BuildIdKind::Md5: 393 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { 394 memcpy(Dest, MD5::hash(Arr).data(), 16); 395 }); 396 break; 397 case BuildIdKind::Sha1: 398 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { 399 memcpy(Dest, SHA1::hash(Arr).data(), 20); 400 }); 401 break; 402 case BuildIdKind::Uuid: 403 if (getRandomBytes(HashBuf, HashSize)) 404 error("entropy source failure"); 405 break; 406 case BuildIdKind::Hexstring: 407 memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size()); 408 break; 409 default: 410 llvm_unreachable("unknown BuildIdKind"); 411 } 412 } 413 414 template <class ELFT> 415 EhFrameSection<ELFT>::EhFrameSection() 416 : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} 417 418 // Search for an existing CIE record or create a new one. 419 // CIE records from input object files are uniquified by their contents 420 // and where their relocations point to. 421 template <class ELFT> 422 template <class RelTy> 423 CieRecord *EhFrameSection<ELFT>::addCie(EhSectionPiece &Piece, 424 ArrayRef<RelTy> Rels) { 425 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID); 426 const endianness E = ELFT::TargetEndianness; 427 if (read32<E>(Piece.data().data() + 4) != 0) 428 fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame"); 429 430 SymbolBody *Personality = nullptr; 431 unsigned FirstRelI = Piece.FirstRelocation; 432 if (FirstRelI != (unsigned)-1) 433 Personality = 434 &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]); 435 436 // Search for an existing CIE by CIE contents/relocation target pair. 437 CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; 438 439 // If not found, create a new one. 440 if (Cie->Piece == nullptr) { 441 Cie->Piece = &Piece; 442 Cies.push_back(Cie); 443 } 444 return Cie; 445 } 446 447 // There is one FDE per function. Returns true if a given FDE 448 // points to a live function. 449 template <class ELFT> 450 template <class RelTy> 451 bool EhFrameSection<ELFT>::isFdeLive(EhSectionPiece &Piece, 452 ArrayRef<RelTy> Rels) { 453 auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID); 454 unsigned FirstRelI = Piece.FirstRelocation; 455 if (FirstRelI == (unsigned)-1) 456 return false; 457 const RelTy &Rel = Rels[FirstRelI]; 458 SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel); 459 auto *D = dyn_cast<DefinedRegular>(&B); 460 if (!D || !D->Section) 461 return false; 462 InputSectionBase *Target = D->Section->Repl; 463 return Target && Target->Live; 464 } 465 466 // .eh_frame is a sequence of CIE or FDE records. In general, there 467 // is one CIE record per input object file which is followed by 468 // a list of FDEs. This function searches an existing CIE or create a new 469 // one and associates FDEs to the CIE. 470 template <class ELFT> 471 template <class RelTy> 472 void EhFrameSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, 473 ArrayRef<RelTy> Rels) { 474 const endianness E = ELFT::TargetEndianness; 475 476 DenseMap<size_t, CieRecord *> OffsetToCie; 477 for (EhSectionPiece &Piece : Sec->Pieces) { 478 // The empty record is the end marker. 479 if (Piece.size() == 4) 480 return; 481 482 size_t Offset = Piece.InputOff; 483 uint32_t ID = read32<E>(Piece.data().data() + 4); 484 if (ID == 0) { 485 OffsetToCie[Offset] = addCie(Piece, Rels); 486 continue; 487 } 488 489 uint32_t CieOffset = Offset + 4 - ID; 490 CieRecord *Cie = OffsetToCie[CieOffset]; 491 if (!Cie) 492 fatal(toString(Sec) + ": invalid CIE reference"); 493 494 if (!isFdeLive(Piece, Rels)) 495 continue; 496 Cie->FdePieces.push_back(&Piece); 497 NumFdes++; 498 } 499 } 500 501 template <class ELFT> 502 void EhFrameSection<ELFT>::addSection(InputSectionBase *C) { 503 auto *Sec = cast<EhInputSection<ELFT>>(C); 504 Sec->EHSec = this; 505 updateAlignment(Sec->Alignment); 506 Sections.push_back(Sec); 507 508 // .eh_frame is a sequence of CIE or FDE records. This function 509 // splits it into pieces so that we can call 510 // SplitInputSection::getSectionPiece on the section. 511 Sec->split(); 512 if (Sec->Pieces.empty()) 513 return; 514 515 if (Sec->NumRelocations) { 516 if (Sec->AreRelocsRela) 517 addSectionAux(Sec, Sec->template relas<ELFT>()); 518 else 519 addSectionAux(Sec, Sec->template rels<ELFT>()); 520 return; 521 } 522 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); 523 } 524 525 template <class ELFT> 526 static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { 527 memcpy(Buf, D.data(), D.size()); 528 529 // Fix the size field. -4 since size does not include the size field itself. 530 const endianness E = ELFT::TargetEndianness; 531 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); 532 } 533 534 template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() { 535 if (this->Size) 536 return; // Already finalized. 537 538 size_t Off = 0; 539 for (CieRecord *Cie : Cies) { 540 Cie->Piece->OutputOff = Off; 541 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); 542 543 for (EhSectionPiece *Fde : Cie->FdePieces) { 544 Fde->OutputOff = Off; 545 Off += alignTo(Fde->size(), sizeof(uintX_t)); 546 } 547 } 548 this->Size = Off; 549 } 550 551 template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { 552 const endianness E = ELFT::TargetEndianness; 553 switch (Size) { 554 case DW_EH_PE_udata2: 555 return read16<E>(Buf); 556 case DW_EH_PE_udata4: 557 return read32<E>(Buf); 558 case DW_EH_PE_udata8: 559 return read64<E>(Buf); 560 case DW_EH_PE_absptr: 561 if (ELFT::Is64Bits) 562 return read64<E>(Buf); 563 return read32<E>(Buf); 564 } 565 fatal("unknown FDE size encoding"); 566 } 567 568 // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. 569 // We need it to create .eh_frame_hdr section. 570 template <class ELFT> 571 typename ELFT::uint EhFrameSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, 572 uint8_t Enc) { 573 // The starting address to which this FDE applies is 574 // stored at FDE + 8 byte. 575 size_t Off = FdeOff + 8; 576 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); 577 if ((Enc & 0x70) == DW_EH_PE_absptr) 578 return Addr; 579 if ((Enc & 0x70) == DW_EH_PE_pcrel) 580 return Addr + this->OutSec->Addr + Off; 581 fatal("unknown FDE size relative encoding"); 582 } 583 584 template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) { 585 const endianness E = ELFT::TargetEndianness; 586 for (CieRecord *Cie : Cies) { 587 size_t CieOffset = Cie->Piece->OutputOff; 588 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); 589 590 for (EhSectionPiece *Fde : Cie->FdePieces) { 591 size_t Off = Fde->OutputOff; 592 writeCieFde<ELFT>(Buf + Off, Fde->data()); 593 594 // FDE's second word should have the offset to an associated CIE. 595 // Write it. 596 write32<E>(Buf + Off + 4, Off + 4 - CieOffset); 597 } 598 } 599 600 for (EhInputSection<ELFT> *S : Sections) 601 S->template relocate<ELFT>(Buf, nullptr); 602 603 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table 604 // to get a FDE from an address to which FDE is applied. So here 605 // we obtain two addresses and pass them to EhFrameHdr object. 606 if (In<ELFT>::EhFrameHdr) { 607 for (CieRecord *Cie : Cies) { 608 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece); 609 for (SectionPiece *Fde : Cie->FdePieces) { 610 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); 611 uintX_t FdeVA = this->OutSec->Addr + Fde->OutputOff; 612 In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); 613 } 614 } 615 } 616 } 617 618 template <class ELFT> 619 GotSection<ELFT>::GotSection() 620 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 621 Target->GotEntrySize, ".got") {} 622 623 template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) { 624 Sym.GotIndex = NumEntries; 625 ++NumEntries; 626 } 627 628 template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { 629 if (Sym.GlobalDynIndex != -1U) 630 return false; 631 Sym.GlobalDynIndex = NumEntries; 632 // Global Dynamic TLS entries take two GOT slots. 633 NumEntries += 2; 634 return true; 635 } 636 637 // Reserves TLS entries for a TLS module ID and a TLS block offset. 638 // In total it takes two GOT slots. 639 template <class ELFT> bool GotSection<ELFT>::addTlsIndex() { 640 if (TlsIndexOff != uint32_t(-1)) 641 return false; 642 TlsIndexOff = NumEntries * sizeof(uintX_t); 643 NumEntries += 2; 644 return true; 645 } 646 647 template <class ELFT> 648 typename GotSection<ELFT>::uintX_t 649 GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { 650 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t); 651 } 652 653 template <class ELFT> 654 typename GotSection<ELFT>::uintX_t 655 GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { 656 return B.GlobalDynIndex * sizeof(uintX_t); 657 } 658 659 template <class ELFT> void GotSection<ELFT>::finalizeContents() { 660 Size = NumEntries * sizeof(uintX_t); 661 } 662 663 template <class ELFT> bool GotSection<ELFT>::empty() const { 664 // If we have a relocation that is relative to GOT (such as GOTOFFREL), 665 // we need to emit a GOT even if it's empty. 666 return NumEntries == 0 && !HasGotOffRel; 667 } 668 669 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { 670 this->template relocate<ELFT>(Buf, Buf + Size); 671 } 672 673 template <class ELFT> 674 MipsGotSection<ELFT>::MipsGotSection() 675 : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, 676 ".got") {} 677 678 template <class ELFT> 679 void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend, 680 RelExpr Expr) { 681 // For "true" local symbols which can be referenced from the same module 682 // only compiler creates two instructions for address loading: 683 // 684 // lw $8, 0($gp) # R_MIPS_GOT16 685 // addi $8, $8, 0 # R_MIPS_LO16 686 // 687 // The first instruction loads high 16 bits of the symbol address while 688 // the second adds an offset. That allows to reduce number of required 689 // GOT entries because only one global offset table entry is necessary 690 // for every 64 KBytes of local data. So for local symbols we need to 691 // allocate number of GOT entries to hold all required "page" addresses. 692 // 693 // All global symbols (hidden and regular) considered by compiler uniformly. 694 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation 695 // to load address of the symbol. So for each such symbol we need to 696 // allocate dedicated GOT entry to store its address. 697 // 698 // If a symbol is preemptible we need help of dynamic linker to get its 699 // final address. The corresponding GOT entries are allocated in the 700 // "global" part of GOT. Entries for non preemptible global symbol allocated 701 // in the "local" part of GOT. 702 // 703 // See "Global Offset Table" in Chapter 5: 704 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 705 if (Expr == R_MIPS_GOT_LOCAL_PAGE) { 706 // At this point we do not know final symbol value so to reduce number 707 // of allocated GOT entries do the following trick. Save all output 708 // sections referenced by GOT relocations. Then later in the `finalize` 709 // method calculate number of "pages" required to cover all saved output 710 // section and allocate appropriate number of GOT entries. 711 auto *DefSym = cast<DefinedRegular>(&Sym); 712 PageIndexMap.insert( 713 {DefSym->Section->template getOutputSection<ELFT>(), 0}); 714 return; 715 } 716 if (Sym.isTls()) { 717 // GOT entries created for MIPS TLS relocations behave like 718 // almost GOT entries from other ABIs. They go to the end 719 // of the global offset table. 720 Sym.GotIndex = TlsEntries.size(); 721 TlsEntries.push_back(&Sym); 722 return; 723 } 724 auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) { 725 if (S.isInGot() && !A) 726 return; 727 size_t NewIndex = Items.size(); 728 if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second) 729 return; 730 Items.emplace_back(&S, A); 731 if (!A) 732 S.GotIndex = NewIndex; 733 }; 734 if (Sym.isPreemptible()) { 735 // Ignore addends for preemptible symbols. They got single GOT entry anyway. 736 AddEntry(Sym, 0, GlobalEntries); 737 Sym.IsInGlobalMipsGot = true; 738 } else if (Expr == R_MIPS_GOT_OFF32) { 739 AddEntry(Sym, Addend, LocalEntries32); 740 Sym.Is32BitMipsGot = true; 741 } else { 742 // Hold local GOT entries accessed via a 16-bit index separately. 743 // That allows to write them in the beginning of the GOT and keep 744 // their indexes as less as possible to escape relocation's overflow. 745 AddEntry(Sym, Addend, LocalEntries); 746 } 747 } 748 749 template <class ELFT> 750 bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { 751 if (Sym.GlobalDynIndex != -1U) 752 return false; 753 Sym.GlobalDynIndex = TlsEntries.size(); 754 // Global Dynamic TLS entries take two GOT slots. 755 TlsEntries.push_back(nullptr); 756 TlsEntries.push_back(&Sym); 757 return true; 758 } 759 760 // Reserves TLS entries for a TLS module ID and a TLS block offset. 761 // In total it takes two GOT slots. 762 template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() { 763 if (TlsIndexOff != uint32_t(-1)) 764 return false; 765 TlsIndexOff = TlsEntries.size() * sizeof(uintX_t); 766 TlsEntries.push_back(nullptr); 767 TlsEntries.push_back(nullptr); 768 return true; 769 } 770 771 static uint64_t getMipsPageAddr(uint64_t Addr) { 772 return (Addr + 0x8000) & ~0xffff; 773 } 774 775 static uint64_t getMipsPageCount(uint64_t Size) { 776 return (Size + 0xfffe) / 0xffff + 1; 777 } 778 779 template <class ELFT> 780 typename MipsGotSection<ELFT>::uintX_t 781 MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B, 782 int64_t Addend) const { 783 const OutputSection *OutSec = 784 cast<DefinedRegular>(&B)->Section->template getOutputSection<ELFT>(); 785 uintX_t SecAddr = getMipsPageAddr(OutSec->Addr); 786 uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend)); 787 uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff; 788 assert(Index < PageEntriesNum); 789 return (HeaderEntriesNum + Index) * sizeof(uintX_t); 790 } 791 792 template <class ELFT> 793 typename MipsGotSection<ELFT>::uintX_t 794 MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B, 795 int64_t Addend) const { 796 // Calculate offset of the GOT entries block: TLS, global, local. 797 uintX_t Index = HeaderEntriesNum + PageEntriesNum; 798 if (B.isTls()) 799 Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size(); 800 else if (B.IsInGlobalMipsGot) 801 Index += LocalEntries.size() + LocalEntries32.size(); 802 else if (B.Is32BitMipsGot) 803 Index += LocalEntries.size(); 804 // Calculate offset of the GOT entry in the block. 805 if (B.isInGot()) 806 Index += B.GotIndex; 807 else { 808 auto It = EntryIndexMap.find({&B, Addend}); 809 assert(It != EntryIndexMap.end()); 810 Index += It->second; 811 } 812 return Index * sizeof(uintX_t); 813 } 814 815 template <class ELFT> 816 typename MipsGotSection<ELFT>::uintX_t 817 MipsGotSection<ELFT>::getTlsOffset() const { 818 return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t); 819 } 820 821 template <class ELFT> 822 typename MipsGotSection<ELFT>::uintX_t 823 MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { 824 return B.GlobalDynIndex * sizeof(uintX_t); 825 } 826 827 template <class ELFT> 828 const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const { 829 return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first; 830 } 831 832 template <class ELFT> 833 unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const { 834 return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() + 835 LocalEntries32.size(); 836 } 837 838 template <class ELFT> void MipsGotSection<ELFT>::finalizeContents() { 839 PageEntriesNum = 0; 840 for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) { 841 // For each output section referenced by GOT page relocations calculate 842 // and save into PageIndexMap an upper bound of MIPS GOT entries required 843 // to store page addresses of local symbols. We assume the worst case - 844 // each 64kb page of the output section has at least one GOT relocation 845 // against it. And take in account the case when the section intersects 846 // page boundaries. 847 P.second = PageEntriesNum; 848 PageEntriesNum += getMipsPageCount(P.first->Size); 849 } 850 Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) * 851 sizeof(uintX_t); 852 } 853 854 template <class ELFT> bool MipsGotSection<ELFT>::empty() const { 855 // We add the .got section to the result for dynamic MIPS target because 856 // its address and properties are mentioned in the .dynamic section. 857 return Config->Relocatable; 858 } 859 860 template <class ELFT> 861 typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const { 862 return ElfSym::MipsGp->template getVA<ELFT>(0); 863 } 864 865 template <class ELFT> 866 static void writeUint(uint8_t *Buf, typename ELFT::uint Val) { 867 typedef typename ELFT::uint uintX_t; 868 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val); 869 } 870 871 template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) { 872 // Set the MSB of the second GOT slot. This is not required by any 873 // MIPS ABI documentation, though. 874 // 875 // There is a comment in glibc saying that "The MSB of got[1] of a 876 // gnu object is set to identify gnu objects," and in GNU gold it 877 // says "the second entry will be used by some runtime loaders". 878 // But how this field is being used is unclear. 879 // 880 // We are not really willing to mimic other linkers behaviors 881 // without understanding why they do that, but because all files 882 // generated by GNU tools have this special GOT value, and because 883 // we've been doing this for years, it is probably a safe bet to 884 // keep doing this for now. We really need to revisit this to see 885 // if we had to do this. 886 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf); 887 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); 888 Buf += HeaderEntriesNum * sizeof(uintX_t); 889 // Write 'page address' entries to the local part of the GOT. 890 for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) { 891 size_t PageCount = getMipsPageCount(L.first->Size); 892 uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr); 893 for (size_t PI = 0; PI < PageCount; ++PI) { 894 uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t); 895 writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000); 896 } 897 } 898 Buf += PageEntriesNum * sizeof(uintX_t); 899 auto AddEntry = [&](const GotEntry &SA) { 900 uint8_t *Entry = Buf; 901 Buf += sizeof(uintX_t); 902 const SymbolBody *Body = SA.first; 903 uintX_t VA = Body->template getVA<ELFT>(SA.second); 904 writeUint<ELFT>(Entry, VA); 905 }; 906 std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry); 907 std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry); 908 std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry); 909 // Initialize TLS-related GOT entries. If the entry has a corresponding 910 // dynamic relocations, leave it initialized by zero. Write down adjusted 911 // TLS symbol's values otherwise. To calculate the adjustments use offsets 912 // for thread-local storage. 913 // https://www.linux-mips.org/wiki/NPTL 914 if (TlsIndexOff != -1U && !Config->pic()) 915 writeUint<ELFT>(Buf + TlsIndexOff, 1); 916 for (const SymbolBody *B : TlsEntries) { 917 if (!B || B->isPreemptible()) 918 continue; 919 uintX_t VA = B->getVA<ELFT>(); 920 if (B->GotIndex != -1U) { 921 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t); 922 writeUint<ELFT>(Entry, VA - 0x7000); 923 } 924 if (B->GlobalDynIndex != -1U) { 925 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t); 926 writeUint<ELFT>(Entry, 1); 927 Entry += sizeof(uintX_t); 928 writeUint<ELFT>(Entry, VA - 0x8000); 929 } 930 } 931 } 932 933 template <class ELFT> 934 GotPltSection<ELFT>::GotPltSection() 935 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 936 Target->GotPltEntrySize, ".got.plt") {} 937 938 template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) { 939 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size(); 940 Entries.push_back(&Sym); 941 } 942 943 template <class ELFT> size_t GotPltSection<ELFT>::getSize() const { 944 return (Target->GotPltHeaderEntriesNum + Entries.size()) * 945 Target->GotPltEntrySize; 946 } 947 948 template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { 949 Target->writeGotPltHeader(Buf); 950 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; 951 for (const SymbolBody *B : Entries) { 952 Target->writeGotPlt(Buf, *B); 953 Buf += sizeof(uintX_t); 954 } 955 } 956 957 // On ARM the IgotPltSection is part of the GotSection, on other Targets it is 958 // part of the .got.plt 959 template <class ELFT> 960 IgotPltSection<ELFT>::IgotPltSection() 961 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 962 Target->GotPltEntrySize, 963 Config->EMachine == EM_ARM ? ".got" : ".got.plt") {} 964 965 template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) { 966 Sym.IsInIgot = true; 967 Sym.GotPltIndex = Entries.size(); 968 Entries.push_back(&Sym); 969 } 970 971 template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const { 972 return Entries.size() * Target->GotPltEntrySize; 973 } 974 975 template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) { 976 for (const SymbolBody *B : Entries) { 977 Target->writeIgotPlt(Buf, *B); 978 Buf += sizeof(uintX_t); 979 } 980 } 981 982 template <class ELFT> 983 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) 984 : SyntheticSection(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name), 985 Dynamic(Dynamic) { 986 // ELF string tables start with a NUL byte. 987 addString(""); 988 } 989 990 // Adds a string to the string table. If HashIt is true we hash and check for 991 // duplicates. It is optional because the name of global symbols are already 992 // uniqued and hashing them again has a big cost for a small value: uniquing 993 // them with some other string that happens to be the same. 994 template <class ELFT> 995 unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) { 996 if (HashIt) { 997 auto R = StringMap.insert(std::make_pair(S, this->Size)); 998 if (!R.second) 999 return R.first->second; 1000 } 1001 unsigned Ret = this->Size; 1002 this->Size = this->Size + S.size() + 1; 1003 Strings.push_back(S); 1004 return Ret; 1005 } 1006 1007 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { 1008 for (StringRef S : Strings) { 1009 memcpy(Buf, S.data(), S.size()); 1010 Buf += S.size() + 1; 1011 } 1012 } 1013 1014 // Returns the number of version definition entries. Because the first entry 1015 // is for the version definition itself, it is the number of versioned symbols 1016 // plus one. Note that we don't support multiple versions yet. 1017 static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } 1018 1019 template <class ELFT> 1020 DynamicSection<ELFT>::DynamicSection() 1021 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, sizeof(uintX_t), 1022 ".dynamic") { 1023 this->Entsize = ELFT::Is64Bits ? 16 : 8; 1024 1025 // .dynamic section is not writable on MIPS. 1026 // See "Special Section" in Chapter 4 in the following document: 1027 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1028 if (Config->EMachine == EM_MIPS) 1029 this->Flags = SHF_ALLOC; 1030 1031 addEntries(); 1032 } 1033 1034 // There are some dynamic entries that don't depend on other sections. 1035 // Such entries can be set early. 1036 template <class ELFT> void DynamicSection<ELFT>::addEntries() { 1037 // Add strings to .dynstr early so that .dynstr's size will be 1038 // fixed early. 1039 for (StringRef S : Config->AuxiliaryList) 1040 add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)}); 1041 if (!Config->RPath.empty()) 1042 add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, 1043 In<ELFT>::DynStrTab->addString(Config->RPath)}); 1044 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles()) 1045 if (F->isNeeded()) 1046 add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())}); 1047 if (!Config->SoName.empty()) 1048 add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)}); 1049 1050 // Set DT_FLAGS and DT_FLAGS_1. 1051 uint32_t DtFlags = 0; 1052 uint32_t DtFlags1 = 0; 1053 if (Config->Bsymbolic) 1054 DtFlags |= DF_SYMBOLIC; 1055 if (Config->ZNodelete) 1056 DtFlags1 |= DF_1_NODELETE; 1057 if (Config->ZNow) { 1058 DtFlags |= DF_BIND_NOW; 1059 DtFlags1 |= DF_1_NOW; 1060 } 1061 if (Config->ZOrigin) { 1062 DtFlags |= DF_ORIGIN; 1063 DtFlags1 |= DF_1_ORIGIN; 1064 } 1065 1066 if (DtFlags) 1067 add({DT_FLAGS, DtFlags}); 1068 if (DtFlags1) 1069 add({DT_FLAGS_1, DtFlags1}); 1070 1071 if (!Config->Shared && !Config->Relocatable) 1072 add({DT_DEBUG, (uint64_t)0}); 1073 } 1074 1075 // Add remaining entries to complete .dynamic contents. 1076 template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { 1077 if (this->Size) 1078 return; // Already finalized. 1079 1080 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; 1081 if (In<ELFT>::RelaDyn->OutSec->Size > 0) { 1082 bool IsRela = Config->Rela; 1083 add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn}); 1084 add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size}); 1085 add({IsRela ? DT_RELAENT : DT_RELENT, 1086 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))}); 1087 1088 // MIPS dynamic loader does not support RELCOUNT tag. 1089 // The problem is in the tight relation between dynamic 1090 // relocations and GOT. So do not emit this tag on MIPS. 1091 if (Config->EMachine != EM_MIPS) { 1092 size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount(); 1093 if (Config->ZCombreloc && NumRelativeRels) 1094 add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels}); 1095 } 1096 } 1097 if (In<ELFT>::RelaPlt->OutSec->Size > 0) { 1098 add({DT_JMPREL, In<ELFT>::RelaPlt}); 1099 add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size}); 1100 add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT, 1101 In<ELFT>::GotPlt}); 1102 add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)}); 1103 } 1104 1105 add({DT_SYMTAB, In<ELFT>::DynSymTab}); 1106 add({DT_SYMENT, sizeof(Elf_Sym)}); 1107 add({DT_STRTAB, In<ELFT>::DynStrTab}); 1108 add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()}); 1109 if (In<ELFT>::GnuHashTab) 1110 add({DT_GNU_HASH, In<ELFT>::GnuHashTab}); 1111 if (In<ELFT>::HashTab) 1112 add({DT_HASH, In<ELFT>::HashTab}); 1113 1114 if (Out::PreinitArray) { 1115 add({DT_PREINIT_ARRAY, Out::PreinitArray}); 1116 add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize}); 1117 } 1118 if (Out::InitArray) { 1119 add({DT_INIT_ARRAY, Out::InitArray}); 1120 add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize}); 1121 } 1122 if (Out::FiniArray) { 1123 add({DT_FINI_ARRAY, Out::FiniArray}); 1124 add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize}); 1125 } 1126 1127 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init)) 1128 add({DT_INIT, B}); 1129 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini)) 1130 add({DT_FINI, B}); 1131 1132 bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0; 1133 if (HasVerNeed || In<ELFT>::VerDef) 1134 add({DT_VERSYM, In<ELFT>::VerSym}); 1135 if (In<ELFT>::VerDef) { 1136 add({DT_VERDEF, In<ELFT>::VerDef}); 1137 add({DT_VERDEFNUM, getVerDefNum()}); 1138 } 1139 if (HasVerNeed) { 1140 add({DT_VERNEED, In<ELFT>::VerNeed}); 1141 add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()}); 1142 } 1143 1144 if (Config->EMachine == EM_MIPS) { 1145 add({DT_MIPS_RLD_VERSION, 1}); 1146 add({DT_MIPS_FLAGS, RHF_NOTPOT}); 1147 add({DT_MIPS_BASE_ADDRESS, Config->ImageBase}); 1148 add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()}); 1149 add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()}); 1150 if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry()) 1151 add({DT_MIPS_GOTSYM, B->DynsymIndex}); 1152 else 1153 add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()}); 1154 add({DT_PLTGOT, In<ELFT>::MipsGot}); 1155 if (In<ELFT>::MipsRldMap) 1156 add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap}); 1157 } 1158 1159 this->OutSec->Link = this->Link; 1160 1161 // +1 for DT_NULL 1162 this->Size = (Entries.size() + 1) * this->Entsize; 1163 } 1164 1165 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { 1166 auto *P = reinterpret_cast<Elf_Dyn *>(Buf); 1167 1168 for (const Entry &E : Entries) { 1169 P->d_tag = E.Tag; 1170 switch (E.Kind) { 1171 case Entry::SecAddr: 1172 P->d_un.d_ptr = E.OutSec->Addr; 1173 break; 1174 case Entry::InSecAddr: 1175 P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff; 1176 break; 1177 case Entry::SecSize: 1178 P->d_un.d_val = E.OutSec->Size; 1179 break; 1180 case Entry::SymAddr: 1181 P->d_un.d_ptr = E.Sym->template getVA<ELFT>(); 1182 break; 1183 case Entry::PlainInt: 1184 P->d_un.d_val = E.Val; 1185 break; 1186 } 1187 ++P; 1188 } 1189 } 1190 1191 template <class ELFT> 1192 typename ELFT::uint DynamicReloc<ELFT>::getOffset() const { 1193 return InputSec->OutSec->Addr + InputSec->getOffset<ELFT>(OffsetInSec); 1194 } 1195 1196 template <class ELFT> int64_t DynamicReloc<ELFT>::getAddend() const { 1197 if (UseSymVA) 1198 return Sym->getVA<ELFT>(Addend); 1199 return Addend; 1200 } 1201 1202 template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const { 1203 if (Sym && !UseSymVA) 1204 return Sym->DynsymIndex; 1205 return 0; 1206 } 1207 1208 template <class ELFT> 1209 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) 1210 : SyntheticSection(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL, 1211 sizeof(uintX_t), Name), 1212 Sort(Sort) { 1213 this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 1214 } 1215 1216 template <class ELFT> 1217 void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) { 1218 if (Reloc.Type == Target->RelativeRel) 1219 ++NumRelativeRelocs; 1220 Relocs.push_back(Reloc); 1221 } 1222 1223 template <class ELFT, class RelTy> 1224 static bool compRelocations(const RelTy &A, const RelTy &B) { 1225 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel; 1226 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel; 1227 if (AIsRel != BIsRel) 1228 return AIsRel; 1229 1230 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL); 1231 } 1232 1233 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { 1234 uint8_t *BufBegin = Buf; 1235 for (const DynamicReloc<ELFT> &Rel : Relocs) { 1236 auto *P = reinterpret_cast<Elf_Rela *>(Buf); 1237 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 1238 1239 if (Config->Rela) 1240 P->r_addend = Rel.getAddend(); 1241 P->r_offset = Rel.getOffset(); 1242 if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot) 1243 // Dynamic relocation against MIPS GOT section make deal TLS entries 1244 // allocated in the end of the GOT. We need to adjust the offset to take 1245 // in account 'local' and 'global' GOT entries. 1246 P->r_offset += In<ELFT>::MipsGot->getTlsOffset(); 1247 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL); 1248 } 1249 1250 if (Sort) { 1251 if (Config->Rela) 1252 std::stable_sort((Elf_Rela *)BufBegin, 1253 (Elf_Rela *)BufBegin + Relocs.size(), 1254 compRelocations<ELFT, Elf_Rela>); 1255 else 1256 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(), 1257 compRelocations<ELFT, Elf_Rel>); 1258 } 1259 } 1260 1261 template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { 1262 return this->Entsize * Relocs.size(); 1263 } 1264 1265 template <class ELFT> void RelocationSection<ELFT>::finalizeContents() { 1266 this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex 1267 : In<ELFT>::SymTab->OutSec->SectionIndex; 1268 1269 // Set required output section properties. 1270 this->OutSec->Link = this->Link; 1271 } 1272 1273 template <class ELFT> 1274 SymbolTableSection<ELFT>::SymbolTableSection( 1275 StringTableSection<ELFT> &StrTabSec) 1276 : SyntheticSection(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0, 1277 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, 1278 sizeof(uintX_t), 1279 StrTabSec.isDynamic() ? ".dynsym" : ".symtab"), 1280 StrTabSec(StrTabSec) { 1281 this->Entsize = sizeof(Elf_Sym); 1282 } 1283 1284 // Orders symbols according to their positions in the GOT, 1285 // in compliance with MIPS ABI rules. 1286 // See "Global Offset Table" in Chapter 5 in the following document 1287 // for detailed description: 1288 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1289 static bool sortMipsSymbols(const SymbolTableEntry &L, const SymbolTableEntry &R) { 1290 // Sort entries related to non-local preemptible symbols by GOT indexes. 1291 // All other entries go to the first part of GOT in arbitrary order. 1292 bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot; 1293 bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot; 1294 if (LIsInLocalGot || RIsInLocalGot) 1295 return !RIsInLocalGot; 1296 return L.Symbol->GotIndex < R.Symbol->GotIndex; 1297 } 1298 1299 // Finalize a symbol table. The ELF spec requires that all local 1300 // symbols precede global symbols, so we sort symbol entries in this 1301 // function. (For .dynsym, we don't do that because symbols for 1302 // dynamic linking are inherently all globals.) 1303 template <class ELFT> void SymbolTableSection<ELFT>::finalizeContents() { 1304 this->OutSec->Link = StrTabSec.OutSec->SectionIndex; 1305 1306 // If it is a .dynsym, there should be no local symbols, but we need 1307 // to do a few things for the dynamic linker. 1308 if (this->Type == SHT_DYNSYM) { 1309 // Section's Info field has the index of the first non-local symbol. 1310 // Because the first symbol entry is a null entry, 1 is the first. 1311 this->OutSec->Info = 1; 1312 1313 if (In<ELFT>::GnuHashTab) { 1314 // NB: It also sorts Symbols to meet the GNU hash table requirements. 1315 In<ELFT>::GnuHashTab->addSymbols(Symbols); 1316 } else if (Config->EMachine == EM_MIPS) { 1317 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols); 1318 } 1319 1320 size_t I = 0; 1321 for (const SymbolTableEntry &S : Symbols) 1322 S.Symbol->DynsymIndex = ++I; 1323 return; 1324 } 1325 1326 // If it is a .symtab, move all local symbols before global symbols. 1327 auto It = std::stable_partition( 1328 Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) { 1329 return S.Symbol->isLocal() || 1330 S.Symbol->symbol()->computeBinding() == STB_LOCAL; 1331 }); 1332 size_t NumLocals = It - Symbols.begin(); 1333 this->OutSec->Info = NumLocals + 1; 1334 } 1335 1336 template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) { 1337 // Adding a local symbol to a .dynsym is a bug. 1338 assert(this->Type != SHT_DYNSYM || !B->isLocal()); 1339 1340 bool HashIt = B->isLocal(); 1341 Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)}); 1342 } 1343 1344 template <class ELFT> 1345 size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) { 1346 auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) { 1347 if (E.Symbol == Body) 1348 return true; 1349 // This is used for -r, so we have to handle multiple section 1350 // symbols being combined. 1351 if (Body->Type == STT_SECTION && E.Symbol->Type == STT_SECTION) 1352 return cast<DefinedRegular>(Body)->Section->OutSec == 1353 cast<DefinedRegular>(E.Symbol)->Section->OutSec; 1354 return false; 1355 }); 1356 if (I == Symbols.end()) 1357 return 0; 1358 return I - Symbols.begin() + 1; 1359 } 1360 1361 // Write the internal symbol table contents to the output symbol table. 1362 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { 1363 // The first entry is a null entry as per the ELF spec. 1364 Buf += sizeof(Elf_Sym); 1365 1366 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); 1367 1368 for (SymbolTableEntry &Ent : Symbols) { 1369 SymbolBody *Body = Ent.Symbol; 1370 1371 // Set st_info and st_other. 1372 if (Body->isLocal()) { 1373 ESym->setBindingAndType(STB_LOCAL, Body->Type); 1374 } else { 1375 ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type); 1376 ESym->setVisibility(Body->symbol()->Visibility); 1377 } 1378 1379 ESym->st_name = Ent.StrTabOffset; 1380 ESym->st_size = Body->getSize<ELFT>(); 1381 1382 // Set a section index. 1383 if (const OutputSection *OutSec = Body->getOutputSection<ELFT>()) 1384 ESym->st_shndx = OutSec->SectionIndex; 1385 else if (isa<DefinedRegular>(Body)) 1386 ESym->st_shndx = SHN_ABS; 1387 else if (isa<DefinedCommon>(Body)) 1388 ESym->st_shndx = SHN_COMMON; 1389 1390 // st_value is usually an address of a symbol, but that has a 1391 // special meaining for uninstantiated common symbols (this can 1392 // occur if -r is given). 1393 if (!Config->DefineCommon && isa<DefinedCommon>(Body)) 1394 ESym->st_value = cast<DefinedCommon>(Body)->Alignment; 1395 else 1396 ESym->st_value = Body->getVA<ELFT>(); 1397 1398 ++ESym; 1399 } 1400 1401 // On MIPS we need to mark symbol which has a PLT entry and requires 1402 // pointer equality by STO_MIPS_PLT flag. That is necessary to help 1403 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. 1404 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt 1405 if (Config->EMachine == EM_MIPS) { 1406 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); 1407 1408 for (SymbolTableEntry &Ent : Symbols) { 1409 SymbolBody *Body = Ent.Symbol; 1410 if (Body->isInPlt() && Body->NeedsPltAddr) 1411 ESym->st_other |= STO_MIPS_PLT; 1412 1413 if (Config->Relocatable) 1414 if (auto *D = dyn_cast<DefinedRegular>(Body)) 1415 if (D->isMipsPIC<ELFT>()) 1416 ESym->st_other |= STO_MIPS_PIC; 1417 ++ESym; 1418 } 1419 } 1420 } 1421 1422 // .hash and .gnu.hash sections contain on-disk hash tables that map 1423 // symbol names to their dynamic symbol table indices. Their purpose 1424 // is to help the dynamic linker resolve symbols quickly. If ELF files 1425 // don't have them, the dynamic linker has to do linear search on all 1426 // dynamic symbols, which makes programs slower. Therefore, a .hash 1427 // section is added to a DSO by default. A .gnu.hash is added if you 1428 // give the -hash-style=gnu or -hash-style=both option. 1429 // 1430 // The Unix semantics of resolving dynamic symbols is somewhat expensive. 1431 // Each ELF file has a list of DSOs that the ELF file depends on and a 1432 // list of dynamic symbols that need to be resolved from any of the 1433 // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) 1434 // where m is the number of DSOs and n is the number of dynamic 1435 // symbols. For modern large programs, both m and n are large. So 1436 // making each step faster by using hash tables substiantially 1437 // improves time to load programs. 1438 // 1439 // (Note that this is not the only way to design the shared library. 1440 // For instance, the Windows DLL takes a different approach. On 1441 // Windows, each dynamic symbol has a name of DLL from which the symbol 1442 // has to be resolved. That makes the cost of symbol resolution O(n). 1443 // This disables some hacky techniques you can use on Unix such as 1444 // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) 1445 // 1446 // Due to historical reasons, we have two different hash tables, .hash 1447 // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new 1448 // and better version of .hash. .hash is just an on-disk hash table, but 1449 // .gnu.hash has a bloom filter in addition to a hash table to skip 1450 // DSOs very quickly. If you are sure that your dynamic linker knows 1451 // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a 1452 // safe bet is to specify -hash-style=both for backward compatibilty. 1453 template <class ELFT> 1454 GnuHashTableSection<ELFT>::GnuHashTableSection() 1455 : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t), ".gnu.hash") { 1456 this->Entsize = ELFT::Is64Bits ? 0 : 4; 1457 } 1458 1459 template <class ELFT> void GnuHashTableSection<ELFT>::finalizeContents() { 1460 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; 1461 1462 // Computes bloom filter size in word size. We want to allocate 8 1463 // bits for each symbol. It must be a power of two. 1464 if (Symbols.empty()) 1465 MaskWords = 1; 1466 else 1467 MaskWords = NextPowerOf2((Symbols.size() - 1) / sizeof(uintX_t)); 1468 1469 Size = 16; // Header 1470 Size += sizeof(uintX_t) * MaskWords; // Bloom filter 1471 Size += NBuckets * 4; // Hash buckets 1472 Size += Symbols.size() * 4; // Hash values 1473 } 1474 1475 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { 1476 // Write a header. 1477 const endianness E = ELFT::TargetEndianness; 1478 write32<E>(Buf, NBuckets); 1479 write32<E>(Buf + 4, In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size()); 1480 write32<E>(Buf + 8, MaskWords); 1481 write32<E>(Buf + 12, getShift2()); 1482 Buf += 16; 1483 1484 // Write a bloom filter and a hash table. 1485 writeBloomFilter(Buf); 1486 Buf += sizeof(uintX_t) * MaskWords; 1487 writeHashTable(Buf); 1488 } 1489 1490 // This function writes a 2-bit bloom filter. This bloom filter alone 1491 // usually filters out 80% or more of all symbol lookups [1]. 1492 // The dynamic linker uses the hash table only when a symbol is not 1493 // filtered out by a bloom filter. 1494 // 1495 // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2), 1496 // p.9, https://www.akkadia.org/drepper/dsohowto.pdf 1497 template <class ELFT> 1498 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *Buf) { 1499 typedef typename ELFT::Off Elf_Off; 1500 const unsigned C = sizeof(uintX_t) * 8; 1501 1502 auto *Filter = reinterpret_cast<Elf_Off *>(Buf); 1503 for (const Entry &Sym : Symbols) { 1504 size_t I = (Sym.Hash / C) & (MaskWords - 1); 1505 Filter[I] |= uintX_t(1) << (Sym.Hash % C); 1506 Filter[I] |= uintX_t(1) << ((Sym.Hash >> getShift2()) % C); 1507 } 1508 } 1509 1510 template <class ELFT> 1511 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { 1512 // A 32-bit integer type in the target endianness. 1513 typedef typename ELFT::Word Elf_Word; 1514 1515 // Group symbols by hash value. 1516 std::vector<std::vector<Entry>> Syms(NBuckets); 1517 for (const Entry &Ent : Symbols) 1518 Syms[Ent.Hash % NBuckets].push_back(Ent); 1519 1520 // Write hash buckets. Hash buckets contain indices in the following 1521 // hash value table. 1522 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); 1523 for (size_t I = 0; I < NBuckets; ++I) 1524 if (!Syms[I].empty()) 1525 Buckets[I] = Syms[I][0].Body->DynsymIndex; 1526 1527 // Write a hash value table. It represents a sequence of chains that 1528 // share the same hash modulo value. The last element of each chain 1529 // is terminated by LSB 1. 1530 Elf_Word *Values = Buckets + NBuckets; 1531 size_t I = 0; 1532 for (std::vector<Entry> &Vec : Syms) { 1533 if (Vec.empty()) 1534 continue; 1535 for (const Entry &Ent : makeArrayRef(Vec).drop_back()) 1536 Values[I++] = Ent.Hash & ~1; 1537 Values[I++] = Vec.back().Hash | 1; 1538 } 1539 } 1540 1541 static uint32_t hashGnu(StringRef Name) { 1542 uint32_t H = 5381; 1543 for (uint8_t C : Name) 1544 H = (H << 5) + H + C; 1545 return H; 1546 } 1547 1548 // Returns a number of hash buckets to accomodate given number of elements. 1549 // We want to choose a moderate number that is not too small (which 1550 // causes too many hash collisions) and not too large (which wastes 1551 // disk space.) 1552 // 1553 // We return a prime number because it (is believed to) achieve good 1554 // hash distribution. 1555 static size_t getBucketSize(size_t NumSymbols) { 1556 // List of largest prime numbers that are not greater than 2^n + 1. 1557 for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509, 1558 251, 127, 61, 31, 13, 7, 3, 1}) 1559 if (N <= NumSymbols) 1560 return N; 1561 return 0; 1562 } 1563 1564 // Add symbols to this symbol hash table. Note that this function 1565 // destructively sort a given vector -- which is needed because 1566 // GNU-style hash table places some sorting requirements. 1567 template <class ELFT> 1568 void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) { 1569 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce 1570 // its type correctly. 1571 std::vector<SymbolTableEntry>::iterator Mid = 1572 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { 1573 return S.Symbol->isUndefined(); 1574 }); 1575 if (Mid == V.end()) 1576 return; 1577 1578 for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) { 1579 SymbolBody *B = Ent.Symbol; 1580 Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())}); 1581 } 1582 1583 NBuckets = getBucketSize(Symbols.size()); 1584 std::stable_sort(Symbols.begin(), Symbols.end(), 1585 [&](const Entry &L, const Entry &R) { 1586 return L.Hash % NBuckets < R.Hash % NBuckets; 1587 }); 1588 1589 V.erase(Mid, V.end()); 1590 for (const Entry &Ent : Symbols) 1591 V.push_back({Ent.Body, Ent.StrTabOffset}); 1592 } 1593 1594 template <class ELFT> 1595 HashTableSection<ELFT>::HashTableSection() 1596 : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { 1597 this->Entsize = 4; 1598 } 1599 1600 template <class ELFT> void HashTableSection<ELFT>::finalizeContents() { 1601 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; 1602 1603 unsigned NumEntries = 2; // nbucket and nchain. 1604 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. 1605 1606 // Create as many buckets as there are symbols. 1607 // FIXME: This is simplistic. We can try to optimize it, but implementing 1608 // support for SHT_GNU_HASH is probably even more profitable. 1609 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); 1610 this->Size = NumEntries * 4; 1611 } 1612 1613 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { 1614 // A 32-bit integer type in the target endianness. 1615 typedef typename ELFT::Word Elf_Word; 1616 1617 unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols(); 1618 1619 auto *P = reinterpret_cast<Elf_Word *>(Buf); 1620 *P++ = NumSymbols; // nbucket 1621 *P++ = NumSymbols; // nchain 1622 1623 Elf_Word *Buckets = P; 1624 Elf_Word *Chains = P + NumSymbols; 1625 1626 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { 1627 SymbolBody *Body = S.Symbol; 1628 StringRef Name = Body->getName(); 1629 unsigned I = Body->DynsymIndex; 1630 uint32_t Hash = hashSysV(Name) % NumSymbols; 1631 Chains[I] = Buckets[Hash]; 1632 Buckets[Hash] = I; 1633 } 1634 } 1635 1636 template <class ELFT> 1637 PltSection<ELFT>::PltSection(size_t S) 1638 : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"), 1639 HeaderSize(S) {} 1640 1641 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { 1642 // At beginning of PLT but not the IPLT, we have code to call the dynamic 1643 // linker to resolve dynsyms at runtime. Write such code. 1644 if (HeaderSize != 0) 1645 Target->writePltHeader(Buf); 1646 size_t Off = HeaderSize; 1647 // The IPlt is immediately after the Plt, account for this in RelOff 1648 unsigned PltOff = getPltRelocOff(); 1649 1650 for (auto &I : Entries) { 1651 const SymbolBody *B = I.first; 1652 unsigned RelOff = I.second + PltOff; 1653 uint64_t Got = B->getGotPltVA<ELFT>(); 1654 uint64_t Plt = this->getVA() + Off; 1655 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); 1656 Off += Target->PltEntrySize; 1657 } 1658 } 1659 1660 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { 1661 Sym.PltIndex = Entries.size(); 1662 RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt; 1663 if (HeaderSize == 0) { 1664 PltRelocSection = In<ELFT>::RelaIplt; 1665 Sym.IsInIplt = true; 1666 } 1667 unsigned RelOff = PltRelocSection->getRelocOffset(); 1668 Entries.push_back(std::make_pair(&Sym, RelOff)); 1669 } 1670 1671 template <class ELFT> size_t PltSection<ELFT>::getSize() const { 1672 return HeaderSize + Entries.size() * Target->PltEntrySize; 1673 } 1674 1675 // Some architectures such as additional symbols in the PLT section. For 1676 // example ARM uses mapping symbols to aid disassembly 1677 template <class ELFT> void PltSection<ELFT>::addSymbols() { 1678 // The PLT may have symbols defined for the Header, the IPLT has no header 1679 if (HeaderSize != 0) 1680 Target->addPltHeaderSymbols(this); 1681 size_t Off = HeaderSize; 1682 for (size_t I = 0; I < Entries.size(); ++I) { 1683 Target->addPltSymbols(this, Off); 1684 Off += Target->PltEntrySize; 1685 } 1686 } 1687 1688 template <class ELFT> unsigned PltSection<ELFT>::getPltRelocOff() const { 1689 return (HeaderSize == 0) ? In<ELFT>::Plt->getSize() : 0; 1690 } 1691 1692 template <class ELFT> 1693 GdbIndexSection<ELFT>::GdbIndexSection() 1694 : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), 1695 StringPool(llvm::StringTableBuilder::ELF) {} 1696 1697 // Iterative hash function for symbol's name is described in .gdb_index format 1698 // specification. Note that we use one for version 5 to 7 here, it is different 1699 // for version 4. 1700 static uint32_t hash(StringRef Str) { 1701 uint32_t R = 0; 1702 for (uint8_t C : Str) 1703 R = R * 67 + tolower(C) - 113; 1704 return R; 1705 } 1706 1707 static std::vector<std::pair<uint64_t, uint64_t>> 1708 readCuList(DWARFContext &Dwarf, InputSection *Sec) { 1709 std::vector<std::pair<uint64_t, uint64_t>> Ret; 1710 for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) 1711 Ret.push_back({Sec->OutSecOff + CU->getOffset(), CU->getLength() + 4}); 1712 return Ret; 1713 } 1714 1715 template <class ELFT> 1716 static InputSectionBase *findSection(ArrayRef<InputSectionBase *> Arr, 1717 uint64_t Offset) { 1718 for (InputSectionBase *S : Arr) 1719 if (S && S != &InputSection::Discarded) 1720 if (Offset >= S->Offset && Offset < S->Offset + S->getSize<ELFT>()) 1721 return S; 1722 return nullptr; 1723 } 1724 1725 template <class ELFT> 1726 static std::vector<AddressEntry> 1727 readAddressArea(DWARFContext &Dwarf, InputSection *Sec, size_t CurrentCU) { 1728 std::vector<AddressEntry> Ret; 1729 1730 for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) { 1731 DWARFAddressRangesVector Ranges; 1732 CU->collectAddressRanges(Ranges); 1733 1734 ArrayRef<InputSectionBase *> Sections = 1735 Sec->template getFile<ELFT>()->getSections(); 1736 1737 for (std::pair<uint64_t, uint64_t> &R : Ranges) 1738 if (InputSectionBase *S = findSection<ELFT>(Sections, R.first)) 1739 Ret.push_back( 1740 {S, R.first - S->Offset, R.second - S->Offset, CurrentCU}); 1741 ++CurrentCU; 1742 } 1743 return Ret; 1744 } 1745 1746 static std::vector<std::pair<StringRef, uint8_t>> 1747 readPubNamesAndTypes(DWARFContext &Dwarf, bool IsLE) { 1748 StringRef Data[] = {Dwarf.getGnuPubNamesSection(), 1749 Dwarf.getGnuPubTypesSection()}; 1750 1751 std::vector<std::pair<StringRef, uint8_t>> Ret; 1752 for (StringRef D : Data) { 1753 DWARFDebugPubTable PubTable(D, IsLE, true); 1754 for (const DWARFDebugPubTable::Set &Set : PubTable.getData()) 1755 for (const DWARFDebugPubTable::Entry &Ent : Set.Entries) 1756 Ret.push_back({Ent.Name, Ent.Descriptor.toBits()}); 1757 } 1758 return Ret; 1759 } 1760 1761 class ObjInfoTy : public llvm::LoadedObjectInfo { 1762 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override { 1763 auto &S = static_cast<const object::ELFSectionRef &>(Sec); 1764 if (S.getFlags() & ELF::SHF_ALLOC) 1765 return S.getOffset(); 1766 return 0; 1767 } 1768 1769 std::unique_ptr<llvm::LoadedObjectInfo> clone() const override { return {}; } 1770 }; 1771 1772 template <class ELFT> void GdbIndexSection<ELFT>::readDwarf(InputSection *Sec) { 1773 elf::ObjectFile<ELFT> *File = Sec->template getFile<ELFT>(); 1774 1775 Expected<std::unique_ptr<object::ObjectFile>> Obj = 1776 object::ObjectFile::createObjectFile(File->MB); 1777 if (!Obj) { 1778 error(toString(File) + ": error creating DWARF context"); 1779 return; 1780 } 1781 1782 ObjInfoTy ObjInfo; 1783 DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo); 1784 1785 size_t CuId = CompilationUnits.size(); 1786 for (std::pair<uint64_t, uint64_t> &P : readCuList(Dwarf, Sec)) 1787 CompilationUnits.push_back(P); 1788 1789 for (AddressEntry &Ent : readAddressArea<ELFT>(Dwarf, Sec, CuId)) 1790 AddressArea.push_back(Ent); 1791 1792 std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes = 1793 readPubNamesAndTypes(Dwarf, ELFT::TargetEndianness == support::little); 1794 1795 for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) { 1796 uint32_t Hash = hash(Pair.first); 1797 size_t Offset = StringPool.add(Pair.first); 1798 1799 bool IsNew; 1800 GdbSymbol *Sym; 1801 std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset); 1802 if (IsNew) { 1803 Sym->CuVectorIndex = CuVectors.size(); 1804 CuVectors.push_back({{CuId, Pair.second}}); 1805 continue; 1806 } 1807 1808 CuVectors[Sym->CuVectorIndex].push_back({CuId, Pair.second}); 1809 } 1810 } 1811 1812 template <class ELFT> void GdbIndexSection<ELFT>::finalizeContents() { 1813 if (Finalized) 1814 return; 1815 Finalized = true; 1816 1817 for (InputSectionBase *S : InputSections) 1818 if (InputSection *IS = dyn_cast<InputSection>(S)) 1819 if (IS->OutSec && IS->Name == ".debug_info") 1820 readDwarf(IS); 1821 1822 SymbolTable.finalizeContents(); 1823 1824 // GdbIndex header consist from version fields 1825 // and 5 more fields with different kinds of offsets. 1826 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; 1827 SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize; 1828 1829 ConstantPoolOffset = 1830 SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize; 1831 1832 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { 1833 CuVectorsOffset.push_back(CuVectorsSize); 1834 CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1); 1835 } 1836 StringPoolOffset = ConstantPoolOffset + CuVectorsSize; 1837 1838 StringPool.finalizeInOrder(); 1839 } 1840 1841 template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const { 1842 const_cast<GdbIndexSection<ELFT> *>(this)->finalizeContents(); 1843 return StringPoolOffset + StringPool.getSize(); 1844 } 1845 1846 template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) { 1847 write32le(Buf, 7); // Write version. 1848 write32le(Buf + 4, CuListOffset); // CU list offset. 1849 write32le(Buf + 8, CuTypesOffset); // Types CU list offset. 1850 write32le(Buf + 12, CuTypesOffset); // Address area offset. 1851 write32le(Buf + 16, SymTabOffset); // Symbol table offset. 1852 write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset. 1853 Buf += 24; 1854 1855 // Write the CU list. 1856 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) { 1857 write64le(Buf, CU.first); 1858 write64le(Buf + 8, CU.second); 1859 Buf += 16; 1860 } 1861 1862 // Write the address area. 1863 for (AddressEntry &E : AddressArea) { 1864 uintX_t BaseAddr = 1865 E.Section->OutSec->Addr + E.Section->template getOffset<ELFT>(0); 1866 write64le(Buf, BaseAddr + E.LowAddress); 1867 write64le(Buf + 8, BaseAddr + E.HighAddress); 1868 write32le(Buf + 16, E.CuIndex); 1869 Buf += 20; 1870 } 1871 1872 // Write the symbol table. 1873 for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) { 1874 GdbSymbol *Sym = SymbolTable.getSymbol(I); 1875 if (Sym) { 1876 size_t NameOffset = 1877 Sym->NameOffset + StringPoolOffset - ConstantPoolOffset; 1878 size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex]; 1879 write32le(Buf, NameOffset); 1880 write32le(Buf + 4, CuVectorOffset); 1881 } 1882 Buf += 8; 1883 } 1884 1885 // Write the CU vectors into the constant pool. 1886 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { 1887 write32le(Buf, CuVec.size()); 1888 Buf += 4; 1889 for (std::pair<uint32_t, uint8_t> &P : CuVec) { 1890 uint32_t Index = P.first; 1891 uint8_t Flags = P.second; 1892 Index |= Flags << 24; 1893 write32le(Buf, Index); 1894 Buf += 4; 1895 } 1896 } 1897 1898 StringPool.write(Buf); 1899 } 1900 1901 template <class ELFT> bool GdbIndexSection<ELFT>::empty() const { 1902 return !Out::DebugInfo; 1903 } 1904 1905 template <class ELFT> 1906 EhFrameHeader<ELFT>::EhFrameHeader() 1907 : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {} 1908 1909 // .eh_frame_hdr contains a binary search table of pointers to FDEs. 1910 // Each entry of the search table consists of two values, 1911 // the starting PC from where FDEs covers, and the FDE's address. 1912 // It is sorted by PC. 1913 template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { 1914 const endianness E = ELFT::TargetEndianness; 1915 1916 // Sort the FDE list by their PC and uniqueify. Usually there is only 1917 // one FDE for a PC (i.e. function), but if ICF merges two functions 1918 // into one, there can be more than one FDEs pointing to the address. 1919 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; 1920 std::stable_sort(Fdes.begin(), Fdes.end(), Less); 1921 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; 1922 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); 1923 1924 Buf[0] = 1; 1925 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; 1926 Buf[2] = DW_EH_PE_udata4; 1927 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; 1928 write32<E>(Buf + 4, In<ELFT>::EhFrame->OutSec->Addr - this->getVA() - 4); 1929 write32<E>(Buf + 8, Fdes.size()); 1930 Buf += 12; 1931 1932 uintX_t VA = this->getVA(); 1933 for (FdeData &Fde : Fdes) { 1934 write32<E>(Buf, Fde.Pc - VA); 1935 write32<E>(Buf + 4, Fde.FdeVA - VA); 1936 Buf += 8; 1937 } 1938 } 1939 1940 template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const { 1941 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. 1942 return 12 + In<ELFT>::EhFrame->NumFdes * 8; 1943 } 1944 1945 template <class ELFT> 1946 void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { 1947 Fdes.push_back({Pc, FdeVA}); 1948 } 1949 1950 template <class ELFT> bool EhFrameHeader<ELFT>::empty() const { 1951 return In<ELFT>::EhFrame->empty(); 1952 } 1953 1954 template <class ELFT> 1955 VersionDefinitionSection<ELFT>::VersionDefinitionSection() 1956 : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), 1957 ".gnu.version_d") {} 1958 1959 static StringRef getFileDefName() { 1960 if (!Config->SoName.empty()) 1961 return Config->SoName; 1962 return Config->OutputFile; 1963 } 1964 1965 template <class ELFT> void VersionDefinitionSection<ELFT>::finalizeContents() { 1966 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName()); 1967 for (VersionDefinition &V : Config->VersionDefinitions) 1968 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name); 1969 1970 this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; 1971 1972 // sh_info should be set to the number of definitions. This fact is missed in 1973 // documentation, but confirmed by binutils community: 1974 // https://sourceware.org/ml/binutils/2014-11/msg00355.html 1975 this->OutSec->Info = getVerDefNum(); 1976 } 1977 1978 template <class ELFT> 1979 void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, 1980 StringRef Name, size_t NameOff) { 1981 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); 1982 Verdef->vd_version = 1; 1983 Verdef->vd_cnt = 1; 1984 Verdef->vd_aux = sizeof(Elf_Verdef); 1985 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); 1986 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); 1987 Verdef->vd_ndx = Index; 1988 Verdef->vd_hash = hashSysV(Name); 1989 1990 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); 1991 Verdaux->vda_name = NameOff; 1992 Verdaux->vda_next = 0; 1993 } 1994 1995 template <class ELFT> 1996 void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { 1997 writeOne(Buf, 1, getFileDefName(), FileDefNameOff); 1998 1999 for (VersionDefinition &V : Config->VersionDefinitions) { 2000 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); 2001 writeOne(Buf, V.Id, V.Name, V.NameOff); 2002 } 2003 2004 // Need to terminate the last version definition. 2005 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); 2006 Verdef->vd_next = 0; 2007 } 2008 2009 template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const { 2010 return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); 2011 } 2012 2013 template <class ELFT> 2014 VersionTableSection<ELFT>::VersionTableSection() 2015 : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), 2016 ".gnu.version") { 2017 this->Entsize = sizeof(Elf_Versym); 2018 } 2019 2020 template <class ELFT> void VersionTableSection<ELFT>::finalizeContents() { 2021 // At the moment of june 2016 GNU docs does not mention that sh_link field 2022 // should be set, but Sun docs do. Also readelf relies on this field. 2023 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; 2024 } 2025 2026 template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const { 2027 return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1); 2028 } 2029 2030 template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { 2031 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; 2032 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { 2033 OutVersym->vs_index = S.Symbol->symbol()->VersionId; 2034 ++OutVersym; 2035 } 2036 } 2037 2038 template <class ELFT> bool VersionTableSection<ELFT>::empty() const { 2039 return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty(); 2040 } 2041 2042 template <class ELFT> 2043 VersionNeedSection<ELFT>::VersionNeedSection() 2044 : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), 2045 ".gnu.version_r") { 2046 // Identifiers in verneed section start at 2 because 0 and 1 are reserved 2047 // for VER_NDX_LOCAL and VER_NDX_GLOBAL. 2048 // First identifiers are reserved by verdef section if it exist. 2049 NextIndex = getVerDefNum() + 1; 2050 } 2051 2052 template <class ELFT> 2053 void VersionNeedSection<ELFT>::addSymbol(SharedSymbol *SS) { 2054 auto *Ver = reinterpret_cast<const typename ELFT::Verdef *>(SS->Verdef); 2055 if (!Ver) { 2056 SS->symbol()->VersionId = VER_NDX_GLOBAL; 2057 return; 2058 } 2059 2060 auto *File = cast<SharedFile<ELFT>>(SS->File); 2061 2062 // If we don't already know that we need an Elf_Verneed for this DSO, prepare 2063 // to create one by adding it to our needed list and creating a dynstr entry 2064 // for the soname. 2065 if (File->VerdefMap.empty()) 2066 Needed.push_back({File, In<ELFT>::DynStrTab->addString(File->getSoName())}); 2067 typename SharedFile<ELFT>::NeededVer &NV = File->VerdefMap[Ver]; 2068 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, 2069 // prepare to create one by allocating a version identifier and creating a 2070 // dynstr entry for the version name. 2071 if (NV.Index == 0) { 2072 NV.StrTab = In<ELFT>::DynStrTab->addString(File->getStringTable().data() + 2073 Ver->getAux()->vda_name); 2074 NV.Index = NextIndex++; 2075 } 2076 SS->symbol()->VersionId = NV.Index; 2077 } 2078 2079 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { 2080 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. 2081 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); 2082 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); 2083 2084 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { 2085 // Create an Elf_Verneed for this DSO. 2086 Verneed->vn_version = 1; 2087 Verneed->vn_cnt = P.first->VerdefMap.size(); 2088 Verneed->vn_file = P.second; 2089 Verneed->vn_aux = 2090 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); 2091 Verneed->vn_next = sizeof(Elf_Verneed); 2092 ++Verneed; 2093 2094 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over 2095 // VerdefMap, which will only contain references to needed version 2096 // definitions. Each Elf_Vernaux is based on the information contained in 2097 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of 2098 // pointers, but is deterministic because the pointers refer to Elf_Verdef 2099 // data structures within a single input file. 2100 for (auto &NV : P.first->VerdefMap) { 2101 Vernaux->vna_hash = NV.first->vd_hash; 2102 Vernaux->vna_flags = 0; 2103 Vernaux->vna_other = NV.second.Index; 2104 Vernaux->vna_name = NV.second.StrTab; 2105 Vernaux->vna_next = sizeof(Elf_Vernaux); 2106 ++Vernaux; 2107 } 2108 2109 Vernaux[-1].vna_next = 0; 2110 } 2111 Verneed[-1].vn_next = 0; 2112 } 2113 2114 template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { 2115 this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; 2116 this->OutSec->Info = Needed.size(); 2117 } 2118 2119 template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { 2120 unsigned Size = Needed.size() * sizeof(Elf_Verneed); 2121 for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed) 2122 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); 2123 return Size; 2124 } 2125 2126 template <class ELFT> bool VersionNeedSection<ELFT>::empty() const { 2127 return getNeedNum() == 0; 2128 } 2129 2130 template <class ELFT> 2131 MergeSyntheticSection<ELFT>::MergeSyntheticSection(StringRef Name, 2132 uint32_t Type, uintX_t Flags, 2133 uintX_t Alignment) 2134 : SyntheticSection(Flags, Type, Alignment, Name), 2135 Builder(StringTableBuilder::RAW, Alignment) {} 2136 2137 template <class ELFT> 2138 void MergeSyntheticSection<ELFT>::addSection(MergeInputSection<ELFT> *MS) { 2139 assert(!Finalized); 2140 MS->MergeSec = this; 2141 Sections.push_back(MS); 2142 } 2143 2144 template <class ELFT> void MergeSyntheticSection<ELFT>::writeTo(uint8_t *Buf) { 2145 Builder.write(Buf); 2146 } 2147 2148 template <class ELFT> 2149 bool MergeSyntheticSection<ELFT>::shouldTailMerge() const { 2150 return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; 2151 } 2152 2153 template <class ELFT> void MergeSyntheticSection<ELFT>::finalizeTailMerge() { 2154 // Add all string pieces to the string table builder to create section 2155 // contents. 2156 for (MergeInputSection<ELFT> *Sec : Sections) 2157 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) 2158 if (Sec->Pieces[I].Live) 2159 Builder.add(Sec->getData(I)); 2160 2161 // Fix the string table content. After this, the contents will never change. 2162 Builder.finalize(); 2163 2164 // finalize() fixed tail-optimized strings, so we can now get 2165 // offsets of strings. Get an offset for each string and save it 2166 // to a corresponding StringPiece for easy access. 2167 for (MergeInputSection<ELFT> *Sec : Sections) 2168 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) 2169 if (Sec->Pieces[I].Live) 2170 Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); 2171 } 2172 2173 template <class ELFT> void MergeSyntheticSection<ELFT>::finalizeNoTailMerge() { 2174 // Add all string pieces to the string table builder to create section 2175 // contents. Because we are not tail-optimizing, offsets of strings are 2176 // fixed when they are added to the builder (string table builder contains 2177 // a hash table from strings to offsets). 2178 for (MergeInputSection<ELFT> *Sec : Sections) 2179 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) 2180 if (Sec->Pieces[I].Live) 2181 Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I)); 2182 2183 Builder.finalizeInOrder(); 2184 } 2185 2186 template <class ELFT> void MergeSyntheticSection<ELFT>::finalizeContents() { 2187 if (Finalized) 2188 return; 2189 Finalized = true; 2190 if (shouldTailMerge()) 2191 finalizeTailMerge(); 2192 else 2193 finalizeNoTailMerge(); 2194 } 2195 2196 template <class ELFT> size_t MergeSyntheticSection<ELFT>::getSize() const { 2197 // We should finalize string builder to know the size. 2198 const_cast<MergeSyntheticSection<ELFT> *>(this)->finalizeContents(); 2199 return Builder.getSize(); 2200 } 2201 2202 template <class ELFT> 2203 MipsRldMapSection<ELFT>::MipsRldMapSection() 2204 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 2205 sizeof(typename ELFT::uint), ".rld_map") {} 2206 2207 template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) { 2208 // Apply filler from linker script. 2209 uint64_t Filler = Script<ELFT>::X->getFiller(this->Name); 2210 Filler = (Filler << 32) | Filler; 2211 memcpy(Buf, &Filler, getSize()); 2212 } 2213 2214 template <class ELFT> 2215 ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection() 2216 : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, 2217 sizeof(typename ELFT::uint), ".ARM.exidx") {} 2218 2219 // Write a terminating sentinel entry to the end of the .ARM.exidx table. 2220 // This section will have been sorted last in the .ARM.exidx table. 2221 // This table entry will have the form: 2222 // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND | 2223 template <class ELFT> 2224 void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) { 2225 // Get the InputSection before us, we are by definition last 2226 auto RI = cast<OutputSection>(this->OutSec)->Sections.rbegin(); 2227 InputSection *LE = *(++RI); 2228 InputSection *LC = cast<InputSection>(LE->template getLinkOrderDep<ELFT>()); 2229 uint64_t S = LC->OutSec->Addr + 2230 LC->template getOffset<ELFT>(LC->template getSize<ELFT>()); 2231 uint64_t P = this->getVA(); 2232 Target->relocateOne(Buf, R_ARM_PREL31, S - P); 2233 write32le(Buf + 4, 0x1); 2234 } 2235 2236 template <class ELFT> 2237 ThunkSection<ELFT>::ThunkSection(OutputSection *OS, uint64_t Off) 2238 : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 2239 sizeof(typename ELFT::uint), ".text.thunk") { 2240 this->OutSec = OS; 2241 this->OutSecOff = Off; 2242 } 2243 2244 template <class ELFT> void ThunkSection<ELFT>::addThunk(Thunk<ELFT> *T) { 2245 uint64_t Off = alignTo(Size, T->alignment); 2246 T->Offset = Off; 2247 Thunks.push_back(T); 2248 T->addSymbols(*this); 2249 Size = Off + T->size(); 2250 } 2251 2252 template <class ELFT> void ThunkSection<ELFT>::writeTo(uint8_t *Buf) { 2253 for (const Thunk<ELFT> *T : Thunks) 2254 T->writeTo(Buf + T->Offset, *this); 2255 } 2256 2257 template <class ELFT> 2258 InputSection *ThunkSection<ELFT>::getTargetInputSection() const { 2259 const Thunk<ELFT> *T = Thunks.front(); 2260 return T->getTargetInputSection(); 2261 } 2262 2263 template InputSection *elf::createCommonSection<ELF32LE>(); 2264 template InputSection *elf::createCommonSection<ELF32BE>(); 2265 template InputSection *elf::createCommonSection<ELF64LE>(); 2266 template InputSection *elf::createCommonSection<ELF64BE>(); 2267 2268 template MergeInputSection<ELF32LE> *elf::createCommentSection(); 2269 template MergeInputSection<ELF32BE> *elf::createCommentSection(); 2270 template MergeInputSection<ELF64LE> *elf::createCommentSection(); 2271 template MergeInputSection<ELF64BE> *elf::createCommentSection(); 2272 2273 template SymbolBody *elf::addSyntheticLocal<ELF32LE>(StringRef, uint8_t, 2274 uint64_t, uint64_t, 2275 InputSectionBase *); 2276 template SymbolBody *elf::addSyntheticLocal<ELF32BE>(StringRef, uint8_t, 2277 uint64_t, uint64_t, 2278 InputSectionBase *); 2279 template SymbolBody *elf::addSyntheticLocal<ELF64LE>(StringRef, uint8_t, 2280 uint64_t, uint64_t, 2281 InputSectionBase *); 2282 template SymbolBody *elf::addSyntheticLocal<ELF64BE>(StringRef, uint8_t, 2283 uint64_t, uint64_t, 2284 InputSectionBase *); 2285 2286 template class elf::MipsAbiFlagsSection<ELF32LE>; 2287 template class elf::MipsAbiFlagsSection<ELF32BE>; 2288 template class elf::MipsAbiFlagsSection<ELF64LE>; 2289 template class elf::MipsAbiFlagsSection<ELF64BE>; 2290 2291 template class elf::MipsOptionsSection<ELF32LE>; 2292 template class elf::MipsOptionsSection<ELF32BE>; 2293 template class elf::MipsOptionsSection<ELF64LE>; 2294 template class elf::MipsOptionsSection<ELF64BE>; 2295 2296 template class elf::MipsReginfoSection<ELF32LE>; 2297 template class elf::MipsReginfoSection<ELF32BE>; 2298 template class elf::MipsReginfoSection<ELF64LE>; 2299 template class elf::MipsReginfoSection<ELF64BE>; 2300 2301 template class elf::BuildIdSection<ELF32LE>; 2302 template class elf::BuildIdSection<ELF32BE>; 2303 template class elf::BuildIdSection<ELF64LE>; 2304 template class elf::BuildIdSection<ELF64BE>; 2305 2306 template class elf::CopyRelSection<ELF32LE>; 2307 template class elf::CopyRelSection<ELF32BE>; 2308 template class elf::CopyRelSection<ELF64LE>; 2309 template class elf::CopyRelSection<ELF64BE>; 2310 2311 template class elf::GotSection<ELF32LE>; 2312 template class elf::GotSection<ELF32BE>; 2313 template class elf::GotSection<ELF64LE>; 2314 template class elf::GotSection<ELF64BE>; 2315 2316 template class elf::MipsGotSection<ELF32LE>; 2317 template class elf::MipsGotSection<ELF32BE>; 2318 template class elf::MipsGotSection<ELF64LE>; 2319 template class elf::MipsGotSection<ELF64BE>; 2320 2321 template class elf::GotPltSection<ELF32LE>; 2322 template class elf::GotPltSection<ELF32BE>; 2323 template class elf::GotPltSection<ELF64LE>; 2324 template class elf::GotPltSection<ELF64BE>; 2325 2326 template class elf::IgotPltSection<ELF32LE>; 2327 template class elf::IgotPltSection<ELF32BE>; 2328 template class elf::IgotPltSection<ELF64LE>; 2329 template class elf::IgotPltSection<ELF64BE>; 2330 2331 template class elf::StringTableSection<ELF32LE>; 2332 template class elf::StringTableSection<ELF32BE>; 2333 template class elf::StringTableSection<ELF64LE>; 2334 template class elf::StringTableSection<ELF64BE>; 2335 2336 template class elf::DynamicSection<ELF32LE>; 2337 template class elf::DynamicSection<ELF32BE>; 2338 template class elf::DynamicSection<ELF64LE>; 2339 template class elf::DynamicSection<ELF64BE>; 2340 2341 template class elf::RelocationSection<ELF32LE>; 2342 template class elf::RelocationSection<ELF32BE>; 2343 template class elf::RelocationSection<ELF64LE>; 2344 template class elf::RelocationSection<ELF64BE>; 2345 2346 template class elf::SymbolTableSection<ELF32LE>; 2347 template class elf::SymbolTableSection<ELF32BE>; 2348 template class elf::SymbolTableSection<ELF64LE>; 2349 template class elf::SymbolTableSection<ELF64BE>; 2350 2351 template class elf::GnuHashTableSection<ELF32LE>; 2352 template class elf::GnuHashTableSection<ELF32BE>; 2353 template class elf::GnuHashTableSection<ELF64LE>; 2354 template class elf::GnuHashTableSection<ELF64BE>; 2355 2356 template class elf::HashTableSection<ELF32LE>; 2357 template class elf::HashTableSection<ELF32BE>; 2358 template class elf::HashTableSection<ELF64LE>; 2359 template class elf::HashTableSection<ELF64BE>; 2360 2361 template class elf::PltSection<ELF32LE>; 2362 template class elf::PltSection<ELF32BE>; 2363 template class elf::PltSection<ELF64LE>; 2364 template class elf::PltSection<ELF64BE>; 2365 2366 template class elf::GdbIndexSection<ELF32LE>; 2367 template class elf::GdbIndexSection<ELF32BE>; 2368 template class elf::GdbIndexSection<ELF64LE>; 2369 template class elf::GdbIndexSection<ELF64BE>; 2370 2371 template class elf::EhFrameHeader<ELF32LE>; 2372 template class elf::EhFrameHeader<ELF32BE>; 2373 template class elf::EhFrameHeader<ELF64LE>; 2374 template class elf::EhFrameHeader<ELF64BE>; 2375 2376 template class elf::VersionTableSection<ELF32LE>; 2377 template class elf::VersionTableSection<ELF32BE>; 2378 template class elf::VersionTableSection<ELF64LE>; 2379 template class elf::VersionTableSection<ELF64BE>; 2380 2381 template class elf::VersionNeedSection<ELF32LE>; 2382 template class elf::VersionNeedSection<ELF32BE>; 2383 template class elf::VersionNeedSection<ELF64LE>; 2384 template class elf::VersionNeedSection<ELF64BE>; 2385 2386 template class elf::VersionDefinitionSection<ELF32LE>; 2387 template class elf::VersionDefinitionSection<ELF32BE>; 2388 template class elf::VersionDefinitionSection<ELF64LE>; 2389 template class elf::VersionDefinitionSection<ELF64BE>; 2390 2391 template class elf::MergeSyntheticSection<ELF32LE>; 2392 template class elf::MergeSyntheticSection<ELF32BE>; 2393 template class elf::MergeSyntheticSection<ELF64LE>; 2394 template class elf::MergeSyntheticSection<ELF64BE>; 2395 2396 template class elf::MipsRldMapSection<ELF32LE>; 2397 template class elf::MipsRldMapSection<ELF32BE>; 2398 template class elf::MipsRldMapSection<ELF64LE>; 2399 template class elf::MipsRldMapSection<ELF64BE>; 2400 2401 template class elf::ARMExidxSentinelSection<ELF32LE>; 2402 template class elf::ARMExidxSentinelSection<ELF32BE>; 2403 template class elf::ARMExidxSentinelSection<ELF64LE>; 2404 template class elf::ARMExidxSentinelSection<ELF64BE>; 2405 2406 template class elf::ThunkSection<ELF32LE>; 2407 template class elf::ThunkSection<ELF32BE>; 2408 template class elf::ThunkSection<ELF64LE>; 2409 template class elf::ThunkSection<ELF64BE>; 2410 2411 template class elf::EhFrameSection<ELF32LE>; 2412 template class elf::EhFrameSection<ELF32BE>; 2413 template class elf::EhFrameSection<ELF64LE>; 2414 template class elf::EhFrameSection<ELF64BE>; 2415