LLVM  6.0.0svn
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)
42 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
43 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
44 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
45 // Just use 64, since it can hold 32-bit values too.
46 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
50 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
51 // Just use 64, since it can hold 32-bit values too.
52 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
53 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)
54 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
55 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
56 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
57 
58 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
59 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
62 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
64 
65 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
66 // since 64-bit can hold 32-bit values too.
67 struct FileHeader {
68  ELF_ELFCLASS Class;
69  ELF_ELFDATA Data;
70  ELF_ELFOSABI OSABI;
71  ELF_ET Type;
72  ELF_EM Machine;
73  ELF_EF Flags;
74  llvm::yaml::Hex64 Entry;
75 };
76 
77 struct SectionName {
79 };
80 
81 struct ProgramHeader {
82  ELF_PT Type;
83  ELF_PF Flags;
84  llvm::yaml::Hex64 VAddr;
85  llvm::yaml::Hex64 PAddr;
87  std::vector<SectionName> Sections;
88 };
89 
90 struct Symbol {
92  ELF_STT Type;
95  llvm::yaml::Hex64 Value;
96  llvm::yaml::Hex64 Size;
97  uint8_t Other;
98 };
99 
101  std::vector<Symbol> Local;
102  std::vector<Symbol> Global;
103  std::vector<Symbol> Weak;
104 };
105 
108 };
109 
110 struct Section {
111  enum class SectionKind {
112  Group,
113  RawContent,
114  Relocation,
115  NoBits,
117  };
120  ELF_SHT Type;
121  ELF_SHF Flags;
122  llvm::yaml::Hex64 Address;
125  llvm::yaml::Hex64 AddressAlign;
126 
127  Section(SectionKind Kind) : Kind(Kind) {}
128  virtual ~Section();
129 };
132  llvm::yaml::Hex64 Size;
133 
135 
136  static bool classof(const Section *S) {
137  return S->Kind == SectionKind::RawContent;
138  }
139 };
140 
142  llvm::yaml::Hex64 Size;
143 
145 
146  static bool classof(const Section *S) {
147  return S->Kind == SectionKind::NoBits;
148  }
149 };
150 
151 struct Group : Section {
152  // Members of a group contain a flag and a list of section indices
153  // that are part of the group.
154  std::vector<SectionOrType> Members;
155 
157 
158  static bool classof(const Section *S) {
159  return S->Kind == SectionKind::Group;
160  }
161 };
162 
163 struct Relocation {
164  llvm::yaml::Hex64 Offset;
165  int64_t Addend;
166  ELF_REL Type;
168 };
169 
171  std::vector<Relocation> Relocations;
172 
174 
175  static bool classof(const Section *S) {
176  return S->Kind == SectionKind::Relocation;
177  }
178 };
179 
180 // Represents .MIPS.abiflags section
182  llvm::yaml::Hex16 Version;
183  MIPS_ISA ISALevel;
184  llvm::yaml::Hex8 ISARevision;
185  MIPS_AFL_REG GPRSize;
186  MIPS_AFL_REG CPR1Size;
187  MIPS_AFL_REG CPR2Size;
188  MIPS_ABI_FP FpABI;
189  MIPS_AFL_EXT ISAExtension;
190  MIPS_AFL_ASE ASEs;
191  MIPS_AFL_FLAGS1 Flags1;
192  llvm::yaml::Hex32 Flags2;
193 
195 
196  static bool classof(const Section *S) {
197  return S->Kind == SectionKind::MipsABIFlags;
198  }
199 };
200 
201 struct Object {
203  std::vector<ProgramHeader> ProgramHeaders;
204  std::vector<std::unique_ptr<Section>> Sections;
205  // Although in reality the symbols reside in a section, it is a lot
206  // cleaner and nicer if we read them from the YAML as a separate
207  // top-level key, which automatically ensures that invariants like there
208  // being a single SHT_SYMTAB section are upheld.
211 };
212 
213 } // end namespace ELFYAML
214 } // end namespace llvm
215 
217 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
222 
223 namespace llvm {
224 namespace yaml {
225 
226 template <>
227 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
228  static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
229 };
230 
231 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
232  static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
233 };
234 
235 template <>
236 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
237  static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
238 };
239 
240 template <>
241 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
242  static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
243 };
244 
245 template <>
246 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
247  static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
248 };
249 
250 template <>
251 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
252  static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
253 };
254 
255 template <>
256 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
257  static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
258 };
259 
260 template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
261  static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
262 };
263 
264 template <>
265 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
266  static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
267 };
268 
269 template <>
270 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
271  static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
272 };
273 
274 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
275  static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
276 };
277 
278 template <>
279 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
280  static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
281 };
282 
283 template <>
284 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
285  static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
286 };
287 
288 template <>
289 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
290  static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
291 };
292 
293 template <>
294 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
295  static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
296 };
297 
298 template <>
299 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
300  static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
301 };
302 
303 template <>
304 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
305  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
306 };
307 
308 template <>
309 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
310  static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
311 };
312 
313 template <>
314 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
315  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
316 };
317 
318 template <>
319 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
320  static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
321 };
322 
323 template <>
324 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
325  static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
326 };
327 
328 template <>
329 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
330  static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
331 };
332 
333 template <>
334 struct MappingTraits<ELFYAML::FileHeader> {
335  static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
336 };
337 
338 template <> struct MappingTraits<ELFYAML::ProgramHeader> {
339  static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
340 };
341 
342 template <>
343 struct MappingTraits<ELFYAML::Symbol> {
344  static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
345  static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
346 };
347 
348 template <>
349 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
350  static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
351 };
352 
353 template <> struct MappingTraits<ELFYAML::Relocation> {
354  static void mapping(IO &IO, ELFYAML::Relocation &Rel);
355 };
356 
357 template <>
358 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
359  static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
360  static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
361 };
362 
363 template <>
364 struct MappingTraits<ELFYAML::Object> {
365  static void mapping(IO &IO, ELFYAML::Object &Object);
366 };
367 
368 template <> struct MappingTraits<ELFYAML::SectionOrType> {
369  static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
370 };
371 
372 template <> struct MappingTraits<ELFYAML::SectionName> {
373  static void mapping(IO &IO, ELFYAML::SectionName &sectionName);
374 };
375 
376 } // end namespace yaml
377 } // end namespace llvm
378 
379 #endif // LLVM_OBJECTYAML_ELFYAML_H
static bool classof(const Section *S)
Definition: ELFYAML.h:146
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static bool classof(const Section *S)
Definition: ELFYAML.h:158
Optional< llvm::yaml::Hex64 > Align
Definition: ELFYAML.h:86
Optional< ELF_SHN > Index
Definition: ELFYAML.h:94
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:85
std::vector< SectionName > Sections
Definition: ELFYAML.h:87
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:182
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:122
#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:202
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:74
LocalGlobalWeakSymbols Symbols
Definition: ELFYAML.h:209
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:125
std::vector< Symbol > Weak
Definition: ELFYAML.h:103
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:142
Definition: BitVector.h:920
StringRef Section
Definition: ELFYAML.h:93
static bool classof(const Section *S)
Definition: ELFYAML.h:175
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:96
StringRef Name
Definition: ELFYAML.h:91
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:102
LocalGlobalWeakSymbols DynamicSymbols
Definition: ELFYAML.h:210
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:203
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:192
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:204
std::vector< Symbol > Local
Definition: ELFYAML.h:101
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:184
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:68
std::vector< SectionOrType > Members
Definition: ELFYAML.h:154
SectionKind Kind
Definition: ELFYAML.h:118
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:164
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:64
static bool classof(const Section *S)
Definition: ELFYAML.h:196
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:189
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:95
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:191
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:132
static bool classof(const Section *S)
Definition: ELFYAML.h:136
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Section(SectionKind Kind)
Definition: ELFYAML.h:127
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:70
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:84
Optional< StringRef > Symbol
Definition: ELFYAML.h:167
std::vector< Relocation > Relocations
Definition: ELFYAML.h:171