forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml2macho.cpp
539 lines (472 loc) · 17 KB
/
yaml2macho.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//===- yaml2macho - Convert YAML to a Mach object file --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief The Mach component of yaml2obj.
///
//===----------------------------------------------------------------------===//
#include "yaml2obj.h"
#include "llvm/ObjectYAML/ObjectYAML.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MachO.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Format.h"
using namespace llvm;
namespace {
class MachOWriter {
public:
MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), is64Bit(true), fileStart(0) {
is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 ||
Obj.Header.magic == MachO::MH_CIGAM_64;
memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64));
}
Error writeMachO(raw_ostream &OS);
private:
Error writeHeader(raw_ostream &OS);
Error writeLoadCommands(raw_ostream &OS);
Error writeSectionData(raw_ostream &OS);
Error writeLinkEditData(raw_ostream &OS);
void writeBindOpcodes(raw_ostream &OS,
std::vector<MachOYAML::BindOpcode> &BindOpcodes);
// LinkEdit writers
Error writeRebaseOpcodes(raw_ostream &OS);
Error writeBasicBindOpcodes(raw_ostream &OS);
Error writeWeakBindOpcodes(raw_ostream &OS);
Error writeLazyBindOpcodes(raw_ostream &OS);
Error writeNameList(raw_ostream &OS);
Error writeStringTable(raw_ostream &OS);
Error writeExportTrie(raw_ostream &OS);
void dumpExportEntry(raw_ostream &OS, MachOYAML::ExportEntry &Entry);
void ZeroToOffset(raw_ostream &OS, size_t offset);
MachOYAML::Object &Obj;
bool is64Bit;
uint64_t fileStart;
MachO::mach_header_64 Header;
};
Error MachOWriter::writeMachO(raw_ostream &OS) {
fileStart = OS.tell();
if (auto Err = writeHeader(OS))
return Err;
if (auto Err = writeLoadCommands(OS))
return Err;
if (auto Err = writeSectionData(OS))
return Err;
return Error::success();
}
Error MachOWriter::writeHeader(raw_ostream &OS) {
Header.magic = Obj.Header.magic;
Header.cputype = Obj.Header.cputype;
Header.cpusubtype = Obj.Header.cpusubtype;
Header.filetype = Obj.Header.filetype;
Header.ncmds = Obj.Header.ncmds;
Header.sizeofcmds = Obj.Header.sizeofcmds;
Header.flags = Obj.Header.flags;
Header.reserved = Obj.Header.reserved;
auto header_size =
is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
OS.write((const char *)&Header, header_size);
return Error::success();
}
template <typename SectionType>
SectionType constructSection(MachOYAML::Section Sec) {
SectionType TempSec;
memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16);
memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16);
TempSec.addr = Sec.addr;
TempSec.size = Sec.size;
TempSec.offset = Sec.offset;
TempSec.align = Sec.align;
TempSec.reloff = Sec.reloff;
TempSec.nreloc = Sec.nreloc;
TempSec.flags = Sec.flags;
TempSec.reserved1 = Sec.reserved1;
TempSec.reserved2 = Sec.reserved2;
return TempSec;
}
template <typename StructType>
size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
return 0;
}
template <>
size_t writeLoadCommandData<MachO::segment_command>(MachOYAML::LoadCommand &LC,
raw_ostream &OS) {
size_t BytesWritten = 0;
for (const auto &Sec : LC.Sections) {
auto TempSec = constructSection<MachO::section>(Sec);
OS.write(reinterpret_cast<const char *>(&(TempSec)),
sizeof(MachO::section));
BytesWritten += sizeof(MachO::section);
}
return BytesWritten;
}
template <>
size_t
writeLoadCommandData<MachO::segment_command_64>(MachOYAML::LoadCommand &LC,
raw_ostream &OS) {
size_t BytesWritten = 0;
for (const auto &Sec : LC.Sections) {
auto TempSec = constructSection<MachO::section_64>(Sec);
TempSec.reserved3 = Sec.reserved3;
OS.write(reinterpret_cast<const char *>(&(TempSec)),
sizeof(MachO::section_64));
BytesWritten += sizeof(MachO::section_64);
}
return BytesWritten;
}
size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
size_t BytesWritten = 0;
if (!LC.PayloadString.empty()) {
OS.write(LC.PayloadString.c_str(), LC.PayloadString.length());
BytesWritten = LC.PayloadString.length();
}
return BytesWritten;
}
template <>
size_t writeLoadCommandData<MachO::dylib_command>(MachOYAML::LoadCommand &LC,
raw_ostream &OS) {
return writePayloadString(LC, OS);
}
template <>
size_t writeLoadCommandData<MachO::dylinker_command>(MachOYAML::LoadCommand &LC,
raw_ostream &OS) {
return writePayloadString(LC, OS);
}
template <>
size_t writeLoadCommandData<MachO::rpath_command>(MachOYAML::LoadCommand &LC,
raw_ostream &OS) {
return writePayloadString(LC, OS);
}
void ZeroFillBytes(raw_ostream &OS, size_t Size) {
std::vector<uint8_t> FillData;
FillData.insert(FillData.begin(), Size, 0);
OS.write(reinterpret_cast<char *>(FillData.data()), Size);
}
void Fill(raw_ostream &OS, size_t Size, uint32_t Data) {
std::vector<uint32_t> FillData;
FillData.insert(FillData.begin(), (Size / 4) + 1, Data);
OS.write(reinterpret_cast<char *>(FillData.data()), Size);
}
void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
auto currOffset = OS.tell() - fileStart;
if (currOffset < Offset)
ZeroFillBytes(OS, Offset - currOffset);
}
Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
for (auto &LC : Obj.LoadCommands) {
size_t BytesWritten = 0;
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
case MachO::LCName: \
OS.write(reinterpret_cast<const char *>(&(LC.Data.LCStruct##_data)), \
sizeof(MachO::LCStruct)); \
BytesWritten = sizeof(MachO::LCStruct); \
BytesWritten += writeLoadCommandData<MachO::LCStruct>(LC, OS); \
break;
switch (LC.Data.load_command_data.cmd) {
default:
OS.write(reinterpret_cast<const char *>(&(LC.Data.load_command_data)),
sizeof(MachO::load_command));
BytesWritten = sizeof(MachO::load_command);
BytesWritten += writeLoadCommandData<MachO::load_command>(LC, OS);
break;
#include "llvm/Support/MachO.def"
}
if (LC.PayloadBytes.size() > 0) {
OS.write(reinterpret_cast<const char *>(LC.PayloadBytes.data()),
LC.PayloadBytes.size());
BytesWritten += LC.PayloadBytes.size();
}
if (LC.ZeroPadBytes > 0) {
ZeroFillBytes(OS, LC.ZeroPadBytes);
BytesWritten += LC.ZeroPadBytes;
}
// Fill remaining bytes with 0. This will only get hit in partially
// specified test cases.
auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten;
if (BytesRemaining > 0) {
ZeroFillBytes(OS, BytesRemaining);
}
}
return Error::success();
}
Error MachOWriter::writeSectionData(raw_ostream &OS) {
for (auto &LC : Obj.LoadCommands) {
switch (LC.Data.load_command_data.cmd) {
case MachO::LC_SEGMENT:
case MachO::LC_SEGMENT_64:
auto currOffset = OS.tell() - fileStart;
auto segname = LC.Data.segment_command_data.segname;
uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff
: LC.Data.segment_command_data.fileoff;
if (0 == strncmp(&segname[0], "__LINKEDIT", 16)) {
if (auto Err = writeLinkEditData(OS))
return Err;
} else {
// Zero Fill any data between the end of the last thing we wrote and the
// start of this section.
if (currOffset < segOff) {
ZeroFillBytes(OS, segOff - currOffset);
}
for (auto &Sec : LC.Sections) {
// Zero Fill any data between the end of the last thing we wrote and
// the
// start of this section.
assert(
OS.tell() - fileStart <= Sec.offset &&
"Wrote too much data somewhere, section offsets don't line up.");
currOffset = OS.tell() - fileStart;
if (currOffset < Sec.offset) {
ZeroFillBytes(OS, Sec.offset - currOffset);
}
// Fills section data with 0xDEADBEEF
Fill(OS, Sec.size, 0xDEADBEEFu);
}
}
uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize
: LC.Data.segment_command_data.filesize;
ZeroToOffset(OS, segOff + segSize);
break;
}
}
return Error::success();
}
void MachOWriter::writeBindOpcodes(
raw_ostream &OS, std::vector<MachOYAML::BindOpcode> &BindOpcodes) {
for (auto Opcode : BindOpcodes) {
uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
OS.write(reinterpret_cast<char *>(&OpByte), 1);
for (auto Data : Opcode.ULEBExtraData) {
encodeULEB128(Data, OS);
}
for (auto Data : Opcode.SLEBExtraData) {
encodeSLEB128(Data, OS);
}
if (!Opcode.Symbol.empty()) {
OS.write(Opcode.Symbol.data(), Opcode.Symbol.size());
OS.write('\0');
}
}
}
void MachOWriter::dumpExportEntry(raw_ostream &OS,
MachOYAML::ExportEntry &Entry) {
encodeSLEB128(Entry.TerminalSize, OS);
if (Entry.TerminalSize > 0) {
encodeSLEB128(Entry.Flags, OS);
if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
encodeSLEB128(Entry.Other, OS);
OS << Entry.ImportName;
OS.write('\0');
} else {
encodeSLEB128(Entry.Address, OS);
if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
encodeSLEB128(Entry.Other, OS);
}
}
OS.write(static_cast<uint8_t>(Entry.Children.size()));
for (auto EE : Entry.Children) {
OS << EE.Name;
OS.write('\0');
encodeSLEB128(EE.NodeOffset, OS);
}
for (auto EE : Entry.Children)
dumpExportEntry(OS, EE);
}
Error MachOWriter::writeExportTrie(raw_ostream &OS) {
dumpExportEntry(OS, Obj.LinkEdit.ExportTrie);
return Error::success();
}
template <typename NListType>
void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS) {
NListType ListEntry;
ListEntry.n_strx = NLE.n_strx;
ListEntry.n_type = NLE.n_type;
ListEntry.n_sect = NLE.n_sect;
ListEntry.n_desc = NLE.n_desc;
ListEntry.n_value = NLE.n_value;
OS.write(reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
}
Error MachOWriter::writeLinkEditData(raw_ostream &OS) {
typedef Error (MachOWriter::*writeHandler)(raw_ostream &);
typedef std::pair<uint64_t, writeHandler> writeOperation;
std::vector<writeOperation> WriteQueue;
MachO::dyld_info_command *DyldInfoOnlyCmd = 0;
MachO::symtab_command *SymtabCmd = 0;
for (auto &LC : Obj.LoadCommands) {
switch (LC.Data.load_command_data.cmd) {
case MachO::LC_SYMTAB:
SymtabCmd = &LC.Data.symtab_command_data;
WriteQueue.push_back(
std::make_pair(SymtabCmd->symoff, &MachOWriter::writeNameList));
WriteQueue.push_back(
std::make_pair(SymtabCmd->stroff, &MachOWriter::writeStringTable));
break;
case MachO::LC_DYLD_INFO_ONLY:
DyldInfoOnlyCmd = &LC.Data.dyld_info_command_data;
WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->rebase_off,
&MachOWriter::writeRebaseOpcodes));
WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->bind_off,
&MachOWriter::writeBasicBindOpcodes));
WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->weak_bind_off,
&MachOWriter::writeWeakBindOpcodes));
WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->lazy_bind_off,
&MachOWriter::writeLazyBindOpcodes));
WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->export_off,
&MachOWriter::writeExportTrie));
break;
}
}
std::sort(WriteQueue.begin(), WriteQueue.end(),
[](const writeOperation &a, const writeOperation &b) {
return a.first < b.first;
});
for (auto writeOp : WriteQueue) {
ZeroToOffset(OS, writeOp.first);
if (auto Err = (this->*writeOp.second)(OS))
return Err;
}
return Error::success();
}
Error MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
for (auto Opcode : LinkEdit.RebaseOpcodes) {
uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
OS.write(reinterpret_cast<char *>(&OpByte), 1);
for (auto Data : Opcode.ExtraData) {
encodeULEB128(Data, OS);
}
}
return Error::success();
}
Error MachOWriter::writeBasicBindOpcodes(raw_ostream &OS) {
writeBindOpcodes(OS, Obj.LinkEdit.BindOpcodes);
return Error::success();
}
Error MachOWriter::writeWeakBindOpcodes(raw_ostream &OS) {
writeBindOpcodes(OS, Obj.LinkEdit.WeakBindOpcodes);
return Error::success();
}
Error MachOWriter::writeLazyBindOpcodes(raw_ostream &OS) {
writeBindOpcodes(OS, Obj.LinkEdit.LazyBindOpcodes);
return Error::success();
}
Error MachOWriter::writeNameList(raw_ostream &OS) {
for (auto NLE : Obj.LinkEdit.NameList) {
if (is64Bit)
writeNListEntry<MachO::nlist_64>(NLE, OS);
else
writeNListEntry<MachO::nlist>(NLE, OS);
}
return Error::success();
}
Error MachOWriter::writeStringTable(raw_ostream &OS) {
for (auto Str : Obj.LinkEdit.StringTable) {
OS.write(Str.data(), Str.size());
OS.write('\0');
}
return Error::success();
}
class UniversalWriter {
public:
UniversalWriter(yaml::YamlObjectFile &ObjectFile)
: ObjectFile(ObjectFile), fileStart(0) {}
Error writeMachO(raw_ostream &OS);
private:
Error writeFatHeader(raw_ostream &OS);
Error writeFatArchs(raw_ostream &OS);
void ZeroToOffset(raw_ostream &OS, size_t offset);
yaml::YamlObjectFile &ObjectFile;
uint64_t fileStart;
};
Error UniversalWriter::writeMachO(raw_ostream &OS) {
fileStart = OS.tell();
if (ObjectFile.MachO) {
MachOWriter Writer(*ObjectFile.MachO);
return Writer.writeMachO(OS);
}
if (auto Err = writeFatHeader(OS))
return Err;
if (auto Err = writeFatArchs(OS))
return Err;
auto &FatFile = *ObjectFile.FatMachO;
assert(FatFile.FatArchs.size() == FatFile.Slices.size());
for (size_t i = 0; i < FatFile.Slices.size(); i++) {
ZeroToOffset(OS, FatFile.FatArchs[i].offset);
MachOWriter Writer(FatFile.Slices[i]);
if (auto Err = Writer.writeMachO(OS))
return Err;
auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size;
ZeroToOffset(OS, SliceEnd);
}
return Error::success();
}
Error UniversalWriter::writeFatHeader(raw_ostream &OS) {
auto &FatFile = *ObjectFile.FatMachO;
MachO::fat_header header;
header.magic = FatFile.Header.magic;
header.nfat_arch = FatFile.Header.nfat_arch;
if (sys::IsLittleEndianHost)
swapStruct(header);
OS.write(reinterpret_cast<const char *>(&header), sizeof(MachO::fat_header));
return Error::success();
}
template <typename FatArchType>
FatArchType constructFatArch(MachOYAML::FatArch &Arch) {
FatArchType FatArch;
FatArch.cputype = Arch.cputype;
FatArch.cpusubtype = Arch.cpusubtype;
FatArch.offset = Arch.offset;
FatArch.size = Arch.size;
FatArch.align = Arch.align;
return FatArch;
}
template <typename StructType>
void writeFatArch(MachOYAML::FatArch &LC, raw_ostream &OS) {}
template <>
void writeFatArch<MachO::fat_arch>(MachOYAML::FatArch &Arch, raw_ostream &OS) {
auto FatArch = constructFatArch<MachO::fat_arch>(Arch);
if (sys::IsLittleEndianHost)
swapStruct(FatArch);
OS.write(reinterpret_cast<const char *>(&FatArch), sizeof(MachO::fat_arch));
}
template <>
void writeFatArch<MachO::fat_arch_64>(MachOYAML::FatArch &Arch,
raw_ostream &OS) {
auto FatArch = constructFatArch<MachO::fat_arch_64>(Arch);
FatArch.reserved = Arch.reserved;
if (sys::IsLittleEndianHost)
swapStruct(FatArch);
OS.write(reinterpret_cast<const char *>(&FatArch),
sizeof(MachO::fat_arch_64));
}
Error UniversalWriter::writeFatArchs(raw_ostream &OS) {
auto &FatFile = *ObjectFile.FatMachO;
bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64;
for (auto Arch : FatFile.FatArchs) {
if (is64Bit)
writeFatArch<MachO::fat_arch_64>(Arch, OS);
else
writeFatArch<MachO::fat_arch>(Arch, OS);
}
return Error::success();
}
void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
auto currOffset = OS.tell() - fileStart;
if (currOffset < Offset)
ZeroFillBytes(OS, Offset - currOffset);
}
} // end anonymous namespace
int yaml2macho(yaml::YamlObjectFile &Doc, raw_ostream &Out) {
UniversalWriter Writer(Doc);
if (auto Err = Writer.writeMachO(Out)) {
errs() << toString(std::move(Err));
return 1;
}
return 0;
}