elf: Add support for sliding and ELF ranges to ELF32 as well
diff --git a/common/lib/elf.c b/common/lib/elf.c
index 9028cca8..df67a720 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -393,6 +393,56 @@ out:
return ret;
}
+static void elf32_get_ranges(uint8_t *elf, uint64_t slide, struct elf_range **_ranges, uint64_t *_ranges_count) {
+ struct elf32_hdr hdr;
+ memcpy(&hdr, elf + (0), sizeof(struct elf32_hdr));
+
+ uint64_t ranges_count = 0;
+
+ if (hdr.phdr_size < sizeof(struct elf32_phdr)) {
+ panic(true, "elf: phdr_size < sizeof(struct elf64_phdr)");
+ }
+
+ for (uint16_t i = 0; i < hdr.ph_num; i++) {
+ struct elf32_phdr phdr;
+ memcpy(&phdr, elf + (hdr.phoff + i * hdr.phdr_size),
+ sizeof(struct elf32_phdr));
+
+ if (phdr.p_type != PT_LOAD)
+ continue;
+
+ ranges_count++;
+ }
+
+ if (ranges_count == 0) {
+ panic(true, "elf: Attempted to use PMRs but no higher half PHDRs exist");
+ }
+
+ struct elf_range *ranges = ext_mem_alloc(ranges_count * sizeof(struct elf_range));
+
+ size_t r = 0;
+ for (uint16_t i = 0; i < hdr.ph_num; i++) {
+ struct elf32_phdr phdr;
+ memcpy(&phdr, elf + (hdr.phoff + i * hdr.phdr_size),
+ sizeof(struct elf32_phdr));
+
+ if (phdr.p_type != PT_LOAD)
+ continue;
+
+ uint64_t load_addr = phdr.p_paddr + slide;
+ uint64_t this_top = load_addr + phdr.p_memsz;
+
+ ranges[r].base = load_addr;
+ ranges[r].length = this_top - ranges[r].base;
+ ranges[r].permissions = phdr.p_flags & 0b111;
+
+ r++;
+ }
+
+ *_ranges_count = ranges_count;
+ *_ranges = ranges;
+}
+
static uint64_t elf64_max_align(uint8_t *elf) {
uint64_t ret = 0;
@@ -725,7 +775,7 @@ final:
return 0;
}
-int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t alloc_type) {
+int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t alloc_type, uint64_t *_slide, struct elf_range **ranges, uint64_t *ranges_count) {
struct elf32_hdr hdr;
memcpy(&hdr, elf + (0), sizeof(struct elf32_hdr));
@@ -744,9 +794,15 @@ int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t allo
return -1;
}
+ uint64_t slide = 0;
+ bool simulation = true;
+ size_t try_count = 0;
+ size_t max_simulated_tries = 0x100000;
+
uint32_t entry = hdr.entry;
bool entry_adjusted = false;
+again:
if (top)
*top = 0;
@@ -767,21 +823,33 @@ int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t allo
panic(true, "elf: p_filesz > p_memsz");
}
+ uint64_t load_addr = phdr.p_paddr + slide;
+
if (top) {
- uint32_t this_top = phdr.p_paddr + phdr.p_memsz;
+ uint32_t this_top = load_addr + phdr.p_memsz;
if (this_top > *top) {
*top = this_top;
}
}
- memmap_alloc_range((size_t)phdr.p_paddr, (size_t)phdr.p_memsz, alloc_type, true, true, false, false);
+ if (!memmap_alloc_range((size_t)load_addr, (size_t)phdr.p_memsz, alloc_type, true, false, simulation, false)) {
+ if (simulation == false || ++try_count == max_simulated_tries) {
+ panic(true, "elf: Failed to allocate necessary memory range (%X-%X)", load_addr, load_addr + phdr.p_memsz);
+ }
+ slide += 0x1000;
+ goto again;
+ }
+
+ if (simulation) {
+ continue;
+ }
- memcpy((void *)(uintptr_t)phdr.p_paddr, elf + (phdr.p_offset), phdr.p_filesz);
+ memcpy((void *)(uintptr_t)load_addr, elf + (phdr.p_offset), phdr.p_filesz);
size_t to_zero = (size_t)(phdr.p_memsz - phdr.p_filesz);
if (to_zero) {
- void *ptr = (void *)(uintptr_t)(phdr.p_paddr + phdr.p_filesz);
+ void *ptr = (void *)(uintptr_t)(load_addr + phdr.p_filesz);
memset(ptr, 0, to_zero);
}
@@ -792,7 +860,18 @@ int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t allo
}
}
- *entry_point = entry;
+ if (simulation) {
+ simulation = false;
+ goto again;
+ }
+
+ *entry_point = entry + slide;
+ if (_slide)
+ *_slide = slide;
+
+ if (ranges_count != NULL && ranges != NULL) {
+ elf32_get_ranges(elf, slide, ranges, ranges_count);
+ }
return 0;
}
diff --git a/common/lib/elf.h b/common/lib/elf.h
index 6c91e2c7..777c9465 100644
--- a/common/lib/elf.h
+++ b/common/lib/elf.h
@@ -30,7 +30,7 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *top, uint64_t *_sl
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);
-int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t alloc_type);
+int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t *top, uint32_t alloc_type, uint64_t *_slide, struct elf_range **ranges, uint64_t *ranges_count);
int elf32_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit);
struct elf_section_hdr_info* elf32_section_hdr_info(uint8_t *elf);
diff --git a/common/protos/multiboot1.c b/common/protos/multiboot1.c
index 9aacae80..cda0c88a 100644
--- a/common/protos/multiboot1.c
+++ b/common/protos/multiboot1.c
@@ -134,7 +134,7 @@ bool multiboot1_load(char *config, char *cmdline) {
switch (bits) {
case 32:
- if (elf32_load(kernel, &entry_point, &kernel_top, MEMMAP_BOOTLOADER_RECLAIMABLE))
+ if (elf32_load(kernel, &entry_point, &kernel_top, MEMMAP_BOOTLOADER_RECLAIMABLE, &slide, &elf_ranges, &elf_ranges_count))
panic(true, "multiboot1: ELF32 load failure");
break;
case 64: {
diff --git a/common/protos/multiboot2.c b/common/protos/multiboot2.c
index b8cebc72..9d8e232e 100644
--- a/common/protos/multiboot2.c
+++ b/common/protos/multiboot2.c
@@ -237,7 +237,7 @@ bool multiboot2_load(char *config, char* cmdline) {
switch (bits) {
case 32:
- if (elf32_load(kernel, (uint32_t *)&e, (uint32_t *)&t, MEMMAP_BOOTLOADER_RECLAIMABLE))
+ if (elf32_load(kernel, (uint32_t *)&e, (uint32_t *)&t, MEMMAP_BOOTLOADER_RECLAIMABLE, &slide, &elf_ranges, &elf_ranges_count))
panic(true, "multiboot2: ELF32 load failure");
break;
diff --git a/test/multiboot.ld b/test/multiboot.ld
index 22de8775..308ea1d6 100644
--- a/test/multiboot.ld
+++ b/test/multiboot.ld
@@ -1,7 +1,7 @@
ENTRY(_start)
SECTIONS {
- . = 1M;
+ . = 0x1000;
.boot :
{
