2004-11-14 21:56:59 +00:00
|
|
|
//===-- ArchiveWriter.cpp - Write LLVM archive files ----------------------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-11-06 08:51:45 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-11-06 08:51:45 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-07-05 17:07:56 +00:00
|
|
|
// Builds up an LLVM archive file (.a) containing LLVM bitcode.
|
2004-11-06 08:51:45 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ArchiveInternals.h"
|
2007-05-06 06:18:07 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2008-04-01 04:26:46 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2004-11-14 21:56:59 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2005-04-21 16:15:19 +00:00
|
|
|
#include "llvm/System/Process.h"
|
2007-05-06 09:28:33 +00:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2004-11-06 08:51:45 +00:00
|
|
|
#include <fstream>
|
2006-11-28 22:49:32 +00:00
|
|
|
#include <ostream>
|
2004-11-14 21:56:59 +00:00
|
|
|
#include <iomanip>
|
2004-11-06 08:51:45 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Write an integer using variable bit rate encoding. This saves a few bytes
|
|
|
|
// per entry in the symbol table.
|
2008-05-13 00:00:25 +00:00
|
|
|
static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
|
2004-11-14 21:56:59 +00:00
|
|
|
while (1) {
|
|
|
|
if (num < 0x80) { // done?
|
|
|
|
ARFile << (unsigned char)num;
|
|
|
|
return;
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Nope, we are bigger than a character, output the next 7 bits and set the
|
|
|
|
// high bit to say that there is more coming...
|
|
|
|
ARFile << (unsigned char)(0x80 | ((unsigned char)num & 0x7F));
|
|
|
|
num >>= 7; // Shift out 7 bits now...
|
|
|
|
}
|
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Compute how many bytes are taken by a given VBR encoded value. This is needed
|
|
|
|
// to pre-compute the size of the symbol table.
|
2008-05-13 00:00:25 +00:00
|
|
|
static inline unsigned numVbrBytes(unsigned num) {
|
2004-11-16 06:47:30 +00:00
|
|
|
|
|
|
|
// Note that the following nested ifs are somewhat equivalent to a binary
|
|
|
|
// search. We split it in half by comparing against 2^14 first. This allows
|
2005-04-21 21:13:18 +00:00
|
|
|
// most reasonable values to be done in 2 comparisons instead of 1 for
|
2004-11-16 06:47:30 +00:00
|
|
|
// small ones and four for large ones. We expect this to access file offsets
|
2005-04-21 21:13:18 +00:00
|
|
|
// in the 2^10 to 2^24 range and symbol lengths in the 2^0 to 2^8 range,
|
2004-11-16 06:47:30 +00:00
|
|
|
// so this approach is reasonable.
|
2008-02-20 11:10:28 +00:00
|
|
|
if (num < 1<<14) {
|
2004-11-16 06:47:30 +00:00
|
|
|
if (num < 1<<7)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 2;
|
2008-02-20 11:10:28 +00:00
|
|
|
}
|
2004-11-16 06:47:30 +00:00
|
|
|
if (num < 1<<21)
|
2004-11-14 21:56:59 +00:00
|
|
|
return 3;
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-16 06:47:30 +00:00
|
|
|
if (num < 1<<28)
|
|
|
|
return 4;
|
|
|
|
return 5; // anything >= 2^28 takes 5 bytes
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Create an empty archive.
|
2009-07-01 23:13:44 +00:00
|
|
|
Archive* Archive::CreateEmpty(const sys::Path& FilePath, LLVMContext& C) {
|
2009-07-01 16:58:40 +00:00
|
|
|
Archive* result = new Archive(FilePath, C);
|
2004-11-06 08:51:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// Fill the ArchiveMemberHeader with the information from a member. If
|
2004-11-16 06:47:30 +00:00
|
|
|
// TruncateNames is true, names are flattened to 15 chars or less. The sz field
|
2005-04-21 21:13:18 +00:00
|
|
|
// is provided here instead of coming from the mbr because the member might be
|
|
|
|
// stored compressed and the compressed size is not the ArchiveMember's size.
|
|
|
|
// Furthermore compressed files have negative size fields to identify them as
|
2004-11-16 06:47:30 +00:00
|
|
|
// compressed.
|
2004-11-14 21:56:59 +00:00
|
|
|
bool
|
|
|
|
Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
|
|
|
|
int sz, bool TruncateNames) const {
|
|
|
|
|
|
|
|
// Set the permissions mode, uid and gid
|
|
|
|
hdr.init();
|
|
|
|
char buffer[32];
|
|
|
|
sprintf(buffer, "%-8o", mbr.getMode());
|
|
|
|
memcpy(hdr.mode,buffer,8);
|
|
|
|
sprintf(buffer, "%-6u", mbr.getUser());
|
|
|
|
memcpy(hdr.uid,buffer,6);
|
|
|
|
sprintf(buffer, "%-6u", mbr.getGroup());
|
|
|
|
memcpy(hdr.gid,buffer,6);
|
|
|
|
|
|
|
|
// Set the last modification date
|
|
|
|
uint64_t secondsSinceEpoch = mbr.getModTime().toEpochTime();
|
|
|
|
sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
|
|
|
|
memcpy(hdr.date,buffer,12);
|
|
|
|
|
2004-11-17 18:28:29 +00:00
|
|
|
// Get rid of trailing blanks in the name
|
2004-12-11 00:14:15 +00:00
|
|
|
std::string mbrPath = mbr.getPath().toString();
|
2004-11-17 18:28:29 +00:00
|
|
|
size_t mbrLen = mbrPath.length();
|
|
|
|
while (mbrLen > 0 && mbrPath[mbrLen-1] == ' ') {
|
|
|
|
mbrPath.erase(mbrLen-1,1);
|
|
|
|
mbrLen--;
|
|
|
|
}
|
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Set the name field in one of its various flavors.
|
|
|
|
bool writeLongName = false;
|
|
|
|
if (mbr.isStringTable()) {
|
|
|
|
memcpy(hdr.name,ARFILE_STRTAB_NAME,16);
|
2004-11-20 07:29:40 +00:00
|
|
|
} else if (mbr.isSVR4SymbolTable()) {
|
|
|
|
memcpy(hdr.name,ARFILE_SVR4_SYMTAB_NAME,16);
|
|
|
|
} else if (mbr.isBSD4SymbolTable()) {
|
|
|
|
memcpy(hdr.name,ARFILE_BSD4_SYMTAB_NAME,16);
|
2004-11-14 21:56:59 +00:00
|
|
|
} else if (mbr.isLLVMSymbolTable()) {
|
|
|
|
memcpy(hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
|
|
|
|
} else if (TruncateNames) {
|
|
|
|
const char* nm = mbrPath.c_str();
|
|
|
|
unsigned len = mbrPath.length();
|
|
|
|
size_t slashpos = mbrPath.rfind('/');
|
|
|
|
if (slashpos != std::string::npos) {
|
|
|
|
nm += slashpos + 1;
|
|
|
|
len -= slashpos +1;
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
if (len > 15)
|
2004-11-14 21:56:59 +00:00
|
|
|
len = 15;
|
2004-11-16 06:47:30 +00:00
|
|
|
memcpy(hdr.name,nm,len);
|
2004-11-14 21:56:59 +00:00
|
|
|
hdr.name[len] = '/';
|
|
|
|
} else if (mbrPath.length() < 16 && mbrPath.find('/') == std::string::npos) {
|
2004-11-17 18:28:29 +00:00
|
|
|
memcpy(hdr.name,mbrPath.c_str(),mbrPath.length());
|
2004-11-14 21:56:59 +00:00
|
|
|
hdr.name[mbrPath.length()] = '/';
|
|
|
|
} else {
|
|
|
|
std::string nm = "#1/";
|
|
|
|
nm += utostr(mbrPath.length());
|
2004-11-17 18:28:29 +00:00
|
|
|
memcpy(hdr.name,nm.data(),nm.length());
|
2004-11-17 16:14:21 +00:00
|
|
|
if (sz < 0)
|
|
|
|
sz -= mbrPath.length();
|
|
|
|
else
|
|
|
|
sz += mbrPath.length();
|
2004-11-14 21:56:59 +00:00
|
|
|
writeLongName = true;
|
|
|
|
}
|
2004-11-17 16:14:21 +00:00
|
|
|
|
|
|
|
// Set the size field
|
|
|
|
if (sz < 0) {
|
|
|
|
buffer[0] = '-';
|
|
|
|
sprintf(&buffer[1],"%-9u",(unsigned)-sz);
|
|
|
|
} else {
|
|
|
|
sprintf(buffer, "%-10u", (unsigned)sz);
|
|
|
|
}
|
|
|
|
memcpy(hdr.size,buffer,10);
|
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
return writeLongName;
|
|
|
|
}
|
|
|
|
|
2004-11-16 06:47:30 +00:00
|
|
|
// Insert a file into the archive before some other member. This also takes care
|
|
|
|
// of extracting the necessary flags and information from the file.
|
2006-08-24 23:45:08 +00:00
|
|
|
bool
|
|
|
|
Archive::addFileBefore(const sys::Path& filePath, iterator where,
|
|
|
|
std::string* ErrMsg) {
|
2006-12-15 19:44:51 +00:00
|
|
|
if (!filePath.exists()) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Can not add a non-existent file to archive";
|
|
|
|
return true;
|
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
|
|
|
|
ArchiveMember* mbr = new ArchiveMember(this);
|
|
|
|
|
|
|
|
mbr->data = 0;
|
|
|
|
mbr->path = filePath;
|
2007-03-29 19:05:44 +00:00
|
|
|
const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
|
2009-06-11 08:09:49 +00:00
|
|
|
if (!FSInfo) {
|
|
|
|
delete mbr;
|
2006-08-24 23:45:08 +00:00
|
|
|
return true;
|
2009-06-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
mbr->info = *FSInfo;
|
2004-11-14 21:56:59 +00:00
|
|
|
|
|
|
|
unsigned flags = 0;
|
2004-12-11 00:14:15 +00:00
|
|
|
bool hasSlash = filePath.toString().find('/') != std::string::npos;
|
2004-11-14 21:56:59 +00:00
|
|
|
if (hasSlash)
|
|
|
|
flags |= ArchiveMember::HasPathFlag;
|
2004-12-11 00:14:15 +00:00
|
|
|
if (hasSlash || filePath.toString().length() > 15)
|
2004-11-14 21:56:59 +00:00
|
|
|
flags |= ArchiveMember::HasLongFilenameFlag;
|
|
|
|
std::string magic;
|
|
|
|
mbr->path.getMagicNumber(magic,4);
|
|
|
|
switch (sys::IdentifyFileType(magic.c_str(),4)) {
|
2007-05-06 06:18:07 +00:00
|
|
|
case sys::Bitcode_FileType:
|
2007-07-06 13:38:17 +00:00
|
|
|
flags |= ArchiveMember::BitcodeFlag;
|
2004-11-14 21:56:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mbr->flags = flags;
|
|
|
|
members.insert(where,mbr);
|
2006-08-24 23:45:08 +00:00
|
|
|
return false;
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-16 06:47:30 +00:00
|
|
|
// Write one member out to the file.
|
2006-07-07 19:09:14 +00:00
|
|
|
bool
|
2004-11-14 21:56:59 +00:00
|
|
|
Archive::writeMember(
|
|
|
|
const ArchiveMember& member,
|
|
|
|
std::ofstream& ARFile,
|
|
|
|
bool CreateSymbolTable,
|
|
|
|
bool TruncateNames,
|
2006-07-07 19:09:14 +00:00
|
|
|
bool ShouldCompress,
|
2006-08-24 23:45:08 +00:00
|
|
|
std::string* ErrMsg
|
2004-11-14 21:56:59 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
unsigned filepos = ARFile.tellp();
|
|
|
|
filepos -= 8;
|
|
|
|
|
|
|
|
// Get the data and its size either from the
|
|
|
|
// member's in-memory data or directly from the file.
|
|
|
|
size_t fSize = member.getSize();
|
2008-04-01 04:26:46 +00:00
|
|
|
const char *data = (const char*)member.getData();
|
|
|
|
MemoryBuffer *mFile = 0;
|
2004-11-14 21:56:59 +00:00
|
|
|
if (!data) {
|
2008-04-01 18:04:03 +00:00
|
|
|
mFile = MemoryBuffer::getFile(member.getPath().c_str(), ErrMsg);
|
2008-04-01 04:26:46 +00:00
|
|
|
if (mFile == 0)
|
2006-08-24 23:45:08 +00:00
|
|
|
return true;
|
2008-04-01 04:26:46 +00:00
|
|
|
data = mFile->getBufferStart();
|
|
|
|
fSize = mFile->getBufferSize();
|
2005-04-21 21:13:18 +00:00
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
// Now that we have the data in memory, update the
|
2007-07-05 17:07:56 +00:00
|
|
|
// symbol table if its a bitcode file.
|
2007-07-06 13:38:17 +00:00
|
|
|
if (CreateSymbolTable && member.isBitcode()) {
|
2004-11-14 21:56:59 +00:00
|
|
|
std::vector<std::string> symbols;
|
2005-04-21 21:13:18 +00:00
|
|
|
std::string FullMemberName = archPath.toString() + "(" +
|
|
|
|
member.getPath().toString()
|
2004-11-17 18:28:29 +00:00
|
|
|
+ ")";
|
2007-02-07 21:41:02 +00:00
|
|
|
ModuleProvider* MP =
|
2007-07-05 17:07:56 +00:00
|
|
|
GetBitcodeSymbols((const unsigned char*)data,fSize,
|
2009-07-01 16:58:40 +00:00
|
|
|
FullMemberName, Context, symbols, ErrMsg);
|
2004-11-15 01:20:11 +00:00
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
// If the bitcode parsed successfully
|
2004-11-15 01:20:11 +00:00
|
|
|
if ( MP ) {
|
2005-04-21 21:13:18 +00:00
|
|
|
for (std::vector<std::string>::iterator SI = symbols.begin(),
|
2004-11-15 01:20:11 +00:00
|
|
|
SE = symbols.end(); SI != SE; ++SI) {
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
std::pair<SymTabType::iterator,bool> Res =
|
2004-11-15 01:20:11 +00:00
|
|
|
symTab.insert(std::make_pair(*SI,filepos));
|
|
|
|
|
|
|
|
if (Res.second) {
|
2005-04-21 21:13:18 +00:00
|
|
|
symTabSize += SI->length() +
|
|
|
|
numVbrBytes(SI->length()) +
|
2004-11-15 01:20:11 +00:00
|
|
|
numVbrBytes(filepos);
|
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2004-11-15 01:20:11 +00:00
|
|
|
// We don't need this module any more.
|
|
|
|
delete MP;
|
|
|
|
} else {
|
2008-04-01 04:26:46 +00:00
|
|
|
delete mFile;
|
2006-08-24 23:45:08 +00:00
|
|
|
if (ErrMsg)
|
2007-07-05 17:07:56 +00:00
|
|
|
*ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
|
2006-08-25 17:43:11 +00:00
|
|
|
+ ": " + *ErrMsg;
|
2006-08-24 23:45:08 +00:00
|
|
|
return true;
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2007-05-06 06:18:07 +00:00
|
|
|
int hdrSize = fSize;
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Compute the fields of the header
|
|
|
|
ArchiveMemberHeader Hdr;
|
|
|
|
bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames);
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Write header to archive file
|
|
|
|
ARFile.write((char*)&Hdr, sizeof(Hdr));
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Write the long filename if its long
|
|
|
|
if (writeLongName) {
|
2004-12-11 00:14:15 +00:00
|
|
|
ARFile.write(member.getPath().toString().data(),
|
|
|
|
member.getPath().toString().length());
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Write the (possibly compressed) member's content to the file.
|
|
|
|
ARFile.write(data,fSize);
|
|
|
|
|
|
|
|
// Make sure the member is an even length
|
2004-12-20 03:23:46 +00:00
|
|
|
if ((ARFile.tellp() & 1) == 1)
|
2004-11-14 21:56:59 +00:00
|
|
|
ARFile << ARFILE_PAD;
|
|
|
|
|
|
|
|
// Close the mapped file if it was opened
|
2008-04-01 04:26:46 +00:00
|
|
|
delete mFile;
|
2006-08-24 23:45:08 +00:00
|
|
|
return false;
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-16 06:47:30 +00:00
|
|
|
// Write out the LLVM symbol table as an archive member to the file.
|
2004-11-06 08:51:45 +00:00
|
|
|
void
|
2004-11-16 06:47:30 +00:00
|
|
|
Archive::writeSymbolTable(std::ofstream& ARFile) {
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Construct the symbol table's header
|
2004-11-06 08:51:45 +00:00
|
|
|
ArchiveMemberHeader Hdr;
|
|
|
|
Hdr.init();
|
2004-11-14 21:56:59 +00:00
|
|
|
memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
|
|
|
|
uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
|
|
|
|
char buffer[32];
|
2005-04-20 03:55:35 +00:00
|
|
|
sprintf(buffer, "%-8o", 0644);
|
|
|
|
memcpy(Hdr.mode,buffer,8);
|
2005-04-21 16:15:19 +00:00
|
|
|
sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId());
|
2005-04-20 03:55:35 +00:00
|
|
|
memcpy(Hdr.uid,buffer,6);
|
2005-04-21 16:15:19 +00:00
|
|
|
sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId());
|
2005-04-20 03:55:35 +00:00
|
|
|
memcpy(Hdr.gid,buffer,6);
|
2004-11-14 21:56:59 +00:00
|
|
|
sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
|
|
|
|
memcpy(Hdr.date,buffer,12);
|
|
|
|
sprintf(buffer,"%-10u",symTabSize);
|
|
|
|
memcpy(Hdr.size,buffer,10);
|
|
|
|
|
|
|
|
// Write the header
|
|
|
|
ARFile.write((char*)&Hdr, sizeof(Hdr));
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2008-11-21 20:00:59 +00:00
|
|
|
#ifndef NDEBUG
|
2004-11-14 21:56:59 +00:00
|
|
|
// Save the starting position of the symbol tables data content.
|
|
|
|
unsigned startpos = ARFile.tellp();
|
2008-11-21 20:00:59 +00:00
|
|
|
#endif
|
2004-11-14 21:56:59 +00:00
|
|
|
|
|
|
|
// Write out the symbols sequentially
|
|
|
|
for ( Archive::SymTabType::iterator I = symTab.begin(), E = symTab.end();
|
|
|
|
I != E; ++I)
|
|
|
|
{
|
|
|
|
// Write out the file index
|
|
|
|
writeInteger(I->second, ARFile);
|
|
|
|
// Write out the length of the symbol
|
|
|
|
writeInteger(I->first.length(), ARFile);
|
|
|
|
// Write out the symbol
|
|
|
|
ARFile.write(I->first.data(), I->first.length());
|
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2008-11-21 20:00:59 +00:00
|
|
|
#ifndef NDEBUG
|
2004-11-14 21:56:59 +00:00
|
|
|
// Now that we're done with the symbol table, get the ending file position
|
|
|
|
unsigned endpos = ARFile.tellp();
|
2008-11-21 20:00:59 +00:00
|
|
|
#endif
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Make sure that the amount we wrote is what we pre-computed. This is
|
|
|
|
// critical for file integrity purposes.
|
|
|
|
assert(endpos - startpos == symTabSize && "Invalid symTabSize computation");
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2004-11-14 21:56:59 +00:00
|
|
|
// Make sure the symbol table is even sized
|
|
|
|
if (symTabSize % 2 != 0 )
|
|
|
|
ARFile << ARFILE_PAD;
|
2004-11-06 08:51:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-16 06:47:30 +00:00
|
|
|
// Write the entire archive to the file specified when the archive was created.
|
2005-04-21 21:13:18 +00:00
|
|
|
// This writes to a temporary file first. Options are for creating a symbol
|
|
|
|
// table, flattening the file names (no directories, 15 chars max) and
|
2004-11-16 06:47:30 +00:00
|
|
|
// compressing each archive member.
|
2006-07-07 19:09:14 +00:00
|
|
|
bool
|
|
|
|
Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
|
2006-08-24 23:45:08 +00:00
|
|
|
std::string* ErrMsg)
|
2006-07-07 19:09:14 +00:00
|
|
|
{
|
2004-11-14 21:56:59 +00:00
|
|
|
// Make sure they haven't opened up the file, not loaded it,
|
|
|
|
// but are now trying to write it which would wipe out the file.
|
2008-04-01 04:26:46 +00:00
|
|
|
if (members.empty() && mapfile && mapfile->getBufferSize() > 8) {
|
2006-12-15 19:44:51 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Can't write an archive not opened for writing";
|
|
|
|
return true;
|
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
|
|
|
|
// Create a temporary file to store the archive in
|
|
|
|
sys::Path TmpArchive = archPath;
|
2006-08-24 23:45:08 +00:00
|
|
|
if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
|
|
|
|
return true;
|
2004-11-14 21:56:59 +00:00
|
|
|
|
|
|
|
// Make sure the temporary gets removed if we crash
|
|
|
|
sys::RemoveFileOnSignal(TmpArchive);
|
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Create archive file for output.
|
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
|
|
|
|
|
|
|
|
// Check for errors opening or creating archive file.
|
2006-07-28 22:29:50 +00:00
|
|
|
if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
|
2006-07-07 19:09:14 +00:00
|
|
|
if (TmpArchive.exists())
|
|
|
|
TmpArchive.eraseFromDisk();
|
2006-08-24 23:45:08 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Error opening archive file: " + archPath.toString();
|
|
|
|
return true;
|
2006-07-07 19:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're creating a symbol table, reset it now
|
|
|
|
if (CreateSymbolTable) {
|
|
|
|
symTabSize = 0;
|
|
|
|
symTab.clear();
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Write magic string to archive.
|
|
|
|
ArchiveFile << ARFILE_MAGIC;
|
|
|
|
|
|
|
|
// Loop over all member files, and write them out. Note that this also
|
|
|
|
// builds the symbol table, symTab.
|
2006-07-28 22:29:50 +00:00
|
|
|
for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
|
2006-08-24 23:45:08 +00:00
|
|
|
if (writeMember(*I, ArchiveFile, CreateSymbolTable,
|
|
|
|
TruncateNames, Compress, ErrMsg)) {
|
2006-07-07 19:09:14 +00:00
|
|
|
if (TmpArchive.exists())
|
|
|
|
TmpArchive.eraseFromDisk();
|
|
|
|
ArchiveFile.close();
|
2006-08-24 23:45:08 +00:00
|
|
|
return true;
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2006-07-07 19:09:14 +00:00
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Close archive file.
|
|
|
|
ArchiveFile.close();
|
|
|
|
|
|
|
|
// Write the symbol table
|
|
|
|
if (CreateSymbolTable) {
|
|
|
|
// At this point we have written a file that is a legal archive but it
|
|
|
|
// doesn't have a symbol table in it. To aid in faster reading and to
|
|
|
|
// ensure compatibility with other archivers we need to put the symbol
|
|
|
|
// table first in the file. Unfortunately, this means mapping the file
|
|
|
|
// we just wrote back in and copying it to the destination file.
|
2008-04-01 04:26:46 +00:00
|
|
|
sys::Path FinalFilePath = archPath;
|
2006-07-07 19:09:14 +00:00
|
|
|
|
|
|
|
// Map in the archive we just wrote.
|
2008-04-01 04:26:46 +00:00
|
|
|
{
|
2008-04-01 18:04:03 +00:00
|
|
|
OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str()));
|
2008-04-01 04:26:46 +00:00
|
|
|
if (arch == 0) return true;
|
|
|
|
const char* base = arch->getBufferStart();
|
2006-07-07 19:09:14 +00:00
|
|
|
|
|
|
|
// Open another temporary file in order to avoid invalidating the
|
|
|
|
// mmapped data
|
2006-08-24 23:45:08 +00:00
|
|
|
if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
|
|
|
|
return true;
|
2006-07-07 19:09:14 +00:00
|
|
|
sys::RemoveFileOnSignal(FinalFilePath);
|
|
|
|
|
|
|
|
std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
|
2006-07-28 22:29:50 +00:00
|
|
|
if (!FinalFile.is_open() || FinalFile.bad()) {
|
2006-07-07 19:09:14 +00:00
|
|
|
if (TmpArchive.exists())
|
|
|
|
TmpArchive.eraseFromDisk();
|
2006-08-24 23:45:08 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Error opening archive file: " + FinalFilePath.toString();
|
|
|
|
return true;
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Write the file magic number
|
|
|
|
FinalFile << ARFILE_MAGIC;
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// If there is a foreign symbol table, put it into the file now. Most
|
|
|
|
// ar(1) implementations require the symbol table to be first but llvm-ar
|
|
|
|
// can deal with it being after a foreign symbol table. This ensures
|
|
|
|
// compatibility with other ar(1) implementations as well as allowing the
|
|
|
|
// archive to store both native .o and LLVM .bc files, both indexed.
|
|
|
|
if (foreignST) {
|
2006-08-24 23:45:08 +00:00
|
|
|
if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
|
2006-07-07 20:56:50 +00:00
|
|
|
FinalFile.close();
|
|
|
|
if (TmpArchive.exists())
|
|
|
|
TmpArchive.eraseFromDisk();
|
2006-08-24 23:45:08 +00:00
|
|
|
return true;
|
2006-07-07 20:56:50 +00:00
|
|
|
}
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2004-11-06 08:51:45 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Put out the LLVM symbol table now.
|
|
|
|
writeSymbolTable(FinalFile);
|
|
|
|
|
|
|
|
// Copy the temporary file contents being sure to skip the file's magic
|
|
|
|
// number.
|
|
|
|
FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
|
2008-04-01 04:26:46 +00:00
|
|
|
arch->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
|
2006-07-07 19:09:14 +00:00
|
|
|
|
|
|
|
// Close up shop
|
|
|
|
FinalFile.close();
|
2008-04-01 04:26:46 +00:00
|
|
|
} // free arch.
|
2005-11-30 05:21:10 +00:00
|
|
|
|
2006-07-07 19:09:14 +00:00
|
|
|
// Move the final file over top of TmpArchive
|
2006-08-24 23:45:08 +00:00
|
|
|
if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
|
|
|
|
return true;
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|
2006-07-07 19:09:14 +00:00
|
|
|
|
|
|
|
// Before we replace the actual archive, we need to forget all the
|
|
|
|
// members, since they point to data in that old archive. We need to do
|
|
|
|
// this because we cannot replace an open file on Windows.
|
|
|
|
cleanUpMemory();
|
|
|
|
|
2006-08-24 23:45:08 +00:00
|
|
|
if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
|
|
|
|
return true;
|
2006-07-07 19:09:14 +00:00
|
|
|
|
2008-05-24 05:42:29 +00:00
|
|
|
// Set correct read and write permissions after temporary file is moved
|
|
|
|
// to final destination path.
|
|
|
|
if (archPath.makeReadableOnDisk(ErrMsg))
|
|
|
|
return true;
|
|
|
|
if (archPath.makeWriteableOnDisk(ErrMsg))
|
|
|
|
return true;
|
|
|
|
|
2006-08-24 23:45:08 +00:00
|
|
|
return false;
|
2004-11-14 21:56:59 +00:00
|
|
|
}
|