//
//===----------------------------------------------------------------------===//
//
-// Builds up an LLVM archive file (.a) containing LLVM bytecode.
+// Builds up an LLVM archive file (.a) containing LLVM bitcode.
//
//===----------------------------------------------------------------------===//
#include "ArchiveInternals.h"
-#include "llvm/Bytecode/Reader.h"
-#include "llvm/Support/Compressor.h"
+#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/System/Signals.h"
#include "llvm/System/Process.h"
+#include "llvm/ModuleProvider.h"
#include <fstream>
-#include <iostream>
+#include <ostream>
#include <iomanip>
-
using namespace llvm;
// Write an integer using variable bit rate encoding. This saves a few bytes
// Create an empty archive.
Archive*
Archive::CreateEmpty(const sys::Path& FilePath ) {
- Archive* result = new Archive(FilePath,false);
+ Archive* result = new Archive(FilePath);
return result;
}
// Insert a file into the archive before some other member. This also takes care
// of extracting the necessary flags and information from the file.
-void
-Archive::addFileBefore(const sys::Path& filePath, iterator where) {
- assert(filePath.exists() && "Can't add a non-existent file");
+bool
+Archive::addFileBefore(const sys::Path& filePath, iterator where,
+ std::string* ErrMsg) {
+ if (!filePath.exists()) {
+ if (ErrMsg)
+ *ErrMsg = "Can not add a non-existent file to archive";
+ return true;
+ }
ArchiveMember* mbr = new ArchiveMember(this);
mbr->data = 0;
mbr->path = filePath;
- mbr->path.getStatusInfo(mbr->info);
+ const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
+ if (FSInfo)
+ mbr->info = *FSInfo;
+ else
+ return true;
unsigned flags = 0;
bool hasSlash = filePath.toString().find('/') != std::string::npos;
std::string magic;
mbr->path.getMagicNumber(magic,4);
switch (sys::IdentifyFileType(magic.c_str(),4)) {
- case sys::BytecodeFileType:
- flags |= ArchiveMember::BytecodeFlag;
- break;
- case sys::CompressedBytecodeFileType:
- flags |= ArchiveMember::CompressedBytecodeFlag;
+ case sys::Bitcode_FileType:
+ flags |= ArchiveMember::BitcodeFlag;
break;
default:
break;
}
mbr->flags = flags;
members.insert(where,mbr);
+ return false;
}
// Write one member out to the file.
-void
+bool
Archive::writeMember(
const ArchiveMember& member,
std::ofstream& ARFile,
bool CreateSymbolTable,
bool TruncateNames,
- bool ShouldCompress
+ bool ShouldCompress,
+ std::string* ErrMsg
) {
unsigned filepos = ARFile.tellp();
const char* data = (const char*)member.getData();
sys::MappedFile* mFile = 0;
if (!data) {
- mFile = new sys::MappedFile(member.getPath());
- data = (const char*) mFile->map();
+ mFile = new sys::MappedFile();
+ if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
+ return true;
+ if (!(data = (const char*) mFile->map(ErrMsg)))
+ return true;
fSize = mFile->size();
}
// Now that we have the data in memory, update the
- // symbol table if its a bytecode file.
- if (CreateSymbolTable &&
- (member.isBytecode() || member.isCompressedBytecode())) {
+ // symbol table if its a bitcode file.
+ if (CreateSymbolTable && member.isBitcode()) {
std::vector<std::string> symbols;
std::string FullMemberName = archPath.toString() + "(" +
member.getPath().toString()
+ ")";
- ModuleProvider* MP = GetBytecodeSymbols(
- (const unsigned char*)data,fSize,FullMemberName, symbols);
+ ModuleProvider* MP =
+ GetBitcodeSymbols((const unsigned char*)data,fSize,
+ FullMemberName, symbols, ErrMsg);
- // If the bytecode parsed successfully
+ // If the bitcode parsed successfully
if ( MP ) {
for (std::vector<std::string>::iterator SI = symbols.begin(),
SE = symbols.end(); SI != SE; ++SI) {
// We don't need this module any more.
delete MP;
} else {
- throw std::string("Can't parse bytecode member: ") +
- member.getPath().toString();
+ if (mFile != 0) {
+ mFile->close();
+ delete mFile;
+ }
+ if (ErrMsg)
+ *ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
+ + ": " + *ErrMsg;
+ return true;
}
}
- // Determine if we actually should compress this member
- bool willCompress =
- (ShouldCompress &&
- !member.isCompressed() &&
- !member.isCompressedBytecode() &&
- !member.isLLVMSymbolTable() &&
- !member.isSVR4SymbolTable() &&
- !member.isBSD4SymbolTable());
-
- // Perform the compression. Note that if the file is uncompressed bytecode
- // then we turn the file into compressed bytecode rather than treating it as
- // compressed data. This is necessary since it allows us to determine that the
- // file contains bytecode instead of looking like a regular compressed data
- // member. A compressed bytecode file has its content compressed but has a
- // magic number of "llvc". This acounts for the +/-4 arithmetic in the code
- // below.
- int hdrSize;
- if (willCompress) {
- char* output = 0;
- if (member.isBytecode()) {
- data +=4;
- fSize -= 4;
- }
- fSize = Compressor::compressToNewBuffer(data,fSize,output);
- data = output;
- if (member.isBytecode())
- hdrSize = -fSize-4;
- else
- hdrSize = -fSize;
- } else {
- hdrSize = fSize;
- }
+ int hdrSize = fSize;
// Compute the fields of the header
ArchiveMemberHeader Hdr;
member.getPath().toString().length());
}
- // Make sure we write the compressed bytecode magic number if we should.
- if (willCompress && member.isBytecode())
- ARFile.write("llvc",4);
-
// Write the (possibly compressed) member's content to the file.
ARFile.write(data,fSize);
if ((ARFile.tellp() & 1) == 1)
ARFile << ARFILE_PAD;
- // Free the compressed data, if necessary
- if (willCompress) {
- free((void*)data);
- }
-
// Close the mapped file if it was opened
if (mFile != 0) {
mFile->close();
delete mFile;
}
+ return false;
}
// Write out the LLVM symbol table as an archive member to the file.
// This writes to a temporary file first. Options are for creating a symbol
// table, flattening the file names (no directories, 15 chars max) and
// compressing each archive member.
-void
-Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress){
-
+bool
+Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
+ std::string* ErrMsg)
+{
// 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.
- assert(!(members.empty() && mapfile->size() > 8) &&
- "Can't write an archive not opened for writing");
+ if (members.empty() && mapfile->size() > 8) {
+ if (ErrMsg)
+ *ErrMsg = "Can't write an archive not opened for writing";
+ return true;
+ }
// Create a temporary file to store the archive in
sys::Path TmpArchive = archPath;
- TmpArchive.createTemporaryFile();
+ if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
+ return true;
// Make sure the temporary gets removed if we crash
sys::RemoveFileOnSignal(TmpArchive);
- // Ensure we can remove the temporary even in the face of an exception
- try {
- // 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.
- if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
- throw std::string("Error opening archive file: ") + archPath.toString();
- }
+ // 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);
- // If we're creating a symbol table, reset it now
- if (CreateSymbolTable) {
- symTabSize = 0;
- symTab.clear();
- }
+ // Check for errors opening or creating archive file.
+ if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
+ if (TmpArchive.exists())
+ TmpArchive.eraseFromDisk();
+ if (ErrMsg)
+ *ErrMsg = "Error opening archive file: " + archPath.toString();
+ return true;
+ }
- // Write magic string to archive.
- ArchiveFile << ARFILE_MAGIC;
+ // If we're creating a symbol table, reset it now
+ if (CreateSymbolTable) {
+ symTabSize = 0;
+ symTab.clear();
+ }
- // Loop over all member files, and write them out. Note that this also
- // builds the symbol table, symTab.
- for ( MembersList::iterator I = begin(), E = end(); I != E; ++I) {
- writeMember(*I,ArchiveFile,CreateSymbolTable,TruncateNames,Compress);
+ // 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.
+ for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
+ if (writeMember(*I, ArchiveFile, CreateSymbolTable,
+ TruncateNames, Compress, ErrMsg)) {
+ if (TmpArchive.exists())
+ TmpArchive.eraseFromDisk();
+ ArchiveFile.close();
+ return true;
}
+ }
- // 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.
-
- // Map in the archive we just wrote.
- sys::MappedFile arch(TmpArchive);
- const char* base = (const char*) arch.map();
-
- // Open the final file to write and check it.
- std::ofstream FinalFile(archPath.c_str(), io_mode);
- if ( !FinalFile.is_open() || FinalFile.bad() ) {
- throw std::string("Error opening archive file: ") + archPath.toString();
- }
-
- // Write the file magic number
- FinalFile << ARFILE_MAGIC;
+ // 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.
+
+ // Map in the archive we just wrote.
+ sys::MappedFile arch;
+ if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
+ return true;
+ const char* base;
+ if (!(base = (const char*) arch.map(ErrMsg)))
+ return true;
+
+ // Open another temporary file in order to avoid invalidating the
+ // mmapped data
+ sys::Path FinalFilePath = archPath;
+ if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
+ return true;
+ sys::RemoveFileOnSignal(FinalFilePath);
+
+ std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
+ if (!FinalFile.is_open() || FinalFile.bad()) {
+ if (TmpArchive.exists())
+ TmpArchive.eraseFromDisk();
+ if (ErrMsg)
+ *ErrMsg = "Error opening archive file: " + FinalFilePath.toString();
+ return true;
+ }
- // 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) {
- writeMember(*foreignST, FinalFile, false, false, false);
+ // Write the file magic number
+ FinalFile << ARFILE_MAGIC;
+
+ // 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) {
+ if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
+ FinalFile.close();
+ if (TmpArchive.exists())
+ TmpArchive.eraseFromDisk();
+ return true;
}
-
- // 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,
- arch.size()-sizeof(ARFILE_MAGIC)+1);
-
- // Close up shop
- FinalFile.close();
- arch.close();
- TmpArchive.destroy();
-
- } else {
- // We don't have to insert the symbol table, so just renaming the temp
- // file to the correct name will suffice.
- TmpArchive.rename(archPath);
}
- } catch (...) {
- // Make sure we clean up.
- if (TmpArchive.exists())
- TmpArchive.destroy();
- throw;
+
+ // 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,
+ arch.size()-sizeof(ARFILE_MAGIC)+1);
+
+ // Close up shop
+ FinalFile.close();
+ arch.close();
+
+ // Move the final file over top of TmpArchive
+ if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
+ return true;
}
+
+ // 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();
+
+ if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
+ return true;
+
+ return false;
}