[yaml2obj][ELF] Add an optional `Size` field to the YAML section declaration.
[oota-llvm.git] / include / llvm / Object / ELFYAML.h
1 //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file declares classes for handling the YAML representation
12 /// of ELF.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_OBJECT_ELFYAML_H
17 #define LLVM_OBJECT_ELFYAML_H
18
19 #include "llvm/Object/YAML.h"
20 #include "llvm/Support/ELF.h"
21
22 namespace llvm {
23 namespace ELFYAML {
24
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(uint8_t, ELF_REL)
44 // Just use 64, since it can hold 32-bit values too.
45 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
46 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
47
48 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
49 // since 64-bit can hold 32-bit values too.
50 struct FileHeader {
51   ELF_ELFCLASS Class;
52   ELF_ELFDATA Data;
53   ELF_ELFOSABI OSABI;
54   ELF_ET Type;
55   ELF_EM Machine;
56   ELF_EF Flags;
57   llvm::yaml::Hex64 Entry;
58 };
59 struct Symbol {
60   StringRef Name;
61   ELF_STT Type;
62   StringRef Section;
63   llvm::yaml::Hex64 Value;
64   llvm::yaml::Hex64 Size;
65 };
66 struct LocalGlobalWeakSymbols {
67   std::vector<Symbol> Local;
68   std::vector<Symbol> Global;
69   std::vector<Symbol> Weak;
70 };
71 struct Section {
72   enum class SectionKind { RawContent, Relocation };
73   SectionKind Kind;
74   StringRef Name;
75   ELF_SHT Type;
76   ELF_SHF Flags;
77   llvm::yaml::Hex64 Address;
78   StringRef Link;
79   StringRef Info;
80   llvm::yaml::Hex64 AddressAlign;
81   Section(SectionKind Kind) : Kind(Kind) {}
82   virtual ~Section();
83 };
84 struct RawContentSection : Section {
85   object::yaml::BinaryRef Content;
86   llvm::yaml::Hex64 Size;
87   RawContentSection() : Section(SectionKind::RawContent) {}
88   static bool classof(const Section *S) {
89     return S->Kind == SectionKind::RawContent;
90   }
91 };
92 struct Relocation {
93   llvm::yaml::Hex64 Offset;
94   int64_t Addend;
95   ELF_REL Type;
96   StringRef Symbol;
97 };
98 struct RelocationSection : Section {
99   std::vector<Relocation> Relocations;
100   RelocationSection() : Section(SectionKind::Relocation) {}
101   static bool classof(const Section *S) {
102     return S->Kind == SectionKind::Relocation;
103   }
104 };
105 struct Object {
106   FileHeader Header;
107   std::vector<std::unique_ptr<Section>> Sections;
108   // Although in reality the symbols reside in a section, it is a lot
109   // cleaner and nicer if we read them from the YAML as a separate
110   // top-level key, which automatically ensures that invariants like there
111   // being a single SHT_SYMTAB section are upheld.
112   LocalGlobalWeakSymbols Symbols;
113 };
114
115 } // end namespace ELFYAML
116 } // end namespace llvm
117
118 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
119 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
120 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
121
122 namespace llvm {
123 namespace yaml {
124
125 template <>
126 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
127   static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
128 };
129
130 template <>
131 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
132   static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
133 };
134
135 template <>
136 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
137   static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
138 };
139
140 template <>
141 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
142   static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
143 };
144
145 template <>
146 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
147   static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
148 };
149
150 template <>
151 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
152   static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
153 };
154
155 template <>
156 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
157   static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
158 };
159
160 template <>
161 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
162   static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
163 };
164
165 template <>
166 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
167   static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
168 };
169
170 template <>
171 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
172   static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
173 };
174
175 template <>
176 struct MappingTraits<ELFYAML::FileHeader> {
177   static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
178 };
179
180 template <>
181 struct MappingTraits<ELFYAML::Symbol> {
182   static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
183 };
184
185 template <>
186 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
187   static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
188 };
189
190 template <> struct MappingTraits<ELFYAML::Relocation> {
191   static void mapping(IO &IO, ELFYAML::Relocation &Rel);
192 };
193
194 template <>
195 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
196   static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
197   static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
198 };
199
200 template <>
201 struct MappingTraits<ELFYAML::Object> {
202   static void mapping(IO &IO, ELFYAML::Object &Object);
203 };
204
205 } // end namespace yaml
206 } // end namespace llvm
207
208 #endif