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