:: commit 8aa1372ce97770d9c85b91e4df932abc12e220f3

mintsuki <mintsuki@protonmail.com> — 2022-03-26 04:03

parents: 69557e9d2a

elf: Return image size

diff --git a/common/lib/elf.c b/common/lib/elf.c
index 63a5d301..9ce7ea5c 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) {
+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) {
     struct elf64_hdr hdr;
     memcpy(&hdr, elf + (0), sizeof(struct elf64_hdr));
 
@@ -548,6 +548,9 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_sl
 
         *physical_base = (uintptr_t)ext_mem_alloc_type_aligned(image_size, alloc_type, max_align);
         *virtual_base = min_vaddr;
+        if (_image_size) {
+            *_image_size = image_size;
+        }
     }
 
     if (!elf64_is_relocatable(elf, &hdr)) {
diff --git a/common/lib/elf.h b/common/lib/elf.h
index 98af704c..ecb93c7c 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);
+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_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 bba01494..54a1612f 100644
--- a/common/protos/limine.c
+++ b/common/protos/limine.c
@@ -146,17 +146,19 @@ bool limine_load(char *config, char *cmdline) {
     struct elf_range *ranges;
     uint64_t ranges_count;
 
+    uint64_t image_size;
+
     if (elf64_load(kernel, &entry_point, NULL, &slide,
                    MEMMAP_KERNEL_AND_MODULES, kaslr, false,
                    &ranges, &ranges_count,
-                   true, &physical_base, &virtual_base)) {
+                   true, &physical_base, &virtual_base, &image_size)) {
         return false;
     }
 
     // Load requests
     requests_count = 0;
     uint64_t common_magic[2] = { LIMINE_COMMON_MAGIC };
-    for (size_t i = 0; i < ALIGN_DOWN(kernel_file_size, 8); i += 8) {
+    for (size_t i = 0; i < ALIGN_DOWN(image_size, 8); i += 8) {
         uint64_t *p = (void *)(uintptr_t)physical_base + i;
 
         if (p[0] != common_magic[0]) {
diff --git a/common/protos/multiboot1.c b/common/protos/multiboot1.c
index 7902a481..4f8abbb6 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))
+                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, 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 1ed1b688..0d39f435 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))
+                if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL))
                     panic(true, "multiboot2: ELF64 load failure");
 
                 break;
diff --git a/common/protos/stivale.c b/common/protos/stivale.c
index 15df9f0e..60542d4c 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, false, 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 95ab5e99..28750b9d 100644
--- a/common/protos/stivale2.c
+++ b/common/protos/stivale2.c
@@ -188,7 +188,7 @@ 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))
+                               want_fully_virtual, &physical_base, &virtual_base, NULL))
                     panic(true, "stivale2: ELF64 load failure");
 
                 if (want_fully_virtual) {
tab: 248 wrap: offon