about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-09-23 14:13:28 +0200
committerptitSeb <sebastien.chev@gmail.com>2024-09-23 14:13:28 +0200
commite0ce06483c1bf676f81405c6048b39f8b993af2a (patch)
tree0ededf00ded1cdf0b1ec8c406ad6bbe8ef2ad58f /src
parent95e78088d6c8b066a169c0e25817f7d397f5ec0c (diff)
downloadbox64-e0ce06483c1bf676f81405c6048b39f8b993af2a.tar.gz
box64-e0ce06483c1bf676f81405c6048b39f8b993af2a.zip
[NON4K][ELFLOADER] more Elfloader work for non-4k pagesize OS ([BOX32] too)
Diffstat (limited to 'src')
-rw-r--r--src/elfs/elfloader.c6
-rw-r--r--src/elfs/elfloader32.c20
2 files changed, 17 insertions, 9 deletions
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index d7d8d539..bd89176b 100644
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -242,8 +242,10 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
         image = raw = mmap64((void*)head->vaddr, sz, 0, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
         if(head->vaddr&(box64_pagesize-1)) {
             // load address might be lower
-            if((uintptr_t)image == (head->vaddr&~(box64_pagesize-1)))
+            if((uintptr_t)image == (head->vaddr&~(box64_pagesize-1))) {
                 image = (void*)head->vaddr;
+                sz += ((uintptr_t)image)-((uintptr_t)raw);
+            }
         }
     }
     if(image!=MAP_FAILED && !head->vaddr && image!=(void*)offs) {
@@ -330,7 +332,7 @@ int AllocLoadElfMemory(box64context_t* context, elfheader_t* head, int mainbin)
                 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, "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);
+                    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);
                     p = mmap64(
                         (void*)paddr,
                         asize,
diff --git a/src/elfs/elfloader32.c b/src/elfs/elfloader32.c
index 57b366fc..cdddb9a1 100644
--- a/src/elfs/elfloader32.c
+++ b/src/elfs/elfloader32.c
@@ -160,8 +160,10 @@ int AllocLoadElfMemory32(box64context_t* context, elfheader_t* head, int mainbin
         image = raw = mmap64(from_ptrv(head->vaddr), sz, 0, MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
         if(from_ptr(head->vaddr)&(box64_pagesize-1)) {
             // load address might be lower
-            if((uintptr_t)image == (from_ptr(head->vaddr)&~(box64_pagesize-1)))
+            if((uintptr_t)image == (from_ptr(head->vaddr)&~(box64_pagesize-1))) {
                 image = from_ptrv(head->vaddr);
+                sz += ((uintptr_t)image)-((uintptr_t)raw);
+            }
         }
     }
     if(image!=MAP_FAILED && !head->vaddr && image!=from_ptrv(offs)) {
@@ -260,14 +262,18 @@ int AllocLoadElfMemory32(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<(size_t)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),