misc: Split off a stage 2 symbol map and embed it into stage 2 so that it can print stacktrace symbol names before stage 3 is loaded
diff --git a/stage23/Makefile b/stage23/Makefile
index f1699641..08e0a62b 100644
--- a/stage23/Makefile
+++ b/stage23/Makefile
@@ -74,7 +74,8 @@ ifeq ($(TARGET), bios)
-static \
-fno-pie \
-lgcc \
- -static-libgcc
+ -static-libgcc \
+ -Wl,--gc-sections
endif
.PHONY: all clean builddir
@@ -121,24 +122,31 @@ $(BUILDDIR)/stage2.bin.gz: $(BUILDDIR)/stage2.bin
$(BUILDDIR)/stage2.bin: $(BUILDDIR)/limine.sys
dd if=$< bs=$$(( 0x$$($(READELF) -S $(BUILDDIR)/limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
-$(BUILDDIR)/limine.map.o: $(BUILDDIR)/limine_nomap.elf
+$(BUILDDIR)/stage2.map.o: $(BUILDDIR)/limine_stage2only.elf
GENSYMS="`pwd`/gensyms.sh" && \
cd "`dirname $<`" && \
- "$$GENSYMS" $(OBJDUMP) $< limine
+ "$$GENSYMS" $(OBJDUMP) $< stage2
+
+$(BUILDDIR)/full.map.o: $(BUILDDIR)/limine_nomap.elf
+ GENSYMS="`pwd`/gensyms.sh" && \
+ cd "`dirname $<`" && \
+ "$$GENSYMS" $(OBJDUMP) $< full
$(BUILDDIR)/limine.sys: $(BUILDDIR)/limine.elf
$(OBJCOPY) -O binary $< $@
-$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
- $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
- $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Wl,--gc-sections -Tlinker_stage2only.ld -o $(BUILDDIR)/limine_stage2only.elf || \
+$(BUILDDIR)/limine_stage2only.elf: $(OBJ)
+ $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@ || \
( echo "This error means that stage 2 was trying to use stage 3 symbols before loading stage 3" && \
false )
-$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/limine.map.o
+$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o
+ $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
+
+$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
-$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
+$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
endif
diff --git a/stage23/gensyms.sh b/stage23/gensyms.sh
index 8aacfaa2..fe6c0618 100755
--- a/stage23/gensyms.sh
+++ b/stage23/gensyms.sh
@@ -11,7 +11,7 @@ $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 "section .$3_map" > "$TMP4"
echo "global $3_map" >> "$TMP4"
echo "$3_map:" >> "$TMP4"
diff --git a/stage23/lib/trace.s2.c b/stage23/lib/trace.s2.c
index 3b6f8639..b8f073be 100644
--- a/stage23/lib/trace.s2.c
+++ b/stage23/lib/trace.s2.c
@@ -8,12 +8,16 @@
#include <fs/file.h>
#include <mm/pmm.h>
-extern symbol limine_map;
+extern symbol stage2_map, full_map;
char *trace_address(size_t *off, size_t addr) {
+ char *limine_map;
+
#if defined (bios)
if (!stage3_loaded)
- return NULL;
+ limine_map = stage2_map;
+ else
+ limine_map = full_map;
#endif
uintptr_t prev_addr = 0;
diff --git a/stage23/linker.ld b/stage23/linker.ld
index b99c1e7a..7b2f9b85 100644
--- a/stage23/linker.ld
+++ b/stage23/linker.ld
@@ -23,6 +23,7 @@ SECTIONS
*.s2.o(.rodata*)
*libgcc.a:*(.data*)
*libgcc.a:*(.rodata*)
+ *(.stage2_map)
}
.stage3.text : {
@@ -35,10 +36,7 @@ SECTIONS
.stage3.data : {
*(.data*)
*(.rodata*)
- }
-
- .map : {
- *(.map*)
+ *(.full_map*)
limine_sys_size = . - 0x8000;
}
diff --git a/stage23/linker_dbg.ld b/stage23/linker_dbg.ld
index 138c2bbd..5a7f1718 100644
--- a/stage23/linker_dbg.ld
+++ b/stage23/linker_dbg.ld
@@ -23,6 +23,7 @@ SECTIONS
*.s2.o(.rodata*)
*libgcc.a:*(.data*)
*libgcc.a:*(.rodata*)
+ *(.stage2_map)
}
.stage3.text : {
@@ -35,11 +36,8 @@ SECTIONS
.stage3.data : {
*(.data*)
*(.rodata*)
- }
-
- .map : {
- limine_map = .;
- limine_sys_size = .;
+ *(.full_map*)
+ limine_sys_size = . - 0x8000;
}
.bss : {
diff --git a/stage23/linker_nomap.ld b/stage23/linker_nomap.ld
index d6c4226c..04e39b9e 100644
--- a/stage23/linker_nomap.ld
+++ b/stage23/linker_nomap.ld
@@ -23,6 +23,7 @@ SECTIONS
*.s2.o(.rodata*)
*libgcc.a:*(.data*)
*libgcc.a:*(.rodata*)
+ *(.stage2_map)
}
.stage3.text : {
@@ -35,11 +36,8 @@ SECTIONS
.stage3.data : {
*(.data*)
*(.rodata*)
- }
-
- .map : {
- limine_map = .;
- limine_sys_size = .;
+ full_map = .;
+ limine_sys_size = . - 0x8000;
}
.bss : {
diff --git a/stage23/linker_stage2only.ld b/stage23/linker_stage2only.ld
index 16689bf8..f2739603 100644
--- a/stage23/linker_stage2only.ld
+++ b/stage23/linker_stage2only.ld
@@ -23,9 +23,10 @@ SECTIONS
*.s2.o(.rodata*)
*libgcc.a:*(.data*)
*libgcc.a:*(.rodata*)
+ stage2_map = .;
stage3_addr = .;
*(.stage3_build_id*)
- limine_map = .;
+ full_map = .;
limine_sys_size = .;
}
