1 //===----------------------------------------------------------------------===//
4 // This utility may be invoked in the following manner:
5 // llvm-ar archivename files..
7 //===----------------------------------------------------------------------===//
8 #include "Support/CommandLine.h"
9 #include "llvm/Bytecode/Reader.h"
10 #include "llvm/Module.h"
18 #include <sys/types.h>
29 #define ARFMAG "\n" /* header trailer string */
30 #define ARMAG "!<arch>\n" /* magic string */
31 #define SARMAG 8 /* length of magic string */
35 // Each file member is preceded by a file member header. Which is
36 // of the following format:
38 // char ar_name[16] - '/' terminated file member name.
39 // If the file name does not fit, a dummy name is used.
40 // char ar_date[12] - file date in decimal
41 // char ar_uid[6] - User id of file owner in decimal.
42 // char ar_gid[6] - Group ID file belongs to in decimal.
43 // char ar_mode[8] - File mode in octal.
44 // char ar_size[10] - Size of file in decimal.
45 // char ar_fmag[2] - Trailer of header file, a newline.
66 //Option to generate symbol table or not
67 //running llvm-ar -s is the same as ranlib
68 cl::opt<bool> SymbolTable ("s", cl::desc("Generate an archive symbol table"));
71 cl::opt<string> Archive (cl::Positional, cl::desc("<archive file>"),
74 //For now we require one or more member files, this should change so
75 //we can just run llvm-ar -s on an archive to generate the symbol
77 cl::list<string> Members(cl::ConsumeAfter, cl::desc("<archive members>..."));
80 static inline bool Error(std::string *ErrorStr, const char *Message) {
81 if (ErrorStr) *ErrorStr = Message;
86 // WriteSymbolTable - Writes symbol table to ArchiveFile, return false
87 // on errors. Also returns by reference size of symbol table.
89 // Overview of method:
90 // 1) Generate the header for the symbol table. This is a normal
91 // archive member header, but it has a zero length name.
92 // 2) For each archive member file, stat the file and parse the bytecode
93 // Store cummulative offset (file size + header size).
94 // 3) Loop over all the symbols for the current member file,
95 // add offset entry to offset vector, and add symbol name to its vector.
96 // Note: The symbol name vector is a vector of chars to speed up calculating
97 // the total size of the symbol table.
98 // 4) Update offset vector once we know the total size of symbol table. This is
99 // because the symbol table appears before all archive member file contents.
100 // We add the size of magic string, and size of symbol table to each offset.
101 // 5) If the new updated offset it not even, we add 1 byte to offset because
102 // a newline will be inserted when writing member files. This adjustment is
103 // cummulative (ie. each time we have an odd offset we add 1 to total adjustment).
104 // 6) Lastly, write symbol table to file.
106 bool WriteSymbolTable(std::ofstream &ArchiveFile) {
108 //Create header for symbol table. This is essentially an empty header with the
109 //name set to a '/' to indicate its a symbol table.
113 //Name of symbol table is '/'
117 //Set the header trailer to a newline
118 memcpy(Hdr.fmag,ARFMAG,sizeof(ARFMAG));
121 //Write header to archive file
122 ArchiveFile.write((char*)&Hdr, sizeof(Hdr));
125 unsigned memoff = 0; //Keep Track of total size of files added to archive
126 vector<unsigned> offsets; //Vector of offsets into archive file
127 vector<char> names; //Vector of characters that are the symbol names.
129 //Loop over archive member files, parse bytecode, and generate symbol table.
130 for(unsigned i=0; i<Members.size(); ++i) {
132 //Open Member file for reading and copy to buffer
133 int FD = open(Members[i].c_str(),O_RDONLY);
135 //Check for errors opening the file.
137 std::cerr << "Error opening file!\n";
141 //Stat the file to get its size.
143 if (stat(Members[i].c_str(), &StatBuf) == -1 || StatBuf.st_size == 0) {
144 std::cerr << "Error stating file\n";
149 unsigned Length = StatBuf.st_size;
151 //Read in file into a buffer.
152 unsigned char *buf = (unsigned char*)mmap(0, Length,PROT_READ,
155 //Check if mmap failed.
156 if (buf == (unsigned char*)MAP_FAILED) {
157 std::cerr << "Error mmapping file!\n";
161 //Parse the bytecode file and get all the symbols.
163 Module *M = ParseBytecodeBuffer(buf,Length,Members[i],&ErrorStr);
165 //Check for errors parsing bytecode.
167 //std::cerr << "Error Parsing Bytecode\n";
171 //Loop over function names and global vars and add to symbol maps
172 for(Module::iterator I = M->begin(), E=M->end(); I != E; ++I) {
175 string NM = ((Function*)I)->getName();
177 //Loop over the characters in the name and add to symbol name vector
178 for(unsigned i=0; i<NM.size(); ++i)
179 names.push_back(NM[i]);
181 //Each symbol is null terminated.
182 names.push_back('\0');
184 //Add offset to vector of offsets
185 offsets.push_back(memoff);
188 memoff += Length + sizeof(Hdr);
191 //Determine how large our symbol table is.
192 unsigned symbolTableSize = sizeof(Hdr) + 4 + 4*(offsets.size()) + names.size();
193 cout << "Symbol Table Size: " << symbolTableSize << "\n";
195 //Number of symbols should be in network byte order as well
197 unsigned temp = offsets.size();
198 num[0] = (temp >> 24) & 255;
199 num[1] = (temp >> 16) & 255;
200 num[2] = (temp >> 8) & 255;
203 //Write number of symbols to archive file
204 ArchiveFile.write(num,4);
206 //Adjustment to offset to start files on even byte boundaries
209 //Update offsets write symbol tabel to archive.
210 for(unsigned i=0; i<offsets.size(); ++i) {
212 offsets[i] = offsets[i] + symbolTableSize + SARMAG;
213 offsets[i] += adjust;
214 if((offsets[i] % 2 != 0)) {
216 offsets[i] += adjust;
219 cout << "Offset: " << offsets[i] << "\n";
220 output[0] = (offsets[i] >> 24) & 255;
221 output[1] = (offsets[i] >> 16) & 255;
222 output[2] = (offsets[i] >> 8) & 255;
223 output[3] = offsets[i] & 255;
224 ArchiveFile.write(output,4);
228 //Write out symbol name vector.
229 for(unsigned i=0; i<names.size(); ++i)
230 ArchiveFile << names[i];
235 // AddMemberToArchive - Writes member file to archive. Returns false on errors.
237 // Overview of method:
238 // 1) Open file, and stat it.
239 // 2) Fill out header using stat information. If name is longer then 15
240 // characters, use "dummy" name.
241 // 3) Write header and file contents to disk.
242 // 4) Keep track of total offset into file, and insert a newline if it is odd.
244 bool AddMemberToArchive(string Member, std::ofstream &ArchiveFile) {
246 ar_hdr Hdr; //Header for archive member file.
248 //stat the file to get info
250 if (stat(Member.c_str(), &StatBuf) == -1 || StatBuf.st_size == 0)
255 //set name to white spaces
256 memset(Hdr.name,' ', sizeof(Hdr.name));
258 //check the size of the name, if less than 15, we can copy it directly
259 //otherwise we give it a dummy name for now
260 if(Member.length() < 16)
261 memcpy(Hdr.name,Member.c_str(),Member.length());
263 memcpy(Hdr.name, "Dummy", 5);
265 //terminate name with forward slash
268 //file member size in decimal
269 unsigned Length = StatBuf.st_size;
270 sprintf(Hdr.size,"%d", Length);
271 cout << "Size: " << Length << "\n";
273 //file member user id in decimal
274 sprintf(Hdr.uid, "%d", StatBuf.st_uid);
276 //file member group id in decimal
277 sprintf(Hdr.gid, "%d", StatBuf.st_gid);
279 //file member date in decimal
280 sprintf(Hdr.date,"%d", (int)StatBuf.st_mtime);
282 //file member mode in OCTAL
283 sprintf(Hdr.mode,"%d", StatBuf.st_mode);
285 //add our header trailer
286 memcpy(Hdr.fmag,ARFMAG,sizeof(ARFMAG));
288 //write header to archive file
289 ArchiveFile.write((char*)&Hdr, sizeof(Hdr));
291 //open Member file for reading and copy to buffer
292 int FD = open(Member.c_str(),O_RDONLY);
294 std::cerr << "Error opening file!\n";
298 unsigned char *buf = (unsigned char*)mmap(0, Length,PROT_READ,
301 //check if mmap failed
302 if (buf == (unsigned char*)MAP_FAILED) {
303 std::cerr << "Error mmapping file!\n";
307 //write to archive file
308 ArchiveFile.write((char*)buf,Length);
310 // Unmmap the memberfile
311 munmap((char*)buf, Length);
317 // CreateArchive - Generates archive with or without symbol table.
319 void CreateArchive() {
321 //Create archive file for output.
322 std::ofstream ArchiveFile(Archive.c_str());
324 //Check for errors opening or creating archive file.
325 if(!ArchiveFile.is_open() || ArchiveFile.bad() ) {
326 std::cerr << "Error opening Archive File\n";
330 //Write magic string to archive.
331 ArchiveFile << ARMAG;
333 //If the '-s' option was specified, generate symbol table.
335 cout << "Symbol Table Start: " << ArchiveFile.tellp() << "\n";
336 if(!WriteSymbolTable(ArchiveFile)) {
337 std::cerr << "Error creating symbol table. Exiting program.";
340 cout << "Symbol Table End: " << ArchiveFile.tellp() << "\n";
342 //Loop over all member files, and add to the archive.
343 for(unsigned i=0; i<Members.size(); ++i) {
344 if(ArchiveFile.tellp() % 2 != 0)
345 ArchiveFile << ARFMAG;
347 cout << "Member File Start: " << ArchiveFile.tellp() << "\n";
349 if(AddMemberToArchive(Members[i],ArchiveFile) != true) {
350 std::cerr << "Error adding file to archive. Exiting program.";
353 cout << "Member File End: " << ArchiveFile.tellp() << "\n";
356 //Close archive file.
361 int main(int argc, char **argv) {
363 //Parse Command line options
364 cl::ParseCommandLineOptions(argc, argv, " llvm-ar\n");