From b0a7ec633de3e5ee5b03e5b006e9775dc946c765 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Tue, 17 May 2022 22:52:13 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=A3=80=E6=B5=8B=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E4=B8=BAelf=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/common/libELF/elf.c | 34 +++++++++++++++++++++++++++++++++- kernel/common/libELF/elf.h | 9 ++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/kernel/common/libELF/elf.c b/kernel/common/libELF/elf.c index 1e65ce4c..01c1d867 100644 --- a/kernel/common/libELF/elf.c +++ b/kernel/common/libELF/elf.c @@ -1 +1,33 @@ -#include "elf.h" \ No newline at end of file +#include "elf.h" +#include +#include + +/** + * @brief 校验是否为ELF文件 + * + * @param ehdr + */ +bool elf_check(void *ehdr) +{ + Elf32_Ehdr *ptr = (Elf32_Ehdr *)ehdr; + bool flag = ptr->e_ident[EI_MAG0] == ELFMAG0 && ptr->e_ident[EI_MAG1] == ELFMAG1 && ptr->e_ident[EI_MAG2] == ELFMAG2 && ptr->e_ident[EI_MAG3] == ELFMAG3; + + // 标头已经不符合要求 + if (!flag) + return false; + + // 检验EI_CLASS是否合法 + if (ptr->e_ident[EI_CLASS] == 0 || ptr->e_ident[EI_CLASS] > 2) + return false; + + // 检验EI_DATA是否合法 + if (ptr->e_ident[EI_DATA] == 0 || ptr->e_ident[EI_DATA] > 2) + return false; + + // 检验EI_VERSION是否合法 + if(ptr->e_ident[EI_VERSION]==EV_NONE) + return false; + // 是elf文件 + return true; +} + diff --git a/kernel/common/libELF/elf.h b/kernel/common/libELF/elf.h index 8db37bea..275ca482 100644 --- a/kernel/common/libELF/elf.h +++ b/kernel/common/libELF/elf.h @@ -357,4 +357,11 @@ Values in this inclusive range are reserved for OS-specific semantics. #define PF_MASKPROC 0xf0000000 // Unspecified -// --> end ========== program header ========= \ No newline at end of file +// --> end ========== program header ========= + +/** + * @brief 校验是否为ELF文件 + * + * @param ehdr + */ +bool elf_check(void * ehdr); \ No newline at end of file