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