From 22587856fda325add521661facc96ebf1e69bf42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20S=C5=82omi=C5=84ski?= Date: Tue, 29 Jun 2021 15:19:46 +0200 Subject: [PATCH] elf: adjust entry point address to paddr in elf32_load --- stage23/lib/elf.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/stage23/lib/elf.c b/stage23/lib/elf.c index fefc39b8..be5ab875 100644 --- a/stage23/lib/elf.c +++ b/stage23/lib/elf.c @@ -392,6 +392,9 @@ int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t alloc_type) { return -1; } + uint32_t entry = hdr.entry; + bool entry_adjusted = false; + for (uint16_t i = 0; i < hdr.ph_num; i++) { struct elf32_phdr phdr; memcpy(&phdr, elf + (hdr.phoff + i * sizeof(struct elf32_phdr)), @@ -410,9 +413,15 @@ int elf32_load(uint8_t *elf, uint32_t *entry_point, uint32_t alloc_type) { void *ptr = (void *)(uintptr_t)(phdr.p_paddr + phdr.p_filesz); memset(ptr, 0, to_zero); } + + 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; + } } - *entry_point = hdr.entry; + *entry_point = entry; return 0; }