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