LLVM  10.0.0svn
ELFYAML.h
Go to the documentation of this file.
1 //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file declares classes for handling the YAML representation
11 /// of ELF.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_OBJECTYAML_ELFYAML_H
16 #define LLVM_OBJECTYAML_ELFYAML_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ObjectYAML/YAML.h"
21 #include <cstdint>
22 #include <memory>
23 #include <vector>
24 
25 namespace llvm {
26 namespace ELFYAML {
27 
28 // These types are invariant across 32/64-bit ELF, so for simplicity just
29 // directly give them their exact sizes. We don't need to worry about
30 // endianness because these are just the types in the YAMLIO structures,
31 // and are appropriately converted to the necessary endianness when
32 // reading/generating binary object files.
33 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
34 // the common prefix of the respective constants. E.g. ELF_EM corresponds
35 // to the `e_machine` constants, like `EM_X86_64`.
36 // In the future, these would probably be better suited by C++11 enum
37 // class's with appropriate fixed underlying type.
38 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
39 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)
40 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
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)
46 // Just use 64, since it can hold 32-bit values too.
47 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)
48 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)
49 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
50 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
51 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
52 // Just use 64, since it can hold 32-bit values too.
53 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
54 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)
55 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STB)
56 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
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 
81 };
82 
83 struct SectionName {
85 };
86 
87 struct ProgramHeader {
88  ELF_PT Type;
89  ELF_PF Flags;
90  llvm::yaml::Hex64 VAddr;
91  llvm::yaml::Hex64 PAddr;
96  std::vector<SectionName> Sections;
97 };
98 
99 struct Symbol {
102  ELF_STT Type;
105  ELF_STB Binding;
106  llvm::yaml::Hex64 Value;
107  llvm::yaml::Hex64 Size;
109 };
110 
113 };
114 
115 struct DynamicEntry {
116  ELF_DYNTAG Tag;
117  llvm::yaml::Hex64 Val;
118 };
119 
120 struct Section {
121  enum class SectionKind {
122  Dynamic,
123  Group,
124  RawContent,
125  Relocation,
126  NoBits,
127  Verdef,
128  Verneed,
130  Symver,
132  };
135  ELF_SHT Type;
137  llvm::yaml::Hex64 Address;
139  llvm::yaml::Hex64 AddressAlign;
141 
142  // Usually sections are not created implicitly, but loaded from YAML.
143  // When they are, this flag is used to signal about that.
145 
146  Section(SectionKind Kind, bool IsImplicit = false)
147  : Kind(Kind), IsImplicit(IsImplicit) {}
148  virtual ~Section();
149 
150  // The following members are used to override section fields which is
151  // useful for creating invalid objects.
152 
153  // This can be used to override the offset stored in the sh_name field.
154  // It does not affect the name stored in the string table.
156 
157  // This can be used to override the sh_offset field. It does not place the
158  // section data at the offset specified.
160 
161  // This can be used to override the sh_size field. It does not affect the
162  // content written.
164 };
165 
167  std::vector<DynamicEntry> Entries;
169 
171 
172  static bool classof(const Section *S) {
173  return S->Kind == SectionKind::Dynamic;
174  }
175 };
176 
181 
183 
184  static bool classof(const Section *S) {
185  return S->Kind == SectionKind::RawContent;
186  }
187 };
188 
190  llvm::yaml::Hex64 Size;
191 
193 
194  static bool classof(const Section *S) {
195  return S->Kind == SectionKind::NoBits;
196  }
197 };
198 
199 struct VernauxEntry {
201  uint16_t Flags;
202  uint16_t Other;
204 };
205 
206 struct VerneedEntry {
207  uint16_t Version;
209  std::vector<VernauxEntry> AuxV;
210 };
211 
213  std::vector<VerneedEntry> VerneedV;
214  llvm::yaml::Hex64 Info;
215 
217 
218  static bool classof(const Section *S) {
219  return S->Kind == SectionKind::Verneed;
220  }
221 };
222 
224  std::vector<uint16_t> Entries;
225 
227 
228  static bool classof(const Section *S) {
229  return S->Kind == SectionKind::Symver;
230  }
231 };
232 
233 struct VerdefEntry {
234  uint16_t Version;
235  uint16_t Flags;
236  uint16_t VersionNdx;
238  std::vector<StringRef> VerNames;
239 };
240 
242  std::vector<VerdefEntry> Entries;
243  llvm::yaml::Hex64 Info;
244 
246 
247  static bool classof(const Section *S) {
248  return S->Kind == SectionKind::Verdef;
249  }
250 };
251 
252 struct Group : Section {
253  // Members of a group contain a flag and a list of section indices
254  // that are part of the group.
255  std::vector<SectionOrType> Members;
256  StringRef Signature; /* Info */
257 
259 
260  static bool classof(const Section *S) {
261  return S->Kind == SectionKind::Group;
262  }
263 };
264 
265 struct Relocation {
266  llvm::yaml::Hex64 Offset;
267  int64_t Addend;
268  ELF_REL Type;
270 };
271 
273  std::vector<Relocation> Relocations;
275 
277 
278  static bool classof(const Section *S) {
279  return S->Kind == SectionKind::Relocation;
280  }
281 };
282 
284  std::vector<uint32_t> Entries;
285 
287 
288  static bool classof(const Section *S) {
289  return S->Kind == SectionKind::SymtabShndxSection;
290  }
291 };
292 
293 // Represents .MIPS.abiflags section
295  llvm::yaml::Hex16 Version;
296  MIPS_ISA ISALevel;
297  llvm::yaml::Hex8 ISARevision;
298  MIPS_AFL_REG GPRSize;
299  MIPS_AFL_REG CPR1Size;
300  MIPS_AFL_REG CPR2Size;
301  MIPS_ABI_FP FpABI;
302  MIPS_AFL_EXT ISAExtension;
303  MIPS_AFL_ASE ASEs;
304  MIPS_AFL_FLAGS1 Flags1;
305  llvm::yaml::Hex32 Flags2;
306 
308 
309  static bool classof(const Section *S) {
310  return S->Kind == SectionKind::MipsABIFlags;
311  }
312 };
313 
314 struct Object {
316  std::vector<ProgramHeader> ProgramHeaders;
317  std::vector<std::unique_ptr<Section>> Sections;
318  // Although in reality the symbols reside in a section, it is a lot
319  // cleaner and nicer if we read them from the YAML as a separate
320  // top-level key, which automatically ensures that invariants like there
321  // being a single SHT_SYMTAB section are upheld.
322  std::vector<Symbol> Symbols;
323  std::vector<Symbol> DynamicSymbols;
324 };
325 
326 } // end namespace ELFYAML
327 } // end namespace llvm
328 
329 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
330 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
331 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
332 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
333 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)
334 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)
335 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)
336 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
337 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
338 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)
339 
340 namespace llvm {
341 namespace yaml {
342 
343 template <>
344 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
345  static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
346 };
347 
348 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
349  static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
350 };
351 
352 template <>
353 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
354  static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
355 };
356 
357 template <>
358 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
359  static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
360 };
361 
362 template <>
363 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
364  static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
365 };
366 
367 template <>
368 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
369  static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
370 };
371 
372 template <>
373 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
374  static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
375 };
376 
377 template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
378  static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
379 };
380 
381 template <>
382 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
383  static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
384 };
385 
386 template <>
387 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
388  static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
389 };
390 
391 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
392  static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
393 };
394 
395 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
396  static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
397 };
398 
399 template <>
400 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
401  static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
402 };
403 
404 template <>
405 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
406  static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
407 };
408 
409 template <>
410 struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
411  static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
412 };
413 
414 template <>
415 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
416  static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
417 };
418 
419 template <>
420 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
421  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
422 };
423 
424 template <>
425 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
426  static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
427 };
428 
429 template <>
430 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
431  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
432 };
433 
434 template <>
435 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
436  static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
437 };
438 
439 template <>
440 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
441  static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
442 };
443 
444 template <>
445 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
446  static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
447 };
448 
449 template <>
450 struct MappingTraits<ELFYAML::FileHeader> {
451  static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
452 };
453 
454 template <> struct MappingTraits<ELFYAML::ProgramHeader> {
455  static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
456 };
457 
458 template <>
459 struct MappingTraits<ELFYAML::Symbol> {
460  static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
461  static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
462 };
463 
464 template <> struct MappingTraits<ELFYAML::DynamicEntry> {
465  static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
466 };
467 
468 template <> struct MappingTraits<ELFYAML::VerdefEntry> {
469  static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
470 };
471 
472 template <> struct MappingTraits<ELFYAML::VerneedEntry> {
473  static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
474 };
475 
476 template <> struct MappingTraits<ELFYAML::VernauxEntry> {
477  static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
478 };
479 
480 template <> struct MappingTraits<ELFYAML::Relocation> {
481  static void mapping(IO &IO, ELFYAML::Relocation &Rel);
482 };
483 
484 template <>
485 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
486  static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
487  static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
488 };
489 
490 template <>
491 struct MappingTraits<ELFYAML::Object> {
492  static void mapping(IO &IO, ELFYAML::Object &Object);
493 };
494 
495 template <> struct MappingTraits<ELFYAML::SectionOrType> {
496  static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
497 };
498 
499 template <> struct MappingTraits<ELFYAML::SectionName> {
500  static void mapping(IO &IO, ELFYAML::SectionName &sectionName);
501 };
502 
503 } // end namespace yaml
504 } // end namespace llvm
505 
506 #endif // LLVM_OBJECTYAML_ELFYAML_H
static bool classof(const Section *S)
Definition: ELFYAML.h:172
static bool classof(const Section *S)
Definition: ELFYAML.h:194
Optional< ELF_SHF > Flags
Definition: ELFYAML.h:136
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool classof(const Section *S)
Definition: ELFYAML.h:288
static bool classof(const Section *S)
Definition: ELFYAML.h:260
Optional< llvm::yaml::Hex64 > Align
Definition: ELFYAML.h:92
static bool classof(const Section *S)
Definition: ELFYAML.h:247
Optional< ELF_SHN > Index
Definition: ELFYAML.h:104
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:91
std::vector< SectionName > Sections
Definition: ELFYAML.h:96
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:295
Optional< llvm::yaml::Hex64 > Size
Definition: ELFYAML.h:179
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:243
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:137
FileHeader Header
Definition: ELFYAML.h:315
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:75
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:139
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:190
std::vector< DynamicEntry > Entries
Definition: ELFYAML.h:167
Definition: BitVector.h:937
StringRef Section
Definition: ELFYAML.h:103
static bool classof(const Section *S)
Definition: ELFYAML.h:278
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:107
std::vector< Symbol > Symbols
Definition: ELFYAML.h:322
llvm::yaml::Hex8 ABIVersion
Definition: ELFYAML.h:71
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:214
Optional< llvm::yaml::Hex64 > EntSize
Definition: ELFYAML.h:140
Optional< uint8_t > Other
Definition: ELFYAML.h:108
Optional< llvm::yaml::Hex64 > ShSize
Definition: ELFYAML.h:163
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:316
std::vector< VernauxEntry > AuxV
Definition: ELFYAML.h:209
Optional< llvm::yaml::Hex16 > SHStrNdx
Definition: ELFYAML.h:80
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:305
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::vector< uint32_t > Entries
Definition: ELFYAML.h:284
Optional< llvm::yaml::Hex64 > SHOff
Definition: ELFYAML.h:78
std::vector< VerneedEntry > VerneedV
Definition: ELFYAML.h:213
std::vector< StringRef > VerNames
Definition: ELFYAML.h:238
StringRef Signature
Definition: ELFYAML.h:256
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:317
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:178
Section(SectionKind Kind, bool IsImplicit=false)
Definition: ELFYAML.h:146
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:168
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:297
Optional< llvm::yaml::Hex64 > MemSize
Definition: ELFYAML.h:94
ELF_ELFCLASS Class
Definition: ELFYAML.h:68
std::vector< SectionOrType > Members
Definition: ELFYAML.h:255
Optional< llvm::yaml::Hex64 > ShOffset
Definition: ELFYAML.h:159
SectionKind Kind
Definition: ELFYAML.h:133
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:266
std::vector< Symbol > DynamicSymbols
Definition: ELFYAML.h:323
static bool classof(const Section *S)
Definition: ELFYAML.h:218
Optional< llvm::yaml::Hex16 > SHNum
Definition: ELFYAML.h:79
Optional< llvm::yaml::Hex64 > Info
Definition: ELFYAML.h:180
static bool classof(const Section *S)
Definition: ELFYAML.h:228
Optional< llvm::yaml::Hex64 > ShName
Definition: ELFYAML.h:155
static bool classof(const Section *S)
Definition: ELFYAML.h:309
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:302
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:106
Optional< llvm::yaml::Hex64 > FileSize
Definition: ELFYAML.h:93
std::vector< VerdefEntry > Entries
Definition: ELFYAML.h:242
Optional< llvm::yaml::Hex16 > SHEntSize
Definition: ELFYAML.h:77
Optional< uint32_t > NameIndex
Definition: ELFYAML.h:101
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:304
LLVM Value Representation.
Definition: Value.h:73
static bool classof(const Section *S)
Definition: ELFYAML.h:184
llvm::yaml::Hex64 Val
Definition: ELFYAML.h:117
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::vector< uint16_t > Entries
Definition: ELFYAML.h:224
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:70
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:90
Optional< StringRef > Symbol
Definition: ELFYAML.h:269
std::vector< Relocation > Relocations
Definition: ELFYAML.h:273
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
Optional< llvm::yaml::Hex64 > Offset
Definition: ELFYAML.h:95