MachOWriter.cpp 29 KB

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