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 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OBJECTYAML_MACHOYAML_H
16 #define LLVM_OBJECTYAML_MACHOYAML_H
17
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/MachO.h"
20 #include "llvm/ObjectYAML/DWARFYAML.h"
21 #include "llvm/ObjectYAML/YAML.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include <cstdint>
24 #include <string>
25 #include <vector>
26
27 namespace llvm {
28 namespace MachOYAML {
29
30 struct Section {
31 char sectname[16];
32 char segname[16];
33 llvm::yaml::Hex64 addr;
34 uint64_t size;
35 llvm::yaml::Hex32 offset;
36 uint32_t align;
37 llvm::yaml::Hex32 reloff;
38 uint32_t nreloc;
39 llvm::yaml::Hex32 flags;
40 llvm::yaml::Hex32 reserved1;
41 llvm::yaml::Hex32 reserved2;
42 llvm::yaml::Hex32 reserved3;
43 Optional<llvm::yaml::BinaryRef> content;
44 };
45
46 struct FileHeader {
47 llvm::yaml::Hex32 magic;
48 llvm::yaml::Hex32 cputype;
49 llvm::yaml::Hex32 cpusubtype;
50 llvm::yaml::Hex32 filetype;
51 uint32_t ncmds;
52 uint32_t sizeofcmds;
53 llvm::yaml::Hex32 flags;
54 llvm::yaml::Hex32 reserved;
55 };
56
57 struct LoadCommand {
58 virtual ~LoadCommand();
59
60 llvm::MachO::macho_load_command Data;
61 std::vector<Section> Sections;
62 std::vector<MachO::build_tool_version> Tools;
63 std::vector<llvm::yaml::Hex8> PayloadBytes;
64 std::string PayloadString;
65 uint64_t ZeroPadBytes;
66 };
67
68 struct NListEntry {
69 uint32_t n_strx;
70 llvm::yaml::Hex8 n_type;
71 uint8_t n_sect;
72 uint16_t n_desc;
73 uint64_t n_value;
74 };
75
76 struct RebaseOpcode {
77 MachO::RebaseOpcode Opcode;
78 uint8_t Imm;
79 std::vector<yaml::Hex64> ExtraData;
80 };
81
82 struct BindOpcode {
83 MachO::BindOpcode Opcode;
84 uint8_t Imm;
85 std::vector<yaml::Hex64> ULEBExtraData;
86 std::vector<int64_t> SLEBExtraData;
87 StringRef Symbol;
88 };
89
90 struct ExportEntry {
91 uint64_t TerminalSize = 0;
92 uint64_t NodeOffset = 0;
93 std::string Name;
94 llvm::yaml::Hex64 Flags = 0;
95 llvm::yaml::Hex64 Address = 0;
96 llvm::yaml::Hex64 Other = 0;
97 std::string ImportName;
98 std::vector<MachOYAML::ExportEntry> Children;
99 };
100
101 struct LinkEditData {
102 std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
103 std::vector<MachOYAML::BindOpcode> BindOpcodes;
104 std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
105 std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
106 MachOYAML::ExportEntry ExportTrie;
107 std::vector<NListEntry> NameList;
108 std::vector<StringRef> StringTable;
109
110 bool isEmpty() const;
111 };
112
113 struct Object {
114 bool IsLittleEndian;
115 FileHeader Header;
116 std::vector<LoadCommand> LoadCommands;
117 std::vector<Section> Sections;
118 LinkEditData LinkEdit;
119 DWARFYAML::Data DWARF;
120 };
121
122 struct FatHeader {
123 llvm::yaml::Hex32 magic;
124 uint32_t nfat_arch;
125 };
126
127 struct FatArch {
128 llvm::yaml::Hex32 cputype;
129 llvm::yaml::Hex32 cpusubtype;
130 llvm::yaml::Hex64 offset;
131 uint64_t size;
132 uint32_t align;
133 llvm::yaml::Hex32 reserved;
134 };
135
136 struct UniversalBinary {
137 FatHeader Header;
138 std::vector<FatArch> FatArchs;
139 std::vector<Object> Slices;
140 };
141
142 } // end namespace MachOYAML
143 } // end namespace llvm
144
145 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)146 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
147 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::RebaseOpcode)
148 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::BindOpcode)
149 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
150 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
151 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
152 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
153 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachO::build_tool_version)
154
155 namespace llvm {
156
157 class raw_ostream;
158
159 namespace yaml {
160
161 template <> struct MappingTraits<MachOYAML::FileHeader> {
162 static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
163 };
164
165 template <> struct MappingTraits<MachOYAML::Object> {
166 static void mapping(IO &IO, MachOYAML::Object &Object);
167 };
168
169 template <> struct MappingTraits<MachOYAML::FatHeader> {
170 static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
171 };
172
173 template <> struct MappingTraits<MachOYAML::FatArch> {
174 static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
175 };
176
177 template <> struct MappingTraits<MachOYAML::UniversalBinary> {
178 static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
179 };
180
181 template <> struct MappingTraits<MachOYAML::LoadCommand> {
182 static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
183 };
184
185 template <> struct MappingTraits<MachOYAML::LinkEditData> {
186 static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
187 };
188
189 template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
190 static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
191 };
192
193 template <> struct MappingTraits<MachOYAML::BindOpcode> {
194 static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
195 };
196
197 template <> struct MappingTraits<MachOYAML::ExportEntry> {
198 static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
199 };
200
201 template <> struct MappingTraits<MachOYAML::Section> {
202 static void mapping(IO &IO, MachOYAML::Section &Section);
203 static StringRef validate(IO &io, MachOYAML::Section &Section);
204 };
205
206 template <> struct MappingTraits<MachOYAML::NListEntry> {
207 static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
208 };
209
210 template <> struct MappingTraits<MachO::build_tool_version> {
211 static void mapping(IO &IO, MachO::build_tool_version &tool);
212 };
213
214 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
215 io.enumCase(value, #LCName, MachO::LCName);
216
217 template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
218 static void enumeration(IO &io, MachO::LoadCommandType &value) {
219 #include "llvm/BinaryFormat/MachO.def"
220 io.enumFallback<Hex32>(value);
221 }
222 };
223
224 #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
225
226 template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
227 static void enumeration(IO &io, MachO::RebaseOpcode &value) {
228 ENUM_CASE(REBASE_OPCODE_DONE)
229 ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM)
230 ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
231 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB)
232 ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED)
233 ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES)
234 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
235 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
236 ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB)
237 io.enumFallback<Hex8>(value);
238 }
239 };
240
241 template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
242 static void enumeration(IO &io, MachO::BindOpcode &value) {
243 ENUM_CASE(BIND_OPCODE_DONE)
244 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM)
245 ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB)
246 ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM)
247 ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
248 ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM)
249 ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB)
250 ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
251 ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB)
252 ENUM_CASE(BIND_OPCODE_DO_BIND)
253 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
254 ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED)
255 ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB)
256 io.enumFallback<Hex8>(value);
257 }
258 };
259
260 // This trait is used for 16-byte chars in Mach structures used for strings
261 using char_16 = char[16];
262
263 template <> struct ScalarTraits<char_16> {
264 static void output(const char_16 &Val, void *, raw_ostream &Out);
265 static StringRef input(StringRef Scalar, void *, char_16 &Val);
266 static QuotingType mustQuote(StringRef S);
267 };
268
269 // This trait is used for UUIDs. It reads and writes them matching otool's
270 // formatting style.
271 using uuid_t = raw_ostream::uuid_t;
272
273 template <> struct ScalarTraits<uuid_t> {
274 static void output(const uuid_t &Val, void *, raw_ostream &Out);
275 static StringRef input(StringRef Scalar, void *, uuid_t &Val);
276 static QuotingType mustQuote(StringRef S);
277 };
278
279 // Load Command struct mapping traits
280
281 #define LOAD_COMMAND_STRUCT(LCStruct) \
282 template <> struct MappingTraits<MachO::LCStruct> { \
283 static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
284 };
285
286 #include "llvm/BinaryFormat/MachO.def"
287
288 // Extra structures used by load commands
289 template <> struct MappingTraits<MachO::dylib> {
290 static void mapping(IO &IO, MachO::dylib &LoadCommand);
291 };
292
293 template <> struct MappingTraits<MachO::fvmlib> {
294 static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
295 };
296
297 template <> struct MappingTraits<MachO::section> {
298 static void mapping(IO &IO, MachO::section &LoadCommand);
299 };
300
301 template <> struct MappingTraits<MachO::section_64> {
302 static void mapping(IO &IO, MachO::section_64 &LoadCommand);
303 };
304
305 } // end namespace yaml
306
307 } // end namespace llvm
308
309 #endif // LLVM_OBJECTYAML_MACHOYAML_H
310