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