diff options
author | Tony Puccinelli | 2010-07-13 06:48:43 +0000 |
---|---|---|
committer | Tony Puccinelli | 2010-07-13 06:48:43 +0000 |
commit | 4d92cefb2fd146d4d56b23dd9d11fb5a30899b2d (patch) | |
tree | 8f6e35cdb436341f9fa228f83fae244ccae390e7 /backends/platform/ds | |
parent | 9d06726b6904f57ca62f3ff89e8c609acbe13ee2 (diff) | |
download | scummvm-rg350-4d92cefb2fd146d4d56b23dd9d11fb5a30899b2d.tar.gz scummvm-rg350-4d92cefb2fd146d4d56b23dd9d11fb5a30899b2d.tar.bz2 scummvm-rg350-4d92cefb2fd146d4d56b23dd9d11fb5a30899b2d.zip |
Changed loader to use Common::SeekableReadStream (compiles but doesn't run and needs polishing)
svn-id: r50843
Diffstat (limited to 'backends/platform/ds')
-rw-r--r-- | backends/platform/ds/arm9/source/dsloader.cpp | 77 | ||||
-rw-r--r-- | backends/platform/ds/arm9/source/dsloader.h | 18 |
2 files changed, 45 insertions, 50 deletions
diff --git a/backends/platform/ds/arm9/source/dsloader.cpp b/backends/platform/ds/arm9/source/dsloader.cpp index ad5160a5b2..595b4fbc54 100644 --- a/backends/platform/ds/arm9/source/dsloader.cpp +++ b/backends/platform/ds/arm9/source/dsloader.cpp @@ -33,12 +33,8 @@ #include <unistd.h> #include <sys/_default_fcntl.h> -#include "common/str.h" -#include "common/util.h" -#include "backends/fs/stdiostream.h" +//#include "backends/fs/stdiostream.h" #include "backends/fs/ds/ds-fs.h" -#include "dsmain.h" -#include "fat/gba_nds_fat.h" #include "backends/platform/ds/arm9/source/dsloader.h" @@ -71,12 +67,12 @@ void DLObject::unload() { /** * Follow the instruction of a relocation section. * - * @param fd File Descriptor + * @param DLFile SeekableReadStream of File * @param offset Offset into the File * @param size Size of relocation section * */ -bool DLObject::relocate(int fd, unsigned long offset, unsigned long size, void *relSegment) { +bool DLObject::relocate(Common::SeekableReadStream* DLFile, unsigned long offset, unsigned long size, void *relSegment) { Elf32_Rela *rela; //relocation entry // Allocate memory for relocation table @@ -86,8 +82,8 @@ bool DLObject::relocate(int fd, unsigned long offset, unsigned long size, void * } // Read in our relocation table - if (lseek(fd, offset, SEEK_SET) < 0 || - read(fd, rela, size) != (ssize_t)size) { + if (DLFile->seek(offset, SEEK_SET) < 0 || + DLFile->read(rela, size) != (ssize_t)size) { seterror("Relocation table load failed."); free(rela); return false; @@ -121,10 +117,10 @@ bool DLObject::relocate(int fd, unsigned long offset, unsigned long size, void * return true; } -bool DLObject::readElfHeader(int fd, Elf32_Ehdr *ehdr) { +bool DLObject::readElfHeader(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr) { // Start reading the elf header. Check for errors - if (read(fd, ehdr, sizeof(*ehdr)) != sizeof(*ehdr) || + if (DLFile->read(ehdr, sizeof(*ehdr)) != sizeof(*ehdr) || memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || // Check MAGIC ehdr->e_type != ET_EXEC || // Check for executable ehdr->e_machine != EM_ARM || // Check for ARM machine type @@ -140,11 +136,11 @@ bool DLObject::readElfHeader(int fd, Elf32_Ehdr *ehdr) { return true; } -bool DLObject::readProgramHeaders(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, int num) { +bool DLObject::readProgramHeaders(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, int num) { // Read program header - if (lseek(fd, ehdr->e_phoff + sizeof(*phdr)*num, SEEK_SET) < 0 || - read(fd, phdr, sizeof(*phdr)) != sizeof(*phdr)) { + if (DLFile->seek(ehdr->e_phoff + sizeof(*phdr)*num, SEEK_SET) < 0 || + DLFile->read(phdr, sizeof(*phdr)) != sizeof(*phdr)) { seterror("Program header load failed."); return false; } @@ -162,7 +158,7 @@ bool DLObject::readProgramHeaders(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, in } -bool DLObject::loadSegment(int fd, Elf32_Phdr *phdr) { +bool DLObject::loadSegment(Common::SeekableReadStream* DLFile, Elf32_Phdr *phdr) { char *baseAddress = 0; @@ -186,8 +182,8 @@ bool DLObject::loadSegment(int fd, Elf32_Phdr *phdr) { memset(baseAddress + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz); } // Read the segment into memory - if (lseek(fd, phdr->p_offset, SEEK_SET) < 0 || - read(fd, baseAddress, phdr->p_filesz) != (ssize_t)phdr->p_filesz) { + if (DLFile->seek(phdr->p_offset, SEEK_SET) < 0 || + DLFile->read(baseAddress, phdr->p_filesz) != (ssize_t)phdr->p_filesz) { seterror("Segment load failed."); return false; } @@ -195,7 +191,7 @@ bool DLObject::loadSegment(int fd, Elf32_Phdr *phdr) { return true; } -Elf32_Shdr * DLObject::loadSectionHeaders(int fd, Elf32_Ehdr *ehdr) { +Elf32_Shdr * DLObject::loadSectionHeaders(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr) { Elf32_Shdr *shdr = NULL; @@ -206,8 +202,8 @@ Elf32_Shdr * DLObject::loadSectionHeaders(int fd, Elf32_Ehdr *ehdr) { } // Read from file into section headers - if (lseek(fd, ehdr->e_shoff, SEEK_SET) < 0 || - read(fd, shdr, ehdr->e_shnum * sizeof(*shdr)) != + if (DLFile->seek(ehdr->e_shoff, SEEK_SET) < 0 || + DLFile->read(shdr, ehdr->e_shnum * sizeof(*shdr)) != (ssize_t)(ehdr->e_shnum * sizeof(*shdr))) { seterror("Section headers load failed."); return NULL; @@ -216,7 +212,7 @@ Elf32_Shdr * DLObject::loadSectionHeaders(int fd, Elf32_Ehdr *ehdr) { return shdr; } -int DLObject::loadSymbolTable(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { +int DLObject::loadSymbolTable(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { // Loop over sections, looking for symbol table linked to a string table for (int i = 0; i < ehdr->e_shnum; i++) { @@ -244,8 +240,8 @@ int DLObject::loadSymbolTable(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { } // Read symbol table into memory - if (lseek(fd, shdr[_symtab_sect].sh_offset, SEEK_SET) < 0 || - read(fd, _symtab, shdr[_symtab_sect].sh_size) != + if (DLFile->seek(shdr[_symtab_sect].sh_offset, SEEK_SET) < 0 || + DLFile->read(_symtab, shdr[_symtab_sect].sh_size) != (ssize_t)shdr[_symtab_sect].sh_size) { seterror("Symbol table load failed."); return -1; @@ -259,7 +255,7 @@ int DLObject::loadSymbolTable(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { } -bool DLObject::loadStringTable(int fd, Elf32_Shdr *shdr) { +bool DLObject::loadStringTable(Common::SeekableReadStream* DLFile, Elf32_Shdr *shdr) { int string_sect = shdr[_symtab_sect].sh_link; @@ -270,8 +266,8 @@ bool DLObject::loadStringTable(int fd, Elf32_Shdr *shdr) { } // Read string table into memory - if (lseek(fd, shdr[string_sect].sh_offset, SEEK_SET) < 0 || - read(fd, _strtab, shdr[string_sect].sh_size) != + if (DLFile->seek(shdr[string_sect].sh_offset, SEEK_SET) < 0 || + DLFile->read(_strtab, shdr[string_sect].sh_size) != (ssize_t)shdr[string_sect].sh_size) { seterror("Symbol table strings load failed."); return false; @@ -302,7 +298,7 @@ void DLObject::relocateSymbols(Elf32_Addr offset) { DBG("Relocated %d symbols.\n",relocCount); } -bool DLObject::relocateRels(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { +bool DLObject::relocateRels(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { // Loop over sections, finding relocation sections for (int i = 0; i < ehdr->e_shnum; i++) { @@ -316,7 +312,7 @@ bool DLObject::relocateRels(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { curShdr->sh_info < ehdr->e_shnum && // Check that the relocated section exists (shdr[curShdr->sh_info].sh_flags & SHF_ALLOC)) { // Check if relocated section resides in memory - if (!relocate(fd, curShdr->sh_offset, curShdr->sh_size, _segment)) { + if (!relocate(DLFile, curShdr->sh_offset, curShdr->sh_size, _segment)) { return false; } @@ -326,7 +322,7 @@ bool DLObject::relocateRels(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) { return true; } -bool DLObject::load(int fd) { +bool DLObject::load(Common::SeekableReadStream* DLFile) { Elf32_Ehdr ehdr; Elf32_Phdr phdr; Elf32_Shdr *shdr; @@ -334,7 +330,7 @@ bool DLObject::load(int fd) { //int symtab_sect = -1; - if (readElfHeader(fd, &ehdr) == false) { + if (readElfHeader(DLFile, &ehdr) == false) { return false; } @@ -342,26 +338,26 @@ bool DLObject::load(int fd) { DBG("Loading segment %d\n", i); - if (readProgramHeaders(fd, &ehdr, &phdr, i) == false) + if (readProgramHeaders(DLFile, &ehdr, &phdr, i) == false) return false; - if (!loadSegment(fd, &phdr)) + if (!loadSegment(DLFile, &phdr)) return false; } - if ((shdr = loadSectionHeaders(fd, &ehdr)) == NULL) + if ((shdr = loadSectionHeaders(DLFile, &ehdr)) == NULL) ret = false; - if (ret && ((_symtab_sect = loadSymbolTable(fd, &ehdr, shdr)) < 0)) + if (ret && ((_symtab_sect = loadSymbolTable(DLFile, &ehdr, shdr)) < 0)) ret = false; - if (ret && (loadStringTable(fd, shdr) == false)) + if (ret && (loadStringTable(DLFile, shdr) == false)) ret = false; if (ret) relocateSymbols((Elf32_Addr)_segment); // Offset by our segment allocated address - if (ret && (relocateRels(fd, &ehdr, shdr) == false)) + if (ret && (relocateRels(DLFile, &ehdr, shdr) == false)) ret = false; free(shdr); @@ -371,9 +367,8 @@ bool DLObject::load(int fd) { } bool DLObject::open(const char *path) { - int fd; - Common::SeekableReadStream* DLFile; //TODO: reimplement everything with SeekableReadStream instead of fd + Common::SeekableReadStream* DLFile; void *ctors_start, *ctors_end; DBG("open(\"%s\")\n", path); @@ -388,13 +383,13 @@ bool DLObject::open(const char *path) { DBG("%s found!\n", path); /*Try to load and relocate*/ - if (!load(fd)) { - ::close(fd); + if (!load(DLFile)) { + //DLFile->finalize(); unload(); return false; } - ::close(fd); + //DLFile->finalize(); //TODO: flush data cache diff --git a/backends/platform/ds/arm9/source/dsloader.h b/backends/platform/ds/arm9/source/dsloader.h index f8a3cdefa4..ddb85900e8 100644 --- a/backends/platform/ds/arm9/source/dsloader.h +++ b/backends/platform/ds/arm9/source/dsloader.h @@ -44,17 +44,17 @@ class DLObject { void seterror(const char *fmt, ...); void unload(); - bool relocate(int fd, unsigned long offset, unsigned long size, void *relSegment); - bool load(int fd); + bool relocate(Common::SeekableReadStream* DLFile, unsigned long offset, unsigned long size, void *relSegment); + bool load(Common::SeekableReadStream* DLFile); - bool readElfHeader(int fd, Elf32_Ehdr *ehdr); - bool readProgramHeaders(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, int num); - bool loadSegment(int fd, Elf32_Phdr *phdr); - Elf32_Shdr *loadSectionHeaders(int fd, Elf32_Ehdr *ehdr); - int loadSymbolTable(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr); - bool loadStringTable(int fd, Elf32_Shdr *shdr); + 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(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr); + bool relocateRels(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr); public: bool open(const char *path); |