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