MachOWriter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //===- MachOWriter.cpp ------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #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>
  18. #if defined(__APPLE__)
  19. #include <sys/mman.h>
  20. #endif
  21. using namespace llvm;
  22. using namespace llvm::objcopy::macho;
  23. using namespace llvm::support::endian;
  24. size_t MachOWriter::headerSize() const {
  25. return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
  26. }
  27. size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; }
  28. size_t MachOWriter::symTableSize() const {
  29. return O.SymTable.Symbols.size() *
  30. (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist));
  31. }
  32. size_t MachOWriter::totalSize() const {
  33. // Going from tail to head and looking for an appropriate "anchor" to
  34. // calculate the total size assuming that all the offsets are either valid
  35. // ("true") or 0 (0 indicates that the corresponding part is missing).
  36. SmallVector<size_t, 7> Ends;
  37. if (O.SymTabCommandIndex) {
  38. const MachO::symtab_command &SymTabCommand =
  39. O.LoadCommands[*O.SymTabCommandIndex]
  40. .MachOLoadCommand.symtab_command_data;
  41. if (SymTabCommand.symoff)
  42. Ends.push_back(SymTabCommand.symoff + symTableSize());
  43. if (SymTabCommand.stroff)
  44. Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize);
  45. }
  46. if (O.DyLdInfoCommandIndex) {
  47. const MachO::dyld_info_command &DyLdInfoCommand =
  48. O.LoadCommands[*O.DyLdInfoCommandIndex]
  49. .MachOLoadCommand.dyld_info_command_data;
  50. if (DyLdInfoCommand.rebase_off) {
  51. assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
  52. "Incorrect rebase opcodes size");
  53. Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size);
  54. }
  55. if (DyLdInfoCommand.bind_off) {
  56. assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
  57. "Incorrect bind opcodes size");
  58. Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size);
  59. }
  60. if (DyLdInfoCommand.weak_bind_off) {
  61. assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
  62. "Incorrect weak bind opcodes size");
  63. Ends.push_back(DyLdInfoCommand.weak_bind_off +
  64. DyLdInfoCommand.weak_bind_size);
  65. }
  66. if (DyLdInfoCommand.lazy_bind_off) {
  67. assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
  68. "Incorrect lazy bind opcodes size");
  69. Ends.push_back(DyLdInfoCommand.lazy_bind_off +
  70. DyLdInfoCommand.lazy_bind_size);
  71. }
  72. if (DyLdInfoCommand.export_off) {
  73. assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
  74. "Incorrect trie size");
  75. Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size);
  76. }
  77. }
  78. if (O.DySymTabCommandIndex) {
  79. const MachO::dysymtab_command &DySymTabCommand =
  80. O.LoadCommands[*O.DySymTabCommandIndex]
  81. .MachOLoadCommand.dysymtab_command_data;
  82. if (DySymTabCommand.indirectsymoff)
  83. Ends.push_back(DySymTabCommand.indirectsymoff +
  84. sizeof(uint32_t) * O.IndirectSymTable.Symbols.size());
  85. }
  86. for (std::optional<size_t> LinkEditDataCommandIndex :
  87. {O.CodeSignatureCommandIndex, O.DylibCodeSignDRsIndex,
  88. O.DataInCodeCommandIndex, O.LinkerOptimizationHintCommandIndex,
  89. O.FunctionStartsCommandIndex, O.ChainedFixupsCommandIndex,
  90. O.ExportsTrieCommandIndex})
  91. if (LinkEditDataCommandIndex) {
  92. const MachO::linkedit_data_command &LinkEditDataCommand =
  93. O.LoadCommands[*LinkEditDataCommandIndex]
  94. .MachOLoadCommand.linkedit_data_command_data;
  95. if (LinkEditDataCommand.dataoff)
  96. Ends.push_back(LinkEditDataCommand.dataoff +
  97. LinkEditDataCommand.datasize);
  98. }
  99. // Otherwise, use the last section / reloction.
  100. for (const LoadCommand &LC : O.LoadCommands)
  101. for (const std::unique_ptr<Section> &S : LC.Sections) {
  102. if (!S->hasValidOffset()) {
  103. assert((S->Offset == 0) && "Skipped section's offset must be zero");
  104. assert((S->isVirtualSection() || S->Size == 0) &&
  105. "Non-zero-fill sections with zero offset must have zero size");
  106. continue;
  107. }
  108. assert((S->Offset != 0) &&
  109. "Non-zero-fill section's offset cannot be zero");
  110. Ends.push_back(S->Offset + S->Size);
  111. if (S->RelOff)
  112. Ends.push_back(S->RelOff +
  113. S->NReloc * sizeof(MachO::any_relocation_info));
  114. }
  115. if (!Ends.empty())
  116. return *std::max_element(Ends.begin(), Ends.end());
  117. // Otherwise, we have only Mach header and load commands.
  118. return headerSize() + loadCommandsSize();
  119. }
  120. void MachOWriter::writeHeader() {
  121. MachO::mach_header_64 Header;
  122. Header.magic = O.Header.Magic;
  123. Header.cputype = O.Header.CPUType;
  124. Header.cpusubtype = O.Header.CPUSubType;
  125. Header.filetype = O.Header.FileType;
  126. Header.ncmds = O.Header.NCmds;
  127. Header.sizeofcmds = O.Header.SizeOfCmds;
  128. Header.flags = O.Header.Flags;
  129. Header.reserved = O.Header.Reserved;
  130. if (IsLittleEndian != sys::IsLittleEndianHost)
  131. MachO::swapStruct(Header);
  132. auto HeaderSize =
  133. Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
  134. memcpy(Buf->getBufferStart(), &Header, HeaderSize);
  135. }
  136. void MachOWriter::writeLoadCommands() {
  137. uint8_t *Begin =
  138. reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + headerSize();
  139. for (const LoadCommand &LC : O.LoadCommands) {
  140. // Construct a load command.
  141. MachO::macho_load_command MLC = LC.MachOLoadCommand;
  142. switch (MLC.load_command_data.cmd) {
  143. case MachO::LC_SEGMENT:
  144. if (IsLittleEndian != sys::IsLittleEndianHost)
  145. MachO::swapStruct(MLC.segment_command_data);
  146. memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command));
  147. Begin += sizeof(MachO::segment_command);
  148. for (const std::unique_ptr<Section> &Sec : LC.Sections)
  149. writeSectionInLoadCommand<MachO::section>(*Sec, Begin);
  150. continue;
  151. case MachO::LC_SEGMENT_64:
  152. if (IsLittleEndian != sys::IsLittleEndianHost)
  153. MachO::swapStruct(MLC.segment_command_64_data);
  154. memcpy(Begin, &MLC.segment_command_64_data,
  155. sizeof(MachO::segment_command_64));
  156. Begin += sizeof(MachO::segment_command_64);
  157. for (const std::unique_ptr<Section> &Sec : LC.Sections)
  158. writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin);
  159. continue;
  160. }
  161. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
  162. case MachO::LCName: \
  163. assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
  164. MLC.load_command_data.cmdsize); \
  165. if (IsLittleEndian != sys::IsLittleEndianHost) \
  166. MachO::swapStruct(MLC.LCStruct##_data); \
  167. memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
  168. Begin += sizeof(MachO::LCStruct); \
  169. if (!LC.Payload.empty()) \
  170. memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
  171. Begin += LC.Payload.size(); \
  172. break;
  173. // Copy the load command as it is.
  174. switch (MLC.load_command_data.cmd) {
  175. default:
  176. assert(sizeof(MachO::load_command) + LC.Payload.size() ==
  177. MLC.load_command_data.cmdsize);
  178. if (IsLittleEndian != sys::IsLittleEndianHost)
  179. MachO::swapStruct(MLC.load_command_data);
  180. memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));
  181. Begin += sizeof(MachO::load_command);
  182. if (!LC.Payload.empty())
  183. memcpy(Begin, LC.Payload.data(), LC.Payload.size());
  184. Begin += LC.Payload.size();
  185. break;
  186. #include "llvm/BinaryFormat/MachO.def"
  187. }
  188. }
  189. }
  190. template <typename StructType>
  191. void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) {
  192. StructType Temp;
  193. assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name");
  194. assert(Sec.Sectname.size() <= sizeof(Temp.sectname) &&
  195. "too long section name");
  196. memset(&Temp, 0, sizeof(StructType));
  197. memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size());
  198. memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size());
  199. Temp.addr = Sec.Addr;
  200. Temp.size = Sec.Size;
  201. Temp.offset = Sec.Offset;
  202. Temp.align = Sec.Align;
  203. Temp.reloff = Sec.RelOff;
  204. Temp.nreloc = Sec.NReloc;
  205. Temp.flags = Sec.Flags;
  206. Temp.reserved1 = Sec.Reserved1;
  207. Temp.reserved2 = Sec.Reserved2;
  208. if (IsLittleEndian != sys::IsLittleEndianHost)
  209. MachO::swapStruct(Temp);
  210. memcpy(Out, &Temp, sizeof(StructType));
  211. Out += sizeof(StructType);
  212. }
  213. void MachOWriter::writeSections() {
  214. for (const LoadCommand &LC : O.LoadCommands)
  215. for (const std::unique_ptr<Section> &Sec : LC.Sections) {
  216. if (!Sec->hasValidOffset()) {
  217. assert((Sec->Offset == 0) && "Skipped section's offset must be zero");
  218. assert((Sec->isVirtualSection() || Sec->Size == 0) &&
  219. "Non-zero-fill sections with zero offset must have zero size");
  220. continue;
  221. }
  222. assert(Sec->Offset && "Section offset can not be zero");
  223. assert((Sec->Size == Sec->Content.size()) && "Incorrect section size");
  224. memcpy(Buf->getBufferStart() + Sec->Offset, Sec->Content.data(),
  225. Sec->Content.size());
  226. for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
  227. RelocationInfo RelocInfo = Sec->Relocations[Index];
  228. if (!RelocInfo.Scattered && !RelocInfo.IsAddend) {
  229. const uint32_t SymbolNum = RelocInfo.Extern
  230. ? (*RelocInfo.Symbol)->Index
  231. : (*RelocInfo.Sec)->Index;
  232. RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
  233. }
  234. if (IsLittleEndian != sys::IsLittleEndianHost)
  235. MachO::swapStruct(
  236. reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));
  237. memcpy(Buf->getBufferStart() + Sec->RelOff +
  238. Index * sizeof(MachO::any_relocation_info),
  239. &RelocInfo.Info, sizeof(RelocInfo.Info));
  240. }
  241. }
  242. }
  243. template <typename NListType>
  244. void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out,
  245. uint32_t Nstrx) {
  246. NListType ListEntry;
  247. ListEntry.n_strx = Nstrx;
  248. ListEntry.n_type = SE.n_type;
  249. ListEntry.n_sect = SE.n_sect;
  250. ListEntry.n_desc = SE.n_desc;
  251. ListEntry.n_value = SE.n_value;
  252. if (IsLittleEndian != sys::IsLittleEndianHost)
  253. MachO::swapStruct(ListEntry);
  254. memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
  255. Out += sizeof(NListType);
  256. }
  257. void MachOWriter::writeStringTable() {
  258. if (!O.SymTabCommandIndex)
  259. return;
  260. const MachO::symtab_command &SymTabCommand =
  261. O.LoadCommands[*O.SymTabCommandIndex]
  262. .MachOLoadCommand.symtab_command_data;
  263. uint8_t *StrTable = (uint8_t *)Buf->getBufferStart() + SymTabCommand.stroff;
  264. LayoutBuilder.getStringTableBuilder().write(StrTable);
  265. }
  266. void MachOWriter::writeSymbolTable() {
  267. if (!O.SymTabCommandIndex)
  268. return;
  269. const MachO::symtab_command &SymTabCommand =
  270. O.LoadCommands[*O.SymTabCommandIndex]
  271. .MachOLoadCommand.symtab_command_data;
  272. char *SymTable = (char *)Buf->getBufferStart() + SymTabCommand.symoff;
  273. for (auto &Symbol : O.SymTable.Symbols) {
  274. SymbolEntry *Sym = Symbol.get();
  275. uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name);
  276. if (Is64Bit)
  277. writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx);
  278. else
  279. writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx);
  280. }
  281. }
  282. void MachOWriter::writeRebaseInfo() {
  283. if (!O.DyLdInfoCommandIndex)
  284. return;
  285. const MachO::dyld_info_command &DyLdInfoCommand =
  286. O.LoadCommands[*O.DyLdInfoCommandIndex]
  287. .MachOLoadCommand.dyld_info_command_data;
  288. char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.rebase_off;
  289. assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
  290. "Incorrect rebase opcodes size");
  291. memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
  292. }
  293. void MachOWriter::writeBindInfo() {
  294. if (!O.DyLdInfoCommandIndex)
  295. return;
  296. const MachO::dyld_info_command &DyLdInfoCommand =
  297. O.LoadCommands[*O.DyLdInfoCommandIndex]
  298. .MachOLoadCommand.dyld_info_command_data;
  299. char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.bind_off;
  300. assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
  301. "Incorrect bind opcodes size");
  302. memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
  303. }
  304. void MachOWriter::writeWeakBindInfo() {
  305. if (!O.DyLdInfoCommandIndex)
  306. return;
  307. const MachO::dyld_info_command &DyLdInfoCommand =
  308. O.LoadCommands[*O.DyLdInfoCommandIndex]
  309. .MachOLoadCommand.dyld_info_command_data;
  310. char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.weak_bind_off;
  311. assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
  312. "Incorrect weak bind opcodes size");
  313. memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
  314. }
  315. void MachOWriter::writeLazyBindInfo() {
  316. if (!O.DyLdInfoCommandIndex)
  317. return;
  318. const MachO::dyld_info_command &DyLdInfoCommand =
  319. O.LoadCommands[*O.DyLdInfoCommandIndex]
  320. .MachOLoadCommand.dyld_info_command_data;
  321. char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.lazy_bind_off;
  322. assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
  323. "Incorrect lazy bind opcodes size");
  324. memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
  325. }
  326. void MachOWriter::writeExportInfo() {
  327. if (!O.DyLdInfoCommandIndex)
  328. return;
  329. const MachO::dyld_info_command &DyLdInfoCommand =
  330. O.LoadCommands[*O.DyLdInfoCommandIndex]
  331. .MachOLoadCommand.dyld_info_command_data;
  332. char *Out = (char *)Buf->getBufferStart() + DyLdInfoCommand.export_off;
  333. assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
  334. "Incorrect export trie size");
  335. memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
  336. }
  337. void MachOWriter::writeIndirectSymbolTable() {
  338. if (!O.DySymTabCommandIndex)
  339. return;
  340. const MachO::dysymtab_command &DySymTabCommand =
  341. O.LoadCommands[*O.DySymTabCommandIndex]
  342. .MachOLoadCommand.dysymtab_command_data;
  343. uint32_t *Out =
  344. (uint32_t *)(Buf->getBufferStart() + DySymTabCommand.indirectsymoff);
  345. for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
  346. uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;
  347. if (IsLittleEndian != sys::IsLittleEndianHost)
  348. sys::swapByteOrder(Entry);
  349. *Out++ = Entry;
  350. }
  351. }
  352. void MachOWriter::writeLinkData(std::optional<size_t> LCIndex,
  353. const LinkData &LD) {
  354. if (!LCIndex)
  355. return;
  356. const MachO::linkedit_data_command &LinkEditDataCommand =
  357. O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
  358. char *Out = (char *)Buf->getBufferStart() + LinkEditDataCommand.dataoff;
  359. assert((LinkEditDataCommand.datasize == LD.Data.size()) &&
  360. "Incorrect data size");
  361. memcpy(Out, LD.Data.data(), LD.Data.size());
  362. }
  363. static uint64_t
  364. getSegmentFileOffset(const LoadCommand &TextSegmentLoadCommand) {
  365. const MachO::macho_load_command &MLC =
  366. TextSegmentLoadCommand.MachOLoadCommand;
  367. switch (MLC.load_command_data.cmd) {
  368. case MachO::LC_SEGMENT:
  369. return MLC.segment_command_data.fileoff;
  370. case MachO::LC_SEGMENT_64:
  371. return MLC.segment_command_64_data.fileoff;
  372. default:
  373. return 0;
  374. }
  375. }
  376. static uint64_t getSegmentFileSize(const LoadCommand &TextSegmentLoadCommand) {
  377. const MachO::macho_load_command &MLC =
  378. TextSegmentLoadCommand.MachOLoadCommand;
  379. switch (MLC.load_command_data.cmd) {
  380. case MachO::LC_SEGMENT:
  381. return MLC.segment_command_data.filesize;
  382. case MachO::LC_SEGMENT_64:
  383. return MLC.segment_command_64_data.filesize;
  384. default:
  385. return 0;
  386. }
  387. }
  388. void MachOWriter::writeCodeSignatureData() {
  389. // NOTE: This CodeSignature section behaviour must be kept in sync with that
  390. // performed in LLD's CodeSignatureSection::write /
  391. // CodeSignatureSection::writeHashes. Furthermore, this call must occur only
  392. // after the rest of the binary has already been written to the buffer. This
  393. // is because the buffer is read from to perform the necessary hashing.
  394. // The CodeSignature section is the last section in the MachO binary and
  395. // contains a hash of all content in the binary before it. Since llvm-objcopy
  396. // has likely modified the target binary, the hash must be regenerated
  397. // entirely. To generate this hash, we must read from the start of the binary
  398. // (HashReadStart) to just before the start of the CodeSignature section
  399. // (HashReadEnd).
  400. const CodeSignatureInfo &CodeSignature = LayoutBuilder.getCodeSignature();
  401. uint8_t *BufferStart = reinterpret_cast<uint8_t *>(Buf->getBufferStart());
  402. uint8_t *HashReadStart = BufferStart;
  403. uint8_t *HashReadEnd = BufferStart + CodeSignature.StartOffset;
  404. // The CodeSignature section begins with a header, after which the hashes
  405. // of each page of the binary are written.
  406. uint8_t *HashWriteStart = HashReadEnd + CodeSignature.AllHeadersSize;
  407. uint32_t TextSegmentFileOff = 0;
  408. uint32_t TextSegmentFileSize = 0;
  409. if (O.TextSegmentCommandIndex) {
  410. const LoadCommand &TextSegmentLoadCommand =
  411. O.LoadCommands[*O.TextSegmentCommandIndex];
  412. assert(TextSegmentLoadCommand.MachOLoadCommand.load_command_data.cmd ==
  413. MachO::LC_SEGMENT ||
  414. TextSegmentLoadCommand.MachOLoadCommand.load_command_data.cmd ==
  415. MachO::LC_SEGMENT_64);
  416. assert(StringRef(TextSegmentLoadCommand.MachOLoadCommand
  417. .segment_command_data.segname) == "__TEXT");
  418. TextSegmentFileOff = getSegmentFileOffset(TextSegmentLoadCommand);
  419. TextSegmentFileSize = getSegmentFileSize(TextSegmentLoadCommand);
  420. }
  421. const uint32_t FileNamePad = CodeSignature.AllHeadersSize -
  422. CodeSignature.FixedHeadersSize -
  423. CodeSignature.OutputFileName.size();
  424. // Write code section header.
  425. auto *SuperBlob = reinterpret_cast<MachO::CS_SuperBlob *>(HashReadEnd);
  426. write32be(&SuperBlob->magic, MachO::CSMAGIC_EMBEDDED_SIGNATURE);
  427. write32be(&SuperBlob->length, CodeSignature.Size);
  428. write32be(&SuperBlob->count, 1);
  429. auto *BlobIndex = reinterpret_cast<MachO::CS_BlobIndex *>(&SuperBlob[1]);
  430. write32be(&BlobIndex->type, MachO::CSSLOT_CODEDIRECTORY);
  431. write32be(&BlobIndex->offset, CodeSignature.BlobHeadersSize);
  432. auto *CodeDirectory = reinterpret_cast<MachO::CS_CodeDirectory *>(
  433. HashReadEnd + CodeSignature.BlobHeadersSize);
  434. write32be(&CodeDirectory->magic, MachO::CSMAGIC_CODEDIRECTORY);
  435. write32be(&CodeDirectory->length,
  436. CodeSignature.Size - CodeSignature.BlobHeadersSize);
  437. write32be(&CodeDirectory->version, MachO::CS_SUPPORTSEXECSEG);
  438. write32be(&CodeDirectory->flags, MachO::CS_ADHOC | MachO::CS_LINKER_SIGNED);
  439. write32be(&CodeDirectory->hashOffset,
  440. sizeof(MachO::CS_CodeDirectory) +
  441. CodeSignature.OutputFileName.size() + FileNamePad);
  442. write32be(&CodeDirectory->identOffset, sizeof(MachO::CS_CodeDirectory));
  443. CodeDirectory->nSpecialSlots = 0;
  444. write32be(&CodeDirectory->nCodeSlots, CodeSignature.BlockCount);
  445. write32be(&CodeDirectory->codeLimit, CodeSignature.StartOffset);
  446. CodeDirectory->hashSize = static_cast<uint8_t>(CodeSignature.HashSize);
  447. CodeDirectory->hashType = MachO::kSecCodeSignatureHashSHA256;
  448. CodeDirectory->platform = 0;
  449. CodeDirectory->pageSize = CodeSignature.BlockSizeShift;
  450. CodeDirectory->spare2 = 0;
  451. CodeDirectory->scatterOffset = 0;
  452. CodeDirectory->teamOffset = 0;
  453. CodeDirectory->spare3 = 0;
  454. CodeDirectory->codeLimit64 = 0;
  455. write64be(&CodeDirectory->execSegBase, TextSegmentFileOff);
  456. write64be(&CodeDirectory->execSegLimit, TextSegmentFileSize);
  457. write64be(&CodeDirectory->execSegFlags, O.Header.FileType == MachO::MH_EXECUTE
  458. ? MachO::CS_EXECSEG_MAIN_BINARY
  459. : 0);
  460. auto *Id = reinterpret_cast<char *>(&CodeDirectory[1]);
  461. memcpy(Id, CodeSignature.OutputFileName.begin(),
  462. CodeSignature.OutputFileName.size());
  463. memset(Id + CodeSignature.OutputFileName.size(), 0, FileNamePad);
  464. // Write the hashes.
  465. uint8_t *CurrHashReadPosition = HashReadStart;
  466. uint8_t *CurrHashWritePosition = HashWriteStart;
  467. while (CurrHashReadPosition < HashReadEnd) {
  468. StringRef Block(reinterpret_cast<char *>(CurrHashReadPosition),
  469. std::min(static_cast<size_t>(HashReadEnd
  470. - CurrHashReadPosition),
  471. static_cast<size_t>(CodeSignature.BlockSize)));
  472. SHA256 Hasher;
  473. Hasher.update(Block);
  474. std::array<uint8_t, 32> Hash = Hasher.final();
  475. assert(Hash.size() == CodeSignature.HashSize);
  476. memcpy(CurrHashWritePosition, Hash.data(), CodeSignature.HashSize);
  477. CurrHashReadPosition += CodeSignature.BlockSize;
  478. CurrHashWritePosition += CodeSignature.HashSize;
  479. }
  480. #if defined(__APPLE__)
  481. // This is macOS-specific work-around and makes no sense for any
  482. // other host OS. See https://openradar.appspot.com/FB8914231
  483. //
  484. // The macOS kernel maintains a signature-verification cache to
  485. // quickly validate applications at time of execve(2). The trouble
  486. // is that for the kernel creates the cache entry at the time of the
  487. // mmap(2) call, before we have a chance to write either the code to
  488. // sign or the signature header+hashes. The fix is to invalidate
  489. // all cached data associated with the output file, thus discarding
  490. // the bogus prematurely-cached signature.
  491. msync(BufferStart, CodeSignature.StartOffset + CodeSignature.Size,
  492. MS_INVALIDATE);
  493. #endif
  494. }
  495. void MachOWriter::writeDataInCodeData() {
  496. return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode);
  497. }
  498. void MachOWriter::writeLinkerOptimizationHint() {
  499. return writeLinkData(O.LinkerOptimizationHintCommandIndex,
  500. O.LinkerOptimizationHint);
  501. }
  502. void MachOWriter::writeFunctionStartsData() {
  503. return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts);
  504. }
  505. void MachOWriter::writeDylibCodeSignDRsData() {
  506. return writeLinkData(O.DylibCodeSignDRsIndex, O.DylibCodeSignDRs);
  507. }
  508. void MachOWriter::writeChainedFixupsData() {
  509. return writeLinkData(O.ChainedFixupsCommandIndex, O.ChainedFixups);
  510. }
  511. void MachOWriter::writeExportsTrieData() {
  512. if (!O.ExportsTrieCommandIndex)
  513. return;
  514. const MachO::linkedit_data_command &ExportsTrieCmd =
  515. O.LoadCommands[*O.ExportsTrieCommandIndex]
  516. .MachOLoadCommand.linkedit_data_command_data;
  517. char *Out = (char *)Buf->getBufferStart() + ExportsTrieCmd.dataoff;
  518. assert((ExportsTrieCmd.datasize == O.Exports.Trie.size()) &&
  519. "Incorrect export trie size");
  520. memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
  521. }
  522. void MachOWriter::writeTail() {
  523. typedef void (MachOWriter::*WriteHandlerType)();
  524. typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
  525. SmallVector<WriteOperation, 7> Queue;
  526. if (O.SymTabCommandIndex) {
  527. const MachO::symtab_command &SymTabCommand =
  528. O.LoadCommands[*O.SymTabCommandIndex]
  529. .MachOLoadCommand.symtab_command_data;
  530. if (SymTabCommand.symoff)
  531. Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});
  532. if (SymTabCommand.stroff)
  533. Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});
  534. }
  535. if (O.DyLdInfoCommandIndex) {
  536. const MachO::dyld_info_command &DyLdInfoCommand =
  537. O.LoadCommands[*O.DyLdInfoCommandIndex]
  538. .MachOLoadCommand.dyld_info_command_data;
  539. if (DyLdInfoCommand.rebase_off)
  540. Queue.push_back(
  541. {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});
  542. if (DyLdInfoCommand.bind_off)
  543. Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});
  544. if (DyLdInfoCommand.weak_bind_off)
  545. Queue.push_back(
  546. {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});
  547. if (DyLdInfoCommand.lazy_bind_off)
  548. Queue.push_back(
  549. {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});
  550. if (DyLdInfoCommand.export_off)
  551. Queue.push_back(
  552. {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});
  553. }
  554. if (O.DySymTabCommandIndex) {
  555. const MachO::dysymtab_command &DySymTabCommand =
  556. O.LoadCommands[*O.DySymTabCommandIndex]
  557. .MachOLoadCommand.dysymtab_command_data;
  558. if (DySymTabCommand.indirectsymoff)
  559. Queue.emplace_back(DySymTabCommand.indirectsymoff,
  560. &MachOWriter::writeIndirectSymbolTable);
  561. }
  562. std::initializer_list<std::pair<std::optional<size_t>, WriteHandlerType>>
  563. LinkEditDataCommandWriters = {
  564. {O.CodeSignatureCommandIndex, &MachOWriter::writeCodeSignatureData},
  565. {O.DylibCodeSignDRsIndex, &MachOWriter::writeDylibCodeSignDRsData},
  566. {O.DataInCodeCommandIndex, &MachOWriter::writeDataInCodeData},
  567. {O.LinkerOptimizationHintCommandIndex,
  568. &MachOWriter::writeLinkerOptimizationHint},
  569. {O.FunctionStartsCommandIndex, &MachOWriter::writeFunctionStartsData},
  570. {O.ChainedFixupsCommandIndex, &MachOWriter::writeChainedFixupsData},
  571. {O.ExportsTrieCommandIndex, &MachOWriter::writeExportsTrieData}};
  572. for (const auto &W : LinkEditDataCommandWriters) {
  573. std::optional<size_t> LinkEditDataCommandIndex;
  574. WriteHandlerType WriteHandler;
  575. std::tie(LinkEditDataCommandIndex, WriteHandler) = W;
  576. if (LinkEditDataCommandIndex) {
  577. const MachO::linkedit_data_command &LinkEditDataCommand =
  578. O.LoadCommands[*LinkEditDataCommandIndex]
  579. .MachOLoadCommand.linkedit_data_command_data;
  580. if (LinkEditDataCommand.dataoff)
  581. Queue.emplace_back(LinkEditDataCommand.dataoff, WriteHandler);
  582. }
  583. }
  584. llvm::sort(Queue, llvm::less_first());
  585. for (auto WriteOp : Queue)
  586. (this->*WriteOp.second)();
  587. }
  588. Error MachOWriter::finalize() { return LayoutBuilder.layout(); }
  589. Error MachOWriter::write() {
  590. size_t TotalSize = totalSize();
  591. Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
  592. if (!Buf)
  593. return createStringError(errc::not_enough_memory,
  594. "failed to allocate memory buffer of " +
  595. Twine::utohexstr(TotalSize) + " bytes");
  596. writeHeader();
  597. writeLoadCommands();
  598. writeSections();
  599. writeTail();
  600. // TODO: Implement direct writing to the output stream (without intermediate
  601. // memory buffer Buf).
  602. Out.write(Buf->getBufferStart(), Buf->getBufferSize());
  603. return Error::success();
  604. }