llvm-lipo.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. //===-- llvm-lipo.cpp - a tool for manipulating universal binaries --------===//
  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. // A utility for creating / splitting / inspecting universal binaries.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/BinaryFormat/MachO.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Object/IRObjectFile.h"
  19. #include "llvm/Object/MachO.h"
  20. #include "llvm/Object/MachOUniversal.h"
  21. #include "llvm/Object/MachOUniversalWriter.h"
  22. #include "llvm/Object/ObjectFile.h"
  23. #include "llvm/Option/Arg.h"
  24. #include "llvm/Option/ArgList.h"
  25. #include "llvm/Support/CommandLine.h"
  26. #include "llvm/Support/Error.h"
  27. #include "llvm/Support/FileOutputBuffer.h"
  28. #include "llvm/Support/InitLLVM.h"
  29. #include "llvm/Support/WithColor.h"
  30. #include "llvm/TextAPI/Architecture.h"
  31. using namespace llvm;
  32. using namespace llvm::object;
  33. static const StringRef ToolName = "llvm-lipo";
  34. static LLVMContext LLVMCtx;
  35. [[noreturn]] static void reportError(Twine Message) {
  36. WithColor::error(errs(), ToolName) << Message << "\n";
  37. errs().flush();
  38. exit(EXIT_FAILURE);
  39. }
  40. [[noreturn]] static void reportError(Error E) {
  41. assert(E);
  42. std::string Buf;
  43. raw_string_ostream OS(Buf);
  44. logAllUnhandledErrors(std::move(E), OS);
  45. OS.flush();
  46. reportError(Buf);
  47. }
  48. [[noreturn]] static void reportError(StringRef File, Error E) {
  49. assert(E);
  50. std::string Buf;
  51. raw_string_ostream OS(Buf);
  52. logAllUnhandledErrors(std::move(E), OS);
  53. OS.flush();
  54. WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
  55. exit(EXIT_FAILURE);
  56. }
  57. namespace {
  58. enum LipoID {
  59. LIPO_INVALID = 0, // This is not an option ID.
  60. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  61. HELPTEXT, METAVAR, VALUES) \
  62. LIPO_##ID,
  63. #include "LipoOpts.inc"
  64. #undef OPTION
  65. };
  66. // LipoInfoTable below references LIPO_##PREFIX. OptionGroup has prefix nullptr.
  67. const char *const *LIPO_nullptr = nullptr;
  68. #define PREFIX(NAME, VALUE) const char *const LIPO_##NAME[] = VALUE;
  69. #include "LipoOpts.inc"
  70. #undef PREFIX
  71. const opt::OptTable::Info LipoInfoTable[] = {
  72. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  73. HELPTEXT, METAVAR, VALUES) \
  74. {LIPO_##PREFIX, NAME, HELPTEXT, \
  75. METAVAR, LIPO_##ID, opt::Option::KIND##Class, \
  76. PARAM, FLAGS, LIPO_##GROUP, \
  77. LIPO_##ALIAS, ALIASARGS, VALUES},
  78. #include "LipoOpts.inc"
  79. #undef OPTION
  80. };
  81. class LipoOptTable : public opt::OptTable {
  82. public:
  83. LipoOptTable() : OptTable(LipoInfoTable) {}
  84. };
  85. enum class LipoAction {
  86. PrintArchs,
  87. PrintInfo,
  88. VerifyArch,
  89. ThinArch,
  90. ExtractArch,
  91. CreateUniversal,
  92. ReplaceArch,
  93. };
  94. struct InputFile {
  95. Optional<StringRef> ArchType;
  96. StringRef FileName;
  97. };
  98. struct Config {
  99. SmallVector<InputFile, 1> InputFiles;
  100. SmallVector<std::string, 1> VerifyArchList;
  101. SmallVector<InputFile, 1> ReplacementFiles;
  102. StringMap<const uint32_t> SegmentAlignments;
  103. std::string ArchType;
  104. std::string OutputFile;
  105. LipoAction ActionToPerform;
  106. };
  107. static Slice createSliceFromArchive(const Archive &A) {
  108. Expected<Slice> ArchiveOrSlice = Slice::create(A, &LLVMCtx);
  109. if (!ArchiveOrSlice)
  110. reportError(A.getFileName(), ArchiveOrSlice.takeError());
  111. return *ArchiveOrSlice;
  112. }
  113. static Slice createSliceFromIR(const IRObjectFile &IRO, unsigned Align) {
  114. Expected<Slice> IROrErr = Slice::create(IRO, Align);
  115. if (!IROrErr)
  116. reportError(IRO.getFileName(), IROrErr.takeError());
  117. return *IROrErr;
  118. }
  119. } // end namespace
  120. static void validateArchitectureName(StringRef ArchitectureName) {
  121. if (!MachOObjectFile::isValidArch(ArchitectureName)) {
  122. std::string Buf;
  123. raw_string_ostream OS(Buf);
  124. OS << "Invalid architecture: " << ArchitectureName
  125. << "\nValid architecture names are:";
  126. for (auto arch : MachOObjectFile::getValidArchs())
  127. OS << " " << arch;
  128. reportError(OS.str());
  129. }
  130. }
  131. static Config parseLipoOptions(ArrayRef<const char *> ArgsArr) {
  132. Config C;
  133. LipoOptTable T;
  134. unsigned MissingArgumentIndex, MissingArgumentCount;
  135. opt::InputArgList InputArgs =
  136. T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
  137. if (MissingArgumentCount)
  138. reportError("missing argument to " +
  139. StringRef(InputArgs.getArgString(MissingArgumentIndex)) +
  140. " option");
  141. if (InputArgs.size() == 0) {
  142. // printHelp does not accept Twine.
  143. T.printHelp(errs(), "llvm-lipo input[s] option[s]", "llvm-lipo");
  144. exit(EXIT_FAILURE);
  145. }
  146. if (InputArgs.hasArg(LIPO_help)) {
  147. // printHelp does not accept Twine.
  148. T.printHelp(outs(), "llvm-lipo input[s] option[s]", "llvm-lipo");
  149. exit(EXIT_SUCCESS);
  150. }
  151. if (InputArgs.hasArg(LIPO_version)) {
  152. outs() << ToolName + "\n";
  153. cl::PrintVersionMessage();
  154. exit(EXIT_SUCCESS);
  155. }
  156. for (auto Arg : InputArgs.filtered(LIPO_UNKNOWN))
  157. reportError("unknown argument '" + Arg->getAsString(InputArgs) + "'");
  158. for (auto Arg : InputArgs.filtered(LIPO_INPUT))
  159. C.InputFiles.push_back({None, Arg->getValue()});
  160. for (auto Arg : InputArgs.filtered(LIPO_arch)) {
  161. validateArchitectureName(Arg->getValue(0));
  162. if (!Arg->getValue(1))
  163. reportError(
  164. "arch is missing an argument: expects -arch arch_type file_name");
  165. C.InputFiles.push_back({StringRef(Arg->getValue(0)), Arg->getValue(1)});
  166. }
  167. if (C.InputFiles.empty())
  168. reportError("at least one input file should be specified");
  169. if (InputArgs.hasArg(LIPO_output))
  170. C.OutputFile = std::string(InputArgs.getLastArgValue(LIPO_output));
  171. for (auto Segalign : InputArgs.filtered(LIPO_segalign)) {
  172. if (!Segalign->getValue(1))
  173. reportError("segalign is missing an argument: expects -segalign "
  174. "arch_type alignment_value");
  175. validateArchitectureName(Segalign->getValue(0));
  176. uint32_t AlignmentValue;
  177. if (!to_integer<uint32_t>(Segalign->getValue(1), AlignmentValue, 16))
  178. reportError("argument to -segalign <arch_type> " +
  179. Twine(Segalign->getValue(1)) +
  180. " (hex) is not a proper hexadecimal number");
  181. if (!isPowerOf2_32(AlignmentValue))
  182. reportError("argument to -segalign <arch_type> " +
  183. Twine(Segalign->getValue(1)) +
  184. " (hex) must be a non-zero power of two");
  185. if (Log2_32(AlignmentValue) > MachOUniversalBinary::MaxSectionAlignment)
  186. reportError(
  187. "argument to -segalign <arch_type> " + Twine(Segalign->getValue(1)) +
  188. " (hex) must be less than or equal to the maximum section align 2^" +
  189. Twine(MachOUniversalBinary::MaxSectionAlignment));
  190. auto Entry = C.SegmentAlignments.try_emplace(Segalign->getValue(0),
  191. Log2_32(AlignmentValue));
  192. if (!Entry.second)
  193. reportError("-segalign " + Twine(Segalign->getValue(0)) +
  194. " <alignment_value> specified multiple times: " +
  195. Twine(1 << Entry.first->second) + ", " +
  196. Twine(AlignmentValue));
  197. }
  198. SmallVector<opt::Arg *, 1> ActionArgs(InputArgs.filtered(LIPO_action_group));
  199. if (ActionArgs.empty())
  200. reportError("at least one action should be specified");
  201. // errors if multiple actions specified other than replace
  202. // multiple replace flags may be specified, as long as they are not mixed with
  203. // other action flags
  204. auto ReplacementArgsRange = InputArgs.filtered(LIPO_replace);
  205. if (ActionArgs.size() > 1 &&
  206. ActionArgs.size() !=
  207. static_cast<size_t>(std::distance(ReplacementArgsRange.begin(),
  208. ReplacementArgsRange.end()))) {
  209. std::string Buf;
  210. raw_string_ostream OS(Buf);
  211. OS << "only one of the following actions can be specified:";
  212. for (auto Arg : ActionArgs)
  213. OS << " " << Arg->getSpelling();
  214. reportError(OS.str());
  215. }
  216. switch (ActionArgs[0]->getOption().getID()) {
  217. case LIPO_verify_arch:
  218. for (auto A : InputArgs.getAllArgValues(LIPO_verify_arch))
  219. C.VerifyArchList.push_back(A);
  220. if (C.VerifyArchList.empty())
  221. reportError(
  222. "verify_arch requires at least one architecture to be specified");
  223. if (C.InputFiles.size() > 1)
  224. reportError("verify_arch expects a single input file");
  225. C.ActionToPerform = LipoAction::VerifyArch;
  226. return C;
  227. case LIPO_archs:
  228. if (C.InputFiles.size() > 1)
  229. reportError("archs expects a single input file");
  230. C.ActionToPerform = LipoAction::PrintArchs;
  231. return C;
  232. case LIPO_info:
  233. C.ActionToPerform = LipoAction::PrintInfo;
  234. return C;
  235. case LIPO_thin:
  236. if (C.InputFiles.size() > 1)
  237. reportError("thin expects a single input file");
  238. if (C.OutputFile.empty())
  239. reportError("thin expects a single output file");
  240. C.ArchType = ActionArgs[0]->getValue();
  241. validateArchitectureName(C.ArchType);
  242. C.ActionToPerform = LipoAction::ThinArch;
  243. return C;
  244. case LIPO_extract:
  245. if (C.InputFiles.size() > 1)
  246. reportError("extract expects a single input file");
  247. if (C.OutputFile.empty())
  248. reportError("extract expects a single output file");
  249. C.ArchType = ActionArgs[0]->getValue();
  250. validateArchitectureName(C.ArchType);
  251. C.ActionToPerform = LipoAction::ExtractArch;
  252. return C;
  253. case LIPO_create:
  254. if (C.OutputFile.empty())
  255. reportError("create expects a single output file to be specified");
  256. C.ActionToPerform = LipoAction::CreateUniversal;
  257. return C;
  258. case LIPO_replace:
  259. for (auto Action : ActionArgs) {
  260. if (!Action->getValue(1))
  261. reportError(
  262. "replace is missing an argument: expects -replace arch_type "
  263. "file_name");
  264. validateArchitectureName(Action->getValue(0));
  265. C.ReplacementFiles.push_back(
  266. {StringRef(Action->getValue(0)), Action->getValue(1)});
  267. }
  268. if (C.OutputFile.empty())
  269. reportError("replace expects a single output file to be specified");
  270. if (C.InputFiles.size() > 1)
  271. reportError("replace expects a single input file");
  272. C.ActionToPerform = LipoAction::ReplaceArch;
  273. return C;
  274. default:
  275. reportError("llvm-lipo action unspecified");
  276. }
  277. }
  278. static SmallVector<OwningBinary<Binary>, 1>
  279. readInputBinaries(ArrayRef<InputFile> InputFiles) {
  280. SmallVector<OwningBinary<Binary>, 1> InputBinaries;
  281. for (const InputFile &IF : InputFiles) {
  282. Expected<OwningBinary<Binary>> BinaryOrErr =
  283. createBinary(IF.FileName, &LLVMCtx);
  284. if (!BinaryOrErr)
  285. reportError(IF.FileName, BinaryOrErr.takeError());
  286. const Binary *B = BinaryOrErr->getBinary();
  287. if (!B->isArchive() && !B->isMachO() && !B->isMachOUniversalBinary() &&
  288. !B->isIR())
  289. reportError("File " + IF.FileName + " has unsupported binary format");
  290. if (IF.ArchType && (B->isMachO() || B->isArchive() || B->isIR())) {
  291. const auto S = B->isMachO()
  292. ? Slice(*cast<MachOObjectFile>(B))
  293. : B->isArchive()
  294. ? createSliceFromArchive(*cast<Archive>(B))
  295. : createSliceFromIR(*cast<IRObjectFile>(B), 0);
  296. const auto SpecifiedCPUType = MachO::getCPUTypeFromArchitecture(
  297. MachO::getArchitectureFromName(
  298. Triple(*IF.ArchType).getArchName()))
  299. .first;
  300. // For compatibility with cctools' lipo the comparison is relaxed just to
  301. // checking cputypes.
  302. if (S.getCPUType() != SpecifiedCPUType)
  303. reportError("specified architecture: " + *IF.ArchType +
  304. " for file: " + B->getFileName() +
  305. " does not match the file's architecture (" +
  306. S.getArchString() + ")");
  307. }
  308. InputBinaries.push_back(std::move(*BinaryOrErr));
  309. }
  310. return InputBinaries;
  311. }
  312. [[noreturn]] static void
  313. verifyArch(ArrayRef<OwningBinary<Binary>> InputBinaries,
  314. ArrayRef<std::string> VerifyArchList) {
  315. assert(!VerifyArchList.empty() &&
  316. "The list of architectures should be non-empty");
  317. assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
  318. for (StringRef Arch : VerifyArchList)
  319. validateArchitectureName(Arch);
  320. if (auto UO =
  321. dyn_cast<MachOUniversalBinary>(InputBinaries.front().getBinary())) {
  322. for (StringRef Arch : VerifyArchList) {
  323. Expected<MachOUniversalBinary::ObjectForArch> Obj =
  324. UO->getObjectForArch(Arch);
  325. if (!Obj)
  326. exit(EXIT_FAILURE);
  327. }
  328. } else if (auto O =
  329. dyn_cast<MachOObjectFile>(InputBinaries.front().getBinary())) {
  330. const Triple::ArchType ObjectArch = O->getArch();
  331. for (StringRef Arch : VerifyArchList)
  332. if (ObjectArch != Triple(Arch).getArch())
  333. exit(EXIT_FAILURE);
  334. } else {
  335. llvm_unreachable("Unexpected binary format");
  336. }
  337. exit(EXIT_SUCCESS);
  338. }
  339. static void printBinaryArchs(const Binary *Binary, raw_ostream &OS) {
  340. // Prints trailing space for compatibility with cctools lipo.
  341. if (auto UO = dyn_cast<MachOUniversalBinary>(Binary)) {
  342. for (const auto &O : UO->objects()) {
  343. // Order here is important, because both MachOObjectFile and
  344. // IRObjectFile can be created with a binary that has embedded bitcode.
  345. Expected<std::unique_ptr<MachOObjectFile>> MachOObjOrError =
  346. O.getAsObjectFile();
  347. if (MachOObjOrError) {
  348. OS << Slice(*(MachOObjOrError->get())).getArchString() << " ";
  349. continue;
  350. }
  351. Expected<std::unique_ptr<IRObjectFile>> IROrError =
  352. O.getAsIRObject(LLVMCtx);
  353. if (IROrError) {
  354. consumeError(MachOObjOrError.takeError());
  355. Expected<Slice> SliceOrErr = Slice::create(**IROrError, O.getAlign());
  356. if (!SliceOrErr) {
  357. reportError(Binary->getFileName(), SliceOrErr.takeError());
  358. continue;
  359. }
  360. OS << SliceOrErr.get().getArchString() << " ";
  361. continue;
  362. }
  363. Expected<std::unique_ptr<Archive>> ArchiveOrError = O.getAsArchive();
  364. if (ArchiveOrError) {
  365. consumeError(MachOObjOrError.takeError());
  366. consumeError(IROrError.takeError());
  367. OS << createSliceFromArchive(**ArchiveOrError).getArchString() << " ";
  368. continue;
  369. }
  370. consumeError(ArchiveOrError.takeError());
  371. reportError(Binary->getFileName(), MachOObjOrError.takeError());
  372. reportError(Binary->getFileName(), IROrError.takeError());
  373. }
  374. OS << "\n";
  375. return;
  376. }
  377. if (const auto *MachO = dyn_cast<MachOObjectFile>(Binary)) {
  378. OS << Slice(*MachO).getArchString() << " \n";
  379. return;
  380. }
  381. // This should be always the case, as this is tested in readInputBinaries
  382. const auto *IR = cast<IRObjectFile>(Binary);
  383. Expected<Slice> SliceOrErr = createSliceFromIR(*IR, 0);
  384. if (!SliceOrErr)
  385. reportError(IR->getFileName(), SliceOrErr.takeError());
  386. OS << SliceOrErr->getArchString() << " \n";
  387. }
  388. [[noreturn]] static void
  389. printArchs(ArrayRef<OwningBinary<Binary>> InputBinaries) {
  390. assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
  391. printBinaryArchs(InputBinaries.front().getBinary(), outs());
  392. exit(EXIT_SUCCESS);
  393. }
  394. [[noreturn]] static void
  395. printInfo(ArrayRef<OwningBinary<Binary>> InputBinaries) {
  396. // Group universal and thin files together for compatibility with cctools lipo
  397. for (auto &IB : InputBinaries) {
  398. const Binary *Binary = IB.getBinary();
  399. if (Binary->isMachOUniversalBinary()) {
  400. outs() << "Architectures in the fat file: " << Binary->getFileName()
  401. << " are: ";
  402. printBinaryArchs(Binary, outs());
  403. }
  404. }
  405. for (auto &IB : InputBinaries) {
  406. const Binary *Binary = IB.getBinary();
  407. if (!Binary->isMachOUniversalBinary()) {
  408. assert(Binary->isMachO() && "expected MachO binary");
  409. outs() << "Non-fat file: " << Binary->getFileName()
  410. << " is architecture: ";
  411. printBinaryArchs(Binary, outs());
  412. }
  413. }
  414. exit(EXIT_SUCCESS);
  415. }
  416. [[noreturn]] static void thinSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,
  417. StringRef ArchType,
  418. StringRef OutputFileName) {
  419. assert(!ArchType.empty() && "The architecture type should be non-empty");
  420. assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
  421. assert(!OutputFileName.empty() && "Thin expects a single output file");
  422. if (InputBinaries.front().getBinary()->isMachO()) {
  423. reportError("input file " +
  424. InputBinaries.front().getBinary()->getFileName() +
  425. " must be a fat file when the -thin option is specified");
  426. exit(EXIT_FAILURE);
  427. }
  428. auto *UO = cast<MachOUniversalBinary>(InputBinaries.front().getBinary());
  429. Expected<std::unique_ptr<MachOObjectFile>> Obj =
  430. UO->getMachOObjectForArch(ArchType);
  431. Expected<std::unique_ptr<IRObjectFile>> IRObj =
  432. UO->getIRObjectForArch(ArchType, LLVMCtx);
  433. Expected<std::unique_ptr<Archive>> Ar = UO->getArchiveForArch(ArchType);
  434. if (!Obj && !IRObj && !Ar)
  435. reportError("fat input file " + UO->getFileName() +
  436. " does not contain the specified architecture " + ArchType +
  437. " to thin it to");
  438. Binary *B;
  439. // Order here is important, because both Obj and IRObj will be valid with a
  440. // binary that has embedded bitcode.
  441. if (Obj)
  442. B = Obj->get();
  443. else if (IRObj)
  444. B = IRObj->get();
  445. else
  446. B = Ar->get();
  447. Expected<std::unique_ptr<FileOutputBuffer>> OutFileOrError =
  448. FileOutputBuffer::create(OutputFileName,
  449. B->getMemoryBufferRef().getBufferSize(),
  450. sys::fs::can_execute(UO->getFileName())
  451. ? FileOutputBuffer::F_executable
  452. : 0);
  453. if (!OutFileOrError)
  454. reportError(OutputFileName, OutFileOrError.takeError());
  455. std::copy(B->getMemoryBufferRef().getBufferStart(),
  456. B->getMemoryBufferRef().getBufferEnd(),
  457. OutFileOrError.get()->getBufferStart());
  458. if (Error E = OutFileOrError.get()->commit())
  459. reportError(OutputFileName, std::move(E));
  460. exit(EXIT_SUCCESS);
  461. }
  462. static void checkArchDuplicates(ArrayRef<Slice> Slices) {
  463. DenseMap<uint64_t, const Binary *> CPUIds;
  464. for (const auto &S : Slices) {
  465. auto Entry = CPUIds.try_emplace(S.getCPUID(), S.getBinary());
  466. if (!Entry.second)
  467. reportError(Entry.first->second->getFileName() + " and " +
  468. S.getBinary()->getFileName() +
  469. " have the same architecture " + S.getArchString() +
  470. " and therefore cannot be in the same universal binary");
  471. }
  472. }
  473. template <typename Range>
  474. static void updateAlignments(Range &Slices,
  475. const StringMap<const uint32_t> &Alignments) {
  476. for (auto &Slice : Slices) {
  477. auto Alignment = Alignments.find(Slice.getArchString());
  478. if (Alignment != Alignments.end())
  479. Slice.setP2Alignment(Alignment->second);
  480. }
  481. }
  482. static void checkUnusedAlignments(ArrayRef<Slice> Slices,
  483. const StringMap<const uint32_t> &Alignments) {
  484. auto HasArch = [&](StringRef Arch) {
  485. return llvm::any_of(Slices,
  486. [Arch](Slice S) { return S.getArchString() == Arch; });
  487. };
  488. for (StringRef Arch : Alignments.keys())
  489. if (!HasArch(Arch))
  490. reportError("-segalign " + Arch +
  491. " <value> specified but resulting fat file does not contain "
  492. "that architecture ");
  493. }
  494. // Updates vector ExtractedObjects with the MachOObjectFiles extracted from
  495. // Universal Binary files to transfer ownership.
  496. static SmallVector<Slice, 2>
  497. buildSlices(ArrayRef<OwningBinary<Binary>> InputBinaries,
  498. const StringMap<const uint32_t> &Alignments,
  499. SmallVectorImpl<std::unique_ptr<SymbolicFile>> &ExtractedObjects) {
  500. SmallVector<Slice, 2> Slices;
  501. for (auto &IB : InputBinaries) {
  502. const Binary *InputBinary = IB.getBinary();
  503. if (auto UO = dyn_cast<MachOUniversalBinary>(InputBinary)) {
  504. for (const auto &O : UO->objects()) {
  505. // Order here is important, because both MachOObjectFile and
  506. // IRObjectFile can be created with a binary that has embedded bitcode.
  507. Expected<std::unique_ptr<MachOObjectFile>> BinaryOrError =
  508. O.getAsObjectFile();
  509. if (BinaryOrError) {
  510. Slices.emplace_back(*(BinaryOrError.get()), O.getAlign());
  511. ExtractedObjects.push_back(std::move(BinaryOrError.get()));
  512. continue;
  513. }
  514. Expected<std::unique_ptr<IRObjectFile>> IROrError =
  515. O.getAsIRObject(LLVMCtx);
  516. if (IROrError) {
  517. consumeError(BinaryOrError.takeError());
  518. Slice S = createSliceFromIR(**IROrError, O.getAlign());
  519. ExtractedObjects.emplace_back(std::move(IROrError.get()));
  520. Slices.emplace_back(std::move(S));
  521. continue;
  522. }
  523. reportError(InputBinary->getFileName(), BinaryOrError.takeError());
  524. }
  525. } else if (const auto *O = dyn_cast<MachOObjectFile>(InputBinary)) {
  526. Slices.emplace_back(*O);
  527. } else if (const auto *A = dyn_cast<Archive>(InputBinary)) {
  528. Slices.push_back(createSliceFromArchive(*A));
  529. } else if (const auto *IRO = dyn_cast<IRObjectFile>(InputBinary)) {
  530. // Original Apple's lipo set the alignment to 0
  531. Expected<Slice> SliceOrErr = Slice::create(*IRO, 0);
  532. if (!SliceOrErr) {
  533. reportError(InputBinary->getFileName(), SliceOrErr.takeError());
  534. continue;
  535. }
  536. Slices.emplace_back(std::move(SliceOrErr.get()));
  537. } else {
  538. llvm_unreachable("Unexpected binary format");
  539. }
  540. }
  541. updateAlignments(Slices, Alignments);
  542. return Slices;
  543. }
  544. [[noreturn]] static void
  545. createUniversalBinary(ArrayRef<OwningBinary<Binary>> InputBinaries,
  546. const StringMap<const uint32_t> &Alignments,
  547. StringRef OutputFileName) {
  548. assert(InputBinaries.size() >= 1 && "Incorrect number of input binaries");
  549. assert(!OutputFileName.empty() && "Create expects a single output file");
  550. SmallVector<std::unique_ptr<SymbolicFile>, 1> ExtractedObjects;
  551. SmallVector<Slice, 1> Slices =
  552. buildSlices(InputBinaries, Alignments, ExtractedObjects);
  553. checkArchDuplicates(Slices);
  554. checkUnusedAlignments(Slices, Alignments);
  555. llvm::stable_sort(Slices);
  556. if (Error E = writeUniversalBinary(Slices, OutputFileName))
  557. reportError(std::move(E));
  558. exit(EXIT_SUCCESS);
  559. }
  560. [[noreturn]] static void
  561. extractSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,
  562. const StringMap<const uint32_t> &Alignments, StringRef ArchType,
  563. StringRef OutputFileName) {
  564. assert(!ArchType.empty() &&
  565. "The architecture type should be non-empty");
  566. assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
  567. assert(!OutputFileName.empty() && "Thin expects a single output file");
  568. if (InputBinaries.front().getBinary()->isMachO()) {
  569. reportError("input file " +
  570. InputBinaries.front().getBinary()->getFileName() +
  571. " must be a fat file when the -extract option is specified");
  572. }
  573. SmallVector<std::unique_ptr<SymbolicFile>, 2> ExtractedObjects;
  574. SmallVector<Slice, 2> Slices =
  575. buildSlices(InputBinaries, Alignments, ExtractedObjects);
  576. erase_if(Slices, [ArchType](const Slice &S) {
  577. return ArchType != S.getArchString();
  578. });
  579. if (Slices.empty())
  580. reportError(
  581. "fat input file " + InputBinaries.front().getBinary()->getFileName() +
  582. " does not contain the specified architecture " + ArchType);
  583. llvm::stable_sort(Slices);
  584. if (Error E = writeUniversalBinary(Slices, OutputFileName))
  585. reportError(std::move(E));
  586. exit(EXIT_SUCCESS);
  587. }
  588. static StringMap<Slice>
  589. buildReplacementSlices(ArrayRef<OwningBinary<Binary>> ReplacementBinaries,
  590. const StringMap<const uint32_t> &Alignments) {
  591. StringMap<Slice> Slices;
  592. // populates StringMap of slices to replace with; error checks for mismatched
  593. // replace flag args, fat files, and duplicate arch_types
  594. for (const auto &OB : ReplacementBinaries) {
  595. const Binary *ReplacementBinary = OB.getBinary();
  596. auto O = dyn_cast<MachOObjectFile>(ReplacementBinary);
  597. if (!O)
  598. reportError("replacement file: " + ReplacementBinary->getFileName() +
  599. " is a fat file (must be a thin file)");
  600. Slice S(*O);
  601. auto Entry = Slices.try_emplace(S.getArchString(), S);
  602. if (!Entry.second)
  603. reportError("-replace " + S.getArchString() +
  604. " <file_name> specified multiple times: " +
  605. Entry.first->second.getBinary()->getFileName() + ", " +
  606. O->getFileName());
  607. }
  608. auto SlicesMapRange = map_range(
  609. Slices, [](StringMapEntry<Slice> &E) -> Slice & { return E.getValue(); });
  610. updateAlignments(SlicesMapRange, Alignments);
  611. return Slices;
  612. }
  613. [[noreturn]] static void
  614. replaceSlices(ArrayRef<OwningBinary<Binary>> InputBinaries,
  615. const StringMap<const uint32_t> &Alignments,
  616. StringRef OutputFileName, ArrayRef<InputFile> ReplacementFiles) {
  617. assert(InputBinaries.size() == 1 && "Incorrect number of input binaries");
  618. assert(!OutputFileName.empty() && "Replace expects a single output file");
  619. if (InputBinaries.front().getBinary()->isMachO())
  620. reportError("input file " +
  621. InputBinaries.front().getBinary()->getFileName() +
  622. " must be a fat file when the -replace option is specified");
  623. SmallVector<OwningBinary<Binary>, 1> ReplacementBinaries =
  624. readInputBinaries(ReplacementFiles);
  625. StringMap<Slice> ReplacementSlices =
  626. buildReplacementSlices(ReplacementBinaries, Alignments);
  627. SmallVector<std::unique_ptr<SymbolicFile>, 2> ExtractedObjects;
  628. SmallVector<Slice, 2> Slices =
  629. buildSlices(InputBinaries, Alignments, ExtractedObjects);
  630. for (auto &Slice : Slices) {
  631. auto It = ReplacementSlices.find(Slice.getArchString());
  632. if (It != ReplacementSlices.end()) {
  633. Slice = It->second;
  634. ReplacementSlices.erase(It); // only keep remaining replacing arch_types
  635. }
  636. }
  637. if (!ReplacementSlices.empty())
  638. reportError("-replace " + ReplacementSlices.begin()->first() +
  639. " <file_name> specified but fat file: " +
  640. InputBinaries.front().getBinary()->getFileName() +
  641. " does not contain that architecture");
  642. checkUnusedAlignments(Slices, Alignments);
  643. llvm::stable_sort(Slices);
  644. if (Error E = writeUniversalBinary(Slices, OutputFileName))
  645. reportError(std::move(E));
  646. exit(EXIT_SUCCESS);
  647. }
  648. int main(int argc, char **argv) {
  649. InitLLVM X(argc, argv);
  650. Config C = parseLipoOptions(makeArrayRef(argv + 1, argc));
  651. SmallVector<OwningBinary<Binary>, 1> InputBinaries =
  652. readInputBinaries(C.InputFiles);
  653. switch (C.ActionToPerform) {
  654. case LipoAction::VerifyArch:
  655. verifyArch(InputBinaries, C.VerifyArchList);
  656. break;
  657. case LipoAction::PrintArchs:
  658. printArchs(InputBinaries);
  659. break;
  660. case LipoAction::PrintInfo:
  661. printInfo(InputBinaries);
  662. break;
  663. case LipoAction::ThinArch:
  664. thinSlice(InputBinaries, C.ArchType, C.OutputFile);
  665. break;
  666. case LipoAction::ExtractArch:
  667. extractSlice(InputBinaries, C.SegmentAlignments, C.ArchType, C.OutputFile);
  668. break;
  669. case LipoAction::CreateUniversal:
  670. createUniversalBinary(InputBinaries, C.SegmentAlignments, C.OutputFile);
  671. break;
  672. case LipoAction::ReplaceArch:
  673. replaceSlices(InputBinaries, C.SegmentAlignments, C.OutputFile,
  674. C.ReplacementFiles);
  675. break;
  676. }
  677. return EXIT_SUCCESS;
  678. }