diff options
-rw-r--r-- | apps/Makefile | 6 | ||||
-rw-r--r-- | apps/init.c | 6 | ||||
-rw-r--r-- | apps/link.ld | 2 | ||||
-rw-r--r-- | kernel/features/load.c | 34 | ||||
-rw-r--r-- | kernel/features/proc.c | 2 | ||||
-rw-r--r-- | kernel/features/syscall.c | 1 | ||||
-rw-r--r-- | kernel/inc/load.h | 52 |
7 files changed, 95 insertions, 8 deletions
diff --git a/apps/Makefile b/apps/Makefile index 29136d0..5a6ec67 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -3,17 +3,15 @@ COBJS = a.o b.o init.o CC = ../cross/opt/bin/i686-elf-gcc LD = ../cross/opt/bin/i686-elf-ld -OC = ../cross/opt/bin/i686-elf-objcopy # Flags to make the binary smaller TODO: Remove after indirect pointer support! CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os -CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -ffunction-sections -fno-builtin -std=c99 -m32 -pedantic-errors -I../lib/inc/ -fPIE -Duserspace +CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Wl,-emain -I../lib/inc/ -Duserspace all: $(COBJS) %.o: %.c @mkdir -p ../build/apps/ @$(CC) -c $(CFLAGS) $< -o $@ - @$(LD) -o $(@:.o=.elf) -Tlink.ld -L../build/ $@ -lc - @$(OC) -O binary $(@:.o=.elf) ../build/apps/$(@:.o=) + @$(CC) -r $(CFLAGS) -o ../build/apps/$(@:.o=) -L../build $< -lc diff --git a/apps/init.c b/apps/init.c index 813021b..5879c1e 100644 --- a/apps/init.c +++ b/apps/init.c @@ -1,5 +1,6 @@ // MIT License, Copyright (c) 2020 Marvin Borner +#include <conv.h> #include <def.h> #include <mem.h> #include <print.h> @@ -9,7 +10,10 @@ void main() { print("Init loaded.\n"); - printf("%x %d %b\n ABC %s", 42, 42, 42, "BAUM"); + char *buf = malloc(10); + conv_base(42, buf, 8, 0); + printf("\n----\nTEST: %s\n----\n", buf); + /* printf("%x %d %b\n ABC %s", 42, 42, 42, "BAUM"); */ sys0(SYS_LOOP); /* sys1(SYS_EXEC, (int)"/a"); */ while (1) { diff --git a/apps/link.ld b/apps/link.ld index 84b2e2f..7a451ba 100644 --- a/apps/link.ld +++ b/apps/link.ld @@ -4,7 +4,7 @@ ENTRY(main) SECTIONS { - . = 0x00000000; + . = 0x00400000; .text : { *(.text.main) diff --git a/kernel/features/load.c b/kernel/features/load.c index 14a3086..fd9883a 100644 --- a/kernel/features/load.c +++ b/kernel/features/load.c @@ -1,7 +1,10 @@ // MIT License, Copyright (c) 2020 Marvin Borner +#include <assert.h> +#include <cpu.h> #include <def.h> #include <fs.h> +#include <load.h> #include <mem.h> #include <proc.h> #include <str.h> @@ -17,3 +20,34 @@ void bin_load(char *path, struct proc *proc) proc->regs.eip = (u32)data; strcpy(proc->name, path + 1); } + +int elf_verify(struct elf_header *h) +{ + return h->ident[0] == ELF_MAG && (strncmp((char *)&h->ident[1], "ELF", 3) == 0) && + h->ident[4] == ELF_32 && h->ident[5] == ELF_LITTLE && h->ident[6] == ELF_CURRENT && + h->machine == ELF_386 && (h->type == ET_REL || h->type == ET_EXEC); +} + +void elf_load(char *path, struct proc *proc) +{ + char *data = read_file(path); + struct elf_header *h = (struct elf_header *)data; + + assert(elf_verify(h)); + + printf("%d", h->type); + switch (h->type) { + case ET_EXEC: + return; + case ET_REL: + return; + } + + loop(); + u32 stack = (u32)malloc(0x1000) + 0x1000; + proc->regs.ebp = (u32)stack; + proc->regs.esp = (u32)stack; + proc->regs.useresp = (u32)stack; + proc->regs.eip = (u32)h->entry; + strcpy(proc->name, path + 1); +} diff --git a/kernel/features/proc.c b/kernel/features/proc.c index 64a4ac1..7e6c95a 100644 --- a/kernel/features/proc.c +++ b/kernel/features/proc.c @@ -94,7 +94,7 @@ void proc_init() irq_install_handler(0, scheduler); root = proc_make(); - bin_load("/init", root); + elf_load("/init", root); proc_print(); _eip = root->regs.eip; diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c index eaa09b3..519532c 100644 --- a/kernel/features/syscall.c +++ b/kernel/features/syscall.c @@ -9,7 +9,6 @@ #include <str.h> #include <sys.h> -int i = 0; void syscall_handler(struct regs *r) { enum sys num = r->eax; diff --git a/kernel/inc/load.h b/kernel/inc/load.h index 60fecf9..597d3b5 100644 --- a/kernel/inc/load.h +++ b/kernel/inc/load.h @@ -5,6 +5,58 @@ #include <proc.h> +#define ELF_MAG 0x7F // 0 +#define ELF_32 (1) // 4: 32-bit Architecture +#define ELF_LITTLE (1) // 5: Little Endian +#define ELF_CURRENT (1) // 6: ELF Current Version +#define ELF_386 (3) // header->machine x86 machine type + +#define ET_NONE 0 // Unkown type +#define ET_REL 1 // Relocatable file +#define ET_EXEC 2 // Executable file + +struct elf_header { + u8 ident[16]; + u16 type; + u16 machine; + u32 version; + u32 entry; + u32 phoff; + u32 shoff; + u32 flags; + u16 ehsize; + u16 phentsize; + u16 phnum; + u16 shentsize; + u16 shnum; + u16 shstrndx; +}; + +struct elf_section_header { + u32 name; + u32 type; + u32 flags; + u32 addr; + u32 offset; + u32 size; + u32 link; + u32 info; + u32 addralign; + u32 entsize; +}; + +struct elf_program_header { + u32 type; + u32 offset; + u32 vaddr; + u32 paddr; + u32 filesz; + u32 memsz; + u32 flags; + u32 align; +}; + void bin_load(char *path, struct proc *proc); +void elf_load(char *path, struct proc *proc); #endif |