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; using namespace std;
ObjectFile * 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)) { if (!N_BADMAG(*(aout_exechdr *)data)) {
// right now this is only used for Alpha PAL code // 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); ObjectFile::Alpha, ObjectFile::UnknownOpSys);
} }
else { 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, size_t _len, uint8_t *_data,
Arch _arch, OpSys _opSys) Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) : ObjectFile(_filename, _len, _data, _arch, _opSys)
{ {
execHdr = (aout_exechdr *)fileData; execHdr = (aout_exechdr *)fileData;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -34,15 +34,15 @@
#include "debug/Loader.hh" #include "debug/Loader.hh"
ObjectFile * 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); 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) uint8_t *_data, Arch _arch, OpSys _opSys)
: ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) : ObjectFile(_filename, _len, _data, _arch, _opSys)
{ {
text.baseAddr = 0; text.baseAddr = 0;
text.size = len; text.size = len;

View file

@ -36,7 +36,7 @@
class RawObject: public ObjectFile class RawObject: public ObjectFile
{ {
protected: 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); uint8_t *_data, Arch _arch, OpSys _opSys);
public: public:
virtual ~RawObject() {} virtual ~RawObject() {}
@ -46,7 +46,7 @@ class RawObject: public ObjectFile
virtual bool loadLocalSymbols(SymbolTable *symtab, Addr addrMask = virtual bool loadLocalSymbols(SymbolTable *symtab, Addr addrMask =
std::numeric_limits<Addr>::max()); 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); uint8_t *data);
}; };