1 //===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Builds up standard unix archive files (.a) containing LLVM bytecode.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileUtilities.h"
18 #include "llvm/System/Signals.h"
24 #include <sys/types.h>
33 #define ARFMAG "\n" /* header trailer string */
34 #define ARMAG "!<arch>\n" /* magic string */
35 #define SARMAG 8 /* length of magic string */
36 #define VERSION "llvm-ar is a part of the LLVM compiler infrastructure.\nPlease see http://llvm.cs.uiuc.edu for more information.\n";
39 // Each file member is preceded by a file member header. Which is
40 // of the following format:
42 // char ar_name[16] - '/' terminated file member name.
43 // If the file name does not fit, a dummy name is used.
44 // char ar_date[12] - file date in decimal
45 // char ar_uid[6] - User id of file owner in decimal.
46 // char ar_gid[6] - Group ID file belongs to in decimal.
47 // char ar_mode[8] - File mode in octal.
48 // char ar_size[10] - Size of file in decimal.
49 // char ar_fmag[2] - Trailer of header file, a newline.
70 //Option for X32_64, not used but must allow it to be present.
71 cl::opt<bool> X32Option ("X32_64", cl::desc("Ignored option spelt -X32_64, for compatibility with AIX"), cl::Optional);
74 cl::opt<string> Options(cl::Positional, cl::desc("{dmpqrstx}[abcfilNoPsSuvV] "), cl::Required);
77 cl::list<string> RestofArgs(cl::Positional, cl::desc("[relpos] [count]] <archive-file> [members..]"), cl::Optional);
79 //booleans to represent Operation, only one can be preformed at a time
80 bool Print, Delete, Move, QuickAppend, InsertWithReplacement, DisplayTable;
83 //Modifiers to follow operation to vary behavior
84 bool AddAfter, AddBefore, Create, TruncateNames, InsertBefore, UseCount;
85 bool OriginalDates, FullPath, SymTable, OnlyUpdate, Verbose;
90 //Count, use for multiple entries in the archive with the same name
97 std::vector<string> Members;
100 // WriteSymbolTable - Writes symbol table to ArchiveFile, return false
101 // on errors. Also returns by reference size of symbol table.
103 // Overview of method:
104 // 1) Generate the header for the symbol table. This is a normal
105 // archive member header, but it has a zero length name.
106 // 2) For each archive member file, stat the file and parse the bytecode
107 // Store cumulative offset (file size + header size).
108 // 3) Loop over all the symbols for the current member file,
109 // add offset entry to offset vector, and add symbol name to its vector.
110 // Note: The symbol name vector is a vector of chars to speed up calculating
111 // the total size of the symbol table.
112 // 4) Update offset vector once we know the total size of symbol table. This is
113 // because the symbol table appears before all archive member file contents.
114 // We add the size of magic string, and size of symbol table to each offset.
115 // 5) If the new updated offset it not even, we add 1 byte to offset because
116 // a newline will be inserted when writing member files. This adjustment is
117 // cummulative (ie. each time we have an odd offset we add 1 to total adjustment).
118 // 6) Lastly, write symbol table to file.
120 bool WriteSymbolTable(std::ofstream &ArchiveFile) {
122 //Create header for symbol table. This is essentially an empty header with the
123 //name set to a '/' to indicate its a symbol table.
127 //Name of symbol table is '/'
131 //Set the header trailer to a newline
132 memcpy(Hdr.fmag,ARFMAG,sizeof(ARFMAG));
135 //Write header to archive file
136 ArchiveFile.write((char*)&Hdr, sizeof(Hdr));
139 unsigned memoff = 0; //Keep Track of total size of files added to archive
140 std::vector<unsigned> offsets; //Vector of offsets into archive file
141 std::vector<char> names; //Vector of characters that are the symbol names.
143 //Loop over archive member files, parse bytecode, and generate symbol table.
144 for(unsigned i=0; i<Members.size(); ++i) {
146 //Open Member file for reading and copy to buffer
147 int FD = open(Members[i].c_str(),O_RDONLY);
149 //Check for errors opening the file.
151 std::cerr << "Error opening file!\n";
156 unsigned Length = getFileSize(Members[i]);
157 if (Length == (unsigned)-1) {
158 std::cerr << "Error stating file\n";
162 //Read in file into a buffer.
163 unsigned char *buf = (unsigned char*)mmap(0, Length,PROT_READ,
166 //Check if mmap failed.
167 if (buf == (unsigned char*)MAP_FAILED) {
168 std::cerr << "Error mmapping file!\n";
172 //Parse the bytecode file and get all the symbols.
174 Module *M = ParseBytecodeBuffer(buf,Length,Members[i],&ErrorStr);
176 //Check for errors parsing bytecode.
178 //std::cerr << "Error Parsing Bytecode\n";
182 //Loop over function names and global vars and add to symbol maps
183 for(Module::iterator I = M->begin(), E=M->end(); I != E; ++I) {
186 string NM = ((Function*)I)->getName();
188 //Loop over the characters in the name and add to symbol name vector
189 for(unsigned i=0; i<NM.size(); ++i)
190 names.push_back(NM[i]);
192 //Each symbol is null terminated.
193 names.push_back('\0');
195 //Add offset to vector of offsets
196 offsets.push_back(memoff);
199 memoff += Length + sizeof(Hdr);
202 //Determine how large our symbol table is.
203 unsigned symbolTableSize = sizeof(Hdr) + 4 + 4*(offsets.size()) + names.size();
204 std::cout << "Symbol Table Size: " << symbolTableSize << "\n";
206 //Number of symbols should be in network byte order as well
208 unsigned temp = offsets.size();
209 num[0] = (temp >> 24) & 255;
210 num[1] = (temp >> 16) & 255;
211 num[2] = (temp >> 8) & 255;
214 //Write number of symbols to archive file
215 ArchiveFile.write(num,4);
217 //Adjustment to offset to start files on even byte boundaries
220 //Update offsets write symbol table to archive.
221 for(unsigned i=0; i<offsets.size(); ++i) {
223 offsets[i] = offsets[i] + symbolTableSize + SARMAG;
224 offsets[i] += adjust;
225 if((offsets[i] % 2 != 0)) {
227 offsets[i] += adjust;
230 std::cout << "Offset: " << offsets[i] << "\n";
231 output[0] = (offsets[i] >> 24) & 255;
232 output[1] = (offsets[i] >> 16) & 255;
233 output[2] = (offsets[i] >> 8) & 255;
234 output[3] = offsets[i] & 255;
235 ArchiveFile.write(output,4);
239 //Write out symbol name vector.
240 for(unsigned i=0; i<names.size(); ++i)
241 ArchiveFile << names[i];
246 // AddMemberToArchive - Writes member file to archive. Returns false on errors.
248 // Overview of method:
249 // 1) Open file, and stat it.
250 // 2) Fill out header using stat information. If name is longer then 15
251 // characters, use "dummy" name.
252 // 3) Write header and file contents to disk.
253 // 4) Keep track of total offset into file, and insert a newline if it is odd.
255 bool AddMemberToArchive(string Member, std::ofstream &ArchiveFile) {
257 std::cout << "Member File Start: " << ArchiveFile.tellp() << "\n";
259 ar_hdr Hdr; //Header for archive member file.
261 //stat the file to get info
263 if (stat(Member.c_str(), &StatBuf) == -1 || StatBuf.st_size == 0)
268 //set name to white spaces
269 memset(Hdr.name,' ', sizeof(Hdr.name));
271 //check the size of the name, if less than 15, we can copy it directly
272 //otherwise we give it a dummy name for now
273 if(Member.length() < 16)
274 memcpy(Hdr.name,Member.c_str(),Member.length());
276 memcpy(Hdr.name, "Dummy", 5);
278 //terminate name with forward slash
281 //file member size in decimal
282 unsigned Length = StatBuf.st_size;
283 sprintf(Hdr.size,"%d", Length);
284 std::cout << "Size: " << Length << "\n";
286 //file member user id in decimal
287 sprintf(Hdr.uid, "%d", StatBuf.st_uid);
289 //file member group id in decimal
290 sprintf(Hdr.gid, "%d", StatBuf.st_gid);
292 //file member date in decimal
293 sprintf(Hdr.date,"%d", (int)StatBuf.st_mtime);
295 //file member mode in OCTAL
296 sprintf(Hdr.mode,"%d", StatBuf.st_mode);
298 //add our header trailer
299 memcpy(Hdr.fmag,ARFMAG,sizeof(ARFMAG));
301 //write header to archive file
302 ArchiveFile.write((char*)&Hdr, sizeof(Hdr));
304 //open Member file for reading and copy to buffer
305 int FD = open(Member.c_str(),O_RDONLY);
307 std::cerr << "Error opening file!\n";
311 unsigned char *buf = (unsigned char*)mmap(0, Length,PROT_READ,
314 //check if mmap failed
315 if (buf == (unsigned char*)MAP_FAILED) {
316 std::cerr << "Error mmapping file!\n";
320 //write to archive file
321 ArchiveFile.write((char*)buf,Length);
323 // Unmmap the memberfile
324 munmap((char*)buf, Length);
326 std::cout << "Member File End: " << ArchiveFile.tellp() << "\n";
332 // CreateArchive - Generates archive with or without symbol table.
334 void CreateArchive() {
336 std::cerr << "Archive File: " << Archive << "\n";
338 //Create archive file for output.
339 std::ofstream ArchiveFile(Archive.c_str());
341 //Check for errors opening or creating archive file.
342 if(!ArchiveFile.is_open() || ArchiveFile.bad() ) {
343 std::cerr << "Error opening Archive File\n";
347 //Write magic string to archive.
348 ArchiveFile << ARMAG;
350 //If the '-s' option was specified, generate symbol table.
352 std::cout << "Symbol Table Start: " << ArchiveFile.tellp() << "\n";
353 if(!WriteSymbolTable(ArchiveFile)) {
354 std::cerr << "Error creating symbol table. Exiting program.";
357 std::cout << "Symbol Table End: " << ArchiveFile.tellp() << "\n";
359 //Loop over all member files, and add to the archive.
360 for(unsigned i=0; i < Members.size(); ++i) {
361 if(ArchiveFile.tellp() % 2 != 0)
362 ArchiveFile << ARFMAG;
363 if(AddMemberToArchive(Members[i],ArchiveFile) != true) {
364 std::cerr << "Error adding " << Members[i] << "to archive. Exiting program.\n";
369 //Close archive file.
373 //Print out usage for errors in command line
375 std::cout << "USAGE: ar [-X32_64] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file [files..]\n\n";
377 std::cout << "commands:\n" <<
378 "d - delete file(s) from the archive\n"
379 << "m[ab] - move file(s) in the archive\n"
380 << "p - print file(s) found in the archive\n"
381 << "q[f] - quick append file(s) to the archive\n"
382 << "r[ab][f][u] - replace existing or insert new file(s) into the archive\n"
383 << "t - display contents of archive\n"
384 << "x[o] - extract file(s) from the archive\n";
386 std::cout << "\ncommand specific modifiers:\n"
387 << "[a] - put file(s) after [member-name]\n"
388 << "[b] - put file(s) before [member-name] (same as [i])\n"
389 << "[N] - use instance [count] of name\n"
390 << "[f] - truncate inserted file names\n"
391 << "[P] - use full path names when matching\n"
392 << "[o] - preserve original dates\n"
393 << "[u] - only replace files that are newer than current archive contents\n";
395 std::cout << "generic modifiers:\n"
396 << "[c] - do not warn if the library had to be created\n"
397 << "[s] - create an archive index (cf. ranlib)\n"
398 << "[S] - do not build a symbol table\n"
399 << "[v] - be verbose\n"
400 << "[V] - display the version number\n";
406 void printVersion() {
407 std::cout << VERSION;
411 //Extract the memberfile name from the command line
413 if(RestofArgs.size() > 0) {
414 RelPos = RestofArgs[0];
415 RestofArgs.erase(RestofArgs.begin());
417 //Throw error if needed and not present
422 //Extract count from the command line
424 if(RestofArgs.size() > 0) {
425 Count = atoi(RestofArgs[0].c_str());
426 RestofArgs.erase(RestofArgs.begin());
428 //Throw error if needed and not present
433 //Get the Archive File Name from the command line
435 std::cerr << RestofArgs.size() << "\n";
436 if(RestofArgs.size() > 0) {
437 Archive = RestofArgs[0];
438 RestofArgs.erase(RestofArgs.begin());
440 //Throw error if needed and not present
446 //Copy over remaining items in RestofArgs to our Member File vector.
447 //This is just for clarity.
449 std::cerr << RestofArgs.size() << "\n";
450 if(RestofArgs.size() > 0)
451 Members = std::vector<string>(RestofArgs);
454 // Parse the operations and operation modifiers
455 // FIXME: Not all of these options has been implemented, but we still
456 // do all the command line parsing for them.
459 //Keep track of number of operations. We can only specify one
461 unsigned NumOperations = 0;
463 for(unsigned i=0; i<Options.size(); ++i) {
479 InsertWithReplacement = true;
501 TruncateNames = true;
514 OriginalDates = true;
539 //Check that only one operation has been specified
540 if(NumOperations > 1)
548 int main(int argc, char **argv) {
549 cl::ParseCommandLineOptions(argc, argv);
550 sys::PrintStackTraceOnErrorSignal();