Jelajahi Sumber

:new: 检测是否为elf文件

fslongjin 2 tahun lalu
induk
melakukan
b0a7ec633d
2 mengubah file dengan 41 tambahan dan 2 penghapusan
  1. 33 1
      kernel/common/libELF/elf.c
  2. 8 1
      kernel/common/libELF/elf.h

+ 33 - 1
kernel/common/libELF/elf.c

@@ -1 +1,33 @@
-#include "elf.h"
+#include "elf.h"
+#include <common/unistd.h>
+#include <common/glib.h>
+
+/**
+ * @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;
+}
+

+ 8 - 1
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 =========
+// --> end ========== program header =========
+
+/**
+ * @brief 校验是否为ELF文件
+ * 
+ * @param ehdr 
+ */
+bool elf_check(void * ehdr);