aboutsummaryrefslogtreecommitdiff
path: root/backends/plugins
diff options
context:
space:
mode:
authorTony Puccinelli2010-07-23 07:49:02 +0000
committerTony Puccinelli2010-07-23 07:49:02 +0000
commit7694809b3de7dade9c9b950a046e89f194058248 (patch)
tree33d8a4cf3323f2c78ae227d85eafe399839dc1e5 /backends/plugins
parent90e9282bf14180ae95f942678dc95849c210c03c (diff)
downloadscummvm-rg350-7694809b3de7dade9c9b950a046e89f194058248.tar.gz
scummvm-rg350-7694809b3de7dade9c9b950a046e89f194058248.tar.bz2
scummvm-rg350-7694809b3de7dade9c9b950a046e89f194058248.zip
Began abstraction of generic ELF-loader
svn-id: r51195
Diffstat (limited to 'backends/plugins')
-rw-r--r--backends/plugins/MIPS-loader.h43
-rw-r--r--backends/plugins/loader.h81
-rw-r--r--backends/plugins/shorts-segment-manager.cpp78
-rw-r--r--backends/plugins/shorts-segment-manager.h91
4 files changed, 293 insertions, 0 deletions
diff --git a/backends/plugins/MIPS-loader.h b/backends/plugins/MIPS-loader.h
new file mode 100644
index 0000000000..3bc482e4f2
--- /dev/null
+++ b/backends/plugins/MIPS-loader.h
@@ -0,0 +1,43 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef MIPS_LOADER_H
+#define MIPS_LOADER_H
+
+#include "loader.h"
+#include "shorts-segment-manager.h"
+
+class MIPS_DLObject : public DLObject {
+protected:
+ ShortSegmentManager::Segment *_shortsSegment; // For assigning shorts ranges
+ unsigned int _gpVal; // Value of Global Pointer
+
+public:
+ MIPS_DLObject(char *errbuf = NULL) : _errbuf(_errbuf), _shortsSegment(NULL), _segment(NULL), _symtab(NULL),
+ _strtab(NULL), _symbol_cnt(0), _symtab_sect(-1), _dtors_start(NULL), _dtors_end(NULL), _gpVal(0) ,
+ _segmentSize(0) {}
+};
+
+#endif /* MIPS_LOADER_H */
diff --git a/backends/plugins/loader.h b/backends/plugins/loader.h
new file mode 100644
index 0000000000..60305f3493
--- /dev/null
+++ b/backends/plugins/loader.h
@@ -0,0 +1,81 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef LOADER_H
+#define LOADER_H
+
+#include "elf32.h"
+#include "common/list.h"
+
+#define MAXDLERRLEN 80
+
+class DLObject {
+protected:
+ char *_errbuf; /* For error messages, at least MAXDLERRLEN in size */
+
+ void *_segment, *_symtab;
+ char *_strtab;
+ int _symbol_cnt;
+ int _symtab_sect;
+ void *_dtors_start, *_dtors_end;
+
+ int _segmentSize;
+
+ void seterror(const char *fmt, ...);
+ void unload();
+ bool relocate(Common::SeekableReadStream* DLFile, unsigned long offset, unsigned long size, void *relSegment);
+ bool load(Common::SeekableReadStream* DLFile);
+
+ bool readElfHeader(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr);
+ bool readProgramHeaders(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, int num);
+ bool loadSegment(Common::SeekableReadStream* DLFile, Elf32_Phdr *phdr);
+ Elf32_Shdr *loadSectionHeaders(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr);
+ int loadSymbolTable(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr);
+ bool loadStringTable(Common::SeekableReadStream* DLFile, Elf32_Shdr *shdr);
+ void relocateSymbols(Elf32_Addr offset);
+ bool relocateRels(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr);
+
+public:
+ bool open(const char *path);
+ bool close();
+ void *symbol(const char *name);
+ void discard_symtab();
+
+ DLObject(char *errbuf = NULL) : _errbuf(_errbuf), _segment(NULL), _symtab(NULL),
+ _strtab(NULL), _symbol_cnt(0), _symtab_sect(-1), _dtors_start(NULL), _dtors_end(NULL),
+ _segmentSize(0) {}
+};
+
+#define RTLD_LAZY 0
+
+extern "C" {
+ void *dlopen(const char *filename, int flags);
+ int dlclose(void *handle);
+ void *dlsym(void *handle, const char *symbol);
+ const char *dlerror();
+ void dlforgetsyms(void *handle);
+}
+
+#endif /* LOADER_H */
diff --git a/backends/plugins/shorts-segment-manager.cpp b/backends/plugins/shorts-segment-manager.cpp
new file mode 100644
index 0000000000..dd2b4e4a7e
--- /dev/null
+++ b/backends/plugins/shorts-segment-manager.cpp
@@ -0,0 +1,78 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#if defined(DYNAMIC_MODULES) //TODO: && defined (MIPS target)
+
+#include "MIPS-loader.h"
+#include "shorts-segment-manager.h"
+
+DECLARE_SINGLETON(ShortSegmentManager); // For singleton
+
+ShortSegmentManager::ShortSegmentManager() {
+ _shortsStart = &__plugin_hole_start ;
+ _shortsEnd = &__plugin_hole_end;
+}
+
+ShortSegmentManager::Segment *ShortSegmentManager::newSegment(int size, char *origAddr) {
+ char *lastAddress = origAddr;
+ Common::List<Segment *>::iterator i;
+
+ // Find a block that fits, starting from the beginning
+ for (i = _list.begin(); i != _list.end(); ++i) {
+ char *currAddress = (*i)->getStart();
+
+ if ((int)(currAddress - lastAddress) >= size) break;
+
+ lastAddress = (*i)->getEnd();
+ }
+
+ if ((Elf32_Addr)lastAddress & 3)
+ lastAddress += 4 - ((Elf32_Addr)lastAddress & 3); // Round up to multiple of 4
+
+ if (lastAddress + size > _shortsEnd) {
+ seterror("Error. No space in shorts segment for %x bytes. Last address is %p, max address is %p.\n",
+ size, lastAddress, _shortsEnd);
+ return NULL;
+ }
+
+ Segment *seg = new Segment(lastAddress, size, origAddr); // Create a new segment
+
+ if (lastAddress + size > _highestAddress) _highestAddress = lastAddress + size; // Keep track of maximum
+
+ _list.insert(i, seg);
+
+ DBG("Shorts segment size %x allocated. End = %p. Remaining space = %x. Highest so far is %p.\n",
+ size, lastAddress + size, _shortsEnd - _list.back()->getEnd(), _highestAddress);
+
+ return seg;
+}
+
+void ShortSegmentManager::deleteSegment(ShortSegmentManager::Segment *seg) {
+ DBG("Deleting shorts segment from %p to %p.\n\n", seg->getStart(), seg->getEnd());
+ _list.remove(seg);
+ delete seg;
+}
+
+#endif /* DYNAMIC_MODULES */
diff --git a/backends/plugins/shorts-segment-manager.h b/backends/plugins/shorts-segment-manager.h
new file mode 100644
index 0000000000..e6c38d45a8
--- /dev/null
+++ b/backends/plugins/shorts-segment-manager.h
@@ -0,0 +1,91 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef SHORTS_SEGMENT_MANAGER_H
+#define SHORTS_SEGMENT_MANAGER_H
+
+#include "loader.h"
+#include "common/singleton.h"
+
+#define ShortsMan ShortSegmentManager::instance()
+
+class ShortSegmentManager : public Common::Singleton<ShortSegmentManager> {
+private:
+ char *_shortsStart;
+ char *_shortsEnd;
+
+public:
+ char *getShortsStart() {
+ return _shortsStart;
+ }
+ bool inGeneralSegment(char *addr) {
+ return ((char *)addr >= _shortsStart && (char *)addr < _shortsEnd);
+ }
+
+ class Segment {
+ private:
+ friend class ShortSegmentManager;
+ Segment(char *start, int size, char *origAddr) : _startAddress(start), _size(size), _origAddress(origAddr) {}
+ ~Segment() {}
+ char *_startAddress; // Start of shorts segment in memory
+ int _size; // Size of shorts segment
+ char *_origAddress; // Original address this segment was supposed to be at
+ public:
+ char *getStart() {
+ return _startAddress;
+ }
+ char *getEnd() {
+ return (_startAddress + _size);
+ }
+ Elf32_Addr getOffset() {
+ return (Elf32_Addr)(_startAddress - _origAddress);
+ }
+ bool inSegment(char *addr) {
+ return ((char *)addr >= _startAddress && (char *)addr <= _startAddress + _size);
+ }
+ };
+
+ Segment *newSegment(int size, char *origAddr);
+ void deleteSegment(Segment *);
+
+private:
+ ShortSegmentManager();
+ friend class Common::Singleton<ShortSegmentManager>;
+ Common::List<Segment *> _list;
+ char *_highestAddress;
+};
+
+class MIPS_DLObject : public DLObject {
+protected:
+ ShortSegmentManager::Segment *_shortsSegment; // For assigning shorts ranges
+ unsigned int _gpVal; // Value of Global Pointer
+
+public:
+ MIPS_DLObject(char *errbuf = NULL) : _errbuf(_errbuf), _shortsSegment(NULL), _segment(NULL), _symtab(NULL),
+ _strtab(NULL), _symbol_cnt(0), _symtab_sect(-1), _dtors_start(NULL), _dtors_end(NULL), _gpVal(0) ,
+ _segmentSize(0) {}
+};
+
+#endif /* SHORTS_SEGMENT_MANAGER_H */