diff --git a/emulator/Loader/Elf.cpp b/emulator/Loader/Elf.cpp index 957eadf3..dc6c9c15 100644 --- a/emulator/Loader/Elf.cpp +++ b/emulator/Loader/Elf.cpp @@ -98,6 +98,12 @@ void Elf::Open(const std::string& file_name) m_elf_header = load_elf_header(*m_f); + if (!isElfFile()) + { + delete m_elf_header; + m_elf_header = nullptr; + } + DebugDump(); } @@ -134,6 +140,27 @@ bool Elf::isSelfFile() const return true; } +bool Elf::isElfFile() const +{ + if (m_f == nullptr) + { + return false; + } + + if (m_elf_header == nullptr) + { + return false; + } + if (m_elf_header->e_ident[0] != '\x7f' || m_elf_header->e_ident[1] != '\x45' || m_elf_header->e_ident[2] != '\x4c' || + m_elf_header->e_ident[3] != '\x46') + { + printf("Not an ELF file magic is wrong!\n"); + return false; + } + + return true; +} + void Elf::DebugDump() { printf("SELF header:\n"); printf(" magic ..............: 0x%08" PRIx32 "\n", m_self->magic); diff --git a/emulator/Loader/Elf.h b/emulator/Loader/Elf.h index 1561d47d..5740535f 100644 --- a/emulator/Loader/Elf.h +++ b/emulator/Loader/Elf.h @@ -95,6 +95,7 @@ public: void Open(const std::string & file_name); bool isSelfFile() const; + bool isElfFile() const; void DebugDump(); private: