aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backends/plugins/elf-loader.cpp51
-rw-r--r--backends/plugins/elf32.h14
2 files changed, 55 insertions, 10 deletions
diff --git a/backends/plugins/elf-loader.cpp b/backends/plugins/elf-loader.cpp
index 053b8120ad..b058f06e86 100644
--- a/backends/plugins/elf-loader.cpp
+++ b/backends/plugins/elf-loader.cpp
@@ -80,19 +80,54 @@ void DLObject::unload() {
}
bool DLObject::readElfHeader(Common::SeekableReadStream* DLFile, Elf32_Ehdr *ehdr) {
- // Start reading the elf header. Check for errors
+ // Start reading the elf header. Check for errors and magic
if (DLFile->read(ehdr, sizeof(*ehdr)) != sizeof(*ehdr) ||
- memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || // Check MAGIC
- ehdr->e_type != ET_EXEC || // Check for executable
+ memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
+ warning("elfloader: No ELF file.");
+ return false;
+ }
+
+ if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
+ warning("elfloader: Wrong ELF file class.");
+ return false;
+ }
+
+ if (ehdr->e_ident[EI_DATA] !=
+#ifdef SCUMM_BIG_ENDIAN
+ ELFDATA2MSB
+#else
+ ELFDATA2LSB
+#endif
+ ) {
+ warning("elfloader: Wrong ELF file endianess.");
+ return false;
+ }
+
+ if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
+ warning("elfloader: Wrong ELF file version.");
+ return false;
+ }
+
+ if (ehdr->e_type != ET_EXEC) {
+ warning("elfloader: No executable ELF file.");
+ return false;
+ }
+
+ if (ehdr->e_machine !=
#ifdef ARM_TARGET
- ehdr->e_machine != EM_ARM || // Check for ARM machine type
+ EM_ARM
#endif
#ifdef MIPS_TARGET
- ehdr->e_machine != EM_MIPS || // Check for MIPS machine type
+ EM_MIPS
#endif
- ehdr->e_phentsize < sizeof(Elf32_Phdr) || // Check for size of program header
- ehdr->e_shentsize != sizeof(Elf32_Shdr)) { // Check for size of section header
- warning("elfloader: Invalid file type.");
+ ) {
+ warning("elfloader: Wrong ELF file architecture.");
+ return false;
+ }
+
+ if (ehdr->e_phentsize < sizeof(Elf32_Phdr) || // Check for size of program header
+ ehdr->e_shentsize != sizeof(Elf32_Shdr)) { // Check for size of section header
+ warning("elfloader: Invalid ELF structure sizes.");
return false;
}
diff --git a/backends/plugins/elf32.h b/backends/plugins/elf32.h
index ef6b714a5e..f18f4090a2 100644
--- a/backends/plugins/elf32.h
+++ b/backends/plugins/elf32.h
@@ -38,7 +38,7 @@ typedef signed int Elf32_Sword;
typedef Elf32_Half Elf32_Versym;
#define EI_NIDENT (16)
-#define SELFMAG 6
+#define SELFMAG 4
/* ELF File format structures. Look up ELF structure for more details */
@@ -61,7 +61,17 @@ typedef struct {
} Elf32_Ehdr;
// Should be in e_ident
-#define ELFMAG "\177ELF\1\1" /* ELF Magic number */
+#define ELFMAG "\177ELF" /* ELF Magic number */
+
+#define EI_CLASS 4 /* File class byte index */
+#define ELFCLASS32 1 /* 32-bit objects */
+
+#define EI_DATA 5 /* Data encoding byte index */
+#define ELFDATA2LSB 1 /* 2's complement, little endian */
+#define ELFDATA2MSB 2 /* 2's complement, big endian */
+
+#define EI_VERSION 6
+#define EV_CURRENT 1 /* Current version */
// e_type values
#define ET_NONE 0 /* no file type */