27size_t MachOWriter::headerSize()
const {
33size_t MachOWriter::symTableSize()
const {
47 .MachOLoadCommand.symtab_command_data;
56 .MachOLoadCommand.dyld_info_command_data;
59 "Incorrect rebase opcodes size");
64 "Incorrect bind opcodes size");
69 "Incorrect weak bind opcodes size");
75 "Incorrect lazy bind opcodes size");
81 "Incorrect trie size");
89 .MachOLoadCommand.dysymtab_command_data;
96 for (std::optional<size_t> LinkEditDataCommandIndex :
101 if (LinkEditDataCommandIndex) {
104 .MachOLoadCommand.linkedit_data_command_data;
105 if (LinkEditDataCommand.
dataoff)
112 for (
const std::unique_ptr<Section> &S : LC.
Sections) {
113 if (!S->hasValidOffset()) {
114 assert((S->Offset == 0) &&
"Skipped section's offset must be zero");
115 assert((S->isVirtualSection() || S->Size == 0) &&
116 "Non-zero-fill sections with zero offset must have zero size");
119 assert((S->Offset != 0) &&
120 "Non-zero-fill section's offset cannot be zero");
131 return headerSize() + loadCommandsSize();
134void MachOWriter::writeHeader() {
151 memcpy(Buf->getBufferStart(), &Header, HeaderSize);
154void MachOWriter::writeLoadCommands() {
156 reinterpret_cast<uint8_t *
>(Buf->getBufferStart()) + headerSize();
160 switch (MLC.load_command_data.cmd) {
161 case MachO::LC_SEGMENT:
167 for (
const std::unique_ptr<Section> &Sec : LC.
Sections)
168 writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
170 case MachO::LC_SEGMENT_64:
173 memcpy(Begin, &MLC.segment_command_64_data,
177 for (
const std::unique_ptr<Section> &Sec : LC.
Sections)
178 writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
182#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
183 case MachO::LCName: \
184 assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
185 MLC.load_command_data.cmdsize); \
186 if (IsLittleEndian != sys::IsLittleEndianHost) \
187 MachO::swapStruct(MLC.LCStruct##_data); \
188 memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
189 Begin += sizeof(MachO::LCStruct); \
190 if (!LC.Payload.empty()) \
191 memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
192 Begin += LC.Payload.size(); \
196 switch (MLC.load_command_data.cmd) {
199 MLC.load_command_data.cmdsize);
208#include "llvm/BinaryFormat/MachO.def"
213template <
typename StructType>
214void MachOWriter::writeSectionInLoadCommand(
const Section &Sec,
uint8_t *&Out) {
216 assert(Sec.
Segname.size() <=
sizeof(Temp.segname) &&
"too long segment name");
218 "too long section name");
222 Temp.addr = Sec.
Addr;
223 Temp.size = Sec.
Size;
225 Temp.align = Sec.
Align;
228 Temp.flags = Sec.
Flags;
238void MachOWriter::writeSections() {
240 for (
const std::unique_ptr<Section> &Sec : LC.
Sections) {
241 if (!Sec->hasValidOffset()) {
242 assert((Sec->Offset == 0) &&
"Skipped section's offset must be zero");
243 assert((Sec->isVirtualSection() || Sec->Size == 0) &&
244 "Non-zero-fill sections with zero offset must have zero size");
248 assert(Sec->Offset &&
"Section offset can not be zero");
249 assert((Sec->Size == Sec->Content.size()) &&
"Incorrect section size");
250 memcpy(Buf->getBufferStart() + Sec->Offset, Sec->Content.data(),
251 Sec->Content.size());
252 for (
size_t Index = 0;
Index < Sec->Relocations.size(); ++
Index) {
256 ? (*RelocInfo.
Symbol)->Index
257 : (*RelocInfo.
Sec)->Index;
263 memcpy(Buf->getBufferStart() + Sec->RelOff +
265 &RelocInfo.
Info,
sizeof(RelocInfo.
Info));
270template <
typename NListType>
274 ListEntry.n_strx = Nstrx;
275 ListEntry.n_type = SE.
n_type;
276 ListEntry.n_sect = SE.
n_sect;
277 ListEntry.n_desc = SE.
n_desc;
278 ListEntry.n_value = SE.
n_value;
282 memcpy(Out,
reinterpret_cast<const char *
>(&ListEntry),
sizeof(NListType));
283 Out +=
sizeof(NListType);
286void MachOWriter::writeStringTable() {
291 .MachOLoadCommand.symtab_command_data;
297void MachOWriter::writeSymbolTable() {
302 .MachOLoadCommand.symtab_command_data;
304 char *SymTable = (
char *)Buf->getBufferStart() + SymTabCommand.
symoff;
310 writeNListEntry<MachO::nlist_64>(*
Sym, IsLittleEndian, SymTable, Nstrx);
312 writeNListEntry<MachO::nlist>(*
Sym, IsLittleEndian, SymTable, Nstrx);
316void MachOWriter::writeRebaseInfo() {
321 .MachOLoadCommand.dyld_info_command_data;
322 char *Out = (
char *)Buf->getBufferStart() + DyLdInfoCommand.
rebase_off;
324 "Incorrect rebase opcodes size");
328void MachOWriter::writeBindInfo() {
333 .MachOLoadCommand.dyld_info_command_data;
334 char *Out = (
char *)Buf->getBufferStart() + DyLdInfoCommand.
bind_off;
336 "Incorrect bind opcodes size");
340void MachOWriter::writeWeakBindInfo() {
345 .MachOLoadCommand.dyld_info_command_data;
346 char *Out = (
char *)Buf->getBufferStart() + DyLdInfoCommand.
weak_bind_off;
348 "Incorrect weak bind opcodes size");
352void MachOWriter::writeLazyBindInfo() {
357 .MachOLoadCommand.dyld_info_command_data;
358 char *Out = (
char *)Buf->getBufferStart() + DyLdInfoCommand.
lazy_bind_off;
360 "Incorrect lazy bind opcodes size");
364void MachOWriter::writeExportInfo() {
369 .MachOLoadCommand.dyld_info_command_data;
370 char *Out = (
char *)Buf->getBufferStart() + DyLdInfoCommand.
export_off;
372 "Incorrect export trie size");
376void MachOWriter::writeIndirectSymbolTable() {
382 .MachOLoadCommand.dysymtab_command_data;
394void MachOWriter::writeLinkData(std::optional<size_t> LCIndex,
399 O.
LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
400 char *Out = (
char *)Buf->getBufferStart() + LinkEditDataCommand.
dataoff;
402 "Incorrect data size");
403 memcpy(Out,
LD.Data.data(),
LD.Data.size());
410 switch (MLC.load_command_data.cmd) {
411 case MachO::LC_SEGMENT:
412 return MLC.segment_command_data.fileoff;
413 case MachO::LC_SEGMENT_64:
414 return MLC.segment_command_64_data.fileoff;
423 switch (MLC.load_command_data.cmd) {
424 case MachO::LC_SEGMENT:
425 return MLC.segment_command_data.filesize;
426 case MachO::LC_SEGMENT_64:
427 return MLC.segment_command_64_data.filesize;
433void MachOWriter::writeCodeSignatureData() {
449 uint8_t *BufferStart =
reinterpret_cast<uint8_t *
>(Buf->getBufferStart());
450 uint8_t *HashReadStart = BufferStart;
465 MachO::LC_SEGMENT_64);
467 .segment_command_data.segname) ==
"__TEXT");
495 CodeDirectory->nSpecialSlots = 0;
498 CodeDirectory->hashSize =
static_cast<uint8_t>(CodeSignature.
HashSize);
500 CodeDirectory->platform = 0;
502 CodeDirectory->spare2 = 0;
503 CodeDirectory->scatterOffset = 0;
504 CodeDirectory->teamOffset = 0;
505 CodeDirectory->spare3 = 0;
506 CodeDirectory->codeLimit64 = 0;
507 write64be(&CodeDirectory->execSegBase, TextSegmentFileOff);
508 write64be(&CodeDirectory->execSegLimit, TextSegmentFileSize);
513 auto *
Id =
reinterpret_cast<char *
>(&CodeDirectory[1]);
519 uint8_t *CurrHashReadPosition = HashReadStart;
520 uint8_t *CurrHashWritePosition = HashWriteStart;
521 while (CurrHashReadPosition < HashReadEnd) {
523 std::min(
static_cast<size_t>(HashReadEnd
524 - CurrHashReadPosition),
525 static_cast<size_t>(CodeSignature.
BlockSize)));
528 std::array<uint8_t, 32> Hash = Hasher.
final();
530 memcpy(CurrHashWritePosition, Hash.data(), CodeSignature.
HashSize);
531 CurrHashReadPosition += CodeSignature.
BlockSize;
532 CurrHashWritePosition += CodeSignature.
HashSize;
534#if defined(__APPLE__)
550void MachOWriter::writeDataInCodeData() {
554void MachOWriter::writeLinkerOptimizationHint() {
559void MachOWriter::writeFunctionStartsData() {
563void MachOWriter::writeDylibCodeSignDRsData() {
567void MachOWriter::writeChainedFixupsData() {
571void MachOWriter::writeExportsTrieData() {
576 .MachOLoadCommand.linkedit_data_command_data;
577 char *Out = (
char *)Buf->getBufferStart() + ExportsTrieCmd.
dataoff;
579 "Incorrect export trie size");
583void MachOWriter::writeTail() {
585 typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
591 .MachOLoadCommand.symtab_command_data;
593 Queue.push_back({SymTabCommand.
symoff, &MachOWriter::writeSymbolTable});
595 Queue.push_back({SymTabCommand.
stroff, &MachOWriter::writeStringTable});
601 .MachOLoadCommand.dyld_info_command_data;
604 {DyLdInfoCommand.
rebase_off, &MachOWriter::writeRebaseInfo});
606 Queue.push_back({DyLdInfoCommand.
bind_off, &MachOWriter::writeBindInfo});
609 {DyLdInfoCommand.
weak_bind_off, &MachOWriter::writeWeakBindInfo});
612 {DyLdInfoCommand.
lazy_bind_off, &MachOWriter::writeLazyBindInfo});
615 {DyLdInfoCommand.
export_off, &MachOWriter::writeExportInfo});
621 .MachOLoadCommand.dysymtab_command_data;
625 &MachOWriter::writeIndirectSymbolTable);
628 std::initializer_list<std::pair<std::optional<size_t>, WriteHandlerType>>
629 LinkEditDataCommandWriters = {
634 &MachOWriter::writeLinkerOptimizationHint},
638 for (
const auto &W : LinkEditDataCommandWriters) {
639 std::optional<size_t> LinkEditDataCommandIndex;
640 WriteHandlerType WriteHandler;
641 std::tie(LinkEditDataCommandIndex, WriteHandler) =
W;
642 if (LinkEditDataCommandIndex) {
645 .MachOLoadCommand.linkedit_data_command_data;
646 if (LinkEditDataCommand.
dataoff)
647 Queue.emplace_back(LinkEditDataCommand.
dataoff, WriteHandler);
653 for (
auto WriteOp : Queue)
654 (this->*WriteOp.second)();
664 "failed to allocate memory buffer of " +
673 Out.write(Buf->getBufferStart(), Buf->getBufferSize());
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
std::array< uint8_t, 32 > final()
Return the current raw 256-bits SHA256 for the digested data since the last call to init().
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.
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
void write(raw_ostream &OS) const
Class to represent struct types.
static Twine utohexstr(const uint64_t &Val)
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
StringTableBuilder & getStringTableBuilder()
const CodeSignatureInfo & getCodeSignature() const
@ CSMAGIC_EMBEDDED_SIGNATURE
@ kSecCodeSignatureHashSHA256
void swapStruct(fat_header &mh)
void write32be(void *P, uint32_t V)
void write64be(void *P, uint64_t V)
static const 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)
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Function object to check whether the first component of a container supported by std::get (like std::...
ArrayRef< uint8_t > Opcodes
When MachO binaries include a LC_CODE_SIGNATURE load command, the __LINKEDIT data segment will includ...
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::vector< IndirectSymbolEntry > Symbols
ArrayRef< uint8_t > Opcodes
MachO::macho_load_command MachOLoadCommand
std::vector< std::unique_ptr< Section > > Sections
std::vector< uint8_t > Payload
std::optional< size_t > FunctionStartsCommandIndex
The index LC_FUNCTION_STARTS load command if present.
std::optional< size_t > ChainedFixupsCommandIndex
The index LC_DYLD_CHAINED_FIXUPS load command if present.
std::optional< size_t > ExportsTrieCommandIndex
The index LC_DYLD_EXPORTS_TRIE load command if present.
std::optional< size_t > DylibCodeSignDRsIndex
The index of LC_DYLIB_CODE_SIGN_DRS load command if present.
std::optional< size_t > SymTabCommandIndex
The index of LC_SYMTAB load command if present.
std::optional< size_t > DyLdInfoCommandIndex
The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
std::vector< LoadCommand > LoadCommands
std::optional< size_t > DataInCodeCommandIndex
The index LC_DATA_IN_CODE load command if present.
std::optional< size_t > DySymTabCommandIndex
The index LC_DYSYMTAB load command if present.
LinkData LinkerOptimizationHint
std::optional< size_t > TextSegmentCommandIndex
The index of the LC_SEGMENT or LC_SEGMENT_64 load command corresponding to the __TEXT segment.
IndirectSymbolTable IndirectSymTable
LinkData DylibCodeSignDRs
std::optional< size_t > CodeSignatureCommandIndex
The index of LC_CODE_SIGNATURE load command if present.
std::optional< size_t > LinkerOptimizationHintCommandIndex
The index of LC_LINKER_OPTIMIZATIN_HINT load command if present.
ArrayRef< uint8_t > Opcodes
MachO::any_relocation_info Info
std::optional< const SymbolEntry * > Symbol
std::optional< const Section * > Sec
void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian)
std::vector< std::unique_ptr< SymbolEntry > > Symbols
ArrayRef< uint8_t > Opcodes