33size_t MachOWriter::headerSize()
const {
34 return Is64Bit ?
sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
37size_t MachOWriter::loadCommandsSize()
const {
return O.Header.SizeOfCmds; }
39size_t MachOWriter::symTableSize()
const {
40 return O.SymTable.Symbols.size() *
41 (Is64Bit ?
sizeof(MachO::nlist_64) :
sizeof(MachO::nlist));
50 if (O.SymTabCommandIndex) {
52 O.LoadCommands[*O.SymTabCommandIndex]
53 .MachOLoadCommand.symtab_command_data;
59 if (O.DyLdInfoCommandIndex) {
61 O.LoadCommands[*O.DyLdInfoCommandIndex]
62 .MachOLoadCommand.dyld_info_command_data;
66 "Incorrect rebase opcodes size");
72 "Incorrect bind opcodes size");
78 "Incorrect weak bind opcodes size");
85 "Incorrect lazy bind opcodes size");
92 "Incorrect trie size");
97 if (O.DySymTabCommandIndex) {
99 O.LoadCommands[*O.DySymTabCommandIndex]
100 .MachOLoadCommand.dysymtab_command_data;
104 sizeof(
uint32_t) * O.IndirectSymTable.Symbols.size());
107 for (std::optional<size_t> LinkEditDataCommandIndex :
108 {O.CodeSignatureCommandIndex, O.DylibCodeSignDRsIndex,
109 O.DataInCodeCommandIndex, O.LinkerOptimizationHintCommandIndex,
110 O.FunctionStartsCommandIndex, O.ChainedFixupsCommandIndex,
111 O.ExportsTrieCommandIndex})
112 if (LinkEditDataCommandIndex) {
114 O.LoadCommands[*LinkEditDataCommandIndex]
115 .MachOLoadCommand.linkedit_data_command_data;
116 if (LinkEditDataCommand.
dataoff)
123 for (
const std::unique_ptr<Section> &S : LC.
Sections) {
124 if (!S->hasValidOffset()) {
125 assert((S->Offset == 0) &&
"Skipped section's offset must be zero");
126 assert((S->isBssSection() || S->Size == 0) &&
127 "Non-zero-fill sections with zero offset must have zero size");
130 assert((S->Offset != 0) &&
131 "Non-zero-fill section's offset cannot be zero");
142 return headerSize() + loadCommandsSize();
145void MachOWriter::writeHeader() {
162 memcpy(Buf->getBufferStart(), &Header, HeaderSize);
165void MachOWriter::writeLoadCommands() {
167 reinterpret_cast<uint8_t *
>(Buf->getBufferStart()) + headerSize();
171 switch (MLC.load_command_data.cmd) {
172 case MachO::LC_SEGMENT:
178 for (
const std::unique_ptr<Section> &Sec : LC.
Sections)
179 writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
181 case MachO::LC_SEGMENT_64:
184 memcpy(Begin, &MLC.segment_command_64_data,
188 for (
const std::unique_ptr<Section> &Sec : LC.
Sections)
189 writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
193#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
194 case MachO::LCName: \
195 assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
196 MLC.load_command_data.cmdsize); \
197 if (IsLittleEndian != sys::IsLittleEndianHost) \
198 MachO::swapStruct(MLC.LCStruct##_data); \
199 memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
200 Begin += sizeof(MachO::LCStruct); \
201 if (!LC.Payload.empty()) \
202 memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
203 Begin += LC.Payload.size(); \
207 switch (MLC.load_command_data.cmd) {
210 MLC.load_command_data.cmdsize);
219#include "llvm/BinaryFormat/MachO.def"
224template <
typename StructType>
225void MachOWriter::writeSectionInLoadCommand(
const Section &Sec, uint8_t *&Out) {
227 assert(Sec.
Segname.size() <=
sizeof(Temp.segname) &&
"too long segment name");
229 "too long section name");
230 memset(&Temp, 0,
sizeof(StructType));
233 Temp.addr = Sec.
Addr;
234 Temp.size = Sec.
Size;
236 Temp.align = Sec.
Align;
239 Temp.flags = Sec.
Flags;
245 memcpy(Out, &Temp,
sizeof(StructType));
246 Out +=
sizeof(StructType);
249void MachOWriter::writeSections() {
250 for (
const LoadCommand &LC : O.LoadCommands)
251 for (
const std::unique_ptr<Section> &Sec : LC.
Sections) {
252 if (!Sec->hasValidOffset()) {
253 assert((Sec->Offset == 0) &&
"Skipped section's offset must be zero");
254 assert((Sec->isBssSection() || Sec->Size == 0) &&
255 "Non-zero-fill sections with zero offset must have zero size");
259 assert(Sec->Offset &&
"Section offset can not be zero");
260 assert((Sec->Size == Sec->Content.size()) &&
"Incorrect section size");
261 memcpy(Buf->getBufferStart() + Sec->Offset, Sec->Content.data(),
262 Sec->Content.size());
263 for (
size_t Index = 0;
Index < Sec->Relocations.size(); ++
Index) {
264 RelocationInfo RelocInfo = Sec->Relocations[
Index];
266 const uint32_t SymbolNum = RelocInfo.
Extern
267 ? (*RelocInfo.
Symbol)->Index
268 : (*RelocInfo.Sec)->
Index;
273 reinterpret_cast<MachO::any_relocation_info &
>(RelocInfo.
Info));
274 memcpy(Buf->getBufferStart() + Sec->RelOff +
275 Index *
sizeof(MachO::any_relocation_info),
276 &RelocInfo.
Info,
sizeof(RelocInfo.
Info));
281template <
typename NListType>
285 ListEntry.n_strx = Nstrx;
286 ListEntry.n_type = SE.
n_type;
287 ListEntry.n_sect = SE.
n_sect;
288 ListEntry.n_desc = SE.
n_desc;
289 ListEntry.n_value = SE.
n_value;
293 memcpy(Out,
reinterpret_cast<const char *
>(&ListEntry),
sizeof(NListType));
294 Out +=
sizeof(NListType);
297void MachOWriter::writeStringTable() {
298 if (!O.SymTabCommandIndex)
300 const MachO::symtab_command &SymTabCommand =
301 O.LoadCommands[*O.SymTabCommandIndex]
302 .MachOLoadCommand.symtab_command_data;
304 uint8_t *StrTable = (uint8_t *)Buf->getBufferStart() + SymTabCommand.
stroff;
305 LayoutBuilder.getStringTableBuilder().write(StrTable);
308void MachOWriter::writeSymbolTable() {
309 if (!O.SymTabCommandIndex)
311 const MachO::symtab_command &SymTabCommand =
312 O.LoadCommands[*O.SymTabCommandIndex]
313 .MachOLoadCommand.symtab_command_data;
315 char *SymTable = Buf->getBufferStart() + SymTabCommand.
symoff;
316 for (
auto &Symbol : O.SymTable.Symbols) {
317 SymbolEntry *Sym =
Symbol.get();
318 uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->
Name);
327void MachOWriter::writeRebaseInfo() {
328 if (!O.DyLdInfoCommandIndex)
330 const MachO::dyld_info_command &DyLdInfoCommand =
331 O.LoadCommands[*O.DyLdInfoCommandIndex]
332 .MachOLoadCommand.dyld_info_command_data;
333 char *Out = Buf->getBufferStart() + DyLdInfoCommand.
rebase_off;
336 "Incorrect rebase opcodes size");
337 memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
340void MachOWriter::writeBindInfo() {
341 if (!O.DyLdInfoCommandIndex)
343 const MachO::dyld_info_command &DyLdInfoCommand =
344 O.LoadCommands[*O.DyLdInfoCommandIndex]
345 .MachOLoadCommand.dyld_info_command_data;
346 char *Out = Buf->getBufferStart() + DyLdInfoCommand.
bind_off;
349 "Incorrect bind opcodes size");
350 memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
353void MachOWriter::writeWeakBindInfo() {
354 if (!O.DyLdInfoCommandIndex)
356 const MachO::dyld_info_command &DyLdInfoCommand =
357 O.LoadCommands[*O.DyLdInfoCommandIndex]
358 .MachOLoadCommand.dyld_info_command_data;
359 char *Out = Buf->getBufferStart() + DyLdInfoCommand.
weak_bind_off;
362 "Incorrect weak bind opcodes size");
363 memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
366void MachOWriter::writeLazyBindInfo() {
367 if (!O.DyLdInfoCommandIndex)
369 const MachO::dyld_info_command &DyLdInfoCommand =
370 O.LoadCommands[*O.DyLdInfoCommandIndex]
371 .MachOLoadCommand.dyld_info_command_data;
372 char *Out = Buf->getBufferStart() + DyLdInfoCommand.
lazy_bind_off;
375 "Incorrect lazy bind opcodes size");
376 memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
379void MachOWriter::writeExportInfo() {
380 if (!O.DyLdInfoCommandIndex)
382 const MachO::dyld_info_command &DyLdInfoCommand =
383 O.LoadCommands[*O.DyLdInfoCommandIndex]
384 .MachOLoadCommand.dyld_info_command_data;
385 char *Out = Buf->getBufferStart() + DyLdInfoCommand.
export_off;
388 "Incorrect export trie size");
389 memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
392void MachOWriter::writeIndirectSymbolTable() {
393 if (!O.DySymTabCommandIndex)
396 const MachO::dysymtab_command &DySymTabCommand =
397 O.LoadCommands[*O.DySymTabCommandIndex]
398 .MachOLoadCommand.dysymtab_command_data;
401 (uint32_t *)(Buf->getBufferStart() + DySymTabCommand.
indirectsymoff);
402 for (
const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
410void MachOWriter::writeLinkData(std::optional<size_t> LCIndex,
414 const MachO::linkedit_data_command &LinkEditDataCommand =
415 O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
416 char *Out = Buf->getBufferStart() + LinkEditDataCommand.
dataoff;
419 "Incorrect data size");
420 memcpy(Out,
LD.Data.data(),
LD.Data.size());
427 switch (MLC.load_command_data.cmd) {
428 case MachO::LC_SEGMENT:
429 return MLC.segment_command_data.fileoff;
430 case MachO::LC_SEGMENT_64:
431 return MLC.segment_command_64_data.fileoff;
440 switch (MLC.load_command_data.cmd) {
441 case MachO::LC_SEGMENT:
442 return MLC.segment_command_data.filesize;
443 case MachO::LC_SEGMENT_64:
444 return MLC.segment_command_64_data.filesize;
450void MachOWriter::writeCodeSignatureData() {
464 const CodeSignatureInfo &CodeSignature = LayoutBuilder.getCodeSignature();
466 uint8_t *BufferStart =
reinterpret_cast<uint8_t *
>(Buf->getBufferStart());
467 uint8_t *HashReadStart = BufferStart;
468 uint8_t *HashReadEnd = BufferStart + CodeSignature.
StartOffset;
472 uint8_t *HashWriteStart = HashReadEnd + CodeSignature.
AllHeadersSize;
474 uint32_t TextSegmentFileOff = 0;
475 uint32_t TextSegmentFileSize = 0;
476 if (O.TextSegmentCommandIndex) {
477 const LoadCommand &TextSegmentLoadCommand =
478 O.LoadCommands[*O.TextSegmentCommandIndex];
482 MachO::LC_SEGMENT_64);
484 .segment_command_data.segname) ==
"__TEXT");
494 auto *SuperBlob =
reinterpret_cast<MachO::CS_SuperBlob *
>(HashReadEnd);
498 auto *BlobIndex =
reinterpret_cast<MachO::CS_BlobIndex *
>(&SuperBlob[1]);
501 auto *CodeDirectory =
reinterpret_cast<MachO::CS_CodeDirectory *
>(
509 sizeof(MachO::CS_CodeDirectory) +
511 write32be(&CodeDirectory->identOffset,
sizeof(MachO::CS_CodeDirectory));
512 CodeDirectory->nSpecialSlots = 0;
515 CodeDirectory->hashSize =
static_cast<uint8_t
>(CodeSignature.
HashSize);
517 CodeDirectory->platform = 0;
519 CodeDirectory->spare2 = 0;
520 CodeDirectory->scatterOffset = 0;
521 CodeDirectory->teamOffset = 0;
522 CodeDirectory->spare3 = 0;
523 CodeDirectory->codeLimit64 = 0;
524 write64be(&CodeDirectory->execSegBase, TextSegmentFileOff);
525 write64be(&CodeDirectory->execSegLimit, TextSegmentFileSize);
530 auto *
Id =
reinterpret_cast<char *
>(&CodeDirectory[1]);
536 uint8_t *CurrHashReadPosition = HashReadStart;
537 uint8_t *CurrHashWritePosition = HashWriteStart;
538 while (CurrHashReadPosition < HashReadEnd) {
539 StringRef
Block(
reinterpret_cast<char *
>(CurrHashReadPosition),
540 std::min(
static_cast<size_t>(HashReadEnd
541 - CurrHashReadPosition),
542 static_cast<size_t>(CodeSignature.
BlockSize)));
545 std::array<uint8_t, 32> Hash = Hasher.
final();
547 memcpy(CurrHashWritePosition, Hash.data(), CodeSignature.
HashSize);
548 CurrHashReadPosition += CodeSignature.
BlockSize;
549 CurrHashWritePosition += CodeSignature.
HashSize;
551#if defined(__APPLE__)
567void MachOWriter::writeDataInCodeData() {
568 return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);
571void MachOWriter::writeLinkerOptimizationHint() {
572 return writeLinkData(O.LinkerOptimizationHintCommandIndex,
573 O.LinkerOptimizationHint);
576void MachOWriter::writeFunctionStartsData() {
577 return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);
580void MachOWriter::writeDylibCodeSignDRsData() {
581 return writeLinkData(O.DylibCodeSignDRsIndex, O.DylibCodeSignDRs);
584void MachOWriter::writeChainedFixupsData() {
585 return writeLinkData(O.ChainedFixupsCommandIndex, O.ChainedFixups);
588void MachOWriter::writeExportsTrieData() {
589 if (!O.ExportsTrieCommandIndex)
591 const MachO::linkedit_data_command &ExportsTrieCmd =
592 O.LoadCommands[*O.ExportsTrieCommandIndex]
593 .MachOLoadCommand.linkedit_data_command_data;
594 char *Out = Buf->getBufferStart() + ExportsTrieCmd.
dataoff;
597 "Incorrect export trie size");
598 memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
601void MachOWriter::writeTail() {
603 typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
606 if (O.SymTabCommandIndex) {
607 const MachO::symtab_command &SymTabCommand =
608 O.LoadCommands[*O.SymTabCommandIndex]
609 .MachOLoadCommand.symtab_command_data;
611 Queue.push_back({SymTabCommand.
symoff, &MachOWriter::writeSymbolTable});
613 Queue.push_back({SymTabCommand.
stroff, &MachOWriter::writeStringTable});
616 if (O.DyLdInfoCommandIndex) {
617 const MachO::dyld_info_command &DyLdInfoCommand =
618 O.LoadCommands[*O.DyLdInfoCommandIndex]
619 .MachOLoadCommand.dyld_info_command_data;
622 {DyLdInfoCommand.
rebase_off, &MachOWriter::writeRebaseInfo});
624 Queue.push_back({DyLdInfoCommand.
bind_off, &MachOWriter::writeBindInfo});
627 {DyLdInfoCommand.
weak_bind_off, &MachOWriter::writeWeakBindInfo});
630 {DyLdInfoCommand.
lazy_bind_off, &MachOWriter::writeLazyBindInfo});
633 {DyLdInfoCommand.
export_off, &MachOWriter::writeExportInfo});
636 if (O.DySymTabCommandIndex) {
637 const MachO::dysymtab_command &DySymTabCommand =
638 O.LoadCommands[*O.DySymTabCommandIndex]
639 .MachOLoadCommand.dysymtab_command_data;
643 &MachOWriter::writeIndirectSymbolTable);
646 std::initializer_list<std::pair<std::optional<size_t>, WriteHandlerType>>
647 LinkEditDataCommandWriters = {
648 {O.CodeSignatureCommandIndex, &MachOWriter::writeCodeSignatureData},
649 {O.DylibCodeSignDRsIndex, &MachOWriter::writeDylibCodeSignDRsData},
650 {O.DataInCodeCommandIndex, &MachOWriter::writeDataInCodeData},
651 {O.LinkerOptimizationHintCommandIndex,
652 &MachOWriter::writeLinkerOptimizationHint},
653 {O.FunctionStartsCommandIndex, &MachOWriter::writeFunctionStartsData},
654 {O.ChainedFixupsCommandIndex, &MachOWriter::writeChainedFixupsData},
655 {O.ExportsTrieCommandIndex, &MachOWriter::writeExportsTrieData}};
656 for (
const auto &W : LinkEditDataCommandWriters) {
657 std::optional<size_t> LinkEditDataCommandIndex;
658 WriteHandlerType WriteHandler;
659 std::tie(LinkEditDataCommandIndex, WriteHandler) =
W;
660 if (LinkEditDataCommandIndex) {
661 const MachO::linkedit_data_command &LinkEditDataCommand =
662 O.LoadCommands[*LinkEditDataCommandIndex]
663 .MachOLoadCommand.linkedit_data_command_data;
664 if (LinkEditDataCommand.
dataoff)
665 Queue.emplace_back(LinkEditDataCommand.
dataoff, WriteHandler);
671 for (
auto WriteOp : Queue)
672 (this->*WriteOp.second)();
682 "failed to allocate memory buffer of " +
691 Out.write(Buf->getBufferStart(), Buf->getBufferSize());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t paddedLinkEditEntrySize(uint64_t Size, bool Is64Bit)
static uint64_t getSegmentFileOffset(const LoadCommand &TextSegmentLoadCommand)
static uint64_t getSegmentFileSize(const LoadCommand &TextSegmentLoadCommand)
void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out, uint32_t Nstrx)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
LLVM_ABI std::array< uint8_t, 32 > final()
Return the current raw 256-bits SHA256 for the digested data since the last call to init().
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
constexpr size_t size() const
Get the string size.
static Twine utohexstr(uint64_t Val)
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
MachOWriter(Object &O, bool Is64Bit, bool IsLittleEndian, StringRef OutputFileName, uint64_t PageSize, raw_ostream &Out)
@ CSMAGIC_EMBEDDED_SIGNATURE
@ kSecCodeSignatureHashSHA256
void swapStruct(fat_header &mh)
void write32be(void *P, uint32_t V)
void write64be(void *P, uint64_t V)
constexpr bool IsLittleEndianHost
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
void sort(IteratorTy Start, IteratorTy End)
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
static constexpr size_t BlobHeadersSize
static constexpr size_t HashSize
static constexpr uint32_t FixedHeadersSize
static constexpr uint8_t BlockSizeShift
static constexpr size_t BlockSize
std::optional< SymbolEntry * > Symbol
The Symbol referenced by this entry.
MachO::macho_load_command MachOLoadCommand
std::vector< std::unique_ptr< Section > > Sections
std::vector< uint8_t > Payload
std::vector< LoadCommand > LoadCommands
MachO::any_relocation_info Info
std::optional< const SymbolEntry * > Symbol
void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian)