From 4768d5fc12a41afdee16419d100dae3770b76663 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 14:40:06 +0200 Subject: [PATCH] ELFLoader: Consider the offset when calculating how many pages to map If a section needs to be mapped at 0x50f50 and its size is 0x200, then that address space exceeds one page. But since 0x200 is less than one page, we only map one page. If we count the offset, 0xf50 + 0x200 need two pages. So we can map the right amount of memory. --- kernel/src/sys/elf/ELFLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/src/sys/elf/ELFLoader.cpp b/kernel/src/sys/elf/ELFLoader.cpp index 68d31f15..4c9f08c7 100644 --- a/kernel/src/sys/elf/ELFLoader.cpp +++ b/kernel/src/sys/elf/ELFLoader.cpp @@ -103,7 +103,7 @@ ELFImage* ELFLoader::load_elf_from_address(uintptr_t addr) kerrorln("Address is NULL, this is invalid :("); return 0; } - uint64_t pages = Utilities::get_blocks_from_size(PAGE_SIZE, phdr->p_memsz); + uint64_t pages = Utilities::get_blocks_from_size(PAGE_SIZE, (phdr->p_vaddr % PAGE_SIZE) + phdr->p_memsz); void* buffer = MemoryManager::get_pages_at(phdr->p_vaddr, pages, phdr->p_flags & 2 ? MAP_READ_WRITE | MAP_USER : MAP_USER); memcpy(buffer, (void*)(addr + phdr->p_offset), phdr->p_filesz);