2015-09-22 00:01:39 +00:00
|
|
|
//===- InputSection.cpp ---------------------------------------------------===//
|
2015-07-24 21:03:07 +00:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 00:01:39 +00:00
|
|
|
#include "InputSection.h"
|
2015-09-25 19:24:57 +00:00
|
|
|
#include "Config.h"
|
2015-08-06 15:08:23 +00:00
|
|
|
#include "Error.h"
|
2015-08-27 23:15:56 +00:00
|
|
|
#include "InputFiles.h"
|
2015-09-21 22:01:00 +00:00
|
|
|
#include "OutputSections.h"
|
2015-09-22 18:19:46 +00:00
|
|
|
#include "Target.h"
|
2015-09-21 22:01:00 +00:00
|
|
|
|
2016-02-25 21:33:56 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
2015-09-21 22:01:00 +00:00
|
|
|
using namespace llvm::object;
|
2016-02-25 21:33:56 +00:00
|
|
|
using namespace llvm::support::endian;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 00:25:54 +00:00
|
|
|
using namespace lld::elf;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
template <class ELFT>
|
2016-03-11 18:46:51 +00:00
|
|
|
InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
|
2015-10-19 21:00:02 +00:00
|
|
|
const Elf_Shdr *Header,
|
|
|
|
Kind SectionKind)
|
2016-02-25 18:43:51 +00:00
|
|
|
: Header(Header), File(File), SectionKind(SectionKind), Repl(this) {
|
2016-02-24 00:23:15 +00:00
|
|
|
// The garbage collector sets sections' Live bits.
|
|
|
|
// If GC is disabled, all sections are considered live by default.
|
2016-02-24 18:33:35 +00:00
|
|
|
Live = !Config->GcSections;
|
2016-02-24 00:38:18 +00:00
|
|
|
|
|
|
|
// The ELF spec states that a value of 0 means the section has
|
|
|
|
// no alignment constraits.
|
2016-02-24 18:33:35 +00:00
|
|
|
Align = std::max<uintX_t>(Header->sh_addralign, 1);
|
2016-02-24 00:23:15 +00:00
|
|
|
}
|
2015-10-19 21:00:02 +00:00
|
|
|
|
|
|
|
template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
|
2016-03-03 22:24:39 +00:00
|
|
|
return check(File->getObj().getSectionName(this->Header));
|
2015-10-19 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
|
2016-03-03 22:24:39 +00:00
|
|
|
return check(this->File->getObj().getSectionContents(this->Header));
|
2015-10-19 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 16:50:37 +00:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t
|
|
|
|
InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
|
|
|
|
switch (SectionKind) {
|
|
|
|
case Regular:
|
|
|
|
return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
|
2015-11-11 19:54:14 +00:00
|
|
|
case EHFrame:
|
|
|
|
return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
|
2015-11-11 16:50:37 +00:00
|
|
|
case Merge:
|
|
|
|
return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
|
2015-12-20 10:57:34 +00:00
|
|
|
case MipsReginfo:
|
2016-01-06 22:01:25 +00:00
|
|
|
// MIPS .reginfo sections are consumed by the linker,
|
|
|
|
// so it should never be copied to output.
|
|
|
|
llvm_unreachable("MIPS .reginfo reached writeTo().");
|
2015-11-11 16:50:37 +00:00
|
|
|
}
|
2016-03-12 08:31:34 +00:00
|
|
|
llvm_unreachable("invalid section kind");
|
2015-11-11 16:50:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t
|
2015-10-23 19:55:11 +00:00
|
|
|
InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
|
2015-11-11 16:50:37 +00:00
|
|
|
return getOffset(Sym.st_value);
|
2015-10-19 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:51:13 +00:00
|
|
|
// Returns a section that Rel relocation is pointing to.
|
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
2016-02-24 00:23:13 +00:00
|
|
|
InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const {
|
2015-10-27 21:51:13 +00:00
|
|
|
// Global symbol
|
|
|
|
uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
|
2016-03-11 12:06:30 +00:00
|
|
|
SymbolBody &B = File->getSymbolBody(SymIndex).repl();
|
2016-03-11 14:21:37 +00:00
|
|
|
InputSectionBase<ELFT> *S = nullptr;
|
2016-03-11 12:06:30 +00:00
|
|
|
if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B))
|
2016-03-11 14:21:37 +00:00
|
|
|
S = D->Section;
|
|
|
|
if (S)
|
|
|
|
return S->Repl;
|
2015-10-27 21:51:13 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
2016-02-24 00:23:13 +00:00
|
|
|
InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const {
|
2015-10-27 21:51:13 +00:00
|
|
|
return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
template <class ELFT>
|
2016-03-11 18:46:51 +00:00
|
|
|
InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
|
|
|
|
const Elf_Shdr *Header)
|
2015-10-19 21:00:02 +00: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-24 21:03:07 +00:00
|
|
|
|
2016-02-25 08:23:37 +00:00
|
|
|
template <class ELFT>
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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>
|
2016-03-13 05:06:50 +00:00
|
|
|
template <class RelTy>
|
2016-02-25 08:23:37 +00:00
|
|
|
void InputSection<ELFT>::copyRelocations(uint8_t *Buf,
|
2016-03-13 05:06:50 +00:00
|
|
|
iterator_range<const RelTy *> Rels) {
|
2016-02-25 08:23:37 +00:00
|
|
|
InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
|
|
|
|
|
2016-03-13 05:06:50 +00:00
|
|
|
for (const RelTy &Rel : Rels) {
|
2016-02-25 08:23:37 +00:00
|
|
|
uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
|
|
|
|
uint32_t Type = Rel.getType(Config->Mips64EL);
|
2016-03-11 12:06:30 +00:00
|
|
|
SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl();
|
2016-02-25 08:23:37 +00:00
|
|
|
|
2016-03-13 05:06:50 +00:00
|
|
|
RelTy *P = reinterpret_cast<RelTy *>(Buf);
|
|
|
|
Buf += sizeof(RelTy);
|
2016-02-25 08:23:37 +00:00
|
|
|
|
|
|
|
P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
|
2016-03-11 12:06:30 +00:00
|
|
|
P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
|
2016-02-25 08:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 03:09:40 +00:00
|
|
|
template <class RelTy>
|
|
|
|
static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
|
|
|
|
switch (Rel->getType(Config->Mips64EL)) {
|
2016-02-25 21:33:56 +00:00
|
|
|
case R_MIPS_HI16:
|
|
|
|
return R_MIPS_LO16;
|
2016-03-12 11:58:15 +00:00
|
|
|
case R_MIPS_GOT16:
|
2016-03-13 03:09:40 +00:00
|
|
|
return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
|
2016-02-25 21:33:56 +00:00
|
|
|
case R_MIPS_PCHI16:
|
|
|
|
return R_MIPS_PCLO16;
|
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
return R_MICROMIPS_LO16;
|
|
|
|
default:
|
|
|
|
return R_MIPS_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 22:01:00 +00:00
|
|
|
template <class ELFT>
|
2016-03-13 05:06:50 +00:00
|
|
|
template <class RelTy>
|
|
|
|
uint8_t *InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf,
|
|
|
|
const RelTy *Rel,
|
|
|
|
const RelTy *End) {
|
2016-03-13 03:09:40 +00:00
|
|
|
uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
|
|
|
|
SymbolBody &Sym = File->getSymbolBody(SymIndex).repl();
|
|
|
|
uint32_t Type = getMipsPairType(Rel, Sym);
|
|
|
|
|
2015-12-01 21:24:45 +00:00
|
|
|
// 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
|
2016-03-13 05:06:50 +00:00
|
|
|
if (RelTy::IsRela || Type == R_MIPS_NONE)
|
2015-12-01 21:24:45 +00:00
|
|
|
return nullptr;
|
2016-03-13 03:09:40 +00:00
|
|
|
|
2016-03-13 05:06:50 +00:00
|
|
|
for (const RelTy *RI = Rel; RI != End; ++RI) {
|
2016-03-13 03:09:40 +00:00
|
|
|
if (RI->getType(Config->Mips64EL) != Type)
|
2015-12-01 21:24:45 +00:00
|
|
|
continue;
|
2016-03-13 03:09:40 +00:00
|
|
|
if (RI->getSymbol(Config->Mips64EL) != SymIndex)
|
2015-12-13 06:49:08 +00:00
|
|
|
continue;
|
2016-03-13 03:09:40 +00:00
|
|
|
uintX_t Offset = getOffset(RI->r_offset);
|
2015-12-01 21:24:45 +00:00
|
|
|
if (Offset == (uintX_t)-1)
|
|
|
|
return nullptr;
|
|
|
|
return Buf + Offset;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-13 15:37:38 +00:00
|
|
|
template <class ELFT, class uintX_t>
|
|
|
|
static uintX_t adjustMipsSymVA(uint32_t Type, const ObjectFile<ELFT> &File,
|
|
|
|
const SymbolBody &Body, uintX_t AddrLoc,
|
|
|
|
uintX_t SymVA) {
|
|
|
|
if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp)
|
|
|
|
return getMipsGpAddr<ELFT>() - AddrLoc;
|
|
|
|
if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp)
|
|
|
|
return getMipsGpAddr<ELFT>() - AddrLoc + 4;
|
|
|
|
if (&Body == Config->MipsLocalGp)
|
|
|
|
return getMipsGpAddr<ELFT>();
|
|
|
|
if (Body.isLocal() && (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32))
|
|
|
|
// 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.
|
|
|
|
return SymVA + File.getMipsGp0();
|
|
|
|
return SymVA;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT, class uintX_t>
|
|
|
|
static uintX_t getMipsGotVA(const SymbolBody &Body, uintX_t SymVA,
|
|
|
|
uint8_t *BufLoc, uint8_t *PairedLoc) {
|
|
|
|
if (Body.isLocal()) {
|
|
|
|
// 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. The addend in that case is
|
|
|
|
// calculated using addends from R_MIPS_GOT16 and paired R_MIPS_LO16
|
|
|
|
// relocations.
|
|
|
|
const endianness E = ELFT::TargetEndianness;
|
|
|
|
uint64_t AHL = read32<E>(BufLoc) << 16;
|
|
|
|
if (PairedLoc)
|
|
|
|
AHL += SignExtend64<16>(read32<E>(PairedLoc));
|
|
|
|
return Out<ELFT>::Got->getMipsLocalPageAddr(SymVA + AHL);
|
|
|
|
}
|
|
|
|
if (!canBePreempted(Body))
|
|
|
|
// For non-local symbols GOT entries should contain their full
|
|
|
|
// addresses. But if such symbol cannot be preempted, we do not
|
|
|
|
// have to put them into the "global" part of GOT and use dynamic
|
|
|
|
// linker to determine their actual addresses. That is why we
|
|
|
|
// create GOT entries for them in the "local" part of GOT.
|
|
|
|
return Out<ELFT>::Got->getMipsLocalFullAddr(Body);
|
|
|
|
return Body.getGotVA<ELFT>();
|
|
|
|
}
|
|
|
|
|
2015-12-01 21:24:45 +00:00
|
|
|
template <class ELFT>
|
2016-03-13 05:06:50 +00:00
|
|
|
template <class RelTy>
|
2015-12-01 21:24:45 +00:00
|
|
|
void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
|
2016-03-13 05:06:50 +00:00
|
|
|
iterator_range<const RelTy *> Rels) {
|
2015-11-25 21:46:05 +00:00
|
|
|
size_t Num = Rels.end() - Rels.begin();
|
|
|
|
for (size_t I = 0; I < Num; ++I) {
|
2016-03-13 05:06:50 +00:00
|
|
|
const RelTy &RI = *(Rels.begin() + I);
|
2015-11-11 19:54:14 +00:00
|
|
|
uintX_t Offset = getOffset(RI.r_offset);
|
|
|
|
if (Offset == (uintX_t)-1)
|
|
|
|
continue;
|
|
|
|
|
2016-03-11 12:57:52 +00:00
|
|
|
uintX_t A = getAddend<ELFT>(RI);
|
|
|
|
uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
|
|
|
|
uint32_t Type = RI.getType(Config->Mips64EL);
|
2015-11-11 19:54:14 +00:00
|
|
|
uint8_t *BufLoc = Buf + Offset;
|
|
|
|
uintX_t AddrLoc = OutSec->getVA() + Offset;
|
2015-11-11 01:00:24 +00:00
|
|
|
|
2016-03-04 21:37:09 +00:00
|
|
|
if (Target->pointsToLocalDynamicGotEntry(Type) &&
|
2016-01-29 00:20:12 +00:00
|
|
|
!Target->canRelaxTls(Type, nullptr)) {
|
2015-11-11 01:00:24 +00:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
|
2016-03-11 12:53:17 +00:00
|
|
|
Out<ELFT>::Got->getTlsIndexVA() + A);
|
2015-11-11 01:00:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-21 22:01:00 +00:00
|
|
|
|
2016-03-11 12:06:30 +00:00
|
|
|
SymbolBody &Body = File->getSymbolBody(SymIndex).repl();
|
2015-12-21 10:37:33 +00:00
|
|
|
|
2016-03-11 12:06:30 +00:00
|
|
|
if (Target->canRelaxTls(Type, &Body)) {
|
2015-12-21 10:37:33 +00:00
|
|
|
uintX_t SymVA;
|
2016-03-11 13:04:28 +00:00
|
|
|
if (Target->needsGot(Type, Body))
|
2016-03-11 12:06:30 +00:00
|
|
|
SymVA = Body.getGotVA<ELFT>();
|
2015-12-21 10:37:33 +00:00
|
|
|
else
|
2016-03-11 12:06:30 +00:00
|
|
|
SymVA = Body.getVA<ELFT>();
|
2015-12-21 10:37:33 +00:00
|
|
|
// 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.
|
2016-01-29 02:33:45 +00:00
|
|
|
I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
|
2015-12-21 10:37:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-11 12:19:05 +00:00
|
|
|
// PPC64 has a special relocation representing the TOC base pointer
|
|
|
|
// that does not have a corresponding symbol.
|
|
|
|
if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) {
|
|
|
|
uintX_t SymVA = getPPC64TocBase() + A;
|
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-11 13:04:28 +00:00
|
|
|
if (Target->isTlsGlobalDynamicRel(Type) &&
|
|
|
|
!Target->canRelaxTls(Type, &Body)) {
|
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
|
|
|
|
Out<ELFT>::Got->getGlobalDynAddr(Body) + A);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintX_t SymVA = Body.getVA<ELFT>(A);
|
|
|
|
bool CBP = canBePreempted(Body);
|
|
|
|
uint8_t *PairedLoc = nullptr;
|
2016-03-12 11:58:15 +00:00
|
|
|
if (Config->EMachine == EM_MIPS)
|
2016-03-13 03:09:40 +00:00
|
|
|
PairedLoc = findMipsPairedReloc(Buf, &RI, Rels.end());
|
2016-03-11 13:04:28 +00:00
|
|
|
|
|
|
|
if (Target->needsPlt<ELFT>(Type, Body)) {
|
|
|
|
SymVA = Body.getPltVA<ELFT>() + A;
|
|
|
|
} else if (Target->needsGot(Type, Body)) {
|
2016-03-13 15:37:38 +00:00
|
|
|
if (Config->EMachine == EM_MIPS)
|
|
|
|
SymVA = getMipsGotVA<ELFT>(Body, SymVA, BufLoc, PairedLoc) + A;
|
|
|
|
else
|
2016-03-11 13:04:28 +00:00
|
|
|
SymVA = Body.getGotVA<ELFT>() + A;
|
2016-03-11 12:06:30 +00:00
|
|
|
if (Body.IsTls)
|
2016-01-29 01:49:32 +00:00
|
|
|
Type = Target->getTlsGotRel(Type);
|
2016-02-26 14:33:23 +00:00
|
|
|
} else if (Target->isSizeRel(Type) && CBP) {
|
2016-01-08 00:13:23 +00:00
|
|
|
// 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.
|
2015-11-25 20:41:53 +00:00
|
|
|
continue;
|
2015-12-16 14:45:09 +00:00
|
|
|
} else if (Config->EMachine == EM_MIPS) {
|
2016-03-13 15:37:38 +00:00
|
|
|
SymVA = adjustMipsSymVA<ELFT>(Type, *File, Body, AddrLoc, SymVA) + A;
|
2016-03-11 12:06:30 +00:00
|
|
|
} else if (!Target->needsCopyRel<ELFT>(Type, Body) && CBP) {
|
2016-03-08 20:24:36 +00:00
|
|
|
continue;
|
2015-10-12 20:28:23 +00:00
|
|
|
}
|
2016-03-11 12:06:30 +00:00
|
|
|
uintX_t Size = Body.getSize<ELFT>();
|
2016-03-11 13:04:28 +00:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, Size + A,
|
|
|
|
PairedLoc);
|
2015-09-21 22:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:18:16 +00:00
|
|
|
template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
|
2015-10-19 21:00:02 +00:00
|
|
|
if (this->Header->sh_type == SHT_NOBITS)
|
2015-07-24 21:03:07 +00:00
|
|
|
return;
|
|
|
|
// Copy section contents from source object file to output file.
|
2015-10-19 21:00:02 +00:00
|
|
|
ArrayRef<uint8_t> Data = this->getSectionData();
|
2016-02-25 08:23:37 +00:00
|
|
|
ELFFile<ELFT> &EObj = this->File->getObj();
|
|
|
|
|
|
|
|
// That happens with -r. In that case we need fix the relocation position and
|
|
|
|
// target. No relocations are applied.
|
|
|
|
if (this->Header->sh_type == SHT_RELA) {
|
|
|
|
this->copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this->Header->sh_type == SHT_REL) {
|
|
|
|
this->copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 01:58:40 +00:00
|
|
|
memcpy(Buf + OutSecOff, Data.data(), Data.size());
|
2015-09-21 22:01:00 +00:00
|
|
|
|
2015-11-11 19:54:14 +00:00
|
|
|
uint8_t *BufEnd = Buf + OutSecOff + Data.size();
|
2015-09-21 22:01:00 +00:00
|
|
|
// Iterate over all relocation sections that apply to this section.
|
2015-11-11 19:54:14 +00:00
|
|
|
for (const Elf_Shdr *RelSec : this->RelocSections) {
|
2015-09-21 22:01:00 +00:00
|
|
|
if (RelSec->sh_type == SHT_RELA)
|
2015-11-11 19:54:14 +00:00
|
|
|
this->relocate(Buf, BufEnd, EObj.relas(RelSec));
|
2015-09-21 22:01:00 +00:00
|
|
|
else
|
2015-11-11 19:54:14 +00:00
|
|
|
this->relocate(Buf, BufEnd, EObj.rels(RelSec));
|
2015-09-21 22:01:00 +00:00
|
|
|
}
|
2015-07-24 21:03:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:43:51 +00:00
|
|
|
template <class ELFT>
|
|
|
|
void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
|
|
|
|
this->Align = std::max(this->Align, Other->Align);
|
|
|
|
Other->Repl = this->Repl;
|
|
|
|
Other->Live = false;
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:54:14 +00:00
|
|
|
template <class ELFT>
|
|
|
|
SplitInputSection<ELFT>::SplitInputSection(
|
2016-03-11 18:46:51 +00:00
|
|
|
elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
|
2015-11-11 19:54:14 +00:00
|
|
|
typename InputSectionBase<ELFT>::Kind SectionKind)
|
|
|
|
: InputSectionBase<ELFT>(File, Header, SectionKind) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-03-11 18:46:51 +00:00
|
|
|
EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
|
2015-11-11 19:54:14 +00:00
|
|
|
const Elf_Shdr *Header)
|
2015-12-24 10:08:54 +00:00
|
|
|
: 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,
|
2016-02-18 15:17:01 +00:00
|
|
|
// the garbage collector would drop all .eh_frame sections.
|
2015-12-24 10:08:54 +00:00
|
|
|
this->Live = true;
|
|
|
|
}
|
2015-11-11 19:54:14 +00:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename EHInputSection<ELFT>::uintX_t
|
|
|
|
EHInputSection<ELFT>::getOffset(uintX_t Offset) {
|
2015-12-25 09:51:42 +00:00
|
|
|
// 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;
|
2015-11-11 19:54:14 +00:00
|
|
|
std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
|
|
|
|
uintX_t Base = I->second;
|
2015-11-13 13:44:59 +00:00
|
|
|
if (Base == uintX_t(-1))
|
2015-11-11 19:54:14 +00:00
|
|
|
return -1; // Not in the output
|
|
|
|
|
|
|
|
uintX_t Addend = Offset - I->first;
|
|
|
|
return Base + Addend;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
template <class ELFT>
|
2016-03-11 16:32:46 +00:00
|
|
|
MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
|
2015-10-19 21:00:02 +00:00
|
|
|
const Elf_Shdr *Header)
|
2015-11-11 19:54:14 +00:00
|
|
|
: SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
|
2015-10-19 21:00:02 +00:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
2015-11-11 19:54:14 +00:00
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::Merge;
|
2015-10-19 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-11-11 19:54:14 +00:00
|
|
|
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) {
|
2015-10-24 22:51:01 +00:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
2015-10-25 16:25:04 +00:00
|
|
|
StringRef Data((const char *)D.data(), D.size());
|
2015-10-24 22:51:01 +00:00
|
|
|
uintX_t Size = Data.size();
|
|
|
|
if (Offset >= Size)
|
2016-03-12 08:31:34 +00:00
|
|
|
fatal("entry is past the end of the section");
|
2015-10-24 22:51:01 +00:00
|
|
|
|
|
|
|
// Find the element this offset points to.
|
|
|
|
auto I = std::upper_bound(
|
2015-11-11 15:20:45 +00:00
|
|
|
Offsets.begin(), Offsets.end(), Offset,
|
2015-11-11 15:55:00 +00:00
|
|
|
[](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
|
2015-10-24 22:51:01 +00:00
|
|
|
return A < B.first;
|
|
|
|
});
|
2015-11-11 15:40:37 +00:00
|
|
|
uintX_t End = I == Offsets.end() ? Data.size() : I->first;
|
2015-10-24 22:51:01 +00:00
|
|
|
--I;
|
2015-11-11 19:54:14 +00:00
|
|
|
return std::make_pair(&*I, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename MergeInputSection<ELFT>::uintX_t
|
|
|
|
MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
|
2015-11-13 13:44:59 +00:00
|
|
|
std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
|
2015-11-11 19:54:14 +00:00
|
|
|
this->getRangeAndSize(Offset);
|
|
|
|
std::pair<uintX_t, uintX_t> *I = T.first;
|
|
|
|
uintX_t End = T.second;
|
2015-10-24 22:51:01 +00:00
|
|
|
uintX_t Start = I->first;
|
|
|
|
|
|
|
|
// Compute the Addend and if the Base is cached, return.
|
|
|
|
uintX_t Addend = Offset - Start;
|
2015-11-11 15:40:37 +00:00
|
|
|
uintX_t &Base = I->second;
|
2015-11-11 15:55:00 +00:00
|
|
|
if (Base != uintX_t(-1))
|
2015-10-24 22:51:01 +00:00
|
|
|
return Base + Addend;
|
|
|
|
|
2015-11-25 23:54:53 +00:00
|
|
|
// Map the base to the offset in the output section and cache it.
|
2015-11-11 19:54:14 +00:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
|
|
|
StringRef Data((const char *)D.data(), D.size());
|
2015-10-24 22:51:01 +00:00
|
|
|
StringRef Entry = Data.substr(Start, End - Start);
|
|
|
|
Base =
|
|
|
|
static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
|
|
|
|
return Base + Addend;
|
2015-08-13 19:18:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 10:57:34 +00:00
|
|
|
template <class ELFT>
|
2016-03-11 18:46:51 +00:00
|
|
|
MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
|
2016-01-06 22:42:43 +00:00
|
|
|
const Elf_Shdr *Hdr)
|
|
|
|
: InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
|
|
|
|
// Initialize this->Reginfo.
|
2015-12-25 13:02:13 +00:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
2016-01-06 22:42:43 +00:00
|
|
|
if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
|
2016-03-12 08:31:34 +00:00
|
|
|
fatal("invalid size of .reginfo section");
|
2016-01-06 22:42:43 +00:00
|
|
|
Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
|
2015-12-25 13:02:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 10:57:34 +00:00
|
|
|
template <class ELFT>
|
|
|
|
bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
|
|
|
|
}
|
|
|
|
|
2016-02-28 00:25:54 +00:00
|
|
|
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::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>;
|