Path: blob/main/contrib/llvm-project/llvm/lib/ObjCopy/MachO/MachOWriter.cpp
35269 views
//===- MachOWriter.cpp ------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "MachOWriter.h"9#include "MachOLayoutBuilder.h"10#include "MachOObject.h"11#include "llvm/ADT/STLExtras.h"12#include "llvm/BinaryFormat/MachO.h"13#include "llvm/Object/MachO.h"14#include "llvm/Support/Errc.h"15#include "llvm/Support/ErrorHandling.h"16#include "llvm/Support/SHA256.h"17#include <memory>1819#if defined(__APPLE__)20#include <sys/mman.h>21#endif2223using namespace llvm;24using namespace llvm::objcopy::macho;25using namespace llvm::support::endian;2627size_t MachOWriter::headerSize() const {28return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);29}3031size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; }3233size_t MachOWriter::symTableSize() const {34return O.SymTable.Symbols.size() *35(Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist));36}3738size_t MachOWriter::totalSize() const {39// Going from tail to head and looking for an appropriate "anchor" to40// calculate the total size assuming that all the offsets are either valid41// ("true") or 0 (0 indicates that the corresponding part is missing).4243SmallVector<size_t, 7> Ends;44if (O.SymTabCommandIndex) {45const MachO::symtab_command &SymTabCommand =46O.LoadCommands[*O.SymTabCommandIndex]47.MachOLoadCommand.symtab_command_data;48if (SymTabCommand.symoff)49Ends.push_back(SymTabCommand.symoff + symTableSize());50if (SymTabCommand.stroff)51Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize);52}53if (O.DyLdInfoCommandIndex) {54const MachO::dyld_info_command &DyLdInfoCommand =55O.LoadCommands[*O.DyLdInfoCommandIndex]56.MachOLoadCommand.dyld_info_command_data;57if (DyLdInfoCommand.rebase_off) {58assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&59"Incorrect rebase opcodes size");60Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size);61}62if (DyLdInfoCommand.bind_off) {63assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&64"Incorrect bind opcodes size");65Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size);66}67if (DyLdInfoCommand.weak_bind_off) {68assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&69"Incorrect weak bind opcodes size");70Ends.push_back(DyLdInfoCommand.weak_bind_off +71DyLdInfoCommand.weak_bind_size);72}73if (DyLdInfoCommand.lazy_bind_off) {74assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&75"Incorrect lazy bind opcodes size");76Ends.push_back(DyLdInfoCommand.lazy_bind_off +77DyLdInfoCommand.lazy_bind_size);78}79if (DyLdInfoCommand.export_off) {80assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&81"Incorrect trie size");82Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size);83}84}8586if (O.DySymTabCommandIndex) {87const MachO::dysymtab_command &DySymTabCommand =88O.LoadCommands[*O.DySymTabCommandIndex]89.MachOLoadCommand.dysymtab_command_data;9091if (DySymTabCommand.indirectsymoff)92Ends.push_back(DySymTabCommand.indirectsymoff +93sizeof(uint32_t) * O.IndirectSymTable.Symbols.size());94}9596for (std::optional<size_t> LinkEditDataCommandIndex :97{O.CodeSignatureCommandIndex, O.DylibCodeSignDRsIndex,98O.DataInCodeCommandIndex, O.LinkerOptimizationHintCommandIndex,99O.FunctionStartsCommandIndex, O.ChainedFixupsCommandIndex,100O.ExportsTrieCommandIndex})101if (LinkEditDataCommandIndex) {102const MachO::linkedit_data_command &LinkEditDataCommand =103O.LoadCommands[*LinkEditDataCommandIndex]104.MachOLoadCommand.linkedit_data_command_data;105if (LinkEditDataCommand.dataoff)106Ends.push_back(LinkEditDataCommand.dataoff +107LinkEditDataCommand.datasize);108}109110// Otherwise, use the last section / reloction.111for (const LoadCommand &LC : O.LoadCommands)112for (const std::unique_ptr<Section> &S : LC.Sections) {113if (!S->hasValidOffset()) {114assert((S->Offset == 0) && "Skipped section's offset must be zero");115assert((S->isVirtualSection() || S->Size == 0) &&116"Non-zero-fill sections with zero offset must have zero size");117continue;118}119assert((S->Offset != 0) &&120"Non-zero-fill section's offset cannot be zero");121Ends.push_back(S->Offset + S->Size);122if (S->RelOff)123Ends.push_back(S->RelOff +124S->NReloc * sizeof(MachO::any_relocation_info));125}126127if (!Ends.empty())128return *llvm::max_element(Ends);129130// Otherwise, we have only Mach header and load commands.131return headerSize() + loadCommandsSize();132}133134void MachOWriter::writeHeader() {135MachO::mach_header_64 Header;136137Header.magic = O.Header.Magic;138Header.cputype = O.Header.CPUType;139Header.cpusubtype = O.Header.CPUSubType;140Header.filetype = O.Header.FileType;141Header.ncmds = O.Header.NCmds;142Header.sizeofcmds = O.Header.SizeOfCmds;143Header.flags = O.Header.Flags;144Header.reserved = O.Header.Reserved;145146if (IsLittleEndian != sys::IsLittleEndianHost)147MachO::swapStruct(Header);148149auto HeaderSize =150Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);151memcpy(Buf->getBufferStart(), &Header, HeaderSize);152}153154void MachOWriter::writeLoadCommands() {155uint8_t *Begin =156reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + headerSize();157for (const LoadCommand &LC : O.LoadCommands) {158// Construct a load command.159MachO::macho_load_command MLC = LC.MachOLoadCommand;160switch (MLC.load_command_data.cmd) {161case MachO::LC_SEGMENT:162if (IsLittleEndian != sys::IsLittleEndianHost)163MachO::swapStruct(MLC.segment_command_data);164memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command));165Begin += sizeof(MachO::segment_command);166167for (const std::unique_ptr<Section> &Sec : LC.Sections)168writeSectionInLoadCommand<MachO::section>(*Sec, Begin);169continue;170case MachO::LC_SEGMENT_64:171if (IsLittleEndian != sys::IsLittleEndianHost)172MachO::swapStruct(MLC.segment_command_64_data);173memcpy(Begin, &MLC.segment_command_64_data,174sizeof(MachO::segment_command_64));175Begin += sizeof(MachO::segment_command_64);176177for (const std::unique_ptr<Section> &Sec : LC.Sections)178writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);179continue;180}181182#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \183case MachO::LCName: \184assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \185MLC.load_command_data.cmdsize); \186if (IsLittleEndian != sys::IsLittleEndianHost) \187MachO::swapStruct(MLC.LCStruct##_data); \188memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \189Begin += sizeof(MachO::LCStruct); \190if (!LC.Payload.empty()) \191memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \192Begin += LC.Payload.size(); \193break;194195// Copy the load command as it is.196switch (MLC.load_command_data.cmd) {197default:198assert(sizeof(MachO::load_command) + LC.Payload.size() ==199MLC.load_command_data.cmdsize);200if (IsLittleEndian != sys::IsLittleEndianHost)201MachO::swapStruct(MLC.load_command_data);202memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));203Begin += sizeof(MachO::load_command);204if (!LC.Payload.empty())205memcpy(Begin, LC.Payload.data(), LC.Payload.size());206Begin += LC.Payload.size();207break;208#include "llvm/BinaryFormat/MachO.def"209}210}211}212213template <typename StructType>214void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) {215StructType Temp;216assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name");217assert(Sec.Sectname.size() <= sizeof(Temp.sectname) &&218"too long section name");219memset(&Temp, 0, sizeof(StructType));220memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size());221memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size());222Temp.addr = Sec.Addr;223Temp.size = Sec.Size;224Temp.offset = Sec.Offset;225Temp.align = Sec.Align;226Temp.reloff = Sec.RelOff;227Temp.nreloc = Sec.NReloc;228Temp.flags = Sec.Flags;229Temp.reserved1 = Sec.Reserved1;230Temp.reserved2 = Sec.Reserved2;231232if (IsLittleEndian != sys::IsLittleEndianHost)233MachO::swapStruct(Temp);234memcpy(Out, &Temp, sizeof(StructType));235Out += sizeof(StructType);236}237238void MachOWriter::writeSections() {239for (const LoadCommand &LC : O.LoadCommands)240for (const std::unique_ptr<Section> &Sec : LC.Sections) {241if (!Sec->hasValidOffset()) {242assert((Sec->Offset == 0) && "Skipped section's offset must be zero");243assert((Sec->isVirtualSection() || Sec->Size == 0) &&244"Non-zero-fill sections with zero offset must have zero size");245continue;246}247248assert(Sec->Offset && "Section offset can not be zero");249assert((Sec->Size == Sec->Content.size()) && "Incorrect section size");250memcpy(Buf->getBufferStart() + Sec->Offset, Sec->Content.data(),251Sec->Content.size());252for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {253RelocationInfo RelocInfo = Sec->Relocations[Index];254if (!RelocInfo.Scattered && !RelocInfo.IsAddend) {255const uint32_t SymbolNum = RelocInfo.Extern256? (*RelocInfo.Symbol)->Index257: (*RelocInfo.Sec)->Index;258RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);259}260if (IsLittleEndian != sys::IsLittleEndianHost)261MachO::swapStruct(262reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));263memcpy(Buf->getBufferStart() + Sec->RelOff +264Index * sizeof(MachO::any_relocation_info),265&RelocInfo.Info, sizeof(RelocInfo.Info));266}267}268}269270template <typename NListType>271void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out,272uint32_t Nstrx) {273NListType ListEntry;274ListEntry.n_strx = Nstrx;275ListEntry.n_type = SE.n_type;276ListEntry.n_sect = SE.n_sect;277ListEntry.n_desc = SE.n_desc;278ListEntry.n_value = SE.n_value;279280if (IsLittleEndian != sys::IsLittleEndianHost)281MachO::swapStruct(ListEntry);282memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));283Out += sizeof(NListType);284}285286void MachOWriter::writeStringTable() {287if (!O.SymTabCommandIndex)288return;289const MachO::symtab_command &SymTabCommand =290O.LoadCommands[*O.SymTabCommandIndex]291.MachOLoadCommand.symtab_command_data;292293uint8_t *StrTable = (uint8_t *)Buf->getBufferStart() + SymTabCommand.stroff;294LayoutBuilder.getStringTableBuilder().write(StrTable);295}296297void MachOWriter::writeSymbolTable() {298if (!O.SymTabCommandIndex)299return;300const MachO::symtab_command &SymTabCommand =301O.LoadCommands[*O.SymTabCommandIndex]302.MachOLoadCommand.symtab_command_data;303304char *SymTable = (char *)Buf->getBufferStart() + SymTabCommand.symoff;305for (auto &Symbol : O.SymTable.Symbols) {306SymbolEntry *Sym = Symbol.get();307uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name);308309if (Is64Bit)310writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx);311else312writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx);313}314}315316void MachOWriter::writeRebaseInfo() {317if (!O.DyLdInfoCommandIndex)318return;319const MachO::dyld_info_command &DyLdInfoCommand =320O.LoadCommands[*O.DyLdInfoCommandIndex]321.MachOLoadCommand.dyld_info_command_data;322char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.rebase_off;323assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&324"Incorrect rebase opcodes size");325memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());326}327328void MachOWriter::writeBindInfo() {329if (!O.DyLdInfoCommandIndex)330return;331const MachO::dyld_info_command &DyLdInfoCommand =332O.LoadCommands[*O.DyLdInfoCommandIndex]333.MachOLoadCommand.dyld_info_command_data;334char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.bind_off;335assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&336"Incorrect bind opcodes size");337memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());338}339340void MachOWriter::writeWeakBindInfo() {341if (!O.DyLdInfoCommandIndex)342return;343const MachO::dyld_info_command &DyLdInfoCommand =344O.LoadCommands[*O.DyLdInfoCommandIndex]345.MachOLoadCommand.dyld_info_command_data;346char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.weak_bind_off;347assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&348"Incorrect weak bind opcodes size");349memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());350}351352void MachOWriter::writeLazyBindInfo() {353if (!O.DyLdInfoCommandIndex)354return;355const MachO::dyld_info_command &DyLdInfoCommand =356O.LoadCommands[*O.DyLdInfoCommandIndex]357.MachOLoadCommand.dyld_info_command_data;358char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.lazy_bind_off;359assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&360"Incorrect lazy bind opcodes size");361memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());362}363364void MachOWriter::writeExportInfo() {365if (!O.DyLdInfoCommandIndex)366return;367const MachO::dyld_info_command &DyLdInfoCommand =368O.LoadCommands[*O.DyLdInfoCommandIndex]369.MachOLoadCommand.dyld_info_command_data;370char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.export_off;371assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&372"Incorrect export trie size");373memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());374}375376void MachOWriter::writeIndirectSymbolTable() {377if (!O.DySymTabCommandIndex)378return;379380const MachO::dysymtab_command &DySymTabCommand =381O.LoadCommands[*O.DySymTabCommandIndex]382.MachOLoadCommand.dysymtab_command_data;383384uint32_t *Out =385(uint32_t *)(Buf->getBufferStart() + DySymTabCommand.indirectsymoff);386for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {387uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;388if (IsLittleEndian != sys::IsLittleEndianHost)389sys::swapByteOrder(Entry);390*Out++ = Entry;391}392}393394void MachOWriter::writeLinkData(std::optional<size_t> LCIndex,395const LinkData &LD) {396if (!LCIndex)397return;398const MachO::linkedit_data_command &LinkEditDataCommand =399O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;400char *Out = (char *)Buf->getBufferStart() + LinkEditDataCommand.dataoff;401assert((LinkEditDataCommand.datasize == LD.Data.size()) &&402"Incorrect data size");403memcpy(Out, LD.Data.data(), LD.Data.size());404}405406static uint64_t407getSegmentFileOffset(const LoadCommand &TextSegmentLoadCommand) {408const MachO::macho_load_command &MLC =409TextSegmentLoadCommand.MachOLoadCommand;410switch (MLC.load_command_data.cmd) {411case MachO::LC_SEGMENT:412return MLC.segment_command_data.fileoff;413case MachO::LC_SEGMENT_64:414return MLC.segment_command_64_data.fileoff;415default:416return 0;417}418}419420static uint64_t getSegmentFileSize(const LoadCommand &TextSegmentLoadCommand) {421const MachO::macho_load_command &MLC =422TextSegmentLoadCommand.MachOLoadCommand;423switch (MLC.load_command_data.cmd) {424case MachO::LC_SEGMENT:425return MLC.segment_command_data.filesize;426case MachO::LC_SEGMENT_64:427return MLC.segment_command_64_data.filesize;428default:429return 0;430}431}432433void MachOWriter::writeCodeSignatureData() {434// NOTE: This CodeSignature section behaviour must be kept in sync with that435// performed in LLD's CodeSignatureSection::write /436// CodeSignatureSection::writeHashes. Furthermore, this call must occur only437// after the rest of the binary has already been written to the buffer. This438// is because the buffer is read from to perform the necessary hashing.439440// The CodeSignature section is the last section in the MachO binary and441// contains a hash of all content in the binary before it. Since llvm-objcopy442// has likely modified the target binary, the hash must be regenerated443// entirely. To generate this hash, we must read from the start of the binary444// (HashReadStart) to just before the start of the CodeSignature section445// (HashReadEnd).446447const CodeSignatureInfo &CodeSignature = LayoutBuilder.getCodeSignature();448449uint8_t *BufferStart = reinterpret_cast<uint8_t *>(Buf->getBufferStart());450uint8_t *HashReadStart = BufferStart;451uint8_t *HashReadEnd = BufferStart + CodeSignature.StartOffset;452453// The CodeSignature section begins with a header, after which the hashes454// of each page of the binary are written.455uint8_t *HashWriteStart = HashReadEnd + CodeSignature.AllHeadersSize;456457uint32_t TextSegmentFileOff = 0;458uint32_t TextSegmentFileSize = 0;459if (O.TextSegmentCommandIndex) {460const LoadCommand &TextSegmentLoadCommand =461O.LoadCommands[*O.TextSegmentCommandIndex];462assert(TextSegmentLoadCommand.MachOLoadCommand.load_command_data.cmd ==463MachO::LC_SEGMENT ||464TextSegmentLoadCommand.MachOLoadCommand.load_command_data.cmd ==465MachO::LC_SEGMENT_64);466assert(StringRef(TextSegmentLoadCommand.MachOLoadCommand467.segment_command_data.segname) == "__TEXT");468TextSegmentFileOff = getSegmentFileOffset(TextSegmentLoadCommand);469TextSegmentFileSize = getSegmentFileSize(TextSegmentLoadCommand);470}471472const uint32_t FileNamePad = CodeSignature.AllHeadersSize -473CodeSignature.FixedHeadersSize -474CodeSignature.OutputFileName.size();475476// Write code section header.477auto *SuperBlob = reinterpret_cast<MachO::CS_SuperBlob *>(HashReadEnd);478write32be(&SuperBlob->magic, MachO::CSMAGIC_EMBEDDED_SIGNATURE);479write32be(&SuperBlob->length, CodeSignature.Size);480write32be(&SuperBlob->count, 1);481auto *BlobIndex = reinterpret_cast<MachO::CS_BlobIndex *>(&SuperBlob[1]);482write32be(&BlobIndex->type, MachO::CSSLOT_CODEDIRECTORY);483write32be(&BlobIndex->offset, CodeSignature.BlobHeadersSize);484auto *CodeDirectory = reinterpret_cast<MachO::CS_CodeDirectory *>(485HashReadEnd + CodeSignature.BlobHeadersSize);486write32be(&CodeDirectory->magic, MachO::CSMAGIC_CODEDIRECTORY);487write32be(&CodeDirectory->length,488CodeSignature.Size - CodeSignature.BlobHeadersSize);489write32be(&CodeDirectory->version, MachO::CS_SUPPORTSEXECSEG);490write32be(&CodeDirectory->flags, MachO::CS_ADHOC | MachO::CS_LINKER_SIGNED);491write32be(&CodeDirectory->hashOffset,492sizeof(MachO::CS_CodeDirectory) +493CodeSignature.OutputFileName.size() + FileNamePad);494write32be(&CodeDirectory->identOffset, sizeof(MachO::CS_CodeDirectory));495CodeDirectory->nSpecialSlots = 0;496write32be(&CodeDirectory->nCodeSlots, CodeSignature.BlockCount);497write32be(&CodeDirectory->codeLimit, CodeSignature.StartOffset);498CodeDirectory->hashSize = static_cast<uint8_t>(CodeSignature.HashSize);499CodeDirectory->hashType = MachO::kSecCodeSignatureHashSHA256;500CodeDirectory->platform = 0;501CodeDirectory->pageSize = CodeSignature.BlockSizeShift;502CodeDirectory->spare2 = 0;503CodeDirectory->scatterOffset = 0;504CodeDirectory->teamOffset = 0;505CodeDirectory->spare3 = 0;506CodeDirectory->codeLimit64 = 0;507write64be(&CodeDirectory->execSegBase, TextSegmentFileOff);508write64be(&CodeDirectory->execSegLimit, TextSegmentFileSize);509write64be(&CodeDirectory->execSegFlags, O.Header.FileType == MachO::MH_EXECUTE510? MachO::CS_EXECSEG_MAIN_BINARY511: 0);512513auto *Id = reinterpret_cast<char *>(&CodeDirectory[1]);514memcpy(Id, CodeSignature.OutputFileName.begin(),515CodeSignature.OutputFileName.size());516memset(Id + CodeSignature.OutputFileName.size(), 0, FileNamePad);517518// Write the hashes.519uint8_t *CurrHashReadPosition = HashReadStart;520uint8_t *CurrHashWritePosition = HashWriteStart;521while (CurrHashReadPosition < HashReadEnd) {522StringRef Block(reinterpret_cast<char *>(CurrHashReadPosition),523std::min(static_cast<size_t>(HashReadEnd524- CurrHashReadPosition),525static_cast<size_t>(CodeSignature.BlockSize)));526SHA256 Hasher;527Hasher.update(Block);528std::array<uint8_t, 32> Hash = Hasher.final();529assert(Hash.size() == CodeSignature.HashSize);530memcpy(CurrHashWritePosition, Hash.data(), CodeSignature.HashSize);531CurrHashReadPosition += CodeSignature.BlockSize;532CurrHashWritePosition += CodeSignature.HashSize;533}534#if defined(__APPLE__)535// This is macOS-specific work-around and makes no sense for any536// other host OS. See https://openradar.appspot.com/FB8914231537//538// The macOS kernel maintains a signature-verification cache to539// quickly validate applications at time of execve(2). The trouble540// is that for the kernel creates the cache entry at the time of the541// mmap(2) call, before we have a chance to write either the code to542// sign or the signature header+hashes. The fix is to invalidate543// all cached data associated with the output file, thus discarding544// the bogus prematurely-cached signature.545msync(BufferStart, CodeSignature.StartOffset + CodeSignature.Size,546MS_INVALIDATE);547#endif548}549550void MachOWriter::writeDataInCodeData() {551return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);552}553554void MachOWriter::writeLinkerOptimizationHint() {555return writeLinkData(O.LinkerOptimizationHintCommandIndex,556O.LinkerOptimizationHint);557}558559void MachOWriter::writeFunctionStartsData() {560return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);561}562563void MachOWriter::writeDylibCodeSignDRsData() {564return writeLinkData(O.DylibCodeSignDRsIndex, O.DylibCodeSignDRs);565}566567void MachOWriter::writeChainedFixupsData() {568return writeLinkData(O.ChainedFixupsCommandIndex, O.ChainedFixups);569}570571void MachOWriter::writeExportsTrieData() {572if (!O.ExportsTrieCommandIndex)573return;574const MachO::linkedit_data_command &ExportsTrieCmd =575O.LoadCommands[*O.ExportsTrieCommandIndex]576.MachOLoadCommand.linkedit_data_command_data;577char *Out = (char *)Buf->getBufferStart() + ExportsTrieCmd.dataoff;578assert((ExportsTrieCmd.datasize == O.Exports.Trie.size()) &&579"Incorrect export trie size");580memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());581}582583void MachOWriter::writeTail() {584typedef void (MachOWriter::*WriteHandlerType)();585typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;586SmallVector<WriteOperation, 7> Queue;587588if (O.SymTabCommandIndex) {589const MachO::symtab_command &SymTabCommand =590O.LoadCommands[*O.SymTabCommandIndex]591.MachOLoadCommand.symtab_command_data;592if (SymTabCommand.symoff)593Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});594if (SymTabCommand.stroff)595Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});596}597598if (O.DyLdInfoCommandIndex) {599const MachO::dyld_info_command &DyLdInfoCommand =600O.LoadCommands[*O.DyLdInfoCommandIndex]601.MachOLoadCommand.dyld_info_command_data;602if (DyLdInfoCommand.rebase_off)603Queue.push_back(604{DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});605if (DyLdInfoCommand.bind_off)606Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});607if (DyLdInfoCommand.weak_bind_off)608Queue.push_back(609{DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});610if (DyLdInfoCommand.lazy_bind_off)611Queue.push_back(612{DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});613if (DyLdInfoCommand.export_off)614Queue.push_back(615{DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});616}617618if (O.DySymTabCommandIndex) {619const MachO::dysymtab_command &DySymTabCommand =620O.LoadCommands[*O.DySymTabCommandIndex]621.MachOLoadCommand.dysymtab_command_data;622623if (DySymTabCommand.indirectsymoff)624Queue.emplace_back(DySymTabCommand.indirectsymoff,625&MachOWriter::writeIndirectSymbolTable);626}627628std::initializer_list<std::pair<std::optional<size_t>, WriteHandlerType>>629LinkEditDataCommandWriters = {630{O.CodeSignatureCommandIndex, &MachOWriter::writeCodeSignatureData},631{O.DylibCodeSignDRsIndex, &MachOWriter::writeDylibCodeSignDRsData},632{O.DataInCodeCommandIndex, &MachOWriter::writeDataInCodeData},633{O.LinkerOptimizationHintCommandIndex,634&MachOWriter::writeLinkerOptimizationHint},635{O.FunctionStartsCommandIndex, &MachOWriter::writeFunctionStartsData},636{O.ChainedFixupsCommandIndex, &MachOWriter::writeChainedFixupsData},637{O.ExportsTrieCommandIndex, &MachOWriter::writeExportsTrieData}};638for (const auto &W : LinkEditDataCommandWriters) {639std::optional<size_t> LinkEditDataCommandIndex;640WriteHandlerType WriteHandler;641std::tie(LinkEditDataCommandIndex, WriteHandler) = W;642if (LinkEditDataCommandIndex) {643const MachO::linkedit_data_command &LinkEditDataCommand =644O.LoadCommands[*LinkEditDataCommandIndex]645.MachOLoadCommand.linkedit_data_command_data;646if (LinkEditDataCommand.dataoff)647Queue.emplace_back(LinkEditDataCommand.dataoff, WriteHandler);648}649}650651llvm::sort(Queue, llvm::less_first());652653for (auto WriteOp : Queue)654(this->*WriteOp.second)();655}656657Error MachOWriter::finalize() { return LayoutBuilder.layout(); }658659Error MachOWriter::write() {660size_t TotalSize = totalSize();661Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);662if (!Buf)663return createStringError(errc::not_enough_memory,664"failed to allocate memory buffer of " +665Twine::utohexstr(TotalSize) + " bytes");666writeHeader();667writeLoadCommands();668writeSections();669writeTail();670671// TODO: Implement direct writing to the output stream (without intermediate672// memory buffer Buf).673Out.write(Buf->getBufferStart(), Buf->getBufferSize());674return Error::success();675}676677678