Skip to content

Commit

Permalink
[yaml2obj][obj2yaml] - Add support for dumping/parsing .dynamic secti…
Browse files Browse the repository at this point in the history
…ons.

This teaches the tools to parse and dump
the .dynamic section and its dynamic tags.

Differential revision: https://reviews.llvm.org/D57691

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@353606 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
George Rimar committed Feb 9, 2019
1 parent a7866b1 commit 0993cc8
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 125 deletions.
29 changes: 29 additions & 0 deletions include/llvm/ObjectYAML/ELFYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
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)
// Just use 64, since it can hold 32-bit values too.
LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
Expand Down Expand Up @@ -107,8 +109,14 @@ struct SectionOrType {
StringRef sectionNameOrType;
};

struct DynamicEntry {
ELF_DYNTAG Tag;
llvm::yaml::Hex64 Val;
};

struct Section {
enum class SectionKind {
Dynamic,
Group,
RawContent,
Relocation,
Expand All @@ -128,6 +136,17 @@ struct Section {
Section(SectionKind Kind) : Kind(Kind) {}
virtual ~Section();
};

struct DynamicSection : Section {
std::vector<DynamicEntry> Entries;

DynamicSection() : Section(SectionKind::Dynamic) {}

static bool classof(const Section *S) {
return S->Kind == SectionKind::Dynamic;
}
};

struct RawContentSection : Section {
yaml::BinaryRef Content;
llvm::yaml::Hex64 Size;
Expand Down Expand Up @@ -214,6 +233,7 @@ struct Object {
} // end namespace ELFYAML
} // end namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
Expand Down Expand Up @@ -296,6 +316,11 @@ struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
};

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

template <>
struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
Expand Down Expand Up @@ -351,6 +376,10 @@ struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
};

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

template <> struct MappingTraits<ELFYAML::Relocation> {
static void mapping(IO &IO, ELFYAML::Relocation &Rel);
};
Expand Down
46 changes: 46 additions & 0 deletions lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,33 @@ void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
IO.enumFallback<Hex32>(Value);
}

void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration(
IO &IO, ELFYAML::ELF_DYNTAG &Value) {
const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
assert(Object && "The IO context is not initialized");

// TODO: For simplicity we do not handle target specific flags. They are
// still supported and will be shown as a raw numeric values in the output.
#define MIPS_DYNAMIC_TAG(name, value)
#define HEXAGON_DYNAMIC_TAG(name, value)
#define PPC64_DYNAMIC_TAG(name, value)
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
#define DYNAMIC_TAG_MARKER(name, value)

#define STRINGIFY(X) (#X)
#define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
#include "llvm/BinaryFormat/DynamicTags.def"

#undef MIPS_DYNAMIC_TAG
#undef HEXAGON_DYNAMIC_TAG
#undef PPC64_DYNAMIC_TAG
#undef DYNAMIC_TAG_MARKER
#undef STRINGIFY
#undef DYNAMIC_TAG

IO.enumFallback<Hex64>(Value);
}

void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
#define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
Expand Down Expand Up @@ -831,6 +858,11 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
IO.mapOptional("Info", Section.Info, StringRef());
}

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

static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Content", Section.Content);
Expand Down Expand Up @@ -891,6 +923,11 @@ void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
IO.mapRequired("Type", sectionType);

switch (sectionType) {
case ELF::SHT_DYNAMIC:
if (!IO.outputting())
Section.reset(new ELFYAML::DynamicSection());
sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
break;
case ELF::SHT_REL:
case ELF::SHT_RELA:
if (!IO.outputting())
Expand Down Expand Up @@ -952,6 +989,15 @@ struct NormalizedMips64RelType {

} // end anonymous namespace

void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO,
ELFYAML::DynamicEntry &Rel) {
const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
assert(Object && "The IO context is not initialized");

IO.mapRequired("Tag", Rel.Tag);
IO.mapRequired("Value", Rel.Val);
}

void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
ELFYAML::Relocation &Rel) {
const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
Expand Down
20 changes: 12 additions & 8 deletions test/tools/llvm-elfabi/binary-read-add-soname.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x0008
AddressAlign: 8
Content: "0a000000000000000100000000000000050000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000"
# DT_STRSZ 1 (0x1)
# DT_STRTAB 0x0
# DT_SYMTAB 0x0
# DT_NULL 0x0
Size: 64
Address: 0x0000000000000008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_STRSZ
Value: 0x0000000000000001
- Tag: DT_STRTAB
Value: 0x0000000000000000
- Tag: DT_SYMTAB
Value: 0x0000000000000000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
20 changes: 12 additions & 8 deletions test/tools/llvm-elfabi/binary-read-arch.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x0008
AddressAlign: 8
Content: "0a000000000000000100000000000000050000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000"
# DT_STRSZ 1 (0x1)
# DT_STRTAB 0x0
# DT_SYMTAB 0x0
# DT_NULL 0x0
Size: 64
Address: 0x0000000000000008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_STRSZ
Value: 0x0000000000000001
- Tag: DT_STRTAB
Value: 0x0000000000000000
- Tag: DT_SYMTAB
Value: 0x0000000000000000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
23 changes: 14 additions & 9 deletions test/tools/llvm-elfabi/binary-read-bad-soname.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x0008
AddressAlign: 8
Content: "0e000000000000000d000000000000000a000000000000000100000000000000050000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000"
# DT_SONAME 13 (0x0d)
# DT_STRSZ 1 (0x01)
# DT_STRTAB 0x0
# DT_SYMTAB 0x0
# DT_NULL 0x0
Size: 80
Address: 0x0000000000000008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_SONAME
Value: 0x000000000000000D
- Tag: DT_STRSZ
Value: 0x0000000000000001
- Tag: DT_STRTAB
Value: 0x0000000000000000
- Tag: DT_SYMTAB
Value: 0x0000000000000000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
23 changes: 14 additions & 9 deletions test/tools/llvm-elfabi/binary-read-bad-vaddr.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x1008
AddressAlign: 8
Content: "0e0000000000000000000000000000000a000000000000000100000000000000050000000000000060020000000000000600000000000000001000000000000000000000000000000000000000000000"
# DT_SONAME 0
# DT_STRSZ 1
# DT_STRTAB 0x0260 # Bad vaddr (no PT_LOAD for 0x0000 to 0x0FFF)
# DT_SYMTAB 0x1000
# DT_NULL 0x0
Size: 80
Address: 0x0000000000001008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_SONAME
Value: 0x0000000000000000
- Tag: DT_STRSZ
Value: 0x0000000000000001
- Tag: DT_STRTAB
Value: 0x0000000000000260 # Bad vaddr (no PT_LOAD for 0x0000 to 0x0FFF)
- Tag: DT_SYMTAB
Value: 0x0000000000001000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
27 changes: 17 additions & 10 deletions test/tools/llvm-elfabi/binary-read-neededlibs-bad-offset.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x1024
Content: "010000000000000001000000000000000e0000000000000015000000000000000100000000000000ffff0000000000000a000000000000002400000000000000050000000000000000100000000000000600000000000000001000000000000000000000000000000000000000000000"
# DT_NEEDED 1 (0x01)
# DT_SONAME 21 (0x15)
Address: 0x0000000000001024
EntSize: 0x0000000000000010
Entries:
- Tag: DT_NEEDED
Value: 0x0000000000000001
- Tag: DT_SONAME
Value: 0x0000000000000015
# Bad DT_NEEDED entry (offset outside string table):
# DT_NEEDED 65535 (0xffff)
# DT_STRSZ 36 (0x24)
# DT_STRTAB 0x1000
# DT_SYMTAB 0x1000
# DT_NULL 0x0
Size: 112
- Tag: DT_NEEDED
Value: 0x000000000000FFFF
- Tag: DT_STRSZ
Value: 0x0000000000000024
- Tag: DT_STRTAB
Value: 0x0000000000001000
- Tag: DT_SYMTAB
Value: 0x0000000000001000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
27 changes: 17 additions & 10 deletions test/tools/llvm-elfabi/binary-read-neededlibs.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x1024
Content: "010000000000000001000000000000000e00000000000000150000000000000001000000000000000b000000000000000a000000000000002400000000000000050000000000000000100000000000000600000000000000001000000000000000000000000000000000000000000000"
# DT_NEEDED 1 (0x01)
# DT_SONAME 21 (0x15)
# DT_NEEDED 11 (0x0b)
# DT_STRSZ 36 (0x24)
# DT_STRTAB 0x1000
# DT_SYMTAB 0x1000
# DT_NULL 0x0
Size: 112
Address: 0x0000000000001024
EntSize: 0x0000000000000010
Entries:
- Tag: DT_NEEDED
Value: 0x0000000000000001
- Tag: DT_SONAME
Value: 0x0000000000000015
- Tag: DT_NEEDED
Value: 0x000000000000000B
- Tag: DT_STRSZ
Value: 0x0000000000000024
- Tag: DT_STRTAB
Value: 0x0000000000001000
- Tag: DT_SYMTAB
Value: 0x0000000000001000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
14 changes: 8 additions & 6 deletions test/tools/llvm-elfabi/binary-read-no-dt-strsz.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x0008
AddressAlign: 8
Content: "0500000000000000000000000000000000000000000000000000000000000000"
# DT_STRTAB 0x0
# DT_NULL 0x0
Size: 32
Address: 0x0000000000000008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_STRTAB
Value: 0x0000000000000000
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
14 changes: 8 additions & 6 deletions test/tools/llvm-elfabi/binary-read-no-dt-strtab.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Flags: [ SHF_ALLOC ]
Address: 0x0008
AddressAlign: 8
Content: "0a00000000000000010000000000000000000000000000000000000000000000"
# DT_STRSZ 1 (0x1)
# DT_NULL 0x0
Size: 32
Address: 0x0000000000000008
Link: .dynstr
AddressAlign: 0x0000000000000008
EntSize: 0x0000000000000010
Entries:
- Tag: DT_STRSZ
Value: 0x0000000000000001
- Tag: DT_NULL
Value: 0x0000000000000000
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_R ]
Expand Down
Loading

0 comments on commit 0993cc8

Please sign in to comment.