:: commit 05c0508c310859ffb32866d64e6fe1c280cc81ce

mintsuki <mintsuki@protonmail.com> — 2022-06-29 10:32

parents: a5ad68b181

misc: Misc elsewhere and multiboot-related bug fixes

diff --git a/common/lib/elf.c b/common/lib/elf.c
index 03948527..073591ca 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -829,9 +829,9 @@ bool elf32_load_elsewhere(uint8_t *elf, uint64_t *entry_point,
             entry_adjusted = true;
         }
 
-        (*ranges[cur_entry]).elsewhere = (uintptr_t)elsewhere;
-        (*ranges[cur_entry]).target = phdr->p_paddr;
-        (*ranges[cur_entry]).length = phdr->p_memsz;
+        (*ranges)[cur_entry].elsewhere = (uintptr_t)elsewhere;
+        (*ranges)[cur_entry].target = phdr->p_paddr;
+        (*ranges)[cur_entry].length = phdr->p_memsz;
 
         cur_entry++;
     }
@@ -903,9 +903,9 @@ bool elf64_load_elsewhere(uint8_t *elf, uint64_t *entry_point,
             entry_adjusted = true;
         }
 
-        (*ranges[cur_entry]).elsewhere = (uintptr_t)elsewhere;
-        (*ranges[cur_entry]).target = phdr->p_paddr;
-        (*ranges[cur_entry]).length = phdr->p_memsz;
+        (*ranges)[cur_entry].elsewhere = (uintptr_t)elsewhere;
+        (*ranges)[cur_entry].target = phdr->p_paddr;
+        (*ranges)[cur_entry].length = phdr->p_memsz;
 
         cur_entry++;
     }
diff --git a/common/lib/elsewhere.c b/common/lib/elsewhere.c
index 8b2509ab..c27b74c1 100644
--- a/common/lib/elsewhere.c
+++ b/common/lib/elsewhere.c
@@ -46,7 +46,7 @@ retry:
                 if (!flexible_target) {
                     return false;
                 }
-                *target = top;
+                *target = ALIGN_UP(top, 4096);
                 goto retry;
             }
         }
diff --git a/common/protos/multiboot2.c b/common/protos/multiboot2.c
index d82aa009..2138939c 100644
--- a/common/protos/multiboot2.c
+++ b/common/protos/multiboot2.c
@@ -177,6 +177,8 @@ bool multiboot2_load(char *config, char* cmdline) {
         }
     }
 
+    struct elf_section_hdr_info *section_hdr_info = NULL;
+
     struct elsewhere_range *ranges;
     uint64_t ranges_count;
 
@@ -217,14 +219,16 @@ bool multiboot2_load(char *config, char* cmdline) {
 
         switch (bits) {
             case 32:
-                if (elf32_load_elsewhere(kernel, &e, &ranges, &ranges_count))
+                if (!elf32_load_elsewhere(kernel, &e, &ranges, &ranges_count))
                     panic(true, "multiboot2: ELF32 load failure");
 
+                section_hdr_info = elf32_section_hdr_info(kernel);
                 break;
             case 64: {
-                if (elf64_load_elsewhere(kernel, &e, &ranges, &ranges_count))
+                if (!elf64_load_elsewhere(kernel, &e, &ranges, &ranges_count))
                     panic(true, "multiboot2: ELF64 load failure");
 
+                section_hdr_info = elf64_section_hdr_info(kernel);
                 break;
             }
             default:
@@ -236,19 +240,6 @@ bool multiboot2_load(char *config, char* cmdline) {
         }
     }
 
-    struct elf_section_hdr_info *section_hdr_info = NULL;
-    int bits = elf_bits(kernel);
-
-    switch (bits) {
-        case 32:
-            section_hdr_info = elf32_section_hdr_info(kernel);
-            break;
-        case 64: {
-            section_hdr_info = elf64_section_hdr_info(kernel);
-            break;
-        }
-    }
-
     size_t modules_size = 0;
     size_t n_modules;
 
diff --git a/common/protos/multiboot_reloc.asm_x86 b/common/protos/multiboot_reloc.asm_x86
index 6bb63107..0ad42b76 100644
--- a/common/protos/multiboot_reloc.asm_x86
+++ b/common/protos/multiboot_reloc.asm_x86
@@ -16,6 +16,7 @@ multiboot_reloc_stub:
     ; EDX = ranges count
 
   .code:
+    mov esp, .mini_stack_top
 
     push edi
     push esi
@@ -53,5 +54,9 @@ multiboot_reloc_stub:
 
     ret
 
+ .mini_stack:
+    times 3 dq 0
+ .mini_stack_top:
+
 global multiboot_reloc_stub_end
 multiboot_reloc_stub_end:
diff --git a/test/GNUmakefile b/test/GNUmakefile
index d4910af4..f96c86b9 100644
--- a/test/GNUmakefile
+++ b/test/GNUmakefile
@@ -18,21 +18,20 @@ INTERNAL_LD_FLAGS_MULTIBOOT2 := \
 	-Tmultiboot2.ld             \
 	-nostdlib                   \
 	-zmax-page-size=0x1000      \
-	-static                     \
-	--no-dynamic-linker         \
+	-static
 
 INTERNAL_LD_FLAGS_MULTIBOOT1 := \
 	-Tmultiboot.ld              \
 	-nostdlib                   \
 	-zmax-page-size=0x1000      \
-	-static                     \
-	--no-dynamic-linker         \
+	-static
 
 INTERNALCFLAGS  :=       \
 	-I../stivale         \
 	-I.                  \
 	-I..                 \
 	-std=c11             \
+	-g                   \
 	-ffreestanding       \
 	-fno-stack-protector \
 	-fpie                \
@@ -44,26 +43,44 @@ INTERNALCFLAGS  :=       \
 	-mno-sse2            \
 	-mno-red-zone
 
+INTERNALCFLAGS_MB  :=    \
+	-I.                  \
+	-I..                 \
+	-I../common/protos   \
+	-std=c11             \
+	-m32                 \
+	-g                   \
+	-ffreestanding       \
+	-fno-stack-protector \
+	-fno-pie             \
+	-fno-pic             \
+	-mabi=sysv           \
+	-mno-80387           \
+	-mno-mmx             \
+	-mno-3dnow           \
+	-mno-sse             \
+	-mno-sse2
+
 all: test.elf multiboot2.elf multiboot.elf
 
 test.elf: stivale.o stivale2.o limine.o e9print.o memory.o
 	$(LD) $^ $(LDFLAGS) $(INTERNALLDFLAGS) -o $@
 
 multiboot2.elf: multiboot2_trampoline.o
-	$(CC) $(CFLAGS) $(INTERNALCFLAGS) -I../common/protos -m32 -c multiboot2.c -o multiboot2.o
-	$(CC) $(CFLAGS) $(INTERNALCFLAGS) -m32 -c e9print.c -o e9print.o
+	$(CC) $(CFLAGS) $(INTERNALCFLAGS_MB) -c multiboot2.c -o multiboot2.o
+	$(CC) $(CFLAGS) $(INTERNALCFLAGS_MB) -c e9print.c -o e9print.o
 	$(LD) $^ multiboot2.o e9print.o $(LDFLAGS) $(INTERNAL_LD_FLAGS_MULTIBOOT2) -m elf_i386 -o $@
 
 multiboot.elf: multiboot_trampoline.o
-	$(CC) $(CFLAGS) $(INTERNALCFLAGS) -I../common/protos -m32 -c multiboot.c -o multiboot.o
-	$(CC) $(CFLAGS) $(INTERNALCFLAGS) -m32 -c e9print.c -o e9print.o
+	$(CC) $(CFLAGS) $(INTERNALCFLAGS_MB) -c multiboot.c -o multiboot.o
+	$(CC) $(CFLAGS) $(INTERNALCFLAGS_MB) -c e9print.c -o e9print.o
 	$(LD) $^ multiboot.o e9print.o $(LDFLAGS) $(INTERNAL_LD_FLAGS_MULTIBOOT1) -m elf_i386 -o $@
 
 %.o: %.c
 	$(CC) $(CFLAGS) $(INTERNALCFLAGS) -c $< -o $@
 
 %.o: %.asm
-	nasm -felf32 $< -o $@
+	nasm -felf32 -F dwarf -g $< -o $@
 
 clean:
 	rm -rf test.elf limine.o stivale.o stivale2.o e9print.o memory.o
diff --git a/test/multiboot.ld b/test/multiboot.ld
index 308ea1d6..7bcdc517 100644
--- a/test/multiboot.ld
+++ b/test/multiboot.ld
@@ -1,7 +1,7 @@
 ENTRY(_start)
 
 SECTIONS {
-    . = 0x1000;
+    . = 0x10000;
 
     .boot :
     {
tab: 248 wrap: offon