summaryrefslogtreecommitdiff
path: root/ELF/InputSection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ELF/InputSection.cpp')
-rw-r--r--ELF/InputSection.cpp779
1 files changed, 537 insertions, 242 deletions
diff --git a/ELF/InputSection.cpp b/ELF/InputSection.cpp
index f6aa51b47b98..6564e7995a89 100644
--- a/ELF/InputSection.cpp
+++ b/ELF/InputSection.cpp
@@ -9,86 +9,118 @@
#include "InputSection.h"
#include "Config.h"
+#include "EhFrame.h"
#include "Error.h"
#include "InputFiles.h"
+#include "LinkerScript.h"
#include "OutputSections.h"
#include "Target.h"
+#include "Thunks.h"
+
+#include "llvm/Support/Compression.h"
+#include "llvm/Support/Endian.h"
using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::object;
+using namespace llvm::support::endian;
using namespace lld;
-using namespace lld::elf2;
+using namespace lld::elf;
+
+template <class ELFT> bool elf::isDiscarded(InputSectionBase<ELFT> *S) {
+ return !S || S == &InputSection<ELFT>::Discarded || !S->Live ||
+ Script<ELFT>::X->isDiscarded(S);
+}
template <class ELFT>
-InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
+InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
const Elf_Shdr *Header,
Kind SectionKind)
- : Header(Header), File(File), SectionKind(SectionKind) {}
+ : Header(Header), File(File), SectionKind(SectionKind), Repl(this),
+ Compressed(Header->sh_flags & SHF_COMPRESSED) {
+ // The garbage collector sets sections' Live bits.
+ // If GC is disabled, all sections are considered live by default.
+ Live = !Config->GcSections;
+
+ // The ELF spec states that a value of 0 means the section has
+ // no alignment constraits.
+ Alignment = std::max<uintX_t>(Header->sh_addralign, 1);
+}
+
+template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
+ if (auto *D = dyn_cast<InputSection<ELFT>>(this))
+ if (D->getThunksSize() > 0)
+ return D->getThunkOff() + D->getThunksSize();
+ return Header->sh_size;
+}
template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
- ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
- error(Name);
- return *Name;
+ return check(File->getObj().getSectionName(this->Header));
}
template <class ELFT>
ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
- ErrorOr<ArrayRef<uint8_t>> Ret =
- this->File->getObj().getSectionContents(this->Header);
- error(Ret);
- return *Ret;
+ if (Compressed)
+ return ArrayRef<uint8_t>((const uint8_t *)Uncompressed.data(),
+ Uncompressed.size());
+ return check(this->File->getObj().getSectionContents(this->Header));
}
template <class ELFT>
-typename ELFFile<ELFT>::uintX_t
-InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
+typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) const {
switch (SectionKind) {
case Regular:
return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
case EHFrame:
- return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
+ return cast<EhInputSection<ELFT>>(this)->getOffset(Offset);
case Merge:
return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
case MipsReginfo:
- // MIPS .reginfo sections are consumed by the linker,
- // so it should never be copied to output.
- llvm_unreachable("MIPS .reginfo reached writeTo().");
+ case MipsOptions:
+ // MIPS .reginfo and .MIPS.options sections are consumed by the linker,
+ // and the linker produces a single output section. It is possible that
+ // input files contain section symbol points to the corresponding input
+ // section. Redirect it to the produced output section.
+ if (Offset != 0)
+ fatal("Unsupported reference to the middle of '" + getSectionName() +
+ "' section");
+ return this->OutSec->getVA();
}
- llvm_unreachable("Invalid section kind");
+ llvm_unreachable("invalid section kind");
}
-template <class ELFT>
-typename ELFFile<ELFT>::uintX_t
-InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
- return getOffset(Sym.st_value);
-}
+template <class ELFT> void InputSectionBase<ELFT>::uncompress() {
+ if (!zlib::isAvailable())
+ fatal("build lld with zlib to enable compressed sections support");
-// Returns a section that Rel relocation is pointing to.
-template <class ELFT>
-InputSectionBase<ELFT> *
-InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) {
- // Global symbol
- uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
- if (SymbolBody *B = File->getSymbolBody(SymIndex))
- if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
- return D->Section;
- // Local symbol
- if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
- if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
- return Sec;
- return nullptr;
+ // A compressed section consists of a header of Elf_Chdr type
+ // followed by compressed data.
+ ArrayRef<uint8_t> Data =
+ check(this->File->getObj().getSectionContents(this->Header));
+ if (Data.size() < sizeof(Elf_Chdr))
+ fatal("corrupt compressed section");
+
+ auto *Hdr = reinterpret_cast<const Elf_Chdr *>(Data.data());
+ Data = Data.slice(sizeof(Elf_Chdr));
+
+ if (Hdr->ch_type != ELFCOMPRESS_ZLIB)
+ fatal("unsupported compression type");
+
+ StringRef Buf((const char *)Data.data(), Data.size());
+ if (zlib::uncompress(Buf, Uncompressed, Hdr->ch_size) != zlib::StatusOK)
+ fatal("error uncompressing section");
}
template <class ELFT>
-InputSectionBase<ELFT> *
-InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) {
- return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
+typename ELFT::uint
+InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) const {
+ return getOffset(Sym.Value);
}
template <class ELFT>
-InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
+InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
+ const Elf_Shdr *Header)
: InputSectionBase<ELFT>(F, Header, Base::Regular) {}
template <class ELFT>
@@ -97,267 +129,494 @@ bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
}
template <class ELFT>
-template <bool isRela>
-uint8_t *
-InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex,
- uint32_t Type,
- RelIteratorRange<isRela> Rels) {
- // Some MIPS relocations use addend calculated from addend of the relocation
- // itself and addend of paired relocation. ABI requires to compute such
- // combined addend in case of REL relocation record format only.
- // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
- if (isRela || Config->EMachine != EM_MIPS)
- return nullptr;
- if (Type == R_MIPS_HI16)
- Type = R_MIPS_LO16;
- else if (Type == R_MIPS_PCHI16)
- Type = R_MIPS_PCLO16;
- else if (Type == R_MICROMIPS_HI16)
- Type = R_MICROMIPS_LO16;
- else
- return nullptr;
- for (const auto &RI : Rels) {
- if (RI.getType(Config->Mips64EL) != Type)
- continue;
- if (RI.getSymbol(Config->Mips64EL) != SymIndex)
- continue;
- uintX_t Offset = getOffset(RI.r_offset);
- if (Offset == (uintX_t)-1)
- return nullptr;
- return Buf + Offset;
- }
- return nullptr;
+InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
+ assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
+ ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
+ return Sections[this->Header->sh_info];
}
template <class ELFT>
-static typename llvm::object::ELFFile<ELFT>::uintX_t
-getSymSize(SymbolBody &Body) {
- if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&Body))
- return SS->Sym.st_size;
- return 0;
+void InputSection<ELFT>::addThunk(const Thunk<ELFT> *T) {
+ Thunks.push_back(T);
+}
+
+template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const {
+ return this->Header->sh_size;
}
+template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const {
+ uint64_t Total = 0;
+ for (const Thunk<ELFT> *T : Thunks)
+ Total += T->size();
+ return Total;
+}
+
+// This is used for -r. We can't use memcpy to copy relocations because we need
+// to update symbol table offset and section index for each relocation. So we
+// copy relocations one by one.
template <class ELFT>
-template <bool isRela>
-void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
- RelIteratorRange<isRela> Rels) {
- typedef Elf_Rel_Impl<ELFT, isRela> RelType;
- size_t Num = Rels.end() - Rels.begin();
- for (size_t I = 0; I < Num; ++I) {
- const RelType &RI = *(Rels.begin() + I);
- uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
- uint32_t Type = RI.getType(Config->Mips64EL);
- uintX_t Offset = getOffset(RI.r_offset);
- if (Offset == (uintX_t)-1)
- continue;
+template <class RelTy>
+void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
+ InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
- uint8_t *BufLoc = Buf + Offset;
- uintX_t AddrLoc = OutSec->getVA() + Offset;
- auto NextRelocs = llvm::make_range(&RI, Rels.end());
+ for (const RelTy &Rel : Rels) {
+ uint32_t Type = Rel.getType(Config->Mips64EL);
+ SymbolBody &Body = this->File->getRelocTargetSym(Rel);
- if (Target->isTlsLocalDynamicReloc(Type) &&
- !Target->isTlsOptimized(Type, nullptr)) {
- Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
- Out<ELFT>::Got->getLocalTlsIndexVA() +
- getAddend<ELFT>(RI));
- continue;
- }
+ RelTy *P = reinterpret_cast<RelTy *>(Buf);
+ Buf += sizeof(RelTy);
- const Elf_Shdr *SymTab = File->getSymbolTable();
- SymbolBody *Body = nullptr;
- if (SymIndex >= SymTab->sh_info)
- Body = File->getSymbolBody(SymIndex)->repl();
-
- if (Target->isTlsOptimized(Type, Body)) {
- uintX_t SymVA;
- if (!Body)
- SymVA = getLocalRelTarget(*File, RI, 0);
- else if (Target->relocNeedsGot(Type, *Body))
- SymVA = Out<ELFT>::Got->getEntryAddr(*Body);
- else
- SymVA = getSymVA<ELFT>(*Body);
- // By optimizing TLS relocations, it is sometimes needed to skip
- // relocations that immediately follow TLS relocations. This function
- // knows how many slots we need to skip.
- I += Target->relocateTlsOptimize(BufLoc, BufEnd, Type, AddrLoc, SymVA,
- *Body);
- continue;
+ P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
+ P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
+ }
+}
+
+// Page(Expr) is the page address of the expression Expr, defined
+// as (Expr & ~0xFFF). (This applies even if the machine page size
+// supported by the platform has a different value.)
+static uint64_t getAArch64Page(uint64_t Expr) {
+ return Expr & (~static_cast<uint64_t>(0xFFF));
+}
+
+template <class ELFT>
+static typename ELFT::uint getSymVA(uint32_t Type, typename ELFT::uint A,
+ typename ELFT::uint P,
+ const SymbolBody &Body, RelExpr Expr) {
+ typedef typename ELFT::uint uintX_t;
+
+ switch (Expr) {
+ case R_HINT:
+ llvm_unreachable("cannot relocate hint relocs");
+ case R_TLSLD:
+ return Out<ELFT>::Got->getTlsIndexOff() + A -
+ Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
+ case R_TLSLD_PC:
+ return Out<ELFT>::Got->getTlsIndexVA() + A - P;
+ case R_THUNK_ABS:
+ return Body.getThunkVA<ELFT>() + A;
+ case R_THUNK_PC:
+ case R_THUNK_PLT_PC:
+ return Body.getThunkVA<ELFT>() + A - P;
+ case R_PPC_TOC:
+ return getPPC64TocBase() + A;
+ case R_TLSGD:
+ return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
+ Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
+ case R_TLSGD_PC:
+ return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
+ case R_TLSDESC:
+ return Out<ELFT>::Got->getGlobalDynAddr(Body) + A;
+ case R_TLSDESC_PAGE:
+ return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) -
+ getAArch64Page(P);
+ case R_PLT:
+ return Body.getPltVA<ELFT>() + A;
+ case R_PLT_PC:
+ case R_PPC_PLT_OPD:
+ return Body.getPltVA<ELFT>() + A - P;
+ case R_SIZE:
+ return Body.getSize<ELFT>() + A;
+ case R_GOTREL:
+ return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
+ case R_RELAX_TLS_GD_TO_IE_END:
+ case R_GOT_FROM_END:
+ return Body.getGotOffset<ELFT>() + A -
+ Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
+ case R_RELAX_TLS_GD_TO_IE_ABS:
+ case R_GOT:
+ return Body.getGotVA<ELFT>() + A;
+ case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
+ case R_GOT_PAGE_PC:
+ return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
+ case R_RELAX_TLS_GD_TO_IE:
+ case R_GOT_PC:
+ return Body.getGotVA<ELFT>() + A - P;
+ case R_GOTONLY_PC:
+ return Out<ELFT>::Got->getVA() + A - P;
+ case R_RELAX_TLS_LD_TO_LE:
+ case R_RELAX_TLS_IE_TO_LE:
+ case R_RELAX_TLS_GD_TO_LE:
+ case R_TLS:
+ if (Target->TcbSize)
+ return Body.getVA<ELFT>(A) +
+ alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);
+ return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
+ case R_RELAX_TLS_GD_TO_LE_NEG:
+ case R_NEG_TLS:
+ return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
+ case R_ABS:
+ case R_RELAX_GOT_PC_NOPIC:
+ return Body.getVA<ELFT>(A);
+ case R_GOT_OFF:
+ return Body.getGotOffset<ELFT>() + A;
+ case R_MIPS_GOT_LOCAL_PAGE:
+ // If relocation against MIPS local symbol requires GOT entry, this entry
+ // should be initialized by 'page address'. This address is high 16-bits
+ // of sum the symbol's value and the addend.
+ return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A));
+ case R_MIPS_GOT_OFF:
+ // In case of MIPS if a GOT relocation has non-zero addend this addend
+ // should be applied to the GOT entry content not to the GOT entry offset.
+ // That is why we use separate expression type.
+ return Out<ELFT>::Got->getMipsGotOffset(Body, A);
+ case R_MIPS_TLSGD:
+ return Out<ELFT>::Got->getGlobalDynOffset(Body) +
+ Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset;
+ case R_MIPS_TLSLD:
+ return Out<ELFT>::Got->getTlsIndexOff() +
+ Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset;
+ case R_PPC_OPD: {
+ uint64_t SymVA = Body.getVA<ELFT>(A);
+ // If we have an undefined weak symbol, we might get here with a symbol
+ // address of zero. That could overflow, but the code must be unreachable,
+ // so don't bother doing anything at all.
+ if (!SymVA)
+ return 0;
+ if (Out<ELF64BE>::Opd) {
+ // If this is a local call, and we currently have the address of a
+ // function-descriptor, get the underlying code address instead.
+ uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
+ uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
+ bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
+ if (InOpd)
+ SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
}
+ return SymVA - P;
+ }
+ case R_PC:
+ case R_RELAX_GOT_PC:
+ return Body.getVA<ELFT>(A) - P;
+ case R_PLT_PAGE_PC:
+ case R_PAGE_PC:
+ return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
+ }
+ llvm_unreachable("Invalid expression");
+}
- // Handle relocations for local symbols -- they never get
- // resolved so we don't allocate a SymbolBody.
- uintX_t A = getAddend<ELFT>(RI);
- if (!Body) {
- uintX_t SymVA = getLocalRelTarget(*File, RI, A);
- // We need to adjust SymVA value in case of R_MIPS_GPREL16/32 relocations
- // because they use the following expression to calculate the relocation's
- // result for local symbol: S + A + GP0 - G.
- if (Config->EMachine == EM_MIPS &&
- (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32))
- SymVA += File->getMipsGp0();
- Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0,
- findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
- continue;
+// This function applies relocations to sections without SHF_ALLOC bit.
+// Such sections are never mapped to memory at runtime. Debug sections are
+// an example. Relocations in non-alloc sections are much easier to
+// handle than in allocated sections because it will never need complex
+// treatement such as GOT or PLT (because at runtime no one refers them).
+// So, we handle relocations for non-alloc sections directly in this
+// function as a performance optimization.
+template <class ELFT>
+template <class RelTy>
+void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
+ const unsigned Bits = sizeof(uintX_t) * 8;
+ for (const RelTy &Rel : Rels) {
+ uint32_t Type = Rel.getType(Config->Mips64EL);
+ uintX_t Offset = this->getOffset(Rel.r_offset);
+ uint8_t *BufLoc = Buf + Offset;
+ uintX_t Addend = getAddend<ELFT>(Rel);
+ if (!RelTy::IsRela)
+ Addend += Target->getImplicitAddend(BufLoc, Type);
+
+ SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
+ if (Target->getRelExpr(Type, Sym) != R_ABS) {
+ error(this->getSectionName() + " has non-ABS reloc");
+ return;
}
- if (Target->isTlsGlobalDynamicReloc(Type) &&
- !Target->isTlsOptimized(Type, Body)) {
- Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
- Out<ELFT>::Got->getGlobalDynAddr(*Body) +
- getAddend<ELFT>(RI));
- continue;
+ uintX_t AddrLoc = this->OutSec->getVA() + Offset;
+ uint64_t SymVA =
+ SignExtend64<Bits>(getSymVA<ELFT>(Type, Addend, AddrLoc, Sym, R_ABS));
+ Target->relocateOne(BufLoc, Type, SymVA);
+ }
+}
+
+template <class ELFT>
+void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
+ // scanReloc function in Writer.cpp constructs Relocations
+ // vector only for SHF_ALLOC'ed sections. For other sections,
+ // we handle relocations directly here.
+ auto *IS = dyn_cast<InputSection<ELFT>>(this);
+ if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
+ for (const Elf_Shdr *RelSec : IS->RelocSections) {
+ if (RelSec->sh_type == SHT_RELA)
+ IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
+ else
+ IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
}
+ return;
+ }
- uintX_t SymVA = getSymVA<ELFT>(*Body);
- if (Target->relocNeedsPlt(Type, *Body)) {
- SymVA = Out<ELFT>::Plt->getEntryAddr(*Body);
- } else if (Target->relocNeedsGot(Type, *Body)) {
- SymVA = Out<ELFT>::Got->getEntryAddr(*Body);
- if (Body->isTls())
- Type = Target->getTlsGotReloc(Type);
- } else if (!Target->needsCopyRel(Type, *Body) &&
- isa<SharedSymbol<ELFT>>(*Body)) {
- continue;
- } else if (Target->isTlsDynReloc(Type, *Body)) {
- continue;
- } else if (Target->isSizeReloc(Type) && canBePreempted(Body, false)) {
- // A SIZE relocation is supposed to set a symbol size, but if a symbol
- // can be preempted, the size at runtime may be different than link time.
- // If that's the case, we leave the field alone rather than filling it
- // with a possibly incorrect value.
- continue;
- } else if (Config->EMachine == EM_MIPS) {
- if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp)
- SymVA = getMipsGpAddr<ELFT>() - AddrLoc;
- else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp)
- SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4;
+ const unsigned Bits = sizeof(uintX_t) * 8;
+ for (const Relocation<ELFT> &Rel : Relocations) {
+ uintX_t Offset = Rel.InputSec->getOffset(Rel.Offset);
+ uint8_t *BufLoc = Buf + Offset;
+ uint32_t Type = Rel.Type;
+ uintX_t A = Rel.Addend;
+
+ uintX_t AddrLoc = OutSec->getVA() + Offset;
+ RelExpr Expr = Rel.Expr;
+ uint64_t SymVA =
+ SignExtend64<Bits>(getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, Expr));
+
+ switch (Expr) {
+ case R_RELAX_GOT_PC:
+ case R_RELAX_GOT_PC_NOPIC:
+ Target->relaxGot(BufLoc, SymVA);
+ break;
+ case R_RELAX_TLS_IE_TO_LE:
+ Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
+ break;
+ case R_RELAX_TLS_LD_TO_LE:
+ Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
+ break;
+ case R_RELAX_TLS_GD_TO_LE:
+ case R_RELAX_TLS_GD_TO_LE_NEG:
+ Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
+ break;
+ case R_RELAX_TLS_GD_TO_IE:
+ case R_RELAX_TLS_GD_TO_IE_ABS:
+ case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
+ case R_RELAX_TLS_GD_TO_IE_END:
+ Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
+ break;
+ case R_PPC_PLT_OPD:
+ // Patch a nop (0x60000000) to a ld.
+ if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000)
+ write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
+ // fallthrough
+ default:
+ Target->relocateOne(BufLoc, Type, SymVA);
+ break;
}
- uintX_t Size = getSymSize<ELFT>(*Body);
- Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A,
- findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
}
}
template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
if (this->Header->sh_type == SHT_NOBITS)
return;
+ ELFFile<ELFT> &EObj = this->File->getObj();
+
+ // If -r is given, then an InputSection may be a relocation section.
+ if (this->Header->sh_type == SHT_RELA) {
+ copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
+ return;
+ }
+ if (this->Header->sh_type == SHT_REL) {
+ copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
+ return;
+ }
+
// Copy section contents from source object file to output file.
ArrayRef<uint8_t> Data = this->getSectionData();
memcpy(Buf + OutSecOff, Data.data(), Data.size());
- ELFFile<ELFT> &EObj = this->File->getObj();
- uint8_t *BufEnd = Buf + OutSecOff + Data.size();
// Iterate over all relocation sections that apply to this section.
- for (const Elf_Shdr *RelSec : this->RelocSections) {
- if (RelSec->sh_type == SHT_RELA)
- this->relocate(Buf, BufEnd, EObj.relas(RelSec));
- else
- this->relocate(Buf, BufEnd, EObj.rels(RelSec));
+ uint8_t *BufEnd = Buf + OutSecOff + Data.size();
+ this->relocate(Buf, BufEnd);
+
+ // The section might have a data/code generated by the linker and need
+ // to be written after the section. Usually these are thunks - small piece
+ // of code used to jump between "incompatible" functions like PIC and non-PIC
+ // or if the jump target too far and its address does not fit to the short
+ // jump istruction.
+ if (!Thunks.empty()) {
+ Buf += OutSecOff + getThunkOff();
+ for (const Thunk<ELFT> *T : Thunks) {
+ T->writeTo(Buf);
+ Buf += T->size();
+ }
}
}
template <class ELFT>
+void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
+ this->Alignment = std::max(this->Alignment, Other->Alignment);
+ Other->Repl = this->Repl;
+ Other->Live = false;
+}
+
+template <class ELFT>
SplitInputSection<ELFT>::SplitInputSection(
- ObjectFile<ELFT> *File, const Elf_Shdr *Header,
+ elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
typename InputSectionBase<ELFT>::Kind SectionKind)
: InputSectionBase<ELFT>(File, Header, SectionKind) {}
template <class ELFT>
-EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F,
+EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F,
const Elf_Shdr *Header)
: SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
// Mark .eh_frame sections as live by default because there are
// usually no relocations that point to .eh_frames. Otherwise,
- // the garbage collector would drop all .eh_frame sections.
+ // the garbage collector would drop all .eh_frame sections.
this->Live = true;
}
template <class ELFT>
-bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
+bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
}
+// .eh_frame is a sequence of CIE or FDE records.
+// This function splits an input section into records and returns them.
+template <class ELFT>
+void EhInputSection<ELFT>::split() {
+ ArrayRef<uint8_t> Data = this->getSectionData();
+ for (size_t Off = 0, End = Data.size(); Off != End;) {
+ size_t Size = readEhRecordSize<ELFT>(Data.slice(Off));
+ this->Pieces.emplace_back(Off, Data.slice(Off, Size));
+ // The empty record is the end marker.
+ if (Size == 4)
+ break;
+ Off += Size;
+ }
+}
+
template <class ELFT>
-typename EHInputSection<ELFT>::uintX_t
-EHInputSection<ELFT>::getOffset(uintX_t Offset) {
+typename ELFT::uint EhInputSection<ELFT>::getOffset(uintX_t Offset) const {
// The file crtbeginT.o has relocations pointing to the start of an empty
// .eh_frame that is known to be the first in the link. It does that to
// identify the start of the output .eh_frame. Handle this special case.
if (this->getSectionHdr()->sh_size == 0)
return Offset;
- std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
- uintX_t Base = I->second;
- if (Base == uintX_t(-1))
+ const SectionPiece *Piece = this->getSectionPiece(Offset);
+ if (Piece->OutputOff == size_t(-1))
return -1; // Not in the output
- uintX_t Addend = Offset - I->first;
- return Base + Addend;
+ uintX_t Addend = Offset - Piece->InputOff;
+ return Piece->OutputOff + Addend;
+}
+
+static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
+ // Optimize the common case.
+ StringRef S((const char *)A.data(), A.size());
+ if (EntSize == 1)
+ return S.find(0);
+
+ for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
+ const char *B = S.begin() + I;
+ if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
+ return I;
+ }
+ return StringRef::npos;
+}
+
+// Split SHF_STRINGS section. Such section is a sequence of
+// null-terminated strings.
+static std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> Data,
+ size_t EntSize) {
+ std::vector<SectionPiece> V;
+ size_t Off = 0;
+ while (!Data.empty()) {
+ size_t End = findNull(Data, EntSize);
+ if (End == StringRef::npos)
+ fatal("string is not null terminated");
+ size_t Size = End + EntSize;
+ V.emplace_back(Off, Data.slice(0, Size));
+ Data = Data.slice(Size);
+ Off += Size;
+ }
+ return V;
+}
+
+// Split non-SHF_STRINGS section. Such section is a sequence of
+// fixed size records.
+static std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> Data,
+ size_t EntSize) {
+ std::vector<SectionPiece> V;
+ size_t Size = Data.size();
+ assert((Size % EntSize) == 0);
+ for (unsigned I = 0, N = Size; I != N; I += EntSize)
+ V.emplace_back(I, Data.slice(I, EntSize));
+ return V;
}
template <class ELFT>
-MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
+MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
const Elf_Shdr *Header)
: SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
+template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
+ ArrayRef<uint8_t> Data = this->getSectionData();
+ uintX_t EntSize = this->Header->sh_entsize;
+ if (this->Header->sh_flags & SHF_STRINGS)
+ this->Pieces = splitStrings(Data, EntSize);
+ else
+ this->Pieces = splitNonStrings(Data, EntSize);
+
+ if (Config->GcSections)
+ for (uintX_t Off : LiveOffsets)
+ this->getSectionPiece(Off)->Live = true;
+}
+
template <class ELFT>
bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
return S->SectionKind == InputSectionBase<ELFT>::Merge;
}
+// Do binary search to get a section piece at a given input offset.
template <class ELFT>
-std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
- typename ELFFile<ELFT>::uintX_t> *,
- typename ELFFile<ELFT>::uintX_t>
-SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
+SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
+ auto *This = static_cast<const SplitInputSection<ELFT> *>(this);
+ return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
+}
+
+template <class ELFT>
+const SectionPiece *
+SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) const {
ArrayRef<uint8_t> D = this->getSectionData();
StringRef Data((const char *)D.data(), D.size());
uintX_t Size = Data.size();
if (Offset >= Size)
- error("Entry is past the end of the section");
+ fatal("entry is past the end of the section");
// Find the element this offset points to.
auto I = std::upper_bound(
- Offsets.begin(), Offsets.end(), Offset,
- [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
- return A < B.first;
- });
- uintX_t End = I == Offsets.end() ? Data.size() : I->first;
+ Pieces.begin(), Pieces.end(), Offset,
+ [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
--I;
- return std::make_pair(&*I, End);
+ return &*I;
}
+// Returns the offset in an output section for a given input offset.
+// Because contents of a mergeable section is not contiguous in output,
+// it is not just an addition to a base output offset.
template <class ELFT>
-typename MergeInputSection<ELFT>::uintX_t
-MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
- std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
- this->getRangeAndSize(Offset);
- std::pair<uintX_t, uintX_t> *I = T.first;
- uintX_t End = T.second;
- uintX_t Start = I->first;
-
- // Compute the Addend and if the Base is cached, return.
- uintX_t Addend = Offset - Start;
- uintX_t &Base = I->second;
- if (Base != uintX_t(-1))
- return Base + Addend;
-
- // Map the base to the offset in the output section and cache it.
- ArrayRef<uint8_t> D = this->getSectionData();
- StringRef Data((const char *)D.data(), D.size());
- StringRef Entry = Data.substr(Start, End - Start);
- Base =
- static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
- return Base + Addend;
+typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) const {
+ auto It = OffsetMap.find(Offset);
+ if (It != OffsetMap.end())
+ return It->second;
+
+ // If Offset is not at beginning of a section piece, it is not in the map.
+ // In that case we need to search from the original section piece vector.
+ const SectionPiece &Piece = *this->getSectionPiece(Offset);
+ assert(Piece.Live);
+ uintX_t Addend = Offset - Piece.InputOff;
+ return Piece.OutputOff + Addend;
+}
+
+// Create a map from input offsets to output offsets for all section pieces.
+// It is called after finalize().
+template <class ELFT> void MergeInputSection<ELFT>::finalizePieces() {
+ OffsetMap.grow(this->Pieces.size());
+ for (SectionPiece &Piece : this->Pieces) {
+ if (!Piece.Live)
+ continue;
+ if (Piece.OutputOff == size_t(-1)) {
+ // Offsets of tail-merged strings are computed lazily.
+ auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec);
+ ArrayRef<uint8_t> D = Piece.data();
+ StringRef S((const char *)D.data(), D.size());
+ Piece.OutputOff = OutSec->getOffset(S);
+ }
+ OffsetMap[Piece.InputOff] = Piece.OutputOff;
+ }
}
template <class ELFT>
-MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
+MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
const Elf_Shdr *Hdr)
: InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
// Initialize this->Reginfo.
ArrayRef<uint8_t> D = this->getSectionData();
- if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
- error("Invalid size of .reginfo section");
+ if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
+ error("invalid size of .reginfo section");
+ return;
+ }
Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
}
@@ -366,31 +625,67 @@ bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
}
-namespace lld {
-namespace elf2 {
-template class InputSectionBase<object::ELF32LE>;
-template class InputSectionBase<object::ELF32BE>;
-template class InputSectionBase<object::ELF64LE>;
-template class InputSectionBase<object::ELF64BE>;
-
-template class InputSection<object::ELF32LE>;
-template class InputSection<object::ELF32BE>;
-template class InputSection<object::ELF64LE>;
-template class InputSection<object::ELF64BE>;
-
-template class EHInputSection<object::ELF32LE>;
-template class EHInputSection<object::ELF32BE>;
-template class EHInputSection<object::ELF64LE>;
-template class EHInputSection<object::ELF64BE>;
-
-template class MergeInputSection<object::ELF32LE>;
-template class MergeInputSection<object::ELF32BE>;
-template class MergeInputSection<object::ELF64LE>;
-template class MergeInputSection<object::ELF64BE>;
-
-template class MipsReginfoInputSection<object::ELF32LE>;
-template class MipsReginfoInputSection<object::ELF32BE>;
-template class MipsReginfoInputSection<object::ELF64LE>;
-template class MipsReginfoInputSection<object::ELF64BE>;
+template <class ELFT>
+MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F,
+ const Elf_Shdr *Hdr)
+ : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsOptions) {
+ // Find ODK_REGINFO option in the section's content.
+ ArrayRef<uint8_t> D = this->getSectionData();
+ while (!D.empty()) {
+ if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) {
+ error("invalid size of .MIPS.options section");
+ break;
+ }
+ auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data());
+ if (O->kind == ODK_REGINFO) {
+ Reginfo = &O->getRegInfo();
+ break;
+ }
+ D = D.slice(O->size);
+ }
}
+
+template <class ELFT>
+bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
+ return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
}
+
+template bool elf::isDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
+template bool elf::isDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
+template bool elf::isDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
+template bool elf::isDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
+
+template class elf::InputSectionBase<ELF32LE>;
+template class elf::InputSectionBase<ELF32BE>;
+template class elf::InputSectionBase<ELF64LE>;
+template class elf::InputSectionBase<ELF64BE>;
+
+template class elf::InputSection<ELF32LE>;
+template class elf::InputSection<ELF32BE>;
+template class elf::InputSection<ELF64LE>;
+template class elf::InputSection<ELF64BE>;
+
+template class elf::SplitInputSection<ELF32LE>;
+template class elf::SplitInputSection<ELF32BE>;
+template class elf::SplitInputSection<ELF64LE>;
+template class elf::SplitInputSection<ELF64BE>;
+
+template class elf::EhInputSection<ELF32LE>;
+template class elf::EhInputSection<ELF32BE>;
+template class elf::EhInputSection<ELF64LE>;
+template class elf::EhInputSection<ELF64BE>;
+
+template class elf::MergeInputSection<ELF32LE>;
+template class elf::MergeInputSection<ELF32BE>;
+template class elf::MergeInputSection<ELF64LE>;
+template class elf::MergeInputSection<ELF64BE>;
+
+template class elf::MipsReginfoInputSection<ELF32LE>;
+template class elf::MipsReginfoInputSection<ELF32BE>;
+template class elf::MipsReginfoInputSection<ELF64LE>;
+template class elf::MipsReginfoInputSection<ELF64BE>;
+
+template class elf::MipsOptionsInputSection<ELF32LE>;
+template class elf::MipsOptionsInputSection<ELF32BE>;
+template class elf::MipsOptionsInputSection<ELF64LE>;
+template class elf::MipsOptionsInputSection<ELF64BE>;