From 3d211143e062566d55be50972e5f7dc6a2bfe820 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Tue, 2 Apr 2024 15:49:51 +0200 Subject: [ELFLOADER] Fixed some residual issue with elfloader (espacially on non-4K pagesize) (should help #1398 #1271 maybe help #939 and probably others) --- src/elfs/elfloader.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c index 29a247c6..ebd5f667 100644 --- a/src/elfs/elfloader.c +++ b/src/elfs/elfloader.c @@ -277,10 +277,10 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) } if(!try_mmap) { uintptr_t paddr = head->multiblocks[n].paddr&~balign; - size_t asize = head->multiblocks[n].asize; + size_t asize = head->multiblocks[n].asize+(head->multiblocks[n].paddr-paddr); void* p = MAP_FAILED; if(paddr==(paddr&~(box64_pagesize-1)) && (asize==ALIGN(asize))) { - printf_dump(log_level, "Allocating 0x%zx (0x%zx) bytes @%p, will read 0x%zx @%p for Elf \"%s\"\n", asize, e->p_memsz, (void*)paddr, e->p_filesz, (void*)head->multiblocks[n].paddr, head->name); + printf_dump(log_level, "Mmapping 0x%zx (0x%zx) bytes @%p, will read 0x%zx @%p for Elf \"%s\"\n", asize, e->p_memsz, (void*)paddr, e->p_filesz, (void*)head->multiblocks[n].paddr, head->name); p = mmap64( (void*)paddr, asize, @@ -292,14 +292,18 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin) } else { // difference in pagesize, so need to mmap only what needed to be... //check startint point - uintptr_t new_addr = paddr; - ssize_t new_size = asize; - while(getProtection(new_addr) && (new_size>0)) { - new_size -= ALIGN(new_addr) - new_addr; - new_addr = ALIGN(new_addr); + uintptr_t new_addr = paddr&~(box64_pagesize-1); // new_addr might be smaller than paddr + ssize_t new_size = asize + (paddr-new_addr); // so need new_size to compensate + while(getProtection(new_addr) && (new_size>0)) {// but then, there might be some overlap + uintptr_t diff = ALIGN(new_addr+1) - new_addr; // next page + if(diff0) { - printf_dump(log_level, "Allocating 0x%zx (0x%zx) bytes @%p, will read 0x%zx @%p for Elf \"%s\"\n", ALIGN(new_size), e->p_memsz, (void*)new_addr, e->p_filesz, (void*)head->multiblocks[n].paddr, head->name); + printf_dump(log_level, "Allocating 0x%zx (0x%zx/0x%zx) bytes @%p, will read 0x%zx @%p for Elf \"%s\"\n", ALIGN(new_size), paddr, e->p_memsz, (void*)new_addr, e->p_filesz, (void*)head->multiblocks[n].paddr, head->name); p = mmap64( (void*)new_addr, ALIGN(new_size), -- cgit 1.4.1