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