about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-04-02 15:49:51 +0200
committerptitSeb <sebastien.chev@gmail.com>2024-04-02 15:49:51 +0200
commit3d211143e062566d55be50972e5f7dc6a2bfe820 (patch)
tree116818cdcc71086d98c2d82cce1eb72f058cd825
parentc1c11d8daaaee82acd1be3521c280bc8eb72d589 (diff)
downloadbox64-3d211143e062566d55be50972e5f7dc6a2bfe820.tar.gz
box64-3d211143e062566d55be50972e5f7dc6a2bfe820.zip
[ELFLOADER] Fixed some residual issue with elfloader (espacially on non-4K pagesize) (should help #1398 #1271 maybe help #939 and probably others)
-rw-r--r--src/elfs/elfloader.c20
1 files changed, 12 insertions, 8 deletions
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(diff<new_size)
+                            new_size -= diff;
+                        else
+                            new_size = 0;
+                        new_addr = ALIGN(new_addr+1);
                     }
                     if(new_size>0) {
-                        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),