blob: 92081f065dce4513b571190a896f5464ff338366 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010011/// This file declares classes for handling the YAML representation
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010012/// of ELF.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_OBJECTYAML_ELFYAML_H
17#define LLVM_OBJECTYAML_ELFYAML_H
18
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ObjectYAML/YAML.h"
21#include "llvm/Support/YAMLTraits.h"
22#include <cstdint>
23#include <memory>
24#include <vector>
25
26namespace llvm {
27namespace ELFYAML {
28
29// These types are invariant across 32/64-bit ELF, so for simplicity just
30// directly give them their exact sizes. We don't need to worry about
31// endianness because these are just the types in the YAMLIO structures,
32// and are appropriately converted to the necessary endianness when
33// reading/generating binary object files.
34// The naming of these types is intended to be ELF_PREFIX, where PREFIX is
35// the common prefix of the respective constants. E.g. ELF_EM corresponds
36// to the `e_machine` constants, like `EM_X86_64`.
37// In the future, these would probably be better suited by C++11 enum
38// class's with appropriate fixed underlying type.
39LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
40LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)
41LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
42LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
43LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
44LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
45// Just use 64, since it can hold 32-bit values too.
46LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
47LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)
48LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
49LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
50LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
51// Just use 64, since it can hold 32-bit values too.
52LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
53LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)
54LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
55LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
56LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
57
58LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
59LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
60LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
61LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
62LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
63LLVM_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.
67struct FileHeader {
68 ELF_ELFCLASS Class;
69 ELF_ELFDATA Data;
70 ELF_ELFOSABI OSABI;
71 ELF_ET Type;
72 ELF_EM Machine;
73 ELF_EF Flags;
74 llvm::yaml::Hex64 Entry;
75};
76
77struct SectionName {
78 StringRef Section;
79};
80
81struct ProgramHeader {
82 ELF_PT Type;
83 ELF_PF Flags;
84 llvm::yaml::Hex64 VAddr;
85 llvm::yaml::Hex64 PAddr;
86 Optional<llvm::yaml::Hex64> Align;
87 std::vector<SectionName> Sections;
88};
89
90struct Symbol {
91 StringRef Name;
92 ELF_STT Type;
93 StringRef Section;
94 Optional<ELF_SHN> Index;
95 llvm::yaml::Hex64 Value;
96 llvm::yaml::Hex64 Size;
97 uint8_t Other;
98};
99
100struct LocalGlobalWeakSymbols {
101 std::vector<Symbol> Local;
102 std::vector<Symbol> Global;
103 std::vector<Symbol> Weak;
104};
105
106struct SectionOrType {
107 StringRef sectionNameOrType;
108};
109
110struct Section {
111 enum class SectionKind {
112 Group,
113 RawContent,
114 Relocation,
115 NoBits,
116 MipsABIFlags
117 };
118 SectionKind Kind;
119 StringRef Name;
120 ELF_SHT Type;
121 ELF_SHF Flags;
122 llvm::yaml::Hex64 Address;
123 StringRef Link;
124 StringRef Info;
125 llvm::yaml::Hex64 AddressAlign;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100126 Optional<llvm::yaml::Hex64> EntSize;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127
128 Section(SectionKind Kind) : Kind(Kind) {}
129 virtual ~Section();
130};
131struct RawContentSection : Section {
132 yaml::BinaryRef Content;
133 llvm::yaml::Hex64 Size;
134
135 RawContentSection() : Section(SectionKind::RawContent) {}
136
137 static bool classof(const Section *S) {
138 return S->Kind == SectionKind::RawContent;
139 }
140};
141
142struct NoBitsSection : Section {
143 llvm::yaml::Hex64 Size;
144
145 NoBitsSection() : Section(SectionKind::NoBits) {}
146
147 static bool classof(const Section *S) {
148 return S->Kind == SectionKind::NoBits;
149 }
150};
151
152struct Group : Section {
153 // Members of a group contain a flag and a list of section indices
154 // that are part of the group.
155 std::vector<SectionOrType> Members;
156
157 Group() : Section(SectionKind::Group) {}
158
159 static bool classof(const Section *S) {
160 return S->Kind == SectionKind::Group;
161 }
162};
163
164struct Relocation {
165 llvm::yaml::Hex64 Offset;
166 int64_t Addend;
167 ELF_REL Type;
168 Optional<StringRef> Symbol;
169};
170
171struct RelocationSection : Section {
172 std::vector<Relocation> Relocations;
173
174 RelocationSection() : Section(SectionKind::Relocation) {}
175
176 static bool classof(const Section *S) {
177 return S->Kind == SectionKind::Relocation;
178 }
179};
180
181// Represents .MIPS.abiflags section
182struct MipsABIFlags : Section {
183 llvm::yaml::Hex16 Version;
184 MIPS_ISA ISALevel;
185 llvm::yaml::Hex8 ISARevision;
186 MIPS_AFL_REG GPRSize;
187 MIPS_AFL_REG CPR1Size;
188 MIPS_AFL_REG CPR2Size;
189 MIPS_ABI_FP FpABI;
190 MIPS_AFL_EXT ISAExtension;
191 MIPS_AFL_ASE ASEs;
192 MIPS_AFL_FLAGS1 Flags1;
193 llvm::yaml::Hex32 Flags2;
194
195 MipsABIFlags() : Section(SectionKind::MipsABIFlags) {}
196
197 static bool classof(const Section *S) {
198 return S->Kind == SectionKind::MipsABIFlags;
199 }
200};
201
202struct Object {
203 FileHeader Header;
204 std::vector<ProgramHeader> ProgramHeaders;
205 std::vector<std::unique_ptr<Section>> Sections;
206 // Although in reality the symbols reside in a section, it is a lot
207 // cleaner and nicer if we read them from the YAML as a separate
208 // top-level key, which automatically ensures that invariants like there
209 // being a single SHT_SYMTAB section are upheld.
210 LocalGlobalWeakSymbols Symbols;
211 LocalGlobalWeakSymbols DynamicSymbols;
212};
213
214} // end namespace ELFYAML
215} // end namespace llvm
216
217LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
218LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
219LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
220LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
221LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
222LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)
223
224namespace llvm {
225namespace yaml {
226
227template <>
228struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
229 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
230};
231
232template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
233 static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
234};
235
236template <>
237struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
238 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
239};
240
241template <>
242struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
243 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
244};
245
246template <>
247struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
248 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
249};
250
251template <>
252struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
253 static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
254};
255
256template <>
257struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
258 static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
259};
260
261template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
262 static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
263};
264
265template <>
266struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
267 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
268};
269
270template <>
271struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
272 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
273};
274
275template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
276 static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
277};
278
279template <>
280struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
281 static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
282};
283
284template <>
285struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
286 static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
287};
288
289template <>
290struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
291 static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
292};
293
294template <>
295struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
296 static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
297};
298
299template <>
300struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
301 static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
302};
303
304template <>
305struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
306 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
307};
308
309template <>
310struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
311 static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
312};
313
314template <>
315struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
316 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
317};
318
319template <>
320struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
321 static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
322};
323
324template <>
325struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
326 static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
327};
328
329template <>
330struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
331 static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
332};
333
334template <>
335struct MappingTraits<ELFYAML::FileHeader> {
336 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
337};
338
339template <> struct MappingTraits<ELFYAML::ProgramHeader> {
340 static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
341};
342
343template <>
344struct MappingTraits<ELFYAML::Symbol> {
345 static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
346 static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
347};
348
349template <>
350struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
351 static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
352};
353
354template <> struct MappingTraits<ELFYAML::Relocation> {
355 static void mapping(IO &IO, ELFYAML::Relocation &Rel);
356};
357
358template <>
359struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
360 static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
361 static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
362};
363
364template <>
365struct MappingTraits<ELFYAML::Object> {
366 static void mapping(IO &IO, ELFYAML::Object &Object);
367};
368
369template <> struct MappingTraits<ELFYAML::SectionOrType> {
370 static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
371};
372
373template <> struct MappingTraits<ELFYAML::SectionName> {
374 static void mapping(IO &IO, ELFYAML::SectionName &sectionName);
375};
376
377} // end namespace yaml
378} // end namespace llvm
379
380#endif // LLVM_OBJECTYAML_ELFYAML_H