1 //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file declares classes for handling the YAML representation
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_OBJECT_ELFYAML_H
17 #define LLVM_OBJECT_ELFYAML_H
19 #include "llvm/MC/YAML.h"
20 #include "llvm/Support/ELF.h"
25 // These types are invariant across 32/64-bit ELF, so for simplicity just
26 // directly give them their exact sizes. We don't need to worry about
27 // endianness because these are just the types in the YAMLIO structures,
28 // and are appropriately converted to the necessary endianness when
29 // reading/generating binary object files.
30 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
31 // the common prefix of the respective constants. E.g. ELF_EM corresponds
32 // to the `e_machine` constants, like `EM_X86_64`.
33 // In the future, these would probably be better suited by C++11 enum
34 // class's with appropriate fixed underlying type.
35 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
37 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
38 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
39 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
40 // Just use 64, since it can hold 32-bit values too.
41 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
42 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
43 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
44 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
45 // Just use 64, since it can hold 32-bit values too.
46 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
47 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
48 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
49 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
51 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
52 // since 64-bit can hold 32-bit values too.
60 llvm::yaml::Hex64 Entry;
66 llvm::yaml::Hex64 Value;
67 llvm::yaml::Hex64 Size;
70 struct LocalGlobalWeakSymbols {
71 std::vector<Symbol> Local;
72 std::vector<Symbol> Global;
73 std::vector<Symbol> Weak;
76 enum class SectionKind { RawContent, Relocation };
81 llvm::yaml::Hex64 Address;
83 llvm::yaml::Hex64 AddressAlign;
84 Section(SectionKind Kind) : Kind(Kind) {}
87 struct RawContentSection : Section {
88 yaml::BinaryRef Content;
89 llvm::yaml::Hex64 Size;
90 RawContentSection() : Section(SectionKind::RawContent) {}
91 static bool classof(const Section *S) {
92 return S->Kind == SectionKind::RawContent;
96 llvm::yaml::Hex64 Offset;
101 struct RelocationSection : Section {
103 std::vector<Relocation> Relocations;
104 RelocationSection() : Section(SectionKind::Relocation) {}
105 static bool classof(const Section *S) {
106 return S->Kind == SectionKind::Relocation;
111 std::vector<std::unique_ptr<Section>> Sections;
112 // Although in reality the symbols reside in a section, it is a lot
113 // cleaner and nicer if we read them from the YAML as a separate
114 // top-level key, which automatically ensures that invariants like there
115 // being a single SHT_SYMTAB section are upheld.
116 LocalGlobalWeakSymbols Symbols;
119 } // end namespace ELFYAML
120 } // end namespace llvm
122 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
123 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
124 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
130 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
131 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
135 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
136 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
140 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
141 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
145 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
146 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
150 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
151 static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
155 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
156 static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
160 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
161 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
165 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
166 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
170 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
171 static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
175 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
176 static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
180 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
181 static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
185 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
186 static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
190 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
191 static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
195 struct MappingTraits<ELFYAML::FileHeader> {
196 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
200 struct MappingTraits<ELFYAML::Symbol> {
201 static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
205 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
206 static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
209 template <> struct MappingTraits<ELFYAML::Relocation> {
210 static void mapping(IO &IO, ELFYAML::Relocation &Rel);
214 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
215 static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
216 static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
220 struct MappingTraits<ELFYAML::Object> {
221 static void mapping(IO &IO, ELFYAML::Object &Object);
224 } // end namespace yaml
225 } // end namespace llvm