base: remove fd from object loaders

All the object loaders directly examine the (already completely loaded
by object_file.cc) memory image. There is no current motivation to
keep the fd around.
This commit is contained in:
Curtis Dunham 2015-07-03 10:14:34 -04:00
parent d9f8f07613
commit e385ae0c72
12 changed files with 39 additions and 61 deletions

View file

@ -39,11 +39,11 @@
using namespace std;
ObjectFile *
AoutObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
AoutObject::tryFile(const string &fname, size_t len, uint8_t *data)
{
if (!N_BADMAG(*(aout_exechdr *)data)) {
// right now this is only used for Alpha PAL code
return new AoutObject(fname, fd, len, data,
return new AoutObject(fname, len, data,
ObjectFile::Alpha, ObjectFile::UnknownOpSys);
}
else {
@ -52,10 +52,10 @@ AoutObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
}
AoutObject::AoutObject(const string &_filename, int _fd,
AoutObject::AoutObject(const string &_filename,
size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
: ObjectFile(_filename, _len, _data, _arch, _opSys)
{
execHdr = (aout_exechdr *)fileData;

View file

@ -41,7 +41,7 @@ class AoutObject : public ObjectFile
protected:
aout_exechdr *execHdr;
AoutObject(const std::string &_filename, int _fd,
AoutObject(const std::string &_filename,
size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys);
@ -53,7 +53,7 @@ class AoutObject : public ObjectFile
virtual bool loadLocalSymbols(SymbolTable *symtab, Addr addrMask =
std::numeric_limits<Addr>::max());
static ObjectFile *tryFile(const std::string &fname, int fd,
static ObjectFile *tryFile(const std::string &fname,
size_t len, uint8_t *data);
};

View file

@ -40,21 +40,20 @@
#include "libfdt.h"
ObjectFile *
DtbObject::tryFile(const std::string &fname, int fd, size_t len, uint8_t *data)
DtbObject::tryFile(const std::string &fname, size_t len, uint8_t *data)
{
// Check if this is a FDT file by looking for magic number
if (fdt_magic((void*)data) == FDT_MAGIC) {
return new DtbObject(fname, fd, len, data,
return new DtbObject(fname, len, data,
ObjectFile::UnknownArch, ObjectFile::UnknownOpSys);
} else {
return NULL;
}
}
DtbObject::DtbObject(const std::string &_filename, int _fd,
size_t _len, uint8_t *_data,
DtbObject::DtbObject(const std::string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
: ObjectFile(_filename, _len, _data, _arch, _opSys)
{
text.baseAddr = 0;
text.size = len;
@ -73,11 +72,6 @@ DtbObject::DtbObject(const std::string &_filename, int _fd,
DtbObject::~DtbObject()
{
if (descriptor >= 0) {
::close(descriptor);
descriptor = -1;
}
// Make sure to clean up memory properly depending
// on how buffer was allocated.
if (fileData && !fileDataMmapped) {

View file

@ -41,8 +41,7 @@
class DtbObject : public ObjectFile
{
protected:
DtbObject(const std::string &_filename, int _fd,
size_t _len, uint8_t *_data,
DtbObject(const std::string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys);
/** Bool marking if this dtb file has replaced the original
@ -75,12 +74,11 @@ class DtbObject : public ObjectFile
/** Static function that tries to load file as a
* flattened device tree blob.
* @param fname path to file
* @param fd file descriptor of object file
* @param len length of file
* @param data mmap'ed data buffer containing file contents
* @return ObjectFile representing closest match of file type
*/
static ObjectFile *tryFile(const std::string &fname, int fd,
static ObjectFile *tryFile(const std::string &fname,
size_t len, uint8_t *data);
};

View file

@ -49,11 +49,11 @@
using namespace std;
ObjectFile *
EcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
EcoffObject::tryFile(const string &fname, size_t len, uint8_t *data)
{
if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
// it's Alpha ECOFF
return new EcoffObject(fname, fd, len, data,
return new EcoffObject(fname, len, data,
ObjectFile::Alpha, ObjectFile::Tru64);
}
else {
@ -62,10 +62,9 @@ EcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
}
EcoffObject::EcoffObject(const string &_filename, int _fd,
size_t _len, uint8_t *_data,
EcoffObject::EcoffObject(const string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
: ObjectFile(_filename, _len, _data, _arch, _opSys)
{
execHdr = (ecoff_exechdr *)fileData;
fileHdr = &(execHdr->f);

View file

@ -45,8 +45,7 @@ class EcoffObject : public ObjectFile
ecoff_filehdr *fileHdr;
ecoff_aouthdr *aoutHdr;
EcoffObject(const std::string &_filename, int _fd,
size_t _len, uint8_t *_data,
EcoffObject(const std::string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys);
public:
@ -57,7 +56,7 @@ class EcoffObject : public ObjectFile
virtual bool loadLocalSymbols(SymbolTable *symtab, Addr addrMask =
std::numeric_limits<Addr>::max());
static ObjectFile *tryFile(const std::string &fname, int fd,
static ObjectFile *tryFile(const std::string &fname,
size_t len, uint8_t *data);
};

View file

@ -56,7 +56,7 @@
using namespace std;
ObjectFile *
ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
ElfObject::tryFile(const string &fname, size_t len, uint8_t *data)
{
Elf *elf;
GElf_Ehdr ehdr;
@ -69,7 +69,6 @@ ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
// get a pointer to elf structure
elf = elf_memory((char*)data,len);
// will only fail if fd is invalid
assert(elf != NULL);
// Check that we actually have a elf file
@ -213,7 +212,7 @@ ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
} // while sections
}
ElfObject * result = new ElfObject(fname, fd, len, data, arch, opSys);
ElfObject * result = new ElfObject(fname, len, data, arch, opSys);
//The number of headers in the file
result->_programHeaderCount = ehdr.e_phnum;
@ -250,10 +249,9 @@ ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
}
ElfObject::ElfObject(const string &_filename, int _fd,
size_t _len, uint8_t *_data,
ElfObject::ElfObject(const string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys),
: ObjectFile(_filename, _len, _data, _arch, _opSys),
_programHeaderTable(0), _programHeaderSize(0), _programHeaderCount(0)
{
@ -266,7 +264,6 @@ ElfObject::ElfObject(const string &_filename, int _fd,
// get a pointer to elf structure
elf = elf_memory((char*)fileData,len);
// will only fail if fd is invalid
assert(elf != NULL);
// Check that we actually have a elf file

View file

@ -65,8 +65,7 @@ class ElfObject : public ObjectFile
/// Helper functions for loadGlobalSymbols() and loadLocalSymbols().
bool loadSomeSymbols(SymbolTable *symtab, int binding, Addr mask);
ElfObject(const std::string &_filename, int _fd,
size_t _len, uint8_t *_data,
ElfObject(const std::string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys);
void getSections();
@ -90,7 +89,7 @@ class ElfObject : public ObjectFile
virtual bool isDynamic() { return sectionExists(".interp"); }
virtual bool hasTLS() { return sectionExists(".tbss"); }
static ObjectFile *tryFile(const std::string &fname, int fd,
static ObjectFile *tryFile(const std::string &fname,
size_t len, uint8_t *data);
Addr programHeaderTable() {return _programHeaderTable;}
uint16_t programHeaderSize() {return _programHeaderSize;}

View file

@ -50,10 +50,10 @@
using namespace std;
ObjectFile::ObjectFile(const string &_filename, int _fd,
ObjectFile::ObjectFile(const string &_filename,
size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys)
: filename(_filename), descriptor(_fd), fileData(_data), len(_len),
: filename(_filename), fileData(_data), len(_len),
arch(_arch), opSys(_opSys), entry(0), globalPtr(0),
text{0, nullptr, 0}, data{0, nullptr, 0}, bss{0, nullptr, 0}
{
@ -95,11 +95,6 @@ ObjectFile::loadSections(PortProxy& memProxy, Addr addrMask, Addr offset)
void
ObjectFile::close()
{
if (descriptor >= 0) {
::close(descriptor);
descriptor = -1;
}
if (fileData) {
::munmap((char*)fileData, len);
fileData = NULL;
@ -124,35 +119,34 @@ createObjectFile(const string &fname, bool raw)
// mmap the whole shebang
uint8_t *fileData =
(uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (fileData == MAP_FAILED) {
close(fd);
return NULL;
}
ObjectFile *fileObj = NULL;
// figure out what we have here
if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
if ((fileObj = ElfObject::tryFile(fname, len, fileData)) != NULL) {
return fileObj;
}
if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
if ((fileObj = EcoffObject::tryFile(fname, len, fileData)) != NULL) {
return fileObj;
}
if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
if ((fileObj = AoutObject::tryFile(fname, len, fileData)) != NULL) {
return fileObj;
}
if ((fileObj = DtbObject::tryFile(fname, fd, len, fileData)) != NULL) {
if ((fileObj = DtbObject::tryFile(fname, len, fileData)) != NULL) {
return fileObj;
}
if (raw)
return RawObject::tryFile(fname, fd, len, fileData);
return RawObject::tryFile(fname, len, fileData);
// don't know what it is
close(fd);
munmap((char*)fileData, len);
return NULL;
}

View file

@ -69,15 +69,13 @@ class ObjectFile
protected:
const std::string filename;
int descriptor;
uint8_t *fileData;
size_t len;
Arch arch;
OpSys opSys;
ObjectFile(const std::string &_filename, int _fd,
size_t _len, uint8_t *_data,
ObjectFile(const std::string &_filename, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys);
public:

View file

@ -34,15 +34,15 @@
#include "debug/Loader.hh"
ObjectFile *
RawObject::tryFile(const std::string &fname, int fd, size_t len, uint8_t *data)
RawObject::tryFile(const std::string &fname, size_t len, uint8_t *data)
{
return new RawObject(fname, fd, len, data, ObjectFile::UnknownArch,
return new RawObject(fname, len, data, ObjectFile::UnknownArch,
ObjectFile::UnknownOpSys);
}
RawObject::RawObject(const std::string &_filename, int _fd, size_t _len,
RawObject::RawObject(const std::string &_filename, size_t _len,
uint8_t *_data, Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
: ObjectFile(_filename, _len, _data, _arch, _opSys)
{
text.baseAddr = 0;
text.size = len;

View file

@ -36,7 +36,7 @@
class RawObject: public ObjectFile
{
protected:
RawObject(const std::string &_filename, int _fd, size_t _len,
RawObject(const std::string &_filename, size_t _len,
uint8_t *_data, Arch _arch, OpSys _opSys);
public:
virtual ~RawObject() {}
@ -46,7 +46,7 @@ class RawObject: public ObjectFile
virtual bool loadLocalSymbols(SymbolTable *symtab, Addr addrMask =
std::numeric_limits<Addr>::max());
static ObjectFile *tryFile(const std::string &fname, int fd, size_t len,
static ObjectFile *tryFile(const std::string &fname, size_t len,
uint8_t *data);
};