Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / ObjectYAML / MachOYAML.h
blobd7e1c033f43b8d81a1e918593602ffde079e959e
1 //===- MachOYAML.h - Mach-O 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 Mach-O.
12 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECTYAML_MACHOYAML_H
16 #define LLVM_OBJECTYAML_MACHOYAML_H
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/MachO.h"
20 #include "llvm/ObjectYAML/DWARFYAML.h"
21 #include "llvm/Support/YAMLTraits.h"
22 #include <cstdint>
23 #include <string>
24 #include <vector>
26 namespace llvm {
27 namespace MachOYAML {
29 struct Section {
30 char sectname[16];
31 char segname[16];
32 llvm::yaml::Hex64 addr;
33 uint64_t size;
34 llvm::yaml::Hex32 offset;
35 uint32_t align;
36 llvm::yaml::Hex32 reloff;
37 uint32_t nreloc;
38 llvm::yaml::Hex32 flags;
39 llvm::yaml::Hex32 reserved1;
40 llvm::yaml::Hex32 reserved2;
41 llvm::yaml::Hex32 reserved3;
44 struct FileHeader {
45 llvm::yaml::Hex32 magic;
46 llvm::yaml::Hex32 cputype;
47 llvm::yaml::Hex32 cpusubtype;
48 llvm::yaml::Hex32 filetype;
49 uint32_t ncmds;
50 uint32_t sizeofcmds;
51 llvm::yaml::Hex32 flags;
52 llvm::yaml::Hex32 reserved;
55 struct LoadCommand {
56 virtual ~LoadCommand();
58 llvm::MachO::macho_load_command Data;
59 std::vector<Section> Sections;
60 std::vector<MachO::build_tool_version> Tools;
61 std::vector<llvm::yaml::Hex8> PayloadBytes;
62 std::string PayloadString;
63 uint64_t ZeroPadBytes;
66 struct NListEntry {
67 uint32_t n_strx;
68 llvm::yaml::Hex8 n_type;
69 uint8_t n_sect;
70 uint16_t n_desc;
71 uint64_t n_value;
74 struct RebaseOpcode {
75 MachO::RebaseOpcode Opcode;
76 uint8_t Imm;
77 std::vector<yaml::Hex64> ExtraData;
80 struct BindOpcode {
81 MachO::BindOpcode Opcode;
82 uint8_t Imm;
83 std::vector<yaml::Hex64> ULEBExtraData;
84 std::vector<int64_t> SLEBExtraData;
85 StringRef Symbol;
88 struct ExportEntry {
89 uint64_t TerminalSize = 0;
90 uint64_t NodeOffset = 0;
91 std::string Name;
92 llvm::yaml::Hex64 Flags = 0;
93 llvm::yaml::Hex64 Address = 0;
94 llvm::yaml::Hex64 Other = 0;
95 std::string ImportName;
96 std::vector<MachOYAML::ExportEntry> Children;
99 struct LinkEditData {
100 std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
101 std::vector<MachOYAML::BindOpcode> BindOpcodes;
102 std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
103 std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
104 MachOYAML::ExportEntry ExportTrie;
105 std::vector<NListEntry> NameList;
106 std::vector<StringRef> StringTable;
108 bool isEmpty() const;
111 struct Object {
112 bool IsLittleEndian;
113 FileHeader Header;
114 std::vector<LoadCommand> LoadCommands;
115 std::vector<Section> Sections;
116 LinkEditData LinkEdit;
117 DWARFYAML::Data DWARF;
120 struct FatHeader {
121 llvm::yaml::Hex32 magic;
122 uint32_t nfat_arch;
125 struct FatArch {
126 llvm::yaml::Hex32 cputype;
127 llvm::yaml::Hex32 cpusubtype;
128 llvm::yaml::Hex64 offset;
129 uint64_t size;
130 uint32_t align;
131 llvm::yaml::Hex32 reserved;
134 struct UniversalBinary {
135 FatHeader Header;
136 std::vector<FatArch> FatArchs;
137 std::vector<Object> Slices;
140 } // end namespace MachOYAML
141 } // end namespace llvm
143 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
144 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
145 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::RebaseOpcode)
146 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::BindOpcode)
147 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
148 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
149 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
150 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
151 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachO::build_tool_version)
153 namespace llvm {
155 class raw_ostream;
157 namespace yaml {
159 template <> struct MappingTraits<MachOYAML::FileHeader> {
160 static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
163 template <> struct MappingTraits<MachOYAML::Object> {
164 static void mapping(IO &IO, MachOYAML::Object &Object);
167 template <> struct MappingTraits<MachOYAML::FatHeader> {
168 static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
171 template <> struct MappingTraits<MachOYAML::FatArch> {
172 static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
175 template <> struct MappingTraits<MachOYAML::UniversalBinary> {
176 static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
179 template <> struct MappingTraits<MachOYAML::LoadCommand> {
180 static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
183 template <> struct MappingTraits<MachOYAML::LinkEditData> {
184 static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
187 template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
188 static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
191 template <> struct MappingTraits<MachOYAML::BindOpcode> {
192 static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
195 template <> struct MappingTraits<MachOYAML::ExportEntry> {
196 static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
199 template <> struct MappingTraits<MachOYAML::Section> {
200 static void mapping(IO &IO, MachOYAML::Section &Section);
203 template <> struct MappingTraits<MachOYAML::NListEntry> {
204 static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
207 template <> struct MappingTraits<MachO::build_tool_version> {
208 static void mapping(IO &IO, MachO::build_tool_version &tool);
211 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
212 io.enumCase(value, #LCName, MachO::LCName);
214 template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
215 static void enumeration(IO &io, MachO::LoadCommandType &value) {
216 #include "llvm/BinaryFormat/MachO.def"
217 io.enumFallback<Hex32>(value);
221 #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
223 template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
224 static void enumeration(IO &io, MachO::RebaseOpcode &value) {
225 ENUM_CASE(REBASE_OPCODE_DONE)
226 ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM)
227 ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
228 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB)
229 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED)
230 ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES)
231 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
232 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
233 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB)
234 io.enumFallback<Hex8>(value);
238 template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
239 static void enumeration(IO &io, MachO::BindOpcode &value) {
240 ENUM_CASE(BIND_OPCODE_DONE)
241 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM)
242 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB)
243 ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM)
244 ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
245 ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM)
246 ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB)
247 ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
248 ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB)
249 ENUM_CASE(BIND_OPCODE_DO_BIND)
250 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
251 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED)
252 ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB)
253 io.enumFallback<Hex8>(value);
257 // This trait is used for 16-byte chars in Mach structures used for strings
258 using char_16 = char[16];
260 template <> struct ScalarTraits<char_16> {
261 static void output(const char_16 &Val, void *, raw_ostream &Out);
262 static StringRef input(StringRef Scalar, void *, char_16 &Val);
263 static QuotingType mustQuote(StringRef S);
266 // This trait is used for UUIDs. It reads and writes them matching otool's
267 // formatting style.
268 using uuid_t = raw_ostream::uuid_t;
270 template <> struct ScalarTraits<uuid_t> {
271 static void output(const uuid_t &Val, void *, raw_ostream &Out);
272 static StringRef input(StringRef Scalar, void *, uuid_t &Val);
273 static QuotingType mustQuote(StringRef S);
276 // Load Command struct mapping traits
278 #define LOAD_COMMAND_STRUCT(LCStruct) \
279 template <> struct MappingTraits<MachO::LCStruct> { \
280 static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
283 #include "llvm/BinaryFormat/MachO.def"
285 // Extra structures used by load commands
286 template <> struct MappingTraits<MachO::dylib> {
287 static void mapping(IO &IO, MachO::dylib &LoadCommand);
290 template <> struct MappingTraits<MachO::fvmlib> {
291 static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
294 template <> struct MappingTraits<MachO::section> {
295 static void mapping(IO &IO, MachO::section &LoadCommand);
298 template <> struct MappingTraits<MachO::section_64> {
299 static void mapping(IO &IO, MachO::section_64 &LoadCommand);
302 } // end namespace yaml
304 } // end namespace llvm
306 #endif // LLVM_OBJECTYAML_MACHOYAML_H