llvm-ar.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. //===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
  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. // Builds up (relatively) standard unix archive files (.a) containing LLVM
  10. // bitcode or other files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/Object/Archive.h"
  17. #include "llvm/Object/ArchiveWriter.h"
  18. #include "llvm/Object/MachO.h"
  19. #include "llvm/Object/ObjectFile.h"
  20. #include "llvm/Support/Chrono.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/Errc.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/Format.h"
  25. #include "llvm/Support/FormatVariadic.h"
  26. #include "llvm/Support/InitLLVM.h"
  27. #include "llvm/Support/LineIterator.h"
  28. #include "llvm/Support/MemoryBuffer.h"
  29. #include "llvm/Support/Path.h"
  30. #include "llvm/Support/Process.h"
  31. #include "llvm/Support/StringSaver.h"
  32. #include "llvm/Support/TargetSelect.h"
  33. #include "llvm/Support/ToolOutputFile.h"
  34. #include "llvm/Support/WithColor.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include "llvm/ToolDrivers/llvm-dlltool/DlltoolDriver.h"
  37. #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
  38. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  39. #include <unistd.h>
  40. #else
  41. #include <io.h>
  42. #endif
  43. using namespace llvm;
  44. // The name this program was invoked as.
  45. static StringRef ToolName;
  46. // The basename of this program.
  47. static StringRef Stem;
  48. const char RanlibHelp[] = R"(
  49. OVERVIEW: LLVM Ranlib (llvm-ranlib)
  50. This program generates an index to speed access to archives
  51. USAGE: llvm-ranlib <archive-file>
  52. OPTIONS:
  53. -help - Display available options
  54. -version - Display the version of this program
  55. )";
  56. const char ArHelp[] = R"(
  57. OVERVIEW: LLVM Archiver
  58. USAGE: llvm-ar [options] [-]<operation>[modifiers] [relpos] [count] <archive> [files]
  59. llvm-ar -M [<mri-script]
  60. OPTIONS:
  61. --format - Archive format to create
  62. =default - default
  63. =gnu - gnu
  64. =darwin - darwin
  65. =bsd - bsd
  66. --plugin=<string> - Ignored for compatibility
  67. --help - Display available options
  68. --version - Display the version of this program
  69. @<file> - read options from <file>
  70. OPERATIONS:
  71. d - delete [files] from the archive
  72. m - move [files] in the archive
  73. p - print [files] found in the archive
  74. q - quick append [files] to the archive
  75. r - replace or insert [files] into the archive
  76. s - act as ranlib
  77. t - display contents of archive
  78. x - extract [files] from the archive
  79. MODIFIERS:
  80. [a] - put [files] after [relpos]
  81. [b] - put [files] before [relpos] (same as [i])
  82. [c] - do not warn if archive had to be created
  83. [D] - use zero for timestamps and uids/gids (default)
  84. [i] - put [files] before [relpos] (same as [b])
  85. [l] - ignored for compatibility
  86. [L] - add archive's contents
  87. [N] - use instance [count] of name
  88. [o] - preserve original dates
  89. [P] - use full names when matching (implied for thin archives)
  90. [s] - create an archive index (cf. ranlib)
  91. [S] - do not build a symbol table
  92. [T] - create a thin archive
  93. [u] - update only [files] newer than archive contents
  94. [U] - use actual timestamps and uids/gids
  95. [v] - be verbose about actions taken
  96. )";
  97. void printHelpMessage() {
  98. if (Stem.contains_lower("ranlib"))
  99. outs() << RanlibHelp;
  100. else if (Stem.contains_lower("ar"))
  101. outs() << ArHelp;
  102. }
  103. // Show the error message and exit.
  104. LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
  105. WithColor::error(errs(), ToolName) << Error << ".\n";
  106. printHelpMessage();
  107. exit(1);
  108. }
  109. static void failIfError(std::error_code EC, Twine Context = "") {
  110. if (!EC)
  111. return;
  112. std::string ContextStr = Context.str();
  113. if (ContextStr.empty())
  114. fail(EC.message());
  115. fail(Context + ": " + EC.message());
  116. }
  117. static void failIfError(Error E, Twine Context = "") {
  118. if (!E)
  119. return;
  120. handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
  121. std::string ContextStr = Context.str();
  122. if (ContextStr.empty())
  123. fail(EIB.message());
  124. fail(Context + ": " + EIB.message());
  125. });
  126. }
  127. static SmallVector<const char *, 256> PositionalArgs;
  128. static bool MRI;
  129. namespace {
  130. enum Format { Default, GNU, BSD, DARWIN, Unknown };
  131. }
  132. static Format FormatType = Default;
  133. static std::string Options;
  134. // This enumeration delineates the kinds of operations on an archive
  135. // that are permitted.
  136. enum ArchiveOperation {
  137. Print, ///< Print the contents of the archive
  138. Delete, ///< Delete the specified members
  139. Move, ///< Move members to end or as given by {a,b,i} modifiers
  140. QuickAppend, ///< Quickly append to end of archive
  141. ReplaceOrInsert, ///< Replace or Insert members
  142. DisplayTable, ///< Display the table of contents
  143. Extract, ///< Extract files back to file system
  144. CreateSymTab ///< Create a symbol table in an existing archive
  145. };
  146. // Modifiers to follow operation to vary behavior
  147. static bool AddAfter = false; ///< 'a' modifier
  148. static bool AddBefore = false; ///< 'b' modifier
  149. static bool Create = false; ///< 'c' modifier
  150. static bool OriginalDates = false; ///< 'o' modifier
  151. static bool CompareFullPath = false; ///< 'P' modifier
  152. static bool OnlyUpdate = false; ///< 'u' modifier
  153. static bool Verbose = false; ///< 'v' modifier
  154. static bool Symtab = true; ///< 's' modifier
  155. static bool Deterministic = true; ///< 'D' and 'U' modifiers
  156. static bool Thin = false; ///< 'T' modifier
  157. static bool AddLibrary = false; ///< 'L' modifier
  158. // Relative Positional Argument (for insert/move). This variable holds
  159. // the name of the archive member to which the 'a', 'b' or 'i' modifier
  160. // refers. Only one of 'a', 'b' or 'i' can be specified so we only need
  161. // one variable.
  162. static std::string RelPos;
  163. // Count parameter for 'N' modifier. This variable specifies which file should
  164. // match for extract/delete operations when there are multiple matches. This is
  165. // 1-indexed. A value of 0 is invalid, and implies 'N' is not used.
  166. static int CountParam = 0;
  167. // This variable holds the name of the archive file as given on the
  168. // command line.
  169. static std::string ArchiveName;
  170. // This variable holds the list of member files to proecess, as given
  171. // on the command line.
  172. static std::vector<StringRef> Members;
  173. // Static buffer to hold StringRefs.
  174. static BumpPtrAllocator Alloc;
  175. // Extract the member filename from the command line for the [relpos] argument
  176. // associated with a, b, and i modifiers
  177. static void getRelPos() {
  178. if (PositionalArgs.empty())
  179. fail("Expected [relpos] for a, b, or i modifier");
  180. RelPos = PositionalArgs[0];
  181. PositionalArgs.erase(PositionalArgs.begin());
  182. }
  183. // Extract the parameter from the command line for the [count] argument
  184. // associated with the N modifier
  185. static void getCountParam() {
  186. if (PositionalArgs.empty())
  187. fail("Expected [count] for N modifier");
  188. auto CountParamArg = StringRef(PositionalArgs[0]);
  189. if (CountParamArg.getAsInteger(10, CountParam))
  190. fail("Value for [count] must be numeric, got: " + CountParamArg);
  191. if (CountParam < 1)
  192. fail("Value for [count] must be positive, got: " + CountParamArg);
  193. PositionalArgs.erase(PositionalArgs.begin());
  194. }
  195. // Get the archive file name from the command line
  196. static void getArchive() {
  197. if (PositionalArgs.empty())
  198. fail("An archive name must be specified");
  199. ArchiveName = PositionalArgs[0];
  200. PositionalArgs.erase(PositionalArgs.begin());
  201. }
  202. // Copy over remaining items in PositionalArgs to our Members vector
  203. static void getMembers() {
  204. for (auto &Arg : PositionalArgs)
  205. Members.push_back(Arg);
  206. }
  207. std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
  208. std::vector<std::unique_ptr<object::Archive>> Archives;
  209. static object::Archive &readLibrary(const Twine &Library) {
  210. auto BufOrErr = MemoryBuffer::getFile(Library, -1, false);
  211. failIfError(BufOrErr.getError(), "Could not open library " + Library);
  212. ArchiveBuffers.push_back(std::move(*BufOrErr));
  213. auto LibOrErr =
  214. object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
  215. failIfError(errorToErrorCode(LibOrErr.takeError()),
  216. "Could not parse library");
  217. Archives.push_back(std::move(*LibOrErr));
  218. return *Archives.back();
  219. }
  220. static void runMRIScript();
  221. // Parse the command line options as presented and return the operation
  222. // specified. Process all modifiers and check to make sure that constraints on
  223. // modifier/operation pairs have not been violated.
  224. static ArchiveOperation parseCommandLine() {
  225. if (MRI) {
  226. if (!PositionalArgs.empty() || !Options.empty())
  227. fail("Cannot mix -M and other options");
  228. runMRIScript();
  229. }
  230. // Keep track of number of operations. We can only specify one
  231. // per execution.
  232. unsigned NumOperations = 0;
  233. // Keep track of the number of positional modifiers (a,b,i). Only
  234. // one can be specified.
  235. unsigned NumPositional = 0;
  236. // Keep track of which operation was requested
  237. ArchiveOperation Operation;
  238. bool MaybeJustCreateSymTab = false;
  239. for (unsigned i = 0; i < Options.size(); ++i) {
  240. switch (Options[i]) {
  241. case 'd':
  242. ++NumOperations;
  243. Operation = Delete;
  244. break;
  245. case 'm':
  246. ++NumOperations;
  247. Operation = Move;
  248. break;
  249. case 'p':
  250. ++NumOperations;
  251. Operation = Print;
  252. break;
  253. case 'q':
  254. ++NumOperations;
  255. Operation = QuickAppend;
  256. break;
  257. case 'r':
  258. ++NumOperations;
  259. Operation = ReplaceOrInsert;
  260. break;
  261. case 't':
  262. ++NumOperations;
  263. Operation = DisplayTable;
  264. break;
  265. case 'x':
  266. ++NumOperations;
  267. Operation = Extract;
  268. break;
  269. case 'c':
  270. Create = true;
  271. break;
  272. case 'l': /* accepted but unused */
  273. break;
  274. case 'o':
  275. OriginalDates = true;
  276. break;
  277. case 'P':
  278. CompareFullPath = true;
  279. break;
  280. case 's':
  281. Symtab = true;
  282. MaybeJustCreateSymTab = true;
  283. break;
  284. case 'S':
  285. Symtab = false;
  286. break;
  287. case 'u':
  288. OnlyUpdate = true;
  289. break;
  290. case 'v':
  291. Verbose = true;
  292. break;
  293. case 'a':
  294. getRelPos();
  295. AddAfter = true;
  296. NumPositional++;
  297. break;
  298. case 'b':
  299. getRelPos();
  300. AddBefore = true;
  301. NumPositional++;
  302. break;
  303. case 'i':
  304. getRelPos();
  305. AddBefore = true;
  306. NumPositional++;
  307. break;
  308. case 'D':
  309. Deterministic = true;
  310. break;
  311. case 'U':
  312. Deterministic = false;
  313. break;
  314. case 'N':
  315. getCountParam();
  316. break;
  317. case 'T':
  318. Thin = true;
  319. // Thin archives store path names, so P should be forced.
  320. CompareFullPath = true;
  321. break;
  322. case 'L':
  323. AddLibrary = true;
  324. break;
  325. default:
  326. fail(std::string("unknown option ") + Options[i]);
  327. }
  328. }
  329. // At this point, the next thing on the command line must be
  330. // the archive name.
  331. getArchive();
  332. // Everything on the command line at this point is a member.
  333. getMembers();
  334. if (NumOperations == 0 && MaybeJustCreateSymTab) {
  335. NumOperations = 1;
  336. Operation = CreateSymTab;
  337. if (!Members.empty())
  338. fail("The s operation takes only an archive as argument");
  339. }
  340. // Perform various checks on the operation/modifier specification
  341. // to make sure we are dealing with a legal request.
  342. if (NumOperations == 0)
  343. fail("You must specify at least one of the operations");
  344. if (NumOperations > 1)
  345. fail("Only one operation may be specified");
  346. if (NumPositional > 1)
  347. fail("You may only specify one of a, b, and i modifiers");
  348. if (AddAfter || AddBefore)
  349. if (Operation != Move && Operation != ReplaceOrInsert)
  350. fail("The 'a', 'b' and 'i' modifiers can only be specified with "
  351. "the 'm' or 'r' operations");
  352. if (CountParam)
  353. if (Operation != Extract && Operation != Delete)
  354. fail("The 'N' modifier can only be specified with the 'x' or 'd' "
  355. "operations");
  356. if (OriginalDates && Operation != Extract)
  357. fail("The 'o' modifier is only applicable to the 'x' operation");
  358. if (OnlyUpdate && Operation != ReplaceOrInsert)
  359. fail("The 'u' modifier is only applicable to the 'r' operation");
  360. if (AddLibrary && Operation != QuickAppend)
  361. fail("The 'L' modifier is only applicable to the 'q' operation");
  362. // Return the parsed operation to the caller
  363. return Operation;
  364. }
  365. // Implements the 'p' operation. This function traverses the archive
  366. // looking for members that match the path list.
  367. static void doPrint(StringRef Name, const object::Archive::Child &C) {
  368. if (Verbose)
  369. outs() << "Printing " << Name << "\n";
  370. Expected<StringRef> DataOrErr = C.getBuffer();
  371. failIfError(DataOrErr.takeError());
  372. StringRef Data = *DataOrErr;
  373. outs().write(Data.data(), Data.size());
  374. }
  375. // Utility function for printing out the file mode when the 't' operation is in
  376. // verbose mode.
  377. static void printMode(unsigned mode) {
  378. outs() << ((mode & 004) ? "r" : "-");
  379. outs() << ((mode & 002) ? "w" : "-");
  380. outs() << ((mode & 001) ? "x" : "-");
  381. }
  382. // Implement the 't' operation. This function prints out just
  383. // the file names of each of the members. However, if verbose mode is requested
  384. // ('v' modifier) then the file type, permission mode, user, group, size, and
  385. // modification time are also printed.
  386. static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
  387. if (Verbose) {
  388. Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
  389. failIfError(ModeOrErr.takeError());
  390. sys::fs::perms Mode = ModeOrErr.get();
  391. printMode((Mode >> 6) & 007);
  392. printMode((Mode >> 3) & 007);
  393. printMode(Mode & 007);
  394. Expected<unsigned> UIDOrErr = C.getUID();
  395. failIfError(UIDOrErr.takeError());
  396. outs() << ' ' << UIDOrErr.get();
  397. Expected<unsigned> GIDOrErr = C.getGID();
  398. failIfError(GIDOrErr.takeError());
  399. outs() << '/' << GIDOrErr.get();
  400. Expected<uint64_t> Size = C.getSize();
  401. failIfError(Size.takeError());
  402. outs() << ' ' << format("%6llu", Size.get());
  403. auto ModTimeOrErr = C.getLastModified();
  404. failIfError(ModTimeOrErr.takeError());
  405. // Note: formatv() only handles the default TimePoint<>, which is in
  406. // nanoseconds.
  407. // TODO: fix format_provider<TimePoint<>> to allow other units.
  408. sys::TimePoint<> ModTimeInNs = ModTimeOrErr.get();
  409. outs() << ' ' << formatv("{0:%b %e %H:%M %Y}", ModTimeInNs);
  410. outs() << ' ';
  411. }
  412. if (C.getParent()->isThin()) {
  413. if (!sys::path::is_absolute(Name)) {
  414. StringRef ParentDir = sys::path::parent_path(ArchiveName);
  415. if (!ParentDir.empty())
  416. outs() << sys::path::convert_to_slash(ParentDir) << '/';
  417. }
  418. }
  419. outs() << Name << "\n";
  420. }
  421. static StringRef normalizePath(StringRef Path) {
  422. return CompareFullPath ? Path : sys::path::filename(Path);
  423. }
  424. // Implement the 'x' operation. This function extracts files back to the file
  425. // system.
  426. static void doExtract(StringRef Name, const object::Archive::Child &C) {
  427. // Retain the original mode.
  428. Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
  429. failIfError(ModeOrErr.takeError());
  430. sys::fs::perms Mode = ModeOrErr.get();
  431. int FD;
  432. failIfError(sys::fs::openFileForWrite(sys::path::filename(Name), FD,
  433. sys::fs::CD_CreateAlways,
  434. sys::fs::OF_None, Mode),
  435. Name);
  436. {
  437. raw_fd_ostream file(FD, false);
  438. // Get the data and its length
  439. Expected<StringRef> BufOrErr = C.getBuffer();
  440. failIfError(BufOrErr.takeError());
  441. StringRef Data = BufOrErr.get();
  442. // Write the data.
  443. file.write(Data.data(), Data.size());
  444. }
  445. // If we're supposed to retain the original modification times, etc. do so
  446. // now.
  447. if (OriginalDates) {
  448. auto ModTimeOrErr = C.getLastModified();
  449. failIfError(ModTimeOrErr.takeError());
  450. failIfError(
  451. sys::fs::setLastAccessAndModificationTime(FD, ModTimeOrErr.get()));
  452. }
  453. if (close(FD))
  454. fail("Could not close the file");
  455. }
  456. static bool shouldCreateArchive(ArchiveOperation Op) {
  457. switch (Op) {
  458. case Print:
  459. case Delete:
  460. case Move:
  461. case DisplayTable:
  462. case Extract:
  463. case CreateSymTab:
  464. return false;
  465. case QuickAppend:
  466. case ReplaceOrInsert:
  467. return true;
  468. }
  469. llvm_unreachable("Missing entry in covered switch.");
  470. }
  471. static void performReadOperation(ArchiveOperation Operation,
  472. object::Archive *OldArchive) {
  473. if (Operation == Extract && OldArchive->isThin())
  474. fail("extracting from a thin archive is not supported");
  475. bool Filter = !Members.empty();
  476. StringMap<int> MemberCount;
  477. {
  478. Error Err = Error::success();
  479. for (auto &C : OldArchive->children(Err)) {
  480. Expected<StringRef> NameOrErr = C.getName();
  481. failIfError(NameOrErr.takeError());
  482. StringRef Name = NameOrErr.get();
  483. if (Filter) {
  484. auto I = find_if(Members, [Name](StringRef Path) {
  485. return Name == normalizePath(Path);
  486. });
  487. if (I == Members.end())
  488. continue;
  489. if (CountParam && ++MemberCount[Name] != CountParam)
  490. continue;
  491. Members.erase(I);
  492. }
  493. switch (Operation) {
  494. default:
  495. llvm_unreachable("Not a read operation");
  496. case Print:
  497. doPrint(Name, C);
  498. break;
  499. case DisplayTable:
  500. doDisplayTable(Name, C);
  501. break;
  502. case Extract:
  503. doExtract(Name, C);
  504. break;
  505. }
  506. }
  507. failIfError(std::move(Err));
  508. }
  509. if (Members.empty())
  510. return;
  511. for (StringRef Name : Members)
  512. WithColor::error(errs(), ToolName) << "'" << Name << "' was not found\n";
  513. exit(1);
  514. }
  515. static void addChildMember(std::vector<NewArchiveMember> &Members,
  516. const object::Archive::Child &M,
  517. bool FlattenArchive = false) {
  518. if (Thin && !M.getParent()->isThin())
  519. fail("Cannot convert a regular archive to a thin one");
  520. Expected<NewArchiveMember> NMOrErr =
  521. NewArchiveMember::getOldMember(M, Deterministic);
  522. failIfError(NMOrErr.takeError());
  523. // If the child member we're trying to add is thin, use the path relative to
  524. // the archive it's in, so the file resolves correctly.
  525. if (Thin && FlattenArchive) {
  526. StringSaver Saver(Alloc);
  527. Expected<std::string> FileNameOrErr = M.getName();
  528. failIfError(FileNameOrErr.takeError());
  529. if (sys::path::is_absolute(*FileNameOrErr)) {
  530. NMOrErr->MemberName = Saver.save(sys::path::convert_to_slash(*FileNameOrErr));
  531. } else {
  532. FileNameOrErr = M.getFullName();
  533. failIfError(FileNameOrErr.takeError());
  534. Expected<std::string> PathOrErr =
  535. computeArchiveRelativePath(ArchiveName, *FileNameOrErr);
  536. NMOrErr->MemberName = Saver.save(
  537. PathOrErr ? *PathOrErr : sys::path::convert_to_slash(*FileNameOrErr));
  538. }
  539. }
  540. if (FlattenArchive &&
  541. identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) {
  542. Expected<std::string> FileNameOrErr = M.getFullName();
  543. failIfError(FileNameOrErr.takeError());
  544. object::Archive &Lib = readLibrary(*FileNameOrErr);
  545. // When creating thin archives, only flatten if the member is also thin.
  546. if (!Thin || Lib.isThin()) {
  547. Error Err = Error::success();
  548. // Only Thin archives are recursively flattened.
  549. for (auto &Child : Lib.children(Err))
  550. addChildMember(Members, Child, /*FlattenArchive=*/Thin);
  551. failIfError(std::move(Err));
  552. return;
  553. }
  554. }
  555. Members.push_back(std::move(*NMOrErr));
  556. }
  557. static void addMember(std::vector<NewArchiveMember> &Members,
  558. StringRef FileName, bool FlattenArchive = false) {
  559. Expected<NewArchiveMember> NMOrErr =
  560. NewArchiveMember::getFile(FileName, Deterministic);
  561. failIfError(NMOrErr.takeError(), FileName);
  562. StringSaver Saver(Alloc);
  563. // For regular archives, use the basename of the object path for the member
  564. // name. For thin archives, use the full relative paths so the file resolves
  565. // correctly.
  566. if (!Thin) {
  567. NMOrErr->MemberName = sys::path::filename(NMOrErr->MemberName);
  568. } else {
  569. if (sys::path::is_absolute(FileName))
  570. NMOrErr->MemberName = Saver.save(sys::path::convert_to_slash(FileName));
  571. else {
  572. Expected<std::string> PathOrErr =
  573. computeArchiveRelativePath(ArchiveName, FileName);
  574. NMOrErr->MemberName = Saver.save(
  575. PathOrErr ? *PathOrErr : sys::path::convert_to_slash(FileName));
  576. }
  577. }
  578. if (FlattenArchive &&
  579. identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) {
  580. object::Archive &Lib = readLibrary(FileName);
  581. // When creating thin archives, only flatten if the member is also thin.
  582. if (!Thin || Lib.isThin()) {
  583. Error Err = Error::success();
  584. // Only Thin archives are recursively flattened.
  585. for (auto &Child : Lib.children(Err))
  586. addChildMember(Members, Child, /*FlattenArchive=*/Thin);
  587. failIfError(std::move(Err));
  588. return;
  589. }
  590. }
  591. Members.push_back(std::move(*NMOrErr));
  592. }
  593. enum InsertAction {
  594. IA_AddOldMember,
  595. IA_AddNewMember,
  596. IA_Delete,
  597. IA_MoveOldMember,
  598. IA_MoveNewMember
  599. };
  600. static InsertAction computeInsertAction(ArchiveOperation Operation,
  601. const object::Archive::Child &Member,
  602. StringRef Name,
  603. std::vector<StringRef>::iterator &Pos,
  604. StringMap<int> &MemberCount) {
  605. if (Operation == QuickAppend || Members.empty())
  606. return IA_AddOldMember;
  607. auto MI = find_if(
  608. Members, [Name](StringRef Path) { return Name == normalizePath(Path); });
  609. if (MI == Members.end())
  610. return IA_AddOldMember;
  611. Pos = MI;
  612. if (Operation == Delete) {
  613. if (CountParam && ++MemberCount[Name] != CountParam)
  614. return IA_AddOldMember;
  615. return IA_Delete;
  616. }
  617. if (Operation == Move)
  618. return IA_MoveOldMember;
  619. if (Operation == ReplaceOrInsert) {
  620. StringRef PosName = normalizePath(RelPos);
  621. if (!OnlyUpdate) {
  622. if (PosName.empty())
  623. return IA_AddNewMember;
  624. return IA_MoveNewMember;
  625. }
  626. // We could try to optimize this to a fstat, but it is not a common
  627. // operation.
  628. sys::fs::file_status Status;
  629. failIfError(sys::fs::status(*MI, Status), *MI);
  630. auto ModTimeOrErr = Member.getLastModified();
  631. failIfError(ModTimeOrErr.takeError());
  632. if (Status.getLastModificationTime() < ModTimeOrErr.get()) {
  633. if (PosName.empty())
  634. return IA_AddOldMember;
  635. return IA_MoveOldMember;
  636. }
  637. if (PosName.empty())
  638. return IA_AddNewMember;
  639. return IA_MoveNewMember;
  640. }
  641. llvm_unreachable("No such operation");
  642. }
  643. // We have to walk this twice and computing it is not trivial, so creating an
  644. // explicit std::vector is actually fairly efficient.
  645. static std::vector<NewArchiveMember>
  646. computeNewArchiveMembers(ArchiveOperation Operation,
  647. object::Archive *OldArchive) {
  648. std::vector<NewArchiveMember> Ret;
  649. std::vector<NewArchiveMember> Moved;
  650. int InsertPos = -1;
  651. StringRef PosName = normalizePath(RelPos);
  652. if (OldArchive) {
  653. Error Err = Error::success();
  654. StringMap<int> MemberCount;
  655. for (auto &Child : OldArchive->children(Err)) {
  656. int Pos = Ret.size();
  657. Expected<StringRef> NameOrErr = Child.getName();
  658. failIfError(NameOrErr.takeError());
  659. StringRef Name = NameOrErr.get();
  660. if (Name == PosName) {
  661. assert(AddAfter || AddBefore);
  662. if (AddBefore)
  663. InsertPos = Pos;
  664. else
  665. InsertPos = Pos + 1;
  666. }
  667. std::vector<StringRef>::iterator MemberI = Members.end();
  668. InsertAction Action =
  669. computeInsertAction(Operation, Child, Name, MemberI, MemberCount);
  670. switch (Action) {
  671. case IA_AddOldMember:
  672. addChildMember(Ret, Child, /*FlattenArchive=*/Thin);
  673. break;
  674. case IA_AddNewMember:
  675. addMember(Ret, *MemberI);
  676. break;
  677. case IA_Delete:
  678. break;
  679. case IA_MoveOldMember:
  680. addChildMember(Moved, Child, /*FlattenArchive=*/Thin);
  681. break;
  682. case IA_MoveNewMember:
  683. addMember(Moved, *MemberI);
  684. break;
  685. }
  686. // When processing elements with the count param, we need to preserve the
  687. // full members list when iterating over all archive members. For
  688. // instance, "llvm-ar dN 2 archive.a member.o" should delete the second
  689. // file named member.o it sees; we are not done with member.o the first
  690. // time we see it in the archive.
  691. if (MemberI != Members.end() && !CountParam)
  692. Members.erase(MemberI);
  693. }
  694. failIfError(std::move(Err));
  695. }
  696. if (Operation == Delete)
  697. return Ret;
  698. if (!RelPos.empty() && InsertPos == -1)
  699. fail("Insertion point not found");
  700. if (RelPos.empty())
  701. InsertPos = Ret.size();
  702. assert(unsigned(InsertPos) <= Ret.size());
  703. int Pos = InsertPos;
  704. for (auto &M : Moved) {
  705. Ret.insert(Ret.begin() + Pos, std::move(M));
  706. ++Pos;
  707. }
  708. if (AddLibrary) {
  709. assert(Operation == QuickAppend);
  710. for (auto &Member : Members)
  711. addMember(Ret, Member, /*FlattenArchive=*/true);
  712. return Ret;
  713. }
  714. std::vector<NewArchiveMember> NewMembers;
  715. for (auto &Member : Members)
  716. addMember(NewMembers, Member, /*FlattenArchive=*/Thin);
  717. Ret.reserve(Ret.size() + NewMembers.size());
  718. std::move(NewMembers.begin(), NewMembers.end(),
  719. std::inserter(Ret, std::next(Ret.begin(), InsertPos)));
  720. return Ret;
  721. }
  722. static object::Archive::Kind getDefaultForHost() {
  723. return Triple(sys::getProcessTriple()).isOSDarwin()
  724. ? object::Archive::K_DARWIN
  725. : object::Archive::K_GNU;
  726. }
  727. static object::Archive::Kind getKindFromMember(const NewArchiveMember &Member) {
  728. Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
  729. object::ObjectFile::createObjectFile(Member.Buf->getMemBufferRef());
  730. if (OptionalObject)
  731. return isa<object::MachOObjectFile>(**OptionalObject)
  732. ? object::Archive::K_DARWIN
  733. : object::Archive::K_GNU;
  734. // squelch the error in case we had a non-object file
  735. consumeError(OptionalObject.takeError());
  736. return getDefaultForHost();
  737. }
  738. static void performWriteOperation(ArchiveOperation Operation,
  739. object::Archive *OldArchive,
  740. std::unique_ptr<MemoryBuffer> OldArchiveBuf,
  741. std::vector<NewArchiveMember> *NewMembersP) {
  742. std::vector<NewArchiveMember> NewMembers;
  743. if (!NewMembersP)
  744. NewMembers = computeNewArchiveMembers(Operation, OldArchive);
  745. object::Archive::Kind Kind;
  746. switch (FormatType) {
  747. case Default:
  748. if (Thin)
  749. Kind = object::Archive::K_GNU;
  750. else if (OldArchive)
  751. Kind = OldArchive->kind();
  752. else if (NewMembersP)
  753. Kind = !NewMembersP->empty() ? getKindFromMember(NewMembersP->front())
  754. : getDefaultForHost();
  755. else
  756. Kind = !NewMembers.empty() ? getKindFromMember(NewMembers.front())
  757. : getDefaultForHost();
  758. break;
  759. case GNU:
  760. Kind = object::Archive::K_GNU;
  761. break;
  762. case BSD:
  763. if (Thin)
  764. fail("Only the gnu format has a thin mode");
  765. Kind = object::Archive::K_BSD;
  766. break;
  767. case DARWIN:
  768. if (Thin)
  769. fail("Only the gnu format has a thin mode");
  770. Kind = object::Archive::K_DARWIN;
  771. break;
  772. case Unknown:
  773. llvm_unreachable("");
  774. }
  775. Error E =
  776. writeArchive(ArchiveName, NewMembersP ? *NewMembersP : NewMembers, Symtab,
  777. Kind, Deterministic, Thin, std::move(OldArchiveBuf));
  778. failIfError(std::move(E), ArchiveName);
  779. }
  780. static void createSymbolTable(object::Archive *OldArchive) {
  781. // When an archive is created or modified, if the s option is given, the
  782. // resulting archive will have a current symbol table. If the S option
  783. // is given, it will have no symbol table.
  784. // In summary, we only need to update the symbol table if we have none.
  785. // This is actually very common because of broken build systems that think
  786. // they have to run ranlib.
  787. if (OldArchive->hasSymbolTable())
  788. return;
  789. performWriteOperation(CreateSymTab, OldArchive, nullptr, nullptr);
  790. }
  791. static void performOperation(ArchiveOperation Operation,
  792. object::Archive *OldArchive,
  793. std::unique_ptr<MemoryBuffer> OldArchiveBuf,
  794. std::vector<NewArchiveMember> *NewMembers) {
  795. switch (Operation) {
  796. case Print:
  797. case DisplayTable:
  798. case Extract:
  799. performReadOperation(Operation, OldArchive);
  800. return;
  801. case Delete:
  802. case Move:
  803. case QuickAppend:
  804. case ReplaceOrInsert:
  805. performWriteOperation(Operation, OldArchive, std::move(OldArchiveBuf),
  806. NewMembers);
  807. return;
  808. case CreateSymTab:
  809. createSymbolTable(OldArchive);
  810. return;
  811. }
  812. llvm_unreachable("Unknown operation.");
  813. }
  814. static int performOperation(ArchiveOperation Operation,
  815. std::vector<NewArchiveMember> *NewMembers) {
  816. // Create or open the archive object.
  817. ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
  818. MemoryBuffer::getFile(ArchiveName, -1, false);
  819. std::error_code EC = Buf.getError();
  820. if (EC && EC != errc::no_such_file_or_directory)
  821. fail("error opening '" + ArchiveName + "': " + EC.message() + "!");
  822. if (!EC) {
  823. Error Err = Error::success();
  824. object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
  825. EC = errorToErrorCode(std::move(Err));
  826. failIfError(EC,
  827. "error loading '" + ArchiveName + "': " + EC.message() + "!");
  828. if (Archive.isThin())
  829. CompareFullPath = true;
  830. performOperation(Operation, &Archive, std::move(Buf.get()), NewMembers);
  831. return 0;
  832. }
  833. assert(EC == errc::no_such_file_or_directory);
  834. if (!shouldCreateArchive(Operation)) {
  835. failIfError(EC, Twine("error loading '") + ArchiveName + "'");
  836. } else {
  837. if (!Create) {
  838. // Produce a warning if we should and we're creating the archive
  839. WithColor::warning(errs(), ToolName)
  840. << "creating " << ArchiveName << "\n";
  841. }
  842. }
  843. performOperation(Operation, nullptr, nullptr, NewMembers);
  844. return 0;
  845. }
  846. static void runMRIScript() {
  847. enum class MRICommand { AddLib, AddMod, Create, CreateThin, Delete, Save, End, Invalid };
  848. ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
  849. failIfError(Buf.getError());
  850. const MemoryBuffer &Ref = *Buf.get();
  851. bool Saved = false;
  852. std::vector<NewArchiveMember> NewMembers;
  853. for (line_iterator I(Ref, /*SkipBlanks*/ false), E; I != E; ++I) {
  854. StringRef Line = *I;
  855. Line = Line.split(';').first;
  856. Line = Line.split('*').first;
  857. Line = Line.trim();
  858. if (Line.empty())
  859. continue;
  860. StringRef CommandStr, Rest;
  861. std::tie(CommandStr, Rest) = Line.split(' ');
  862. Rest = Rest.trim();
  863. if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
  864. Rest = Rest.drop_front().drop_back();
  865. auto Command = StringSwitch<MRICommand>(CommandStr.lower())
  866. .Case("addlib", MRICommand::AddLib)
  867. .Case("addmod", MRICommand::AddMod)
  868. .Case("create", MRICommand::Create)
  869. .Case("createthin", MRICommand::CreateThin)
  870. .Case("delete", MRICommand::Delete)
  871. .Case("save", MRICommand::Save)
  872. .Case("end", MRICommand::End)
  873. .Default(MRICommand::Invalid);
  874. switch (Command) {
  875. case MRICommand::AddLib: {
  876. object::Archive &Lib = readLibrary(Rest);
  877. {
  878. Error Err = Error::success();
  879. for (auto &Member : Lib.children(Err))
  880. addChildMember(NewMembers, Member, /*FlattenArchive=*/Thin);
  881. failIfError(std::move(Err));
  882. }
  883. break;
  884. }
  885. case MRICommand::AddMod:
  886. addMember(NewMembers, Rest);
  887. break;
  888. case MRICommand::CreateThin:
  889. Thin = true;
  890. LLVM_FALLTHROUGH;
  891. case MRICommand::Create:
  892. Create = true;
  893. if (!ArchiveName.empty())
  894. fail("Editing multiple archives not supported");
  895. if (Saved)
  896. fail("File already saved");
  897. ArchiveName = Rest;
  898. break;
  899. case MRICommand::Delete: {
  900. StringRef Name = normalizePath(Rest);
  901. llvm::erase_if(NewMembers,
  902. [=](NewArchiveMember &M) { return M.MemberName == Name; });
  903. break;
  904. }
  905. case MRICommand::Save:
  906. Saved = true;
  907. break;
  908. case MRICommand::End:
  909. break;
  910. case MRICommand::Invalid:
  911. fail("Unknown command: " + CommandStr);
  912. }
  913. }
  914. // Nothing to do if not saved.
  915. if (Saved)
  916. performOperation(ReplaceOrInsert, &NewMembers);
  917. exit(0);
  918. }
  919. static bool handleGenericOption(StringRef arg) {
  920. if (arg == "-help" || arg == "--help") {
  921. printHelpMessage();
  922. return true;
  923. }
  924. if (arg == "-version" || arg == "--version") {
  925. cl::PrintVersionMessage();
  926. return true;
  927. }
  928. return false;
  929. }
  930. static int ar_main(int argc, char **argv) {
  931. SmallVector<const char *, 0> Argv(argv, argv + argc);
  932. StringSaver Saver(Alloc);
  933. cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Argv);
  934. for (size_t i = 1; i < Argv.size(); ++i) {
  935. StringRef Arg = Argv[i];
  936. const char *match;
  937. auto MatchFlagWithArg = [&](const char *expected) {
  938. size_t len = strlen(expected);
  939. if (Arg == expected) {
  940. if (++i >= Argv.size())
  941. fail(std::string(expected) + " requires an argument");
  942. match = Argv[i];
  943. return true;
  944. }
  945. if (Arg.startswith(expected) && Arg.size() > len && Arg[len] == '=') {
  946. match = Arg.data() + len + 1;
  947. return true;
  948. }
  949. return false;
  950. };
  951. if (handleGenericOption(Argv[i]))
  952. return 0;
  953. if (Arg == "--") {
  954. for (; i < Argv.size(); ++i)
  955. PositionalArgs.push_back(Argv[i]);
  956. break;
  957. }
  958. if (Arg[0] == '-') {
  959. if (Arg.startswith("--"))
  960. Arg = Argv[i] + 2;
  961. else
  962. Arg = Argv[i] + 1;
  963. if (Arg == "M") {
  964. MRI = true;
  965. } else if (MatchFlagWithArg("format")) {
  966. FormatType = StringSwitch<Format>(match)
  967. .Case("default", Default)
  968. .Case("gnu", GNU)
  969. .Case("darwin", DARWIN)
  970. .Case("bsd", BSD)
  971. .Default(Unknown);
  972. if (FormatType == Unknown)
  973. fail(std::string("Invalid format ") + match);
  974. } else if (MatchFlagWithArg("plugin")) {
  975. // Ignored.
  976. } else {
  977. Options += Argv[i] + 1;
  978. }
  979. } else if (Options.empty()) {
  980. Options += Argv[i];
  981. } else {
  982. PositionalArgs.push_back(Argv[i]);
  983. }
  984. }
  985. ArchiveOperation Operation = parseCommandLine();
  986. return performOperation(Operation, nullptr);
  987. }
  988. static int ranlib_main(int argc, char **argv) {
  989. bool ArchiveSpecified = false;
  990. for (int i = 1; i < argc; ++i) {
  991. if (handleGenericOption(argv[i])) {
  992. return 0;
  993. } else {
  994. if (ArchiveSpecified)
  995. fail("Exactly one archive should be specified");
  996. ArchiveSpecified = true;
  997. ArchiveName = argv[i];
  998. }
  999. }
  1000. return performOperation(CreateSymTab, nullptr);
  1001. }
  1002. int main(int argc, char **argv) {
  1003. InitLLVM X(argc, argv);
  1004. ToolName = argv[0];
  1005. llvm::InitializeAllTargetInfos();
  1006. llvm::InitializeAllTargetMCs();
  1007. llvm::InitializeAllAsmParsers();
  1008. Stem = sys::path::stem(ToolName);
  1009. if (Stem.contains_lower("dlltool"))
  1010. return dlltoolDriverMain(makeArrayRef(argv, argc));
  1011. if (Stem.contains_lower("ranlib"))
  1012. return ranlib_main(argc, argv);
  1013. if (Stem.contains_lower("lib"))
  1014. return libDriverMain(makeArrayRef(argv, argc));
  1015. if (Stem.contains_lower("ar"))
  1016. return ar_main(argc, argv);
  1017. fail("Not ranlib, ar, lib or dlltool!");
  1018. }