2003-09-22 23:44:46 +00:00
|
|
|
//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
|
|
|
|
//
|
|
|
|
// This file implements loading and parsing a bytecode file and parsing a
|
|
|
|
// bytecode module from a given buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
#include "ReaderInternals.h"
|
|
|
|
#include "Support/StringExtras.h"
|
|
|
|
#include "Config/fcntl.h"
|
2003-10-06 03:30:28 +00:00
|
|
|
#include <sys/stat.h>
|
2003-09-22 23:38:23 +00:00
|
|
|
#include "Config/unistd.h"
|
|
|
|
#include "Config/sys/mman.h"
|
|
|
|
|
|
|
|
namespace {
|
2003-09-23 16:13:28 +00:00
|
|
|
/// FDHandle - Simple handle class to make sure a file descriptor gets closed
|
|
|
|
/// when the object is destroyed.
|
|
|
|
///
|
|
|
|
class FDHandle {
|
|
|
|
int FD;
|
|
|
|
public:
|
|
|
|
FDHandle(int fd) : FD(fd) {}
|
|
|
|
operator int() const { return FD; }
|
|
|
|
~FDHandle() {
|
|
|
|
if (FD != -1) close(FD);
|
|
|
|
}
|
|
|
|
};
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
/// BytecodeFileReader - parses a bytecode file from a file
|
|
|
|
///
|
|
|
|
class BytecodeFileReader : public BytecodeParser {
|
|
|
|
private:
|
|
|
|
unsigned char *Buffer;
|
|
|
|
int Length;
|
|
|
|
|
|
|
|
BytecodeFileReader(const BytecodeFileReader&); // Do not implement
|
2003-09-23 15:09:26 +00:00
|
|
|
void operator=(const BytecodeFileReader &BFR); // Do not implement
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeFileReader(const std::string &Filename);
|
|
|
|
~BytecodeFileReader();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
|
|
|
|
FDHandle FD = open(Filename.c_str(), O_RDONLY);
|
|
|
|
if (FD == -1)
|
|
|
|
throw std::string("Error opening file!");
|
|
|
|
|
|
|
|
// Stat the file to get its length...
|
|
|
|
struct stat StatBuf;
|
|
|
|
if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
|
|
|
|
throw std::string("Error stat'ing file!");
|
|
|
|
|
|
|
|
// mmap in the file all at once...
|
|
|
|
Length = StatBuf.st_size;
|
2003-09-25 04:13:53 +00:00
|
|
|
Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
if (Buffer == (unsigned char*)MAP_FAILED)
|
|
|
|
throw std::string("Error mmapping file!");
|
|
|
|
|
2003-10-08 19:55:47 +00:00
|
|
|
try {
|
|
|
|
// Parse the bytecode we mmapped in
|
|
|
|
ParseBytecode(Buffer, Length, Filename);
|
|
|
|
} catch (...) {
|
|
|
|
munmap((char*)Buffer, Length);
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeFileReader::~BytecodeFileReader() {
|
|
|
|
// Unmmap the bytecode...
|
|
|
|
munmap((char*)Buffer, Length);
|
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeBufferReader - parses a bytecode file from a buffer
|
|
|
|
///
|
|
|
|
class BytecodeBufferReader : public BytecodeParser {
|
|
|
|
private:
|
|
|
|
const unsigned char *Buffer;
|
|
|
|
bool MustDelete;
|
|
|
|
|
|
|
|
BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
|
|
|
|
void operator=(const BytecodeBufferReader &BFR); // Do not implement
|
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
|
|
|
|
const std::string &ModuleID);
|
|
|
|
~BytecodeBufferReader();
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
|
2003-09-24 22:04:02 +00:00
|
|
|
unsigned Length,
|
2003-09-23 16:13:28 +00:00
|
|
|
const std::string &ModuleID)
|
|
|
|
{
|
|
|
|
// If not aligned, allocate a new buffer to hold the bytecode...
|
|
|
|
const unsigned char *ParseBegin = 0;
|
|
|
|
if ((intptr_t)Buf & 3) {
|
2003-09-24 22:04:02 +00:00
|
|
|
Buffer = new unsigned char[Length+4];
|
2003-09-24 22:34:17 +00:00
|
|
|
unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
|
2003-09-23 16:13:28 +00:00
|
|
|
ParseBegin = Buffer + Offset;
|
2003-09-24 22:04:02 +00:00
|
|
|
memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
|
2003-09-23 16:13:28 +00:00
|
|
|
MustDelete = true;
|
|
|
|
} else {
|
|
|
|
// If we don't need to copy it over, just use the caller's copy
|
2003-09-23 21:19:11 +00:00
|
|
|
ParseBegin = Buffer = Buf;
|
2003-09-23 16:13:28 +00:00
|
|
|
MustDelete = false;
|
|
|
|
}
|
2003-10-08 19:55:47 +00:00
|
|
|
try {
|
|
|
|
ParseBytecode(ParseBegin, Length, ModuleID);
|
|
|
|
} catch (...) {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-23 16:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::~BytecodeBufferReader() {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeStdinReader - parses a bytecode file from stdin
|
|
|
|
///
|
|
|
|
class BytecodeStdinReader : public BytecodeParser {
|
|
|
|
private:
|
|
|
|
std::vector<unsigned char> FileData;
|
|
|
|
unsigned char *FileBuf;
|
|
|
|
|
|
|
|
BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
|
|
|
|
void operator=(const BytecodeStdinReader &BFR); // Do not implement
|
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeStdinReader();
|
|
|
|
};
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
BytecodeStdinReader::BytecodeStdinReader() {
|
|
|
|
int BlockSize;
|
|
|
|
unsigned char Buffer[4096*4];
|
|
|
|
|
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin...
|
|
|
|
while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
|
|
|
|
if (BlockSize == -1)
|
|
|
|
throw std::string("Error reading from stdin!");
|
2003-09-23 16:13:28 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileData.empty())
|
|
|
|
throw std::string("Standard Input empty!");
|
|
|
|
|
|
|
|
FileBuf = &FileData[0];
|
|
|
|
ParseBytecode(FileBuf, FileData.size(), "<stdin>");
|
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Wrapper functions
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
|
|
|
|
/// buffer
|
2003-10-04 20:14:59 +00:00
|
|
|
ModuleProvider*
|
2003-09-22 23:38:23 +00:00
|
|
|
getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID) {
|
2003-09-23 16:13:28 +00:00
|
|
|
return new BytecodeBufferReader(Buffer, Length, ModuleID);
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// ParseBytecodeBuffer - Parse a given bytecode buffer
|
|
|
|
///
|
2003-09-22 23:38:23 +00:00
|
|
|
Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID, std::string *ErrorStr){
|
2003-09-23 16:13:28 +00:00
|
|
|
try {
|
2003-10-04 20:14:59 +00:00
|
|
|
std::auto_ptr<ModuleProvider>
|
2003-10-04 19:19:37 +00:00
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
|
|
|
|
return AMP->releaseModule();
|
2003-09-23 16:13:28 +00:00
|
|
|
} catch (std::string &err) {
|
2003-09-24 22:10:47 +00:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 16:13:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
|
2003-09-22 23:38:23 +00:00
|
|
|
///
|
2003-10-04 20:14:59 +00:00
|
|
|
ModuleProvider *getBytecodeModuleProvider(const std::string &Filename) {
|
2003-09-22 23:38:23 +00:00
|
|
|
if (Filename != std::string("-")) // Read from a file...
|
|
|
|
return new BytecodeFileReader(Filename);
|
|
|
|
else // Read from stdin
|
|
|
|
return new BytecodeStdinReader();
|
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// ParseBytecodeFile - Parse the given bytecode file
|
|
|
|
///
|
2003-09-22 23:38:23 +00:00
|
|
|
Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
|
2003-09-23 16:13:28 +00:00
|
|
|
try {
|
2003-10-04 20:14:59 +00:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
|
2003-10-04 19:19:37 +00:00
|
|
|
return AMP->releaseModule();
|
2003-09-23 16:13:28 +00:00
|
|
|
} catch (std::string &err) {
|
2003-09-24 22:10:47 +00:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 16:13:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|