InputSection.cpp revision 303239
18876Srgrimes//===- InputSection.cpp ---------------------------------------------------===//
24Srgrimes//
34Srgrimes//                             The LLVM Linker
44Srgrimes//
58876Srgrimes// This file is distributed under the University of Illinois Open Source
64Srgrimes// License. See LICENSE.TXT for details.
74Srgrimes//
84Srgrimes//===----------------------------------------------------------------------===//
94Srgrimes
104Srgrimes#include "InputSection.h"
118876Srgrimes#include "Config.h"
128876Srgrimes#include "EhFrame.h"
134Srgrimes#include "Error.h"
144Srgrimes#include "InputFiles.h"
158876Srgrimes#include "LinkerScript.h"
164Srgrimes#include "OutputSections.h"
178876Srgrimes#include "Target.h"
184Srgrimes#include "Thunks.h"
194Srgrimes
204Srgrimes#include "llvm/Support/Compression.h"
214Srgrimes#include "llvm/Support/Endian.h"
228876Srgrimes
234Srgrimesusing namespace llvm;
244Srgrimesusing namespace llvm::ELF;
254Srgrimesusing namespace llvm::object;
2636735Sdfrusing namespace llvm::support::endian;
274Srgrimes
28623Srgrimesusing namespace lld;
294Srgrimesusing namespace lld::elf;
304Srgrimes
314Srgrimestemplate <class ELFT> bool elf::isDiscarded(InputSectionBase<ELFT> *S) {
324Srgrimes  return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
334Srgrimes         Script<ELFT>::X->isDiscarded(S);
344Srgrimes}
354Srgrimes
362056Swollmantemplate <class ELFT>
3712734SbdeInputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
3812662Sdg                                         const Elf_Shdr *Header,
3912734Sbde                                         Kind SectionKind)
4012734Sbde    : Header(Header), File(File), SectionKind(SectionKind), Repl(this),
412056Swollman      Compressed(Header->sh_flags & SHF_COMPRESSED) {
424Srgrimes  // The garbage collector sets sections' Live bits.
434Srgrimes  // If GC is disabled, all sections are considered live by default.
444Srgrimes  Live = !Config->GcSections;
454Srgrimes
464Srgrimes  // The ELF spec states that a value of 0 means the section has
4712720Sphk  // no alignment constraits.
4812515Sphk  Alignment = std::max<uintX_t>(Header->sh_addralign, 1);
4912515Sphk}
5012515Sphk
514Srgrimestemplate <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
5212515Sphk  if (auto *D = dyn_cast<InputSection<ELFT>>(this))
5312515Sphk    if (D->getThunksSize() > 0)
5412515Sphk      return D->getThunkOff() + D->getThunksSize();
5512515Sphk  return Header->sh_size;
5612515Sphk}
5712515Sphk
5812515Sphktemplate <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
5912515Sphk  return check(File->getObj().getSectionName(this->Header));
6012515Sphk}
6112515Sphk
6212515Sphktemplate <class ELFT>
6312515SphkArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
6412515Sphk  if (Compressed)
654Srgrimes    return ArrayRef<uint8_t>((const uint8_t *)Uncompressed.data(),
664Srgrimes                             Uncompressed.size());
674Srgrimes  return check(this->File->getObj().getSectionContents(this->Header));
684Srgrimes}
694Srgrimes
704Srgrimestemplate <class ELFT>
714Srgrimestypename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) const {
724Srgrimes  switch (SectionKind) {
734Srgrimes  case Regular:
744Srgrimes    return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
754Srgrimes  case EHFrame:
764Srgrimes    return cast<EhInputSection<ELFT>>(this)->getOffset(Offset);
774Srgrimes  case Merge:
784Srgrimes    return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
794Srgrimes  case MipsReginfo:
804Srgrimes  case MipsOptions:
814Srgrimes    // MIPS .reginfo and .MIPS.options sections are consumed by the linker,
824Srgrimes    // and the linker produces a single output section. It is possible that
8312515Sphk    // input files contain section symbol points to the corresponding input
844Srgrimes    // section. Redirect it to the produced output section.
854Srgrimes    if (Offset != 0)
864Srgrimes      fatal("Unsupported reference to the middle of '" + getSectionName() +
874Srgrimes            "' section");
884Srgrimes    return this->OutSec->getVA();
894Srgrimes  }
904Srgrimes  llvm_unreachable("invalid section kind");
9112515Sphk}
924Srgrimes
934Srgrimestemplate <class ELFT> void InputSectionBase<ELFT>::uncompress() {
944Srgrimes  if (!zlib::isAvailable())
954Srgrimes    fatal("build lld with zlib to enable compressed sections support");
964Srgrimes
974Srgrimes  // A compressed section consists of a header of Elf_Chdr type
984Srgrimes  // followed by compressed data.
994Srgrimes  ArrayRef<uint8_t> Data =
1004Srgrimes      check(this->File->getObj().getSectionContents(this->Header));
1014Srgrimes  if (Data.size() < sizeof(Elf_Chdr))
1024Srgrimes    fatal("corrupt compressed section");
1034Srgrimes
1044Srgrimes  auto *Hdr = reinterpret_cast<const Elf_Chdr *>(Data.data());
1054Srgrimes  Data = Data.slice(sizeof(Elf_Chdr));
1064Srgrimes
1074Srgrimes  if (Hdr->ch_type != ELFCOMPRESS_ZLIB)
1084Srgrimes    fatal("unsupported compression type");
1094Srgrimes
1104Srgrimes  StringRef Buf((const char *)Data.data(), Data.size());
1114Srgrimes  if (zlib::uncompress(Buf, Uncompressed, Hdr->ch_size) != zlib::StatusOK)
1124Srgrimes    fatal("error uncompressing section");
1134Srgrimes}
1144Srgrimes
1154Srgrimestemplate <class ELFT>
1164Srgrimestypename ELFT::uint
1174SrgrimesInputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) const {
1184Srgrimes  return getOffset(Sym.Value);
1194Srgrimes}
12012515Sphk
1214Srgrimestemplate <class ELFT>
1224SrgrimesInputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
1234Srgrimes                                 const Elf_Shdr *Header)
1244Srgrimes    : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
1254Srgrimes
1264Srgrimestemplate <class ELFT>
1274Srgrimesbool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
1284Srgrimes  return S->SectionKind == Base::Regular;
1294Srgrimes}
1304Srgrimes
1314Srgrimestemplate <class ELFT>
1324SrgrimesInputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
1334Srgrimes  assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
1344Srgrimes  ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
1354Srgrimes  return Sections[this->Header->sh_info];
1364Srgrimes}
1374Srgrimes
1384Srgrimestemplate <class ELFT>
1394Srgrimesvoid InputSection<ELFT>::addThunk(const Thunk<ELFT> *T) {
1404Srgrimes  Thunks.push_back(T);
1414Srgrimes}
1424Srgrimes
1434Srgrimestemplate <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const {
1444Srgrimes  return this->Header->sh_size;
14512515Sphk}
1464Srgrimes
1474Srgrimestemplate <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const {
1484Srgrimes  uint64_t Total = 0;
1494Srgrimes  for (const Thunk<ELFT> *T : Thunks)
1504Srgrimes    Total += T->size();
1514Srgrimes  return Total;
1524Srgrimes}
1534Srgrimes
1544Srgrimes// This is used for -r. We can't use memcpy to copy relocations because we need
1554Srgrimes// to update symbol table offset and section index for each relocation. So we
1564Srgrimes// copy relocations one by one.
1574Srgrimestemplate <class ELFT>
1584Srgrimestemplate <class RelTy>
1594Srgrimesvoid InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
1604Srgrimes  InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
1614Srgrimes
1624Srgrimes  for (const RelTy &Rel : Rels) {
1634Srgrimes    uint32_t Type = Rel.getType(Config->Mips64EL);
1644Srgrimes    SymbolBody &Body = this->File->getRelocTargetSym(Rel);
1654Srgrimes
1664Srgrimes    RelTy *P = reinterpret_cast<RelTy *>(Buf);
1674Srgrimes    Buf += sizeof(RelTy);
1684Srgrimes
1694Srgrimes    P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
17012515Sphk    P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
1714Srgrimes  }
1724Srgrimes}
1734Srgrimes
1744Srgrimes// Page(Expr) is the page address of the expression Expr, defined
1754Srgrimes// as (Expr & ~0xFFF). (This applies even if the machine page size
1764Srgrimes// supported by the platform has a different value.)
1774Srgrimesstatic uint64_t getAArch64Page(uint64_t Expr) {
1784Srgrimes  return Expr & (~static_cast<uint64_t>(0xFFF));
1794Srgrimes}
1804Srgrimes
1814Srgrimestemplate <class ELFT>
1824Srgrimesstatic typename ELFT::uint getSymVA(uint32_t Type, typename ELFT::uint A,
1834Srgrimes                                    typename ELFT::uint P,
1844Srgrimes                                    const SymbolBody &Body, RelExpr Expr) {
1854Srgrimes  typedef typename ELFT::uint uintX_t;
1864Srgrimes
1874Srgrimes  switch (Expr) {
1884Srgrimes  case R_HINT:
1894Srgrimes    llvm_unreachable("cannot relocate hint relocs");
1904Srgrimes  case R_TLSLD:
1914Srgrimes    return Out<ELFT>::Got->getTlsIndexOff() + A -
1924Srgrimes           Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
1934Srgrimes  case R_TLSLD_PC:
1944Srgrimes    return Out<ELFT>::Got->getTlsIndexVA() + A - P;
1954Srgrimes  case R_THUNK_ABS:
1964Srgrimes    return Body.getThunkVA<ELFT>() + A;
1974Srgrimes  case R_THUNK_PC:
1984Srgrimes  case R_THUNK_PLT_PC:
1994Srgrimes    return Body.getThunkVA<ELFT>() + A - P;
2004Srgrimes  case R_PPC_TOC:
2014Srgrimes    return getPPC64TocBase() + A;
2024Srgrimes  case R_TLSGD:
2034Srgrimes    return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
2044Srgrimes           Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
2054Srgrimes  case R_TLSGD_PC:
2064Srgrimes    return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
2074Srgrimes  case R_TLSDESC:
2084Srgrimes    return Out<ELFT>::Got->getGlobalDynAddr(Body) + A;
2094Srgrimes  case R_TLSDESC_PAGE:
2104Srgrimes    return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) -
21136735Sdfr           getAArch64Page(P);
2124Srgrimes  case R_PLT:
2134Srgrimes    return Body.getPltVA<ELFT>() + A;
2144Srgrimes  case R_PLT_PC:
2154Srgrimes  case R_PPC_PLT_OPD:
2164Srgrimes    return Body.getPltVA<ELFT>() + A - P;
21736735Sdfr  case R_SIZE:
2184Srgrimes    return Body.getSize<ELFT>() + A;
2194Srgrimes  case R_GOTREL:
2204Srgrimes    return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
2214Srgrimes  case R_RELAX_TLS_GD_TO_IE_END:
2224Srgrimes  case R_GOT_FROM_END:
2234Srgrimes    return Body.getGotOffset<ELFT>() + A -
2244Srgrimes           Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
2254Srgrimes  case R_RELAX_TLS_GD_TO_IE_ABS:
2264Srgrimes  case R_GOT:
2274Srgrimes    return Body.getGotVA<ELFT>() + A;
2284Srgrimes  case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
2294Srgrimes  case R_GOT_PAGE_PC:
2304Srgrimes    return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
2314Srgrimes  case R_RELAX_TLS_GD_TO_IE:
2324Srgrimes  case R_GOT_PC:
2334Srgrimes    return Body.getGotVA<ELFT>() + A - P;
2344Srgrimes  case R_GOTONLY_PC:
2354Srgrimes    return Out<ELFT>::Got->getVA() + A - P;
2364Srgrimes  case R_RELAX_TLS_LD_TO_LE:
2374Srgrimes  case R_RELAX_TLS_IE_TO_LE:
2384Srgrimes  case R_RELAX_TLS_GD_TO_LE:
2394Srgrimes  case R_TLS:
24036735Sdfr    if (Target->TcbSize)
2414Srgrimes      return Body.getVA<ELFT>(A) +
2424Srgrimes             alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);
2434Srgrimes    return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
2444Srgrimes  case R_RELAX_TLS_GD_TO_LE_NEG:
2454Srgrimes  case R_NEG_TLS:
2464Srgrimes    return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
24736735Sdfr  case R_ABS:
24812515Sphk  case R_RELAX_GOT_PC_NOPIC:
2494Srgrimes    return Body.getVA<ELFT>(A);
2504Srgrimes  case R_GOT_OFF:
2514Srgrimes    return Body.getGotOffset<ELFT>() + A;
2524Srgrimes  case R_MIPS_GOT_LOCAL_PAGE:
25312515Sphk    // If relocation against MIPS local symbol requires GOT entry, this entry
2544Srgrimes    // should be initialized by 'page address'. This address is high 16-bits
2554Srgrimes    // of sum the symbol's value and the addend.
2564Srgrimes    return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A));
2574Srgrimes  case R_MIPS_GOT_OFF:
2584Srgrimes    // In case of MIPS if a GOT relocation has non-zero addend this addend
2594Srgrimes    // should be applied to the GOT entry content not to the GOT entry offset.
2604Srgrimes    // That is why we use separate expression type.
2614Srgrimes    return Out<ELFT>::Got->getMipsGotOffset(Body, A);
2624Srgrimes  case R_MIPS_TLSGD:
2634Srgrimes    return Out<ELFT>::Got->getGlobalDynOffset(Body) +
2644Srgrimes           Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset;
2654Srgrimes  case R_MIPS_TLSLD:
2664Srgrimes    return Out<ELFT>::Got->getTlsIndexOff() +
2674Srgrimes           Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset;
2684Srgrimes  case R_PPC_OPD: {
2694Srgrimes    uint64_t SymVA = Body.getVA<ELFT>(A);
2704Srgrimes    // If we have an undefined weak symbol, we might get here with a symbol
2714Srgrimes    // address of zero. That could overflow, but the code must be unreachable,
2724Srgrimes    // so don't bother doing anything at all.
2734Srgrimes    if (!SymVA)
2744Srgrimes      return 0;
2754Srgrimes    if (Out<ELF64BE>::Opd) {
2764Srgrimes      // If this is a local call, and we currently have the address of a
2774Srgrimes      // function-descriptor, get the underlying code address instead.
2784Srgrimes      uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
2794Srgrimes      uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
2804Srgrimes      bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
28112473Sbde      if (InOpd)
2824Srgrimes        SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
2834Srgrimes    }
2844Srgrimes    return SymVA - P;
2854Srgrimes  }
2864Srgrimes  case R_PC:
2874Srgrimes  case R_RELAX_GOT_PC:
2884Srgrimes    return Body.getVA<ELFT>(A) - P;
2894Srgrimes  case R_PLT_PAGE_PC:
2904Srgrimes  case R_PAGE_PC:
2914Srgrimes    return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
2924Srgrimes  }
29312473Sbde  llvm_unreachable("Invalid expression");
2944Srgrimes}
2954Srgrimes
2964Srgrimes// This function applies relocations to sections without SHF_ALLOC bit.
2974Srgrimes// Such sections are never mapped to memory at runtime. Debug sections are
2984Srgrimes// an example. Relocations in non-alloc sections are much easier to
2994Srgrimes// handle than in allocated sections because it will never need complex
3004Srgrimes// treatement such as GOT or PLT (because at runtime no one refers them).
3014Srgrimes// So, we handle relocations for non-alloc sections directly in this
3024Srgrimes// function as a performance optimization.
3034Srgrimestemplate <class ELFT>
3044Srgrimestemplate <class RelTy>
30512473Sbdevoid InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
30612473Sbde  const unsigned Bits = sizeof(uintX_t) * 8;
30712473Sbde  for (const RelTy &Rel : Rels) {
30812473Sbde    uint32_t Type = Rel.getType(Config->Mips64EL);
30912473Sbde    uintX_t Offset = this->getOffset(Rel.r_offset);
3104Srgrimes    uint8_t *BufLoc = Buf + Offset;
3114Srgrimes    uintX_t Addend = getAddend<ELFT>(Rel);
3124Srgrimes    if (!RelTy::IsRela)
3134Srgrimes      Addend += Target->getImplicitAddend(BufLoc, Type);
3144Srgrimes
3154Srgrimes    SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
3164Srgrimes    if (Target->getRelExpr(Type, Sym) != R_ABS) {
3174Srgrimes      error(this->getSectionName() + " has non-ABS reloc");
3184Srgrimes      return;
3194Srgrimes    }
3204Srgrimes
3214Srgrimes    uintX_t AddrLoc = this->OutSec->getVA() + Offset;
3224Srgrimes    uint64_t SymVA =
3234Srgrimes        SignExtend64<Bits>(getSymVA<ELFT>(Type, Addend, AddrLoc, Sym, R_ABS));
3244Srgrimes    Target->relocateOne(BufLoc, Type, SymVA);
3254Srgrimes  }
3264Srgrimes}
3274Srgrimes
3284Srgrimestemplate <class ELFT>
3294Srgrimesvoid InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
3304Srgrimes  // scanReloc function in Writer.cpp constructs Relocations
3314Srgrimes  // vector only for SHF_ALLOC'ed sections. For other sections,
3324Srgrimes  // we handle relocations directly here.
3334Srgrimes  auto *IS = dyn_cast<InputSection<ELFT>>(this);
3344Srgrimes  if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
3354Srgrimes    for (const Elf_Shdr *RelSec : IS->RelocSections) {
3364Srgrimes      if (RelSec->sh_type == SHT_RELA)
3374Srgrimes        IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
3384Srgrimes      else
3394Srgrimes        IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
3404Srgrimes    }
3414Srgrimes    return;
3424Srgrimes  }
3434Srgrimes
3444Srgrimes  const unsigned Bits = sizeof(uintX_t) * 8;
3454Srgrimes  for (const Relocation<ELFT> &Rel : Relocations) {
3464Srgrimes    uintX_t Offset = Rel.InputSec->getOffset(Rel.Offset);
3474Srgrimes    uint8_t *BufLoc = Buf + Offset;
3484Srgrimes    uint32_t Type = Rel.Type;
3494Srgrimes    uintX_t A = Rel.Addend;
3504Srgrimes
3514Srgrimes    uintX_t AddrLoc = OutSec->getVA() + Offset;
3524Srgrimes    RelExpr Expr = Rel.Expr;
3534Srgrimes    uint64_t SymVA =
3544Srgrimes        SignExtend64<Bits>(getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, Expr));
3554Srgrimes
3564Srgrimes    switch (Expr) {
3574Srgrimes    case R_RELAX_GOT_PC:
3584Srgrimes    case R_RELAX_GOT_PC_NOPIC:
3594Srgrimes      Target->relaxGot(BufLoc, SymVA);
3604Srgrimes      break;
3614Srgrimes    case R_RELAX_TLS_IE_TO_LE:
3624Srgrimes      Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
3634Srgrimes      break;
3644Srgrimes    case R_RELAX_TLS_LD_TO_LE:
3654Srgrimes      Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
366      break;
367    case R_RELAX_TLS_GD_TO_LE:
368    case R_RELAX_TLS_GD_TO_LE_NEG:
369      Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
370      break;
371    case R_RELAX_TLS_GD_TO_IE:
372    case R_RELAX_TLS_GD_TO_IE_ABS:
373    case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
374    case R_RELAX_TLS_GD_TO_IE_END:
375      Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
376      break;
377    case R_PPC_PLT_OPD:
378      // Patch a nop (0x60000000) to a ld.
379      if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000)
380        write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
381      // fallthrough
382    default:
383      Target->relocateOne(BufLoc, Type, SymVA);
384      break;
385    }
386  }
387}
388
389template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
390  if (this->Header->sh_type == SHT_NOBITS)
391    return;
392  ELFFile<ELFT> &EObj = this->File->getObj();
393
394  // If -r is given, then an InputSection may be a relocation section.
395  if (this->Header->sh_type == SHT_RELA) {
396    copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
397    return;
398  }
399  if (this->Header->sh_type == SHT_REL) {
400    copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
401    return;
402  }
403
404  // Copy section contents from source object file to output file.
405  ArrayRef<uint8_t> Data = this->getSectionData();
406  memcpy(Buf + OutSecOff, Data.data(), Data.size());
407
408  // Iterate over all relocation sections that apply to this section.
409  uint8_t *BufEnd = Buf + OutSecOff + Data.size();
410  this->relocate(Buf, BufEnd);
411
412  // The section might have a data/code generated by the linker and need
413  // to be written after the section. Usually these are thunks - small piece
414  // of code used to jump between "incompatible" functions like PIC and non-PIC
415  // or if the jump target too far and its address does not fit to the short
416  // jump istruction.
417  if (!Thunks.empty()) {
418    Buf += OutSecOff + getThunkOff();
419    for (const Thunk<ELFT> *T : Thunks) {
420      T->writeTo(Buf);
421      Buf += T->size();
422    }
423  }
424}
425
426template <class ELFT>
427void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
428  this->Alignment = std::max(this->Alignment, Other->Alignment);
429  Other->Repl = this->Repl;
430  Other->Live = false;
431}
432
433template <class ELFT>
434SplitInputSection<ELFT>::SplitInputSection(
435    elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
436    typename InputSectionBase<ELFT>::Kind SectionKind)
437    : InputSectionBase<ELFT>(File, Header, SectionKind) {}
438
439template <class ELFT>
440EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F,
441                                     const Elf_Shdr *Header)
442    : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
443  // Mark .eh_frame sections as live by default because there are
444  // usually no relocations that point to .eh_frames. Otherwise,
445  // the garbage collector would drop all .eh_frame sections.
446  this->Live = true;
447}
448
449template <class ELFT>
450bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
451  return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
452}
453
454// .eh_frame is a sequence of CIE or FDE records.
455// This function splits an input section into records and returns them.
456template <class ELFT>
457void EhInputSection<ELFT>::split() {
458  ArrayRef<uint8_t> Data = this->getSectionData();
459  for (size_t Off = 0, End = Data.size(); Off != End;) {
460    size_t Size = readEhRecordSize<ELFT>(Data.slice(Off));
461    this->Pieces.emplace_back(Off, Data.slice(Off, Size));
462    // The empty record is the end marker.
463    if (Size == 4)
464      break;
465    Off += Size;
466  }
467}
468
469template <class ELFT>
470typename ELFT::uint EhInputSection<ELFT>::getOffset(uintX_t Offset) const {
471  // The file crtbeginT.o has relocations pointing to the start of an empty
472  // .eh_frame that is known to be the first in the link. It does that to
473  // identify the start of the output .eh_frame. Handle this special case.
474  if (this->getSectionHdr()->sh_size == 0)
475    return Offset;
476  const SectionPiece *Piece = this->getSectionPiece(Offset);
477  if (Piece->OutputOff == size_t(-1))
478    return -1; // Not in the output
479
480  uintX_t Addend = Offset - Piece->InputOff;
481  return Piece->OutputOff + Addend;
482}
483
484static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
485  // Optimize the common case.
486  StringRef S((const char *)A.data(), A.size());
487  if (EntSize == 1)
488    return S.find(0);
489
490  for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
491    const char *B = S.begin() + I;
492    if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
493      return I;
494  }
495  return StringRef::npos;
496}
497
498// Split SHF_STRINGS section. Such section is a sequence of
499// null-terminated strings.
500static std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> Data,
501                                              size_t EntSize) {
502  std::vector<SectionPiece> V;
503  size_t Off = 0;
504  while (!Data.empty()) {
505    size_t End = findNull(Data, EntSize);
506    if (End == StringRef::npos)
507      fatal("string is not null terminated");
508    size_t Size = End + EntSize;
509    V.emplace_back(Off, Data.slice(0, Size));
510    Data = Data.slice(Size);
511    Off += Size;
512  }
513  return V;
514}
515
516// Split non-SHF_STRINGS section. Such section is a sequence of
517// fixed size records.
518static std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> Data,
519                                                 size_t EntSize) {
520  std::vector<SectionPiece> V;
521  size_t Size = Data.size();
522  assert((Size % EntSize) == 0);
523  for (unsigned I = 0, N = Size; I != N; I += EntSize)
524    V.emplace_back(I, Data.slice(I, EntSize));
525  return V;
526}
527
528template <class ELFT>
529MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
530                                           const Elf_Shdr *Header)
531    : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
532
533template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
534  ArrayRef<uint8_t> Data = this->getSectionData();
535  uintX_t EntSize = this->Header->sh_entsize;
536  if (this->Header->sh_flags & SHF_STRINGS)
537    this->Pieces = splitStrings(Data, EntSize);
538  else
539    this->Pieces = splitNonStrings(Data, EntSize);
540
541  if (Config->GcSections)
542    for (uintX_t Off : LiveOffsets)
543      this->getSectionPiece(Off)->Live = true;
544}
545
546template <class ELFT>
547bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
548  return S->SectionKind == InputSectionBase<ELFT>::Merge;
549}
550
551// Do binary search to get a section piece at a given input offset.
552template <class ELFT>
553SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
554  auto *This = static_cast<const SplitInputSection<ELFT> *>(this);
555  return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
556}
557
558template <class ELFT>
559const SectionPiece *
560SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) const {
561  ArrayRef<uint8_t> D = this->getSectionData();
562  StringRef Data((const char *)D.data(), D.size());
563  uintX_t Size = Data.size();
564  if (Offset >= Size)
565    fatal("entry is past the end of the section");
566
567  // Find the element this offset points to.
568  auto I = std::upper_bound(
569      Pieces.begin(), Pieces.end(), Offset,
570      [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
571  --I;
572  return &*I;
573}
574
575// Returns the offset in an output section for a given input offset.
576// Because contents of a mergeable section is not contiguous in output,
577// it is not just an addition to a base output offset.
578template <class ELFT>
579typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) const {
580  auto It = OffsetMap.find(Offset);
581  if (It != OffsetMap.end())
582    return It->second;
583
584  // If Offset is not at beginning of a section piece, it is not in the map.
585  // In that case we need to search from the original section piece vector.
586  const SectionPiece &Piece = *this->getSectionPiece(Offset);
587  assert(Piece.Live);
588  uintX_t Addend = Offset - Piece.InputOff;
589  return Piece.OutputOff + Addend;
590}
591
592// Create a map from input offsets to output offsets for all section pieces.
593// It is called after finalize().
594template <class ELFT> void  MergeInputSection<ELFT>::finalizePieces() {
595  OffsetMap.grow(this->Pieces.size());
596  for (SectionPiece &Piece : this->Pieces) {
597    if (!Piece.Live)
598      continue;
599    if (Piece.OutputOff == size_t(-1)) {
600      // Offsets of tail-merged strings are computed lazily.
601      auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec);
602      ArrayRef<uint8_t> D = Piece.data();
603      StringRef S((const char *)D.data(), D.size());
604      Piece.OutputOff = OutSec->getOffset(S);
605    }
606    OffsetMap[Piece.InputOff] = Piece.OutputOff;
607  }
608}
609
610template <class ELFT>
611MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
612                                                       const Elf_Shdr *Hdr)
613    : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
614  // Initialize this->Reginfo.
615  ArrayRef<uint8_t> D = this->getSectionData();
616  if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
617    error("invalid size of .reginfo section");
618    return;
619  }
620  Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
621}
622
623template <class ELFT>
624bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
625  return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
626}
627
628template <class ELFT>
629MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F,
630                                                       const Elf_Shdr *Hdr)
631    : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsOptions) {
632  // Find ODK_REGINFO option in the section's content.
633  ArrayRef<uint8_t> D = this->getSectionData();
634  while (!D.empty()) {
635    if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) {
636      error("invalid size of .MIPS.options section");
637      break;
638    }
639    auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data());
640    if (O->kind == ODK_REGINFO) {
641      Reginfo = &O->getRegInfo();
642      break;
643    }
644    D = D.slice(O->size);
645  }
646}
647
648template <class ELFT>
649bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
650  return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
651}
652
653template bool elf::isDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
654template bool elf::isDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
655template bool elf::isDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
656template bool elf::isDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
657
658template class elf::InputSectionBase<ELF32LE>;
659template class elf::InputSectionBase<ELF32BE>;
660template class elf::InputSectionBase<ELF64LE>;
661template class elf::InputSectionBase<ELF64BE>;
662
663template class elf::InputSection<ELF32LE>;
664template class elf::InputSection<ELF32BE>;
665template class elf::InputSection<ELF64LE>;
666template class elf::InputSection<ELF64BE>;
667
668template class elf::SplitInputSection<ELF32LE>;
669template class elf::SplitInputSection<ELF32BE>;
670template class elf::SplitInputSection<ELF64LE>;
671template class elf::SplitInputSection<ELF64BE>;
672
673template class elf::EhInputSection<ELF32LE>;
674template class elf::EhInputSection<ELF32BE>;
675template class elf::EhInputSection<ELF64LE>;
676template class elf::EhInputSection<ELF64BE>;
677
678template class elf::MergeInputSection<ELF32LE>;
679template class elf::MergeInputSection<ELF32BE>;
680template class elf::MergeInputSection<ELF64LE>;
681template class elf::MergeInputSection<ELF64BE>;
682
683template class elf::MipsReginfoInputSection<ELF32LE>;
684template class elf::MipsReginfoInputSection<ELF32BE>;
685template class elf::MipsReginfoInputSection<ELF64LE>;
686template class elf::MipsReginfoInputSection<ELF64BE>;
687
688template class elf::MipsOptionsInputSection<ELF32LE>;
689template class elf::MipsOptionsInputSection<ELF32BE>;
690template class elf::MipsOptionsInputSection<ELF64LE>;
691template class elf::MipsOptionsInputSection<ELF64BE>;
692