From 571f43ec01f49a9630b10c4c3c8a224f0aeb01ee Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 24 Oct 2012 23:40:31 +0000 Subject: A little more ELF loader logic git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5253 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/binfmt/libelf/libelf_verify.c | 42 ++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'nuttx/binfmt/libelf/libelf_verify.c') diff --git a/nuttx/binfmt/libelf/libelf_verify.c b/nuttx/binfmt/libelf/libelf_verify.c index 9139700b3..0e0dd8e1a 100644 --- a/nuttx/binfmt/libelf/libelf_verify.c +++ b/nuttx/binfmt/libelf/libelf_verify.c @@ -42,7 +42,7 @@ #include #include #include -#include + #include /**************************************************************************** @@ -53,6 +53,8 @@ * Private Constant Data ****************************************************************************/ +static const char g_elfmagic[EI_MAGIC_SIZE] = { 0x7f, 'E', 'L', 'F' } + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -72,17 +74,47 @@ * 0 (OK) is returned on success and a negated errno is returned on * failure. * + * -ENOEXEC : Not an ELF file + * -EINVALID : Not a relocatable ELF file or not supported by the current, + * configured architecture. + * ****************************************************************************/ -int elf_verifyheader(const Elf32_Ehdr *header) +int elf_verifyheader(const Elf32_Ehdr *ehdr) { - if (!header) + if (!ehdr) { bdbg("NULL ELF header!"); return -ENOEXEC; } -#warning "Missing Logic" - return -ENOSYS; + /* Verify that the magic number indicates an ELF file */ + + if (memcmp(ehdr->e_ident, g_elfmagic, EI_MAGIC_SIZE) != 0) + { + bvdbg("Not ELF magic {%02x, %02x, %02x, %02x}\n", + ehdr->e_ident[0], ehdr->e_ident[1], ehdr->e_ident[2], ehdr->e_ident[3]); + return -ENOEXEC; + } + + /* Verify that this is a relocatable file */ + + if (ehdr->e_type != ET_REL) + { + bdbg("Not a relocatable file: e_type=%d\n", ehdr->e_type); + return -EINVALID; + } + + /* Verify that this file works with the currently configured architecture */ + + if (arch_checkarch(ehdr)) + { + bdbg("Not a supported architecture\n"); + return -ENOEXEC; + } + + /* Looks good so far... we still might find some problems later. */ + + return OK; } -- cgit v1.2.3