The NULL kernel
authorJack Miller <jack@codezen.org>
Fri, 31 Aug 2012 21:40:37 +0000 (16:40 -0500)
committerJack Miller <jack@codezen.org>
Fri, 31 Aug 2012 21:40:37 +0000 (16:40 -0500)
This includes the code as it is at the end of the article

"The NULL kernel"

- Generates a slim 64-bit ELF binary bootable by GRUB 2

Signed-off-by: Jack Miller <jack@codezen.org>
Makefile [new file with mode: 0644]
linker.ld [new file with mode: 0644]
main.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..ad56011
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+CC = gcc
+LD = ld
+CFLAGS = -std=c99 -pedantic -Wall -nostdlib -ffreestanding
+LDFLAGS = -T linker.ld -nostdlib -n
+
+SRC = main.c
+OBJ = ${SRC:.c=.o}
+
+all: kernel
+
+.c.o:
+       @echo CC $<
+       @${CC} -c ${CFLAGS} $<
+
+kernel: ${OBJ} linker.ld
+       @echo CC -o $@
+       @${LD} ${LDFLAGS} -o kernel ${OBJ}
+
+clean:
+       @echo cleaning
+       @rm -f ${OBJ} kernel
+
+.PHONY: all
diff --git a/linker.ld b/linker.ld
new file mode 100644 (file)
index 0000000..f40c85c
--- /dev/null
+++ b/linker.ld
@@ -0,0 +1,26 @@
+OUTPUT_FORMAT("elf64-x86-64")
+ENTRY(main)
+SECTIONS
+{
+    .grub_sig 0xC0100000 : AT(0x100000)
+    {
+        *(.grub_sig)
+    }
+    .text :
+    {
+        *(.text)
+    }
+    .data :
+    {
+        *(.data)
+    }
+    .bss :
+    {
+        *(.bss)
+    }
+    /DISCARD/ :
+    {
+        *(.comment)
+        *(.eh_frame)
+    }
+}
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..aed4eb7
--- /dev/null
+++ b/main.c
@@ -0,0 +1,17 @@
+struct grub_signature {
+       unsigned int magic;
+       unsigned int flags;
+       unsigned int checksum;
+};
+
+#define GRUB_MAGIC 0x1BADB002
+#define GRUB_FLAGS 0x0
+#define GRUB_CHECKSUM (-1 * (GRUB_MAGIC + GRUB_FLAGS))
+
+struct grub_signature gs __attribute__ ((section (".grub_sig"))) =
+       { GRUB_MAGIC, GRUB_FLAGS, GRUB_CHECKSUM };
+
+void main (void)
+{
+    while(1) {}
+}