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