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);
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal(Name);
|
2015-10-20 05:00:02 +08:00
|
|
|
return *Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
|
|
|
|
ErrorOr<ArrayRef<uint8_t>> Ret =
|
|
|
|
this->File->getObj().getSectionContents(this->Header);
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal(Ret);
|
2015-10-20 05:00:02 +08:00
|
|
|
return *Ret;
|
|
|
|
}
|
|
|
|
|
2015-11-12 00:50:37 +08: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-12 03:54:14 +08:00
|
|
|
case EHFrame:
|
|
|
|
return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
|
2015-11-12 00:50:37 +08:00
|
|
|
case Merge:
|
|
|
|
return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
|
2015-12-20 18:57:34 +08:00
|
|
|
case MipsReginfo:
|
2016-01-07 06:01:25 +08: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-12 00:50:37 +08:00
|
|
|
}
|
2015-11-12 17:11:20 +08:00
|
|
|
llvm_unreachable("Invalid section kind");
|
2015-11-12 00:50:37 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t
|
2015-10-24 03:55:11 +08:00
|
|
|
InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
|
2015-11-12 00:50:37 +08:00
|
|
|
return getOffset(Sym.st_value);
|
2015-10-20 05:00:02 +08:00
|
|
|
}
|
|
|
|
|
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()))
|
2015-12-24 22:22:24 +08:00
|
|
|
return D->Section;
|
2015-10-28 05:51:13 +08:00
|
|
|
// 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>
|
2015-12-02 05:24:45 +08:00
|
|
|
uint8_t *
|
2015-12-13 14:49:08 +08:00
|
|
|
InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex,
|
|
|
|
uint32_t Type,
|
2015-12-02 05:24:45 +08:00
|
|
|
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;
|
2015-12-13 14:49:08 +08:00
|
|
|
if (RI.getSymbol(Config->Mips64EL) != SymIndex)
|
|
|
|
continue;
|
2015-12-02 05:24:45 +08:00
|
|
|
uintX_t Offset = getOffset(RI.r_offset);
|
|
|
|
if (Offset == (uintX_t)-1)
|
|
|
|
return nullptr;
|
|
|
|
return Buf + Offset;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
template <bool isRela>
|
|
|
|
void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
|
|
|
|
RelIteratorRange<isRela> Rels) {
|
2015-09-22 06:01:00 +08:00
|
|
|
typedef Elf_Rel_Impl<ELFT, isRela> RelType;
|
2015-11-26 05:46:05 +08:00
|
|
|
size_t Num = Rels.end() - Rels.begin();
|
|
|
|
for (size_t I = 0; I < Num; ++I) {
|
|
|
|
const RelType &RI = *(Rels.begin() + I);
|
2015-10-17 06:51:43 +08:00
|
|
|
uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
|
|
|
|
uint32_t Type = RI.getType(Config->Mips64EL);
|
2015-11-12 03:54:14 +08:00
|
|
|
uintX_t Offset = getOffset(RI.r_offset);
|
|
|
|
if (Offset == (uintX_t)-1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint8_t *BufLoc = Buf + Offset;
|
|
|
|
uintX_t AddrLoc = OutSec->getVA() + Offset;
|
2015-12-02 05:24:45 +08:00
|
|
|
auto NextRelocs = llvm::make_range(&RI, Rels.end());
|
2015-11-11 09:00:24 +08:00
|
|
|
|
2016-01-29 10:33:45 +08:00
|
|
|
if (Target->isTlsLocalDynamicRel(Type) &&
|
2016-01-29 08:20:12 +08:00
|
|
|
!Target->canRelaxTls(Type, nullptr)) {
|
2015-11-11 09:00:24 +08:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
|
2016-02-05 08:10:02 +08:00
|
|
|
Out<ELFT>::Got->getTlsIndexVA() +
|
2015-11-11 09:00:24 +08:00
|
|
|
getAddend<ELFT>(RI));
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-22 06:01:00 +08:00
|
|
|
|
2015-12-21 18:37:33 +08:00
|
|
|
const Elf_Shdr *SymTab = File->getSymbolTable();
|
|
|
|
SymbolBody *Body = nullptr;
|
|
|
|
if (SymIndex >= SymTab->sh_info)
|
|
|
|
Body = File->getSymbolBody(SymIndex)->repl();
|
|
|
|
|
2016-01-29 08:20:12 +08:00
|
|
|
if (Target->canRelaxTls(Type, Body)) {
|
2015-12-21 18:37:33 +08:00
|
|
|
uintX_t SymVA;
|
|
|
|
if (!Body)
|
|
|
|
SymVA = getLocalRelTarget(*File, RI, 0);
|
2016-01-29 10:33:45 +08:00
|
|
|
else if (Target->needsGot(Type, *Body))
|
2016-02-02 05:00:35 +08:00
|
|
|
SymVA = Body->getGotVA<ELFT>();
|
2015-12-21 18:37:33 +08:00
|
|
|
else
|
2016-02-02 05:00:35 +08:00
|
|
|
SymVA = Body->getVA<ELFT>();
|
2015-12-21 18:37:33 +08: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 10:33:45 +08:00
|
|
|
I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
|
2015-12-21 18:37:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-22 06:01:00 +08:00
|
|
|
// Handle relocations for local symbols -- they never get
|
|
|
|
// resolved so we don't allocate a SymbolBody.
|
2015-12-21 18:37:33 +08:00
|
|
|
uintX_t A = getAddend<ELFT>(RI);
|
|
|
|
if (!Body) {
|
|
|
|
uintX_t SymVA = getLocalRelTarget(*File, RI, A);
|
2016-01-21 13:33:23 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
|
|
|
if (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.
|
|
|
|
SymVA += File->getMipsGp0();
|
|
|
|
else if (Type == R_MIPS_GOT16)
|
|
|
|
// R_MIPS_GOT16 relocation against local symbol requires index of
|
|
|
|
// a local GOT entry which contains page address corresponds
|
|
|
|
// to the symbol address.
|
|
|
|
SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA);
|
|
|
|
}
|
2015-12-11 16:59:37 +08:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0,
|
2015-12-13 14:49:08 +08:00
|
|
|
findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
|
2015-10-13 04:28:23 +08:00
|
|
|
continue;
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 10:33:45 +08:00
|
|
|
if (Target->isTlsGlobalDynamicRel(Type) &&
|
2016-01-29 08:20:12 +08:00
|
|
|
!Target->canRelaxTls(Type, Body)) {
|
2015-11-13 08:28:34 +08:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
|
2015-12-21 18:37:33 +08:00
|
|
|
Out<ELFT>::Got->getGlobalDynAddr(*Body) +
|
2015-11-13 08:28:34 +08:00
|
|
|
getAddend<ELFT>(RI));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
uintX_t SymVA = Body->getVA<ELFT>();
|
2016-01-29 10:33:45 +08:00
|
|
|
if (Target->needsPlt(Type, *Body)) {
|
2016-02-02 05:00:35 +08:00
|
|
|
SymVA = Body->getPltVA<ELFT>();
|
2016-01-29 10:33:45 +08:00
|
|
|
} else if (Target->needsGot(Type, *Body)) {
|
2016-02-04 19:51:45 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && !canBePreempted(Body, true))
|
2016-01-21 13:33:23 +08:00
|
|
|
// Under some conditions relocations against non-local symbols require
|
|
|
|
// entries in the local part of MIPS GOT. In that case we need an entry
|
|
|
|
// initialized by full address of the symbol.
|
|
|
|
SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(*Body);
|
|
|
|
else
|
2016-02-02 05:00:35 +08:00
|
|
|
SymVA = Body->getGotVA<ELFT>();
|
2016-02-23 07:16:05 +08:00
|
|
|
if (Body->IsTls)
|
2016-01-29 09:49:32 +08:00
|
|
|
Type = Target->getTlsGotRel(Type);
|
2015-12-21 18:37:33 +08:00
|
|
|
} else if (!Target->needsCopyRel(Type, *Body) &&
|
|
|
|
isa<SharedSymbol<ELFT>>(*Body)) {
|
2015-10-13 04:28:23 +08:00
|
|
|
continue;
|
2016-01-29 10:33:45 +08:00
|
|
|
} else if (Target->isTlsDynRel(Type, *Body)) {
|
2016-01-08 08:13:23 +08:00
|
|
|
continue;
|
2016-01-29 10:33:45 +08:00
|
|
|
} else if (Target->isSizeRel(Type) && canBePreempted(Body, false)) {
|
2016-01-08 08:13:23 +08: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-26 04:41:53 +08:00
|
|
|
continue;
|
2015-12-16 22:45:09 +08:00
|
|
|
} else if (Config->EMachine == EM_MIPS) {
|
2015-12-21 18:37:33 +08:00
|
|
|
if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp)
|
2015-12-16 22:45:09 +08:00
|
|
|
SymVA = getMipsGpAddr<ELFT>() - AddrLoc;
|
2015-12-21 18:37:33 +08:00
|
|
|
else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp)
|
2015-12-16 22:45:09 +08:00
|
|
|
SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4;
|
2016-02-04 20:09:49 +08:00
|
|
|
else if (Body == Config->MipsLocalGp)
|
|
|
|
SymVA = getMipsGpAddr<ELFT>();
|
2015-10-13 04:28:23 +08:00
|
|
|
}
|
2016-02-03 08:12:24 +08:00
|
|
|
uintX_t Size = Body->getSize<ELFT>();
|
2015-12-11 16:59:37 +08:00
|
|
|
Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A,
|
2015-12-13 14:49:08 +08:00
|
|
|
findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
|
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-11-12 03:54:14 +08:00
|
|
|
uint8_t *BufEnd = Buf + OutSecOff + Data.size();
|
2015-09-22 06:01:00 +08:00
|
|
|
// Iterate over all relocation sections that apply to this section.
|
2015-11-12 03:54:14 +08:00
|
|
|
for (const Elf_Shdr *RelSec : this->RelocSections) {
|
2015-09-22 06:01:00 +08:00
|
|
|
if (RelSec->sh_type == SHT_RELA)
|
2015-11-12 03:54:14 +08:00
|
|
|
this->relocate(Buf, BufEnd, EObj.relas(RelSec));
|
2015-09-22 06:01:00 +08:00
|
|
|
else
|
2015-11-12 03:54:14 +08:00
|
|
|
this->relocate(Buf, BufEnd, EObj.rels(RelSec));
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SplitInputSection<ELFT>::SplitInputSection(
|
|
|
|
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,
|
|
|
|
const Elf_Shdr *Header)
|
2015-12-24 18:08:54 +08: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 23:17:01 +08:00
|
|
|
// the garbage collector would drop all .eh_frame sections.
|
2015-12-24 18:08:54 +08:00
|
|
|
this->Live = true;
|
|
|
|
}
|
2015-11-12 03:54:14 +08: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 17:51:42 +08: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-12 03:54:14 +08:00
|
|
|
std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
|
|
|
|
uintX_t Base = I->second;
|
2015-11-13 21:44:59 +08:00
|
|
|
if (Base == uintX_t(-1))
|
2015-11-12 03:54:14 +08:00
|
|
|
return -1; // Not in the output
|
|
|
|
|
|
|
|
uintX_t Addend = Offset - I->first;
|
|
|
|
return Base + Addend;
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT>
|
|
|
|
MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
|
|
|
|
const Elf_Shdr *Header)
|
2015-11-12 03:54:14 +08:00
|
|
|
: SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
|
2015-10-20 05:00:02 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
2015-11-12 03:54:14 +08:00
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::Merge;
|
2015-10-20 05:00:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-11-12 03:54:14 +08: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-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)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("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(
|
2015-11-11 23:20:45 +08:00
|
|
|
Offsets.begin(), Offsets.end(), Offset,
|
2015-11-11 23:55:00 +08:00
|
|
|
[](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
|
2015-10-25 06:51:01 +08:00
|
|
|
return A < B.first;
|
|
|
|
});
|
2015-11-11 23:40:37 +08:00
|
|
|
uintX_t End = I == Offsets.end() ? Data.size() : I->first;
|
2015-10-25 06:51:01 +08:00
|
|
|
--I;
|
2015-11-12 03:54:14 +08:00
|
|
|
return std::make_pair(&*I, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename MergeInputSection<ELFT>::uintX_t
|
|
|
|
MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
|
2015-11-13 21:44:59 +08:00
|
|
|
std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
|
2015-11-12 03:54:14 +08:00
|
|
|
this->getRangeAndSize(Offset);
|
|
|
|
std::pair<uintX_t, uintX_t> *I = T.first;
|
|
|
|
uintX_t End = T.second;
|
2015-10-25 06:51:01 +08:00
|
|
|
uintX_t Start = I->first;
|
|
|
|
|
|
|
|
// Compute the Addend and if the Base is cached, return.
|
|
|
|
uintX_t Addend = Offset - Start;
|
2015-11-11 23:40:37 +08:00
|
|
|
uintX_t &Base = I->second;
|
2015-11-11 23:55:00 +08:00
|
|
|
if (Base != uintX_t(-1))
|
2015-10-25 06:51:01 +08:00
|
|
|
return Base + Addend;
|
|
|
|
|
2015-11-26 07:54:53 +08:00
|
|
|
// Map the base to the offset in the output section and cache it.
|
2015-11-12 03:54:14 +08:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
|
|
|
StringRef Data((const char *)D.data(), D.size());
|
2015-10-25 06:51:01 +08:00
|
|
|
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-12-20 18:57:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
|
2016-01-07 06:42:43 +08:00
|
|
|
const Elf_Shdr *Hdr)
|
|
|
|
: InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
|
|
|
|
// Initialize this->Reginfo.
|
2015-12-25 21:02:13 +08:00
|
|
|
ArrayRef<uint8_t> D = this->getSectionData();
|
2016-01-07 06:42:43 +08:00
|
|
|
if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid size of .reginfo section");
|
2016-01-07 06:42:43 +08:00
|
|
|
Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
|
2015-12-25 21:02:13 +08:00
|
|
|
}
|
|
|
|
|
2015-12-20 18:57:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
|
|
|
|
}
|
|
|
|
|
2016-01-23 05:49:07 +08:00
|
|
|
template class elf2::InputSectionBase<ELF32LE>;
|
|
|
|
template class elf2::InputSectionBase<ELF32BE>;
|
|
|
|
template class elf2::InputSectionBase<ELF64LE>;
|
|
|
|
template class elf2::InputSectionBase<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::InputSection<ELF32LE>;
|
|
|
|
template class elf2::InputSection<ELF32BE>;
|
|
|
|
template class elf2::InputSection<ELF64LE>;
|
|
|
|
template class elf2::InputSection<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::EHInputSection<ELF32LE>;
|
|
|
|
template class elf2::EHInputSection<ELF32BE>;
|
|
|
|
template class elf2::EHInputSection<ELF64LE>;
|
|
|
|
template class elf2::EHInputSection<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::MergeInputSection<ELF32LE>;
|
|
|
|
template class elf2::MergeInputSection<ELF32BE>;
|
|
|
|
template class elf2::MergeInputSection<ELF64LE>;
|
|
|
|
template class elf2::MergeInputSection<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::MipsReginfoInputSection<ELF32LE>;
|
|
|
|
template class elf2::MipsReginfoInputSection<ELF32BE>;
|
|
|
|
template class elf2::MipsReginfoInputSection<ELF64LE>;
|
|
|
|
template class elf2::MipsReginfoInputSection<ELF64BE>;
|