//===- InputSection.cpp ---------------------------------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "InputSection.h" #include "Config.h" #include "Error.h" #include "InputFiles.h" #include "OutputSections.h" #include "Target.h" using namespace llvm; using namespace llvm::ELF; using namespace llvm::object; using namespace lld; using namespace lld::elf2; template InputSectionBase::InputSectionBase(ObjectFile *File, const Elf_Shdr *Header, Kind SectionKind) : Header(Header), File(File), SectionKind(SectionKind) {} template StringRef InputSectionBase::getSectionName() const { ErrorOr Name = File->getObj().getSectionName(this->Header); error(Name); return *Name; } template ArrayRef InputSectionBase::getSectionData() const { ErrorOr> Ret = this->File->getObj().getSectionContents(this->Header); error(Ret); return *Ret; } template typename ELFFile::uintX_t InputSectionBase::getOffset(uintX_t Offset) { switch (SectionKind) { case Regular: return cast>(this)->OutSecOff + Offset; case EHFrame: return cast>(this)->getOffset(Offset); case Merge: return cast>(this)->getOffset(Offset); } llvm_unreachable("Invalid section kind"); } template typename ELFFile::uintX_t InputSectionBase::getOffset(const Elf_Sym &Sym) { return getOffset(Sym.st_value); } // Returns a section that Rel relocation is pointing to. template InputSectionBase * InputSectionBase::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>(B->repl())) return &D->Section; // Local symbol if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex)) if (InputSectionBase *Sec = File->getSection(*Sym)) return Sec; return nullptr; } template InputSectionBase * InputSectionBase::getRelocTarget(const Elf_Rela &Rel) { return getRelocTarget(reinterpret_cast(Rel)); } template InputSection::InputSection(ObjectFile *F, const Elf_Shdr *Header) : InputSectionBase(F, Header, Base::Regular) {} template bool InputSection::classof(const InputSectionBase *S) { return S->SectionKind == Base::Regular; } template template uint8_t * InputSectionBase::findMipsPairedReloc(uint8_t *Buf, uint32_t Type, RelIteratorRange 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; uintX_t Offset = getOffset(RI.r_offset); if (Offset == (uintX_t)-1) return nullptr; return Buf + Offset; } return nullptr; } template template void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd, RelIteratorRange Rels) { typedef Elf_Rel_Impl 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; uint8_t *BufLoc = Buf + Offset; uintX_t AddrLoc = OutSec->getVA() + Offset; auto NextRelocs = llvm::make_range(&RI, Rels.end()); if (Target->isTlsLocalDynamicReloc(Type) && !Target->isTlsOptimized(Type, nullptr)) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, Out::Got->getLocalTlsIndexVA() + getAddend(RI)); continue; } // Handle relocations for local symbols -- they never get // resolved so we don't allocate a SymbolBody. const Elf_Shdr *SymTab = File->getSymbolTable(); if (SymIndex < SymTab->sh_info) { uintX_t SymVA = getLocalRelTarget(*File, RI); Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, findMipsPairedReloc(Buf, Type, NextRelocs)); continue; } SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); if (Target->isTlsGlobalDynamicReloc(Type) && !Target->isTlsOptimized(Type, &Body)) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, Out::Got->getGlobalDynAddr(Body) + getAddend(RI)); continue; } if (Target->isTlsOptimized(Type, &Body)) { uintX_t SymVA = Target->relocNeedsGot(Type, Body) ? Out::Got->getEntryAddr(Body) : getSymVA(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; } uintX_t SymVA = getSymVA(Body); if (Target->relocNeedsPlt(Type, Body)) { SymVA = Out::Plt->getEntryAddr(Body); Type = Target->getPltRefReloc(Type); } else if (Target->relocNeedsGot(Type, Body)) { SymVA = Out::Got->getEntryAddr(Body); if (Body.isTLS()) Type = Target->getTlsGotReloc(); } else if (!Target->relocNeedsCopy(Type, Body) && isa>(Body)) { continue; } else if (Target->isTlsDynReloc(Type)) { continue; } Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + getAddend(RI), findMipsPairedReloc(Buf, Type, NextRelocs)); } } template void InputSection::writeTo(uint8_t *Buf) { if (this->Header->sh_type == SHT_NOBITS) return; // Copy section contents from source object file to output file. ArrayRef Data = this->getSectionData(); memcpy(Buf + OutSecOff, Data.data(), Data.size()); ELFFile &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)); } } template SplitInputSection::SplitInputSection( ObjectFile *File, const Elf_Shdr *Header, typename InputSectionBase::Kind SectionKind) : InputSectionBase(File, Header, SectionKind) {} template EHInputSection::EHInputSection(ObjectFile *F, const Elf_Shdr *Header) : SplitInputSection(F, Header, InputSectionBase::EHFrame) {} template bool EHInputSection::classof(const InputSectionBase *S) { return S->SectionKind == InputSectionBase::EHFrame; } template typename EHInputSection::uintX_t EHInputSection::getOffset(uintX_t Offset) { std::pair *I = this->getRangeAndSize(Offset).first; uintX_t Base = I->second; if (Base == uintX_t(-1)) return -1; // Not in the output uintX_t Addend = Offset - I->first; return Base + Addend; } template MergeInputSection::MergeInputSection(ObjectFile *F, const Elf_Shdr *Header) : SplitInputSection(F, Header, InputSectionBase::Merge) {} template bool MergeInputSection::classof(const InputSectionBase *S) { return S->SectionKind == InputSectionBase::Merge; } template std::pair::uintX_t, typename ELFFile::uintX_t> *, typename ELFFile::uintX_t> SplitInputSection::getRangeAndSize(uintX_t Offset) { ArrayRef 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"); // Find the element this offset points to. auto I = std::upper_bound( Offsets.begin(), Offsets.end(), Offset, [](const uintX_t &A, const std::pair &B) { return A < B.first; }); uintX_t End = I == Offsets.end() ? Data.size() : I->first; --I; return std::make_pair(&*I, End); } template typename MergeInputSection::uintX_t MergeInputSection::getOffset(uintX_t Offset) { std::pair *, uintX_t> T = this->getRangeAndSize(Offset); std::pair *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 D = this->getSectionData(); StringRef Data((const char *)D.data(), D.size()); StringRef Entry = Data.substr(Start, End - Start); Base = static_cast *>(this->OutSec)->getOffset(Entry); return Base + Addend; } namespace lld { namespace elf2 { template class InputSectionBase; template class InputSectionBase; template class InputSectionBase; template class InputSectionBase; template class InputSection; template class InputSection; template class InputSection; template class InputSection; template class EHInputSection; template class EHInputSection; template class EHInputSection; template class EHInputSection; template class MergeInputSection; template class MergeInputSection; template class MergeInputSection; template class MergeInputSection; } }