blob: b98d4622a36b37f91b1b7e7e04d29d0cbe5496c6 [file] [log] [blame]
Chris Bieneman9062ab92016-05-12 16:04:16 +00001//===- MachOYAML.cpp - MachO YAMLIO implementation ------------------------===//
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// This file defines classes for handling the YAML representation of MachO.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ObjectYAML/MachOYAML.h"
15#include "llvm/Support/Casting.h"
Chris Bieneman3f2eb832016-05-17 19:44:06 +000016#include "llvm/Support/Format.h"
17
18#include <string.h> // For memcpy and memset.
Chris Bieneman9062ab92016-05-12 16:04:16 +000019
20namespace llvm {
21
Chris Bieneman8b5906e2016-05-13 17:41:41 +000022MachOYAML::LoadCommand::~LoadCommand() {}
23
Chris Bieneman9062ab92016-05-12 16:04:16 +000024namespace yaml {
25
Chris Bieneman3f2eb832016-05-17 19:44:06 +000026void ScalarTraits<char_16>::output(const char_16 &Val, void *,
27 llvm::raw_ostream &Out) {
28 Out << Val;
29}
30
31StringRef ScalarTraits<char_16>::input(StringRef Scalar, void *, char_16 &Val) {
32 size_t CopySize = 16 >= Scalar.size() ? 16 : Scalar.size();
33 memcpy((void *)Val, Scalar.data(), CopySize);
34
35 if (Scalar.size() < 16) {
36 memset((void *)&Val[Scalar.size()], 0, 16 - Scalar.size());
37 }
38
39 return StringRef();
40}
41
42bool ScalarTraits<char_16>::mustQuote(StringRef S) { return needsQuotes(S); }
43
44void ScalarTraits<uuid_t>::output(const uuid_t &Val, void *,
45 llvm::raw_ostream &Out) {
46 for (int Idx = 0; Idx < 16; ++Idx) {
47 Out << format("%02" PRIX32, Val[Idx]);
48 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
49 Out << "-";
50 }
51}
52
53StringRef ScalarTraits<uuid_t>::input(StringRef Scalar, void *, uuid_t &Val) {
54 size_t OutIdx = 0;
55 for (size_t Idx = 0; Idx < Scalar.size(); ++Idx) {
56 if (Scalar[Idx] == '-' || OutIdx >= 16)
57 continue;
58 unsigned long long TempInt;
59 if (getAsUnsignedInteger(Scalar.slice(Idx, Idx + 2), 16, TempInt))
60 return "invalid number";
61 if (TempInt > 0xFF)
62 return "out of range number";
63 Val[OutIdx] = static_cast<uint8_t>(TempInt);
64 ++Idx; // increment idx an extra time because we're consuming 2 chars
65 ++OutIdx;
66 }
67 return StringRef();
68}
69
70bool ScalarTraits<uuid_t>::mustQuote(StringRef S) { return needsQuotes(S); }
71
Chris Bieneman9062ab92016-05-12 16:04:16 +000072void MappingTraits<MachOYAML::FileHeader>::mapping(
73 IO &IO, MachOYAML::FileHeader &FileHdr) {
Chris Bieneman24f07472016-05-12 17:44:43 +000074 IO.mapRequired("magic", FileHdr.magic);
Chris Bieneman9062ab92016-05-12 16:04:16 +000075 IO.mapRequired("cputype", FileHdr.cputype);
76 IO.mapRequired("cpusubtype", FileHdr.cpusubtype);
Chris Bieneman668beb22016-05-12 17:53:01 +000077 IO.mapRequired("filetype", FileHdr.filetype);
Chris Bieneman9062ab92016-05-12 16:04:16 +000078 IO.mapRequired("ncmds", FileHdr.ncmds);
Chris Bieneman24f07472016-05-12 17:44:43 +000079 IO.mapRequired("sizeofcmds", FileHdr.sizeofcmds);
Chris Bieneman9062ab92016-05-12 16:04:16 +000080 IO.mapRequired("flags", FileHdr.flags);
Chris Bienemanfc889272016-05-12 18:21:09 +000081 IO.mapOptional("reserved", FileHdr.reserved,
82 static_cast<llvm::yaml::Hex32>(0xDEADBEEFu));
Chris Bieneman9062ab92016-05-12 16:04:16 +000083}
84
85void MappingTraits<MachOYAML::Object>::mapping(IO &IO,
86 MachOYAML::Object &Object) {
87 // If the context isn't already set, tag the document as !mach-o.
88 // For Fat files there will be a different tag so they can be differentiated.
Chris Bieneman24f07472016-05-12 17:44:43 +000089 if (!IO.getContext()) {
Chris Bieneman9062ab92016-05-12 16:04:16 +000090 IO.setContext(&Object);
91 IO.mapTag("!mach-o", true);
92 }
93 IO.mapRequired("FileHeader", Object.Header);
Chris Bieneman8b5906e2016-05-13 17:41:41 +000094 IO.mapOptional("LoadCommands", Object.LoadCommands);
Chris Bieneman9062ab92016-05-12 16:04:16 +000095 IO.setContext(nullptr);
96}
97
Chris Bieneman3f2eb832016-05-17 19:44:06 +000098void MappingTraits<MachOYAML::LoadCommand>::mapping(
99 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
100 IO.mapRequired(
101 "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd);
102 IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
103
104#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
105 case MachO::LCName: \
106 MappingTraits<MachO::LCStruct>::mapping(IO, \
107 LoadCommand.Data.LCStruct##_data); \
108 break;
109
110 switch (LoadCommand.Data.load_command_data.cmd) {
111#include "llvm/Support/MachO.def"
112 }
113}
114
115void MappingTraits<MachO::dyld_info_command>::mapping(
116 IO &IO, MachO::dyld_info_command &LoadCommand) {
117 IO.mapRequired("rebase_off", LoadCommand.rebase_off);
118 IO.mapRequired("rebase_size", LoadCommand.rebase_size);
119 IO.mapRequired("bind_off", LoadCommand.bind_size);
120 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
121 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
122 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_size);
123 IO.mapRequired("export_off", LoadCommand.export_off);
124 IO.mapRequired("export_size", LoadCommand.export_size);
125}
126
127void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) {
128 IO.mapRequired("name", DylibStruct.name);
129 IO.mapRequired("timestamp", DylibStruct.timestamp);
130 IO.mapRequired("current_version", DylibStruct.current_version);
131 IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
132}
133
134void MappingTraits<MachO::dylib_command>::mapping(
135 IO &IO, MachO::dylib_command &LoadCommand) {
136 IO.mapRequired("dylib", LoadCommand.dylib);
137}
138
139void MappingTraits<MachO::dylinker_command>::mapping(
140 IO &IO, MachO::dylinker_command &LoadCommand) {
141
142 IO.mapRequired("name", LoadCommand.name);
143}
144
145void MappingTraits<MachO::dysymtab_command>::mapping(
146 IO &IO, MachO::dysymtab_command &LoadCommand) {
147
148 IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
149 IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
150 IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
151 IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
152 IO.mapRequired("iundefsym", LoadCommand.iundefsym);
153 IO.mapRequired("nundefsym", LoadCommand.nundefsym);
154 IO.mapRequired("tocoff", LoadCommand.tocoff);
155 IO.mapRequired("ntoc", LoadCommand.ntoc);
156 IO.mapRequired("modtaboff", LoadCommand.modtaboff);
157 IO.mapRequired("nmodtab", LoadCommand.nmodtab);
158 IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
159 IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
160 IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
161 IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
162 IO.mapRequired("extreloff", LoadCommand.extreloff);
163 IO.mapRequired("nextrel", LoadCommand.nextrel);
164 IO.mapRequired("locreloff", LoadCommand.locreloff);
165 IO.mapRequired("nlocrel", LoadCommand.nlocrel);
166}
167
168void MappingTraits<MachO::encryption_info_command>::mapping(
169 IO &IO, MachO::encryption_info_command &LoadCommand) {
170
171 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
172 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
173 IO.mapRequired("cryptid", LoadCommand.cryptid);
174}
175
176void MappingTraits<MachO::encryption_info_command_64>::mapping(
177 IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
178
179 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
180 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
181 IO.mapRequired("cryptid", LoadCommand.cryptid);
182 IO.mapRequired("pad", LoadCommand.pad);
183}
184
185void MappingTraits<MachO::entry_point_command>::mapping(
186 IO &IO, MachO::entry_point_command &LoadCommand) {
187
188 IO.mapRequired("entryoff", LoadCommand.entryoff);
189 IO.mapRequired("stacksize", LoadCommand.stacksize);
190}
191
192void MappingTraits<MachO::fvmfile_command>::mapping(
193 IO &IO, MachO::fvmfile_command &LoadCommand) {
194
195 IO.mapRequired("name", LoadCommand.name);
196 IO.mapRequired("header_addr", LoadCommand.header_addr);
197}
198
199void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) {
200 IO.mapRequired("name", FVMLib.name);
201 IO.mapRequired("minor_version", FVMLib.minor_version);
202 IO.mapRequired("header_addr", FVMLib.header_addr);
203}
204
205void MappingTraits<MachO::fvmlib_command>::mapping(
206 IO &IO, MachO::fvmlib_command &LoadCommand) {
207
208 IO.mapRequired("fvmlib", LoadCommand.fvmlib);
209}
210
211void MappingTraits<MachO::ident_command>::mapping(
212 IO &IO, MachO::ident_command &LoadCommand) {}
213
214void MappingTraits<MachO::linkedit_data_command>::mapping(
215 IO &IO, MachO::linkedit_data_command &LoadCommand) {
216
217 IO.mapRequired("dataoff", LoadCommand.dataoff);
218 IO.mapRequired("datasize", LoadCommand.datasize);
219}
220
221void MappingTraits<MachO::linker_option_command>::mapping(
222 IO &IO, MachO::linker_option_command &LoadCommand) {
223
224 IO.mapRequired("count", LoadCommand.count);
225}
226
227void MappingTraits<MachO::prebind_cksum_command>::mapping(
228 IO &IO, MachO::prebind_cksum_command &LoadCommand) {
229
230 IO.mapRequired("cksum", LoadCommand.cksum);
231}
232
233void MappingTraits<MachO::load_command>::mapping(
234 IO &IO, MachO::load_command &LoadCommand) {}
235
236void MappingTraits<MachO::prebound_dylib_command>::mapping(
237 IO &IO, MachO::prebound_dylib_command &LoadCommand) {
238
239 IO.mapRequired("name", LoadCommand.name);
240 IO.mapRequired("nmodules", LoadCommand.nmodules);
241 IO.mapRequired("linked_modules", LoadCommand.linked_modules);
242}
243
244void MappingTraits<MachO::routines_command>::mapping(
245 IO &IO, MachO::routines_command &LoadCommand) {
246
247 IO.mapRequired("init_address", LoadCommand.init_address);
248 IO.mapRequired("init_module", LoadCommand.init_module);
249 IO.mapRequired("reserved1", LoadCommand.reserved1);
250 IO.mapRequired("reserved2", LoadCommand.reserved2);
251 IO.mapRequired("reserved3", LoadCommand.reserved3);
252 IO.mapRequired("reserved4", LoadCommand.reserved4);
253 IO.mapRequired("reserved5", LoadCommand.reserved5);
254 IO.mapRequired("reserved6", LoadCommand.reserved6);
255}
256
257void MappingTraits<MachO::routines_command_64>::mapping(
258 IO &IO, MachO::routines_command_64 &LoadCommand) {
259
260 IO.mapRequired("init_address", LoadCommand.init_address);
261 IO.mapRequired("init_module", LoadCommand.init_module);
262 IO.mapRequired("reserved1", LoadCommand.reserved1);
263 IO.mapRequired("reserved2", LoadCommand.reserved2);
264 IO.mapRequired("reserved3", LoadCommand.reserved3);
265 IO.mapRequired("reserved4", LoadCommand.reserved4);
266 IO.mapRequired("reserved5", LoadCommand.reserved5);
267 IO.mapRequired("reserved6", LoadCommand.reserved6);
268}
269
270void MappingTraits<MachO::rpath_command>::mapping(
271 IO &IO, MachO::rpath_command &LoadCommand) {
272
273 IO.mapRequired("path", LoadCommand.path);
274}
275
276void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) {
277 IO.mapRequired("sectname", Section.sectname);
278 IO.mapRequired("segname", Section.segname);
279 IO.mapRequired("addr", Section.addr);
280 IO.mapRequired("size", Section.size);
281 IO.mapRequired("offset", Section.offset);
282 IO.mapRequired("align", Section.align);
283 IO.mapRequired("reloff", Section.reloff);
284 IO.mapRequired("nreloc", Section.nreloc);
285 IO.mapRequired("flags", Section.flags);
286 IO.mapRequired("reserved1", Section.reserved1);
287 IO.mapRequired("reserved2", Section.reserved2);
288}
289
290void MappingTraits<MachO::section_64>::mapping(IO &IO,
291 MachO::section_64 &Section) {
292 IO.mapRequired("sectname", Section.sectname);
293 IO.mapRequired("segname", Section.segname);
294 IO.mapRequired("addr", Section.addr);
295 IO.mapRequired("size", Section.size);
296 IO.mapRequired("offset", Section.offset);
297 IO.mapRequired("align", Section.align);
298 IO.mapRequired("reloff", Section.reloff);
299 IO.mapRequired("nreloc", Section.nreloc);
300 IO.mapRequired("flags", Section.flags);
301 IO.mapRequired("reserved1", Section.reserved1);
302 IO.mapRequired("reserved2", Section.reserved2);
303 IO.mapRequired("reserved3", Section.reserved3);
304}
305
306void MappingTraits<MachO::segment_command>::mapping(
307 IO &IO, MachO::segment_command &LoadCommand) {
308
309 IO.mapRequired("segname", LoadCommand.segname);
310 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
311 IO.mapRequired("vmsize", LoadCommand.vmsize);
312 IO.mapRequired("fileoff", LoadCommand.fileoff);
313 IO.mapRequired("filesize", LoadCommand.filesize);
314 IO.mapRequired("maxprot", LoadCommand.maxprot);
315 IO.mapRequired("initprot", LoadCommand.initprot);
316 IO.mapRequired("nsects", LoadCommand.nsects);
317 IO.mapRequired("flags", LoadCommand.flags);
318}
319
320void MappingTraits<MachO::segment_command_64>::mapping(
321 IO &IO, MachO::segment_command_64 &LoadCommand) {
322
323 IO.mapRequired("segname", LoadCommand.segname);
324 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
325 IO.mapRequired("vmsize", LoadCommand.vmsize);
326 IO.mapRequired("fileoff", LoadCommand.fileoff);
327 IO.mapRequired("filesize", LoadCommand.filesize);
328 IO.mapRequired("maxprot", LoadCommand.maxprot);
329 IO.mapRequired("initprot", LoadCommand.initprot);
330 IO.mapRequired("nsects", LoadCommand.nsects);
331 IO.mapRequired("flags", LoadCommand.flags);
332}
333
334void MappingTraits<MachO::source_version_command>::mapping(
335 IO &IO, MachO::source_version_command &LoadCommand) {
336
337 IO.mapRequired("version", LoadCommand.version);
338}
339
340void MappingTraits<MachO::sub_client_command>::mapping(
341 IO &IO, MachO::sub_client_command &LoadCommand) {
342
343 IO.mapRequired("client", LoadCommand.client);
344}
345
346void MappingTraits<MachO::sub_framework_command>::mapping(
347 IO &IO, MachO::sub_framework_command &LoadCommand) {
348
349 IO.mapRequired("umbrella", LoadCommand.umbrella);
350}
351
352void MappingTraits<MachO::sub_library_command>::mapping(
353 IO &IO, MachO::sub_library_command &LoadCommand) {
354
355 IO.mapRequired("sub_library", LoadCommand.sub_library);
356}
357
358void MappingTraits<MachO::sub_umbrella_command>::mapping(
359 IO &IO, MachO::sub_umbrella_command &LoadCommand) {
360
361 IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
362}
363
364void MappingTraits<MachO::symseg_command>::mapping(
365 IO &IO, MachO::symseg_command &LoadCommand) {
366
367 IO.mapRequired("offset", LoadCommand.offset);
368 IO.mapRequired("size", LoadCommand.size);
369}
370
371void MappingTraits<MachO::symtab_command>::mapping(
372 IO &IO, MachO::symtab_command &LoadCommand) {
373
374 IO.mapRequired("symoff", LoadCommand.symoff);
375 IO.mapRequired("nsyms", LoadCommand.nsyms);
376 IO.mapRequired("stroff", LoadCommand.stroff);
377 IO.mapRequired("strsize", LoadCommand.strsize);
378}
379
380void MappingTraits<MachO::thread_command>::mapping(
381 IO &IO, MachO::thread_command &LoadCommand) {}
382
383void MappingTraits<MachO::twolevel_hints_command>::mapping(
384 IO &IO, MachO::twolevel_hints_command &LoadCommand) {
385
386 IO.mapRequired("offset", LoadCommand.offset);
387 IO.mapRequired("nhints", LoadCommand.nhints);
388}
389
390void MappingTraits<MachO::uuid_command>::mapping(
391 IO &IO, MachO::uuid_command &LoadCommand) {
392
393 IO.mapRequired("cmdsize", LoadCommand.cmdsize);
394 IO.mapRequired("uuid", LoadCommand.uuid);
395}
396
397void MappingTraits<MachO::version_min_command>::mapping(
398 IO &IO, MachO::version_min_command &LoadCommand) {
399
400 IO.mapRequired("version", LoadCommand.version);
401 IO.mapRequired("sdk", LoadCommand.sdk);
Chris Bieneman8b5906e2016-05-13 17:41:41 +0000402}
403
Chris Bieneman9062ab92016-05-12 16:04:16 +0000404} // namespace llvm::yaml
405
406} // namespace llvm