From e385ae0c72993d1d58256324d0fe425247fad207 Mon Sep 17 00:00:00 2001 From: Curtis Dunham Date: Fri, 3 Jul 2015 10:14:34 -0400 Subject: 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. --- src/base/loader/aout_object.cc | 8 ++++---- src/base/loader/aout_object.hh | 4 ++-- src/base/loader/dtb_object.cc | 14 ++++---------- src/base/loader/dtb_object.hh | 6 ++---- src/base/loader/ecoff_object.cc | 9 ++++----- src/base/loader/ecoff_object.hh | 5 ++--- src/base/loader/elf_object.cc | 11 ++++------- src/base/loader/elf_object.hh | 5 ++--- src/base/loader/object_file.cc | 22 ++++++++-------------- src/base/loader/object_file.hh | 4 +--- src/base/loader/raw_object.cc | 8 ++++---- src/base/loader/raw_object.hh | 4 ++-- 12 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/base/loader/aout_object.cc b/src/base/loader/aout_object.cc index 756f03a9e..07acc8fbd 100644 --- a/src/base/loader/aout_object.cc +++ b/src/base/loader/aout_object.cc @@ -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; diff --git a/src/base/loader/aout_object.hh b/src/base/loader/aout_object.hh index 7bac5be65..110dcfd36 100644 --- a/src/base/loader/aout_object.hh +++ b/src/base/loader/aout_object.hh @@ -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::max()); - static ObjectFile *tryFile(const std::string &fname, int fd, + static ObjectFile *tryFile(const std::string &fname, size_t len, uint8_t *data); }; diff --git a/src/base/loader/dtb_object.cc b/src/base/loader/dtb_object.cc index adf7d2c3a..841db8c12 100644 --- a/src/base/loader/dtb_object.cc +++ b/src/base/loader/dtb_object.cc @@ -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) { diff --git a/src/base/loader/dtb_object.hh b/src/base/loader/dtb_object.hh index 74bb52d44..451b52a4c 100644 --- a/src/base/loader/dtb_object.hh +++ b/src/base/loader/dtb_object.hh @@ -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); }; diff --git a/src/base/loader/ecoff_object.cc b/src/base/loader/ecoff_object.cc index 263085d16..3b9f6ba3f 100644 --- a/src/base/loader/ecoff_object.cc +++ b/src/base/loader/ecoff_object.cc @@ -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); diff --git a/src/base/loader/ecoff_object.hh b/src/base/loader/ecoff_object.hh index ca6fa2dd0..0a5a43f72 100644 --- a/src/base/loader/ecoff_object.hh +++ b/src/base/loader/ecoff_object.hh @@ -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::max()); - static ObjectFile *tryFile(const std::string &fname, int fd, + static ObjectFile *tryFile(const std::string &fname, size_t len, uint8_t *data); }; diff --git a/src/base/loader/elf_object.cc b/src/base/loader/elf_object.cc index ed4fee37a..d1d89f20b 100644 --- a/src/base/loader/elf_object.cc +++ b/src/base/loader/elf_object.cc @@ -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 diff --git a/src/base/loader/elf_object.hh b/src/base/loader/elf_object.hh index 84b73b0a8..969896290 100644 --- a/src/base/loader/elf_object.hh +++ b/src/base/loader/elf_object.hh @@ -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;} diff --git a/src/base/loader/object_file.cc b/src/base/loader/object_file.cc index 29cea8ccf..e24aeddd3 100644 --- a/src/base/loader/object_file.cc +++ b/src/base/loader/object_file.cc @@ -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; } diff --git a/src/base/loader/object_file.hh b/src/base/loader/object_file.hh index 37f2aa585..e5961add2 100644 --- a/src/base/loader/object_file.hh +++ b/src/base/loader/object_file.hh @@ -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: diff --git a/src/base/loader/raw_object.cc b/src/base/loader/raw_object.cc index eb1e06d3f..73d8372ef 100644 --- a/src/base/loader/raw_object.cc +++ b/src/base/loader/raw_object.cc @@ -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; diff --git a/src/base/loader/raw_object.hh b/src/base/loader/raw_object.hh index 9014a2d30..3865425d0 100644 --- a/src/base/loader/raw_object.hh +++ b/src/base/loader/raw_object.hh @@ -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::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); }; -- cgit v1.2.3