This reverts commit r241350 and r241352.
[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/MC/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(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)
50
51 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
52 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
53 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
54 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
55 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
56 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)
57
58 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
59 // since 64-bit can hold 32-bit values too.
60 struct FileHeader {
61   ELF_ELFCLASS Class;
62   ELF_ELFDATA Data;
63   ELF_ELFOSABI OSABI;
64   ELF_ET Type;
65   ELF_EM Machine;
66   ELF_EF Flags;
67   llvm::yaml::Hex64 Entry;
68 };
69 struct Symbol {
70   StringRef Name;
71   ELF_STT Type;
72   StringRef Section;
73   llvm::yaml::Hex64 Value;
74   llvm::yaml::Hex64 Size;
75   uint8_t Other;
76 };
77 struct LocalGlobalWeakSymbols {
78   std::vector<Symbol> Local;
79   std::vector<Symbol> Global;
80   std::vector<Symbol> Weak;
81 };
82
83 struct SectionOrType {
84   StringRef sectionNameOrType;
85 };
86
87 struct Section {
88   enum class SectionKind { Group, RawContent, Relocation, MipsABIFlags };
89   SectionKind Kind;
90   StringRef Name;
91   ELF_SHT Type;
92   ELF_SHF Flags;
93   llvm::yaml::Hex64 Address;
94   StringRef Link;
95   StringRef Info;
96   llvm::yaml::Hex64 AddressAlign;
97   Section(SectionKind Kind) : Kind(Kind) {}
98   virtual ~Section();
99 };
100 struct RawContentSection : Section {
101   yaml::BinaryRef Content;
102   llvm::yaml::Hex64 Size;
103   RawContentSection() : Section(SectionKind::RawContent) {}
104   static bool classof(const Section *S) {
105     return S->Kind == SectionKind::RawContent;
106   }
107 };
108
109 struct Group : Section {
110   // Members of a group contain a flag and a list of section indices
111   // that are part of the group.
112   std::vector<SectionOrType> Members;
113   Group() : Section(SectionKind::Group) {}
114   static bool classof(const Section *S) {
115     return S->Kind == SectionKind::Group;
116   }
117 };
118
119 struct Relocation {
120   llvm::yaml::Hex64 Offset;
121   int64_t Addend;
122   ELF_REL Type;
123   StringRef Symbol;
124 };
125 struct RelocationSection : Section {
126   std::vector<Relocation> Relocations;
127   RelocationSection() : Section(SectionKind::Relocation) {}
128   static bool classof(const Section *S) {
129     return S->Kind == SectionKind::Relocation;
130   }
131 };
132
133 // Represents .MIPS.abiflags section
134 struct MipsABIFlags : Section {
135   llvm::yaml::Hex16 Version;
136   MIPS_ISA ISALevel;
137   llvm::yaml::Hex8 ISARevision;
138   MIPS_AFL_REG GPRSize;
139   MIPS_AFL_REG CPR1Size;
140   MIPS_AFL_REG CPR2Size;
141   MIPS_ABI_FP FpABI;
142   MIPS_AFL_EXT ISAExtension;
143   MIPS_AFL_ASE ASEs;
144   MIPS_AFL_FLAGS1 Flags1;
145   llvm::yaml::Hex32 Flags2;
146   MipsABIFlags() : Section(SectionKind::MipsABIFlags) {}
147   static bool classof(const Section *S) {
148     return S->Kind == SectionKind::MipsABIFlags;
149   }
150 };
151
152 struct Object {
153   FileHeader Header;
154   std::vector<std::unique_ptr<Section>> Sections;
155   // Although in reality the symbols reside in a section, it is a lot
156   // cleaner and nicer if we read them from the YAML as a separate
157   // top-level key, which automatically ensures that invariants like there
158   // being a single SHT_SYMTAB section are upheld.
159   LocalGlobalWeakSymbols Symbols;
160 };
161
162 } // end namespace ELFYAML
163 } // end namespace llvm
164
165 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
166 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
167 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
168 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
169
170 namespace llvm {
171 namespace yaml {
172
173 template <>
174 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
175   static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
176 };
177
178 template <>
179 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
180   static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
181 };
182
183 template <>
184 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
185   static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
186 };
187
188 template <>
189 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
190   static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
191 };
192
193 template <>
194 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
195   static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
196 };
197
198 template <>
199 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
200   static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
201 };
202
203 template <>
204 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
205   static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
206 };
207
208 template <>
209 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
210   static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
211 };
212
213 template <>
214 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
215   static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
216 };
217
218 template <>
219 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
220   static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
221 };
222
223 template <>
224 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
225   static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
226 };
227
228 template <>
229 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
230   static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
231 };
232
233 template <>
234 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
235   static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
236 };
237
238 template <>
239 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
240   static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
241 };
242
243 template <>
244 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
245   static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
246 };
247
248 template <>
249 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
250   static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
251 };
252
253 template <>
254 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
255   static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
256 };
257
258 template <>
259 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
260   static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
261 };
262
263 template <>
264 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
265   static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
266 };
267
268 template <>
269 struct MappingTraits<ELFYAML::FileHeader> {
270   static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
271 };
272
273 template <>
274 struct MappingTraits<ELFYAML::Symbol> {
275   static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
276 };
277
278 template <>
279 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
280   static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
281 };
282
283 template <> struct MappingTraits<ELFYAML::Relocation> {
284   static void mapping(IO &IO, ELFYAML::Relocation &Rel);
285 };
286
287 template <>
288 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
289   static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
290   static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
291 };
292
293 template <>
294 struct MappingTraits<ELFYAML::Object> {
295   static void mapping(IO &IO, ELFYAML::Object &Object);
296 };
297
298 template <> struct MappingTraits<ELFYAML::SectionOrType> {
299   static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
300 };
301
302 } // end namespace yaml
303 } // end namespace llvm
304
305 #endif