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_OBJECT_ELFYAML_H
17 #define LLVM_OBJECT_ELFYAML_H
18 
19 #include "llvm/MC/YAML.h"
20 #include "llvm/Support/ELF.h"
21 
22 namespace llvm {
23 namespace ELFYAML {
24 
25 // These types are invariant across 32/64-bit ELF, so for simplicity just
26 // directly give them their exact sizes. We don't need to worry about
27 // endianness because these are just the types in the YAMLIO structures,
28 // and are appropriately converted to the necessary endianness when
29 // reading/generating binary object files.
30 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
31 // the common prefix of the respective constants. E.g. ELF_EM corresponds
32 // to the `e_machine` constants, like `EM_X86_64`.
33 // In the future, these would probably be better suited by C++11 enum
34 // class's with appropriate fixed underlying type.
35 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
37 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
38 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
39 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
40 // Just use 64, since it can hold 32-bit values too.
41 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
42 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
43 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
44 // Just use 64, since it can hold 32-bit values too.
45 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
46 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
47 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
48 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
49 
50 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
51 // since 64-bit can hold 32-bit values too.
52 struct FileHeader {
53   ELF_ELFCLASS Class;
54   ELF_ELFDATA Data;
55   ELF_ELFOSABI OSABI;
56   ELF_ET Type;
57   ELF_EM Machine;
58   ELF_EF Flags;
59   llvm::yaml::Hex64 Entry;
60 };
61 struct Symbol {
62   StringRef Name;
63   ELF_STT Type;
64   StringRef Section;
65   llvm::yaml::Hex64 Value;
66   llvm::yaml::Hex64 Size;
67   uint8_t Other;
68 };
69 struct LocalGlobalWeakSymbols {
70   std::vector<Symbol> Local;
71   std::vector<Symbol> Global;
72   std::vector<Symbol> Weak;
73 };
74 struct Section {
75   enum class SectionKind { RawContent, Relocation };
76   SectionKind Kind;
77   StringRef Name;
78   ELF_SHT Type;
79   ELF_SHF Flags;
80   llvm::yaml::Hex64 Address;
81   StringRef Link;
82   llvm::yaml::Hex64 AddressAlign;
SectionSection83   Section(SectionKind Kind) : Kind(Kind) {}
84   virtual ~Section();
85 };
86 struct RawContentSection : Section {
87   yaml::BinaryRef Content;
88   llvm::yaml::Hex64 Size;
RawContentSectionRawContentSection89   RawContentSection() : Section(SectionKind::RawContent) {}
classofRawContentSection90   static bool classof(const Section *S) {
91     return S->Kind == SectionKind::RawContent;
92   }
93 };
94 struct Relocation {
95   llvm::yaml::Hex64 Offset;
96   int64_t Addend;
97   ELF_REL Type;
98   StringRef Symbol;
99 };
100 struct RelocationSection : Section {
101   StringRef Info;
102   std::vector<Relocation> Relocations;
RelocationSectionRelocationSection103   RelocationSection() : Section(SectionKind::Relocation) {}
classofRelocationSection104   static bool classof(const Section *S) {
105     return S->Kind == SectionKind::Relocation;
106   }
107 };
108 struct Object {
109   FileHeader Header;
110   std::vector<std::unique_ptr<Section>> Sections;
111   // Although in reality the symbols reside in a section, it is a lot
112   // cleaner and nicer if we read them from the YAML as a separate
113   // top-level key, which automatically ensures that invariants like there
114   // being a single SHT_SYMTAB section are upheld.
115   LocalGlobalWeakSymbols Symbols;
116 };
117 
118 } // end namespace ELFYAML
119 } // end namespace llvm
120 
121 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)122 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
123 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
124 
125 namespace llvm {
126 namespace yaml {
127 
128 template <>
129 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
130   static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
131 };
132 
133 template <>
134 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
135   static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
136 };
137 
138 template <>
139 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
140   static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
141 };
142 
143 template <>
144 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
145   static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
146 };
147 
148 template <>
149 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
150   static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
151 };
152 
153 template <>
154 struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
155   static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
156 };
157 
158 template <>
159 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
160   static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
161 };
162 
163 template <>
164 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
165   static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
166 };
167 
168 template <>
169 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
170   static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
171 };
172 
173 template <>
174 struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
175   static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
176 };
177 
178 template <>
179 struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
180   static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
181 };
182 
183 template <>
184 struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
185   static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
186 };
187 
188 template <>
189 struct MappingTraits<ELFYAML::FileHeader> {
190   static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
191 };
192 
193 template <>
194 struct MappingTraits<ELFYAML::Symbol> {
195   static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
196 };
197 
198 template <>
199 struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
200   static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
201 };
202 
203 template <> struct MappingTraits<ELFYAML::Relocation> {
204   static void mapping(IO &IO, ELFYAML::Relocation &Rel);
205 };
206 
207 template <>
208 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
209   static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
210   static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
211 };
212 
213 template <>
214 struct MappingTraits<ELFYAML::Object> {
215   static void mapping(IO &IO, ELFYAML::Object &Object);
216 };
217 
218 } // end namespace yaml
219 } // end namespace llvm
220 
221 #endif
222