aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile20
-rw-r--r--apps/link.ld34
-rw-r--r--apps/test.c4
-rw-r--r--src/features/elf.c39
-rw-r--r--src/features/load.c13
-rw-r--r--src/inc/elf.h61
-rw-r--r--src/inc/load.h10
-rw-r--r--src/main.c5
8 files changed, 59 insertions, 127 deletions
diff --git a/Makefile b/Makefile
index 061aafa..7916d68 100644
--- a/Makefile
+++ b/Makefile
@@ -12,22 +12,22 @@ COBJS_KERNEL = src/main.o \
src/features/fs.o \
src/features/psf.o \
src/features/gui.o \
- src/features/elf.o \
+ src/features/load.o \
src/lib/str.o \
src/lib/mem.o \
src/lib/math.o \
src/lib/conv.o \
src/lib/print.o
-COBJS_APPS = apps/test.o
-CC = ccache cross/opt/bin/i686-elf-gcc
-LD = ccache cross/opt/bin/i686-elf-ld
-AS = ccache nasm
+CC = cross/opt/bin/i686-elf-gcc
+LD = cross/opt/bin/i686-elf-ld
+OC = cross/opt/bin/i686-elf-objcopy
+AS = nasm
# Flags to make the binary smaller TODO: Remove after indirect pointer support!
CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os
# TODO: Use lib as external library
-CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -fno-pic -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Isrc/lib/inc/ -Isrc/inc/
+CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Isrc/lib/inc/ -Isrc/inc/
ASFLAGS = -f elf32 -O3
@@ -41,14 +41,14 @@ all: compile clean
kernel: $(COBJS_KERNEL)
-apps: $(COBJS_APPS)
-
-compile: kernel apps
+compile: kernel
@mkdir -p build/
@$(AS) -f bin src/entry.asm -o build/boot.bin
@$(LD) -N -emain -Ttext 0x00050000 -o build/kernel.bin $(COBJS_KERNEL) --oformat binary
@$(CC) $(CFLAGS) -o build/debug.o $(COBJS_KERNEL)
- @$(LD) -N -emain -Tapps/link.ld -o build/test apps/test.o
+ @$(CC) $(CFLAGS) -fPIE -c apps/test.c -o build/test.o
+ @$(LD) -o build/test.elf -Tapps/link.ld build/test.o
+ @$(OC) -O binary build/test.elf build/test
clean:
@find src/ apps/ -name "*.o" -type f -delete
diff --git a/apps/link.ld b/apps/link.ld
index ee41578..14fe1e3 100644
--- a/apps/link.ld
+++ b/apps/link.ld
@@ -1,24 +1,34 @@
-ENTRY(main)
+OUTPUT_FORMAT("elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(start)
+
SECTIONS
{
- .text 0x40000000:
- {
- code = .; _code = .; __code = .;
+ . = 0x00000000;
+
+ .text : {
*(.text)
}
- .data ALIGN(0x400000):
- {
- data = .; _data = .; __data = .;
- *(.data)
+ .rodata : {
*(.rodata)
}
- .bss ALIGN(0x400000):
- {
- bss = .; _bss = .; __bss = .;
+ . = ALIGN(4096);
+
+ .data : {
+ *(.data)
+ }
+
+ . = ALIGN(4096);
+
+ .bss : {
*(.bss)
}
- end = .; _end = .; __end = .;
+ . = ALIGN(4096);
+
+ _GLOBAL_OFFSET_TABLE_ = .;
+
+ . = ALIGN(4096);
}
diff --git a/apps/test.c b/apps/test.c
index f9e40cb..592f35b 100644
--- a/apps/test.c
+++ b/apps/test.c
@@ -81,8 +81,8 @@ void serial_print(const char *data)
serial_put(data[i]);
}
-void main()
+void start()
{
serial_install();
- serial_print("WELCOME TO USERSPACE");
+ serial_print("Follow the white rabbit.");
}
diff --git a/src/features/elf.c b/src/features/elf.c
deleted file mode 100644
index f92f399..0000000
--- a/src/features/elf.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <assert.h>
-#include <def.h>
-#include <elf.h>
-#include <fs.h>
-#include <mem.h>
-#include <str.h>
-
-int elf_verify(struct elf_header *h)
-{
- return (h->ident[0] == ELF_MAG && !strncmp((char *)&h->ident[1], "ELF", 3) &&
- 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)
-{
- u32 *data = read_file(path);
-
- struct elf_header *h = (struct elf_header *)data;
- assert(elf_verify(h));
-
- struct elf_program_header *p = (struct elf_program_header *)((u32)data + h->phoff);
- struct elf_program_header *p_end =
- (struct elf_program_header *)((u32)p + (h->phentsize * h->phnum));
-
- u32 offset = (p->vaddr - p->paddr);
- while (p < p_end) {
- printf("\nheader: 0x%x\n", p->paddr);
- printf("filesz: %d\n", p->filesz);
- /* memcpy(p->paddr, (u32)data + p->offset, p->filesz); */
- memcpy((u32 *)p->paddr, (u32 *)((u32)data + p->offset), p->filesz);
- p++;
- }
-
- void (*entry)();
- entry = (void (*)())(h->entry - offset);
-
- entry();
-}
diff --git a/src/features/load.c b/src/features/load.c
new file mode 100644
index 0000000..37ed6ad
--- /dev/null
+++ b/src/features/load.c
@@ -0,0 +1,13 @@
+#include <def.h>
+#include <fs.h>
+#include <print.h>
+
+void bin_load(char *path)
+{
+ char *data = read_file(path);
+
+ void (*entry)();
+ *(void **)(&entry) = data + 0xfe;
+
+ entry();
+}
diff --git a/src/inc/elf.h b/src/inc/elf.h
deleted file mode 100644
index c10ba4e..0000000
--- a/src/inc/elf.h
+++ /dev/null
@@ -1,61 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-
-#ifndef ELF_H
-#define ELF_H
-
-#include <def.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 elf_load(char *path);
-
-#endif
diff --git a/src/inc/load.h b/src/inc/load.h
new file mode 100644
index 0000000..afca249
--- /dev/null
+++ b/src/inc/load.h
@@ -0,0 +1,10 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef LOAD_H
+#define LOAD_H
+
+#include <def.h>
+
+void bin_load(char *path);
+
+#endif
diff --git a/src/main.c b/src/main.c
index 283d937..68993ba 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,11 +3,11 @@
#include "config.h"
#include <boot.h>
#include <def.h>
-#include <elf.h>
#include <fs.h>
#include <gui.h>
#include <interrupts.h>
#include <keyboard.h>
+#include <load.h>
#include <print.h>
#include <serial.h>
#include <timer.h>
@@ -42,8 +42,7 @@ void main(struct mem_info *mem_info, struct vid_info *vid_info)
gui_init(FONT_PATH);
gui_term_write("Wake up, " USERNAME "...\n");
- elf_load("/test");
- printf("loaded");
+ bin_load("/test");
while (1) {
};