ELF: Move registerRelocationNames() from TargetHandler to <Arch>ELFLinkingContext.

registerRelocationNames() function is called to register all known
ELF relocation types to the central registry. Since we have separate
LinkingContext class for each ELF machine type, we need to call the
function for each LinkingContext.

However, the function belonged to TargetHandler instead of LinkingContext.
So we needed to do ctx.getTargetHandler().registerRelocationNames().
This patch removes that redundant getTargetHandler call by moving the
function from TargetHandler to LinkingContext.

Conceptually this patch is small, but in reality it's not that small.
It's because the same code is copied to each architecture.
Most of this patch is just repetition. We need to merge them, but
that cannot be done in this patch.

llvm-svn: 233883
This commit is contained in:
Rui Ueyama 2015-04-02 04:18:54 +00:00
parent 0052736eb1
commit 7d5492d9ab
28 changed files with 118 additions and 139 deletions

View File

@ -55,7 +55,6 @@ public:
class TargetHandler {
public:
virtual ~TargetHandler() {}
virtual void registerRelocationNames(Registry &) = 0;
/// Determines how relocations need to be applied.
virtual const elf::TargetRelocationHandler &getRelocationHandler() const = 0;
@ -173,6 +172,8 @@ public:
return *_targetHandler;
}
virtual void registerRelocationNames(Registry &) = 0;
void addPasses(PassManager &pm) override;
void setTriple(llvm::Triple trip) { _triple = trip; }

View File

@ -16,7 +16,7 @@ namespace lld {
namespace elf {
#define LLVM_TARGET(TargetName) \
class TargetName##LinkingContext final : public ELFLinkingContext { \
class TargetName##LinkingContext : public ELFLinkingContext { \
public: \
static std::unique_ptr<ELFLinkingContext> create(llvm::Triple); \
};

View File

@ -12,21 +12,34 @@
#include "AArch64TargetHandler.h"
using namespace lld;
using namespace lld::elf;
std::unique_ptr<ELFLinkingContext>
elf::AArch64LinkingContext::create(llvm::Triple triple) {
AArch64LinkingContext::create(llvm::Triple triple) {
if (triple.getArch() == llvm::Triple::aarch64)
return llvm::make_unique<elf::AArch64LinkingContext>(triple);
return llvm::make_unique<AArch64LinkingContext>(triple);
return nullptr;
}
elf::AArch64LinkingContext::AArch64LinkingContext(llvm::Triple triple)
AArch64LinkingContext::AArch64LinkingContext(llvm::Triple triple)
: ELFLinkingContext(triple, std::unique_ptr<TargetHandler>(
new AArch64TargetHandler(*this))) {}
void elf::AArch64LinkingContext::addPasses(PassManager &pm) {
void AArch64LinkingContext::addPasses(PassManager &pm) {
auto pass = createAArch64RelocationPass(*this);
if (pass)
pm.add(std::move(pass));
ELFLinkingContext::addPasses(pm);
}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/AArch64.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void AArch64LinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::AArch64, kindStrings);
}

View File

@ -28,6 +28,7 @@ public:
AArch64LinkingContext(llvm::Triple);
void addPasses(PassManager &) override;
void registerRelocationNames(Registry &r) override;
uint64_t getBaseAddress() const override {
if (_baseAddress == 0)

View File

@ -20,18 +20,6 @@ AArch64TargetHandler::AArch64TargetHandler(AArch64LinkingContext &ctx)
: _ctx(ctx), _targetLayout(new TargetLayout<AArch64ELFType>(ctx)),
_relocationHandler(new AArch64TargetRelocationHandler()) {}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/AArch64.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void AArch64TargetHandler::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::AArch64, kindStrings);
}
std::unique_ptr<Writer> AArch64TargetHandler::getWriter() {
switch (this->_ctx.getOutputELFType()) {
case llvm::ELF::ET_EXEC:

View File

@ -24,8 +24,6 @@ class AArch64TargetHandler final : public TargetHandler {
public:
AArch64TargetHandler(AArch64LinkingContext &ctx);
void registerRelocationNames(Registry &registry) override;
const AArch64TargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}

View File

@ -11,39 +11,54 @@
#include "ARMRelocationPass.h"
#include "ARMTargetHandler.h"
using namespace lld;
using namespace lld::elf;
namespace lld {
namespace elf {
std::unique_ptr<ELFLinkingContext>
elf::ARMLinkingContext::create(llvm::Triple triple) {
ARMLinkingContext::create(llvm::Triple triple) {
if (triple.getArch() == llvm::Triple::arm)
return llvm::make_unique<elf::ARMLinkingContext>(triple);
return llvm::make_unique<ARMLinkingContext>(triple);
return nullptr;
}
elf::ARMLinkingContext::ARMLinkingContext(llvm::Triple triple)
ARMLinkingContext::ARMLinkingContext(llvm::Triple triple)
: ELFLinkingContext(triple, llvm::make_unique<ARMTargetHandler>(*this)) {}
void elf::ARMLinkingContext::addPasses(PassManager &pm) {
void ARMLinkingContext::addPasses(PassManager &pm) {
auto pass = createARMRelocationPass(*this);
if (pass)
pm.add(std::move(pass));
ELFLinkingContext::addPasses(pm);
}
bool elf::isARMCode(const DefinedAtom *atom) {
bool isARMCode(const DefinedAtom *atom) {
return isARMCode(atom->codeModel());
}
bool elf::isARMCode(DefinedAtom::CodeModel codeModel) {
bool isARMCode(DefinedAtom::CodeModel codeModel) {
return !isThumbCode(codeModel);
}
bool elf::isThumbCode(const DefinedAtom *atom) {
bool isThumbCode(const DefinedAtom *atom) {
return isThumbCode(atom->codeModel());
}
bool elf::isThumbCode(DefinedAtom::CodeModel codeModel) {
bool isThumbCode(DefinedAtom::CodeModel codeModel) {
return codeModel == DefinedAtom::codeARMThumb ||
codeModel == DefinedAtom::codeARM_t;
}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/ARM.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void ARMLinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF, Reference::KindArch::ARM,
kindStrings);
}
} // namespace elf
} // namespace lld

View File

@ -22,9 +22,10 @@ public:
static std::unique_ptr<ELFLinkingContext> create(llvm::Triple);
ARMLinkingContext(llvm::Triple);
bool isRelaOutputFormat() const override { return false; }
void addPasses(PassManager &) override;
void registerRelocationNames(Registry &r) override;
bool isRelaOutputFormat() const override { return false; }
uint64_t getBaseAddress() const override {
if (_baseAddress == 0)

View File

@ -19,18 +19,6 @@ ARMTargetHandler::ARMTargetHandler(ARMLinkingContext &ctx)
: _ctx(ctx), _targetLayout(new ARMTargetLayout<ARMELFType>(ctx)),
_relocationHandler(new ARMTargetRelocationHandler(*_targetLayout)) {}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/ARM.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void ARMTargetHandler::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF, Reference::KindArch::ARM,
kindStrings);
}
std::unique_ptr<Writer> ARMTargetHandler::getWriter() {
switch (this->_ctx.getOutputELFType()) {
case llvm::ELF::ET_EXEC:

View File

@ -61,8 +61,6 @@ class ARMTargetHandler final : public TargetHandler {
public:
ARMTargetHandler(ARMLinkingContext &ctx);
void registerRelocationNames(Registry &registry) override;
const ARMTargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}

View File

@ -10,6 +10,7 @@
#include "HexagonLinkingContext.h"
#include "HexagonTargetHandler.h"
using namespace lld;
using namespace lld::elf;
std::unique_ptr<lld::ELFLinkingContext>
@ -22,3 +23,15 @@ HexagonLinkingContext::create(llvm::Triple triple) {
HexagonLinkingContext::HexagonLinkingContext(llvm::Triple triple)
: ELFLinkingContext(triple, std::unique_ptr<TargetHandler>(
new HexagonTargetHandler(*this))) {}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/Hexagon.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void HexagonLinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::Hexagon, kindStrings);
}

View File

@ -26,6 +26,7 @@ public:
HexagonLinkingContext(llvm::Triple triple);
void addPasses(PassManager &) override;
void registerRelocationNames(Registry &r) override;
bool isDynamicRelocation(const Reference &r) const override {
if (r.kindNamespace() != Reference::KindNamespace::ELF)

View File

@ -313,15 +313,3 @@ void elf::HexagonLinkingContext::addPasses(PassManager &pm) {
pm.add(llvm::make_unique<DynamicGOTPLTPass>(*this));
ELFLinkingContext::addPasses(pm);
}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/Hexagon.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void HexagonTargetHandler::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::Hexagon, kindStrings);
}

View File

@ -100,8 +100,6 @@ class HexagonTargetHandler final : public TargetHandler {
public:
HexagonTargetHandler(HexagonLinkingContext &targetInfo);
void registerRelocationNames(Registry &registry) override;
const HexagonTargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}

View File

@ -4,7 +4,6 @@ add_llvm_library(lldMipsELFTarget
MipsLinkingContext.cpp
MipsRelocationHandler.cpp
MipsRelocationPass.cpp
MipsTargetHandler.cpp
LINK_LIBS
lldELF
lldReaderWriter

View File

@ -127,3 +127,23 @@ bool MipsLinkingContext::isRelativeReloc(const Reference &r) const {
return false;
}
}
const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/Mips.def"
#undef ELF_RELOC
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_GOT),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_32_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_64_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_26),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_LO16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_STO_PLT),
LLD_KIND_STRING_ENTRY(LLD_R_MICROMIPS_GLOBAL_26_S1),
LLD_KIND_STRING_END
};
void MipsLinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::Mips, kindStrings);
}

View File

@ -47,6 +47,7 @@ public:
uint32_t getMergedELFFlags() const;
MipsELFFlagsMerger &getELFFlagsMerger();
void registerRelocationNames(Registry &r) override;
// ELFLinkingContext
uint64_t getBaseAddress() const override;

View File

@ -1,33 +0,0 @@
//===- lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp --------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "MipsTargetHandler.h"
using namespace lld;
using namespace elf;
const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/Mips.def"
#undef ELF_RELOC
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_GOT),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_32_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_64_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_GLOBAL_26),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_HI16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_LO16),
LLD_KIND_STRING_ENTRY(LLD_R_MIPS_STO_PLT),
LLD_KIND_STRING_ENTRY(LLD_R_MICROMIPS_GLOBAL_26_S1),
LLD_KIND_STRING_END
};
void MipsRelocationStringTable::registerTable(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::Mips, kindStrings);
}

View File

@ -90,12 +90,6 @@ public:
: RuntimeFile<ELFT>(ctx, "Mips runtime file") {}
};
/// \brief Auxiliary class holds relocation's names table.
class MipsRelocationStringTable {
public:
static void registerTable(Registry &registry);
};
/// \brief TargetHandler for Mips
template <class ELFT> class MipsTargetHandler final : public TargetHandler {
public:
@ -131,10 +125,6 @@ public:
}
}
void registerRelocationNames(Registry &registry) override {
MipsRelocationStringTable::registerTable(registry);
}
private:
MipsLinkingContext &_ctx;
std::unique_ptr<MipsRuntimeFile<ELFT>> _runtimeFile;

View File

@ -32,7 +32,7 @@ void Registry::addSupportELFObjects(ELFLinkingContext &ctx) {
add(std::move(ctx.getTargetHandler().getObjReader()));
// Tell registry about the relocation name to number mapping for this arch.
ctx.getTargetHandler().registerRelocationNames(*this);
ctx.registerRelocationNames(*this);
}
void Registry::addSupportELFDynamicSharedObjects(ELFLinkingContext &ctx) {

View File

@ -14,13 +14,26 @@
#include "llvm/Support/ErrorOr.h"
using namespace lld;
using namespace lld::elf;
std::unique_ptr<ELFLinkingContext>
elf::X86LinkingContext::create(llvm::Triple triple) {
X86LinkingContext::create(llvm::Triple triple) {
if (triple.getArch() == llvm::Triple::x86)
return llvm::make_unique<elf::X86LinkingContext>(triple);
return llvm::make_unique<X86LinkingContext>(triple);
return nullptr;
}
elf::X86LinkingContext::X86LinkingContext(llvm::Triple triple)
X86LinkingContext::X86LinkingContext(llvm::Triple triple)
: ELFLinkingContext(triple, llvm::make_unique<X86TargetHandler>(*this)) {}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/i386.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void X86LinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF, Reference::KindArch::x86,
kindStrings);
}

View File

@ -20,6 +20,7 @@ class X86LinkingContext final : public ELFLinkingContext {
public:
static std::unique_ptr<ELFLinkingContext> create(llvm::Triple);
X86LinkingContext(llvm::Triple);
void registerRelocationNames(Registry &r) override;
/// \brief X86 has only two relative relocation
/// a) for supporting IFUNC relocs - R_386_IRELATIVE

View File

@ -33,18 +33,6 @@ std::unique_ptr<Writer> X86TargetHandler::getWriter() {
}
}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/i386.def"
#undef ELF_RELOC
LLD_KIND_STRING_END
};
void X86TargetHandler::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF, Reference::KindArch::x86,
kindStrings);
}
X86TargetHandler::X86TargetHandler(X86LinkingContext &ctx)
: _ctx(ctx), _targetLayout(new TargetLayout<X86ELFType>(ctx)),
_relocationHandler(new X86TargetRelocationHandler()) {}

View File

@ -24,8 +24,6 @@ class X86TargetHandler final : public TargetHandler {
public:
X86TargetHandler(X86LinkingContext &ctx);
void registerRelocationNames(Registry &registry) override;
const X86TargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}

View File

@ -12,7 +12,7 @@
#include "X86_64TargetHandler.h"
using namespace lld;
using namespace elf;
using namespace lld::elf;
X86_64LinkingContext::X86_64LinkingContext(
llvm::Triple triple, std::unique_ptr<TargetHandler> handler)
@ -32,6 +32,19 @@ void X86_64LinkingContext::addPasses(PassManager &pm) {
std::unique_ptr<ELFLinkingContext>
X86_64LinkingContext::create(llvm::Triple triple) {
if (triple.getArch() == llvm::Triple::x86_64)
return llvm::make_unique<elf::X86_64LinkingContext>(triple);
return llvm::make_unique<X86_64LinkingContext>(triple);
return nullptr;
}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/x86_64.def"
#undef ELF_RELOC
LLD_KIND_STRING_ENTRY(LLD_R_X86_64_GOTRELINDEX),
LLD_KIND_STRING_END
};
void X86_64LinkingContext::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::x86_64, kindStrings);
}

View File

@ -33,6 +33,7 @@ public:
X86_64LinkingContext(llvm::Triple);
void addPasses(PassManager &) override;
void registerRelocationNames(Registry &r) override;
uint64_t getBaseAddress() const override {
if (_baseAddress == 0)

View File

@ -20,19 +20,6 @@ X86_64TargetHandler::X86_64TargetHandler(X86_64LinkingContext &ctx)
: _ctx(ctx), _targetLayout(new X86_64TargetLayout(ctx)),
_relocationHandler(new X86_64TargetRelocationHandler(*_targetLayout)) {}
static const Registry::KindStrings kindStrings[] = {
#define ELF_RELOC(name, value) LLD_KIND_STRING_ENTRY(name),
#include "llvm/Support/ELFRelocs/x86_64.def"
#undef ELF_RELOC
LLD_KIND_STRING_ENTRY(LLD_R_X86_64_GOTRELINDEX),
LLD_KIND_STRING_END
};
void X86_64TargetHandler::registerRelocationNames(Registry &registry) {
registry.addKindTable(Reference::KindNamespace::ELF,
Reference::KindArch::x86_64, kindStrings);
}
std::unique_ptr<Writer> X86_64TargetHandler::getWriter() {
switch (this->_ctx.getOutputELFType()) {
case llvm::ELF::ET_EXEC:

View File

@ -62,8 +62,6 @@ class X86_64TargetHandler : public TargetHandler {
public:
X86_64TargetHandler(X86_64LinkingContext &ctx);
void registerRelocationNames(Registry &registry) override;
const X86_64TargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}