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