arch,base,cpu,kern,sim: Encapsulate symbols in a class.
[gem5.git] / src / base / loader / object_file.cc
index da5aa9552e0a3b8d928f31fbeb7ab1d34217b2b5..12e5606755184ad4e506b46a23f560a216ab8172 100644 (file)
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Nathan Binkert
- *          Steve Reinhardt
  */
 
-#include <list>
-#include <string>
-
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-
-#include "base/cprintf.hh"
 #include "base/loader/object_file.hh"
-#include "base/loader/symtab.hh"
-
-#include "base/loader/ecoff_object.hh"
-#include "base/loader/aout_object.hh"
-#include "base/loader/elf_object.hh"
-#include "base/loader/raw_object.hh"
-
-#include "mem/translating_port.hh"
-
-using namespace std;
 
-ObjectFile::ObjectFile(const string &_filename, int _fd,
-                       size_t _len, uint8_t *_data,
-                       Arch _arch, OpSys _opSys)
-    : filename(_filename), descriptor(_fd), fileData(_data), len(_len),
-      arch(_arch), opSys(_opSys)
-{
-}
+#include <string>
+#include <vector>
 
+#include "base/loader/raw_image.hh"
 
-ObjectFile::~ObjectFile()
+namespace Loader
 {
-    close();
-}
 
+ObjectFile::ObjectFile(ImageFileDataPtr ifd) : ImageFile(ifd) {}
 
-bool
-ObjectFile::loadSection(Section *sec, Port *memPort, Addr addrMask)
+namespace
 {
-    if (sec->size != 0) {
-        Addr addr = sec->baseAddr & addrMask;
-        if (sec->fileImage) {
-            memPort->writeBlob(addr, sec->fileImage, sec->size);
-        }
-        else {
-            // no image: must be bss
-            memPort->memsetBlob(addr, 0, sec->size);
-        }
-    }
-    return true;
-}
 
+typedef std::vector<ObjectFileFormat *> ObjectFileFormatList;
 
-bool
-ObjectFile::loadSections(Port *memPort, Addr addrMask)
+ObjectFileFormatList &
+object_file_formats()
 {
-    return (loadSection(&text, memPort, addrMask)
-            && loadSection(&data, memPort, addrMask)
-            && loadSection(&bss, memPort, addrMask));
+    static ObjectFileFormatList formats;
+    return formats;
 }
 
+} // anonymous namespace
 
-void
-ObjectFile::close()
+ObjectFileFormat::ObjectFileFormat()
 {
-    if (descriptor >= 0) {
-        ::close(descriptor);
-        descriptor = -1;
-    }
-
-    if (fileData) {
-        ::munmap(fileData, len);
-        fileData = NULL;
-    }
+    object_file_formats().emplace_back(this);
 }
 
-
 ObjectFile *
-createObjectFile(const string &fname, bool raw)
+createObjectFile(const std::string &fname, bool raw)
 {
-    // open the file
-    int fd = open(fname.c_str(), O_RDONLY);
-    if (fd < 0) {
-        return NULL;
-    }
-
-    // find the length of the file by seeking to the end
-    size_t len = (size_t)lseek(fd, 0, SEEK_END);
-
-    // mmap the whole shebang
-    uint8_t *fileData =
-        (uint8_t *)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
-    if (fileData == MAP_FAILED) {
-        close(fd);
-        return NULL;
-    }
+    ImageFileDataPtr ifd(new ImageFileData(fname));
 
-    ObjectFile *fileObj = NULL;
-
-    // figure out what we have here
-    if ((fileObj = EcoffObject::tryFile(fname, fd, len, fileData)) != NULL) {
-        return fileObj;
-    }
-
-    if ((fileObj = AoutObject::tryFile(fname, fd, len, fileData)) != NULL) {
-        return fileObj;
-    }
-
-    if ((fileObj = ElfObject::tryFile(fname, fd, len, fileData)) != NULL) {
-        return fileObj;
+    for (auto &format: object_file_formats()) {
+        ObjectFile *file_obj = format->load(ifd);
+        if (file_obj)
+            return file_obj;
     }
 
     if (raw)
-        return RawObject::tryFile(fname, fd, len, fileData);
+        return new RawImage(ifd);
 
-    // don't know what it is
-    close(fd);
-    munmap(fileData, len);
-    return NULL;
+    return nullptr;
 }
 
-bool
-ObjectFile::isDynamic()
-{
-    return false;
-}
+} // namespace Loader