Support loading unaligned binaries

This commit is contained in:
Ali Mashtizadeh 2023-08-16 23:05:52 -04:00
parent a7c7a06a5b
commit febc7b7997

View File

@ -51,26 +51,26 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff); phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff);
if (!Loader_CheckHeader(ehdr)) { if (!Loader_CheckHeader(ehdr)) {
kprintf("Not a valid executable!\n"); Log(loader, "Not a valid executable!\n");
return false; return false;
} }
kprintf("%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize"); Log(loader, "%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
for (i = 0; i < ehdr->e_phnum; i++) for (i = 0; i < ehdr->e_phnum; i++)
{ {
ASSERT(phdr[i].p_type != PT_DYNAMIC); ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) { if (phdr[i].p_type == PT_LOAD) {
uint64_t va = phdr[i].p_vaddr; uint64_t va = phdr[i].p_vaddr;
uint64_t memsz = phdr[i].p_memsz; uint64_t memsz = phdr[i].p_memsz;
kprintf("%08llx %016llx %08llx %08llx\n", phdr[i].p_offset, Log(loader, "%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz); phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
// Make sure it is page aligned // Make sure it is page aligned
va = va & ~(uint64_t)PGMASK; va = va & ~(uint64_t)PGMASK;
memsz += phdr[i].p_vaddr - va; memsz += phdr[i].p_vaddr - va;
kprintf("%016llx %08llx\n", va, memsz); Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, phdr[i].p_memsz, PTE_W)) { if (!PMap_AllocMap(as, va, memsz, PTE_W)) {
// XXX: Cleanup! // XXX: Cleanup!
ASSERT(false); ASSERT(false);
return false; return false;
@ -86,6 +86,7 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
ASSERT(phdr[i].p_type != PT_DYNAMIC); ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) { if (phdr[i].p_type == PT_LOAD) {
if (phdr[i].p_filesz != 0) { if (phdr[i].p_filesz != 0) {
Log(loader, "Read %lx %lx %lx\n", phdr[i].p_vaddr, phdr[i].p_offset, phdr[i].p_filesz);
VFS_Read(vn, (void *)phdr[i].p_vaddr, VFS_Read(vn, (void *)phdr[i].p_vaddr,
phdr[i].p_offset, phdr[i].p_filesz); phdr[i].p_offset, phdr[i].p_filesz);
} }
@ -95,6 +96,8 @@ Loader_LoadFirst(Thread *thr, VNode *vn, void *buf, uint64_t len)
} }
} }
Log(loader, "Jumping to userspace\n");
TrapFrame tf; TrapFrame tf;
memset(&tf, 0, sizeof(tf)); memset(&tf, 0, sizeof(tf));
tf.ds = SEL_UDS | 3; tf.ds = SEL_UDS | 3;
@ -216,26 +219,26 @@ Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len)
phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff); phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff);
if (!Loader_CheckHeader(ehdr)) { if (!Loader_CheckHeader(ehdr)) {
kprintf("Not a valid executable!\n"); Log(loader, "Not a valid executable!\n");
return false; return false;
} }
kprintf("%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize"); Log(loader, "%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
for (i = 0; i < ehdr->e_phnum; i++) for (i = 0; i < ehdr->e_phnum; i++)
{ {
ASSERT(phdr[i].p_type != PT_DYNAMIC); ASSERT(phdr[i].p_type != PT_DYNAMIC);
if (phdr[i].p_type == PT_LOAD) { if (phdr[i].p_type == PT_LOAD) {
uint64_t va = phdr[i].p_vaddr; uint64_t va = phdr[i].p_vaddr;
uint64_t memsz = phdr[i].p_memsz; uint64_t memsz = phdr[i].p_memsz;
kprintf("%08llx %016llx %08llx %08llx\n", phdr[i].p_offset, Log(loader, "%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz); phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
// Make sure it is page aligned // Make sure it is page aligned
va = va & ~(uint64_t)PGMASK; va = va & ~(uint64_t)PGMASK;
memsz += phdr[i].p_vaddr - va; memsz += phdr[i].p_vaddr - va;
kprintf("%016llx %08llx\n", va, memsz); Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, phdr[i].p_memsz, PTE_W)) { if (!PMap_AllocMap(as, va, memsz, PTE_W)) {
// XXX: Cleanup! // XXX: Cleanup!
ASSERT(false); ASSERT(false);
return false; return false;