summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/Archive/ArchiveWriter.cpp
diff options
context:
space:
mode:
authordim <dim@FreeBSD.org>2014-03-21 17:53:59 +0000
committerdim <dim@FreeBSD.org>2014-03-21 17:53:59 +0000
commit9cedb8bb69b89b0f0c529937247a6a80cabdbaec (patch)
treec978f0e9ec1ab92dc8123783f30b08a7fd1e2a39 /contrib/llvm/lib/Archive/ArchiveWriter.cpp
parent03fdc2934eb61c44c049a02b02aa974cfdd8a0eb (diff)
downloadFreeBSD-src-9cedb8bb69b89b0f0c529937247a6a80cabdbaec.zip
FreeBSD-src-9cedb8bb69b89b0f0c529937247a6a80cabdbaec.tar.gz
MFC 261991:
Upgrade our copy of llvm/clang to 3.4 release. This version supports all of the features in the current working draft of the upcoming C++ standard, provisionally named C++1y. The code generator's performance is greatly increased, and the loop auto-vectorizer is now enabled at -Os and -O2 in addition to -O3. The PowerPC backend has made several major improvements to code generation quality and compile time, and the X86, SPARC, ARM32, Aarch64 and SystemZ backends have all seen major feature work. Release notes for llvm and clang can be found here: <http://llvm.org/releases/3.4/docs/ReleaseNotes.html> <http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html> MFC 262121 (by emaste): Update lldb for clang/llvm 3.4 import This commit largely restores the lldb source to the upstream r196259 snapshot with the addition of threaded inferior support and a few bug fixes. Specific upstream lldb revisions restored include: SVN git 181387 779e6ac 181703 7bef4e2 182099 b31044e 182650 f2dcf35 182683 0d91b80 183862 15c1774 183929 99447a6 184177 0b2934b 184948 4dc3761 184954 007e7bc 186990 eebd175 Sponsored by: DARPA, AFRL MFC 262186 (by emaste): Fix mismerge in r262121 A break statement was lost in the merge. The error had no functional impact, but restore it to reduce the diff against upstream. MFC 262303: Pull in r197521 from upstream clang trunk (by rdivacky): Use the integrated assembler by default on FreeBSD/ppc and ppc64. Requested by: jhibbits MFC 262611: Pull in r196874 from upstream llvm trunk: Fix a crash that occurs when PWD is invalid. MCJIT needs to be able to run in hostile environments, even when PWD is invalid. There's no need to crash MCJIT in this case. The obvious fix is to simply leave MCContext's CompilationDir empty when PWD can't be determined. This way, MCJIT clients, and other clients that link with LLVM don't need a valid working directory. If we do want to guarantee valid CompilationDir, that should be done only for clients of getCompilationDir(). This is as simple as checking for an empty string. The only current use of getCompilationDir is EmitGenDwarfInfo, which won't conceivably run with an invalid working dir. However, in the purely hypothetically and untestable case that this happens, the AT_comp_dir will be omitted from the compilation_unit DIE. This should help fix assertions occurring with ports-mgmt/tinderbox, when it is using jails, and sometimes invalidates clang's current working directory. Reported by: decke MFC 262809: Pull in r203007 from upstream clang trunk: Don't produce an alias between destructors with different calling conventions. Fixes pr19007. (Please note that is an LLVM PR identifier, not a FreeBSD one.) This should fix Firefox and/or libxul crashes (due to problems with regparm/stdcall calling conventions) on i386. Reported by: multiple users on freebsd-current PR: bin/187103 MFC 263048: Repair recognition of "CC" as an alias for the C++ compiler, since it was silently broken by upstream for a Windows-specific use-case. Apparently some versions of CMake still rely on this archaic feature... Reported by: rakuco MFC 263049: Garbage collect the old way of adding the libstdc++ include directories in clang's InitHeaderSearch.cpp. This has been superseded by David Chisnall's commit in r255321. Moreover, if libc++ is used, the libstdc++ include directories should not be in the search path at all. These directories are now only used if you pass -stdlib=libstdc++.
Diffstat (limited to 'contrib/llvm/lib/Archive/ArchiveWriter.cpp')
-rw-r--r--contrib/llvm/lib/Archive/ArchiveWriter.cpp489
1 files changed, 0 insertions, 489 deletions
diff --git a/contrib/llvm/lib/Archive/ArchiveWriter.cpp b/contrib/llvm/lib/Archive/ArchiveWriter.cpp
deleted file mode 100644
index 3eba701..0000000
--- a/contrib/llvm/lib/Archive/ArchiveWriter.cpp
+++ /dev/null
@@ -1,489 +0,0 @@
-//===-- ArchiveWriter.cpp - Write LLVM archive files ----------------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// Builds up an LLVM archive file (.a) containing LLVM bitcode.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Bitcode/Archive.h"
-#include "ArchiveInternals.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/IR/Module.h"
-#include "llvm/Support/FileSystem.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/Process.h"
-#include "llvm/Support/Signals.h"
-#include "llvm/Support/system_error.h"
-#include <fstream>
-#include <iomanip>
-#include <ostream>
-using namespace llvm;
-
-// Write an integer using variable bit rate encoding. This saves a few bytes
-// per entry in the symbol table.
-static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
- while (1) {
- if (num < 0x80) { // done?
- ARFile << (unsigned char)num;
- return;
- }
-
- // 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...
- }
-}
-
-// Compute how many bytes are taken by a given VBR encoded value. This is needed
-// to pre-compute the size of the symbol table.
-static inline unsigned numVbrBytes(unsigned num) {
-
- // 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
- // most reasonable values to be done in 2 comparisons instead of 1 for
- // small ones and four for large ones. We expect this to access file offsets
- // in the 2^10 to 2^24 range and symbol lengths in the 2^0 to 2^8 range,
- // so this approach is reasonable.
- if (num < 1<<14) {
- if (num < 1<<7)
- return 1;
- else
- return 2;
- }
- if (num < 1<<21)
- return 3;
-
- if (num < 1<<28)
- return 4;
- return 5; // anything >= 2^28 takes 5 bytes
-}
-
-// Create an empty archive.
-Archive* Archive::CreateEmpty(const sys::Path& FilePath, LLVMContext& C) {
- Archive* result = new Archive(FilePath, C);
- return result;
-}
-
-// Fill the ArchiveMemberHeader with the information from a member. If
-// TruncateNames is true, names are flattened to 15 chars or less. The sz field
-// 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
-// compressed.
-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);
-
- // Get rid of trailing blanks in the name
- std::string mbrPath = mbr.getPath().str();
- size_t mbrLen = mbrPath.length();
- while (mbrLen > 0 && mbrPath[mbrLen-1] == ' ') {
- mbrPath.erase(mbrLen-1,1);
- mbrLen--;
- }
-
- // Set the name field in one of its various flavors.
- bool writeLongName = false;
- if (mbr.isStringTable()) {
- memcpy(hdr.name,ARFILE_STRTAB_NAME,16);
- } else if (mbr.isSVR4SymbolTable()) {
- memcpy(hdr.name,ARFILE_SVR4_SYMTAB_NAME,16);
- } else if (mbr.isBSD4SymbolTable()) {
- memcpy(hdr.name,ARFILE_BSD4_SYMTAB_NAME,16);
- } 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;
- }
- if (len > 15)
- len = 15;
- memcpy(hdr.name,nm,len);
- hdr.name[len] = '/';
- } else if (mbrPath.length() < 16 && mbrPath.find('/') == std::string::npos) {
- memcpy(hdr.name,mbrPath.c_str(),mbrPath.length());
- hdr.name[mbrPath.length()] = '/';
- } else {
- std::string nm = "#1/";
- nm += utostr(mbrPath.length());
- memcpy(hdr.name,nm.data(),nm.length());
- if (sz < 0)
- sz -= mbrPath.length();
- else
- sz += mbrPath.length();
- writeLongName = true;
- }
-
- // 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);
-
- return writeLongName;
-}
-
-// Insert a file into the archive before some other member. This also takes care
-// of extracting the necessary flags and information from the file.
-bool
-Archive::addFileBefore(const sys::Path& filePath, iterator where,
- std::string* ErrMsg) {
- bool Exists;
- if (sys::fs::exists(filePath.str(), Exists) || !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;
- const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
- if (!FSInfo) {
- delete mbr;
- return true;
- }
- mbr->info = *FSInfo;
-
- unsigned flags = 0;
- bool hasSlash = filePath.str().find('/') != std::string::npos;
- if (hasSlash)
- flags |= ArchiveMember::HasPathFlag;
- if (hasSlash || filePath.str().length() > 15)
- flags |= ArchiveMember::HasLongFilenameFlag;
-
- sys::fs::file_magic type;
- if (sys::fs::identify_magic(mbr->path.str(), type))
- type = sys::fs::file_magic::unknown;
- switch (type) {
- case sys::fs::file_magic::bitcode:
- flags |= ArchiveMember::BitcodeFlag;
- break;
- default:
- break;
- }
- mbr->flags = flags;
- members.insert(where,mbr);
- return false;
-}
-
-// Write one member out to the file.
-bool
-Archive::writeMember(
- const ArchiveMember& member,
- std::ofstream& ARFile,
- bool CreateSymbolTable,
- bool TruncateNames,
- std::string* ErrMsg
-) {
-
- 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();
- const char *data = (const char*)member.getData();
- MemoryBuffer *mFile = 0;
- if (!data) {
- OwningPtr<MemoryBuffer> File;
- if (error_code ec = MemoryBuffer::getFile(member.getPath().c_str(), File)) {
- if (ErrMsg)
- *ErrMsg = ec.message();
- return true;
- }
- mFile = File.take();
- data = mFile->getBufferStart();
- fSize = mFile->getBufferSize();
- }
-
- // Now that we have the data in memory, update the
- // symbol table if it's a bitcode file.
- if (CreateSymbolTable && member.isBitcode()) {
- std::vector<std::string> symbols;
- std::string FullMemberName = archPath.str() + "(" + member.getPath().str()
- + ")";
- Module* M =
- GetBitcodeSymbols(data, fSize, FullMemberName, Context, symbols, ErrMsg);
-
- // If the bitcode parsed successfully
- if ( M ) {
- for (std::vector<std::string>::iterator SI = symbols.begin(),
- SE = symbols.end(); SI != SE; ++SI) {
-
- std::pair<SymTabType::iterator,bool> Res =
- symTab.insert(std::make_pair(*SI,filepos));
-
- if (Res.second) {
- symTabSize += SI->length() +
- numVbrBytes(SI->length()) +
- numVbrBytes(filepos);
- }
- }
- // We don't need this module any more.
- delete M;
- } else {
- delete mFile;
- if (ErrMsg)
- *ErrMsg = "Can't parse bitcode member: " + member.getPath().str()
- + ": " + *ErrMsg;
- return true;
- }
- }
-
- int hdrSize = fSize;
-
- // Compute the fields of the header
- ArchiveMemberHeader Hdr;
- bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames);
-
- // Write header to archive file
- ARFile.write((char*)&Hdr, sizeof(Hdr));
-
- // Write the long filename if its long
- if (writeLongName) {
- ARFile.write(member.getPath().str().data(),
- member.getPath().str().length());
- }
-
- // Write the (possibly compressed) member's content to the file.
- ARFile.write(data,fSize);
-
- // Make sure the member is an even length
- if ((ARFile.tellp() & 1) == 1)
- ARFile << ARFILE_PAD;
-
- // Close the mapped file if it was opened
- delete mFile;
- return false;
-}
-
-// Write out the LLVM symbol table as an archive member to the file.
-void
-Archive::writeSymbolTable(std::ofstream& ARFile) {
-
- // Construct the symbol table's header
- ArchiveMemberHeader Hdr;
- Hdr.init();
- memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
- uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
- char buffer[32];
- sprintf(buffer, "%-8o", 0644);
- memcpy(Hdr.mode,buffer,8);
- sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId());
- memcpy(Hdr.uid,buffer,6);
- sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId());
- memcpy(Hdr.gid,buffer,6);
- 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));
-
-#ifndef NDEBUG
- // Save the starting position of the symbol tables data content.
- unsigned startpos = ARFile.tellp();
-#endif
-
- // 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());
- }
-
-#ifndef NDEBUG
- // Now that we're done with the symbol table, get the ending file position
- unsigned endpos = ARFile.tellp();
-#endif
-
- // 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");
-
- // Make sure the symbol table is even sized
- if (symTabSize % 2 != 0 )
- ARFile << ARFILE_PAD;
-}
-
-// Write the entire archive to the file specified when the archive was created.
-// 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.
-bool
-Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames,
- 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.
- if (members.empty() && mapfile && mapfile->getBufferSize() > 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;
- if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
- return true;
-
- // Make sure the temporary gets removed if we crash
- sys::RemoveFileOnSignal(TmpArchive);
-
- // 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()) {
- TmpArchive.eraseFromDisk();
- if (ErrMsg)
- *ErrMsg = "Error opening archive file: " + archPath.str();
- return true;
- }
-
- // If we're creating a symbol table, reset it now
- if (CreateSymbolTable) {
- symTabSize = 0;
- symTab.clear();
- }
-
- // 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, ErrMsg)) {
- 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.
- sys::Path FinalFilePath = archPath;
-
- // Map in the archive we just wrote.
- {
- OwningPtr<MemoryBuffer> arch;
- if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
- if (ErrMsg)
- *ErrMsg = ec.message();
- return true;
- }
- const char* base = arch->getBufferStart();
-
- // Open another temporary file in order to avoid invalidating the
- // mmapped data
- if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
- return true;
- sys::RemoveFileOnSignal(FinalFilePath);
-
- std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
- if (!FinalFile.is_open() || FinalFile.bad()) {
- TmpArchive.eraseFromDisk();
- if (ErrMsg)
- *ErrMsg = "Error opening archive file: " + FinalFilePath.str();
- return true;
- }
-
- // 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, ErrMsg)) {
- FinalFile.close();
- 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->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
-
- // Close up shop
- FinalFile.close();
- } // free arch.
-
- // 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;
-
- // 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;
-
- return false;
-}
OpenPOWER on IntegriCloud