blob: e194e59a4851d724369d192e7e4e2762dec80780 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010/// This file declares classes for handling the YAML representation
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011/// 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"
20#include "llvm/Support/YAMLTraits.h"
21#include <cstdint>
22#include <memory>
23#include <vector>
24
25namespace llvm {
26namespace 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.
38LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
39LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)
40LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
41LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
42LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
43LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
44// Just use 64, since it can hold 32-bit values too.
45LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
Andrew Walbran16937d02019-10-22 13:54:20 +010046// Just use 64, since it can hold 32-bit values too.
47LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)
49LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
50LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
51LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
52// Just use 64, since it can hold 32-bit values too.
53LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
54LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)
55LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
56LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
57LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
58
59LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
60LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
61LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
62LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
63LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
64LLVM_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.
68struct FileHeader {
69 ELF_ELFCLASS Class;
70 ELF_ELFDATA Data;
71 ELF_ELFOSABI OSABI;
Andrew Walbran16937d02019-10-22 13:54:20 +010072 llvm::yaml::Hex8 ABIVersion;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 ELF_ET Type;
74 ELF_EM Machine;
75 ELF_EF Flags;
76 llvm::yaml::Hex64 Entry;
77};
78
79struct SectionName {
80 StringRef Section;
81};
82
83struct ProgramHeader {
84 ELF_PT Type;
85 ELF_PF Flags;
86 llvm::yaml::Hex64 VAddr;
87 llvm::yaml::Hex64 PAddr;
88 Optional<llvm::yaml::Hex64> Align;
89 std::vector<SectionName> Sections;
90};
91
92struct Symbol {
93 StringRef Name;
94 ELF_STT Type;
95 StringRef Section;
96 Optional<ELF_SHN> Index;
97 llvm::yaml::Hex64 Value;
98 llvm::yaml::Hex64 Size;
99 uint8_t Other;
100};
101
102struct LocalGlobalWeakSymbols {
103 std::vector<Symbol> Local;
104 std::vector<Symbol> Global;
105 std::vector<Symbol> Weak;
106};
107
108struct SectionOrType {
109 StringRef sectionNameOrType;
110};
111
Andrew Walbran16937d02019-10-22 13:54:20 +0100112struct DynamicEntry {
113 ELF_DYNTAG Tag;
114 llvm::yaml::Hex64 Val;
115};
116
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117struct Section {
118 enum class SectionKind {
Andrew Walbran16937d02019-10-22 13:54:20 +0100119 Dynamic,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 Group,
121 RawContent,
122 Relocation,
123 NoBits,
124 MipsABIFlags
125 };
126 SectionKind Kind;
127 StringRef Name;
128 ELF_SHT Type;
129 ELF_SHF Flags;
130 llvm::yaml::Hex64 Address;
131 StringRef Link;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132 llvm::yaml::Hex64 AddressAlign;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100133 Optional<llvm::yaml::Hex64> EntSize;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100134
135 Section(SectionKind Kind) : Kind(Kind) {}
136 virtual ~Section();
137};
Andrew Walbran16937d02019-10-22 13:54:20 +0100138
139struct DynamicSection : Section {
140 std::vector<DynamicEntry> Entries;
141
142 DynamicSection() : Section(SectionKind::Dynamic) {}
143
144 static bool classof(const Section *S) {
145 return S->Kind == SectionKind::Dynamic;
146 }
147};
148
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100149struct RawContentSection : Section {
150 yaml::BinaryRef Content;
151 llvm::yaml::Hex64 Size;
152
153 RawContentSection() : Section(SectionKind::RawContent) {}
154
155 static bool classof(const Section *S) {
156 return S->Kind == SectionKind::RawContent;
157 }
158};
159
160struct NoBitsSection : Section {
161 llvm::yaml::Hex64 Size;
162
163 NoBitsSection() : Section(SectionKind::NoBits) {}
164
165 static bool classof(const Section *S) {
166 return S->Kind == SectionKind::NoBits;
167 }
168};
169
170struct 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;
Andrew Walbran16937d02019-10-22 13:54:20 +0100174 StringRef Signature; /* Info */
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100175
176 Group() : Section(SectionKind::Group) {}
177
178 static bool classof(const Section *S) {
179 return S->Kind == SectionKind::Group;
180 }
181};
182
183struct Relocation {
184 llvm::yaml::Hex64 Offset;
185 int64_t Addend;
186 ELF_REL Type;
187 Optional<StringRef> Symbol;
188};
189
190struct RelocationSection : Section {
191 std::vector<Relocation> Relocations;
Andrew Walbran16937d02019-10-22 13:54:20 +0100192 StringRef RelocatableSec; /* Info */
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100193
194 RelocationSection() : Section(SectionKind::Relocation) {}
195
196 static bool classof(const Section *S) {
197 return S->Kind == SectionKind::Relocation;
198 }
199};
200
201// Represents .MIPS.abiflags section
202struct MipsABIFlags : 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
215 MipsABIFlags() : Section(SectionKind::MipsABIFlags) {}
216
217 static bool classof(const Section *S) {
218 return S->Kind == SectionKind::MipsABIFlags;
219 }
220};
221
222struct Object {
223 FileHeader Header;
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.
230 LocalGlobalWeakSymbols Symbols;
231 LocalGlobalWeakSymbols DynamicSymbols;
232};
233
234} // end namespace ELFYAML
235} // end namespace llvm
236
Andrew Walbran16937d02019-10-22 13:54:20 +0100237LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100238LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
239LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
240LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
241LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
242LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
243LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)
244
245namespace llvm {
246namespace yaml {
247
248template <>
249struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
250 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
251};
252
253template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
254 static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
255};
256
257template <>
258struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
259 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
260};
261
262template <>
263struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
264 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
265};
266
267template <>
268struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
269 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
270};
271
272template <>
273struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
274 static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
275};
276
277template <>
278struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
279 static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
280};
281
282template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
283 static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
284};
285
286template <>
287struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
288 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
289};
290
291template <>
292struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
293 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
294};
295
296template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
297 static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
298};
299
300template <>
301struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
302 static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
303};
304
305template <>
306struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
307 static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
308};
309
310template <>
311struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
312 static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
313};
314
315template <>
316struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
317 static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
318};
319
320template <>
Andrew Walbran16937d02019-10-22 13:54:20 +0100321struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
322 static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
323};
324
325template <>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100326struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
327 static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
328};
329
330template <>
331struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
332 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
333};
334
335template <>
336struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
337 static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
338};
339
340template <>
341struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
342 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
343};
344
345template <>
346struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
347 static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
348};
349
350template <>
351struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
352 static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
353};
354
355template <>
356struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
357 static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
358};
359
360template <>
361struct MappingTraits<ELFYAML::FileHeader> {
362 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
363};
364
365template <> struct MappingTraits<ELFYAML::ProgramHeader> {
366 static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
367};
368
369template <>
370struct MappingTraits<ELFYAML::Symbol> {
371 static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
372 static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol);
373};
374
375template <>
376struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
377 static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
378};
379
Andrew Walbran16937d02019-10-22 13:54:20 +0100380template <> struct MappingTraits<ELFYAML::DynamicEntry> {
381 static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
382};
383
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100384template <> struct MappingTraits<ELFYAML::Relocation> {
385 static void mapping(IO &IO, ELFYAML::Relocation &Rel);
386};
387
388template <>
389struct 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
394template <>
395struct MappingTraits<ELFYAML::Object> {
396 static void mapping(IO &IO, ELFYAML::Object &Object);
397};
398
399template <> struct MappingTraits<ELFYAML::SectionOrType> {
400 static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
401};
402
403template <> 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