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 
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/BinaryFormat/Magic.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/ArchiveWriter.h"
21 #include "llvm/Object/IRObjectFile.h"
22 #include "llvm/Object/MachO.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Object/SymbolicFile.h"
25 #include "llvm/Object/XCOFFObjectFile.h"
26 #include "llvm/Support/Chrono.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/ConvertUTF.h"
29 #include "llvm/Support/Errc.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/FormatVariadic.h"
33 #include "llvm/Support/Host.h"
34 #include "llvm/Support/InitLLVM.h"
35 #include "llvm/Support/LineIterator.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/Process.h"
39 #include "llvm/Support/StringSaver.h"
40 #include "llvm/Support/TargetSelect.h"
41 #include "llvm/Support/ToolOutputFile.h"
42 #include "llvm/Support/WithColor.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/ToolDrivers/llvm-dlltool/DlltoolDriver.h"
45 #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
46 
47 #if !defined(_MSC_VER) && !defined(__MINGW32__)
48 #include <unistd.h>
49 #else
50 #include <io.h>
51 #endif
52 
53 #ifdef _WIN32
54 #include "llvm/Support/Windows/WindowsSupport.h"
55 #endif
56 
57 using namespace llvm;
58 
59 // The name this program was invoked as.
60 static StringRef ToolName;
61 
62 // The basename of this program.
63 static StringRef Stem;
64 
65 const char RanlibHelp[] = R"(OVERVIEW: LLVM Ranlib (llvm-ranlib)
66 
67   This program generates an index to speed access to archives
68 
69 USAGE: llvm-ranlib <archive-file>
70 
71 OPTIONS:
72   -h --help             - Display available options
73   -v --version          - Display the version of this program
74   -D                    - Use zero for timestamps and uids/gids (default)
75   -U                    - Use actual timestamps and uids/gids
76 )";
77 
78 const char ArHelp[] = R"(OVERVIEW: LLVM Archiver
79 
80 USAGE: llvm-ar [options] [-]<operation>[modifiers] [relpos] [count] <archive> [files]
81        llvm-ar -M [<mri-script]
82 
83 OPTIONS:
84   --format              - archive format to create
85     =default            -   default
86     =gnu                -   gnu
87     =darwin             -   darwin
88     =bsd                -   bsd
89   --plugin=<string>     - ignored for compatibility
90   -h --help             - display this help and exit
91   --rsp-quoting         - quoting style for response files
92     =posix              -   posix
93     =windows            -   windows
94   --thin                - create a thin archive
95   --version             - print the version and exit
96   @<file>               - read options from <file>
97 
98 OPERATIONS:
99   d - delete [files] from the archive
100   m - move [files] in the archive
101   p - print contents of [files] found in the archive
102   q - quick append [files] to the archive
103   r - replace or insert [files] into the archive
104   s - act as ranlib
105   t - display list of files in archive
106   x - extract [files] from the archive
107 
108 MODIFIERS:
109   [a] - put [files] after [relpos]
110   [b] - put [files] before [relpos] (same as [i])
111   [c] - do not warn if archive had to be created
112   [D] - use zero for timestamps and uids/gids (default)
113   [h] - display this help and exit
114   [i] - put [files] before [relpos] (same as [b])
115   [l] - ignored for compatibility
116   [L] - add archive's contents
117   [N] - use instance [count] of name
118   [o] - preserve original dates
119   [O] - display member offsets
120   [P] - use full names when matching (implied for thin archives)
121   [s] - create an archive index (cf. ranlib)
122   [S] - do not build a symbol table
123   [T] - deprecated, use --thin instead
124   [u] - update only [files] newer than archive contents
125   [U] - use actual timestamps and uids/gids
126   [v] - be verbose about actions taken
127   [V] - display the version and exit
128 )";
129 
130 static void printHelpMessage() {
131   if (Stem.contains_insensitive("ranlib"))
132     outs() << RanlibHelp;
133   else if (Stem.contains_insensitive("ar"))
134     outs() << ArHelp;
135 }
136 
137 static unsigned MRILineNumber;
138 static bool ParsingMRIScript;
139 
140 // Show the error plus the usage message, and exit.
141 [[noreturn]] static void badUsage(Twine Error) {
142   WithColor::error(errs(), ToolName) << Error << "\n";
143   printHelpMessage();
144   exit(1);
145 }
146 
147 // Show the error message and exit.
148 [[noreturn]] static void fail(Twine Error) {
149   if (ParsingMRIScript) {
150     WithColor::error(errs(), ToolName)
151         << "script line " << MRILineNumber << ": " << Error << "\n";
152   } else {
153     WithColor::error(errs(), ToolName) << Error << "\n";
154   }
155   exit(1);
156 }
157 
158 static void failIfError(std::error_code EC, Twine Context = "") {
159   if (!EC)
160     return;
161 
162   std::string ContextStr = Context.str();
163   if (ContextStr.empty())
164     fail(EC.message());
165   fail(Context + ": " + EC.message());
166 }
167 
168 static void failIfError(Error E, Twine Context = "") {
169   if (!E)
170     return;
171 
172   handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
173     std::string ContextStr = Context.str();
174     if (ContextStr.empty())
175       fail(EIB.message());
176     fail(Context + ": " + EIB.message());
177   });
178 }
179 
180 static SmallVector<const char *, 256> PositionalArgs;
181 
182 static bool MRI;
183 
184 namespace {
185 enum Format { Default, GNU, BSD, DARWIN, Unknown };
186 }
187 
188 static Format FormatType = Default;
189 
190 static std::string Options;
191 
192 // This enumeration delineates the kinds of operations on an archive
193 // that are permitted.
194 enum ArchiveOperation {
195   Print,           ///< Print the contents of the archive
196   Delete,          ///< Delete the specified members
197   Move,            ///< Move members to end or as given by {a,b,i} modifiers
198   QuickAppend,     ///< Quickly append to end of archive
199   ReplaceOrInsert, ///< Replace or Insert members
200   DisplayTable,    ///< Display the table of contents
201   Extract,         ///< Extract files back to file system
202   CreateSymTab     ///< Create a symbol table in an existing archive
203 };
204 
205 // Modifiers to follow operation to vary behavior
206 static bool AddAfter = false;             ///< 'a' modifier
207 static bool AddBefore = false;            ///< 'b' modifier
208 static bool Create = false;               ///< 'c' modifier
209 static bool OriginalDates = false;        ///< 'o' modifier
210 static bool DisplayMemberOffsets = false; ///< 'O' modifier
211 static bool CompareFullPath = false;      ///< 'P' modifier
212 static bool OnlyUpdate = false;           ///< 'u' modifier
213 static bool Verbose = false;              ///< 'v' modifier
214 static bool Symtab = true;                ///< 's' modifier
215 static bool Deterministic = true;         ///< 'D' and 'U' modifiers
216 static bool Thin = false;                 ///< 'T' modifier
217 static bool AddLibrary = false;           ///< 'L' modifier
218 
219 // Relative Positional Argument (for insert/move). This variable holds
220 // the name of the archive member to which the 'a', 'b' or 'i' modifier
221 // refers. Only one of 'a', 'b' or 'i' can be specified so we only need
222 // one variable.
223 static std::string RelPos;
224 
225 // Count parameter for 'N' modifier. This variable specifies which file should
226 // match for extract/delete operations when there are multiple matches. This is
227 // 1-indexed. A value of 0 is invalid, and implies 'N' is not used.
228 static int CountParam = 0;
229 
230 // This variable holds the name of the archive file as given on the
231 // command line.
232 static std::string ArchiveName;
233 
234 static std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
235 static std::vector<std::unique_ptr<object::Archive>> Archives;
236 
237 // This variable holds the list of member files to proecess, as given
238 // on the command line.
239 static std::vector<StringRef> Members;
240 
241 // Static buffer to hold StringRefs.
242 static BumpPtrAllocator Alloc;
243 
244 // Extract the member filename from the command line for the [relpos] argument
245 // associated with a, b, and i modifiers
246 static void getRelPos() {
247   if (PositionalArgs.empty())
248     fail("expected [relpos] for 'a', 'b', or 'i' modifier");
249   RelPos = PositionalArgs[0];
250   PositionalArgs.erase(PositionalArgs.begin());
251 }
252 
253 // Extract the parameter from the command line for the [count] argument
254 // associated with the N modifier
255 static void getCountParam() {
256   if (PositionalArgs.empty())
257     badUsage("expected [count] for 'N' modifier");
258   auto CountParamArg = StringRef(PositionalArgs[0]);
259   if (CountParamArg.getAsInteger(10, CountParam))
260     badUsage("value for [count] must be numeric, got: " + CountParamArg);
261   if (CountParam < 1)
262     badUsage("value for [count] must be positive, got: " + CountParamArg);
263   PositionalArgs.erase(PositionalArgs.begin());
264 }
265 
266 // Get the archive file name from the command line
267 static void getArchive() {
268   if (PositionalArgs.empty())
269     badUsage("an archive name must be specified");
270   ArchiveName = PositionalArgs[0];
271   PositionalArgs.erase(PositionalArgs.begin());
272 }
273 
274 static object::Archive &readLibrary(const Twine &Library) {
275   auto BufOrErr = MemoryBuffer::getFile(Library, /*IsText=*/false,
276                                         /*RequiresNullTerminator=*/false);
277   failIfError(BufOrErr.getError(), "could not open library " + Library);
278   ArchiveBuffers.push_back(std::move(*BufOrErr));
279   auto LibOrErr =
280       object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
281   failIfError(errorToErrorCode(LibOrErr.takeError()),
282               "could not parse library");
283   Archives.push_back(std::move(*LibOrErr));
284   return *Archives.back();
285 }
286 
287 static void runMRIScript();
288 
289 // Parse the command line options as presented and return the operation
290 // specified. Process all modifiers and check to make sure that constraints on
291 // modifier/operation pairs have not been violated.
292 static ArchiveOperation parseCommandLine() {
293   if (MRI) {
294     if (!PositionalArgs.empty() || !Options.empty())
295       badUsage("cannot mix -M and other options");
296     runMRIScript();
297   }
298 
299   // Keep track of number of operations. We can only specify one
300   // per execution.
301   unsigned NumOperations = 0;
302 
303   // Keep track of the number of positional modifiers (a,b,i). Only
304   // one can be specified.
305   unsigned NumPositional = 0;
306 
307   // Keep track of which operation was requested
308   ArchiveOperation Operation;
309 
310   bool MaybeJustCreateSymTab = false;
311 
312   for (unsigned i = 0; i < Options.size(); ++i) {
313     switch (Options[i]) {
314     case 'd':
315       ++NumOperations;
316       Operation = Delete;
317       break;
318     case 'm':
319       ++NumOperations;
320       Operation = Move;
321       break;
322     case 'p':
323       ++NumOperations;
324       Operation = Print;
325       break;
326     case 'q':
327       ++NumOperations;
328       Operation = QuickAppend;
329       break;
330     case 'r':
331       ++NumOperations;
332       Operation = ReplaceOrInsert;
333       break;
334     case 't':
335       ++NumOperations;
336       Operation = DisplayTable;
337       break;
338     case 'x':
339       ++NumOperations;
340       Operation = Extract;
341       break;
342     case 'c':
343       Create = true;
344       break;
345     case 'l': /* accepted but unused */
346       break;
347     case 'o':
348       OriginalDates = true;
349       break;
350     case 'O':
351       DisplayMemberOffsets = true;
352       break;
353     case 'P':
354       CompareFullPath = true;
355       break;
356     case 's':
357       Symtab = true;
358       MaybeJustCreateSymTab = true;
359       break;
360     case 'S':
361       Symtab = false;
362       break;
363     case 'u':
364       OnlyUpdate = true;
365       break;
366     case 'v':
367       Verbose = true;
368       break;
369     case 'a':
370       getRelPos();
371       AddAfter = true;
372       NumPositional++;
373       break;
374     case 'b':
375       getRelPos();
376       AddBefore = true;
377       NumPositional++;
378       break;
379     case 'i':
380       getRelPos();
381       AddBefore = true;
382       NumPositional++;
383       break;
384     case 'D':
385       Deterministic = true;
386       break;
387     case 'U':
388       Deterministic = false;
389       break;
390     case 'N':
391       getCountParam();
392       break;
393     case 'T':
394       Thin = true;
395       break;
396     case 'L':
397       AddLibrary = true;
398       break;
399     case 'V':
400       cl::PrintVersionMessage();
401       exit(0);
402     case 'h':
403       printHelpMessage();
404       exit(0);
405     default:
406       badUsage(std::string("unknown option ") + Options[i]);
407     }
408   }
409 
410   // Thin archives store path names, so P should be forced.
411   if (Thin)
412     CompareFullPath = true;
413 
414   // At this point, the next thing on the command line must be
415   // the archive name.
416   getArchive();
417 
418   // Everything on the command line at this point is a member.
419   Members.assign(PositionalArgs.begin(), PositionalArgs.end());
420 
421   if (NumOperations == 0 && MaybeJustCreateSymTab) {
422     NumOperations = 1;
423     Operation = CreateSymTab;
424     if (!Members.empty())
425       badUsage("the 's' operation takes only an archive as argument");
426   }
427 
428   // Perform various checks on the operation/modifier specification
429   // to make sure we are dealing with a legal request.
430   if (NumOperations == 0)
431     badUsage("you must specify at least one of the operations");
432   if (NumOperations > 1)
433     badUsage("only one operation may be specified");
434   if (NumPositional > 1)
435     badUsage("you may only specify one of 'a', 'b', and 'i' modifiers");
436   if (AddAfter || AddBefore)
437     if (Operation != Move && Operation != ReplaceOrInsert)
438       badUsage("the 'a', 'b' and 'i' modifiers can only be specified with "
439                "the 'm' or 'r' operations");
440   if (CountParam)
441     if (Operation != Extract && Operation != Delete)
442       badUsage("the 'N' modifier can only be specified with the 'x' or 'd' "
443                "operations");
444   if (OriginalDates && Operation != Extract)
445     badUsage("the 'o' modifier is only applicable to the 'x' operation");
446   if (OnlyUpdate && Operation != ReplaceOrInsert)
447     badUsage("the 'u' modifier is only applicable to the 'r' operation");
448   if (AddLibrary && Operation != QuickAppend)
449     badUsage("the 'L' modifier is only applicable to the 'q' operation");
450 
451   // Return the parsed operation to the caller
452   return Operation;
453 }
454 
455 // Implements the 'p' operation. This function traverses the archive
456 // looking for members that match the path list.
457 static void doPrint(StringRef Name, const object::Archive::Child &C) {
458   if (Verbose)
459     outs() << "Printing " << Name << "\n";
460 
461   Expected<StringRef> DataOrErr = C.getBuffer();
462   failIfError(DataOrErr.takeError());
463   StringRef Data = *DataOrErr;
464   outs().write(Data.data(), Data.size());
465 }
466 
467 // Utility function for printing out the file mode when the 't' operation is in
468 // verbose mode.
469 static void printMode(unsigned mode) {
470   outs() << ((mode & 004) ? "r" : "-");
471   outs() << ((mode & 002) ? "w" : "-");
472   outs() << ((mode & 001) ? "x" : "-");
473 }
474 
475 // Implement the 't' operation. This function prints out just
476 // the file names of each of the members. However, if verbose mode is requested
477 // ('v' modifier) then the file type, permission mode, user, group, size, and
478 // modification time are also printed.
479 static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
480   if (Verbose) {
481     Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
482     failIfError(ModeOrErr.takeError());
483     sys::fs::perms Mode = ModeOrErr.get();
484     printMode((Mode >> 6) & 007);
485     printMode((Mode >> 3) & 007);
486     printMode(Mode & 007);
487     Expected<unsigned> UIDOrErr = C.getUID();
488     failIfError(UIDOrErr.takeError());
489     outs() << ' ' << UIDOrErr.get();
490     Expected<unsigned> GIDOrErr = C.getGID();
491     failIfError(GIDOrErr.takeError());
492     outs() << '/' << GIDOrErr.get();
493     Expected<uint64_t> Size = C.getSize();
494     failIfError(Size.takeError());
495     outs() << ' ' << format("%6llu", Size.get());
496     auto ModTimeOrErr = C.getLastModified();
497     failIfError(ModTimeOrErr.takeError());
498     // Note: formatv() only handles the default TimePoint<>, which is in
499     // nanoseconds.
500     // TODO: fix format_provider<TimePoint<>> to allow other units.
501     sys::TimePoint<> ModTimeInNs = ModTimeOrErr.get();
502     outs() << ' ' << formatv("{0:%b %e %H:%M %Y}", ModTimeInNs);
503     outs() << ' ';
504   }
505 
506   if (C.getParent()->isThin()) {
507     if (!sys::path::is_absolute(Name)) {
508       StringRef ParentDir = sys::path::parent_path(ArchiveName);
509       if (!ParentDir.empty())
510         outs() << sys::path::convert_to_slash(ParentDir) << '/';
511     }
512     outs() << Name;
513   } else {
514     outs() << Name;
515     if (DisplayMemberOffsets)
516       outs() << " 0x" << utohexstr(C.getDataOffset(), true);
517   }
518   outs() << '\n';
519 }
520 
521 static std::string normalizePath(StringRef Path) {
522   return CompareFullPath ? sys::path::convert_to_slash(Path)
523                          : std::string(sys::path::filename(Path));
524 }
525 
526 static bool comparePaths(StringRef Path1, StringRef Path2) {
527 // When on Windows this function calls CompareStringOrdinal
528 // as Windows file paths are case-insensitive.
529 // CompareStringOrdinal compares two Unicode strings for
530 // binary equivalence and allows for case insensitivity.
531 #ifdef _WIN32
532   SmallVector<wchar_t, 128> WPath1, WPath2;
533   failIfError(sys::windows::UTF8ToUTF16(normalizePath(Path1), WPath1));
534   failIfError(sys::windows::UTF8ToUTF16(normalizePath(Path2), WPath2));
535 
536   return CompareStringOrdinal(WPath1.data(), WPath1.size(), WPath2.data(),
537                               WPath2.size(), true) == CSTR_EQUAL;
538 #else
539   return normalizePath(Path1) == normalizePath(Path2);
540 #endif
541 }
542 
543 // Implement the 'x' operation. This function extracts files back to the file
544 // system.
545 static void doExtract(StringRef Name, const object::Archive::Child &C) {
546   // Retain the original mode.
547   Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
548   failIfError(ModeOrErr.takeError());
549   sys::fs::perms Mode = ModeOrErr.get();
550 
551   llvm::StringRef outputFilePath = sys::path::filename(Name);
552   if (Verbose)
553     outs() << "x - " << outputFilePath << '\n';
554 
555   int FD;
556   failIfError(sys::fs::openFileForWrite(outputFilePath, FD,
557                                         sys::fs::CD_CreateAlways,
558                                         sys::fs::OF_None, Mode),
559               Name);
560 
561   {
562     raw_fd_ostream file(FD, false);
563 
564     // Get the data and its length
565     Expected<StringRef> BufOrErr = C.getBuffer();
566     failIfError(BufOrErr.takeError());
567     StringRef Data = BufOrErr.get();
568 
569     // Write the data.
570     file.write(Data.data(), Data.size());
571   }
572 
573   // If we're supposed to retain the original modification times, etc. do so
574   // now.
575   if (OriginalDates) {
576     auto ModTimeOrErr = C.getLastModified();
577     failIfError(ModTimeOrErr.takeError());
578     failIfError(
579         sys::fs::setLastAccessAndModificationTime(FD, ModTimeOrErr.get()));
580   }
581 
582   if (close(FD))
583     fail("Could not close the file");
584 }
585 
586 static bool shouldCreateArchive(ArchiveOperation Op) {
587   switch (Op) {
588   case Print:
589   case Delete:
590   case Move:
591   case DisplayTable:
592   case Extract:
593   case CreateSymTab:
594     return false;
595 
596   case QuickAppend:
597   case ReplaceOrInsert:
598     return true;
599   }
600 
601   llvm_unreachable("Missing entry in covered switch.");
602 }
603 
604 static void performReadOperation(ArchiveOperation Operation,
605                                  object::Archive *OldArchive) {
606   if (Operation == Extract && OldArchive->isThin())
607     fail("extracting from a thin archive is not supported");
608 
609   bool Filter = !Members.empty();
610   StringMap<int> MemberCount;
611   {
612     Error Err = Error::success();
613     for (auto &C : OldArchive->children(Err)) {
614       Expected<StringRef> NameOrErr = C.getName();
615       failIfError(NameOrErr.takeError());
616       StringRef Name = NameOrErr.get();
617 
618       if (Filter) {
619         auto I = find_if(Members, [Name](StringRef Path) {
620           return comparePaths(Name, Path);
621         });
622         if (I == Members.end())
623           continue;
624         if (CountParam && ++MemberCount[Name] != CountParam)
625           continue;
626         Members.erase(I);
627       }
628 
629       switch (Operation) {
630       default:
631         llvm_unreachable("Not a read operation");
632       case Print:
633         doPrint(Name, C);
634         break;
635       case DisplayTable:
636         doDisplayTable(Name, C);
637         break;
638       case Extract:
639         doExtract(Name, C);
640         break;
641       }
642     }
643     failIfError(std::move(Err));
644   }
645 
646   if (Members.empty())
647     return;
648   for (StringRef Name : Members)
649     WithColor::error(errs(), ToolName) << "'" << Name << "' was not found\n";
650   exit(1);
651 }
652 
653 static void addChildMember(std::vector<NewArchiveMember> &Members,
654                            const object::Archive::Child &M,
655                            bool FlattenArchive = false) {
656   Expected<NewArchiveMember> NMOrErr =
657       NewArchiveMember::getOldMember(M, Deterministic);
658   failIfError(NMOrErr.takeError());
659   // If the child member we're trying to add is thin, use the path relative to
660   // the archive it's in, so the file resolves correctly.
661   if (Thin && FlattenArchive) {
662     StringSaver Saver(Alloc);
663     Expected<std::string> FileNameOrErr(M.getName());
664     failIfError(FileNameOrErr.takeError());
665     if (sys::path::is_absolute(*FileNameOrErr)) {
666       NMOrErr->MemberName = Saver.save(sys::path::convert_to_slash(*FileNameOrErr));
667     } else {
668       FileNameOrErr = M.getFullName();
669       failIfError(FileNameOrErr.takeError());
670       Expected<std::string> PathOrErr =
671           computeArchiveRelativePath(ArchiveName, *FileNameOrErr);
672       NMOrErr->MemberName = Saver.save(
673           PathOrErr ? *PathOrErr : sys::path::convert_to_slash(*FileNameOrErr));
674     }
675   }
676   if (FlattenArchive &&
677       identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) {
678     Expected<std::string> FileNameOrErr = M.getFullName();
679     failIfError(FileNameOrErr.takeError());
680     object::Archive &Lib = readLibrary(*FileNameOrErr);
681     // When creating thin archives, only flatten if the member is also thin.
682     if (!Thin || Lib.isThin()) {
683       Error Err = Error::success();
684       // Only Thin archives are recursively flattened.
685       for (auto &Child : Lib.children(Err))
686         addChildMember(Members, Child, /*FlattenArchive=*/Thin);
687       failIfError(std::move(Err));
688       return;
689     }
690   }
691   Members.push_back(std::move(*NMOrErr));
692 }
693 
694 static void addMember(std::vector<NewArchiveMember> &Members,
695                       StringRef FileName, bool FlattenArchive = false) {
696   Expected<NewArchiveMember> NMOrErr =
697       NewArchiveMember::getFile(FileName, Deterministic);
698   failIfError(NMOrErr.takeError(), FileName);
699   StringSaver Saver(Alloc);
700   // For regular archives, use the basename of the object path for the member
701   // name. For thin archives, use the full relative paths so the file resolves
702   // correctly.
703   if (!Thin) {
704     NMOrErr->MemberName = sys::path::filename(NMOrErr->MemberName);
705   } else {
706     if (sys::path::is_absolute(FileName))
707       NMOrErr->MemberName = Saver.save(sys::path::convert_to_slash(FileName));
708     else {
709       Expected<std::string> PathOrErr =
710           computeArchiveRelativePath(ArchiveName, FileName);
711       NMOrErr->MemberName = Saver.save(
712           PathOrErr ? *PathOrErr : sys::path::convert_to_slash(FileName));
713     }
714   }
715 
716   if (FlattenArchive &&
717       identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) {
718     object::Archive &Lib = readLibrary(FileName);
719     // When creating thin archives, only flatten if the member is also thin.
720     if (!Thin || Lib.isThin()) {
721       Error Err = Error::success();
722       // Only Thin archives are recursively flattened.
723       for (auto &Child : Lib.children(Err))
724         addChildMember(Members, Child, /*FlattenArchive=*/Thin);
725       failIfError(std::move(Err));
726       return;
727     }
728   }
729   Members.push_back(std::move(*NMOrErr));
730 }
731 
732 enum InsertAction {
733   IA_AddOldMember,
734   IA_AddNewMember,
735   IA_Delete,
736   IA_MoveOldMember,
737   IA_MoveNewMember
738 };
739 
740 static InsertAction computeInsertAction(ArchiveOperation Operation,
741                                         const object::Archive::Child &Member,
742                                         StringRef Name,
743                                         std::vector<StringRef>::iterator &Pos,
744                                         StringMap<int> &MemberCount) {
745   if (Operation == QuickAppend || Members.empty())
746     return IA_AddOldMember;
747   auto MI = find_if(
748       Members, [Name](StringRef Path) { return comparePaths(Name, Path); });
749 
750   if (MI == Members.end())
751     return IA_AddOldMember;
752 
753   Pos = MI;
754 
755   if (Operation == Delete) {
756     if (CountParam && ++MemberCount[Name] != CountParam)
757       return IA_AddOldMember;
758     return IA_Delete;
759   }
760 
761   if (Operation == Move)
762     return IA_MoveOldMember;
763 
764   if (Operation == ReplaceOrInsert) {
765     if (!OnlyUpdate) {
766       if (RelPos.empty())
767         return IA_AddNewMember;
768       return IA_MoveNewMember;
769     }
770 
771     // We could try to optimize this to a fstat, but it is not a common
772     // operation.
773     sys::fs::file_status Status;
774     failIfError(sys::fs::status(*MI, Status), *MI);
775     auto ModTimeOrErr = Member.getLastModified();
776     failIfError(ModTimeOrErr.takeError());
777     if (Status.getLastModificationTime() < ModTimeOrErr.get()) {
778       if (RelPos.empty())
779         return IA_AddOldMember;
780       return IA_MoveOldMember;
781     }
782 
783     if (RelPos.empty())
784       return IA_AddNewMember;
785     return IA_MoveNewMember;
786   }
787   llvm_unreachable("No such operation");
788 }
789 
790 // We have to walk this twice and computing it is not trivial, so creating an
791 // explicit std::vector is actually fairly efficient.
792 static std::vector<NewArchiveMember>
793 computeNewArchiveMembers(ArchiveOperation Operation,
794                          object::Archive *OldArchive) {
795   std::vector<NewArchiveMember> Ret;
796   std::vector<NewArchiveMember> Moved;
797   int InsertPos = -1;
798   if (OldArchive) {
799     Error Err = Error::success();
800     StringMap<int> MemberCount;
801     for (auto &Child : OldArchive->children(Err)) {
802       int Pos = Ret.size();
803       Expected<StringRef> NameOrErr = Child.getName();
804       failIfError(NameOrErr.takeError());
805       std::string Name = std::string(NameOrErr.get());
806       if (comparePaths(Name, RelPos)) {
807         assert(AddAfter || AddBefore);
808         if (AddBefore)
809           InsertPos = Pos;
810         else
811           InsertPos = Pos + 1;
812       }
813 
814       std::vector<StringRef>::iterator MemberI = Members.end();
815       InsertAction Action =
816           computeInsertAction(Operation, Child, Name, MemberI, MemberCount);
817       switch (Action) {
818       case IA_AddOldMember:
819         addChildMember(Ret, Child, /*FlattenArchive=*/Thin);
820         break;
821       case IA_AddNewMember:
822         addMember(Ret, *MemberI);
823         break;
824       case IA_Delete:
825         break;
826       case IA_MoveOldMember:
827         addChildMember(Moved, Child, /*FlattenArchive=*/Thin);
828         break;
829       case IA_MoveNewMember:
830         addMember(Moved, *MemberI);
831         break;
832       }
833       // When processing elements with the count param, we need to preserve the
834       // full members list when iterating over all archive members. For
835       // instance, "llvm-ar dN 2 archive.a member.o" should delete the second
836       // file named member.o it sees; we are not done with member.o the first
837       // time we see it in the archive.
838       if (MemberI != Members.end() && !CountParam)
839         Members.erase(MemberI);
840     }
841     failIfError(std::move(Err));
842   }
843 
844   if (Operation == Delete)
845     return Ret;
846 
847   if (!RelPos.empty() && InsertPos == -1)
848     fail("insertion point not found");
849 
850   if (RelPos.empty())
851     InsertPos = Ret.size();
852 
853   assert(unsigned(InsertPos) <= Ret.size());
854   int Pos = InsertPos;
855   for (auto &M : Moved) {
856     Ret.insert(Ret.begin() + Pos, std::move(M));
857     ++Pos;
858   }
859 
860   if (AddLibrary) {
861     assert(Operation == QuickAppend);
862     for (auto &Member : Members)
863       addMember(Ret, Member, /*FlattenArchive=*/true);
864     return Ret;
865   }
866 
867   std::vector<NewArchiveMember> NewMembers;
868   for (auto &Member : Members)
869     addMember(NewMembers, Member, /*FlattenArchive=*/Thin);
870   Ret.reserve(Ret.size() + NewMembers.size());
871   std::move(NewMembers.begin(), NewMembers.end(),
872             std::inserter(Ret, std::next(Ret.begin(), InsertPos)));
873 
874   return Ret;
875 }
876 
877 static object::Archive::Kind getDefaultForHost() {
878   Triple HostTriple(sys::getProcessTriple());
879   return HostTriple.isOSDarwin()
880              ? object::Archive::K_DARWIN
881              : (HostTriple.isOSAIX() ? object::Archive::K_AIXBIG
882                                      : object::Archive::K_GNU);
883 }
884 
885 static object::Archive::Kind getKindFromMember(const NewArchiveMember &Member) {
886   auto MemBufferRef = Member.Buf->getMemBufferRef();
887   Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
888       object::ObjectFile::createObjectFile(MemBufferRef);
889 
890   if (OptionalObject)
891     return isa<object::MachOObjectFile>(**OptionalObject)
892                ? object::Archive::K_DARWIN
893                : (isa<object::XCOFFObjectFile>(**OptionalObject)
894                       ? object::Archive::K_AIXBIG
895                       : object::Archive::K_GNU);
896 
897   // squelch the error in case we had a non-object file
898   consumeError(OptionalObject.takeError());
899 
900   // If we're adding a bitcode file to the archive, detect the Archive kind
901   // based on the target triple.
902   LLVMContext Context;
903   if (identify_magic(MemBufferRef.getBuffer()) == file_magic::bitcode) {
904     if (auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
905             MemBufferRef, file_magic::bitcode, &Context)) {
906       auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr);
907       return Triple(IRObject.getTargetTriple()).isOSDarwin()
908                  ? object::Archive::K_DARWIN
909                  : object::Archive::K_GNU;
910     } else {
911       // Squelch the error in case this was not a SymbolicFile.
912       consumeError(ObjOrErr.takeError());
913     }
914   }
915 
916   return getDefaultForHost();
917 }
918 
919 static void performWriteOperation(ArchiveOperation Operation,
920                                   object::Archive *OldArchive,
921                                   std::unique_ptr<MemoryBuffer> OldArchiveBuf,
922                                   std::vector<NewArchiveMember> *NewMembersP) {
923   if (OldArchive) {
924     if (Thin && !OldArchive->isThin())
925       fail("cannot convert a regular archive to a thin one");
926 
927     if (OldArchive->isThin())
928       Thin = true;
929   }
930 
931   std::vector<NewArchiveMember> NewMembers;
932   if (!NewMembersP)
933     NewMembers = computeNewArchiveMembers(Operation, OldArchive);
934 
935   object::Archive::Kind Kind;
936   switch (FormatType) {
937   case Default:
938     if (Thin)
939       Kind = object::Archive::K_GNU;
940     else if (OldArchive)
941       Kind = OldArchive->kind();
942     else if (NewMembersP)
943       Kind = !NewMembersP->empty() ? getKindFromMember(NewMembersP->front())
944                                    : getDefaultForHost();
945     else
946       Kind = !NewMembers.empty() ? getKindFromMember(NewMembers.front())
947                                  : getDefaultForHost();
948     if (Kind == object::Archive::K_AIXBIG)
949       fail("big archive writer operation on AIX not yet supported");
950     break;
951   case GNU:
952     Kind = object::Archive::K_GNU;
953     break;
954   case BSD:
955     if (Thin)
956       fail("only the gnu format has a thin mode");
957     Kind = object::Archive::K_BSD;
958     break;
959   case DARWIN:
960     if (Thin)
961       fail("only the gnu format has a thin mode");
962     Kind = object::Archive::K_DARWIN;
963     break;
964   case Unknown:
965     llvm_unreachable("");
966   }
967 
968   Error E =
969       writeArchive(ArchiveName, NewMembersP ? *NewMembersP : NewMembers, Symtab,
970                    Kind, Deterministic, Thin, std::move(OldArchiveBuf));
971   failIfError(std::move(E), ArchiveName);
972 }
973 
974 static void createSymbolTable(object::Archive *OldArchive) {
975   // When an archive is created or modified, if the s option is given, the
976   // resulting archive will have a current symbol table. If the S option
977   // is given, it will have no symbol table.
978   // In summary, we only need to update the symbol table if we have none.
979   // This is actually very common because of broken build systems that think
980   // they have to run ranlib.
981   if (OldArchive->hasSymbolTable())
982     return;
983 
984   if (OldArchive->isThin())
985     Thin = true;
986   performWriteOperation(CreateSymTab, OldArchive, nullptr, nullptr);
987 }
988 
989 static void performOperation(ArchiveOperation Operation,
990                              object::Archive *OldArchive,
991                              std::unique_ptr<MemoryBuffer> OldArchiveBuf,
992                              std::vector<NewArchiveMember> *NewMembers) {
993   switch (Operation) {
994   case Print:
995   case DisplayTable:
996   case Extract:
997     performReadOperation(Operation, OldArchive);
998     return;
999 
1000   case Delete:
1001   case Move:
1002   case QuickAppend:
1003   case ReplaceOrInsert:
1004     performWriteOperation(Operation, OldArchive, std::move(OldArchiveBuf),
1005                           NewMembers);
1006     return;
1007   case CreateSymTab:
1008     createSymbolTable(OldArchive);
1009     return;
1010   }
1011   llvm_unreachable("Unknown operation.");
1012 }
1013 
1014 static int performOperation(ArchiveOperation Operation,
1015                             std::vector<NewArchiveMember> *NewMembers) {
1016   // Create or open the archive object.
1017   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
1018       ArchiveName, /*IsText=*/false, /*RequiresNullTerminator=*/false);
1019   std::error_code EC = Buf.getError();
1020   if (EC && EC != errc::no_such_file_or_directory)
1021     fail("unable to open '" + ArchiveName + "': " + EC.message());
1022 
1023   if (!EC) {
1024     Expected<std::unique_ptr<object::Archive>> ArchiveOrError =
1025         object::Archive::create(Buf.get()->getMemBufferRef());
1026     if (!ArchiveOrError)
1027       failIfError(ArchiveOrError.takeError(),
1028                   "unable to load '" + ArchiveName + "'");
1029 
1030     std::unique_ptr<object::Archive> Archive = std::move(ArchiveOrError.get());
1031     if (Archive->isThin())
1032       CompareFullPath = true;
1033     performOperation(Operation, Archive.get(), std::move(Buf.get()),
1034                      NewMembers);
1035     return 0;
1036   }
1037 
1038   assert(EC == errc::no_such_file_or_directory);
1039 
1040   if (!shouldCreateArchive(Operation)) {
1041     failIfError(EC, Twine("unable to load '") + ArchiveName + "'");
1042   } else {
1043     if (!Create) {
1044       // Produce a warning if we should and we're creating the archive
1045       WithColor::warning(errs(), ToolName)
1046           << "creating " << ArchiveName << "\n";
1047     }
1048   }
1049 
1050   performOperation(Operation, nullptr, nullptr, NewMembers);
1051   return 0;
1052 }
1053 
1054 static void runMRIScript() {
1055   enum class MRICommand { AddLib, AddMod, Create, CreateThin, Delete, Save, End, Invalid };
1056 
1057   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
1058   failIfError(Buf.getError());
1059   const MemoryBuffer &Ref = *Buf.get();
1060   bool Saved = false;
1061   std::vector<NewArchiveMember> NewMembers;
1062   ParsingMRIScript = true;
1063 
1064   for (line_iterator I(Ref, /*SkipBlanks*/ false), E; I != E; ++I) {
1065     ++MRILineNumber;
1066     StringRef Line = *I;
1067     Line = Line.split(';').first;
1068     Line = Line.split('*').first;
1069     Line = Line.trim();
1070     if (Line.empty())
1071       continue;
1072     StringRef CommandStr, Rest;
1073     std::tie(CommandStr, Rest) = Line.split(' ');
1074     Rest = Rest.trim();
1075     if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
1076       Rest = Rest.drop_front().drop_back();
1077     auto Command = StringSwitch<MRICommand>(CommandStr.lower())
1078                        .Case("addlib", MRICommand::AddLib)
1079                        .Case("addmod", MRICommand::AddMod)
1080                        .Case("create", MRICommand::Create)
1081                        .Case("createthin", MRICommand::CreateThin)
1082                        .Case("delete", MRICommand::Delete)
1083                        .Case("save", MRICommand::Save)
1084                        .Case("end", MRICommand::End)
1085                        .Default(MRICommand::Invalid);
1086 
1087     switch (Command) {
1088     case MRICommand::AddLib: {
1089       object::Archive &Lib = readLibrary(Rest);
1090       {
1091         Error Err = Error::success();
1092         for (auto &Member : Lib.children(Err))
1093           addChildMember(NewMembers, Member, /*FlattenArchive=*/Thin);
1094         failIfError(std::move(Err));
1095       }
1096       break;
1097     }
1098     case MRICommand::AddMod:
1099       addMember(NewMembers, Rest);
1100       break;
1101     case MRICommand::CreateThin:
1102       Thin = true;
1103       LLVM_FALLTHROUGH;
1104     case MRICommand::Create:
1105       Create = true;
1106       if (!ArchiveName.empty())
1107         fail("editing multiple archives not supported");
1108       if (Saved)
1109         fail("file already saved");
1110       ArchiveName = std::string(Rest);
1111       break;
1112     case MRICommand::Delete: {
1113       llvm::erase_if(NewMembers, [=](NewArchiveMember &M) {
1114         return comparePaths(M.MemberName, Rest);
1115       });
1116       break;
1117     }
1118     case MRICommand::Save:
1119       Saved = true;
1120       break;
1121     case MRICommand::End:
1122       break;
1123     case MRICommand::Invalid:
1124       fail("unknown command: " + CommandStr);
1125     }
1126   }
1127 
1128   ParsingMRIScript = false;
1129 
1130   // Nothing to do if not saved.
1131   if (Saved)
1132     performOperation(ReplaceOrInsert, &NewMembers);
1133   exit(0);
1134 }
1135 
1136 static bool handleGenericOption(StringRef arg) {
1137   if (arg == "--help" || arg == "-h") {
1138     printHelpMessage();
1139     return true;
1140   }
1141   if (arg == "--version") {
1142     cl::PrintVersionMessage();
1143     return true;
1144   }
1145   return false;
1146 }
1147 
1148 static const char *matchFlagWithArg(StringRef Expected,
1149                                     ArrayRef<const char *>::iterator &ArgIt,
1150                                     ArrayRef<const char *> Args) {
1151   StringRef Arg = *ArgIt;
1152 
1153   if (Arg.startswith("--"))
1154     Arg = Arg.substr(2);
1155 
1156   size_t len = Expected.size();
1157   if (Arg == Expected) {
1158     if (++ArgIt == Args.end())
1159       fail(std::string(Expected) + " requires an argument");
1160 
1161     return *ArgIt;
1162   }
1163   if (Arg.startswith(Expected) && Arg.size() > len && Arg[len] == '=')
1164     return Arg.data() + len + 1;
1165 
1166   return nullptr;
1167 }
1168 
1169 static cl::TokenizerCallback getRspQuoting(ArrayRef<const char *> ArgsArr) {
1170   cl::TokenizerCallback Ret =
1171       Triple(sys::getProcessTriple()).getOS() == Triple::Win32
1172           ? cl::TokenizeWindowsCommandLine
1173           : cl::TokenizeGNUCommandLine;
1174 
1175   for (ArrayRef<const char *>::iterator ArgIt = ArgsArr.begin();
1176        ArgIt != ArgsArr.end(); ++ArgIt) {
1177     if (const char *Match = matchFlagWithArg("rsp-quoting", ArgIt, ArgsArr)) {
1178       StringRef MatchRef = Match;
1179       if (MatchRef == "posix")
1180         Ret = cl::TokenizeGNUCommandLine;
1181       else if (MatchRef == "windows")
1182         Ret = cl::TokenizeWindowsCommandLine;
1183       else
1184         fail(std::string("Invalid response file quoting style ") + Match);
1185     }
1186   }
1187 
1188   return Ret;
1189 }
1190 
1191 static int ar_main(int argc, char **argv) {
1192   SmallVector<const char *, 0> Argv(argv + 1, argv + argc);
1193   StringSaver Saver(Alloc);
1194 
1195   cl::ExpandResponseFiles(Saver, getRspQuoting(makeArrayRef(argv, argc)), Argv);
1196 
1197   for (ArrayRef<const char *>::iterator ArgIt = Argv.begin();
1198        ArgIt != Argv.end(); ++ArgIt) {
1199     const char *Match = nullptr;
1200 
1201     if (handleGenericOption(*ArgIt))
1202       return 0;
1203     if (strcmp(*ArgIt, "--") == 0) {
1204       ++ArgIt;
1205       for (; ArgIt != Argv.end(); ++ArgIt)
1206         PositionalArgs.push_back(*ArgIt);
1207       break;
1208     }
1209 
1210     if (*ArgIt[0] != '-') {
1211       if (Options.empty())
1212         Options += *ArgIt;
1213       else
1214         PositionalArgs.push_back(*ArgIt);
1215       continue;
1216     }
1217 
1218     if (strcmp(*ArgIt, "-M") == 0) {
1219       MRI = true;
1220       continue;
1221     }
1222 
1223     if (strcmp(*ArgIt, "--thin") == 0) {
1224       Thin = true;
1225       continue;
1226     }
1227 
1228     Match = matchFlagWithArg("format", ArgIt, Argv);
1229     if (Match) {
1230       FormatType = StringSwitch<Format>(Match)
1231                        .Case("default", Default)
1232                        .Case("gnu", GNU)
1233                        .Case("darwin", DARWIN)
1234                        .Case("bsd", BSD)
1235                        .Default(Unknown);
1236       if (FormatType == Unknown)
1237         fail(std::string("Invalid format ") + Match);
1238       continue;
1239     }
1240 
1241     if (matchFlagWithArg("plugin", ArgIt, Argv) ||
1242         matchFlagWithArg("rsp-quoting", ArgIt, Argv))
1243       continue;
1244 
1245     Options += *ArgIt + 1;
1246   }
1247 
1248   ArchiveOperation Operation = parseCommandLine();
1249   return performOperation(Operation, nullptr);
1250 }
1251 
1252 static int ranlib_main(int argc, char **argv) {
1253   bool ArchiveSpecified = false;
1254   for (int i = 1; i < argc; ++i) {
1255     StringRef arg(argv[i]);
1256     if (handleGenericOption(arg)) {
1257       return 0;
1258     } else if (arg.consume_front("-")) {
1259       // Handle the -D/-U flag
1260       while (!arg.empty()) {
1261         if (arg.front() == 'D') {
1262           Deterministic = true;
1263         } else if (arg.front() == 'U') {
1264           Deterministic = false;
1265         } else if (arg.front() == 'h') {
1266           printHelpMessage();
1267           return 0;
1268         } else if (arg.front() == 'v') {
1269           cl::PrintVersionMessage();
1270           return 0;
1271         } else {
1272           // TODO: GNU ranlib also supports a -t flag
1273           fail("Invalid option: '-" + arg + "'");
1274         }
1275         arg = arg.drop_front(1);
1276       }
1277     } else {
1278       if (ArchiveSpecified)
1279         fail("exactly one archive should be specified");
1280       ArchiveSpecified = true;
1281       ArchiveName = arg.str();
1282     }
1283   }
1284   if (!ArchiveSpecified) {
1285     badUsage("an archive name must be specified");
1286   }
1287   return performOperation(CreateSymTab, nullptr);
1288 }
1289 
1290 int main(int argc, char **argv) {
1291   InitLLVM X(argc, argv);
1292   ToolName = argv[0];
1293 
1294   llvm::InitializeAllTargetInfos();
1295   llvm::InitializeAllTargetMCs();
1296   llvm::InitializeAllAsmParsers();
1297 
1298   Stem = sys::path::stem(ToolName);
1299   auto Is = [](StringRef Tool) {
1300     // We need to recognize the following filenames.
1301     //
1302     // Lib.exe -> lib (see D44808, MSBuild runs Lib.exe)
1303     // dlltool.exe -> dlltool
1304     // arm-pokymllib32-linux-gnueabi-llvm-ar-10 -> ar
1305     auto I = Stem.rfind_insensitive(Tool);
1306     return I != StringRef::npos &&
1307            (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
1308   };
1309 
1310   if (Is("dlltool"))
1311     return dlltoolDriverMain(makeArrayRef(argv, argc));
1312   if (Is("ranlib"))
1313     return ranlib_main(argc, argv);
1314   if (Is("lib"))
1315     return libDriverMain(makeArrayRef(argv, argc));
1316   if (Is("ar"))
1317     return ar_main(argc, argv);
1318 
1319   fail("not ranlib, ar, lib or dlltool");
1320 }
1321