:: commit 27848fc85c1d2f4d5f48905dedccd18b5e31a333

mintsuki <mintsuki@protonmail.com> — 2021-02-25 00:24

parents: 7eb22e5c15

misc: General code reorganisation

diff --git a/.gitignore b/.gitignore
index 97c31e71..8b85629f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+bin
 /**/*.gen
 /**/*.map
 /**/*.o
diff --git a/Makefile b/Makefile
index 5bc11540..44614283 100644
--- a/Makefile
+++ b/Makefile
@@ -6,38 +6,34 @@ DESTDIR =
 
 PATH := $(shell pwd)/toolchain/bin:$(PATH)
 
-.PHONY: all clean install tinf-clean bootloader bootloader-clean distclean stages stages-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test
+.PHONY: all clean install tinf-clean bootloader bootloader-clean distclean stage23 stage23-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test iso9660-test
 
-all: limine-install
+all: bin/limine-install
 
-limine-install: limine-install.c limine.o limine_sys.o
-	$(CC) $(CFLAGS) -std=c11 limine.o limine_sys.o limine-install.c -o limine-install
+bin/limine-install: limine-install.c limine-hdd.o
+	$(CC) $(CFLAGS) -std=c11 limine-hdd.o limine-install.c -o $@
 
-limine.o: limine.bin
-	$(OBJCOPY) -B i8086 -I binary -O default limine.bin limine.o
-
-limine_sys.o: limine.bin
-	$(OBJCOPY) -B i8086 -I binary -O default limine.sys limine_sys.o
+limine-hdd.o: bin/limine-hdd.bin
+	$(OBJCOPY) -B i8086 -I binary -O default bin/limine-hdd.bin $@
 
 clean:
-	rm -f limine.o limine_sys.o limine-install
+	rm -f limine-hdd.o
 
 install: all
 	install -d $(DESTDIR)$(PREFIX)/bin
 	install -s limine-install $(DESTDIR)$(PREFIX)/bin/
 
-bootloader: | decompressor stages
-	cd hddboot && nasm bootsect.asm -fbin -o ../limine-hdd.bin
-	cd cdboot && nasm bootsect.asm -fbin -o ../limine-cd.bin
-	cd pxeboot && nasm bootsect.asm -fbin -o ../limine-pxe.bin
-	cp stages/stages.map ./
-	cp stages/stage3.bin ./limine.sys
+bootloader: | decompressor stage23
+	mkdir -p bin
+	cd stage1/hdd && nasm bootsect.asm -fbin -o ../../bin/limine-hdd.bin
+	cd stage1/cd  && nasm bootsect.asm -fbin -o ../../bin/limine-cd.bin
+	cd stage1/pxe && nasm bootsect.asm -fbin -o ../../bin/limine-pxe.bin
+	cp stage23/limine.sys ./bin/
 
-bootloader-clean: stages-clean decompressor-clean test-clean
-	rm -f test/stages.map test.hdd
+bootloader-clean: stage23-clean decompressor-clean test-clean
 
 distclean: clean bootloader-clean
-	rm -rf stivale
+	rm -rf bin stivale
 
 tinf-clean:
 	cd tinf && rm -rf *.o *.d
@@ -46,11 +42,11 @@ stivale:
 	git clone https://github.com/stivale/stivale.git
 	cd stivale && git checkout d0a7ca5642d89654f8d688c2481c2771a8653c99
 
-stages: tinf-clean stivale
-	$(MAKE) -C stages all
+stage23: tinf-clean stivale
+	$(MAKE) -C stage23 all
 
-stages-clean:
-	$(MAKE) -C stages clean
+stage23-clean:
+	$(MAKE) -C stage23 clean
 
 decompressor: tinf-clean
 	$(MAKE) -C decompressor all
@@ -60,6 +56,7 @@ decompressor-clean:
 
 test-clean:
 	$(MAKE) -C test clean
+	rm -f test/limine.map test.hdd
 
 toolchain:
 	cd toolchain && ./make_toolchain.sh -j`nproc`
@@ -76,16 +73,15 @@ echfs-test: test.hdd bootloader | all
 	sed "s/@GUID@/`cat part_guid`/g" < test/limine.cfg > limine.cfg.tmp
 	echfs-utils -g -p0 test.hdd import limine.cfg.tmp limine.cfg
 	rm -f limine.cfg.tmp part_guid
-	echfs-utils -g -p0 test.hdd import stages.map boot/stages.map
 	echfs-utils -g -p0 test.hdd import test/test.elf boot/test.elf
 	echfs-utils -g -p0 test.hdd import test/bg.bmp boot/bg.bmp
-	./limine-install ./ test.hdd
-	echfs-utils -g -p0 test.hdd import ./limine.sys boot/limine.sys
+	echfs-utils -g -p0 test.hdd import bin/limine.sys boot/limine.sys
+	echfs-utils -g -p0 test.hdd import bin/limine.map boot/limine.map
+	bin/limine-install test.hdd
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
 
 ext2-test: test.hdd bootloader | all
 	$(MAKE) -C test
-	cp stages.map test/
 	rm -rf test_image/
 	mkdir test_image
 	sudo losetup -Pf --show test.hdd > loopback_dev
@@ -93,17 +89,16 @@ ext2-test: test.hdd bootloader | all
 	sudo mkfs.ext2 `cat loopback_dev`p1
 	sudo mount `cat loopback_dev`p1 test_image
 	sudo mkdir test_image/boot
-	sudo cp -rv ./limine.sys test/* test_image/boot/
+	sudo cp -rv bin/* test/* test_image/boot/
 	sync
 	sudo umount test_image/
 	sudo losetup -d `cat loopback_dev`
 	rm -rf test_image loopback_dev
-	./limine-install ./ test.hdd
+	bin/limine-install test.hdd
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
 
 fat32-test: test.hdd bootloader | all
 	$(MAKE) -C test
-	cp stages.map test/
 	rm -rf test_image/
 	mkdir test_image
 	sudo losetup -Pf --show test.hdd > loopback_dev
@@ -111,19 +106,18 @@ fat32-test: test.hdd bootloader | all
 	sudo mkfs.fat -F 32 `cat loopback_dev`p1
 	sudo mount `cat loopback_dev`p1 test_image
 	sudo mkdir test_image/boot
-	sudo cp -rv ./limine.sys test/* test_image/boot/
+	sudo cp -rv bin/* test/* test_image/boot/
 	sync
 	sudo umount test_image/
 	sudo losetup -d `cat loopback_dev`
 	rm -rf test_image loopback_dev
-	./limine-install ./ test.hdd
+	bin/limine-install test.hdd
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
 
 iso9660-test: bootloader
 	$(MAKE) -C test
-	cp stages.map test/
 	rm -rf test_image/
 	mkdir -p test_image/boot
-	cp -rv limine-cd.bin limine.sys stages/stages.bin test/* test_image/boot/
+	cp -rv bin/* test/* test_image/boot/
 	genisoimage -no-emul-boot -b boot/limine-cd.bin -o test.iso test_image/
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -cdrom test.iso -debugcon stdio
diff --git a/limine-install.c b/limine-install.c
index 74cf034d..dfe053d2 100644
--- a/limine-install.c
+++ b/limine-install.c
@@ -259,54 +259,26 @@ static bool _device_write(const void *buffer, uint64_t loc, size_t count) {
             goto cleanup;                       \
     } while (0)
 
-extern uint8_t _binary_limine_bin_start[], _binary_limine_bin_end[];
-extern uint8_t _binary_limine_sys_start[], _binary_limine_sys_end[];
+extern uint8_t _binary_bin_limine_hdd_bin_start[], _binary_bin_limine_hdd_bin_end[];
 
 int main(int argc, char *argv[]) {
     int      ok = 1;
-    uint8_t *bootloader_img = _binary_limine_bin_start;
+    uint8_t *bootloader_img = _binary_bin_limine_hdd_bin_start;
     size_t   bootloader_file_size =
-        (size_t)_binary_limine_bin_end - (size_t)_binary_limine_bin_start;
-    uint8_t *stage3_img = _binary_limine_sys_start;
-    size_t   stage3_file_size =
-        (size_t)_binary_limine_sys_end - (size_t)_binary_limine_sys_start;
+        (size_t)_binary_bin_limine_hdd_bin_end - (size_t)_binary_bin_limine_hdd_bin_start;
     uint8_t  orig_mbr[70], timestamp[6];
-    char *limine_sys_path = NULL;
-    int   limine_sys = -1;
 
     if (sizeof(off_t) != 8) {
         fprintf(stderr, "ERROR: off_t type is not 64-bit.\n");
         goto cleanup;
     }
 
-    if (argc < 3) {
-        printf("Usage: %s <boot directory> <device> [GPT partition index]\n", argv[0]);
+    if (argc < 2) {
+        printf("Usage: %s <device> [GPT partition index]\n", argv[0]);
         goto cleanup;
     }
 
-    #define MAX_STAGE3_PATH 1024
-
-    limine_sys_path = malloc(MAX_STAGE3_PATH);
-    if (limine_sys_path == NULL) {
-        perror("ERROR");
-        goto cleanup;
-    }
-
-    snprintf(limine_sys_path, MAX_STAGE3_PATH, "%s/limine.sys", argv[1]);
-
-    limine_sys = creat(limine_sys_path, 0644);
-    if (limine_sys == -1) {
-        perror("ERROR");
-        goto cleanup;
-    }
-
-    if (write(limine_sys, stage3_img, stage3_file_size) !=
-        (ssize_t)stage3_file_size) {
-        perror("ERROR");
-        goto cleanup;
-    }
-
-    device = open(argv[2], O_RDWR);
+    device = open(argv[1], O_RDWR);
     if (device == -1) {
         perror("ERROR");
         goto cleanup;
@@ -358,7 +330,7 @@ int main(int argc, char *argv[]) {
     if (gpt) {
         if (argc > 3) {
             uint32_t partition_num;
-            sscanf(argv[3], "%" SCNu32, &partition_num);
+            sscanf(argv[2], "%" SCNu32, &partition_num);
             partition_num--;
             if (partition_num > gpt_header.number_of_partition_entries) {
                 fprintf(stderr, "ERROR: Partition number is too large.\n");
@@ -502,10 +474,6 @@ cleanup:
         free(cache);
     if (device != -1)
         close(device);
-    if (limine_sys_path != NULL)
-        free(limine_sys_path);
-    if (limine_sys != -1)
-        close(limine_sys);
 
     return ok;
 }
diff --git a/cdboot/bootsect.asm b/stage1/cd/bootsect.asm
similarity index 90%
rename from cdboot/bootsect.asm
rename to stage1/cd/bootsect.asm
index adc1db28..69d346fc 100644
--- a/cdboot/bootsect.asm
+++ b/stage1/cd/bootsect.asm
@@ -62,9 +62,9 @@ skip_bpb:
     call read_file
     jc err
 
-    ; Find and load '/BOOT/STAGES.BIN'
-    mov ebx, TXT_STAGES
-    mov cl, TXT_STAGES_SZ
+    ; Find and load '/BOOT/LIMINE.SYS'
+    mov ebx, TXT_LIMINE
+    mov cl, TXT_LIMINE_SZ
     call read_file  ; esi is set from the last call
     jc err
 
@@ -82,7 +82,7 @@ err:
     jmp err
 
 %include 'iso9660.asm'
-%include '../hddboot/gdt.inc'
+%include '../gdt.asm'
 
 BITS 32
 pmode:
@@ -102,8 +102,8 @@ pmode:
 
 TXT_BOOT: db "BOOT"
 TXT_BOOT_SZ equ $ - TXT_BOOT
-TXT_STAGES: db "STAGES.BIN;1"
-TXT_STAGES_SZ equ $ - TXT_STAGES
+TXT_LIMINE: db "LIMINE.SYS;1"
+TXT_LIMINE_SZ equ $ - TXT_LIMINE
 
 ; Just making sure the entry point (ISO9660_BUFFER) is not reached
 times (0x8000 - 0x7C00) - ($ - $$) db 0
diff --git a/cdboot/findFile.asm b/stage1/cd/findFile.asm
similarity index 100%
rename from cdboot/findFile.asm
rename to stage1/cd/findFile.asm
diff --git a/cdboot/findPVD.asm b/stage1/cd/findPVD.asm
similarity index 100%
rename from cdboot/findPVD.asm
rename to stage1/cd/findPVD.asm
diff --git a/cdboot/iso9660.asm b/stage1/cd/iso9660.asm
similarity index 100%
rename from cdboot/iso9660.asm
rename to stage1/cd/iso9660.asm
diff --git a/cdboot/read_2k_sectors.asm b/stage1/cd/read_2k_sectors.asm
similarity index 100%
rename from cdboot/read_2k_sectors.asm
rename to stage1/cd/read_2k_sectors.asm
diff --git a/cdboot/strcmp.asm b/stage1/cd/strcmp.asm
similarity index 100%
rename from cdboot/strcmp.asm
rename to stage1/cd/strcmp.asm
diff --git a/hddboot/gdt.inc b/stage1/gdt.asm
similarity index 100%
rename from hddboot/gdt.inc
rename to stage1/gdt.asm
diff --git a/hddboot/bootsect.asm b/stage1/hdd/bootsect.asm
similarity index 95%
rename from hddboot/bootsect.asm
rename to stage1/hdd/bootsect.asm
index c5722ba8..6277de4f 100644
--- a/hddboot/bootsect.asm
+++ b/stage1/hdd/bootsect.asm
@@ -89,8 +89,8 @@ times 6 db 0
 
 ; Includes
 
-%include 'disk.inc'
-%include 'gdt.inc'
+%include 'disk.asm'
+%include '../gdt.asm'
 
 bits 32
 vector:
@@ -126,9 +126,9 @@ dw 0xaa55
 ; ********************* Stage 2 *********************
 
 decompressor:
-incbin '../decompressor/decompressor.bin'
+incbin '../../decompressor/decompressor.bin'
 
 align 16
 stage2:
-incbin '../stages/stage2.bin.gz'
+incbin '../../stage23/stage2.bin.gz'
 .size: equ $ - stage2
diff --git a/hddboot/disk.inc b/stage1/hdd/disk.asm
similarity index 100%
rename from hddboot/disk.inc
rename to stage1/hdd/disk.asm
diff --git a/pxeboot/bootsect.asm b/stage1/pxe/bootsect.asm
similarity index 88%
rename from pxeboot/bootsect.asm
rename to stage1/pxe/bootsect.asm
index a003c506..e67aa637 100644
--- a/pxeboot/bootsect.asm
+++ b/stage1/pxe/bootsect.asm
@@ -50,15 +50,15 @@ err:
 
 ; Includes
 
-%include '../hddboot/gdt.inc'
+%include '../gdt.asm'
 
 ; ********************* Stage 2 *********************
 
 decompressor:
-incbin '../decompressor/decompressor.bin'
+incbin '../../decompressor/decompressor.bin'
 
 align 16
 stage2:
-incbin '../stages/stage2.bin.gz'
+incbin '../../stage23/stage2.bin.gz'
 .size: equ $ - stage2
 .fullsize: equ $ - decompressor
diff --git a/stages/Makefile b/stage23/Makefile
similarity index 61%
rename from stages/Makefile
rename to stage23/Makefile
index 1f2975d0..32609b65 100644
--- a/stages/Makefile
+++ b/stage23/Makefile
@@ -35,8 +35,7 @@ INTERNAL_LDFLAGS = \
 	-nostdlib \
 	-no-pie \
 	-z max-page-size=0x1000 \
-	-static \
-	-Tlinker.ld
+	-static
 
 .PHONY: all clean
 
@@ -45,26 +44,25 @@ ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
 OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)
 HEADER_DEPS := $(C_FILES:.c=.d)
 
-all: stages.map stages.bin stage2.bin stage2.bin.gz stage3.bin
+all: limine.sys stage2.bin stage2.bin.gz
 
 stage2.bin.gz: stage2.bin
 	gzip -n -9 < stage2.bin > stage2.bin.gz
 
-stage2.bin: stages.bin
-	dd if=stages.bin bs=$$(( 0x$$($(READELF) -S stages.elf | grep .stage3 | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
+stage2.bin: limine.sys
+	dd if=limine.sys bs=$$(( 0x$$($(READELF) -S limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
 
-stage3.bin: stages.bin
-	dd if=stages.bin bs=$$(( 0x$$($(READELF) -S stages.elf | grep .stage3 | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) skip=1 of=$@
+limine.map.o: limine_nomap.elf
+	./gensyms.sh $(OBJDUMP) limine_nomap.elf limine
 
-stages.map: stages.elf
-	./gensyms.sh $(OBJDUMP)
-	nasm symlist.gen -f bin -o $@
-
-stages.bin: stages.elf
+limine.sys: limine.elf
 	$(OBJCOPY) -O binary $< $@
 
-stages.elf: $(OBJ)
-	$(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $@
+limine_nomap.elf: $(OBJ)
+	$(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
+
+limine.elf: $(OBJ) limine.map.o
+	$(LD) $(OBJ) limine.map.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
 
 -include $(HEADER_DEPS)
 
@@ -75,4 +73,4 @@ stages.elf: $(OBJ)
 	nasm $< -f elf32 -o $@
 
 clean:
-	rm -f symlist.gen stages.elf stages.map stages.bin stage2.bin stage2.bin.gz stage3.bin $(OBJ) $(HEADER_DEPS)
+	rm -f symlist.gen limine.elf limine.sys stage2.bin stage2.bin.gz $(OBJ) $(HEADER_DEPS)
diff --git a/stages/drivers/disk.c b/stage23/drivers/disk.c
similarity index 100%
rename from stages/drivers/disk.c
rename to stage23/drivers/disk.c
diff --git a/stages/drivers/disk.h b/stage23/drivers/disk.h
similarity index 100%
rename from stages/drivers/disk.h
rename to stage23/drivers/disk.h
diff --git a/stages/drivers/vbe.c b/stage23/drivers/vbe.c
similarity index 100%
rename from stages/drivers/vbe.c
rename to stage23/drivers/vbe.c
diff --git a/stages/drivers/vbe.h b/stage23/drivers/vbe.h
similarity index 100%
rename from stages/drivers/vbe.h
rename to stage23/drivers/vbe.h
diff --git a/stages/drivers/vga_textmode.c b/stage23/drivers/vga_textmode.c
similarity index 100%
rename from stages/drivers/vga_textmode.c
rename to stage23/drivers/vga_textmode.c
diff --git a/stages/drivers/vga_textmode.h b/stage23/drivers/vga_textmode.h
similarity index 100%
rename from stages/drivers/vga_textmode.h
rename to stage23/drivers/vga_textmode.h
diff --git a/stages/entry.asm b/stage23/entry.asm
similarity index 100%
rename from stages/entry.asm
rename to stage23/entry.asm
diff --git a/stages/fs/echfs.c b/stage23/fs/echfs.c
similarity index 100%
rename from stages/fs/echfs.c
rename to stage23/fs/echfs.c
diff --git a/stages/fs/echfs.h b/stage23/fs/echfs.h
similarity index 100%
rename from stages/fs/echfs.h
rename to stage23/fs/echfs.h
diff --git a/stages/fs/ext2.c b/stage23/fs/ext2.c
similarity index 100%
rename from stages/fs/ext2.c
rename to stage23/fs/ext2.c
diff --git a/stages/fs/ext2.h b/stage23/fs/ext2.h
similarity index 100%
rename from stages/fs/ext2.h
rename to stage23/fs/ext2.h
diff --git a/stages/fs/fat32.c b/stage23/fs/fat32.c
similarity index 100%
rename from stages/fs/fat32.c
rename to stage23/fs/fat32.c
diff --git a/stages/fs/fat32.h b/stage23/fs/fat32.h
similarity index 100%
rename from stages/fs/fat32.h
rename to stage23/fs/fat32.h
diff --git a/stages/fs/file.c b/stage23/fs/file.c
similarity index 100%
rename from stages/fs/file.c
rename to stage23/fs/file.c
diff --git a/stages/fs/file.h b/stage23/fs/file.h
similarity index 100%
rename from stages/fs/file.h
rename to stage23/fs/file.h
diff --git a/stages/fs/iso9660.c b/stage23/fs/iso9660.c
similarity index 100%
rename from stages/fs/iso9660.c
rename to stage23/fs/iso9660.c
diff --git a/stages/fs/iso9660.h b/stage23/fs/iso9660.h
similarity index 100%
rename from stages/fs/iso9660.h
rename to stage23/fs/iso9660.h
diff --git a/stage23/gensyms.sh b/stage23/gensyms.sh
new file mode 100755
index 00000000..8aacfaa2
--- /dev/null
+++ b/stage23/gensyms.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+set -e
+
+TMP1=$(mktemp)
+TMP2=$(mktemp)
+TMP3=$(mktemp)
+TMP4=$(mktemp)
+
+$1 -t "$2" | sed '/\bd\b/d' | sort > "$TMP1"
+grep "\.text" < "$TMP1" | cut -d' ' -f1 > "$TMP2"
+grep "\.text" < "$TMP1" | awk 'NF{ print $NF }' > "$TMP3"
+
+echo "section .map" > "$TMP4"
+echo "global $3_map" >> "$TMP4"
+echo "$3_map:" >> "$TMP4"
+
+paste -d'$' "$TMP2" "$TMP3" | sed 's/^/dd 0x/g' | sed 's/$/", 0/g' | sed 's/\$/\ndb "/g' >> "$TMP4"
+
+echo "dd 0xffffffff" >> "$TMP4"
+
+nasm -f elf32 "$TMP4" -o $3.map.o
+
+rm "$TMP1" "$TMP2" "$TMP3" "$TMP4"
diff --git a/stages/lib/acpi.c b/stage23/lib/acpi.c
similarity index 100%
rename from stages/lib/acpi.c
rename to stage23/lib/acpi.c
diff --git a/stages/lib/acpi.h b/stage23/lib/acpi.h
similarity index 100%
rename from stages/lib/acpi.h
rename to stage23/lib/acpi.h
diff --git a/stages/lib/blib.c b/stage23/lib/blib.c
similarity index 100%
rename from stages/lib/blib.c
rename to stage23/lib/blib.c
diff --git a/stages/lib/blib.h b/stage23/lib/blib.h
similarity index 97%
rename from stages/lib/blib.h
rename to stage23/lib/blib.h
index b6c9f152..794353c8 100644
--- a/stages/lib/blib.h
+++ b/stage23/lib/blib.h
@@ -43,7 +43,7 @@ uint64_t strtoui(const char *s, const char **end, int base);
 
 #define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
 
-typedef void *symbol[];
+typedef char symbol[];
 
 #define stage3_text __attribute__((section(".stage3_text")))
 #define stage3_data __attribute__((section(".stage3_data")))
diff --git a/stages/lib/bmp.c b/stage23/lib/bmp.c
similarity index 100%
rename from stages/lib/bmp.c
rename to stage23/lib/bmp.c
diff --git a/stages/lib/bmp.h b/stage23/lib/bmp.h
similarity index 100%
rename from stages/lib/bmp.h
rename to stage23/lib/bmp.h
diff --git a/stages/lib/builtins.asm b/stage23/lib/builtins.asm
similarity index 100%
rename from stages/lib/builtins.asm
rename to stage23/lib/builtins.asm
diff --git a/stages/lib/config.c b/stage23/lib/config.c
similarity index 95%
rename from stages/lib/config.c
rename to stage23/lib/config.c
index 77eefd45..d4c006d6 100644
--- a/stages/lib/config.c
+++ b/stage23/lib/config.c
@@ -27,7 +27,12 @@ int init_config_disk(struct volume *part) {
             panic("Could not open stage 3");
         }
 
-        fread(&stage3, stage3_addr, 0, stage3.size);
+        print("%x %x %x\n", stage3_addr, (uintptr_t)stage3_addr - 0x8000,
+              stage3.size - (uintptr_t)stage3_addr - 0x8000);
+
+        fread(&stage3, stage3_addr,
+              (uintptr_t)stage3_addr - 0x8000,
+              stage3.size - ((uintptr_t)stage3_addr - 0x8000));
 
         stage3_loaded = true;
     }
diff --git a/stages/lib/config.h b/stage23/lib/config.h
similarity index 100%
rename from stages/lib/config.h
rename to stage23/lib/config.h
diff --git a/stages/lib/elf.c b/stage23/lib/elf.c
similarity index 100%
rename from stages/lib/elf.c
rename to stage23/lib/elf.c
diff --git a/stages/lib/elf.h b/stage23/lib/elf.h
similarity index 100%
rename from stages/lib/elf.h
rename to stage23/lib/elf.h
diff --git a/stages/lib/guid.c b/stage23/lib/guid.c
similarity index 100%
rename from stages/lib/guid.c
rename to stage23/lib/guid.c
diff --git a/stages/lib/guid.h b/stage23/lib/guid.h
similarity index 100%
rename from stages/lib/guid.h
rename to stage23/lib/guid.h
diff --git a/stages/lib/image.c b/stage23/lib/image.c
similarity index 100%
rename from stages/lib/image.c
rename to stage23/lib/image.c
diff --git a/stages/lib/image.h b/stage23/lib/image.h
similarity index 100%
rename from stages/lib/image.h
rename to stage23/lib/image.h
diff --git a/stages/lib/libc.c b/stage23/lib/libc.c
similarity index 100%
rename from stages/lib/libc.c
rename to stage23/lib/libc.c
diff --git a/stages/lib/libc.h b/stage23/lib/libc.h
similarity index 100%
rename from stages/lib/libc.h
rename to stage23/lib/libc.h
diff --git a/stages/lib/part.c b/stage23/lib/part.c
similarity index 100%
rename from stages/lib/part.c
rename to stage23/lib/part.c
diff --git a/stages/lib/part.h b/stage23/lib/part.h
similarity index 100%
rename from stages/lib/part.h
rename to stage23/lib/part.h
diff --git a/stages/lib/print.c b/stage23/lib/print.c
similarity index 100%
rename from stages/lib/print.c
rename to stage23/lib/print.c
diff --git a/stages/lib/print.h b/stage23/lib/print.h
similarity index 100%
rename from stages/lib/print.h
rename to stage23/lib/print.h
diff --git a/stages/lib/pxe.asm b/stage23/lib/pxe.asm
similarity index 100%
rename from stages/lib/pxe.asm
rename to stage23/lib/pxe.asm
diff --git a/stages/lib/rand.c b/stage23/lib/rand.c
similarity index 100%
rename from stages/lib/rand.c
rename to stage23/lib/rand.c
diff --git a/stages/lib/rand.h b/stage23/lib/rand.h
similarity index 100%
rename from stages/lib/rand.h
rename to stage23/lib/rand.h
diff --git a/stages/lib/readline.c b/stage23/lib/readline.c
similarity index 100%
rename from stages/lib/readline.c
rename to stage23/lib/readline.c
diff --git a/stages/lib/readline.h b/stage23/lib/readline.h
similarity index 100%
rename from stages/lib/readline.h
rename to stage23/lib/readline.h
diff --git a/stages/lib/real.asm b/stage23/lib/real.asm
similarity index 100%
rename from stages/lib/real.asm
rename to stage23/lib/real.asm
diff --git a/stages/lib/real.h b/stage23/lib/real.h
similarity index 100%
rename from stages/lib/real.h
rename to stage23/lib/real.h
diff --git a/stages/lib/sleep.asm b/stage23/lib/sleep.asm
similarity index 100%
rename from stages/lib/sleep.asm
rename to stage23/lib/sleep.asm
diff --git a/stages/lib/term.c b/stage23/lib/term.c
similarity index 100%
rename from stages/lib/term.c
rename to stage23/lib/term.c
diff --git a/stages/lib/term.h b/stage23/lib/term.h
similarity index 100%
rename from stages/lib/term.h
rename to stage23/lib/term.h
diff --git a/stages/lib/time.c b/stage23/lib/time.c
similarity index 100%
rename from stages/lib/time.c
rename to stage23/lib/time.c
diff --git a/stages/lib/time.h b/stage23/lib/time.h
similarity index 100%
rename from stages/lib/time.h
rename to stage23/lib/time.h
diff --git a/stages/lib/trace.c b/stage23/lib/trace.c
similarity index 58%
rename from stages/lib/trace.c
rename to stage23/lib/trace.c
index 362379ed..2c502949 100644
--- a/stages/lib/trace.c
+++ b/stage23/lib/trace.c
@@ -8,45 +8,30 @@
 #include <fs/file.h>
 #include <mm/pmm.h>
 
-static char *stage2_map = NULL;
-
-void trace_init(void) {
-    char *map_filename = config_get_value(NULL, 0, "STAGE2_MAP");
-    if (map_filename == NULL)
-        return;
-
-    struct file_handle stage2_map_file;
-    if (!uri_open(&stage2_map_file, map_filename))
-        panic("Could not open stage2 map file `%s`", map_filename);
-
-    stage2_map = ext_mem_alloc(stage2_map_file.size);
-    fread(&stage2_map_file, stage2_map, 0, stage2_map_file.size);
-
-    print("trace: Stage 2 map file `%s` loaded.\n", map_filename);
-}
+extern symbol limine_map;
 
 char *trace_address(size_t *off, size_t addr) {
-    if (!stage2_map)
+    if (!stage3_loaded)
         return NULL;
 
     uint32_t prev_addr = 0;
     char    *prev_sym  = NULL;
 
     for (size_t i = 0;;) {
-        if (*((uint32_t *)&stage2_map[i]) >= addr) {
+        if (*((uint32_t *)&limine_map[i]) >= addr) {
             *off = addr - prev_addr;
             return prev_sym;
         }
-        prev_addr = *((uint32_t *)&stage2_map[i]);
+        prev_addr = *((uint32_t *)&limine_map[i]);
         i += sizeof(uint32_t);
-        prev_sym  = &stage2_map[i];
-        while (stage2_map[i++] != 0);
+        prev_sym  = &limine_map[i];
+        while (limine_map[i++] != 0);
     }
 }
 
 void print_stacktrace(size_t *base_ptr) {
-    if (!stage2_map) {
-        print("trace: Stack trace omitted due to unavailable map file.\n");
+    if (!stage3_loaded) {
+        print("trace: Stack trace omitted because stage 3 was not loaded yet.\n");
         return;
     }
 
diff --git a/stages/lib/trace.h b/stage23/lib/trace.h
similarity index 87%
rename from stages/lib/trace.h
rename to stage23/lib/trace.h
index 30c3cf47..d67aa13d 100644
--- a/stages/lib/trace.h
+++ b/stage23/lib/trace.h
@@ -3,7 +3,6 @@
 
 #include <stdint.h>
 
-void trace_init(void);
 char *trace_address(size_t *off, size_t addr);
 void print_stacktrace(size_t *base_ptr);
 
diff --git a/stages/lib/uri.c b/stage23/lib/uri.c
similarity index 100%
rename from stages/lib/uri.c
rename to stage23/lib/uri.c
diff --git a/stages/lib/uri.h b/stage23/lib/uri.h
similarity index 100%
rename from stages/lib/uri.h
rename to stage23/lib/uri.h
diff --git a/stages/linker.ld b/stage23/linker.ld
similarity index 76%
rename from stages/linker.ld
rename to stage23/linker.ld
index b4cf68e2..2c0f573b 100644
--- a/stages/linker.ld
+++ b/stage23/linker.ld
@@ -13,18 +13,28 @@ SECTIONS
         KEEP(*(.realmode*))
     }
 
-    .stage2 : {
+    .stage2.text : {
         *(.text*)
+    }
+
+    .stage2.data : {
         *(.data*)
         *(.rodata*)
     }
 
-    .stage3 : {
+    .stage3.text : {
         stage3_addr = .;
         *(.stage3_text*)
+    }
+
+    .stage3.data : {
         *(.stage3_data*)
     }
 
+    .map : {
+        KEEP(*(.map*))
+    }
+
     .bss : {
         bss_begin = .;
         *(COMMON)
diff --git a/stage23/linker_nomap.ld b/stage23/linker_nomap.ld
new file mode 100644
index 00000000..80a70c16
--- /dev/null
+++ b/stage23/linker_nomap.ld
@@ -0,0 +1,48 @@
+OUTPUT_FORMAT(elf32-i386)
+ENTRY(_start)
+
+SECTIONS
+{
+    . = 0x8000;
+
+    .entry : {
+        KEEP(*(.entry*))
+    }
+
+    .realmode : {
+        KEEP(*(.realmode*))
+    }
+
+    .stage2.text : {
+        *(.text*)
+    }
+
+    .stage2.data : {
+        *(.data*)
+        *(.rodata*)
+    }
+
+    .stage3.text : {
+        stage3_addr = .;
+        *(.stage3_text*)
+    }
+
+    .stage3.data : {
+        *(.stage3_data*)
+    }
+
+    .map : {
+        limine_map = .;
+    }
+
+    .bss : {
+        bss_begin = .;
+        *(COMMON)
+        *(.bss*)
+        bss_end = .;
+    }
+
+    /DISCARD/ : {
+        *(*)
+    }
+}
diff --git a/stages/main.c b/stage23/main.c
similarity index 99%
rename from stages/main.c
rename to stage23/main.c
index 5a284dff..76e7cc84 100644
--- a/stages/main.c
+++ b/stage23/main.c
@@ -87,8 +87,6 @@ void entry(uint8_t _boot_drive, int boot_from) {
         break;
     }
 
-    trace_init();
-
     char *cmdline;
     char *config = menu(&cmdline);
 
diff --git a/stages/menu.c b/stage23/menu.c
similarity index 100%
rename from stages/menu.c
rename to stage23/menu.c
diff --git a/stages/menu.h b/stage23/menu.h
similarity index 100%
rename from stages/menu.h
rename to stage23/menu.h
diff --git a/stages/mm/mtrr.c b/stage23/mm/mtrr.c
similarity index 100%
rename from stages/mm/mtrr.c
rename to stage23/mm/mtrr.c
diff --git a/stages/mm/mtrr.h b/stage23/mm/mtrr.h
similarity index 100%
rename from stages/mm/mtrr.h
rename to stage23/mm/mtrr.h
diff --git a/stages/mm/pmm.c b/stage23/mm/pmm.c
similarity index 100%
rename from stages/mm/pmm.c
rename to stage23/mm/pmm.c
diff --git a/stages/mm/pmm.h b/stage23/mm/pmm.h
similarity index 100%
rename from stages/mm/pmm.h
rename to stage23/mm/pmm.h
diff --git a/stages/mm/vmm.c b/stage23/mm/vmm.c
similarity index 100%
rename from stages/mm/vmm.c
rename to stage23/mm/vmm.c
diff --git a/stages/mm/vmm.h b/stage23/mm/vmm.h
similarity index 100%
rename from stages/mm/vmm.h
rename to stage23/mm/vmm.h
diff --git a/stages/protos/chainload.c b/stage23/protos/chainload.c
similarity index 100%
rename from stages/protos/chainload.c
rename to stage23/protos/chainload.c
diff --git a/stages/protos/chainload.h b/stage23/protos/chainload.h
similarity index 100%
rename from stages/protos/chainload.h
rename to stage23/protos/chainload.h
diff --git a/stages/protos/linux.c b/stage23/protos/linux.c
similarity index 100%
rename from stages/protos/linux.c
rename to stage23/protos/linux.c
diff --git a/stages/protos/linux.h b/stage23/protos/linux.h
similarity index 100%
rename from stages/protos/linux.h
rename to stage23/protos/linux.h
diff --git a/stages/protos/stivale.c b/stage23/protos/stivale.c
similarity index 100%
rename from stages/protos/stivale.c
rename to stage23/protos/stivale.c
diff --git a/stages/protos/stivale.h b/stage23/protos/stivale.h
similarity index 100%
rename from stages/protos/stivale.h
rename to stage23/protos/stivale.h
diff --git a/stages/protos/stivale2.c b/stage23/protos/stivale2.c
similarity index 100%
rename from stages/protos/stivale2.c
rename to stage23/protos/stivale2.c
diff --git a/stages/protos/stivale2.h b/stage23/protos/stivale2.h
similarity index 100%
rename from stages/protos/stivale2.h
rename to stage23/protos/stivale2.h
diff --git a/stages/pxe/pxe.c b/stage23/pxe/pxe.c
similarity index 100%
rename from stages/pxe/pxe.c
rename to stage23/pxe/pxe.c
diff --git a/stages/pxe/pxe.h b/stage23/pxe/pxe.h
similarity index 100%
rename from stages/pxe/pxe.h
rename to stage23/pxe/pxe.h
diff --git a/stages/pxe/tftp.c b/stage23/pxe/tftp.c
similarity index 100%
rename from stages/pxe/tftp.c
rename to stage23/pxe/tftp.c
diff --git a/stages/pxe/tftp.h b/stage23/pxe/tftp.h
similarity index 100%
rename from stages/pxe/tftp.h
rename to stage23/pxe/tftp.h
diff --git a/stages/sys/a20.c b/stage23/sys/a20.c
similarity index 100%
rename from stages/sys/a20.c
rename to stage23/sys/a20.c
diff --git a/stages/sys/a20.h b/stage23/sys/a20.h
similarity index 100%
rename from stages/sys/a20.h
rename to stage23/sys/a20.h
diff --git a/stages/sys/cpu.h b/stage23/sys/cpu.h
similarity index 100%
rename from stages/sys/cpu.h
rename to stage23/sys/cpu.h
diff --git a/stages/sys/e820.c b/stage23/sys/e820.c
similarity index 100%
rename from stages/sys/e820.c
rename to stage23/sys/e820.c
diff --git a/stages/sys/e820.h b/stage23/sys/e820.h
similarity index 100%
rename from stages/sys/e820.h
rename to stage23/sys/e820.h
diff --git a/stages/sys/gdt.asm b/stage23/sys/gdt.asm
similarity index 100%
rename from stages/sys/gdt.asm
rename to stage23/sys/gdt.asm
diff --git a/stages/sys/lapic.c b/stage23/sys/lapic.c
similarity index 100%
rename from stages/sys/lapic.c
rename to stage23/sys/lapic.c
diff --git a/stages/sys/lapic.h b/stage23/sys/lapic.h
similarity index 100%
rename from stages/sys/lapic.h
rename to stage23/sys/lapic.h
diff --git a/stages/sys/pic.c b/stage23/sys/pic.c
similarity index 100%
rename from stages/sys/pic.c
rename to stage23/sys/pic.c
diff --git a/stages/sys/pic.h b/stage23/sys/pic.h
similarity index 100%
rename from stages/sys/pic.h
rename to stage23/sys/pic.h
diff --git a/stages/sys/smp.c b/stage23/sys/smp.c
similarity index 100%
rename from stages/sys/smp.c
rename to stage23/sys/smp.c
diff --git a/stages/sys/smp.h b/stage23/sys/smp.h
similarity index 100%
rename from stages/sys/smp.h
rename to stage23/sys/smp.h
diff --git a/stages/sys/smp_trampoline.asm b/stage23/sys/smp_trampoline.asm
similarity index 100%
rename from stages/sys/smp_trampoline.asm
rename to stage23/sys/smp_trampoline.asm
diff --git a/stages/tinf b/stage23/tinf
similarity index 100%
rename from stages/tinf
rename to stage23/tinf
diff --git a/stages/gensyms.sh b/stages/gensyms.sh
deleted file mode 100755
index 16145d55..00000000
--- a/stages/gensyms.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-
-set -e
-
-TMP1=$(mktemp)
-TMP2=$(mktemp)
-TMP3=$(mktemp)
-
-$1 -t stage2.elf | sed '/\bd\b/d' | sort > "$TMP1"
-grep "\.text" < "$TMP1" | cut -d' ' -f1 > "$TMP2"
-grep "\.text" < "$TMP1" | awk 'NF{ print $NF }' > "$TMP3"
-
-paste -d'$' "$TMP2" "$TMP3" | sed 's/^/dd 0x/g' | sed 's/$/", 0/g' | sed 's/\$/\ndb "/g' > symlist.gen
-
-echo "dd 0xffffffff" >> symlist.gen
-
-rm "$TMP1" "$TMP2" "$TMP3"
diff --git a/test/limine.cfg b/test/limine.cfg
index 4fefa794..263998ca 100644
--- a/test/limine.cfg
+++ b/test/limine.cfg
@@ -5,7 +5,7 @@ GRAPHICS=yes
 MENU_RESOLUTION=1024x768
 MENU_FONT=boot:///boot/font.bin
 E9_OUTPUT=yes
-STAGE2_MAP=boot:///boot/stages.map
+STAGE2_MAP=boot:///boot/limine.map
 
 THEME_COLOURS=60000000;aa0000;00aaff;aa5500;0000aa;aa00aa;9076de;aaaaaa
 THEME_MARGIN=64
tab: 248 wrap: offon