LLVM  9.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 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
58 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
59 
60 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
61 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
62 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
63 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
64 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
65 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)
66 
67 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
68 // since 64-bit can hold 32-bit values too.
69 struct FileHeader {
70  ELF_ELFCLASS Class;
71  ELF_ELFDATA Data;
72  ELF_ELFOSABI OSABI;
73  llvm::yaml::Hex8 ABIVersion;
74  ELF_ET Type;
75  ELF_EM Machine;
76  ELF_EF Flags;
77  llvm::yaml::Hex64 Entry;
78 };
79 
80 struct SectionName {
82 };
83 
84 struct ProgramHeader {
85  ELF_PT Type;
86  ELF_PF Flags;
87  llvm::yaml::Hex64 VAddr;
88  llvm::yaml::Hex64 PAddr;
93  std::vector<SectionName> Sections;
94 };
95 
96 struct Symbol {
98  ELF_STT Type;
101  ELF_STB Binding;
102  llvm::yaml::Hex64 Value;
103  llvm::yaml::Hex64 Size;
104  uint8_t Other;
105 };
106 
109 };
110 
111 struct DynamicEntry {
112  ELF_DYNTAG Tag;
113  llvm::yaml::Hex64 Val;
114 };
115 
116 struct Section {
117  enum class SectionKind {
118  Dynamic,
119  Group,
120  RawContent,
121  Relocation,
122  NoBits,
123  Verdef,
124  Verneed,
125  Symver,
127  };
130  ELF_SHT Type;
131  ELF_SHF Flags;
132  llvm::yaml::Hex64 Address;
134  llvm::yaml::Hex64 AddressAlign;
136 
137  Section(SectionKind Kind) : Kind(Kind) {}
138  virtual ~Section();
139 };
140 
142  std::vector<DynamicEntry> Entries;
144 
146 
147  static bool classof(const Section *S) {
148  return S->Kind == SectionKind::Dynamic;
149  }
150 };
151 
154  llvm::yaml::Hex64 Size;
155  llvm::yaml::Hex64 Info;
156 
158 
159  static bool classof(const Section *S) {
160  return S->Kind == SectionKind::RawContent;
161  }
162 };
163 
165  llvm::yaml::Hex64 Size;
166 
168 
169  static bool classof(const Section *S) {
170  return S->Kind == SectionKind::NoBits;
171  }
172 };
173 
174 struct VernauxEntry {
176  uint16_t Flags;
177  uint16_t Other;
179 };
180 
181 struct VerneedEntry {
182  uint16_t Version;
184  std::vector<VernauxEntry> AuxV;
185 };
186 
188  std::vector<VerneedEntry> VerneedV;
189  llvm::yaml::Hex64 Info;
190 
192 
193  static bool classof(const Section *S) {
194  return S->Kind == SectionKind::Verneed;
195  }
196 };
197 
199  std::vector<uint16_t> Entries;
200 
202 
203  static bool classof(const Section *S) {
204  return S->Kind == SectionKind::Symver;
205  }
206 };
207 
208 struct VerdefEntry {
209  uint16_t Version;
210  uint16_t Flags;
211  uint16_t VersionNdx;
213  std::vector<StringRef> VerNames;
214 };
215 
217  std::vector<VerdefEntry> Entries;
218  llvm::yaml::Hex64 Info;
219 
221 
222  static bool classof(const Section *S) {
223  return S->Kind == SectionKind::Verdef;
224  }
225 };
226 
227 struct Group : Section {
228  // Members of a group contain a flag and a list of section indices
229  // that are part of the group.
230  std::vector<SectionOrType> Members;
231  StringRef Signature; /* Info */
232 
234 
235  static bool classof(const Section *S) {
236  return S->Kind == SectionKind::Group;
237  }
238 };
239 
240 struct Relocation {
241  llvm::yaml::Hex64 Offset;
242  int64_t Addend;
243  ELF_REL Type;
245 };
246 
248  std::vector<Relocation> Relocations;
250 
252 
253  static bool classof(const Section *S) {
254  return S->Kind == SectionKind::Relocation;
255  }
256 };
257 
258 // Represents .MIPS.abiflags section
260  llvm::yaml::Hex16 Version;
261  MIPS_ISA ISALevel;
262  llvm::yaml::Hex8 ISARevision;
263  MIPS_AFL_REG GPRSize;
264  MIPS_AFL_REG CPR1Size;
265  MIPS_AFL_REG CPR2Size;
266  MIPS_ABI_FP FpABI;
267  MIPS_AFL_EXT ISAExtension;
268  MIPS_AFL_ASE ASEs;
269  MIPS_AFL_FLAGS1 Flags1;
270  llvm::yaml::Hex32 Flags2;
271 
273 
274  static bool classof(const Section *S) {
275  return S->Kind == SectionKind::MipsABIFlags;
276  }
277 };
278 
279 struct Object {
281  std::vector<ProgramHeader> ProgramHeaders;
282  std::vector<std::unique_ptr<Section>> Sections;
283  // Although in reality the symbols reside in a section, it is a lot
284  // cleaner and nicer if we read them from the YAML as a separate
285  // top-level key, which automatically ensures that invariants like there
286  // being a single SHT_SYMTAB section are upheld.
287  std::vector<Symbol> Symbols;
288  std::vector<Symbol> DynamicSymbols;
289 };
290 
291 } // end namespace ELFYAML
292 } // end namespace llvm
293 
294 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
295 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
296 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
297 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
298 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)
299 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)
300 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)
301 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
302 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
303 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)
304 
305 namespace llvm {
306 namespace yaml {
307 
308 template <>
309 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
310  static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
311 };
312 
313 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
314  static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
315 };
316 
317 template <>
318 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
319  static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
320 };
321 
322 template <>
323 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
324  static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
325 };
326 
327 template <>
328 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
329  static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
330 };
331 
332 template <>
333 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
334  static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
335 };
336 
337 template <>
338 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
339  static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
340 };
341 
342 template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
343  static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
344 };
345 
346 template <>
347 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
348  static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
349 };
350 
351 template <>
352 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
353  static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
354 };
355 
356 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
357  static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
358 };
359 
360 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
361  static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
362 };
363 
364 template <>
365 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
366  static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
367 };
368 
369 template <>
370 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
371  static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
372 };
373 
374 template <>
375 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
376  static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
377 };
378 
379 template <>
380 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
381  static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
382 };
383 
384 template <>
385 struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
386  static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
387 };
388 
389 template <>
390 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
391  static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
392 };
393 
394 template <>
395 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
396  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
397 };
398 
399 template <>
400 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
401  static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
402 };
403 
404 template <>
405 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
406  static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
407 };
408 
409 template <>
410 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
411  static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
412 };
413 
414 template <>
415 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
416  static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
417 };
418 
419 template <>
420 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
421  static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
422 };
423 
424 template <>
425 struct MappingTraits<ELFYAML::FileHeader> {
426  static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
427 };
428 
429 template <> struct MappingTraits<ELFYAML::ProgramHeader> {
430  static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
431 };
432 
433 template <>
434 struct MappingTraits<ELFYAML::Symbol> {
435  static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
436  static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
437 };
438 
439 template <> struct MappingTraits<ELFYAML::DynamicEntry> {
440  static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
441 };
442 
443 template <> struct MappingTraits<ELFYAML::VerdefEntry> {
444  static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
445 };
446 
447 template <> struct MappingTraits<ELFYAML::VerneedEntry> {
448  static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
449 };
450 
451 template <> struct MappingTraits<ELFYAML::VernauxEntry> {
452  static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
453 };
454 
455 template <> struct MappingTraits<ELFYAML::Relocation> {
456  static void mapping(IO &IO, ELFYAML::Relocation &Rel);
457 };
458 
459 template <>
460 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
461  static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
462  static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
463 };
464 
465 template <>
466 struct MappingTraits<ELFYAML::Object> {
467  static void mapping(IO &IO, ELFYAML::Object &Object);
468 };
469 
470 template <> struct MappingTraits<ELFYAML::SectionOrType> {
471  static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
472 };
473 
474 template <> struct MappingTraits<ELFYAML::SectionName> {
475  static void mapping(IO &IO, ELFYAML::SectionName &sectionName);
476 };
477 
478 } // end namespace yaml
479 } // end namespace llvm
480 
481 #endif // LLVM_OBJECTYAML_ELFYAML_H
static bool classof(const Section *S)
Definition: ELFYAML.h:147
static bool classof(const Section *S)
Definition: ELFYAML.h:169
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool classof(const Section *S)
Definition: ELFYAML.h:235
Optional< llvm::yaml::Hex64 > Align
Definition: ELFYAML.h:89
static bool classof(const Section *S)
Definition: ELFYAML.h:222
Optional< ELF_SHN > Index
Definition: ELFYAML.h:100
llvm::yaml::Hex64 PAddr
Definition: ELFYAML.h:88
std::vector< SectionName > Sections
Definition: ELFYAML.h:93
llvm::yaml::Hex16 Version
Definition: ELFYAML.h:260
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:218
llvm::yaml::Hex64 Address
Definition: ELFYAML.h:132
FileHeader Header
Definition: ELFYAML.h:280
llvm::yaml::Hex64 Entry
Definition: ELFYAML.h:77
llvm::yaml::Hex64 AddressAlign
Definition: ELFYAML.h:134
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:165
std::vector< DynamicEntry > Entries
Definition: ELFYAML.h:142
Definition: BitVector.h:937
StringRef Section
Definition: ELFYAML.h:99
static bool classof(const Section *S)
Definition: ELFYAML.h:253
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:103
std::vector< Symbol > Symbols
Definition: ELFYAML.h:287
StringRef Name
Definition: ELFYAML.h:97
llvm::yaml::Hex8 ABIVersion
Definition: ELFYAML.h:73
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:189
Optional< llvm::yaml::Hex64 > EntSize
Definition: ELFYAML.h:135
std::vector< ProgramHeader > ProgramHeaders
Definition: ELFYAML.h:281
std::vector< VernauxEntry > AuxV
Definition: ELFYAML.h:184
llvm::yaml::Hex32 Flags2
Definition: ELFYAML.h:270
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::yaml::Hex64 Info
Definition: ELFYAML.h:155
std::vector< VerneedEntry > VerneedV
Definition: ELFYAML.h:188
std::vector< StringRef > VerNames
Definition: ELFYAML.h:213
StringRef Signature
Definition: ELFYAML.h:231
std::vector< std::unique_ptr< Section > > Sections
Definition: ELFYAML.h:282
Optional< yaml::BinaryRef > Content
Definition: ELFYAML.h:143
llvm::yaml::Hex8 ISARevision
Definition: ELFYAML.h:262
Optional< llvm::yaml::Hex64 > MemSize
Definition: ELFYAML.h:91
ELF_ELFCLASS Class
Definition: ELFYAML.h:70
std::vector< SectionOrType > Members
Definition: ELFYAML.h:230
SectionKind Kind
Definition: ELFYAML.h:128
llvm::yaml::Hex64 Offset
Definition: ELFYAML.h:241
std::vector< Symbol > DynamicSymbols
Definition: ELFYAML.h:288
static bool classof(const Section *S)
Definition: ELFYAML.h:193
static bool classof(const Section *S)
Definition: ELFYAML.h:203
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
static bool classof(const Section *S)
Definition: ELFYAML.h:274
MIPS_AFL_EXT ISAExtension
Definition: ELFYAML.h:267
llvm::yaml::Hex64 Value
Definition: ELFYAML.h:102
Optional< llvm::yaml::Hex64 > FileSize
Definition: ELFYAML.h:90
std::vector< VerdefEntry > Entries
Definition: ELFYAML.h:217
MIPS_AFL_FLAGS1 Flags1
Definition: ELFYAML.h:269
LLVM Value Representation.
Definition: Value.h:72
llvm::yaml::Hex64 Size
Definition: ELFYAML.h:154
static bool classof(const Section *S)
Definition: ELFYAML.h:159
llvm::yaml::Hex64 Val
Definition: ELFYAML.h:113
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::vector< uint16_t > Entries
Definition: ELFYAML.h:199
Section(SectionKind Kind)
Definition: ELFYAML.h:137
ELF_ELFOSABI OSABI
Definition: ELFYAML.h:72
llvm::yaml::Hex64 VAddr
Definition: ELFYAML.h:87
Optional< StringRef > Symbol
Definition: ELFYAML.h:244
std::vector< Relocation > Relocations
Definition: ELFYAML.h:248
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
Optional< llvm::yaml::Hex64 > Offset
Definition: ELFYAML.h:92