blob: 2faefca432e5d585f523f437448746b21fef38e5 [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 Bieneman3f2eb832016-05-17 19:44:06 +000099void MappingTraits<MachOYAML::LoadCommand>::mapping(
100 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
101 IO.mapRequired(
102 "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd);
103 IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
104
105#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
106 case MachO::LCName: \
107 MappingTraits<MachO::LCStruct>::mapping(IO, \
108 LoadCommand.Data.LCStruct##_data); \
109 break;
110
111 switch (LoadCommand.Data.load_command_data.cmd) {
112#include "llvm/Support/MachO.def"
113 }
Chris Bienemanf605d102016-05-19 20:48:54 +0000114 if (LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT ||
115 LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) {
116 IO.mapOptional("Sections", LoadCommand.Sections);
117 }
Chris Bieneman3f2eb832016-05-17 19:44:06 +0000118}
119
120void MappingTraits<MachO::dyld_info_command>::mapping(
121 IO &IO, MachO::dyld_info_command &LoadCommand) {
122 IO.mapRequired("rebase_off", LoadCommand.rebase_off);
123 IO.mapRequired("rebase_size", LoadCommand.rebase_size);
Chris Bieneman2de17d42016-05-18 16:17:23 +0000124 IO.mapRequired("bind_off", LoadCommand.bind_off);
125 IO.mapRequired("bind_size", LoadCommand.bind_size);
Chris Bieneman3f2eb832016-05-17 19:44:06 +0000126 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
127 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
Chris Bieneman2de17d42016-05-18 16:17:23 +0000128 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off);
129 IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size);
Chris Bieneman3f2eb832016-05-17 19:44:06 +0000130 IO.mapRequired("export_off", LoadCommand.export_off);
131 IO.mapRequired("export_size", LoadCommand.export_size);
132}
133
Chris Bieneman2de17d42016-05-18 16:17:23 +0000134void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
135 MachOYAML::Section &Section) {
136 IO.mapRequired("sectname", Section.sectname);
137 IO.mapRequired("segname", Section.segname);
138 IO.mapRequired("addr", Section.addr);
139 IO.mapRequired("size", Section.size);
140 IO.mapRequired("offset", Section.offset);
141 IO.mapRequired("align", Section.align);
142 IO.mapRequired("reloff", Section.reloff);
143 IO.mapRequired("nreloc", Section.nreloc);
144 IO.mapRequired("flags", Section.flags);
145 IO.mapRequired("reserved1", Section.reserved1);
146 IO.mapRequired("reserved2", Section.reserved2);
147 IO.mapOptional("reserved3", Section.reserved3);
148}
149
Chris Bieneman3f2eb832016-05-17 19:44:06 +0000150void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) {
151 IO.mapRequired("name", DylibStruct.name);
152 IO.mapRequired("timestamp", DylibStruct.timestamp);
153 IO.mapRequired("current_version", DylibStruct.current_version);
154 IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
155}
156
157void MappingTraits<MachO::dylib_command>::mapping(
158 IO &IO, MachO::dylib_command &LoadCommand) {
159 IO.mapRequired("dylib", LoadCommand.dylib);
160}
161
162void MappingTraits<MachO::dylinker_command>::mapping(
163 IO &IO, MachO::dylinker_command &LoadCommand) {
164
165 IO.mapRequired("name", LoadCommand.name);
166}
167
168void MappingTraits<MachO::dysymtab_command>::mapping(
169 IO &IO, MachO::dysymtab_command &LoadCommand) {
170
171 IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
172 IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
173 IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
174 IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
175 IO.mapRequired("iundefsym", LoadCommand.iundefsym);
176 IO.mapRequired("nundefsym", LoadCommand.nundefsym);
177 IO.mapRequired("tocoff", LoadCommand.tocoff);
178 IO.mapRequired("ntoc", LoadCommand.ntoc);
179 IO.mapRequired("modtaboff", LoadCommand.modtaboff);
180 IO.mapRequired("nmodtab", LoadCommand.nmodtab);
181 IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
182 IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
183 IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
184 IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
185 IO.mapRequired("extreloff", LoadCommand.extreloff);
186 IO.mapRequired("nextrel", LoadCommand.nextrel);
187 IO.mapRequired("locreloff", LoadCommand.locreloff);
188 IO.mapRequired("nlocrel", LoadCommand.nlocrel);
189}
190
191void MappingTraits<MachO::encryption_info_command>::mapping(
192 IO &IO, MachO::encryption_info_command &LoadCommand) {
193
194 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
195 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
196 IO.mapRequired("cryptid", LoadCommand.cryptid);
197}
198
199void MappingTraits<MachO::encryption_info_command_64>::mapping(
200 IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
201
202 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
203 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
204 IO.mapRequired("cryptid", LoadCommand.cryptid);
205 IO.mapRequired("pad", LoadCommand.pad);
206}
207
208void MappingTraits<MachO::entry_point_command>::mapping(
209 IO &IO, MachO::entry_point_command &LoadCommand) {
210
211 IO.mapRequired("entryoff", LoadCommand.entryoff);
212 IO.mapRequired("stacksize", LoadCommand.stacksize);
213}
214
215void MappingTraits<MachO::fvmfile_command>::mapping(
216 IO &IO, MachO::fvmfile_command &LoadCommand) {
217
218 IO.mapRequired("name", LoadCommand.name);
219 IO.mapRequired("header_addr", LoadCommand.header_addr);
220}
221
222void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) {
223 IO.mapRequired("name", FVMLib.name);
224 IO.mapRequired("minor_version", FVMLib.minor_version);
225 IO.mapRequired("header_addr", FVMLib.header_addr);
226}
227
228void MappingTraits<MachO::fvmlib_command>::mapping(
229 IO &IO, MachO::fvmlib_command &LoadCommand) {
230
231 IO.mapRequired("fvmlib", LoadCommand.fvmlib);
232}
233
234void MappingTraits<MachO::ident_command>::mapping(
235 IO &IO, MachO::ident_command &LoadCommand) {}
236
237void MappingTraits<MachO::linkedit_data_command>::mapping(
238 IO &IO, MachO::linkedit_data_command &LoadCommand) {
239
240 IO.mapRequired("dataoff", LoadCommand.dataoff);
241 IO.mapRequired("datasize", LoadCommand.datasize);
242}
243
244void MappingTraits<MachO::linker_option_command>::mapping(
245 IO &IO, MachO::linker_option_command &LoadCommand) {
246
247 IO.mapRequired("count", LoadCommand.count);
248}
249
250void MappingTraits<MachO::prebind_cksum_command>::mapping(
251 IO &IO, MachO::prebind_cksum_command &LoadCommand) {
252
253 IO.mapRequired("cksum", LoadCommand.cksum);
254}
255
256void MappingTraits<MachO::load_command>::mapping(
257 IO &IO, MachO::load_command &LoadCommand) {}
258
259void MappingTraits<MachO::prebound_dylib_command>::mapping(
260 IO &IO, MachO::prebound_dylib_command &LoadCommand) {
261
262 IO.mapRequired("name", LoadCommand.name);
263 IO.mapRequired("nmodules", LoadCommand.nmodules);
264 IO.mapRequired("linked_modules", LoadCommand.linked_modules);
265}
266
267void MappingTraits<MachO::routines_command>::mapping(
268 IO &IO, MachO::routines_command &LoadCommand) {
269
270 IO.mapRequired("init_address", LoadCommand.init_address);
271 IO.mapRequired("init_module", LoadCommand.init_module);
272 IO.mapRequired("reserved1", LoadCommand.reserved1);
273 IO.mapRequired("reserved2", LoadCommand.reserved2);
274 IO.mapRequired("reserved3", LoadCommand.reserved3);
275 IO.mapRequired("reserved4", LoadCommand.reserved4);
276 IO.mapRequired("reserved5", LoadCommand.reserved5);
277 IO.mapRequired("reserved6", LoadCommand.reserved6);
278}
279
280void MappingTraits<MachO::routines_command_64>::mapping(
281 IO &IO, MachO::routines_command_64 &LoadCommand) {
282
283 IO.mapRequired("init_address", LoadCommand.init_address);
284 IO.mapRequired("init_module", LoadCommand.init_module);
285 IO.mapRequired("reserved1", LoadCommand.reserved1);
286 IO.mapRequired("reserved2", LoadCommand.reserved2);
287 IO.mapRequired("reserved3", LoadCommand.reserved3);
288 IO.mapRequired("reserved4", LoadCommand.reserved4);
289 IO.mapRequired("reserved5", LoadCommand.reserved5);
290 IO.mapRequired("reserved6", LoadCommand.reserved6);
291}
292
293void MappingTraits<MachO::rpath_command>::mapping(
294 IO &IO, MachO::rpath_command &LoadCommand) {
295
296 IO.mapRequired("path", LoadCommand.path);
297}
298
299void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) {
300 IO.mapRequired("sectname", Section.sectname);
301 IO.mapRequired("segname", Section.segname);
302 IO.mapRequired("addr", Section.addr);
303 IO.mapRequired("size", Section.size);
304 IO.mapRequired("offset", Section.offset);
305 IO.mapRequired("align", Section.align);
306 IO.mapRequired("reloff", Section.reloff);
307 IO.mapRequired("nreloc", Section.nreloc);
308 IO.mapRequired("flags", Section.flags);
309 IO.mapRequired("reserved1", Section.reserved1);
310 IO.mapRequired("reserved2", Section.reserved2);
311}
312
313void MappingTraits<MachO::section_64>::mapping(IO &IO,
314 MachO::section_64 &Section) {
315 IO.mapRequired("sectname", Section.sectname);
316 IO.mapRequired("segname", Section.segname);
317 IO.mapRequired("addr", Section.addr);
318 IO.mapRequired("size", Section.size);
319 IO.mapRequired("offset", Section.offset);
320 IO.mapRequired("align", Section.align);
321 IO.mapRequired("reloff", Section.reloff);
322 IO.mapRequired("nreloc", Section.nreloc);
323 IO.mapRequired("flags", Section.flags);
324 IO.mapRequired("reserved1", Section.reserved1);
325 IO.mapRequired("reserved2", Section.reserved2);
326 IO.mapRequired("reserved3", Section.reserved3);
327}
328
329void MappingTraits<MachO::segment_command>::mapping(
330 IO &IO, MachO::segment_command &LoadCommand) {
331
332 IO.mapRequired("segname", LoadCommand.segname);
333 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
334 IO.mapRequired("vmsize", LoadCommand.vmsize);
335 IO.mapRequired("fileoff", LoadCommand.fileoff);
336 IO.mapRequired("filesize", LoadCommand.filesize);
337 IO.mapRequired("maxprot", LoadCommand.maxprot);
338 IO.mapRequired("initprot", LoadCommand.initprot);
339 IO.mapRequired("nsects", LoadCommand.nsects);
340 IO.mapRequired("flags", LoadCommand.flags);
341}
342
343void MappingTraits<MachO::segment_command_64>::mapping(
344 IO &IO, MachO::segment_command_64 &LoadCommand) {
345
346 IO.mapRequired("segname", LoadCommand.segname);
347 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
348 IO.mapRequired("vmsize", LoadCommand.vmsize);
349 IO.mapRequired("fileoff", LoadCommand.fileoff);
350 IO.mapRequired("filesize", LoadCommand.filesize);
351 IO.mapRequired("maxprot", LoadCommand.maxprot);
352 IO.mapRequired("initprot", LoadCommand.initprot);
353 IO.mapRequired("nsects", LoadCommand.nsects);
354 IO.mapRequired("flags", LoadCommand.flags);
355}
356
357void MappingTraits<MachO::source_version_command>::mapping(
358 IO &IO, MachO::source_version_command &LoadCommand) {
359
360 IO.mapRequired("version", LoadCommand.version);
361}
362
363void MappingTraits<MachO::sub_client_command>::mapping(
364 IO &IO, MachO::sub_client_command &LoadCommand) {
365
366 IO.mapRequired("client", LoadCommand.client);
367}
368
369void MappingTraits<MachO::sub_framework_command>::mapping(
370 IO &IO, MachO::sub_framework_command &LoadCommand) {
371
372 IO.mapRequired("umbrella", LoadCommand.umbrella);
373}
374
375void MappingTraits<MachO::sub_library_command>::mapping(
376 IO &IO, MachO::sub_library_command &LoadCommand) {
377
378 IO.mapRequired("sub_library", LoadCommand.sub_library);
379}
380
381void MappingTraits<MachO::sub_umbrella_command>::mapping(
382 IO &IO, MachO::sub_umbrella_command &LoadCommand) {
383
384 IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
385}
386
387void MappingTraits<MachO::symseg_command>::mapping(
388 IO &IO, MachO::symseg_command &LoadCommand) {
389
390 IO.mapRequired("offset", LoadCommand.offset);
391 IO.mapRequired("size", LoadCommand.size);
392}
393
394void MappingTraits<MachO::symtab_command>::mapping(
395 IO &IO, MachO::symtab_command &LoadCommand) {
396
397 IO.mapRequired("symoff", LoadCommand.symoff);
398 IO.mapRequired("nsyms", LoadCommand.nsyms);
399 IO.mapRequired("stroff", LoadCommand.stroff);
400 IO.mapRequired("strsize", LoadCommand.strsize);
401}
402
403void MappingTraits<MachO::thread_command>::mapping(
404 IO &IO, MachO::thread_command &LoadCommand) {}
405
406void MappingTraits<MachO::twolevel_hints_command>::mapping(
407 IO &IO, MachO::twolevel_hints_command &LoadCommand) {
408
409 IO.mapRequired("offset", LoadCommand.offset);
410 IO.mapRequired("nhints", LoadCommand.nhints);
411}
412
413void MappingTraits<MachO::uuid_command>::mapping(
414 IO &IO, MachO::uuid_command &LoadCommand) {
415
416 IO.mapRequired("cmdsize", LoadCommand.cmdsize);
417 IO.mapRequired("uuid", LoadCommand.uuid);
418}
419
420void MappingTraits<MachO::version_min_command>::mapping(
421 IO &IO, MachO::version_min_command &LoadCommand) {
422
423 IO.mapRequired("version", LoadCommand.version);
424 IO.mapRequired("sdk", LoadCommand.sdk);
Chris Bieneman8b5906e2016-05-13 17:41:41 +0000425}
426
Chris Bieneman9062ab92016-05-12 16:04:16 +0000427} // namespace llvm::yaml
428
429} // namespace llvm