2015-09-22 08:01:39 +08:00
|
|
|
//===- InputSection.cpp ---------------------------------------------------===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-09-26 03:24:57 +08:00
|
|
|
#include "Config.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2015-08-28 07:15:56 +08:00
|
|
|
#include "InputFiles.h"
|
2015-09-22 06:01:00 +08:00
|
|
|
#include "OutputSections.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "Target.h"
|
2015-09-22 06:01:00 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
2015-09-22 06:01:00 +08:00
|
|
|
using namespace llvm::object;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
|
|
|
|
const Elf_Shdr *Header,
|
|
|
|
Kind SectionKind)
|
|
|
|
: Header(Header), File(File), SectionKind(SectionKind) {}
|
|
|
|
|
|
|
|
template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
|
|
|
|
ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
|
|
|
|
error(Name);
|
|
|
|
return *Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t
|
2015-10-24 03:55:11 +08:00
|
|
|
InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
|
2015-10-20 05:00:02 +08:00
|
|
|
if (auto *S = dyn_cast<InputSection<ELFT>>(this))
|
|
|
|
return S->OutSecOff + Sym.st_value;
|
|
|
|
return cast<MergeInputSection<ELFT>>(this)->getOffset(Sym.st_value);
|
|
|
|
}
|
|
|
|
|
2015-10-28 05:51:13 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
|
|
|
InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) {
|
|
|
|
return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT>
|
2015-09-22 01:47:00 +08:00
|
|
|
InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
|
2015-10-20 05:00:02 +08:00
|
|
|
: InputSectionBase<ELFT>(F, Header, Base::Regular) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == Base::Regular;
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 06:01:00 +08:00
|
|
|
template <class ELFT>
|
|
|
|
template <bool isRela>
|
|
|
|
void InputSection<ELFT>::relocate(
|
2015-10-13 05:19:18 +08:00
|
|
|
uint8_t *Buf, uint8_t *BufEnd,
|
|
|
|
iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels,
|
2015-10-08 03:18:16 +08:00
|
|
|
const ObjectFile<ELFT> &File, uintX_t BaseAddr) {
|
2015-09-22 06:01:00 +08:00
|
|
|
typedef Elf_Rel_Impl<ELFT, isRela> RelType;
|
|
|
|
for (const RelType &RI : Rels) {
|
2015-10-17 06:51:43 +08:00
|
|
|
uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
|
|
|
|
uint32_t Type = RI.getType(Config->Mips64EL);
|
2015-09-22 06:01:00 +08:00
|
|
|
|
|
|
|
// 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) {
|
2015-10-17 06:37:32 +08:00
|
|
|
uintX_t SymVA = getLocalRelTarget(File, RI);
|
2015-10-23 10:40:46 +08:00
|
|
|
Target->relocateOne(Buf + RI.r_offset, BufEnd, Type,
|
|
|
|
BaseAddr + RI.r_offset, SymVA);
|
2015-10-13 04:28:23 +08:00
|
|
|
continue;
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 04:28:23 +08:00
|
|
|
SymbolBody &Body = *File.getSymbolBody(SymIndex)->repl();
|
|
|
|
uintX_t SymVA = getSymVA<ELFT>(Body);
|
|
|
|
if (Target->relocNeedsPlt(Type, Body)) {
|
|
|
|
SymVA = Out<ELFT>::Plt->getEntryAddr(Body);
|
2015-10-15 00:15:46 +08:00
|
|
|
Type = Target->getPLTRefReloc(Type);
|
2015-10-13 04:28:23 +08:00
|
|
|
} else if (Target->relocNeedsGot(Type, Body)) {
|
|
|
|
SymVA = Out<ELFT>::Got->getEntryAddr(Body);
|
|
|
|
Type = Target->getGotRefReloc();
|
|
|
|
} else if (Target->relocPointsToGot(Type)) {
|
|
|
|
SymVA = Out<ELFT>::Got->getVA();
|
|
|
|
Type = Target->getPCRelReloc();
|
|
|
|
} else if (isa<SharedSymbol<ELFT>>(Body)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-23 10:40:46 +08:00
|
|
|
Target->relocateOne(Buf + RI.r_offset, BufEnd, Type, BaseAddr + RI.r_offset,
|
2015-10-20 04:24:44 +08:00
|
|
|
SymVA + getAddend<ELFT>(RI));
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 03:18:16 +08:00
|
|
|
template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
|
2015-10-20 05:00:02 +08:00
|
|
|
if (this->Header->sh_type == SHT_NOBITS)
|
2015-07-25 05:03:07 +08:00
|
|
|
return;
|
|
|
|
// Copy section contents from source object file to output file.
|
2015-10-20 05:00:02 +08:00
|
|
|
ArrayRef<uint8_t> Data = this->getSectionData();
|
2015-10-15 09:58:40 +08:00
|
|
|
memcpy(Buf + OutSecOff, Data.data(), Data.size());
|
2015-09-22 06:01:00 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
ELFFile<ELFT> &EObj = this->File->getObj();
|
2015-10-15 09:58:40 +08:00
|
|
|
uint8_t *Base = Buf + OutSecOff;
|
2015-10-20 05:00:02 +08:00
|
|
|
uintX_t BaseAddr = this->OutSec->getVA() + OutSecOff;
|
2015-09-22 06:01:00 +08:00
|
|
|
// Iterate over all relocation sections that apply to this section.
|
|
|
|
for (const Elf_Shdr *RelSec : RelocSections) {
|
|
|
|
if (RelSec->sh_type == SHT_RELA)
|
2015-10-20 05:00:02 +08:00
|
|
|
relocate(Base, Base + Data.size(), EObj.relas(RelSec), *this->File,
|
|
|
|
BaseAddr);
|
2015-09-22 06:01:00 +08:00
|
|
|
else
|
2015-10-20 05:00:02 +08:00
|
|
|
relocate(Base, Base + Data.size(), EObj.rels(RelSec), *this->File,
|
|
|
|
BaseAddr);
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT>
|
|
|
|
MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
|
|
|
|
const Elf_Shdr *Header)
|
|
|
|
: InputSectionBase<ELFT>(F, Header, Base::Merge) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == Base::Merge;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename MergeInputSection<ELFT>::uintX_t
|
2015-10-24 03:55:11 +08:00
|
|
|
MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
|
2015-10-25 06:51:01 +08:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
2015-10-26 00:25:04 +08:00
|
|
|
StringRef Data((const char *)D.data(), D.size());
|
2015-10-25 06:51:01 +08:00
|
|
|
uintX_t Size = Data.size();
|
|
|
|
if (Offset >= Size)
|
2015-10-20 05:00:02 +08:00
|
|
|
error("Entry is past the end of the section");
|
2015-10-25 06:51:01 +08:00
|
|
|
|
|
|
|
// Find the element this offset points to.
|
|
|
|
auto I = std::upper_bound(
|
|
|
|
this->Offsets.begin(), this->Offsets.end(), Offset,
|
|
|
|
[](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
|
|
|
|
return A < B.first;
|
|
|
|
});
|
|
|
|
size_t End = I == this->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 != uintX_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<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
|
|
|
|
return Base + Addend;
|
2015-08-14 03:18:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
2015-10-20 05:00:02 +08:00
|
|
|
template class InputSectionBase<object::ELF32LE>;
|
|
|
|
template class InputSectionBase<object::ELF32BE>;
|
|
|
|
template class InputSectionBase<object::ELF64LE>;
|
|
|
|
template class InputSectionBase<object::ELF64BE>;
|
|
|
|
|
2015-09-22 01:47:00 +08:00
|
|
|
template class InputSection<object::ELF32LE>;
|
|
|
|
template class InputSection<object::ELF32BE>;
|
|
|
|
template class InputSection<object::ELF64LE>;
|
|
|
|
template class InputSection<object::ELF64BE>;
|
2015-10-20 05:00:02 +08:00
|
|
|
|
|
|
|
template class MergeInputSection<object::ELF32LE>;
|
|
|
|
template class MergeInputSection<object::ELF32BE>;
|
|
|
|
template class MergeInputSection<object::ELF64LE>;
|
|
|
|
template class MergeInputSection<object::ELF64BE>;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|