MachOUniversalWriter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //===- MachOUniversalWriter.cpp - MachO universal binary writer---*- 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. //
  9. // Defines the Slice class and writeUniversalBinary function for writing a MachO
  10. // universal binary file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/MachOUniversalWriter.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/Object/Archive.h"
  16. #include "llvm/Object/Binary.h"
  17. #include "llvm/Object/Error.h"
  18. #include "llvm/Object/IRObjectFile.h"
  19. #include "llvm/Object/MachO.h"
  20. #include "llvm/Object/MachOUniversal.h"
  21. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  22. using namespace llvm;
  23. using namespace object;
  24. // For compatibility with cctools lipo, a file's alignment is calculated as the
  25. // minimum aligment of all segments. For object files, the file's alignment is
  26. // the maximum alignment of its sections.
  27. static uint32_t calculateFileAlignment(const MachOObjectFile &O) {
  28. uint32_t P2CurrentAlignment;
  29. uint32_t P2MinAlignment = MachOUniversalBinary::MaxSectionAlignment;
  30. const bool Is64Bit = O.is64Bit();
  31. for (const auto &LC : O.load_commands()) {
  32. if (LC.C.cmd != (Is64Bit ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT))
  33. continue;
  34. if (O.getHeader().filetype == MachO::MH_OBJECT) {
  35. unsigned NumberOfSections =
  36. (Is64Bit ? O.getSegment64LoadCommand(LC).nsects
  37. : O.getSegmentLoadCommand(LC).nsects);
  38. P2CurrentAlignment = NumberOfSections ? 2 : P2MinAlignment;
  39. for (unsigned SI = 0; SI < NumberOfSections; ++SI) {
  40. P2CurrentAlignment = std::max(P2CurrentAlignment,
  41. (Is64Bit ? O.getSection64(LC, SI).align
  42. : O.getSection(LC, SI).align));
  43. }
  44. } else {
  45. P2CurrentAlignment =
  46. countTrailingZeros(Is64Bit ? O.getSegment64LoadCommand(LC).vmaddr
  47. : O.getSegmentLoadCommand(LC).vmaddr);
  48. }
  49. P2MinAlignment = std::min(P2MinAlignment, P2CurrentAlignment);
  50. }
  51. // return a value >= 4 byte aligned, and less than MachO MaxSectionAlignment
  52. return std::max(
  53. static_cast<uint32_t>(2),
  54. std::min(P2MinAlignment, static_cast<uint32_t>(
  55. MachOUniversalBinary::MaxSectionAlignment)));
  56. }
  57. static uint32_t calculateAlignment(const MachOObjectFile &ObjectFile) {
  58. switch (ObjectFile.getHeader().cputype) {
  59. case MachO::CPU_TYPE_I386:
  60. case MachO::CPU_TYPE_X86_64:
  61. case MachO::CPU_TYPE_POWERPC:
  62. case MachO::CPU_TYPE_POWERPC64:
  63. return 12; // log2 value of page size(4k) for x86 and PPC
  64. case MachO::CPU_TYPE_ARM:
  65. case MachO::CPU_TYPE_ARM64:
  66. case MachO::CPU_TYPE_ARM64_32:
  67. return 14; // log2 value of page size(16k) for Darwin ARM
  68. default:
  69. return calculateFileAlignment(ObjectFile);
  70. }
  71. }
  72. Slice::Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
  73. std::string ArchName, uint32_t Align)
  74. : B(&A), CPUType(CPUType), CPUSubType(CPUSubType),
  75. ArchName(std::move(ArchName)), P2Alignment(Align) {}
  76. Slice::Slice(const MachOObjectFile &O, uint32_t Align)
  77. : B(&O), CPUType(O.getHeader().cputype),
  78. CPUSubType(O.getHeader().cpusubtype),
  79. ArchName(std::string(O.getArchTriple().getArchName())),
  80. P2Alignment(Align) {}
  81. Slice::Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
  82. std::string ArchName, uint32_t Align)
  83. : B(&IRO), CPUType(CPUType), CPUSubType(CPUSubType),
  84. ArchName(std::move(ArchName)), P2Alignment(Align) {}
  85. Slice::Slice(const MachOObjectFile &O) : Slice(O, calculateAlignment(O)) {}
  86. using MachoCPUTy = std::pair<unsigned, unsigned>;
  87. static Expected<MachoCPUTy> getMachoCPUFromTriple(Triple TT) {
  88. auto CPU = std::make_pair(MachO::getCPUType(TT), MachO::getCPUSubType(TT));
  89. if (!CPU.first) {
  90. return CPU.first.takeError();
  91. }
  92. if (!CPU.second) {
  93. return CPU.second.takeError();
  94. }
  95. return std::make_pair(*CPU.first, *CPU.second);
  96. }
  97. static Expected<MachoCPUTy> getMachoCPUFromTriple(StringRef TT) {
  98. return getMachoCPUFromTriple(Triple{TT});
  99. }
  100. Expected<Slice> Slice::create(const Archive &A, LLVMContext *LLVMCtx) {
  101. Error Err = Error::success();
  102. std::unique_ptr<MachOObjectFile> MFO = nullptr;
  103. std::unique_ptr<IRObjectFile> IRFO = nullptr;
  104. for (const Archive::Child &Child : A.children(Err)) {
  105. Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(LLVMCtx);
  106. if (!ChildOrErr)
  107. return createFileError(A.getFileName(), ChildOrErr.takeError());
  108. Binary *Bin = ChildOrErr.get().get();
  109. if (Bin->isMachOUniversalBinary())
  110. return createStringError(std::errc::invalid_argument,
  111. ("archive member " + Bin->getFileName() +
  112. " is a fat file (not allowed in an archive)")
  113. .str()
  114. .c_str());
  115. if (Bin->isMachO()) {
  116. MachOObjectFile *O = cast<MachOObjectFile>(Bin);
  117. if (IRFO) {
  118. return createStringError(
  119. std::errc::invalid_argument,
  120. "archive member %s is a MachO, while previous archive member "
  121. "%s was an IR LLVM object",
  122. O->getFileName().str().c_str(), IRFO->getFileName().str().c_str());
  123. }
  124. if (MFO &&
  125. std::tie(MFO->getHeader().cputype, MFO->getHeader().cpusubtype) !=
  126. std::tie(O->getHeader().cputype, O->getHeader().cpusubtype)) {
  127. return createStringError(
  128. std::errc::invalid_argument,
  129. ("archive member " + O->getFileName() + " cputype (" +
  130. Twine(O->getHeader().cputype) + ") and cpusubtype(" +
  131. Twine(O->getHeader().cpusubtype) +
  132. ") does not match previous archive members cputype (" +
  133. Twine(MFO->getHeader().cputype) + ") and cpusubtype(" +
  134. Twine(MFO->getHeader().cpusubtype) +
  135. ") (all members must match) " + MFO->getFileName())
  136. .str()
  137. .c_str());
  138. }
  139. if (!MFO) {
  140. ChildOrErr.get().release();
  141. MFO.reset(O);
  142. }
  143. } else if (Bin->isIR()) {
  144. IRObjectFile *O = cast<IRObjectFile>(Bin);
  145. if (MFO) {
  146. return createStringError(std::errc::invalid_argument,
  147. "archive member '%s' is an LLVM IR object, "
  148. "while previous archive member "
  149. "'%s' was a MachO",
  150. O->getFileName().str().c_str(),
  151. MFO->getFileName().str().c_str());
  152. }
  153. if (IRFO) {
  154. Expected<MachoCPUTy> CPUO = getMachoCPUFromTriple(O->getTargetTriple());
  155. Expected<MachoCPUTy> CPUFO =
  156. getMachoCPUFromTriple(IRFO->getTargetTriple());
  157. if (!CPUO)
  158. return CPUO.takeError();
  159. if (!CPUFO)
  160. return CPUFO.takeError();
  161. if (*CPUO != *CPUFO) {
  162. return createStringError(
  163. std::errc::invalid_argument,
  164. ("archive member " + O->getFileName() + " cputype (" +
  165. Twine(CPUO->first) + ") and cpusubtype(" + Twine(CPUO->second) +
  166. ") does not match previous archive members cputype (" +
  167. Twine(CPUFO->first) + ") and cpusubtype(" +
  168. Twine(CPUFO->second) + ") (all members must match) " +
  169. IRFO->getFileName())
  170. .str()
  171. .c_str());
  172. }
  173. } else {
  174. ChildOrErr.get().release();
  175. IRFO.reset(O);
  176. }
  177. } else
  178. return createStringError(std::errc::invalid_argument,
  179. ("archive member " + Bin->getFileName() +
  180. " is neither a MachO file or an LLVM IR file "
  181. "(not allowed in an archive)")
  182. .str()
  183. .c_str());
  184. }
  185. if (Err)
  186. return createFileError(A.getFileName(), std::move(Err));
  187. if (!MFO && !IRFO)
  188. return createStringError(
  189. std::errc::invalid_argument,
  190. ("empty archive with no architecture specification: " +
  191. A.getFileName() + " (can't determine architecture for it)")
  192. .str()
  193. .c_str());
  194. if (MFO) {
  195. Slice ArchiveSlice(*(MFO.get()), MFO->is64Bit() ? 3 : 2);
  196. ArchiveSlice.B = &A;
  197. return ArchiveSlice;
  198. }
  199. // For IR objects
  200. Expected<Slice> ArchiveSliceOrErr = Slice::create(*IRFO, 0);
  201. if (!ArchiveSliceOrErr)
  202. return createFileError(A.getFileName(), ArchiveSliceOrErr.takeError());
  203. auto &ArchiveSlice = ArchiveSliceOrErr.get();
  204. ArchiveSlice.B = &A;
  205. return std::move(ArchiveSlice);
  206. }
  207. Expected<Slice> Slice::create(const IRObjectFile &IRO, uint32_t Align) {
  208. Expected<MachoCPUTy> CPUOrErr = getMachoCPUFromTriple(IRO.getTargetTriple());
  209. if (!CPUOrErr)
  210. return CPUOrErr.takeError();
  211. unsigned CPUType, CPUSubType;
  212. std::tie(CPUType, CPUSubType) = CPUOrErr.get();
  213. // We don't directly use the architecture name of the target triple T, as,
  214. // for instance, thumb is treated as ARM by the MachOUniversal object.
  215. std::string ArchName(
  216. MachOObjectFile::getArchTriple(CPUType, CPUSubType).getArchName());
  217. return Slice{IRO, CPUType, CPUSubType, std::move(ArchName), Align};
  218. }
  219. static Expected<SmallVector<MachO::fat_arch, 2>>
  220. buildFatArchList(ArrayRef<Slice> Slices) {
  221. SmallVector<MachO::fat_arch, 2> FatArchList;
  222. uint64_t Offset =
  223. sizeof(MachO::fat_header) + Slices.size() * sizeof(MachO::fat_arch);
  224. for (const auto &S : Slices) {
  225. Offset = alignTo(Offset, 1ull << S.getP2Alignment());
  226. if (Offset > UINT32_MAX)
  227. return createStringError(
  228. std::errc::invalid_argument,
  229. ("fat file too large to be created because the offset "
  230. "field in struct fat_arch is only 32-bits and the offset " +
  231. Twine(Offset) + " for " + S.getBinary()->getFileName() +
  232. " for architecture " + S.getArchString() + "exceeds that.")
  233. .str()
  234. .c_str());
  235. MachO::fat_arch FatArch;
  236. FatArch.cputype = S.getCPUType();
  237. FatArch.cpusubtype = S.getCPUSubType();
  238. FatArch.offset = Offset;
  239. FatArch.size = S.getBinary()->getMemoryBufferRef().getBufferSize();
  240. FatArch.align = S.getP2Alignment();
  241. Offset += FatArch.size;
  242. FatArchList.push_back(FatArch);
  243. }
  244. return FatArchList;
  245. }
  246. static Error writeUniversalBinaryToStream(ArrayRef<Slice> Slices,
  247. raw_ostream &Out) {
  248. MachO::fat_header FatHeader;
  249. FatHeader.magic = MachO::FAT_MAGIC;
  250. FatHeader.nfat_arch = Slices.size();
  251. Expected<SmallVector<MachO::fat_arch, 2>> FatArchListOrErr =
  252. buildFatArchList(Slices);
  253. if (!FatArchListOrErr)
  254. return FatArchListOrErr.takeError();
  255. SmallVector<MachO::fat_arch, 2> FatArchList = *FatArchListOrErr;
  256. if (sys::IsLittleEndianHost)
  257. MachO::swapStruct(FatHeader);
  258. Out.write(reinterpret_cast<const char *>(&FatHeader),
  259. sizeof(MachO::fat_header));
  260. if (sys::IsLittleEndianHost)
  261. for (MachO::fat_arch &FA : FatArchList)
  262. MachO::swapStruct(FA);
  263. Out.write(reinterpret_cast<const char *>(FatArchList.data()),
  264. sizeof(MachO::fat_arch) * FatArchList.size());
  265. if (sys::IsLittleEndianHost)
  266. for (MachO::fat_arch &FA : FatArchList)
  267. MachO::swapStruct(FA);
  268. size_t Offset =
  269. sizeof(MachO::fat_header) + sizeof(MachO::fat_arch) * FatArchList.size();
  270. for (size_t Index = 0, Size = Slices.size(); Index < Size; ++Index) {
  271. MemoryBufferRef BufferRef = Slices[Index].getBinary()->getMemoryBufferRef();
  272. assert((Offset <= FatArchList[Index].offset) && "Incorrect slice offset");
  273. Out.write_zeros(FatArchList[Index].offset - Offset);
  274. Out.write(BufferRef.getBufferStart(), BufferRef.getBufferSize());
  275. Offset = FatArchList[Index].offset + BufferRef.getBufferSize();
  276. }
  277. Out.flush();
  278. return Error::success();
  279. }
  280. Error object::writeUniversalBinary(ArrayRef<Slice> Slices,
  281. StringRef OutputFileName) {
  282. const bool IsExecutable = any_of(Slices, [](Slice S) {
  283. return sys::fs::can_execute(S.getBinary()->getFileName());
  284. });
  285. unsigned Mode = sys::fs::all_read | sys::fs::all_write;
  286. if (IsExecutable)
  287. Mode |= sys::fs::all_exe;
  288. Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
  289. OutputFileName + ".temp-universal-%%%%%%", Mode);
  290. if (!Temp)
  291. return Temp.takeError();
  292. raw_fd_ostream Out(Temp->FD, false);
  293. if (Error E = writeUniversalBinaryToStream(Slices, Out)) {
  294. if (Error DiscardError = Temp->discard())
  295. return joinErrors(std::move(E), std::move(DiscardError));
  296. return E;
  297. }
  298. return Temp->keep(OutputFileName);
  299. }
  300. Expected<std::unique_ptr<MemoryBuffer>>
  301. object::writeUniversalBinaryToBuffer(ArrayRef<Slice> Slices) {
  302. SmallVector<char, 0> Buffer;
  303. raw_svector_ostream Out(Buffer);
  304. if (Error E = writeUniversalBinaryToStream(Slices, Out))
  305. return std::move(E);
  306. return std::make_unique<SmallVectorMemoryBuffer>(std::move(Buffer));
  307. }