llvm-project/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
Nick Kledzik e555277780 [lld] Introduce registry and Reference kind tuple
The main changes are in:
  include/lld/Core/Reference.h
  include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.

1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers.  It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.

The new model is that there is a global Registry object. You programmatically 
register the Readers you want with the registry object. Whenever you need to 
read/parse a file, you ask the registry to do it, and the registry tries each 
registered reader.

For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object. 


2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings.  Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values.  The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value.  This tuple system allows conversion to and from strings with 
no ambiguities.

llvm-svn: 197727
2013-12-19 21:58:00 +00:00

232 lines
7.4 KiB
C++

//===- lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h ----------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_READER_WRITER_ELF_HEXAGON_HEXAGON_TARGET_HANDLER_H
#define LLD_READER_WRITER_ELF_HEXAGON_HEXAGON_TARGET_HANDLER_H
#include "DefaultTargetHandler.h"
#include "HexagonExecutableAtoms.h"
#include "HexagonRelocationHandler.h"
#include "HexagonSectionChunks.h"
#include "TargetLayout.h"
namespace lld {
namespace elf {
typedef llvm::object::ELFType<llvm::support::little, 2, false> HexagonELFType;
class HexagonLinkingContext;
/// \brief Handle Hexagon specific Atoms
template <class HexagonELFType>
class HexagonTargetAtomHandler LLVM_FINAL :
public TargetAtomHandler<HexagonELFType> {
typedef llvm::object::Elf_Sym_Impl<HexagonELFType> Elf_Sym;
typedef llvm::object::Elf_Shdr_Impl<HexagonELFType> Elf_Shdr;
public:
virtual DefinedAtom::ContentType
contentType(const ELFDefinedAtom<HexagonELFType> *atom) const {
return contentType(atom->section(), atom->symbol());
}
virtual DefinedAtom::ContentType
contentType(const Elf_Shdr *section, const Elf_Sym *sym) const {
switch (sym->st_shndx) {
// Common symbols
case llvm::ELF::SHN_HEXAGON_SCOMMON:
case llvm::ELF::SHN_HEXAGON_SCOMMON_1:
case llvm::ELF::SHN_HEXAGON_SCOMMON_2:
case llvm::ELF::SHN_HEXAGON_SCOMMON_4:
case llvm::ELF::SHN_HEXAGON_SCOMMON_8:
return DefinedAtom::typeZeroFillFast;
default:
if (section->sh_type == llvm::ELF::SHT_NOBITS)
return DefinedAtom::typeZeroFillFast;
else if (section->sh_flags & llvm::ELF::SHF_HEX_GPREL)
return DefinedAtom::typeDataFast;
else
llvm_unreachable("unknown symbol type");
}
}
virtual DefinedAtom::ContentPermissions
contentPermissions(const ELFDefinedAtom<HexagonELFType> *atom) const {
// All of the hexagon specific symbols belong in the data segment
return DefinedAtom::permRW_;
}
virtual int64_t getType(const Elf_Sym *sym) const {
switch (sym->st_shndx) {
// Common symbols
case llvm::ELF::SHN_HEXAGON_SCOMMON:
case llvm::ELF::SHN_HEXAGON_SCOMMON_1:
case llvm::ELF::SHN_HEXAGON_SCOMMON_2:
case llvm::ELF::SHN_HEXAGON_SCOMMON_4:
case llvm::ELF::SHN_HEXAGON_SCOMMON_8:
return llvm::ELF::STT_COMMON;
default:
return sym->getType();
}
}
};
/// \brief TargetLayout for Hexagon
template <class HexagonELFType>
class HexagonTargetLayout LLVM_FINAL : public TargetLayout<HexagonELFType> {
public:
enum HexagonSectionOrder {
ORDER_SDATA = 205
};
HexagonTargetLayout(const HexagonLinkingContext &hti)
: TargetLayout<HexagonELFType>(hti), _sdataSection(nullptr) {
_sdataSection = new (_alloc) SDataSection<HexagonELFType>(hti);
}
/// \brief Return the section order for a input section
virtual Layout::SectionOrder getSectionOrder(
StringRef name, int32_t contentType, int32_t contentPermissions) {
if ((contentType == DefinedAtom::typeDataFast) ||
(contentType == DefinedAtom::typeZeroFillFast))
return ORDER_SDATA;
return DefaultLayout<HexagonELFType>::getSectionOrder(name, contentType,
contentPermissions);
}
/// \brief This maps the input sections to the output section names
virtual StringRef getSectionName(const DefinedAtom *da) const {
switch (da->contentType()) {
case DefinedAtom::typeDataFast:
case DefinedAtom::typeZeroFillFast:
return ".sdata";
default:
break;
}
return DefaultLayout<HexagonELFType>::getSectionName(da);
}
/// \brief Gets or creates a section.
virtual AtomSection<HexagonELFType> *
createSection(StringRef name, int32_t contentType,
DefinedAtom::ContentPermissions contentPermissions,
Layout::SectionOrder sectionOrder) {
if ((contentType == DefinedAtom::typeDataFast) ||
(contentType == DefinedAtom::typeZeroFillFast))
return _sdataSection;
return DefaultLayout<HexagonELFType>::createSection(
name, contentType, contentPermissions, sectionOrder);
}
/// \brief get the segment type for the section thats defined by the target
virtual Layout::SegmentType
getSegmentType(Section<HexagonELFType> *section) const {
if (section->order() == ORDER_SDATA)
return PT_LOAD;
return DefaultLayout<HexagonELFType>::getSegmentType(section);
}
Section<HexagonELFType> *getSDataSection() const {
return _sdataSection;
}
private:
llvm::BumpPtrAllocator _alloc;
SDataSection<HexagonELFType> *_sdataSection;
};
/// \brief TargetHandler for Hexagon
class HexagonTargetHandler LLVM_FINAL :
public DefaultTargetHandler<HexagonELFType> {
public:
HexagonTargetHandler(HexagonLinkingContext &targetInfo);
virtual void registerRelocationNames(Registry &registry);
bool doesOverrideELFHeader() { return true; }
void setELFHeader(ELFHeader<HexagonELFType> *elfHeader) {
elfHeader->e_ident(llvm::ELF::EI_VERSION, 1);
elfHeader->e_ident(llvm::ELF::EI_OSABI, 0);
elfHeader->e_version(1);
elfHeader->e_flags(0x3);
}
virtual HexagonTargetLayout<HexagonELFType> &targetLayout() {
return _targetLayout;
}
virtual HexagonTargetAtomHandler<HexagonELFType> &targetAtomHandler() {
return _targetAtomHandler;
}
virtual const HexagonTargetRelocationHandler &getRelocationHandler() const {
return _relocationHandler;
}
void addDefaultAtoms() {
_hexagonRuntimeFile->addAbsoluteAtom("_SDA_BASE_");
if (_context.isDynamic()) {
_hexagonRuntimeFile->addAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
_hexagonRuntimeFile->addAbsoluteAtom("_DYNAMIC");
}
}
virtual bool
createImplicitFiles(std::vector<std::unique_ptr<File> > &result) {
// Add the default atoms as defined for hexagon
addDefaultAtoms();
result.push_back(std::move(_hexagonRuntimeFile));
return true;
}
void finalizeSymbolValues() {
auto sdabaseAtomIter = _targetLayout.findAbsoluteAtom("_SDA_BASE_");
(*sdabaseAtomIter)->_virtualAddr =
_targetLayout.getSDataSection()->virtualAddr();
if (_context.isDynamic()) {
auto gotAtomIter =
_targetLayout.findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
_gotSymAtom = (*gotAtomIter);
auto gotpltSection = _targetLayout.findOutputSection(".got.plt");
if (gotpltSection)
_gotSymAtom->_virtualAddr = gotpltSection->virtualAddr();
else
_gotSymAtom->_virtualAddr = 0;
auto dynamicAtomIter = _targetLayout.findAbsoluteAtom("_DYNAMIC");
auto dynamicSection = _targetLayout.findOutputSection(".dynamic");
if (dynamicSection)
(*dynamicAtomIter)->_virtualAddr = dynamicSection->virtualAddr();
else
(*dynamicAtomIter)->_virtualAddr = 0;
}
}
uint64_t getGOTSymAddr() const {
if (!_gotSymAtom) return 0;
return _gotSymAtom->_virtualAddr;
}
private:
static const Registry::KindStrings kindStrings[];
HexagonTargetLayout<HexagonELFType> _targetLayout;
HexagonTargetRelocationHandler _relocationHandler;
HexagonTargetAtomHandler<HexagonELFType> _targetAtomHandler;
std::unique_ptr<HexagonRuntimeFile<HexagonELFType> > _hexagonRuntimeFile;
AtomLayout *_gotSymAtom;
};
} // end namespace elf
} // end namespace lld
#endif