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