1 //===- OutputSections.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 #include "OutputSections.h" 11 #include "Config.h" 12 #include "EhFrame.h" 13 #include "LinkerScript.h" 14 #include "Strings.h" 15 #include "SymbolTable.h" 16 #include "Target.h" 17 #include "lld/Core/Parallel.h" 18 #include "llvm/Support/Dwarf.h" 19 #include "llvm/Support/MD5.h" 20 #include "llvm/Support/MathExtras.h" 21 #include "llvm/Support/SHA1.h" 22 #include <map> 23 24 using namespace llvm; 25 using namespace llvm::dwarf; 26 using namespace llvm::object; 27 using namespace llvm::support::endian; 28 using namespace llvm::ELF; 29 30 using namespace lld; 31 using namespace lld::elf; 32 33 template <class ELFT> 34 OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type, 35 uintX_t Flags) 36 : Name(Name) { 37 memset(&Header, 0, sizeof(Elf_Shdr)); 38 Header.sh_type = Type; 39 Header.sh_flags = Flags; 40 } 41 42 template <class ELFT> 43 void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *Shdr) { 44 *Shdr = Header; 45 } 46 47 template <class ELFT> 48 GotPltSection<ELFT>::GotPltSection() 49 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { 50 this->Header.sh_addralign = sizeof(uintX_t); 51 } 52 53 template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) { 54 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size(); 55 Entries.push_back(&Sym); 56 } 57 58 template <class ELFT> bool GotPltSection<ELFT>::empty() const { 59 return Entries.empty(); 60 } 61 62 template <class ELFT> void GotPltSection<ELFT>::finalize() { 63 this->Header.sh_size = 64 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t); 65 } 66 67 template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { 68 Target->writeGotPltHeader(Buf); 69 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t); 70 for (const SymbolBody *B : Entries) { 71 Target->writeGotPlt(Buf, *B); 72 Buf += sizeof(uintX_t); 73 } 74 } 75 76 template <class ELFT> 77 GotSection<ELFT>::GotSection() 78 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { 79 if (Config->EMachine == EM_MIPS) 80 this->Header.sh_flags |= SHF_MIPS_GPREL; 81 this->Header.sh_addralign = sizeof(uintX_t); 82 } 83 84 template <class ELFT> 85 void GotSection<ELFT>::addEntry(SymbolBody &Sym) { 86 Sym.GotIndex = Entries.size(); 87 Entries.push_back(&Sym); 88 } 89 90 template <class ELFT> 91 void GotSection<ELFT>::addMipsEntry(SymbolBody &Sym, uintX_t Addend, 92 RelExpr Expr) { 93 // For "true" local symbols which can be referenced from the same module 94 // only compiler creates two instructions for address loading: 95 // 96 // lw $8, 0($gp) # R_MIPS_GOT16 97 // addi $8, $8, 0 # R_MIPS_LO16 98 // 99 // The first instruction loads high 16 bits of the symbol address while 100 // the second adds an offset. That allows to reduce number of required 101 // GOT entries because only one global offset table entry is necessary 102 // for every 64 KBytes of local data. So for local symbols we need to 103 // allocate number of GOT entries to hold all required "page" addresses. 104 // 105 // All global symbols (hidden and regular) considered by compiler uniformly. 106 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation 107 // to load address of the symbol. So for each such symbol we need to 108 // allocate dedicated GOT entry to store its address. 109 // 110 // If a symbol is preemptible we need help of dynamic linker to get its 111 // final address. The corresponding GOT entries are allocated in the 112 // "global" part of GOT. Entries for non preemptible global symbol allocated 113 // in the "local" part of GOT. 114 // 115 // See "Global Offset Table" in Chapter 5: 116 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 117 if (Expr == R_MIPS_GOT_LOCAL_PAGE) { 118 // At this point we do not know final symbol value so to reduce number 119 // of allocated GOT entries do the following trick. Save all output 120 // sections referenced by GOT relocations. Then later in the `finalize` 121 // method calculate number of "pages" required to cover all saved output 122 // section and allocate appropriate number of GOT entries. 123 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec; 124 MipsOutSections.insert(OutSec); 125 return; 126 } 127 if (Sym.isTls()) { 128 // GOT entries created for MIPS TLS relocations behave like 129 // almost GOT entries from other ABIs. They go to the end 130 // of the global offset table. 131 Sym.GotIndex = Entries.size(); 132 Entries.push_back(&Sym); 133 return; 134 } 135 auto AddEntry = [&](SymbolBody &S, uintX_t A, MipsGotEntries &Items) { 136 if (S.isInGot() && !A) 137 return; 138 size_t NewIndex = Items.size(); 139 if (!MipsGotMap.insert({{&S, A}, NewIndex}).second) 140 return; 141 Items.emplace_back(&S, A); 142 if (!A) 143 S.GotIndex = NewIndex; 144 }; 145 if (Sym.isPreemptible()) { 146 // Ignore addends for preemptible symbols. They got single GOT entry anyway. 147 AddEntry(Sym, 0, MipsGlobal); 148 Sym.IsInGlobalMipsGot = true; 149 } else 150 AddEntry(Sym, Addend, MipsLocal); 151 } 152 153 template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { 154 if (Sym.GlobalDynIndex != -1U) 155 return false; 156 Sym.GlobalDynIndex = Entries.size(); 157 // Global Dynamic TLS entries take two GOT slots. 158 Entries.push_back(nullptr); 159 Entries.push_back(&Sym); 160 return true; 161 } 162 163 // Reserves TLS entries for a TLS module ID and a TLS block offset. 164 // In total it takes two GOT slots. 165 template <class ELFT> bool GotSection<ELFT>::addTlsIndex() { 166 if (TlsIndexOff != uint32_t(-1)) 167 return false; 168 TlsIndexOff = Entries.size() * sizeof(uintX_t); 169 Entries.push_back(nullptr); 170 Entries.push_back(nullptr); 171 return true; 172 } 173 174 template <class ELFT> 175 typename GotSection<ELFT>::uintX_t 176 GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) { 177 // Initialize the entry by the %hi(EntryValue) expression 178 // but without right-shifting. 179 EntryValue = (EntryValue + 0x8000) & ~0xffff; 180 // Take into account MIPS GOT header. 181 // See comment in the GotSection::writeTo. 182 size_t NewIndex = MipsLocalGotPos.size() + 2; 183 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex)); 184 assert(!P.second || MipsLocalGotPos.size() <= MipsPageEntries); 185 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset; 186 } 187 188 template <class ELFT> 189 typename GotSection<ELFT>::uintX_t 190 GotSection<ELFT>::getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const { 191 uintX_t Off = MipsPageEntries; 192 if (B.isTls()) 193 Off += MipsLocal.size() + MipsGlobal.size() + B.GotIndex; 194 else if (B.IsInGlobalMipsGot) 195 Off += MipsLocal.size() + B.GotIndex; 196 else if (B.isInGot()) 197 Off += B.GotIndex; 198 else { 199 auto It = MipsGotMap.find({&B, Addend}); 200 assert(It != MipsGotMap.end()); 201 Off += It->second; 202 } 203 return Off * sizeof(uintX_t) - MipsGPOffset; 204 } 205 206 template <class ELFT> 207 typename GotSection<ELFT>::uintX_t GotSection<ELFT>::getMipsTlsOffset() { 208 return (MipsPageEntries + MipsLocal.size() + MipsGlobal.size()) * 209 sizeof(uintX_t); 210 } 211 212 template <class ELFT> 213 typename GotSection<ELFT>::uintX_t 214 GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { 215 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t); 216 } 217 218 template <class ELFT> 219 typename GotSection<ELFT>::uintX_t 220 GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { 221 return B.GlobalDynIndex * sizeof(uintX_t); 222 } 223 224 template <class ELFT> 225 const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const { 226 return MipsGlobal.empty() ? nullptr : MipsGlobal.front().first; 227 } 228 229 template <class ELFT> 230 unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const { 231 return MipsPageEntries + MipsLocal.size(); 232 } 233 234 template <class ELFT> void GotSection<ELFT>::finalize() { 235 size_t EntriesNum = Entries.size(); 236 if (Config->EMachine == EM_MIPS) { 237 // Take into account MIPS GOT header. 238 // See comment in the GotSection::writeTo. 239 MipsPageEntries += 2; 240 for (const OutputSectionBase<ELFT> *OutSec : MipsOutSections) { 241 // Calculate an upper bound of MIPS GOT entries required to store page 242 // addresses of local symbols. We assume the worst case - each 64kb 243 // page of the output section has at least one GOT relocation against it. 244 // Add 0x8000 to the section's size because the page address stored 245 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff. 246 MipsPageEntries += (OutSec->getSize() + 0x8000 + 0xfffe) / 0xffff; 247 } 248 EntriesNum += MipsPageEntries + MipsLocal.size() + MipsGlobal.size(); 249 } 250 this->Header.sh_size = EntriesNum * sizeof(uintX_t); 251 } 252 253 template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *&Buf) { 254 // Set the MSB of the second GOT slot. This is not required by any 255 // MIPS ABI documentation, though. 256 // 257 // There is a comment in glibc saying that "The MSB of got[1] of a 258 // gnu object is set to identify gnu objects," and in GNU gold it 259 // says "the second entry will be used by some runtime loaders". 260 // But how this field is being used is unclear. 261 // 262 // We are not really willing to mimic other linkers behaviors 263 // without understanding why they do that, but because all files 264 // generated by GNU tools have this special GOT value, and because 265 // we've been doing this for years, it is probably a safe bet to 266 // keep doing this for now. We really need to revisit this to see 267 // if we had to do this. 268 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf); 269 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); 270 // Write 'page address' entries to the local part of the GOT. 271 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) { 272 uint8_t *Entry = Buf + L.second * sizeof(uintX_t); 273 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first); 274 } 275 Buf += MipsPageEntries * sizeof(uintX_t); 276 auto AddEntry = [&](const MipsGotEntry &SA) { 277 uint8_t *Entry = Buf; 278 Buf += sizeof(uintX_t); 279 const SymbolBody* Body = SA.first; 280 uintX_t VA = Body->template getVA<ELFT>(SA.second); 281 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA); 282 }; 283 std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry); 284 std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry); 285 } 286 287 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { 288 if (Config->EMachine == EM_MIPS) 289 writeMipsGot(Buf); 290 for (const SymbolBody *B : Entries) { 291 uint8_t *Entry = Buf; 292 Buf += sizeof(uintX_t); 293 if (!B) 294 continue; 295 if (B->isPreemptible()) 296 continue; // The dynamic linker will take care of it. 297 uintX_t VA = B->getVA<ELFT>(); 298 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA); 299 } 300 } 301 302 template <class ELFT> 303 PltSection<ELFT>::PltSection() 304 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) { 305 this->Header.sh_addralign = 16; 306 } 307 308 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { 309 // At beginning of PLT, we have code to call the dynamic linker 310 // to resolve dynsyms at runtime. Write such code. 311 Target->writePltHeader(Buf); 312 size_t Off = Target->PltHeaderSize; 313 314 for (auto &I : Entries) { 315 const SymbolBody *B = I.first; 316 unsigned RelOff = I.second; 317 uint64_t Got = B->getGotPltVA<ELFT>(); 318 uint64_t Plt = this->getVA() + Off; 319 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); 320 Off += Target->PltEntrySize; 321 } 322 } 323 324 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { 325 Sym.PltIndex = Entries.size(); 326 unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset(); 327 Entries.push_back(std::make_pair(&Sym, RelOff)); 328 } 329 330 template <class ELFT> void PltSection<ELFT>::finalize() { 331 this->Header.sh_size = 332 Target->PltHeaderSize + Entries.size() * Target->PltEntrySize; 333 } 334 335 template <class ELFT> 336 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) 337 : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL, 338 SHF_ALLOC), 339 Sort(Sort) { 340 this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 341 this->Header.sh_addralign = sizeof(uintX_t); 342 } 343 344 template <class ELFT> 345 void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) { 346 Relocs.push_back(Reloc); 347 } 348 349 template <class ELFT, class RelTy> 350 static bool compRelocations(const RelTy &A, const RelTy &B) { 351 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL); 352 } 353 354 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { 355 uint8_t *BufBegin = Buf; 356 for (const DynamicReloc<ELFT> &Rel : Relocs) { 357 auto *P = reinterpret_cast<Elf_Rela *>(Buf); 358 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 359 360 if (Config->Rela) 361 P->r_addend = Rel.getAddend(); 362 P->r_offset = Rel.getOffset(); 363 if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got) 364 // Dynamic relocation against MIPS GOT section make deal TLS entries 365 // allocated in the end of the GOT. We need to adjust the offset to take 366 // in account 'local' and 'global' GOT entries. 367 P->r_offset += Out<ELFT>::Got->getMipsTlsOffset(); 368 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL); 369 } 370 371 if (Sort) { 372 if (Config->Rela) 373 std::stable_sort((Elf_Rela *)BufBegin, 374 (Elf_Rela *)BufBegin + Relocs.size(), 375 compRelocations<ELFT, Elf_Rela>); 376 else 377 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(), 378 compRelocations<ELFT, Elf_Rel>); 379 } 380 } 381 382 template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { 383 return this->Header.sh_entsize * Relocs.size(); 384 } 385 386 template <class ELFT> void RelocationSection<ELFT>::finalize() { 387 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex 388 : Out<ELFT>::DynSymTab->SectionIndex; 389 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; 390 } 391 392 template <class ELFT> 393 InterpSection<ELFT>::InterpSection() 394 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) { 395 this->Header.sh_size = Config->DynamicLinker.size() + 1; 396 this->Header.sh_addralign = 1; 397 } 398 399 template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) { 400 StringRef S = Config->DynamicLinker; 401 memcpy(Buf, S.data(), S.size()); 402 } 403 404 template <class ELFT> 405 HashTableSection<ELFT>::HashTableSection() 406 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) { 407 this->Header.sh_entsize = sizeof(Elf_Word); 408 this->Header.sh_addralign = sizeof(Elf_Word); 409 } 410 411 static uint32_t hashSysv(StringRef Name) { 412 uint32_t H = 0; 413 for (char C : Name) { 414 H = (H << 4) + C; 415 uint32_t G = H & 0xf0000000; 416 if (G) 417 H ^= G >> 24; 418 H &= ~G; 419 } 420 return H; 421 } 422 423 template <class ELFT> void HashTableSection<ELFT>::finalize() { 424 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; 425 426 unsigned NumEntries = 2; // nbucket and nchain. 427 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. 428 429 // Create as many buckets as there are symbols. 430 // FIXME: This is simplistic. We can try to optimize it, but implementing 431 // support for SHT_GNU_HASH is probably even more profitable. 432 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); 433 this->Header.sh_size = NumEntries * sizeof(Elf_Word); 434 } 435 436 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { 437 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols(); 438 auto *P = reinterpret_cast<Elf_Word *>(Buf); 439 *P++ = NumSymbols; // nbucket 440 *P++ = NumSymbols; // nchain 441 442 Elf_Word *Buckets = P; 443 Elf_Word *Chains = P + NumSymbols; 444 445 for (const std::pair<SymbolBody *, unsigned> &P : 446 Out<ELFT>::DynSymTab->getSymbols()) { 447 SymbolBody *Body = P.first; 448 StringRef Name = Body->getName(); 449 unsigned I = Body->DynsymIndex; 450 uint32_t Hash = hashSysv(Name) % NumSymbols; 451 Chains[I] = Buckets[Hash]; 452 Buckets[Hash] = I; 453 } 454 } 455 456 static uint32_t hashGnu(StringRef Name) { 457 uint32_t H = 5381; 458 for (uint8_t C : Name) 459 H = (H << 5) + H + C; 460 return H; 461 } 462 463 template <class ELFT> 464 GnuHashTableSection<ELFT>::GnuHashTableSection() 465 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) { 466 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4; 467 this->Header.sh_addralign = sizeof(uintX_t); 468 } 469 470 template <class ELFT> 471 unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { 472 if (!NumHashed) 473 return 0; 474 475 // These values are prime numbers which are not greater than 2^(N-1) + 1. 476 // In result, for any particular NumHashed we return a prime number 477 // which is not greater than NumHashed. 478 static const unsigned Primes[] = { 479 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, 480 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; 481 482 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), 483 array_lengthof(Primes) - 1)]; 484 } 485 486 // Bloom filter estimation: at least 8 bits for each hashed symbol. 487 // GNU Hash table requirement: it should be a power of 2, 488 // the minimum value is 1, even for an empty table. 489 // Expected results for a 32-bit target: 490 // calcMaskWords(0..4) = 1 491 // calcMaskWords(5..8) = 2 492 // calcMaskWords(9..16) = 4 493 // For a 64-bit target: 494 // calcMaskWords(0..8) = 1 495 // calcMaskWords(9..16) = 2 496 // calcMaskWords(17..32) = 4 497 template <class ELFT> 498 unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { 499 if (!NumHashed) 500 return 1; 501 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); 502 } 503 504 template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { 505 unsigned NumHashed = Symbols.size(); 506 NBuckets = calcNBuckets(NumHashed); 507 MaskWords = calcMaskWords(NumHashed); 508 // Second hash shift estimation: just predefined values. 509 Shift2 = ELFT::Is64Bits ? 6 : 5; 510 511 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; 512 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header 513 + sizeof(Elf_Off) * MaskWords // Bloom Filter 514 + sizeof(Elf_Word) * NBuckets // Hash Buckets 515 + sizeof(Elf_Word) * NumHashed; // Hash Values 516 } 517 518 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { 519 writeHeader(Buf); 520 if (Symbols.empty()) 521 return; 522 writeBloomFilter(Buf); 523 writeHashTable(Buf); 524 } 525 526 template <class ELFT> 527 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { 528 auto *P = reinterpret_cast<Elf_Word *>(Buf); 529 *P++ = NBuckets; 530 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size(); 531 *P++ = MaskWords; 532 *P++ = Shift2; 533 Buf = reinterpret_cast<uint8_t *>(P); 534 } 535 536 template <class ELFT> 537 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { 538 unsigned C = sizeof(Elf_Off) * 8; 539 540 auto *Masks = reinterpret_cast<Elf_Off *>(Buf); 541 for (const SymbolData &Sym : Symbols) { 542 size_t Pos = (Sym.Hash / C) & (MaskWords - 1); 543 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) | 544 (uintX_t(1) << ((Sym.Hash >> Shift2) % C)); 545 Masks[Pos] |= V; 546 } 547 Buf += sizeof(Elf_Off) * MaskWords; 548 } 549 550 template <class ELFT> 551 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { 552 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); 553 Elf_Word *Values = Buckets + NBuckets; 554 555 int PrevBucket = -1; 556 int I = 0; 557 for (const SymbolData &Sym : Symbols) { 558 int Bucket = Sym.Hash % NBuckets; 559 assert(PrevBucket <= Bucket); 560 if (Bucket != PrevBucket) { 561 Buckets[Bucket] = Sym.Body->DynsymIndex; 562 PrevBucket = Bucket; 563 if (I > 0) 564 Values[I - 1] |= 1; 565 } 566 Values[I] = Sym.Hash & ~1; 567 ++I; 568 } 569 if (I > 0) 570 Values[I - 1] |= 1; 571 } 572 573 // Add symbols to this symbol hash table. Note that this function 574 // destructively sort a given vector -- which is needed because 575 // GNU-style hash table places some sorting requirements. 576 template <class ELFT> 577 void GnuHashTableSection<ELFT>::addSymbols( 578 std::vector<std::pair<SymbolBody *, size_t>> &V) { 579 auto Mid = std::stable_partition(V.begin(), V.end(), 580 [](std::pair<SymbolBody *, size_t> &P) { 581 return P.first->isUndefined(); 582 }); 583 if (Mid == V.end()) 584 return; 585 for (auto I = Mid, E = V.end(); I != E; ++I) { 586 SymbolBody *B = I->first; 587 size_t StrOff = I->second; 588 Symbols.push_back({B, StrOff, hashGnu(B->getName())}); 589 } 590 591 unsigned NBuckets = calcNBuckets(Symbols.size()); 592 std::stable_sort(Symbols.begin(), Symbols.end(), 593 [&](const SymbolData &L, const SymbolData &R) { 594 return L.Hash % NBuckets < R.Hash % NBuckets; 595 }); 596 597 V.erase(Mid, V.end()); 598 for (const SymbolData &Sym : Symbols) 599 V.push_back({Sym.Body, Sym.STName}); 600 } 601 602 // Returns the number of version definition entries. Because the first entry 603 // is for the version definition itself, it is the number of versioned symbols 604 // plus one. Note that we don't support multiple versions yet. 605 static unsigned getVerDefNum() { return Config->SymbolVersions.size() + 1; } 606 607 template <class ELFT> 608 DynamicSection<ELFT>::DynamicSection() 609 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) { 610 Elf_Shdr &Header = this->Header; 611 Header.sh_addralign = sizeof(uintX_t); 612 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8; 613 614 // .dynamic section is not writable on MIPS. 615 // See "Special Section" in Chapter 4 in the following document: 616 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 617 if (Config->EMachine == EM_MIPS) 618 Header.sh_flags = SHF_ALLOC; 619 } 620 621 template <class ELFT> void DynamicSection<ELFT>::finalize() { 622 if (this->Header.sh_size) 623 return; // Already finalized. 624 625 Elf_Shdr &Header = this->Header; 626 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex; 627 628 auto Add = [=](Entry E) { Entries.push_back(E); }; 629 630 // Add strings. We know that these are the last strings to be added to 631 // DynStrTab and doing this here allows this function to set DT_STRSZ. 632 if (!Config->RPath.empty()) 633 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, 634 Out<ELFT>::DynStrTab->addString(Config->RPath)}); 635 for (const std::unique_ptr<SharedFile<ELFT>> &F : 636 Symtab<ELFT>::X->getSharedFiles()) 637 if (F->isNeeded()) 638 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())}); 639 if (!Config->SoName.empty()) 640 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)}); 641 642 Out<ELFT>::DynStrTab->finalize(); 643 644 if (Out<ELFT>::RelaDyn->hasRelocs()) { 645 bool IsRela = Config->Rela; 646 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn}); 647 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()}); 648 Add({IsRela ? DT_RELAENT : DT_RELENT, 649 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))}); 650 } 651 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { 652 Add({DT_JMPREL, Out<ELFT>::RelaPlt}); 653 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()}); 654 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT, 655 Out<ELFT>::GotPlt}); 656 Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)}); 657 } 658 659 Add({DT_SYMTAB, Out<ELFT>::DynSymTab}); 660 Add({DT_SYMENT, sizeof(Elf_Sym)}); 661 Add({DT_STRTAB, Out<ELFT>::DynStrTab}); 662 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()}); 663 if (Out<ELFT>::GnuHashTab) 664 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab}); 665 if (Out<ELFT>::HashTab) 666 Add({DT_HASH, Out<ELFT>::HashTab}); 667 668 if (PreInitArraySec) { 669 Add({DT_PREINIT_ARRAY, PreInitArraySec}); 670 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()}); 671 } 672 if (InitArraySec) { 673 Add({DT_INIT_ARRAY, InitArraySec}); 674 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()}); 675 } 676 if (FiniArraySec) { 677 Add({DT_FINI_ARRAY, FiniArraySec}); 678 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()}); 679 } 680 681 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init)) 682 Add({DT_INIT, B}); 683 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini)) 684 Add({DT_FINI, B}); 685 686 uint32_t DtFlags = 0; 687 uint32_t DtFlags1 = 0; 688 if (Config->Bsymbolic) 689 DtFlags |= DF_SYMBOLIC; 690 if (Config->ZNodelete) 691 DtFlags1 |= DF_1_NODELETE; 692 if (Config->ZNow) { 693 DtFlags |= DF_BIND_NOW; 694 DtFlags1 |= DF_1_NOW; 695 } 696 if (Config->ZOrigin) { 697 DtFlags |= DF_ORIGIN; 698 DtFlags1 |= DF_1_ORIGIN; 699 } 700 701 if (DtFlags) 702 Add({DT_FLAGS, DtFlags}); 703 if (DtFlags1) 704 Add({DT_FLAGS_1, DtFlags1}); 705 706 if (!Config->Entry.empty()) 707 Add({DT_DEBUG, (uint64_t)0}); 708 709 bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0; 710 if (HasVerNeed || Out<ELFT>::VerDef) 711 Add({DT_VERSYM, Out<ELFT>::VerSym}); 712 if (Out<ELFT>::VerDef) { 713 Add({DT_VERDEF, Out<ELFT>::VerDef}); 714 Add({DT_VERDEFNUM, getVerDefNum()}); 715 } 716 if (HasVerNeed) { 717 Add({DT_VERNEED, Out<ELFT>::VerNeed}); 718 Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()}); 719 } 720 721 if (Config->EMachine == EM_MIPS) { 722 Add({DT_MIPS_RLD_VERSION, 1}); 723 Add({DT_MIPS_FLAGS, RHF_NOTPOT}); 724 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()}); 725 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()}); 726 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()}); 727 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry()) 728 Add({DT_MIPS_GOTSYM, B->DynsymIndex}); 729 else 730 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()}); 731 Add({DT_PLTGOT, Out<ELFT>::Got}); 732 if (Out<ELFT>::MipsRldMap) 733 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap}); 734 } 735 736 // +1 for DT_NULL 737 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize; 738 } 739 740 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { 741 auto *P = reinterpret_cast<Elf_Dyn *>(Buf); 742 743 for (const Entry &E : Entries) { 744 P->d_tag = E.Tag; 745 switch (E.Kind) { 746 case Entry::SecAddr: 747 P->d_un.d_ptr = E.OutSec->getVA(); 748 break; 749 case Entry::SymAddr: 750 P->d_un.d_ptr = E.Sym->template getVA<ELFT>(); 751 break; 752 case Entry::PlainInt: 753 P->d_un.d_val = E.Val; 754 break; 755 } 756 ++P; 757 } 758 } 759 760 template <class ELFT> 761 EhFrameHeader<ELFT>::EhFrameHeader() 762 : OutputSectionBase<ELFT>(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {} 763 764 // .eh_frame_hdr contains a binary search table of pointers to FDEs. 765 // Each entry of the search table consists of two values, 766 // the starting PC from where FDEs covers, and the FDE's address. 767 // It is sorted by PC. 768 template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { 769 const endianness E = ELFT::TargetEndianness; 770 771 // Sort the FDE list by their PC and uniqueify. Usually there is only 772 // one FDE for a PC (i.e. function), but if ICF merges two functions 773 // into one, there can be more than one FDEs pointing to the address. 774 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; 775 std::stable_sort(Fdes.begin(), Fdes.end(), Less); 776 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; 777 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); 778 779 Buf[0] = 1; 780 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; 781 Buf[2] = DW_EH_PE_udata4; 782 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; 783 write32<E>(Buf + 4, Out<ELFT>::EhFrame->getVA() - this->getVA() - 4); 784 write32<E>(Buf + 8, Fdes.size()); 785 Buf += 12; 786 787 uintX_t VA = this->getVA(); 788 for (FdeData &Fde : Fdes) { 789 write32<E>(Buf, Fde.Pc - VA); 790 write32<E>(Buf + 4, Fde.FdeVA - VA); 791 Buf += 8; 792 } 793 } 794 795 template <class ELFT> void EhFrameHeader<ELFT>::finalize() { 796 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. 797 this->Header.sh_size = 12 + Out<ELFT>::EhFrame->NumFdes * 8; 798 } 799 800 template <class ELFT> 801 void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { 802 Fdes.push_back({Pc, FdeVA}); 803 } 804 805 template <class ELFT> 806 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags) 807 : OutputSectionBase<ELFT>(Name, Type, Flags) { 808 if (Type == SHT_RELA) 809 this->Header.sh_entsize = sizeof(Elf_Rela); 810 else if (Type == SHT_REL) 811 this->Header.sh_entsize = sizeof(Elf_Rel); 812 } 813 814 template <class ELFT> void OutputSection<ELFT>::finalize() { 815 uint32_t Type = this->Header.sh_type; 816 if (Type != SHT_RELA && Type != SHT_REL) 817 return; 818 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex; 819 // sh_info for SHT_REL[A] sections should contain the section header index of 820 // the section to which the relocation applies. 821 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection(); 822 this->Header.sh_info = S->OutSec->SectionIndex; 823 } 824 825 template <class ELFT> 826 void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { 827 assert(C->Live); 828 auto *S = cast<InputSection<ELFT>>(C); 829 Sections.push_back(S); 830 S->OutSec = this; 831 this->updateAlignment(S->Alignment); 832 } 833 834 // If an input string is in the form of "foo.N" where N is a number, 835 // return N. Otherwise, returns 65536, which is one greater than the 836 // lowest priority. 837 static int getPriority(StringRef S) { 838 size_t Pos = S.rfind('.'); 839 if (Pos == StringRef::npos) 840 return 65536; 841 int V; 842 if (S.substr(Pos + 1).getAsInteger(10, V)) 843 return 65536; 844 return V; 845 } 846 847 // This function is called after we sort input sections 848 // and scan relocations to setup sections' offsets. 849 template <class ELFT> void OutputSection<ELFT>::assignOffsets() { 850 uintX_t Off = this->Header.sh_size; 851 for (InputSection<ELFT> *S : Sections) { 852 Off = alignTo(Off, S->Alignment); 853 S->OutSecOff = Off; 854 Off += S->getSize(); 855 } 856 this->Header.sh_size = Off; 857 } 858 859 // Sorts input sections by section name suffixes, so that .foo.N comes 860 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. 861 // We want to keep the original order if the priorities are the same 862 // because the compiler keeps the original initialization order in a 863 // translation unit and we need to respect that. 864 // For more detail, read the section of the GCC's manual about init_priority. 865 template <class ELFT> void OutputSection<ELFT>::sortInitFini() { 866 // Sort sections by priority. 867 typedef std::pair<int, InputSection<ELFT> *> Pair; 868 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; 869 870 std::vector<Pair> V; 871 for (InputSection<ELFT> *S : Sections) 872 V.push_back({getPriority(S->getSectionName()), S}); 873 std::stable_sort(V.begin(), V.end(), Comp); 874 Sections.clear(); 875 for (Pair &P : V) 876 Sections.push_back(P.second); 877 } 878 879 // Returns true if S matches /Filename.?\.o$/. 880 static bool isCrtBeginEnd(StringRef S, StringRef Filename) { 881 if (!S.endswith(".o")) 882 return false; 883 S = S.drop_back(2); 884 if (S.endswith(Filename)) 885 return true; 886 return !S.empty() && S.drop_back().endswith(Filename); 887 } 888 889 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } 890 static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } 891 892 // .ctors and .dtors are sorted by this priority from highest to lowest. 893 // 894 // 1. The section was contained in crtbegin (crtbegin contains 895 // some sentinel value in its .ctors and .dtors so that the runtime 896 // can find the beginning of the sections.) 897 // 898 // 2. The section has an optional priority value in the form of ".ctors.N" 899 // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, 900 // they are compared as string rather than number. 901 // 902 // 3. The section is just ".ctors" or ".dtors". 903 // 904 // 4. The section was contained in crtend, which contains an end marker. 905 // 906 // In an ideal world, we don't need this function because .init_array and 907 // .ctors are duplicate features (and .init_array is newer.) However, there 908 // are too many real-world use cases of .ctors, so we had no choice to 909 // support that with this rather ad-hoc semantics. 910 template <class ELFT> 911 static bool compCtors(const InputSection<ELFT> *A, 912 const InputSection<ELFT> *B) { 913 bool BeginA = isCrtbegin(A->getFile()->getName()); 914 bool BeginB = isCrtbegin(B->getFile()->getName()); 915 if (BeginA != BeginB) 916 return BeginA; 917 bool EndA = isCrtend(A->getFile()->getName()); 918 bool EndB = isCrtend(B->getFile()->getName()); 919 if (EndA != EndB) 920 return EndB; 921 StringRef X = A->getSectionName(); 922 StringRef Y = B->getSectionName(); 923 assert(X.startswith(".ctors") || X.startswith(".dtors")); 924 assert(Y.startswith(".ctors") || Y.startswith(".dtors")); 925 X = X.substr(6); 926 Y = Y.substr(6); 927 if (X.empty() && Y.empty()) 928 return false; 929 return X < Y; 930 } 931 932 // Sorts input sections by the special rules for .ctors and .dtors. 933 // Unfortunately, the rules are different from the one for .{init,fini}_array. 934 // Read the comment above. 935 template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() { 936 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>); 937 } 938 939 static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) { 940 size_t I = 0; 941 for (; I + A.size() < Size; I += A.size()) 942 memcpy(Buf + I, A.data(), A.size()); 943 memcpy(Buf + I, A.data(), Size - I); 944 } 945 946 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { 947 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name); 948 if (!Filler.empty()) 949 fill(Buf, this->getSize(), Filler); 950 if (Config->Threads) { 951 parallel_for_each(Sections.begin(), Sections.end(), 952 [=](InputSection<ELFT> *C) { C->writeTo(Buf); }); 953 } else { 954 for (InputSection<ELFT> *C : Sections) 955 C->writeTo(Buf); 956 } 957 } 958 959 template <class ELFT> 960 EhOutputSection<ELFT>::EhOutputSection() 961 : OutputSectionBase<ELFT>(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {} 962 963 // Returns the first relocation that points to a region 964 // between Begin and Begin+Size. 965 template <class IntTy, class RelTy> 966 static const RelTy *getReloc(IntTy Begin, IntTy Size, ArrayRef<RelTy> &Rels) { 967 for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) { 968 if (I->r_offset < Begin) 969 continue; 970 971 // Truncate Rels for fast access. That means we expect that the 972 // relocations are sorted and we are looking up symbols in 973 // sequential order. It is naturally satisfied for .eh_frame. 974 Rels = Rels.slice(I - Rels.begin()); 975 if (I->r_offset < Begin + Size) 976 return I; 977 return nullptr; 978 } 979 Rels = ArrayRef<RelTy>(); 980 return nullptr; 981 } 982 983 // Search for an existing CIE record or create a new one. 984 // CIE records from input object files are uniquified by their contents 985 // and where their relocations point to. 986 template <class ELFT> 987 template <class RelTy> 988 CieRecord *EhOutputSection<ELFT>::addCie(SectionPiece &Piece, 989 EhInputSection<ELFT> *Sec, 990 ArrayRef<RelTy> &Rels) { 991 const endianness E = ELFT::TargetEndianness; 992 if (read32<E>(Piece.data().data() + 4) != 0) 993 fatal("CIE expected at beginning of .eh_frame: " + Sec->getSectionName()); 994 995 SymbolBody *Personality = nullptr; 996 if (const RelTy *Rel = getReloc(Piece.InputOff, Piece.size(), Rels)) 997 Personality = &Sec->getFile()->getRelocTargetSym(*Rel); 998 999 // Search for an existing CIE by CIE contents/relocation target pair. 1000 CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; 1001 1002 // If not found, create a new one. 1003 if (Cie->Piece == nullptr) { 1004 Cie->Piece = &Piece; 1005 Cies.push_back(Cie); 1006 } 1007 return Cie; 1008 } 1009 1010 // There is one FDE per function. Returns true if a given FDE 1011 // points to a live function. 1012 template <class ELFT> 1013 template <class RelTy> 1014 bool EhOutputSection<ELFT>::isFdeLive(SectionPiece &Piece, 1015 EhInputSection<ELFT> *Sec, 1016 ArrayRef<RelTy> &Rels) { 1017 const RelTy *Rel = getReloc(Piece.InputOff, Piece.size(), Rels); 1018 if (!Rel) 1019 fatal("FDE doesn't reference another section"); 1020 SymbolBody &B = Sec->getFile()->getRelocTargetSym(*Rel); 1021 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B); 1022 if (!D || !D->Section) 1023 return false; 1024 InputSectionBase<ELFT> *Target = D->Section->Repl; 1025 return Target && Target->Live; 1026 } 1027 1028 // .eh_frame is a sequence of CIE or FDE records. In general, there 1029 // is one CIE record per input object file which is followed by 1030 // a list of FDEs. This function searches an existing CIE or create a new 1031 // one and associates FDEs to the CIE. 1032 template <class ELFT> 1033 template <class RelTy> 1034 void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, 1035 ArrayRef<RelTy> Rels) { 1036 const endianness E = ELFT::TargetEndianness; 1037 1038 DenseMap<size_t, CieRecord *> OffsetToCie; 1039 for (SectionPiece &Piece : Sec->Pieces) { 1040 // The empty record is the end marker. 1041 if (Piece.size() == 4) 1042 return; 1043 1044 size_t Offset = Piece.InputOff; 1045 uint32_t ID = read32<E>(Piece.data().data() + 4); 1046 if (ID == 0) { 1047 OffsetToCie[Offset] = addCie(Piece, Sec, Rels); 1048 continue; 1049 } 1050 1051 uint32_t CieOffset = Offset + 4 - ID; 1052 CieRecord *Cie = OffsetToCie[CieOffset]; 1053 if (!Cie) 1054 fatal("invalid CIE reference"); 1055 1056 if (!isFdeLive(Piece, Sec, Rels)) 1057 continue; 1058 Cie->FdePieces.push_back(&Piece); 1059 NumFdes++; 1060 } 1061 } 1062 1063 template <class ELFT> 1064 void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { 1065 auto *Sec = cast<EhInputSection<ELFT>>(C); 1066 Sec->OutSec = this; 1067 this->updateAlignment(Sec->Alignment); 1068 Sections.push_back(Sec); 1069 1070 // .eh_frame is a sequence of CIE or FDE records. This function 1071 // splits it into pieces so that we can call 1072 // SplitInputSection::getSectionPiece on the section. 1073 Sec->split(); 1074 if (Sec->Pieces.empty()) 1075 return; 1076 1077 if (const Elf_Shdr *RelSec = Sec->RelocSection) { 1078 ELFFile<ELFT> &Obj = Sec->getFile()->getObj(); 1079 if (RelSec->sh_type == SHT_RELA) 1080 addSectionAux(Sec, Obj.relas(RelSec)); 1081 else 1082 addSectionAux(Sec, Obj.rels(RelSec)); 1083 return; 1084 } 1085 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); 1086 } 1087 1088 template <class ELFT> 1089 static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { 1090 memcpy(Buf, D.data(), D.size()); 1091 1092 // Fix the size field. -4 since size does not include the size field itself. 1093 const endianness E = ELFT::TargetEndianness; 1094 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); 1095 } 1096 1097 template <class ELFT> void EhOutputSection<ELFT>::finalize() { 1098 if (Finalized) 1099 return; 1100 Finalized = true; 1101 1102 size_t Off = 0; 1103 for (CieRecord *Cie : Cies) { 1104 Cie->Piece->OutputOff = Off; 1105 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); 1106 1107 for (SectionPiece *Fde : Cie->FdePieces) { 1108 Fde->OutputOff = Off; 1109 Off += alignTo(Fde->size(), sizeof(uintX_t)); 1110 } 1111 } 1112 this->Header.sh_size = Off; 1113 } 1114 1115 template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { 1116 const endianness E = ELFT::TargetEndianness; 1117 switch (Size) { 1118 case DW_EH_PE_udata2: 1119 return read16<E>(Buf); 1120 case DW_EH_PE_udata4: 1121 return read32<E>(Buf); 1122 case DW_EH_PE_udata8: 1123 return read64<E>(Buf); 1124 case DW_EH_PE_absptr: 1125 if (ELFT::Is64Bits) 1126 return read64<E>(Buf); 1127 return read32<E>(Buf); 1128 } 1129 fatal("unknown FDE size encoding"); 1130 } 1131 1132 // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. 1133 // We need it to create .eh_frame_hdr section. 1134 template <class ELFT> 1135 typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, 1136 uint8_t Enc) { 1137 // The starting address to which this FDE applies is 1138 // stored at FDE + 8 byte. 1139 size_t Off = FdeOff + 8; 1140 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); 1141 if ((Enc & 0x70) == DW_EH_PE_absptr) 1142 return Addr; 1143 if ((Enc & 0x70) == DW_EH_PE_pcrel) 1144 return Addr + this->getVA() + Off; 1145 fatal("unknown FDE size relative encoding"); 1146 } 1147 1148 template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) { 1149 const endianness E = ELFT::TargetEndianness; 1150 for (CieRecord *Cie : Cies) { 1151 size_t CieOffset = Cie->Piece->OutputOff; 1152 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); 1153 1154 for (SectionPiece *Fde : Cie->FdePieces) { 1155 size_t Off = Fde->OutputOff; 1156 writeCieFde<ELFT>(Buf + Off, Fde->data()); 1157 1158 // FDE's second word should have the offset to an associated CIE. 1159 // Write it. 1160 write32<E>(Buf + Off + 4, Off + 4 - CieOffset); 1161 } 1162 } 1163 1164 for (EhInputSection<ELFT> *S : Sections) 1165 S->relocate(Buf, nullptr); 1166 1167 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table 1168 // to get a FDE from an address to which FDE is applied. So here 1169 // we obtain two addresses and pass them to EhFrameHdr object. 1170 if (Out<ELFT>::EhFrameHdr) { 1171 for (CieRecord *Cie : Cies) { 1172 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data()); 1173 for (SectionPiece *Fde : Cie->FdePieces) { 1174 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); 1175 uintX_t FdeVA = this->getVA() + Fde->OutputOff; 1176 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); 1177 } 1178 } 1179 } 1180 } 1181 1182 template <class ELFT> 1183 MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type, 1184 uintX_t Flags, uintX_t Alignment) 1185 : OutputSectionBase<ELFT>(Name, Type, Flags), 1186 Builder(llvm::StringTableBuilder::RAW, Alignment) {} 1187 1188 template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { 1189 if (shouldTailMerge()) { 1190 StringRef Data = Builder.data(); 1191 memcpy(Buf, Data.data(), Data.size()); 1192 return; 1193 } 1194 for (const std::pair<CachedHash<StringRef>, size_t> &P : Builder.getMap()) { 1195 StringRef Data = P.first.Val; 1196 memcpy(Buf + P.second, Data.data(), Data.size()); 1197 } 1198 } 1199 1200 static StringRef toStringRef(ArrayRef<uint8_t> A) { 1201 return {(const char *)A.data(), A.size()}; 1202 } 1203 1204 template <class ELFT> 1205 void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { 1206 auto *Sec = cast<MergeInputSection<ELFT>>(C); 1207 Sec->OutSec = this; 1208 this->updateAlignment(Sec->Alignment); 1209 this->Header.sh_entsize = Sec->getSectionHdr()->sh_entsize; 1210 Sections.push_back(Sec); 1211 1212 bool IsString = this->Header.sh_flags & SHF_STRINGS; 1213 1214 for (SectionPiece &Piece : Sec->Pieces) { 1215 if (!Piece.Live) 1216 continue; 1217 uintX_t OutputOffset = Builder.add(toStringRef(Piece.data())); 1218 if (!IsString || !shouldTailMerge()) 1219 Piece.OutputOff = OutputOffset; 1220 } 1221 } 1222 1223 template <class ELFT> 1224 unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) { 1225 return Builder.getOffset(Val); 1226 } 1227 1228 template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { 1229 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS; 1230 } 1231 1232 template <class ELFT> void MergeOutputSection<ELFT>::finalize() { 1233 if (shouldTailMerge()) 1234 Builder.finalize(); 1235 this->Header.sh_size = Builder.getSize(); 1236 } 1237 1238 template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() { 1239 for (MergeInputSection<ELFT> *Sec : Sections) 1240 Sec->finalizePieces(); 1241 } 1242 1243 template <class ELFT> 1244 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) 1245 : OutputSectionBase<ELFT>(Name, SHT_STRTAB, 1246 Dynamic ? (uintX_t)SHF_ALLOC : 0), 1247 Dynamic(Dynamic) { 1248 this->Header.sh_addralign = 1; 1249 } 1250 1251 // Adds a string to the string table. If HashIt is true we hash and check for 1252 // duplicates. It is optional because the name of global symbols are already 1253 // uniqued and hashing them again has a big cost for a small value: uniquing 1254 // them with some other string that happens to be the same. 1255 template <class ELFT> 1256 unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) { 1257 if (HashIt) { 1258 auto R = StringMap.insert(std::make_pair(S, Size)); 1259 if (!R.second) 1260 return R.first->second; 1261 } 1262 unsigned Ret = Size; 1263 Size += S.size() + 1; 1264 Strings.push_back(S); 1265 return Ret; 1266 } 1267 1268 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { 1269 // ELF string tables start with NUL byte, so advance the pointer by one. 1270 ++Buf; 1271 for (StringRef S : Strings) { 1272 memcpy(Buf, S.data(), S.size()); 1273 Buf += S.size() + 1; 1274 } 1275 } 1276 1277 template <class ELFT> 1278 typename ELFT::uint DynamicReloc<ELFT>::getOffset() const { 1279 if (OutputSec) 1280 return OutputSec->getVA() + OffsetInSec; 1281 return InputSec->OutSec->getVA() + InputSec->getOffset(OffsetInSec); 1282 } 1283 1284 template <class ELFT> 1285 typename ELFT::uint DynamicReloc<ELFT>::getAddend() const { 1286 if (UseSymVA) 1287 return Sym->getVA<ELFT>(Addend); 1288 return Addend; 1289 } 1290 1291 template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const { 1292 if (Sym && !UseSymVA) 1293 return Sym->DynsymIndex; 1294 return 0; 1295 } 1296 1297 template <class ELFT> 1298 SymbolTableSection<ELFT>::SymbolTableSection( 1299 StringTableSection<ELFT> &StrTabSec) 1300 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab", 1301 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, 1302 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0), 1303 StrTabSec(StrTabSec) { 1304 this->Header.sh_entsize = sizeof(Elf_Sym); 1305 this->Header.sh_addralign = sizeof(uintX_t); 1306 } 1307 1308 // Orders symbols according to their positions in the GOT, 1309 // in compliance with MIPS ABI rules. 1310 // See "Global Offset Table" in Chapter 5 in the following document 1311 // for detailed description: 1312 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1313 static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L, 1314 const std::pair<SymbolBody *, unsigned> &R) { 1315 // Sort entries related to non-local preemptible symbols by GOT indexes. 1316 // All other entries go to the first part of GOT in arbitrary order. 1317 bool LIsInLocalGot = !L.first->IsInGlobalMipsGot; 1318 bool RIsInLocalGot = !R.first->IsInGlobalMipsGot; 1319 if (LIsInLocalGot || RIsInLocalGot) 1320 return !RIsInLocalGot; 1321 return L.first->GotIndex < R.first->GotIndex; 1322 } 1323 1324 static uint8_t getSymbolBinding(SymbolBody *Body) { 1325 Symbol *S = Body->symbol(); 1326 uint8_t Visibility = S->Visibility; 1327 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) 1328 return STB_LOCAL; 1329 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE) 1330 return STB_GLOBAL; 1331 return S->Binding; 1332 } 1333 1334 template <class ELFT> void SymbolTableSection<ELFT>::finalize() { 1335 if (this->Header.sh_size) 1336 return; // Already finalized. 1337 1338 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym); 1339 this->Header.sh_link = StrTabSec.SectionIndex; 1340 this->Header.sh_info = NumLocals + 1; 1341 1342 if (Config->Relocatable) { 1343 size_t I = NumLocals; 1344 for (const std::pair<SymbolBody *, size_t> &P : Symbols) 1345 P.first->DynsymIndex = ++I; 1346 return; 1347 } 1348 1349 if (!StrTabSec.isDynamic()) { 1350 std::stable_sort(Symbols.begin(), Symbols.end(), 1351 [](const std::pair<SymbolBody *, unsigned> &L, 1352 const std::pair<SymbolBody *, unsigned> &R) { 1353 return getSymbolBinding(L.first) == STB_LOCAL && 1354 getSymbolBinding(R.first) != STB_LOCAL; 1355 }); 1356 return; 1357 } 1358 if (Out<ELFT>::GnuHashTab) 1359 // NB: It also sorts Symbols to meet the GNU hash table requirements. 1360 Out<ELFT>::GnuHashTab->addSymbols(Symbols); 1361 else if (Config->EMachine == EM_MIPS) 1362 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols); 1363 size_t I = 0; 1364 for (const std::pair<SymbolBody *, size_t> &P : Symbols) 1365 P.first->DynsymIndex = ++I; 1366 } 1367 1368 template <class ELFT> 1369 void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) { 1370 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)}); 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->DiscardAll && !StrTabSec.isDynamic()) 1379 writeLocalSymbols(Buf); 1380 1381 writeGlobalSymbols(Buf); 1382 } 1383 1384 template <class ELFT> 1385 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { 1386 // Iterate over all input object files to copy their local symbols 1387 // to the output symbol table pointed by Buf. 1388 for (const std::unique_ptr<ObjectFile<ELFT>> &File : 1389 Symtab<ELFT>::X->getObjectFiles()) { 1390 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P : 1391 File->KeptLocalSyms) { 1392 const DefinedRegular<ELFT> &Body = *P.first; 1393 InputSectionBase<ELFT> *Section = Body.Section; 1394 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); 1395 1396 if (!Section) { 1397 ESym->st_shndx = SHN_ABS; 1398 ESym->st_value = Body.Value; 1399 } else { 1400 const OutputSectionBase<ELFT> *OutSec = Section->OutSec; 1401 ESym->st_shndx = OutSec->SectionIndex; 1402 ESym->st_value = OutSec->getVA() + Section->getOffset(Body); 1403 } 1404 ESym->st_name = P.second; 1405 ESym->st_size = Body.template getSize<ELFT>(); 1406 ESym->setBindingAndType(STB_LOCAL, Body.Type); 1407 Buf += sizeof(*ESym); 1408 } 1409 } 1410 } 1411 1412 template <class ELFT> 1413 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { 1414 // Write the internal symbol table contents to the output symbol table 1415 // pointed by Buf. 1416 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); 1417 for (const std::pair<SymbolBody *, size_t> &P : Symbols) { 1418 SymbolBody *Body = P.first; 1419 size_t StrOff = P.second; 1420 1421 uint8_t Type = Body->Type; 1422 uintX_t Size = Body->getSize<ELFT>(); 1423 1424 ESym->setBindingAndType(getSymbolBinding(Body), Type); 1425 ESym->st_size = Size; 1426 ESym->st_name = StrOff; 1427 ESym->setVisibility(Body->symbol()->Visibility); 1428 ESym->st_value = Body->getVA<ELFT>(); 1429 1430 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body)) 1431 ESym->st_shndx = OutSec->SectionIndex; 1432 else if (isa<DefinedRegular<ELFT>>(Body)) 1433 ESym->st_shndx = SHN_ABS; 1434 1435 // On MIPS we need to mark symbol which has a PLT entry and requires pointer 1436 // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker 1437 // distinguish such symbols and MIPS lazy-binding stubs. 1438 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt 1439 if (Config->EMachine == EM_MIPS && Body->isInPlt() && 1440 Body->NeedsCopyOrPltAddr) 1441 ESym->st_other |= STO_MIPS_PLT; 1442 ++ESym; 1443 } 1444 } 1445 1446 template <class ELFT> 1447 const OutputSectionBase<ELFT> * 1448 SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) { 1449 switch (Sym->kind()) { 1450 case SymbolBody::DefinedSyntheticKind: 1451 return cast<DefinedSynthetic<ELFT>>(Sym)->Section; 1452 case SymbolBody::DefinedRegularKind: { 1453 auto &D = cast<DefinedRegular<ELFT>>(*Sym); 1454 if (D.Section) 1455 return D.Section->OutSec; 1456 break; 1457 } 1458 case SymbolBody::DefinedCommonKind: 1459 return Out<ELFT>::Bss; 1460 case SymbolBody::SharedKind: 1461 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy()) 1462 return Out<ELFT>::Bss; 1463 break; 1464 case SymbolBody::UndefinedKind: 1465 case SymbolBody::LazyArchiveKind: 1466 case SymbolBody::LazyObjectKind: 1467 break; 1468 case SymbolBody::DefinedBitcodeKind: 1469 llvm_unreachable("should have been replaced"); 1470 } 1471 return nullptr; 1472 } 1473 1474 template <class ELFT> 1475 VersionDefinitionSection<ELFT>::VersionDefinitionSection() 1476 : OutputSectionBase<ELFT>(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {} 1477 1478 static StringRef getFileDefName() { 1479 if (!Config->SoName.empty()) 1480 return Config->SoName; 1481 return Config->OutputFile; 1482 } 1483 1484 template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() { 1485 FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName()); 1486 for (Version &V : Config->SymbolVersions) 1487 V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name); 1488 1489 this->Header.sh_size = 1490 (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); 1491 for (Version &V : Config->SymbolVersions) 1492 if (!V.Parent.empty()) 1493 this->Header.sh_size += sizeof(Elf_Verdaux); 1494 1495 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex; 1496 this->Header.sh_addralign = sizeof(uint32_t); 1497 1498 // sh_info should be set to the number of definitions. This fact is missed in 1499 // documentation, but confirmed by binutils community: 1500 // https://sourceware.org/ml/binutils/2014-11/msg00355.html 1501 this->Header.sh_info = getVerDefNum(); 1502 } 1503 1504 static size_t getVersionNameStrTabOffset(StringRef Name) { 1505 for (Version &V : Config->SymbolVersions) 1506 if (V.Name == Name) 1507 return V.NameOff; 1508 error("unknown version name " + Name + " used as a dependency"); 1509 return 0; 1510 } 1511 1512 template <class Elf_Verdef, class Elf_Verdaux> 1513 static void writeDefinition(Elf_Verdef *&Verdef, Elf_Verdaux *&Verdaux, 1514 uint32_t Flags, uint32_t Index, StringRef Name, 1515 size_t StrTabOffset, StringRef ParentName) { 1516 bool HasParent = !ParentName.empty(); 1517 1518 Verdef->vd_version = 1; 1519 Verdef->vd_cnt = HasParent ? 2 : 1; 1520 Verdef->vd_aux = 1521 reinterpret_cast<char *>(Verdaux) - reinterpret_cast<char *>(Verdef); 1522 Verdef->vd_next = sizeof(Elf_Verdef); 1523 1524 Verdef->vd_flags = Flags; 1525 Verdef->vd_ndx = Index; 1526 Verdef->vd_hash = hashSysv(Name); 1527 ++Verdef; 1528 1529 Verdaux->vda_name = StrTabOffset; 1530 if (HasParent) { 1531 Verdaux->vda_next = sizeof(Elf_Verdaux); 1532 ++Verdaux; 1533 Verdaux->vda_name = getVersionNameStrTabOffset(ParentName); 1534 } 1535 1536 Verdaux->vda_next = 0; 1537 ++Verdaux; 1538 } 1539 1540 template <class ELFT> 1541 void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { 1542 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); 1543 Elf_Verdaux *Verdaux = 1544 reinterpret_cast<Elf_Verdaux *>(Verdef + getVerDefNum()); 1545 1546 writeDefinition(Verdef, Verdaux, VER_FLG_BASE, 1, getFileDefName(), 1547 FileDefNameOff, "" /* Parent */); 1548 1549 uint32_t I = 2; 1550 for (Version &V : Config->SymbolVersions) 1551 writeDefinition(Verdef, Verdaux, 0 /* Flags */, I++, V.Name, V.NameOff, 1552 V.Parent); 1553 1554 Verdef[-1].vd_next = 0; 1555 } 1556 1557 template <class ELFT> 1558 VersionTableSection<ELFT>::VersionTableSection() 1559 : OutputSectionBase<ELFT>(".gnu.version", SHT_GNU_versym, SHF_ALLOC) { 1560 this->Header.sh_addralign = sizeof(uint16_t); 1561 } 1562 1563 template <class ELFT> void VersionTableSection<ELFT>::finalize() { 1564 this->Header.sh_size = 1565 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1); 1566 this->Header.sh_entsize = sizeof(Elf_Versym); 1567 // At the moment of june 2016 GNU docs does not mention that sh_link field 1568 // should be set, but Sun docs do. Also readelf relies on this field. 1569 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; 1570 } 1571 1572 template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { 1573 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; 1574 for (const std::pair<SymbolBody *, size_t> &P : 1575 Out<ELFT>::DynSymTab->getSymbols()) { 1576 OutVersym->vs_index = P.first->symbol()->VersionId; 1577 ++OutVersym; 1578 } 1579 } 1580 1581 template <class ELFT> 1582 VersionNeedSection<ELFT>::VersionNeedSection() 1583 : OutputSectionBase<ELFT>(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) { 1584 this->Header.sh_addralign = sizeof(uint32_t); 1585 1586 // Identifiers in verneed section start at 2 because 0 and 1 are reserved 1587 // for VER_NDX_LOCAL and VER_NDX_GLOBAL. 1588 // First identifiers are reserved by verdef section if it exist. 1589 NextIndex = getVerDefNum() + 1; 1590 } 1591 1592 template <class ELFT> 1593 void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) { 1594 if (!SS->Verdef) { 1595 SS->symbol()->VersionId = VER_NDX_GLOBAL; 1596 return; 1597 } 1598 SharedFile<ELFT> *F = SS->File; 1599 // If we don't already know that we need an Elf_Verneed for this DSO, prepare 1600 // to create one by adding it to our needed list and creating a dynstr entry 1601 // for the soname. 1602 if (F->VerdefMap.empty()) 1603 Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())}); 1604 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef]; 1605 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, 1606 // prepare to create one by allocating a version identifier and creating a 1607 // dynstr entry for the version name. 1608 if (NV.Index == 0) { 1609 NV.StrTab = Out<ELFT>::DynStrTab->addString( 1610 SS->File->getStringTable().data() + SS->Verdef->getAux()->vda_name); 1611 NV.Index = NextIndex++; 1612 } 1613 SS->symbol()->VersionId = NV.Index; 1614 } 1615 1616 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { 1617 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. 1618 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); 1619 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); 1620 1621 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { 1622 // Create an Elf_Verneed for this DSO. 1623 Verneed->vn_version = 1; 1624 Verneed->vn_cnt = P.first->VerdefMap.size(); 1625 Verneed->vn_file = P.second; 1626 Verneed->vn_aux = 1627 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); 1628 Verneed->vn_next = sizeof(Elf_Verneed); 1629 ++Verneed; 1630 1631 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over 1632 // VerdefMap, which will only contain references to needed version 1633 // definitions. Each Elf_Vernaux is based on the information contained in 1634 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of 1635 // pointers, but is deterministic because the pointers refer to Elf_Verdef 1636 // data structures within a single input file. 1637 for (auto &NV : P.first->VerdefMap) { 1638 Vernaux->vna_hash = NV.first->vd_hash; 1639 Vernaux->vna_flags = 0; 1640 Vernaux->vna_other = NV.second.Index; 1641 Vernaux->vna_name = NV.second.StrTab; 1642 Vernaux->vna_next = sizeof(Elf_Vernaux); 1643 ++Vernaux; 1644 } 1645 1646 Vernaux[-1].vna_next = 0; 1647 } 1648 Verneed[-1].vn_next = 0; 1649 } 1650 1651 template <class ELFT> void VersionNeedSection<ELFT>::finalize() { 1652 this->Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex; 1653 this->Header.sh_info = Needed.size(); 1654 unsigned Size = Needed.size() * sizeof(Elf_Verneed); 1655 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) 1656 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); 1657 this->Header.sh_size = Size; 1658 } 1659 1660 template <class ELFT> 1661 BuildIdSection<ELFT>::BuildIdSection(size_t HashSize) 1662 : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC), 1663 HashSize(HashSize) { 1664 // 16 bytes for the note section header. 1665 this->Header.sh_size = 16 + HashSize; 1666 } 1667 1668 template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) { 1669 const endianness E = ELFT::TargetEndianness; 1670 write32<E>(Buf, 4); // Name size 1671 write32<E>(Buf + 4, HashSize); // Content size 1672 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type 1673 memcpy(Buf + 12, "GNU", 4); // Name string 1674 HashBuf = Buf + 16; 1675 } 1676 1677 template <class ELFT> 1678 void BuildIdFnv1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) { 1679 const endianness E = ELFT::TargetEndianness; 1680 1681 // 64-bit FNV-1 hash 1682 uint64_t Hash = 0xcbf29ce484222325; 1683 for (ArrayRef<uint8_t> Buf : Bufs) { 1684 for (uint8_t B : Buf) { 1685 Hash *= 0x100000001b3; 1686 Hash ^= B; 1687 } 1688 } 1689 write64<E>(this->HashBuf, Hash); 1690 } 1691 1692 template <class ELFT> 1693 void BuildIdMd5<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) { 1694 llvm::MD5 Hash; 1695 for (ArrayRef<uint8_t> Buf : Bufs) 1696 Hash.update(Buf); 1697 MD5::MD5Result Res; 1698 Hash.final(Res); 1699 memcpy(this->HashBuf, Res, 16); 1700 } 1701 1702 template <class ELFT> 1703 void BuildIdSha1<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) { 1704 llvm::SHA1 Hash; 1705 for (ArrayRef<uint8_t> Buf : Bufs) 1706 Hash.update(Buf); 1707 memcpy(this->HashBuf, Hash.final().data(), 20); 1708 } 1709 1710 template <class ELFT> 1711 BuildIdHexstring<ELFT>::BuildIdHexstring() 1712 : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {} 1713 1714 template <class ELFT> 1715 void BuildIdHexstring<ELFT>::writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) { 1716 memcpy(this->HashBuf, Config->BuildIdVector.data(), 1717 Config->BuildIdVector.size()); 1718 } 1719 1720 template <class ELFT> 1721 MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection() 1722 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) { 1723 this->Header.sh_addralign = 4; 1724 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo); 1725 this->Header.sh_size = sizeof(Elf_Mips_RegInfo); 1726 } 1727 1728 template <class ELFT> 1729 void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) { 1730 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf); 1731 R->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset; 1732 R->ri_gprmask = GprMask; 1733 } 1734 1735 template <class ELFT> 1736 void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { 1737 // Copy input object file's .reginfo gprmask to output. 1738 auto *S = cast<MipsReginfoInputSection<ELFT>>(C); 1739 GprMask |= S->Reginfo->ri_gprmask; 1740 S->OutSec = this; 1741 } 1742 1743 template <class ELFT> 1744 MipsOptionsOutputSection<ELFT>::MipsOptionsOutputSection() 1745 : OutputSectionBase<ELFT>(".MIPS.options", SHT_MIPS_OPTIONS, 1746 SHF_ALLOC | SHF_MIPS_NOSTRIP) { 1747 this->Header.sh_addralign = 8; 1748 this->Header.sh_entsize = 1; 1749 this->Header.sh_size = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); 1750 } 1751 1752 template <class ELFT> 1753 void MipsOptionsOutputSection<ELFT>::writeTo(uint8_t *Buf) { 1754 auto *Opt = reinterpret_cast<Elf_Mips_Options *>(Buf); 1755 Opt->kind = ODK_REGINFO; 1756 Opt->size = this->Header.sh_size; 1757 Opt->section = 0; 1758 Opt->info = 0; 1759 auto *Reg = reinterpret_cast<Elf_Mips_RegInfo *>(Buf + sizeof(*Opt)); 1760 Reg->ri_gp_value = Out<ELFT>::Got->getVA() + MipsGPOffset; 1761 Reg->ri_gprmask = GprMask; 1762 } 1763 1764 template <class ELFT> 1765 void MipsOptionsOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { 1766 auto *S = cast<MipsOptionsInputSection<ELFT>>(C); 1767 if (S->Reginfo) 1768 GprMask |= S->Reginfo->ri_gprmask; 1769 S->OutSec = this; 1770 } 1771 1772 namespace lld { 1773 namespace elf { 1774 template class OutputSectionBase<ELF32LE>; 1775 template class OutputSectionBase<ELF32BE>; 1776 template class OutputSectionBase<ELF64LE>; 1777 template class OutputSectionBase<ELF64BE>; 1778 1779 template class EhFrameHeader<ELF32LE>; 1780 template class EhFrameHeader<ELF32BE>; 1781 template class EhFrameHeader<ELF64LE>; 1782 template class EhFrameHeader<ELF64BE>; 1783 1784 template class GotPltSection<ELF32LE>; 1785 template class GotPltSection<ELF32BE>; 1786 template class GotPltSection<ELF64LE>; 1787 template class GotPltSection<ELF64BE>; 1788 1789 template class GotSection<ELF32LE>; 1790 template class GotSection<ELF32BE>; 1791 template class GotSection<ELF64LE>; 1792 template class GotSection<ELF64BE>; 1793 1794 template class PltSection<ELF32LE>; 1795 template class PltSection<ELF32BE>; 1796 template class PltSection<ELF64LE>; 1797 template class PltSection<ELF64BE>; 1798 1799 template class RelocationSection<ELF32LE>; 1800 template class RelocationSection<ELF32BE>; 1801 template class RelocationSection<ELF64LE>; 1802 template class RelocationSection<ELF64BE>; 1803 1804 template class InterpSection<ELF32LE>; 1805 template class InterpSection<ELF32BE>; 1806 template class InterpSection<ELF64LE>; 1807 template class InterpSection<ELF64BE>; 1808 1809 template class GnuHashTableSection<ELF32LE>; 1810 template class GnuHashTableSection<ELF32BE>; 1811 template class GnuHashTableSection<ELF64LE>; 1812 template class GnuHashTableSection<ELF64BE>; 1813 1814 template class HashTableSection<ELF32LE>; 1815 template class HashTableSection<ELF32BE>; 1816 template class HashTableSection<ELF64LE>; 1817 template class HashTableSection<ELF64BE>; 1818 1819 template class DynamicSection<ELF32LE>; 1820 template class DynamicSection<ELF32BE>; 1821 template class DynamicSection<ELF64LE>; 1822 template class DynamicSection<ELF64BE>; 1823 1824 template class OutputSection<ELF32LE>; 1825 template class OutputSection<ELF32BE>; 1826 template class OutputSection<ELF64LE>; 1827 template class OutputSection<ELF64BE>; 1828 1829 template class EhOutputSection<ELF32LE>; 1830 template class EhOutputSection<ELF32BE>; 1831 template class EhOutputSection<ELF64LE>; 1832 template class EhOutputSection<ELF64BE>; 1833 1834 template class MipsReginfoOutputSection<ELF32LE>; 1835 template class MipsReginfoOutputSection<ELF32BE>; 1836 template class MipsReginfoOutputSection<ELF64LE>; 1837 template class MipsReginfoOutputSection<ELF64BE>; 1838 1839 template class MipsOptionsOutputSection<ELF32LE>; 1840 template class MipsOptionsOutputSection<ELF32BE>; 1841 template class MipsOptionsOutputSection<ELF64LE>; 1842 template class MipsOptionsOutputSection<ELF64BE>; 1843 1844 template class MergeOutputSection<ELF32LE>; 1845 template class MergeOutputSection<ELF32BE>; 1846 template class MergeOutputSection<ELF64LE>; 1847 template class MergeOutputSection<ELF64BE>; 1848 1849 template class StringTableSection<ELF32LE>; 1850 template class StringTableSection<ELF32BE>; 1851 template class StringTableSection<ELF64LE>; 1852 template class StringTableSection<ELF64BE>; 1853 1854 template class SymbolTableSection<ELF32LE>; 1855 template class SymbolTableSection<ELF32BE>; 1856 template class SymbolTableSection<ELF64LE>; 1857 template class SymbolTableSection<ELF64BE>; 1858 1859 template class VersionTableSection<ELF32LE>; 1860 template class VersionTableSection<ELF32BE>; 1861 template class VersionTableSection<ELF64LE>; 1862 template class VersionTableSection<ELF64BE>; 1863 1864 template class VersionNeedSection<ELF32LE>; 1865 template class VersionNeedSection<ELF32BE>; 1866 template class VersionNeedSection<ELF64LE>; 1867 template class VersionNeedSection<ELF64BE>; 1868 1869 template class VersionDefinitionSection<ELF32LE>; 1870 template class VersionDefinitionSection<ELF32BE>; 1871 template class VersionDefinitionSection<ELF64LE>; 1872 template class VersionDefinitionSection<ELF64BE>; 1873 1874 template class BuildIdSection<ELF32LE>; 1875 template class BuildIdSection<ELF32BE>; 1876 template class BuildIdSection<ELF64LE>; 1877 template class BuildIdSection<ELF64BE>; 1878 1879 template class BuildIdFnv1<ELF32LE>; 1880 template class BuildIdFnv1<ELF32BE>; 1881 template class BuildIdFnv1<ELF64LE>; 1882 template class BuildIdFnv1<ELF64BE>; 1883 1884 template class BuildIdMd5<ELF32LE>; 1885 template class BuildIdMd5<ELF32BE>; 1886 template class BuildIdMd5<ELF64LE>; 1887 template class BuildIdMd5<ELF64BE>; 1888 1889 template class BuildIdSha1<ELF32LE>; 1890 template class BuildIdSha1<ELF32BE>; 1891 template class BuildIdSha1<ELF64LE>; 1892 template class BuildIdSha1<ELF64BE>; 1893 1894 template class BuildIdHexstring<ELF32LE>; 1895 template class BuildIdHexstring<ELF32BE>; 1896 template class BuildIdHexstring<ELF64LE>; 1897 template class BuildIdHexstring<ELF64BE>; 1898 } 1899 } 1900