1 //===- 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"
16 #include "llvm/Support/Format.h"
17 
18 #include <string.h> // For memcpy, memset and strnlen.
19 
20 namespace llvm {
21 
22 MachOYAML::LoadCommand::~LoadCommand() {}
23 
24 namespace yaml {
25 
26 void ScalarTraits<char_16>::output(const char_16 &Val, void *,
27                                    llvm::raw_ostream &Out) {
28   auto Len = strnlen(&Val[0], 16);
29   Out << StringRef(&Val[0], Len);
30 }
31 
32 StringRef 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 
43 bool ScalarTraits<char_16>::mustQuote(StringRef S) { return needsQuotes(S); }
44 
45 void 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 
54 StringRef 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 
71 bool ScalarTraits<uuid_t>::mustQuote(StringRef S) { return needsQuotes(S); }
72 
73 void MappingTraits<MachOYAML::FileHeader>::mapping(
74     IO &IO, MachOYAML::FileHeader &FileHdr) {
75   IO.mapRequired("magic", FileHdr.magic);
76   IO.mapRequired("cputype", FileHdr.cputype);
77   IO.mapRequired("cpusubtype", FileHdr.cpusubtype);
78   IO.mapRequired("filetype", FileHdr.filetype);
79   IO.mapRequired("ncmds", FileHdr.ncmds);
80   IO.mapRequired("sizeofcmds", FileHdr.sizeofcmds);
81   IO.mapRequired("flags", FileHdr.flags);
82   IO.mapOptional("reserved", FileHdr.reserved,
83                  static_cast<llvm::yaml::Hex32>(0xDEADBEEFu));
84 }
85 
86 void 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.
90   if (!IO.getContext()) {
91     IO.setContext(&Object);
92     IO.mapTag("!mach-o", true);
93   }
94   IO.mapRequired("FileHeader", Object.Header);
95   IO.mapOptional("LoadCommands", Object.LoadCommands);
96   IO.mapOptional("LinkEditData", Object.LinkEdit);
97   IO.setContext(nullptr);
98 }
99 
100 void MappingTraits<MachOYAML::LinkEditData>::mapping(
101     IO &IO, MachOYAML::LinkEditData &LinkEditData) {
102   IO.mapOptional("RebaseOpcodes", LinkEditData.RebaseOpcodes);
103   IO.mapOptional("BindOpcodes", LinkEditData.BindOpcodes);
104   IO.mapOptional("WeakBindOpcodes", LinkEditData.WeakBindOpcodes);
105   IO.mapOptional("LazyBindOpcodes", LinkEditData.LazyBindOpcodes);
106   IO.mapOptional("ExportTrie", LinkEditData.ExportTrie);
107   IO.mapOptional("NameList", LinkEditData.NameList);
108   IO.mapOptional("StringTable", LinkEditData.StringTable);
109 }
110 
111 void MappingTraits<MachOYAML::RebaseOpcode>::mapping(
112     IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode) {
113   IO.mapRequired("Opcode", RebaseOpcode.Opcode);
114   IO.mapRequired("Imm", RebaseOpcode.Imm);
115   IO.mapOptional("ExtraData", RebaseOpcode.ExtraData);
116 }
117 
118 void MappingTraits<MachOYAML::BindOpcode>::mapping(
119     IO &IO, MachOYAML::BindOpcode &BindOpcode) {
120   IO.mapRequired("Opcode", BindOpcode.Opcode);
121   IO.mapRequired("Imm", BindOpcode.Imm);
122   IO.mapOptional("ULEBExtraData", BindOpcode.ULEBExtraData);
123   IO.mapOptional("SLEBExtraData", BindOpcode.SLEBExtraData);
124   IO.mapOptional("Symbol", BindOpcode.Symbol);
125 }
126 
127 void MappingTraits<MachOYAML::ExportEntry>::mapping(
128     IO &IO, MachOYAML::ExportEntry &ExportEntry) {
129   IO.mapRequired("TerminalSize", ExportEntry.TerminalSize);
130   IO.mapOptional("NodeOffset", ExportEntry.NodeOffset);
131   IO.mapOptional("Name", ExportEntry.Name);
132   IO.mapOptional("Flags", ExportEntry.Flags);
133   IO.mapOptional("Address", ExportEntry.Address);
134   IO.mapOptional("Other", ExportEntry.Other);
135   IO.mapOptional("ImportName", ExportEntry.ImportName);
136   IO.mapOptional("Children", ExportEntry.Children);
137 }
138 
139 void MappingTraits<MachOYAML::NListEntry>::mapping(
140     IO &IO, MachOYAML::NListEntry &NListEntry) {
141   IO.mapRequired("n_strx", NListEntry.n_strx);
142   IO.mapRequired("n_type", NListEntry.n_type);
143   IO.mapRequired("n_sect", NListEntry.n_sect);
144   IO.mapRequired("n_desc", NListEntry.n_desc);
145   IO.mapRequired("n_value", NListEntry.n_value);
146 }
147 
148 template <typename StructType>
149 void mapLoadCommandData(IO &IO, MachOYAML::LoadCommand &LoadCommand) {}
150 
151 template <>
152 void mapLoadCommandData<MachO::segment_command>(
153     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
154   IO.mapOptional("Sections", LoadCommand.Sections);
155 }
156 
157 template <>
158 void mapLoadCommandData<MachO::segment_command_64>(
159     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
160   IO.mapOptional("Sections", LoadCommand.Sections);
161 }
162 
163 template <>
164 void mapLoadCommandData<MachO::dylib_command>(
165     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
166   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
167 }
168 
169 template <>
170 void mapLoadCommandData<MachO::rpath_command>(
171     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
172   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
173 }
174 
175 template <>
176 void mapLoadCommandData<MachO::dylinker_command>(
177     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
178   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
179 }
180 
181 void MappingTraits<MachOYAML::LoadCommand>::mapping(
182     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
183   IO.mapRequired(
184       "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd);
185   IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
186 
187 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \
188   case MachO::LCName:                                                          \
189     MappingTraits<MachO::LCStruct>::mapping(IO,                                \
190                                             LoadCommand.Data.LCStruct##_data); \
191     mapLoadCommandData<MachO::LCStruct>(IO, LoadCommand);                      \
192     break;
193 
194   switch (LoadCommand.Data.load_command_data.cmd) {
195 #include "llvm/Support/MachO.def"
196   }
197   IO.mapOptional("PayloadBytes", LoadCommand.PayloadBytes);
198   IO.mapOptional("ZeroPadBytes", LoadCommand.ZeroPadBytes, (uint64_t)0ull);
199 }
200 
201 void MappingTraits<MachO::dyld_info_command>::mapping(
202     IO &IO, MachO::dyld_info_command &LoadCommand) {
203   IO.mapRequired("rebase_off", LoadCommand.rebase_off);
204   IO.mapRequired("rebase_size", LoadCommand.rebase_size);
205   IO.mapRequired("bind_off", LoadCommand.bind_off);
206   IO.mapRequired("bind_size", LoadCommand.bind_size);
207   IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
208   IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
209   IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off);
210   IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size);
211   IO.mapRequired("export_off", LoadCommand.export_off);
212   IO.mapRequired("export_size", LoadCommand.export_size);
213 }
214 
215 void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
216                                                 MachOYAML::Section &Section) {
217   IO.mapRequired("sectname", Section.sectname);
218   IO.mapRequired("segname", Section.segname);
219   IO.mapRequired("addr", Section.addr);
220   IO.mapRequired("size", Section.size);
221   IO.mapRequired("offset", Section.offset);
222   IO.mapRequired("align", Section.align);
223   IO.mapRequired("reloff", Section.reloff);
224   IO.mapRequired("nreloc", Section.nreloc);
225   IO.mapRequired("flags", Section.flags);
226   IO.mapRequired("reserved1", Section.reserved1);
227   IO.mapRequired("reserved2", Section.reserved2);
228   IO.mapOptional("reserved3", Section.reserved3);
229 }
230 
231 void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) {
232   IO.mapRequired("name", DylibStruct.name);
233   IO.mapRequired("timestamp", DylibStruct.timestamp);
234   IO.mapRequired("current_version", DylibStruct.current_version);
235   IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
236 }
237 
238 void MappingTraits<MachO::dylib_command>::mapping(
239     IO &IO, MachO::dylib_command &LoadCommand) {
240   IO.mapRequired("dylib", LoadCommand.dylib);
241 }
242 
243 void MappingTraits<MachO::dylinker_command>::mapping(
244     IO &IO, MachO::dylinker_command &LoadCommand) {
245 
246   IO.mapRequired("name", LoadCommand.name);
247 }
248 
249 void MappingTraits<MachO::dysymtab_command>::mapping(
250     IO &IO, MachO::dysymtab_command &LoadCommand) {
251 
252   IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
253   IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
254   IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
255   IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
256   IO.mapRequired("iundefsym", LoadCommand.iundefsym);
257   IO.mapRequired("nundefsym", LoadCommand.nundefsym);
258   IO.mapRequired("tocoff", LoadCommand.tocoff);
259   IO.mapRequired("ntoc", LoadCommand.ntoc);
260   IO.mapRequired("modtaboff", LoadCommand.modtaboff);
261   IO.mapRequired("nmodtab", LoadCommand.nmodtab);
262   IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
263   IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
264   IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
265   IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
266   IO.mapRequired("extreloff", LoadCommand.extreloff);
267   IO.mapRequired("nextrel", LoadCommand.nextrel);
268   IO.mapRequired("locreloff", LoadCommand.locreloff);
269   IO.mapRequired("nlocrel", LoadCommand.nlocrel);
270 }
271 
272 void MappingTraits<MachO::encryption_info_command>::mapping(
273     IO &IO, MachO::encryption_info_command &LoadCommand) {
274 
275   IO.mapRequired("cryptoff", LoadCommand.cryptoff);
276   IO.mapRequired("cryptsize", LoadCommand.cryptsize);
277   IO.mapRequired("cryptid", LoadCommand.cryptid);
278 }
279 
280 void MappingTraits<MachO::encryption_info_command_64>::mapping(
281     IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
282 
283   IO.mapRequired("cryptoff", LoadCommand.cryptoff);
284   IO.mapRequired("cryptsize", LoadCommand.cryptsize);
285   IO.mapRequired("cryptid", LoadCommand.cryptid);
286   IO.mapRequired("pad", LoadCommand.pad);
287 }
288 
289 void MappingTraits<MachO::entry_point_command>::mapping(
290     IO &IO, MachO::entry_point_command &LoadCommand) {
291 
292   IO.mapRequired("entryoff", LoadCommand.entryoff);
293   IO.mapRequired("stacksize", LoadCommand.stacksize);
294 }
295 
296 void MappingTraits<MachO::fvmfile_command>::mapping(
297     IO &IO, MachO::fvmfile_command &LoadCommand) {
298 
299   IO.mapRequired("name", LoadCommand.name);
300   IO.mapRequired("header_addr", LoadCommand.header_addr);
301 }
302 
303 void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) {
304   IO.mapRequired("name", FVMLib.name);
305   IO.mapRequired("minor_version", FVMLib.minor_version);
306   IO.mapRequired("header_addr", FVMLib.header_addr);
307 }
308 
309 void MappingTraits<MachO::fvmlib_command>::mapping(
310     IO &IO, MachO::fvmlib_command &LoadCommand) {
311 
312   IO.mapRequired("fvmlib", LoadCommand.fvmlib);
313 }
314 
315 void MappingTraits<MachO::ident_command>::mapping(
316     IO &IO, MachO::ident_command &LoadCommand) {}
317 
318 void MappingTraits<MachO::linkedit_data_command>::mapping(
319     IO &IO, MachO::linkedit_data_command &LoadCommand) {
320 
321   IO.mapRequired("dataoff", LoadCommand.dataoff);
322   IO.mapRequired("datasize", LoadCommand.datasize);
323 }
324 
325 void MappingTraits<MachO::linker_option_command>::mapping(
326     IO &IO, MachO::linker_option_command &LoadCommand) {
327 
328   IO.mapRequired("count", LoadCommand.count);
329 }
330 
331 void MappingTraits<MachO::prebind_cksum_command>::mapping(
332     IO &IO, MachO::prebind_cksum_command &LoadCommand) {
333 
334   IO.mapRequired("cksum", LoadCommand.cksum);
335 }
336 
337 void MappingTraits<MachO::load_command>::mapping(
338     IO &IO, MachO::load_command &LoadCommand) {}
339 
340 void MappingTraits<MachO::prebound_dylib_command>::mapping(
341     IO &IO, MachO::prebound_dylib_command &LoadCommand) {
342 
343   IO.mapRequired("name", LoadCommand.name);
344   IO.mapRequired("nmodules", LoadCommand.nmodules);
345   IO.mapRequired("linked_modules", LoadCommand.linked_modules);
346 }
347 
348 void MappingTraits<MachO::routines_command>::mapping(
349     IO &IO, MachO::routines_command &LoadCommand) {
350 
351   IO.mapRequired("init_address", LoadCommand.init_address);
352   IO.mapRequired("init_module", LoadCommand.init_module);
353   IO.mapRequired("reserved1", LoadCommand.reserved1);
354   IO.mapRequired("reserved2", LoadCommand.reserved2);
355   IO.mapRequired("reserved3", LoadCommand.reserved3);
356   IO.mapRequired("reserved4", LoadCommand.reserved4);
357   IO.mapRequired("reserved5", LoadCommand.reserved5);
358   IO.mapRequired("reserved6", LoadCommand.reserved6);
359 }
360 
361 void MappingTraits<MachO::routines_command_64>::mapping(
362     IO &IO, MachO::routines_command_64 &LoadCommand) {
363 
364   IO.mapRequired("init_address", LoadCommand.init_address);
365   IO.mapRequired("init_module", LoadCommand.init_module);
366   IO.mapRequired("reserved1", LoadCommand.reserved1);
367   IO.mapRequired("reserved2", LoadCommand.reserved2);
368   IO.mapRequired("reserved3", LoadCommand.reserved3);
369   IO.mapRequired("reserved4", LoadCommand.reserved4);
370   IO.mapRequired("reserved5", LoadCommand.reserved5);
371   IO.mapRequired("reserved6", LoadCommand.reserved6);
372 }
373 
374 void MappingTraits<MachO::rpath_command>::mapping(
375     IO &IO, MachO::rpath_command &LoadCommand) {
376 
377   IO.mapRequired("path", LoadCommand.path);
378 }
379 
380 void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) {
381   IO.mapRequired("sectname", Section.sectname);
382   IO.mapRequired("segname", Section.segname);
383   IO.mapRequired("addr", Section.addr);
384   IO.mapRequired("size", Section.size);
385   IO.mapRequired("offset", Section.offset);
386   IO.mapRequired("align", Section.align);
387   IO.mapRequired("reloff", Section.reloff);
388   IO.mapRequired("nreloc", Section.nreloc);
389   IO.mapRequired("flags", Section.flags);
390   IO.mapRequired("reserved1", Section.reserved1);
391   IO.mapRequired("reserved2", Section.reserved2);
392 }
393 
394 void MappingTraits<MachO::section_64>::mapping(IO &IO,
395                                                MachO::section_64 &Section) {
396   IO.mapRequired("sectname", Section.sectname);
397   IO.mapRequired("segname", Section.segname);
398   IO.mapRequired("addr", Section.addr);
399   IO.mapRequired("size", Section.size);
400   IO.mapRequired("offset", Section.offset);
401   IO.mapRequired("align", Section.align);
402   IO.mapRequired("reloff", Section.reloff);
403   IO.mapRequired("nreloc", Section.nreloc);
404   IO.mapRequired("flags", Section.flags);
405   IO.mapRequired("reserved1", Section.reserved1);
406   IO.mapRequired("reserved2", Section.reserved2);
407   IO.mapRequired("reserved3", Section.reserved3);
408 }
409 
410 void MappingTraits<MachO::segment_command>::mapping(
411     IO &IO, MachO::segment_command &LoadCommand) {
412 
413   IO.mapRequired("segname", LoadCommand.segname);
414   IO.mapRequired("vmaddr", LoadCommand.vmaddr);
415   IO.mapRequired("vmsize", LoadCommand.vmsize);
416   IO.mapRequired("fileoff", LoadCommand.fileoff);
417   IO.mapRequired("filesize", LoadCommand.filesize);
418   IO.mapRequired("maxprot", LoadCommand.maxprot);
419   IO.mapRequired("initprot", LoadCommand.initprot);
420   IO.mapRequired("nsects", LoadCommand.nsects);
421   IO.mapRequired("flags", LoadCommand.flags);
422 }
423 
424 void MappingTraits<MachO::segment_command_64>::mapping(
425     IO &IO, MachO::segment_command_64 &LoadCommand) {
426 
427   IO.mapRequired("segname", LoadCommand.segname);
428   IO.mapRequired("vmaddr", LoadCommand.vmaddr);
429   IO.mapRequired("vmsize", LoadCommand.vmsize);
430   IO.mapRequired("fileoff", LoadCommand.fileoff);
431   IO.mapRequired("filesize", LoadCommand.filesize);
432   IO.mapRequired("maxprot", LoadCommand.maxprot);
433   IO.mapRequired("initprot", LoadCommand.initprot);
434   IO.mapRequired("nsects", LoadCommand.nsects);
435   IO.mapRequired("flags", LoadCommand.flags);
436 }
437 
438 void MappingTraits<MachO::source_version_command>::mapping(
439     IO &IO, MachO::source_version_command &LoadCommand) {
440 
441   IO.mapRequired("version", LoadCommand.version);
442 }
443 
444 void MappingTraits<MachO::sub_client_command>::mapping(
445     IO &IO, MachO::sub_client_command &LoadCommand) {
446 
447   IO.mapRequired("client", LoadCommand.client);
448 }
449 
450 void MappingTraits<MachO::sub_framework_command>::mapping(
451     IO &IO, MachO::sub_framework_command &LoadCommand) {
452 
453   IO.mapRequired("umbrella", LoadCommand.umbrella);
454 }
455 
456 void MappingTraits<MachO::sub_library_command>::mapping(
457     IO &IO, MachO::sub_library_command &LoadCommand) {
458 
459   IO.mapRequired("sub_library", LoadCommand.sub_library);
460 }
461 
462 void MappingTraits<MachO::sub_umbrella_command>::mapping(
463     IO &IO, MachO::sub_umbrella_command &LoadCommand) {
464 
465   IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
466 }
467 
468 void MappingTraits<MachO::symseg_command>::mapping(
469     IO &IO, MachO::symseg_command &LoadCommand) {
470 
471   IO.mapRequired("offset", LoadCommand.offset);
472   IO.mapRequired("size", LoadCommand.size);
473 }
474 
475 void MappingTraits<MachO::symtab_command>::mapping(
476     IO &IO, MachO::symtab_command &LoadCommand) {
477 
478   IO.mapRequired("symoff", LoadCommand.symoff);
479   IO.mapRequired("nsyms", LoadCommand.nsyms);
480   IO.mapRequired("stroff", LoadCommand.stroff);
481   IO.mapRequired("strsize", LoadCommand.strsize);
482 }
483 
484 void MappingTraits<MachO::thread_command>::mapping(
485     IO &IO, MachO::thread_command &LoadCommand) {}
486 
487 void MappingTraits<MachO::twolevel_hints_command>::mapping(
488     IO &IO, MachO::twolevel_hints_command &LoadCommand) {
489 
490   IO.mapRequired("offset", LoadCommand.offset);
491   IO.mapRequired("nhints", LoadCommand.nhints);
492 }
493 
494 void MappingTraits<MachO::uuid_command>::mapping(
495     IO &IO, MachO::uuid_command &LoadCommand) {
496 
497   IO.mapRequired("uuid", LoadCommand.uuid);
498 }
499 
500 void MappingTraits<MachO::version_min_command>::mapping(
501     IO &IO, MachO::version_min_command &LoadCommand) {
502 
503   IO.mapRequired("version", LoadCommand.version);
504   IO.mapRequired("sdk", LoadCommand.sdk);
505 }
506 
507 } // namespace llvm::yaml
508 
509 } // namespace llvm
510