Skip to content

Commit

Permalink
[ELFLOADER] Fixed some residual issue with elfloader (espacially on n…
Browse files Browse the repository at this point in the history
…on-4K pagesize) (should help ptitSeb#1398 ptitSeb#1271 maybe help ptitSeb#939 and probably others)
  • Loading branch information
ptitSeb authored and Javier97sm committed Aug 16, 2024
1 parent 0ebb14a commit 011d592
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/elfs/elfloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit 011d592

Please sign in to comment.