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;
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-09-22 01:47:00 +08:00
|
|
|
InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
|
2015-08-28 07:15:56 +08:00
|
|
|
: File(F), Header(Header) {}
|
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;
|
2015-09-24 23:11:50 +08:00
|
|
|
bool IsMips64EL = File.getObj().isMips64EL();
|
2015-09-22 06:01:00 +08:00
|
|
|
for (const RelType &RI : Rels) {
|
|
|
|
uint32_t SymIndex = RI.getSymbol(IsMips64EL);
|
|
|
|
uint32_t Type = RI.getType(IsMips64EL);
|
|
|
|
|
|
|
|
// 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-13 04:28:23 +08:00
|
|
|
uintX_t SymVA = getLocalRelTarget(File, RI);
|
2015-10-13 05:19:18 +08:00
|
|
|
Target->relocateOne(Buf, BufEnd, reinterpret_cast<const void *>(&RI),
|
|
|
|
Type, BaseAddr, 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-13 05:19:18 +08:00
|
|
|
Target->relocateOne(Buf, BufEnd, reinterpret_cast<const void *>(&RI), Type,
|
2015-10-09 04:06:07 +08:00
|
|
|
BaseAddr, SymVA);
|
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-07-25 05:03:07 +08:00
|
|
|
if (Header->sh_type == SHT_NOBITS)
|
|
|
|
return;
|
|
|
|
// Copy section contents from source object file to output file.
|
2015-09-24 23:11:50 +08:00
|
|
|
ArrayRef<uint8_t> Data = *File->getObj().getSectionContents(Header);
|
2015-10-15 09:58:40 +08:00
|
|
|
memcpy(Buf + OutSecOff, Data.data(), Data.size());
|
2015-09-22 06:01:00 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
ELFFile<ELFT> &EObj = File->getObj();
|
2015-10-15 09:58:40 +08:00
|
|
|
uint8_t *Base = Buf + OutSecOff;
|
|
|
|
uintX_t BaseAddr = 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-13 05:19:18 +08:00
|
|
|
relocate(Base, Base + Data.size(), EObj.relas(RelSec), *File, BaseAddr);
|
2015-09-22 06:01:00 +08:00
|
|
|
else
|
2015-10-13 05:19:18 +08:00
|
|
|
relocate(Base, Base + Data.size(), EObj.rels(RelSec), *File, BaseAddr);
|
2015-09-22 06:01:00 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 01:47:00 +08:00
|
|
|
template <class ELFT> StringRef InputSection<ELFT>::getSectionName() const {
|
2015-09-24 23:11:50 +08:00
|
|
|
ErrorOr<StringRef> Name = File->getObj().getSectionName(Header);
|
2015-08-14 03:18:30 +08:00
|
|
|
error(Name);
|
|
|
|
return *Name;
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
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-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|