1//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Builds up (relatively) standard unix archive files (.a) containing LLVM
11// bitcode or other files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Triple.h"
17#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
19#include "llvm/LibDriver/LibDriver.h"
20#include "llvm/Object/Archive.h"
21#include "llvm/Object/ArchiveWriter.h"
22#include "llvm/Object/MachO.h"
23#include "llvm/Object/ObjectFile.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Errc.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/LineIterator.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/Path.h"
32#include "llvm/Support/PrettyStackTrace.h"
33#include "llvm/Support/Signals.h"
34#include "llvm/Support/TargetSelect.h"
35#include "llvm/Support/ToolOutputFile.h"
36#include "llvm/Support/raw_ostream.h"
37#include <algorithm>
38#include <cstdlib>
39#include <memory>
40
41#if !defined(_MSC_VER) && !defined(__MINGW32__)
42#include <unistd.h>
43#else
44#include <io.h>
45#endif
46
47using namespace llvm;
48
49// The name this program was invoked as.
50static StringRef ToolName;
51
52// Show the error message and exit.
53LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
54  outs() << ToolName << ": " << Error << ".\n";
55  exit(1);
56}
57
58static void failIfError(std::error_code EC, Twine Context = "") {
59  if (!EC)
60    return;
61
62  std::string ContextStr = Context.str();
63  if (ContextStr == "")
64    fail(EC.message());
65  fail(Context + ": " + EC.message());
66}
67
68static void failIfError(Error E, Twine Context = "") {
69  if (!E)
70    return;
71
72  handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
73    std::string ContextStr = Context.str();
74    if (ContextStr == "")
75      fail(EIB.message());
76    fail(Context + ": " + EIB.message());
77  });
78}
79
80// llvm-ar/llvm-ranlib remaining positional arguments.
81static cl::list<std::string>
82    RestOfArgs(cl::Positional, cl::ZeroOrMore,
83               cl::desc("[relpos] [count] <archive-file> [members]..."));
84
85static cl::opt<bool> MRI("M", cl::desc(""));
86static cl::opt<std::string> Plugin("plugin", cl::desc("plugin (ignored for compatibility"));
87
88namespace {
89enum Format { Default, GNU, BSD };
90}
91
92static cl::opt<Format>
93    FormatOpt("format", cl::desc("Archive format to create"),
94              cl::values(clEnumValN(Default, "default", "default"),
95                         clEnumValN(GNU, "gnu", "gnu"),
96                         clEnumValN(BSD, "bsd", "bsd"), clEnumValEnd));
97
98static std::string Options;
99
100// Provide additional help output explaining the operations and modifiers of
101// llvm-ar. This object instructs the CommandLine library to print the text of
102// the constructor when the --help option is given.
103static cl::extrahelp MoreHelp(
104  "\nOPERATIONS:\n"
105  "  d[NsS]       - delete file(s) from the archive\n"
106  "  m[abiSs]     - move file(s) in the archive\n"
107  "  p[kN]        - print file(s) found in the archive\n"
108  "  q[ufsS]      - quick append file(s) to the archive\n"
109  "  r[abfiuRsS]  - replace or insert file(s) into the archive\n"
110  "  t            - display contents of archive\n"
111  "  x[No]        - extract file(s) from the archive\n"
112  "\nMODIFIERS (operation specific):\n"
113  "  [a] - put file(s) after [relpos]\n"
114  "  [b] - put file(s) before [relpos] (same as [i])\n"
115  "  [i] - put file(s) before [relpos] (same as [b])\n"
116  "  [o] - preserve original dates\n"
117  "  [s] - create an archive index (cf. ranlib)\n"
118  "  [S] - do not build a symbol table\n"
119  "  [u] - update only files newer than archive contents\n"
120  "\nMODIFIERS (generic):\n"
121  "  [c] - do not warn if the library had to be created\n"
122  "  [v] - be verbose about actions taken\n"
123);
124
125// This enumeration delineates the kinds of operations on an archive
126// that are permitted.
127enum ArchiveOperation {
128  Print,            ///< Print the contents of the archive
129  Delete,           ///< Delete the specified members
130  Move,             ///< Move members to end or as given by {a,b,i} modifiers
131  QuickAppend,      ///< Quickly append to end of archive
132  ReplaceOrInsert,  ///< Replace or Insert members
133  DisplayTable,     ///< Display the table of contents
134  Extract,          ///< Extract files back to file system
135  CreateSymTab      ///< Create a symbol table in an existing archive
136};
137
138// Modifiers to follow operation to vary behavior
139static bool AddAfter = false;      ///< 'a' modifier
140static bool AddBefore = false;     ///< 'b' modifier
141static bool Create = false;        ///< 'c' modifier
142static bool OriginalDates = false; ///< 'o' modifier
143static bool OnlyUpdate = false;    ///< 'u' modifier
144static bool Verbose = false;       ///< 'v' modifier
145static bool Symtab = true;         ///< 's' modifier
146static bool Deterministic = true;  ///< 'D' and 'U' modifiers
147static bool Thin = false;          ///< 'T' modifier
148
149// Relative Positional Argument (for insert/move). This variable holds
150// the name of the archive member to which the 'a', 'b' or 'i' modifier
151// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
152// one variable.
153static std::string RelPos;
154
155// This variable holds the name of the archive file as given on the
156// command line.
157static std::string ArchiveName;
158
159// This variable holds the list of member files to proecess, as given
160// on the command line.
161static std::vector<StringRef> Members;
162
163// Show the error message, the help message and exit.
164LLVM_ATTRIBUTE_NORETURN static void
165show_help(const std::string &msg) {
166  errs() << ToolName << ": " << msg << "\n\n";
167  cl::PrintHelpMessage();
168  std::exit(1);
169}
170
171// Extract the member filename from the command line for the [relpos] argument
172// associated with a, b, and i modifiers
173static void getRelPos() {
174  if(RestOfArgs.size() == 0)
175    show_help("Expected [relpos] for a, b, or i modifier");
176  RelPos = RestOfArgs[0];
177  RestOfArgs.erase(RestOfArgs.begin());
178}
179
180static void getOptions() {
181  if(RestOfArgs.size() == 0)
182    show_help("Expected options");
183  Options = RestOfArgs[0];
184  RestOfArgs.erase(RestOfArgs.begin());
185}
186
187// Get the archive file name from the command line
188static void getArchive() {
189  if(RestOfArgs.size() == 0)
190    show_help("An archive name must be specified");
191  ArchiveName = RestOfArgs[0];
192  RestOfArgs.erase(RestOfArgs.begin());
193}
194
195// Copy over remaining items in RestOfArgs to our Members vector
196static void getMembers() {
197  for (auto &Arg : RestOfArgs)
198    Members.push_back(Arg);
199}
200
201static void runMRIScript();
202
203// Parse the command line options as presented and return the operation
204// specified. Process all modifiers and check to make sure that constraints on
205// modifier/operation pairs have not been violated.
206static ArchiveOperation parseCommandLine() {
207  if (MRI) {
208    if (!RestOfArgs.empty())
209      fail("Cannot mix -M and other options");
210    runMRIScript();
211  }
212
213  getOptions();
214
215  // Keep track of number of operations. We can only specify one
216  // per execution.
217  unsigned NumOperations = 0;
218
219  // Keep track of the number of positional modifiers (a,b,i). Only
220  // one can be specified.
221  unsigned NumPositional = 0;
222
223  // Keep track of which operation was requested
224  ArchiveOperation Operation;
225
226  bool MaybeJustCreateSymTab = false;
227
228  for(unsigned i=0; i<Options.size(); ++i) {
229    switch(Options[i]) {
230    case 'd': ++NumOperations; Operation = Delete; break;
231    case 'm': ++NumOperations; Operation = Move ; break;
232    case 'p': ++NumOperations; Operation = Print; break;
233    case 'q': ++NumOperations; Operation = QuickAppend; break;
234    case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
235    case 't': ++NumOperations; Operation = DisplayTable; break;
236    case 'x': ++NumOperations; Operation = Extract; break;
237    case 'c': Create = true; break;
238    case 'l': /* accepted but unused */ break;
239    case 'o': OriginalDates = true; break;
240    case 's':
241      Symtab = true;
242      MaybeJustCreateSymTab = true;
243      break;
244    case 'S':
245      Symtab = false;
246      break;
247    case 'u': OnlyUpdate = true; break;
248    case 'v': Verbose = true; break;
249    case 'a':
250      getRelPos();
251      AddAfter = true;
252      NumPositional++;
253      break;
254    case 'b':
255      getRelPos();
256      AddBefore = true;
257      NumPositional++;
258      break;
259    case 'i':
260      getRelPos();
261      AddBefore = true;
262      NumPositional++;
263      break;
264    case 'D':
265      Deterministic = true;
266      break;
267    case 'U':
268      Deterministic = false;
269      break;
270    case 'T':
271      Thin = true;
272      break;
273    default:
274      cl::PrintHelpMessage();
275    }
276  }
277
278  // At this point, the next thing on the command line must be
279  // the archive name.
280  getArchive();
281
282  // Everything on the command line at this point is a member.
283  getMembers();
284
285 if (NumOperations == 0 && MaybeJustCreateSymTab) {
286    NumOperations = 1;
287    Operation = CreateSymTab;
288    if (!Members.empty())
289      show_help("The s operation takes only an archive as argument");
290  }
291
292  // Perform various checks on the operation/modifier specification
293  // to make sure we are dealing with a legal request.
294  if (NumOperations == 0)
295    show_help("You must specify at least one of the operations");
296  if (NumOperations > 1)
297    show_help("Only one operation may be specified");
298  if (NumPositional > 1)
299    show_help("You may only specify one of a, b, and i modifiers");
300  if (AddAfter || AddBefore) {
301    if (Operation != Move && Operation != ReplaceOrInsert)
302      show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
303            "the 'm' or 'r' operations");
304  }
305  if (OriginalDates && Operation != Extract)
306    show_help("The 'o' modifier is only applicable to the 'x' operation");
307  if (OnlyUpdate && Operation != ReplaceOrInsert)
308    show_help("The 'u' modifier is only applicable to the 'r' operation");
309
310  // Return the parsed operation to the caller
311  return Operation;
312}
313
314// Implements the 'p' operation. This function traverses the archive
315// looking for members that match the path list.
316static void doPrint(StringRef Name, const object::Archive::Child &C) {
317  if (Verbose)
318    outs() << "Printing " << Name << "\n";
319
320  ErrorOr<StringRef> DataOrErr = C.getBuffer();
321  failIfError(DataOrErr.getError());
322  StringRef Data = *DataOrErr;
323  outs().write(Data.data(), Data.size());
324}
325
326// Utility function for printing out the file mode when the 't' operation is in
327// verbose mode.
328static void printMode(unsigned mode) {
329  outs() << ((mode & 004) ? "r" : "-");
330  outs() << ((mode & 002) ? "w" : "-");
331  outs() << ((mode & 001) ? "x" : "-");
332}
333
334// Implement the 't' operation. This function prints out just
335// the file names of each of the members. However, if verbose mode is requested
336// ('v' modifier) then the file type, permission mode, user, group, size, and
337// modification time are also printed.
338static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
339  if (Verbose) {
340    sys::fs::perms Mode = C.getAccessMode();
341    printMode((Mode >> 6) & 007);
342    printMode((Mode >> 3) & 007);
343    printMode(Mode & 007);
344    outs() << ' ' << C.getUID();
345    outs() << '/' << C.getGID();
346    ErrorOr<uint64_t> Size = C.getSize();
347    failIfError(Size.getError());
348    outs() << ' ' << format("%6llu", Size.get());
349    outs() << ' ' << C.getLastModified().str();
350    outs() << ' ';
351  }
352  outs() << Name << "\n";
353}
354
355// Implement the 'x' operation. This function extracts files back to the file
356// system.
357static void doExtract(StringRef Name, const object::Archive::Child &C) {
358  // Retain the original mode.
359  sys::fs::perms Mode = C.getAccessMode();
360
361  int FD;
362  failIfError(sys::fs::openFileForWrite(Name, FD, sys::fs::F_None, Mode), Name);
363
364  {
365    raw_fd_ostream file(FD, false);
366
367    // Get the data and its length
368    StringRef Data = *C.getBuffer();
369
370    // Write the data.
371    file.write(Data.data(), Data.size());
372  }
373
374  // If we're supposed to retain the original modification times, etc. do so
375  // now.
376  if (OriginalDates)
377    failIfError(
378        sys::fs::setLastModificationAndAccessTime(FD, C.getLastModified()));
379
380  if (close(FD))
381    fail("Could not close the file");
382}
383
384static bool shouldCreateArchive(ArchiveOperation Op) {
385  switch (Op) {
386  case Print:
387  case Delete:
388  case Move:
389  case DisplayTable:
390  case Extract:
391  case CreateSymTab:
392    return false;
393
394  case QuickAppend:
395  case ReplaceOrInsert:
396    return true;
397  }
398
399  llvm_unreachable("Missing entry in covered switch.");
400}
401
402static void performReadOperation(ArchiveOperation Operation,
403                                 object::Archive *OldArchive) {
404  if (Operation == Extract && OldArchive->isThin())
405    fail("extracting from a thin archive is not supported");
406
407  bool Filter = !Members.empty();
408  {
409    Error Err;
410    for (auto &C : OldArchive->children(Err)) {
411      ErrorOr<StringRef> NameOrErr = C.getName();
412      failIfError(NameOrErr.getError());
413      StringRef Name = NameOrErr.get();
414
415      if (Filter) {
416        auto I = std::find(Members.begin(), Members.end(), Name);
417        if (I == Members.end())
418          continue;
419        Members.erase(I);
420      }
421
422      switch (Operation) {
423      default:
424        llvm_unreachable("Not a read operation");
425      case Print:
426        doPrint(Name, C);
427        break;
428      case DisplayTable:
429        doDisplayTable(Name, C);
430        break;
431      case Extract:
432        doExtract(Name, C);
433        break;
434      }
435    }
436    failIfError(std::move(Err));
437  }
438
439  if (Members.empty())
440    return;
441  for (StringRef Name : Members)
442    errs() << Name << " was not found\n";
443  std::exit(1);
444}
445
446static void addMember(std::vector<NewArchiveMember> &Members,
447                      StringRef FileName, int Pos = -1) {
448  Expected<NewArchiveMember> NMOrErr =
449      NewArchiveMember::getFile(FileName, Deterministic);
450  failIfError(NMOrErr.takeError(), FileName);
451  if (Pos == -1)
452    Members.push_back(std::move(*NMOrErr));
453  else
454    Members[Pos] = std::move(*NMOrErr);
455}
456
457static void addMember(std::vector<NewArchiveMember> &Members,
458                      const object::Archive::Child &M, int Pos = -1) {
459  if (Thin && !M.getParent()->isThin())
460    fail("Cannot convert a regular archive to a thin one");
461  Expected<NewArchiveMember> NMOrErr =
462      NewArchiveMember::getOldMember(M, Deterministic);
463  failIfError(NMOrErr.takeError());
464  if (Pos == -1)
465    Members.push_back(std::move(*NMOrErr));
466  else
467    Members[Pos] = std::move(*NMOrErr);
468}
469
470enum InsertAction {
471  IA_AddOldMember,
472  IA_AddNewMeber,
473  IA_Delete,
474  IA_MoveOldMember,
475  IA_MoveNewMember
476};
477
478static InsertAction computeInsertAction(ArchiveOperation Operation,
479                                        const object::Archive::Child &Member,
480                                        StringRef Name,
481                                        std::vector<StringRef>::iterator &Pos) {
482  if (Operation == QuickAppend || Members.empty())
483    return IA_AddOldMember;
484
485  auto MI =
486      std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
487        return Name == sys::path::filename(Path);
488      });
489
490  if (MI == Members.end())
491    return IA_AddOldMember;
492
493  Pos = MI;
494
495  if (Operation == Delete)
496    return IA_Delete;
497
498  if (Operation == Move)
499    return IA_MoveOldMember;
500
501  if (Operation == ReplaceOrInsert) {
502    StringRef PosName = sys::path::filename(RelPos);
503    if (!OnlyUpdate) {
504      if (PosName.empty())
505        return IA_AddNewMeber;
506      return IA_MoveNewMember;
507    }
508
509    // We could try to optimize this to a fstat, but it is not a common
510    // operation.
511    sys::fs::file_status Status;
512    failIfError(sys::fs::status(*MI, Status), *MI);
513    if (Status.getLastModificationTime() < Member.getLastModified()) {
514      if (PosName.empty())
515        return IA_AddOldMember;
516      return IA_MoveOldMember;
517    }
518
519    if (PosName.empty())
520      return IA_AddNewMeber;
521    return IA_MoveNewMember;
522  }
523  llvm_unreachable("No such operation");
524}
525
526// We have to walk this twice and computing it is not trivial, so creating an
527// explicit std::vector is actually fairly efficient.
528static std::vector<NewArchiveMember>
529computeNewArchiveMembers(ArchiveOperation Operation,
530                         object::Archive *OldArchive) {
531  std::vector<NewArchiveMember> Ret;
532  std::vector<NewArchiveMember> Moved;
533  int InsertPos = -1;
534  StringRef PosName = sys::path::filename(RelPos);
535  if (OldArchive) {
536    Error Err;
537    for (auto &Child : OldArchive->children(Err)) {
538      int Pos = Ret.size();
539      ErrorOr<StringRef> NameOrErr = Child.getName();
540      failIfError(NameOrErr.getError());
541      StringRef Name = NameOrErr.get();
542      if (Name == PosName) {
543        assert(AddAfter || AddBefore);
544        if (AddBefore)
545          InsertPos = Pos;
546        else
547          InsertPos = Pos + 1;
548      }
549
550      std::vector<StringRef>::iterator MemberI = Members.end();
551      InsertAction Action =
552          computeInsertAction(Operation, Child, Name, MemberI);
553      switch (Action) {
554      case IA_AddOldMember:
555        addMember(Ret, Child);
556        break;
557      case IA_AddNewMeber:
558        addMember(Ret, *MemberI);
559        break;
560      case IA_Delete:
561        break;
562      case IA_MoveOldMember:
563        addMember(Moved, Child);
564        break;
565      case IA_MoveNewMember:
566        addMember(Moved, *MemberI);
567        break;
568      }
569      if (MemberI != Members.end())
570        Members.erase(MemberI);
571    }
572    failIfError(std::move(Err));
573  }
574
575  if (Operation == Delete)
576    return Ret;
577
578  if (!RelPos.empty() && InsertPos == -1)
579    fail("Insertion point not found");
580
581  if (RelPos.empty())
582    InsertPos = Ret.size();
583
584  assert(unsigned(InsertPos) <= Ret.size());
585  int Pos = InsertPos;
586  for (auto &M : Moved) {
587    Ret.insert(Ret.begin() + Pos, std::move(M));
588    ++Pos;
589  }
590
591  for (unsigned I = 0; I != Members.size(); ++I)
592    Ret.insert(Ret.begin() + InsertPos, NewArchiveMember());
593  Pos = InsertPos;
594  for (auto &Member : Members) {
595    addMember(Ret, Member, Pos);
596    ++Pos;
597  }
598
599  return Ret;
600}
601
602static object::Archive::Kind getDefaultForHost() {
603  return Triple(sys::getProcessTriple()).isOSDarwin() ? object::Archive::K_BSD
604                                                      : object::Archive::K_GNU;
605}
606
607static object::Archive::Kind getKindFromMember(const NewArchiveMember &Member) {
608  Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
609      object::ObjectFile::createObjectFile(Member.Buf->getMemBufferRef());
610
611  if (OptionalObject)
612    return isa<object::MachOObjectFile>(**OptionalObject)
613               ? object::Archive::K_BSD
614               : object::Archive::K_GNU;
615
616  // squelch the error in case we had a non-object file
617  consumeError(OptionalObject.takeError());
618  return getDefaultForHost();
619}
620
621static void
622performWriteOperation(ArchiveOperation Operation,
623                      object::Archive *OldArchive,
624                      std::unique_ptr<MemoryBuffer> OldArchiveBuf,
625                      std::vector<NewArchiveMember> *NewMembersP) {
626  std::vector<NewArchiveMember> NewMembers;
627  if (!NewMembersP)
628    NewMembers = computeNewArchiveMembers(Operation, OldArchive);
629
630  object::Archive::Kind Kind;
631  switch (FormatOpt) {
632  case Default:
633    if (Thin)
634      Kind = object::Archive::K_GNU;
635    else if (OldArchive)
636      Kind = OldArchive->kind();
637    else if (NewMembersP)
638      Kind = NewMembersP->size() ? getKindFromMember(NewMembersP->front())
639                                 : getDefaultForHost();
640    else
641      Kind = NewMembers.size() ? getKindFromMember(NewMembers.front())
642                               : getDefaultForHost();
643    break;
644  case GNU:
645    Kind = object::Archive::K_GNU;
646    break;
647  case BSD:
648    if (Thin)
649      fail("Only the gnu format has a thin mode");
650    Kind = object::Archive::K_BSD;
651    break;
652  }
653
654  std::pair<StringRef, std::error_code> Result =
655      writeArchive(ArchiveName, NewMembersP ? *NewMembersP : NewMembers, Symtab,
656                   Kind, Deterministic, Thin, std::move(OldArchiveBuf));
657  failIfError(Result.second, Result.first);
658}
659
660static void createSymbolTable(object::Archive *OldArchive) {
661  // When an archive is created or modified, if the s option is given, the
662  // resulting archive will have a current symbol table. If the S option
663  // is given, it will have no symbol table.
664  // In summary, we only need to update the symbol table if we have none.
665  // This is actually very common because of broken build systems that think
666  // they have to run ranlib.
667  if (OldArchive->hasSymbolTable())
668    return;
669
670  performWriteOperation(CreateSymTab, OldArchive, nullptr, nullptr);
671}
672
673static void performOperation(ArchiveOperation Operation,
674                             object::Archive *OldArchive,
675                             std::unique_ptr<MemoryBuffer> OldArchiveBuf,
676                             std::vector<NewArchiveMember> *NewMembers) {
677  switch (Operation) {
678  case Print:
679  case DisplayTable:
680  case Extract:
681    performReadOperation(Operation, OldArchive);
682    return;
683
684  case Delete:
685  case Move:
686  case QuickAppend:
687  case ReplaceOrInsert:
688    performWriteOperation(Operation, OldArchive, std::move(OldArchiveBuf),
689                          NewMembers);
690    return;
691  case CreateSymTab:
692    createSymbolTable(OldArchive);
693    return;
694  }
695  llvm_unreachable("Unknown operation.");
696}
697
698static int performOperation(ArchiveOperation Operation,
699                            std::vector<NewArchiveMember> *NewMembers) {
700  // Create or open the archive object.
701  ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
702      MemoryBuffer::getFile(ArchiveName, -1, false);
703  std::error_code EC = Buf.getError();
704  if (EC && EC != errc::no_such_file_or_directory)
705    fail("error opening '" + ArchiveName + "': " + EC.message() + "!");
706
707  if (!EC) {
708    Error Err;
709    object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
710    EC = errorToErrorCode(std::move(Err));
711    failIfError(EC,
712                "error loading '" + ArchiveName + "': " + EC.message() + "!");
713    performOperation(Operation, &Archive, std::move(Buf.get()), NewMembers);
714    return 0;
715  }
716
717  assert(EC == errc::no_such_file_or_directory);
718
719  if (!shouldCreateArchive(Operation)) {
720    failIfError(EC, Twine("error loading '") + ArchiveName + "'");
721  } else {
722    if (!Create) {
723      // Produce a warning if we should and we're creating the archive
724      errs() << ToolName << ": creating " << ArchiveName << "\n";
725    }
726  }
727
728  performOperation(Operation, nullptr, nullptr, NewMembers);
729  return 0;
730}
731
732static void runMRIScript() {
733  enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
734
735  ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
736  failIfError(Buf.getError());
737  const MemoryBuffer &Ref = *Buf.get();
738  bool Saved = false;
739  std::vector<NewArchiveMember> NewMembers;
740  std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
741  std::vector<std::unique_ptr<object::Archive>> Archives;
742
743  for (line_iterator I(Ref, /*SkipBlanks*/ true, ';'), E; I != E; ++I) {
744    StringRef Line = *I;
745    StringRef CommandStr, Rest;
746    std::tie(CommandStr, Rest) = Line.split(' ');
747    Rest = Rest.trim();
748    if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
749      Rest = Rest.drop_front().drop_back();
750    auto Command = StringSwitch<MRICommand>(CommandStr.lower())
751                       .Case("addlib", MRICommand::AddLib)
752                       .Case("addmod", MRICommand::AddMod)
753                       .Case("create", MRICommand::Create)
754                       .Case("save", MRICommand::Save)
755                       .Case("end", MRICommand::End)
756                       .Default(MRICommand::Invalid);
757
758    switch (Command) {
759    case MRICommand::AddLib: {
760      auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false);
761      failIfError(BufOrErr.getError(), "Could not open library");
762      ArchiveBuffers.push_back(std::move(*BufOrErr));
763      auto LibOrErr =
764          object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
765      failIfError(errorToErrorCode(LibOrErr.takeError()),
766                  "Could not parse library");
767      Archives.push_back(std::move(*LibOrErr));
768      object::Archive &Lib = *Archives.back();
769      {
770        Error Err;
771        for (auto &Member : Lib.children(Err))
772          addMember(NewMembers, Member);
773        failIfError(std::move(Err));
774      }
775      break;
776    }
777    case MRICommand::AddMod:
778      addMember(NewMembers, Rest);
779      break;
780    case MRICommand::Create:
781      Create = true;
782      if (!ArchiveName.empty())
783        fail("Editing multiple archives not supported");
784      if (Saved)
785        fail("File already saved");
786      ArchiveName = Rest;
787      break;
788    case MRICommand::Save:
789      Saved = true;
790      break;
791    case MRICommand::End:
792      break;
793    case MRICommand::Invalid:
794      fail("Unknown command: " + CommandStr);
795    }
796  }
797
798  // Nothing to do if not saved.
799  if (Saved)
800    performOperation(ReplaceOrInsert, &NewMembers);
801  exit(0);
802}
803
804static int ar_main() {
805  // Do our own parsing of the command line because the CommandLine utility
806  // can't handle the grouped positional parameters without a dash.
807  ArchiveOperation Operation = parseCommandLine();
808  return performOperation(Operation, nullptr);
809}
810
811static int ranlib_main() {
812  if (RestOfArgs.size() != 1)
813    fail(ToolName + " takes just one archive as an argument");
814  ArchiveName = RestOfArgs[0];
815  return performOperation(CreateSymTab, nullptr);
816}
817
818int main(int argc, char **argv) {
819  ToolName = argv[0];
820  // Print a stack trace if we signal out.
821  sys::PrintStackTraceOnErrorSignal(argv[0]);
822  PrettyStackTraceProgram X(argc, argv);
823  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
824
825  llvm::InitializeAllTargetInfos();
826  llvm::InitializeAllTargetMCs();
827  llvm::InitializeAllAsmParsers();
828
829  StringRef Stem = sys::path::stem(ToolName);
830  if (Stem.find("ranlib") == StringRef::npos &&
831      Stem.find("lib") != StringRef::npos)
832    return libDriverMain(makeArrayRef(argv, argc));
833
834  // Have the command line options parsed and handle things
835  // like --help and --version.
836  cl::ParseCommandLineOptions(argc, argv,
837    "LLVM Archiver (llvm-ar)\n\n"
838    "  This program archives bitcode files into single libraries\n"
839  );
840
841  if (Stem.find("ranlib") != StringRef::npos)
842    return ranlib_main();
843  if (Stem.find("ar") != StringRef::npos)
844    return ar_main();
845  fail("Not ranlib, ar or lib!");
846}
847