1f4a2713aSLionel Sambuc //===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // Builds up (relatively) standard unix archive files (.a) containing LLVM
11f4a2713aSLionel Sambuc // bitcode or other files.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
16f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
17f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
18f4a2713aSLionel Sambuc #include "llvm/Object/Archive.h"
19f4a2713aSLionel Sambuc #include "llvm/Object/ObjectFile.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/Errc.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/Format.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/Support/LineIterator.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/ManagedStatic.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/PrettyStackTrace.h"
28f4a2713aSLionel Sambuc #include "llvm/Support/Signals.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/Support/TargetSelect.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/ToolOutputFile.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
32f4a2713aSLionel Sambuc #include <algorithm>
33f4a2713aSLionel Sambuc #include <cstdlib>
34f4a2713aSLionel Sambuc #include <memory>
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc #if !defined(_MSC_VER) && !defined(__MINGW32__)
37f4a2713aSLionel Sambuc #include <unistd.h>
38f4a2713aSLionel Sambuc #else
39f4a2713aSLionel Sambuc #include <io.h>
40f4a2713aSLionel Sambuc #endif
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc using namespace llvm;
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc // The name this program was invoked as.
45f4a2713aSLionel Sambuc static StringRef ToolName;
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc static const char *TemporaryOutput;
48f4a2713aSLionel Sambuc static int TmpArchiveFD = -1;
49f4a2713aSLionel Sambuc
50*0a6a1f1dSLionel Sambuc // Show the error message and exit.
fail(Twine Error)51f4a2713aSLionel Sambuc LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
52f4a2713aSLionel Sambuc outs() << ToolName << ": " << Error << ".\n";
53f4a2713aSLionel Sambuc if (TmpArchiveFD != -1)
54f4a2713aSLionel Sambuc close(TmpArchiveFD);
55f4a2713aSLionel Sambuc if (TemporaryOutput)
56f4a2713aSLionel Sambuc sys::fs::remove(TemporaryOutput);
57f4a2713aSLionel Sambuc exit(1);
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc
failIfError(std::error_code EC,Twine Context="")60*0a6a1f1dSLionel Sambuc static void failIfError(std::error_code EC, Twine Context = "") {
61f4a2713aSLionel Sambuc if (!EC)
62f4a2713aSLionel Sambuc return;
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc std::string ContextStr = Context.str();
65f4a2713aSLionel Sambuc if (ContextStr == "")
66f4a2713aSLionel Sambuc fail(EC.message());
67f4a2713aSLionel Sambuc fail(Context + ": " + EC.message());
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc // llvm-ar/llvm-ranlib remaining positional arguments.
71f4a2713aSLionel Sambuc static cl::list<std::string>
72*0a6a1f1dSLionel Sambuc RestOfArgs(cl::Positional, cl::ZeroOrMore,
73f4a2713aSLionel Sambuc cl::desc("[relpos] [count] <archive-file> [members]..."));
74f4a2713aSLionel Sambuc
75*0a6a1f1dSLionel Sambuc static cl::opt<bool> MRI("M", cl::desc(""));
76*0a6a1f1dSLionel Sambuc
77f4a2713aSLionel Sambuc std::string Options;
78f4a2713aSLionel Sambuc
79*0a6a1f1dSLionel Sambuc // Provide additional help output explaining the operations and modifiers of
80*0a6a1f1dSLionel Sambuc // llvm-ar. This object instructs the CommandLine library to print the text of
81*0a6a1f1dSLionel Sambuc // the constructor when the --help option is given.
82f4a2713aSLionel Sambuc static cl::extrahelp MoreHelp(
83f4a2713aSLionel Sambuc "\nOPERATIONS:\n"
84f4a2713aSLionel Sambuc " d[NsS] - delete file(s) from the archive\n"
85f4a2713aSLionel Sambuc " m[abiSs] - move file(s) in the archive\n"
86f4a2713aSLionel Sambuc " p[kN] - print file(s) found in the archive\n"
87f4a2713aSLionel Sambuc " q[ufsS] - quick append file(s) to the archive\n"
88f4a2713aSLionel Sambuc " r[abfiuRsS] - replace or insert file(s) into the archive\n"
89f4a2713aSLionel Sambuc " t - display contents of archive\n"
90f4a2713aSLionel Sambuc " x[No] - extract file(s) from the archive\n"
91f4a2713aSLionel Sambuc "\nMODIFIERS (operation specific):\n"
92f4a2713aSLionel Sambuc " [a] - put file(s) after [relpos]\n"
93f4a2713aSLionel Sambuc " [b] - put file(s) before [relpos] (same as [i])\n"
94f4a2713aSLionel Sambuc " [i] - put file(s) before [relpos] (same as [b])\n"
95f4a2713aSLionel Sambuc " [N] - use instance [count] of name\n"
96f4a2713aSLionel Sambuc " [o] - preserve original dates\n"
97f4a2713aSLionel Sambuc " [s] - create an archive index (cf. ranlib)\n"
98f4a2713aSLionel Sambuc " [S] - do not build a symbol table\n"
99f4a2713aSLionel Sambuc " [u] - update only files newer than archive contents\n"
100f4a2713aSLionel Sambuc "\nMODIFIERS (generic):\n"
101f4a2713aSLionel Sambuc " [c] - do not warn if the library had to be created\n"
102f4a2713aSLionel Sambuc " [v] - be verbose about actions taken\n"
103f4a2713aSLionel Sambuc );
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc // This enumeration delineates the kinds of operations on an archive
106f4a2713aSLionel Sambuc // that are permitted.
107f4a2713aSLionel Sambuc enum ArchiveOperation {
108f4a2713aSLionel Sambuc Print, ///< Print the contents of the archive
109f4a2713aSLionel Sambuc Delete, ///< Delete the specified members
110f4a2713aSLionel Sambuc Move, ///< Move members to end or as given by {a,b,i} modifiers
111f4a2713aSLionel Sambuc QuickAppend, ///< Quickly append to end of archive
112f4a2713aSLionel Sambuc ReplaceOrInsert, ///< Replace or Insert members
113f4a2713aSLionel Sambuc DisplayTable, ///< Display the table of contents
114f4a2713aSLionel Sambuc Extract, ///< Extract files back to file system
115f4a2713aSLionel Sambuc CreateSymTab ///< Create a symbol table in an existing archive
116f4a2713aSLionel Sambuc };
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc // Modifiers to follow operation to vary behavior
119f4a2713aSLionel Sambuc static bool AddAfter = false; ///< 'a' modifier
120f4a2713aSLionel Sambuc static bool AddBefore = false; ///< 'b' modifier
121f4a2713aSLionel Sambuc static bool Create = false; ///< 'c' modifier
122f4a2713aSLionel Sambuc static bool OriginalDates = false; ///< 'o' modifier
123f4a2713aSLionel Sambuc static bool OnlyUpdate = false; ///< 'u' modifier
124f4a2713aSLionel Sambuc static bool Verbose = false; ///< 'v' modifier
125f4a2713aSLionel Sambuc static bool Symtab = true; ///< 's' modifier
126f4a2713aSLionel Sambuc
127f4a2713aSLionel Sambuc // Relative Positional Argument (for insert/move). This variable holds
128f4a2713aSLionel Sambuc // the name of the archive member to which the 'a', 'b' or 'i' modifier
129f4a2713aSLionel Sambuc // refers. Only one of 'a', 'b' or 'i' can be specified so we only need
130f4a2713aSLionel Sambuc // one variable.
131f4a2713aSLionel Sambuc static std::string RelPos;
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc // This variable holds the name of the archive file as given on the
134f4a2713aSLionel Sambuc // command line.
135f4a2713aSLionel Sambuc static std::string ArchiveName;
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc // This variable holds the list of member files to proecess, as given
138f4a2713aSLionel Sambuc // on the command line.
139*0a6a1f1dSLionel Sambuc static std::vector<StringRef> Members;
140f4a2713aSLionel Sambuc
141*0a6a1f1dSLionel Sambuc // Show the error message, the help message and exit.
142f4a2713aSLionel Sambuc LLVM_ATTRIBUTE_NORETURN static void
show_help(const std::string & msg)143f4a2713aSLionel Sambuc show_help(const std::string &msg) {
144f4a2713aSLionel Sambuc errs() << ToolName << ": " << msg << "\n\n";
145f4a2713aSLionel Sambuc cl::PrintHelpMessage();
146f4a2713aSLionel Sambuc std::exit(1);
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
149*0a6a1f1dSLionel Sambuc // Extract the member filename from the command line for the [relpos] argument
150*0a6a1f1dSLionel Sambuc // associated with a, b, and i modifiers
getRelPos()151f4a2713aSLionel Sambuc static void getRelPos() {
152f4a2713aSLionel Sambuc if(RestOfArgs.size() == 0)
153f4a2713aSLionel Sambuc show_help("Expected [relpos] for a, b, or i modifier");
154f4a2713aSLionel Sambuc RelPos = RestOfArgs[0];
155f4a2713aSLionel Sambuc RestOfArgs.erase(RestOfArgs.begin());
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
getOptions()158f4a2713aSLionel Sambuc static void getOptions() {
159f4a2713aSLionel Sambuc if(RestOfArgs.size() == 0)
160f4a2713aSLionel Sambuc show_help("Expected options");
161f4a2713aSLionel Sambuc Options = RestOfArgs[0];
162f4a2713aSLionel Sambuc RestOfArgs.erase(RestOfArgs.begin());
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc
165*0a6a1f1dSLionel Sambuc // Get the archive file name from the command line
getArchive()166f4a2713aSLionel Sambuc static void getArchive() {
167f4a2713aSLionel Sambuc if(RestOfArgs.size() == 0)
168f4a2713aSLionel Sambuc show_help("An archive name must be specified");
169f4a2713aSLionel Sambuc ArchiveName = RestOfArgs[0];
170f4a2713aSLionel Sambuc RestOfArgs.erase(RestOfArgs.begin());
171f4a2713aSLionel Sambuc }
172f4a2713aSLionel Sambuc
173*0a6a1f1dSLionel Sambuc // Copy over remaining items in RestOfArgs to our Members vector
getMembers()174f4a2713aSLionel Sambuc static void getMembers() {
175*0a6a1f1dSLionel Sambuc for (auto &Arg : RestOfArgs)
176*0a6a1f1dSLionel Sambuc Members.push_back(Arg);
177f4a2713aSLionel Sambuc }
178f4a2713aSLionel Sambuc
179*0a6a1f1dSLionel Sambuc static void runMRIScript();
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc // Parse the command line options as presented and return the operation
182*0a6a1f1dSLionel Sambuc // specified. Process all modifiers and check to make sure that constraints on
183*0a6a1f1dSLionel Sambuc // modifier/operation pairs have not been violated.
parseCommandLine()184f4a2713aSLionel Sambuc static ArchiveOperation parseCommandLine() {
185*0a6a1f1dSLionel Sambuc if (MRI) {
186*0a6a1f1dSLionel Sambuc if (!RestOfArgs.empty())
187*0a6a1f1dSLionel Sambuc fail("Cannot mix -M and other options");
188*0a6a1f1dSLionel Sambuc runMRIScript();
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc
191f4a2713aSLionel Sambuc getOptions();
192f4a2713aSLionel Sambuc
193f4a2713aSLionel Sambuc // Keep track of number of operations. We can only specify one
194f4a2713aSLionel Sambuc // per execution.
195f4a2713aSLionel Sambuc unsigned NumOperations = 0;
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // Keep track of the number of positional modifiers (a,b,i). Only
198f4a2713aSLionel Sambuc // one can be specified.
199f4a2713aSLionel Sambuc unsigned NumPositional = 0;
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc // Keep track of which operation was requested
202f4a2713aSLionel Sambuc ArchiveOperation Operation;
203f4a2713aSLionel Sambuc
204f4a2713aSLionel Sambuc bool MaybeJustCreateSymTab = false;
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc for(unsigned i=0; i<Options.size(); ++i) {
207f4a2713aSLionel Sambuc switch(Options[i]) {
208f4a2713aSLionel Sambuc case 'd': ++NumOperations; Operation = Delete; break;
209f4a2713aSLionel Sambuc case 'm': ++NumOperations; Operation = Move ; break;
210f4a2713aSLionel Sambuc case 'p': ++NumOperations; Operation = Print; break;
211f4a2713aSLionel Sambuc case 'q': ++NumOperations; Operation = QuickAppend; break;
212f4a2713aSLionel Sambuc case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
213f4a2713aSLionel Sambuc case 't': ++NumOperations; Operation = DisplayTable; break;
214f4a2713aSLionel Sambuc case 'x': ++NumOperations; Operation = Extract; break;
215f4a2713aSLionel Sambuc case 'c': Create = true; break;
216f4a2713aSLionel Sambuc case 'l': /* accepted but unused */ break;
217f4a2713aSLionel Sambuc case 'o': OriginalDates = true; break;
218f4a2713aSLionel Sambuc case 's':
219f4a2713aSLionel Sambuc Symtab = true;
220f4a2713aSLionel Sambuc MaybeJustCreateSymTab = true;
221f4a2713aSLionel Sambuc break;
222f4a2713aSLionel Sambuc case 'S':
223f4a2713aSLionel Sambuc Symtab = false;
224f4a2713aSLionel Sambuc break;
225f4a2713aSLionel Sambuc case 'u': OnlyUpdate = true; break;
226f4a2713aSLionel Sambuc case 'v': Verbose = true; break;
227f4a2713aSLionel Sambuc case 'a':
228f4a2713aSLionel Sambuc getRelPos();
229f4a2713aSLionel Sambuc AddAfter = true;
230f4a2713aSLionel Sambuc NumPositional++;
231f4a2713aSLionel Sambuc break;
232f4a2713aSLionel Sambuc case 'b':
233f4a2713aSLionel Sambuc getRelPos();
234f4a2713aSLionel Sambuc AddBefore = true;
235f4a2713aSLionel Sambuc NumPositional++;
236f4a2713aSLionel Sambuc break;
237f4a2713aSLionel Sambuc case 'i':
238f4a2713aSLionel Sambuc getRelPos();
239f4a2713aSLionel Sambuc AddBefore = true;
240f4a2713aSLionel Sambuc NumPositional++;
241f4a2713aSLionel Sambuc break;
242f4a2713aSLionel Sambuc default:
243f4a2713aSLionel Sambuc cl::PrintHelpMessage();
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc }
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc // At this point, the next thing on the command line must be
248f4a2713aSLionel Sambuc // the archive name.
249f4a2713aSLionel Sambuc getArchive();
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc // Everything on the command line at this point is a member.
252f4a2713aSLionel Sambuc getMembers();
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc if (NumOperations == 0 && MaybeJustCreateSymTab) {
255f4a2713aSLionel Sambuc NumOperations = 1;
256f4a2713aSLionel Sambuc Operation = CreateSymTab;
257f4a2713aSLionel Sambuc if (!Members.empty())
258f4a2713aSLionel Sambuc show_help("The s operation takes only an archive as argument");
259f4a2713aSLionel Sambuc }
260f4a2713aSLionel Sambuc
261f4a2713aSLionel Sambuc // Perform various checks on the operation/modifier specification
262f4a2713aSLionel Sambuc // to make sure we are dealing with a legal request.
263f4a2713aSLionel Sambuc if (NumOperations == 0)
264f4a2713aSLionel Sambuc show_help("You must specify at least one of the operations");
265f4a2713aSLionel Sambuc if (NumOperations > 1)
266f4a2713aSLionel Sambuc show_help("Only one operation may be specified");
267f4a2713aSLionel Sambuc if (NumPositional > 1)
268f4a2713aSLionel Sambuc show_help("You may only specify one of a, b, and i modifiers");
269f4a2713aSLionel Sambuc if (AddAfter || AddBefore) {
270f4a2713aSLionel Sambuc if (Operation != Move && Operation != ReplaceOrInsert)
271f4a2713aSLionel Sambuc show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
272f4a2713aSLionel Sambuc "the 'm' or 'r' operations");
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc if (OriginalDates && Operation != Extract)
275f4a2713aSLionel Sambuc show_help("The 'o' modifier is only applicable to the 'x' operation");
276f4a2713aSLionel Sambuc if (OnlyUpdate && Operation != ReplaceOrInsert)
277f4a2713aSLionel Sambuc show_help("The 'u' modifier is only applicable to the 'r' operation");
278f4a2713aSLionel Sambuc
279f4a2713aSLionel Sambuc // Return the parsed operation to the caller
280f4a2713aSLionel Sambuc return Operation;
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc
283f4a2713aSLionel Sambuc // Implements the 'p' operation. This function traverses the archive
284f4a2713aSLionel Sambuc // looking for members that match the path list.
doPrint(StringRef Name,object::Archive::child_iterator I)285f4a2713aSLionel Sambuc static void doPrint(StringRef Name, object::Archive::child_iterator I) {
286f4a2713aSLionel Sambuc if (Verbose)
287f4a2713aSLionel Sambuc outs() << "Printing " << Name << "\n";
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc StringRef Data = I->getBuffer();
290f4a2713aSLionel Sambuc outs().write(Data.data(), Data.size());
291f4a2713aSLionel Sambuc }
292f4a2713aSLionel Sambuc
293*0a6a1f1dSLionel Sambuc // Utility function for printing out the file mode when the 't' operation is in
294*0a6a1f1dSLionel Sambuc // verbose mode.
printMode(unsigned mode)295f4a2713aSLionel Sambuc static void printMode(unsigned mode) {
296f4a2713aSLionel Sambuc if (mode & 004)
297f4a2713aSLionel Sambuc outs() << "r";
298f4a2713aSLionel Sambuc else
299f4a2713aSLionel Sambuc outs() << "-";
300f4a2713aSLionel Sambuc if (mode & 002)
301f4a2713aSLionel Sambuc outs() << "w";
302f4a2713aSLionel Sambuc else
303f4a2713aSLionel Sambuc outs() << "-";
304f4a2713aSLionel Sambuc if (mode & 001)
305f4a2713aSLionel Sambuc outs() << "x";
306f4a2713aSLionel Sambuc else
307f4a2713aSLionel Sambuc outs() << "-";
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc // Implement the 't' operation. This function prints out just
311f4a2713aSLionel Sambuc // the file names of each of the members. However, if verbose mode is requested
312f4a2713aSLionel Sambuc // ('v' modifier) then the file type, permission mode, user, group, size, and
313f4a2713aSLionel Sambuc // modification time are also printed.
doDisplayTable(StringRef Name,object::Archive::child_iterator I)314f4a2713aSLionel Sambuc static void doDisplayTable(StringRef Name, object::Archive::child_iterator I) {
315f4a2713aSLionel Sambuc if (Verbose) {
316f4a2713aSLionel Sambuc sys::fs::perms Mode = I->getAccessMode();
317f4a2713aSLionel Sambuc printMode((Mode >> 6) & 007);
318f4a2713aSLionel Sambuc printMode((Mode >> 3) & 007);
319f4a2713aSLionel Sambuc printMode(Mode & 007);
320f4a2713aSLionel Sambuc outs() << ' ' << I->getUID();
321f4a2713aSLionel Sambuc outs() << '/' << I->getGID();
322f4a2713aSLionel Sambuc outs() << ' ' << format("%6llu", I->getSize());
323f4a2713aSLionel Sambuc outs() << ' ' << I->getLastModified().str();
324f4a2713aSLionel Sambuc outs() << ' ';
325f4a2713aSLionel Sambuc }
326f4a2713aSLionel Sambuc outs() << Name << "\n";
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc
329f4a2713aSLionel Sambuc // Implement the 'x' operation. This function extracts files back to the file
330f4a2713aSLionel Sambuc // system.
doExtract(StringRef Name,object::Archive::child_iterator I)331f4a2713aSLionel Sambuc static void doExtract(StringRef Name, object::Archive::child_iterator I) {
332f4a2713aSLionel Sambuc // Retain the original mode.
333f4a2713aSLionel Sambuc sys::fs::perms Mode = I->getAccessMode();
334f4a2713aSLionel Sambuc SmallString<128> Storage = Name;
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc int FD;
337f4a2713aSLionel Sambuc failIfError(
338*0a6a1f1dSLionel Sambuc sys::fs::openFileForWrite(Storage.c_str(), FD, sys::fs::F_None, Mode),
339f4a2713aSLionel Sambuc Storage.c_str());
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc {
342f4a2713aSLionel Sambuc raw_fd_ostream file(FD, false);
343f4a2713aSLionel Sambuc
344f4a2713aSLionel Sambuc // Get the data and its length
345f4a2713aSLionel Sambuc StringRef Data = I->getBuffer();
346f4a2713aSLionel Sambuc
347f4a2713aSLionel Sambuc // Write the data.
348f4a2713aSLionel Sambuc file.write(Data.data(), Data.size());
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc // If we're supposed to retain the original modification times, etc. do so
352f4a2713aSLionel Sambuc // now.
353f4a2713aSLionel Sambuc if (OriginalDates)
354f4a2713aSLionel Sambuc failIfError(
355f4a2713aSLionel Sambuc sys::fs::setLastModificationAndAccessTime(FD, I->getLastModified()));
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc if (close(FD))
358f4a2713aSLionel Sambuc fail("Could not close the file");
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
shouldCreateArchive(ArchiveOperation Op)361f4a2713aSLionel Sambuc static bool shouldCreateArchive(ArchiveOperation Op) {
362f4a2713aSLionel Sambuc switch (Op) {
363f4a2713aSLionel Sambuc case Print:
364f4a2713aSLionel Sambuc case Delete:
365f4a2713aSLionel Sambuc case Move:
366f4a2713aSLionel Sambuc case DisplayTable:
367f4a2713aSLionel Sambuc case Extract:
368f4a2713aSLionel Sambuc case CreateSymTab:
369f4a2713aSLionel Sambuc return false;
370f4a2713aSLionel Sambuc
371f4a2713aSLionel Sambuc case QuickAppend:
372f4a2713aSLionel Sambuc case ReplaceOrInsert:
373f4a2713aSLionel Sambuc return true;
374f4a2713aSLionel Sambuc }
375f4a2713aSLionel Sambuc
376f4a2713aSLionel Sambuc llvm_unreachable("Missing entry in covered switch.");
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc
performReadOperation(ArchiveOperation Operation,object::Archive * OldArchive)379f4a2713aSLionel Sambuc static void performReadOperation(ArchiveOperation Operation,
380f4a2713aSLionel Sambuc object::Archive *OldArchive) {
381*0a6a1f1dSLionel Sambuc for (object::Archive::child_iterator I = OldArchive->child_begin(),
382*0a6a1f1dSLionel Sambuc E = OldArchive->child_end();
383f4a2713aSLionel Sambuc I != E; ++I) {
384*0a6a1f1dSLionel Sambuc ErrorOr<StringRef> NameOrErr = I->getName();
385*0a6a1f1dSLionel Sambuc failIfError(NameOrErr.getError());
386*0a6a1f1dSLionel Sambuc StringRef Name = NameOrErr.get();
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc if (!Members.empty() &&
389f4a2713aSLionel Sambuc std::find(Members.begin(), Members.end(), Name) == Members.end())
390f4a2713aSLionel Sambuc continue;
391f4a2713aSLionel Sambuc
392f4a2713aSLionel Sambuc switch (Operation) {
393f4a2713aSLionel Sambuc default:
394f4a2713aSLionel Sambuc llvm_unreachable("Not a read operation");
395f4a2713aSLionel Sambuc case Print:
396f4a2713aSLionel Sambuc doPrint(Name, I);
397f4a2713aSLionel Sambuc break;
398f4a2713aSLionel Sambuc case DisplayTable:
399f4a2713aSLionel Sambuc doDisplayTable(Name, I);
400f4a2713aSLionel Sambuc break;
401f4a2713aSLionel Sambuc case Extract:
402f4a2713aSLionel Sambuc doExtract(Name, I);
403f4a2713aSLionel Sambuc break;
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc }
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc
408f4a2713aSLionel Sambuc namespace {
409f4a2713aSLionel Sambuc class NewArchiveIterator {
410f4a2713aSLionel Sambuc bool IsNewMember;
411f4a2713aSLionel Sambuc StringRef Name;
412*0a6a1f1dSLionel Sambuc
413f4a2713aSLionel Sambuc object::Archive::child_iterator OldI;
414*0a6a1f1dSLionel Sambuc
415*0a6a1f1dSLionel Sambuc StringRef NewFilename;
416f4a2713aSLionel Sambuc
417f4a2713aSLionel Sambuc public:
418f4a2713aSLionel Sambuc NewArchiveIterator(object::Archive::child_iterator I, StringRef Name);
419*0a6a1f1dSLionel Sambuc NewArchiveIterator(StringRef I, StringRef Name);
420f4a2713aSLionel Sambuc NewArchiveIterator();
421f4a2713aSLionel Sambuc bool isNewMember() const;
422f4a2713aSLionel Sambuc StringRef getName() const;
423*0a6a1f1dSLionel Sambuc
424*0a6a1f1dSLionel Sambuc object::Archive::child_iterator getOld() const;
425*0a6a1f1dSLionel Sambuc
426*0a6a1f1dSLionel Sambuc StringRef getNew() const;
427*0a6a1f1dSLionel Sambuc int getFD(sys::fs::file_status &NewStatus) const;
428*0a6a1f1dSLionel Sambuc const sys::fs::file_status &getStatus() const;
429f4a2713aSLionel Sambuc };
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc
NewArchiveIterator()432f4a2713aSLionel Sambuc NewArchiveIterator::NewArchiveIterator() {}
433f4a2713aSLionel Sambuc
NewArchiveIterator(object::Archive::child_iterator I,StringRef Name)434f4a2713aSLionel Sambuc NewArchiveIterator::NewArchiveIterator(object::Archive::child_iterator I,
435f4a2713aSLionel Sambuc StringRef Name)
436f4a2713aSLionel Sambuc : IsNewMember(false), Name(Name), OldI(I) {}
437f4a2713aSLionel Sambuc
NewArchiveIterator(StringRef NewFilename,StringRef Name)438*0a6a1f1dSLionel Sambuc NewArchiveIterator::NewArchiveIterator(StringRef NewFilename, StringRef Name)
439*0a6a1f1dSLionel Sambuc : IsNewMember(true), Name(Name), NewFilename(NewFilename) {}
440f4a2713aSLionel Sambuc
getName() const441f4a2713aSLionel Sambuc StringRef NewArchiveIterator::getName() const { return Name; }
442f4a2713aSLionel Sambuc
isNewMember() const443f4a2713aSLionel Sambuc bool NewArchiveIterator::isNewMember() const { return IsNewMember; }
444f4a2713aSLionel Sambuc
getOld() const445f4a2713aSLionel Sambuc object::Archive::child_iterator NewArchiveIterator::getOld() const {
446f4a2713aSLionel Sambuc assert(!IsNewMember);
447f4a2713aSLionel Sambuc return OldI;
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc
getNew() const450*0a6a1f1dSLionel Sambuc StringRef NewArchiveIterator::getNew() const {
451f4a2713aSLionel Sambuc assert(IsNewMember);
452*0a6a1f1dSLionel Sambuc return NewFilename;
453*0a6a1f1dSLionel Sambuc }
454*0a6a1f1dSLionel Sambuc
getFD(sys::fs::file_status & NewStatus) const455*0a6a1f1dSLionel Sambuc int NewArchiveIterator::getFD(sys::fs::file_status &NewStatus) const {
456*0a6a1f1dSLionel Sambuc assert(IsNewMember);
457*0a6a1f1dSLionel Sambuc int NewFD;
458*0a6a1f1dSLionel Sambuc failIfError(sys::fs::openFileForRead(NewFilename, NewFD), NewFilename);
459*0a6a1f1dSLionel Sambuc assert(NewFD != -1);
460*0a6a1f1dSLionel Sambuc
461*0a6a1f1dSLionel Sambuc failIfError(sys::fs::status(NewFD, NewStatus), NewFilename);
462*0a6a1f1dSLionel Sambuc
463*0a6a1f1dSLionel Sambuc // Opening a directory doesn't make sense. Let it fail.
464*0a6a1f1dSLionel Sambuc // Linux cannot open directories with open(2), although
465*0a6a1f1dSLionel Sambuc // cygwin and *bsd can.
466*0a6a1f1dSLionel Sambuc if (NewStatus.type() == sys::fs::file_type::directory_file)
467*0a6a1f1dSLionel Sambuc failIfError(make_error_code(errc::is_a_directory), NewFilename);
468*0a6a1f1dSLionel Sambuc
469*0a6a1f1dSLionel Sambuc return NewFD;
470f4a2713aSLionel Sambuc }
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambuc template <typename T>
addMember(std::vector<NewArchiveIterator> & Members,T I,StringRef Name,int Pos=-1)473f4a2713aSLionel Sambuc void addMember(std::vector<NewArchiveIterator> &Members, T I, StringRef Name,
474f4a2713aSLionel Sambuc int Pos = -1) {
475f4a2713aSLionel Sambuc NewArchiveIterator NI(I, Name);
476f4a2713aSLionel Sambuc if (Pos == -1)
477f4a2713aSLionel Sambuc Members.push_back(NI);
478f4a2713aSLionel Sambuc else
479f4a2713aSLionel Sambuc Members[Pos] = NI;
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc
482f4a2713aSLionel Sambuc enum InsertAction {
483f4a2713aSLionel Sambuc IA_AddOldMember,
484f4a2713aSLionel Sambuc IA_AddNewMeber,
485f4a2713aSLionel Sambuc IA_Delete,
486f4a2713aSLionel Sambuc IA_MoveOldMember,
487f4a2713aSLionel Sambuc IA_MoveNewMember
488f4a2713aSLionel Sambuc };
489f4a2713aSLionel Sambuc
computeInsertAction(ArchiveOperation Operation,object::Archive::child_iterator I,StringRef Name,std::vector<StringRef>::iterator & Pos)490*0a6a1f1dSLionel Sambuc static InsertAction computeInsertAction(ArchiveOperation Operation,
491*0a6a1f1dSLionel Sambuc object::Archive::child_iterator I,
492*0a6a1f1dSLionel Sambuc StringRef Name,
493*0a6a1f1dSLionel Sambuc std::vector<StringRef>::iterator &Pos) {
494f4a2713aSLionel Sambuc if (Operation == QuickAppend || Members.empty())
495f4a2713aSLionel Sambuc return IA_AddOldMember;
496f4a2713aSLionel Sambuc
497*0a6a1f1dSLionel Sambuc auto MI =
498*0a6a1f1dSLionel Sambuc std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
499*0a6a1f1dSLionel Sambuc return Name == sys::path::filename(Path);
500*0a6a1f1dSLionel Sambuc });
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc if (MI == Members.end())
503f4a2713aSLionel Sambuc return IA_AddOldMember;
504f4a2713aSLionel Sambuc
505f4a2713aSLionel Sambuc Pos = MI;
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc if (Operation == Delete)
508f4a2713aSLionel Sambuc return IA_Delete;
509f4a2713aSLionel Sambuc
510f4a2713aSLionel Sambuc if (Operation == Move)
511f4a2713aSLionel Sambuc return IA_MoveOldMember;
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc if (Operation == ReplaceOrInsert) {
514f4a2713aSLionel Sambuc StringRef PosName = sys::path::filename(RelPos);
515f4a2713aSLionel Sambuc if (!OnlyUpdate) {
516f4a2713aSLionel Sambuc if (PosName.empty())
517f4a2713aSLionel Sambuc return IA_AddNewMeber;
518f4a2713aSLionel Sambuc return IA_MoveNewMember;
519f4a2713aSLionel Sambuc }
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc // We could try to optimize this to a fstat, but it is not a common
522f4a2713aSLionel Sambuc // operation.
523f4a2713aSLionel Sambuc sys::fs::file_status Status;
524*0a6a1f1dSLionel Sambuc failIfError(sys::fs::status(*MI, Status), *MI);
525f4a2713aSLionel Sambuc if (Status.getLastModificationTime() < I->getLastModified()) {
526f4a2713aSLionel Sambuc if (PosName.empty())
527f4a2713aSLionel Sambuc return IA_AddOldMember;
528f4a2713aSLionel Sambuc return IA_MoveOldMember;
529f4a2713aSLionel Sambuc }
530f4a2713aSLionel Sambuc
531f4a2713aSLionel Sambuc if (PosName.empty())
532f4a2713aSLionel Sambuc return IA_AddNewMeber;
533f4a2713aSLionel Sambuc return IA_MoveNewMember;
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc llvm_unreachable("No such operation");
536f4a2713aSLionel Sambuc }
537f4a2713aSLionel Sambuc
538f4a2713aSLionel Sambuc // We have to walk this twice and computing it is not trivial, so creating an
539f4a2713aSLionel Sambuc // explicit std::vector is actually fairly efficient.
540f4a2713aSLionel Sambuc static std::vector<NewArchiveIterator>
computeNewArchiveMembers(ArchiveOperation Operation,object::Archive * OldArchive)541f4a2713aSLionel Sambuc computeNewArchiveMembers(ArchiveOperation Operation,
542f4a2713aSLionel Sambuc object::Archive *OldArchive) {
543f4a2713aSLionel Sambuc std::vector<NewArchiveIterator> Ret;
544f4a2713aSLionel Sambuc std::vector<NewArchiveIterator> Moved;
545f4a2713aSLionel Sambuc int InsertPos = -1;
546f4a2713aSLionel Sambuc StringRef PosName = sys::path::filename(RelPos);
547f4a2713aSLionel Sambuc if (OldArchive) {
548*0a6a1f1dSLionel Sambuc for (auto &Child : OldArchive->children()) {
549f4a2713aSLionel Sambuc int Pos = Ret.size();
550*0a6a1f1dSLionel Sambuc ErrorOr<StringRef> NameOrErr = Child.getName();
551*0a6a1f1dSLionel Sambuc failIfError(NameOrErr.getError());
552*0a6a1f1dSLionel Sambuc StringRef Name = NameOrErr.get();
553f4a2713aSLionel Sambuc if (Name == PosName) {
554f4a2713aSLionel Sambuc assert(AddAfter || AddBefore);
555f4a2713aSLionel Sambuc if (AddBefore)
556f4a2713aSLionel Sambuc InsertPos = Pos;
557f4a2713aSLionel Sambuc else
558f4a2713aSLionel Sambuc InsertPos = Pos + 1;
559f4a2713aSLionel Sambuc }
560f4a2713aSLionel Sambuc
561*0a6a1f1dSLionel Sambuc std::vector<StringRef>::iterator MemberI = Members.end();
562*0a6a1f1dSLionel Sambuc InsertAction Action =
563*0a6a1f1dSLionel Sambuc computeInsertAction(Operation, Child, Name, MemberI);
564f4a2713aSLionel Sambuc switch (Action) {
565f4a2713aSLionel Sambuc case IA_AddOldMember:
566*0a6a1f1dSLionel Sambuc addMember(Ret, Child, Name);
567f4a2713aSLionel Sambuc break;
568f4a2713aSLionel Sambuc case IA_AddNewMeber:
569*0a6a1f1dSLionel Sambuc addMember(Ret, *MemberI, Name);
570f4a2713aSLionel Sambuc break;
571f4a2713aSLionel Sambuc case IA_Delete:
572f4a2713aSLionel Sambuc break;
573f4a2713aSLionel Sambuc case IA_MoveOldMember:
574*0a6a1f1dSLionel Sambuc addMember(Moved, Child, Name);
575f4a2713aSLionel Sambuc break;
576f4a2713aSLionel Sambuc case IA_MoveNewMember:
577*0a6a1f1dSLionel Sambuc addMember(Moved, *MemberI, Name);
578f4a2713aSLionel Sambuc break;
579f4a2713aSLionel Sambuc }
580f4a2713aSLionel Sambuc if (MemberI != Members.end())
581f4a2713aSLionel Sambuc Members.erase(MemberI);
582f4a2713aSLionel Sambuc }
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc
585f4a2713aSLionel Sambuc if (Operation == Delete)
586f4a2713aSLionel Sambuc return Ret;
587f4a2713aSLionel Sambuc
588f4a2713aSLionel Sambuc if (!RelPos.empty() && InsertPos == -1)
589f4a2713aSLionel Sambuc fail("Insertion point not found");
590f4a2713aSLionel Sambuc
591f4a2713aSLionel Sambuc if (RelPos.empty())
592f4a2713aSLionel Sambuc InsertPos = Ret.size();
593f4a2713aSLionel Sambuc
594f4a2713aSLionel Sambuc assert(unsigned(InsertPos) <= Ret.size());
595f4a2713aSLionel Sambuc Ret.insert(Ret.begin() + InsertPos, Moved.begin(), Moved.end());
596f4a2713aSLionel Sambuc
597f4a2713aSLionel Sambuc Ret.insert(Ret.begin() + InsertPos, Members.size(), NewArchiveIterator());
598f4a2713aSLionel Sambuc int Pos = InsertPos;
599*0a6a1f1dSLionel Sambuc for (auto &Member : Members) {
600*0a6a1f1dSLionel Sambuc StringRef Name = sys::path::filename(Member);
601*0a6a1f1dSLionel Sambuc addMember(Ret, Member, Name, Pos);
602*0a6a1f1dSLionel Sambuc ++Pos;
603f4a2713aSLionel Sambuc }
604f4a2713aSLionel Sambuc
605f4a2713aSLionel Sambuc return Ret;
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc template <typename T>
printWithSpacePadding(raw_fd_ostream & OS,T Data,unsigned Size,bool MayTruncate=false)609*0a6a1f1dSLionel Sambuc static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size,
610*0a6a1f1dSLionel Sambuc bool MayTruncate = false) {
611f4a2713aSLionel Sambuc uint64_t OldPos = OS.tell();
612f4a2713aSLionel Sambuc OS << Data;
613f4a2713aSLionel Sambuc unsigned SizeSoFar = OS.tell() - OldPos;
614*0a6a1f1dSLionel Sambuc if (Size > SizeSoFar) {
615f4a2713aSLionel Sambuc unsigned Remaining = Size - SizeSoFar;
616f4a2713aSLionel Sambuc for (unsigned I = 0; I < Remaining; ++I)
617f4a2713aSLionel Sambuc OS << ' ';
618*0a6a1f1dSLionel Sambuc } else if (Size < SizeSoFar) {
619*0a6a1f1dSLionel Sambuc assert(MayTruncate && "Data doesn't fit in Size");
620*0a6a1f1dSLionel Sambuc // Some of the data this is used for (like UID) can be larger than the
621*0a6a1f1dSLionel Sambuc // space available in the archive format. Truncate in that case.
622*0a6a1f1dSLionel Sambuc OS.seek(OldPos + Size);
623*0a6a1f1dSLionel Sambuc }
624f4a2713aSLionel Sambuc }
625f4a2713aSLionel Sambuc
print32BE(raw_fd_ostream & Out,unsigned Val)626f4a2713aSLionel Sambuc static void print32BE(raw_fd_ostream &Out, unsigned Val) {
627f4a2713aSLionel Sambuc for (int I = 3; I >= 0; --I) {
628f4a2713aSLionel Sambuc char V = (Val >> (8 * I)) & 0xff;
629f4a2713aSLionel Sambuc Out << V;
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc
printRestOfMemberHeader(raw_fd_ostream & Out,const sys::TimeValue & ModTime,unsigned UID,unsigned GID,unsigned Perms,unsigned Size)633f4a2713aSLionel Sambuc static void printRestOfMemberHeader(raw_fd_ostream &Out,
634f4a2713aSLionel Sambuc const sys::TimeValue &ModTime, unsigned UID,
635f4a2713aSLionel Sambuc unsigned GID, unsigned Perms,
636f4a2713aSLionel Sambuc unsigned Size) {
637f4a2713aSLionel Sambuc printWithSpacePadding(Out, ModTime.toEpochTime(), 12);
638*0a6a1f1dSLionel Sambuc printWithSpacePadding(Out, UID, 6, true);
639*0a6a1f1dSLionel Sambuc printWithSpacePadding(Out, GID, 6, true);
640f4a2713aSLionel Sambuc printWithSpacePadding(Out, format("%o", Perms), 8);
641f4a2713aSLionel Sambuc printWithSpacePadding(Out, Size, 10);
642f4a2713aSLionel Sambuc Out << "`\n";
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc
printMemberHeader(raw_fd_ostream & Out,StringRef Name,const sys::TimeValue & ModTime,unsigned UID,unsigned GID,unsigned Perms,unsigned Size)645f4a2713aSLionel Sambuc static void printMemberHeader(raw_fd_ostream &Out, StringRef Name,
646f4a2713aSLionel Sambuc const sys::TimeValue &ModTime, unsigned UID,
647f4a2713aSLionel Sambuc unsigned GID, unsigned Perms, unsigned Size) {
648f4a2713aSLionel Sambuc printWithSpacePadding(Out, Twine(Name) + "/", 16);
649f4a2713aSLionel Sambuc printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
650f4a2713aSLionel Sambuc }
651f4a2713aSLionel Sambuc
printMemberHeader(raw_fd_ostream & Out,unsigned NameOffset,const sys::TimeValue & ModTime,unsigned UID,unsigned GID,unsigned Perms,unsigned Size)652f4a2713aSLionel Sambuc static void printMemberHeader(raw_fd_ostream &Out, unsigned NameOffset,
653f4a2713aSLionel Sambuc const sys::TimeValue &ModTime, unsigned UID,
654f4a2713aSLionel Sambuc unsigned GID, unsigned Perms, unsigned Size) {
655f4a2713aSLionel Sambuc Out << '/';
656f4a2713aSLionel Sambuc printWithSpacePadding(Out, NameOffset, 15);
657f4a2713aSLionel Sambuc printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
658f4a2713aSLionel Sambuc }
659f4a2713aSLionel Sambuc
writeStringTable(raw_fd_ostream & Out,ArrayRef<NewArchiveIterator> Members,std::vector<unsigned> & StringMapIndexes)660f4a2713aSLionel Sambuc static void writeStringTable(raw_fd_ostream &Out,
661f4a2713aSLionel Sambuc ArrayRef<NewArchiveIterator> Members,
662f4a2713aSLionel Sambuc std::vector<unsigned> &StringMapIndexes) {
663f4a2713aSLionel Sambuc unsigned StartOffset = 0;
664f4a2713aSLionel Sambuc for (ArrayRef<NewArchiveIterator>::iterator I = Members.begin(),
665f4a2713aSLionel Sambuc E = Members.end();
666f4a2713aSLionel Sambuc I != E; ++I) {
667f4a2713aSLionel Sambuc StringRef Name = I->getName();
668f4a2713aSLionel Sambuc if (Name.size() < 16)
669f4a2713aSLionel Sambuc continue;
670f4a2713aSLionel Sambuc if (StartOffset == 0) {
671f4a2713aSLionel Sambuc printWithSpacePadding(Out, "//", 58);
672f4a2713aSLionel Sambuc Out << "`\n";
673f4a2713aSLionel Sambuc StartOffset = Out.tell();
674f4a2713aSLionel Sambuc }
675f4a2713aSLionel Sambuc StringMapIndexes.push_back(Out.tell() - StartOffset);
676f4a2713aSLionel Sambuc Out << Name << "/\n";
677f4a2713aSLionel Sambuc }
678f4a2713aSLionel Sambuc if (StartOffset == 0)
679f4a2713aSLionel Sambuc return;
680f4a2713aSLionel Sambuc if (Out.tell() % 2)
681f4a2713aSLionel Sambuc Out << '\n';
682f4a2713aSLionel Sambuc int Pos = Out.tell();
683f4a2713aSLionel Sambuc Out.seek(StartOffset - 12);
684f4a2713aSLionel Sambuc printWithSpacePadding(Out, Pos - StartOffset, 10);
685f4a2713aSLionel Sambuc Out.seek(Pos);
686f4a2713aSLionel Sambuc }
687f4a2713aSLionel Sambuc
688*0a6a1f1dSLionel Sambuc // Returns the offset of the first reference to a member offset.
writeSymbolTable(raw_fd_ostream & Out,ArrayRef<NewArchiveIterator> Members,ArrayRef<MemoryBufferRef> Buffers,std::vector<unsigned> & MemberOffsetRefs)689*0a6a1f1dSLionel Sambuc static unsigned writeSymbolTable(raw_fd_ostream &Out,
690*0a6a1f1dSLionel Sambuc ArrayRef<NewArchiveIterator> Members,
691*0a6a1f1dSLionel Sambuc ArrayRef<MemoryBufferRef> Buffers,
692*0a6a1f1dSLionel Sambuc std::vector<unsigned> &MemberOffsetRefs) {
693f4a2713aSLionel Sambuc unsigned StartOffset = 0;
694f4a2713aSLionel Sambuc unsigned MemberNum = 0;
695*0a6a1f1dSLionel Sambuc std::string NameBuf;
696*0a6a1f1dSLionel Sambuc raw_string_ostream NameOS(NameBuf);
697*0a6a1f1dSLionel Sambuc unsigned NumSyms = 0;
698*0a6a1f1dSLionel Sambuc LLVMContext &Context = getGlobalContext();
699f4a2713aSLionel Sambuc for (ArrayRef<NewArchiveIterator>::iterator I = Members.begin(),
700f4a2713aSLionel Sambuc E = Members.end();
701f4a2713aSLionel Sambuc I != E; ++I, ++MemberNum) {
702*0a6a1f1dSLionel Sambuc MemoryBufferRef MemberBuffer = Buffers[MemberNum];
703*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
704*0a6a1f1dSLionel Sambuc object::SymbolicFile::createSymbolicFile(
705*0a6a1f1dSLionel Sambuc MemberBuffer, sys::fs::file_magic::unknown, &Context);
706*0a6a1f1dSLionel Sambuc if (!ObjOrErr)
707*0a6a1f1dSLionel Sambuc continue; // FIXME: check only for "not an object file" errors.
708*0a6a1f1dSLionel Sambuc object::SymbolicFile &Obj = *ObjOrErr.get();
709*0a6a1f1dSLionel Sambuc
710f4a2713aSLionel Sambuc if (!StartOffset) {
711f4a2713aSLionel Sambuc printMemberHeader(Out, "", sys::TimeValue::now(), 0, 0, 0, 0);
712f4a2713aSLionel Sambuc StartOffset = Out.tell();
713f4a2713aSLionel Sambuc print32BE(Out, 0);
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc
716*0a6a1f1dSLionel Sambuc for (const object::BasicSymbolRef &S : Obj.symbols()) {
717*0a6a1f1dSLionel Sambuc uint32_t Symflags = S.getFlags();
718f4a2713aSLionel Sambuc if (Symflags & object::SymbolRef::SF_FormatSpecific)
719f4a2713aSLionel Sambuc continue;
720f4a2713aSLionel Sambuc if (!(Symflags & object::SymbolRef::SF_Global))
721f4a2713aSLionel Sambuc continue;
722f4a2713aSLionel Sambuc if (Symflags & object::SymbolRef::SF_Undefined)
723f4a2713aSLionel Sambuc continue;
724*0a6a1f1dSLionel Sambuc failIfError(S.printName(NameOS));
725*0a6a1f1dSLionel Sambuc NameOS << '\0';
726*0a6a1f1dSLionel Sambuc ++NumSyms;
727*0a6a1f1dSLionel Sambuc MemberOffsetRefs.push_back(MemberNum);
728f4a2713aSLionel Sambuc print32BE(Out, 0);
729f4a2713aSLionel Sambuc }
730f4a2713aSLionel Sambuc }
731*0a6a1f1dSLionel Sambuc Out << NameOS.str();
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc if (StartOffset == 0)
734*0a6a1f1dSLionel Sambuc return 0;
735f4a2713aSLionel Sambuc
736f4a2713aSLionel Sambuc if (Out.tell() % 2)
737f4a2713aSLionel Sambuc Out << '\0';
738f4a2713aSLionel Sambuc
739f4a2713aSLionel Sambuc unsigned Pos = Out.tell();
740f4a2713aSLionel Sambuc Out.seek(StartOffset - 12);
741f4a2713aSLionel Sambuc printWithSpacePadding(Out, Pos - StartOffset, 10);
742f4a2713aSLionel Sambuc Out.seek(StartOffset);
743*0a6a1f1dSLionel Sambuc print32BE(Out, NumSyms);
744f4a2713aSLionel Sambuc Out.seek(Pos);
745*0a6a1f1dSLionel Sambuc return StartOffset + 4;
746f4a2713aSLionel Sambuc }
747f4a2713aSLionel Sambuc
748*0a6a1f1dSLionel Sambuc static void
performWriteOperation(ArchiveOperation Operation,object::Archive * OldArchive,std::vector<NewArchiveIterator> & NewMembers)749*0a6a1f1dSLionel Sambuc performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
750*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> &NewMembers) {
751f4a2713aSLionel Sambuc SmallString<128> TmpArchive;
752f4a2713aSLionel Sambuc failIfError(sys::fs::createUniqueFile(ArchiveName + ".temp-archive-%%%%%%%.a",
753f4a2713aSLionel Sambuc TmpArchiveFD, TmpArchive));
754f4a2713aSLionel Sambuc
755f4a2713aSLionel Sambuc TemporaryOutput = TmpArchive.c_str();
756f4a2713aSLionel Sambuc tool_output_file Output(TemporaryOutput, TmpArchiveFD);
757f4a2713aSLionel Sambuc raw_fd_ostream &Out = Output.os();
758f4a2713aSLionel Sambuc Out << "!<arch>\n";
759f4a2713aSLionel Sambuc
760*0a6a1f1dSLionel Sambuc std::vector<unsigned> MemberOffsetRefs;
761f4a2713aSLionel Sambuc
762*0a6a1f1dSLionel Sambuc std::vector<std::unique_ptr<MemoryBuffer>> Buffers;
763*0a6a1f1dSLionel Sambuc std::vector<MemoryBufferRef> Members;
764*0a6a1f1dSLionel Sambuc std::vector<sys::fs::file_status> NewMemberStatus;
765f4a2713aSLionel Sambuc
766*0a6a1f1dSLionel Sambuc for (unsigned I = 0, N = NewMembers.size(); I < N; ++I) {
767*0a6a1f1dSLionel Sambuc NewArchiveIterator &Member = NewMembers[I];
768*0a6a1f1dSLionel Sambuc MemoryBufferRef MemberRef;
769*0a6a1f1dSLionel Sambuc
770*0a6a1f1dSLionel Sambuc if (Member.isNewMember()) {
771*0a6a1f1dSLionel Sambuc StringRef Filename = Member.getNew();
772*0a6a1f1dSLionel Sambuc NewMemberStatus.resize(NewMemberStatus.size() + 1);
773*0a6a1f1dSLionel Sambuc sys::fs::file_status &Status = NewMemberStatus.back();
774*0a6a1f1dSLionel Sambuc int FD = Member.getFD(Status);
775*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
776*0a6a1f1dSLionel Sambuc MemoryBuffer::getOpenFile(FD, Filename, Status.getSize(), false);
777*0a6a1f1dSLionel Sambuc failIfError(MemberBufferOrErr.getError(), Filename);
778*0a6a1f1dSLionel Sambuc if (close(FD) != 0)
779*0a6a1f1dSLionel Sambuc fail("Could not close file");
780*0a6a1f1dSLionel Sambuc Buffers.push_back(std::move(MemberBufferOrErr.get()));
781*0a6a1f1dSLionel Sambuc MemberRef = Buffers.back()->getMemBufferRef();
782*0a6a1f1dSLionel Sambuc } else {
783*0a6a1f1dSLionel Sambuc object::Archive::child_iterator OldMember = Member.getOld();
784*0a6a1f1dSLionel Sambuc ErrorOr<MemoryBufferRef> MemberBufferOrErr =
785*0a6a1f1dSLionel Sambuc OldMember->getMemoryBufferRef();
786*0a6a1f1dSLionel Sambuc failIfError(MemberBufferOrErr.getError());
787*0a6a1f1dSLionel Sambuc MemberRef = MemberBufferOrErr.get();
788*0a6a1f1dSLionel Sambuc }
789*0a6a1f1dSLionel Sambuc Members.push_back(MemberRef);
790*0a6a1f1dSLionel Sambuc }
791*0a6a1f1dSLionel Sambuc
792*0a6a1f1dSLionel Sambuc unsigned MemberReferenceOffset = 0;
793f4a2713aSLionel Sambuc if (Symtab) {
794*0a6a1f1dSLionel Sambuc MemberReferenceOffset =
795*0a6a1f1dSLionel Sambuc writeSymbolTable(Out, NewMembers, Members, MemberOffsetRefs);
796f4a2713aSLionel Sambuc }
797f4a2713aSLionel Sambuc
798f4a2713aSLionel Sambuc std::vector<unsigned> StringMapIndexes;
799f4a2713aSLionel Sambuc writeStringTable(Out, NewMembers, StringMapIndexes);
800f4a2713aSLionel Sambuc
801f4a2713aSLionel Sambuc unsigned MemberNum = 0;
802f4a2713aSLionel Sambuc unsigned LongNameMemberNum = 0;
803*0a6a1f1dSLionel Sambuc unsigned NewMemberNum = 0;
804*0a6a1f1dSLionel Sambuc std::vector<unsigned> MemberOffset;
805f4a2713aSLionel Sambuc for (std::vector<NewArchiveIterator>::iterator I = NewMembers.begin(),
806f4a2713aSLionel Sambuc E = NewMembers.end();
807f4a2713aSLionel Sambuc I != E; ++I, ++MemberNum) {
808f4a2713aSLionel Sambuc
809f4a2713aSLionel Sambuc unsigned Pos = Out.tell();
810*0a6a1f1dSLionel Sambuc MemberOffset.push_back(Pos);
811f4a2713aSLionel Sambuc
812*0a6a1f1dSLionel Sambuc MemoryBufferRef File = Members[MemberNum];
813f4a2713aSLionel Sambuc if (I->isNewMember()) {
814*0a6a1f1dSLionel Sambuc StringRef FileName = I->getNew();
815*0a6a1f1dSLionel Sambuc const sys::fs::file_status &Status = NewMemberStatus[NewMemberNum];
816*0a6a1f1dSLionel Sambuc NewMemberNum++;
817f4a2713aSLionel Sambuc
818f4a2713aSLionel Sambuc StringRef Name = sys::path::filename(FileName);
819f4a2713aSLionel Sambuc if (Name.size() < 16)
820f4a2713aSLionel Sambuc printMemberHeader(Out, Name, Status.getLastModificationTime(),
821f4a2713aSLionel Sambuc Status.getUser(), Status.getGroup(),
822f4a2713aSLionel Sambuc Status.permissions(), Status.getSize());
823f4a2713aSLionel Sambuc else
824f4a2713aSLionel Sambuc printMemberHeader(Out, StringMapIndexes[LongNameMemberNum++],
825f4a2713aSLionel Sambuc Status.getLastModificationTime(), Status.getUser(),
826f4a2713aSLionel Sambuc Status.getGroup(), Status.permissions(),
827f4a2713aSLionel Sambuc Status.getSize());
828f4a2713aSLionel Sambuc } else {
829f4a2713aSLionel Sambuc object::Archive::child_iterator OldMember = I->getOld();
830f4a2713aSLionel Sambuc StringRef Name = I->getName();
831f4a2713aSLionel Sambuc
832f4a2713aSLionel Sambuc if (Name.size() < 16)
833f4a2713aSLionel Sambuc printMemberHeader(Out, Name, OldMember->getLastModified(),
834f4a2713aSLionel Sambuc OldMember->getUID(), OldMember->getGID(),
835f4a2713aSLionel Sambuc OldMember->getAccessMode(), OldMember->getSize());
836f4a2713aSLionel Sambuc else
837f4a2713aSLionel Sambuc printMemberHeader(Out, StringMapIndexes[LongNameMemberNum++],
838f4a2713aSLionel Sambuc OldMember->getLastModified(), OldMember->getUID(),
839f4a2713aSLionel Sambuc OldMember->getGID(), OldMember->getAccessMode(),
840f4a2713aSLionel Sambuc OldMember->getSize());
841f4a2713aSLionel Sambuc }
842f4a2713aSLionel Sambuc
843*0a6a1f1dSLionel Sambuc Out << File.getBuffer();
844*0a6a1f1dSLionel Sambuc
845f4a2713aSLionel Sambuc if (Out.tell() % 2)
846f4a2713aSLionel Sambuc Out << '\n';
847f4a2713aSLionel Sambuc }
848*0a6a1f1dSLionel Sambuc
849*0a6a1f1dSLionel Sambuc if (MemberReferenceOffset) {
850*0a6a1f1dSLionel Sambuc Out.seek(MemberReferenceOffset);
851*0a6a1f1dSLionel Sambuc for (unsigned MemberNum : MemberOffsetRefs)
852*0a6a1f1dSLionel Sambuc print32BE(Out, MemberOffset[MemberNum]);
853*0a6a1f1dSLionel Sambuc }
854*0a6a1f1dSLionel Sambuc
855f4a2713aSLionel Sambuc Output.keep();
856f4a2713aSLionel Sambuc Out.close();
857f4a2713aSLionel Sambuc sys::fs::rename(TemporaryOutput, ArchiveName);
858*0a6a1f1dSLionel Sambuc TemporaryOutput = nullptr;
859*0a6a1f1dSLionel Sambuc }
860*0a6a1f1dSLionel Sambuc
861*0a6a1f1dSLionel Sambuc static void
performWriteOperation(ArchiveOperation Operation,object::Archive * OldArchive,std::vector<NewArchiveIterator> * NewMembersP)862*0a6a1f1dSLionel Sambuc performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
863*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> *NewMembersP) {
864*0a6a1f1dSLionel Sambuc if (NewMembersP) {
865*0a6a1f1dSLionel Sambuc performWriteOperation(Operation, OldArchive, *NewMembersP);
866*0a6a1f1dSLionel Sambuc return;
867*0a6a1f1dSLionel Sambuc }
868*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> NewMembers =
869*0a6a1f1dSLionel Sambuc computeNewArchiveMembers(Operation, OldArchive);
870*0a6a1f1dSLionel Sambuc performWriteOperation(Operation, OldArchive, NewMembers);
871f4a2713aSLionel Sambuc }
872f4a2713aSLionel Sambuc
createSymbolTable(object::Archive * OldArchive)873f4a2713aSLionel Sambuc static void createSymbolTable(object::Archive *OldArchive) {
874f4a2713aSLionel Sambuc // When an archive is created or modified, if the s option is given, the
875f4a2713aSLionel Sambuc // resulting archive will have a current symbol table. If the S option
876f4a2713aSLionel Sambuc // is given, it will have no symbol table.
877f4a2713aSLionel Sambuc // In summary, we only need to update the symbol table if we have none.
878f4a2713aSLionel Sambuc // This is actually very common because of broken build systems that think
879f4a2713aSLionel Sambuc // they have to run ranlib.
880f4a2713aSLionel Sambuc if (OldArchive->hasSymbolTable())
881f4a2713aSLionel Sambuc return;
882f4a2713aSLionel Sambuc
883*0a6a1f1dSLionel Sambuc performWriteOperation(CreateSymTab, OldArchive, nullptr);
884f4a2713aSLionel Sambuc }
885f4a2713aSLionel Sambuc
performOperation(ArchiveOperation Operation,object::Archive * OldArchive,std::vector<NewArchiveIterator> * NewMembers)886f4a2713aSLionel Sambuc static void performOperation(ArchiveOperation Operation,
887*0a6a1f1dSLionel Sambuc object::Archive *OldArchive,
888*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> *NewMembers) {
889f4a2713aSLionel Sambuc switch (Operation) {
890f4a2713aSLionel Sambuc case Print:
891f4a2713aSLionel Sambuc case DisplayTable:
892f4a2713aSLionel Sambuc case Extract:
893f4a2713aSLionel Sambuc performReadOperation(Operation, OldArchive);
894f4a2713aSLionel Sambuc return;
895f4a2713aSLionel Sambuc
896f4a2713aSLionel Sambuc case Delete:
897f4a2713aSLionel Sambuc case Move:
898f4a2713aSLionel Sambuc case QuickAppend:
899f4a2713aSLionel Sambuc case ReplaceOrInsert:
900*0a6a1f1dSLionel Sambuc performWriteOperation(Operation, OldArchive, NewMembers);
901f4a2713aSLionel Sambuc return;
902f4a2713aSLionel Sambuc case CreateSymTab:
903f4a2713aSLionel Sambuc createSymbolTable(OldArchive);
904f4a2713aSLionel Sambuc return;
905f4a2713aSLionel Sambuc }
906f4a2713aSLionel Sambuc llvm_unreachable("Unknown operation.");
907f4a2713aSLionel Sambuc }
908f4a2713aSLionel Sambuc
performOperation(ArchiveOperation Operation,std::vector<NewArchiveIterator> * NewMembers)909*0a6a1f1dSLionel Sambuc static int performOperation(ArchiveOperation Operation,
910*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> *NewMembers) {
911*0a6a1f1dSLionel Sambuc // Create or open the archive object.
912*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
913*0a6a1f1dSLionel Sambuc MemoryBuffer::getFile(ArchiveName, -1, false);
914*0a6a1f1dSLionel Sambuc std::error_code EC = Buf.getError();
915*0a6a1f1dSLionel Sambuc if (EC && EC != errc::no_such_file_or_directory) {
916*0a6a1f1dSLionel Sambuc errs() << ToolName << ": error opening '" << ArchiveName
917*0a6a1f1dSLionel Sambuc << "': " << EC.message() << "!\n";
918*0a6a1f1dSLionel Sambuc return 1;
919*0a6a1f1dSLionel Sambuc }
920f4a2713aSLionel Sambuc
921*0a6a1f1dSLionel Sambuc if (!EC) {
922*0a6a1f1dSLionel Sambuc object::Archive Archive(Buf.get()->getMemBufferRef(), EC);
923*0a6a1f1dSLionel Sambuc
924*0a6a1f1dSLionel Sambuc if (EC) {
925*0a6a1f1dSLionel Sambuc errs() << ToolName << ": error loading '" << ArchiveName
926*0a6a1f1dSLionel Sambuc << "': " << EC.message() << "!\n";
927*0a6a1f1dSLionel Sambuc return 1;
928*0a6a1f1dSLionel Sambuc }
929*0a6a1f1dSLionel Sambuc performOperation(Operation, &Archive, NewMembers);
930*0a6a1f1dSLionel Sambuc return 0;
931*0a6a1f1dSLionel Sambuc }
932*0a6a1f1dSLionel Sambuc
933*0a6a1f1dSLionel Sambuc assert(EC == errc::no_such_file_or_directory);
934*0a6a1f1dSLionel Sambuc
935*0a6a1f1dSLionel Sambuc if (!shouldCreateArchive(Operation)) {
936*0a6a1f1dSLionel Sambuc failIfError(EC, Twine("error loading '") + ArchiveName + "'");
937*0a6a1f1dSLionel Sambuc } else {
938*0a6a1f1dSLionel Sambuc if (!Create) {
939*0a6a1f1dSLionel Sambuc // Produce a warning if we should and we're creating the archive
940*0a6a1f1dSLionel Sambuc errs() << ToolName << ": creating " << ArchiveName << "\n";
941*0a6a1f1dSLionel Sambuc }
942*0a6a1f1dSLionel Sambuc }
943*0a6a1f1dSLionel Sambuc
944*0a6a1f1dSLionel Sambuc performOperation(Operation, nullptr, NewMembers);
945*0a6a1f1dSLionel Sambuc return 0;
946*0a6a1f1dSLionel Sambuc }
947*0a6a1f1dSLionel Sambuc
runMRIScript()948*0a6a1f1dSLionel Sambuc static void runMRIScript() {
949*0a6a1f1dSLionel Sambuc enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
950*0a6a1f1dSLionel Sambuc
951*0a6a1f1dSLionel Sambuc ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
952*0a6a1f1dSLionel Sambuc failIfError(Buf.getError());
953*0a6a1f1dSLionel Sambuc const MemoryBuffer &Ref = *Buf.get();
954*0a6a1f1dSLionel Sambuc bool Saved = false;
955*0a6a1f1dSLionel Sambuc std::vector<NewArchiveIterator> NewMembers;
956*0a6a1f1dSLionel Sambuc std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
957*0a6a1f1dSLionel Sambuc std::vector<std::unique_ptr<object::Archive>> Archives;
958*0a6a1f1dSLionel Sambuc
959*0a6a1f1dSLionel Sambuc for (line_iterator I(Ref, /*SkipBlanks*/ true, ';'), E; I != E; ++I) {
960*0a6a1f1dSLionel Sambuc StringRef Line = *I;
961*0a6a1f1dSLionel Sambuc StringRef CommandStr, Rest;
962*0a6a1f1dSLionel Sambuc std::tie(CommandStr, Rest) = Line.split(' ');
963*0a6a1f1dSLionel Sambuc Rest = Rest.trim();
964*0a6a1f1dSLionel Sambuc if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
965*0a6a1f1dSLionel Sambuc Rest = Rest.drop_front().drop_back();
966*0a6a1f1dSLionel Sambuc auto Command = StringSwitch<MRICommand>(CommandStr.lower())
967*0a6a1f1dSLionel Sambuc .Case("addlib", MRICommand::AddLib)
968*0a6a1f1dSLionel Sambuc .Case("addmod", MRICommand::AddMod)
969*0a6a1f1dSLionel Sambuc .Case("create", MRICommand::Create)
970*0a6a1f1dSLionel Sambuc .Case("save", MRICommand::Save)
971*0a6a1f1dSLionel Sambuc .Case("end", MRICommand::End)
972*0a6a1f1dSLionel Sambuc .Default(MRICommand::Invalid);
973*0a6a1f1dSLionel Sambuc
974*0a6a1f1dSLionel Sambuc switch (Command) {
975*0a6a1f1dSLionel Sambuc case MRICommand::AddLib: {
976*0a6a1f1dSLionel Sambuc auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false);
977*0a6a1f1dSLionel Sambuc failIfError(BufOrErr.getError(), "Could not open library");
978*0a6a1f1dSLionel Sambuc ArchiveBuffers.push_back(std::move(*BufOrErr));
979*0a6a1f1dSLionel Sambuc auto LibOrErr =
980*0a6a1f1dSLionel Sambuc object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
981*0a6a1f1dSLionel Sambuc failIfError(LibOrErr.getError(), "Could not parse library");
982*0a6a1f1dSLionel Sambuc Archives.push_back(std::move(*LibOrErr));
983*0a6a1f1dSLionel Sambuc object::Archive &Lib = *Archives.back();
984*0a6a1f1dSLionel Sambuc for (auto &Member : Lib.children()) {
985*0a6a1f1dSLionel Sambuc ErrorOr<StringRef> NameOrErr = Member.getName();
986*0a6a1f1dSLionel Sambuc failIfError(NameOrErr.getError());
987*0a6a1f1dSLionel Sambuc addMember(NewMembers, Member, *NameOrErr);
988*0a6a1f1dSLionel Sambuc }
989*0a6a1f1dSLionel Sambuc break;
990*0a6a1f1dSLionel Sambuc }
991*0a6a1f1dSLionel Sambuc case MRICommand::AddMod:
992*0a6a1f1dSLionel Sambuc addMember(NewMembers, Rest, sys::path::filename(Rest));
993*0a6a1f1dSLionel Sambuc break;
994*0a6a1f1dSLionel Sambuc case MRICommand::Create:
995*0a6a1f1dSLionel Sambuc Create = true;
996*0a6a1f1dSLionel Sambuc if (!ArchiveName.empty())
997*0a6a1f1dSLionel Sambuc fail("Editing multiple archives not supported");
998*0a6a1f1dSLionel Sambuc if (Saved)
999*0a6a1f1dSLionel Sambuc fail("File already saved");
1000*0a6a1f1dSLionel Sambuc ArchiveName = Rest;
1001*0a6a1f1dSLionel Sambuc break;
1002*0a6a1f1dSLionel Sambuc case MRICommand::Save:
1003*0a6a1f1dSLionel Sambuc Saved = true;
1004*0a6a1f1dSLionel Sambuc break;
1005*0a6a1f1dSLionel Sambuc case MRICommand::End:
1006*0a6a1f1dSLionel Sambuc break;
1007*0a6a1f1dSLionel Sambuc case MRICommand::Invalid:
1008*0a6a1f1dSLionel Sambuc fail("Unknown command: " + CommandStr);
1009*0a6a1f1dSLionel Sambuc }
1010*0a6a1f1dSLionel Sambuc }
1011*0a6a1f1dSLionel Sambuc
1012*0a6a1f1dSLionel Sambuc // Nothing to do if not saved.
1013*0a6a1f1dSLionel Sambuc if (Saved)
1014*0a6a1f1dSLionel Sambuc performOperation(ReplaceOrInsert, &NewMembers);
1015*0a6a1f1dSLionel Sambuc exit(0);
1016*0a6a1f1dSLionel Sambuc }
1017*0a6a1f1dSLionel Sambuc
ar_main()1018*0a6a1f1dSLionel Sambuc static int ar_main() {
1019*0a6a1f1dSLionel Sambuc // Do our own parsing of the command line because the CommandLine utility
1020*0a6a1f1dSLionel Sambuc // can't handle the grouped positional parameters without a dash.
1021*0a6a1f1dSLionel Sambuc ArchiveOperation Operation = parseCommandLine();
1022*0a6a1f1dSLionel Sambuc return performOperation(Operation, nullptr);
1023*0a6a1f1dSLionel Sambuc }
1024*0a6a1f1dSLionel Sambuc
ranlib_main()1025*0a6a1f1dSLionel Sambuc static int ranlib_main() {
1026*0a6a1f1dSLionel Sambuc if (RestOfArgs.size() != 1)
1027*0a6a1f1dSLionel Sambuc fail(ToolName + "takes just one archive as argument");
1028*0a6a1f1dSLionel Sambuc ArchiveName = RestOfArgs[0];
1029*0a6a1f1dSLionel Sambuc return performOperation(CreateSymTab, nullptr);
1030*0a6a1f1dSLionel Sambuc }
1031*0a6a1f1dSLionel Sambuc
main(int argc,char ** argv)1032f4a2713aSLionel Sambuc int main(int argc, char **argv) {
1033f4a2713aSLionel Sambuc ToolName = argv[0];
1034f4a2713aSLionel Sambuc // Print a stack trace if we signal out.
1035f4a2713aSLionel Sambuc sys::PrintStackTraceOnErrorSignal();
1036f4a2713aSLionel Sambuc PrettyStackTraceProgram X(argc, argv);
1037f4a2713aSLionel Sambuc llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1038f4a2713aSLionel Sambuc
1039f4a2713aSLionel Sambuc // Have the command line options parsed and handle things
1040f4a2713aSLionel Sambuc // like --help and --version.
1041f4a2713aSLionel Sambuc cl::ParseCommandLineOptions(argc, argv,
1042f4a2713aSLionel Sambuc "LLVM Archiver (llvm-ar)\n\n"
1043f4a2713aSLionel Sambuc " This program archives bitcode files into single libraries\n"
1044f4a2713aSLionel Sambuc );
1045f4a2713aSLionel Sambuc
1046*0a6a1f1dSLionel Sambuc llvm::InitializeAllTargetInfos();
1047*0a6a1f1dSLionel Sambuc llvm::InitializeAllTargetMCs();
1048*0a6a1f1dSLionel Sambuc llvm::InitializeAllAsmParsers();
1049*0a6a1f1dSLionel Sambuc
1050f4a2713aSLionel Sambuc StringRef Stem = sys::path::stem(ToolName);
1051f4a2713aSLionel Sambuc if (Stem.find("ar") != StringRef::npos)
1052*0a6a1f1dSLionel Sambuc return ar_main();
1053f4a2713aSLionel Sambuc if (Stem.find("ranlib") != StringRef::npos)
1054f4a2713aSLionel Sambuc return ranlib_main();
1055f4a2713aSLionel Sambuc fail("Not ranlib or ar!");
1056f4a2713aSLionel Sambuc }
1057