llvm-lipo.cpp 28 KB

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