LLVM  5.0.0
ELFYAML.h
Go to the documentation of this file.
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_OBJECTYAML_ELFYAML_H
17 #define LLVM_OBJECTYAML_ELFYAML_H
18 
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ObjectYAML/YAML.h"
22 #include <cstdint>
23 #include <memory>
24 #include <vector>
25 
26 namespace llvm {
27 namespace ELFYAML {
28 
29 // These types are invariant across 32/64-bit ELF, so for simplicity just
30 // directly give them their exact sizes. We don't need to worry about
31 // endianness because these are just the types in the YAMLIO structures,
32 // and are appropriately converted to the necessary endianness when
33 // reading/generating binary object files.
34 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
35 // the common prefix of the respective constants. E.g. ELF_EM corresponds
36 // to the `e_machine` constants, like `EM_X86_64`.
37 // In the future, these would probably be better suited by C++11 enum
38 // class's with appropriate fixed underlying type.
39 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
41 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
42 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
43 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
44 // Just use 64, since it can hold 32-bit values too.
45 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
48 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
49 // Just use 64, since it can hold 32-bit values too.
50 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
51 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
52 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
53 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
54 
55 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
56 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
59 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
61 
62 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
63 // since 64-bit can hold 32-bit values too.
64 struct FileHeader {
65  ELF_ELFCLASS Class;
66  ELF_ELFDATA Data;
67  ELF_ELFOSABI OSABI;
68  ELF_ET Type;
69  ELF_EM Machine;
70  ELF_EF Flags;
71  llvm::yaml::Hex64 Entry;
72 };
73 
74 struct Symbol {
76  ELF_STT Type;
78  llvm::yaml::Hex64 Value;
79  llvm::yaml::Hex64 Size;
80  uint8_t Other;
81 };
82 
84  std::vector<Symbol> Local;
85  std::vector<Symbol> Global;
86  std::vector<Symbol> Weak;
87 };
88 
89 struct SectionOrType {
91 };
92 
93 struct Section {
94  enum class SectionKind {
95  Group,
96  RawContent,
97  Relocation,
98  NoBits,
100  };
103  ELF_SHT Type;
104  ELF_SHF Flags;
105  llvm::yaml::Hex64 Address;
108  llvm::yaml::Hex64 AddressAlign;
109 
110  Section(SectionKind Kind) : Kind(Kind) {}
111  virtual ~Section();
112 };
115  llvm::yaml::Hex64 Size;
116 
118 
119  static bool classof(const Section *S) {
120  return S->Kind == SectionKind::RawContent;
121  }
122 };
123 
125  llvm::yaml::Hex64 Size;
126 
128 
129  static bool classof(const Section *S) {
130  return S->Kind == SectionKind::NoBits;
131  }
132 };
133 
134 struct Group : Section {
135  // Members of a group contain a flag and a list of section indices
136  // that are part of the group.
137  std::vector<SectionOrType> Members;
138 
140 
141  static bool classof(const Section *S) {
142  return S->Kind == SectionKind::Group;
143  }
144 };
145 
146 struct Relocation {
147  llvm::yaml::Hex64 Offset;
148  int64_t Addend;
149  ELF_REL Type;
151 };
152 
154  std::vector<Relocation> Relocations;
155 
157 
158  static bool classof(const Section *S) {
159  return S->Kind == SectionKind::Relocation;
160  }
161 };
162 
163 // Represents .MIPS.abiflags section
165  llvm::yaml::Hex16 Version;
166  MIPS_ISA ISALevel;
167  llvm::yaml::Hex8 ISARevision;
168  MIPS_AFL_REG GPRSize;
169  MIPS_AFL_REG CPR1Size;
170  MIPS_AFL_REG CPR2Size;
171  MIPS_ABI_FP FpABI;
172  MIPS_AFL_EXT ISAExtension;
173  MIPS_AFL_ASE ASEs;
174  MIPS_AFL_FLAGS1 Flags1;
175  llvm::yaml::Hex32 Flags2;
176 
178 
179  static bool classof(const Section *S) {
180  return S->Kind == SectionKind::MipsABIFlags;
181  }
182 };
183 
184 struct Object {
186  std::vector<std::unique_ptr<Section>> Sections;
187  // Although in reality the symbols reside in a section, it is a lot
188  // cleaner and nicer if we read them from the YAML as a separate
189  // top-level key, which automatically ensures that invariants like there
190  // being a single SHT_SYMTAB section are upheld.
192 };
193 
194 } // end namespace ELFYAML
195 } // end namespace llvm
196 
197 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
198 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
199 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
200 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
201 
202 namespace llvm {
203 namespace yaml {
204 
205 template <>
206 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
207  static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
208 };
209 
210 template <>
211 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
212  static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
213 };
214 
215 template <>
216 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
217  static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
218 };
219 
220 template <>
221 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
222  static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
223 };
224 
225 template <>
226 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
227  static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
228 };
229 
230 template <>
231 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
232  static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
233 };
234 
235 template <>
236 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
237  static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
238 };
239 
240 template <>
241 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
242  static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
243 };
244 
245 template <>
246 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
247  static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
248 };
249 
250 template <>
251 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
252  static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
253 };
254 
255 template <>
256 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
257  static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
258 };
259 
260 template <>
261 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
262  static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
263 };
264 
265 template <>
266 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
267  static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
268 };
269 
270 template <>
271 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
272  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
273 };
274 
275 template <>
276 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
277  static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
278 };
279 
280 template <>
281 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
282  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
283 };
284 
285 template <>
286 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
287  static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
288 };
289 
290 template <>
291 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
292  static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
293 };
294 
295 template <>
296 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
297  static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
298 };
299 
300 template <>
301 struct MappingTraits<ELFYAML::FileHeader> {
302  static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
303 };
304 
305 template <>
306 struct MappingTraits<ELFYAML::Symbol> {
307  static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
308 };
309 
310 template <>
311 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
312  static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
313 };
314 
315 template <> struct MappingTraits<ELFYAML::Relocation> {
316  static void mapping(IO &IO, ELFYAML::Relocation &Rel);
317 };
318 
319 template <>
320 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
321  static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
322  static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
323 };
324 
325 template <>
326 struct MappingTraits<ELFYAML::Object> {
327  static void mapping(IO &IO, ELFYAML::Object &Object);
328 };
329 
330 template <> struct MappingTraits<ELFYAML::SectionOrType> {
331  static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
332 };
333 
334 } // end namespace yaml
335 } // end namespace llvm
336 
337 #endif // LLVM_OBJECTYAML_ELFYAML_H
static bool classof(const Section *S)
Definition: ELFYAML.h:129
static bool classof(const Section *S)
Definition: ELFYAML.h:141
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:165
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:105
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence...
Definition: YAMLTraits.h:1643
FileHeader Header
Definition: ELFYAML.h:185
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:71
LocalGlobalWeakSymbols Symbols
Definition: ELFYAML.h:191
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:108
std::vector< Symbol > Weak
Definition: ELFYAML.h:86
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:125
StringRef Section
Definition: ELFYAML.h:77
static bool classof(const Section *S)
Definition: ELFYAML.h:158
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:79
StringRef Name
Definition: ELFYAML.h:75
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
std::vector< Symbol > Global
Definition: ELFYAML.h:85
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:175
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:186
std::vector< Symbol > Local
Definition: ELFYAML.h:84
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:167
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition: YAMLTraits.h:98
#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)
YAML I/O does conversion based on types.
Definition: YAMLTraits.h:1346
ELF_ELFCLASS Class
Definition: ELFYAML.h:65
std::vector< SectionOrType > Members
Definition: ELFYAML.h:137
SectionKind Kind
Definition: ELFYAML.h:101
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:147
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:64
static bool classof(const Section *S)
Definition: ELFYAML.h:179
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:172
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:78
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:174
This class should be specialized by any integer type that is a union of bit values and the YAML repre...
Definition: YAMLTraits.h:115
LLVM Value Representation.
Definition: Value.h:73
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:115
static bool classof(const Section *S)
Definition: ELFYAML.h:119
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Section(SectionKind Kind)
Definition: ELFYAML.h:110
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:67
std::vector< Relocation > Relocations
Definition: ELFYAML.h:154