1 //===-- ArchiveWriter.cpp - Write LLVM archive files ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Builds up an LLVM archive file (.a) containing LLVM bytecode.
12 //===----------------------------------------------------------------------===//
14 #include "ArchiveInternals.h"
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Support/FileUtilities.h"
17 #include "llvm/Support/Compressor.h"
18 #include "llvm/System/Signals.h"
27 // Write an integer using variable bit rate encoding. This saves a few bytes
28 // per entry in the symbol table.
29 inline void writeInteger(unsigned num, std::ofstream& ARFile) {
31 if (num < 0x80) { // done?
32 ARFile << (unsigned char)num;
36 // Nope, we are bigger than a character, output the next 7 bits and set the
37 // high bit to say that there is more coming...
38 ARFile << (unsigned char)(0x80 | ((unsigned char)num & 0x7F));
39 num >>= 7; // Shift out 7 bits now...
43 // Compute how many bytes are taken by a given VBR encoded value. This is needed
44 // to pre-compute the size of the symbol table.
45 inline unsigned numVbrBytes(unsigned num) {
48 if (num < 16384) // 2^14
50 if (num < 2097152) // 2^21
52 if (num < 268435456) // 2^28
54 return 5; // anything >= 2^28 takes 5 bytes
59 // Create an empty archive.
61 Archive::CreateEmpty(const sys::Path& FilePath ) {
62 Archive* result = new Archive(FilePath,false);
67 Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
68 int sz, bool TruncateNames) const {
70 // Set the permissions mode, uid and gid
73 sprintf(buffer, "%-8o", mbr.getMode());
74 memcpy(hdr.mode,buffer,8);
75 sprintf(buffer, "%-6u", mbr.getUser());
76 memcpy(hdr.uid,buffer,6);
77 sprintf(buffer, "%-6u", mbr.getGroup());
78 memcpy(hdr.gid,buffer,6);
83 sprintf(&buffer[1],"%-9u",(unsigned)-sz);
85 sprintf(buffer, "%-10u", (unsigned)sz);
87 memcpy(hdr.size,buffer,10);
89 // Set the last modification date
90 uint64_t secondsSinceEpoch = mbr.getModTime().toEpochTime();
91 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
92 memcpy(hdr.date,buffer,12);
94 // Set the name field in one of its various flavors.
95 bool writeLongName = false;
96 const std::string& mbrPath = mbr.getPath().get();
97 if (mbr.isStringTable()) {
98 memcpy(hdr.name,ARFILE_STRTAB_NAME,16);
99 } else if (mbr.isForeignSymbolTable()) {
100 memcpy(hdr.name,ARFILE_SYMTAB_NAME,16);
101 } else if (mbr.isLLVMSymbolTable()) {
102 memcpy(hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
103 } else if (TruncateNames) {
104 const char* nm = mbrPath.c_str();
105 unsigned len = mbrPath.length();
106 size_t slashpos = mbrPath.rfind('/');
107 if (slashpos != std::string::npos) {
113 mbrPath.copy(hdr.name,len);
115 } else if (mbrPath.length() < 16 && mbrPath.find('/') == std::string::npos) {
116 mbrPath.copy(hdr.name,mbrPath.length());
117 hdr.name[mbrPath.length()] = '/';
119 std::string nm = "#1/";
120 nm += utostr(mbrPath.length());
121 nm.copy(hdr.name,nm.length());
122 writeLongName = true;
124 return writeLongName;
128 Archive::addFileBefore(const sys::Path& filePath, iterator where) {
129 assert(filePath.exists() && "Can't add a non-existent file");
131 ArchiveMember* mbr = new ArchiveMember(this);
134 mbr->path = filePath;
135 mbr->path.getStatusInfo(mbr->info);
138 bool hasSlash = filePath.get().find('/') != std::string::npos;
140 flags |= ArchiveMember::HasPathFlag;
141 if (hasSlash || filePath.get().length() > 15)
142 flags |= ArchiveMember::HasLongFilenameFlag;
144 mbr->path.getMagicNumber(magic,4);
145 switch (sys::IdentifyFileType(magic.c_str(),4)) {
146 case sys::BytecodeFileType:
147 flags |= ArchiveMember::BytecodeFlag;
149 case sys::CompressedBytecodeFileType:
150 flags |= ArchiveMember::CompressedBytecodeFlag;
156 members.insert(where,mbr);
160 Archive::moveMemberBefore(iterator target, iterator where) {
161 assert(target != end() && "Target iterator for moveMemberBefore is invalid");
162 ArchiveMember* mbr = members.remove(target);
163 members.insert(where, mbr);
167 Archive::remove(iterator target) {
168 assert(target != end() && "Target iterator for remove is invalid");
169 ArchiveMember* mbr = members.remove(target);
173 Archive::writeMember(
174 const ArchiveMember& member,
175 std::ofstream& ARFile,
176 bool CreateSymbolTable,
181 unsigned filepos = ARFile.tellp();
184 // Get the data and its size either from the
185 // member's in-memory data or directly from the file.
186 size_t fSize = member.getSize();
187 const char* data = (const char*)member.getData();
188 sys::MappedFile* mFile = 0;
190 mFile = new sys::MappedFile(member.getPath());
191 data = (const char*) mFile->map();
192 fSize = mFile->size();
195 // Now that we have the data in memory, update the
196 // symbol table if its a bytecode file.
197 if (CreateSymbolTable &&
198 (member.isBytecode() || member.isCompressedBytecode())) {
199 std::vector<std::string> symbols;
200 ModuleProvider* MP = GetBytecodeSymbols(
201 (const unsigned char*)data,fSize,member.getPath().get(), symbols);
203 // If the bytecode parsed successfully
205 for (std::vector<std::string>::iterator SI = symbols.begin(),
206 SE = symbols.end(); SI != SE; ++SI) {
208 std::pair<SymTabType::iterator,bool> Res =
209 symTab.insert(std::make_pair(*SI,filepos));
212 symTabSize += SI->length() +
213 numVbrBytes(SI->length()) +
214 numVbrBytes(filepos);
217 // We don't need this module any more.
220 throw std::string("Can't parse bytecode member: ") +
221 member.getPath().get();
225 // Determine if we actually should compress this member
228 !member.isForeignSymbolTable() &&
229 !member.isLLVMSymbolTable() &&
230 !member.isCompressed() &&
231 !member.isCompressedBytecode());
233 // Perform the compression. Note that if the file is uncompressed bytecode
234 // then we turn the file into compressed bytecode rather than treating it as
235 // compressed data. This is necessary since it allows us to determine that the
236 // file contains bytecode instead of looking like a regular compressed data
237 // member. A compressed bytecode file has its content compressed but has a
238 // magic number of "llvc". This acounts for the +/-4 arithmetic in the code
243 if (member.isBytecode()) {
247 fSize = Compressor::compressToNewBuffer(
248 data,fSize,output,Compressor::COMP_TYPE_ZLIB);
250 if (member.isBytecode())
258 // Compute the fields of the header
259 ArchiveMemberHeader Hdr;
260 bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames);
262 // Write header to archive file
263 ARFile.write((char*)&Hdr, sizeof(Hdr));
265 // Write the long filename if its long
267 ARFile << member.getPath().c_str();
271 // Make sure we write the compressed bytecode magic number if we should.
272 if (willCompress && member.isBytecode())
273 ARFile.write("llvc",4);
275 // Write the (possibly compressed) member's content to the file.
276 ARFile.write(data,fSize);
278 // Make sure the member is an even length
279 if (ARFile.tellp() % 2 != 0)
280 ARFile << ARFILE_PAD;
282 // Free the compressed data, if necessary
287 // Close the mapped file if it was opened
295 Archive::writeSymbolTable(std::ofstream& ARFile,bool PrintSymTab ) {
297 // Construct the symbol table's header
298 ArchiveMemberHeader Hdr;
300 memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
301 uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
303 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
304 memcpy(Hdr.date,buffer,12);
305 sprintf(buffer,"%-10u",symTabSize);
306 memcpy(Hdr.size,buffer,10);
309 ARFile.write((char*)&Hdr, sizeof(Hdr));
311 // Save the starting position of the symbol tables data content.
312 unsigned startpos = ARFile.tellp();
314 // Print the symbol table header if we're supposed to
316 std::cout << "Symbol Table:\n";
318 // Write out the symbols sequentially
319 for ( Archive::SymTabType::iterator I = symTab.begin(), E = symTab.end();
322 // Write out the file index
323 writeInteger(I->second, ARFile);
324 // Write out the length of the symbol
325 writeInteger(I->first.length(), ARFile);
326 // Write out the symbol
327 ARFile.write(I->first.data(), I->first.length());
329 // Print this entry to std::cout if we should
331 unsigned filepos = I->second + symTabSize + sizeof(ArchiveMemberHeader) +
332 (symTabSize % 2 != 0) + 8;
333 std::cout << " " << std::setw(9) << filepos << "\t" << I->first << "\n";
337 // Now that we're done with the symbol table, get the ending file position
338 unsigned endpos = ARFile.tellp();
340 // Make sure that the amount we wrote is what we pre-computed. This is
341 // critical for file integrity purposes.
342 assert(endpos - startpos == symTabSize && "Invalid symTabSize computation");
344 // Make sure the symbol table is even sized
345 if (symTabSize % 2 != 0 )
346 ARFile << ARFILE_PAD;
350 Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames,
351 bool Compress, bool PrintSymTab) {
353 // Make sure they haven't opened up the file, not loaded it,
354 // but are now trying to write it which would wipe out the file.
355 assert(!(members.empty() && mapfile->size() > 8));
357 // Create a temporary file to store the archive in
358 sys::Path TmpArchive = archPath;
359 TmpArchive.createTemporaryFile();
361 // Make sure the temporary gets removed if we crash
362 sys::RemoveFileOnSignal(TmpArchive);
364 // Ensure we can remove the temporary even in the face of an exception
366 // Create archive file for output.
367 std::ofstream ArchiveFile(TmpArchive.c_str());
369 // Check for errors opening or creating archive file.
370 if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
371 throw std::string("Error opening archive file: ") + archPath.get();
374 // If we're creating a symbol table, reset it now
375 if (CreateSymbolTable) {
380 // Write magic string to archive.
381 ArchiveFile << ARFILE_MAGIC;
383 // Loop over all member files, and write them out. Note that this also
384 // builds the symbol table, symTab.
385 for ( MembersList::iterator I = begin(), E = end(); I != E; ++I) {
386 writeMember(*I,ArchiveFile,CreateSymbolTable,TruncateNames,Compress);
389 // Close archive file.
392 // Write the symbol table
393 if (CreateSymbolTable) {
394 // At this point we have written a file that is a legal archive but it
395 // doesn't have a symbol table in it. To aid in faster reading and to
396 // ensure compatibility with other archivers we need to put the symbol
397 // table first in the file. Unfortunately, this means mapping the file
398 // we just wrote back in and copying it to the destination file.
399 sys::MappedFile arch(TmpArchive);
400 const char* base = (const char*) arch.map();
402 // Open the final file to write and check it.
403 std::ofstream FinalFile(archPath.c_str());
404 if ( !FinalFile.is_open() || FinalFile.bad() ) {
405 throw std::string("Error opening archive file: ") + archPath.get();
408 // Write the file magic number
409 FinalFile << ARFILE_MAGIC;
411 // Put out the symbol table
412 writeSymbolTable(FinalFile,PrintSymTab);
414 // Copy the temporary file contents being sure to skip the file's magic
416 FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
417 arch.size()-sizeof(ARFILE_MAGIC)+1);
422 TmpArchive.destroyFile();
425 // We don't have to insert the symbol table, so just renaming the temp
426 // file to the correct name will suffice.
427 TmpArchive.renameFile(archPath);
430 // Make sure we clean up.
431 if (TmpArchive.exists())
432 TmpArchive.destroyFile();