1 //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file declares classes for handling the YAML representation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECTYAML_ELFYAML_H
16 #define LLVM_OBJECTYAML_ELFYAML_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ObjectYAML/YAML.h"
20 #include "llvm/Support/YAMLTraits.h"
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)
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)
67 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
68 // since 64-bit can hold 32-bit values too.
73 llvm::yaml::Hex8 ABIVersion;
77 llvm::yaml::Hex64 Entry;
79 Optional<llvm::yaml::Hex16> SHEntSize;
80 Optional<llvm::yaml::Hex16> SHOffset;
81 Optional<llvm::yaml::Hex16> SHNum;
82 Optional<llvm::yaml::Hex16> SHStrNdx;
89 struct ProgramHeader {
92 llvm::yaml::Hex64 VAddr;
93 llvm::yaml::Hex64 PAddr;
94 Optional<llvm::yaml::Hex64> Align;
95 Optional<llvm::yaml::Hex64> FileSize;
96 Optional<llvm::yaml::Hex64> MemSize;
97 Optional<llvm::yaml::Hex64> Offset;
98 std::vector<SectionName> Sections;
103 Optional<uint32_t> NameIndex;
106 Optional<ELF_SHN> Index;
108 llvm::yaml::Hex64 Value;
109 llvm::yaml::Hex64 Size;
113 struct SectionOrType {
114 StringRef sectionNameOrType;
117 struct DynamicEntry {
119 llvm::yaml::Hex64 Val;
123 enum class SectionKind {
137 Optional<ELF_SHF> Flags;
138 llvm::yaml::Hex64 Address;
140 llvm::yaml::Hex64 AddressAlign;
141 Optional<llvm::yaml::Hex64> EntSize;
143 // This can be used to override the sh_offset field. It does not place the
144 // section data at the offset specified. Useful for creating invalid objects.
145 Optional<llvm::yaml::Hex64> ShOffset;
147 // This can be used to override the sh_size field. It does not affect the
149 Optional<llvm::yaml::Hex64> ShSize;
151 Section(SectionKind Kind) : Kind(Kind) {}
155 struct DynamicSection : Section {
156 std::vector<DynamicEntry> Entries;
157 Optional<yaml::BinaryRef> Content;
159 DynamicSection() : Section(SectionKind::Dynamic) {}
161 static bool classof(const Section *S) {
162 return S->Kind == SectionKind::Dynamic;
166 struct RawContentSection : Section {
167 Optional<yaml::BinaryRef> Content;
168 Optional<llvm::yaml::Hex64> Size;
169 Optional<llvm::yaml::Hex64> Info;
171 RawContentSection() : Section(SectionKind::RawContent) {}
173 static bool classof(const Section *S) {
174 return S->Kind == SectionKind::RawContent;
178 struct NoBitsSection : Section {
179 llvm::yaml::Hex64 Size;
181 NoBitsSection() : Section(SectionKind::NoBits) {}
183 static bool classof(const Section *S) {
184 return S->Kind == SectionKind::NoBits;
188 struct VernauxEntry {
195 struct VerneedEntry {
198 std::vector<VernauxEntry> AuxV;
201 struct VerneedSection : Section {
202 std::vector<VerneedEntry> VerneedV;
203 llvm::yaml::Hex64 Info;
205 VerneedSection() : Section(SectionKind::Verneed) {}
207 static bool classof(const Section *S) {
208 return S->Kind == SectionKind::Verneed;
212 struct SymverSection : Section {
213 std::vector<uint16_t> Entries;
215 SymverSection() : Section(SectionKind::Symver) {}
217 static bool classof(const Section *S) {
218 return S->Kind == SectionKind::Symver;
227 std::vector<StringRef> VerNames;
230 struct VerdefSection : Section {
231 std::vector<VerdefEntry> Entries;
232 llvm::yaml::Hex64 Info;
234 VerdefSection() : Section(SectionKind::Verdef) {}
236 static bool classof(const Section *S) {
237 return S->Kind == SectionKind::Verdef;
241 struct Group : Section {
242 // Members of a group contain a flag and a list of section indices
243 // that are part of the group.
244 std::vector<SectionOrType> Members;
245 StringRef Signature; /* Info */
247 Group() : Section(SectionKind::Group) {}
249 static bool classof(const Section *S) {
250 return S->Kind == SectionKind::Group;
255 llvm::yaml::Hex64 Offset;
258 Optional<StringRef> Symbol;
261 struct RelocationSection : Section {
262 std::vector<Relocation> Relocations;
263 StringRef RelocatableSec; /* Info */
265 RelocationSection() : Section(SectionKind::Relocation) {}
267 static bool classof(const Section *S) {
268 return S->Kind == SectionKind::Relocation;
272 // Represents .MIPS.abiflags section
273 struct MipsABIFlags : Section {
274 llvm::yaml::Hex16 Version;
276 llvm::yaml::Hex8 ISARevision;
277 MIPS_AFL_REG GPRSize;
278 MIPS_AFL_REG CPR1Size;
279 MIPS_AFL_REG CPR2Size;
281 MIPS_AFL_EXT ISAExtension;
283 MIPS_AFL_FLAGS1 Flags1;
284 llvm::yaml::Hex32 Flags2;
286 MipsABIFlags() : Section(SectionKind::MipsABIFlags) {}
288 static bool classof(const Section *S) {
289 return S->Kind == SectionKind::MipsABIFlags;
295 std::vector<ProgramHeader> ProgramHeaders;
296 std::vector<std::unique_ptr<Section>> Sections;
297 // Although in reality the symbols reside in a section, it is a lot
298 // cleaner and nicer if we read them from the YAML as a separate
299 // top-level key, which automatically ensures that invariants like there
300 // being a single SHT_SYMTAB section are upheld.
301 std::vector<Symbol> Symbols;
302 std::vector<Symbol> DynamicSymbols;
305 } // end namespace ELFYAML
306 } // end namespace llvm
308 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
309 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
310 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
311 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
312 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)
313 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)
314 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)
315 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
316 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
317 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)
323 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
324 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
327 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
328 static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
332 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
333 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
337 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
338 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
342 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
343 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
347 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
348 static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
352 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
353 static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
356 template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
357 static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
361 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
362 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
366 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
367 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
370 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
371 static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
374 template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
375 static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
379 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
380 static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
384 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
385 static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
389 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
390 static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
394 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
395 static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
399 struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
400 static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
404 struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
405 static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
409 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
410 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
414 struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
415 static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
419 struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
420 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
424 struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
425 static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
429 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
430 static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
434 struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
435 static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
439 struct MappingTraits<ELFYAML::FileHeader> {
440 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
443 template <> struct MappingTraits<ELFYAML::ProgramHeader> {
444 static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
448 struct MappingTraits<ELFYAML::Symbol> {
449 static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
450 static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
453 template <> struct MappingTraits<ELFYAML::DynamicEntry> {
454 static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
457 template <> struct MappingTraits<ELFYAML::VerdefEntry> {
458 static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
461 template <> struct MappingTraits<ELFYAML::VerneedEntry> {
462 static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
465 template <> struct MappingTraits<ELFYAML::VernauxEntry> {
466 static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
469 template <> struct MappingTraits<ELFYAML::Relocation> {
470 static void mapping(IO &IO, ELFYAML::Relocation &Rel);
474 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
475 static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
476 static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
480 struct MappingTraits<ELFYAML::Object> {
481 static void mapping(IO &IO, ELFYAML::Object &Object);
484 template <> struct MappingTraits<ELFYAML::SectionOrType> {
485 static void mapping(IO &IO, ELFYAML::SectionOrType §ionOrType);
488 template <> struct MappingTraits<ELFYAML::SectionName> {
489 static void mapping(IO &IO, ELFYAML::SectionName §ionName);
492 } // end namespace yaml
493 } // end namespace llvm
495 #endif // LLVM_OBJECTYAML_ELFYAML_H