From a85be6eb62b9d488183f8282bbdfea3b8135ae32 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 15 Nov 2023 12:50:46 -0500 Subject: [PATCH] elf: add basic elf parsing --- include/errno.h | 1 + include/fs/vfs.h | 7 +++++ include/kernel/elf.h | 69 ++++++++++++++++++++++++++++++++++++++++++++ init/cmd.c | 63 ++++++++++++++++++++++++++++++++++++++-- lib/errno.c | 1 + sched/load.c | 50 ++++++++++++++++++++++++++++++++ 6 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 include/kernel/elf.h create mode 100644 sched/load.c diff --git a/include/errno.h b/include/errno.h index 72d41fa..91fd873 100644 --- a/include/errno.h +++ b/include/errno.h @@ -10,6 +10,7 @@ #define EISDIR 6 #define ENOSPC 7 #define EEXIST 8 +#define ENOEXEC 9 const char *strerror(int errnum); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 6fbb29a..8712928 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -21,6 +21,13 @@ #define SEEK_CUR 1 #define SEEK_END 2 +#define O_RDONLY 0x0001 +#define O_WRONLY 0x0002 +#define O_RDWR 0x0004 +#define O_CREAT 0x0008 +#define O_TRUNC 0x0010 +#define O_APPEND 0x0020 + typedef uint32_t ino_t; typedef uint8_t ftype_t; diff --git a/include/kernel/elf.h b/include/kernel/elf.h new file mode 100644 index 0000000..fb1f8e5 --- /dev/null +++ b/include/kernel/elf.h @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ELF_H_ +#define _ELF_H_ + +#include + +#define ET_NONE 0 +#define ET_REL 1 +#define ET_EXEC 2 + +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_OSABI 7 +#define EI_ABIVERSION 8 +#define EI_PAD 9 + +#define EI_CLASS_64 2 +#define EI_OSABI_SYSV 0 + +#define EM_X86_64 62 + +#define EI_VERSION_CURRENT 1 + +#define ELFCLASS64 2 +#define ELFDATA2LSB 1 /* Little endian, two's complement */ + +struct elf64_header { + uint8_t e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint64_t e_entry; + uint64_t e_phoff; + uint64_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +} PACKED; + +struct elf64_program_header { + uint32_t p_type; + uint32_t p_flags; + uint64_t p_offset; + uint64_t p_vaddr; + uint64_t p_paddr; + uint64_t p_filesz; + uint64_t p_memsz; + uint64_t p_align; +} PACKED; + +struct elf64_section_header { + uint32_t sh_name; + uint32_t sh_type; + uint64_t sh_flags; + uint64_t sh_addr; + uint64_t sh_offset; + uint64_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint64_t sh_addralign; + uint64_t sh_entsize; +} PACKED; + +#endif /* _ELF_H_ */ diff --git a/init/cmd.c b/init/cmd.c index 37b4ce9..21c9e61 100644 --- a/init/cmd.c +++ b/init/cmd.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include #include @@ -17,6 +18,7 @@ int kcmd_basename(int argc, char **argv); int kcmd_dirname(int argc, char **argv); int kcmd_mkdir(int argc, char **argv); int kcmd_unlink(int argc, char **argv); +int kcmd_elf(int argc, char **argv); #define KCMD_DECL(name) \ { \ @@ -28,8 +30,8 @@ struct kcmd { int (*func)(int argc, char **argv); }; -struct kcmd cmd_list[] = { KCMD_DECL(basename), KCMD_DECL(cat), KCMD_DECL(dirname), KCMD_DECL(clear), - KCMD_DECL(help), KCMD_DECL(ls), KCMD_DECL(mkdir), KCMD_DECL(stat), KCMD_DECL(unlink) }; +struct kcmd cmd_list[] = { KCMD_DECL(basename), KCMD_DECL(cat), KCMD_DECL(dirname), KCMD_DECL(clear), KCMD_DECL(help), + KCMD_DECL(ls), KCMD_DECL(mkdir), KCMD_DECL(stat), KCMD_DECL(unlink), KCMD_DECL(elf) }; int kcmd_basename(int argc, char **argv) { @@ -193,6 +195,63 @@ int kcmd_unlink(int argc, char **argv) return 0; } +int kcmd_elf(int argc, char **argv) +{ + /* open file and check magic */ + if (!argv[1] || strempty(argv[1])) { + kprintf("Usage: elf \n"); + return 1; + } + + int fd = open(argv[1], 0); + if (fd < 0) { + kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd)); + return 1; + } + + char *buf = kmalloc(4096, ALLOC_KERN); + if (!buf) + return -ENOMEM; + + int ret = read(fd, buf, 4096); + if (ret < 0) { + kprintf("Failed to read %s: %s\n", argv[1], strerror(-ret)); + close(fd); + return 1; + } + + char elf_magic[] = { 0x7F, 'E', 'L', 'F' }; + if (strncmp(buf, elf_magic, 4)) { + kprintf("Not an ELF file\n"); + close(fd); + return 1; + } + + struct elf64_header *hdr = (struct elf64_header *)buf; + + kprintf("ELF header:\n"); + kprintf(" Magic: %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); + kprintf(" Class: %d\n", hdr->e_ident[EI_CLASS]); + kprintf(" Data: %d\n", hdr->e_ident[EI_DATA]); + kprintf(" Type: %d\n", hdr->e_type); + kprintf(" Machine: %d\n", hdr->e_machine); + kprintf(" Version: %d\n", hdr->e_version); + kprintf(" Entry: %Xh\n", hdr->e_entry); + kprintf(" Program header offset: %d\n", hdr->e_phoff); + kprintf(" Section header offset: %d\n", hdr->e_shoff); + kprintf(" Flags: %d\n", hdr->e_flags); + kprintf(" Header size: %d\n", hdr->e_ehsize); + kprintf(" Program header size: %d\n", hdr->e_phentsize); + kprintf(" Program header count: %d\n", hdr->e_phnum); + kprintf(" Section header size: %d\n", hdr->e_shentsize); + kprintf(" Section header count: %d\n", hdr->e_shnum); + kprintf(" Section header string table index: %d\n", hdr->e_shstrndx); + + close(fd); + + return 0; +} + void kexec(const char *cmd) { int n; diff --git a/lib/errno.c b/lib/errno.c index 8404480..23edbe8 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -12,6 +12,7 @@ static const char *errno_str[] = { [EISDIR] = "Is a directory", [ENOSPC] = "No space left on device", [EEXIST] = "File exists", + [ENOEXEC] = "Exec format error", }; inline const char *strerror(int errnum) diff --git a/sched/load.c b/sched/load.c new file mode 100644 index 0000000..3ea095e --- /dev/null +++ b/sched/load.c @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* ELF loader */ +#include +#include +#include + +#include +#include + +static const char elf_magic[] = { 0x7F, 'E', 'L', 'F' }; + +pid_t elf_load_proc(const char *proc_path) +{ + int fd = open(proc_path, O_RDONLY); + if (fd < 0) + return fd; + + char *buf = kmalloc(4096, ALLOC_KERN); + if (!buf) + return -ENOMEM; + + int ret = read(fd, buf, 4096); + + if (ret < 0) + return ret; + + if(strncmp(buf, elf_magic, 4)) + return -ENOEXEC; + + struct elf64_header *hdr = (struct elf64_header *)buf; + + /* must be 64-bit */ + if (hdr->e_ident[EI_CLASS] != ELFCLASS64) + return -ENOEXEC; + + /* must be little endian */ + if (hdr->e_ident[EI_DATA] != ELFDATA2LSB) + return -ENOEXEC; + + /* must be executable */ + if (hdr->e_type != ET_EXEC) + return -ENOEXEC; + + /* must be x86_64 */ + if (hdr->e_machine != EM_X86_64) + return -ENOEXEC; + + return 0; +} +