llvm.org GIT mirror llvm / 0865cea
Reapply r269782 "[obj2yaml] [yaml2obj] Support for MachO load command structures"" This adds support for all the MachO *_command structures. The load_command payloads still are not represented, but that will come next. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269808 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
7 changed file(s) with 709 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
3434
3535 struct LoadCommand {
3636 virtual ~LoadCommand();
37 MachO::LoadCommandType cmd;
38 uint32_t cmdsize;
37 llvm::MachO::macho_load_command Data;
3938 };
4039
4140 struct Object {
4241 FileHeader Header;
43 std::vector<std::unique_ptr> LoadCommands;
42 std::vector<LoadCommand> LoadCommands;
4443 };
4544
4645 } // namespace llvm::MachOYAML
4746 } // namespace llvm
4847
49 LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr)
48 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
5049
5150 namespace llvm {
5251 namespace yaml {
5958 static void mapping(IO &IO, MachOYAML::Object &Object);
6059 };
6160
62 template <> struct MappingTraits> {
63 static void mapping(IO &IO,
64 std::unique_ptr &LoadCommand);
61 template <> struct MappingTraits {
62 static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
6563 };
6664
67 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
65 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
6866 io.enumCase(value, #LCName, MachO::LCName);
6967
7068 template <> struct ScalarEnumerationTraits {
7371 }
7472 };
7573
76 #undef HANDLE_LOAD_COMMAND
74 // This trait is used for 16-byte chars in Mach structures used for strings
75 typedef char char_16[16];
76
77 template <> struct ScalarTraits {
78 static void output(const char_16 &Val, void *, llvm::raw_ostream &Out);
79
80 static StringRef input(StringRef Scalar, void *, char_16 &Val);
81 static bool mustQuote(StringRef S);
82 };
83
84 // This trait is used for UUIDs. It reads and writes them matching otool's
85 // formatting style.
86 typedef uint8_t uuid_t[16];
87
88 template <> struct ScalarTraits {
89 static void output(const uuid_t &Val, void *, llvm::raw_ostream &Out);
90
91 static StringRef input(StringRef Scalar, void *, uuid_t &Val);
92 static bool mustQuote(StringRef S);
93 };
94
95 // Load Command struct mapping traits
96
97 #define LOAD_COMMAND_STRUCT(LCStruct) \
98 template <> struct MappingTraits { \
99 static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
100 };
101
102 #include "llvm/Support/MachO.def"
103
104 // Extra structures used by load commands
105 template <> struct MappingTraits {
106 static void mapping(IO &IO, MachO::dylib &LoadCommand);
107 };
108
109 template <> struct MappingTraits {
110 static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
111 };
112
113 template <> struct MappingTraits {
114 static void mapping(IO &IO, MachO::section &LoadCommand);
115 };
116
117 template <> struct MappingTraits {
118 static void mapping(IO &IO, MachO::section_64 &LoadCommand);
119 };
77120
78121 } // namespace llvm::yaml
79122
1010 //
1111 //,,,----------------------------------------------------------------------,,,//
1212
13 #ifndef HANDLE_LOAD_COMMAND
14 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)
15 #endif
13 #ifdef HANDLE_LOAD_COMMAND
1614
1715 HANDLE_LOAD_COMMAND(LC_SEGMENT, 0x00000001u, segment_command)
1816 HANDLE_LOAD_COMMAND(LC_SYMTAB, 0x00000002u, symtab_command)
6462 HANDLE_LOAD_COMMAND(LC_LINKER_OPTIMIZATION_HINT, 0x0000002Eu, linkedit_data_command)
6563 HANDLE_LOAD_COMMAND(LC_VERSION_MIN_TVOS, 0x0000002Fu, version_min_command)
6664 HANDLE_LOAD_COMMAND(LC_VERSION_MIN_WATCHOS, 0x00000030u, version_min_command)
65
66 #endif
67
68 #ifdef LOAD_COMMAND_STRUCT
69
70 LOAD_COMMAND_STRUCT(dyld_info_command)
71 LOAD_COMMAND_STRUCT(dylib_command)
72 LOAD_COMMAND_STRUCT(dylinker_command)
73 LOAD_COMMAND_STRUCT(dysymtab_command)
74 LOAD_COMMAND_STRUCT(encryption_info_command)
75 LOAD_COMMAND_STRUCT(encryption_info_command_64)
76 LOAD_COMMAND_STRUCT(entry_point_command)
77 LOAD_COMMAND_STRUCT(fvmfile_command)
78 LOAD_COMMAND_STRUCT(fvmlib_command)
79 LOAD_COMMAND_STRUCT(ident_command)
80 LOAD_COMMAND_STRUCT(linkedit_data_command)
81 LOAD_COMMAND_STRUCT(linker_option_command)
82 LOAD_COMMAND_STRUCT(load_command)
83 LOAD_COMMAND_STRUCT(prebind_cksum_command)
84 LOAD_COMMAND_STRUCT(prebound_dylib_command)
85 LOAD_COMMAND_STRUCT(routines_command)
86 LOAD_COMMAND_STRUCT(routines_command_64)
87 LOAD_COMMAND_STRUCT(rpath_command)
88 LOAD_COMMAND_STRUCT(segment_command)
89 LOAD_COMMAND_STRUCT(segment_command_64)
90 LOAD_COMMAND_STRUCT(source_version_command)
91 LOAD_COMMAND_STRUCT(sub_client_command)
92 LOAD_COMMAND_STRUCT(sub_framework_command)
93 LOAD_COMMAND_STRUCT(sub_library_command)
94 LOAD_COMMAND_STRUCT(sub_umbrella_command)
95 LOAD_COMMAND_STRUCT(symseg_command)
96 LOAD_COMMAND_STRUCT(symtab_command)
97 LOAD_COMMAND_STRUCT(thread_command)
98 LOAD_COMMAND_STRUCT(twolevel_hints_command)
99 LOAD_COMMAND_STRUCT(uuid_command)
100 LOAD_COMMAND_STRUCT(version_min_command)
101
102 #endif
103
104 #undef HANDLE_LOAD_COMMAND
105 #undef LOAD_COMMAND_STRUCT
16761676 const uint32_t x86_EXCEPTION_STATE_COUNT =
16771677 sizeof(x86_exception_state_t) / sizeof(uint32_t);
16781678
1679 // Define a union of all load command structs
1680 #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
1681
1682 union macho_load_command {
1683 #include "llvm/Support/MachO.def"
1684 };
1685
16791686 } // end namespace MachO
16801687 } // end namespace llvm
16811688
1212
1313 #include "llvm/ObjectYAML/MachOYAML.h"
1414 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/Format.h"
16
17 #include // For memcpy and memset.
1518
1619 namespace llvm {
1720
1821 MachOYAML::LoadCommand::~LoadCommand() {}
1922
2023 namespace yaml {
24
25 void ScalarTraits::output(const char_16 &Val, void *,
26 llvm::raw_ostream &Out) {
27 Out << Val;
28 }
29
30 StringRef ScalarTraits::input(StringRef Scalar, void *, char_16 &Val) {
31 size_t CopySize = 16 >= Scalar.size() ? 16 : Scalar.size();
32 memcpy((void *)Val, Scalar.data(), CopySize);
33
34 if (Scalar.size() < 16) {
35 memset((void *)&Val[Scalar.size()], 0, 16 - Scalar.size());
36 }
37
38 return StringRef();
39 }
40
41 bool ScalarTraits::mustQuote(StringRef S) { return needsQuotes(S); }
42
43 void ScalarTraits::output(const uuid_t &Val, void *,
44 llvm::raw_ostream &Out) {
45 for (int Idx = 0; Idx < 16; ++Idx) {
46 Out << format("%02" PRIX32, Val[Idx]);
47 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
48 Out << "-";
49 }
50 }
51
52 StringRef ScalarTraits::input(StringRef Scalar, void *, uuid_t &Val) {
53 size_t OutIdx = 0;
54 for (size_t Idx = 0; Idx < Scalar.size(); ++Idx) {
55 if (Scalar[Idx] == '-' || OutIdx >= 16)
56 continue;
57 unsigned long long TempInt;
58 if (getAsUnsignedInteger(Scalar.slice(Idx, Idx + 2), 16, TempInt))
59 return "invalid number";
60 if (TempInt > 0xFF)
61 return "out of range number";
62 Val[OutIdx] = static_cast(TempInt);
63 ++Idx; // increment idx an extra time because we're consuming 2 chars
64 ++OutIdx;
65 }
66 return StringRef();
67 }
68
69 bool ScalarTraits::mustQuote(StringRef S) { return needsQuotes(S); }
2170
2271 void MappingTraits::mapping(
2372 IO &IO, MachOYAML::FileHeader &FileHdr) {
4594 IO.setContext(nullptr);
4695 }
4796
48 void MappingTraits>::mapping(
49 IO &IO, std::unique_ptr &LoadCommand) {
50 if (!IO.outputting())
51 LoadCommand.reset(new MachOYAML::LoadCommand());
52 IO.mapRequired("cmd", LoadCommand->cmd);
53 IO.mapRequired("cmdsize", LoadCommand->cmdsize);
97 void MappingTraits::mapping(
98 IO &IO, MachOYAML::LoadCommand &LoadCommand) {
99 IO.mapRequired(
100 "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd);
101 IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
102
103 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
104 case MachO::LCName: \
105 MappingTraits::mapping(IO, \
106 LoadCommand.Data.LCStruct##_data); \
107 break;
108
109 switch (LoadCommand.Data.load_command_data.cmd) {
110 #include "llvm/Support/MachO.def"
111 }
112 }
113
114 void MappingTraits::mapping(
115 IO &IO, MachO::dyld_info_command &LoadCommand) {
116 IO.mapRequired("rebase_off", LoadCommand.rebase_off);
117 IO.mapRequired("rebase_size", LoadCommand.rebase_size);
118 IO.mapRequired("bind_off", LoadCommand.bind_size);
119 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
120 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
121 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_size);
122 IO.mapRequired("export_off", LoadCommand.export_off);
123 IO.mapRequired("export_size", LoadCommand.export_size);
124 }
125
126 void MappingTraits::mapping(IO &IO, MachO::dylib &DylibStruct) {
127 IO.mapRequired("name", DylibStruct.name);
128 IO.mapRequired("timestamp", DylibStruct.timestamp);
129 IO.mapRequired("current_version", DylibStruct.current_version);
130 IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
131 }
132
133 void MappingTraits::mapping(
134 IO &IO, MachO::dylib_command &LoadCommand) {
135 IO.mapRequired("dylib", LoadCommand.dylib);
136 }
137
138 void MappingTraits::mapping(
139 IO &IO, MachO::dylinker_command &LoadCommand) {
140
141 IO.mapRequired("name", LoadCommand.name);
142 }
143
144 void MappingTraits::mapping(
145 IO &IO, MachO::dysymtab_command &LoadCommand) {
146
147 IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
148 IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
149 IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
150 IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
151 IO.mapRequired("iundefsym", LoadCommand.iundefsym);
152 IO.mapRequired("nundefsym", LoadCommand.nundefsym);
153 IO.mapRequired("tocoff", LoadCommand.tocoff);
154 IO.mapRequired("ntoc", LoadCommand.ntoc);
155 IO.mapRequired("modtaboff", LoadCommand.modtaboff);
156 IO.mapRequired("nmodtab", LoadCommand.nmodtab);
157 IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
158 IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
159 IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
160 IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
161 IO.mapRequired("extreloff", LoadCommand.extreloff);
162 IO.mapRequired("nextrel", LoadCommand.nextrel);
163 IO.mapRequired("locreloff", LoadCommand.locreloff);
164 IO.mapRequired("nlocrel", LoadCommand.nlocrel);
165 }
166
167 void MappingTraits::mapping(
168 IO &IO, MachO::encryption_info_command &LoadCommand) {
169
170 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
171 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
172 IO.mapRequired("cryptid", LoadCommand.cryptid);
173 }
174
175 void MappingTraits::mapping(
176 IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
177
178 IO.mapRequired("cryptoff", LoadCommand.cryptoff);
179 IO.mapRequired("cryptsize", LoadCommand.cryptsize);
180 IO.mapRequired("cryptid", LoadCommand.cryptid);
181 IO.mapRequired("pad", LoadCommand.pad);
182 }
183
184 void MappingTraits::mapping(
185 IO &IO, MachO::entry_point_command &LoadCommand) {
186
187 IO.mapRequired("entryoff", LoadCommand.entryoff);
188 IO.mapRequired("stacksize", LoadCommand.stacksize);
189 }
190
191 void MappingTraits::mapping(
192 IO &IO, MachO::fvmfile_command &LoadCommand) {
193
194 IO.mapRequired("name", LoadCommand.name);
195 IO.mapRequired("header_addr", LoadCommand.header_addr);
196 }
197
198 void MappingTraits::mapping(IO &IO, MachO::fvmlib &FVMLib) {
199 IO.mapRequired("name", FVMLib.name);
200 IO.mapRequired("minor_version", FVMLib.minor_version);
201 IO.mapRequired("header_addr", FVMLib.header_addr);
202 }
203
204 void MappingTraits::mapping(
205 IO &IO, MachO::fvmlib_command &LoadCommand) {
206
207 IO.mapRequired("fvmlib", LoadCommand.fvmlib);
208 }
209
210 void MappingTraits::mapping(
211 IO &IO, MachO::ident_command &LoadCommand) {}
212
213 void MappingTraits::mapping(
214 IO &IO, MachO::linkedit_data_command &LoadCommand) {
215
216 IO.mapRequired("dataoff", LoadCommand.dataoff);
217 IO.mapRequired("datasize", LoadCommand.datasize);
218 }
219
220 void MappingTraits::mapping(
221 IO &IO, MachO::linker_option_command &LoadCommand) {
222
223 IO.mapRequired("count", LoadCommand.count);
224 }
225
226 void MappingTraits::mapping(
227 IO &IO, MachO::prebind_cksum_command &LoadCommand) {
228
229 IO.mapRequired("cksum", LoadCommand.cksum);
230 }
231
232 void MappingTraits::mapping(
233 IO &IO, MachO::load_command &LoadCommand) {}
234
235 void MappingTraits::mapping(
236 IO &IO, MachO::prebound_dylib_command &LoadCommand) {
237
238 IO.mapRequired("name", LoadCommand.name);
239 IO.mapRequired("nmodules", LoadCommand.nmodules);
240 IO.mapRequired("linked_modules", LoadCommand.linked_modules);
241 }
242
243 void MappingTraits::mapping(
244 IO &IO, MachO::routines_command &LoadCommand) {
245
246 IO.mapRequired("init_address", LoadCommand.init_address);
247 IO.mapRequired("init_module", LoadCommand.init_module);
248 IO.mapRequired("reserved1", LoadCommand.reserved1);
249 IO.mapRequired("reserved2", LoadCommand.reserved2);
250 IO.mapRequired("reserved3", LoadCommand.reserved3);
251 IO.mapRequired("reserved4", LoadCommand.reserved4);
252 IO.mapRequired("reserved5", LoadCommand.reserved5);
253 IO.mapRequired("reserved6", LoadCommand.reserved6);
254 }
255
256 void MappingTraits::mapping(
257 IO &IO, MachO::routines_command_64 &LoadCommand) {
258
259 IO.mapRequired("init_address", LoadCommand.init_address);
260 IO.mapRequired("init_module", LoadCommand.init_module);
261 IO.mapRequired("reserved1", LoadCommand.reserved1);
262 IO.mapRequired("reserved2", LoadCommand.reserved2);
263 IO.mapRequired("reserved3", LoadCommand.reserved3);
264 IO.mapRequired("reserved4", LoadCommand.reserved4);
265 IO.mapRequired("reserved5", LoadCommand.reserved5);
266 IO.mapRequired("reserved6", LoadCommand.reserved6);
267 }
268
269 void MappingTraits::mapping(
270 IO &IO, MachO::rpath_command &LoadCommand) {
271
272 IO.mapRequired("path", LoadCommand.path);
273 }
274
275 void MappingTraits::mapping(IO &IO, MachO::section &Section) {
276 IO.mapRequired("sectname", Section.sectname);
277 IO.mapRequired("segname", Section.segname);
278 IO.mapRequired("addr", Section.addr);
279 IO.mapRequired("size", Section.size);
280 IO.mapRequired("offset", Section.offset);
281 IO.mapRequired("align", Section.align);
282 IO.mapRequired("reloff", Section.reloff);
283 IO.mapRequired("nreloc", Section.nreloc);
284 IO.mapRequired("flags", Section.flags);
285 IO.mapRequired("reserved1", Section.reserved1);
286 IO.mapRequired("reserved2", Section.reserved2);
287 }
288
289 void MappingTraits::mapping(IO &IO,
290 MachO::section_64 &Section) {
291 IO.mapRequired("sectname", Section.sectname);
292 IO.mapRequired("segname", Section.segname);
293 IO.mapRequired("addr", Section.addr);
294 IO.mapRequired("size", Section.size);
295 IO.mapRequired("offset", Section.offset);
296 IO.mapRequired("align", Section.align);
297 IO.mapRequired("reloff", Section.reloff);
298 IO.mapRequired("nreloc", Section.nreloc);
299 IO.mapRequired("flags", Section.flags);
300 IO.mapRequired("reserved1", Section.reserved1);
301 IO.mapRequired("reserved2", Section.reserved2);
302 IO.mapRequired("reserved3", Section.reserved3);
303 }
304
305 void MappingTraits::mapping(
306 IO &IO, MachO::segment_command &LoadCommand) {
307
308 IO.mapRequired("segname", LoadCommand.segname);
309 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
310 IO.mapRequired("vmsize", LoadCommand.vmsize);
311 IO.mapRequired("fileoff", LoadCommand.fileoff);
312 IO.mapRequired("filesize", LoadCommand.filesize);
313 IO.mapRequired("maxprot", LoadCommand.maxprot);
314 IO.mapRequired("initprot", LoadCommand.initprot);
315 IO.mapRequired("nsects", LoadCommand.nsects);
316 IO.mapRequired("flags", LoadCommand.flags);
317 }
318
319 void MappingTraits::mapping(
320 IO &IO, MachO::segment_command_64 &LoadCommand) {
321
322 IO.mapRequired("segname", LoadCommand.segname);
323 IO.mapRequired("vmaddr", LoadCommand.vmaddr);
324 IO.mapRequired("vmsize", LoadCommand.vmsize);
325 IO.mapRequired("fileoff", LoadCommand.fileoff);
326 IO.mapRequired("filesize", LoadCommand.filesize);
327 IO.mapRequired("maxprot", LoadCommand.maxprot);
328 IO.mapRequired("initprot", LoadCommand.initprot);
329 IO.mapRequired("nsects", LoadCommand.nsects);
330 IO.mapRequired("flags", LoadCommand.flags);
331 }
332
333 void MappingTraits::mapping(
334 IO &IO, MachO::source_version_command &LoadCommand) {
335
336 IO.mapRequired("version", LoadCommand.version);
337 }
338
339 void MappingTraits::mapping(
340 IO &IO, MachO::sub_client_command &LoadCommand) {
341
342 IO.mapRequired("client", LoadCommand.client);
343 }
344
345 void MappingTraits::mapping(
346 IO &IO, MachO::sub_framework_command &LoadCommand) {
347
348 IO.mapRequired("umbrella", LoadCommand.umbrella);
349 }
350
351 void MappingTraits::mapping(
352 IO &IO, MachO::sub_library_command &LoadCommand) {
353
354 IO.mapRequired("sub_library", LoadCommand.sub_library);
355 }
356
357 void MappingTraits::mapping(
358 IO &IO, MachO::sub_umbrella_command &LoadCommand) {
359
360 IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
361 }
362
363 void MappingTraits::mapping(
364 IO &IO, MachO::symseg_command &LoadCommand) {
365
366 IO.mapRequired("offset", LoadCommand.offset);
367 IO.mapRequired("size", LoadCommand.size);
368 }
369
370 void MappingTraits::mapping(
371 IO &IO, MachO::symtab_command &LoadCommand) {
372
373 IO.mapRequired("symoff", LoadCommand.symoff);
374 IO.mapRequired("nsyms", LoadCommand.nsyms);
375 IO.mapRequired("stroff", LoadCommand.stroff);
376 IO.mapRequired("strsize", LoadCommand.strsize);
377 }
378
379 void MappingTraits::mapping(
380 IO &IO, MachO::thread_command &LoadCommand) {}
381
382 void MappingTraits::mapping(
383 IO &IO, MachO::twolevel_hints_command &LoadCommand) {
384
385 IO.mapRequired("offset", LoadCommand.offset);
386 IO.mapRequired("nhints", LoadCommand.nhints);
387 }
388
389 void MappingTraits::mapping(
390 IO &IO, MachO::uuid_command &LoadCommand) {
391
392 IO.mapRequired("cmdsize", LoadCommand.cmdsize);
393 IO.mapRequired("uuid", LoadCommand.uuid);
394 }
395
396 void MappingTraits::mapping(
397 IO &IO, MachO::version_min_command &LoadCommand) {
398
399 IO.mapRequired("version", LoadCommand.version);
400 IO.mapRequired("sdk", LoadCommand.sdk);
54401 }
55402
56403 } // namespace llvm::yaml
1212 LoadCommands:
1313 - cmd: LC_SEGMENT_64
1414 cmdsize: 72
15 segname: __PAGEZERO
16 vmaddr: 0
17 vmsize: 4294967296
18 fileoff: 0
19 filesize: 0
20 maxprot: 0
21 initprot: 0
22 nsects: 0
23 flags: 0
1524 - cmd: LC_SEGMENT_64
1625 cmdsize: 552
26 segname: __TEXT
27 vmaddr: 4294967296
28 vmsize: 8192
29 fileoff: 0
30 filesize: 8192
31 maxprot: 7
32 initprot: 5
33 nsects: 6
34 flags: 0
1735 - cmd: LC_SEGMENT_64
1836 cmdsize: 312
37 segname: __DATA
38 vmaddr: 4294975488
39 vmsize: 4096
40 fileoff: 8192
41 filesize: 4096
42 maxprot: 7
43 initprot: 3
44 nsects: 3
45 flags: 0
1946 - cmd: LC_SEGMENT_64
2047 cmdsize: 72
48 segname: __LINKEDIT
49 vmaddr: 4294979584
50 vmsize: 4096
51 fileoff: 12288
52 filesize: 2508
53 maxprot: 7
54 initprot: 1
55 nsects: 0
56 flags: 0
2157 - cmd: LC_DYLD_INFO_ONLY
2258 cmdsize: 48
59 rebase_off: 12288
60 rebase_size: 8
61 bind_off: 96
62 weak_bind_off: 0
63 weak_bind_size: 0
64 lazy_bind_off: 624
65 export_off: 13016
66 export_size: 48
2367 - cmd: LC_SYMTAB
2468 cmdsize: 24
69 symoff: 13080
70 nsyms: 30
71 stroff: 13700
72 strsize: 1096
2573 - cmd: LC_DYSYMTAB
2674 cmdsize: 80
75 ilocalsym: 0
76 nlocalsym: 9
77 iextdefsym: 9
78 nextdefsym: 2
79 iundefsym: 11
80 nundefsym: 19
81 tocoff: 0
82 ntoc: 0
83 modtaboff: 0
84 nmodtab: 0
85 extrefsymoff: 0
86 nextrefsyms: 0
87 indirectsymoff: 13560
88 nindirectsyms: 35
89 extreloff: 0
90 nextrel: 0
91 locreloff: 0
92 nlocrel: 0
2793 - cmd: LC_LOAD_DYLINKER
2894 cmdsize: 32
95 name: 12
2996 - cmd: LC_UUID
3097 cmdsize: 24
98 cmdsize: 24
99 uuid: 461A1B28-822F-3F38-B670-645419E636F5
31100 - cmd: LC_VERSION_MIN_MACOSX
32101 cmdsize: 16
102 version: 658176
103 sdk: 658176
33104 - cmd: LC_SOURCE_VERSION
34105 cmdsize: 16
106 version: 0
35107 - cmd: LC_MAIN
36108 cmdsize: 24
109 entryoff: 4448
110 stacksize: 0
37111 - cmd: LC_LOAD_DYLIB
38112 cmdsize: 48
113 dylib:
114 name: 24
115 timestamp: 2
116 current_version: 7864576
117 compatibility_version: 65536
39118 - cmd: LC_LOAD_DYLIB
40119 cmdsize: 56
120 dylib:
121 name: 24
122 timestamp: 2
123 current_version: 80349697
124 compatibility_version: 65536
41125 - cmd: LC_FUNCTION_STARTS
42126 cmdsize: 16
127 dataoff: 13064
128 datasize: 16
43129 - cmd: LC_DATA_IN_CODE
44130 cmdsize: 16
131 dataoff: 13080
132 datasize: 0
45133 ...
46134
47135
48 # CHECK: LoadCommands:
49 # CHECK: - cmd: LC_SEGMENT_64
50 # CHECK: cmdsize: 72
51 # CHECK: - cmd: LC_SEGMENT_64
52 # CHECK: cmdsize: 552
53 # CHECK: - cmd: LC_SEGMENT_64
54 # CHECK: cmdsize: 312
55 # CHECK: - cmd: LC_SEGMENT_64
56 # CHECK: cmdsize: 72
57 # CHECK: - cmd: LC_DYLD_INFO_ONLY
58 # CHECK: cmdsize: 48
59 # CHECK: - cmd: LC_SYMTAB
60 # CHECK: cmdsize: 24
61 # CHECK: - cmd: LC_DYSYMTAB
62 # CHECK: cmdsize: 80
63 # CHECK: - cmd: LC_LOAD_DYLINKER
64 # CHECK: cmdsize: 32
65 # CHECK: - cmd: LC_UUID
66 # CHECK: cmdsize: 24
67 # CHECK: - cmd: LC_VERSION_MIN_MACOSX
68 # CHECK: cmdsize: 16
69 # CHECK: - cmd: LC_SOURCE_VERSION
70 # CHECK: cmdsize: 16
71 # CHECK: - cmd: LC_MAIN
72 # CHECK: cmdsize: 24
73 # CHECK: - cmd: LC_LOAD_DYLIB
74 # CHECK: cmdsize: 48
75 # CHECK: - cmd: LC_LOAD_DYLIB
76 # CHECK: cmdsize: 56
77 # CHECK: - cmd: LC_FUNCTION_STARTS
78 # CHECK: cmdsize: 16
79 # CHECK: - cmd: LC_DATA_IN_CODE
80 # CHECK: cmdsize: 16
136
137 #CHECK: LoadCommands:
138 #CHECK: - cmd: LC_SEGMENT_64
139 #CHECK: cmdsize: 72
140 #CHECK: segname: __PAGEZERO
141 #CHECK: vmaddr: 0
142 #CHECK: vmsize: 4294967296
143 #CHECK: fileoff: 0
144 #CHECK: filesize: 0
145 #CHECK: maxprot: 0
146 #CHECK: initprot: 0
147 #CHECK: nsects: 0
148 #CHECK: flags: 0
149 #CHECK: - cmd: LC_SEGMENT_64
150 #CHECK: cmdsize: 552
151 #CHECK: segname: __TEXT
152 #CHECK: vmaddr: 4294967296
153 #CHECK: vmsize: 8192
154 #CHECK: fileoff: 0
155 #CHECK: filesize: 8192
156 #CHECK: maxprot: 7
157 #CHECK: initprot: 5
158 #CHECK: nsects: 6
159 #CHECK: flags: 0
160 #CHECK: - cmd: LC_SEGMENT_64
161 #CHECK: cmdsize: 312
162 #CHECK: segname: __DATA
163 #CHECK: vmaddr: 4294975488
164 #CHECK: vmsize: 4096
165 #CHECK: fileoff: 8192
166 #CHECK: filesize: 4096
167 #CHECK: maxprot: 7
168 #CHECK: initprot: 3
169 #CHECK: nsects: 3
170 #CHECK: flags: 0
171 #CHECK: - cmd: LC_SEGMENT_64
172 #CHECK: cmdsize: 72
173 #CHECK: segname: __LINKEDIT
174 #CHECK: vmaddr: 4294979584
175 #CHECK: vmsize: 4096
176 #CHECK: fileoff: 12288
177 #CHECK: filesize: 2508
178 #CHECK: maxprot: 7
179 #CHECK: initprot: 1
180 #CHECK: nsects: 0
181 #CHECK: flags: 0
182 #CHECK: - cmd: LC_DYLD_INFO_ONLY
183 #CHECK: cmdsize: 48
184 #CHECK: rebase_off: 12288
185 #CHECK: rebase_size: 8
186 #CHECK: bind_off: 96
187 #CHECK: weak_bind_off: 0
188 #CHECK: weak_bind_size: 0
189 #CHECK: lazy_bind_off: 624
190 #CHECK: export_off: 13016
191 #CHECK: export_size: 48
192 #CHECK: - cmd: LC_SYMTAB
193 #CHECK: cmdsize: 24
194 #CHECK: symoff: 13080
195 #CHECK: nsyms: 30
196 #CHECK: stroff: 13700
197 #CHECK: strsize: 1096
198 #CHECK: - cmd: LC_DYSYMTAB
199 #CHECK: cmdsize: 80
200 #CHECK: ilocalsym: 0
201 #CHECK: nlocalsym: 9
202 #CHECK: iextdefsym: 9
203 #CHECK: nextdefsym: 2
204 #CHECK: iundefsym: 11
205 #CHECK: nundefsym: 19
206 #CHECK: tocoff: 0
207 #CHECK: ntoc: 0
208 #CHECK: modtaboff: 0
209 #CHECK: nmodtab: 0
210 #CHECK: extrefsymoff: 0
211 #CHECK: nextrefsyms: 0
212 #CHECK: indirectsymoff: 13560
213 #CHECK: nindirectsyms: 35
214 #CHECK: extreloff: 0
215 #CHECK: nextrel: 0
216 #CHECK: locreloff: 0
217 #CHECK: nlocrel: 0
218 #CHECK: - cmd: LC_LOAD_DYLINKER
219 #CHECK: cmdsize: 32
220 #CHECK: name: 12
221 #CHECK: - cmd: LC_UUID
222 #CHECK: cmdsize: 24
223 #CHECK: cmdsize: 24
224 #CHECK: uuid: 461A1B28-822F-3F38-B670-645419E636F5
225 #CHECK: - cmd: LC_VERSION_MIN_MACOSX
226 #CHECK: cmdsize: 16
227 #CHECK: version: 658176
228 #CHECK: sdk: 658176
229 #CHECK: - cmd: LC_SOURCE_VERSION
230 #CHECK: cmdsize: 16
231 #CHECK: version: 0
232 #CHECK: - cmd: LC_MAIN
233 #CHECK: cmdsize: 24
234 #CHECK: entryoff: 4448
235 #CHECK: stacksize: 0
236 #CHECK: - cmd: LC_LOAD_DYLIB
237 #CHECK: cmdsize: 48
238 #CHECK: dylib:
239 #CHECK: name: 24
240 #CHECK: timestamp: 2
241 #CHECK: current_version: 7864576
242 #CHECK: compatibility_version: 65536
243 #CHECK: - cmd: LC_LOAD_DYLIB
244 #CHECK: cmdsize: 56
245 #CHECK: dylib:
246 #CHECK: name: 24
247 #CHECK: timestamp: 2
248 #CHECK: current_version: 80349697
249 #CHECK: compatibility_version: 65536
250 #CHECK: - cmd: LC_FUNCTION_STARTS
251 #CHECK: cmdsize: 16
252 #CHECK: dataoff: 13064
253 #CHECK: datasize: 16
254 #CHECK: - cmd: LC_DATA_IN_CODE
255 #CHECK: cmdsize: 16
256 #CHECK: dataoff: 13080
257 #CHECK: datasize: 0
88
99 #include "Error.h"
1010 #include "obj2yaml.h"
11 #include "llvm/Support/ErrorHandling.h"
1211 #include "llvm/Object/MachOUniversal.h"
1312 #include "llvm/ObjectYAML/MachOYAML.h"
13 #include "llvm/Support/ErrorHandling.h"
1414
1515 using namespace llvm;
1616
2323 Expected> dump();
2424 };
2525
26 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
27 case MachO::LCName: \
28 memcpy((void *) & (LC.Data.LCStruct##_data), LoadCmd.Ptr, \
29 sizeof(MachO::LCStruct)); \
30 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
31 MachO::swapStruct(LC.Data.LCStruct##_data); \
32 break;
33
2634 Expected> MachODumper::dump() {
2735 auto Y = make_unique();
2836 Y->Header.magic = Obj.getHeader().magic;
3442 Y->Header.flags = Obj.getHeader().flags;
3543 Y->Header.reserved = 0;
3644
37 for (auto load_command : Obj.load_commands()) {
38 auto LC = make_unique();
39 LC->cmd = static_cast(load_command.C.cmd);
40 LC->cmdsize = load_command.C.cmdsize;
45 for (auto LoadCmd : Obj.load_commands()) {
46 MachOYAML::LoadCommand LC;
47 switch (LoadCmd.C.cmd) {
48 default:
49 memcpy((void *)&(LC.Data.load_command_data), LoadCmd.Ptr,
50 sizeof(MachO::load_command));
51 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
52 MachO::swapStruct(LC.Data.load_command_data);
53 break;
54 #include "llvm/Support/MachO.def"
55 }
4156 Y->LoadCommands.push_back(std::move(LC));
4257 }
4358
7878
7979 Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
8080 for (auto &LC : Obj.LoadCommands) {
81 MachO::load_command LCTemp;
82 LCTemp.cmd = LC->cmd;
83 LCTemp.cmdsize = LC->cmdsize;
84 OS.write(reinterpret_cast(&LCTemp),
85 sizeof(MachO::load_command));
86 auto remaining_size = LC->cmdsize - sizeof(MachO::load_command);
87 if (remaining_size > 0) {
81 size_t BytesWritten = 0;
82 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
83 case MachO::LCName: \
84 OS.write(reinterpret_cast(&(LC.Data.LCStruct##_data)), \
85 sizeof(MachO::LCStruct)); \
86 BytesWritten = sizeof(MachO::LCStruct); \
87 break;
88
89 switch (LC.Data.load_command_data.cmd) {
90 default:
91 OS.write(reinterpret_cast(&(LC.Data.load_command_data)),
92 sizeof(MachO::load_command));
93 BytesWritten = sizeof(MachO::load_command);
94 break;
95 #include "llvm/Support/MachO.def"
96 }
97
98 auto BytesRemaining =
99 LC.Data.load_command_data.cmdsize - BytesWritten;
100 if (BytesRemaining > 0) {
88101 // TODO: Replace all this once the load command data is present in yaml.
89 std::vector fill_data;
90 fill_data.insert(fill_data.begin(), remaining_size, 0);
91 OS.write(fill_data.data(), remaining_size);
102 // For now I fill with 0xDEADBEEF because it is easy to spot on a hex
103 // viewer.
104 std::vector FillData;
105 FillData.insert(FillData.begin(), BytesRemaining / 4 + 1, 0xDEADBEEFu);
106 OS.write(reinterpret_cast(FillData.data()), BytesRemaining);
92107 }
93108 }
94109 return Error::success();