//===- 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(const Elf_Sym &Sym) { if (auto *S = dyn_cast>(this)) return S->OutSecOff + Sym.st_value; return cast>(this)->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 void InputSectionBase::relocate( uint8_t *Buf, uint8_t *BufEnd, iterator_range *> Rels, uintX_t BaseAddr) { typedef Elf_Rel_Impl RelType; for (const RelType &RI : Rels) { uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); uint32_t Type = RI.getType(Config->Mips64EL); uint8_t *BufLoc = Buf + RI.r_offset; uintX_t AddrLoc = BaseAddr + RI.r_offset; if (Type == Target->getTlsLocalDynamicReloc()) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, Out::Got->getVA() + Out::LocalModuleTlsIndexOffset + 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); continue; } SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); 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); Type = Target->getGotRefReloc(); } else if (Target->relocPointsToGot(Type)) { SymVA = Out::Got->getVA(); Type = Target->getPCRelReloc(); } else if (!Target->relocNeedsCopy(Type, Body) && isa>(Body)) { continue; } Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + getAddend(RI)); } } 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 *Base = Buf + OutSecOff; uintX_t BaseAddr = this->OutSec->getVA() + OutSecOff; // Iterate over all relocation sections that apply to this section. for (const Elf_Shdr *RelSec : RelocSections) { if (RelSec->sh_type == SHT_RELA) this->relocate(Base, Base + Data.size(), EObj.relas(RelSec), BaseAddr); else this->relocate(Base, Base + Data.size(), EObj.rels(RelSec), BaseAddr); } } template MergeInputSection::MergeInputSection(ObjectFile *F, const Elf_Shdr *Header) : InputSectionBase(F, Header, Base::Merge) {} template bool MergeInputSection::classof(const InputSectionBase *S) { return S->SectionKind == Base::Merge; } template typename MergeInputSection::uintX_t MergeInputSection::getOffset(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; 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 != size_t(-1)) return Base + Addend; // Map the base to the offset in the output section and cashe it. 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 MergeInputSection; template class MergeInputSection; template class MergeInputSection; template class MergeInputSection; } }