:: commit 41ea16e2ff3af4256b3132ea13b0f9aca2c70dbe

mintsuki <mintsuki@protonmail.com> — 2022-03-28 03:13

parents: 2cc4ff856f

elf: Return is_reloc

diff --git a/common/lib/elf.c b/common/lib/elf.c
index 9ce7ea5c..df516193 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -479,7 +479,7 @@ static void elf64_get_ranges(uint8_t *elf, uint64_t slide, bool use_paddr, struc
     *_ranges = ranges;
 }
 
-int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr, struct elf_range **ranges, uint64_t *ranges_count, bool fully_virtual, uint64_t *physical_base, uint64_t *virtual_base, uint64_t *_image_size) {
+int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr, struct elf_range **ranges, uint64_t *ranges_count, bool fully_virtual, uint64_t *physical_base, uint64_t *virtual_base, uint64_t *_image_size, bool *is_reloc) {
     struct elf64_hdr hdr;
     memcpy(&hdr, elf + (0), sizeof(struct elf64_hdr));
 
@@ -496,6 +496,10 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_sl
         panic(true, "elf: Not an x86_64 ELF file.\n");
     }
 
+    if (is_reloc) {
+        *is_reloc = false;
+    }
+
     uint64_t slide = 0;
     bool simulation = true;
     size_t try_count = 0;
@@ -556,6 +560,10 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_sl
     if (!elf64_is_relocatable(elf, &hdr)) {
         simulation = false;
         goto final;
+    } else {
+        if (is_reloc) {
+            *is_reloc = true;
+        }
     }
 
 again:
diff --git a/common/lib/elf.h b/common/lib/elf.h
index ecb93c7c..afd31a0c 100644
--- a/common/lib/elf.h
+++ b/common/lib/elf.h
@@ -27,7 +27,7 @@ struct elf_section_hdr_info {
 
 int elf_bits(uint8_t *elf);
 
-int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr, struct elf_range **ranges, uint64_t *ranges_count, bool fully_virtual, uint64_t *physical_base, uint64_t *virtual_base, uint64_t *image_size);
+int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr, struct elf_range **ranges, uint64_t *ranges_count, bool fully_virtual, uint64_t *physical_base, uint64_t *virtual_base, uint64_t *image_size, bool *is_reloc);
 int elf64_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit, uint64_t slide);
 struct elf_section_hdr_info* elf64_section_hdr_info(uint8_t *elf);
 
diff --git a/common/protos/limine.c b/common/protos/limine.c
index 297c2368..cac3240d 100644
--- a/common/protos/limine.c
+++ b/common/protos/limine.c
@@ -142,14 +142,18 @@ bool limine_load(char *config, char *cmdline) {
     uint64_t ranges_count;
 
     uint64_t image_size;
+    bool is_reloc;
 
     if (elf64_load(kernel, &entry_point, NULL, &slide,
                    MEMMAP_KERNEL_AND_MODULES, kaslr, false,
                    &ranges, &ranges_count,
-                   true, &physical_base, &virtual_base, &image_size)) {
+                   true, &physical_base, &virtual_base, &image_size,
+                   &is_reloc)) {
         return false;
     }
 
+    kaslr = is_reloc;
+
     // Load requests
     requests_count = 0;
     uint64_t common_magic[2] = { LIMINE_COMMON_MAGIC };
diff --git a/common/protos/multiboot1.c b/common/protos/multiboot1.c
index 4f8abbb6..7e9497c4 100644
--- a/common/protos/multiboot1.c
+++ b/common/protos/multiboot1.c
@@ -106,7 +106,7 @@ bool multiboot1_load(char *config, char *cmdline) {
                 break;
             case 64: {
                 uint64_t e, t;
-                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL))
+                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL, NULL))
                     panic(true, "multiboot1: ELF64 load failure");
                 entry_point = e;
                 kernel_top = t;
diff --git a/common/protos/multiboot2.c b/common/protos/multiboot2.c
index 0d39f435..98eff332 100644
--- a/common/protos/multiboot2.c
+++ b/common/protos/multiboot2.c
@@ -219,7 +219,7 @@ bool multiboot2_load(char *config, char* cmdline) {
 
                 break;
             case 64: {
-                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL))
+                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL, NULL))
                     panic(true, "multiboot2: ELF64 load failure");
 
                 break;
diff --git a/common/protos/stivale.c b/common/protos/stivale.c
index 60542d4c..a8cae28d 100644
--- a/common/protos/stivale.c
+++ b/common/protos/stivale.c
@@ -143,7 +143,7 @@ bool stivale_load(char *config, char *cmdline) {
             if (!loaded_by_anchor) {
                 if (elf64_load(kernel, &entry_point, NULL, &slide,
                                STIVALE_MMAP_KERNEL_AND_MODULES, kaslr, false,
-                               NULL, NULL, false, NULL, NULL, NULL))
+                               NULL, NULL, false, NULL, NULL, NULL, NULL))
                     panic(true, "stivale: ELF64 load failure");
 
                 ret = elf64_load_section(kernel, &stivale_hdr, ".stivalehdr",
diff --git a/common/protos/stivale2.c b/common/protos/stivale2.c
index a1eb16ef..87be3cd1 100644
--- a/common/protos/stivale2.c
+++ b/common/protos/stivale2.c
@@ -188,7 +188,8 @@ bool stivale2_load(char *config, char *cmdline) {
                                STIVALE2_MMAP_KERNEL_AND_MODULES, kaslr, false,
                                want_pmrs ? &ranges : NULL,
                                want_pmrs ? &ranges_count : NULL,
-                               want_fully_virtual, &physical_base, &virtual_base, NULL))
+                               want_fully_virtual, &physical_base, &virtual_base,
+                               NULL, NULL))
                     panic(true, "stivale2: ELF64 load failure");
 
                 if (want_fully_virtual) {
tab: 248 wrap: offon