elf: add basic elf parsing
This commit is contained in:
parent
fcbcde3666
commit
a85be6eb62
6 changed files with 189 additions and 2 deletions
|
|
@ -10,6 +10,7 @@
|
|||
#define EISDIR 6
|
||||
#define ENOSPC 7
|
||||
#define EEXIST 8
|
||||
#define ENOEXEC 9
|
||||
|
||||
const char *strerror(int errnum);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
69
include/kernel/elf.h
Normal file
69
include/kernel/elf.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _ELF_H_
|
||||
#define _ELF_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#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_ */
|
||||
63
init/cmd.c
63
init/cmd.c
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/cmd.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/elf.h>
|
||||
|
||||
#include <dev/console.h>
|
||||
|
||||
|
|
@ -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 <file>\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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
50
sched/load.c
Normal file
50
sched/load.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
/* ELF loader */
|
||||
#include <kernel/elf.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/proc.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue