Skip to content

Commit

Permalink
[yaml2obj][ELF] ELF Relocations Support.
Browse files Browse the repository at this point in the history
The patch implements support for both relocation record formats: Elf_Rel
and Elf_Rela. It is possible to define relocation against symbol only.
Relocations against sections will be implemented later. Now yaml2obj
recognizes X86_64, MIPS and Hexagon relocation types.

Example of relocation section specification:
Sections:
- Name: .text
  Type: SHT_PROGBITS
  Content: "0000000000000000"
  AddressAlign: 16
  Flags: [SHF_ALLOC]

- Name: .rel.text
  Type: SHT_REL
  Info: .text
  AddressAlign: 4
  Relocations:
    - Offset: 0x1
      Symbol: glob1
      Type: R_MIPS_32
    - Offset: 0x2
      Symbol: glob2
      Type: R_MIPS_CALL16

The patch reviewed by Michael Spencer, Sean Silva, Shankar Easwaran.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206017 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
atanasyan committed Apr 11, 2014
1 parent 77cf856 commit eb0c909
Show file tree
Hide file tree
Showing 4 changed files with 531 additions and 24 deletions.
44 changes: 39 additions & 5 deletions include/llvm/Object/ELFYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
// Just use 64, since it can hold 32-bit values too.
LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_REL)
// Just use 64, since it can hold 32-bit values too.
LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
Expand Down Expand Up @@ -68,17 +69,40 @@ struct LocalGlobalWeakSymbols {
std::vector<Symbol> Weak;
};
struct Section {
enum class SectionKind { RawContent, Relocation };
SectionKind Kind;
StringRef Name;
ELF_SHT Type;
ELF_SHF Flags;
llvm::yaml::Hex64 Address;
object::yaml::BinaryRef Content;
StringRef Link;
StringRef Info;
llvm::yaml::Hex64 AddressAlign;
Section(SectionKind Kind) : Kind(Kind) {}
};
struct RawContentSection : Section {
object::yaml::BinaryRef Content;
RawContentSection() : Section(SectionKind::RawContent) {}
static bool classof(const Section *S) {
return S->Kind == SectionKind::RawContent;
}
};
struct Relocation {
uint32_t Offset;
uint32_t Addend;
ELF_REL Type;
StringRef Symbol;
};
struct RelocationSection : Section {
std::vector<Relocation> Relocations;
RelocationSection() : Section(SectionKind::Relocation) {}
static bool classof(const Section *S) {
return S->Kind == SectionKind::Relocation;
}
};
struct Object {
FileHeader Header;
std::vector<Section> Sections;
std::vector<std::unique_ptr<Section>> Sections;
// Although in reality the symbols reside in a section, it is a lot
// cleaner and nicer if we read them from the YAML as a separate
// top-level key, which automatically ensures that invariants like there
Expand All @@ -89,8 +113,9 @@ struct Object {
} // end namespace ELFYAML
} // end namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Section)
LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)

namespace llvm {
namespace yaml {
Expand Down Expand Up @@ -140,6 +165,11 @@ struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
};

template <>
struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
};

template <>
struct MappingTraits<ELFYAML::FileHeader> {
static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
Expand All @@ -155,9 +185,13 @@ struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
};

template <> struct MappingTraits<ELFYAML::Relocation> {
static void mapping(IO &IO, ELFYAML::Relocation &Rel);
};

template <>
struct MappingTraits<ELFYAML::Section> {
static void mapping(IO &IO, ELFYAML::Section &Section);
struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
};

template <>
Expand Down
259 changes: 256 additions & 3 deletions lib/Object/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,218 @@ void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
#undef ECase
}

void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
IO &IO, ELFYAML::ELF_REL &Value) {
const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
assert(Object && "The IO context is not initialized");
#define ECase(X) IO.enumCase(Value, #X, ELF::X);
switch (Object->Header.Machine) {
case ELF::EM_X86_64:
ECase(R_X86_64_NONE)
ECase(R_X86_64_64)
ECase(R_X86_64_PC32)
ECase(R_X86_64_GOT32)
ECase(R_X86_64_PLT32)
ECase(R_X86_64_COPY)
ECase(R_X86_64_GLOB_DAT)
ECase(R_X86_64_JUMP_SLOT)
ECase(R_X86_64_RELATIVE)
ECase(R_X86_64_GOTPCREL)
ECase(R_X86_64_32)
ECase(R_X86_64_32S)
ECase(R_X86_64_16)
ECase(R_X86_64_PC16)
ECase(R_X86_64_8)
ECase(R_X86_64_PC8)
ECase(R_X86_64_DTPMOD64)
ECase(R_X86_64_DTPOFF64)
ECase(R_X86_64_TPOFF64)
ECase(R_X86_64_TLSGD)
ECase(R_X86_64_TLSLD)
ECase(R_X86_64_DTPOFF32)
ECase(R_X86_64_GOTTPOFF)
ECase(R_X86_64_TPOFF32)
ECase(R_X86_64_PC64)
ECase(R_X86_64_GOTOFF64)
ECase(R_X86_64_GOTPC32)
ECase(R_X86_64_GOT64)
ECase(R_X86_64_GOTPCREL64)
ECase(R_X86_64_GOTPC64)
ECase(R_X86_64_GOTPLT64)
ECase(R_X86_64_PLTOFF64)
ECase(R_X86_64_SIZE32)
ECase(R_X86_64_SIZE64)
ECase(R_X86_64_GOTPC32_TLSDESC)
ECase(R_X86_64_TLSDESC_CALL)
ECase(R_X86_64_TLSDESC)
ECase(R_X86_64_IRELATIVE)
break;
case ELF::EM_MIPS:
ECase(R_MIPS_NONE)
ECase(R_MIPS_16)
ECase(R_MIPS_32)
ECase(R_MIPS_REL32)
ECase(R_MIPS_26)
ECase(R_MIPS_HI16)
ECase(R_MIPS_LO16)
ECase(R_MIPS_GPREL16)
ECase(R_MIPS_LITERAL)
ECase(R_MIPS_GOT16)
ECase(R_MIPS_PC16)
ECase(R_MIPS_CALL16)
ECase(R_MIPS_GPREL32)
ECase(R_MIPS_UNUSED1)
ECase(R_MIPS_UNUSED2)
ECase(R_MIPS_SHIFT5)
ECase(R_MIPS_SHIFT6)
ECase(R_MIPS_64)
ECase(R_MIPS_GOT_DISP)
ECase(R_MIPS_GOT_PAGE)
ECase(R_MIPS_GOT_OFST)
ECase(R_MIPS_GOT_HI16)
ECase(R_MIPS_GOT_LO16)
ECase(R_MIPS_SUB)
ECase(R_MIPS_INSERT_A)
ECase(R_MIPS_INSERT_B)
ECase(R_MIPS_DELETE)
ECase(R_MIPS_HIGHER)
ECase(R_MIPS_HIGHEST)
ECase(R_MIPS_CALL_HI16)
ECase(R_MIPS_CALL_LO16)
ECase(R_MIPS_SCN_DISP)
ECase(R_MIPS_REL16)
ECase(R_MIPS_ADD_IMMEDIATE)
ECase(R_MIPS_PJUMP)
ECase(R_MIPS_RELGOT)
ECase(R_MIPS_JALR)
ECase(R_MIPS_TLS_DTPMOD32)
ECase(R_MIPS_TLS_DTPREL32)
ECase(R_MIPS_TLS_DTPMOD64)
ECase(R_MIPS_TLS_DTPREL64)
ECase(R_MIPS_TLS_GD)
ECase(R_MIPS_TLS_LDM)
ECase(R_MIPS_TLS_DTPREL_HI16)
ECase(R_MIPS_TLS_DTPREL_LO16)
ECase(R_MIPS_TLS_GOTTPREL)
ECase(R_MIPS_TLS_TPREL32)
ECase(R_MIPS_TLS_TPREL64)
ECase(R_MIPS_TLS_TPREL_HI16)
ECase(R_MIPS_TLS_TPREL_LO16)
ECase(R_MIPS_GLOB_DAT)
ECase(R_MIPS_COPY)
ECase(R_MIPS_JUMP_SLOT)
ECase(R_MICROMIPS_26_S1)
ECase(R_MICROMIPS_HI16)
ECase(R_MICROMIPS_LO16)
ECase(R_MICROMIPS_GOT16)
ECase(R_MICROMIPS_PC16_S1)
ECase(R_MICROMIPS_CALL16)
ECase(R_MICROMIPS_GOT_DISP)
ECase(R_MICROMIPS_GOT_PAGE)
ECase(R_MICROMIPS_GOT_OFST)
ECase(R_MICROMIPS_TLS_GD)
ECase(R_MICROMIPS_TLS_LDM)
ECase(R_MICROMIPS_TLS_DTPREL_HI16)
ECase(R_MICROMIPS_TLS_DTPREL_LO16)
ECase(R_MICROMIPS_TLS_TPREL_HI16)
ECase(R_MICROMIPS_TLS_TPREL_LO16)
ECase(R_MIPS_NUM)
ECase(R_MIPS_PC32)
break;
case ELF::EM_HEXAGON:
ECase(R_HEX_NONE)
ECase(R_HEX_B22_PCREL)
ECase(R_HEX_B15_PCREL)
ECase(R_HEX_B7_PCREL)
ECase(R_HEX_LO16)
ECase(R_HEX_HI16)
ECase(R_HEX_32)
ECase(R_HEX_16)
ECase(R_HEX_8)
ECase(R_HEX_GPREL16_0)
ECase(R_HEX_GPREL16_1)
ECase(R_HEX_GPREL16_2)
ECase(R_HEX_GPREL16_3)
ECase(R_HEX_HL16)
ECase(R_HEX_B13_PCREL)
ECase(R_HEX_B9_PCREL)
ECase(R_HEX_B32_PCREL_X)
ECase(R_HEX_32_6_X)
ECase(R_HEX_B22_PCREL_X)
ECase(R_HEX_B15_PCREL_X)
ECase(R_HEX_B13_PCREL_X)
ECase(R_HEX_B9_PCREL_X)
ECase(R_HEX_B7_PCREL_X)
ECase(R_HEX_16_X)
ECase(R_HEX_12_X)
ECase(R_HEX_11_X)
ECase(R_HEX_10_X)
ECase(R_HEX_9_X)
ECase(R_HEX_8_X)
ECase(R_HEX_7_X)
ECase(R_HEX_6_X)
ECase(R_HEX_32_PCREL)
ECase(R_HEX_COPY)
ECase(R_HEX_GLOB_DAT)
ECase(R_HEX_JMP_SLOT)
ECase(R_HEX_RELATIVE)
ECase(R_HEX_PLT_B22_PCREL)
ECase(R_HEX_GOTREL_LO16)
ECase(R_HEX_GOTREL_HI16)
ECase(R_HEX_GOTREL_32)
ECase(R_HEX_GOT_LO16)
ECase(R_HEX_GOT_HI16)
ECase(R_HEX_GOT_32)
ECase(R_HEX_GOT_16)
ECase(R_HEX_DTPMOD_32)
ECase(R_HEX_DTPREL_LO16)
ECase(R_HEX_DTPREL_HI16)
ECase(R_HEX_DTPREL_32)
ECase(R_HEX_DTPREL_16)
ECase(R_HEX_GD_PLT_B22_PCREL)
ECase(R_HEX_GD_GOT_LO16)
ECase(R_HEX_GD_GOT_HI16)
ECase(R_HEX_GD_GOT_32)
ECase(R_HEX_GD_GOT_16)
ECase(R_HEX_IE_LO16)
ECase(R_HEX_IE_HI16)
ECase(R_HEX_IE_32)
ECase(R_HEX_IE_GOT_LO16)
ECase(R_HEX_IE_GOT_HI16)
ECase(R_HEX_IE_GOT_32)
ECase(R_HEX_IE_GOT_16)
ECase(R_HEX_TPREL_LO16)
ECase(R_HEX_TPREL_HI16)
ECase(R_HEX_TPREL_32)
ECase(R_HEX_TPREL_16)
ECase(R_HEX_6_PCREL_X)
ECase(R_HEX_GOTREL_32_6_X)
ECase(R_HEX_GOTREL_16_X)
ECase(R_HEX_GOTREL_11_X)
ECase(R_HEX_GOT_32_6_X)
ECase(R_HEX_GOT_16_X)
ECase(R_HEX_GOT_11_X)
ECase(R_HEX_DTPREL_32_6_X)
ECase(R_HEX_DTPREL_16_X)
ECase(R_HEX_DTPREL_11_X)
ECase(R_HEX_GD_GOT_32_6_X)
ECase(R_HEX_GD_GOT_16_X)
ECase(R_HEX_GD_GOT_11_X)
ECase(R_HEX_IE_32_6_X)
ECase(R_HEX_IE_16_X)
ECase(R_HEX_IE_GOT_32_6_X)
ECase(R_HEX_IE_GOT_16_X)
ECase(R_HEX_IE_GOT_11_X)
ECase(R_HEX_TPREL_32_6_X)
ECase(R_HEX_TPREL_16_X)
ECase(R_HEX_TPREL_11_X)
break;
default:
llvm_unreachable("Unsupported architecture");
}
#undef ECase
}

void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
ELFYAML::FileHeader &FileHdr) {
IO.mapRequired("Class", FileHdr.Class);
Expand All @@ -360,21 +572,62 @@ void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping(
IO.mapOptional("Weak", Symbols.Weak);
}

void MappingTraits<ELFYAML::Section>::mapping(IO &IO,
ELFYAML::Section &Section) {
static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
IO.mapOptional("Name", Section.Name, StringRef());
IO.mapRequired("Type", Section.Type);
IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
IO.mapOptional("Address", Section.Address, Hex64(0));
IO.mapOptional("Content", Section.Content);
IO.mapOptional("Link", Section.Link);
IO.mapOptional("Info", Section.Info);
IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
}

static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Content", Section.Content);
}

static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Relocations", Section.Relocations);
}

void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
ELFYAML::ELF_SHT sectionType;
if (IO.outputting())
sectionType = Section->Type;
IO.mapRequired("Type", sectionType);

switch (sectionType) {
case ELF::SHT_REL:
case ELF::SHT_RELA:
if (!IO.outputting())
Section.reset(new ELFYAML::RelocationSection());
sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
break;
default:
if (!IO.outputting())
Section.reset(new ELFYAML::RawContentSection());
sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
}
}

void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
ELFYAML::Relocation &Rel) {
IO.mapRequired("Offset", Rel.Offset);
IO.mapRequired("Symbol", Rel.Symbol);
IO.mapRequired("Type", Rel.Type);
IO.mapOptional("Addend", Rel.Addend);
}

void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
assert(!IO.getContext() && "The IO context is initialized already");
IO.setContext(&Object);
IO.mapRequired("FileHeader", Object.Header);
IO.mapOptional("Sections", Object.Sections);
IO.mapOptional("Symbols", Object.Symbols);
IO.setContext(nullptr);
}

} // end namespace yaml
Expand Down
Loading

0 comments on commit eb0c909

Please sign in to comment.