diff --git a/stage23/lib/elf.c b/stage23/lib/elf.c index be5ab875..132bf6c2 100644 --- a/stage23/lib/elf.c +++ b/stage23/lib/elf.c @@ -292,7 +292,7 @@ int elf32_load_section(uint8_t *elf, void *buffer, const char *name, size_t limi return 2; } -int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *_slide, uint32_t alloc_type, bool kaslr) { +int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *_slide, uint32_t alloc_type, bool kaslr, bool use_paddr) { struct elf64_hdr hdr; memcpy(&hdr, elf + (0), sizeof(struct elf64_hdr)); @@ -316,6 +316,9 @@ int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *_slide, uint32_t a size_t try_count = 0; size_t max_simulated_tries = 250; + uint64_t entry = hdr.entry; + bool entry_adjusted = false; + if (!elf64_is_relocatable(elf, &hdr)) { simulation = false; goto final; @@ -334,14 +337,20 @@ final: if (phdr.p_type != PT_LOAD) continue; - uint64_t load_vaddr = phdr.p_vaddr; + uint64_t load_addr = 0; - if (load_vaddr & ((uint64_t)1 << 63)) - load_vaddr -= FIXED_HIGHER_HALF_OFFSET_64; + if (use_paddr) { + load_addr = phdr.p_paddr; + } else { + load_addr = phdr.p_vaddr; - load_vaddr += slide; + if (load_addr & ((uint64_t)1 << 63)) + load_addr -= FIXED_HIGHER_HALF_OFFSET_64; + } - if (!memmap_alloc_range((size_t)load_vaddr, (size_t)phdr.p_memsz, alloc_type, true, false, simulation, false)) { + load_addr += slide; + + if (!memmap_alloc_range((size_t)load_addr, (size_t)phdr.p_memsz, alloc_type, true, false, simulation, false)) { if (++try_count == max_simulated_tries || simulation == false) return -1; if (!kaslr) @@ -349,17 +358,25 @@ final: goto again; } - memcpy((void *)(uintptr_t)load_vaddr, 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)(load_vaddr + phdr.p_filesz); + void *ptr = (void *)(uintptr_t)(load_addr + phdr.p_filesz); memset(ptr, 0, to_zero); } - if (elf64_apply_relocations(elf, &hdr, (void *)(uintptr_t)load_vaddr, phdr.p_vaddr, phdr.p_memsz, slide)) + if (elf64_apply_relocations(elf, &hdr, (void *)(uintptr_t)load_addr, phdr.p_vaddr, phdr.p_memsz, slide)) return -1; + + if (use_paddr) { + if (!entry_adjusted && entry >= phdr.p_vaddr && entry <= (phdr.p_vaddr + phdr.p_memsz)) { + entry -= phdr.p_vaddr; + entry += phdr.p_paddr; + entry_adjusted = true; + } + } } if (simulation) { @@ -367,8 +384,9 @@ final: goto final; } - *entry_point = hdr.entry + slide; - *_slide = slide; + *entry_point = entry + slide; + if (_slide) + *_slide = slide; return 0; } diff --git a/stage23/lib/elf.h b/stage23/lib/elf.h index e9b0b6c2..be38ec13 100644 --- a/stage23/lib/elf.h +++ b/stage23/lib/elf.h @@ -9,7 +9,7 @@ int elf_bits(uint8_t *elf); -int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *slide, uint32_t alloc_type, bool kaslr); +int elf64_load(uint8_t *elf, uint64_t *entry_point, uint64_t *slide, uint32_t alloc_type, bool kaslr, bool use_paddr); int elf64_load_section(uint8_t *elf, void *buffer, const char *name, size_t limit, uint64_t slide); int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t alloc_type); diff --git a/stage23/protos/stivale.c b/stage23/protos/stivale.c index 25b99265..841c80b9 100644 --- a/stage23/protos/stivale.c +++ b/stage23/protos/stivale.c @@ -79,7 +79,7 @@ void stivale_load(char *config, char *cmdline) { level5pg = true; } - if (elf64_load(kernel, &entry_point, &slide, STIVALE_MMAP_KERNEL_AND_MODULES, kaslr)) + if (elf64_load(kernel, &entry_point, &slide, STIVALE_MMAP_KERNEL_AND_MODULES, kaslr, false)) panic("stivale: ELF64 load failure"); ret = elf64_load_section(kernel, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide); diff --git a/stage23/protos/stivale2.c b/stage23/protos/stivale2.c index 70271477..9159da79 100644 --- a/stage23/protos/stivale2.c +++ b/stage23/protos/stivale2.c @@ -101,7 +101,7 @@ void stivale2_load(char *config, char *cmdline, bool pxe, void *efi_system_table level5pg = true; } - if (elf64_load(kernel, &entry_point, &slide, STIVALE2_MMAP_KERNEL_AND_MODULES, kaslr)) + if (elf64_load(kernel, &entry_point, &slide, STIVALE2_MMAP_KERNEL_AND_MODULES, kaslr, false)) panic("stivale2: ELF64 load failure"); ret = elf64_load_section(kernel, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);