mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-02 07:11:49 +00:00
change the archive stuff to use MemoryBuffer instead of mappedfile.
MemoryBuffer is higher level and more closely matches the model needed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49029 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
685412e92f
commit
7f6b447904
@ -24,7 +24,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
namespace sys { class MappedFile; }
|
class MemoryBuffer;
|
||||||
|
|
||||||
// Forward declare classes
|
// Forward declare classes
|
||||||
class ModuleProvider; // From VMCore
|
class ModuleProvider; // From VMCore
|
||||||
@ -534,7 +534,7 @@ class Archive {
|
|||||||
protected:
|
protected:
|
||||||
sys::Path archPath; ///< Path to the archive file we read/write
|
sys::Path archPath; ///< Path to the archive file we read/write
|
||||||
MembersList members; ///< The ilist of ArchiveMember
|
MembersList members; ///< The ilist of ArchiveMember
|
||||||
sys::MappedFile* mapfile; ///< Raw Archive contents mapped into memory
|
MemoryBuffer *mapfile; ///< Raw Archive contents mapped into memory
|
||||||
const char* base; ///< Base of the memory mapped file data
|
const char* base; ///< Base of the memory mapped file data
|
||||||
SymTabType symTab; ///< The symbol table
|
SymTabType symTab; ///< The symbol table
|
||||||
std::string strtab; ///< The string table for long file names
|
std::string strtab; ///< The string table for long file names
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
#include "llvm/ModuleProvider.h"
|
#include "llvm/ModuleProvider.h"
|
||||||
#include "llvm/Module.h"
|
#include "llvm/Module.h"
|
||||||
#include "llvm/Support/MemoryBuffer.h"
|
#include "llvm/Support/MemoryBuffer.h"
|
||||||
#include "llvm/System/MappedFile.h"
|
|
||||||
#include "llvm/System/Process.h"
|
#include "llvm/System/Process.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -145,25 +144,19 @@ Archive::Archive(const sys::Path& filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Archive::mapToMemory(std::string* ErrMsg)
|
Archive::mapToMemory(std::string* ErrMsg) {
|
||||||
{
|
mapfile = MemoryBuffer::getFile(archPath.c_str(), archPath.size(), ErrMsg);
|
||||||
mapfile = new sys::MappedFile();
|
if (mapfile == 0)
|
||||||
if (mapfile->open(archPath, ErrMsg))
|
|
||||||
return true;
|
|
||||||
if (!(base = (char*) mapfile->map(ErrMsg)))
|
|
||||||
return true;
|
return true;
|
||||||
|
base = mapfile->getBufferStart();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Archive::cleanUpMemory() {
|
void Archive::cleanUpMemory() {
|
||||||
// Shutdown the file mapping
|
// Shutdown the file mapping
|
||||||
if (mapfile) {
|
delete mapfile;
|
||||||
mapfile->close();
|
mapfile = 0;
|
||||||
delete mapfile;
|
base = 0;
|
||||||
|
|
||||||
mapfile = 0;
|
|
||||||
base = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forget the entire symbol table
|
// Forget the entire symbol table
|
||||||
symTab.clear();
|
symTab.clear();
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
#include "ArchiveInternals.h"
|
#include "ArchiveInternals.h"
|
||||||
#include "llvm/Bitcode/ReaderWriter.h"
|
#include "llvm/Bitcode/ReaderWriter.h"
|
||||||
#include "llvm/Support/MemoryBuffer.h"
|
#include "llvm/Support/MemoryBuffer.h"
|
||||||
#include "llvm/System/MappedFile.h"
|
|
||||||
#include "llvm/Module.h"
|
#include "llvm/Module.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -239,7 +238,7 @@ Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
|
|||||||
bool
|
bool
|
||||||
Archive::checkSignature(std::string* error) {
|
Archive::checkSignature(std::string* error) {
|
||||||
// Check the magic string at file's header
|
// Check the magic string at file's header
|
||||||
if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
|
if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
|
||||||
if (error)
|
if (error)
|
||||||
*error = "invalid signature for an archive file";
|
*error = "invalid signature for an archive file";
|
||||||
return false;
|
return false;
|
||||||
@ -257,7 +256,7 @@ Archive::loadArchive(std::string* error) {
|
|||||||
members.clear();
|
members.clear();
|
||||||
symTab.clear();
|
symTab.clear();
|
||||||
const char *At = base;
|
const char *At = base;
|
||||||
const char *End = base + mapfile->size();
|
const char *End = mapfile->getBufferEnd();
|
||||||
|
|
||||||
if (!checkSignature(error))
|
if (!checkSignature(error))
|
||||||
return false;
|
return false;
|
||||||
@ -370,7 +369,7 @@ Archive::loadSymbolTable(std::string* ErrorMsg) {
|
|||||||
members.clear();
|
members.clear();
|
||||||
symTab.clear();
|
symTab.clear();
|
||||||
const char *At = base;
|
const char *At = base;
|
||||||
const char *End = base + mapfile->size();
|
const char *End = mapfile->getBufferEnd();
|
||||||
|
|
||||||
// Make sure we're dealing with an archive
|
// Make sure we're dealing with an archive
|
||||||
if (!checkSignature(ErrorMsg))
|
if (!checkSignature(ErrorMsg))
|
||||||
@ -478,7 +477,8 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
|
|||||||
|
|
||||||
// Module hasn't been loaded yet, we need to load it
|
// Module hasn't been loaded yet, we need to load it
|
||||||
const char* modptr = base + fileOffset;
|
const char* modptr = base + fileOffset;
|
||||||
ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg);
|
ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
|
||||||
|
ErrMsg);
|
||||||
if (!mbr)
|
if (!mbr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -517,8 +517,8 @@ Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
|
|||||||
// below.
|
// below.
|
||||||
|
|
||||||
// Get a pointer to the first file
|
// Get a pointer to the first file
|
||||||
const char* At = ((const char*)base) + firstFileOffset;
|
const char* At = base + firstFileOffset;
|
||||||
const char* End = ((const char*)base) + mapfile->size();
|
const char* End = mapfile->getBufferEnd();
|
||||||
|
|
||||||
while ( At < End) {
|
while ( At < End) {
|
||||||
// Compute the offset to be put in the symbol table
|
// Compute the offset to be put in the symbol table
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
|
|
||||||
#include "ArchiveInternals.h"
|
#include "ArchiveInternals.h"
|
||||||
#include "llvm/Bitcode/ReaderWriter.h"
|
#include "llvm/Bitcode/ReaderWriter.h"
|
||||||
#include "llvm/System/MappedFile.h"
|
#include "llvm/ADT/OwningPtr.h"
|
||||||
|
#include "llvm/Support/MemoryBuffer.h"
|
||||||
#include "llvm/System/Signals.h"
|
#include "llvm/System/Signals.h"
|
||||||
#include "llvm/System/Process.h"
|
#include "llvm/System/Process.h"
|
||||||
#include "llvm/ModuleProvider.h"
|
#include "llvm/ModuleProvider.h"
|
||||||
@ -208,15 +209,15 @@ Archive::writeMember(
|
|||||||
// Get the data and its size either from the
|
// Get the data and its size either from the
|
||||||
// member's in-memory data or directly from the file.
|
// member's in-memory data or directly from the file.
|
||||||
size_t fSize = member.getSize();
|
size_t fSize = member.getSize();
|
||||||
const char* data = (const char*)member.getData();
|
const char *data = (const char*)member.getData();
|
||||||
sys::MappedFile* mFile = 0;
|
MemoryBuffer *mFile = 0;
|
||||||
if (!data) {
|
if (!data) {
|
||||||
mFile = new sys::MappedFile();
|
mFile = MemoryBuffer::getFile(member.getPath().c_str(),
|
||||||
if (mFile->open(member.getPath(), ErrMsg))
|
member.getPath().size(), ErrMsg);
|
||||||
|
if (mFile == 0)
|
||||||
return true;
|
return true;
|
||||||
if (!(data = (const char*) mFile->map(ErrMsg)))
|
data = mFile->getBufferStart();
|
||||||
return true;
|
fSize = mFile->getBufferSize();
|
||||||
fSize = mFile->size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that we have the data in memory, update the
|
// Now that we have the data in memory, update the
|
||||||
@ -247,10 +248,7 @@ Archive::writeMember(
|
|||||||
// We don't need this module any more.
|
// We don't need this module any more.
|
||||||
delete MP;
|
delete MP;
|
||||||
} else {
|
} else {
|
||||||
if (mFile != 0) {
|
delete mFile;
|
||||||
mFile->close();
|
|
||||||
delete mFile;
|
|
||||||
}
|
|
||||||
if (ErrMsg)
|
if (ErrMsg)
|
||||||
*ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
|
*ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
|
||||||
+ ": " + *ErrMsg;
|
+ ": " + *ErrMsg;
|
||||||
@ -281,10 +279,7 @@ Archive::writeMember(
|
|||||||
ARFile << ARFILE_PAD;
|
ARFile << ARFILE_PAD;
|
||||||
|
|
||||||
// Close the mapped file if it was opened
|
// Close the mapped file if it was opened
|
||||||
if (mFile != 0) {
|
delete mFile;
|
||||||
mFile->close();
|
|
||||||
delete mFile;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -349,7 +344,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
|
|||||||
{
|
{
|
||||||
// Make sure they haven't opened up the file, not loaded it,
|
// 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.
|
// but are now trying to write it which would wipe out the file.
|
||||||
if (members.empty() && mapfile && mapfile->size() > 8) {
|
if (members.empty() && mapfile && mapfile->getBufferSize() > 8) {
|
||||||
if (ErrMsg)
|
if (ErrMsg)
|
||||||
*ErrMsg = "Can't write an archive not opened for writing";
|
*ErrMsg = "Can't write an archive not opened for writing";
|
||||||
return true;
|
return true;
|
||||||
@ -408,18 +403,17 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
|
|||||||
// ensure compatibility with other archivers we need to put the symbol
|
// ensure compatibility with other archivers we need to put the symbol
|
||||||
// table first in the file. Unfortunately, this means mapping the file
|
// table first in the file. Unfortunately, this means mapping the file
|
||||||
// we just wrote back in and copying it to the destination file.
|
// we just wrote back in and copying it to the destination file.
|
||||||
|
sys::Path FinalFilePath = archPath;
|
||||||
|
|
||||||
// Map in the archive we just wrote.
|
// Map in the archive we just wrote.
|
||||||
sys::MappedFile arch;
|
{
|
||||||
if (arch.open(TmpArchive, ErrMsg))
|
OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(),
|
||||||
return true;
|
TmpArchive.size()));
|
||||||
const char* base;
|
if (arch == 0) return true;
|
||||||
if (!(base = (const char*) arch.map(ErrMsg)))
|
const char* base = arch->getBufferStart();
|
||||||
return true;
|
|
||||||
|
|
||||||
// Open another temporary file in order to avoid invalidating the
|
// Open another temporary file in order to avoid invalidating the
|
||||||
// mmapped data
|
// mmapped data
|
||||||
sys::Path FinalFilePath = archPath;
|
|
||||||
if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
|
if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
|
||||||
return true;
|
return true;
|
||||||
sys::RemoveFileOnSignal(FinalFilePath);
|
sys::RemoveFileOnSignal(FinalFilePath);
|
||||||
@ -456,11 +450,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
|
|||||||
// Copy the temporary file contents being sure to skip the file's magic
|
// Copy the temporary file contents being sure to skip the file's magic
|
||||||
// number.
|
// number.
|
||||||
FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
|
FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
|
||||||
arch.size()-sizeof(ARFILE_MAGIC)+1);
|
arch->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
|
||||||
|
|
||||||
// Close up shop
|
// Close up shop
|
||||||
FinalFile.close();
|
FinalFile.close();
|
||||||
arch.close();
|
} // free arch.
|
||||||
|
|
||||||
// Move the final file over top of TmpArchive
|
// Move the final file over top of TmpArchive
|
||||||
if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
|
if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
|
||||||
|
Loading…
Reference in New Issue
Block a user